Пример #1
0
        public override void Close()
        {
            if (!_Initialized)
            {
                return;
            }

            _Initialized = false;

            if (!_FileOpened)
            {
                return;
            }

            if (_audiodecoder != IntPtr.Zero)
            {
                CAcinerella.ac_free_decoder(_audiodecoder);
            }

            if (_instance != IntPtr.Zero)
            {
                CAcinerella.ac_close(_instance);
            }

            if (_instance != IntPtr.Zero)
            {
                CAcinerella.ac_free(_instance);
            }

            _FileOpened = false;
        }
Пример #2
0
        public void Decode(out byte[] buffer, out float timeStamp)
        {
            if (!_FileOpened)
            {
                buffer    = null;
                timeStamp = 0f;
                return;
            }

            bool frameFinished = CAcinerella.AcGetAudioFrame(_InstancePtr, _Audiodecoder);

            if (frameFinished)
            {
                var decoder = (SACDecoder)Marshal.PtrToStructure(_Audiodecoder, typeof(SACDecoder));

                timeStamp    = (float)decoder.Timecode;
                _CurrentTime = timeStamp;
                //Console.WriteLine(_CurrentTime.ToString("#0.000") + " Buffer size: " + Decoder.buffer_size.ToString());
                buffer = new byte[decoder.BufferSize];

                if (decoder.BufferSize > 0)
                {
                    Marshal.Copy(decoder.Buffer, buffer, 0, buffer.Length);
                }

                return;
            }

            buffer    = null;
            timeStamp = 0f;
        }
Пример #3
0
        private void DoSkip()
        {
            _VideoSkipTime = _Gap;
            _SkipTime      = _Start + _Gap;
            _BeforeLoop    = false;

            if (_SkipTime > 0)
            {
                _VideoDecoderTime = _SkipTime;
                try
                {
                    CAcinerella.ac_seek(_videodecoder, -1, (Int64)(_SkipTime * 1000f));
                }
                catch (Exception e)
                {
                    CLog.LogError("Error seeking video file \"" + _FileName + "\": " + e.Message);
                }
            }
            else
            {
                _VideoDecoderTime = 0f;
                try
                {
                    CAcinerella.ac_seek(_videodecoder, -1, (Int64)0);
                }
                catch (Exception e)
                {
                    CLog.LogError("Error seeking video file \"" + _FileName + "\": " + e.Message);
                }
            }

            lock (MutexSyncSignals)
            {
                _CurrentVideoTime = _VideoDecoderTime;
            }

            lock (MutexFramebuffer)
            {
                for (int i = 0; i < _FrameBuffer.Length; i++)
                {
                    _FrameBuffer[i].displayed = true;
                    _FrameBuffer[i].time      = -1f;
                }
            }

            _BufferFull = false;
            _skip       = false;
            _NewFrame   = false;
            //EventDecode.Set();
        }
Пример #4
0
        public void Close()
        {
            if (_Audiodecoder != IntPtr.Zero)
            {
                CAcinerella.AcFreeDecoder(_Audiodecoder);
                _Audiodecoder = IntPtr.Zero;
            }
            if (_InstancePtr != IntPtr.Zero)
            {
                CAcinerella.AcClose(_InstancePtr);
                CAcinerella.AcFree(_InstancePtr);
                _InstancePtr = IntPtr.Zero;
            }

            _FileOpened = false;
        }
Пример #5
0
        public override void SetPosition(float Time)
        {
            if (!_Initialized && !_FileOpened)
            {
                return;
            }

            try
            {
                CAcinerella.ac_seek(_audiodecoder, 0, (Int64)(Time * 1000f));
            }
            catch (Exception)
            {
                CLog.LogError("Error seeking in file: " + _FileName);
                Close();
            }
        }
Пример #6
0
        public void SetPosition(float time)
        {
            if (!_FileOpened)
            {
                return;
            }

            try
            {
                CAcinerella.AcSeek(_Audiodecoder, (time > _CurrentTime) ? 0 : -1, (Int64)(time * 1000f));
            }
            catch (Exception)
            {
                CLog.Error("Error seeking in file: " + _FileName);
                Close();
            }
        }
Пример #7
0
        private void DoFree()
        {
            if (_videodecoder != IntPtr.Zero)
            {
                CAcinerella.ac_free_decoder(_videodecoder);
            }

            if (_instance != IntPtr.Zero)
            {
                CAcinerella.ac_close(_instance);
            }

            if (_instance != IntPtr.Zero)
            {
                CAcinerella.ac_free(_instance);
            }

            _Closeproc(_StreamID);
        }
