src_movie.c 23 KB

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