Browse code

lavfi: merge all filtering code into ff_filter_frame.

Nicolas George authored on 2012/12/31 03:28:51
Showing 3 changed files
... ...
@@ -179,114 +179,3 @@ AVFilterBufferRef* avfilter_get_audio_buffer_ref_from_arrays(uint8_t **data,
179 179
                                                               nb_samples, sample_fmt,
180 180
                                                               channels, channel_layout);
181 181
 }
182
-
183
-static int default_filter_frame(AVFilterLink *link, AVFilterBufferRef *frame)
184
-{
185
-    return ff_filter_frame(link->dst->outputs[0], frame);
186
-}
187
-
188
-int ff_filter_samples_framed(AVFilterLink *link, AVFilterBufferRef *samplesref)
189
-{
190
-    int (*filter_frame)(AVFilterLink *, AVFilterBufferRef *);
191
-    AVFilterPad *src = link->srcpad;
192
-    AVFilterPad *dst = link->dstpad;
193
-    int64_t pts;
194
-    AVFilterBufferRef *buf_out;
195
-    int ret;
196
-
197
-    FF_TPRINTF_START(NULL, filter_frame); ff_tlog_link(NULL, link, 1);
198
-
199
-    if (link->closed) {
200
-        avfilter_unref_buffer(samplesref);
201
-        return AVERROR_EOF;
202
-    }
203
-
204
-    if (!(filter_frame = dst->filter_frame))
205
-        filter_frame = default_filter_frame;
206
-
207
-    av_assert1((samplesref->perms & src->min_perms) == src->min_perms);
208
-    samplesref->perms &= ~ src->rej_perms;
209
-
210
-    /* prepare to copy the samples if the buffer has insufficient permissions */
211
-    if ((dst->min_perms & samplesref->perms) != dst->min_perms ||
212
-        dst->rej_perms & samplesref->perms) {
213
-        av_log(link->dst, AV_LOG_DEBUG,
214
-               "Copying audio data in avfilter (have perms %x, need %x, reject %x)\n",
215
-               samplesref->perms, link->dstpad->min_perms, link->dstpad->rej_perms);
216
-
217
-        buf_out = ff_default_get_audio_buffer(link, dst->min_perms,
218
-                                              samplesref->audio->nb_samples);
219
-        if (!buf_out) {
220
-            avfilter_unref_buffer(samplesref);
221
-            return AVERROR(ENOMEM);
222
-        }
223
-        buf_out->pts                = samplesref->pts;
224
-        buf_out->audio->sample_rate = samplesref->audio->sample_rate;
225
-
226
-        /* Copy actual data into new samples buffer */
227
-        av_samples_copy(buf_out->extended_data, samplesref->extended_data,
228
-                        0, 0, samplesref->audio->nb_samples,
229
-                        av_get_channel_layout_nb_channels(link->channel_layout),
230
-                        link->format);
231
-
232
-        avfilter_unref_buffer(samplesref);
233
-    } else
234
-        buf_out = samplesref;
235
-
236
-    link->cur_buf = buf_out;
237
-    pts = buf_out->pts;
238
-    ret = filter_frame(link, buf_out);
239
-    ff_update_link_current_pts(link, pts);
240
-    return ret;
241
-}
242
-
243
-int ff_filter_samples(AVFilterLink *link, AVFilterBufferRef *samplesref)
244
-{
245
-    int insamples = samplesref->audio->nb_samples, inpos = 0, nb_samples;
246
-    AVFilterBufferRef *pbuf = link->partial_buf;
247
-    int nb_channels = av_get_channel_layout_nb_channels(link->channel_layout);
248
-    int ret = 0;
249
-
250
-    av_assert1(samplesref->format                == link->format);
251
-    av_assert1(samplesref->audio->channels       == link->channels);
252
-    av_assert1(samplesref->audio->channel_layout == link->channel_layout);
253
-    av_assert1(samplesref->audio->sample_rate    == link->sample_rate);
254
-
255
-    if (!link->min_samples ||
256
-        (!pbuf &&
257
-         insamples >= link->min_samples && insamples <= link->max_samples)) {
258
-        return ff_filter_samples_framed(link, samplesref);
259
-    }
260
-    /* Handle framing (min_samples, max_samples) */
261
-    while (insamples) {
262
-        if (!pbuf) {
263
-            AVRational samples_tb = { 1, link->sample_rate };
264
-            int perms = link->dstpad->min_perms | AV_PERM_WRITE;
265
-            pbuf = ff_get_audio_buffer(link, perms, link->partial_buf_size);
266
-            if (!pbuf) {
267
-                av_log(link->dst, AV_LOG_WARNING,
268
-                       "Samples dropped due to memory allocation failure.\n");
269
-                return 0;
270
-            }
271
-            avfilter_copy_buffer_ref_props(pbuf, samplesref);
272
-            pbuf->pts = samplesref->pts +
273
-                        av_rescale_q(inpos, samples_tb, link->time_base);
274
-            pbuf->audio->nb_samples = 0;
275
-        }
276
-        nb_samples = FFMIN(insamples,
277
-                           link->partial_buf_size - pbuf->audio->nb_samples);
278
-        av_samples_copy(pbuf->extended_data, samplesref->extended_data,
279
-                        pbuf->audio->nb_samples, inpos,
280
-                        nb_samples, nb_channels, link->format);
281
-        inpos                   += nb_samples;
282
-        insamples               -= nb_samples;
283
-        pbuf->audio->nb_samples += nb_samples;
284
-        if (pbuf->audio->nb_samples >= link->min_samples) {
285
-            ret = ff_filter_samples_framed(link, pbuf);
286
-            pbuf = NULL;
287
-        }
288
-    }
289
-    avfilter_unref_buffer(samplesref);
290
-    link->partial_buf = pbuf;
291
-    return ret;
292
-}
... ...
@@ -34,6 +34,8 @@
34 34
 #include "internal.h"
