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.

490 lines
16KB

  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. int loop_count;
  46. AVFormatContext *format_ctx;
  47. AVCodecContext *codec_ctx;
  48. int is_done;
  49. AVFrame *frame; ///< video frame to store the decoded images in
  50. /* video-only fields */
  51. int w, h;
  52. AVFilterBufferRef *picref;
  53. /* audio-only fields */
  54. int bps; ///< bytes per sample
  55. AVPacket pkt, pkt0;
  56. AVFilterBufferRef *samplesref;
  57. } MovieContext;
  58. #define OFFSET(x) offsetof(MovieContext, x)
  59. static const AVOption movie_options[]= {
  60. {"format_name", "set format name", OFFSET(format_name), AV_OPT_TYPE_STRING, {.str = 0}, CHAR_MIN, CHAR_MAX },
  61. {"f", "set format name", OFFSET(format_name), AV_OPT_TYPE_STRING, {.str = 0}, CHAR_MIN, CHAR_MAX },
  62. {"stream_index", "set stream index", OFFSET(stream_index), AV_OPT_TYPE_INT, {.dbl = -1}, -1, INT_MAX },
  63. {"si", "set stream index", OFFSET(stream_index), AV_OPT_TYPE_INT, {.dbl = -1}, -1, INT_MAX },
  64. {"seek_point", "set seekpoint (seconds)", OFFSET(seek_point_d), AV_OPT_TYPE_DOUBLE, {.dbl = 0}, 0, (INT64_MAX-1) / 1000000 },
  65. {"sp", "set seekpoint (seconds)", OFFSET(seek_point_d), AV_OPT_TYPE_DOUBLE, {.dbl = 0}, 0, (INT64_MAX-1) / 1000000 },
  66. {"loop", "set loop count", OFFSET(loop_count), AV_OPT_TYPE_INT, {.dbl = 1}, 0, INT_MAX },
  67. {NULL},
  68. };
  69. static const char *movie_get_name(void *ctx)
  70. {
  71. return "movie";
  72. }
  73. static const AVClass movie_class = {
  74. "MovieContext",
  75. movie_get_name,
  76. movie_options
  77. };
  78. static av_cold int movie_common_init(AVFilterContext *ctx, const char *args, void *opaque,
  79. enum AVMediaType type)
  80. {
  81. MovieContext *movie = ctx->priv;
  82. AVInputFormat *iformat = NULL;
  83. AVCodec *codec;
  84. int64_t timestamp;
  85. int ret;
  86. movie->class = &movie_class;
  87. av_opt_set_defaults(movie);
  88. if (args)
  89. movie->file_name = av_get_token(&args, ":");
  90. if (!movie->file_name || !*movie->file_name) {
  91. av_log(ctx, AV_LOG_ERROR, "No filename provided!\n");
  92. return AVERROR(EINVAL);
  93. }
  94. if (*args++ == ':' && (ret = av_set_options_string(movie, args, "=", ":")) < 0) {
  95. av_log(ctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
  96. return ret;
  97. }
  98. movie->seek_point = movie->seek_point_d * 1000000 + 0.5;
  99. av_register_all();
  100. // Try to find the movie format (container)
  101. iformat = movie->format_name ? av_find_input_format(movie->format_name) : NULL;
  102. movie->format_ctx = NULL;
  103. if ((ret = avformat_open_input(&movie->format_ctx, movie->file_name, iformat, NULL)) < 0) {
  104. av_log(ctx, AV_LOG_ERROR,
  105. "Failed to avformat_open_input '%s'\n", movie->file_name);
  106. return ret;
  107. }
  108. if ((ret = avformat_find_stream_info(movie->format_ctx, NULL)) < 0)
  109. av_log(ctx, AV_LOG_WARNING, "Failed to find stream info\n");
  110. // if seeking requested, we execute it
  111. if (movie->seek_point > 0) {
  112. timestamp = movie->seek_point;
  113. // add the stream start time, should it exist
  114. if (movie->format_ctx->start_time != AV_NOPTS_VALUE) {
  115. if (timestamp > INT64_MAX - movie->format_ctx->start_time) {
  116. av_log(ctx, AV_LOG_ERROR,
  117. "%s: seek value overflow with start_time:%"PRId64" seek_point:%"PRId64"\n",
  118. movie->file_name, movie->format_ctx->start_time, movie->seek_point);
  119. return AVERROR(EINVAL);
  120. }
  121. timestamp += movie->format_ctx->start_time;
  122. }
  123. if ((ret = av_seek_frame(movie->format_ctx, -1, timestamp, AVSEEK_FLAG_BACKWARD)) < 0) {
  124. av_log(ctx, AV_LOG_ERROR, "%s: could not seek to position %"PRId64"\n",
  125. movie->file_name, timestamp);
  126. return ret;
  127. }
  128. }
  129. /* select the media stream */
  130. if ((ret = av_find_best_stream(movie->format_ctx, type,
  131. movie->stream_index, -1, NULL, 0)) < 0) {
  132. av_log(ctx, AV_LOG_ERROR, "No %s stream with index '%d' found\n",
  133. av_get_media_type_string(type), movie->stream_index);
  134. return ret;
  135. }
  136. movie->stream_index = ret;
  137. movie->codec_ctx = movie->format_ctx->streams[movie->stream_index]->codec;
  138. /*
  139. * So now we've got a pointer to the so-called codec context for our video
  140. * stream, but we still have to find the actual codec and open it.
  141. */
  142. codec = avcodec_find_decoder(movie->codec_ctx->codec_id);
  143. if (!codec) {
  144. av_log(ctx, AV_LOG_ERROR, "Failed to find any codec\n");
  145. return AVERROR(EINVAL);
  146. }
  147. if ((ret = avcodec_open2(movie->codec_ctx, codec, NULL)) < 0) {
  148. av_log(ctx, AV_LOG_ERROR, "Failed to open codec\n");
  149. return ret;
  150. }
  151. av_log(ctx, AV_LOG_INFO, "seek_point:%"PRIi64" format_name:%s file_name:%s stream_index:%d\n",
  152. movie->seek_point, movie->format_name, movie->file_name,
  153. movie->stream_index);
  154. if (!(movie->frame = avcodec_alloc_frame()) ) {
  155. av_log(ctx, AV_LOG_ERROR, "Failed to alloc frame\n");
  156. return AVERROR(ENOMEM);
  157. }
  158. return 0;
  159. }
  160. static av_cold void movie_common_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. avfilter_unref_buffer(movie->samplesref);
  172. }
  173. #if CONFIG_MOVIE_FILTER
  174. static av_cold int movie_init(AVFilterContext *ctx, const char *args, void *opaque)
  175. {
  176. MovieContext *movie = ctx->priv;
  177. int ret;
  178. if ((ret = movie_common_init(ctx, args, opaque, AVMEDIA_TYPE_VIDEO)) < 0)
  179. return ret;
  180. movie->w = movie->codec_ctx->width;
  181. movie->h = movie->codec_ctx->height;
  182. return 0;
  183. }
  184. static int movie_query_formats(AVFilterContext *ctx)
  185. {
  186. MovieContext *movie = ctx->priv;
  187. enum PixelFormat pix_fmts[] = { movie->codec_ctx->pix_fmt, PIX_FMT_NONE };
  188. avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(pix_fmts));
  189. return 0;
  190. }
  191. static int movie_config_output_props(AVFilterLink *outlink)
  192. {
  193. MovieContext *movie = outlink->src->priv;
  194. outlink->w = movie->w;
  195. outlink->h = movie->h;
  196. outlink->time_base = movie->format_ctx->streams[movie->stream_index]->time_base;
  197. return 0;
  198. }
  199. static int movie_get_frame(AVFilterLink *outlink)
  200. {
  201. MovieContext *movie = outlink->src->priv;
  202. AVPacket pkt;
  203. int ret, frame_decoded;
  204. AVStream *st = movie->format_ctx->streams[movie->stream_index];
  205. if (movie->is_done == 1)
  206. return 0;
  207. while (1) {
  208. ret = av_read_frame(movie->format_ctx, &pkt);
  209. if (ret == AVERROR_EOF) {
  210. int64_t timestamp;
  211. if (movie->loop_count != 1) {
  212. timestamp = movie->seek_point;
  213. if (movie->format_ctx->start_time != AV_NOPTS_VALUE)
  214. timestamp += movie->format_ctx->start_time;
  215. if (av_seek_frame(movie->format_ctx, -1, timestamp, AVSEEK_FLAG_BACKWARD) < 0) {
  216. movie->is_done = 1;
  217. break;
  218. } else if (movie->loop_count>1)
  219. movie->loop_count--;
  220. continue;
  221. } else {
  222. movie->is_done = 1;
  223. break;
  224. }
  225. } else if (ret < 0)
  226. break;
  227. // Is this a packet from the video stream?
  228. if (pkt.stream_index == movie->stream_index) {
  229. avcodec_decode_video2(movie->codec_ctx, movie->frame, &frame_decoded, &pkt);
  230. if (frame_decoded) {
  231. /* FIXME: avoid the memcpy */
  232. movie->picref = avfilter_get_video_buffer(outlink, AV_PERM_WRITE | AV_PERM_PRESERVE |
  233. AV_PERM_REUSE2, outlink->w, outlink->h);
  234. av_image_copy(movie->picref->data, movie->picref->linesize,
  235. (void*)movie->frame->data, movie->frame->linesize,
  236. movie->picref->format, outlink->w, outlink->h);
  237. avfilter_copy_frame_props(movie->picref, movie->frame);
  238. /* FIXME: use a PTS correction mechanism as that in
  239. * ffplay.c when some API will be available for that */
  240. /* use pkt_dts if pkt_pts is not available */
  241. movie->picref->pts = movie->frame->pkt_pts == AV_NOPTS_VALUE ?
  242. movie->frame->pkt_dts : movie->frame->pkt_pts;
  243. if (!movie->frame->sample_aspect_ratio.num)
  244. movie->picref->video->sample_aspect_ratio = st->sample_aspect_ratio;
  245. av_dlog(outlink->src,
  246. "movie_get_frame(): file:'%s' pts:%"PRId64" time:%lf pos:%"PRId64" aspect:%d/%d\n",
  247. movie->file_name, movie->picref->pts,
  248. (double)movie->picref->pts * av_q2d(st->time_base),
  249. movie->picref->pos,
  250. movie->picref->video->sample_aspect_ratio.num,
  251. movie->picref->video->sample_aspect_ratio.den);
  252. // We got it. Free the packet since we are returning
  253. av_free_packet(&pkt);
  254. return 0;
  255. }
  256. }
  257. // Free the packet that was allocated by av_read_frame
  258. av_free_packet(&pkt);
  259. }
  260. return ret;
  261. }
  262. static int movie_request_frame(AVFilterLink *outlink)
  263. {
  264. AVFilterBufferRef *outpicref;
  265. MovieContext *movie = outlink->src->priv;
  266. int ret;
  267. if (movie->is_done)
  268. return AVERROR_EOF;
  269. if ((ret = movie_get_frame(outlink)) < 0)
  270. return ret;
  271. outpicref = avfilter_ref_buffer(movie->picref, ~0);
  272. avfilter_start_frame(outlink, outpicref);
  273. avfilter_draw_slice(outlink, 0, outlink->h, 1);
  274. avfilter_end_frame(outlink);
  275. avfilter_unref_buffer(movie->picref);
  276. movie->picref = NULL;
  277. return 0;
  278. }
  279. AVFilter avfilter_vsrc_movie = {
  280. .name = "movie",
  281. .description = NULL_IF_CONFIG_SMALL("Read from a movie source."),
  282. .priv_size = sizeof(MovieContext),
  283. .init = movie_init,
  284. .uninit = movie_common_uninit,
  285. .query_formats = movie_query_formats,
  286. .inputs = (const AVFilterPad[]) {{ .name = NULL }},
  287. .outputs = (const AVFilterPad[]) {{ .name = "default",
  288. .type = AVMEDIA_TYPE_VIDEO,
  289. .request_frame = movie_request_frame,
  290. .config_props = movie_config_output_props, },
  291. { .name = NULL}},
  292. };
  293. #endif /* CONFIG_MOVIE_FILTER */
  294. #if CONFIG_AMOVIE_FILTER
  295. static av_cold int amovie_init(AVFilterContext *ctx, const char *args, void *opaque)
  296. {
  297. MovieContext *movie = ctx->priv;
  298. int ret;
  299. if ((ret = movie_common_init(ctx, args, opaque, AVMEDIA_TYPE_AUDIO)) < 0)
  300. return ret;
  301. movie->bps = av_get_bytes_per_sample(movie->codec_ctx->sample_fmt);
  302. return 0;
  303. }
  304. static int amovie_query_formats(AVFilterContext *ctx)
  305. {
  306. MovieContext *movie = ctx->priv;
  307. AVCodecContext *c = movie->codec_ctx;
  308. enum AVSampleFormat sample_fmts[] = { c->sample_fmt, -1 };
  309. int packing_fmts[] = { AVFILTER_PACKED, -1 };
  310. int64_t chlayouts[] = { c->channel_layout ? c->channel_layout :
  311. av_get_default_channel_layout(c->channels), -1 };
  312. avfilter_set_common_sample_formats (ctx, avfilter_make_format_list(sample_fmts));
  313. avfilter_set_common_packing_formats(ctx, avfilter_make_format_list(packing_fmts));
  314. avfilter_set_common_channel_layouts(ctx, avfilter_make_format64_list(chlayouts));
  315. return 0;
  316. }
  317. static int amovie_config_output_props(AVFilterLink *outlink)
  318. {
  319. MovieContext *movie = outlink->src->priv;
  320. AVCodecContext *c = movie->codec_ctx;
  321. outlink->sample_rate = c->sample_rate;
  322. outlink->time_base = movie->format_ctx->streams[movie->stream_index]->time_base;
  323. return 0;
  324. }
  325. static int amovie_get_samples(AVFilterLink *outlink)
  326. {
  327. MovieContext *movie = outlink->src->priv;
  328. AVPacket pkt;
  329. int ret, got_frame = 0;
  330. if (!movie->pkt.size && movie->is_done == 1)
  331. return AVERROR_EOF;
  332. /* check for another frame, in case the previous one was completely consumed */
  333. if (!movie->pkt.size) {
  334. while ((ret = av_read_frame(movie->format_ctx, &pkt)) >= 0) {
  335. // Is this a packet from the selected stream?
  336. if (pkt.stream_index != movie->stream_index) {
  337. av_free_packet(&pkt);
  338. continue;
  339. } else {
  340. movie->pkt0 = movie->pkt = pkt;
  341. break;
  342. }
  343. }
  344. if (ret == AVERROR_EOF) {
  345. movie->is_done = 1;
  346. return ret;
  347. }
  348. }
  349. /* decode and update the movie pkt */
  350. avcodec_get_frame_defaults(movie->frame);
  351. ret = avcodec_decode_audio4(movie->codec_ctx, movie->frame, &got_frame, &movie->pkt);
  352. if (ret < 0) {
  353. movie->pkt.size = 0;
  354. return ret;
  355. }
  356. movie->pkt.data += ret;
  357. movie->pkt.size -= ret;
  358. /* wrap the decoded data in a samplesref */
  359. if (got_frame) {
  360. int nb_samples = movie->frame->nb_samples;
  361. int data_size =
  362. av_samples_get_buffer_size(NULL, movie->codec_ctx->channels,
  363. nb_samples, movie->codec_ctx->sample_fmt, 1);
  364. if (data_size < 0)
  365. return data_size;
  366. movie->samplesref =
  367. avfilter_get_audio_buffer(outlink, AV_PERM_WRITE, nb_samples);
  368. memcpy(movie->samplesref->data[0], movie->frame->data[0], data_size);
  369. movie->samplesref->pts = movie->pkt.pts;
  370. movie->samplesref->pos = movie->pkt.pos;
  371. movie->samplesref->audio->sample_rate = movie->codec_ctx->sample_rate;
  372. }
  373. // We got it. Free the packet since we are returning
  374. if (movie->pkt.size <= 0)
  375. av_free_packet(&movie->pkt0);
  376. return 0;
  377. }
  378. static int amovie_request_frame(AVFilterLink *outlink)
  379. {
  380. MovieContext *movie = outlink->src->priv;
  381. int ret;
  382. if (movie->is_done)
  383. return AVERROR_EOF;
  384. do {
  385. if ((ret = amovie_get_samples(outlink)) < 0)
  386. return ret;
  387. } while (!movie->samplesref);
  388. avfilter_filter_samples(outlink, avfilter_ref_buffer(movie->samplesref, ~0));
  389. avfilter_unref_buffer(movie->samplesref);
  390. movie->samplesref = NULL;
  391. return 0;
  392. }
  393. AVFilter avfilter_asrc_amovie = {
  394. .name = "amovie",
  395. .description = NULL_IF_CONFIG_SMALL("Read audio from a movie source."),
  396. .priv_size = sizeof(MovieContext),
  397. .init = amovie_init,
  398. .uninit = movie_common_uninit,
  399. .query_formats = amovie_query_formats,
  400. .inputs = (const AVFilterPad[]) {{ .name = NULL }},
  401. .outputs = (const AVFilterPad[]) {{ .name = "default",
  402. .type = AVMEDIA_TYPE_AUDIO,
  403. .request_frame = amovie_request_frame,
  404. .config_props = amovie_config_output_props, },
  405. { .name = NULL}},
  406. };
  407. #endif /* CONFIG_AMOVIE_FILTER */