Statistics
| Branch: | Revision:

ffmpeg / libavcodec / utils.c @ bebe72f4

History | View | Annotate | Download (35.3 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 Libav.
7
 *
8
 * Libav 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
 * Libav 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 Libav; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21
 */
22

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

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

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

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

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

    
58
    ptr= av_realloc(ptr, min_size);
59
    if(!ptr) //we could set this to the unmodified min_size but this is safer if the user lost the ptr and uses NULL now
60
        min_size= 0;
61

    
62
    *size= min_size;
63

    
64
    return ptr;
65
}
66

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

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

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

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

    
97
unsigned avcodec_get_edge_width(void)
98
{
99
    return EDGE_WIDTH;
100
}
101

    
102
void avcodec_set_dimensions(AVCodecContext *s, int width, int height){
103
    s->coded_width = width;
104
    s->coded_height= height;
105
    s->width = -((-width )>>s->lowres);
106
    s->height= -((-height)>>s->lowres);
107
}
108

    
109
typedef struct InternalBuffer{
110
    int last_pic_num;
111
    uint8_t *base[4];
112
    uint8_t *data[4];
113
    int linesize[4];
114
    int width, height;
115
    enum PixelFormat pix_fmt;
116
}InternalBuffer;
117

    
118
#define INTERNAL_BUFFER_SIZE 32
119

    
120
void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height, int linesize_align[4]){
121
    int w_align= 1;
122
    int h_align= 1;
123

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

    
179
    *width = FFALIGN(*width , w_align);
180
    *height= FFALIGN(*height, h_align);
181
    if(s->codec_id == CODEC_ID_H264 || s->lowres)
182
        *height+=2; // some of the optimized chroma MC reads one line too much
183
                    // which is also done in mpeg decoders with lowres > 0
184

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

    
204
void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height){
205
    int chroma_shift = av_pix_fmt_descriptors[s->pix_fmt].log2_chroma_w;
206
    int linesize_align[4];
207
    int align;
208
    avcodec_align_dimensions2(s, width, height, linesize_align);
209
    align = FFMAX(linesize_align[0], linesize_align[3]);
210
    linesize_align[1] <<= chroma_shift;
211
    linesize_align[2] <<= chroma_shift;
212
    align = FFMAX3(align, linesize_align[1], linesize_align[2]);
213
    *width=FFALIGN(*width, align);
214
}
215

    
216
int avcodec_default_get_buffer(AVCodecContext *s, AVFrame *pic){
217
    int i;
218
    int w= s->width;
219
    int h= s->height;
220
    InternalBuffer *buf;
221
    int *picture_number;
222

    
223
    if(pic->data[0]!=NULL) {
224
        av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
225
        return -1;
226
    }
227
    if(s->internal_buffer_count >= INTERNAL_BUFFER_SIZE) {
228
        av_log(s, AV_LOG_ERROR, "internal_buffer_count overflow (missing release_buffer?)\n");
229
        return -1;
230
    }
231

    
232
    if(av_image_check_size(w, h, 0, s))
233
        return -1;
234

    
235
    if(s->internal_buffer==NULL){
236
        s->internal_buffer= av_mallocz((INTERNAL_BUFFER_SIZE+1)*sizeof(InternalBuffer));
237
    }
238
#if 0
239
    s->internal_buffer= av_fast_realloc(
240
        s->internal_buffer,
241
        &s->internal_buffer_size,
242
        sizeof(InternalBuffer)*FFMAX(99,  s->internal_buffer_count+1)/*FIXME*/
243
        );
244
#endif
245

    
246
    buf= &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
247
    picture_number= &(((InternalBuffer*)s->internal_buffer)[INTERNAL_BUFFER_SIZE]).last_pic_num; //FIXME ugly hack
248
    (*picture_number)++;
249

    
250
    if(buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)){
251
        if(s->active_thread_type&FF_THREAD_FRAME) {
252
            av_log_missing_feature(s, "Width/height changing with frame threads is", 0);
253
            return -1;
254
        }
255

    
256
        for(i=0; i<4; i++){
257
            av_freep(&buf->base[i]);
258
            buf->data[i]= NULL;
259
        }
260
    }
