Example #1
0
        public static SnmFile FromAviFile(string fileName)
        {
            if (!File.Exists(fileName))
            {
                throw new FileNotFoundException();
            }

            var snm        = new SnmFile();
            var aviManager = new AviManager(fileName);

            try
            {
                AudioStream audioStream = aviManager.GetWaveStream();
                byte[]      audioData   = null;

                if (audioStream != null)
                {
                    if (audioStream.ChannelsCount != 2 || audioStream.BitsPerSample != 16)
                    {
                        throw new InvalidDataException();
                    }

                    if (audioStream.SamplesPerSecond != 22050 && audioStream.SamplesPerSecond != 44100)
                    {
                        throw new NotSupportedException();
                    }

                    snm.AudioHeader = new SnmAudioHeader
                    {
                        Frequency   = audioStream.SamplesPerSecond,
                        NumChannels = audioStream.ChannelsCount
                    };

                    audioData = audioStream.GetStreamData();

                    if (snm.AudioHeader.Frequency == 44100)
                    {
                        snm.AudioHeader.Frequency = 22050;
                        audioData = SnmFile.ConvertAudio44100To22050(audioData);
                    }
                }

                VideoStream videoStream = aviManager.GetVideoStream();

                if (videoStream.BitsPerPixel != 24 && videoStream.BitsPerPixel != 32)
                {
                    throw new NotSupportedException();
                }

                snm.Header.FrameDelay = (int)(1000000 / videoStream.FrameRate + 0.5);
                snm.Header.Width      = (short)videoStream.Width;
                snm.Header.Height     = (short)videoStream.Height;
                snm.Header.NumFrames  = (short)videoStream.FramesCount;

                for (int i = 0; i < videoStream.FramesCount; i++)
                {
                    snm.VideoHeaders.Add(new SnmVideoHeader
                    {
                        Width  = snm.Header.Width,
                        Height = snm.Header.Height
                    });
                }

                videoStream.GetFrameOpen();

                try
                {
                    int fps             = (1000000 + snm.Header.FrameDelay / 2) / snm.Header.FrameDelay;
                    int samplesPerFrame = snm.AudioHeader.Frequency / fps;

                    if (samplesPerFrame * fps != snm.AudioHeader.Frequency)
                    {
                        throw new InvalidDataException();
                    }

                    for (int i = 0; i < videoStream.FramesCount; i++)
                    {
                        byte[] videoData = videoStream.GetFrameData(i);

                        var frame = new SnmFrame();

                        if (audioData != null)
                        {
                            int audioPosition = i * samplesPerFrame * 4;
                            int audioLength   = Math.Min(samplesPerFrame * 4, audioData.Length - audioPosition);

                            if (audioPosition < audioData.Length && audioLength != 0)
                            {
                                frame.Audio = new SnmAudioFrame
                                {
                                    NumSamples = audioLength / 4
                                };

                                byte[] buffer = new byte[audioLength];
                                Array.Copy(audioData, audioPosition, buffer, 0, audioLength);

                                //frame.Audio.Data = Imc.Vima.Compress(buffer, 2);

                                frame.Audio.Data = buffer;
                            }
                        }

                        if (videoData != null)
                        {
                            frame.Video = new SnmVideoFrame
                            {
                                Width         = snm.Header.Width,
                                Height        = snm.Header.Height,
                                RleOutputSize = snm.Header.Width * snm.Header.Height * 2,

                                SubcodecId = (byte)videoStream.BitsPerPixel,
                                Data       = videoData
                            };

                            //byte[] buffer;

                            //if (videoStream.BitsPerPixel == 24)
                            //{
                            //    buffer = SnmFile.Convert24BppTo16Bpp(videoData);
                            //}
                            //else
                            //{
                            //    buffer = SnmFile.Convert32BppTo16Bpp(videoData);
                            //}

                            //byte subcodecId;
                            //frame.Video.Data = Blocky16.Compress(buffer, out subcodecId);
                            //frame.Video.SubcodecId = subcodecId;
                        }

                        snm.Frames.Add(frame);
                    }
                }
                finally
                {
                    videoStream.GetFrameClose();
                }
            }
            finally
            {
                aviManager.Close();
            }

            snm.Frames
            .AsParallel()
            .ForAll(frame =>
            {
                if (frame.Audio != null)
                {
                    frame.Audio.Data = Imc.Vima.Compress(frame.Audio.Data, 2);
                }

                if (frame.Video != null)
                {
                    byte[] buffer;

                    if (frame.Video.SubcodecId == 24)
                    {
                        buffer = SnmFile.Convert24BppTo16Bpp(frame.Video.Data);
                    }
                    else
                    {
                        buffer = SnmFile.Convert32BppTo16Bpp(frame.Video.Data);
                    }

                    frame.Video.Data       = Blocky16.Compress(buffer, out byte subcodecId);
                    frame.Video.SubcodecId = subcodecId;
                }
            });

            return(snm);
        }