Statistics
| Branch: | Revision:

ffmpeg / libavcodec / utils.c @ 043d2ff2

History | View | Annotate | Download (38.4 KB)

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

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

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

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

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

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

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

    
63
    *size= min_size;
64

    
65
    return ptr;
66
}
67

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

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

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

    
88
void avcodec_register(AVCodec *codec)
89
{
90
    AVCodec **p;
91
    avcodec_init();
92
    p = &first_avcodec;
93
    while (*p != NULL) p = &(*p)->next;
94
    *p = codec;
95
    codec->next = NULL;
96
}
97

    
98
#if LIBAVCODEC_VERSION_MAJOR < 53
99
void register_avcodec(AVCodec *codec)
100
{
101
    avcodec_register(codec);
102
}
103
#endif
104

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

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

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

    
126
#define INTERNAL_BUFFER_SIZE 32
127

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

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

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

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

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

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

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

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

    
246
    if(av_image_check_size(w, h, 0, s))
247
        return -1;
248

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

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

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

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

    
276
    if(buf->base[0]){
277
        pic->age= *picture_number - buf->last_pic_num;
278
        buf->last_pic_num= *picture_number;
279
    }else{
280
        int h_chroma_shift, v_chroma_shift;
281
        int size[4] = {0};
282
        int tmpsize;
283
        int unaligned;
284
        AVPicture picture;
285
        int stride_align[4];
286

    
287
        avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
288

    
289
        avcodec_align_dimensions2(s, &w, &h, stride_align);
290

    
291
        if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
292
            w+= EDGE_WIDTH*2;
293
            h+= EDGE_WIDTH*2;
294
        }
295

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

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

    
309
        tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
310
        if (tmpsize < 0)
311
            return -1;
312

    
313
        for (i=0; i<3 && picture.data[i+1]; i++)
314
            size[i] = picture.data[i+1] - picture.data[i];
315
        size[i] = tmpsize - (picture.data[i] - picture.data[0]);
316

    
317
        buf->last_pic_num= -256*256*256*64;
318
        memset(buf->base, 0, sizeof(buf->base));
319
        memset(buf->data, 0, sizeof(buf->data));
320

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

    
325
            buf->linesize[i]= picture.linesize[i];
326

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

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

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

    
353
    if(s->pkt) pic->pkt_pts= s->pkt->pts;
354
    else       pic->pkt_pts= AV_NOPTS_VALUE;
355
    pic->reordered_opaque= s->reordered_opaque;
356

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

    
360
    return 0;
361
}
362

    
363
void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
364
    int i;
365
    InternalBuffer *buf, *last;
366

    
367
    assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
368
    assert(s->internal_buffer_count);
369

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

    
380
    FFSWAP(InternalBuffer, *buf, *last);
381

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

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

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

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

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

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

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

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

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

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

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

    
454
void avcodec_get_frame_defaults(AVFrame *pic){
455
    memset(pic, 0, sizeof(AVFrame));
456

    
457
    pic->pts = pic->best_effort_timestamp = AV_NOPTS_VALUE;
458
    pic->key_frame= 1;
459
}
460

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

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

    
466
    avcodec_get_frame_defaults(pic);
467

    
468
    return pic;
469
}
470

    
471
int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
472
{
473
    int ret= -1;
474

    
475
    /* If there is a user-supplied mutex locking routine, call it. */
476
    if (ff_lockmgr_cb) {
477
        if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
478
            return -1;
479
    }
480

    
481
    entangled_thread_counter++;
482
    if(entangled_thread_counter != 1){
483
        av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
484
        goto end;
485
    }
486

    
487
    if(avctx->codec || !codec)
488
        goto end;
489

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

    
506
    if(avctx->coded_width && avctx->coded_height)
507
        avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
508
    else if(avctx->width && avctx->height)
509
        avcodec_set_dimensions(avctx, avctx->width, avctx->height);
510

    
511
    if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
512
        && (  av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
513
           || av_image_check_size(avctx->width,       avctx->height,       0, avctx) < 0)) {
514
        av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
515
        avcodec_set_dimensions(avctx, 0, 0);
516
    }
