]>
iEval git - fdkaac.git/blob - src/m4af.c
2 * Copyright (C) 2013 nu774
3 * For conditions of distribution and use, see copyright notice in COPYING
16 #include "m4af_endian.h"
18 #define m4af_realloc(memory,size) realloc(memory, size)
19 #define m4af_free(memory) free(memory)
21 typedef struct m4af_sample_entry_t
{
24 } m4af_sample_entry_t
;
26 typedef struct m4af_chunk_entry_t
{
29 uint32_t samples_per_chunk
;
33 typedef struct m4af_itmf_entry_t
{
34 uint32_t type
; /* fcc */
43 typedef struct m4af_track_t
{
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
;
58 m4af_sample_entry_t
*sample_table
;
60 uint32_t sample_table_capacity
;
62 m4af_chunk_entry_t
*chunk_table
;
64 uint32_t chunk_table_capacity
;
66 uint8_t *chunk_buffer
;
68 uint32_t chunk_capacity
;
71 struct m4af_writer_t
{
73 int64_t creation_time
;
74 int64_t modification_time
;
79 m4af_itmf_entry_t
*itmf_table
;
81 uint32_t itmf_table_capacity
;
83 m4af_io_callbacks_t io
;
87 m4af_track_t track
[1];
91 int64_t m4af_timestamp(void)
93 return (int64_t)(time(0)) + (((1970 - 1904) * 365) + 17) * 24 * 60 * 60;
97 * http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
100 uint32_t m4af_roundup(uint32_t n
)
113 int64_t m4af_tell(m4af_writer_t
*ctx
)
116 if (!ctx
->last_error
&& (pos
= ctx
->io
.tell(ctx
->io_cookie
)) < 0)
117 ctx
->last_error
= M4AF_IO_ERROR
;
122 int m4af_set_pos(m4af_writer_t
*ctx
, int64_t pos
)
125 if (!ctx
->last_error
&&
126 (rc
= ctx
->io
.seek(ctx
->io_cookie
, pos
, SEEK_SET
)) < 0)
127 ctx
->last_error
= M4AF_IO_ERROR
;
132 int m4af_write(m4af_writer_t
*ctx
, const void *data
, uint32_t size
)
135 if (!ctx
->last_error
&&
136 (rc
= ctx
->io
.write(ctx
->io_cookie
, data
, size
)) < 0)
137 ctx
->last_error
= M4AF_IO_ERROR
;
142 int m4af_write32(m4af_writer_t
*ctx
, uint32_t data
)
144 data
= m4af_htob32(data
);
145 return m4af_write(ctx
, &data
, 4);
149 int m4af_write64(m4af_writer_t
*ctx
, uint64_t data
)
151 data
= m4af_htob64(data
);
152 return m4af_write(ctx
, &data
, 8);
156 int m4af_write24(m4af_writer_t
*ctx
, uint32_t data
)
158 data
= m4af_htob32(data
<< 8);
159 return m4af_write(ctx
, &data
, 3);
163 void m4af_write32_at(m4af_writer_t
*ctx
, int64_t pos
, uint32_t value
)
165 int64_t current_pos
= m4af_tell(ctx
);
166 m4af_set_pos(ctx
, pos
);
167 m4af_write32(ctx
, value
);
168 m4af_set_pos(ctx
, current_pos
);
171 m4af_writer_t
*m4af_create(uint32_t codec
, uint32_t timescale
,
172 m4af_io_callbacks_t
*io
, void *io_cookie
)
177 if (codec
!= M4AF_FOURCC('m','p','4','a') &&
178 codec
!= M4AF_FOURCC('a','l','a','c'))
180 if ((ctx
= m4af_realloc(0, sizeof(m4af_writer_t
))) == 0)
182 memset(ctx
, 0, sizeof(m4af_writer_t
));
183 memcpy(&ctx
->io
, io
, sizeof(m4af_io_callbacks_t
));
184 ctx
->io_cookie
= io_cookie
;
185 ctx
->timescale
= timescale
;
186 timestamp
= m4af_timestamp();
187 ctx
->creation_time
= timestamp
;
188 ctx
->modification_time
= timestamp
;
190 ctx
->track
[0].codec
= codec
;
191 ctx
->track
[0].timescale
= timescale
;
192 ctx
->track
[0].creation_time
= timestamp
;
193 ctx
->track
[0].modification_time
= timestamp
;
197 void m4af_set_fixed_frame_duration(m4af_writer_t
*ctx
, int track_idx
,
200 ctx
->track
[track_idx
].frame_duration
= length
;
203 int m4af_decoder_specific_info(m4af_writer_t
*ctx
, int track_idx
,
204 uint8_t *data
, uint32_t size
)
206 m4af_track_t
*track
= &ctx
->track
[track_idx
];
207 if (size
> track
->decSpecificInfoSize
) {
208 uint8_t *memory
= m4af_realloc(track
->decSpecificInfo
, size
);
210 ctx
->last_error
= M4AF_NO_MEMORY
;
213 track
->decSpecificInfo
= memory
;
216 memcpy(track
->decSpecificInfo
, data
, size
);
217 track
->decSpecificInfoSize
= size
;
221 void m4af_set_priming(m4af_writer_t
*ctx
, int track_idx
,
222 uint32_t encoder_delay
, uint32_t padding
)
224 m4af_track_t
*track
= &ctx
->track
[track_idx
];
225 track
->encoder_delay
= encoder_delay
;
226 track
->padding
= padding
;
230 int m4af_add_sample_entry(m4af_writer_t
*ctx
, int track_idx
,
231 uint32_t size
, uint32_t delta
)
233 m4af_track_t
*track
= &ctx
->track
[track_idx
];
234 m4af_sample_entry_t
*entry
;
238 if (track
->num_samples
== track
->sample_table_capacity
) {
239 uint32_t new_size
= track
->sample_table_capacity
;
240 new_size
= new_size
? new_size
* 2 : 1;
241 entry
= m4af_realloc(track
->sample_table
, new_size
* sizeof(*entry
));
243 ctx
->last_error
= M4AF_NO_MEMORY
;
246 track
->sample_table
= entry
;
247 track
->sample_table_capacity
= new_size
;
249 entry
= track
->sample_table
+ track
->num_samples
;
251 entry
->delta
= delta
;
252 ++track
->num_samples
;
257 int m4af_flush_chunk(m4af_writer_t
*ctx
, int track_idx
)
259 m4af_track_t
*track
= &ctx
->track
[track_idx
];
260 m4af_chunk_entry_t
*entry
;
261 if (!track
->num_chunks
|| !track
->chunk_size
)
263 entry
= &track
->chunk_table
[track
->num_chunks
- 1];
264 entry
->offset
= m4af_tell(ctx
);
265 m4af_write(ctx
, track
->chunk_buffer
, track
->chunk_size
);
266 ctx
->mdat_size
+= track
->chunk_size
;
267 track
->chunk_size
= 0;
268 return ctx
->last_error
? -1 : 0;
272 int m4af_update_chunk_table(m4af_writer_t
*ctx
, int track_idx
,
273 uint32_t size
, uint32_t delta
)
275 m4af_track_t
*track
= &ctx
->track
[track_idx
];
276 m4af_chunk_entry_t
*entry
;
277 int add_new_chunk
= 0;
281 if (track
->num_chunks
== 0)
284 entry
= &track
->chunk_table
[track
->num_chunks
- 1];
285 if (entry
->duration
+ delta
> track
->timescale
/ 2)
289 m4af_flush_chunk(ctx
, track_idx
);
290 if (track
->num_chunks
== track
->chunk_table_capacity
) {
291 uint32_t new_size
= track
->chunk_table_capacity
;
292 new_size
= new_size
? new_size
* 2 : 1;
293 entry
= m4af_realloc(track
->chunk_table
, new_size
* sizeof(*entry
));
295 ctx
->last_error
= M4AF_NO_MEMORY
;
298 track
->chunk_table
= entry
;
299 track
->chunk_table_capacity
= new_size
;
301 memset(&track
->chunk_table
[track
->num_chunks
++], 0,
302 sizeof(m4af_chunk_entry_t
));
304 entry
= &track
->chunk_table
[track
->num_chunks
- 1];
306 ++entry
->samples_per_chunk
;
307 entry
->duration
+= delta
;
312 void m4af_update_max_bitrate(m4af_writer_t
*ctx
, int track_idx
)
314 m4af_track_t
*track
= &ctx
->track
[track_idx
];
315 uint32_t duration
= 0, size
= 0, bitrate
;
316 m4af_sample_entry_t
*ent
= track
->sample_table
+ track
->num_samples
- 1;
318 for (; ent
>= track
->sample_table
&& duration
< track
->timescale
; --ent
) {
319 duration
+= ent
->delta
;
322 bitrate
= (uint32_t)(size
* 8.0 * track
->timescale
/ duration
+ .5);
323 if (bitrate
> track
->maxBitrate
)
324 track
->maxBitrate
= bitrate
;
328 int m4af_append_sample_to_chunk(m4af_writer_t
*ctx
, int track_idx
,
329 const void *data
, uint32_t size
)
331 m4af_track_t
*track
= &ctx
->track
[track_idx
];
332 uint32_t newsize
= track
->chunk_size
+ size
;
336 if (track
->chunk_capacity
< newsize
) {
337 uint32_t capacity
= m4af_roundup(newsize
);
338 uint8_t *memory
= realloc(track
->chunk_buffer
, capacity
);
340 ctx
->last_error
= M4AF_NO_MEMORY
;
343 track
->chunk_buffer
= memory
;
344 track
->chunk_capacity
= capacity
;
346 memcpy(track
->chunk_buffer
+ track
->chunk_size
, data
, size
);
347 track
->chunk_size
= newsize
;
351 int m4af_write_sample(m4af_writer_t
*ctx
, int track_idx
, const void *data
,
352 uint32_t size
, uint32_t duration
)
354 m4af_track_t
*track
= &ctx
->track
[track_idx
];
355 if (track
->frame_duration
)
356 duration
= track
->frame_duration
;
357 if (size
> track
->bufferSizeDB
)
358 track
->bufferSizeDB
= size
;
359 track
->duration
+= duration
;
360 m4af_add_sample_entry(ctx
, track_idx
, size
, duration
);
361 m4af_update_chunk_table(ctx
, track_idx
, size
, duration
);
362 m4af_update_max_bitrate(ctx
, track_idx
);
363 m4af_append_sample_to_chunk(ctx
, track_idx
, data
, size
);
364 return ctx
->last_error
? -1 : 0;
368 int m4af_add_itmf_entry(m4af_writer_t
*ctx
)
370 m4af_itmf_entry_t
*entry
;
371 if (ctx
->num_tags
== ctx
->itmf_table_capacity
) {
372 uint32_t new_size
= ctx
->itmf_table_capacity
;
373 new_size
= new_size
? new_size
* 2 : 1;
374 entry
= m4af_realloc(ctx
->itmf_table
, new_size
* sizeof(*entry
));
376 ctx
->last_error
= M4AF_NO_MEMORY
;
379 ctx
->itmf_table
= entry
;
380 ctx
->itmf_table_capacity
= new_size
;
386 int m4af_add_itmf_long_tag(m4af_writer_t
*ctx
, const char *name
,
389 m4af_itmf_entry_t
*entry
;
390 size_t name_len
= strlen(name
);
391 size_t data_len
= strlen(data
);
392 char *name_copy
= m4af_realloc(0, name_len
+ 1);
393 char *data_copy
= m4af_realloc(0, data_len
);
394 if (!name_copy
|| !data_copy
) {
395 ctx
->last_error
= M4AF_NO_MEMORY
;
398 if (m4af_add_itmf_entry(ctx
) < 0)
400 memcpy(name_copy
, name
, name_len
+ 1);
401 memcpy(data_copy
, data
, data_len
);
402 entry
= ctx
->itmf_table
+ ctx
->num_tags
- 1;
403 entry
->type
= M4AF_FOURCC('-','-','-','-');
404 entry
->u
.name
= name_copy
;
405 entry
->data
= data_copy
;
406 entry
->data_size
= data_len
;
410 m4af_free(name_copy
);
412 m4af_free(data_copy
);
416 int m4af_add_itmf_short_tag(m4af_writer_t
*ctx
, uint32_t type
,
417 uint32_t type_code
, const void *data
,
420 m4af_itmf_entry_t
*entry
;
421 char *data_copy
= m4af_realloc(0, data_size
);
423 ctx
->last_error
= M4AF_NO_MEMORY
;
426 if (m4af_add_itmf_entry(ctx
) < 0)
428 entry
= ctx
->itmf_table
+ ctx
->num_tags
- 1;
430 entry
->u
.type_code
= type_code
;
431 memcpy(data_copy
, data
, data_size
);
432 entry
->data
= data_copy
;
433 entry
->data_size
= data_size
;
437 m4af_free(data_copy
);
441 int m4af_add_itmf_string_tag(m4af_writer_t
*ctx
, uint32_t type
,
444 return m4af_add_itmf_short_tag(ctx
, type
, M4AF_UTF8
, data
, strlen(data
));
447 int m4af_add_itmf_int8_tag(m4af_writer_t
*ctx
, uint32_t type
, int value
)
449 uint8_t data
= value
;
450 return m4af_add_itmf_short_tag(ctx
, type
, M4AF_INTEGER
, &data
, 1);
453 int m4af_add_itmf_int16_tag(m4af_writer_t
*ctx
, uint32_t type
, int value
)
455 uint16_t data
= m4af_htob16(value
);
456 return m4af_add_itmf_short_tag(ctx
, type
, M4AF_INTEGER
, &data
, 2);
459 int m4af_add_itmf_int32_tag(m4af_writer_t
*ctx
, uint32_t type
, uint32_t value
)
461 uint32_t data
= m4af_htob32(value
);
462 return m4af_add_itmf_short_tag(ctx
, type
, M4AF_INTEGER
, &data
, 4);
465 int m4af_add_itmf_int64_tag(m4af_writer_t
*ctx
, uint32_t type
, uint64_t value
)
467 uint64_t data
= m4af_htob64(value
);
468 return m4af_add_itmf_short_tag(ctx
, type
, M4AF_INTEGER
, &data
, 8);
471 int m4af_add_itmf_track_tag(m4af_writer_t
*ctx
, int track
, int total
)
473 uint16_t data
[4] = { 0 };
474 data
[1] = m4af_htob16(track
);
475 data
[2] = m4af_htob16(total
);
476 return m4af_add_itmf_short_tag(ctx
, M4AF_FOURCC('t','r','k','n'),
477 M4AF_IMPLICIT
, &data
, 8);
480 int m4af_add_itmf_disk_tag(m4af_writer_t
*ctx
, int disk
, int total
)
482 uint16_t data
[3] = { 0 };
483 data
[1] = m4af_htob16(disk
);
484 data
[2] = m4af_htob16(total
);
485 return m4af_add_itmf_short_tag(ctx
, M4AF_FOURCC('d','i','s','k'),
486 M4AF_IMPLICIT
, &data
, 6);
489 int m4af_add_itmf_genre_tag(m4af_writer_t
*ctx
, int genre
)
491 uint16_t data
= m4af_htob16(genre
);
492 return m4af_add_itmf_short_tag(ctx
, M4AF_FOURCC('g','n','r','e'),
493 M4AF_IMPLICIT
, &data
, 2);
497 int m4af_set_iTunSMPB(m4af_writer_t
*ctx
)
499 const char *template = " 00000000 %08X %08X %08X%08X 00000000 00000000 "
500 "00000000 00000000 00000000 00000000 00000000 00000000";
501 m4af_track_t
*track
= &ctx
->track
[0];
503 uint64_t length
= track
->duration
- track
->encoder_delay
- track
->padding
;
504 sprintf(buf
, template, track
->encoder_delay
, track
->padding
,
505 (uint32_t)(length
>> 32), (uint32_t)length
);
506 return m4af_add_itmf_long_tag(ctx
, "iTunSMPB", buf
);
510 void m4af_update_size(m4af_writer_t
*ctx
, int64_t pos
)
512 int64_t current_pos
= m4af_tell(ctx
);
513 m4af_set_pos(ctx
, pos
);
514 m4af_write32(ctx
, current_pos
- pos
);
515 m4af_set_pos(ctx
, current_pos
);
519 int m4af_head_version(m4af_writer_t
*ctx
, int track_idx
)
521 m4af_track_t
*track
= &ctx
->track
[track_idx
];
522 return track
->duration
> UINT32_MAX
523 || track
->creation_time
> UINT32_MAX
524 || track
->modification_time
> UINT32_MAX
;
528 void m4af_descriptor(m4af_writer_t
*ctx
, uint32_t tag
, uint32_t size
)
532 buf
[1] = ((size
>> 21) | 0x80);
533 buf
[2] = ((size
>> 14) | 0x80);
534 buf
[3] = ((size
>> 7) | 0x80);
535 buf
[4] = (size
& 0x7f);
536 m4af_write(ctx
, buf
, 5);
540 void m4af_ftyp_box(m4af_writer_t
*ctx
)
542 m4af_write(ctx
, "\0\0\0\040""ftypM4A \0\0\0\0M4A mp42isom\0\0\0\0", 32);
546 void m4af_free_box(m4af_writer_t
*ctx
, uint32_t size
)
548 int64_t pos
= m4af_tell(ctx
);
549 m4af_write32(ctx
, size
+ 8);
550 m4af_write(ctx
, "free", 4);
552 m4af_set_pos(ctx
, pos
+ size
+ 8);
555 int m4af_begin_write(m4af_writer_t
*ctx
)
558 m4af_free_box(ctx
, 0);
559 m4af_write(ctx
, "\0\0\0\0mdat", 8);
560 ctx
->mdat_pos
= m4af_tell(ctx
);
561 return ctx
->last_error
? -1 : 0;
565 void m4af_stco_box(m4af_writer_t
*ctx
, int track_idx
)
567 m4af_track_t
*track
= &ctx
->track
[track_idx
];
569 m4af_chunk_entry_t
*index
= track
->chunk_table
;
570 int is_co64
= (ctx
->mdat_pos
+ ctx
->mdat_size
> UINT32_MAX
);
571 int64_t pos
= m4af_tell(ctx
);
573 m4af_write32(ctx
, 0); /* size */
574 m4af_write(ctx
, is_co64
? "co64" : "stco", 4);
575 m4af_write32(ctx
, 0); /* version and flags */
576 m4af_write32(ctx
, track
->num_chunks
);
577 for (i
= 0; i
< track
->num_chunks
; ++i
, ++index
) {
579 m4af_write64(ctx
, index
->offset
);
581 m4af_write32(ctx
, index
->offset
);
583 m4af_update_size(ctx
, pos
);
587 void m4af_stsz_box(m4af_writer_t
*ctx
, int track_idx
)
589 m4af_track_t
*track
= &ctx
->track
[track_idx
];
590 m4af_sample_entry_t
*index
= track
->sample_table
;
592 int64_t pos
= m4af_tell(ctx
);
594 "\0\0\0\0" /* size */
598 "\0\0\0\0" /* sample_size: 0(variable) */
600 m4af_write32(ctx
, track
->num_samples
);
601 for (i
= 0; i
< track
->num_samples
; ++i
, ++index
)
602 m4af_write32(ctx
, index
->size
);
603 m4af_update_size(ctx
, pos
);
607 void m4af_stsc_box(m4af_writer_t
*ctx
, int track_idx
)
609 m4af_track_t
*track
= &ctx
->track
[track_idx
];
610 m4af_chunk_entry_t
*index
= track
->chunk_table
;
611 uint32_t i
, prev_samples_per_chunk
= 0, entry_count
= 0;
612 int64_t pos
= m4af_tell(ctx
);
614 "\0\0\0\0" /* size */
618 "\0\0\0\0" /* entry_count */
621 for (i
= 0; i
< track
->num_chunks
; ++i
, ++index
) {
622 if (index
->samples_per_chunk
!= prev_samples_per_chunk
) {
624 m4af_write32(ctx
, i
+ 1);
625 m4af_write32(ctx
, index
->samples_per_chunk
);
626 m4af_write32(ctx
, 1); /* sample_description_index */
627 prev_samples_per_chunk
= index
->samples_per_chunk
;
630 m4af_write32_at(ctx
, pos
+ 12, entry_count
);
631 m4af_update_size(ctx
, pos
);
635 void m4af_stts_box(m4af_writer_t
*ctx
, int track_idx
)
637 m4af_track_t
*track
= &ctx
->track
[track_idx
];
638 m4af_sample_entry_t
*index
= track
->sample_table
;
639 uint32_t i
, prev_delta
= 0, entry_count
= 0, sample_count
= 0;
640 int64_t pos
= m4af_tell(ctx
);
642 "\0\0\0\0" /* size */
646 "\0\0\0\0" /* entry_count */
649 for (i
= 0; i
< track
->num_samples
; ++i
, ++index
) {
650 if (index
->delta
== prev_delta
)
655 m4af_write32(ctx
, sample_count
);
656 m4af_write32(ctx
, prev_delta
);
658 prev_delta
= index
->delta
;
663 m4af_write32(ctx
, sample_count
);
664 m4af_write32(ctx
, prev_delta
);
666 m4af_write32_at(ctx
, pos
+ 12, entry_count
);
667 m4af_update_size(ctx
, pos
);
671 void m4af_esds_box(m4af_writer_t
*ctx
, int track_idx
)
673 m4af_track_t
*track
= &ctx
->track
[track_idx
];
674 int64_t pos
= m4af_tell(ctx
);
675 m4af_write(ctx
, "\0\0\0\0esds", 8);
676 m4af_write32(ctx
, 0); /* version + flags */
679 m4af_descriptor(ctx
, 3, 32 + track
->decSpecificInfoSize
);
680 m4af_write(ctx
, "\0\0\0", 3);
681 /* DecoderConfigDescriptor */
682 m4af_descriptor(ctx
, 4, 18 + track
->decSpecificInfoSize
);
684 "\x40" /* objectTypeIndication: 0x40(Audio ISO/IEC 14496-3)*/
685 "\x15" /* streamType(6): 0x05(AudioStream)
690 m4af_write24(ctx
, track
->bufferSizeDB
);
691 m4af_write32(ctx
, track
->maxBitrate
);
692 m4af_write32(ctx
, track
->avgBitrate
);
693 /* DecoderSpecificInfo */
694 m4af_descriptor(ctx
, 5, track
->decSpecificInfoSize
);
695 m4af_write(ctx
, track
->decSpecificInfo
, track
->decSpecificInfoSize
);
696 /* SLConfigDescriptor */
697 m4af_descriptor(ctx
, 6, 1);
698 m4af_write(ctx
, "\002", 1); /* predefined */
700 m4af_update_size(ctx
, pos
);
704 void m4af_alac_box(m4af_writer_t
*ctx
, int track_idx
)
706 m4af_track_t
*track
= &ctx
->track
[track_idx
];
707 int64_t pos
= m4af_tell(ctx
);
709 "\0\0\0\0" /* size */
714 m4af_write(ctx
, track
->decSpecificInfo
, track
->decSpecificInfoSize
);
715 m4af_update_size(ctx
, pos
);
719 void m4af_mp4a_box(m4af_writer_t
*ctx
, int track_idx
)
721 m4af_track_t
*track
= &ctx
->track
[track_idx
];
722 int64_t pos
= m4af_tell(ctx
);
723 m4af_write32(ctx
, 0); /* size */
724 m4af_write32(ctx
, track
->codec
); /* mp4a or alac */
726 "\0\0\0\0\0\0" /* reserved */
727 "\0\001" /* data_reference_index: 1 */
728 "\0\0\0\0" /* reserved[0] */
729 "\0\0\0\0" /* reserved[1] */
730 "\0\002" /* channelcount: 2 */
731 "\0\020" /* samplesize: 16 */
732 "\0\0" /* pre_defined */
733 "\0\0" /* reserved */
735 if (track
->codec
== M4AF_FOURCC('m','p','4','a')) {
736 m4af_write32(ctx
, track
->timescale
<< 16);
737 m4af_esds_box(ctx
, track_idx
);
739 m4af_write32(ctx
, 44100);
740 m4af_alac_box(ctx
, track_idx
);
742 m4af_update_size(ctx
, pos
);
746 void m4af_stsd_box(m4af_writer_t
*ctx
, int track_idx
)
748 int64_t pos
= m4af_tell(ctx
);
749 m4af_write(ctx
, "\0\0\0\0stsd", 8);
753 "\0\0\0\001" /* entry_count: 1 */
755 m4af_mp4a_box(ctx
, track_idx
);
756 m4af_update_size(ctx
, pos
);
760 void m4af_stbl_box(m4af_writer_t
*ctx
, int track_idx
)
762 int64_t pos
= m4af_tell(ctx
);
763 m4af_write(ctx
, "\0\0\0\0stbl", 8);
764 m4af_stsd_box(ctx
, track_idx
);
765 m4af_stts_box(ctx
, track_idx
);
766 m4af_stsc_box(ctx
, track_idx
);
767 m4af_stsz_box(ctx
, track_idx
);
768 m4af_stco_box(ctx
, track_idx
);
769 m4af_update_size(ctx
, pos
);
773 void m4af_url_box(m4af_writer_t
*ctx
, int track_idx
)
776 "\0\0\0\014" /* size */
779 "\0\0\001" /* flags: 1(in the same file) */
784 void m4af_dref_box(m4af_writer_t
*ctx
, int track_idx
)
786 int64_t pos
= m4af_tell(ctx
);
787 m4af_write(ctx
, "\0\0\0\0dref", 8);
791 "\0\0\0\001" /* entry_count: 1 */
793 m4af_url_box(ctx
, track_idx
);
794 m4af_update_size(ctx
, pos
);
798 void m4af_dinf_box(m4af_writer_t
*ctx
, int track_idx
)
800 int64_t pos
= m4af_tell(ctx
);
801 m4af_write(ctx
, "\0\0\0\0dinf", 8);
802 m4af_dref_box(ctx
, track_idx
);
803 m4af_update_size(ctx
, pos
);
807 void m4af_smhd_box(m4af_writer_t
*ctx
, int track_idx
)
810 "\0\0\0\020" /* size */
815 "\0\0" /* reserved */
820 void m4af_minf_box(m4af_writer_t
*ctx
, int track_idx
)
822 m4af_track_t
*track
= &ctx
->track
[track_idx
];
823 int64_t pos
= m4af_tell(ctx
);
824 m4af_write(ctx
, "\0\0\0\0minf", 8);
825 /* TODO: add TEXT support */
826 if (track
->codec
!= M4AF_CODEC_TEXT
)
827 m4af_smhd_box(ctx
, track_idx
);
828 m4af_dinf_box(ctx
, track_idx
);
829 m4af_stbl_box(ctx
, track_idx
);
830 m4af_update_size(ctx
, pos
);
834 void m4af_mdhd_box(m4af_writer_t
*ctx
, int track_idx
)
836 m4af_track_t
*track
= &ctx
->track
[track_idx
];
837 int64_t pos
= m4af_tell(ctx
);
838 uint8_t version
= m4af_head_version(ctx
, track_idx
);
840 m4af_write(ctx
, "\0\0\0\0mdhd", 8);
841 m4af_write(ctx
, &version
, 1);
842 m4af_write(ctx
, "\0\0\0", 3); /* flags */
844 m4af_write64(ctx
, track
->creation_time
);
845 m4af_write64(ctx
, track
->modification_time
);
846 m4af_write32(ctx
, track
->timescale
);
847 m4af_write64(ctx
, track
->duration
);
849 m4af_write32(ctx
, track
->creation_time
);
850 m4af_write32(ctx
, track
->modification_time
);
851 m4af_write32(ctx
, track
->timescale
);
852 m4af_write32(ctx
, track
->duration
);
855 "\x55\xc4" /* language: und */
856 "\0\0" /* pre_defined */
858 m4af_update_size(ctx
, pos
);
862 void m4af_hdlr_box(m4af_writer_t
*ctx
, int track_idx
, const char *type
)
864 int64_t pos
= m4af_tell(ctx
);
865 static const char reserved_and_name
[10] = { 0 };
868 "\0\0\0\0" /* size */
872 "\0\0\0\0" /* pre_defined */
874 m4af_write(ctx
, type
, 4); /* handler_type */
876 m4af_write(ctx
, !strcmp(type
, "mdir") ? "appl" : "\0\0\0\0", 4);
877 /* reserved[1], reserved[2], name */
878 m4af_write(ctx
, reserved_and_name
, (pos
& 1) ? 9 : 10);
879 m4af_update_size(ctx
, pos
);
883 void m4af_mdia_box(m4af_writer_t
*ctx
, int track_idx
)
885 m4af_track_t
*track
= &ctx
->track
[track_idx
];
887 (track
->codec
== M4AF_CODEC_TEXT
) ? "text" : "soun";
888 int64_t pos
= m4af_tell(ctx
);
889 m4af_write(ctx
, "\0\0\0\0mdia", 8);
890 m4af_mdhd_box(ctx
, track_idx
);
891 m4af_hdlr_box(ctx
, track_idx
, hdlr
);
892 m4af_minf_box(ctx
, track_idx
);
893 m4af_update_size(ctx
, pos
);
897 void m4af_tkhd_box(m4af_writer_t
*ctx
, int track_idx
)
899 m4af_track_t
*track
= &ctx
->track
[track_idx
];
900 int64_t pos
= m4af_tell(ctx
);
901 uint8_t version
= m4af_head_version(ctx
, track_idx
);
902 m4af_write(ctx
, "\0\0\0\0tkhd", 8);
903 m4af_write(ctx
, &version
, 1);
904 m4af_write(ctx
, "\0\0\007", 3); /* flags */
906 m4af_write64(ctx
, track
->creation_time
);
907 m4af_write64(ctx
, track
->modification_time
);
908 m4af_write32(ctx
, track_idx
+ 1);
909 m4af_write(ctx
, "\0\0\0\0" /* reserved */
911 m4af_write64(ctx
, track
->duration
);
913 m4af_write32(ctx
, track
->creation_time
);
914 m4af_write32(ctx
, track
->modification_time
);
915 m4af_write32(ctx
, track_idx
+ 1);
916 m4af_write(ctx
, "\0\0\0\0" /* reserved */
918 m4af_write32(ctx
, track
->duration
);
921 "\0\0\0\0" /* reserved[0] */
922 "\0\0\0\0" /* reserved[1] */
924 "\0\0" /* alternate_group */
925 "\001\0" /* volume: 1.0 */
926 "\0\0" /* reserved */
927 "\0\001\0\0" /* matrix[0] */
928 "\0\0\0\0" /* matrix[1] */
929 "\0\0\0\0" /* matrix[2] */
930 "\0\0\0\0" /* matrix[3] */
931 "\0\001\0\0" /* matrix[4] */
932 "\0\0\0\0" /* matrix[5] */
933 "\0\0\0\0" /* matrix[6] */
934 "\0\0\0\0" /* matrix[7] */
935 "\100\0\0\0" /* matrix[8] */
936 "\0\0\0\0" /* width */
937 "\0\0\0\0" /* height */
939 m4af_update_size(ctx
, pos
);
943 void m4af_trak_box(m4af_writer_t
*ctx
, int track_idx
)
945 int64_t pos
= m4af_tell(ctx
);
946 m4af_write(ctx
, "\0\0\0\0trak", 8);
947 m4af_tkhd_box(ctx
, track_idx
);
948 m4af_mdia_box(ctx
, track_idx
);
949 m4af_update_size(ctx
, pos
);
953 int64_t m4af_movie_duration(m4af_writer_t
*ctx
)
955 int64_t movie_duration
= 0;
957 for (i
= 0; i
< ctx
->num_tracks
; ++i
) {
958 double x
= ctx
->track
[i
].duration
;
959 int64_t duration
= x
* ctx
->track
[i
].timescale
/ ctx
->timescale
+ .5;
960 if (duration
> movie_duration
)
961 movie_duration
= duration
;
963 return movie_duration
;
967 void m4af_mvhd_box(m4af_writer_t
*ctx
)
969 int64_t pos
= m4af_tell(ctx
);
970 uint8_t version
= m4af_head_version(ctx
, 0);
971 int64_t movie_duration
= m4af_movie_duration(ctx
);
973 m4af_write(ctx
, "\0\0\0\0mvhd", 8);
974 m4af_write(ctx
, &version
, 1);
975 m4af_write(ctx
, "\0\0\0", 3); /* flags */
977 m4af_write64(ctx
, ctx
->creation_time
);
978 m4af_write64(ctx
, ctx
->modification_time
);
979 m4af_write32(ctx
, ctx
->timescale
);
980 m4af_write64(ctx
, movie_duration
);
982 m4af_write32(ctx
, ctx
->creation_time
);
983 m4af_write32(ctx
, ctx
->modification_time
);
984 m4af_write32(ctx
, ctx
->timescale
);
985 m4af_write32(ctx
, movie_duration
);
988 "\0\001\0\0" /* rate: 1.0 */
989 "\001\0" /* volume: 1.0 */
990 "\0\0" /* reserved */
991 "\0\0\0\0" /* reserved[0] */
992 "\0\0\0\0" /* reserved[1] */
993 "\0\001\0\0" /* matrix[0] */
994 "\0\0\0\0" /* matrix[1] */
995 "\0\0\0\0" /* matrix[2] */
996 "\0\0\0\0" /* matrix[3] */
997 "\0\001\0\0" /* matrix[4] */
998 "\0\0\0\0" /* matrix[5] */
999 "\0\0\0\0" /* matrix[6] */
1000 "\0\0\0\0" /* matrix[7] */
1001 "\100\0\0\0" /* matrix[8] */
1002 "\0\0\0\0" /* pre_defined[0] */
1003 "\0\0\0\0" /* pre_defined[1] */
1004 "\0\0\0\0" /* pre_defined[2] */
1005 "\0\0\0\0" /* pre_defined[3] */
1006 "\0\0\0\0" /* pre_defined[4] */
1007 "\0\0\0\0" /* pre_defined[5] */
1009 m4af_write32(ctx
, ctx
->num_tracks
+ 1);
1010 m4af_update_size(ctx
, pos
);
1014 void m4af_mean_box(m4af_writer_t
*ctx
)
1017 "\0\0\0\034" /* size */
1020 "\0\0\0" /* flags */
1021 "com.apple.iTunes" /* meaning-string */
1026 void m4af_name_box(m4af_writer_t
*ctx
, const char *name
)
1028 int64_t pos
= m4af_tell(ctx
);
1030 "\0\0\0\0" /* size */
1033 "\0\0\0" /* flags */
1035 m4af_write(ctx
, name
, strlen(name
));
1036 m4af_update_size(ctx
, pos
);
1040 void m4af_data_box(m4af_writer_t
*ctx
, uint32_t type_code
,
1041 const char *data
, uint32_t data_size
)
1043 int64_t pos
= m4af_tell(ctx
);
1044 uint8_t code
= type_code
;
1046 "\0\0\0\0" /* size */
1048 "\0\0" /* reserved */
1049 "\0" /* type_set_indifier */
1051 m4af_write(ctx
, &code
, 1);
1052 m4af_write(ctx
, "\0\0\0\0", 4); /* locale */
1053 m4af_write(ctx
, data
, data_size
);
1054 m4af_update_size(ctx
, pos
);
1058 void m4af_write_metadata(m4af_writer_t
*ctx
, m4af_itmf_entry_t
*entry
)
1060 int64_t pos
= m4af_tell(ctx
);
1061 m4af_write(ctx
, "\0\0\0\0", 4);
1062 m4af_write32(ctx
, entry
->type
);
1063 if (entry
->type
!= M4AF_FOURCC('-','-','-','-'))
1064 m4af_data_box(ctx
, entry
->u
.type_code
, entry
->data
, entry
->data_size
);
1067 m4af_name_box(ctx
, entry
->u
.name
);
1068 m4af_data_box(ctx
, 1, entry
->data
, entry
->data_size
);
1070 m4af_update_size(ctx
, pos
);
1074 void m4af_ilst_box(m4af_writer_t
*ctx
)
1077 int64_t pos
= m4af_tell(ctx
);
1078 m4af_write(ctx
, "\0\0\0\0ilst", 8);
1079 for (i
= 0; i
< ctx
->num_tags
; ++i
)
1080 m4af_write_metadata(ctx
, &ctx
->itmf_table
[i
]);
1081 m4af_update_size(ctx
, pos
);
1085 void m4af_meta_box(m4af_writer_t
*ctx
)
1087 int64_t pos
= m4af_tell(ctx
);
1089 "\0\0\0\0" /* size */
1092 "\0\0\0" /* flags */
1094 m4af_hdlr_box(ctx
, 0, "mdir");
1096 m4af_update_size(ctx
, pos
);
1100 void m4af_udta_box(m4af_writer_t
*ctx
)
1102 int64_t pos
= m4af_tell(ctx
);
1103 m4af_write(ctx
, "\0\0\0\0udta", 8);
1105 m4af_update_size(ctx
, pos
);
1109 void m4af_moov_box(m4af_writer_t
*ctx
)
1112 int64_t pos
= m4af_tell(ctx
);
1113 m4af_write(ctx
, "\0\0\0\0moov", 8);
1115 for (i
= 0; i
< ctx
->num_tracks
; ++i
)
1116 m4af_trak_box(ctx
, i
);
1119 m4af_update_size(ctx
, pos
);
1123 void m4af_finalize_mdat(m4af_writer_t
*ctx
)
1125 if (ctx
->mdat_size
+ 8 > UINT32_MAX
) {
1126 m4af_set_pos(ctx
, ctx
->mdat_pos
- 16);
1127 m4af_write32(ctx
, 1);
1128 m4af_write(ctx
, "mdat", 4);
1129 m4af_write64(ctx
, ctx
->mdat_size
+ 16);
1131 m4af_set_pos(ctx
, ctx
->mdat_pos
- 8);
1132 m4af_write32(ctx
, ctx
->mdat_size
+ 8);
1134 m4af_set_pos(ctx
, ctx
->mdat_pos
+ ctx
->mdat_size
);
1137 int m4af_finalize(m4af_writer_t
*ctx
)
1140 m4af_track_t
*track
;
1142 for (i
= 0; i
< ctx
->num_tracks
; ++i
) {
1143 track
= ctx
->track
+ i
;
1144 if (track
->duration
) {
1145 int64_t track_size
= 0;
1147 for (j
= 0; j
< track
->num_chunks
; ++j
)
1148 track_size
+= track
->chunk_table
[j
].size
;
1150 8.0 * track_size
* track
->timescale
/ track
->duration
+ .5;
1152 m4af_flush_chunk(ctx
, i
);
1154 if (ctx
->track
[0].encoder_delay
|| ctx
->track
[0].padding
)
1155 m4af_set_iTunSMPB(ctx
);
1156 m4af_finalize_mdat(ctx
);
1158 return ctx
->last_error
? -1 : 0;
1162 void m4af_free_itmf_table(m4af_writer_t
*ctx
)
1165 m4af_itmf_entry_t
*entry
= ctx
->itmf_table
;
1166 for (i
= 0; i
< ctx
->num_tags
; ++i
, ++entry
) {
1167 if (entry
->type
== M4AF_FOURCC('-','-','-','-'))
1168 m4af_free(entry
->u
.name
);
1169 m4af_free(entry
->data
);
1171 m4af_free(ctx
->itmf_table
);
1174 void m4af_teardown(m4af_writer_t
**ctxp
)
1177 m4af_writer_t
*ctx
= *ctxp
;
1178 m4af_track_t
*track
;
1179 for (i
= 0; i
< ctx
->num_tracks
; ++i
) {
1180 track
= ctx
->track
+ i
;
1181 if (track
->decSpecificInfo
)
1182 m4af_free(track
->decSpecificInfo
);
1183 if (track
->sample_table
)
1184 m4af_free(track
->sample_table
);
1185 if (track
->chunk_table
)
1186 m4af_free(track
->chunk_table
);
1187 if (track
->chunk_buffer
)
1188 m4af_free(track
->chunk_buffer
);
1190 if (ctx
->itmf_table
)
1191 m4af_free_itmf_table(ctx
);
This page took 0.156518 seconds and 4 git commands to generate.