FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/src_movie.c
Date: 2024-02-29 09:57:37
Exec Total Coverage
Lines: 231 366 63.1%
Functions: 10 13 76.9%
Branches: 112 236 47.5%

Line Branch Exec Source
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 support a PTS correction mechanism
27 */
28
29 #include "config_components.h"
30
31 #include <float.h>
32 #include <stdint.h>
33
34 #include "libavutil/attributes.h"
35 #include "libavutil/avstring.h"
36 #include "libavutil/channel_layout.h"
37 #include "libavutil/opt.h"
38 #include "libavutil/imgutils.h"
39 #include "libavutil/internal.h"
40 #include "libavutil/timestamp.h"
41
42 #include "libavcodec/avcodec.h"
43
44 #include "libavformat/avformat.h"
45
46 #include "audio.h"
47 #include "avfilter.h"
48 #include "filters.h"
49 #include "formats.h"
50 #include "internal.h"
51 #include "video.h"
52
53 typedef struct MovieStream {
54 AVFilterLink *link;
55 AVStream *st;
56 AVCodecContext *codec_ctx;
57 int64_t discontinuity_threshold;
58 int64_t last_pts;
59 AVFrame *frame;
60 int eof;
61 } MovieStream;
62
63 typedef struct MovieContext {
64 /* common A/V fields */
65 const AVClass *class;
66 int64_t seek_point; ///< seekpoint in microseconds
67 double seek_point_d;
68 char *format_name;
69 char *file_name;
70 char *stream_specs; /**< user-provided list of streams, separated by + */
71 int stream_index; /**< for compatibility */
72 int loop_count;
73 int64_t discontinuity_threshold;
74 int64_t ts_offset;
75 int dec_threads;
76
77 AVPacket *pkt;
78 AVFormatContext *format_ctx;
79
80 int eof;
81 int max_stream_index; /**< max stream # actually used for output */
82 MovieStream *st; /**< array of all streams, one per output */
83 int *out_index; /**< stream number -> output number map, or -1 */
84 AVDictionary *format_opts;
85 } MovieContext;
86
87 #define OFFSET(x) offsetof(MovieContext, x)
88 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_VIDEO_PARAM
89
90 static const AVOption movie_options[]= {
91 { "filename", NULL, OFFSET(file_name), AV_OPT_TYPE_STRING, .flags = FLAGS },
92 { "format_name", "set format name", OFFSET(format_name), AV_OPT_TYPE_STRING, .flags = FLAGS },
93 { "f", "set format name", OFFSET(format_name), AV_OPT_TYPE_STRING, .flags = FLAGS },
94 { "stream_index", "set stream index", OFFSET(stream_index), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS },
95 { "si", "set stream index", OFFSET(stream_index), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS },
96 { "seek_point", "set seekpoint (seconds)", OFFSET(seek_point_d), AV_OPT_TYPE_DOUBLE, { .dbl = 0 }, 0, (INT64_MAX-1) / 1000000, FLAGS },
97 { "sp", "set seekpoint (seconds)", OFFSET(seek_point_d), AV_OPT_TYPE_DOUBLE, { .dbl = 0 }, 0, (INT64_MAX-1) / 1000000, FLAGS },
98 { "streams", "set streams", OFFSET(stream_specs), AV_OPT_TYPE_STRING, {.str = 0}, 0, 0, FLAGS },
99 { "s", "set streams", OFFSET(stream_specs), AV_OPT_TYPE_STRING, {.str = 0}, 0, 0, FLAGS },
100 { "loop", "set loop count", OFFSET(loop_count), AV_OPT_TYPE_INT, {.i64 = 1}, 0, INT_MAX, FLAGS },
101 { "discontinuity", "set discontinuity threshold", OFFSET(discontinuity_threshold), AV_OPT_TYPE_DURATION, {.i64 = 0}, 0, INT64_MAX, FLAGS },
102 { "dec_threads", "set the number of threads for decoding", OFFSET(dec_threads), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, FLAGS },
103 { "format_opts", "set format options for the opened file", OFFSET(format_opts), AV_OPT_TYPE_DICT, {.str = NULL}, 0, 0, FLAGS},
104 { NULL },
105 };
106
107 static int movie_config_output_props(AVFilterLink *outlink);
108
109 15 static AVStream *find_stream(void *log, AVFormatContext *avf, const char *spec)
110 {
111 15 int i, ret, already = 0, stream_id = -1;
112 char type_char[2], dummy;
113 15 AVStream *found = NULL;
114 enum AVMediaType type;
115
116 15 ret = sscanf(spec, "d%1[av]%d%c", type_char, &stream_id, &dummy);
117
2/4
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 15 times.
✗ Branch 3 not taken.
15 if (ret >= 1 && ret <= 2) {
118 15 type = type_char[0] == 'v' ? AVMEDIA_TYPE_VIDEO : AVMEDIA_TYPE_AUDIO;
119 15 ret = av_find_best_stream(avf, type, stream_id, -1, NULL, 0);
120
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 if (ret < 0) {
121 av_log(log, AV_LOG_ERROR, "No %s stream with index '%d' found\n",
122 av_get_media_type_string(type), stream_id);
123 return NULL;
124 }
125 15 return avf->streams[ret];
126 }
127 for (i = 0; i < avf->nb_streams; i++) {
128 ret = avformat_match_stream_specifier(avf, avf->streams[i], spec);
129 if (ret < 0) {
130 av_log(log, AV_LOG_ERROR,
131 "Invalid stream specifier \"%s\"\n", spec);
132 return NULL;
133 }
134 if (!ret)
135 continue;
136 if (avf->streams[i]->discard != AVDISCARD_ALL) {
137 already++;
138 continue;
139 }
140 if (found) {
141 av_log(log, AV_LOG_WARNING,
142 "Ambiguous stream specifier \"%s\", using #%d\n", spec, i);
143 break;
144 }
145 found = avf->streams[i];
146 }
147 if (!found) {
148 av_log(log, AV_LOG_WARNING, "Stream specifier \"%s\" %s\n", spec,
149 already ? "matched only already used streams" :
150 "did not match any stream");
151 return NULL;
152 }
153 if (found->codecpar->codec_type != AVMEDIA_TYPE_VIDEO &&
154 found->codecpar->codec_type != AVMEDIA_TYPE_AUDIO) {
155 av_log(log, AV_LOG_ERROR, "Stream specifier \"%s\" matched a %s stream,"
156 "currently unsupported by libavfilter\n", spec,
157 av_get_media_type_string(found->codecpar->codec_type));
158 return NULL;
159 }
160 return found;
161 }
162
163 3735 static int get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
164 {
165 int linesize_align[AV_NUM_DATA_POINTERS];
166 3735 MovieStream *st = avctx->opaque;
167 3735 AVFilterLink *outlink = st->link;
168 3735 int w, h, ow, oh, copy = 0;
169 AVFrame *new;
170
171 3735 h = oh = frame->height;
172 3735 w = ow = frame->width;
173
174 3735 copy = frame->format != outlink->format;
175
2/3
✓ Branch 0 taken 3420 times.
✓ Branch 1 taken 315 times.
✗ Branch 2 not taken.
3735 switch (avctx->codec_type) {
176 3420 case AVMEDIA_TYPE_VIDEO:
177
3/4
✓ Branch 0 taken 3420 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 42 times.
✓ Branch 3 taken 3378 times.
3420 if (w != outlink->w || h != outlink->h)
178 42 copy |= 1;
179 3420 break;
180 315 case AVMEDIA_TYPE_AUDIO:
181
2/4
✓ Branch 0 taken 315 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 315 times.
630 if (outlink->sample_rate != frame->sample_rate ||
182 315 av_channel_layout_compare(&outlink->ch_layout, &frame->ch_layout))
183 copy |= 1;
184 315 break;
185 }
186
187
3/4
✓ Branch 0 taken 3693 times.
✓ Branch 1 taken 42 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3693 times.
3735 if (copy || !(avctx->codec->capabilities & AV_CODEC_CAP_DR1))
188 42 return avcodec_default_get_buffer2(avctx, frame, flags);
189
190
2/3
✓ Branch 0 taken 3378 times.
✓ Branch 1 taken 315 times.
✗ Branch 2 not taken.
3693 switch (avctx->codec_type) {
191 3378 case AVMEDIA_TYPE_VIDEO:
192 3378 avcodec_align_dimensions2(avctx, &w, &h, linesize_align);
193 3378 new = ff_default_get_video_buffer(outlink, w, h);
194 3378 break;
195 315 case AVMEDIA_TYPE_AUDIO:
196 315 new = ff_default_get_audio_buffer(outlink, frame->nb_samples);
197 315 break;
198 default:
199 return -1;
200 }
201
202 3693 av_frame_copy_props(new, frame);
203 3693 av_frame_unref(frame);
204 3693 av_frame_move_ref(frame, new);
205 3693 av_frame_free(&new);
206
207 3693 frame->width = ow;
208 3693 frame->height = oh;
209
210 3693 return 0;
211 }
212
213 15 static int open_stream(AVFilterContext *ctx, MovieStream *st, int dec_threads)
214 {
215 const AVCodec *codec;
216 int ret;
217
218 15 codec = avcodec_find_decoder(st->st->codecpar->codec_id);
219
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 if (!codec) {
220 av_log(ctx, AV_LOG_ERROR, "Failed to find any codec\n");
221 return AVERROR(EINVAL);
222 }
223
224 15 st->codec_ctx = avcodec_alloc_context3(codec);
225
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 if (!st->codec_ctx)
226 return AVERROR(ENOMEM);
227
228 15 st->codec_ctx->opaque = st;
229 15 st->codec_ctx->get_buffer2 = get_buffer;
230 15 ret = avcodec_parameters_to_context(st->codec_ctx, st->st->codecpar);
231
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 if (ret < 0)
232 return ret;
233
234
1/2
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
15 if (!dec_threads)
235 15 dec_threads = ff_filter_get_nb_threads(ctx);
236 15 st->codec_ctx->thread_count = dec_threads;
237
238
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 15 times.
15 if ((ret = avcodec_open2(st->codec_ctx, codec, NULL)) < 0) {
239 av_log(ctx, AV_LOG_ERROR, "Failed to open codec\n");
240 return ret;
241 }
242
243 15 return 0;
244 }
245
246 static int guess_channel_layout(MovieStream *st, int st_index, void *log_ctx)
247 {
248 AVCodecParameters *dec_par = st->st->codecpar;
249 char buf[256];
250 AVChannelLayout chl = { 0 };
251
252 av_channel_layout_default(&chl, dec_par->ch_layout.nb_channels);
253
254 if (!KNOWN(&chl)) {
255 av_log(log_ctx, AV_LOG_WARNING,
256 "Channel layout is not set in stream %d, and could not "
257 "be guessed from the number of channels (%d)\n",
258 st_index, dec_par->ch_layout.nb_channels);
259 return av_channel_layout_copy(&dec_par->ch_layout, &chl);
260 }
261
262 av_channel_layout_describe(&chl, buf, sizeof(buf));
263 av_log(log_ctx, AV_LOG_WARNING,
264 "Channel layout is not set in output stream %d, "
265 "guessed channel layout is '%s'\n",
266 st_index, buf);
267 return av_channel_layout_copy(&dec_par->ch_layout, &chl);
268 }
269
270 15 static av_cold int movie_common_init(AVFilterContext *ctx)
271 {
272 15 MovieContext *movie = ctx->priv;
273 15 const AVInputFormat *iformat = NULL;
274 int64_t timestamp;
275 15 int nb_streams = 1, ret, i;
276 char default_streams[16], *stream_specs, *spec, *cursor;
277 AVStream *st;
278
279
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 if (!movie->file_name) {
280 av_log(ctx, AV_LOG_ERROR, "No filename provided!\n");
281 return AVERROR(EINVAL);
282 }
283
284 15 movie->seek_point = movie->seek_point_d * 1000000 + 0.5;
285
286 15 stream_specs = movie->stream_specs;
287
1/2
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
15 if (!stream_specs) {
288 15 snprintf(default_streams, sizeof(default_streams), "d%c%d",
289
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 12 times.
15 !strcmp(ctx->filter->name, "amovie") ? 'a' : 'v',
290 movie->stream_index);
291 15 stream_specs = default_streams;
292 }
293
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 15 times.
75 for (cursor = stream_specs; *cursor; cursor++)
294
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 60 times.
60 if (*cursor == '+')
295 nb_streams++;
296
297
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
15 if (movie->loop_count != 1 && nb_streams != 1) {
298 av_log(ctx, AV_LOG_ERROR,
299 "Loop with several streams is currently unsupported\n");
300 return AVERROR_PATCHWELCOME;
301 }
302
303 // Try to find the movie format (container)
304
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 iformat = movie->format_name ? av_find_input_format(movie->format_name) : NULL;
305
306 15 movie->format_ctx = NULL;
307
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 15 times.
15 if ((ret = avformat_open_input(&movie->format_ctx, movie->file_name, iformat, &movie->format_opts)) < 0) {
308 av_log(ctx, AV_LOG_ERROR,
309 "Failed to avformat_open_input '%s'\n", movie->file_name);
310 return ret;
311 }
312
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 15 times.
15 if ((ret = avformat_find_stream_info(movie->format_ctx, NULL)) < 0)
313 av_log(ctx, AV_LOG_WARNING, "Failed to find stream info\n");
314
315 // if seeking requested, we execute it
316
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 if (movie->seek_point > 0) {
317 timestamp = movie->seek_point;
318 // add the stream start time, should it exist
319 if (movie->format_ctx->start_time != AV_NOPTS_VALUE) {
320 if (timestamp > 0 && movie->format_ctx->start_time > INT64_MAX - timestamp) {
321 av_log(ctx, AV_LOG_ERROR,
322 "%s: seek value overflow with start_time:%"PRId64" seek_point:%"PRId64"\n",
323 movie->file_name, movie->format_ctx->start_time, movie->seek_point);
324 return AVERROR(EINVAL);
325 }
326 timestamp += movie->format_ctx->start_time;
327 }
328 if ((ret = av_seek_frame(movie->format_ctx, -1, timestamp, AVSEEK_FLAG_BACKWARD)) < 0) {
329 av_log(ctx, AV_LOG_ERROR, "%s: could not seek to position %"PRId64"\n",
330 movie->file_name, timestamp);
331 return ret;
332 }
333 }
334
335
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 15 times.
33 for (i = 0; i < movie->format_ctx->nb_streams; i++)
336 18 movie->format_ctx->streams[i]->discard = AVDISCARD_ALL;
337
338 15 movie->pkt = av_packet_alloc();
339
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 if (!movie->pkt)
340 return AVERROR(ENOMEM);
341 15 movie->st = av_calloc(nb_streams, sizeof(*movie->st));
342
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 if (!movie->st)
343 return AVERROR(ENOMEM);
344
345
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 15 times.
30 for (i = 0; i < nb_streams; i++) {
346 15 spec = av_strtok(stream_specs, "+", &cursor);
347
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 if (!spec)
348 return AVERROR_BUG;
349 15 stream_specs = NULL; /* for next strtok */
350 15 st = find_stream(ctx, movie->format_ctx, spec);
351
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 if (!st)
352 return AVERROR(EINVAL);
353 15 st->discard = AVDISCARD_DEFAULT;
354 15 movie->st[i].st = st;
355 15 movie->max_stream_index = FFMAX(movie->max_stream_index, st->index);
356 15 movie->st[i].discontinuity_threshold =
357 15 av_rescale_q(movie->discontinuity_threshold, AV_TIME_BASE_Q, st->time_base);
358
359 15 movie->st[i].frame = av_frame_alloc();
360
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 if (!movie->st[i].frame)
361 return AVERROR(ENOMEM);
362 }
363
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 15 times.
15 if (av_strtok(NULL, "+", &cursor))
364 return AVERROR_BUG;
365
366 15 movie->out_index = av_calloc(movie->max_stream_index + 1,
367 sizeof(*movie->out_index));
368
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 if (!movie->out_index)
369 return AVERROR(ENOMEM);
370
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 15 times.
30 for (i = 0; i <= movie->max_stream_index; i++)
371 15 movie->out_index[i] = -1;
372
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 15 times.
30 for (i = 0; i < nb_streams; i++) {
373 15 AVFilterPad pad = { 0 };
374 15 movie->out_index[movie->st[i].st->index] = i;
375 15 pad.type = movie->st[i].st->codecpar->codec_type;
376 15 pad.name = av_asprintf("out%d", i);
377
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 if (!pad.name)
378 return AVERROR(ENOMEM);
379 15 pad.config_props = movie_config_output_props;
380
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 15 times.
15 if ((ret = ff_append_outpad_free_name(ctx, &pad)) < 0)
381 return ret;
382
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 12 times.
15 if ( movie->st[i].st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
383
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
3 !KNOWN(&movie->st[i].st->codecpar->ch_layout)) {
384 ret = guess_channel_layout(&movie->st[i], i, ctx);
385 if (ret < 0)
386 return ret;
387 }
388 15 ret = open_stream(ctx, &movie->st[i], movie->dec_threads);
389
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 if (ret < 0)
390 return ret;
391 }
392
393 15 av_log(ctx, AV_LOG_VERBOSE, "seek_point:%"PRIi64" format_name:%s file_name:%s stream_index:%d\n",
394 movie->seek_point, movie->format_name, movie->file_name,
395 movie->stream_index);
396
397 15 return 0;
398 }
399
400 15 static av_cold void movie_uninit(AVFilterContext *ctx)
401 {
402 15 MovieContext *movie = ctx->priv;
403 int i;
404
405
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 15 times.
30 for (i = 0; i < ctx->nb_outputs; i++) {
406
1/2
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
15 if (movie->st[i].st)
407 15 avcodec_free_context(&movie->st[i].codec_ctx);
408 15 av_frame_free(&movie->st[i].frame);
409 }
410 15 av_packet_free(&movie->pkt);
411 15 av_freep(&movie->st);
412 15 av_freep(&movie->out_index);
413
1/2
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
15 if (movie->format_ctx)
414 15 avformat_close_input(&movie->format_ctx);
415 15 }
416
417 15 static int movie_query_formats(AVFilterContext *ctx)
418 {
419 15 MovieContext *movie = ctx->priv;
420 15 int list[] = { 0, -1 };
421 15 AVChannelLayout list64[] = { { 0 }, { 0 } };
422 int i, ret;
423
424
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 15 times.
30 for (i = 0; i < ctx->nb_outputs; i++) {
425 15 MovieStream *st = &movie->st[i];
426 15 AVCodecParameters *c = st->st->codecpar;
427 15 AVFilterLink *outlink = ctx->outputs[i];
428
429
2/3
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
15 switch (c->codec_type) {
430 12 case AVMEDIA_TYPE_VIDEO:
431 12 list[0] = c->format;
432
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 12 times.
12 if ((ret = ff_formats_ref(ff_make_format_list(list), &outlink->incfg.formats)) < 0)
433 return ret;
434 12 break;
435 3 case AVMEDIA_TYPE_AUDIO:
436 3 list[0] = c->format;
437
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
3 if ((ret = ff_formats_ref(ff_make_format_list(list), &outlink->incfg.formats)) < 0)
438 return ret;
439 3 list[0] = c->sample_rate;
440
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
3 if ((ret = ff_formats_ref(ff_make_format_list(list), &outlink->incfg.samplerates)) < 0)
441 return ret;
442 3 list64[0] = c->ch_layout;
443
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
3 if ((ret = ff_channel_layouts_ref(ff_make_channel_layout_list(list64),
444 &outlink->incfg.channel_layouts)) < 0)
445 return ret;
446 3 break;
447 }
448 }
449
450 15 return 0;
451 }
452
453 15 static int movie_config_output_props(AVFilterLink *outlink)
454 {
455 15 AVFilterContext *ctx = outlink->src;
456 15 MovieContext *movie = ctx->priv;
457 15 unsigned out_id = FF_OUTLINK_IDX(outlink);
458 15 MovieStream *st = &movie->st[out_id];
459 15 AVCodecParameters *c = st->st->codecpar;
460
461 15 outlink->time_base = st->st->time_base;
462
463
2/3
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
15 switch (c->codec_type) {
464 12 case AVMEDIA_TYPE_VIDEO:
465 12 outlink->w = c->width;
466 12 outlink->h = c->height;
467 12 outlink->frame_rate = st->st->r_frame_rate;
468 12 break;
469 3 case AVMEDIA_TYPE_AUDIO:
470 3 break;
471 }
472
473 15 st->link = outlink;
474
475 15 return 0;
476 }
477
478 static int rewind_file(AVFilterContext *ctx)
479 {
480 MovieContext *movie = ctx->priv;
481 int64_t timestamp = movie->seek_point;
482 int ret, i;
483
484 if (movie->format_ctx->start_time != AV_NOPTS_VALUE)
485 timestamp += movie->format_ctx->start_time;
486 ret = av_seek_frame(movie->format_ctx, -1, timestamp, AVSEEK_FLAG_BACKWARD);
487 if (ret < 0) {
488 av_log(ctx, AV_LOG_ERROR, "Unable to loop: %s\n", av_err2str(ret));
489 movie->loop_count = 1; /* do not try again */
490 return ret;
491 }
492
493 for (i = 0; i < ctx->nb_outputs; i++) {
494 avcodec_flush_buffers(movie->st[i].codec_ctx);
495 }
496 return 0;
497 }
498
499 15 static int flush_decoder(AVFilterContext *ctx, int i)
500 {
501 15 MovieContext *movie = ctx->priv;
502 15 AVCodecContext *dec = movie->st[i].codec_ctx;
503
504 15 return avcodec_send_packet(dec, NULL);
505 }
506
507 3829 static int decode_packet(AVFilterContext *ctx, int i)
508 {
509 3829 AVFilterLink *outlink = ctx->outputs[i];
510 3829 MovieContext *movie = ctx->priv;
511 3829 MovieStream *st = &movie->st[i];
512 3829 AVCodecContext *dec = movie->st[i].codec_ctx;
513 3829 AVFrame *frame = movie->st[i].frame;
514 3829 AVPacket *pkt = movie->pkt;
515 3829 int ret = 0;
516
517 // submit the packet to the decoder
518
2/2
✓ Branch 0 taken 3745 times.
✓ Branch 1 taken 84 times.
3829 if (!movie->eof) {
519 3745 ret = avcodec_send_packet(dec, pkt);
520
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3745 times.
3745 if (ret < 0)
521 return ret;
522 }
523
524 // get all the available frames from the decoder
525
1/2
✓ Branch 0 taken 3829 times.
✗ Branch 1 not taken.
3829 if (ret >= 0) {
526 3829 ret = avcodec_receive_frame(dec, frame);
527
2/2
✓ Branch 0 taken 94 times.
✓ Branch 1 taken 3735 times.
3829 if (ret < 0) {
528 // those two return values are special and mean there is no output
529 // frame available, but there were no errors during decoding
530
3/4
✓ Branch 0 taken 79 times.
✓ Branch 1 taken 15 times.
✓ Branch 2 taken 79 times.
✗ Branch 3 not taken.
94 if (ret == AVERROR_EOF || ret == AVERROR(EAGAIN))
531 94 return 0;
532 return ret;
533 }
534
535 3735 frame->pts = frame->best_effort_timestamp;
536
2/2
✓ Branch 0 taken 3731 times.
✓ Branch 1 taken 4 times.
3735 if (frame->pts != AV_NOPTS_VALUE) {
537
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3731 times.
3731 if (movie->ts_offset)
538 frame->pts += av_rescale_q_rnd(movie->ts_offset, AV_TIME_BASE_Q, outlink->time_base, AV_ROUND_UP);
539
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3731 times.
3731 if (st->discontinuity_threshold) {
540 if (st->last_pts != AV_NOPTS_VALUE) {
541 int64_t diff = frame->pts - st->last_pts;
542 if (diff < 0 || diff > st->discontinuity_threshold) {
543 av_log(ctx, AV_LOG_VERBOSE, "Discontinuity in stream:%d diff:%"PRId64"\n", i, diff);
544 movie->ts_offset += av_rescale_q_rnd(-diff, outlink->time_base, AV_TIME_BASE_Q, AV_ROUND_UP);
545 frame->pts -= diff;
546 }
547 }
548 }
549 3731 st->last_pts = frame->pts;
550 }
551 3735 ret = ff_filter_frame(outlink, av_frame_clone(frame));
552
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3735 times.
3735 if (ret < 0)
553 return ret;
554
1/2
✓ Branch 0 taken 3735 times.
✗ Branch 1 not taken.
3735 if (ret == 0)
555 3735 return 1;
556 }
557
558 return 0;
559 }
560
561 4399 static int activate(AVFilterContext *ctx)
562 {
563 4399 MovieContext *movie = ctx->priv;
564 4399 int wanted = 0, ret;
565
566
2/2
✓ Branch 0 taken 4399 times.
✓ Branch 1 taken 4399 times.
8798 for (int i = 0; i < ctx->nb_outputs; i++) {
567
2/2
✓ Branch 1 taken 4065 times.
✓ Branch 2 taken 334 times.
4399 if (ff_outlink_frame_wanted(ctx->outputs[i]))
568 4065 wanted++;
569 }
570
571
2/2
✓ Branch 0 taken 334 times.
✓ Branch 1 taken 4065 times.
4399 if (wanted == 0)
572 334 return FFERROR_NOT_READY;
573
574
2/2
✓ Branch 0 taken 3981 times.
✓ Branch 1 taken 84 times.
4065 if (!movie->eof) {
575 3981 ret = av_read_frame(movie->format_ctx, movie->pkt);
576
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 3966 times.
3981 if (ret < 0) {
577 15 movie->eof = 1;
578
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 15 times.
30 for (int i = 0; i < ctx->nb_outputs; i++)
579 15 flush_decoder(ctx, i);
580 15 ff_filter_set_ready(ctx, 100);
581 15 return 0;
582 } else {
583
2/2
✓ Branch 0 taken 3745 times.
✓ Branch 1 taken 221 times.
3966 int pkt_out_id = movie->pkt->stream_index > movie->max_stream_index ? -1 :
584 3745 movie->out_index[movie->pkt->stream_index];
585
586
2/2
✓ Branch 0 taken 3745 times.
✓ Branch 1 taken 221 times.
3966 if (pkt_out_id >= 0) {
587 3745 ret = decode_packet(ctx, pkt_out_id);
588 }
589 3966 av_packet_unref(movie->pkt);
590 3966 ff_filter_set_ready(ctx, 100);
591 3966 return (ret <= 0) ? ret : 0;
592 }
593 } else {
594 84 int nb_eofs = 0;
595
596
2/2
✓ Branch 0 taken 84 times.
✓ Branch 1 taken 84 times.
168 for (int i = 0; i < ctx->nb_outputs; i++) {
597
1/2
✓ Branch 0 taken 84 times.
✗ Branch 1 not taken.
84 if (!movie->st[i].eof) {
598 84 ret = decode_packet(ctx, i);
599
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 69 times.
84 if (ret <= 0)
600 15 movie->st[i].eof = 1;
601 }
602 84 nb_eofs += movie->st[i].eof == 1;
603 }
604
3/4
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 69 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 15 times.
84 if (nb_eofs == ctx->nb_outputs && movie->loop_count != 1) {
605 ret = rewind_file(ctx);
606 if (ret < 0)
607 return ret;
608 movie->loop_count -= movie->loop_count > 1;
609 av_log(ctx, AV_LOG_VERBOSE, "Stream finished, looping.\n");
610 ff_filter_set_ready(ctx, 100);
611 for (int i = 0; i < ctx->nb_outputs; i++)
612 movie->st[i].eof = 0;
613 movie->eof = 0;
614 return 0;
615 } else {
616
2/2
✓ Branch 0 taken 84 times.
✓ Branch 1 taken 84 times.
168 for (int i = 0; i < ctx->nb_outputs; i++) {
617
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 69 times.
84 if (movie->st[i].eof) {
618 15 ff_outlink_set_status(ctx->outputs[i], AVERROR_EOF, movie->st[i].last_pts);
619 15 nb_eofs++;
620 }
621 }
622 }
623
624
2/2
✓ Branch 0 taken 69 times.
✓ Branch 1 taken 15 times.
84 if (nb_eofs < ctx->nb_outputs)
625 69 ff_filter_set_ready(ctx, 100);
626 84 return 0;
627 }
628
629 return FFERROR_NOT_READY;
630 }
631
632 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
633 char *res, int res_len, int flags)
634 {
635 MovieContext *movie = ctx->priv;
636 int ret = AVERROR(ENOSYS);
637
638 if (!strcmp(cmd, "seek")) {
639 int idx, flags, i;
640 int64_t ts;
641 char tail[2];
642
643 if (sscanf(args, "%i|%"SCNi64"|%i %1s", &idx, &ts, &flags, tail) != 3)
644 return AVERROR(EINVAL);
645
646 ret = av_seek_frame(movie->format_ctx, idx, ts, flags);
647 if (ret < 0)
648 return ret;
649
650 for (i = 0; i < ctx->nb_outputs; i++) {
651 avcodec_flush_buffers(movie->st[i].codec_ctx);
652 }
653 return ret;
654 } else if (!strcmp(cmd, "get_duration")) {
655 int print_len;
656 char tail[2];
657
658 if (!res || res_len <= 0)
659 return AVERROR(EINVAL);
660
661 if (args && sscanf(args, "%1s", tail) == 1)
662 return AVERROR(EINVAL);
663
664 print_len = snprintf(res, res_len, "%"PRId64, movie->format_ctx->duration);
665 if (print_len < 0 || print_len >= res_len)
666 return AVERROR(EINVAL);
667
668 return 0;
669 }
670
671 return ret;
672 }
673
674 AVFILTER_DEFINE_CLASS_EXT(movie, "(a)movie", movie_options);
675
676 #if CONFIG_MOVIE_FILTER
677
678 const AVFilter ff_avsrc_movie = {
679 .name = "movie",
680 .description = NULL_IF_CONFIG_SMALL("Read from a movie source."),
681 .priv_size = sizeof(MovieContext),
682 .priv_class = &movie_class,
683 .init = movie_common_init,
684 .activate = activate,
685 .uninit = movie_uninit,
686 FILTER_QUERY_FUNC(movie_query_formats),
687
688 .inputs = NULL,
689 .outputs = NULL,
690 .flags = AVFILTER_FLAG_DYNAMIC_OUTPUTS,
691 .process_command = process_command
692 };
693
694 #endif /* CONFIG_MOVIE_FILTER */
695
696 #if CONFIG_AMOVIE_FILTER
697
698 const AVFilter ff_avsrc_amovie = {
699 .name = "amovie",
700 .description = NULL_IF_CONFIG_SMALL("Read audio from a movie source."),
701 .priv_class = &movie_class,
702 .priv_size = sizeof(MovieContext),
703 .init = movie_common_init,
704 .activate = activate,
705 .uninit = movie_uninit,
706 FILTER_QUERY_FUNC(movie_query_formats),
707
708 .inputs = NULL,
709 .outputs = NULL,
710 .flags = AVFILTER_FLAG_DYNAMIC_OUTPUTS,
711 .process_command = process_command,
712 };
713
714 #endif /* CONFIG_AMOVIE_FILTER */
715