~medibuntu-maintainers/mplayer/medibuntu.precise

« back to all changes in this revision

Viewing changes to ffmpeg/libavcodec/utils.c

  • Committer: Gauvain Pocentek
  • Date: 2012-03-06 11:59:12 UTC
  • mfrom: (66.1.15 precise)
  • Revision ID: gauvain@pocentek.net-20120306115912-h9d6kt9j0l532oo5
* Merge from Ubuntu:
  - put back faac support
  - recommends apport-hooks-medibuntu
  - change Maintainer, Uploaders & Vcs-* fields.
* New upstream snapshot
* upload to unstable
* Build against external libmpeg2
* drop 51_FTBFS_arm.patch again
* no longer build depend on libcdparanoia-dev on the Hurd
* Fix FTBFS on the hurd.
  Thanks to Samuel Thibault <sthibault@debian.org> (Closes: #654974)
* Fix FTBFS on arm
* New upstream snapshot, Closes: #650339, #643621, #481807
* Imported Upstream version 1.0~rc4+svn34492
* Bump standards version
* Bump dependency on libav >= 4:0.8~, Closes: #653887
* Fix build-indep
* Build mplayer-gui again, Closes: #568514
* Drop debian/all-lang-config-mak.sh, no longer needed
* include .dfsg1 in version number
* remove get-orig-source target
* no longer prune compiler flags from the environment
* No longer advertise nor build 3fdx, mga and dxr3 backends,
  Closes: #496106, #442181, #533546
* beautify mplayer version identification string
* Brown paperbag upload.
* Next try to fix build failure on sparce after recent binutils change.
* Brown paperbag upload.
* Really fix build failure on sparc after recent binutils change.
* Properly set Replaces/Conflicts on mplayer2{,-dbg} to avoid
  file overwrite errors.
* Adjust versioning of mplayer listed in the mplayer-dbg's Depends field.
* Fix build failure on sparc after recent binutils change.
* Urgency medium bumped because of RC-level bugfix
  and speeding up x264 transition.
* Update to my @debian.org email.
* Upload to unstable
* Enable joystick support on Linux only, Closes: #638408
* Rebuild fixes toolchain issue on arm, Closes: #637077
* New upstream snapshot
* following the discussion started by Diego Biurrun <diego@biurrun.de>
  in debian-devel, I have prepared a new packaging of 'mplayer'
  (with code that comes from CVS)
* the upstream tar.bz cannot be distributed by Debian, since it contains
   CSS code; so I am repackaging it 
* I have tried my best to address all known issues:
  - the package contains the detailed Copyright made by Diego Biurrun 
  - the package does not contain CSS code, or  AFAIK other code on which 
     there is active patent enforcement
  - there is a script  debian/cvs-changelog.sh  that shows all changes
     done to files included in this source.
    This should comply with GPLv2 sec 2.a  (in spirit if not in letter)
    For this reason, the source code contains CVS directories.
* needs   make (>= 3.80) for 'html-chunked-$(1)' in DOCS/xml/Makefile

* some corrections, as suggested Diego Biurrun
  - binary codecs should go into /usr/lib/codecs (upstream default)
  - better template 'mplayer/install_codecs'
  - an empty 'font=' in mplayer.conf breaks mplayer: postinst corrected
* correction in 'mplayer/cfgnote'
* better mplayer.postinst and mplayer.config

* New upstream release
* better debian/copyright file
* do not ship a skin
* New upstream release
* changed DEB_BUILD_OPTIONS to DEB_BUILD_CONFIGURE ,
  DEB_BUILD_OPTIONS is used as in debian policy
* use gcc-3.4
* changed xlibs-dev to a long list of dependencies, for Debian/etch
* try to adhere to  http://www.mplayerhq.hu/DOCS/tech/binary-packaging.txt
  (see README.Debian for details)
* removed dependency on xlibmesa-dev, disabled opengl
* New upstream release
* Simon McVittie <hacks@pseudorandom.co.uk> wonderful work:
- Work around Debian bug #267442 (glibc's sys/uio.h and gcc's altivec.h have
  conflicting uses for __vector) by re-ordering #includes
- Fix potential symlink attack in ./configure
- Disable support for binary codecs on platforms for which those codecs
  aren't available; also disable the corresponding Debconf note when it's
  inappropriate
- Changed Build-Depends: so it works in pbuilder
- Explicitly build-depend on libjpeg62-dev, libfontconfig1-dev,
  libungif4-dev 
- Tweak debian/rules to avoid certain errors being ignored
- Use --language=all
* provide a target  'debian/rules get-orig-source' 
  that recreates the orig.tar.gz ; then use the above orig.tar.gz
* rewrote some parts of debian/rules
* don't clean and recompile docs if upstream ships them
* mplayer-doc was shipping too much stuff
* translated man pages where not installed properly
* compile with libdv4-dev
* correct README.Debian
* Forgot build-dep on libtheora
* Must not depend on libxvidcore
* New upstream release
* new release.
* rc1 to become 0.90
* new pre-release
* new pre-release
* gtk bug fixed.
* new release.
* version bumped
* 0.60 pre2 release
* 0.60 pre-release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
 */
27
27
 
28
28
#include "libavutil/avstring.h"
29
 
#include "libavutil/integer.h"
30
29
#include "libavutil/crc.h"
 
