/// <summary>
        /// Finds the best stream of the specified type in the file.
        /// </summary>
        /// <param name="container">The media container.</param>
        /// <param name="type">Type of the stream to find.</param>
        /// <param name="relStream">Optional. Index of the related stream.</param>
        /// <returns>Index of the found stream, otherwise <see langword="null"/>.</returns>
        private static int?FindBestStream(AVFormatContext *container, AVMediaType type, int relStream = -1)
        {
            AVCodec *codec = null;
            var      id    = ffmpeg.av_find_best_stream(container, type, -1, relStream, &codec, 0);

            return(id >= 0 ? (int?)id : null);
        }
Example #2
0
        public void SelectStream(AVMediaType type)
        {
            AVCodec *avCodec = null;

            _streamIndex    = ffmpeg.av_find_best_stream(_avFormatContext, type, -1, -1, &avCodec, 0).ThrowOnError();
            _avCodecContext = ffmpeg.avcodec_alloc_context3(avCodec);
            var stream = _avFormatContext->streams[_streamIndex];

            if (HardwareDevice != AVHWDeviceType.AV_HWDEVICE_TYPE_NONE)
            {
                ffmpeg.av_hwdevice_ctx_create(&_avCodecContext->hw_device_ctx, HardwareDevice, null, null, 0).ThrowOnError();
            }

            ffmpeg.avcodec_parameters_to_context(_avCodecContext, stream->codecpar).ThrowOnError();
            ffmpeg.avcodec_open2(_avCodecContext, avCodec, null).ThrowOnError();

            CodecId        = avCodec->id;
            CodecName      = ffmpeg.avcodec_get_name(CodecId);
            FrameSize      = new Size(_avCodecContext->width, _avCodecContext->height);
            AudioFrameSize = _avCodecContext->frame_size;;
            PixelFormat    = HardwareDevice == AVHWDeviceType.AV_HWDEVICE_TYPE_NONE ? _avCodecContext->pix_fmt : GetHWPixelFormat(HardwareDevice);
            BitRate        = _avCodecContext->bit_rate;
            FrameRate      = _avCodecContext->framerate;
            TimeBase       = stream->time_base;
        }
Example #3
0
        private void BufferingDone(AVMediaType mType)
        {
            lock (lockerBufferDone)
            {
                if (status != Status.BUFFERING)
                {
                    return;
                }

                if (mType == AVMediaType.AVMEDIA_TYPE_AUDIO)
                {
                    aDone = true;
                }
                else if (mType == AVMediaType.AVMEDIA_TYPE_VIDEO)
                {
                    vDone = true;
                }
                else if (mType == AVMediaType.AVMEDIA_TYPE_SUBTITLE)
                {
                    sDone = true;
                }

                if (vDone && (!decoder.hasAudio || aDone) && (!decoder.hasSubs || IsSubsExternal || sDone))
                {
                    Log($"[BUFFER] Done");
                    status = Status.BUFFERED;
                    BufferingDoneClbk?.BeginInvoke(true, null, null);
                }
            }
        }
Example #4
0
 public static extern AVCodecID av_guess_codec(
     IntPtr /* AVOutputFormat*  */ fmt,
     [MarshalAs(UnmanagedType.LPStr)]
     string short_name,
     [MarshalAs(UnmanagedType.LPStr)]
     string filename,
     [MarshalAs(UnmanagedType.LPStr)]
     string mime_type,
     AVMediaType type);
Example #5
0
 public static extern System.Int32 av_find_best_stream(
     IntPtr /* AVFormatContext*  */ ic,
     AVMediaType type,
     [MarshalAs(UnmanagedType.I4)]
     System.Int32 wanted_stream_nb,
     [MarshalAs(UnmanagedType.I4)]
     System.Int32 related_stream,
     IntPtr /* IntPtr*  */ decoder_ret,
     [MarshalAs(UnmanagedType.I4)]
     System.Int32 flags);
Example #6
0
        protected Format(AVCodecID eCodecID, IntPtr pAVCC, byte nThreads, AVFieldOrder eFieldsOrder)
            : this()
        {
            helper.Initialize();
            _pCodec     = NULL;
            nBufferSize = 0;
            int nResult = 0;

            _bEncode        = false;
            pAVCodecContext = pAVCC;
            _bAVCodecContextAllocationInternal = false;
            AVMediaType eAVMediaType = AVMediaType.AVMEDIA_TYPE_UNKNOWN;

            if (NULL != pAVCodecContext)
            {
                stAVCodecContext = (AVCodecContext)Marshal.PtrToStructure(pAVCodecContext, typeof(AVCodecContext));
                eAVMediaType     = stAVCodecContext.codec_type;
            }

            if (AVMediaType.AVMEDIA_TYPE_UNKNOWN == eAVMediaType)
            {
                if (CodecIDRawGet() != eCodecID)
                {
                    //if (AVCodecID.CODEC_ID_H264_MOBILE == eCodecID)
                    //	eCodecID = AVCodecID.CODEC_ID_H264;
                    if (NULL == (_pCodec = Functions.avcodec_find_encoder(eCodecID)))
                    {
                        throw new Exception("can't find codec " + eCodecID.ToString());
                    }
                }
                if (NULL == pAVCodecContext)
                {
                    //lock (helper._oSyncRootGlobal)
                    {
                        pAVCodecContext = Functions.avcodec_alloc_context3(_pCodec);
                        _bAVCodecContextAllocationInternal = true;
                    }
                }
                else
                {
                    //lock (helper._oSyncRootGlobal)
                    nResult = Functions.avcodec_get_context_defaults3(pAVCodecContext, _pCodec);
                }
                stAVCodecContext          = (AVCodecContext)Marshal.PtrToStructure(pAVCodecContext, typeof(AVCodecContext));
                stAVCodecContext.codec_id = eCodecID;
                _bEncode = true;
            }
            if (1 > nThreads)
            {
                nThreads = (byte)Environment.ProcessorCount;
            }
            stAVCodecContext.thread_count = nThreads;
            stAVCodecContext.field_order  = eFieldsOrder;
            Marshal.StructureToPtr(stAVCodecContext, pAVCodecContext, true);
        }
