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.

699 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. if ((ret = ff_insert_outpad(ctx, i, &pad)) < 0) {
  279. av_freep(&pad.name);
  280. return ret;
  281. }
  282. if ( movie->st[i].st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
  283. !movie->st[i].st->codecpar->channel_layout) {
  284. ret = guess_channel_layout(&movie->st[i], i, ctx);
  285. if (ret < 0)
  286. return ret;
  287. }
  288. ret = open_stream(ctx, &movie->st[i]);
  289. if (ret < 0)
  290. return ret;
  291. }
  292. av_log(ctx, AV_LOG_VERBOSE, "seek_point:%"PRIi64" format_name:%s file_name:%s stream_index:%d\n",
  293. movie->seek_point, movie->format_name, movie->file_name,
  294. movie->stream_index);
  295. return 0;
  296. }
  297. static av_cold void movie_uninit(AVFilterContext *ctx)
  298. {
  299. MovieContext *movie = ctx->priv;
  300. int i;
  301. for (i = 0; i < ctx->nb_outputs; i++) {
  302. av_freep(&ctx->output_pads[i].name);
  303. if (movie->st[i].st)
  304. avcodec_free_context(&movie->st[i].codec_ctx);
  305. }
  306. av_freep(&movie->st);
  307. av_freep(&movie->out_index);
  308. if (movie->format_ctx)
  309. avformat_close_input(&movie->format_ctx);
  310. }
  311. static int movie_query_formats(AVFilterContext *ctx)
  312. {
  313. MovieContext *movie = ctx->priv;
  314. int list[] = { 0, -1 };
  315. int64_t list64[] = { 0, -1 };
  316. int i, ret;
  317. for (i = 0; i < ctx->nb_outputs; i++) {
  318. MovieStream *st = &movie->st[i];
  319. AVCodecParameters *c = st->st->codecpar;
  320. AVFilterLink *outlink = ctx->outputs[i];
  321. switch (c->codec_type) {
  322. case AVMEDIA_TYPE_VIDEO:
  323. list[0] = c->format;
  324. if ((ret = ff_formats_ref(ff_make_format_list(list), &outlink->in_formats)) < 0)
  325. return ret;
  326. break;
  327. case AVMEDIA_TYPE_AUDIO:
  328. list[0] = c->format;
  329. if ((ret = ff_formats_ref(ff_make_format_list(list), &outlink->in_formats)) < 0)
  330. return ret;
  331. list[0] = c->sample_rate;
  332. if ((ret = ff_formats_ref(ff_make_format_list(list), &outlink->in_samplerates)) < 0)
  333. return ret;
  334. list64[0] = c->channel_layout;
  335. if ((ret = ff_channel_layouts_ref(avfilter_make_format64_list(list64),
  336. &outlink->in_channel_layouts)) < 0)
  337. return ret;
  338. break;
  339. }
  340. }
  341. return 0;
  342. }
  343. static int movie_config_output_props(AVFilterLink *outlink)
  344. {
  345. AVFilterContext *ctx = outlink->src;
  346. MovieContext *movie = ctx->priv;
  347. unsigned out_id = FF_OUTLINK_IDX(outlink);
  348. MovieStream *st = &movie->st[out_id];
  349. AVCodecParameters *c = st->st->codecpar;
  350. outlink->time_base = st->st->time_base;
  351. switch (c->codec_type) {
  352. case AVMEDIA_TYPE_VIDEO:
  353. outlink->w = c->width;
  354. outlink->h = c->height;
  355. outlink->frame_rate = st->st->r_frame_rate;
  356. break;
  357. case AVMEDIA_TYPE_AUDIO:
  358. break;
  359. }
  360. return 0;
  361. }
  362. static char *describe_frame_to_str(char *dst, size_t dst_size,
  363. AVFrame *frame, enum AVMediaType frame_type,
  364. AVFilterLink *link)
  365. {
  366. switch (frame_type) {
  367. case AVMEDIA_TYPE_VIDEO:
  368. snprintf(dst, dst_size,
  369. "video pts:%s time:%s size:%dx%d aspect:%d/%d",
  370. av_ts2str(frame->pts), av_ts2timestr(frame->pts, &link->time_base),
  371. frame->width, frame->height,
  372. frame->sample_aspect_ratio.num,
  373. frame->sample_aspect_ratio.den);
  374. break;
  375. case AVMEDIA_TYPE_AUDIO:
  376. snprintf(dst, dst_size,
  377. "audio pts:%s time:%s samples:%d",
  378. av_ts2str(frame->pts), av_ts2timestr(frame->pts, &link->time_base),
  379. frame->nb_samples);
  380. break;
  381. default:
  382. snprintf(dst, dst_size, "%s BUG", av_get_media_type_string(frame_type));
  383. break;
  384. }
  385. return dst;
  386. }
  387. static int rewind_file(AVFilterContext *ctx)
  388. {
  389. MovieContext *movie = ctx->priv;
  390. int64_t timestamp = movie->seek_point;
  391. int ret, i;
  392. if (movie->format_ctx->start_time != AV_NOPTS_VALUE)
  393. timestamp += movie->format_ctx->start_time;
  394. ret = av_seek_frame(movie->format_ctx, -1, timestamp, AVSEEK_FLAG_BACKWARD);
  395. if (ret < 0) {
  396. av_log(ctx, AV_LOG_ERROR, "Unable to loop: %s\n", av_err2str(ret));
  397. movie->loop_count = 1; /* do not try again */
  398. return ret;
  399. }
  400. for (i = 0; i < ctx->nb_outputs; i++) {
  401. avcodec_flush_buffers(movie->st[i].codec_ctx);
  402. movie->st[i].done = 0;
  403. }
  404. movie->eof = 0;
  405. return 0;
  406. }
  407. /**
  408. * Try to push a frame to the requested output.
  409. *
  410. * @param ctx filter context
  411. * @param out_id number of output where a frame is wanted;
  412. * if the frame is read from file, used to set the return value;
  413. * if the codec is being flushed, flush the corresponding stream
  414. * @return 1 if a frame was pushed on the requested output,
  415. * 0 if another attempt is possible,
  416. * <0 AVERROR code
  417. */
  418. static int movie_push_frame(AVFilterContext *ctx, unsigned out_id)
  419. {
  420. MovieContext *movie = ctx->priv;
  421. AVPacket *pkt = &movie->pkt;
  422. enum AVMediaType frame_type;
  423. MovieStream *st;
  424. int ret, got_frame = 0, pkt_out_id;
  425. AVFilterLink *outlink;
  426. AVFrame *frame;
  427. if (!pkt->size) {
  428. if (movie->eof) {
  429. if (movie->st[out_id].done) {
  430. if (movie->loop_count != 1) {
  431. ret = rewind_file(ctx);
  432. if (ret < 0)
  433. return ret;
  434. movie->loop_count -= movie->loop_count > 1;
  435. av_log(ctx, AV_LOG_VERBOSE, "Stream finished, looping.\n");
  436. return 0; /* retry */
  437. }
  438. return AVERROR_EOF;
  439. }
  440. pkt->stream_index = movie->st[out_id].st->index;
  441. /* packet is already ready for flushing */
  442. } else {
  443. ret = av_read_frame(movie->format_ctx, &movie->pkt0);
  444. if (ret < 0) {
  445. av_init_packet(&movie->pkt0); /* ready for flushing */
  446. *pkt = movie->pkt0;
  447. if (ret == AVERROR_EOF) {
  448. movie->eof = 1;
  449. return 0; /* start flushing */
  450. }
  451. return ret;
  452. }
  453. *pkt = movie->pkt0;
  454. }
  455. }
  456. pkt_out_id = pkt->stream_index > movie->max_stream_index ? -1 :
  457. movie->out_index[pkt->stream_index];
  458. if (pkt_out_id < 0) {
  459. av_packet_unref(&movie->pkt0);
  460. pkt->size = 0; /* ready for next run */
  461. pkt->data = NULL;
  462. return 0;
  463. }
  464. st = &movie->st[pkt_out_id];
  465. outlink = ctx->outputs[pkt_out_id];
  466. frame = av_frame_alloc();
  467. if (!frame)
  468. return AVERROR(ENOMEM);
  469. frame_type = st->st->codecpar->codec_type;
  470. switch (frame_type) {
  471. case AVMEDIA_TYPE_VIDEO:
  472. ret = avcodec_decode_video2(st->codec_ctx, frame, &got_frame, pkt);
  473. break;
  474. case AVMEDIA_TYPE_AUDIO:
  475. ret = avcodec_decode_audio4(st->codec_ctx, frame, &got_frame, pkt);
  476. break;
  477. default:
  478. ret = AVERROR(ENOSYS);
  479. break;
  480. }
  481. if (ret < 0) {
  482. av_log(ctx, AV_LOG_WARNING, "Decode error: %s\n", av_err2str(ret));
  483. av_frame_free(&frame);
  484. av_packet_unref(&movie->pkt0);
  485. movie->pkt.size = 0;
  486. movie->pkt.data = NULL;
  487. return 0;
  488. }
  489. if (!ret || st->st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
  490. ret = pkt->size;
  491. pkt->data += ret;
  492. pkt->size -= ret;
  493. if (pkt->size <= 0) {
  494. av_packet_unref(&movie->pkt0);
  495. pkt->size = 0; /* ready for next run */
  496. pkt->data = NULL;
  497. }
  498. if (!got_frame) {
  499. if (!ret)
  500. st->done = 1;
  501. av_frame_free(&frame);
  502. return 0;
  503. }
  504. frame->pts = frame->best_effort_timestamp;
  505. if (frame->pts != AV_NOPTS_VALUE) {
  506. if (movie->ts_offset)
  507. frame->pts += av_rescale_q_rnd(movie->ts_offset, AV_TIME_BASE_Q, outlink->time_base, AV_ROUND_UP);
  508. if (st->discontinuity_threshold) {
  509. if (st->last_pts != AV_NOPTS_VALUE) {
  510. int64_t diff = frame->pts - st->last_pts;
  511. if (diff < 0 || diff > st->discontinuity_threshold) {
  512. av_log(ctx, AV_LOG_VERBOSE, "Discontinuity in stream:%d diff:%"PRId64"\n", pkt_out_id, diff);
  513. movie->ts_offset += av_rescale_q_rnd(-diff, outlink->time_base, AV_TIME_BASE_Q, AV_ROUND_UP);
  514. frame->pts -= diff;
  515. }
  516. }
  517. }
  518. st->last_pts = frame->pts;
  519. }
  520. ff_dlog(ctx, "movie_push_frame(): file:'%s' %s\n", movie->file_name,
  521. describe_frame_to_str((char[1024]){0}, 1024, frame, frame_type, outlink));
  522. if (st->st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
  523. if (frame->format != outlink->format) {
  524. av_log(ctx, AV_LOG_ERROR, "Format changed %s -> %s, discarding frame\n",
  525. av_get_pix_fmt_name(outlink->format),
  526. av_get_pix_fmt_name(frame->format)
  527. );
  528. av_frame_free(&frame);
  529. return 0;
  530. }
  531. }
  532. ret = ff_filter_frame(outlink, frame);
  533. if (ret < 0)
  534. return ret;
  535. return pkt_out_id == out_id;
  536. }
  537. static int movie_request_frame(AVFilterLink *outlink)
  538. {
  539. AVFilterContext *ctx = outlink->src;
  540. unsigned out_id = FF_OUTLINK_IDX(outlink);
  541. int ret;
  542. while (1) {
  543. ret = movie_push_frame(ctx, out_id);
  544. if (ret)
  545. return FFMIN(ret, 0);
  546. }
  547. }
  548. static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
  549. char *res, int res_len, int flags)
  550. {
  551. MovieContext *movie = ctx->priv;
  552. int ret = AVERROR(ENOSYS);
  553. if (!strcmp(cmd, "seek")) {
  554. int idx, flags, i;
  555. int64_t ts;
  556. char tail[2];
  557. if (sscanf(args, "%i|%"SCNi64"|%i %1s", &idx, &ts, &flags, tail) != 3)
  558. return AVERROR(EINVAL);
  559. ret = av_seek_frame(movie->format_ctx, idx, ts, flags);
  560. if (ret < 0)
  561. return ret;
  562. for (i = 0; i < ctx->nb_outputs; i++) {
  563. avcodec_flush_buffers(movie->st[i].codec_ctx);
  564. movie->st[i].done = 0;
  565. }
  566. return ret;
  567. } else if (!strcmp(cmd, "get_duration")) {
  568. int print_len;
  569. char tail[2];
  570. if (!res || res_len <= 0)
  571. return AVERROR(EINVAL);
  572. if (args && sscanf(args, "%1s", tail) == 1)
  573. return AVERROR(EINVAL);
  574. print_len = snprintf(res, res_len, "%"PRId64, movie->format_ctx->duration);
  575. if (print_len < 0 || print_len >= res_len)
  576. return AVERROR(EINVAL);
  577. return 0;
  578. }
  579. return ret;
  580. }
  581. #if CONFIG_MOVIE_FILTER
  582. AVFILTER_DEFINE_CLASS(movie);
  583. AVFilter ff_avsrc_movie = {
  584. .name = "movie",
  585. .description = NULL_IF_CONFIG_SMALL("Read from a movie source."),
  586. .priv_size = sizeof(MovieContext),
  587. .priv_class = &movie_class,
  588. .init = movie_common_init,
  589. .uninit = movie_uninit,
  590. .query_formats = movie_query_formats,
  591. .inputs = NULL,
  592. .outputs = NULL,
  593. .flags = AVFILTER_FLAG_DYNAMIC_OUTPUTS,
  594. .process_command = process_command
  595. };
  596. #endif /* CONFIG_MOVIE_FILTER */
  597. #if CONFIG_AMOVIE_FILTER
  598. #define amovie_options movie_options
  599. AVFILTER_DEFINE_CLASS(amovie);
  600. AVFilter ff_avsrc_amovie = {
  601. .name = "amovie",
  602. .description = NULL_IF_CONFIG_SMALL("Read audio from a movie source."),
  603. .priv_size = sizeof(MovieContext),
  604. .init = movie_common_init,
  605. .uninit = movie_uninit,
  606. .query_formats = movie_query_formats,
  607. .inputs = NULL,
  608. .outputs = NULL,
  609. .priv_class = &amovie_class,
  610. .flags = AVFILTER_FLAG_DYNAMIC_OUTPUTS,
  611. .process_command = process_command,
  612. };
  613. #endif /* CONFIG_AMOVIE_FILTER */