Statistics
| Branch: | Revision:

ffmpeg / libavcodec / utils.c @ 2d2b5a14

History | View | Annotate | Download (39.2 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, FF_INTERNALC_MEM_TYPE 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, FF_INTERNALC_MEM_TYPE 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
#if LIBAVCODEC_VERSION_MAJOR < 53
98
void register_avcodec(AVCodec *codec)
99
{
100
    avcodec_register(codec);
101
}
102
#endif
103

    
104
unsigned avcodec_get_edge_width(void)
105
{
106
    return EDGE_WIDTH;
107
}
108

    
109
void avcodec_set_dimensions(AVCodecContext *s, int width, int height){
110
    s->coded_width = width;
111
    s->coded_height= height;
112
    s->width = -((-width )>>s->lowres);
113
    s->height= -((-height)>>s->lowres);
114
}
115

    
116
typedef struct InternalBuffer{
117
    int last_pic_num;
118
    uint8_t *base[4];
119
    uint8_t *data[4];
120
    int linesize[4];
121
    int width, height;
122
    enum PixelFormat pix_fmt;
123
}InternalBuffer;
124

    
125
#define INTERNAL_BUFFER_SIZE 32
126

    
127
void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height, int linesize_align[4]){
128
    int w_align= 1;
129
    int h_align= 1;
130

    
131
    switch(s->pix_fmt){
132
    case PIX_FMT_YUV420P:
133
    case PIX_FMT_YUYV422:
134
    case PIX_FMT_UYVY422:
135
    case PIX_FMT_YUV422P:
136
    case PIX_FMT_YUV440P:
137
    case PIX_FMT_YUV444P:
138
    case PIX_FMT_GRAY8:
139
    case PIX_FMT_GRAY16BE:
140
    case PIX_FMT_GRAY16LE:
141
    case PIX_FMT_YUVJ420P:
142
    case PIX_FMT_YUVJ422P:
143
    case PIX_FMT_YUVJ440P:
144
    case PIX_FMT_YUVJ444P:
145
    case PIX_FMT_YUVA420P:
146
    case PIX_FMT_YUV420P9LE:
147
    case PIX_FMT_YUV420P9BE:
148
    case PIX_FMT_YUV420P10LE:
149
    case PIX_FMT_YUV420P10BE:
150
        w_align= 16; //FIXME check for non mpeg style codecs and use less alignment
151
        h_align= 16;
152
        if(s->codec_id == CODEC_ID_MPEG2VIDEO || s->codec_id == CODEC_ID_MJPEG || s->codec_id == CODEC_ID_AMV || s->codec_id == CODEC_ID_THP || s->codec_id == CODEC_ID_H264)
153
            h_align= 32; // interlaced is rounded up to 2 MBs
154
        break;
155
    case PIX_FMT_YUV411P:
156
    case PIX_FMT_UYYVYY411:
157
        w_align=32;
158
        h_align=8;
159
        break;
160
    case PIX_FMT_YUV410P:
161
        if(s->codec_id == CODEC_ID_SVQ1){
162
            w_align=64;
163
            h_align=64;
164
        }
165
    case PIX_FMT_RGB555:
166
        if(s->codec_id == CODEC_ID_RPZA){
167
            w_align=4;
168
            h_align=4;
169
        }
170
    case PIX_FMT_PAL8:
171
    case PIX_FMT_BGR8:
172
    case PIX_FMT_RGB8:
173
        if(s->codec_id == CODEC_ID_SMC){
174
            w_align=4;
175
            h_align=4;
176
        }
177
        break;
178
    case PIX_FMT_BGR24:
179
        if((s->codec_id == CODEC_ID_MSZH) || (s->codec_id == CODEC_ID_ZLIB)){
180
            w_align=4;
181
            h_align=4;
182
        }
183
        break;
184
    default:
185
        w_align= 1;
186
        h_align= 1;
187
        break;
188
    }
189

    
190
    *width = FFALIGN(*width , w_align);
191
    *height= FFALIGN(*height, h_align);
192
    if(s->codec_id == CODEC_ID_H264 || s->lowres)
193
        *height+=2; // some of the optimized chroma MC reads one line too much
194
                    // which is also done in mpeg decoders with lowres > 0
195

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

    
215
void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height){
216
    int chroma_shift = av_pix_fmt_descriptors[s->pix_fmt].log2_chroma_w;
217
    int linesize_align[4];
218
    int align;
219
    avcodec_align_dimensions2(s, width, height, linesize_align);
220
    align = FFMAX(linesize_align[0], linesize_align[3]);
221
    linesize_align[1] <<= chroma_shift;
222
    linesize_align[2] <<= chroma_shift;
223
    align = FFMAX3(align, linesize_align[1], linesize_align[2]);
224
    *width=FFALIGN(*width, align);
225
}
226

    
227
#if LIBAVCODEC_VERSION_MAJOR < 53
228
int avcodec_check_dimensions(void *av_log_ctx, unsigned int w, unsigned int h){
229
    return av_image_check_size(w, h, 0, av_log_ctx);
230
}
231
#endif
232

    
233
int avcodec_default_get_buffer(AVCodecContext *s, AVFrame *pic){
234
    int i;
235
    int w= s->width;
236
    int h= s->height;
237
    InternalBuffer *buf;
238
    int *picture_number;
239

    
240
    if(pic->data[0]!=NULL) {
241
        av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
242
        return -1;
243
    }
244
    if(s->internal_buffer_count >= INTERNAL_BUFFER_SIZE) {
245
        av_log(s, AV_LOG_ERROR, "internal_buffer_count overflow (missing release_buffer?)\n");
246
        return -1;
247
    }
248

    
249
    if(av_image_check_size(w, h, 0, s))
250
        return -1;
251

    
252
    if(s->internal_buffer==NULL){
253
        s->internal_buffer= av_mallocz((INTERNAL_BUFFER_SIZE+1)*sizeof(InternalBuffer));
254
    }
255
#if 0
256
    s->internal_buffer= av_fast_realloc(
257
        s->internal_buffer,
258
        &s->internal_buffer_size,
259
        sizeof(InternalBuffer)*FFMAX(99,  s->internal_buffer_count+1)/*FIXME*/
260
        );
261
#endif
262

    
263
    buf= &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
264
    picture_number= &(((InternalBuffer*)s->internal_buffer)[INTERNAL_BUFFER_SIZE]).last_pic_num; //FIXME ugly hack
265
    (*picture_number)++;
266

    
267
    if(buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)){
268
        if(s->active_thread_type&FF_THREAD_FRAME) {
269
            av_log_missing_feature(s, "Width/height changing with frame threads is", 0);
270
            return -1;
271
        }
272

    
273
        for(i=0; i<4; i++){
274
            av_freep(&buf->base[i]);
275
            buf->data[i]= NULL;
276
        }
277
    }
