You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

749 lines
23KB

  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. #include "config.h"
  21. #include "libavutil/intreadwrite.h"
  22. #include "libavutil/mathematics.h"
  23. #include "libavutil/md5.h"
  24. #include "libavformat/avformat.h"
  25. #if HAVE_UNISTD_H
  26. #include <unistd.h>
  27. #endif
  28. #if !HAVE_GETOPT
  29. #include "compat/getopt.c"
  30. #endif
  31. #define HASH_SIZE 16
  32. static const uint8_t h264_extradata[] = {
  33. 0x01, 0x4d, 0x40, 0x1e, 0xff, 0xe1, 0x00, 0x02, 0x67, 0x4d, 0x01, 0x00, 0x02, 0x68, 0xef
  34. };
  35. static const uint8_t aac_extradata[] = {
  36. 0x12, 0x10
  37. };
  38. const char *format = "mp4";
  39. AVFormatContext *ctx;
  40. uint8_t iobuf[32768];
  41. AVDictionary *opts;
  42. int write_file;
  43. const char *cur_name;
  44. FILE* out;
  45. int out_size;
  46. struct AVMD5* md5;
  47. uint8_t hash[HASH_SIZE];
  48. AVStream *video_st, *audio_st;
  49. int64_t audio_dts, video_dts;
  50. int bframes;
  51. int64_t duration;
  52. int64_t audio_duration;
  53. int frames;
  54. int gop_size;
  55. int64_t next_p_pts;
  56. enum AVPictureType last_picture;
  57. int skip_write;
  58. int skip_write_audio;
  59. int clear_duration;
  60. int force_iobuf_size;
  61. int do_interleave;
  62. int fake_pkt_duration;
  63. int num_warnings;
  64. int check_faults;
  65. static void count_warnings(void *avcl, int level, const char *fmt, va_list vl)
  66. {
  67. if (level == AV_LOG_WARNING)
  68. num_warnings++;
  69. }
  70. static void init_count_warnings(void)
  71. {
  72. av_log_set_callback(count_warnings);
  73. num_warnings = 0;
  74. }
  75. static void reset_count_warnings(void)
  76. {
  77. av_log_set_callback(av_log_default_callback);
  78. }
  79. static int io_write(void *opaque, uint8_t *buf, int size)
  80. {
  81. out_size += size;
  82. av_md5_update(md5, buf, size);
  83. if (out)
  84. fwrite(buf, 1, size, out);
  85. return size;
  86. }
  87. static int io_write_data_type(void *opaque, uint8_t *buf, int size,
  88. enum AVIODataMarkerType type, int64_t time)
  89. {
  90. char timebuf[30], content[5] = { 0 };
  91. const char *str;
  92. switch (type) {
  93. case AVIO_DATA_MARKER_HEADER: str = "header"; break;
  94. case AVIO_DATA_MARKER_SYNC_POINT: str = "sync"; break;
  95. case AVIO_DATA_MARKER_BOUNDARY_POINT: str = "boundary"; break;
  96. case AVIO_DATA_MARKER_UNKNOWN: str = "unknown"; break;
  97. case AVIO_DATA_MARKER_TRAILER: str = "trailer"; break;
  98. }
  99. if (time == AV_NOPTS_VALUE)
  100. snprintf(timebuf, sizeof(timebuf), "nopts");
  101. else
  102. snprintf(timebuf, sizeof(timebuf), "%"PRId64, time);
  103. // There can be multiple header/trailer callbacks, only log the box type
  104. // for header at out_size == 0
  105. if (type != AVIO_DATA_MARKER_UNKNOWN &&
  106. type != AVIO_DATA_MARKER_TRAILER &&
  107. (type != AVIO_DATA_MARKER_HEADER || out_size == 0) &&
  108. size >= 8)
  109. memcpy(content, &buf[4], 4);
  110. else
  111. snprintf(content, sizeof(content), "-");
  112. printf("write_data len %d, time %s, type %s atom %s\n", size, timebuf, str, content);
  113. return io_write(opaque, buf, size);
  114. }
  115. static void init_out(const char *name)
  116. {
  117. char buf[100];
  118. cur_name = name;
  119. snprintf(buf, sizeof(buf), "%s.%s", cur_name, format);
  120. av_md5_init(md5);
  121. if (write_file) {
  122. out = fopen(buf, "wb");
  123. if (!out)
  124. perror(buf);
  125. }
  126. out_size = 0;
  127. }
  128. static void close_out(void)
  129. {
  130. int i;
  131. av_md5_final(md5, hash);
  132. for (i = 0; i < HASH_SIZE; i++)
  133. printf("%02x", hash[i]);
  134. printf(" %d %s\n", out_size, cur_name);
  135. if (out)
  136. fclose(out);
  137. out = NULL;
  138. }
  139. static void check_func(int value, int line, const char *msg, ...)
  140. {
  141. if (!value) {
  142. va_list ap;
  143. va_start(ap, msg);
  144. printf("%d: ", line);
  145. vprintf(msg, ap);
  146. printf("\n");
  147. check_faults++;
  148. va_end(ap);
  149. }
  150. }
  151. #define check(value, ...) check_func(value, __LINE__, __VA_ARGS__)
  152. static void init_fps(int bf, int audio_preroll, int fps)
  153. {
  154. AVStream *st;
  155. int iobuf_size = force_iobuf_size ? force_iobuf_size : sizeof(iobuf);
  156. ctx = avformat_alloc_context();
  157. if (!ctx)
  158. exit(1);
  159. ctx->oformat = av_guess_format(format, NULL, NULL);
  160. if (!ctx->oformat)
  161. exit(1);
  162. ctx->pb = avio_alloc_context(iobuf, iobuf_size, AVIO_FLAG_WRITE, NULL, NULL, io_write, NULL);
  163. if (!ctx->pb)
  164. exit(1);
  165. ctx->pb->write_data_type = io_write_data_type;
  166. ctx->flags |= AVFMT_FLAG_BITEXACT;
  167. st = avformat_new_stream(ctx, NULL);
  168. if (!st)
  169. exit(1);
  170. st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
  171. st->codecpar->codec_id = AV_CODEC_ID_H264;
  172. st->codecpar->width = 640;
  173. st->codecpar->height = 480;
  174. st->time_base.num = 1;
  175. st->time_base.den = 30;
  176. st->codecpar->extradata_size = sizeof(h264_extradata);
  177. st->codecpar->extradata = av_mallocz(st->codecpar->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
  178. if (!st->codecpar->extradata)
  179. exit(1);
  180. memcpy(st->codecpar->extradata, h264_extradata, sizeof(h264_extradata));
  181. video_st = st;
  182. st = avformat_new_stream(ctx, NULL);
  183. if (!st)
  184. exit(1);
  185. st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
  186. st->codecpar->codec_id = AV_CODEC_ID_AAC;
  187. st->codecpar->sample_rate = 44100;
  188. st->codecpar->channels = 2;
  189. st->time_base.num = 1;
  190. st->time_base.den = 44100;
  191. st->codecpar->extradata_size = sizeof(aac_extradata);
  192. st->codecpar->extradata = av_mallocz(st->codecpar->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
  193. if (!st->codecpar->extradata)
  194. exit(1);
  195. memcpy(st->codecpar->extradata, aac_extradata, sizeof(aac_extradata));
  196. audio_st = st;
  197. if (avformat_write_header(ctx, &opts) < 0)
  198. exit(1);
  199. av_dict_free(&opts);
  200. frames = 0;
  201. gop_size = 30;
  202. duration = video_st->time_base.den / fps;
  203. audio_duration = 1024LL * audio_st->time_base.den / audio_st->codecpar->sample_rate;
  204. if (audio_preroll)
  205. audio_preroll = 2048LL * audio_st->time_base.den / audio_st->codecpar->sample_rate;
  206. bframes = bf;
  207. video_dts = bframes ? -duration : 0;
  208. audio_dts = -audio_preroll;
  209. }
  210. static void init(int bf, int audio_preroll)
  211. {
  212. init_fps(bf, audio_preroll, 30);
  213. }
  214. static void mux_frames(int n)
  215. {
  216. int end_frames = frames + n;
  217. while (1) {
  218. AVPacket pkt;
  219. uint8_t pktdata[4];
  220. av_init_packet(&pkt);
  221. if (av_compare_ts(audio_dts, audio_st->time_base, video_dts, video_st->time_base) < 0) {
  222. pkt.dts = pkt.pts = audio_dts;
  223. pkt.stream_index = 1;
  224. pkt.duration = audio_duration;
  225. audio_dts += audio_duration;
  226. } else {
  227. if (frames == end_frames)
  228. break;
  229. pkt.dts = video_dts;
  230. pkt.stream_index = 0;
  231. pkt.duration = duration;
  232. if ((frames % gop_size) == 0) {
  233. pkt.flags |= AV_PKT_FLAG_KEY;
  234. last_picture = AV_PICTURE_TYPE_I;
  235. pkt.pts = pkt.dts + duration;
  236. video_dts = pkt.pts;
  237. } else {
  238. if (last_picture == AV_PICTURE_TYPE_P) {
  239. last_picture = AV_PICTURE_TYPE_B;
  240. pkt.pts = pkt.dts;
  241. video_dts = next_p_pts;
  242. } else {
  243. last_picture = AV_PICTURE_TYPE_P;
  244. if (((frames + 1) % gop_size) == 0) {
  245. pkt.pts = pkt.dts + duration;
  246. video_dts = pkt.pts;
  247. } else {
  248. next_p_pts = pkt.pts = pkt.dts + 2 * duration;
  249. video_dts += duration;
  250. }
  251. }
  252. }
  253. if (!bframes)
  254. pkt.pts = pkt.dts;
  255. if (fake_pkt_duration)
  256. pkt.duration = fake_pkt_duration;
  257. frames++;
  258. }
  259. if (clear_duration)
  260. pkt.duration = 0;
  261. AV_WB32(pktdata, pkt.pts);
  262. pkt.data = pktdata;
  263. pkt.size = 4;
  264. if (skip_write)
  265. continue;
  266. if (skip_write_audio && pkt.stream_index == 1)
  267. continue;
  268. if (do_interleave)
  269. av_interleaved_write_frame(ctx, &pkt);
  270. else
  271. av_write_frame(ctx, &pkt);
  272. }
  273. }
  274. static void mux_gops(int n)
  275. {
  276. mux_frames(gop_size * n);
  277. }
  278. static void skip_gops(int n)
  279. {
  280. skip_write = 1;
  281. mux_gops(n);
  282. skip_write = 0;
  283. }
  284. static void signal_init_ts(void)
  285. {
  286. AVPacket pkt;
  287. av_init_packet(&pkt);
  288. pkt.size = 0;
  289. pkt.data = NULL;
  290. pkt.stream_index = 0;
  291. pkt.dts = video_dts;
  292. pkt.pts = 0;
  293. av_write_frame(ctx, &pkt);
  294. pkt.stream_index = 1;
  295. pkt.dts = pkt.pts = audio_dts;
  296. av_write_frame(ctx, &pkt);
  297. }
  298. static void finish(void)
  299. {
  300. av_write_trailer(ctx);
  301. avio_context_free(&ctx->pb);
  302. avformat_free_context(ctx);
  303. ctx = NULL;
  304. }
  305. static void help(void)
  306. {
  307. printf("movenc-test [-w]\n"
  308. "-w write output into files\n");
  309. }
  310. int main(int argc, char **argv)
  311. {
  312. int c;
  313. uint8_t header[HASH_SIZE];
  314. uint8_t content[HASH_SIZE];
  315. int empty_moov_pos;
  316. int prev_pos;
  317. for (;;) {
  318. c = getopt(argc, argv, "wh");
  319. if (c == -1)
  320. break;
  321. switch (c) {
  322. case 'w':
  323. write_file = 1;
  324. break;
  325. default:
  326. case 'h':
  327. help();
  328. return 0;
  329. }
  330. }
  331. av_register_all();
  332. md5 = av_md5_alloc();
  333. if (!md5)
  334. return 1;
  335. // Write a fragmented file with an initial moov that actually contains some
  336. // samples. One moov+mdat with 1 second of data and one moof+mdat with 1
  337. // second of data.
  338. init_out("non-empty-moov");
  339. av_dict_set(&opts, "movflags", "frag_keyframe", 0);
  340. init(0, 0);
  341. mux_gops(2);
  342. finish();
  343. close_out();
  344. // Write a similar file, but with B-frames and audio preroll, handled
  345. // via an edit list.
  346. init_out("non-empty-moov-elst");
  347. av_dict_set(&opts, "movflags", "frag_keyframe", 0);
  348. av_dict_set(&opts, "use_editlist", "1", 0);
  349. init(1, 1);
  350. mux_gops(2);
  351. finish();
  352. close_out();
  353. // Use B-frames but no audio-preroll, but without an edit list.
  354. // Due to avoid_negative_ts == AVFMT_AVOID_NEG_TS_MAKE_ZERO, the dts
  355. // of the first audio packet is > 0, but it is set to zero since edit
  356. // lists aren't used, increasing the duration of the first packet instead.
  357. init_out("non-empty-moov-no-elst");
  358. av_dict_set(&opts, "movflags", "frag_keyframe", 0);
  359. av_dict_set(&opts, "use_editlist", "0", 0);
  360. init(1, 0);
  361. mux_gops(2);
  362. finish();
  363. close_out();
  364. format = "ismv";
  365. // Write an ISMV, with B-frames and audio preroll.
  366. init_out("ismv");
  367. av_dict_set(&opts, "movflags", "frag_keyframe", 0);
  368. init(1, 1);
  369. mux_gops(2);
  370. finish();
  371. close_out();
  372. format = "mp4";
  373. // An initial moov that doesn't contain any samples, followed by two
  374. // moof+mdat pairs.
  375. init_out("empty-moov");
  376. av_dict_set(&opts, "movflags", "frag_keyframe+empty_moov", 0);
  377. init(0, 0);
  378. mux_gops(2);
  379. finish();
  380. close_out();
  381. memcpy(content, hash, HASH_SIZE);
  382. // Similar to the previous one, but with input that doesn't start at
  383. // pts/dts 0. avoid_negative_ts behaves in the same way as
  384. // in non-empty-moov-no-elst above.
  385. init_out("empty-moov-no-elst");
  386. av_dict_set(&opts, "movflags", "frag_keyframe+empty_moov", 0);
  387. init(1, 0);
  388. mux_gops(2);
  389. finish();
  390. close_out();
  391. // Same as the previous one, but disable avoid_negative_ts (which
  392. // would require using an edit list, but with empty_moov, one can't
  393. // write a sensible edit list, when the start timestamps aren't known).
  394. // This should trigger a warning - we check that the warning is produced.
  395. init_count_warnings();
  396. init_out("empty-moov-no-elst-no-adjust");
  397. av_dict_set(&opts, "movflags", "frag_keyframe+empty_moov", 0);
  398. av_dict_set(&opts, "avoid_negative_ts", "0", 0);
  399. init(1, 0);
  400. mux_gops(2);
  401. finish();
  402. close_out();
  403. reset_count_warnings();
  404. check(num_warnings > 0, "No warnings printed for unhandled start offset");
  405. // Verify that delay_moov produces the same as empty_moov for
  406. // simple input
  407. init_out("delay-moov");
  408. av_dict_set(&opts, "movflags", "frag_keyframe+delay_moov", 0);
  409. init(0, 0);
  410. mux_gops(2);
  411. finish();
  412. close_out();
  413. check(!memcmp(hash, content, HASH_SIZE), "delay_moov differs from empty_moov");
  414. // Test writing content that requires an edit list using delay_moov
  415. init_out("delay-moov-elst");
  416. av_dict_set(&opts, "movflags", "frag_keyframe+delay_moov", 0);
  417. init(1, 1);
  418. mux_gops(2);
  419. finish();
  420. close_out();
  421. // Test writing a file with one track lacking packets, with delay_moov.
  422. skip_write_audio = 1;
  423. init_out("delay-moov-empty-track");
  424. av_dict_set(&opts, "movflags", "frag_keyframe+delay_moov", 0);
  425. init(0, 0);
  426. mux_gops(2);
  427. // The automatic flushing shouldn't output anything, since we're still
  428. // waiting for data for some tracks
  429. check(out_size == 0, "delay_moov flushed prematurely");
  430. // When closed (or manually flushed), all the written data should still
  431. // be output.
  432. finish();
  433. close_out();
  434. check(out_size > 0, "delay_moov didn't output anything");
  435. // Check that manually flushing still outputs things as expected. This
  436. // produces two fragments, while the one above produces only one.
  437. init_out("delay-moov-empty-track-flush");
  438. av_dict_set(&opts, "movflags", "frag_custom+delay_moov", 0);
  439. init(0, 0);
  440. mux_gops(1);
  441. av_write_frame(ctx, NULL); // Force writing the moov
  442. check(out_size > 0, "No moov written");
  443. av_write_frame(ctx, NULL);
  444. mux_gops(1);
  445. av_write_frame(ctx, NULL);
  446. finish();
  447. close_out();
  448. skip_write_audio = 0;
  449. // Verify that the header written by delay_moov when manually flushed
  450. // is identical to the one by empty_moov.
  451. init_out("empty-moov-header");
  452. av_dict_set(&opts, "movflags", "frag_keyframe+empty_moov", 0);
  453. init(0, 0);
  454. close_out();
  455. memcpy(header, hash, HASH_SIZE);
  456. init_out("empty-moov-content");
  457. mux_gops(2);
  458. // Written 2 seconds of content, with an automatic flush after 1 second.
  459. check(out_size > 0, "No automatic flush?");
  460. empty_moov_pos = prev_pos = out_size;
  461. // Manually flush the second fragment
  462. av_write_frame(ctx, NULL);
  463. check(out_size > prev_pos, "No second fragment flushed?");
  464. prev_pos = out_size;
  465. // Check that an extra flush doesn't output any more data
  466. av_write_frame(ctx, NULL);
  467. check(out_size == prev_pos, "More data written?");
  468. close_out();
  469. memcpy(content, hash, HASH_SIZE);
  470. // Ignore the trailer written here
  471. finish();
  472. init_out("delay-moov-header");
  473. av_dict_set(&opts, "movflags", "frag_custom+delay_moov", 0);
  474. init(0, 0);
  475. check(out_size == 0, "Output written during init with delay_moov");
  476. mux_gops(1); // Write 1 second of content
  477. av_write_frame(ctx, NULL); // Force writing the moov
  478. close_out();
  479. check(!memcmp(hash, header, HASH_SIZE), "delay_moov header differs from empty_moov");
  480. init_out("delay-moov-content");
  481. av_write_frame(ctx, NULL); // Flush the first fragment
  482. check(out_size == empty_moov_pos, "Manually flushed content differs from automatically flushed, %d vs %d", out_size, empty_moov_pos);
  483. mux_gops(1); // Write the rest of the content
  484. av_write_frame(ctx, NULL); // Flush the second fragment
  485. close_out();
  486. check(!memcmp(hash, content, HASH_SIZE), "delay_moov content differs from empty_moov");
  487. finish();
  488. // Verify that we can produce an identical second fragment without
  489. // writing the first one. First write the reference fragments that
  490. // we want to reproduce.
  491. av_dict_set(&opts, "movflags", "frag_custom+empty_moov+dash", 0);
  492. init(0, 0);
  493. mux_gops(1);
  494. av_write_frame(ctx, NULL); // Output the first fragment
  495. init_out("empty-moov-second-frag");
  496. mux_gops(1);
  497. av_write_frame(ctx, NULL); // Output the second fragment
  498. close_out();
  499. memcpy(content, hash, HASH_SIZE);
  500. finish();
  501. // Produce the same second fragment without actually writing the first
  502. // one before.
  503. av_dict_set(&opts, "movflags", "frag_custom+empty_moov+dash+frag_discont", 0);
  504. av_dict_set(&opts, "fragment_index", "2", 0);
  505. av_dict_set(&opts, "avoid_negative_ts", "0", 0);
  506. av_dict_set(&opts, "use_editlist", "0", 0);
  507. init(0, 0);
  508. skip_gops(1);
  509. init_out("empty-moov-second-frag-discont");
  510. mux_gops(1);
  511. av_write_frame(ctx, NULL); // Output the second fragment
  512. close_out();
  513. check(!memcmp(hash, content, HASH_SIZE), "discontinuously written fragment differs");
  514. finish();
  515. // Produce the same thing by using delay_moov, which requires a slightly
  516. // different call sequence.
  517. av_dict_set(&opts, "movflags", "frag_custom+delay_moov+dash+frag_discont", 0);
  518. av_dict_set(&opts, "fragment_index", "2", 0);
  519. init(0, 0);
  520. skip_gops(1);
  521. mux_gops(1);
  522. av_write_frame(ctx, NULL); // Output the moov
  523. init_out("delay-moov-second-frag-discont");
  524. av_write_frame(ctx, NULL); // Output the second fragment
  525. close_out();
  526. check(!memcmp(hash, content, HASH_SIZE), "discontinuously written fragment differs");
  527. finish();
  528. // Test discontinuously written fragments with B-frames (where the
  529. // assumption of starting at pts=0 works) but not with audio preroll
  530. // (which can't be guessed).
  531. av_dict_set(&opts, "movflags", "frag_custom+delay_moov+dash", 0);
  532. init(1, 0);
  533. mux_gops(1);
  534. init_out("delay-moov-elst-init");
  535. av_write_frame(ctx, NULL); // Output the moov
  536. close_out();
  537. memcpy(header, hash, HASH_SIZE);
  538. av_write_frame(ctx, NULL); // Output the first fragment
  539. init_out("delay-moov-elst-second-frag");
  540. mux_gops(1);
  541. av_write_frame(ctx, NULL); // Output the second fragment
  542. close_out();
  543. memcpy(content, hash, HASH_SIZE);
  544. finish();
  545. av_dict_set(&opts, "movflags", "frag_custom+delay_moov+dash+frag_discont", 0);
  546. av_dict_set(&opts, "fragment_index", "2", 0);
  547. init(1, 0);
  548. skip_gops(1);
  549. mux_gops(1); // Write the second fragment
  550. init_out("delay-moov-elst-init-discont");
  551. av_write_frame(ctx, NULL); // Output the moov
  552. close_out();
  553. check(!memcmp(hash, header, HASH_SIZE), "discontinuously written header differs");
  554. init_out("delay-moov-elst-second-frag-discont");
  555. av_write_frame(ctx, NULL); // Output the second fragment
  556. close_out();
  557. check(!memcmp(hash, content, HASH_SIZE), "discontinuously written fragment differs");
  558. finish();
  559. // Test discontinuously written fragments with B-frames and audio preroll,
  560. // properly signaled.
  561. av_dict_set(&opts, "movflags", "frag_custom+delay_moov+dash", 0);
  562. init(1, 1);
  563. mux_gops(1);
  564. init_out("delay-moov-elst-signal-init");
  565. av_write_frame(ctx, NULL); // Output the moov
  566. close_out();
  567. memcpy(header, hash, HASH_SIZE);
  568. av_write_frame(ctx, NULL); // Output the first fragment
  569. init_out("delay-moov-elst-signal-second-frag");
  570. mux_gops(1);
  571. av_write_frame(ctx, NULL); // Output the second fragment
  572. close_out();
  573. memcpy(content, hash, HASH_SIZE);
  574. finish();
  575. av_dict_set(&opts, "movflags", "frag_custom+delay_moov+dash+frag_discont", 0);
  576. av_dict_set(&opts, "fragment_index", "2", 0);
  577. init(1, 1);
  578. signal_init_ts();
  579. skip_gops(1);
  580. mux_gops(1); // Write the second fragment
  581. init_out("delay-moov-elst-signal-init-discont");
  582. av_write_frame(ctx, NULL); // Output the moov
  583. close_out();
  584. check(!memcmp(hash, header, HASH_SIZE), "discontinuously written header differs");
  585. init_out("delay-moov-elst-signal-second-frag-discont");
  586. av_write_frame(ctx, NULL); // Output the second fragment
  587. close_out();
  588. check(!memcmp(hash, content, HASH_SIZE), "discontinuously written fragment differs");
  589. finish();
  590. // Test VFR content, with sidx atoms (which declare the pts duration
  591. // of a fragment, forcing overriding the start pts of the next one).
  592. // Here, the fragment duration in pts is significantly different from
  593. // the duration in dts. The video stream starts at dts=-10,pts=0, and
  594. // the second fragment starts at dts=155,pts=156. The trun duration sum
  595. // of the first fragment is 165, which also is written as
  596. // baseMediaDecodeTime in the tfdt in the second fragment. The sidx for
  597. // the first fragment says earliest_presentation_time = 0 and
  598. // subsegment_duration = 156, which also matches the sidx in the second
  599. // fragment. For the audio stream, the pts and dts durations also don't
  600. // match - the input stream starts at pts=-2048, but that part is excluded
  601. // by the edit list.
  602. init_out("vfr");
  603. av_dict_set(&opts, "movflags", "frag_keyframe+delay_moov+dash", 0);
  604. init_fps(1, 1, 3);
  605. mux_frames(gop_size/2);
  606. duration /= 10;
  607. mux_frames(gop_size/2);
  608. mux_gops(1);
  609. finish();
  610. close_out();
  611. // Test VFR content, with cleared duration fields. In these cases,
  612. // the muxer must guess the duration of the last packet of each
  613. // fragment. As long as the framerate doesn't vary (too much) at the
  614. // fragment edge, it works just fine. Additionally, when automatically
  615. // cutting fragments, the muxer already know the timestamps of the next
  616. // packet for one stream (in most cases the video stream), avoiding
  617. // having to use guesses for that one.
  618. init_count_warnings();
  619. clear_duration = 1;
  620. init_out("vfr-noduration");
  621. av_dict_set(&opts, "movflags", "frag_keyframe+delay_moov+dash", 0);
  622. init_fps(1, 1, 3);
  623. mux_frames(gop_size/2);
  624. duration /= 10;
  625. mux_frames(gop_size/2);
  626. mux_gops(1);
  627. finish();
  628. close_out();
  629. clear_duration = 0;
  630. reset_count_warnings();
  631. check(num_warnings > 0, "No warnings printed for filled in durations");
  632. // Test with an IO buffer size that is too small to hold a full fragment;
  633. // this will cause write_data_type to be called with the type unknown.
  634. force_iobuf_size = 1500;
  635. init_out("large_frag");
  636. av_dict_set(&opts, "movflags", "frag_keyframe+delay_moov", 0);
  637. init_fps(1, 1, 3);
  638. mux_gops(2);
  639. finish();
  640. close_out();
  641. force_iobuf_size = 0;
  642. // Test VFR content with bframes with interleaving.
  643. // Here, using av_interleaved_write_frame allows the muxer to get the
  644. // fragment end durations right. We always set the packet duration to
  645. // the expected, but we simulate dropped frames at one point.
  646. do_interleave = 1;
  647. init_out("vfr-noduration-interleave");
  648. av_dict_set(&opts, "movflags", "frag_keyframe+delay_moov", 0);
  649. av_dict_set(&opts, "frag_duration", "650000", 0);
  650. init_fps(1, 1, 30);
  651. mux_frames(gop_size/2);
  652. // Pretend that the packet duration is the normal, even if
  653. // we actually skip a bunch of frames. (I.e., simulate that
  654. // we don't know of the framedrop in advance.)
  655. fake_pkt_duration = duration;
  656. duration *= 10;
  657. mux_frames(1);
  658. fake_pkt_duration = 0;
  659. duration /= 10;
  660. mux_frames(gop_size/2 - 1);
  661. mux_gops(1);
  662. finish();
  663. close_out();
  664. clear_duration = 0;
  665. do_interleave = 0;
  666. av_free(md5);
  667. return check_faults > 0 ? 1 : 0;
  668. }