54 {
"movflags",
"MOV muxer flags", offsetof(
MOVMuxContext,
flags),
AV_OPT_TYPE_FLAGS, {.i64 = 0}, INT_MIN, INT_MAX,
AV_OPT_FLAG_ENCODING_PARAM,
"movflags" },
56 {
"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 },
68 {
"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" },
69 {
"write_colr",
"Write colr atom (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" },
84 {
"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 },
88 #define MOV_CLASS(flavor)\
89 static const AVClass flavor ## _muxer_class = {\
90 .class_name = #flavor " muxer",\
91 .item_name = av_default_item_name,\
93 .version = LIBAVUTIL_VERSION_INT,\
140 for (i = 0; i < track->
entry; i++) {
155 int i, j, entries = 0, tst = -1, oldtst = -1;
162 for (i = 0; i < track->
entry; i++) {
164 if (oldtst != -1 && tst != oldtst)
169 if (equalChunks && track->
entry) {
171 sSize =
FFMAX(1, sSize);
177 for (i = 0; i < track->
entry; i++) {
190 int index = 0, oldval = -1, i;
191 int64_t entryPos, curpos;
199 for (i = 0; i < track->
entry; i++) {
219 int64_t curpos, entryPos;
227 for (i = 0; i < track->
entry; i++) {
259 int fscod, bsid, bsmod, acmod, lfeon, frmsizecod;
276 if ((acmod & 1) && acmod != 1)
330 #if CONFIG_AC3_PARSER
390 while (cumul_size != pkt->
size) {
537 avio_w8(pb, (size >> (7 * i)) | 0x80);
547 for (i = 0; i < track->
entry; i++)
555 int decoder_specific_info_len = track->
vos_len ? 5 + track->
vos_len : 0;
556 unsigned avg_bitrate;
563 put_descr(pb, 0x03, 3 + 5+13 + decoder_specific_info_len + 5+1);
568 put_descr(pb, 0x04, 13 + decoder_specific_info_len);
647 uint32_t layout_tag, bitmap;
655 "lack of channel information\n");
720 int unescaped_size, seq_found = 0;
721 int level = 0, interlace = 0;
730 packet_seq = packet_entry = 1;
732 "moov atom written before any packets, unable to write correct "
733 "dvc1 atom. Set the delay_moov flag to fix this.\n");
740 for (next = start; next <
end; start = next) {
744 size = next - start - 4;
856 if (cluster_idx >= track->
entry)
859 if (cluster_idx + 1 == track->
entry)
862 next_dts = track->
cluster[cluster_idx + 1].
dts;
874 int i, first_duration;
886 for (i = 1; i < track->
entry; i++) {
890 return first_duration;
897 uint32_t
tag = track->
tag;
984 else if (track->
tag ==
MKTAG(
'm',
'p',
'4',
'a'))
1051 interlaced = (track->
vos_data[5] & 2);
1103 for (i = 0; i < 10; i++)
1160 (tag ==
MKTAG(
't',
'x',
'3',
'g') ||
1161 tag ==
MKTAG(
't',
'e',
'x',
't'))))
1168 "Quicktime/Ipod might not play the file\n");
1180 else tag =
MKTAG(
'd',
'v',
'c',
' ');
1183 else tag =
MKTAG(
'd',
'v',
'p',
'p');
1186 else tag =
MKTAG(
'd',
'v',
'h',
'p');
1187 }
else if (track->
enc->
height == 1080) {
1189 else tag =
MKTAG(
'd',
'v',
'h',
'6');
1219 tag =
MKTAG(
'm',
'2',
'v',
'1');
1224 if (rate == 24) tag =
MKTAG(
'x',
'd',
'v',
'4');
1225 else if (rate == 25) tag =
MKTAG(
'x',
'd',
'v',
'5');
1226 else if (rate == 30) tag =
MKTAG(
'x',
'd',
'v',
'1');
1227 else if (rate == 50) tag =
MKTAG(
'x',
'd',
'v',
'a');
1228 else if (rate == 60) tag =
MKTAG(
'x',
'd',
'v',
'9');
1232 if (rate == 24) tag =
MKTAG(
'x',
'd',
'v',
'6');
1233 else if (rate == 25) tag =
MKTAG(
'x',
'd',
'v',
'7');
1234 else if (rate == 30) tag =
MKTAG(
'x',
'd',
'v',
'8');
1236 if (rate == 25) tag =
MKTAG(
'x',
'd',
'v',
'3');
1237 else if (rate == 30) tag =
MKTAG(
'x',
'd',
'v',
'2');
1241 if (rate == 24) tag =
MKTAG(
'x',
'd',
'v',
'd');
1242 else if (rate == 25) tag =
MKTAG(
'x',
'd',
'v',
'e');
1243 else if (rate == 30) tag =
MKTAG(
'x',
'd',
'v',
'f');
1245 if (rate == 25) tag =
MKTAG(
'x',
'd',
'v',
'c');
1246 else if (rate == 30) tag =
MKTAG(
'x',
'd',
'v',
'b');
1252 if (rate == 24) tag =
MKTAG(
'x',
'd',
'5',
'4');
1253 else if (rate == 25) tag =
MKTAG(
'x',
'd',
'5',
'5');
1254 else if (rate == 30) tag =
MKTAG(
'x',
'd',
'5',
'1');
1255 else if (rate == 50) tag =
MKTAG(
'x',
'd',
'5',
'a');
1256 else if (rate == 60) tag =
MKTAG(
'x',
'd',
'5',
'9');
1260 if (rate == 24) tag =
MKTAG(
'x',
'd',
'5',
'd');
1261 else if (rate == 25) tag =
MKTAG(
'x',
'd',
'5',
'e');
1262 else if (rate == 30) tag =
MKTAG(
'x',
'd',
'5',
'f');
1264 if (rate == 25) tag =
MKTAG(
'x',
'd',
'5',
'c');
1265 else if (rate == 30) tag =
MKTAG(
'x',
'd',
'5',
'b');
1281 tag =
MKTAG(
'a',
'v',
'c',
'i');
1286 if (rate == 24) tag =
MKTAG(
'a',
'i',
'5',
'p');
1287 else if (rate == 25) tag =
MKTAG(
'a',
'i',
'5',
'q');
1288 else if (rate == 30) tag =
MKTAG(
'a',
'i',
'5',
'p');
1289 else if (rate == 50) tag =
MKTAG(
'a',
'i',
'5',
'q');
1290 else if (rate == 60) tag =
MKTAG(
'a',
'i',
'5',
'p');
1294 if (rate == 24) tag =
MKTAG(
'a',
'i',
'5',
'3');
1295 else if (rate == 25) tag =
MKTAG(
'a',
'i',
'5',
'2');
1296 else if (rate == 30) tag =
MKTAG(
'a',
'i',
'5',
'3');
1298 if (rate == 50) tag =
MKTAG(
'a',
'i',
'5',
'5');
1299 else if (rate == 60) tag =
MKTAG(
'a',
'i',
'5',
'6');
1305 if (rate == 24) tag =
MKTAG(
'a',
'i',
'1',
'p');
1306 else if (rate == 25) tag =
MKTAG(
'a',
'i',
'1',
'q');
1307 else if (rate == 30) tag =
MKTAG(
'a',
'i',
'1',
'p');
1308 else if (rate == 50) tag =
MKTAG(
'a',
'i',
'1',
'q');
1309 else if (rate == 60) tag =
MKTAG(
'a',
'i',
'1',
'p');
1313 if (rate == 24) tag =
MKTAG(
'a',
'i',
'1',
'3');
1314 else if (rate == 25) tag =
MKTAG(
'a',
'i',
'1',
'2');
1315 else if (rate == 30) tag =
MKTAG(
'a',
'i',
'1',
'3');
1317 if (rate == 25) tag =
MKTAG(
'a',
'i',
'1',
'5');
1318 else if (rate == 50) tag =
MKTAG(
'a',
'i',
'1',
'5');
1319 else if (rate == 60) tag =
MKTAG(
'a',
'i',
'1',
'6');
1324 tag =
MKTAG(
'a',
'i',
'v',
'x');
1331 static const struct {
1370 if (tag ==
MKTAG(
'r',
'a',
'w',
' ') &&
1373 av_log(s,
AV_LOG_ERROR,
"%s rawvideo cannot be written to mov, output file will be unreadable\n",
1403 "the file may be unplayable!\n");
1410 tag =
MKTAG(
'm',
's', ((ms_tag >> 8) & 0xff), (ms_tag & 0xff));
1412 "the file may be unplayable!\n");
1451 tag =
MKTAG(
'w',
'm',
'a',
' ');
1481 0x0000, 0x0100, 0x0201, 0x0206, 0x0209, 0x020e
1486 unsigned mov_field_order = 0;
1537 gama = (uint32_t)
lrint((
double)(1<<16) * gamma);
1567 }
else if (track->
enc->
width == 720 &&
1643 }
else if(track->
enc->
width == 1440) {
1650 av_strlcatf(compressor_name, len,
"%d", rate * (interlaced + 1));
1657 char compressor_name[32] = { 0 };
1691 avio_w8(pb, strlen(compressor_name));
1699 if (track->
tag ==
MKTAG(
'm',
'p',
'4',
'v'))
1725 }
else if (track->
vos_len > 0)
1782 uint64_t str_size =strlen(reel_name);
1785 if (str_size >= UINT16_MAX){
1807 if (nb_frames > 255) {
1865 uint32_t entries = 0;
1872 ctts_entries[0].
count = 1;
1874 for (i = 1; i < track->
entry; i++) {
1876 ctts_entries[entries].
count++;
1880 ctts_entries[entries].
count = 1;
1884 atom_size = 16 + (entries * 8);
1889 for (i = 0; i < entries; i++) {
1901 uint32_t entries = -1;
1906 stts_entries =
av_malloc(
sizeof(*stts_entries));
1918 for (i = 0; i < track->
entry; i++) {
1920 if (i && duration == stts_entries[entries].duration) {
1921 stts_entries[entries].
count++;
1925 stts_entries[entries].
count = 1;
1930 atom_size = 16 + (entries * 8);
1935 for (i = 0; i < entries; i++) {
2005 const char *font =
"Lucida Grande";
2045 if (track->
tag !=
MKTAG(
'c',
'6',
'0',
'8')) {
2091 return track->
tag ==
MKTAG(
'c',
'7',
'0',
'8') ||
2092 track->
tag ==
MKTAG(
'c',
'6',
'0',
'8');
2097 const char *hdlr, *descr =
NULL, *hdlr_type =
NULL;
2102 descr =
"DataHandler";
2105 hdlr = (track->
mode ==
MODE_MOV) ?
"mhlr" :
"\0\0\0\0";
2108 descr =
"VideoHandler";
2111 descr =
"SoundHandler";
2115 descr =
"ClosedCaptionHandler";
2117 if (track->
tag ==
MKTAG(
't',
'x',
'3',
'g')) {
2119 }
else if (track->
tag ==
MKTAG(
'm',
'p',
'4',
's')) {
2124 descr =
"SubtitleHandler";
2128 descr =
"HintHandler";
2131 descr =
"TimeCodeHandler";
2138 "Unknown hldr_type for %s / 0x%04X, writing dummy values\n",
2200 }
else if (track->
tag ==
MKTAG(
'r',
't',
'p',
' ')) {
2202 }
else if (track->
tag ==
MKTAG(
't',
'm',
'c',
'd')) {
2234 (version == 1) ?
avio_wb64(pb, UINT64_C(0xffffffffffffffff)) :
avio_wb32(pb, 0xffffffff);
2235 else if (!track->
entry)
2244 "FATAL error, file duration too long for timebase, this file will not be\n"
2245 "playable with quicktime. Choose a different timebase or a different\n"
2246 "container format\n");
2272 int16_t d, int16_t tx, int16_t ty)
2290 int version = duration < INT32_MAX ? 0 : 1;
2295 uint32_t *display_matrix =
NULL;
2296 int display_matrix_size, i;
2305 &display_matrix_size);
2306 if (display_matrix && display_matrix_size < 9 *
sizeof(*display_matrix))
2307 display_matrix =
NULL;
2330 (version == 1) ?
avio_wb64(pb, UINT64_C(0xffffffffffffffff)) :
avio_wb32(pb, 0xffffffff);
2331 else if (!track->
entry)
2350 rotation = (rot && rot->
value) ? atoi(rot->
value) : 0;
2352 if (display_matrix) {
2353 for (i = 0; i < 9; i++)
2355 }
else if (rotation == 90) {
2357 }
else if (rotation == 180) {
2359 }
else if (rotation == 270) {
2374 if (!track_width_1616 ||
2376 track_width_1616 > UINT32_MAX)
2377 track_width_1616 = track->
enc->
width * 0x10000
U;
2425 int version = duration < INT32_MAX ? 0 : 1;
2426 int entry_size, entry_count,
size;
2427 int64_t delay, start_ct = track->
start_cts;
2434 "EDTS using dts:%"PRId64
" cts:%d instead of dts:%"PRId64
" cts:%"PRId64
" tid:%d\n",
2436 start_dts, start_ct, track->
track_id);
2444 version |= delay < INT32_MAX ? 0 : 1;
2446 entry_size = (version == 1) ? 20 : 12;
2447 entry_count = 1 + (delay > 0);
2448 size = 24 + entry_count * entry_size;
2479 start_ct = -
FFMIN(start_dts, 0);
2537 char buf[1000] =
"";
2556 const char *
tag,
const char *str)
2600 int entry_backup = track->
entry;
2620 "Not writing any edit list even though one would have been required\n");
2630 if (track->
tag ==
MKTAG(
'r',
't',
'p',
' '))
2644 track->
entry = entry_backup;
2651 int i, has_audio = 0, has_video = 0;
2661 if (audio_profile < 0)
2662 audio_profile = 0xFF - has_audio;
2663 if (video_profile < 0)
2664 video_profile = 0xFF - has_video;
2704 int max_track_id = 1, i;
2705 int64_t max_track_len = 0;
2714 if (max_track_len < max_track_len_temp)
2715 max_track_len = max_track_len_temp;
2716 if (max_track_id < mov->tracks[i].track_id)
2727 version = max_track_len < UINT32_MAX ? 0 : 1;
2728 avio_wb32(pb, version == 1 ? 120 : 108);
2781 int size = 16 + strlen(data);
2794 return strlen(data) + 4;
2799 const char *
value,
int lang,
int long_style)
2802 if (value && value[0]) {
2813 const char *
name,
const char *
tag,
2816 int l, lang = 0,
len, len2;
2824 snprintf(tag2,
sizeof(tag2),
"%s-", tag);
2826 len2 = strlen(t2->
key);
2840 int size = 0, tmpo = t ? atoi(t->
value) : 0;
2859 disc ?
"disc" :
"track",
2861 int size = 0, track = t ? atoi(t->
value) : 0;
2864 char *slash = strchr(t->
value,
'/');
2866 tracks = atoi(slash + 1);
2883 const char *
name,
const char *
tag,
2890 if (len != 1 && len != 4)
2895 num = atoi(t->
value);
2963 const char *
name,
const char *key)
2971 len = strlen(t->
value);
2995 return (((str[0] - 0x60) & 0x1F) << 10) +
2996 (((str[1] - 0x60) & 0x1F) << 5) +
2997 (( str[2] - 0x60) & 0x1F);
3001 const char *
tag,
const char *str)
3010 if (!strcmp(tag,
"yrrc"))
3015 if (!strcmp(tag,
"albm") &&
3033 for (i = 0; i < nb_chapters; i++) {
3039 int len =
FFMIN(strlen(t->value), 255);
3104 const char *str,
const char *lang,
int type)
3159 uint64_t chunkSize = chunk->
size;
3164 for (i = 1; i<trk->
entry; i++){
3172 chunkSize = chunk->
size;
3244 avio_printf(pb,
"<param name=\"%s\" value=\"%d\" valuetype=\"data\"/>\n", name, value);
3249 avio_printf(pb,
"<param name=\"%s\" value=\"%s\" valuetype=\"data\"/>\n", name, value);
3255 len =
FFMIN(
sizeof(buf) / 2 - 1, len);
3257 buf[2 *
len] =
'\0';
3258 avio_printf(pb,
"<param name=\"%s\" value=\"%s\" valuetype=\"data\"/>\n", name, buf);
3265 static const uint8_t uuid[] = {
3266 0xa5, 0xd4, 0x0b, 0x30, 0xe8, 0x14, 0x11, 0xdd,
3267 0xba, 0x2f, 0x08, 0x00, 0x20, 0x0c, 0x9a, 0x66
3275 avio_printf(pb,
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
3276 avio_printf(pb,
"<smil xmlns=\"http://www.w3.org/2001/SMIL20/Language\">\n");
3279 avio_printf(pb,
"<meta name=\"creator\" content=\"%s\" />\n",
3289 int track_id = i + 1;
3298 avio_printf(pb,
"<%s systemBitrate=\"%d\">\n", type,
3364 MOVTrack *track, int64_t moof_offset)
3369 if (!track->
entry) {
3410 if (track->
entry > 1)
3431 for (i = first; i <
end; i++) {
3461 for (i = first; i <
end; i++) {
3466 if (flags & MOV_TRUN_SAMPLE_FLAGS)
3479 static const uint8_t uuid[] = {
3480 0x6d, 0x1d, 0x9b, 0x05, 0x42, 0xd5, 0x44, 0xe6,
3481 0x80, 0xe2, 0x14, 0x1d, 0xaf, 0xf7, 0x57, 0xb2
3501 int size = 8 + 16 + 4 + 1 + 16*
n;
3502 static const uint8_t uuid[] = {
3503 0xd4, 0x80, 0x7e, 0xf2, 0xca, 0x39, 0x46, 0x95,
3504 0x8e, 0x54, 0x26, 0xcb, 0x9e, 0x46, 0xa7, 0x9f
3517 for (i = 0; i <
n; i++) {
3518 int index = entry + 1 + i;
3522 if (n < mov->ism_lookahead) {
3553 if ((tracks >= 0 && i != tracks) || !track->
entry)
3576 if (info->
time < 0) {
3599 MOVTrack *track, int64_t moof_offset,
3610 for (i = 1; i < track->
entry; i++) {
3630 for (i = 0; i <
size; i++)
3639 int tracks,
int moof_size)
3651 if (tracks >= 0 && i != tracks)
3662 MOVTrack *track,
int ref_size,
int total_sidx_size)
3664 int64_t pos =
avio_tell(pb), offset_pos, end_pos;
3666 int starts_with_SAP, i, entries;
3677 if (presentation_time < 0) {
3678 duration += presentation_time;
3679 presentation_time = 0;
3698 for (i = 0; i < entries; i++) {
3699 if (!track->
entry) {
3705 starts_with_SAP = 1;
3707 avio_wb32(pb, (0 << 31) | (ref_size & 0x7fffffff));
3709 avio_wb32(pb, (starts_with_SAP << 31) | (0 << 28) | 0);
3713 offset = pos + total_sidx_size - end_pos;
3721 int tracks,
int ref_size)
3726 for (round = 0; round < 2; round++) {
3739 if (tracks >= 0 && i != tracks)
3744 if (!track->
entry && ref_size > 0)
3841 int has_h264 = 0, has_video = 0;
3860 minor = has_h264 ? 0x100 : 0x200;
3863 minor = has_h264 ? 0x20000 : 0x10000;
3920 int audio_kbitrate = audio_codec->
bit_rate / 1000;
3921 int video_kbitrate =
FFMIN(video_codec->
bit_rate / 1000, 800 - audio_kbitrate);
3982 int video_streams_nb = 0, audio_streams_nb = 0, other_streams_nb = 0;
3993 if (video_streams_nb != 1 || audio_streams_nb != 1 || other_streams_nb) {
4005 int i, closed_gop = 0;
4007 for (i = 0; i < pkt->
size - 4; i++) {
4008 c = (c << 8) + pkt->
data[i];
4010 closed_gop = pkt->
data[i + 4] >> 6 & 0x01;
4011 }
else if (c == 0x100) {
4012 int temp_ref = (pkt->
data[i + 1] << 2) | (pkt->
data[i + 2] >> 6);
4013 if (!temp_ref || closed_gop)
4026 int seq = 0, entry = 0;
4029 for (next = start; next <
end; start = next) {
4053 for (i = 0; i < trk->
entry; i++)
4112 int i, first_track = -1;
4113 int64_t mdat_size = 0;
4121 if (track->
entry <= 1)
4139 "Estimating the duration of the last packet in a "
4140 "fragment, consider setting the duration field in "
4141 "AVPacket instead.\n");
4149 int buf_size, moov_size;
4218 if (first_track < 0)
4227 int buf_size, write_moof = 1, moof_tracks = -1;
4240 write_moof = i == first_track;
4299 unsigned int samples_in_chunk = 0;
4305 if (duration < 0 || duration > INT_MAX) {
4306 av_log(s,
AV_LOG_ERROR,
"Application provided duration: %"PRId64
" / timestamp: %"PRId64
" is out of range for mov/mp4 format\n",
4344 static uint16_t packed_size[16] =
4345 {13, 14, 16, 18, 20, 21, 27, 32, 6, 0, 0, 0, 0, 0, 0, 1};
4348 while (len < size && samples_in_chunk < 100) {
4349 len += packed_size[(pkt->
data[
len] >> 3) & 0x0F];
4352 if (samples_in_chunk > 1) {
4353 av_log(s,
AV_LOG_ERROR,
"fatal error, input is not a single packet, implement a AVParser for it\n");
4362 samples_in_chunk = 1;
4381 "use the audio bitstream filter 'aac_adtstoasc' to fix it "
4382 "('-bsf:a aac_adtstoasc' option with ffmpeg)\n");
4406 #if CONFIG_AC3_PARSER
4408 size = handle_eac3(mov, pkt, trk);