30
#include "libavutil/mathematics.h"
31
31
#include "libavutil/pixdesc.h"
32
32
#include "libavutil/audioconvert.h"
33
33
#include "libavutil/imgutils.h"
34
34
#include "libavutil/samplefmt.h"
 
35
#include "libavutil/dict.h"
35
36
#include "avcodec.h"
36
37
#include "dsputil.h"
37
38
#include "libavutil/opt.h"
39
40
#include "thread.h"
40
41
#include "audioconvert.h"
41
42
#include "internal.h"
 
43
#include "bytestream.h"
42
44
#include <stdlib.h>
43
45
#include <stdarg.h>
44
46
#include <limits.h>
47
49
static int volatile entangled_thread_counter=0;
48
50
static int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op);
49
51
static void *codec_mutex;
 
52
static void *avformat_mutex;
50
53
 
51
54
void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
52
55
{
84
87
    else  return first_avcodec;
85
88
}
86
89
 
 
90
#if !FF_API_AVCODEC_INIT
 
91
static
 
92
#endif
 
93
void avcodec_init(void)
 
94
{
 
95
    static int initialized = 0;
 
96
 
 
97
    if (initialized != 0)
 
98
        return;
 
99
    initialized = 1;
 
100
 
 
101
    dsputil_static_init();
 
102
}
 
103
 
87
104
void avcodec_register(AVCodec *codec)
88
105
{
89
106
    AVCodec **p;
92
109
    while (*p != NULL) p = &(*p)->next;
93
110
    *p = codec;
94
111
    codec->next = NULL;
 
112
 
 
113
    if (codec->init_static_data)
 
114
        codec->init_static_data(codec);
95
115
}
96
116
 
97
117
unsigned avcodec_get_edge_width(void)
106
126
    s->height= -((-height)>>s->lowres);
107
127
}
108
128
 
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
129
#define INTERNAL_BUFFER_SIZE (32+1)
119
130
 
120
 
void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height, int linesize_align[4]){
 
131
void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
 
132
                               int linesize_align[AV_NUM_DATA_POINTERS])
 
133
{
 
134
    int i;
121
135
    int w_align= 1;
122
136
    int h_align= 1;
123
137
 
128
142
    case PIX_FMT_YUV422P:
129
143
    case PIX_FMT_YUV440P:
130
144
    case PIX_FMT_YUV444P:
 
145
    case PIX_FMT_GBRP:
131
146
    case PIX_FMT_GRAY8:
132
147
    case PIX_FMT_GRAY16BE:
133
148
    case PIX_FMT_GRAY16LE:
140
155
    case PIX_FMT_YUV420P9BE:
141
156
    case PIX_FMT_YUV420P10LE:
142
157
    case PIX_FMT_YUV420P10BE:
 
158
    case PIX_FMT_YUV422P9LE:
 
159
    case PIX_FMT_YUV422P9BE:
143
160
    case PIX_FMT_YUV422P10LE:
144
161
    case PIX_FMT_YUV422P10BE:
145
 
        w_align= 16; //FIXME check for non mpeg style codecs and use less alignment
146
 
        h_align= 16;
147
 
        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)
148
 
            h_align= 32; // interlaced is rounded up to 2 MBs
 
162
    case PIX_FMT_YUV444P9LE:
 
163
    case PIX_FMT_YUV444P9BE:
 
164
    case PIX_FMT_YUV444P10LE:
 
165
    case PIX_FMT_YUV444P10BE:
 
166
    case PIX_FMT_GBRP9LE:
 
167
    case PIX_FMT_GBRP9BE:
 
168
    case PIX_FMT_GBRP10LE:
 
169
    case PIX_FMT_GBRP10BE:
 
170
        w_align = 16; //FIXME assume 16 pixel per macroblock
 
171
        h_align = 16 * 2; // interlaced needs 2 macroblocks height
149
172
        break;
150
173
    case PIX_FMT_YUV411P:
151
174
    case PIX_FMT_UYYVYY411:
188
211
        *height+=2; // some of the optimized chroma MC reads one line too much
189
212
                    // which is also done in mpeg decoders with lowres > 0
190
213
 
191
 
    linesize_align[0] =
192
 
    linesize_align[1] =
193
 
    linesize_align[2] =
194
 
    linesize_align[3] = STRIDE_ALIGN;
 
214
    for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
 
215
        linesize_align[i] = STRIDE_ALIGN;
195
216
//STRIDE_ALIGN is 8 for SSE* but this does not work for SVQ1 chroma planes
196
217
//we could change STRIDE_ALIGN to 16 for x86/sse but it would increase the
197
218
//picture size unneccessarily in some cases. The solution here is not
200
221
    if(s->codec_id == CODEC_ID_SVQ1 || s->codec_id == CODEC_ID_VP5 ||
201
222
       s->codec_id == CODEC_ID_VP6 || s->codec_id == CODEC_ID_VP6F ||
202
223
       s->codec_id == CODEC_ID_VP6A) {
203
 
        linesize_align[0] =
204
 
        linesize_align[1] =
205
 
        linesize_align[2] = 16;
 
224
        for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
 
225
            linesize_align[i] = 16;
206
226
    }
207
227
#endif
208
228
}
209
229
 
