]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_fifo.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavfilter / vf_fifo.c
1 /*
2  * Copyright (c) 2007 Bobby Bingham
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 /**
22  * @file
23  * FIFO buffering video filter
24  */
25
26 #include "avfilter.h"
27 #include "video.h"
28
29 typedef struct BufPic {
30     AVFilterBufferRef *picref;
31     struct BufPic     *next;
32 } BufPic;
33
34 typedef struct {
35     BufPic  root;
36     BufPic *last;   ///< last buffered picture
37 } FifoContext;
38
39 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
40 {
41     FifoContext *fifo = ctx->priv;
42     fifo->last = &fifo->root;
43
44     av_log(ctx, AV_LOG_INFO, "\n");
45     return 0;
46 }
47
48 static av_cold void uninit(AVFilterContext *ctx)
49 {
50     FifoContext *fifo = ctx->priv;
51     BufPic *pic, *tmp;
52
53     for (pic = fifo->root.next; pic; pic = tmp) {
54         tmp = pic->next;
55         avfilter_unref_buffer(pic->picref);
56         av_free(pic);
57     }
58 }
59
60 static void start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
61 {
62     FifoContext *fifo = inlink->dst->priv;
63
64     fifo->last->next = av_mallocz(sizeof(BufPic));
65     fifo->last = fifo->last->next;
66     fifo->last->picref = picref;
67 }
68
69 static void end_frame(AVFilterLink *inlink) { }
70
71 static void draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir) { }
72
73 static int request_frame(AVFilterLink *outlink)
74 {
75     FifoContext *fifo = outlink->src->priv;
76     BufPic *tmp;
77     int ret;
78
79     if (!fifo->root.next) {
80         if ((ret = avfilter_request_frame(outlink->src->inputs[0])) < 0)
81             return ret;
82     }
83
84     /* by doing this, we give ownership of the reference to the next filter,
85      * so we don't have to worry about dereferencing it ourselves. */
86     avfilter_start_frame(outlink, fifo->root.next->picref);
87     avfilter_draw_slice (outlink, 0, outlink->h, 1);
88     avfilter_end_frame  (outlink);
89
90     if (fifo->last == fifo->root.next)
91         fifo->last = &fifo->root;
92     tmp = fifo->root.next->next;
93     av_free(fifo->root.next);
94     fifo->root.next = tmp;
95
96     return 0;
97 }
98
99 AVFilter avfilter_vf_fifo = {
100     .name      = "fifo",
101     .description = NULL_IF_CONFIG_SMALL("Buffer input images and send them when they are requested."),
102
103     .init      = init,
104     .uninit    = uninit,
105
106     .priv_size = sizeof(FifoContext),
107
108     .inputs    = (const AVFilterPad[]) {{ .name      = "default",
109                                     .type            = AVMEDIA_TYPE_VIDEO,
110                                     .get_video_buffer= ff_null_get_video_buffer,
111                                     .start_frame     = start_frame,
112                                     .draw_slice      = draw_slice,
113                                     .end_frame       = end_frame,
114                                     .rej_perms       = AV_PERM_REUSE2, },
115                                   { .name = NULL}},
116     .outputs   = (const AVFilterPad[]) {{ .name      = "default",
117                                     .type            = AVMEDIA_TYPE_VIDEO,
118                                     .request_frame   = request_frame, },
119                                   { .name = NULL}},
120 };