Esempio n. 1
0
        public VideoForm()
        {
            Debug.WriteLine(DateTime.Now.ToString("HH:mm:ss - ") + TAG + ".VideoForm()", "DVR INFO");

            InitializeComponent();

            ////////////////////////////////////////////////////////
            IntPtr pCodec_pt = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(FFmpeg.AVCodec)));

            pCodec_pt = FFmpeg.avcodec_find_decoder(FFmpeg.CodecID.CODEC_ID_H264);

            FFmpeg.AVCodec pCodec = (FFmpeg.AVCodec)Marshal.PtrToStructure((IntPtr)((UInt32)pCodec_pt), typeof(FFmpeg.AVCodec));

            pCodecCtx_pt = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(FFmpeg.AVCodecContext)));
            pCodecCtx_pt = FFmpeg.avcodec_alloc_context();

            pCodecCtx = (FFmpeg.AVCodecContext)Marshal.PtrToStructure((IntPtr)((UInt32)pCodecCtx_pt), typeof(FFmpeg.AVCodecContext));

            int open_en = FFmpeg.avcodec_open(pCodecCtx_pt, pCodec_pt);

            ////////////////////////////////////////////////////////
            ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
            ici = null;
            foreach (ImageCodecInfo codec in codecs)
            {
                if (codec.MimeType == "image/jpeg")
                {
                    ici = codec;
                }
            }
            ep          = new EncoderParameters();
            ep.Param[0] = new EncoderParameter(Encoder.Quality, (long)50);
        }
Esempio n. 2
0
        static void ShowFormats()
        {
            Console.WriteLine("File formats:");

            IntPtr ifmtPtr = IntPtr.Zero;

            FFmpeg.AVInputFormat ifmt;
            IntPtr ofmtPtr = IntPtr.Zero;

            FFmpeg.AVOutputFormat ofmt;

            string last_name = "";

            while (true)
            {
                bool   encode    = false;
                bool   decode    = false;
                string name      = null;
                string long_name = null;

                while ((ofmtPtr = FFmpeg.av_oformat_next(ofmtPtr)) != IntPtr.Zero)
                {
                    ofmt = (FFmpeg.AVOutputFormat)Marshal.PtrToStructure(ofmtPtr, typeof(FFmpeg.AVOutputFormat));
                    if ((name == null
                         ||
                         String.Compare(ofmt.name, name, StringComparison.InvariantCulture) < 0) &&
                        String.Compare(ofmt.name, last_name, StringComparison.InvariantCulture) > 0)
                    {
                        name      = ofmt.name;
                        long_name = ofmt.long_name;
                        encode    = true;
                    }
                }

                while ((ifmtPtr = FFmpeg.av_iformat_next(ifmtPtr)) != IntPtr.Zero)
                {
                    ifmt = (FFmpeg.AVInputFormat)Marshal.PtrToStructure(ifmtPtr, typeof(FFmpeg.AVInputFormat));
                    if ((name == null
                         ||
                         String.Compare(ifmt.name, name, StringComparison.InvariantCulture) < 0) &&
                        String.Compare(ifmt.name, last_name, StringComparison.InvariantCulture) > 0)
                    {
                        name      = ifmt.name;
                        long_name = ifmt.long_name;
                        encode    = false;
                    }
                    if (name != null && string.Compare(ifmt.name, name) == 0)
                    {
                        decode = true;
                    }
                }

                if (name == null)
                {
                    break;
                }
                last_name = name;

                Console.WriteLine(String.Format("{0}{1} {2,-15} {3}", decode ? "D" : " ", encode ? "E" : " ", name, long_name));
            }

            Console.WriteLine();

            Console.WriteLine("Codecs:");

            last_name = "";

            IntPtr pPtr = IntPtr.Zero;

            FFmpeg.AVCodec p     = new FFmpeg.AVCodec();
            IntPtr         p2Ptr = IntPtr.Zero;

            FFmpeg.AVCodec p2 = new FFmpeg.AVCodec();

            while (true)
            {
                bool   decode = false;
                bool   encode = false;
                int    cap    = 0;
                string type_str;

                p2Ptr = IntPtr.Zero;
                while ((pPtr = FFmpeg.av_codec_next(pPtr)) != IntPtr.Zero)
                {
                    p = (FFmpeg.AVCodec)Marshal.PtrToStructure(pPtr, typeof(FFmpeg.AVCodec));
                    if ((p2Ptr == IntPtr.Zero || string.Compare(p.name, p2.name) < 0) &&
                        string.Compare(p.name, last_name) > 0)
                    {
                        p2     = p;
                        p2Ptr  = pPtr;
                        decode = false;
                        encode = false;
                        cap    = 0;
                    }
                    if (p2Ptr != IntPtr.Zero && string.Compare(p.name, p2.name) == 0)
                    {
                        if (p.decode != null)
                        {
                            decode = true;
                        }
                        if (p.encode != null)
                        {
                            encode = true;
                        }
                        cap |= p.capabilities;
                    }
                }
                if (p2Ptr == IntPtr.Zero)
                {
                    break;
                }
                last_name = p2.name;

                switch (p2.type)
                {
                case FFmpeg.CodecType.CODEC_TYPE_VIDEO:
                    type_str = "V";
                    break;

                case FFmpeg.CodecType.CODEC_TYPE_AUDIO:
                    type_str = "A";
                    break;

                case FFmpeg.CodecType.CODEC_TYPE_SUBTITLE:
                    type_str = "S";
                    break;

                default:
                    type_str = "?";
                    break;
                }
                Console.WriteLine(String.Format(
                                      " {0}{1}{2}{3}{4}{5} {6,-15} {7}",
                                      decode ? "D": (/*p2->decoder ? "d":*/ " "),
                                      encode ? "E":" ",
                                      type_str,
                                      (cap & FFmpeg.CODEC_CAP_DRAW_HORIZ_BAND) > 0 ? "S":" ",
                                      (cap & FFmpeg.CODEC_CAP_DR1) > 0 ? "D":" ",
                                      (cap & FFmpeg.CODEC_CAP_TRUNCATED) > 0 ? "T":" ",
                                      p2.name,
                                      p2.long_name ?? ""));

                /* if(p2->decoder && decode==0)
                 *   printf(" use %s for decoding", p2->decoder->name);*/
            }

            Console.WriteLine();

            IntPtr bsfPtr = IntPtr.Zero;

            FFmpeg.AVBitStreamFilter bsf = new FFmpeg.AVBitStreamFilter();

            Console.WriteLine("Bitstream filters:");
            while ((bsfPtr = FFmpeg.av_bitstream_filter_next(bsfPtr)) != IntPtr.Zero)
            {
                bsf = (FFmpeg.AVBitStreamFilter)Marshal.PtrToStructure(bsfPtr, typeof(FFmpeg.AVBitStreamFilter));
                Console.Write(" {0}", bsf.name);
            }
            Console.WriteLine();
            Console.WriteLine();

            /*
             * IntPtr upPtr = IntPtr.Zero;
             * FFmpeg.URLProtocol up = new FFmpeg.URLProtocol();
             *
             * Console.WriteLine("Supported file protocols:");
             * while ((bsfPtr = FFmpeg.av_protocol_next(bsfPtr)) != IntPtr.Zero)
             * {
             * bsf = (FFmpeg.URLProtocol)Marshal.PtrToStructure(bsfPtr, typeof(FFmpeg.URLProtocol));
             * Console.Write(" {0}:", up.name);
             * }
             * Console.WriteLine();
             * Console.WriteLine();
             */

            Console.WriteLine(
                "Frame size, frame rate abbreviations:\n ntsc pal qntsc qpal sntsc spal film ntsc-film sqcif qcif cif 4cif");
            Console.WriteLine();

            Console.WriteLine("Note, the names of encoders and decoders do not always match, so there are");
            Console.WriteLine("several cases where the above table shows encoder only or decoder only entries");
            Console.WriteLine("even though both encoding and decoding are supported. For example, the h263");
            Console.WriteLine("decoder corresponds to the h263 and h263p encoders, for file formats it is even");
            Console.WriteLine("worse.");
        }
