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