24 #include "config_components.h"
73 {
"movflags",
"MOV muxer flags", offsetof(
MOVMuxContext,
flags),
AV_OPT_TYPE_FLAGS, {.i64 = 0}, INT_MIN, INT_MAX,
AV_OPT_FLAG_ENCODING_PARAM,
"movflags" },
75 {
"moov_size",
"maximum moov size so it can be placed at the begin", offsetof(
MOVMuxContext, reserved_moov_size),
AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX,
AV_OPT_FLAG_ENCODING_PARAM, 0 },
89 {
"delay_moov",
"Delay writing the initial moov until the first fragment is cut, or until the first fragment flush", 0,
AV_OPT_TYPE_CONST, {.i64 =
FF_MOV_FLAG_DELAY_MOOV}, INT_MIN, INT_MAX,
AV_OPT_FLAG_ENCODING_PARAM,
"movflags" },
92 {
"write_colr",
"Write colr atom even if the color info is unspecified (Experimental, may be renamed or changed, do not use from scripts)", 0,
AV_OPT_TYPE_CONST, {.i64 =
FF_MOV_FLAG_WRITE_COLR}, INT_MIN, INT_MAX,
AV_OPT_FLAG_ENCODING_PARAM,
"movflags" },
111 {
"frag_interleave",
"Interleave samples within fragments (max number of consecutive samples, lower is tighter interleaving, but with more overhead)", offsetof(
MOVMuxContext, frag_interleave),
AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX,
AV_OPT_FLAG_ENCODING_PARAM },
118 {
"write_prft",
"Write producer reference time box with specified time source", offsetof(
MOVMuxContext, write_prft),
AV_OPT_TYPE_INT, {.i64 =
MOV_PRFT_NONE}, 0,
MOV_PRFT_NB-1,
AV_OPT_FLAG_ENCODING_PARAM,
"prft"},
127 .
class_name =
"mov/mp4/tgp/psp/tg2/ipod/ismv/f4v muxer",
194 for (
i = 0;
i < track->
entry;
i++) {
209 int i, j, entries = 0, tst = -1, oldtst = -1;
216 for (
i = 0;
i < track->
entry;
i++) {
218 if (oldtst != -1 && tst != oldtst)
223 if (equalChunks && track->
entry) {
225 sSize =
FFMAX(1, sSize);
231 for (
i = 0;
i < track->
entry;
i++) {
244 int index = 0, oldval = -1,
i;
245 int64_t entryPos, curpos;
253 for (
i = 0;
i < track->
entry;
i++) {
273 int64_t curpos, entryPos;
281 for (
i = 0;
i < track->
entry;
i++) {
298 uint8_t leading, dependent, reference, redundancy;
303 for (
i = 0;
i < track->
entry;
i++) {
312 avio_w8(pb, (leading << 6) | (dependent << 4) |
313 (reference << 2) | redundancy);
373 "Cannot write moov atom before AC3 packets."
374 " Set the delay_moov flag to fix this.\n");
378 if (
info->substream[0].bsid > 8) {
380 "RealAudio AC-3/DolbyNet with bsid %d is not defined by the "
381 "ISOBMFF specification in ETSI TS 102 366!\n",
382 info->substream[0].bsid);
386 if (
info->ac3_bit_rate_code < 0) {
388 "No valid AC3 bit rate code for data rate of %d!\n",
446 if (!
info->ec3_done) {
501 while (cumul_size !=
pkt->
size) {
511 info->substream[parent].num_dep_sub++;
527 info->substream[parent].chan_loc |= (
get_bits(&gbc, 16) >> 5) & 0x1f;
545 if (!
info->num_blocks) {
556 if (
info->num_blocks != 6)
560 info->num_blocks = 0;
579 "Cannot write moov atom before EAC3 packets parsed.\n");
584 size = 2 + ((34 * (
info->num_ind_sub + 1) + 7) >> 3);
593 for (
i = 0;
i <=
info->num_ind_sub;
i++) {
603 if (!
info->substream[
i].num_dep_sub) {
677 track->st->codecpar->nb_coded_side_data,
683 if (!bit_rates.avg_bit_rate) {
692 if (props && props->avg_bitrate) {
693 bit_rates.avg_bit_rate = props->avg_bitrate;
694 }
else if (track->par->bit_rate) {
695 bit_rates.avg_bit_rate = track->par->bit_rate;
696 }
else if (props && props->max_bitrate) {
697 bit_rates.avg_bit_rate = props->max_bitrate;
702 bit_rates.max_bit_rate =
FFMAX(track->par->bit_rate,
703 bit_rates.avg_bit_rate);
707 bit_rates.max_bit_rate =
FFMAX(bit_rates.max_bit_rate,
709 bit_rates.buffer_size = props->buffer_size / 8;
719 int decoder_specific_info_len = track->
vos_len ? 5 + track->
vos_len : 0;
726 put_descr(pb, 0x03, 3 + 5+13 + decoder_specific_info_len + 5+1);
731 put_descr(pb, 0x04, 13 + decoder_specific_info_len);
868 "Cannot write moov atom before TrueHD packets."
869 " Set the delay_moov flag to fix this.\n");
874 if (length < 20 || length > track->
vos_len)
890 uint32_t layout_tag, bitmap, *channel_desc;
898 &bitmap, &channel_desc);
903 "lack of channel information\n");
912 channel_desc =
av_malloc(
sizeof(*channel_desc));
931 for (
int i = 0;
i < num_desc;
i++) {
991 int unescaped_size, seq_found = 0;
992 int level = 0, interlace = 0;
1001 packet_seq = packet_entry = 1;
1003 "moov atom written before any packets, unable to write correct "
1004 "dvc1 atom. Set the delay_moov flag to fix this.\n");
1011 for (next = start; next < end; start = next) {
1015 size = next - start - 4;
1072 uint8_t buf[7] = { 0 };
1127 if (cluster_idx >= track->
entry)
1130 if (cluster_idx + 1 == track->
entry)
1133 next_dts = track->
cluster[cluster_idx + 1].
dts;
1145 int i, first_duration;
1157 for (
i = 1;
i < track->
entry;
i++) {
1161 return first_duration;
1188 uint8_t *speaker_pos =
NULL;
1196 speaker_pos,
layout->nb_channels);
1198 char buf[128] = {0};
1216 for (
int i = 0;
i <
layout->nb_channels;
i++)
1254 uint32_t
tag = track->
tag;
1358 else if (track->
tag ==
MKTAG(
'm',
'p',
'4',
'a'))
1382 }
else if (track->
vos_len > 0)
1454 if (track->
tag ==
MKTAG(
'h',
'v',
'c',
'1'))
1468 if (track->
tag ==
MKTAG(
'e',
'v',
'c',
'1'))
1481 int display_width = track->
par->
width;
1509 if (track->
tag ==
MKTAG(
'A',
'V',
'd',
'h')) {
1585 }
else if (track->
par->
height == 1080) {
1600 if (rational_framerate.
den != 0)
1601 rate =
av_q2d(rational_framerate);
1618 if (rate == 24)
tag =
MKTAG(
'x',
'd',
'v',
'4');
1619 else if (rate == 25)
tag =
MKTAG(
'x',
'd',
'v',
'5');
1620 else if (rate == 30)
tag =
MKTAG(
'x',
'd',
'v',
'1');
1621 else if (rate == 50)
tag =
MKTAG(
'x',
'd',
'v',
'a');
1622 else if (rate == 60)
tag =
MKTAG(
'x',
'd',
'v',
'9');
1626 if (rate == 24)
tag =
MKTAG(
'x',
'd',
'v',
'6');
1627 else if (rate == 25)
tag =
MKTAG(
'x',
'd',
'v',
'7');
1628 else if (rate == 30)
tag =
MKTAG(
'x',
'd',
'v',
'8');
1630 if (rate == 25)
tag =
MKTAG(
'x',
'd',
'v',
'3');
1631 else if (rate == 30)
tag =
MKTAG(
'x',
'd',
'v',
'2');
1635 if (rate == 24)
tag =
MKTAG(
'x',
'd',
'v',
'd');
1636 else if (rate == 25)
tag =
MKTAG(
'x',
'd',
'v',
'e');
1637 else if (rate == 30)
tag =
MKTAG(
'x',
'd',
'v',
'f');
1639 if (rate == 25)
tag =
MKTAG(
'x',
'd',
'v',
'c');
1640 else if (rate == 30)
tag =
MKTAG(
'x',
'd',
'v',
'b');
1646 if (rate == 24)
tag =
MKTAG(
'x',
'd',
'5',
'4');
1647 else if (rate == 25)
tag =
MKTAG(
'x',
'd',
'5',
'5');
1648 else if (rate == 30)
tag =
MKTAG(
'x',
'd',
'5',
'1');
1649 else if (rate == 50)
tag =
MKTAG(
'x',
'd',
'5',
'a');
1650 else if (rate == 60)
tag =
MKTAG(
'x',
'd',
'5',
'9');
1654 if (rate == 24)
tag =
MKTAG(
'x',
'd',
'5',
'd');
1655 else if (rate == 25)
tag =
MKTAG(
'x',
'd',
'5',
'e');
1656 else if (rate == 30)
tag =
MKTAG(
'x',
'd',
'5',
'f');
1658 if (rate == 25)
tag =
MKTAG(
'x',
'd',
'5',
'c');
1659 else if (rate == 30)
tag =
MKTAG(
'x',
'd',
'5',
'b');
1680 if (rate == 24)
tag =
MKTAG(
'a',
'i',
'5',
'p');
1681 else if (rate == 25)
tag =
MKTAG(
'a',
'i',
'5',
'q');
1682 else if (rate == 30)
tag =
MKTAG(
'a',
'i',
'5',
'p');
1683 else if (rate == 50)
tag =
MKTAG(
'a',
'i',
'5',
'q');
1684 else if (rate == 60)
tag =
MKTAG(
'a',
'i',
'5',
'p');
1688 if (rate == 24)
tag =
MKTAG(
'a',
'i',
'5',
'3');
1689 else if (rate == 25)
tag =
MKTAG(
'a',
'i',
'5',
'2');
1690 else if (rate == 30)
tag =
MKTAG(
'a',
'i',
'5',
'3');
1692 if (rate == 50)
tag =
MKTAG(
'a',
'i',
'5',
'5');
1693 else if (rate == 60)
tag =
MKTAG(
'a',
'i',
'5',
'6');
1699 if (rate == 24)
tag =
MKTAG(
'a',
'i',
'1',
'p');
1700 else if (rate == 25)
tag =
MKTAG(
'a',
'i',
'1',
'q');
1701 else if (rate == 30)
tag =
MKTAG(
'a',
'i',
'1',
'p');
1702 else if (rate == 50)
tag =
MKTAG(
'a',
'i',
'1',
'q');
1703 else if (rate == 60)
tag =
MKTAG(
'a',
'i',
'1',
'p');
1707 if (rate == 24)
tag =
MKTAG(
'a',
'i',
'1',
'3');
1708 else if (rate == 25)
tag =
MKTAG(
'a',
'i',
'1',
'2');
1709 else if (rate == 30)
tag =
MKTAG(
'a',
'i',
'1',
'3');
1711 if (rate == 25)
tag =
MKTAG(
'a',
'i',
'1',
'5');
1712 else if (rate == 50)
tag =
MKTAG(
'a',
'i',
'1',
'5');
1713 else if (rate == 60)
tag =
MKTAG(
'a',
'i',
'1',
'6');
1735 static const struct {
1783 if (
tag ==
MKTAG(
'r',
'a',
'w',
' ') &&
1787 av_log(
s,
AV_LOG_ERROR,
"%s rawvideo cannot be written to mov, output file will be unreadable\n",
1826 "the file may be unplayable!\n");
1833 tag =
MKTAG(
'm',
's', ((ms_tag >> 8) & 0xff), (ms_tag & 0xff));
1835 "the file may be unplayable!\n");
1860 for (
i = 0; tags && tags[
i];
i++) {
1865 return codec_tags->
tag;
1882 "Quicktime/Ipod might not play the file\n");
1908 0x0000, 0x0100, 0x0201, 0x0206, 0x0209, 0x020e
1913 unsigned mov_field_order = 0;
1915 mov_field_order =
fiel_data[field_order];
1953 "Unknown codec tag '%s' utilized for TTML stream with "
1954 "index %d (track id %d)!\n",
1973 if (stereo_3d->
flags != 0) {
1978 switch (stereo_3d->
type) {
2001 int64_t sv3d_pos, svhd_pos, proj_pos;
2111 gama = (uint32_t)
lrint((
double)(1<<16) * gamma);
2142 return 12 + sd->
size;
2200 const int chroma_den = 50000;
2201 const int luma_den = 10000;
2248 }
else if(track->
par->
width == 1440) {
2267 uint8_t ccstValue = (0 << 7) |
2284 avio_write(pb,
"urn:mpeg:mpegB:cicp:systems:auxiliary:alpha\0", 44);
2292 char compressor_name[32] = { 0 };
2312 if (uncompressed_ycbcr) {
2338 avio_w8(pb, strlen(compressor_name));
2359 for (
i = 0;
i < pal_size;
i++) {
2361 uint16_t
r = (
rgb >> 16) & 0xff;
2362 uint16_t
g = (
rgb >> 8) & 0xff;
2363 uint16_t
b =
rgb & 0xff;
2372 if (track->
tag ==
MKTAG(
'm',
'p',
'4',
'v'))
2405 }
else if (track->
vos_len > 0)
2455 if (spherical_mapping)
2465 if (uncompressed_ycbcr){
2484 if (
s->nb_streams > 0 && track == &mov->
tracks[1])
2513 uint64_t str_size =strlen(reel_name);
2516 if (str_size >= UINT16_MAX){
2546 if (nb_frames > 255) {
2622 uint32_t entries = 0;
2629 ctts_entries[0].
count = 1;
2631 for (
i = 1;
i < track->
entry;
i++) {
2633 ctts_entries[entries].
count++;
2637 ctts_entries[entries].
count = 1;
2641 atom_size = 16 + (entries * 8);
2650 for (
i = 0;
i < entries;
i++) {
2662 uint32_t entries = -1;
2667 stts_entries =
av_malloc(
sizeof(*stts_entries));
2679 for (
i = 0;
i < track->
entry;
i++) {
2682 stts_entries[entries].
count++;
2686 stts_entries[entries].
count = 1;
2691 atom_size = 16 + (entries * 8);
2696 for (
i = 0;
i < entries;
i++) {
2723 int16_t roll_distance;
2724 int group_description_index;
2727 struct sgpd_entry *sgpd_entries =
NULL;
2747 for (
i = 0;
i < track->
entry;
i++) {
2748 int roll_samples_remaining = roll_samples;
2750 for (j =
i - 1; j >= 0; j--) {
2753 if (roll_samples_remaining <= 0)
2759 if (roll_samples_remaining > 0)
2764 if (
i &&
distance == sgpd_entries[entries].roll_distance) {
2765 sgpd_entries[entries].count++;
2768 sgpd_entries[entries].count = 1;
2769 sgpd_entries[entries].roll_distance =
distance;
2770 sgpd_entries[entries].group_description_index =
distance ? ++group : 0;
2776 sgpd_entries[entries].roll_distance = 1;
2777 sgpd_entries[entries].group_description_index = ++group;
2793 for (
i = 0;
i < entries;
i++) {
2794 if (sgpd_entries[
i].group_description_index) {
2795 avio_wb16(pb, -sgpd_entries[
i].roll_distance);
2805 for (
i = 0;
i < entries;
i++) {
2807 avio_wb32(pb, sgpd_entries[
i].group_description_index);
2880 const char *font =
"Lucida Grande";
2920 if (track->
tag !=
MKTAG(
'c',
'6',
'0',
'8')) {
2972 return track->
tag ==
MKTAG(
'c',
'7',
'0',
'8') ||
2973 track->
tag ==
MKTAG(
'c',
'6',
'0',
'8');
2979 const char *hdlr, *descr =
NULL, *hdlr_type =
NULL;
2985 descr =
"DataHandler";
2988 hdlr = (track->
mode ==
MODE_MOV) ?
"mhlr" :
"\0\0\0\0";
2991 hdlr_type = (track == &mov->
tracks[0]) ?
"pict" :
"auxv";
2992 descr =
"PictureHandler";
2995 descr =
"VideoHandler";
2999 descr =
"SoundHandler";
3003 descr =
"ClosedCaptionHandler";
3005 if (track->
tag ==
MKTAG(
't',
'x',
'3',
'g')) {
3007 }
else if (track->
tag ==
MKTAG(
'm',
'p',
'4',
's')) {
3014 descr =
"SubtitleHandler";
3018 descr =
"HintHandler";
3021 descr =
"TimeCodeHandler";
3024 descr =
"GoPro MET";
3027 "Unknown hdlr_type for %s, writing dummy values\n",
3052 descr_len = strlen(descr);
3081 for (
int i = 0;
i <
s->nb_streams;
i++) {
3102 for (
int i = 0;
i <
s->nb_streams;
i++) {
3145 avio_wb32(pb,
s->streams[stream_index]->codecpar->width);
3146 avio_wb32(pb,
s->streams[stream_index]->codecpar->height);
3171 for (
int i = 0;
i <
s->nb_streams;
i++) {
3191 for (
int i = 0,
index = 1;
i <
s->nb_streams;
i++) {
3251 }
else if (track->
tag ==
MKTAG(
'r',
't',
'p',
' ')) {
3253 }
else if (track->
tag ==
MKTAG(
't',
'm',
'c',
'd')) {
3258 }
else if (track->
tag ==
MKTAG(
'g',
'p',
'm',
'd')) {
3270 int64_t *start, int64_t *end)
3344 else if (!track->
entry)
3353 "FATAL error, file duration too long for timebase, this file will not be\n"
3354 "playable with QuickTime. Choose a different timebase with "
3355 "-video_track_timescale or a different container format\n");
3381 int16_t
d, int16_t tx, int16_t ty)
3404 uint32_t *display_matrix =
NULL;
3423 if (sd && sd->
size == 9 *
sizeof(*display_matrix))
3424 display_matrix = (uint32_t *)sd->
data;
3447 else if (!track->
entry)
3464 if (display_matrix) {
3465 for (
i = 0;
i < 9;
i++)
3473 int64_t track_width_1616;
3475 track_width_1616 = track->
par->
width * 0x10000ULL;
3480 if (!track_width_1616 ||
3482 track_width_1616 > UINT32_MAX)
3483 track_width_1616 = track->
par->
width * 0x10000ULL;
3485 if (track_width_1616 > UINT32_MAX) {
3487 track_width_1616 = 0;
3490 if (track->
height > 0xFFFF) {
3544 int entry_size, entry_count,
size;
3545 int64_t delay, start_ct = track->
start_cts;
3553 "EDTS using dts:%"PRId64
" cts:%d instead of dts:%"PRId64
" cts:%"PRId64
" tid:%d\n",
3555 start_dts, start_ct, track->
track_id);
3574 version |= delay < INT32_MAX ? 0 : 1;
3576 entry_size = (
version == 1) ? 20 : 12;
3577 entry_count = 1 + (delay > 0);
3578 size = 24 + entry_count * entry_size;
3609 start_ct = -
FFMIN(start_dts, 0);
3667 char buf[1000] =
"";
3686 const char *
tag,
const char *str)
3711 avio_write(pb, (
const unsigned char *)scheme_uri,
3712 strlen(scheme_uri));
3732 for (
int j = 0;
map.value_maps[j].disposition; j++) {
3781 int entry_backup = track->
entry;
3801 "Not writing any edit list even though one would have been required\n");
3814 if (track->
tag ==
MKTAG(
'r',
't',
'p',
' '))
3828 track->
entry = entry_backup;
3835 int i, has_audio = 0, has_video = 0;
3845 if (audio_profile < 0)
3846 audio_profile = 0xFF - has_audio;
3847 if (video_profile < 0)
3848 video_profile = 0xFF - has_video;
3888 int max_track_id = 1,
i;
3889 int64_t max_track_len = 0;
3900 if (max_track_len < max_track_len_temp)
3901 max_track_len = max_track_len_temp;
3902 if (max_track_id < mov->tracks[
i].track_id)
3969 size_t data_len = strlen(
data);
3971 int size = 16 + data_len;
3984 return data_len + 4;
3989 const char *
value,
int lang,
int long_style)
4003 const char *
tag,
int *lang)
4017 len2 = strlen(
t2->key);
4018 if (len2 ==
len + 4 && !strcmp(t->
value,
t2->value)
4028 const char *
name,
const char *
tag,
4042 int size = 0, tmpo = t ? atoi(t->
value) : 0;
4061 double latitude, longitude, altitude;
4062 int32_t latitude_fix, longitude_fix, altitude_fix;
4064 const char *ptr, *place =
"";
4066 static const char *astronomical_body =
"earth";
4071 latitude =
strtod(ptr, &end);
4077 longitude =
strtod(ptr, &end);
4083 altitude =
strtod(ptr, &end);
4088 latitude_fix = (
int32_t) ((1 << 16) * latitude);
4089 longitude_fix = (
int32_t) ((1 << 16) * longitude);
4090 altitude_fix = (
int32_t) ((1 << 16) * altitude);
4101 avio_write(pb, astronomical_body, strlen(astronomical_body) + 1);
4112 disc ?
"disc" :
"track",
4114 int size = 0, track = t ? atoi(t->
value) : 0;
4117 char *slash = strchr(t->
value,
'/');
4119 tracks = atoi(slash + 1);
4136 const char *
name,
const char *
tag,
4143 if (
len != 1 &&
len != 4)
4148 num = atoi(t->
value);
4168 for (
i = 0;
i <
s->nb_streams;
i++) {