278

    
279
    if(buf->base[0]){
280
        pic->age= *picture_number - buf->last_pic_num;
281
        buf->last_pic_num= *picture_number;
282
    }else{
283
        int h_chroma_shift, v_chroma_shift;
284
        int size[4] = {0};
285
        int tmpsize;
286
        int unaligned;
287
        AVPicture picture;
288
        int stride_align[4];
289
        const int pixel_size = av_pix_fmt_descriptors[s->pix_fmt].comp[0].step_minus1+1;
290

    
291
        avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
292

    
293
        avcodec_align_dimensions2(s, &w, &h, stride_align);
294

    
295
        if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
296
            w+= EDGE_WIDTH*2;
297
            h+= EDGE_WIDTH*2;
298
        }
299

    
300
        do {
301
            // NOTE: do not align linesizes individually, this breaks e.g. assumptions
302
            // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
303
            av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
304
            // increase alignment of w for next try (rhs gives the lowest bit set in w)
305
            w += w & ~(w-1);
306

    
307
            unaligned = 0;
308
            for (i=0; i<4; i++){
309
                unaligned |= picture.linesize[i] % stride_align[i];
310
            }
311
        } while (unaligned);
312

    
313
        tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
314
        if (tmpsize < 0)
315
            return -1;
316

    
317
        for (i=0; i<3 && picture.data[i+1]; i++)
318
            size[i] = picture.data[i+1] - picture.data[i];
319
        size[i] = tmpsize - (picture.data[i] - picture.data[0]);
