Statistics
| Branch: | Revision:

ffmpeg / libavcodec / utils.c @ 0fd0ef79

History | View | Annotate | Download (36.5 KB)

1
/*
2
 * utils for libavcodec
3
 * Copyright (c) 2001 Fabrice Bellard
4
 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
5
 *
6
 * This file is part of FFmpeg.
7
 *
8
 * FFmpeg is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU Lesser General Public
10
 * License as published by the Free Software Foundation; either
11
 * version 2.1 of the License, or (at your option) any later version.
12
 *
13
 * FFmpeg is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16
 * Lesser General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public
19
 * License along with FFmpeg; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21
 */
22

    
23
/**
24
 * @file
25
 * utils.
26
 */
27

    
28
#include "libavutil/avstring.h"
29
#include "libavutil/integer.h"
30
#include "libavutil/crc.h"
31
#include "libavutil/pixdesc.h"
32
#include "avcodec.h"
33
#include "dsputil.h"
34
#include "opt.h"
35
#include "imgconvert.h"
36
#include "audioconvert.h"
37
#include "internal.h"
38
#include <stdlib.h>
39
#include <stdarg.h>
40
#include <limits.h>
41
#include <float.h>
42

    
43
static int volatile entangled_thread_counter=0;
44
int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op);
45
static void *codec_mutex;
46

    
47
void *av_fast_realloc(void *ptr, unsigned int *size, unsigned int min_size)
48
{
49
    if(min_size < *size)
50
        return ptr;
51

    
52
    *size= FFMAX(17*min_size/16 + 32, min_size);
53

    
54
    ptr= av_realloc(ptr, *size);
55
    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
56
        *size= 0;
57

    
58
    return ptr;
59
}
60

    
61
void av_fast_malloc(void *ptr, unsigned int *size, unsigned int min_size)
62
{
63
    void **p = ptr;
64
    if (min_size < *size)
65
        return;
66
    *size= FFMAX(17*min_size/16 + 32, min_size);
67
    av_free(*p);
68
    *p = av_malloc(*size);
69
    if (!*p) *size = 0;
70
}
71

    
72
/* encoder management */
73
static AVCodec *first_avcodec = NULL;
74

    
75
AVCodec *av_codec_next(AVCodec *c){
76
    if(c) return c->next;
77
    else  return first_avcodec;
78
}
79

    
80
void avcodec_register(AVCodec *codec)
81
{
82
    AVCodec **p;
83
    avcodec_init();
84
    p = &first_avcodec;
85
    while (*p != NULL) p = &(*p)->next;
86
    *p = codec;
87
    codec->next = NULL;
88
}
89

    
90
#if LIBAVCODEC_VERSION_MAJOR < 53
91
void register_avcodec(AVCodec *codec)
92
{
93
    avcodec_register(codec);
94
}
95
#endif
96

    
97
unsigned avcodec_get_edge_width(void)
98
{
99
    return EDGE_WIDTH;
100
}
101

    
102
void avcodec_set_dimensions(AVCodecContext *s, int width, int height){
103
    s->coded_width = width;
104
    s->coded_height= height;
105
    s->width = -((-width )>>s->lowres);
106
    s->height= -((-height)>>s->lowres);
107
}
108

    
109
typedef struct InternalBuffer{
110
    int last_pic_num;
111
    uint8_t *base[4];
112
    uint8_t *data[4];
113
    int linesize[4];
114
    int width, height;
115
    enum PixelFormat pix_fmt;
116
}InternalBuffer;
117

    
118
#define INTERNAL_BUFFER_SIZE 32
119

    
120
void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height, int linesize_align[4]){
121
    int w_align= 1;
122
    int h_align= 1;
123

    
124
    switch(s->pix_fmt){
125
    case PIX_FMT_YUV420P:
126
    case PIX_FMT_YUYV422:
127
    case PIX_FMT_UYVY422:
128
    case PIX_FMT_YUV422P:
129
    case PIX_FMT_YUV440P:
130
    case PIX_FMT_YUV444P:
131
    case PIX_FMT_GRAY8:
132
    case PIX_FMT_GRAY16BE:
133
    case PIX_FMT_GRAY16LE:
134
    case PIX_FMT_YUVJ420P:
135
    case PIX_FMT_YUVJ422P:
136
    case PIX_FMT_YUVJ440P:
137
    case PIX_FMT_YUVJ444P:
138
    case PIX_FMT_YUVA420P:
139
        w_align= 16; //FIXME check for non mpeg style codecs and use less alignment
140
        h_align= 16;
141
        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)
142
            h_align= 32; // interlaced is rounded up to 2 MBs
143
        break;
144
    case PIX_FMT_YUV411P:
145
    case PIX_FMT_UYYVYY411:
146
        w_align=32;
147
        h_align=8;
148
        break;
149
    case PIX_FMT_YUV410P:
150
        if(s->codec_id == CODEC_ID_SVQ1){
151
            w_align=64;
152
            h_align=64;
153
        }
154
    case PIX_FMT_RGB555:
155
        if(s->codec_id == CODEC_ID_RPZA){
156
            w_align=4;
157
            h_align=4;
158
        }
159
    case PIX_FMT_PAL8:
160
    case PIX_FMT_BGR8:
161
    case PIX_FMT_RGB8:
162
        if(s->codec_id == CODEC_ID_SMC){
163
            w_align=4;
164
            h_align=4;
165
        }
166
        break;
167
    case PIX_FMT_BGR24:
168
        if((s->codec_id == CODEC_ID_MSZH) || (s->codec_id == CODEC_ID_ZLIB)){
169
            w_align=4;
170
            h_align=4;
171
        }
172
        break;
173
    default:
174
        w_align= 1;
175
        h_align= 1;
176
        break;
177
    }
