* Fix a problem with the first sample when down sampling.
[libav.git] / libavcodec / utils.c
CommitLineData
de6d9b64
FB
1/*
2 * utils for libavcodec
3 * Copyright (c) 2001 Gerard Lantau.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
de6d9b64
FB
19#include <stdio.h>
20#include <string.h>
21#include <errno.h>
22#include "common.h"
23#include "dsputil.h"
24#include "avcodec.h"
1c2a8c7f 25#include "mpegvideo.h"
3d204385
NK
26#ifdef HAVE_MALLOC_H
27#include <malloc.h>
28#else
29#include <stdlib.h>
30#endif
de6d9b64
FB
31
32/* memory alloc */
33void *av_mallocz(int size)
34{
35 void *ptr;
36157009 36#if defined ( ARCH_X86 ) && defined ( HAVE_MEMALIGN )
36157009
NK
37 ptr = memalign(64,size);
38 /* Why 64?
39 Indeed, we should align it:
40 on 4 for 386
41 on 16 for 486
42 on 32 for 586, PPro - k6-III
43 on 64 for K7 (maybe for P3 too).
44 Because L1 and L2 caches are aligned on those values.
45 But I don't want to code such logic here!
46 */
47#else
de6d9b64 48 ptr = malloc(size);
36157009 49#endif
de6d9b64
FB
50 if (!ptr)
51 return NULL;
52 memset(ptr, 0, size);
53 return ptr;
54}
55
56/* encoder management */
57AVCodec *first_avcodec;
58
59void register_avcodec(AVCodec *format)
60{
61 AVCodec **p;
62 p = &first_avcodec;
63 while (*p != NULL) p = &(*p)->next;
64 *p = format;
65 format->next = NULL;
66}
67
68int avcodec_open(AVCodecContext *avctx, AVCodec *codec)
69{
70 int ret;
71
72 avctx->codec = codec;
73 avctx->frame_number = 0;
74 avctx->priv_data = av_mallocz(codec->priv_data_size);
75 if (!avctx->priv_data)
76 return -ENOMEM;
77 ret = avctx->codec->init(avctx);
78 if (ret < 0) {
79 free(avctx->priv_data);
80 avctx->priv_data = NULL;
81 return ret;
82 }
83 return 0;
84}
85
86int avcodec_encode_audio(AVCodecContext *avctx, UINT8 *buf, int buf_size,
87 const short *samples)
88{
89 int ret;
90
91 ret = avctx->codec->encode(avctx, buf, buf_size, (void *)samples);
92 avctx->frame_number++;
93 return ret;
94}
95
96int avcodec_encode_video(AVCodecContext *avctx, UINT8 *buf, int buf_size,
97 const AVPicture *pict)
98{
99 int ret;
100
101 ret = avctx->codec->encode(avctx, buf, buf_size, (void *)pict);
102 avctx->frame_number++;
103 return ret;
104}
105
106/* decode a frame. return -1 if error, otherwise return the number of
107 bytes used. If no frame could be decompressed, *got_picture_ptr is
108 zero. Otherwise, it is non zero */
109int avcodec_decode_video(AVCodecContext *avctx, AVPicture *picture,
110 int *got_picture_ptr,
111 UINT8 *buf, int buf_size)
112{
113 int ret;
114
115 ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
116 buf, buf_size);
1cb0edb4
J
117 if (*got_picture_ptr)
118 avctx->frame_number++;
de6d9b64
FB
119 return ret;
120}
121
122/* decode an audio frame. return -1 if error, otherwise return the
123 *number of bytes used. If no frame could be decompressed,
124 *frame_size_ptr is zero. Otherwise, it is the decompressed frame
125 *size in BYTES. */
126int avcodec_decode_audio(AVCodecContext *avctx, INT16 *samples,
127 int *frame_size_ptr,
128 UINT8 *buf, int buf_size)
129{
130 int ret;
131
132 ret = avctx->codec->decode(avctx, samples, frame_size_ptr,
133 buf, buf_size);
134 avctx->frame_number++;
135 return ret;
136}
137
138int avcodec_close(AVCodecContext *avctx)
139{
140 if (avctx->codec->close)
141 avctx->codec->close(avctx);
142 free(avctx->priv_data);
143 avctx->priv_data = NULL;
144 avctx->codec = NULL;
145 return 0;
146}
147
148AVCodec *avcodec_find_encoder(enum CodecID id)
149{
150 AVCodec *p;
151 p = first_avcodec;
152 while (p) {
153 if (p->encode != NULL && p->id == id)
154 return p;
155 p = p->next;
156 }
157 return NULL;
158}
159
98f3b098
A
160AVCodec *avcodec_find_encoder_by_name(const char *name)
161{
162 AVCodec *p;
163 p = first_avcodec;
164 while (p) {
165 if (p->encode != NULL && strcmp(name,p->name) == 0)
166 return p;
167 p = p->next;
168 }
169 return NULL;
170}
171
de6d9b64
FB
172AVCodec *avcodec_find_decoder(enum CodecID id)
173{
174 AVCodec *p;
175 p = first_avcodec;
176 while (p) {
177 if (p->decode != NULL && p->id == id)
178 return p;
179 p = p->next;
180 }
181 return NULL;
182}
183
184AVCodec *avcodec_find_decoder_by_name(const char *name)
185{
186 AVCodec *p;
187 p = first_avcodec;
188 while (p) {
189 if (p->decode != NULL && strcmp(name,p->name) == 0)
190 return p;
191 p = p->next;
192 }
193 return NULL;
194}
195
196AVCodec *avcodec_find(enum CodecID id)
197{
198 AVCodec *p;
199 p = first_avcodec;
200 while (p) {
201 if (p->id == id)
202 return p;
203 p = p->next;
204 }
205 return NULL;
206}
207
cf087595 208const char *pix_fmt_str[] = {
bc657ac3 209 "??",
cf087595
FB
210 "yuv420p",
211 "yuv422",
212 "rgb24",
213 "bgr24",
214 "yuv422p",
215 "yuv444p",
216};
217
de6d9b64
FB
218void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
219{
220 const char *codec_name;
221 AVCodec *p;
222 char buf1[32];
623563c0 223 char channels_str[100];
a96b68b7 224 int bitrate;
de6d9b64
FB
225
226 if (encode)
227 p = avcodec_find_encoder(enc->codec_id);
228 else
229 p = avcodec_find_decoder(enc->codec_id);
230
231 if (p) {
232 codec_name = p->name;
233 } else if (enc->codec_name[0] != '\0') {
234 codec_name = enc->codec_name;
235 } else {
236 /* output avi tags */
237 if (enc->codec_type == CODEC_TYPE_VIDEO) {
238 snprintf(buf1, sizeof(buf1), "%c%c%c%c",
239 enc->codec_tag & 0xff,
240 (enc->codec_tag >> 8) & 0xff,
241 (enc->codec_tag >> 16) & 0xff,
242 (enc->codec_tag >> 24) & 0xff);
243 } else {
244 snprintf(buf1, sizeof(buf1), "0x%04x", enc->codec_tag);
245 }
246 codec_name = buf1;
247 }
248
249 switch(enc->codec_type) {
250 case CODEC_TYPE_VIDEO:
251 snprintf(buf, buf_size,
252 "Video: %s%s",
253 codec_name, enc->flags & CODEC_FLAG_HQ ? " (hq)" : "");
cf087595
FB
254 if (enc->codec_id == CODEC_ID_RAWVIDEO) {
255 snprintf(buf + strlen(buf), buf_size - strlen(buf),
256 ", %s",
257 pix_fmt_str[enc->pix_fmt]);
258 }
de6d9b64
FB
259 if (enc->width) {
260 snprintf(buf + strlen(buf), buf_size - strlen(buf),
261 ", %dx%d, %0.2f fps",
262 enc->width, enc->height,
263 (float)enc->frame_rate / FRAME_RATE_BASE);
264 }
bc657ac3
ZK
265 snprintf(buf + strlen(buf), buf_size - strlen(buf),
266 ", q=%d-%d", enc->qmin, enc->qmax);
267
a96b68b7 268 bitrate = enc->bit_rate;
de6d9b64
FB
269 break;
270 case CODEC_TYPE_AUDIO:
271 snprintf(buf, buf_size,
272 "Audio: %s",
273 codec_name);
e0d2714a
J
274 switch (enc->channels) {
275 case 1:
623563c0 276 strcpy(channels_str, "mono");
e0d2714a
J
277 break;
278 case 2:
623563c0 279 strcpy(channels_str, "stereo");
e0d2714a
J
280 break;
281 case 6:
623563c0 282 strcpy(channels_str, "5:1");
e0d2714a
J
283 break;
284 default:
285 sprintf(channels_str, "%d channels", enc->channels);
286 break;
287 }
de6d9b64
FB
288 if (enc->sample_rate) {
289 snprintf(buf + strlen(buf), buf_size - strlen(buf),
290 ", %d Hz, %s",
291 enc->sample_rate,
e0d2714a 292 channels_str);
de6d9b64 293 }
e0d2714a 294
a96b68b7
FB
295 /* for PCM codecs, compute bitrate directly */
296 switch(enc->codec_id) {
297 case CODEC_ID_PCM_S16LE:
298 case CODEC_ID_PCM_S16BE:
299 case CODEC_ID_PCM_U16LE:
300 case CODEC_ID_PCM_U16BE:
a190b7e9 301 bitrate = enc->sample_rate * enc->channels * 16;
a96b68b7
FB
302 break;
303 case CODEC_ID_PCM_S8:
304 case CODEC_ID_PCM_U8:
305 case CODEC_ID_PCM_ALAW:
306 case CODEC_ID_PCM_MULAW:
a190b7e9 307 bitrate = enc->sample_rate * enc->channels * 8;
a96b68b7
FB
308 break;
309 default:
310 bitrate = enc->bit_rate;
311 break;
312 }
de6d9b64
FB
313 break;
314 default:
315 abort();
316 }
a96b68b7 317 if (bitrate != 0) {
de6d9b64 318 snprintf(buf + strlen(buf), buf_size - strlen(buf),
a96b68b7 319 ", %d kb/s", bitrate / 1000);
de6d9b64
FB
320 }
321}
322
cf087595
FB
323/* Picture field are filled with 'ptr' addresses */
324void avpicture_fill(AVPicture *picture, UINT8 *ptr,
325 int pix_fmt, int width, int height)
326{
327 int size;
328
329 size = width * height;
330 switch(pix_fmt) {
331 case PIX_FMT_YUV420P:
332 picture->data[0] = ptr;
333 picture->data[1] = picture->data[0] + size;
334 picture->data[2] = picture->data[1] + size / 4;
335 picture->linesize[0] = width;
336 picture->linesize[1] = width / 2;
337 picture->linesize[2] = width / 2;
338 break;
339 case PIX_FMT_YUV422P:
340 picture->data[0] = ptr;
341 picture->data[1] = picture->data[0] + size;
342 picture->data[2] = picture->data[1] + size / 2;
343 picture->linesize[0] = width;
344 picture->linesize[1] = width / 2;
345 picture->linesize[2] = width / 2;
346 break;
347 case PIX_FMT_YUV444P:
348 picture->data[0] = ptr;
349 picture->data[1] = picture->data[0] + size;
350 picture->data[2] = picture->data[1] + size;
351 picture->linesize[0] = width;
352 picture->linesize[1] = width;
353 picture->linesize[2] = width;
354 break;
355 case PIX_FMT_RGB24:
356 case PIX_FMT_BGR24:
357 picture->data[0] = ptr;
358 picture->data[1] = NULL;
359 picture->data[2] = NULL;
360 picture->linesize[0] = width * 3;
361 break;
362 case PIX_FMT_YUV422:
363 picture->data[0] = ptr;
364 picture->data[1] = NULL;
365 picture->data[2] = NULL;
366 picture->linesize[0] = width * 2;
367 break;
368 default:
369 picture->data[0] = NULL;
370 picture->data[1] = NULL;
371 picture->data[2] = NULL;
372 break;
373 }
374}
375
376int avpicture_get_size(int pix_fmt, int width, int height)
377{
378 int size;
379
380 size = width * height;
381 switch(pix_fmt) {
382 case PIX_FMT_YUV420P:
383 size = (size * 3) / 2;
384 break;
385 case PIX_FMT_YUV422P:
386 size = (size * 2);
387 break;
388 case PIX_FMT_YUV444P:
389 size = (size * 3);
390 break;
391 case PIX_FMT_RGB24:
392 case PIX_FMT_BGR24:
393 size = (size * 3);
394 break;
395 case PIX_FMT_YUV422:
396 size = (size * 2);
397 break;
398 default:
399 size = -1;
400 break;
401 }
402 return size;
403}
404
156e5023
NK
405unsigned avcodec_version( void )
406{
407 return LIBAVCODEC_VERSION_INT;
408}
cf087595 409
de6d9b64
FB
410/* must be called before any other functions */
411void avcodec_init(void)
412{
0344cd0a
AB
413 static int inited = 0;
414
415 if (inited != 0)
416 return;
417 inited = 1;
418
de6d9b64
FB
419 dsputil_init();
420}
421
422/* simple call to use all the codecs */
423void avcodec_register_all(void)
424{
0344cd0a
AB
425 static int inited = 0;
426
427 if (inited != 0)
428 return;
429 inited = 1;
430
d771bcae
FB
431 /* encoders */
432#ifdef CONFIG_ENCODERS
de6d9b64
FB
433 register_avcodec(&ac3_encoder);
434 register_avcodec(&mp2_encoder);
9d36bdc9
J
435#ifdef CONFIG_MP3LAME
436 register_avcodec(&mp3lame_encoder);
437#endif
de6d9b64
FB
438 register_avcodec(&mpeg1video_encoder);
439 register_avcodec(&h263_encoder);
440 register_avcodec(&h263p_encoder);
441 register_avcodec(&rv10_encoder);
442 register_avcodec(&mjpeg_encoder);
58f26ba9 443 register_avcodec(&mpeg4_encoder);
84afee34
MN
444 register_avcodec(&msmpeg4v1_encoder);
445 register_avcodec(&msmpeg4v2_encoder);
446 register_avcodec(&msmpeg4v3_encoder);
d771bcae 447#endif /* CONFIG_ENCODERS */
de6d9b64 448 register_avcodec(&rawvideo_codec);
d771bcae 449
de6d9b64 450 /* decoders */
d771bcae 451#ifdef CONFIG_DECODERS
de6d9b64 452 register_avcodec(&h263_decoder);
58f26ba9 453 register_avcodec(&mpeg4_decoder);
84afee34
MN
454 register_avcodec(&msmpeg4v1_decoder);
455 register_avcodec(&msmpeg4v2_decoder);
456 register_avcodec(&msmpeg4v3_decoder);
e1a9dbff 457 register_avcodec(&wmv1_decoder);
de6d9b64
FB
458 register_avcodec(&mpeg_decoder);
459 register_avcodec(&h263i_decoder);
460 register_avcodec(&rv10_decoder);
4e66ab3b 461 register_avcodec(&mjpeg_decoder);
4b1f4f23 462 register_avcodec(&mp2_decoder);
d771bcae 463 register_avcodec(&mp3_decoder);
d771bcae
FB
464#ifdef CONFIG_AC3
465 register_avcodec(&ac3_decoder);
466#endif
467#endif /* CONFIG_DECODERS */
a96b68b7
FB
468
469 /* pcm codecs */
470
471#define PCM_CODEC(id, name) \
472 register_avcodec(& name ## _encoder); \
473 register_avcodec(& name ## _decoder); \
474
475PCM_CODEC(CODEC_ID_PCM_S16LE, pcm_s16le);
476PCM_CODEC(CODEC_ID_PCM_S16BE, pcm_s16be);
477PCM_CODEC(CODEC_ID_PCM_U16LE, pcm_u16le);
478PCM_CODEC(CODEC_ID_PCM_U16BE, pcm_u16be);
479PCM_CODEC(CODEC_ID_PCM_S8, pcm_s8);
480PCM_CODEC(CODEC_ID_PCM_U8, pcm_u8);
481PCM_CODEC(CODEC_ID_PCM_ALAW, pcm_alaw);
482PCM_CODEC(CODEC_ID_PCM_MULAW, pcm_mulaw);
483
484#undef PCM_CODEC
de6d9b64
FB
485}
486
1c2a8c7f
MN
487/* this should be called after seeking and before trying to decode the next frame */
488void avcodec_flush_buffers(AVCodecContext *avctx)
489{
490 MpegEncContext *s = avctx->priv_data;
491 s->num_available_buffers=0;
492}
493
494
de6d9b64
FB
495static int encode_init(AVCodecContext *s)
496{
497 return 0;
498}
499
500static int decode_frame(AVCodecContext *avctx,
501 void *data, int *data_size,
502 UINT8 *buf, int buf_size)
503{
504 return -1;
505}
506
507static int encode_frame(AVCodecContext *avctx,
508 unsigned char *frame, int buf_size, void *data)
509{
510 return -1;
511}
512
de6d9b64
FB
513AVCodec rawvideo_codec = {
514 "rawvideo",
515 CODEC_TYPE_VIDEO,
516 CODEC_ID_RAWVIDEO,
517 0,
518 encode_init,
519 encode_frame,
520 NULL,
521 decode_frame,
522};