320

    
321
        buf->last_pic_num= -256*256*256*64;
322
        memset(buf->base, 0, sizeof(buf->base));
323
        memset(buf->data, 0, sizeof(buf->data));
324

    
325
        for(i=0; i<4 && size[i]; i++){
326
            const int h_shift= i==0 ? 0 : h_chroma_shift;
327
            const int v_shift= i==0 ? 0 : v_chroma_shift;
328

    
329
            buf->linesize[i]= picture.linesize[i];
330

    
331
            buf->base[i]= av_malloc(size[i]+16); //FIXME 16
332
            if(buf->base[i]==NULL) return -1;
333
            memset(buf->base[i], 128, size[i]);
334

    
335
            // no edge if EDGE EMU or not planar YUV
336
            if((s->flags&CODEC_FLAG_EMU_EDGE) || !size[2])
337
                buf->data[i] = buf->base[i];
338
            else
339
                buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (pixel_size*EDGE_WIDTH>>h_shift), stride_align[i]);
340
        }
341
        if(size[1] && !size[2])
342
            ff_set_systematic_pal2((uint32_t*)buf->data[1], s->pix_fmt);
343
        buf->width  = s->width;
344
        buf->height = s->height;
345
        buf->pix_fmt= s->pix_fmt;
346
        pic->age= 256*256*256*64;
347
    }
348
    pic->type= FF_BUFFER_TYPE_INTERNAL;
349

    
350
    for(i=0; i<4; i++){
351
        pic->base[i]= buf->base[i];
352
        pic->data[i]= buf->data[i];
353
        pic->linesize[i]= buf->linesize[i];
354
    }
355
    s->internal_buffer_count++;
356

    
357
    if(s->pkt) pic->pkt_pts= s->pkt->pts;
358
    else       pic->pkt_pts= AV_NOPTS_VALUE;
359
    pic->reordered_opaque= s->reordered_opaque;
360

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

    
364
    return 0;
365
}
366

    
367
void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
368
    int i;
369
    InternalBuffer *buf, *last;
370

    
371
    assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
372
    assert(s->internal_buffer_count);
373

    
374
    if(s->internal_buffer){
375
    buf = NULL; /* avoids warning */
376
    for(i=0; i<s->internal_buffer_count; i++){ //just 3-5 checks so is not worth to optimize
377
        buf= &((InternalBuffer*)s->internal_buffer)[i];
378
        if(buf->data[0] == pic->data[0])
379
            break;
380
    }
381
    assert(i < s->internal_buffer_count);
382
    s->internal_buffer_count--;
383
    last = &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
384

    
385
    FFSWAP(InternalBuffer, *buf, *last);
386
    }
387

    
388
    for(i=0; i<4; i++){
389
        pic->data[i]=NULL;
390
//        pic->base[i]=NULL;
391
    }
392
//printf("R%X\n", pic->opaque);
393

    
394
    if(s->debug&FF_DEBUG_BUFFERS)
395
        av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d buffers used\n", pic, s->internal_buffer_count);
396
}
397

    
398
int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
399
    AVFrame temp_pic;
400
    int i;
401

    
402
    /* If no picture return a new buffer */
403
    if(pic->data[0] == NULL) {
404
        /* We will copy from buffer, so must be readable */
405
        pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
406
        return s->get_buffer(s, pic);
407
    }
408

    
409
    /* If internal buffer type return the same buffer */
410
    if(pic->type == FF_BUFFER_TYPE_INTERNAL) {
411
        if(s->pkt) pic->pkt_pts= s->pkt->pts;
412
        else       pic->pkt_pts= AV_NOPTS_VALUE;
413
        pic->reordered_opaque= s->reordered_opaque;
414
        return 0;
415
    }
416

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

    
434
int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
435
    int i;