Example #7
0
        public void SelectStream(AVMediaType type)
        {
            this.type = type;

            streamIndex = ffmpeg.av_find_best_stream(formatContext, type, -1, -1, null, 0);
            if (streamIndex < 0)
            {
                throw new FFmpegException(streamIndex, "Failed to find stream for type " + type.ToString());
            }

            SetupCodecContext(formatContext->streams[streamIndex]);
        }
Example #8
0
    private MediaStream?FindBestStream(AVMediaType mediaType)
    {
        AVCodec *decoder;
        var      streamIndex = ffmpeg.av_find_best_stream(RawFormatContext, mediaType, -1, -1, &decoder, 0);

        if (streamIndex < 0)
        {
            return(null);
        }

        return(new MediaStream(RawFormatContext, streamIndex, RawFormatContext->streams[streamIndex], decoder));
    }
Example #9
0
        static int dec_func(AVMediaType type, AVCodecContext *ctx, AVFrame *frame, int *value, AVPacket *act)
        {
            bool isVideo = type == AVMediaType.AVMEDIA_TYPE_VIDEO;

            if (isVideo)
            {
                return(ffmpeg.avcodec_decode_video2(ctx, frame, value, act));
            }
            else
            {
                return(ffmpeg.avcodec_decode_audio4(ctx, frame, value, act));
            }
        }
Example #10
0
        // Internal Decoder Buffer | (FFmpeg AVIO)
        private byte[]  DecoderRequestsBuffer(long pos, int len, AVMediaType mType)
        {
            byte[] data = null;
            if (streamType == StreamType.FILE)
            {
                //Log($"[BB] [REQUEST] [POS: {pos}] [LEN: {len}] {mType}");
                data = new byte[len];

                lock (fsStream)
                {
                    fsStream.Seek(pos, SeekOrigin.Begin);
                    fsStream.Read(data, 0, len);
                }
            }
            else if (streamType == StreamType.TORRENT)
            {
                lock (lockerBuffering)
                {
                    if (torrent.data.progress.GetFirst0(FilePosToPiece(pos), FilePosToPiece(pos + len)) == -1)
                    {
                        return(torrent.data.files[fileIndex].Read(pos, len));
                    }

                    if (mType == AVMediaType.AVMEDIA_TYPE_SUBTITLE)
                    {
                        Log($"[BB] [REQUEST] [POS: {pos}] [LEN: {len}] {mType}");
                    }
                    if (UpdateFocusPoints(pos, len) == -1)
                    {
                        CreateFocusPoint(pos, len);
                    }

                    while (torrent.data.progress.GetFirst0(FilePosToPiece(pos), FilePosToPiece(pos + len)) != -1)
                    {
                        Thread.Sleep(20);
                    }

                    //Log($"[BB] [REQUEST] [POS: {pos}] [LEN: {len}] {isAudio}");

                    data = torrent.data.files[fileIndex].Read(pos, len);

                    return(data);
                }
            }

            return(data);
        }
Example #11
0
        public Demuxer(Type type = Type.Video, DecoderContext decCtx = null)
        {
            this.decCtx = decCtx;
            this.type   = type;
            status      = Status.NOTSET;

            switch (type)
            {
            case Type.Video:
                decoder = decCtx.vDecoder;
                mType   = AVMEDIA_TYPE_VIDEO;
                break;

            case Type.Audio:
                decoder = decCtx.aDecoder;
                mType   = AVMEDIA_TYPE_AUDIO;
                break;

            case Type.Subs:
                decoder = decCtx.sDecoder;
                mType   = AVMEDIA_TYPE_SUBTITLE;
                break;
            }

            demuxARE           = new AutoResetEvent(false);
            enabledStreams     = new List <int>();
            defaultAudioStream = -1;

            interruptClbk.Pointer = Marshal.GetFunctionPointerForDelegate(InterruptClbk);
            ioread.Pointer        = Marshal.GetFunctionPointerForDelegate(IORead);
            ioseek.Pointer        = Marshal.GetFunctionPointerForDelegate(IOSeek);

            gcPrevent = new List <object>();
            gcPrevent.Add(ioread);
            gcPrevent.Add(ioseek);
            gcPrevent.Add(IORead);
            gcPrevent.Add(IOSeek);
        }
