summaryrefslogtreecommitdiffstats
path: root/video/decode/lavc_dr1.c
blob: f730a6c23139d4fbd2e004fa0a89480aecc00262 (plain)
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*
 * Various utilities for command line tools
 * Copyright (c) 2000-2003 Fabrice Bellard
 *
 * 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
 */


/*
 * NOTE: this file is for compatibility with older versions of
 *       libavcodec, before AVFrame reference counting was introduced.
 *       It is not compiled if libavcodec is new enough.
 */

#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <math.h>
#include <pthread.h>

#include <libavcodec/avcodec.h>
#include <libavutil/avassert.h>
#include <libavutil/mathematics.h>
#include <libavutil/imgutils.h>
#include <libavutil/pixdesc.h>
#include <libavutil/common.h>

#include "config.h"

#include "lavc.h"
#include "video/decode/dec_video.h"

static pthread_mutex_t pool_mutex = PTHREAD_MUTEX_INITIALIZER;
#define pool_lock() pthread_mutex_lock(&pool_mutex)
#define pool_unlock() pthread_mutex_unlock(&pool_mutex)

typedef struct FramePool {
    struct FrameBuffer *list;
    // used to deal with frames that live past the time the pool should live
    int dead;
    int refcount; // number of allocated buffers (not in list)
} FramePool;

typedef struct FrameBuffer {
    uint8_t *base[4];
    uint8_t *data[4];
    int  linesize[4];

    int h, w;
    int pix_fmt;

    int used_by_decoder, needed_by_decoder;
    int refcount;
    struct FramePool *pool;
    struct FrameBuffer *next;
} FrameBuffer;


static int alloc_buffer(FramePool *pool, AVCodecContext *s)
{
    const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[s->pix_fmt];
    FrameBuffer *buf;
    int i, ret;
    int pixel_size;
    int h_chroma_shift, v_chroma_shift;
    int edge = 32; // XXX should be avcodec_get_edge_width(), but that fails on svq1
    int w = s->width, h = s->height;

    if (!desc)
        return AVERROR(EINVAL);
    pixel_size = desc->comp[0].step_minus1 + 1;

    buf = av_mallocz(sizeof(*buf));
    if (!buf)
        return AVERROR(ENOMEM);

    avcodec_align_dimensions(s, &w, &h);

    if (!(s->flags & CODEC_FLAG_EMU_EDGE)) {
        w += 2*edge;
        h += 2*edge;
    }

    if ((ret = av_image_alloc(buf->base, buf->linesize, w, h,
                              s->pix_fmt, 32)) < 0) {
        av_freep(&buf);
        av_log(s, AV_LOG_ERROR, "alloc_buffer: av_image_alloc() failed\n");
        return ret;
    }
    /* XXX this shouldn't be needed, but some tests break without this line
     * those decoders are buggy and need to be fixed.
     * the following tests fail:
     * cdgraphics, ansi, aasc, fraps-v1, qtrle-1bit
     */
    memset(buf->base[0], 128, ret);

    avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
    for (i = 0; i < FF_ARRAY_ELEMS(buf->data); i++) {
        const int h_shift = i==0 ? 0 : h_chroma_shift;
        const int v_shift = i==0 ? 0 : v_chroma_shift;
        if ((s->flags & CODEC_FLAG_EMU_EDGE) || !buf->linesize[i] || !buf->base[i])
            buf->data[i] = buf->base[i];
        else
            buf->data[i] = buf->base[i] +
                           FFALIGN((buf->linesize[i]*edge >> v_shift) +
                                   (pixel_size*edge >> h_shift), 32);
    }
    buf->w       = s->width;
    buf->h       = s->height;
    buf->pix_fmt = s->pix_fmt;
    buf->pool    = pool;

    buf->next    = pool->list;
    pool->list   = buf;
    return 0;
}