35 35
 #include "audio.h"
36 36
 
37
+static int ff_filter_frame_framed(AVFilterLink *link, AVFilterBufferRef *frame);
38
+
37 39
 char *ff_get_ref_perms_string(char *buf, size_t buf_size, int perms)
38 40
 {
39 41
     snprintf(buf, buf_size, "%s%s%s%s%s%s",
... ...
@@ -342,7 +344,7 @@ int ff_request_frame(AVFilterLink *link)
342 342
     if (ret == AVERROR_EOF && link->partial_buf) {
343 343
         AVFilterBufferRef *pbuf = link->partial_buf;
344 344
         link->partial_buf = NULL;
345
-        ff_filter_samples_framed(link, pbuf);
345
+        ff_filter_frame_framed(link, pbuf);
346 346
         return 0;
347 347
     }
348 348
     if (ret == AVERROR_EOF)
... ...
@@ -631,22 +633,161 @@ enum AVMediaType avfilter_pad_get_type(AVFilterPad *pads, int pad_idx)
631 631
     return pads[pad_idx].type;
632 632
 }
633 633
 
634
+static int default_filter_frame(AVFilterLink *link, AVFilterBufferRef *frame)
635
+{
636
+    return ff_filter_frame(link->dst->outputs[0], frame);
637
+}
638
+
639
+static int ff_filter_frame_framed(AVFilterLink *link, AVFilterBufferRef *frame)
640
+{
641
+    int (*filter_frame)(AVFilterLink *, AVFilterBufferRef *);
642
+    AVFilterPad *src = link->srcpad;
643
+    AVFilterPad *dst = link->dstpad;
644
+    AVFilterBufferRef *out;
645
+    int perms, ret;
646
+    AVFilterCommand *cmd= link->dst->command_queue;
647
+    int64_t pts;
648
+
649
+    if (link->closed) {
650
+        avfilter_unref_buffer(frame);
651
+        return AVERROR_EOF;
652
+    }
653
+
654
+    if (!(filter_frame = dst->filter_frame))
655
+        filter_frame = default_filter_frame;
656
+
657
+    av_assert1((frame->perms & src->min_perms) == src->min_perms);
658
+    frame->perms &= ~ src->rej_perms;
659
+    perms = frame->perms;
660
+
661
+    if (frame->linesize[0] < 0)
662
+        perms |= AV_PERM_NEG_LINESIZES;
663
+
664
+    /* prepare to copy the frame if the buffer has insufficient permissions */
665
+    if ((dst->min_perms & perms) != dst->min_perms ||
666
+        dst->rej_perms & perms) {
667
+        av_log(link->dst, AV_LOG_DEBUG,
668
+               "Copying data in avfilter (have perms %x, need %x, reject %x)\n",
669
+               perms, link->dstpad->min_perms, link->dstpad->rej_perms);
670
+
671
+        /* Maybe use ff_copy_buffer_ref instead? */
672
+        switch (link->type) {
673
+        case AVMEDIA_TYPE_VIDEO:
674
+            out = ff_get_video_buffer(link, dst->min_perms,
675
+                                      link->w, link->h);
676
+            break;
677
+        case AVMEDIA_TYPE_AUDIO:
678
+            out = ff_get_audio_buffer(link, dst->min_perms,
679
+                                      frame->audio->nb_samples);
680
+            break;
681
+        default: return AVERROR(EINVAL);
682
+        }
683
+        if (!out) {
684
+            avfilter_unref_buffer(frame);
685
+            return AVERROR(ENOMEM);
686
+        }
687
+        avfilter_copy_buffer_ref_props(out, frame);
688
+
689
+        switch (link->type) {
690
+        case AVMEDIA_TYPE_VIDEO:
691
+            av_image_copy(out->data, out->linesize, frame->data, frame->linesize,
692
+                          frame->format, frame->video->w, frame->video->h);
693
+            break;
694
+        case AVMEDIA_TYPE_AUDIO:
695
+            av_samples_copy(out->extended_data, frame->extended_data,
696
+                            0, 0, frame->audio->nb_samples,
697
+                            av_get_channel_layout_nb_channels(frame->audio->channel_layout),
698
+                            frame->format);
699
+            break;
700
+        default: return AVERROR(EINVAL);
701
+        }
702
+
703
+        avfilter_unref_buffer(frame);
704
+    } else
705
+        out = frame;
706
+
707
+    while(cmd && cmd->time <= frame->pts * av_q2d(link->time_base)){
708
+        av_log(link->dst, AV_LOG_DEBUG,
709
+               "Processing command time:%f command:%s arg:%s\n",
710
+               cmd->time, cmd->command, cmd->arg);
711
+        avfilter_process_command(link->dst, cmd->command, cmd->arg, 0, 0, cmd->flags);
712
+        ff_command_queue_pop(link->dst);
713
+        cmd= link->dst->command_queue;
714
+    }
715
+
716
+    pts = out->pts;
717
+    ret = filter_frame(link, out);
718
+    ff_update_link_current_pts(link, pts);
719
+    return ret;
720
+}
721
+
722
+static int ff_filter_frame_needs_framing(AVFilterLink *link, AVFilterBufferRef *frame)
723
+{
724
+    int insamples = frame->audio->nb_samples, inpos = 0, nb_samples;
725
+    AVFilterBufferRef *pbuf = link->partial_buf;
726
+    int nb_channels = frame->audio->channels;
727
+    int ret = 0;
728
+
729
+    /* Handle framing (min_samples, max_samples) */
730
+    while (insamples) {
731
+        if (!pbuf) {
732
+            AVRational samples_tb = { 1, link->sample_rate };
733
+            int perms = link->dstpad->min_perms | AV_PERM_WRITE;
734
+            pbuf = ff_get_audio_buffer(link, perms, link->partial_buf_size);
735
+            if (!pbuf) {
736
+                av_log(link->dst, AV_LOG_WARNING,
737
+                       "Samples dropped due to memory allocation failure.\n");
738
+                return 0;
739
+            }
740
+            avfilter_copy_buffer_ref_props(pbuf, frame);
741
+            pbuf->pts = frame->pts +
742
+                        av_rescale_q(inpos, samples_tb, link->time_base);
743
+            pbuf->audio->nb_samples = 0;
744
+        }
745
+        nb_samples = FFMIN(insamples,
746
+                           link->partial_buf_size - pbuf->audio->nb_samples);
747
+        av_samples_copy(pbuf->extended_data, frame->extended_data,
748
+                        pbuf->audio->nb_samples, inpos,
749
+                        nb_samples, nb_channels, link->format);
750
+        inpos                   += nb_samples;
751
+        insamples               -= nb_samples;
752
+        pbuf->audio->nb_samples += nb_samples;
753
+        if (pbuf->audio->nb_samples >= link->min_samples) {
754
+            ret = ff_filter_frame_framed(link, pbuf);
755
+            pbuf = NULL;
756
+        }
757
+    }
758
+    avfilter_unref_buffer(frame);
759
+    link->partial_buf = pbuf;
760
+    return ret;
761
+}
762
+
634 763
 int ff_filter_frame(AVFilterLink *link, AVFilterBufferRef *frame)
635 764
 {
636
-    int ret;
637 765
     FF_TPRINTF_START(NULL, filter_frame); ff_tlog_link(NULL, link, 1); ff_tlog(NULL, " "); ff_tlog_ref(NULL, frame, 1);
638 766
 
639
-    switch (link->type) {
640
-    case AVMEDIA_TYPE_VIDEO:
641
-        if((ret = ff_start_frame(link, frame)) < 0)
642
-            return ret;
643
-        if((ret = ff_draw_slice(link, 0, frame->video->h, 1)) < 0)
644
-            return ret;
645
-        if((ret = ff_end_frame(link)) < 0)
646
-            return ret;
647
-        return ret;
648
-    case AVMEDIA_TYPE_AUDIO:
649
-        return ff_filter_samples(link, frame);
650
-    default: return AVERROR(EINVAL);
767
+    /* Consistency checks */
768
+    if (link->type == AVMEDIA_TYPE_VIDEO) {
769
+        if (strcmp(link->dst->filter->name, "scale")) {
770
+            av_assert1(frame->format                 == link->format);
771
+            av_assert1(frame->video->w               == link->w);
772
+            av_assert1(frame->video->h               == link->h);
773
+        }
774
+    } else {
775
+        av_assert1(frame->format                == link->format);
776
+        av_assert1(frame->audio->channels       == link->channels);
777
+        av_assert1(frame->audio->channel_layout == link->channel_layout);
778
+        av_assert1(frame->audio->sample_rate    == link->sample_rate);
779
+    }
780
+
781
+    /* Go directly to actual filtering if possible */
782
+    if (link->type == AVMEDIA_TYPE_AUDIO &&
783
+        link->min_samples &&
784
+        (link->partial_buf ||
785
+         frame->audio->nb_samples < link->min_samples ||
786
+         frame->audio->nb_samples > link->max_samples)) {
787
+        return ff_filter_frame_needs_framing(link, frame);
788
+    } else {
789
+        return ff_filter_frame_framed(link, frame);
651 790
     }
652 791
 }
... ...
@@ -158,204 +158,3 @@ AVFilterBufferRef *ff_get_video_buffer(AVFilterLink *link, int perms, int w, int
158 158
 
159 159
     return ret;
160 160
 }
161
-
162
-static int default_start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
163
-{
164
-    AVFilterLink *outlink = NULL;
165
-
166
-    if (inlink->dstpad->filter_frame)
167
-        return 0;
168
-
169
-    if (inlink->dst->nb_outputs)
170
-        outlink = inlink->dst->outputs[0];
171
-
172
-    if (outlink && (inlink->dstpad->start_frame || inlink->dstpad->end_frame)) {
173
-        AVFilterBufferRef *buf_out;
174
-        outlink->out_buf = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
175
-        if (!outlink->out_buf)
176
-            return AVERROR(ENOMEM);
177
-
178
-        avfilter_copy_buffer_ref_props(outlink->out_buf, picref);
179
-        outlink->out_buf->video->w = outlink->w;
180
-        outlink->out_buf->video->h = outlink->h;
181
-        buf_out = avfilter_ref_buffer(outlink->out_buf, ~0);
182
-        if (!buf_out)
183
-            return AVERROR(ENOMEM);
184
-
185
-        return ff_start_frame(outlink, buf_out);
186
-    }
187
-    return 0;
188
-}
189
-
190
-static void clear_link(AVFilterLink *link)
191
-{
192
-    avfilter_unref_bufferp(&link->cur_buf);
193
-    avfilter_unref_bufferp(&link->src_buf);
194
-    avfilter_unref_bufferp(&link->out_buf);
195
-    link->cur_buf_copy = NULL; /* we do not own the reference */
196
-}
197
-
198
-/* XXX: should we do the duplicating of the picture ref here, instead of
199
- * forcing the source filter to do it? */
200
-int ff_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
201
-{
202
-    int (*start_frame)(AVFilterLink *, AVFilterBufferRef *);
203
-    AVFilterPad *src = link->srcpad;
204
-    AVFilterPad *dst = link->dstpad;
205
-    int ret, perms;
206
-    AVFilterCommand *cmd= link->dst->command_queue;
207
-    int64_t pts;
208
-
209
-    FF_TPRINTF_START(NULL, start_frame); ff_tlog_link(NULL, link, 0); ff_tlog(NULL, " "); ff_tlog_ref(NULL, picref, 1);
210
-
211
-    if (strcmp(link->dst->filter->name, "scale")) {
212
-        av_assert1(picref->format                     == link->format);
213
-        av_assert1(picref->video->w                   == link->w);
214
-        av_assert1(picref->video->h                   == link->h);
215
-    }
216
-
217
-    if (link->closed) {
218
-        avfilter_unref_buffer(picref);
219
-        return AVERROR_EOF;
220
-    }
221
-
222
-    if (!(start_frame = dst->start_frame))
223
-        start_frame = default_start_frame;
224
-
225
-    av_assert1((picref->perms & src->min_perms) == src->min_perms);
226
-    picref->perms &= ~ src->rej_perms;
227
-    perms = picref->perms;
228
-
229
-    if (picref->linesize[0] < 0)
230
-        perms |= AV_PERM_NEG_LINESIZES;
231
-    /* prepare to copy the picture if it has insufficient permissions */
232
-    if ((dst->min_perms & perms) != dst->min_perms || dst->rej_perms & perms) {
233
-        av_log(link->dst, AV_LOG_DEBUG,
234
-                "frame copy needed (have perms %x, need %x, reject %x)\n",
235
-                picref->perms,
236
-                link->dstpad->min_perms, link->dstpad->rej_perms);
237
-
238
-        link->cur_buf = ff_get_video_buffer(link, dst->min_perms, link->w, link->h);
239
-        if (!link->cur_buf) {
240
-            avfilter_unref_bufferp(&picref);
241
-            return AVERROR(ENOMEM);
242
-        }
243
-
244
-        link->src_buf = picref;
245
-        avfilter_copy_buffer_ref_props(link->cur_buf, link->src_buf);
246
-
247
-        /* copy palette if required */
248
-        if (av_pix_fmt_desc_get(link->format)->flags & PIX_FMT_PAL)
249
-            memcpy(link->cur_buf->data[1], link->src_buf-> data[1], AVPALETTE_SIZE);
250
-    }
251
-    else
252
-        link->cur_buf = picref;
253
-
254
-    link->cur_buf_copy = link->cur_buf;
255
-
256
-    while(cmd && cmd->time <= picref->pts * av_q2d(link->time_base)){
257
-        av_log(link->dst, AV_LOG_DEBUG,
258
-               "Processing command time:%f command:%s arg:%s\n",
259
-               cmd->time, cmd->command, cmd->arg);
260
-        avfilter_process_command(link->dst, cmd->command, cmd->arg, 0, 0, cmd->flags);
261
-        ff_command_queue_pop(link->dst);
262
-        cmd= link->dst->command_queue;
263
-    }
264
-    pts = link->cur_buf->pts;
265
-    ret = start_frame(link, link->cur_buf);
266
-    ff_update_link_current_pts(link, pts);
267
-    if (ret < 0)
268
-        clear_link(link);
269
-    else
270
-        /* incoming buffers must not be freed in start frame,
271
-           because they can still be in use by the automatic copy mechanism */
272
-        av_assert1(link->cur_buf_copy->buf->refcount > 0);
273
-
274
-    return ret;
275
-}
276
-
277
-static int default_end_frame(AVFilterLink *inlink)
278
-{
279
-    AVFilterLink *outlink = NULL;
280
-
281
-    if (inlink->dstpad->filter_frame) {
282
-        int ret = inlink->dstpad->filter_frame(inlink, inlink->cur_buf);
283
-        inlink->cur_buf = NULL;
284
-        return ret;
285
-    }
286
-
287
-    if (inlink->dst->nb_outputs)
288
-        outlink = inlink->dst->outputs[0];
289
-
290
-    if (outlink) {
291
-        if (inlink->dstpad->filter_frame) {
292
-            int ret = inlink->dstpad->filter_frame(inlink, inlink->cur_buf);
293
-            inlink->cur_buf = NULL;
294
-            return ret;
295
-        } else if (inlink->dstpad->start_frame || inlink->dstpad->end_frame){
296
-            return ff_end_frame(outlink);
297
-        } else {
298
-            int ret = ff_filter_frame(outlink, inlink->cur_buf);
299
-            inlink->cur_buf = NULL;
300
-            return ret;
301
-        }
302
-    }
303
-    return 0;
304
-}
305
-
306
-int ff_end_frame(AVFilterLink *link)
307
-{
308
-    int (*end_frame)(AVFilterLink *);
309
-    int ret;
310
-
311
-    if (!(end_frame = link->dstpad->end_frame))
312
-        end_frame = default_end_frame;
313
-
314
-    ret = end_frame(link);
315
-
316
-    clear_link(link);
317
-
318
-    return ret;
319
-}
320
-
321
-int ff_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
322
-{
323
-    uint8_t *src[4], *dst[4];
324
-    int i, j, vsub;
325
-
326
-    FF_TPRINTF_START(NULL, draw_slice); ff_tlog_link(NULL, link, 0); ff_tlog(NULL, " y:%d h:%d dir:%d\n", y, h, slice_dir);
327
-
328
-    /* copy the slice if needed for permission reasons */
329
-    if (link->src_buf) {
330
-        const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(link->format);
331
-        vsub = desc->log2_chroma_h;
332
-
333
-        for (i = 0; i < 4; i++) {
334
-            if (link->src_buf->data[i]) {
335
-                src[i] = link->src_buf-> data[i] +
336
-                    (y >> (i==1 || i==2 ? vsub : 0)) * link->src_buf-> linesize[i];
337
-                dst[i] = link->cur_buf_copy->data[i] +
338
-                    (y >> (i==1 || i==2 ? vsub : 0)) * link->cur_buf_copy->linesize[i];
339
-            } else
340
-                src[i] = dst[i] = NULL;
341
-        }
342
-
343
-        for (i = 0; i < 4; i++) {
344
-            int planew =
345
-                av_image_get_linesize(link->format, link->cur_buf_copy->video->w, i);
346
-
347
-            if (!src[i]) continue;
348
-
349
-            for (j = 0; j < h >> (i==1 || i==2 ? vsub : 0); j++) {
350
-                memcpy(dst[i], src[i], planew);
351
-                src[i] += link->src_buf->linesize[i];
352
-                dst[i] += link->cur_buf_copy->linesize[i];
353
-            }
354
-        }
355
-    }
356
-
357
-        /* incoming buffers must not be freed in start frame,
358
-           because they can still be in use by the automatic copy mechanism */
359
-        av_assert1(link->cur_buf_copy->buf->refcount > 0);
360
-    return 0;
361
-}