FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
src_movie.c
Go to the documentation of this file.
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 #include "libavformat/avformat.h"
41 #include "audio.h"
42 #include "avfilter.h"
43 #include "formats.h"
44 #include "internal.h"
45 #include "video.h"
46 
47 typedef struct MovieStream {
50  int done;
52  int64_t last_pts;
53 } MovieStream;
54 
55 typedef struct MovieContext {
56  /* common A/V fields */
57  const AVClass *class;
58  int64_t seek_point; ///< seekpoint in microseconds
59  double seek_point_d;
60  char *format_name;
61  char *file_name;
62  char *stream_specs; /**< user-provided list of streams, separated by + */
63  int stream_index; /**< for compatibility */
66  int64_t ts_offset;
67 
69  int eof;
71 
72  int max_stream_index; /**< max stream # actually used for output */
73  MovieStream *st; /**< array of all streams, one per output */
74  int *out_index; /**< stream number -> output number map, or -1 */
75 } MovieContext;
76 
77 #define OFFSET(x) offsetof(MovieContext, x)
78 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_VIDEO_PARAM
79 
80 static const AVOption movie_options[]= {
81  { "filename", NULL, OFFSET(file_name), AV_OPT_TYPE_STRING, .flags = FLAGS },
82  { "format_name", "set format name", OFFSET(format_name), AV_OPT_TYPE_STRING, .flags = FLAGS },
83  { "f", "set format name", OFFSET(format_name), AV_OPT_TYPE_STRING, .flags = FLAGS },
84  { "stream_index", "set stream index", OFFSET(stream_index), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS },
85  { "si", "set stream index", OFFSET(stream_index), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS },
86  { "seek_point", "set seekpoint (seconds)", OFFSET(seek_point_d), AV_OPT_TYPE_DOUBLE, { .dbl = 0 }, 0, (INT64_MAX-1) / 1000000, FLAGS },
87  { "sp", "set seekpoint (seconds)", OFFSET(seek_point_d), AV_OPT_TYPE_DOUBLE, { .dbl = 0 }, 0, (INT64_MAX-1) / 1000000, FLAGS },
88  { "streams", "set streams", OFFSET(stream_specs), AV_OPT_TYPE_STRING, {.str = 0}, CHAR_MAX, CHAR_MAX, FLAGS },
89  { "s", "set streams", OFFSET(stream_specs), AV_OPT_TYPE_STRING, {.str = 0}, CHAR_MAX, CHAR_MAX, FLAGS },
90  { "loop", "set loop count", OFFSET(loop_count), AV_OPT_TYPE_INT, {.i64 = 1}, 0, INT_MAX, FLAGS },
91  { "discontinuity", "set discontinuity threshold", OFFSET(discontinuity_threshold), AV_OPT_TYPE_DURATION, {.i64 = 0}, 0, INT64_MAX, FLAGS },
92  { NULL },
93 };
94 
95 static int movie_config_output_props(AVFilterLink *outlink);
96 static int movie_request_frame(AVFilterLink *outlink);
97 
98 static AVStream *find_stream(void *log, AVFormatContext *avf, const char *spec)
99 {
100  int i, ret, already = 0, stream_id = -1;
101  char type_char[2], dummy;
102  AVStream *found = NULL;
103  enum AVMediaType type;
104 
105  ret = sscanf(spec, "d%1[av]%d%c", type_char, &stream_id, &dummy);
106  if (ret >= 1 && ret <= 2) {
107  type = type_char[0] == 'v' ? AVMEDIA_TYPE_VIDEO : AVMEDIA_TYPE_AUDIO;
108  ret = av_find_best_stream(avf, type, stream_id, -1, NULL, 0);
109  if (ret < 0) {
110  av_log(log, AV_LOG_ERROR, "No %s stream with index '%d' found\n",
111  av_get_media_type_string(type), stream_id);
112  return NULL;
113  }
114  return avf->streams[ret];
115  }
116  for (i = 0; i < avf->nb_streams; i++) {
117  ret = avformat_match_stream_specifier(avf, avf->streams[i], spec);
118  if (ret < 0) {
119  av_log(log, AV_LOG_ERROR,
120  "Invalid stream specifier \"%s\"\n", spec);
121  return NULL;
122  }
123  if (!ret)
124  continue;
125  if (avf->streams[i]->discard != AVDISCARD_ALL) {
126  already++;
127  continue;
128  }
129  if (found) {
130  av_log(log, AV_LOG_WARNING,
131  "Ambiguous stream specifier \"%s\", using #%d\n", spec, i);
132  break;
133  }
134  found = avf->streams[i];
135  }
136  if (!found) {
137  av_log(log, AV_LOG_WARNING, "Stream specifier \"%s\" %s\n", spec,
138  already ? "matched only already used streams" :
139  "did not match any stream");
140  return NULL;
141  }
142  if (found->codecpar->codec_type != AVMEDIA_TYPE_VIDEO &&
144  av_log(log, AV_LOG_ERROR, "Stream specifier \"%s\" matched a %s stream,"
145  "currently unsupported by libavfilter\n", spec,
147  return NULL;
148  }
149  return found;
150 }
151 
152 static int open_stream(void *log, MovieStream *st)
153 {
154  AVCodec *codec;
155  int ret;
156 
157  codec = avcodec_find_decoder(st->st->codecpar->codec_id);
158  if (!codec) {
159  av_log(log, AV_LOG_ERROR, "Failed to find any codec\n");
160  return AVERROR(EINVAL);
161  }
162 
163  st->codec_ctx = avcodec_alloc_context3(codec);
164  if (!st->codec_ctx)
165  return AVERROR(ENOMEM);
166 
168  if (ret < 0)
169  return ret;
170 
171  st->codec_ctx->refcounted_frames = 1;
172 
173  if ((ret = avcodec_open2(st->codec_ctx, codec, NULL)) < 0) {
174  av_log(log, AV_LOG_ERROR, "Failed to open codec\n");
175  return ret;
176  }
177 
178  return 0;
179 }
180 
181 static int guess_channel_layout(MovieStream *st, int st_index, void *log_ctx)
182 {
183  AVCodecParameters *dec_par = st->st->codecpar;
184  char buf[256];
185  int64_t chl = av_get_default_channel_layout(dec_par->channels);
186 
187  if (!chl) {
188  av_log(log_ctx, AV_LOG_ERROR,
189  "Channel layout is not set in stream %d, and could not "
190  "be guessed from the number of channels (%d)\n",
191  st_index, dec_par->channels);
192  return AVERROR(EINVAL);
193  }
194 
195  av_get_channel_layout_string(buf, sizeof(buf), dec_par->channels, chl);
196  av_log(log_ctx, AV_LOG_WARNING,
197  "Channel layout is not set in output stream %d, "
198  "guessed channel layout is '%s'\n",
199  st_index, buf);
200  dec_par->channel_layout = chl;
201  return 0;
202 }
203 
205 {
206  MovieContext *movie = ctx->priv;
208  int64_t timestamp;
209  int nb_streams = 1, ret, i;
210  char default_streams[16], *stream_specs, *spec, *cursor;
211  char name[16];
212  AVStream *st;
213 
214  if (!movie->file_name) {
215  av_log(ctx, AV_LOG_ERROR, "No filename provided!\n");
216  return AVERROR(EINVAL);
217  }
218 
219  movie->seek_point = movie->seek_point_d * 1000000 + 0.5;
220 
221  stream_specs = movie->stream_specs;
222  if (!stream_specs) {
223  snprintf(default_streams, sizeof(default_streams), "d%c%d",
224  !strcmp(ctx->filter->name, "amovie") ? 'a' : 'v',
225  movie->stream_index);
226  stream_specs = default_streams;
227  }
228  for (cursor = stream_specs; *cursor; cursor++)
229  if (*cursor == '+')
230  nb_streams++;
231 
232  if (movie->loop_count != 1 && nb_streams != 1) {
233  av_log(ctx, AV_LOG_ERROR,
234  "Loop with several streams is currently unsupported\n");
235  return AVERROR_PATCHWELCOME;
236  }
237 
238  av_register_all();
239 
240  // Try to find the movie format (container)
241  iformat = movie->format_name ? av_find_input_format(movie->format_name) : NULL;
242 
243  movie->format_ctx = NULL;
244  if ((ret = avformat_open_input(&movie->format_ctx, movie->file_name, iformat, NULL)) < 0) {
245  av_log(ctx, AV_LOG_ERROR,
246  "Failed to avformat_open_input '%s'\n", movie->file_name);
247  return ret;
248  }
249  if ((ret = avformat_find_stream_info(movie->format_ctx, NULL)) < 0)
250  av_log(ctx, AV_LOG_WARNING, "Failed to find stream info\n");
251 
252  // if seeking requested, we execute it
253  if (movie->seek_point > 0) {
254  timestamp = movie->seek_point;
255  // add the stream start time, should it exist
256  if (movie->format_ctx->start_time != AV_NOPTS_VALUE) {
257  if (timestamp > 0 && movie->format_ctx->start_time > INT64_MAX - timestamp) {
258  av_log(ctx, AV_LOG_ERROR,
259  "%s: seek value overflow with start_time:%"PRId64" seek_point:%"PRId64"\n",
260  movie->file_name, movie->format_ctx->start_time, movie->seek_point);
261  return AVERROR(EINVAL);
262  }
263  timestamp += movie->format_ctx->start_time;
264  }
265  if ((ret = av_seek_frame(movie->format_ctx, -1, timestamp, AVSEEK_FLAG_BACKWARD)) < 0) {
266  av_log(ctx, AV_LOG_ERROR, "%s: could not seek to position %"PRId64"\n",
267  movie->file_name, timestamp);
268  return ret;
269  }
270  }
271 
272  for (i = 0; i < movie->format_ctx->nb_streams; i++)
273  movie->format_ctx->streams[i]->discard = AVDISCARD_ALL;
274 
275  movie->st = av_calloc(nb_streams, sizeof(*movie->st));
276  if (!movie->st)
277  return AVERROR(ENOMEM);
278 
279  for (i = 0; i < nb_streams; i++) {
280  spec = av_strtok(stream_specs, "+", &cursor);
281  if (!spec)
282  return AVERROR_BUG;
283  stream_specs = NULL; /* for next strtok */
284  st = find_stream(ctx, movie->format_ctx, spec);
285  if (!st)
286  return AVERROR(EINVAL);
288  movie->st[i].st = st;
289  movie->max_stream_index = FFMAX(movie->max_stream_index, st->index);
290  movie->st[i].discontinuity_threshold =
292  }
293  if (av_strtok(NULL, "+", &cursor))
294  return AVERROR_BUG;
295 
296  movie->out_index = av_calloc(movie->max_stream_index + 1,
297  sizeof(*movie->out_index));
298  if (!movie->out_index)
299  return AVERROR(ENOMEM);
300  for (i = 0; i <= movie->max_stream_index; i++)
301  movie->out_index[i] = -1;
302  for (i = 0; i < nb_streams; i++) {
303  AVFilterPad pad = { 0 };
304  movie->out_index[movie->st[i].st->index] = i;
305  snprintf(name, sizeof(name), "out%d", i);
306  pad.type = movie->st[i].st->codecpar->codec_type;
307  pad.name = av_strdup(name);
308  if (!pad.name)
309  return AVERROR(ENOMEM);
312  ff_insert_outpad(ctx, i, &pad);
313  if ( movie->st[i].st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
314  !movie->st[i].st->codecpar->channel_layout) {
315  ret = guess_channel_layout(&movie->st[i], i, ctx);
316  if (ret < 0)
317  return ret;
318  }
319  ret = open_stream(ctx, &movie->st[i]);
320  if (ret < 0)
321  return ret;
322  }
323 
324  av_log(ctx, AV_LOG_VERBOSE, "seek_point:%"PRIi64" format_name:%s file_name:%s stream_index:%d\n",
325  movie->seek_point, movie->format_name, movie->file_name,
326  movie->stream_index);
327 
328  return 0;
329 }
330 
332 {
333  MovieContext *movie = ctx->priv;
334  int i;
335 
336  for (i = 0; i < ctx->nb_outputs; i++) {
337  av_freep(&ctx->output_pads[i].name);
338  if (movie->st[i].st)
339  avcodec_free_context(&movie->st[i].codec_ctx);
340  }
341  av_freep(&movie->st);
342  av_freep(&movie->out_index);
343  if (movie->format_ctx)
345 }
346 
348 {
349  MovieContext *movie = ctx->priv;
350  int list[] = { 0, -1 };
351  int64_t list64[] = { 0, -1 };
352  int i, ret;
353 
354  for (i = 0; i < ctx->nb_outputs; i++) {
355  MovieStream *st = &movie->st[i];
356  AVCodecParameters *c = st->st->codecpar;
357  AVFilterLink *outlink = ctx->outputs[i];
358 
359  switch (c->codec_type) {
360  case AVMEDIA_TYPE_VIDEO:
361  list[0] = c->format;
362  if ((ret = ff_formats_ref(ff_make_format_list(list), &outlink->in_formats)) < 0)
363  return ret;
364  break;
365  case AVMEDIA_TYPE_AUDIO:
366  list[0] = c->format;
367  if ((ret = ff_formats_ref(ff_make_format_list(list), &outlink->in_formats)) < 0)
368  return ret;
369  list[0] = c->sample_rate;
370  if ((ret = ff_formats_ref(ff_make_format_list(list), &outlink->in_samplerates)) < 0)
371  return ret;
372  list64[0] = c->channel_layout;
374  &outlink->in_channel_layouts)) < 0)
375  return ret;
376  break;
377  }
378  }
379 
380  return 0;
381 }
382 
384 {
385  AVFilterContext *ctx = outlink->src;
386  MovieContext *movie = ctx->priv;
387  unsigned out_id = FF_OUTLINK_IDX(outlink);
388  MovieStream *st = &movie->st[out_id];
389  AVCodecParameters *c = st->st->codecpar;
390 
391  outlink->time_base = st->st->time_base;
392 
393  switch (c->codec_type) {
394  case AVMEDIA_TYPE_VIDEO:
395  outlink->w = c->width;
396  outlink->h = c->height;
397  outlink->frame_rate = st->st->r_frame_rate;
398  break;
399  case AVMEDIA_TYPE_AUDIO:
400  break;
401  }
402 
403  return 0;
404 }
405 
406 static char *describe_frame_to_str(char *dst, size_t dst_size,
407  AVFrame *frame, enum AVMediaType frame_type,
408  AVFilterLink *link)
409 {
410  switch (frame_type) {
411  case AVMEDIA_TYPE_VIDEO:
412  snprintf(dst, dst_size,
413  "video pts:%s time:%s size:%dx%d aspect:%d/%d",
414  av_ts2str(frame->pts), av_ts2timestr(frame->pts, &link->time_base),
415  frame->width, frame->height,
416  frame->sample_aspect_ratio.num,
417  frame->sample_aspect_ratio.den);
418  break;
419  case AVMEDIA_TYPE_AUDIO:
420  snprintf(dst, dst_size,
421  "audio pts:%s time:%s samples:%d",
422  av_ts2str(frame->pts), av_ts2timestr(frame->pts, &link->time_base),
423  frame->nb_samples);
424  break;
425  default:
426  snprintf(dst, dst_size, "%s BUG", av_get_media_type_string(frame_type));
427  break;
428  }
429  return dst;
430 }
431 
433 {
434  MovieContext *movie = ctx->priv;
435  int64_t timestamp = movie->seek_point;
436  int ret, i;
437 
438  if (movie->format_ctx->start_time != AV_NOPTS_VALUE)
439  timestamp += movie->format_ctx->start_time;
440  ret = av_seek_frame(movie->format_ctx, -1, timestamp, AVSEEK_FLAG_BACKWARD);
441  if (ret < 0) {
442  av_log(ctx, AV_LOG_ERROR, "Unable to loop: %s\n", av_err2str(ret));
443  movie->loop_count = 1; /* do not try again */
444  return ret;
445  }
446 
447  for (i = 0; i < ctx->nb_outputs; i++) {
449  movie->st[i].done = 0;
450  }
451  movie->eof = 0;
452  return 0;
453 }
454 
455 /**
456  * Try to push a frame to the requested output.
457  *
458  * @param ctx filter context
459  * @param out_id number of output where a frame is wanted;
460  * if the frame is read from file, used to set the return value;
461  * if the codec is being flushed, flush the corresponding stream
462  * @return 1 if a frame was pushed on the requested output,
463  * 0 if another attempt is possible,
464  * <0 AVERROR code
465  */
466 static int movie_push_frame(AVFilterContext *ctx, unsigned out_id)
467 {
468  MovieContext *movie = ctx->priv;
469  AVPacket *pkt = &movie->pkt;
470  enum AVMediaType frame_type;
471  MovieStream *st;
472  int ret, got_frame = 0, pkt_out_id;
473  AVFilterLink *outlink;
474  AVFrame *frame;
475 
476  if (!pkt->size) {
477  if (movie->eof) {
478  if (movie->st[out_id].done) {
479  if (movie->loop_count != 1) {
480  ret = rewind_file(ctx);
481  if (ret < 0)
482  return ret;
483  movie->loop_count -= movie->loop_count > 1;
484  av_log(ctx, AV_LOG_VERBOSE, "Stream finished, looping.\n");
485  return 0; /* retry */
486  }
487  return AVERROR_EOF;
488  }
489  pkt->stream_index = movie->st[out_id].st->index;
490  /* packet is already ready for flushing */
491  } else {
492  ret = av_read_frame(movie->format_ctx, &movie->pkt0);
493  if (ret < 0) {
494  av_init_packet(&movie->pkt0); /* ready for flushing */
495  *pkt = movie->pkt0;
496  if (ret == AVERROR_EOF) {
497  movie->eof = 1;
498  return 0; /* start flushing */
499  }
500  return ret;
501  }
502  *pkt = movie->pkt0;
503  }
504  }
505 
506  pkt_out_id = pkt->stream_index > movie->max_stream_index ? -1 :
507  movie->out_index[pkt->stream_index];
508  if (pkt_out_id < 0) {
509  av_packet_unref(&movie->pkt0);
510  pkt->size = 0; /* ready for next run */
511  pkt->data = NULL;
512  return 0;
513  }
514  st = &movie->st[pkt_out_id];
515  outlink = ctx->outputs[pkt_out_id];
516 
517  frame = av_frame_alloc();
518  if (!frame)
519  return AVERROR(ENOMEM);
520 
521  frame_type = st->st->codecpar->codec_type;
522  switch (frame_type) {
523  case AVMEDIA_TYPE_VIDEO:
524  ret = avcodec_decode_video2(st->codec_ctx, frame, &got_frame, pkt);
525  break;
526  case AVMEDIA_TYPE_AUDIO:
527  ret = avcodec_decode_audio4(st->codec_ctx, frame, &got_frame, pkt);
528  break;
529  default:
530  ret = AVERROR(ENOSYS);
531  break;
532  }
533  if (ret < 0) {
534  av_log(ctx, AV_LOG_WARNING, "Decode error: %s\n", av_err2str(ret));
535  av_frame_free(&frame);
536  av_packet_unref(&movie->pkt0);
537  movie->pkt.size = 0;
538  movie->pkt.data = NULL;
539  return 0;
540  }
541  if (!ret || st->st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
542  ret = pkt->size;
543 
544  pkt->data += ret;
545  pkt->size -= ret;
546  if (pkt->size <= 0) {
547  av_packet_unref(&movie->pkt0);
548  pkt->size = 0; /* ready for next run */
549  pkt->data = NULL;
550  }
551  if (!got_frame) {
552  if (!ret)
553  st->done = 1;
554  av_frame_free(&frame);
555  return 0;
556  }
557 
558  frame->pts = av_frame_get_best_effort_timestamp(frame);
559  if (frame->pts != AV_NOPTS_VALUE) {
560  if (movie->ts_offset)
561  frame->pts += av_rescale_q_rnd(movie->ts_offset, AV_TIME_BASE_Q, outlink->time_base, AV_ROUND_UP);
562  if (st->discontinuity_threshold) {
563  if (st->last_pts != AV_NOPTS_VALUE) {
564  int64_t diff = frame->pts - st->last_pts;
565  if (diff < 0 || diff > st->discontinuity_threshold) {
566  av_log(ctx, AV_LOG_VERBOSE, "Discontinuity in stream:%d diff:%"PRId64"\n", pkt_out_id, diff);
567  movie->ts_offset += av_rescale_q_rnd(-diff, outlink->time_base, AV_TIME_BASE_Q, AV_ROUND_UP);
568  frame->pts -= diff;
569  }
570  }
571  }
572  st->last_pts = frame->pts;
573  }
574  ff_dlog(ctx, "movie_push_frame(): file:'%s' %s\n", movie->file_name,
575  describe_frame_to_str((char[1024]){0}, 1024, frame, frame_type, outlink));
576 
577  if (st->st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
578  if (frame->format != outlink->format) {
579  av_log(ctx, AV_LOG_ERROR, "Format changed %s -> %s, discarding frame\n",
580  av_get_pix_fmt_name(outlink->format),
582  );
583  av_frame_free(&frame);
584  return 0;
585  }
586  }
587  ret = ff_filter_frame(outlink, frame);
588 
589  if (ret < 0)
590  return ret;
591  return pkt_out_id == out_id;
592 }
593 
594 static int movie_request_frame(AVFilterLink *outlink)
595 {
596  AVFilterContext *ctx = outlink->src;
597  unsigned out_id = FF_OUTLINK_IDX(outlink);
598  int ret;
599 
600  while (1) {
601  ret = movie_push_frame(ctx, out_id);
602  if (ret)
603  return FFMIN(ret, 0);
604  }
605 }
606 
607 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
608  char *res, int res_len, int flags)
609 {
610  MovieContext *movie = ctx->priv;
611  int ret = AVERROR(ENOSYS);
612 
613  if (!strcmp(cmd, "seek")) {
614  int idx, flags, i;
615  int64_t ts;
616  char tail[2];
617 
618  if (sscanf(args, "%i|%"SCNi64"|%i %1s", &idx, &ts, &flags, tail) != 3)
619  return AVERROR(EINVAL);
620 
621  ret = av_seek_frame(movie->format_ctx, idx, ts, flags);
622  if (ret < 0)
623  return ret;
624 
625  for (i = 0; i < ctx->nb_outputs; i++) {
627  movie->st[i].done = 0;
628  }
629  return ret;
630  } else if (!strcmp(cmd, "get_duration")) {
631  int print_len;
632  char tail[2];
633 
634  if (!res || res_len <= 0)
635  return AVERROR(EINVAL);
636 
637  if (args && sscanf(args, "%1s", tail) == 1)
638  return AVERROR(EINVAL);
639 
640  print_len = snprintf(res, res_len, "%"PRId64, movie->format_ctx->duration);
641  if (print_len < 0 || print_len >= res_len)
642  return AVERROR(EINVAL);
643 
644  return 0;
645  }
646 
647  return ret;
648 }
649 
650 #if CONFIG_MOVIE_FILTER
651 
652 AVFILTER_DEFINE_CLASS(movie);
653 
654 AVFilter ff_avsrc_movie = {
655  .name = "movie",
656  .description = NULL_IF_CONFIG_SMALL("Read from a movie source."),
657  .priv_size = sizeof(MovieContext),
658  .priv_class = &movie_class,
660  .uninit = movie_uninit,
662 
663  .inputs = NULL,
664  .outputs = NULL,
667 };
668 
669 #endif /* CONFIG_MOVIE_FILTER */
670 
671 #if CONFIG_AMOVIE_FILTER
672 
673 #define amovie_options movie_options
674 AVFILTER_DEFINE_CLASS(amovie);
675 
676 AVFilter ff_avsrc_amovie = {
677  .name = "amovie",
678  .description = NULL_IF_CONFIG_SMALL("Read audio from a movie source."),
679  .priv_size = sizeof(MovieContext),
681  .uninit = movie_uninit,
683 
684  .inputs = NULL,
685  .outputs = NULL,
686  .priv_class = &amovie_class,
689 };
690 
691 #endif /* CONFIG_AMOVIE_FILTER */
#define AVSEEK_FLAG_BACKWARD
Definition: avformat.h:2423
#define NULL
Definition: coverity.c:32
static AVStream * find_stream(void *log, AVFormatContext *avf, const char *spec)
Definition: src_movie.c:98
int64_t discontinuity_threshold
Definition: src_movie.c:51
This structure describes decoded (raw) audio or video data.
Definition: frame.h:187
AVOption.
Definition: opt.h:246
int64_t ts_offset
Definition: src_movie.c:66
static av_cold void movie_uninit(AVFilterContext *ctx)
Definition: src_movie.c:331
misc image utilities
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
Main libavfilter public API header.
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
AVStream * st
Definition: src_movie.c:48
int64_t last_pts
Definition: src_movie.c:52
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:4066
int num
Numerator.
Definition: rational.h:59
int index
stream index in AVFormatContext
Definition: avformat.h:890
int size
Definition: avcodec.h:1658
static const AVOption movie_options[]
Definition: src_movie.c:80
attribute_deprecated int avcodec_decode_audio4(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, const AVPacket *avpkt)
Decode the audio frame of size avpkt->size from avpkt->data into frame.
Definition: utils.c:2336
enum AVMediaType type
AVFilterPad type.
Definition: internal.h:65
#define FF_OUTLINK_IDX(link)
Definition: internal.h:357
discard all
Definition: avcodec.h:822
static AVPacket pkt
char * stream_specs
user-provided list of streams, separated by +
Definition: src_movie.c:62
AVCodec.
Definition: avcodec.h:3681
This struct describes the properties of an encoded stream.
Definition: avcodec.h:4058
char * file_name
Definition: src_movie.c:61
Macro definitions for various function/variable attributes.
AVPacket pkt0
Definition: src_movie.c:70
void * av_calloc(size_t nmemb, size_t size)
Non-inlined equivalent of av_mallocz_array().
Definition: mem.c:230
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:283
Format I/O context.
Definition: avformat.h:1349
double seek_point_d
Definition: src_movie.c:59
const char * name
Pad name.
Definition: internal.h:60
int ff_channel_layouts_ref(AVFilterChannelLayouts *f, AVFilterChannelLayouts **ref)
Add *ref as a new reference to f.
Definition: formats.c:435
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1125
AVFilterPad * output_pads
array of output pads
Definition: avfilter.h:334
Round toward +infinity.
Definition: mathematics.h:83
static int nb_streams
Definition: ffprobe.c:273
#define av_cold
Definition: attributes.h:82
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:150
int(* request_frame)(AVFilterLink *link)
Frame request callback.
Definition: internal.h:113
int width
Video only.
Definition: avcodec.h:4132
static av_cold int uninit(AVCodecContext *avctx)
Definition: crystalhd.c:287
AVOptions.
timestamp utils, mostly useful for debugging/logging purposes
int avformat_match_stream_specifier(AVFormatContext *s, AVStream *st, const char *spec)
Check if the stream st contained in s is matched by the stream specifier spec.
Definition: utils.c:4821
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:271
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1417
int avcodec_parameters_to_context(AVCodecContext *codec, const AVCodecParameters *par)
Fill the codec context based on the values from the supplied codec parameters.
Definition: utils.c:4264
static AVFrame * frame
uint8_t * data
Definition: avcodec.h:1657
static int flags
Definition: log.c:57
#define ff_dlog(a,...)
#define AVERROR_EOF
End of file.
Definition: error.h:55
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:192
#define AVFILTER_FLAG_DYNAMIC_OUTPUTS
The number of the filter outputs is not determined just by AVFilter.outputs.
Definition: avfilter.h:111
int64_t av_frame_get_best_effort_timestamp(const AVFrame *frame)
Accessors for some AVFrame fields.
uint64_t channel_layout
Audio only.
Definition: avcodec.h:4168
#define av_log(a,...)
A filter pad used for either input or output.
Definition: internal.h:54
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:142
int av_find_best_stream(AVFormatContext *ic, enum AVMediaType type, int wanted_stream_nb, int related_stream, AVCodec **decoder_ret, int flags)
Find the "best" stream in the file.
Definition: utils.c:3973
int width
width and height of the video frame
Definition: frame.h:239
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#define AVERROR(e)
Definition: error.h:43
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:163
#define av_ts2timestr(ts, tb)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: timestamp.h:76
unsigned nb_outputs
number of output pads
Definition: avfilter.h:336
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:179
void * priv
private data for use by the filter
Definition: avfilter.h:338
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:4062
simple assert() macros that are a bit more flexible than ISO C assert().
#define FFMAX(a, b)
Definition: common.h:94
static int guess_channel_layout(MovieStream *st, int st_index, void *log_ctx)
Definition: src_movie.c:181
#define FLAGS
Definition: src_movie.c:78
common internal API header
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1405
AVInputFormat * av_find_input_format(const char *short_name)
Find AVInputFormat based on the short name of the input format.
Definition: format.c:164
#define FFMIN(a, b)
Definition: common.h:96
int64_t discontinuity_threshold
Definition: src_movie.c:65
AVCodecContext * avcodec_alloc_context3(const AVCodec *codec)
Allocate an AVCodecContext and set its fields to default values.
Definition: options.c:157
int ff_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
Add *ref as a new reference to formats.
Definition: formats.c:440
int loop_count
Definition: src_movie.c:64
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: error.h:119
AVFormatContext * ctx
Definition: movenc.c:48
static int movie_config_output_props(AVFilterLink *outlink)
Definition: src_movie.c:383
char * format_name
Definition: src_movie.c:60
int refcounted_frames
If non-zero, the decoded audio and video frames returned from avcodec_decode_video2() and avcodec_dec...
Definition: avcodec.h:2664
int dummy
Definition: motion.c:64
static const AVFilterPad outputs[]
Definition: af_afftfilt.c:386
int max_stream_index
max stream # actually used for output
Definition: src_movie.c:72
static av_cold int movie_common_init(AVFilterContext *ctx)
Definition: src_movie.c:204
void av_get_channel_layout_string(char *buf, int buf_size, int nb_channels, uint64_t channel_layout)
Return a description of a channel layout.
Stream structure.
Definition: avformat.h:889
void avcodec_flush_buffers(AVCodecContext *avctx)
Reset the internal decoder state / flush internal buffers.
Definition: utils.c:3473
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
Definition: frame.h:251
int * out_index
stream number -> output number map, or -1
Definition: src_movie.c:74
static AVInputFormat * iformat
Definition: ffprobe.c:250
static const AVFilterPad inputs[]
Definition: af_afftfilt.c:376
char * av_strdup(const char *s)
Duplicate a string.
Definition: mem.c:237
AVFilterChannelLayouts * avfilter_make_format64_list(const int64_t *fmts)
Definition: formats.c:303
attribute_deprecated int avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture, int *got_picture_ptr, const AVPacket *avpkt)
Decode the video frame of size avpkt->size from avpkt->data into picture.
Definition: utils.c:2227
void avcodec_free_context(AVCodecContext **avctx)
Free the codec context and everything associated with it and write NULL to the provided pointer...
Definition: options.c:172
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
Definition: avutil.h:260
main external API structure.
Definition: avcodec.h:1732
AVCodec * avcodec_find_decoder(enum AVCodecID id)
Find a registered decoder with a matching codec ID.
Definition: utils.c:3168
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:589
static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, char *res, int res_len, int flags)
Definition: src_movie.c:607
AVRational sample_aspect_ratio
Sample aspect ratio for the video frame, 0/1 if unknown/unspecified.
Definition: frame.h:266
void * buf
Definition: avisynth_c.h:690
GLint GLenum type
Definition: opengl_enc.c:105
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition: error.h:50
Describe the class of an AVClass context structure.
Definition: log.h:67
Filter definition.
Definition: avfilter.h:144
int64_t seek_point
seekpoint in microseconds
Definition: src_movie.c:58
static int movie_push_frame(AVFilterContext *ctx, unsigned out_id)
Try to push a frame to the requested output.
Definition: src_movie.c:466
AVMediaType
Definition: avutil.h:199
discard useless packets like 0 size packets in avi
Definition: avcodec.h:817
const char * name
Filter name.
Definition: avfilter.h:148
int avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
Initialize the AVCodecContext to use the given AVCodec.
Definition: utils.c:1242
#define snprintf
Definition: snprintf.h:34
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:335
int av_read_frame(AVFormatContext *s, AVPacket *pkt)
Return the next frame of a stream.
Definition: utils.c:1710
static char * describe_frame_to_str(char *dst, size_t dst_size, AVFrame *frame, enum AVMediaType frame_type, AVFilterLink *link)
Definition: src_movie.c:406
const char * av_get_media_type_string(enum AVMediaType media_type)
Return a string describing the media_type enum, NULL if media_type is unknown.
Definition: utils.c:79
int64_t start_time
Position of the first frame of the component, in AV_TIME_BASE fractional seconds. ...
Definition: avformat.h:1434
int stream_index
for compatibility
Definition: src_movie.c:63
int av_seek_frame(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
Seek to the keyframe at timestamp.
Definition: utils.c:2447
char * av_strtok(char *s, const char *delim, char **saveptr)
Split the string into several tokens which can be accessed by successive calls to av_strtok()...
Definition: avstring.c:184
int sample_rate
Audio only.
Definition: avcodec.h:4176
#define OFFSET(x)
Definition: src_movie.c:77
static int movie_request_frame(AVFilterLink *outlink)
Definition: src_movie.c:594
Main libavformat public API header.
static int query_formats(AVFilterContext *ctx)
Definition: aeval.c:244
static int open_stream(void *log, MovieStream *st)
Definition: src_movie.c:152
int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
Read packets of a media file to get stream information.
Definition: utils.c:3374
static double c[64]
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
Definition: avpacket.c:33
int den
Denominator.
Definition: rational.h:60
AVPacket pkt
Definition: src_movie.c:70
void avformat_close_input(AVFormatContext **s)
Close an opened input AVFormatContext.
Definition: utils.c:4203
static int movie_query_formats(AVFilterContext *ctx)
Definition: src_movie.c:347
static av_always_inline int diff(const uint32_t a, const uint32_t b)
#define av_ts2str(ts)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: timestamp.h:54
#define AVFILTER_DEFINE_CLASS(fname)
Definition: internal.h:342
static int rewind_file(AVFilterContext *ctx)
Definition: src_movie.c:432
AVCodecContext * codec_ctx
Definition: src_movie.c:49
AVFormatContext * format_ctx
Definition: src_movie.c:68
int avformat_open_input(AVFormatContext **ps, const char *url, AVInputFormat *fmt, AVDictionary **options)
Open an input stream and read the header.
Definition: utils.c:510
int channels
Audio only.
Definition: avcodec.h:4172
An instance of a filter.
Definition: avfilter.h:323
int64_t av_get_default_channel_layout(int nb_channels)
Return default channel layout for a given number of channels.
int64_t duration
Duration of the stream, in AV_TIME_BASE fractional seconds.
Definition: avformat.h:1444
int height
Definition: frame.h:239
#define av_freep(p)
int(* config_props)(AVFilterLink *link)
Link configuration callback.
Definition: internal.h:129
AVCodecParameters * codecpar
Definition: avformat.h:1252
int64_t av_rescale_q_rnd(int64_t a, AVRational bq, AVRational cq, enum AVRounding rnd)
Rescale a 64-bit integer by 2 rational numbers with specified rounding.
Definition: mathematics.c:134
const char * av_get_pix_fmt_name(enum AVPixelFormat pix_fmt)
Return the short name for a pixel format, NULL in case pix_fmt is unknown.
Definition: pixdesc.c:2249
int stream_index
Definition: avcodec.h:1659
internal API functions
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:926
enum AVDiscard discard
Selects which packets can be discarded at will and do not need to be demuxed.
Definition: avformat.h:952
AVRational r_frame_rate
Real base framerate of the stream.
Definition: avformat.h:1108
static int ff_insert_outpad(AVFilterContext *f, unsigned index, AVFilterPad *p)
Insert a new output pad for the filter.
Definition: internal.h:293
This structure stores compressed data.
Definition: avcodec.h:1634
void av_register_all(void)
Initialize libavformat and register all the muxers, demuxers and protocols.
Definition: allformats.c:385
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:244
const AVFilter * filter
the AVFilter of which this is an instance
Definition: avfilter.h:326
for(j=16;j >0;--j)
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
const char * name
Definition: opengl_enc.c:103
MovieStream * st
array of all streams, one per output
Definition: src_movie.c:73