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
26
28
29 #if HAVE_UNISTD_H
30 #include <unistd.h>
31 #endif
32
33 #if !HAVE_GETOPT
35 #endif
36
38
40 0x01, 0x4d, 0x40, 0x1e, 0xff, 0xe1, 0x00, 0x02, 0x67, 0x4d, 0x01, 0x00, 0x02, 0x68, 0xef
41 };
43 0x12, 0x10
44 };
45
46
51
58
62
76
78
80
81
83 {
86 }
87
89 {
92 }
93
95 {
97 }
98
100 {
106 }
107
110 {
111 char timebuf[30], content[5] = { 0 };
112 const char *str;
119 default: str = "unknown"; break;
120 }
122 snprintf(timebuf,
sizeof(timebuf),
"nopts");
123 else
124 snprintf(timebuf,
sizeof(timebuf),
"%"PRId64, time);
125 // There can be multiple header/trailer callbacks, only log the box type
126 // for header at out_size == 0
131 memcpy(content, &buf[4], 4);
132 else
133 snprintf(content,
sizeof(content),
"-");
134 printf(
"write_data len %d, time %s, type %s atom %s\n",
size, timebuf, str, content);
136 }
137
139 {
140 char buf[100];
143
146 out = fopen(buf,
"wb");
148 perror(buf);
149 }
151 }
152
154 {
163 }
164
166 {
168 va_list ap;
169 va_start(ap, msg);
171 vprintf(msg, ap);
174 va_end(ap);
175 }
176 }
177 #define check(value, ...) check_func(value, __LINE__, __VA_ARGS__)
178
179 static void init_fps(
int bf,
int audio_preroll,
int fps)
180 {
185 exit(1);
188 exit(1);
191 exit(1);
194
196 if (!st)
197 exit(1);
207 exit(1);
210
212 if (!st)
213 exit(1);
223 exit(1);
226
228 exit(1);
230
235 if (audio_preroll)
237
241 }
242
243 static void init(
int bf,
int audio_preroll)
244 {
246 }
247
249 {
250 int end_frames =
frames + n;
251 while (1) {
252 uint8_t pktdata[8] = { 0 };
254
260 } else {
262 break;
271 } else {
276 } else {
281 } else {
284 }
285 }
286 }
292 }
293
300 continue;
302 continue;
303
307 }
308
311 else
313 }
314 }
315
317 {
319 }
320
322 {
326 }
327
329 {
331
336
340 }
341
343 {
348 }
349
351 {
352 printf(
"movenc-test [-w]\n"
353 "-w write output into files\n");
354 }
355
356 int main(
int argc,
char **argv)
357 {
361 int empty_moov_pos;
362 int prev_pos;
363
364 for (;;) {
367 break;
369 case 'w':
371 break;
372 default:
373 case 'h':
375 return 0;
376 }
377 }
378
381 return 1;
385 return 1;
386 }
387
388 // Write a fragmented file with an initial moov that actually contains some
389 // samples. One moov+mdat with 1 second of data and one moof+mdat with 1
390 // second of data.
397
398 // Write a similar file, but with B-frames and audio preroll, handled
399 // via an edit list.
407
408 // Use B-frames but no audio-preroll, but without an edit list.
409 // Due to avoid_negative_ts == AVFMT_AVOID_NEG_TS_MAKE_ZERO, the dts
410 // of the first audio packet is > 0, but it is set to zero since edit
411 // lists aren't used, increasing the duration of the first packet instead.
419
421 // Write an ISMV, with B-frames and audio preroll.
429
430 // An initial moov that doesn't contain any samples, followed by two
431 // moof+mdat pairs.
440
441 // Similar to the previous one, but with input that doesn't start at
442 // pts/dts 0. avoid_negative_ts behaves in the same way as
443 // in non-empty-moov-no-elst above.
450
451 // Same as the previous one, but disable avoid_negative_ts (which
452 // would require using an edit list, but with empty_moov, one can't
453 // write a sensible edit list, when the start timestamps aren't known).
454 // This should trigger a warning - we check that the warning is produced.
456 init_out(
"empty-moov-no-elst-no-adjust");
463
466
467 // Verify that delay_moov produces the same as empty_moov for
468 // simple input
477
478 // Test writing content that requires an edit list using delay_moov
485
486 // Test writing a file with one track lacking packets, with delay_moov.
492 // The automatic flushing shouldn't output anything, since we're still
493 // waiting for data for some tracks
495 // When closed (or manually flushed), all the written data should still
496 // be output.
500
501 // Check that manually flushing still outputs things as expected. This
502 // produces two fragments, while the one above produces only one.
503 init_out(
"delay-moov-empty-track-flush");
514
516
517
518
519 // Verify that the header written by delay_moov when manually flushed
520 // is identical to the one by empty_moov.
529 // Written 2 seconds of content, with an automatic flush after 1 second.
531 empty_moov_pos = prev_pos =
out_size;
532 // Manually flush the second fragment
536 // Check that an extra flush doesn't output any more data
541 // Ignore the trailer written here
543
548 check(
out_size == 0,
"Output written during init with delay_moov");
549 mux_gops(1);
// Write 1 second of content
555 check(
out_size == empty_moov_pos,
"Manually flushed content differs from automatically flushed, %d vs %d",
out_size, empty_moov_pos);
556 mux_gops(1);
// Write the rest of the content
559 check(!memcmp(
hash, content,
HASH_SIZE),
"delay_moov content differs from empty_moov");
561
562
563 // Verify that we can produce an identical second fragment without
564 // writing the first one. First write the reference fragments that
565 // we want to reproduce.
576
577 // Produce the same second fragment without actually writing the first
578 // one before.
579 av_dict_set(&
opts,
"movflags",
"+frag_custom+empty_moov+dash+frag_discont", 0);
585 init_out(
"empty-moov-second-frag-discont");
591
592 // Produce the same thing by using delay_moov, which requires a slightly
593 // different call sequence.
594 av_dict_set(&
opts,
"movflags",
"+frag_custom+delay_moov+dash+frag_discont", 0);
600 init_out(
"delay-moov-second-frag-discont");
605
606
607 // Test discontinuously written fragments with B-frames (where the
608 // assumption of starting at pts=0 works) but not with audio preroll
609 // (which can't be guessed).
618 init_out(
"delay-moov-elst-second-frag");
624
625 av_dict_set(&
opts,
"movflags",
"+frag_custom+delay_moov+dash+frag_discont", 0);
629 mux_gops(1);
// Write the second fragment
630 init_out(
"delay-moov-elst-init-discont");
634 init_out(
"delay-moov-elst-second-frag-discont");
639
640
641 // Test discontinuously written fragments with B-frames and audio preroll,
642 // properly signaled.
646 init_out(
"delay-moov-elst-signal-init");
651 init_out(
"delay-moov-elst-signal-second-frag");
657
658 av_dict_set(&
opts,
"movflags",
"+frag_custom+delay_moov+dash+frag_discont", 0);
663 mux_gops(1);
// Write the second fragment
664 init_out(
"delay-moov-elst-signal-init-discont");
668 init_out(
"delay-moov-elst-signal-second-frag-discont");
673
674
675 // Test muxing discontinuous fragments with very large (> (1<<31)) timestamps.
676 av_dict_set(&
opts,
"movflags",
"+frag_custom+delay_moov+dash+frag_discont", 0);
682 init_out(
"delay-moov-elst-signal-init-discont-largets");
685 init_out(
"delay-moov-elst-signal-second-frag-discont-largets");
689
690 // Test VFR content, with sidx atoms (which declare the pts duration
691 // of a fragment, forcing overriding the start pts of the next one).
692 // Here, the fragment duration in pts is significantly different from
693 // the duration in dts. The video stream starts at dts=-10,pts=0, and
694 // the second fragment starts at dts=155,pts=156. The trun duration sum
695 // of the first fragment is 165, which also is written as
696 // baseMediaDecodeTime in the tfdt in the second fragment. The sidx for
697 // the first fragment says earliest_presentation_time = 0 and
698 // subsegment_duration = 156, which also matches the sidx in the second
699 // fragment. For the audio stream, the pts and dts durations also don't
700 // match - the input stream starts at pts=-2048, but that part is excluded
701 // by the edit list.
711
712 // Test VFR content, with cleared duration fields. In these cases,
713 // the muxer must guess the duration of the last packet of each
714 // fragment. As long as the framerate doesn't vary (too much) at the
715 // fragment edge, it works just fine. Additionally, when automatically
716 // cutting fragments, the muxer already know the timestamps of the next
717 // packet for one stream (in most cases the video stream), avoiding
718 // having to use guesses for that one.
733
734 // Test with an IO buffer size that is too small to hold a full fragment;
735 // this will cause write_data_type to be called with the type unknown.
744
745 // Test VFR content with bframes with interleaving.
746 // Here, using av_interleaved_write_frame allows the muxer to get the
747 // fragment end durations right. We always set the packet duration to
748 // the expected, but we simulate dropped frames at one point.
750 init_out(
"vfr-noduration-interleave");
755 // Pretend that the packet duration is the normal, even if
756 // we actually skip a bunch of frames. (I.e., simulate that
757 // we don't know of the framedrop in advance.)
769
770 // Write a fragmented file with b-frames and audio preroll,
771 // with negative cts values, removing the edit list for the
772 // video track.
773 init_out(
"delay-moov-elst-neg-cts");
774 av_dict_set(&
opts,
"movflags",
"+frag_keyframe+delay_moov+negative_cts_offsets", 0);
779
780 // Write a fragmented file with b-frames without audio preroll,
781 // with negative cts values, avoiding any edit lists, allowing
782 // to use empty_moov instead of delay_moov.
784 av_dict_set(&
opts,
"movflags",
"+frag_keyframe+empty_moov+negative_cts_offsets", 0);
789
792
794 }