210
230
void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height){
211
231
    int chroma_shift = av_pix_fmt_descriptors[s->pix_fmt].log2_chroma_w;
212
 
    int linesize_align[4];
 
232
    int linesize_align[AV_NUM_DATA_POINTERS];
213
233
    int align;
214
234
    avcodec_align_dimensions2(s, width, height, linesize_align);
215
235
    align = FFMAX(linesize_align[0], linesize_align[3]);
219
239
    *width=FFALIGN(*width, align);
220
240
}
221
241
 
222
 
int avcodec_default_get_buffer(AVCodecContext *s, AVFrame *pic){
 
242
static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
 
243
{
 
244
    AVCodecInternal *avci = avctx->internal;
 
245
    InternalBuffer *buf;
 
246
    int buf_size, ret, i, needs_extended_data;
 
247
 
 
248
    buf_size = av_samples_get_buffer_size(NULL, avctx->channels,
 
249
                                          frame->nb_samples, avctx->sample_fmt,
 
250
                                          32);
 
251
    if (buf_size < 0)
 
252
        return AVERROR(EINVAL);
 
253
 
 
254
    needs_extended_data = av_sample_fmt_is_planar(avctx->sample_fmt) &&
 
255
                          avctx->channels > AV_NUM_DATA_POINTERS;
 
256
 
 
257
    /* allocate InternalBuffer if needed */
 
258
    if (!avci->buffer) {
 
259
        avci->buffer = av_mallocz(sizeof(InternalBuffer));
 
260
        if (!avci->buffer)
 
261
            return AVERROR(ENOMEM);
 
262
    }
 
263
    buf = avci->buffer;
 
264
 
 
265
    /* if there is a previously-used internal buffer, check its size and
 
266
       channel count to see if we can reuse it */
 
267
    if (buf->extended_data) {
 
268
        /* if current buffer is too small, free it */
 
269
        if (buf->extended_data[0] && buf_size > buf->audio_data_size) {
 
270
            av_free(buf->extended_data[0]);
 
271
            if (buf->extended_data != buf->data)
 
272
                av_free(&buf->extended_data);
 
273
            buf->extended_data = NULL;
 
274
            buf->data[0] = NULL;
 
275
        }
 
276
        /* if number of channels has changed, reset and/or free extended data
 
277
           pointers but leave data buffer in buf->data[0] for reuse */
 
278
        if (buf->nb_channels != avctx->channels) {
 
279
            if (buf->extended_data != buf->data)
 
280
                av_free(buf->extended_data);
 
281
            buf->extended_data = NULL;
 
282
        }
 
283
    }
 
284
 
 
285
    /* if there is no previous buffer or the previous buffer cannot be used
 
286
       as-is, allocate a new buffer and/or rearrange the channel pointers */
 
287
    if (!buf->extended_data) {
 
288
        /* if the channel pointers will fit, just set extended_data to data,
 
289
           otherwise allocate the extended_data channel pointers */
 
290
        if (needs_extended_data) {
 
291
            buf->extended_data = av_mallocz(avctx->channels *
 
292
                                            sizeof(*buf->extended_data));
 
293
            if (!buf->extended_data)
 
294
                return AVERROR(ENOMEM);
 
295
        } else {
 
296
            buf->extended_data = buf->data;
 
297
        }
 
298
 
 
299
        /* if there is a previous buffer and it is large enough, reuse it and
 
300
           just fill-in new channel pointers and linesize, otherwise allocate
 
301
           a new buffer */
 
302
        if (buf->extended_data[0]) {
 
303
            ret = av_samples_fill_arrays(buf->extended_data, &buf->linesize[0],
 
304
                                         buf->extended_data[0], avctx->channels,
 
305
                                         frame->nb_samples, avctx->sample_fmt,
 
306
                                         32);
 
307
        } else {
 
308
            ret = av_samples_alloc(buf->extended_data, &buf->linesize[0],
 
309
                                   avctx->channels, frame->nb_samples,
 
310
                                   avctx->sample_fmt, 32);
 
311
        }
 
312
        if (ret)
 
313
            return ret;
 
314
 
 
315
        /* if data was not used for extended_data, we need to copy as many of
 
316
           the extended_data channel pointers as will fit */
 
317
        if (needs_extended_data) {
 
318
            for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
 
319
                buf->data[i] = buf->extended_data[i];
 
320
        }
 
321
        buf->audio_data_size = buf_size;
 
322
        buf->nb_channels     = avctx->channels;
 
323
    }
 
324
 
 
325
    /* copy InternalBuffer info to the AVFrame */
 
326
    frame->type          = FF_BUFFER_TYPE_INTERNAL;
 
327
    frame->extended_data = buf->extended_data;
 
328
    frame->linesize[0]   = buf->linesize[0];
 
329
    memcpy(frame->data, buf->data, sizeof(frame->data));
 
330
 
 
331
    if (avctx->pkt) frame->pkt_pts = avctx->pkt->pts;
 
332
    else            frame->pkt_pts = AV_NOPTS_VALUE;
 
333
    frame->reordered_opaque = avctx->reordered_opaque;
 
334
 
 
335
    if (avctx->debug & FF_DEBUG_BUFFERS)
 
336
        av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p, "
 
337
               "internal audio buffer used\n", frame);
 
338
 
 
339
    return 0;
 
340
}
 
