LCOV - code coverage report
Current view: top level - src/libavformat - concatdec.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 257 446 57.6 %
Date: 2017-09-22 10:31:19 Functions: 15 20 75.0 %

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

Generated by: LCOV version 1.13