436

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

    
444
int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr),void *arg, int *ret, int count){
445
    int i;
446

    
447
    for(i=0; i<count; i++){
448
        int r= func(c, arg, i, 0);
449
        if(ret) ret[i]= r;
450
    }
451
    return 0;
452
}
453

    
454
enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat *fmt){
455
    while (*fmt != PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
456
        ++fmt;
457
    return fmt[0];
458
}
459

    
460
void avcodec_get_frame_defaults(AVFrame *pic){
461
    memset(pic, 0, sizeof(AVFrame));
462

    
463
    pic->pts = pic->best_effort_timestamp = AV_NOPTS_VALUE;
464
    pic->key_frame= 1;
465
}
466

    
467
AVFrame *avcodec_alloc_frame(void){
468
    AVFrame *pic= av_malloc(sizeof(AVFrame));
469

    
470
    if(pic==NULL) return NULL;
471

    
472
    avcodec_get_frame_defaults(pic);
473

    
474
    return pic;
475
}
476

    
477
static void avcodec_get_subtitle_defaults(AVSubtitle *sub)
478
{
479
    memset(sub, 0, sizeof(*sub));
480
    sub->pts = AV_NOPTS_VALUE;
481
}
482

    
483
int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
484
{
485
    int ret= -1;
486

    
487
    /* If there is a user-supplied mutex locking routine, call it. */
488
    if (ff_lockmgr_cb) {
489
        if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
490
            return -1;
491
    }
492

    
493
    entangled_thread_counter++;
494
    if(entangled_thread_counter != 1){
495
        av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
496
        goto end;
497
    }
498

    
499
    if(avctx->codec || !codec)
500
        goto end;
501

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

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

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

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

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

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

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

    
561
    if (avctx->codec->max_lowres < avctx->lowres) {
562
        av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
563
               avctx->codec->max_lowres);
564
        goto free_and_end;
565
    }
566
    if (avctx->codec->sample_fmts && avctx->codec->encode) {
567
        int i;
568
        for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++)
569
            if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
570
                break;
571
        if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
572
            av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
573
            goto free_and_end;
574
        }
575
    }
576

    
577
    avctx->pts_correction_num_faulty_pts =
578
    avctx->pts_correction_num_faulty_dts = 0;
579
    avctx->pts_correction_last_pts =
580
    avctx->pts_correction_last_dts = INT64_MIN;
581

    
582
    if(avctx->codec->init && !(avctx->active_thread_type&FF_THREAD_FRAME)){
583
        ret = avctx->codec->init(avctx);
584
        if (ret < 0) {
585
            goto free_and_end;
586
        }
587
    }
588

    
589
    ret=0;
590
end:
591
    entangled_thread_counter--;
592

    
593
    /* Release any user-supplied mutex. */
594
    if (ff_lockmgr_cb) {
595
        (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
596
    }
597
    return ret;
598
free_and_end:
599
    av_freep(&avctx->priv_data);
600
    avctx->codec= NULL;
601
    goto end;
602
}
603

    
604
int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx, uint8_t *buf, int buf_size,
605
                         const short *samples)
606
{
607
    if(buf_size < FF_MIN_BUFFER_SIZE && 0){
608
        av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
609
        return -1;
610
    }
611
    if((avctx->codec->capabilities & CODEC_CAP_DELAY) || samples){
612
        int ret = avctx->codec->encode(avctx, buf, buf_size, samples);
613
        avctx->frame_number++;
614
        return ret;
615
    }else
616
        return 0;
617
}
618

    
619
int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
620
                         const AVFrame *pict)
621
{
622
    if(buf_size < FF_MIN_BUFFER_SIZE){
623
        av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
624
        return -1;
625
    }
626
    if(av_image_check_size(avctx->width, avctx->height, 0, avctx))
627
        return -1;
628
    if((avctx->codec->capabilities & CODEC_CAP_DELAY) || pict){
629
        int ret = avctx->codec->encode(avctx, buf, buf_size, pict);
630
        avctx->frame_number++;
631
        emms_c(); //needed to avoid an emms_c() call before every return;
632

    
633
        return ret;
634
    }else
635
        return 0;
636
}
637

    
638
int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
639
                            const AVSubtitle *sub)
