rv34: Intra 16x16 handling
[libav.git] / libavcodec / utils.c
CommitLineData
de6d9b64
FB
1/*
2 * utils for libavcodec
406792e7 3 * Copyright (c) 2001 Fabrice Bellard
8f2ab833 4 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
de6d9b64 5 *
2912e87a 6 * This file is part of Libav.
b78e7197 7 *
2912e87a 8 * Libav is free software; you can redistribute it and/or
ff4ec49e
FB
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
b78e7197 11 * version 2.1 of the License, or (at your option) any later version.
de6d9b64 12 *
2912e87a 13 * Libav is distributed in the hope that it will be useful,
de6d9b64 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
ff4ec49e
FB
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
de6d9b64 17 *
ff4ec49e 18 * You should have received a copy of the GNU Lesser General Public
2912e87a 19 * License along with Libav; if not, write to the Free Software
5509bffa 20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
de6d9b64 21 */
115329f1 22
983e3246 23/**
ba87f080 24 * @file
983e3246
MN
25 * utils.
26 */
115329f1 27
7fb94406 28#include "libavutil/avstring.h"
245976da 29#include "libavutil/crc.h"
0ebcdf5c 30#include "libavutil/mathematics.h"
eb285cfe 31#include "libavutil/pixdesc.h"
737eb597
RT
32#include "libavutil/audioconvert.h"
33#include "libavutil/imgutils.h"
34#include "libavutil/samplefmt.h"
0b950fe2 35#include "libavutil/dict.h"
de6d9b64 36#include "avcodec.h"
3123dd79 37#include "dsputil.h"
6ed04040 38#include "libavutil/opt.h"
db7ae7d1 39#include "imgconvert.h"
37b00b47 40#include "thread.h"
9e82a113 41#include "audioconvert.h"
0ba39dd1 42#include "internal.h"
f13db94d 43#include "bytestream.h"
7246177d 44#include <stdlib.h>
9b879566 45#include <stdarg.h>
4c263142 46#include <limits.h>
860a40c8 47#include <float.h>
de6d9b64 48
ddebfb15 49static int volatile entangled_thread_counter=0;
bb875b75 50static int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op);
f988ce6c 51static void *codec_mutex;
2d1b6fb7 52static void *avformat_mutex;
ddebfb15 53
3453a231 54void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
8e1e6f31 55{
115329f1 56 if(min_size < *size)
8e1e6f31 57 return ptr;
115329f1 58
372c3f82 59 min_size= FFMAX(17*min_size/16 + 32, min_size);
8e1e6f31 60
372c3f82 61 ptr= av_realloc(ptr, min_size);
978805b2 62 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
372c3f82
MN
63 min_size= 0;
64
65 *size= min_size;
978805b2
MN
66
67 return ptr;
8e1e6f31
FB
68}
69
3453a231 70void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
238ef6da
RD
71{
72 void **p = ptr;
73 if (min_size < *size)
74 return;
372c3f82 75 min_size= FFMAX(17*min_size/16 + 32, min_size);
238ef6da 76 av_free(*p);
372c3f82
MN
77 *p = av_malloc(min_size);
78 if (!*p) min_size = 0;
79 *size= min_size;
238ef6da
RD
80}
81
de6d9b64 82/* encoder management */
e6df765e 83static AVCodec *first_avcodec = NULL;
de6d9b64 84
55b9e69a
MN
85AVCodec *av_codec_next(AVCodec *c){
86 if(c) return c->next;
87 else return first_avcodec;
88}
89
9ecfbb3e
AK
90#if !FF_API_AVCODEC_INIT
91static
92#endif
93void 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
85662f49 104void avcodec_register(AVCodec *codec)
de6d9b64
FB
105{
106 AVCodec **p;
7a961a46 107 avcodec_init();
de6d9b64
FB
108 p = &first_avcodec;
109 while (*p != NULL) p = &(*p)->next;
335a761a
SS
110 *p = codec;
111 codec->next = NULL;
d97efd7f
AK
112
113 if (codec->init_static_data)
114 codec->init_static_data(codec);
de6d9b64
FB
115}
116
0fb49b59
BB
117unsigned avcodec_get_edge_width(void)
118{
119 return EDGE_WIDTH;
120}
121
21adafec
MN
122void avcodec_set_dimensions(AVCodecContext *s, int width, int height){
123 s->coded_width = width;
124 s->coded_height= height;
125 s->width = -((-width )>>s->lowres);
126 s->height= -((-height)>>s->lowres);
127}
128
6a9c8594 129#define INTERNAL_BUFFER_SIZE (32+1)
1e491e29 130
560f773c
JR
131void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
132 int linesize_align[AV_NUM_DATA_POINTERS])
133{
134 int i;
115329f1
DB
135 int w_align= 1;
136 int h_align= 1;
137
f0bbfc4a
MN
138 switch(s->pix_fmt){
139 case PIX_FMT_YUV420P:
71e445fc 140 case PIX_FMT_YUYV422:
ebb177dd 141 case PIX_FMT_UYVY422:
f0bbfc4a 142 case PIX_FMT_YUV422P:
98c82d69 143 case PIX_FMT_YUV440P:
f0bbfc4a 144 case PIX_FMT_YUV444P:
db431f7e 145 case PIX_FMT_GBRP:
f0bbfc4a 146 case PIX_FMT_GRAY8:
34380af0
KS
147 case PIX_FMT_GRAY16BE:
148 case PIX_FMT_GRAY16LE:
f0bbfc4a
MN
149 case PIX_FMT_YUVJ420P:
150 case PIX_FMT_YUVJ422P:
98c82d69 151 case PIX_FMT_YUVJ440P:
f0bbfc4a 152 case PIX_FMT_YUVJ444P:
b70335a2 153 case PIX_FMT_YUVA420P:
42239ced
OA
154 case PIX_FMT_YUV420P9LE:
155 case PIX_FMT_YUV420P9BE:
156 case PIX_FMT_YUV420P10LE:
157 case PIX_FMT_YUV420P10BE:
dc49bf12
RB
158 case PIX_FMT_YUV422P9LE:
159 case PIX_FMT_YUV422P9BE:
5c511ad4
BC
160 case PIX_FMT_YUV422P10LE:
161 case PIX_FMT_YUV422P10BE:
da55ee6c
JGG
162 case PIX_FMT_YUV444P9LE:
163 case PIX_FMT_YUV444P9BE:
164 case PIX_FMT_YUV444P10LE:
165 case PIX_FMT_YUV444P10BE:
db431f7e
RB
166 case PIX_FMT_GBRP9LE:
167 case PIX_FMT_GBRP9BE:
168 case PIX_FMT_GBRP10LE:
169 case PIX_FMT_GBRP10BE:
37c0dc62
RC
170 w_align = 16; //FIXME assume 16 pixel per macroblock
171 h_align = 16 * 2; // interlaced needs 2 macroblocks height
f0bbfc4a
MN
172 break;
173 case PIX_FMT_YUV411P:
71e445fc 174 case PIX_FMT_UYYVYY411:
f0bbfc4a
MN
175 w_align=32;
176 h_align=8;
177 break;
178 case PIX_FMT_YUV410P:
179 if(s->codec_id == CODEC_ID_SVQ1){
180 w_align=64;
181 h_align=64;
182 }
d99fbbf4
RT
183 case PIX_FMT_RGB555:
184 if(s->codec_id == CODEC_ID_RPZA){
185 w_align=4;
186 h_align=4;
187 }
188 case PIX_FMT_PAL8:
6337178b
MN
189 case PIX_FMT_BGR8:
190 case PIX_FMT_RGB8:
d99fbbf4
RT
191 if(s->codec_id == CODEC_ID_SMC){
192 w_align=4;
193 h_align=4;
194 }
f0bbfc4a 195 break;
c31b8121
RT
196 case PIX_FMT_BGR24:
197 if((s->codec_id == CODEC_ID_MSZH) || (s->codec_id == CODEC_ID_ZLIB)){
198 w_align=4;
199 h_align=4;
200 }
201 break;
f0bbfc4a
MN
202 default:
203 w_align= 1;
204 h_align= 1;
205 break;
206 }
207
ef516f73
DC
208 *width = FFALIGN(*width , w_align);
209 *height= FFALIGN(*height, h_align);
ba68d9d3 210 if(s->codec_id == CODEC_ID_H264 || s->lowres)
ae4ffe9f 211 *height+=2; // some of the optimized chroma MC reads one line too much
ba68d9d3 212 // which is also done in mpeg decoders with lowres > 0
eb285cfe 213
560f773c
JR
214 for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
215 linesize_align[i] = STRIDE_ALIGN;
eb285cfe
RD
216//STRIDE_ALIGN is 8 for SSE* but this does not work for SVQ1 chroma planes
217//we could change STRIDE_ALIGN to 16 for x86/sse but it would increase the
218//picture size unneccessarily in some cases. The solution here is not
219//pretty and better ideas are welcome!
220#if HAVE_MMX
221 if(s->codec_id == CODEC_ID_SVQ1 || s->codec_id == CODEC_ID_VP5 ||
222 s->codec_id == CODEC_ID_VP6 || s->codec_id == CODEC_ID_VP6F ||
223 s->codec_id == CODEC_ID_VP6A) {
0eea2129
JR
224 for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
225 linesize_align[i] = 16;
eb285cfe
RD
226 }
227#endif
228}
229
230void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height){
231 int chroma_shift = av_pix_fmt_descriptors[s->pix_fmt].log2_chroma_w;
560f773c 232 int linesize_align[AV_NUM_DATA_POINTERS];
eb285cfe
RD
233 int align;
234 avcodec_align_dimensions2(s, width, height, linesize_align);
235 align = FFMAX(linesize_align[0], linesize_align[3]);
236 linesize_align[1] <<= chroma_shift;
237 linesize_align[2] <<= chroma_shift;
238 align = FFMAX3(align, linesize_align[1], linesize_align[2]);
239 *width=FFALIGN(*width, align);
f0bbfc4a
MN
240}
241
0eea2129
JR
242static 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
342static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
343{
1e491e29 344 int i;
f0bbfc4a
MN
345 int w= s->width;
346 int h= s->height;
d90cf87b 347 InternalBuffer *buf;
f3a29b75 348 AVCodecInternal *avci = s->internal;
0ecca7a4 349
65d999d6
MB
350 if(pic->data[0]!=NULL) {
351 av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
352 return -1;
353 }
f3a29b75
JR
354 if(avci->buffer_count >= INTERNAL_BUFFER_SIZE) {
355 av_log(s, AV_LOG_ERROR, "buffer_count overflow (missing release_buffer?)\n");
65d999d6
MB
356 return -1;
357 }
1e491e29 358
e16f217c 359 if(av_image_check_size(w, h, 0, s))
0ecca7a4
MN
360 return -1;
361
f3a29b75
JR
362 if (!avci->buffer) {
363 avci->buffer = av_mallocz((INTERNAL_BUFFER_SIZE+1) *
364 sizeof(InternalBuffer));
d90cf87b 365 }
115329f1 366
f3a29b75 367 buf = &avci->buffer[avci->buffer_count];
115329f1 368
0701006e 369 if(buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)){
37b00b47
AS
370 if(s->active_thread_type&FF_THREAD_FRAME) {
371 av_log_missing_feature(s, "Width/height changing with frame threads is", 0);
372 return -1;
373 }
374
560f773c 375 for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
0701006e
MN
376 av_freep(&buf->base[i]);
377 buf->data[i]= NULL;
378 }
379 }
380
8400b126 381 if (!buf->base[0]) {
f0bbfc4a 382 int h_chroma_shift, v_chroma_shift;
db7ae7d1
VS
383 int size[4] = {0};
384 int tmpsize;
c9d6e847 385 int unaligned;
c7622f9a 386 AVPicture picture;
560f773c 387 int stride_align[AV_NUM_DATA_POINTERS];
6e3ef511 388 const int pixel_size = av_pix_fmt_descriptors[s->pix_fmt].comp[0].step_minus1+1;
c7622f9a 389
1e491e29 390 avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
f0bbfc4a 391
eb285cfe 392 avcodec_align_dimensions2(s, &w, &h, stride_align);
115329f1 393
1e491e29
MN
394 if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
395 w+= EDGE_WIDTH*2;
396 h+= EDGE_WIDTH*2;
397 }
5b67307a 398
c9d6e847
RD
399 do {
400 // NOTE: do not align linesizes individually, this breaks e.g. assumptions
401 // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
9d2e0ad8 402 av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
c9d6e847
RD
403 // increase alignment of w for next try (rhs gives the lowest bit set in w)
404 w += w & ~(w-1);
db7ae7d1 405
c9d6e847 406 unaligned = 0;
45bae968 407 for (i=0; i<4; i++){
c9d6e847 408 unaligned |= picture.linesize[i] % stride_align[i];
45bae968 409 }
c9d6e847 410 } while (unaligned);
db7ae7d1 411
9d2e0ad8 412 tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
f8c96d01
RD
413 if (tmpsize < 0)
414 return -1;
db7ae7d1
VS
415
416 for (i=0; i<3 && picture.data[i+1]; i++)
417 size[i] = picture.data[i+1] - picture.data[i];
cf73e32a 418 size[i] = tmpsize - (picture.data[i] - picture.data[0]);
c7622f9a 419
c7622f9a
MN
420 memset(buf->base, 0, sizeof(buf->base));
421 memset(buf->data, 0, sizeof(buf->data));
1e491e29 422
b70335a2 423 for(i=0; i<4 && size[i]; i++){
2c19981a
MN
424 const int h_shift= i==0 ? 0 : h_chroma_shift;
425 const int v_shift= i==0 ? 0 : v_chroma_shift;
1e491e29 426
c7622f9a 427 buf->linesize[i]= picture.linesize[i];
1e491e29 428
c7622f9a 429 buf->base[i]= av_malloc(size[i]+16); //FIXME 16
d90cf87b 430 if(buf->base[i]==NULL) return -1;
c7622f9a
MN
431 memset(buf->base[i], 128, size[i]);
432
3491a9b2 433 // no edge if EDGE EMU or not planar YUV
6337178b 434 if((s->flags&CODEC_FLAG_EMU_EDGE) || !size[2])
d90cf87b 435 buf->data[i] = buf->base[i];
1e491e29 436 else
6e3ef511 437 buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (pixel_size*EDGE_WIDTH>>h_shift), stride_align[i]);
1e491e29 438 }
560f773c
JR
439 for (; i < AV_NUM_DATA_POINTERS; i++) {
440 buf->base[i] = buf->data[i] = NULL;
441 buf->linesize[i] = 0;
442 }
6337178b 443 if(size[1] && !size[2])
ed5d30d9 444 ff_set_systematic_pal2((uint32_t*)buf->data[1], s->pix_fmt);
0701006e
MN
445 buf->width = s->width;
446 buf->height = s->height;
447 buf->pix_fmt= s->pix_fmt;
1e491e29 448 }
237e4938 449 pic->type= FF_BUFFER_TYPE_INTERNAL;
1e491e29 450
560f773c 451 for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
d90cf87b
MN
452 pic->base[i]= buf->base[i];
453 pic->data[i]= buf->data[i];
237e4938 454 pic->linesize[i]= buf->linesize[i];
d90cf87b 455 }
0eea2129 456 pic->extended_data = pic->data;
f3a29b75 457 avci->buffer_count++;
d90cf87b 458
393cbb96
MN
459 if(s->pkt) pic->pkt_pts= s->pkt->pts;
460 else pic->pkt_pts= AV_NOPTS_VALUE;
79de84f2
MN
461 pic->reordered_opaque= s->reordered_opaque;
462
385c820b 463 if(s->debug&FF_DEBUG_BUFFERS)
f3a29b75
JR
464 av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d "
465 "buffers used\n", pic, avci->buffer_count);
385c820b 466
1e491e29
MN
467 return 0;
468}
469
0eea2129
JR
470int 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
492cd3a9 482void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
1e491e29 483 int i;
6829ac8d 484 InternalBuffer *buf, *last;
f3a29b75 485 AVCodecInternal *avci = s->internal;
d90cf87b 486
0eea2129
JR
487 assert(s->codec_type == AVMEDIA_TYPE_VIDEO);
488
4e00e76b 489 assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
f3a29b75
JR
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];
d90cf87b 502
a09bb3ba
MR
503 if (buf != last)
504 FFSWAP(InternalBuffer, *buf, *last);
6a9c8594 505 }
d90cf87b 506
560f773c 507 for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
1e491e29 508 pic->data[i]=NULL;
d90cf87b
MN
509// pic->base[i]=NULL;
510 }
1e491e29 511//printf("R%X\n", pic->opaque);
385c820b
AS
512
513 if(s->debug&FF_DEBUG_BUFFERS)
f3a29b75
JR
514 av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d "
515 "buffers used\n", pic, avci->buffer_count);
1e491e29
MN
516}
517
e1c2a5a0
RT
518int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
519 AVFrame temp_pic;
520 int i;
521
0eea2129
JR
522 assert(s->codec_type == AVMEDIA_TYPE_VIDEO);
523
e1c2a5a0
RT
524 /* If no picture return a new buffer */
525 if(pic->data[0] == NULL) {
526 /* We will copy from buffer, so must be readable */
527 pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
528 return s->get_buffer(s, pic);
529 }
530
531 /* If internal buffer type return the same buffer */
b8af4fe9 532 if(pic->type == FF_BUFFER_TYPE_INTERNAL) {
62ecd363
NG
533 if(s->pkt) pic->pkt_pts= s->pkt->pts;
534 else pic->pkt_pts= AV_NOPTS_VALUE;
b8af4fe9 535 pic->reordered_opaque= s->reordered_opaque;
e1c2a5a0 536 return 0;
b8af4fe9 537 }
e1c2a5a0
RT
538
539 /*
540 * Not internal type and reget_buffer not overridden, emulate cr buffer
541 */
542 temp_pic = *pic;
560f773c 543 for(i = 0; i < AV_NUM_DATA_POINTERS; i++)
e1c2a5a0
RT
544 pic->data[i] = pic->base[i] = NULL;
545 pic->opaque = NULL;
546 /* Allocate new frame */
547 if (s->get_buffer(s, pic))
548 return -1;
549 /* Copy image data from old buffer to new buffer */
636d6a4a 550 av_picture_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width,
e1c2a5a0
RT
551 s->height);
552 s->release_buffer(s, &temp_pic); // Release old frame
553 return 0;
554}
555
3a84713a 556int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
9c3d33d6
MN
557 int i;
558
559 for(i=0; i<count; i++){
3a84713a 560 int r= func(c, (char*)arg + i*size);
9c3d33d6
MN
561 if(ret) ret[i]= r;
562 }
563 return 0;
564}
565
8d23a86f
RD
566int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr),void *arg, int *ret, int count){
567 int i;
568
569 for(i=0; i<count; i++){
570 int r= func(c, arg, i, 0);
571 if(ret) ret[i]= r;
572 }
573 return 0;
574}
575
c269cf68
MN
576enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat *fmt){
577 while (*fmt != PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
578 ++fmt;
a33c7159
MN
579 return fmt[0];
580}
581
9740beff
MN
582void avcodec_get_frame_defaults(AVFrame *pic){
583 memset(pic, 0, sizeof(AVFrame));
584
585 pic->pts= AV_NOPTS_VALUE;
c342499d 586 pic->key_frame= 1;
b58dbb5b 587 pic->sample_aspect_ratio = (AVRational){0, 1};
8a4a5f6f 588 pic->format = -1; /* unknown */
9740beff
MN
589}
590
492cd3a9 591AVFrame *avcodec_alloc_frame(void){
9740beff 592 AVFrame *pic= av_malloc(sizeof(AVFrame));
115329f1 593
9740beff 594 if(pic==NULL) return NULL;
115329f1 595
9740beff 596 avcodec_get_frame_defaults(pic);
115329f1 597
1e491e29
MN
598 return pic;
599}
600
0b950fe2 601#if FF_API_AVCODEC_OPEN
5e4c7ca2 602int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
de6d9b64 603{
0b950fe2
AK
604 return avcodec_open2(avctx, codec, NULL);
605}
606#endif
607
608int attribute_align_arg avcodec_open2(AVCodecContext *avctx, AVCodec *codec, AVDictionary **options)
609{
31d76ec2 610 int ret = 0;
0b950fe2
AK
611 AVDictionary *tmp = NULL;
612
4df30f71
AC
613 if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
614 return AVERROR(EINVAL);
615
0b950fe2
AK
616 if (options)
617 av_dict_copy(&tmp, *options, 0);
115329f1 618
f988ce6c
619 /* If there is a user-supplied mutex locking routine, call it. */
620 if (ff_lockmgr_cb) {
621 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
622 return -1;
623 }
624
ddebfb15
MN
625 entangled_thread_counter++;
626 if(entangled_thread_counter != 1){
627 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
31d76ec2 628 ret = -1;
ddebfb15
MN
629 goto end;
630 }
de6d9b64 631
31d76ec2
JR
632 if(avctx->codec || !codec) {
633 ret = AVERROR(EINVAL);
ddebfb15 634 goto end;
31d76ec2 635 }
09770af8 636
f3a29b75
JR
637 avctx->internal = av_mallocz(sizeof(AVCodecInternal));
638 if (!avctx->internal) {
639 ret = AVERROR(ENOMEM);
640 goto end;
641 }
642
0edf8a7a 643 if (codec->priv_data_size > 0) {
dc51a72b 644 if(!avctx->priv_data){
0edf8a7a 645 avctx->priv_data = av_mallocz(codec->priv_data_size);
90f06cea
PI
646 if (!avctx->priv_data) {
647 ret = AVERROR(ENOMEM);
ddebfb15 648 goto end;
90f06cea 649 }
0b950fe2 650 if (codec->priv_class) {
dc51a72b
MN
651 *(AVClass**)avctx->priv_data= codec->priv_class;
652 av_opt_set_defaults(avctx->priv_data);
653 }
654 }
1d36fb13 655 if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
0b950fe2 656 goto free_and_end;
0edf8a7a
PG
657 } else {
658 avctx->priv_data = NULL;
659 }
0b950fe2
AK
660 if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
661 goto free_and_end;
21adafec
MN
662
663 if(avctx->coded_width && avctx->coded_height)
664 avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
665 else if(avctx->width && avctx->height)
666 avcodec_set_dimensions(avctx, avctx->width, avctx->height);
667
82eac2f3
RD
668 if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
669 && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
670 || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
671 av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
672 avcodec_set_dimensions(avctx, 0, 0);
673 }
674
f19c58b4
AJ
675 /* if the decoder init function was already called previously,
676 free the already allocated subtitle_header before overwriting it */
677 if (codec->decode)
678 av_freep(&avctx->subtitle_header);
679
fa77dd63 680#define SANE_NB_CHANNELS 128U
82eac2f3 681 if (avctx->channels > SANE_NB_CHANNELS) {
7868349a 682 ret = AVERROR(EINVAL);
2a9b5c9b 683 goto free_and_end;
0ecca7a4
MN
684 }
685
b5c85991 686 avctx->codec = codec;
72415b2a 687 if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
681c180d
RD
688 avctx->codec_id == CODEC_ID_NONE) {
689 avctx->codec_type = codec->type;
690 avctx->codec_id = codec->id;
691 }
e83c716e
AJ
692 if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
693 && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
4c0dda2b 694 av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
31d76ec2 695 ret = AVERROR(EINVAL);
2a9b5c9b 696 goto free_and_end;
4c0dda2b 697 }
b5c85991 698 avctx->frame_number = 0;
5ea0001f
DB
699#if FF_API_ER
700
701 av_log(avctx, AV_LOG_DEBUG, "err{or,}_recognition separate: %d; %d\n",
702 avctx->error_recognition, avctx->err_recognition);
703 /* FF_ER_CAREFUL (==1) implies AV_EF_CRCCHECK (== 1<<1 - 1),
704 FF_ER_COMPLIANT (==2) implies AV_EF_{CRCCHECK,BITSTREAM} (== 1<<2 - 1), et cetera} */
705 avctx->err_recognition |= (1<<(avctx->error_recognition-(avctx->error_recognition>=FF_ER_VERY_AGGRESSIVE))) - 1;
706 av_log(avctx, AV_LOG_DEBUG, "err{or,}_recognition combined: %d; %d\n",
707 avctx->error_recognition, avctx->err_recognition);
708#endif
37b00b47
AS
709
710 if (HAVE_THREADS && !avctx->thread_opaque) {
ba9ef8d0 711 ret = ff_thread_init(avctx);
37b00b47
AS
712 if (ret < 0) {
713 goto free_and_end;
714 }
715 }
b6064d9a
JG
716 if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
717 avctx->thread_count = 1;
37b00b47 718
527c91e3
CEH
719 if (avctx->codec->max_lowres < avctx->lowres) {
720 av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
721 avctx->codec->max_lowres);
31d76ec2 722 ret = AVERROR(EINVAL);
527c91e3
CEH
723 goto free_and_end;
724 }
8b00ab01 725 if (avctx->codec->encode) {
2cfa2d92 726 int i;
8b00ab01 727 if (avctx->codec->sample_fmts) {
3dfc3e70
JR
728 for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++)
729 if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
730 break;
731 if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
732 av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
733 ret = AVERROR(EINVAL);
734 goto free_and_end;
735 }
8b00ab01
JR
736 }
737 if (avctx->codec->supported_samplerates) {
738 for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
739 if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
740 break;
741 if (avctx->codec->supported_samplerates[i] == 0) {
742 av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
743 ret = AVERROR(EINVAL);
744 goto free_and_end;
745 }
746 }
747 if (avctx->codec->channel_layouts) {
748 if (!avctx->channel_layout) {
749 av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
750 } else {
751 for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
752 if (avctx->channel_layout == avctx->codec->channel_layouts[i])
753 break;
754 if (avctx->codec->channel_layouts[i] == 0) {
755 av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
756 ret = AVERROR(EINVAL);
757 goto free_and_end;
758 }
759 }
760 }
168f9e8c
JR
761 if (avctx->channel_layout && avctx->channels) {
762 if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
763 av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
764 ret = AVERROR(EINVAL);
765 goto free_and_end;
766 }
688b09fa
JR
767 } else if (avctx->channel_layout) {
768 avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
168f9e8c 769 }
2cfa2d92 770 }
0fd0ef79 771
37b00b47 772 if(avctx->codec->init && !(avctx->active_thread_type&FF_THREAD_FRAME)){
2de4f9eb
MN
773 ret = avctx->codec->init(avctx);
774 if (ret < 0) {
2a9b5c9b 775 goto free_and_end;
2de4f9eb 776 }
6e546aaa 777 }
ddebfb15
MN
778end:
779 entangled_thread_counter--;
f988ce6c
780
781 /* Release any user-supplied mutex. */
782 if (ff_lockmgr_cb) {
783 (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
784 }
0b950fe2
AK
785 if (options) {
786 av_dict_free(options);
787 *options = tmp;
788 }
789
ddebfb15 790 return ret;
2a9b5c9b 791free_and_end:
0b950fe2 792 av_dict_free(&tmp);
2a9b5c9b 793 av_freep(&avctx->priv_data);
f3a29b75 794 av_freep(&avctx->internal);
2a9b5c9b
MN
795 avctx->codec= NULL;
796 goto end;
de6d9b64
FB
797}
798
5e4c7ca2 799int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx, uint8_t *buf, int buf_size,
de6d9b64
FB
800 const short *samples)
801{
0ecca7a4 802 if(buf_size < FF_MIN_BUFFER_SIZE && 0){
5286d11f 803 av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
0ecca7a4
MN
804 return -1;
805 }
6f824977 806 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || samples){
8eb027c8 807 int ret = avctx->codec->encode(avctx, buf, buf_size, samples);
6f824977
MN
808 avctx->frame_number++;
809 return ret;
810 }else
811 return 0;
de6d9b64
FB
812}
813
5e4c7ca2 814int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
492cd3a9 815 const AVFrame *pict)
de6d9b64 816{
0ecca7a4 817 if(buf_size < FF_MIN_BUFFER_SIZE){
5286d11f 818 av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
0ecca7a4
MN
819 return -1;
820 }
e16f217c 821 if(av_image_check_size(avctx->width, avctx->height, 0, avctx))
0ecca7a4 822 return -1;
6f824977 823 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || pict){
8eb027c8 824 int ret = avctx->codec->encode(avctx, buf, buf_size, pict);
6f824977 825 avctx->frame_number++;
bb628dae 826 emms_c(); //needed to avoid an emms_c() call before every return;
115329f1 827
6f824977
MN
828 return ret;
829 }else
830 return 0;
de6d9b64
FB
831}
832
115329f1 833int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
240c1657
FB
834 const AVSubtitle *sub)
835{
836 int ret;
9413db9e
BA
837 if(sub->start_display_time) {
838 av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
839 return -1;
840 }
505aa6c9
BA
841 if(sub->num_rects == 0 || !sub->rects)
842 return -1;
8eb027c8 843 ret = avctx->codec->encode(avctx, buf, buf_size, sub);
240c1657
FB
844 avctx->frame_number++;
845 return ret;
846}
847
867f923d
MS
848static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
849{
850 int size = 0;
851 const uint8_t *data;
852 uint32_t flags;
853
854 if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE))
855 return;
856
857 data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
858 if (!data || size < 4)
859 return;
860 flags = bytestream_get_le32(&data);
861 size -= 4;
862 if (size < 4) /* Required for any of the changes */
863 return;
864 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
865 avctx->channels = bytestream_get_le32(&data);
866 size -= 4;
867 }
868 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
869 if (size < 8)
870 return;
871 avctx->channel_layout = bytestream_get_le64(&data);
872 size -= 8;
873 }
874 if (size < 4)
875 return;
876 if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
877 avctx->sample_rate = bytestream_get_le32(&data);
878 size -= 4;
879 }
880 if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
881 if (size < 8)
882 return;
883 avctx->width = bytestream_get_le32(&data);
884 avctx->height = bytestream_get_le32(&data);
c5d907b6 885 avcodec_set_dimensions(avctx, avctx->width, avctx->height);
867f923d
MS
886 size -= 8;
887 }
888}
889
7a00bbad
TB
890int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
891 int *got_picture_ptr,
892 AVPacket *avpkt)
893{
de6d9b64 894 int ret;
115329f1 895
53db1cae 896 *got_picture_ptr= 0;
e16f217c 897 if((avctx->coded_width||avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
0ecca7a4 898 return -1;
393cbb96
MN
899
900 avctx->pkt = avpkt;
c5d907b6 901 apply_param_change(avctx, avpkt);
393cbb96 902
37b00b47 903 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type&FF_THREAD_FRAME)){
27237d52 904 if (HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
37b00b47
AS
905 ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
906 avpkt);
907 else {
908 ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
909 avpkt);
910 picture->pkt_dts= avpkt->dts;
b58dbb5b 911 picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
3a2ddf7c
SS
912 picture->width = avctx->width;
913 picture->height = avctx->height;
8a4a5f6f 914 picture->format = avctx->pix_fmt;
37b00b47 915 }
6bb925f4 916
bb628dae 917 emms_c(); //needed to avoid an emms_c() call before every return;
115329f1
DB
918
919 if (*got_picture_ptr)
934982c4
MN
920 avctx->frame_number++;
921 }else
922 ret= 0;
923
de6d9b64
FB
924 return ret;
925}
926
0eea2129 927#if FF_API_OLD_DECODE_AUDIO
7a00bbad
TB
928int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
929 int *frame_size_ptr,
930 AVPacket *avpkt)
931{
0eea2129
JR
932 AVFrame frame;
933 int ret, got_frame = 0;
934
935 if (avctx->get_buffer != avcodec_default_get_buffer) {
e2ff436e
RT
936 av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with"
937 "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
938 av_log(avctx, AV_LOG_ERROR, "Please port your application to "
939 "avcodec_decode_audio4()\n");
940 avctx->get_buffer = avcodec_default_get_buffer;
0eea2129
JR
941 }
942
943 ret = avcodec_decode_audio4(avctx, &frame, &got_frame, avpkt);
944
945 if (ret >= 0 && got_frame) {
946 int ch, plane_size;
947 int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
948 int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
949 frame.nb_samples,
950 avctx->sample_fmt, 1);
951 if (*frame_size_ptr < data_size) {
952 av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
953 "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
954 return AVERROR(EINVAL);
955 }
956
957 memcpy(samples, frame.extended_data[0], plane_size);
958
959 if (planar && avctx->channels > 1) {
960 uint8_t *out = ((uint8_t *)samples) + plane_size;
961 for (ch = 1; ch < avctx->channels; ch++) {
962 memcpy(out, frame.extended_data[ch], plane_size);
963 out += plane_size;
964 }
965 }
966 *frame_size_ptr = data_size;
967 } else {
968 *frame_size_ptr = 0;
969 }
970 return ret;
971}
972#endif
973
974int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
975 AVFrame *frame,
976 int *got_frame_ptr,
977 AVPacket *avpkt)
978{
979 int ret = 0;
980
981 *got_frame_ptr = 0;
de6d9b64 982
393cbb96
MN
983 avctx->pkt = avpkt;
984
6326afd5
JR
985 if (!avpkt->data && avpkt->size) {
986 av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
987 return AVERROR(EINVAL);
988 }
989
f13db94d
MS
990 apply_param_change(avctx, avpkt);
991
0eea2129
JR
992 if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
993 ret = avctx->codec->decode(avctx, frame, got_frame_ptr, avpkt);
994 if (ret >= 0 && *got_frame_ptr) {
995 avctx->frame_number++;
996 frame->pkt_dts = avpkt->dts;
8a4a5f6f
SS
997 if (frame->format == AV_SAMPLE_FMT_NONE)
998 frame->format = avctx->sample_fmt;
9c856d62 999 }
ac66834c 1000 }
de6d9b64
FB
1001 return ret;
1002}
1003
7a00bbad
TB
1004int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
1005 int *got_sub_ptr,
1006 AVPacket *avpkt)
1007{
240c1657
FB
1008 int ret;
1009
393cbb96 1010 avctx->pkt = avpkt;
240c1657 1011 *got_sub_ptr = 0;
7a00bbad 1012 ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
240c1657
FB
1013 if (*got_sub_ptr)
1014 avctx->frame_number++;
1015 return ret;
1016}
1017
e1d7c883
RD
1018void avsubtitle_free(AVSubtitle *sub)
1019{
1020 int i;
1021
1022 for (i = 0; i < sub->num_rects; i++)
1023 {
8b834ac5
RD
1024 av_freep(&sub->rects[i]->pict.data[0]);
1025 av_freep(&sub->rects[i]->pict.data[1]);
1026 av_freep(&sub->rects[i]->pict.data[2]);
1027 av_freep(&sub->rects[i]->pict.data[3]);
1028 av_freep(&sub->rects[i]->text);
1029 av_freep(&sub->rects[i]->ass);
1030 av_freep(&sub->rects[i]);
e1d7c883
RD
1031 }
1032
8b834ac5 1033 av_freep(&sub->rects);
e1d7c883
RD
1034
1035 memset(sub, 0, sizeof(AVSubtitle));
1036}
1037
0752cd39 1038av_cold int avcodec_close(AVCodecContext *avctx)
de6d9b64 1039{
f988ce6c
1040 /* If there is a user-supplied mutex locking routine, call it. */
1041 if (ff_lockmgr_cb) {
1042 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
1043 return -1;
1044 }
1045
ddebfb15
MN
1046 entangled_thread_counter++;
1047 if(entangled_thread_counter != 1){
1048 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
1049 entangled_thread_counter--;
1050 return -1;
1051 }
1052
49fb20cb 1053 if (HAVE_THREADS && avctx->thread_opaque)
c0b102ca 1054 ff_thread_free(avctx);
dccda293 1055 if (avctx->codec && avctx->codec->close)
de6d9b64 1056 avctx->codec->close(avctx);
eea8c08f 1057 avcodec_default_free_buffers(avctx);
b3ab3199 1058 avctx->coded_frame = NULL;
f3a29b75 1059 av_freep(&avctx->internal);
ceff045d 1060 if (avctx->codec && avctx->codec->priv_class)
36773283
AK
1061 av_opt_free(avctx->priv_data);
1062 av_opt_free(avctx);
3123dd79 1063 av_freep(&avctx->priv_data);
77a670e7 1064 if(avctx->codec && avctx->codec->encode)
c4f267ab 1065 av_freep(&avctx->extradata);
de6d9b64 1066 avctx->codec = NULL;
37b00b47 1067 avctx->active_thread_type = 0;
ddebfb15 1068 entangled_thread_counter--;
f988ce6c
1069
1070 /* Release any user-supplied mutex. */
1071 if (ff_lockmgr_cb) {
1072 (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
1073 }
de6d9b64
FB
1074 return 0;
1075}
1076
1077AVCodec *avcodec_find_encoder(enum CodecID id)
1078{
93ebfeea 1079 AVCodec *p, *experimental=NULL;
de6d9b64
FB
1080 p = first_avcodec;
1081 while (p) {
93ebfeea
JG
1082 if (p->encode != NULL && p->id == id) {
1083 if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
1084 experimental = p;
1085 } else
1086 return p;
1087 }
de6d9b64
FB
1088 p = p->next;
1089 }
93ebfeea 1090 return experimental;
de6d9b64
FB
1091}
1092
98f3b098
A
1093AVCodec *avcodec_find_encoder_by_name(const char *name)
1094{
1095 AVCodec *p;
fc228c90
AJ
1096 if (!name)
1097 return NULL;
98f3b098
A
1098 p = first_avcodec;
1099 while (p) {
1100 if (p->encode != NULL && strcmp(name,p->name) == 0)
1101 return p;
1102 p = p->next;
1103 }
1104 return NULL;
1105}
1106
de6d9b64
FB
1107AVCodec *avcodec_find_decoder(enum CodecID id)
1108{
1109 AVCodec *p;
1110 p = first_avcodec;
1111 while (p) {
1112 if (p->decode != NULL && p->id == id)
1113 return p;
1114 p = p->next;
1115 }
1116 return NULL;
1117}
1118
1119AVCodec *avcodec_find_decoder_by_name(const char *name)
1120{
1121 AVCodec *p;
fc228c90
AJ
1122 if (!name)
1123 return NULL;
de6d9b64
FB
1124 p = first_avcodec;
1125 while (p) {
1126 if (p->decode != NULL && strcmp(name,p->name) == 0)
1127 return p;
1128 p = p->next;
1129 }
1130 return NULL;
1131}
1132
406aa93f 1133static int get_bit_rate(AVCodecContext *ctx)
ce34ff6b
RK
1134{
1135 int bit_rate;
1136 int bits_per_sample;
1137
1138 switch(ctx->codec_type) {
72415b2a 1139 case AVMEDIA_TYPE_VIDEO:
4563cf24
SS
1140 case AVMEDIA_TYPE_DATA:
1141 case AVMEDIA_TYPE_SUBTITLE:
1142 case AVMEDIA_TYPE_ATTACHMENT:
ce34ff6b
RK
1143 bit_rate = ctx->bit_rate;
1144 break;
72415b2a 1145 case AVMEDIA_TYPE_AUDIO:
ce34ff6b
RK
1146 bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
1147 bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
1148 break;
ce34ff6b
RK
1149 default:
1150 bit_rate = 0;
1151 break;
1152 }
1153 return bit_rate;
1154}
1155
7e566bbe
SS
1156size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
1157{
1158 int i, len, ret = 0;
1159
1160 for (i = 0; i < 4; i++) {
1161 len = snprintf(buf, buf_size,
1162 isprint(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
1163 buf += len;
1164 buf_size = buf_size > len ? buf_size - len : 0;
1165 ret += len;
1166 codec_tag>>=8;
1167 }
1168 return ret;
1169}
1170
de6d9b64
FB
1171void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
1172{
1173 const char *codec_name;
2a81f4bd 1174 const char *profile = NULL;
de6d9b64
FB
1175 AVCodec *p;
1176 char buf1[32];
a96b68b7 1177 int bitrate;
59771f71 1178 AVRational display_aspect_ratio;
de6d9b64
FB
1179
1180 if (encode)
1181 p = avcodec_find_encoder(enc->codec_id);
1182 else
1183 p = avcodec_find_decoder(enc->codec_id);
1184
1185 if (p) {
1186 codec_name = p->name;
2a81f4bd 1187 profile = av_get_profile_name(p, enc->profile);
985180a1
FB
1188 } else if (enc->codec_id == CODEC_ID_MPEG2TS) {
1189 /* fake mpeg2 transport stream codec (currently not
1190 registered) */
1191 codec_name = "mpeg2ts";
de6d9b64
FB
1192 } else if (enc->codec_name[0] != '\0') {
1193 codec_name = enc->codec_name;
1194 } else {
1195 /* output avi tags */
ab0b5378
SS
1196 char tag_buf[32];
1197 av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
1198 snprintf(buf1, sizeof(buf1), "%s / 0x%04X", tag_buf, enc->codec_tag);
de6d9b64
FB
1199 codec_name = buf1;
1200 }
1201
1202 switch(enc->codec_type) {
72415b2a 1203 case AVMEDIA_TYPE_VIDEO:
de6d9b64
FB
1204 snprintf(buf, buf_size,
1205 "Video: %s%s",
7d1c3fc1 1206 codec_name, enc->mb_decision ? " (hq)" : "");
2a81f4bd
AH
1207 if (profile)
1208 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1209 " (%s)", profile);
82c0c4ba 1210 if (enc->pix_fmt != PIX_FMT_NONE) {
cf087595
FB
1211 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1212 ", %s",
94bed8e5 1213 av_get_pix_fmt_name(enc->pix_fmt));
cf087595 1214 }
de6d9b64
FB
1215 if (enc->width) {
1216 snprintf(buf + strlen(buf), buf_size - strlen(buf),
21189011
MN
1217 ", %dx%d",
1218 enc->width, enc->height);
7ee4dd02 1219 if (enc->sample_aspect_ratio.num) {
cbaf50f8
BC
1220 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
1221 enc->width*enc->sample_aspect_ratio.num,
1222 enc->height*enc->sample_aspect_ratio.den,
1223 1024*1024);
1224 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1225 " [PAR %d:%d DAR %d:%d]",
1226 enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
1227 display_aspect_ratio.num, display_aspect_ratio.den);
7ee4dd02 1228 }
a309073b 1229 if(av_log_get_level() >= AV_LOG_DEBUG){
9ce6c138 1230 int g= av_gcd(enc->time_base.num, enc->time_base.den);
21189011
MN
1231 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1232 ", %d/%d",
1233 enc->time_base.num/g, enc->time_base.den/g);
1234 }
de6d9b64 1235 }
4bfad535
FB
1236 if (encode) {
1237 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1238 ", q=%d-%d", enc->qmin, enc->qmax);
1239 }
de6d9b64 1240 break;
72415b2a 1241 case AVMEDIA_TYPE_AUDIO:
de6d9b64
FB
1242 snprintf(buf, buf_size,
1243 "Audio: %s",
1244 codec_name);
2a81f4bd
AH
1245 if (profile)
1246 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1247 " (%s)", profile);
de6d9b64
FB
1248 if (enc->sample_rate) {
1249 snprintf(buf + strlen(buf), buf_size - strlen(buf),
0d72e7d0 1250 ", %d Hz", enc->sample_rate);
de6d9b64 1251 }
0d72e7d0 1252 av_strlcat(buf, ", ", buf_size);
63e8d976 1253 av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
5d6e4c16 1254 if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
9e82a113 1255 snprintf(buf + strlen(buf), buf_size - strlen(buf),
ba7d6e79 1256 ", %s", av_get_sample_fmt_name(enc->sample_fmt));
9e82a113 1257 }
de6d9b64 1258 break;
72415b2a 1259 case AVMEDIA_TYPE_DATA:
985180a1 1260 snprintf(buf, buf_size, "Data: %s", codec_name);
240c1657 1261 break;
72415b2a 1262 case AVMEDIA_TYPE_SUBTITLE:
240c1657 1263 snprintf(buf, buf_size, "Subtitle: %s", codec_name);
985180a1 1264 break;
72415b2a 1265 case AVMEDIA_TYPE_ATTACHMENT:
f8d7c9d3 1266 snprintf(buf, buf_size, "Attachment: %s", codec_name);
f8d7c9d3 1267 break;
de6d9b64 1268 default:
9fe5a7b8
MN
1269 snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
1270 return;
de6d9b64 1271 }
4bfad535
FB
1272 if (encode) {
1273 if (enc->flags & CODEC_FLAG_PASS1)
1274 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1275 ", pass 1");
1276 if (enc->flags & CODEC_FLAG_PASS2)
1277 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1278 ", pass 2");
1279 }
406aa93f 1280 bitrate = get_bit_rate(enc);
a96b68b7 1281 if (bitrate != 0) {
115329f1 1282 snprintf(buf + strlen(buf), buf_size - strlen(buf),
a96b68b7 1283 ", %d kb/s", bitrate / 1000);
de6d9b64
FB
1284 }
1285}
1286
060ec0a8
AH
1287const char *av_get_profile_name(const AVCodec *codec, int profile)
1288{
1289 const AVProfile *p;
1290 if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
1291 return NULL;
1292
1293 for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
1294 if (p->profile == profile)
1295 return p->name;
1296
1297 return NULL;
1298}
1299
156e5023
NK
1300unsigned avcodec_version( void )
1301{
1302 return LIBAVCODEC_VERSION_INT;
1303}
cf087595 1304
41600690 1305const char *avcodec_configuration(void)
c1736936 1306{
29ba0911 1307 return LIBAV_CONFIGURATION;
c1736936
DB
1308}
1309
41600690 1310const char *avcodec_license(void)
c1736936
DB
1311{
1312#define LICENSE_PREFIX "libavcodec license: "
a03be6e1 1313 return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
c1736936
DB
1314}
1315
1c2a8c7f
MN
1316void avcodec_flush_buffers(AVCodecContext *avctx)
1317{
27237d52 1318 if(HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
37b00b47 1319 ff_thread_flush(avctx);
d1cf4591 1320 else if(avctx->codec->flush)
7a06ff14 1321 avctx->codec->flush(avctx);
1c2a8c7f
MN
1322}
1323
0eea2129
JR
1324static void video_free_buffers(AVCodecContext *s)
1325{
f3a29b75 1326 AVCodecInternal *avci = s->internal;
d90cf87b
MN
1327 int i, j;
1328
f3a29b75
JR
1329 if (!avci->buffer)
1330 return;
115329f1 1331
f3a29b75
JR
1332 if (avci->buffer_count)
1333 av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n",
1334 avci->buffer_count);
d90cf87b 1335 for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
f3a29b75 1336 InternalBuffer *buf = &avci->buffer[i];
d90cf87b
MN
1337 for(j=0; j<4; j++){
1338 av_freep(&buf->base[j]);
1339 buf->data[j]= NULL;
1340 }
1341 }
f3a29b75 1342 av_freep(&avci->buffer);
115329f1 1343
f3a29b75 1344 avci->buffer_count=0;
d90cf87b
MN
1345}
1346
0eea2129
JR
1347static void audio_free_buffers(AVCodecContext *avctx)
1348{
1349 AVCodecInternal *avci = avctx->internal;
1350 InternalBuffer *buf;
1351
1352 if (!avci->buffer)
1353 return;
1354 buf = avci->buffer;
1355
1356 if (buf->extended_data) {
1357 av_free(buf->extended_data[0]);
1358 if (buf->extended_data != buf->data)
1359 av_free(buf->extended_data);
1360 }
1361 av_freep(&avci->buffer);
1362}
1363
1364void avcodec_default_free_buffers(AVCodecContext *avctx)
1365{
1366 switch (avctx->codec_type) {
1367 case AVMEDIA_TYPE_VIDEO:
1368 video_free_buffers(avctx);
1369 break;
1370 case AVMEDIA_TYPE_AUDIO:
1371 audio_free_buffers(avctx);
1372 break;
1373 default:
1374 break;
1375 }
1376}
1377
bebe72f4 1378#if FF_API_OLD_FF_PICT_TYPES
d8085ea7 1379char av_get_pict_type_char(int pict_type){
bebe72f4 1380 return av_get_picture_type_char(pict_type);
d8085ea7 1381}
bebe72f4 1382#endif
d8085ea7 1383
ac3e1834
BC
1384int av_get_bits_per_sample(enum CodecID codec_id){
1385 switch(codec_id){
5da71469 1386 case CODEC_ID_ADPCM_SBPRO_2:
f1b163e0 1387 return 2;
5da71469 1388 case CODEC_ID_ADPCM_SBPRO_3:
f1b163e0 1389 return 3;
5da71469 1390 case CODEC_ID_ADPCM_SBPRO_4:
f1b163e0 1391 case CODEC_ID_ADPCM_CT:
07f2a575 1392 case CODEC_ID_ADPCM_IMA_WAV:
9ff6d079
JR
1393 case CODEC_ID_ADPCM_IMA_QT:
1394 case CODEC_ID_ADPCM_SWF:
9df9b810
DV
1395 case CODEC_ID_ADPCM_MS:
1396 case CODEC_ID_ADPCM_YAMAHA:
029f966c 1397 case CODEC_ID_ADPCM_G722:
f32fd318 1398 return 4;
ac3e1834
BC
1399 case CODEC_ID_PCM_ALAW:
1400 case CODEC_ID_PCM_MULAW:
1401 case CODEC_ID_PCM_S8:
1402 case CODEC_ID_PCM_U8:
9d49b8ff 1403 case CODEC_ID_PCM_ZORK:
ac3e1834
BC
1404 return 8;
1405 case CODEC_ID_PCM_S16BE:
1406 case CODEC_ID_PCM_S16LE:
725d86bf 1407 case CODEC_ID_PCM_S16LE_PLANAR:
ac3e1834
BC
1408 case CODEC_ID_PCM_U16BE:
1409 case CODEC_ID_PCM_U16LE:
1410 return 16;
1411 case CODEC_ID_PCM_S24DAUD:
1412 case CODEC_ID_PCM_S24BE:
1413 case CODEC_ID_PCM_S24LE:
1414 case CODEC_ID_PCM_U24BE:
1415 case CODEC_ID_PCM_U24LE:
1416 return 24;
1417 case CODEC_ID_PCM_S32BE:
1418 case CODEC_ID_PCM_S32LE:
1419 case CODEC_ID_PCM_U32BE:
1420 case CODEC_ID_PCM_U32LE:
aa29709e 1421 case CODEC_ID_PCM_F32BE:
143a5d6f 1422 case CODEC_ID_PCM_F32LE:
ac3e1834 1423 return 32;
143a5d6f
PR
1424 case CODEC_ID_PCM_F64BE:
1425 case CODEC_ID_PCM_F64LE:
1426 return 64;
ac3e1834
BC
1427 default:
1428 return 0;
1429 }
1430}
1431
6f84cd12 1432#if FF_API_OLD_SAMPLE_FMT
5d6e4c16 1433int av_get_bits_per_sample_format(enum AVSampleFormat sample_fmt) {
e6c52cee 1434 return av_get_bytes_per_sample(sample_fmt) << 3;
42c71907 1435}
6f84cd12 1436#endif
42c71907 1437
b250f9c6 1438#if !HAVE_THREADS
ba9ef8d0 1439int ff_thread_init(AVCodecContext *s){
ca8ad847
MN
1440 return -1;
1441}
1442#endif
ad2b531d
MR
1443
1444unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
1445{
1446 unsigned int n = 0;
1447
1448 while(v >= 0xff) {
1449 *s++ = 0xff;
1450 v -= 0xff;
1451 n++;
1452 }
1453 *s = v;
1454 n++;
1455 return n;
1456}
1005f542 1457
c46eeae2
MN
1458int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
1459 int i;
1460 for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
1461 return i;
1462}
1463
ce863d7f 1464void av_log_missing_feature(void *avc, const char *feature, int want_sample)
ea779d91 1465{
6001dad6 1466 av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your Libav "
db323491 1467 "version to the newest one from Git. If the problem still "
ea779d91 1468 "occurs, it means that your file has a feature which has not "
3fd3632f 1469 "been implemented.\n", feature);
ea779d91 1470 if(want_sample)
ce863d7f 1471 av_log_ask_for_sample(avc, NULL);
0ba39dd1
KG
1472}
1473
44f566b7 1474void av_log_ask_for_sample(void *avc, const char *msg, ...)
0ba39dd1 1475{
44f566b7
DB
1476 va_list argument_list;
1477
1478 va_start(argument_list, msg);
1479
0ba39dd1 1480 if (msg)
44f566b7 1481 av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
0ba39dd1 1482 av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
f0a41afd 1483 "of this file to ftp://upload.libav.org/incoming/ "
21de9204 1484 "and contact the libav-devel mailing list.\n");
44f566b7
DB
1485
1486 va_end(argument_list);
ea779d91 1487}
c895618b
MN
1488
1489static AVHWAccel *first_hwaccel = NULL;
1490
1491void av_register_hwaccel(AVHWAccel *hwaccel)
1492{
1493 AVHWAccel **p = &first_hwaccel;
1494 while (*p)
1495 p = &(*p)->next;
1496 *p = hwaccel;
1497 hwaccel->next = NULL;
1498}
414d9d7f
MN
1499
1500AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
1501{
1502 return hwaccel ? hwaccel->next : first_hwaccel;
1503}
6059f13c
MN
1504
1505AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
1506{
1507 AVHWAccel *hwaccel=NULL;
1508
1509 while((hwaccel= av_hwaccel_next(hwaccel))){
1510 if ( hwaccel->id == codec_id
1511 && hwaccel->pix_fmt == pix_fmt)
1512 return hwaccel;
1513 }
1514 return NULL;
1515}
f988ce6c
1516
1517int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
1518{
1519 if (ff_lockmgr_cb) {
1520 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
1521 return -1;
2d1b6fb7
MS
1522 if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
1523 return -1;
f988ce6c
1524 }
1525
1526 ff_lockmgr_cb = cb;
1527
1528 if (ff_lockmgr_cb) {
1529 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
1530 return -1;
2d1b6fb7
MS
1531 if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
1532 return -1;
1533 }
1534 return 0;
1535}
1536
1537int avpriv_lock_avformat(void)
1538{
1539 if (ff_lockmgr_cb) {
1540 if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
1541 return -1;
1542 }
1543 return 0;
1544}
1545
1546int avpriv_unlock_avformat(void)
1547{
1548 if (ff_lockmgr_cb) {
1549 if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
1550 return -1;
f988ce6c
1551 }
1552 return 0;
1553}
603a5f04 1554
0842d589 1555unsigned int avpriv_toupper4(unsigned int x)
603a5f04
FL
1556{
1557 return toupper( x &0xFF)
1558 + (toupper((x>>8 )&0xFF)<<8 )
1559 + (toupper((x>>16)&0xFF)<<16)
1560 + (toupper((x>>24)&0xFF)<<24);
1561}
37b00b47 1562
27237d52 1563#if !HAVE_THREADS
37b00b47
AS
1564
1565int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
1566{
1567 f->owner = avctx;
1568 return avctx->get_buffer(avctx, f);
1569}
1570
1571void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
1572{
1573 f->owner->release_buffer(f->owner, f);
1574}
1575
1576void ff_thread_finish_setup(AVCodecContext *avctx)
1577{
1578}
1579
1580void ff_thread_report_progress(AVFrame *f, int progress, int field)
1581{
1582}
1583
1584void ff_thread_await_progress(AVFrame *f, int progress, int field)
1585{
1586}
1587
1588#endif
65af48b5
AK
1589
1590#if FF_API_THREAD_INIT
1591int avcodec_thread_init(AVCodecContext *s, int thread_count)
1592{
1593 s->thread_count = thread_count;
1594 return ff_thread_init(s);
1595}
1596#endif
bca06e77
AK
1597
1598enum AVMediaType avcodec_get_type(enum CodecID codec_id)
1599{
1600 if (codec_id <= CODEC_ID_NONE)
1601 return AVMEDIA_TYPE_UNKNOWN;
1602 else if (codec_id < CODEC_ID_FIRST_AUDIO)
1603 return AVMEDIA_TYPE_VIDEO;
1604 else if (codec_id < CODEC_ID_FIRST_SUBTITLE)
1605 return AVMEDIA_TYPE_AUDIO;
1606 else if (codec_id < CODEC_ID_FIRST_UNKNOWN)
1607 return AVMEDIA_TYPE_SUBTITLE;
1608
1609 return AVMEDIA_TYPE_UNKNOWN;
1610}