VirtualBox

source: vbox/trunk/src/libs/ffmpeg-20060710/libavformat/movenc.c@ 8998

最後變更 在這個檔案從8998是 5776,由 vboxsync 提交於 17 年 前

ffmpeg: exported to OSE

檔案大小: 51.5 KB
 
1/*
2 * MOV, 3GP, MP4 encoder.
3 * Copyright (c) 2003 Thomas Raivio.
4 * Copyright (c) 2004 Gildas Bazin <gbazin at videolan dot org>.
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20#include "avformat.h"
21#include "avi.h"
22#include "avio.h"
23#include "mov.h"
24
25#undef NDEBUG
26#include <assert.h>
27
28#define MOV_INDEX_CLUSTER_SIZE 16384
29#define globalTimescale 1000
30
31#define MODE_MP4 0
32#define MODE_MOV 1
33#define MODE_3GP 2
34#define MODE_PSP 3 // example working PSP command line:
35// ffmpeg -i testinput.avi -f psp -r 14.985 -s 320x240 -b 768 -ar 24000 -ab 32 M4V00001.MP4
36#define MODE_3G2 4
37
38typedef struct MOVIentry {
39 unsigned int flags, size;
40 uint64_t pos;
41 unsigned int samplesInChunk;
42 char key_frame;
43 unsigned int entries;
44 int64_t cts;
45} MOVIentry;
46
47typedef struct MOVIndex {
48 int mode;
49 int entry;
50 long timescale;
51 long time;
52 int64_t trackDuration;
53 long sampleCount;
54 long sampleDuration;
55 long sampleSize;
56 int hasKeyframes;
57 int hasBframes;
58 int language;
59 int trackID;
60 int tag;
61 AVCodecContext *enc;
62
63 int vosLen;
64 uint8_t *vosData;
65 MOVIentry *cluster;
66} MOVTrack;
67
68typedef struct MOVContext {
69 int mode;
70 int64_t time;
71 int nb_streams;
72 offset_t mdat_pos;
73 uint64_t mdat_size;
74 long timescale;
75 MOVTrack tracks[MAX_STREAMS];
76} MOVContext;
77
78//FIXME supprt 64bit varaint with wide placeholders
79static offset_t updateSize (ByteIOContext *pb, offset_t pos)
80{
81 offset_t curpos = url_ftell(pb);
82 url_fseek(pb, pos, SEEK_SET);
83 put_be32(pb, curpos - pos); /* rewrite size */
84 url_fseek(pb, curpos, SEEK_SET);
85
86 return curpos - pos;
87}
88
89/* Chunk offset atom */
90static int mov_write_stco_tag(ByteIOContext *pb, MOVTrack* track)
91{
92 int i;
93 int mode64 = 0; // use 32 bit size variant if possible
94 offset_t pos = url_ftell(pb);
95 put_be32(pb, 0); /* size */
96 if (pos > UINT32_MAX) {
97 mode64 = 1;
98 put_tag(pb, "co64");
99 } else
100 put_tag(pb, "stco");
101 put_be32(pb, 0); /* version & flags */
102 put_be32(pb, track->entry); /* entry count */
103 for (i=0; i<track->entry; i++) {
104 if(mode64 == 1)
105 put_be64(pb, track->cluster[i].pos);
106 else
107 put_be32(pb, track->cluster[i].pos);
108 }
109 return updateSize (pb, pos);
110}
111
112/* Sample size atom */
113static int mov_write_stsz_tag(ByteIOContext *pb, MOVTrack* track)
114{
115 int equalChunks = 1;
116 int i, j, entries = 0, tst = -1, oldtst = -1;
117
118 offset_t pos = url_ftell(pb);
119 put_be32(pb, 0); /* size */
120 put_tag(pb, "stsz");
121 put_be32(pb, 0); /* version & flags */
122
123 for (i=0; i<track->entry; i++) {
124 tst = track->cluster[i].size/track->cluster[i].entries;
125 if(oldtst != -1 && tst != oldtst) {
126 equalChunks = 0;
127 }
128 oldtst = tst;
129 entries += track->cluster[i].entries;
130 }
131 if (equalChunks) {
132 int sSize = track->cluster[0].size/track->cluster[0].entries;
133 put_be32(pb, sSize); // sample size
134 put_be32(pb, entries); // sample count
135 }
136 else {
137 put_be32(pb, 0); // sample size
138 put_be32(pb, entries); // sample count
139 for (i=0; i<track->entry; i++) {
140 for ( j=0; j<track->cluster[i].entries; j++) {
141 put_be32(pb, track->cluster[i].size /
142 track->cluster[i].entries);
143 }
144 }
145 }
146 return updateSize (pb, pos);
147}
148
149/* Sample to chunk atom */
150static int mov_write_stsc_tag(ByteIOContext *pb, MOVTrack* track)
151{
152 int index = 0, oldval = -1, i;
153 offset_t entryPos, curpos;
154
155 offset_t pos = url_ftell(pb);
156 put_be32(pb, 0); /* size */
157 put_tag(pb, "stsc");
158 put_be32(pb, 0); // version & flags
159 entryPos = url_ftell(pb);
160 put_be32(pb, track->entry); // entry count
161 for (i=0; i<track->entry; i++) {
162 if(oldval != track->cluster[i].samplesInChunk)
163 {
164 put_be32(pb, i+1); // first chunk
165 put_be32(pb, track->cluster[i].samplesInChunk); // samples per chunk
166 put_be32(pb, 0x1); // sample description index
167 oldval = track->cluster[i].samplesInChunk;
168 index++;
169 }
170 }
171 curpos = url_ftell(pb);
172 url_fseek(pb, entryPos, SEEK_SET);
173 put_be32(pb, index); // rewrite size
174 url_fseek(pb, curpos, SEEK_SET);
175
176 return updateSize (pb, pos);
177}
178
179/* Sync sample atom */
180static int mov_write_stss_tag(ByteIOContext *pb, MOVTrack* track)
181{
182 offset_t curpos, entryPos;
183 int i, index = 0;
184 offset_t pos = url_ftell(pb);
185 put_be32(pb, 0); // size
186 put_tag(pb, "stss");
187 put_be32(pb, 0); // version & flags
188 entryPos = url_ftell(pb);
189 put_be32(pb, track->entry); // entry count
190 for (i=0; i<track->entry; i++) {
191 if(track->cluster[i].key_frame == 1) {
192 put_be32(pb, i+1);
193 index++;
194 }
195 }
196 curpos = url_ftell(pb);
197 url_fseek(pb, entryPos, SEEK_SET);
198 put_be32(pb, index); // rewrite size
199 url_fseek(pb, curpos, SEEK_SET);
200 return updateSize (pb, pos);
201}
202
203static int mov_write_amr_tag(ByteIOContext *pb, MOVTrack *track)
204{
205 put_be32(pb, 0x11); /* size */
206 if (track->mode == MODE_MOV) put_tag(pb, "samr");
207 else put_tag(pb, "damr");
208 put_tag(pb, "FFMP");
209 put_byte(pb, 0); /* decoder version */
210
211 put_be16(pb, 0x81FF); /* Mode set (all modes for AMR_NB) */
212 put_byte(pb, 0x00); /* Mode change period (no restriction) */
213 put_byte(pb, 0x01); /* Frames per sample */
214 return 0x11;
215}
216
217static int mov_write_enda_tag(ByteIOContext *pb)
218{
219 put_be32(pb, 10);
220 put_tag(pb, "enda");
221 put_be16(pb, 1); /* little endian */
222 return 10;
223}
224
225static unsigned int descrLength(unsigned int len)
226{
227 int i;
228 for(i=1; len>>(7*i); i++);
229 return len + 1 + i;
230}
231
232static void putDescr(ByteIOContext *pb, int tag, unsigned int size)
233{
234 int i= descrLength(size) - size - 2;
235 put_byte(pb, tag);
236 for(; i>0; i--)
237 put_byte(pb, (size>>(7*i)) | 0x80);
238 put_byte(pb, size & 0x7F);
239}
240
241static int mov_write_esds_tag(ByteIOContext *pb, MOVTrack* track) // Basic
242{
243 offset_t pos = url_ftell(pb);
244 int decoderSpecificInfoLen = track->vosLen ? descrLength(track->vosLen):0;
245
246 put_be32(pb, 0); // size
247 put_tag(pb, "esds");
248 put_be32(pb, 0); // Version
249
250 // ES descriptor
251 putDescr(pb, 0x03, 3 + descrLength(13 + decoderSpecificInfoLen) +
252 descrLength(1));
253 put_be16(pb, track->trackID);
254 put_byte(pb, 0x00); // flags (= no flags)
255
256 // DecoderConfig descriptor
257 putDescr(pb, 0x04, 13 + decoderSpecificInfoLen);
258
259 // Object type indication
260 put_byte(pb, codec_get_tag(ff_mov_obj_type, track->enc->codec_id));
261
262 // the following fields is made of 6 bits to identify the streamtype (4 for video, 5 for audio)
263 // plus 1 bit to indicate upstream and 1 bit set to 1 (reserved)
264 if(track->enc->codec_type == CODEC_TYPE_AUDIO)
265 put_byte(pb, 0x15); // flags (= Audiostream)
266 else
267 put_byte(pb, 0x11); // flags (= Visualstream)
268
269 put_byte(pb, track->enc->rc_buffer_size>>(3+16)); // Buffersize DB (24 bits)
270 put_be16(pb, (track->enc->rc_buffer_size>>3)&0xFFFF); // Buffersize DB
271
272 put_be32(pb, FFMAX(track->enc->bit_rate, track->enc->rc_max_rate)); // maxbitrate (FIXME should be max rate in any 1 sec window)
273 if(track->enc->rc_max_rate != track->enc->rc_min_rate || track->enc->rc_min_rate==0)
274 put_be32(pb, 0); // vbr
275 else
276 put_be32(pb, track->enc->rc_max_rate); // avg bitrate
277
278 if (track->vosLen)
279 {
280 // DecoderSpecific info descriptor
281 putDescr(pb, 0x05, track->vosLen);
282 put_buffer(pb, track->vosData, track->vosLen);
283 }
284
285
286 // SL descriptor
287 putDescr(pb, 0x06, 1);
288 put_byte(pb, 0x02);
289 return updateSize (pb, pos);
290}
291
292static int mov_write_wave_tag(ByteIOContext *pb, MOVTrack* track)
293{
294 offset_t pos = url_ftell(pb);
295
296 put_be32(pb, 0); /* size */
297 put_tag(pb, "wave");
298
299 put_be32(pb, 12); /* size */
300 put_tag(pb, "frma");
301 put_le32(pb, track->tag);
302
303 if (track->enc->codec_id == CODEC_ID_AAC) {
304 /* useless atom needed by mplayer, ipod, not needed by quicktime */
305 put_be32(pb, 12); /* size */
306 put_tag(pb, "mp4a");
307 put_be32(pb, 0);
308 mov_write_esds_tag(pb, track);
309 } else if (track->enc->codec_id == CODEC_ID_PCM_S24LE ||
310 track->enc->codec_id == CODEC_ID_PCM_S32LE) {
311 mov_write_enda_tag(pb);
312 } else if (track->enc->codec_id == CODEC_ID_AMR_NB) {
313 mov_write_amr_tag(pb, track);
314 }
315
316 put_be32(pb, 8); /* size */
317 put_be32(pb, 0); /* null tag */
318
319 return updateSize (pb, pos);
320}
321
322static const CodecTag codec_movaudio_tags[] = {
323 { CODEC_ID_PCM_MULAW, MKTAG('u', 'l', 'a', 'w') },
324 { CODEC_ID_PCM_ALAW, MKTAG('a', 'l', 'a', 'w') },
325 { CODEC_ID_ADPCM_IMA_QT, MKTAG('i', 'm', 'a', '4') },
326 { CODEC_ID_MACE3, MKTAG('M', 'A', 'C', '3') },
327 { CODEC_ID_MACE6, MKTAG('M', 'A', 'C', '6') },
328 { CODEC_ID_AAC, MKTAG('m', 'p', '4', 'a') },
329 { CODEC_ID_AMR_NB, MKTAG('s', 'a', 'm', 'r') },
330 { CODEC_ID_AMR_WB, MKTAG('s', 'a', 'w', 'b') },
331 { CODEC_ID_PCM_S16BE, MKTAG('t', 'w', 'o', 's') },
332 { CODEC_ID_PCM_S16LE, MKTAG('s', 'o', 'w', 't') },
333 { CODEC_ID_PCM_S24BE, MKTAG('i', 'n', '2', '4') },
334 { CODEC_ID_PCM_S24LE, MKTAG('i', 'n', '2', '4') },
335 { CODEC_ID_PCM_S32BE, MKTAG('i', 'n', '3', '2') },
336 { CODEC_ID_PCM_S32LE, MKTAG('i', 'n', '3', '2') },
337 { CODEC_ID_MP3, MKTAG('.', 'm', 'p', '3') },
338 { CODEC_ID_NONE, 0 },
339};
340
341static int mov_write_audio_tag(ByteIOContext *pb, MOVTrack* track)
342{
343 offset_t pos = url_ftell(pb);
344 int vbr= track->enc->codec_id == CODEC_ID_AAC ||
345 track->enc->codec_id == CODEC_ID_MP3 ||
346 track->enc->codec_id == CODEC_ID_AMR_NB;
347 int version = track->mode == MODE_MOV &&
348 (vbr ||
349 track->enc->codec_id == CODEC_ID_PCM_S32LE ||
350 track->enc->codec_id == CODEC_ID_PCM_S24LE);
351
352 put_be32(pb, 0); /* size */
353 put_le32(pb, track->tag); // store it byteswapped
354 put_be32(pb, 0); /* Reserved */
355 put_be16(pb, 0); /* Reserved */
356 put_be16(pb, 1); /* Data-reference index, XXX == 1 */
357
358 /* SoundDescription */
359 put_be16(pb, version); /* Version */
360 put_be16(pb, 0); /* Revision level */
361 put_be32(pb, 0); /* Reserved */
362
363 put_be16(pb, track->enc->channels); /* Number of channels */
364 /* TODO: Currently hard-coded to 16-bit, there doesn't seem
365 to be a good way to get number of bits of audio */
366 put_be16(pb, 0x10); /* Reserved */
367
368 if(vbr) {
369 put_be16(pb, 0xfffe); /* compression ID (vbr)*/
370 } else {
371 put_be16(pb, 0); /* compression ID (= 0) */
372 }
373 put_be16(pb, 0); /* packet size (= 0) */
374 put_be16(pb, track->timescale); /* Time scale */
375 put_be16(pb, 0); /* Reserved */
376
377 if(version == 1) { /* SoundDescription V1 extended info */
378 if (vbr)
379 track->sampleSize = 0;
380 put_be32(pb, track->enc->frame_size); /* Samples per packet */
381 put_be32(pb, track->sampleSize / track->enc->channels); /* Bytes per packet */
382 put_be32(pb, track->sampleSize); /* Bytes per frame */
383 put_be32(pb, 2); /* Bytes per sample */
384 }
385
386 if(track->mode == MODE_MOV &&
387 (track->enc->codec_id == CODEC_ID_AAC ||
388 track->enc->codec_id == CODEC_ID_AMR_NB ||
389 track->enc->codec_id == CODEC_ID_PCM_S24LE ||
390 track->enc->codec_id == CODEC_ID_PCM_S32LE))
391 mov_write_wave_tag(pb, track);
392 else if(track->enc->codec_id == CODEC_ID_AAC)
393 mov_write_esds_tag(pb, track);
394 else if(track->enc->codec_id == CODEC_ID_AMR_NB)
395 mov_write_amr_tag(pb, track);
396
397 return updateSize (pb, pos);
398}
399
400static int mov_write_d263_tag(ByteIOContext *pb)
401{
402 put_be32(pb, 0xf); /* size */
403 put_tag(pb, "d263");
404 put_tag(pb, "FFMP");
405 put_be16(pb, 0x0a);
406 put_byte(pb, 0);
407 return 0xf;
408}
409
410/* TODO: No idea about these values */
411static int mov_write_svq3_tag(ByteIOContext *pb)
412{
413 put_be32(pb, 0x15);
414 put_tag(pb, "SMI ");
415 put_tag(pb, "SEQH");
416 put_be32(pb, 0x5);
417 put_be32(pb, 0xe2c0211d);
418 put_be32(pb, 0xc0000000);
419 put_byte(pb, 0);
420 return 0x15;
421}
422
423static uint8_t *avc_find_startcode( uint8_t *p, uint8_t *end )
424{
425 uint8_t *a = p + 4 - ((int)p & 3);
426
427 for( end -= 3; p < a && p < end; p++ ) {
428 if( p[0] == 0 && p[1] == 0 && p[2] == 1 )
429 return p;
430 }
431
432 for( end -= 3; p < end; p += 4 ) {
433 uint32_t x = *(uint32_t*)p;
434// if( (x - 0x01000100) & (~x) & 0x80008000 ) // little endian
435// if( (x - 0x00010001) & (~x) & 0x00800080 ) // big endian
436 if( (x - 0x01010101) & (~x) & 0x80808080 ) { // generic
437 if( p[1] == 0 ) {
438 if( p[0] == 0 && p[2] == 1 )
439 return p-1;
440 if( p[2] == 0 && p[3] == 1 )
441 return p;
442 }
443 if( p[3] == 0 ) {
444 if( p[2] == 0 && p[4] == 1 )
445 return p+1;
446 if( p[4] == 0 && p[5] == 1 )
447 return p+2;
448 }
449 }
450 }
451
452 for( end += 3; p < end; p++ ) {
453 if( p[0] == 0 && p[1] == 0 && p[2] == 1 )
454 return p;
455 }
456
457 return end + 3;
458}
459
460static void avc_parse_nal_units(uint8_t **buf, int *size)
461{
462 ByteIOContext pb;
463 uint8_t *p = *buf;
464 uint8_t *end = p + *size;
465 uint8_t *nal_start, *nal_end;
466
467 url_open_dyn_buf(&pb);
468 nal_start = avc_find_startcode(p, end);
469 while (nal_start < end) {
470 while(!*(nal_start++));
471 nal_end = avc_find_startcode(nal_start, end);
472 put_be32(&pb, nal_end - nal_start);
473 put_buffer(&pb, nal_start, nal_end - nal_start);
474 nal_start = nal_end;
475 }
476 av_freep(buf);
477 *size = url_close_dyn_buf(&pb, buf);
478}
479
480static int mov_write_avcc_tag(ByteIOContext *pb, MOVTrack *track)
481{
482 offset_t pos = url_ftell(pb);
483
484 put_be32(pb, 0);
485 put_tag(pb, "avcC");
486 if (track->vosLen > 6) {
487 /* check for h264 start code */
488 if (BE_32(track->vosData) == 0x00000001) {
489 uint8_t *buf, *end;
490 uint32_t sps_size=0, pps_size=0;
491 uint8_t *sps=0, *pps=0;
492
493 avc_parse_nal_units(&track->vosData, &track->vosLen);
494 buf = track->vosData;
495 end = track->vosData + track->vosLen;
496
497 /* look for sps and pps */
498 while (buf < end) {
499 unsigned int size;
500 uint8_t nal_type;
501 size = BE_32(buf);
502 nal_type = buf[4] & 0x1f;
503 if (nal_type == 7) { /* SPS */
504 sps = buf + 4;
505 sps_size = size;
506 } else if (nal_type == 8) { /* PPS */
507 pps = buf + 4;
508 pps_size = size;
509 }
510 buf += size + 4;
511 }
512 assert(sps);
513 assert(pps);
514
515 put_byte(pb, 1); /* version */
516 put_byte(pb, sps[1]); /* profile */
517 put_byte(pb, sps[2]); /* profile compat */
518 put_byte(pb, sps[3]); /* level */
519 put_byte(pb, 0xff); /* 6 bits reserved (111111) + 2 bits nal size length - 1 (11) */
520 put_byte(pb, 0xe1); /* 3 bits reserved (111) + 5 bits number of sps (00001) */
521
522 put_be16(pb, sps_size);
523 put_buffer(pb, sps, sps_size);
524 put_byte(pb, 1); /* number of pps */
525 put_be16(pb, pps_size);
526 put_buffer(pb, pps, pps_size);
527 } else {
528 put_buffer(pb, track->vosData, track->vosLen);
529 }
530 }
531 return updateSize(pb, pos);
532}
533
534static const CodecTag codec_movvideo_tags[] = {
535 { CODEC_ID_SVQ1, MKTAG('S', 'V', 'Q', '1') },
536 { CODEC_ID_SVQ3, MKTAG('S', 'V', 'Q', '3') },
537 { CODEC_ID_MPEG4, MKTAG('m', 'p', '4', 'v') },
538 { CODEC_ID_H263, MKTAG('s', '2', '6', '3') },
539 { CODEC_ID_H264, MKTAG('a', 'v', 'c', '1') },
540 /* special handling in mov_find_video_codec_tag */
541 { CODEC_ID_DVVIDEO, MKTAG('d', 'v', 'c', ' ') }, /* DV NTSC */
542 { CODEC_ID_DVVIDEO, MKTAG('d', 'v', 'c', 'p') }, /* DV PAL */
543 { CODEC_ID_DVVIDEO, MKTAG('d', 'v', 'p', 'p') }, /* DVCPRO PAL */
544 { CODEC_ID_DVVIDEO, MKTAG('d', 'v', '5', 'n') }, /* DVCPRO50 NTSC */
545 { CODEC_ID_DVVIDEO, MKTAG('d', 'v', '5', 'p') }, /* DVCPRO50 PAL */
546 { CODEC_ID_NONE, 0 },
547};
548
549static int mov_find_video_codec_tag(AVFormatContext *s, MOVTrack *track)
550{
551 int tag = track->enc->codec_tag;
552 if (!tag) {
553 if (track->enc->codec_id == CODEC_ID_DVVIDEO) {
554 if (track->enc->height == 480) { /* NTSC */
555 if (track->enc->pix_fmt == PIX_FMT_YUV422P)
556 tag = MKTAG('d', 'v', '5', 'n');
557 else
558 tag = MKTAG('d', 'v', 'c', ' ');
559 } else { /* assume PAL */
560 if (track->enc->pix_fmt == PIX_FMT_YUV422P)
561 tag = MKTAG('d', 'v', '5', 'p');
562 else if (track->enc->pix_fmt == PIX_FMT_YUV420P)
563 tag = MKTAG('d', 'v', 'c', 'p');
564 else
565 tag = MKTAG('d', 'v', 'p', 'p');
566 }
567 } else {
568 tag = codec_get_tag(codec_movvideo_tags, track->enc->codec_id);
569 }
570 }
571 // if no mac fcc found, try with Microsoft tags
572 if (!tag) {
573 tag = codec_get_tag(codec_bmp_tags, track->enc->codec_id);
574 if (tag) {
575 av_log(s, AV_LOG_INFO, "Warning, using MS style video codec tag, the file may be unplayable!\n");
576 }
577 }
578 assert(tag);
579 return tag;
580}
581
582static int mov_find_audio_codec_tag(AVFormatContext *s, MOVTrack *track)
583{
584 int tag = track->enc->codec_tag;
585 if (!tag) {
586 tag = codec_get_tag(codec_movaudio_tags, track->enc->codec_id);
587 }
588 // if no mac fcc found, try with Microsoft tags
589 if (!tag) {
590 int ms_tag = codec_get_tag(codec_wav_tags, track->enc->codec_id);
591 if (ms_tag) {
592 tag = MKTAG('m', 's', ((ms_tag >> 8) & 0xff), (ms_tag & 0xff));
593 av_log(s, AV_LOG_INFO, "Warning, using MS style audio codec tag, the file may be unplayable!\n");
594 }
595 }
596 assert(tag);
597 return tag;
598}
599
600static int mov_write_video_tag(ByteIOContext *pb, MOVTrack* track)
601{
602 offset_t pos = url_ftell(pb);
603 char compressor_name[32];
604
605 put_be32(pb, 0); /* size */
606 put_le32(pb, track->tag); // store it byteswapped
607 put_be32(pb, 0); /* Reserved */
608 put_be16(pb, 0); /* Reserved */
609 put_be16(pb, 1); /* Data-reference index */
610
611 put_be16(pb, 0); /* Codec stream version */
612 put_be16(pb, 0); /* Codec stream revision (=0) */
613 put_tag(pb, "FFMP"); /* Vendor */
614 if(track->enc->codec_id == CODEC_ID_RAWVIDEO) {
615 put_be32(pb, 0); /* Temporal Quality */
616 put_be32(pb, 0x400); /* Spatial Quality = lossless*/
617 } else {
618 put_be32(pb, 0x200); /* Temporal Quality = normal */
619 put_be32(pb, 0x200); /* Spatial Quality = normal */
620 }
621 put_be16(pb, track->enc->width); /* Video width */
622 put_be16(pb, track->enc->height); /* Video height */
623 put_be32(pb, 0x00480000); /* Horizontal resolution 72dpi */
624 put_be32(pb, 0x00480000); /* Vertical resolution 72dpi */
625 put_be32(pb, 0); /* Data size (= 0) */
626 put_be16(pb, 1); /* Frame count (= 1) */
627
628 memset(compressor_name,0,32);
629 if (track->enc->codec && track->enc->codec->name)
630 strncpy(compressor_name,track->enc->codec->name,31);
631 put_byte(pb, strlen(compressor_name));
632 put_buffer(pb, compressor_name, 31);
633
634 put_be16(pb, 0x18); /* Reserved */
635 put_be16(pb, 0xffff); /* Reserved */
636 if(track->enc->codec_id == CODEC_ID_MPEG4)
637 mov_write_esds_tag(pb, track);
638 else if(track->enc->codec_id == CODEC_ID_H263)
639 mov_write_d263_tag(pb);
640 else if(track->enc->codec_id == CODEC_ID_SVQ3)
641 mov_write_svq3_tag(pb);
642 else if(track->enc->codec_id == CODEC_ID_H264)
643 mov_write_avcc_tag(pb, track);
644
645 return updateSize (pb, pos);
646}
647
648static int mov_write_stsd_tag(ByteIOContext *pb, MOVTrack* track)
649{
650 offset_t pos = url_ftell(pb);
651 put_be32(pb, 0); /* size */
652 put_tag(pb, "stsd");
653 put_be32(pb, 0); /* version & flags */
654 put_be32(pb, 1); /* entry count */
655 if (track->enc->codec_type == CODEC_TYPE_VIDEO)
656 mov_write_video_tag(pb, track);
657 else if (track->enc->codec_type == CODEC_TYPE_AUDIO)
658 mov_write_audio_tag(pb, track);
659 return updateSize(pb, pos);
660}
661
662static int mov_write_ctts_tag(ByteIOContext *pb, MOVTrack* track)
663{
664 Time2Sample *ctts_entries;
665 uint32_t entries = 0;
666 uint32_t atom_size;
667 int i;
668
669 ctts_entries = av_malloc((track->entry + 1) * sizeof(*ctts_entries)); /* worst case */
670 ctts_entries[0].count = 1;
671 ctts_entries[0].duration = track->cluster[0].cts;
672 for (i=1; i<track->entry; i++) {
673 if (track->cluster[i].cts == ctts_entries[entries].duration) {
674 ctts_entries[entries].count++; /* compress */
675 } else {
676 entries++;
677 ctts_entries[entries].duration = track->cluster[i].cts;
678 ctts_entries[entries].count = 1;
679 }
680 }
681 entries++; /* last one */
682 atom_size = 16 + (entries * 8);
683 put_be32(pb, atom_size); /* size */
684 put_tag(pb, "ctts");
685 put_be32(pb, 0); /* version & flags */
686 put_be32(pb, entries); /* entry count */
687 for (i=0; i<entries; i++) {
688 put_be32(pb, ctts_entries[i].count);
689 put_be32(pb, ctts_entries[i].duration);
690 }
691 av_free(ctts_entries);
692 return atom_size;
693}
694
695/* TODO: */
696/* Time to sample atom */
697static int mov_write_stts_tag(ByteIOContext *pb, MOVTrack* track)
698{
699 put_be32(pb, 0x18); /* size */
700 put_tag(pb, "stts");
701 put_be32(pb, 0); /* version & flags */
702 put_be32(pb, 1); /* entry count */
703
704 put_be32(pb, track->sampleCount); /* sample count */
705 put_be32(pb, track->sampleDuration); /* sample duration */
706 return 0x18;
707}
708
709static int mov_write_dref_tag(ByteIOContext *pb)
710{
711 put_be32(pb, 28); /* size */
712 put_tag(pb, "dref");
713 put_be32(pb, 0); /* version & flags */
714 put_be32(pb, 1); /* entry count */
715
716 put_be32(pb, 0xc); /* size */
717 put_tag(pb, "url ");
718 put_be32(pb, 1); /* version & flags */
719
720 return 28;
721}
722
723static int mov_write_stbl_tag(ByteIOContext *pb, MOVTrack* track)
724{
725 offset_t pos = url_ftell(pb);
726 put_be32(pb, 0); /* size */
727 put_tag(pb, "stbl");
728 mov_write_stsd_tag(pb, track);
729 mov_write_stts_tag(pb, track);
730 if (track->enc->codec_type == CODEC_TYPE_VIDEO &&
731 track->hasKeyframes < track->entry)
732 mov_write_stss_tag(pb, track);
733 if (track->enc->codec_type == CODEC_TYPE_VIDEO &&
734 track->hasBframes)
735 mov_write_ctts_tag(pb, track);
736 mov_write_stsc_tag(pb, track);
737 mov_write_stsz_tag(pb, track);
738 mov_write_stco_tag(pb, track);
739 return updateSize(pb, pos);
740}
741
742static int mov_write_dinf_tag(ByteIOContext *pb)
743{
744 offset_t pos = url_ftell(pb);
745 put_be32(pb, 0); /* size */
746 put_tag(pb, "dinf");
747 mov_write_dref_tag(pb);
748 return updateSize(pb, pos);
749}
750
751static int mov_write_smhd_tag(ByteIOContext *pb)
752{
753 put_be32(pb, 16); /* size */
754 put_tag(pb, "smhd");
755 put_be32(pb, 0); /* version & flags */
756 put_be16(pb, 0); /* reserved (balance, normally = 0) */
757 put_be16(pb, 0); /* reserved */
758 return 16;
759}
760
761static int mov_write_vmhd_tag(ByteIOContext *pb)
762{
763 put_be32(pb, 0x14); /* size (always 0x14) */
764 put_tag(pb, "vmhd");
765 put_be32(pb, 0x01); /* version & flags */
766 put_be64(pb, 0); /* reserved (graphics mode = copy) */
767 return 0x14;
768}
769
770static int mov_write_hdlr_tag(ByteIOContext *pb, MOVTrack* track)
771{
772 const char *descr, *hdlr, *hdlr_type;
773 offset_t pos = url_ftell(pb);
774
775 if (!track) { /* no media --> data handler */
776 hdlr = "dhlr";
777 hdlr_type = "url ";
778 descr = "DataHandler";
779 } else {
780 hdlr = (track->mode == MODE_MOV) ? "mhlr" : "\0\0\0\0";
781 if (track->enc->codec_type == CODEC_TYPE_VIDEO) {
782 hdlr_type = "vide";
783 descr = "VideoHandler";
784 } else {
785 hdlr_type = "soun";
786 descr = "SoundHandler";
787 }
788 }
789
790 put_be32(pb, 0); /* size */
791 put_tag(pb, "hdlr");
792 put_be32(pb, 0); /* Version & flags */
793 put_buffer(pb, hdlr, 4); /* handler */
794 put_tag(pb, hdlr_type); /* handler type */
795 put_be32(pb ,0); /* reserved */
796 put_be32(pb ,0); /* reserved */
797 put_be32(pb ,0); /* reserved */
798 put_byte(pb, strlen(descr)); /* string counter */
799 put_buffer(pb, descr, strlen(descr)); /* handler description */
800 return updateSize(pb, pos);
801}
802
803static int mov_write_minf_tag(ByteIOContext *pb, MOVTrack* track)
804{
805 offset_t pos = url_ftell(pb);
806 put_be32(pb, 0); /* size */
807 put_tag(pb, "minf");
808 if(track->enc->codec_type == CODEC_TYPE_VIDEO)
809 mov_write_vmhd_tag(pb);
810 else
811 mov_write_smhd_tag(pb);
812 if (track->mode == MODE_MOV) /* FIXME: Why do it for MODE_MOV only ? */
813 mov_write_hdlr_tag(pb, NULL);
814 mov_write_dinf_tag(pb);
815 mov_write_stbl_tag(pb, track);
816 return updateSize(pb, pos);
817}
818
819static int mov_write_mdhd_tag(ByteIOContext *pb, MOVTrack* track)
820{
821 int version = track->trackDuration < INT32_MAX ? 0 : 1;
822
823 (version == 1) ? put_be32(pb, 44) : put_be32(pb, 32); /* size */
824 put_tag(pb, "mdhd");
825 put_byte(pb, version);
826 put_be24(pb, 0); /* flags */
827 if (version == 1) {
828 put_be64(pb, track->time);
829 put_be64(pb, track->time);
830 } else {
831 put_be32(pb, track->time); /* creation time */
832 put_be32(pb, track->time); /* modification time */
833 }
834 put_be32(pb, track->timescale); /* time scale (sample rate for audio) */
835 (version == 1) ? put_be64(pb, track->trackDuration) : put_be32(pb, track->trackDuration); /* duration */
836 put_be16(pb, track->language); /* language */
837 put_be16(pb, 0); /* reserved (quality) */
838 return 32;
839}
840
841static int mov_write_mdia_tag(ByteIOContext *pb, MOVTrack* track)
842{
843 offset_t pos = url_ftell(pb);
844 put_be32(pb, 0); /* size */
845 put_tag(pb, "mdia");
846 mov_write_mdhd_tag(pb, track);
847 mov_write_hdlr_tag(pb, track);
848 mov_write_minf_tag(pb, track);
849 return updateSize(pb, pos);
850}
851
852static int mov_write_tkhd_tag(ByteIOContext *pb, MOVTrack* track)
853{
854 int64_t duration = av_rescale_rnd(track->trackDuration, globalTimescale, track->timescale, AV_ROUND_UP);
855 int version = duration < INT32_MAX ? 0 : 1;
856
857 (version == 1) ? put_be32(pb, 104) : put_be32(pb, 92); /* size */
858 put_tag(pb, "tkhd");
859 put_byte(pb, version);
860 put_be24(pb, 0xf); /* flags (track enabled) */
861 if (version == 1) {
862 put_be64(pb, track->time);
863 put_be64(pb, track->time);
864 } else {
865 put_be32(pb, track->time); /* creation time */
866 put_be32(pb, track->time); /* modification time */
867 }
868 put_be32(pb, track->trackID); /* track-id */
869 put_be32(pb, 0); /* reserved */
870 (version == 1) ? put_be64(pb, duration) : put_be32(pb, duration);
871
872 put_be32(pb, 0); /* reserved */
873 put_be32(pb, 0); /* reserved */
874 put_be32(pb, 0x0); /* reserved (Layer & Alternate group) */
875 /* Volume, only for audio */
876 if(track->enc->codec_type == CODEC_TYPE_AUDIO)
877 put_be16(pb, 0x0100);
878 else
879 put_be16(pb, 0);
880 put_be16(pb, 0); /* reserved */
881
882 /* Matrix structure */
883 put_be32(pb, 0x00010000); /* reserved */
884 put_be32(pb, 0x0); /* reserved */
885 put_be32(pb, 0x0); /* reserved */
886 put_be32(pb, 0x0); /* reserved */
887 put_be32(pb, 0x00010000); /* reserved */
888 put_be32(pb, 0x0); /* reserved */
889 put_be32(pb, 0x0); /* reserved */
890 put_be32(pb, 0x0); /* reserved */
891 put_be32(pb, 0x40000000); /* reserved */
892
893 /* Track width and height, for visual only */
894 if(track->enc->codec_type == CODEC_TYPE_VIDEO) {
895 double sample_aspect_ratio = av_q2d(track->enc->sample_aspect_ratio);
896 if( !sample_aspect_ratio ) sample_aspect_ratio = 1;
897 put_be32(pb, sample_aspect_ratio * track->enc->width*0x10000);
898 put_be32(pb, track->enc->height*0x10000);
899 }
900 else {
901 put_be32(pb, 0);
902 put_be32(pb, 0);
903 }
904 return 0x5c;
905}
906
907// This box seems important for the psp playback ... without it the movie seems to hang
908static int mov_write_edts_tag(ByteIOContext *pb, MOVTrack *track)
909{
910 put_be32(pb, 0x24); /* size */
911 put_tag(pb, "edts");
912 put_be32(pb, 0x1c); /* size */
913 put_tag(pb, "elst");
914 put_be32(pb, 0x0);
915 put_be32(pb, 0x1);
916
917 put_be32(pb, av_rescale_rnd(track->trackDuration, globalTimescale, track->timescale, AV_ROUND_UP)); /* duration ... doesn't seem to effect psp */
918
919 if (track->hasBframes)
920 put_be32(pb, track->sampleDuration); /* first pts is 1 */
921 else
922 put_be32(pb, 0);
923 put_be32(pb, 0x00010000);
924 return 0x24;
925}
926
927// goes at the end of each track! ... Critical for PSP playback ("Incompatible data" without it)
928static int mov_write_uuid_tag_psp(ByteIOContext *pb, MOVTrack *mov)
929{
930 put_be32(pb, 0x34); /* size ... reports as 28 in mp4box! */
931 put_tag(pb, "uuid");
932 put_tag(pb, "USMT");
933 put_be32(pb, 0x21d24fce);
934 put_be32(pb, 0xbb88695c);
935 put_be32(pb, 0xfac9c740);
936 put_be32(pb, 0x1c); // another size here!
937 put_tag(pb, "MTDT");
938 put_be32(pb, 0x00010012);
939 put_be32(pb, 0x0a);
940 put_be32(pb, 0x55c40000);
941 put_be32(pb, 0x1);
942 put_be32(pb, 0x0);
943 return 0x34;
944}
945
946static int mov_write_trak_tag(ByteIOContext *pb, MOVTrack* track)
947{
948 offset_t pos = url_ftell(pb);
949 put_be32(pb, 0); /* size */
950 put_tag(pb, "trak");
951 mov_write_tkhd_tag(pb, track);
952 if (track->mode == MODE_PSP || track->hasBframes)
953 mov_write_edts_tag(pb, track); // PSP Movies require edts box
954 mov_write_mdia_tag(pb, track);
955 if (track->mode == MODE_PSP)
956 mov_write_uuid_tag_psp(pb,track); // PSP Movies require this uuid box
957 return updateSize(pb, pos);
958}
959
960#if 0
961/* TODO: Not sorted out, but not necessary either */
962static int mov_write_iods_tag(ByteIOContext *pb, MOVContext *mov)
963{
964 put_be32(pb, 0x15); /* size */
965 put_tag(pb, "iods");
966 put_be32(pb, 0); /* version & flags */
967 put_be16(pb, 0x1007);
968 put_byte(pb, 0);
969 put_be16(pb, 0x4fff);
970 put_be16(pb, 0xfffe);
971 put_be16(pb, 0x01ff);
972 return 0x15;
973}
974#endif
975
976static int mov_write_mvhd_tag(ByteIOContext *pb, MOVContext *mov)
977{
978 int maxTrackID = 1, i;
979 int64_t maxTrackLenTemp, maxTrackLen = 0;
980 int version;
981
982 for (i=0; i<mov->nb_streams; i++) {
983 if(mov->tracks[i].entry > 0) {
984 maxTrackLenTemp = av_rescale_rnd(mov->tracks[i].trackDuration, globalTimescale, mov->tracks[i].timescale, AV_ROUND_UP);
985 if(maxTrackLen < maxTrackLenTemp)
986 maxTrackLen = maxTrackLenTemp;
987 if(maxTrackID < mov->tracks[i].trackID)
988 maxTrackID = mov->tracks[i].trackID;
989 }
990 }
991
992 version = maxTrackLen < UINT32_MAX ? 0 : 1;
993 (version == 1) ? put_be32(pb, 120) : put_be32(pb, 108); /* size */
994 put_tag(pb, "mvhd");
995 put_byte(pb, version);
996 put_be24(pb, 0); /* flags */
997 if (version == 1) {
998 put_be64(pb, mov->time);
999 put_be64(pb, mov->time);
1000 } else {
1001 put_be32(pb, mov->time); /* creation time */
1002 put_be32(pb, mov->time); /* modification time */
1003 }
1004 put_be32(pb, mov->timescale); /* timescale */
1005 (version == 1) ? put_be64(pb, maxTrackLen) : put_be32(pb, maxTrackLen); /* duration of longest track */
1006
1007 put_be32(pb, 0x00010000); /* reserved (preferred rate) 1.0 = normal */
1008 put_be16(pb, 0x0100); /* reserved (preferred volume) 1.0 = normal */
1009 put_be16(pb, 0); /* reserved */
1010 put_be32(pb, 0); /* reserved */
1011 put_be32(pb, 0); /* reserved */
1012
1013 /* Matrix structure */
1014 put_be32(pb, 0x00010000); /* reserved */
1015 put_be32(pb, 0x0); /* reserved */
1016 put_be32(pb, 0x0); /* reserved */
1017 put_be32(pb, 0x0); /* reserved */
1018 put_be32(pb, 0x00010000); /* reserved */
1019 put_be32(pb, 0x0); /* reserved */
1020 put_be32(pb, 0x0); /* reserved */
1021 put_be32(pb, 0x0); /* reserved */
1022 put_be32(pb, 0x40000000); /* reserved */
1023
1024 put_be32(pb, 0); /* reserved (preview time) */
1025 put_be32(pb, 0); /* reserved (preview duration) */
1026 put_be32(pb, 0); /* reserved (poster time) */
1027 put_be32(pb, 0); /* reserved (selection time) */
1028 put_be32(pb, 0); /* reserved (selection duration) */
1029 put_be32(pb, 0); /* reserved (current time) */
1030 put_be32(pb, maxTrackID+1); /* Next track id */
1031 return 0x6c;
1032}
1033
1034static int mov_write_itunes_hdlr_tag(ByteIOContext *pb, MOVContext* mov,
1035 AVFormatContext *s)
1036{
1037 offset_t pos = url_ftell(pb);
1038 put_be32(pb, 0); /* size */
1039 put_tag(pb, "hdlr");
1040 put_be32(pb, 0);
1041 put_be32(pb, 0);
1042 put_tag(pb, "mdir");
1043 put_tag(pb, "appl");
1044 put_be32(pb, 0);
1045 put_be32(pb, 0);
1046 put_be16(pb, 0);
1047 return updateSize(pb, pos);
1048}
1049
1050/* helper function to write a data tag with the specified string as data */
1051static int mov_write_string_data_tag(ByteIOContext *pb, const char *data, int long_style)
1052{
1053 if(long_style){
1054 offset_t pos = url_ftell(pb);
1055 put_be32(pb, 0); /* size */
1056 put_tag(pb, "data");
1057 put_be32(pb, 1);
1058 put_be32(pb, 0);
1059 put_buffer(pb, data, strlen(data));
1060 return updateSize(pb, pos);
1061 }else{
1062 put_be16(pb, strlen(data)); /* string length */
1063 put_be16(pb, 0);
1064 put_buffer(pb, data, strlen(data));
1065 return strlen(data) + 4;
1066 }
1067}
1068
1069static int mov_write_string_tag(ByteIOContext *pb, const char *name, const char *value, int long_style){
1070 int size = 0;
1071 if ( value && value[0] ) {
1072 offset_t pos = url_ftell(pb);
1073 put_be32(pb, 0); /* size */
1074 put_tag(pb, name);
1075 mov_write_string_data_tag(pb, value, long_style);
1076 size= updateSize(pb, pos);
1077 }
1078 return size;
1079}
1080
1081/* iTunes year */
1082static int mov_write_day_tag(ByteIOContext *pb, int year, int long_style)
1083{
1084 if(year){
1085 char year_str[5];
1086 snprintf(year_str, sizeof(year_str), "%04d", year);
1087 return mov_write_string_tag(pb, "\251day", year_str, long_style);
1088 }else
1089 return 0;
1090}
1091
1092/* iTunes track number */
1093static int mov_write_trkn_tag(ByteIOContext *pb, MOVContext* mov,
1094 AVFormatContext *s)
1095{
1096 int size = 0;
1097 if ( s->track ) {
1098 offset_t pos = url_ftell(pb);
1099 put_be32(pb, 0); /* size */
1100 put_tag(pb, "trkn");
1101 {
1102 offset_t pos = url_ftell(pb);
1103 put_be32(pb, 0); /* size */
1104 put_tag(pb, "data");
1105 put_be32(pb, 0); // 8 bytes empty
1106 put_be32(pb, 0);
1107 put_be16(pb, 0); // empty
1108 put_be16(pb, s->track); // track number
1109 put_be16(pb, 0); // total track number
1110 put_be16(pb, 0); // empty
1111 updateSize(pb, pos);
1112 }
1113 size = updateSize(pb, pos);
1114 }
1115 return size;
1116}
1117
1118/* iTunes meta data list */
1119static int mov_write_ilst_tag(ByteIOContext *pb, MOVContext* mov,
1120 AVFormatContext *s)
1121{
1122 offset_t pos = url_ftell(pb);
1123 put_be32(pb, 0); /* size */
1124 put_tag(pb, "ilst");
1125 mov_write_string_tag(pb, "\251nam", s->title , 1);
1126 mov_write_string_tag(pb, "\251ART", s->author , 1);
1127 mov_write_string_tag(pb, "\251wrt", s->author , 1);
1128 mov_write_string_tag(pb, "\251alb", s->album , 1);
1129 mov_write_day_tag(pb, s->year ,1);
1130 if(mov->tracks[0].enc && !(mov->tracks[0].enc->flags & CODEC_FLAG_BITEXACT))
1131 mov_write_string_tag(pb, "\251too", LIBAVFORMAT_IDENT, 1);
1132 mov_write_string_tag(pb, "\251cmt", s->comment , 1);
1133 mov_write_string_tag(pb, "\251gen", s->genre , 1);
1134 mov_write_trkn_tag(pb, mov, s);
1135 return updateSize(pb, pos);
1136}
1137
1138/* iTunes meta data tag */
1139static int mov_write_meta_tag(ByteIOContext *pb, MOVContext* mov,
1140 AVFormatContext *s)
1141{
1142 int size = 0;
1143
1144 // only save meta tag if required
1145 if ( s->title[0] || s->author[0] || s->album[0] || s->year ||
1146 s->comment[0] || s->genre[0] || s->track ) {
1147 offset_t pos = url_ftell(pb);
1148 put_be32(pb, 0); /* size */
1149 put_tag(pb, "meta");
1150 put_be32(pb, 0);
1151 mov_write_itunes_hdlr_tag(pb, mov, s);
1152 mov_write_ilst_tag(pb, mov, s);
1153 size = updateSize(pb, pos);
1154 }
1155 return size;
1156}
1157
1158static int mov_write_udta_tag(ByteIOContext *pb, MOVContext* mov,
1159 AVFormatContext *s)
1160{
1161 offset_t pos = url_ftell(pb);
1162 int i;
1163
1164 put_be32(pb, 0); /* size */
1165 put_tag(pb, "udta");
1166
1167 /* iTunes meta data */
1168 mov_write_meta_tag(pb, mov, s);
1169
1170 if(mov->mode == MODE_MOV){ // the title field breaks gtkpod with mp4 and my suspicion is that stuff isnt valid in mp4
1171 /* Requirements */
1172 for (i=0; i<mov->nb_streams; i++) {
1173 if(mov->tracks[i].entry <= 0) continue;
1174 if (mov->tracks[i].enc->codec_id == CODEC_ID_AAC ||
1175 mov->tracks[i].enc->codec_id == CODEC_ID_MPEG4) {
1176 mov_write_string_tag(pb, "\251req", "QuickTime 6.0 or greater", 0);
1177 break;
1178 }
1179 }
1180
1181 mov_write_string_tag(pb, "\251nam", s->title , 0);
1182 mov_write_string_tag(pb, "\251aut", s->author , 0);
1183 mov_write_string_tag(pb, "\251alb", s->album , 0);
1184 mov_write_day_tag(pb, s->year, 0);
1185 if(mov->tracks[0].enc && !(mov->tracks[0].enc->flags & CODEC_FLAG_BITEXACT))
1186 mov_write_string_tag(pb, "\251enc", LIBAVFORMAT_IDENT, 0);
1187 mov_write_string_tag(pb, "\251des", s->comment , 0);
1188 mov_write_string_tag(pb, "\251gen", s->genre , 0);
1189 }
1190
1191 return updateSize(pb, pos);
1192}
1193
1194
1195static size_t ascii_to_wc (ByteIOContext *pb, char *b, size_t n)
1196{
1197 size_t i;
1198 unsigned char c;
1199 for (i = 0; i < n - 1; i++) {
1200 c = b[i];
1201 if (! (0x20 <= c && c <= 0x7f ))
1202 c = 0x3f; /* '?' */
1203 put_be16(pb, c);
1204 }
1205 put_be16(pb, 0x00);
1206 return 2*n;
1207}
1208
1209static uint16_t language_code (const char *str)
1210{
1211 return ((((str[0]-0x60) & 0x1F)<<10) + (((str[1]-0x60) & 0x1F)<<5) + ((str[2]-0x60) & 0x1F));
1212}
1213
1214static int mov_write_uuidusmt_tag (ByteIOContext *pb, AVFormatContext *s)
1215{
1216 size_t len, size;
1217 offset_t pos, curpos;
1218
1219 size = 0;
1220 if (s->title[0]) {
1221 pos = url_ftell(pb);
1222 put_be32(pb, 0); /* size placeholder*/
1223 put_tag(pb, "uuid");
1224 put_tag(pb, "USMT");
1225 put_be32(pb, 0x21d24fce ); /* 96 bit UUID */
1226 put_be32(pb, 0xbb88695c );
1227 put_be32(pb, 0xfac9c740 );
1228 size += 24;
1229
1230 put_be32(pb, 0); /* size placeholder*/
1231 put_tag(pb, "MTDT");
1232 put_be16(pb, 4);
1233 size += 10;
1234
1235 // ?
1236 put_be16(pb, 0x0C); /* size */
1237 put_be32(pb, 0x0B); /* type */
1238 put_be16(pb, language_code("und")); /* language */
1239 put_be16(pb, 0x0); /* ? */
1240 put_be16(pb, 0x021C); /* data */
1241 size += 12;
1242
1243 // Encoder
1244 len = strlen(LIBAVCODEC_IDENT)+1;
1245 put_be16(pb, len*2+10); /* size */
1246 put_be32(pb, 0x04); /* type */
1247 put_be16(pb, language_code("eng")); /* language */
1248 put_be16(pb, 0x01); /* ? */
1249 ascii_to_wc(pb, LIBAVCODEC_IDENT, len);
1250 size += len*2+10;
1251
1252 // Title
1253 len = strlen(s->title)+1;
1254 put_be16(pb, len*2+10); /* size */
1255 put_be32(pb, 0x01); /* type */
1256 put_be16(pb, language_code("eng")); /* language */
1257 put_be16(pb, 0x01); /* ? */
1258 ascii_to_wc (pb, s->title, len);
1259 size += len*2+10;
1260
1261 // Date
1262// snprintf(dt,32,"%04d/%02d/%02d %02d:%02d:%02d",t_st->tm_year+1900,t_st->tm_mon+1,t_st->tm_mday,t_st->tm_hour,t_st->tm_min,t_st->tm_sec);
1263 len = strlen("2006/04/01 11:11:11")+1;
1264 put_be16(pb, len*2+10); /* size */
1265 put_be32(pb, 0x03); /* type */
1266 put_be16(pb, language_code("und")); /* language */
1267 put_be16(pb, 0x01); /* ? */
1268 ascii_to_wc (pb, "2006/04/01 11:11:11", len);
1269 size += len*2+10;
1270
1271 // size
1272 curpos = url_ftell(pb);
1273 url_fseek(pb, pos, SEEK_SET);
1274 put_be32(pb, size);
1275 url_fseek(pb, pos+24, SEEK_SET);
1276 put_be32(pb, size-24);
1277 url_fseek(pb, curpos, SEEK_SET);
1278 }
1279
1280 return size;
1281}
1282
1283static int mov_write_moov_tag(ByteIOContext *pb, MOVContext *mov,
1284 AVFormatContext *s)
1285{
1286 int i;
1287 offset_t pos = url_ftell(pb);
1288 put_be32(pb, 0); /* size placeholder*/
1289 put_tag(pb, "moov");
1290 mov->timescale = globalTimescale;
1291
1292 for (i=0; i<mov->nb_streams; i++) {
1293 if(mov->tracks[i].entry <= 0) continue;
1294
1295 if(mov->tracks[i].enc->codec_type == CODEC_TYPE_VIDEO) {
1296 mov->tracks[i].timescale = mov->tracks[i].enc->time_base.den;
1297 mov->tracks[i].sampleDuration = mov->tracks[i].enc->time_base.num;
1298 } else if(mov->tracks[i].enc->codec_type == CODEC_TYPE_AUDIO) {
1299 mov->tracks[i].timescale = mov->tracks[i].enc->sample_rate;
1300 mov->tracks[i].sampleDuration = mov->tracks[i].enc->frame_size;
1301 }
1302
1303 mov->tracks[i].trackDuration =
1304 (int64_t)mov->tracks[i].sampleCount * mov->tracks[i].sampleDuration;
1305 mov->tracks[i].time = mov->time;
1306 mov->tracks[i].trackID = i+1;
1307 }
1308
1309 mov_write_mvhd_tag(pb, mov);
1310 //mov_write_iods_tag(pb, mov);
1311 for (i=0; i<mov->nb_streams; i++) {
1312 if(mov->tracks[i].entry > 0) {
1313 mov_write_trak_tag(pb, &(mov->tracks[i]));
1314 }
1315 }
1316
1317 if (mov->mode == MODE_PSP)
1318 mov_write_uuidusmt_tag(pb, s);
1319 else
1320 mov_write_udta_tag(pb, mov, s);
1321
1322 return updateSize(pb, pos);
1323}
1324
1325static int mov_write_mdat_tag(ByteIOContext *pb, MOVContext* mov)
1326{
1327 put_be32(pb, 8); // placeholder for extended size field (64 bit)
1328 put_tag(pb, "wide");
1329
1330 mov->mdat_pos = url_ftell(pb);
1331 put_be32(pb, 0); /* size placeholder*/
1332 put_tag(pb, "mdat");
1333 return 0;
1334}
1335
1336/* TODO: This needs to be more general */
1337static void mov_write_ftyp_tag (ByteIOContext *pb, AVFormatContext *s)
1338{
1339 MOVContext *mov = s->priv_data;
1340
1341 put_be32(pb, 0x14 ); /* size */
1342 put_tag(pb, "ftyp");
1343
1344 if ( mov->mode == MODE_3GP )
1345 put_tag(pb, "3gp4");
1346 else if ( mov->mode == MODE_3G2 )
1347 put_tag(pb, "3g2a");
1348 else if ( mov->mode == MODE_PSP )
1349 put_tag(pb, "MSNV");
1350 else if ( mov->mode == MODE_MP4 )
1351 put_tag(pb, "isom");
1352 else
1353 put_tag(pb, "qt ");
1354
1355 put_be32(pb, 0x200 );
1356
1357 if ( mov->mode == MODE_3GP )
1358 put_tag(pb, "3gp4");
1359 else if ( mov->mode == MODE_3G2 )
1360 put_tag(pb, "3g2a");
1361 else if ( mov->mode == MODE_PSP )
1362 put_tag(pb, "MSNV");
1363 else if ( mov->mode == MODE_MP4 )
1364 put_tag(pb, "mp41");
1365 else
1366 put_tag(pb, "qt ");
1367}
1368
1369static void mov_write_uuidprof_tag(ByteIOContext *pb, AVFormatContext *s)
1370{
1371 AVCodecContext *VideoCodec = s->streams[0]->codec;
1372 AVCodecContext *AudioCodec = s->streams[1]->codec;
1373 int AudioRate = AudioCodec->sample_rate;
1374 int FrameRate = ((VideoCodec->time_base.den) * (0x10000))/ (VideoCodec->time_base.num);
1375 int audio_kbitrate= AudioCodec->bit_rate / 1000;
1376 int video_kbitrate= FFMIN(VideoCodec->bit_rate / 1000, 800 - audio_kbitrate);
1377
1378 put_be32(pb, 0x94 ); /* size */
1379 put_tag(pb, "uuid");
1380 put_tag(pb, "PROF");
1381
1382 put_be32(pb, 0x21d24fce ); /* 96 bit UUID */
1383 put_be32(pb, 0xbb88695c );
1384 put_be32(pb, 0xfac9c740 );
1385
1386 put_be32(pb, 0x0 ); /* ? */
1387 put_be32(pb, 0x3 ); /* 3 sections ? */
1388
1389 put_be32(pb, 0x14 ); /* size */
1390 put_tag(pb, "FPRF");
1391 put_be32(pb, 0x0 ); /* ? */
1392 put_be32(pb, 0x0 ); /* ? */
1393 put_be32(pb, 0x0 ); /* ? */
1394
1395 put_be32(pb, 0x2c ); /* size */
1396 put_tag(pb, "APRF"); /* audio */
1397 put_be32(pb, 0x0 );
1398 put_be32(pb, 0x2 ); /* TrackID */
1399 put_tag(pb, "mp4a");
1400 put_be32(pb, 0x20f );
1401 put_be32(pb, 0x0 );
1402 put_be32(pb, audio_kbitrate);
1403 put_be32(pb, audio_kbitrate);
1404 put_be32(pb, AudioRate );
1405 put_be32(pb, AudioCodec->channels );
1406
1407 put_be32(pb, 0x34 ); /* size */
1408 put_tag(pb, "VPRF"); /* video */
1409 put_be32(pb, 0x0 );
1410 put_be32(pb, 0x1 ); /* TrackID */
1411 if (VideoCodec->codec_id == CODEC_ID_H264) {
1412 put_tag(pb, "avc1");
1413 put_be16(pb, 0x014D );
1414 put_be16(pb, 0x0015 );
1415 } else {
1416 put_tag(pb, "mp4v");
1417 put_be16(pb, 0x0000 );
1418 put_be16(pb, 0x0103 );
1419 }
1420 put_be32(pb, 0x0 );
1421 put_be32(pb, video_kbitrate);
1422 put_be32(pb, video_kbitrate);
1423 put_be32(pb, FrameRate);
1424 put_be32(pb, FrameRate);
1425 put_be16(pb, VideoCodec->width);
1426 put_be16(pb, VideoCodec->height);
1427 put_be32(pb, 0x010001); /* ? */
1428}
1429
1430static int mov_write_header(AVFormatContext *s)
1431{
1432 ByteIOContext *pb = &s->pb;
1433 MOVContext *mov = s->priv_data;
1434 int i;
1435
1436 /* Default mode == MP4 */
1437 mov->mode = MODE_MP4;
1438
1439 if (s->oformat != NULL) {
1440 if (!strcmp("3gp", s->oformat->name)) mov->mode = MODE_3GP;
1441 else if (!strcmp("3g2", s->oformat->name)) mov->mode = MODE_3G2;
1442 else if (!strcmp("mov", s->oformat->name)) mov->mode = MODE_MOV;
1443 else if (!strcmp("psp", s->oformat->name)) mov->mode = MODE_PSP;
1444
1445 mov_write_ftyp_tag(pb,s);
1446 if ( mov->mode == MODE_PSP ) {
1447 if ( s->nb_streams != 2 ) {
1448 av_log(s, AV_LOG_ERROR, "PSP mode need one video and one audio stream\n");
1449 return -1;
1450 }
1451 mov_write_uuidprof_tag(pb,s);
1452 }
1453 }
1454
1455 for(i=0; i<s->nb_streams; i++){
1456 AVStream *st= s->streams[i];
1457 MOVTrack *track= &mov->tracks[i];
1458
1459 track->enc = st->codec;
1460 if(st->codec->codec_type == CODEC_TYPE_VIDEO){
1461 track->tag = mov_find_video_codec_tag(s, track);
1462 av_set_pts_info(st, 64, 1, st->codec->time_base.den);
1463 }else if(st->codec->codec_type == CODEC_TYPE_AUDIO){
1464 track->tag = mov_find_audio_codec_tag(s, track);
1465 av_set_pts_info(st, 64, 1, st->codec->sample_rate);
1466 track->sampleSize = (av_get_bits_per_sample(st->codec->codec_id) >> 3) * st->codec->channels;
1467 }
1468 track->language = ff_mov_iso639_to_lang(st->language, mov->mode != MODE_MOV);
1469 track->mode = mov->mode;
1470 }
1471
1472 mov_write_mdat_tag(pb, mov);
1473 mov->time = s->timestamp + 0x7C25B080; //1970 based -> 1904 based
1474 mov->nb_streams = s->nb_streams;
1475
1476 put_flush_packet(pb);
1477
1478 return 0;
1479}
1480
1481static int mov_write_packet(AVFormatContext *s, AVPacket *pkt)
1482{
1483 MOVContext *mov = s->priv_data;
1484 ByteIOContext *pb = &s->pb;
1485 MOVTrack *trk = &mov->tracks[pkt->stream_index];
1486 AVCodecContext *enc = trk->enc;
1487 unsigned int samplesInChunk = 0;
1488 int size= pkt->size;
1489
1490 if (url_is_streamed(&s->pb)) return 0; /* Can't handle that */
1491 if (!size) return 0; /* Discard 0 sized packets */
1492
1493 if (enc->codec_id == CODEC_ID_AMR_NB) {
1494 /* We must find out how many AMR blocks there are in one packet */
1495 static uint16_t packed_size[16] =
1496 {13, 14, 16, 18, 20, 21, 27, 32, 6, 0, 0, 0, 0, 0, 0, 0};
1497 int len = 0;
1498
1499 while (len < size && samplesInChunk < 100) {
1500 len += packed_size[(pkt->data[len] >> 3) & 0x0F];
1501 samplesInChunk++;
1502 }
1503 if(samplesInChunk > 1){
1504 av_log(s, AV_LOG_ERROR, "fatal error, input is not a single packet, inplement a AVParser for it\n");
1505 return -1;
1506 }
1507 } else if (trk->sampleSize)
1508 samplesInChunk = size/trk->sampleSize;
1509 else
1510 samplesInChunk = 1;
1511
1512 /* copy extradata if it exists */
1513 if (trk->vosLen == 0 && enc->extradata_size > 0) {
1514 trk->vosLen = enc->extradata_size;
1515 trk->vosData = av_malloc(trk->vosLen);
1516 memcpy(trk->vosData, enc->extradata, trk->vosLen);
1517 }
1518
1519 if (enc->codec_id == CODEC_ID_H264 && trk->vosLen > 0 && *(uint8_t *)trk->vosData != 1) {
1520 /* from x264 or from bytestream h264 */
1521 /* nal reformating needed */
1522 avc_parse_nal_units(&pkt->data, &pkt->size);
1523 assert(pkt->size);
1524 size = pkt->size;
1525 }
1526
1527 if (!(trk->entry % MOV_INDEX_CLUSTER_SIZE)) {
1528 trk->cluster = av_realloc(trk->cluster, (trk->entry + MOV_INDEX_CLUSTER_SIZE) * sizeof(*trk->cluster));
1529 if (!trk->cluster)
1530 return -1;
1531 }
1532
1533 trk->cluster[trk->entry].pos = url_ftell(pb);
1534 trk->cluster[trk->entry].samplesInChunk = samplesInChunk;
1535 trk->cluster[trk->entry].size = size;
1536 trk->cluster[trk->entry].entries = samplesInChunk;
1537 if(enc->codec_type == CODEC_TYPE_VIDEO) {
1538 if (pkt->dts != pkt->pts)
1539 trk->hasBframes = 1;
1540 trk->cluster[trk->entry].cts = pkt->pts - pkt->dts;
1541 trk->cluster[trk->entry].key_frame = !!(pkt->flags & PKT_FLAG_KEY);
1542 if(trk->cluster[trk->entry].key_frame)
1543 trk->hasKeyframes++;
1544 }
1545 trk->entry++;
1546 trk->sampleCount += samplesInChunk;
1547 mov->mdat_size += size;
1548
1549 put_buffer(pb, pkt->data, size);
1550
1551 put_flush_packet(pb);
1552 return 0;
1553}
1554
1555static int mov_write_trailer(AVFormatContext *s)
1556{
1557 MOVContext *mov = s->priv_data;
1558 ByteIOContext *pb = &s->pb;
1559 int res = 0;
1560 int i;
1561
1562 offset_t moov_pos = url_ftell(pb);
1563
1564 /* Write size of mdat tag */
1565 if (mov->mdat_size+8 <= UINT32_MAX) {
1566 url_fseek(pb, mov->mdat_pos, SEEK_SET);
1567 put_be32(pb, mov->mdat_size+8);
1568 } else {
1569 /* overwrite 'wide' placeholder atom */
1570 url_fseek(pb, mov->mdat_pos - 8, SEEK_SET);
1571 put_be32(pb, 1); /* special value: real atom size will be 64 bit value after tag field */
1572 put_tag(pb, "mdat");
1573 put_be64(pb, mov->mdat_size+16);
1574 }
1575 url_fseek(pb, moov_pos, SEEK_SET);
1576
1577 mov_write_moov_tag(pb, mov, s);
1578
1579 for (i=0; i<mov->nb_streams; i++) {
1580 av_freep(&mov->tracks[i].cluster);
1581
1582 if( mov->tracks[i].vosLen ) av_free( mov->tracks[i].vosData );
1583
1584 }
1585
1586 put_flush_packet(pb);
1587
1588 return res;
1589}
1590
1591static AVOutputFormat mov_muxer = {
1592 "mov",
1593 "mov format",
1594 NULL,
1595 "mov",
1596 sizeof(MOVContext),
1597 CODEC_ID_AAC,
1598 CODEC_ID_MPEG4,
1599 mov_write_header,
1600 mov_write_packet,
1601 mov_write_trailer,
1602 .flags = AVFMT_GLOBALHEADER,
1603};
1604
1605static AVOutputFormat _3gp_muxer = {
1606 "3gp",
1607 "3gp format",
1608 NULL,
1609 "3gp",
1610 sizeof(MOVContext),
1611 CODEC_ID_AMR_NB,
1612 CODEC_ID_H263,
1613 mov_write_header,
1614 mov_write_packet,
1615 mov_write_trailer,
1616 .flags = AVFMT_GLOBALHEADER,
1617};
1618
1619static AVOutputFormat mp4_muxer = {
1620 "mp4",
1621 "mp4 format",
1622 "application/mp4",
1623 "mp4,m4a",
1624 sizeof(MOVContext),
1625 CODEC_ID_AAC,
1626 CODEC_ID_MPEG4,
1627 mov_write_header,
1628 mov_write_packet,
1629 mov_write_trailer,
1630 .flags = AVFMT_GLOBALHEADER,
1631};
1632
1633static AVOutputFormat psp_muxer = {
1634 "psp",
1635 "psp mp4 format",
1636 NULL,
1637 "mp4,psp",
1638 sizeof(MOVContext),
1639 CODEC_ID_AAC,
1640 CODEC_ID_MPEG4,
1641 mov_write_header,
1642 mov_write_packet,
1643 mov_write_trailer,
1644 .flags = AVFMT_GLOBALHEADER,
1645};
1646
1647static AVOutputFormat _3g2_muxer = {
1648 "3g2",
1649 "3gp2 format",
1650 NULL,
1651 "3g2",
1652 sizeof(MOVContext),
1653 CODEC_ID_AMR_NB,
1654 CODEC_ID_H263,
1655 mov_write_header,
1656 mov_write_packet,
1657 mov_write_trailer,
1658 .flags = AVFMT_GLOBALHEADER,
1659};
1660
1661int movenc_init(void)
1662{
1663 av_register_output_format(&mov_muxer);
1664 av_register_output_format(&_3gp_muxer);
1665 av_register_output_format(&mp4_muxer);
1666 av_register_output_format(&psp_muxer);
1667 av_register_output_format(&_3g2_muxer);
1668 return 0;
1669}
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette