Statistics
| Branch: | Revision:

ffmpeg / libavcodec / utils.c @ 1ba57272

History | View | Annotate | Download (39 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 "libavutil/audioconvert.h"
33
#include "libavutil/imgutils.h"
34
#include "libavutil/samplefmt.h"
35
#include "avcodec.h"
36
#include "dsputil.h"
37
#include "libavutil/opt.h"
38
#include "imgconvert.h"
39
#include "thread.h"
40
#include "audioconvert.h"
41
#include "internal.h"
42
#include <stdlib.h>
43
#include <stdarg.h>
44
#include <limits.h>
45
#include <float.h>
46

    
47
static int volatile entangled_thread_counter=0;
48
static int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op);
49
static void *codec_mutex;
50

    
51
void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
52
{
53
    if(min_size < *size)
54
        return ptr;
55

    
56
    min_size= FFMAX(17*min_size/16 + 32, min_size);
57

    
58
    ptr= av_realloc(ptr, min_size);
59
    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
        min_size= 0;
61

    
62
    *size= min_size;
63

    
64
    return ptr;
65
}
66

    
67
void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
68
{
69
    void **p = ptr;
70
    if (min_size < *size)
71
        return;
72
    min_size= FFMAX(17*min_size/16 + 32, min_size);
73
    av_free(*p);
74
    *p = av_malloc(min_size);
75
    if (!*p) min_size = 0;
76
    *size= min_size;
77
}
78

    
79
/* encoder management */
80
static AVCodec *first_avcodec = NULL;
81

    
82
AVCodec *av_codec_next(AVCodec *c){
83
    if(c) return c->next;
84
    else  return first_avcodec;
85
}
86

    
87
void avcodec_register(AVCodec *codec)
88
{
89
    AVCodec **p;
90
    avcodec_init();
91
    p = &first_avcodec;
92
    while (*p != NULL) p = &(*p)->next;
93
    *p = codec;
94
    codec->next = NULL;
95
}
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
    case PIX_FMT_YUV420P9LE:
140
    case PIX_FMT_YUV420P9BE:
141
    case PIX_FMT_YUV420P10LE:
142
    case PIX_FMT_YUV420P10BE:
143
        w_align= 16; //FIXME check for non mpeg style codecs and use less alignment
144
        h_align= 16;
145
        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)
146
            h_align= 32; // interlaced is rounded up to 2 MBs
147
        break;
148
    case PIX_FMT_YUV411P:
149
    case PIX_FMT_UYYVYY411:
150
        w_align=32;
151
        h_align=8;
152
        break;
153
    case PIX_FMT_YUV410P:
154
        if(s->codec_id == CODEC_ID_SVQ1){
155
            w_align=64;
156
            h_align=64;
157
        }
158
    case PIX_FMT_RGB555:
159
        if(s->codec_id == CODEC_ID_RPZA){
160
            w_align=4;
161
            h_align=4;
162
        }
163
    case PIX_FMT_PAL8:
164
    case PIX_FMT_BGR8:
165
    case PIX_FMT_RGB8:
166
        if(s->codec_id == CODEC_ID_SMC){
167
            w_align=4;
168
            h_align=4;
169
        }
170
        break;
171
    case PIX_FMT_BGR24:
172
        if((s->codec_id == CODEC_ID_MSZH) || (s->codec_id == CODEC_ID_ZLIB)){
173
            w_align=4;
174
            h_align=4;
175
        }
176
        break;
177
    default:
178
        w_align= 1;
179
        h_align= 1;
180
        break;
181
    }
182

    
183
    *width = FFALIGN(*width , w_align);
184
    *height= FFALIGN(*height, h_align);
185
    if(s->codec_id == CODEC_ID_H264 || s->lowres)
186
        *height+=2; // some of the optimized chroma MC reads one line too much
187
                    // which is also done in mpeg decoders with lowres > 0
188

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

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

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

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

    
236
    if(av_image_check_size(w, h, 0, s))
237
        return -1;
238

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

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

    
254
    if(buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)){
255
        if(s->active_thread_type&FF_THREAD_FRAME) {
256
            av_log_missing_feature(s, "Width/height changing with frame threads is", 0);
257
            return -1;
258
        }
259

    
260
        for(i=0; i<4; i++){
261
            av_freep(&buf->base[i]);
262
            buf->data[i]= NULL;
263
        }
264
    }
265

    
266
    if(buf->base[0]){
267
        pic->age= *picture_number - buf->last_pic_num;
268
        buf->last_pic_num= *picture_number;
269
    }else{
270
        int h_chroma_shift, v_chroma_shift;
271
        int size[4] = {0};
272
        int tmpsize;
273
        int unaligned;
274
        AVPicture picture;
275
        int stride_align[4];
276
        const int pixel_size = av_pix_fmt_descriptors[s->pix_fmt].comp[0].step_minus1+1;
277

    
278
        avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
279

    
280
        avcodec_align_dimensions2(s, &w, &h, stride_align);
281

    
282
        if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
283
            w+= EDGE_WIDTH*2;
284
            h+= EDGE_WIDTH*2;
285
        }