Пример #8
0
        public override void Decode(out byte[] Buffer, out float TimeStamp)
        {
            if (!_Initialized && !_FileOpened)
            {
                Buffer    = null;
                TimeStamp = 0f;
                return;
            }

            int FrameFinished = 0;

            try
            {
                FrameFinished = CAcinerella.ac_get_audio_frame(_instance, _audiodecoder);
            }
            catch (Exception)
            {
                FrameFinished = 0;
            }

            if (FrameFinished == 1)
            {
                TAc_decoder Decoder = (TAc_decoder)Marshal.PtrToStructure(_audiodecoder, typeof(TAc_decoder));

                TimeStamp    = (float)Decoder.timecode;
                _CurrentTime = TimeStamp;
                //Console.WriteLine(_CurrentTime.ToString("#0.000") + " Buffer size: " + Decoder.buffer_size.ToString());

                Buffer = new byte[Decoder.buffer_size];

                if (Decoder.buffer_size > 0)
                {
                    Marshal.Copy(Decoder.buffer, Buffer, 0, Buffer.Length);
                }

                return;
            }

            Buffer    = null;
            TimeStamp = 0f;
        }
Пример #9
0
        private void DoDecode()
        {
            const int FRAMEDROPCOUNT = 4;

            if (!_FileOpened || _NewFrame)
            {
                return;
            }

            if (_Paused || _NoMoreFrames || _BufferFull)
            {
                return;
            }

            if ((_SkipTime < 0f) && (_Time + _SkipTime >= 0f))
            {
                _SkipTime = 0f;
            }

            float myTime         = _Time + _VideoSkipTime;
            float TimeDifference = myTime - _VideoDecoderTime;

            bool DropFrame = false;

            if (TimeDifference >= (FRAMEDROPCOUNT - 1) * _VideoTimeBase)
            {
                DropFrame = true;
            }

            if (_terminated)
            {
                return;
            }

            int FrameFinished = 0;

            if (DropFrame && !_BeforeLoop)
            {
                try
                {
                    FrameFinished = CAcinerella.ac_skip_frames(_instance, _videodecoder, FRAMEDROPCOUNT - 1);
                }
                catch (Exception)
                {
                    CLog.LogError("Error AcSkipFrame " + _FileName);
                }
            }

            if (!_BeforeLoop && (!DropFrame || FrameFinished != 0))
            {
                try
                {
                    FrameFinished = CAcinerella.ac_get_frame(_instance, _videodecoder);
                }
                catch (Exception)
                {
                    CLog.LogError("Error AcGetFrame " + _FileName);
                }
            }

            if (FrameFinished == 0)
            {
                if (_Loop)
                {
                    _BeforeLoop = true;
                    bool  doskip = true;
                    float tm     = 0f;
                    int   num    = -1;
                    lock (MutexFramebuffer)
                    {
                        for (int i = 0; i < _FrameBuffer.Length; i++)
                        {
                            if (_FrameBuffer[i].time > tm && !_FrameBuffer[i].displayed)
                            {
                                tm  = _FrameBuffer[i].time;
                                num = i;
                            }
                        }

                        if (num >= 0)
                        {
                            doskip = _FrameBuffer[num].displayed;
                        }
                    }

                    if (!doskip)
                    {
                        return;
                    }

                    lock (MutexSyncSignals)
                    {
                        _Start   = 0f;
                        _Gap     = 0f;
                        _SetTime = 0f;
                    }

                    DoSkip();
                }
                else
                {
                    _NoMoreFrames = true;
                }
                return;
            }
            else
            {
                _NewFrame = true;
                Copy();
            }
        }
Пример #10
0
        private void DoOpen()
        {
            bool         ok       = false;
            TAc_instance Instance = new TAc_instance();

            try
            {
                _fs = new FileStream(_FileName, FileMode.Open, FileAccess.Read, FileShare.Read);


                _instance = CAcinerella.ac_init();
                CAcinerella.ac_open(_instance, IntPtr.Zero, null, _rc, _sc, null, IntPtr.Zero);

                Instance = (TAc_instance)Marshal.PtrToStructure(_instance, typeof(TAc_instance));
                ok       = true;
            }
            catch (Exception)
            {
                CLog.LogError("Error opening video file: " + _FileName);
                ok = false;
            }


            if (!Instance.opened || !ok)
            {
                //Free();
                return;
            }

            _Duration = (float)Instance.info.duration / 1000f;

            int VideoStreamIndex = -1;

            TAc_stream_info Info = new TAc_stream_info();

            for (int i = 0; i < Instance.stream_count; i++)
            {
                CAcinerella.ac_get_stream_info(_instance, i, out Info);

                if (Info.stream_type == TAc_stream_type.AC_STREAM_TYPE_VIDEO)
                {
                    _videodecoder = CAcinerella.ac_create_decoder(_instance, i);

                    VideoStreamIndex = i;
                    break;
                }
            }

            if (VideoStreamIndex < 0)
            {
                //Free();
                return;
            }

            TAc_decoder Videodecoder = (TAc_decoder)Marshal.PtrToStructure(_videodecoder, typeof(TAc_decoder));

            _Width  = Videodecoder.stream_info.video_info.frame_width;
            _Height = Videodecoder.stream_info.video_info.frame_height;

            if (Videodecoder.stream_info.video_info.frames_per_second > 0)
            {
                _VideoTimeBase = 1f / (float)Videodecoder.stream_info.video_info.frames_per_second;
            }

            _VideoDecoderTime = 0f;
            _Time             = 0f;

            for (int i = 0; i < _FrameBuffer.Length; i++)
            {
                _FrameBuffer[i].time      = -1f;
                _FrameBuffer[i].displayed = true;
                _FrameBuffer[i].data      = new byte[_Width * _Height * 4];
            }
            _FileOpened = true;
        }
