本文整理匯總了Golang中C.av_frame_alloc函數的典型用法代碼示例。如果您正苦於以下問題:Golang av_frame_alloc函數的具體用法?Golang av_frame_alloc怎麽用?Golang av_frame_alloc使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了av_frame_alloc函數的11個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: NewFrame
func NewFrame() (*Frame, error) {
cFrame := C.av_frame_alloc()
if cFrame == nil {
return nil, ErrAllocationError
}
return NewFrameFromC(unsafe.Pointer(cFrame)), nil
}
示例2: Decode
// @todo should be private
func (this *Packet) Decode(cc *CodecCtx) (*Frame, bool, int, error) {
var gotOutput int
var ret int = 0
if frames[cc.Type()] == nil {
frames[cc.Type()] = &Frame{avFrame: C.av_frame_alloc(), mediaType: cc.Type()}
}
switch cc.Type() {
case AVMEDIA_TYPE_AUDIO:
ret = int(C.avcodec_decode_audio4(cc.avCodecCtx, frames[AVMEDIA_TYPE_AUDIO].avFrame, (*C.int)(unsafe.Pointer(&gotOutput)), &this.avPacket))
if ret < 0 {
return nil, false, int(ret), errors.New(fmt.Sprintf("Unable to decode audio packet, averror: %s", AvError(int(ret))))
}
break
case AVMEDIA_TYPE_VIDEO:
ret = int(C.avcodec_decode_video2(cc.avCodecCtx, frames[AVMEDIA_TYPE_VIDEO].avFrame, (*C.int)(unsafe.Pointer(&gotOutput)), &this.avPacket))
if ret < 0 {
return nil, false, int(ret), errors.New(fmt.Sprintf("Unable to decode video packet, averror: %s", AvError(int(ret))))
}
break
default:
return nil, false, int(ret), errors.New(fmt.Sprintf("Unknown codec type: %v", cc.Type()))
}
return frames[cc.Type()], (gotOutput > 0), int(ret), nil
}
示例3: NewCapture
func NewCapture(driver, device string) (*Capture, error) {
id := Capture{index: -1}
id.context = C.avformat_alloc_context()
if id.context == (*C.AVFormatContext)(null) {
return nil, fmt.Errorf("allocate output format context failed")
}
_driver := C.CString(driver)
defer C.free(unsafe.Pointer(_driver))
ifmt := C.av_find_input_format(_driver)
if ifmt == (*C.AVInputFormat)(null) {
return nil, fmt.Errorf("cannot find input driver: %s", driver)
}
dev := C.CString(device)
defer C.free(unsafe.Pointer(dev))
if C.avformat_open_input(&(id.context), dev, ifmt, (**C.AVDictionary)(null)) < 0 {
return nil, fmt.Errorf("cannot open device %s", device)
}
if C.avformat_find_stream_info(id.context, (**C.AVDictionary)(null)) < 0 {
return nil, fmt.Errorf("cannot find stream information")
}
num := int(id.context.nb_streams)
streams := (*[1 << 30]*C.AVStream)(unsafe.Pointer(id.context.streams))
var deCtx *C.AVCodecContext
for i := 0; i < num; i++ {
if streams[i].codec.codec_type == C.AVMEDIA_TYPE_VIDEO {
deCtx = streams[i].codec
id.index = i
break
}
}
if id.index == -1 {
return nil, fmt.Errorf("cannot find video stream")
}
codec := C.avcodec_find_decoder(deCtx.codec_id)
if codec == (*C.AVCodec)(null) {
return nil, fmt.Errorf("cannot find decode codec")
}
id.codec = C.avcodec_alloc_context3(codec)
if C.avcodec_copy_context(id.codec, deCtx) != 0 {
return nil, fmt.Errorf("cannot copy codec context")
}
if C.avcodec_open2(id.codec, codec, (**C.struct_AVDictionary)(null)) < 0 {
return nil, fmt.Errorf("cannot open decode codec")
}
id.sws = C.sws_getContext(id.codec.width,
id.codec.height,
id.codec.pix_fmt,
id.codec.width,
id.codec.height,
C.AV_PIX_FMT_YUV420P, C.SWS_BILINEAR, (*C.struct_SwsFilter)(null), (*C.struct_SwsFilter)(null), (*C.double)(null))
id.frame = C.av_frame_alloc()
return &id, nil
}
示例4: 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
}
示例5: Decode
// @todo should be private
func (this *Packet) Decode(cc *CodecCtx) (*Frame, bool, int, error) {
var gotOutput int
var ret int = 0
if frames[cc.Type()] == nil {
frames[cc.Type()] = &Frame{avFrame: C.av_frame_alloc(), mediaType: cc.Type()}
}
switch cc.Type() {
case AVMEDIA_TYPE_AUDIO:
ret = int(C.avcodec_decode_audio4(cc.avCodecCtx, frames[AVMEDIA_TYPE_AUDIO].avFrame, (*C.int)(unsafe.Pointer(&gotOutput)), &this.avPacket))
if ret < 0 {
return nil, false, int(ret), errors.New(fmt.Sprintf("Unable to decode audio packet, averror: %s", AvError(int(ret))))
}
break
case AVMEDIA_TYPE_VIDEO:
ret = int(C.avcodec_decode_video2(cc.avCodecCtx, frames[AVMEDIA_TYPE_VIDEO].avFrame, (*C.int)(unsafe.Pointer(&gotOutput)), &this.avPacket))
if ret < 0 {
return nil, false, int(ret), errors.New(fmt.Sprintf("Unable to decode video packet, averror: %s", AvError(int(ret))))
}
break
default:
return nil, false, int(ret), errors.New(fmt.Sprintf("Unknown codec type: %v", cc.Type()))
}
defer func() {
if r := recover(); r != nil {
buf := make([]byte, 1<<16)
runtime.Stack(buf, false)
reason := fmt.Sprintf("%v: %s", r, buf)
fmt.Println("Runtime failure, reason -> %s", reason)
}
}()
return frames[cc.Type()], (gotOutput > 0), int(ret), nil
}
示例6: DecodeToNewFrame
func (this *Packet) DecodeToNewFrame(cc *CodecCtx) (*Frame, bool, int, error) {
f := &Frame{avFrame: C.av_frame_alloc(), mediaType: cc.Type()}
return this.decode(cc, f)
}
示例7: NewFrame
func NewFrame() *Frame {
return &Frame{avFrame: C.av_frame_alloc()}
}
示例8: 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
}
示例9: Av_frame_alloc
//AVFrame * av_frame_alloc (void)
//Allocate an AVFrame and set its fields to default values.
func Av_frame_alloc() *AVFrame {
return (*AVFrame)(unsafe.Pointer(C.av_frame_alloc()))
}
示例10: AvFrameAlloc
//Allocate an Frame and set its fields to default values.
func AvFrameAlloc() *Frame {
return (*Frame)(unsafe.Pointer(C.av_frame_alloc()))
}
示例11: 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
//.........這裏部分代碼省略.........