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.

320 lines
11KB

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