本文整理匯總了Golang中C.av_free_packet函數的典型用法代碼示例。如果您正苦於以下問題:Golang av_free_packet函數的具體用法?Golang av_free_packet怎麽用?Golang av_free_packet使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了av_free_packet函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: writeAudioFrame
func (m *Muxer) writeAudioFrame(frame *C.AVFrame) bool {
for C.av_audio_fifo_size(m.fifo) < 1024 { // generate & store in fifo
C.fill_audio_frame(frame, m.audioStream.stream.codec)
frame_size := frame.nb_samples
if C.av_audio_fifo_realloc(m.fifo, C.av_audio_fifo_size(m.fifo)+frame_size) < 0 {
return false
}
if C.av_audio_fifo_write(m.fifo, (*unsafe.Pointer)(unsafe.Pointer(&frame.data[0])), frame_size) < frame_size {
return false
}
}
got_packet := C.int(0)
for C.av_audio_fifo_size(m.fifo) >= 1024 { // read & encode & write
frame_size := C.min(C.av_audio_fifo_size(m.fifo), m.audioStream.stream.codec.frame_size)
output_frame := C.alloc_audio_frame(m.audioStream.stream.codec)
if C.av_audio_fifo_read(m.fifo, (*unsafe.Pointer)(unsafe.Pointer(&output_frame.data[0])), frame_size) < frame_size {
C.av_frame_free(&output_frame)
return false
}
pkt := C.AVPacket{}
C.av_init_packet(&pkt)
output_frame.pts = C.int64_t(m.audioStream.ts)
m.audioStream.ts += int(m.audioStream.stream.codec.frame_size)
if C.avcodec_encode_audio2(m.audioStream.stream.codec, &pkt, frame, &got_packet) < 0 {
C.av_free_packet(&pkt)
return false
}
if got_packet == 0 {
continue
}
C.av_packet_rescale_ts(&pkt, m.audioStream.stream.codec.time_base, m.audioStream.stream.time_base)
pkt.stream_index = m.audioStream.stream.index
if C.av_interleaved_write_frame(m.context, &pkt) < 0 {
return false
}
}
return true
}
示例2: 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
}
示例3: 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
}
示例4: 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")
}
示例5: Encode
func (m *H264Encoder) Encode(img *image.YCbCr) (out h264Out, err error) {
var f *C.AVFrame
if img == nil {
f = nil
} else {
if img.SubsampleRatio != m.Pixfmt {
err = errors.New("image pixfmt not match")
return
}
if img.Rect.Dx() != m.W || img.Rect.Dy() != m.H {
err = errors.New("image size not match")
return
}
f = m.m.f
f.data[0] = (*C.uint8_t)(unsafe.Pointer(&img.Y[0]))
f.data[1] = (*C.uint8_t)(unsafe.Pointer(&img.Cb[0]))
f.data[2] = (*C.uint8_t)(unsafe.Pointer(&img.Cr[0]))
f.linesize[0] = (C.int)(img.YStride)
f.linesize[1] = (C.int)(img.CStride)
f.linesize[2] = (C.int)(img.CStride)
}
C.av_init_packet(&m.m.pkt)
r := C.avcodec_encode_video2(m.m.ctx, &m.m.pkt, f, &m.m.got)
defer C.av_free_packet(&m.m.pkt)
if int(r) < 0 {
err = errors.New("encode failed")
return
}
if m.m.got == 0 {
err = errors.New("no picture")
return
}
if m.m.pkt.size == 0 {
err = errors.New("packet size == 0")
return
}
out.Data = make([]byte, m.m.pkt.size)
C.memcpy(
unsafe.Pointer(&out.Data[0]),
unsafe.Pointer(m.m.pkt.data),
(C.size_t)(m.m.pkt.size),
)
out.Key = (m.m.pkt.flags & C.AV_PKT_FLAG_KEY) != 0
return
}
示例6: decode
func (stream *Stream) decode() {
for packet := range stream.packets {
if packet == nil {
close(stream.Frames)
return
}
stream.frame.Defaults()
gotFrame, err := stream.decodeF(stream.cdcctx, stream.frame, packet)
C.av_free_packet(packet)
if err != nil {
// ignore frame
continue
}
if gotFrame {
stream.Frames <- stream.frame
}
}
}
示例7: decodeAudio
func (c *Decoder) decodeAudio(p Packet) *Frame {
packet := new(C.AVPacket)
C.av_init_packet(packet)
defer C.av_free_packet(packet)
packet.pts = C.int64_t(p.Pts)
packet.dts = C.int64_t(p.Dts)
packet.size = C.int(p.Size)
packet.data = (*C.uint8_t)(unsafe.Pointer(&p.Data[0]))
packet.stream_index = C.int(p.Stream)
packet.flags = C.int(p.Flags)
packet.duration = C.int(p.Duration)
packet.pos = C.int64_t(p.Pos)
//size:=packet.size;
samples_size := C.int(C.AVCODEC_MAX_AUDIO_FRAME_SIZE)
//bps := C.av_get_bits_per_sample_fmt(c.Ctx.sample_fmt) >> 3;
outbuf := (*C.uint8_t)(C.av_malloc(C.uint(samples_size)))
defer C.av_free(unsafe.Pointer(outbuf))
C.avcodec_decode_audio3(c.Ctx, (*C.int16_t)(unsafe.Pointer(outbuf)), &samples_size, packet)
//println(data_len)
return nil
}
示例8: writeVideoFrame
func (m *Muxer) writeVideoFrame(frame *C.AVFrame) bool {
if m.capture.Read(frame) != nil {
return false
}
if m.display != nil {
m.display.Render(frame)
}
pkt := C.AVPacket{}
C.av_init_packet(&pkt)
frame.pts = C.int64_t(m.videoStream.ts)
m.videoStream.ts++
got_packet := C.int(0)
if C.avcodec_encode_video2(m.videoStream.stream.codec, &pkt, frame, &got_packet) < 0 {
C.av_free_packet(&pkt)
return false
}
if got_packet == 0 {
return false
}
C.av_packet_rescale_ts(&pkt, m.videoStream.stream.codec.time_base, m.videoStream.stream.time_base)
pkt.stream_index = m.videoStream.stream.index
return C.av_interleaved_write_frame(m.context, &pkt) == 0
}
示例9: 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
}
}()
}
示例10: 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
}
示例11: av_free_packet2
func av_free_packet2(p *avPacket) {
if p != nil {
C.av_free_packet((*C.AVPacket)(unsafe.Pointer(p)))
p = nil
}
}
示例12: av_free_packet
func av_free_packet(p *Packet) {
if p.avpacket != nil {
C.av_free_packet(p.avpacket)
p.avpacket = nil
}
}
示例13: AvFreePacket
//Free a packet.
func (p *Packet) AvFreePacket() {
C.av_free_packet((*C.struct_AVPacket)(p))
}
示例14: Free
func (this *Packet) Free() {
C.av_free_packet(&this.avPacket)
}
示例15: 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)
}
}