FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/tests/api/api-movenc-test.c
Date: 2026-09-16 13:08:21
Exec Total Coverage
Lines: 515 549 93.8%
Functions: 17 18 94.4%
Branches: 71 94 75.5%

Line Branch Exec Source
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
23 #include "libavutil/intreadwrite.h"
24 #include "libavutil/mathematics.h"
25 #include "libavutil/md5.h"
26 #include "libavutil/mem.h"
27
28 #include "libavformat/avformat.h"
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
40 static const uint8_t h264_extradata[] = {
41 0x01, 0x4d, 0x40, 0x1e, 0xff, 0xe1, 0x00, 0x02, 0x67, 0x4d, 0x01, 0x00, 0x02, 0x68, 0xef
42 };
43 static const uint8_t aac_extradata[] = {
44 0x12, 0x10
45 };
46
47
48 static const char *format = "mp4";
49 static AVFormatContext *ctx;
50 static uint8_t iobuf[32768];
51 static AVDictionary *opts;
52
53 static int write_file;
54 static const char *cur_name;
55 static FILE* out;
56 static int out_size;
57 static struct AVMD5* md5;
58 static uint8_t hash[HASH_SIZE];
59
60 static AVPacket *pkt;
61 static AVStream *video_st, *audio_st, *id3_st;
62 static int64_t audio_dts, video_dts;
63
64 static int bframes;
65 static int64_t duration;
66 static int64_t audio_duration;
67 static int frames;
68 static int gop_size;
69 static int64_t next_p_pts;
70 static enum AVPictureType last_picture;
71 static int skip_write;
72 static int skip_write_audio;
73 static int clear_duration;
74 static int force_iobuf_size;
75 static int do_interleave;
76 static int fake_pkt_duration;
77
78 static int num_warnings;
79
80 static int check_faults;
81
82
83 6 static void count_warnings(void *avcl, int level, const char *fmt, va_list vl)
84 {
85
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
6 if (level == AV_LOG_WARNING)
86 3 num_warnings++;
87 6 }
88
89 3 static void init_count_warnings(void)
90 {
91 3 av_log_set_callback(count_warnings);
92 3 num_warnings = 0;
93 3 }
94
95 3 static void reset_count_warnings(void)
96 {
97 3 av_log_set_callback(av_log_default_callback);
98 3 }
99
100 129 static int io_write(void *opaque, const uint8_t *buf, int size)
101 {
102 129 out_size += size;
103 129 av_md5_update(md5, buf, size);
104
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 129 times.
129 if (out)
105 fwrite(buf, 1, size, out);
106 129 return size;
107 }
108
109 129 static int io_write_data_type(void *opaque, const uint8_t *buf, int size,
110 enum AVIODataMarkerType type, int64_t time)
111 {
112 129 char timebuf[30], content[5] = { 0 };
113 const char *str;
114
5/6
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 47 times.
✓ Branch 2 taken 5 times.
✓ Branch 3 taken 6 times.
✓ Branch 4 taken 29 times.
✗ Branch 5 not taken.
129 switch (type) {
115 42 case AVIO_DATA_MARKER_HEADER: str = "header"; break;
116 47 case AVIO_DATA_MARKER_SYNC_POINT: str = "sync"; break;
117 5 case AVIO_DATA_MARKER_BOUNDARY_POINT: str = "boundary"; break;
118 6 case AVIO_DATA_MARKER_UNKNOWN: str = "unknown"; break;
119 29 case AVIO_DATA_MARKER_TRAILER: str = "trailer"; break;
120 default: str = "unknown"; break;
121 }
122
2/2
✓ Branch 0 taken 77 times.
✓ Branch 1 taken 52 times.
129 if (time == AV_NOPTS_VALUE)
123 77 snprintf(timebuf, sizeof(timebuf), "nopts");
124 else
125 52 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
128
4/4
✓ Branch 0 taken 123 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 94 times.
✓ Branch 3 taken 29 times.
129 if (type != AVIO_DATA_MARKER_UNKNOWN &&
129
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 52 times.
94 type != AVIO_DATA_MARKER_TRAILER &&
130
3/4
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 16 times.
✓ Branch 2 taken 78 times.
✗ Branch 3 not taken.
94 (type != AVIO_DATA_MARKER_HEADER || out_size == 0) &&
131 size >= 8)
132 78 memcpy(content, &buf[4], 4);
133 else
134 51 snprintf(content, sizeof(content), "-");
135 129 printf("write_data len %d, time %s, type %s atom %s\n", size, timebuf, str, content);
136 129 return io_write(opaque, buf, size);
137 }
138
139 36 static void init_out(const char *name)
140 {
141 char buf[100];
142 36 cur_name = name;
143 36 snprintf(buf, sizeof(buf), "%s.%s", cur_name, format);
144
145 36 av_md5_init(md5);
146
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 36 times.
36 if (write_file) {
147 out = fopen(buf, "wb");
148 if (!out)
149 perror(buf);
150 }
151 36 out_size = 0;
152 36 }
153
154 36 static void close_out(void)
155 {
156 int i;
157 36 av_md5_final(md5, hash);
158
2/2
✓ Branch 0 taken 576 times.
✓ Branch 1 taken 36 times.
612 for (i = 0; i < HASH_SIZE; i++)
159 576 printf("%02x", hash[i]);
160 36 printf(" %d %s\n", out_size, cur_name);
161
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 36 times.
36 if (out)
162 fclose(out);
163 36 out = NULL;
164 36 }
165
166 20 static void check_func(int value, int line, const char *msg, ...)
167 {
168
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 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 20 }
178 #define check(value, ...) check_func(value, __LINE__, __VA_ARGS__)
179
180 29 static void init_fps(int bf, int audio_preroll, int fps, int id3)
181 {
182 AVStream *st;
183
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 28 times.
29 int iobuf_size = force_iobuf_size ? force_iobuf_size : sizeof(iobuf);
184 29 ctx = avformat_alloc_context();
185
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29 times.
29 if (!ctx)
186 exit(1);
187 29 ctx->oformat = av_guess_format(format, NULL, NULL);
188
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29 times.
29 if (!ctx->oformat)
189 exit(1);
190 29 ctx->pb = avio_alloc_context(iobuf, iobuf_size, 1, NULL, NULL, io_write, NULL);
191
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29 times.
29 if (!ctx->pb)
192 exit(1);
193 29 ctx->pb->write_data_type = io_write_data_type;
194 29 ctx->flags |= AVFMT_FLAG_BITEXACT;
195
196 29 st = avformat_new_stream(ctx, NULL);
197
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29 times.
29 if (!st)
198 exit(1);
199 29 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
200 29 st->codecpar->codec_id = AV_CODEC_ID_H264;
201 29 st->codecpar->width = 640;
202 29 st->codecpar->height = 480;
203 29 st->time_base.num = 1;
204 29 st->time_base.den = 30;
205 29 st->codecpar->extradata_size = sizeof(h264_extradata);
206 29 st->codecpar->extradata = av_mallocz(st->codecpar->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
207
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29 times.
29 if (!st->codecpar->extradata)
208 exit(1);
209 29 memcpy(st->codecpar->extradata, h264_extradata, sizeof(h264_extradata));
210 29 video_st = st;
211
212 29 st = avformat_new_stream(ctx, NULL);
213
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29 times.
29 if (!st)
214 exit(1);
215 29 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
216 29 st->codecpar->codec_id = AV_CODEC_ID_AAC;
217 29 st->codecpar->sample_rate = 44100;
218 29 st->codecpar->frame_size = 1024;
219 29 st->codecpar->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO;
220 29 st->time_base.num = 1;
221 29 st->time_base.den = 44100;
222 29 st->codecpar->extradata_size = sizeof(aac_extradata);
223 29 st->codecpar->extradata = av_mallocz(st->codecpar->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
224
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29 times.
29 if (!st->codecpar->extradata)
225 exit(1);
226 29 memcpy(st->codecpar->extradata, aac_extradata, sizeof(aac_extradata));
227 29 audio_st = st;
228
229
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 28 times.
29 if (id3) {
230 1 st = avformat_new_stream(ctx, NULL);
231
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!st)
232 exit(1);
233 1 st->codecpar->codec_type = AVMEDIA_TYPE_DATA;
234 1 st->codecpar->codec_id = AV_CODEC_ID_TIMED_ID3;
235 1 st->time_base.num = 1;
236 1 st->time_base.den = 1000;
237 1 id3_st = st;
238 }
239
240
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 29 times.
29 if (avformat_write_header(ctx, &opts) < 0)
241 exit(1);
242 29 av_dict_free(&opts);
243
244 29 frames = 0;
245 29 gop_size = 30;
246 29 duration = video_st->time_base.den / fps;
247 29 audio_duration = (long long)audio_st->codecpar->frame_size *
248 29 audio_st->time_base.den / audio_st->codecpar->sample_rate;
249
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 18 times.
29 if (audio_preroll)
250 11 audio_preroll = 2 * audio_duration;
251
252 29 bframes = bf;
253
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 11 times.
29 video_dts = bframes ? -duration : 0;
254 29 audio_dts = -audio_preroll;
255 29 }
256
257 24 static void init(int bf, int audio_preroll)
258 {
259 24 init_fps(bf, audio_preroll, 30, 0);
260 24 }
261
262 46 static void mux_frames(int n, int c)
263 {
264 46 int end_frames = frames + n;
265 5398 while (1) {
266 5444 uint8_t pktdata[8] = { 0 };
267 5444 av_packet_unref(pkt);
268
269
2/2
✓ Branch 1 taken 3658 times.
✓ Branch 2 taken 1786 times.
5444 if (av_compare_ts(audio_dts, audio_st->time_base, video_dts, video_st->time_base) < 0) {
270 3658 pkt->dts = pkt->pts = audio_dts;
271 3658 pkt->stream_index = 1;
272 3658 pkt->duration = audio_duration;
273 3658 pkt->flags |= AV_PKT_FLAG_KEY;
274 3658 audio_dts += audio_duration;
275 } else {
276
2/2
✓ Branch 0 taken 46 times.
✓ Branch 1 taken 1740 times.
1786 if (frames == end_frames)
277 46 break;
278 1740 pkt->dts = video_dts;
279 1740 pkt->stream_index = 0;
280 1740 pkt->duration = duration;
281
2/2
✓ Branch 0 taken 58 times.
✓ Branch 1 taken 1682 times.
1740 if ((frames % gop_size) == 0) {
282 58 pkt->flags |= AV_PKT_FLAG_KEY;
283 58 last_picture = AV_PICTURE_TYPE_I;
284 58 pkt->pts = pkt->dts + duration;
285 58 video_dts = pkt->pts;
286 } else {
287
2/2
✓ Branch 0 taken 812 times.
✓ Branch 1 taken 870 times.
1682 if (last_picture == AV_PICTURE_TYPE_P) {
288 812 last_picture = AV_PICTURE_TYPE_B;
289 812 pkt->pts = pkt->dts;
290 812 video_dts = next_p_pts;
291 } else {
292 870 last_picture = AV_PICTURE_TYPE_P;
293
2/2
✓ Branch 0 taken 58 times.
✓ Branch 1 taken 812 times.
870 if (((frames + 1) % gop_size) == 0) {
294 58 pkt->pts = pkt->dts + duration;
295 58 video_dts = pkt->pts;
296 } else {
297 812 next_p_pts = pkt->pts = pkt->dts + 2 * duration;
298 812 video_dts += duration;
299 }
300 }
301 }
302
2/2
✓ Branch 0 taken 600 times.
✓ Branch 1 taken 1140 times.
1740 if (!bframes)
303 600 pkt->pts = pkt->dts;
304
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1739 times.
1740 if (fake_pkt_duration)
305 1 pkt->duration = fake_pkt_duration;
306 1740 frames++;
307 }
308
309
2/2
✓ Branch 0 taken 328 times.
✓ Branch 1 taken 5070 times.
5398 if (clear_duration)
310 328 pkt->duration = 0;
311 5398 AV_WB32(pktdata + 4, pkt->pts);
312 5398 pkt->data = pktdata;
313 5398 pkt->size = 8;
314
2/2
✓ Branch 0 taken 368 times.
✓ Branch 1 taken 5030 times.
5398 if (skip_write)
315 542 continue;
316
4/4
✓ Branch 0 taken 294 times.
✓ Branch 1 taken 4736 times.
✓ Branch 2 taken 174 times.
✓ Branch 3 taken 120 times.
5030 if (skip_write_audio && pkt->stream_index == 1)
317 174 continue;
318
319
2/2
✓ Branch 0 taken 73 times.
✓ Branch 1 taken 4783 times.
4856 if (c) {
320 73 pkt->pts += (1LL<<32);
321 73 pkt->dts += (1LL<<32);
322 }
323
324
2/2
✓ Branch 0 taken 173 times.
✓ Branch 1 taken 4683 times.
4856 if (do_interleave)
325 173 av_interleaved_write_frame(ctx, pkt);
326 else
327 4683 av_write_frame(ctx, pkt);
328 }
329 46 }
330
331 2 static void mux_id3(void)
332 {
333 2 uint8_t pktdata[8] = { 0 };
334 2 av_packet_unref(pkt);
335
336 2 pkt->dts = pkt->pts = av_rescale_q(video_dts + (bframes ? duration : 0),
337
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 video_st->time_base, id3_st->time_base);
338 2 pkt->stream_index = id3_st->index;
339 2 pkt->duration = 0;
340
341 2 AV_WB32(pktdata + 4, pkt->pts);
342 2 pkt->data = pktdata;
343 2 pkt->size = 8;
344
345 2 av_write_frame(ctx, pkt);
346 2 }
347
348 38 static void mux_gops(int n)
349 {
350 38 mux_frames(gop_size * n, 0);
351 38 }
352
353 5 static void skip_gops(int n)
354 {
355 5 skip_write = 1;
356 5 mux_gops(n);
357 5 skip_write = 0;
358 5 }
359
360 2 static void signal_init_ts(void)
361 {
362 2 av_packet_unref(pkt);
363
364 2 pkt->stream_index = 0;
365 2 pkt->dts = video_dts;
366 2 pkt->pts = 0;
367 2 av_write_frame(ctx, pkt);
368
369 2 pkt->stream_index = 1;
370 2 pkt->dts = pkt->pts = audio_dts;
371 2 av_write_frame(ctx, pkt);
372 2 }
373
374 29 static void finish(void)
375 {
376 29 av_write_trailer(ctx);
377 29 avio_context_free(&ctx->pb);
378 29 avformat_free_context(ctx);
379 29 ctx = NULL;
380 29 }
381
382 static void help(void)
383 {
384 printf("movenc-test [-w]\n"
385 "-w write output into files\n");
386 }
387
388 1 int 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 1 c = getopt(argc, argv, "wh");
398
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (c == -1)
399 1 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 1 md5 = av_md5_alloc();
412
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!md5)
413 return 1;
414 1 pkt = av_packet_alloc();
415
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!pkt) {
416 av_free(md5);
417 return 1;
418 }
419
420 // Write a fragmented file with an initial moov and no samples.
421 1 init_out("non-empty-moov-no-mdat");
422 1 av_dict_set(&opts, "movflags", "+frag_keyframe", 0);
423 1 init(0, 0);
424 1 finish();
425 1 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 1 init_out("non-empty-moov");
431 1 av_dict_set(&opts, "movflags", "+frag_keyframe", 0);
432 1 init(0, 0);
433 1 mux_gops(2);
434 1 finish();
435 1 close_out();
436
437 // Write a similar file, but with B-frames and audio preroll, handled
438 // via an edit list.
439 1 init_out("non-empty-moov-elst");
440 1 av_dict_set(&opts, "movflags", "+frag_keyframe", 0);
441 1 av_dict_set(&opts, "use_editlist", "1", 0);
442 1 init(1, 1);
443 1 mux_gops(2);
444 1 finish();
445 1 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 1 init_out("non-empty-moov-no-elst");
452 1 av_dict_set(&opts, "movflags", "+frag_keyframe", 0);
453 1 av_dict_set(&opts, "use_editlist", "0", 0);
454 1 init(1, 0);
455 1 mux_gops(2);
456 1 finish();
457 1 close_out();
458
459 1 format = "ismv";
460 // Write an ISMV, with B-frames and audio preroll.
461 1 init_out("ismv");
462 1 av_dict_set(&opts, "movflags", "+frag_keyframe", 0);
463 1 init(1, 1);
464 1 mux_gops(2);
465 1 finish();
466 1 close_out();
467 1 format = "mp4";
468
469 // An initial moov that doesn't contain any samples, followed by two
470 // moof+mdat pairs.
471 1 init_out("empty-moov");
472 1 av_dict_set(&opts, "movflags", "+frag_keyframe+empty_moov", 0);
473 1 av_dict_set(&opts, "use_editlist", "0", 0);
474 1 init(0, 0);
475 1 mux_gops(2);
476 1 finish();
477 1 close_out();
478 1 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.
483 1 init_count_warnings();
484 1 init_out("empty-moov-no-elst");
485 1 av_dict_set(&opts, "movflags", "+frag_keyframe+empty_moov", 0);
486 1 init(1, 0);
487 1 mux_gops(2);
488 1 finish();
489 1 close_out();
490
491 1 reset_count_warnings();
492 1 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.
498 1 init_count_warnings();
499 1 init_out("empty-moov-no-elst-no-adjust");
500 1 av_dict_set(&opts, "movflags", "+frag_keyframe+empty_moov", 0);
501 1 av_dict_set(&opts, "avoid_negative_ts", "disabled", 0);
502 1 init(1, 0);
503 1 mux_gops(2);
504 1 finish();
505 1 close_out();
506
507 1 reset_count_warnings();
508 1 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 1 init_out("delay-moov");
513 1 av_dict_set(&opts, "movflags", "+frag_keyframe+delay_moov", 0);
514 1 av_dict_set(&opts, "use_editlist", "0", 0);
515 1 init(0, 0);
516 1 mux_gops(2);
517 1 finish();
518 1 close_out();
519 1 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 1 init_out("delay-moov-elst");
523 1 av_dict_set(&opts, "movflags", "+frag_keyframe+delay_moov", 0);
524 1 init(1, 1);
525 1 mux_gops(2);
526 1 finish();
527 1 close_out();
528
529 // Test writing a file with one track lacking packets, with delay_moov.
530 1 skip_write_audio = 1;
531 1 init_out("delay-moov-empty-track");
532 1 av_dict_set(&opts, "movflags", "+frag_keyframe+delay_moov", 0);
533 1 init(0, 0);
534 1 mux_gops(2);
535 // The automatic flushing shouldn't output anything, since we're still
536 // waiting for data for some tracks
537 1 check(out_size == 0, "delay_moov flushed prematurely");
538 // When closed (or manually flushed), all the written data should still
539 // be output.
540 1 finish();
541 1 close_out();
542 1 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 1 init_out("delay-moov-empty-track-flush");
547 1 av_dict_set(&opts, "movflags", "+frag_custom+delay_moov", 0);
548 1 init(0, 0);
549 1 mux_gops(1);
550 1 av_write_frame(ctx, NULL); // Force writing the moov
551 1 check(out_size > 0, "No moov written");
552 1 av_write_frame(ctx, NULL);
553 1 mux_gops(1);
554 1 av_write_frame(ctx, NULL);
555 1 finish();
556 1 close_out();
557
558 1 skip_write_audio = 0;
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 1 init_out("empty-moov-header");
565 1 av_dict_set(&opts, "movflags", "+frag_keyframe+empty_moov", 0);
566 1 av_dict_set(&opts, "use_editlist", "0", 0);
567 1 init(0, 0);
568 1 close_out();
569 1 memcpy(header, hash, HASH_SIZE);
570 1 init_out("empty-moov-content");
571 1 mux_gops(2);
572 // Written 2 seconds of content, with an automatic flush after 1 second.
573 1 check(out_size > 0, "No automatic flush?");
574 1 empty_moov_pos = prev_pos = out_size;
575 // Manually flush the second fragment
576 1 av_write_frame(ctx, NULL);
577 1 check(out_size > prev_pos, "No second fragment flushed?");
578 1 prev_pos = out_size;
579 // Check that an extra flush doesn't output any more data
580 1 av_write_frame(ctx, NULL);
581 1 check(out_size == prev_pos, "More data written?");
582 1 close_out();
583 1 memcpy(content, hash, HASH_SIZE);
584 // Ignore the trailer written here
585 1 finish();
586
587 1 init_out("delay-moov-header");
588 1 av_dict_set(&opts, "movflags", "+frag_custom+delay_moov", 0);
589 1 av_dict_set(&opts, "use_editlist", "0", 0);
590 1 init(0, 0);
591 1 check(out_size == 0, "Output written during init with delay_moov");
592 1 mux_gops(1); // Write 1 second of content
593 1 av_write_frame(ctx, NULL); // Force writing the moov
594 1 close_out();
595 1 check(!memcmp(hash, header, HASH_SIZE), "delay_moov header differs from empty_moov");
596 1 init_out("delay-moov-content");
597 1 av_write_frame(ctx, NULL); // Flush the first fragment
598 1 check(out_size == empty_moov_pos, "Manually flushed content differs from automatically flushed, %d vs %d", out_size, empty_moov_pos);
599 1 mux_gops(1); // Write the rest of the content
600 1 av_write_frame(ctx, NULL); // Flush the second fragment
601 1 close_out();
602 1 check(!memcmp(hash, content, HASH_SIZE), "delay_moov content differs from empty_moov");
603 1 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 1 av_dict_set(&opts, "movflags", "+frag_custom+empty_moov+dash", 0);
610 1 init(0, 0);
611 1 mux_gops(1);
612 1 av_write_frame(ctx, NULL); // Output the first fragment
613 1 init_out("empty-moov-second-frag");
614 1 mux_gops(1);
615 1 av_write_frame(ctx, NULL); // Output the second fragment
616 1 close_out();
617 1 memcpy(content, hash, HASH_SIZE);
618 1 finish();
619
620 // Produce the same second fragment without actually writing the first
621 // one before.
622 1 av_dict_set(&opts, "movflags", "+frag_custom+empty_moov+dash+frag_discont", 0);
623 1 av_dict_set(&opts, "fragment_index", "2", 0);
624 1 av_dict_set(&opts, "avoid_negative_ts", "disabled", 0);
625 1 av_dict_set(&opts, "use_editlist", "0", 0);
626 1 init(0, 0);
627 1 skip_gops(1);
628 1 init_out("empty-moov-second-frag-discont");
629 1 mux_gops(1);
630 1 av_write_frame(ctx, NULL); // Output the second fragment
631 1 close_out();
632 1 check(!memcmp(hash, content, HASH_SIZE), "discontinuously written fragment differs");
633 1 finish();
634
635 // Produce the same thing by using delay_moov, which requires a slightly
636 // different call sequence.
637 1 av_dict_set(&opts, "movflags", "+frag_custom+delay_moov+dash+frag_discont", 0);
638 1 av_dict_set(&opts, "fragment_index", "2", 0);
639 1 init(0, 0);
640 1 skip_gops(1);
641 1 mux_gops(1);
642 1 av_write_frame(ctx, NULL); // Output the moov
643 1 init_out("delay-moov-second-frag-discont");
644 1 av_write_frame(ctx, NULL); // Output the second fragment
645 1 close_out();
646 1 check(!memcmp(hash, content, HASH_SIZE), "discontinuously written fragment differs");
647 1 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 1 av_dict_set(&opts, "movflags", "+frag_custom+delay_moov+dash", 0);
654 1 init(1, 0);
655 1 mux_gops(1);
656 1 init_out("delay-moov-elst-init");
657 1 av_write_frame(ctx, NULL); // Output the moov
658 1 close_out();
659 1 memcpy(header, hash, HASH_SIZE);
660 1 av_write_frame(ctx, NULL); // Output the first fragment
661 1 init_out("delay-moov-elst-second-frag");
662 1 mux_gops(1);
663 1 av_write_frame(ctx, NULL); // Output the second fragment
664 1 close_out();
665 1 memcpy(content, hash, HASH_SIZE);
666 1 finish();
667
668 1 av_dict_set(&opts, "movflags", "+frag_custom+delay_moov+dash+frag_discont", 0);
669 1 av_dict_set(&opts, "fragment_index", "2", 0);
670 1 init(1, 0);
671 1 skip_gops(1);
672 1 mux_gops(1); // Write the second fragment
673 1 init_out("delay-moov-elst-init-discont");
674 1 av_write_frame(ctx, NULL); // Output the moov
675 1 close_out();
676 1 check(!memcmp(hash, header, HASH_SIZE), "discontinuously written header differs");
677 1 init_out("delay-moov-elst-second-frag-discont");
678 1 av_write_frame(ctx, NULL); // Output the second fragment
679 1 close_out();
680 1 check(!memcmp(hash, content, HASH_SIZE), "discontinuously written fragment differs");
681 1 finish();
682
683
684 // Test discontinuously written fragments with B-frames and audio preroll,
685 // properly signaled.
686 1 av_dict_set(&opts, "movflags", "+frag_custom+delay_moov+dash", 0);
687 1 init(1, 1);
688 1 mux_gops(1);
689 1 init_out("delay-moov-elst-signal-init");
690 1 av_write_frame(ctx, NULL); // Output the moov
691 1 close_out();
692 1 memcpy(header, hash, HASH_SIZE);
693 1 av_write_frame(ctx, NULL); // Output the first fragment
694 1 init_out("delay-moov-elst-signal-second-frag");
695 1 mux_gops(1);
696 1 av_write_frame(ctx, NULL); // Output the second fragment
697 1 close_out();
698 1 memcpy(content, hash, HASH_SIZE);
699 1 finish();
700
701 1 av_dict_set(&opts, "movflags", "+frag_custom+delay_moov+dash+frag_discont", 0);
702 1 av_dict_set(&opts, "fragment_index", "2", 0);
703 1 init(1, 1);
704 1 signal_init_ts();
705 1 skip_gops(1);
706 1 mux_gops(1); // Write the second fragment
707 1 init_out("delay-moov-elst-signal-init-discont");
708 1 av_write_frame(ctx, NULL); // Output the moov
709 1 close_out();
710 1 check(!memcmp(hash, header, HASH_SIZE), "discontinuously written header differs");
711 1 init_out("delay-moov-elst-signal-second-frag-discont");
712 1 av_write_frame(ctx, NULL); // Output the second fragment
713 1 close_out();
714 1 check(!memcmp(hash, content, HASH_SIZE), "discontinuously written fragment differs");
715 1 finish();
716
717
718 // Test muxing discontinuous fragments with very large (> (1<<31)) timestamps.
719 1 av_dict_set(&opts, "movflags", "+frag_custom+delay_moov+dash+frag_discont", 0);
720 1 av_dict_set(&opts, "fragment_index", "2", 0);
721 1 init(1, 1);
722 1 signal_init_ts();
723 1 skip_gops(1);
724 1 mux_frames(gop_size, 1); // Write the second fragment
725 1 init_out("delay-moov-elst-signal-init-discont-largets");
726 1 av_write_frame(ctx, NULL); // Output the moov
727 1 close_out();
728 1 init_out("delay-moov-elst-signal-second-frag-discont-largets");
729 1 av_write_frame(ctx, NULL); // Output the second fragment
730 1 close_out();
731 1 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 1 init_out("vfr");
746 1 av_dict_set(&opts, "movflags", "+frag_keyframe+delay_moov+dash", 0);
747 1 init_fps(1, 1, 3, 0);
748 1 mux_frames(gop_size/2, 0);
749 1 duration /= 10;
750 1 mux_frames(gop_size/2, 0);
751 1 mux_gops(1);
752 1 finish();
753 1 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.
762 1 init_count_warnings();
763 1 clear_duration = 1;
764 1 init_out("vfr-noduration");
765 1 av_dict_set(&opts, "movflags", "+frag_keyframe+delay_moov+dash", 0);
766 1 init_fps(1, 1, 3, 0);
767 1 mux_frames(gop_size/2, 0);
768 1 duration /= 10;
769 1 mux_frames(gop_size/2, 0);
770 1 mux_gops(1);
771 1 finish();
772 1 close_out();
773 1 clear_duration = 0;
774 1 reset_count_warnings();
775 1 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 1 force_iobuf_size = 1500;
780 1 init_out("large_frag");
781 1 av_dict_set(&opts, "movflags", "+frag_keyframe+delay_moov", 0);
782 1 init_fps(1, 1, 3, 0);
783 1 mux_gops(2);
784 1 finish();
785 1 close_out();
786 1 force_iobuf_size = 0;
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 1 do_interleave = 1;
793 1 init_out("vfr-noduration-interleave");
794 1 av_dict_set(&opts, "movflags", "+frag_keyframe+delay_moov", 0);
795 1 av_dict_set(&opts, "frag_duration", "650000", 0);
796 1 init_fps(1, 1, 30, 0);
797 1 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.)
801 1 fake_pkt_duration = duration;
802 1 duration *= 10;
803 1 mux_frames(1, 0);
804 1 fake_pkt_duration = 0;
805 1 duration /= 10;
806 1 mux_frames(gop_size/2 - 1, 0);
807 1 mux_gops(1);
808 1 finish();
809 1 close_out();
810 1 clear_duration = 0;
811 1 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 1 init_out("delay-moov-elst-neg-cts");
817 1 av_dict_set(&opts, "movflags", "+frag_keyframe+delay_moov+negative_cts_offsets", 0);
818 1 init(1, 1);
819 1 mux_gops(2);
820 1 finish();
821 1 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 1 init_out("empty-moov-neg-cts");
827 1 av_dict_set(&opts, "movflags", "+frag_keyframe+empty_moov+negative_cts_offsets", 0);
828 1 init(1, 0);
829 1 mux_gops(2);
830 1 finish();
831 1 close_out();
832
833 // Write a manually fragmented file, with timed ID3 packets at the head
834 // of each fragment.
835 1 init_out("emsg");
836 1 av_dict_set(&opts, "movflags", "+frag_custom+cmaf", 0);
837 1 init_fps(1, 0, 30, 1);
838 1 mux_id3();
839 1 mux_gops(2);
840 1 av_write_frame(ctx, NULL); // Flush fragment.
841 1 mux_id3();
842 1 mux_gops(2);
843 1 finish();
844 1 close_out();
845
846 1 av_free(md5);
847 1 av_packet_free(&pkt);
848
849 1 return check_faults > 0 ? 1 : 0;
850 }
851