movenc.c
Go to the documentation of this file.
1 /*
2  * MOV, 3GP, MP4 muxer
3  * Copyright (c) 2003 Thomas Raivio
4  * Copyright (c) 2004 Gildas Bazin <gbazin at videolan dot org>
5  * Copyright (c) 2009 Baptiste Coudurier <baptiste dot coudurier at gmail dot com>
6  *
7  * This file is part of Libav.
8  *
9  * Libav is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * Libav is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with Libav; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  */
23 
24 #include "movenc.h"
25 #include "avformat.h"
26 #include "avio_internal.h"
27 #include "riff.h"
28 #include "avio.h"
29 #include "isom.h"
30 #include "avc.h"
31 #include "libavcodec/get_bits.h"
32 #include "libavcodec/put_bits.h"
33 #include "internal.h"
34 #include "libavutil/avstring.h"
35 #include "libavutil/intfloat.h"
36 #include "libavutil/mathematics.h"
37 #include "libavutil/opt.h"
38 #include "libavutil/dict.h"
39 #include "rtpenc.h"
40 #include "mov_chan.h"
41 
42 #undef NDEBUG
43 #include <assert.h>
44 
45 static const AVOption options[] = {
46  { "movflags", "MOV muxer flags", offsetof(MOVMuxContext, flags), AV_OPT_TYPE_FLAGS, {.dbl = 0}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
47  { "rtphint", "Add RTP hint tracks", 0, AV_OPT_TYPE_CONST, {.dbl = FF_MOV_FLAG_RTP_HINT}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
48  FF_RTP_FLAG_OPTS(MOVMuxContext, rtp_flags),
49  { "skip_iods", "Skip writing iods atom.", offsetof(MOVMuxContext, iods_skip), AV_OPT_TYPE_INT, {.dbl = 0}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM},
50  { "iods_audio_profile", "iods audio profile atom.", offsetof(MOVMuxContext, iods_audio_profile), AV_OPT_TYPE_INT, {.dbl = -1}, -1, 255, AV_OPT_FLAG_ENCODING_PARAM},
51  { "iods_video_profile", "iods video profile atom.", offsetof(MOVMuxContext, iods_video_profile), AV_OPT_TYPE_INT, {.dbl = -1}, -1, 255, AV_OPT_FLAG_ENCODING_PARAM},
52  { NULL },
53 };
54 
55 #define MOV_CLASS(flavor)\
56 static const AVClass flavor ## _muxer_class = {\
57  .class_name = #flavor " muxer",\
58  .item_name = av_default_item_name,\
59  .option = options,\
60  .version = LIBAVUTIL_VERSION_INT,\
61 };
62 
63 //FIXME support 64 bit variant with wide placeholders
64 static int64_t updateSize(AVIOContext *pb, int64_t pos)
65 {
66  int64_t curpos = avio_tell(pb);
67  avio_seek(pb, pos, SEEK_SET);
68  avio_wb32(pb, curpos - pos); /* rewrite size */
69  avio_seek(pb, curpos, SEEK_SET);
70 
71  return curpos - pos;
72 }
73 
74 /* Chunk offset atom */
75 static int mov_write_stco_tag(AVIOContext *pb, MOVTrack *track)
76 {
77  int i;
78  int mode64 = 0; // use 32 bit size variant if possible
79  int64_t pos = avio_tell(pb);
80  avio_wb32(pb, 0); /* size */
81  if (pos > UINT32_MAX) {
82  mode64 = 1;
83  ffio_wfourcc(pb, "co64");
84  } else
85  ffio_wfourcc(pb, "stco");
86  avio_wb32(pb, 0); /* version & flags */
87  avio_wb32(pb, track->entry); /* entry count */
88  for (i=0; i<track->entry; i++) {
89  if(mode64 == 1)
90  avio_wb64(pb, track->cluster[i].pos);
91  else
92  avio_wb32(pb, track->cluster[i].pos);
93  }
94  return updateSize(pb, pos);
95 }
96 
97 /* Sample size atom */
98 static int mov_write_stsz_tag(AVIOContext *pb, MOVTrack *track)
99 {
100  int equalChunks = 1;
101  int i, j, entries = 0, tst = -1, oldtst = -1;
102 
103  int64_t pos = avio_tell(pb);
104  avio_wb32(pb, 0); /* size */
105  ffio_wfourcc(pb, "stsz");
106  avio_wb32(pb, 0); /* version & flags */
107 
108  for (i=0; i<track->entry; i++) {
109  tst = track->cluster[i].size/track->cluster[i].entries;
110  if(oldtst != -1 && tst != oldtst) {
111  equalChunks = 0;
112  }
113  oldtst = tst;
114  entries += track->cluster[i].entries;
115  }
116  if (equalChunks) {
117  int sSize = track->cluster[0].size/track->cluster[0].entries;
118  sSize = FFMAX(1, sSize); // adpcm mono case could make sSize == 0
119  avio_wb32(pb, sSize); // sample size
120  avio_wb32(pb, entries); // sample count
121  }
122  else {
123  avio_wb32(pb, 0); // sample size
124  avio_wb32(pb, entries); // sample count
125  for (i=0; i<track->entry; i++) {
126  for (j=0; j<track->cluster[i].entries; j++) {
127  avio_wb32(pb, track->cluster[i].size /
128  track->cluster[i].entries);
129  }
130  }
131  }
132  return updateSize(pb, pos);
133 }
134 
135 /* Sample to chunk atom */
136 static int mov_write_stsc_tag(AVIOContext *pb, MOVTrack *track)
137 {
138  int index = 0, oldval = -1, i;
139  int64_t entryPos, curpos;
140 
141  int64_t pos = avio_tell(pb);
142  avio_wb32(pb, 0); /* size */
143  ffio_wfourcc(pb, "stsc");
144  avio_wb32(pb, 0); // version & flags
145  entryPos = avio_tell(pb);
146  avio_wb32(pb, track->entry); // entry count
147  for (i=0; i<track->entry; i++) {
148  if(oldval != track->cluster[i].samplesInChunk)
149  {
150  avio_wb32(pb, i+1); // first chunk
151  avio_wb32(pb, track->cluster[i].samplesInChunk); // samples per chunk
152  avio_wb32(pb, 0x1); // sample description index
153  oldval = track->cluster[i].samplesInChunk;
154  index++;
155  }
156  }
157  curpos = avio_tell(pb);
158  avio_seek(pb, entryPos, SEEK_SET);
159  avio_wb32(pb, index); // rewrite size
160  avio_seek(pb, curpos, SEEK_SET);
161 
162  return updateSize(pb, pos);
163 }
164 
165 /* Sync sample atom */
166 static int mov_write_stss_tag(AVIOContext *pb, MOVTrack *track, uint32_t flag)
167 {
168  int64_t curpos, entryPos;
169  int i, index = 0;
170  int64_t pos = avio_tell(pb);
171  avio_wb32(pb, 0); // size
172  ffio_wfourcc(pb, flag == MOV_SYNC_SAMPLE ? "stss" : "stps");
173  avio_wb32(pb, 0); // version & flags
174  entryPos = avio_tell(pb);
175  avio_wb32(pb, track->entry); // entry count
176  for (i=0; i<track->entry; i++) {
177  if (track->cluster[i].flags & flag) {
178  avio_wb32(pb, i+1);
179  index++;
180  }
181  }
182  curpos = avio_tell(pb);
183  avio_seek(pb, entryPos, SEEK_SET);
184  avio_wb32(pb, index); // rewrite size
185  avio_seek(pb, curpos, SEEK_SET);
186  return updateSize(pb, pos);
187 }
188 
189 static int mov_write_amr_tag(AVIOContext *pb, MOVTrack *track)
190 {
191  avio_wb32(pb, 0x11); /* size */
192  if (track->mode == MODE_MOV) ffio_wfourcc(pb, "samr");
193  else ffio_wfourcc(pb, "damr");
194  ffio_wfourcc(pb, "FFMP");
195  avio_w8(pb, 0); /* decoder version */
196 
197  avio_wb16(pb, 0x81FF); /* Mode set (all modes for AMR_NB) */
198  avio_w8(pb, 0x00); /* Mode change period (no restriction) */
199  avio_w8(pb, 0x01); /* Frames per sample */
200  return 0x11;
201 }
202 
203 static int mov_write_ac3_tag(AVIOContext *pb, MOVTrack *track)
204 {
205  GetBitContext gbc;
206  PutBitContext pbc;
207  uint8_t buf[3];
208  int fscod, bsid, bsmod, acmod, lfeon, frmsizecod;
209 
210  if (track->vosLen < 7)
211  return -1;
212 
213  avio_wb32(pb, 11);
214  ffio_wfourcc(pb, "dac3");
215 
216  init_get_bits(&gbc, track->vosData+4, (track->vosLen-4) * 8);
217  fscod = get_bits(&gbc, 2);
218  frmsizecod = get_bits(&gbc, 6);
219  bsid = get_bits(&gbc, 5);
220  bsmod = get_bits(&gbc, 3);
221  acmod = get_bits(&gbc, 3);
222  if (acmod == 2) {
223  skip_bits(&gbc, 2); // dsurmod
224  } else {
225  if ((acmod & 1) && acmod != 1)
226  skip_bits(&gbc, 2); // cmixlev
227  if (acmod & 4)
228  skip_bits(&gbc, 2); // surmixlev
229  }
230  lfeon = get_bits1(&gbc);
231 
232  init_put_bits(&pbc, buf, sizeof(buf));
233  put_bits(&pbc, 2, fscod);
234  put_bits(&pbc, 5, bsid);
235  put_bits(&pbc, 3, bsmod);
236  put_bits(&pbc, 3, acmod);
237  put_bits(&pbc, 1, lfeon);
238  put_bits(&pbc, 5, frmsizecod>>1); // bit_rate_code
239  put_bits(&pbc, 5, 0); // reserved
240 
241  flush_put_bits(&pbc);
242  avio_write(pb, buf, sizeof(buf));
243 
244  return 11;
245 }
246 
252 {
253  avio_write(pb, track->enc->extradata, track->enc->extradata_size);
254  return track->enc->extradata_size;
255 }
256 
258 {
259  avio_wb32(pb, 10);
260  ffio_wfourcc(pb, "enda");
261  avio_wb16(pb, 1); /* little endian */
262  return 10;
263 }
264 
265 static void putDescr(AVIOContext *pb, int tag, unsigned int size)
266 {
267  int i = 3;
268  avio_w8(pb, tag);
269  for(; i>0; i--)
270  avio_w8(pb, (size>>(7*i)) | 0x80);
271  avio_w8(pb, size & 0x7F);
272 }
273 
274 static int mov_write_esds_tag(AVIOContext *pb, MOVTrack *track) // Basic
275 {
276  int64_t pos = avio_tell(pb);
277  int decoderSpecificInfoLen = track->vosLen ? 5+track->vosLen : 0;
278 
279  avio_wb32(pb, 0); // size
280  ffio_wfourcc(pb, "esds");
281  avio_wb32(pb, 0); // Version
282 
283  // ES descriptor
284  putDescr(pb, 0x03, 3 + 5+13 + decoderSpecificInfoLen + 5+1);
285  avio_wb16(pb, track->trackID);
286  avio_w8(pb, 0x00); // flags (= no flags)
287 
288  // DecoderConfig descriptor
289  putDescr(pb, 0x04, 13 + decoderSpecificInfoLen);
290 
291  // Object type indication
292  if ((track->enc->codec_id == CODEC_ID_MP2 ||
293  track->enc->codec_id == CODEC_ID_MP3) &&
294  track->enc->sample_rate > 24000)
295  avio_w8(pb, 0x6B); // 11172-3
296  else
298 
299  // the following fields is made of 6 bits to identify the streamtype (4 for video, 5 for audio)
300  // plus 1 bit to indicate upstream and 1 bit set to 1 (reserved)
301  if(track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
302  avio_w8(pb, 0x15); // flags (= Audiostream)
303  else
304  avio_w8(pb, 0x11); // flags (= Visualstream)
305 
306  avio_w8(pb, track->enc->rc_buffer_size>>(3+16)); // Buffersize DB (24 bits)
307  avio_wb16(pb, (track->enc->rc_buffer_size>>3)&0xFFFF); // Buffersize DB
308 
309  avio_wb32(pb, FFMAX(track->enc->bit_rate, track->enc->rc_max_rate)); // maxbitrate (FIXME should be max rate in any 1 sec window)
310  if(track->enc->rc_max_rate != track->enc->rc_min_rate || track->enc->rc_min_rate==0)
311  avio_wb32(pb, 0); // vbr
312  else
313  avio_wb32(pb, track->enc->rc_max_rate); // avg bitrate
314 
315  if (track->vosLen) {
316  // DecoderSpecific info descriptor
317  putDescr(pb, 0x05, track->vosLen);
318  avio_write(pb, track->vosData, track->vosLen);
319  }
320 
321  // SL descriptor
322  putDescr(pb, 0x06, 1);
323  avio_w8(pb, 0x02);
324  return updateSize(pb, pos);
325 }
326 
328 {
329  return codec_id == CODEC_ID_PCM_S24LE ||
330  codec_id == CODEC_ID_PCM_S32LE ||
331  codec_id == CODEC_ID_PCM_F32LE ||
332  codec_id == CODEC_ID_PCM_F64LE;
333 }
334 
335 static int mov_write_ms_tag(AVIOContext *pb, MOVTrack *track)
336 {
337  int64_t pos = avio_tell(pb);
338  avio_wb32(pb, 0);
339  avio_wl32(pb, track->tag); // store it byteswapped
340  track->enc->codec_tag = av_bswap16(track->tag >> 16);
341  ff_put_wav_header(pb, track->enc);
342  return updateSize(pb, pos);
343 }
344 
345 static int mov_write_chan_tag(AVIOContext *pb, MOVTrack *track)
346 {
347  uint32_t layout_tag, bitmap;
348  int64_t pos = avio_tell(pb);
349 
350  layout_tag = ff_mov_get_channel_layout_tag(track->enc->codec_id,
351  track->enc->channel_layout,
352  &bitmap);
353  if (!layout_tag) {
354  av_log(track->enc, AV_LOG_WARNING, "not writing 'chan' tag due to "
355  "lack of channel information\n");
356  return 0;
357  }
358 
359  avio_wb32(pb, 0); // Size
360  ffio_wfourcc(pb, "chan"); // Type
361  avio_w8(pb, 0); // Version
362  avio_wb24(pb, 0); // Flags
363  avio_wb32(pb, layout_tag); // mChannelLayoutTag
364  avio_wb32(pb, bitmap); // mChannelBitmap
365  avio_wb32(pb, 0); // mNumberChannelDescriptions
366 
367  return updateSize(pb, pos);
368 }
369 
370 static int mov_write_wave_tag(AVIOContext *pb, MOVTrack *track)
371 {
372  int64_t pos = avio_tell(pb);
373 
374  avio_wb32(pb, 0); /* size */
375  ffio_wfourcc(pb, "wave");
376 
377  avio_wb32(pb, 12); /* size */
378  ffio_wfourcc(pb, "frma");
379  avio_wl32(pb, track->tag);
380 
381  if (track->enc->codec_id == CODEC_ID_AAC) {
382  /* useless atom needed by mplayer, ipod, not needed by quicktime */
383  avio_wb32(pb, 12); /* size */
384  ffio_wfourcc(pb, "mp4a");
385  avio_wb32(pb, 0);
386  mov_write_esds_tag(pb, track);
387  } else if (mov_pcm_le_gt16(track->enc->codec_id)) {
388  mov_write_enda_tag(pb);
389  } else if (track->enc->codec_id == CODEC_ID_AMR_NB) {
390  mov_write_amr_tag(pb, track);
391  } else if (track->enc->codec_id == CODEC_ID_AC3) {
392  mov_write_chan_tag(pb, track);
393  mov_write_ac3_tag(pb, track);
394  } else if (track->enc->codec_id == CODEC_ID_ALAC) {
395  mov_write_extradata_tag(pb, track);
396  } else if (track->enc->codec_id == CODEC_ID_ADPCM_MS ||
397  track->enc->codec_id == CODEC_ID_ADPCM_IMA_WAV) {
398  mov_write_ms_tag(pb, track);
399  }
400 
401  avio_wb32(pb, 8); /* size */
402  avio_wb32(pb, 0); /* null tag */
403 
404  return updateSize(pb, pos);
405 }
406 
407 static int mov_write_glbl_tag(AVIOContext *pb, MOVTrack *track)
408 {
409  avio_wb32(pb, track->vosLen+8);
410  ffio_wfourcc(pb, "glbl");
411  avio_write(pb, track->vosData, track->vosLen);
412  return 8+track->vosLen;
413 }
414 
420 {
421  switch (codec_id) {
422  case CODEC_ID_PCM_F32BE:
423  case CODEC_ID_PCM_F64BE:
424  return 11;
425  case CODEC_ID_PCM_F32LE:
426  case CODEC_ID_PCM_F64LE:
427  return 9;
428  case CODEC_ID_PCM_U8:
429  return 10;
430  case CODEC_ID_PCM_S16BE:
431  case CODEC_ID_PCM_S24BE:
432  case CODEC_ID_PCM_S32BE:
433  return 14;
434  case CODEC_ID_PCM_S8:
435  case CODEC_ID_PCM_S16LE:
436  case CODEC_ID_PCM_S24LE:
437  case CODEC_ID_PCM_S32LE:
438  return 12;
439  default:
440  return 0;
441  }
442 }
443 
444 static int mov_write_audio_tag(AVIOContext *pb, MOVTrack *track)
445 {
446  int64_t pos = avio_tell(pb);
447  int version = 0;
448  uint32_t tag = track->tag;
449 
450  if (track->mode == MODE_MOV) {
451  if (mov_get_lpcm_flags(track->enc->codec_id))
452  tag = AV_RL32("lpcm");
453  version = 2;
454  }
455 
456  avio_wb32(pb, 0); /* size */
457  avio_wl32(pb, tag); // store it byteswapped
458  avio_wb32(pb, 0); /* Reserved */
459  avio_wb16(pb, 0); /* Reserved */
460  avio_wb16(pb, 1); /* Data-reference index, XXX == 1 */
461 
462  /* SoundDescription */
463  avio_wb16(pb, version); /* Version */
464  avio_wb16(pb, 0); /* Revision level */
465  avio_wb32(pb, 0); /* Reserved */
466 
467  if (version == 2) {
468  avio_wb16(pb, 3);
469  avio_wb16(pb, 16);
470  avio_wb16(pb, 0xfffe);
471  avio_wb16(pb, 0);
472  avio_wb32(pb, 0x00010000);
473  avio_wb32(pb, 72);
474  avio_wb64(pb, av_double2int(track->timescale));
475  avio_wb32(pb, track->enc->channels);
476  avio_wb32(pb, 0x7F000000);
479  avio_wb32(pb, track->sampleSize);
480  avio_wb32(pb, track->audio_vbr ? track->enc->frame_size : 1);
481  } else {
482  /* reserved for mp4/3gp */
483  avio_wb16(pb, 2);
484  avio_wb16(pb, 16);
485  avio_wb16(pb, 0);
486 
487  avio_wb16(pb, 0); /* packet size (= 0) */
488  avio_wb16(pb, track->timescale); /* Time scale */
489  avio_wb16(pb, 0); /* Reserved */
490  }
491 
492  if(track->mode == MODE_MOV &&
493  (track->enc->codec_id == CODEC_ID_AAC ||
494  track->enc->codec_id == CODEC_ID_AC3 ||
495  track->enc->codec_id == CODEC_ID_AMR_NB ||
496  track->enc->codec_id == CODEC_ID_ALAC ||
497  track->enc->codec_id == CODEC_ID_ADPCM_MS ||
498  track->enc->codec_id == CODEC_ID_ADPCM_IMA_WAV ||
499  mov_pcm_le_gt16(track->enc->codec_id)))
500  mov_write_wave_tag(pb, track);
501  else if(track->tag == MKTAG('m','p','4','a'))
502  mov_write_esds_tag(pb, track);
503  else if(track->enc->codec_id == CODEC_ID_AMR_NB)
504  mov_write_amr_tag(pb, track);
505  else if(track->enc->codec_id == CODEC_ID_AC3)
506  mov_write_ac3_tag(pb, track);
507  else if(track->enc->codec_id == CODEC_ID_ALAC)
508  mov_write_extradata_tag(pb, track);
509  else if(track->vosLen > 0)
510  mov_write_glbl_tag(pb, track);
511 
512  return updateSize(pb, pos);
513 }
514 
516 {
517  avio_wb32(pb, 0xf); /* size */
518  ffio_wfourcc(pb, "d263");
519  ffio_wfourcc(pb, "FFMP");
520  avio_w8(pb, 0); /* decoder version */
521  /* FIXME use AVCodecContext level/profile, when encoder will set values */
522  avio_w8(pb, 0xa); /* level */
523  avio_w8(pb, 0); /* profile */
524  return 0xf;
525 }
526 
527 /* TODO: No idea about these values */
529 {
530  avio_wb32(pb, 0x15);
531  ffio_wfourcc(pb, "SMI ");
532  ffio_wfourcc(pb, "SEQH");
533  avio_wb32(pb, 0x5);
534  avio_wb32(pb, 0xe2c0211d);
535  avio_wb32(pb, 0xc0000000);
536  avio_w8(pb, 0);
537  return 0x15;
538 }
539 
540 static int mov_write_avcc_tag(AVIOContext *pb, MOVTrack *track)
541 {
542  int64_t pos = avio_tell(pb);
543 
544  avio_wb32(pb, 0);
545  ffio_wfourcc(pb, "avcC");
546  ff_isom_write_avcc(pb, track->vosData, track->vosLen);
547  return updateSize(pb, pos);
548 }
549 
550 /* also used by all avid codecs (dv, imx, meridien) and their variants */
551 static int mov_write_avid_tag(AVIOContext *pb, MOVTrack *track)
552 {
553  int i;
554  avio_wb32(pb, 24); /* size */
555  ffio_wfourcc(pb, "ACLR");
556  ffio_wfourcc(pb, "ACLR");
557  ffio_wfourcc(pb, "0001");
558  avio_wb32(pb, 2); /* yuv range: full 1 / normal 2 */
559  avio_wb32(pb, 0); /* unknown */
560 
561  avio_wb32(pb, 24); /* size */
562  ffio_wfourcc(pb, "APRG");
563  ffio_wfourcc(pb, "APRG");
564  ffio_wfourcc(pb, "0001");
565  avio_wb32(pb, 1); /* unknown */
566  avio_wb32(pb, 0); /* unknown */
567 
568  avio_wb32(pb, 120); /* size */
569  ffio_wfourcc(pb, "ARES");
570  ffio_wfourcc(pb, "ARES");
571  ffio_wfourcc(pb, "0001");
572  avio_wb32(pb, AV_RB32(track->vosData + 0x28)); /* dnxhd cid, some id ? */
573  avio_wb32(pb, track->enc->width);
574  /* values below are based on samples created with quicktime and avid codecs */
575  if (track->vosData[5] & 2) { // interlaced
576  avio_wb32(pb, track->enc->height/2);
577  avio_wb32(pb, 2); /* unknown */
578  avio_wb32(pb, 0); /* unknown */
579  avio_wb32(pb, 4); /* unknown */
580  } else {
581  avio_wb32(pb, track->enc->height);
582  avio_wb32(pb, 1); /* unknown */
583  avio_wb32(pb, 0); /* unknown */
584  if (track->enc->height == 1080)
585  avio_wb32(pb, 5); /* unknown */
586  else
587  avio_wb32(pb, 6); /* unknown */
588  }
589  /* padding */
590  for (i = 0; i < 10; i++)
591  avio_wb64(pb, 0);
592 
593  /* extra padding for stsd needed */
594  avio_wb32(pb, 0);
595  return 0;
596 }
597 
599 {
600  int tag = track->enc->codec_tag;
601 
603  return 0;
604 
605  if (track->enc->codec_id == CODEC_ID_H264) tag = MKTAG('a','v','c','1');
606  else if (track->enc->codec_id == CODEC_ID_AC3) tag = MKTAG('a','c','-','3');
607  else if (track->enc->codec_id == CODEC_ID_DIRAC) tag = MKTAG('d','r','a','c');
608  else if (track->enc->codec_id == CODEC_ID_MOV_TEXT) tag = MKTAG('t','x','3','g');
609  else if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) tag = MKTAG('m','p','4','v');
610  else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO) tag = MKTAG('m','p','4','a');
611 
612  return tag;
613 }
614 
615 static const AVCodecTag codec_ipod_tags[] = {
616  { CODEC_ID_H264, MKTAG('a','v','c','1') },
617  { CODEC_ID_MPEG4, MKTAG('m','p','4','v') },
618  { CODEC_ID_AAC, MKTAG('m','p','4','a') },
619  { CODEC_ID_ALAC, MKTAG('a','l','a','c') },
620  { CODEC_ID_AC3, MKTAG('a','c','-','3') },
621  { CODEC_ID_MOV_TEXT, MKTAG('t','x','3','g') },
622  { CODEC_ID_MOV_TEXT, MKTAG('t','e','x','t') },
623  { CODEC_ID_NONE, 0 },
624 };
625 
627 {
628  int tag = track->enc->codec_tag;
629 
630  // keep original tag for subs, ipod supports both formats
631  if (!(track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE &&
632  (tag == MKTAG('t','x','3','g') ||
633  tag == MKTAG('t','e','x','t'))))
634  tag = ff_codec_get_tag(codec_ipod_tags, track->enc->codec_id);
635 
636  if (!av_match_ext(s->filename, "m4a") && !av_match_ext(s->filename, "m4v"))
637  av_log(s, AV_LOG_WARNING, "Warning, extension is not .m4a nor .m4v "
638  "Quicktime/Ipod might not play the file\n");
639 
640  return tag;
641 }
642 
644 {
645  int tag;
646 
647  if (track->enc->width == 720) /* SD */
648  if (track->enc->height == 480) /* NTSC */
649  if (track->enc->pix_fmt == PIX_FMT_YUV422P) tag = MKTAG('d','v','5','n');
650  else tag = MKTAG('d','v','c',' ');
651  else if (track->enc->pix_fmt == PIX_FMT_YUV422P) tag = MKTAG('d','v','5','p');
652  else if (track->enc->pix_fmt == PIX_FMT_YUV420P) tag = MKTAG('d','v','c','p');
653  else tag = MKTAG('d','v','p','p');
654  else if (track->enc->height == 720) /* HD 720 line */
655  if (track->enc->time_base.den == 50) tag = MKTAG('d','v','h','q');
656  else tag = MKTAG('d','v','h','p');
657  else if (track->enc->height == 1080) /* HD 1080 line */
658  if (track->enc->time_base.den == 25) tag = MKTAG('d','v','h','5');
659  else tag = MKTAG('d','v','h','6');
660  else {
661  av_log(s, AV_LOG_ERROR, "unsupported height for dv codec\n");
662  return 0;
663  }
664 
665  return tag;
666 }
667 
668 static const struct {
670  uint32_t tag;
671  unsigned bps;
672 } mov_pix_fmt_tags[] = {
673  { PIX_FMT_YUYV422, MKTAG('y','u','v','s'), 0 },
674  { PIX_FMT_UYVY422, MKTAG('2','v','u','y'), 0 },
675  { PIX_FMT_RGB555BE,MKTAG('r','a','w',' '), 16 },
676  { PIX_FMT_RGB555LE,MKTAG('L','5','5','5'), 16 },
677  { PIX_FMT_RGB565LE,MKTAG('L','5','6','5'), 16 },
678  { PIX_FMT_RGB565BE,MKTAG('B','5','6','5'), 16 },
679  { PIX_FMT_GRAY16BE,MKTAG('b','1','6','g'), 16 },
680  { PIX_FMT_RGB24, MKTAG('r','a','w',' '), 24 },
681  { PIX_FMT_BGR24, MKTAG('2','4','B','G'), 24 },
682  { PIX_FMT_ARGB, MKTAG('r','a','w',' '), 32 },
683  { PIX_FMT_BGRA, MKTAG('B','G','R','A'), 32 },
684  { PIX_FMT_RGBA, MKTAG('R','G','B','A'), 32 },
685  { PIX_FMT_ABGR, MKTAG('A','B','G','R'), 32 },
686  { PIX_FMT_RGB48BE, MKTAG('b','4','8','r'), 48 },
687 };
688 
690 {
691  int tag = track->enc->codec_tag;
692  int i;
693 
694  for (i = 0; i < FF_ARRAY_ELEMS(mov_pix_fmt_tags); i++) {
695  if (track->enc->pix_fmt == mov_pix_fmt_tags[i].pix_fmt) {
696  tag = mov_pix_fmt_tags[i].tag;
697  track->enc->bits_per_coded_sample = mov_pix_fmt_tags[i].bps;
698  break;
699  }
700  }
701 
702  return tag;
703 }
704 
706 {
707  int tag = track->enc->codec_tag;
708 
709  if (!tag || (track->enc->strict_std_compliance >= FF_COMPLIANCE_NORMAL &&
710  (track->enc->codec_id == CODEC_ID_DVVIDEO ||
711  track->enc->codec_id == CODEC_ID_RAWVIDEO ||
712  track->enc->codec_id == CODEC_ID_H263 ||
713  av_get_bits_per_sample(track->enc->codec_id)))) { // pcm audio
714  if (track->enc->codec_id == CODEC_ID_DVVIDEO)
715  tag = mov_get_dv_codec_tag(s, track);
716  else if (track->enc->codec_id == CODEC_ID_RAWVIDEO)
717  tag = mov_get_rawvideo_codec_tag(s, track);
718  else if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
720  if (!tag) { // if no mac fcc found, try with Microsoft tags
722  if (tag)
723  av_log(s, AV_LOG_WARNING, "Using MS style video codec tag, "
724  "the file may be unplayable!\n");
725  }
726  } else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO) {
728  if (!tag) { // if no mac fcc found, try with Microsoft tags
729  int ms_tag = ff_codec_get_tag(ff_codec_wav_tags, track->enc->codec_id);
730  if (ms_tag) {
731  tag = MKTAG('m', 's', ((ms_tag >> 8) & 0xff), (ms_tag & 0xff));
732  av_log(s, AV_LOG_WARNING, "Using MS style audio codec tag, "
733  "the file may be unplayable!\n");
734  }
735  }
736  } else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE)
738  }
739 
740  return tag;
741 }
742 
743 static const AVCodecTag codec_3gp_tags[] = {
744  { CODEC_ID_H263, MKTAG('s','2','6','3') },
745  { CODEC_ID_H264, MKTAG('a','v','c','1') },
746  { CODEC_ID_MPEG4, MKTAG('m','p','4','v') },
747  { CODEC_ID_AAC, MKTAG('m','p','4','a') },
748  { CODEC_ID_AMR_NB, MKTAG('s','a','m','r') },
749  { CODEC_ID_AMR_WB, MKTAG('s','a','w','b') },
750  { CODEC_ID_MOV_TEXT, MKTAG('t','x','3','g') },
751  { CODEC_ID_NONE, 0 },
752 };
753 
755 {
756  int tag = track->enc->codec_tag;
757 
758  if (track->mode == MODE_MP4 || track->mode == MODE_PSP)
759  tag = mp4_get_codec_tag(s, track);
760  else if (track->mode == MODE_IPOD)
761  tag = ipod_get_codec_tag(s, track);
762  else if (track->mode & MODE_3GP)
763  tag = ff_codec_get_tag(codec_3gp_tags, track->enc->codec_id);
764  else
765  tag = mov_get_codec_tag(s, track);
766 
767  return tag;
768 }
769 
775 {
776  avio_wb32(pb, 28);
777  ffio_wfourcc(pb, "uuid");
778  avio_wb32(pb, 0x6b6840f2);
779  avio_wb32(pb, 0x5f244fc5);
780  avio_wb32(pb, 0xba39a51b);
781  avio_wb32(pb, 0xcf0323f3);
782  avio_wb32(pb, 0x0);
783  return 28;
784 }
785 
786 static const uint16_t fiel_data[] = {
787  0x0000, 0x0100, 0x0201, 0x0206, 0x0209, 0x020e
788 };
789 
790 static int mov_write_fiel_tag(AVIOContext *pb, MOVTrack *track)
791 {
792  unsigned mov_field_order = 0;
793  if (track->enc->field_order < FF_ARRAY_ELEMS(fiel_data))
794  mov_field_order = fiel_data[track->enc->field_order];
795  else
796  return 0;
797  avio_wb32(pb, 10);
798  ffio_wfourcc(pb, "fiel");
799  avio_wb16(pb, mov_field_order);
800  return 10;
801 }
802 
804 {
805  int64_t pos = avio_tell(pb);
806  avio_wb32(pb, 0); /* size */
807  avio_wl32(pb, track->tag); // store it byteswapped
808  avio_wb32(pb, 0); /* Reserved */
809  avio_wb16(pb, 0); /* Reserved */
810  avio_wb16(pb, 1); /* Data-reference index */
811 
812  if (track->enc->extradata_size)
813  avio_write(pb, track->enc->extradata, track->enc->extradata_size);
814 
815  return updateSize(pb, pos);
816 }
817 
818 static int mov_write_pasp_tag(AVIOContext *pb, MOVTrack *track)
819 {
820  AVRational sar;
821  av_reduce(&sar.num, &sar.den, track->enc->sample_aspect_ratio.num,
822  track->enc->sample_aspect_ratio.den, INT_MAX);
823 
824  avio_wb32(pb, 16);
825  ffio_wfourcc(pb, "pasp");
826  avio_wb32(pb, sar.num);
827  avio_wb32(pb, sar.den);
828  return 16;
829 }
830 
831 static int mov_write_video_tag(AVIOContext *pb, MOVTrack *track)
832 {
833  int64_t pos = avio_tell(pb);
834  char compressor_name[32];
835 
836  avio_wb32(pb, 0); /* size */
837  avio_wl32(pb, track->tag); // store it byteswapped
838  avio_wb32(pb, 0); /* Reserved */
839  avio_wb16(pb, 0); /* Reserved */
840  avio_wb16(pb, 1); /* Data-reference index */
841 
842  avio_wb16(pb, 0); /* Codec stream version */
843  avio_wb16(pb, 0); /* Codec stream revision (=0) */
844  if (track->mode == MODE_MOV) {
845  ffio_wfourcc(pb, "FFMP"); /* Vendor */
846  if(track->enc->codec_id == CODEC_ID_RAWVIDEO) {
847  avio_wb32(pb, 0); /* Temporal Quality */
848  avio_wb32(pb, 0x400); /* Spatial Quality = lossless*/
849  } else {
850  avio_wb32(pb, 0x200); /* Temporal Quality = normal */
851  avio_wb32(pb, 0x200); /* Spatial Quality = normal */
852  }
853  } else {
854  avio_wb32(pb, 0); /* Reserved */
855  avio_wb32(pb, 0); /* Reserved */
856  avio_wb32(pb, 0); /* Reserved */
857  }
858  avio_wb16(pb, track->enc->width); /* Video width */
859  avio_wb16(pb, track->height); /* Video height */
860  avio_wb32(pb, 0x00480000); /* Horizontal resolution 72dpi */
861  avio_wb32(pb, 0x00480000); /* Vertical resolution 72dpi */
862  avio_wb32(pb, 0); /* Data size (= 0) */
863  avio_wb16(pb, 1); /* Frame count (= 1) */
864 
865  memset(compressor_name,0,32);
866  /* FIXME not sure, ISO 14496-1 draft where it shall be set to 0 */
867  if (track->mode == MODE_MOV && track->enc->codec && track->enc->codec->name)
868  av_strlcpy(compressor_name,track->enc->codec->name,32);
869  avio_w8(pb, strlen(compressor_name));
870  avio_write(pb, compressor_name, 31);
871 
872  if (track->mode == MODE_MOV && track->enc->bits_per_coded_sample)
873  avio_wb16(pb, track->enc->bits_per_coded_sample);
874  else
875  avio_wb16(pb, 0x18); /* Reserved */
876  avio_wb16(pb, 0xffff); /* Reserved */
877  if(track->tag == MKTAG('m','p','4','v'))
878  mov_write_esds_tag(pb, track);
879  else if(track->enc->codec_id == CODEC_ID_H263)
880  mov_write_d263_tag(pb);
881  else if(track->enc->codec_id == CODEC_ID_SVQ3)
882  mov_write_svq3_tag(pb);
883  else if(track->enc->codec_id == CODEC_ID_DNXHD)
884  mov_write_avid_tag(pb, track);
885  else if(track->enc->codec_id == CODEC_ID_H264) {
886  mov_write_avcc_tag(pb, track);
887  if(track->mode == MODE_IPOD)
889  } else if (track->enc->field_order != AV_FIELD_UNKNOWN)
890  mov_write_fiel_tag(pb, track);
891  else if(track->vosLen > 0)
892  mov_write_glbl_tag(pb, track);
893 
894  if (track->enc->sample_aspect_ratio.den && track->enc->sample_aspect_ratio.num &&
895  track->enc->sample_aspect_ratio.den != track->enc->sample_aspect_ratio.num) {
896  mov_write_pasp_tag(pb, track);
897  }
898 
899  return updateSize(pb, pos);
900 }
901 
902 static int mov_write_rtp_tag(AVIOContext *pb, MOVTrack *track)
903 {
904  int64_t pos = avio_tell(pb);
905  avio_wb32(pb, 0); /* size */
906  ffio_wfourcc(pb, "rtp ");
907  avio_wb32(pb, 0); /* Reserved */
908  avio_wb16(pb, 0); /* Reserved */
909  avio_wb16(pb, 1); /* Data-reference index */
910 
911  avio_wb16(pb, 1); /* Hint track version */
912  avio_wb16(pb, 1); /* Highest compatible version */
913  avio_wb32(pb, track->max_packet_size); /* Max packet size */
914 
915  avio_wb32(pb, 12); /* size */
916  ffio_wfourcc(pb, "tims");
917  avio_wb32(pb, track->timescale);
918 
919  return updateSize(pb, pos);
920 }
921 
922 static int mov_write_stsd_tag(AVIOContext *pb, MOVTrack *track)
923 {
924  int64_t pos = avio_tell(pb);
925  avio_wb32(pb, 0); /* size */
926  ffio_wfourcc(pb, "stsd");
927  avio_wb32(pb, 0); /* version & flags */
928  avio_wb32(pb, 1); /* entry count */
929  if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO)
930  mov_write_video_tag(pb, track);
931  else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
932  mov_write_audio_tag(pb, track);
933  else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE)
934  mov_write_subtitle_tag(pb, track);
935  else if (track->enc->codec_tag == MKTAG('r','t','p',' '))
936  mov_write_rtp_tag(pb, track);
937  return updateSize(pb, pos);
938 }
939 
940 static int mov_write_ctts_tag(AVIOContext *pb, MOVTrack *track)
941 {
942  MOVStts *ctts_entries;
943  uint32_t entries = 0;
944  uint32_t atom_size;
945  int i;
946 
947  ctts_entries = av_malloc((track->entry + 1) * sizeof(*ctts_entries)); /* worst case */
948  ctts_entries[0].count = 1;
949  ctts_entries[0].duration = track->cluster[0].cts;
950  for (i=1; i<track->entry; i++) {
951  if (track->cluster[i].cts == ctts_entries[entries].duration) {
952  ctts_entries[entries].count++; /* compress */
953  } else {
954  entries++;
955  ctts_entries[entries].duration = track->cluster[i].cts;
956  ctts_entries[entries].count = 1;
957  }
958  }
959  entries++; /* last one */
960  atom_size = 16 + (entries * 8);
961  avio_wb32(pb, atom_size); /* size */
962  ffio_wfourcc(pb, "ctts");
963  avio_wb32(pb, 0); /* version & flags */
964  avio_wb32(pb, entries); /* entry count */
965  for (i=0; i<entries; i++) {
966  avio_wb32(pb, ctts_entries[i].count);
967  avio_wb32(pb, ctts_entries[i].duration);
968  }
969  av_free(ctts_entries);
970  return atom_size;
971 }
972 
973 /* Time to sample atom */
974 static int mov_write_stts_tag(AVIOContext *pb, MOVTrack *track)
975 {
976  MOVStts *stts_entries;
977  uint32_t entries = -1;
978  uint32_t atom_size;
979  int i;
980 
981  if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO && !track->audio_vbr) {
982  stts_entries = av_malloc(sizeof(*stts_entries)); /* one entry */
983  stts_entries[0].count = track->sampleCount;
984  stts_entries[0].duration = 1;
985  entries = 1;
986  } else {
987  stts_entries = av_malloc(track->entry * sizeof(*stts_entries)); /* worst case */
988  for (i=0; i<track->entry; i++) {
989  int64_t duration = i + 1 == track->entry ?
990  track->trackDuration - track->cluster[i].dts + track->cluster[0].dts : /* readjusting */
991  track->cluster[i+1].dts - track->cluster[i].dts;
992  if (i && duration == stts_entries[entries].duration) {
993  stts_entries[entries].count++; /* compress */
994  } else {
995  entries++;
996  stts_entries[entries].duration = duration;
997  stts_entries[entries].count = 1;
998  }
999  }
1000  entries++; /* last one */
1001  }
1002  atom_size = 16 + (entries * 8);
1003  avio_wb32(pb, atom_size); /* size */
1004  ffio_wfourcc(pb, "stts");
1005  avio_wb32(pb, 0); /* version & flags */
1006  avio_wb32(pb, entries); /* entry count */
1007  for (i=0; i<entries; i++) {
1008  avio_wb32(pb, stts_entries[i].count);
1009  avio_wb32(pb, stts_entries[i].duration);
1010  }
1011  av_free(stts_entries);
1012  return atom_size;
1013 }
1014 
1016 {
1017  avio_wb32(pb, 28); /* size */
1018  ffio_wfourcc(pb, "dref");
1019  avio_wb32(pb, 0); /* version & flags */
1020  avio_wb32(pb, 1); /* entry count */
1021 
1022  avio_wb32(pb, 0xc); /* size */
1023  ffio_wfourcc(pb, "url ");
1024  avio_wb32(pb, 1); /* version & flags */
1025 
1026  return 28;
1027 }
1028 
1029 static int mov_write_stbl_tag(AVIOContext *pb, MOVTrack *track)
1030 {
1031  int64_t pos = avio_tell(pb);
1032  avio_wb32(pb, 0); /* size */
1033  ffio_wfourcc(pb, "stbl");
1034  mov_write_stsd_tag(pb, track);
1035  mov_write_stts_tag(pb, track);
1036  if ((track->enc->codec_type == AVMEDIA_TYPE_VIDEO ||
1037  track->enc->codec_tag == MKTAG('r','t','p',' ')) &&
1038  track->hasKeyframes && track->hasKeyframes < track->entry)
1039  mov_write_stss_tag(pb, track, MOV_SYNC_SAMPLE);
1040  if (track->mode == MODE_MOV && track->flags & MOV_TRACK_STPS)
1042  if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO &&
1043  track->flags & MOV_TRACK_CTTS)
1044  mov_write_ctts_tag(pb, track);
1045  mov_write_stsc_tag(pb, track);
1046  mov_write_stsz_tag(pb, track);
1047  mov_write_stco_tag(pb, track);
1048  return updateSize(pb, pos);
1049 }
1050 
1052 {
1053  int64_t pos = avio_tell(pb);
1054  avio_wb32(pb, 0); /* size */
1055  ffio_wfourcc(pb, "dinf");
1056  mov_write_dref_tag(pb);
1057  return updateSize(pb, pos);
1058 }
1059 
1061 {
1062  avio_wb32(pb, 12);
1063  ffio_wfourcc(pb, "nmhd");
1064  avio_wb32(pb, 0);
1065  return 12;
1066 }
1067 
1069 {
1070  avio_wb32(pb, 0x20); /* size */
1071  ffio_wfourcc(pb, "gmhd");
1072  avio_wb32(pb, 0x18); /* gmin size */
1073  ffio_wfourcc(pb, "gmin");/* generic media info */
1074  avio_wb32(pb, 0); /* version & flags */
1075  avio_wb16(pb, 0x40); /* graphics mode = */
1076  avio_wb16(pb, 0x8000); /* opColor (r?) */
1077  avio_wb16(pb, 0x8000); /* opColor (g?) */
1078  avio_wb16(pb, 0x8000); /* opColor (b?) */
1079  avio_wb16(pb, 0); /* balance */
1080  avio_wb16(pb, 0); /* reserved */
1081  return 0x20;
1082 }
1083 
1085 {
1086  avio_wb32(pb, 16); /* size */
1087  ffio_wfourcc(pb, "smhd");
1088  avio_wb32(pb, 0); /* version & flags */
1089  avio_wb16(pb, 0); /* reserved (balance, normally = 0) */
1090  avio_wb16(pb, 0); /* reserved */
1091  return 16;
1092 }
1093 
1095 {
1096  avio_wb32(pb, 0x14); /* size (always 0x14) */
1097  ffio_wfourcc(pb, "vmhd");
1098  avio_wb32(pb, 0x01); /* version & flags */
1099  avio_wb64(pb, 0); /* reserved (graphics mode = copy) */
1100  return 0x14;
1101 }
1102 
1103 static int mov_write_hdlr_tag(AVIOContext *pb, MOVTrack *track)
1104 {
1105  const char *hdlr, *descr = NULL, *hdlr_type = NULL;
1106  int64_t pos = avio_tell(pb);
1107 
1108  if (!track) { /* no media --> data handler */
1109  hdlr = "dhlr";
1110  hdlr_type = "url ";
1111  descr = "DataHandler";
1112  } else {
1113  hdlr = (track->mode == MODE_MOV) ? "mhlr" : "\0\0\0\0";
1114  if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
1115  hdlr_type = "vide";
1116  descr = "VideoHandler";
1117  } else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO) {
1118  hdlr_type = "soun";
1119  descr = "SoundHandler";
1120  } else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE) {
1121  if (track->tag == MKTAG('t','x','3','g')) hdlr_type = "sbtl";
1122  else hdlr_type = "text";
1123  descr = "SubtitleHandler";
1124  } else if (track->enc->codec_tag == MKTAG('r','t','p',' ')) {
1125  hdlr_type = "hint";
1126  descr = "HintHandler";
1127  }
1128  }
1129 
1130  avio_wb32(pb, 0); /* size */
1131  ffio_wfourcc(pb, "hdlr");
1132  avio_wb32(pb, 0); /* Version & flags */
1133  avio_write(pb, hdlr, 4); /* handler */
1134  ffio_wfourcc(pb, hdlr_type); /* handler type */
1135  avio_wb32(pb ,0); /* reserved */
1136  avio_wb32(pb ,0); /* reserved */
1137  avio_wb32(pb ,0); /* reserved */
1138  if (!track || track->mode == MODE_MOV)
1139  avio_w8(pb, strlen(descr)); /* pascal string */
1140  avio_write(pb, descr, strlen(descr)); /* handler description */
1141  if (track && track->mode != MODE_MOV)
1142  avio_w8(pb, 0); /* c string */
1143  return updateSize(pb, pos);
1144 }
1145 
1147 {
1148  /* This atom must be present, but leaving the values at zero
1149  * seems harmless. */
1150  avio_wb32(pb, 28); /* size */
1151  ffio_wfourcc(pb, "hmhd");
1152  avio_wb32(pb, 0); /* version, flags */
1153  avio_wb16(pb, 0); /* maxPDUsize */
1154  avio_wb16(pb, 0); /* avgPDUsize */
1155  avio_wb32(pb, 0); /* maxbitrate */
1156  avio_wb32(pb, 0); /* avgbitrate */
1157  avio_wb32(pb, 0); /* reserved */
1158  return 28;
1159 }
1160 
1161 static int mov_write_minf_tag(AVIOContext *pb, MOVTrack *track)
1162 {
1163  int64_t pos = avio_tell(pb);
1164  avio_wb32(pb, 0); /* size */
1165  ffio_wfourcc(pb, "minf");
1166  if(track->enc->codec_type == AVMEDIA_TYPE_VIDEO)
1167  mov_write_vmhd_tag(pb);
1168  else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
1169  mov_write_smhd_tag(pb);
1170  else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE) {
1171  if (track->tag == MKTAG('t','e','x','t')) mov_write_gmhd_tag(pb);
1172  else mov_write_nmhd_tag(pb);
1173  } else if (track->tag == MKTAG('r','t','p',' ')) {
1174  mov_write_hmhd_tag(pb);
1175  }
1176  if (track->mode == MODE_MOV) /* FIXME: Why do it for MODE_MOV only ? */
1177  mov_write_hdlr_tag(pb, NULL);
1178  mov_write_dinf_tag(pb);
1179  mov_write_stbl_tag(pb, track);
1180  return updateSize(pb, pos);
1181 }
1182 
1183 static int mov_write_mdhd_tag(AVIOContext *pb, MOVTrack *track)
1184 {
1185  int version = track->trackDuration < INT32_MAX ? 0 : 1;
1186 
1187  (version == 1) ? avio_wb32(pb, 44) : avio_wb32(pb, 32); /* size */
1188  ffio_wfourcc(pb, "mdhd");
1189  avio_w8(pb, version);
1190  avio_wb24(pb, 0); /* flags */
1191  if (version == 1) {
1192  avio_wb64(pb, track->time);
1193  avio_wb64(pb, track->time);
1194  } else {
1195  avio_wb32(pb, track->time); /* creation time */
1196  avio_wb32(pb, track->time); /* modification time */
1197  }
1198  avio_wb32(pb, track->timescale); /* time scale (sample rate for audio) */
1199  (version == 1) ? avio_wb64(pb, track->trackDuration) : avio_wb32(pb, track->trackDuration); /* duration */
1200  avio_wb16(pb, track->language); /* language */
1201  avio_wb16(pb, 0); /* reserved (quality) */
1202 
1203  if(version!=0 && track->mode == MODE_MOV){
1205  "FATAL error, file duration too long for timebase, this file will not be\n"
1206  "playable with quicktime. Choose a different timebase or a different\n"
1207  "container format\n");
1208  }
1209 
1210  return 32;
1211 }
1212 
1213 static int mov_write_mdia_tag(AVIOContext *pb, MOVTrack *track)
1214 {
1215  int64_t pos = avio_tell(pb);
1216  avio_wb32(pb, 0); /* size */
1217  ffio_wfourcc(pb, "mdia");
1218  mov_write_mdhd_tag(pb, track);
1219  mov_write_hdlr_tag(pb, track);
1220  mov_write_minf_tag(pb, track);
1221  return updateSize(pb, pos);
1222 }
1223 
1224 static int mov_write_tkhd_tag(AVIOContext *pb, MOVTrack *track, AVStream *st)
1225 {
1227  track->timescale, AV_ROUND_UP);
1228  int version = duration < INT32_MAX ? 0 : 1;
1229 
1230  (version == 1) ? avio_wb32(pb, 104) : avio_wb32(pb, 92); /* size */
1231  ffio_wfourcc(pb, "tkhd");
1232  avio_w8(pb, version);
1233  avio_wb24(pb, 0xf); /* flags (track enabled) */
1234  if (version == 1) {
1235  avio_wb64(pb, track->time);
1236  avio_wb64(pb, track->time);
1237  } else {
1238  avio_wb32(pb, track->time); /* creation time */
1239  avio_wb32(pb, track->time); /* modification time */
1240  }
1241  avio_wb32(pb, track->trackID); /* track-id */
1242  avio_wb32(pb, 0); /* reserved */
1243  (version == 1) ? avio_wb64(pb, duration) : avio_wb32(pb, duration);
1244 
1245  avio_wb32(pb, 0); /* reserved */
1246  avio_wb32(pb, 0); /* reserved */
1247  avio_wb16(pb, 0); /* layer */
1248  avio_wb16(pb, st ? st->codec->codec_type : 0); /* alternate group) */
1249  /* Volume, only for audio */
1250  if(track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
1251  avio_wb16(pb, 0x0100);
1252  else
1253  avio_wb16(pb, 0);
1254  avio_wb16(pb, 0); /* reserved */
1255 
1256  /* Matrix structure */
1257  avio_wb32(pb, 0x00010000); /* reserved */
1258  avio_wb32(pb, 0x0); /* reserved */
1259  avio_wb32(pb, 0x0); /* reserved */
1260  avio_wb32(pb, 0x0); /* reserved */
1261  avio_wb32(pb, 0x00010000); /* reserved */
1262  avio_wb32(pb, 0x0); /* reserved */
1263  avio_wb32(pb, 0x0); /* reserved */
1264  avio_wb32(pb, 0x0); /* reserved */
1265  avio_wb32(pb, 0x40000000); /* reserved */
1266 
1267  /* Track width and height, for visual only */
1268  if(st && (track->enc->codec_type == AVMEDIA_TYPE_VIDEO ||
1269  track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE)) {
1270  if(track->mode == MODE_MOV) {
1271  avio_wb32(pb, track->enc->width << 16);
1272  avio_wb32(pb, track->height << 16);
1273  } else {
1274  double sample_aspect_ratio = av_q2d(st->sample_aspect_ratio);
1275  if(!sample_aspect_ratio || track->height != track->enc->height)
1276  sample_aspect_ratio = 1;
1277  avio_wb32(pb, sample_aspect_ratio * track->enc->width*0x10000);
1278  avio_wb32(pb, track->height*0x10000);
1279  }
1280  }
1281  else {
1282  avio_wb32(pb, 0);
1283  avio_wb32(pb, 0);
1284  }
1285  return 0x5c;
1286 }
1287 
1288 static int mov_write_tapt_tag(AVIOContext *pb, MOVTrack *track)
1289 {
1290  int32_t width = av_rescale(track->enc->sample_aspect_ratio.num, track->enc->width,
1291  track->enc->sample_aspect_ratio.den);
1292 
1293  int64_t pos = avio_tell(pb);
1294 
1295  avio_wb32(pb, 0); /* size */
1296  ffio_wfourcc(pb, "tapt");
1297 
1298  avio_wb32(pb, 20);
1299  ffio_wfourcc(pb, "clef");
1300  avio_wb32(pb, 0);
1301  avio_wb32(pb, width << 16);
1302  avio_wb32(pb, track->enc->height << 16);
1303 
1304  avio_wb32(pb, 20);
1305  ffio_wfourcc(pb, "enof");
1306  avio_wb32(pb, 0);
1307  avio_wb32(pb, track->enc->width << 16);
1308  avio_wb32(pb, track->enc->height << 16);
1309 
1310  return updateSize(pb, pos);
1311 }
1312 
1313 // This box seems important for the psp playback ... without it the movie seems to hang
1314 static int mov_write_edts_tag(AVIOContext *pb, MOVTrack *track)
1315 {
1317  track->timescale, AV_ROUND_UP);
1318  int version = duration < INT32_MAX ? 0 : 1;
1319  int entry_size, entry_count, size;
1320  int64_t delay, start_ct = track->cluster[0].cts;
1321  delay = av_rescale_rnd(track->cluster[0].dts + start_ct, MOV_TIMESCALE,
1322  track->timescale, AV_ROUND_DOWN);
1323  version |= delay < INT32_MAX ? 0 : 1;
1324 
1325  entry_size = (version == 1) ? 20 : 12;
1326  entry_count = 1 + (delay > 0);
1327  size = 24 + entry_count * entry_size;
1328 
1329  /* write the atom data */
1330  avio_wb32(pb, size);
1331  ffio_wfourcc(pb, "edts");
1332  avio_wb32(pb, size - 8);
1333  ffio_wfourcc(pb, "elst");
1334  avio_w8(pb, version);
1335  avio_wb24(pb, 0); /* flags */
1336 
1337  avio_wb32(pb, entry_count);
1338  if (delay > 0) { /* add an empty edit to delay presentation */
1339  if (version == 1) {
1340  avio_wb64(pb, delay);
1341  avio_wb64(pb, -1);
1342  } else {
1343  avio_wb32(pb, delay);
1344  avio_wb32(pb, -1);
1345  }
1346  avio_wb32(pb, 0x00010000);
1347  }
1348 
1349  /* duration */
1350  if (version == 1) {
1351  avio_wb64(pb, duration);
1352  avio_wb64(pb, start_ct);
1353  } else {
1354  avio_wb32(pb, duration);
1355  avio_wb32(pb, start_ct);
1356  }
1357  avio_wb32(pb, 0x00010000);
1358  return size;
1359 }
1360 
1361 static int mov_write_tref_tag(AVIOContext *pb, MOVTrack *track)
1362 {
1363  avio_wb32(pb, 20); // size
1364  ffio_wfourcc(pb, "tref");
1365  avio_wb32(pb, 12); // size (subatom)
1366  avio_wl32(pb, track->tref_tag);
1367  avio_wb32(pb, track->tref_id);
1368  return 20;
1369 }
1370 
1371 // goes at the end of each track! ... Critical for PSP playback ("Incompatible data" without it)
1373 {
1374  avio_wb32(pb, 0x34); /* size ... reports as 28 in mp4box! */
1375  ffio_wfourcc(pb, "uuid");
1376  ffio_wfourcc(pb, "USMT");
1377  avio_wb32(pb, 0x21d24fce);
1378  avio_wb32(pb, 0xbb88695c);
1379  avio_wb32(pb, 0xfac9c740);
1380  avio_wb32(pb, 0x1c); // another size here!
1381  ffio_wfourcc(pb, "MTDT");
1382  avio_wb32(pb, 0x00010012);
1383  avio_wb32(pb, 0x0a);
1384  avio_wb32(pb, 0x55c40000);
1385  avio_wb32(pb, 0x1);
1386  avio_wb32(pb, 0x0);
1387  return 0x34;
1388 }
1389 
1391 {
1392  char buf[1000] = "";
1393  int len;
1394 
1395  ff_sdp_write_media(buf, sizeof(buf), ctx->streams[0]->codec, NULL, NULL, 0, 0, ctx);
1396  av_strlcatf(buf, sizeof(buf), "a=control:streamid=%d\r\n", index);
1397  len = strlen(buf);
1398 
1399  avio_wb32(pb, len + 24);
1400  ffio_wfourcc(pb, "udta");
1401  avio_wb32(pb, len + 16);
1402  ffio_wfourcc(pb, "hnti");
1403  avio_wb32(pb, len + 8);
1404  ffio_wfourcc(pb, "sdp ");
1405  avio_write(pb, buf, len);
1406  return len + 24;
1407 }
1408 
1409 static int mov_write_trak_tag(AVIOContext *pb, MOVTrack *track, AVStream *st)
1410 {
1411  int64_t pos = avio_tell(pb);
1412  avio_wb32(pb, 0); /* size */
1413  ffio_wfourcc(pb, "trak");
1414  mov_write_tkhd_tag(pb, track, st);
1415  if (track->mode == MODE_PSP || track->flags & MOV_TRACK_CTTS || track->cluster[0].dts)
1416  mov_write_edts_tag(pb, track); // PSP Movies require edts box
1417  if (track->tref_tag)
1418  mov_write_tref_tag(pb, track);
1419  mov_write_mdia_tag(pb, track);
1420  if (track->mode == MODE_PSP)
1421  mov_write_uuid_tag_psp(pb,track); // PSP Movies require this uuid box
1422  if (track->tag == MKTAG('r','t','p',' '))
1423  mov_write_udta_sdp(pb, track->rtp_ctx, track->trackID);
1424  if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO && track->mode == MODE_MOV) {
1425  double sample_aspect_ratio = av_q2d(st->sample_aspect_ratio);
1426  if (0.0 != sample_aspect_ratio && 1.0 != sample_aspect_ratio)
1427  mov_write_tapt_tag(pb, track);
1428  };
1429  return updateSize(pb, pos);
1430 }
1431 
1433 {
1434  int i, has_audio = 0, has_video = 0;
1435  int64_t pos = avio_tell(pb);
1436  int audio_profile = mov->iods_audio_profile;
1437  int video_profile = mov->iods_video_profile;
1438  for (i = 0; i < mov->nb_streams; i++) {
1439  if(mov->tracks[i].entry > 0) {
1440  has_audio |= mov->tracks[i].enc->codec_type == AVMEDIA_TYPE_AUDIO;
1441  has_video |= mov->tracks[i].enc->codec_type == AVMEDIA_TYPE_VIDEO;
1442  }
1443  }
1444  if (audio_profile < 0)
1445  audio_profile = 0xFF - has_audio;
1446  if (video_profile < 0)
1447  video_profile = 0xFF - has_video;
1448  avio_wb32(pb, 0x0); /* size */
1449  ffio_wfourcc(pb, "iods");
1450  avio_wb32(pb, 0); /* version & flags */
1451  putDescr(pb, 0x10, 7);
1452  avio_wb16(pb, 0x004f);
1453  avio_w8(pb, 0xff);
1454  avio_w8(pb, 0xff);
1455  avio_w8(pb, audio_profile);
1456  avio_w8(pb, video_profile);
1457  avio_w8(pb, 0xff);
1458  return updateSize(pb, pos);
1459 }
1460 
1462 {
1463  int maxTrackID = 1, i;
1464  int64_t maxTrackLenTemp, maxTrackLen = 0;
1465  int version;
1466 
1467  for (i=0; i<mov->nb_streams; i++) {
1468  if(mov->tracks[i].entry > 0) {
1469  maxTrackLenTemp = av_rescale_rnd(mov->tracks[i].trackDuration,
1470  MOV_TIMESCALE,
1471  mov->tracks[i].timescale,
1472  AV_ROUND_UP);
1473  if(maxTrackLen < maxTrackLenTemp)
1474  maxTrackLen = maxTrackLenTemp;
1475  if(maxTrackID < mov->tracks[i].trackID)
1476  maxTrackID = mov->tracks[i].trackID;
1477  }
1478  }
1479 
1480  version = maxTrackLen < UINT32_MAX ? 0 : 1;
1481  (version == 1) ? avio_wb32(pb, 120) : avio_wb32(pb, 108); /* size */
1482  ffio_wfourcc(pb, "mvhd");
1483  avio_w8(pb, version);
1484  avio_wb24(pb, 0); /* flags */
1485  if (version == 1) {
1486  avio_wb64(pb, mov->time);
1487  avio_wb64(pb, mov->time);
1488  } else {
1489  avio_wb32(pb, mov->time); /* creation time */
1490  avio_wb32(pb, mov->time); /* modification time */
1491  }
1492  avio_wb32(pb, MOV_TIMESCALE);
1493  (version == 1) ? avio_wb64(pb, maxTrackLen) : avio_wb32(pb, maxTrackLen); /* duration of longest track */
1494 
1495  avio_wb32(pb, 0x00010000); /* reserved (preferred rate) 1.0 = normal */
1496  avio_wb16(pb, 0x0100); /* reserved (preferred volume) 1.0 = normal */
1497  avio_wb16(pb, 0); /* reserved */
1498  avio_wb32(pb, 0); /* reserved */
1499  avio_wb32(pb, 0); /* reserved */
1500 
1501  /* Matrix structure */
1502  avio_wb32(pb, 0x00010000); /* reserved */
1503  avio_wb32(pb, 0x0); /* reserved */
1504  avio_wb32(pb, 0x0); /* reserved */
1505  avio_wb32(pb, 0x0); /* reserved */
1506  avio_wb32(pb, 0x00010000); /* reserved */
1507  avio_wb32(pb, 0x0); /* reserved */
1508  avio_wb32(pb, 0x0); /* reserved */
1509  avio_wb32(pb, 0x0); /* reserved */
1510  avio_wb32(pb, 0x40000000); /* reserved */
1511 
1512  avio_wb32(pb, 0); /* reserved (preview time) */
1513  avio_wb32(pb, 0); /* reserved (preview duration) */
1514  avio_wb32(pb, 0); /* reserved (poster time) */
1515  avio_wb32(pb, 0); /* reserved (selection time) */
1516  avio_wb32(pb, 0); /* reserved (selection duration) */
1517  avio_wb32(pb, 0); /* reserved (current time) */
1518  avio_wb32(pb, maxTrackID+1); /* Next track id */
1519  return 0x6c;
1520 }
1521 
1523  AVFormatContext *s)
1524 {
1525  avio_wb32(pb, 33); /* size */
1526  ffio_wfourcc(pb, "hdlr");
1527  avio_wb32(pb, 0);
1528  avio_wb32(pb, 0);
1529  ffio_wfourcc(pb, "mdir");
1530  ffio_wfourcc(pb, "appl");
1531  avio_wb32(pb, 0);
1532  avio_wb32(pb, 0);
1533  avio_w8(pb, 0);
1534  return 33;
1535 }
1536 
1537 /* helper function to write a data tag with the specified string as data */
1538 static int mov_write_string_data_tag(AVIOContext *pb, const char *data, int lang, int long_style)
1539 {
1540  if(long_style){
1541  int size = 16 + strlen(data);
1542  avio_wb32(pb, size); /* size */
1543  ffio_wfourcc(pb, "data");
1544  avio_wb32(pb, 1);
1545  avio_wb32(pb, 0);
1546  avio_write(pb, data, strlen(data));
1547  return size;
1548  }else{
1549  if (!lang)
1550  lang = ff_mov_iso639_to_lang("und", 1);
1551  avio_wb16(pb, strlen(data)); /* string length */
1552  avio_wb16(pb, lang);
1553  avio_write(pb, data, strlen(data));
1554  return strlen(data) + 4;
1555  }
1556 }
1557 
1558 static int mov_write_string_tag(AVIOContext *pb, const char *name, const char *value, int lang, int long_style){
1559  int size = 0;
1560  if (value && value[0]) {
1561  int64_t pos = avio_tell(pb);
1562  avio_wb32(pb, 0); /* size */
1563  ffio_wfourcc(pb, name);
1564  mov_write_string_data_tag(pb, value, lang, long_style);
1565  size= updateSize(pb, pos);
1566  }
1567  return size;
1568 }
1569 
1571  const char *name, const char *tag,
1572  int long_style)
1573 {
1574  int l, lang = 0, len, len2;
1575  AVDictionaryEntry *t, *t2 = NULL;
1576  char tag2[16];
1577 
1578  if (!(t = av_dict_get(s->metadata, tag, NULL, 0)))
1579  return 0;
1580 
1581  len = strlen(t->key);
1582  snprintf(tag2, sizeof(tag2), "%s-", tag);
1583  while ((t2 = av_dict_get(s->metadata, tag2, t2, AV_DICT_IGNORE_SUFFIX))) {
1584  len2 = strlen(t2->key);
1585  if (len2 == len+4 && !strcmp(t->value, t2->value)
1586  && (l=ff_mov_iso639_to_lang(&t2->key[len2-3], 1)) >= 0) {
1587  lang = l;
1588  break;
1589  }
1590  }
1591  return mov_write_string_tag(pb, name, t->value, lang, long_style);
1592 }
1593 
1594 /* iTunes track number */
1596  AVFormatContext *s)
1597 {
1598  AVDictionaryEntry *t = av_dict_get(s->metadata, "track", NULL, 0);
1599  int size = 0, track = t ? atoi(t->value) : 0;
1600  if (track) {
1601  avio_wb32(pb, 32); /* size */
1602  ffio_wfourcc(pb, "trkn");
1603  avio_wb32(pb, 24); /* size */
1604  ffio_wfourcc(pb, "data");
1605  avio_wb32(pb, 0); // 8 bytes empty
1606  avio_wb32(pb, 0);
1607  avio_wb16(pb, 0); // empty
1608  avio_wb16(pb, track); // track number
1609  avio_wb16(pb, 0); // total track number
1610  avio_wb16(pb, 0); // empty
1611  size = 32;
1612  }
1613  return size;
1614 }
1615 
1616 /* iTunes meta data list */
1618  AVFormatContext *s)
1619 {
1620  int64_t pos = avio_tell(pb);
1621  avio_wb32(pb, 0); /* size */
1622  ffio_wfourcc(pb, "ilst");
1623  mov_write_string_metadata(s, pb, "\251nam", "title" , 1);
1624  mov_write_string_metadata(s, pb, "\251ART", "artist" , 1);
1625  mov_write_string_metadata(s, pb, "aART", "album_artist", 1);
1626  mov_write_string_metadata(s, pb, "\251wrt", "composer" , 1);
1627  mov_write_string_metadata(s, pb, "\251alb", "album" , 1);
1628  mov_write_string_metadata(s, pb, "\251day", "date" , 1);
1629  if (!mov_write_string_metadata(s, pb, "\251too", "encoding_tool", 1))
1630  mov_write_string_tag(pb, "\251too", LIBAVFORMAT_IDENT, 0, 1);
1631  mov_write_string_metadata(s, pb, "\251cmt", "comment" , 1);
1632  mov_write_string_metadata(s, pb, "\251gen", "genre" , 1);
1633  mov_write_string_metadata(s, pb, "\251cpy", "copyright", 1);
1634  mov_write_string_metadata(s, pb, "\251grp", "grouping" , 1);
1635  mov_write_string_metadata(s, pb, "\251lyr", "lyrics" , 1);
1636  mov_write_string_metadata(s, pb, "desc", "description",1);
1637  mov_write_string_metadata(s, pb, "ldes", "synopsis" , 1);
1638  mov_write_string_metadata(s, pb, "tvsh", "show" , 1);
1639  mov_write_string_metadata(s, pb, "tven", "episode_id",1);
1640  mov_write_string_metadata(s, pb, "tvnn", "network" , 1);
1641  mov_write_trkn_tag(pb, mov, s);
1642  return updateSize(pb, pos);
1643 }
1644 
1645 /* iTunes meta data tag */
1647  AVFormatContext *s)
1648 {
1649  int size = 0;
1650  int64_t pos = avio_tell(pb);
1651  avio_wb32(pb, 0); /* size */
1652  ffio_wfourcc(pb, "meta");
1653  avio_wb32(pb, 0);
1654  mov_write_itunes_hdlr_tag(pb, mov, s);
1655  mov_write_ilst_tag(pb, mov, s);
1656  size = updateSize(pb, pos);
1657  return size;
1658 }
1659 
1660 static int utf8len(const uint8_t *b)
1661 {
1662  int len=0;
1663  int val;
1664  while(*b){
1665  GET_UTF8(val, *b++, return -1;)
1666  len++;
1667  }
1668  return len;
1669 }
1670 
1671 static int ascii_to_wc(AVIOContext *pb, const uint8_t *b)
1672 {
1673  int val;
1674  while(*b){
1675  GET_UTF8(val, *b++, return -1;)
1676  avio_wb16(pb, val);
1677  }
1678  avio_wb16(pb, 0x00);
1679  return 0;
1680 }
1681 
1682 static uint16_t language_code(const char *str)
1683 {
1684  return (((str[0]-0x60) & 0x1F) << 10) + (((str[1]-0x60) & 0x1F) << 5) + ((str[2]-0x60) & 0x1F);
1685 }
1686 
1688  const char *tag, const char *str)
1689 {
1690  int64_t pos = avio_tell(pb);
1691  AVDictionaryEntry *t = av_dict_get(s->metadata, str, NULL, 0);
1692  if (!t || !utf8len(t->value))
1693  return 0;
1694  avio_wb32(pb, 0); /* size */
1695  ffio_wfourcc(pb, tag); /* type */
1696  avio_wb32(pb, 0); /* version + flags */
1697  if (!strcmp(tag, "yrrc"))
1698  avio_wb16(pb, atoi(t->value));
1699  else {
1700  avio_wb16(pb, language_code("eng")); /* language */
1701  avio_write(pb, t->value, strlen(t->value)+1); /* UTF8 string value */
1702  if (!strcmp(tag, "albm") &&
1703  (t = av_dict_get(s->metadata, "track", NULL, 0)))
1704  avio_w8(pb, atoi(t->value));
1705  }
1706  return updateSize(pb, pos);
1707 }
1708 
1710 {
1711  int64_t pos = avio_tell(pb);
1712  int i, nb_chapters = FFMIN(s->nb_chapters, 255);
1713 
1714  avio_wb32(pb, 0); // size
1715  ffio_wfourcc(pb, "chpl");
1716  avio_wb32(pb, 0x01000000); // version + flags
1717  avio_wb32(pb, 0); // unknown
1718  avio_w8(pb, nb_chapters);
1719 
1720  for (i = 0; i < nb_chapters; i++) {
1721  AVChapter *c = s->chapters[i];
1723  avio_wb64(pb, av_rescale_q(c->start, c->time_base, (AVRational){1,10000000}));
1724 
1725  if ((t = av_dict_get(c->metadata, "title", NULL, 0))) {
1726  int len = FFMIN(strlen(t->value), 255);
1727  avio_w8(pb, len);
1728  avio_write(pb, t->value, len);
1729  } else
1730  avio_w8(pb, 0);
1731  }
1732  return updateSize(pb, pos);
1733 }
1734 
1736  AVFormatContext *s)
1737 {
1738  AVIOContext *pb_buf;
1739  int i, ret, size;
1740  uint8_t *buf;
1741 
1742  for (i = 0; i < s->nb_streams; i++)
1743  if (mov->tracks[i].enc->flags & CODEC_FLAG_BITEXACT) {
1744  return 0;
1745  }
1746 
1747  ret = avio_open_dyn_buf(&pb_buf);
1748  if(ret < 0)
1749  return ret;
1750 
1751  if (mov->mode & MODE_3GP) {
1752  mov_write_3gp_udta_tag(pb_buf, s, "perf", "artist");
1753  mov_write_3gp_udta_tag(pb_buf, s, "titl", "title");
1754  mov_write_3gp_udta_tag(pb_buf, s, "auth", "author");
1755  mov_write_3gp_udta_tag(pb_buf, s, "gnre", "genre");
1756  mov_write_3gp_udta_tag(pb_buf, s, "dscp", "comment");
1757  mov_write_3gp_udta_tag(pb_buf, s, "albm", "album");
1758  mov_write_3gp_udta_tag(pb_buf, s, "cprt", "copyright");
1759  mov_write_3gp_udta_tag(pb_buf, s, "yrrc", "date");
1760  } else if (mov->mode == MODE_MOV) { // the title field breaks gtkpod with mp4 and my suspicion is that stuff is not valid in mp4
1761  mov_write_string_metadata(s, pb_buf, "\251ART", "artist" , 0);
1762  mov_write_string_metadata(s, pb_buf, "\251nam", "title" , 0);
1763  mov_write_string_metadata(s, pb_buf, "\251aut", "author" , 0);
1764  mov_write_string_metadata(s, pb_buf, "\251alb", "album" , 0);
1765  mov_write_string_metadata(s, pb_buf, "\251day", "date" , 0);
1766  mov_write_string_metadata(s, pb_buf, "\251swr", "encoder" , 0);
1767  mov_write_string_metadata(s, pb_buf, "\251des", "comment" , 0);
1768  mov_write_string_metadata(s, pb_buf, "\251gen", "genre" , 0);
1769  mov_write_string_metadata(s, pb_buf, "\251cpy", "copyright" , 0);
1770  } else {
1771  /* iTunes meta data */
1772  mov_write_meta_tag(pb_buf, mov, s);
1773  }
1774 
1775  if (s->nb_chapters)
1776  mov_write_chpl_tag(pb_buf, s);
1777 
1778  if ((size = avio_close_dyn_buf(pb_buf, &buf)) > 0) {
1779  avio_wb32(pb, size+8);
1780  ffio_wfourcc(pb, "udta");
1781  avio_write(pb, buf, size);
1782  }
1783  av_free(buf);
1784 
1785  return 0;
1786 }
1787 
1789  const char *str, const char *lang, int type)
1790 {
1791  int len = utf8len(str)+1;
1792  if(len<=0)
1793  return;
1794  avio_wb16(pb, len*2+10); /* size */
1795  avio_wb32(pb, type); /* type */
1796  avio_wb16(pb, language_code(lang)); /* language */
1797  avio_wb16(pb, 0x01); /* ? */
1798  ascii_to_wc(pb, str);
1799 }
1800 
1802 {
1803  AVDictionaryEntry *title = av_dict_get(s->metadata, "title", NULL, 0);
1804  int64_t pos, pos2;
1805 
1806  if (title) {
1807  pos = avio_tell(pb);
1808  avio_wb32(pb, 0); /* size placeholder*/
1809  ffio_wfourcc(pb, "uuid");
1810  ffio_wfourcc(pb, "USMT");
1811  avio_wb32(pb, 0x21d24fce); /* 96 bit UUID */
1812  avio_wb32(pb, 0xbb88695c);
1813  avio_wb32(pb, 0xfac9c740);
1814 
1815  pos2 = avio_tell(pb);
1816  avio_wb32(pb, 0); /* size placeholder*/
1817  ffio_wfourcc(pb, "MTDT");
1818  avio_wb16(pb, 4);
1819 
1820  // ?
1821  avio_wb16(pb, 0x0C); /* size */
1822  avio_wb32(pb, 0x0B); /* type */
1823  avio_wb16(pb, language_code("und")); /* language */
1824  avio_wb16(pb, 0x0); /* ? */
1825  avio_wb16(pb, 0x021C); /* data */
1826 
1827  mov_write_psp_udta_tag(pb, LIBAVCODEC_IDENT, "eng", 0x04);
1828  mov_write_psp_udta_tag(pb, title->value, "eng", 0x01);
1829 // 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);
1830  mov_write_psp_udta_tag(pb, "2006/04/01 11:11:11", "und", 0x03);
1831 
1832  updateSize(pb, pos2);
1833  return updateSize(pb, pos);
1834  }
1835 
1836  return 0;
1837 }
1838 
1840  AVFormatContext *s)
1841 {
1842  int i;
1843  int64_t pos = avio_tell(pb);
1844  avio_wb32(pb, 0); /* size placeholder*/
1845  ffio_wfourcc(pb, "moov");
1846 
1847  for (i=0; i<mov->nb_streams; i++) {
1848  if(mov->tracks[i].entry <= 0) continue;
1849 
1850  mov->tracks[i].time = mov->time;
1851  mov->tracks[i].trackID = i+1;
1852  }
1853 
1854  if (mov->chapter_track)
1855  for (i=0; i<s->nb_streams; i++) {
1856  mov->tracks[i].tref_tag = MKTAG('c','h','a','p');
1857  mov->tracks[i].tref_id = mov->tracks[mov->chapter_track].trackID;
1858  }
1859  for (i = 0; i < mov->nb_streams; i++) {
1860  if (mov->tracks[i].tag == MKTAG('r','t','p',' ')) {
1861  mov->tracks[i].tref_tag = MKTAG('h','i','n','t');
1862  mov->tracks[i].tref_id =
1863  mov->tracks[mov->tracks[i].src_track].trackID;
1864  }
1865  }
1866 
1867  mov_write_mvhd_tag(pb, mov);
1868  if (mov->mode != MODE_MOV && !mov->iods_skip)
1869  mov_write_iods_tag(pb, mov);
1870  for (i=0; i<mov->nb_streams; i++) {
1871  if(mov->tracks[i].entry > 0) {
1872  mov_write_trak_tag(pb, &(mov->tracks[i]), i < s->nb_streams ? s->streams[i] : NULL);
1873  }
1874  }
1875 
1876  if (mov->mode == MODE_PSP)
1877  mov_write_uuidusmt_tag(pb, s);
1878  else
1879  mov_write_udta_tag(pb, mov, s);
1880 
1881  return updateSize(pb, pos);
1882 }
1883 
1885 {
1886  avio_wb32(pb, 8); // placeholder for extended size field (64 bit)
1887  ffio_wfourcc(pb, mov->mode == MODE_MOV ? "wide" : "free");
1888 
1889  mov->mdat_pos = avio_tell(pb);
1890  avio_wb32(pb, 0); /* size placeholder*/
1891  ffio_wfourcc(pb, "mdat");
1892  return 0;
1893 }
1894 
1895 /* TODO: This needs to be more general */
1897 {
1898  MOVMuxContext *mov = s->priv_data;
1899  int64_t pos = avio_tell(pb);
1900  int has_h264 = 0, has_video = 0;
1901  int minor = 0x200;
1902  int i;
1903 
1904  for (i = 0; i < s->nb_streams; i++) {
1905  AVStream *st = s->streams[i];
1906  if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
1907  has_video = 1;
1908  if (st->codec->codec_id == CODEC_ID_H264)
1909  has_h264 = 1;
1910  }
1911 
1912  avio_wb32(pb, 0); /* size */
1913  ffio_wfourcc(pb, "ftyp");
1914 
1915  if (mov->mode == MODE_3GP) {
1916  ffio_wfourcc(pb, has_h264 ? "3gp6" : "3gp4");
1917  minor = has_h264 ? 0x100 : 0x200;
1918  } else if (mov->mode & MODE_3G2) {
1919  ffio_wfourcc(pb, has_h264 ? "3g2b" : "3g2a");
1920  minor = has_h264 ? 0x20000 : 0x10000;
1921  }else if (mov->mode == MODE_PSP)
1922  ffio_wfourcc(pb, "MSNV");
1923  else if (mov->mode == MODE_MP4)
1924  ffio_wfourcc(pb, "isom");
1925  else if (mov->mode == MODE_IPOD)
1926  ffio_wfourcc(pb, has_video ? "M4V ":"M4A ");
1927  else
1928  ffio_wfourcc(pb, "qt ");
1929 
1930  avio_wb32(pb, minor);
1931 
1932  if(mov->mode == MODE_MOV)
1933  ffio_wfourcc(pb, "qt ");
1934  else{
1935  ffio_wfourcc(pb, "isom");
1936  ffio_wfourcc(pb, "iso2");
1937  if(has_h264)
1938  ffio_wfourcc(pb, "avc1");
1939  }
1940 
1941  if (mov->mode == MODE_3GP)
1942  ffio_wfourcc(pb, has_h264 ? "3gp6":"3gp4");
1943  else if (mov->mode & MODE_3G2)
1944  ffio_wfourcc(pb, has_h264 ? "3g2b":"3g2a");
1945  else if (mov->mode == MODE_PSP)
1946  ffio_wfourcc(pb, "MSNV");
1947  else if (mov->mode == MODE_MP4)
1948  ffio_wfourcc(pb, "mp41");
1949  return updateSize(pb, pos);
1950 }
1951 
1953 {
1954  AVCodecContext *VideoCodec = s->streams[0]->codec;
1955  AVCodecContext *AudioCodec = s->streams[1]->codec;
1956  int AudioRate = AudioCodec->sample_rate;
1957  int FrameRate = ((VideoCodec->time_base.den) * (0x10000))/ (VideoCodec->time_base.num);
1958  int audio_kbitrate= AudioCodec->bit_rate / 1000;
1959  int video_kbitrate= FFMIN(VideoCodec->bit_rate / 1000, 800 - audio_kbitrate);
1960 
1961  avio_wb32(pb, 0x94); /* size */
1962  ffio_wfourcc(pb, "uuid");
1963  ffio_wfourcc(pb, "PROF");
1964 
1965  avio_wb32(pb, 0x21d24fce); /* 96 bit UUID */
1966  avio_wb32(pb, 0xbb88695c);
1967  avio_wb32(pb, 0xfac9c740);
1968 
1969  avio_wb32(pb, 0x0); /* ? */
1970  avio_wb32(pb, 0x3); /* 3 sections ? */
1971 
1972  avio_wb32(pb, 0x14); /* size */
1973  ffio_wfourcc(pb, "FPRF");
1974  avio_wb32(pb, 0x0); /* ? */
1975  avio_wb32(pb, 0x0); /* ? */
1976  avio_wb32(pb, 0x0); /* ? */
1977 
1978  avio_wb32(pb, 0x2c); /* size */
1979  ffio_wfourcc(pb, "APRF");/* audio */
1980  avio_wb32(pb, 0x0);
1981  avio_wb32(pb, 0x2); /* TrackID */
1982  ffio_wfourcc(pb, "mp4a");
1983  avio_wb32(pb, 0x20f);
1984  avio_wb32(pb, 0x0);
1985  avio_wb32(pb, audio_kbitrate);
1986  avio_wb32(pb, audio_kbitrate);
1987  avio_wb32(pb, AudioRate);
1988  avio_wb32(pb, AudioCodec->channels);
1989 
1990  avio_wb32(pb, 0x34); /* size */
1991  ffio_wfourcc(pb, "VPRF"); /* video */
1992  avio_wb32(pb, 0x0);
1993  avio_wb32(pb, 0x1); /* TrackID */
1994  if (VideoCodec->codec_id == CODEC_ID_H264) {
1995  ffio_wfourcc(pb, "avc1");
1996  avio_wb16(pb, 0x014D);
1997  avio_wb16(pb, 0x0015);
1998  } else {
1999  ffio_wfourcc(pb, "mp4v");
2000  avio_wb16(pb, 0x0000);
2001  avio_wb16(pb, 0x0103);
2002  }
2003  avio_wb32(pb, 0x0);
2004  avio_wb32(pb, video_kbitrate);
2005  avio_wb32(pb, video_kbitrate);
2006  avio_wb32(pb, FrameRate);
2007  avio_wb32(pb, FrameRate);
2008  avio_wb16(pb, VideoCodec->width);
2009  avio_wb16(pb, VideoCodec->height);
2010  avio_wb32(pb, 0x010001); /* ? */
2011 }
2012 
2013 static int mov_parse_mpeg2_frame(AVPacket *pkt, uint32_t *flags)
2014 {
2015  uint32_t c = -1;
2016  int i, closed_gop = 0;
2017 
2018  for (i = 0; i < pkt->size - 4; i++) {
2019  c = (c<<8) + pkt->data[i];
2020  if (c == 0x1b8) { // gop
2021  closed_gop = pkt->data[i+4]>>6 & 0x01;
2022  } else if (c == 0x100) { // pic
2023  int temp_ref = (pkt->data[i+1]<<2) | (pkt->data[i+2]>>6);
2024  if (!temp_ref || closed_gop) // I picture is not reordered
2025  *flags = MOV_SYNC_SAMPLE;
2026  else
2027  *flags = MOV_PARTIAL_SYNC_SAMPLE;
2028  break;
2029  }
2030  }
2031  return 0;
2032 }
2033 
2035 {
2036  MOVMuxContext *mov = s->priv_data;
2037  AVIOContext *pb = s->pb;
2038  MOVTrack *trk = &mov->tracks[pkt->stream_index];
2039  AVCodecContext *enc = trk->enc;
2040  unsigned int samplesInChunk = 0;
2041  int size= pkt->size;
2042  uint8_t *reformatted_data = NULL;
2043 
2044  if (!s->pb->seekable) return 0; /* Can't handle that */
2045  if (!size) return 0; /* Discard 0 sized packets */
2046 
2047  if (enc->codec_id == CODEC_ID_AMR_NB) {
2048  /* We must find out how many AMR blocks there are in one packet */
2049  static uint16_t packed_size[16] =
2050  {13, 14, 16, 18, 20, 21, 27, 32, 6, 0, 0, 0, 0, 0, 0, 1};
2051  int len = 0;
2052 
2053  while (len < size && samplesInChunk < 100) {
2054  len += packed_size[(pkt->data[len] >> 3) & 0x0F];
2055  samplesInChunk++;
2056  }
2057  if(samplesInChunk > 1){
2058  av_log(s, AV_LOG_ERROR, "fatal error, input is not a single packet, implement a AVParser for it\n");
2059  return -1;
2060  }
2061  } else if (trk->sampleSize)
2062  samplesInChunk = size/trk->sampleSize;
2063  else
2064  samplesInChunk = 1;
2065 
2066  /* copy extradata if it exists */
2067  if (trk->vosLen == 0 && enc->extradata_size > 0) {
2068  trk->vosLen = enc->extradata_size;
2069  trk->vosData = av_malloc(trk->vosLen);
2070  memcpy(trk->vosData, enc->extradata, trk->vosLen);
2071  }
2072 
2073  if (enc->codec_id == CODEC_ID_H264 && trk->vosLen > 0 && *(uint8_t *)trk->vosData != 1) {
2074  /* from x264 or from bytestream h264 */
2075  /* nal reformating needed */
2076  if (trk->hint_track >= 0 && trk->hint_track < mov->nb_streams) {
2077  ff_avc_parse_nal_units_buf(pkt->data, &reformatted_data,
2078  &size);
2079  avio_write(pb, reformatted_data, size);
2080  } else {
2081  size = ff_avc_parse_nal_units(pb, pkt->data, pkt->size);
2082  }
2083  } else {
2084  avio_write(pb, pkt->data, size);
2085  }
2086 
2087  if ((enc->codec_id == CODEC_ID_DNXHD ||
2088  enc->codec_id == CODEC_ID_AC3) && !trk->vosLen) {
2089  /* copy frame to create needed atoms */
2090  trk->vosLen = size;
2091  trk->vosData = av_malloc(size);
2092  if (!trk->vosData)
2093  return AVERROR(ENOMEM);
2094  memcpy(trk->vosData, pkt->data, size);
2095  }
2096 
2097  if (!(trk->entry % MOV_INDEX_CLUSTER_SIZE)) {
2098  trk->cluster = av_realloc(trk->cluster, (trk->entry + MOV_INDEX_CLUSTER_SIZE) * sizeof(*trk->cluster));
2099  if (!trk->cluster)
2100  return -1;
2101  }
2102 
2103  trk->cluster[trk->entry].pos = avio_tell(pb) - size;
2104  trk->cluster[trk->entry].samplesInChunk = samplesInChunk;
2105  trk->cluster[trk->entry].size = size;
2106  trk->cluster[trk->entry].entries = samplesInChunk;
2107  trk->cluster[trk->entry].dts = pkt->dts;
2108  trk->trackDuration = pkt->dts - trk->cluster[0].dts + pkt->duration;
2109 
2110  if (pkt->pts == AV_NOPTS_VALUE) {
2111  av_log(s, AV_LOG_WARNING, "pts has no value\n");
2112  pkt->pts = pkt->dts;
2113  }
2114  if (pkt->dts != pkt->pts)
2115  trk->flags |= MOV_TRACK_CTTS;
2116  trk->cluster[trk->entry].cts = pkt->pts - pkt->dts;
2117  trk->cluster[trk->entry].flags = 0;
2118  if (pkt->flags & AV_PKT_FLAG_KEY) {
2119  if (mov->mode == MODE_MOV && enc->codec_id == CODEC_ID_MPEG2VIDEO &&
2120  trk->entry > 0) { // force sync sample for the first key frame
2121  mov_parse_mpeg2_frame(pkt, &trk->cluster[trk->entry].flags);
2122  if (trk->cluster[trk->entry].flags & MOV_PARTIAL_SYNC_SAMPLE)
2123  trk->flags |= MOV_TRACK_STPS;
2124  } else {
2125  trk->cluster[trk->entry].flags = MOV_SYNC_SAMPLE;
2126  }
2127  if (trk->cluster[trk->entry].flags & MOV_SYNC_SAMPLE)
2128  trk->hasKeyframes++;
2129  }
2130  trk->entry++;
2131  trk->sampleCount += samplesInChunk;
2132  mov->mdat_size += size;
2133 
2134  avio_flush(pb);
2135 
2136  if (trk->hint_track >= 0 && trk->hint_track < mov->nb_streams)
2137  ff_mov_add_hinted_packet(s, pkt, trk->hint_track, trk->entry,
2138  reformatted_data, size);
2139  av_free(reformatted_data);
2140  return 0;
2141 }
2142 
2143 // QuickTime chapters involve an additional text track with the chapter names
2144 // as samples, and a tref pointing from the other tracks to the chapter one.
2145 static void mov_create_chapter_track(AVFormatContext *s, int tracknum)
2146 {
2147  MOVMuxContext *mov = s->priv_data;
2148  MOVTrack *track = &mov->tracks[tracknum];
2149  AVPacket pkt = { .stream_index = tracknum, .flags = AV_PKT_FLAG_KEY };
2150  int i, len;
2151 
2152  track->mode = mov->mode;
2153  track->tag = MKTAG('t','e','x','t');
2154  track->timescale = MOV_TIMESCALE;
2155  track->enc = avcodec_alloc_context3(NULL);
2157 
2158  for (i = 0; i < s->nb_chapters; i++) {
2159  AVChapter *c = s->chapters[i];
2161 
2162  int64_t end = av_rescale_q(c->end, c->time_base, (AVRational){1,MOV_TIMESCALE});
2163  pkt.pts = pkt.dts = av_rescale_q(c->start, c->time_base, (AVRational){1,MOV_TIMESCALE});
2164  pkt.duration = end - pkt.dts;
2165 
2166  if ((t = av_dict_get(c->metadata, "title", NULL, 0))) {
2167  len = strlen(t->value);
2168  pkt.size = len+2;
2169  pkt.data = av_malloc(pkt.size);
2170  AV_WB16(pkt.data, len);
2171  memcpy(pkt.data+2, t->value, len);
2172  ff_mov_write_packet(s, &pkt);
2173  av_freep(&pkt.data);
2174  }
2175  }
2176 }
2177 
2179 {
2180  AVIOContext *pb = s->pb;
2181  MOVMuxContext *mov = s->priv_data;
2183  int i, hint_track = 0;
2184 
2185  if (!s->pb->seekable) {
2186  av_log(s, AV_LOG_ERROR, "muxer does not support non seekable output\n");
2187  return -1;
2188  }
2189 
2190  /* Default mode == MP4 */
2191  mov->mode = MODE_MP4;
2192 
2193  if (s->oformat != NULL) {
2194  if (!strcmp("3gp", s->oformat->name)) mov->mode = MODE_3GP;
2195  else if (!strcmp("3g2", s->oformat->name)) mov->mode = MODE_3GP|MODE_3G2;
2196  else if (!strcmp("mov", s->oformat->name)) mov->mode = MODE_MOV;
2197  else if (!strcmp("psp", s->oformat->name)) mov->mode = MODE_PSP;
2198  else if (!strcmp("ipod",s->oformat->name)) mov->mode = MODE_IPOD;
2199 
2200  mov_write_ftyp_tag(pb,s);
2201  if (mov->mode == MODE_PSP) {
2202  if (s->nb_streams != 2) {
2203  av_log(s, AV_LOG_ERROR, "PSP mode need one video and one audio stream\n");
2204  return -1;
2205  }
2206  mov_write_uuidprof_tag(pb,s);
2207  }
2208  }
2209 
2210  mov->nb_streams = s->nb_streams;
2211  if (mov->mode & (MODE_MOV|MODE_IPOD) && s->nb_chapters)
2212  mov->chapter_track = mov->nb_streams++;
2213 
2214 #if FF_API_FLAG_RTP_HINT
2215  if (s->flags & AVFMT_FLAG_RTP_HINT) {
2216  av_log(s, AV_LOG_WARNING, "The RTP_HINT flag is deprecated, enable it "
2217  "via the -movflags rtphint muxer option "
2218  "instead.\n");
2219  mov->flags |= FF_MOV_FLAG_RTP_HINT;
2220  }
2221 #endif
2222  if (mov->flags & FF_MOV_FLAG_RTP_HINT) {
2223  /* Add hint tracks for each audio and video stream */
2224  hint_track = mov->nb_streams;
2225  for (i = 0; i < s->nb_streams; i++) {
2226  AVStream *st = s->streams[i];
2227  if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO ||
2229  mov->nb_streams++;
2230  }
2231  }
2232  }
2233 
2234  mov->tracks = av_mallocz(mov->nb_streams*sizeof(*mov->tracks));
2235  if (!mov->tracks)
2236  return AVERROR(ENOMEM);
2237 
2238  for(i=0; i<s->nb_streams; i++){
2239  AVStream *st= s->streams[i];
2240  MOVTrack *track= &mov->tracks[i];
2241  AVDictionaryEntry *lang = av_dict_get(st->metadata, "language", NULL,0);
2242 
2243  track->enc = st->codec;
2244  track->language = ff_mov_iso639_to_lang(lang?lang->value:"und", mov->mode!=MODE_MOV);
2245  if (track->language < 0)
2246  track->language = 0;
2247  track->mode = mov->mode;
2248  track->tag = mov_find_codec_tag(s, track);
2249  if (!track->tag) {
2250  av_log(s, AV_LOG_ERROR, "track %d: could not find tag, "
2251  "codec not currently supported in container\n", i);
2252  goto error;
2253  }
2254  /* If hinting of this track is enabled by a later hint track,
2255  * this is updated. */
2256  track->hint_track = -1;
2257  if(st->codec->codec_type == AVMEDIA_TYPE_VIDEO){
2258  if (track->tag == MKTAG('m','x','3','p') || track->tag == MKTAG('m','x','3','n') ||
2259  track->tag == MKTAG('m','x','4','p') || track->tag == MKTAG('m','x','4','n') ||
2260  track->tag == MKTAG('m','x','5','p') || track->tag == MKTAG('m','x','5','n')) {
2261  if (st->codec->width != 720 || (st->codec->height != 608 && st->codec->height != 512)) {
2262  av_log(s, AV_LOG_ERROR, "D-10/IMX must use 720x608 or 720x512 video resolution\n");
2263  goto error;
2264  }
2265  track->height = track->tag>>24 == 'n' ? 486 : 576;
2266  }
2267  track->timescale = st->codec->time_base.den;
2268  if (track->mode == MODE_MOV && track->timescale > 100000)
2270  "WARNING codec timebase is very high. If duration is too long,\n"
2271  "file may not be playable by quicktime. Specify a shorter timebase\n"
2272  "or choose different container.\n");
2273  }else if(st->codec->codec_type == AVMEDIA_TYPE_AUDIO){
2274  track->timescale = st->codec->sample_rate;
2275  /* set sampleSize for PCM and ADPCM */
2277  if (!st->codec->block_align) {
2278  av_log(s, AV_LOG_ERROR, "track %d: codec block align is not set\n", i);
2279  goto error;
2280  }
2281  track->sampleSize = st->codec->block_align;
2282  }
2283  /* set audio_vbr for compressed audio */
2284  if (av_get_bits_per_sample(st->codec->codec_id) < 8) {
2285  if (!st->codec->frame_size) {
2286  av_log(s, AV_LOG_ERROR, "track %d: codec frame size is not set\n", i);
2287  goto error;
2288  }
2289  track->audio_vbr = 1;
2290  }
2291  if (track->mode != MODE_MOV) {
2292  if (track->timescale > UINT16_MAX) {
2293  av_log(s, AV_LOG_ERROR, "track %d: output format does not support "
2294  "sample rate %dhz\n", i, track->timescale);
2295  goto error;
2296  }
2297  if (track->enc->codec_id == CODEC_ID_MP3 && track->timescale < 16000) {
2298  av_log(s, AV_LOG_ERROR, "track %d: muxing mp3 at %dhz is not supported\n",
2299  i, track->enc->sample_rate);
2300  goto error;
2301  }
2302  }
2303  }else if(st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE){
2304  track->timescale = st->codec->time_base.den;
2305  }
2306  if (!track->height)
2307  track->height = st->codec->height;
2308 
2309  avpriv_set_pts_info(st, 64, 1, track->timescale);
2310  }
2311 
2312  mov_write_mdat_tag(pb, mov);
2313 
2314 #if FF_API_TIMESTAMP
2315  if (s->timestamp)
2316  mov->time = s->timestamp;
2317  else
2318 #endif
2319  if (t = av_dict_get(s->metadata, "creation_time", NULL, 0))
2320  mov->time = ff_iso8601_to_unix_time(t->value);
2321  if (mov->time)
2322  mov->time += 0x7C25B080; // 1970 based -> 1904 based
2323 
2324  if (mov->chapter_track)
2326 
2327  if (mov->flags & FF_MOV_FLAG_RTP_HINT) {
2328  /* Initialize the hint tracks for each audio and video stream */
2329  for (i = 0; i < s->nb_streams; i++) {
2330  AVStream *st = s->streams[i];
2331  if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO ||
2333  ff_mov_init_hinting(s, hint_track, i);
2334  hint_track++;
2335  }
2336  }
2337  }
2338 
2339  avio_flush(pb);
2340 
2341  return 0;
2342  error:
2343  av_freep(&mov->tracks);
2344  return -1;
2345 }
2346 
2348 {
2349  MOVMuxContext *mov = s->priv_data;
2350  AVIOContext *pb = s->pb;
2351  int res = 0;
2352  int i;
2353 
2354  int64_t moov_pos = avio_tell(pb);
2355 
2356  /* Write size of mdat tag */
2357  if (mov->mdat_size+8 <= UINT32_MAX) {
2358  avio_seek(pb, mov->mdat_pos, SEEK_SET);
2359  avio_wb32(pb, mov->mdat_size+8);
2360  } else {
2361  /* overwrite 'wide' placeholder atom */
2362  avio_seek(pb, mov->mdat_pos - 8, SEEK_SET);
2363  avio_wb32(pb, 1); /* special value: real atom size will be 64 bit value after tag field */
2364  ffio_wfourcc(pb, "mdat");
2365  avio_wb64(pb, mov->mdat_size+16);
2366  }
2367  avio_seek(pb, moov_pos, SEEK_SET);
2368 
2369  mov_write_moov_tag(pb, mov, s);
2370 
2371  if (mov->chapter_track)
2372  av_freep(&mov->tracks[mov->chapter_track].enc);
2373 
2374  for (i=0; i<mov->nb_streams; i++) {
2375  if (mov->tracks[i].tag == MKTAG('r','t','p',' '))
2376  ff_mov_close_hinting(&mov->tracks[i]);
2377  av_freep(&mov->tracks[i].cluster);
2378 
2379  if(mov->tracks[i].vosLen) av_free(mov->tracks[i].vosData);
2380 
2381  }
2382 
2383  avio_flush(pb);
2384 
2385  av_freep(&mov->tracks);
2386 
2387  return res;
2388 }
2389 
2390 #if CONFIG_MOV_MUXER
2391 MOV_CLASS(mov)
2392 AVOutputFormat ff_mov_muxer = {
2393  .name = "mov",
2394  .long_name = NULL_IF_CONFIG_SMALL("MOV format"),
2395  .extensions = "mov",
2396  .priv_data_size = sizeof(MOVMuxContext),
2397  .audio_codec = CODEC_ID_AAC,
2399  .video_codec = CODEC_ID_H264,
2400 #else
2401  .video_codec = CODEC_ID_MPEG4,
2402 #endif
2407  .codec_tag = (const AVCodecTag* const []){codec_movvideo_tags, codec_movaudio_tags, 0},
2408  .priv_class = &mov_muxer_class,
2409 };
2410 #endif
2411 #if CONFIG_TGP_MUXER
2412 MOV_CLASS(tgp)
2413 AVOutputFormat ff_tgp_muxer = {
2414  .name = "3gp",
2415  .long_name = NULL_IF_CONFIG_SMALL("3GP format"),
2416  .extensions = "3gp",
2417  .priv_data_size = sizeof(MOVMuxContext),
2418  .audio_codec = CODEC_ID_AMR_NB,
2419  .video_codec = CODEC_ID_H263,
2424  .codec_tag = (const AVCodecTag* const []){codec_3gp_tags, 0},
2425  .priv_class = &tgp_muxer_class,
2426 };
2427 #endif
2428 #if CONFIG_MP4_MUXER
2429 MOV_CLASS(mp4)
2430 AVOutputFormat ff_mp4_muxer = {
2431  .name = "mp4",
2432  .long_name = NULL_IF_CONFIG_SMALL("MP4 format"),
2433  .mime_type = "application/mp4",
2434  .extensions = "mp4",
2435  .priv_data_size = sizeof(MOVMuxContext),
2436  .audio_codec = CODEC_ID_AAC,
2438  .video_codec = CODEC_ID_H264,
2439 #else
2440  .video_codec = CODEC_ID_MPEG4,
2441 #endif
2446  .codec_tag = (const AVCodecTag* const []){ff_mp4_obj_type, 0},
2447  .priv_class = &mp4_muxer_class,
2448 };
2449 #endif
2450 #if CONFIG_PSP_MUXER
2451 MOV_CLASS(psp)
2452 AVOutputFormat ff_psp_muxer = {
2453  .name = "psp",
2454  .long_name = NULL_IF_CONFIG_SMALL("PSP MP4 format"),
2455  .extensions = "mp4,psp",
2456  .priv_data_size = sizeof(MOVMuxContext),
2457  .audio_codec = CODEC_ID_AAC,
2459  .video_codec = CODEC_ID_H264,
2460 #else
2461  .video_codec = CODEC_ID_MPEG4,
2462 #endif
2467  .codec_tag = (const AVCodecTag* const []){ff_mp4_obj_type, 0},
2468  .priv_class = &psp_muxer_class,
2469 };
2470 #endif
2471 #if CONFIG_TG2_MUXER
2472 MOV_CLASS(tg2)
2473 AVOutputFormat ff_tg2_muxer = {
2474  .name = "3g2",
2475  .long_name = NULL_IF_CONFIG_SMALL("3GP2 format"),
2476  .extensions = "3g2",
2477  .priv_data_size = sizeof(MOVMuxContext),
2478  .audio_codec = CODEC_ID_AMR_NB,
2479  .video_codec = CODEC_ID_H263,
2484  .codec_tag = (const AVCodecTag* const []){codec_3gp_tags, 0},
2485  .priv_class = &tg2_muxer_class,
2486 };
2487 #endif
2488 #if CONFIG_IPOD_MUXER
2489 MOV_CLASS(ipod)
2490 AVOutputFormat ff_ipod_muxer = {
2491  .name = "ipod",
2492  .long_name = NULL_IF_CONFIG_SMALL("iPod H.264 MP4 format"),
2493  .mime_type = "application/mp4",
2494  .extensions = "m4v,m4a",
2495  .priv_data_size = sizeof(MOVMuxContext),
2496  .audio_codec = CODEC_ID_AAC,
2497  .video_codec = CODEC_ID_H264,
2502  .codec_tag = (const AVCodecTag* const []){codec_ipod_tags, 0},
2503  .priv_class = &ipod_muxer_class,
2504 };
2505 #endif