Beispiel #1
0
        private unsafe void DecodeAVIHeader(RiffParser rp, int length)
        {
            //if (length < sizeof(AVIMAINHEADER))
            //{
            //  throw new RiffParserException(String.Format("Header size mismatch. Needed {0} but only have {1}",
            //      sizeof(AVIMAINHEADER), length));
            //}

            byte[] ba = new byte[length];

            if (rp.ReadData(ba, 0, length) != length)
            {
                throw new RiffParserException("Problem reading AVI header.");
            }

            fixed(Byte *bp = &ba[0])
            {
                AVIMAINHEADER *avi = (AVIMAINHEADER *)bp;

                m_frameRate   = avi->dwMicroSecPerFrame;
                m_height      = avi->dwHeight;
                m_maxBitRate  = avi->dwMaxBytesPerSec;
                m_numStreams  = avi->dwStreams;
                m_totalFrames = avi->dwTotalFrames;
                m_width       = avi->dwWidth;
            }
        }
Beispiel #2
0
        /// <summary>
        /// Handle chunk elements found in the AVI file. Ignores unknown chunks and
        /// </summary>
        /// <param name="rp"></param>
        /// <param name="FourCC"></param>
        /// <param name="unpaddedLength"></param>
        /// <param name="paddedLength"></param>
        private void ProcessAVIChunk(RiffParser rp, int FourCC, int unpaddedLength, int paddedLength)
        {
            if (AviRiffData.ckidMainAVIHeader == FourCC)
            {
                // Main AVI header
                DecodeAVIHeader(rp, paddedLength);
            }
            else if (AviRiffData.ckidAVIStreamHeader == FourCC)
            {
                // Stream header
                DecodeAVIStream(rp, paddedLength);
            }
            else if (AviRiffData.ckidAVIISFT == FourCC)
            {
                Byte[] ba = new byte[paddedLength];
                rp.ReadData(ba, 0, paddedLength);
                StringBuilder sb = new StringBuilder(unpaddedLength);
                for (int i = 0; i < unpaddedLength; ++i)
                {
                    if (0 != ba[i])
                    {
                        sb.Append((char)ba[i]);
                    }
                }

                m_isft = sb.ToString();
            }
            else
            {
                // Unknon chunk - skip
                rp.SkipData(paddedLength);
            }
        }
Beispiel #3
0
 private void ProcessWaveChunk(RiffParser rp, int FourCC, int unpaddedLength, int length)
 {
     // Is this a 'fmt' chunk?
     if (AviRiffData.ckidWaveFMT == FourCC)
     {
         DecodeWave(rp, length);
     }
     else
     {
         rp.SkipData(length);
     }
 }
Beispiel #4
0
        private unsafe void DecodeAVIStream(RiffParser rp, int length)
        {
            byte[] ba = new byte[length];

            if (rp.ReadData(ba, 0, length) != length)
            {
                throw new RiffParserException("Problem reading AVI header.");
            }

            fixed(Byte *bp = &ba[0])
            {
                AVISTREAMHEADER *avi = (AVISTREAMHEADER *)bp;

                if (AviRiffData.streamtypeVIDEO == avi->fccType)
                {
                    m_vidHandler = RiffParser.FromFourCC(avi->fccHandler);
                    if (avi->dwScale > 0)
                    {
                        m_vidDataRate = (double)avi->dwRate / (double)avi->dwScale;
                    }
                    else
                    {
                        m_vidDataRate = 0.0;
                    }
                }
                else if (AviRiffData.streamtypeAUDIO == avi->fccType)
                {
                    if (AviRiffData.ckidMP3 == avi->fccHandler)
                    {
                        m_audHandler = "MP3";
                    }
                    else
                    {
                        m_audHandler = RiffParser.FromFourCC(avi->fccHandler);
                    }
                    if (avi->dwScale > 0)
                    {
                        m_audDataRate = 8.0 * (double)avi->dwRate / (double)avi->dwScale;
                        if (avi->dwSampleSize > 0)
                        {
                            m_audDataRate /= (double)avi->dwSampleSize;
                        }
                    }
                    else
                    {
                        m_audDataRate = 0.0;
                    }
                }
            }
        }
Beispiel #5
0
        private unsafe void DecodeWave(RiffParser rp, int length)
        {
            byte[] ba = new byte[length];
            rp.ReadData(ba, 0, length);

            fixed(byte *bp = &ba[0])
            {
                WAVEFORMATEX *wave = (WAVEFORMATEX *)bp;

                m_numChannels   = wave->nChannels;
                m_bitsPerSec    = wave->nAvgBytesPerSec;
                m_bitsPerSample = wave->wBitsPerSample;
                m_samplesPerSec = wave->nSamplesPerSec;
            }
        }
