Statistics
| Branch: | Revision:

ffmpeg / libavcodec / utils.c @ 18ded93a

History | View | Annotate | Download (39.4 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
    pic->sample_aspect_ratio = (AVRational){0, 1};
459
    pic->format = -1;           /* unknown */
460
}
461

    
462
AVFrame *avcodec_alloc_frame(void){
463
    AVFrame *pic= av_malloc(sizeof(AVFrame));
464

    
465
    if(pic==NULL) return NULL;
466

    
467
    avcodec_get_frame_defaults(pic);
468

    
469
    return pic;
470
}
471

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

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

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

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

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

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

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

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

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

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

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

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

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

    
613
    avctx->pts_correction_num_faulty_pts =
614
    avctx->pts_correction_num_faulty_dts = 0;
615
    avctx->pts_correction_last_pts =
616
    avctx->pts_correction_last_dts = INT64_MIN;
617

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

    
625
    ret=0;
626
end:
627
    entangled_thread_counter--;
628

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

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

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

    
669
        return ret;
670
    }else
671
        return 0;
672
}
673

    
674
int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
675
                            const AVSubtitle *sub)
676
{
677
    int ret;
678
    if(sub->start_display_time) {
679
        av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
680
        return -1;
681
    }
682

    
683
    ret = avctx->codec->encode(avctx, buf, buf_size, sub);
684
    avctx->frame_number++;
685
    return ret;
686
}
687

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

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

    
717
    return pts;
718
}
719

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

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

    
730
    avctx->pkt = avpkt;
731

    
732
    if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type&FF_THREAD_FRAME)){
733
        if (HAVE_PTHREADS && avctx->active_thread_type&FF_THREAD_FRAME)
734
             ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
735
                                          avpkt);
736
        else {
737
            ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
738
                              avpkt);
739
            picture->pkt_dts= avpkt->dts;
740
            picture->pkt_pos= avpkt->pos;
741
            if (!picture->sample_aspect_ratio.num)
742
                picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
743
            if (!picture->width)
744
                picture->width = avctx->width;
745
            if (!picture->height)
746
                picture->height = avctx->height;
747
            if (picture->format == PIX_FMT_NONE)
748
                picture->format = avctx->pix_fmt;
749
        }
750

    
751
        emms_c(); //needed to avoid an emms_c() call before every return;
752

    
753

    
754
        if (*got_picture_ptr){
755
            avctx->frame_number++;
756
            picture->best_effort_timestamp = guess_correct_pts(avctx,
757
                                                            picture->pkt_pts,
758
                                                            picture->pkt_dts);
759
        }
760
    }else
761
        ret= 0;
762

    
763
    return ret;
764
}
765

    
766
int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
767
                         int *frame_size_ptr,
768
                         AVPacket *avpkt)
769
{
770
    int ret;
771

    
772
    avctx->pkt = avpkt;
773

    
774
    if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size){
775
        //FIXME remove the check below _after_ ensuring that all audio check that the available space is enough
776
        if(*frame_size_ptr < AVCODEC_MAX_AUDIO_FRAME_SIZE){
777
            av_log(avctx, AV_LOG_ERROR, "buffer smaller than AVCODEC_MAX_AUDIO_FRAME_SIZE\n");
778
            return -1;
779
        }
780
        if(*frame_size_ptr < FF_MIN_BUFFER_SIZE ||
781
        *frame_size_ptr < avctx->channels * avctx->frame_size * sizeof(int16_t)){
782
            av_log(avctx, AV_LOG_ERROR, "buffer %d too small\n", *frame_size_ptr);
783
            return -1;
784
        }
785

    
786
        ret = avctx->codec->decode(avctx, samples, frame_size_ptr, avpkt);
787
        avctx->frame_number++;
788
    }else{
789
        ret= 0;
790
        *frame_size_ptr=0;
791
    }
792
    return ret;
793
}
794

    
795
int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
796
                            int *got_sub_ptr,
797
                            AVPacket *avpkt)
