2 * Copyright (c) 2015 Martin Storsjo
4 * This file is part of Libav.
6 * Libav 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 * Libav 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 Libav; 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"
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 const char *format = "mp4";
57 uint8_t hash[HASH_SIZE];
59 AVStream *video_st, *audio_st;
60 int64_t audio_dts, video_dts;
68 enum AVPictureType last_picture;
78 static void count_warnings(void *avcl, int level, const char *fmt, va_list vl)
80 if (level == AV_LOG_WARNING)
84 static void init_count_warnings(void)
86 av_log_set_callback(count_warnings);
90 static void reset_count_warnings(void)
92 av_log_set_callback(av_log_default_callback);
95 static int io_write(void *opaque, uint8_t *buf, int size)
98 av_md5_update(md5, buf, size);
100 fwrite(buf, 1, size, out);
104 static void init_out(const char *name)
108 snprintf(buf, sizeof(buf), "%s.%s", cur_name, format);
112 out = fopen(buf, "wb");
119 static void close_out(void)
122 av_md5_final(md5, hash);
123 for (i = 0; i < HASH_SIZE; i++)
124 printf("%02x", hash[i]);
125 printf(" %d %s\n", out_size, cur_name);
131 static void check_func(int value, int line, const char *msg, ...)
136 printf("%d: ", line);
142 #define check(value, ...) check_func(value, __LINE__, __VA_ARGS__)
144 static void init_fps(int bf, int audio_preroll, int fps)
147 ctx = avformat_alloc_context();
150 ctx->oformat = av_guess_format(format, NULL, NULL);
153 ctx->pb = avio_alloc_context(iobuf, sizeof(iobuf), AVIO_FLAG_WRITE, NULL, NULL, io_write, NULL);
156 ctx->flags |= AVFMT_FLAG_BITEXACT;
158 st = avformat_new_stream(ctx, NULL);
161 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
162 st->codec->codec_id = AV_CODEC_ID_H264;
163 st->codec->width = 640;
164 st->codec->height = 480;
165 st->time_base.num = 1;
166 st->time_base.den = 30;
167 st->codec->extradata_size = sizeof(h264_extradata);
168 st->codec->extradata = av_mallocz(st->codec->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
169 if (!st->codec->extradata)
171 memcpy(st->codec->extradata, h264_extradata, sizeof(h264_extradata));
172 st->codec->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
175 st = avformat_new_stream(ctx, NULL);
178 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
179 st->codec->codec_id = AV_CODEC_ID_AAC;
180 st->codec->sample_rate = 44100;
181 st->codec->channels = 2;
182 st->time_base.num = 1;
183 st->time_base.den = 44100;
184 st->codec->extradata_size = sizeof(aac_extradata);
185 st->codec->extradata = av_mallocz(st->codec->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
186 if (!st->codec->extradata)
188 memcpy(st->codec->extradata, aac_extradata, sizeof(aac_extradata));
189 st->codec->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
192 if (avformat_write_header(ctx, &opts) < 0)
198 duration = video_st->time_base.den / fps;
199 audio_duration = 1024 * audio_st->time_base.den / audio_st->codec->sample_rate;
201 audio_preroll = 2048 * audio_st->time_base.den / audio_st->codec->sample_rate;
204 video_dts = bframes ? -duration : 0;
205 audio_dts = -audio_preroll;
208 static void init(int bf, int audio_preroll)
210 init_fps(bf, audio_preroll, 30);
213 static void mux_frames(int n)
215 int end_frames = frames + n;
219 av_init_packet(&pkt);
221 if (av_compare_ts(audio_dts, audio_st->time_base, video_dts, video_st->time_base) < 0) {
222 pkt.dts = pkt.pts = audio_dts;
223 pkt.stream_index = 1;
224 pkt.duration = audio_duration;
225 audio_dts += audio_duration;
227 if (frames == end_frames)
230 pkt.stream_index = 0;
231 pkt.duration = duration;
232 if ((frames % gop_size) == 0) {
233 pkt.flags |= AV_PKT_FLAG_KEY;
234 last_picture = AV_PICTURE_TYPE_I;
235 pkt.pts = pkt.dts + duration;
238 if (last_picture == AV_PICTURE_TYPE_P) {
239 last_picture = AV_PICTURE_TYPE_B;
241 video_dts = next_p_pts;
243 last_picture = AV_PICTURE_TYPE_P;
244 if (((frames + 1) % gop_size) == 0) {
245 pkt.pts = pkt.dts + duration;
248 next_p_pts = pkt.pts = pkt.dts + 2 * duration;
249 video_dts += duration;
260 AV_WB32(pktdata, pkt.pts);
265 if (skip_write_audio && pkt.stream_index == 1)
267 av_write_frame(ctx, &pkt);
271 static void mux_gops(int n)
273 mux_frames(gop_size * n);
276 static void skip_gops(int n)
283 static void signal_init_ts(void)
286 av_init_packet(&pkt);
290 pkt.stream_index = 0;
293 av_write_frame(ctx, &pkt);
295 pkt.stream_index = 1;
296 pkt.dts = pkt.pts = audio_dts;
297 av_write_frame(ctx, &pkt);
300 static void finish(void)
302 av_write_trailer(ctx);
304 avformat_free_context(ctx);
308 static void help(void)
310 printf("movenc-test [-w]\n"
311 "-w write output into files\n");
314 int main(int argc, char **argv)
317 uint8_t header[HASH_SIZE];
318 uint8_t content[HASH_SIZE];
323 c = getopt(argc, argv, "wh");
339 md5 = av_md5_alloc();
343 // Write a fragmented file with an initial moov that actually contains some
344 // samples. One moov+mdat with 1 second of data and one moof+mdat with 1
346 init_out("non-empty-moov");
347 av_dict_set(&opts, "movflags", "frag_keyframe", 0);
353 // Write a similar file, but with b-frames and audio preroll, handled
355 init_out("non-empty-moov-elst");
356 av_dict_set(&opts, "movflags", "frag_keyframe", 0);
357 av_dict_set(&opts, "use_editlist", "1", 0);
363 // Use b-frames but no audio-preroll, but without an edit list.
364 // Due to avoid_negative_ts == AVFMT_AVOID_NEG_TS_MAKE_ZERO, the dts
365 // of the first audio packet is > 0, but it is set to zero since edit
366 // lists aren't used, increasing the duration of the first packet instead.
367 init_out("non-empty-moov-no-elst");
368 av_dict_set(&opts, "movflags", "frag_keyframe", 0);
369 av_dict_set(&opts, "use_editlist", "0", 0);
376 // Write an ISMV, with b-frames and audio preroll.
378 av_dict_set(&opts, "movflags", "frag_keyframe", 0);
385 // An initial moov that doesn't contain any samples, followed by two
387 init_out("empty-moov");
388 av_dict_set(&opts, "movflags", "frag_keyframe+empty_moov", 0);
393 memcpy(content, hash, HASH_SIZE);
395 // Similar to the previous one, but with input that doesn't start at
396 // pts/dts 0. avoid_negative_ts behaves in the same way as
397 // in non-empty-moov-no-elst above.
398 init_out("empty-moov-no-elst");
399 av_dict_set(&opts, "movflags", "frag_keyframe+empty_moov", 0);
405 // Same as the previous one, but disable avoid_negative_ts (which
406 // would require using an edit list, but with empty_moov, one can't
407 // write a sensible edit list, when the start timestamps aren't known).
408 // This should trigger a warning - we check that the warning is produced.
409 init_count_warnings();
410 init_out("empty-moov-no-elst-no-adjust");
411 av_dict_set(&opts, "movflags", "frag_keyframe+empty_moov", 0);
412 av_dict_set(&opts, "avoid_negative_ts", "0", 0);
418 reset_count_warnings();
419 check(num_warnings > 0, "No warnings printed for unhandled start offset");
421 // Verify that delay_moov produces the same as empty_moov for
423 init_out("delay-moov");
424 av_dict_set(&opts, "movflags", "frag_keyframe+delay_moov", 0);
429 check(!memcmp(hash, content, HASH_SIZE), "delay_moov differs from empty_moov");
431 // Test writing content that requires an edit list using delay_moov
432 init_out("delay-moov-elst");
433 av_dict_set(&opts, "movflags", "frag_keyframe+delay_moov", 0);
439 // Test writing a file with one track lacking packets, with delay_moov.
440 skip_write_audio = 1;
441 init_out("delay-moov-empty-track");
442 av_dict_set(&opts, "movflags", "frag_keyframe+delay_moov", 0);
445 // The automatic flushing shouldn't output anything, since we're still
446 // waiting for data for some tracks
447 check(out_size == 0, "delay_moov flushed prematurely");
448 // When closed (or manually flushed), all the written data should still
452 check(out_size > 0, "delay_moov didn't output anything");
454 // Check that manually flushing still outputs things as expected. This
455 // produces two fragments, while the one above produces only one.
456 init_out("delay-moov-empty-track-flush");
457 av_dict_set(&opts, "movflags", "frag_custom+delay_moov", 0);
460 av_write_frame(ctx, NULL); // Force writing the moov
461 check(out_size > 0, "No moov written");
462 av_write_frame(ctx, NULL);
464 av_write_frame(ctx, NULL);
468 skip_write_audio = 0;
472 // Verify that the header written by delay_moov when manually flushed
473 // is identical to the one by empty_moov.
474 init_out("empty-moov-header");
475 av_dict_set(&opts, "movflags", "frag_keyframe+empty_moov", 0);
478 memcpy(header, hash, HASH_SIZE);
479 init_out("empty-moov-content");
481 // Written 2 seconds of content, with an automatic flush after 1 second.
482 check(out_size > 0, "No automatic flush?");
483 empty_moov_pos = prev_pos = out_size;
484 // Manually flush the second fragment
485 av_write_frame(ctx, NULL);
486 check(out_size > prev_pos, "No second fragment flushed?");
488 // Check that an extra flush doesn't output any more data
489 av_write_frame(ctx, NULL);
490 check(out_size == prev_pos, "More data written?");
492 memcpy(content, hash, HASH_SIZE);
493 // Ignore the trailer written here
496 init_out("delay-moov-header");
497 av_dict_set(&opts, "movflags", "frag_custom+delay_moov", 0);
499 check(out_size == 0, "Output written during init with delay_moov");
500 mux_gops(1); // Write 1 second of content
501 av_write_frame(ctx, NULL); // Force writing the moov
503 check(!memcmp(hash, header, HASH_SIZE), "delay_moov header differs from empty_moov");
504 init_out("delay-moov-content");
505 av_write_frame(ctx, NULL); // Flush the first fragment
506 check(out_size == empty_moov_pos, "Manually flushed content differs from automatically flushed, %d vs %d", out_size, empty_moov_pos);
507 mux_gops(1); // Write the rest of the content
508 av_write_frame(ctx, NULL); // Flush the second fragment
510 check(!memcmp(hash, content, HASH_SIZE), "delay_moov content differs from empty_moov");
514 // Verify that we can produce an identical second fragment without
515 // writing the first one. First write the reference fragments that
516 // we want to reproduce.
517 av_dict_set(&opts, "movflags", "frag_custom+empty_moov+dash", 0);
520 av_write_frame(ctx, NULL); // Output the first fragment
521 init_out("empty-moov-second-frag");
523 av_write_frame(ctx, NULL); // Output the second fragment
525 memcpy(content, hash, HASH_SIZE);
528 // Produce the same second fragment without actually writing the first
530 av_dict_set(&opts, "movflags", "frag_custom+empty_moov+dash+frag_discont", 0);
531 av_dict_set(&opts, "fragment_index", "2", 0);
532 av_dict_set(&opts, "avoid_negative_ts", "0", 0);
533 av_dict_set(&opts, "use_editlist", "0", 0);
536 init_out("empty-moov-second-frag-discont");
538 av_write_frame(ctx, NULL); // Output the second fragment
540 check(!memcmp(hash, content, HASH_SIZE), "discontinuously written fragment differs");
543 // Produce the same thing by using delay_moov, which requires a slightly
544 // different call sequence.
545 av_dict_set(&opts, "movflags", "frag_custom+delay_moov+dash+frag_discont", 0);
546 av_dict_set(&opts, "fragment_index", "2", 0);
550 av_write_frame(ctx, NULL); // Output the moov
551 init_out("delay-moov-second-frag-discont");
552 av_write_frame(ctx, NULL); // Output the second fragment
554 check(!memcmp(hash, content, HASH_SIZE), "discontinuously written fragment differs");
558 // Test discontinously written fragments with b-frames (where the
559 // assumption of starting at pts=0 works) but not with audio preroll
560 // (which can't be guessed).
561 av_dict_set(&opts, "movflags", "frag_custom+delay_moov+dash", 0);
564 init_out("delay-moov-elst-init");
565 av_write_frame(ctx, NULL); // Output the moov
567 memcpy(header, hash, HASH_SIZE);
568 av_write_frame(ctx, NULL); // Output the first fragment
569 init_out("delay-moov-elst-second-frag");
571 av_write_frame(ctx, NULL); // Output the second fragment
573 memcpy(content, hash, HASH_SIZE);
576 av_dict_set(&opts, "movflags", "frag_custom+delay_moov+dash+frag_discont", 0);
577 av_dict_set(&opts, "fragment_index", "2", 0);
580 mux_gops(1); // Write the second fragment
581 init_out("delay-moov-elst-init-discont");
582 av_write_frame(ctx, NULL); // Output the moov
584 check(!memcmp(hash, header, HASH_SIZE), "discontinuously written header differs");
585 init_out("delay-moov-elst-second-frag-discont");
586 av_write_frame(ctx, NULL); // Output the second fragment
588 check(!memcmp(hash, content, HASH_SIZE), "discontinuously written fragment differs");
592 // Test discontinously written fragments with b-frames and audio preroll,
593 // properly signaled.
594 av_dict_set(&opts, "movflags", "frag_custom+delay_moov+dash", 0);
597 init_out("delay-moov-elst-signal-init");
598 av_write_frame(ctx, NULL); // Output the moov
600 memcpy(header, hash, HASH_SIZE);
601 av_write_frame(ctx, NULL); // Output the first fragment
602 init_out("delay-moov-elst-signal-second-frag");
604 av_write_frame(ctx, NULL); // Output the second fragment
606 memcpy(content, hash, HASH_SIZE);
609 av_dict_set(&opts, "movflags", "frag_custom+delay_moov+dash+frag_discont", 0);
610 av_dict_set(&opts, "fragment_index", "2", 0);
614 mux_gops(1); // Write the second fragment
615 init_out("delay-moov-elst-signal-init-discont");
616 av_write_frame(ctx, NULL); // Output the moov
618 check(!memcmp(hash, header, HASH_SIZE), "discontinuously written header differs");
619 init_out("delay-moov-elst-signal-second-frag-discont");
620 av_write_frame(ctx, NULL); // Output the second fragment
622 check(!memcmp(hash, content, HASH_SIZE), "discontinuously written fragment differs");
626 // Test VFR content, with sidx atoms (which declare the pts duration
627 // of a fragment, forcing overriding the start pts of the next one).
628 // Here, the fragment duration in pts is significantly different from
629 // the duration in dts. The video stream starts at dts=-10,pts=0, and
630 // the second fragment starts at dts=155,pts=156. The trun duration sum
631 // of the first fragment is 165, which also is written as
632 // baseMediaDecodeTime in the tfdt in the second fragment. The sidx for
633 // the first fragment says earliest_presentation_time = 0 and
634 // subsegment_duration = 156, which also matches the sidx in the second
635 // fragment. For the audio stream, the pts and dts durations also don't
636 // match - the input stream starts at pts=-2048, but that part is excluded
639 av_dict_set(&opts, "movflags", "frag_keyframe+delay_moov+dash", 0);
641 mux_frames(gop_size/2);
643 mux_frames(gop_size/2);
648 // Test VFR content, with cleared duration fields. In these cases,
649 // the muxer must guess the duration of the last packet of each
650 // fragment. As long as the framerate doesn't vary (too much) at the
651 // fragment edge, it works just fine. Additionally, when automatically
652 // cutting fragments, the muxer already know the timestamps of the next
653 // packet for one stream (in most cases the video stream), avoiding
654 // having to use guesses for that one.
655 init_count_warnings();
657 init_out("vfr-noduration");
658 av_dict_set(&opts, "movflags", "frag_keyframe+delay_moov+dash", 0);
660 mux_frames(gop_size/2);
662 mux_frames(gop_size/2);
667 reset_count_warnings();
668 check(num_warnings > 0, "No warnings printed for filled in durations");
672 return check_faults > 0 ? 1 : 0;