178

    
179
    *width = FFALIGN(*width , w_align);
180
    *height= FFALIGN(*height, h_align);
181
    if(s->codec_id == CODEC_ID_H264)
182
        *height+=2; // some of the optimized chroma MC reads one line too much
183

    
184
    linesize_align[0] =
185
    linesize_align[1] =
186
    linesize_align[2] =
187
    linesize_align[3] = STRIDE_ALIGN;
188
//STRIDE_ALIGN is 8 for SSE* but this does not work for SVQ1 chroma planes
189
//we could change STRIDE_ALIGN to 16 for x86/sse but it would increase the
190
//picture size unneccessarily in some cases. The solution here is not
191
//pretty and better ideas are welcome!
192
#if HAVE_MMX
193
    if(s->codec_id == CODEC_ID_SVQ1 || s->codec_id == CODEC_ID_VP5 ||
194
       s->codec_id == CODEC_ID_VP6 || s->codec_id == CODEC_ID_VP6F ||
195
       s->codec_id == CODEC_ID_VP6A) {
196
        linesize_align[0] =
197
        linesize_align[1] =
198
        linesize_align[2] = 16;
199
    }
200
#endif
201
}
202

    
203
void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height){
204
    int chroma_shift = av_pix_fmt_descriptors[s->pix_fmt].log2_chroma_w;
205
    int linesize_align[4];
206
    int align;
207
    avcodec_align_dimensions2(s, width, height, linesize_align);
208
    align = FFMAX(linesize_align[0], linesize_align[3]);
209
    linesize_align[1] <<= chroma_shift;
210
    linesize_align[2] <<= chroma_shift;
211
    align = FFMAX3(align, linesize_align[1], linesize_align[2]);
212
    *width=FFALIGN(*width, align);
213
}
214

    
215
int avcodec_check_dimensions(void *av_log_ctx, unsigned int w, unsigned int h){
216
    if((int)w>0 && (int)h>0 && (w+128)*(uint64_t)(h+128) < INT_MAX/8)
217
        return 0;
218

    
219
    av_log(av_log_ctx, AV_LOG_ERROR, "picture size invalid (%ux%u)\n", w, h);
220
    return AVERROR(EINVAL);
221
}
222

    
223
int avcodec_default_get_buffer(AVCodecContext *s, AVFrame *pic){
224
    int i;
225
    int w= s->width;
226
    int h= s->height;
227
    InternalBuffer *buf;
228
    int *picture_number;
229

    
230
    if(pic->data[0]!=NULL) {
231
        av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
232
        return -1;
233
    }
234
    if(s->internal_buffer_count >= INTERNAL_BUFFER_SIZE) {
235
        av_log(s, AV_LOG_ERROR, "internal_buffer_count overflow (missing release_buffer?)\n");
236
        return -1;
237
    }
238

    
239
    if(avcodec_check_dimensions(s,w,h))
240
        return -1;
241

    
242
    if(s->internal_buffer==NULL){
243
        s->internal_buffer= av_mallocz((INTERNAL_BUFFER_SIZE+1)*sizeof(InternalBuffer));
244
    }
245
#if 0
246
    s->internal_buffer= av_fast_realloc(
247
        s->internal_buffer,
248
        &s->internal_buffer_size,
249
        sizeof(InternalBuffer)*FFMAX(99,  s->internal_buffer_count+1)/*FIXME*/
250
        );
251
#endif
252

    
253
    buf= &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
254
    picture_number= &(((InternalBuffer*)s->internal_buffer)[INTERNAL_BUFFER_SIZE]).last_pic_num; //FIXME ugly hack
255
    (*picture_number)++;
256

    
257
    if(buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)){
258
        for(i=0; i<4; i++){
259
            av_freep(&buf->base[i]);
260
            buf->data[i]= NULL;
261
        }
262
    }
263

    
264
    if(buf->base[0]){
265
        pic->age= *picture_number - buf->last_pic_num;
266
        buf->last_pic_num= *picture_number;
267
    }else{
268
        int h_chroma_shift, v_chroma_shift;
269
        int size[4] = {0};
270
        int tmpsize;
271
        int unaligned;
272
        AVPicture picture;
273
        int stride_align[4];
274

    
275
        avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
276

    
277
        avcodec_align_dimensions2(s, &w, &h, stride_align);
278

    
279
        if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
280
            w+= EDGE_WIDTH*2;
281
            h+= EDGE_WIDTH*2;
282
        }
283

    
284
        do {
285
            // NOTE: do not align linesizes individually, this breaks e.g. assumptions
286
            // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
287
            ff_fill_linesize(&picture, s->pix_fmt, w);
288
            // increase alignment of w for next try (rhs gives the lowest bit set in w)
289
            w += w & ~(w-1);
290

    
291
            unaligned = 0;
292
            for (i=0; i<4; i++){
293
                unaligned |= picture.linesize[i] % stride_align[i];
294
            }
295
        } while (unaligned);
296

    
297
        tmpsize = ff_fill_pointer(&picture, NULL, s->pix_fmt, h);
298
        if (tmpsize < 0)
299
            return -1;
300

    
301
        for (i=0; i<3 && picture.data[i+1]; i++)
302
            size[i] = picture.data[i+1] - picture.data[i];
303
        size[i] = tmpsize - (picture.data[i] - picture.data[0]);
304

    
305
        buf->last_pic_num= -256*256*256*64;
306
        memset(buf->base, 0, sizeof(buf->base));
307
        memset(buf->data, 0, sizeof(buf->data));
