/// <summary>
        /// Decodes one frame of AAC data in frame mode and returns the raw PCM data.
        /// </summary>
        /// <param name="frame">the AAC frame</param>
        /// <param name="buffer">a buffer to hold the decoded PCM data</param>
        public void decodeFrame(byte[] frame, SampleBuffer buffer)
        {
            if (frame != null)
            {
                inStream.setData(frame);
            }
//      try
            {
                decode(buffer);
            }
            //catch (AACException e)
            //{
            //  Logger.LogServe("unexpected end of frame: " + e);
            //}
            // throw new NotImplementedException();
        }
Пример #2
0
        private static void decodeMP4(string inFile, string outFile)
        {
            using (var inFileStream = File.OpenRead(inFile))
            {
                var cont = new MP4Container(inFileStream);

                var movie = cont.getMovie();

                // --- read cover-picture if exists ---
                if (movie.containsMetaData())
                {
                    var meta = movie.getMetaData();
                    if (meta.contains(MetaData.Fields.COVER_ARTWORKS))
                    {
                        coverPicture = meta.get(MetaData.Fields.COVER_ARTWORKS).First().getImage();
                    }
                }


                var tracks = movie.getTracks(AudioTrack.AudioCodec.CodecType.AAC);
                if (tracks.Length == 0)
                {
                    throw new Exception("movie does not contain any AAC track");
                }
                var track = (AudioTrack)tracks[0];

                using (var wav = new WaveFileWriter(File.Create(outFile), track.getSampleRate(), track.getChannelCount(), track.getSampleSize()))
                {
                    var dec = new Decoder(track.getDecoderSpecificInfo());

                    var buf = new SampleBuffer();
                    while (track.hasMoreFrames())
                    {
                        if (track.currentFrame % 10 == 0)
                        {
                            Console.WriteLine(track.currentFrame.ToString("N0") + " / " + track.frames.Count.ToString("N0"));
                        }
                        var frame = track.readNextFrame();
                        dec.decodeFrame(frame.getData(), buf);
                        wav.write(buf.getData());
                    }
                }
            }
        }
        public void sendToOutput(SampleBuffer buffer)
        {
            bool be = buffer.isBigEndian();

            int chs    = data.Length;
            int mult   = (sbrPresent && config.isSBREnabled()) ? 2 : 1;
            int length = mult * config.getFrameLength();
            int freq   = mult * config.getSampleFrequency().getFrequency();

            byte[] b = buffer.getData();
            if (b.Length != chs * length * 2)
            {
                b = new byte[chs * length * 2];
            }

            float[] cur;
            int     i, j, off;
            short   s;

            for (i = 0; i < chs; i++)
            {
                cur = data[i];
                for (j = 0; j < length; j++)
                {
                    s   = (short)Math.Max(Math.Min(Math.Round(cur[j]), short.MaxValue), short.MinValue);
                    off = (j * chs + i) * 2;
                    if (be)
                    {
                        b[off]     = (byte)((s >> 8) & BYTE_MASK);
                        b[off + 1] = (byte)(s & BYTE_MASK);
                    }
                    else
                    {
                        b[off + 1] = (byte)((s >> 8) & BYTE_MASK);
                        b[off]     = (byte)(s & BYTE_MASK);
                    }
                }
            }

            buffer.setData(b, freq, chs, 16, bitsRead);
        }
        void decode(SampleBuffer buffer)
        {
            if (ADIFHeader.isPresent(inStream))
            {
                adifHeader = ADIFHeader.readHeader(inStream);
                var pce = adifHeader.getFirstPCE();
                config.setProfile(pce.getProfile());
                config.setSampleFrequency(pce.getSampleFrequency());
                config.setChannelConfiguration((ChannelConfiguration)pce.getChannelCount());
            }

            if (!canDecode(config.getProfile()))
            {
                throw new AACException("unsupported profile: " + config.getProfile());
            }

            syntacticElements.startNewFrame();

            //try
            {
                // 1: bitstream parsing and noiseless coding
                syntacticElements.decode(inStream);
                // 2: spectral processing
                syntacticElements.process(filterBank);
                // 3: send to output buffer
                syntacticElements.sendToOutput(buffer);
            }
            //catch (AACException e)
            //{
            //  buffer.setData(new byte[0], 0, 0, 0, 0);
            //  throw e;
            //}
            //catch (Exception e)
            //{
            //  buffer.setData(new byte[0], 0, 0, 0, 0);
            //  throw new AACException(e);
            //}
            // throw new NotImplementedException();
        }