Example #12
0
        // set output format from file extension
        public void SetOutputFormat(AVCodecID codecId, int width, int height, AVPixelFormat pixelFormat, int compression)
        {
            type = AVMediaType.AVMEDIA_TYPE_VIDEO;

            AVOutputFormat *outputFormat = ffmpeg.av_guess_format(null, path, null);

            if (outputFormat == null)
            {
                throw new ApplicationException("Failed to guess output format for extension " + path);
            }

            string name    = Marshal.PtrToStringAnsi((IntPtr)outputFormat->name);
            string lname   = Marshal.PtrToStringAnsi((IntPtr)outputFormat->long_name);
            string extlist = Marshal.PtrToStringAnsi((IntPtr)outputFormat->extensions);

            if (codecId == AVCodecID.AV_CODEC_ID_NONE)
            {
                codecId = ffmpeg.av_guess_codec(outputFormat, null, path, null, AVMediaType.AVMEDIA_TYPE_VIDEO);
            }

            outputFormat->video_codec = codecId;
            SetupOutput(outputFormat, codecId, width, height, pixelFormat, compression);
        }
Example #13
0
        public void SetOutputFormat(AVCodecID codecId, int sampleRate, int sampleCount, AVSampleFormat sampleFormat)
        {
            type = AVMediaType.AVMEDIA_TYPE_AUDIO;

            AVOutputFormat *outputFormat = ffmpeg.av_guess_format(null, path, null);

            if (outputFormat == null)
            {
                throw new ApplicationException("Failed to guess output format for extension " + path);
            }

            string name    = Marshal.PtrToStringAnsi((IntPtr)outputFormat->name);
            string lname   = Marshal.PtrToStringAnsi((IntPtr)outputFormat->long_name);
            string extlist = Marshal.PtrToStringAnsi((IntPtr)outputFormat->extensions);

            if (codecId == AVCodecID.AV_CODEC_ID_NONE)
            {
                codecId = ffmpeg.av_guess_codec(outputFormat, null, path, null, AVMediaType.AVMEDIA_TYPE_AUDIO);
            }

            outputFormat->audio_codec = codecId;
            SetupOutput(outputFormat, codecId, sampleRate, sampleCount, sampleFormat);
        }
Example #14
0
 public static extern string av_get_media_type_string(
     AVMediaType media_type);
 public static extern AVCodecID av_guess_codec(AVOutputFormat* fmt, String short_name, String filename, String mime_type, AVMediaType type);
Example #16
0
public static extern System.Int32 av_find_best_stream(
	IntPtr/* AVFormatContext*  */ ic, 
	AVMediaType type, 
	[MarshalAs(UnmanagedType.I4)]
	System.Int32 wanted_stream_nb, 
	[MarshalAs(UnmanagedType.I4)]
	System.Int32 related_stream, 
	IntPtr/* IntPtr*  */ decoder_ret, 
	[MarshalAs(UnmanagedType.I4)]
	System.Int32 flags);
 public static extern String av_get_media_type_string(AVMediaType media_type);
 public static extern int av_find_best_stream(AVFormatContext* ic, AVMediaType type, int wanted_stream_nb, int related_stream, AVCodec** decoder_ret, int flags);
Example #19
0
 private static extern int av_find_best_stream(AVFormatContext *ic, AVMediaType type, int wanted_stream_nb, int related_stream, AVCodec **decoder_ret, int flags);
Example #20
0
public static extern AVCodecID av_guess_codec(
	IntPtr/* AVOutputFormat*  */ fmt, 
	[MarshalAs(UnmanagedType.LPStr)]
	string short_name, 
	[MarshalAs(UnmanagedType.LPStr)]
	string filename, 
	[MarshalAs(UnmanagedType.LPStr)]
	string mime_type, 
	AVMediaType type);
 public static extern int av_find_best_stream(AVFormatContext* @ic, AVMediaType @type, int @wanted_stream_nb, int @related_stream, AVCodec** @decoder_ret, int @flags);
 public static extern AVCodecID av_guess_codec(AVOutputFormat* @fmt, [MarshalAs(UnmanagedType.LPStr)] string @short_name, [MarshalAs(UnmanagedType.LPStr)] string @filename, [MarshalAs(UnmanagedType.LPStr)] string @mime_type, AVMediaType @type);
Example #23
0
 public static extern AVCodecID av_guess_codec(AVOutputFormat *fmt, string short_name, string filename, string mime_type, AVMediaType type);
        private int FindBestStream(AVMediaType mediaType)
        {
            var streamId = formatContext.FindBestBandwidthStream(mediaType);

            return(streamId >= 0 ? streamId : formatContext.FindBestStream(mediaType));
        }
 public static extern void avcodec_get_context_defaults2(AVCodecContext* s, AVMediaType p1);
 public static extern AVCodecContext* avcodec_alloc_context2(AVMediaType p0);
 public static MediaType ConvertToMediaType(this AVMediaType mediaType)
 {
     return((MediaType)mediaType);
 }