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.

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