286

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

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

    
300
        tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
301
        if (tmpsize < 0)
302
            return -1;
303

    
304
        for (i=0; i<3 && picture.data[i+1]; i++)
305
            size[i] = picture.data[i+1] - picture.data[i];
306
        size[i] = tmpsize - (picture.data[i] - picture.data[0]);
307

    
308
        buf->last_pic_num= -256*256*256*64;
309
        memset(buf->base, 0, sizeof(buf->base));
310
        memset(buf->data, 0, sizeof(buf->data));
311

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

    
316
            buf->linesize[i]= picture.linesize[i];
317

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

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

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

    
344
    if (s->pkt) {
345
        pic->pkt_pts = s->pkt->pts;
346
        pic->pkt_pos = s->pkt->pos;
347
    } else {
348
        pic->pkt_pts = AV_NOPTS_VALUE;
349
        pic->pkt_pos = -1;
350
    }
351
    pic->reordered_opaque= s->reordered_opaque;
352

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

    
356
    return 0;
357
}
358

    
359
void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
360
    int i;
361
    InternalBuffer *buf, *last;
362

    
363
    assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
364
    assert(s->internal_buffer_count);
365

    
366
    if(s->internal_buffer){
367
    buf = NULL; /* avoids warning */
368
    for(i=0; i<s->internal_buffer_count; i++){ //just 3-5 checks so is not worth to optimize
369
        buf= &((InternalBuffer*)s->internal_buffer)[i];
370
        if(buf->data[0] == pic->data[0])
371
            break;
372
    }
373
    assert(i < s->internal_buffer_count);
374
    s->internal_buffer_count--;
375
    last = &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
376

    
377
    FFSWAP(InternalBuffer, *buf, *last);
378
    }
379

    
380
    for(i=0; i<4; i++){
381
        pic->data[i]=NULL;
382
//        pic->base[i]=NULL;
383
    }
384
//printf("R%X\n", pic->opaque);
385

    
386
    if(s->debug&FF_DEBUG_BUFFERS)
387
        av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d buffers used\n", pic, s->internal_buffer_count);
388
}
389

    
390
int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
391
    AVFrame temp_pic;
392
    int i;
393

    
394
    /* If no picture return a new buffer */
395
    if(pic->data[0] == NULL) {
396
        /* We will copy from buffer, so must be readable */
397
        pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
398
        return s->get_buffer(s, pic);
399
    }
400

    
401
    /* If internal buffer type return the same buffer */
402
    if(pic->type == FF_BUFFER_TYPE_INTERNAL) {
403
        if(s->pkt) pic->pkt_pts= s->pkt->pts;
404
        else       pic->pkt_pts= AV_NOPTS_VALUE;
405
        pic->reordered_opaque= s->reordered_opaque;
406
        return 0;
407
    }
408

    
409
    /*
410
     * Not internal type and reget_buffer not overridden, emulate cr buffer
411
     */
412
    temp_pic = *pic;
413
    for(i = 0; i < 4; i++)
414
        pic->data[i] = pic->base[i] = NULL;
415
    pic->opaque = NULL;
416
    /* Allocate new frame */
417
    if (s->get_buffer(s, pic))
418
        return -1;
419
    /* Copy image data from old buffer to new buffer */
420
    av_picture_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width,
421
             s->height);
422
    s->release_buffer(s, &temp_pic); // Release old frame
423
    return 0;
424
}
425

    
426
int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
427
    int i;
428

    
429
    for(i=0; i<count; i++){
430
        int r= func(c, (char*)arg + i*size);
431
        if(ret) ret[i]= r;
432
    }
433
    return 0;
