FFmpeg
Loading...
Searching...
No Matches
api-movenc-test.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2015 Martin Storsjo
3 *
4 * This file is part of FFmpeg.
5 *
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.
10 *
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.
15 *
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
19 */
20
21#include "config.h"
22
25#include "libavutil/md5.h"
26#include "libavutil/mem.h"
27
29
30#if HAVE_UNISTD_H
31#include <unistd.h>
32#endif
33
34#if !HAVE_GETOPT
35#include "compat/getopt.c"
36#endif
37
38#define HASH_SIZE 16
39
40static const uint8_t h264_extradata[] = {
41 0x01, 0x4d, 0x40, 0x1e, 0xff, 0xe1, 0x00, 0x02, 0x67, 0x4d, 0x01, 0x00, 0x02, 0x68, 0xef
42};
43static const uint8_t aac_extradata[] = {
44 0x12, 0x10
45};
46
47
48static const char *format = "mp4";
50static uint8_t iobuf[32768];
52
53static int write_file;
54static const char *out_path;
55static const char *cur_name;
56static FILE* out;
57static int out_size;
58static struct AVMD5* md5;
59static uint8_t hash[HASH_SIZE];
60
61static AVPacket *pkt;
64
65static int bframes;
68static int frames;
69static int gop_size;
72static int skip_write;
74static int clear_duration;
76static int do_interleave;
78
79static int num_warnings;
80
81static int check_faults;
82
83
84static void count_warnings(void *avcl, int level, const char *fmt, va_list vl)
85{
86 if (level == AV_LOG_WARNING)
88}
89
95
100
101static int io_write(void *opaque, const uint8_t *buf, int size)
102{
103 out_size += size;
104 av_md5_update(md5, buf, size);
105 if (out)
106 fwrite(buf, 1, size, out);
107 return size;
108}
109
110static int io_write_data_type(void *opaque, const uint8_t *buf, int size,
112{
113 char timebuf[30], content[5] = { 0 };
114 const char *str;
115 switch (type) {
116 case AVIO_DATA_MARKER_HEADER: str = "header"; break;
117 case AVIO_DATA_MARKER_SYNC_POINT: str = "sync"; break;
118 case AVIO_DATA_MARKER_BOUNDARY_POINT: str = "boundary"; break;
119 case AVIO_DATA_MARKER_UNKNOWN: str = "unknown"; break;
120 case AVIO_DATA_MARKER_TRAILER: str = "trailer"; break;
121 default: str = "unknown"; break;
122 }
123 if (time == AV_NOPTS_VALUE)
124 snprintf(timebuf, sizeof(timebuf), "nopts");
125 else
126 snprintf(timebuf, sizeof(timebuf), "%"PRId64, time);
127 // There can be multiple header/trailer callbacks, only log the box type
128 // for header at out_size == 0
132 size >= 8)
133 memcpy(content, &buf[4], 4);
134 else
135 snprintf(content, sizeof(content), "-");
136 printf("write_data len %d, time %s, type %s atom %s\n", size, timebuf, str, content);
137 return io_write(opaque, buf, size);
138}
139
140static void init_out(const char *name)
141{
142 char buf[100];
143 cur_name = name;
144 snprintf(buf, sizeof(buf), "%s.%s", cur_name, format);
145
147 if (write_file) {
148 out = fopen(buf, "wb");
149 if (!out)
150 perror(buf);
151 }
152 out_size = 0;
153}
154
155static void close_out(void)
156{
157 int i;
159 for (i = 0; i < HASH_SIZE; i++)
160 printf("%02x", hash[i]);
161 printf(" %d %s\n", out_size, cur_name);
162 if (out)
163 fclose(out);
164 out = NULL;
165}
166
167static void check_func(int value, int line, const char *msg, ...)
168{
169 if (!value) {
170 va_list ap;
171 va_start(ap, msg);
172 printf("%d: ", line);
173 vprintf(msg, ap);
174 printf("\n");
175 check_faults++;
176 va_end(ap);
177 }
178}
179#define check(value, ...) check_func(value, __LINE__, __VA_ARGS__)
180
181static void init_fps(int bf, int audio_preroll, int fps, int id3)
182{
183 AVStream *st;
184 int iobuf_size = force_iobuf_size ? force_iobuf_size : sizeof(iobuf);
186 exit(1);
187 if (out_path) {
188 // A real file, as global_sidx seeks and reopens the output
189 if (avio_open(&ctx->pb, out_path, AVIO_FLAG_WRITE) < 0)
190 exit(1);
191 } else {
192 ctx->pb = avio_alloc_context(iobuf, iobuf_size, 1, NULL, NULL, io_write, NULL);
193 if (!ctx->pb)
194 exit(1);
195 ctx->pb->write_data_type = io_write_data_type;
196 }
197 ctx->flags |= AVFMT_FLAG_BITEXACT;
198
200 if (!st)
201 exit(1);
204 st->codecpar->width = 640;
205 st->codecpar->height = 480;
206 st->time_base.num = 1;
207 st->time_base.den = 30;
210 if (!st->codecpar->extradata)
211 exit(1);
212 memcpy(st->codecpar->extradata, h264_extradata, sizeof(h264_extradata));
213 video_st = st;
214
216 if (!st)
217 exit(1);
220 st->codecpar->sample_rate = 44100;
221 st->codecpar->frame_size = 1024;
223 st->time_base.num = 1;
224 st->time_base.den = 44100;
227 if (!st->codecpar->extradata)
228 exit(1);
229 memcpy(st->codecpar->extradata, aac_extradata, sizeof(aac_extradata));
230 audio_st = st;
231
232 if (id3) {
234 if (!st)
235 exit(1);
238 st->time_base.num = 1;
239 st->time_base.den = 1000;
240 id3_st = st;
241 }
242
243 if (avformat_write_header(ctx, &opts) < 0)
244 exit(1);
246
247 frames = 0;
248 gop_size = 30;
249 duration = video_st->time_base.den / fps;
250 audio_duration = (long long)audio_st->codecpar->frame_size *
251 audio_st->time_base.den / audio_st->codecpar->sample_rate;
252 if (audio_preroll)
253 audio_preroll = 2 * audio_duration;
254
255 bframes = bf;
256 video_dts = bframes ? -duration : 0;
257 audio_dts = -audio_preroll;
258}
259
260static void init(int bf, int audio_preroll)
261{
262 init_fps(bf, audio_preroll, 30, 0);
263}
264
265static void mux_frames(int n, int c)
266{
267 int end_frames = frames + n;
268 while (1) {
269 uint8_t pktdata[8] = { 0 };
271
272 if (av_compare_ts(audio_dts, audio_st->time_base, video_dts, video_st->time_base) < 0) {
273 pkt->dts = pkt->pts = audio_dts;
274 pkt->stream_index = 1;
275 pkt->duration = audio_duration;
276 pkt->flags |= AV_PKT_FLAG_KEY;
278 } else {
279 if (frames == end_frames)
280 break;
281 pkt->dts = video_dts;
282 pkt->stream_index = 0;
283 pkt->duration = duration;
284 if ((frames % gop_size) == 0) {
285 pkt->flags |= AV_PKT_FLAG_KEY;
287 pkt->pts = pkt->dts + duration;
288 video_dts = pkt->pts;
289 } else {
292 pkt->pts = pkt->dts;
294 } else {
296 if (((frames + 1) % gop_size) == 0) {
297 pkt->pts = pkt->dts + duration;
298 video_dts = pkt->pts;
299 } else {
300 next_p_pts = pkt->pts = pkt->dts + 2 * duration;
302 }
303 }
304 }
305 if (!bframes)
306 pkt->pts = pkt->dts;
308 pkt->duration = fake_pkt_duration;
309 frames++;
310 }
311
312 if (clear_duration)
313 pkt->duration = 0;
314 AV_WB32(pktdata + 4, pkt->pts);
315 pkt->data = pktdata;
316 pkt->size = 8;
317 if (skip_write)
318 continue;
319 if (skip_write_audio && pkt->stream_index == 1)
320 continue;
321
322 if (c) {
323 pkt->pts += (1LL<<32);
324 pkt->dts += (1LL<<32);
325 }
326
327 if (do_interleave)
329 else
331 }
332}
333
334static void mux_id3(void)
335{
336 uint8_t pktdata[8] = { 0 };
338
339 pkt->dts = pkt->pts = av_rescale_q(video_dts + (bframes ? duration : 0),
340 video_st->time_base, id3_st->time_base);
341 pkt->stream_index = id3_st->index;
342 pkt->duration = 0;
343
344 AV_WB32(pktdata + 4, pkt->pts);
345 pkt->data = pktdata;
346 pkt->size = 8;
347
349}
350
351static void mux_gops(int n)
352{
353 mux_frames(gop_size * n, 0);
354}
355
356static void skip_gops(int n)
357{
358 skip_write = 1;
359 mux_gops(n);
360 skip_write = 0;
361}
362
363static void signal_init_ts(void)
364{
366
367 pkt->stream_index = 0;
368 pkt->dts = video_dts;
369 pkt->pts = 0;
371
372 pkt->stream_index = 1;
373 pkt->dts = pkt->pts = audio_dts;
375}
376
377static void finish(void)
378{
379 check(av_write_trailer(ctx) >= 0, "Failed to write trailer");
380 if (out_path)
381 avio_closep(&ctx->pb);
382 else
385 ctx = NULL;
386}
387
388// Inserting a global sidx shifts all fragments; check that every packet
389// still reads back the payload mux_frames() wrote, its pts.
390static void test_global_sidx(const char *movflags)
391{
392 AVFormatContext *ic = NULL;
393 int ret, count = 0, wrong = 0;
394
395 av_dict_set(&opts, "movflags", movflags, 0);
396 av_dict_set(&opts, "use_editlist", "0", 0);
397 init(0, 0);
398 mux_gops(2);
399 finish();
400
402 while (ret >= 0 && (ret = av_read_frame(ic, pkt)) >= 0) {
403 wrong += pkt->size != 8 || AV_RB32(pkt->data + 4) != pkt->pts;
404 count++;
406 }
407 printf("%s: %d packets, %d wrong, eof=%d\n",
408 movflags, count, wrong, ret == AVERROR_EOF);
410}
411
412static void help(void)
413{
414 printf("movenc-test [-w] [-s file]\n"
415 "-w write output into files\n"
416 "-s file check global_sidx output written to file\n");
417}
418
419int main(int argc, char **argv)
420{
421 int c;
422 uint8_t header[HASH_SIZE];
423 uint8_t content[HASH_SIZE];
424 int empty_moov_pos;
425 int prev_pos;
426
427 for (;;) {
428 c = getopt(argc, argv, "ws:h");
429 if (c == -1)
430 break;
431 switch (c) {
432 case 's':
434 break;
435 case 'w':
436 write_file = 1;
437 break;
438 default:
439 case 'h':
440 help();
441 return 0;
442 }
443 }
444
445 md5 = av_md5_alloc();
446 if (!md5)
447 return 1;
449 if (!pkt) {
450 av_free(md5);
451 return 1;
452 }
453
454 if (out_path) {
455 test_global_sidx("frag_keyframe+empty_moov+global_sidx");
456 test_global_sidx("frag_keyframe+delay_moov+global_sidx");
457 goto end;
458 }
459
460 // Write a fragmented file with an initial moov and no samples.
461 init_out("non-empty-moov-no-mdat");
462 av_dict_set(&opts, "movflags", "+frag_keyframe", 0);
463 init(0, 0);
464 finish();
465 close_out();
466
467 // Write a fragmented file with an initial moov that actually contains some
468 // samples. One moov+mdat with 1 second of data and one moof+mdat with 1
469 // second of data.
470 init_out("non-empty-moov");
471 av_dict_set(&opts, "movflags", "+frag_keyframe", 0);
472 init(0, 0);
473 mux_gops(2);
474 finish();
475 close_out();
476
477 // Write a similar file, but with B-frames and audio preroll, handled
478 // via an edit list.
479 init_out("non-empty-moov-elst");
480 av_dict_set(&opts, "movflags", "+frag_keyframe", 0);
481 av_dict_set(&opts, "use_editlist", "1", 0);
482 init(1, 1);
483 mux_gops(2);
484 finish();
485 close_out();
486
487 // Use B-frames but no audio-preroll, but without an edit list.
488 // Due to avoid_negative_ts == AVFMT_AVOID_NEG_TS_MAKE_ZERO, the dts
489 // of the first audio packet is > 0, but it is set to zero since edit
490 // lists aren't used, increasing the duration of the first packet instead.
491 init_out("non-empty-moov-no-elst");
492 av_dict_set(&opts, "movflags", "+frag_keyframe", 0);
493 av_dict_set(&opts, "use_editlist", "0", 0);
494 init(1, 0);
495 mux_gops(2);
496 finish();
497 close_out();
498
499 format = "ismv";
500 // Write an ISMV, with B-frames and audio preroll.
501 init_out("ismv");
502 av_dict_set(&opts, "movflags", "+frag_keyframe", 0);
503 init(1, 1);
504 mux_gops(2);
505 finish();
506 close_out();
507 format = "mp4";
508
509 // An initial moov that doesn't contain any samples, followed by two
510 // moof+mdat pairs.
511 init_out("empty-moov");
512 av_dict_set(&opts, "movflags", "+frag_keyframe+empty_moov", 0);
513 av_dict_set(&opts, "use_editlist", "0", 0);
514 init(0, 0);
515 mux_gops(2);
516 finish();
517 close_out();
518 memcpy(content, hash, HASH_SIZE);
519
520 // Similar to the previous one, but with input that doesn't start at
521 // pts/dts 0. avoid_negative_ts behaves in the same way as
522 // in non-empty-moov-no-elst above.
524 init_out("empty-moov-no-elst");
525 av_dict_set(&opts, "movflags", "+frag_keyframe+empty_moov", 0);
526 init(1, 0);
527 mux_gops(2);
528 finish();
529 close_out();
530
532 check(num_warnings == 0, "Unexpected warnings printed");
533
534 // Same as the previous one, but disable avoid_negative_ts (which
535 // would require using an edit list, but with empty_moov, one can't
536 // write a sensible edit list, when the start timestamps aren't known).
537 // This should trigger a warning - we check that the warning is produced.
539 init_out("empty-moov-no-elst-no-adjust");
540 av_dict_set(&opts, "movflags", "+frag_keyframe+empty_moov", 0);
541 av_dict_set(&opts, "avoid_negative_ts", "disabled", 0);
542 init(1, 0);
543 mux_gops(2);
544 finish();
545 close_out();
546
548 check(num_warnings > 0, "No warnings printed for unhandled start offset");
549
550 // Verify that delay_moov produces the same as empty_moov for
551 // simple input
552 init_out("delay-moov");
553 av_dict_set(&opts, "movflags", "+frag_keyframe+delay_moov", 0);
554 av_dict_set(&opts, "use_editlist", "0", 0);
555 init(0, 0);
556 mux_gops(2);
557 finish();
558 close_out();
559 check(!memcmp(hash, content, HASH_SIZE), "delay_moov differs from empty_moov");
560
561 // Test writing content that requires an edit list using delay_moov
562 init_out("delay-moov-elst");
563 av_dict_set(&opts, "movflags", "+frag_keyframe+delay_moov", 0);
564 init(1, 1);
565 mux_gops(2);
566 finish();
567 close_out();
568
569 // Test writing a file with one track lacking packets, with delay_moov.
571 init_out("delay-moov-empty-track");
572 av_dict_set(&opts, "movflags", "+frag_keyframe+delay_moov", 0);
573 init(0, 0);
574 mux_gops(2);
575 // The automatic flushing shouldn't output anything, since we're still
576 // waiting for data for some tracks
577 check(out_size == 0, "delay_moov flushed prematurely");
578 // When closed (or manually flushed), all the written data should still
579 // be output.
580 finish();
581 close_out();
582 check(out_size > 0, "delay_moov didn't output anything");
583
584 // Check that manually flushing still outputs things as expected. This
585 // produces two fragments, while the one above produces only one.
586 init_out("delay-moov-empty-track-flush");
587 av_dict_set(&opts, "movflags", "+frag_custom+delay_moov", 0);
588 init(0, 0);
589 mux_gops(1);
590 av_write_frame(ctx, NULL); // Force writing the moov
591 check(out_size > 0, "No moov written");
593 mux_gops(1);
595 finish();
596 close_out();
597
599
600
601
602 // Verify that the header written by delay_moov when manually flushed
603 // is identical to the one by empty_moov.
604 init_out("empty-moov-header");
605 av_dict_set(&opts, "movflags", "+frag_keyframe+empty_moov", 0);
606 av_dict_set(&opts, "use_editlist", "0", 0);
607 init(0, 0);
608 close_out();
609 memcpy(header, hash, HASH_SIZE);
610 init_out("empty-moov-content");
611 mux_gops(2);
612 // Written 2 seconds of content, with an automatic flush after 1 second.
613 check(out_size > 0, "No automatic flush?");
614 empty_moov_pos = prev_pos = out_size;
615 // Manually flush the second fragment
617 check(out_size > prev_pos, "No second fragment flushed?");
618 prev_pos = out_size;
619 // Check that an extra flush doesn't output any more data
621 check(out_size == prev_pos, "More data written?");
622 close_out();
623 memcpy(content, hash, HASH_SIZE);
624 // Ignore the trailer written here
625 finish();
626
627 init_out("delay-moov-header");
628 av_dict_set(&opts, "movflags", "+frag_custom+delay_moov", 0);
629 av_dict_set(&opts, "use_editlist", "0", 0);
630 init(0, 0);
631 check(out_size == 0, "Output written during init with delay_moov");
632 mux_gops(1); // Write 1 second of content
633 av_write_frame(ctx, NULL); // Force writing the moov
634 close_out();
635 check(!memcmp(hash, header, HASH_SIZE), "delay_moov header differs from empty_moov");
636 init_out("delay-moov-content");
637 av_write_frame(ctx, NULL); // Flush the first fragment
638 check(out_size == empty_moov_pos, "Manually flushed content differs from automatically flushed, %d vs %d", out_size, empty_moov_pos);
639 mux_gops(1); // Write the rest of the content
640 av_write_frame(ctx, NULL); // Flush the second fragment
641 close_out();
642 check(!memcmp(hash, content, HASH_SIZE), "delay_moov content differs from empty_moov");
643 finish();
644
645
646 // Verify that we can produce an identical second fragment without
647 // writing the first one. First write the reference fragments that
648 // we want to reproduce.
649 av_dict_set(&opts, "movflags", "+frag_custom+empty_moov+dash", 0);
650 init(0, 0);
651 mux_gops(1);
652 av_write_frame(ctx, NULL); // Output the first fragment
653 init_out("empty-moov-second-frag");
654 mux_gops(1);
655 av_write_frame(ctx, NULL); // Output the second fragment
656 close_out();
657 memcpy(content, hash, HASH_SIZE);
658 finish();
659
660 // Produce the same second fragment without actually writing the first
661 // one before.
662 av_dict_set(&opts, "movflags", "+frag_custom+empty_moov+dash+frag_discont", 0);
663 av_dict_set(&opts, "fragment_index", "2", 0);
664 av_dict_set(&opts, "avoid_negative_ts", "disabled", 0);
665 av_dict_set(&opts, "use_editlist", "0", 0);
666 init(0, 0);
667 skip_gops(1);
668 init_out("empty-moov-second-frag-discont");
669 mux_gops(1);
670 av_write_frame(ctx, NULL); // Output the second fragment
671 close_out();
672 check(!memcmp(hash, content, HASH_SIZE), "discontinuously written fragment differs");
673 finish();
674
675 // Produce the same thing by using delay_moov, which requires a slightly
676 // different call sequence.
677 av_dict_set(&opts, "movflags", "+frag_custom+delay_moov+dash+frag_discont", 0);
678 av_dict_set(&opts, "fragment_index", "2", 0);
679 init(0, 0);
680 skip_gops(1);
681 mux_gops(1);
682 av_write_frame(ctx, NULL); // Output the moov
683 init_out("delay-moov-second-frag-discont");
684 av_write_frame(ctx, NULL); // Output the second fragment
685 close_out();
686 check(!memcmp(hash, content, HASH_SIZE), "discontinuously written fragment differs");
687 finish();
688
689
690 // Test discontinuously written fragments with B-frames (where the
691 // assumption of starting at pts=0 works) but not with audio preroll
692 // (which can't be guessed).
693 av_dict_set(&opts, "movflags", "+frag_custom+delay_moov+dash", 0);
694 init(1, 0);
695 mux_gops(1);
696 init_out("delay-moov-elst-init");
697 av_write_frame(ctx, NULL); // Output the moov
698 close_out();
699 memcpy(header, hash, HASH_SIZE);
700 av_write_frame(ctx, NULL); // Output the first fragment
701 init_out("delay-moov-elst-second-frag");
702 mux_gops(1);
703 av_write_frame(ctx, NULL); // Output the second fragment
704 close_out();
705 memcpy(content, hash, HASH_SIZE);
706 finish();
707
708 av_dict_set(&opts, "movflags", "+frag_custom+delay_moov+dash+frag_discont", 0);
709 av_dict_set(&opts, "fragment_index", "2", 0);
710 init(1, 0);
711 skip_gops(1);
712 mux_gops(1); // Write the second fragment
713 init_out("delay-moov-elst-init-discont");
714 av_write_frame(ctx, NULL); // Output the moov
715 close_out();
716 check(!memcmp(hash, header, HASH_SIZE), "discontinuously written header differs");
717 init_out("delay-moov-elst-second-frag-discont");
718 av_write_frame(ctx, NULL); // Output the second fragment
719 close_out();
720 check(!memcmp(hash, content, HASH_SIZE), "discontinuously written fragment differs");
721 finish();
722
723
724 // Test discontinuously written fragments with B-frames and audio preroll,
725 // properly signaled.
726 av_dict_set(&opts, "movflags", "+frag_custom+delay_moov+dash", 0);
727 init(1, 1);
728 mux_gops(1);
729 init_out("delay-moov-elst-signal-init");
730 av_write_frame(ctx, NULL); // Output the moov
731 close_out();
732 memcpy(header, hash, HASH_SIZE);
733 av_write_frame(ctx, NULL); // Output the first fragment
734 init_out("delay-moov-elst-signal-second-frag");
735 mux_gops(1);
736 av_write_frame(ctx, NULL); // Output the second fragment
737 close_out();
738 memcpy(content, hash, HASH_SIZE);
739 finish();
740
741 av_dict_set(&opts, "movflags", "+frag_custom+delay_moov+dash+frag_discont", 0);
742 av_dict_set(&opts, "fragment_index", "2", 0);
743 init(1, 1);
745 skip_gops(1);
746 mux_gops(1); // Write the second fragment
747 init_out("delay-moov-elst-signal-init-discont");
748 av_write_frame(ctx, NULL); // Output the moov
749 close_out();
750 check(!memcmp(hash, header, HASH_SIZE), "discontinuously written header differs");
751 init_out("delay-moov-elst-signal-second-frag-discont");
752 av_write_frame(ctx, NULL); // Output the second fragment
753 close_out();
754 check(!memcmp(hash, content, HASH_SIZE), "discontinuously written fragment differs");
755 finish();
756
757
758 // Test muxing discontinuous fragments with very large (> (1<<31)) timestamps.
759 av_dict_set(&opts, "movflags", "+frag_custom+delay_moov+dash+frag_discont", 0);
760 av_dict_set(&opts, "fragment_index", "2", 0);
761 init(1, 1);
763 skip_gops(1);
764 mux_frames(gop_size, 1); // Write the second fragment
765 init_out("delay-moov-elst-signal-init-discont-largets");
766 av_write_frame(ctx, NULL); // Output the moov
767 close_out();
768 init_out("delay-moov-elst-signal-second-frag-discont-largets");
769 av_write_frame(ctx, NULL); // Output the second fragment
770 close_out();
771 finish();
772
773 // Test VFR content, with sidx atoms (which declare the pts duration
774 // of a fragment, forcing overriding the start pts of the next one).
775 // Here, the fragment duration in pts is significantly different from
776 // the duration in dts. The video stream starts at dts=-10,pts=0, and
777 // the second fragment starts at dts=155,pts=156. The trun duration sum
778 // of the first fragment is 165, which also is written as
779 // baseMediaDecodeTime in the tfdt in the second fragment. The sidx for
780 // the first fragment says earliest_presentation_time = 0 and
781 // subsegment_duration = 156, which also matches the sidx in the second
782 // fragment. For the audio stream, the pts and dts durations also don't
783 // match - the input stream starts at pts=-2048, but that part is excluded
784 // by the edit list.
785 init_out("vfr");
786 av_dict_set(&opts, "movflags", "+frag_keyframe+delay_moov+dash", 0);
787 init_fps(1, 1, 3, 0);
788 mux_frames(gop_size/2, 0);
789 duration /= 10;
790 mux_frames(gop_size/2, 0);
791 mux_gops(1);
792 finish();
793 close_out();
794
795 // Test VFR content, with cleared duration fields. In these cases,
796 // the muxer must guess the duration of the last packet of each
797 // fragment. As long as the framerate doesn't vary (too much) at the
798 // fragment edge, it works just fine. Additionally, when automatically
799 // cutting fragments, the muxer already know the timestamps of the next
800 // packet for one stream (in most cases the video stream), avoiding
801 // having to use guesses for that one.
803 clear_duration = 1;
804 init_out("vfr-noduration");
805 av_dict_set(&opts, "movflags", "+frag_keyframe+delay_moov+dash", 0);
806 init_fps(1, 1, 3, 0);
807 mux_frames(gop_size/2, 0);
808 duration /= 10;
809 mux_frames(gop_size/2, 0);
810 mux_gops(1);
811 finish();
812 close_out();
813 clear_duration = 0;
815 check(num_warnings > 0, "No warnings printed for filled in durations");
816
817 // Test with an IO buffer size that is too small to hold a full fragment;
818 // this will cause write_data_type to be called with the type unknown.
819 force_iobuf_size = 1500;
820 init_out("large_frag");
821 av_dict_set(&opts, "movflags", "+frag_keyframe+delay_moov", 0);
822 init_fps(1, 1, 3, 0);
823 mux_gops(2);
824 finish();
825 close_out();
827
828 // Test VFR content with bframes with interleaving.
829 // Here, using av_interleaved_write_frame allows the muxer to get the
830 // fragment end durations right. We always set the packet duration to
831 // the expected, but we simulate dropped frames at one point.
832 do_interleave = 1;
833 init_out("vfr-noduration-interleave");
834 av_dict_set(&opts, "movflags", "+frag_keyframe+delay_moov", 0);
835 av_dict_set(&opts, "frag_duration", "650000", 0);
836 init_fps(1, 1, 30, 0);
837 mux_frames(gop_size/2, 0);
838 // Pretend that the packet duration is the normal, even if
839 // we actually skip a bunch of frames. (I.e., simulate that
840 // we don't know of the framedrop in advance.)
842 duration *= 10;
843 mux_frames(1, 0);
845 duration /= 10;
846 mux_frames(gop_size/2 - 1, 0);
847 mux_gops(1);
848 finish();
849 close_out();
850 clear_duration = 0;
851 do_interleave = 0;
852
853 // Write a fragmented file with b-frames and audio preroll,
854 // with negative cts values, removing the edit list for the
855 // video track.
856 init_out("delay-moov-elst-neg-cts");
857 av_dict_set(&opts, "movflags", "+frag_keyframe+delay_moov+negative_cts_offsets", 0);
858 init(1, 1);
859 mux_gops(2);
860 finish();
861 close_out();
862
863 // Write a fragmented file with b-frames without audio preroll,
864 // with negative cts values, avoiding any edit lists, allowing
865 // to use empty_moov instead of delay_moov.
866 init_out("empty-moov-neg-cts");
867 av_dict_set(&opts, "movflags", "+frag_keyframe+empty_moov+negative_cts_offsets", 0);
868 init(1, 0);
869 mux_gops(2);
870 finish();
871 close_out();
872
873 // Write a manually fragmented file, with timed ID3 packets at the head
874 // of each fragment.
875 init_out("emsg");
876 av_dict_set(&opts, "movflags", "+frag_custom+cmaf", 0);
877 init_fps(1, 0, 30, 1);
878 mux_id3();
879 mux_gops(2);
880 av_write_frame(ctx, NULL); // Flush fragment.
881 mux_id3();
882 mux_gops(2);
883 finish();
884 close_out();
885
886end:
887 av_free(md5);
889
890 return check_faults > 0 ? 1 : 0;
891}
static const char *const format[]
Definition af_aiir.c:445
static int io_write(void *opaque, const uint8_t *buf, int size)
static int64_t audio_duration
static FILE * out
static void mux_gops(int n)
static int do_interleave
static int io_write_data_type(void *opaque, const uint8_t *buf, int size, enum AVIODataMarkerType type, int64_t time)
static int64_t audio_dts
static void skip_gops(int n)
static int gop_size
static int skip_write
static void count_warnings(void *avcl, int level, const char *fmt, va_list vl)
static int write_file
static struct AVMD5 * md5
static void reset_count_warnings(void)
static int fake_pkt_duration
static int skip_write_audio
static const uint8_t h264_extradata[]
static AVStream * audio_st
static int frames
static void mux_frames(int n, int c)
static void mux_id3(void)
static AVStream * video_st
static int out_size
static uint8_t iobuf[32768]
static uint8_t hash[HASH_SIZE]
static int64_t video_dts
static void init_out(const char *name)
static AVStream * id3_st
static AVFormatContext * ctx
static int force_iobuf_size
static void test_global_sidx(const char *movflags)
static int bframes
static const char * cur_name
static enum AVPictureType last_picture
#define check(value,...)
static void help(void)
static void finish(void)
static void close_out(void)
static int check_faults
static const uint8_t aac_extradata[]
static int num_warnings
static int clear_duration
static const char * out_path
static int64_t next_p_pts
static void init_count_warnings(void)
static void signal_init_ts(void)
static AVDictionary * opts
static void init_fps(int bf, int audio_preroll, int fps, int id3)
Main libavformat public API header.
#define AVFMT_FLAG_BITEXACT
When muxing, try to avoid writing any random/volatile data to the output.
Definition avformat.h:1570
int avformat_alloc_output_context2(AVFormatContext **ctx, const AVOutputFormat *oformat, const char *format_name, const char *filename)
Allocate an AVFormatContext for an output format.
Definition mux.c:95
int avio_open(AVIOContext **s, const char *filename, int flags)
Create and initialize a AVIOContext for accessing the resource indicated by url.
Definition avio.c:573
int avio_closep(AVIOContext **s)
Close the resource accessed by the AVIOContext *s, free it and set the pointer pointing to it to NULL...
Definition avio.c:725
AVIODataMarkerType
Different data types that can be returned via the AVIO write_data_type callback.
Definition avio.h:110
@ AVIO_DATA_MARKER_BOUNDARY_POINT
A point in the output bytestream where a demuxer can start parsing (for non self synchronizing bytest...
Definition avio.h:127
@ AVIO_DATA_MARKER_TRAILER
Trailer data, which doesn't contain actual content, but only for finalizing the output file.
Definition avio.h:139
@ AVIO_DATA_MARKER_HEADER
Header data; this needs to be present for the stream to be decodeable.
Definition avio.h:114
@ AVIO_DATA_MARKER_UNKNOWN
This is any, unlabelled data.
Definition avio.h:134
@ AVIO_DATA_MARKER_SYNC_POINT
A point in the output bytestream where a decoder can start decoding (i.e.
Definition avio.h:121
#define AVIO_FLAG_WRITE
write-only
Definition avio.h:631
AVIOContext * avio_alloc_context(unsigned char *buffer, int buffer_size, int write_flag, void *opaque, int(*read_packet)(void *opaque, uint8_t *buf, int buf_size), int(*write_packet)(void *opaque, const uint8_t *buf, int buf_size), int64_t(*seek)(void *opaque, int64_t offset, int whence))
Allocate and initialize an AVIOContext for buffered I/O.
Definition aviobuf.c:109
void avio_context_free(AVIOContext **s)
Free the supplied IO context and everything associated with it.
Definition aviobuf.c:126
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
__device__ int printf(const char *,...)
static AVPacket * pkt
int main
Definition dovi_rpuenc.c:38
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:613
double value
Definition eval.c:102
static int64_t duration
Definition ffplay.c:359
int ret
Definition ffplay.c:391
static int getopt(int argc, char *argv[], const char *opts)
Definition getopt.c:41
static char * optarg
Definition getopt.c:39
#define check_func
Definition test.h:481
@ AV_CODEC_ID_TIMED_ID3
Definition codec_id.h:612
@ AV_CODEC_ID_H264
Definition codec_id.h:77
@ AV_CODEC_ID_AAC
Definition codec_id.h:461
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding.
Definition defs.h:40
void av_packet_free(AVPacket **pkt)
Free the packet, if the packet is reference counted, it will be unreferenced first.
Definition packet.c:74
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition packet.c:435
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition packet.h:657
AVPacket * av_packet_alloc(void)
Allocate an AVPacket and set its fields to default values.
Definition packet.c:63
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
void avformat_free_context(AVFormatContext *s)
Free an AVFormatContext and all its streams.
Definition avformat.c:165
int av_read_frame(AVFormatContext *s, AVPacket *pkt)
Return the next frame of a stream.
Definition demux.c:1662
int avformat_open_input(AVFormatContext **ps, const char *url, const AVInputFormat *fmt, AVDictionary **options)
Open an input stream and read the header.
Definition demux.c:231
void avformat_close_input(AVFormatContext **s)
Close an opened input AVFormatContext.
Definition demux.c:400
av_warn_unused_result int avformat_write_header(AVFormatContext *s, AVDictionary **options)
Allocate the stream private data and write the stream header to an output media file.
Definition mux.c:512
int av_interleaved_write_frame(AVFormatContext *s, AVPacket *pkt)
Write a packet to an output media file ensuring correct interleaving.
Definition mux.c:1268
int av_write_trailer(AVFormatContext *s)
Write the stream trailer to an output media file and free the file private data.
Definition mux.c:1283
int av_write_frame(AVFormatContext *s, AVPacket *pkt)
Write a packet to an output media file.
Definition mux.c:1221
#define AV_CHANNEL_LAYOUT_STEREO
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values.
Definition dict.c:236
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition dict.c:87
#define AVERROR_EOF
End of file.
Definition error.h:57
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
void av_log_set_callback(void(*callback)(void *, int, const char *, va_list))
Set the logging callback.
Definition log.c:497
void av_log_default_callback(void *ptr, int level, const char *fmt, va_list vl)
Default logging callback.
Definition log.c:385
int av_compare_ts(int64_t ts_a, AVRational tb_a, int64_t ts_b, AVRational tb_b)
Compare two timestamps each in its own time base.
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
void av_md5_init(AVMD5 *ctx)
Initialize MD5 hashing.
Definition md5.c:143
void av_md5_final(AVMD5 *ctx, uint8_t *dst)
Finish hashing and output digest value.
Definition md5.c:188
struct AVMD5 * av_md5_alloc(void)
Allocate an AVMD5 context.
Definition md5.c:50
void av_md5_update(AVMD5 *ctx, const uint8_t *src, size_t len)
Update hash value.
Definition md5.c:153
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
@ AVMEDIA_TYPE_DATA
Opaque data information usually continuous.
Definition avutil.h:202
AVPictureType
Definition avutil.h:276
@ AV_PICTURE_TYPE_I
Intra.
Definition avutil.h:278
@ AV_PICTURE_TYPE_P
Predicted.
Definition avutil.h:279
@ AV_PICTURE_TYPE_B
Bi-dir predicted.
Definition avutil.h:280
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition avutil.h:247
#define AV_WB32(p, v)
#define AV_RB32(p)
enum MPFType type
Definition jpegmpfenc.c:55
Public header for MD5 hash function implementation.
Memory handling functions.
const char * name
Definition qsvenc.c:142
#define bf(fn, bd, opt)
Definition dsp_init.c:30
static const uint8_t header[24]
Definition sdr2.c:68
#define HASH_SIZE
Definition shared.c:68
An AVChannelLayout holds information about the channel layout of audio data.
int extradata_size
Size of the extradata content in bytes.
Definition codec_par.h:75
int frame_size
Audio frame size, if known.
Definition codec_par.h:227
int height
The height of the video frame in pixels.
Definition codec_par.h:150
AVChannelLayout ch_layout
The channel layout and number of channels.
Definition codec_par.h:207
int width
The width of the video frame in pixels.
Definition codec_par.h:143
enum AVMediaType codec_type
General type of the encoded data.
Definition codec_par.h:53
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition codec_par.h:71
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition codec_par.h:57
int sample_rate
The number of audio samples per second.
Definition codec_par.h:213
Format I/O context.
Definition avformat.h:1401
Definition md5.c:42
This structure stores compressed data.
Definition packet.h:587
int num
Numerator.
Definition rational.h:59
int den
Denominator.
Definition rational.h:60
Stream structure.
Definition avformat.h:768
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:791
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
Definition avformat.h:807
uint8_t level
Definition svq3.c:208
#define av_free(p)
#define av_mallocz(s)
int size
static double c[64]