Libav
movenc.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 Martin Storsjo
3  *
4  * This file is part of Libav.
5  *
6  * Libav 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  * Libav 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 Libav; 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 
27 #include "libavformat/avformat.h"
28 
29 #if HAVE_UNISTD_H
30 #include <unistd.h>
31 #endif
32 
33 #if !HAVE_GETOPT
34 #include "compat/getopt.c"
35 #endif
36 
37 #define HASH_SIZE 16
38 
39 static const uint8_t h264_extradata[] = {
40  0x01, 0x4d, 0x40, 0x1e, 0xff, 0xe1, 0x00, 0x02, 0x67, 0x4d, 0x01, 0x00, 0x02, 0x68, 0xef
41 };
42 static const uint8_t aac_extradata[] = {
43  0x12, 0x10
44 };
45 
46 
47 const char *format = "mp4";
49 uint8_t iobuf[32768];
51 
53 const char *cur_name;
54 FILE* out;
56 struct AVMD5* md5;
58 
61 
62 int bframes;
63 int64_t duration;
65 int frames;
67 int64_t next_p_pts;
75 
77 
79 
80 
81 static void count_warnings(void *avcl, int level, const char *fmt, va_list vl)
82 {
83  if (level == AV_LOG_WARNING)
84  num_warnings++;
85 }
86 
87 static void init_count_warnings(void)
88 {
90  num_warnings = 0;
91 }
92 
93 static void reset_count_warnings(void)
94 {
96 }
97 
98 static int io_write(void *opaque, uint8_t *buf, int size)
99 {
100  out_size += size;
101  av_md5_update(md5, buf, size);
102  if (out)
103  fwrite(buf, 1, size, out);
104  return size;
105 }
106 
107 static int io_write_data_type(void *opaque, uint8_t *buf, int size,
108  enum AVIODataMarkerType type, int64_t time)
109 {
110  char timebuf[30], content[5] = { 0 };
111  const char *str;
112  switch (type) {
113  case AVIO_DATA_MARKER_HEADER: str = "header"; break;
114  case AVIO_DATA_MARKER_SYNC_POINT: str = "sync"; break;
115  case AVIO_DATA_MARKER_BOUNDARY_POINT: str = "boundary"; break;
116  case AVIO_DATA_MARKER_UNKNOWN: str = "unknown"; break;
117  case AVIO_DATA_MARKER_TRAILER: str = "trailer"; break;
118  }
119  if (time == AV_NOPTS_VALUE)
120  snprintf(timebuf, sizeof(timebuf), "nopts");
121  else
122  snprintf(timebuf, sizeof(timebuf), "%"PRId64, time);
123  // There can be multiple header/trailer callbacks, only log the box type
124  // for header at out_size == 0
125  if (type != AVIO_DATA_MARKER_UNKNOWN &&
126  type != AVIO_DATA_MARKER_TRAILER &&
127  (type != AVIO_DATA_MARKER_HEADER || out_size == 0) &&
128  size >= 8)
129  memcpy(content, &buf[4], 4);
130  else
131  snprintf(content, sizeof(content), "-");
132  printf("write_data len %d, time %s, type %s atom %s\n", size, timebuf, str, content);
133  return io_write(opaque, buf, size);
134 }
135 
136 static void init_out(const char *name)
137 {
138  char buf[100];
139  cur_name = name;
140  snprintf(buf, sizeof(buf), "%s.%s", cur_name, format);
141 
142  av_md5_init(md5);
143  if (write_file) {
144  out = fopen(buf, "wb");
145  if (!out)
146  perror(buf);
147  }
148  out_size = 0;
149 }
150 
151 static void close_out(void)
152 {
153  int i;
154  av_md5_final(md5, hash);
155  for (i = 0; i < HASH_SIZE; i++)
156  printf("%02x", hash[i]);
157  printf(" %d %s\n", out_size, cur_name);
158  if (out)
159  fclose(out);
160  out = NULL;
161 }
162 
163 static void check_func(int value, int line, const char *msg, ...)
164 {
165  if (!value) {
166  va_list ap;
167  va_start(ap, msg);
168  printf("%d: ", line);
169  vprintf(msg, ap);
170  printf("\n");
171  check_faults++;
172  va_end(ap);
173  }
174 }
175 #define check(value, ...) check_func(value, __LINE__, __VA_ARGS__)
176 
177 static void init_fps(int bf, int audio_preroll, int fps)
178 {
179  AVStream *st;
180  int iobuf_size = force_iobuf_size ? force_iobuf_size : sizeof(iobuf);
181  ctx = avformat_alloc_context();
182  if (!ctx)
183  exit(1);
185  if (!ctx->oformat)
186  exit(1);
187  ctx->pb = avio_alloc_context(iobuf, iobuf_size, AVIO_FLAG_WRITE, NULL, NULL, io_write, NULL);
188  if (!ctx->pb)
189  exit(1);
191  ctx->flags |= AVFMT_FLAG_BITEXACT;
192 
193  st = avformat_new_stream(ctx, NULL);
194  if (!st)
195  exit(1);
198  st->codecpar->width = 640;
199  st->codecpar->height = 480;
200  st->time_base.num = 1;
201  st->time_base.den = 30;
202  st->codecpar->extradata_size = sizeof(h264_extradata);
204  if (!st->codecpar->extradata)
205  exit(1);
206  memcpy(st->codecpar->extradata, h264_extradata, sizeof(h264_extradata));
207  video_st = st;
208 
209  st = avformat_new_stream(ctx, NULL);
210  if (!st)
211  exit(1);
214  st->codecpar->sample_rate = 44100;
215  st->codecpar->channels = 2;
216  st->time_base.num = 1;
217  st->time_base.den = 44100;
218  st->codecpar->extradata_size = sizeof(aac_extradata);
220  if (!st->codecpar->extradata)
221  exit(1);
222  memcpy(st->codecpar->extradata, aac_extradata, sizeof(aac_extradata));
223  audio_st = st;
224 
225  if (avformat_write_header(ctx, &opts) < 0)
226  exit(1);
227  av_dict_free(&opts);
228 
229  frames = 0;
230  gop_size = 30;
231  duration = video_st->time_base.den / fps;
232  audio_duration = 1024LL * audio_st->time_base.den / audio_st->codecpar->sample_rate;
233  if (audio_preroll)
234  audio_preroll = 2048LL * audio_st->time_base.den / audio_st->codecpar->sample_rate;
235 
236  bframes = bf;
237  video_dts = bframes ? -duration : 0;
238  audio_dts = -audio_preroll;
239 }
240 
241 static void init(int bf, int audio_preroll)
242 {
243  init_fps(bf, audio_preroll, 30);
244 }
245 
246 static void mux_frames(int n)
247 {
248  int end_frames = frames + n;
249  while (1) {
250  AVPacket pkt;
251  uint8_t pktdata[4];
252  av_init_packet(&pkt);
253 
254  if (av_compare_ts(audio_dts, audio_st->time_base, video_dts, video_st->time_base) < 0) {
255  pkt.dts = pkt.pts = audio_dts;
256  pkt.stream_index = 1;
257  pkt.duration = audio_duration;
259  } else {
260  if (frames == end_frames)
261  break;
262  pkt.dts = video_dts;
263  pkt.stream_index = 0;
264  pkt.duration = duration;
265  if ((frames % gop_size) == 0) {
266  pkt.flags |= AV_PKT_FLAG_KEY;
268  pkt.pts = pkt.dts + duration;
269  video_dts = pkt.pts;
270  } else {
273  pkt.pts = pkt.dts;
275  } else {
277  if (((frames + 1) % gop_size) == 0) {
278  pkt.pts = pkt.dts + duration;
279  video_dts = pkt.pts;
280  } else {
281  next_p_pts = pkt.pts = pkt.dts + 2 * duration;
282  video_dts += duration;
283  }
284  }
285  }
286  if (!bframes)
287  pkt.pts = pkt.dts;
288  if (fake_pkt_duration)
290  frames++;
291  }
292 
293  if (clear_duration)
294  pkt.duration = 0;
295  AV_WB32(pktdata, pkt.pts);
296  pkt.data = pktdata;
297  pkt.size = 4;
298  if (skip_write)
299  continue;
300  if (skip_write_audio && pkt.stream_index == 1)
301  continue;
302  if (do_interleave)
303  av_interleaved_write_frame(ctx, &pkt);
304  else
305  av_write_frame(ctx, &pkt);
306  }
307 }
308 
309 static void mux_gops(int n)
310 {
311  mux_frames(gop_size * n);
312 }
313 
314 static void skip_gops(int n)
315 {
316  skip_write = 1;
317  mux_gops(n);
318  skip_write = 0;
319 }
320 
321 static void signal_init_ts(void)
322 {
323  AVPacket pkt;
324  av_init_packet(&pkt);
325  pkt.size = 0;
326  pkt.data = NULL;
327 
328  pkt.stream_index = 0;
329  pkt.dts = video_dts;
330  pkt.pts = 0;
331  av_write_frame(ctx, &pkt);
332 
333  pkt.stream_index = 1;
334  pkt.dts = pkt.pts = audio_dts;
335  av_write_frame(ctx, &pkt);
336 }
337 
338 static void finish(void)
339 {
340  av_write_trailer(ctx);
341  av_free(ctx->pb);
343  ctx = NULL;
344 }
345 
346 static void help(void)
347 {
348  printf("movenc-test [-w]\n"
349  "-w write output into files\n");
350 }
351 
352 int main(int argc, char **argv)
353 {
354  int c;
355  uint8_t header[HASH_SIZE];
356  uint8_t content[HASH_SIZE];
357  int empty_moov_pos;
358  int prev_pos;
359 
360  for (;;) {
361  c = getopt(argc, argv, "wh");
362  if (c == -1)
363  break;
364  switch (c) {
365  case 'w':
366  write_file = 1;
367  break;
368  default:
369  case 'h':
370  help();
371  return 0;
372  }
373  }
374 
375  av_register_all();
376 
377  md5 = av_md5_alloc();
378  if (!md5)
379  return 1;
380 
381  // Write a fragmented file with an initial moov that actually contains some
382  // samples. One moov+mdat with 1 second of data and one moof+mdat with 1
383  // second of data.
384  init_out("non-empty-moov");
385  av_dict_set(&opts, "movflags", "frag_keyframe", 0);
386  init(0, 0);
387  mux_gops(2);
388  finish();
389  close_out();
390 
391  // Write a similar file, but with B-frames and audio preroll, handled
392  // via an edit list.
393  init_out("non-empty-moov-elst");
394  av_dict_set(&opts, "movflags", "frag_keyframe", 0);
395  av_dict_set(&opts, "use_editlist", "1", 0);
396  init(1, 1);
397  mux_gops(2);
398  finish();
399  close_out();
400 
401  // Use B-frames but no audio-preroll, but without an edit list.
402  // Due to avoid_negative_ts == AVFMT_AVOID_NEG_TS_MAKE_ZERO, the dts
403  // of the first audio packet is > 0, but it is set to zero since edit
404  // lists aren't used, increasing the duration of the first packet instead.
405  init_out("non-empty-moov-no-elst");
406  av_dict_set(&opts, "movflags", "frag_keyframe", 0);
407  av_dict_set(&opts, "use_editlist", "0", 0);
408  init(1, 0);
409  mux_gops(2);
410  finish();
411  close_out();
412 
413  format = "ismv";
414  // Write an ISMV, with B-frames and audio preroll.
415  init_out("ismv");
416  av_dict_set(&opts, "movflags", "frag_keyframe", 0);
417  init(1, 1);
418  mux_gops(2);
419  finish();
420  close_out();
421  format = "mp4";
422 
423  // An initial moov that doesn't contain any samples, followed by two
424  // moof+mdat pairs.
425  init_out("empty-moov");
426  av_dict_set(&opts, "movflags", "frag_keyframe+empty_moov", 0);
427  init(0, 0);
428  mux_gops(2);
429  finish();
430  close_out();
431  memcpy(content, hash, HASH_SIZE);
432 
433  // Similar to the previous one, but with input that doesn't start at
434  // pts/dts 0. avoid_negative_ts behaves in the same way as
435  // in non-empty-moov-no-elst above.
436  init_out("empty-moov-no-elst");
437  av_dict_set(&opts, "movflags", "frag_keyframe+empty_moov", 0);
438  init(1, 0);
439  mux_gops(2);
440  finish();
441  close_out();
442 
443  // Same as the previous one, but disable avoid_negative_ts (which
444  // would require using an edit list, but with empty_moov, one can't
445  // write a sensible edit list, when the start timestamps aren't known).
446  // This should trigger a warning - we check that the warning is produced.
448  init_out("empty-moov-no-elst-no-adjust");
449  av_dict_set(&opts, "movflags", "frag_keyframe+empty_moov", 0);
450  av_dict_set(&opts, "avoid_negative_ts", "0", 0);
451  init(1, 0);
452  mux_gops(2);
453  finish();
454  close_out();
455 
457  check(num_warnings > 0, "No warnings printed for unhandled start offset");
458 
459  // Verify that delay_moov produces the same as empty_moov for
460  // simple input
461  init_out("delay-moov");
462  av_dict_set(&opts, "movflags", "frag_keyframe+delay_moov", 0);
463  init(0, 0);
464  mux_gops(2);
465  finish();
466  close_out();
467  check(!memcmp(hash, content, HASH_SIZE), "delay_moov differs from empty_moov");
468 
469  // Test writing content that requires an edit list using delay_moov
470  init_out("delay-moov-elst");
471  av_dict_set(&opts, "movflags", "frag_keyframe+delay_moov", 0);
472  init(1, 1);
473  mux_gops(2);
474  finish();
475  close_out();
476 
477  // Test writing a file with one track lacking packets, with delay_moov.
478  skip_write_audio = 1;
479  init_out("delay-moov-empty-track");
480  av_dict_set(&opts, "movflags", "frag_keyframe+delay_moov", 0);
481  init(0, 0);
482  mux_gops(2);
483  // The automatic flushing shouldn't output anything, since we're still
484  // waiting for data for some tracks
485  check(out_size == 0, "delay_moov flushed prematurely");
486  // When closed (or manually flushed), all the written data should still
487  // be output.
488  finish();
489  close_out();
490  check(out_size > 0, "delay_moov didn't output anything");
491 
492  // Check that manually flushing still outputs things as expected. This
493  // produces two fragments, while the one above produces only one.
494  init_out("delay-moov-empty-track-flush");
495  av_dict_set(&opts, "movflags", "frag_custom+delay_moov", 0);
496  init(0, 0);
497  mux_gops(1);
498  av_write_frame(ctx, NULL); // Force writing the moov
499  check(out_size > 0, "No moov written");
500  av_write_frame(ctx, NULL);
501  mux_gops(1);
502  av_write_frame(ctx, NULL);
503  finish();
504  close_out();
505 
506  skip_write_audio = 0;
507 
508 
509 
510  // Verify that the header written by delay_moov when manually flushed
511  // is identical to the one by empty_moov.
512  init_out("empty-moov-header");
513  av_dict_set(&opts, "movflags", "frag_keyframe+empty_moov", 0);
514  init(0, 0);
515  close_out();
516  memcpy(header, hash, HASH_SIZE);
517  init_out("empty-moov-content");
518  mux_gops(2);
519  // Written 2 seconds of content, with an automatic flush after 1 second.
520  check(out_size > 0, "No automatic flush?");
521  empty_moov_pos = prev_pos = out_size;
522  // Manually flush the second fragment
523  av_write_frame(ctx, NULL);
524  check(out_size > prev_pos, "No second fragment flushed?");
525  prev_pos = out_size;
526  // Check that an extra flush doesn't output any more data
527  av_write_frame(ctx, NULL);
528  check(out_size == prev_pos, "More data written?");
529  close_out();
530  memcpy(content, hash, HASH_SIZE);
531  // Ignore the trailer written here
532  finish();
533 
534  init_out("delay-moov-header");
535  av_dict_set(&opts, "movflags", "frag_custom+delay_moov", 0);
536  init(0, 0);
537  check(out_size == 0, "Output written during init with delay_moov");
538  mux_gops(1); // Write 1 second of content
539  av_write_frame(ctx, NULL); // Force writing the moov
540  close_out();
541  check(!memcmp(hash, header, HASH_SIZE), "delay_moov header differs from empty_moov");
542  init_out("delay-moov-content");
543  av_write_frame(ctx, NULL); // Flush the first fragment
544  check(out_size == empty_moov_pos, "Manually flushed content differs from automatically flushed, %d vs %d", out_size, empty_moov_pos);
545  mux_gops(1); // Write the rest of the content
546  av_write_frame(ctx, NULL); // Flush the second fragment
547  close_out();
548  check(!memcmp(hash, content, HASH_SIZE), "delay_moov content differs from empty_moov");
549  finish();
550 
551 
552  // Verify that we can produce an identical second fragment without
553  // writing the first one. First write the reference fragments that
554  // we want to reproduce.
555  av_dict_set(&opts, "movflags", "frag_custom+empty_moov+dash", 0);
556  init(0, 0);
557  mux_gops(1);
558  av_write_frame(ctx, NULL); // Output the first fragment
559  init_out("empty-moov-second-frag");
560  mux_gops(1);
561  av_write_frame(ctx, NULL); // Output the second fragment
562  close_out();
563  memcpy(content, hash, HASH_SIZE);
564  finish();
565 
566  // Produce the same second fragment without actually writing the first
567  // one before.
568  av_dict_set(&opts, "movflags", "frag_custom+empty_moov+dash+frag_discont", 0);
569  av_dict_set(&opts, "fragment_index", "2", 0);
570  av_dict_set(&opts, "avoid_negative_ts", "0", 0);
571  av_dict_set(&opts, "use_editlist", "0", 0);
572  init(0, 0);
573  skip_gops(1);
574  init_out("empty-moov-second-frag-discont");
575  mux_gops(1);
576  av_write_frame(ctx, NULL); // Output the second fragment
577  close_out();
578  check(!memcmp(hash, content, HASH_SIZE), "discontinuously written fragment differs");
579  finish();
580 
581  // Produce the same thing by using delay_moov, which requires a slightly
582  // different call sequence.
583  av_dict_set(&opts, "movflags", "frag_custom+delay_moov+dash+frag_discont", 0);
584  av_dict_set(&opts, "fragment_index", "2", 0);
585  init(0, 0);
586  skip_gops(1);
587  mux_gops(1);
588  av_write_frame(ctx, NULL); // Output the moov
589  init_out("delay-moov-second-frag-discont");
590  av_write_frame(ctx, NULL); // Output the second fragment
591  close_out();
592  check(!memcmp(hash, content, HASH_SIZE), "discontinuously written fragment differs");
593  finish();
594 
595 
596  // Test discontinuously written fragments with B-frames (where the
597  // assumption of starting at pts=0 works) but not with audio preroll
598  // (which can't be guessed).
599  av_dict_set(&opts, "movflags", "frag_custom+delay_moov+dash", 0);
600  init(1, 0);
601  mux_gops(1);
602  init_out("delay-moov-elst-init");
603  av_write_frame(ctx, NULL); // Output the moov
604  close_out();
605  memcpy(header, hash, HASH_SIZE);
606  av_write_frame(ctx, NULL); // Output the first fragment
607  init_out("delay-moov-elst-second-frag");
608  mux_gops(1);
609  av_write_frame(ctx, NULL); // Output the second fragment
610  close_out();
611  memcpy(content, hash, HASH_SIZE);
612  finish();
613 
614  av_dict_set(&opts, "movflags", "frag_custom+delay_moov+dash+frag_discont", 0);
615  av_dict_set(&opts, "fragment_index", "2", 0);
616  init(1, 0);
617  skip_gops(1);
618  mux_gops(1); // Write the second fragment
619  init_out("delay-moov-elst-init-discont");
620  av_write_frame(ctx, NULL); // Output the moov
621  close_out();
622  check(!memcmp(hash, header, HASH_SIZE), "discontinuously written header differs");
623  init_out("delay-moov-elst-second-frag-discont");
624  av_write_frame(ctx, NULL); // Output the second fragment
625  close_out();
626  check(!memcmp(hash, content, HASH_SIZE), "discontinuously written fragment differs");
627  finish();
628 
629 
630  // Test discontinuously written fragments with B-frames and audio preroll,
631  // properly signaled.
632  av_dict_set(&opts, "movflags", "frag_custom+delay_moov+dash", 0);
633  init(1, 1);
634  mux_gops(1);
635  init_out("delay-moov-elst-signal-init");
636  av_write_frame(ctx, NULL); // Output the moov
637  close_out();
638  memcpy(header, hash, HASH_SIZE);
639  av_write_frame(ctx, NULL); // Output the first fragment
640  init_out("delay-moov-elst-signal-second-frag");
641  mux_gops(1);
642  av_write_frame(ctx, NULL); // Output the second fragment
643  close_out();
644  memcpy(content, hash, HASH_SIZE);
645  finish();
646 
647  av_dict_set(&opts, "movflags", "frag_custom+delay_moov+dash+frag_discont", 0);
648  av_dict_set(&opts, "fragment_index", "2", 0);
649  init(1, 1);
650  signal_init_ts();
651  skip_gops(1);
652  mux_gops(1); // Write the second fragment
653  init_out("delay-moov-elst-signal-init-discont");
654  av_write_frame(ctx, NULL); // Output the moov
655  close_out();
656  check(!memcmp(hash, header, HASH_SIZE), "discontinuously written header differs");
657  init_out("delay-moov-elst-signal-second-frag-discont");
658  av_write_frame(ctx, NULL); // Output the second fragment
659  close_out();
660  check(!memcmp(hash, content, HASH_SIZE), "discontinuously written fragment differs");
661  finish();
662 
663 
664  // Test VFR content, with sidx atoms (which declare the pts duration
665  // of a fragment, forcing overriding the start pts of the next one).
666  // Here, the fragment duration in pts is significantly different from
667  // the duration in dts. The video stream starts at dts=-10,pts=0, and
668  // the second fragment starts at dts=155,pts=156. The trun duration sum
669  // of the first fragment is 165, which also is written as
670  // baseMediaDecodeTime in the tfdt in the second fragment. The sidx for
671  // the first fragment says earliest_presentation_time = 0 and
672  // subsegment_duration = 156, which also matches the sidx in the second
673  // fragment. For the audio stream, the pts and dts durations also don't
674  // match - the input stream starts at pts=-2048, but that part is excluded
675  // by the edit list.
676  init_out("vfr");
677  av_dict_set(&opts, "movflags", "frag_keyframe+delay_moov+dash", 0);
678  init_fps(1, 1, 3);
679  mux_frames(gop_size/2);
680  duration /= 10;
681  mux_frames(gop_size/2);
682  mux_gops(1);
683  finish();
684  close_out();
685 
686  // Test VFR content, with cleared duration fields. In these cases,
687  // the muxer must guess the duration of the last packet of each
688  // fragment. As long as the framerate doesn't vary (too much) at the
689  // fragment edge, it works just fine. Additionally, when automatically
690  // cutting fragments, the muxer already know the timestamps of the next
691  // packet for one stream (in most cases the video stream), avoiding
692  // having to use guesses for that one.
694  clear_duration = 1;
695  init_out("vfr-noduration");
696  av_dict_set(&opts, "movflags", "frag_keyframe+delay_moov+dash", 0);
697  init_fps(1, 1, 3);
698  mux_frames(gop_size/2);
699  duration /= 10;
700  mux_frames(gop_size/2);
701  mux_gops(1);
702  finish();
703  close_out();
704  clear_duration = 0;
706  check(num_warnings > 0, "No warnings printed for filled in durations");
707 
708  // Test with an IO buffer size that is too small to hold a full fragment;
709  // this will cause write_data_type to be called with the type unknown.
710  force_iobuf_size = 1500;
711  init_out("large_frag");
712  av_dict_set(&opts, "movflags", "frag_keyframe+delay_moov", 0);
713  init_fps(1, 1, 3);
714  mux_gops(2);
715  finish();
716  close_out();
717  force_iobuf_size = 0;
718 
719  // Test VFR content with bframes with interleaving.
720  // Here, using av_interleaved_write_frame allows the muxer to get the
721  // fragment end durations right. We always set the packet duration to
722  // the expected, but we simulate dropped frames at one point.
723  do_interleave = 1;
724  init_out("vfr-noduration-interleave");
725  av_dict_set(&opts, "movflags", "frag_keyframe+delay_moov", 0);
726  av_dict_set(&opts, "frag_duration", "650000", 0);
727  init_fps(1, 1, 30);
728  mux_frames(gop_size/2);
729  // Pretend that the packet duration is the normal, even if
730  // we actually skip a bunch of frames. (I.e., simulate that
731  // we don't know of the framedrop in advance.)
733  duration *= 10;
734  mux_frames(1);
735  fake_pkt_duration = 0;
736  duration /= 10;
737  mux_frames(gop_size/2 - 1);
738  mux_gops(1);
739  finish();
740  close_out();
741  clear_duration = 0;
742  do_interleave = 0;
743 
744 
745  av_free(md5);
746 
747  return check_faults > 0 ? 1 : 0;
748 }
int64_t video_dts
Definition: movenc.c:60
int size
int av_interleaved_write_frame(AVFormatContext *s, AVPacket *pkt)
Write a packet to an output media file ensuring correct interleaving.
Definition: mux.c:662
int skip_write
Definition: movenc.c:69
static const uint8_t h264_extradata[]
Definition: movenc.c:39
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:252
int av_write_frame(AVFormatContext *s, AVPacket *pkt)
Write a packet to an output media file.
Definition: mux.c:470
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:130
int bframes
Definition: movenc.c:62
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3483
int num
numerator
Definition: rational.h:44
static void count_warnings(void *avcl, int level, const char *fmt, va_list vl)
Definition: movenc.c:81
int size
Definition: avcodec.h:1347
#define AVIO_FLAG_WRITE
write-only
Definition: avio.h:369
int out_size
Definition: movenc.c:55
int num_warnings
Definition: movenc.c:76
int fake_pkt_duration
Definition: movenc.c:74
enum AVPictureType last_picture
Definition: movenc.c:68
static int io_write_data_type(void *opaque, uint8_t *buf, int size, enum AVIODataMarkerType type, int64_t time)
Definition: movenc.c:107
#define AV_WB32(p, val)
Definition: intreadwrite.h:246
Trailer data, which doesn&#39;t contain actual content, but only for finalizing the output file...
Definition: avio.h:89
Format I/O context.
Definition: avformat.h:940
const char * cur_name
Definition: movenc.c:53
int64_t audio_dts
Definition: movenc.c:60
struct AVMD5 * av_md5_alloc(void)
Definition: md5.c:46
uint8_t
int width
Video only.
Definition: avcodec.h:3525
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:1364
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:2648
int64_t duration
Definition: movenc.c:63
A point in the output bytestream where a demuxer can start parsing (for non self synchronizing bytest...
Definition: avio.h:77
AVFormatContext * avformat_alloc_context(void)
Allocate an AVFormatContext.
Definition: options.c:136
struct AVMD5 * md5
Definition: movenc.c:56
static void finish(void)
Definition: movenc.c:338
int flags
Flags modifying the (de)muxer behaviour.
Definition: avformat.h:1051
uint8_t * data
Definition: avcodec.h:1346
static void reset_count_warnings(void)
Definition: movenc.c:93
Definition: md5.c:40
#define AVFMT_FLAG_BITEXACT
When muxing, try to avoid writing any random/volatile data to the output.
Definition: avformat.h:1068
struct AVOutputFormat * oformat
The output container format.
Definition: avformat.h:959
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1378
uint8_t hash[HASH_SIZE]
Definition: movenc.c:57
uint8_t iobuf[32768]
Definition: movenc.c:49
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.
Definition: aviobuf.c:151
void av_md5_update(AVMD5 *ctx, const uint8_t *src, const int len)
Definition: md5.c:149
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:190
AVStream * video_st
Definition: movenc.c:59
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values. ...
Definition: dict.c:175
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:3479
Definition: graph2dot.c:48
void av_log_default_callback(void *avcl, int level, const char *fmt, va_list vl)
Default logging callback.
Definition: log.c:123
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1352
int av_compare_ts(int64_t ts_a, AVRational tb_a, int64_t ts_b, AVRational tb_b)
Compare 2 timestamps each in its own timebases.
Definition: mathematics.c:104
int extradata_size
Size of the extradata content in bytes.
Definition: avcodec.h:3501
int gop_size
Definition: movenc.c:66
static void init_fps(int bf, int audio_preroll, int fps)
Definition: movenc.c:177
AVDictionary * opts
Definition: movenc.c:50
#define HASH_SIZE
Definition: movenc.c:37
This is any, unlabelled data.
Definition: avio.h:84
const char * name
Definition: qsvenc.c:44
void av_log_set_callback(void(*callback)(void *, int, const char *, va_list))
Set the logging callback.
Definition: log.c:215
AVIODataMarkerType
Different data types that can be returned via the AVIO write_data_type callback.
Definition: avio.h:60
AVFormatContext * ctx
Definition: movenc.c:48
int force_iobuf_size
Definition: movenc.c:72
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:104
int frames
Definition: movenc.c:65
int write_file
Definition: movenc.c:52
int64_t next_p_pts
Definition: movenc.c:67
static void mux_gops(int n)
Definition: movenc.c:309
int64_t audio_duration
Definition: movenc.c:64
static void help(void)
Definition: movenc.c:346
Stream structure.
Definition: avformat.h:705
NULL
Definition: eval.c:55
AVIOContext * pb
I/O context.
Definition: avformat.h:982
AVStream * audio_st
Definition: movenc.c:59
static void close_out(void)
Definition: movenc.c:151
static int getopt(int argc, char *argv[], char *opts)
Definition: getopt.c:41
void av_md5_init(AVMD5 *ctx)
Definition: md5.c:139
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:68
const char * format
Definition: movenc.c:47
AVPictureType
Definition: avutil.h:259
void avformat_free_context(AVFormatContext *s)
Free an AVFormatContext and all its streams.
Definition: utils.c:2594
void av_md5_final(AVMD5 *ctx, uint8_t *dst)
Definition: md5.c:165
static void mux_frames(int n)
Definition: movenc.c:246
static void init_out(const char *name)
Definition: movenc.c:136
#define check(value,...)
Definition: movenc.c:175
int skip_write_audio
Definition: movenc.c:70
int do_interleave
Definition: movenc.c:73
uint8_t level
Definition: svq3.c:204
static void init_count_warnings(void)
Definition: movenc.c:87
int check_faults
Definition: movenc.c:78
int sample_rate
Audio only.
Definition: avcodec.h:3564
Main libavformat public API header.
int clear_duration
Definition: movenc.c:71
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.
Definition: avio.h:158
int main(int argc, char **argv)
Definition: movenc.c:352
Bi-dir predicted.
Definition: avutil.h:262
static void init(int bf, int audio_preroll)
Definition: movenc.c:241
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
Definition: avpacket.c:31
static void signal_init_ts(void)
Definition: movenc.c:321
int den
denominator
Definition: rational.h:45
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:638
static void skip_gops(int n)
Definition: movenc.c:314
A point in the output bytestream where a decoder can start decoding (i.e.
Definition: avio.h:71
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: avcodec.h:3497
int channels
Audio only.
Definition: avcodec.h:3560
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:1345
int av_write_trailer(AVFormatContext *s)
Write the stream trailer to an output media file and free the file private data.
Definition: mux.c:714
static int io_write(void *opaque, uint8_t *buf, int size)
Definition: movenc.c:98
FILE * out
Definition: movenc.c:54
AVCodecParameters * codecpar
Definition: avformat.h:831
static const uint8_t aac_extradata[]
Definition: movenc.c:42
int stream_index
Definition: avcodec.h:1348
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:742
This structure stores compressed data.
Definition: avcodec.h:1323
void av_register_all(void)
Initialize libavformat and register all the muxers, demuxers and protocols.
Definition: allformats.c:44
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:211
static void check_func(int value, int line, const char *msg,...)
Definition: movenc.c:163
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1339
Header data; this needs to be present for the stream to be decodeable.
Definition: avio.h:64
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:235
Predicted.
Definition: avutil.h:261