434
}
435

    
436
int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr),void *arg, int *ret, int count){
437
    int i;
438

    
439
    for(i=0; i<count; i++){
440
        int r= func(c, arg, i, 0);
441
        if(ret) ret[i]= r;
442
    }
443
    return 0;
444
}
445

    
446
enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat *fmt){
447
    while (*fmt != PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
448
        ++fmt;
449
    return fmt[0];
450
}
451

    
452
void avcodec_get_frame_defaults(AVFrame *pic){
453
    memset(pic, 0, sizeof(AVFrame));
454

    
455
    pic->pts = pic->best_effort_timestamp = AV_NOPTS_VALUE;
456
    pic->pkt_pos = -1;
457
    pic->key_frame= 1;
458
}
459

    
460
AVFrame *avcodec_alloc_frame(void){
461
    AVFrame *pic= av_malloc(sizeof(AVFrame));
462

    
463
    if(pic==NULL) return NULL;
464

    
465
    avcodec_get_frame_defaults(pic);
466

    
467
    return pic;
468
}
469

    
470
static void avcodec_get_subtitle_defaults(AVSubtitle *sub)
471
{
472
    memset(sub, 0, sizeof(*sub));
473
    sub->pts = AV_NOPTS_VALUE;
474
}
475

    
476
int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
477
{
478
    int ret = 0;
479

    
480
    /* If there is a user-supplied mutex locking routine, call it. */
481
    if (ff_lockmgr_cb) {
482
        if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
483
            return -1;
484
    }
485

    
486
    entangled_thread_counter++;
487
    if(entangled_thread_counter != 1){
488
        av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
489
        ret = -1;
490
        goto end;
491
    }
492

    
493
    if(avctx->codec || !codec) {
494
        ret = AVERROR(EINVAL);
495
        goto end;
496
    }
497

    
498
    if (codec->priv_data_size > 0) {
499
      if(!avctx->priv_data){
500
        avctx->priv_data = av_mallocz(codec->priv_data_size);
501
        if (!avctx->priv_data) {
502
            ret = AVERROR(ENOMEM);
503
            goto end;
504
        }
505
        if(codec->priv_class){ //this can be droped once all user apps use   avcodec_get_context_defaults3()
506
            *(AVClass**)avctx->priv_data= codec->priv_class;
507
            av_opt_set_defaults(avctx->priv_data);
508
        }
509
      }
510
    } else {
511
        avctx->priv_data = NULL;
512
    }
513

    
514
    if(avctx->coded_width && avctx->coded_height)
515
        avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
516
    else if(avctx->width && avctx->height)
517
        avcodec_set_dimensions(avctx, avctx->width, avctx->height);
518

    
519
    if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
520
        && (  av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
521
           || av_image_check_size(avctx->width,       avctx->height,       0, avctx) < 0)) {
522
        av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
523
        avcodec_set_dimensions(avctx, 0, 0);
524
    }
525

    
526
    /* if the decoder init function was already called previously,
527
       free the already allocated subtitle_header before overwriting it */
528
    if (codec->decode)
529
        av_freep(&avctx->subtitle_header);
530

    
531
#define SANE_NB_CHANNELS 128U
532
    if (avctx->channels > SANE_NB_CHANNELS) {
533
        ret = AVERROR(EINVAL);
534
        goto free_and_end;
535
    }
536

    
537
    avctx->codec = codec;
538
    if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
539
        avctx->codec_id == CODEC_ID_NONE) {
540
        avctx->codec_type = codec->type;
541
        avctx->codec_id   = codec->id;
542
    }
543
    if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
544
                           && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
545
        av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
546
        ret = AVERROR(EINVAL);
547
        goto free_and_end;
548
    }
549
    avctx->frame_number = 0;
550

    
551
    if (HAVE_THREADS && !avctx->thread_opaque) {
552
        ret = ff_thread_init(avctx);
553
        if (ret < 0) {
554
            goto free_and_end;
555
        }
556
    }
557

    
558
    if (avctx->codec->max_lowres < avctx->lowres) {
559
        av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
560
               avctx->codec->max_lowres);
561
        ret = AVERROR(EINVAL);
562
        goto free_and_end;
563
    }
564
    if (avctx->codec->encode) {
565
        int i;
566
        if (avctx->codec->sample_fmts) {
567
            for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++)
568
                if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
569
                    break;
570
            if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
571
                av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
572
                ret = AVERROR(EINVAL);
573
                goto free_and_end;
574
            }
575
        }
576
        if (avctx->codec->supported_samplerates) {
577
            for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
578
                if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
579
                    break;
580
            if (avctx->codec->supported_samplerates[i] == 0) {
581
                av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
582
                ret = AVERROR(EINVAL);
583
                goto free_and_end;
584
            }
585
        }
586
        if (avctx->codec->channel_layouts) {
587
            if (!avctx->channel_layout) {
588
                av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
589
            } else {
590
                for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
591
                    if (avctx->channel_layout == avctx->codec->channel_layouts[i])
592
                        break;
593
                if (avctx->codec->channel_layouts[i] == 0) {
594
                    av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
595
                    ret = AVERROR(EINVAL);
596
                    goto free_and_end;
597
                }
598
            }
599
        }
600
        if (avctx->channel_layout && avctx->channels) {
601
            if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
602
                av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
603
                ret = AVERROR(EINVAL);
604
                goto free_and_end;
605
            }
606
        } else if (avctx->channel_layout) {
607
            avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
608
        }
609
    }
610

    
611
    avctx->pts_correction_num_faulty_pts =
612
    avctx->pts_correction_num_faulty_dts = 0;
613
    avctx->pts_correction_last_pts =
614
    avctx->pts_correction_last_dts = INT64_MIN;
