Statistics
| Branch: | Revision:

ffmpeg / libavcodec / utils.c @ 2d2b5a14

History | View | Annotate | Download (39.2 KB)

1 de6d9b64 Fabrice Bellard
/*
2
 * utils for libavcodec
3 406792e7 Diego Biurrun
 * Copyright (c) 2001 Fabrice Bellard
4 8f2ab833 Michael Niedermayer
 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
5 de6d9b64 Fabrice Bellard
 *
6 b78e7197 Diego Biurrun
 * This file is part of FFmpeg.
7
 *
8
 * FFmpeg is free software; you can redistribute it and/or
9 ff4ec49e Fabrice Bellard
 * modify it under the terms of the GNU Lesser General Public
10
 * License as published by the Free Software Foundation; either
11 b78e7197 Diego Biurrun
 * version 2.1 of the License, or (at your option) any later version.
12 de6d9b64 Fabrice Bellard
 *
13 b78e7197 Diego Biurrun
 * FFmpeg is distributed in the hope that it will be useful,
14 de6d9b64 Fabrice Bellard
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ff4ec49e Fabrice Bellard
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16
 * Lesser General Public License for more details.
17 de6d9b64 Fabrice Bellard
 *
18 ff4ec49e Fabrice Bellard
 * You should have received a copy of the GNU Lesser General Public
19 b78e7197 Diego Biurrun
 * License along with FFmpeg; if not, write to the Free Software
20 5509bffa Diego Biurrun
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 de6d9b64 Fabrice Bellard
 */
22 115329f1 Diego Biurrun
23 983e3246 Michael Niedermayer
/**
24 ba87f080 Diego Biurrun
 * @file
25 983e3246 Michael Niedermayer
 * utils.
26
 */