308

    
309
        for(i=0; i<4 && size[i]; i++){
310
            const int h_shift= i==0 ? 0 : h_chroma_shift;
311
            const int v_shift= i==0 ? 0 : v_chroma_shift;
312

    
313
            buf->linesize[i]= picture.linesize[i];
314

    
315
            buf->base[i]= av_malloc(size[i]+16); //FIXME 16
316
            if(buf->base[i]==NULL) return -1;
317
            memset(buf->base[i], 128, size[i]);
318

    
319
            // no edge if EDGE EMU or not planar YUV
320
            if((s->flags&CODEC_FLAG_EMU_EDGE) || !size[2])
321
                buf->data[i] = buf->base[i];
322
            else
323
                buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (EDGE_WIDTH>>h_shift), stride_align[i]);
324
        }
325
        if(size[1] && !size[2])
326
            ff_set_systematic_pal((uint32_t*)buf->data[1], s->pix_fmt);
327
        buf->width  = s->width;
328
        buf->height = s->height;
329
        buf->pix_fmt= s->pix_fmt;
330
        pic->age= 256*256*256*64;
331
    }
332
    pic->type= FF_BUFFER_TYPE_INTERNAL;
333

    
334
    for(i=0; i<4; i++){
335
        pic->base[i]= buf->base[i];
336
        pic->data[i]= buf->data[i];
337
        pic->linesize[i]= buf->linesize[i];
338
    }
339
    s->internal_buffer_count++;
340

    
341
    pic->reordered_opaque= s->reordered_opaque;
342

    
343
    if(s->debug&FF_DEBUG_BUFFERS)
344
        av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d buffers used\n", pic, s->internal_buffer_count);
345

    
346
    return 0;
347
}
348

    
349
void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
350
    int i;
351
    InternalBuffer *buf, *last;
352

    
353
    assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
354
    assert(s->internal_buffer_count);
355

    
356
    buf = NULL; /* avoids warning */
357
    for(i=0; i<s->internal_buffer_count; i++){ //just 3-5 checks so is not worth to optimize
358
        buf= &((InternalBuffer*)s->internal_buffer)[i];
359
        if(buf->data[0] == pic->data[0])
360
            break;
361
    }
362
    assert(i < s->internal_buffer_count);
363
    s->internal_buffer_count--;
364
    last = &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
365

    
366
    FFSWAP(InternalBuffer, *buf, *last);
367

    
368
    for(i=0; i<4; i++){
369
        pic->data[i]=NULL;
370
//        pic->base[i]=NULL;
371
    }
372
//printf("R%X\n", pic->opaque);
373

    
374
    if(s->debug&FF_DEBUG_BUFFERS)
375
        av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d buffers used\n", pic, s->internal_buffer_count);
376
}
377

    
378
int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
379
    AVFrame temp_pic;
380
    int i;
381

    
382
    /* If no picture return a new buffer */
383
    if(pic->data[0] == NULL) {
384
        /* We will copy from buffer, so must be readable */
385
        pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
386
        return s->get_buffer(s, pic);
387
    }
388

    
389
    /* If internal buffer type return the same buffer */
390
    if(pic->type == FF_BUFFER_TYPE_INTERNAL) {
391
        pic->reordered_opaque= s->reordered_opaque;
392
        return 0;
393
    }
394

    
395
    /*
396
     * Not internal type and reget_buffer not overridden, emulate cr buffer
397
     */
398
    temp_pic = *pic;
399
    for(i = 0; i < 4; i++)
400
        pic->data[i] = pic->base[i] = NULL;
401
    pic->opaque = NULL;
402
    /* Allocate new frame */
403
    if (s->get_buffer(s, pic))
404
        return -1;
405
    /* Copy image data from old buffer to new buffer */
406
    av_picture_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width,
407
             s->height);
408
    s->release_buffer(s, &temp_pic); // Release old frame
409
    return 0;
410
}
411

    
412
int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
413
    int i;
414

    
415
    for(i=0; i<count; i++){
416
        int r= func(c, (char*)arg + i*size);
417
        if(ret) ret[i]= r;
418
    }
419
    return 0;
420
}
421

    
422
int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr),void *arg, int *ret, int count){
423
    int i;
424

    
425
    for(i=0; i<count; i++){
426
        int r= func(c, arg, i, 0);
427
        if(ret) ret[i]= r;
428
    }
429
    return 0;
430
}
431

    
432
enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat *fmt){
433
    while (*fmt != PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
434
        ++fmt;
435
    return fmt[0];
436
}
437

    
438
void avcodec_get_frame_defaults(AVFrame *pic){
439
    memset(pic, 0, sizeof(AVFrame));
440

    
441
    pic->pts= AV_NOPTS_VALUE;
442
    pic->key_frame= 1;
443
}
444

    
445
AVFrame *avcodec_alloc_frame(void){
446
    AVFrame *pic= av_malloc(sizeof(AVFrame));
447

    
448
    if(pic==NULL) return NULL;
449

    
450
    avcodec_get_frame_defaults(pic);
451

    
452
    return pic;
453
}
454

    
455
int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
456
{
457
    int ret= -1;
458

    
459
    /* If there is a user-supplied mutex locking routine, call it. */
460
    if (ff_lockmgr_cb) {
461
        if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
462
            return -1;
463
    }
464

    
465
    entangled_thread_counter++;
466
    if(entangled_thread_counter != 1){
467
        av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
468
        goto end;
469
    }
470

    
471
    if(avctx->codec || !codec)
472
        goto end;
473

    
474
    if (codec->priv_data_size > 0) {
475
        avctx->priv_data = av_mallocz(codec->priv_data_size);
476
        if (!avctx->priv_data) {
477
            ret = AVERROR(ENOMEM);
478
            goto end;
479
        }
480
    } else {
481
        avctx->priv_data = NULL;
482
    }
483

    
484
    if(avctx->coded_width && avctx->coded_height)
485
        avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
486
    else if(avctx->width && avctx->height)
487
        avcodec_set_dimensions(avctx, avctx->width, avctx->height);
488

    
489
#define SANE_NB_CHANNELS 128U
490
    if (((avctx->coded_width || avctx->coded_height)
491
        && avcodec_check_dimensions(avctx, avctx->coded_width, avctx->coded_height))
492
        || avctx->channels > SANE_NB_CHANNELS) {
493
        ret = AVERROR(EINVAL);
494
        goto free_and_end;
495
    }
496

    
497
    avctx->codec = codec;
498
    if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
499
        avctx->codec_id == CODEC_ID_NONE) {
500
        avctx->codec_type = codec->type;
501
        avctx->codec_id   = codec->id;
502
    }
503
    if(avctx->codec_id != codec->id || avctx->codec_type != codec->type){
504
        av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
505
        goto free_and_end;
506
    }
507
    avctx->frame_number = 0;
508
    if(avctx->codec->init){
509
        if(avctx->codec_type == AVMEDIA_TYPE_VIDEO &&
510
           avctx->codec->max_lowres < avctx->lowres){
511
            av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
512
                   avctx->codec->max_lowres);
513
            goto free_and_end;
514
        }
515

    
516
        ret = avctx->codec->init(avctx);
517
        if (ret < 0) {
518
            goto free_and_end;
519
        }
520
    }