261

    
262
    if(buf->base[0]){
263
        pic->age= *picture_number - buf->last_pic_num;
264
        buf->last_pic_num= *picture_number;
265
    }else{
266
        int h_chroma_shift, v_chroma_shift;
267
        int size[4] = {0};
268
        int tmpsize;
269
        int unaligned;
270
        AVPicture picture;
271
        int stride_align[4];
272

    
273
        avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
274

    
275
        avcodec_align_dimensions2(s, &w, &h, stride_align);
276

    
277
        if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
278
            w+= EDGE_WIDTH*2;
279
            h+= EDGE_WIDTH*2;
280
        }
281

    
282
        do {
283
            // NOTE: do not align linesizes individually, this breaks e.g. assumptions
284
            // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
285
            av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
286
            // increase alignment of w for next try (rhs gives the lowest bit set in w)
287
            w += w & ~(w-1);
288

    
289
            unaligned = 0;
290
            for (i=0; i<4; i++){
291
                unaligned |= picture.linesize[i] % stride_align[i];
292
            }
293
        } while (unaligned);
294

    
295
        tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
296
        if (tmpsize < 0)
297
            return -1;
298

    
299
        for (i=0; i<3 && picture.data[i+1]; i++)
300
            size[i] = picture.data[i+1] - picture.data[i];
301
        size[i] = tmpsize - (picture.data[i] - picture.data[0]);
302

    
303
        buf->last_pic_num= -256*256*256*64;
304
        memset(buf->base, 0, sizeof(buf->base));
305
        memset(buf->data, 0, sizeof(buf->data));
306

    
307
        for(i=0; i<4 && size[i]; i++){
308
            const int h_shift= i==0 ? 0 : h_chroma_shift;
309
            const int v_shift= i==0 ? 0 : v_chroma_shift;
310

    
311
            buf->linesize[i]= picture.linesize[i];
312

    
313
            buf->base[i]= av_malloc(size[i]+16); //FIXME 16
314
            if(buf->base[i]==NULL) return -1;
315
            memset(buf->base[i], 128, size[i]);
316

    
317
            // no edge if EDGE EMU or not planar YUV
318
            if((s->flags&CODEC_FLAG_EMU_EDGE) || !size[2])
319
                buf->data[i] = buf->base[i];
320
            else
321
                buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (EDGE_WIDTH>>h_shift), stride_align[i]);
322
        }
323
        if(size[1] && !size[2])
324
            ff_set_systematic_pal2((uint32_t*)buf->data[1], s->pix_fmt);
325
        buf->width  = s->width;
326
        buf->height = s->height;
327
        buf->pix_fmt= s->pix_fmt;
328
        pic->age= 256*256*256*64;
329
    }
330
    pic->type= FF_BUFFER_TYPE_INTERNAL;
331

    
332
    for(i=0; i<4; i++){
333
        pic->base[i]= buf->base[i];
334
        pic->data[i]= buf->data[i];
335
        pic->linesize[i]= buf->linesize[i];
336
    }
337
    s->internal_buffer_count++;
338

    
339
    if(s->pkt) pic->pkt_pts= s->pkt->pts;
340
    else       pic->pkt_pts= AV_NOPTS_VALUE;
341
    pic->reordered_opaque= s->reordered_opaque;
342

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

    
346
    return 0;
347
}
348

    
349
void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
350
    int i;
351
    InternalBuffer *buf, *last;
352

    
353
    assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
354
    assert(s->internal_buffer_count);
355

    
356
    buf = NULL; /* avoids warning */
357
    for(i=0; i<s->internal_buffer_count; i++){ //just 3-5 checks so is not worth to optimize
358
        buf= &((InternalBuffer*)s->internal_buffer)[i];
359
        if(buf->data[0] == pic->data[0])
360
            break;
361
    }
362
    assert(i < s->internal_buffer_count);
363
    s->internal_buffer_count--;
364
    last = &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
365

    
366
    FFSWAP(InternalBuffer, *buf, *last);
367

    
368
    for(i=0; i<4; i++){
369
        pic->data[i]=NULL;
370
//        pic->base[i]=NULL;
371
    }
372
//printf("R%X\n", pic->opaque);
373

    
374
    if(s->debug&FF_DEBUG_BUFFERS)
375
        av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d buffers used\n", pic, s->internal_buffer_count);
376
}
377

    
378
int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
379
    AVFrame temp_pic;
380
    int i;
381

    
382
    /* If no picture return a new buffer */
383
    if(pic->data[0] == NULL) {
384
        /* We will copy from buffer, so must be readable */
385
        pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
386
        return s->get_buffer(s, pic);
387
    }