Beispiel #6
0
        /// <summary>
        /// Handle List elements found in the AVI file. Ignores unknown lists and recursively looks
        /// at the content of known lists.
        /// </summary>
        /// <param name="rp"></param>
        /// <param name="FourCC"></param>
        /// <param name="length"></param>
        private void ProcessAVIList(RiffParser rp, int FourCC, int length)
        {
            RiffParser.ProcessChunkElement pac = new RiffParser.ProcessChunkElement(ProcessAVIChunk);
            RiffParser.ProcessListElement  pal = new RiffParser.ProcessListElement(ProcessAVIList);

            // Is this the header?
            if ((AviRiffData.ckidAVIHeaderList == FourCC) ||
                (AviRiffData.ckidAVIStreamList == FourCC) ||
                (AviRiffData.ckidINFOList == FourCC))
            {
                while (length > 0)
                {
                    if (false == rp.ReadElement(ref length, pac, pal))
                    {
                        break;
                    }
                }
            }
            else
            {
                // Unknown lists - ignore
                rp.SkipData(length);
            }
        }
Beispiel #7
0
        private void SetFrameRateFromVideoFile()
        {
            double frameRate = 0;

            // Cursor = Cursors.WaitCursor; will Invalid operation exection (Note: Belong to UI thread, the expection won't crash the app 'CAREFUL')
            try
            {
                if (!string.IsNullOrEmpty(_videoFileName) && File.Exists(_videoFileName))
                {
                    var ext = Path.GetExtension(_videoFileName).ToLowerInvariant();
                    if (ext == ".mkv")
                    {
                        bool isValid = false;
                        using (var mkv = new VideoFormats.Mkv())
                        {
                            bool   hasConstantFrameRate = true;
                            int    pixelWidth           = 0;
                            int    pixelHeight          = 0;
                            double millisecsDuration    = 0;
                            string videoCodec           = string.Empty;
                            mkv.GetMatroskaInfo(_videoFileName, ref isValid, ref hasConstantFrameRate, ref frameRate, ref pixelWidth, ref pixelHeight, ref millisecsDuration, ref videoCodec);
                        }
                        if (!isValid)
                        {
                            return;
                        }
                    }
                    if (frameRate < 1 && (ext == ".mp4" || ext == ".mov" || ext == ".m4v"))
                    {
                        var mp4 = new VideoFormats.MP4(_videoFileName);
                        frameRate = mp4.FrameRate;
                    }
                    if (frameRate < 1)
                    {
                        using (var rp = new VideoFormats.RiffParser())
                        {
                            var dh = new VideoFormats.RiffDecodeHeader(rp);
                            rp.OpenFile(_videoFileName);
                            if (VideoFormats.RiffParser.ckidAVI == rp.FileType)
                            {
                                dh.ProcessMainAVI();
                                frameRate = dh.FrameRate;
                            }
                        }
                    }
                }
                //CheckForIllegalCrossThreadCalls = false;
                Invoke(new MethodInvoker(() =>
                {
                    double minDiff = 100;
                    var index      = 0;
                    for (int i = 1; i < comboBoxFrameRate.Items.Count; i++)
                    {
                        var element = comboBoxFrameRate.Items[i];
                        double d;
                        if (double.TryParse(element.ToString(), NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out d))
                        {
                            double diff = Math.Abs(d - frameRate);
                            if (diff < 0.01 && diff < minDiff)
                            {
                                index   = i;
                                minDiff = diff;
                            }
                        }
                    }
                    comboBoxFrameRate.SelectedIndex = index;
                }));
            }
            catch
            {
            }

            // Restore controls stats
            Invoke(new MethodInvoker(() =>
            {
                Cursor           = Cursors.Default;
                labelStatus.Text = string.Empty;
                EnableDisableActionButtons(true);
                labelStatus.ForeColor = System.Drawing.Color.Black;
            }));
        }
Beispiel #8
0
 /// <summary>
 /// Default list element handler - skip the entire list
 /// </summary>
 /// <param name="rp"></param>
 /// <param name="FourCC"></param>
 /// <param name="length"></param>
 private void ProcessList(RiffParser rp, int FourCC, int length)
 {
     rp.SkipData(length);
 }
Beispiel #9
0
 public RiffDecodeHeader(RiffParser rp)
 {
     m_parser = rp;
 }