798
{
799
    int ret;
800

    
801
    avctx->pkt = avpkt;
802
    *got_sub_ptr = 0;
803
    avcodec_get_subtitle_defaults(sub);
804
    ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
805
    if (*got_sub_ptr)
806
        avctx->frame_number++;
807
    return ret;
808
}
809

    
810
void avsubtitle_free(AVSubtitle *sub)
811
{
812
    int i;
813

    
814
    for (i = 0; i < sub->num_rects; i++)
815
    {
816
        av_freep(&sub->rects[i]->pict.data[0]);
817
        av_freep(&sub->rects[i]->pict.data[1]);
818
        av_freep(&sub->rects[i]->pict.data[2]);
819
        av_freep(&sub->rects[i]->pict.data[3]);
820
        av_freep(&sub->rects[i]->text);
821
        av_freep(&sub->rects[i]->ass);
822
        av_freep(&sub->rects[i]);
823
    }
824

    
825
    av_freep(&sub->rects);
826

    
827
    memset(sub, 0, sizeof(AVSubtitle));
828
}
829

    
830
av_cold int avcodec_close(AVCodecContext *avctx)
831
{
832
    /* If there is a user-supplied mutex locking routine, call it. */
833
    if (ff_lockmgr_cb) {
834
        if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
835
            return -1;
836
    }
837

    
838
    entangled_thread_counter++;
839
    if(entangled_thread_counter != 1){
840
        av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
841
        entangled_thread_counter--;
842
        return -1;
843
    }
844

    
845
    if (HAVE_THREADS && avctx->thread_opaque)
846
        ff_thread_free(avctx);
847
    if (avctx->codec && avctx->codec->close)
848
        avctx->codec->close(avctx);
849
    avcodec_default_free_buffers(avctx);
850
    avctx->coded_frame = NULL;
851
    av_freep(&avctx->priv_data);
852
    if(avctx->codec && avctx->codec->encode)
853
        av_freep(&avctx->extradata);
854
    avctx->codec = NULL;
855
    avctx->active_thread_type = 0;
856
    entangled_thread_counter--;
857

    
858
    /* Release any user-supplied mutex. */
859
    if (ff_lockmgr_cb) {
860
        (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
861
    }
862
    return 0;
863
}
864

    
865
AVCodec *avcodec_find_encoder(enum CodecID id)
866
{
867
    AVCodec *p, *experimental=NULL;
868
    p = first_avcodec;
869
    while (p) {
870
        if (p->encode != NULL && p->id == id) {
871
            if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
872
                experimental = p;
873
            } else
874
                return p;
875
        }
876
        p = p->next;
877
    }
878
    return experimental;
879
}
880

    
881
AVCodec *avcodec_find_encoder_by_name(const char *name)
882
{
883
    AVCodec *p;
884
    if (!name)
885
        return NULL;
886
    p = first_avcodec;
887
    while (p) {
888
        if (p->encode != NULL && strcmp(name,p->name) == 0)
889
            return p;
890
        p = p->next;
891
    }
892
    return NULL;
893
}
894

    
895
AVCodec *avcodec_find_decoder(enum CodecID id)
896
{
897
    AVCodec *p;
898
    p = first_avcodec;
899
    while (p) {
900
        if (p->decode != NULL && p->id == id)
901
            return p;
902
        p = p->next;
903
    }
904
    return NULL;
905
}
906

    
907
AVCodec *avcodec_find_decoder_by_name(const char *name)
908
{
909
    AVCodec *p;
910
    if (!name)
911
        return NULL;
912
    p = first_avcodec;
913
    while (p) {
914
        if (p->decode != NULL && strcmp(name,p->name) == 0)
915
            return p;
916
        p = p->next;
917
    }
918
    return NULL;
919
}
920

    
921
static int get_bit_rate(AVCodecContext *ctx)
922
{
923
    int bit_rate;
924
    int bits_per_sample;
925

    
926
    switch(ctx->codec_type) {
927
    case AVMEDIA_TYPE_VIDEO:
928
    case AVMEDIA_TYPE_DATA:
929
    case AVMEDIA_TYPE_SUBTITLE:
930
    case AVMEDIA_TYPE_ATTACHMENT:
931
        bit_rate = ctx->bit_rate;
932
        break;
933
    case AVMEDIA_TYPE_AUDIO:
934
        bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
935
        bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
936
        break;
937
    default:
938
        bit_rate = 0;
939
        break;
940
    }
941
    return bit_rate;
942
}
943

    
944
size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
945
{
946
    int i, len, ret = 0;
947

    
948
    for (i = 0; i < 4; i++) {
949
        len = snprintf(buf, buf_size,
950
                       isprint(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
951
        buf      += len;
952
        buf_size  = buf_size > len ? buf_size - len : 0;
953
        ret      += len;
954
        codec_tag>>=8;
955
    }
956
    return ret;
957
}
958

    
959
void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
960
{
961
    const char *codec_name;
962
    const char *profile = NULL;
963
    AVCodec *p;
964
    char buf1[32];
965
    int bitrate;
966
    AVRational display_aspect_ratio;
967

    
968
    if (encode)
969
        p = avcodec_find_encoder(enc->codec_id);
970
    else
971
        p = avcodec_find_decoder(enc->codec_id);
972

    
973
    if (p) {
974
        codec_name = p->name;
975
        profile = av_get_profile_name(p, enc->profile);
976
    } else if (enc->codec_id == CODEC_ID_MPEG2TS) {
977
        /* fake mpeg2 transport stream codec (currently not
978
           registered) */
979
        codec_name = "mpeg2ts";
980
    } else if (enc->codec_name[0] != '\0') {
981
        codec_name = enc->codec_name;
982
    } else {
983
        /* output avi tags */
984
        char tag_buf[32];
985
        av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
986
        snprintf(buf1, sizeof(buf1), "%s / 0x%04X", tag_buf, enc->codec_tag);
987
        codec_name = buf1;
988
    }
989

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

    
1075
const char *av_get_profile_name(const AVCodec *codec, int profile)
1076
{
1077
    const AVProfile *p;
1078
    if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
1079
        return NULL;
1080

    
1081
    for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
1082
        if (p->profile == profile)
1083
            return p->name;
1084

    
1085
    return NULL;
1086
}
1087

    
1088
unsigned avcodec_version( void )
1089
{
1090
  return LIBAVCODEC_VERSION_INT;
1091
}
1092

    
1093
const char *avcodec_configuration(void)
1094
{
1095
    return FFMPEG_CONFIGURATION;
1096
}
1097

    
1098
const char *avcodec_license(void)
1099
{
1100
#define LICENSE_PREFIX "libavcodec license: "
1101
    return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
1102
}
1103

    
1104
void avcodec_init(void)
1105
{
1106
    static int initialized = 0;
1107

    
1108
    if (initialized != 0)
1109
        return;
1110
    initialized = 1;
1111

    
1112
    dsputil_static_init();
1113
}
1114

    
1115
void avcodec_flush_buffers(AVCodecContext *avctx)
1116
{
1117
    if(HAVE_PTHREADS && avctx->active_thread_type&FF_THREAD_FRAME)
1118
        ff_thread_flush(avctx);
1119
    if(avctx->codec->flush)
1120
        avctx->codec->flush(avctx);
1121
}
1122

    
1123
void avcodec_default_free_buffers(AVCodecContext *s){
1124
    int i, j;
1125

    
1126
    if(s->internal_buffer==NULL) return;
1127

    
1128
    if (s->internal_buffer_count)
1129
        av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n", s->internal_buffer_count);
1130
    for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
1131
        InternalBuffer *buf= &((InternalBuffer*)s->internal_buffer)[i];
1132
        for(j=0; j<4; j++){
1133
            av_freep(&buf->base[j]);
1134
            buf->data[j]= NULL;
1135
        }
1136
    }
1137
    av_freep(&s->internal_buffer);
1138

    
1139
    s->internal_buffer_count=0;
1140
}
1141

    
1142
#if FF_API_OLD_FF_PICT_TYPES
1143
char av_get_pict_type_char(int pict_type){
1144
    return av_get_picture_type_char(pict_type);
1145
}
1146
#endif
1147

    
1148
int av_get_bits_per_sample(enum CodecID codec_id){
1149
    switch(codec_id){
1150
    case CODEC_ID_ADPCM_SBPRO_2:
1151
        return 2;
1152
    case CODEC_ID_ADPCM_SBPRO_3:
1153
        return 3;
1154
    case CODEC_ID_ADPCM_SBPRO_4:
1155
    case CODEC_ID_ADPCM_CT:
1156
    case CODEC_ID_ADPCM_IMA_WAV:
1157
    case CODEC_ID_ADPCM_MS:
1158
    case CODEC_ID_ADPCM_YAMAHA:
1159
        return 4;
1160
    case CODEC_ID_ADPCM_G722:
1161
    case CODEC_ID_PCM_ALAW:
1162
    case CODEC_ID_PCM_MULAW:
1163
    case CODEC_ID_PCM_S8:
1164
    case CODEC_ID_PCM_U8:
1165
    case CODEC_ID_PCM_ZORK:
1166
        return 8;
1167
    case CODEC_ID_PCM_S16BE:
1168
    case CODEC_ID_PCM_S16LE:
1169
    case CODEC_ID_PCM_S16LE_PLANAR:
1170
    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
    case CODEC_ID_PCM_F32BE:
1184
    case CODEC_ID_PCM_F32LE:
1185
        return 32;
1186
    case CODEC_ID_PCM_F64BE:
1187
    case CODEC_ID_PCM_F64LE:
1188
        return 64;
1189
    default:
1190
        return 0;
1191
    }
1192
}
1193

    
1194
#if FF_API_OLD_SAMPLE_FMT
1195
int av_get_bits_per_sample_format(enum AVSampleFormat sample_fmt) {
1196
    return av_get_bits_per_sample_fmt(sample_fmt);
1197
}
1198
#endif
1199

    
1200
#if !HAVE_THREADS
1201
int ff_thread_init(AVCodecContext *s){
1202
    return -1;
1203
}
1204
#endif
1205

    
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

    
1220
int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
1221
    int i;
1222
    for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
1223
    return i;
1224
}
1225

    
1226
void av_log_missing_feature(void *avc, const char *feature, int want_sample)
1227
{
1228
    av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your FFmpeg "
1229
            "version to the newest one from Git. If the problem still "
1230
            "occurs, it means that your file has a feature which has not "
1231
            "been implemented.\n", feature);
1232
    if(want_sample)
1233
        av_log_ask_for_sample(avc, NULL);
1234
}
1235

    
1236
void av_log_ask_for_sample(void *avc, const char *msg, ...)
1237
{
1238
    va_list argument_list;
1239

    
1240
    va_start(argument_list, msg);
1241

    
1242
    if (msg)
1243
        av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
1244
    av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
1245
            "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
1246
            "and contact the ffmpeg-devel mailing list.\n");