615

    
616
    if(avctx->codec->init && !(avctx->active_thread_type&FF_THREAD_FRAME)){
617
        ret = avctx->codec->init(avctx);
618
        if (ret < 0) {
619
            goto free_and_end;
620
        }
621
    }
622

    
623
    ret=0;
624
end:
625
    entangled_thread_counter--;
626

    
627
    /* Release any user-supplied mutex. */
628
    if (ff_lockmgr_cb) {
629
        (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
630
    }
631
    return ret;
632
free_and_end:
633
    av_freep(&avctx->priv_data);
634
    avctx->codec= NULL;
635
    goto end;
636
}
637

    
638
int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx, uint8_t *buf, int buf_size,
639
                         const short *samples)
640
{
641
    if(buf_size < FF_MIN_BUFFER_SIZE && 0){
642
        av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
643
        return -1;
644
    }
645
    if((avctx->codec->capabilities & CODEC_CAP_DELAY) || samples){
646
        int ret = avctx->codec->encode(avctx, buf, buf_size, samples);
647
        avctx->frame_number++;
648
        return ret;
649
    }else
650
        return 0;
651
}
652

    
653
int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
654
                         const AVFrame *pict)
655
{
656
    if(buf_size < FF_MIN_BUFFER_SIZE){
657
        av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
658
        return -1;
659
    }
660
    if(av_image_check_size(avctx->width, avctx->height, 0, avctx))
661
        return -1;
662
    if((avctx->codec->capabilities & CODEC_CAP_DELAY) || pict){
663
        int ret = avctx->codec->encode(avctx, buf, buf_size, pict);
664
        avctx->frame_number++;
665
        emms_c(); //needed to avoid an emms_c() call before every return;
666

    
667
        return ret;
668
    }else
669
        return 0;
670
}
671

    
672
int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
673
                            const AVSubtitle *sub)
674
{
675
    int ret;
676
    if(sub->start_display_time) {
677
        av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
678
        return -1;
679
    }
680
    if(sub->num_rects == 0 || !sub->rects)
681
        return -1;
682
    ret = avctx->codec->encode(avctx, buf, buf_size, sub);
683
    avctx->frame_number++;
684
    return ret;
685
}
686

    
687
/**
688
 * Attempt to guess proper monotonic timestamps for decoded video frames
689
 * which might have incorrect times. Input timestamps may wrap around, in
690
 * which case the output will as well.
691
 *
692
 * @param pts the pts field of the decoded AVPacket, as passed through
693
 * AVFrame.pkt_pts
694
 * @param dts the dts field of the decoded AVPacket
695
 * @return one of the input values, may be AV_NOPTS_VALUE
696
 */
697
static int64_t guess_correct_pts(AVCodecContext *ctx,
698
                                 int64_t reordered_pts, int64_t dts)
699
{
700
    int64_t pts = AV_NOPTS_VALUE;
701

    
702
    if (dts != AV_NOPTS_VALUE) {
703
        ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
704
        ctx->pts_correction_last_dts = dts;
705
    }
706
    if (reordered_pts != AV_NOPTS_VALUE) {
707
        ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
708
        ctx->pts_correction_last_pts = reordered_pts;
709
    }
710
    if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
711
       && reordered_pts != AV_NOPTS_VALUE)
712
        pts = reordered_pts;
713
    else
714
        pts = dts;
715

    
716
    return pts;
717
}
718

    
719
int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
720
                         int *got_picture_ptr,
721
                         AVPacket *avpkt)
722
{
723
    int ret;
724

    
725
    *got_picture_ptr= 0;
726
    if((avctx->coded_width||avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
727
        return -1;
728

    
729
    avctx->pkt = avpkt;
730

    
731
    if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type&FF_THREAD_FRAME)){
732
        if (HAVE_PTHREADS && avctx->active_thread_type&FF_THREAD_FRAME)
733
             ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
734
                                          avpkt);
735
        else {
736
            ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
737
                              avpkt);
738
            picture->pkt_dts= avpkt->dts;
739
            picture->pkt_pos= avpkt->pos;
740
        }
741

    
742
        emms_c(); //needed to avoid an emms_c() call before every return;
743

    
744

    
745
        if (*got_picture_ptr){
746
            avctx->frame_number++;
747
            picture->best_effort_timestamp = guess_correct_pts(avctx,
748
                                                            picture->pkt_pts,
749
                                                            picture->pkt_dts);
750
        }
751
    }else
752
        ret= 0;
753

    
754
    return ret;
755
}
756

    
757
int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
758
                         int *frame_size_ptr,
759
                         AVPacket *avpkt)
