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

Generated by: LCOV version 1.13