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
27
29
30 #if HAVE_UNISTD_H
31 #include <unistd.h>
32 #endif
33
34 #if !HAVE_GETOPT
36 #endif
37
39
41 0x01, 0x4d, 0x40, 0x1e, 0xff, 0xe1, 0x00, 0x02, 0x67, 0x4d, 0x01, 0x00, 0x02, 0x68, 0xef
42 };
44 0x12, 0x10
45 };
46
47
52
59
63
77
79
81
82
84 {
87 }
88
90 {
93 }
94
96 {
98 }
99
101 {
107 }
108
111 {
112 char timebuf[30], content[5] = { 0 };
113 const char *str;
120 default: str = "unknown"; break;
121 }
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
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);
137 }
138
140 {
141 char buf[100];
144
147 out = fopen(buf,
"wb");
149 perror(buf);
150 }
152 }
153
155 {
164 }
165
167 {
169 va_list ap;
170 va_start(ap, msg);
172 vprintf(msg, ap);
175 va_end(ap);
176 }
177 }
178 #define check(value, ...) check_func(value, __LINE__, __VA_ARGS__)
179
180 static void init_fps(
int bf,
int audio_preroll,
int fps,
int id3)
181 {
186 exit(1);
189 exit(1);
192 exit(1);
195
197 if (!st)
198 exit(1);
208 exit(1);
211
213 if (!st)
214 exit(1);
225 exit(1);
228
229 if (id3) {
231 if (!st)
232 exit(1);
238 }
239
241 exit(1);
243
251
255 }
256
257 static void init(
int bf,
int audio_preroll)
258 {
260 }
261
263 {
264 int end_frames =
frames + n;
265 while (1) {
266 uint8_t pktdata[8] = { 0 };
268
275 } else {
277 break;
286 } else {
291 } else {
296 } else {
299 }
300 }
301 }
307 }
308
315 continue;
317 continue;
318
322 }
323
326 else
328 }
329 }
330
332 {
333 uint8_t pktdata[8] = { 0 };
335
340
344
346 }
347
349 {
351 }
352
354 {
358 }
359
361 {
363
368
372 }
373
375 {
380 }
381
383 {
384 printf(
"movenc-test [-w]\n"
385 "-w write output into files\n");
386 }
387
388 int main(
int argc,
char **argv)
389 {
393 int empty_moov_pos;
394 int prev_pos;
395
396 for (;;) {
399 break;
401 case 'w':
403 break;
404 default:
405 case 'h':
407 return 0;
408 }
409 }
410
413 return 1;
417 return 1;
418 }
419
420 // Write a fragmented file with an initial moov that actually contains some
421 // samples. One moov+mdat with 1 second of data and one moof+mdat with 1
422 // second of data.
429
430 // Write a similar file, but with B-frames and audio preroll, handled
431 // via an edit list.
439
440 // Use B-frames but no audio-preroll, but without an edit list.
441 // Due to avoid_negative_ts == AVFMT_AVOID_NEG_TS_MAKE_ZERO, the dts
442 // of the first audio packet is > 0, but it is set to zero since edit
443 // lists aren't used, increasing the duration of the first packet instead.
451
453 // Write an ISMV, with B-frames and audio preroll.
461
462 // An initial moov that doesn't contain any samples, followed by two
463 // moof+mdat pairs.
472
473 // Similar to the previous one, but with input that doesn't start at
474 // pts/dts 0. avoid_negative_ts behaves in the same way as
475 // in non-empty-moov-no-elst above.
483
486
487 // Same as the previous one, but disable avoid_negative_ts (which
488 // would require using an edit list, but with empty_moov, one can't
489 // write a sensible edit list, when the start timestamps aren't known).
490 // This should trigger a warning - we check that the warning is produced.
492 init_out(
"empty-moov-no-elst-no-adjust");
499
502
503 // Verify that delay_moov produces the same as empty_moov for
504 // simple input
513
514 // Test writing content that requires an edit list using delay_moov
521
522 // Test writing a file with one track lacking packets, with delay_moov.
528 // The automatic flushing shouldn't output anything, since we're still
529 // waiting for data for some tracks
531 // When closed (or manually flushed), all the written data should still
532 // be output.
536
537 // Check that manually flushing still outputs things as expected. This
538 // produces two fragments, while the one above produces only one.
539 init_out(
"delay-moov-empty-track-flush");
550
552
553
554
555 // Verify that the header written by delay_moov when manually flushed
556 // is identical to the one by empty_moov.
565 // Written 2 seconds of content, with an automatic flush after 1 second.
567 empty_moov_pos = prev_pos =
out_size;
568 // Manually flush the second fragment
572 // Check that an extra flush doesn't output any more data
577 // Ignore the trailer written here
579
584 check(
out_size == 0,
"Output written during init with delay_moov");
585 mux_gops(1);
// Write 1 second of content
591 check(
out_size == empty_moov_pos,
"Manually flushed content differs from automatically flushed, %d vs %d",
out_size, empty_moov_pos);
592 mux_gops(1);
// Write the rest of the content
595 check(!memcmp(
hash, content,
HASH_SIZE),
"delay_moov content differs from empty_moov");
597
598
599 // Verify that we can produce an identical second fragment without
600 // writing the first one. First write the reference fragments that
601 // we want to reproduce.
612
613 // Produce the same second fragment without actually writing the first
614 // one before.
615 av_dict_set(&
opts,
"movflags",
"+frag_custom+empty_moov+dash+frag_discont", 0);
621 init_out(
"empty-moov-second-frag-discont");
627
628 // Produce the same thing by using delay_moov, which requires a slightly
629 // different call sequence.
630 av_dict_set(&
opts,
"movflags",
"+frag_custom+delay_moov+dash+frag_discont", 0);
636 init_out(
"delay-moov-second-frag-discont");
641
642
643 // Test discontinuously written fragments with B-frames (where the
644 // assumption of starting at pts=0 works) but not with audio preroll
645 // (which can't be guessed).
654 init_out(
"delay-moov-elst-second-frag");
660
661 av_dict_set(&
opts,
"movflags",
"+frag_custom+delay_moov+dash+frag_discont", 0);
665 mux_gops(1);
// Write the second fragment
666 init_out(
"delay-moov-elst-init-discont");
670 init_out(
"delay-moov-elst-second-frag-discont");
675
676
677 // Test discontinuously written fragments with B-frames and audio preroll,
678 // properly signaled.
682 init_out(
"delay-moov-elst-signal-init");
687 init_out(
"delay-moov-elst-signal-second-frag");
693
694 av_dict_set(&
opts,
"movflags",
"+frag_custom+delay_moov+dash+frag_discont", 0);
699 mux_gops(1);
// Write the second fragment
700 init_out(
"delay-moov-elst-signal-init-discont");
704 init_out(
"delay-moov-elst-signal-second-frag-discont");
709
710
711 // Test muxing discontinuous fragments with very large (> (1<<31)) timestamps.
712 av_dict_set(&
opts,
"movflags",
"+frag_custom+delay_moov+dash+frag_discont", 0);
718 init_out(
"delay-moov-elst-signal-init-discont-largets");
721 init_out(
"delay-moov-elst-signal-second-frag-discont-largets");
725
726 // Test VFR content, with sidx atoms (which declare the pts duration
727 // of a fragment, forcing overriding the start pts of the next one).
728 // Here, the fragment duration in pts is significantly different from
729 // the duration in dts. The video stream starts at dts=-10,pts=0, and
730 // the second fragment starts at dts=155,pts=156. The trun duration sum
731 // of the first fragment is 165, which also is written as
732 // baseMediaDecodeTime in the tfdt in the second fragment. The sidx for
733 // the first fragment says earliest_presentation_time = 0 and
734 // subsegment_duration = 156, which also matches the sidx in the second
735 // fragment. For the audio stream, the pts and dts durations also don't
736 // match - the input stream starts at pts=-2048, but that part is excluded
737 // by the edit list.
747
748 // Test VFR content, with cleared duration fields. In these cases,
749 // the muxer must guess the duration of the last packet of each
750 // fragment. As long as the framerate doesn't vary (too much) at the
751 // fragment edge, it works just fine. Additionally, when automatically
752 // cutting fragments, the muxer already know the timestamps of the next
753 // packet for one stream (in most cases the video stream), avoiding
754 // having to use guesses for that one.
769
770 // Test with an IO buffer size that is too small to hold a full fragment;
771 // this will cause write_data_type to be called with the type unknown.
780
781 // Test VFR content with bframes with interleaving.
782 // Here, using av_interleaved_write_frame allows the muxer to get the
783 // fragment end durations right. We always set the packet duration to
784 // the expected, but we simulate dropped frames at one point.
786 init_out(
"vfr-noduration-interleave");
791 // Pretend that the packet duration is the normal, even if
792 // we actually skip a bunch of frames. (I.e., simulate that
793 // we don't know of the framedrop in advance.)
805
806 // Write a fragmented file with b-frames and audio preroll,
807 // with negative cts values, removing the edit list for the
808 // video track.
809 init_out(
"delay-moov-elst-neg-cts");
810 av_dict_set(&
opts,
"movflags",
"+frag_keyframe+delay_moov+negative_cts_offsets", 0);
815
816 // Write a fragmented file with b-frames without audio preroll,
817 // with negative cts values, avoiding any edit lists, allowing
818 // to use empty_moov instead of delay_moov.
820 av_dict_set(&
opts,
"movflags",
"+frag_keyframe+empty_moov+negative_cts_offsets", 0);
825
826 // Write a manually fragmented file, with timed ID3 packets at the head
827 // of each fragment.
838
841
843 }