760
{
761
    int ret;
762

    
763
    avctx->pkt = avpkt;
764

    
765
    if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size){
766
        //FIXME remove the check below _after_ ensuring that all audio check that the available space is enough
767
        if(*frame_size_ptr < AVCODEC_MAX_AUDIO_FRAME_SIZE){
768
            av_log(avctx, AV_LOG_ERROR, "buffer smaller than AVCODEC_MAX_AUDIO_FRAME_SIZE\n");
769
            return -1;
770
        }
771
        if(*frame_size_ptr < FF_MIN_BUFFER_SIZE ||
772
        *frame_size_ptr < avctx->channels * avctx->frame_size * sizeof(int16_t)){
773
            av_log(avctx, AV_LOG_ERROR, "buffer %d too small\n", *frame_size_ptr);
774
            return -1;
775
        }
776

    
777
        ret = avctx->codec->decode(avctx, samples, frame_size_ptr, avpkt);
778
        avctx->frame_number++;
779
    }else{
780
        ret= 0;
781
        *frame_size_ptr=0;
782
    }
783
    return ret;
784
}
785

    
786
int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
787
                            int *got_sub_ptr,
788
                            AVPacket *avpkt)
789
{
790
    int ret;
791

    
792
    avctx->pkt = avpkt;
793
    *got_sub_ptr = 0;
794
    avcodec_get_subtitle_defaults(sub);
795
    ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
796
    if (*got_sub_ptr)
797
        avctx->frame_number++;
798
    return ret;
799
}
800

    
801
void avsubtitle_free(AVSubtitle *sub)
802
{
803
    int i;
804

    
805
    for (i = 0; i < sub->num_rects; i++)
806
    {
807
        av_freep(&sub->rects[i]->pict.data[0]);
808
        av_freep(&sub->rects[i]->pict.data[1]);
809
        av_freep(&sub->rects[i]->pict.data[2]);
810
        av_freep(&sub->rects[i]->pict.data[3]);
811
        av_freep(&sub->rects[i]->text);
812
        av_freep(&sub->rects[i]->ass);
813
        av_freep(&sub->rects[i]);
814
    }
815

    
816
    av_freep(&sub->rects);
817

    
818
    memset(sub, 0, sizeof(AVSubtitle));
819
}
820

    
821
av_cold int avcodec_close(AVCodecContext *avctx)
822
{
823
    /* If there is a user-supplied mutex locking routine, call it. */
824
    if (ff_lockmgr_cb) {
825
        if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
826
            return -1;
827
    }
828

    
829
    entangled_thread_counter++;
830
    if(entangled_thread_counter != 1){
831
        av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
832
        entangled_thread_counter--;
833
        return -1;
834
    }
835

    
836
    if (HAVE_THREADS && avctx->thread_opaque)
837
        ff_thread_free(avctx);
838
    if (avctx->codec && avctx->codec->close)
839
        avctx->codec->close(avctx);
840
    avcodec_default_free_buffers(avctx);
841
    avctx->coded_frame = NULL;
842
    av_freep(&avctx->priv_data);
843
    if(avctx->codec && avctx->codec->encode)
844
        av_freep(&avctx->extradata);
845
    avctx->codec = NULL;
846
    avctx->active_thread_type = 0;
847
    entangled_thread_counter--;
848

    
849
    /* Release any user-supplied mutex. */
850
    if (ff_lockmgr_cb) {
851
        (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
852
    }
853
    return 0;
854
}
855

    
856
AVCodec *avcodec_find_encoder(enum CodecID id)
857
{
858
    AVCodec *p, *experimental=NULL;
859
    p = first_avcodec;
860
    while (p) {
861
        if (p->encode != NULL && p->id == id) {
862
            if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
863
                experimental = p;
864
            } else
865
                return p;
866
        }
867
        p = p->next;
868
    }
869
    return experimental;
870
}
871

    
872
AVCodec *avcodec_find_encoder_by_name(const char *name)
873
{
874
    AVCodec *p;
875
    if (!name)
876
        return NULL;
877
    p = first_avcodec;
878
    while (p) {
879
        if (p->encode != NULL && strcmp(name,p->name) == 0)
880
            return p;
881
        p = p->next;
882
    }
883
    return NULL;
884
}
885

    
886
AVCodec *avcodec_find_decoder(enum CodecID id)
887
{
888
    AVCodec *p;
889
    p = first_avcodec;
890
    while (p) {
891
        if (p->decode != NULL && p->id == id)
892
            return p;
893
        p = p->next;
894
    }
895
    return NULL;
896
}
897

    
898
AVCodec *avcodec_find_decoder_by_name(const char *name)
899
{
900
    AVCodec *p;
901
    if (!name)
902
        return NULL;
903
    p = first_avcodec;
904
    while (p) {
905
        if (p->decode != NULL && strcmp(name,p->name) == 0)
906
            return p;
907
        p = p->next;
908
    }
909
    return NULL;
910
}
911

    
912
static int get_bit_rate(AVCodecContext *ctx)
913
{
914
    int bit_rate;
915
    int bits_per_sample;
916

    
917
    switch(ctx->codec_type) {
918
    case AVMEDIA_TYPE_VIDEO:
919
    case AVMEDIA_TYPE_DATA:
920
    case AVMEDIA_TYPE_SUBTITLE:
921
    case AVMEDIA_TYPE_ATTACHMENT:
922
        bit_rate = ctx->bit_rate;
923
        break;
924
    case AVMEDIA_TYPE_AUDIO:
925
        bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
926
        bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
927
        break;
928
    default:
929
        bit_rate = 0;
930
        break;
931
    }
932
    return bit_rate;
933
}
934

    
935
size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
936
{
937
    int i, len, ret = 0;
938

    
939
    for (i = 0; i < 4; i++) {
940
        len = snprintf(buf, buf_size,
941
                       isprint(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
942
        buf      += len;
943
        buf_size  = buf_size > len ? buf_size - len : 0;
944
        ret      += len;
945
        codec_tag>>=8;
946
    }
947
    return ret;
948
}
949

    
950
void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
951
{
952
    const char *codec_name;
953
    const char *profile = NULL;
954
    AVCodec *p;
955
    char buf1[32];
956
    int bitrate;
957
    AVRational display_aspect_ratio;
958

    
959
    if (encode)
960
        p = avcodec_find_encoder(enc->codec_id);
961
    else
962
        p = avcodec_find_decoder(enc->codec_id);
963

    
964
    if (p) {
965
        codec_name = p->name;
966
        profile = av_get_profile_name(p, enc->profile);
967
    } else if (enc->codec_id == CODEC_ID_MPEG2TS) {
968
        /* fake mpeg2 transport stream codec (currently not
969
           registered) */
970
        codec_name = "mpeg2ts";
971
    } else if (enc->codec_name[0] != '\0') {
972
        codec_name = enc->codec_name;
973
    } else {
974
        /* output avi tags */
975
        char tag_buf[32];
976
        av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
977
        snprintf(buf1, sizeof(buf1), "%s / 0x%04X", tag_buf, enc->codec_tag);
978
        codec_name = buf1;
979
    }
980

    
981
    switch(enc->codec_type) {
982
    case AVMEDIA_TYPE_VIDEO:
983
        snprintf(buf, buf_size,
984
                 "Video: %s%s",
985
                 codec_name, enc->mb_decision ? " (hq)" : "");
986
        if (profile)
987
            snprintf(buf + strlen(buf), buf_size - strlen(buf),
988
                     " (%s)", profile);
989
        if (enc->pix_fmt != PIX_FMT_NONE) {
990
            snprintf(buf + strlen(buf), buf_size - strlen(buf),
991
                     ", %s",
992
                     avcodec_get_pix_fmt_name(enc->pix_fmt));
993
        }
994
        if (enc->width) {
995
            snprintf(buf + strlen(buf), buf_size - strlen(buf),
996
                     ", %dx%d",
997
                     enc->width, enc->height);
998
            if (enc->sample_aspect_ratio.num) {
999
                av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
1000
                          enc->width*enc->sample_aspect_ratio.num,
1001
                          enc->height*enc->sample_aspect_ratio.den,
1002
                          1024*1024);
1003
                snprintf(buf + strlen(buf), buf_size - strlen(buf),
1004
                         " [PAR %d:%d DAR %d:%d]",
1005
                         enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
1006
                         display_aspect_ratio.num, display_aspect_ratio.den);
1007
            }
1008
            if(av_log_get_level() >= AV_LOG_DEBUG){
1009
                int g= av_gcd(enc->time_base.num, enc->time_base.den);
1010
                snprintf(buf + strlen(buf), buf_size - strlen(buf),
1011
                     ", %d/%d",
1012
                     enc->time_base.num/g, enc->time_base.den/g);
1013
            }
1014
        }
1015
        if (encode) {
1016
            snprintf(buf + strlen(buf), buf_size - strlen(buf),
1017
                     ", q=%d-%d", enc->qmin, enc->qmax);
1018
        }
1019
        break;
1020
    case AVMEDIA_TYPE_AUDIO:
1021
        snprintf(buf, buf_size,
1022
                 "Audio: %s",
1023
                 codec_name);
1024
        if (profile)
1025
            snprintf(buf + strlen(buf), buf_size - strlen(buf),
1026
                     " (%s)", profile);
1027
        if (enc->sample_rate) {
1028
            snprintf(buf + strlen(buf), buf_size - strlen(buf),
1029
                     ", %d Hz", enc->sample_rate);
1030
        }
1031
        av_strlcat(buf, ", ", buf_size);
1032
        av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
1033
        if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
1034
            snprintf(buf + strlen(buf), buf_size - strlen(buf),
1035
                     ", %s", av_get_sample_fmt_name(enc->sample_fmt));
1036
        }
1037
        break;
1038
    case AVMEDIA_TYPE_DATA:
1039
        snprintf(buf, buf_size, "Data: %s", codec_name);
1040
        break;
1041
    case AVMEDIA_TYPE_SUBTITLE:
1042
        snprintf(buf, buf_size, "Subtitle: %s", codec_name);
1043
        break;
1044
    case AVMEDIA_TYPE_ATTACHMENT:
1045
        snprintf(buf, buf_size, "Attachment: %s", codec_name);
1046
        break;
1047
    default:
1048
        snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
1049
        return;
1050
    }
1051
    if (encode) {
1052
        if (enc->flags & CODEC_FLAG_PASS1)
1053
            snprintf(buf + strlen(buf), buf_size - strlen(buf),
1054
                     ", pass 1");
1055
        if (enc->flags & CODEC_FLAG_PASS2)
1056
            snprintf(buf + strlen(buf), buf_size - strlen(buf),
1057
                     ", pass 2");
1058
    }
1059
    bitrate = get_bit_rate(enc);
1060
    if (bitrate != 0) {
1061
        snprintf(buf + strlen(buf), buf_size - strlen(buf),
1062
                 ", %d kb/s", bitrate / 1000);
1063
    }
1064
}
1065

    
1066
const char *av_get_profile_name(const AVCodec *codec, int profile)
1067
{
1068
    const AVProfile *p;
1069
    if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
1070
        return NULL;
1071

    
1072
    for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
1073
        if (p->profile == profile)
1074
            return p->name;
1075

    
1076
    return NULL;
1077
}
1078

    
1079
unsigned avcodec_version( void )
1080
{
1081
  return LIBAVCODEC_VERSION_INT;
1082
}
1083

    
1084
const char *avcodec_configuration(void)
1085
{
1086
    return FFMPEG_CONFIGURATION;
1087
}
1088

    
1089
const char *avcodec_license(void)
1090
{
1091
#define LICENSE_PREFIX "libavcodec license: "
1092
    return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
1093
}
1094

    
1095
void avcodec_init(void)
1096
{
1097
    static int initialized = 0;
1098

    
1099
    if (initialized != 0)
1100
        return;
1101
    initialized = 1;
1102

    
1103
    dsputil_static_init();
1104
}
1105

    
1106
void avcodec_flush_buffers(AVCodecContext *avctx)
1107
{
1108
    if(HAVE_PTHREADS && avctx->active_thread_type&FF_THREAD_FRAME)
1109
        ff_thread_flush(avctx);
1110
    if(avctx->codec->flush)
1111
        avctx->codec->flush(avctx);
1112
}
1113

    
1114
void avcodec_default_free_buffers(AVCodecContext *s){
1115
    int i, j;
1116

    
1117
    if(s->internal_buffer==NULL) return;
1118

    
1119
    if (s->internal_buffer_count)
1120
        av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n", s->internal_buffer_count);
1121
    for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
1122
        InternalBuffer *buf= &((InternalBuffer*)s->internal_buffer)[i];
1123
        for(j=0; j<4; j++){
1124
            av_freep(&buf->base[j]);
1125
            buf->data[j]= NULL;
1126
        }
1127
    }
