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.

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