Liba52 can be compiled to use only integers and FFmpeg can handle the
[libav.git] / libavformat / movenc.c
CommitLineData
1cb5f7fd
MN
1/*
2 * MOV, 3GP, MP4 encoder.
3 * Copyright (c) 2003 Thomas Raivio.
69dde1ad 4 * Copyright (c) 2004 Gildas Bazin <gbazin at videolan dot org>.
1cb5f7fd
MN
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20#include "avformat.h"
f578f938 21#include "avi.h"
1cb5f7fd 22#include "avio.h"
1cb5f7fd 23
6e6d6dc0
MN
24#undef NDEBUG
25#include <assert.h>
26
1cb5f7fd
MN
27#define MOV_INDEX_CLUSTER_SIZE 16384
28#define globalTimescale 1000
29
69dde1ad
GB
30#define MODE_MP4 0
31#define MODE_MOV 1
32#define MODE_3GP 2
33
1cb5f7fd 34typedef struct MOVIentry {
e45ccf79
GB
35 unsigned int flags, pos, size;
36 unsigned int samplesInChunk;
f578f938 37 char key_frame;
1cb5f7fd
MN
38 unsigned int entries;
39} MOVIentry;
40
41typedef struct MOVIndex {
69dde1ad 42 int mode;
1cb5f7fd 43 int entry;
1cb5f7fd 44 int mdat_size;
1cb5f7fd
MN
45 int ents_allocated;
46 long timescale;
47 long time;
f578f938 48 long trackDuration;
e45ccf79
GB
49 long sampleCount;
50 long sampleDuration;
f578f938 51 int hasKeyframes;
1cb5f7fd
MN
52 int trackID;
53 AVCodecContext *enc;
54
55 int vosLen;
6e6d6dc0 56 uint8_t *vosData;
1cb5f7fd
MN
57 MOVIentry** cluster;
58} MOVTrack;
59
60typedef struct {
69dde1ad 61 int mode;
1cb5f7fd
MN
62 long time;
63 int nb_streams;
f578f938
TR
64 int mdat_written;
65 offset_t mdat_pos;
1cb5f7fd
MN
66 long timescale;
67 MOVTrack tracks[MAX_STREAMS];
68} MOVContext;
69
e45ccf79
GB
70static int mov_write_esds_tag(ByteIOContext *pb, MOVTrack* track);
71
6e6d6dc0
MN
72//FIXME supprt 64bit varaint with wide placeholders
73static int updateSize (ByteIOContext *pb, int pos)
1cb5f7fd
MN
74{
75 long curpos = url_ftell(pb);
76 url_fseek(pb, pos, SEEK_SET);
6e6d6dc0 77 put_be32(pb, curpos - pos); /* rewrite size */
1cb5f7fd 78 url_fseek(pb, curpos, SEEK_SET);
6e6d6dc0
MN
79
80 return curpos - pos;
1cb5f7fd
MN
81}
82
e45ccf79 83/* Chunk offset atom */
6e6d6dc0 84static int mov_write_stco_tag(ByteIOContext *pb, MOVTrack* track)
1cb5f7fd
MN
85{
86 int i;
f578f938
TR
87 int pos = url_ftell(pb);
88 put_be32(pb, 0); /* size */
1cb5f7fd
MN
89 put_tag(pb, "stco");
90 put_be32(pb, 0); /* version & flags */
91 put_be32(pb, track->entry); /* entry count */
92 for (i=0; i<track->entry; i++) {
93 int cl = i / MOV_INDEX_CLUSTER_SIZE;
94 int id = i % MOV_INDEX_CLUSTER_SIZE;
95 put_be32(pb, track->cluster[cl][id].pos);
96 }
f578f938 97 return updateSize (pb, pos);
1cb5f7fd
MN
98}
99
e45ccf79 100/* Sample size atom */
6e6d6dc0 101static int mov_write_stsz_tag(ByteIOContext *pb, MOVTrack* track)
1cb5f7fd 102{
f578f938 103 int equalChunks = 1;
e45ccf79 104 int i, j, entries = 0, tst = -1, oldtst = -1;
1cb5f7fd 105
f578f938
TR
106 int pos = url_ftell(pb);
107 put_be32(pb, 0); /* size */
1cb5f7fd
MN
108 put_tag(pb, "stsz");
109 put_be32(pb, 0); /* version & flags */
110
f578f938
TR
111 for (i=0; i<track->entry; i++) {
112 int cl = i / MOV_INDEX_CLUSTER_SIZE;
113 int id = i % MOV_INDEX_CLUSTER_SIZE;
e45ccf79
GB
114 tst = track->cluster[cl][id].size/track->cluster[cl][id].entries;
115 if(oldtst != -1 && tst != oldtst) {
116 equalChunks = 0;
f578f938
TR
117 }
118 oldtst = tst;
e45ccf79 119 entries += track->cluster[cl][id].entries;
f578f938 120 }
e45ccf79
GB
121 if (equalChunks) {
122 int sSize = track->cluster[0][0].size/track->cluster[0][0].entries;
f578f938 123 put_be32(pb, sSize); // sample size
e45ccf79 124 put_be32(pb, entries); // sample count
1cb5f7fd 125 }
f578f938
TR
126 else {
127 put_be32(pb, 0); // sample size
e45ccf79 128 put_be32(pb, entries); // sample count
f578f938 129 for (i=0; i<track->entry; i++) {
1cb5f7fd
MN
130 int cl = i / MOV_INDEX_CLUSTER_SIZE;
131 int id = i % MOV_INDEX_CLUSTER_SIZE;
e45ccf79
GB
132 for ( j=0; j<track->cluster[cl][id].entries; j++) {
133 put_be32(pb, track->cluster[cl][id].size /
134 track->cluster[cl][id].entries);
135 }
1cb5f7fd
MN
136 }
137 }
f578f938 138 return updateSize (pb, pos);
1cb5f7fd
MN
139}
140
e45ccf79 141/* Sample to chunk atom */
6e6d6dc0 142static int mov_write_stsc_tag(ByteIOContext *pb, MOVTrack* track)
1cb5f7fd 143{
f578f938
TR
144 int index = 0, oldval = -1, i, entryPos, curpos;
145
146 int pos = url_ftell(pb);
147 put_be32(pb, 0); /* size */
1cb5f7fd
MN
148 put_tag(pb, "stsc");
149 put_be32(pb, 0); // version & flags
f578f938
TR
150 entryPos = url_ftell(pb);
151 put_be32(pb, track->entry); // entry count
152 for (i=0; i<track->entry; i++) {
153 int cl = i / MOV_INDEX_CLUSTER_SIZE;
154 int id = i % MOV_INDEX_CLUSTER_SIZE;
e45ccf79 155 if(oldval != track->cluster[cl][id].samplesInChunk)
f578f938 156 {
1cb5f7fd 157 put_be32(pb, i+1); // first chunk
e45ccf79 158 put_be32(pb, track->cluster[cl][id].samplesInChunk); // samples per chunk
1cb5f7fd 159 put_be32(pb, 0x1); // sample description index
e45ccf79 160 oldval = track->cluster[cl][id].samplesInChunk;
f578f938 161 index++;
1cb5f7fd
MN
162 }
163 }
f578f938
TR
164 curpos = url_ftell(pb);
165 url_fseek(pb, entryPos, SEEK_SET);
166 put_be32(pb, index); // rewrite size
167 url_fseek(pb, curpos, SEEK_SET);
1cb5f7fd 168
f578f938 169 return updateSize (pb, pos);
1cb5f7fd
MN
170}
171
e45ccf79 172/* Sync sample atom */
f578f938 173static int mov_write_stss_tag(ByteIOContext *pb, MOVTrack* track)
1cb5f7fd 174{
f578f938
TR
175 long curpos;
176 int i, index = 0, entryPos;
177 int pos = url_ftell(pb);
178 put_be32(pb, 0); // size
1cb5f7fd 179 put_tag(pb, "stss");
f578f938
TR
180 put_be32(pb, 0); // version & flags
181 entryPos = url_ftell(pb);
182 put_be32(pb, track->entry); // entry count
183 for (i=0; i<track->entry; i++) {
184 int cl = i / MOV_INDEX_CLUSTER_SIZE;
185 int id = i % MOV_INDEX_CLUSTER_SIZE;
186 if(track->cluster[cl][id].key_frame == 1) {
187 put_be32(pb, i+1);
188 index++;
189 }
190 }
191 curpos = url_ftell(pb);
192 url_fseek(pb, entryPos, SEEK_SET);
193 put_be32(pb, index); // rewrite size
194 url_fseek(pb, curpos, SEEK_SET);
195 return updateSize (pb, pos);
1cb5f7fd
MN
196}
197
6e6d6dc0 198static int mov_write_damr_tag(ByteIOContext *pb)
1cb5f7fd
MN
199{
200 put_be32(pb, 0x11); /* size */
201 put_tag(pb, "damr");
202 put_tag(pb, "FFMP");
203 put_byte(pb, 0);
f578f938
TR
204
205 put_be16(pb, 0x80); /* Mode set (all modes for AMR_NB) */
206 put_be16(pb, 0xa); /* Mode change period (no restriction) */
207 //put_be16(pb, 0x81ff); /* Mode set (all modes for AMR_NB) */
208 //put_be16(pb, 1); /* Mode change period (no restriction) */
1cb5f7fd
MN
209 return 0x11;
210}
211
69dde1ad
GB
212static int mov_write_wave_tag(ByteIOContext *pb, MOVTrack* track)
213{
214 int pos = url_ftell(pb);
215
216 put_be32(pb, 0); /* size */
217 put_tag(pb, "wave");
218
219 put_be32(pb, 12); /* size */
220 put_tag(pb, "frma");
221 put_tag(pb, "mp4a");
222
223 put_be32(pb, 12); /* size */
224 put_tag(pb, "mp4a");
225 put_be32(pb, 0);
226
227 mov_write_esds_tag(pb, track);
228
229 put_be32(pb, 12); /* size */
230 put_tag(pb, "srcq");
231 put_be32(pb, 0x40);
232
233 put_be32(pb, 8); /* size */
234 put_be32(pb, 0); /* null tag */
235
236 return updateSize (pb, pos);
237}
238
8cc7a34d
AB
239const CodecTag codec_movaudio_tags[] = {
240 { CODEC_ID_PCM_MULAW, MKTAG('u', 'l', 'a', 'w') },
241 { CODEC_ID_PCM_ALAW, MKTAG('a', 'l', 'a', 'w') },
242 { CODEC_ID_ADPCM_IMA_QT, MKTAG('i', 'm', 'a', '4') },
243 { CODEC_ID_MACE3, MKTAG('M', 'A', 'C', '3') },
244 { CODEC_ID_MACE6, MKTAG('M', 'A', 'C', '6') },
245 { CODEC_ID_AAC, MKTAG('m', 'p', '4', 'a') },
246 { CODEC_ID_AMR_NB, MKTAG('s', 'a', 'm', 'r') },
247 { CODEC_ID_PCM_S16BE, MKTAG('t', 'w', 'o', 's') },
248 { CODEC_ID_PCM_S16LE, MKTAG('s', 'o', 'w', 't') },
249 { CODEC_ID_MP3, MKTAG('.', 'm', 'p', '3') },
250 { 0, 0 },
251};
252
f578f938 253static int mov_write_audio_tag(ByteIOContext *pb, MOVTrack* track)
1cb5f7fd 254{
8cc7a34d
AB
255 int pos = url_ftell(pb), tag;
256
1cb5f7fd 257 put_be32(pb, 0); /* size */
f578f938 258
8cc7a34d
AB
259 tag = codec_get_tag(codec_movaudio_tags, track->enc->codec_id);
260 // if no mac fcc found, try with Microsoft tags
261 if (!tag)
262 {
263 int tmp = codec_get_tag(codec_wav_tags, track->enc->codec_id);
264 if (tmp)
265 tag = MKTAG('m', 's', ((tmp >> 8) & 0xff), (tmp & 0xff));
266 }
267 if (!tag)
268 put_tag(pb, " ");
f578f938 269 else
8cc7a34d 270 put_le32(pb, tag); // store it byteswapped
f578f938 271
1cb5f7fd
MN
272 put_be32(pb, 0); /* Reserved */
273 put_be16(pb, 0); /* Reserved */
274 put_be16(pb, 1); /* Data-reference index, XXX == 1 */
69dde1ad 275
e45ccf79 276 /* SoundDescription */
69dde1ad
GB
277 if(track->mode == MODE_MOV && track->enc->codec_id == CODEC_ID_AAC)
278 put_be16(pb, 1); /* Version 1 */
279 else
280 put_be16(pb, 0); /* Version 0 */
e45ccf79 281 put_be16(pb, 0); /* Revision level */
1cb5f7fd
MN
282 put_be32(pb, 0); /* Reserved */
283
f578f938
TR
284 put_be16(pb, track->enc->channels); /* Number of channels */
285 /* TODO: Currently hard-coded to 16-bit, there doesn't seem
e45ccf79 286 to be a good way to get number of bits of audio */
1cb5f7fd 287 put_be16(pb, 0x10); /* Reserved */
9a4d9388
RS
288 put_be16(pb, 0); /* compression ID (= 0) */
289 put_be16(pb, 0); /* packet size (= 0) */
1cb5f7fd
MN
290 put_be16(pb, track->timescale); /* Time scale */
291 put_be16(pb, 0); /* Reserved */
292
69dde1ad
GB
293 if(track->mode == MODE_MOV && track->enc->codec_id == CODEC_ID_AAC)
294 {
295 /* SoundDescription V1 extended info */
296 put_be32(pb, track->enc->frame_size); /* Samples per packet */
297 put_be32(pb, 1536); /* Bytes per packet */
298 put_be32(pb, 2); /* Bytes per frame */
299 put_be32(pb, 2); /* Bytes per sample */
300 }
301
302 if(track->enc->codec_id == CODEC_ID_AAC) {
303 if( track->mode == MODE_MOV ) mov_write_wave_tag(pb, track);
304 else mov_write_esds_tag(pb, track);
305 }
f578f938
TR
306 if(track->enc->codec_id == CODEC_ID_AMR_NB)
307 mov_write_damr_tag(pb);
6e6d6dc0 308 return updateSize (pb, pos);
1cb5f7fd
MN
309}
310
6e6d6dc0 311static int mov_write_d263_tag(ByteIOContext *pb)
1cb5f7fd
MN
312{
313 put_be32(pb, 0xf); /* size */
314 put_tag(pb, "d263");
315 put_tag(pb, "FFMP");
316 put_be16(pb, 0x0a);
317 put_byte(pb, 0);
318 return 0xf;
319}
320
f578f938
TR
321/* TODO: No idea about these values */
322static int mov_write_svq3_tag(ByteIOContext *pb)
1cb5f7fd 323{
f578f938
TR
324 put_be32(pb, 0x15);
325 put_tag(pb, "SMI ");
326 put_tag(pb, "SEQH");
327 put_be32(pb, 0x5);
328 put_be32(pb, 0xe2c0211d);
329 put_be32(pb, 0xc0000000);
330 put_byte(pb, 0);
331 return 0x15;
1cb5f7fd
MN
332}
333
e45ccf79
GB
334static unsigned int descrLength(unsigned int len)
335{
336 if (len < 0x00000080)
337 return 2 + len;
338 else if (len < 0x00004000)
339 return 3 + len;
340 else if(len < 0x00200000)
341 return 4 + len;
342 else
343 return 5 + len;
344}
345
346static void putDescr(ByteIOContext *pb, int tag, int size)
1cb5f7fd 347{
e45ccf79
GB
348 uint32_t len;
349 uint8_t vals[4];
350
351 len = size;
352 vals[3] = (uint8_t)(len & 0x7f);
353 len >>= 7;
354 vals[2] = (uint8_t)((len & 0x7f) | 0x80);
355 len >>= 7;
356 vals[1] = (uint8_t)((len & 0x7f) | 0x80);
357 len >>= 7;
358 vals[0] = (uint8_t)((len & 0x7f) | 0x80);
359
360 put_byte(pb, tag); // DescriptorTag
361
362 if (size < 0x00000080)
363 {
364 put_byte(pb, vals[3]);
365 }
366 else if (size < 0x00004000)
367 {
368 put_byte(pb, vals[2]);
369 put_byte(pb, vals[3]);
370 }
371 else if (size < 0x00200000)
372 {
373 put_byte(pb, vals[1]);
374 put_byte(pb, vals[2]);
375 put_byte(pb, vals[3]);
376 }
377 else if (size < 0x10000000)
378 {
379 put_byte(pb, vals[0]);
380 put_byte(pb, vals[1]);
381 put_byte(pb, vals[2]);
382 put_byte(pb, vals[3]);
383 }
1cb5f7fd
MN
384}
385
6e6d6dc0 386static int mov_write_esds_tag(ByteIOContext *pb, MOVTrack* track) // Basic
1cb5f7fd 387{
e45ccf79
GB
388 int decoderSpecificInfoLen = track->vosLen ? descrLength(track->vosLen):0;
389 int pos = url_ftell(pb);
390
391 put_be32(pb, 0); // size
1cb5f7fd 392 put_tag(pb, "esds");
e45ccf79 393 put_be32(pb, 0); // Version
1cb5f7fd 394
e45ccf79
GB
395 // ES descriptor
396 putDescr(pb, 0x03, 3 + descrLength(13 + decoderSpecificInfoLen) +
397 descrLength(1));
398 put_be16(pb, 0x0001); // ID (= 1)
1cb5f7fd
MN
399 put_byte(pb, 0x00); // flags (= no flags)
400
e45ccf79
GB
401 // DecoderConfig descriptor
402 putDescr(pb, 0x04, 13 + decoderSpecificInfoLen);
403
404 if(track->enc->codec_id == CODEC_ID_AAC)
405 put_byte(pb, 0x40); // Object type indication
406 else if(track->enc->codec_id == CODEC_ID_MPEG4)
407 put_byte(pb, 0x20); // Object type indication (Visual 14496-2)
408
409 if(track->enc->codec_type == CODEC_TYPE_AUDIO)
410 put_byte(pb, 0x15); // flags (= Audiostream)
411 else
412 put_byte(pb, 0x11); // flags (= Visualstream)
413
1cb5f7fd
MN
414 put_byte(pb, 0x0); // Buffersize DB (24 bits)
415 put_be16(pb, 0x0dd2); // Buffersize DB
416
417 // TODO: find real values for these
418 put_be32(pb, 0x0002e918); // maxbitrate
419 put_be32(pb, 0x00017e6b); // avg bitrate
420
e45ccf79
GB
421 if (track->vosLen)
422 {
423 // DecoderSpecific info descriptor
424 putDescr(pb, 0x05, track->vosLen);
425 put_buffer(pb, track->vosData, track->vosLen);
426 }
427
428 // SL descriptor
4bfc029f 429 putDescr(pb, 0x06, 1);
1cb5f7fd 430 put_byte(pb, 0x02);
e45ccf79 431 return updateSize (pb, pos);
1cb5f7fd
MN
432}
433
8cc7a34d
AB
434const CodecTag codec_movvideo_tags[] = {
435 { CODEC_ID_SVQ1, MKTAG('S', 'V', 'Q', '1') },
436 { CODEC_ID_SVQ3, MKTAG('S', 'V', 'Q', '3') },
437 { CODEC_ID_MPEG4, MKTAG('m', 'p', '4', 'v') },
438 { CODEC_ID_H263, MKTAG('s', '2', '6', '3') },
439 { CODEC_ID_DVVIDEO, MKTAG('d', 'v', 'c', ' ') },
440 { 0, 0 },
441};
442
f578f938 443static int mov_write_video_tag(ByteIOContext *pb, MOVTrack* track)
1cb5f7fd 444{
8cc7a34d
AB
445 int pos = url_ftell(pb), tag;
446
f578f938 447 put_be32(pb, 0); /* size */
8cc7a34d
AB
448
449 tag = codec_get_tag(codec_movvideo_tags, track->enc->codec_id);
450 // if no mac fcc found, try with Microsoft tags
451 if (!tag)
452 tag = codec_get_tag(codec_bmp_tags, track->enc->codec_id);
453 if (!tag)
454 put_tag(pb, " ");
f578f938 455 else
8cc7a34d 456 put_le32(pb, tag); // store it byteswapped
6e6d6dc0 457
f578f938
TR
458 put_be32(pb, 0); /* Reserved */
459 put_be16(pb, 0); /* Reserved */
460 put_be16(pb, 1); /* Data-reference index */
461
462 put_be32(pb, 0); /* Reserved (= 02000c) */
463 put_be32(pb, 0); /* Reserved ("SVis")*/
464 put_be32(pb, 0); /* Reserved */
465 put_be32(pb, 0); /* Reserved (400)*/
466 put_be16(pb, track->enc->width); /* Video width */
467 put_be16(pb, track->enc->height); /* Video height */
468 put_be32(pb, 0x00480000); /* Reserved */
469 put_be32(pb, 0x00480000); /* Reserved */
9a4d9388
RS
470 put_be32(pb, 0); /* Data size (= 0) */
471 put_be16(pb, 1); /* Frame count (= 1) */
472
f578f938
TR
473 put_be32(pb, 0); /* Reserved */
474 put_be32(pb, 0); /* Reserved */
475 put_be32(pb, 0); /* Reserved */
476 put_be32(pb, 0); /* Reserved */
477 put_be32(pb, 0); /* Reserved */
478 put_be32(pb, 0); /* Reserved */
f578f938
TR
479 put_be32(pb, 0); /* Reserved */
480 put_be32(pb, 0); /* Reserved */
9a4d9388 481
f578f938
TR
482 put_be16(pb, 0x18); /* Reserved */
483 put_be16(pb, 0xffff); /* Reserved */
484 if(track->enc->codec_id == CODEC_ID_MPEG4)
485 mov_write_esds_tag(pb, track);
486 else if(track->enc->codec_id == CODEC_ID_H263)
487 mov_write_d263_tag(pb);
488 else if(track->enc->codec_id == CODEC_ID_SVQ3)
489 mov_write_svq3_tag(pb);
490
491 return updateSize (pb, pos);
1cb5f7fd
MN
492}
493
6e6d6dc0 494static int mov_write_stsd_tag(ByteIOContext *pb, MOVTrack* track)
1cb5f7fd 495{
6e6d6dc0 496 int pos = url_ftell(pb);
1cb5f7fd
MN
497 put_be32(pb, 0); /* size */
498 put_tag(pb, "stsd");
499 put_be32(pb, 0); /* version & flags */
500 put_be32(pb, 1); /* entry count */
f578f938
TR
501 if (track->enc->codec_type == CODEC_TYPE_VIDEO)
502 mov_write_video_tag(pb, track);
503 else if (track->enc->codec_type == CODEC_TYPE_AUDIO)
504 mov_write_audio_tag(pb, track);
6e6d6dc0 505 return updateSize(pb, pos);
1cb5f7fd
MN
506}
507
f578f938 508/* TODO?: Currently all samples/frames seem to have same duration */
e45ccf79 509/* Time to sample atom */
6e6d6dc0 510static int mov_write_stts_tag(ByteIOContext *pb, MOVTrack* track)
1cb5f7fd
MN
511{
512 put_be32(pb, 0x18); /* size */
513 put_tag(pb, "stts");
514 put_be32(pb, 0); /* version & flags */
515 put_be32(pb, 1); /* entry count */
516
e45ccf79
GB
517 put_be32(pb, track->sampleCount); /* sample count */
518 put_be32(pb, track->sampleDuration); /* sample duration */
1cb5f7fd
MN
519 return 0x18;
520}
521
6e6d6dc0 522static int mov_write_dref_tag(ByteIOContext *pb)
1cb5f7fd
MN
523{
524 put_be32(pb, 28); /* size */
525 put_tag(pb, "dref");
526 put_be32(pb, 0); /* version & flags */
527 put_be32(pb, 1); /* entry count */
528
529 put_be32(pb, 0xc); /* size */
530 put_tag(pb, "url ");
531 put_be32(pb, 1); /* version & flags */
532
533 return 28;
534}
535
6e6d6dc0 536static int mov_write_stbl_tag(ByteIOContext *pb, MOVTrack* track)
1cb5f7fd 537{
6e6d6dc0 538 int pos = url_ftell(pb);
1cb5f7fd
MN
539 put_be32(pb, 0); /* size */
540 put_tag(pb, "stbl");
6e6d6dc0
MN
541 mov_write_stsd_tag(pb, track);
542 mov_write_stts_tag(pb, track);
f578f938
TR
543 if (track->enc->codec_type == CODEC_TYPE_VIDEO &&
544 track->hasKeyframes)
545 mov_write_stss_tag(pb, track);
6e6d6dc0
MN
546 mov_write_stsc_tag(pb, track);
547 mov_write_stsz_tag(pb, track);
548 mov_write_stco_tag(pb, track);
549 return updateSize(pb, pos);
1cb5f7fd
MN
550}
551
6e6d6dc0 552static int mov_write_dinf_tag(ByteIOContext *pb)
1cb5f7fd 553{
6e6d6dc0 554 int pos = url_ftell(pb);
1cb5f7fd
MN
555 put_be32(pb, 0); /* size */
556 put_tag(pb, "dinf");
6e6d6dc0
MN
557 mov_write_dref_tag(pb);
558 return updateSize(pb, pos);
1cb5f7fd
MN
559}
560
6e6d6dc0 561static int mov_write_smhd_tag(ByteIOContext *pb)
1cb5f7fd
MN
562{
563 put_be32(pb, 16); /* size */
564 put_tag(pb, "smhd");
565 put_be32(pb, 0); /* version & flags */
566 put_be16(pb, 0); /* reserved (balance, normally = 0) */
567 put_be16(pb, 0); /* reserved */
568 return 16;
569}
570
6e6d6dc0 571static int mov_write_vmhd_tag(ByteIOContext *pb)
1cb5f7fd
MN
572{
573 put_be32(pb, 0x14); /* size (always 0x14) */
574 put_tag(pb, "vmhd");
575 put_be32(pb, 0x01); /* version & flags */
576 put_be64(pb, 0); /* reserved (graphics mode = copy) */
577 return 0x14;
578}
579
6e6d6dc0 580static int mov_write_hdlr_tag(ByteIOContext *pb, MOVTrack* track)
1cb5f7fd 581{
9a4d9388 582 char *descr, *hdlr, *hdlr_type;
f578f938 583 int pos = url_ftell(pb);
9a4d9388
RS
584
585 if (!track) { /* no media --> data handler */
586 hdlr = "dhlr";
587 hdlr_type = "url ";
588 descr = "DataHandler";
589 } else {
590 hdlr = (track->mode == MODE_MOV) ? "mhlr" : "\0\0\0\0";
591 if (track->enc->codec_type == CODEC_TYPE_VIDEO) {
592 hdlr_type = "vide";
593 descr = "VideoHandler";
594 } else {
595 hdlr_type = "soun";
596 descr = "SoundHandler";
597 }
598 }
599
f578f938 600 put_be32(pb, 0); /* size */
1cb5f7fd
MN
601 put_tag(pb, "hdlr");
602 put_be32(pb, 0); /* Version & flags */
906b578f 603 put_buffer(pb, hdlr, 4); /* handler */
9a4d9388 604 put_tag(pb, hdlr_type); /* handler type */
f578f938
TR
605 put_be32(pb ,0); /* reserved */
606 put_be32(pb ,0); /* reserved */
607 put_be32(pb ,0); /* reserved */
9a4d9388
RS
608 put_byte(pb, strlen(descr)); /* string counter */
609 put_buffer(pb, descr, strlen(descr)); /* handler description */
610 return updateSize(pb, pos);
611}
612
613static int mov_write_minf_tag(ByteIOContext *pb, MOVTrack* track)
614{
615 int pos = url_ftell(pb);
616 put_be32(pb, 0); /* size */
617 put_tag(pb, "minf");
1cb5f7fd 618 if(track->enc->codec_type == CODEC_TYPE_VIDEO)
9a4d9388 619 mov_write_vmhd_tag(pb);
1cb5f7fd 620 else
9a4d9388
RS
621 mov_write_smhd_tag(pb);
622 if (track->mode == MODE_MOV) /* FIXME: Why do it for MODE_MOV only ? */
623 mov_write_hdlr_tag(pb, NULL);
624 mov_write_dinf_tag(pb);
625 mov_write_stbl_tag(pb, track);
f578f938 626 return updateSize(pb, pos);
1cb5f7fd
MN
627}
628
6e6d6dc0 629static int mov_write_mdhd_tag(ByteIOContext *pb, MOVTrack* track)
1cb5f7fd
MN
630{
631 put_be32(pb, 32); /* size */
632 put_tag(pb, "mdhd");
633 put_be32(pb, 0); /* Version & flags */
634 put_be32(pb, track->time); /* creation time */
635 put_be32(pb, track->time); /* modification time */
e45ccf79
GB
636 put_be32(pb, track->timescale); /* time scale (sample rate for audio) */
637 put_be32(pb, track->trackDuration); /* duration */
1cb5f7fd
MN
638 put_be16(pb, 0); /* language, 0 = english */
639 put_be16(pb, 0); /* reserved (quality) */
640 return 32;
641}
642
6e6d6dc0 643static int mov_write_mdia_tag(ByteIOContext *pb, MOVTrack* track)
1cb5f7fd 644{
6e6d6dc0 645 int pos = url_ftell(pb);
1cb5f7fd
MN
646 put_be32(pb, 0); /* size */
647 put_tag(pb, "mdia");
6e6d6dc0
MN
648 mov_write_mdhd_tag(pb, track);
649 mov_write_hdlr_tag(pb, track);
650 mov_write_minf_tag(pb, track);
651 return updateSize(pb, pos);
1cb5f7fd
MN
652}
653
6e6d6dc0 654static int mov_write_tkhd_tag(ByteIOContext *pb, MOVTrack* track)
1cb5f7fd 655{
f578f938 656 int64_t maxTrackLenTemp;
1cb5f7fd
MN
657 put_be32(pb, 0x5c); /* size (always 0x5c) */
658 put_tag(pb, "tkhd");
f578f938 659 put_be32(pb, 0xf); /* version & flags (track enabled) */
1cb5f7fd
MN
660 put_be32(pb, track->time); /* creation time */
661 put_be32(pb, track->time); /* modification time */
662 put_be32(pb, track->trackID); /* track-id */
663 put_be32(pb, 0); /* reserved */
f578f938
TR
664 maxTrackLenTemp = ((int64_t)globalTimescale*(int64_t)track->trackDuration)/(int64_t)track->timescale;
665 put_be32(pb, (long)maxTrackLenTemp); /* duration */
1cb5f7fd
MN
666
667 put_be32(pb, 0); /* reserved */
668 put_be32(pb, 0); /* reserved */
669 put_be32(pb, 0x0); /* reserved (Layer & Alternate group) */
670 /* Volume, only for audio */
671 if(track->enc->codec_type == CODEC_TYPE_AUDIO)
672 put_be16(pb, 0x0100);
673 else
674 put_be16(pb, 0);
675 put_be16(pb, 0); /* reserved */
676
677 /* Matrix structure */
678 put_be32(pb, 0x00010000); /* reserved */
679 put_be32(pb, 0x0); /* reserved */
680 put_be32(pb, 0x0); /* reserved */
681 put_be32(pb, 0x0); /* reserved */
682 put_be32(pb, 0x00010000); /* reserved */
683 put_be32(pb, 0x0); /* reserved */
684 put_be32(pb, 0x0); /* reserved */
685 put_be32(pb, 0x0); /* reserved */
686 put_be32(pb, 0x40000000); /* reserved */
687
688 /* Track width and height, for visual only */
689 if(track->enc->codec_type == CODEC_TYPE_VIDEO) {
69dde1ad
GB
690 double sample_aspect_ratio = av_q2d(track->enc->sample_aspect_ratio);
691 if( !sample_aspect_ratio ) sample_aspect_ratio = 1;
692 put_be32(pb, sample_aspect_ratio * track->enc->width*0x10000);
f578f938 693 put_be32(pb, track->enc->height*0x10000);
1cb5f7fd
MN
694 }
695 else {
696 put_be32(pb, 0);
697 put_be32(pb, 0);
698 }
699 return 0x5c;
700}
701
6e6d6dc0 702static int mov_write_trak_tag(ByteIOContext *pb, MOVTrack* track)
1cb5f7fd 703{
6e6d6dc0 704 int pos = url_ftell(pb);
1cb5f7fd
MN
705 put_be32(pb, 0); /* size */
706 put_tag(pb, "trak");
6e6d6dc0
MN
707 mov_write_tkhd_tag(pb, track);
708 mov_write_mdia_tag(pb, track);
709 return updateSize(pb, pos);
1cb5f7fd
MN
710}
711
712/* TODO: Not sorted out, but not necessary either */
6e6d6dc0 713static int mov_write_iods_tag(ByteIOContext *pb, MOVContext *mov)
1cb5f7fd
MN
714{
715 put_be32(pb, 0x15); /* size */
716 put_tag(pb, "iods");
717 put_be32(pb, 0); /* version & flags */
718 put_be16(pb, 0x1007);
719 put_byte(pb, 0);
720 put_be16(pb, 0x4fff);
721 put_be16(pb, 0xfffe);
722 put_be16(pb, 0x01ff);
723 return 0x15;
724}
725
6e6d6dc0 726static int mov_write_mvhd_tag(ByteIOContext *pb, MOVContext *mov)
1cb5f7fd
MN
727{
728 int maxTrackID = 1, maxTrackLen = 0, i;
f578f938 729 int64_t maxTrackLenTemp;
1cb5f7fd
MN
730
731 put_be32(pb, 0x6c); /* size (always 0x6c) */
732 put_tag(pb, "mvhd");
733 put_be32(pb, 0); /* version & flags */
734 put_be32(pb, mov->time); /* creation time */
735 put_be32(pb, mov->time); /* modification time */
736 put_be32(pb, mov->timescale); /* timescale */
737 for (i=0; i<MAX_STREAMS; i++) {
738 if(mov->tracks[i].entry > 0) {
f578f938
TR
739 maxTrackLenTemp = ((int64_t)globalTimescale*(int64_t)mov->tracks[i].trackDuration)/(int64_t)mov->tracks[i].timescale;
740 if(maxTrackLen < maxTrackLenTemp)
741 maxTrackLen = maxTrackLenTemp;
1cb5f7fd
MN
742 if(maxTrackID < mov->tracks[i].trackID)
743 maxTrackID = mov->tracks[i].trackID;
744 }
745 }
746 put_be32(pb, maxTrackLen); /* duration of longest track */
747
748 put_be32(pb, 0x00010000); /* reserved (preferred rate) 1.0 = normal */
749 put_be16(pb, 0x0100); /* reserved (preferred volume) 1.0 = normal */
750 put_be16(pb, 0); /* reserved */
751 put_be32(pb, 0); /* reserved */
752 put_be32(pb, 0); /* reserved */
753
754 /* Matrix structure */
755 put_be32(pb, 0x00010000); /* reserved */
756 put_be32(pb, 0x0); /* reserved */
757 put_be32(pb, 0x0); /* reserved */
758 put_be32(pb, 0x0); /* reserved */
759 put_be32(pb, 0x00010000); /* reserved */
760 put_be32(pb, 0x0); /* reserved */
761 put_be32(pb, 0x0); /* reserved */
762 put_be32(pb, 0x0); /* reserved */
763 put_be32(pb, 0x40000000); /* reserved */
764
765 put_be32(pb, 0); /* reserved (preview time) */
766 put_be32(pb, 0); /* reserved (preview duration) */
767 put_be32(pb, 0); /* reserved (poster time) */
768 put_be32(pb, 0); /* reserved (selection time) */
769 put_be32(pb, 0); /* reserved (selection duration) */
770 put_be32(pb, 0); /* reserved (current time) */
771 put_be32(pb, maxTrackID+1); /* Next track id */
772 return 0x6c;
773}
774
69dde1ad
GB
775static int mov_write_udta_tag(ByteIOContext *pb, MOVContext* mov,
776 AVFormatContext *s)
777{
778 int pos = url_ftell(pb);
779 int i;
780
781 put_be32(pb, 0); /* size */
782 put_tag(pb, "udta");
783
784 /* Requirements */
785 for (i=0; i<MAX_STREAMS; i++) {
786 if(mov->tracks[i].entry <= 0) continue;
787 if (mov->tracks[i].enc->codec_id == CODEC_ID_AAC ||
788 mov->tracks[i].enc->codec_id == CODEC_ID_MPEG4) {
789 int pos = url_ftell(pb);
790 put_be32(pb, 0); /* size */
791 put_tag(pb, "\251req");
792 put_be16(pb, sizeof("QuickTime 6.0 or greater") - 1);
793 put_be16(pb, 0);
794 put_buffer(pb, "QuickTime 6.0 or greater",
795 sizeof("QuickTime 6.0 or greater") - 1);
796 updateSize(pb, pos);
797 break;
798 }
799 }
800
801 /* Encoder */
75553f82 802 if(!(mov->tracks[0].enc->flags & CODEC_FLAG_BITEXACT))
69dde1ad
GB
803 {
804 int pos = url_ftell(pb);
805 put_be32(pb, 0); /* size */
806 put_tag(pb, "\251enc");
807 put_be16(pb, sizeof(LIBAVFORMAT_IDENT) - 1); /* string length */
808 put_be16(pb, 0);
809 put_buffer(pb, LIBAVFORMAT_IDENT, sizeof(LIBAVFORMAT_IDENT) - 1);
810 updateSize(pb, pos);
811 }
812
813 if( s->title[0] )
814 {
815 int pos = url_ftell(pb);
816 put_be32(pb, 0); /* size */
817 put_tag(pb, "\251nam");
818 put_be16(pb, strlen(s->title)); /* string length */
819 put_be16(pb, 0);
820 put_buffer(pb, s->title, strlen(s->title));
821 updateSize(pb, pos);
822 }
823
824 if( s->author[0] )
825 {
826 int pos = url_ftell(pb);
827 put_be32(pb, 0); /* size */
828 put_tag(pb, /*"\251aut"*/ "\251day" );
829 put_be16(pb, strlen(s->author)); /* string length */
830 put_be16(pb, 0);
831 put_buffer(pb, s->author, strlen(s->author));
832 updateSize(pb, pos);
833 }
834
835 if( s->comment[0] )
836 {
837 int pos = url_ftell(pb);
838 put_be32(pb, 0); /* size */
839 put_tag(pb, "\251des");
840 put_be16(pb, strlen(s->comment)); /* string length */
841 put_be16(pb, 0);
842 put_buffer(pb, s->comment, strlen(s->comment));
843 updateSize(pb, pos);
844 }
845
846 return updateSize(pb, pos);
847}
848
849static int mov_write_moov_tag(ByteIOContext *pb, MOVContext *mov,
850 AVFormatContext *s)
1cb5f7fd 851{
6e6d6dc0 852 int pos, i;
1cb5f7fd
MN
853 pos = url_ftell(pb);
854 put_be32(pb, 0); /* size placeholder*/
855 put_tag(pb, "moov");
856 mov->timescale = globalTimescale;
857
858 for (i=0; i<MAX_STREAMS; i++) {
e45ccf79
GB
859 if(mov->tracks[i].entry <= 0) continue;
860
861 if(mov->tracks[i].enc->codec_type == CODEC_TYPE_VIDEO) {
862 mov->tracks[i].timescale = mov->tracks[i].enc->frame_rate;
863 mov->tracks[i].sampleDuration = mov->tracks[i].enc->frame_rate_base;
864 }
865 else if(mov->tracks[i].enc->codec_type == CODEC_TYPE_AUDIO) {
866 /* If AMR, track timescale = 8000, AMR_WB = 16000 */
867 if(mov->tracks[i].enc->codec_id == CODEC_ID_AMR_NB) {
868 mov->tracks[i].sampleDuration = 160; // Bytes per chunk
869 mov->tracks[i].timescale = 8000;
1cb5f7fd 870 }
e45ccf79
GB
871 else {
872 mov->tracks[i].timescale = mov->tracks[i].enc->sample_rate;
873 mov->tracks[i].sampleDuration = mov->tracks[i].enc->frame_size;
1cb5f7fd 874 }
1cb5f7fd 875 }
e45ccf79
GB
876
877 mov->tracks[i].trackDuration =
878 mov->tracks[i].sampleCount * mov->tracks[i].sampleDuration;
879 mov->tracks[i].time = mov->time;
880 mov->tracks[i].trackID = i+1;
1cb5f7fd
MN
881 }
882
6e6d6dc0
MN
883 mov_write_mvhd_tag(pb, mov);
884 //mov_write_iods_tag(pb, mov);
1cb5f7fd
MN
885 for (i=0; i<MAX_STREAMS; i++) {
886 if(mov->tracks[i].entry > 0) {
6e6d6dc0 887 mov_write_trak_tag(pb, &(mov->tracks[i]));
1cb5f7fd
MN
888 }
889 }
890
69dde1ad
GB
891 mov_write_udta_tag(pb, mov, s);
892
6e6d6dc0 893 return updateSize(pb, pos);
1cb5f7fd
MN
894}
895
f578f938 896int mov_write_mdat_tag(ByteIOContext *pb, MOVContext* mov)
1cb5f7fd 897{
f578f938 898 mov->mdat_pos = url_ftell(pb);
1cb5f7fd
MN
899 put_be32(pb, 0); /* size placeholder*/
900 put_tag(pb, "mdat");
901 return 0;
902}
903
904/* TODO: This needs to be more general */
e45ccf79 905int mov_write_ftyp_tag(ByteIOContext *pb, AVFormatContext *s)
1cb5f7fd 906{
69dde1ad
GB
907 MOVContext *mov = s->priv_data;
908
1cb5f7fd
MN
909 put_be32(pb, 0x14 ); /* size */
910 put_tag(pb, "ftyp");
e45ccf79 911
69dde1ad 912 if ( mov->mode == MODE_3GP )
e45ccf79
GB
913 put_tag(pb, "3gp4");
914 else
915 put_tag(pb, "isom");
916
1cb5f7fd 917 put_be32(pb, 0x200 );
e45ccf79 918
69dde1ad 919 if ( mov->mode == MODE_3GP )
e45ccf79
GB
920 put_tag(pb, "3gp4");
921 else
922 put_tag(pb, "mp41");
923
1cb5f7fd
MN
924 return 0x14;
925}
926
927static int mov_write_header(AVFormatContext *s)
928{
929 ByteIOContext *pb = &s->pb;
69dde1ad
GB
930 MOVContext *mov = s->priv_data;
931 int i;
1cb5f7fd 932
69dde1ad
GB
933 /* Default mode == MP4 */
934 mov->mode = MODE_MP4;
935
936 if (s->oformat != NULL) {
937 if (!strcmp("3gp", s->oformat->name)) mov->mode = MODE_3GP;
938 else if (!strcmp("mov", s->oformat->name)) mov->mode = MODE_MOV;
939
940 if ( mov->mode == MODE_3GP || mov->mode == MODE_MP4 )
941 mov_write_ftyp_tag(pb,s);
942 }
943
944 for (i=0; i<MAX_STREAMS; i++) {
945 mov->tracks[i].mode = mov->mode;
f578f938
TR
946 }
947
1cb5f7fd
MN
948 put_flush_packet(pb);
949
950 return 0;
951}
952
e928649b 953static int mov_write_packet(AVFormatContext *s, AVPacket *pkt)
1cb5f7fd
MN
954{
955 MOVContext *mov = s->priv_data;
956 ByteIOContext *pb = &s->pb;
e928649b
MN
957 AVCodecContext *enc = &s->streams[pkt->stream_index]->codec;
958 MOVTrack* trk = &mov->tracks[pkt->stream_index];
1a31840c 959 int cl, id;
e45ccf79 960 unsigned int samplesInChunk = 0;
e928649b 961 int size= pkt->size;
1cb5f7fd 962
e45ccf79
GB
963 if (url_is_streamed(&s->pb)) return 0; /* Can't handle that */
964 if (!size) return 0; /* Discard 0 sized packets */
1cb5f7fd 965
e45ccf79
GB
966 if (enc->codec_type == CODEC_TYPE_VIDEO ) {
967 samplesInChunk = 1;
968 }
969 else if (enc->codec_type == CODEC_TYPE_AUDIO ) {
970 if( enc->codec_id == CODEC_ID_AMR_NB) {
f578f938 971 /* We must find out how many AMR blocks there are in one packet */
e45ccf79
GB
972 static uint16_t packed_size[16] =
973 {13, 14, 16, 18, 20, 21, 27, 32, 6, 0, 0, 0, 0, 0, 0, 0};
974 int len = 0;
975
976 while (len < size && samplesInChunk < 100) {
e928649b 977 len += packed_size[(pkt->data[len] >> 3) & 0x0F];
e45ccf79 978 samplesInChunk++;
f578f938 979 }
1cb5f7fd 980 }
e45ccf79
GB
981 else if(enc->codec_id == CODEC_ID_PCM_ALAW) {
982 samplesInChunk = size/enc->channels;
1cb5f7fd 983 }
9a4d9388
RS
984 else if(enc->codec_id == CODEC_ID_PCM_S16BE || enc->codec_id == CODEC_ID_PCM_S16LE) {
985 samplesInChunk = size/(2*enc->channels);
986 }
e45ccf79
GB
987 else {
988 samplesInChunk = 1;
1cb5f7fd 989 }
e45ccf79
GB
990 }
991
992 if ((enc->codec_id == CODEC_ID_MPEG4 || enc->codec_id == CODEC_ID_AAC)
993 && trk->vosLen == 0) {
21f52609 994// assert(enc->extradata_size);
e45ccf79
GB
995
996 trk->vosLen = enc->extradata_size;
997 trk->vosData = av_malloc(trk->vosLen);
998 memcpy(trk->vosData, enc->extradata, trk->vosLen);
999 }
1000
1001 cl = trk->entry / MOV_INDEX_CLUSTER_SIZE;
1002 id = trk->entry % MOV_INDEX_CLUSTER_SIZE;
1003
1004 if (trk->ents_allocated <= trk->entry) {
1005 trk->cluster = av_realloc(trk->cluster, (cl+1)*sizeof(void*));
1006 if (!trk->cluster)
1007 return -1;
1008 trk->cluster[cl] = av_malloc(MOV_INDEX_CLUSTER_SIZE*sizeof(MOVIentry));
1009 if (!trk->cluster[cl])
1010 return -1;
1011 trk->ents_allocated += MOV_INDEX_CLUSTER_SIZE;
1012 }
1013 if (mov->mdat_written == 0) {
1014 mov_write_mdat_tag(pb, mov);
1015 mov->mdat_written = 1;
21f52609 1016 mov->time = s->timestamp;
e45ccf79
GB
1017 }
1018
69dde1ad 1019 trk->cluster[cl][id].pos = url_ftell(pb);
e45ccf79
GB
1020 trk->cluster[cl][id].samplesInChunk = samplesInChunk;
1021 trk->cluster[cl][id].size = size;
1022 trk->cluster[cl][id].entries = samplesInChunk;
1023 if(enc->codec_type == CODEC_TYPE_VIDEO) {
e928649b
MN
1024 trk->cluster[cl][id].key_frame = !!(pkt->flags & PKT_FLAG_KEY);
1025 if(trk->cluster[cl][id].key_frame)
f578f938 1026 trk->hasKeyframes = 1;
1cb5f7fd 1027 }
e45ccf79
GB
1028 trk->enc = enc;
1029 trk->entry++;
1030 trk->sampleCount += samplesInChunk;
1031 trk->mdat_size += size;
1032
e928649b 1033 put_buffer(pb, pkt->data, size);
1cb5f7fd
MN
1034
1035 put_flush_packet(pb);
1036 return 0;
1037}
1038
1039static int mov_write_trailer(AVFormatContext *s)
1040{
1041 MOVContext *mov = s->priv_data;
1042 ByteIOContext *pb = &s->pb;
1043 int res = 0;
1044 int i, j;
1cb5f7fd 1045
69dde1ad 1046 offset_t moov_pos = url_ftell(pb);
1cb5f7fd
MN
1047
1048 /* Write size of mdat tag */
69dde1ad 1049 for (i=0, j=0; i<MAX_STREAMS; i++) {
1cb5f7fd
MN
1050 if(mov->tracks[i].ents_allocated > 0) {
1051 j += mov->tracks[i].mdat_size;
1052 }
1053 }
f578f938 1054 url_fseek(pb, mov->mdat_pos, SEEK_SET);
1cb5f7fd 1055 put_be32(pb, j+8);
69dde1ad 1056 url_fseek(pb, moov_pos, SEEK_SET);
1cb5f7fd 1057
69dde1ad 1058 mov_write_moov_tag(pb, mov, s);
1cb5f7fd
MN
1059
1060 for (i=0; i<MAX_STREAMS; i++) {
1061 for (j=0; j<mov->tracks[i].ents_allocated/MOV_INDEX_CLUSTER_SIZE; j++) {
1062 av_free(mov->tracks[i].cluster[j]);
1063 }
1064 av_free(mov->tracks[i].cluster);
ec7d0d2e
GB
1065 if( mov->tracks[i].vosLen ) av_free( mov->tracks[i].vosData );
1066
1cb5f7fd
MN
1067 mov->tracks[i].cluster = NULL;
1068 mov->tracks[i].ents_allocated = mov->tracks[i].entry = 0;
1069 }
69dde1ad 1070
1cb5f7fd
MN
1071 put_flush_packet(pb);
1072
1073 return res;
1074}
1075
1076static AVOutputFormat mov_oformat = {
1077 "mov",
1078 "mov format",
1079 NULL,
1080 "mov",
1081 sizeof(MOVContext),
69dde1ad 1082 CODEC_ID_AAC,
2187d948 1083 CODEC_ID_MPEG4,
1cb5f7fd
MN
1084 mov_write_header,
1085 mov_write_packet,
1086 mov_write_trailer,
1087};
1088
1089static AVOutputFormat _3gp_oformat = {
1090 "3gp",
1091 "3gp format",
1092 NULL,
1093 "3gp",
1094 sizeof(MOVContext),
1095 CODEC_ID_AMR_NB,
1096 CODEC_ID_H263,
1097 mov_write_header,
1098 mov_write_packet,
1099 mov_write_trailer,
1100};
1101
1102static AVOutputFormat mp4_oformat = {
1103 "mp4",
1104 "mp4 format",
4cb3f3b6
DC
1105 "application/mp4",
1106 "mp4,m4a",
1cb5f7fd
MN
1107 sizeof(MOVContext),
1108 CODEC_ID_AAC,
1109 CODEC_ID_MPEG4,
1110 mov_write_header,
1111 mov_write_packet,
1112 mov_write_trailer,
1113};
1114
1115int movenc_init(void)
1116{
1117 av_register_output_format(&mov_oformat);
1118 av_register_output_format(&_3gp_oformat);
1119 av_register_output_format(&mp4_oformat);
1120 return 0;
1121}