27 115329f1 Diego Biurrun
28 7fb94406 Diego Biurrun
#include "libavutil/avstring.h"
29 245976da Diego Biurrun
#include "libavutil/integer.h"
30
#include "libavutil/crc.h"
31 eb285cfe Reimar Döffinger
#include "libavutil/pixdesc.h"
32 7ffe76e5 Reinhard Tartler
#include "libavutil/audioconvert.h"
33
#include "libavutil/imgutils.h"
34
#include "libavutil/samplefmt.h"
35 de6d9b64 Fabrice Bellard
#include "avcodec.h"
36 3123dd79 Fabrice Bellard
#include "dsputil.h"
37 6ed04040 Michael Niedermayer
#include "libavutil/opt.h"
38 db7ae7d1 Vitor Sessak
#include "imgconvert.h"
39 b38f008e Alexander Strange
#include "thread.h"
40 9e82a113 Peter Ross
#include "audioconvert.h"
41 0ba39dd1 Kenan Gillet
#include "internal.h"
42 7246177d Aurelien Jacobs
#include <stdlib.h>
43 9b879566 Michel Bardiaux
#include <stdarg.h>
44 4c263142 Michael Niedermayer
#include <limits.h>
45 860a40c8 Michael Niedermayer
#include <float.h>
46 de6d9b64 Fabrice Bellard
47 ddebfb15 Michael Niedermayer
static int volatile entangled_thread_counter=0;
48 291f326a Diego Elio Pettenò
static int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op);
49 f988ce6c Andreas Öman
static void *codec_mutex;
50 ddebfb15 Michael Niedermayer
51 372c3f82 Michael Niedermayer
void *av_fast_realloc(void *ptr, unsigned int *size, FF_INTERNALC_MEM_TYPE min_size)
52 8e1e6f31 Fabrice Bellard
{
53 115329f1 Diego Biurrun
    if(min_size < *size)
54 8e1e6f31 Fabrice Bellard
        return ptr;
55 115329f1 Diego Biurrun
56 372c3f82 Michael Niedermayer
    min_size= FFMAX(17*min_size/16 + 32, min_size);
57 8e1e6f31 Fabrice Bellard
58 372c3f82 Michael Niedermayer
    ptr= av_realloc(ptr, min_size);
59 978805b2 Michael Niedermayer
    if(!ptr) //we could set this to the unmodified min_size but this is safer if the user lost the ptr and uses NULL now
60 372c3f82 Michael Niedermayer
        min_size= 0;
61
62
    *size= min_size;
63 978805b2 Michael Niedermayer
64
    return ptr;
65 8e1e6f31 Fabrice Bellard
}
66
67 372c3f82 Michael Niedermayer
void av_fast_malloc(void *ptr, unsigned int *size, FF_INTERNALC_MEM_TYPE min_size)
68 238ef6da Reimar Döffinger
{
69
    void **p = ptr;
70
    if (min_size < *size)
71
        return;
72 372c3f82 Michael Niedermayer
    min_size= FFMAX(17*min_size/16 + 32, min_size);
73 238ef6da Reimar Döffinger
    av_free(*p);
74 372c3f82 Michael Niedermayer
    *p = av_malloc(min_size);
75
    if (!*p) min_size = 0;
76
    *size= min_size;
77 238ef6da Reimar Döffinger
}
78
79 de6d9b64 Fabrice Bellard
/* encoder management */
80 e6df765e Diego Pettenò
static AVCodec *first_avcodec = NULL;
81 de6d9b64 Fabrice Bellard
82 55b9e69a Michael Niedermayer
AVCodec *av_codec_next(AVCodec *c){
83
    if(c) return c->next;
84
    else  return first_avcodec;
85
}
86
87 85662f49 Stefano Sabatini
void avcodec_register(AVCodec *codec)
88 de6d9b64 Fabrice Bellard
{
89
    AVCodec **p;
90 7a961a46 Stefano Sabatini
    avcodec_init();
91 de6d9b64 Fabrice Bellard
    p = &first_avcodec;
92
    while (*p != NULL) p = &(*p)->next;
93 335a761a Stefano Sabatini
    *p = codec;
94
    codec->next = NULL;
95 de6d9b64 Fabrice Bellard
}
96
97 9d385cfe Stefano Sabatini
#if LIBAVCODEC_VERSION_MAJOR < 53
98 85662f49 Stefano Sabatini
void register_avcodec(AVCodec *codec)
99
{
100
    avcodec_register(codec);
101
}
102 9d385cfe Stefano Sabatini
#endif
103 85662f49 Stefano Sabatini
104 0fb49b59 Bobby Bingham
unsigned avcodec_get_edge_width(void)
105
{
106
    return EDGE_WIDTH;
107
}
108
109 21adafec Michael Niedermayer
void avcodec_set_dimensions(AVCodecContext *s, int width, int height){
110
    s->coded_width = width;
111
    s->coded_height= height;
112
    s->width = -((-width )>>s->lowres);
113
    s->height= -((-height)>>s->lowres);
114
}
115
116 d90cf87b Michael Niedermayer
typedef struct InternalBuffer{
117 1e491e29 Michael Niedermayer
    int last_pic_num;
118 d90cf87b Michael Niedermayer
    uint8_t *base[4];
119 1e491e29 Michael Niedermayer
    uint8_t *data[4];
120 237e4938 Michael Niedermayer
    int linesize[4];
121 0701006e Michael Niedermayer
    int width, height;
122
    enum PixelFormat pix_fmt;
123 d90cf87b Michael Niedermayer
}InternalBuffer;
124
125
#define INTERNAL_BUFFER_SIZE 32
126 1e491e29 Michael Niedermayer
127 eb285cfe Reimar Döffinger
void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height, int linesize_align[4]){
128 115329f1 Diego Biurrun
    int w_align= 1;
129
    int h_align= 1;
130
131 f0bbfc4a Michael Niedermayer
    switch(s->pix_fmt){
132
    case PIX_FMT_YUV420P:
133 71e445fc Diego Biurrun
    case PIX_FMT_YUYV422:
134 ebb177dd Todd Kirby
    case PIX_FMT_UYVY422:
135 f0bbfc4a Michael Niedermayer
    case PIX_FMT_YUV422P:
136 98c82d69 Michael Niedermayer
    case PIX_FMT_YUV440P:
137 f0bbfc4a Michael Niedermayer
    case PIX_FMT_YUV444P:
138
    case PIX_FMT_GRAY8:
139 34380af0 Kostya Shishkov
    case PIX_FMT_GRAY16BE:
140
    case PIX_FMT_GRAY16LE:
141 f0bbfc4a Michael Niedermayer
    case PIX_FMT_YUVJ420P:
142
    case PIX_FMT_YUVJ422P:
143 98c82d69 Michael Niedermayer
    case PIX_FMT_YUVJ440P:
144 f0bbfc4a Michael Niedermayer
    case PIX_FMT_YUVJ444P:
145 b70335a2 Aurelien Jacobs
    case PIX_FMT_YUVA420P:
146 d4497f6d Oskar Arvidsson
    case PIX_FMT_YUV420P9LE:
147
    case PIX_FMT_YUV420P9BE:
148
    case PIX_FMT_YUV420P10LE:
149
    case PIX_FMT_YUV420P10BE:
150 f0bbfc4a Michael Niedermayer
        w_align= 16; //FIXME check for non mpeg style codecs and use less alignment
151
        h_align= 16;
152 00d1e96b Reimar Döffinger
        if(s->codec_id == CODEC_ID_MPEG2VIDEO || s->codec_id == CODEC_ID_MJPEG || s->codec_id == CODEC_ID_AMV || s->codec_id == CODEC_ID_THP || s->codec_id == CODEC_ID_H264)
153 c81185a1 Michael Niedermayer
            h_align= 32; // interlaced is rounded up to 2 MBs
154 f0bbfc4a Michael Niedermayer
        break;
155
    case PIX_FMT_YUV411P:
156 71e445fc Diego Biurrun
    case PIX_FMT_UYYVYY411:
157 f0bbfc4a Michael Niedermayer
        w_align=32;
158
        h_align=8;
159
        break;
160
    case PIX_FMT_YUV410P:
161
        if(s->codec_id == CODEC_ID_SVQ1){
162
            w_align=64;
163
            h_align=64;
164
        }
165 d99fbbf4 Roberto Togni
    case PIX_FMT_RGB555:
166
        if(s->codec_id == CODEC_ID_RPZA){
167
            w_align=4;
168
            h_align=4;
169
        }
170
    case PIX_FMT_PAL8:
171 6337178b Michael Niedermayer
    case PIX_FMT_BGR8:
172
    case PIX_FMT_RGB8:
173 d99fbbf4 Roberto Togni
        if(s->codec_id == CODEC_ID_SMC){
174
            w_align=4;
175
            h_align=4;
176
        }
177 f0bbfc4a Michael Niedermayer
        break;
178 c31b8121 Roberto Togni
    case PIX_FMT_BGR24:
179
        if((s->codec_id == CODEC_ID_MSZH) || (s->codec_id == CODEC_ID_ZLIB)){
180
            w_align=4;
181
            h_align=4;
182
        }
183
        break;
184 f0bbfc4a Michael Niedermayer
    default:
185
        w_align= 1;
186
        h_align= 1;
187
        break;
188
    }
189
190 ef516f73 David Conrad
    *width = FFALIGN(*width , w_align);
191
    *height= FFALIGN(*height, h_align);
192 ba68d9d3 Anatoly Nenashev
    if(s->codec_id == CODEC_ID_H264 || s->lowres)
193 ae4ffe9f Michael Niedermayer
        *height+=2; // some of the optimized chroma MC reads one line too much
194 ba68d9d3 Anatoly Nenashev
                    // which is also done in mpeg decoders with lowres > 0
195 eb285cfe Reimar Döffinger
196
    linesize_align[0] =
197
    linesize_align[1] =
198
    linesize_align[2] =
199
    linesize_align[3] = STRIDE_ALIGN;
200
//STRIDE_ALIGN is 8 for SSE* but this does not work for SVQ1 chroma planes
201
//we could change STRIDE_ALIGN to 16 for x86/sse but it would increase the
202
//picture size unneccessarily in some cases. The solution here is not
203
//pretty and better ideas are welcome!
204
#if HAVE_MMX
205
    if(s->codec_id == CODEC_ID_SVQ1 || s->codec_id == CODEC_ID_VP5 ||
206
       s->codec_id == CODEC_ID_VP6 || s->codec_id == CODEC_ID_VP6F ||
207
       s->codec_id == CODEC_ID_VP6A) {
208
        linesize_align[0] =
209
        linesize_align[1] =
210
        linesize_align[2] = 16;
211
    }
212
#endif
213
}
214
215
void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height){
216
    int chroma_shift = av_pix_fmt_descriptors[s->pix_fmt].log2_chroma_w;
217
    int linesize_align[4];
218
    int align;
219
    avcodec_align_dimensions2(s, width, height, linesize_align);
220
    align = FFMAX(linesize_align[0], linesize_align[3]);
221
    linesize_align[1] <<= chroma_shift;
222
    linesize_align[2] <<= chroma_shift;
223
    align = FFMAX3(align, linesize_align[1], linesize_align[2]);
224
    *width=FFALIGN(*width, align);
225 f0bbfc4a Michael Niedermayer
}
226
227 bf176f58 Stefano Sabatini
#if LIBAVCODEC_VERSION_MAJOR < 53
228 0ecca7a4 Michael Niedermayer
int avcodec_check_dimensions(void *av_log_ctx, unsigned int w, unsigned int h){
229 e16f217c Stefano Sabatini
    return av_image_check_size(w, h, 0, av_log_ctx);
230 0ecca7a4 Michael Niedermayer
}
231 bf176f58 Stefano Sabatini
#endif
232 0ecca7a4 Michael Niedermayer
233 492cd3a9 Michael Niedermayer
int avcodec_default_get_buffer(AVCodecContext *s, AVFrame *pic){
234 1e491e29 Michael Niedermayer
    int i;
235 f0bbfc4a Michael Niedermayer
    int w= s->width;
236
    int h= s->height;
237 d90cf87b Michael Niedermayer
    InternalBuffer *buf;
238 237e4938 Michael Niedermayer
    int *picture_number;
239 0ecca7a4 Michael Niedermayer
240 65d999d6 Michel Bardiaux
    if(pic->data[0]!=NULL) {
241
        av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
242
        return -1;
243
    }
244
    if(s->internal_buffer_count >= INTERNAL_BUFFER_SIZE) {
245
        av_log(s, AV_LOG_ERROR, "internal_buffer_count overflow (missing release_buffer?)\n");
246
        return -1;
247
    }
248 1e491e29 Michael Niedermayer
249 e16f217c Stefano Sabatini
    if(av_image_check_size(w, h, 0, s))
250 0ecca7a4 Michael Niedermayer
        return -1;
251
252 d90cf87b Michael Niedermayer
    if(s->internal_buffer==NULL){
253 6ebc89ac Michael Niedermayer
        s->internal_buffer= av_mallocz((INTERNAL_BUFFER_SIZE+1)*sizeof(InternalBuffer));
254 d90cf87b Michael Niedermayer
    }
255
#if 0
256
    s->internal_buffer= av_fast_realloc(
257 115329f1 Diego Biurrun
        s->internal_buffer,
258
        &s->internal_buffer_size,
259 d90cf87b Michael Niedermayer
        sizeof(InternalBuffer)*FFMAX(99,  s->internal_buffer_count+1)/*FIXME*/
260
        );
261
#endif
262 115329f1 Diego Biurrun
263 d90cf87b Michael Niedermayer
    buf= &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
264 6ebc89ac Michael Niedermayer
    picture_number= &(((InternalBuffer*)s->internal_buffer)[INTERNAL_BUFFER_SIZE]).last_pic_num; //FIXME ugly hack
265 237e4938 Michael Niedermayer
    (*picture_number)++;
266 115329f1 Diego Biurrun
267 0701006e Michael Niedermayer
    if(buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)){
268 b38f008e Alexander Strange
        if(s->active_thread_type&FF_THREAD_FRAME) {
269
            av_log_missing_feature(s, "Width/height changing with frame threads is", 0);
270
            return -1;
271
        }
272
273 0701006e Michael Niedermayer
        for(i=0; i<4; i++){
274
            av_freep(&buf->base[i]);
275
            buf->data[i]= NULL;
276
        }
277
    }
278
279 d90cf87b Michael Niedermayer
    if(buf->base[0]){
280 237e4938 Michael Niedermayer
        pic->age= *picture_number - buf->last_pic_num;
281
        buf->last_pic_num= *picture_number;
282 1e491e29 Michael Niedermayer
    }else{
283 f0bbfc4a Michael Niedermayer
        int h_chroma_shift, v_chroma_shift;
284 db7ae7d1 Vitor Sessak
        int size[4] = {0};
285
        int tmpsize;
286 c9d6e847 Reimar Döffinger
        int unaligned;
287 c7622f9a Michael Niedermayer
        AVPicture picture;
288 503bc402 Michael Niedermayer
        int stride_align[4];
289 dc172ecc Oskar Arvidsson
        const int pixel_size = av_pix_fmt_descriptors[s->pix_fmt].comp[0].step_minus1+1;
290 c7622f9a Michael Niedermayer
291 1e491e29 Michael Niedermayer
        avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
292 f0bbfc4a Michael Niedermayer
293 eb285cfe Reimar Döffinger
        avcodec_align_dimensions2(s, &w, &h, stride_align);
294 115329f1 Diego Biurrun
295 1e491e29 Michael Niedermayer
        if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
296
            w+= EDGE_WIDTH*2;
297
            h+= EDGE_WIDTH*2;
298
        }
