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.

278 lines
8.1KB

  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 "libavutil/opt.h"
  22. #include "avformat.h"
  23. #include "internal.h"
  24. typedef struct {
  25. char *url;
  26. int64_t start_time;
  27. int64_t duration;
  28. } ConcatFile;
  29. typedef struct {
  30. AVClass *class;
  31. ConcatFile *files;
  32. ConcatFile *cur_file;
  33. unsigned nb_files;
  34. AVFormatContext *avf;
  35. int safe;
  36. } ConcatContext;
  37. static int concat_probe(AVProbeData *probe)
  38. {
  39. return memcmp(probe->buf, "ffconcat version 1.0", 20) ?
  40. 0 : AVPROBE_SCORE_MAX;
  41. }
  42. static char *get_keyword(uint8_t **cursor)
  43. {
  44. char *ret = *cursor += strspn(*cursor, SPACE_CHARS);
  45. *cursor += strcspn(*cursor, SPACE_CHARS);
  46. if (**cursor) {
  47. *((*cursor)++) = 0;
  48. *cursor += strspn(*cursor, SPACE_CHARS);
  49. }
  50. return ret;
  51. }
  52. static int safe_filename(const char *f)
  53. {
  54. const char *start = f;
  55. for (; *f; f++) {
  56. /* A-Za-z0-9_- */
  57. if (!((unsigned)((*f | 32) - 'a') < 26 ||
  58. (unsigned)(*f - '0') < 10 || *f == '_' || *f == '-')) {
  59. if (f == start)
  60. return 0;
  61. else if (*f == '/')
  62. start = f + 1;
  63. else if (*f != '.')
  64. return 0;
  65. }
  66. }
  67. return 1;
  68. }
  69. #define FAIL(retcode) do { ret = (retcode); goto fail; } while(0)
  70. static int add_file(AVFormatContext *avf, char *filename, ConcatFile **rfile,
  71. unsigned *nb_files_alloc)
  72. {
  73. ConcatContext *cat = avf->priv_data;
  74. ConcatFile *file;
  75. char *url;
  76. size_t url_len;
  77. if (cat->safe > 0 && !safe_filename(filename)) {
  78. av_log(avf, AV_LOG_ERROR, "Unsafe file name '%s'\n", filename);
  79. return AVERROR(EPERM);
  80. }
  81. url_len = strlen(avf->filename) + strlen(filename) + 16;
  82. if (!(url = av_malloc(url_len)))
  83. return AVERROR(ENOMEM);
  84. ff_make_absolute_url(url, url_len, avf->filename, filename);
  85. av_free(filename);
  86. if (cat->nb_files >= *nb_files_alloc) {
  87. size_t n = FFMAX(*nb_files_alloc * 2, 16);
  88. ConcatFile *new_files;
  89. if (n <= cat->nb_files || n > SIZE_MAX / sizeof(*cat->files) ||
  90. !(new_files = av_realloc(cat->files, n * sizeof(*cat->files))))
  91. return AVERROR(ENOMEM);
  92. cat->files = new_files;
  93. *nb_files_alloc = n;
  94. }
  95. file = &cat->files[cat->nb_files++];
  96. memset(file, 0, sizeof(*file));
  97. *rfile = file;
  98. file->url = url;
  99. file->start_time = AV_NOPTS_VALUE;
  100. file->duration = AV_NOPTS_VALUE;
  101. return 0;
  102. }
  103. static int open_file(AVFormatContext *avf, unsigned fileno)
  104. {
  105. ConcatContext *cat = avf->priv_data;
  106. ConcatFile *file = &cat->files[fileno];
  107. int ret;
  108. if ((ret = avformat_open_input(&cat->avf, file->url, NULL, NULL)) < 0 ||
  109. (ret = avformat_find_stream_info(cat->avf, NULL)) < 0) {
  110. av_log(avf, AV_LOG_ERROR, "Impossible to open '%s'\n", file->url);
  111. return ret;
  112. }
  113. cat->cur_file = file;
  114. if (file->start_time == AV_NOPTS_VALUE)
  115. file->start_time = !fileno ? 0 :
  116. cat->files[fileno - 1].start_time +
  117. cat->files[fileno - 1].duration;
  118. return 0;
  119. }
  120. static int concat_read_close(AVFormatContext *avf)
  121. {
  122. ConcatContext *cat = avf->priv_data;
  123. unsigned i;
  124. if (cat->avf)
  125. avformat_close_input(&cat->avf);
  126. for (i = 0; i < cat->nb_files; i++)
  127. av_freep(&cat->files[i].url);
  128. av_freep(&cat->files);
  129. return 0;
  130. }
  131. static int concat_read_header(AVFormatContext *avf)
  132. {
  133. ConcatContext *cat = avf->priv_data;
  134. uint8_t buf[4096];
  135. uint8_t *cursor, *keyword;
  136. int ret, line = 0, i;
  137. unsigned nb_files_alloc = 0;
  138. ConcatFile *file = NULL;
  139. AVStream *st, *source_st;
  140. while (1) {
  141. if ((ret = ff_get_line(avf->pb, buf, sizeof(buf))) <= 0)
  142. break;
  143. line++;
  144. cursor = buf;
  145. keyword = get_keyword(&cursor);
  146. if (!*keyword || *keyword == '#')
  147. continue;
  148. if (!strcmp(keyword, "file")) {
  149. char *filename = av_get_token((const char **)&cursor, SPACE_CHARS);
  150. if (!filename) {
  151. av_log(avf, AV_LOG_ERROR, "Line %d: filename required\n", line);
  152. FAIL(AVERROR_INVALIDDATA);
  153. }
  154. if ((ret = add_file(avf, filename, &file, &nb_files_alloc)) < 0)
  155. FAIL(ret);
  156. } else if (!strcmp(keyword, "ffconcat")) {
  157. char *ver_kw = get_keyword(&cursor);
  158. char *ver_val = get_keyword(&cursor);
  159. if (strcmp(ver_kw, "version") || strcmp(ver_val, "1.0")) {
  160. av_log(avf, AV_LOG_ERROR, "Line %d: invalid version\n", line);
  161. FAIL(AVERROR_INVALIDDATA);
  162. }
  163. if (cat->safe < 0)
  164. cat->safe = 1;
  165. } else {
  166. av_log(avf, AV_LOG_ERROR, "Line %d: unknown keyword '%s'\n",
  167. line, keyword);
  168. FAIL(AVERROR_INVALIDDATA);
  169. }
  170. }
  171. if (ret < 0)
  172. FAIL(ret);
  173. if ((ret = open_file(avf, 0)) < 0)
  174. FAIL(ret);
  175. for (i = 0; i < cat->avf->nb_streams; i++) {
  176. if (!(st = avformat_new_stream(avf, NULL)))
  177. FAIL(AVERROR(ENOMEM));
  178. source_st = cat->avf->streams[i];
  179. if ((ret = avcodec_copy_context(st->codec, source_st->codec)) < 0)
  180. FAIL(ret);
  181. st->r_frame_rate = source_st->r_frame_rate;
  182. st->avg_frame_rate = source_st->avg_frame_rate;
  183. st->time_base = source_st->time_base;
  184. st->sample_aspect_ratio = source_st->sample_aspect_ratio;
  185. }
  186. return 0;
  187. fail:
  188. concat_read_close(avf);
  189. return ret;
  190. }
  191. static int open_next_file(AVFormatContext *avf)
  192. {
  193. ConcatContext *cat = avf->priv_data;
  194. unsigned fileno = cat->cur_file - cat->files;
  195. if (cat->cur_file->duration == AV_NOPTS_VALUE)
  196. cat->cur_file->duration = cat->avf->duration;
  197. if (++fileno >= cat->nb_files)
  198. return AVERROR_EOF;
  199. avformat_close_input(&cat->avf);
  200. return open_file(avf, fileno);
  201. }
  202. static int concat_read_packet(AVFormatContext *avf, AVPacket *pkt)
  203. {
  204. ConcatContext *cat = avf->priv_data;
  205. int ret;
  206. int64_t delta;
  207. while (1) {
  208. if ((ret = av_read_frame(cat->avf, pkt)) != AVERROR_EOF ||
  209. (ret = open_next_file(avf)) < 0)
  210. break;
  211. }
  212. delta = av_rescale_q(cat->cur_file->start_time - cat->avf->start_time,
  213. AV_TIME_BASE_Q,
  214. cat->avf->streams[pkt->stream_index]->time_base);
  215. if (pkt->pts != AV_NOPTS_VALUE)
  216. pkt->pts += delta;
  217. if (pkt->dts != AV_NOPTS_VALUE)
  218. pkt->dts += delta;
  219. return ret;
  220. }
  221. #define OFFSET(x) offsetof(ConcatContext, x)
  222. #define DEC AV_OPT_FLAG_DECODING_PARAM
  223. static const AVOption options[] = {
  224. { "safe", "enable safe mode",
  225. OFFSET(safe), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 1, DEC },
  226. { NULL }
  227. };
  228. static const AVClass concat_class = {
  229. .class_name = "concat demuxer",
  230. .item_name = av_default_item_name,
  231. .option = options,
  232. .version = LIBAVUTIL_VERSION_INT,
  233. };
  234. AVInputFormat ff_concat_demuxer = {
  235. .name = "concat",
  236. .long_name = NULL_IF_CONFIG_SMALL("Virtual concatenation script"),
  237. .priv_data_size = sizeof(ConcatContext),
  238. .read_probe = concat_probe,
  239. .read_header = concat_read_header,
  240. .read_packet = concat_read_packet,
  241. .read_close = concat_read_close,
  242. .priv_class = &concat_class,
  243. };