added mpeg in avi support
[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;
a949d72e
MN
64}
65
66/**
67 * allocates a AVCodecContext and set it to defaults.
68 * this can be deallocated by simply calling free()
69 */
7ffbb60e 70AVCodecContext *avcodec_alloc_context(void){
a949d72e
MN
71 AVCodecContext *avctx= av_mallocz(sizeof(AVCodecContext));
72
73 if(avctx==NULL) return NULL;
74
75 avcodec_get_context_defaults(avctx);
76
77 return avctx;
78}
79
de6d9b64
FB
80int avcodec_open(AVCodecContext *avctx, AVCodec *codec)
81{
82 int ret;
83
84 avctx->codec = codec;
85 avctx->frame_number = 0;
0edf8a7a
PG
86 if (codec->priv_data_size > 0) {
87 avctx->priv_data = av_mallocz(codec->priv_data_size);
88 if (!avctx->priv_data)
89 return -ENOMEM;
90 } else {
91 avctx->priv_data = NULL;
92 }
de6d9b64
FB
93 ret = avctx->codec->init(avctx);
94 if (ret < 0) {
3123dd79 95 av_freep(&avctx->priv_data);
de6d9b64
FB
96 return ret;
97 }
98 return 0;
99}
100
101int avcodec_encode_audio(AVCodecContext *avctx, UINT8 *buf, int buf_size,
102 const short *samples)
103{
104 int ret;
105
106 ret = avctx->codec->encode(avctx, buf, buf_size, (void *)samples);
107 avctx->frame_number++;
108 return ret;
109}
110
111int avcodec_encode_video(AVCodecContext *avctx, UINT8 *buf, int buf_size,
112 const AVPicture *pict)
113{
114 int ret;
115
116 ret = avctx->codec->encode(avctx, buf, buf_size, (void *)pict);
117 avctx->frame_number++;
118 return ret;
119}
120
121/* decode a frame. return -1 if error, otherwise return the number of
122 bytes used. If no frame could be decompressed, *got_picture_ptr is
123 zero. Otherwise, it is non zero */
124int avcodec_decode_video(AVCodecContext *avctx, AVPicture *picture,
125 int *got_picture_ptr,
126 UINT8 *buf, int buf_size)
127{
128 int ret;
129
130 ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
131 buf, buf_size);
1cb0edb4
J
132 if (*got_picture_ptr)
133 avctx->frame_number++;
de6d9b64
FB
134 return ret;
135}
136
137/* decode an audio frame. return -1 if error, otherwise return the
138 *number of bytes used. If no frame could be decompressed,
139 *frame_size_ptr is zero. Otherwise, it is the decompressed frame
140 *size in BYTES. */
141int avcodec_decode_audio(AVCodecContext *avctx, INT16 *samples,
142 int *frame_size_ptr,
143 UINT8 *buf, int buf_size)
144{
145 int ret;
146
147 ret = avctx->codec->decode(avctx, samples, frame_size_ptr,
148 buf, buf_size);
149 avctx->frame_number++;
150 return ret;
151}
152
153int avcodec_close(AVCodecContext *avctx)
154{
155 if (avctx->codec->close)
156 avctx->codec->close(avctx);
3123dd79 157 av_freep(&avctx->priv_data);
de6d9b64
FB
158 avctx->codec = NULL;
159 return 0;
160}
161
162AVCodec *avcodec_find_encoder(enum CodecID id)
163{
164 AVCodec *p;
165 p = first_avcodec;
166 while (p) {
167 if (p->encode != NULL && p->id == id)
168 return p;
169 p = p->next;
170 }
171 return NULL;
172}
173
98f3b098
A
174AVCodec *avcodec_find_encoder_by_name(const char *name)
175{
176 AVCodec *p;
177 p = first_avcodec;
178 while (p) {
179 if (p->encode != NULL && strcmp(name,p->name) == 0)
180 return p;
181 p = p->next;
182 }
183 return NULL;
184}
185
de6d9b64
FB
186AVCodec *avcodec_find_decoder(enum CodecID id)
187{
188 AVCodec *p;
189 p = first_avcodec;
190 while (p) {
191 if (p->decode != NULL && p->id == id)
192 return p;
193 p = p->next;
194 }
195 return NULL;
196}
197
198AVCodec *avcodec_find_decoder_by_name(const char *name)
199{
200 AVCodec *p;
201 p = first_avcodec;
202 while (p) {
203 if (p->decode != NULL && strcmp(name,p->name) == 0)
204 return p;
205 p = p->next;
206 }
207 return NULL;
208}
209
210AVCodec *avcodec_find(enum CodecID id)
211{
212 AVCodec *p;
213 p = first_avcodec;
214 while (p) {
215 if (p->id == id)
216 return p;
217 p = p->next;
218 }
219 return NULL;
220}
221
cf087595 222const char *pix_fmt_str[] = {
bc657ac3 223 "??",
cf087595
FB
224 "yuv420p",
225 "yuv422",
226 "rgb24",
227 "bgr24",
228 "yuv422p",
229 "yuv444p",
b71472eb
PG
230 "rgba32",
231 "bgra32",
be8ffec9 232 "yuv410p"
cf087595
FB
233};
234
de6d9b64
FB
235void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
236{
237 const char *codec_name;
238 AVCodec *p;
239 char buf1[32];
623563c0 240 char channels_str[100];
a96b68b7 241 int bitrate;
de6d9b64
FB
242
243 if (encode)
244 p = avcodec_find_encoder(enc->codec_id);
245 else
246 p = avcodec_find_decoder(enc->codec_id);
247
248 if (p) {
249 codec_name = p->name;
250 } else if (enc->codec_name[0] != '\0') {
251 codec_name = enc->codec_name;
252 } else {
253 /* output avi tags */
254 if (enc->codec_type == CODEC_TYPE_VIDEO) {
255 snprintf(buf1, sizeof(buf1), "%c%c%c%c",
256 enc->codec_tag & 0xff,
257 (enc->codec_tag >> 8) & 0xff,
258 (enc->codec_tag >> 16) & 0xff,
259 (enc->codec_tag >> 24) & 0xff);
260 } else {
261 snprintf(buf1, sizeof(buf1), "0x%04x", enc->codec_tag);
262 }
263 codec_name = buf1;
264 }
265
266 switch(enc->codec_type) {
267 case CODEC_TYPE_VIDEO:
268 snprintf(buf, buf_size,
269 "Video: %s%s",
270 codec_name, enc->flags & CODEC_FLAG_HQ ? " (hq)" : "");
cf087595
FB
271 if (enc->codec_id == CODEC_ID_RAWVIDEO) {
272 snprintf(buf + strlen(buf), buf_size - strlen(buf),
273 ", %s",
274 pix_fmt_str[enc->pix_fmt]);
275 }
de6d9b64
FB
276 if (enc->width) {
277 snprintf(buf + strlen(buf), buf_size - strlen(buf),
278 ", %dx%d, %0.2f fps",
279 enc->width, enc->height,
280 (float)enc->frame_rate / FRAME_RATE_BASE);
281 }
bc657ac3
ZK
282 snprintf(buf + strlen(buf), buf_size - strlen(buf),
283 ", q=%d-%d", enc->qmin, enc->qmax);
284
a96b68b7 285 bitrate = enc->bit_rate;
de6d9b64
FB
286 break;
287 case CODEC_TYPE_AUDIO:
288 snprintf(buf, buf_size,
289 "Audio: %s",
290 codec_name);
e0d2714a
J
291 switch (enc->channels) {
292 case 1:
623563c0 293 strcpy(channels_str, "mono");
e0d2714a
J
294 break;
295 case 2:
623563c0 296 strcpy(channels_str, "stereo");
e0d2714a
J
297 break;
298 case 6:
623563c0 299 strcpy(channels_str, "5:1");
e0d2714a
J
300 break;
301 default:
302 sprintf(channels_str, "%d channels", enc->channels);
303 break;
304 }
de6d9b64
FB
305 if (enc->sample_rate) {
306 snprintf(buf + strlen(buf), buf_size - strlen(buf),
307 ", %d Hz, %s",
308 enc->sample_rate,
e0d2714a 309 channels_str);
de6d9b64 310 }
e0d2714a 311
a96b68b7
FB
312 /* for PCM codecs, compute bitrate directly */
313 switch(enc->codec_id) {
314 case CODEC_ID_PCM_S16LE:
315 case CODEC_ID_PCM_S16BE:
316 case CODEC_ID_PCM_U16LE:
317 case CODEC_ID_PCM_U16BE:
a190b7e9 318 bitrate = enc->sample_rate * enc->channels * 16;
a96b68b7
FB
319 break;
320 case CODEC_ID_PCM_S8:
321 case CODEC_ID_PCM_U8:
322 case CODEC_ID_PCM_ALAW:
323 case CODEC_ID_PCM_MULAW:
a190b7e9 324 bitrate = enc->sample_rate * enc->channels * 8;
a96b68b7
FB
325 break;
326 default:
327 bitrate = enc->bit_rate;
328 break;
329 }
de6d9b64
FB
330 break;
331 default:
b71472eb 332 av_abort();
de6d9b64 333 }
a96b68b7 334 if (bitrate != 0) {
de6d9b64 335 snprintf(buf + strlen(buf), buf_size - strlen(buf),
a96b68b7 336 ", %d kb/s", bitrate / 1000);
de6d9b64
FB
337 }
338}
339
cf087595
FB
340/* Picture field are filled with 'ptr' addresses */
341void avpicture_fill(AVPicture *picture, UINT8 *ptr,
342 int pix_fmt, int width, int height)
343{
344 int size;
345
346 size = width * height;
347 switch(pix_fmt) {
348 case PIX_FMT_YUV420P:
349 picture->data[0] = ptr;
350 picture->data[1] = picture->data[0] + size;
351 picture->data[2] = picture->data[1] + size / 4;
352 picture->linesize[0] = width;
353 picture->linesize[1] = width / 2;
354 picture->linesize[2] = width / 2;
355 break;
356 case PIX_FMT_YUV422P:
357 picture->data[0] = ptr;
358 picture->data[1] = picture->data[0] + size;
359 picture->data[2] = picture->data[1] + size / 2;
360 picture->linesize[0] = width;
361 picture->linesize[1] = width / 2;
362 picture->linesize[2] = width / 2;
363 break;
364 case PIX_FMT_YUV444P:
365 picture->data[0] = ptr;
366 picture->data[1] = picture->data[0] + size;
367 picture->data[2] = picture->data[1] + size;
368 picture->linesize[0] = width;
369 picture->linesize[1] = width;
370 picture->linesize[2] = width;
371 break;
372 case PIX_FMT_RGB24:
373 case PIX_FMT_BGR24:
374 picture->data[0] = ptr;
375 picture->data[1] = NULL;
376 picture->data[2] = NULL;
377 picture->linesize[0] = width * 3;
378 break;
b71472eb
PG
379 case PIX_FMT_RGBA32:
380 case PIX_FMT_BGRA32:
381 picture->data[0] = ptr;
382 picture->data[1] = NULL;
383 picture->data[2] = NULL;
384 picture->linesize[0] = width * 4;
385 break;
cf087595
FB
386 case PIX_FMT_YUV422:
387 picture->data[0] = ptr;
388 picture->data[1] = NULL;
389 picture->data[2] = NULL;
390 picture->linesize[0] = width * 2;
391 break;
392 default:
393 picture->data[0] = NULL;
394 picture->data[1] = NULL;
395 picture->data[2] = NULL;
396 break;
397 }
398}
399
400int avpicture_get_size(int pix_fmt, int width, int height)
401{
402 int size;
403
404 size = width * height;
405 switch(pix_fmt) {
406 case PIX_FMT_YUV420P:
407 size = (size * 3) / 2;
408 break;
409 case PIX_FMT_YUV422P:
410 size = (size * 2);
411 break;
412 case PIX_FMT_YUV444P:
413 size = (size * 3);
414 break;
415 case PIX_FMT_RGB24:
416 case PIX_FMT_BGR24:
417 size = (size * 3);
418 break;
b71472eb
PG
419 case PIX_FMT_RGBA32:
420 case PIX_FMT_BGRA32:
421 size = (size * 4);
422 break;
cf087595
FB
423 case PIX_FMT_YUV422:
424 size = (size * 2);
425 break;
426 default:
427 size = -1;
428 break;
429 }
430 return size;
431}
432
156e5023
NK
433unsigned avcodec_version( void )
434{
435 return LIBAVCODEC_VERSION_INT;
436}
cf087595 437
8bceb6af
NK
438unsigned avcodec_build( void )
439{
440 return LIBAVCODEC_BUILD;
441}
442
de6d9b64
FB
443/* must be called before any other functions */
444void avcodec_init(void)
445{
0344cd0a
AB
446 static int inited = 0;
447
448 if (inited != 0)
449 return;
450 inited = 1;
451
de6d9b64
FB
452 dsputil_init();
453}
454
1c2a8c7f
MN
455/* this should be called after seeking and before trying to decode the next frame */
456void avcodec_flush_buffers(AVCodecContext *avctx)
457{
458 MpegEncContext *s = avctx->priv_data;
459 s->num_available_buffers=0;
460}
461
462
cd4af68a 463static int raw_encode_init(AVCodecContext *s)
de6d9b64
FB
464{
465 return 0;
466}
467
cd4af68a
ZK
468static int raw_decode_frame(AVCodecContext *avctx,
469 void *data, int *data_size,
470 UINT8 *buf, int buf_size)
de6d9b64
FB
471{
472 return -1;
473}
474
cd4af68a
ZK
475static int raw_encode_frame(AVCodecContext *avctx,
476 unsigned char *frame, int buf_size, void *data)
de6d9b64
FB
477{
478 return -1;
479}
480
de6d9b64
FB
481AVCodec rawvideo_codec = {
482 "rawvideo",
483 CODEC_TYPE_VIDEO,
484 CODEC_ID_RAWVIDEO,
485 0,
cd4af68a
ZK
486 raw_encode_init,
487 raw_encode_frame,
de6d9b64 488 NULL,
cd4af68a 489 raw_decode_frame,
de6d9b64 490};