Exemple #1
0
func (file *MediaFile) Close() {
	for i := range file.Streams {
		file.Streams[i].freeCodecContext()
	}

	if file.fmtctx != nil {
		C.avformat_close_input(&file.fmtctx)
	}

	runtime.SetFinalizer(file, nil)
}
Exemple #2
0
func (self *Decoder) Close() {
	C.avformat_close_input(&self.FormatContext)
	for _, stream := range self.Streams {
		C.avcodec_close(stream.codec)
	}
	for _, frame := range self.frames {
		C.av_frame_free(&frame)
	}
	for _, buffer := range self.buffers {
		C.av_free(unsafe.Pointer(buffer))
	}
	close(self.frameChan)
	self.running = false
}
Exemple #3
0
func (this *FmtCtx) CloseInputAndRelease() {
	C.avformat_close_input(&this.avCtx)
	Release(this)
}
Exemple #4
0
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)
	}
}
Exemple #5
0
//Close an opened input Context.
func (s *Context) AvformatCloseInput() {
	C.avformat_close_input((**C.struct_AVFormatContext)(unsafe.Pointer(&s)))
}
Exemple #6
0
func (this *FmtCtx) CloseInput() {
	C.avformat_close_input(&this.avCtx)
	this.Free()
}
Exemple #7
0
//void avformat_close_input (AVFormatContext **s)
//Close an opened input AVFormatContext.
func Avformat_close_input(s *AVFormatContext) {
	C.avformat_close_input((**C.struct_AVFormatContext)(unsafe.Pointer(s)))
}
Exemple #8
0
func (id *Capture) Close() {
	C.av_frame_free(&(id.frame))
	C.avcodec_close(id.codec)
	C.avformat_close_input(&(id.context))
	C.sws_freeContext(id.sws)
}
// NewGenerator returns new generator of screenshots for the video file fn.
func NewGenerator(fn string) (_ *Generator, err error) {
	avfCtx := C.avformat_alloc_context()
	cfn := C.CString(fn)
	defer C.free(unsafe.Pointer(cfn))
	if C.avformat_open_input(&avfCtx, cfn, nil, nil) != 0 {
		return nil, errors.New("can't open input stream")
	}
	defer func() {
		if err != nil {
			C.avformat_close_input(&avfCtx)
		}
	}()
	if C.avformat_find_stream_info(avfCtx, nil) < 0 {
		return nil, errors.New("can't get stream info")
	}
	duration := int64(avfCtx.duration) / 1000
	bitrate := int(avfCtx.bit_rate) / 1000
	numberOfStreams := int(avfCtx.nb_streams)
	hdr := reflect.SliceHeader{
		Data: uintptr(unsafe.Pointer(avfCtx.streams)),
		Len:  numberOfStreams,
		Cap:  numberOfStreams,
	}
	streams := *(*[]*C.struct_AVStream)(unsafe.Pointer(&hdr))
	vStreamIndex := -1
	aStreamIndex := -1
	for i := 0; i < numberOfStreams; i++ {
		if streams[i].codec.codec_type == C.AVMEDIA_TYPE_VIDEO {
			vStreamIndex = i
		} else if streams[i].codec.codec_type == C.AVMEDIA_TYPE_AUDIO {
			aStreamIndex = i
		}
	}
	if vStreamIndex == -1 {
		return nil, errors.New("no video stream")
	}
	avcCtx := streams[vStreamIndex].codec
	vCodec := C.avcodec_find_decoder(avcCtx.codec_id)
	if vCodec == nil {
		return nil, errors.New("can't find decoder")
	}
	if C.avcodec_open2(avcCtx, vCodec, nil) != 0 {
		return nil, errors.New("can't initialize codec context")
	}
	width := int(avcCtx.width)
	height := int(avcCtx.height)
	fps := (float64(streams[vStreamIndex].r_frame_rate.num) /
		float64(streams[vStreamIndex].r_frame_rate.den))
	vCodecName := strings.ToUpper(C.GoString(vCodec.name))
	vCodecHuman := C.GoString(vCodec.long_name)

	aCodecName := ""
	aCodecHuman := ""
	if aStreamIndex != -1 {
		aacCtx := streams[aStreamIndex].codec
		aCodec := C.avcodec_find_decoder(aacCtx.codec_id)
		if aCodec != nil {
			aCodecName = strings.ToUpper(C.GoString(aCodec.name))
			aCodecHuman = C.GoString(aCodec.long_name)
		}
	}

	return &Generator{
		Width:              width,
		Height:             height,
		Duration:           duration,
		VideoCodec:         vCodecName,
		VideoCodecLongName: vCodecHuman,
		AudioCodec:         aCodecName,
		AudioCodecLongName: aCodecHuman,
		numberOfStreams:    numberOfStreams,
		vStreamIndex:       vStreamIndex,
		aStreamIndex:       aStreamIndex,
		FPS:                fps,
		Bitrate:            bitrate,
		streams:            streams,
		avfContext:         avfCtx,
		avcContext:         avcCtx,
	}, nil
}
// Close closes the internal ffmpeg context.
func (g *Generator) Close() error {
	C.avformat_close_input(&g.avfContext)
	return nil
}
Exemple #11
0
func (ctx *Context) CloseInput() {
	C.avformat_close_input(&ctx.CAVFormatContext)
}