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.

297 lines
9.4KB

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