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