2 * Copyright (c) 2015 Martin Storsjo
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #include "libavutil/intreadwrite.h"
24 #include "libavutil/mathematics.h"
25 #include "libavutil/md5.h"
27 #include "libavformat/avformat.h"
34 #include "compat/getopt.c"
39 static const uint8_t h264_extradata[] = {
40 0x01, 0x4d, 0x40, 0x1e, 0xff, 0xe1, 0x00, 0x02, 0x67, 0x4d, 0x01, 0x00, 0x02, 0x68, 0xef
42 static const uint8_t aac_extradata[] = {
47 static const char *format = "mp4";
57 uint8_t hash[HASH_SIZE];
59 AVStream *video_st, *audio_st;
60 int64_t audio_dts, video_dts;
64 int64_t audio_duration;
68 enum AVPictureType last_picture;
74 int fake_pkt_duration;
81 static void count_warnings(void *avcl, int level, const char *fmt, va_list vl)
83 if (level == AV_LOG_WARNING)
87 static void init_count_warnings(void)
89 av_log_set_callback(count_warnings);
93 static void reset_count_warnings(void)
95 av_log_set_callback(av_log_default_callback);
98 static int io_write(void *opaque, uint8_t *buf, int size)
101 av_md5_update(md5, buf, size);
103 fwrite(buf, 1, size, out);
107 static int io_write_data_type(void *opaque, uint8_t *buf, int size,
108 enum AVIODataMarkerType type, int64_t time)
110 char timebuf[30], content[5] = { 0 };
113 case AVIO_DATA_MARKER_HEADER: str = "header"; break;
114 case AVIO_DATA_MARKER_SYNC_POINT: str = "sync"; break;
115 case AVIO_DATA_MARKER_BOUNDARY_POINT: str = "boundary"; break;
116 case AVIO_DATA_MARKER_UNKNOWN: str = "unknown"; break;
117 case AVIO_DATA_MARKER_TRAILER: str = "trailer"; break;
118 default: str = "unknown"; break;
120 if (time == AV_NOPTS_VALUE)
121 snprintf(timebuf, sizeof(timebuf), "nopts");
123 snprintf(timebuf, sizeof(timebuf), "%"PRId64, time);
124 // There can be multiple header/trailer callbacks, only log the box type
125 // for header at out_size == 0
126 if (type != AVIO_DATA_MARKER_UNKNOWN &&
127 type != AVIO_DATA_MARKER_TRAILER &&
128 (type != AVIO_DATA_MARKER_HEADER || out_size == 0) &&
130 memcpy(content, &buf[4], 4);
132 snprintf(content, sizeof(content), "-");
133 printf("write_data len %d, time %s, type %s atom %s\n", size, timebuf, str, content);
134 return io_write(opaque, buf, size);
137 static void init_out(const char *name)
141 snprintf(buf, sizeof(buf), "%s.%s", cur_name, format);
145 out = fopen(buf, "wb");
152 static void close_out(void)
155 av_md5_final(md5, hash);
156 for (i = 0; i < HASH_SIZE; i++)
157 printf("%02x", hash[i]);
158 printf(" %d %s\n", out_size, cur_name);
164 static void check_func(int value, int line, const char *msg, ...)
169 printf("%d: ", line);
176 #define check(value, ...) check_func(value, __LINE__, __VA_ARGS__)
178 static void init_fps(int bf, int audio_preroll, int fps)
181 int iobuf_size = force_iobuf_size ? force_iobuf_size : sizeof(iobuf);
182 ctx = avformat_alloc_context();
185 ctx->oformat = av_guess_format(format, NULL, NULL);
188 ctx->pb = avio_alloc_context(iobuf, iobuf_size, AVIO_FLAG_WRITE, NULL, NULL, io_write, NULL);
191 ctx->pb->write_data_type = io_write_data_type;
192 ctx->flags |= AVFMT_FLAG_BITEXACT;
194 st = avformat_new_stream(ctx, NULL);
197 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
198 st->codecpar->codec_id = AV_CODEC_ID_H264;
199 st->codecpar->width = 640;
200 st->codecpar->height = 480;
201 st->time_base.num = 1;
202 st->time_base.den = 30;
203 st->codecpar->extradata_size = sizeof(h264_extradata);
204 st->codecpar->extradata = av_mallocz(st->codecpar->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
205 if (!st->codecpar->extradata)
207 memcpy(st->codecpar->extradata, h264_extradata, sizeof(h264_extradata));
210 st = avformat_new_stream(ctx, NULL);
213 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
214 st->codecpar->codec_id = AV_CODEC_ID_AAC;
215 st->codecpar->sample_rate = 44100;
216 st->codecpar->channels = 2;
217 st->time_base.num = 1;
218 st->time_base.den = 44100;
219 st->codecpar->extradata_size = sizeof(aac_extradata);
220 st->codecpar->extradata = av_mallocz(st->codecpar->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
221 if (!st->codecpar->extradata)
223 memcpy(st->codecpar->extradata, aac_extradata, sizeof(aac_extradata));
226 if (avformat_write_header(ctx, &opts) < 0)
232 duration = video_st->time_base.den / fps;
233 audio_duration = 1024LL * audio_st->time_base.den / audio_st->codecpar->sample_rate;
235 audio_preroll = 2048LL * audio_st->time_base.den / audio_st->codecpar->sample_rate;
238 video_dts = bframes ? -duration : 0;
239 audio_dts = -audio_preroll;
242 static void init(int bf, int audio_preroll)
244 init_fps(bf, audio_preroll, 30);
247 static void mux_frames(int n, int c)
249 int end_frames = frames + n;
252 uint8_t pktdata[8] = { 0 };
253 av_init_packet(&pkt);
255 if (av_compare_ts(audio_dts, audio_st->time_base, video_dts, video_st->time_base) < 0) {
256 pkt.dts = pkt.pts = audio_dts;
257 pkt.stream_index = 1;
258 pkt.duration = audio_duration;
259 audio_dts += audio_duration;
261 if (frames == end_frames)
264 pkt.stream_index = 0;
265 pkt.duration = duration;
266 if ((frames % gop_size) == 0) {
267 pkt.flags |= AV_PKT_FLAG_KEY;
268 last_picture = AV_PICTURE_TYPE_I;
269 pkt.pts = pkt.dts + duration;
272 if (last_picture == AV_PICTURE_TYPE_P) {
273 last_picture = AV_PICTURE_TYPE_B;
275 video_dts = next_p_pts;
277 last_picture = AV_PICTURE_TYPE_P;
278 if (((frames + 1) % gop_size) == 0) {
279 pkt.pts = pkt.dts + duration;
282 next_p_pts = pkt.pts = pkt.dts + 2 * duration;
283 video_dts += duration;
289 if (fake_pkt_duration)
290 pkt.duration = fake_pkt_duration;
296 AV_WB32(pktdata + 4, pkt.pts);
301 if (skip_write_audio && pkt.stream_index == 1)
305 pkt.pts += (1LL<<32);
306 pkt.dts += (1LL<<32);
310 av_interleaved_write_frame(ctx, &pkt);
312 av_write_frame(ctx, &pkt);
316 static void mux_gops(int n)
318 mux_frames(gop_size * n, 0);
321 static void skip_gops(int n)
328 static void signal_init_ts(void)
331 av_init_packet(&pkt);
335 pkt.stream_index = 0;
338 av_write_frame(ctx, &pkt);
340 pkt.stream_index = 1;
341 pkt.dts = pkt.pts = audio_dts;
342 av_write_frame(ctx, &pkt);
345 static void finish(void)
347 av_write_trailer(ctx);
348 avio_context_free(&ctx->pb);
349 avformat_free_context(ctx);
353 static void help(void)
355 printf("movenc-test [-w]\n"
356 "-w write output into files\n");
359 int main(int argc, char **argv)
362 uint8_t header[HASH_SIZE];
363 uint8_t content[HASH_SIZE];
368 c = getopt(argc, argv, "wh");
382 md5 = av_md5_alloc();
386 // Write a fragmented file with an initial moov that actually contains some
387 // samples. One moov+mdat with 1 second of data and one moof+mdat with 1
389 init_out("non-empty-moov");
390 av_dict_set(&opts, "movflags", "frag_keyframe", 0);
396 // Write a similar file, but with B-frames and audio preroll, handled
398 init_out("non-empty-moov-elst");
399 av_dict_set(&opts, "movflags", "frag_keyframe", 0);
400 av_dict_set(&opts, "use_editlist", "1", 0);
406 // Use B-frames but no audio-preroll, but without an edit list.
407 // Due to avoid_negative_ts == AVFMT_AVOID_NEG_TS_MAKE_ZERO, the dts
408 // of the first audio packet is > 0, but it is set to zero since edit
409 // lists aren't used, increasing the duration of the first packet instead.
410 init_out("non-empty-moov-no-elst");
411 av_dict_set(&opts, "movflags", "frag_keyframe", 0);
412 av_dict_set(&opts, "use_editlist", "0", 0);
419 // Write an ISMV, with B-frames and audio preroll.
421 av_dict_set(&opts, "movflags", "frag_keyframe", 0);
428 // An initial moov that doesn't contain any samples, followed by two
430 init_out("empty-moov");
431 av_dict_set(&opts, "movflags", "frag_keyframe+empty_moov", 0);
432 av_dict_set(&opts, "use_editlist", "0", 0);
437 memcpy(content, hash, HASH_SIZE);
439 // Similar to the previous one, but with input that doesn't start at
440 // pts/dts 0. avoid_negative_ts behaves in the same way as
441 // in non-empty-moov-no-elst above.
442 init_out("empty-moov-no-elst");
443 av_dict_set(&opts, "movflags", "frag_keyframe+empty_moov", 0);
449 // Same as the previous one, but disable avoid_negative_ts (which
450 // would require using an edit list, but with empty_moov, one can't
451 // write a sensible edit list, when the start timestamps aren't known).
452 // This should trigger a warning - we check that the warning is produced.
453 init_count_warnings();
454 init_out("empty-moov-no-elst-no-adjust");
455 av_dict_set(&opts, "movflags", "frag_keyframe+empty_moov", 0);
456 av_dict_set(&opts, "avoid_negative_ts", "0", 0);
462 reset_count_warnings();
463 check(num_warnings > 0, "No warnings printed for unhandled start offset");
465 // Verify that delay_moov produces the same as empty_moov for
467 init_out("delay-moov");
468 av_dict_set(&opts, "movflags", "frag_keyframe+delay_moov", 0);
469 av_dict_set(&opts, "use_editlist", "0", 0);
474 check(!memcmp(hash, content, HASH_SIZE), "delay_moov differs from empty_moov");
476 // Test writing content that requires an edit list using delay_moov
477 init_out("delay-moov-elst");
478 av_dict_set(&opts, "movflags", "frag_keyframe+delay_moov", 0);
484 // Test writing a file with one track lacking packets, with delay_moov.
485 skip_write_audio = 1;
486 init_out("delay-moov-empty-track");
487 av_dict_set(&opts, "movflags", "frag_keyframe+delay_moov", 0);
490 // The automatic flushing shouldn't output anything, since we're still
491 // waiting for data for some tracks
492 check(out_size == 0, "delay_moov flushed prematurely");
493 // When closed (or manually flushed), all the written data should still
497 check(out_size > 0, "delay_moov didn't output anything");
499 // Check that manually flushing still outputs things as expected. This
500 // produces two fragments, while the one above produces only one.
501 init_out("delay-moov-empty-track-flush");
502 av_dict_set(&opts, "movflags", "frag_custom+delay_moov", 0);
505 av_write_frame(ctx, NULL); // Force writing the moov
506 check(out_size > 0, "No moov written");
507 av_write_frame(ctx, NULL);
509 av_write_frame(ctx, NULL);
513 skip_write_audio = 0;
517 // Verify that the header written by delay_moov when manually flushed
518 // is identical to the one by empty_moov.
519 init_out("empty-moov-header");
520 av_dict_set(&opts, "movflags", "frag_keyframe+empty_moov", 0);
521 av_dict_set(&opts, "use_editlist", "0", 0);
524 memcpy(header, hash, HASH_SIZE);
525 init_out("empty-moov-content");
527 // Written 2 seconds of content, with an automatic flush after 1 second.
528 check(out_size > 0, "No automatic flush?");
529 empty_moov_pos = prev_pos = out_size;
530 // Manually flush the second fragment
531 av_write_frame(ctx, NULL);
532 check(out_size > prev_pos, "No second fragment flushed?");
534 // Check that an extra flush doesn't output any more data
535 av_write_frame(ctx, NULL);
536 check(out_size == prev_pos, "More data written?");
538 memcpy(content, hash, HASH_SIZE);
539 // Ignore the trailer written here
542 init_out("delay-moov-header");
543 av_dict_set(&opts, "movflags", "frag_custom+delay_moov", 0);
544 av_dict_set(&opts, "use_editlist", "0", 0);
546 check(out_size == 0, "Output written during init with delay_moov");
547 mux_gops(1); // Write 1 second of content
548 av_write_frame(ctx, NULL); // Force writing the moov
550 check(!memcmp(hash, header, HASH_SIZE), "delay_moov header differs from empty_moov");
551 init_out("delay-moov-content");
552 av_write_frame(ctx, NULL); // Flush the first fragment
553 check(out_size == empty_moov_pos, "Manually flushed content differs from automatically flushed, %d vs %d", out_size, empty_moov_pos);
554 mux_gops(1); // Write the rest of the content
555 av_write_frame(ctx, NULL); // Flush the second fragment
557 check(!memcmp(hash, content, HASH_SIZE), "delay_moov content differs from empty_moov");
561 // Verify that we can produce an identical second fragment without
562 // writing the first one. First write the reference fragments that
563 // we want to reproduce.
564 av_dict_set(&opts, "movflags", "frag_custom+empty_moov+dash", 0);
567 av_write_frame(ctx, NULL); // Output the first fragment
568 init_out("empty-moov-second-frag");
570 av_write_frame(ctx, NULL); // Output the second fragment
572 memcpy(content, hash, HASH_SIZE);
575 // Produce the same second fragment without actually writing the first
577 av_dict_set(&opts, "movflags", "frag_custom+empty_moov+dash+frag_discont", 0);
578 av_dict_set(&opts, "fragment_index", "2", 0);
579 av_dict_set(&opts, "avoid_negative_ts", "0", 0);
580 av_dict_set(&opts, "use_editlist", "0", 0);
583 init_out("empty-moov-second-frag-discont");
585 av_write_frame(ctx, NULL); // Output the second fragment
587 check(!memcmp(hash, content, HASH_SIZE), "discontinuously written fragment differs");
590 // Produce the same thing by using delay_moov, which requires a slightly
591 // different call sequence.
592 av_dict_set(&opts, "movflags", "frag_custom+delay_moov+dash+frag_discont", 0);
593 av_dict_set(&opts, "fragment_index", "2", 0);
597 av_write_frame(ctx, NULL); // Output the moov
598 init_out("delay-moov-second-frag-discont");
599 av_write_frame(ctx, NULL); // Output the second fragment
601 check(!memcmp(hash, content, HASH_SIZE), "discontinuously written fragment differs");
605 // Test discontinuously written fragments with B-frames (where the
606 // assumption of starting at pts=0 works) but not with audio preroll
607 // (which can't be guessed).
608 av_dict_set(&opts, "movflags", "frag_custom+delay_moov+dash", 0);
611 init_out("delay-moov-elst-init");
612 av_write_frame(ctx, NULL); // Output the moov
614 memcpy(header, hash, HASH_SIZE);
615 av_write_frame(ctx, NULL); // Output the first fragment
616 init_out("delay-moov-elst-second-frag");
618 av_write_frame(ctx, NULL); // Output the second fragment
620 memcpy(content, hash, HASH_SIZE);
623 av_dict_set(&opts, "movflags", "frag_custom+delay_moov+dash+frag_discont", 0);
624 av_dict_set(&opts, "fragment_index", "2", 0);
627 mux_gops(1); // Write the second fragment
628 init_out("delay-moov-elst-init-discont");
629 av_write_frame(ctx, NULL); // Output the moov
631 check(!memcmp(hash, header, HASH_SIZE), "discontinuously written header differs");
632 init_out("delay-moov-elst-second-frag-discont");
633 av_write_frame(ctx, NULL); // Output the second fragment
635 check(!memcmp(hash, content, HASH_SIZE), "discontinuously written fragment differs");
639 // Test discontinuously written fragments with B-frames and audio preroll,
640 // properly signaled.
641 av_dict_set(&opts, "movflags", "frag_custom+delay_moov+dash", 0);
644 init_out("delay-moov-elst-signal-init");
645 av_write_frame(ctx, NULL); // Output the moov
647 memcpy(header, hash, HASH_SIZE);
648 av_write_frame(ctx, NULL); // Output the first fragment
649 init_out("delay-moov-elst-signal-second-frag");
651 av_write_frame(ctx, NULL); // Output the second fragment
653 memcpy(content, hash, HASH_SIZE);
656 av_dict_set(&opts, "movflags", "frag_custom+delay_moov+dash+frag_discont", 0);
657 av_dict_set(&opts, "fragment_index", "2", 0);
661 mux_gops(1); // Write the second fragment
662 init_out("delay-moov-elst-signal-init-discont");
663 av_write_frame(ctx, NULL); // Output the moov
665 check(!memcmp(hash, header, HASH_SIZE), "discontinuously written header differs");
666 init_out("delay-moov-elst-signal-second-frag-discont");
667 av_write_frame(ctx, NULL); // Output the second fragment
669 check(!memcmp(hash, content, HASH_SIZE), "discontinuously written fragment differs");
673 // Test muxing discontinuous fragments with very large (> (1<<31)) timestamps.
674 av_dict_set(&opts, "movflags", "frag_custom+delay_moov+dash+frag_discont", 0);
675 av_dict_set(&opts, "fragment_index", "2", 0);
679 mux_frames(gop_size, 1); // Write the second fragment
680 init_out("delay-moov-elst-signal-init-discont-largets");
681 av_write_frame(ctx, NULL); // Output the moov
683 init_out("delay-moov-elst-signal-second-frag-discont-largets");
684 av_write_frame(ctx, NULL); // Output the second fragment
688 // Test VFR content, with sidx atoms (which declare the pts duration
689 // of a fragment, forcing overriding the start pts of the next one).
690 // Here, the fragment duration in pts is significantly different from
691 // the duration in dts. The video stream starts at dts=-10,pts=0, and
692 // the second fragment starts at dts=155,pts=156. The trun duration sum
693 // of the first fragment is 165, which also is written as
694 // baseMediaDecodeTime in the tfdt in the second fragment. The sidx for
695 // the first fragment says earliest_presentation_time = 0 and
696 // subsegment_duration = 156, which also matches the sidx in the second
697 // fragment. For the audio stream, the pts and dts durations also don't
698 // match - the input stream starts at pts=-2048, but that part is excluded
701 av_dict_set(&opts, "movflags", "frag_keyframe+delay_moov+dash", 0);
703 mux_frames(gop_size/2, 0);
705 mux_frames(gop_size/2, 0);
710 // Test VFR content, with cleared duration fields. In these cases,
711 // the muxer must guess the duration of the last packet of each
712 // fragment. As long as the framerate doesn't vary (too much) at the
713 // fragment edge, it works just fine. Additionally, when automatically
714 // cutting fragments, the muxer already know the timestamps of the next
715 // packet for one stream (in most cases the video stream), avoiding
716 // having to use guesses for that one.
717 init_count_warnings();
719 init_out("vfr-noduration");
720 av_dict_set(&opts, "movflags", "frag_keyframe+delay_moov+dash", 0);
722 mux_frames(gop_size/2, 0);
724 mux_frames(gop_size/2, 0);
729 reset_count_warnings();
730 check(num_warnings > 0, "No warnings printed for filled in durations");
732 // Test with an IO buffer size that is too small to hold a full fragment;
733 // this will cause write_data_type to be called with the type unknown.
734 force_iobuf_size = 1500;
735 init_out("large_frag");
736 av_dict_set(&opts, "movflags", "frag_keyframe+delay_moov", 0);
741 force_iobuf_size = 0;
743 // Test VFR content with bframes with interleaving.
744 // Here, using av_interleaved_write_frame allows the muxer to get the
745 // fragment end durations right. We always set the packet duration to
746 // the expected, but we simulate dropped frames at one point.
748 init_out("vfr-noduration-interleave");
749 av_dict_set(&opts, "movflags", "frag_keyframe+delay_moov", 0);
750 av_dict_set(&opts, "frag_duration", "650000", 0);
752 mux_frames(gop_size/2, 0);
753 // Pretend that the packet duration is the normal, even if
754 // we actually skip a bunch of frames. (I.e., simulate that
755 // we don't know of the framedrop in advance.)
756 fake_pkt_duration = duration;
759 fake_pkt_duration = 0;
761 mux_frames(gop_size/2 - 1, 0);
768 // Write a fragmented file with b-frames and audio preroll,
769 // with negative cts values, removing the edit list for the
771 init_out("delay-moov-elst-neg-cts");
772 av_dict_set(&opts, "movflags", "frag_keyframe+delay_moov+negative_cts_offsets", 0);
778 // Write a fragmented file with b-frames without audio preroll,
779 // with negative cts values, avoiding any edit lists, allowing
780 // to use empty_moov instead of delay_moov.
781 init_out("empty-moov-neg-cts");
782 av_dict_set(&opts, "movflags", "frag_keyframe+empty_moov+negative_cts_offsets", 0);
790 return check_faults > 0 ? 1 : 0;