2011-01-29 10:22:41 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2010 Stefano Sabatini
|
|
|
|
* Copyright (c) 2008 Victor Paesa
|
|
|
|
*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* movie video source
|
|
|
|
*
|
|
|
|
* @todo use direct rendering (no allocation of a new frame)
|
|
|
|
* @todo support a PTS correction mechanism
|
|
|
|
* @todo support more than one output stream
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* #define DEBUG */
|
|
|
|
|
|
|
|
#include <float.h>
|
|
|
|
#include "libavutil/avstring.h"
|
|
|
|
#include "libavutil/opt.h"
|
2011-02-07 13:37:08 +00:00
|
|
|
#include "libavutil/imgutils.h"
|
2011-01-29 10:22:41 +00:00
|
|
|
#include "libavformat/avformat.h"
|
2012-05-12 15:58:14 +00:00
|
|
|
#include "audio.h"
|
2011-05-01 12:47:05 +00:00
|
|
|
#include "avcodec.h"
|
2011-01-29 10:22:41 +00:00
|
|
|
#include "avfilter.h"
|
2012-05-16 00:27:31 +00:00
|
|
|
#include "formats.h"
|
2012-06-12 18:12:42 +00:00
|
|
|
#include "internal.h"
|
2012-05-30 09:20:32 +00:00
|
|
|
#include "video.h"
|
2011-01-29 10:22:41 +00:00
|
|
|
|
|
|
|
typedef struct {
|
2011-08-18 10:36:39 +00:00
|
|
|
/* common A/V fields */
|
2011-01-29 10:22:41 +00:00
|
|
|
const AVClass *class;
|
|
|
|
int64_t seek_point; ///< seekpoint in microseconds
|
|
|
|
double seek_point_d;
|
|
|
|
char *format_name;
|
|
|
|
char *file_name;
|
|
|
|
int stream_index;
|
2012-03-15 11:45:26 +00:00
|
|
|
int loop_count;
|
2011-01-29 10:22:41 +00:00
|
|
|
|
|
|
|
AVFormatContext *format_ctx;
|
|
|
|
AVCodecContext *codec_ctx;
|
|
|
|
int is_done;
|
|
|
|
AVFrame *frame; ///< video frame to store the decoded images in
|
|
|
|
|
2011-08-18 10:36:39 +00:00
|
|
|
/* video-only fields */
|
2011-01-29 10:22:41 +00:00
|
|
|
int w, h;
|
|
|
|
AVFilterBufferRef *picref;
|
2011-08-18 12:26:15 +00:00
|
|
|
|
|
|
|
/* audio-only fields */
|
|
|
|
int bps; ///< bytes per sample
|
|
|
|
AVPacket pkt, pkt0;
|
|
|
|
AVFilterBufferRef *samplesref;
|
2011-01-29 10:22:41 +00:00
|
|
|
} MovieContext;
|
|
|
|
|
|
|
|
#define OFFSET(x) offsetof(MovieContext, x)
|
|
|
|
|
|
|
|
static const AVOption movie_options[]= {
|
2011-10-04 05:38:01 +00:00
|
|
|
{"format_name", "set format name", OFFSET(format_name), AV_OPT_TYPE_STRING, {.str = 0}, CHAR_MIN, CHAR_MAX },
|
|
|
|
{"f", "set format name", OFFSET(format_name), AV_OPT_TYPE_STRING, {.str = 0}, CHAR_MIN, CHAR_MAX },
|
|
|
|
{"stream_index", "set stream index", OFFSET(stream_index), AV_OPT_TYPE_INT, {.dbl = -1}, -1, INT_MAX },
|
|
|
|
{"si", "set stream index", OFFSET(stream_index), AV_OPT_TYPE_INT, {.dbl = -1}, -1, INT_MAX },
|
|
|
|
{"seek_point", "set seekpoint (seconds)", OFFSET(seek_point_d), AV_OPT_TYPE_DOUBLE, {.dbl = 0}, 0, (INT64_MAX-1) / 1000000 },
|
|
|
|
{"sp", "set seekpoint (seconds)", OFFSET(seek_point_d), AV_OPT_TYPE_DOUBLE, {.dbl = 0}, 0, (INT64_MAX-1) / 1000000 },
|
2012-03-15 11:45:26 +00:00
|
|
|
{"loop", "set loop count", OFFSET(loop_count), AV_OPT_TYPE_INT, {.dbl = 1}, 0, INT_MAX },
|
2011-01-29 10:22:41 +00:00
|
|
|
{NULL},
|
|
|
|
};
|
|
|
|
|
|
|
|
static const AVClass movie_class = {
|
2012-06-16 12:34:36 +00:00
|
|
|
.class_name = "movie",
|
|
|
|
.item_name = av_default_item_name,
|
|
|
|
.option = movie_options,
|
|
|
|
.version = LIBAVUTIL_VERSION_INT,
|
|
|
|
.category = AV_CLASS_CATEGORY_FILTER,
|
2011-01-29 10:22:41 +00:00
|
|
|
};
|
|
|
|
|
2011-08-18 10:36:39 +00:00
|
|
|
static av_cold int movie_common_init(AVFilterContext *ctx, const char *args, void *opaque,
|
|
|
|
enum AVMediaType type)
|
2011-01-29 10:22:41 +00:00
|
|
|
{
|
|
|
|
MovieContext *movie = ctx->priv;
|
|
|
|
AVInputFormat *iformat = NULL;
|
|
|
|
AVCodec *codec;
|
|
|
|
int64_t timestamp;
|
2011-08-18 10:36:39 +00:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
movie->class = &movie_class;
|
2011-09-07 20:32:59 +00:00
|
|
|
av_opt_set_defaults(movie);
|
2011-08-18 10:36:39 +00:00
|
|
|
|
|
|
|
if (args)
|
|
|
|
movie->file_name = av_get_token(&args, ":");
|
|
|
|
if (!movie->file_name || !*movie->file_name) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "No filename provided!\n");
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*args++ == ':' && (ret = av_set_options_string(movie, args, "=", ":")) < 0) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
movie->seek_point = movie->seek_point_d * 1000000 + 0.5;
|
2011-01-29 10:22:41 +00:00
|
|
|
|
|
|
|
av_register_all();
|
|
|
|
|
|
|
|
// Try to find the movie format (container)
|
|
|
|
iformat = movie->format_name ? av_find_input_format(movie->format_name) : NULL;
|
|
|
|
|
|
|
|
movie->format_ctx = NULL;
|
2011-06-04 15:36:30 +00:00
|
|
|
if ((ret = avformat_open_input(&movie->format_ctx, movie->file_name, iformat, NULL)) < 0) {
|
2011-01-29 10:22:41 +00:00
|
|
|
av_log(ctx, AV_LOG_ERROR,
|
2011-06-04 15:36:30 +00:00
|
|
|
"Failed to avformat_open_input '%s'\n", movie->file_name);
|
2011-01-29 10:22:41 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2011-05-22 17:24:59 +00:00
|
|
|
if ((ret = avformat_find_stream_info(movie->format_ctx, NULL)) < 0)
|
2011-01-29 10:22:41 +00:00
|
|
|
av_log(ctx, AV_LOG_WARNING, "Failed to find stream info\n");
|
|
|
|
|
|
|
|
// if seeking requested, we execute it
|
|
|
|
if (movie->seek_point > 0) {
|
|
|
|
timestamp = movie->seek_point;
|
|
|
|
// add the stream start time, should it exist
|
|
|
|
if (movie->format_ctx->start_time != AV_NOPTS_VALUE) {
|
|
|
|
if (timestamp > INT64_MAX - movie->format_ctx->start_time) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR,
|
|
|
|
"%s: seek value overflow with start_time:%"PRId64" seek_point:%"PRId64"\n",
|
|
|
|
movie->file_name, movie->format_ctx->start_time, movie->seek_point);
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
timestamp += movie->format_ctx->start_time;
|
|
|
|
}
|
|
|
|
if ((ret = av_seek_frame(movie->format_ctx, -1, timestamp, AVSEEK_FLAG_BACKWARD)) < 0) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "%s: could not seek to position %"PRId64"\n",
|
|
|
|
movie->file_name, timestamp);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-18 10:36:39 +00:00
|
|
|
/* select the media stream */
|
|
|
|
if ((ret = av_find_best_stream(movie->format_ctx, type,
|
2011-01-29 10:22:41 +00:00
|
|
|
movie->stream_index, -1, NULL, 0)) < 0) {
|
2011-08-18 10:36:39 +00:00
|
|
|
av_log(ctx, AV_LOG_ERROR, "No %s stream with index '%d' found\n",
|
|
|
|
av_get_media_type_string(type), movie->stream_index);
|
2011-01-29 10:22:41 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
movie->stream_index = ret;
|
|
|
|
movie->codec_ctx = movie->format_ctx->streams[movie->stream_index]->codec;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* So now we've got a pointer to the so-called codec context for our video
|
|
|
|
* stream, but we still have to find the actual codec and open it.
|
|
|
|
*/
|
|
|
|
codec = avcodec_find_decoder(movie->codec_ctx->codec_id);
|
|
|
|
if (!codec) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Failed to find any codec\n");
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
|
2011-05-22 12:10:49 +00:00
|
|
|
if ((ret = avcodec_open2(movie->codec_ctx, codec, NULL)) < 0) {
|
2011-01-29 10:22:41 +00:00
|
|
|
av_log(ctx, AV_LOG_ERROR, "Failed to open codec\n");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-05-31 19:23:45 +00:00
|
|
|
av_log(ctx, AV_LOG_INFO, "seek_point:%"PRIi64" format_name:%s file_name:%s stream_index:%d\n",
|
2011-01-29 10:22:41 +00:00
|
|
|
movie->seek_point, movie->format_name, movie->file_name,
|
|
|
|
movie->stream_index);
|
|
|
|
|
2011-12-13 14:36:34 +00:00
|
|
|
if (!(movie->frame = avcodec_alloc_frame()) ) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Failed to alloc frame\n");
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
|
|
|
|
2011-01-29 10:22:41 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-08-18 10:36:39 +00:00
|
|
|
static av_cold void movie_common_uninit(AVFilterContext *ctx)
|
2011-01-29 10:22:41 +00:00
|
|
|
{
|
|
|
|
MovieContext *movie = ctx->priv;
|
|
|
|
|
|
|
|
av_free(movie->file_name);
|
|
|
|
av_free(movie->format_name);
|
|
|
|
if (movie->codec_ctx)
|
|
|
|
avcodec_close(movie->codec_ctx);
|
|
|
|
if (movie->format_ctx)
|
2011-12-11 09:38:28 +00:00
|
|
|
avformat_close_input(&movie->format_ctx);
|
2011-08-18 10:36:39 +00:00
|
|
|
|
2011-01-29 10:22:41 +00:00
|
|
|
avfilter_unref_buffer(movie->picref);
|
|
|
|
av_freep(&movie->frame);
|
2011-08-18 12:26:15 +00:00
|
|
|
|
|
|
|
avfilter_unref_buffer(movie->samplesref);
|
2011-01-29 10:22:41 +00:00
|
|
|
}
|
|
|
|
|
2011-08-18 12:26:15 +00:00
|
|
|
#if CONFIG_MOVIE_FILTER
|
|
|
|
|
2011-08-18 10:36:39 +00:00
|
|
|
static av_cold int movie_init(AVFilterContext *ctx, const char *args, void *opaque)
|
|
|
|
{
|
|
|
|
MovieContext *movie = ctx->priv;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if ((ret = movie_common_init(ctx, args, opaque, AVMEDIA_TYPE_VIDEO)) < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
movie->w = movie->codec_ctx->width;
|
|
|
|
movie->h = movie->codec_ctx->height;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-08-18 10:53:18 +00:00
|
|
|
static int movie_query_formats(AVFilterContext *ctx)
|
2011-01-29 10:22:41 +00:00
|
|
|
{
|
|
|
|
MovieContext *movie = ctx->priv;
|
|
|
|
enum PixelFormat pix_fmts[] = { movie->codec_ctx->pix_fmt, PIX_FMT_NONE };
|
|
|
|
|
2012-05-30 08:12:55 +00:00
|
|
|
ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
|
2011-01-29 10:22:41 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-08-18 10:53:18 +00:00
|
|
|
static int movie_config_output_props(AVFilterLink *outlink)
|
2011-01-29 10:22:41 +00:00
|
|
|
{
|
|
|
|
MovieContext *movie = outlink->src->priv;
|
|
|
|
|
|
|
|
outlink->w = movie->w;
|
|
|
|
outlink->h = movie->h;
|
|
|
|
outlink->time_base = movie->format_ctx->streams[movie->stream_index]->time_base;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int movie_get_frame(AVFilterLink *outlink)
|
|
|
|
{
|
|
|
|
MovieContext *movie = outlink->src->priv;
|
|
|
|
AVPacket pkt;
|
|
|
|
int ret, frame_decoded;
|
|
|
|
AVStream *st = movie->format_ctx->streams[movie->stream_index];
|
|
|
|
|
|
|
|
if (movie->is_done == 1)
|
|
|
|
return 0;
|
|
|
|
|
2012-03-15 11:45:26 +00:00
|
|
|
while (1) {
|
|
|
|
ret = av_read_frame(movie->format_ctx, &pkt);
|
|
|
|
if (ret == AVERROR_EOF) {
|
|
|
|
int64_t timestamp;
|
|
|
|
if (movie->loop_count != 1) {
|
|
|
|
timestamp = movie->seek_point;
|
|
|
|
if (movie->format_ctx->start_time != AV_NOPTS_VALUE)
|
|
|
|
timestamp += movie->format_ctx->start_time;
|
|
|
|
if (av_seek_frame(movie->format_ctx, -1, timestamp, AVSEEK_FLAG_BACKWARD) < 0) {
|
|
|
|
movie->is_done = 1;
|
|
|
|
break;
|
|
|
|
} else if (movie->loop_count>1)
|
|
|
|
movie->loop_count--;
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
movie->is_done = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else if (ret < 0)
|
|
|
|
break;
|
|
|
|
|
2011-01-29 10:22:41 +00:00
|
|
|
// Is this a packet from the video stream?
|
|
|
|
if (pkt.stream_index == movie->stream_index) {
|
|
|
|
avcodec_decode_video2(movie->codec_ctx, movie->frame, &frame_decoded, &pkt);
|
|
|
|
|
|
|
|
if (frame_decoded) {
|
|
|
|
/* FIXME: avoid the memcpy */
|
2012-05-30 08:31:48 +00:00
|
|
|
movie->picref = ff_get_video_buffer(outlink, AV_PERM_WRITE | AV_PERM_PRESERVE |
|
|
|
|
AV_PERM_REUSE2, outlink->w, outlink->h);
|
2011-01-29 10:22:41 +00:00
|
|
|
av_image_copy(movie->picref->data, movie->picref->linesize,
|
2011-10-18 22:03:56 +00:00
|
|
|
(void*)movie->frame->data, movie->frame->linesize,
|
2011-01-29 10:22:41 +00:00
|
|
|
movie->picref->format, outlink->w, outlink->h);
|
2011-05-01 12:47:05 +00:00
|
|
|
avfilter_copy_frame_props(movie->picref, movie->frame);
|
2011-01-29 10:22:41 +00:00
|
|
|
|
|
|
|
/* FIXME: use a PTS correction mechanism as that in
|
|
|
|
* ffplay.c when some API will be available for that */
|
|
|
|
/* use pkt_dts if pkt_pts is not available */
|
|
|
|
movie->picref->pts = movie->frame->pkt_pts == AV_NOPTS_VALUE ?
|
|
|
|
movie->frame->pkt_dts : movie->frame->pkt_pts;
|
2011-01-29 10:22:41 +00:00
|
|
|
|
2011-04-29 11:04:47 +00:00
|
|
|
if (!movie->frame->sample_aspect_ratio.num)
|
2011-04-29 11:12:39 +00:00
|
|
|
movie->picref->video->sample_aspect_ratio = st->sample_aspect_ratio;
|
2011-01-29 10:22:41 +00:00
|
|
|
av_dlog(outlink->src,
|
|
|
|
"movie_get_frame(): file:'%s' pts:%"PRId64" time:%lf pos:%"PRId64" aspect:%d/%d\n",
|
|
|
|
movie->file_name, movie->picref->pts,
|
|
|
|
(double)movie->picref->pts * av_q2d(st->time_base),
|
|
|
|
movie->picref->pos,
|
2011-04-29 11:12:39 +00:00
|
|
|
movie->picref->video->sample_aspect_ratio.num,
|
|
|
|
movie->picref->video->sample_aspect_ratio.den);
|
2011-01-29 10:22:41 +00:00
|
|
|
// We got it. Free the packet since we are returning
|
|
|
|
av_free_packet(&pkt);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Free the packet that was allocated by av_read_frame
|
|
|
|
av_free_packet(&pkt);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-08-18 10:53:18 +00:00
|
|
|
static int movie_request_frame(AVFilterLink *outlink)
|
2011-01-29 10:22:41 +00:00
|
|
|
{
|
|
|
|
AVFilterBufferRef *outpicref;
|
|
|
|
MovieContext *movie = outlink->src->priv;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (movie->is_done)
|
|
|
|
return AVERROR_EOF;
|
|
|
|
if ((ret = movie_get_frame(outlink)) < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
outpicref = avfilter_ref_buffer(movie->picref, ~0);
|
2012-05-30 09:20:32 +00:00
|
|
|
ff_start_frame(outlink, outpicref);
|
|
|
|
ff_draw_slice(outlink, 0, outlink->h, 1);
|
|
|
|
ff_end_frame(outlink);
|
2011-04-18 17:21:25 +00:00
|
|
|
avfilter_unref_buffer(movie->picref);
|
|
|
|
movie->picref = NULL;
|
2011-01-29 10:22:41 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
AVFilter avfilter_vsrc_movie = {
|
|
|
|
.name = "movie",
|
|
|
|
.description = NULL_IF_CONFIG_SMALL("Read from a movie source."),
|
|
|
|
.priv_size = sizeof(MovieContext),
|
2011-08-18 10:36:39 +00:00
|
|
|
.init = movie_init,
|
|
|
|
.uninit = movie_common_uninit,
|
2011-08-18 10:53:18 +00:00
|
|
|
.query_formats = movie_query_formats,
|
2011-01-29 10:22:41 +00:00
|
|
|
|
2011-11-05 13:39:24 +00:00
|
|
|
.inputs = (const AVFilterPad[]) {{ .name = NULL }},
|
|
|
|
.outputs = (const AVFilterPad[]) {{ .name = "default",
|
2011-01-29 10:22:41 +00:00
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
2011-08-18 10:53:18 +00:00
|
|
|
.request_frame = movie_request_frame,
|
|
|
|
.config_props = movie_config_output_props, },
|
2011-01-29 10:22:41 +00:00
|
|
|
{ .name = NULL}},
|
|
|
|
};
|
2011-08-18 12:26:15 +00:00
|
|
|
|
|
|
|
#endif /* CONFIG_MOVIE_FILTER */
|
|
|
|
|
|
|
|
#if CONFIG_AMOVIE_FILTER
|
|
|
|
|
|
|
|
static av_cold int amovie_init(AVFilterContext *ctx, const char *args, void *opaque)
|
|
|
|
{
|
|
|
|
MovieContext *movie = ctx->priv;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if ((ret = movie_common_init(ctx, args, opaque, AVMEDIA_TYPE_AUDIO)) < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
movie->bps = av_get_bytes_per_sample(movie->codec_ctx->sample_fmt);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int amovie_query_formats(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
MovieContext *movie = ctx->priv;
|
|
|
|
AVCodecContext *c = movie->codec_ctx;
|
|
|
|
|
|
|
|
enum AVSampleFormat sample_fmts[] = { c->sample_fmt, -1 };
|
2012-05-16 00:27:31 +00:00
|
|
|
int sample_rates[] = { c->sample_rate, -1 };
|
2011-11-05 20:21:47 +00:00
|
|
|
int64_t chlayouts[] = { c->channel_layout ? c->channel_layout :
|
|
|
|
av_get_default_channel_layout(c->channels), -1 };
|
2011-08-18 12:26:15 +00:00
|
|
|
|
2012-06-16 09:47:46 +00:00
|
|
|
ff_set_common_formats (ctx, ff_make_format_list(sample_fmts));
|
|
|
|
ff_set_common_samplerates (ctx, ff_make_format_list(sample_rates));
|
2012-05-16 00:27:31 +00:00
|
|
|
ff_set_common_channel_layouts(ctx, avfilter_make_format64_list(chlayouts));
|
2011-08-18 12:26:15 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int amovie_config_output_props(AVFilterLink *outlink)
|
|
|
|
{
|
|
|
|
MovieContext *movie = outlink->src->priv;
|
|
|
|
AVCodecContext *c = movie->codec_ctx;
|
|
|
|
|
|
|
|
outlink->sample_rate = c->sample_rate;
|
|
|
|
outlink->time_base = movie->format_ctx->streams[movie->stream_index]->time_base;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int amovie_get_samples(AVFilterLink *outlink)
|
|
|
|
{
|
|
|
|
MovieContext *movie = outlink->src->priv;
|
|
|
|
AVPacket pkt;
|
2011-12-13 14:36:34 +00:00
|
|
|
int ret, got_frame = 0;
|
2011-08-18 12:26:15 +00:00
|
|
|
|
|
|
|
if (!movie->pkt.size && movie->is_done == 1)
|
|
|
|
return AVERROR_EOF;
|
|
|
|
|
|
|
|
/* check for another frame, in case the previous one was completely consumed */
|
|
|
|
if (!movie->pkt.size) {
|
|
|
|
while ((ret = av_read_frame(movie->format_ctx, &pkt)) >= 0) {
|
|
|
|
// Is this a packet from the selected stream?
|
|
|
|
if (pkt.stream_index != movie->stream_index) {
|
|
|
|
av_free_packet(&pkt);
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
movie->pkt0 = movie->pkt = pkt;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret == AVERROR_EOF) {
|
|
|
|
movie->is_done = 1;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* decode and update the movie pkt */
|
2011-12-13 14:36:34 +00:00
|
|
|
avcodec_get_frame_defaults(movie->frame);
|
|
|
|
ret = avcodec_decode_audio4(movie->codec_ctx, movie->frame, &got_frame, &movie->pkt);
|
2011-12-13 14:54:59 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
movie->pkt.size = 0;
|
2011-08-18 12:26:15 +00:00
|
|
|
return ret;
|
2011-12-13 14:54:59 +00:00
|
|
|
}
|
2011-08-18 12:26:15 +00:00
|
|
|
movie->pkt.data += ret;
|
|
|
|
movie->pkt.size -= ret;
|
|
|
|
|
|
|
|
/* wrap the decoded data in a samplesref */
|
2011-12-13 14:36:34 +00:00
|
|
|
if (got_frame) {
|
|
|
|
int nb_samples = movie->frame->nb_samples;
|
|
|
|
int data_size =
|
|
|
|
av_samples_get_buffer_size(NULL, movie->codec_ctx->channels,
|
|
|
|
nb_samples, movie->codec_ctx->sample_fmt, 1);
|
|
|
|
if (data_size < 0)
|
|
|
|
return data_size;
|
2011-08-18 12:26:15 +00:00
|
|
|
movie->samplesref =
|
2012-05-10 20:41:29 +00:00
|
|
|
ff_get_audio_buffer(outlink, AV_PERM_WRITE, nb_samples);
|
2011-12-13 14:36:34 +00:00
|
|
|
memcpy(movie->samplesref->data[0], movie->frame->data[0], data_size);
|
2011-08-18 12:26:15 +00:00
|
|
|
movie->samplesref->pts = movie->pkt.pts;
|
|
|
|
movie->samplesref->pos = movie->pkt.pos;
|
|
|
|
movie->samplesref->audio->sample_rate = movie->codec_ctx->sample_rate;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We got it. Free the packet since we are returning
|
|
|
|
if (movie->pkt.size <= 0)
|
|
|
|
av_free_packet(&movie->pkt0);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int amovie_request_frame(AVFilterLink *outlink)
|
|
|
|
{
|
|
|
|
MovieContext *movie = outlink->src->priv;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (movie->is_done)
|
|
|
|
return AVERROR_EOF;
|
2011-11-13 22:51:06 +00:00
|
|
|
do {
|
|
|
|
if ((ret = amovie_get_samples(outlink)) < 0)
|
|
|
|
return ret;
|
|
|
|
} while (!movie->samplesref);
|
2011-08-18 12:26:15 +00:00
|
|
|
|
2012-05-10 20:41:29 +00:00
|
|
|
ff_filter_samples(outlink, avfilter_ref_buffer(movie->samplesref, ~0));
|
2011-08-18 12:26:15 +00:00
|
|
|
avfilter_unref_buffer(movie->samplesref);
|
|
|
|
movie->samplesref = NULL;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
AVFilter avfilter_asrc_amovie = {
|
|
|
|
.name = "amovie",
|
|
|
|
.description = NULL_IF_CONFIG_SMALL("Read audio from a movie source."),
|
|
|
|
.priv_size = sizeof(MovieContext),
|
|
|
|
.init = amovie_init,
|
|
|
|
.uninit = movie_common_uninit,
|
|
|
|
.query_formats = amovie_query_formats,
|
|
|
|
|
2011-11-05 13:39:24 +00:00
|
|
|
.inputs = (const AVFilterPad[]) {{ .name = NULL }},
|
|
|
|
.outputs = (const AVFilterPad[]) {{ .name = "default",
|
2011-08-18 12:26:15 +00:00
|
|
|
.type = AVMEDIA_TYPE_AUDIO,
|
|
|
|
.request_frame = amovie_request_frame,
|
|
|
|
.config_props = amovie_config_output_props, },
|
|
|
|
{ .name = NULL}},
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* CONFIG_AMOVIE_FILTER */
|