517

    
518
    /* if the decoder init function was already called previously,
519
       free the already allocated subtitle_header before overwriting it */
520
    if (codec->decode)
521
        av_freep(&avctx->subtitle_header);
522

    
523
#define SANE_NB_CHANNELS 128U
524
    if (avctx->channels > SANE_NB_CHANNELS) {
525
        ret = AVERROR(EINVAL);
526
        goto free_and_end;
527
    }
528

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

    
542
    if (HAVE_THREADS && !avctx->thread_opaque) {
543
        ret = ff_thread_init(avctx, avctx->thread_count);
544
        if (ret < 0) {
545
            goto free_and_end;
546
        }
547
    }
548

    
549
    if (avctx->codec->max_lowres < avctx->lowres) {
550
        av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
551
               avctx->codec->max_lowres);
552
        goto free_and_end;
553
    }
554

    
555
    avctx->pts_correction_num_faulty_pts =
556
    avctx->pts_correction_num_faulty_dts = 0;
557
    avctx->pts_correction_last_pts =
558
    avctx->pts_correction_last_dts = INT64_MIN;
559

    
560
    if(avctx->codec->init && !(avctx->active_thread_type&FF_THREAD_FRAME)){
561
        ret = avctx->codec->init(avctx);
562
        if (ret < 0) {
563
            goto free_and_end;
564
        }
565
    }
566
    ret=0;
567
end:
568
    entangled_thread_counter--;
569

    
570
    /* Release any user-supplied mutex. */
571
    if (ff_lockmgr_cb) {
572
        (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
573
    }
574
    return ret;
575
free_and_end:
576
    av_freep(&avctx->priv_data);
577
    avctx->codec= NULL;
578
    goto end;
579
}
580

    
581
int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx, uint8_t *buf, int buf_size,
582
                         const short *samples)
583
{
584
    if(buf_size < FF_MIN_BUFFER_SIZE && 0){
585
        av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
586
        return -1;
587
    }
588
    if((avctx->codec->capabilities & CODEC_CAP_DELAY) || samples){
589
        int ret = avctx->codec->encode(avctx, buf, buf_size, samples);
590
        avctx->frame_number++;
591
        return ret;
592
    }else
593
        return 0;
594
}
595

    
596
int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
597
                         const AVFrame *pict)
598
{
599
    if(buf_size < FF_MIN_BUFFER_SIZE){
600
        av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
601
        return -1;
602
    }
603
    if(av_image_check_size(avctx->width, avctx->height, 0, avctx))
604
        return -1;
605
    if((avctx->codec->capabilities & CODEC_CAP_DELAY) || pict){
606
        int ret = avctx->codec->encode(avctx, buf, buf_size, pict);
607
        avctx->frame_number++;
608
        emms_c(); //needed to avoid an emms_c() call before every return;
609

    
610
        return ret;
611
    }else
612
        return 0;
613
}
614

    
615
int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
616
                            const AVSubtitle *sub)
617
{
618
    int ret;
619
    if(sub->start_display_time) {
620
        av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
621
        return -1;
622
    }
623
    if(sub->num_rects == 0 || !sub->rects)
624
        return -1;
625
    ret = avctx->codec->encode(avctx, buf, buf_size, sub);
626
    avctx->frame_number++;
627
    return ret;
628
}
629

    
630
/**
631
 * Attempt to guess proper monotonic timestamps for decoded video frames
632
 * which might have incorrect times. Input timestamps may wrap around, in
633
 * which case the output will as well.
634
 *
635
 * @param pts the pts field of the decoded AVPacket, as passed through
636
 * AVFrame.pkt_pts
637
 * @param dts the dts field of the decoded AVPacket
638
 * @return one of the input values, may be AV_NOPTS_VALUE
639
 */