Esempio n. 3
0
        public MediaFile(Stream inStream)
        {
            // Create unique name
            string filename = "stream://" + counter;

            // Register stream
            streams.Add(filename, inStream);

            // Open stream with FFmpeg
            if (FFmpeg.av_open_input_file(out pFormatContext, filename, IntPtr.Zero, 0, IntPtr.Zero) < 0)
            {
                throw new Exception("Unable to open stream");
            }

            // Get context
            FFmpeg.AVFormatContext formatContext = PtrToStructure <FFmpeg.AVFormatContext>(pFormatContext);

            // Get stream info
            if (FFmpeg.av_find_stream_info(pFormatContext) < 0)
            {
                throw new Exception("Unable to find stream info");
            }

            // Loop through streams in this file
            for (int i = 0; i < formatContext.nb_streams; ++i)
            {
                FFmpeg.AVStream       stream       = PtrToStructure <FFmpeg.AVStream>(formatContext.streams[i]);
                FFmpeg.AVCodecContext codecContext = PtrToStructure <FFmpeg.AVCodecContext>(stream.codec);

                // Get codec
                IntPtr         pCodec = FFmpeg.avcodec_find_decoder(codecContext.codec_id);
                FFmpeg.AVCodec codec  = PtrToStructure <FFmpeg.AVCodec>(pCodec);
                if (pCodec == IntPtr.Zero)
                {
                    continue;
                }

                // Check codec type
                bool open = false;
                switch (codecContext.codec_type)
                {
                case FFmpeg.CodecType.CODEC_TYPE_AUDIO:
                    // We only need 1 audio stream
                    if (hasAudio)
                    {
                        break;
                    }

                    // Get stream information
                    hasAudio      = true;
                    numChannels   = codecContext.channels;
                    audioDepth    = (codecContext.sample_fmt == FFmpeg.SampleFormat.SAMPLE_FMT_U8) ? 8 : 16;
                    frequency     = codecContext.sample_rate;
                    audioStream   = stream;
                    audioTimebase = (double)stream.time_base.num / (double)stream.time_base.den;

                    open = true;
                    break;

                case FFmpeg.CodecType.CODEC_TYPE_VIDEO:
                    // We only need 1 video stream
                    if (hasVideo)
                    {
                        break;
                    }

                    // Set codec flags
                    if ((codec.capabilities & FFmpeg.CODEC_CAP_TRUNCATED) != 0)
                    {
                        codecContext.flags = codecContext.flags | FFmpeg.CODEC_FLAG_TRUNCATED;
                    }

                    // Get stream information
                    hasVideo            = true;
                    width               = codecContext.width;
                    height              = codecContext.height;
                    videoStream         = stream;
                    originalVideoFormat = codecContext.pix_fmt;
                    videoTimebase       = (double)codecContext.time_base.num / (double)codecContext.time_base.den;

                    open = true;
                    break;
                }

                // Update codec context
                Marshal.StructureToPtr(codecContext, stream.codec, false);

                // Open codec
                if (open)
                {
                    if (FFmpeg.avcodec_open(stream.codec, pCodec) < 0)
                    {
                        throw new Exception("Unable to open codec");
                    }
                }
            }

            // No video or audio found
            if (!hasAudio && !hasVideo)
            {
                throw new Exception("No codecs or streams found");
            }
        }