| 1 | /***************************************************************** |
| 2 | | |
| 3 | | AP4 - MP4 to AVC File Converter |
| 4 | | |
| 5 | | Copyright 2002-2009 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 | |
| 37 | /*---------------------------------------------------------------------- |
| 38 | | constants |
| 39 | +---------------------------------------------------------------------*/ |
| 40 | #define BANNER "MP4 To AVC File Converter - Version 1.0\n"\ |
| 41 | "(Bento4 Version " AP4_VERSION_STRING ")\n"\ |
| 42 | "(c) 2002-2009 Axiomatic Systems, LLC" |
| 43 | |
| 44 | /*---------------------------------------------------------------------- |
| 45 | | PrintUsageAndExit |
| 46 | +---------------------------------------------------------------------*/ |
| 47 | static void |
| 48 | PrintUsageAndExit() |
| 49 | { |
| 50 | fprintf(stderr, |
| 51 | BANNER |
| 52 | "\n\nusage: mp42avc [options] <input> <output>\n" |
| 53 | " Options:\n" |
| 54 | " --key <hex>: 128-bit decryption key (in hex: 32 chars)\n"); |
| 55 | exit(1); |
| 56 | } |
| 57 | |
| 58 | /*---------------------------------------------------------------------- |
| 59 | | WriteSample |
| 60 | +---------------------------------------------------------------------*/ |
| 61 | static void |
| 62 | WriteSample(const AP4_DataBuffer& sample_data, |
| 63 | AP4_DataBuffer& prefix, |
| 64 | unsigned int nalu_length_size, |
| 65 | AP4_ByteStream* output) |
| 66 | { |
| 67 | const unsigned char* data = sample_data.GetData(); |
| 68 | unsigned int data_size = sample_data.GetDataSize(); |
| 69 | |
| 70 | // allocate a buffer for the PES packet |
| 71 | AP4_DataBuffer frame_data; |
| 72 | unsigned char* frame_buffer = NULL; |
| 73 | |
| 74 | // add a delimiter if we don't already have one |
| 75 | bool have_access_unit_delimiter = (data_size > nalu_length_size) && ((data[nalu_length_size] & 0x1F) == AP4_AVC_NAL_UNIT_TYPE_ACCESS_UNIT_DELIMITER); |
| 76 | if (!have_access_unit_delimiter) { |
| 77 | AP4_Size frame_data_size = frame_data.GetDataSize(); |
| 78 | frame_data.SetDataSize(frame_data_size+6); |
| 79 | frame_buffer = frame_data.UseData()+frame_data_size; |
| 80 | |
| 81 | // start of access unit |
| 82 | frame_buffer[0] = 0; |
| 83 | frame_buffer[1] = 0; |
| 84 | frame_buffer[2] = 0; |
| 85 | frame_buffer[3] = 1; |
| 86 | frame_buffer[4] = 9; // NAL type = Access Unit Delimiter; |
| 87 | frame_buffer[5] = 0xE0; // Slice types = ANY |
| 88 | } |
| 89 | |
| 90 | // write the NAL units |
| 91 | bool prefix_added = false; |
| 92 | while (data_size) { |
| 93 | // sanity check |
| 94 | if (data_size < nalu_length_size) break; |
| 95 | |
| 96 | // get the next NAL unit |
| 97 | AP4_UI32 nalu_size; |
| 98 | if (nalu_length_size == 1) { |
| 99 | nalu_size = *data++; |
| 100 | data_size--; |
| 101 | } else if (nalu_length_size == 2) { |
| 102 | nalu_size = AP4_BytesToInt16BE(data); |
| 103 | data += 2; |
| 104 | data_size -= 2; |
| 105 | } else if (nalu_length_size == 4) { |
| 106 | nalu_size = AP4_BytesToInt32BE(data); |
| 107 | data += 4; |
| 108 | data_size -= 4; |
| 109 | } else { |
| 110 | break; |
| 111 | } |
| 112 | if (nalu_size > data_size) break; |
| 113 | |
| 114 | // add the prefix if needed |
| 115 | if (prefix.GetDataSize() && !prefix_added && !have_access_unit_delimiter) { |
| 116 | AP4_Size frame_data_size = frame_data.GetDataSize(); |
| 117 | frame_data.SetDataSize(frame_data_size+prefix.GetDataSize()); |
| 118 | frame_buffer = frame_data.UseData()+frame_data_size; |
| 119 | AP4_CopyMemory(frame_buffer, prefix.GetData(), prefix.GetDataSize()); |
| 120 | prefix_added = true; |
| 121 | } |
| 122 | |
| 123 | // add a start code before the NAL unit |
| 124 | AP4_Size frame_data_size = frame_data.GetDataSize(); |
| 125 | frame_data.SetDataSize(frame_data_size+3+nalu_size); |
| 126 | frame_buffer = frame_data.UseData()+frame_data_size; |
| 127 | frame_buffer[0] = 0; |
| 128 | frame_buffer[1] = 0; |
| 129 | frame_buffer[2] = 1; |
| 130 | AP4_CopyMemory(frame_buffer+3, data, nalu_size); |
| 131 | |
| 132 | // add the prefix if needed |
| 133 | if (prefix.GetDataSize() && !prefix_added) { |
| 134 | AP4_Size frame_data_size = frame_data.GetDataSize(); |
| 135 | frame_data.SetDataSize(frame_data_size+prefix.GetDataSize()); |
| 136 | frame_buffer = frame_data.UseData()+frame_data_size; |
| 137 | AP4_CopyMemory(frame_buffer, prefix.GetData(), prefix.GetDataSize()); |
| 138 | prefix_added = true; |
| 139 | } |
| 140 | |
| 141 | // move to the next NAL unit |
| 142 | data += nalu_size; |
| 143 | data_size -= nalu_size; |
| 144 | } |
| 145 | |
| 146 | output->Write(frame_data.GetData(), frame_data.GetDataSize()); |
| 147 | } |
| 148 | |
| 149 | /*---------------------------------------------------------------------- |
| 150 | | MakeFramePrefix |
| 151 | +---------------------------------------------------------------------*/ |
| 152 | static AP4_Result |
| 153 | MakeFramePrefix(AP4_SampleDescription* sdesc, AP4_DataBuffer& prefix, unsigned int& nalu_length_size) |
| 154 | { |
| 155 | AP4_AvcSampleDescription* avc_desc = AP4_DYNAMIC_CAST(AP4_AvcSampleDescription, sdesc); |
| 156 | if (avc_desc == NULL) { |
| 157 | fprintf(stderr, "ERROR: track does not contain an AVC stream\n"); |
| 158 | return AP4_FAILURE; |
| 159 | } |
| 160 | |
| 161 | if (sdesc->GetFormat() == AP4_SAMPLE_FORMAT_AVC3 || |
| 162 | sdesc->GetFormat() == AP4_SAMPLE_FORMAT_AVC4 || |
| 163 | sdesc->GetFormat() == AP4_SAMPLE_FORMAT_DVAV) { |
| 164 | // no need for a prefix, SPS/PPS NALs should be in the elementary stream already |
| 165 | return AP4_SUCCESS; |
| 166 | } |
| 167 | |
| 168 | // make the SPS/PPS prefix |
| 169 | nalu_length_size = avc_desc->GetNaluLengthSize(); |
| 170 | for (unsigned int i=0; i<avc_desc->GetSequenceParameters().ItemCount(); i++) { |
| 171 | AP4_DataBuffer& buffer = avc_desc->GetSequenceParameters()[i]; |
| 172 | unsigned int prefix_size = prefix.GetDataSize(); |
| 173 | prefix.SetDataSize(prefix_size+4+buffer.GetDataSize()); |
| 174 | unsigned char* p = prefix.UseData()+prefix_size; |
| 175 | *p++ = 0; |
| 176 | *p++ = 0; |
| 177 | *p++ = 0; |
| 178 | *p++ = 1; |
| 179 | AP4_CopyMemory(p, buffer.GetData(), buffer.GetDataSize()); |
| 180 | } |
| 181 | for (unsigned int i=0; i<avc_desc->GetPictureParameters().ItemCount(); i++) { |
| 182 | AP4_DataBuffer& buffer = avc_desc->GetPictureParameters()[i]; |
| 183 | unsigned int prefix_size = prefix.GetDataSize(); |
| 184 | prefix.SetDataSize(prefix_size+4+buffer.GetDataSize()); |
| 185 | unsigned char* p = prefix.UseData()+prefix_size; |
| 186 | *p++ = 0; |
| 187 | *p++ = 0; |
| 188 | *p++ = 0; |
| 189 | *p++ = 1; |
| 190 | AP4_CopyMemory(p, buffer.GetData(), buffer.GetDataSize()); |
| 191 | } |
| 192 | |
| 193 | return AP4_SUCCESS; |
| 194 | } |
| 195 | |
| 196 | /*---------------------------------------------------------------------- |
| 197 | | DecryptAndWriteSamples |
| 198 | +---------------------------------------------------------------------*/ |
| 199 | static void |
| 200 | DecryptAndWriteSamples(AP4_Track* track, |
| 201 | AP4_SampleDescription* sdesc, |
| 202 | AP4_Byte* key, |
| 203 | AP4_ByteStream* output) |
| 204 | { |
| 205 | AP4_ProtectedSampleDescription* pdesc = AP4_DYNAMIC_CAST(AP4_ProtectedSampleDescription, sdesc); |
| 206 | if (pdesc == NULL) { |
| 207 | fprintf(stderr, "ERROR: unable to obtain cipher info\n"); |
| 208 | return; |
| 209 | } |
| 210 | |
| 211 | // get the original sample description and make the prefix |
| 212 | AP4_SampleDescription* orig_sdesc = pdesc->GetOriginalSampleDescription(); |
| 213 | unsigned int nalu_length_size = 0; |
| 214 | AP4_DataBuffer prefix; |
| 215 | if (AP4_FAILED(MakeFramePrefix(orig_sdesc, prefix, nalu_length_size))) { |
| 216 | return; |
| 217 | } |
| 218 | |
| 219 | // create the decrypter |
| 220 | AP4_SampleDecrypter* decrypter = AP4_SampleDecrypter::Create(pdesc, key, 16); |
| 221 | if (decrypter == NULL) { |
| 222 | fprintf(stderr, "ERROR: unable to create decrypter\n"); |
| 223 | return; |
| 224 | } |
| 225 | |
| 226 | AP4_Sample sample; |
| 227 | AP4_DataBuffer encrypted_data; |
| 228 | AP4_DataBuffer decrypted_data; |
| 229 | AP4_Ordinal index = 0; |
| 230 | while (AP4_SUCCEEDED(track->ReadSample(index, sample, encrypted_data))) { |
| 231 | if (AP4_FAILED(decrypter->DecryptSampleData(encrypted_data, decrypted_data))) { |
| 232 | fprintf(stderr, "ERROR: failed to decrypt sample\n"); |
| 233 | return; |
| 234 | } |
| 235 | WriteSample(decrypted_data, prefix, nalu_length_size, output); |
| 236 | index++; |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | /*---------------------------------------------------------------------- |
| 241 | | WriteSamples |
| 242 | +---------------------------------------------------------------------*/ |
| 243 | static void |
| 244 | WriteSamples(AP4_Track* track, |
| 245 | AP4_SampleDescription* sdesc, |
| 246 | AP4_ByteStream* output) |
| 247 | { |
| 248 | // make the frame prefix |
| 249 | unsigned int nalu_length_size = 0; |
| 250 | AP4_DataBuffer prefix; |
| 251 | if (AP4_FAILED(MakeFramePrefix(sdesc, prefix, nalu_length_size))) { |
| 252 | return; |
| 253 | } |
| 254 | |
| 255 | AP4_Sample sample; |
| 256 | AP4_DataBuffer data; |
| 257 | AP4_Ordinal index = 0; |
| 258 | while (AP4_SUCCEEDED(track->ReadSample(index, sample, data))) { |
| 259 | WriteSample(data, prefix, nalu_length_size, output); |
| 260 | index++; |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | /*---------------------------------------------------------------------- |
| 265 | | main |
| 266 | +---------------------------------------------------------------------*/ |
| 267 | int |
| 268 | main(int argc, char** argv) |
| 269 | { |
| 270 | if (argc < 3) { |
| 271 | PrintUsageAndExit(); |
| 272 | } |
| 273 | |
| 274 | // parse command line |
| 275 | AP4_Result result; |
| 276 | char** args = argv+1; |
| 277 | unsigned char key[16]; |
| 278 | bool key_option = false; |
| 279 | if (!strcmp(*args, "--key")) { |
| 280 | if (argc != 5) { |
| 281 | fprintf(stderr, "ERROR: invalid command line\n"); |
| 282 | return 1; |
| 283 | } |
| 284 | ++args; |
| 285 | if (AP4_ParseHex(*args++, key, 16)) { |
| 286 | fprintf(stderr, "ERROR: invalid hex format for key\n"); |
| 287 | return 1; |
| 288 | } |
| 289 | key_option = true; |
| 290 | } |
| 291 | |
| 292 | // create the input stream |
| 293 | AP4_ByteStream* input = NULL; |
| 294 | result = AP4_FileByteStream::Create(*args++, AP4_FileByteStream::STREAM_MODE_READ, input); |
| 295 | if (AP4_FAILED(result)) { |
| 296 | fprintf(stderr, "ERROR: cannot open input (%d)\n", result); |
| 297 | } |
| 298 | |
| 299 | // create the output stream |
| 300 | AP4_ByteStream* output = NULL; |
| 301 | result = AP4_FileByteStream::Create(*args++, AP4_FileByteStream::STREAM_MODE_WRITE, output); |
| 302 | if (AP4_FAILED(result)) { |
| 303 | fprintf(stderr, "ERROR: cannot open output (%d)\n", result); |
| 304 | } |
| 305 | |
| 306 | // open the file |
| 307 | AP4_File* input_file = new AP4_File(*input); |
| 308 | |
| 309 | // get the movie |
| 310 | AP4_SampleDescription* sample_description; |
| 311 | AP4_Track* video_track; |
| 312 | AP4_Movie* movie = input_file->GetMovie(); |
| 313 | if (movie == NULL) { |
| 314 | fprintf(stderr, "ERROR: no movie in file\n"); |
| 315 | goto end; |
| 316 | } |
| 317 | |
| 318 | // get the video track |
| 319 | video_track = movie->GetTrack(AP4_Track::TYPE_VIDEO); |
| 320 | if (video_track == NULL) { |
| 321 | fprintf(stderr, "ERROR: no video track found\n"); |
| 322 | goto end; |
| 323 | } |
| 324 | |
| 325 | // check that the track is of the right type |
| 326 | sample_description = video_track->GetSampleDescription(0); |
| 327 | if (sample_description == NULL) { |
| 328 | fprintf(stderr, "ERROR: unable to parse sample description\n"); |
| 329 | goto end; |
| 330 | } |
| 331 | |
| 332 | // show info |
| 333 | AP4_Debug("Video Track:\n"); |
| 334 | AP4_Debug(" duration: %u ms\n", (int)video_track->GetDurationMs()); |
| 335 | AP4_Debug(" sample count: %u\n", (int)video_track->GetSampleCount()); |
| 336 | |
| 337 | switch (sample_description->GetType()) { |
| 338 | case AP4_SampleDescription::TYPE_AVC: |
| 339 | WriteSamples(video_track, sample_description, output); |
| 340 | break; |
| 341 | |
| 342 | case AP4_SampleDescription::TYPE_PROTECTED: |
| 343 | if (!key_option) { |
| 344 | fprintf(stderr, "ERROR: encrypted tracks require a key\n"); |
| 345 | goto end; |
| 346 | } |
| 347 | DecryptAndWriteSamples(video_track, sample_description, key, output); |
| 348 | break; |
| 349 | |
| 350 | default: |
| 351 | fprintf(stderr, "ERROR: unsupported sample type\n"); |
| 352 | break; |
| 353 | } |
| 354 | |
| 355 | end: |
| 356 | delete input_file; |
| 357 | input->Release(); |
| 358 | output->Release(); |
| 359 | |
| 360 | return 0; |
| 361 | } |
| 362 |