521
    ret=0;
522
end:
523
    entangled_thread_counter--;
524

    
525
    /* Release any user-supplied mutex. */
526
    if (ff_lockmgr_cb) {
527
        (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
528
    }
529
    return ret;
530
free_and_end:
531
    av_freep(&avctx->priv_data);
532
    avctx->codec= NULL;
533
    goto end;
534
}
535

    
536
int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx, uint8_t *buf, int buf_size,
537
                         const short *samples)
538
{
539
    if(buf_size < FF_MIN_BUFFER_SIZE && 0){
540
        av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
541
        return -1;
542
    }
543
    if((avctx->codec->capabilities & CODEC_CAP_DELAY) || samples){
544
        int ret = avctx->codec->encode(avctx, buf, buf_size, samples);
545
        avctx->frame_number++;
546
        return ret;
547
    }else
548
        return 0;
549
}
550

    
551
int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
552
                         const AVFrame *pict)
553
{
554
    if(buf_size < FF_MIN_BUFFER_SIZE){
555
        av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
556
        return -1;
557
    }
558
    if(avcodec_check_dimensions(avctx,avctx->width,avctx->height))
559
        return -1;
560
    if((avctx->codec->capabilities & CODEC_CAP_DELAY) || pict){
561
        int ret = avctx->codec->encode(avctx, buf, buf_size, pict);
562
        avctx->frame_number++;
563
        emms_c(); //needed to avoid an emms_c() call before every return;
564

    
565
        return ret;
566
    }else
567
        return 0;
568
}
569

    
570
int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
571
                            const AVSubtitle *sub)
572
{
573
    int ret;
574
    if(sub->start_display_time) {
575
        av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
576
        return -1;
577
    }
578
    if(sub->num_rects == 0 || !sub->rects)
579
        return -1;
580
    ret = avctx->codec->encode(avctx, buf, buf_size, sub);
581
    avctx->frame_number++;
582
    return ret;
583
}
584

    
585
#if LIBAVCODEC_VERSION_MAJOR < 53
586
int attribute_align_arg avcodec_decode_video(AVCodecContext *avctx, AVFrame *picture,
587
                         int *got_picture_ptr,
588
                         const uint8_t *buf, int buf_size)
589
{
590
    AVPacket avpkt;
591
    av_init_packet(&avpkt);
592
    avpkt.data = buf;
593
    avpkt.size = buf_size;
594
    // HACK for CorePNG to decode as normal PNG by default
595
    avpkt.flags = AV_PKT_FLAG_KEY;
596

    
597
    return avcodec_decode_video2(avctx, picture, got_picture_ptr, &avpkt);
598
}
599
#endif
600

    
601
int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
602
                         int *got_picture_ptr,
603
                         AVPacket *avpkt)
604
{
605
    int ret;
606

    
607
    *got_picture_ptr= 0;
608
    if((avctx->coded_width||avctx->coded_height) && avcodec_check_dimensions(avctx,avctx->coded_width,avctx->coded_height))
609
        return -1;
610
    if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size){
611
        ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
612
                                avpkt);
613

    
614
        emms_c(); //needed to avoid an emms_c() call before every return;
615

    
616
        if (*got_picture_ptr)
617
            avctx->frame_number++;
618
    }else
619
        ret= 0;
620

    
621
    return ret;
622
}
623

    
624
#if LIBAVCODEC_VERSION_MAJOR < 53
625
int attribute_align_arg avcodec_decode_audio2(AVCodecContext *avctx, int16_t *samples,
626
                         int *frame_size_ptr,
627
                         const uint8_t *buf, int buf_size)
628
{
629
    AVPacket avpkt;
630
    av_init_packet(&avpkt);
631
    avpkt.data = buf;
632
    avpkt.size = buf_size;
633

    
634
    return avcodec_decode_audio3(avctx, samples, frame_size_ptr, &avpkt);
635
}
636
#endif
637

    
638
int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
639
                         int *frame_size_ptr,
640
                         AVPacket *avpkt)
