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.

224 lines
6.5KB

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