Statistics
| Branch: | Revision:

ffmpeg / libavcodec / utils.c @ dc172ecc

History | View | Annotate | Download (39 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 5e4c7ca2 Ramiro Polla
int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
478 de6d9b64 Fabrice Bellard
{
479 ddebfb15 Michael Niedermayer
    int ret= -1;
480 115329f1 Diego Biurrun
481 f988ce6c Andreas Öman
    /* If there is a user-supplied mutex locking routine, call it. */
482
    if (ff_lockmgr_cb) {
483
        if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
484
            return -1;
485
    }
486
487 ddebfb15 Michael Niedermayer
    entangled_thread_counter++;
488
    if(entangled_thread_counter != 1){
489
        av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
490
        goto end;
491
    }
492 de6d9b64 Fabrice Bellard
493 9c87b8f7 Reimar Döffinger
    if(avctx->codec || !codec)
494 ddebfb15 Michael Niedermayer
        goto end;
495 09770af8 Michael Niedermayer
496 0edf8a7a Philip Gladstone
    if (codec->priv_data_size > 0) {
497 dc51a72b Michael Niedermayer
      if(!avctx->priv_data){
498 0edf8a7a Philip Gladstone
        avctx->priv_data = av_mallocz(codec->priv_data_size);
499 90f06cea Panagiotis Issaris
        if (!avctx->priv_data) {
500
            ret = AVERROR(ENOMEM);
501 ddebfb15 Michael Niedermayer
            goto end;
502 90f06cea Panagiotis Issaris
        }
503 dc51a72b Michael Niedermayer
        if(codec->priv_class){ //this can be droped once all user apps use   avcodec_get_context_defaults3()
504
            *(AVClass**)avctx->priv_data= codec->priv_class;
505
            av_opt_set_defaults(avctx->priv_data);
506
        }
507
      }
508 0edf8a7a Philip Gladstone
    } else {
509
        avctx->priv_data = NULL;
510
    }
511 21adafec Michael Niedermayer
512
    if(avctx->coded_width && avctx->coded_height)
513
        avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
514
    else if(avctx->width && avctx->height)
515
        avcodec_set_dimensions(avctx, avctx->width, avctx->height);
516
517 82eac2f3 Reimar Döffinger
    if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
518
        && (  av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
519
           || av_image_check_size(avctx->width,       avctx->height,       0, avctx) < 0)) {
520
        av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
521
        avcodec_set_dimensions(avctx, 0, 0);
522
    }
523
524 f19c58b4 Aurelien Jacobs
    /* if the decoder init function was already called previously,
525
       free the already allocated subtitle_header before overwriting it */
526
    if (codec->decode)
527
        av_freep(&avctx->subtitle_header);
528
529 fa77dd63 Peter Ross
#define SANE_NB_CHANNELS 128U
530 82eac2f3 Reimar Döffinger
    if (avctx->channels > SANE_NB_CHANNELS) {
531 7868349a Panagiotis Issaris
        ret = AVERROR(EINVAL);
532 2a9b5c9b Michael Niedermayer
        goto free_and_end;
533 0ecca7a4 Michael Niedermayer
    }
534
535 b5c85991 Michael Niedermayer
    avctx->codec = codec;
536 72415b2a Stefano Sabatini
    if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
537 681c180d Reimar Döffinger
        avctx->codec_id == CODEC_ID_NONE) {
538
        avctx->codec_type = codec->type;
539
        avctx->codec_id   = codec->id;
540
    }
541 e83c716e Aurelien Jacobs
    if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
542
                           && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
543 4c0dda2b Michael Niedermayer
        av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
544 2a9b5c9b Michael Niedermayer
        goto free_and_end;
545 4c0dda2b Michael Niedermayer
    }
546 b5c85991 Michael Niedermayer
    avctx->frame_number = 0;
547 b38f008e Alexander Strange
548
    if (HAVE_THREADS && !avctx->thread_opaque) {
549 46027c72 Reimar Döffinger
        ret = ff_thread_init(avctx);
550 b38f008e Alexander Strange
        if (ret < 0) {
551
            goto free_and_end;
552
        }
553
    }
554
555 527c91e3 Carl Eugen Hoyos
    if (avctx->codec->max_lowres < avctx->lowres) {
556
        av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
557
               avctx->codec->max_lowres);
558
        goto free_and_end;
559
    }
560 7ade06cc Reimar Döffinger
    if (avctx->codec->sample_fmts && avctx->codec->encode) {
561
        int i;
562
        for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++)
563
            if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
564
                break;
565
        if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
566
            av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
567
            goto free_and_end;
568
        }
569
    }
570 0fd0ef79 Carl Eugen Hoyos
571 76ad67ca Nicolas George
    avctx->pts_correction_num_faulty_pts =
572
    avctx->pts_correction_num_faulty_dts = 0;
573
    avctx->pts_correction_last_pts =
574
    avctx->pts_correction_last_dts = INT64_MIN;
575
576 b38f008e Alexander Strange
    if(avctx->codec->init && !(avctx->active_thread_type&FF_THREAD_FRAME)){
577 2de4f9eb Michael Niedermayer
        ret = avctx->codec->init(avctx);
578
        if (ret < 0) {
579 2a9b5c9b Michael Niedermayer
            goto free_and_end;
580 2de4f9eb Michael Niedermayer
        }
581 6e546aaa Michael Niedermayer
    }