299 5b67307a Loren Merritt
300 c9d6e847 Reimar Döffinger
        do {
301
            // NOTE: do not align linesizes individually, this breaks e.g. assumptions
302
            // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
303 9d2e0ad8 Stefano Sabatini
            av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
304 c9d6e847 Reimar Döffinger
            // increase alignment of w for next try (rhs gives the lowest bit set in w)
305
            w += w & ~(w-1);
306 db7ae7d1 Vitor Sessak
307 c9d6e847 Reimar Döffinger
            unaligned = 0;
308 45bae968 Reimar Döffinger
            for (i=0; i<4; i++){
309 c9d6e847 Reimar Döffinger
                unaligned |= picture.linesize[i] % stride_align[i];
310 45bae968 Reimar Döffinger
            }
311 c9d6e847 Reimar Döffinger
        } while (unaligned);
312 db7ae7d1 Vitor Sessak
313 9d2e0ad8 Stefano Sabatini
        tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
314 f8c96d01 Reimar Döffinger
        if (tmpsize < 0)
315
            return -1;
316 db7ae7d1 Vitor Sessak
317
        for (i=0; i<3 && picture.data[i+1]; i++)
318
            size[i] = picture.data[i+1] - picture.data[i];
319 cf73e32a Vitor Sessak
        size[i] = tmpsize - (picture.data[i] - picture.data[0]);
320 c7622f9a Michael Niedermayer
321 d90cf87b Michael Niedermayer
        buf->last_pic_num= -256*256*256*64;
322 c7622f9a Michael Niedermayer
        memset(buf->base, 0, sizeof(buf->base));
323
        memset(buf->data, 0, sizeof(buf->data));
324 1e491e29 Michael Niedermayer
325 b70335a2 Aurelien Jacobs
        for(i=0; i<4 && size[i]; i++){
326 2c19981a Michael Niedermayer
            const int h_shift= i==0 ? 0 : h_chroma_shift;
327
            const int v_shift= i==0 ? 0 : v_chroma_shift;
328 1e491e29 Michael Niedermayer
329 c7622f9a Michael Niedermayer
            buf->linesize[i]= picture.linesize[i];
330 1e491e29 Michael Niedermayer
331 c7622f9a Michael Niedermayer
            buf->base[i]= av_malloc(size[i]+16); //FIXME 16
332 d90cf87b Michael Niedermayer
            if(buf->base[i]==NULL) return -1;
333 c7622f9a Michael Niedermayer
            memset(buf->base[i], 128, size[i]);
334
335 3491a9b2 Jai Menon
            // no edge if EDGE EMU or not planar YUV
336 6337178b Michael Niedermayer
            if((s->flags&CODEC_FLAG_EMU_EDGE) || !size[2])
337 d90cf87b Michael Niedermayer
                buf->data[i] = buf->base[i];
338 1e491e29 Michael Niedermayer
            else
339 dc172ecc Oskar Arvidsson
                buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (pixel_size*EDGE_WIDTH>>h_shift), stride_align[i]);
340 1e491e29 Michael Niedermayer
        }
341 6337178b Michael Niedermayer
        if(size[1] && !size[2])
342 ed5d30d9 Stefano Sabatini
            ff_set_systematic_pal2((uint32_t*)buf->data[1], s->pix_fmt);
343 0701006e Michael Niedermayer
        buf->width  = s->width;
344
        buf->height = s->height;
345
        buf->pix_fmt= s->pix_fmt;
346 1e491e29 Michael Niedermayer
        pic->age= 256*256*256*64;
347
    }
348 237e4938 Michael Niedermayer
    pic->type= FF_BUFFER_TYPE_INTERNAL;
349 1e491e29 Michael Niedermayer
350 d90cf87b Michael Niedermayer
    for(i=0; i<4; i++){
351
        pic->base[i]= buf->base[i];
352
        pic->data[i]= buf->data[i];
353 237e4938 Michael Niedermayer
        pic->linesize[i]= buf->linesize[i];
354 d90cf87b Michael Niedermayer
    }
355
    s->internal_buffer_count++;
356
357 393cbb96 Michael Niedermayer
    if(s->pkt) pic->pkt_pts= s->pkt->pts;
358
    else       pic->pkt_pts= AV_NOPTS_VALUE;
359 79de84f2 Michael Niedermayer
    pic->reordered_opaque= s->reordered_opaque;
360
361 385c820b Alexander Strange
    if(s->debug&FF_DEBUG_BUFFERS)
362
        av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d buffers used\n", pic, s->internal_buffer_count);
363
364 1e491e29 Michael Niedermayer
    return 0;
365
}
366
367 492cd3a9 Michael Niedermayer
void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
368 1e491e29 Michael Niedermayer
    int i;
369 6829ac8d Aurelien Jacobs
    InternalBuffer *buf, *last;
370 d90cf87b Michael Niedermayer
371 4e00e76b Michael Niedermayer
    assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
372 59c673d5 Michael Niedermayer
    assert(s->internal_buffer_count);
373 d90cf87b Michael Niedermayer
374 d375c104 Michael Niedermayer
    if(s->internal_buffer){
375 b1609412 Fabrice Bellard
    buf = NULL; /* avoids warning */
376 d90cf87b Michael Niedermayer
    for(i=0; i<s->internal_buffer_count; i++){ //just 3-5 checks so is not worth to optimize
377
        buf= &((InternalBuffer*)s->internal_buffer)[i];
378
        if(buf->data[0] == pic->data[0])
379
            break;
380
    }
381
    assert(i < s->internal_buffer_count);
382
    s->internal_buffer_count--;
383
    last = &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
384
385 6829ac8d Aurelien Jacobs
    FFSWAP(InternalBuffer, *buf, *last);
386 d375c104 Michael Niedermayer
    }
387 d90cf87b Michael Niedermayer
388 b70335a2 Aurelien Jacobs
    for(i=0; i<4; i++){
389 1e491e29 Michael Niedermayer
        pic->data[i]=NULL;
390 d90cf87b Michael Niedermayer
//        pic->base[i]=NULL;
391
    }
392 1e491e29 Michael Niedermayer
//printf("R%X\n", pic->opaque);
393 385c820b Alexander Strange
394
    if(s->debug&FF_DEBUG_BUFFERS)
395
        av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d buffers used\n", pic, s->internal_buffer_count);
396 1e491e29 Michael Niedermayer
}
397
398 e1c2a5a0 Roberto Togni
int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
399
    AVFrame temp_pic;
400
    int i;
401
402
    /* If no picture return a new buffer */
403
    if(pic->data[0] == NULL) {
404
        /* We will copy from buffer, so must be readable */
405
        pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
406
        return s->get_buffer(s, pic);
407
    }
408
409
    /* If internal buffer type return the same buffer */
410 b8af4fe9 Michael Niedermayer
    if(pic->type == FF_BUFFER_TYPE_INTERNAL) {
411 cf96cce7 Nicolas George
        if(s->pkt) pic->pkt_pts= s->pkt->pts;
412
        else       pic->pkt_pts= AV_NOPTS_VALUE;
413 b8af4fe9 Michael Niedermayer
        pic->reordered_opaque= s->reordered_opaque;
414 e1c2a5a0 Roberto Togni
        return 0;
415 b8af4fe9 Michael Niedermayer
    }
416 e1c2a5a0 Roberto Togni
417
    /*
418
     * Not internal type and reget_buffer not overridden, emulate cr buffer
419
     */
420
    temp_pic = *pic;
421
    for(i = 0; i < 4; i++)
422
        pic->data[i] = pic->base[i] = NULL;
423
    pic->opaque = NULL;
424
    /* Allocate new frame */
425
    if (s->get_buffer(s, pic))
426
        return -1;
427
    /* Copy image data from old buffer to new buffer */
428 636d6a4a Panagiotis Issaris
    av_picture_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width,
429 e1c2a5a0 Roberto Togni
             s->height);
430
    s->release_buffer(s, &temp_pic); // Release old frame
431
    return 0;
432
}
433
434 3a84713a Roman Shaposhnik
int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
435 9c3d33d6 Michael Niedermayer
    int i;
436
437
    for(i=0; i<count; i++){
438 3a84713a Roman Shaposhnik
        int r= func(c, (char*)arg + i*size);
439 9c3d33d6 Michael Niedermayer
        if(ret) ret[i]= r;
440
    }
441
    return 0;