341
 
 
342
static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
 
343
{
223
344
    int i;
224
345
    int w= s->width;
225
346
    int h= s->height;
226
347
    InternalBuffer *buf;
227
 
    int *picture_number;
 
348
    AVCodecInternal *avci = s->internal;
228
349
 
229
350
    if(pic->data[0]!=NULL) {
230
351
        av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
231
352
        return -1;
232
353
    }
233
 
    if(s->internal_buffer_count >= INTERNAL_BUFFER_SIZE) {
234
 
        av_log(s, AV_LOG_ERROR, "internal_buffer_count overflow (missing release_buffer?)\n");
 
354
    if(avci->buffer_count >= INTERNAL_BUFFER_SIZE) {
 
355
        av_log(s, AV_LOG_ERROR, "buffer_count overflow (missing release_buffer?)\n");
235
356
        return -1;
236
357
    }
237
358
 
238
359
    if(av_image_check_size(w, h, 0, s))
239
360
        return -1;
240
361
 
241
 
    if(s->internal_buffer==NULL){
242
 
        s->internal_buffer= av_mallocz((INTERNAL_BUFFER_SIZE+1)*sizeof(InternalBuffer));
 
362
    if (!avci->buffer) {
 
363
        avci->buffer = av_mallocz((INTERNAL_BUFFER_SIZE+1) *
 
364
                                  sizeof(InternalBuffer));
243
365
    }
244
 
#if 0
245
 
    s->internal_buffer= av_fast_realloc(
246
 
        s->internal_buffer,
247
 
        &s->internal_buffer_size,
248
 
        sizeof(InternalBuffer)*FFMAX(99,  s->internal_buffer_count+1)/*FIXME*/
249
 
        );
250
 
#endif
251
366
 
252
 
    buf= &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
253
 
    picture_number= &(((InternalBuffer*)s->internal_buffer)[INTERNAL_BUFFER_SIZE]).last_pic_num; //FIXME ugly hack
254
 
    (*picture_number)++;
 
367
    buf = &avci->buffer[avci->buffer_count];
255
368
 
256
369
    if(buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)){
257
370
        if(s->active_thread_type&FF_THREAD_FRAME) {
259
372
            return -1;
260
373
        }
261
374
 
262
 
        for(i=0; i<4; i++){
 
375
        for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
263
376
            av_freep(&buf->base[i]);
264
377
            buf->data[i]= NULL;
265
378
        }
266
379
    }
267
380
 
268
 
    if(buf->base[0]){
269
 
        pic->age= *picture_number - buf->last_pic_num;
270
 
        buf->last_pic_num= *picture_number;
271
 
    }else{
 
381
    if (!buf->base[0]) {
272
382
        int h_chroma_shift, v_chroma_shift;
273
383
        int size[4] = {0};
274
384
        int tmpsize;
275
385
        int unaligned;
276
386
        AVPicture picture;
277
 
        int stride_align[4];
 
387
        int stride_align[AV_NUM_DATA_POINTERS];
278
388
        const int pixel_size = av_pix_fmt_descriptors[s->pix_fmt].comp[0].step_minus1+1;
279
389
 
280
390
        avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
307
417
            size[i] = picture.data[i+1] - picture.data[i];
308
418
        size[i] = tmpsize - (picture.data[i] - picture.data[0]);
309
419
 
310
 
        buf->last_pic_num= -256*256*256*64;
311
420
        memset(buf->base, 0, sizeof(buf->base));
312
421
        memset(buf->data, 0, sizeof(buf->data));
313
422
 
327
436
            else
328
437
                buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (pixel_size*EDGE_WIDTH>>h_shift), stride_align[i]);
329
438
        }
 
439
        for (; i < AV_NUM_DATA_POINTERS; i++) {
 
440
            buf->base[i] = buf->data[i] = NULL;
 
441
            buf->linesize[i] = 0;
 
442
        }
330
443
        if(size[1] && !size[2])
331
444
            ff_set_systematic_pal2((uint32_t*)buf->data[1], s->pix_fmt);
332
445
        buf->width  = s->width;
333
446
        buf->height = s->height;
334
447
        buf->pix_fmt= s->pix_fmt;
335
 
        pic->age= 256*256*256*64;
336
448
    }
337
449
    pic->type= FF_BUFFER_TYPE_INTERNAL;
338
450
 
339
 
    for(i=0; i<4; i++){
 
451
    for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
340
452
        pic->base[i]= buf->base[i];
341
453
        pic->data[i]= buf->data[i];
342
454
        pic->linesize[i]= buf->linesize[i];
343
455
    }
344
 
    s->internal_buffer_count++;
 
456
    pic->extended_data = pic->data;
 
457
    avci->buffer_count++;
345
458
 
346
459
    if(s->pkt) pic->pkt_pts= s->pkt->pts;
347
460
    else       pic->pkt_pts= AV_NOPTS_VALUE;
348
461
    pic->reordered_opaque= s->reordered_opaque;
349
462
 
350
463
    if(s->debug&FF_DEBUG_BUFFERS)
351
 
        av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d buffers used\n", pic, s->internal_buffer_count);
 
464
        av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d "
 
465
               "buffers used\n", pic, avci->buffer_count);
352
466
 
353
467
    return 0;
354
468
}
355
469
 
 
470
int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
 