1128
    av_freep(&s->internal_buffer);
1129

    
1130
    s->internal_buffer_count=0;
1131
}
1132

    
1133
#if FF_API_OLD_FF_PICT_TYPES
1134
char av_get_pict_type_char(int pict_type){
1135
    return av_get_picture_type_char(pict_type);
1136
}
1137
#endif
1138

    
1139
int av_get_bits_per_sample(enum CodecID codec_id){
1140
    switch(codec_id){
1141
    case CODEC_ID_ADPCM_SBPRO_2:
1142
        return 2;
1143
    case CODEC_ID_ADPCM_SBPRO_3:
1144
        return 3;
1145
    case CODEC_ID_ADPCM_SBPRO_4:
1146
    case CODEC_ID_ADPCM_CT:
1147
    case CODEC_ID_ADPCM_IMA_WAV:
1148
    case CODEC_ID_ADPCM_MS:
1149
    case CODEC_ID_ADPCM_YAMAHA:
1150
        return 4;
1151
    case CODEC_ID_ADPCM_G722:
1152
    case CODEC_ID_PCM_ALAW:
1153
    case CODEC_ID_PCM_MULAW:
1154
    case CODEC_ID_PCM_S8:
1155
    case CODEC_ID_PCM_U8:
1156
    case CODEC_ID_PCM_ZORK:
1157
        return 8;
1158
    case CODEC_ID_PCM_S16BE:
1159
    case CODEC_ID_PCM_S16LE:
1160
    case CODEC_ID_PCM_S16LE_PLANAR:
1161
    case CODEC_ID_PCM_U16BE:
1162
    case CODEC_ID_PCM_U16LE:
1163
        return 16;
1164
    case CODEC_ID_PCM_S24DAUD:
1165
    case CODEC_ID_PCM_S24BE:
1166
    case CODEC_ID_PCM_S24LE:
1167
    case CODEC_ID_PCM_U24BE:
1168
    case CODEC_ID_PCM_U24LE:
1169
        return 24;
1170
    case CODEC_ID_PCM_S32BE:
1171
    case CODEC_ID_PCM_S32LE:
1172
    case CODEC_ID_PCM_U32BE:
1173
    case CODEC_ID_PCM_U32LE:
1174
    case CODEC_ID_PCM_F32BE:
1175
    case CODEC_ID_PCM_F32LE:
1176
        return 32;
1177
    case CODEC_ID_PCM_F64BE:
1178
    case CODEC_ID_PCM_F64LE:
1179
        return 64;
1180
    default:
1181
        return 0;
1182
    }
1183
}
1184

    
1185
#if FF_API_OLD_SAMPLE_FMT
1186
int av_get_bits_per_sample_format(enum AVSampleFormat sample_fmt) {
1187
    return av_get_bits_per_sample_fmt(sample_fmt);
1188
}
1189
#endif
1190

    
1191
#if !HAVE_THREADS
1192
int ff_thread_init(AVCodecContext *s){
1193
    return -1;
1194
}
1195
#endif
1196

    
1197
unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
1198
{
1199
    unsigned int n = 0;
1200

    
1201
    while(v >= 0xff) {
1202
        *s++ = 0xff;
1203
        v -= 0xff;
1204
        n++;
1205
    }
1206
    *s = v;
1207
    n++;
1208
    return n;
1209
}
1210

    
1211
int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
1212
    int i;
