Beispiel #1
0
func (stream *Stream) init() error {
	// need to allocate this first so that it can be closed on error
	stream.Frames = make(chan *Frame)

	if stream.avstream == nil {
		close(stream.Frames)
		return errors.New("nil avstream")
	}

	if stream.cdcctx != nil {
		stream.freeCodecContext()
	}
	stream.cdcctx = stream.avstream.codec

	if decoder := C.avcodec_find_decoder(stream.cdcctx.codec_id); decoder == nil || C.avcodec_open2(stream.cdcctx, decoder, nil) < 0 {
		stream.cdcctx = nil
		close(stream.Frames)
		return errors.New("Cannot find decoder for " + C.GoString(C.avcodec_get_name(stream.cdcctx.codec_id)))
	}

	stream.packets = make(chan *C.AVPacket)
	stream.frame = &Frame{}
	switch stream.avstream.codec.codec_type {
	case C.AVMEDIA_TYPE_AUDIO:
		stream.decodeF = avcodec_decode_audio
	case C.AVMEDIA_TYPE_VIDEO:
		stream.decodeF = avcodec_decode_video
	default:
		stream.freeCodecContext()
		close(stream.Frames)
		return errors.New("unsupported codec")
	}

	return nil
}
Beispiel #2
0
//Get the name of a codec.
func AvcodecGetName(d CodecId) string {
	return C.GoString(C.avcodec_get_name((C.enum_AVCodecID)(d)))
}