582 d375c104 Michael Niedermayer
583 ddebfb15 Michael Niedermayer
    ret=0;
584
end:
585
    entangled_thread_counter--;
586 f988ce6c Andreas Öman
587
    /* Release any user-supplied mutex. */
588
    if (ff_lockmgr_cb) {
589
        (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
590
    }
591 ddebfb15 Michael Niedermayer
    return ret;
592 2a9b5c9b Michael Niedermayer
free_and_end:
593
    av_freep(&avctx->priv_data);
594
    avctx->codec= NULL;
595
    goto end;
596 de6d9b64 Fabrice Bellard
}
597
598 5e4c7ca2 Ramiro Polla
int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx, uint8_t *buf, int buf_size,
599 de6d9b64 Fabrice Bellard
                         const short *samples)
600
{
601 0ecca7a4 Michael Niedermayer
    if(buf_size < FF_MIN_BUFFER_SIZE && 0){
602 5286d11f Michel Bardiaux
        av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
603 0ecca7a4 Michael Niedermayer
        return -1;
604
    }
605 6f824977 Michael Niedermayer
    if((avctx->codec->capabilities & CODEC_CAP_DELAY) || samples){
606 8eb027c8 Baptiste Coudurier
        int ret = avctx->codec->encode(avctx, buf, buf_size, samples);
607 6f824977 Michael Niedermayer
        avctx->frame_number++;
608
        return ret;
609
    }else
610
        return 0;
611 de6d9b64 Fabrice Bellard
}
612
613 5e4c7ca2 Ramiro Polla
int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
614 492cd3a9 Michael Niedermayer
                         const AVFrame *pict)
615 de6d9b64 Fabrice Bellard
{
616 0ecca7a4 Michael Niedermayer
    if(buf_size < FF_MIN_BUFFER_SIZE){
617 5286d11f Michel Bardiaux
        av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
618 0ecca7a4 Michael Niedermayer
        return -1;
619
    }
620 e16f217c Stefano Sabatini
    if(av_image_check_size(avctx->width, avctx->height, 0, avctx))
621 0ecca7a4 Michael Niedermayer
        return -1;
622 6f824977 Michael Niedermayer
    if((avctx->codec->capabilities & CODEC_CAP_DELAY) || pict){
623 8eb027c8 Baptiste Coudurier
        int ret = avctx->codec->encode(avctx, buf, buf_size, pict);
624 6f824977 Michael Niedermayer
        avctx->frame_number++;
625 bb628dae Diego Biurrun
        emms_c(); //needed to avoid an emms_c() call before every return;
626 115329f1 Diego Biurrun
627 6f824977 Michael Niedermayer
        return ret;
628
    }else
629
        return 0;
630 de6d9b64 Fabrice Bellard
}
631
632 115329f1 Diego Biurrun
int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
633 240c1657 Fabrice Bellard
                            const AVSubtitle *sub)
634
{
635
    int ret;
636 9413db9e Björn Axelsson
    if(sub->start_display_time) {
637
        av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
638
        return -1;
639
    }
640 505aa6c9 Björn Axelsson
    if(sub->num_rects == 0 || !sub->rects)
641
        return -1;
642 8eb027c8 Baptiste Coudurier
    ret = avctx->codec->encode(avctx, buf, buf_size, sub);
643 240c1657 Fabrice Bellard
    avctx->frame_number++;
644
    return ret;
645
}
646
647 76ad67ca Nicolas George
/**
648
 * Attempt to guess proper monotonic timestamps for decoded video frames
649
 * which might have incorrect times. Input timestamps may wrap around, in
650
 * which case the output will as well.
651
 *
652
 * @param pts the pts field of the decoded AVPacket, as passed through
653
 * AVFrame.pkt_pts
654
 * @param dts the dts field of the decoded AVPacket
655
 * @return one of the input values, may be AV_NOPTS_VALUE
656
 */
657
static int64_t guess_correct_pts(AVCodecContext *ctx,
658
                                 int64_t reordered_pts, int64_t dts)
659
{
660
    int64_t pts = AV_NOPTS_VALUE;
661
662
    if (dts != AV_NOPTS_VALUE) {
663
        ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
664
        ctx->pts_correction_last_dts = dts;
665
    }
666
    if (reordered_pts != AV_NOPTS_VALUE) {
667
        ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
668
        ctx->pts_correction_last_pts = reordered_pts;
669
    }
670
    if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
671
       && reordered_pts != AV_NOPTS_VALUE)
672
        pts = reordered_pts;
673
    else
674
        pts = dts;
675
676
    return pts;
677
}
678
679
680 164bc38c Aurelien Jacobs
#if FF_API_VIDEO_OLD
681 5e4c7ca2 Ramiro Polla
int attribute_align_arg avcodec_decode_video(AVCodecContext *avctx, AVFrame *picture,
682 de6d9b64 Fabrice Bellard
                         int *got_picture_ptr,
683 2efa7fd1 Michael Niedermayer
                         const uint8_t *buf, int buf_size)
684 de6d9b64 Fabrice Bellard
{
685 7a00bbad Thilo Borgmann
    AVPacket avpkt;
686
    av_init_packet(&avpkt);
687
    avpkt.data = buf;
688
    avpkt.size = buf_size;
689 10f9ff9b Reimar Döffinger
    // HACK for CorePNG to decode as normal PNG by default
690
    avpkt.flags = AV_PKT_FLAG_KEY;
691 7a00bbad Thilo Borgmann
692
    return avcodec_decode_video2(avctx, picture, got_picture_ptr, &avpkt);
693
}
694
#endif
695
696
int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
697
                         int *got_picture_ptr,
