00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "libavutil/audioconvert.h"
00023 #include "libavutil/imgutils.h"
00024 #include "libavutil/samplefmt.h"
00025 #include "avfilter.h"
00026 #include "internal.h"
00027
00028 void ff_avfilter_default_free_buffer(AVFilterBuffer *ptr)
00029 {
00030 av_free(ptr->data[0]);
00031 av_free(ptr);
00032 }
00033
00034
00035
00036
00037 AVFilterBufferRef *avfilter_default_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
00038 {
00039 int linesize[4];
00040 uint8_t *data[4];
00041 int i;
00042 AVFilterBufferRef *picref = NULL;
00043 AVFilterPool *pool = link->pool;
00044
00045 if (pool) {
00046 for (i = 0; i < POOL_SIZE; i++) {
00047 picref = pool->pic[i];
00048 if (picref && picref->buf->format == link->format && picref->buf->w == w && picref->buf->h == h) {
00049 AVFilterBuffer *pic = picref->buf;
00050 pool->pic[i] = NULL;
00051 pool->count--;
00052 picref->video->w = w;
00053 picref->video->h = h;
00054 picref->perms = perms | AV_PERM_READ;
00055 picref->format = link->format;
00056 pic->refcount = 1;
00057 memcpy(picref->data, pic->data, sizeof(picref->data));
00058 memcpy(picref->linesize, pic->linesize, sizeof(picref->linesize));
00059 return picref;
00060 }
00061 }
00062 } else
00063 pool = link->pool = av_mallocz(sizeof(AVFilterPool));
00064
00065
00066 if ((i = av_image_alloc(data, linesize, w, h, link->format, 32)) < 0)
00067 return NULL;
00068
00069 picref = avfilter_get_video_buffer_ref_from_arrays(data, linesize,
00070 perms, w, h, link->format);
00071 if (!picref) {
00072 av_free(data[0]);
00073 return NULL;
00074 }
00075 memset(data[0], 128, i);
00076
00077 picref->buf->priv = pool;
00078 picref->buf->free = NULL;
00079
00080 return picref;
00081 }
00082
00083 AVFilterBufferRef *avfilter_default_get_audio_buffer(AVFilterLink *link, int perms,
00084 int nb_samples)
00085 {
00086 AVFilterBufferRef *samplesref = NULL;
00087 int linesize[8];
00088 uint8_t *data[8];
00089 int nb_channels = av_get_channel_layout_nb_channels(link->channel_layout);
00090
00091
00092 if (av_samples_alloc(data, linesize,
00093 nb_channels, nb_samples, link->format,
00094 16) < 0)
00095 return NULL;
00096
00097 samplesref =
00098 avfilter_get_audio_buffer_ref_from_arrays(data, linesize, perms,
00099 nb_samples, link->format,
00100 link->channel_layout, link->planar);
00101 if (!samplesref) {
00102 av_free(data[0]);
00103 return NULL;
00104 }
00105
00106 return samplesref;
00107 }
00108
00109 void avfilter_default_start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
00110 {
00111 AVFilterLink *outlink = NULL;
00112
00113 if (inlink->dst->output_count)
00114 outlink = inlink->dst->outputs[0];
00115
00116 if (outlink) {
00117 outlink->out_buf = avfilter_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
00118 avfilter_copy_buffer_ref_props(outlink->out_buf, picref);
00119 avfilter_start_frame(outlink, avfilter_ref_buffer(outlink->out_buf, ~0));
00120 }
00121 }
00122
00123 void avfilter_default_draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
00124 {
00125 AVFilterLink *outlink = NULL;
00126
00127 if (inlink->dst->output_count)
00128 outlink = inlink->dst->outputs[0];
00129
00130 if (outlink)
00131 avfilter_draw_slice(outlink, y, h, slice_dir);
00132 }
00133
00134 void avfilter_default_end_frame(AVFilterLink *inlink)
00135 {
00136 AVFilterLink *outlink = NULL;
00137
00138 if (inlink->dst->output_count)
00139 outlink = inlink->dst->outputs[0];
00140
00141 avfilter_unref_buffer(inlink->cur_buf);
00142 inlink->cur_buf = NULL;
00143
00144 if (outlink) {
00145 if (outlink->out_buf) {
00146 avfilter_unref_buffer(outlink->out_buf);
00147 outlink->out_buf = NULL;
00148 }
00149 avfilter_end_frame(outlink);
00150 }
00151 }
00152
00153
00154 void avfilter_default_filter_samples(AVFilterLink *inlink, AVFilterBufferRef *samplesref)
00155 {
00156 AVFilterLink *outlink = NULL;
00157
00158 if (inlink->dst->output_count)
00159 outlink = inlink->dst->outputs[0];
00160
00161 if (outlink) {
00162 outlink->out_buf = avfilter_default_get_audio_buffer(inlink, AV_PERM_WRITE,
00163 samplesref->audio->nb_samples);
00164 outlink->out_buf->pts = samplesref->pts;
00165 outlink->out_buf->audio->sample_rate = samplesref->audio->sample_rate;
00166 avfilter_filter_samples(outlink, avfilter_ref_buffer(outlink->out_buf, ~0));
00167 avfilter_unref_buffer(outlink->out_buf);
00168 outlink->out_buf = NULL;
00169 }
00170 avfilter_unref_buffer(samplesref);
00171 inlink->cur_buf = NULL;
00172 }
00173
00174 static void set_common_formats(AVFilterContext *ctx, AVFilterFormats *fmts,
00175 enum AVMediaType type, int offin, int offout)
00176 {
00177 int i;
00178 for (i = 0; i < ctx->input_count; i++)
00179 if (ctx->inputs[i] && ctx->inputs[i]->type == type)
00180 avfilter_formats_ref(fmts,
00181 (AVFilterFormats **)((uint8_t *)ctx->inputs[i]+offout));
00182
00183 for (i = 0; i < ctx->output_count; i++)
00184 if (ctx->outputs[i] && ctx->outputs[i]->type == type)
00185 avfilter_formats_ref(fmts,
00186 (AVFilterFormats **)((uint8_t *)ctx->outputs[i]+offin));
00187
00188 if (!fmts->refcount) {
00189 av_free(fmts->formats);
00190 av_free(fmts->refs);
00191 av_free(fmts);
00192 }
00193 }
00194
00195 void avfilter_set_common_pixel_formats(AVFilterContext *ctx, AVFilterFormats *formats)
00196 {
00197 set_common_formats(ctx, formats, AVMEDIA_TYPE_VIDEO,
00198 offsetof(AVFilterLink, in_formats),
00199 offsetof(AVFilterLink, out_formats));
00200 }
00201
00202 void avfilter_set_common_sample_formats(AVFilterContext *ctx, AVFilterFormats *formats)
00203 {
00204 set_common_formats(ctx, formats, AVMEDIA_TYPE_AUDIO,
00205 offsetof(AVFilterLink, in_formats),
00206 offsetof(AVFilterLink, out_formats));
00207 }
00208
00209 void avfilter_set_common_channel_layouts(AVFilterContext *ctx, AVFilterFormats *formats)
00210 {
00211 set_common_formats(ctx, formats, AVMEDIA_TYPE_AUDIO,
00212 offsetof(AVFilterLink, in_chlayouts),
00213 offsetof(AVFilterLink, out_chlayouts));
00214 }
00215
00216 void avfilter_set_common_packing_formats(AVFilterContext *ctx, AVFilterFormats *formats)
00217 {
00218 set_common_formats(ctx, formats, AVMEDIA_TYPE_AUDIO,
00219 offsetof(AVFilterLink, in_packing),
00220 offsetof(AVFilterLink, out_packing));
00221 }
00222
00223 int avfilter_default_query_formats(AVFilterContext *ctx)
00224 {
00225 avfilter_set_common_pixel_formats(ctx, avfilter_make_all_formats(AVMEDIA_TYPE_VIDEO));
00226 avfilter_set_common_sample_formats(ctx, avfilter_make_all_formats(AVMEDIA_TYPE_AUDIO));
00227 avfilter_set_common_channel_layouts(ctx, avfilter_make_all_channel_layouts());
00228 avfilter_set_common_packing_formats(ctx, avfilter_make_all_packing_formats());
00229
00230 return 0;
00231 }
00232
00233 void avfilter_null_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
00234 {
00235 avfilter_start_frame(link->dst->outputs[0], picref);
00236 }
00237
00238 void avfilter_null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
00239 {
00240 avfilter_draw_slice(link->dst->outputs[0], y, h, slice_dir);
00241 }
00242
00243 void avfilter_null_end_frame(AVFilterLink *link)
00244 {
00245 avfilter_end_frame(link->dst->outputs[0]);
00246 }
00247
00248 void avfilter_null_filter_samples(AVFilterLink *link, AVFilterBufferRef *samplesref)
00249 {
00250 avfilter_filter_samples(link->dst->outputs[0], samplesref);
00251 }
00252
00253 AVFilterBufferRef *avfilter_null_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
00254 {
00255 return avfilter_get_video_buffer(link->dst->outputs[0], perms, w, h);
00256 }
00257
00258 AVFilterBufferRef *avfilter_null_get_audio_buffer(AVFilterLink *link, int perms,
00259 int nb_samples)
00260 {
00261 return avfilter_get_audio_buffer(link->dst->outputs[0], perms, nb_samples);
00262 }
00263