src_movie.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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. * @todo support more than one output stream
  28. */
  29. /* #define DEBUG */
  30. #include <float.h>
  31. #include "libavutil/avstring.h"
  32. #include "libavutil/opt.h"
  33. #include "libavutil/imgutils.h"
  34. #include "libavformat/avformat.h"
  35. #include "audio.h"
  36. #include "avcodec.h"
  37. #include "avfilter.h"
  38. #include "formats.h"
  39. #include "internal.h"
  40. #include "video.h"
  41. typedef struct {
  42. /* common A/V fields */
  43. const AVClass *class;
  44. int64_t seek_point; ///< seekpoint in microseconds
  45. double seek_point_d;
  46. char *format_name;
  47. char *file_name;
  48. int stream_index;
  49. int loop_count;
  50. AVFormatContext *format_ctx;
  51. AVCodecContext *codec_ctx;
  52. int is_done;
  53. AVFrame *frame; ///< video frame to store the decoded images in
  54. /* video-only fields */
  55. int w, h;
  56. AVFilterBufferRef *picref;
  57. /* audio-only fields */
  58. int bps; ///< bytes per sample
  59. AVPacket pkt, pkt0;
  60. AVFilterBufferRef *samplesref;
  61. } MovieContext;
  62. #define OFFSET(x) offsetof(MovieContext, x)
  63. static const AVOption movie_options[]= {
  64. {"format_name", "set format name", OFFSET(format_name), AV_OPT_TYPE_STRING, {.str = 0}, CHAR_MIN, CHAR_MAX },
  65. {"f", "set format name", OFFSET(format_name), AV_OPT_TYPE_STRING, {.str = 0}, CHAR_MIN, CHAR_MAX },
  66. {"stream_index", "set stream index", OFFSET(stream_index), AV_OPT_TYPE_INT, {.dbl = -1}, -1, INT_MAX },
  67. {"si", "set stream index", OFFSET(stream_index), AV_OPT_TYPE_INT, {.dbl = -1}, -1, INT_MAX },
  68. {"seek_point", "set seekpoint (seconds)", OFFSET(seek_point_d), AV_OPT_TYPE_DOUBLE, {.dbl = 0}, 0, (INT64_MAX-1) / 1000000 },
  69. {"sp", "set seekpoint (seconds)", OFFSET(seek_point_d), AV_OPT_TYPE_DOUBLE, {.dbl = 0}, 0, (INT64_MAX-1) / 1000000 },
  70. {"loop", "set loop count", OFFSET(loop_count), AV_OPT_TYPE_INT, {.dbl = 1}, 0, INT_MAX },
  71. {NULL},
  72. };
  73. AVFILTER_DEFINE_CLASS(movie);
  74. static av_cold int movie_common_init(AVFilterContext *ctx, const char *args,
  75. enum AVMediaType type)
  76. {
  77. MovieContext *movie = ctx->priv;
  78. AVInputFormat *iformat = NULL;
  79. AVCodec *codec;
  80. int64_t timestamp;
  81. int ret;
  82. movie->class = &movie_class;
  83. av_opt_set_defaults(movie);
  84. if (args)
  85. movie->file_name = av_get_token(&args, ":");
  86. if (!movie->file_name || !*movie->file_name) {
  87. av_log(ctx, AV_LOG_ERROR, "No filename provided!\n");
  88. return AVERROR(EINVAL);
  89. }
  90. if (*args++ == ':' && (ret = av_set_options_string(movie, args, "=", ":")) < 0) {
  91. av_log(ctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
  92. return ret;
  93. }
  94. movie->seek_point = movie->seek_point_d * 1000000 + 0.5;
  95. av_register_all();
  96. // Try to find the movie format (container)
  97. iformat = movie->format_name ? av_find_input_format(movie->format_name) : NULL;
  98. movie->format_ctx = NULL;
  99. if ((ret = avformat_open_input(&movie->format_ctx, movie->file_name, iformat, NULL)) < 0) {
  100. av_log(ctx, AV_LOG_ERROR,
  101. "Failed to avformat_open_input '%s'\n", movie->file_name);
  102. return ret;
  103. }
  104. if ((ret = avformat_find_stream_info(movie->format_ctx, NULL)) < 0)
  105. av_log(ctx, AV_LOG_WARNING, "Failed to find stream info\n");
  106. // if seeking requested, we execute it
  107. if (movie->seek_point > 0) {
  108. timestamp = movie->seek_point;
  109. // add the stream start time, should it exist
  110. if (movie->format_ctx->start_time != AV_NOPTS_VALUE) {
  111. if (timestamp > INT64_MAX - movie->format_ctx->start_time) {
  112. av_log(ctx, AV_LOG_ERROR,
  113. "%s: seek value overflow with start_time:%"PRId64" seek_point:%"PRId64"\n",
  114. movie->file_name, movie->format_ctx->start_time, movie->seek_point);
  115. return AVERROR(EINVAL);
  116. }
  117. timestamp += movie->format_ctx->start_time;
  118. }
  119. if ((ret = av_seek_frame(movie->format_ctx, -1, timestamp, AVSEEK_FLAG_BACKWARD)) < 0) {
  120. av_log(ctx, AV_LOG_ERROR, "%s: could not seek to position %"PRId64"\n",
  121. movie->file_name, timestamp);
  122. return ret;
  123. }
  124. }
  125. /* select the media stream */
  126. if ((ret = av_find_best_stream(movie->format_ctx, type,
  127. movie->stream_index, -1, NULL, 0)) < 0) {
  128. av_log(ctx, AV_LOG_ERROR, "No %s stream with index '%d' found\n",
  129. av_get_media_type_string(type), movie->stream_index);
  130. return ret;
  131. }
  132. movie->stream_index = ret;
  133. movie->codec_ctx = movie->format_ctx->streams[movie->stream_index]->codec;
  134. /*
  135. * So now we've got a pointer to the so-called codec context for our video
  136. * stream, but we still have to find the actual codec and open it.
  137. */
  138. codec = avcodec_find_decoder(movie->codec_ctx->codec_id);
  139. if (!codec) {
  140. av_log(ctx, AV_LOG_ERROR, "Failed to find any codec\n");
  141. return AVERROR(EINVAL);
  142. }
  143. if ((ret = avcodec_open2(movie->codec_ctx, codec, NULL)) < 0) {
  144. av_log(ctx, AV_LOG_ERROR, "Failed to open codec\n");
  145. return ret;
  146. }
  147. av_log(ctx, AV_LOG_VERBOSE, "seek_point:%"PRIi64" format_name:%s file_name:%s stream_index:%d\n",
  148. movie->seek_point, movie->format_name, movie->file_name,
  149. movie->stream_index);
  150. if (!(movie->frame = avcodec_alloc_frame()) ) {
  151. av_log(ctx, AV_LOG_ERROR, "Failed to alloc frame\n");
  152. return AVERROR(ENOMEM);
  153. }
  154. return 0;
  155. }
  156. static av_cold void movie_common_uninit(AVFilterContext *ctx)
  157. {
  158. MovieContext *movie = ctx->priv;
  159. av_free(movie->file_name);
  160. av_free(movie->format_name);
  161. if (movie->codec_ctx)
  162. avcodec_close(movie->codec_ctx);
  163. if (movie->format_ctx)
  164. avformat_close_input(&movie->format_ctx);
  165. avfilter_unref_buffer(movie->picref);
  166. av_freep(&movie->frame);
  167. avfilter_unref_buffer(movie->samplesref);
  168. }
  169. #if CONFIG_MOVIE_FILTER
  170. static av_cold int movie_init(AVFilterContext *ctx, const char *args)
  171. {
  172. MovieContext *movie = ctx->priv;
  173. int ret;
  174. if ((ret = movie_common_init(ctx, args, AVMEDIA_TYPE_VIDEO)) < 0)
  175. return ret;
  176. movie->w = movie->codec_ctx->width;
  177. movie->h = movie->codec_ctx->height;
  178. return 0;
  179. }
  180. static int movie_query_formats(AVFilterContext *ctx)
  181. {
  182. MovieContext *movie = ctx->priv;
  183. enum PixelFormat pix_fmts[] = { movie->codec_ctx->pix_fmt, PIX_FMT_NONE };
  184. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  185. return 0;
  186. }
  187. static int movie_config_output_props(AVFilterLink *outlink)
  188. {
  189. MovieContext *movie = outlink->src->priv;
  190. outlink->w = movie->w;
  191. outlink->h = movie->h;
  192. outlink->time_base = movie->format_ctx->streams[movie->stream_index]->time_base;
  193. return 0;
  194. }
  195. static int movie_get_frame(AVFilterLink *outlink)
  196. {
  197. MovieContext *movie = outlink->src->priv;
  198. AVPacket pkt;
  199. int ret, frame_decoded;
  200. AVStream *st = movie->format_ctx->streams[movie->stream_index];
  201. if (movie->is_done == 1)
  202. return 0;
  203. while (1) {
  204. ret = av_read_frame(movie->format_ctx, &pkt);
  205. if (ret == AVERROR_EOF) {
  206. int64_t timestamp;
  207. if (movie->loop_count != 1) {
  208. timestamp = movie->seek_point;
  209. if (movie->format_ctx->start_time != AV_NOPTS_VALUE)
  210. timestamp += movie->format_ctx->start_time;
  211. if (av_seek_frame(movie->format_ctx, -1, timestamp, AVSEEK_FLAG_BACKWARD) < 0) {
  212. movie->is_done = 1;
  213. break;
  214. } else if (movie->loop_count>1)
  215. movie->loop_count--;
  216. continue;
  217. } else {
  218. movie->is_done = 1;
  219. break;
  220. }
  221. } else if (ret < 0)
  222. break;
  223. // Is this a packet from the video stream?
  224. if (pkt.stream_index == movie->stream_index) {
  225. avcodec_decode_video2(movie->codec_ctx, movie->frame, &frame_decoded, &pkt);
  226. if (frame_decoded) {
  227. /* FIXME: avoid the memcpy */
  228. movie->picref = ff_get_video_buffer(outlink, AV_PERM_WRITE | AV_PERM_PRESERVE |
  229. AV_PERM_REUSE2, outlink->w, outlink->h);
  230. av_image_copy(movie->picref->data, movie->picref->linesize,
  231. (void*)movie->frame->data, movie->frame->linesize,
  232. movie->picref->format, outlink->w, outlink->h);
  233. avfilter_copy_frame_props(movie->picref, movie->frame);
  234. /* FIXME: use a PTS correction mechanism as that in
  235. * ffplay.c when some API will be available for that */
  236. /* use pkt_dts if pkt_pts is not available */
  237. movie->picref->pts = movie->frame->pkt_pts == AV_NOPTS_VALUE ?
  238. movie->frame->pkt_dts : movie->frame->pkt_pts;
  239. if (!movie->frame->sample_aspect_ratio.num)
  240. movie->picref->video->sample_aspect_ratio = st->sample_aspect_ratio;
  241. av_dlog(outlink->src,
  242. "movie_get_frame(): file:'%s' pts:%"PRId64" time:%lf pos:%"PRId64" aspect:%d/%d\n",
  243. movie->file_name, movie->picref->pts,
  244. (double)movie->picref->pts * av_q2d(st->time_base),
  245. movie->picref->pos,
  246. movie->picref->video->sample_aspect_ratio.num,
  247. movie->picref->video->sample_aspect_ratio.den);
  248. // We got it. Free the packet since we are returning
  249. av_free_packet(&pkt);
  250. return 0;
  251. }
  252. }
  253. // Free the packet that was allocated by av_read_frame
  254. av_free_packet(&pkt);
  255. }
  256. return ret;
  257. }
  258. static int movie_request_frame(AVFilterLink *outlink)
  259. {
  260. AVFilterBufferRef *outpicref;
  261. MovieContext *movie = outlink->src->priv;
  262. int ret;
  263. if (movie->is_done)
  264. return AVERROR_EOF;
  265. if ((ret = movie_get_frame(outlink)) < 0)
  266. return ret;
  267. outpicref = avfilter_ref_buffer(movie->picref, ~0);
  268. ff_start_frame(outlink, outpicref);
  269. ff_draw_slice(outlink, 0, outlink->h, 1);
  270. ff_end_frame(outlink);
  271. avfilter_unref_buffer(movie->picref);
  272. movie->picref = NULL;
  273. return 0;
  274. }
  275. AVFilter avfilter_vsrc_movie = {
  276. .name = "movie",
  277. .description = NULL_IF_CONFIG_SMALL("Read from a movie source."),
  278. .priv_size = sizeof(MovieContext),
  279. .init = movie_init,
  280. .uninit = movie_common_uninit,
  281. .query_formats = movie_query_formats,
  282. .inputs = (const AVFilterPad[]) {{ .name = NULL }},
  283. .outputs = (const AVFilterPad[]) {{ .name = "default",
  284. .type = AVMEDIA_TYPE_VIDEO,
  285. .request_frame = movie_request_frame,
  286. .config_props = movie_config_output_props, },
  287. { .name = NULL}},
  288. };
  289. #endif /* CONFIG_MOVIE_FILTER */
  290. #if CONFIG_AMOVIE_FILTER
  291. static av_cold int amovie_init(AVFilterContext *ctx, const char *args)
  292. {
  293. MovieContext *movie = ctx->priv;
  294. int ret;
  295. if ((ret = movie_common_init(ctx, args, AVMEDIA_TYPE_AUDIO)) < 0)
  296. return ret;
  297. movie->bps = av_get_bytes_per_sample(movie->codec_ctx->sample_fmt);
  298. return 0;
  299. }
  300. static int amovie_query_formats(AVFilterContext *ctx)
  301. {
  302. MovieContext *movie = ctx->priv;
  303. AVCodecContext *c = movie->codec_ctx;
  304. enum AVSampleFormat sample_fmts[] = { c->sample_fmt, -1 };
  305. int sample_rates[] = { c->sample_rate, -1 };
  306. int64_t chlayouts[] = { c->channel_layout ? c->channel_layout :
  307. av_get_default_channel_layout(c->channels), -1 };
  308. ff_set_common_formats (ctx, ff_make_format_list(sample_fmts));
  309. ff_set_common_samplerates (ctx, ff_make_format_list(sample_rates));
  310. ff_set_common_channel_layouts(ctx, avfilter_make_format64_list(chlayouts));
  311. return 0;
  312. }
  313. static int amovie_config_output_props(AVFilterLink *outlink)
  314. {
  315. MovieContext *movie = outlink->src->priv;
  316. AVCodecContext *c = movie->codec_ctx;
  317. outlink->sample_rate = c->sample_rate;
  318. outlink->time_base = movie->format_ctx->streams[movie->stream_index]->time_base;
  319. return 0;
  320. }
  321. static int amovie_get_samples(AVFilterLink *outlink)
  322. {
  323. MovieContext *movie = outlink->src->priv;
  324. AVPacket pkt;
  325. int ret, got_frame = 0;
  326. if (!movie->pkt.size && movie->is_done == 1)
  327. return AVERROR_EOF;
  328. /* check for another frame, in case the previous one was completely consumed */
  329. if (!movie->pkt.size) {
  330. while ((ret = av_read_frame(movie->format_ctx, &pkt)) >= 0) {
  331. // Is this a packet from the selected stream?
  332. if (pkt.stream_index != movie->stream_index) {
  333. av_free_packet(&pkt);
  334. continue;
  335. } else {
  336. movie->pkt0 = movie->pkt = pkt;
  337. break;
  338. }
  339. }
  340. if (ret == AVERROR_EOF) {
  341. movie->is_done = 1;
  342. return ret;
  343. }
  344. }
  345. /* decode and update the movie pkt */
  346. avcodec_get_frame_defaults(movie->frame);
  347. ret = avcodec_decode_audio4(movie->codec_ctx, movie->frame, &got_frame, &movie->pkt);
  348. if (ret < 0) {
  349. movie->pkt.size = 0;
  350. return ret;
  351. }
  352. movie->pkt.data += ret;
  353. movie->pkt.size -= ret;
  354. /* wrap the decoded data in a samplesref */
  355. if (got_frame) {
  356. int nb_samples = movie->frame->nb_samples;
  357. int data_size =
  358. av_samples_get_buffer_size(NULL, movie->codec_ctx->channels,
  359. nb_samples, movie->codec_ctx->sample_fmt, 1);
  360. if (data_size < 0)
  361. return data_size;
  362. movie->samplesref =
  363. ff_get_audio_buffer(outlink, AV_PERM_WRITE, nb_samples);
  364. memcpy(movie->samplesref->data[0], movie->frame->data[0], data_size);
  365. movie->samplesref->pts = movie->pkt.pts;
  366. movie->samplesref->pos = movie->pkt.pos;
  367. movie->samplesref->audio->sample_rate = movie->codec_ctx->sample_rate;
  368. }
  369. // We got it. Free the packet since we are returning
  370. if (movie->pkt.size <= 0)
  371. av_free_packet(&movie->pkt0);
  372. return 0;
  373. }
  374. static int amovie_request_frame(AVFilterLink *outlink)
  375. {
  376. MovieContext *movie = outlink->src->priv;
  377. int ret;
  378. if (movie->is_done)
  379. return AVERROR_EOF;
  380. do {
  381. if ((ret = amovie_get_samples(outlink)) < 0)
  382. return ret;
  383. } while (!movie->samplesref);
  384. ff_filter_samples(outlink, avfilter_ref_buffer(movie->samplesref, ~0));
  385. avfilter_unref_buffer(movie->samplesref);
  386. movie->samplesref = NULL;
  387. return 0;
  388. }
  389. AVFilter avfilter_asrc_amovie = {
  390. .name = "amovie",
  391. .description = NULL_IF_CONFIG_SMALL("Read audio from a movie source."),
  392. .priv_size = sizeof(MovieContext),
  393. .init = amovie_init,
  394. .uninit = movie_common_uninit,
  395. .query_formats = amovie_query_formats,
  396. .inputs = (const AVFilterPad[]) {{ .name = NULL }},
  397. .outputs = (const AVFilterPad[]) {{ .name = "default",
  398. .type = AVMEDIA_TYPE_AUDIO,
  399. .request_frame = amovie_request_frame,
  400. .config_props = amovie_config_output_props, },
  401. { .name = NULL}},
  402. };
  403. #endif /* CONFIG_AMOVIE_FILTER */