698
                         AVPacket *avpkt)
699
{
700 de6d9b64 Fabrice Bellard
    int ret;
701 115329f1 Diego Biurrun
702 53db1cae Michael Niedermayer
    *got_picture_ptr= 0;
703 e16f217c Stefano Sabatini
    if((avctx->coded_width||avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
704 0ecca7a4 Michael Niedermayer
        return -1;
705 393cbb96 Michael Niedermayer
706
    avctx->pkt = avpkt;
707
708 b38f008e Alexander Strange
    if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type&FF_THREAD_FRAME)){
709
        if (HAVE_PTHREADS && avctx->active_thread_type&FF_THREAD_FRAME)
710
             ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
711
                                          avpkt);
712
        else {
713
            ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
714
                              avpkt);
715
            picture->pkt_dts= avpkt->dts;
716
        }
717 6bb925f4 Michael Niedermayer
718 bb628dae Diego Biurrun
        emms_c(); //needed to avoid an emms_c() call before every return;
719 115329f1 Diego Biurrun
720 1919feaf Michael Niedermayer
721 ad82bf9c Michael Niedermayer
        if (*got_picture_ptr){
722 934982c4 Michael Niedermayer
            avctx->frame_number++;
723 ad82bf9c Michael Niedermayer
            picture->best_effort_timestamp = guess_correct_pts(avctx,
724
                                                            picture->pkt_pts,
725
                                                            picture->pkt_dts);
726
        }
727 934982c4 Michael Niedermayer
    }else
728
        ret= 0;
729
730 de6d9b64 Fabrice Bellard
    return ret;
731
}
732
733 a377cfa6 Aurelien Jacobs
#if FF_API_AUDIO_OLD
734 5e4c7ca2 Ramiro Polla
int attribute_align_arg avcodec_decode_audio2(AVCodecContext *avctx, int16_t *samples,
735 de6d9b64 Fabrice Bellard
                         int *frame_size_ptr,
736 2efa7fd1 Michael Niedermayer
                         const uint8_t *buf, int buf_size)
737 de6d9b64 Fabrice Bellard
{
738 7a00bbad Thilo Borgmann
    AVPacket avpkt;
739
    av_init_packet(&avpkt);
740
    avpkt.data = buf;
741
    avpkt.size = buf_size;
742
743
    return avcodec_decode_audio3(avctx, samples, frame_size_ptr, &avpkt);
744
}
745
#endif
746
747
int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
748
                         int *frame_size_ptr,
749
                         AVPacket *avpkt)
750
{
751 de6d9b64 Fabrice Bellard
    int ret;
752
753 393cbb96 Michael Niedermayer
    avctx->pkt = avpkt;
754
755 7a00bbad Thilo Borgmann
    if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size){
756 9c856d62 Michael Niedermayer
        //FIXME remove the check below _after_ ensuring that all audio check that the available space is enough
757
        if(*frame_size_ptr < AVCODEC_MAX_AUDIO_FRAME_SIZE){
758
            av_log(avctx, AV_LOG_ERROR, "buffer smaller than AVCODEC_MAX_AUDIO_FRAME_SIZE\n");
759
            return -1;
760
        }
761
        if(*frame_size_ptr < FF_MIN_BUFFER_SIZE ||
762 81fc2f37 Kostya Shishkov
        *frame_size_ptr < avctx->channels * avctx->frame_size * sizeof(int16_t)){
763 9c856d62 Michael Niedermayer
            av_log(avctx, AV_LOG_ERROR, "buffer %d too small\n", *frame_size_ptr);
764
            return -1;
765
        }
766
767 7a00bbad Thilo Borgmann
        ret = avctx->codec->decode(avctx, samples, frame_size_ptr, avpkt);
768 fec9ccb7 Michael Niedermayer
        avctx->frame_number++;
769 ac66834c Michael Niedermayer
    }else{
770 fec9ccb7 Michael Niedermayer
        ret= 0;
771 ac66834c Michael Niedermayer
        *frame_size_ptr=0;
772
    }
773 de6d9b64 Fabrice Bellard
    return ret;
774
}
775
776 448ce925 Aurelien Jacobs
#if FF_API_SUBTITLE_OLD
777 240c1657 Fabrice Bellard
int avcodec_decode_subtitle(AVCodecContext *avctx, AVSubtitle *sub,
778
                            int *got_sub_ptr,
779
                            const uint8_t *buf, int buf_size)
780
{
781 7a00bbad Thilo Borgmann
    AVPacket avpkt;
782
    av_init_packet(&avpkt);
783
    avpkt.data = buf;
784
    avpkt.size = buf_size;
785
786
    return avcodec_decode_subtitle2(avctx, sub, got_sub_ptr, &avpkt);
787
}
788
#endif
789
790
int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
791
                            int *got_sub_ptr,
792
                            AVPacket *avpkt)
