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.

287 lines
9.3KB

  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. AVCodec *codec;
  79. int ret;
  80. int64_t timestamp;
  81. av_register_all();
  82. // Try to find the movie format (container)
  83. iformat = movie->format_name ? av_find_input_format(movie->format_name) : NULL;
  84. movie->format_ctx = NULL;
  85. if ((ret = avformat_open_input(&movie->format_ctx, movie->file_name, iformat, NULL)) < 0) {
  86. av_log(ctx, AV_LOG_ERROR,
  87. "Failed to avformat_open_input '%s'\n", movie->file_name);
  88. return ret;
  89. }
  90. if ((ret = avformat_find_stream_info(movie->format_ctx, NULL)) < 0)
  91. av_log(ctx, AV_LOG_WARNING, "Failed to find stream info\n");
  92. // if seeking requested, we execute it
  93. if (movie->seek_point > 0) {
  94. timestamp = movie->seek_point;
  95. // add the stream start time, should it exist
  96. if (movie->format_ctx->start_time != AV_NOPTS_VALUE) {
  97. if (timestamp > INT64_MAX - movie->format_ctx->start_time) {
  98. av_log(ctx, AV_LOG_ERROR,
  99. "%s: seek value overflow with start_time:%"PRId64" seek_point:%"PRId64"\n",
  100. movie->file_name, movie->format_ctx->start_time, movie->seek_point);
  101. return AVERROR(EINVAL);
  102. }
  103. timestamp += movie->format_ctx->start_time;
  104. }
  105. if ((ret = av_seek_frame(movie->format_ctx, -1, timestamp, AVSEEK_FLAG_BACKWARD)) < 0) {
  106. av_log(ctx, AV_LOG_ERROR, "%s: could not seek to position %"PRId64"\n",
  107. movie->file_name, timestamp);
  108. return ret;
  109. }
  110. }
  111. /* select the video stream */
  112. if ((ret = av_find_best_stream(movie->format_ctx, AVMEDIA_TYPE_VIDEO,
  113. movie->stream_index, -1, NULL, 0)) < 0) {
  114. av_log(ctx, AV_LOG_ERROR, "No video stream with index '%d' found\n",
  115. movie->stream_index);
  116. return ret;
  117. }
  118. movie->stream_index = ret;
  119. movie->codec_ctx = movie->format_ctx->streams[movie->stream_index]->codec;
  120. /*
  121. * So now we've got a pointer to the so-called codec context for our video
  122. * stream, but we still have to find the actual codec and open it.
  123. */
  124. codec = avcodec_find_decoder(movie->codec_ctx->codec_id);
  125. if (!codec) {
  126. av_log(ctx, AV_LOG_ERROR, "Failed to find any codec\n");
  127. return AVERROR(EINVAL);
  128. }
  129. movie->codec_ctx->refcounted_frames = 1;
  130. if ((ret = avcodec_open2(movie->codec_ctx, codec, NULL)) < 0) {
  131. av_log(ctx, AV_LOG_ERROR, "Failed to open codec\n");
  132. return ret;
  133. }
  134. movie->w = movie->codec_ctx->width;
  135. movie->h = movie->codec_ctx->height;
  136. av_log(ctx, AV_LOG_VERBOSE, "seek_point:%"PRIi64" format_name:%s file_name:%s stream_index:%d\n",
  137. movie->seek_point, movie->format_name, movie->file_name,
  138. movie->stream_index);
  139. return 0;
  140. }
  141. static av_cold int init(AVFilterContext *ctx)
  142. {
  143. MovieContext *movie = ctx->priv;
  144. movie->seek_point = movie->seek_point_d * 1000000 + 0.5;
  145. return movie_init(ctx);
  146. }
  147. static av_cold void uninit(AVFilterContext *ctx)
  148. {
  149. MovieContext *movie = ctx->priv;
  150. if (movie->codec_ctx)
  151. avcodec_close(movie->codec_ctx);
  152. if (movie->format_ctx)
  153. avformat_close_input(&movie->format_ctx);
  154. av_frame_free(&movie->frame);
  155. }
  156. static int query_formats(AVFilterContext *ctx)
  157. {
  158. MovieContext *movie = ctx->priv;
  159. enum AVPixelFormat pix_fmts[] = { movie->codec_ctx->pix_fmt, AV_PIX_FMT_NONE };
  160. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  161. return 0;
  162. }
  163. static int config_output_props(AVFilterLink *outlink)
  164. {
  165. MovieContext *movie = outlink->src->priv;
  166. outlink->w = movie->w;
  167. outlink->h = movie->h;
  168. outlink->time_base = movie->format_ctx->streams[movie->stream_index]->time_base;
  169. return 0;
  170. }
  171. static int movie_get_frame(AVFilterLink *outlink)
  172. {
  173. MovieContext *movie = outlink->src->priv;
  174. AVPacket pkt;
  175. int ret, frame_decoded;
  176. if (movie->is_done == 1)
  177. return 0;
  178. movie->frame = av_frame_alloc();
  179. if (!movie->frame)
  180. return AVERROR(ENOMEM);
  181. while ((ret = av_read_frame(movie->format_ctx, &pkt)) >= 0) {
  182. // Is this a packet from the video stream?
  183. if (pkt.stream_index == movie->stream_index) {
  184. avcodec_decode_video2(movie->codec_ctx, movie->frame, &frame_decoded, &pkt);
  185. if (frame_decoded) {
  186. if (movie->frame->pkt_pts != AV_NOPTS_VALUE)
  187. movie->frame->pts = movie->frame->pkt_pts;
  188. av_log(outlink->src, AV_LOG_TRACE,
  189. "movie_get_frame(): file:'%s' pts:%"PRId64" time:%f aspect:%d/%d\n",
  190. movie->file_name, movie->frame->pts,
  191. (double)movie->frame->pts *
  192. av_q2d(movie->format_ctx->streams[movie->stream_index]->time_base),
  193. movie->frame->sample_aspect_ratio.num,
  194. movie->frame->sample_aspect_ratio.den);
  195. // We got it. Free the packet since we are returning
  196. av_packet_unref(&pkt);
  197. return 0;
  198. }
  199. }
  200. // Free the packet that was allocated by av_read_frame
  201. av_packet_unref(&pkt);
  202. }
  203. // On multi-frame source we should stop the mixing process when
  204. // the movie source does not have more frames
  205. if (ret == AVERROR_EOF)
  206. movie->is_done = 1;
  207. return ret;
  208. }
  209. static int request_frame(AVFilterLink *outlink)
  210. {
  211. MovieContext *movie = outlink->src->priv;
  212. int ret;
  213. if (movie->is_done)
  214. return AVERROR_EOF;
  215. if ((ret = movie_get_frame(outlink)) < 0)
  216. return ret;
  217. ret = ff_filter_frame(outlink, movie->frame);
  218. movie->frame = NULL;
  219. return ret;
  220. }
  221. static const AVFilterPad avfilter_vsrc_movie_outputs[] = {
  222. {
  223. .name = "default",
  224. .type = AVMEDIA_TYPE_VIDEO,
  225. .request_frame = request_frame,
  226. .config_props = config_output_props,
  227. },
  228. { NULL }
  229. };
  230. AVFilter ff_vsrc_movie = {
  231. .name = "movie",
  232. .description = NULL_IF_CONFIG_SMALL("Read from a movie source."),
  233. .priv_size = sizeof(MovieContext),
  234. .priv_class = &movie_class,
  235. .init = init,
  236. .uninit = uninit,
  237. .query_formats = query_formats,
  238. .inputs = NULL,
  239. .outputs = avfilter_vsrc_movie_outputs,
  240. };