640
static int64_t guess_correct_pts(AVCodecContext *ctx,
641
                                 int64_t reordered_pts, int64_t dts)
642
{
643
    int64_t pts = AV_NOPTS_VALUE;
644

    
645
    if (dts != AV_NOPTS_VALUE) {
646
        ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
647
        ctx->pts_correction_last_dts = dts;
648
    }
649
    if (reordered_pts != AV_NOPTS_VALUE) {
650
        ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
651
        ctx->pts_correction_last_pts = reordered_pts;
652
    }
653
    if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
654
       && reordered_pts != AV_NOPTS_VALUE)
655
        pts = reordered_pts;
656
    else
657
        pts = dts;
658

    
659
    return pts;
660
}
661

    
662

    
663
#if FF_API_VIDEO_OLD
664
int attribute_align_arg avcodec_decode_video(AVCodecContext *avctx, AVFrame *picture,
665
                         int *got_picture_ptr,
666
                         const uint8_t *buf, int buf_size)
667
{
668
    AVPacket avpkt;
669
    av_init_packet(&avpkt);
670
    avpkt.data = buf;
671
    avpkt.size = buf_size;
672
    // HACK for CorePNG to decode as normal PNG by default
673
    avpkt.flags = AV_PKT_FLAG_KEY;
674

    
675
    return avcodec_decode_video2(avctx, picture, got_picture_ptr, &avpkt);
676
}
677
#endif
678

    
679
int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
680
                         int *got_picture_ptr,
681
                         AVPacket *avpkt)
682
{
683
    int ret;
684

    
685
    *got_picture_ptr= 0;
686
    if((avctx->coded_width||avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
687
        return -1;
688

    
689
    avctx->pkt = avpkt;
690

    
691
    if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type&FF_THREAD_FRAME)){
692
        if (HAVE_PTHREADS && avctx->active_thread_type&FF_THREAD_FRAME)
693
             ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
694
                                          avpkt);
695
        else {
696
            ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
697
                              avpkt);
698
            picture->pkt_dts= avpkt->dts;
699
        }
700

    
701
        emms_c(); //needed to avoid an emms_c() call before every return;
702

    
703
        picture->best_effort_timestamp = guess_correct_pts(avctx,
704
                                                           picture->pkt_pts,
705
                                                           picture->pkt_dts);
706

    
707
        if (*got_picture_ptr)
708
            avctx->frame_number++;
709
    }else
710
        ret= 0;
711

    
712
    return ret;
713
}
714

    
715
#if FF_API_AUDIO_OLD
716
int attribute_align_arg avcodec_decode_audio2(AVCodecContext *avctx, int16_t *samples,
717
                         int *frame_size_ptr,
718
                         const uint8_t *buf, int buf_size)
719
{
720
    AVPacket avpkt;
721
    av_init_packet(&avpkt);
722
    avpkt.data = buf;
723
    avpkt.size = buf_size;
724

    
725
    return avcodec_decode_audio3(avctx, samples, frame_size_ptr, &avpkt);
726
}
727
#endif
728

    
729
int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
730
                         int *frame_size_ptr,
731
                         AVPacket *avpkt)
732
{
733
    int ret;
734

    
735
    avctx->pkt = avpkt;
736

    
737
    if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size){
738
        //FIXME remove the check below _after_ ensuring that all audio check that the available space is enough
739
        if(*frame_size_ptr < AVCODEC_MAX_AUDIO_FRAME_SIZE){
740
            av_log(avctx, AV_LOG_ERROR, "buffer smaller than AVCODEC_MAX_AUDIO_FRAME_SIZE\n");
741
            return -1;
742
        }
743
        if(*frame_size_ptr < FF_MIN_BUFFER_SIZE ||
744
        *frame_size_ptr < avctx->channels * avctx->frame_size * sizeof(int16_t)){
745
            av_log(avctx, AV_LOG_ERROR, "buffer %d too small\n", *frame_size_ptr);
746
            return -1;
747
        }
748

    
749
        ret = avctx->codec->decode(avctx, samples, frame_size_ptr, avpkt);
750
        avctx->frame_number++;
751
    }else{
752
        ret= 0;
753
        *frame_size_ptr=0;
754
    }