388

    
389
    /* If internal buffer type return the same buffer */
390
    if(pic->type == FF_BUFFER_TYPE_INTERNAL) {
391
        if(s->pkt) pic->pkt_pts= s->pkt->pts;
392
        else       pic->pkt_pts= AV_NOPTS_VALUE;
393
        pic->reordered_opaque= s->reordered_opaque;
394
        return 0;
395
    }
396

    
397
    /*
398
     * Not internal type and reget_buffer not overridden, emulate cr buffer
399
     */
400
    temp_pic = *pic;
401
    for(i = 0; i < 4; i++)
402
        pic->data[i] = pic->base[i] = NULL;
403
    pic->opaque = NULL;
404
    /* Allocate new frame */
405
    if (s->get_buffer(s, pic))
406
        return -1;
407
    /* Copy image data from old buffer to new buffer */
408
    av_picture_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width,
409
             s->height);
410
    s->release_buffer(s, &temp_pic); // Release old frame
411
    return 0;
412
}
413

    
414
int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
415
    int i;
416

    
417
    for(i=0; i<count; i++){
418
        int r= func(c, (char*)arg + i*size);
419
        if(ret) ret[i]= r;
420
    }
421
    return 0;
422
}
423

    
424
int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr),void *arg, int *ret, int count){
425
    int i;
426

    
427
    for(i=0; i<count; i++){
428
        int r= func(c, arg, i, 0);
429
        if(ret) ret[i]= r;
430
    }
431
    return 0;
432
}
433

    
434
enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat *fmt){
435
    while (*fmt != PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
436
        ++fmt;
437
    return fmt[0];
438
}
439

    
440
void avcodec_get_frame_defaults(AVFrame *pic){
441
    memset(pic, 0, sizeof(AVFrame));
442

    
443
    pic->pts= AV_NOPTS_VALUE;
444
    pic->key_frame= 1;
445
}
446

    
447
AVFrame *avcodec_alloc_frame(void){
448
    AVFrame *pic= av_malloc(sizeof(AVFrame));
449

    
450
    if(pic==NULL) return NULL;
451

    
452
    avcodec_get_frame_defaults(pic);
453

    
454
    return pic;
455
}
456

    
457
int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
458
{
459
    int ret = 0;
460

    
461
    /* If there is a user-supplied mutex locking routine, call it. */
462
    if (ff_lockmgr_cb) {
463
        if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
464
            return -1;
465
    }
466

    
467
    entangled_thread_counter++;
468
    if(entangled_thread_counter != 1){
469
        av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
470
        ret = -1;
471
        goto end;
472
    }
473

    
474
    if(avctx->codec || !codec) {
475
        ret = AVERROR(EINVAL);
476
        goto end;
477
    }
478

    
479
    if (codec->priv_data_size > 0) {
480
      if(!avctx->priv_data){
481
        avctx->priv_data = av_mallocz(codec->priv_data_size);
482
        if (!avctx->priv_data) {
483
            ret = AVERROR(ENOMEM);
484
            goto end;
485
        }
486
        if(codec->priv_class){ //this can be droped once all user apps use   avcodec_get_context_defaults3()
487
            *(AVClass**)avctx->priv_data= codec->priv_class;
488
            av_opt_set_defaults(avctx->priv_data);
489
        }
490
      }
491
    } else {
492
        avctx->priv_data = NULL;
493
    }
494

    
495
    if(avctx->coded_width && avctx->coded_height)
496
        avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
497
    else if(avctx->width && avctx->height)
498
        avcodec_set_dimensions(avctx, avctx->width, avctx->height);
499

    
500
    if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
501
        && (  av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
502
           || av_image_check_size(avctx->width,       avctx->height,       0, avctx) < 0)) {
503
        av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
504
        avcodec_set_dimensions(avctx, 0, 0);
505
    }
506

    
507
    /* if the decoder init function was already called previously,
508
       free the already allocated subtitle_header before overwriting it */
509
    if (codec->decode)
510
        av_freep(&avctx->subtitle_header);
511

    
512
#define SANE_NB_CHANNELS 128U
513
    if (avctx->channels > SANE_NB_CHANNELS) {
514
        ret = AVERROR(EINVAL);
515
        goto free_and_end;
516
    }
517

    
518
    avctx->codec = codec;
519
    if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
520
        avctx->codec_id == CODEC_ID_NONE) {
521
        avctx->codec_type = codec->type;
522
        avctx->codec_id   = codec->id;
523
    }
