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.

294 lines
8.7KB

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