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.

312 lines
9.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 "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 = NULL;
  77. size_t url_len;
  78. int ret;
  79. if (cat->safe > 0 && !safe_filename(filename)) {
  80. av_log(avf, AV_LOG_ERROR, "Unsafe file name '%s'\n", filename);
  81. FAIL(AVERROR(EPERM));
  82. }
  83. url_len = strlen(avf->filename) + strlen(filename) + 16;
  84. if (!(url = av_malloc(url_len)))
  85. FAIL(AVERROR(ENOMEM));
  86. ff_make_absolute_url(url, url_len, avf->filename, filename);
  87. av_freep(&filename);
  88. if (cat->nb_files >= *nb_files_alloc) {
  89. size_t n = FFMAX(*nb_files_alloc * 2, 16);
  90. ConcatFile *new_files;
  91. if (n <= cat->nb_files || n > SIZE_MAX / sizeof(*cat->files) ||
  92. !(new_files = av_realloc(cat->files, n * sizeof(*cat->files))))
  93. FAIL(AVERROR(ENOMEM));
  94. cat->files = new_files;
  95. *nb_files_alloc = n;
  96. }
  97. file = &cat->files[cat->nb_files++];
  98. memset(file, 0, sizeof(*file));
  99. *rfile = file;
  100. file->url = url;
  101. file->start_time = AV_NOPTS_VALUE;
  102. file->duration = AV_NOPTS_VALUE;
  103. return 0;
  104. fail:
  105. av_free(url);
  106. av_free(filename);
  107. return ret;
  108. }
  109. static int open_file(AVFormatContext *avf, unsigned fileno)
  110. {
  111. ConcatContext *cat = avf->priv_data;
  112. ConcatFile *file = &cat->files[fileno];
  113. int ret;
  114. if ((ret = avformat_open_input(&cat->avf, file->url, NULL, NULL)) < 0 ||
  115. (ret = avformat_find_stream_info(cat->avf, NULL)) < 0) {
  116. av_log(avf, AV_LOG_ERROR, "Impossible to open '%s'\n", file->url);
  117. return ret;
  118. }
  119. cat->cur_file = file;
  120. if (file->start_time == AV_NOPTS_VALUE)
  121. file->start_time = !fileno ? 0 :
  122. cat->files[fileno - 1].start_time +
  123. cat->files[fileno - 1].duration;
  124. return 0;
  125. }
  126. static int concat_read_close(AVFormatContext *avf)
  127. {
  128. ConcatContext *cat = avf->priv_data;
  129. unsigned i;
  130. if (cat->avf)
  131. avformat_close_input(&cat->avf);
  132. for (i = 0; i < cat->nb_files; i++)
  133. av_freep(&cat->files[i].url);
  134. av_freep(&cat->files);
  135. return 0;
  136. }
  137. static int concat_read_header(AVFormatContext *avf)
  138. {
  139. ConcatContext *cat = avf->priv_data;
  140. uint8_t buf[4096];
  141. uint8_t *cursor, *keyword;
  142. int ret, line = 0, i;
  143. unsigned nb_files_alloc = 0;
  144. ConcatFile *file = NULL;
  145. AVStream *st, *source_st;
  146. int64_t time = 0;
  147. while (1) {
  148. if ((ret = ff_get_line(avf->pb, buf, sizeof(buf))) <= 0)
  149. break;
  150. line++;
  151. cursor = buf;
  152. keyword = get_keyword(&cursor);
  153. if (!*keyword || *keyword == '#')
  154. continue;
  155. if (!strcmp(keyword, "file")) {
  156. char *filename = av_get_token((const char **)&cursor, SPACE_CHARS);
  157. if (!filename) {
  158. av_log(avf, AV_LOG_ERROR, "Line %d: filename required\n", line);
  159. FAIL(AVERROR_INVALIDDATA);
  160. }
  161. if ((ret = add_file(avf, filename, &file, &nb_files_alloc)) < 0)
  162. FAIL(ret);
  163. } else if (!strcmp(keyword, "duration")) {
  164. char *dur_str = get_keyword(&cursor);
  165. int64_t dur;
  166. if (!file) {
  167. av_log(avf, AV_LOG_ERROR, "Line %d: duration without file\n",
  168. line);
  169. FAIL(AVERROR_INVALIDDATA);
  170. }
  171. if ((ret = av_parse_time(&dur, dur_str, 1)) < 0) {
  172. av_log(avf, AV_LOG_ERROR, "Line %d: invalid duration '%s'\n",
  173. line, dur_str);
  174. FAIL(ret);
  175. }
  176. file->duration = dur;
  177. } else if (!strcmp(keyword, "ffconcat")) {
  178. char *ver_kw = get_keyword(&cursor);
  179. char *ver_val = get_keyword(&cursor);
  180. if (strcmp(ver_kw, "version") || strcmp(ver_val, "1.0")) {
  181. av_log(avf, AV_LOG_ERROR, "Line %d: invalid version\n", line);
  182. FAIL(AVERROR_INVALIDDATA);
  183. }
  184. if (cat->safe < 0)
  185. cat->safe = 1;
  186. } else {
  187. av_log(avf, AV_LOG_ERROR, "Line %d: unknown keyword '%s'\n",
  188. line, keyword);
  189. FAIL(AVERROR_INVALIDDATA);
  190. }
  191. }
  192. if (ret < 0)
  193. FAIL(ret);
  194. for (i = 0; i < cat->nb_files; i++) {
  195. if (cat->files[i].start_time == AV_NOPTS_VALUE)
  196. cat->files[i].start_time = time;
  197. else
  198. time = cat->files[i].start_time;
  199. if (cat->files[i].duration == AV_NOPTS_VALUE)
  200. break;
  201. time += cat->files[i].duration;
  202. }
  203. if (i == cat->nb_files)
  204. avf->duration = time;
  205. if ((ret = open_file(avf, 0)) < 0)
  206. FAIL(ret);
  207. for (i = 0; i < cat->avf->nb_streams; i++) {
  208. if (!(st = avformat_new_stream(avf, NULL)))
  209. FAIL(AVERROR(ENOMEM));
  210. source_st = cat->avf->streams[i];
  211. if ((ret = avcodec_copy_context(st->codec, source_st->codec)) < 0)
  212. FAIL(ret);
  213. st->r_frame_rate = source_st->r_frame_rate;
  214. st->avg_frame_rate = source_st->avg_frame_rate;
  215. st->time_base = source_st->time_base;
  216. st->sample_aspect_ratio = source_st->sample_aspect_ratio;
  217. }
  218. return 0;
  219. fail:
  220. concat_read_close(avf);
  221. return ret;
  222. }
  223. static int open_next_file(AVFormatContext *avf)
  224. {
  225. ConcatContext *cat = avf->priv_data;
  226. unsigned fileno = cat->cur_file - cat->files;
  227. if (cat->cur_file->duration == AV_NOPTS_VALUE)
  228. cat->cur_file->duration = cat->avf->duration;
  229. if (++fileno >= cat->nb_files)
  230. return AVERROR_EOF;
  231. avformat_close_input(&cat->avf);
  232. return open_file(avf, fileno);
  233. }
  234. static int concat_read_packet(AVFormatContext *avf, AVPacket *pkt)
  235. {
  236. ConcatContext *cat = avf->priv_data;
  237. int ret;
  238. int64_t delta;
  239. while (1) {
  240. if ((ret = av_read_frame(cat->avf, pkt)) != AVERROR_EOF ||
  241. (ret = open_next_file(avf)) < 0)
  242. break;
  243. }
  244. delta = av_rescale_q(cat->cur_file->start_time - cat->avf->start_time,
  245. AV_TIME_BASE_Q,
  246. cat->avf->streams[pkt->stream_index]->time_base);
  247. if (pkt->pts != AV_NOPTS_VALUE)
  248. pkt->pts += delta;
  249. if (pkt->dts != AV_NOPTS_VALUE)
  250. pkt->dts += delta;
  251. return ret;
  252. }
  253. #define OFFSET(x) offsetof(ConcatContext, x)
  254. #define DEC AV_OPT_FLAG_DECODING_PARAM
  255. static const AVOption options[] = {
  256. { "safe", "enable safe mode",
  257. OFFSET(safe), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 1, DEC },
  258. { NULL }
  259. };
  260. static const AVClass concat_class = {
  261. .class_name = "concat demuxer",
  262. .item_name = av_default_item_name,
  263. .option = options,
  264. .version = LIBAVUTIL_VERSION_INT,
  265. };
  266. AVInputFormat ff_concat_demuxer = {
  267. .name = "concat",
  268. .long_name = NULL_IF_CONFIG_SMALL("Virtual concatenation script"),
  269. .priv_data_size = sizeof(ConcatContext),
  270. .read_probe = concat_probe,
  271. .read_header = concat_read_header,
  272. .read_packet = concat_read_packet,
  273. .read_close = concat_read_close,
  274. .priv_class = &concat_class,
  275. };