471
{
 
472
    switch (avctx->codec_type) {
 
473
    case AVMEDIA_TYPE_VIDEO:
 
474
        return video_get_buffer(avctx, frame);
 
475
    case AVMEDIA_TYPE_AUDIO:
 
476
        return audio_get_buffer(avctx, frame);
 
477
    default:
 
478
        return -1;
 
479
    }
 
480
}
 
481
 
356
482
void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
357
483
    int i;
358
484
    InternalBuffer *buf, *last;
 
485
    AVCodecInternal *avci = s->internal;
 
486
 
 
487
    assert(s->codec_type == AVMEDIA_TYPE_VIDEO);
359
488
 
360
489
    assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
361
 
    assert(s->internal_buffer_count);
362
 
 
363
 
    if(s->internal_buffer){
364
 
    buf = NULL; /* avoids warning */
365
 
    for(i=0; i<s->internal_buffer_count; i++){ //just 3-5 checks so is not worth to optimize
366
 
        buf= &((InternalBuffer*)s->internal_buffer)[i];
367
 
        if(buf->data[0] == pic->data[0])
368
 
            break;
369
 
    }
370
 
    assert(i < s->internal_buffer_count);
371
 
    s->internal_buffer_count--;
372
 
    last = &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
373
 
 
374
 
    FFSWAP(InternalBuffer, *buf, *last);
375
 
    }
376
 
 
377
 
    for(i=0; i<4; i++){
 
490
    assert(avci->buffer_count);
 
491
 
 
492
    if (avci->buffer) {
 
493
        buf = NULL; /* avoids warning */
 
494
        for (i = 0; i < avci->buffer_count; i++) { //just 3-5 checks so is not worth to optimize
 
495
            buf = &avci->buffer[i];
 
496
            if (buf->data[0] == pic->data[0])
 
497
                break;
 
498
        }
 
499
        assert(i < avci->buffer_count);
 
500
        avci->buffer_count--;
 
501
        last = &avci->buffer[avci->buffer_count];
 
502
 
 
503
        if (buf != last)
 
504
            FFSWAP(InternalBuffer, *buf, *last);
 
505
    }
 
506
 
 
507
    for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
378
508
        pic->data[i]=NULL;
379
509
//        pic->base[i]=NULL;
380
510
    }
381
511
//printf("R%X\n", pic->opaque);
382
512
 
383
513
    if(s->debug&FF_DEBUG_BUFFERS)
384
 
        av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d buffers used\n", pic, s->internal_buffer_count);
 
514
        av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d "
 
515
               "buffers used\n", pic, avci->buffer_count);
385
516
}
386
517
 
387
518
int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
388
519
    AVFrame temp_pic;
389
520
    int i;
390
521
 
 
522
    assert(s->codec_type == AVMEDIA_TYPE_VIDEO);
 
523
 
391
524
    /* If no picture return a new buffer */
392
525
    if(pic->data[0] == NULL) {
393
526
        /* We will copy from buffer, so must be readable */
407
540
     * Not internal type and reget_buffer not overridden, emulate cr buffer
408
541
     */
409
542
    temp_pic = *pic;
410
 
    for(i = 0; i < 4; i++)
 
543
    for(i = 0; i < AV_NUM_DATA_POINTERS; i++)
411
544
        pic->data[i] = pic->base[i] = NULL;
412
545
    pic->opaque = NULL;
413
546
    /* Allocate new frame */
451
584
 
452
585
    pic->pts= AV_NOPTS_VALUE;
453
586
    pic->key_frame= 1;
 
587
    pic->sample_aspect_ratio = (AVRational){0, 1};
 
588
    pic->format = -1;           /* unknown */
454
589
}
455
590
 
456
591
AVFrame *avcodec_alloc_frame(void){
463
598
    return pic;
464
599
}
465
600
 
 
601
#if FF_API_AVCODEC_OPEN
466
602
int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
467
603
{
 
604
    return avcodec_open2(avctx, codec, NULL);
 
605
}
 
606
#endif
 
607
 
 
608
int attribute_align_arg avcodec_open2(AVCodecContext *avctx, AVCodec *codec, AVDictionary **options)
 
609
{
468
610
    int ret = 0;
 
611
    AVDictionary *tmp = NULL;
 
612
 
 
613
    if (options)
 
614
        av_dict_copy(&tmp, *options, 0);
469
615
 
470
616
    /* If there is a user-supplied mutex locking routine, call it. */
471
617
    if (ff_lockmgr_cb) {
485
631
        goto end;
486
632
    }
487
633
 
 
634
    avctx->internal = av_mallocz(sizeof(AVCodecInternal));
 
635
    if (!avctx->internal) {
 
636
        ret = AVERROR(ENOMEM);
 
637
        goto end;
 
638
    }
 
639
 
488
640
    if (codec->priv_data_size > 0) {
489
641
      if(!avctx->priv_data){
490
642
        avctx->priv_data = av_mallocz(codec->priv_data_size);
492
644
            ret = AVERROR(ENOMEM);
493
645
            goto end;
494
646
        }
495
 
        if(codec->priv_class){ //this can be droped once all user apps use   avcodec_get_context_defaults3()
 
647
        if (codec->priv_class) {
496
648
            *(AVClass**)avctx->priv_data= codec->priv_class;
497
649
            av_opt_set_defaults(avctx->priv_data);
498
650
        }
499
651
      }
 
652
      if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
 
653
          goto free_and_end;
500
654
    } else {
501
655
        avctx->priv_data = NULL;
502
656
    }
 
657
    if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
 
658
        goto free_and_end;
503
659
 
504
660
    if(avctx->coded_width && avctx->coded_height)
505
661
        avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
537
693
        goto free_and_end;
538
694
    }
