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
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
61
75
77
79
80
82 {
85 }
86
88 {
91 }
92
94 {
96 }
97
99 {
103 fwrite(buf, 1, size,
out);
105 }
106
109 {
110 char timebuf[30], content[5] = { 0 };
111 const char *str;
112 switch (type) {
118 default: str = "unknown"; break;
119 }
121 snprintf(timebuf,
sizeof(timebuf),
"nopts");
122 else
123 snprintf(timebuf,
sizeof(timebuf),
"%"PRId64, time);
124 // There can be multiple header/trailer callbacks, only log the box type
125 // for header at out_size == 0
129 size >= 8)
130 memcpy(content, &buf[4], 4);
131 else
132 snprintf(content,
sizeof(content),
"-");
133 printf("write_data len %d, time %s, type %s atom %s\n", size, timebuf, str, content);
135 }
136
138 {
142
145 out = fopen(buf,
"wb");
147 perror(buf);
148 }
150 }
151
153 {
154 int i;
157 printf(
"%02x",
hash[i]);
162 }
163
165 {
166 if (!value) {
167 va_list ap;
168 va_start(ap, msg);
169 printf("%d: ", line);
170 vprintf(msg, ap);
171 printf("\n");
173 va_end(ap);
174 }
175 }
176 #define check(value, ...) check_func(value, __LINE__, __VA_ARGS__)
177
178 static void init_fps(
int bf,
int audio_preroll,
int fps)
179 {
183 if (!ctx)
184 exit(1);
187 exit(1);
190 exit(1);
193
195 if (!st)
196 exit(1);
206 exit(1);
208 video_st = st;
209
211 if (!st)
212 exit(1);
222 exit(1);
224 audio_st = st;
225
227 exit(1);
229
234 if (audio_preroll)
236
240 }
241
242 static void init(
int bf,
int audio_preroll)
243 {
245 }
246
248 {
250 while (1) {
254
260 } else {
262 break;
271 } else {
276 } else {
278 if (((
frames + 1) % gop_size) == 0) {
281 } else {
284 }
285 }
286 }
292 }
293
300 continue;
302 continue;
303
304 if (c) {
305 pkt.
pts += (1LL<<32);
306 pkt.
dts += (1LL<<32);
307 }
308
311 else
313 }
314 }
315
317 {
319 }
320
322 {
326 }
327
329 {
334
339
343 }
344
346 {
351 }
352
354 {
355 printf("movenc-test [-w]\n"
356 "-w write output into files\n");
357 }
358
359 int main(
int argc,
char **argv)
360 {
364 int empty_moov_pos;
365 int prev_pos;
366
367 for (;;) {
368 c =
getopt(argc, argv,
"wh");
369 if (c == -1)
370 break;
371 switch (c) {
372 case 'w':
374 break;
375 default:
376 case 'h':
378 return 0;
379 }
380 }
381
383 if (!md5)
384 return 1;
385
386 // Write a fragmented file with an initial moov that actually contains some
387 // samples. One moov+mdat with 1 second of data and one moof+mdat with 1
388 // second of data.
390 av_dict_set(&opts,
"movflags",
"frag_keyframe", 0);
395
396 // Write a similar file, but with B-frames and audio preroll, handled
397 // via an edit list.
399 av_dict_set(&opts,
"movflags",
"frag_keyframe", 0);
405
406 // Use B-frames but no audio-preroll, but without an edit list.
407 // Due to avoid_negative_ts == AVFMT_AVOID_NEG_TS_MAKE_ZERO, the dts
408 // of the first audio packet is > 0, but it is set to zero since edit
409 // lists aren't used, increasing the duration of the first packet instead.
411 av_dict_set(&opts,
"movflags",
"frag_keyframe", 0);
417
419 // Write an ISMV, with B-frames and audio preroll.
421 av_dict_set(&opts,
"movflags",
"frag_keyframe", 0);
427
428 // An initial moov that doesn't contain any samples, followed by two
429 // moof+mdat pairs.
431 av_dict_set(&opts,
"movflags",
"frag_keyframe+empty_moov", 0);
438
439 // Similar to the previous one, but with input that doesn't start at
440 // pts/dts 0. avoid_negative_ts behaves in the same way as
441 // in non-empty-moov-no-elst above.
443 av_dict_set(&opts,
"movflags",
"frag_keyframe+empty_moov", 0);
448
449 // Same as the previous one, but disable avoid_negative_ts (which
450 // would require using an edit list, but with empty_moov, one can't
451 // write a sensible edit list, when the start timestamps aren't known).
452 // This should trigger a warning - we check that the warning is produced.
454 init_out(
"empty-moov-no-elst-no-adjust");
455 av_dict_set(&opts,
"movflags",
"frag_keyframe+empty_moov", 0);
461
464
465 // Verify that delay_moov produces the same as empty_moov for
466 // simple input
468 av_dict_set(&opts,
"movflags",
"frag_keyframe+delay_moov", 0);
475
476 // Test writing content that requires an edit list using delay_moov
478 av_dict_set(&opts,
"movflags",
"frag_keyframe+delay_moov", 0);
483
484 // Test writing a file with one track lacking packets, with delay_moov.
487 av_dict_set(&opts,
"movflags",
"frag_keyframe+delay_moov", 0);
490 // The automatic flushing shouldn't output anything, since we're still
491 // waiting for data for some tracks
493 // When closed (or manually flushed), all the written data should still
494 // be output.
498
499 // Check that manually flushing still outputs things as expected. This
500 // produces two fragments, while the one above produces only one.
501 init_out(
"delay-moov-empty-track-flush");
502 av_dict_set(&opts,
"movflags",
"frag_custom+delay_moov", 0);
512
514
515
516
517 // Verify that the header written by delay_moov when manually flushed
518 // is identical to the one by empty_moov.
520 av_dict_set(&opts,
"movflags",
"frag_keyframe+empty_moov", 0);
527 // Written 2 seconds of content, with an automatic flush after 1 second.
529 empty_moov_pos = prev_pos =
out_size;
530 // Manually flush the second fragment
534 // Check that an extra flush doesn't output any more data
539 // Ignore the trailer written here
541
543 av_dict_set(&opts,
"movflags",
"frag_custom+delay_moov", 0);
546 check(
out_size == 0,
"Output written during init with delay_moov");
547 mux_gops(1);
// Write 1 second of content
553 check(
out_size == empty_moov_pos,
"Manually flushed content differs from automatically flushed, %d vs %d",
out_size, empty_moov_pos);
554 mux_gops(1);
// Write the rest of the content
557 check(!memcmp(
hash, content,
HASH_SIZE),
"delay_moov content differs from empty_moov");
559
560
561 // Verify that we can produce an identical second fragment without
562 // writing the first one. First write the reference fragments that
563 // we want to reproduce.
564 av_dict_set(&opts,
"movflags",
"frag_custom+empty_moov+dash", 0);
574
575 // Produce the same second fragment without actually writing the first
576 // one before.
577 av_dict_set(&opts,
"movflags",
"frag_custom+empty_moov+dash+frag_discont", 0);
583 init_out(
"empty-moov-second-frag-discont");
589
590 // Produce the same thing by using delay_moov, which requires a slightly
591 // different call sequence.
592 av_dict_set(&opts,
"movflags",
"frag_custom+delay_moov+dash+frag_discont", 0);
598 init_out(
"delay-moov-second-frag-discont");
603
604
605 // Test discontinuously written fragments with B-frames (where the
606 // assumption of starting at pts=0 works) but not with audio preroll
607 // (which can't be guessed).
608 av_dict_set(&opts,
"movflags",
"frag_custom+delay_moov+dash", 0);
616 init_out(
"delay-moov-elst-second-frag");
622
623 av_dict_set(&opts,
"movflags",
"frag_custom+delay_moov+dash+frag_discont", 0);
627 mux_gops(1);
// Write the second fragment
628 init_out(
"delay-moov-elst-init-discont");
632 init_out(
"delay-moov-elst-second-frag-discont");
637
638
639 // Test discontinuously written fragments with B-frames and audio preroll,
640 // properly signaled.
641 av_dict_set(&opts,
"movflags",
"frag_custom+delay_moov+dash", 0);
644 init_out(
"delay-moov-elst-signal-init");
649 init_out(
"delay-moov-elst-signal-second-frag");
655
656 av_dict_set(&opts,
"movflags",
"frag_custom+delay_moov+dash+frag_discont", 0);
661 mux_gops(1);
// Write the second fragment
662 init_out(
"delay-moov-elst-signal-init-discont");
666 init_out(
"delay-moov-elst-signal-second-frag-discont");
671
672
673 // Test muxing discontinuous fragments with very large (> (1<<31)) timestamps.
674 av_dict_set(&opts,
"movflags",
"frag_custom+delay_moov+dash+frag_discont", 0);
680 init_out(
"delay-moov-elst-signal-init-discont-largets");
683 init_out(
"delay-moov-elst-signal-second-frag-discont-largets");
687
688 // Test VFR content, with sidx atoms (which declare the pts duration
689 // of a fragment, forcing overriding the start pts of the next one).
690 // Here, the fragment duration in pts is significantly different from
691 // the duration in dts. The video stream starts at dts=-10,pts=0, and
692 // the second fragment starts at dts=155,pts=156. The trun duration sum
693 // of the first fragment is 165, which also is written as
694 // baseMediaDecodeTime in the tfdt in the second fragment. The sidx for
695 // the first fragment says earliest_presentation_time = 0 and
696 // subsegment_duration = 156, which also matches the sidx in the second
697 // fragment. For the audio stream, the pts and dts durations also don't
698 // match - the input stream starts at pts=-2048, but that part is excluded
699 // by the edit list.
701 av_dict_set(&opts,
"movflags",
"frag_keyframe+delay_moov+dash", 0);
709
710 // Test VFR content, with cleared duration fields. In these cases,
711 // the muxer must guess the duration of the last packet of each
712 // fragment. As long as the framerate doesn't vary (too much) at the
713 // fragment edge, it works just fine. Additionally, when automatically
714 // cutting fragments, the muxer already know the timestamps of the next
715 // packet for one stream (in most cases the video stream), avoiding
716 // having to use guesses for that one.
720 av_dict_set(&opts,
"movflags",
"frag_keyframe+delay_moov+dash", 0);
731
732 // Test with an IO buffer size that is too small to hold a full fragment;
733 // this will cause write_data_type to be called with the type unknown.
736 av_dict_set(&opts,
"movflags",
"frag_keyframe+delay_moov", 0);
742
743 // Test VFR content with bframes with interleaving.
744 // Here, using av_interleaved_write_frame allows the muxer to get the
745 // fragment end durations right. We always set the packet duration to
746 // the expected, but we simulate dropped frames at one point.
748 init_out(
"vfr-noduration-interleave");
749 av_dict_set(&opts,
"movflags",
"frag_keyframe+delay_moov", 0);
753 // Pretend that the packet duration is the normal, even if
754 // we actually skip a bunch of frames. (I.e., simulate that
755 // we don't know of the framedrop in advance.)
767
768 // Write a fragmented file with b-frames and audio preroll,
769 // with negative cts values, removing the edit list for the
770 // video track.
771 init_out(
"delay-moov-elst-neg-cts");
772 av_dict_set(&opts,
"movflags",
"frag_keyframe+delay_moov+negative_cts_offsets", 0);
777
778 // Write a fragmented file with b-frames without audio preroll,
779 // with negative cts values, avoiding any edit lists, allowing
780 // to use empty_moov instead of delay_moov.
782 av_dict_set(&opts,
"movflags",
"frag_keyframe+empty_moov+negative_cts_offsets", 0);
787
789
791 }
int av_interleaved_write_frame(AVFormatContext *s, AVPacket *pkt)
Write a packet to an output media file ensuring correct interleaving.
static const uint8_t h264_extradata[]
int av_write_frame(AVFormatContext *s, AVPacket *pkt)
Write a packet to an output media file.
#define AV_LOG_WARNING
Something somehow does not look correct.
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
static void count_warnings(void *avcl, int level, const char *fmt, va_list vl)
#define AVIO_FLAG_WRITE
write-only
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
static void mux_frames(int n, int c)
enum AVPictureType last_picture
static int io_write_data_type(void *opaque, uint8_t *buf, int size, enum AVIODataMarkerType type, int64_t time)
void av_md5_update(AVMD5 *ctx, const uint8_t *src, int len)
Update hash value.
Trailer data, which doesn't contain actual content, but only for finalizing the output file...
struct AVMD5 * av_md5_alloc(void)
Allocate an AVMD5 context.
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
A point in the output bytestream where a demuxer can start parsing (for non self synchronizing bytest...
AVFormatContext * avformat_alloc_context(void)
Allocate an AVFormatContext.
int flags
Flags modifying the (de)muxer behaviour.
static void reset_count_warnings(void)
static const uint8_t header[24]
struct AVOutputFormat * oformat
The output container format.
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
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, 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.
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values. ...
enum AVMediaType codec_type
General type of the encoded data.
void av_log_default_callback(void *ptr, int level, const char *fmt, va_list vl)
Default logging callback.
int flags
A combination of AV_PKT_FLAG values.
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.
int extradata_size
Size of the extradata content in bytes.
int(* write_data_type)(void *opaque, uint8_t *buf, int buf_size, enum AVIODataMarkerType type, int64_t time)
A callback that is used instead of write_packet.
static void init_fps(int bf, int audio_preroll, int fps)
This is any, unlabelled data.
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.
void av_log_set_callback(void(*callback)(void *, int, const char *, va_list))
Set the logging callback.
GLsizei GLboolean const GLfloat * value
AVIODataMarkerType
Different data types that can be returned via the AVIO write_data_type callback.
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...
static void mux_gops(int n)
AVIOContext * pb
I/O context.
static void close_out(void)
static int getopt(int argc, char *argv[], char *opts)
void av_md5_init(AVMD5 *ctx)
Initialize MD5 hashing.
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
static const char * format
void avformat_free_context(AVFormatContext *s)
Free an AVFormatContext and all its streams.
void av_md5_final(AVMD5 *ctx, uint8_t *dst)
Finish hashing and output digest value.
static void init_out(const char *name)
static void init_count_warnings(void)
int sample_rate
Audio only.
void avio_context_free(AVIOContext **s)
Free the supplied IO context and everything associated with it.
int main(int argc, char **argv)
static void init(int bf, int audio_preroll)
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
static void signal_init_ts(void)
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
static void skip_gops(int n)
A point in the output bytestream where a decoder can start decoding (i.e.
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
int av_write_trailer(AVFormatContext *s)
Write the stream trailer to an output media file and free the file private data.
static int io_write(void *opaque, uint8_t *buf, int size)
Public header for MD5 hash function implementation.
AVCodecParameters * codecpar
Codec parameters associated with this stream.
static const uint8_t aac_extradata[]
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
This structure stores compressed data.
static void check_func(int value, int line, const char *msg,...)
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Header data; this needs to be present for the stream to be decodeable.
#define AV_NOPTS_VALUE
Undefined timestamp value.