FFmpeg coverage


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