524
    if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
525
                           && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
526
        av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
527
        ret = AVERROR(EINVAL);
528
        goto free_and_end;
529
    }
530
    avctx->frame_number = 0;
531

    
532
    if (HAVE_THREADS && !avctx->thread_opaque) {
533
        ret = ff_thread_init(avctx);
534
        if (ret < 0) {
535
            goto free_and_end;
536
        }
537
    }
538

    
539
    if (avctx->codec->max_lowres < avctx->lowres) {
540
        av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
541
               avctx->codec->max_lowres);
542
        ret = AVERROR(EINVAL);
543
        goto free_and_end;
544
    }
545
    if (avctx->codec->sample_fmts && avctx->codec->encode) {
546
        int i;
547
        for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++)
548
            if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
549
                break;
550
        if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
551
            av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
552
            ret = AVERROR(EINVAL);
553
            goto free_and_end;
554
        }
555
    }
556

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

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

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

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

    
606
        return ret;
607
    }else
608
        return 0;
609
}
610

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

    
626
int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
627
                         int *got_picture_ptr,
628
                         AVPacket *avpkt)
629
{
630
    int ret;
631

    
632
    *got_picture_ptr= 0;
633
    if((avctx->coded_width||avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
634
        return -1;
635

    
636
    avctx->pkt = avpkt;
637

    
638
    if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type&FF_THREAD_FRAME)){
639
        if (HAVE_PTHREADS && avctx->active_thread_type&FF_THREAD_FRAME)
640
             ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
641
                                          avpkt);
642
        else {
643
            ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
644
                              avpkt);
645
            picture->pkt_dts= avpkt->dts;
646
        }
647

    
648
        emms_c(); //needed to avoid an emms_c() call before every return;
649

    
650
        if (*got_picture_ptr)
651
            avctx->frame_number++;
652
    }else
653
        ret= 0;
654

    
655
    return ret;
656
}
657

    
658
int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
659
                         int *frame_size_ptr,
660
                         AVPacket *avpkt)
661
{
662
    int ret;
663

    
664
    avctx->pkt = avpkt;
665

    
666
    if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size){
667
        //FIXME remove the check below _after_ ensuring that all audio check that the available space is enough
668
        if(*frame_size_ptr < AVCODEC_MAX_AUDIO_FRAME_SIZE){
669
            av_log(avctx, AV_LOG_ERROR, "buffer smaller than AVCODEC_MAX_AUDIO_FRAME_SIZE\n");
670
            return -1;
671
        }
672
        if(*frame_size_ptr < FF_MIN_BUFFER_SIZE ||
673
        *frame_size_ptr < avctx->channels * avctx->frame_size * sizeof(int16_t)){
674
            av_log(avctx, AV_LOG_ERROR, "buffer %d too small\n", *frame_size_ptr);
675
            return -1;
676
        }
677

    
678
        ret = avctx->codec->decode(avctx, samples, frame_size_ptr, avpkt);
679
        avctx->frame_number++;
680
    }else{
681
        ret= 0;
682
        *frame_size_ptr=0;
683
    }
684
    return ret;
685
}
686

    
687
int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
688
                            int *got_sub_ptr,
689
                            AVPacket *avpkt)
