LCOV - code coverage report
Current view: top level - libavfilter - src_movie.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 192 349 55.0 %
Date: 2017-12-12 11:08:38 Functions: 8 12 66.7 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2010 Stefano Sabatini
       3             :  * Copyright (c) 2008 Victor Paesa
       4             :  *
       5             :  * This file is part of FFmpeg.
       6             :  *
       7             :  * FFmpeg is free software; you can redistribute it and/or
       8             :  * modify it under the terms of the GNU Lesser General Public
       9             :  * License as published by the Free Software Foundation; either
      10             :  * version 2.1 of the License, or (at your option) any later version.
      11             :  *
      12             :  * FFmpeg is distributed in the hope that it will be useful,
      13             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      14             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      15             :  * Lesser General Public License for more details.
      16             :  *
      17             :  * You should have received a copy of the GNU Lesser General Public
      18             :  * License along with FFmpeg; if not, write to the Free Software
      19             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      20             :  */
      21             : 
      22             : /**
      23             :  * @file
      24             :  * movie video source
      25             :  *
      26             :  * @todo use direct rendering (no allocation of a new frame)
      27             :  * @todo support a PTS correction mechanism
      28             :  */
      29             : 
      30             : #include <float.h>
      31             : #include <stdint.h>
      32             : 
      33             : #include "libavutil/attributes.h"
      34             : #include "libavutil/avstring.h"
      35             : #include "libavutil/avassert.h"
      36             : #include "libavutil/opt.h"
      37             : #include "libavutil/imgutils.h"
      38             : #include "libavutil/internal.h"
      39             : #include "libavutil/timestamp.h"
      40             : 
      41             : #include "libavcodec/avcodec.h"
      42             : 
      43             : #include "libavformat/avformat.h"
      44             : 
      45             : #include "audio.h"
      46             : #include "avfilter.h"
      47             : #include "formats.h"
      48             : #include "internal.h"
      49             : #include "video.h"
      50             : 
      51             : typedef struct MovieStream {
      52             :     AVStream *st;
      53             :     AVCodecContext *codec_ctx;
      54             :     int done;
      55             :     int64_t discontinuity_threshold;
      56             :     int64_t last_pts;
      57             : } MovieStream;
      58             : 
      59             : typedef struct MovieContext {
      60             :     /* common A/V fields */
      61             :     const AVClass *class;
      62             :     int64_t seek_point;   ///< seekpoint in microseconds
      63             :     double seek_point_d;
      64             :     char *format_name;
      65             :     char *file_name;
      66             :     char *stream_specs; /**< user-provided list of streams, separated by + */
      67             :     int stream_index; /**< for compatibility */
      68             :     int loop_count;
      69             :     int64_t discontinuity_threshold;
      70             :     int64_t ts_offset;
      71             : 
      72             :     AVFormatContext *format_ctx;
      73             :     int eof;
      74             :     AVPacket pkt, pkt0;
      75             : 
      76             :     int max_stream_index; /**< max stream # actually used for output */
      77             :     MovieStream *st; /**< array of all streams, one per output */
      78             :     int *out_index; /**< stream number -> output number map, or -1 */
      79             : } MovieContext;
      80             : 
      81             : #define OFFSET(x) offsetof(MovieContext, x)
      82             : #define FLAGS AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_VIDEO_PARAM
      83             : 
      84             : static const AVOption movie_options[]= {
      85             :     { "filename",     NULL,                      OFFSET(file_name),    AV_OPT_TYPE_STRING,                                    .flags = FLAGS },
      86             :     { "format_name",  "set format name",         OFFSET(format_name),  AV_OPT_TYPE_STRING,                                    .flags = FLAGS },
      87             :     { "f",            "set format name",         OFFSET(format_name),  AV_OPT_TYPE_STRING,                                    .flags = FLAGS },
      88             :     { "stream_index", "set stream index",        OFFSET(stream_index), AV_OPT_TYPE_INT,    { .i64 = -1 }, -1, INT_MAX,                 FLAGS  },
      89             :     { "si",           "set stream index",        OFFSET(stream_index), AV_OPT_TYPE_INT,    { .i64 = -1 }, -1, INT_MAX,                 FLAGS  },
      90             :     { "seek_point",   "set seekpoint (seconds)", OFFSET(seek_point_d), AV_OPT_TYPE_DOUBLE, { .dbl =  0 },  0, (INT64_MAX-1) / 1000000, FLAGS },
      91             :     { "sp",           "set seekpoint (seconds)", OFFSET(seek_point_d), AV_OPT_TYPE_DOUBLE, { .dbl =  0 },  0, (INT64_MAX-1) / 1000000, FLAGS },
      92             :     { "streams",      "set streams",             OFFSET(stream_specs), AV_OPT_TYPE_STRING, {.str =  0},  CHAR_MAX, CHAR_MAX, FLAGS },
      93             :     { "s",            "set streams",             OFFSET(stream_specs), AV_OPT_TYPE_STRING, {.str =  0},  CHAR_MAX, CHAR_MAX, FLAGS },
      94             :     { "loop",         "set loop count",          OFFSET(loop_count),   AV_OPT_TYPE_INT,    {.i64 =  1},  0,        INT_MAX, FLAGS },
      95             :     { "discontinuity", "set discontinuity threshold", OFFSET(discontinuity_threshold), AV_OPT_TYPE_DURATION, {.i64 = 0}, 0, INT64_MAX, FLAGS },
      96             :     { NULL },
      97             : };
      98             : 
      99             : static int movie_config_output_props(AVFilterLink *outlink);
     100             : static int movie_request_frame(AVFilterLink *outlink);
     101             : 
     102          10 : static AVStream *find_stream(void *log, AVFormatContext *avf, const char *spec)
     103             : {
     104          10 :     int i, ret, already = 0, stream_id = -1;
     105             :     char type_char[2], dummy;
     106          10 :     AVStream *found = NULL;
     107             :     enum AVMediaType type;
     108             : 
     109          10 :     ret = sscanf(spec, "d%1[av]%d%c", type_char, &stream_id, &dummy);
     110          10 :     if (ret >= 1 && ret <= 2) {
     111          10 :         type = type_char[0] == 'v' ? AVMEDIA_TYPE_VIDEO : AVMEDIA_TYPE_AUDIO;
     112          10 :         ret = av_find_best_stream(avf, type, stream_id, -1, NULL, 0);
     113          10 :         if (ret < 0) {
     114           0 :             av_log(log, AV_LOG_ERROR, "No %s stream with index '%d' found\n",
     115             :                    av_get_media_type_string(type), stream_id);
     116           0 :             return NULL;
     117             :         }
     118          10 :         return avf->streams[ret];
     119             :     }
     120           0 :     for (i = 0; i < avf->nb_streams; i++) {
     121           0 :         ret = avformat_match_stream_specifier(avf, avf->streams[i], spec);
     122           0 :         if (ret < 0) {
     123           0 :             av_log(log, AV_LOG_ERROR,
     124             :                    "Invalid stream specifier \"%s\"\n", spec);
     125           0 :             return NULL;
     126             :         }
     127           0 :         if (!ret)
     128           0 :             continue;
     129           0 :         if (avf->streams[i]->discard != AVDISCARD_ALL) {
     130           0 :             already++;
     131           0 :             continue;
     132             :         }
     133           0 :         if (found) {
     134           0 :             av_log(log, AV_LOG_WARNING,
     135             :                    "Ambiguous stream specifier \"%s\", using #%d\n", spec, i);
     136           0 :             break;
     137             :         }
     138           0 :         found = avf->streams[i];
     139             :     }
     140           0 :     if (!found) {
     141           0 :         av_log(log, AV_LOG_WARNING, "Stream specifier \"%s\" %s\n", spec,
     142             :                already ? "matched only already used streams" :
     143             :                          "did not match any stream");
     144           0 :         return NULL;
     145             :     }
     146           0 :     if (found->codecpar->codec_type != AVMEDIA_TYPE_VIDEO &&
     147           0 :         found->codecpar->codec_type != AVMEDIA_TYPE_AUDIO) {
     148           0 :         av_log(log, AV_LOG_ERROR, "Stream specifier \"%s\" matched a %s stream,"
     149             :                "currently unsupported by libavfilter\n", spec,
     150           0 :                av_get_media_type_string(found->codecpar->codec_type));
     151           0 :         return NULL;
     152             :     }
     153           0 :     return found;
     154             : }
     155             : 
     156          10 : static int open_stream(void *log, MovieStream *st)
     157             : {
     158             :     AVCodec *codec;
     159             :     int ret;
     160             : 
     161          10 :     codec = avcodec_find_decoder(st->st->codecpar->codec_id);
     162          10 :     if (!codec) {
     163           0 :         av_log(log, AV_LOG_ERROR, "Failed to find any codec\n");
     164           0 :         return AVERROR(EINVAL);
     165             :     }
     166             : 
     167          10 :     st->codec_ctx = avcodec_alloc_context3(codec);
     168          10 :     if (!st->codec_ctx)
     169           0 :         return AVERROR(ENOMEM);
     170             : 
     171          10 :     ret = avcodec_parameters_to_context(st->codec_ctx, st->st->codecpar);
     172          10 :     if (ret < 0)
     173           0 :         return ret;
     174             : 
     175          10 :     st->codec_ctx->refcounted_frames = 1;
     176             : 
     177          10 :     if ((ret = avcodec_open2(st->codec_ctx, codec, NULL)) < 0) {
     178           0 :         av_log(log, AV_LOG_ERROR, "Failed to open codec\n");
     179           0 :         return ret;
     180             :     }
     181             : 
     182          10 :     return 0;
     183             : }
     184             : 
     185           0 : static int guess_channel_layout(MovieStream *st, int st_index, void *log_ctx)
     186             : {
     187           0 :     AVCodecParameters *dec_par = st->st->codecpar;
     188             :     char buf[256];
     189           0 :     int64_t chl = av_get_default_channel_layout(dec_par->channels);
     190             : 
     191           0 :     if (!chl) {
     192           0 :         av_log(log_ctx, AV_LOG_ERROR,
     193             :                "Channel layout is not set in stream %d, and could not "
     194             :                "be guessed from the number of channels (%d)\n",
     195             :                st_index, dec_par->channels);
     196           0 :         return AVERROR(EINVAL);
     197             :     }
     198             : 
     199           0 :     av_get_channel_layout_string(buf, sizeof(buf), dec_par->channels, chl);
     200           0 :     av_log(log_ctx, AV_LOG_WARNING,
     201             :            "Channel layout is not set in output stream %d, "
     202             :            "guessed channel layout is '%s'\n",
     203             :            st_index, buf);
     204           0 :     dec_par->channel_layout = chl;
     205           0 :     return 0;
     206             : }
     207             : 
     208          10 : static av_cold int movie_common_init(AVFilterContext *ctx)
     209             : {
     210          10 :     MovieContext *movie = ctx->priv;
     211          10 :     AVInputFormat *iformat = NULL;
     212             :     int64_t timestamp;
     213          10 :     int nb_streams = 1, ret, i;
     214             :     char default_streams[16], *stream_specs, *spec, *cursor;
     215             :     char name[16];
     216             :     AVStream *st;
     217             : 
     218          10 :     if (!movie->file_name) {
     219           0 :         av_log(ctx, AV_LOG_ERROR, "No filename provided!\n");
     220           0 :         return AVERROR(EINVAL);
     221             :     }
     222             : 
     223          10 :     movie->seek_point = movie->seek_point_d * 1000000 + 0.5;
     224             : 
     225          10 :     stream_specs = movie->stream_specs;
     226          10 :     if (!stream_specs) {
     227          20 :         snprintf(default_streams, sizeof(default_streams), "d%c%d",
     228          10 :                  !strcmp(ctx->filter->name, "amovie") ? 'a' : 'v',
     229             :                  movie->stream_index);
     230          10 :         stream_specs = default_streams;
     231             :     }
     232          50 :     for (cursor = stream_specs; *cursor; cursor++)
     233          40 :         if (*cursor == '+')
     234           0 :             nb_streams++;
     235             : 
     236          10 :     if (movie->loop_count != 1 && nb_streams != 1) {
     237           0 :         av_log(ctx, AV_LOG_ERROR,
     238             :                "Loop with several streams is currently unsupported\n");
     239           0 :         return AVERROR_PATCHWELCOME;
     240             :     }
     241             : 
     242          10 :     av_register_all();
     243             : 
     244             :     // Try to find the movie format (container)
     245          10 :     iformat = movie->format_name ? av_find_input_format(movie->format_name) : NULL;
     246             : 
     247          10 :     movie->format_ctx = NULL;
     248          10 :     if ((ret = avformat_open_input(&movie->format_ctx, movie->file_name, iformat, NULL)) < 0) {
     249           0 :         av_log(ctx, AV_LOG_ERROR,
     250             :                "Failed to avformat_open_input '%s'\n", movie->file_name);
     251           0 :         return ret;
     252             :     }
     253          10 :     if ((ret = avformat_find_stream_info(movie->format_ctx, NULL)) < 0)
     254           0 :         av_log(ctx, AV_LOG_WARNING, "Failed to find stream info\n");
     255             : 
     256             :     // if seeking requested, we execute it
     257          10 :     if (movie->seek_point > 0) {
     258           0 :         timestamp = movie->seek_point;
     259             :         // add the stream start time, should it exist
     260           0 :         if (movie->format_ctx->start_time != AV_NOPTS_VALUE) {
     261           0 :             if (timestamp > 0 && movie->format_ctx->start_time > INT64_MAX - timestamp) {
     262           0 :                 av_log(ctx, AV_LOG_ERROR,
     263             :                        "%s: seek value overflow with start_time:%"PRId64" seek_point:%"PRId64"\n",
     264           0 :                        movie->file_name, movie->format_ctx->start_time, movie->seek_point);
     265           0 :                 return AVERROR(EINVAL);
     266             :             }
     267           0 :             timestamp += movie->format_ctx->start_time;
     268             :         }
     269           0 :         if ((ret = av_seek_frame(movie->format_ctx, -1, timestamp, AVSEEK_FLAG_BACKWARD)) < 0) {
     270           0 :             av_log(ctx, AV_LOG_ERROR, "%s: could not seek to position %"PRId64"\n",
     271             :                    movie->file_name, timestamp);
     272           0 :             return ret;
     273             :         }
     274             :     }
     275             : 
     276          22 :     for (i = 0; i < movie->format_ctx->nb_streams; i++)
     277          12 :         movie->format_ctx->streams[i]->discard = AVDISCARD_ALL;
     278             : 
     279          10 :     movie->st = av_calloc(nb_streams, sizeof(*movie->st));
     280          10 :     if (!movie->st)
     281           0 :         return AVERROR(ENOMEM);
     282             : 
     283          20 :     for (i = 0; i < nb_streams; i++) {
     284          10 :         spec = av_strtok(stream_specs, "+", &cursor);
     285          10 :         if (!spec)
     286           0 :             return AVERROR_BUG;
     287          10 :         stream_specs = NULL; /* for next strtok */
     288          10 :         st = find_stream(ctx, movie->format_ctx, spec);
     289          10 :         if (!st)
     290           0 :             return AVERROR(EINVAL);
     291          10 :         st->discard = AVDISCARD_DEFAULT;
     292          10 :         movie->st[i].st = st;
     293          10 :         movie->max_stream_index = FFMAX(movie->max_stream_index, st->index);
     294          20 :         movie->st[i].discontinuity_threshold =
     295          20 :             av_rescale_q(movie->discontinuity_threshold, AV_TIME_BASE_Q, st->time_base);
     296             :     }
     297          10 :     if (av_strtok(NULL, "+", &cursor))
     298           0 :         return AVERROR_BUG;
     299             : 
     300          10 :     movie->out_index = av_calloc(movie->max_stream_index + 1,
     301             :                                  sizeof(*movie->out_index));
     302          10 :     if (!movie->out_index)
     303           0 :         return AVERROR(ENOMEM);
     304          20 :     for (i = 0; i <= movie->max_stream_index; i++)
     305          10 :         movie->out_index[i] = -1;
     306          40 :     for (i = 0; i < nb_streams; i++) {
     307          10 :         AVFilterPad pad = { 0 };
     308          10 :         movie->out_index[movie->st[i].st->index] = i;
     309          10 :         snprintf(name, sizeof(name), "out%d", i);
     310          10 :         pad.type          = movie->st[i].st->codecpar->codec_type;
     311          10 :         pad.name          = av_strdup(name);
     312          10 :         if (!pad.name)
     313           0 :             return AVERROR(ENOMEM);
     314          10 :         pad.config_props  = movie_config_output_props;
     315          10 :         pad.request_frame = movie_request_frame;
     316          10 :         if ((ret = ff_insert_outpad(ctx, i, &pad)) < 0) {
     317           0 :             av_freep(&pad.name);
     318           0 :             return ret;
     319             :         }
     320          13 :         if ( movie->st[i].st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
     321           3 :             !movie->st[i].st->codecpar->channel_layout) {
     322           0 :             ret = guess_channel_layout(&movie->st[i], i, ctx);
     323           0 :             if (ret < 0)
     324           0 :                 return ret;
     325             :         }
     326          10 :         ret = open_stream(ctx, &movie->st[i]);
     327          10 :         if (ret < 0)
     328           0 :             return ret;
     329             :     }
     330             : 
     331          10 :     av_log(ctx, AV_LOG_VERBOSE, "seek_point:%"PRIi64" format_name:%s file_name:%s stream_index:%d\n",
     332             :            movie->seek_point, movie->format_name, movie->file_name,
     333             :            movie->stream_index);
     334             : 
     335          10 :     return 0;
     336             : }
     337             : 
     338          10 : static av_cold void movie_uninit(AVFilterContext *ctx)
     339             : {
     340          10 :     MovieContext *movie = ctx->priv;
     341             :     int i;
     342             : 
     343          20 :     for (i = 0; i < ctx->nb_outputs; i++) {
     344          10 :         av_freep(&ctx->output_pads[i].name);
     345          10 :         if (movie->st[i].st)
     346          10 :             avcodec_free_context(&movie->st[i].codec_ctx);
     347             :     }
     348          10 :     av_freep(&movie->st);
     349          10 :     av_freep(&movie->out_index);
     350          10 :     if (movie->format_ctx)
     351          10 :         avformat_close_input(&movie->format_ctx);
     352          10 : }
     353             : 
     354          10 : static int movie_query_formats(AVFilterContext *ctx)
     355             : {
     356          10 :     MovieContext *movie = ctx->priv;
     357          10 :     int list[] = { 0, -1 };
     358          10 :     int64_t list64[] = { 0, -1 };
     359             :     int i, ret;
     360             : 
     361          20 :     for (i = 0; i < ctx->nb_outputs; i++) {
     362          10 :         MovieStream *st = &movie->st[i];
     363          10 :         AVCodecParameters *c = st->st->codecpar;
     364          10 :         AVFilterLink *outlink = ctx->outputs[i];
     365             : 
     366          10 :         switch (c->codec_type) {
     367           7 :         case AVMEDIA_TYPE_VIDEO:
     368           7 :             list[0] = c->format;
     369           7 :             if ((ret = ff_formats_ref(ff_make_format_list(list), &outlink->in_formats)) < 0)
     370           0 :                 return ret;
     371           7 :             break;
     372           3 :         case AVMEDIA_TYPE_AUDIO:
     373           3 :             list[0] = c->format;
     374           3 :             if ((ret = ff_formats_ref(ff_make_format_list(list), &outlink->in_formats)) < 0)
     375           0 :                 return ret;
     376           3 :             list[0] = c->sample_rate;
     377           3 :             if ((ret = ff_formats_ref(ff_make_format_list(list), &outlink->in_samplerates)) < 0)
     378           0 :                 return ret;
     379           3 :             list64[0] = c->channel_layout;
     380           3 :             if ((ret = ff_channel_layouts_ref(avfilter_make_format64_list(list64),
     381           3 :                                    &outlink->in_channel_layouts)) < 0)
     382           0 :                 return ret;
     383           3 :             break;
     384             :         }
     385             :     }
     386             : 
     387          10 :     return 0;
     388             : }
     389             : 
     390          10 : static int movie_config_output_props(AVFilterLink *outlink)
     391             : {
     392          10 :     AVFilterContext *ctx = outlink->src;
     393          10 :     MovieContext *movie  = ctx->priv;
     394          10 :     unsigned out_id = FF_OUTLINK_IDX(outlink);
     395          10 :     MovieStream *st = &movie->st[out_id];
     396          10 :     AVCodecParameters *c = st->st->codecpar;
     397             : 
     398          10 :     outlink->time_base = st->st->time_base;
     399             : 
     400          10 :     switch (c->codec_type) {
     401           7 :     case AVMEDIA_TYPE_VIDEO:
     402           7 :         outlink->w          = c->width;
     403           7 :         outlink->h          = c->height;
     404           7 :         outlink->frame_rate = st->st->r_frame_rate;
     405           7 :         break;
     406           3 :     case AVMEDIA_TYPE_AUDIO:
     407           3 :         break;
     408             :     }
     409             : 
     410          10 :     return 0;
     411             : }
     412             : 
     413           0 : static char *describe_frame_to_str(char *dst, size_t dst_size,
     414             :                                    AVFrame *frame, enum AVMediaType frame_type,
     415             :                                    AVFilterLink *link)
     416             : {
     417           0 :     switch (frame_type) {
     418           0 :     case AVMEDIA_TYPE_VIDEO:
     419           0 :         snprintf(dst, dst_size,
     420             :                  "video pts:%s time:%s size:%dx%d aspect:%d/%d",
     421           0 :                  av_ts2str(frame->pts), av_ts2timestr(frame->pts, &link->time_base),
     422             :                  frame->width, frame->height,
     423             :                  frame->sample_aspect_ratio.num,
     424             :                  frame->sample_aspect_ratio.den);
     425           0 :                  break;
     426           0 :     case AVMEDIA_TYPE_AUDIO:
     427           0 :         snprintf(dst, dst_size,
     428             :                  "audio pts:%s time:%s samples:%d",
     429           0 :                  av_ts2str(frame->pts), av_ts2timestr(frame->pts, &link->time_base),
     430             :                  frame->nb_samples);
     431           0 :                  break;
     432           0 :     default:
     433           0 :         snprintf(dst, dst_size, "%s BUG", av_get_media_type_string(frame_type));
     434           0 :         break;
     435             :     }
     436           0 :     return dst;
     437             : }
     438             : 
     439           0 : static int rewind_file(AVFilterContext *ctx)
     440             : {
     441           0 :     MovieContext *movie = ctx->priv;
     442           0 :     int64_t timestamp = movie->seek_point;
     443             :     int ret, i;
     444             : 
     445           0 :     if (movie->format_ctx->start_time != AV_NOPTS_VALUE)
     446           0 :         timestamp += movie->format_ctx->start_time;
     447           0 :     ret = av_seek_frame(movie->format_ctx, -1, timestamp, AVSEEK_FLAG_BACKWARD);
     448           0 :     if (ret < 0) {
     449           0 :         av_log(ctx, AV_LOG_ERROR, "Unable to loop: %s\n", av_err2str(ret));
     450           0 :         movie->loop_count = 1; /* do not try again */
     451           0 :         return ret;
     452             :     }
     453             : 
     454           0 :     for (i = 0; i < ctx->nb_outputs; i++) {
     455           0 :         avcodec_flush_buffers(movie->st[i].codec_ctx);
     456           0 :         movie->st[i].done = 0;
     457             :     }
     458           0 :     movie->eof = 0;
     459           0 :     return 0;
     460             : }
     461             : 
     462             : /**
     463             :  * Try to push a frame to the requested output.
     464             :  *
     465             :  * @param ctx     filter context
     466             :  * @param out_id  number of output where a frame is wanted;
     467             :  *                if the frame is read from file, used to set the return value;
     468             :  *                if the codec is being flushed, flush the corresponding stream
     469             :  * @return  1 if a frame was pushed on the requested output,
     470             :  *          0 if another attempt is possible,
     471             :  *          <0 AVERROR code
     472             :  */
     473        2790 : static int movie_push_frame(AVFilterContext *ctx, unsigned out_id)
     474             : {
     475        2790 :     MovieContext *movie = ctx->priv;
     476        2790 :     AVPacket *pkt = &movie->pkt;
     477             :     enum AVMediaType frame_type;
     478             :     MovieStream *st;
     479        2790 :     int ret, got_frame = 0, pkt_out_id;
     480             :     AVFilterLink *outlink;
     481             :     AVFrame *frame;
     482             : 
     483        2790 :     if (!pkt->size) {
     484        2790 :         if (movie->eof) {
     485          26 :             if (movie->st[out_id].done) {
     486          10 :                 if (movie->loop_count != 1) {
     487           0 :                     ret = rewind_file(ctx);
     488           0 :                     if (ret < 0)
     489           0 :                         return ret;
     490           0 :                     movie->loop_count -= movie->loop_count > 1;
     491           0 :                     av_log(ctx, AV_LOG_VERBOSE, "Stream finished, looping.\n");
     492           0 :                     return 0; /* retry */
     493             :                 }
     494          10 :                 return AVERROR_EOF;
     495             :             }
     496          16 :             pkt->stream_index = movie->st[out_id].st->index;
     497             :             /* packet is already ready for flushing */
     498             :         } else {
     499        2764 :             ret = av_read_frame(movie->format_ctx, &movie->pkt0);
     500        2764 :             if (ret < 0) {
     501          10 :                 av_init_packet(&movie->pkt0); /* ready for flushing */
     502          10 :                 *pkt = movie->pkt0;
     503          10 :                 if (ret == AVERROR_EOF) {
     504          10 :                     movie->eof = 1;
     505          10 :                     return 0; /* start flushing */
     506             :                 }
     507           0 :                 return ret;
     508             :             }
     509        2754 :             *pkt = movie->pkt0;
     510             :         }
     511             :     }
     512             : 
     513        5429 :     pkt_out_id = pkt->stream_index > movie->max_stream_index ? -1 :
     514        2659 :                  movie->out_index[pkt->stream_index];
     515        2770 :     if (pkt_out_id < 0) {
     516         111 :         av_packet_unref(&movie->pkt0);
     517         111 :         pkt->size = 0; /* ready for next run */
     518         111 :         pkt->data = NULL;
     519         111 :         return 0;
     520             :     }
     521        2659 :     st = &movie->st[pkt_out_id];
     522        2659 :     outlink = ctx->outputs[pkt_out_id];
     523             : 
     524        2659 :     frame = av_frame_alloc();
     525        2659 :     if (!frame)
     526           0 :         return AVERROR(ENOMEM);
     527             : 
     528        2659 :     frame_type = st->st->codecpar->codec_type;
     529        2659 :     switch (frame_type) {
     530        1841 :     case AVMEDIA_TYPE_VIDEO:
     531        1841 :         ret = avcodec_decode_video2(st->codec_ctx, frame, &got_frame, pkt);
     532        1841 :         break;
     533         818 :     case AVMEDIA_TYPE_AUDIO:
     534         818 :         ret = avcodec_decode_audio4(st->codec_ctx, frame, &got_frame, pkt);
     535         818 :         break;
     536           0 :     default:
     537           0 :         ret = AVERROR(ENOSYS);
     538           0 :         break;
     539             :     }
     540        2659 :     if (ret < 0) {
     541           0 :         av_log(ctx, AV_LOG_WARNING, "Decode error: %s\n", av_err2str(ret));
     542           0 :         av_frame_free(&frame);
     543           0 :         av_packet_unref(&movie->pkt0);
     544           0 :         movie->pkt.size = 0;
     545           0 :         movie->pkt.data = NULL;
     546           0 :         return 0;
     547             :     }
     548        2659 :     if (!ret || st->st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
     549        1844 :         ret = pkt->size;
     550             : 
     551        2659 :     pkt->data += ret;
     552        2659 :     pkt->size -= ret;
     553        2659 :     if (pkt->size <= 0) {
     554        2659 :         av_packet_unref(&movie->pkt0);
     555        2659 :         pkt->size = 0; /* ready for next run */
     556        2659 :         pkt->data = NULL;
     557             :     }
     558        2659 :     if (!got_frame) {
     559          22 :         if (!ret)
     560          10 :             st->done = 1;
     561          22 :         av_frame_free(&frame);
     562          22 :         return 0;
     563             :     }
     564             : 
     565        2637 :     frame->pts = frame->best_effort_timestamp;
     566        2637 :     if (frame->pts != AV_NOPTS_VALUE) {
     567        2635 :         if (movie->ts_offset)
     568           0 :             frame->pts += av_rescale_q_rnd(movie->ts_offset, AV_TIME_BASE_Q, outlink->time_base, AV_ROUND_UP);
     569        2635 :         if (st->discontinuity_threshold) {
     570           0 :             if (st->last_pts != AV_NOPTS_VALUE) {
     571           0 :                 int64_t diff = frame->pts - st->last_pts;
     572           0 :                 if (diff < 0 || diff > st->discontinuity_threshold) {
     573           0 :                     av_log(ctx, AV_LOG_VERBOSE, "Discontinuity in stream:%d diff:%"PRId64"\n", pkt_out_id, diff);
     574           0 :                     movie->ts_offset += av_rescale_q_rnd(-diff, outlink->time_base, AV_TIME_BASE_Q, AV_ROUND_UP);
     575           0 :                     frame->pts -= diff;
     576             :                 }
     577             :             }
     578             :         }
     579        2635 :         st->last_pts = frame->pts;
     580             :     }
     581             :     ff_dlog(ctx, "movie_push_frame(): file:'%s' %s\n", movie->file_name,
     582             :             describe_frame_to_str((char[1024]){0}, 1024, frame, frame_type, outlink));
     583             : 
     584        2637 :     if (st->st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
     585        1822 :         if (frame->format != outlink->format) {
     586           0 :             av_log(ctx, AV_LOG_ERROR, "Format changed %s -> %s, discarding frame\n",
     587           0 :                 av_get_pix_fmt_name(outlink->format),
     588           0 :                 av_get_pix_fmt_name(frame->format)
     589             :                 );
     590           0 :             av_frame_free(&frame);
     591           0 :             return 0;
     592             :         }
     593             :     }
     594        2637 :     ret = ff_filter_frame(outlink, frame);
     595             : 
     596        2637 :     if (ret < 0)
     597           0 :         return ret;
     598        2637 :     return pkt_out_id == out_id;
     599             : }
     600             : 
     601        2647 : static int movie_request_frame(AVFilterLink *outlink)
     602             : {
     603        2647 :     AVFilterContext *ctx = outlink->src;
     604        2647 :     unsigned out_id = FF_OUTLINK_IDX(outlink);
     605             :     int ret;
     606             : 
     607             :     while (1) {
     608        2933 :         ret = movie_push_frame(ctx, out_id);
     609        2790 :         if (ret)
     610        5294 :             return FFMIN(ret, 0);
     611             :     }
     612             : }
     613             : 
     614           0 : static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
     615             :                            char *res, int res_len, int flags)
     616             : {
     617           0 :     MovieContext *movie = ctx->priv;
     618           0 :     int ret = AVERROR(ENOSYS);
     619             : 
     620           0 :     if (!strcmp(cmd, "seek")) {
     621             :         int idx, flags, i;
     622             :         int64_t ts;
     623             :         char tail[2];
     624             : 
     625           0 :         if (sscanf(args, "%i|%"SCNi64"|%i %1s", &idx, &ts, &flags, tail) != 3)
     626           0 :             return AVERROR(EINVAL);
     627             : 
     628           0 :         ret = av_seek_frame(movie->format_ctx, idx, ts, flags);
     629           0 :         if (ret < 0)
     630           0 :             return ret;
     631             : 
     632           0 :         for (i = 0; i < ctx->nb_outputs; i++) {
     633           0 :             avcodec_flush_buffers(movie->st[i].codec_ctx);
     634           0 :             movie->st[i].done = 0;
     635             :         }
     636           0 :         return ret;
     637           0 :     } else if (!strcmp(cmd, "get_duration")) {
     638             :         int print_len;
     639             :         char tail[2];
     640             : 
     641           0 :         if (!res || res_len <= 0)
     642           0 :             return AVERROR(EINVAL);
     643             : 
     644           0 :         if (args && sscanf(args, "%1s", tail) == 1)
     645           0 :             return AVERROR(EINVAL);
     646             : 
     647           0 :         print_len = snprintf(res, res_len, "%"PRId64, movie->format_ctx->duration);
     648           0 :         if (print_len < 0 || print_len >= res_len)
     649           0 :             return AVERROR(EINVAL);
     650             : 
     651           0 :         return 0;
     652             :     }
     653             : 
     654           0 :     return ret;
     655             : }
     656             : 
     657             : #if CONFIG_MOVIE_FILTER
     658             : 
     659             : AVFILTER_DEFINE_CLASS(movie);
     660             : 
     661             : AVFilter ff_avsrc_movie = {
     662             :     .name          = "movie",
     663             :     .description   = NULL_IF_CONFIG_SMALL("Read from a movie source."),
     664             :     .priv_size     = sizeof(MovieContext),
     665             :     .priv_class    = &movie_class,
     666             :     .init          = movie_common_init,
     667             :     .uninit        = movie_uninit,
     668             :     .query_formats = movie_query_formats,
     669             : 
     670             :     .inputs    = NULL,
     671             :     .outputs   = NULL,
     672             :     .flags     = AVFILTER_FLAG_DYNAMIC_OUTPUTS,
     673             :     .process_command = process_command
     674             : };
     675             : 
     676             : #endif  /* CONFIG_MOVIE_FILTER */
     677             : 
     678             : #if CONFIG_AMOVIE_FILTER
     679             : 
     680             : #define amovie_options movie_options
     681             : AVFILTER_DEFINE_CLASS(amovie);
     682             : 
     683             : AVFilter ff_avsrc_amovie = {
     684             :     .name          = "amovie",
     685             :     .description   = NULL_IF_CONFIG_SMALL("Read audio from a movie source."),
     686             :     .priv_size     = sizeof(MovieContext),
     687             :     .init          = movie_common_init,
     688             :     .uninit        = movie_uninit,
     689             :     .query_formats = movie_query_formats,
     690             : 
     691             :     .inputs     = NULL,
     692             :     .outputs    = NULL,
     693             :     .priv_class = &amovie_class,
     694             :     .flags      = AVFILTER_FLAG_DYNAMIC_OUTPUTS,
     695             :     .process_command = process_command,
     696             : };
     697             : 
     698             : #endif /* CONFIG_AMOVIE_FILTER */

Generated by: LCOV version 1.13