442
}
443
444 8d23a86f Reimar Döffinger
int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr),void *arg, int *ret, int count){
445
    int i;
446
447
    for(i=0; i<count; i++){
448
        int r= func(c, arg, i, 0);
449
        if(ret) ret[i]= r;
450
    }
451
    return 0;
452
}
453
454 c269cf68 Michael Niedermayer
enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat *fmt){
455
    while (*fmt != PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
456
        ++fmt;
457 a33c7159 Michael Niedermayer
    return fmt[0];
458
}
459
460 9740beff Michael Niedermayer
void avcodec_get_frame_defaults(AVFrame *pic){
461
    memset(pic, 0, sizeof(AVFrame));
462
463 76ad67ca Nicolas George
    pic->pts = pic->best_effort_timestamp = AV_NOPTS_VALUE;
464 c342499d Michael Niedermayer
    pic->key_frame= 1;
465 9740beff Michael Niedermayer
}
466
467 492cd3a9 Michael Niedermayer
AVFrame *avcodec_alloc_frame(void){
468 9740beff Michael Niedermayer
    AVFrame *pic= av_malloc(sizeof(AVFrame));
469 115329f1 Diego Biurrun
470 9740beff Michael Niedermayer
    if(pic==NULL) return NULL;
471 115329f1 Diego Biurrun
472 9740beff Michael Niedermayer
    avcodec_get_frame_defaults(pic);
473 115329f1 Diego Biurrun
474 1e491e29 Michael Niedermayer
    return pic;
475
}
476
477 3ee8ca9b Aurelien Jacobs
static void avcodec_get_subtitle_defaults(AVSubtitle *sub)
478
{
479
    memset(sub, 0, sizeof(*sub));
480
    sub->pts = AV_NOPTS_VALUE;
481
}
482
483 5e4c7ca2 Ramiro Polla
int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
484 de6d9b64 Fabrice Bellard
{
485 ddebfb15 Michael Niedermayer
    int ret= -1;
486 115329f1 Diego Biurrun
487 f988ce6c Andreas Öman
    /* If there is a user-supplied mutex locking routine, call it. */
488
    if (ff_lockmgr_cb) {
489
        if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
490
            return -1;
491
    }
492
493 ddebfb15 Michael Niedermayer
    entangled_thread_counter++;
494
    if(entangled_thread_counter != 1){
495
        av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
496
        goto end;
497
    }
498 de6d9b64 Fabrice Bellard
499 9c87b8f7 Reimar Döffinger
    if(avctx->codec || !codec)
500 ddebfb15 Michael Niedermayer
        goto end;
501 09770af8 Michael Niedermayer
502 0edf8a7a Philip Gladstone
    if (codec->priv_data_size > 0) {
503 dc51a72b Michael Niedermayer
      if(!avctx->priv_data){
504 0edf8a7a Philip Gladstone
        avctx->priv_data = av_mallocz(codec->priv_data_size);
505 90f06cea Panagiotis Issaris
        if (!avctx->priv_data) {
506
            ret = AVERROR(ENOMEM);
507 ddebfb15 Michael Niedermayer
            goto end;
508 90f06cea Panagiotis Issaris
        }
509 dc51a72b Michael Niedermayer
        if(codec->priv_class){ //this can be droped once all user apps use   avcodec_get_context_defaults3()
510
            *(AVClass**)avctx->priv_data= codec->priv_class;
511
            av_opt_set_defaults(avctx->priv_data);
512
        }
513
      }
514 0edf8a7a Philip Gladstone
    } else {
515
        avctx->priv_data = NULL;
516
    }
517 21adafec Michael Niedermayer
518
    if(avctx->coded_width && avctx->coded_height)
519
        avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
520
    else if(avctx->width && avctx->height)
521
        avcodec_set_dimensions(avctx, avctx->width, avctx->height);
522
523 82eac2f3 Reimar Döffinger
    if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
524
        && (  av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
525
           || av_image_check_size(avctx->width,       avctx->height,       0, avctx) < 0)) {
526
        av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
527
        avcodec_set_dimensions(avctx, 0, 0);
528
    }
529
530 f19c58b4 Aurelien Jacobs
    /* if the decoder init function was already called previously,
531
       free the already allocated subtitle_header before overwriting it */
532
    if (codec->decode)
533
        av_freep(&avctx->subtitle_header);
534
535 fa77dd63 Peter Ross
#define SANE_NB_CHANNELS 128U
536 82eac2f3 Reimar Döffinger
    if (avctx->channels > SANE_NB_CHANNELS) {
537 7868349a Panagiotis Issaris
        ret = AVERROR(EINVAL);
538 2a9b5c9b Michael Niedermayer
        goto free_and_end;
539 0ecca7a4 Michael Niedermayer
    }
540
541 b5c85991 Michael Niedermayer
    avctx->codec = codec;
542 72415b2a Stefano Sabatini
    if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
543 681c180d Reimar Döffinger
        avctx->codec_id == CODEC_ID_NONE) {
544
        avctx->codec_type = codec->type;
545
        avctx->codec_id   = codec->id;
546
    }
547 e83c716e Aurelien Jacobs
    if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
548
                           && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
549 4c0dda2b Michael Niedermayer
        av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
550 2a9b5c9b Michael Niedermayer
        goto free_and_end;
551 4c0dda2b Michael Niedermayer
    }
552 b5c85991 Michael Niedermayer
    avctx->frame_number = 0;
553 b38f008e Alexander Strange
554
    if (HAVE_THREADS && !avctx->thread_opaque) {
555 46027c72 Reimar Döffinger
        ret = ff_thread_init(avctx);
556 b38f008e Alexander Strange
        if (ret < 0) {
557
            goto free_and_end;
558
        }
559
    }
560
561 527c91e3 Carl Eugen Hoyos
    if (avctx->codec->max_lowres < avctx->lowres) {
562
        av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
563
               avctx->codec->max_lowres);
564
        goto free_and_end;
565
    }
566 7ade06cc Reimar Döffinger
    if (avctx->codec->sample_fmts && avctx->codec->encode) {
567
        int i;
568
        for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++)
569
            if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
570
                break;
571
        if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
572
            av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
573
            goto free_and_end;
574
        }
575
    }
576 0fd0ef79 Carl Eugen Hoyos
577 76ad67ca Nicolas George
    avctx->pts_correction_num_faulty_pts =
578
    avctx->pts_correction_num_faulty_dts = 0;
579
    avctx->pts_correction_last_pts =
580
    avctx->pts_correction_last_dts = INT64_MIN;
581
582 b38f008e Alexander Strange
    if(avctx->codec->init && !(avctx->active_thread_type&FF_THREAD_FRAME)){
583 2de4f9eb Michael Niedermayer
        ret = avctx->codec->init(avctx);
584
        if (ret < 0) {
585 2a9b5c9b Michael Niedermayer
            goto free_and_end;
586 2de4f9eb Michael Niedermayer
        }
587 6e546aaa Michael Niedermayer
    }
588 d375c104 Michael Niedermayer
589 ddebfb15 Michael Niedermayer
    ret=0;
590
end:
591
    entangled_thread_counter--;
592 f988ce6c Andreas Öman
593
    /* Release any user-supplied mutex. */
594
    if (ff_lockmgr_cb) {
595
        (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
596
    }
597 ddebfb15 Michael Niedermayer
    return ret;
598 2a9b5c9b Michael Niedermayer
free_and_end:
599
    av_freep(&avctx->priv_data);
600
    avctx->codec= NULL;
601
    goto end;
602 de6d9b64 Fabrice Bellard
}
603
604 5e4c7ca2 Ramiro Polla
int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx, uint8_t *buf, int buf_size,
605 de6d9b64 Fabrice Bellard
                         const short *samples)
606
{
607 0ecca7a4 Michael Niedermayer
    if(buf_size < FF_MIN_BUFFER_SIZE && 0){
608 5286d11f Michel Bardiaux
        av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
609 0ecca7a4 Michael Niedermayer
        return -1;
610
    }
611 6f824977 Michael Niedermayer
    if((avctx->codec->capabilities & CODEC_CAP_DELAY) || samples){
612 8eb027c8 Baptiste Coudurier
        int ret = avctx->codec->encode(avctx, buf, buf_size, samples);
613 6f824977 Michael Niedermayer
        avctx->frame_number++;
614
        return ret;
615
    }else
616
        return 0;
617 de6d9b64 Fabrice Bellard
}
618
619 5e4c7ca2 Ramiro Polla
int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
620 492cd3a9 Michael Niedermayer
                         const AVFrame *pict)
621 de6d9b64 Fabrice Bellard
{
622 0ecca7a4 Michael Niedermayer
    if(buf_size < FF_MIN_BUFFER_SIZE){
623 5286d11f Michel Bardiaux
        av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
624 0ecca7a4 Michael Niedermayer
        return -1;
625
    }
626 e16f217c Stefano Sabatini
    if(av_image_check_size(avctx->width, avctx->height, 0, avctx))
627 0ecca7a4 Michael Niedermayer
        return -1;
628 6f824977 Michael Niedermayer
    if((avctx->codec->capabilities & CODEC_CAP_DELAY) || pict){
629 8eb027c8 Baptiste Coudurier
        int ret = avctx->codec->encode(avctx, buf, buf_size, pict);
630 6f824977 Michael Niedermayer
        avctx->frame_number++;
631 bb628dae Diego Biurrun
        emms_c(); //needed to avoid an emms_c() call before every return;
632 115329f1 Diego Biurrun
633 6f824977 Michael Niedermayer
        return ret;
634
    }else
635
        return 0;
636 de6d9b64 Fabrice Bellard
}
637
638 115329f1 Diego Biurrun
int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
639 240c1657 Fabrice Bellard
                            const AVSubtitle *sub)
