You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

678 lines
23KB

  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. * @file
  23. * movie video source
  24. *
  25. * @todo use direct rendering (no allocation of a new frame)
  26. * @todo support a PTS correction mechanism
  27. */
  28. #include <float.h>
  29. #include <stdint.h>
  30. #include "libavutil/attributes.h"
  31. #include "libavutil/avstring.h"
  32. #include "libavutil/avassert.h"
  33. #include "libavutil/opt.h"
  34. #include "libavutil/imgutils.h"
  35. #include "libavutil/internal.h"
  36. #include "libavutil/timestamp.h"
  37. #include "libavcodec/avcodec.h"
  38. #include "libavformat/avformat.h"
  39. #include "audio.h"
  40. #include "avfilter.h"
  41. #include "formats.h"
  42. #include "internal.h"
  43. #include "video.h"
  44. typedef struct MovieStream {
  45. AVStream *st;
  46. AVCodecContext *codec_ctx;
  47. int64_t discontinuity_threshold;
  48. int64_t last_pts;
  49. } MovieStream;
  50. typedef struct MovieContext {
  51. /* common A/V fields */
  52. const AVClass *class;
  53. int64_t seek_point; ///< seekpoint in microseconds
  54. double seek_point_d;
  55. char *format_name;
  56. char *file_name;
  57. char *stream_specs; /**< user-provided list of streams, separated by + */
  58. int stream_index; /**< for compatibility */
  59. int loop_count;
  60. int64_t discontinuity_threshold;
  61. int64_t ts_offset;
  62. AVFormatContext *format_ctx;
  63. int max_stream_index; /**< max stream # actually used for output */
  64. MovieStream *st; /**< array of all streams, one per output */
  65. int *out_index; /**< stream number -> output number map, or -1 */
  66. } MovieContext;
  67. #define OFFSET(x) offsetof(MovieContext, x)
  68. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_VIDEO_PARAM
  69. static const AVOption movie_options[]= {
  70. { "filename", NULL, OFFSET(file_name), AV_OPT_TYPE_STRING, .flags = FLAGS },
  71. { "format_name", "set format name", OFFSET(format_name), AV_OPT_TYPE_STRING, .flags = FLAGS },
  72. { "f", "set format name", OFFSET(format_name), AV_OPT_TYPE_STRING, .flags = FLAGS },
  73. { "stream_index", "set stream index", OFFSET(stream_index), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS },
  74. { "si", "set stream index", OFFSET(stream_index), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS },
  75. { "seek_point", "set seekpoint (seconds)", OFFSET(seek_point_d), AV_OPT_TYPE_DOUBLE, { .dbl = 0 }, 0, (INT64_MAX-1) / 1000000, FLAGS },
  76. { "sp", "set seekpoint (seconds)", OFFSET(seek_point_d), AV_OPT_TYPE_DOUBLE, { .dbl = 0 }, 0, (INT64_MAX-1) / 1000000, FLAGS },
  77. { "streams", "set streams", OFFSET(stream_specs), AV_OPT_TYPE_STRING, {.str = 0}, 0, 0, FLAGS },
  78. { "s", "set streams", OFFSET(stream_specs), AV_OPT_TYPE_STRING, {.str = 0}, 0, 0, FLAGS },
  79. { "loop", "set loop count", OFFSET(loop_count), AV_OPT_TYPE_INT, {.i64 = 1}, 0, INT_MAX, FLAGS },
  80. { "discontinuity", "set discontinuity threshold", OFFSET(discontinuity_threshold), AV_OPT_TYPE_DURATION, {.i64 = 0}, 0, INT64_MAX, FLAGS },
  81. { NULL },
  82. };
  83. static int movie_config_output_props(AVFilterLink *outlink);
  84. static int movie_request_frame(AVFilterLink *outlink);
  85. static AVStream *find_stream(void *log, AVFormatContext *avf, const char *spec)
  86. {
  87. int i, ret, already = 0, stream_id = -1;
  88. char type_char[2], dummy;
  89. AVStream *found = NULL;
  90. enum AVMediaType type;
  91. ret = sscanf(spec, "d%1[av]%d%c", type_char, &stream_id, &dummy);
  92. if (ret >= 1 && ret <= 2) {
  93. type = type_char[0] == 'v' ? AVMEDIA_TYPE_VIDEO : AVMEDIA_TYPE_AUDIO;
  94. ret = av_find_best_stream(avf, type, stream_id, -1, NULL, 0);
  95. if (ret < 0) {
  96. av_log(log, AV_LOG_ERROR, "No %s stream with index '%d' found\n",
  97. av_get_media_type_string(type), stream_id);
  98. return NULL;
  99. }
  100. return avf->streams[ret];
  101. }
  102. for (i = 0; i < avf->nb_streams; i++) {
  103. ret = avformat_match_stream_specifier(avf, avf->streams[i], spec);
  104. if (ret < 0) {
  105. av_log(log, AV_LOG_ERROR,
  106. "Invalid stream specifier \"%s\"\n", spec);
  107. return NULL;
  108. }
  109. if (!ret)
  110. continue;
  111. if (avf->streams[i]->discard != AVDISCARD_ALL) {
  112. already++;
  113. continue;
  114. }
  115. if (found) {
  116. av_log(log, AV_LOG_WARNING,
  117. "Ambiguous stream specifier \"%s\", using #%d\n", spec, i);
  118. break;
  119. }
  120. found = avf->streams[i];
  121. }
  122. if (!found) {
  123. av_log(log, AV_LOG_WARNING, "Stream specifier \"%s\" %s\n", spec,
  124. already ? "matched only already used streams" :
  125. "did not match any stream");
  126. return NULL;
  127. }
  128. if (found->codecpar->codec_type != AVMEDIA_TYPE_VIDEO &&
  129. found->codecpar->codec_type != AVMEDIA_TYPE_AUDIO) {
  130. av_log(log, AV_LOG_ERROR, "Stream specifier \"%s\" matched a %s stream,"
  131. "currently unsupported by libavfilter\n", spec,
  132. av_get_media_type_string(found->codecpar->codec_type));
  133. return NULL;
  134. }
  135. return found;
  136. }
  137. static int open_stream(AVFilterContext *ctx, MovieStream *st)
  138. {
  139. const AVCodec *codec;
  140. int ret;
  141. codec = avcodec_find_decoder(st->st->codecpar->codec_id);
  142. if (!codec) {
  143. av_log(ctx, AV_LOG_ERROR, "Failed to find any codec\n");
  144. return AVERROR(EINVAL);
  145. }
  146. st->codec_ctx = avcodec_alloc_context3(codec);
  147. if (!st->codec_ctx)
  148. return AVERROR(ENOMEM);
  149. ret = avcodec_parameters_to_context(st->codec_ctx, st->st->codecpar);
  150. if (ret < 0)
  151. return ret;
  152. st->codec_ctx->thread_count = ff_filter_get_nb_threads(ctx);
  153. if ((ret = avcodec_open2(st->codec_ctx, codec, NULL)) < 0) {
  154. av_log(ctx, AV_LOG_ERROR, "Failed to open codec\n");
  155. return ret;
  156. }
  157. return 0;
  158. }
  159. static int guess_channel_layout(MovieStream *st, int st_index, void *log_ctx)
  160. {
  161. AVCodecParameters *dec_par = st->st->codecpar;
  162. char buf[256];
  163. int64_t chl = av_get_default_channel_layout(dec_par->channels);
  164. if (!chl) {
  165. av_log(log_ctx, AV_LOG_ERROR,
  166. "Channel layout is not set in stream %d, and could not "
  167. "be guessed from the number of channels (%d)\n",
  168. st_index, dec_par->channels);
  169. return AVERROR(EINVAL);
  170. }
  171. av_get_channel_layout_string(buf, sizeof(buf), dec_par->channels, chl);
  172. av_log(log_ctx, AV_LOG_WARNING,
  173. "Channel layout is not set in output stream %d, "
  174. "guessed channel layout is '%s'\n",
  175. st_index, buf);
  176. dec_par->channel_layout = chl;
  177. return 0;
  178. }
  179. static av_cold int movie_common_init(AVFilterContext *ctx)
  180. {
  181. MovieContext *movie = ctx->priv;
  182. AVInputFormat *iformat = NULL;
  183. int64_t timestamp;
  184. int nb_streams = 1, ret, i;
  185. char default_streams[16], *stream_specs, *spec, *cursor;
  186. AVStream *st;
  187. if (!movie->file_name) {
  188. av_log(ctx, AV_LOG_ERROR, "No filename provided!\n");
  189. return AVERROR(EINVAL);
  190. }
  191. movie->seek_point = movie->seek_point_d * 1000000 + 0.5;
  192. stream_specs = movie->stream_specs;
  193. if (!stream_specs) {
  194. snprintf(default_streams, sizeof(default_streams), "d%c%d",
  195. !strcmp(ctx->filter->name, "amovie") ? 'a' : 'v',
  196. movie->stream_index);
  197. stream_specs = default_streams;
  198. }
  199. for (cursor = stream_specs; *cursor; cursor++)
  200. if (*cursor == '+')
  201. nb_streams++;
  202. if (movie->loop_count != 1 && nb_streams != 1) {
  203. av_log(ctx, AV_LOG_ERROR,
  204. "Loop with several streams is currently unsupported\n");
  205. return AVERROR_PATCHWELCOME;
  206. }
  207. // Try to find the movie format (container)
  208. iformat = movie->format_name ? av_find_input_format(movie->format_name) : NULL;
  209. movie->format_ctx = NULL;
  210. if ((ret = avformat_open_input(&movie->format_ctx, movie->file_name, iformat, NULL)) < 0) {
  211. av_log(ctx, AV_LOG_ERROR,
  212. "Failed to avformat_open_input '%s'\n", movie->file_name);
  213. return ret;
  214. }
  215. if ((ret = avformat_find_stream_info(movie->format_ctx, NULL)) < 0)
  216. av_log(ctx, AV_LOG_WARNING, "Failed to find stream info\n");
  217. // if seeking requested, we execute it
  218. if (movie->seek_point > 0) {
  219. timestamp = movie->seek_point;
  220. // add the stream start time, should it exist
  221. if (movie->format_ctx->start_time != AV_NOPTS_VALUE) {
  222. if (timestamp > 0 && movie->format_ctx->start_time > INT64_MAX - timestamp) {
  223. av_log(ctx, AV_LOG_ERROR,
  224. "%s: seek value overflow with start_time:%"PRId64" seek_point:%"PRId64"\n",
  225. movie->file_name, movie->format_ctx->start_time, movie->seek_point);
  226. return AVERROR(EINVAL);
  227. }
  228. timestamp += movie->format_ctx->start_time;
  229. }
  230. if ((ret = av_seek_frame(movie->format_ctx, -1, timestamp, AVSEEK_FLAG_BACKWARD)) < 0) {
  231. av_log(ctx, AV_LOG_ERROR, "%s: could not seek to position %"PRId64"\n",
  232. movie->file_name, timestamp);
  233. return ret;
  234. }
  235. }
  236. for (i = 0; i < movie->format_ctx->nb_streams; i++)
  237. movie->format_ctx->streams[i]->discard = AVDISCARD_ALL;
  238. movie->st = av_calloc(nb_streams, sizeof(*movie->st));
  239. if (!movie->st)
  240. return AVERROR(ENOMEM);
  241. for (i = 0; i < nb_streams; i++) {
  242. spec = av_strtok(stream_specs, "+", &cursor);
  243. if (!spec)
  244. return AVERROR_BUG;
  245. stream_specs = NULL; /* for next strtok */
  246. st = find_stream(ctx, movie->format_ctx, spec);
  247. if (!st)
  248. return AVERROR(EINVAL);
  249. st->discard = AVDISCARD_DEFAULT;
  250. movie->st[i].st = st;
  251. movie->max_stream_index = FFMAX(movie->max_stream_index, st->index);
  252. movie->st[i].discontinuity_threshold =
  253. av_rescale_q(movie->discontinuity_threshold, AV_TIME_BASE_Q, st->time_base);
  254. }
  255. if (av_strtok(NULL, "+", &cursor))
  256. return AVERROR_BUG;
  257. movie->out_index = av_calloc(movie->max_stream_index + 1,
  258. sizeof(*movie->out_index));
  259. if (!movie->out_index)
  260. return AVERROR(ENOMEM);
  261. for (i = 0; i <= movie->max_stream_index; i++)
  262. movie->out_index[i] = -1;
  263. for (i = 0; i < nb_streams; i++) {
  264. AVFilterPad pad = { 0 };
  265. movie->out_index[movie->st[i].st->index] = i;
  266. pad.type = movie->st[i].st->codecpar->codec_type;
  267. pad.name = av_asprintf("out%d", i);
  268. if (!pad.name)
  269. return AVERROR(ENOMEM);
  270. pad.config_props = movie_config_output_props;
  271. pad.request_frame = movie_request_frame;
  272. if ((ret = ff_insert_outpad(ctx, i, &pad)) < 0) {
  273. av_freep(&pad.name);
  274. return ret;
  275. }
  276. if ( movie->st[i].st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
  277. !movie->st[i].st->codecpar->channel_layout) {
  278. ret = guess_channel_layout(&movie->st[i], i, ctx);
  279. if (ret < 0)
  280. return ret;
  281. }
  282. ret = open_stream(ctx, &movie->st[i]);
  283. if (ret < 0)
  284. return ret;
  285. }
  286. av_log(ctx, AV_LOG_VERBOSE, "seek_point:%"PRIi64" format_name:%s file_name:%s stream_index:%d\n",
  287. movie->seek_point, movie->format_name, movie->file_name,
  288. movie->stream_index);
  289. return 0;
  290. }
  291. static av_cold void movie_uninit(AVFilterContext *ctx)
  292. {
  293. MovieContext *movie = ctx->priv;
  294. int i;
  295. for (i = 0; i < ctx->nb_outputs; i++) {
  296. av_freep(&ctx->output_pads[i].name);
  297. if (movie->st[i].st)
  298. avcodec_free_context(&movie->st[i].codec_ctx);
  299. }
  300. av_freep(&movie->st);
  301. av_freep(&movie->out_index);
  302. if (movie->format_ctx)
  303. avformat_close_input(&movie->format_ctx);
  304. }
  305. static int movie_query_formats(AVFilterContext *ctx)
  306. {
  307. MovieContext *movie = ctx->priv;
  308. int list[] = { 0, -1 };
  309. int64_t list64[] = { 0, -1 };
  310. int i, ret;
  311. for (i = 0; i < ctx->nb_outputs; i++) {
  312. MovieStream *st = &movie->st[i];
  313. AVCodecParameters *c = st->st->codecpar;
  314. AVFilterLink *outlink = ctx->outputs[i];
  315. switch (c->codec_type) {
  316. case AVMEDIA_TYPE_VIDEO:
  317. list[0] = c->format;
  318. if ((ret = ff_formats_ref(ff_make_format_list(list), &outlink->incfg.formats)) < 0)
  319. return ret;
  320. break;
  321. case AVMEDIA_TYPE_AUDIO:
  322. list[0] = c->format;
  323. if ((ret = ff_formats_ref(ff_make_format_list(list), &outlink->incfg.formats)) < 0)
  324. return ret;
  325. list[0] = c->sample_rate;
  326. if ((ret = ff_formats_ref(ff_make_format_list(list), &outlink->incfg.samplerates)) < 0)
  327. return ret;
  328. list64[0] = c->channel_layout;
  329. if ((ret = ff_channel_layouts_ref(ff_make_format64_list(list64),
  330. &outlink->incfg.channel_layouts)) < 0)
  331. return ret;
  332. break;
  333. }
  334. }
  335. return 0;
  336. }
  337. static int movie_config_output_props(AVFilterLink *outlink)
  338. {
  339. AVFilterContext *ctx = outlink->src;
  340. MovieContext *movie = ctx->priv;
  341. unsigned out_id = FF_OUTLINK_IDX(outlink);
  342. MovieStream *st = &movie->st[out_id];
  343. AVCodecParameters *c = st->st->codecpar;
  344. outlink->time_base = st->st->time_base;
  345. switch (c->codec_type) {
  346. case AVMEDIA_TYPE_VIDEO:
  347. outlink->w = c->width;
  348. outlink->h = c->height;
  349. outlink->frame_rate = st->st->r_frame_rate;
  350. break;
  351. case AVMEDIA_TYPE_AUDIO:
  352. break;
  353. }
  354. return 0;
  355. }
  356. static char *describe_frame_to_str(char *dst, size_t dst_size,
  357. AVFrame *frame, enum AVMediaType frame_type,
  358. AVFilterLink *link)
  359. {
  360. switch (frame_type) {
  361. case AVMEDIA_TYPE_VIDEO:
  362. snprintf(dst, dst_size,
  363. "video pts:%s time:%s size:%dx%d aspect:%d/%d",
  364. av_ts2str(frame->pts), av_ts2timestr(frame->pts, &link->time_base),
  365. frame->width, frame->height,
  366. frame->sample_aspect_ratio.num,
  367. frame->sample_aspect_ratio.den);
  368. break;
  369. case AVMEDIA_TYPE_AUDIO:
  370. snprintf(dst, dst_size,
  371. "audio pts:%s time:%s samples:%d",
  372. av_ts2str(frame->pts), av_ts2timestr(frame->pts, &link->time_base),
  373. frame->nb_samples);
  374. break;
  375. default:
  376. snprintf(dst, dst_size, "%s BUG", av_get_media_type_string(frame_type));
  377. break;
  378. }
  379. return dst;
  380. }
  381. static int rewind_file(AVFilterContext *ctx)
  382. {
  383. MovieContext *movie = ctx->priv;
  384. int64_t timestamp = movie->seek_point;
  385. int ret, i;
  386. if (movie->format_ctx->start_time != AV_NOPTS_VALUE)
  387. timestamp += movie->format_ctx->start_time;
  388. ret = av_seek_frame(movie->format_ctx, -1, timestamp, AVSEEK_FLAG_BACKWARD);
  389. if (ret < 0) {
  390. av_log(ctx, AV_LOG_ERROR, "Unable to loop: %s\n", av_err2str(ret));
  391. movie->loop_count = 1; /* do not try again */
  392. return ret;
  393. }
  394. for (i = 0; i < ctx->nb_outputs; i++) {
  395. avcodec_flush_buffers(movie->st[i].codec_ctx);
  396. }
  397. return 0;
  398. }
  399. static int movie_decode_packet(AVFilterContext *ctx)
  400. {
  401. MovieContext *movie = ctx->priv;
  402. AVPacket pkt = { 0 };
  403. int pkt_out_id, ret;
  404. /* read a new packet from input stream */
  405. ret = av_read_frame(movie->format_ctx, &pkt);
  406. if (ret == AVERROR_EOF) {
  407. /* EOF -> set all decoders for flushing */
  408. for (int i = 0; i < ctx->nb_outputs; i++) {
  409. ret = avcodec_send_packet(movie->st[i].codec_ctx, NULL);
  410. if (ret < 0 && ret != AVERROR_EOF)
  411. return ret;
  412. }
  413. return 0;
  414. } else if (ret < 0)
  415. return ret;
  416. /* send the packet to its decoder, if any */
  417. pkt_out_id = pkt.stream_index > movie->max_stream_index ? -1 :
  418. movie->out_index[pkt.stream_index];
  419. if (pkt_out_id >= 0)
  420. ret = avcodec_send_packet(movie->st[pkt_out_id].codec_ctx, &pkt);
  421. av_packet_unref(&pkt);
  422. return ret;
  423. }
  424. /**
  425. * Try to push a frame to the requested output.
  426. *
  427. * @param ctx filter context
  428. * @param out_id number of output where a frame is wanted;
  429. * @return 0 if a frame was pushed on the requested output,
  430. * AVERROR(EAGAIN) if the decoder requires more input
  431. * AVERROR(EOF) if the decoder has been completely flushed
  432. * <0 AVERROR code
  433. */
  434. static int movie_push_frame(AVFilterContext *ctx, unsigned out_id)
  435. {
  436. MovieContext *movie = ctx->priv;
  437. MovieStream *st = &movie->st[out_id];
  438. AVFilterLink *outlink = ctx->outputs[out_id];
  439. AVFrame *frame;
  440. int ret;
  441. frame = av_frame_alloc();
  442. if (!frame)
  443. return AVERROR(ENOMEM);
  444. ret = avcodec_receive_frame(st->codec_ctx, frame);
  445. if (ret < 0) {
  446. if (ret != AVERROR_EOF && ret != AVERROR(EAGAIN))
  447. av_log(ctx, AV_LOG_WARNING, "Decode error: %s\n", av_err2str(ret));
  448. av_frame_free(&frame);
  449. return ret;
  450. }
  451. frame->pts = frame->best_effort_timestamp;
  452. if (frame->pts != AV_NOPTS_VALUE) {
  453. if (movie->ts_offset)
  454. frame->pts += av_rescale_q_rnd(movie->ts_offset, AV_TIME_BASE_Q, outlink->time_base, AV_ROUND_UP);
  455. if (st->discontinuity_threshold) {
  456. if (st->last_pts != AV_NOPTS_VALUE) {
  457. int64_t diff = frame->pts - st->last_pts;
  458. if (diff < 0 || diff > st->discontinuity_threshold) {
  459. av_log(ctx, AV_LOG_VERBOSE, "Discontinuity in stream:%d diff:%"PRId64"\n", out_id, diff);
  460. movie->ts_offset += av_rescale_q_rnd(-diff, outlink->time_base, AV_TIME_BASE_Q, AV_ROUND_UP);
  461. frame->pts -= diff;
  462. }
  463. }
  464. }
  465. st->last_pts = frame->pts;
  466. }
  467. ff_dlog(ctx, "movie_push_frame(): file:'%s' %s\n", movie->file_name,
  468. describe_frame_to_str((char[1024]){0}, 1024, frame,
  469. st->st->codecpar->codec_type, outlink));
  470. if (st->st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
  471. if (frame->format != outlink->format) {
  472. av_log(ctx, AV_LOG_ERROR, "Format changed %s -> %s, discarding frame\n",
  473. av_get_pix_fmt_name(outlink->format),
  474. av_get_pix_fmt_name(frame->format)
  475. );
  476. av_frame_free(&frame);
  477. return 0;
  478. }
  479. }
  480. ret = ff_filter_frame(outlink, frame);
  481. if (ret < 0)
  482. return ret;
  483. return 0;
  484. }
  485. static int movie_request_frame(AVFilterLink *outlink)
  486. {
  487. AVFilterContext *ctx = outlink->src;
  488. MovieContext *movie = ctx->priv;
  489. unsigned out_id = FF_OUTLINK_IDX(outlink);
  490. while (1) {
  491. int got_eagain = 0, got_eof = 0;
  492. int ret = 0;
  493. /* check all decoders for available output */
  494. for (int i = 0; i < ctx->nb_outputs; i++) {
  495. ret = movie_push_frame(ctx, i);
  496. if (ret == AVERROR(EAGAIN))
  497. got_eagain++;
  498. else if (ret == AVERROR_EOF)
  499. got_eof++;
  500. else if (ret < 0)
  501. return ret;
  502. else if (i == out_id)
  503. return 0;
  504. }
  505. if (got_eagain) {
  506. /* all decoders require more input -> read a new packet */
  507. ret = movie_decode_packet(ctx);
  508. if (ret < 0)
  509. return ret;
  510. } else if (got_eof) {
  511. /* all decoders flushed */
  512. if (movie->loop_count != 1) {
  513. ret = rewind_file(ctx);
  514. if (ret < 0)
  515. return ret;
  516. movie->loop_count -= movie->loop_count > 1;
  517. av_log(ctx, AV_LOG_VERBOSE, "Stream finished, looping.\n");
  518. continue;
  519. }
  520. return AVERROR_EOF;
  521. }
  522. }
  523. }
  524. static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
  525. char *res, int res_len, int flags)
  526. {
  527. MovieContext *movie = ctx->priv;
  528. int ret = AVERROR(ENOSYS);
  529. if (!strcmp(cmd, "seek")) {
  530. int idx, flags, i;
  531. int64_t ts;
  532. char tail[2];
  533. if (sscanf(args, "%i|%"SCNi64"|%i %1s", &idx, &ts, &flags, tail) != 3)
  534. return AVERROR(EINVAL);
  535. ret = av_seek_frame(movie->format_ctx, idx, ts, flags);
  536. if (ret < 0)
  537. return ret;
  538. for (i = 0; i < ctx->nb_outputs; i++) {
  539. avcodec_flush_buffers(movie->st[i].codec_ctx);
  540. }
  541. return ret;
  542. } else if (!strcmp(cmd, "get_duration")) {
  543. int print_len;
  544. char tail[2];
  545. if (!res || res_len <= 0)
  546. return AVERROR(EINVAL);
  547. if (args && sscanf(args, "%1s", tail) == 1)
  548. return AVERROR(EINVAL);
  549. print_len = snprintf(res, res_len, "%"PRId64, movie->format_ctx->duration);
  550. if (print_len < 0 || print_len >= res_len)
  551. return AVERROR(EINVAL);
  552. return 0;
  553. }
  554. return ret;
  555. }
  556. #if CONFIG_MOVIE_FILTER
  557. AVFILTER_DEFINE_CLASS(movie);
  558. AVFilter ff_avsrc_movie = {
  559. .name = "movie",
  560. .description = NULL_IF_CONFIG_SMALL("Read from a movie source."),
  561. .priv_size = sizeof(MovieContext),
  562. .priv_class = &movie_class,
  563. .init = movie_common_init,
  564. .uninit = movie_uninit,
  565. .query_formats = movie_query_formats,
  566. .inputs = NULL,
  567. .outputs = NULL,
  568. .flags = AVFILTER_FLAG_DYNAMIC_OUTPUTS,
  569. .process_command = process_command
  570. };
  571. #endif /* CONFIG_MOVIE_FILTER */
  572. #if CONFIG_AMOVIE_FILTER
  573. #define amovie_options movie_options
  574. AVFILTER_DEFINE_CLASS(amovie);
  575. AVFilter ff_avsrc_amovie = {
  576. .name = "amovie",
  577. .description = NULL_IF_CONFIG_SMALL("Read audio from a movie source."),
  578. .priv_size = sizeof(MovieContext),
  579. .init = movie_common_init,
  580. .uninit = movie_uninit,
  581. .query_formats = movie_query_formats,
  582. .inputs = NULL,
  583. .outputs = NULL,
  584. .priv_class = &amovie_class,
  585. .flags = AVFILTER_FLAG_DYNAMIC_OUTPUTS,
  586. .process_command = process_command,
  587. };
  588. #endif /* CONFIG_AMOVIE_FILTER */