793
{
794 240c1657 Fabrice Bellard
    int ret;
795
796 393cbb96 Michael Niedermayer
    avctx->pkt = avpkt;
797 240c1657 Fabrice Bellard
    *got_sub_ptr = 0;
798 7a00bbad Thilo Borgmann
    ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
799 240c1657 Fabrice Bellard
    if (*got_sub_ptr)
800
        avctx->frame_number++;
801
    return ret;
802
}
803
804 e1d7c883 Reimar Döffinger
void avsubtitle_free(AVSubtitle *sub)
805
{
806
    int i;
807
808
    for (i = 0; i < sub->num_rects; i++)
809
    {
810 8b834ac5 Reimar Döffinger
        av_freep(&sub->rects[i]->pict.data[0]);
811
        av_freep(&sub->rects[i]->pict.data[1]);
812
        av_freep(&sub->rects[i]->pict.data[2]);
813
        av_freep(&sub->rects[i]->pict.data[3]);
814
        av_freep(&sub->rects[i]->text);
815
        av_freep(&sub->rects[i]->ass);
816
        av_freep(&sub->rects[i]);
817 e1d7c883 Reimar Döffinger
    }
818
819 8b834ac5 Reimar Döffinger
    av_freep(&sub->rects);
820 e1d7c883 Reimar Döffinger
821
    memset(sub, 0, sizeof(AVSubtitle));
822
}
823
824 0752cd39 Zuxy Meng
av_cold int avcodec_close(AVCodecContext *avctx)
825 de6d9b64 Fabrice Bellard
{
826 f988ce6c Andreas Öman
    /* If there is a user-supplied mutex locking routine, call it. */
827
    if (ff_lockmgr_cb) {
828
        if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
829
            return -1;
830
    }
831
832 ddebfb15 Michael Niedermayer
    entangled_thread_counter++;
833
    if(entangled_thread_counter != 1){
834
        av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
835
        entangled_thread_counter--;
836
        return -1;
837
    }
838
839 49fb20cb Aurelien Jacobs
    if (HAVE_THREADS && avctx->thread_opaque)
840 043d2ff2 Alexander Strange
        ff_thread_free(avctx);
841 dccda293 Michael Niedermayer
    if (avctx->codec && avctx->codec->close)
842 de6d9b64 Fabrice Bellard
        avctx->codec->close(avctx);
843 eea8c08f Michael Niedermayer
    avcodec_default_free_buffers(avctx);
844 b3ab3199 Reimar Döffinger
    avctx->coded_frame = NULL;
845 3123dd79 Fabrice Bellard
    av_freep(&avctx->priv_data);
846 77a670e7 Craig Thomasson
    if(avctx->codec && avctx->codec->encode)
847 c4f267ab Vitor Sessak
        av_freep(&avctx->extradata);
848 de6d9b64 Fabrice Bellard
    avctx->codec = NULL;
849 b38f008e Alexander Strange
    avctx->active_thread_type = 0;
850 ddebfb15 Michael Niedermayer
    entangled_thread_counter--;
851 f988ce6c Andreas Öman
852
    /* Release any user-supplied mutex. */
853
    if (ff_lockmgr_cb) {
854
        (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
855
    }
856 de6d9b64 Fabrice Bellard
    return 0;
857
}
858
859
AVCodec *avcodec_find_encoder(enum CodecID id)
860
{
861 93ebfeea Janne Grunau
    AVCodec *p, *experimental=NULL;
862 de6d9b64 Fabrice Bellard
    p = first_avcodec;
863
    while (p) {
864 93ebfeea Janne Grunau
        if (p->encode != NULL && p->id == id) {
865
            if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
866
                experimental = p;
867
            } else
868
                return p;
869
        }
870 de6d9b64 Fabrice Bellard
        p = p->next;
871
    }
872 93ebfeea Janne Grunau
    return experimental;
873 de6d9b64 Fabrice Bellard
}
874
875 98f3b098 Arpi
AVCodec *avcodec_find_encoder_by_name(const char *name)
876
{
877
    AVCodec *p;
878 fc228c90 Aurelien Jacobs
    if (!name)
879
        return NULL;
880 98f3b098 Arpi
    p = first_avcodec;
881
    while (p) {
882
        if (p->encode != NULL && strcmp(name,p->name) == 0)
883
            return p;
884
        p = p->next;
885
    }
886
    return NULL;
887
}
888
889 de6d9b64 Fabrice Bellard
AVCodec *avcodec_find_decoder(enum CodecID id)
890
{
891
    AVCodec *p;
892
    p = first_avcodec;
893
    while (p) {
894
        if (p->decode != NULL && p->id == id)
895
            return p;
896
        p = p->next;
897
    }
898
    return NULL;
899
}
900
901
AVCodec *avcodec_find_decoder_by_name(const char *name)
902
{
903
    AVCodec *p;
904 fc228c90 Aurelien Jacobs
    if (!name)
905
        return NULL;
906 de6d9b64 Fabrice Bellard
    p = first_avcodec;
907
    while (p) {
908
        if (p->decode != NULL && strcmp(name,p->name) == 0)
909
            return p;
910
        p = p->next;
911
    }
912
    return NULL;
913
}
914
915 406aa93f Carl Eugen Hoyos
static int get_bit_rate(AVCodecContext *ctx)
916 ce34ff6b Robert Krüger
{
917
    int bit_rate;
918
    int bits_per_sample;
919
920
    switch(ctx->codec_type) {
921 72415b2a Stefano Sabatini
    case AVMEDIA_TYPE_VIDEO:
922 4563cf24 Stefano Sabatini
    case AVMEDIA_TYPE_DATA:
923
    case AVMEDIA_TYPE_SUBTITLE:
924
    case AVMEDIA_TYPE_ATTACHMENT:
925 ce34ff6b Robert Krüger
        bit_rate = ctx->bit_rate;
926
        break;
927 72415b2a Stefano Sabatini
    case AVMEDIA_TYPE_AUDIO:
928 ce34ff6b Robert Krüger
        bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
929
        bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
930
        break;
931
    default:
932
        bit_rate = 0;
933
        break;
934
    }
935
    return bit_rate;
936
}
937
938 7e566bbe Stefano Sabatini
size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
939
{
940
    int i, len, ret = 0;
941
942
    for (i = 0; i < 4; i++) {
943
        len = snprintf(buf, buf_size,
944
                       isprint(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
945
        buf      += len;
946
        buf_size  = buf_size > len ? buf_size - len : 0;
947
        ret      += len;
948
        codec_tag>>=8;
949
    }
950
    return ret;
951
}
952
953 de6d9b64 Fabrice Bellard
void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
954
{
955
    const char *codec_name;
956 2a81f4bd Anssi Hannula
    const char *profile = NULL;
957 de6d9b64 Fabrice Bellard
    AVCodec *p;
958
    char buf1[32];
959 a96b68b7 Fabrice Bellard
    int bitrate;
960 59771f71 Michel Bardiaux
    AVRational display_aspect_ratio;
961 de6d9b64 Fabrice Bellard
962
    if (encode)
963
        p = avcodec_find_encoder(enc->codec_id);
964
    else
965
        p = avcodec_find_decoder(enc->codec_id);
966
967
    if (p) {
968
        codec_name = p->name;
969 2a81f4bd Anssi Hannula
        profile = av_get_profile_name(p, enc->profile);
970 985180a1 Fabrice Bellard
    } else if (enc->codec_id == CODEC_ID_MPEG2TS) {
971
        /* fake mpeg2 transport stream codec (currently not
972
           registered) */
973
        codec_name = "mpeg2ts";
974 de6d9b64 Fabrice Bellard
    } else if (enc->codec_name[0] != '\0') {
975
        codec_name = enc->codec_name;
976
    } else {
977
        /* output avi tags */
978 ab0b5378 Stefano Sabatini
        char tag_buf[32];
979
        av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
980
        snprintf(buf1, sizeof(buf1), "%s / 0x%04X", tag_buf, enc->codec_tag);
981 de6d9b64 Fabrice Bellard
        codec_name = buf1;
982
    }
983
984
    switch(enc->codec_type) {
985 72415b2a Stefano Sabatini
    case AVMEDIA_TYPE_VIDEO:
986 de6d9b64 Fabrice Bellard
        snprintf(buf, buf_size,
987
                 "Video: %s%s",
988 7d1c3fc1 Michael Niedermayer
                 codec_name, enc->mb_decision ? " (hq)" : "");
989 2a81f4bd Anssi Hannula
        if (profile)
990
            snprintf(buf + strlen(buf), buf_size - strlen(buf),
991
                     " (%s)", profile);
992 82c0c4ba Michael Niedermayer
        if (enc->pix_fmt != PIX_FMT_NONE) {
993 cf087595 Fabrice Bellard
            snprintf(buf + strlen(buf), buf_size - strlen(buf),
994
                     ", %s",
995 9bbffbb1 Fabrice Bellard
                     avcodec_get_pix_fmt_name(enc->pix_fmt));
996 cf087595 Fabrice Bellard
        }
997 de6d9b64 Fabrice Bellard
        if (enc->width) {
998
            snprintf(buf + strlen(buf), buf_size - strlen(buf),
999 21189011 Michael Niedermayer
                     ", %dx%d",
1000
                     enc->width, enc->height);
1001 7ee4dd02 Baptiste Coudurier
            if (enc->sample_aspect_ratio.num) {
1002 cbaf50f8 Baptiste Coudurier
                av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
1003
                          enc->width*enc->sample_aspect_ratio.num,
1004
                          enc->height*enc->sample_aspect_ratio.den,
1005
                          1024*1024);
1006
                snprintf(buf + strlen(buf), buf_size - strlen(buf),
1007
                         " [PAR %d:%d DAR %d:%d]",
1008
                         enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
1009
                         display_aspect_ratio.num, display_aspect_ratio.den);
1010 7ee4dd02 Baptiste Coudurier
            }
1011 a309073b Måns Rullgård
            if(av_log_get_level() >= AV_LOG_DEBUG){
1012 9ce6c138 Aurelien Jacobs
                int g= av_gcd(enc->time_base.num, enc->time_base.den);
1013 21189011 Michael Niedermayer
                snprintf(buf + strlen(buf), buf_size - strlen(buf),
1014
                     ", %d/%d",
1015
                     enc->time_base.num/g, enc->time_base.den/g);
1016
            }
1017 de6d9b64 Fabrice Bellard
        }
1018 4bfad535 Fabrice Bellard
        if (encode) {
1019
            snprintf(buf + strlen(buf), buf_size - strlen(buf),
1020
                     ", q=%d-%d", enc->qmin, enc->qmax);
1021
        }
1022 de6d9b64 Fabrice Bellard
        break;
1023 72415b2a Stefano Sabatini
    case AVMEDIA_TYPE_AUDIO:
1024 de6d9b64 Fabrice Bellard
        snprintf(buf, buf_size,
1025
                 "Audio: %s",
1026
                 codec_name);
1027 2a81f4bd Anssi Hannula
        if (profile)
1028
            snprintf(buf + strlen(buf), buf_size - strlen(buf),
1029
                     " (%s)", profile);
1030 de6d9b64 Fabrice Bellard
        if (enc->sample_rate) {
1031
            snprintf(buf + strlen(buf), buf_size - strlen(buf),
1032 0d72e7d0 Peter Ross
                     ", %d Hz", enc->sample_rate);
1033 de6d9b64 Fabrice Bellard
        }
1034 0d72e7d0 Peter Ross
        av_strlcat(buf, ", ", buf_size);
1035 63e8d976 Stefano Sabatini
        av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
1036 5d6e4c16 Stefano Sabatini
        if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
1037 9e82a113 Peter Ross
            snprintf(buf + strlen(buf), buf_size - strlen(buf),
1038 ba7d6e79 Stefano Sabatini
                     ", %s", av_get_sample_fmt_name(enc->sample_fmt));
1039 9e82a113 Peter Ross
        }
1040 de6d9b64 Fabrice Bellard
        break;
1041 72415b2a Stefano Sabatini
    case AVMEDIA_TYPE_DATA:
1042 985180a1 Fabrice Bellard
        snprintf(buf, buf_size, "Data: %s", codec_name);
1043 240c1657 Fabrice Bellard
        break;
1044 72415b2a Stefano Sabatini
    case AVMEDIA_TYPE_SUBTITLE:
1045 240c1657 Fabrice Bellard
        snprintf(buf, buf_size, "Subtitle: %s", codec_name);
1046 985180a1 Fabrice Bellard
        break;
1047 72415b2a Stefano Sabatini
    case AVMEDIA_TYPE_ATTACHMENT:
1048 f8d7c9d3 Evgeniy Stepanov
        snprintf(buf, buf_size, "Attachment: %s", codec_name);
1049
        break;
1050 de6d9b64 Fabrice Bellard
    default:
1051 9fe5a7b8 Michael Niedermayer
        snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
1052
        return;
1053 de6d9b64 Fabrice Bellard
    }
1054 4bfad535 Fabrice Bellard
    if (encode) {
1055
        if (enc->flags & CODEC_FLAG_PASS1)
1056
            snprintf(buf + strlen(buf), buf_size - strlen(buf),
1057
                     ", pass 1");
1058
        if (enc->flags & CODEC_FLAG_PASS2)
1059
            snprintf(buf + strlen(buf), buf_size - strlen(buf),
1060
                     ", pass 2");
1061
    }
1062 406aa93f Carl Eugen Hoyos
    bitrate = get_bit_rate(enc);
1063 a96b68b7 Fabrice Bellard
    if (bitrate != 0) {
1064 115329f1 Diego Biurrun
        snprintf(buf + strlen(buf), buf_size - strlen(buf),
1065 a96b68b7 Fabrice Bellard
                 ", %d kb/s", bitrate / 1000);
1066 de6d9b64 Fabrice Bellard
    }
1067
}
1068
1069 060ec0a8 Anssi Hannula
const char *av_get_profile_name(const AVCodec *codec, int profile)
1070
{
1071
    const AVProfile *p;
1072
    if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
1073
        return NULL;
1074
1075
    for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
1076
        if (p->profile == profile)
1077
            return p->name;
1078
1079
    return NULL;
1080
}
1081
1082 156e5023 Nick Kurshev
unsigned avcodec_version( void )
1083
{
1084
  return LIBAVCODEC_VERSION_INT;
1085
}
1086 cf087595 Fabrice Bellard
1087 41600690 Stefano Sabatini
const char *avcodec_configuration(void)
1088 c1736936 Diego Biurrun
{
1089 e528cdac Michael Niedermayer
    return FFMPEG_CONFIGURATION;
1090 c1736936 Diego Biurrun
}
1091
1092 41600690 Stefano Sabatini
const char *avcodec_license(void)
1093 c1736936 Diego Biurrun
{
1094
#define LICENSE_PREFIX "libavcodec license: "
1095 0cb88628 Michael Niedermayer
    return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
1096 c1736936 Diego Biurrun
}
1097
1098 de6d9b64 Fabrice Bellard
void avcodec_init(void)
1099
{
1100 5e534865 Diego Biurrun
    static int initialized = 0;
1101 0344cd0a Alex Beregszaszi
1102 5e534865 Diego Biurrun
    if (initialized != 0)
1103 bb270c08 Diego Biurrun
        return;
1104 5e534865 Diego Biurrun
    initialized = 1;
1105 0344cd0a Alex Beregszaszi
1106 486497e0 Måns Rullgård
    dsputil_static_init();
1107 de6d9b64 Fabrice Bellard
}
1108
1109 1c2a8c7f Michael Niedermayer
void avcodec_flush_buffers(AVCodecContext *avctx)
1110
{
1111 b38f008e Alexander Strange
    if(HAVE_PTHREADS && avctx->active_thread_type&FF_THREAD_FRAME)
1112
        ff_thread_flush(avctx);
1113 7a06ff14 Michael Niedermayer
    if(avctx->codec->flush)
1114
        avctx->codec->flush(avctx);
1115 1c2a8c7f Michael Niedermayer
}
1116
1117 b100eab8 Michael Niedermayer
void avcodec_default_free_buffers(AVCodecContext *s){
1118 d90cf87b Michael Niedermayer
    int i, j;
1119
1120
    if(s->internal_buffer==NULL) return;
1121 115329f1 Diego Biurrun
1122 ea09f691 Reimar Döffinger
    if (s->internal_buffer_count)
1123
        av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n", s->internal_buffer_count);
1124 d90cf87b Michael Niedermayer
    for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
1125
        InternalBuffer *buf= &((InternalBuffer*)s->internal_buffer)[i];
1126
        for(j=0; j<4; j++){
1127
            av_freep(&buf->base[j]);
1128
            buf->data[j]= NULL;
1129
        }
1130
    }