640
{
641
    int ret;
642 9413db9e Björn Axelsson
    if(sub->start_display_time) {
643
        av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
644
        return -1;
645
    }
646 505aa6c9 Björn Axelsson
    if(sub->num_rects == 0 || !sub->rects)
647
        return -1;
648 8eb027c8 Baptiste Coudurier
    ret = avctx->codec->encode(avctx, buf, buf_size, sub);
649 240c1657 Fabrice Bellard
    avctx->frame_number++;
650
    return ret;
651
}
652
653 76ad67ca Nicolas George
/**
654
 * Attempt to guess proper monotonic timestamps for decoded video frames
655
 * which might have incorrect times. Input timestamps may wrap around, in
656
 * which case the output will as well.
657
 *
658
 * @param pts the pts field of the decoded AVPacket, as passed through
659
 * AVFrame.pkt_pts
660
 * @param dts the dts field of the decoded AVPacket
661
 * @return one of the input values, may be AV_NOPTS_VALUE
662
 */
663
static int64_t guess_correct_pts(AVCodecContext *ctx,
664
                                 int64_t reordered_pts, int64_t dts)
665
{
666
    int64_t pts = AV_NOPTS_VALUE;
667
668
    if (dts != AV_NOPTS_VALUE) {
669
        ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
670
        ctx->pts_correction_last_dts = dts;
671
    }
672
    if (reordered_pts != AV_NOPTS_VALUE) {
673
        ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
674
        ctx->pts_correction_last_pts = reordered_pts;
675
    }
676
    if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
677
       && reordered_pts != AV_NOPTS_VALUE)
678
        pts = reordered_pts;
679
    else
680
        pts = dts;
681
682
    return pts;
683
}
684
685
686 164bc38c Aurelien Jacobs
#if FF_API_VIDEO_OLD
687 5e4c7ca2 Ramiro Polla
int attribute_align_arg avcodec_decode_video(AVCodecContext *avctx, AVFrame *picture,
688 de6d9b64 Fabrice Bellard
                         int *got_picture_ptr,
689 2efa7fd1 Michael Niedermayer
                         const uint8_t *buf, int buf_size)
690 de6d9b64 Fabrice Bellard
{
691 7a00bbad Thilo Borgmann
    AVPacket avpkt;
692
    av_init_packet(&avpkt);
693
    avpkt.data = buf;
694
    avpkt.size = buf_size;
695 10f9ff9b Reimar Döffinger
    // HACK for CorePNG to decode as normal PNG by default
696
    avpkt.flags = AV_PKT_FLAG_KEY;
697 7a00bbad Thilo Borgmann
698
    return avcodec_decode_video2(avctx, picture, got_picture_ptr, &avpkt);
699
}
700
#endif
701
702
int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
703
                         int *got_picture_ptr,
704
                         AVPacket *avpkt)
705
{
706 de6d9b64 Fabrice Bellard
    int ret;
707 115329f1 Diego Biurrun
708 53db1cae Michael Niedermayer
    *got_picture_ptr= 0;
709 e16f217c Stefano Sabatini
    if((avctx->coded_width||avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
710 0ecca7a4 Michael Niedermayer
        return -1;
711 393cbb96 Michael Niedermayer
712
    avctx->pkt = avpkt;
713
714 b38f008e Alexander Strange
    if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type&FF_THREAD_FRAME)){
715
        if (HAVE_PTHREADS && avctx->active_thread_type&FF_THREAD_FRAME)
716
             ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
717
                                          avpkt);
718
        else {
719
            ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
720
                              avpkt);
721
            picture->pkt_dts= avpkt->dts;
722
        }
723 6bb925f4 Michael Niedermayer
724 bb628dae Diego Biurrun
        emms_c(); //needed to avoid an emms_c() call before every return;
725 115329f1 Diego Biurrun
726 1919feaf Michael Niedermayer
727 ad82bf9c Michael Niedermayer
        if (*got_picture_ptr){
728 934982c4 Michael Niedermayer
            avctx->frame_number++;
729 ad82bf9c Michael Niedermayer
            picture->best_effort_timestamp = guess_correct_pts(avctx,
730
                                                            picture->pkt_pts,
731
                                                            picture->pkt_dts);
732
        }
733 934982c4 Michael Niedermayer
    }else
734
        ret= 0;
735
736 de6d9b64 Fabrice Bellard
    return ret;
737
}
738
739 a377cfa6 Aurelien Jacobs
#if FF_API_AUDIO_OLD
740 5e4c7ca2 Ramiro Polla
int attribute_align_arg avcodec_decode_audio2(AVCodecContext *avctx, int16_t *samples,
741 de6d9b64 Fabrice Bellard
                         int *frame_size_ptr,
742 2efa7fd1 Michael Niedermayer
                         const uint8_t *buf, int buf_size)
743 de6d9b64 Fabrice Bellard
{
744 7a00bbad Thilo Borgmann
    AVPacket avpkt;
745
    av_init_packet(&avpkt);
746
    avpkt.data = buf;
747
    avpkt.size = buf_size;
748
749
    return avcodec_decode_audio3(avctx, samples, frame_size_ptr, &avpkt);
750
}
751
#endif
752
753
int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
754
                         int *frame_size_ptr,
755
                         AVPacket *avpkt)
756
{
757 de6d9b64 Fabrice Bellard
    int ret;
758
759 393cbb96 Michael Niedermayer
    avctx->pkt = avpkt;
760
761 7a00bbad Thilo Borgmann
    if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size){
762 9c856d62 Michael Niedermayer
        //FIXME remove the check below _after_ ensuring that all audio check that the available space is enough
763
        if(*frame_size_ptr < AVCODEC_MAX_AUDIO_FRAME_SIZE){
764
            av_log(avctx, AV_LOG_ERROR, "buffer smaller than AVCODEC_MAX_AUDIO_FRAME_SIZE\n");
765
            return -1;
766
        }
767
        if(*frame_size_ptr < FF_MIN_BUFFER_SIZE ||
768 81fc2f37 Kostya Shishkov
        *frame_size_ptr < avctx->channels * avctx->frame_size * sizeof(int16_t)){
769 9c856d62 Michael Niedermayer
            av_log(avctx, AV_LOG_ERROR, "buffer %d too small\n", *frame_size_ptr);
770
            return -1;
771
        }
772
773 7a00bbad Thilo Borgmann
        ret = avctx->codec->decode(avctx, samples, frame_size_ptr, avpkt);
774 fec9ccb7 Michael Niedermayer
        avctx->frame_number++;
775 ac66834c Michael Niedermayer
    }else{
776 fec9ccb7 Michael Niedermayer
        ret= 0;
777 ac66834c Michael Niedermayer
        *frame_size_ptr=0;
778
    }
779 de6d9b64 Fabrice Bellard
    return ret;
780
}
781
782 448ce925 Aurelien Jacobs
#if FF_API_SUBTITLE_OLD
783 240c1657 Fabrice Bellard
int avcodec_decode_subtitle(AVCodecContext *avctx, AVSubtitle *sub,
784
                            int *got_sub_ptr,
785
                            const uint8_t *buf, int buf_size)
786
{
787 7a00bbad Thilo Borgmann
    AVPacket avpkt;
788
    av_init_packet(&avpkt);
789
    avpkt.data = buf;
790
    avpkt.size = buf_size;
791
792
    return avcodec_decode_subtitle2(avctx, sub, got_sub_ptr, &avpkt);
793
}
794
#endif
795
796
int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
797
                            int *got_sub_ptr,
798
                            AVPacket *avpkt)