641
{
642
    int ret;
643

    
644
    if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size){
645
        //FIXME remove the check below _after_ ensuring that all audio check that the available space is enough
646
        if(*frame_size_ptr < AVCODEC_MAX_AUDIO_FRAME_SIZE){
647
            av_log(avctx, AV_LOG_ERROR, "buffer smaller than AVCODEC_MAX_AUDIO_FRAME_SIZE\n");
648
            return -1;
649
        }
650
        if(*frame_size_ptr < FF_MIN_BUFFER_SIZE ||
651
        *frame_size_ptr < avctx->channels * avctx->frame_size * sizeof(int16_t)){
652
            av_log(avctx, AV_LOG_ERROR, "buffer %d too small\n", *frame_size_ptr);
653
            return -1;
654
        }
655

    
656
        ret = avctx->codec->decode(avctx, samples, frame_size_ptr, avpkt);
657
        avctx->frame_number++;
658
    }else{
659
        ret= 0;
660
        *frame_size_ptr=0;
661
    }
662
    return ret;
663
}
664

    
665
#if LIBAVCODEC_VERSION_MAJOR < 53
666
int avcodec_decode_subtitle(AVCodecContext *avctx, AVSubtitle *sub,
667
                            int *got_sub_ptr,
668
                            const uint8_t *buf, int buf_size)
669
{
670
    AVPacket avpkt;
671
    av_init_packet(&avpkt);
672
    avpkt.data = buf;
673
    avpkt.size = buf_size;
674

    
675
    return avcodec_decode_subtitle2(avctx, sub, got_sub_ptr, &avpkt);
676
}
677
#endif
678

    
679
int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
680
                            int *got_sub_ptr,
681
                            AVPacket *avpkt)
