00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00026 #include "libavutil/avassert.h"
00027 #include "libavutil/audioconvert.h"
00028 #include "libavutil/common.h"
00029 #include "libavutil/mathematics.h"
00030 #include "libavutil/samplefmt.h"
00031
00032 #include "audio.h"
00033 #include "avfilter.h"
00034 #include "internal.h"
00035 #include "video.h"
00036
00037 typedef struct Buf {
00038 AVFilterBufferRef *buf;
00039 struct Buf *next;
00040 } Buf;
00041
00042 typedef struct {
00043 Buf root;
00044 Buf *last;
00045
00050 AVFilterBufferRef *buf_out;
00051 int allocated_samples;
00052 } FifoContext;
00053
00054 static av_cold int init(AVFilterContext *ctx, const char *args)
00055 {
00056 FifoContext *fifo = ctx->priv;
00057 fifo->last = &fifo->root;
00058
00059 return 0;
00060 }
00061
00062 static av_cold void uninit(AVFilterContext *ctx)
00063 {
00064 FifoContext *fifo = ctx->priv;
00065 Buf *buf, *tmp;
00066
00067 for (buf = fifo->root.next; buf; buf = tmp) {
00068 tmp = buf->next;
00069 avfilter_unref_bufferp(&buf->buf);
00070 av_free(buf);
00071 }
00072
00073 avfilter_unref_bufferp(&fifo->buf_out);
00074 }
00075
00076 static int add_to_queue(AVFilterLink *inlink, AVFilterBufferRef *buf)
00077 {
00078 FifoContext *fifo = inlink->dst->priv;
00079
00080 inlink->cur_buf = NULL;
00081 fifo->last->next = av_mallocz(sizeof(Buf));
00082 if (!fifo->last->next) {
00083 avfilter_unref_buffer(buf);
00084 return AVERROR(ENOMEM);
00085 }
00086
00087 fifo->last = fifo->last->next;
00088 fifo->last->buf = buf;
00089
00090 return 0;
00091 }
00092
00093 static void queue_pop(FifoContext *s)
00094 {
00095 Buf *tmp = s->root.next->next;
00096 if (s->last == s->root.next)
00097 s->last = &s->root;
00098 av_freep(&s->root.next);
00099 s->root.next = tmp;
00100 }
00101
00102 static int end_frame(AVFilterLink *inlink)
00103 {
00104 return 0;
00105 }
00106
00107 static int draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
00108 {
00109 return 0;
00110 }
00111
00115 static void buffer_offset(AVFilterLink *link, AVFilterBufferRef *buf,
00116 int offset)
00117 {
00118 int nb_channels = av_get_channel_layout_nb_channels(link->channel_layout);
00119 int planar = av_sample_fmt_is_planar(link->format);
00120 int planes = planar ? nb_channels : 1;
00121 int block_align = av_get_bytes_per_sample(link->format) * (planar ? 1 : nb_channels);
00122 int i;
00123
00124 av_assert0(buf->audio->nb_samples > offset);
00125
00126 for (i = 0; i < planes; i++)
00127 buf->extended_data[i] += block_align*offset;
00128 if (buf->data != buf->extended_data)
00129 memcpy(buf->data, buf->extended_data,
00130 FFMIN(planes, FF_ARRAY_ELEMS(buf->data)) * sizeof(*buf->data));
00131 buf->linesize[0] -= block_align*offset;
00132 buf->audio->nb_samples -= offset;
00133
00134 if (buf->pts != AV_NOPTS_VALUE) {
00135 buf->pts += av_rescale_q(offset, (AVRational){1, link->sample_rate},
00136 link->time_base);
00137 }
00138 }
00139
00140 static int calc_ptr_alignment(AVFilterBufferRef *buf)
00141 {
00142 int planes = av_sample_fmt_is_planar(buf->format) ?
00143 av_get_channel_layout_nb_channels(buf->audio->channel_layout) : 1;
00144 int min_align = 128;
00145 int p;
00146
00147 for (p = 0; p < planes; p++) {
00148 int cur_align = 128;
00149 while ((intptr_t)buf->extended_data[p] % cur_align)
00150 cur_align >>= 1;
00151 if (cur_align < min_align)
00152 min_align = cur_align;
00153 }
00154 return min_align;
00155 }
00156
00157 static int return_audio_frame(AVFilterContext *ctx)
00158 {
00159 AVFilterLink *link = ctx->outputs[0];
00160 FifoContext *s = ctx->priv;
00161 AVFilterBufferRef *head = s->root.next->buf;
00162 AVFilterBufferRef *buf_out;
00163 int ret;
00164
00165 if (!s->buf_out &&
00166 head->audio->nb_samples >= link->request_samples &&
00167 calc_ptr_alignment(head) >= 32) {
00168 if (head->audio->nb_samples == link->request_samples) {
00169 buf_out = head;
00170 queue_pop(s);
00171 } else {
00172 buf_out = avfilter_ref_buffer(head, AV_PERM_READ);
00173 if (!buf_out)
00174 return AVERROR(ENOMEM);
00175
00176 buf_out->audio->nb_samples = link->request_samples;
00177 buffer_offset(link, head, link->request_samples);
00178 }
00179 } else {
00180 int nb_channels = av_get_channel_layout_nb_channels(link->channel_layout);
00181
00182 if (!s->buf_out) {
00183 s->buf_out = ff_get_audio_buffer(link, AV_PERM_WRITE,
00184 link->request_samples);
00185 if (!s->buf_out)
00186 return AVERROR(ENOMEM);
00187
00188 s->buf_out->audio->nb_samples = 0;
00189 s->buf_out->pts = head->pts;
00190 s->allocated_samples = link->request_samples;
00191 } else if (link->request_samples != s->allocated_samples) {
00192 av_log(ctx, AV_LOG_ERROR, "request_samples changed before the "
00193 "buffer was returned.\n");
00194 return AVERROR(EINVAL);
00195 }
00196
00197 while (s->buf_out->audio->nb_samples < s->allocated_samples) {
00198 int len = FFMIN(s->allocated_samples - s->buf_out->audio->nb_samples,
00199 head->audio->nb_samples);
00200
00201 av_samples_copy(s->buf_out->extended_data, head->extended_data,
00202 s->buf_out->audio->nb_samples, 0, len, nb_channels,
00203 link->format);
00204 s->buf_out->audio->nb_samples += len;
00205
00206 if (len == head->audio->nb_samples) {
00207 avfilter_unref_buffer(head);
00208 queue_pop(s);
00209
00210 if (!s->root.next &&
00211 (ret = ff_request_frame(ctx->inputs[0])) < 0) {
00212 if (ret == AVERROR_EOF) {
00213 av_samples_set_silence(s->buf_out->extended_data,
00214 s->buf_out->audio->nb_samples,
00215 s->allocated_samples -
00216 s->buf_out->audio->nb_samples,
00217 nb_channels, link->format);
00218 s->buf_out->audio->nb_samples = s->allocated_samples;
00219 break;
00220 }
00221 return ret;
00222 }
00223 head = s->root.next->buf;
00224 } else {
00225 buffer_offset(link, head, len);
00226 }
00227 }
00228 buf_out = s->buf_out;
00229 s->buf_out = NULL;
00230 }
00231 return ff_filter_samples(link, buf_out);
00232 }
00233
00234 static int request_frame(AVFilterLink *outlink)
00235 {
00236 FifoContext *fifo = outlink->src->priv;
00237 int ret = 0;
00238
00239 if (!fifo->root.next) {
00240 if ((ret = ff_request_frame(outlink->src->inputs[0])) < 0)
00241 return ret;
00242 }
00243
00244
00245
00246 switch (outlink->type) {
00247 case AVMEDIA_TYPE_VIDEO:
00248 if ((ret = ff_start_frame(outlink, fifo->root.next->buf)) < 0 ||
00249 (ret = ff_draw_slice(outlink, 0, outlink->h, 1)) < 0 ||
00250 (ret = ff_end_frame(outlink)) < 0)
00251 return ret;
00252
00253 queue_pop(fifo);
00254 break;
00255 case AVMEDIA_TYPE_AUDIO:
00256 if (outlink->request_samples) {
00257 return return_audio_frame(outlink->src);
00258 } else {
00259 ret = ff_filter_samples(outlink, fifo->root.next->buf);
00260 queue_pop(fifo);
00261 }
00262 break;
00263 default:
00264 return AVERROR(EINVAL);
00265 }
00266
00267 return ret;
00268 }
00269
00270 AVFilter avfilter_vf_fifo = {
00271 .name = "fifo",
00272 .description = NULL_IF_CONFIG_SMALL("Buffer input images and send them when they are requested."),
00273
00274 .init = init,
00275 .uninit = uninit,
00276
00277 .priv_size = sizeof(FifoContext),
00278
00279 .inputs = (const AVFilterPad[]) {{ .name = "default",
00280 .type = AVMEDIA_TYPE_VIDEO,
00281 .get_video_buffer= ff_null_get_video_buffer,
00282 .start_frame = add_to_queue,
00283 .draw_slice = draw_slice,
00284 .end_frame = end_frame,
00285 .min_perms = AV_PERM_PRESERVE, },
00286 { .name = NULL}},
00287 .outputs = (const AVFilterPad[]) {{ .name = "default",
00288 .type = AVMEDIA_TYPE_VIDEO,
00289 .request_frame = request_frame, },
00290 { .name = NULL}},
00291 };
00292
00293 AVFilter avfilter_af_afifo = {
00294 .name = "afifo",
00295 .description = NULL_IF_CONFIG_SMALL("Buffer input frames and send them when they are requested."),
00296
00297 .init = init,
00298 .uninit = uninit,
00299
00300 .priv_size = sizeof(FifoContext),
00301
00302 .inputs = (const AVFilterPad[]) {{ .name = "default",
00303 .type = AVMEDIA_TYPE_AUDIO,
00304 .get_audio_buffer = ff_null_get_audio_buffer,
00305 .filter_samples = add_to_queue,
00306 .min_perms = AV_PERM_PRESERVE, },
00307 { .name = NULL}},
00308 .outputs = (const AVFilterPad[]) {{ .name = "default",
00309 .type = AVMEDIA_TYPE_AUDIO,
00310 .request_frame = request_frame, },
00311 { .name = NULL}},
00312 };