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