1131
    av_freep(&s->internal_buffer);
1132 115329f1 Diego Biurrun
1133 d90cf87b Michael Niedermayer
    s->internal_buffer_count=0;
1134
}
1135
1136 d8085ea7 Michael Niedermayer
char av_get_pict_type_char(int pict_type){
1137
    switch(pict_type){
1138 fbd4293d Aurelien Jacobs
    case FF_I_TYPE: return 'I';
1139
    case FF_P_TYPE: return 'P';
1140
    case FF_B_TYPE: return 'B';
1141
    case FF_S_TYPE: return 'S';
1142
    case FF_SI_TYPE:return 'i';
1143
    case FF_SP_TYPE:return 'p';
1144 48d533e6 Aurelien Jacobs
    case FF_BI_TYPE:return 'b';
1145 c06ccb13 Aurelien Jacobs
    default:        return '?';
1146 d8085ea7 Michael Niedermayer
    }
1147
}
1148
1149 ac3e1834 Baptiste Coudurier
int av_get_bits_per_sample(enum CodecID codec_id){
1150
    switch(codec_id){
1151 5da71469 Aurelien Jacobs
    case CODEC_ID_ADPCM_SBPRO_2:
1152 f1b163e0 Aurelien Jacobs
        return 2;
1153 5da71469 Aurelien Jacobs
    case CODEC_ID_ADPCM_SBPRO_3:
1154 f1b163e0 Aurelien Jacobs
        return 3;
1155 5da71469 Aurelien Jacobs
    case CODEC_ID_ADPCM_SBPRO_4:
1156 f1b163e0 Aurelien Jacobs
    case CODEC_ID_ADPCM_CT:
1157 07f2a575 Carl Eugen Hoyos
    case CODEC_ID_ADPCM_IMA_WAV:
1158 9df9b810 Daniel Verkamp
    case CODEC_ID_ADPCM_MS:
1159
    case CODEC_ID_ADPCM_YAMAHA:
1160 f1b163e0 Aurelien Jacobs
        return 4;
1161 ac3e1834 Baptiste Coudurier
    case CODEC_ID_PCM_ALAW:
1162
    case CODEC_ID_PCM_MULAW:
1163
    case CODEC_ID_PCM_S8:
1164
    case CODEC_ID_PCM_U8:
1165 9d49b8ff Peter Ross
    case CODEC_ID_PCM_ZORK:
1166 ac3e1834 Baptiste Coudurier
        return 8;
1167
    case CODEC_ID_PCM_S16BE:
1168
    case CODEC_ID_PCM_S16LE:
1169 725d86bf Aurelien Jacobs
    case CODEC_ID_PCM_S16LE_PLANAR:
1170 ac3e1834 Baptiste Coudurier
    case CODEC_ID_PCM_U16BE:
1171
    case CODEC_ID_PCM_U16LE:
1172
        return 16;
1173
    case CODEC_ID_PCM_S24DAUD:
1174
    case CODEC_ID_PCM_S24BE:
1175
    case CODEC_ID_PCM_S24LE:
1176
    case CODEC_ID_PCM_U24BE:
1177
    case CODEC_ID_PCM_U24LE:
1178
        return 24;
1179
    case CODEC_ID_PCM_S32BE:
1180
    case CODEC_ID_PCM_S32LE:
1181
    case CODEC_ID_PCM_U32BE:
1182
    case CODEC_ID_PCM_U32LE:
1183 aa29709e Peter Ross
    case CODEC_ID_PCM_F32BE:
1184 143a5d6f Peter Ross
    case CODEC_ID_PCM_F32LE:
1185 ac3e1834 Baptiste Coudurier
        return 32;
1186 143a5d6f Peter Ross
    case CODEC_ID_PCM_F64BE:
1187
    case CODEC_ID_PCM_F64LE:
1188
        return 64;
1189 ac3e1834 Baptiste Coudurier
    default:
1190
        return 0;
1191
    }
1192
}
1193
1194 6f84cd12 Stefano Sabatini
#if FF_API_OLD_SAMPLE_FMT
1195 5d6e4c16 Stefano Sabatini
int av_get_bits_per_sample_format(enum AVSampleFormat sample_fmt) {
1196 6f84cd12 Stefano Sabatini
    return av_get_bits_per_sample_fmt(sample_fmt);
1197 42c71907 David Conrad
}
1198 6f84cd12 Stefano Sabatini
#endif
1199 42c71907 David Conrad
1200 b250f9c6 Aurelien Jacobs
#if !HAVE_THREADS
1201 46027c72 Reimar Döffinger
int ff_thread_init(AVCodecContext *s){
1202 ca8ad847 Michael Niedermayer
    return -1;
1203
}
1204
#endif
1205 ad2b531d Måns Rullgård
1206
unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
1207
{
1208
    unsigned int n = 0;
1209
1210
    while(v >= 0xff) {
1211
        *s++ = 0xff;
1212
        v -= 0xff;
1213
        n++;
1214
    }
1215
    *s = v;
1216
    n++;
1217
    return n;
1218
}
1219 1005f542 Corey Hickey
1220 126b638e Stefano Sabatini
#if LIBAVCODEC_VERSION_MAJOR < 53
1221 7ffe76e5 Reinhard Tartler
#include "libavutil/parseutils.h"
1222 26ef3220 Stefano Sabatini
1223
int av_parse_video_frame_size(int *width_ptr, int *height_ptr, const char *str)
1224
{
1225 126b638e Stefano Sabatini
    return av_parse_video_size(width_ptr, height_ptr, str);
1226 26ef3220 Stefano Sabatini
}
1227
1228
int av_parse_video_frame_rate(AVRational *frame_rate, const char *arg)
1229
{
1230 126b638e Stefano Sabatini
    return av_parse_video_rate(frame_rate, arg);
1231 26ef3220 Stefano Sabatini
}
1232 126b638e Stefano Sabatini
#endif
1233 ea779d91 Justin Ruggles
1234 c46eeae2 Michael Niedermayer
int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
1235
    int i;
