FFmpeg coverage


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