799
{
800 240c1657 Fabrice Bellard
    int ret;
801
802 393cbb96 Michael Niedermayer
    avctx->pkt = avpkt;
803 240c1657 Fabrice Bellard
    *got_sub_ptr = 0;
804 3ee8ca9b Aurelien Jacobs
    avcodec_get_subtitle_defaults(sub);
805 7a00bbad Thilo Borgmann
    ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
806 240c1657 Fabrice Bellard
    if (*got_sub_ptr)
807
        avctx->frame_number++;
808
    return ret;
809
}
810
811 e1d7c883 Reimar Döffinger
void avsubtitle_free(AVSubtitle *sub)
812
{
813
    int i;
814
815
    for (i = 0; i < sub->num_rects; i++)
816
    {
817 8b834ac5 Reimar Döffinger
        av_freep(&sub->rects[i]->pict.data[0]);
818
        av_freep(&sub->rects[i]->pict.data[1]);
819
        av_freep(&sub->rects[i]->pict.data[2]);
820
        av_freep(&sub->rects[i]->pict.data[3]);
821
        av_freep(&sub->rects[i]->text);
822
        av_freep(&sub->rects[i]->ass);
823
        av_freep(&sub->rects[i]);
824 e1d7c883 Reimar Döffinger
    }
825
826 8b834ac5 Reimar Döffinger
    av_freep(&sub->rects);
827 e1d7c883 Reimar Döffinger
828
    memset(sub, 0, sizeof(AVSubtitle));
829
}
830
831 0752cd39 Zuxy Meng
av_cold int avcodec_close(AVCodecContext *avctx)
832 de6d9b64 Fabrice Bellard
{
833 f988ce6c Andreas Öman
    /* If there is a user-supplied mutex locking routine, call it. */
834
    if (ff_lockmgr_cb) {
835
        if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
836
            return -1;
837
    }
838
839 ddebfb15 Michael Niedermayer
    entangled_thread_counter++;
840
    if(entangled_thread_counter != 1){
841
        av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
842
        entangled_thread_counter--;
843
        return -1;
844
    }
845
846 49fb20cb Aurelien Jacobs
    if (HAVE_THREADS && avctx->thread_opaque)
847 043d2ff2 Alexander Strange
        ff_thread_free(avctx);
848 dccda293 Michael Niedermayer
    if (avctx->codec && avctx->codec->close)
849 de6d9b64 Fabrice Bellard
        avctx->codec->close(avctx);
850 eea8c08f Michael Niedermayer
    avcodec_default_free_buffers(avctx);
851 b3ab3199 Reimar Döffinger
    avctx->coded_frame = NULL;
852 3123dd79 Fabrice Bellard
    av_freep(&avctx->priv_data);
853 77a670e7 Craig Thomasson
    if(avctx->codec && avctx->codec->encode)
854 c4f267ab Vitor Sessak
        av_freep(&avctx->extradata);
855 de6d9b64 Fabrice Bellard
    avctx->codec = NULL;
856 b38f008e Alexander Strange
    avctx->active_thread_type = 0;
857 ddebfb15 Michael Niedermayer
    entangled_thread_counter--;
858 f988ce6c Andreas Öman
859
    /* Release any user-supplied mutex. */
860
    if (ff_lockmgr_cb) {
861
        (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
862
    }
863 de6d9b64 Fabrice Bellard
    return 0;
864
}
865
866
AVCodec *avcodec_find_encoder(enum CodecID id)
867
{
868 93ebfeea Janne Grunau
    AVCodec *p, *experimental=NULL;
869 de6d9b64 Fabrice Bellard
    p = first_avcodec;
870
    while (p) {
871 93ebfeea Janne Grunau
        if (p->encode != NULL && p->id == id) {
872
            if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
873
                experimental = p;
874
            } else
875
                return p;
876
        }
877 de6d9b64 Fabrice Bellard
        p = p->next;
878
    }
879 93ebfeea Janne Grunau
    return experimental;
880 de6d9b64 Fabrice Bellard
}
881
882 98f3b098 Arpi
AVCodec *avcodec_find_encoder_by_name(const char *name)
883
{
884
    AVCodec *p;
885 fc228c90 Aurelien Jacobs
    if (!name)
886
        return NULL;
887 98f3b098 Arpi
    p = first_avcodec;
888
    while (p) {
889
        if (p->encode != NULL && strcmp(name,p->name) == 0)
890
            return p;
891
        p = p->next;
892
    }
893
    return NULL;
894
}
895
896 de6d9b64 Fabrice Bellard
AVCodec *avcodec_find_decoder(enum CodecID id)
897
{
898
    AVCodec *p;
899
    p = first_avcodec;
900
    while (p) {
901
        if (p->decode != NULL && p->id == id)
902
            return p;
903
        p = p->next;
904
    }
905
    return NULL;
906
}
907
908
AVCodec *avcodec_find_decoder_by_name(const char *name)
909
{
910
    AVCodec *p;
911 fc228c90 Aurelien Jacobs
    if (!name)
912
        return NULL;
913 de6d9b64 Fabrice Bellard
    p = first_avcodec;
914
    while (p) {
915
        if (p->decode != NULL && strcmp(name,p->name) == 0)
916
            return p;
917
        p = p->next;
918
    }
919
    return NULL;
920
}
921
922 406aa93f Carl Eugen Hoyos
static int get_bit_rate(AVCodecContext *ctx)
923 ce34ff6b Robert Krüger
{
924
    int bit_rate;
925
    int bits_per_sample;
926
927
    switch(ctx->codec_type) {
928 72415b2a Stefano Sabatini
    case AVMEDIA_TYPE_VIDEO:
929 4563cf24 Stefano Sabatini
    case AVMEDIA_TYPE_DATA:
930
    case AVMEDIA_TYPE_SUBTITLE:
931
    case AVMEDIA_TYPE_ATTACHMENT:
932 ce34ff6b Robert Krüger
        bit_rate = ctx->bit_rate;
933
        break;
934 72415b2a Stefano Sabatini
    case AVMEDIA_TYPE_AUDIO:
935 ce34ff6b Robert Krüger
        bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
936
        bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
937
        break;
938
    default:
939
        bit_rate = 0;
940
        break;
941
    }
942
    return bit_rate;
943
}
944
945 7e566bbe Stefano Sabatini
size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
946
{
947
    int i, len, ret = 0;
948
949
    for (i = 0; i < 4; i++) {
950
        len = snprintf(buf, buf_size,
951
                       isprint(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
952
        buf      += len;
953
        buf_size  = buf_size > len ? buf_size - len : 0;
954
        ret      += len;
955
        codec_tag>>=8;
956
    }
957
    return ret;
958
}
959
960 de6d9b64 Fabrice Bellard
void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
961
{
962
    const char *codec_name;
963 2a81f4bd Anssi Hannula
    const char *profile = NULL;
964 de6d9b64 Fabrice Bellard
    AVCodec *p;
965
    char buf1[32];
966 a96b68b7 Fabrice Bellard
    int bitrate;
967 59771f71 Michel Bardiaux
    AVRational display_aspect_ratio;
968 de6d9b64 Fabrice Bellard
969
    if (encode)
970
        p = avcodec_find_encoder(enc->codec_id);
971
    else
972
        p = avcodec_find_decoder(enc->codec_id);
973
974
    if (p) {
975
        codec_name = p->name;
976 2a81f4bd Anssi Hannula
        profile = av_get_profile_name(p, enc->profile);
977 985180a1 Fabrice Bellard
    } else if (enc->codec_id == CODEC_ID_MPEG2TS) {
978
        /* fake mpeg2 transport stream codec (currently not
979
           registered) */
980
        codec_name = "mpeg2ts";
981 de6d9b64 Fabrice Bellard
    } else if (enc->codec_name[0] != '\0') {
982
        codec_name = enc->codec_name;
983
    } else {
984
        /* output avi tags */
985 ab0b5378 Stefano Sabatini
        char tag_buf[32];
986
        av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
987
        snprintf(buf1, sizeof(buf1), "%s / 0x%04X", tag_buf, enc->codec_tag);
988 de6d9b64 Fabrice Bellard
        codec_name = buf1;
989
    }
990
991
    switch(enc->codec_type) {
992 72415b2a Stefano Sabatini
    case AVMEDIA_TYPE_VIDEO:
993 de6d9b64 Fabrice Bellard
        snprintf(buf, buf_size,
994
                 "Video: %s%s",
995 7d1c3fc1 Michael Niedermayer
                 codec_name, enc->mb_decision ? " (hq)" : "");
996 2a81f4bd Anssi Hannula
        if (profile)
997
            snprintf(buf + strlen(buf), buf_size - strlen(buf),
998
                     " (%s)", profile);
999 82c0c4ba Michael Niedermayer
        if (enc->pix_fmt != PIX_FMT_NONE) {
1000 cf087595 Fabrice Bellard
            snprintf(buf + strlen(buf), buf_size - strlen(buf),
1001
                     ", %s",
1002 9bbffbb1 Fabrice Bellard
                     avcodec_get_pix_fmt_name(enc->pix_fmt));
1003 cf087595 Fabrice Bellard
        }
1004 de6d9b64 Fabrice Bellard
        if (enc->width) {
1005
            snprintf(buf + strlen(buf), buf_size - strlen(buf),
1006 21189011 Michael Niedermayer
                     ", %dx%d",
1007
                     enc->width, enc->height);
1008 7ee4dd02 Baptiste Coudurier
            if (enc->sample_aspect_ratio.num) {
1009 cbaf50f8 Baptiste Coudurier
                av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
1010
                          enc->width*enc->sample_aspect_ratio.num,
1011
                          enc->height*enc->sample_aspect_ratio.den,
1012
                          1024*1024);
1013
                snprintf(buf + strlen(buf), buf_size - strlen(buf),
1014
                         " [PAR %d:%d DAR %d:%d]",
1015
                         enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
1016
                         display_aspect_ratio.num, display_aspect_ratio.den);
1017 7ee4dd02 Baptiste Coudurier
            }
1018 a309073b Måns Rullgård
            if(av_log_get_level() >= AV_LOG_DEBUG){
1019 9ce6c138 Aurelien Jacobs
                int g= av_gcd(enc->time_base.num, enc->time_base.den);
1020 21189011 Michael Niedermayer
                snprintf(buf + strlen(buf), buf_size - strlen(buf),
1021
                     ", %d/%d",
1022
                     enc->time_base.num/g, enc->time_base.den/g);
1023
            }
1024 de6d9b64 Fabrice Bellard
        }
1025 4bfad535 Fabrice Bellard
        if (encode) {
1026
            snprintf(buf + strlen(buf), buf_size - strlen(buf),
1027
                     ", q=%d-%d", enc->qmin, enc->qmax);
1028
        }
1029 de6d9b64 Fabrice Bellard
        break;
1030 72415b2a Stefano Sabatini
    case AVMEDIA_TYPE_AUDIO:
1031 de6d9b64 Fabrice Bellard
        snprintf(buf, buf_size,
1032
                 "Audio: %s",
1033
                 codec_name);
1034 2a81f4bd Anssi Hannula
        if (profile)
1035
            snprintf(buf + strlen(buf), buf_size - strlen(buf),
1036
                     " (%s)", profile);
1037 de6d9b64 Fabrice Bellard
        if (enc->sample_rate) {
1038
            snprintf(buf + strlen(buf), buf_size - strlen(buf),
1039 0d72e7d0 Peter Ross
                     ", %d Hz", enc->sample_rate);
1040 de6d9b64 Fabrice Bellard
        }
1041 0d72e7d0 Peter Ross
        av_strlcat(buf, ", ", buf_size);
1042 63e8d976 Stefano Sabatini
        av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
1043 5d6e4c16 Stefano Sabatini
        if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
1044 9e82a113 Peter Ross
            snprintf(buf + strlen(buf), buf_size - strlen(buf),
1045 ba7d6e79 Stefano Sabatini
                     ", %s", av_get_sample_fmt_name(enc->sample_fmt));
1046 9e82a113 Peter Ross
        }
1047 de6d9b64 Fabrice Bellard
        break;
1048 72415b2a Stefano Sabatini
    case AVMEDIA_TYPE_DATA:
1049 985180a1 Fabrice Bellard
        snprintf(buf, buf_size, "Data: %s", codec_name);
1050 240c1657 Fabrice Bellard
        break;
1051 72415b2a Stefano Sabatini
    case AVMEDIA_TYPE_SUBTITLE:
1052 240c1657 Fabrice Bellard
        snprintf(buf, buf_size, "Subtitle: %s", codec_name);
1053 985180a1 Fabrice Bellard
        break;
1054 72415b2a Stefano Sabatini
    case AVMEDIA_TYPE_ATTACHMENT:
1055 f8d7c9d3 Evgeniy Stepanov
        snprintf(buf, buf_size, "Attachment: %s", codec_name);
1056
        break;
1057 de6d9b64 Fabrice Bellard
    default:
1058 9fe5a7b8 Michael Niedermayer
        snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
1059
        return;
1060 de6d9b64 Fabrice Bellard
    }
1061 4bfad535 Fabrice Bellard
    if (encode) {
1062
        if (enc->flags & CODEC_FLAG_PASS1)
1063
            snprintf(buf + strlen(buf), buf_size - strlen(buf),
1064
                     ", pass 1");
1065
        if (enc->flags & CODEC_FLAG_PASS2)
1066
            snprintf(buf + strlen(buf), buf_size - strlen(buf),
1067
                     ", pass 2");
1068
    }
1069 406aa93f Carl Eugen Hoyos
    bitrate = get_bit_rate(enc);
1070 a96b68b7 Fabrice Bellard
    if (bitrate != 0) {
1071 115329f1 Diego Biurrun
        snprintf(buf + strlen(buf), buf_size - strlen(buf),
1072 a96b68b7 Fabrice Bellard
                 ", %d kb/s", bitrate / 1000);
1073 de6d9b64 Fabrice Bellard
    }
1074
}
1075
1076 060ec0a8 Anssi Hannula
const char *av_get_profile_name(const AVCodec *codec, int profile)
1077
{
1078
    const AVProfile *p;
1079
    if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
1080
        return NULL;
1081
1082
    for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
1083
        if (p->profile == profile)
1084
            return p->name;
1085
1086
    return NULL;
1087
}
1088
1089 156e5023 Nick Kurshev
unsigned avcodec_version( void )
1090
{
1091
  return LIBAVCODEC_VERSION_INT;
1092
}
1093 cf087595 Fabrice Bellard
1094 41600690 Stefano Sabatini
const char *avcodec_configuration(void)
1095 c1736936 Diego Biurrun
{
1096 e528cdac Michael Niedermayer
    return FFMPEG_CONFIGURATION;
1097 c1736936 Diego Biurrun
}
1098
1099 41600690 Stefano Sabatini
const char *avcodec_license(void)
1100 c1736936 Diego Biurrun
{
1101
#define LICENSE_PREFIX "libavcodec license: "
1102 0cb88628 Michael Niedermayer
    return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
1103 c1736936 Diego Biurrun
}
1104
1105 de6d9b64 Fabrice Bellard
void avcodec_init(void)
1106
{
1107 5e534865 Diego Biurrun
    static int initialized = 0;
1108 0344cd0a Alex Beregszaszi
1109 5e534865 Diego Biurrun
    if (initialized != 0)
1110 bb270c08 Diego Biurrun
        return;
1111 5e534865 Diego Biurrun
    initialized = 1;
1112 0344cd0a Alex Beregszaszi
1113 486497e0 Måns Rullgård
    dsputil_static_init();
1114 de6d9b64 Fabrice Bellard
}
1115
1116 1c2a8c7f Michael Niedermayer
void avcodec_flush_buffers(AVCodecContext *avctx)
1117
{
1118 b38f008e Alexander Strange
    if(HAVE_PTHREADS && avctx->active_thread_type&FF_THREAD_FRAME)
1119
        ff_thread_flush(avctx);
1120 7a06ff14 Michael Niedermayer
    if(avctx->codec->flush)
1121
        avctx->codec->flush(avctx);
1122 1c2a8c7f Michael Niedermayer
}
1123
1124 b100eab8 Michael Niedermayer
void avcodec_default_free_buffers(AVCodecContext *s){
1125 d90cf87b Michael Niedermayer
    int i, j;
1126
1127
    if(s->internal_buffer==NULL) return;
1128 115329f1 Diego Biurrun
1129 ea09f691 Reimar Döffinger
    if (s->internal_buffer_count)
1130
        av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n", s->internal_buffer_count);
1131 d90cf87b Michael Niedermayer
    for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
1132
        InternalBuffer *buf= &((InternalBuffer*)s->internal_buffer)[i];
1133
        for(j=0; j<4; j++){
1134
            av_freep(&buf->base[j]);
1135
            buf->data[j]= NULL;
1136
        }
1137
    }