690
{
691
    int ret;
692

    
693
    avctx->pkt = avpkt;
694
    *got_sub_ptr = 0;
695
    ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
696
    if (*got_sub_ptr)
697
        avctx->frame_number++;
698
    return ret;
699
}
700

    
701
void avsubtitle_free(AVSubtitle *sub)
702
{
703
    int i;
704

    
705
    for (i = 0; i < sub->num_rects; i++)
706
    {
707
        av_freep(&sub->rects[i]->pict.data[0]);
708
        av_freep(&sub->rects[i]->pict.data[1]);
709
        av_freep(&sub->rects[i]->pict.data[2]);
710
        av_freep(&sub->rects[i]->pict.data[3]);
711
        av_freep(&sub->rects[i]->text);
712
        av_freep(&sub->rects[i]->ass);
713
        av_freep(&sub->rects[i]);
714
    }
715

    
716
    av_freep(&sub->rects);
717

    
718
    memset(sub, 0, sizeof(AVSubtitle));
719
}
720

    
721
av_cold int avcodec_close(AVCodecContext *avctx)
722
{
723
    /* If there is a user-supplied mutex locking routine, call it. */
724
    if (ff_lockmgr_cb) {
725
        if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
726
            return -1;
727
    }
728

    
729
    entangled_thread_counter++;
730
    if(entangled_thread_counter != 1){
731
        av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
732
        entangled_thread_counter--;
733
        return -1;
734
    }
735

    
736
    if (HAVE_THREADS && avctx->thread_opaque)
737
        ff_thread_free(avctx);
738
    if (avctx->codec && avctx->codec->close)
739
        avctx->codec->close(avctx);
740
    avcodec_default_free_buffers(avctx);
741
    avctx->coded_frame = NULL;
742
    av_freep(&avctx->priv_data);
743
    if(avctx->codec && avctx->codec->encode)
744
        av_freep(&avctx->extradata);
745
    avctx->codec = NULL;
746
    avctx->active_thread_type = 0;
747
    entangled_thread_counter--;
748

    
749
    /* Release any user-supplied mutex. */
750
    if (ff_lockmgr_cb) {
751
        (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
752
    }
753
    return 0;
754
}
755

    
756
AVCodec *avcodec_find_encoder(enum CodecID id)
757
{
758
    AVCodec *p, *experimental=NULL;
759
    p = first_avcodec;
760
    while (p) {
761
        if (p->encode != NULL && p->id == id) {
762
            if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
763
                experimental = p;
764
            } else
765
                return p;
766
        }
767
        p = p->next;
768
    }
769
    return experimental;
770
}
771

    
772
AVCodec *avcodec_find_encoder_by_name(const char *name)
773
{
774
    AVCodec *p;
775
    if (!name)
776
        return NULL;
777
    p = first_avcodec;
778
    while (p) {
779
        if (p->encode != NULL && strcmp(name,p->name) == 0)
780
            return p;
781
        p = p->next;
782
    }
783
    return NULL;
784
}
785

    
786
AVCodec *avcodec_find_decoder(enum CodecID id)
787
{
788
    AVCodec *p;
789
    p = first_avcodec;
790
    while (p) {
791
        if (p->decode != NULL && p->id == id)
792
            return p;
793
        p = p->next;
794
    }
795
    return NULL;
796
}
797

    
798
AVCodec *avcodec_find_decoder_by_name(const char *name)
799
{
800
    AVCodec *p;
801
    if (!name)
802
        return NULL;
803
    p = first_avcodec;
804
    while (p) {
805
        if (p->decode != NULL && strcmp(name,p->name) == 0)
806
            return p;
807
        p = p->next;
808
    }
809
    return NULL;
810
}
811

    
812
static int get_bit_rate(AVCodecContext *ctx)
813
{
814
    int bit_rate;
815
    int bits_per_sample;
816

    
817
    switch(ctx->codec_type) {
818
    case AVMEDIA_TYPE_VIDEO:
819
    case AVMEDIA_TYPE_DATA:
820
    case AVMEDIA_TYPE_SUBTITLE:
821
    case AVMEDIA_TYPE_ATTACHMENT:
822
        bit_rate = ctx->bit_rate;
823
        break;
824
    case AVMEDIA_TYPE_AUDIO:
825
        bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
826
        bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
827
        break;
828
    default:
829
        bit_rate = 0;
830
        break;
831
    }
832
    return bit_rate;
833
}
834

    
835
size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
836
{
837
    int i, len, ret = 0;
838

    
839
    for (i = 0; i < 4; i++) {
840
        len = snprintf(buf, buf_size,
841
                       isprint(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
842
        buf      += len;
843
        buf_size  = buf_size > len ? buf_size - len : 0;
844
        ret      += len;
845
        codec_tag>>=8;
846
    }
847
    return ret;
848
}
849

    
850
void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
851
{
852
    const char *codec_name;
853
    const char *profile = NULL;
854
    AVCodec *p;
855
    char buf1[32];
856
    int bitrate;
857
    AVRational display_aspect_ratio;
858

    
859
    if (encode)
860
        p = avcodec_find_encoder(enc->codec_id);
861
    else
862
        p = avcodec_find_decoder(enc->codec_id);
863

    
864
    if (p) {
865
        codec_name = p->name;
866
        profile = av_get_profile_name(p, enc->profile);
867
    } else if (enc->codec_id == CODEC_ID_MPEG2TS) {
868
        /* fake mpeg2 transport stream codec (currently not
869
           registered) */
870
        codec_name = "mpeg2ts";
871
    } else if (enc->codec_name[0] != '\0') {
872
        codec_name = enc->codec_name;
873
    } else {
874
        /* output avi tags */
875
        char tag_buf[32];
876
        av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
877
        snprintf(buf1, sizeof(buf1), "%s / 0x%04X", tag_buf, enc->codec_tag);
878
        codec_name = buf1;
879
    }
880

    
881
    switch(enc->codec_type) {
882
    case AVMEDIA_TYPE_VIDEO:
883
        snprintf(buf, buf_size,
884
                 "Video: %s%s",
885
                 codec_name, enc->mb_decision ? " (hq)" : "");
886
        if (profile)
887
            snprintf(buf + strlen(buf), buf_size - strlen(buf),
888
                     " (%s)", profile);
889
        if (enc->pix_fmt != PIX_FMT_NONE) {
890
            snprintf(buf + strlen(buf), buf_size - strlen(buf),
891
                     ", %s",
892
                     avcodec_get_pix_fmt_name(enc->pix_fmt));
893
        }
894
        if (enc->width) {
895
            snprintf(buf + strlen(buf), buf_size - strlen(buf),
896
                     ", %dx%d",
897
                     enc->width, enc->height);
898
            if (enc->sample_aspect_ratio.num) {
899
                av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
900
                          enc->width*enc->sample_aspect_ratio.num,
901
                          enc->height*enc->sample_aspect_ratio.den,
902
                          1024*1024);
903
                snprintf(buf + strlen(buf), buf_size - strlen(buf),
904
                         " [PAR %d:%d DAR %d:%d]",
905
                         enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
906
                         display_aspect_ratio.num, display_aspect_ratio.den);
907
            }
908
            if(av_log_get_level() >= AV_LOG_DEBUG){
909
                int g= av_gcd(enc->time_base.num, enc->time_base.den);
910
                snprintf(buf + strlen(buf), buf_size - strlen(buf),
911
                     ", %d/%d",
912
                     enc->time_base.num/g, enc->time_base.den/g);
913
            }
914
        }
915
        if (encode) {
916
            snprintf(buf + strlen(buf), buf_size - strlen(buf),
917
                     ", q=%d-%d", enc->qmin, enc->qmax);
918
        }
919
        break;
920
    case AVMEDIA_TYPE_AUDIO:
921
        snprintf(buf, buf_size,
922
                 "Audio: %s",
923
                 codec_name);
924
        if (profile)
925
            snprintf(buf + strlen(buf), buf_size - strlen(buf),
926
                     " (%s)", profile);
927
        if (enc->sample_rate) {
928
            snprintf(buf + strlen(buf), buf_size - strlen(buf),
929
                     ", %d Hz", enc->sample_rate);
930
        }
931
        av_strlcat(buf, ", ", buf_size);
932
        av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
933
        if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
934
            snprintf(buf + strlen(buf), buf_size - strlen(buf),
935
                     ", %s", av_get_sample_fmt_name(enc->sample_fmt));
936
        }
937
        break;
938
    case AVMEDIA_TYPE_DATA:
939
        snprintf(buf, buf_size, "Data: %s", codec_name);
940
        break;
941
    case AVMEDIA_TYPE_SUBTITLE:
942
        snprintf(buf, buf_size, "Subtitle: %s", codec_name);
943
        break;
944
    case AVMEDIA_TYPE_ATTACHMENT:
945
        snprintf(buf, buf_size, "Attachment: %s", codec_name);
946
        break;
947
    default:
948
        snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
949
        return;
950
    }
951
    if (encode) {
952
        if (enc->flags & CODEC_FLAG_PASS1)
953
            snprintf(buf + strlen(buf), buf_size - strlen(buf),
954
                     ", pass 1");
955
        if (enc->flags & CODEC_FLAG_PASS2)
956
            snprintf(buf + strlen(buf), buf_size - strlen(buf),
957
                     ", pass 2");
958
    }
959
    bitrate = get_bit_rate(enc);
960
    if (bitrate != 0) {
961
        snprintf(buf + strlen(buf), buf_size - strlen(buf),
962
                 ", %d kb/s", bitrate / 1000);
963
    }
964
}
965

    
966
const char *av_get_profile_name(const AVCodec *codec, int profile)
967
{
968
    const AVProfile *p;
969
    if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
970
        return NULL;
971

    
972
    for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
973
        if (p->profile == profile)
974
            return p->name;
975

    
976
    return NULL;
977
}
978

    
979
unsigned avcodec_version( void )
980
{
981
  return LIBAVCODEC_VERSION_INT;
982
}
983

    
984
const char *avcodec_configuration(void)
985
{
986
    return LIBAV_CONFIGURATION;
987
}
988

    
989
const char *avcodec_license(void)
990
{
991
#define LICENSE_PREFIX "libavcodec license: "
992
    return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
993
}
994

    
995
void avcodec_init(void)
996
{
997
    static int initialized = 0;
998

    
999
    if (initialized != 0)
1000
        return;
1001
    initialized = 1;
1002

    
1003
    dsputil_static_init();
1004
}
1005

    
1006
void avcodec_flush_buffers(AVCodecContext *avctx)
1007
{
1008
    if(HAVE_PTHREADS && avctx->active_thread_type&FF_THREAD_FRAME)
1009
        ff_thread_flush(avctx);
1010
    if(avctx->codec->flush)
1011
        avctx->codec->flush(avctx);
1012
}
1013

    
1014
void avcodec_default_free_buffers(AVCodecContext *s){
1015
    int i, j;
1016

    
1017
    if(s->internal_buffer==NULL) return;
1018

    
1019
    if (s->internal_buffer_count)
1020
        av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n", s->internal_buffer_count);
1021
    for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
1022
        InternalBuffer *buf= &((InternalBuffer*)s->internal_buffer)[i];
1023
        for(j=0; j<4; j++){
1024
            av_freep(&buf->base[j]);
1025
            buf->data[j]= NULL;
1026
        }
1027
    }
