src_movie.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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. /* audio-only fields */
  53. void *samples_buf;
  54. int samples_buf_size;
  55. int bps; ///< bytes per sample
  56. AVPacket pkt, pkt0;
  57. AVFilterBufferRef *samplesref;
  58. } MovieContext;
  59. #define OFFSET(x) offsetof(MovieContext, x)
  60. static const AVOption movie_options[]= {
  61. {"format_name", "set format name", OFFSET(format_name), AV_OPT_TYPE_STRING, {.str = 0}, CHAR_MIN, CHAR_MAX },
  62. {"f", "set format name", OFFSET(format_name), AV_OPT_TYPE_STRING, {.str = 0}, CHAR_MIN, CHAR_MAX },
  63. {"stream_index", "set stream index", OFFSET(stream_index), AV_OPT_TYPE_INT, {.dbl = -1}, -1, INT_MAX },
  64. {"si", "set stream index", OFFSET(stream_index), AV_OPT_TYPE_INT, {.dbl = -1}, -1, INT_MAX },
  65. {"seek_point", "set seekpoint (seconds)", OFFSET(seek_point_d), AV_OPT_TYPE_DOUBLE, {.dbl = 0}, 0, (INT64_MAX-1) / 1000000 },
  66. {"sp", "set seekpoint (seconds)", OFFSET(seek_point_d), AV_OPT_TYPE_DOUBLE, {.dbl = 0}, 0, (INT64_MAX-1) / 1000000 },
  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. return 0;
  155. }
  156. static av_cold void movie_common_uninit(AVFilterContext *ctx)
  157. {
  158. MovieContext *movie = ctx->priv;
  159. av_free(movie->file_name);
  160. av_free(movie->format_name);
  161. if (movie->codec_ctx)
  162. avcodec_close(movie->codec_ctx);
  163. if (movie->format_ctx)
  164. av_close_input_file(movie->format_ctx);
  165. avfilter_unref_buffer(movie->picref);
  166. av_freep(&movie->frame);
  167. avfilter_unref_buffer(movie->samplesref);
  168. av_freep(&movie->samples_buf);
  169. }
  170. #if CONFIG_MOVIE_FILTER
  171. static av_cold int movie_init(AVFilterContext *ctx, const char *args, void *opaque)
  172. {
  173. MovieContext *movie = ctx->priv;
  174. int ret;
  175. if ((ret = movie_common_init(ctx, args, opaque, AVMEDIA_TYPE_VIDEO)) < 0)
  176. return ret;
  177. if (!(movie->frame = avcodec_alloc_frame()) ) {
  178. av_log(ctx, AV_LOG_ERROR, "Failed to alloc frame\n");
  179. return AVERROR(ENOMEM);
  180. }
  181. movie->w = movie->codec_ctx->width;
  182. movie->h = movie->codec_ctx->height;
  183. return 0;
  184. }
  185. static int movie_query_formats(AVFilterContext *ctx)
  186. {
  187. MovieContext *movie = ctx->priv;
  188. enum PixelFormat pix_fmts[] = { movie->codec_ctx->pix_fmt, PIX_FMT_NONE };
  189. avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(pix_fmts));
  190. return 0;
  191. }
  192. static int movie_config_output_props(AVFilterLink *outlink)
  193. {
  194. MovieContext *movie = outlink->src->priv;
  195. outlink->w = movie->w;
  196. outlink->h = movie->h;
  197. outlink->time_base = movie->format_ctx->streams[movie->stream_index]->time_base;
  198. return 0;
  199. }
  200. static int movie_get_frame(AVFilterLink *outlink)
  201. {
  202. MovieContext *movie = outlink->src->priv;
  203. AVPacket pkt;
  204. int ret, frame_decoded;
  205. AVStream *st = movie->format_ctx->streams[movie->stream_index];
  206. if (movie->is_done == 1)
  207. return 0;
  208. while ((ret = av_read_frame(movie->format_ctx, &pkt)) >= 0) {
  209. // Is this a packet from the video stream?
  210. if (pkt.stream_index == movie->stream_index) {
  211. avcodec_decode_video2(movie->codec_ctx, movie->frame, &frame_decoded, &pkt);
  212. if (frame_decoded) {
  213. /* FIXME: avoid the memcpy */
  214. movie->picref = avfilter_get_video_buffer(outlink, AV_PERM_WRITE | AV_PERM_PRESERVE |
  215. AV_PERM_REUSE2, outlink->w, outlink->h);
  216. av_image_copy(movie->picref->data, movie->picref->linesize,
  217. (void*)movie->frame->data, movie->frame->linesize,
  218. movie->picref->format, outlink->w, outlink->h);
  219. avfilter_copy_frame_props(movie->picref, movie->frame);
  220. /* FIXME: use a PTS correction mechanism as that in
  221. * ffplay.c when some API will be available for that */
  222. /* use pkt_dts if pkt_pts is not available */
  223. movie->picref->pts = movie->frame->pkt_pts == AV_NOPTS_VALUE ?
  224. movie->frame->pkt_dts : movie->frame->pkt_pts;
  225. if (!movie->frame->sample_aspect_ratio.num)
  226. movie->picref->video->sample_aspect_ratio = st->sample_aspect_ratio;
  227. av_dlog(outlink->src,
  228. "movie_get_frame(): file:'%s' pts:%"PRId64" time:%lf pos:%"PRId64" aspect:%d/%d\n",
  229. movie->file_name, movie->picref->pts,
  230. (double)movie->picref->pts * av_q2d(st->time_base),
  231. movie->picref->pos,
  232. movie->picref->video->sample_aspect_ratio.num,
  233. movie->picref->video->sample_aspect_ratio.den);
  234. // We got it. Free the packet since we are returning
  235. av_free_packet(&pkt);
  236. return 0;
  237. }
  238. }
  239. // Free the packet that was allocated by av_read_frame
  240. av_free_packet(&pkt);
  241. }
  242. // On multi-frame source we should stop the mixing process when
  243. // the movie source does not have more frames
  244. if (ret == AVERROR_EOF)
  245. movie->is_done = 1;
  246. return ret;
  247. }
  248. static int movie_request_frame(AVFilterLink *outlink)
  249. {
  250. AVFilterBufferRef *outpicref;
  251. MovieContext *movie = outlink->src->priv;
  252. int ret;
  253. if (movie->is_done)
  254. return AVERROR_EOF;
  255. if ((ret = movie_get_frame(outlink)) < 0)
  256. return ret;
  257. outpicref = avfilter_ref_buffer(movie->picref, ~0);
  258. avfilter_start_frame(outlink, outpicref);
  259. avfilter_draw_slice(outlink, 0, outlink->h, 1);
  260. avfilter_end_frame(outlink);
  261. avfilter_unref_buffer(movie->picref);
  262. movie->picref = NULL;
  263. return 0;
  264. }
  265. AVFilter avfilter_vsrc_movie = {
  266. .name = "movie",
  267. .description = NULL_IF_CONFIG_SMALL("Read from a movie source."),
  268. .priv_size = sizeof(MovieContext),
  269. .init = movie_init,
  270. .uninit = movie_common_uninit,
  271. .query_formats = movie_query_formats,
  272. .inputs = (const AVFilterPad[]) {{ .name = NULL }},
  273. .outputs = (const AVFilterPad[]) {{ .name = "default",
  274. .type = AVMEDIA_TYPE_VIDEO,
  275. .request_frame = movie_request_frame,
  276. .config_props = movie_config_output_props, },
  277. { .name = NULL}},
  278. };
  279. #endif /* CONFIG_MOVIE_FILTER */
  280. #if CONFIG_AMOVIE_FILTER
  281. static av_cold int amovie_init(AVFilterContext *ctx, const char *args, void *opaque)
  282. {
  283. MovieContext *movie = ctx->priv;
  284. int ret;
  285. if ((ret = movie_common_init(ctx, args, opaque, AVMEDIA_TYPE_AUDIO)) < 0)
  286. return ret;
  287. movie->bps = av_get_bytes_per_sample(movie->codec_ctx->sample_fmt);
  288. return 0;
  289. }
  290. static int amovie_query_formats(AVFilterContext *ctx)
  291. {
  292. MovieContext *movie = ctx->priv;
  293. AVCodecContext *c = movie->codec_ctx;
  294. enum AVSampleFormat sample_fmts[] = { c->sample_fmt, -1 };
  295. int packing_fmts[] = { AVFILTER_PACKED, -1 };
  296. int64_t chlayouts[] = { c->channel_layout ? c->channel_layout :
  297. av_get_default_channel_layout(c->channels), -1 };
  298. avfilter_set_common_sample_formats (ctx, avfilter_make_format_list(sample_fmts));
  299. avfilter_set_common_packing_formats(ctx, avfilter_make_format_list(packing_fmts));
  300. avfilter_set_common_channel_layouts(ctx, avfilter_make_format64_list(chlayouts));
  301. return 0;
  302. }
  303. static int amovie_config_output_props(AVFilterLink *outlink)
  304. {
  305. MovieContext *movie = outlink->src->priv;
  306. AVCodecContext *c = movie->codec_ctx;
  307. outlink->sample_rate = c->sample_rate;
  308. outlink->time_base = movie->format_ctx->streams[movie->stream_index]->time_base;
  309. return 0;
  310. }
  311. static int amovie_get_samples(AVFilterLink *outlink)
  312. {
  313. MovieContext *movie = outlink->src->priv;
  314. AVPacket pkt;
  315. int ret, samples_size, decoded_data_size;
  316. if (!movie->pkt.size && movie->is_done == 1)
  317. return AVERROR_EOF;
  318. /* check for another frame, in case the previous one was completely consumed */
  319. if (!movie->pkt.size) {
  320. while ((ret = av_read_frame(movie->format_ctx, &pkt)) >= 0) {
  321. // Is this a packet from the selected stream?
  322. if (pkt.stream_index != movie->stream_index) {
  323. av_free_packet(&pkt);
  324. continue;
  325. } else {
  326. movie->pkt0 = movie->pkt = pkt;
  327. break;
  328. }
  329. }
  330. if (ret == AVERROR_EOF) {
  331. movie->is_done = 1;
  332. return ret;
  333. }
  334. }
  335. /* reallocate the buffer for the decoded samples, if necessary */
  336. samples_size =
  337. FFMAX(movie->pkt.size*sizeof(movie->bps), AVCODEC_MAX_AUDIO_FRAME_SIZE);
  338. if (samples_size > movie->samples_buf_size) {
  339. movie->samples_buf = av_fast_realloc(movie->samples_buf,
  340. &movie->samples_buf_size, samples_size);
  341. if (!movie->samples_buf)
  342. return AVERROR(ENOMEM);
  343. }
  344. decoded_data_size = movie->samples_buf_size;
  345. /* decode and update the movie pkt */
  346. ret = avcodec_decode_audio3(movie->codec_ctx, movie->samples_buf,
  347. &decoded_data_size, &movie->pkt);
  348. if (ret < 0) {
  349. movie->pkt.size = 0;
  350. return ret;
  351. }
  352. movie->pkt.data += ret;
  353. movie->pkt.size -= ret;
  354. /* wrap the decoded data in a samplesref */
  355. if (decoded_data_size > 0) {
  356. int nb_samples = decoded_data_size / movie->bps / movie->codec_ctx->channels;
  357. movie->samplesref =
  358. avfilter_get_audio_buffer(outlink, AV_PERM_WRITE, nb_samples);
  359. memcpy(movie->samplesref->data[0], movie->samples_buf, decoded_data_size);
  360. movie->samplesref->pts = movie->pkt.pts;
  361. movie->samplesref->pos = movie->pkt.pos;
  362. movie->samplesref->audio->sample_rate = movie->codec_ctx->sample_rate;
  363. }
  364. // We got it. Free the packet since we are returning
  365. if (movie->pkt.size <= 0)
  366. av_free_packet(&movie->pkt0);
  367. return 0;
  368. }
  369. static int amovie_request_frame(AVFilterLink *outlink)
  370. {
  371. MovieContext *movie = outlink->src->priv;
  372. int ret;
  373. if (movie->is_done)
  374. return AVERROR_EOF;
  375. do {
  376. if ((ret = amovie_get_samples(outlink)) < 0)
  377. return ret;
  378. } while (!movie->samplesref);
  379. avfilter_filter_samples(outlink, avfilter_ref_buffer(movie->samplesref, ~0));
  380. avfilter_unref_buffer(movie->samplesref);
  381. movie->samplesref = NULL;
  382. return 0;
  383. }
  384. AVFilter avfilter_asrc_amovie = {
  385. .name = "amovie",
  386. .description = NULL_IF_CONFIG_SMALL("Read audio from a movie source."),
  387. .priv_size = sizeof(MovieContext),
  388. .init = amovie_init,
  389. .uninit = movie_common_uninit,
  390. .query_formats = amovie_query_formats,
  391. .inputs = (const AVFilterPad[]) {{ .name = NULL }},
  392. .outputs = (const AVFilterPad[]) {{ .name = "default",
  393. .type = AVMEDIA_TYPE_AUDIO,
  394. .request_frame = amovie_request_frame,
  395. .config_props = amovie_config_output_props, },
  396. { .name = NULL}},
  397. };
  398. #endif /* CONFIG_AMOVIE_FILTER */