aboutsummaryrefslogtreecommitdiffstats
path: root/libavcodec/container_fifo.c
blob: 82e86d946530aae9537b3688507a907c40f34d77 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * FFmpeg is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with FFmpeg; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include "libavutil/error.h"
#include "libavutil/fifo.h"
#include "libavutil/frame.h"
#include "libavutil/mem.h"

#include "container_fifo.h"
#include "refstruct.h"

struct ContainerFifo {
    AVFifo             *fifo;
    FFRefStructPool    *pool;

    void*             (*container_alloc)(void);
    void              (*container_reset)(void *obj);
    void              (*container_free) (void *obj);
    int               (*fifo_write)     (void *dst, void *src);
    int               (*fifo_read)      (void *dst, void *src);

};

static int container_fifo_init_entry(FFRefStructOpaque opaque, void *obj)
{
    ContainerFifo *cf = opaque.nc;
    void **pobj = obj;

    *pobj = cf->container_alloc();
    if (!*pobj)
        return AVERROR(ENOMEM);

    return 0;
}

static void container_fifo_reset_entry(FFRefStructOpaque opaque, void *obj)
{
    ContainerFifo *cf = opaque.nc;
    cf->container_reset(*(void**)obj);
}

static void container_fifo_free_entry(FFRefStructOpaque opaque, void *obj)
{
    ContainerFifo *cf = opaque.nc;
    cf->container_free(*(void**)obj);
}

ContainerFifo*
ff_container_fifo_alloc(void* (*container_alloc)(void),
                        void  (*container_reset)(void *obj),
                        void  (*container_free) (void *obj),
                        int   (*fifo_write)     (void *dst, void *src),
                        int   (*fifo_read)      (void *dst, void *src))
{
    ContainerFifo *cf;

    cf = av_mallocz(sizeof(*cf));
    if (!cf)
        return NULL;

    cf->container_alloc = container_alloc;
    cf->container_reset = container_reset;
    cf->container_free  = container_free;
    cf->fifo_write      = fifo_write;
    cf->fifo_read       = fifo_read;

    cf->fifo = av_fifo_alloc2(1, sizeof(void*), AV_FIFO_FLAG_AUTO_GROW);
    if (!cf->fifo)
        goto fail;

    cf->pool = ff_refstruct_pool_alloc_ext(sizeof(void*), 0, cf,
                                           container_fifo_init_entry,
                                           container_fifo_reset_entry,
                                           container_fifo_free_entry,
                                           NULL);
    if (!cf->pool)
        goto fail;

    return cf;
fail:
    ff_container_fifo_free(&cf);
    return NULL;
}

void ff_container_fifo_free(ContainerFifo **pcf)
{
    ContainerFifo *cf;

    if (!*pcf)
        return;

    cf = *pcf;

    if (cf->fifo) {
        void *obj;
        while (av_fifo_read(cf->fifo, &obj, 1) >= 0)
            ff_refstruct_unref(&obj);
        av_fifo_freep2(&cf->fifo);
    }

    ff_refstruct_pool_uninit(&cf->pool);

    av_freep(pcf);
}

int ff_container_fifo_read(ContainerFifo *cf, void *obj)
{
    void **psrc;
    int ret;

    ret = av_fifo_read(cf->fifo, &psrc, 1);
    if (ret < 0)
        return ret;

    ret = cf->fifo_read(obj, *psrc);
    ff_refstruct_unref(&psrc);

    return ret;
}

int ff_container_fifo_write(ContainerFifo *cf, void *obj)
{
    void **pdst;
    int ret;

    pdst = ff_refstruct_pool_get(cf->pool);
    if (!pdst)
        return AVERROR(ENOMEM);

    ret = cf->fifo_write(*pdst, obj);
    if (ret < 0)
        goto fail;

    ret = av_fifo_write(cf->fifo, &pdst, 1);
    if (ret < 0)
        goto fail;

    return 0;
fail:
    ff_refstruct_unref(&pdst);
    return ret;
}

size_t ff_container_fifo_can_read(ContainerFifo *cf)
{
    return av_fifo_can_read(cf->fifo);
}

static void *frame_alloc(void)
{
    return av_frame_alloc();
}

static void frame_reset(void *obj)
{
    av_frame_unref(obj);
}

static void frame_free(void *obj)
{
    AVFrame *frame = obj;
    av_frame_free(&frame);
}

static int frame_ref(void *dst, void *src)
{
    return av_frame_ref(dst, src);
}

static int frame_move_ref(void *dst, void *src)
{
    av_frame_move_ref(dst, src);
    return 0;
}

ContainerFifo *ff_container_fifo_alloc_avframe(unsigned flags)
{
    return ff_container_fifo_alloc(frame_alloc, frame_reset, frame_free,
                                   frame_ref, frame_move_ref);
}