int mp_codec_get_buffer(AVCodecContext *s, AVFrame *frame)
{
    struct dec_video *vd = s->opaque;
    struct lavc_ctx *ctx = vd->priv;

    if (!ctx->dr1_buffer_pool) {
        ctx->dr1_buffer_pool = av_mallocz(sizeof(*ctx->dr1_buffer_pool));
        if (!ctx->dr1_buffer_pool)
            return AVERROR(ENOMEM);
    }

    FramePool *pool = ctx->dr1_buffer_pool;
    FrameBuffer *buf;
    int ret, i;

    if(av_image_check_size(s->width, s->height, 0, s) || s->pix_fmt<0) {
        av_log(s, AV_LOG_ERROR, "codec_get_buffer: image parameters invalid\n");
        return -1;
    }

    pool_lock();

    if (!pool->list && (ret = alloc_buffer(pool, s)) < 0) {
        pool_unlock();
        return ret;
    }

    buf = pool->list;
    if (buf->w != s->width || buf->h != s->height || buf->pix_fmt != s->pix_fmt) {
        pool->list = buf->next;
        av_freep(&buf->base[0]);
        av_free(buf);
        if ((ret = alloc_buffer(pool, s)) < 0) {
            pool_unlock();
            return ret;
        }
        buf = pool->list;
    }
    av_assert0(!buf->refcount);
    buf->refcount++;

    pool->list = buf->next;
    pool->refcount++;

    pool_unlock();

    frame->opaque        = buf;
    frame->type          = FF_BUFFER_TYPE_USER;
    frame->extended_data = frame->data;

    buf->used_by_decoder = buf->needed_by_decoder = 1;
    if (frame->buffer_hints & FF_BUFFER_HINTS_VALID) {
        buf->needed_by_decoder =
            (frame->buffer_hints & FF_BUFFER_HINTS_PRESERVE) ||
            (frame->buffer_hints & FF_BUFFER_HINTS_REUSABLE);
    } else {
        buf->needed_by_decoder = !!frame->reference;
    }

    for (i = 0; i < FF_ARRAY_ELEMS(buf->data); i++) {
        frame->base[i]     = buf->base[i];  // XXX h264.c uses base though it shouldn't
        frame->data[i]     = buf->data[i];
        frame->linesize[i] = buf->linesize[i];
    }

    return 0;
}

void mp_buffer_ref(struct FrameBuffer *buf)
{
    pool_lock();
    buf->refcount++;
    pool_unlock();
}

bool mp_buffer_check(struct FrameBuffer *buf)
{
    pool_lock();
    bool ok = buf->refcount > 0;
    if (ok)
        buf->refcount++;
    pool_unlock();
    return ok;
}

void mp_buffer_unref(struct FrameBuffer *buf)
{
    FramePool *pool = buf->pool;
    bool pool_dead;

    pool_lock();

    av_assert0(pool->refcount > 0);
    av_assert0(buf->refcount > 0);
    buf->refcount--;
    if (!buf->refcount) {
        FrameBuffer *tmp;
        for(tmp= pool->list; tmp; tmp= tmp->next)
            av_assert1(tmp != buf);

        buf->next = pool->list;
        pool->list = buf;
        pool->refcount--;
    }

    pool_dead = pool->dead && pool->refcount == 0;
    pool_unlock();

    if (pool_dead)
        mp_buffer_pool_free(&pool);
}

bool mp_buffer_is_unique(struct FrameBuffer *buf)
{
    int refcount;
    pool_lock();
    refcount = buf->refcount;
    // Decoder has a reference, but doesn't want to use it. (ffmpeg has no good
    // way of transferring frame ownership to the user.)
    if (buf->used_by_decoder && !buf->needed_by_decoder)
        refcount--;
    pool_unlock();
    return refcount == 1;
}

void mp_codec_release_buffer(AVCodecContext *s, AVFrame *frame)
{
    FrameBuffer *buf = frame->opaque;
    int i;

    if(frame->type!=FF_BUFFER_TYPE_USER) {
        avcodec_default_release_buffer(s, frame);
        return;
    }

    buf->used_by_decoder = buf->needed_by_decoder = 0;

    for (i = 0; i < FF_ARRAY_ELEMS(frame->data); i++)
        frame->data[i] = NULL;

    mp_buffer_unref(buf);
}

void mp_buffer_pool_free(struct FramePool **p_pool)
{
    struct FramePool *pool = *p_pool;
    if (!pool)
        return;

    pool_lock();

    while (pool->list) {
        FrameBuffer *buf = pool->list;
        pool->list = buf->next;
        av_assert0(buf->refcount == 0);
        av_freep(&buf->base[0]);
        av_free(buf);
    }
    pool->dead = 1;
    if (pool->refcount == 0)
        av_free(pool);

    pool_unlock();

    *p_pool = NULL;
}