682
{
683
    int ret;
684

    
685
    *got_sub_ptr = 0;
686
    ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
687
    if (*got_sub_ptr)
688
        avctx->frame_number++;
689
    return ret;
690
}
691

    
692
av_cold int avcodec_close(AVCodecContext *avctx)
693
{
694
    /* If there is a user-supplied mutex locking routine, call it. */
695
    if (ff_lockmgr_cb) {
696
        if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
697
            return -1;
698
    }
699

    
700
    entangled_thread_counter++;
701
    if(entangled_thread_counter != 1){
702
        av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
703
        entangled_thread_counter--;
704
        return -1;
705
    }
706

    
707
    if (HAVE_THREADS && avctx->thread_opaque)
708
        avcodec_thread_free(avctx);
709
    if (avctx->codec && avctx->codec->close)
710
        avctx->codec->close(avctx);
711
    avcodec_default_free_buffers(avctx);
712
    avctx->coded_frame = NULL;
713
    av_freep(&avctx->priv_data);
714
    if(avctx->codec && avctx->codec->encode)
715
        av_freep(&avctx->extradata);
716
    avctx->codec = NULL;
717
    entangled_thread_counter--;
718

    
719
    /* Release any user-supplied mutex. */
720
    if (ff_lockmgr_cb) {
721
        (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
722
    }
723
    return 0;
724
}
725

    
726
AVCodec *avcodec_find_encoder(enum CodecID id)
727
{
728
    AVCodec *p, *experimental=NULL;
729
    p = first_avcodec;
730
    while (p) {
731
        if (p->encode != NULL && p->id == id) {
732
            if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
733
                experimental = p;
734
            } else
735
                return p;
736
        }
737
        p = p->next;
738
    }
739
    return experimental;
740
}
741

    
742
AVCodec *avcodec_find_encoder_by_name(const char *name)
743
{
744
    AVCodec *p;
745
    if (!name)
746
        return NULL;
747
    p = first_avcodec;
748
    while (p) {
749
        if (p->encode != NULL && strcmp(name,p->name) == 0)
750
            return p;
751
        p = p->next;
752
    }
753
    return NULL;
754
}
755

    
756
AVCodec *avcodec_find_decoder(enum CodecID id)
757
{
758
    AVCodec *p;
759
    p = first_avcodec;
760
    while (p) {
761
        if (p->decode != NULL && p->id == id)
762
            return p;
763
        p = p->next;
764
    }
765
    return NULL;
766
}
767

    
768
AVCodec *avcodec_find_decoder_by_name(const char *name)
769
{
770
    AVCodec *p;
771
    if (!name)
772
        return NULL;
773
    p = first_avcodec;
774
    while (p) {
775
        if (p->decode != NULL && strcmp(name,p->name) == 0)
776
            return p;
777
        p = p->next;
778
    }
779
    return NULL;
780
}
781

    
782
static int get_bit_rate(AVCodecContext *ctx)
783
{
784
    int bit_rate;
785
    int bits_per_sample;
786

    
787
    switch(ctx->codec_type) {
788
    case AVMEDIA_TYPE_VIDEO:
789
    case AVMEDIA_TYPE_DATA:
790
    case AVMEDIA_TYPE_SUBTITLE:
791
    case AVMEDIA_TYPE_ATTACHMENT:
792
        bit_rate = ctx->bit_rate;
793
        break;
794
    case AVMEDIA_TYPE_AUDIO:
795
        bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
796
        bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
797
        break;
798
    default:
799
        bit_rate = 0;
800
        break;
801
    }
802
    return bit_rate;
803
}
804

    
805
size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
806
{
807
    int i, len, ret = 0;
808

    
809
    for (i = 0; i < 4; i++) {
810
        len = snprintf(buf, buf_size,
811
                       isprint(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
812
        buf      += len;
813
        buf_size  = buf_size > len ? buf_size - len : 0;
814
        ret      += len;
815
        codec_tag>>=8;
816
    }
817
    return ret;
818
}
819

    
820
void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
821
{
822
    const char *codec_name;
823
    AVCodec *p;
824
    char buf1[32];
825
    int bitrate;
826
    AVRational display_aspect_ratio;
827

    
828
    if (encode)
829
        p = avcodec_find_encoder(enc->codec_id);
830
    else
831
        p = avcodec_find_decoder(enc->codec_id);
832

    
833
    if (p) {
834
        codec_name = p->name;
835
    } else if (enc->codec_id == CODEC_ID_MPEG2TS) {
836
        /* fake mpeg2 transport stream codec (currently not
837
           registered) */
838
        codec_name = "mpeg2ts";
839
    } else if (enc->codec_name[0] != '\0') {
840
        codec_name = enc->codec_name;
841
    } else {
842
        /* output avi tags */
843
        char tag_buf[32];
844
        av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
845
        snprintf(buf1, sizeof(buf1), "%s / 0x%04X", tag_buf, enc->codec_tag);
846
        codec_name = buf1;
847
    }
848

    
849
    switch(enc->codec_type) {
850
    case AVMEDIA_TYPE_VIDEO:
851
        snprintf(buf, buf_size,
852
                 "Video: %s%s",
853
                 codec_name, enc->mb_decision ? " (hq)" : "");
854
        if (enc->pix_fmt != PIX_FMT_NONE) {
855
            snprintf(buf + strlen(buf), buf_size - strlen(buf),
856
                     ", %s",
857
                     avcodec_get_pix_fmt_name(enc->pix_fmt));
858
        }
859
        if (enc->width) {
860
            snprintf(buf + strlen(buf), buf_size - strlen(buf),
861
                     ", %dx%d",
862
                     enc->width, enc->height);
863
            if (enc->sample_aspect_ratio.num) {
864
                av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
865
                          enc->width*enc->sample_aspect_ratio.num,
866
                          enc->height*enc->sample_aspect_ratio.den,
867
                          1024*1024);
868
                snprintf(buf + strlen(buf), buf_size - strlen(buf),
869
                         " [PAR %d:%d DAR %d:%d]",
870
                         enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
871
                         display_aspect_ratio.num, display_aspect_ratio.den);
872
            }
873
            if(av_log_get_level() >= AV_LOG_DEBUG){
874
                int g= av_gcd(enc->time_base.num, enc->time_base.den);
875
                snprintf(buf + strlen(buf), buf_size - strlen(buf),
876
                     ", %d/%d",
877
                     enc->time_base.num/g, enc->time_base.den/g);
878
            }
879
        }
880
        if (encode) {
881
            snprintf(buf + strlen(buf), buf_size - strlen(buf),
882
                     ", q=%d-%d", enc->qmin, enc->qmax);
883
        }
884
        break;
885
    case AVMEDIA_TYPE_AUDIO:
886
        snprintf(buf, buf_size,
887
                 "Audio: %s",
888
                 codec_name);
889
        if (enc->sample_rate) {
890
            snprintf(buf + strlen(buf), buf_size - strlen(buf),
891
                     ", %d Hz", enc->sample_rate);
892
        }
893
        av_strlcat(buf, ", ", buf_size);
894
        avcodec_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
895
        if (enc->sample_fmt != SAMPLE_FMT_NONE) {
896
            snprintf(buf + strlen(buf), buf_size - strlen(buf),
897
                     ", %s", avcodec_get_sample_fmt_name(enc->sample_fmt));
898
        }
899
        break;
900
    case AVMEDIA_TYPE_DATA:
901
        snprintf(buf, buf_size, "Data: %s", codec_name);
902
        break;
903
    case AVMEDIA_TYPE_SUBTITLE:
904
        snprintf(buf, buf_size, "Subtitle: %s", codec_name);
905
        break;
906
    case AVMEDIA_TYPE_ATTACHMENT:
907
        snprintf(buf, buf_size, "Attachment: %s", codec_name);
908
        break;
909
    default:
910
        snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
911
        return;
912
    }
913
    if (encode) {
914
        if (enc->flags & CODEC_FLAG_PASS1)
915
            snprintf(buf + strlen(buf), buf_size - strlen(buf),
916
                     ", pass 1");
917
        if (enc->flags & CODEC_FLAG_PASS2)
918
            snprintf(buf + strlen(buf), buf_size - strlen(buf),
919
                     ", pass 2");
920
    }
921
    bitrate = get_bit_rate(enc);
922
    if (bitrate != 0) {
923
        snprintf(buf + strlen(buf), buf_size - strlen(buf),
924
                 ", %d kb/s", bitrate / 1000);
925
    }
926
}
927

    
928
unsigned avcodec_version( void )
929
{
930
  return LIBAVCODEC_VERSION_INT;
931
}
932

    
933
const char *avcodec_configuration(void)
934
{
935
    return FFMPEG_CONFIGURATION;
936
}
937

    
938
const char *avcodec_license(void)
939
{
940
#define LICENSE_PREFIX "libavcodec license: "
941
    return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
942
}
943

    
944
void avcodec_init(void)
945
{
946
    static int initialized = 0;
947

    
948
    if (initialized != 0)
949
        return;
950
    initialized = 1;
951

    
952
    dsputil_static_init();
953
}
954

    
955
void avcodec_flush_buffers(AVCodecContext *avctx)
956
{
957
    if(avctx->codec->flush)
958
        avctx->codec->flush(avctx);
959
}
960

    
961
void avcodec_default_free_buffers(AVCodecContext *s){
962
    int i, j;
963

    
964
    if(s->internal_buffer==NULL) return;
965

    
966
    if (s->internal_buffer_count)
967
        av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n", s->internal_buffer_count);
968
    for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
969
        InternalBuffer *buf= &((InternalBuffer*)s->internal_buffer)[i];
970
        for(j=0; j<4; j++){
971
            av_freep(&buf->base[j]);
972
            buf->data[j]= NULL;
973
        }
974
    }
975
    av_freep(&s->internal_buffer);
976

    
977
    s->internal_buffer_count=0;
978
}
979

    
980
char av_get_pict_type_char(int pict_type){
981
    switch(pict_type){
982
    case FF_I_TYPE: return 'I';
983
    case FF_P_TYPE: return 'P';
984
    case FF_B_TYPE: return 'B';
985
    case FF_S_TYPE: return 'S';
986
    case FF_SI_TYPE:return 'i';
987
    case FF_SP_TYPE:return 'p';
988
    case FF_BI_TYPE:return 'b';
989
    default:        return '?';
990
    }
991
}
992

    
993
int av_get_bits_per_sample(enum CodecID codec_id){
994
    switch(codec_id){
995
    case CODEC_ID_ADPCM_SBPRO_2:
996
        return 2;
997
    case CODEC_ID_ADPCM_SBPRO_3:
998
        return 3;
999
    case CODEC_ID_ADPCM_SBPRO_4:
1000
    case CODEC_ID_ADPCM_CT:
1001
    case CODEC_ID_ADPCM_IMA_WAV:
1002
    case CODEC_ID_ADPCM_MS:
1003
    case CODEC_ID_ADPCM_YAMAHA:
1004
        return 4;
1005
    case CODEC_ID_PCM_ALAW:
1006
    case CODEC_ID_PCM_MULAW:
1007
    case CODEC_ID_PCM_S8:
1008
    case CODEC_ID_PCM_U8:
1009
    case CODEC_ID_PCM_ZORK:
1010
        return 8;
1011
    case CODEC_ID_PCM_S16BE:
1012
    case CODEC_ID_PCM_S16LE:
1013
    case CODEC_ID_PCM_S16LE_PLANAR:
1014
    case CODEC_ID_PCM_U16BE:
1015
    case CODEC_ID_PCM_U16LE:
1016
        return 16;
1017
    case CODEC_ID_PCM_S24DAUD:
1018
    case CODEC_ID_PCM_S24BE:
1019
    case CODEC_ID_PCM_S24LE:
1020
    case CODEC_ID_PCM_U24BE:
1021
    case CODEC_ID_PCM_U24LE:
1022
        return 24;
1023
    case CODEC_ID_PCM_S32BE:
1024
    case CODEC_ID_PCM_S32LE:
1025
    case CODEC_ID_PCM_U32BE:
1026
    case CODEC_ID_PCM_U32LE:
1027
    case CODEC_ID_PCM_F32BE:
1028
    case CODEC_ID_PCM_F32LE:
1029
        return 32;
1030
    case CODEC_ID_PCM_F64BE:
1031
    case CODEC_ID_PCM_F64LE:
1032
        return 64;
1033
    default:
1034
        return 0;
1035
    }
1036
}
1037

    
1038
int av_get_bits_per_sample_format(enum SampleFormat sample_fmt) {
1039
    switch (sample_fmt) {
1040
    case SAMPLE_FMT_U8:
1041
        return 8;
1042
    case SAMPLE_FMT_S16:
1043
        return 16;
1044
    case SAMPLE_FMT_S32:
1045
    case SAMPLE_FMT_FLT:
1046
        return 32;
1047
    case SAMPLE_FMT_DBL:
1048
        return 64;
1049
    default:
1050
        return 0;
1051
    }
1052
}
1053

    
1054
#if !HAVE_THREADS
1055
int avcodec_thread_init(AVCodecContext *s, int thread_count){
1056
    s->thread_count = thread_count;
1057
    return -1;
1058
}
1059
#endif
1060

    
1061
unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
1062
{
1063
    unsigned int n = 0;
1064

    
1065
    while(v >= 0xff) {
1066
        *s++ = 0xff;
1067
        v -= 0xff;
1068
        n++;
1069
    }
1070
    *s = v;
1071
    n++;
1072
    return n;
1073
}
1074

    
1075
typedef struct {
1076
    const char *abbr;
1077
    int width, height;
1078
} VideoFrameSizeAbbr;
1079

    
1080
typedef struct {
1081
    const char *abbr;
1082
    int rate_num, rate_den;
1083
} VideoFrameRateAbbr;
1084

    
1085
static const VideoFrameSizeAbbr video_frame_size_abbrs[] = {
1086
    { "ntsc",      720, 480 },
1087
    { "pal",       720, 576 },
1088
    { "qntsc",     352, 240 }, /* VCD compliant NTSC */
1089
    { "qpal",      352, 288 }, /* VCD compliant PAL */
1090
    { "sntsc",     640, 480 }, /* square pixel NTSC */
1091
    { "spal",      768, 576 }, /* square pixel PAL */
1092
    { "film",      352, 240 },
1093
    { "ntsc-film", 352, 240 },
1094
    { "sqcif",     128,  96 },
1095
    { "qcif",      176, 144 },
1096
    { "cif",       352, 288 },
1097
    { "4cif",      704, 576 },
1098
    { "16cif",    1408,1152 },
1099
    { "qqvga",     160, 120 },
1100
    { "qvga",      320, 240 },
1101
    { "vga",       640, 480 },
1102
    { "svga",      800, 600 },
1103
    { "xga",      1024, 768 },
1104
    { "uxga",     1600,1200 },
1105
    { "qxga",     2048,1536 },
1106
    { "sxga",     1280,1024 },
1107
    { "qsxga",    2560,2048 },
1108
    { "hsxga",    5120,4096 },
1109
    { "wvga",      852, 480 },
1110
    { "wxga",     1366, 768 },
1111
    { "wsxga",    1600,1024 },
1112
    { "wuxga",    1920,1200 },
1113
    { "woxga",    2560,1600 },
1114
    { "wqsxga",   3200,2048 },
1115
    { "wquxga",   3840,2400 },
1116
    { "whsxga",   6400,4096 },
1117
    { "whuxga",   7680,4800 },
1118
    { "cga",       320, 200 },
1119
    { "ega",       640, 350 },
1120
    { "hd480",     852, 480 },
1121
    { "hd720",    1280, 720 },
1122
    { "hd1080",   1920,1080 },
1123
};
1124

    
1125
static const VideoFrameRateAbbr video_frame_rate_abbrs[]= {
1126
    { "ntsc",      30000, 1001 },
1127
    { "pal",          25,    1 },
1128
    { "qntsc",     30000, 1001 }, /* VCD compliant NTSC */
1129
    { "qpal",         25,    1 }, /* VCD compliant PAL */
1130
    { "sntsc",     30000, 1001 }, /* square pixel NTSC */
1131
    { "spal",         25,    1 }, /* square pixel PAL */
1132
    { "film",         24,    1 },
1133
    { "ntsc-film", 24000, 1001 },
1134
};
1135

    
1136
int av_parse_video_frame_size(int *width_ptr, int *height_ptr, const char *str)
1137
{
1138
    int i;
1139
    int n = FF_ARRAY_ELEMS(video_frame_size_abbrs);
1140
    char *p;
1141
    int frame_width = 0, frame_height = 0;
1142

    
1143
    for(i=0;i<n;i++) {
1144
        if (!strcmp(video_frame_size_abbrs[i].abbr, str)) {
1145
            frame_width = video_frame_size_abbrs[i].width;
1146
            frame_height = video_frame_size_abbrs[i].height;
1147
            break;
1148
        }
1149
    }
1150
    if (i == n) {
1151
        p = str;
1152
        frame_width = strtol(p, &p, 10);
1153
        if (*p)
1154
            p++;
1155
        frame_height = strtol(p, &p, 10);
1156
    }
1157
    if (frame_width <= 0 || frame_height <= 0)
1158
        return -1;
1159
    *width_ptr = frame_width;
1160
    *height_ptr = frame_height;
1161
    return 0;
1162
}
1163

    
1164
int av_parse_video_frame_rate(AVRational *frame_rate, const char *arg)
1165
{
1166
    int i;
1167
    int n = FF_ARRAY_ELEMS(video_frame_rate_abbrs);
1168
    char* cp;
1169

    
1170
    /* First, we check our abbreviation table */
1171
    for (i = 0; i < n; ++i)
1172
         if (!strcmp(video_frame_rate_abbrs[i].abbr, arg)) {
1173
             frame_rate->num = video_frame_rate_abbrs[i].rate_num;
1174
             frame_rate->den = video_frame_rate_abbrs[i].rate_den;
1175
             return 0;
1176
         }
1177

    
1178
    /* Then, we try to parse it as fraction */
1179
    cp = strchr(arg, '/');
1180
    if (!cp)
1181
        cp = strchr(arg, ':');
1182
    if (cp) {
1183
        char* cpp;
1184
        frame_rate->num = strtol(arg, &cpp, 10);
1185
        if (cpp != arg || cpp == cp)
1186
            frame_rate->den = strtol(cp+1, &cpp, 10);
1187
        else
1188
           frame_rate->num = 0;
1189
    }
1190
    else {
1191
        /* Finally we give up and parse it as double */
1192
        AVRational time_base = av_d2q(strtod(arg, 0), 1001000);
1193
        frame_rate->den = time_base.den;
1194
        frame_rate->num = time_base.num;
1195
    }
1196
    if (!frame_rate->num || !frame_rate->den)
1197
        return -1;
1198
    else
1199
        return 0;
1200
}
1201

    
1202
int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
1203
    int i;
