src_movie.c 21 KB

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