539
695
    avctx->frame_number = 0;
 
696
#if FF_API_ER
 
697
 
 
698
    av_log(avctx, AV_LOG_DEBUG, "err{or,}_recognition separate: %d; %d\n",
 
699
           avctx->error_recognition, avctx->err_recognition);
 
700
    /* FF_ER_CAREFUL (==1) implies AV_EF_CRCCHECK (== 1<<1 - 1),
 
701
       FF_ER_COMPLIANT (==2) implies AV_EF_{CRCCHECK,BITSTREAM} (== 1<<2 - 1), et cetera} */
 
702
    avctx->err_recognition |= (1<<(avctx->error_recognition-(avctx->error_recognition>=FF_ER_VERY_AGGRESSIVE))) - 1;
 
703
    av_log(avctx, AV_LOG_DEBUG, "err{or,}_recognition combined: %d; %d\n",
 
704
           avctx->error_recognition, avctx->err_recognition);
 
705
#endif
540
706
 
541
707
    if (HAVE_THREADS && !avctx->thread_opaque) {
542
708
        ret = ff_thread_init(avctx);
544
710
            goto free_and_end;
545
711
        }
546
712
    }
 
713
    if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
 
714
        avctx->thread_count = 1;
547
715
 
548
716
    if (avctx->codec->max_lowres < avctx->lowres) {
549
717
        av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
611
779
    if (ff_lockmgr_cb) {
612
780
        (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
613
781
    }
 
782
    if (options) {
 
783
        av_dict_free(options);
 
784
        *options = tmp;
 
785
    }
 
786
 
614
787
    return ret;
615
788
free_and_end:
 
789
    av_dict_free(&tmp);
616
790
    av_freep(&avctx->priv_data);
 
791
    av_freep(&avctx->internal);
617
792
    avctx->codec= NULL;
618
793
    goto end;
619
794
}
667
842
    return ret;
668
843
}
669
844
 
 
845
static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
 
846
{
 
847
    int size = 0;
 
848
    const uint8_t *data;
 
849
    uint32_t flags;
 
850
 
 
851
    if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE))
 
852
        return;
 
853
 
 
854
    data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
 
855
    if (!data || size < 4)
 
856
        return;
 
857
    flags = bytestream_get_le32(&data);
 
858
    size -= 4;
 
859
    if (size < 4) /* Required for any of the changes */
 
860
        return;
 
861
    if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
 
862
        avctx->channels = bytestream_get_le32(&data);
 
863
        size -= 4;
 
864
    }
 
865
    if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
 
866
        if (size < 8)
 
867
            return;
 
868
        avctx->channel_layout = bytestream_get_le64(&data);
 
869
        size -= 8;
 
870
    }
 
871
    if (size < 4)
 
872
        return;
 
873
    if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
 
874
        avctx->sample_rate = bytestream_get_le32(&data);
 
875
        size -= 4;
 
876
    }
 
877
    if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
 
878
        if (size < 8)
 
879
            return;
 
880
        avctx->width  = bytestream_get_le32(&data);
 
881
        avctx->height = bytestream_get_le32(&data);
 
882
        avcodec_set_dimensions(avctx, avctx->width, avctx->height);
 
883
        size -= 8;
 
884
    }
 
885
}
 
886
 
670
887
int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
671
888
                         int *got_picture_ptr,
672
889
                         AVPacket *avpkt)
678
895
        return -1;
679
896
 
680
897
    avctx->pkt = avpkt;
 
898
    apply_param_change(avctx, avpkt);
681
899
 
682
900
    if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type&FF_THREAD_FRAME)){
683
 
        if (HAVE_PTHREADS && avctx->active_thread_type&FF_THREAD_FRAME)
 
901
        if (HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
684
902
             ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
685
903
                                          avpkt);
686
904
        else {
687
905
            ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
688
906
                              avpkt);
689
907
            picture->pkt_dts= avpkt->dts;
 
908
            picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
 
909
            picture->width  = avctx->width;
 
910
            picture->height = avctx->height;
 
911
            picture->format = avctx->pix_fmt;
690
912
        }
691
913
 
692
914
        emms_c(); //needed to avoid an emms_c() call before every return;
699
921
    return ret;
700
922
}
701
923
 
 
924
#if FF_API_OLD_DECODE_AUDIO
702
925
int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
703
926
                         int *frame_size_ptr,
704
927
                         AVPacket *avpkt)