1236
    for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
1237
    return i;
1238
}
1239
1240 ce863d7f Ronald S. Bultje
void av_log_missing_feature(void *avc, const char *feature, int want_sample)
1241 ea779d91 Justin Ruggles
{
1242
    av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your FFmpeg "
1243 db323491 Janne Grunau
            "version to the newest one from Git. If the problem still "
1244 ea779d91 Justin Ruggles
            "occurs, it means that your file has a feature which has not "
1245
            "been implemented.", feature);
1246
    if(want_sample)
1247 ce863d7f Ronald S. Bultje
        av_log_ask_for_sample(avc, NULL);
1248 0ba39dd1 Kenan Gillet
    else
1249
        av_log(avc, AV_LOG_WARNING, "\n");
1250
}
1251
1252 ce863d7f Ronald S. Bultje
void av_log_ask_for_sample(void *avc, const char *msg)
1253 0ba39dd1 Kenan Gillet
{
1254
    if (msg)
1255
        av_log(avc, AV_LOG_WARNING, "%s ", msg);
1256
    av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
1257
            "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
1258
            "and contact the ffmpeg-devel mailing list.\n");
1259 ea779d91 Justin Ruggles
}
1260 c895618b Michael Niedermayer
1261
static AVHWAccel *first_hwaccel = NULL;
1262
1263
void av_register_hwaccel(AVHWAccel *hwaccel)
1264
{
1265
    AVHWAccel **p = &first_hwaccel;
1266
    while (*p)
1267
        p = &(*p)->next;
1268
    *p = hwaccel;
1269
    hwaccel->next = NULL;
1270
}
1271 414d9d7f Michael Niedermayer
1272
AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
1273
{
1274
    return hwaccel ? hwaccel->next : first_hwaccel;
1275
}
1276 6059f13c Michael Niedermayer
1277
AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
1278
{
1279
    AVHWAccel *hwaccel=NULL;
1280
1281
    while((hwaccel= av_hwaccel_next(hwaccel))){
1282
        if (   hwaccel->id      == codec_id
1283
            && hwaccel->pix_fmt == pix_fmt)
1284
            return hwaccel;
1285
    }
1286
    return NULL;
1287
}
1288 f988ce6c Andreas Öman
1289
int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
1290
{
1291
    if (ff_lockmgr_cb) {
1292
        if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
1293
            return -1;
1294
    }
1295
1296
    ff_lockmgr_cb = cb;
1297
1298
    if (ff_lockmgr_cb) {
1299
        if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
1300
            return -1;
1301
    }
1302
    return 0;
1303
}
1304 603a5f04 Francesco Lavra
1305
unsigned int ff_toupper4(unsigned int x)
1306
{
1307
    return     toupper( x     &0xFF)
1308 d375c104 Michael Niedermayer
    + (toupper((x>>8 )&0xFF)<<8 )
1309
    + (toupper((x>>16)&0xFF)<<16)
1310
    + (toupper((x>>24)&0xFF)<<24);
1311 603a5f04 Francesco Lavra
}
1312 b38f008e Alexander Strange
1313
#if !HAVE_PTHREADS
1314
1315
int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
1316
{
1317
    f->owner = avctx;
1318
    return avctx->get_buffer(avctx, f);
1319
}
1320
1321
void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
1322
{
1323
    f->owner->release_buffer(f->owner, f);
1324
}
1325
1326
void ff_thread_finish_setup(AVCodecContext *avctx)
1327
{
1328
}
1329
1330
void ff_thread_report_progress(AVFrame *f, int progress, int field)
1331
{
1332
}
1333
1334
void ff_thread_await_progress(AVFrame *f, int progress, int field)
1335
{
1336
}
1337
1338
#endif
1339 043d2ff2 Alexander Strange
1340
#if LIBAVCODEC_VERSION_MAJOR < 53
1341
1342
int avcodec_thread_init(AVCodecContext *s, int thread_count)
1343
{
1344 46027c72 Reimar Döffinger
    s->thread_count = thread_count;
1345
    return ff_thread_init(s);
1346 043d2ff2 Alexander Strange
}
1347
1348
void avcodec_thread_free(AVCodecContext *s)
1349
{
1350 ee5bb31f Mans Rullgard
#if HAVE_THREADS
1351 952f2315 Mans Rullgard
    ff_thread_free(s);
1352 ee5bb31f Mans Rullgard
#endif
1353 043d2ff2 Alexander Strange
}
1354
1355
#endif