1213
    for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
1214
    return i;
1215
}
1216

    
1217
void av_log_missing_feature(void *avc, const char *feature, int want_sample)
1218
{
1219
    av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your FFmpeg "
1220
            "version to the newest one from Git. If the problem still "
1221
            "occurs, it means that your file has a feature which has not "
1222
            "been implemented.\n", feature);
1223
    if(want_sample)
1224
        av_log_ask_for_sample(avc, NULL);
1225
}
1226

    
1227
void av_log_ask_for_sample(void *avc, const char *msg, ...)
1228
{
1229
    va_list argument_list;
1230

    
1231
    va_start(argument_list, msg);
1232

    
1233
    if (msg)
1234
        av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
1235
    av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
1236
            "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
1237
            "and contact the ffmpeg-devel mailing list.\n");
1238

    
1239
    va_end(argument_list);
1240
}
1241

    
1242
static AVHWAccel *first_hwaccel = NULL;
1243

    
1244
void av_register_hwaccel(AVHWAccel *hwaccel)
1245
{
1246
    AVHWAccel **p = &first_hwaccel;
1247
    while (*p)
1248
        p = &(*p)->next;
1249
    *p = hwaccel;
1250
    hwaccel->next = NULL;
1251
}
1252

    
1253
AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
1254
{
1255
    return hwaccel ? hwaccel->next : first_hwaccel;
1256
}
1257

    
1258
AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
1259
{
1260
    AVHWAccel *hwaccel=NULL;
1261

    
1262
    while((hwaccel= av_hwaccel_next(hwaccel))){
1263
        if (   hwaccel->id      == codec_id
1264
            && hwaccel->pix_fmt == pix_fmt)
1265
            return hwaccel;
1266
    }
1267
    return NULL;
1268
}
1269

    
1270
int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
1271
{
1272
    if (ff_lockmgr_cb) {
1273
        if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
1274
            return -1;
1275
    }
1276

    
1277
    ff_lockmgr_cb = cb;
1278

    
1279
    if (ff_lockmgr_cb) {
1280
        if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
1281
            return -1;
1282
    }
1283
    return 0;
1284
}
1285

    
1286
unsigned int ff_toupper4(unsigned int x)
1287
{
1288
    return     toupper( x     &0xFF)
1289
    + (toupper((x>>8 )&0xFF)<<8 )
1290
    + (toupper((x>>16)&0xFF)<<16)
1291
    + (toupper((x>>24)&0xFF)<<24);
1292
}
1293

    
1294
#if !HAVE_PTHREADS
1295

    
1296
int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
1297
{
1298
    f->owner = avctx;
1299
    return avctx->get_buffer(avctx, f);
1300
}
1301

    
1302
void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
1303
{
1304
    f->owner->release_buffer(f->owner, f);
1305
}
1306

    
1307
void ff_thread_finish_setup(AVCodecContext *avctx)
1308
{
1309
}
1310

    
1311
void ff_thread_report_progress(AVFrame *f, int progress, int field)
1312
{
1313
}
1314

    
1315
void ff_thread_await_progress(AVFrame *f, int progress, int field)
1316
{
1317
}
1318

    
1319
#endif
1320

    
1321
#if FF_API_THREAD_INIT
1322
int avcodec_thread_init(AVCodecContext *s, int thread_count)
1323
{
1324
    s->thread_count = thread_count;
1325
    return ff_thread_init(s);
1326
}
1327
#endif