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.

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