640
{
641
    int ret;
642
    if(sub->start_display_time) {
643
        av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
644
        return -1;
645
    }
646
    if(sub->num_rects == 0 || !sub->rects)
647
        return -1;
648
    ret = avctx->codec->encode(avctx, buf, buf_size, sub);
649
    avctx->frame_number++;
650
    return ret;
651
}
652

    
653
/**
654
 * Attempt to guess proper monotonic timestamps for decoded video frames
655
 * which might have incorrect times. Input timestamps may wrap around, in
656
 * which case the output will as well.
657
 *
658
 * @param pts the pts field of the decoded AVPacket, as passed through
659
 * AVFrame.pkt_pts
660
 * @param dts the dts field of the decoded AVPacket
661
 * @return one of the input values, may be AV_NOPTS_VALUE
662
 */
663
static int64_t guess_correct_pts(AVCodecContext *ctx,
664
                                 int64_t reordered_pts, int64_t dts)
665
{
666
    int64_t pts = AV_NOPTS_VALUE;
667

    
668
    if (dts != AV_NOPTS_VALUE) {
669
        ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
670
        ctx->pts_correction_last_dts = dts;
671
    }
672
    if (reordered_pts != AV_NOPTS_VALUE) {
673
        ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
674
        ctx->pts_correction_last_pts = reordered_pts;
675
    }
676
    if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
677
       && reordered_pts != AV_NOPTS_VALUE)
678
        pts = reordered_pts;
679
    else
680
        pts = dts;
681

    
682
    return pts;
683
}
684

    
685

    
686
#if FF_API_VIDEO_OLD
687
int attribute_align_arg avcodec_decode_video(AVCodecContext *avctx, AVFrame *picture,
688
                         int *got_picture_ptr,
689
                         const uint8_t *buf, int buf_size)
690
{
691
    AVPacket avpkt;
692
    av_init_packet(&avpkt);
693
    avpkt.data = buf;
694
    avpkt.size = buf_size;
695
    // HACK for CorePNG to decode as normal PNG by default
696
    avpkt.flags = AV_PKT_FLAG_KEY;
697

    
698
    return avcodec_decode_video2(avctx, picture, got_picture_ptr, &avpkt);
699
}
700
#endif
701

    
702
int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
703
                         int *got_picture_ptr,
704
                         AVPacket *avpkt)
705
{
706
    int ret;
707

    
708
    *got_picture_ptr= 0;
709
    if((avctx->coded_width||avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
710
        return -1;
711

    
712
    avctx->pkt = avpkt;
713

    
714
    if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type&FF_THREAD_FRAME)){
715
        if (HAVE_PTHREADS && avctx->active_thread_type&FF_THREAD_FRAME)
716
             ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
717
                                          avpkt);
718
        else {
719
            ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
720
                              avpkt);
721
            picture->pkt_dts= avpkt->dts;
722
        }
723

    
724
        emms_c(); //needed to avoid an emms_c() call before every return;
725

    
726

    
727
        if (*got_picture_ptr){
728
            avctx->frame_number++;
729
            picture->best_effort_timestamp = guess_correct_pts(avctx,
730
                                                            picture->pkt_pts,
731
                                                            picture->pkt_dts);
732
        }
733
    }else
734
        ret= 0;
735

    
736
    return ret;
737
}
738

    
739
#if FF_API_AUDIO_OLD
740
int attribute_align_arg avcodec_decode_audio2(AVCodecContext *avctx, int16_t *samples,
741
                         int *frame_size_ptr,
742
                         const uint8_t *buf, int buf_size)
743
{
744
    AVPacket avpkt;
745
    av_init_packet(&avpkt);
746
    avpkt.data = buf;
747
    avpkt.size = buf_size;
748

    
749
    return avcodec_decode_audio3(avctx, samples, frame_size_ptr, &avpkt);
750
}
751
#endif
752

    
753
int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
754
                         int *frame_size_ptr,
755
                         AVPacket *avpkt)
756
{
757
    int ret;
758

    
759
    avctx->pkt = avpkt;
760

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

    
773
        ret = avctx->codec->decode(avctx, samples, frame_size_ptr, avpkt);
774
        avctx->frame_number++;
775
    }else{
776
        ret= 0;
777
        *frame_size_ptr=0;
778
    }
779
    return ret;
780
}
781

    
782
#if FF_API_SUBTITLE_OLD
783
int avcodec_decode_subtitle(AVCodecContext *avctx, AVSubtitle *sub,
784
                            int *got_sub_ptr,
785
                            const uint8_t *buf, int buf_size)
