vsrc_movie.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. typedef struct {
  37. const AVClass *class;
  38. int64_t seek_point; ///< seekpoint in microseconds
  39. double seek_point_d;
  40. char *format_name;
  41. char *file_name;
  42. int stream_index;
  43. AVFormatContext *format_ctx;
  44. AVCodecContext *codec_ctx;
  45. int is_done;
  46. AVFrame *frame; ///< video frame to store the decoded images in
  47. int w, h;
  48. AVFilterBufferRef *picref;
  49. } MovieContext;
  50. #define OFFSET(x) offsetof(MovieContext, x)
  51. static const AVOption movie_options[]= {
  52. {"format_name", "set format name", OFFSET(format_name), FF_OPT_TYPE_STRING, {.str = 0}, CHAR_MIN, CHAR_MAX },
  53. {"f", "set format name", OFFSET(format_name), FF_OPT_TYPE_STRING, {.str = 0}, CHAR_MIN, CHAR_MAX },
  54. {"stream_index", "set stream index", OFFSET(stream_index), FF_OPT_TYPE_INT, {.dbl = -1}, -1, INT_MAX },
  55. {"si", "set stream index", OFFSET(stream_index), FF_OPT_TYPE_INT, {.dbl = -1}, -1, INT_MAX },
  56. {"seek_point", "set seekpoint (seconds)", OFFSET(seek_point_d), FF_OPT_TYPE_DOUBLE, {.dbl = 0}, 0, (INT64_MAX-1) / 1000000 },
  57. {"sp", "set seekpoint (seconds)", OFFSET(seek_point_d), FF_OPT_TYPE_DOUBLE, {.dbl = 0}, 0, (INT64_MAX-1) / 1000000 },
  58. {NULL},
  59. };
  60. static const char *movie_get_name(void *ctx)
  61. {
  62. return "movie";
  63. }
  64. static const AVClass movie_class = {
  65. "MovieContext",
  66. movie_get_name,
  67. movie_options
  68. };
  69. static int movie_init(AVFilterContext *ctx)
  70. {
  71. MovieContext *movie = ctx->priv;
  72. AVInputFormat *iformat = NULL;
  73. AVCodec *codec;
  74. int ret;
  75. int64_t timestamp;
  76. av_register_all();
  77. // Try to find the movie format (container)
  78. iformat = movie->format_name ? av_find_input_format(movie->format_name) : NULL;
  79. movie->format_ctx = NULL;
  80. if ((ret = av_open_input_file(&movie->format_ctx, movie->file_name, iformat, 0, NULL)) < 0) {
  81. av_log(ctx, AV_LOG_ERROR,
  82. "Failed to av_open_input_file '%s'\n", movie->file_name);
  83. return ret;
  84. }
  85. if ((ret = av_find_stream_info(movie->format_ctx)) < 0)
  86. av_log(ctx, AV_LOG_WARNING, "Failed to find stream info\n");
  87. // if seeking requested, we execute it
  88. if (movie->seek_point > 0) {
  89. timestamp = movie->seek_point;
  90. // add the stream start time, should it exist
  91. if (movie->format_ctx->start_time != AV_NOPTS_VALUE) {
  92. if (timestamp > INT64_MAX - movie->format_ctx->start_time) {
  93. av_log(ctx, AV_LOG_ERROR,
  94. "%s: seek value overflow with start_time:%"PRId64" seek_point:%"PRId64"\n",
  95. movie->file_name, movie->format_ctx->start_time, movie->seek_point);
  96. return AVERROR(EINVAL);
  97. }
  98. timestamp += movie->format_ctx->start_time;
  99. }
  100. if ((ret = av_seek_frame(movie->format_ctx, -1, timestamp, AVSEEK_FLAG_BACKWARD)) < 0) {
  101. av_log(ctx, AV_LOG_ERROR, "%s: could not seek to position %"PRId64"\n",
  102. movie->file_name, timestamp);
  103. return ret;
  104. }
  105. }
  106. /* select the video stream */
  107. if ((ret = av_find_best_stream(movie->format_ctx, AVMEDIA_TYPE_VIDEO,
  108. movie->stream_index, -1, NULL, 0)) < 0) {
  109. av_log(ctx, AV_LOG_ERROR, "No video stream with index '%d' found\n",
  110. movie->stream_index);
  111. return ret;
  112. }
  113. movie->stream_index = ret;
  114. movie->codec_ctx = movie->format_ctx->streams[movie->stream_index]->codec;
  115. /*
  116. * So now we've got a pointer to the so-called codec context for our video
  117. * stream, but we still have to find the actual codec and open it.
  118. */
  119. codec = avcodec_find_decoder(movie->codec_ctx->codec_id);
  120. if (!codec) {
  121. av_log(ctx, AV_LOG_ERROR, "Failed to find any codec\n");
  122. return AVERROR(EINVAL);
  123. }
  124. if ((ret = avcodec_open(movie->codec_ctx, codec)) < 0) {
  125. av_log(ctx, AV_LOG_ERROR, "Failed to open codec\n");
  126. return ret;
  127. }
  128. if (!(movie->frame = avcodec_alloc_frame()) ) {
  129. av_log(ctx, AV_LOG_ERROR, "Failed to alloc frame\n");
  130. return AVERROR(ENOMEM);
  131. }
  132. movie->w = movie->codec_ctx->width;
  133. movie->h = movie->codec_ctx->height;
  134. av_log(ctx, AV_LOG_INFO, "seek_point:%lld format_name:%s file_name:%s stream_index:%d\n",
  135. movie->seek_point, movie->format_name, movie->file_name,
  136. movie->stream_index);
  137. return 0;
  138. }
  139. static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
  140. {
  141. MovieContext *movie = ctx->priv;
  142. int ret;
  143. movie->class = &movie_class;
  144. av_opt_set_defaults2(movie, 0, 0);
  145. if (args)
  146. movie->file_name = av_get_token(&args, ":");
  147. if (!movie->file_name || !*movie->file_name) {
  148. av_log(ctx, AV_LOG_ERROR, "No filename provided!\n");
  149. return AVERROR(EINVAL);
  150. }
  151. if (*args++ == ':' && (ret = av_set_options_string(movie, args, "=", ":")) < 0) {
  152. av_log(ctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
  153. return ret;
  154. }
  155. movie->seek_point = movie->seek_point_d * 1000000 + 0.5;
  156. return movie_init(ctx);
  157. }
  158. static av_cold void uninit(AVFilterContext *ctx)
  159. {
  160. MovieContext *movie = ctx->priv;
  161. av_free(movie->file_name);
  162. av_free(movie->format_name);
  163. if (movie->codec_ctx)
  164. avcodec_close(movie->codec_ctx);
  165. if (movie->format_ctx)
  166. av_close_input_file(movie->format_ctx);
  167. avfilter_unref_buffer(movie->picref);
  168. av_freep(&movie->frame);
  169. }
  170. static int query_formats(AVFilterContext *ctx)
  171. {
  172. MovieContext *movie = ctx->priv;
  173. enum PixelFormat pix_fmts[] = { movie->codec_ctx->pix_fmt, PIX_FMT_NONE };
  174. avfilter_set_common_formats(ctx, avfilter_make_format_list(pix_fmts));
  175. return 0;
  176. }
  177. static int config_output_props(AVFilterLink *outlink)
  178. {
  179. MovieContext *movie = outlink->src->priv;
  180. outlink->w = movie->w;
  181. outlink->h = movie->h;
  182. outlink->time_base = movie->format_ctx->streams[movie->stream_index]->time_base;
  183. return 0;
  184. }
  185. static int movie_get_frame(AVFilterLink *outlink)
  186. {
  187. MovieContext *movie = outlink->src->priv;
  188. AVPacket pkt;
  189. int ret, frame_decoded;
  190. AVStream *st = movie->format_ctx->streams[movie->stream_index];
  191. if (movie->is_done == 1)
  192. return 0;
  193. while ((ret = av_read_frame(movie->format_ctx, &pkt)) >= 0) {
  194. // Is this a packet from the video stream?
  195. if (pkt.stream_index == movie->stream_index) {
  196. movie->codec_ctx->reordered_opaque = pkt.pos;
  197. avcodec_decode_video2(movie->codec_ctx, movie->frame, &frame_decoded, &pkt);
  198. if (frame_decoded) {
  199. /* FIXME: avoid the memcpy */
  200. movie->picref = avfilter_get_video_buffer(outlink, AV_PERM_WRITE | AV_PERM_PRESERVE |
  201. AV_PERM_REUSE2, outlink->w, outlink->h);
  202. av_image_copy(movie->picref->data, movie->picref->linesize,
  203. movie->frame->data, movie->frame->linesize,
  204. movie->picref->format, outlink->w, outlink->h);
  205. /* FIXME: use a PTS correction mechanism as that in
  206. * ffplay.c when some API will be available for that */
  207. /* use pkt_dts if pkt_pts is not available */
  208. movie->picref->pts = movie->frame->pkt_pts == AV_NOPTS_VALUE ?
  209. movie->frame->pkt_dts : movie->frame->pkt_pts;
  210. movie->picref->pos = movie->frame->reordered_opaque;
  211. movie->picref->video->pixel_aspect = st->sample_aspect_ratio.num ?
  212. st->sample_aspect_ratio : movie->codec_ctx->sample_aspect_ratio;
  213. movie->picref->video->interlaced = movie->frame->interlaced_frame;
  214. movie->picref->video->top_field_first = movie->frame->top_field_first;
  215. movie->picref->video->key_frame = movie->frame->key_frame;
  216. movie->picref->video->pict_type = movie->frame->pict_type;
  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. avfilter_start_frame(outlink, outpicref);
  248. avfilter_draw_slice(outlink, 0, outlink->h, 1);
  249. avfilter_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. };