| 1 | /***************************************************************** |
| 2 | | |
| 3 | | AP4 - AAC to MP4 Converter |
| 4 | | |
| 5 | | Copyright 2002-2008 Axiomatic Systems, LLC |
| 6 | | |
| 7 | | |
| 8 | | This file is part of Bento4/AP4 (MP4 Atom Processing Library). |
| 9 | | |
| 10 | | Unless you have obtained Bento4 under a difference license, |
| 11 | | this version of Bento4 is Bento4|GPL. |
| 12 | | Bento4|GPL is free software; you can redistribute it and/or modify |
| 13 | | it under the terms of the GNU General Public License as published by |
| 14 | | the Free Software Foundation; either version 2, or (at your option) |
| 15 | | any later version. |
| 16 | | |
| 17 | | Bento4|GPL is distributed in the hope that it will be useful, |
| 18 | | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | | GNU General Public License for more details. |
| 21 | | |
| 22 | | You should have received a copy of the GNU General Public License |
| 23 | | along with Bento4|GPL; see the file COPYING. If not, write to the |
| 24 | | Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA |
| 25 | | 02111-1307, USA. |
| 26 | | |
| 27 | ****************************************************************/ |
| 28 | |
| 29 | /*---------------------------------------------------------------------- |
| 30 | | includes |
| 31 | +---------------------------------------------------------------------*/ |
| 32 | #include <stdio.h> |
| 33 | #include <stdlib.h> |
| 34 | |
| 35 | #include "Ap4.h" |
| 36 | #include "Ap4AdtsParser.h" |
| 37 | |
| 38 | /*---------------------------------------------------------------------- |
| 39 | | constants |
| 40 | +---------------------------------------------------------------------*/ |
| 41 | #define BANNER "AAC to MP4 Converter - Version 1.0\n"\ |
| 42 | "(Bento4 Version " AP4_VERSION_STRING ")\n"\ |
| 43 | "(c) 2002-2008 Axiomatic Systems, LLC" |
| 44 | |
| 45 | /*---------------------------------------------------------------------- |
| 46 | | PrintUsageAndExit |
| 47 | +---------------------------------------------------------------------*/ |
| 48 | static void |
| 49 | PrintUsageAndExit() |
| 50 | { |
| 51 | fprintf(stderr, |
| 52 | BANNER |
| 53 | "\n\nusage: aac2mp4 <input> <output>\n"); |
| 54 | exit(1); |
| 55 | } |
| 56 | |
| 57 | /*---------------------------------------------------------------------- |
| 58 | | MakeDsi |
| 59 | +---------------------------------------------------------------------*/ |
| 60 | static void |
| 61 | MakeDsi(unsigned int sampling_frequency_index, unsigned int channel_configuration, unsigned char* dsi) |
| 62 | { |
| 63 | unsigned int object_type = 2; // AAC LC by default |
| 64 | dsi[0] = (object_type<<3) | (sampling_frequency_index>>1); |
| 65 | dsi[1] = ((sampling_frequency_index&1)<<7) | (channel_configuration<<3); |
| 66 | } |
| 67 | |
| 68 | /*---------------------------------------------------------------------- |
| 69 | | main |
| 70 | +---------------------------------------------------------------------*/ |
| 71 | int |
| 72 | main(int argc, char** argv) |
| 73 | { |
| 74 | AP4_Result result; |
| 75 | |
| 76 | if (argc < 3) { |
| 77 | PrintUsageAndExit(); |
| 78 | } |
| 79 | |
| 80 | // open the input |
| 81 | AP4_ByteStream* input = NULL; |
| 82 | result = AP4_FileByteStream::Create(argv[1], AP4_FileByteStream::STREAM_MODE_READ, input); |
| 83 | if (AP4_FAILED(result)) { |
| 84 | AP4_Debug("ERROR: cannot open input (%s) %d\n", argv[1], result); |
| 85 | return 1; |
| 86 | } |
| 87 | |
| 88 | // open the output |
| 89 | AP4_ByteStream* output = NULL; |
| 90 | result = AP4_FileByteStream::Create(argv[2], AP4_FileByteStream::STREAM_MODE_WRITE, output); |
| 91 | if (AP4_FAILED(result)) { |
| 92 | AP4_Debug("ERROR: cannot open output (%s) %d\n", argv[2], result); |
| 93 | return 1; |
| 94 | } |
| 95 | |
| 96 | // create a sample table |
| 97 | AP4_SyntheticSampleTable* sample_table = new AP4_SyntheticSampleTable(); |
| 98 | |
| 99 | // create an ADTS parser |
| 100 | AP4_AdtsParser parser; |
| 101 | bool initialized = false; |
| 102 | unsigned int sample_description_index = 0; |
| 103 | |
| 104 | // read from the input, feed, and get AAC frames |
| 105 | AP4_UI32 sample_rate = 0; |
| 106 | AP4_Cardinal sample_count = 0; |
| 107 | bool eos = false; |
| 108 | for(;;) { |
| 109 | // try to get a frame |
| 110 | AP4_AacFrame frame; |
| 111 | result = parser.FindFrame(frame); |
| 112 | if (AP4_SUCCEEDED(result)) { |
| 113 | AP4_Debug("AAC frame [%06d]: size = %d, %d kHz, %d ch\n", |
| 114 | sample_count, |
| 115 | frame.m_Info.m_FrameLength, |
| 116 | frame.m_Info.m_SamplingFrequency, |
| 117 | frame.m_Info.m_ChannelConfiguration); |
| 118 | if (!initialized) { |
| 119 | initialized = true; |
| 120 | |
| 121 | // create a sample description for our samples |
| 122 | AP4_DataBuffer dsi; |
| 123 | unsigned char aac_dsi[2]; |
| 124 | MakeDsi(frame.m_Info.m_SamplingFrequencyIndex, frame.m_Info.m_ChannelConfiguration, aac_dsi); |
| 125 | dsi.SetData(aac_dsi, 2); |
| 126 | AP4_MpegAudioSampleDescription* sample_description = |
| 127 | new AP4_MpegAudioSampleDescription( |
| 128 | AP4_OTI_MPEG4_AUDIO, // object type |
| 129 | frame.m_Info.m_SamplingFrequency, |
| 130 | 16, // sample size |
| 131 | frame.m_Info.m_ChannelConfiguration, |
| 132 | &dsi, // decoder info |
| 133 | 6144, // buffer size |
| 134 | 128000, // max bitrate |
| 135 | 128000); // average bitrate |
| 136 | sample_description_index = sample_table->GetSampleDescriptionCount(); |
| 137 | sample_table->AddSampleDescription(sample_description); |
| 138 | sample_rate = frame.m_Info.m_SamplingFrequency; |
| 139 | } |
| 140 | |
| 141 | AP4_MemoryByteStream* sample_data = new AP4_MemoryByteStream(frame.m_Info.m_FrameLength); |
| 142 | frame.m_Source->ReadBytes(sample_data->UseData(), frame.m_Info.m_FrameLength); |
| 143 | sample_table->AddSample(*sample_data, 0, frame.m_Info.m_FrameLength, 1024, sample_description_index, 0, 0, true); |
| 144 | sample_data->Release(); |
| 145 | sample_count++; |
| 146 | } else if (!eos) { |
| 147 | // read some data and feed the parser |
| 148 | AP4_UI08 input_buffer[4096]; |
| 149 | AP4_Size to_read = parser.GetBytesFree(); |
| 150 | if (to_read) { |
| 151 | AP4_Size bytes_read = 0; |
| 152 | if (to_read > sizeof(input_buffer)) to_read = sizeof(input_buffer); |
| 153 | result = input->ReadPartial(input_buffer, to_read, bytes_read); |
| 154 | if (AP4_SUCCEEDED(result)) { |
| 155 | AP4_Size to_feed = bytes_read; |
| 156 | result = parser.Feed(input_buffer, &to_feed); |
| 157 | if (AP4_FAILED(result)) { |
| 158 | AP4_Debug("ERROR: parser.Feed() failed (%d)\n", result); |
| 159 | return 1; |
| 160 | } |
| 161 | } else { |
| 162 | if (result == AP4_ERROR_EOS) { |
| 163 | eos = true; |
| 164 | parser.Feed(NULL, 0, true); |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | } else { |
| 169 | break; |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | // create a movie |
| 174 | AP4_Movie* movie = new AP4_Movie(); |
| 175 | |
| 176 | // create an audio track |
| 177 | AP4_Track* track = new AP4_Track(AP4_Track::TYPE_AUDIO, |
| 178 | sample_table, |
| 179 | 0, // track id |
| 180 | sample_rate, // movie time scale |
| 181 | sample_count*1024, // track duration |
| 182 | sample_rate, // media time scale |
| 183 | sample_count*1024, // media duration |
| 184 | "eng", // language |
| 185 | 0, 0); // width, height |
| 186 | |
| 187 | // add the track to the movie |
| 188 | movie->AddTrack(track); |
| 189 | |
| 190 | // create a multimedia file |
| 191 | AP4_File* file = new AP4_File(movie); |
| 192 | |
| 193 | // set the file type |
| 194 | AP4_UI32 compatible_brands[2] = { |
| 195 | AP4_FILE_BRAND_ISOM, |
| 196 | AP4_FILE_BRAND_MP42 |
| 197 | }; |
| 198 | file->SetFileType(AP4_FILE_BRAND_M4A_, 0, compatible_brands, 2); |
| 199 | |
| 200 | // write the file to the output |
| 201 | AP4_FileWriter::Write(*file, *output); |
| 202 | |
| 203 | delete file; |
| 204 | input->Release(); |
| 205 | output->Release(); |
| 206 | |
| 207 | return 0; |
| 208 | } |