786
{
787
    AVPacket avpkt;
788
    av_init_packet(&avpkt);
789
    avpkt.data = buf;
790
    avpkt.size = buf_size;
791

    
792
    return avcodec_decode_subtitle2(avctx, sub, got_sub_ptr, &avpkt);
793
}
794
#endif
795

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

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

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

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

    
826
    av_freep(&sub->rects);
827

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
1086
    return NULL;
1087
}
1088

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

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

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

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

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

    
1113
    dsputil_static_init();
1114
}
1115

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

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

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

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

    
1140
    s->internal_buffer_count=0;
1141
}
1142

    
1143
char av_get_pict_type_char(int pict_type){
1144
    switch(pict_type){
1145
    case FF_I_TYPE: return 'I';
1146
    case FF_P_TYPE: return 'P';
1147
    case FF_B_TYPE: return 'B';
1148
    case FF_S_TYPE: return 'S';
1149
    case FF_SI_TYPE:return 'i';
1150
    case FF_SP_TYPE:return 'p';
1151
    case FF_BI_TYPE:return 'b';
1152
    default:        return '?';
1153
    }
1154
}
1155

    
1156
int av_get_bits_per_sample(enum CodecID codec_id){
1157
    switch(codec_id){
1158
    case CODEC_ID_ADPCM_SBPRO_2:
1159
        return 2;
1160
    case CODEC_ID_ADPCM_SBPRO_3:
1161
        return 3;
1162
    case CODEC_ID_ADPCM_SBPRO_4:
1163
    case CODEC_ID_ADPCM_CT:
1164
    case CODEC_ID_ADPCM_IMA_WAV:
1165
    case CODEC_ID_ADPCM_MS:
1166
    case CODEC_ID_ADPCM_YAMAHA:
1167
        return 4;
1168
    case CODEC_ID_ADPCM_G722:
1169
    case CODEC_ID_PCM_ALAW:
1170
    case CODEC_ID_PCM_MULAW:
1171
    case CODEC_ID_PCM_S8:
1172
    case CODEC_ID_PCM_U8:
1173
    case CODEC_ID_PCM_ZORK:
1174
        return 8;
1175
    case CODEC_ID_PCM_S16BE:
1176
    case CODEC_ID_PCM_S16LE:
1177
    case CODEC_ID_PCM_S16LE_PLANAR:
1178
    case CODEC_ID_PCM_U16BE:
1179
    case CODEC_ID_PCM_U16LE:
1180
        return 16;
1181
    case CODEC_ID_PCM_S24DAUD:
1182
    case CODEC_ID_PCM_S24BE:
1183
    case CODEC_ID_PCM_S24LE:
1184
    case CODEC_ID_PCM_U24BE:
1185
    case CODEC_ID_PCM_U24LE:
1186
        return 24;
1187
    case CODEC_ID_PCM_S32BE:
1188
    case CODEC_ID_PCM_S32LE:
1189
    case CODEC_ID_PCM_U32BE:
1190
    case CODEC_ID_PCM_U32LE:
1191
    case CODEC_ID_PCM_F32BE:
1192
    case CODEC_ID_PCM_F32LE:
1193
        return 32;
1194
    case CODEC_ID_PCM_F64BE:
1195
    case CODEC_ID_PCM_F64LE:
1196
        return 64;
1197
    default:
1198
        return 0;
1199
    }
1200
}
1201

    
1202
#if FF_API_OLD_SAMPLE_FMT
1203
int av_get_bits_per_sample_format(enum AVSampleFormat sample_fmt) {
1204
    return av_get_bits_per_sample_fmt(sample_fmt);
1205
}
1206
#endif
1207

    
1208
#if !HAVE_THREADS
1209
int ff_thread_init(AVCodecContext *s){
1210
    return -1;
1211
}
1212
#endif
1213

    
1214
unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
1215
{
1216
    unsigned int n = 0;
1217

    
1218
    while(v >= 0xff) {
1219
        *s++ = 0xff;
1220
        v -= 0xff;
1221
        n++;
1222
    }
1223
    *s = v;
1224
    n++;
1225
    return n;
1226
}
1227

    
1228
#if LIBAVCODEC_VERSION_MAJOR < 53
1229
#include "libavutil/parseutils.h"
1230

    
1231
int av_parse_video_frame_size(int *width_ptr, int *height_ptr, const char *str)
1232
{
1233
    return av_parse_video_size(width_ptr, height_ptr, str);
1234
}
1235

    
1236
int av_parse_video_frame_rate(AVRational *frame_rate, const char *arg)
1237
{
1238
    return av_parse_video_rate(frame_rate, arg);
1239
}
1240
#endif
1241

    
1242
int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
1243
    int i;
