예제 #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Decoder"/> class.
        /// </summary>
        /// <param name="codec">The underlying codec.</param>
        /// <param name="stream">The multimedia stream.</param>
        /// <param name="owner">The container that owns the stream.</param>
        public Decoder(AVCodecContext *codec, AVStream *stream, InputContainer owner)
            : base(codec)
        {
            OwnerFile = owner;
            Info      = StreamInfo.Create(stream, owner);
            switch (Info.Type)
            {
            case MediaType.Audio:
                RecentlyDecodedFrame = new AudioFrame();
                break;

            case MediaType.Video:
                RecentlyDecodedFrame = new VideoFrame();
                break;

            default:
                throw new Exception("Tried to create a decoder from an unsupported stream or codec type.");
            }

            BufferedPackets = new Queue <MediaPacket>();
        }
예제 #2
0
        private static InputContainer MakeContainer(string url, MediaOptions options, AVFormatContextDelegate contextDelegate)
        {
            FFmpegLoader.LoadFFmpeg();

            var context = ffmpeg.avformat_alloc_context();

            options.DemuxerOptions.ApplyFlags(context);
            var dict = new FFDictionary(options.DemuxerOptions.PrivateOptions, false).Pointer;

            contextDelegate(context);

            ffmpeg.avformat_open_input(&context, url, null, &dict)
            .ThrowIfError("An error occurred while opening the file");

            ffmpeg.avformat_find_stream_info(context, null)
            .ThrowIfError("Cannot find stream info");

            var container = new InputContainer(context, options.PacketBufferSizeLimit);

            container.OpenStreams(options);
            return(container);
        }
예제 #3
0
        /// <summary>
        /// Opens a media container and stream codecs from given path.
        /// </summary>
        /// <param name="path">A path to the multimedia file.</param>
        /// <param name="options">The media settings.</param>
        /// <returns>A new instance of the <see cref="InputContainer"/> class.</returns>
        public static InputContainer LoadFile(string path, MediaOptions options)
        {
            MediaToolkit.LoadFFmpeg();

            var context = ffmpeg.avformat_alloc_context();

            options.DemuxerOptions.ApplyFlags(context);
            var dict = new FFDictionary(options.DemuxerOptions.PrivateOptions);
            var ptr  = dict.Pointer;

            ffmpeg.avformat_open_input(&context, path, null, &ptr)
            .ThrowIfError("An error ocurred while opening the file");

            ffmpeg.avformat_find_stream_info(context, null)
            .ThrowIfError("Cannot find stream info");

            dict.Update(ptr);

            var container = new InputContainer(context);

            container.OpenStreams(options);
            return(container);
        }
예제 #4
0
        private static InputContainer MakeContainer(Stream input, MediaOptions options)
        {
            var avioStream = new AvioStream(input);
            var read       = (avio_alloc_context_read_packet)avioStream.Read;
            var seek       = (avio_alloc_context_seek)avioStream.Seek;

            var context = MakeContext(null, options, ctx =>
            {
                int bufferLength = 4096;
                var avioBuffer   = (byte *)ffmpeg.av_malloc((ulong)bufferLength);

                ctx->pb = ffmpeg.avio_alloc_context(avioBuffer, bufferLength, 0, null, read, null, seek);
                if (ctx->pb == null)
                {
                    throw new FFmpegException("Cannot allocate AVIOContext.");
                }
            });

            var container = new InputContainer(context, read, seek, options.PacketBufferSizeLimit);

            container.OpenStreams(options);
            return(container);
        }
예제 #5
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Decoder{TFrame}"/> class.
 /// </summary>
 /// <param name="stream">The multimedia stream.</param>
 /// <param name="owner">The container that owns the stream.</param>
 public Decoder(AVCodecContext *codec, AVStream *stream, InputContainer owner)
     : base(codec)
 {
     OwnerFile = owner;
     Info      = new StreamInfo(stream, owner);
 }