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.

222 lines
6.4KB

  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/avstring.h"
  21. #include "avformat.h"
  22. #include "internal.h"
  23. typedef struct {
  24. char *url;
  25. int64_t start_time;
  26. int64_t duration;
  27. } ConcatFile;
  28. typedef struct {
  29. ConcatFile *files;
  30. ConcatFile *cur_file;
  31. unsigned nb_files;
  32. AVFormatContext *avf;
  33. } ConcatContext;
  34. static int concat_probe(AVProbeData *probe)
  35. {
  36. return 0;
  37. }
  38. static char *get_keyword(uint8_t **cursor)
  39. {
  40. char *ret = *cursor += strspn(*cursor, SPACE_CHARS);
  41. *cursor += strcspn(*cursor, SPACE_CHARS);
  42. if (**cursor) {
  43. *((*cursor)++) = 0;
  44. *cursor += strspn(*cursor, SPACE_CHARS);
  45. }
  46. return ret;
  47. }
  48. #define FAIL(retcode) do { ret = (retcode); goto fail; } while(0)
  49. static int add_file(AVFormatContext *avf, char *filename, ConcatFile **rfile,
  50. unsigned *nb_files_alloc)
  51. {
  52. ConcatContext *cat = avf->priv_data;
  53. ConcatFile *file;
  54. char *url;
  55. size_t url_len;
  56. url_len = strlen(avf->filename) + strlen(filename) + 16;
  57. if (!(url = av_malloc(url_len)))
  58. return AVERROR(ENOMEM);
  59. ff_make_absolute_url(url, url_len, avf->filename, filename);
  60. av_free(filename);
  61. if (cat->nb_files >= *nb_files_alloc) {
  62. unsigned n = FFMAX(*nb_files_alloc * 2, 16);
  63. if (n <= cat->nb_files ||
  64. !(cat->files = av_realloc_f(cat->files, n, sizeof(*cat->files))))
  65. return AVERROR(ENOMEM);
  66. *nb_files_alloc = n;
  67. }
  68. file = &cat->files[cat->nb_files++];
  69. memset(file, 0, sizeof(*file));
  70. *rfile = file;
  71. file->url = url;
  72. file->start_time = AV_NOPTS_VALUE;
  73. file->duration = AV_NOPTS_VALUE;
  74. return 0;
  75. }
  76. static int open_file(AVFormatContext *avf, unsigned fileno)
  77. {
  78. ConcatContext *cat = avf->priv_data;
  79. ConcatFile *file = &cat->files[fileno];
  80. int ret;
  81. if ((ret = avformat_open_input(&cat->avf, file->url, NULL, NULL)) < 0 ||
  82. (ret = avformat_find_stream_info(cat->avf, NULL)) < 0) {
  83. av_log(avf, AV_LOG_ERROR, "Impossible to open '%s'\n", file->url);
  84. return ret;
  85. }
  86. cat->cur_file = file;
  87. if (file->start_time == AV_NOPTS_VALUE)
  88. file->start_time = !fileno ? 0 :
  89. cat->files[fileno - 1].start_time +
  90. cat->files[fileno - 1].duration;
  91. return 0;
  92. }
  93. static int concat_read_close(AVFormatContext *avf)
  94. {
  95. ConcatContext *cat = avf->priv_data;
  96. unsigned i;
  97. if (cat->avf)
  98. avformat_close_input(&cat->avf);
  99. for (i = 0; i < cat->nb_files; i++)
  100. av_freep(&cat->files[i].url);
  101. av_freep(&cat->files);
  102. return 0;
  103. }
  104. static int concat_read_header(AVFormatContext *avf)
  105. {
  106. ConcatContext *cat = avf->priv_data;
  107. uint8_t buf[4096];
  108. uint8_t *cursor, *keyword;
  109. int ret, line = 0, i;
  110. unsigned nb_files_alloc = 0;
  111. ConcatFile *file = NULL;
  112. AVStream *st, *source_st;
  113. while (1) {
  114. if ((ret = ff_get_line(avf->pb, buf, sizeof(buf))) <= 0)
  115. break;
  116. line++;
  117. cursor = buf;
  118. keyword = get_keyword(&cursor);
  119. if (!*keyword || *keyword == '#')
  120. continue;
  121. if (!strcmp(keyword, "file")) {
  122. char *filename = av_get_token((const char **)&cursor, SPACE_CHARS);
  123. if (!filename) {
  124. av_log(avf, AV_LOG_ERROR, "Line %d: filename required\n", line);
  125. FAIL(AVERROR_INVALIDDATA);
  126. }
  127. if ((ret = add_file(avf, filename, &file, &nb_files_alloc)) < 0)
  128. FAIL(ret);
  129. } else {
  130. av_log(avf, AV_LOG_ERROR, "Line %d: unknown keyword '%s'\n",
  131. line, keyword);
  132. FAIL(AVERROR_INVALIDDATA);
  133. }
  134. }
  135. if (ret < 0)
  136. FAIL(ret);
  137. if ((ret = open_file(avf, 0)) < 0)
  138. FAIL(ret);
  139. for (i = 0; i < cat->avf->nb_streams; i++) {
  140. if (!(st = avformat_new_stream(avf, NULL)))
  141. FAIL(AVERROR(ENOMEM));
  142. source_st = cat->avf->streams[i];
  143. if ((ret = avcodec_copy_context(st->codec, source_st->codec)) < 0)
  144. FAIL(ret);
  145. st->r_frame_rate = source_st->r_frame_rate;
  146. st->avg_frame_rate = source_st->avg_frame_rate;
  147. st->time_base = source_st->time_base;
  148. st->sample_aspect_ratio = source_st->sample_aspect_ratio;
  149. }
  150. return 0;
  151. fail:
  152. concat_read_close(avf);
  153. return ret;
  154. }
  155. static int open_next_file(AVFormatContext *avf)
  156. {
  157. ConcatContext *cat = avf->priv_data;
  158. unsigned fileno = cat->cur_file - cat->files;
  159. if (cat->cur_file->duration == AV_NOPTS_VALUE)
  160. cat->cur_file->duration = cat->avf->duration;
  161. if (++fileno >= cat->nb_files)
  162. return AVERROR_EOF;
  163. avformat_close_input(&cat->avf);
  164. return open_file(avf, fileno);
  165. }
  166. static int concat_read_packet(AVFormatContext *avf, AVPacket *pkt)
  167. {
  168. ConcatContext *cat = avf->priv_data;
  169. int ret;
  170. int64_t delta;
  171. while (1) {
  172. if ((ret = av_read_frame(cat->avf, pkt)) != AVERROR_EOF ||
  173. (ret = open_next_file(avf)) < 0)
  174. break;
  175. }
  176. delta = av_rescale_q(cat->cur_file->start_time - cat->avf->start_time,
  177. AV_TIME_BASE_Q,
  178. cat->avf->streams[pkt->stream_index]->time_base);
  179. if (pkt->pts != AV_NOPTS_VALUE)
  180. pkt->pts += delta;
  181. if (pkt->dts != AV_NOPTS_VALUE)
  182. pkt->dts += delta;
  183. return ret;
  184. }
  185. AVInputFormat ff_concat_demuxer = {
  186. .name = "concat",
  187. .long_name = NULL_IF_CONFIG_SMALL("Virtual concatenation script"),
  188. .priv_data_size = sizeof(ConcatContext),
  189. .read_probe = concat_probe,
  190. .read_header = concat_read_header,
  191. .read_packet = concat_read_packet,
  192. .read_close = concat_read_close,
  193. };