1028
    av_freep(&s->internal_buffer);
1029

    
1030
    s->internal_buffer_count=0;
1031
}
1032

    
1033
#if FF_API_OLD_FF_PICT_TYPES
1034
char av_get_pict_type_char(int pict_type){
1035
    return av_get_picture_type_char(pict_type);
1036
}
1037
#endif
1038

    
1039
int av_get_bits_per_sample(enum CodecID codec_id){
1040
    switch(codec_id){
1041
    case CODEC_ID_ADPCM_SBPRO_2:
1042
        return 2;
1043
    case CODEC_ID_ADPCM_SBPRO_3:
1044
        return 3;
1045
    case CODEC_ID_ADPCM_SBPRO_4:
1046
    case CODEC_ID_ADPCM_CT:
1047
    case CODEC_ID_ADPCM_IMA_WAV:
1048
    case CODEC_ID_ADPCM_MS:
1049
    case CODEC_ID_ADPCM_YAMAHA:
1050
        return 4;
1051
    case CODEC_ID_ADPCM_G722:
1052
    case CODEC_ID_PCM_ALAW:
1053
    case CODEC_ID_PCM_MULAW:
1054
    case CODEC_ID_PCM_S8:
1055
    case CODEC_ID_PCM_U8:
1056
    case CODEC_ID_PCM_ZORK:
1057
        return 8;
1058
    case CODEC_ID_PCM_S16BE:
1059
    case CODEC_ID_PCM_S16LE:
1060
    case CODEC_ID_PCM_S16LE_PLANAR:
1061
    case CODEC_ID_PCM_U16BE:
1062
    case CODEC_ID_PCM_U16LE:
1063
        return 16;
1064
    case CODEC_ID_PCM_S24DAUD:
1065
    case CODEC_ID_PCM_S24BE:
1066
    case CODEC_ID_PCM_S24LE:
1067
    case CODEC_ID_PCM_U24BE:
1068
    case CODEC_ID_PCM_U24LE:
1069
        return 24;
1070
    case CODEC_ID_PCM_S32BE:
1071
    case CODEC_ID_PCM_S32LE:
1072
    case CODEC_ID_PCM_U32BE:
1073
    case CODEC_ID_PCM_U32LE:
1074
    case CODEC_ID_PCM_F32BE:
1075
    case CODEC_ID_PCM_F32LE:
1076
        return 32;
1077
    case CODEC_ID_PCM_F64BE:
1078
    case CODEC_ID_PCM_F64LE:
1079
        return 64;
1080
    default:
1081
        return 0;
1082
    }
1083
}
1084

    
1085
#if FF_API_OLD_SAMPLE_FMT
1086
int av_get_bits_per_sample_format(enum AVSampleFormat sample_fmt) {
1087
    return av_get_bits_per_sample_fmt(sample_fmt);
1088
}
1089
#endif
1090

    
1091
#if !HAVE_THREADS
1092
int ff_thread_init(AVCodecContext *s){
1093
    return -1;
1094
}
1095
#endif
1096

    
1097
unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
1098
{
1099
    unsigned int n = 0;
1100

    
1101
    while(v >= 0xff) {
1102
        *s++ = 0xff;
1103
        v -= 0xff;
1104
        n++;
1105
    }
1106
    *s = v;
1107
    n++;
1108
    return n;
1109
}
1110

    
1111
int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
1112
    int i;
