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;
79 static void count_warnings(void *avcl, int level, const char *fmt, va_list vl)
81 if (level == AV_LOG_WARNING)
85 static void init_count_warnings(void)
87 av_log_set_callback(count_warnings);
91 static void reset_count_warnings(void)
93 av_log_set_callback(av_log_default_callback);
96 static int io_write(void *opaque, uint8_t *buf, int size)
99 av_md5_update(md5, buf, size);
101 fwrite(buf, 1, size, out);
105 static int io_write_data_type(void *opaque, uint8_t *buf, int size,
106 enum AVIODataMarkerType type, int64_t time)
108 char timebuf[30], content[5] = { 0 };
111 case AVIO_DATA_MARKER_HEADER: str = "header"; break;
112 case AVIO_DATA_MARKER_SYNC_POINT: str = "sync"; break;
113 case AVIO_DATA_MARKER_BOUNDARY_POINT: str = "boundary"; break;
114 case AVIO_DATA_MARKER_UNKNOWN: str = "unknown"; break;
115 case AVIO_DATA_MARKER_TRAILER: str = "trailer"; break;
117 if (time == AV_NOPTS_VALUE)
118 snprintf(timebuf, sizeof(timebuf), "nopts");
120 snprintf(timebuf, sizeof(timebuf), "%"PRId64, time);
121 // There can be multiple header/trailer callbacks, only log the box type
122 // for header at out_size == 0
123 if (type != AVIO_DATA_MARKER_UNKNOWN &&
124 type != AVIO_DATA_MARKER_TRAILER &&
125 (type != AVIO_DATA_MARKER_HEADER || out_size == 0) &&
127 memcpy(content, &buf[4], 4);
129 snprintf(content, sizeof(content), "-");
130 printf("write_data len %d, time %s, type %s atom %s\n", size, timebuf, str, content);
131 return io_write(opaque, buf, size);
134 static void init_out(const char *name)
138 snprintf(buf, sizeof(buf), "%s.%s", cur_name, format);
142 out = fopen(buf, "wb");
149 static void close_out(void)
152 av_md5_final(md5, hash);
153 for (i = 0; i < HASH_SIZE; i++)
154 printf("%02x", hash[i]);
155 printf(" %d %s\n", out_size, cur_name);
161 static void check_func(int value, int line, const char *msg, ...)
166 printf("%d: ", line);
173 #define check(value, ...) check_func(value, __LINE__, __VA_ARGS__)
175 static void init_fps(int bf, int audio_preroll, int fps)
178 int iobuf_size = force_iobuf_size ? force_iobuf_size : sizeof(iobuf);
179 ctx = avformat_alloc_context();
182 ctx->oformat = av_guess_format(format, NULL, NULL);
185 ctx->pb = avio_alloc_context(iobuf, iobuf_size, AVIO_FLAG_WRITE, NULL, NULL, io_write, NULL);
188 ctx->pb->write_data_type = io_write_data_type;
189 ctx->flags |= AVFMT_FLAG_BITEXACT;
191 st = avformat_new_stream(ctx, NULL);
194 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
195 st->codecpar->codec_id = AV_CODEC_ID_H264;
196 st->codecpar->width = 640;
197 st->codecpar->height = 480;
198 st->time_base.num = 1;
199 st->time_base.den = 30;
200 st->codecpar->extradata_size = sizeof(h264_extradata);
201 st->codecpar->extradata = av_mallocz(st->codecpar->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
202 if (!st->codecpar->extradata)
204 memcpy(st->codecpar->extradata, h264_extradata, sizeof(h264_extradata));
207 st = avformat_new_stream(ctx, NULL);
210 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
211 st->codecpar->codec_id = AV_CODEC_ID_AAC;
212 st->codecpar->sample_rate = 44100;
213 st->codecpar->channels = 2;
214 st->time_base.num = 1;
215 st->time_base.den = 44100;
216 st->codecpar->extradata_size = sizeof(aac_extradata);
217 st->codecpar->extradata = av_mallocz(st->codecpar->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
218 if (!st->codecpar->extradata)
220 memcpy(st->codecpar->extradata, aac_extradata, sizeof(aac_extradata));
223 if (avformat_write_header(ctx, &opts) < 0)
229 duration = video_st->time_base.den / fps;
230 audio_duration = 1024LL * audio_st->time_base.den / audio_st->codecpar->sample_rate;
232 audio_preroll = 2048LL * audio_st->time_base.den / audio_st->codecpar->sample_rate;
235 video_dts = bframes ? -duration : 0;
236 audio_dts = -audio_preroll;
239 static void init(int bf, int audio_preroll)
241 init_fps(bf, audio_preroll, 30);
244 static void mux_frames(int n)
246 int end_frames = frames + n;
249 uint8_t pktdata[8] = { 0 };
250 av_init_packet(&pkt);
252 if (av_compare_ts(audio_dts, audio_st->time_base, video_dts, video_st->time_base) < 0) {
253 pkt.dts = pkt.pts = audio_dts;
254 pkt.stream_index = 1;
255 pkt.duration = audio_duration;
256 audio_dts += audio_duration;
258 if (frames == end_frames)
261 pkt.stream_index = 0;
262 pkt.duration = duration;
263 if ((frames % gop_size) == 0) {
264 pkt.flags |= AV_PKT_FLAG_KEY;
265 last_picture = AV_PICTURE_TYPE_I;
266 pkt.pts = pkt.dts + duration;
269 if (last_picture == AV_PICTURE_TYPE_P) {
270 last_picture = AV_PICTURE_TYPE_B;
272 video_dts = next_p_pts;
274 last_picture = AV_PICTURE_TYPE_P;
275 if (((frames + 1) % gop_size) == 0) {
276 pkt.pts = pkt.dts + duration;
279 next_p_pts = pkt.pts = pkt.dts + 2 * duration;
280 video_dts += duration;
291 AV_WB32(pktdata + 4, pkt.pts);
296 if (skip_write_audio && pkt.stream_index == 1)
298 av_write_frame(ctx, &pkt);
302 static void mux_gops(int n)
304 mux_frames(gop_size * n);
307 static void skip_gops(int n)
314 static void signal_init_ts(void)
317 av_init_packet(&pkt);
321 pkt.stream_index = 0;
324 av_write_frame(ctx, &pkt);
326 pkt.stream_index = 1;
327 pkt.dts = pkt.pts = audio_dts;
328 av_write_frame(ctx, &pkt);
331 static void finish(void)
333 av_write_trailer(ctx);
335 avformat_free_context(ctx);
339 static void help(void)
341 printf("movenc-test [-w]\n"
342 "-w write output into files\n");
345 int main(int argc, char **argv)
348 uint8_t header[HASH_SIZE];
349 uint8_t content[HASH_SIZE];
354 c = getopt(argc, argv, "wh");
370 md5 = av_md5_alloc();
374 // Write a fragmented file with an initial moov that actually contains some
375 // samples. One moov+mdat with 1 second of data and one moof+mdat with 1
377 init_out("non-empty-moov");
378 av_dict_set(&opts, "movflags", "frag_keyframe", 0);
384 // Write a similar file, but with B-frames and audio preroll, handled
386 init_out("non-empty-moov-elst");
387 av_dict_set(&opts, "movflags", "frag_keyframe", 0);
388 av_dict_set(&opts, "use_editlist", "1", 0);
394 // Use B-frames but no audio-preroll, but without an edit list.
395 // Due to avoid_negative_ts == AVFMT_AVOID_NEG_TS_MAKE_ZERO, the dts
396 // of the first audio packet is > 0, but it is set to zero since edit
397 // lists aren't used, increasing the duration of the first packet instead.
398 init_out("non-empty-moov-no-elst");
399 av_dict_set(&opts, "movflags", "frag_keyframe", 0);
400 av_dict_set(&opts, "use_editlist", "0", 0);
407 // Write an ISMV, with B-frames and audio preroll.
409 av_dict_set(&opts, "movflags", "frag_keyframe", 0);
416 // An initial moov that doesn't contain any samples, followed by two
418 init_out("empty-moov");
419 av_dict_set(&opts, "movflags", "frag_keyframe+empty_moov", 0);
420 av_dict_set(&opts, "use_editlist", "0", 0);
425 memcpy(content, hash, HASH_SIZE);
427 // Similar to the previous one, but with input that doesn't start at
428 // pts/dts 0. avoid_negative_ts behaves in the same way as
429 // in non-empty-moov-no-elst above.
430 init_out("empty-moov-no-elst");
431 av_dict_set(&opts, "movflags", "frag_keyframe+empty_moov", 0);
437 // Same as the previous one, but disable avoid_negative_ts (which
438 // would require using an edit list, but with empty_moov, one can't
439 // write a sensible edit list, when the start timestamps aren't known).
440 // This should trigger a warning - we check that the warning is produced.
441 init_count_warnings();
442 init_out("empty-moov-no-elst-no-adjust");
443 av_dict_set(&opts, "movflags", "frag_keyframe+empty_moov", 0);
444 av_dict_set(&opts, "avoid_negative_ts", "0", 0);
450 reset_count_warnings();
451 check(num_warnings > 0, "No warnings printed for unhandled start offset");
453 // Verify that delay_moov produces the same as empty_moov for
455 init_out("delay-moov");
456 av_dict_set(&opts, "movflags", "frag_keyframe+delay_moov", 0);
457 av_dict_set(&opts, "use_editlist", "0", 0);
462 check(!memcmp(hash, content, HASH_SIZE), "delay_moov differs from empty_moov");
464 // Test writing content that requires an edit list using delay_moov
465 init_out("delay-moov-elst");
466 av_dict_set(&opts, "movflags", "frag_keyframe+delay_moov", 0);
472 // Test writing a file with one track lacking packets, with delay_moov.
473 skip_write_audio = 1;
474 init_out("delay-moov-empty-track");
475 av_dict_set(&opts, "movflags", "frag_keyframe+delay_moov", 0);
478 // The automatic flushing shouldn't output anything, since we're still
479 // waiting for data for some tracks
480 check(out_size == 0, "delay_moov flushed prematurely");
481 // When closed (or manually flushed), all the written data should still
485 check(out_size > 0, "delay_moov didn't output anything");
487 // Check that manually flushing still outputs things as expected. This
488 // produces two fragments, while the one above produces only one.
489 init_out("delay-moov-empty-track-flush");
490 av_dict_set(&opts, "movflags", "frag_custom+delay_moov", 0);
493 av_write_frame(ctx, NULL); // Force writing the moov
494 check(out_size > 0, "No moov written");
495 av_write_frame(ctx, NULL);
497 av_write_frame(ctx, NULL);
501 skip_write_audio = 0;
505 // Verify that the header written by delay_moov when manually flushed
506 // is identical to the one by empty_moov.
507 init_out("empty-moov-header");
508 av_dict_set(&opts, "movflags", "frag_keyframe+empty_moov", 0);
509 av_dict_set(&opts, "use_editlist", "0", 0);
512 memcpy(header, hash, HASH_SIZE);
513 init_out("empty-moov-content");
515 // Written 2 seconds of content, with an automatic flush after 1 second.
516 check(out_size > 0, "No automatic flush?");
517 empty_moov_pos = prev_pos = out_size;
518 // Manually flush the second fragment
519 av_write_frame(ctx, NULL);
520 check(out_size > prev_pos, "No second fragment flushed?");
522 // Check that an extra flush doesn't output any more data
523 av_write_frame(ctx, NULL);
524 check(out_size == prev_pos, "More data written?");
526 memcpy(content, hash, HASH_SIZE);
527 // Ignore the trailer written here
530 init_out("delay-moov-header");
531 av_dict_set(&opts, "movflags", "frag_custom+delay_moov", 0);
532 av_dict_set(&opts, "use_editlist", "0", 0);
534 check(out_size == 0, "Output written during init with delay_moov");
535 mux_gops(1); // Write 1 second of content
536 av_write_frame(ctx, NULL); // Force writing the moov
538 check(!memcmp(hash, header, HASH_SIZE), "delay_moov header differs from empty_moov");
539 init_out("delay-moov-content");
540 av_write_frame(ctx, NULL); // Flush the first fragment
541 check(out_size == empty_moov_pos, "Manually flushed content differs from automatically flushed, %d vs %d", out_size, empty_moov_pos);
542 mux_gops(1); // Write the rest of the content
543 av_write_frame(ctx, NULL); // Flush the second fragment
545 check(!memcmp(hash, content, HASH_SIZE), "delay_moov content differs from empty_moov");
549 // Verify that we can produce an identical second fragment without
550 // writing the first one. First write the reference fragments that
551 // we want to reproduce.
552 av_dict_set(&opts, "movflags", "frag_custom+empty_moov+dash", 0);
555 av_write_frame(ctx, NULL); // Output the first fragment
556 init_out("empty-moov-second-frag");
558 av_write_frame(ctx, NULL); // Output the second fragment
560 memcpy(content, hash, HASH_SIZE);
563 // Produce the same second fragment without actually writing the first
565 av_dict_set(&opts, "movflags", "frag_custom+empty_moov+dash+frag_discont", 0);
566 av_dict_set(&opts, "fragment_index", "2", 0);
567 av_dict_set(&opts, "avoid_negative_ts", "0", 0);
568 av_dict_set(&opts, "use_editlist", "0", 0);
571 init_out("empty-moov-second-frag-discont");
573 av_write_frame(ctx, NULL); // Output the second fragment
575 check(!memcmp(hash, content, HASH_SIZE), "discontinuously written fragment differs");
578 // Produce the same thing by using delay_moov, which requires a slightly
579 // different call sequence.
580 av_dict_set(&opts, "movflags", "frag_custom+delay_moov+dash+frag_discont", 0);
581 av_dict_set(&opts, "fragment_index", "2", 0);
585 av_write_frame(ctx, NULL); // Output the moov
586 init_out("delay-moov-second-frag-discont");
587 av_write_frame(ctx, NULL); // Output the second fragment
589 check(!memcmp(hash, content, HASH_SIZE), "discontinuously written fragment differs");
593 // Test discontinuously written fragments with B-frames (where the
594 // assumption of starting at pts=0 works) but not with audio preroll
595 // (which can't be guessed).
596 av_dict_set(&opts, "movflags", "frag_custom+delay_moov+dash", 0);
599 init_out("delay-moov-elst-init");
600 av_write_frame(ctx, NULL); // Output the moov
602 memcpy(header, hash, HASH_SIZE);
603 av_write_frame(ctx, NULL); // Output the first fragment
604 init_out("delay-moov-elst-second-frag");
606 av_write_frame(ctx, NULL); // Output the second fragment
608 memcpy(content, hash, HASH_SIZE);
611 av_dict_set(&opts, "movflags", "frag_custom+delay_moov+dash+frag_discont", 0);
612 av_dict_set(&opts, "fragment_index", "2", 0);
615 mux_gops(1); // Write the second fragment
616 init_out("delay-moov-elst-init-discont");
617 av_write_frame(ctx, NULL); // Output the moov
619 check(!memcmp(hash, header, HASH_SIZE), "discontinuously written header differs");
620 init_out("delay-moov-elst-second-frag-discont");
621 av_write_frame(ctx, NULL); // Output the second fragment
623 check(!memcmp(hash, content, HASH_SIZE), "discontinuously written fragment differs");
627 // Test discontinuously written fragments with B-frames and audio preroll,
628 // properly signaled.
629 av_dict_set(&opts, "movflags", "frag_custom+delay_moov+dash", 0);
632 init_out("delay-moov-elst-signal-init");
633 av_write_frame(ctx, NULL); // Output the moov
635 memcpy(header, hash, HASH_SIZE);
636 av_write_frame(ctx, NULL); // Output the first fragment
637 init_out("delay-moov-elst-signal-second-frag");
639 av_write_frame(ctx, NULL); // Output the second fragment
641 memcpy(content, hash, HASH_SIZE);
644 av_dict_set(&opts, "movflags", "frag_custom+delay_moov+dash+frag_discont", 0);
645 av_dict_set(&opts, "fragment_index", "2", 0);
649 mux_gops(1); // Write the second fragment
650 init_out("delay-moov-elst-signal-init-discont");
651 av_write_frame(ctx, NULL); // Output the moov
653 check(!memcmp(hash, header, HASH_SIZE), "discontinuously written header differs");
654 init_out("delay-moov-elst-signal-second-frag-discont");
655 av_write_frame(ctx, NULL); // Output the second fragment
657 check(!memcmp(hash, content, HASH_SIZE), "discontinuously written fragment differs");
661 // Test VFR content, with sidx atoms (which declare the pts duration
662 // of a fragment, forcing overriding the start pts of the next one).
663 // Here, the fragment duration in pts is significantly different from
664 // the duration in dts. The video stream starts at dts=-10,pts=0, and
665 // the second fragment starts at dts=155,pts=156. The trun duration sum
666 // of the first fragment is 165, which also is written as
667 // baseMediaDecodeTime in the tfdt in the second fragment. The sidx for
668 // the first fragment says earliest_presentation_time = 0 and
669 // subsegment_duration = 156, which also matches the sidx in the second
670 // fragment. For the audio stream, the pts and dts durations also don't
671 // match - the input stream starts at pts=-2048, but that part is excluded
674 av_dict_set(&opts, "movflags", "frag_keyframe+delay_moov+dash", 0);
676 mux_frames(gop_size/2);
678 mux_frames(gop_size/2);
683 // Test VFR content, with cleared duration fields. In these cases,
684 // the muxer must guess the duration of the last packet of each
685 // fragment. As long as the framerate doesn't vary (too much) at the
686 // fragment edge, it works just fine. Additionally, when automatically
687 // cutting fragments, the muxer already know the timestamps of the next
688 // packet for one stream (in most cases the video stream), avoiding
689 // having to use guesses for that one.
690 init_count_warnings();
692 init_out("vfr-noduration");
693 av_dict_set(&opts, "movflags", "frag_keyframe+delay_moov+dash", 0);
695 mux_frames(gop_size/2);
697 mux_frames(gop_size/2);
702 reset_count_warnings();
703 check(num_warnings > 0, "No warnings printed for filled in durations");
705 // Test with an IO buffer size that is too small to hold a full fragment;
706 // this will cause write_data_type to be called with the type unknown.
707 force_iobuf_size = 1500;
708 init_out("large_frag");
709 av_dict_set(&opts, "movflags", "frag_keyframe+delay_moov", 0);
714 force_iobuf_size = 0;
718 return check_faults > 0 ? 1 : 0;