705
928
{
706
 
    int ret;
 
929
    AVFrame frame;
 
930
    int ret, got_frame = 0;
 
931
 
 
932
    if (avctx->get_buffer != avcodec_default_get_buffer) {
 
933
        av_log(avctx, AV_LOG_ERROR, "A custom get_buffer() cannot be used with "
 
934
               "avcodec_decode_audio3()\n");
 
935
        return AVERROR(EINVAL);
 
936
    }
 
937
 
 
938
    ret = avcodec_decode_audio4(avctx, &frame, &got_frame, avpkt);
 
939
 
 
940
    if (ret >= 0 && got_frame) {
 
941
        int ch, plane_size;
 
942
        int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
 
943
        int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
 
944
                                                   frame.nb_samples,
 
945
                                                   avctx->sample_fmt, 1);
 
946
        if (*frame_size_ptr < data_size) {
 
947
            av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
 
948
                   "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
 
949
            return AVERROR(EINVAL);
 
950
        }
 
951
 
 
952
        memcpy(samples, frame.extended_data[0], plane_size);
 
953
 
 
954
        if (planar && avctx->channels > 1) {
 
955
            uint8_t *out = ((uint8_t *)samples) + plane_size;
 
956
            for (ch = 1; ch < avctx->channels; ch++) {
 
957
                memcpy(out, frame.extended_data[ch], plane_size);
 
958
                out += plane_size;
 
959
            }
 
960
        }
 
961
        *frame_size_ptr = data_size;
 
962
    } else {
 
963
        *frame_size_ptr = 0;
 
964
    }
 
965
    return ret;
 
966
}
 
967
#endif
 
968
 
 
969
int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
 
970
                                              AVFrame *frame,
 
971
                                              int *got_frame_ptr,
 
972
                                              AVPacket *avpkt)
 
973
{
 
974
    int ret = 0;
 
975
 
 
976
    *got_frame_ptr = 0;
707
977
 
708
978
    avctx->pkt = avpkt;
709
979
 
710
 
    if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size){
711
 
        //FIXME remove the check below _after_ ensuring that all audio check that the available space is enough
712
 
        if(*frame_size_ptr < AVCODEC_MAX_AUDIO_FRAME_SIZE){
713
 
            av_log(avctx, AV_LOG_ERROR, "buffer smaller than AVCODEC_MAX_AUDIO_FRAME_SIZE\n");
714
 
            return -1;
715
 
        }
716
 
        if(*frame_size_ptr < FF_MIN_BUFFER_SIZE ||
717
 
        *frame_size_ptr < avctx->channels * avctx->frame_size * sizeof(int16_t)){
718
 
            av_log(avctx, AV_LOG_ERROR, "buffer %d too small\n", *frame_size_ptr);
719
 
            return -1;
720
 
        }
721
 
 
722
 
        ret = avctx->codec->decode(avctx, samples, frame_size_ptr, avpkt);
723
 
        avctx->frame_number++;
724
 
    }else{
725
 
        ret= 0;
726
 
        *frame_size_ptr=0;
 
980
    if (!avpkt->data && avpkt->size) {
 
981
        av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
 
982
        return AVERROR(EINVAL);
 
983
    }
 
984
 
 
985
    apply_param_change(avctx, avpkt);
 
986
 
 
987
    if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
 
988
        ret = avctx->codec->decode(avctx, frame, got_frame_ptr, avpkt);
 
989
        if (ret >= 0 && *got_frame_ptr) {
 
990
            avctx->frame_number++;
 
991
            frame->pkt_dts = avpkt->dts;
 
992
            if (frame->format == AV_SAMPLE_FMT_NONE)
 
993
                frame->format = avctx->sample_fmt;
 
994
        }
727
995
    }
728
996
    return ret;
729
997
}
783
1051
        avctx->codec->close(avctx);
784
1052
    avcodec_default_free_buffers(avctx);
785
1053
    avctx->coded_frame = NULL;
 
1054
    av_freep(&avctx->internal);
786
1055
    if (avctx->codec && avctx->codec->priv_class)
787
1056
        av_opt_free(avctx->priv_data);
788
1057
    av_opt_free(avctx);
1039
1308
    return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
1040
1309
}
1041
1310
 
1042
 
void avcodec_init(void)
1043
 
{
1044
 
    static int initialized = 0;
1045
 
 
1046
 
    if (initialized != 0)
1047
 
        return;
1048
 
    initialized = 1;
1049
 
 
1050
 
    dsputil_static_init();
1051
 
}
1052
 
 
1053
1311
void avcodec_flush_buffers(AVCodecContext *avctx)
1054
1312
{
1055
 
    if(HAVE_PTHREADS && avctx->active_thread_type&FF_THREAD_FRAME)
 
1313
    if(HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
1056
1314
        ff_thread_flush(avctx);
1057
 
    if(avctx->codec->flush)
 
1315
    else if(avctx->codec->flush)
1058
1316
        avctx->codec->flush(avctx);
1059
1317
}
1060
1318
 
1061
 
void avcodec_default_free_buffers(AVCodecContext *s){
 
1319
static void video_free_buffers(AVCodecContext *s)
 
1320
{
 
1321
    AVCodecInternal *avci = s->internal;
1062
1322
    int i, j;
1063
1323
 
1064
 
    if(s->internal_buffer==NULL) return;
 
1324
    if (!avci->buffer)
 
1325
        return;
1065
1326
 
1066
 
    if (s->internal_buffer_count)
1067
 
        av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n", s->internal_buffer_count);
 
1327
    if (avci->buffer_count)
 
1328
        av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n",
 
1329
               avci->buffer_count);
1068
1330
    for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
1069
 
        InternalBuffer *buf= &((InternalBuffer*)s->internal_buffer)[i];
 
1331
        InternalBuffer *buf = &avci->buffer[i];
1070
1332
        for(j=0; j<4; j++){
1071
1333
            av_freep(&buf->base[j]);
1072
1334
            buf->data[j]= NULL;
1073
1335
        }
1074
1336
    }
1075
 
    av_freep(&s->internal_buffer);
1076
 
 
1077
 
    s->internal_buffer_count=0;
 
1337
    av_freep(&avci->buffer);
 
1338
 
 
1339
    avci->buffer_count=0;
 
1340
}
 