1113
    for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
1114
    return i;
1115
}
1116

    
1117
void av_log_missing_feature(void *avc, const char *feature, int want_sample)
1118
{
1119
    av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your Libav "
1120
            "version to the newest one from Git. If the problem still "
1121
            "occurs, it means that your file has a feature which has not "
1122
            "been implemented.", feature);
1123
    if(want_sample)
1124
        av_log_ask_for_sample(avc, NULL);
1125
    else
1126
        av_log(avc, AV_LOG_WARNING, "\n");
1127
}
1128

    
1129
void av_log_ask_for_sample(void *avc, const char *msg, ...)
1130
{
1131
    va_list argument_list;
1132

    
1133
    va_start(argument_list, msg);
1134

    
1135
    if (msg)
1136
        av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
1137
    av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
1138
            "of this file to ftp://upload.libav.org/incoming/ "
1139
            "and contact the libav-devel mailing list.\n");
1140

    
1141
    va_end(argument_list);
1142
}
1143

    
1144
static AVHWAccel *first_hwaccel = NULL;
1145

    
1146
void av_register_hwaccel(AVHWAccel *hwaccel)
1147
{
1148
    AVHWAccel **p = &first_hwaccel;
1149
    while (*p)
1150
        p = &(*p)->next;
1151
    *p = hwaccel;
1152
    hwaccel->next = NULL;
1153
}
1154

    
1155
AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
1156
{
1157
    return hwaccel ? hwaccel->next : first_hwaccel;
1158
}
1159

    
1160
AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
1161
{
1162
    AVHWAccel *hwaccel=NULL;
1163

    
1164
    while((hwaccel= av_hwaccel_next(hwaccel))){
1165
        if (   hwaccel->id      == codec_id
1166
            && hwaccel->pix_fmt == pix_fmt)
1167
            return hwaccel;
1168
    }
1169
    return NULL;
1170
}
1171

    
1172
int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
1173
{
1174
    if (ff_lockmgr_cb) {
1175
        if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
1176
            return -1;
1177
    }
1178

    
1179
    ff_lockmgr_cb = cb;
1180

    
1181
    if (ff_lockmgr_cb) {
1182
        if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
1183
            return -1;
1184
    }
1185
    return 0;
1186
}
1187

    
1188
unsigned int ff_toupper4(unsigned int x)
1189
{
1190
    return     toupper( x     &0xFF)
1191
            + (toupper((x>>8 )&0xFF)<<8 )
1192
            + (toupper((x>>16)&0xFF)<<16)
1193
            + (toupper((x>>24)&0xFF)<<24);
1194
}
1195

    
1196
#if !HAVE_PTHREADS
1197

    
1198
int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
1199
{
1200
    f->owner = avctx;
1201
    return avctx->get_buffer(avctx, f);
1202
}
1203

    
1204
void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
1205
{
1206
    f->owner->release_buffer(f->owner, f);
1207
}
1208

    
1209
void ff_thread_finish_setup(AVCodecContext *avctx)
1210
{
1211
}
1212

    
1213
void ff_thread_report_progress(AVFrame *f, int progress, int field)
1214
{
1215
}
1216

    
1217
void ff_thread_await_progress(AVFrame *f, int progress, int field)
1218
{
1219
}
1220

    
1221
#endif
1222

    
1223
#if FF_API_THREAD_INIT
1224
int avcodec_thread_init(AVCodecContext *s, int thread_count)
1225
{
1226
    s->thread_count = thread_count;
1227
    return ff_thread_init(s);
1228
}
1229
#endif