vsrc_movie.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*
  2. * Copyright (c) 2010 Stefano Sabatini
  3. * Copyright (c) 2008 Victor Paesa
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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. #include <float.h>
  30. #include <stdint.h>
  31. #include "libavutil/attributes.h"
  32. #include "libavutil/avstring.h"
  33. #include "libavutil/opt.h"
  34. #include "libavutil/imgutils.h"
  35. #include "libavformat/avformat.h"
  36. #include "avfilter.h"
  37. #include "formats.h"
  38. #include "internal.h"
  39. #include "video.h"
  40. typedef struct MovieContext {
  41. const AVClass *class;
  42. int64_t seek_point; ///< seekpoint in microseconds
  43. double seek_point_d;
  44. char *format_name;
  45. char *file_name;
  46. int stream_index;
  47. AVFormatContext *format_ctx;
  48. AVCodecContext *codec_ctx;
  49. int is_done;
  50. AVFrame *frame; ///< video frame to store the decoded images in
  51. int w, h;
  52. } MovieContext;
  53. #define OFFSET(x) offsetof(MovieContext, x)
  54. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM
  55. static const AVOption movie_options[]= {
  56. { "filename", NULL, OFFSET(file_name), AV_OPT_TYPE_STRING, .flags = FLAGS },
  57. { "format_name", "set format name", OFFSET(format_name), AV_OPT_TYPE_STRING, .flags = FLAGS },
  58. { "f", "set format name", OFFSET(format_name), AV_OPT_TYPE_STRING, .flags = FLAGS },
  59. { "stream_index", "set stream index", OFFSET(stream_index), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS },
  60. { "si", "set stream index", OFFSET(stream_index), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS },
  61. { "seek_point", "set seekpoint (seconds)", OFFSET(seek_point_d), AV_OPT_TYPE_DOUBLE, { .dbl = 0 }, 0, (INT64_MAX-1) / 1000000, FLAGS },
  62. { "sp", "set seekpoint (seconds)", OFFSET(seek_point_d), AV_OPT_TYPE_DOUBLE, { .dbl = 0 }, 0, (INT64_MAX-1) / 1000000, FLAGS },
  63. { NULL },
  64. };
  65. static const char *movie_get_name(void *ctx)
  66. {
  67. return "movie";
  68. }
  69. static const AVClass movie_class = {
  70. "MovieContext",
  71. movie_get_name,
  72. movie_options
  73. };
  74. static av_cold int movie_init(AVFilterContext *ctx)
  75. {
  76. MovieContext *movie = ctx->priv;
  77. AVInputFormat *iformat = NULL;
  78. AVStream *st;
  79. AVCodec *codec;
  80. int ret;
  81. int64_t timestamp;
  82. av_register_all();
  83. // Try to find the movie format (container)
  84. iformat = movie->format_name ? av_find_input_format(movie->format_name) : NULL;
  85. movie->format_ctx = NULL;
  86. if ((ret = avformat_open_input(&movie->format_ctx, movie->file_name, iformat, NULL)) < 0) {
  87. av_log(ctx, AV_LOG_ERROR,
  88. "Failed to avformat_open_input '%s'\n", movie->file_name);
  89. return ret;
  90. }
  91. if ((ret = avformat_find_stream_info(movie->format_ctx, NULL)) < 0)
  92. av_log(ctx, AV_LOG_WARNING, "Failed to find stream info\n");
  93. // if seeking requested, we execute it
  94. if (movie->seek_point > 0) {
  95. timestamp = movie->seek_point;
  96. // add the stream start time, should it exist
  97. if (movie->format_ctx->start_time != AV_NOPTS_VALUE) {
  98. if (timestamp > INT64_MAX - movie->format_ctx->start_time) {
  99. av_log(ctx, AV_LOG_ERROR,
  100. "%s: seek value overflow with start_time:%"PRId64" seek_point:%"PRId64"\n",
  101. movie->file_name, movie->format_ctx->start_time, movie->seek_point);
  102. return AVERROR(EINVAL);
  103. }
  104. timestamp += movie->format_ctx->start_time;
  105. }
  106. if ((ret = av_seek_frame(movie->format_ctx, -1, timestamp, AVSEEK_FLAG_BACKWARD)) < 0) {
  107. av_log(ctx, AV_LOG_ERROR, "%s: could not seek to position %"PRId64"\n",
  108. movie->file_name, timestamp);
  109. return ret;
  110. }
  111. }
  112. /* select the video stream */
  113. if ((ret = av_find_best_stream(movie->format_ctx, AVMEDIA_TYPE_VIDEO,
  114. movie->stream_index, -1, NULL, 0)) < 0) {
  115. av_log(ctx, AV_LOG_ERROR, "No video stream with index '%d' found\n",
  116. movie->stream_index);
  117. return ret;
  118. }
  119. movie->stream_index = ret;
  120. st = movie->format_ctx->streams[movie->stream_index];
  121. /*
  122. * So now we've got a pointer to the so-called codec context for our video
  123. * stream, but we still have to find the actual codec and open it.
  124. */
  125. codec = avcodec_find_decoder(st->codecpar->codec_id);
  126. if (!codec) {
  127. av_log(ctx, AV_LOG_ERROR, "Failed to find any codec\n");
  128. return AVERROR(EINVAL);
  129. }
  130. movie->codec_ctx = avcodec_alloc_context3(codec);
  131. if (!movie->codec_ctx)
  132. return AVERROR(ENOMEM);
  133. ret = avcodec_parameters_to_context(movie->codec_ctx, st->codecpar);
  134. if (ret < 0)
  135. return ret;
  136. movie->codec_ctx->refcounted_frames = 1;
  137. if ((ret = avcodec_open2(movie->codec_ctx, codec, NULL)) < 0) {
  138. av_log(ctx, AV_LOG_ERROR, "Failed to open codec\n");
  139. return ret;
  140. }
  141. movie->w = movie->codec_ctx->width;
  142. movie->h = movie->codec_ctx->height;
  143. av_log(ctx, AV_LOG_VERBOSE, "seek_point:%"PRIi64" format_name:%s file_name:%s stream_index:%d\n",
  144. movie->seek_point, movie->format_name, movie->file_name,
  145. movie->stream_index);
  146. return 0;
  147. }
  148. static av_cold int init(AVFilterContext *ctx)
  149. {
  150. MovieContext *movie = ctx->priv;
  151. movie->seek_point = movie->seek_point_d * 1000000 + 0.5;
  152. return movie_init(ctx);
  153. }
  154. static av_cold void uninit(AVFilterContext *ctx)
  155. {
  156. MovieContext *movie = ctx->priv;
  157. avcodec_free_context(&movie->codec_ctx);
  158. if (movie->format_ctx)
  159. avformat_close_input(&movie->format_ctx);
  160. av_frame_free(&movie->frame);
  161. }
  162. static int query_formats(AVFilterContext *ctx)
  163. {
  164. MovieContext *movie = ctx->priv;
  165. enum AVPixelFormat pix_fmts[] = { movie->codec_ctx->pix_fmt, AV_PIX_FMT_NONE };
  166. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  167. return 0;
  168. }
  169. static int config_output_props(AVFilterLink *outlink)
  170. {
  171. MovieContext *movie = outlink->src->priv;
  172. outlink->w = movie->w;
  173. outlink->h = movie->h;
  174. outlink->time_base = movie->format_ctx->streams[movie->stream_index]->time_base;
  175. return 0;
  176. }
  177. static int movie_get_frame(AVFilterLink *outlink)
  178. {
  179. MovieContext *movie = outlink->src->priv;
  180. AVPacket pkt;
  181. int ret, frame_decoded;
  182. if (movie->is_done == 1)
  183. return 0;
  184. movie->frame = av_frame_alloc();
  185. if (!movie->frame)
  186. return AVERROR(ENOMEM);
  187. while ((ret = av_read_frame(movie->format_ctx, &pkt)) >= 0) {
  188. // Is this a packet from the video stream?
  189. if (pkt.stream_index == movie->stream_index) {
  190. avcodec_decode_video2(movie->codec_ctx, movie->frame, &frame_decoded, &pkt);
  191. if (frame_decoded) {
  192. if (movie->frame->pkt_pts != AV_NOPTS_VALUE)
  193. movie->frame->pts = movie->frame->pkt_pts;
  194. av_log(outlink->src, AV_LOG_TRACE,
  195. "movie_get_frame(): file:'%s' pts:%"PRId64" time:%f aspect:%d/%d\n",
  196. movie->file_name, movie->frame->pts,
  197. (double)movie->frame->pts *
  198. av_q2d(movie->format_ctx->streams[movie->stream_index]->time_base),
  199. movie->frame->sample_aspect_ratio.num,
  200. movie->frame->sample_aspect_ratio.den);
  201. // We got it. Free the packet since we are returning
  202. av_packet_unref(&pkt);
  203. return 0;
  204. }
  205. }
  206. // Free the packet that was allocated by av_read_frame
  207. av_packet_unref(&pkt);
  208. }
  209. // On multi-frame source we should stop the mixing process when
  210. // the movie source does not have more frames
  211. if (ret == AVERROR_EOF)
  212. movie->is_done = 1;
  213. return ret;
  214. }
  215. static int request_frame(AVFilterLink *outlink)
  216. {
  217. MovieContext *movie = outlink->src->priv;
  218. int ret;
  219. if (movie->is_done)
  220. return AVERROR_EOF;
  221. if ((ret = movie_get_frame(outlink)) < 0)
  222. return ret;
  223. ret = ff_filter_frame(outlink, movie->frame);
  224. movie->frame = NULL;
  225. return ret;
  226. }
  227. static const AVFilterPad avfilter_vsrc_movie_outputs[] = {
  228. {
  229. .name = "default",
  230. .type = AVMEDIA_TYPE_VIDEO,
  231. .request_frame = request_frame,
  232. .config_props = config_output_props,
  233. },
  234. { NULL }
  235. };
  236. AVFilter ff_vsrc_movie = {
  237. .name = "movie",
  238. .description = NULL_IF_CONFIG_SMALL("Read from a movie source."),
  239. .priv_size = sizeof(MovieContext),
  240. .priv_class = &movie_class,
  241. .init = init,
  242. .uninit = uninit,
  243. .query_formats = query_formats,
  244. .inputs = NULL,
  245. .outputs = avfilter_vsrc_movie_outputs,
  246. };