1244
    for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
1245
    return i;
1246
}
1247

    
1248
void av_log_missing_feature(void *avc, const char *feature, int want_sample)
1249
{
1250
    av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your FFmpeg "
1251
            "version to the newest one from Git. If the problem still "
1252
            "occurs, it means that your file has a feature which has not "
1253
            "been implemented.", feature);
1254
    if(want_sample)
1255
        av_log_ask_for_sample(avc, NULL);
1256
    else
1257
        av_log(avc, AV_LOG_WARNING, "\n");
1258
}
1259

    
1260
void av_log_ask_for_sample(void *avc, const char *msg)
1261
{
1262
    if (msg)
1263
        av_log(avc, AV_LOG_WARNING, "%s ", msg);
1264
    av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
1265
            "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
1266
            "and contact the ffmpeg-devel mailing list.\n");
1267
}
1268

    
1269
static AVHWAccel *first_hwaccel = NULL;
1270

    
1271
void av_register_hwaccel(AVHWAccel *hwaccel)
1272
{
1273
    AVHWAccel **p = &first_hwaccel;
1274
    while (*p)
1275
        p = &(*p)->next;
1276
    *p = hwaccel;
1277
    hwaccel->next = NULL;
1278
}
1279

    
1280
AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
1281
{
1282
    return hwaccel ? hwaccel->next : first_hwaccel;
1283
}
1284

    
1285
AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
1286
{
1287
    AVHWAccel *hwaccel=NULL;
1288

    
1289
    while((hwaccel= av_hwaccel_next(hwaccel))){
1290
        if (   hwaccel->id      == codec_id
1291
            && hwaccel->pix_fmt == pix_fmt)
1292
            return hwaccel;
1293
    }
1294
    return NULL;
1295
}
1296

    
1297
int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
1298
{
1299
    if (ff_lockmgr_cb) {
1300
        if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
1301
            return -1;
1302
    }
1303

    
1304
    ff_lockmgr_cb = cb;
1305

    
1306
    if (ff_lockmgr_cb) {
1307
        if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
1308
            return -1;
1309
    }
1310
    return 0;
1311
}
1312

    
1313
unsigned int ff_toupper4(unsigned int x)
1314
{
1315
    return     toupper( x     &0xFF)
1316
    + (toupper((x>>8 )&0xFF)<<8 )
1317
    + (toupper((x>>16)&0xFF)<<16)
1318
    + (toupper((x>>24)&0xFF)<<24);
1319
}
1320

    
1321
#if !HAVE_PTHREADS
1322

    
1323
int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
1324
{
1325
    f->owner = avctx;
1326
    return avctx->get_buffer(avctx, f);
1327
}
1328

    
1329
void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
1330
{
1331
    f->owner->release_buffer(f->owner, f);
1332
}
1333

    
1334
void ff_thread_finish_setup(AVCodecContext *avctx)
1335
{
1336
}
1337

    
1338
void ff_thread_report_progress(AVFrame *f, int progress, int field)
1339
{
1340
}
1341

    
1342
void ff_thread_await_progress(AVFrame *f, int progress, int field)
1343
{
1344
}
1345

    
1346
#endif
1347

    
1348
#if LIBAVCODEC_VERSION_MAJOR < 53
1349

    
1350
int avcodec_thread_init(AVCodecContext *s, int thread_count)
1351
{
1352
    s->thread_count = thread_count;
1353
    return ff_thread_init(s);
1354
}
1355

    
1356
void avcodec_thread_free(AVCodecContext *s)
1357
{
1358
#if HAVE_THREADS
1359
    ff_thread_free(s);
1360
#endif
1361
}
1362

    
1363
#endif