1138
    av_freep(&s->internal_buffer);
1139 115329f1 Diego Biurrun
1140 d90cf87b Michael Niedermayer
    s->internal_buffer_count=0;
1141
}
1142
1143 d8085ea7 Michael Niedermayer
char av_get_pict_type_char(int pict_type){
1144
    switch(pict_type){
1145 fbd4293d Aurelien Jacobs
    case FF_I_TYPE: return 'I';
1146
    case FF_P_TYPE: return 'P';
1147
    case FF_B_TYPE: return 'B';
1148
    case FF_S_TYPE: return 'S';
1149
    case FF_SI_TYPE:return 'i';
1150
    case FF_SP_TYPE:return 'p';
1151 48d533e6 Aurelien Jacobs
    case FF_BI_TYPE:return 'b';
1152 c06ccb13 Aurelien Jacobs
    default:        return '?';
1153 d8085ea7 Michael Niedermayer
    }
1154
}
1155
1156 ac3e1834 Baptiste Coudurier
int av_get_bits_per_sample(enum CodecID codec_id){
1157
    switch(codec_id){
1158 5da71469 Aurelien Jacobs
    case CODEC_ID_ADPCM_SBPRO_2:
1159 f1b163e0 Aurelien Jacobs
        return 2;
1160 5da71469 Aurelien Jacobs
    case CODEC_ID_ADPCM_SBPRO_3:
1161 f1b163e0 Aurelien Jacobs
        return 3;
1162 5da71469 Aurelien Jacobs
    case CODEC_ID_ADPCM_SBPRO_4:
1163 f1b163e0 Aurelien Jacobs
    case CODEC_ID_ADPCM_CT:
1164 07f2a575 Carl Eugen Hoyos
    case CODEC_ID_ADPCM_IMA_WAV:
1165 9df9b810 Daniel Verkamp
    case CODEC_ID_ADPCM_MS:
1166
    case CODEC_ID_ADPCM_YAMAHA:
1167 f1b163e0 Aurelien Jacobs
        return 4;
1168 029f966c Martin Storsjö
    case CODEC_ID_ADPCM_G722:
1169 ac3e1834 Baptiste Coudurier
    case CODEC_ID_PCM_ALAW:
1170
    case CODEC_ID_PCM_MULAW:
1171
    case CODEC_ID_PCM_S8:
1172
    case CODEC_ID_PCM_U8:
1173 9d49b8ff Peter Ross
    case CODEC_ID_PCM_ZORK:
1174 ac3e1834 Baptiste Coudurier
        return 8;
1175
    case CODEC_ID_PCM_S16BE:
1176
    case CODEC_ID_PCM_S16LE:
1177 725d86bf Aurelien Jacobs
    case CODEC_ID_PCM_S16LE_PLANAR:
1178 ac3e1834 Baptiste Coudurier
    case CODEC_ID_PCM_U16BE:
1179
    case CODEC_ID_PCM_U16LE:
1180
        return 16;
1181
    case CODEC_ID_PCM_S24DAUD:
1182
    case CODEC_ID_PCM_S24BE:
1183
    case CODEC_ID_PCM_S24LE:
1184
    case CODEC_ID_PCM_U24BE:
1185
    case CODEC_ID_PCM_U24LE:
1186
        return 24;
1187
    case CODEC_ID_PCM_S32BE:
1188
    case CODEC_ID_PCM_S32LE:
1189
    case CODEC_ID_PCM_U32BE:
1190
    case CODEC_ID_PCM_U32LE:
1191 aa29709e Peter Ross
    case CODEC_ID_PCM_F32BE:
1192 143a5d6f Peter Ross
    case CODEC_ID_PCM_F32LE:
1193 ac3e1834 Baptiste Coudurier
        return 32;
1194 143a5d6f Peter Ross
    case CODEC_ID_PCM_F64BE:
1195
    case CODEC_ID_PCM_F64LE:
1196
        return 64;
1197 ac3e1834 Baptiste Coudurier
    default:
1198
        return 0;
1199
    }
1200
}
1201
1202 6f84cd12 Stefano Sabatini
#if FF_API_OLD_SAMPLE_FMT
1203 5d6e4c16 Stefano Sabatini
int av_get_bits_per_sample_format(enum AVSampleFormat sample_fmt) {
1204 6f84cd12 Stefano Sabatini
    return av_get_bits_per_sample_fmt(sample_fmt);
1205 42c71907 David Conrad
}
1206 6f84cd12 Stefano Sabatini
#endif
1207 42c71907 David Conrad
1208 b250f9c6 Aurelien Jacobs
#if !HAVE_THREADS
1209 46027c72 Reimar Döffinger
int ff_thread_init(AVCodecContext *s){
1210 ca8ad847 Michael Niedermayer
    return -1;
1211
}
1212
#endif
1213 ad2b531d Måns Rullgård
1214
unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
1215
{
1216
    unsigned int n = 0;
1217
1218
    while(v >= 0xff) {
1219
        *s++ = 0xff;
1220
        v -= 0xff;
1221
        n++;
1222
    }
1223
    *s = v;
1224
    n++;
1225
    return n;
1226
}
1227 1005f542 Corey Hickey
1228 126b638e Stefano Sabatini
#if LIBAVCODEC_VERSION_MAJOR < 53
1229 7ffe76e5 Reinhard Tartler
#include "libavutil/parseutils.h"
1230 26ef3220 Stefano Sabatini
1231
int av_parse_video_frame_size(int *width_ptr, int *height_ptr, const char *str)
1232
{
1233 126b638e Stefano Sabatini
    return av_parse_video_size(width_ptr, height_ptr, str);
1234 26ef3220 Stefano Sabatini
}
1235
1236
int av_parse_video_frame_rate(AVRational *frame_rate, const char *arg)
1237
{
1238 126b638e Stefano Sabatini
    return av_parse_video_rate(frame_rate, arg);
1239 26ef3220 Stefano Sabatini
}
1240 126b638e Stefano Sabatini
#endif
1241 ea779d91 Justin Ruggles
1242 c46eeae2 Michael Niedermayer
int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
1243
    int i;
