Statistics
| Branch: | Revision:

ffmpeg / libavcodec / utils.c @ dc172ecc

History | View | Annotate | Download (39 KB)

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

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

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

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

    
51
void *av_fast_realloc(void *ptr, unsigned int *size, 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
int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
478
{
479
    int ret= -1;
480

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

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

    
493
    if(avctx->codec || !codec)
494
        goto end;
495

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

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

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

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

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

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

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

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

    
571
    avctx->pts_correction_num_faulty_pts =
572
    avctx->pts_correction_num_faulty_dts = 0;
573
    avctx->pts_correction_last_pts =
574
    avctx->pts_correction_last_dts = INT64_MIN;
575

    
576
    if(avctx->codec->init && !(avctx->active_thread_type&FF_THREAD_FRAME)){
577
        ret = avctx->codec->init(avctx);
578
        if (ret < 0) {
579
            goto free_and_end;
580
        }
581
    }
582

    
583
    ret=0;
584
end:
585
    entangled_thread_counter--;
586

    
587
    /* Release any user-supplied mutex. */
588
    if (ff_lockmgr_cb) {
589
        (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
590
    }
591
    return ret;
592
free_and_end:
593
    av_freep(&avctx->priv_data);
594
    avctx->codec= NULL;
595
    goto end;
596
}
597

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

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

    
627
        return ret;
628
    }else
629
        return 0;
630
}
631

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

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

    
662
    if (dts != AV_NOPTS_VALUE) {
663
        ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
664
        ctx->pts_correction_last_dts = dts;
665
    }
666
    if (reordered_pts != AV_NOPTS_VALUE) {
667
        ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
668
        ctx->pts_correction_last_pts = reordered_pts;
669
    }
670
    if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
671
       && reordered_pts != AV_NOPTS_VALUE)
672
        pts = reordered_pts;
673
    else
674
        pts = dts;
675

    
676
    return pts;
677
}
678

    
679

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

    
692
    return avcodec_decode_video2(avctx, picture, got_picture_ptr, &avpkt);
693
}
694
#endif
695

    
696
int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
697
                         int *got_picture_ptr,
698
                         AVPacket *avpkt)
699
{
700
    int ret;
701

    
702
    *got_picture_ptr= 0;
703
    if((avctx->coded_width||avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
704
        return -1;
705

    
706
    avctx->pkt = avpkt;
707

    
708
    if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type&FF_THREAD_FRAME)){
709
        if (HAVE_PTHREADS && avctx->active_thread_type&FF_THREAD_FRAME)
710
             ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
711
                                          avpkt);
712
        else {
713
            ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
714
                              avpkt);
715
            picture->pkt_dts= avpkt->dts;
716
        }
717

    
718
        emms_c(); //needed to avoid an emms_c() call before every return;
719

    
720

    
721
        if (*got_picture_ptr){
722
            avctx->frame_number++;
723
            picture->best_effort_timestamp = guess_correct_pts(avctx,
724
                                                            picture->pkt_pts,
725
                                                            picture->pkt_dts);
726
        }
727
    }else
728
        ret= 0;
729

    
730
    return ret;
731
}
732

    
733
#if FF_API_AUDIO_OLD
734
int attribute_align_arg avcodec_decode_audio2(AVCodecContext *avctx, int16_t *samples,
735
                         int *frame_size_ptr,
736
                         const uint8_t *buf, int buf_size)
737
{
738
    AVPacket avpkt;
739
    av_init_packet(&avpkt);
740
    avpkt.data = buf;
741
    avpkt.size = buf_size;
742

    
743
    return avcodec_decode_audio3(avctx, samples, frame_size_ptr, &avpkt);
744
}
745
#endif
746

    
747
int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
748
                         int *frame_size_ptr,
749
                         AVPacket *avpkt)
750
{
751
    int ret;
752

    
753
    avctx->pkt = avpkt;
754

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

    
767
        ret = avctx->codec->decode(avctx, samples, frame_size_ptr, avpkt);
768
        avctx->frame_number++;
769
    }else{
770
        ret= 0;
771
        *frame_size_ptr=0;
772
    }
