]> git.sesse.net Git - ffmpeg/blob - libavformat/concatdec.c
Merge commit '600d5ee6b12bad144756b0772319bb04796bc528'
[ffmpeg] / libavformat / concatdec.c
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
21 #include "libavutil/avassert.h"
22 #include "libavutil/avstring.h"
23 #include "libavutil/intreadwrite.h"
24 #include "libavutil/opt.h"
25 #include "libavutil/parseutils.h"
26 #include "avformat.h"
27 #include "internal.h"
28 #include "url.h"
29
30 typedef enum ConcatMatchMode {
31     MATCH_ONE_TO_ONE,
32     MATCH_EXACT_ID,
33 } ConcatMatchMode;
34
35 typedef struct ConcatStream {
36     AVBitStreamFilterContext *bsf;
37     int out_stream_index;
38 } ConcatStream;
39
40 typedef struct {
41     char *url;
42     int64_t start_time;
43     int64_t duration;
44     ConcatStream *streams;
45     int nb_streams;
46 } ConcatFile;
47
48 typedef struct {
49     AVClass *class;
50     ConcatFile *files;
51     ConcatFile *cur_file;
52     unsigned nb_files;
53     AVFormatContext *avf;
54     int safe;
55     int seekable;
56     ConcatMatchMode stream_match_mode;
57     unsigned auto_convert;
58 } ConcatContext;
59
60 static int concat_probe(AVProbeData *probe)
61 {
62     return memcmp(probe->buf, "ffconcat version 1.0", 20) ?
63            0 : AVPROBE_SCORE_MAX;
64 }
65
66 static char *get_keyword(uint8_t **cursor)
67 {
68     char *ret = *cursor += strspn(*cursor, SPACE_CHARS);
69     *cursor += strcspn(*cursor, SPACE_CHARS);
70     if (**cursor) {
71         *((*cursor)++) = 0;
72         *cursor += strspn(*cursor, SPACE_CHARS);
73     }
74     return ret;
75 }
76
77 static int safe_filename(const char *f)
78 {
79     const char *start = f;
80
81     for (; *f; f++) {
82         /* A-Za-z0-9_- */
83         if (!((unsigned)((*f | 32) - 'a') < 26 ||
84               (unsigned)(*f - '0') < 10 || *f == '_' || *f == '-')) {
85             if (f == start)
86                 return 0;
87             else if (*f == '/')
88                 start = f + 1;
89             else if (*f != '.')
90                 return 0;
91         }
92     }
93     return 1;
94 }
95
96 #define FAIL(retcode) do { ret = (retcode); goto fail; } while(0)
97
98 static int add_file(AVFormatContext *avf, char *filename, ConcatFile **rfile,
99                     unsigned *nb_files_alloc)
100 {
101     ConcatContext *cat = avf->priv_data;
102     ConcatFile *file;
103     char *url = NULL;
104     const char *proto;
105     size_t url_len, proto_len;
106     int ret;
107
108     if (cat->safe > 0 && !safe_filename(filename)) {
109         av_log(avf, AV_LOG_ERROR, "Unsafe file name '%s'\n", filename);
110         FAIL(AVERROR(EPERM));
111     }
112
113     proto = avio_find_protocol_name(filename);
114     proto_len = proto ? strlen(proto) : 0;
115     if (!memcmp(filename, proto, proto_len) &&
116         (filename[proto_len] == ':' || filename[proto_len] == ',')) {
117         url = filename;
118         filename = NULL;
119     } else {
120         url_len = strlen(avf->filename) + strlen(filename) + 16;
121         if (!(url = av_malloc(url_len)))
122             FAIL(AVERROR(ENOMEM));
123         ff_make_absolute_url(url, url_len, avf->filename, filename);
124         av_freep(&filename);
125     }
126
127     if (cat->nb_files >= *nb_files_alloc) {
128         size_t n = FFMAX(*nb_files_alloc * 2, 16);
129         ConcatFile *new_files;
130         if (n <= cat->nb_files || n > SIZE_MAX / sizeof(*cat->files) ||
131             !(new_files = av_realloc(cat->files, n * sizeof(*cat->files))))
132             FAIL(AVERROR(ENOMEM));
133         cat->files = new_files;
134         *nb_files_alloc = n;
135     }
136
137     file = &cat->files[cat->nb_files++];
138     memset(file, 0, sizeof(*file));
139     *rfile = file;
140
141     file->url        = url;
142     file->start_time = AV_NOPTS_VALUE;
143     file->duration   = AV_NOPTS_VALUE;
144
145     return 0;
146
147 fail:
148     av_free(url);
149     av_free(filename);
150     return ret;
151 }
152
153 static int copy_stream_props(AVStream *st, AVStream *source_st)
154 {
155     int ret;
156
157     if (st->codec->codec_id || !source_st->codec->codec_id) {
158         if (st->codec->extradata_size < source_st->codec->extradata_size) {
159             ret = ff_alloc_extradata(st->codec,
160                                      source_st->codec->extradata_size);
161             if (ret < 0)
162                 return ret;
163         }
164         memcpy(st->codec->extradata, source_st->codec->extradata,
165                source_st->codec->extradata_size);
166         return 0;
167     }
168     if ((ret = avcodec_copy_context(st->codec, source_st->codec)) < 0)
169         return ret;
170     st->r_frame_rate        = source_st->r_frame_rate;
171     st->avg_frame_rate      = source_st->avg_frame_rate;
172     st->time_base           = source_st->time_base;
173     st->sample_aspect_ratio = source_st->sample_aspect_ratio;
174     return 0;
175 }
176
177 static int detect_stream_specific(AVFormatContext *avf, int idx)
178 {
179     ConcatContext *cat = avf->priv_data;
180     AVStream *st = cat->avf->streams[idx];
181     ConcatStream *cs = &cat->cur_file->streams[idx];
182     AVBitStreamFilterContext *bsf;
183
184     if (cat->auto_convert && st->codec->codec_id == AV_CODEC_ID_H264 &&
185         (st->codec->extradata_size < 4 || AV_RB32(st->codec->extradata) != 1)) {
186         av_log(cat->avf, AV_LOG_INFO,
187                "Auto-inserting h264_mp4toannexb bitstream filter\n");
188         if (!(bsf = av_bitstream_filter_init("h264_mp4toannexb"))) {
189             av_log(avf, AV_LOG_ERROR, "h264_mp4toannexb bitstream filter "
190                    "required for H.264 streams\n");
191             return AVERROR_BSF_NOT_FOUND;
192         }
193         cs->bsf = bsf;
194     }
195     return 0;
196 }
197
198 static int match_streams_one_to_one(AVFormatContext *avf)
199 {
200     ConcatContext *cat = avf->priv_data;
201     AVStream *st;
202     int i, ret;
203
204     for (i = cat->cur_file->nb_streams; i < cat->avf->nb_streams; i++) {
205         if (i < avf->nb_streams) {
206             st = avf->streams[i];
207         } else {
208             if (!(st = avformat_new_stream(avf, NULL)))
209                 return AVERROR(ENOMEM);
210         }
211         if ((ret = copy_stream_props(st, cat->avf->streams[i])) < 0)
212             return ret;
213         cat->cur_file->streams[i].out_stream_index = i;
214     }
215     return 0;
216 }
217
218 static int match_streams_exact_id(AVFormatContext *avf)
219 {
220     ConcatContext *cat = avf->priv_data;
221     AVStream *st;
222     int i, j, ret;
223
224     for (i = cat->cur_file->nb_streams; i < cat->avf->nb_streams; i++) {
225         st = cat->avf->streams[i];
226         for (j = 0; j < avf->nb_streams; j++) {
227             if (avf->streams[j]->id == st->id) {
228                 av_log(avf, AV_LOG_VERBOSE,
229                        "Match slave stream #%d with stream #%d id 0x%x\n",
230                        i, j, st->id);
231                 if ((ret = copy_stream_props(avf->streams[j], st)) < 0)
232                     return ret;
233                 cat->cur_file->streams[i].out_stream_index = j;
234             }
235         }
236     }
237     return 0;
238 }
239
240 static int match_streams(AVFormatContext *avf)
241 {
242     ConcatContext *cat = avf->priv_data;
243     ConcatStream *map;
244     int i, ret;
245
246     if (cat->cur_file->nb_streams >= cat->avf->nb_streams)
247         return 0;
248     map = av_realloc(cat->cur_file->streams,
249                      cat->avf->nb_streams * sizeof(*map));
250     if (!map)
251         return AVERROR(ENOMEM);
252     cat->cur_file->streams = map;
253     memset(map + cat->cur_file->nb_streams, 0,
254            (cat->avf->nb_streams - cat->cur_file->nb_streams) * sizeof(*map));
255
256     for (i = cat->cur_file->nb_streams; i < cat->avf->nb_streams; i++)
257         map[i].out_stream_index = -1;
258     switch (cat->stream_match_mode) {
259     case MATCH_ONE_TO_ONE:
260         ret = match_streams_one_to_one(avf);
261         break;
262     case MATCH_EXACT_ID:
263         ret = match_streams_exact_id(avf);
264         break;
265     default:
266         ret = AVERROR_BUG;
267     }
268     if (ret < 0)
269         return ret;
270     for (i = cat->cur_file->nb_streams; i < cat->avf->nb_streams; i++)
271         if ((ret = detect_stream_specific(avf, i)) < 0)
272             return ret;
273     cat->cur_file->nb_streams = cat->avf->nb_streams;
274     return 0;
275 }
276
277 static int open_file(AVFormatContext *avf, unsigned fileno)
278 {
279     ConcatContext *cat = avf->priv_data;
280     ConcatFile *file = &cat->files[fileno];
281     int ret;
282
283     if (cat->avf)
284         avformat_close_input(&cat->avf);
285
286     cat->avf = avformat_alloc_context();
287     if (!cat->avf)
288         return AVERROR(ENOMEM);
289
290     cat->avf->interrupt_callback = avf->interrupt_callback;
291
292     if ((ret = ff_copy_whitelists(cat->avf, avf)) < 0)
293         return ret;
294
295     if ((ret = avformat_open_input(&cat->avf, file->url, NULL, NULL)) < 0 ||
296         (ret = avformat_find_stream_info(cat->avf, NULL)) < 0) {
297         av_log(avf, AV_LOG_ERROR, "Impossible to open '%s'\n", file->url);
298         avformat_close_input(&cat->avf);
299         return ret;
300     }
301     cat->cur_file = file;
302     if (file->start_time == AV_NOPTS_VALUE)
303         file->start_time = !fileno ? 0 :
304                            cat->files[fileno - 1].start_time +
305                            cat->files[fileno - 1].duration;
306     if ((ret = match_streams(avf)) < 0)
307         return ret;
308     return 0;
309 }
310
311 static int concat_read_close(AVFormatContext *avf)
312 {
313     ConcatContext *cat = avf->priv_data;
314     unsigned i;
315
316     if (cat->avf)
317         avformat_close_input(&cat->avf);
318     for (i = 0; i < cat->nb_files; i++) {
319         av_freep(&cat->files[i].url);
320         av_freep(&cat->files[i].streams);
321     }
322     av_freep(&cat->files);
323     return 0;
324 }
325
326 static int concat_read_header(AVFormatContext *avf)
327 {
328     ConcatContext *cat = avf->priv_data;
329     uint8_t buf[4096];
330     uint8_t *cursor, *keyword;
331     int ret, line = 0, i;
332     unsigned nb_files_alloc = 0;
333     ConcatFile *file = NULL;
334     int64_t time = 0;
335
336     while (1) {
337         if ((ret = ff_get_line(avf->pb, buf, sizeof(buf))) <= 0)
338             break;
339         line++;
340         cursor = buf;
341         keyword = get_keyword(&cursor);
342         if (!*keyword || *keyword == '#')
343             continue;
344
345         if (!strcmp(keyword, "file")) {
346             char *filename = av_get_token((const char **)&cursor, SPACE_CHARS);
347             if (!filename) {
348                 av_log(avf, AV_LOG_ERROR, "Line %d: filename required\n", line);
349                 FAIL(AVERROR_INVALIDDATA);
350             }
351             if ((ret = add_file(avf, filename, &file, &nb_files_alloc)) < 0)
352                 goto fail;
353         } else if (!strcmp(keyword, "duration")) {
354             char *dur_str = get_keyword(&cursor);
355             int64_t dur;
356             if (!file) {
357                 av_log(avf, AV_LOG_ERROR, "Line %d: duration without file\n",
358                        line);
359                 FAIL(AVERROR_INVALIDDATA);
360             }
361             if ((ret = av_parse_time(&dur, dur_str, 1)) < 0) {
362                 av_log(avf, AV_LOG_ERROR, "Line %d: invalid duration '%s'\n",
363                        line, dur_str);
364                 goto fail;
365             }
366             file->duration = dur;
367         } else if (!strcmp(keyword, "stream")) {
368             if (!avformat_new_stream(avf, NULL))
369                 FAIL(AVERROR(ENOMEM));
370         } else if (!strcmp(keyword, "exact_stream_id")) {
371             if (!avf->nb_streams) {
372                 av_log(avf, AV_LOG_ERROR, "Line %d: exact_stream_id without stream\n",
373                        line);
374                 FAIL(AVERROR_INVALIDDATA);
375             }
376             avf->streams[avf->nb_streams - 1]->id =
377                 strtol(get_keyword(&cursor), NULL, 0);
378         } else if (!strcmp(keyword, "ffconcat")) {
379             char *ver_kw  = get_keyword(&cursor);
380             char *ver_val = get_keyword(&cursor);
381             if (strcmp(ver_kw, "version") || strcmp(ver_val, "1.0")) {
382                 av_log(avf, AV_LOG_ERROR, "Line %d: invalid version\n", line);
383                 FAIL(AVERROR_INVALIDDATA);
384             }
385             if (cat->safe < 0)
386                 cat->safe = 1;
387         } else {
388             av_log(avf, AV_LOG_ERROR, "Line %d: unknown keyword '%s'\n",
389                    line, keyword);
390             FAIL(AVERROR_INVALIDDATA);
391         }
392     }
393     if (ret < 0)
394         goto fail;
395     if (!cat->nb_files)
396         FAIL(AVERROR_INVALIDDATA);
397
398     for (i = 0; i < cat->nb_files; i++) {
399         if (cat->files[i].start_time == AV_NOPTS_VALUE)
400             cat->files[i].start_time = time;
401         else
402             time = cat->files[i].start_time;
403         if (cat->files[i].duration == AV_NOPTS_VALUE)
404             break;
405         time += cat->files[i].duration;
406     }
407     if (i == cat->nb_files) {
408         avf->duration = time;
409         cat->seekable = 1;
410     }
411
412     cat->stream_match_mode = avf->nb_streams ? MATCH_EXACT_ID :
413                                                MATCH_ONE_TO_ONE;
414     if ((ret = open_file(avf, 0)) < 0)
415         goto fail;
416     return 0;
417
418 fail:
419     concat_read_close(avf);
420     return ret;
421 }
422
423 static int open_next_file(AVFormatContext *avf)
424 {
425     ConcatContext *cat = avf->priv_data;
426     unsigned fileno = cat->cur_file - cat->files;
427
428     if (cat->cur_file->duration == AV_NOPTS_VALUE)
429         cat->cur_file->duration = cat->avf->duration;
430
431     if (++fileno >= cat->nb_files)
432         return AVERROR_EOF;
433     return open_file(avf, fileno);
434 }
435
436 static int filter_packet(AVFormatContext *avf, ConcatStream *cs, AVPacket *pkt)
437 {
438     AVStream *st = avf->streams[cs->out_stream_index];
439     AVBitStreamFilterContext *bsf;
440     AVPacket pkt2;
441     int ret;
442
443     av_assert0(cs->out_stream_index >= 0);
444     for (bsf = cs->bsf; bsf; bsf = bsf->next) {
445         pkt2 = *pkt;
446         ret = av_bitstream_filter_filter(bsf, st->codec, NULL,
447                                          &pkt2.data, &pkt2.size,
448                                          pkt->data, pkt->size,
449                                          !!(pkt->flags & AV_PKT_FLAG_KEY));
450         if (ret < 0) {
451             av_packet_unref(pkt);
452             return ret;
453         }
454         av_assert0(pkt2.buf);
455         if (ret == 0 && pkt2.data != pkt->data) {
456             if ((ret = av_copy_packet(&pkt2, pkt)) < 0) {
457                 av_free(pkt2.data);
458                 return ret;
459             }
460             ret = 1;
461         }
462         if (ret > 0) {
463             av_free_packet(pkt);
464             pkt2.buf = av_buffer_create(pkt2.data, pkt2.size,
465                                         av_buffer_default_free, NULL, 0);
466             if (!pkt2.buf) {
467                 av_free(pkt2.data);
468                 return AVERROR(ENOMEM);
469             }
470         }
471         *pkt = pkt2;
472     }
473     return 0;
474 }
475
476 static int concat_read_packet(AVFormatContext *avf, AVPacket *pkt)
477 {
478     ConcatContext *cat = avf->priv_data;
479     int ret;
480     int64_t delta;
481     ConcatStream *cs;
482
483     while (1) {
484         ret = av_read_frame(cat->avf, pkt);
485         if (ret == AVERROR_EOF) {
486             if ((ret = open_next_file(avf)) < 0)
487                 return ret;
488             continue;
489         }
490         if (ret < 0)
491             return ret;
492         if ((ret = match_streams(avf)) < 0) {
493             av_packet_unref(pkt);
494             return ret;
495         }
496         cs = &cat->cur_file->streams[pkt->stream_index];
497         if (cs->out_stream_index < 0) {
498             av_packet_unref(pkt);
499             continue;
500         }
501         pkt->stream_index = cs->out_stream_index;
502         break;
503     }
504     if ((ret = filter_packet(avf, cs, pkt)))
505         return ret;
506
507     delta = av_rescale_q(cat->cur_file->start_time - cat->avf->start_time,
508                          AV_TIME_BASE_Q,
509                          cat->avf->streams[pkt->stream_index]->time_base);
510     if (pkt->pts != AV_NOPTS_VALUE)
511         pkt->pts += delta;
512     if (pkt->dts != AV_NOPTS_VALUE)
513         pkt->dts += delta;
514     return ret;
515 }
516
517 static void rescale_interval(AVRational tb_in, AVRational tb_out,
518                              int64_t *min_ts, int64_t *ts, int64_t *max_ts)
519 {
520     *ts     = av_rescale_q    (*    ts, tb_in, tb_out);
521     *min_ts = av_rescale_q_rnd(*min_ts, tb_in, tb_out,
522                                AV_ROUND_UP   | AV_ROUND_PASS_MINMAX);
523     *max_ts = av_rescale_q_rnd(*max_ts, tb_in, tb_out,
524                                AV_ROUND_DOWN | AV_ROUND_PASS_MINMAX);
525 }
526
527 static int try_seek(AVFormatContext *avf, int stream,
528                     int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
529 {
530     ConcatContext *cat = avf->priv_data;
531     int64_t t0 = cat->cur_file->start_time - cat->avf->start_time;
532
533     ts -= t0;
534     min_ts = min_ts == INT64_MIN ? INT64_MIN : min_ts - t0;
535     max_ts = max_ts == INT64_MAX ? INT64_MAX : max_ts - t0;
536     if (stream >= 0) {
537         if (stream >= cat->avf->nb_streams)
538             return AVERROR(EIO);
539         rescale_interval(AV_TIME_BASE_Q, cat->avf->streams[stream]->time_base,
540                          &min_ts, &ts, &max_ts);
541     }
542     return avformat_seek_file(cat->avf, stream, min_ts, ts, max_ts, flags);
543 }
544
545 static int real_seek(AVFormatContext *avf, int stream,
546                      int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
547 {
548     ConcatContext *cat = avf->priv_data;
549     int ret, left, right;
550
551     if (stream >= 0) {
552         if (stream >= avf->nb_streams)
553             return AVERROR(EINVAL);
554         rescale_interval(avf->streams[stream]->time_base, AV_TIME_BASE_Q,
555                          &min_ts, &ts, &max_ts);
556     }
557
558     left  = 0;
559     right = cat->nb_files;
560     while (right - left > 1) {
561         int mid = (left + right) / 2;
562         if (ts < cat->files[mid].start_time)
563             right = mid;
564         else
565             left  = mid;
566     }
567
568     if ((ret = open_file(avf, left)) < 0)
569         return ret;
570
571     ret = try_seek(avf, stream, min_ts, ts, max_ts, flags);
572     if (ret < 0 &&
573         left < cat->nb_files - 1 &&
574         cat->files[left + 1].start_time < max_ts) {
575         if ((ret = open_file(avf, left + 1)) < 0)
576             return ret;
577         ret = try_seek(avf, stream, min_ts, ts, max_ts, flags);
578     }
579     return ret;
580 }
581
582 static int concat_seek(AVFormatContext *avf, int stream,
583                        int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
584 {
585     ConcatContext *cat = avf->priv_data;
586     ConcatFile *cur_file_saved = cat->cur_file;
587     AVFormatContext *cur_avf_saved = cat->avf;
588     int ret;
589
590     if (!cat->seekable)
591         return AVERROR(ESPIPE); /* XXX: can we use it? */
592     if (flags & (AVSEEK_FLAG_BYTE | AVSEEK_FLAG_FRAME))
593         return AVERROR(ENOSYS);
594     cat->avf = NULL;
595     if ((ret = real_seek(avf, stream, min_ts, ts, max_ts, flags)) < 0) {
596         if (cat->avf)
597             avformat_close_input(&cat->avf);
598         cat->avf      = cur_avf_saved;
599         cat->cur_file = cur_file_saved;
600     } else {
601         avformat_close_input(&cur_avf_saved);
602     }
603     return ret;
604 }
605
606 #define OFFSET(x) offsetof(ConcatContext, x)
607 #define DEC AV_OPT_FLAG_DECODING_PARAM
608
609 static const AVOption options[] = {
610     { "safe", "enable safe mode",
611       OFFSET(safe), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 1, DEC },
612     { "auto_convert", "automatically convert bitstream format",
613       OFFSET(auto_convert), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, DEC },
614     { NULL }
615 };
616
617 static const AVClass concat_class = {
618     .class_name = "concat demuxer",
619     .item_name  = av_default_item_name,
620     .option     = options,
621     .version    = LIBAVUTIL_VERSION_INT,
622 };
623
624
625 AVInputFormat ff_concat_demuxer = {
626     .name           = "concat",
627     .long_name      = NULL_IF_CONFIG_SMALL("Virtual concatenation script"),
628     .priv_data_size = sizeof(ConcatContext),
629     .read_probe     = concat_probe,
630     .read_header    = concat_read_header,
631     .read_packet    = concat_read_packet,
632     .read_close     = concat_read_close,
633     .read_seek2     = concat_seek,
634     .priv_class     = &concat_class,
635 };