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.

774 lines
24KB

  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. #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. static 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, int c)
  215. {
  216. int end_frames = frames + n;
  217. while (1) {
  218. AVPacket pkt;
  219. uint8_t pktdata[8] = { 0 };
  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 + 4, pkt.pts);
  262. pkt.data = pktdata;
  263. pkt.size = 8;
  264. if (skip_write)
  265. continue;
  266. if (skip_write_audio && pkt.stream_index == 1)
  267. continue;
  268. if (c) {
  269. pkt.pts += (1LL<<32);
  270. pkt.dts += (1LL<<32);
  271. }
  272. if (do_interleave)
  273. av_interleaved_write_frame(ctx, &pkt);
  274. else
  275. av_write_frame(ctx, &pkt);
  276. }
  277. }
  278. static void mux_gops(int n)
  279. {
  280. mux_frames(gop_size * n, 0);
  281. }
  282. static void skip_gops(int n)
  283. {
  284. skip_write = 1;
  285. mux_gops(n);
  286. skip_write = 0;
  287. }
  288. static void signal_init_ts(void)
  289. {
  290. AVPacket pkt;
  291. av_init_packet(&pkt);
  292. pkt.size = 0;
  293. pkt.data = NULL;
  294. pkt.stream_index = 0;
  295. pkt.dts = video_dts;
  296. pkt.pts = 0;
  297. av_write_frame(ctx, &pkt);
  298. pkt.stream_index = 1;
  299. pkt.dts = pkt.pts = audio_dts;
  300. av_write_frame(ctx, &pkt);
  301. }
  302. static void finish(void)
  303. {
  304. av_write_trailer(ctx);
  305. av_free(ctx->pb);
  306. avformat_free_context(ctx);
  307. ctx = NULL;
  308. }
  309. static void help(void)
  310. {
  311. printf("movenc-test [-w]\n"
  312. "-w write output into files\n");
  313. }
  314. int main(int argc, char **argv)
  315. {
  316. int c;
  317. uint8_t header[HASH_SIZE];
  318. uint8_t content[HASH_SIZE];
  319. int empty_moov_pos;
  320. int prev_pos;
  321. for (;;) {
  322. c = getopt(argc, argv, "wh");
  323. if (c == -1)
  324. break;
  325. switch (c) {
  326. case 'w':
  327. write_file = 1;
  328. break;
  329. default:
  330. case 'h':
  331. help();
  332. return 0;
  333. }
  334. }
  335. av_register_all();
  336. md5 = av_md5_alloc();
  337. if (!md5)
  338. return 1;
  339. // Write a fragmented file with an initial moov that actually contains some
  340. // samples. One moov+mdat with 1 second of data and one moof+mdat with 1
  341. // second of data.
  342. init_out("non-empty-moov");
  343. av_dict_set(&opts, "movflags", "frag_keyframe", 0);
  344. init(0, 0);
  345. mux_gops(2);
  346. finish();
  347. close_out();
  348. // Write a similar file, but with B-frames and audio preroll, handled
  349. // via an edit list.
  350. init_out("non-empty-moov-elst");
  351. av_dict_set(&opts, "movflags", "frag_keyframe", 0);
  352. av_dict_set(&opts, "use_editlist", "1", 0);
  353. init(1, 1);
  354. mux_gops(2);
  355. finish();
  356. close_out();
  357. // Use B-frames but no audio-preroll, but without an edit list.
  358. // Due to avoid_negative_ts == AVFMT_AVOID_NEG_TS_MAKE_ZERO, the dts
  359. // of the first audio packet is > 0, but it is set to zero since edit
  360. // lists aren't used, increasing the duration of the first packet instead.
  361. init_out("non-empty-moov-no-elst");
  362. av_dict_set(&opts, "movflags", "frag_keyframe", 0);
  363. av_dict_set(&opts, "use_editlist", "0", 0);
  364. init(1, 0);
  365. mux_gops(2);
  366. finish();
  367. close_out();
  368. format = "ismv";
  369. // Write an ISMV, with B-frames and audio preroll.
  370. init_out("ismv");
  371. av_dict_set(&opts, "movflags", "frag_keyframe", 0);
  372. init(1, 1);
  373. mux_gops(2);
  374. finish();
  375. close_out();
  376. format = "mp4";
  377. // An initial moov that doesn't contain any samples, followed by two
  378. // moof+mdat pairs.
  379. init_out("empty-moov");
  380. av_dict_set(&opts, "movflags", "frag_keyframe+empty_moov", 0);
  381. av_dict_set(&opts, "use_editlist", "0", 0);
  382. init(0, 0);
  383. mux_gops(2);
  384. finish();
  385. close_out();
  386. memcpy(content, hash, HASH_SIZE);
  387. // Similar to the previous one, but with input that doesn't start at
  388. // pts/dts 0. avoid_negative_ts behaves in the same way as
  389. // in non-empty-moov-no-elst above.
  390. init_out("empty-moov-no-elst");
  391. av_dict_set(&opts, "movflags", "frag_keyframe+empty_moov", 0);
  392. init(1, 0);
  393. mux_gops(2);
  394. finish();
  395. close_out();
  396. // Same as the previous one, but disable avoid_negative_ts (which
  397. // would require using an edit list, but with empty_moov, one can't
  398. // write a sensible edit list, when the start timestamps aren't known).
  399. // This should trigger a warning - we check that the warning is produced.
  400. init_count_warnings();
  401. init_out("empty-moov-no-elst-no-adjust");
  402. av_dict_set(&opts, "movflags", "frag_keyframe+empty_moov", 0);
  403. av_dict_set(&opts, "avoid_negative_ts", "0", 0);
  404. init(1, 0);
  405. mux_gops(2);
  406. finish();
  407. close_out();
  408. reset_count_warnings();
  409. check(num_warnings > 0, "No warnings printed for unhandled start offset");
  410. // Verify that delay_moov produces the same as empty_moov for
  411. // simple input
  412. init_out("delay-moov");
  413. av_dict_set(&opts, "movflags", "frag_keyframe+delay_moov", 0);
  414. av_dict_set(&opts, "use_editlist", "0", 0);
  415. init(0, 0);
  416. mux_gops(2);
  417. finish();
  418. close_out();
  419. check(!memcmp(hash, content, HASH_SIZE), "delay_moov differs from empty_moov");
  420. // Test writing content that requires an edit list using delay_moov
  421. init_out("delay-moov-elst");
  422. av_dict_set(&opts, "movflags", "frag_keyframe+delay_moov", 0);
  423. init(1, 1);
  424. mux_gops(2);
  425. finish();
  426. close_out();
  427. // Test writing a file with one track lacking packets, with delay_moov.
  428. skip_write_audio = 1;
  429. init_out("delay-moov-empty-track");
  430. av_dict_set(&opts, "movflags", "frag_keyframe+delay_moov", 0);
  431. init(0, 0);
  432. mux_gops(2);
  433. // The automatic flushing shouldn't output anything, since we're still
  434. // waiting for data for some tracks
  435. check(out_size == 0, "delay_moov flushed prematurely");
  436. // When closed (or manually flushed), all the written data should still
  437. // be output.
  438. finish();
  439. close_out();
  440. check(out_size > 0, "delay_moov didn't output anything");
  441. // Check that manually flushing still outputs things as expected. This
  442. // produces two fragments, while the one above produces only one.
  443. init_out("delay-moov-empty-track-flush");
  444. av_dict_set(&opts, "movflags", "frag_custom+delay_moov", 0);
  445. init(0, 0);
  446. mux_gops(1);
  447. av_write_frame(ctx, NULL); // Force writing the moov
  448. check(out_size > 0, "No moov written");
  449. av_write_frame(ctx, NULL);
  450. mux_gops(1);
  451. av_write_frame(ctx, NULL);
  452. finish();
  453. close_out();
  454. skip_write_audio = 0;
  455. // Verify that the header written by delay_moov when manually flushed
  456. // is identical to the one by empty_moov.
  457. init_out("empty-moov-header");
  458. av_dict_set(&opts, "movflags", "frag_keyframe+empty_moov", 0);
  459. av_dict_set(&opts, "use_editlist", "0", 0);
  460. init(0, 0);
  461. close_out();
  462. memcpy(header, hash, HASH_SIZE);
  463. init_out("empty-moov-content");
  464. mux_gops(2);
  465. // Written 2 seconds of content, with an automatic flush after 1 second.
  466. check(out_size > 0, "No automatic flush?");
  467. empty_moov_pos = prev_pos = out_size;
  468. // Manually flush the second fragment
  469. av_write_frame(ctx, NULL);
  470. check(out_size > prev_pos, "No second fragment flushed?");
  471. prev_pos = out_size;
  472. // Check that an extra flush doesn't output any more data
  473. av_write_frame(ctx, NULL);
  474. check(out_size == prev_pos, "More data written?");
  475. close_out();
  476. memcpy(content, hash, HASH_SIZE);
  477. // Ignore the trailer written here
  478. finish();
  479. init_out("delay-moov-header");
  480. av_dict_set(&opts, "movflags", "frag_custom+delay_moov", 0);
  481. av_dict_set(&opts, "use_editlist", "0", 0);
  482. init(0, 0);
  483. check(out_size == 0, "Output written during init with delay_moov");
  484. mux_gops(1); // Write 1 second of content
  485. av_write_frame(ctx, NULL); // Force writing the moov
  486. close_out();
  487. check(!memcmp(hash, header, HASH_SIZE), "delay_moov header differs from empty_moov");
  488. init_out("delay-moov-content");
  489. av_write_frame(ctx, NULL); // Flush the first fragment
  490. check(out_size == empty_moov_pos, "Manually flushed content differs from automatically flushed, %d vs %d", out_size, empty_moov_pos);
  491. mux_gops(1); // Write the rest of the content
  492. av_write_frame(ctx, NULL); // Flush the second fragment
  493. close_out();
  494. check(!memcmp(hash, content, HASH_SIZE), "delay_moov content differs from empty_moov");
  495. finish();
  496. // Verify that we can produce an identical second fragment without
  497. // writing the first one. First write the reference fragments that
  498. // we want to reproduce.
  499. av_dict_set(&opts, "movflags", "frag_custom+empty_moov+dash", 0);
  500. init(0, 0);
  501. mux_gops(1);
  502. av_write_frame(ctx, NULL); // Output the first fragment
  503. init_out("empty-moov-second-frag");
  504. mux_gops(1);
  505. av_write_frame(ctx, NULL); // Output the second fragment
  506. close_out();
  507. memcpy(content, hash, HASH_SIZE);
  508. finish();
  509. // Produce the same second fragment without actually writing the first
  510. // one before.
  511. av_dict_set(&opts, "movflags", "frag_custom+empty_moov+dash+frag_discont", 0);
  512. av_dict_set(&opts, "fragment_index", "2", 0);
  513. av_dict_set(&opts, "avoid_negative_ts", "0", 0);
  514. av_dict_set(&opts, "use_editlist", "0", 0);
  515. init(0, 0);
  516. skip_gops(1);
  517. init_out("empty-moov-second-frag-discont");
  518. mux_gops(1);
  519. av_write_frame(ctx, NULL); // Output the second fragment
  520. close_out();
  521. check(!memcmp(hash, content, HASH_SIZE), "discontinuously written fragment differs");
  522. finish();
  523. // Produce the same thing by using delay_moov, which requires a slightly
  524. // different call sequence.
  525. av_dict_set(&opts, "movflags", "frag_custom+delay_moov+dash+frag_discont", 0);
  526. av_dict_set(&opts, "fragment_index", "2", 0);
  527. init(0, 0);
  528. skip_gops(1);
  529. mux_gops(1);
  530. av_write_frame(ctx, NULL); // Output the moov
  531. init_out("delay-moov-second-frag-discont");
  532. av_write_frame(ctx, NULL); // Output the second fragment
  533. close_out();
  534. check(!memcmp(hash, content, HASH_SIZE), "discontinuously written fragment differs");
  535. finish();
  536. // Test discontinuously written fragments with B-frames (where the
  537. // assumption of starting at pts=0 works) but not with audio preroll
  538. // (which can't be guessed).
  539. av_dict_set(&opts, "movflags", "frag_custom+delay_moov+dash", 0);
  540. init(1, 0);
  541. mux_gops(1);
  542. init_out("delay-moov-elst-init");
  543. av_write_frame(ctx, NULL); // Output the moov
  544. close_out();
  545. memcpy(header, hash, HASH_SIZE);
  546. av_write_frame(ctx, NULL); // Output the first fragment
  547. init_out("delay-moov-elst-second-frag");
  548. mux_gops(1);
  549. av_write_frame(ctx, NULL); // Output the second fragment
  550. close_out();
  551. memcpy(content, hash, HASH_SIZE);
  552. finish();
  553. av_dict_set(&opts, "movflags", "frag_custom+delay_moov+dash+frag_discont", 0);
  554. av_dict_set(&opts, "fragment_index", "2", 0);
  555. init(1, 0);
  556. skip_gops(1);
  557. mux_gops(1); // Write the second fragment
  558. init_out("delay-moov-elst-init-discont");
  559. av_write_frame(ctx, NULL); // Output the moov
  560. close_out();
  561. check(!memcmp(hash, header, HASH_SIZE), "discontinuously written header differs");
  562. init_out("delay-moov-elst-second-frag-discont");
  563. av_write_frame(ctx, NULL); // Output the second fragment
  564. close_out();
  565. check(!memcmp(hash, content, HASH_SIZE), "discontinuously written fragment differs");
  566. finish();
  567. // Test discontinuously written fragments with B-frames and audio preroll,
  568. // properly signaled.
  569. av_dict_set(&opts, "movflags", "frag_custom+delay_moov+dash", 0);
  570. init(1, 1);
  571. mux_gops(1);
  572. init_out("delay-moov-elst-signal-init");
  573. av_write_frame(ctx, NULL); // Output the moov
  574. close_out();
  575. memcpy(header, hash, HASH_SIZE);
  576. av_write_frame(ctx, NULL); // Output the first fragment
  577. init_out("delay-moov-elst-signal-second-frag");
  578. mux_gops(1);
  579. av_write_frame(ctx, NULL); // Output the second fragment
  580. close_out();
  581. memcpy(content, hash, HASH_SIZE);
  582. finish();
  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(1, 1);
  586. signal_init_ts();
  587. skip_gops(1);
  588. mux_gops(1); // Write the second fragment
  589. init_out("delay-moov-elst-signal-init-discont");
  590. av_write_frame(ctx, NULL); // Output the moov
  591. close_out();
  592. check(!memcmp(hash, header, HASH_SIZE), "discontinuously written header differs");
  593. init_out("delay-moov-elst-signal-second-frag-discont");
  594. av_write_frame(ctx, NULL); // Output the second fragment
  595. close_out();
  596. check(!memcmp(hash, content, HASH_SIZE), "discontinuously written fragment differs");
  597. finish();
  598. // Test muxing discontinuous fragments with very large (> (1<<31)) timestamps.
  599. av_dict_set(&opts, "movflags", "frag_custom+delay_moov+dash+frag_discont", 0);
  600. av_dict_set(&opts, "fragment_index", "2", 0);
  601. init(1, 1);
  602. signal_init_ts();
  603. skip_gops(1);
  604. mux_frames(gop_size, 1); // Write the second fragment
  605. init_out("delay-moov-elst-signal-init-discont-largets");
  606. av_write_frame(ctx, NULL); // Output the moov
  607. close_out();
  608. init_out("delay-moov-elst-signal-second-frag-discont-largets");
  609. av_write_frame(ctx, NULL); // Output the second fragment
  610. close_out();
  611. finish();
  612. // Test VFR content, with sidx atoms (which declare the pts duration
  613. // of a fragment, forcing overriding the start pts of the next one).
  614. // Here, the fragment duration in pts is significantly different from
  615. // the duration in dts. The video stream starts at dts=-10,pts=0, and
  616. // the second fragment starts at dts=155,pts=156. The trun duration sum
  617. // of the first fragment is 165, which also is written as
  618. // baseMediaDecodeTime in the tfdt in the second fragment. The sidx for
  619. // the first fragment says earliest_presentation_time = 0 and
  620. // subsegment_duration = 156, which also matches the sidx in the second
  621. // fragment. For the audio stream, the pts and dts durations also don't
  622. // match - the input stream starts at pts=-2048, but that part is excluded
  623. // by the edit list.
  624. init_out("vfr");
  625. av_dict_set(&opts, "movflags", "frag_keyframe+delay_moov+dash", 0);
  626. init_fps(1, 1, 3);
  627. mux_frames(gop_size/2, 0);
  628. duration /= 10;
  629. mux_frames(gop_size/2, 0);
  630. mux_gops(1);
  631. finish();
  632. close_out();
  633. // Test VFR content, with cleared duration fields. In these cases,
  634. // the muxer must guess the duration of the last packet of each
  635. // fragment. As long as the framerate doesn't vary (too much) at the
  636. // fragment edge, it works just fine. Additionally, when automatically
  637. // cutting fragments, the muxer already know the timestamps of the next
  638. // packet for one stream (in most cases the video stream), avoiding
  639. // having to use guesses for that one.
  640. init_count_warnings();
  641. clear_duration = 1;
  642. init_out("vfr-noduration");
  643. av_dict_set(&opts, "movflags", "frag_keyframe+delay_moov+dash", 0);
  644. init_fps(1, 1, 3);
  645. mux_frames(gop_size/2, 0);
  646. duration /= 10;
  647. mux_frames(gop_size/2, 0);
  648. mux_gops(1);
  649. finish();
  650. close_out();
  651. clear_duration = 0;
  652. reset_count_warnings();
  653. check(num_warnings > 0, "No warnings printed for filled in durations");
  654. // Test with an IO buffer size that is too small to hold a full fragment;
  655. // this will cause write_data_type to be called with the type unknown.
  656. force_iobuf_size = 1500;
  657. init_out("large_frag");
  658. av_dict_set(&opts, "movflags", "frag_keyframe+delay_moov", 0);
  659. init_fps(1, 1, 3);
  660. mux_gops(2);
  661. finish();
  662. close_out();
  663. force_iobuf_size = 0;
  664. // Test VFR content with bframes with interleaving.
  665. // Here, using av_interleaved_write_frame allows the muxer to get the
  666. // fragment end durations right. We always set the packet duration to
  667. // the expected, but we simulate dropped frames at one point.
  668. do_interleave = 1;
  669. init_out("vfr-noduration-interleave");
  670. av_dict_set(&opts, "movflags", "frag_keyframe+delay_moov", 0);
  671. av_dict_set(&opts, "frag_duration", "650000", 0);
  672. init_fps(1, 1, 30);
  673. mux_frames(gop_size/2, 0);
  674. // Pretend that the packet duration is the normal, even if
  675. // we actually skip a bunch of frames. (I.e., simulate that
  676. // we don't know of the framedrop in advance.)
  677. fake_pkt_duration = duration;
  678. duration *= 10;
  679. mux_frames(1, 0);
  680. fake_pkt_duration = 0;
  681. duration /= 10;
  682. mux_frames(gop_size/2 - 1, 0);
  683. mux_gops(1);
  684. finish();
  685. close_out();
  686. clear_duration = 0;
  687. do_interleave = 0;
  688. av_free(md5);
  689. return check_faults > 0 ? 1 : 0;
  690. }