2 * Copyright (C) 2013 nu774
3 * For conditions of distribution and use, see copyright notice in COPYING
17 # include <inttypes.h>
18 #elif defined _MSC_VER
19 # define PRId64 "I64d"
22 #include "m4af_endian.h"
24 #define m4af_realloc(memory,size) realloc(memory, size)
25 #define m4af_free(memory) free(memory)
26 #define m4af_max(a,b) ((a)<(b)?(b):(a))
28 #define M4AF_ATOM_WILD 0xffffffff
30 typedef struct m4af_sample_entry_t
{
33 } m4af_sample_entry_t
;
35 typedef struct m4af_chunk_entry_t
{
38 uint32_t samples_per_chunk
;
42 typedef struct m4af_track_t
{
45 uint16_t num_channels
;
46 int64_t creation_time
;
47 int64_t modification_time
;
49 uint32_t frame_duration
;
50 uint32_t encoder_delay
;
52 uint8_t *decSpecificInfo
;
53 uint32_t decSpecificInfoSize
;
54 uint32_t bufferSizeDB
;
59 m4af_sample_entry_t
*sample_table
;
61 uint32_t sample_table_capacity
;
63 m4af_chunk_entry_t
*chunk_table
;
65 uint32_t chunk_table_capacity
;
67 uint8_t *chunk_buffer
;
69 uint32_t chunk_capacity
;
71 /* temporary, to help parsing */
81 int64_t creation_time
;
82 int64_t modification_time
;
88 m4af_itmf_entry_t
*itmf_table
;
90 uint32_t itmf_table_capacity
;
92 m4af_io_callbacks_t io
;
96 m4af_track_t track
[2];
98 m4af_itmf_entry_t current_tag
;
101 typedef struct m4af_box_parser_t
{
103 int (*handler
)(m4af_ctx_t
*ctx
, uint32_t name
, uint64_t size
);
107 int m4af_write_null_cb(void *cookie
, const void *data
, uint32_t size
)
109 int64_t *pos
= cookie
;
114 int m4af_seek_null_cb(void *cookie
, int64_t off
, int whence
)
116 int64_t *pos
= cookie
;
117 *pos
= off
; /* XXX: we use only SEEK_SET */
121 int64_t m4af_tell_null_cb(void *cookie
)
123 return *((int64_t*)cookie
);
126 static m4af_io_callbacks_t m4af_null_io_callbacks
= {
127 0, m4af_write_null_cb
, m4af_seek_null_cb
, m4af_tell_null_cb
131 int64_t m4af_timestamp(void)
133 return (int64_t)(time(0)) + (((1970 - 1904) * 365) + 17) * 24 * 60 * 60;
138 * http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
141 uint32_t m4af_roundup(uint32_t n
)
154 int64_t m4af_tell(m4af_ctx_t
*ctx
)
157 if ((pos
= ctx
->io
.tell(ctx
->io_cookie
)) < 0)
158 ctx
->last_error
= M4AF_IO_ERROR
;
163 int m4af_set_pos(m4af_ctx_t
*ctx
, int64_t pos
)
166 if ((rc
= ctx
->io
.seek(ctx
->io_cookie
, pos
, SEEK_SET
)) < 0)
167 ctx
->last_error
= M4AF_IO_ERROR
;
172 int m4af_write(m4af_ctx_t
*ctx
, const void *data
, uint32_t size
)
175 if ((rc
= ctx
->io
.write(ctx
->io_cookie
, data
, size
)) < 0)
176 ctx
->last_error
= M4AF_IO_ERROR
;
181 int m4af_write16(m4af_ctx_t
*ctx
, uint32_t data
)
183 data
= m4af_htob16(data
);
184 return m4af_write(ctx
, &data
, 2);
188 int m4af_write32(m4af_ctx_t
*ctx
, uint32_t data
)
190 data
= m4af_htob32(data
);
191 return m4af_write(ctx
, &data
, 4);
195 int m4af_write64(m4af_ctx_t
*ctx
, uint64_t data
)
197 data
= m4af_htob64(data
);
198 return m4af_write(ctx
, &data
, 8);
202 int m4af_write24(m4af_ctx_t
*ctx
, uint32_t data
)
204 data
= m4af_htob32(data
<< 8);
205 return m4af_write(ctx
, &data
, 3);
209 void m4af_write32_at(m4af_ctx_t
*ctx
, int64_t pos
, uint32_t value
)
211 int64_t current_pos
= m4af_tell(ctx
);
212 m4af_set_pos(ctx
, pos
);
213 m4af_write32(ctx
, value
);
214 m4af_set_pos(ctx
, current_pos
);
217 m4af_ctx_t
*m4af_create(uint32_t codec
, uint32_t timescale
,
218 m4af_io_callbacks_t
*io
, void *io_cookie
)
223 if (codec
!= M4AF_FOURCC('m','p','4','a') &&
224 codec
!= M4AF_FOURCC('a','l','a','c'))
226 if ((ctx
= m4af_realloc(0, sizeof(m4af_ctx_t
))) == 0)
228 memset(ctx
, 0, sizeof(m4af_ctx_t
));
229 memcpy(&ctx
->io
, io
, sizeof(m4af_io_callbacks_t
));
230 ctx
->io_cookie
= io_cookie
;
231 ctx
->timescale
= timescale
;
232 timestamp
= m4af_timestamp();
233 ctx
->creation_time
= timestamp
;
234 ctx
->modification_time
= timestamp
;
236 ctx
->track
[0].codec
= codec
;
237 ctx
->track
[0].timescale
= timescale
;
238 ctx
->track
[0].creation_time
= timestamp
;
239 ctx
->track
[0].modification_time
= timestamp
;
240 ctx
->track
[0].num_channels
= 2;
245 void m4af_free_itmf_table(m4af_ctx_t
*ctx
)
248 m4af_itmf_entry_t
*entry
= ctx
->itmf_table
;
249 for (i
= 0; i
< ctx
->num_tags
; ++i
, ++entry
) {
250 if (entry
->fcc
== M4AF_FOURCC('-','-','-','-'))
251 m4af_free(entry
->name
);
252 m4af_free(entry
->data
);
254 m4af_free(ctx
->itmf_table
);
258 void m4af_clear_track(m4af_ctx_t
*ctx
, int track_idx
)
260 m4af_track_t
*track
= ctx
->track
+ track_idx
;
261 if (track
->decSpecificInfo
)
262 m4af_free(track
->decSpecificInfo
);
263 if (track
->sample_table
)
264 m4af_free(track
->sample_table
);
265 if (track
->chunk_table
)
266 m4af_free(track
->chunk_table
);
267 if (track
->chunk_buffer
)
268 m4af_free(track
->chunk_buffer
);
269 memset(track
, 0, sizeof(m4af_track_t
));
272 void m4af_teardown(m4af_ctx_t
**ctxp
)
275 m4af_ctx_t
*ctx
= *ctxp
;
276 for (i
= 0; i
< ctx
->num_tracks
; ++i
)
277 m4af_clear_track(ctx
, i
);
279 m4af_free_itmf_table(ctx
);
284 void m4af_set_num_channels(m4af_ctx_t
*ctx
, uint32_t track_idx
,
287 ctx
->track
[track_idx
].num_channels
= channels
;
290 void m4af_set_fixed_frame_duration(m4af_ctx_t
*ctx
, uint32_t track_idx
,
293 ctx
->track
[track_idx
].frame_duration
= length
;
296 int m4af_set_decoder_specific_info(m4af_ctx_t
*ctx
, uint32_t track_idx
,
297 uint8_t *data
, uint32_t size
)
299 m4af_track_t
*track
= &ctx
->track
[track_idx
];
300 if (size
> track
->decSpecificInfoSize
) {
301 uint8_t *memory
= m4af_realloc(track
->decSpecificInfo
, size
);
303 ctx
->last_error
= M4AF_NO_MEMORY
;
306 track
->decSpecificInfo
= memory
;
309 memcpy(track
->decSpecificInfo
, data
, size
);
310 track
->decSpecificInfoSize
= size
;
312 return ctx
->last_error
;
315 void m4af_set_vbr_mode(m4af_ctx_t
*ctx
, uint32_t track_idx
, int is_vbr
)
317 m4af_track_t
*track
= &ctx
->track
[track_idx
];
318 track
->is_vbr
= is_vbr
;
321 void m4af_set_priming(m4af_ctx_t
*ctx
, uint32_t track_idx
,
322 uint32_t encoder_delay
, uint32_t padding
)
324 m4af_track_t
*track
= &ctx
->track
[track_idx
];
325 track
->encoder_delay
= encoder_delay
;
326 track
->padding
= padding
;
329 void m4af_set_priming_mode(m4af_ctx_t
*ctx
, int mode
)
331 ctx
->priming_mode
= mode
;
335 int m4af_add_sample_entry(m4af_ctx_t
*ctx
, uint32_t track_idx
,
336 uint32_t size
, uint32_t delta
)
338 m4af_track_t
*track
= &ctx
->track
[track_idx
];
339 m4af_sample_entry_t
*entry
;
343 if (track
->num_samples
== track
->sample_table_capacity
) {
344 uint32_t new_size
= track
->sample_table_capacity
;
345 new_size
= new_size
? new_size
* 2 : 1;
346 entry
= m4af_realloc(track
->sample_table
, new_size
* sizeof(*entry
));
348 ctx
->last_error
= M4AF_NO_MEMORY
;
351 track
->sample_table
= entry
;
352 track
->sample_table_capacity
= new_size
;
354 entry
= track
->sample_table
+ track
->num_samples
;
356 entry
->delta
= delta
;
357 ++track
->num_samples
;
362 int m4af_flush_chunk(m4af_ctx_t
*ctx
, uint32_t track_idx
)
364 m4af_track_t
*track
= &ctx
->track
[track_idx
];
365 m4af_chunk_entry_t
*entry
;
366 if (!track
->num_chunks
|| !track
->chunk_size
)
368 entry
= &track
->chunk_table
[track
->num_chunks
- 1];
369 entry
->offset
= m4af_tell(ctx
);
370 m4af_write(ctx
, track
->chunk_buffer
, track
->chunk_size
);
371 ctx
->mdat_size
+= track
->chunk_size
;
372 track
->chunk_size
= 0;
373 return ctx
->last_error
? -1 : 0;
377 int m4af_add_chunk_entry(m4af_ctx_t
*ctx
, uint32_t track_idx
)
379 m4af_track_t
*track
= &ctx
->track
[track_idx
];
380 m4af_chunk_entry_t
*entry
;
381 if (track
->num_chunks
== track
->chunk_table_capacity
) {
382 uint32_t new_size
= track
->chunk_table_capacity
;
383 new_size
= new_size
? new_size
* 2 : 1;
384 entry
= m4af_realloc(track
->chunk_table
, new_size
* sizeof(*entry
));
386 ctx
->last_error
= M4AF_NO_MEMORY
;
389 track
->chunk_table
= entry
;
390 track
->chunk_table_capacity
= new_size
;
392 memset(&track
->chunk_table
[track
->num_chunks
++], 0,
393 sizeof(m4af_chunk_entry_t
));
398 int m4af_update_chunk_table(m4af_ctx_t
*ctx
, uint32_t track_idx
,
399 uint32_t size
, uint32_t delta
)
401 m4af_track_t
*track
= &ctx
->track
[track_idx
];
402 m4af_chunk_entry_t
*entry
;
403 int add_new_chunk
= 0;
407 if (track
->num_chunks
== 0)
410 entry
= &track
->chunk_table
[track
->num_chunks
- 1];
411 if (entry
->duration
+ delta
> track
->timescale
/ 2)
415 m4af_flush_chunk(ctx
, track_idx
);
416 if (m4af_add_chunk_entry(ctx
, track_idx
) < 0)
419 entry
= &track
->chunk_table
[track
->num_chunks
- 1];
421 ++entry
->samples_per_chunk
;
422 entry
->duration
+= delta
;
427 void m4af_update_max_bitrate(m4af_ctx_t
*ctx
, uint32_t track_idx
)
429 m4af_track_t
*track
= &ctx
->track
[track_idx
];
430 uint32_t duration
= 0, size
= 0, bitrate
;
431 m4af_sample_entry_t
*ent
= track
->sample_table
+ track
->num_samples
- 1;
433 for (; ent
>= track
->sample_table
&& duration
< track
->timescale
; --ent
) {
434 duration
+= ent
->delta
;
437 bitrate
= (uint32_t)(size
* 8.0 * track
->timescale
/ duration
+ .5);
438 if (bitrate
> track
->maxBitrate
)
439 track
->maxBitrate
= bitrate
;
443 int m4af_append_sample_to_chunk(m4af_ctx_t
*ctx
, uint32_t track_idx
,
444 const void *data
, uint32_t size
)
446 m4af_track_t
*track
= &ctx
->track
[track_idx
];
447 uint32_t newsize
= track
->chunk_size
+ size
;
451 if (track
->chunk_capacity
< newsize
) {
452 uint32_t capacity
= m4af_roundup(newsize
);
453 uint8_t *memory
= realloc(track
->chunk_buffer
, capacity
);
455 ctx
->last_error
= M4AF_NO_MEMORY
;
458 track
->chunk_buffer
= memory
;
459 track
->chunk_capacity
= capacity
;
461 memcpy(track
->chunk_buffer
+ track
->chunk_size
, data
, size
);
462 track
->chunk_size
= newsize
;
466 int m4af_write_sample(m4af_ctx_t
*ctx
, uint32_t track_idx
, const void *data
,
467 uint32_t size
, uint32_t duration
)
469 m4af_track_t
*track
= &ctx
->track
[track_idx
];
470 if (track
->frame_duration
)
471 duration
= track
->frame_duration
;
472 if (size
> track
->bufferSizeDB
)
473 track
->bufferSizeDB
= size
;
474 track
->duration
+= duration
;
475 m4af_add_sample_entry(ctx
, track_idx
, size
, duration
);
476 m4af_update_chunk_table(ctx
, track_idx
, size
, duration
);
477 m4af_update_max_bitrate(ctx
, track_idx
);
478 m4af_append_sample_to_chunk(ctx
, track_idx
, data
, size
);
479 return ctx
->last_error
;
483 m4af_itmf_entry_t
*m4af_find_itmf_slot(m4af_ctx_t
*ctx
, uint32_t fcc
,
486 m4af_itmf_entry_t
*entry
= ctx
->itmf_table
;
489 fcc
= M4AF_FOURCC('-','-','-','-');
491 if (fcc
!= M4AF_TAG_ARTWORK
)
492 for (; entry
!= ctx
->itmf_table
+ ctx
->num_tags
; ++entry
)
493 if (fcc
== entry
->fcc
&& (!name
|| !strcmp(name
, entry
->name
)))
496 if (ctx
->num_tags
== ctx
->itmf_table_capacity
) {
497 uint32_t new_size
= ctx
->itmf_table_capacity
;
498 new_size
= new_size
? new_size
* 2 : 1;
499 entry
= m4af_realloc(ctx
->itmf_table
, new_size
* sizeof(*entry
));
501 ctx
->last_error
= M4AF_NO_MEMORY
;
504 ctx
->itmf_table
= entry
;
505 ctx
->itmf_table_capacity
= new_size
;
507 entry
= &ctx
->itmf_table
[ctx
->num_tags
++];
508 memset(entry
, 0, sizeof(m4af_itmf_entry_t
));
511 char *name_copy
= m4af_realloc(0, strlen(name
) + 1);
513 ctx
->last_error
= M4AF_NO_MEMORY
;
517 strcpy(name_copy
, name
);
518 entry
->name
= name_copy
;
523 int m4af_add_itmf_long_tag(m4af_ctx_t
*ctx
, const char *name
,
526 m4af_itmf_entry_t
*entry
;
528 size_t name_len
= strlen(name
);
529 size_t data_len
= strlen(data
);
530 if (!name_len
|| !data_len
)
533 if ((entry
= m4af_find_itmf_slot(ctx
, 0, name
)) == 0)
535 entry
->type_code
= M4AF_UTF8
;
536 if ((data_copy
= m4af_realloc(entry
->data
, data_len
)) == 0) {
537 ctx
->last_error
= M4AF_NO_MEMORY
;
540 memcpy(data_copy
, data
, data_len
);
541 entry
->data
= data_copy
;
542 entry
->data_size
= data_len
;
545 return ctx
->last_error
;
548 int m4af_add_itmf_short_tag(m4af_ctx_t
*ctx
, uint32_t fcc
,
549 uint32_t type_code
, const void *data
,
552 m4af_itmf_entry_t
*entry
;
557 if ((entry
= m4af_find_itmf_slot(ctx
, fcc
, 0)) == 0)
559 entry
->type_code
= type_code
;
560 if ((data_copy
= m4af_realloc(entry
->data
, data_size
)) == 0) {
561 ctx
->last_error
= M4AF_NO_MEMORY
;
564 memcpy(data_copy
, data
, data_size
);
565 entry
->data
= data_copy
;
566 entry
->data_size
= data_size
;
569 return ctx
->last_error
;
572 int m4af_add_itmf_string_tag(m4af_ctx_t
*ctx
, uint32_t fcc
, const char *data
)
574 return m4af_add_itmf_short_tag(ctx
, fcc
, M4AF_UTF8
, data
, strlen(data
));
577 int m4af_add_itmf_int8_tag(m4af_ctx_t
*ctx
, uint32_t fcc
, int value
)
579 uint8_t data
= value
;
580 return m4af_add_itmf_short_tag(ctx
, fcc
, M4AF_INTEGER
, &data
, 1);
583 int m4af_add_itmf_int16_tag(m4af_ctx_t
*ctx
, uint32_t fcc
, int value
)
585 uint16_t data
= m4af_htob16(value
);
586 return m4af_add_itmf_short_tag(ctx
, fcc
, M4AF_INTEGER
, &data
, 2);
589 int m4af_add_itmf_int32_tag(m4af_ctx_t
*ctx
, uint32_t fcc
, uint32_t value
)
591 uint32_t data
= m4af_htob32(value
);
592 return m4af_add_itmf_short_tag(ctx
, fcc
, M4AF_INTEGER
, &data
, 4);
595 int m4af_add_itmf_int64_tag(m4af_ctx_t
*ctx
, uint32_t fcc
, uint64_t value
)
597 uint64_t data
= m4af_htob64(value
);
598 return m4af_add_itmf_short_tag(ctx
, fcc
, M4AF_INTEGER
, &data
, 8);
601 int m4af_add_itmf_track_tag(m4af_ctx_t
*ctx
, int track
, int total
)
603 uint16_t data
[4] = { 0 };
604 data
[1] = m4af_htob16(track
);
605 data
[2] = m4af_htob16(total
);
606 return m4af_add_itmf_short_tag(ctx
, M4AF_FOURCC('t','r','k','n'),
607 M4AF_IMPLICIT
, &data
, 8);
610 int m4af_add_itmf_disk_tag(m4af_ctx_t
*ctx
, int disk
, int total
)
612 uint16_t data
[3] = { 0 };
613 data
[1] = m4af_htob16(disk
);
614 data
[2] = m4af_htob16(total
);
615 return m4af_add_itmf_short_tag(ctx
, M4AF_FOURCC('d','i','s','k'),
616 M4AF_IMPLICIT
, &data
, 6);
619 int m4af_add_itmf_genre_tag(m4af_ctx_t
*ctx
, int genre
)
621 uint16_t data
= m4af_htob16(genre
);
622 return m4af_add_itmf_short_tag(ctx
, M4AF_FOURCC('g','n','r','e'),
623 M4AF_IMPLICIT
, &data
, 2);
627 int m4af_set_iTunSMPB(m4af_ctx_t
*ctx
)
629 const char *fmt
= " 00000000 %08X %08X %08X%08X 00000000 00000000 "
630 "00000000 00000000 00000000 00000000 00000000 00000000";
631 m4af_track_t
*track
= &ctx
->track
[0];
633 uint64_t length
= track
->duration
- track
->encoder_delay
- track
->padding
;
634 sprintf(buf
, fmt
, track
->encoder_delay
, track
->padding
,
635 (uint32_t)(length
>> 32), (uint32_t)length
);
636 return m4af_add_itmf_long_tag(ctx
, "iTunSMPB", buf
);
640 uint32_t m4af_update_box_size(m4af_ctx_t
*ctx
, int64_t pos
)
642 int64_t current_pos
= m4af_tell(ctx
);
643 m4af_set_pos(ctx
, pos
);
644 m4af_write32(ctx
, current_pos
- pos
);
645 m4af_set_pos(ctx
, current_pos
);
646 return current_pos
- pos
;
650 void m4af_write_descriptor(m4af_ctx_t
*ctx
, uint32_t tag
, uint32_t size
)
654 buf
[1] = ((size
>> 21) | 0x80);
655 buf
[2] = ((size
>> 14) | 0x80);
656 buf
[3] = ((size
>> 7) | 0x80);
657 buf
[4] = (size
& 0x7f);
658 m4af_write(ctx
, buf
, 5);
662 void m4af_write_ftyp_box(m4af_ctx_t
*ctx
)
664 m4af_write(ctx
, "\0\0\0\040""ftypM4A \0\0\0\0M4A mp42isom\0\0\0\0", 32);
668 void m4af_write_free_box(m4af_ctx_t
*ctx
, uint32_t size
)
670 int64_t pos
= m4af_tell(ctx
);
671 m4af_write32(ctx
, size
+ 8);
672 m4af_write(ctx
, "free", 4);
674 m4af_set_pos(ctx
, pos
+ size
+ 8);
677 int m4af_begin_write(m4af_ctx_t
*ctx
)
679 m4af_write_ftyp_box(ctx
);
680 m4af_write_free_box(ctx
, 0);
681 m4af_write(ctx
, "\0\0\0\0mdat", 8);
682 ctx
->mdat_pos
= m4af_tell(ctx
);
683 return ctx
->last_error
;
687 void m4af_write_stco_box(m4af_ctx_t
*ctx
, uint32_t track_idx
)
689 m4af_track_t
*track
= &ctx
->track
[track_idx
];
691 m4af_chunk_entry_t
*index
= track
->chunk_table
;
692 int is_co64
= index
[track
->num_chunks
- 1].offset
> 0xffffffff;
693 int64_t pos
= m4af_tell(ctx
);
695 m4af_write32(ctx
, 0); /* size */
696 m4af_write(ctx
, is_co64
? "co64" : "stco", 4);
697 m4af_write32(ctx
, 0); /* version and flags */
698 m4af_write32(ctx
, track
->num_chunks
);
699 for (i
= 0; i
< track
->num_chunks
; ++i
, ++index
) {
701 m4af_write64(ctx
, index
->offset
);
703 m4af_write32(ctx
, index
->offset
);
705 m4af_update_box_size(ctx
, pos
);
709 void m4af_write_stsz_box(m4af_ctx_t
*ctx
, uint32_t track_idx
)
711 m4af_track_t
*track
= &ctx
->track
[track_idx
];
712 m4af_sample_entry_t
*index
= track
->sample_table
;
714 int64_t pos
= m4af_tell(ctx
);
716 "\0\0\0\0" /* size */
720 "\0\0\0\0" /* sample_size: 0(variable) */
722 m4af_write32(ctx
, track
->num_samples
);
723 for (i
= 0; i
< track
->num_samples
; ++i
, ++index
)
724 m4af_write32(ctx
, index
->size
);
725 m4af_update_box_size(ctx
, pos
);
729 void m4af_write_stsc_box(m4af_ctx_t
*ctx
, uint32_t track_idx
)
731 m4af_track_t
*track
= &ctx
->track
[track_idx
];
732 m4af_chunk_entry_t
*index
= track
->chunk_table
;
733 uint32_t i
, prev_samples_per_chunk
= 0, entry_count
= 0;
734 int64_t pos
= m4af_tell(ctx
);
736 "\0\0\0\0" /* size */
740 "\0\0\0\0" /* entry_count */
743 for (i
= 0; i
< track
->num_chunks
; ++i
, ++index
) {
744 if (index
->samples_per_chunk
!= prev_samples_per_chunk
) {
746 m4af_write32(ctx
, i
+ 1);
747 m4af_write32(ctx
, index
->samples_per_chunk
);
748 m4af_write32(ctx
, 1); /* sample_description_index */
749 prev_samples_per_chunk
= index
->samples_per_chunk
;
752 m4af_write32_at(ctx
, pos
+ 12, entry_count
);
753 m4af_update_box_size(ctx
, pos
);
757 void m4af_write_stts_box(m4af_ctx_t
*ctx
, uint32_t track_idx
)
759 m4af_track_t
*track
= &ctx
->track
[track_idx
];
760 m4af_sample_entry_t
*index
= track
->sample_table
;
761 uint32_t i
, prev_delta
= 0, entry_count
= 0, sample_count
= 0;
762 int64_t pos
= m4af_tell(ctx
);
764 "\0\0\0\0" /* size */
768 "\0\0\0\0" /* entry_count */
771 for (i
= 0; i
< track
->num_samples
; ++i
, ++index
) {
772 if (index
->delta
== prev_delta
)
777 m4af_write32(ctx
, sample_count
);
778 m4af_write32(ctx
, prev_delta
);
780 prev_delta
= index
->delta
;
785 m4af_write32(ctx
, sample_count
);
786 m4af_write32(ctx
, prev_delta
);
788 m4af_write32_at(ctx
, pos
+ 12, entry_count
);
789 m4af_update_box_size(ctx
, pos
);
793 void m4af_write_esds_box(m4af_ctx_t
*ctx
, uint32_t track_idx
)
795 m4af_track_t
*track
= &ctx
->track
[track_idx
];
796 int64_t pos
= m4af_tell(ctx
);
797 m4af_write(ctx
, "\0\0\0\0esds", 8);
798 m4af_write32(ctx
, 0); /* version + flags */
801 m4af_write_descriptor(ctx
, 3, 32 + track
->decSpecificInfoSize
);
802 m4af_write(ctx
, "\0\0\0", 3);
803 /* DecoderConfigDescriptor */
804 m4af_write_descriptor(ctx
, 4, 18 + track
->decSpecificInfoSize
);
806 "\x40" /* objectTypeIndication: 0x40(Audio ISO/IEC 14496-3)*/
807 "\x15" /* streamType(6): 0x05(AudioStream)
812 m4af_write24(ctx
, track
->bufferSizeDB
);
813 m4af_write32(ctx
, track
->maxBitrate
);
814 m4af_write32(ctx
, track
->is_vbr
? 0: track
->avgBitrate
);
815 /* DecoderSpecificInfo */
816 m4af_write_descriptor(ctx
, 5, track
->decSpecificInfoSize
);
817 m4af_write(ctx
, track
->decSpecificInfo
, track
->decSpecificInfoSize
);
818 /* SLConfigDescriptor */
819 m4af_write_descriptor(ctx
, 6, 1);
820 m4af_write(ctx
, "\002", 1); /* predefined */
822 m4af_update_box_size(ctx
, pos
);
826 void m4af_write_alac_box(m4af_ctx_t
*ctx
, uint32_t track_idx
)
828 m4af_track_t
*track
= &ctx
->track
[track_idx
];
829 int64_t pos
= m4af_tell(ctx
);
831 "\0\0\0\0" /* size */
836 m4af_write(ctx
, track
->decSpecificInfo
, track
->decSpecificInfoSize
);
837 m4af_update_box_size(ctx
, pos
);
841 void m4af_write_mp4a_box(m4af_ctx_t
*ctx
, uint32_t track_idx
)
843 m4af_track_t
*track
= &ctx
->track
[track_idx
];
844 int64_t pos
= m4af_tell(ctx
);
845 m4af_write32(ctx
, 0); /* size */
846 m4af_write32(ctx
, track
->codec
); /* mp4a or alac */
848 "\0\0\0\0\0\0" /* reserved */
849 "\0\001" /* data_reference_index: 1 */
850 "\0\0\0\0" /* reserved[0] */
851 "\0\0\0\0" /* reserved[1] */
853 m4af_write16(ctx
, track
->num_channels
);
855 "\0\020" /* samplesize: 16 */
856 "\0\0" /* pre_defined */
857 "\0\0" /* reserved */
859 if (track
->codec
== M4AF_FOURCC('m','p','4','a')) {
860 m4af_write32(ctx
, track
->timescale
<< 16);
861 m4af_write_esds_box(ctx
, track_idx
);
863 m4af_write32(ctx
, 44100 << 16);
864 m4af_write_alac_box(ctx
, track_idx
);
866 m4af_update_box_size(ctx
, pos
);
870 void m4af_write_stsd_box(m4af_ctx_t
*ctx
, uint32_t track_idx
)
872 int64_t pos
= m4af_tell(ctx
);
873 m4af_write(ctx
, "\0\0\0\0stsd", 8);
877 "\0\0\0\001" /* entry_count: 1 */
879 m4af_write_mp4a_box(ctx
, track_idx
);
880 m4af_update_box_size(ctx
, pos
);
884 void m4af_write_sbgp_box(m4af_ctx_t
*ctx
, uint32_t track_idx
)
886 m4af_track_t
*track
= &ctx
->track
[track_idx
];
888 "\0\0\0\034" /* size: 28 */
892 "roll" /* grouping_type */
893 "\0\0\0\001" /* entry_count: 1 */
895 m4af_write32(ctx
, track
->num_samples
);
896 m4af_write32(ctx
, 1); /* group_description_index */
900 void m4af_write_sgpd_box(m4af_ctx_t
*ctx
, uint32_t track_idx
)
903 "\0\0\0\026" /* size: 22 */
907 "roll" /* grouping_type */
908 "\0\0\0\001" /* entry_count: 1 */
909 "\377\377" /* payload_data: -1 */
914 void m4af_write_stbl_box(m4af_ctx_t
*ctx
, uint32_t track_idx
)
916 m4af_track_t
*track
= &ctx
->track
[track_idx
];
917 int64_t pos
= m4af_tell(ctx
);
918 m4af_write(ctx
, "\0\0\0\0stbl", 8);
919 m4af_write_stsd_box(ctx
, track_idx
);
920 if ((ctx
->priming_mode
& M4AF_PRIMING_MODE_EDTS
) &&
921 (track
->encoder_delay
|| track
->padding
)) {
922 m4af_write_sbgp_box(ctx
, track_idx
);
923 m4af_write_sgpd_box(ctx
, track_idx
);
925 m4af_write_stts_box(ctx
, track_idx
);
926 m4af_write_stsc_box(ctx
, track_idx
);
927 m4af_write_stsz_box(ctx
, track_idx
);
928 m4af_write_stco_box(ctx
, track_idx
);
929 m4af_update_box_size(ctx
, pos
);
933 void m4af_write_url_box(m4af_ctx_t
*ctx
, uint32_t track_idx
)
936 "\0\0\0\014" /* size */
939 "\0\0\001" /* flags: 1(in the same file) */
944 void m4af_write_dref_box(m4af_ctx_t
*ctx
, uint32_t track_idx
)
946 int64_t pos
= m4af_tell(ctx
);
947 m4af_write(ctx
, "\0\0\0\0dref", 8);
951 "\0\0\0\001" /* entry_count: 1 */
953 m4af_write_url_box(ctx
, track_idx
);
954 m4af_update_box_size(ctx
, pos
);
958 void m4af_write_dinf_box(m4af_ctx_t
*ctx
, uint32_t track_idx
)
960 int64_t pos
= m4af_tell(ctx
);
961 m4af_write(ctx
, "\0\0\0\0dinf", 8);
962 m4af_write_dref_box(ctx
, track_idx
);
963 m4af_update_box_size(ctx
, pos
);
967 void m4af_write_smhd_box(m4af_ctx_t
*ctx
, uint32_t track_idx
)
970 "\0\0\0\020" /* size */
975 "\0\0" /* reserved */
980 void m4af_write_minf_box(m4af_ctx_t
*ctx
, uint32_t track_idx
)
982 m4af_track_t
*track
= &ctx
->track
[track_idx
];
983 int64_t pos
= m4af_tell(ctx
);
984 m4af_write(ctx
, "\0\0\0\0minf", 8);
985 /* TODO: add TEXT support */
986 if (track
->codec
!= M4AF_CODEC_TEXT
)
987 m4af_write_smhd_box(ctx
, track_idx
);
988 m4af_write_dinf_box(ctx
, track_idx
);
989 m4af_write_stbl_box(ctx
, track_idx
);
990 m4af_update_box_size(ctx
, pos
);
994 void m4af_write_mdhd_box(m4af_ctx_t
*ctx
, uint32_t track_idx
)
996 m4af_track_t
*track
= &ctx
->track
[track_idx
];
997 int64_t pos
= m4af_tell(ctx
);
998 uint8_t version
= (track
->creation_time
> UINT32_MAX
||
999 track
->modification_time
> UINT32_MAX
||
1000 track
->duration
> UINT32_MAX
);
1002 m4af_write(ctx
, "\0\0\0\0mdhd", 8);
1003 m4af_write(ctx
, &version
, 1);
1004 m4af_write(ctx
, "\0\0\0", 3); /* flags */
1006 m4af_write64(ctx
, track
->creation_time
);
1007 m4af_write64(ctx
, track
->modification_time
);
1008 m4af_write32(ctx
, track
->timescale
);
1009 m4af_write64(ctx
, track
->duration
);
1011 m4af_write32(ctx
, track
->creation_time
);
1012 m4af_write32(ctx
, track
->modification_time
);
1013 m4af_write32(ctx
, track
->timescale
);
1014 m4af_write32(ctx
, track
->duration
);
1017 "\x55\xc4" /* language: und */
1018 "\0\0" /* pre_defined */
1020 m4af_update_box_size(ctx
, pos
);
1024 void m4af_write_hdlr_box(m4af_ctx_t
*ctx
, uint32_t track_idx
, const char *type
)
1026 int64_t pos
= m4af_tell(ctx
);
1027 static const char reserved_and_name
[10] = { 0 };
1030 "\0\0\0\0" /* size */
1033 "\0\0\0" /* flags */
1034 "\0\0\0\0" /* pre_defined */
1036 m4af_write(ctx
, type
, 4); /* handler_type */
1038 m4af_write(ctx
, !strcmp(type
, "mdir") ? "appl" : "\0\0\0\0", 4);
1039 /* reserved[1], reserved[2], name */
1040 m4af_write(ctx
, reserved_and_name
, 9);
1041 m4af_update_box_size(ctx
, pos
);
1045 void m4af_write_mdia_box(m4af_ctx_t
*ctx
, uint32_t track_idx
)
1047 m4af_track_t
*track
= &ctx
->track
[track_idx
];
1049 (track
->codec
== M4AF_CODEC_TEXT
) ? "text" : "soun";
1050 int64_t pos
= m4af_tell(ctx
);
1051 m4af_write(ctx
, "\0\0\0\0mdia", 8);
1052 m4af_write_mdhd_box(ctx
, track_idx
);
1053 m4af_write_hdlr_box(ctx
, track_idx
, hdlr
);
1054 m4af_write_minf_box(ctx
, track_idx
);
1055 m4af_update_box_size(ctx
, pos
);
1059 void m4af_write_elst_box(m4af_ctx_t
*ctx
, uint32_t track_idx
)
1061 m4af_track_t
*track
= &ctx
->track
[track_idx
];
1063 int64_t duration
= track
->duration
- track
->encoder_delay
- track
->padding
;
1064 int64_t pos
= m4af_tell(ctx
);
1065 duration
= (double)duration
/ track
->timescale
* ctx
->timescale
+ .5;
1066 version
= (duration
> UINT32_MAX
);
1068 m4af_write(ctx
, "\0\0\0\0elst", 8);
1069 m4af_write(ctx
, &version
, 1);
1070 m4af_write(ctx
, "\0\0\0", 3); /* flags */
1071 m4af_write32(ctx
, 1); /* entry_count: 1 */
1073 m4af_write64(ctx
, duration
);
1074 m4af_write64(ctx
, track
->encoder_delay
);
1076 m4af_write32(ctx
, duration
);
1077 m4af_write32(ctx
, track
->encoder_delay
);
1079 m4af_write16(ctx
, 1); /* media_rate_integer */
1080 m4af_write16(ctx
, 0); /* media_rate_fraction */
1081 m4af_update_box_size(ctx
, pos
);
1085 void m4af_write_edts_box(m4af_ctx_t
*ctx
, uint32_t track_idx
)
1087 int64_t pos
= m4af_tell(ctx
);
1088 m4af_write(ctx
, "\0\0\0\0edts", 8);
1089 m4af_write_elst_box(ctx
, track_idx
);
1090 m4af_update_box_size(ctx
, pos
);
1094 void m4af_write_tkhd_box(m4af_ctx_t
*ctx
, uint32_t track_idx
)
1096 m4af_track_t
*track
= &ctx
->track
[track_idx
];
1097 int64_t pos
= m4af_tell(ctx
);
1099 (double)track
->duration
/ track
->timescale
* ctx
->timescale
+ .5;
1100 uint8_t version
= (track
->creation_time
> UINT32_MAX
||
1101 track
->modification_time
> UINT32_MAX
||
1102 duration
> UINT32_MAX
);
1103 m4af_write(ctx
, "\0\0\0\0tkhd", 8);
1104 m4af_write(ctx
, &version
, 1);
1105 m4af_write(ctx
, "\0\0\007", 3); /* flags */
1107 m4af_write64(ctx
, track
->creation_time
);
1108 m4af_write64(ctx
, track
->modification_time
);
1109 m4af_write32(ctx
, track_idx
+ 1);
1110 m4af_write(ctx
, "\0\0\0\0" /* reserved */
1112 m4af_write64(ctx
, duration
);
1114 m4af_write32(ctx
, track
->creation_time
);
1115 m4af_write32(ctx
, track
->modification_time
);
1116 m4af_write32(ctx
, track_idx
+ 1);
1117 m4af_write(ctx
, "\0\0\0\0" /* reserved */
1119 m4af_write32(ctx
, duration
);
1122 "\0\0\0\0" /* reserved[0] */
1123 "\0\0\0\0" /* reserved[1] */
1125 "\0\0" /* alternate_group */
1126 "\001\0" /* volume: 1.0 */
1127 "\0\0" /* reserved */
1128 "\0\001\0\0" /* matrix[0] */
1129 "\0\0\0\0" /* matrix[1] */
1130 "\0\0\0\0" /* matrix[2] */
1131 "\0\0\0\0" /* matrix[3] */
1132 "\0\001\0\0" /* matrix[4] */
1133 "\0\0\0\0" /* matrix[5] */
1134 "\0\0\0\0" /* matrix[6] */
1135 "\0\0\0\0" /* matrix[7] */
1136 "\100\0\0\0" /* matrix[8] */
1137 "\0\0\0\0" /* width */
1138 "\0\0\0\0" /* height */
1140 m4af_update_box_size(ctx
, pos
);
1144 void m4af_write_trak_box(m4af_ctx_t
*ctx
, uint32_t track_idx
)
1146 m4af_track_t
*track
= &ctx
->track
[track_idx
];
1147 int64_t pos
= m4af_tell(ctx
);
1148 m4af_write(ctx
, "\0\0\0\0trak", 8);
1149 m4af_write_tkhd_box(ctx
, track_idx
);
1150 if ((ctx
->priming_mode
& M4AF_PRIMING_MODE_EDTS
) &&
1151 (track
->encoder_delay
|| track
->padding
))
1152 m4af_write_edts_box(ctx
, track_idx
);
1153 m4af_write_mdia_box(ctx
, track_idx
);
1154 m4af_update_box_size(ctx
, pos
);
1158 int64_t m4af_movie_duration(m4af_ctx_t
*ctx
)
1160 int64_t movie_duration
= 0;
1162 for (i
= 0; i
< ctx
->num_tracks
; ++i
) {
1163 double x
= ctx
->track
[i
].duration
;
1164 int64_t duration
= x
/ ctx
->track
[i
].timescale
* ctx
->timescale
+ .5;
1165 if (duration
> movie_duration
)
1166 movie_duration
= duration
;
1168 return movie_duration
;
1172 void m4af_write_mvhd_box(m4af_ctx_t
*ctx
)
1174 int64_t pos
= m4af_tell(ctx
);
1175 int64_t movie_duration
= m4af_movie_duration(ctx
);
1176 uint8_t version
= (ctx
->creation_time
> UINT32_MAX
||
1177 ctx
->modification_time
> UINT32_MAX
||
1178 movie_duration
> UINT32_MAX
);
1180 m4af_write(ctx
, "\0\0\0\0mvhd", 8);
1181 m4af_write(ctx
, &version
, 1);
1182 m4af_write(ctx
, "\0\0\0", 3); /* flags */
1184 m4af_write64(ctx
, ctx
->creation_time
);
1185 m4af_write64(ctx
, ctx
->modification_time
);
1186 m4af_write32(ctx
, ctx
->timescale
);
1187 m4af_write64(ctx
, movie_duration
);
1189 m4af_write32(ctx
, ctx
->creation_time
);
1190 m4af_write32(ctx
, ctx
->modification_time
);
1191 m4af_write32(ctx
, ctx
->timescale
);
1192 m4af_write32(ctx
, movie_duration
);
1195 "\0\001\0\0" /* rate: 1.0 */
1196 "\001\0" /* volume: 1.0 */
1197 "\0\0" /* reserved */
1198 "\0\0\0\0" /* reserved[0] */
1199 "\0\0\0\0" /* reserved[1] */
1200 "\0\001\0\0" /* matrix[0] */
1201 "\0\0\0\0" /* matrix[1] */
1202 "\0\0\0\0" /* matrix[2] */
1203 "\0\0\0\0" /* matrix[3] */
1204 "\0\001\0\0" /* matrix[4] */
1205 "\0\0\0\0" /* matrix[5] */
1206 "\0\0\0\0" /* matrix[6] */
1207 "\0\0\0\0" /* matrix[7] */
1208 "\100\0\0\0" /* matrix[8] */
1209 "\0\0\0\0" /* pre_defined[0] */
1210 "\0\0\0\0" /* pre_defined[1] */
1211 "\0\0\0\0" /* pre_defined[2] */
1212 "\0\0\0\0" /* pre_defined[3] */
1213 "\0\0\0\0" /* pre_defined[4] */
1214 "\0\0\0\0" /* pre_defined[5] */
1216 m4af_write32(ctx
, ctx
->num_tracks
+ 1);
1217 m4af_update_box_size(ctx
, pos
);
1221 void m4af_write_mean_box(m4af_ctx_t
*ctx
)
1224 "\0\0\0\034" /* size */
1227 "\0\0\0" /* flags */
1228 "com.apple.iTunes" /* meaning-string */
1233 void m4af_write_name_box(m4af_ctx_t
*ctx
, const char *name
)
1235 int64_t pos
= m4af_tell(ctx
);
1237 "\0\0\0\0" /* size */
1240 "\0\0\0" /* flags */
1242 m4af_write(ctx
, name
, strlen(name
));
1243 m4af_update_box_size(ctx
, pos
);
1247 void m4af_write_data_box(m4af_ctx_t
*ctx
, uint32_t type_code
,
1248 const char *data
, uint32_t data_size
)
1250 int64_t pos
= m4af_tell(ctx
);
1251 uint8_t code
= type_code
;
1253 "\0\0\0\0" /* size */
1255 "\0\0" /* reserved */
1256 "\0" /* type_set_indifier */
1258 m4af_write(ctx
, &code
, 1);
1259 m4af_write(ctx
, "\0\0\0\0", 4); /* locale */
1260 m4af_write(ctx
, data
, data_size
);
1261 m4af_update_box_size(ctx
, pos
);
1265 void m4af_write_metadata(m4af_ctx_t
*ctx
, m4af_itmf_entry_t
*entry
)
1267 int64_t pos
= m4af_tell(ctx
);
1268 m4af_write(ctx
, "\0\0\0\0", 4);
1269 m4af_write32(ctx
, entry
->fcc
);
1270 if (entry
->fcc
!= M4AF_FOURCC('-','-','-','-'))
1271 m4af_write_data_box(ctx
, entry
->type_code
,
1272 entry
->data
, entry
->data_size
);
1274 m4af_write_mean_box(ctx
);
1275 m4af_write_name_box(ctx
, entry
->name
);
1276 m4af_write_data_box(ctx
, 1, entry
->data
, entry
->data_size
);
1278 m4af_update_box_size(ctx
, pos
);
1282 void m4af_write_ilst_box(m4af_ctx_t
*ctx
)
1285 int64_t pos
= m4af_tell(ctx
);
1286 m4af_write(ctx
, "\0\0\0\0ilst", 8);
1287 for (i
= 0; i
< ctx
->num_tags
; ++i
)
1288 m4af_write_metadata(ctx
, &ctx
->itmf_table
[i
]);
1289 m4af_update_box_size(ctx
, pos
);
1293 void m4af_write_meta_box(m4af_ctx_t
*ctx
)
1295 int64_t pos
= m4af_tell(ctx
);
1297 "\0\0\0\0" /* size */
1300 "\0\0\0" /* flags */
1302 m4af_write_hdlr_box(ctx
, 0, "mdir");
1303 m4af_write_ilst_box(ctx
);
1304 m4af_update_box_size(ctx
, pos
);
1308 void m4af_write_udta_box(m4af_ctx_t
*ctx
)
1310 int64_t pos
= m4af_tell(ctx
);
1311 m4af_write(ctx
, "\0\0\0\0udta", 8);
1312 m4af_write_meta_box(ctx
);
1313 m4af_update_box_size(ctx
, pos
);
1317 uint32_t m4af_write_moov_box(m4af_ctx_t
*ctx
)
1320 int64_t pos
= m4af_tell(ctx
);
1321 m4af_write(ctx
, "\0\0\0\0moov", 8);
1322 m4af_write_mvhd_box(ctx
);
1323 for (i
= 0; i
< ctx
->num_tracks
; ++i
)
1324 m4af_write_trak_box(ctx
, i
);
1326 m4af_write_udta_box(ctx
);
1327 return m4af_update_box_size(ctx
, pos
);
1331 void m4af_finalize_mdat(m4af_ctx_t
*ctx
)
1333 if (ctx
->mdat_size
+ 8 > UINT32_MAX
) {
1334 m4af_set_pos(ctx
, ctx
->mdat_pos
- 16);
1335 m4af_write32(ctx
, 1);
1336 m4af_write(ctx
, "mdat", 4);
1337 m4af_write64(ctx
, ctx
->mdat_size
+ 16);
1339 m4af_set_pos(ctx
, ctx
->mdat_pos
- 8);
1340 m4af_write32(ctx
, ctx
->mdat_size
+ 8);
1342 m4af_set_pos(ctx
, ctx
->mdat_pos
+ ctx
->mdat_size
);
1346 uint64_t m4af_patch_moov(m4af_ctx_t
*ctx
, uint32_t moov_size
, uint32_t offset
)
1349 uint32_t moov_size2
;
1351 m4af_io_callbacks_t io_reserve
= ctx
->io
;
1352 void *io_cookie_reserve
= ctx
->io_cookie
;
1354 for (i
= 0; i
< ctx
->num_tracks
; ++i
)
1355 for (j
= 0; j
< ctx
->track
[i
].num_chunks
; ++j
)
1356 ctx
->track
[i
].chunk_table
[j
].offset
+= offset
;
1358 ctx
->io
= m4af_null_io_callbacks
;
1359 ctx
->io_cookie
= &pos
;
1360 moov_size2
= m4af_write_moov_box(ctx
);
1362 if (moov_size2
!= moov_size
) {
1363 /* stco -> co64 switching */
1364 for (i
= 0; i
< ctx
->num_tracks
; ++i
)
1365 for (j
= 0; j
< ctx
->track
[i
].num_chunks
; ++j
)
1366 ctx
->track
[i
].chunk_table
[j
].offset
+= moov_size2
- moov_size
;
1367 moov_size2
= m4af_write_moov_box(ctx
);
1369 ctx
->io
= io_reserve
;
1370 ctx
->io_cookie
= io_cookie_reserve
;
1375 void m4af_shift_mdat_pos(m4af_ctx_t
*ctx
, uint32_t offset
)
1381 buf
= malloc(1024*1024*2);
1383 end
= ctx
->mdat_pos
+ ctx
->mdat_size
;
1384 for (; (begin
= m4af_max(ctx
->mdat_pos
, end
- 1024*1024*2)) < end
;
1386 m4af_set_pos(ctx
, begin
);
1387 ctx
->io
.read(ctx
->io_cookie
, buf
, end
- begin
);
1388 m4af_set_pos(ctx
, begin
+ offset
);
1389 m4af_write(ctx
, buf
, end
- begin
);
1391 ctx
->mdat_pos
+= offset
;
1392 m4af_set_pos(ctx
, ctx
->mdat_pos
- 16);
1393 m4af_write_free_box(ctx
, 0);
1394 m4af_write(ctx
, "\0\0\0\0mdat", 8);
1395 m4af_finalize_mdat(ctx
);
1400 int m4af_finalize(m4af_ctx_t
*ctx
, int optimize
)
1403 m4af_track_t
*track
;
1406 for (i
= 0; i
< ctx
->num_tracks
; ++i
) {
1407 track
= ctx
->track
+ i
;
1408 if (track
->duration
) {
1409 int64_t track_size
= 0;
1411 for (j
= 0; j
< track
->num_chunks
; ++j
)
1412 track_size
+= track
->chunk_table
[j
].size
;
1414 8.0 * track_size
* track
->timescale
/ track
->duration
+ .5;
1416 m4af_flush_chunk(ctx
, i
);
1419 if ((ctx
->priming_mode
& M4AF_PRIMING_MODE_ITUNSMPB
) &&
1420 (track
->encoder_delay
|| track
->padding
))
1421 m4af_set_iTunSMPB(ctx
);
1422 m4af_finalize_mdat(ctx
);
1423 moov_size
= m4af_write_moov_box(ctx
);
1426 uint32_t moov_size2
= m4af_patch_moov(ctx
, moov_size
, moov_size
+ 1024);
1427 m4af_shift_mdat_pos(ctx
, moov_size2
+ 1024);
1428 m4af_set_pos(ctx
, 32);
1429 m4af_write_moov_box(ctx
);
1430 pos
= m4af_tell(ctx
);
1431 m4af_write_free_box(ctx
, ctx
->mdat_pos
- pos
- 24);
1433 return ctx
->last_error
;