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.

670 lines
22KB

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