src_movie.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  1. /*
  2. * Copyright (c) 2010 Stefano Sabatini
  3. * Copyright (c) 2008 Victor Paesa
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * movie video source
  24. *
  25. * @todo use direct rendering (no allocation of a new frame)
  26. * @todo support a PTS correction mechanism
  27. */
  28. #include <float.h>
  29. #include <stdint.h>
  30. #include "libavutil/attributes.h"
  31. #include "libavutil/avstring.h"
  32. #include "libavutil/avassert.h"
  33. #include "libavutil/opt.h"
  34. #include "libavutil/imgutils.h"
  35. #include "libavutil/internal.h"
  36. #include "libavutil/timestamp.h"
  37. #include "libavformat/avformat.h"
  38. #include "audio.h"
  39. #include "avfilter.h"
  40. #include "formats.h"
  41. #include "internal.h"
  42. #include "video.h"
  43. typedef struct MovieStream {
  44. AVStream *st;
  45. AVCodecContext *codec_ctx;
  46. int done;
  47. int64_t discontinuity_threshold;
  48. int64_t last_pts;
  49. } MovieStream;
  50. typedef struct MovieContext {
  51. /* common A/V fields */
  52. const AVClass *class;
  53. int64_t seek_point; ///< seekpoint in microseconds
  54. double seek_point_d;
  55. char *format_name;
  56. char *file_name;
  57. char *stream_specs; /**< user-provided list of streams, separated by + */
  58. int stream_index; /**< for compatibility */
  59. int loop_count;
  60. int64_t discontinuity_threshold;
  61. int64_t ts_offset;
  62. AVFormatContext *format_ctx;
  63. int eof;
  64. AVPacket pkt, pkt0;
  65. int max_stream_index; /**< max stream # actually used for output */
  66. MovieStream *st; /**< array of all streams, one per output */
  67. int *out_index; /**< stream number -> output number map, or -1 */
  68. } MovieContext;
  69. #define OFFSET(x) offsetof(MovieContext, x)
  70. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_VIDEO_PARAM
  71. static const AVOption movie_options[]= {
  72. { "filename", NULL, OFFSET(file_name), AV_OPT_TYPE_STRING, .flags = FLAGS },
  73. { "format_name", "set format name", OFFSET(format_name), AV_OPT_TYPE_STRING, .flags = FLAGS },
  74. { "f", "set format name", OFFSET(format_name), AV_OPT_TYPE_STRING, .flags = FLAGS },
  75. { "stream_index", "set stream index", OFFSET(stream_index), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS },
  76. { "si", "set stream index", OFFSET(stream_index), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS },
  77. { "seek_point", "set seekpoint (seconds)", OFFSET(seek_point_d), AV_OPT_TYPE_DOUBLE, { .dbl = 0 }, 0, (INT64_MAX-1) / 1000000, FLAGS },
  78. { "sp", "set seekpoint (seconds)", OFFSET(seek_point_d), AV_OPT_TYPE_DOUBLE, { .dbl = 0 }, 0, (INT64_MAX-1) / 1000000, FLAGS },
  79. { "streams", "set streams", OFFSET(stream_specs), AV_OPT_TYPE_STRING, {.str = 0}, CHAR_MAX, CHAR_MAX, FLAGS },
  80. { "s", "set streams", OFFSET(stream_specs), AV_OPT_TYPE_STRING, {.str = 0}, CHAR_MAX, CHAR_MAX, FLAGS },
  81. { "loop", "set loop count", OFFSET(loop_count), AV_OPT_TYPE_INT, {.i64 = 1}, 0, INT_MAX, FLAGS },
  82. { "discontinuity", "set discontinuity threshold", OFFSET(discontinuity_threshold), AV_OPT_TYPE_DURATION, {.i64 = 0}, 0, INT64_MAX, FLAGS },
  83. { NULL },
  84. };
  85. static int movie_config_output_props(AVFilterLink *outlink);
  86. static int movie_request_frame(AVFilterLink *outlink);
  87. static AVStream *find_stream(void *log, AVFormatContext *avf, const char *spec)
  88. {
  89. int i, ret, already = 0, stream_id = -1;
  90. char type_char[2], dummy;
  91. AVStream *found = NULL;
  92. enum AVMediaType type;
  93. ret = sscanf(spec, "d%1[av]%d%c", type_char, &stream_id, &dummy);
  94. if (ret >= 1 && ret <= 2) {
  95. type = type_char[0] == 'v' ? AVMEDIA_TYPE_VIDEO : AVMEDIA_TYPE_AUDIO;
  96. ret = av_find_best_stream(avf, type, stream_id, -1, NULL, 0);
  97. if (ret < 0) {
  98. av_log(log, AV_LOG_ERROR, "No %s stream with index '%d' found\n",
  99. av_get_media_type_string(type), stream_id);
  100. return NULL;
  101. }
  102. return avf->streams[ret];
  103. }
  104. for (i = 0; i < avf->nb_streams; i++) {
  105. ret = avformat_match_stream_specifier(avf, avf->streams[i], spec);
  106. if (ret < 0) {
  107. av_log(log, AV_LOG_ERROR,
  108. "Invalid stream specifier \"%s\"\n", spec);
  109. return NULL;
  110. }
  111. if (!ret)
  112. continue;
  113. if (avf->streams[i]->discard != AVDISCARD_ALL) {
  114. already++;
  115. continue;
  116. }
  117. if (found) {
  118. av_log(log, AV_LOG_WARNING,
  119. "Ambiguous stream specifier \"%s\", using #%d\n", spec, i);
  120. break;
  121. }
  122. found = avf->streams[i];
  123. }
  124. if (!found) {
  125. av_log(log, AV_LOG_WARNING, "Stream specifier \"%s\" %s\n", spec,
  126. already ? "matched only already used streams" :
  127. "did not match any stream");
  128. return NULL;
  129. }
  130. if (found->codecpar->codec_type != AVMEDIA_TYPE_VIDEO &&
  131. found->codecpar->codec_type != AVMEDIA_TYPE_AUDIO) {
  132. av_log(log, AV_LOG_ERROR, "Stream specifier \"%s\" matched a %s stream,"
  133. "currently unsupported by libavfilter\n", spec,
  134. av_get_media_type_string(found->codecpar->codec_type));
  135. return NULL;
  136. }
  137. return found;
  138. }
  139. static int open_stream(void *log, MovieStream *st)
  140. {
  141. AVCodec *codec;
  142. int ret;
  143. codec = avcodec_find_decoder(st->st->codecpar->codec_id);
  144. if (!codec) {
  145. av_log(log, AV_LOG_ERROR, "Failed to find any codec\n");
  146. return AVERROR(EINVAL);
  147. }
  148. st->codec_ctx = avcodec_alloc_context3(codec);
  149. if (!st->codec_ctx)
  150. return AVERROR(ENOMEM);
  151. ret = avcodec_parameters_to_context(st->codec_ctx, st->st->codecpar);
  152. if (ret < 0)
  153. return ret;
  154. st->codec_ctx->refcounted_frames = 1;
  155. if ((ret = avcodec_open2(st->codec_ctx, codec, NULL)) < 0) {
  156. av_log(log, AV_LOG_ERROR, "Failed to open codec\n");
  157. return ret;
  158. }
  159. return 0;
  160. }
  161. static int guess_channel_layout(MovieStream *st, int st_index, void *log_ctx)
  162. {
  163. AVCodecParameters *dec_par = st->st->codecpar;
  164. char buf[256];
  165. int64_t chl = av_get_default_channel_layout(dec_par->channels);
  166. if (!chl) {
  167. av_log(log_ctx, AV_LOG_ERROR,
  168. "Channel layout is not set in stream %d, and could not "
  169. "be guessed from the number of channels (%d)\n",
  170. st_index, dec_par->channels);
  171. return AVERROR(EINVAL);
  172. }
  173. av_get_channel_layout_string(buf, sizeof(buf), dec_par->channels, chl);
  174. av_log(log_ctx, AV_LOG_WARNING,
  175. "Channel layout is not set in output stream %d, "
  176. "guessed channel layout is '%s'\n",
  177. st_index, buf);
  178. dec_par->channel_layout = chl;
  179. return 0;
  180. }
  181. static av_cold int movie_common_init(AVFilterContext *ctx)
  182. {
  183. MovieContext *movie = ctx->priv;
  184. AVInputFormat *iformat = NULL;
  185. int64_t timestamp;
  186. int nb_streams = 1, ret, i;
  187. char default_streams[16], *stream_specs, *spec, *cursor;
  188. char name[16];
  189. AVStream *st;
  190. if (!movie->file_name) {
  191. av_log(ctx, AV_LOG_ERROR, "No filename provided!\n");
  192. return AVERROR(EINVAL);
  193. }
  194. movie->seek_point = movie->seek_point_d * 1000000 + 0.5;
  195. stream_specs = movie->stream_specs;
  196. if (!stream_specs) {
  197. snprintf(default_streams, sizeof(default_streams), "d%c%d",
  198. !strcmp(ctx->filter->name, "amovie") ? 'a' : 'v',
  199. movie->stream_index);
  200. stream_specs = default_streams;
  201. }
  202. for (cursor = stream_specs; *cursor; cursor++)
  203. if (*cursor == '+')
  204. nb_streams++;
  205. if (movie->loop_count != 1 && nb_streams != 1) {
  206. av_log(ctx, AV_LOG_ERROR,
  207. "Loop with several streams is currently unsupported\n");
  208. return AVERROR_PATCHWELCOME;
  209. }
  210. av_register_all();
  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. ff_insert_outpad(ctx, i, &pad);
  278. if ( movie->st[i].st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
  279. !movie->st[i].st->codecpar->channel_layout) {
  280. ret = guess_channel_layout(&movie->st[i], i, ctx);
  281. if (ret < 0)
  282. return ret;
  283. }
  284. ret = open_stream(ctx, &movie->st[i]);
  285. if (ret < 0)
  286. return ret;
  287. }
  288. av_log(ctx, AV_LOG_VERBOSE, "seek_point:%"PRIi64" format_name:%s file_name:%s stream_index:%d\n",
  289. movie->seek_point, movie->format_name, movie->file_name,
  290. movie->stream_index);
  291. return 0;
  292. }
  293. static av_cold void movie_uninit(AVFilterContext *ctx)
  294. {
  295. MovieContext *movie = ctx->priv;
  296. int i;
  297. for (i = 0; i < ctx->nb_outputs; i++) {
  298. av_freep(&ctx->output_pads[i].name);
  299. if (movie->st[i].st)
  300. avcodec_free_context(&movie->st[i].codec_ctx);
  301. }
  302. av_freep(&movie->st);
  303. av_freep(&movie->out_index);
  304. if (movie->format_ctx)
  305. avformat_close_input(&movie->format_ctx);
  306. }
  307. static int movie_query_formats(AVFilterContext *ctx)
  308. {
  309. MovieContext *movie = ctx->priv;
  310. int list[] = { 0, -1 };
  311. int64_t list64[] = { 0, -1 };
  312. int i, ret;
  313. for (i = 0; i < ctx->nb_outputs; i++) {
  314. MovieStream *st = &movie->st[i];
  315. AVCodecParameters *c = st->st->codecpar;
  316. AVFilterLink *outlink = ctx->outputs[i];
  317. switch (c->codec_type) {
  318. case AVMEDIA_TYPE_VIDEO:
  319. list[0] = c->format;
  320. if ((ret = ff_formats_ref(ff_make_format_list(list), &outlink->in_formats)) < 0)
  321. return ret;
  322. break;
  323. case AVMEDIA_TYPE_AUDIO:
  324. list[0] = c->format;
  325. if ((ret = ff_formats_ref(ff_make_format_list(list), &outlink->in_formats)) < 0)
  326. return ret;
  327. list[0] = c->sample_rate;
  328. if ((ret = ff_formats_ref(ff_make_format_list(list), &outlink->in_samplerates)) < 0)
  329. return ret;
  330. list64[0] = c->channel_layout;
  331. if ((ret = ff_channel_layouts_ref(avfilter_make_format64_list(list64),
  332. &outlink->in_channel_layouts)) < 0)
  333. return ret;
  334. break;
  335. }
  336. }
  337. return 0;
  338. }
  339. static int movie_config_output_props(AVFilterLink *outlink)
  340. {
  341. AVFilterContext *ctx = outlink->src;
  342. MovieContext *movie = ctx->priv;
  343. unsigned out_id = FF_OUTLINK_IDX(outlink);
  344. MovieStream *st = &movie->st[out_id];
  345. AVCodecParameters *c = st->st->codecpar;
  346. outlink->time_base = st->st->time_base;
  347. switch (c->codec_type) {
  348. case AVMEDIA_TYPE_VIDEO:
  349. outlink->w = c->width;
  350. outlink->h = c->height;
  351. outlink->frame_rate = st->st->r_frame_rate;
  352. break;
  353. case AVMEDIA_TYPE_AUDIO:
  354. break;
  355. }
  356. return 0;
  357. }
  358. static char *describe_frame_to_str(char *dst, size_t dst_size,
  359. AVFrame *frame, enum AVMediaType frame_type,
  360. AVFilterLink *link)
  361. {
  362. switch (frame_type) {
  363. case AVMEDIA_TYPE_VIDEO:
  364. snprintf(dst, dst_size,
  365. "video pts:%s time:%s size:%dx%d aspect:%d/%d",
  366. av_ts2str(frame->pts), av_ts2timestr(frame->pts, &link->time_base),
  367. frame->width, frame->height,
  368. frame->sample_aspect_ratio.num,
  369. frame->sample_aspect_ratio.den);
  370. break;
  371. case AVMEDIA_TYPE_AUDIO:
  372. snprintf(dst, dst_size,
  373. "audio pts:%s time:%s samples:%d",
  374. av_ts2str(frame->pts), av_ts2timestr(frame->pts, &link->time_base),
  375. frame->nb_samples);
  376. break;
  377. default:
  378. snprintf(dst, dst_size, "%s BUG", av_get_media_type_string(frame_type));
  379. break;
  380. }
  381. return dst;
  382. }
  383. static int rewind_file(AVFilterContext *ctx)
  384. {
  385. MovieContext *movie = ctx->priv;
  386. int64_t timestamp = movie->seek_point;
  387. int ret, i;
  388. if (movie->format_ctx->start_time != AV_NOPTS_VALUE)
  389. timestamp += movie->format_ctx->start_time;
  390. ret = av_seek_frame(movie->format_ctx, -1, timestamp, AVSEEK_FLAG_BACKWARD);
  391. if (ret < 0) {
  392. av_log(ctx, AV_LOG_ERROR, "Unable to loop: %s\n", av_err2str(ret));
  393. movie->loop_count = 1; /* do not try again */
  394. return ret;
  395. }
  396. for (i = 0; i < ctx->nb_outputs; i++) {
  397. avcodec_flush_buffers(movie->st[i].codec_ctx);
  398. movie->st[i].done = 0;
  399. }
  400. movie->eof = 0;
  401. return 0;
  402. }
  403. /**
  404. * Try to push a frame to the requested output.
  405. *
  406. * @param ctx filter context
  407. * @param out_id number of output where a frame is wanted;
  408. * if the frame is read from file, used to set the return value;
  409. * if the codec is being flushed, flush the corresponding stream
  410. * @return 1 if a frame was pushed on the requested output,
  411. * 0 if another attempt is possible,
  412. * <0 AVERROR code
  413. */
  414. static int movie_push_frame(AVFilterContext *ctx, unsigned out_id)
  415. {
  416. MovieContext *movie = ctx->priv;
  417. AVPacket *pkt = &movie->pkt;
  418. enum AVMediaType frame_type;
  419. MovieStream *st;
  420. int ret, got_frame = 0, pkt_out_id;
  421. AVFilterLink *outlink;
  422. AVFrame *frame;
  423. if (!pkt->size) {
  424. if (movie->eof) {
  425. if (movie->st[out_id].done) {
  426. if (movie->loop_count != 1) {
  427. ret = rewind_file(ctx);
  428. if (ret < 0)
  429. return ret;
  430. movie->loop_count -= movie->loop_count > 1;
  431. av_log(ctx, AV_LOG_VERBOSE, "Stream finished, looping.\n");
  432. return 0; /* retry */
  433. }
  434. return AVERROR_EOF;
  435. }
  436. pkt->stream_index = movie->st[out_id].st->index;
  437. /* packet is already ready for flushing */
  438. } else {
  439. ret = av_read_frame(movie->format_ctx, &movie->pkt0);
  440. if (ret < 0) {
  441. av_init_packet(&movie->pkt0); /* ready for flushing */
  442. *pkt = movie->pkt0;
  443. if (ret == AVERROR_EOF) {
  444. movie->eof = 1;
  445. return 0; /* start flushing */
  446. }
  447. return ret;
  448. }
  449. *pkt = movie->pkt0;
  450. }
  451. }
  452. pkt_out_id = pkt->stream_index > movie->max_stream_index ? -1 :
  453. movie->out_index[pkt->stream_index];
  454. if (pkt_out_id < 0) {
  455. av_packet_unref(&movie->pkt0);
  456. pkt->size = 0; /* ready for next run */
  457. pkt->data = NULL;
  458. return 0;
  459. }
  460. st = &movie->st[pkt_out_id];
  461. outlink = ctx->outputs[pkt_out_id];
  462. frame = av_frame_alloc();
  463. if (!frame)
  464. return AVERROR(ENOMEM);
  465. frame_type = st->st->codecpar->codec_type;
  466. switch (frame_type) {
  467. case AVMEDIA_TYPE_VIDEO:
  468. ret = avcodec_decode_video2(st->codec_ctx, frame, &got_frame, pkt);
  469. break;
  470. case AVMEDIA_TYPE_AUDIO:
  471. ret = avcodec_decode_audio4(st->codec_ctx, frame, &got_frame, pkt);
  472. break;
  473. default:
  474. ret = AVERROR(ENOSYS);
  475. break;
  476. }
  477. if (ret < 0) {
  478. av_log(ctx, AV_LOG_WARNING, "Decode error: %s\n", av_err2str(ret));
  479. av_frame_free(&frame);
  480. av_packet_unref(&movie->pkt0);
  481. movie->pkt.size = 0;
  482. movie->pkt.data = NULL;
  483. return 0;
  484. }
  485. if (!ret || st->st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
  486. ret = pkt->size;
  487. pkt->data += ret;
  488. pkt->size -= ret;
  489. if (pkt->size <= 0) {
  490. av_packet_unref(&movie->pkt0);
  491. pkt->size = 0; /* ready for next run */
  492. pkt->data = NULL;
  493. }
  494. if (!got_frame) {
  495. if (!ret)
  496. st->done = 1;
  497. av_frame_free(&frame);
  498. return 0;
  499. }
  500. frame->pts = av_frame_get_best_effort_timestamp(frame);
  501. if (frame->pts != AV_NOPTS_VALUE) {
  502. if (movie->ts_offset)
  503. frame->pts += av_rescale_q_rnd(movie->ts_offset, AV_TIME_BASE_Q, outlink->time_base, AV_ROUND_UP);
  504. if (st->discontinuity_threshold) {
  505. if (st->last_pts != AV_NOPTS_VALUE) {
  506. int64_t diff = frame->pts - st->last_pts;
  507. if (diff < 0 || diff > st->discontinuity_threshold) {
  508. av_log(ctx, AV_LOG_VERBOSE, "Discontinuity in stream:%d diff:%"PRId64"\n", pkt_out_id, diff);
  509. movie->ts_offset += av_rescale_q_rnd(-diff, outlink->time_base, AV_TIME_BASE_Q, AV_ROUND_UP);
  510. frame->pts -= diff;
  511. }
  512. }
  513. }
  514. st->last_pts = frame->pts;
  515. }
  516. ff_dlog(ctx, "movie_push_frame(): file:'%s' %s\n", movie->file_name,
  517. describe_frame_to_str((char[1024]){0}, 1024, frame, frame_type, outlink));
  518. if (st->st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
  519. if (frame->format != outlink->format) {
  520. av_log(ctx, AV_LOG_ERROR, "Format changed %s -> %s, discarding frame\n",
  521. av_get_pix_fmt_name(outlink->format),
  522. av_get_pix_fmt_name(frame->format)
  523. );
  524. av_frame_free(&frame);
  525. return 0;
  526. }
  527. }
  528. ret = ff_filter_frame(outlink, frame);
  529. if (ret < 0)
  530. return ret;
  531. return pkt_out_id == out_id;
  532. }
  533. static int movie_request_frame(AVFilterLink *outlink)
  534. {
  535. AVFilterContext *ctx = outlink->src;
  536. unsigned out_id = FF_OUTLINK_IDX(outlink);
  537. int ret;
  538. while (1) {
  539. ret = movie_push_frame(ctx, out_id);
  540. if (ret)
  541. return FFMIN(ret, 0);
  542. }
  543. }
  544. static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
  545. char *res, int res_len, int flags)
  546. {
  547. MovieContext *movie = ctx->priv;
  548. int ret = AVERROR(ENOSYS);
  549. if (!strcmp(cmd, "seek")) {
  550. int idx, flags, i;
  551. int64_t ts;
  552. char tail[2];
  553. if (sscanf(args, "%i|%"SCNi64"|%i %1s", &idx, &ts, &flags, tail) != 3)
  554. return AVERROR(EINVAL);
  555. ret = av_seek_frame(movie->format_ctx, idx, ts, flags);
  556. if (ret < 0)
  557. return ret;
  558. for (i = 0; i < ctx->nb_outputs; i++) {
  559. avcodec_flush_buffers(movie->st[i].codec_ctx);
  560. movie->st[i].done = 0;
  561. }
  562. return ret;
  563. } else if (!strcmp(cmd, "get_duration")) {
  564. int print_len;
  565. char tail[2];
  566. if (!res || res_len <= 0)
  567. return AVERROR(EINVAL);
  568. if (args && sscanf(args, "%1s", tail) == 1)
  569. return AVERROR(EINVAL);
  570. print_len = snprintf(res, res_len, "%"PRId64, movie->format_ctx->duration);
  571. if (print_len < 0 || print_len >= res_len)
  572. return AVERROR(EINVAL);
  573. return 0;
  574. }
  575. return ret;
  576. }
  577. #if CONFIG_MOVIE_FILTER
  578. AVFILTER_DEFINE_CLASS(movie);
  579. AVFilter ff_avsrc_movie = {
  580. .name = "movie",
  581. .description = NULL_IF_CONFIG_SMALL("Read from a movie source."),
  582. .priv_size = sizeof(MovieContext),
  583. .priv_class = &movie_class,
  584. .init = movie_common_init,
  585. .uninit = movie_uninit,
  586. .query_formats = movie_query_formats,
  587. .inputs = NULL,
  588. .outputs = NULL,
  589. .flags = AVFILTER_FLAG_DYNAMIC_OUTPUTS,
  590. .process_command = process_command
  591. };
  592. #endif /* CONFIG_MOVIE_FILTER */
  593. #if CONFIG_AMOVIE_FILTER
  594. #define amovie_options movie_options
  595. AVFILTER_DEFINE_CLASS(amovie);
  596. AVFilter ff_avsrc_amovie = {
  597. .name = "amovie",
  598. .description = NULL_IF_CONFIG_SMALL("Read audio from a movie source."),
  599. .priv_size = sizeof(MovieContext),
  600. .init = movie_common_init,
  601. .uninit = movie_uninit,
  602. .query_formats = movie_query_formats,
  603. .inputs = NULL,
  604. .outputs = NULL,
  605. .priv_class = &amovie_class,
  606. .flags = AVFILTER_FLAG_DYNAMIC_OUTPUTS,
  607. .process_command = process_command,
  608. };
  609. #endif /* CONFIG_AMOVIE_FILTER */