本文整理汇总了Golang中C.av_read_frame函数的典型用法代码示例。如果您正苦于以下问题:Golang av_read_frame函数的具体用法?Golang av_read_frame怎么用?Golang av_read_frame使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了av_read_frame函数的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Read
func (id *Capture) Read(frame *C.AVFrame) error {
if frame == (*C.AVFrame)(null) {
return fmt.Errorf("buffer error")
}
pkt := C.AVPacket{}
C.av_init_packet(&pkt)
defer C.av_free_packet(&pkt)
if C.av_read_frame(id.context, &pkt) < 0 {
return fmt.Errorf("read frame error")
}
if int(pkt.stream_index) != id.index {
return fmt.Errorf("not video frame")
}
got_frame := C.int(0)
if C.avcodec_decode_video2(id.codec, id.frame, &got_frame, &pkt) < 0 {
return fmt.Errorf("decode frame error")
}
if got_frame != 0 {
if C.sws_scale(id.sws, (**C.uint8_t)(&(id.frame.data[0])), &id.frame.linesize[0], 0, id.codec.height, &frame.data[0], &frame.linesize[0]) >= 0 {
return nil
}
return fmt.Errorf("scale error")
}
return fmt.Errorf("no frame out")
}
示例2: Start
func (dpx *Demultiplexer) Start() {
for true {
dpx.Packet = new(C.AVPacket)
C.av_init_packet(dpx.Packet)
if C.av_read_frame(dpx.Ds.Ctx, dpx.Packet) >= 0 {
C.av_dup_packet(dpx.Packet)
} else {
println("end of file reached, closing channels")
for i := 0; i < len(*dpx.tracks); i++ {
print("closing channel ")
println(i)
close((*dpx.tracks)[i].stream)
}
break
}
var re Packet
re.Pts = int64(dpx.Packet.pts)
re.Dts = int64(dpx.Packet.dts)
re.Size = int(dpx.Packet.size)
re.Data = make([]byte, re.Size)
data := (*(*[1 << 30]byte)(unsafe.Pointer(dpx.Packet.data)))[0:dpx.Packet.size]
for i := 0; i < re.Size; i++ {
re.Data[i] = data[i]
}
re.Stream = int(dpx.Packet.stream_index)
re.Flags = int(dpx.Packet.flags)
re.Duration = int(dpx.Packet.duration)
re.Pos = int64(dpx.Packet.pos)
C.av_free_packet(dpx.Packet)
(*dpx.tracks)[re.Stream].stream <- re
}
//re * Packet
}
示例3: ReadFrame
func (ctx *Context) ReadFrame(pkt *avcodec.Packet) (bool, error) {
cPkt := (*C.AVPacket)(unsafe.Pointer(&pkt.CAVPacket))
code := C.av_read_frame(ctx.CAVFormatContext, cPkt)
if code < 0 {
if avutil.ErrorCode(code) == avutil.ErrorCodeEOF {
return false, nil
}
return false, avutil.NewErrorFromCode(avutil.ErrorCode(code))
}
return true, nil
}
示例4: GetNextPacket
func (this *FmtCtx) GetNextPacket() *Packet {
p := NewPacket()
for {
if ret := C.av_read_frame(this.avCtx, &p.avPacket); int(ret) < 0 {
Release(p)
return nil
}
return p
}
}
示例5: NextFrame
func (g *Generator) NextFrame() (image.Image, int64, error) {
img := image.NewRGBA(image.Rect(0, 0, g.Width, g.Height))
frame := C.av_frame_alloc()
var pkt C.struct_AVPacket
var frameFinished C.int
for C.av_read_frame(g.avfContext, &pkt) == 0 {
if int(pkt.stream_index) != g.vStreamIndex {
C.av_free_packet(&pkt)
continue
}
if C.avcodec_decode_video2(g.avcContext, frame, &frameFinished, &pkt) <= 0 {
C.av_free_packet(&pkt)
return nil, 0, errors.New("can't decode frame")
}
C.av_free_packet(&pkt)
if frameFinished == 0 {
continue
}
ctx := C.sws_getContext(
C.int(g.Width),
C.int(g.Height),
g.avcContext.pix_fmt,
C.int(g.Width),
C.int(g.Height),
C.PIX_FMT_RGBA,
C.SWS_BICUBIC,
nil,
nil,
nil,
)
if ctx == nil {
return nil, 0, errors.New("can't allocate scaling context")
}
srcSlice := (**C.uint8_t)(&frame.data[0])
srcStride := (*C.int)(&frame.linesize[0])
dst := (**C.uint8_t)(unsafe.Pointer(&img.Pix))
dstStride := (*C.int)(unsafe.Pointer(&[1]int{img.Stride}))
C.sws_scale(
ctx,
srcSlice,
srcStride,
C.int(0),
g.avcContext.height,
dst,
dstStride,
)
break
}
timestamp := int64(C.av_frame_get_best_effort_timestamp(frame))
return img, timestamp, nil
}
示例6: GetNewPackets
func (this *FmtCtx) GetNewPackets() chan *Packet {
yield := make(chan *Packet)
go func() {
for {
p := NewPacket()
if ret := C.av_read_frame(this.avCtx, &p.avPacket); int(ret) < 0 {
break
}
yield <- p
}
close(yield)
}()
return yield
}
示例7: StartDecoding
func (file *MediaFile) StartDecoding() {
for _, i := range file.DecodedStreams {
go file.Streams[i].decode()
}
go func() {
get_packets:
for packet := range file.packets {
if packet == nil {
break get_packets
}
for _, i := range file.DecodedStreams {
if packet.stream_index == C.int(i) {
file.Streams[i].packets <- packet
continue get_packets
}
}
C.av_free_packet(packet)
}
for _, i := range file.DecodedStreams {
file.Streams[i].packets <- nil
}
}()
go func() {
for {
packet := new(C.AVPacket)
C.av_init_packet(packet)
if C.av_read_frame(file.fmtctx, packet) < 0 {
// assume EOF
file.packets <- nil
return
}
file.packets <- packet
}
}()
}
示例8: ReadPacket
func (dpx *Demultiplexer) ReadPacket(re *Packet) bool {
dpx.Packet = new(C.AVPacket)
C.av_init_packet(dpx.Packet)
defer C.av_free_packet(dpx.Packet)
if C.av_read_frame(dpx.Ds.Ctx, dpx.Packet) >= 0 {
C.av_dup_packet(dpx.Packet)
} else {
return false
}
re.Pts = int64(dpx.Packet.pts)
re.Dts = int64(dpx.Packet.dts)
re.Size = int(dpx.Packet.size)
re.Data = make([]byte, re.Size)
data := (*(*[1 << 30]byte)(unsafe.Pointer(dpx.Packet.data)))[0:dpx.Packet.size]
for i := 0; i < re.Size; i++ {
re.Data[i] = data[i]
}
re.Stream = int(dpx.Packet.stream_index)
re.Flags = int(dpx.Packet.flags)
re.Duration = int(dpx.Packet.duration)
re.Pos = int64(dpx.Packet.pos)
return true
}
示例9: av_read_frame
func av_read_frame(ctx *FormatContext, packet *AVPacket) int {
return int(C.av_read_frame(ctx.ctx, (*C.AVPacket)(unsafe.Pointer(packet))))
}
示例10: main
func main() {
var (
fmt_ctx *C.AVFormatContext
video_stream_idx C.int
pkt C.AVPacket
fn string
)
flag.StringVar(&fn, "i", fn, "Input filename")
flag.Parse()
if fn == "" {
flag.PrintDefaults()
os.Exit(1)
}
cfn := C.CString(fn)
defer C.free(unsafe.Pointer(cfn))
C.av_register_all()
if err := C.avformat_open_input(&fmt_ctx, cfn, nil, nil); err < 0 {
log.Fatalf("Could not open source file %s, %d\n", fn, err)
}
// The smd codecs aren't too happy with missing PTS
fmt_ctx.flags |= C.AVFMT_FLAG_GENPTS
defer C.avformat_close_input(&fmt_ctx)
if err := C.avformat_find_stream_info(fmt_ctx, nil); err < 0 {
log.Fatalf("Could not find stream information: %d", err)
}
if err := open_codec_context(&video_stream_idx, fmt_ctx, C.AVMEDIA_TYPE_VIDEO); err < 0 {
log.Fatalf("Could not open codec context: %d", err)
}
log.Printf("fmt_ctx: %+v", fmt_ctx)
streams := (*[32]*C.AVStream)(unsafe.Pointer(fmt_ctx.streams))
log.Printf("video stream codec: %+v", streams[video_stream_idx].codec.codec_id)
log.Printf("time_base: %+v", streams[video_stream_idx].time_base)
num := 1000000 * float64(streams[video_stream_idx].time_base.num)
den := float64(streams[video_stream_idx].time_base.den)
var codec C.ismd_codec_type_t
switch vc := streams[video_stream_idx].codec.codec_id; vc {
case C.AV_CODEC_ID_H264:
codec = C.ISMD_CODEC_TYPE_H264
case C.AV_CODEC_ID_MPEG1VIDEO:
fallthrough
case C.AV_CODEC_ID_MPEG2VIDEO:
codec = C.ISMD_CODEC_TYPE_MPEG2
case C.AV_CODEC_ID_MPEG4:
codec = C.ISMD_CODEC_TYPE_MPEG4
default:
log.Fatalf("Unhandled video codec: %d", vc)
}
Init(codec, C.GDL_PLANE_ID_UPP_C)
defer Destroy()
C.av_init_packet(&pkt)
pkt.data = nil
pkt.size = 0
running := true
go func() {
os.Stdin.Read(make([]byte, 1))
running = false
}()
frame := 0
for running && C.av_read_frame(fmt_ctx, &pkt) >= 0 {
orig_pkt := pkt
wrote := false
for pkt.stream_index == video_stream_idx && (pkt.size > 0) {
pts := num * float64(pkt.pts) / den
WriteToInputPort(uintptr(unsafe.Pointer(pkt.data)), C.size_t(pkt.size), pts, 32*1024)
wrote = true
break
}
if wrote {
frame++
if frame%100 == 0 {
var stat C.ismd_vidrend_stats_t
C.ismd_vidrend_get_stats(m_video_render, &stat)
log.Printf("%+v", stat)
}
}
C.av_free_packet(&orig_pkt)
}
}
示例11: AvReadFrame
//Return the next frame of a stream.
func (s *Context) AvReadFrame(pkt *avcodec.Packet) int {
return int(C.av_read_frame((*C.struct_AVFormatContext)(unsafe.Pointer(s)), (*C.struct_AVPacket)(unsafe.Pointer(pkt))))
}
示例12: Av_read_frame
//int av_read_frame (AVFormatContext *s, AVPacket *pkt)
//Return the next frame of a stream.
func Av_read_frame(s *AVFormatContext, pkt *avcodec.AVPacket) int {
return int(C.av_read_frame((*C.struct_AVFormatContext)(unsafe.Pointer(s)), (*C.struct_AVPacket)(unsafe.Pointer(pkt))))
}
示例13: ImageWxH
// ImageWxH returns a screenshot at the ts milliseconds, scaled to the specified width and height.
func (g *Generator) ImageWxH(ts int64, width, height int) (image.Image, error) {
img := image.NewRGBA(image.Rect(0, 0, width, height))
frame := C.av_frame_alloc()
defer C.av_frame_free(&frame)
frameNum := C.av_rescale(
C.int64_t(ts),
C.int64_t(g.streams[g.vStreamIndex].time_base.den),
C.int64_t(g.streams[g.vStreamIndex].time_base.num),
) / 1000
if C.avformat_seek_file(
g.avfContext,
C.int(g.vStreamIndex),
0,
frameNum,
frameNum,
C.AVSEEK_FLAG_FRAME,
) < 0 {
return nil, errors.New("can't seek to timestamp")
}
C.avcodec_flush_buffers(g.avcContext)
var pkt C.struct_AVPacket
var frameFinished C.int
for C.av_read_frame(g.avfContext, &pkt) == 0 {
if int(pkt.stream_index) != g.vStreamIndex {
C.av_free_packet(&pkt)
continue
}
if C.avcodec_decode_video2(g.avcContext, frame, &frameFinished, &pkt) <= 0 {
C.av_free_packet(&pkt)
return nil, errors.New("can't decode frame")
}
C.av_free_packet(&pkt)
if frameFinished == 0 || pkt.dts < frameNum {
continue
}
ctx := C.sws_getContext(
C.int(g.Width),
C.int(g.Height),
g.avcContext.pix_fmt,
C.int(width),
C.int(height),
C.PIX_FMT_RGBA,
C.SWS_BICUBIC,
nil,
nil,
nil,
)
if ctx == nil {
return nil, errors.New("can't allocate scaling context")
}
srcSlice := (**C.uint8_t)(&frame.data[0])
srcStride := (*C.int)(&frame.linesize[0])
dst := (**C.uint8_t)(unsafe.Pointer(&img.Pix))
dstStride := (*C.int)(unsafe.Pointer(&[1]int{img.Stride}))
C.sws_scale(
ctx,
srcSlice,
srcStride,
0,
g.avcContext.height,
dst,
dstStride,
)
break
}
return img, nil
}
示例14: Start
func (self *Decoder) Start(videoStream, audioStream *C.AVStream,
scaleWidth, scaleHeight C.int) *Decoder {
self.running = true
self.duration = time.Duration(self.FormatContext.duration * C.AV_TIME_BASE / 1000)
vCodecCtx := videoStream.codec
aCodecCtx := audioStream.codec
self.durationPerSample = time.Second / time.Duration(aCodecCtx.sample_rate)
// frame pool
poolSize := 16
pool := make(chan *C.AVFrame, poolSize)
self.pool = pool
numBytes := C.size_t(C.avpicture_get_size(C.PIX_FMT_YUV420P, scaleWidth, scaleHeight))
for i := 0; i < poolSize; i++ {
frame := C.av_frame_alloc()
self.frames = append(self.frames, frame)
buffer := (*C.uint8_t)(unsafe.Pointer(C.av_malloc(numBytes)))
self.buffers = append(self.buffers, buffer)
C.avpicture_fill((*C.AVPicture)(unsafe.Pointer(frame)), buffer, C.PIX_FMT_YUV420P,
scaleWidth, scaleHeight)
pool <- frame
}
// decode
self.frameChan = make(chan *C.AVFrame, 512)
go func() {
runtime.LockOSThread()
// scale context
scaleContext := C.sws_getCachedContext(nil, vCodecCtx.width, vCodecCtx.height, vCodecCtx.pix_fmt,
scaleWidth, scaleHeight, C.PIX_FMT_YUV420P, C.SWS_LANCZOS, nil, nil, nil)
if scaleContext == nil {
log.Fatal("get scale context failed")
}
// resample context
resampleContext := C.swr_alloc_set_opts(nil,
C.AV_CH_LAYOUT_STEREO, C.AV_SAMPLE_FMT_FLT, aCodecCtx.sample_rate,
C.int64_t(aCodecCtx.channel_layout), aCodecCtx.sample_fmt, aCodecCtx.sample_rate,
0, nil)
if resampleContext == nil {
log.Fatal("get resample context failed")
}
C.swr_init(resampleContext)
var packet C.AVPacket
var frameFinished C.int
var pts int64
var packetTime time.Duration
vFrame := C.av_frame_alloc()
aFrame := C.av_frame_alloc()
videoIndex := videoStream.index
audioIndex := audioStream.index
resampleBuffer := (*C.uint8_t)(C.av_malloc(4096 * 8))
resampleBufferp := &resampleBuffer
self.Timer = NewTimer()
// decode
for self.running {
// seek
if self.seekTarget > 0 {
if C.av_seek_frame(self.FormatContext, -1,
C.int64_t(float64(self.seekNext)/float64(time.Second)*float64(C.AV_TIME_BASE)),
C.AVSEEK_FLAG_BACKWARD) < 0 {
log.Fatal("seek error")
}
for _, codecCtx := range self.openedCodecs {
C.avcodec_flush_buffers(codecCtx)
}
p("frame seek done\n")
}
read_packet:
// read packet
C.av_free_packet(&packet)
if C.av_read_frame(self.FormatContext, &packet) < 0 { // read packet
log.Fatal("read frame error") //TODO stop gracefully
}
// get packet time
if packet.dts != C.AV_NOPTS_VALUE {
pts = int64(packet.dts)
} else {
pts = 0
}
if packet.stream_index == videoIndex {
packetTime = time.Duration(float64(pts) * float64(C.av_q2d(videoStream.time_base)) * float64(time.Second))
} else if packet.stream_index == audioIndex {
packetTime = time.Duration(float64(pts) * float64(C.av_q2d(audioStream.time_base)) * float64(time.Second))
} else { // ignore packet
goto read_packet
}
p("packet time %v at timer time %v\n", packetTime, self.Timer.Now())
// check seek
if self.seekTarget > 0 && packetTime > 0 { // if packet time cannot determined, skip
//.........这里部分代码省略.........