1247

    
1248
    va_end(argument_list);
1249
}
1250

    
1251
static AVHWAccel *first_hwaccel = NULL;
1252

    
1253
void av_register_hwaccel(AVHWAccel *hwaccel)
1254
{
1255
    AVHWAccel **p = &first_hwaccel;
1256
    while (*p)
1257
        p = &(*p)->next;
1258
    *p = hwaccel;
1259
    hwaccel->next = NULL;
1260
}
1261

    
1262
AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
1263
{
1264
    return hwaccel ? hwaccel->next : first_hwaccel;
1265
}
1266

    
1267
AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
1268
{
1269
    AVHWAccel *hwaccel=NULL;
1270

    
1271
    while((hwaccel= av_hwaccel_next(hwaccel))){
1272
        if (   hwaccel->id      == codec_id
1273
            && hwaccel->pix_fmt == pix_fmt)
1274
            return hwaccel;
1275
    }
1276
    return NULL;
1277
}
1278

    
1279
int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
1280
{
1281
    if (ff_lockmgr_cb) {
1282
        if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
1283
            return -1;
1284
    }
1285

    
1286
    ff_lockmgr_cb = cb;
1287

    
1288
    if (ff_lockmgr_cb) {
1289
        if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
1290
            return -1;
1291
    }
1292
    return 0;
1293
}
1294

    
1295
unsigned int ff_toupper4(unsigned int x)
1296
{
1297
    return     toupper( x     &0xFF)
1298
    + (toupper((x>>8 )&0xFF)<<8 )
1299
    + (toupper((x>>16)&0xFF)<<16)
1300
    + (toupper((x>>24)&0xFF)<<24);
1301
}
1302

    
1303
#if !HAVE_PTHREADS
1304

    
1305
int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
1306
{
1307
    f->owner = avctx;
1308
    return avctx->get_buffer(avctx, f);
1309
}
1310

    
1311
void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
1312
{
1313
    f->owner->release_buffer(f->owner, f);
1314
}
1315

    
1316
void ff_thread_finish_setup(AVCodecContext *avctx)
1317
{
1318
}
1319

    
1320
void ff_thread_report_progress(AVFrame *f, int progress, int field)
1321
{
1322
}
1323

    
1324
void ff_thread_await_progress(AVFrame *f, int progress, int field)
1325
{
1326
}
1327

    
1328
#endif
1329

    
1330
#if FF_API_THREAD_INIT
1331
int avcodec_thread_init(AVCodecContext *s, int thread_count)
1332
{
1333
    s->thread_count = thread_count;
1334
    return ff_thread_init(s);
1335
}
1336
#endif