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 *cur_name;
55static FILE* out;
56static int out_size;
57static struct AVMD5* md5;
58static uint8_t hash[HASH_SIZE];
59
60static AVPacket *pkt;
63
64static int bframes;
67static int frames;
68static int gop_size;
71static int skip_write;
73static int clear_duration;
75static int do_interleave;
77
78static int num_warnings;
79
80static int check_faults;
81
82
83static void count_warnings(void *avcl, int level, const char *fmt, va_list vl)
84{
85 if (level == AV_LOG_WARNING)
87}
88
94
99
100static int io_write(void *opaque, const uint8_t *buf, int size)
101{
102 out_size += size;
103 av_md5_update(md5, buf, size);
104 if (out)
105 fwrite(buf, 1, size, out);
106 return size;
107}
108
109static int io_write_data_type(void *opaque, const uint8_t *buf, int size,
111{
112 char timebuf[30], content[5] = { 0 };
113 const char *str;
114 switch (type) {
115 case AVIO_DATA_MARKER_HEADER: str = "header"; break;
116 case AVIO_DATA_MARKER_SYNC_POINT: str = "sync"; break;
117 case AVIO_DATA_MARKER_BOUNDARY_POINT: str = "boundary"; break;
118 case AVIO_DATA_MARKER_UNKNOWN: str = "unknown"; break;
119 case AVIO_DATA_MARKER_TRAILER: str = "trailer"; break;
120 default: str = "unknown"; break;
121 }
122 if (time == AV_NOPTS_VALUE)
123 snprintf(timebuf, sizeof(timebuf), "nopts");
124 else
125 snprintf(timebuf, sizeof(timebuf), "%"PRId64, time);
126 // There can be multiple header/trailer callbacks, only log the box type
127 // for header at out_size == 0
131 size >= 8)
132 memcpy(content, &buf[4], 4);
133 else
134 snprintf(content, sizeof(content), "-");
135 printf("write_data len %d, time %s, type %s atom %s\n", size, timebuf, str, content);
136 return io_write(opaque, buf, size);
137}
138
139static void init_out(const char *name)
140{
141 char buf[100];
142 cur_name = name;
143 snprintf(buf, sizeof(buf), "%s.%s", cur_name, format);
144
146 if (write_file) {
147 out = fopen(buf, "wb");
148 if (!out)
149 perror(buf);
150 }
151 out_size = 0;
152}
153
154static void close_out(void)
155{
156 int i;
158 for (i = 0; i < HASH_SIZE; i++)
159 printf("%02x", hash[i]);
160 printf(" %d %s\n", out_size, cur_name);
161 if (out)
162 fclose(out);
163 out = NULL;
164}
165
166static void check_func(int value, int line, const char *msg, ...)
167{
168 if (!value) {
169 va_list ap;
170 va_start(ap, msg);
171 printf("%d: ", line);
172 vprintf(msg, ap);
173 printf("\n");
174 check_faults++;
175 va_end(ap);
176 }
177}
178#define check(value, ...) check_func(value, __LINE__, __VA_ARGS__)
179
180static void init_fps(int bf, int audio_preroll, int fps, int id3)
181{
182 AVStream *st;
183 int iobuf_size = force_iobuf_size ? force_iobuf_size : sizeof(iobuf);
185 if (!ctx)
186 exit(1);
187 ctx->oformat = av_guess_format(format, NULL, NULL);
188 if (!ctx->oformat)
189 exit(1);
190 ctx->pb = avio_alloc_context(iobuf, iobuf_size, 1, NULL, NULL, io_write, NULL);
191 if (!ctx->pb)
192 exit(1);
193 ctx->pb->write_data_type = io_write_data_type;
194 ctx->flags |= AVFMT_FLAG_BITEXACT;
195
197 if (!st)
198 exit(1);
201 st->codecpar->width = 640;
202 st->codecpar->height = 480;
203 st->time_base.num = 1;
204 st->time_base.den = 30;
207 if (!st->codecpar->extradata)
208 exit(1);
209 memcpy(st->codecpar->extradata, h264_extradata, sizeof(h264_extradata));
210 video_st = st;
211
213 if (!st)
214 exit(1);
217 st->codecpar->sample_rate = 44100;
218 st->codecpar->frame_size = 1024;
220 st->time_base.num = 1;
221 st->time_base.den = 44100;
224 if (!st->codecpar->extradata)
225 exit(1);
226 memcpy(st->codecpar->extradata, aac_extradata, sizeof(aac_extradata));
227 audio_st = st;
228
229 if (id3) {
231 if (!st)
232 exit(1);
235 st->time_base.num = 1;
236 st->time_base.den = 1000;
237 id3_st = st;
238 }
239
240 if (avformat_write_header(ctx, &opts) < 0)
241 exit(1);
243
244 frames = 0;
245 gop_size = 30;
246 duration = video_st->time_base.den / fps;
247 audio_duration = (long long)audio_st->codecpar->frame_size *
248 audio_st->time_base.den / audio_st->codecpar->sample_rate;
249 if (audio_preroll)
250 audio_preroll = 2 * audio_duration;
251
252 bframes = bf;
253 video_dts = bframes ? -duration : 0;
254 audio_dts = -audio_preroll;
255}
256
257static void init(int bf, int audio_preroll)
258{
259 init_fps(bf, audio_preroll, 30, 0);
260}
261
262static void mux_frames(int n, int c)
263{
264 int end_frames = frames + n;
265 while (1) {
266 uint8_t pktdata[8] = { 0 };
268
269 if (av_compare_ts(audio_dts, audio_st->time_base, video_dts, video_st->time_base) < 0) {
270 pkt->dts = pkt->pts = audio_dts;
271 pkt->stream_index = 1;
272 pkt->duration = audio_duration;
273 pkt->flags |= AV_PKT_FLAG_KEY;
275 } else {
276 if (frames == end_frames)
277 break;
278 pkt->dts = video_dts;
279 pkt->stream_index = 0;
280 pkt->duration = duration;
281 if ((frames % gop_size) == 0) {
282 pkt->flags |= AV_PKT_FLAG_KEY;
284 pkt->pts = pkt->dts + duration;
285 video_dts = pkt->pts;
286 } else {
289 pkt->pts = pkt->dts;
291 } else {
293 if (((frames + 1) % gop_size) == 0) {
294 pkt->pts = pkt->dts + duration;
295 video_dts = pkt->pts;
296 } else {
297 next_p_pts = pkt->pts = pkt->dts + 2 * duration;
299 }
300 }
301 }
302 if (!bframes)
303 pkt->pts = pkt->dts;
305 pkt->duration = fake_pkt_duration;
306 frames++;
307 }
308
309 if (clear_duration)
310 pkt->duration = 0;
311 AV_WB32(pktdata + 4, pkt->pts);
312 pkt->data = pktdata;
313 pkt->size = 8;
314 if (skip_write)
315 continue;
316 if (skip_write_audio && pkt->stream_index == 1)
317 continue;
318
319 if (c) {
320 pkt->pts += (1LL<<32);
321 pkt->dts += (1LL<<32);
322 }
323
324 if (do_interleave)
326 else
328 }
329}
330
331static void mux_id3(void)
332{
333 uint8_t pktdata[8] = { 0 };
335
336 pkt->dts = pkt->pts = av_rescale_q(video_dts + (bframes ? duration : 0),
337 video_st->time_base, id3_st->time_base);
338 pkt->stream_index = id3_st->index;
339 pkt->duration = 0;
340
341 AV_WB32(pktdata + 4, pkt->pts);
342 pkt->data = pktdata;
343 pkt->size = 8;
344
346}
347
348static void mux_gops(int n)
349{
350 mux_frames(gop_size * n, 0);
351}
352
353static void skip_gops(int n)
354{
355 skip_write = 1;
356 mux_gops(n);
357 skip_write = 0;
358}
359
360static void signal_init_ts(void)
361{
363
364 pkt->stream_index = 0;
365 pkt->dts = video_dts;
366 pkt->pts = 0;
368
369 pkt->stream_index = 1;
370 pkt->dts = pkt->pts = audio_dts;
372}
373
374static void finish(void)
375{
379 ctx = NULL;
380}
381
382static void help(void)
383{
384 printf("movenc-test [-w]\n"
385 "-w write output into files\n");
386}
387
388int main(int argc, char **argv)
389{
390 int c;
391 uint8_t header[HASH_SIZE];
392 uint8_t content[HASH_SIZE];
393 int empty_moov_pos;
394 int prev_pos;
395
396 for (;;) {
397 c = getopt(argc, argv, "wh");
398 if (c == -1)
399 break;
400 switch (c) {
401 case 'w':
402 write_file = 1;
403 break;
404 default:
405 case 'h':
406 help();
407 return 0;
408 }
409 }
410
411 md5 = av_md5_alloc();
412 if (!md5)
413 return 1;
415 if (!pkt) {
416 av_free(md5);
417 return 1;
418 }
419
420 // Write a fragmented file with an initial moov and no samples.
421 init_out("non-empty-moov-no-mdat");
422 av_dict_set(&opts, "movflags", "+frag_keyframe", 0);
423 init(0, 0);
424 finish();
425 close_out();
426
427 // Write a fragmented file with an initial moov that actually contains some
428 // samples. One moov+mdat with 1 second of data and one moof+mdat with 1
429 // second of data.
430 init_out("non-empty-moov");
431 av_dict_set(&opts, "movflags", "+frag_keyframe", 0);
432 init(0, 0);
433 mux_gops(2);
434 finish();
435 close_out();
436
437 // Write a similar file, but with B-frames and audio preroll, handled
438 // via an edit list.
439 init_out("non-empty-moov-elst");
440 av_dict_set(&opts, "movflags", "+frag_keyframe", 0);
441 av_dict_set(&opts, "use_editlist", "1", 0);
442 init(1, 1);
443 mux_gops(2);
444 finish();
445 close_out();
446
447 // Use B-frames but no audio-preroll, but without an edit list.
448 // Due to avoid_negative_ts == AVFMT_AVOID_NEG_TS_MAKE_ZERO, the dts
449 // of the first audio packet is > 0, but it is set to zero since edit
450 // lists aren't used, increasing the duration of the first packet instead.
451 init_out("non-empty-moov-no-elst");
452 av_dict_set(&opts, "movflags", "+frag_keyframe", 0);
453 av_dict_set(&opts, "use_editlist", "0", 0);
454 init(1, 0);
455 mux_gops(2);
456 finish();
457 close_out();
458
459 format = "ismv";
460 // Write an ISMV, with B-frames and audio preroll.
461 init_out("ismv");
462 av_dict_set(&opts, "movflags", "+frag_keyframe", 0);
463 init(1, 1);
464 mux_gops(2);
465 finish();
466 close_out();
467 format = "mp4";
468
469 // An initial moov that doesn't contain any samples, followed by two
470 // moof+mdat pairs.
471 init_out("empty-moov");
472 av_dict_set(&opts, "movflags", "+frag_keyframe+empty_moov", 0);
473 av_dict_set(&opts, "use_editlist", "0", 0);
474 init(0, 0);
475 mux_gops(2);
476 finish();
477 close_out();
478 memcpy(content, hash, HASH_SIZE);
479
480 // Similar to the previous one, but with input that doesn't start at
481 // pts/dts 0. avoid_negative_ts behaves in the same way as
482 // in non-empty-moov-no-elst above.
484 init_out("empty-moov-no-elst");
485 av_dict_set(&opts, "movflags", "+frag_keyframe+empty_moov", 0);
486 init(1, 0);
487 mux_gops(2);
488 finish();
489 close_out();
490
492 check(num_warnings == 0, "Unexpected warnings printed");
493
494 // Same as the previous one, but disable avoid_negative_ts (which
495 // would require using an edit list, but with empty_moov, one can't
496 // write a sensible edit list, when the start timestamps aren't known).
497 // This should trigger a warning - we check that the warning is produced.
499 init_out("empty-moov-no-elst-no-adjust");
500 av_dict_set(&opts, "movflags", "+frag_keyframe+empty_moov", 0);
501 av_dict_set(&opts, "avoid_negative_ts", "disabled", 0);
502 init(1, 0);
503 mux_gops(2);
504 finish();
505 close_out();
506
508 check(num_warnings > 0, "No warnings printed for unhandled start offset");
509
510 // Verify that delay_moov produces the same as empty_moov for
511 // simple input
512 init_out("delay-moov");
513 av_dict_set(&opts, "movflags", "+frag_keyframe+delay_moov", 0);
514 av_dict_set(&opts, "use_editlist", "0", 0);
515 init(0, 0);
516 mux_gops(2);
517 finish();
518 close_out();
519 check(!memcmp(hash, content, HASH_SIZE), "delay_moov differs from empty_moov");
520
521 // Test writing content that requires an edit list using delay_moov
522 init_out("delay-moov-elst");
523 av_dict_set(&opts, "movflags", "+frag_keyframe+delay_moov", 0);
524 init(1, 1);
525 mux_gops(2);
526 finish();
527 close_out();
528
529 // Test writing a file with one track lacking packets, with delay_moov.
531 init_out("delay-moov-empty-track");
532 av_dict_set(&opts, "movflags", "+frag_keyframe+delay_moov", 0);
533 init(0, 0);
534 mux_gops(2);
535 // The automatic flushing shouldn't output anything, since we're still
536 // waiting for data for some tracks
537 check(out_size == 0, "delay_moov flushed prematurely");
538 // When closed (or manually flushed), all the written data should still
539 // be output.
540 finish();
541 close_out();
542 check(out_size > 0, "delay_moov didn't output anything");
543
544 // Check that manually flushing still outputs things as expected. This
545 // produces two fragments, while the one above produces only one.
546 init_out("delay-moov-empty-track-flush");
547 av_dict_set(&opts, "movflags", "+frag_custom+delay_moov", 0);
548 init(0, 0);
549 mux_gops(1);
550 av_write_frame(ctx, NULL); // Force writing the moov
551 check(out_size > 0, "No moov written");
553 mux_gops(1);
555 finish();
556 close_out();
557
559
560
561
562 // Verify that the header written by delay_moov when manually flushed
563 // is identical to the one by empty_moov.
564 init_out("empty-moov-header");
565 av_dict_set(&opts, "movflags", "+frag_keyframe+empty_moov", 0);
566 av_dict_set(&opts, "use_editlist", "0", 0);
567 init(0, 0);
568 close_out();
569 memcpy(header, hash, HASH_SIZE);
570 init_out("empty-moov-content");
571 mux_gops(2);
572 // Written 2 seconds of content, with an automatic flush after 1 second.
573 check(out_size > 0, "No automatic flush?");
574 empty_moov_pos = prev_pos = out_size;
575 // Manually flush the second fragment
577 check(out_size > prev_pos, "No second fragment flushed?");
578 prev_pos = out_size;
579 // Check that an extra flush doesn't output any more data
581 check(out_size == prev_pos, "More data written?");
582 close_out();
583 memcpy(content, hash, HASH_SIZE);
584 // Ignore the trailer written here
585 finish();
586
587 init_out("delay-moov-header");
588 av_dict_set(&opts, "movflags", "+frag_custom+delay_moov", 0);
589 av_dict_set(&opts, "use_editlist", "0", 0);
590 init(0, 0);
591 check(out_size == 0, "Output written during init with delay_moov");
592 mux_gops(1); // Write 1 second of content
593 av_write_frame(ctx, NULL); // Force writing the moov
594 close_out();
595 check(!memcmp(hash, header, HASH_SIZE), "delay_moov header differs from empty_moov");
596 init_out("delay-moov-content");
597 av_write_frame(ctx, NULL); // Flush the first fragment
598 check(out_size == empty_moov_pos, "Manually flushed content differs from automatically flushed, %d vs %d", out_size, empty_moov_pos);
599 mux_gops(1); // Write the rest of the content
600 av_write_frame(ctx, NULL); // Flush the second fragment
601 close_out();
602 check(!memcmp(hash, content, HASH_SIZE), "delay_moov content differs from empty_moov");
603 finish();
604
605
606 // Verify that we can produce an identical second fragment without
607 // writing the first one. First write the reference fragments that
608 // we want to reproduce.
609 av_dict_set(&opts, "movflags", "+frag_custom+empty_moov+dash", 0);
610 init(0, 0);
611 mux_gops(1);
612 av_write_frame(ctx, NULL); // Output the first fragment
613 init_out("empty-moov-second-frag");
614 mux_gops(1);
615 av_write_frame(ctx, NULL); // Output the second fragment
616 close_out();
617 memcpy(content, hash, HASH_SIZE);
618 finish();
619
620 // Produce the same second fragment without actually writing the first
621 // one before.
622 av_dict_set(&opts, "movflags", "+frag_custom+empty_moov+dash+frag_discont", 0);
623 av_dict_set(&opts, "fragment_index", "2", 0);
624 av_dict_set(&opts, "avoid_negative_ts", "disabled", 0);
625 av_dict_set(&opts, "use_editlist", "0", 0);
626 init(0, 0);
627 skip_gops(1);
628 init_out("empty-moov-second-frag-discont");
629 mux_gops(1);
630 av_write_frame(ctx, NULL); // Output the second fragment
631 close_out();
632 check(!memcmp(hash, content, HASH_SIZE), "discontinuously written fragment differs");
633 finish();
634
635 // Produce the same thing by using delay_moov, which requires a slightly
636 // different call sequence.
637 av_dict_set(&opts, "movflags", "+frag_custom+delay_moov+dash+frag_discont", 0);
638 av_dict_set(&opts, "fragment_index", "2", 0);
639 init(0, 0);
640 skip_gops(1);
641 mux_gops(1);
642 av_write_frame(ctx, NULL); // Output the moov
643 init_out("delay-moov-second-frag-discont");
644 av_write_frame(ctx, NULL); // Output the second fragment
645 close_out();
646 check(!memcmp(hash, content, HASH_SIZE), "discontinuously written fragment differs");
647 finish();
648
649
650 // Test discontinuously written fragments with B-frames (where the
651 // assumption of starting at pts=0 works) but not with audio preroll
652 // (which can't be guessed).
653 av_dict_set(&opts, "movflags", "+frag_custom+delay_moov+dash", 0);
654 init(1, 0);
655 mux_gops(1);
656 init_out("delay-moov-elst-init");
657 av_write_frame(ctx, NULL); // Output the moov
658 close_out();
659 memcpy(header, hash, HASH_SIZE);
660 av_write_frame(ctx, NULL); // Output the first fragment
661 init_out("delay-moov-elst-second-frag");
662 mux_gops(1);
663 av_write_frame(ctx, NULL); // Output the second fragment
664 close_out();
665 memcpy(content, hash, HASH_SIZE);
666 finish();
667
668 av_dict_set(&opts, "movflags", "+frag_custom+delay_moov+dash+frag_discont", 0);
669 av_dict_set(&opts, "fragment_index", "2", 0);
670 init(1, 0);
671 skip_gops(1);
672 mux_gops(1); // Write the second fragment
673 init_out("delay-moov-elst-init-discont");
674 av_write_frame(ctx, NULL); // Output the moov
675 close_out();
676 check(!memcmp(hash, header, HASH_SIZE), "discontinuously written header differs");
677 init_out("delay-moov-elst-second-frag-discont");
678 av_write_frame(ctx, NULL); // Output the second fragment
679 close_out();
680 check(!memcmp(hash, content, HASH_SIZE), "discontinuously written fragment differs");
681 finish();
682
683
684 // Test discontinuously written fragments with B-frames and audio preroll,
685 // properly signaled.
686 av_dict_set(&opts, "movflags", "+frag_custom+delay_moov+dash", 0);
687 init(1, 1);
688 mux_gops(1);
689 init_out("delay-moov-elst-signal-init");
690 av_write_frame(ctx, NULL); // Output the moov
691 close_out();
692 memcpy(header, hash, HASH_SIZE);
693 av_write_frame(ctx, NULL); // Output the first fragment
694 init_out("delay-moov-elst-signal-second-frag");
695 mux_gops(1);
696 av_write_frame(ctx, NULL); // Output the second fragment
697 close_out();
698 memcpy(content, hash, HASH_SIZE);
699 finish();
700
701 av_dict_set(&opts, "movflags", "+frag_custom+delay_moov+dash+frag_discont", 0);
702 av_dict_set(&opts, "fragment_index", "2", 0);
703 init(1, 1);
705 skip_gops(1);
706 mux_gops(1); // Write the second fragment
707 init_out("delay-moov-elst-signal-init-discont");
708 av_write_frame(ctx, NULL); // Output the moov
709 close_out();
710 check(!memcmp(hash, header, HASH_SIZE), "discontinuously written header differs");
711 init_out("delay-moov-elst-signal-second-frag-discont");
712 av_write_frame(ctx, NULL); // Output the second fragment
713 close_out();
714 check(!memcmp(hash, content, HASH_SIZE), "discontinuously written fragment differs");
715 finish();
716
717
718 // Test muxing discontinuous fragments with very large (> (1<<31)) timestamps.
719 av_dict_set(&opts, "movflags", "+frag_custom+delay_moov+dash+frag_discont", 0);
720 av_dict_set(&opts, "fragment_index", "2", 0);
721 init(1, 1);
723 skip_gops(1);
724 mux_frames(gop_size, 1); // Write the second fragment
725 init_out("delay-moov-elst-signal-init-discont-largets");
726 av_write_frame(ctx, NULL); // Output the moov
727 close_out();
728 init_out("delay-moov-elst-signal-second-frag-discont-largets");
729 av_write_frame(ctx, NULL); // Output the second fragment
730 close_out();
731 finish();
732
733 // Test VFR content, with sidx atoms (which declare the pts duration
734 // of a fragment, forcing overriding the start pts of the next one).
735 // Here, the fragment duration in pts is significantly different from
736 // the duration in dts. The video stream starts at dts=-10,pts=0, and
737 // the second fragment starts at dts=155,pts=156. The trun duration sum
738 // of the first fragment is 165, which also is written as
739 // baseMediaDecodeTime in the tfdt in the second fragment. The sidx for
740 // the first fragment says earliest_presentation_time = 0 and
741 // subsegment_duration = 156, which also matches the sidx in the second
742 // fragment. For the audio stream, the pts and dts durations also don't
743 // match - the input stream starts at pts=-2048, but that part is excluded
744 // by the edit list.
745 init_out("vfr");
746 av_dict_set(&opts, "movflags", "+frag_keyframe+delay_moov+dash", 0);
747 init_fps(1, 1, 3, 0);
748 mux_frames(gop_size/2, 0);
749 duration /= 10;
750 mux_frames(gop_size/2, 0);
751 mux_gops(1);
752 finish();
753 close_out();
754
755 // Test VFR content, with cleared duration fields. In these cases,
756 // the muxer must guess the duration of the last packet of each
757 // fragment. As long as the framerate doesn't vary (too much) at the
758 // fragment edge, it works just fine. Additionally, when automatically
759 // cutting fragments, the muxer already know the timestamps of the next
760 // packet for one stream (in most cases the video stream), avoiding
761 // having to use guesses for that one.
763 clear_duration = 1;
764 init_out("vfr-noduration");
765 av_dict_set(&opts, "movflags", "+frag_keyframe+delay_moov+dash", 0);
766 init_fps(1, 1, 3, 0);
767 mux_frames(gop_size/2, 0);
768 duration /= 10;
769 mux_frames(gop_size/2, 0);
770 mux_gops(1);
771 finish();
772 close_out();
773 clear_duration = 0;
775 check(num_warnings > 0, "No warnings printed for filled in durations");
776
777 // Test with an IO buffer size that is too small to hold a full fragment;
778 // this will cause write_data_type to be called with the type unknown.
779 force_iobuf_size = 1500;
780 init_out("large_frag");
781 av_dict_set(&opts, "movflags", "+frag_keyframe+delay_moov", 0);
782 init_fps(1, 1, 3, 0);
783 mux_gops(2);
784 finish();
785 close_out();
787
788 // Test VFR content with bframes with interleaving.
789 // Here, using av_interleaved_write_frame allows the muxer to get the
790 // fragment end durations right. We always set the packet duration to
791 // the expected, but we simulate dropped frames at one point.
792 do_interleave = 1;
793 init_out("vfr-noduration-interleave");
794 av_dict_set(&opts, "movflags", "+frag_keyframe+delay_moov", 0);
795 av_dict_set(&opts, "frag_duration", "650000", 0);
796 init_fps(1, 1, 30, 0);
797 mux_frames(gop_size/2, 0);
798 // Pretend that the packet duration is the normal, even if
799 // we actually skip a bunch of frames. (I.e., simulate that
800 // we don't know of the framedrop in advance.)
802 duration *= 10;
803 mux_frames(1, 0);
805 duration /= 10;
806 mux_frames(gop_size/2 - 1, 0);
807 mux_gops(1);
808 finish();
809 close_out();
810 clear_duration = 0;
811 do_interleave = 0;
812
813 // Write a fragmented file with b-frames and audio preroll,
814 // with negative cts values, removing the edit list for the
815 // video track.
816 init_out("delay-moov-elst-neg-cts");
817 av_dict_set(&opts, "movflags", "+frag_keyframe+delay_moov+negative_cts_offsets", 0);
818 init(1, 1);
819 mux_gops(2);
820 finish();
821 close_out();
822
823 // Write a fragmented file with b-frames without audio preroll,
824 // with negative cts values, avoiding any edit lists, allowing
825 // to use empty_moov instead of delay_moov.
826 init_out("empty-moov-neg-cts");
827 av_dict_set(&opts, "movflags", "+frag_keyframe+empty_moov+negative_cts_offsets", 0);
828 init(1, 0);
829 mux_gops(2);
830 finish();
831 close_out();
832
833 // Write a manually fragmented file, with timed ID3 packets at the head
834 // of each fragment.
835 init_out("emsg");
836 av_dict_set(&opts, "movflags", "+frag_custom+cmaf", 0);
837 init_fps(1, 0, 30, 1);
838 mux_id3();
839 mux_gops(2);
840 av_write_frame(ctx, NULL); // Flush fragment.
841 mux_id3();
842 mux_gops(2);
843 finish();
844 close_out();
845
846 av_free(md5);
848
849 return check_faults > 0 ? 1 : 0;
850}
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 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 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:1503
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
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:608
double value
Definition eval.c:102
static int64_t duration
Definition ffplay.c:330
static int getopt(int argc, char *argv[], const char *opts)
Definition getopt.c:41
#define check_func
Definition test.h:481
@ AV_CODEC_ID_TIMED_ID3
Definition codec_id.h:607
@ AV_CODEC_ID_H264
Definition codec_id.h:77
@ AV_CODEC_ID_AAC
Definition codec_id.h:456
#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:434
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition packet.h:650
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.
AVFormatContext * avformat_alloc_context(void)
Allocate an AVFormatContext.
Definition options.c:165
void avformat_free_context(AVFormatContext *s)
Free an AVFormatContext and all its streams.
Definition avformat.c:150
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:467
int av_interleaved_write_frame(AVFormatContext *s, AVPacket *pkt)
Write a packet to an output media file ensuring correct interleaving.
Definition mux.c:1223
int av_write_trailer(AVFormatContext *s)
Write the stream trailer to an output media file and free the file private data.
Definition mux.c:1238
const AVOutputFormat * av_guess_format(const char *short_name, const char *filename, const char *mime_type)
Return the output format in the list of registered output formats which best matches the provided par...
Definition format.c:79
int av_write_frame(AVFormatContext *s, AVPacket *pkt)
Write a packet to an output media file.
Definition mux.c:1176
#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:233
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:86
#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:491
void av_log_default_callback(void *ptr, int level, const char *fmt, va_list vl)
Default logging callback.
Definition log.c:379
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
cl_device_type type
#define AV_WB32(p, v)
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:63
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:1335
Definition md5.c:42
This structure stores compressed data.
Definition packet.h:580
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]