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.

315 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 "video.h"
  38. typedef struct {
  39. const AVClass *class;
  40. int64_t seek_point; ///< seekpoint in microseconds
  41. double seek_point_d;
  42. char *format_name;
  43. char *file_name;
  44. int stream_index;
  45. AVFormatContext *format_ctx;
  46. AVCodecContext *codec_ctx;
  47. int is_done;
  48. AVFrame *frame; ///< video frame to store the decoded images in
  49. int w, h;
  50. AVFilterBufferRef *picref;
  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, {.dbl = -1}, -1, INT_MAX },
  57. {"si", "set stream index", OFFSET(stream_index), AV_OPT_TYPE_INT, {.dbl = -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. if ((ret = avcodec_open2(movie->codec_ctx, codec, NULL)) < 0) {
  127. av_log(ctx, AV_LOG_ERROR, "Failed to open codec\n");
  128. return ret;
  129. }
  130. if (!(movie->frame = avcodec_alloc_frame()) ) {
  131. av_log(ctx, AV_LOG_ERROR, "Failed to alloc frame\n");
  132. return AVERROR(ENOMEM);
  133. }
  134. movie->w = movie->codec_ctx->width;
  135. movie->h = movie->codec_ctx->height;
  136. av_log(ctx, AV_LOG_INFO, "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, const char *args, void *opaque)
  142. {
  143. MovieContext *movie = ctx->priv;
  144. int ret;
  145. movie->class = &movie_class;
  146. av_opt_set_defaults(movie);
  147. if (args)
  148. movie->file_name = av_get_token(&args, ":");
  149. if (!movie->file_name || !*movie->file_name) {
  150. av_log(ctx, AV_LOG_ERROR, "No filename provided!\n");
  151. return AVERROR(EINVAL);
  152. }
  153. if (*args++ == ':' && (ret = av_set_options_string(movie, args, "=", ":")) < 0) {
  154. av_log(ctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
  155. return ret;
  156. }
  157. movie->seek_point = movie->seek_point_d * 1000000 + 0.5;
  158. return movie_init(ctx);
  159. }
  160. static av_cold void uninit(AVFilterContext *ctx)
  161. {
  162. MovieContext *movie = ctx->priv;
  163. av_free(movie->file_name);
  164. av_free(movie->format_name);
  165. if (movie->codec_ctx)
  166. avcodec_close(movie->codec_ctx);
  167. if (movie->format_ctx)
  168. avformat_close_input(&movie->format_ctx);
  169. avfilter_unref_buffer(movie->picref);
  170. av_freep(&movie->frame);
  171. }
  172. static int query_formats(AVFilterContext *ctx)
  173. {
  174. MovieContext *movie = ctx->priv;
  175. enum PixelFormat pix_fmts[] = { movie->codec_ctx->pix_fmt, PIX_FMT_NONE };
  176. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  177. return 0;
  178. }
  179. static int config_output_props(AVFilterLink *outlink)
  180. {
  181. MovieContext *movie = outlink->src->priv;
  182. outlink->w = movie->w;
  183. outlink->h = movie->h;
  184. outlink->time_base = movie->format_ctx->streams[movie->stream_index]->time_base;
  185. return 0;
  186. }
  187. static int movie_get_frame(AVFilterLink *outlink)
  188. {
  189. MovieContext *movie = outlink->src->priv;
  190. AVPacket pkt;
  191. int ret, frame_decoded;
  192. AVStream *st = movie->format_ctx->streams[movie->stream_index];
  193. if (movie->is_done == 1)
  194. return 0;
  195. while ((ret = av_read_frame(movie->format_ctx, &pkt)) >= 0) {
  196. // Is this a packet from the video stream?
  197. if (pkt.stream_index == movie->stream_index) {
  198. movie->codec_ctx->reordered_opaque = pkt.pos;
  199. avcodec_decode_video2(movie->codec_ctx, movie->frame, &frame_decoded, &pkt);
  200. if (frame_decoded) {
  201. /* FIXME: avoid the memcpy */
  202. movie->picref = avfilter_get_video_buffer(outlink, AV_PERM_WRITE | AV_PERM_PRESERVE |
  203. AV_PERM_REUSE2, outlink->w, outlink->h);
  204. av_image_copy(movie->picref->data, movie->picref->linesize,
  205. movie->frame->data, movie->frame->linesize,
  206. movie->picref->format, outlink->w, outlink->h);
  207. avfilter_copy_frame_props(movie->picref, movie->frame);
  208. /* FIXME: use a PTS correction mechanism as that in
  209. * ffplay.c when some API will be available for that */
  210. /* use pkt_dts if pkt_pts is not available */
  211. movie->picref->pts = movie->frame->pkt_pts == AV_NOPTS_VALUE ?
  212. movie->frame->pkt_dts : movie->frame->pkt_pts;
  213. movie->picref->pos = movie->frame->reordered_opaque;
  214. if (!movie->frame->sample_aspect_ratio.num)
  215. movie->picref->video->pixel_aspect = st->sample_aspect_ratio;
  216. av_dlog(outlink->src,
  217. "movie_get_frame(): file:'%s' pts:%"PRId64" time:%lf pos:%"PRId64" aspect:%d/%d\n",
  218. movie->file_name, movie->picref->pts,
  219. (double)movie->picref->pts * av_q2d(st->time_base),
  220. movie->picref->pos,
  221. movie->picref->video->pixel_aspect.num, movie->picref->video->pixel_aspect.den);
  222. // We got it. Free the packet since we are returning
  223. av_free_packet(&pkt);
  224. return 0;
  225. }
  226. }
  227. // Free the packet that was allocated by av_read_frame
  228. av_free_packet(&pkt);
  229. }
  230. // On multi-frame source we should stop the mixing process when
  231. // the movie source does not have more frames
  232. if (ret == AVERROR_EOF)
  233. movie->is_done = 1;
  234. return ret;
  235. }
  236. static int request_frame(AVFilterLink *outlink)
  237. {
  238. AVFilterBufferRef *outpicref;
  239. MovieContext *movie = outlink->src->priv;
  240. int ret;
  241. if (movie->is_done)
  242. return AVERROR_EOF;
  243. if ((ret = movie_get_frame(outlink)) < 0)
  244. return ret;
  245. outpicref = avfilter_ref_buffer(movie->picref, ~0);
  246. ff_start_frame(outlink, outpicref);
  247. ff_draw_slice(outlink, 0, outlink->h, 1);
  248. ff_end_frame(outlink);
  249. avfilter_unref_buffer(movie->picref);
  250. movie->picref = NULL;
  251. return 0;
  252. }
  253. AVFilter avfilter_vsrc_movie = {
  254. .name = "movie",
  255. .description = NULL_IF_CONFIG_SMALL("Read from a movie source."),
  256. .priv_size = sizeof(MovieContext),
  257. .init = init,
  258. .uninit = uninit,
  259. .query_formats = query_formats,
  260. .inputs = (AVFilterPad[]) {{ .name = NULL }},
  261. .outputs = (AVFilterPad[]) {{ .name = "default",
  262. .type = AVMEDIA_TYPE_VIDEO,
  263. .request_frame = request_frame,
  264. .config_props = config_output_props, },
  265. { .name = NULL}},
  266. };