コード例 #1
0
        public void Properties_ReturnsNativeValues()
        {
            var nativeCodecParameters = new NativeAVCodecParameters
            {
                codec_type = NativeAVMediaType.AVMEDIA_TYPE_VIDEO,
                codec_id   = NativeAVCodecID.AV_CODEC_ID_4XM,
                format     = 124,
            };

            var codecParameters = new AVCodecParameters(&nativeCodecParameters);

            Assert.Equal(NativeAVMediaType.AVMEDIA_TYPE_VIDEO, codecParameters.Type);
            Assert.Equal(NativeAVCodecID.AV_CODEC_ID_4XM, codecParameters.Id);
            Assert.Equal(124, codecParameters.Format);
        }
コード例 #2
0
        // sets up libavformat state: creates the AVFormatContext, the frames, etc. to start decoding, but does not actually start the decodingLoop
        private void prepareDecoding()
        {
            const int context_buffer_size = 4096;

            // the first call to FFmpeg will throw an exception if the libraries cannot be found
            // this will be safely handled in StartDecoding()
            var fcPtr = ffmpeg.avformat_alloc_context();

            formatContext        = fcPtr;
            contextBuffer        = (byte *)ffmpeg.av_malloc(context_buffer_size);
            managedContextBuffer = new byte[context_buffer_size];
            readPacketCallback   = readPacket;
            seekCallback         = streamSeekCallbacks;
            formatContext->pb    = ffmpeg.avio_alloc_context(contextBuffer, context_buffer_size, 0, (void *)handle.Handle, readPacketCallback, null, seekCallback);

            int openInputResult = ffmpeg.avformat_open_input(&fcPtr, "dummy", null, null);

            inputOpened = openInputResult >= 0;
            if (!inputOpened)
            {
                throw new InvalidOperationException($"Error opening file or stream: {getErrorMessage(openInputResult)}");
            }

            int findStreamInfoResult = ffmpeg.avformat_find_stream_info(formatContext, null);

            if (findStreamInfoResult < 0)
            {
                throw new InvalidOperationException($"Error finding stream info: {getErrorMessage(findStreamInfoResult)}");
            }

            var nStreams = formatContext->nb_streams;

            for (var i = 0; i < nStreams; ++i)
            {
                stream = formatContext->streams[i];

                codecParams = *stream->codecpar;

                if (codecParams.codec_type == AVMediaType.AVMEDIA_TYPE_VIDEO)
                {
                    duration = stream->duration <= 0 ? formatContext->duration : stream->duration;

                    timeBaseInSeconds = stream->time_base.GetValue();
                    var codecPtr = ffmpeg.avcodec_find_decoder(codecParams.codec_id);
                    if (codecPtr == null)
                    {
                        throw new InvalidOperationException($"Couldn't find codec with id: {codecParams.codec_id}");
                    }

                    int openCodecResult = ffmpeg.avcodec_open2(stream->codec, codecPtr, null);
                    if (openCodecResult < 0)
                    {
                        throw new InvalidOperationException($"Error trying to open codec with id {codecParams.codec_id}: {getErrorMessage(openCodecResult)}");
                    }

                    break;
                }
            }

            prepareFilters();
        }
コード例 #3
0
        // sets up libavformat state: creates the AVFormatContext, the frames, etc. to start decoding, but does not actually start the decodingLoop
        private void prepareDecoding()
        {
            const int context_buffer_size = 4096;

            var fcPtr = ffmpeg.avformat_alloc_context();

            formatContext        = fcPtr;
            contextBuffer        = (byte *)ffmpeg.av_malloc(context_buffer_size);
            managedContextBuffer = new byte[context_buffer_size];
            readPacketCallback   = readPacket;
            seekCallback         = seek;
            formatContext->pb    = ffmpeg.avio_alloc_context(contextBuffer, context_buffer_size, 0, null, readPacketCallback, null, seekCallback);
            if (ffmpeg.avformat_open_input(&fcPtr, "dummy", null, null) < 0)
            {
                throw new Exception("Error opening file.");
            }

            if (ffmpeg.avformat_find_stream_info(formatContext, null) < 0)
            {
                throw new Exception("Could not find stream info.");
            }

            var nStreams = formatContext->nb_streams;

            for (var i = 0; i < nStreams; ++i)
            {
                stream = formatContext->streams[i];

                codecParams = *stream->codecpar;
                if (codecParams.codec_type == AVMediaType.AVMEDIA_TYPE_VIDEO)
                {
                    timeBaseInSeconds = stream->time_base.GetValue();
                    var codecPtr = ffmpeg.avcodec_find_decoder(codecParams.codec_id);
                    if (codecPtr == null)
                    {
                        throw new Exception("Could not find codec.");
                    }

                    if (ffmpeg.avcodec_open2(stream->codec, codecPtr, null) < 0)
                    {
                        throw new Exception("Could not open codec.");
                    }

                    frame    = ffmpeg.av_frame_alloc();
                    frameRgb = ffmpeg.av_frame_alloc();

                    uncompressedFrameSize = ffmpeg.av_image_get_buffer_size(AVPixelFormat.AV_PIX_FMT_RGBA, codecParams.width, codecParams.height, 1);
                    frameRgbBufferPtr     = Marshal.AllocHGlobal(uncompressedFrameSize);

                    var dataArr4     = *(byte_ptrArray4 *)&frameRgb->data;
                    var linesizeArr4 = *(int_array4 *)&frameRgb->linesize;
                    var result       = ffmpeg.av_image_fill_arrays(ref dataArr4, ref linesizeArr4, (byte *)frameRgbBufferPtr, AVPixelFormat.AV_PIX_FMT_RGBA, codecParams.width, codecParams.height, 1);
                    if (result < 0)
                    {
                        throw new Exception("Could not fill image arrays");
                    }

                    for (uint j = 0; j < byte_ptrArray4.Size; ++j)
                    {
                        frameRgb->data[j]     = dataArr4[j];
                        frameRgb->linesize[j] = linesizeArr4[j];
                    }

                    break;
                }
            }
        }