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.

277 lines
8.0KB

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