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.

300 lines
9.5KB

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