public StreamDecoder(string url, DecoderConfiguration configuration) { FFmpegBinariesHelper.RegisterFFmpegBinaries(); //Should not be here this.decoderConfiguration = configuration; _pFormatContext = ffmpeg.avformat_alloc_context(); var pFormatContext = _pFormatContext; ffmpeg.avformat_open_input(&pFormatContext, url, null, null).ThrowExceptionIfError(); ffmpeg.avformat_find_stream_info(_pFormatContext, null).ThrowExceptionIfError(); // find the first video stream AVStream *pStream = null; for (var i = 0; i < _pFormatContext->nb_streams; i++) { if (_pFormatContext->streams[i]->codec->codec_type == AVMediaType.AVMEDIA_TYPE_VIDEO) { pStream = _pFormatContext->streams[i]; break; } } if (pStream == null) { throw new InvalidOperationException("Could not found video stream."); } _streamIndex = pStream->index; _pCodecContext = pStream->codec; var codecId = _pCodecContext->codec_id; var pCodec = ffmpeg.avcodec_find_decoder(codecId); if (pCodec == null) { throw new InvalidOperationException("Unsupported codec."); } ffmpeg.avcodec_open2(_pCodecContext, pCodec, null).ThrowExceptionIfError(); decoderConfiguration.codec = FormatHelper.FFmpegToOT2(codecId); decoderConfiguration.inputResolution = new Resolution(_pCodecContext->height, _pCodecContext->width); //CodecName = ffmpeg.avcodec_get_name(codecId); //PixelFormat = _pCodecContext->pix_fmt; decoderConfiguration.inputPixelFormat = FormatHelper.FFmpegToOT2(_pCodecContext->pix_fmt); _pPacket = ffmpeg.av_packet_alloc(); _pFrame = ffmpeg.av_frame_alloc(); decodingThread = new Thread(() => { while (DecodeFrame() == 0) { ; } }); }
public VideoDecoder(DecoderConfiguration configuration) { FFmpegBinariesHelper.RegisterFFmpegBinaries(); //outputResolution = resolution; Console.WriteLine("Current directory: " + Environment.CurrentDirectory); Console.WriteLine("Runnung in {0}-bit mode.", Environment.Is64BitProcess ? "64" : "32"); Console.WriteLine($"FFmpeg version info: {ffmpeg.av_version_info()}"); //FFMPEG initialization _pFormatContext = ffmpeg.avformat_alloc_context(); var pFormatContext = _pFormatContext; //ffmpeg.avformat_open_input(&pFormatContext, url, null, null).ThrowExceptionIfError(); ffmpeg.avformat_find_stream_info(_pFormatContext, null).ThrowExceptionIfError(); // find the first video stream //AVStream* pStream = null; //for (var i = 0; i < _pFormatContext->nb_streams; i++) // if (_pFormatContext->streams[i]->codec->codec_type == AVMediaType.AVMEDIA_TYPE_VIDEO) // { // pStream = _pFormatContext->streams[i]; // break; //} //if (pStream == null) throw new InvalidOperationException("Could not found video stream."); // GET DECODER FOR STREAM //_streamIndex = pStream->index; //_pCodecContext = pStream->codec; //var codecId = _pCodecContext->codec_id; var codecId = FormatHelper.OT2ToFFmpeg(configuration.codec); var pCodec = ffmpeg.avcodec_find_decoder(codecId); _pCodecContext = ffmpeg.avcodec_alloc_context3(pCodec); if (pCodec == null) { throw new InvalidOperationException("Unsupported codec."); } ffmpeg.avcodec_open2(_pCodecContext, pCodec, null).ThrowExceptionIfError(); var codecName = ffmpeg.avcodec_get_name(codecId); var pixelFormat = _pCodecContext->pix_fmt; // ALLOC FRAME AND PACKET packet = ffmpeg.av_packet_alloc(); frame = ffmpeg.av_frame_alloc(); decodingThread = new Thread(DecodeFrames); }
public VideoFrameConverter(DecoderConfiguration decoderConfiguration) { configuration = decoderConfiguration; _pConvertContext = ffmpeg.sws_getContext(configuration.inputResolution.width, configuration.inputResolution.height, FormatHelper.OT2ToFFmpeg(configuration.inputPixelFormat), configuration.outputResolution.width, configuration.outputResolution.height, FormatHelper.OT2ToFFmpeg(configuration.outputPixelFormat), ffmpeg.SWS_FAST_BILINEAR, null, null, null); if (_pConvertContext == null) { throw new ApplicationException("Could not initialize the conversion context."); } var convertedFrameBufferSize = ffmpeg.av_image_get_buffer_size(FormatHelper.OT2ToFFmpeg(configuration.outputPixelFormat) , configuration.outputResolution.width, configuration.outputResolution.height, 1); _convertedFrameBufferPtr = Marshal.AllocHGlobal(convertedFrameBufferSize); _dstData = new byte_ptrArray4(); _dstLinesize = new int_array4(); ffmpeg.av_image_fill_arrays(ref _dstData, ref _dstLinesize, (byte *)_convertedFrameBufferPtr, FormatHelper.OT2ToFFmpeg(configuration.outputPixelFormat) , configuration.outputResolution.width, configuration.outputResolution.height, 1); }