Exemple #1
0
        public virtual int init(int[] extraData)
        {
            context = MpegEncContext.avcodec_alloc_context();

            picture = AVFrame.avcodec_alloc_frame();

            packet = new AVPacket();
            packet.av_init_packet();

            if (extraData != null)
            {
                context.extradata_size = extraData.Length;
                // Add 4 additional values to avoid exceptions while parsing
                int[] extraDataPlus4 = new int[context.extradata_size + 4];
                Array.Copy(extraData, 0, extraDataPlus4, 0, context.extradata_size);
                context.extradata = extraDataPlus4;
            }

            int result = context.avcodec_open(new com.twilight.h264.decoder.H264Decoder());

            if (result < 0)
            {
                return(result);
            }

            gotPicture[0] = 0;

            return(0);
        }
        private void TryInit()
        {
            if (Initialized)
            {
                return;
            }
            Initialized = true;

            avpkt.av_init_packet();

            // Find the mpeg1 video decoder
            InitProtected();

            // The codec gives us the frame size, in samples

            frame = 0;

            var cacheRead = stackalloc int[3];

            // avpkt must contain exactly 1 NAL Unit in order for decoder to decode correctly.
            // thus we must read until we get next NAL header before sending it to decoder.
            // Find 1st NAL
            cacheRead[0] = ReadByte();
            cacheRead[1] = ReadByte();
            cacheRead[2] = ReadByte();

            while (!(cacheRead[0] == 0x00 && cacheRead[1] == 0x00 && cacheRead[2] == 0x01))
            {
                cacheRead[0] = cacheRead[1];
                cacheRead[1] = cacheRead[2];
                cacheRead[2] = ReadByte();
                if (cacheRead[2] == -1)
                {
                    throw(new EndOfStreamException());
                }
            } // while

            // 4 first bytes always indicate NAL header
            inbuf_int[0] = inbuf_int[1] = inbuf_int[2] = 0x00;
            inbuf_int[3] = 0x01;

            //hasMoreNAL = true;
        }
        private void Init()
        {
            avpkt.av_init_packet();

            // Set end of buffer to 0 (this ensures that no overreading happens for damaged mpeg streams)
            Arrays.Fill(inbuf, INBUF_SIZE, MpegEncContext.FF_INPUT_BUFFER_PADDING_SIZE + INBUF_SIZE, (sbyte)0);

            // Find the mpeg1 video decoder
            codec = new H264Decoder();
            if (codec == null)
            {
                throw (new Exception("codec not found"));
            }

            c       = MpegEncContext.avcodec_alloc_context();
            picture = AVFrame.avcodec_alloc_frame();

            // We do not send complete frames
            if ((codec.capabilities & H264Decoder.CODEC_CAP_TRUNCATED) != 0)
            {
                c.flags |= MpegEncContext.CODEC_FLAG_TRUNCATED;
            }

            // For some codecs, such as msmpeg4 and mpeg4, width and height
            // MUST be initialized there because this information is not
            // available in the bitstream.

            // Open it
            if (c.avcodec_open(codec) < 0)
            {
                throw (new Exception("could not open codec"));
            }

            // The codec gives us the frame size, in samples

            frame = 0;

            // avpkt must contain exactly 1 NAL Unit in order for decoder to decode correctly.
            // thus we must read until we get next NAL header before sending it to decoder.
            // Find 1st NAL
            cacheRead[0] = fin.ReadByte();
            cacheRead[1] = fin.ReadByte();
            cacheRead[2] = fin.ReadByte();

            while (!(cacheRead[0] == 0x00 && cacheRead[1] == 0x00 && cacheRead[2] == 0x01))
            {
                cacheRead[0] = cacheRead[1];
                cacheRead[1] = cacheRead[2];
                cacheRead[2] = fin.ReadByte();
                if (cacheRead[2] == -1)
                {
                    throw(new EndOfStreamException());
                }
            }             // while

            // 4 first bytes always indicate NAL header
            inbuf_int[0] = inbuf_int[1] = inbuf_int[2] = 0x00;
            inbuf_int[3] = 0x01;

            hasMoreNAL = true;
        }