Пример #11
0
        public override void Open(string FileName, bool Loop)
        {
            if (!_Initialized)
            {
                return;
            }

            _FileName = FileName;
            _fs       = new FileStream(FileName, FileMode.Open, FileAccess.Read, FileShare.Read);

            _instance = CAcinerella.ac_init();
            CAcinerella.ac_open(_instance, IntPtr.Zero, null, _rc, _sc, null, IntPtr.Zero);

            _Instance = (TAc_instance)Marshal.PtrToStructure(_instance, typeof(TAc_instance));

            if (!_Instance.opened)
            {
                //Free();
                return;
            }

            int AudioStreamIndex = -1;

            TAc_stream_info Info = new TAc_stream_info();

            for (int i = 0; i < _Instance.stream_count; i++)
            {
                CAcinerella.ac_get_stream_info(_instance, i, out Info);

                if (Info.stream_type == TAc_stream_type.AC_STREAM_TYPE_AUDIO)
                {
                    try
                    {
                        _audiodecoder = CAcinerella.ac_create_decoder(_instance, i);
                    }
                    catch (Exception)
                    {
                        return;
                    }

                    AudioStreamIndex = i;
                    break;
                }
            }

            if (AudioStreamIndex < 0)
            {
                //Free();
                return;
            }

            TAc_decoder Audiodecoder = (TAc_decoder)Marshal.PtrToStructure(_audiodecoder, typeof(TAc_decoder));

            _FormatInfo = new FormatInfo();

            _FormatInfo.SamplesPerSecond = Audiodecoder.stream_info.audio_info.samples_per_second;
            _FormatInfo.BitDepth         = Audiodecoder.stream_info.audio_info.bit_depth;
            _FormatInfo.ChannelCount     = Audiodecoder.stream_info.audio_info.channel_count;

            _CurrentTime = 0f;

            if (_FormatInfo.BitDepth != 16)
            {
                CLog.LogError("Unsupported BitDepth in file " + FileName);
                return;
            }
            _FileOpened = true;
        }
Пример #12
0
        public bool Open(string fileName)
        {
            _FileName = fileName;

            try
            {
                _InstancePtr = CAcinerella.AcInit();
                CAcinerella.AcOpen2(_InstancePtr, _FileName);
                _Instance = (SACInstance)Marshal.PtrToStructure(_InstancePtr, typeof(SACInstance));
            }
            catch (Exception)
            {
                CLog.Error("Error opening audio file: " + _FileName);
                Close();
                return(false);
            }


            if (!_Instance.Opened)
            {
                Close();
                return(false);
            }

            int        audioStreamIndex;
            SACDecoder audiodecoder;

            try
            {
                _Audiodecoder    = CAcinerella.AcCreateAudioDecoder(_InstancePtr);
                audiodecoder     = (SACDecoder)Marshal.PtrToStructure(_Audiodecoder, typeof(SACDecoder));
                audioStreamIndex = audiodecoder.StreamIndex;
            }
            catch (Exception)
            {
                CLog.Error("Error opening audio file (can't find decoder): " + _FileName);
                Close();
                return(false);
            }

            if (audioStreamIndex < 0)
            {
                Close();
                return(false);
            }

            _FormatInfo = new SFormatInfo
            {
                SamplesPerSecond = audiodecoder.StreamInfo.AudioInfo.SamplesPerSecond,
                BitDepth         = audiodecoder.StreamInfo.AudioInfo.BitDepth,
                ChannelCount     = audiodecoder.StreamInfo.AudioInfo.ChannelCount
            };

            _CurrentTime = 0f;

            if (_FormatInfo.BitDepth != 16)
            {
                CLog.Error("Unsupported BitDepth in file " + fileName);
                Close();
                return(false);
            }
            _FileOpened = true;
            return(true);
        }