Commit | Line | Data |
---|---|---|
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 | |
b2c75b6e | 28 | #include "libavutil/avassert.h" |
7fb94406 | 29 | #include "libavutil/avstring.h" |
245976da | 30 | #include "libavutil/crc.h" |
0ebcdf5c | 31 | #include "libavutil/mathematics.h" |
eb285cfe | 32 | #include "libavutil/pixdesc.h" |
737eb597 RT |
33 | #include "libavutil/audioconvert.h" |
34 | #include "libavutil/imgutils.h" | |
35 | #include "libavutil/samplefmt.h" | |
0b950fe2 | 36 | #include "libavutil/dict.h" |
de6d9b64 | 37 | #include "avcodec.h" |
3123dd79 | 38 | #include "dsputil.h" |
6ed04040 | 39 | #include "libavutil/opt.h" |
db7ae7d1 | 40 | #include "imgconvert.h" |
37b00b47 | 41 | #include "thread.h" |
9e82a113 | 42 | #include "audioconvert.h" |
0ba39dd1 | 43 | #include "internal.h" |
f13db94d | 44 | #include "bytestream.h" |
7246177d | 45 | #include <stdlib.h> |
9b879566 | 46 | #include <stdarg.h> |
4c263142 | 47 | #include <limits.h> |
860a40c8 | 48 | #include <float.h> |
de6d9b64 | 49 | |
ddebfb15 | 50 | static int volatile entangled_thread_counter=0; |
bb875b75 | 51 | static int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op); |
f988ce6c | 52 | static void *codec_mutex; |
2d1b6fb7 | 53 | static void *avformat_mutex; |
ddebfb15 | 54 | |
3453a231 | 55 | void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size) |
8e1e6f31 | 56 | { |
115329f1 | 57 | if(min_size < *size) |
8e1e6f31 | 58 | return ptr; |
115329f1 | 59 | |
372c3f82 | 60 | min_size= FFMAX(17*min_size/16 + 32, min_size); |
8e1e6f31 | 61 | |
372c3f82 | 62 | ptr= av_realloc(ptr, min_size); |
978805b2 | 63 | 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 |
64 | min_size= 0; |
65 | ||
66 | *size= min_size; | |
978805b2 MN |
67 | |
68 | return ptr; | |
8e1e6f31 FB |
69 | } |
70 | ||
3453a231 | 71 | void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size) |
238ef6da RD |
72 | { |
73 | void **p = ptr; | |
74 | if (min_size < *size) | |
75 | return; | |
372c3f82 | 76 | min_size= FFMAX(17*min_size/16 + 32, min_size); |
238ef6da | 77 | av_free(*p); |
372c3f82 MN |
78 | *p = av_malloc(min_size); |
79 | if (!*p) min_size = 0; | |
80 | *size= min_size; | |
238ef6da RD |
81 | } |
82 | ||
316fc744 JG |
83 | void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size) |
84 | { | |
85 | void **p = ptr; | |
86 | if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) { | |
87 | av_freep(p); | |
88 | *size = 0; | |
89 | return; | |
90 | } | |
91 | av_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE); | |
92 | if (*size) | |
93 | memset((uint8_t *)*p + min_size, 0, FF_INPUT_BUFFER_PADDING_SIZE); | |
94 | } | |
95 | ||
de6d9b64 | 96 | /* encoder management */ |
e6df765e | 97 | static AVCodec *first_avcodec = NULL; |
de6d9b64 | 98 | |
0a0f19b5 AK |
99 | AVCodec *av_codec_next(const AVCodec *c) |
100 | { | |
55b9e69a MN |
101 | if(c) return c->next; |
102 | else return first_avcodec; | |
103 | } | |
104 | ||
3211932c | 105 | static void avcodec_init(void) |
9ecfbb3e AK |
106 | { |
107 | static int initialized = 0; | |
108 | ||
109 | if (initialized != 0) | |
110 | return; | |
111 | initialized = 1; | |
112 | ||
9cf0841e | 113 | ff_dsputil_static_init(); |
9ecfbb3e AK |
114 | } |
115 | ||
0a0f19b5 | 116 | int av_codec_is_encoder(const AVCodec *codec) |
b2c75b6e | 117 | { |
466b39ef | 118 | return codec && (codec->encode_sub || codec->encode2); |
b2c75b6e JR |
119 | } |
120 | ||
0a0f19b5 | 121 | int av_codec_is_decoder(const AVCodec *codec) |
b2c75b6e JR |
122 | { |
123 | return codec && codec->decode; | |
124 | } | |
125 | ||
85662f49 | 126 | void avcodec_register(AVCodec *codec) |
de6d9b64 FB |
127 | { |
128 | AVCodec **p; | |
7a961a46 | 129 | avcodec_init(); |
de6d9b64 FB |
130 | p = &first_avcodec; |
131 | while (*p != NULL) p = &(*p)->next; | |
335a761a SS |
132 | *p = codec; |
133 | codec->next = NULL; | |
d97efd7f AK |
134 | |
135 | if (codec->init_static_data) | |
136 | codec->init_static_data(codec); | |
de6d9b64 FB |
137 | } |
138 | ||
0fb49b59 BB |
139 | unsigned avcodec_get_edge_width(void) |
140 | { | |
141 | return EDGE_WIDTH; | |
142 | } | |
143 | ||
21adafec MN |
144 | void avcodec_set_dimensions(AVCodecContext *s, int width, int height){ |
145 | s->coded_width = width; | |
146 | s->coded_height= height; | |
2bcbd984 MR |
147 | s->width = width; |
148 | s->height = height; | |
21adafec MN |
149 | } |
150 | ||
6a9c8594 | 151 | #define INTERNAL_BUFFER_SIZE (32+1) |
1e491e29 | 152 | |
560f773c JR |
153 | void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height, |
154 | int linesize_align[AV_NUM_DATA_POINTERS]) | |
155 | { | |
156 | int i; | |
115329f1 DB |
157 | int w_align= 1; |
158 | int h_align= 1; | |
159 | ||
f0bbfc4a MN |
160 | switch(s->pix_fmt){ |
161 | case PIX_FMT_YUV420P: | |
71e445fc | 162 | case PIX_FMT_YUYV422: |
ebb177dd | 163 | case PIX_FMT_UYVY422: |
f0bbfc4a | 164 | case PIX_FMT_YUV422P: |
98c82d69 | 165 | case PIX_FMT_YUV440P: |
f0bbfc4a | 166 | case PIX_FMT_YUV444P: |
db431f7e | 167 | case PIX_FMT_GBRP: |
f0bbfc4a | 168 | case PIX_FMT_GRAY8: |
34380af0 KS |
169 | case PIX_FMT_GRAY16BE: |
170 | case PIX_FMT_GRAY16LE: | |
f0bbfc4a MN |
171 | case PIX_FMT_YUVJ420P: |
172 | case PIX_FMT_YUVJ422P: | |
98c82d69 | 173 | case PIX_FMT_YUVJ440P: |
f0bbfc4a | 174 | case PIX_FMT_YUVJ444P: |
b70335a2 | 175 | case PIX_FMT_YUVA420P: |
42239ced OA |
176 | case PIX_FMT_YUV420P9LE: |
177 | case PIX_FMT_YUV420P9BE: | |
178 | case PIX_FMT_YUV420P10LE: | |
179 | case PIX_FMT_YUV420P10BE: | |
dc49bf12 RB |
180 | case PIX_FMT_YUV422P9LE: |
181 | case PIX_FMT_YUV422P9BE: | |
5c511ad4 BC |
182 | case PIX_FMT_YUV422P10LE: |
183 | case PIX_FMT_YUV422P10BE: | |
da55ee6c JGG |
184 | case PIX_FMT_YUV444P9LE: |
185 | case PIX_FMT_YUV444P9BE: | |
186 | case PIX_FMT_YUV444P10LE: | |
187 | case PIX_FMT_YUV444P10BE: | |
db431f7e RB |
188 | case PIX_FMT_GBRP9LE: |
189 | case PIX_FMT_GBRP9BE: | |
190 | case PIX_FMT_GBRP10LE: | |
191 | case PIX_FMT_GBRP10BE: | |
37c0dc62 RC |
192 | w_align = 16; //FIXME assume 16 pixel per macroblock |
193 | h_align = 16 * 2; // interlaced needs 2 macroblocks height | |
f0bbfc4a MN |
194 | break; |
195 | case PIX_FMT_YUV411P: | |
71e445fc | 196 | case PIX_FMT_UYYVYY411: |
f0bbfc4a MN |
197 | w_align=32; |
198 | h_align=8; | |
199 | break; | |
200 | case PIX_FMT_YUV410P: | |
36ef5369 | 201 | if(s->codec_id == AV_CODEC_ID_SVQ1){ |
f0bbfc4a MN |
202 | w_align=64; |
203 | h_align=64; | |
204 | } | |
d99fbbf4 | 205 | case PIX_FMT_RGB555: |
36ef5369 | 206 | if(s->codec_id == AV_CODEC_ID_RPZA){ |
d99fbbf4 RT |
207 | w_align=4; |
208 | h_align=4; | |
209 | } | |
210 | case PIX_FMT_PAL8: | |
6337178b MN |
211 | case PIX_FMT_BGR8: |
212 | case PIX_FMT_RGB8: | |
36ef5369 | 213 | if(s->codec_id == AV_CODEC_ID_SMC){ |
d99fbbf4 RT |
214 | w_align=4; |
215 | h_align=4; | |
216 | } | |
f0bbfc4a | 217 | break; |
c31b8121 | 218 | case PIX_FMT_BGR24: |
36ef5369 | 219 | if((s->codec_id == AV_CODEC_ID_MSZH) || (s->codec_id == AV_CODEC_ID_ZLIB)){ |
c31b8121 RT |
220 | w_align=4; |
221 | h_align=4; | |
222 | } | |
223 | break; | |
f0bbfc4a MN |
224 | default: |
225 | w_align= 1; | |
226 | h_align= 1; | |
227 | break; | |
228 | } | |
229 | ||
ef516f73 DC |
230 | *width = FFALIGN(*width , w_align); |
231 | *height= FFALIGN(*height, h_align); | |
36ef5369 | 232 | if (s->codec_id == AV_CODEC_ID_H264) |
ae4ffe9f | 233 | *height+=2; // some of the optimized chroma MC reads one line too much |
eb285cfe | 234 | |
2d9535ad | 235 | for (i = 0; i < 4; i++) |
560f773c | 236 | linesize_align[i] = STRIDE_ALIGN; |
eb285cfe RD |
237 | } |
238 | ||
239 | void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height){ | |
240 | int chroma_shift = av_pix_fmt_descriptors[s->pix_fmt].log2_chroma_w; | |
560f773c | 241 | int linesize_align[AV_NUM_DATA_POINTERS]; |
eb285cfe RD |
242 | int align; |
243 | avcodec_align_dimensions2(s, width, height, linesize_align); | |
244 | align = FFMAX(linesize_align[0], linesize_align[3]); | |
245 | linesize_align[1] <<= chroma_shift; | |
246 | linesize_align[2] <<= chroma_shift; | |
247 | align = FFMAX3(align, linesize_align[1], linesize_align[2]); | |
248 | *width=FFALIGN(*width, align); | |
f0bbfc4a MN |
249 | } |
250 | ||
5ee5fa02 JR |
251 | int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels, |
252 | enum AVSampleFormat sample_fmt, const uint8_t *buf, | |
253 | int buf_size, int align) | |
254 | { | |
255 | int ch, planar, needed_size, ret = 0; | |
256 | ||
257 | needed_size = av_samples_get_buffer_size(NULL, nb_channels, | |
258 | frame->nb_samples, sample_fmt, | |
259 | align); | |
260 | if (buf_size < needed_size) | |
261 | return AVERROR(EINVAL); | |
262 | ||
263 | planar = av_sample_fmt_is_planar(sample_fmt); | |
264 | if (planar && nb_channels > AV_NUM_DATA_POINTERS) { | |
265 | if (!(frame->extended_data = av_mallocz(nb_channels * | |
266 | sizeof(*frame->extended_data)))) | |
267 | return AVERROR(ENOMEM); | |
268 | } else { | |
269 | frame->extended_data = frame->data; | |
270 | } | |
271 | ||
272 | if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0], | |
273 | buf, nb_channels, frame->nb_samples, | |
274 | sample_fmt, align)) < 0) { | |
275 | if (frame->extended_data != frame->data) | |
276 | av_free(frame->extended_data); | |
277 | return ret; | |
278 | } | |
279 | if (frame->extended_data != frame->data) { | |
280 | for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++) | |
281 | frame->data[ch] = frame->extended_data[ch]; | |
282 | } | |
283 | ||
284 | return ret; | |
285 | } | |
286 | ||
0eea2129 JR |
287 | static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame) |
288 | { | |
289 | AVCodecInternal *avci = avctx->internal; | |
290 | InternalBuffer *buf; | |
5ee5fa02 | 291 | int buf_size, ret; |
0eea2129 JR |
292 | |
293 | buf_size = av_samples_get_buffer_size(NULL, avctx->channels, | |
294 | frame->nb_samples, avctx->sample_fmt, | |
c58846f3 | 295 | 0); |
0eea2129 JR |
296 | if (buf_size < 0) |
297 | return AVERROR(EINVAL); | |
298 | ||
0eea2129 JR |
299 | /* allocate InternalBuffer if needed */ |
300 | if (!avci->buffer) { | |
301 | avci->buffer = av_mallocz(sizeof(InternalBuffer)); | |
302 | if (!avci->buffer) | |
303 | return AVERROR(ENOMEM); | |
304 | } | |
305 | buf = avci->buffer; | |
306 | ||
307 | /* if there is a previously-used internal buffer, check its size and | |
308 | channel count to see if we can reuse it */ | |
309 | if (buf->extended_data) { | |
310 | /* if current buffer is too small, free it */ | |
311 | if (buf->extended_data[0] && buf_size > buf->audio_data_size) { | |
312 | av_free(buf->extended_data[0]); | |
313 | if (buf->extended_data != buf->data) | |
314 | av_free(&buf->extended_data); | |
315 | buf->extended_data = NULL; | |
316 | buf->data[0] = NULL; | |
317 | } | |
318 | /* if number of channels has changed, reset and/or free extended data | |
319 | pointers but leave data buffer in buf->data[0] for reuse */ | |
320 | if (buf->nb_channels != avctx->channels) { | |
321 | if (buf->extended_data != buf->data) | |
322 | av_free(buf->extended_data); | |
323 | buf->extended_data = NULL; | |
324 | } | |
325 | } | |
326 | ||
327 | /* if there is no previous buffer or the previous buffer cannot be used | |
328 | as-is, allocate a new buffer and/or rearrange the channel pointers */ | |
329 | if (!buf->extended_data) { | |
5ee5fa02 JR |
330 | if (!buf->data[0]) { |
331 | if (!(buf->data[0] = av_mallocz(buf_size))) | |
0eea2129 | 332 | return AVERROR(ENOMEM); |
5ee5fa02 | 333 | buf->audio_data_size = buf_size; |
0eea2129 | 334 | } |
5ee5fa02 JR |
335 | if ((ret = avcodec_fill_audio_frame(frame, avctx->channels, |
336 | avctx->sample_fmt, buf->data[0], | |
c58846f3 | 337 | buf->audio_data_size, 0))) |
0eea2129 JR |
338 | return ret; |
339 | ||
5ee5fa02 JR |
340 | if (frame->extended_data == frame->data) |
341 | buf->extended_data = buf->data; | |
342 | else | |
343 | buf->extended_data = frame->extended_data; | |
344 | memcpy(buf->data, frame->data, sizeof(frame->data)); | |
345 | buf->linesize[0] = frame->linesize[0]; | |
346 | buf->nb_channels = avctx->channels; | |
347 | } else { | |
348 | /* copy InternalBuffer info to the AVFrame */ | |
349 | frame->extended_data = buf->extended_data; | |
350 | frame->linesize[0] = buf->linesize[0]; | |
351 | memcpy(frame->data, buf->data, sizeof(frame->data)); | |
0eea2129 JR |
352 | } |
353 | ||
0eea2129 | 354 | frame->type = FF_BUFFER_TYPE_INTERNAL; |
0eea2129 JR |
355 | |
356 | if (avctx->pkt) frame->pkt_pts = avctx->pkt->pts; | |
357 | else frame->pkt_pts = AV_NOPTS_VALUE; | |
358 | frame->reordered_opaque = avctx->reordered_opaque; | |
359 | ||
828bd088 AK |
360 | frame->sample_rate = avctx->sample_rate; |
361 | frame->format = avctx->sample_fmt; | |
362 | frame->channel_layout = avctx->channel_layout; | |
363 | ||
0eea2129 JR |
364 | if (avctx->debug & FF_DEBUG_BUFFERS) |
365 | av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p, " | |
366 | "internal audio buffer used\n", frame); | |
367 | ||
368 | return 0; | |
369 | } | |
370 | ||
371 | static int video_get_buffer(AVCodecContext *s, AVFrame *pic) | |
372 | { | |
1e491e29 | 373 | int i; |
f0bbfc4a MN |
374 | int w= s->width; |
375 | int h= s->height; | |
d90cf87b | 376 | InternalBuffer *buf; |
f3a29b75 | 377 | AVCodecInternal *avci = s->internal; |
0ecca7a4 | 378 | |
65d999d6 MB |
379 | if(pic->data[0]!=NULL) { |
380 | av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n"); | |
381 | return -1; | |
382 | } | |
f3a29b75 JR |
383 | if(avci->buffer_count >= INTERNAL_BUFFER_SIZE) { |
384 | av_log(s, AV_LOG_ERROR, "buffer_count overflow (missing release_buffer?)\n"); | |
65d999d6 MB |
385 | return -1; |
386 | } | |
1e491e29 | 387 | |
e16f217c | 388 | if(av_image_check_size(w, h, 0, s)) |
0ecca7a4 MN |
389 | return -1; |
390 | ||
f3a29b75 JR |
391 | if (!avci->buffer) { |
392 | avci->buffer = av_mallocz((INTERNAL_BUFFER_SIZE+1) * | |
393 | sizeof(InternalBuffer)); | |
d90cf87b | 394 | } |
115329f1 | 395 | |
f3a29b75 | 396 | buf = &avci->buffer[avci->buffer_count]; |
115329f1 | 397 | |
0701006e | 398 | if(buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)){ |
560f773c | 399 | for (i = 0; i < AV_NUM_DATA_POINTERS; i++) { |
0701006e MN |
400 | av_freep(&buf->base[i]); |
401 | buf->data[i]= NULL; | |
402 | } | |
403 | } | |
404 | ||
8400b126 | 405 | if (!buf->base[0]) { |
f0bbfc4a | 406 | int h_chroma_shift, v_chroma_shift; |
db7ae7d1 VS |
407 | int size[4] = {0}; |
408 | int tmpsize; | |
c9d6e847 | 409 | int unaligned; |
c7622f9a | 410 | AVPicture picture; |
560f773c | 411 | int stride_align[AV_NUM_DATA_POINTERS]; |
6e3ef511 | 412 | const int pixel_size = av_pix_fmt_descriptors[s->pix_fmt].comp[0].step_minus1+1; |
c7622f9a | 413 | |
1e491e29 | 414 | avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift); |
f0bbfc4a | 415 | |
eb285cfe | 416 | avcodec_align_dimensions2(s, &w, &h, stride_align); |
115329f1 | 417 | |
1e491e29 MN |
418 | if(!(s->flags&CODEC_FLAG_EMU_EDGE)){ |
419 | w+= EDGE_WIDTH*2; | |
420 | h+= EDGE_WIDTH*2; | |
421 | } | |
5b67307a | 422 | |
c9d6e847 RD |
423 | do { |
424 | // NOTE: do not align linesizes individually, this breaks e.g. assumptions | |
425 | // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2 | |
9d2e0ad8 | 426 | av_image_fill_linesizes(picture.linesize, s->pix_fmt, w); |
c9d6e847 RD |
427 | // increase alignment of w for next try (rhs gives the lowest bit set in w) |
428 | w += w & ~(w-1); | |
db7ae7d1 | 429 | |
c9d6e847 | 430 | unaligned = 0; |
45bae968 | 431 | for (i=0; i<4; i++){ |
c9d6e847 | 432 | unaligned |= picture.linesize[i] % stride_align[i]; |
45bae968 | 433 | } |
c9d6e847 | 434 | } while (unaligned); |
db7ae7d1 | 435 | |
9d2e0ad8 | 436 | tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize); |
f8c96d01 RD |
437 | if (tmpsize < 0) |
438 | return -1; | |
db7ae7d1 VS |
439 | |
440 | for (i=0; i<3 && picture.data[i+1]; i++) | |
441 | size[i] = picture.data[i+1] - picture.data[i]; | |
cf73e32a | 442 | size[i] = tmpsize - (picture.data[i] - picture.data[0]); |
c7622f9a | 443 | |
c7622f9a MN |
444 | memset(buf->base, 0, sizeof(buf->base)); |
445 | memset(buf->data, 0, sizeof(buf->data)); | |
1e491e29 | 446 | |
b70335a2 | 447 | for(i=0; i<4 && size[i]; i++){ |
2c19981a MN |
448 | const int h_shift= i==0 ? 0 : h_chroma_shift; |
449 | const int v_shift= i==0 ? 0 : v_chroma_shift; | |
1e491e29 | 450 | |
c7622f9a | 451 | buf->linesize[i]= picture.linesize[i]; |
1e491e29 | 452 | |
c7622f9a | 453 | buf->base[i]= av_malloc(size[i]+16); //FIXME 16 |
d90cf87b | 454 | if(buf->base[i]==NULL) return -1; |
c7622f9a MN |
455 | memset(buf->base[i], 128, size[i]); |
456 | ||
3491a9b2 | 457 | // no edge if EDGE EMU or not planar YUV |
6337178b | 458 | if((s->flags&CODEC_FLAG_EMU_EDGE) || !size[2]) |
d90cf87b | 459 | buf->data[i] = buf->base[i]; |
1e491e29 | 460 | else |
6e3ef511 | 461 | buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (pixel_size*EDGE_WIDTH>>h_shift), stride_align[i]); |
1e491e29 | 462 | } |
560f773c JR |
463 | for (; i < AV_NUM_DATA_POINTERS; i++) { |
464 | buf->base[i] = buf->data[i] = NULL; | |
465 | buf->linesize[i] = 0; | |
466 | } | |
6337178b | 467 | if(size[1] && !size[2]) |
ed5d30d9 | 468 | ff_set_systematic_pal2((uint32_t*)buf->data[1], s->pix_fmt); |
0701006e MN |
469 | buf->width = s->width; |
470 | buf->height = s->height; | |
471 | buf->pix_fmt= s->pix_fmt; | |
1e491e29 | 472 | } |
237e4938 | 473 | pic->type= FF_BUFFER_TYPE_INTERNAL; |
1e491e29 | 474 | |
560f773c | 475 | for (i = 0; i < AV_NUM_DATA_POINTERS; i++) { |
d90cf87b MN |
476 | pic->base[i]= buf->base[i]; |
477 | pic->data[i]= buf->data[i]; | |
237e4938 | 478 | pic->linesize[i]= buf->linesize[i]; |
d90cf87b | 479 | } |
0eea2129 | 480 | pic->extended_data = pic->data; |
f3a29b75 | 481 | avci->buffer_count++; |
bc1ef855 RB |
482 | pic->width = buf->width; |
483 | pic->height = buf->height; | |
484 | pic->format = buf->pix_fmt; | |
485 | pic->sample_aspect_ratio = s->sample_aspect_ratio; | |
d90cf87b | 486 | |
393cbb96 MN |
487 | if(s->pkt) pic->pkt_pts= s->pkt->pts; |
488 | else pic->pkt_pts= AV_NOPTS_VALUE; | |
79de84f2 MN |
489 | pic->reordered_opaque= s->reordered_opaque; |
490 | ||
385c820b | 491 | if(s->debug&FF_DEBUG_BUFFERS) |
f3a29b75 JR |
492 | av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d " |
493 | "buffers used\n", pic, avci->buffer_count); | |
385c820b | 494 | |
1e491e29 MN |
495 | return 0; |
496 | } | |
497 | ||
0eea2129 JR |
498 | int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame) |
499 | { | |
500 | switch (avctx->codec_type) { | |
501 | case AVMEDIA_TYPE_VIDEO: | |
502 | return video_get_buffer(avctx, frame); | |
503 | case AVMEDIA_TYPE_AUDIO: | |
504 | return audio_get_buffer(avctx, frame); | |
505 | default: | |
506 | return -1; | |
507 | } | |
508 | } | |
509 | ||
492cd3a9 | 510 | void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){ |
1e491e29 | 511 | int i; |
6829ac8d | 512 | InternalBuffer *buf, *last; |
f3a29b75 | 513 | AVCodecInternal *avci = s->internal; |
d90cf87b | 514 | |
0eea2129 JR |
515 | assert(s->codec_type == AVMEDIA_TYPE_VIDEO); |
516 | ||
4e00e76b | 517 | assert(pic->type==FF_BUFFER_TYPE_INTERNAL); |
f3a29b75 JR |
518 | assert(avci->buffer_count); |
519 | ||
520 | if (avci->buffer) { | |
521 | buf = NULL; /* avoids warning */ | |
522 | for (i = 0; i < avci->buffer_count; i++) { //just 3-5 checks so is not worth to optimize | |
523 | buf = &avci->buffer[i]; | |
524 | if (buf->data[0] == pic->data[0]) | |
525 | break; | |
526 | } | |
527 | assert(i < avci->buffer_count); | |
528 | avci->buffer_count--; | |
529 | last = &avci->buffer[avci->buffer_count]; | |
d90cf87b | 530 | |
a09bb3ba MR |
531 | if (buf != last) |
532 | FFSWAP(InternalBuffer, *buf, *last); | |
6a9c8594 | 533 | } |
d90cf87b | 534 | |
560f773c | 535 | for (i = 0; i < AV_NUM_DATA_POINTERS; i++) { |
1e491e29 | 536 | pic->data[i]=NULL; |
d90cf87b MN |
537 | // pic->base[i]=NULL; |
538 | } | |
1e491e29 | 539 | //printf("R%X\n", pic->opaque); |
385c820b AS |
540 | |
541 | if(s->debug&FF_DEBUG_BUFFERS) | |
f3a29b75 JR |
542 | av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d " |
543 | "buffers used\n", pic, avci->buffer_count); | |
1e491e29 MN |
544 | } |
545 | ||
e1c2a5a0 RT |
546 | int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){ |
547 | AVFrame temp_pic; | |
548 | int i; | |
549 | ||
0eea2129 JR |
550 | assert(s->codec_type == AVMEDIA_TYPE_VIDEO); |
551 | ||
e1c2a5a0 RT |
552 | /* If no picture return a new buffer */ |
553 | if(pic->data[0] == NULL) { | |
554 | /* We will copy from buffer, so must be readable */ | |
555 | pic->buffer_hints |= FF_BUFFER_HINTS_READABLE; | |
556 | return s->get_buffer(s, pic); | |
557 | } | |
558 | ||
d10319d8 | 559 | assert(s->pix_fmt == pic->format); |
87840eeb | 560 | |
e1c2a5a0 | 561 | /* If internal buffer type return the same buffer */ |
b8af4fe9 | 562 | if(pic->type == FF_BUFFER_TYPE_INTERNAL) { |
62ecd363 NG |
563 | if(s->pkt) pic->pkt_pts= s->pkt->pts; |
564 | else pic->pkt_pts= AV_NOPTS_VALUE; | |
b8af4fe9 | 565 | pic->reordered_opaque= s->reordered_opaque; |
e1c2a5a0 | 566 | return 0; |
b8af4fe9 | 567 | } |
e1c2a5a0 RT |
568 | |
569 | /* | |
570 | * Not internal type and reget_buffer not overridden, emulate cr buffer | |
571 | */ | |
572 | temp_pic = *pic; | |
560f773c | 573 | for(i = 0; i < AV_NUM_DATA_POINTERS; i++) |
e1c2a5a0 RT |
574 | pic->data[i] = pic->base[i] = NULL; |
575 | pic->opaque = NULL; | |
576 | /* Allocate new frame */ | |
577 | if (s->get_buffer(s, pic)) | |
578 | return -1; | |
579 | /* Copy image data from old buffer to new buffer */ | |
636d6a4a | 580 | av_picture_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width, |
e1c2a5a0 RT |
581 | s->height); |
582 | s->release_buffer(s, &temp_pic); // Release old frame | |
583 | return 0; | |
584 | } | |
585 | ||
3a84713a | 586 | int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){ |
9c3d33d6 MN |
587 | int i; |
588 | ||
589 | for(i=0; i<count; i++){ | |
3a84713a | 590 | int r= func(c, (char*)arg + i*size); |
9c3d33d6 MN |
591 | if(ret) ret[i]= r; |
592 | } | |
593 | return 0; | |
594 | } | |
595 | ||
8d23a86f RD |
596 | int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr),void *arg, int *ret, int count){ |
597 | int i; | |
598 | ||
599 | for(i=0; i<count; i++){ | |
600 | int r= func(c, arg, i, 0); | |
601 | if(ret) ret[i]= r; | |
602 | } | |
603 | return 0; | |
604 | } | |
605 | ||
c269cf68 MN |
606 | enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat *fmt){ |
607 | while (*fmt != PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt)) | |
608 | ++fmt; | |
a33c7159 MN |
609 | return fmt[0]; |
610 | } | |
611 | ||
9740beff MN |
612 | void avcodec_get_frame_defaults(AVFrame *pic){ |
613 | memset(pic, 0, sizeof(AVFrame)); | |
614 | ||
615 | pic->pts= AV_NOPTS_VALUE; | |
c342499d | 616 | pic->key_frame= 1; |
b58dbb5b | 617 | pic->sample_aspect_ratio = (AVRational){0, 1}; |
8a4a5f6f | 618 | pic->format = -1; /* unknown */ |
9740beff MN |
619 | } |
620 | ||
492cd3a9 | 621 | AVFrame *avcodec_alloc_frame(void){ |
9740beff | 622 | AVFrame *pic= av_malloc(sizeof(AVFrame)); |
115329f1 | 623 | |
9740beff | 624 | if(pic==NULL) return NULL; |
115329f1 | 625 | |
9740beff | 626 | avcodec_get_frame_defaults(pic); |
115329f1 | 627 | |
1e491e29 MN |
628 | return pic; |
629 | } | |
630 | ||
0a0f19b5 | 631 | int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options) |
0b950fe2 | 632 | { |
31d76ec2 | 633 | int ret = 0; |
0b950fe2 AK |
634 | AVDictionary *tmp = NULL; |
635 | ||
af08d9ae AK |
636 | if (avcodec_is_open(avctx)) |
637 | return 0; | |
638 | ||
bc901998 AK |
639 | if ((!codec && !avctx->codec)) { |
640 | av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2().\n"); | |
641 | return AVERROR(EINVAL); | |
642 | } | |
643 | if ((codec && avctx->codec && codec != avctx->codec)) { | |
644 | av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, " | |
645 | "but %s passed to avcodec_open2().\n", avctx->codec->name, codec->name); | |
646 | return AVERROR(EINVAL); | |
647 | } | |
648 | if (!codec) | |
649 | codec = avctx->codec; | |
650 | ||
4df30f71 AC |
651 | if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE) |
652 | return AVERROR(EINVAL); | |
653 | ||
0b950fe2 AK |
654 | if (options) |
655 | av_dict_copy(&tmp, *options, 0); | |
115329f1 | 656 | |
f988ce6c AÖ |
657 | /* If there is a user-supplied mutex locking routine, call it. */ |
658 | if (ff_lockmgr_cb) { | |
659 | if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN)) | |
660 | return -1; | |
661 | } | |
662 | ||
ddebfb15 MN |
663 | entangled_thread_counter++; |
664 | if(entangled_thread_counter != 1){ | |
665 | av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n"); | |
31d76ec2 | 666 | ret = -1; |
ddebfb15 MN |
667 | goto end; |
668 | } | |
de6d9b64 | 669 | |
f3a29b75 JR |
670 | avctx->internal = av_mallocz(sizeof(AVCodecInternal)); |
671 | if (!avctx->internal) { | |
672 | ret = AVERROR(ENOMEM); | |
673 | goto end; | |
674 | } | |
675 | ||
0edf8a7a | 676 | if (codec->priv_data_size > 0) { |
dc51a72b | 677 | if(!avctx->priv_data){ |
0edf8a7a | 678 | avctx->priv_data = av_mallocz(codec->priv_data_size); |
90f06cea PI |
679 | if (!avctx->priv_data) { |
680 | ret = AVERROR(ENOMEM); | |
ddebfb15 | 681 | goto end; |
90f06cea | 682 | } |
0b950fe2 | 683 | if (codec->priv_class) { |
9f64c821 | 684 | *(const AVClass**)avctx->priv_data = codec->priv_class; |
dc51a72b MN |
685 | av_opt_set_defaults(avctx->priv_data); |
686 | } | |
687 | } | |
1d36fb13 | 688 | if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0) |
0b950fe2 | 689 | goto free_and_end; |
0edf8a7a PG |
690 | } else { |
691 | avctx->priv_data = NULL; | |
692 | } | |
0b950fe2 AK |
693 | if ((ret = av_opt_set_dict(avctx, &tmp)) < 0) |
694 | goto free_and_end; | |
21adafec MN |
695 | |
696 | if(avctx->coded_width && avctx->coded_height) | |
697 | avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height); | |
698 | else if(avctx->width && avctx->height) | |
699 | avcodec_set_dimensions(avctx, avctx->width, avctx->height); | |
700 | ||
82eac2f3 RD |
701 | if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height) |
702 | && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0 | |
703 | || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) { | |
704 | av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n"); | |
705 | avcodec_set_dimensions(avctx, 0, 0); | |
706 | } | |
707 | ||
f19c58b4 AJ |
708 | /* if the decoder init function was already called previously, |
709 | free the already allocated subtitle_header before overwriting it */ | |
44fe77b3 | 710 | if (av_codec_is_decoder(codec)) |
f19c58b4 AJ |
711 | av_freep(&avctx->subtitle_header); |
712 | ||
fa77dd63 | 713 | #define SANE_NB_CHANNELS 128U |
82eac2f3 | 714 | if (avctx->channels > SANE_NB_CHANNELS) { |
7868349a | 715 | ret = AVERROR(EINVAL); |
2a9b5c9b | 716 | goto free_and_end; |
0ecca7a4 MN |
717 | } |
718 | ||
b5c85991 | 719 | avctx->codec = codec; |
72415b2a | 720 | if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) && |
36ef5369 | 721 | avctx->codec_id == AV_CODEC_ID_NONE) { |
681c180d RD |
722 | avctx->codec_type = codec->type; |
723 | avctx->codec_id = codec->id; | |
724 | } | |
e83c716e AJ |
725 | if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type |
726 | && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) { | |
4c0dda2b | 727 | av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n"); |
31d76ec2 | 728 | ret = AVERROR(EINVAL); |
2a9b5c9b | 729 | goto free_and_end; |
4c0dda2b | 730 | } |
b5c85991 | 731 | avctx->frame_number = 0; |
37b00b47 | 732 | |
9a7dc618 MS |
733 | if (avctx->codec_type == AVMEDIA_TYPE_AUDIO && |
734 | (!avctx->time_base.num || !avctx->time_base.den)) { | |
735 | avctx->time_base.num = 1; | |
736 | avctx->time_base.den = avctx->sample_rate; | |
737 | } | |
738 | ||
37b00b47 | 739 | if (HAVE_THREADS && !avctx->thread_opaque) { |
ba9ef8d0 | 740 | ret = ff_thread_init(avctx); |
37b00b47 AS |
741 | if (ret < 0) { |
742 | goto free_and_end; | |
743 | } | |
744 | } | |
b6064d9a JG |
745 | if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS)) |
746 | avctx->thread_count = 1; | |
37b00b47 | 747 | |
44fe77b3 | 748 | if (av_codec_is_encoder(avctx->codec)) { |
2cfa2d92 | 749 | int i; |
8b00ab01 | 750 | if (avctx->codec->sample_fmts) { |
3dfc3e70 JR |
751 | for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) |
752 | if (avctx->sample_fmt == avctx->codec->sample_fmts[i]) | |
753 | break; | |
754 | if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) { | |
755 | av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n"); | |
756 | ret = AVERROR(EINVAL); | |
757 | goto free_and_end; | |
758 | } | |
8b00ab01 | 759 | } |
dcd2b55e PM |
760 | if (avctx->codec->pix_fmts) { |
761 | for (i = 0; avctx->codec->pix_fmts[i] != PIX_FMT_NONE; i++) | |
762 | if (avctx->pix_fmt == avctx->codec->pix_fmts[i]) | |
763 | break; | |
764 | if (avctx->codec->pix_fmts[i] == PIX_FMT_NONE) { | |
765 | av_log(avctx, AV_LOG_ERROR, "Specified pix_fmt is not supported\n"); | |
766 | ret = AVERROR(EINVAL); | |
767 | goto free_and_end; | |
768 | } | |
769 | } | |
8b00ab01 JR |
770 | if (avctx->codec->supported_samplerates) { |
771 | for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++) | |
772 | if (avctx->sample_rate == avctx->codec->supported_samplerates[i]) | |
773 | break; | |
774 | if (avctx->codec->supported_samplerates[i] == 0) { | |
775 | av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n"); | |
776 | ret = AVERROR(EINVAL); | |
777 | goto free_and_end; | |
778 | } | |
779 | } | |
780 | if (avctx->codec->channel_layouts) { | |
781 | if (!avctx->channel_layout) { | |
782 | av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n"); | |
783 | } else { | |
784 | for (i = 0; avctx->codec->channel_layouts[i] != 0; i++) | |
785 | if (avctx->channel_layout == avctx->codec->channel_layouts[i]) | |
786 | break; | |
787 | if (avctx->codec->channel_layouts[i] == 0) { | |
788 | av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n"); | |
789 | ret = AVERROR(EINVAL); | |
790 | goto free_and_end; | |
791 | } | |
792 | } | |
793 | } | |
168f9e8c JR |
794 | if (avctx->channel_layout && avctx->channels) { |
795 | if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) { | |
796 | av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n"); | |
797 | ret = AVERROR(EINVAL); | |
798 | goto free_and_end; | |
799 | } | |
688b09fa JR |
800 | } else if (avctx->channel_layout) { |
801 | avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout); | |
168f9e8c | 802 | } |
2cfa2d92 | 803 | } |
0fd0ef79 | 804 | |
37b00b47 | 805 | if(avctx->codec->init && !(avctx->active_thread_type&FF_THREAD_FRAME)){ |
2de4f9eb MN |
806 | ret = avctx->codec->init(avctx); |
807 | if (ret < 0) { | |
2a9b5c9b | 808 | goto free_and_end; |
2de4f9eb | 809 | } |
6e546aaa | 810 | } |
1337de0c JR |
811 | |
812 | if (av_codec_is_decoder(avctx->codec)) { | |
813 | /* validate channel layout from the decoder */ | |
814 | if (avctx->channel_layout && | |
815 | av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) { | |
816 | av_log(avctx, AV_LOG_WARNING, "channel layout does not match number of channels\n"); | |
817 | avctx->channel_layout = 0; | |
818 | } | |
819 | } | |
ddebfb15 MN |
820 | end: |
821 | entangled_thread_counter--; | |
f988ce6c AÖ |
822 | |
823 | /* Release any user-supplied mutex. */ | |
824 | if (ff_lockmgr_cb) { | |
825 | (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE); | |
826 | } | |
0b950fe2 AK |
827 | if (options) { |
828 | av_dict_free(options); | |
829 | *options = tmp; | |
830 | } | |
831 | ||
ddebfb15 | 832 | return ret; |
2a9b5c9b | 833 | free_and_end: |
0b950fe2 | 834 | av_dict_free(&tmp); |
2a9b5c9b | 835 | av_freep(&avctx->priv_data); |
f3a29b75 | 836 | av_freep(&avctx->internal); |
2a9b5c9b MN |
837 | avctx->codec= NULL; |
838 | goto end; | |
de6d9b64 FB |
839 | } |
840 | ||
b2c75b6e | 841 | int ff_alloc_packet(AVPacket *avpkt, int size) |
de6d9b64 | 842 | { |
b2c75b6e JR |
843 | if (size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE) |
844 | return AVERROR(EINVAL); | |
845 | ||
846 | if (avpkt->data) { | |
e42e9b0e | 847 | void *destruct = avpkt->destruct; |
b2c75b6e JR |
848 | |
849 | if (avpkt->size < size) | |
850 | return AVERROR(EINVAL); | |
851 | ||
b2c75b6e | 852 | av_init_packet(avpkt); |
e42e9b0e | 853 | avpkt->destruct = destruct; |
1a670973 | 854 | avpkt->size = size; |
b2c75b6e JR |
855 | return 0; |
856 | } else { | |
857 | return av_new_packet(avpkt, size); | |
0ecca7a4 | 858 | } |
b2c75b6e JR |
859 | } |
860 | ||
a5117a24 AK |
861 | /** |
862 | * Pad last frame with silence. | |
863 | */ | |
864 | static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src) | |
865 | { | |
866 | AVFrame *frame = NULL; | |
867 | uint8_t *buf = NULL; | |
868 | int ret; | |
869 | ||
870 | if (!(frame = avcodec_alloc_frame())) | |
871 | return AVERROR(ENOMEM); | |
872 | *frame = *src; | |
873 | ||
874 | if ((ret = av_samples_get_buffer_size(&frame->linesize[0], s->channels, | |
875 | s->frame_size, s->sample_fmt, 0)) < 0) | |
876 | goto fail; | |
877 | ||
878 | if (!(buf = av_malloc(ret))) { | |
879 | ret = AVERROR(ENOMEM); | |
880 | goto fail; | |
881 | } | |
882 | ||
883 | frame->nb_samples = s->frame_size; | |
884 | if ((ret = avcodec_fill_audio_frame(frame, s->channels, s->sample_fmt, | |
885 | buf, ret, 0)) < 0) | |
886 | goto fail; | |
887 | if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0, | |
888 | src->nb_samples, s->channels, s->sample_fmt)) < 0) | |
889 | goto fail; | |
890 | if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples, | |
891 | frame->nb_samples - src->nb_samples, | |
892 | s->channels, s->sample_fmt)) < 0) | |
893 | goto fail; | |
894 | ||
895 | *dst = frame; | |
896 | ||
897 | return 0; | |
898 | ||
899 | fail: | |
900 | if (frame->extended_data != frame->data) | |
901 | av_freep(&frame->extended_data); | |
902 | av_freep(&buf); | |
903 | av_freep(&frame); | |
904 | return ret; | |
905 | } | |
906 | ||
b2c75b6e JR |
907 | int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx, |
908 | AVPacket *avpkt, | |
909 | const AVFrame *frame, | |
910 | int *got_packet_ptr) | |
911 | { | |
c22953b8 | 912 | AVFrame tmp; |
a5117a24 | 913 | AVFrame *padded_frame = NULL; |
b2c75b6e JR |
914 | int ret; |
915 | int user_packet = !!avpkt->data; | |
b2c75b6e | 916 | |
52953d61 AK |
917 | *got_packet_ptr = 0; |
918 | ||
b2c75b6e | 919 | if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) { |
7fb6c922 | 920 | av_free_packet(avpkt); |
b2c75b6e | 921 | av_init_packet(avpkt); |
6f824977 | 922 | return 0; |
b2c75b6e JR |
923 | } |
924 | ||
c22953b8 AK |
925 | /* ensure that extended_data is properly set */ |
926 | if (frame && !frame->extended_data) { | |
927 | if (av_sample_fmt_is_planar(avctx->sample_fmt) && | |
928 | avctx->channels > AV_NUM_DATA_POINTERS) { | |
929 | av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, " | |
930 | "with more than %d channels, but extended_data is not set.\n", | |
931 | AV_NUM_DATA_POINTERS); | |
932 | return AVERROR(EINVAL); | |
933 | } | |
934 | av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n"); | |
935 | ||
936 | tmp = *frame; | |
937 | tmp.extended_data = tmp.data; | |
938 | frame = &tmp; | |
939 | } | |
940 | ||
b2c75b6e JR |
941 | /* check for valid frame size */ |
942 | if (frame) { | |
b2c75b6e | 943 | if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) { |
b461cd4d | 944 | if (frame->nb_samples > avctx->frame_size) |
b2c75b6e JR |
945 | return AVERROR(EINVAL); |
946 | } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) { | |
a5117a24 AK |
947 | if (frame->nb_samples < avctx->frame_size && |
948 | !avctx->internal->last_audio_frame) { | |
949 | ret = pad_last_frame(avctx, &padded_frame, frame); | |
950 | if (ret < 0) | |
951 | return ret; | |
952 | ||
953 | frame = padded_frame; | |
954 | avctx->internal->last_audio_frame = 1; | |
955 | } | |
956 | ||
b461cd4d | 957 | if (frame->nb_samples != avctx->frame_size) |
b2c75b6e JR |
958 | return AVERROR(EINVAL); |
959 | } | |
b2c75b6e JR |
960 | } |
961 | ||
fa0319b4 JR |
962 | ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr); |
963 | if (!ret) { | |
964 | if (*got_packet_ptr) { | |
a75bc764 | 965 | if (!(avctx->codec->capabilities & CODEC_CAP_DELAY)) { |
70749c48 JR |
966 | if (avpkt->pts == AV_NOPTS_VALUE) |
967 | avpkt->pts = frame->pts; | |
968 | if (!avpkt->duration) | |
969 | avpkt->duration = ff_samples_to_time_base(avctx, | |
970 | frame->nb_samples); | |
a75bc764 JR |
971 | } |
972 | avpkt->dts = avpkt->pts; | |
b758cf73 JR |
973 | } else { |
974 | avpkt->size = 0; | |
b2c75b6e | 975 | } |
b2c75b6e | 976 | |
3c6607eb | 977 | if (!user_packet && avpkt->size) { |
a1977e01 JR |
978 | uint8_t *new_data = av_realloc(avpkt->data, avpkt->size); |
979 | if (new_data) | |
980 | avpkt->data = new_data; | |
981 | } | |
982 | ||
b2c75b6e | 983 | avctx->frame_number++; |
a1977e01 | 984 | } |
b2c75b6e | 985 | |
74e10b62 | 986 | if (ret < 0 || !*got_packet_ptr) { |
7fb6c922 | 987 | av_free_packet(avpkt); |
74e10b62 JR |
988 | av_init_packet(avpkt); |
989 | return ret; | |
990 | } | |
7fb6c922 | 991 | |
b2c75b6e JR |
992 | /* NOTE: if we add any audio encoders which output non-keyframe packets, |
993 | this needs to be moved to the encoders, but for now we can do it | |
994 | here to simplify things */ | |
995 | avpkt->flags |= AV_PKT_FLAG_KEY; | |
996 | ||
a5117a24 AK |
997 | if (padded_frame) { |
998 | av_freep(&padded_frame->data[0]); | |
999 | if (padded_frame->extended_data != padded_frame->data) | |
1000 | av_freep(&padded_frame->extended_data); | |
1001 | av_freep(&padded_frame); | |
1002 | } | |
1003 | ||
b2c75b6e | 1004 | return ret; |
de6d9b64 FB |
1005 | } |
1006 | ||
b2c75b6e JR |
1007 | #if FF_API_OLD_DECODE_AUDIO |
1008 | int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx, | |
1009 | uint8_t *buf, int buf_size, | |
1010 | const short *samples) | |
1011 | { | |
1012 | AVPacket pkt; | |
1013 | AVFrame frame0; | |
1014 | AVFrame *frame; | |
1015 | int ret, samples_size, got_packet; | |
1016 | ||
1017 | av_init_packet(&pkt); | |
1018 | pkt.data = buf; | |
1019 | pkt.size = buf_size; | |
1020 | ||
1021 | if (samples) { | |
1022 | frame = &frame0; | |
1023 | avcodec_get_frame_defaults(frame); | |
1024 | ||
1025 | if (avctx->frame_size) { | |
1026 | frame->nb_samples = avctx->frame_size; | |
1027 | } else { | |
1028 | /* if frame_size is not set, the number of samples must be | |
1029 | calculated from the buffer size */ | |
1030 | int64_t nb_samples; | |
1031 | if (!av_get_bits_per_sample(avctx->codec_id)) { | |
1032 | av_log(avctx, AV_LOG_ERROR, "avcodec_encode_audio() does not " | |
1033 | "support this codec\n"); | |
1034 | return AVERROR(EINVAL); | |
1035 | } | |
1036 | nb_samples = (int64_t)buf_size * 8 / | |
1037 | (av_get_bits_per_sample(avctx->codec_id) * | |
1038 | avctx->channels); | |
1039 | if (nb_samples >= INT_MAX) | |
1040 | return AVERROR(EINVAL); | |
1041 | frame->nb_samples = nb_samples; | |
1042 | } | |
1043 | ||
1044 | /* it is assumed that the samples buffer is large enough based on the | |
1045 | relevant parameters */ | |
1046 | samples_size = av_samples_get_buffer_size(NULL, avctx->channels, | |
1047 | frame->nb_samples, | |
1048 | avctx->sample_fmt, 1); | |
1049 | if ((ret = avcodec_fill_audio_frame(frame, avctx->channels, | |
1050 | avctx->sample_fmt, | |
0c517644 DB |
1051 | (const uint8_t *) samples, |
1052 | samples_size, 1))) | |
b2c75b6e JR |
1053 | return ret; |
1054 | ||
1055 | /* fabricate frame pts from sample count. | |
1056 | this is needed because the avcodec_encode_audio() API does not have | |
1057 | a way for the user to provide pts */ | |
91a28b0e JR |
1058 | frame->pts = ff_samples_to_time_base(avctx, |
1059 | avctx->internal->sample_count); | |
b2c75b6e JR |
1060 | avctx->internal->sample_count += frame->nb_samples; |
1061 | } else { | |
1062 | frame = NULL; | |
1063 | } | |
1064 | ||
1065 | got_packet = 0; | |
1066 | ret = avcodec_encode_audio2(avctx, &pkt, frame, &got_packet); | |
1067 | if (!ret && got_packet && avctx->coded_frame) { | |
1068 | avctx->coded_frame->pts = pkt.pts; | |
1069 | avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY); | |
1070 | } | |
1071 | /* free any side data since we cannot return it */ | |
1072 | if (pkt.side_data_elems > 0) { | |
1073 | int i; | |
1074 | for (i = 0; i < pkt.side_data_elems; i++) | |
1075 | av_free(pkt.side_data[i].data); | |
1076 | av_freep(&pkt.side_data); | |
1077 | pkt.side_data_elems = 0; | |
1078 | } | |
1079 | ||
1080 | if (frame && frame->extended_data != frame->data) | |
1081 | av_free(frame->extended_data); | |
1082 | ||
1083 | return ret ? ret : pkt.size; | |
1084 | } | |
1085 | #endif | |
1086 | ||
52f82a11 | 1087 | #if FF_API_OLD_ENCODE_VIDEO |
5e4c7ca2 | 1088 | int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size, |
492cd3a9 | 1089 | const AVFrame *pict) |
de6d9b64 | 1090 | { |
52f82a11 AK |
1091 | AVPacket pkt; |
1092 | int ret, got_packet = 0; | |
1093 | ||
0ecca7a4 | 1094 | if(buf_size < FF_MIN_BUFFER_SIZE){ |
5286d11f | 1095 | av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n"); |
0ecca7a4 MN |
1096 | return -1; |
1097 | } | |
115329f1 | 1098 | |
52f82a11 AK |
1099 | av_init_packet(&pkt); |
1100 | pkt.data = buf; | |
1101 | pkt.size = buf_size; | |
1102 | ||
1103 | ret = avcodec_encode_video2(avctx, &pkt, pict, &got_packet); | |
1104 | if (!ret && got_packet && avctx->coded_frame) { | |
1105 | avctx->coded_frame->pts = pkt.pts; | |
1106 | avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY); | |
1107 | } | |
1108 | ||
1109 | /* free any side data since we cannot return it */ | |
1110 | if (pkt.side_data_elems > 0) { | |
1111 | int i; | |
1112 | for (i = 0; i < pkt.side_data_elems; i++) | |
1113 | av_free(pkt.side_data[i].data); | |
1114 | av_freep(&pkt.side_data); | |
1115 | pkt.side_data_elems = 0; | |
1116 | } | |
1117 | ||
1118 | return ret ? ret : pkt.size; | |
1119 | } | |
1120 | #endif | |
1121 | ||
52f82a11 AK |
1122 | int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx, |
1123 | AVPacket *avpkt, | |
1124 | const AVFrame *frame, | |
1125 | int *got_packet_ptr) | |
1126 | { | |
1127 | int ret; | |
1128 | int user_packet = !!avpkt->data; | |
1129 | ||
d55fa6f9 AK |
1130 | *got_packet_ptr = 0; |
1131 | ||
52f82a11 | 1132 | if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) { |
7fb6c922 | 1133 | av_free_packet(avpkt); |
52f82a11 AK |
1134 | av_init_packet(avpkt); |
1135 | avpkt->size = 0; | |
6f824977 | 1136 | return 0; |
52f82a11 AK |
1137 | } |
1138 | ||
1139 | if (av_image_check_size(avctx->width, avctx->height, 0, avctx)) | |
1140 | return AVERROR(EINVAL); | |
1141 | ||
ff311c09 | 1142 | av_assert0(avctx->codec->encode2); |
52f82a11 | 1143 | |
ff311c09 AK |
1144 | ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr); |
1145 | if (!ret) { | |
1146 | if (!*got_packet_ptr) | |
1147 | avpkt->size = 0; | |
1148 | else if (!(avctx->codec->capabilities & CODEC_CAP_DELAY)) | |
1149 | avpkt->pts = avpkt->dts = frame->pts; | |
52f82a11 | 1150 | |
3c6607eb | 1151 | if (!user_packet && avpkt->size) { |
eb727387 AK |
1152 | uint8_t *new_data = av_realloc(avpkt->data, avpkt->size); |
1153 | if (new_data) | |
1154 | avpkt->data = new_data; | |
1155 | } | |
1156 | ||
52f82a11 | 1157 | avctx->frame_number++; |
03ca0a5b | 1158 | } |
52f82a11 | 1159 | |
7fb6c922 AK |
1160 | if (ret < 0 || !*got_packet_ptr) |
1161 | av_free_packet(avpkt); | |
1162 | ||
52f82a11 AK |
1163 | emms_c(); |
1164 | return ret; | |
de6d9b64 FB |
1165 | } |
1166 | ||
115329f1 | 1167 | int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size, |
240c1657 FB |
1168 | const AVSubtitle *sub) |
1169 | { | |
1170 | int ret; | |
9413db9e BA |
1171 | if(sub->start_display_time) { |
1172 | av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n"); | |
1173 | return -1; | |
1174 | } | |
505aa6c9 BA |
1175 | if(sub->num_rects == 0 || !sub->rects) |
1176 | return -1; | |
466b39ef | 1177 | ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub); |
240c1657 FB |
1178 | avctx->frame_number++; |
1179 | return ret; | |
1180 | } | |
1181 | ||
867f923d MS |
1182 | static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt) |
1183 | { | |
1184 | int size = 0; | |
1185 | const uint8_t *data; | |
1186 | uint32_t flags; | |
1187 | ||
1188 | if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE)) | |
1189 | return; | |
1190 | ||
1191 | data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size); | |
1192 | if (!data || size < 4) | |
1193 | return; | |
1194 | flags = bytestream_get_le32(&data); | |
1195 | size -= 4; | |
1196 | if (size < 4) /* Required for any of the changes */ | |
1197 | return; | |
1198 | if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) { | |
1199 | avctx->channels = bytestream_get_le32(&data); | |
1200 | size -= 4; | |
1201 | } | |
1202 | if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) { | |
1203 | if (size < 8) | |
1204 | return; | |
1205 | avctx->channel_layout = bytestream_get_le64(&data); | |
1206 | size -= 8; | |
1207 | } | |
1208 | if (size < 4) | |
1209 | return; | |
1210 | if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) { | |
1211 | avctx->sample_rate = bytestream_get_le32(&data); | |
1212 | size -= 4; | |
1213 | } | |
1214 | if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) { | |
1215 | if (size < 8) | |
1216 | return; | |
1217 | avctx->width = bytestream_get_le32(&data); | |
1218 | avctx->height = bytestream_get_le32(&data); | |
c5d907b6 | 1219 | avcodec_set_dimensions(avctx, avctx->width, avctx->height); |
867f923d MS |
1220 | size -= 8; |
1221 | } | |
1222 | } | |
1223 | ||
7a00bbad TB |
1224 | int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture, |
1225 | int *got_picture_ptr, | |
1226 | AVPacket *avpkt) | |
1227 | { | |
de6d9b64 | 1228 | int ret; |
115329f1 | 1229 | |
53db1cae | 1230 | *got_picture_ptr= 0; |
e16f217c | 1231 | if((avctx->coded_width||avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx)) |
0ecca7a4 | 1232 | return -1; |
393cbb96 MN |
1233 | |
1234 | avctx->pkt = avpkt; | |
c5d907b6 | 1235 | apply_param_change(avctx, avpkt); |
393cbb96 | 1236 | |
37b00b47 | 1237 | if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type&FF_THREAD_FRAME)){ |
27237d52 | 1238 | if (HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME) |
37b00b47 AS |
1239 | ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr, |
1240 | avpkt); | |
1241 | else { | |
1242 | ret = avctx->codec->decode(avctx, picture, got_picture_ptr, | |
1243 | avpkt); | |
1244 | picture->pkt_dts= avpkt->dts; | |
b58dbb5b | 1245 | picture->sample_aspect_ratio = avctx->sample_aspect_ratio; |
3a2ddf7c SS |
1246 | picture->width = avctx->width; |
1247 | picture->height = avctx->height; | |
8a4a5f6f | 1248 | picture->format = avctx->pix_fmt; |
37b00b47 | 1249 | } |
6bb925f4 | 1250 | |
bb628dae | 1251 | emms_c(); //needed to avoid an emms_c() call before every return; |
115329f1 DB |
1252 | |
1253 | if (*got_picture_ptr) | |
934982c4 MN |
1254 | avctx->frame_number++; |
1255 | }else | |
1256 | ret= 0; | |
1257 | ||
de6d9b64 FB |
1258 | return ret; |
1259 | } | |
1260 | ||
0eea2129 | 1261 | #if FF_API_OLD_DECODE_AUDIO |
7a00bbad TB |
1262 | int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples, |
1263 | int *frame_size_ptr, | |
1264 | AVPacket *avpkt) | |
1265 | { | |
0eea2129 JR |
1266 | AVFrame frame; |
1267 | int ret, got_frame = 0; | |
1268 | ||
1269 | if (avctx->get_buffer != avcodec_default_get_buffer) { | |
e2ff436e RT |
1270 | av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with" |
1271 | "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n"); | |
1272 | av_log(avctx, AV_LOG_ERROR, "Please port your application to " | |
1273 | "avcodec_decode_audio4()\n"); | |
1274 | avctx->get_buffer = avcodec_default_get_buffer; | |
0eea2129 JR |
1275 | } |
1276 | ||
1277 | ret = avcodec_decode_audio4(avctx, &frame, &got_frame, avpkt); | |
1278 | ||
1279 | if (ret >= 0 && got_frame) { | |
1280 | int ch, plane_size; | |
1281 | int planar = av_sample_fmt_is_planar(avctx->sample_fmt); | |
1282 | int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels, | |
1283 | frame.nb_samples, | |
1284 | avctx->sample_fmt, 1); | |
1285 | if (*frame_size_ptr < data_size) { | |
1286 | av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for " | |
1287 | "the current frame (%d < %d)\n", *frame_size_ptr, data_size); | |
1288 | return AVERROR(EINVAL); | |
1289 | } | |
1290 | ||
1291 | memcpy(samples, frame.extended_data[0], plane_size); | |
1292 | ||
1293 | if (planar && avctx->channels > 1) { | |
1294 | uint8_t *out = ((uint8_t *)samples) + plane_size; | |
1295 | for (ch = 1; ch < avctx->channels; ch++) { | |
1296 | memcpy(out, frame.extended_data[ch], plane_size); | |
1297 | out += plane_size; | |
1298 | } | |
1299 | } | |
1300 | *frame_size_ptr = data_size; | |
1301 | } else { | |
1302 | *frame_size_ptr = 0; | |
1303 | } | |
1304 | return ret; | |
1305 | } | |
1306 | #endif | |
1307 | ||
1308 | int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx, | |
1309 | AVFrame *frame, | |
1310 | int *got_frame_ptr, | |
1311 | AVPacket *avpkt) | |
1312 | { | |
1313 | int ret = 0; | |
1314 | ||
1315 | *got_frame_ptr = 0; | |
de6d9b64 | 1316 | |
393cbb96 MN |
1317 | avctx->pkt = avpkt; |
1318 | ||
6326afd5 JR |
1319 | if (!avpkt->data && avpkt->size) { |
1320 | av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n"); | |
1321 | return AVERROR(EINVAL); | |
1322 | } | |
1323 | ||
f13db94d MS |
1324 | apply_param_change(avctx, avpkt); |
1325 | ||
0eea2129 JR |
1326 | if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) { |
1327 | ret = avctx->codec->decode(avctx, frame, got_frame_ptr, avpkt); | |
1328 | if (ret >= 0 && *got_frame_ptr) { | |
1329 | avctx->frame_number++; | |
1330 | frame->pkt_dts = avpkt->dts; | |
8a4a5f6f SS |
1331 | if (frame->format == AV_SAMPLE_FMT_NONE) |
1332 | frame->format = avctx->sample_fmt; | |
9c856d62 | 1333 | } |
ac66834c | 1334 | } |
de6d9b64 FB |
1335 | return ret; |
1336 | } | |
1337 | ||
7a00bbad TB |
1338 | int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub, |
1339 | int *got_sub_ptr, | |
1340 | AVPacket *avpkt) | |
1341 | { | |
240c1657 FB |
1342 | int ret; |
1343 | ||
393cbb96 | 1344 | avctx->pkt = avpkt; |
240c1657 | 1345 | *got_sub_ptr = 0; |
7a00bbad | 1346 | ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt); |
240c1657 FB |
1347 | if (*got_sub_ptr) |
1348 | avctx->frame_number++; | |
1349 | return ret; | |
1350 | } | |
1351 | ||
e1d7c883 RD |
1352 | void avsubtitle_free(AVSubtitle *sub) |
1353 | { | |
1354 | int i; | |
1355 | ||
1356 | for (i = 0; i < sub->num_rects; i++) | |
1357 | { | |
8b834ac5 RD |
1358 | av_freep(&sub->rects[i]->pict.data[0]); |
1359 | av_freep(&sub->rects[i]->pict.data[1]); | |
1360 | av_freep(&sub->rects[i]->pict.data[2]); | |
1361 | av_freep(&sub->rects[i]->pict.data[3]); | |
1362 | av_freep(&sub->rects[i]->text); | |
1363 | av_freep(&sub->rects[i]->ass); | |
1364 | av_freep(&sub->rects[i]); | |
e1d7c883 RD |
1365 | } |
1366 | ||
8b834ac5 | 1367 | av_freep(&sub->rects); |
e1d7c883 RD |
1368 | |
1369 | memset(sub, 0, sizeof(AVSubtitle)); | |
1370 | } | |
1371 | ||
0752cd39 | 1372 | av_cold int avcodec_close(AVCodecContext *avctx) |
de6d9b64 | 1373 | { |
f988ce6c AÖ |
1374 | /* If there is a user-supplied mutex locking routine, call it. */ |
1375 | if (ff_lockmgr_cb) { | |
1376 | if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN)) | |
1377 | return -1; | |
1378 | } | |
1379 | ||
ddebfb15 MN |
1380 | entangled_thread_counter++; |
1381 | if(entangled_thread_counter != 1){ | |
1382 | av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n"); | |
1383 | entangled_thread_counter--; | |
1384 | return -1; | |
1385 | } | |
1386 | ||
0e72ad95 AK |
1387 | if (avcodec_is_open(avctx)) { |
1388 | if (HAVE_THREADS && avctx->thread_opaque) | |
1389 | ff_thread_free(avctx); | |
1390 | if (avctx->codec && avctx->codec->close) | |
1391 | avctx->codec->close(avctx); | |
1392 | avcodec_default_free_buffers(avctx); | |
1393 | avctx->coded_frame = NULL; | |
1394 | av_freep(&avctx->internal); | |
1395 | } | |
1396 | ||
1397 | if (avctx->priv_data && avctx->codec && avctx->codec->priv_class) | |
36773283 AK |
1398 | av_opt_free(avctx->priv_data); |
1399 | av_opt_free(avctx); | |
3123dd79 | 1400 | av_freep(&avctx->priv_data); |
44fe77b3 | 1401 | if (av_codec_is_encoder(avctx->codec)) |
c4f267ab | 1402 | av_freep(&avctx->extradata); |
de6d9b64 | 1403 | avctx->codec = NULL; |
37b00b47 | 1404 | avctx->active_thread_type = 0; |
ddebfb15 | 1405 | entangled_thread_counter--; |
f988ce6c AÖ |
1406 | |
1407 | /* Release any user-supplied mutex. */ | |
1408 | if (ff_lockmgr_cb) { | |
1409 | (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE); | |
1410 | } | |
de6d9b64 FB |
1411 | return 0; |
1412 | } | |
1413 | ||
36ef5369 | 1414 | AVCodec *avcodec_find_encoder(enum AVCodecID id) |
de6d9b64 | 1415 | { |
93ebfeea | 1416 | AVCodec *p, *experimental=NULL; |
de6d9b64 FB |
1417 | p = first_avcodec; |
1418 | while (p) { | |
44fe77b3 | 1419 | if (av_codec_is_encoder(p) && p->id == id) { |
93ebfeea JG |
1420 | if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) { |
1421 | experimental = p; | |
1422 | } else | |
1423 | return p; | |
1424 | } | |
de6d9b64 FB |
1425 | p = p->next; |
1426 | } | |
93ebfeea | 1427 | return experimental; |
de6d9b64 FB |
1428 | } |
1429 | ||
98f3b098 A |
1430 | AVCodec *avcodec_find_encoder_by_name(const char *name) |
1431 | { | |
1432 | AVCodec *p; | |
fc228c90 AJ |
1433 | if (!name) |
1434 | return NULL; | |
98f3b098 A |
1435 | p = first_avcodec; |
1436 | while (p) { | |
44fe77b3 | 1437 | if (av_codec_is_encoder(p) && strcmp(name,p->name) == 0) |
98f3b098 A |
1438 | return p; |
1439 | p = p->next; | |
1440 | } | |
1441 | return NULL; | |
1442 | } | |
1443 | ||
36ef5369 | 1444 | AVCodec *avcodec_find_decoder(enum AVCodecID id) |
de6d9b64 FB |
1445 | { |
1446 | AVCodec *p; | |
1447 | p = first_avcodec; | |
1448 | while (p) { | |
44fe77b3 | 1449 | if (av_codec_is_decoder(p) && p->id == id) |
de6d9b64 FB |
1450 | return p; |
1451 | p = p->next; | |
1452 | } | |
1453 | return NULL; | |
1454 | } | |
1455 | ||
1456 | AVCodec *avcodec_find_decoder_by_name(const char *name) | |
1457 | { | |
1458 | AVCodec *p; | |
fc228c90 AJ |
1459 | if (!name) |
1460 | return NULL; | |
de6d9b64 FB |
1461 | p = first_avcodec; |
1462 | while (p) { | |
44fe77b3 | 1463 | if (av_codec_is_decoder(p) && strcmp(name,p->name) == 0) |
de6d9b64 FB |
1464 | return p; |
1465 | p = p->next; | |
1466 | } | |
1467 | return NULL; | |
1468 | } | |
1469 | ||
406aa93f | 1470 | static int get_bit_rate(AVCodecContext *ctx) |
ce34ff6b RK |
1471 | { |
1472 | int bit_rate; | |
1473 | int bits_per_sample; | |
1474 | ||
1475 | switch(ctx->codec_type) { | |
72415b2a | 1476 | case AVMEDIA_TYPE_VIDEO: |
4563cf24 SS |
1477 | case AVMEDIA_TYPE_DATA: |
1478 | case AVMEDIA_TYPE_SUBTITLE: | |
1479 | case AVMEDIA_TYPE_ATTACHMENT: | |
ce34ff6b RK |
1480 | bit_rate = ctx->bit_rate; |
1481 | break; | |
72415b2a | 1482 | case AVMEDIA_TYPE_AUDIO: |
ce34ff6b RK |
1483 | bits_per_sample = av_get_bits_per_sample(ctx->codec_id); |
1484 | bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate; | |
1485 | break; | |
ce34ff6b RK |
1486 | default: |
1487 | bit_rate = 0; | |
1488 | break; | |
1489 | } | |
1490 | return bit_rate; | |
1491 | } | |
1492 | ||
7e566bbe SS |
1493 | size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag) |
1494 | { | |
1495 | int i, len, ret = 0; | |
1496 | ||
1497 | for (i = 0; i < 4; i++) { | |
1498 | len = snprintf(buf, buf_size, | |
1499 | isprint(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF); | |
1500 | buf += len; | |
1501 | buf_size = buf_size > len ? buf_size - len : 0; | |
1502 | ret += len; | |
1503 | codec_tag>>=8; | |
1504 | } | |
1505 | return ret; | |
1506 | } | |
1507 | ||
de6d9b64 FB |
1508 | void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode) |
1509 | { | |
1510 | const char *codec_name; | |
2a81f4bd | 1511 | const char *profile = NULL; |
0a0f19b5 | 1512 | const AVCodec *p; |
de6d9b64 | 1513 | char buf1[32]; |
a96b68b7 | 1514 | int bitrate; |
59771f71 | 1515 | AVRational display_aspect_ratio; |
de6d9b64 | 1516 | |
df8d5eaa AC |
1517 | if (enc->codec) |
1518 | p = enc->codec; | |
1519 | else if (encode) | |
de6d9b64 FB |
1520 | p = avcodec_find_encoder(enc->codec_id); |
1521 | else | |
1522 | p = avcodec_find_decoder(enc->codec_id); | |
1523 | ||
1524 | if (p) { | |
1525 | codec_name = p->name; | |
2a81f4bd | 1526 | profile = av_get_profile_name(p, enc->profile); |
36ef5369 | 1527 | } else if (enc->codec_id == AV_CODEC_ID_MPEG2TS) { |
985180a1 FB |
1528 | /* fake mpeg2 transport stream codec (currently not |
1529 | registered) */ | |
1530 | codec_name = "mpeg2ts"; | |
de6d9b64 FB |
1531 | } else if (enc->codec_name[0] != '\0') { |
1532 | codec_name = enc->codec_name; | |
1533 | } else { | |
1534 | /* output avi tags */ | |
ab0b5378 SS |
1535 | char tag_buf[32]; |
1536 | av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag); | |
1537 | snprintf(buf1, sizeof(buf1), "%s / 0x%04X", tag_buf, enc->codec_tag); | |
de6d9b64 FB |
1538 | codec_name = buf1; |
1539 | } | |
1540 | ||
1541 | switch(enc->codec_type) { | |
72415b2a | 1542 | case AVMEDIA_TYPE_VIDEO: |
de6d9b64 FB |
1543 | snprintf(buf, buf_size, |
1544 | "Video: %s%s", | |
7d1c3fc1 | 1545 | codec_name, enc->mb_decision ? " (hq)" : ""); |
2a81f4bd AH |
1546 | if (profile) |
1547 | snprintf(buf + strlen(buf), buf_size - strlen(buf), | |
1548 | " (%s)", profile); | |
82c0c4ba | 1549 | if (enc->pix_fmt != PIX_FMT_NONE) { |
cf087595 FB |
1550 | snprintf(buf + strlen(buf), buf_size - strlen(buf), |
1551 | ", %s", | |
94bed8e5 | 1552 | av_get_pix_fmt_name(enc->pix_fmt)); |
cf087595 | 1553 | } |
de6d9b64 FB |
1554 | if (enc->width) { |
1555 | snprintf(buf + strlen(buf), buf_size - strlen(buf), | |
21189011 MN |
1556 | ", %dx%d", |
1557 | enc->width, enc->height); | |
7ee4dd02 | 1558 | if (enc->sample_aspect_ratio.num) { |
cbaf50f8 BC |
1559 | av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den, |
1560 | enc->width*enc->sample_aspect_ratio.num, | |
1561 | enc->height*enc->sample_aspect_ratio.den, | |
1562 | 1024*1024); | |
1563 | snprintf(buf + strlen(buf), buf_size - strlen(buf), | |
1564 | " [PAR %d:%d DAR %d:%d]", | |
1565 | enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den, | |
1566 | display_aspect_ratio.num, display_aspect_ratio.den); | |
7ee4dd02 | 1567 | } |
a309073b | 1568 | if(av_log_get_level() >= AV_LOG_DEBUG){ |
9ce6c138 | 1569 | int g= av_gcd(enc->time_base.num, enc->time_base.den); |
21189011 MN |
1570 | snprintf(buf + strlen(buf), buf_size - strlen(buf), |
1571 | ", %d/%d", | |
1572 | enc->time_base.num/g, enc->time_base.den/g); | |
1573 | } | |
de6d9b64 | 1574 | } |
4bfad535 FB |
1575 | if (encode) { |
1576 | snprintf(buf + strlen(buf), buf_size - strlen(buf), | |
1577 | ", q=%d-%d", enc->qmin, enc->qmax); | |
1578 | } | |
de6d9b64 | 1579 | break; |
72415b2a | 1580 | case AVMEDIA_TYPE_AUDIO: |
de6d9b64 FB |
1581 | snprintf(buf, buf_size, |
1582 | "Audio: %s", | |
1583 | codec_name); | |
2a81f4bd AH |
1584 | if (profile) |
1585 | snprintf(buf + strlen(buf), buf_size - strlen(buf), | |
1586 | " (%s)", profile); | |
de6d9b64 FB |
1587 | if (enc->sample_rate) { |
1588 | snprintf(buf + strlen(buf), buf_size - strlen(buf), | |
0d72e7d0 | 1589 | ", %d Hz", enc->sample_rate); |
de6d9b64 | 1590 | } |
0d72e7d0 | 1591 | av_strlcat(buf, ", ", buf_size); |
63e8d976 | 1592 | av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout); |
5d6e4c16 | 1593 | if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) { |
9e82a113 | 1594 | snprintf(buf + strlen(buf), buf_size - strlen(buf), |
ba7d6e79 | 1595 | ", %s", av_get_sample_fmt_name(enc->sample_fmt)); |
9e82a113 | 1596 | } |
de6d9b64 | 1597 | break; |
72415b2a | 1598 | case AVMEDIA_TYPE_DATA: |
985180a1 | 1599 | snprintf(buf, buf_size, "Data: %s", codec_name); |
240c1657 | 1600 | break; |
72415b2a | 1601 | case AVMEDIA_TYPE_SUBTITLE: |
240c1657 | 1602 | snprintf(buf, buf_size, "Subtitle: %s", codec_name); |
985180a1 | 1603 | break; |
72415b2a | 1604 | case AVMEDIA_TYPE_ATTACHMENT: |
f8d7c9d3 | 1605 | snprintf(buf, buf_size, "Attachment: %s", codec_name); |
f8d7c9d3 | 1606 | break; |
de6d9b64 | 1607 | default: |
9fe5a7b8 MN |
1608 | snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type); |
1609 | return; | |
de6d9b64 | 1610 | } |
4bfad535 FB |
1611 | if (encode) { |
1612 | if (enc->flags & CODEC_FLAG_PASS1) | |
1613 | snprintf(buf + strlen(buf), buf_size - strlen(buf), | |
1614 | ", pass 1"); | |
1615 | if (enc->flags & CODEC_FLAG_PASS2) | |
1616 | snprintf(buf + strlen(buf), buf_size - strlen(buf), | |
1617 | ", pass 2"); | |
1618 | } | |
406aa93f | 1619 | bitrate = get_bit_rate(enc); |
a96b68b7 | 1620 | if (bitrate != 0) { |
115329f1 | 1621 | snprintf(buf + strlen(buf), buf_size - strlen(buf), |
a96b68b7 | 1622 | ", %d kb/s", bitrate / 1000); |
de6d9b64 FB |
1623 | } |
1624 | } | |
1625 | ||
060ec0a8 AH |
1626 | const char *av_get_profile_name(const AVCodec *codec, int profile) |
1627 | { | |
1628 | const AVProfile *p; | |
1629 | if (profile == FF_PROFILE_UNKNOWN || !codec->profiles) | |
1630 | return NULL; | |
1631 | ||
1632 | for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++) | |
1633 | if (p->profile == profile) | |
1634 | return p->name; | |
1635 | ||
1636 | return NULL; | |
1637 | } | |
1638 | ||
156e5023 NK |
1639 | unsigned avcodec_version( void ) |
1640 | { | |
1641 | return LIBAVCODEC_VERSION_INT; | |
1642 | } | |
cf087595 | 1643 | |
41600690 | 1644 | const char *avcodec_configuration(void) |
c1736936 | 1645 | { |
29ba0911 | 1646 | return LIBAV_CONFIGURATION; |
c1736936 DB |
1647 | } |
1648 | ||
41600690 | 1649 | const char *avcodec_license(void) |
c1736936 DB |
1650 | { |
1651 | #define LICENSE_PREFIX "libavcodec license: " | |
a03be6e1 | 1652 | return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1; |
c1736936 DB |
1653 | } |
1654 | ||
1c2a8c7f MN |
1655 | void avcodec_flush_buffers(AVCodecContext *avctx) |
1656 | { | |
27237d52 | 1657 | if(HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME) |
37b00b47 | 1658 | ff_thread_flush(avctx); |
d1cf4591 | 1659 | else if(avctx->codec->flush) |
7a06ff14 | 1660 | avctx->codec->flush(avctx); |
1c2a8c7f MN |
1661 | } |
1662 | ||
0eea2129 JR |
1663 | static void video_free_buffers(AVCodecContext *s) |
1664 | { | |
f3a29b75 | 1665 | AVCodecInternal *avci = s->internal; |
d90cf87b MN |
1666 | int i, j; |
1667 | ||
f3a29b75 JR |
1668 | if (!avci->buffer) |
1669 | return; | |
115329f1 | 1670 | |
f3a29b75 JR |
1671 | if (avci->buffer_count) |
1672 | av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n", | |
1673 | avci->buffer_count); | |
d90cf87b | 1674 | for(i=0; i<INTERNAL_BUFFER_SIZE; i++){ |
f3a29b75 | 1675 | InternalBuffer *buf = &avci->buffer[i]; |
d90cf87b MN |
1676 | for(j=0; j<4; j++){ |
1677 | av_freep(&buf->base[j]); | |
1678 | buf->data[j]= NULL; | |
1679 | } | |
1680 | } | |
f3a29b75 | 1681 | av_freep(&avci->buffer); |
115329f1 | 1682 | |
f3a29b75 | 1683 | avci->buffer_count=0; |
d90cf87b MN |
1684 | } |
1685 | ||
0eea2129 JR |
1686 | static void audio_free_buffers(AVCodecContext *avctx) |
1687 | { | |
1688 | AVCodecInternal *avci = avctx->internal; | |
1689 | InternalBuffer *buf; | |
1690 | ||
1691 | if (!avci->buffer) | |
1692 | return; | |
1693 | buf = avci->buffer; | |
1694 | ||
1695 | if (buf->extended_data) { | |
1696 | av_free(buf->extended_data[0]); | |
1697 | if (buf->extended_data != buf->data) | |
1698 | av_free(buf->extended_data); | |
1699 | } | |
1700 | av_freep(&avci->buffer); | |
1701 | } | |
1702 | ||
1703 | void avcodec_default_free_buffers(AVCodecContext *avctx) | |
1704 | { | |
1705 | switch (avctx->codec_type) { | |
1706 | case AVMEDIA_TYPE_VIDEO: | |
1707 | video_free_buffers(avctx); | |
1708 | break; | |
1709 | case AVMEDIA_TYPE_AUDIO: | |
1710 | audio_free_buffers(avctx); | |
1711 | break; | |
1712 | default: | |
1713 | break; | |
1714 | } | |
1715 | } | |
1716 | ||
36ef5369 | 1717 | int av_get_exact_bits_per_sample(enum AVCodecID codec_id) |
6699d074 | 1718 | { |
ac3e1834 | 1719 | switch(codec_id){ |
36ef5369 AK |
1720 | case AV_CODEC_ID_ADPCM_CT: |
1721 | case AV_CODEC_ID_ADPCM_IMA_APC: | |
1722 | case AV_CODEC_ID_ADPCM_IMA_EA_SEAD: | |
1723 | case AV_CODEC_ID_ADPCM_IMA_WS: | |
1724 | case AV_CODEC_ID_ADPCM_G722: | |
1725 | case AV_CODEC_ID_ADPCM_YAMAHA: | |
f32fd318 | 1726 | return 4; |
36ef5369 AK |
1727 | case AV_CODEC_ID_PCM_ALAW: |
1728 | case AV_CODEC_ID_PCM_MULAW: | |
1729 | case AV_CODEC_ID_PCM_S8: | |
1730 | case AV_CODEC_ID_PCM_U8: | |
1731 | case AV_CODEC_ID_PCM_ZORK: | |
ac3e1834 | 1732 | return 8; |
36ef5369 AK |
1733 | case AV_CODEC_ID_PCM_S16BE: |
1734 | case AV_CODEC_ID_PCM_S16LE: | |
1735 | case AV_CODEC_ID_PCM_S16LE_PLANAR: | |
1736 | case AV_CODEC_ID_PCM_U16BE: | |
1737 | case AV_CODEC_ID_PCM_U16LE: | |
ac3e1834 | 1738 | return 16; |
36ef5369 AK |
1739 | case AV_CODEC_ID_PCM_S24DAUD: |
1740 | case AV_CODEC_ID_PCM_S24BE: | |
1741 | case AV_CODEC_ID_PCM_S24LE: | |
1742 | case AV_CODEC_ID_PCM_U24BE: | |
1743 | case AV_CODEC_ID_PCM_U24LE: | |
ac3e1834 | 1744 | return 24; |
36ef5369 AK |
1745 | case AV_CODEC_ID_PCM_S32BE: |
1746 | case AV_CODEC_ID_PCM_S32LE: | |
1747 | case AV_CODEC_ID_PCM_U32BE: | |
1748 | case AV_CODEC_ID_PCM_U32LE: | |
1749 | case AV_CODEC_ID_PCM_F32BE: | |
1750 | case AV_CODEC_ID_PCM_F32LE: | |
ac3e1834 | 1751 | return 32; |
36ef5369 AK |
1752 | case AV_CODEC_ID_PCM_F64BE: |
1753 | case AV_CODEC_ID_PCM_F64LE: | |
143a5d6f | 1754 | return 64; |
ac3e1834 BC |
1755 | default: |
1756 | return 0; | |
1757 | } | |
1758 | } | |
1759 | ||
36ef5369 | 1760 | int av_get_bits_per_sample(enum AVCodecID codec_id) |
6699d074 JR |
1761 | { |
1762 | switch (codec_id) { | |
36ef5369 | 1763 | case AV_CODEC_ID_ADPCM_SBPRO_2: |
6699d074 | 1764 | return 2; |
36ef5369 | 1765 | case AV_CODEC_ID_ADPCM_SBPRO_3: |
6699d074 | 1766 | return 3; |
36ef5369 AK |
1767 | case AV_CODEC_ID_ADPCM_SBPRO_4: |
1768 | case AV_CODEC_ID_ADPCM_IMA_WAV: | |
1769 | case AV_CODEC_ID_ADPCM_IMA_QT: | |
1770 | case AV_CODEC_ID_ADPCM_SWF: | |
1771 | case AV_CODEC_ID_ADPCM_MS: | |
6699d074 JR |
1772 | return 4; |
1773 | default: | |
1774 | return av_get_exact_bits_per_sample(codec_id); | |
1775 | } | |
1776 | } | |
1777 | ||
9524cf79 JR |
1778 | int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes) |
1779 | { | |
1780 | int id, sr, ch, ba, tag, bps; | |
1781 | ||
1782 | id = avctx->codec_id; | |
1783 | sr = avctx->sample_rate; | |
1784 | ch = avctx->channels; | |
1785 | ba = avctx->block_align; | |
1786 | tag = avctx->codec_tag; | |
1787 | bps = av_get_exact_bits_per_sample(avctx->codec_id); | |
1788 | ||
1789 | /* codecs with an exact constant bits per sample */ | |
1790 | if (bps > 0 && ch > 0 && frame_bytes > 0) | |
1791 | return (frame_bytes * 8) / (bps * ch); | |
1792 | bps = avctx->bits_per_coded_sample; | |
1793 | ||
1794 | /* codecs with a fixed packet duration */ | |
1795 | switch (id) { | |
36ef5369 AK |
1796 | case AV_CODEC_ID_ADPCM_ADX: return 32; |
1797 | case AV_CODEC_ID_ADPCM_IMA_QT: return 64; | |
1798 | case AV_CODEC_ID_ADPCM_EA_XAS: return 128; | |
1799 | case AV_CODEC_ID_AMR_NB: | |
1800 | case AV_CODEC_ID_GSM: | |
1801 | case AV_CODEC_ID_QCELP: | |
1802 | case AV_CODEC_ID_RA_144: | |
1803 | case AV_CODEC_ID_RA_288: return 160; | |
1804 | case AV_CODEC_ID_IMC: return 256; | |
1805 | case AV_CODEC_ID_AMR_WB: | |
1806 | case AV_CODEC_ID_GSM_MS: return 320; | |
1807 | case AV_CODEC_ID_MP1: return 384; | |
1808 | case AV_CODEC_ID_ATRAC1: return 512; | |
1809 | case AV_CODEC_ID_ATRAC3: return 1024; | |
1810 | case AV_CODEC_ID_MP2: | |
1811 | case AV_CODEC_ID_MUSEPACK7: return 1152; | |
1812 | case AV_CODEC_ID_AC3: return 1536; | |
9524cf79 JR |
1813 | } |
1814 | ||
1815 | if (sr > 0) { | |
1816 | /* calc from sample rate */ | |
36ef5369 | 1817 | if (id == AV_CODEC_ID_TTA) |
9524cf79 JR |
1818 | return 256 * sr / 245; |
1819 | ||
1820 | if (ch > 0) { | |
1821 | /* calc from sample rate and channels */ | |
36ef5369 | 1822 | if (id == AV_CODEC_ID_BINKAUDIO_DCT) |
9524cf79 JR |
1823 | return (480 << (sr / 22050)) / ch; |
1824 | } | |
1825 | } | |
1826 | ||
1827 | if (ba > 0) { | |
1828 | /* calc from block_align */ | |
36ef5369 | 1829 | if (id == AV_CODEC_ID_SIPR) { |
9524cf79 JR |
1830 | switch (ba) { |
1831 | case 20: return 160; | |
1832 | case 19: return 144; | |
1833 | case 29: return 288; | |
1834 | case 37: return 480; | |
1835 | } | |
36ef5369 | 1836 | } else if (id == AV_CODEC_ID_ILBC) { |
3641b048 MS |
1837 | switch (ba) { |
1838 | case 38: return 160; | |
1839 | case 50: return 240; | |
1840 | } | |
9524cf79 JR |
1841 | } |
1842 | } | |
1843 | ||
1844 | if (frame_bytes > 0) { | |
1845 | /* calc from frame_bytes only */ | |
36ef5369 | 1846 | if (id == AV_CODEC_ID_TRUESPEECH) |
9524cf79 | 1847 | return 240 * (frame_bytes / 32); |
36ef5369 | 1848 | if (id == AV_CODEC_ID_NELLYMOSER) |
9524cf79 JR |
1849 | return 256 * (frame_bytes / 64); |
1850 | ||
1851 | if (bps > 0) { | |
1852 | /* calc from frame_bytes and bits_per_coded_sample */ | |
36ef5369 | 1853 | if (id == AV_CODEC_ID_ADPCM_G726) |
9524cf79 JR |
1854 | return frame_bytes * 8 / bps; |
1855 | } | |
1856 | ||
1857 | if (ch > 0) { | |
1858 | /* calc from frame_bytes and channels */ | |
1859 | switch (id) { | |
36ef5369 AK |
1860 | case AV_CODEC_ID_ADPCM_4XM: |
1861 | case AV_CODEC_ID_ADPCM_IMA_ISS: | |
9524cf79 | 1862 | return (frame_bytes - 4 * ch) * 2 / ch; |
36ef5369 | 1863 | case AV_CODEC_ID_ADPCM_IMA_SMJPEG: |
9524cf79 | 1864 | return (frame_bytes - 4) * 2 / ch; |
36ef5369 | 1865 | case AV_CODEC_ID_ADPCM_IMA_AMV: |
9524cf79 | 1866 | return (frame_bytes - 8) * 2 / ch; |
36ef5369 | 1867 | case AV_CODEC_ID_ADPCM_XA: |
9524cf79 | 1868 | return (frame_bytes / 128) * 224 / ch; |
36ef5369 | 1869 | case AV_CODEC_ID_INTERPLAY_DPCM: |
9524cf79 | 1870 | return (frame_bytes - 6 - ch) / ch; |
36ef5369 | 1871 | case AV_CODEC_ID_ROQ_DPCM: |
9524cf79 | 1872 | return (frame_bytes - 8) / ch; |
36ef5369 | 1873 | case AV_CODEC_ID_XAN_DPCM: |
9524cf79 | 1874 | return (frame_bytes - 2 * ch) / ch; |
36ef5369 | 1875 | case AV_CODEC_ID_MACE3: |
9524cf79 | 1876 | return 3 * frame_bytes / ch; |
36ef5369 | 1877 | case AV_CODEC_ID_MACE6: |
9524cf79 | 1878 | return 6 * frame_bytes / ch; |
36ef5369 | 1879 | case AV_CODEC_ID_PCM_LXF: |
9524cf79 JR |
1880 | return 2 * (frame_bytes / (5 * ch)); |
1881 | } | |
1882 | ||
1883 | if (tag) { | |
1884 | /* calc from frame_bytes, channels, and codec_tag */ | |
36ef5369 | 1885 | if (id == AV_CODEC_ID_SOL_DPCM) { |
9524cf79 JR |
1886 | if (tag == 3) |
1887 | return frame_bytes / ch; | |
1888 | else | |
1889 | return frame_bytes * 2 / ch; | |
1890 | } | |
1891 | } | |
1892 | ||
1893 | if (ba > 0) { | |
1894 | /* calc from frame_bytes, channels, and block_align */ | |
1895 | int blocks = frame_bytes / ba; | |
1896 | switch (avctx->codec_id) { | |
36ef5369 | 1897 | case AV_CODEC_ID_ADPCM_IMA_WAV: |
9524cf79 | 1898 | return blocks * (1 + (ba - 4 * ch) / (4 * ch) * 8); |
36ef5369 | 1899 | case AV_CODEC_ID_ADPCM_IMA_DK3: |
c346f630 | 1900 | return blocks * (((ba - 16) * 2 / 3 * 4) / ch); |
36ef5369 | 1901 | case AV_CODEC_ID_ADPCM_IMA_DK4: |
9524cf79 | 1902 | return blocks * (1 + (ba - 4 * ch) * 2 / ch); |
36ef5369 | 1903 | case AV_CODEC_ID_ADPCM_MS: |
9524cf79 JR |
1904 | return blocks * (2 + (ba - 7 * ch) * 2 / ch); |
1905 | } | |
1906 | } | |
1907 | ||
1908 | if (bps > 0) { | |
1909 | /* calc from frame_bytes, channels, and bits_per_coded_sample */ | |
1910 | switch (avctx->codec_id) { | |
36ef5369 | 1911 | case AV_CODEC_ID_PCM_DVD: |
9524cf79 | 1912 | return 2 * (frame_bytes / ((bps * 2 / 8) * ch)); |
36ef5369 | 1913 | case AV_CODEC_ID_PCM_BLURAY: |
9524cf79 | 1914 | return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8); |
36ef5369 | 1915 | case AV_CODEC_ID_S302M: |
9524cf79 JR |
1916 | return 2 * (frame_bytes / ((bps + 4) / 4)) / ch; |
1917 | } | |
1918 | } | |
1919 | } | |
1920 | } | |
1921 | ||
1922 | return 0; | |
1923 | } | |
1924 | ||
b250f9c6 | 1925 | #if !HAVE_THREADS |
ba9ef8d0 | 1926 | int ff_thread_init(AVCodecContext *s){ |
ca8ad847 MN |
1927 | return -1; |
1928 | } | |
1929 | #endif | |
ad2b531d MR |
1930 | |
1931 | unsigned int av_xiphlacing(unsigned char *s, unsigned int v) | |
1932 | { | |
1933 | unsigned int n = 0; | |
1934 | ||
1935 | while(v >= 0xff) { | |
1936 | *s++ = 0xff; | |
1937 | v -= 0xff; | |
1938 | n++; | |
1939 | } | |
1940 | *s = v; | |
1941 | n++; | |
1942 | return n; | |
1943 | } | |
1005f542 | 1944 | |
c46eeae2 MN |
1945 | int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){ |
1946 | int i; | |
1947 | for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++); | |
1948 | return i; | |
1949 | } | |
1950 | ||
ce863d7f | 1951 | void av_log_missing_feature(void *avc, const char *feature, int want_sample) |
ea779d91 | 1952 | { |
6001dad6 | 1953 | av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your Libav " |
db323491 | 1954 | "version to the newest one from Git. If the problem still " |
ea779d91 | 1955 | "occurs, it means that your file has a feature which has not " |
3fd3632f | 1956 | "been implemented.\n", feature); |
ea779d91 | 1957 | if(want_sample) |
ce863d7f | 1958 | av_log_ask_for_sample(avc, NULL); |
0ba39dd1 KG |
1959 | } |
1960 | ||
44f566b7 | 1961 | void av_log_ask_for_sample(void *avc, const char *msg, ...) |
0ba39dd1 | 1962 | { |
44f566b7 DB |
1963 | va_list argument_list; |
1964 | ||
1965 | va_start(argument_list, msg); | |
1966 | ||
0ba39dd1 | 1967 | if (msg) |
44f566b7 | 1968 | av_vlog(avc, AV_LOG_WARNING, msg, argument_list); |
0ba39dd1 | 1969 | av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample " |
f0a41afd | 1970 | "of this file to ftp://upload.libav.org/incoming/ " |
21de9204 | 1971 | "and contact the libav-devel mailing list.\n"); |
44f566b7 DB |
1972 | |
1973 | va_end(argument_list); | |
ea779d91 | 1974 | } |
c895618b MN |
1975 | |
1976 | static AVHWAccel *first_hwaccel = NULL; | |
1977 | ||
1978 | void av_register_hwaccel(AVHWAccel *hwaccel) | |
1979 | { | |
1980 | AVHWAccel **p = &first_hwaccel; | |
1981 | while (*p) | |
1982 | p = &(*p)->next; | |
1983 | *p = hwaccel; | |
1984 | hwaccel->next = NULL; | |
1985 | } | |
414d9d7f MN |
1986 | |
1987 | AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel) | |
1988 | { | |
1989 | return hwaccel ? hwaccel->next : first_hwaccel; | |
1990 | } | |
6059f13c | 1991 | |
36ef5369 | 1992 | AVHWAccel *ff_find_hwaccel(enum AVCodecID codec_id, enum PixelFormat pix_fmt) |
6059f13c MN |
1993 | { |
1994 | AVHWAccel *hwaccel=NULL; | |
1995 | ||
1996 | while((hwaccel= av_hwaccel_next(hwaccel))){ | |
1997 | if ( hwaccel->id == codec_id | |
1998 | && hwaccel->pix_fmt == pix_fmt) | |
1999 | return hwaccel; | |
2000 | } | |
2001 | return NULL; | |
2002 | } | |
f988ce6c AÖ |
2003 | |
2004 | int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op)) | |
2005 | { | |
2006 | if (ff_lockmgr_cb) { | |
2007 | if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY)) | |
2008 | return -1; | |
2d1b6fb7 MS |
2009 | if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY)) |
2010 | return -1; | |
f988ce6c AÖ |
2011 | } |
2012 | ||
2013 | ff_lockmgr_cb = cb; | |
2014 | ||
2015 | if (ff_lockmgr_cb) { | |
2016 | if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE)) | |
2017 | return -1; | |
2d1b6fb7 MS |
2018 | if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE)) |
2019 | return -1; | |
2020 | } | |
2021 | return 0; | |
2022 | } | |
2023 | ||
2024 | int avpriv_lock_avformat(void) | |
2025 | { | |
2026 | if (ff_lockmgr_cb) { | |
2027 | if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN)) | |
2028 | return -1; | |
2029 | } | |
2030 | return 0; | |
2031 | } | |
2032 | ||
2033 | int avpriv_unlock_avformat(void) | |
2034 | { | |
2035 | if (ff_lockmgr_cb) { | |
2036 | if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE)) | |
2037 | return -1; | |
f988ce6c AÖ |
2038 | } |
2039 | return 0; | |
2040 | } | |
603a5f04 | 2041 | |
0842d589 | 2042 | unsigned int avpriv_toupper4(unsigned int x) |
603a5f04 FL |
2043 | { |
2044 | return toupper( x &0xFF) | |
2045 | + (toupper((x>>8 )&0xFF)<<8 ) | |
2046 | + (toupper((x>>16)&0xFF)<<16) | |
2047 | + (toupper((x>>24)&0xFF)<<24); | |
2048 | } | |
37b00b47 | 2049 | |
27237d52 | 2050 | #if !HAVE_THREADS |
37b00b47 AS |
2051 | |
2052 | int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f) | |
2053 | { | |
2054 | f->owner = avctx; | |
2055 | return avctx->get_buffer(avctx, f); | |
2056 | } | |
2057 | ||
2058 | void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f) | |
2059 | { | |
2060 | f->owner->release_buffer(f->owner, f); | |
2061 | } | |
2062 | ||
2063 | void ff_thread_finish_setup(AVCodecContext *avctx) | |
2064 | { | |
2065 | } | |
2066 | ||
2067 | void ff_thread_report_progress(AVFrame *f, int progress, int field) | |
2068 | { | |
2069 | } | |
2070 | ||
2071 | void ff_thread_await_progress(AVFrame *f, int progress, int field) | |
2072 | { | |
2073 | } | |
2074 | ||
2075 | #endif | |
65af48b5 | 2076 | |
36ef5369 | 2077 | enum AVMediaType avcodec_get_type(enum AVCodecID codec_id) |
bca06e77 | 2078 | { |
36ef5369 | 2079 | if (codec_id <= AV_CODEC_ID_NONE) |
bca06e77 | 2080 | return AVMEDIA_TYPE_UNKNOWN; |
36ef5369 | 2081 | else if (codec_id < AV_CODEC_ID_FIRST_AUDIO) |
bca06e77 | 2082 | return AVMEDIA_TYPE_VIDEO; |
36ef5369 | 2083 | else if (codec_id < AV_CODEC_ID_FIRST_SUBTITLE) |
bca06e77 | 2084 | return AVMEDIA_TYPE_AUDIO; |
36ef5369 | 2085 | else if (codec_id < AV_CODEC_ID_FIRST_UNKNOWN) |
bca06e77 AK |
2086 | return AVMEDIA_TYPE_SUBTITLE; |
2087 | ||
2088 | return AVMEDIA_TYPE_UNKNOWN; | |
2089 | } | |
af08d9ae AK |
2090 | |
2091 | int avcodec_is_open(AVCodecContext *s) | |
2092 | { | |
2093 | return !!s->internal; | |
2094 | } |