1341
 
 
1342
static void audio_free_buffers(AVCodecContext *avctx)
 
1343
{
 
1344
    AVCodecInternal *avci = avctx->internal;
 
1345
    InternalBuffer *buf;
 
1346
 
 
1347
    if (!avci->buffer)
 
1348
        return;
 
1349
    buf = avci->buffer;
 
1350
 
 
1351
    if (buf->extended_data) {
 
1352
        av_free(buf->extended_data[0]);
 
1353
        if (buf->extended_data != buf->data)
 
1354
            av_free(buf->extended_data);
 
1355
    }
 
1356
    av_freep(&avci->buffer);
 
1357
}
 
1358
 
 
1359
void avcodec_default_free_buffers(AVCodecContext *avctx)
 
1360
{
 
1361
    switch (avctx->codec_type) {
 
1362
    case AVMEDIA_TYPE_VIDEO:
 
1363
        video_free_buffers(avctx);
 
1364
        break;
 
1365
    case AVMEDIA_TYPE_AUDIO:
 
1366
        audio_free_buffers(avctx);
 
1367
        break;
 
1368
    default:
 
1369
        break;
 
1370
    }
1078
1371
}
1079
1372
 
1080
1373
#if FF_API_OLD_FF_PICT_TYPES
1092
1385
    case CODEC_ID_ADPCM_SBPRO_4:
1093
1386
    case CODEC_ID_ADPCM_CT:
1094
1387
    case CODEC_ID_ADPCM_IMA_WAV:
 
1388
    case CODEC_ID_ADPCM_IMA_QT:
 
1389
    case CODEC_ID_ADPCM_SWF:
1095
1390
    case CODEC_ID_ADPCM_MS:
1096
1391
    case CODEC_ID_ADPCM_YAMAHA:
 
1392
    case CODEC_ID_ADPCM_G722:
1097
1393
        return 4;
1098
 
    case CODEC_ID_ADPCM_G722:
1099
1394
    case CODEC_ID_PCM_ALAW:
1100
1395
    case CODEC_ID_PCM_MULAW:
1101
1396
    case CODEC_ID_PCM_S8:
1219
1514
    if (ff_lockmgr_cb) {
1220
1515
        if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
1221
1516
            return -1;
 
1517
        if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
 
1518
            return -1;
1222
1519
    }
1223
1520
 
1224
1521
    ff_lockmgr_cb = cb;
1226
1523
    if (ff_lockmgr_cb) {
1227
1524
        if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
1228
1525
            return -1;
1229
 
    }
1230
 
    return 0;
1231
 
}
1232
 
 
1233
 
unsigned int ff_toupper4(unsigned int x)
 
1526
        if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
 
1527
            return -1;
 
1528
    }
 
1529
    return 0;
 
1530
}
 
1531
 
 
1532
int avpriv_lock_avformat(void)
 
1533
{
 
1534
    if (ff_lockmgr_cb) {
 
1535
        if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
 
1536
            return -1;
 
1537
    }
 
1538
    return 0;
 
1539
}
 
1540
 
 
1541
int avpriv_unlock_avformat(void)
 
1542
{
 
1543
    if (ff_lockmgr_cb) {
 
1544
        if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
 
1545
            return -1;
 
1546
    }
 
1547
    return 0;
 
1548
}
 
1549
 
 
1550
unsigned int avpriv_toupper4(unsigned int x)
1234
1551
{
1235
1552
    return     toupper( x     &0xFF)
1236
1553
            + (toupper((x>>8 )&0xFF)<<8 )
1238
1555
            + (toupper((x>>24)&0xFF)<<24);
1239
1556
}
1240
1557
 
1241
 
#if !HAVE_PTHREADS
 
1558
#if !HAVE_THREADS
1242
1559
 
1243
1560
int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
1244
1561
{
1272
1589
    return ff_thread_init(s);
1273
1590
}
1274
1591
#endif
 
1592
 
 
1593
enum AVMediaType avcodec_get_type(enum CodecID codec_id)
 
1594
{
 
1595
    if (codec_id <= CODEC_ID_NONE)
 
1596
        return AVMEDIA_TYPE_UNKNOWN;
 
1597
    else if (codec_id < CODEC_ID_FIRST_AUDIO)
 
1598
        return AVMEDIA_TYPE_VIDEO;
 
1599
    else if (codec_id < CODEC_ID_FIRST_SUBTITLE)
 
1600
        return AVMEDIA_TYPE_AUDIO;
 
1601
    else if (codec_id < CODEC_ID_FIRST_UNKNOWN)
 
1602
        return AVMEDIA_TYPE_SUBTITLE;
 
1603
 
 
1604
    return AVMEDIA_TYPE_UNKNOWN;
 
1605
}