755
    return ret;
756
}
757

    
758
#if FF_API_SUBTITLE_OLD
759
int avcodec_decode_subtitle(AVCodecContext *avctx, AVSubtitle *sub,
760
                            int *got_sub_ptr,
761
                            const uint8_t *buf, int buf_size)
762
{
763
    AVPacket avpkt;
764
    av_init_packet(&avpkt);
765
    avpkt.data = buf;
766
    avpkt.size = buf_size;
767

    
768
    return avcodec_decode_subtitle2(avctx, sub, got_sub_ptr, &avpkt);
769
}
770
#endif
771

    
772
int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
773
                            int *got_sub_ptr,
774
                            AVPacket *avpkt)
775
{
776
    int ret;
777

    
778
    avctx->pkt = avpkt;
779
    *got_sub_ptr = 0;
780
    ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
781
    if (*got_sub_ptr)
782
        avctx->frame_number++;
783
    return ret;
784
}
785

    
786
void avsubtitle_free(AVSubtitle *sub)
787
{
788
    int i;
789

    
790
    for (i = 0; i < sub->num_rects; i++)
791
    {
792
        av_freep(&sub->rects[i]->pict.data[0]);
793
        av_freep(&sub->rects[i]->pict.data[1]);
794
        av_freep(&sub->rects[i]->pict.data[2]);
795
        av_freep(&sub->rects[i]->pict.data[3]);
796
        av_freep(&sub->rects[i]->text);
797
        av_freep(&sub->rects[i]->ass);
798
        av_freep(&sub->rects[i]);
799
    }
800

    
801
    av_freep(&sub->rects);
802

    
803
    memset(sub, 0, sizeof(AVSubtitle));
804
}
805

    
806
av_cold int avcodec_close(AVCodecContext *avctx)
807
{
808
    /* If there is a user-supplied mutex locking routine, call it. */
809
    if (ff_lockmgr_cb) {
810
        if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
811
            return -1;
812
    }
813

    
814
    entangled_thread_counter++;
815
    if(entangled_thread_counter != 1){
816
        av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
817
        entangled_thread_counter--;
818
        return -1;
819
    }
820

    
821
    if (HAVE_THREADS && avctx->thread_opaque)
822
        ff_thread_free(avctx);
823
    if (avctx->codec && avctx->codec->close)
824
        avctx->codec->close(avctx);
825
    avcodec_default_free_buffers(avctx);
826
    avctx->coded_frame = NULL;
827
    av_freep(&avctx->priv_data);
828
    if(avctx->codec && avctx->codec->encode)
829
        av_freep(&avctx->extradata);
830
    avctx->codec = NULL;
831
    avctx->active_thread_type = 0;
832
    entangled_thread_counter--;
833

    
834
    /* Release any user-supplied mutex. */
835
    if (ff_lockmgr_cb) {
836
        (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
837
    }
838
    return 0;
839
}
840

    
841
AVCodec *avcodec_find_encoder(enum CodecID id)
842
{
843
    AVCodec *p, *experimental=NULL;
844
    p = first_avcodec;
845
    while (p) {
846
        if (p->encode != NULL && p->id == id) {
847
            if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
848
                experimental = p;
849
            } else
850
                return p;
851
        }
852
        p = p->next;
853
    }
854
    return experimental;
855
}
856

    
857
AVCodec *avcodec_find_encoder_by_name(const char *name)
858
{
859
    AVCodec *p;
860
    if (!name)
861
        return NULL;
862
    p = first_avcodec;
863
    while (p) {
864
        if (p->encode != NULL && strcmp(name,p->name) == 0)
865
            return p;
866
        p = p->next;
867
    }
868
    return NULL;
869
}
870

    
871
AVCodec *avcodec_find_decoder(enum CodecID id)
872
{
873
    AVCodec *p;
874
    p = first_avcodec;
875
    while (p) {
876
        if (p->decode != NULL && p->id == id)
877
            return p;
878
        p = p->next;
879
    }
880
    return NULL;
881
}
882

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

    
897
static int get_bit_rate(AVCodecContext *ctx)
898
{
899
    int bit_rate;
900
    int bits_per_sample;
901

    
902
    switch(ctx->codec_type) {
903
    case AVMEDIA_TYPE_VIDEO:
904
    case AVMEDIA_TYPE_DATA:
905
    case AVMEDIA_TYPE_SUBTITLE:
906
    case AVMEDIA_TYPE_ATTACHMENT:
907
        bit_rate = ctx->bit_rate;
908
        break;
909
    case AVMEDIA_TYPE_AUDIO:
910
        bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
911
        bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
912
        break;
913
    default:
914
        bit_rate = 0;
915
        break;
916
    }
917
    return bit_rate;
918
}
919

    
920
size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
921
{
922
    int i, len, ret = 0;
923

    
924
    for (i = 0; i < 4; i++) {
925
        len = snprintf(buf, buf_size,
926
                       isprint(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
927
        buf      += len;
928
        buf_size  = buf_size > len ? buf_size - len : 0;
929
        ret      += len;
930
        codec_tag>>=8;
931
    }
932
    return ret;
933
}
934

    
935
void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
936
{
937
    const char *codec_name;
938
    const char *profile = NULL;
939
    AVCodec *p;
940
    char buf1[32];
941
    int bitrate;
942
    AVRational display_aspect_ratio;
943

    
944
    if (encode)
945
        p = avcodec_find_encoder(enc->codec_id);
946
    else
947
        p = avcodec_find_decoder(enc->codec_id);
948

    
949
    if (p) {
950
        codec_name = p->name;
951
        profile = av_get_profile_name(p, enc->profile);
952
    } else if (enc->codec_id == CODEC_ID_MPEG2TS) {
953
        /* fake mpeg2 transport stream codec (currently not
954
           registered) */
955
        codec_name = "mpeg2ts";
956
    } else if (enc->codec_name[0] != '\0') {
957
        codec_name = enc->codec_name;
958
    } else {
959
        /* output avi tags */
960
        char tag_buf[32];
961
        av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
962
        snprintf(buf1, sizeof(buf1), "%s / 0x%04X", tag_buf, enc->codec_tag);
963
        codec_name = buf1;
964
    }
965

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

    
1051
const char *av_get_profile_name(const AVCodec *codec, int profile)
1052
{
1053
    const AVProfile *p;
1054
    if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
1055
        return NULL;
1056

    
1057
    for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
1058
        if (p->profile == profile)
1059
            return p->name;
1060

    
1061
    return NULL;
1062
}
1063

    
1064
unsigned avcodec_version( void )
1065
{
1066
  return LIBAVCODEC_VERSION_INT;
1067
}
1068

    
1069
const char *avcodec_configuration(void)
1070
{
1071
    return FFMPEG_CONFIGURATION;
1072
}
1073

    
1074
const char *avcodec_license(void)
1075
{
1076
#define LICENSE_PREFIX "libavcodec license: "
1077
    return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
1078
}
1079

    
1080
void avcodec_init(void)
1081
{
1082
    static int initialized = 0;
1083

    
1084
    if (initialized != 0)
1085
        return;
1086
    initialized = 1;
1087

    
1088
    dsputil_static_init();
1089
}
1090

    
1091
void avcodec_flush_buffers(AVCodecContext *avctx)
1092
{
1093
    if(HAVE_PTHREADS && avctx->active_thread_type&FF_THREAD_FRAME)
1094
        ff_thread_flush(avctx);
1095
    if(avctx->codec->flush)
1096
        avctx->codec->flush(avctx);
1097
}
1098

    
1099
void avcodec_default_free_buffers(AVCodecContext *s){
1100
    int i, j;
1101

    
1102
    if(s->internal_buffer==NULL) return;
1103

    
1104
    if (s->internal_buffer_count)
1105
        av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n", s->internal_buffer_count);
1106
    for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
1107
        InternalBuffer *buf= &((InternalBuffer*)s->internal_buffer)[i];
1108
        for(j=0; j<4; j++){
1109
            av_freep(&buf->base[j]);
1110
            buf->data[j]= NULL;
1111
        }
1112
    }
1113
    av_freep(&s->internal_buffer);
1114

    
1115
    s->internal_buffer_count=0;
1116
}
1117

    
1118
char av_get_pict_type_char(int pict_type){
1119
    switch(pict_type){
1120
    case FF_I_TYPE: return 'I';
1121
    case FF_P_TYPE: return 'P';
1122
    case FF_B_TYPE: return 'B';
1123
    case FF_S_TYPE: return 'S';
1124
    case FF_SI_TYPE:return 'i';
1125
    case FF_SP_TYPE:return 'p';
1126
    case FF_BI_TYPE:return 'b';
1127
    default:        return '?';
1128
    }
1129
}
1130

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

    
1176
#if FF_API_OLD_SAMPLE_FMT
1177
int av_get_bits_per_sample_format(enum AVSampleFormat sample_fmt) {
1178
    return av_get_bits_per_sample_fmt(sample_fmt);
1179
}
1180
#endif
1181

    
1182
#if !HAVE_THREADS
1183
int ff_thread_init(AVCodecContext *s, int thread_count){
1184
    s->thread_count = thread_count;
1185
    return -1;
1186
}
1187
#endif
1188

    
1189
unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
1190
{
1191
    unsigned int n = 0;
1192

    
1193
    while(v >= 0xff) {
1194
        *s++ = 0xff;
1195
        v -= 0xff;
1196
        n++;
1197
    }
1198
    *s = v;
1199
    n++;
1200
    return n;
1201
}
1202

    
1203
#if LIBAVCODEC_VERSION_MAJOR < 53
1204
#include "libavcore/parseutils.h"
1205

    
1206
int av_parse_video_frame_size(int *width_ptr, int *height_ptr, const char *str)
1207
{
1208
    return av_parse_video_size(width_ptr, height_ptr, str);
1209
}
1210

    
1211
int av_parse_video_frame_rate(AVRational *frame_rate, const char *arg)
1212
{
1213
    return av_parse_video_rate(frame_rate, arg);
1214
}
1215
#endif
1216

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

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

    
1235
void av_log_ask_for_sample(void *avc, const char *msg)
1236
{
1237
    if (msg)
1238
        av_log(avc, AV_LOG_WARNING, "%s ", msg);
1239
    av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
1240
            "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
1241
            "and contact the ffmpeg-devel mailing list.\n");
1242
}
1243

    
1244
static AVHWAccel *first_hwaccel = NULL;
1245

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

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

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

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

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

    
1279
    ff_lockmgr_cb = cb;
1280

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

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

    
1296
#if !HAVE_PTHREADS
1297

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

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

    
1309
void ff_thread_finish_setup(AVCodecContext *avctx)
1310
{
1311
}
1312

    
1313
void ff_thread_report_progress(AVFrame *f, int progress, int field)
1314
{
1315
}
1316

    
1317
void ff_thread_await_progress(AVFrame *f, int progress, int field)
1318
{
1319
}
1320

    
1321
#endif
1322

    
1323
#if LIBAVCODEC_VERSION_MAJOR < 53
1324

    
1325
int avcodec_thread_init(AVCodecContext *s, int thread_count)
1326
{
1327
    return ff_thread_init(s, thread_count);
1328
}
1329

    
1330
void avcodec_thread_free(AVCodecContext *s)
1331
{
1332
    return ff_thread_free(s);
1333
}
1334

    
1335
#endif