Statistics
| Branch: | Revision:

ffmpeg / libavcodec / utils.c @ d4497f6d

History | View | Annotate | Download (38.9 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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
363
    return 0;
364
}
365

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
471
    avcodec_get_frame_defaults(pic);
472

    
473
    return pic;
474
}
475

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
675
    return pts;
676
}
677

    
678

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

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

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

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

    
705
    avctx->pkt = avpkt;
706

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

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

    
719

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

    
729
    return ret;
730
}
731

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

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

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

    
752
    avctx->pkt = avpkt;
753

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

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

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

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

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

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

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

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

    
818
    av_freep(&sub->rects);
819

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
1078
    return NULL;
1079
}
1080

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

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

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

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

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

    
1105
    dsputil_static_init();
1106
}
1107

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

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

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

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

    
1132
    s->internal_buffer_count=0;
1133
}
1134

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

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

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

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

    
1205
unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
1206
{
1207
    unsigned int n = 0;
1208

    
1209
    while(v >= 0xff) {
1210
        *s++ = 0xff;
1211
        v -= 0xff;
1212
        n++;
1213
    }
1214
    *s = v;
1215
    n++;
1216
    return n;
1217
}
1218

    
1219
#if LIBAVCODEC_VERSION_MAJOR < 53
1220
#include "libavutil/parseutils.h"
1221

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

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

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

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

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

    
1260
static AVHWAccel *first_hwaccel = NULL;
1261

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

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

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

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

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

    
1295
    ff_lockmgr_cb = cb;
1296

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

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

    
1312
#if !HAVE_PTHREADS
1313

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

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

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

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

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

    
1337
#endif
1338

    
1339
#if LIBAVCODEC_VERSION_MAJOR < 53
1340

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

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

    
1354
#endif