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.

316 lines
11KB

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