1204
    for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
1205
    return i;
1206
}
1207

    
1208
void av_log_missing_feature(void *avc, const char *feature, int want_sample)
1209
{
1210
    av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your FFmpeg "
1211
            "version to the newest one from SVN. If the problem still "
1212
            "occurs, it means that your file has a feature which has not "
1213
            "been implemented.", feature);
1214
    if(want_sample)
1215
        av_log_ask_for_sample(avc, NULL);
1216
    else
1217
        av_log(avc, AV_LOG_WARNING, "\n");
1218
}
1219

    
1220
void av_log_ask_for_sample(void *avc, const char *msg)
1221
{
1222
    if (msg)
1223
        av_log(avc, AV_LOG_WARNING, "%s ", msg);
1224
    av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
1225
            "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
1226
            "and contact the ffmpeg-devel mailing list.\n");
1227
}
1228

    
1229
static AVHWAccel *first_hwaccel = NULL;
1230

    
1231
void av_register_hwaccel(AVHWAccel *hwaccel)
1232
{
1233
    AVHWAccel **p = &first_hwaccel;
1234
    while (*p)
1235
        p = &(*p)->next;
1236
    *p = hwaccel;
1237
    hwaccel->next = NULL;
1238
}
1239

    
1240
AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
1241
{
1242
    return hwaccel ? hwaccel->next : first_hwaccel;
1243
}
1244

    
1245
AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
1246
{
1247
    AVHWAccel *hwaccel=NULL;
1248

    
1249
    while((hwaccel= av_hwaccel_next(hwaccel))){
1250
        if (   hwaccel->id      == codec_id
1251
            && hwaccel->pix_fmt == pix_fmt)
1252
            return hwaccel;
1253
    }
1254
    return NULL;
1255
}
1256

    
1257
int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
1258
{
1259
    if (ff_lockmgr_cb) {
1260
        if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
1261
            return -1;
1262
    }
1263

    
1264
    ff_lockmgr_cb = cb;
1265

    
1266
    if (ff_lockmgr_cb) {
1267
        if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
1268
            return -1;
1269
    }
1270
    return 0;
1271
}
1272

    
1273
unsigned int ff_toupper4(unsigned int x)
1274
{
1275
    return     toupper( x     &0xFF)
1276
            + (toupper((x>>8 )&0xFF)<<8 )
1277
            + (toupper((x>>16)&0xFF)<<16)
1278
            + (toupper((x>>24)&0xFF)<<24);
1279
}