"Fossies" - the Fresh Open Source Software Archive

Member "quicktime4linux-2.3/thirdparty/faad2-2.0/plugins/QCDMp4/aac2mp4.cpp" (31 May 2008, 7641 Bytes) of package /linux/privat/old/quicktime4linux-2.3-src.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 /*
    2 ** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
    3 ** Copyright (C) 2003 M. Bakker, Ahead Software AG, http://www.nero.com
    4 **  
    5 ** This program is free software; you can redistribute it and/or modify
    6 ** it under the terms of the GNU General Public License as published by
    7 ** the Free Software Foundation; either version 2 of the License, or
    8 ** (at your option) any later version.
    9 ** 
   10 ** This program is distributed in the hope that it will be useful,
   11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
   12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   13 ** GNU General Public License for more details.
   14 ** 
   15 ** You should have received a copy of the GNU General Public License
   16 ** along with this program; if not, write to the Free Software 
   17 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
   18 **
   19 ** Any non-GPL usage of this software or parts of this software is strictly
   20 ** forbidden.
   21 **
   22 ** Commercial non-GPL licensing of this software is possible.
   23 ** For more info contact Ahead Software through Mpeg4AAClicense@nero.com.
   24 **
   25 ** $Id: aac2mp4.cpp,v 1.3 2003/12/06 04:24:17 rjamorim Exp $
   26 **/
   27 
   28 #include <mpeg4ip.h>
   29 #include <mp4.h>
   30 #include <mp4av.h>
   31 
   32 #include "aac2mp4.h"
   33 
   34 int covert_aac_to_mp4(char *inputFileName, char *mp4FileName)
   35 {
   36     int Mp4TimeScale = 90000;
   37     int allMpeg4Streams = 0;
   38     MP4FileHandle mp4File;
   39     FILE* inFile;
   40     const char *type;
   41     MP4TrackId createdTrackId = MP4_INVALID_TRACK_ID;
   42 
   43     mp4File = MP4Create(mp4FileName, 0, 0, 0);
   44     if (mp4File)
   45     {
   46         MP4SetTimeScale(mp4File, Mp4TimeScale);
   47     } else {
   48         return 1;
   49     }
   50 
   51     inFile = fopen(inputFileName, "rb");
   52 
   53     if (inFile == NULL)
   54     {
   55         MP4Close(mp4File);
   56         return 2;
   57     }
   58 
   59     createdTrackId = AacCreator(mp4File, inFile);
   60 
   61     if (createdTrackId == MP4_INVALID_TRACK_ID)
   62     {
   63         fclose(inFile);
   64         MP4Close(mp4File);
   65         return 3;
   66     }
   67 
   68     type = MP4GetTrackType(mp4File, createdTrackId);
   69 
   70     if (!strcmp(type, MP4_AUDIO_TRACK_TYPE))
   71     {
   72         allMpeg4Streams &=
   73             (MP4GetTrackAudioType(mp4File, createdTrackId)
   74             == MP4_MPEG4_AUDIO_TYPE);
   75     }
   76 
   77     if (inFile)
   78     {
   79         fclose(inFile);
   80     }
   81 
   82     MP4Close(mp4File);
   83     MP4MakeIsmaCompliant(mp4FileName, 0, allMpeg4Streams);
   84 
   85     return 0;
   86 }
   87 
   88 #define ADTS_HEADER_MAX_SIZE 10 /* bytes */
   89 
   90 static u_int8_t firstHeader[ADTS_HEADER_MAX_SIZE];
   91 
   92 /* 
   93  * hdr must point to at least ADTS_HEADER_MAX_SIZE bytes of memory 
   94  */
   95 static bool LoadNextAdtsHeader(FILE* inFile, u_int8_t* hdr)
   96 {
   97     u_int state = 0;
   98     u_int dropped = 0;
   99     u_int hdrByteSize = ADTS_HEADER_MAX_SIZE;
  100 
  101     while (1) {
  102         /* read a byte */
  103         u_int8_t b;
  104 
  105         if (fread(&b, 1, 1, inFile) == 0) {
  106             return false;
  107         }
  108 
  109         /* header is complete, return it */
  110         if (state == hdrByteSize - 1) {
  111             hdr[state] = b;
  112             if (dropped > 0) {
  113                 fprintf(stderr, "Warning: dropped %u input bytes\n", dropped);
  114             }
  115             return true;
  116         }
  117 
  118         /* collect requisite number of bytes, no constraints on data */
  119         if (state >= 2) {
  120             hdr[state++] = b;
  121         } else {
  122             /* have first byte, check if we have 1111X00X */
  123             if (state == 1) {
  124                 if ((b & 0xF6) == 0xF0) {
  125                     hdr[state] = b;
  126                     state = 2;
  127                     /* compute desired header size */
  128                     hdrByteSize = MP4AV_AdtsGetHeaderByteSize(hdr);
  129                 } else {
  130                     state = 0;
  131                 }
  132             }
  133             /* initial state, looking for 11111111 */
  134             if (state == 0) {
  135                 if (b == 0xFF) {
  136                     hdr[state] = b;
  137                     state = 1;
  138                 } else {
  139                      /* else drop it */ 
  140                     dropped++;
  141                 }
  142             }
  143         }
  144     }
  145 }
  146 
  147 /*
  148  * Load the next frame from the file
  149  * into the supplied buffer, which better be large enough!
  150  *
  151  * Note: Frames are padded to byte boundaries
  152  */
  153 static bool LoadNextAacFrame(FILE* inFile, u_int8_t* pBuf, u_int32_t* pBufSize, bool stripAdts)
  154 {
  155     u_int16_t frameSize;
  156     u_int16_t hdrBitSize, hdrByteSize;
  157     u_int8_t hdrBuf[ADTS_HEADER_MAX_SIZE];
  158 
  159     /* get the next AAC frame header, more or less */
  160     if (!LoadNextAdtsHeader(inFile, hdrBuf)) {
  161         return false;
  162     }
  163     
  164     /* get frame size from header */
  165     frameSize = MP4AV_AdtsGetFrameSize(hdrBuf);
  166 
  167     /* get header size in bits and bytes from header */
  168     hdrBitSize = MP4AV_AdtsGetHeaderBitSize(hdrBuf);
  169     hdrByteSize = MP4AV_AdtsGetHeaderByteSize(hdrBuf);
  170     
  171     /* adjust the frame size to what remains to be read */
  172     frameSize -= hdrByteSize;
  173 
  174     if (stripAdts) {
  175         if ((hdrBitSize % 8) == 0) {
  176             /* header is byte aligned, i.e. MPEG-2 ADTS */
  177             /* read the frame data into the buffer */
  178             if (fread(pBuf, 1, frameSize, inFile) != frameSize) {
  179                 return false;
  180             }
  181             (*pBufSize) = frameSize;
  182         } else {
  183             /* header is not byte aligned, i.e. MPEG-4 ADTS */
  184             int i;
  185             u_int8_t newByte;
  186             int upShift = hdrBitSize % 8;
  187             int downShift = 8 - upShift;
  188 
  189             pBuf[0] = hdrBuf[hdrBitSize / 8] << upShift;
  190 
  191             for (i = 0; i < frameSize; i++) {
  192                 if (fread(&newByte, 1, 1, inFile) != 1) {
  193                     return false;
  194                 }
  195                 pBuf[i] |= (newByte >> downShift);
  196                 pBuf[i+1] = (newByte << upShift);
  197             }
  198             (*pBufSize) = frameSize + 1;
  199         }
  200     } else { /* don't strip ADTS headers */
  201         memcpy(pBuf, hdrBuf, hdrByteSize);
  202         if (fread(&pBuf[hdrByteSize], 1, frameSize, inFile) != frameSize) {
  203             return false;
  204         }
  205     }
  206 
  207     return true;
  208 }
  209 
  210 static bool GetFirstHeader(FILE* inFile)
  211 {
  212     /* read file until we find an audio frame */
  213     fpos_t curPos;
  214 
  215     /* already read first header */
  216     if (firstHeader[0] == 0xff) {
  217         return true;
  218     }
  219 
  220     /* remember where we are */
  221     fgetpos(inFile, &curPos);
  222     
  223     /* go back to start of file */
  224     rewind(inFile);
  225 
  226     if (!LoadNextAdtsHeader(inFile, firstHeader)) {
  227         return false;
  228     }
  229 
  230     /* reposition the file to where we were */
  231     fsetpos(inFile, &curPos);
  232 
  233     return true;
  234 }
  235 
  236 MP4TrackId AacCreator(MP4FileHandle mp4File, FILE* inFile)
  237 {
  238     // collect all the necessary meta information
  239     u_int32_t samplesPerSecond;
  240     u_int8_t mpegVersion;
  241     u_int8_t profile;
  242     u_int8_t channelConfig;
  243 
  244     if (!GetFirstHeader(inFile)) {
  245         return MP4_INVALID_TRACK_ID;
  246     }
  247 
  248     samplesPerSecond = MP4AV_AdtsGetSamplingRate(firstHeader);
  249     mpegVersion = MP4AV_AdtsGetVersion(firstHeader);
  250     profile = MP4AV_AdtsGetProfile(firstHeader);
  251     channelConfig = MP4AV_AdtsGetChannels(firstHeader);
  252 
  253     u_int8_t audioType = MP4_INVALID_AUDIO_TYPE;
  254     switch (mpegVersion) {
  255     case 0:
  256         audioType = MP4_MPEG4_AUDIO_TYPE;
  257         break;
  258     case 1:
  259         switch (profile) {
  260         case 0:
  261             audioType = MP4_MPEG2_AAC_MAIN_AUDIO_TYPE;
  262             break;
  263         case 1:
  264             audioType = MP4_MPEG2_AAC_LC_AUDIO_TYPE;
  265             break;
  266         case 2:
  267             audioType = MP4_MPEG2_AAC_SSR_AUDIO_TYPE;
  268             break;
  269         case 3:
  270             return MP4_INVALID_TRACK_ID;
  271         }
  272         break;
  273     }
  274 
  275     // add the new audio track
  276     MP4TrackId trackId = 
  277         MP4AddAudioTrack(mp4File, 
  278             samplesPerSecond, 1024, audioType);
  279 
  280     if (trackId == MP4_INVALID_TRACK_ID) {
  281         return MP4_INVALID_TRACK_ID;
  282     }
  283 
  284     if (MP4GetNumberOfTracks(mp4File, MP4_AUDIO_TRACK_TYPE) == 1) {
  285         MP4SetAudioProfileLevel(mp4File, 0x0F);
  286     }
  287 
  288     u_int8_t* pConfig = NULL;
  289     u_int32_t configLength = 0;
  290 
  291     MP4AV_AacGetConfiguration(
  292         &pConfig,
  293         &configLength,
  294         profile,
  295         samplesPerSecond,
  296         channelConfig);
  297 
  298     if (!MP4SetTrackESConfiguration(mp4File, trackId, 
  299       pConfig, configLength)) {
  300         MP4DeleteTrack(mp4File, trackId);
  301         return MP4_INVALID_TRACK_ID;
  302     }
  303 
  304     // parse the ADTS frames, and write the MP4 samples
  305     u_int8_t sampleBuffer[8 * 1024];
  306     u_int32_t sampleSize = sizeof(sampleBuffer);
  307     MP4SampleId sampleId = 1;
  308 
  309     while (LoadNextAacFrame(inFile, sampleBuffer, &sampleSize, true)) {
  310         if (!MP4WriteSample(mp4File, trackId, sampleBuffer, sampleSize)) {
  311             MP4DeleteTrack(mp4File, trackId);
  312             return MP4_INVALID_TRACK_ID;
  313         }
  314         sampleId++;
  315         sampleSize = sizeof(sampleBuffer);
  316     }
  317 
  318     return trackId;
  319 }