00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00031
00032
00033 #include <float.h>
00034 #include "libavutil/avstring.h"
00035 #include "libavutil/opt.h"
00036 #include "libavutil/imgutils.h"
00037 #include "libavformat/avformat.h"
00038 #include "audio.h"
00039 #include "avcodec.h"
00040 #include "avfilter.h"
00041 #include "formats.h"
00042
00043 typedef struct {
00044
00045 const AVClass *class;
00046 int64_t seek_point;
00047 double seek_point_d;
00048 char *format_name;
00049 char *file_name;
00050 int stream_index;
00051 int loop_count;
00052
00053 AVFormatContext *format_ctx;
00054 AVCodecContext *codec_ctx;
00055 int is_done;
00056 AVFrame *frame;
00057
00058
00059 int w, h;
00060 AVFilterBufferRef *picref;
00061
00062
00063 int bps;
00064 AVPacket pkt, pkt0;
00065 AVFilterBufferRef *samplesref;
00066 } MovieContext;
00067
00068 #define OFFSET(x) offsetof(MovieContext, x)
00069
00070 static const AVOption movie_options[]= {
00071 {"format_name", "set format name", OFFSET(format_name), AV_OPT_TYPE_STRING, {.str = 0}, CHAR_MIN, CHAR_MAX },
00072 {"f", "set format name", OFFSET(format_name), AV_OPT_TYPE_STRING, {.str = 0}, CHAR_MIN, CHAR_MAX },
00073 {"stream_index", "set stream index", OFFSET(stream_index), AV_OPT_TYPE_INT, {.dbl = -1}, -1, INT_MAX },
00074 {"si", "set stream index", OFFSET(stream_index), AV_OPT_TYPE_INT, {.dbl = -1}, -1, INT_MAX },
00075 {"seek_point", "set seekpoint (seconds)", OFFSET(seek_point_d), AV_OPT_TYPE_DOUBLE, {.dbl = 0}, 0, (INT64_MAX-1) / 1000000 },
00076 {"sp", "set seekpoint (seconds)", OFFSET(seek_point_d), AV_OPT_TYPE_DOUBLE, {.dbl = 0}, 0, (INT64_MAX-1) / 1000000 },
00077 {"loop", "set loop count", OFFSET(loop_count), AV_OPT_TYPE_INT, {.dbl = 1}, 0, INT_MAX },
00078 {NULL},
00079 };
00080
00081 static const char *movie_get_name(void *ctx)
00082 {
00083 return "movie";
00084 }
00085
00086 static const AVClass movie_class = {
00087 "MovieContext",
00088 movie_get_name,
00089 movie_options
00090 };
00091
00092 static av_cold int movie_common_init(AVFilterContext *ctx, const char *args, void *opaque,
00093 enum AVMediaType type)
00094 {
00095 MovieContext *movie = ctx->priv;
00096 AVInputFormat *iformat = NULL;
00097 AVCodec *codec;
00098 int64_t timestamp;
00099 int ret;
00100
00101 movie->class = &movie_class;
00102 av_opt_set_defaults(movie);
00103
00104 if (args)
00105 movie->file_name = av_get_token(&args, ":");
00106 if (!movie->file_name || !*movie->file_name) {
00107 av_log(ctx, AV_LOG_ERROR, "No filename provided!\n");
00108 return AVERROR(EINVAL);
00109 }
00110
00111 if (*args++ == ':' && (ret = av_set_options_string(movie, args, "=", ":")) < 0) {
00112 av_log(ctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
00113 return ret;
00114 }
00115
00116 movie->seek_point = movie->seek_point_d * 1000000 + 0.5;
00117
00118 av_register_all();
00119
00120
00121 iformat = movie->format_name ? av_find_input_format(movie->format_name) : NULL;
00122
00123 movie->format_ctx = NULL;
00124 if ((ret = avformat_open_input(&movie->format_ctx, movie->file_name, iformat, NULL)) < 0) {
00125 av_log(ctx, AV_LOG_ERROR,
00126 "Failed to avformat_open_input '%s'\n", movie->file_name);
00127 return ret;
00128 }
00129 if ((ret = avformat_find_stream_info(movie->format_ctx, NULL)) < 0)
00130 av_log(ctx, AV_LOG_WARNING, "Failed to find stream info\n");
00131
00132
00133 if (movie->seek_point > 0) {
00134 timestamp = movie->seek_point;
00135
00136 if (movie->format_ctx->start_time != AV_NOPTS_VALUE) {
00137 if (timestamp > INT64_MAX - movie->format_ctx->start_time) {
00138 av_log(ctx, AV_LOG_ERROR,
00139 "%s: seek value overflow with start_time:%"PRId64" seek_point:%"PRId64"\n",
00140 movie->file_name, movie->format_ctx->start_time, movie->seek_point);
00141 return AVERROR(EINVAL);
00142 }
00143 timestamp += movie->format_ctx->start_time;
00144 }
00145 if ((ret = av_seek_frame(movie->format_ctx, -1, timestamp, AVSEEK_FLAG_BACKWARD)) < 0) {
00146 av_log(ctx, AV_LOG_ERROR, "%s: could not seek to position %"PRId64"\n",
00147 movie->file_name, timestamp);
00148 return ret;
00149 }
00150 }
00151
00152
00153 if ((ret = av_find_best_stream(movie->format_ctx, type,
00154 movie->stream_index, -1, NULL, 0)) < 0) {
00155 av_log(ctx, AV_LOG_ERROR, "No %s stream with index '%d' found\n",
00156 av_get_media_type_string(type), movie->stream_index);
00157 return ret;
00158 }
00159 movie->stream_index = ret;
00160 movie->codec_ctx = movie->format_ctx->streams[movie->stream_index]->codec;
00161
00162
00163
00164
00165
00166 codec = avcodec_find_decoder(movie->codec_ctx->codec_id);
00167 if (!codec) {
00168 av_log(ctx, AV_LOG_ERROR, "Failed to find any codec\n");
00169 return AVERROR(EINVAL);
00170 }
00171
00172 if ((ret = avcodec_open2(movie->codec_ctx, codec, NULL)) < 0) {
00173 av_log(ctx, AV_LOG_ERROR, "Failed to open codec\n");
00174 return ret;
00175 }
00176
00177 av_log(ctx, AV_LOG_INFO, "seek_point:%"PRIi64" format_name:%s file_name:%s stream_index:%d\n",
00178 movie->seek_point, movie->format_name, movie->file_name,
00179 movie->stream_index);
00180
00181 if (!(movie->frame = avcodec_alloc_frame()) ) {
00182 av_log(ctx, AV_LOG_ERROR, "Failed to alloc frame\n");
00183 return AVERROR(ENOMEM);
00184 }
00185
00186 return 0;
00187 }
00188
00189 static av_cold void movie_common_uninit(AVFilterContext *ctx)
00190 {
00191 MovieContext *movie = ctx->priv;
00192
00193 av_free(movie->file_name);
00194 av_free(movie->format_name);
00195 if (movie->codec_ctx)
00196 avcodec_close(movie->codec_ctx);
00197 if (movie->format_ctx)
00198 avformat_close_input(&movie->format_ctx);
00199
00200 avfilter_unref_buffer(movie->picref);
00201 av_freep(&movie->frame);
00202
00203 avfilter_unref_buffer(movie->samplesref);
00204 }
00205
00206 #if CONFIG_MOVIE_FILTER
00207
00208 static av_cold int movie_init(AVFilterContext *ctx, const char *args, void *opaque)
00209 {
00210 MovieContext *movie = ctx->priv;
00211 int ret;
00212
00213 if ((ret = movie_common_init(ctx, args, opaque, AVMEDIA_TYPE_VIDEO)) < 0)
00214 return ret;
00215
00216 movie->w = movie->codec_ctx->width;
00217 movie->h = movie->codec_ctx->height;
00218
00219 return 0;
00220 }
00221
00222 static int movie_query_formats(AVFilterContext *ctx)
00223 {
00224 MovieContext *movie = ctx->priv;
00225 enum PixelFormat pix_fmts[] = { movie->codec_ctx->pix_fmt, PIX_FMT_NONE };
00226
00227 avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(pix_fmts));
00228 return 0;
00229 }
00230
00231 static int movie_config_output_props(AVFilterLink *outlink)
00232 {
00233 MovieContext *movie = outlink->src->priv;
00234
00235 outlink->w = movie->w;
00236 outlink->h = movie->h;
00237 outlink->time_base = movie->format_ctx->streams[movie->stream_index]->time_base;
00238
00239 return 0;
00240 }
00241
00242 static int movie_get_frame(AVFilterLink *outlink)
00243 {
00244 MovieContext *movie = outlink->src->priv;
00245 AVPacket pkt;
00246 int ret, frame_decoded;
00247 AVStream *st = movie->format_ctx->streams[movie->stream_index];
00248
00249 if (movie->is_done == 1)
00250 return 0;
00251
00252 while (1) {
00253 ret = av_read_frame(movie->format_ctx, &pkt);
00254 if (ret == AVERROR_EOF) {
00255 int64_t timestamp;
00256 if (movie->loop_count != 1) {
00257 timestamp = movie->seek_point;
00258 if (movie->format_ctx->start_time != AV_NOPTS_VALUE)
00259 timestamp += movie->format_ctx->start_time;
00260 if (av_seek_frame(movie->format_ctx, -1, timestamp, AVSEEK_FLAG_BACKWARD) < 0) {
00261 movie->is_done = 1;
00262 break;
00263 } else if (movie->loop_count>1)
00264 movie->loop_count--;
00265 continue;
00266 } else {
00267 movie->is_done = 1;
00268 break;
00269 }
00270 } else if (ret < 0)
00271 break;
00272
00273
00274 if (pkt.stream_index == movie->stream_index) {
00275 avcodec_decode_video2(movie->codec_ctx, movie->frame, &frame_decoded, &pkt);
00276
00277 if (frame_decoded) {
00278
00279 movie->picref = avfilter_get_video_buffer(outlink, AV_PERM_WRITE | AV_PERM_PRESERVE |
00280 AV_PERM_REUSE2, outlink->w, outlink->h);
00281 av_image_copy(movie->picref->data, movie->picref->linesize,
00282 (void*)movie->frame->data, movie->frame->linesize,
00283 movie->picref->format, outlink->w, outlink->h);
00284 avfilter_copy_frame_props(movie->picref, movie->frame);
00285
00286
00287
00288
00289 movie->picref->pts = movie->frame->pkt_pts == AV_NOPTS_VALUE ?
00290 movie->frame->pkt_dts : movie->frame->pkt_pts;
00291
00292 if (!movie->frame->sample_aspect_ratio.num)
00293 movie->picref->video->sample_aspect_ratio = st->sample_aspect_ratio;
00294 av_dlog(outlink->src,
00295 "movie_get_frame(): file:'%s' pts:%"PRId64" time:%lf pos:%"PRId64" aspect:%d/%d\n",
00296 movie->file_name, movie->picref->pts,
00297 (double)movie->picref->pts * av_q2d(st->time_base),
00298 movie->picref->pos,
00299 movie->picref->video->sample_aspect_ratio.num,
00300 movie->picref->video->sample_aspect_ratio.den);
00301
00302 av_free_packet(&pkt);
00303
00304 return 0;
00305 }
00306 }
00307
00308 av_free_packet(&pkt);
00309 }
00310
00311 return ret;
00312 }
00313
00314 static int movie_request_frame(AVFilterLink *outlink)
00315 {
00316 AVFilterBufferRef *outpicref;
00317 MovieContext *movie = outlink->src->priv;
00318 int ret;
00319
00320 if (movie->is_done)
00321 return AVERROR_EOF;
00322 if ((ret = movie_get_frame(outlink)) < 0)
00323 return ret;
00324
00325 outpicref = avfilter_ref_buffer(movie->picref, ~0);
00326 avfilter_start_frame(outlink, outpicref);
00327 avfilter_draw_slice(outlink, 0, outlink->h, 1);
00328 avfilter_end_frame(outlink);
00329 avfilter_unref_buffer(movie->picref);
00330 movie->picref = NULL;
00331
00332 return 0;
00333 }
00334
00335 AVFilter avfilter_vsrc_movie = {
00336 .name = "movie",
00337 .description = NULL_IF_CONFIG_SMALL("Read from a movie source."),
00338 .priv_size = sizeof(MovieContext),
00339 .init = movie_init,
00340 .uninit = movie_common_uninit,
00341 .query_formats = movie_query_formats,
00342
00343 .inputs = (const AVFilterPad[]) {{ .name = NULL }},
00344 .outputs = (const AVFilterPad[]) {{ .name = "default",
00345 .type = AVMEDIA_TYPE_VIDEO,
00346 .request_frame = movie_request_frame,
00347 .config_props = movie_config_output_props, },
00348 { .name = NULL}},
00349 };
00350
00351 #endif
00352
00353 #if CONFIG_AMOVIE_FILTER
00354
00355 static av_cold int amovie_init(AVFilterContext *ctx, const char *args, void *opaque)
00356 {
00357 MovieContext *movie = ctx->priv;
00358 int ret;
00359
00360 if ((ret = movie_common_init(ctx, args, opaque, AVMEDIA_TYPE_AUDIO)) < 0)
00361 return ret;
00362
00363 movie->bps = av_get_bytes_per_sample(movie->codec_ctx->sample_fmt);
00364 return 0;
00365 }
00366
00367 static int amovie_query_formats(AVFilterContext *ctx)
00368 {
00369 MovieContext *movie = ctx->priv;
00370 AVCodecContext *c = movie->codec_ctx;
00371
00372 enum AVSampleFormat sample_fmts[] = { c->sample_fmt, -1 };
00373 int sample_rates[] = { c->sample_rate, -1 };
00374 int64_t chlayouts[] = { c->channel_layout ? c->channel_layout :
00375 av_get_default_channel_layout(c->channels), -1 };
00376
00377 avfilter_set_common_sample_formats (ctx, avfilter_make_format_list(sample_fmts));
00378 ff_set_common_samplerates (ctx, avfilter_make_format_list(sample_rates));
00379 ff_set_common_channel_layouts(ctx, avfilter_make_format64_list(chlayouts));
00380
00381 return 0;
00382 }
00383
00384 static int amovie_config_output_props(AVFilterLink *outlink)
00385 {
00386 MovieContext *movie = outlink->src->priv;
00387 AVCodecContext *c = movie->codec_ctx;
00388
00389 outlink->sample_rate = c->sample_rate;
00390 outlink->time_base = movie->format_ctx->streams[movie->stream_index]->time_base;
00391
00392 return 0;
00393 }
00394
00395 static int amovie_get_samples(AVFilterLink *outlink)
00396 {
00397 MovieContext *movie = outlink->src->priv;
00398 AVPacket pkt;
00399 int ret, got_frame = 0;
00400
00401 if (!movie->pkt.size && movie->is_done == 1)
00402 return AVERROR_EOF;
00403
00404
00405 if (!movie->pkt.size) {
00406 while ((ret = av_read_frame(movie->format_ctx, &pkt)) >= 0) {
00407
00408 if (pkt.stream_index != movie->stream_index) {
00409 av_free_packet(&pkt);
00410 continue;
00411 } else {
00412 movie->pkt0 = movie->pkt = pkt;
00413 break;
00414 }
00415 }
00416
00417 if (ret == AVERROR_EOF) {
00418 movie->is_done = 1;
00419 return ret;
00420 }
00421 }
00422
00423
00424 avcodec_get_frame_defaults(movie->frame);
00425 ret = avcodec_decode_audio4(movie->codec_ctx, movie->frame, &got_frame, &movie->pkt);
00426 if (ret < 0) {
00427 movie->pkt.size = 0;
00428 return ret;
00429 }
00430 movie->pkt.data += ret;
00431 movie->pkt.size -= ret;
00432
00433
00434 if (got_frame) {
00435 int nb_samples = movie->frame->nb_samples;
00436 int data_size =
00437 av_samples_get_buffer_size(NULL, movie->codec_ctx->channels,
00438 nb_samples, movie->codec_ctx->sample_fmt, 1);
00439 if (data_size < 0)
00440 return data_size;
00441 movie->samplesref =
00442 ff_get_audio_buffer(outlink, AV_PERM_WRITE, nb_samples);
00443 memcpy(movie->samplesref->data[0], movie->frame->data[0], data_size);
00444 movie->samplesref->pts = movie->pkt.pts;
00445 movie->samplesref->pos = movie->pkt.pos;
00446 movie->samplesref->audio->sample_rate = movie->codec_ctx->sample_rate;
00447 }
00448
00449
00450 if (movie->pkt.size <= 0)
00451 av_free_packet(&movie->pkt0);
00452
00453 return 0;
00454 }
00455
00456 static int amovie_request_frame(AVFilterLink *outlink)
00457 {
00458 MovieContext *movie = outlink->src->priv;
00459 int ret;
00460
00461 if (movie->is_done)
00462 return AVERROR_EOF;
00463 do {
00464 if ((ret = amovie_get_samples(outlink)) < 0)
00465 return ret;
00466 } while (!movie->samplesref);
00467
00468 ff_filter_samples(outlink, avfilter_ref_buffer(movie->samplesref, ~0));
00469 avfilter_unref_buffer(movie->samplesref);
00470 movie->samplesref = NULL;
00471
00472 return 0;
00473 }
00474
00475 AVFilter avfilter_asrc_amovie = {
00476 .name = "amovie",
00477 .description = NULL_IF_CONFIG_SMALL("Read audio from a movie source."),
00478 .priv_size = sizeof(MovieContext),
00479 .init = amovie_init,
00480 .uninit = movie_common_uninit,
00481 .query_formats = amovie_query_formats,
00482
00483 .inputs = (const AVFilterPad[]) {{ .name = NULL }},
00484 .outputs = (const AVFilterPad[]) {{ .name = "default",
00485 .type = AVMEDIA_TYPE_AUDIO,
00486 .request_frame = amovie_request_frame,
00487 .config_props = amovie_config_output_props, },
00488 { .name = NULL}},
00489 };
00490
00491 #endif