cleanup
[libav.git] / libavcodec / utils.c
CommitLineData
de6d9b64
FB
1/*
2 * utils for libavcodec
ff4ec49e 3 * Copyright (c) 2001 Fabrice Bellard.
de6d9b64 4 *
ff4ec49e
FB
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
de6d9b64 9 *
ff4ec49e 10 * This library is distributed in the hope that it will be useful,
de6d9b64 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
ff4ec49e
FB
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
de6d9b64 14 *
ff4ec49e
FB
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
de6d9b64 18 */
de6d9b64 19#include "avcodec.h"
3123dd79 20#include "dsputil.h"
1c2a8c7f 21#include "mpegvideo.h"
de6d9b64 22
3123dd79
FB
23void *av_mallocz(int size)
24{
25 void *ptr;
26 ptr = av_malloc(size);
27 if (!ptr)
28 return NULL;
29 memset(ptr, 0, size);
30 return ptr;
31}
32
4d7a0a05
FB
33/* cannot call it directly because of 'void **' casting is not automatic */
34void __av_freep(void **ptr)
35{
36 av_free(*ptr);
37 *ptr = NULL;
38}
39
de6d9b64
FB
40/* encoder management */
41AVCodec *first_avcodec;
42
43void register_avcodec(AVCodec *format)
44{
45 AVCodec **p;
46 p = &first_avcodec;
47 while (*p != NULL) p = &(*p)->next;
48 *p = format;
49 format->next = NULL;
50}
51
a949d72e 52void avcodec_get_context_defaults(AVCodecContext *s){
e8b62df6
MN
53 s->bit_rate= 800*1000;
54 s->bit_rate_tolerance= s->bit_rate*10;
a949d72e
MN
55 s->qmin= 2;
56 s->qmax= 31;
57 s->rc_eq= "tex^qComp";
58 s->qcompress= 0.5;
e8b62df6
MN
59 s->max_qdiff= 3;
60 s->b_quant_factor=1.25;
61 s->b_quant_offset=1.25;
b3a391e8 62 s->i_quant_factor=-0.8;
e8b62df6 63 s->i_quant_offset=0.0;
4d2858de 64 s->error_concealment= 3;
8d0e42ca 65 s->error_resilience= 1;
4d2858de 66 s->workaround_bugs= FF_BUG_AUTODETECT;
8d0e42ca
MN
67 s->frame_rate = 25 * FRAME_RATE_BASE;
68 s->gop_size= 50;
69 s->me_method= ME_EPZS;
a949d72e
MN
70}
71
72/**
73 * allocates a AVCodecContext and set it to defaults.
74 * this can be deallocated by simply calling free()
75 */
7ffbb60e 76AVCodecContext *avcodec_alloc_context(void){
a949d72e
MN
77 AVCodecContext *avctx= av_mallocz(sizeof(AVCodecContext));
78
79 if(avctx==NULL) return NULL;
80
81 avcodec_get_context_defaults(avctx);
82
83 return avctx;
84}
85
de6d9b64
FB
86int avcodec_open(AVCodecContext *avctx, AVCodec *codec)
87{
88 int ret;
89
90 avctx->codec = codec;
91 avctx->frame_number = 0;
0edf8a7a
PG
92 if (codec->priv_data_size > 0) {
93 avctx->priv_data = av_mallocz(codec->priv_data_size);
94 if (!avctx->priv_data)
95 return -ENOMEM;
96 } else {
97 avctx->priv_data = NULL;
98 }
de6d9b64
FB
99 ret = avctx->codec->init(avctx);
100 if (ret < 0) {
3123dd79 101 av_freep(&avctx->priv_data);
de6d9b64
FB
102 return ret;
103 }
104 return 0;
105}
106
107int avcodec_encode_audio(AVCodecContext *avctx, UINT8 *buf, int buf_size,
108 const short *samples)
109{
110 int ret;
111
112 ret = avctx->codec->encode(avctx, buf, buf_size, (void *)samples);
113 avctx->frame_number++;
114 return ret;
115}
116
117int avcodec_encode_video(AVCodecContext *avctx, UINT8 *buf, int buf_size,
118 const AVPicture *pict)
119{
120 int ret;
121
122 ret = avctx->codec->encode(avctx, buf, buf_size, (void *)pict);
123 avctx->frame_number++;
124 return ret;
125}
126
127/* decode a frame. return -1 if error, otherwise return the number of
128 bytes used. If no frame could be decompressed, *got_picture_ptr is
129 zero. Otherwise, it is non zero */
130int avcodec_decode_video(AVCodecContext *avctx, AVPicture *picture,
131 int *got_picture_ptr,
132 UINT8 *buf, int buf_size)
133{
134 int ret;
135
136 ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
137 buf, buf_size);
1cb0edb4
J
138 if (*got_picture_ptr)
139 avctx->frame_number++;
de6d9b64
FB
140 return ret;
141}
142
143/* decode an audio frame. return -1 if error, otherwise return the
144 *number of bytes used. If no frame could be decompressed,
145 *frame_size_ptr is zero. Otherwise, it is the decompressed frame
146 *size in BYTES. */
147int avcodec_decode_audio(AVCodecContext *avctx, INT16 *samples,
148 int *frame_size_ptr,
149 UINT8 *buf, int buf_size)
150{
151 int ret;
152
153 ret = avctx->codec->decode(avctx, samples, frame_size_ptr,
154 buf, buf_size);
155 avctx->frame_number++;
156 return ret;
157}
158
159int avcodec_close(AVCodecContext *avctx)
160{
161 if (avctx->codec->close)
162 avctx->codec->close(avctx);
3123dd79 163 av_freep(&avctx->priv_data);
de6d9b64
FB
164 avctx->codec = NULL;
165 return 0;
166}
167
168AVCodec *avcodec_find_encoder(enum CodecID id)
169{
170 AVCodec *p;
171 p = first_avcodec;
172 while (p) {
173 if (p->encode != NULL && p->id == id)
174 return p;
175 p = p->next;
176 }
177 return NULL;
178}
179
98f3b098
A
180AVCodec *avcodec_find_encoder_by_name(const char *name)
181{
182 AVCodec *p;
183 p = first_avcodec;
184 while (p) {
185 if (p->encode != NULL && strcmp(name,p->name) == 0)
186 return p;
187 p = p->next;
188 }
189 return NULL;
190}
191
de6d9b64
FB
192AVCodec *avcodec_find_decoder(enum CodecID id)
193{
194 AVCodec *p;
195 p = first_avcodec;
196 while (p) {
197 if (p->decode != NULL && p->id == id)
198 return p;
199 p = p->next;
200 }
201 return NULL;
202}
203
204AVCodec *avcodec_find_decoder_by_name(const char *name)
205{
206 AVCodec *p;
207 p = first_avcodec;
208 while (p) {
209 if (p->decode != NULL && strcmp(name,p->name) == 0)
210 return p;
211 p = p->next;
212 }
213 return NULL;
214}
215
216AVCodec *avcodec_find(enum CodecID id)
217{
218 AVCodec *p;
219 p = first_avcodec;
220 while (p) {
221 if (p->id == id)
222 return p;
223 p = p->next;
224 }
225 return NULL;
226}
227
cf087595
FB
228const char *pix_fmt_str[] = {
229 "yuv420p",
230 "yuv422",
231 "rgb24",
232 "bgr24",
233 "yuv422p",
234 "yuv444p",
b71472eb
PG
235 "rgba32",
236 "bgra32",
a5e88058
FB
237 "yuv410p",
238 "yuv411p",
cf087595 239};
a5e88058 240
de6d9b64
FB
241void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
242{
243 const char *codec_name;
244 AVCodec *p;
245 char buf1[32];
623563c0 246 char channels_str[100];
a96b68b7 247 int bitrate;
de6d9b64
FB
248
249 if (encode)
250 p = avcodec_find_encoder(enc->codec_id);
251 else
252 p = avcodec_find_decoder(enc->codec_id);
253
254 if (p) {
255 codec_name = p->name;
256 } else if (enc->codec_name[0] != '\0') {
257 codec_name = enc->codec_name;
258 } else {
259 /* output avi tags */
260 if (enc->codec_type == CODEC_TYPE_VIDEO) {
261 snprintf(buf1, sizeof(buf1), "%c%c%c%c",
262 enc->codec_tag & 0xff,
263 (enc->codec_tag >> 8) & 0xff,
264 (enc->codec_tag >> 16) & 0xff,
265 (enc->codec_tag >> 24) & 0xff);
266 } else {
267 snprintf(buf1, sizeof(buf1), "0x%04x", enc->codec_tag);
268 }
269 codec_name = buf1;
270 }
271
272 switch(enc->codec_type) {
273 case CODEC_TYPE_VIDEO:
274 snprintf(buf, buf_size,
275 "Video: %s%s",
276 codec_name, enc->flags & CODEC_FLAG_HQ ? " (hq)" : "");
cf087595
FB
277 if (enc->codec_id == CODEC_ID_RAWVIDEO) {
278 snprintf(buf + strlen(buf), buf_size - strlen(buf),
279 ", %s",
280 pix_fmt_str[enc->pix_fmt]);
281 }
de6d9b64
FB
282 if (enc->width) {
283 snprintf(buf + strlen(buf), buf_size - strlen(buf),
284 ", %dx%d, %0.2f fps",
285 enc->width, enc->height,
286 (float)enc->frame_rate / FRAME_RATE_BASE);
287 }
4bfad535
FB
288 if (encode) {
289 snprintf(buf + strlen(buf), buf_size - strlen(buf),
290 ", q=%d-%d", enc->qmin, enc->qmax);
291 }
a96b68b7 292 bitrate = enc->bit_rate;
de6d9b64
FB
293 break;
294 case CODEC_TYPE_AUDIO:
295 snprintf(buf, buf_size,
296 "Audio: %s",
297 codec_name);
e0d2714a
J
298 switch (enc->channels) {
299 case 1:
623563c0 300 strcpy(channels_str, "mono");
e0d2714a
J
301 break;
302 case 2:
623563c0 303 strcpy(channels_str, "stereo");
e0d2714a
J
304 break;
305 case 6:
623563c0 306 strcpy(channels_str, "5:1");
e0d2714a
J
307 break;
308 default:
309 sprintf(channels_str, "%d channels", enc->channels);
310 break;
311 }
de6d9b64
FB
312 if (enc->sample_rate) {
313 snprintf(buf + strlen(buf), buf_size - strlen(buf),
314 ", %d Hz, %s",
315 enc->sample_rate,
e0d2714a 316 channels_str);
de6d9b64 317 }
e0d2714a 318
a96b68b7
FB
319 /* for PCM codecs, compute bitrate directly */
320 switch(enc->codec_id) {
321 case CODEC_ID_PCM_S16LE:
322 case CODEC_ID_PCM_S16BE:
323 case CODEC_ID_PCM_U16LE:
324 case CODEC_ID_PCM_U16BE:
a190b7e9 325 bitrate = enc->sample_rate * enc->channels * 16;
a96b68b7
FB
326 break;
327 case CODEC_ID_PCM_S8:
328 case CODEC_ID_PCM_U8:
329 case CODEC_ID_PCM_ALAW:
330 case CODEC_ID_PCM_MULAW:
a190b7e9 331 bitrate = enc->sample_rate * enc->channels * 8;
a96b68b7
FB
332 break;
333 default:
334 bitrate = enc->bit_rate;
335 break;
336 }
de6d9b64
FB
337 break;
338 default:
b71472eb 339 av_abort();
de6d9b64 340 }
4bfad535
FB
341 if (encode) {
342 if (enc->flags & CODEC_FLAG_PASS1)
343 snprintf(buf + strlen(buf), buf_size - strlen(buf),
344 ", pass 1");
345 if (enc->flags & CODEC_FLAG_PASS2)
346 snprintf(buf + strlen(buf), buf_size - strlen(buf),
347 ", pass 2");
348 }
a96b68b7 349 if (bitrate != 0) {
de6d9b64 350 snprintf(buf + strlen(buf), buf_size - strlen(buf),
a96b68b7 351 ", %d kb/s", bitrate / 1000);
de6d9b64
FB
352 }
353}
354
cf087595
FB
355/* Picture field are filled with 'ptr' addresses */
356void avpicture_fill(AVPicture *picture, UINT8 *ptr,
357 int pix_fmt, int width, int height)
358{
359 int size;
360
361 size = width * height;
362 switch(pix_fmt) {
363 case PIX_FMT_YUV420P:
364 picture->data[0] = ptr;
365 picture->data[1] = picture->data[0] + size;
366 picture->data[2] = picture->data[1] + size / 4;
367 picture->linesize[0] = width;
368 picture->linesize[1] = width / 2;
369 picture->linesize[2] = width / 2;
370 break;
371 case PIX_FMT_YUV422P:
372 picture->data[0] = ptr;
373 picture->data[1] = picture->data[0] + size;
374 picture->data[2] = picture->data[1] + size / 2;
375 picture->linesize[0] = width;
376 picture->linesize[1] = width / 2;
377 picture->linesize[2] = width / 2;
378 break;
379 case PIX_FMT_YUV444P:
380 picture->data[0] = ptr;
381 picture->data[1] = picture->data[0] + size;
382 picture->data[2] = picture->data[1] + size;
383 picture->linesize[0] = width;
384 picture->linesize[1] = width;
385 picture->linesize[2] = width;
386 break;
387 case PIX_FMT_RGB24:
388 case PIX_FMT_BGR24:
389 picture->data[0] = ptr;
390 picture->data[1] = NULL;
391 picture->data[2] = NULL;
392 picture->linesize[0] = width * 3;
393 break;
b71472eb
PG
394 case PIX_FMT_RGBA32:
395 case PIX_FMT_BGRA32:
396 picture->data[0] = ptr;
397 picture->data[1] = NULL;
398 picture->data[2] = NULL;
399 picture->linesize[0] = width * 4;
400 break;
cf087595
FB
401 case PIX_FMT_YUV422:
402 picture->data[0] = ptr;
403 picture->data[1] = NULL;
404 picture->data[2] = NULL;
405 picture->linesize[0] = width * 2;
406 break;
407 default:
408 picture->data[0] = NULL;
409 picture->data[1] = NULL;
410 picture->data[2] = NULL;
411 break;
412 }
413}
414
415int avpicture_get_size(int pix_fmt, int width, int height)
416{
417 int size;
418
419 size = width * height;
420 switch(pix_fmt) {
421 case PIX_FMT_YUV420P:
422 size = (size * 3) / 2;
423 break;
424 case PIX_FMT_YUV422P:
425 size = (size * 2);
426 break;
427 case PIX_FMT_YUV444P:
428 size = (size * 3);
429 break;
430 case PIX_FMT_RGB24:
431 case PIX_FMT_BGR24:
432 size = (size * 3);
433 break;
b71472eb
PG
434 case PIX_FMT_RGBA32:
435 case PIX_FMT_BGRA32:
436 size = (size * 4);
437 break;
cf087595
FB
438 case PIX_FMT_YUV422:
439 size = (size * 2);
440 break;
441 default:
442 size = -1;
443 break;
444 }
445 return size;
446}
447
156e5023
NK
448unsigned avcodec_version( void )
449{
450 return LIBAVCODEC_VERSION_INT;
451}
cf087595 452
8bceb6af
NK
453unsigned avcodec_build( void )
454{
455 return LIBAVCODEC_BUILD;
456}
457
de6d9b64
FB
458/* must be called before any other functions */
459void avcodec_init(void)
460{
0344cd0a
AB
461 static int inited = 0;
462
463 if (inited != 0)
464 return;
465 inited = 1;
466
de6d9b64
FB
467 dsputil_init();
468}
469
1c2a8c7f
MN
470/* this should be called after seeking and before trying to decode the next frame */
471void avcodec_flush_buffers(AVCodecContext *avctx)
472{
473 MpegEncContext *s = avctx->priv_data;
474 s->num_available_buffers=0;
475}
476
477
cd4af68a 478static int raw_encode_init(AVCodecContext *s)
de6d9b64
FB
479{
480 return 0;
481}
482
cd4af68a
ZK
483static int raw_decode_frame(AVCodecContext *avctx,
484 void *data, int *data_size,
485 UINT8 *buf, int buf_size)
de6d9b64
FB
486{
487 return -1;
488}
489
cd4af68a
ZK
490static int raw_encode_frame(AVCodecContext *avctx,
491 unsigned char *frame, int buf_size, void *data)
de6d9b64
FB
492{
493 return -1;
494}
495
de6d9b64
FB
496AVCodec rawvideo_codec = {
497 "rawvideo",
498 CODEC_TYPE_VIDEO,
499 CODEC_ID_RAWVIDEO,
500 0,
cd4af68a
ZK
501 raw_encode_init,
502 raw_encode_frame,
de6d9b64 503 NULL,
cd4af68a 504 raw_decode_frame,
de6d9b64 505};