773
    return ret;
774
}
775

    
776
#if FF_API_SUBTITLE_OLD
777
int avcodec_decode_subtitle(AVCodecContext *avctx, AVSubtitle *sub,
778
                            int *got_sub_ptr,
779
                            const uint8_t *buf, int buf_size)
780
{
781
    AVPacket avpkt;
782
    av_init_packet(&avpkt);
783
    avpkt.data = buf;
784
    avpkt.size = buf_size;
785

    
786
    return avcodec_decode_subtitle2(avctx, sub, got_sub_ptr, &avpkt);
787
}
788
#endif
789

    
790
int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
791
                            int *got_sub_ptr,
792
                            AVPacket *avpkt)
793
{
794
    int ret;
795

    
796
    avctx->pkt = avpkt;
797
    *got_sub_ptr = 0;
798
    ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
799
    if (*got_sub_ptr)
800
        avctx->frame_number++;
801
    return ret;
802
}
803

    
804
void avsubtitle_free(AVSubtitle *sub)
805
{
806
    int i;
807

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

    
819
    av_freep(&sub->rects);
820

    
821
    memset(sub, 0, sizeof(AVSubtitle));
822
}
823

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

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

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

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

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

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

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

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

    
915
static int get_bit_rate(AVCodecContext *ctx)
916
{
917
    int bit_rate;
918
    int bits_per_sample;
919

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

    
938
size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
939
{
940
    int i, len, ret = 0;
941

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

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

    
962
    if (encode)
963
        p = avcodec_find_encoder(enc->codec_id);
964
    else
965
        p = avcodec_find_decoder(enc->codec_id);
966

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

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

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

    
1075
    for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
1076
        if (p->profile == profile)
1077
            return p->name;
1078

    
1079
    return NULL;
1080
}
1081

    
1082
unsigned avcodec_version( void )
1083
{
1084
  return LIBAVCODEC_VERSION_INT;
1085
}
1086

    
1087
const char *avcodec_configuration(void)
1088
{
1089
    return FFMPEG_CONFIGURATION;
1090
}
1091

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

    
1098
void avcodec_init(void)
1099
{
1100
    static int initialized = 0;
1101

    
1102
    if (initialized != 0)
1103
        return;
1104
    initialized = 1;
1105

    
1106
    dsputil_static_init();
1107
}
1108

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

    
1117
void avcodec_default_free_buffers(AVCodecContext *s){
1118
    int i, j;
1119

    
1120
    if(s->internal_buffer==NULL) return;
1121

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

    
1133
    s->internal_buffer_count=0;
1134
}
1135

    
1136
char av_get_pict_type_char(int pict_type){
1137
    switch(pict_type){
1138
    case FF_I_TYPE: return 'I';
1139
    case FF_P_TYPE: return 'P';
1140
    case FF_B_TYPE: return 'B';
1141
    case FF_S_TYPE: return 'S';
1142
    case FF_SI_TYPE:return 'i';
1143
    case FF_SP_TYPE:return 'p';
1144
    case FF_BI_TYPE:return 'b';
1145
    default:        return '?';
1146
    }
1147
}
1148

    
1149
int av_get_bits_per_sample(enum CodecID codec_id){
1150
    switch(codec_id){
1151
    case CODEC_ID_ADPCM_SBPRO_2:
1152
        return 2;
1153
    case CODEC_ID_ADPCM_SBPRO_3:
1154
        return 3;
1155
    case CODEC_ID_ADPCM_SBPRO_4:
1156
    case CODEC_ID_ADPCM_CT:
1157
    case CODEC_ID_ADPCM_IMA_WAV:
1158
    case CODEC_ID_ADPCM_MS:
1159
    case CODEC_ID_ADPCM_YAMAHA:
1160
        return 4;
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
#if LIBAVCODEC_VERSION_MAJOR < 53
1221
#include "libavutil/parseutils.h"
1222

    
1223
int av_parse_video_frame_size(int *width_ptr, int *height_ptr, const char *str)
1224
{
1225
    return av_parse_video_size(width_ptr, height_ptr, str);
1226
}
1227

    
1228
int av_parse_video_frame_rate(AVRational *frame_rate, const char *arg)
1229
{
1230
    return av_parse_video_rate(frame_rate, arg);
1231
}
1232
#endif
1233

    
1234
int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
1235
    int i;
1236
    for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
1237
    return i;
1238
}
1239

    
1240
void av_log_missing_feature(void *avc, const char *feature, int want_sample)
1241
{
1242
    av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your FFmpeg "
1243
            "version to the newest one from Git. If the problem still "
1244
            "occurs, it means that your file has a feature which has not "
1245
            "been implemented.", feature);
1246
    if(want_sample)
1247
        av_log_ask_for_sample(avc, NULL);
1248
    else
1249
        av_log(avc, AV_LOG_WARNING, "\n");
1250
}
1251

    
1252
void av_log_ask_for_sample(void *avc, const char *msg)
1253
{
1254
    if (msg)
1255
        av_log(avc, AV_LOG_WARNING, "%s ", msg);
1256
    av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
1257
            "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
1258
            "and contact the ffmpeg-devel mailing list.\n");
1259
}
1260

    
1261
static AVHWAccel *first_hwaccel = NULL;
1262

    
1263
void av_register_hwaccel(AVHWAccel *hwaccel)
1264
{
1265
    AVHWAccel **p = &first_hwaccel;
1266
    while (*p)
1267
        p = &(*p)->next;
1268
    *p = hwaccel;
1269
    hwaccel->next = NULL;
1270
}
1271

    
1272
AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
1273
{
1274
    return hwaccel ? hwaccel->next : first_hwaccel;
1275
}
1276

    
1277
AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
1278
{
1279
    AVHWAccel *hwaccel=NULL;
1280

    
1281
    while((hwaccel= av_hwaccel_next(hwaccel))){
1282
        if (   hwaccel->id      == codec_id
1283
            && hwaccel->pix_fmt == pix_fmt)
1284
            return hwaccel;
1285
    }
1286
    return NULL;
1287
}
1288

    
1289
int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
1290
{
1291
    if (ff_lockmgr_cb) {
1292
        if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
1293
            return -1;
1294
    }
1295

    
1296
    ff_lockmgr_cb = cb;
1297

    
1298
    if (ff_lockmgr_cb) {
1299
        if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
1300
            return -1;
1301
    }
1302
    return 0;
1303
}
1304

    
1305
unsigned int ff_toupper4(unsigned int x)
1306
{
1307
    return     toupper( x     &0xFF)
1308
    + (toupper((x>>8 )&0xFF)<<8 )
1309
    + (toupper((x>>16)&0xFF)<<16)
1310
    + (toupper((x>>24)&0xFF)<<24);
1311
}
1312

    
1313
#if !HAVE_PTHREADS
1314

    
1315
int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
1316
{
1317
    f->owner = avctx;
1318
    return avctx->get_buffer(avctx, f);
1319
}
1320

    
1321
void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
1322
{
1323
    f->owner->release_buffer(f->owner, f);
1324
}
1325

    
1326
void ff_thread_finish_setup(AVCodecContext *avctx)
1327
{
1328
}
1329

    
1330
void ff_thread_report_progress(AVFrame *f, int progress, int field)
1331
{
1332
}
1333

    
1334
void ff_thread_await_progress(AVFrame *f, int progress, int field)
1335
{
1336
}
1337

    
1338
#endif
1339

    
1340
#if LIBAVCODEC_VERSION_MAJOR < 53
1341

    
1342
int avcodec_thread_init(AVCodecContext *s, int thread_count)
1343
{
1344
    s->thread_count = thread_count;
1345
    return ff_thread_init(s);
1346
}
1347

    
1348
void avcodec_thread_free(AVCodecContext *s)
1349
{
1350
#if HAVE_THREADS
1351
    ff_thread_free(s);
1352
#endif
1353
}
1354

    
1355
#endif