1244
    for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
1245
    return i;
1246
}
1247
1248 ce863d7f Ronald S. Bultje
void av_log_missing_feature(void *avc, const char *feature, int want_sample)
1249 ea779d91 Justin Ruggles
{
1250 2d2b5a14 Michael Niedermayer
    av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your FFmpeg "
1251 db323491 Janne Grunau
            "version to the newest one from Git. If the problem still "
1252 ea779d91 Justin Ruggles
            "occurs, it means that your file has a feature which has not "
1253
            "been implemented.", feature);
1254
    if(want_sample)
1255 ce863d7f Ronald S. Bultje
        av_log_ask_for_sample(avc, NULL);
1256 0ba39dd1 Kenan Gillet
    else
1257
        av_log(avc, AV_LOG_WARNING, "\n");
1258
}
1259
1260 ce863d7f Ronald S. Bultje
void av_log_ask_for_sample(void *avc, const char *msg)
1261 0ba39dd1 Kenan Gillet
{
1262
    if (msg)
1263
        av_log(avc, AV_LOG_WARNING, "%s ", msg);
1264
    av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
1265
            "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
1266
            "and contact the ffmpeg-devel mailing list.\n");
1267 ea779d91 Justin Ruggles
}
1268 c895618b Michael Niedermayer
1269
static AVHWAccel *first_hwaccel = NULL;
1270
1271
void av_register_hwaccel(AVHWAccel *hwaccel)
1272
{
1273
    AVHWAccel **p = &first_hwaccel;
1274
    while (*p)
1275
        p = &(*p)->next;
1276
    *p = hwaccel;
1277
    hwaccel->next = NULL;
1278
}
1279 414d9d7f Michael Niedermayer
1280
AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
1281
{
1282
    return hwaccel ? hwaccel->next : first_hwaccel;
1283
}
1284 6059f13c Michael Niedermayer
1285
AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
1286
{
1287
    AVHWAccel *hwaccel=NULL;
1288
1289
    while((hwaccel= av_hwaccel_next(hwaccel))){
1290
        if (   hwaccel->id      == codec_id
1291
            && hwaccel->pix_fmt == pix_fmt)
1292
            return hwaccel;
1293
    }
1294
    return NULL;
1295
}
1296 f988ce6c Andreas Öman
1297
int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
1298
{
1299
    if (ff_lockmgr_cb) {
1300
        if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
1301
            return -1;
1302
    }
1303
1304
    ff_lockmgr_cb = cb;
1305
1306
    if (ff_lockmgr_cb) {
1307
        if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
1308
            return -1;
1309
    }
1310
    return 0;
1311
}
1312 603a5f04 Francesco Lavra
1313
unsigned int ff_toupper4(unsigned int x)
1314
{
1315
    return     toupper( x     &0xFF)
1316 d375c104 Michael Niedermayer
    + (toupper((x>>8 )&0xFF)<<8 )
1317
    + (toupper((x>>16)&0xFF)<<16)
1318
    + (toupper((x>>24)&0xFF)<<24);
1319 603a5f04 Francesco Lavra
}
1320 b38f008e Alexander Strange
1321
#if !HAVE_PTHREADS
1322
1323
int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
1324
{
1325
    f->owner = avctx;
1326
    return avctx->get_buffer(avctx, f);
1327
}
1328
1329
void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
1330
{
1331
    f->owner->release_buffer(f->owner, f);
1332
}
1333
1334
void ff_thread_finish_setup(AVCodecContext *avctx)
1335
{
1336
}
1337
1338
void ff_thread_report_progress(AVFrame *f, int progress, int field)
1339
{
1340
}
1341
1342
void ff_thread_await_progress(AVFrame *f, int progress, int field)
1343
{
1344
}
1345
1346
#endif
1347 043d2ff2 Alexander Strange
1348
#if LIBAVCODEC_VERSION_MAJOR < 53
1349
1350
int avcodec_thread_init(AVCodecContext *s, int thread_count)
1351
{
1352 46027c72 Reimar Döffinger
    s->thread_count = thread_count;
1353
    return ff_thread_init(s);
1354 043d2ff2 Alexander Strange
}
1355
1356
void avcodec_thread_free(AVCodecContext *s)
1357
{
1358 ee5bb31f Mans Rullgard
#if HAVE_THREADS
1359 952f2315 Mans Rullgard
    ff_thread_free(s);
1360 ee5bb31f Mans Rullgard
#endif
1361 043d2ff2 Alexander Strange
}
1362
1363
#endif