00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00026 #include "audio.h"
00027 #include "avfilter.h"
00028 #include "buffersrc.h"
00029 #include "formats.h"
00030 #include "internal.h"
00031 #include "vsrc_buffer.h"
00032 #include "avcodec.h"
00033
00034 #include "libavutil/audioconvert.h"
00035 #include "libavutil/fifo.h"
00036 #include "libavutil/imgutils.h"
00037 #include "libavutil/opt.h"
00038 #include "libavutil/samplefmt.h"
00039
00040 typedef struct {
00041 const AVClass *class;
00042 AVFifoBuffer *fifo;
00043 AVRational time_base;
00044 unsigned nb_failed_requests;
00045
00046
00047 int h, w;
00048 enum PixelFormat pix_fmt;
00049 AVRational pixel_aspect;
00050 char sws_param[256];
00051
00052
00053 int sample_rate;
00054 enum AVSampleFormat sample_fmt;
00055 char *sample_fmt_str;
00056 uint64_t channel_layout;
00057 char *channel_layout_str;
00058
00059 int eof;
00060 } BufferSourceContext;
00061
00062 #define CHECK_VIDEO_PARAM_CHANGE(s, c, width, height, format)\
00063 if (c->w != width || c->h != height || c->pix_fmt != format) {\
00064 av_log(s, AV_LOG_ERROR, "Changing frame properties on the fly is not supported.\n");\
00065 return AVERROR(EINVAL);\
00066 }
00067
00068 #define CHECK_AUDIO_PARAM_CHANGE(s, c, srate, ch_layout, format)\
00069 if (c->sample_fmt != format || c->sample_rate != srate ||\
00070 c->channel_layout != ch_layout) {\
00071 av_log(s, AV_LOG_ERROR, "Changing frame properties on the fly is not supported.\n");\
00072 return AVERROR(EINVAL);\
00073 }
00074
00075 static AVFilterBufferRef *copy_buffer_ref(AVFilterContext *ctx,
00076 AVFilterBufferRef *ref)
00077 {
00078 AVFilterLink *outlink = ctx->outputs[0];
00079 AVFilterBufferRef *buf;
00080 int channels;
00081
00082 switch (outlink->type) {
00083
00084 case AVMEDIA_TYPE_VIDEO:
00085 buf = avfilter_get_video_buffer(outlink, AV_PERM_WRITE,
00086 ref->video->w, ref->video->h);
00087 if(!buf)
00088 return NULL;
00089 av_image_copy(buf->data, buf->linesize,
00090 (void*)ref->data, ref->linesize,
00091 ref->format, ref->video->w, ref->video->h);
00092 break;
00093
00094 case AVMEDIA_TYPE_AUDIO:
00095 buf = ff_get_audio_buffer(outlink, AV_PERM_WRITE,
00096 ref->audio->nb_samples);
00097 if(!buf)
00098 return NULL;
00099 channels = av_get_channel_layout_nb_channels(ref->audio->channel_layout);
00100 av_samples_copy(buf->extended_data, ref->buf->extended_data,
00101 0, 0, ref->audio->nb_samples,
00102 channels,
00103 ref->format);
00104 break;
00105
00106 default:
00107 return NULL;
00108 }
00109 avfilter_copy_buffer_ref_props(buf, ref);
00110 return buf;
00111 }
00112
00113 #if FF_API_VSRC_BUFFER_ADD_FRAME
00114 static int av_vsrc_buffer_add_frame_alt(AVFilterContext *buffer_filter, AVFrame *frame,
00115 int64_t pts, AVRational pixel_aspect)
00116 {
00117 int64_t orig_pts = frame->pts;
00118 AVRational orig_sar = frame->sample_aspect_ratio;
00119 int ret;
00120
00121 frame->pts = pts;
00122 frame->sample_aspect_ratio = pixel_aspect;
00123 if ((ret = av_buffersrc_write_frame(buffer_filter, frame)) < 0)
00124 return ret;
00125 frame->pts = orig_pts;
00126 frame->sample_aspect_ratio = orig_sar;
00127
00128 return 0;
00129 }
00130 #endif
00131
00132 int av_buffersrc_add_frame(AVFilterContext *buffer_src,
00133 const AVFrame *frame, int flags)
00134 {
00135 AVFilterBufferRef *picref;
00136 int ret;
00137
00138 if (!frame)
00139 return av_buffersrc_add_ref(buffer_src, NULL, flags);
00140
00141 switch (buffer_src->outputs[0]->type) {
00142 case AVMEDIA_TYPE_VIDEO:
00143 picref = avfilter_get_video_buffer_ref_from_frame(frame, AV_PERM_WRITE);
00144 break;
00145 case AVMEDIA_TYPE_AUDIO:
00146 picref = avfilter_get_audio_buffer_ref_from_frame(frame, AV_PERM_WRITE);
00147 break;
00148 default:
00149 return AVERROR(ENOSYS);
00150 }
00151 if (!picref)
00152 return AVERROR(ENOMEM);
00153 ret = av_buffersrc_add_ref(buffer_src, picref, flags);
00154 picref->buf->data[0] = NULL;
00155 avfilter_unref_buffer(picref);
00156 return ret;
00157 }
00158
00159 int av_buffersrc_write_frame(AVFilterContext *buffer_filter, AVFrame *frame)
00160 {
00161 return av_buffersrc_add_frame(buffer_filter, frame, 0);
00162 }
00163
00164 int av_buffersrc_add_ref(AVFilterContext *s, AVFilterBufferRef *buf, int flags)
00165 {
00166 BufferSourceContext *c = s->priv;
00167 AVFilterBufferRef *to_free = NULL;
00168 int ret;
00169
00170 if (!buf) {
00171 c->eof = 1;
00172 return 0;
00173 } else if (c->eof)
00174 return AVERROR(EINVAL);
00175
00176 if (!av_fifo_space(c->fifo) &&
00177 (ret = av_fifo_realloc2(c->fifo, av_fifo_size(c->fifo) +
00178 sizeof(buf))) < 0)
00179 return ret;
00180
00181 if (!(flags & AV_BUFFERSRC_FLAG_NO_CHECK_FORMAT)) {
00182 switch (s->outputs[0]->type) {
00183 case AVMEDIA_TYPE_VIDEO:
00184 CHECK_VIDEO_PARAM_CHANGE(s, c, buf->video->w, buf->video->h, buf->format);
00185 break;
00186 case AVMEDIA_TYPE_AUDIO:
00187 CHECK_AUDIO_PARAM_CHANGE(s, c, buf->audio->sample_rate, buf->audio->channel_layout,
00188 buf->format);
00189 break;
00190 default:
00191 return AVERROR(EINVAL);
00192 }
00193 }
00194 if (!(flags & AV_BUFFERSRC_FLAG_NO_COPY))
00195 to_free = buf = copy_buffer_ref(s, buf);
00196 if(!buf)
00197 return -1;
00198
00199 if ((ret = av_fifo_generic_write(c->fifo, &buf, sizeof(buf), NULL)) < 0) {
00200 avfilter_unref_buffer(to_free);
00201 return ret;
00202 }
00203 c->nb_failed_requests = 0;
00204
00205 return 0;
00206 }
00207
00208 int av_buffersrc_buffer(AVFilterContext *s, AVFilterBufferRef *buf)
00209 {
00210 return av_buffersrc_add_ref(s, buf, AV_BUFFERSRC_FLAG_NO_COPY);
00211 }
00212
00213 unsigned av_buffersrc_get_nb_failed_requests(AVFilterContext *buffer_src)
00214 {
00215 return ((BufferSourceContext *)buffer_src->priv)->nb_failed_requests;
00216 }
00217
00218 static av_cold int init_video(AVFilterContext *ctx, const char *args, void *opaque)
00219 {
00220 BufferSourceContext *c = ctx->priv;
00221 char pix_fmt_str[128];
00222 int ret, n = 0;
00223 *c->sws_param = 0;
00224
00225 if (!args ||
00226 (n = sscanf(args, "%d:%d:%127[^:]:%d:%d:%d:%d:%255c", &c->w, &c->h, pix_fmt_str,
00227 &c->time_base.num, &c->time_base.den,
00228 &c->pixel_aspect.num, &c->pixel_aspect.den, c->sws_param)) < 7) {
00229 av_log(ctx, AV_LOG_ERROR, "Expected at least 7 arguments, but only %d found in '%s'\n", n, args);
00230 return AVERROR(EINVAL);
00231 }
00232
00233 if ((ret = ff_parse_pixel_format(&c->pix_fmt, pix_fmt_str, ctx)) < 0)
00234 return ret;
00235
00236 if (!(c->fifo = av_fifo_alloc(sizeof(AVFilterBufferRef*))))
00237 return AVERROR(ENOMEM);
00238
00239 av_log(ctx, AV_LOG_INFO, "w:%d h:%d pixfmt:%s tb:%d/%d sar:%d/%d sws_param:%s\n",
00240 c->w, c->h, av_pix_fmt_descriptors[c->pix_fmt].name,
00241 c->time_base.num, c->time_base.den,
00242 c->pixel_aspect.num, c->pixel_aspect.den, c->sws_param);
00243 return 0;
00244 }
00245
00246 #define OFFSET(x) offsetof(BufferSourceContext, x)
00247 #define A AV_OPT_FLAG_AUDIO_PARAM
00248 static const AVOption audio_options[] = {
00249 { "time_base", NULL, OFFSET(time_base), AV_OPT_TYPE_RATIONAL, { 0 }, 0, INT_MAX, A },
00250 { "sample_rate", NULL, OFFSET(sample_rate), AV_OPT_TYPE_INT, { 0 }, 0, INT_MAX, A },
00251 { "sample_fmt", NULL, OFFSET(sample_fmt_str), AV_OPT_TYPE_STRING, .flags = A },
00252 { "channel_layout", NULL, OFFSET(channel_layout_str), AV_OPT_TYPE_STRING, .flags = A },
00253 { NULL },
00254 };
00255
00256 static const AVClass abuffer_class = {
00257 .class_name = "abuffer source",
00258 .item_name = av_default_item_name,
00259 .option = audio_options,
00260 .version = LIBAVUTIL_VERSION_INT,
00261 };
00262
00263 static av_cold int init_audio(AVFilterContext *ctx, const char *args, void *opaque)
00264 {
00265 BufferSourceContext *s = ctx->priv;
00266 int ret = 0;
00267
00268 s->class = &abuffer_class;
00269 av_opt_set_defaults(s);
00270
00271 if ((ret = av_set_options_string(s, args, "=", ":")) < 0) {
00272 av_log(ctx, AV_LOG_ERROR, "Error parsing options string: %s.\n", args);
00273 goto fail;
00274 }
00275
00276 s->sample_fmt = av_get_sample_fmt(s->sample_fmt_str);
00277 if (s->sample_fmt == AV_SAMPLE_FMT_NONE) {
00278 av_log(ctx, AV_LOG_ERROR, "Invalid sample format %s.\n",
00279 s->sample_fmt_str);
00280 ret = AVERROR(EINVAL);
00281 goto fail;
00282 }
00283
00284 s->channel_layout = av_get_channel_layout(s->channel_layout_str);
00285 if (!s->channel_layout) {
00286 av_log(ctx, AV_LOG_ERROR, "Invalid channel layout %s.\n",
00287 s->channel_layout_str);
00288 ret = AVERROR(EINVAL);
00289 goto fail;
00290 }
00291
00292 if (!(s->fifo = av_fifo_alloc(sizeof(AVFilterBufferRef*)))) {
00293 ret = AVERROR(ENOMEM);
00294 goto fail;
00295 }
00296
00297 if (!s->time_base.num)
00298 s->time_base = (AVRational){1, s->sample_rate};
00299
00300 av_log(ctx, AV_LOG_VERBOSE, "tb:%d/%d samplefmt:%s samplerate: %d "
00301 "ch layout:%s\n", s->time_base.num, s->time_base.den, s->sample_fmt_str,
00302 s->sample_rate, s->channel_layout_str);
00303
00304 fail:
00305 av_opt_free(s);
00306 return ret;
00307 }
00308
00309 static av_cold void uninit(AVFilterContext *ctx)
00310 {
00311 BufferSourceContext *s = ctx->priv;
00312 while (s->fifo && av_fifo_size(s->fifo)) {
00313 AVFilterBufferRef *buf;
00314 av_fifo_generic_read(s->fifo, &buf, sizeof(buf), NULL);
00315 avfilter_unref_buffer(buf);
00316 }
00317 av_fifo_free(s->fifo);
00318 s->fifo = NULL;
00319 }
00320
00321 static int query_formats(AVFilterContext *ctx)
00322 {
00323 BufferSourceContext *c = ctx->priv;
00324 AVFilterChannelLayouts *channel_layouts = NULL;
00325 AVFilterFormats *formats = NULL;
00326 AVFilterFormats *samplerates = NULL;
00327
00328 switch (ctx->outputs[0]->type) {
00329 case AVMEDIA_TYPE_VIDEO:
00330 avfilter_add_format(&formats, c->pix_fmt);
00331 avfilter_set_common_formats(ctx, formats);
00332 break;
00333 case AVMEDIA_TYPE_AUDIO:
00334 avfilter_add_format(&formats, c->sample_fmt);
00335 avfilter_set_common_formats(ctx, formats);
00336
00337 avfilter_add_format(&samplerates, c->sample_rate);
00338 ff_set_common_samplerates(ctx, samplerates);
00339
00340 ff_add_channel_layout(&channel_layouts, c->channel_layout);
00341 ff_set_common_channel_layouts(ctx, channel_layouts);
00342 break;
00343 default:
00344 return AVERROR(EINVAL);
00345 }
00346
00347 return 0;
00348 }
00349
00350 static int config_props(AVFilterLink *link)
00351 {
00352 BufferSourceContext *c = link->src->priv;
00353
00354 switch (link->type) {
00355 case AVMEDIA_TYPE_VIDEO:
00356 link->w = c->w;
00357 link->h = c->h;
00358 link->sample_aspect_ratio = c->pixel_aspect;
00359 break;
00360 case AVMEDIA_TYPE_AUDIO:
00361 link->channel_layout = c->channel_layout;
00362 link->sample_rate = c->sample_rate;
00363 break;
00364 default:
00365 return AVERROR(EINVAL);
00366 }
00367
00368 link->time_base = c->time_base;
00369 return 0;
00370 }
00371
00372 static int request_frame(AVFilterLink *link)
00373 {
00374 BufferSourceContext *c = link->src->priv;
00375 AVFilterBufferRef *buf;
00376
00377 if (!av_fifo_size(c->fifo)) {
00378 if (c->eof)
00379 return AVERROR_EOF;
00380 c->nb_failed_requests++;
00381 return AVERROR(EAGAIN);
00382 }
00383 av_fifo_generic_read(c->fifo, &buf, sizeof(buf), NULL);
00384
00385 switch (link->type) {
00386 case AVMEDIA_TYPE_VIDEO:
00387 avfilter_start_frame(link, avfilter_ref_buffer(buf, ~0));
00388 avfilter_draw_slice(link, 0, link->h, 1);
00389 avfilter_end_frame(link);
00390 break;
00391 case AVMEDIA_TYPE_AUDIO:
00392 ff_filter_samples(link, avfilter_ref_buffer(buf, ~0));
00393 break;
00394 default:
00395 return AVERROR(EINVAL);
00396 }
00397
00398 avfilter_unref_buffer(buf);
00399
00400 return 0;
00401 }
00402
00403 static int poll_frame(AVFilterLink *link)
00404 {
00405 BufferSourceContext *c = link->src->priv;
00406 int size = av_fifo_size(c->fifo);
00407 if (!size && c->eof)
00408 return AVERROR_EOF;
00409 return size/sizeof(AVFilterBufferRef*);
00410 }
00411
00412 AVFilter avfilter_vsrc_buffer = {
00413 .name = "buffer",
00414 .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them accessible to the filterchain."),
00415 .priv_size = sizeof(BufferSourceContext),
00416 .query_formats = query_formats,
00417
00418 .init = init_video,
00419 .uninit = uninit,
00420
00421 .inputs = (AVFilterPad[]) {{ .name = NULL }},
00422 .outputs = (AVFilterPad[]) {{ .name = "default",
00423 .type = AVMEDIA_TYPE_VIDEO,
00424 .request_frame = request_frame,
00425 .poll_frame = poll_frame,
00426 .config_props = config_props, },
00427 { .name = NULL}},
00428 };
00429
00430 AVFilter avfilter_asrc_abuffer = {
00431 .name = "abuffer",
00432 .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them accessible to the filterchain."),
00433 .priv_size = sizeof(BufferSourceContext),
00434 .query_formats = query_formats,
00435
00436 .init = init_audio,
00437 .uninit = uninit,
00438
00439 .inputs = (AVFilterPad[]) {{ .name = NULL }},
00440 .outputs = (AVFilterPad[]) {{ .name = "default",
00441 .type = AVMEDIA_TYPE_AUDIO,
00442 .request_frame = request_frame,
00443 .poll_frame = poll_frame,
00444 .config_props = config_props, },
00445 { .name = NULL}},
00446 };