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.

724 lines
23KB

  1. /*
  2. * Copyright (c) 2012 Nicolas George
  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 License
  8. * 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
  14. * GNU Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with FFmpeg; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "libavutil/avassert.h"
  21. #include "libavutil/avstring.h"
  22. #include "libavutil/intreadwrite.h"
  23. #include "libavutil/opt.h"
  24. #include "libavutil/parseutils.h"
  25. #include "libavutil/timestamp.h"
  26. #include "avformat.h"
  27. #include "internal.h"
  28. #include "url.h"
  29. typedef enum ConcatMatchMode {
  30. MATCH_ONE_TO_ONE,
  31. MATCH_EXACT_ID,
  32. } ConcatMatchMode;
  33. typedef struct ConcatStream {
  34. AVBitStreamFilterContext *bsf;
  35. int out_stream_index;
  36. } ConcatStream;
  37. typedef struct {
  38. char *url;
  39. int64_t start_time;
  40. int64_t file_start_time;
  41. int64_t file_inpoint;
  42. int64_t duration;
  43. ConcatStream *streams;
  44. int64_t inpoint;
  45. int64_t outpoint;
  46. AVDictionary *metadata;
  47. int nb_streams;
  48. } ConcatFile;
  49. typedef struct {
  50. AVClass *class;
  51. ConcatFile *files;
  52. ConcatFile *cur_file;
  53. unsigned nb_files;
  54. AVFormatContext *avf;
  55. int safe;
  56. int seekable;
  57. int eof;
  58. ConcatMatchMode stream_match_mode;
  59. unsigned auto_convert;
  60. } ConcatContext;
  61. static int concat_probe(AVProbeData *probe)
  62. {
  63. return memcmp(probe->buf, "ffconcat version 1.0", 20) ?
  64. 0 : AVPROBE_SCORE_MAX;
  65. }
  66. static char *get_keyword(uint8_t **cursor)
  67. {
  68. char *ret = *cursor += strspn(*cursor, SPACE_CHARS);
  69. *cursor += strcspn(*cursor, SPACE_CHARS);
  70. if (**cursor) {
  71. *((*cursor)++) = 0;
  72. *cursor += strspn(*cursor, SPACE_CHARS);
  73. }
  74. return ret;
  75. }
  76. static int safe_filename(const char *f)
  77. {
  78. const char *start = f;
  79. for (; *f; f++) {
  80. /* A-Za-z0-9_- */
  81. if (!((unsigned)((*f | 32) - 'a') < 26 ||
  82. (unsigned)(*f - '0') < 10 || *f == '_' || *f == '-')) {
  83. if (f == start)
  84. return 0;
  85. else if (*f == '/')
  86. start = f + 1;
  87. else if (*f != '.')
  88. return 0;
  89. }
  90. }
  91. return 1;
  92. }
  93. #define FAIL(retcode) do { ret = (retcode); goto fail; } while(0)
  94. static int add_file(AVFormatContext *avf, char *filename, ConcatFile **rfile,
  95. unsigned *nb_files_alloc)
  96. {
  97. ConcatContext *cat = avf->priv_data;
  98. ConcatFile *file;
  99. char *url = NULL;
  100. const char *proto;
  101. size_t url_len, proto_len;
  102. int ret;
  103. if (cat->safe > 0 && !safe_filename(filename)) {
  104. av_log(avf, AV_LOG_ERROR, "Unsafe file name '%s'\n", filename);
  105. FAIL(AVERROR(EPERM));
  106. }
  107. proto = avio_find_protocol_name(filename);
  108. proto_len = proto ? strlen(proto) : 0;
  109. if (!memcmp(filename, proto, proto_len) &&
  110. (filename[proto_len] == ':' || filename[proto_len] == ',')) {
  111. url = filename;
  112. filename = NULL;
  113. } else {
  114. url_len = strlen(avf->filename) + strlen(filename) + 16;
  115. if (!(url = av_malloc(url_len)))
  116. FAIL(AVERROR(ENOMEM));
  117. ff_make_absolute_url(url, url_len, avf->filename, filename);
  118. av_freep(&filename);
  119. }
  120. if (cat->nb_files >= *nb_files_alloc) {
  121. size_t n = FFMAX(*nb_files_alloc * 2, 16);
  122. ConcatFile *new_files;
  123. if (n <= cat->nb_files || n > SIZE_MAX / sizeof(*cat->files) ||
  124. !(new_files = av_realloc(cat->files, n * sizeof(*cat->files))))
  125. FAIL(AVERROR(ENOMEM));
  126. cat->files = new_files;
  127. *nb_files_alloc = n;
  128. }
  129. file = &cat->files[cat->nb_files++];
  130. memset(file, 0, sizeof(*file));
  131. *rfile = file;
  132. file->url = url;
  133. file->start_time = AV_NOPTS_VALUE;
  134. file->duration = AV_NOPTS_VALUE;
  135. file->inpoint = AV_NOPTS_VALUE;
  136. file->outpoint = AV_NOPTS_VALUE;
  137. return 0;
  138. fail:
  139. av_free(url);
  140. av_free(filename);
  141. return ret;
  142. }
  143. static int copy_stream_props(AVStream *st, AVStream *source_st)
  144. {
  145. int ret;
  146. if (st->codec->codec_id || !source_st->codec->codec_id) {
  147. if (st->codec->extradata_size < source_st->codec->extradata_size) {
  148. ret = ff_alloc_extradata(st->codec,
  149. source_st->codec->extradata_size);
  150. if (ret < 0)
  151. return ret;
  152. }
  153. memcpy(st->codec->extradata, source_st->codec->extradata,
  154. source_st->codec->extradata_size);
  155. return 0;
  156. }
  157. if ((ret = avcodec_copy_context(st->codec, source_st->codec)) < 0)
  158. return ret;
  159. st->r_frame_rate = source_st->r_frame_rate;
  160. st->avg_frame_rate = source_st->avg_frame_rate;
  161. st->time_base = source_st->time_base;
  162. st->sample_aspect_ratio = source_st->sample_aspect_ratio;
  163. av_dict_copy(&st->metadata, source_st->metadata, 0);
  164. return 0;
  165. }
  166. static int detect_stream_specific(AVFormatContext *avf, int idx)
  167. {
  168. ConcatContext *cat = avf->priv_data;
  169. AVStream *st = cat->avf->streams[idx];
  170. ConcatStream *cs = &cat->cur_file->streams[idx];
  171. AVBitStreamFilterContext *bsf;
  172. if (cat->auto_convert && st->codec->codec_id == AV_CODEC_ID_H264 &&
  173. (st->codec->extradata_size < 4 || AV_RB32(st->codec->extradata) != 1)) {
  174. av_log(cat->avf, AV_LOG_INFO,
  175. "Auto-inserting h264_mp4toannexb bitstream filter\n");
  176. if (!(bsf = av_bitstream_filter_init("h264_mp4toannexb"))) {
  177. av_log(avf, AV_LOG_ERROR, "h264_mp4toannexb bitstream filter "
  178. "required for H.264 streams\n");
  179. return AVERROR_BSF_NOT_FOUND;
  180. }
  181. cs->bsf = bsf;
  182. }
  183. return 0;
  184. }
  185. static int match_streams_one_to_one(AVFormatContext *avf)
  186. {
  187. ConcatContext *cat = avf->priv_data;
  188. AVStream *st;
  189. int i, ret;
  190. for (i = cat->cur_file->nb_streams; i < cat->avf->nb_streams; i++) {
  191. if (i < avf->nb_streams) {
  192. st = avf->streams[i];
  193. } else {
  194. if (!(st = avformat_new_stream(avf, NULL)))
  195. return AVERROR(ENOMEM);
  196. }
  197. if ((ret = copy_stream_props(st, cat->avf->streams[i])) < 0)
  198. return ret;
  199. cat->cur_file->streams[i].out_stream_index = i;
  200. }
  201. return 0;
  202. }
  203. static int match_streams_exact_id(AVFormatContext *avf)
  204. {
  205. ConcatContext *cat = avf->priv_data;
  206. AVStream *st;
  207. int i, j, ret;
  208. for (i = cat->cur_file->nb_streams; i < cat->avf->nb_streams; i++) {
  209. st = cat->avf->streams[i];
  210. for (j = 0; j < avf->nb_streams; j++) {
  211. if (avf->streams[j]->id == st->id) {
  212. av_log(avf, AV_LOG_VERBOSE,
  213. "Match slave stream #%d with stream #%d id 0x%x\n",
  214. i, j, st->id);
  215. if ((ret = copy_stream_props(avf->streams[j], st)) < 0)
  216. return ret;
  217. cat->cur_file->streams[i].out_stream_index = j;
  218. }
  219. }
  220. }
  221. return 0;
  222. }
  223. static int match_streams(AVFormatContext *avf)
  224. {
  225. ConcatContext *cat = avf->priv_data;
  226. ConcatStream *map;
  227. int i, ret;
  228. if (cat->cur_file->nb_streams >= cat->avf->nb_streams)
  229. return 0;
  230. map = av_realloc(cat->cur_file->streams,
  231. cat->avf->nb_streams * sizeof(*map));
  232. if (!map)
  233. return AVERROR(ENOMEM);
  234. cat->cur_file->streams = map;
  235. memset(map + cat->cur_file->nb_streams, 0,
  236. (cat->avf->nb_streams - cat->cur_file->nb_streams) * sizeof(*map));
  237. for (i = cat->cur_file->nb_streams; i < cat->avf->nb_streams; i++)
  238. map[i].out_stream_index = -1;
  239. switch (cat->stream_match_mode) {
  240. case MATCH_ONE_TO_ONE:
  241. ret = match_streams_one_to_one(avf);
  242. break;
  243. case MATCH_EXACT_ID:
  244. ret = match_streams_exact_id(avf);
  245. break;
  246. default:
  247. ret = AVERROR_BUG;
  248. }
  249. if (ret < 0)
  250. return ret;
  251. for (i = cat->cur_file->nb_streams; i < cat->avf->nb_streams; i++)
  252. if ((ret = detect_stream_specific(avf, i)) < 0)
  253. return ret;
  254. cat->cur_file->nb_streams = cat->avf->nb_streams;
  255. return 0;
  256. }
  257. static int open_file(AVFormatContext *avf, unsigned fileno)
  258. {
  259. ConcatContext *cat = avf->priv_data;
  260. ConcatFile *file = &cat->files[fileno];
  261. int ret;
  262. if (cat->avf)
  263. avformat_close_input(&cat->avf);
  264. cat->avf = avformat_alloc_context();
  265. if (!cat->avf)
  266. return AVERROR(ENOMEM);
  267. cat->avf->interrupt_callback = avf->interrupt_callback;
  268. if ((ret = ff_copy_whitelists(cat->avf, avf)) < 0)
  269. return ret;
  270. if ((ret = avformat_open_input(&cat->avf, file->url, NULL, NULL)) < 0 ||
  271. (ret = avformat_find_stream_info(cat->avf, NULL)) < 0) {
  272. av_log(avf, AV_LOG_ERROR, "Impossible to open '%s'\n", file->url);
  273. avformat_close_input(&cat->avf);
  274. return ret;
  275. }
  276. cat->cur_file = file;
  277. if (file->start_time == AV_NOPTS_VALUE)
  278. file->start_time = !fileno ? 0 :
  279. cat->files[fileno - 1].start_time +
  280. cat->files[fileno - 1].duration;
  281. file->file_start_time = (avf->start_time == AV_NOPTS_VALUE) ? 0 : avf->start_time;
  282. file->file_inpoint = (file->file_inpoint == AV_NOPTS_VALUE) ? file->file_start_time : file->inpoint;
  283. if ((ret = match_streams(avf)) < 0)
  284. return ret;
  285. if (file->inpoint != AV_NOPTS_VALUE) {
  286. if ((ret = avformat_seek_file(cat->avf, -1, INT64_MIN, file->inpoint, file->inpoint, 0)) < 0)
  287. return ret;
  288. }
  289. return 0;
  290. }
  291. static int concat_read_close(AVFormatContext *avf)
  292. {
  293. ConcatContext *cat = avf->priv_data;
  294. unsigned i;
  295. if (cat->avf)
  296. avformat_close_input(&cat->avf);
  297. for (i = 0; i < cat->nb_files; i++) {
  298. av_freep(&cat->files[i].url);
  299. av_freep(&cat->files[i].streams);
  300. av_dict_free(&cat->files[i].metadata);
  301. }
  302. av_freep(&cat->files);
  303. return 0;
  304. }
  305. static int concat_read_header(AVFormatContext *avf)
  306. {
  307. ConcatContext *cat = avf->priv_data;
  308. uint8_t buf[4096];
  309. uint8_t *cursor, *keyword;
  310. int ret, line = 0, i;
  311. unsigned nb_files_alloc = 0;
  312. ConcatFile *file = NULL;
  313. int64_t time = 0;
  314. while (1) {
  315. if ((ret = ff_get_line(avf->pb, buf, sizeof(buf))) <= 0)
  316. break;
  317. line++;
  318. cursor = buf;
  319. keyword = get_keyword(&cursor);
  320. if (!*keyword || *keyword == '#')
  321. continue;
  322. if (!strcmp(keyword, "file")) {
  323. char *filename = av_get_token((const char **)&cursor, SPACE_CHARS);
  324. if (!filename) {
  325. av_log(avf, AV_LOG_ERROR, "Line %d: filename required\n", line);
  326. FAIL(AVERROR_INVALIDDATA);
  327. }
  328. if ((ret = add_file(avf, filename, &file, &nb_files_alloc)) < 0)
  329. goto fail;
  330. } else if (!strcmp(keyword, "duration") || !strcmp(keyword, "inpoint") || !strcmp(keyword, "outpoint")) {
  331. char *dur_str = get_keyword(&cursor);
  332. int64_t dur;
  333. if (!file) {
  334. av_log(avf, AV_LOG_ERROR, "Line %d: %s without file\n",
  335. line, keyword);
  336. FAIL(AVERROR_INVALIDDATA);
  337. }
  338. if ((ret = av_parse_time(&dur, dur_str, 1)) < 0) {
  339. av_log(avf, AV_LOG_ERROR, "Line %d: invalid %s '%s'\n",
  340. line, keyword, dur_str);
  341. goto fail;
  342. }
  343. if (!strcmp(keyword, "duration"))
  344. file->duration = dur;
  345. else if (!strcmp(keyword, "inpoint"))
  346. file->inpoint = dur;
  347. else if (!strcmp(keyword, "outpoint"))
  348. file->outpoint = dur;
  349. } else if (!strcmp(keyword, "file_packet_metadata")) {
  350. char *metadata;
  351. metadata = av_get_token((const char **)&cursor, SPACE_CHARS);
  352. if (!metadata) {
  353. av_log(avf, AV_LOG_ERROR, "Line %d: packet metadata required\n", line);
  354. FAIL(AVERROR_INVALIDDATA);
  355. }
  356. if ((ret = av_dict_parse_string(&file->metadata, metadata, "=", "", 0)) < 0) {
  357. av_log(avf, AV_LOG_ERROR, "Line %d: failed to parse metadata string\n", line);
  358. av_freep(&metadata);
  359. FAIL(AVERROR_INVALIDDATA);
  360. }
  361. av_freep(&metadata);
  362. } else if (!strcmp(keyword, "stream")) {
  363. if (!avformat_new_stream(avf, NULL))
  364. FAIL(AVERROR(ENOMEM));
  365. } else if (!strcmp(keyword, "exact_stream_id")) {
  366. if (!avf->nb_streams) {
  367. av_log(avf, AV_LOG_ERROR, "Line %d: exact_stream_id without stream\n",
  368. line);
  369. FAIL(AVERROR_INVALIDDATA);
  370. }
  371. avf->streams[avf->nb_streams - 1]->id =
  372. strtol(get_keyword(&cursor), NULL, 0);
  373. } else if (!strcmp(keyword, "ffconcat")) {
  374. char *ver_kw = get_keyword(&cursor);
  375. char *ver_val = get_keyword(&cursor);
  376. if (strcmp(ver_kw, "version") || strcmp(ver_val, "1.0")) {
  377. av_log(avf, AV_LOG_ERROR, "Line %d: invalid version\n", line);
  378. FAIL(AVERROR_INVALIDDATA);
  379. }
  380. if (cat->safe < 0)
  381. cat->safe = 1;
  382. } else {
  383. av_log(avf, AV_LOG_ERROR, "Line %d: unknown keyword '%s'\n",
  384. line, keyword);
  385. FAIL(AVERROR_INVALIDDATA);
  386. }
  387. }
  388. if (ret < 0)
  389. goto fail;
  390. if (!cat->nb_files)
  391. FAIL(AVERROR_INVALIDDATA);
  392. for (i = 0; i < cat->nb_files; i++) {
  393. if (cat->files[i].start_time == AV_NOPTS_VALUE)
  394. cat->files[i].start_time = time;
  395. else
  396. time = cat->files[i].start_time;
  397. if (cat->files[i].duration == AV_NOPTS_VALUE) {
  398. if (cat->files[i].inpoint == AV_NOPTS_VALUE || cat->files[i].outpoint == AV_NOPTS_VALUE)
  399. break;
  400. cat->files[i].duration = cat->files[i].outpoint - cat->files[i].inpoint;
  401. }
  402. time += cat->files[i].duration;
  403. }
  404. if (i == cat->nb_files) {
  405. avf->duration = time;
  406. cat->seekable = 1;
  407. }
  408. cat->stream_match_mode = avf->nb_streams ? MATCH_EXACT_ID :
  409. MATCH_ONE_TO_ONE;
  410. if ((ret = open_file(avf, 0)) < 0)
  411. goto fail;
  412. return 0;
  413. fail:
  414. concat_read_close(avf);
  415. return ret;
  416. }
  417. static int open_next_file(AVFormatContext *avf)
  418. {
  419. ConcatContext *cat = avf->priv_data;
  420. unsigned fileno = cat->cur_file - cat->files;
  421. if (cat->cur_file->duration == AV_NOPTS_VALUE) {
  422. cat->cur_file->duration = cat->avf->duration;
  423. if (cat->cur_file->inpoint != AV_NOPTS_VALUE)
  424. cat->cur_file->duration -= (cat->cur_file->inpoint - cat->cur_file->file_start_time);
  425. if (cat->cur_file->outpoint != AV_NOPTS_VALUE)
  426. cat->cur_file->duration -= cat->avf->duration - (cat->cur_file->outpoint - cat->cur_file->file_start_time);
  427. }
  428. if (++fileno >= cat->nb_files) {
  429. cat->eof = 1;
  430. return AVERROR_EOF;
  431. }
  432. return open_file(avf, fileno);
  433. }
  434. static int filter_packet(AVFormatContext *avf, ConcatStream *cs, AVPacket *pkt)
  435. {
  436. AVStream *st = avf->streams[cs->out_stream_index];
  437. AVBitStreamFilterContext *bsf;
  438. AVPacket pkt2;
  439. int ret;
  440. av_assert0(cs->out_stream_index >= 0);
  441. for (bsf = cs->bsf; bsf; bsf = bsf->next) {
  442. pkt2 = *pkt;
  443. ret = av_bitstream_filter_filter(bsf, st->codec, NULL,
  444. &pkt2.data, &pkt2.size,
  445. pkt->data, pkt->size,
  446. !!(pkt->flags & AV_PKT_FLAG_KEY));
  447. if (ret < 0) {
  448. av_packet_unref(pkt);
  449. return ret;
  450. }
  451. av_assert0(pkt2.buf);
  452. if (ret == 0 && pkt2.data != pkt->data) {
  453. if ((ret = av_copy_packet(&pkt2, pkt)) < 0) {
  454. av_free(pkt2.data);
  455. return ret;
  456. }
  457. ret = 1;
  458. }
  459. if (ret > 0) {
  460. av_free_packet(pkt);
  461. pkt2.buf = av_buffer_create(pkt2.data, pkt2.size,
  462. av_buffer_default_free, NULL, 0);
  463. if (!pkt2.buf) {
  464. av_free(pkt2.data);
  465. return AVERROR(ENOMEM);
  466. }
  467. }
  468. *pkt = pkt2;
  469. }
  470. return 0;
  471. }
  472. /* Returns true if the packet dts is greater or equal to the specified outpoint. */
  473. static int packet_after_outpoint(ConcatContext *cat, AVPacket *pkt)
  474. {
  475. if (cat->cur_file->outpoint != AV_NOPTS_VALUE && pkt->dts != AV_NOPTS_VALUE) {
  476. return av_compare_ts(pkt->dts, cat->avf->streams[pkt->stream_index]->time_base,
  477. cat->cur_file->outpoint, AV_TIME_BASE_Q) >= 0;
  478. }
  479. return 0;
  480. }
  481. static int concat_read_packet(AVFormatContext *avf, AVPacket *pkt)
  482. {
  483. ConcatContext *cat = avf->priv_data;
  484. int ret;
  485. int64_t delta;
  486. ConcatStream *cs;
  487. AVStream *st;
  488. if (cat->eof)
  489. return AVERROR_EOF;
  490. if (!cat->avf)
  491. return AVERROR(EIO);
  492. while (1) {
  493. ret = av_read_frame(cat->avf, pkt);
  494. if (ret == AVERROR_EOF || packet_after_outpoint(cat, pkt)) {
  495. if (ret == 0)
  496. av_packet_unref(pkt);
  497. if ((ret = open_next_file(avf)) < 0)
  498. return ret;
  499. continue;
  500. }
  501. if (ret < 0)
  502. return ret;
  503. if ((ret = match_streams(avf)) < 0) {
  504. av_packet_unref(pkt);
  505. return ret;
  506. }
  507. cs = &cat->cur_file->streams[pkt->stream_index];
  508. if (cs->out_stream_index < 0) {
  509. av_packet_unref(pkt);
  510. continue;
  511. }
  512. pkt->stream_index = cs->out_stream_index;
  513. break;
  514. }
  515. if ((ret = filter_packet(avf, cs, pkt)))
  516. return ret;
  517. st = cat->avf->streams[pkt->stream_index];
  518. av_log(avf, AV_LOG_DEBUG, "file:%d stream:%d pts:%s pts_time:%s dts:%s dts_time:%s",
  519. (unsigned)(cat->cur_file - cat->files), pkt->stream_index,
  520. av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &st->time_base),
  521. av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &st->time_base));
  522. delta = av_rescale_q(cat->cur_file->start_time - cat->cur_file->file_inpoint,
  523. AV_TIME_BASE_Q,
  524. cat->avf->streams[pkt->stream_index]->time_base);
  525. if (pkt->pts != AV_NOPTS_VALUE)
  526. pkt->pts += delta;
  527. if (pkt->dts != AV_NOPTS_VALUE)
  528. pkt->dts += delta;
  529. av_log(avf, AV_LOG_DEBUG, " -> pts:%s pts_time:%s dts:%s dts_time:%s\n",
  530. av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &st->time_base),
  531. av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &st->time_base));
  532. if (cat->cur_file->metadata) {
  533. uint8_t* metadata;
  534. int metadata_len;
  535. char* packed_metadata = av_packet_pack_dictionary(cat->cur_file->metadata, &metadata_len);
  536. if (!packed_metadata)
  537. return AVERROR(ENOMEM);
  538. if (!(metadata = av_packet_new_side_data(pkt, AV_PKT_DATA_STRINGS_METADATA, metadata_len))) {
  539. av_freep(&packed_metadata);
  540. return AVERROR(ENOMEM);
  541. }
  542. memcpy(metadata, packed_metadata, metadata_len);
  543. av_freep(&packed_metadata);
  544. }
  545. return ret;
  546. }
  547. static void rescale_interval(AVRational tb_in, AVRational tb_out,
  548. int64_t *min_ts, int64_t *ts, int64_t *max_ts)
  549. {
  550. *ts = av_rescale_q (* ts, tb_in, tb_out);
  551. *min_ts = av_rescale_q_rnd(*min_ts, tb_in, tb_out,
  552. AV_ROUND_UP | AV_ROUND_PASS_MINMAX);
  553. *max_ts = av_rescale_q_rnd(*max_ts, tb_in, tb_out,
  554. AV_ROUND_DOWN | AV_ROUND_PASS_MINMAX);
  555. }
  556. static int try_seek(AVFormatContext *avf, int stream,
  557. int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
  558. {
  559. ConcatContext *cat = avf->priv_data;
  560. int64_t t0 = cat->cur_file->start_time - cat->cur_file->file_inpoint;
  561. ts -= t0;
  562. min_ts = min_ts == INT64_MIN ? INT64_MIN : min_ts - t0;
  563. max_ts = max_ts == INT64_MAX ? INT64_MAX : max_ts - t0;
  564. if (stream >= 0) {
  565. if (stream >= cat->avf->nb_streams)
  566. return AVERROR(EIO);
  567. rescale_interval(AV_TIME_BASE_Q, cat->avf->streams[stream]->time_base,
  568. &min_ts, &ts, &max_ts);
  569. }
  570. return avformat_seek_file(cat->avf, stream, min_ts, ts, max_ts, flags);
  571. }
  572. static int real_seek(AVFormatContext *avf, int stream,
  573. int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
  574. {
  575. ConcatContext *cat = avf->priv_data;
  576. int ret, left, right;
  577. if (stream >= 0) {
  578. if (stream >= avf->nb_streams)
  579. return AVERROR(EINVAL);
  580. rescale_interval(avf->streams[stream]->time_base, AV_TIME_BASE_Q,
  581. &min_ts, &ts, &max_ts);
  582. }
  583. left = 0;
  584. right = cat->nb_files;
  585. while (right - left > 1) {
  586. int mid = (left + right) / 2;
  587. if (ts < cat->files[mid].start_time)
  588. right = mid;
  589. else
  590. left = mid;
  591. }
  592. if ((ret = open_file(avf, left)) < 0)
  593. return ret;
  594. ret = try_seek(avf, stream, min_ts, ts, max_ts, flags);
  595. if (ret < 0 &&
  596. left < cat->nb_files - 1 &&
  597. cat->files[left + 1].start_time < max_ts) {
  598. if ((ret = open_file(avf, left + 1)) < 0)
  599. return ret;
  600. ret = try_seek(avf, stream, min_ts, ts, max_ts, flags);
  601. }
  602. return ret;
  603. }
  604. static int concat_seek(AVFormatContext *avf, int stream,
  605. int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
  606. {
  607. ConcatContext *cat = avf->priv_data;
  608. ConcatFile *cur_file_saved = cat->cur_file;
  609. AVFormatContext *cur_avf_saved = cat->avf;
  610. int ret;
  611. if (!cat->seekable)
  612. return AVERROR(ESPIPE); /* XXX: can we use it? */
  613. if (flags & (AVSEEK_FLAG_BYTE | AVSEEK_FLAG_FRAME))
  614. return AVERROR(ENOSYS);
  615. cat->avf = NULL;
  616. if ((ret = real_seek(avf, stream, min_ts, ts, max_ts, flags)) < 0) {
  617. if (cat->avf)
  618. avformat_close_input(&cat->avf);
  619. cat->avf = cur_avf_saved;
  620. cat->cur_file = cur_file_saved;
  621. } else {
  622. avformat_close_input(&cur_avf_saved);
  623. cat->eof = 0;
  624. }
  625. return ret;
  626. }
  627. #define OFFSET(x) offsetof(ConcatContext, x)
  628. #define DEC AV_OPT_FLAG_DECODING_PARAM
  629. static const AVOption options[] = {
  630. { "safe", "enable safe mode",
  631. OFFSET(safe), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 1, DEC },
  632. { "auto_convert", "automatically convert bitstream format",
  633. OFFSET(auto_convert), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, DEC },
  634. { NULL }
  635. };
  636. static const AVClass concat_class = {
  637. .class_name = "concat demuxer",
  638. .item_name = av_default_item_name,
  639. .option = options,
  640. .version = LIBAVUTIL_VERSION_INT,
  641. };
  642. AVInputFormat ff_concat_demuxer = {
  643. .name = "concat",
  644. .long_name = NULL_IF_CONFIG_SMALL("Virtual concatenation script"),
  645. .priv_data_size = sizeof(ConcatContext),
  646. .read_probe = concat_probe,
  647. .read_header = concat_read_header,
  648. .read_packet = concat_read_packet,
  649. .read_close = concat_read_close,
  650. .read_seek2 = concat_seek,
  651. .priv_class = &concat_class,
  652. };