Mercurial > libavcodec.hg
annotate utils.c @ 315:40d8092e2ff0 libavcodec
* using pixtype as enum - by Philip Gladstone
author | kabi |
---|---|
date | Mon, 08 Apr 2002 21:03:35 +0000 |
parents | ac677a84d5df |
children | 21697f35a9ca |
rev | line source |
---|---|
0 | 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 */ | |
19 #include <stdio.h> | |
20 #include <string.h> | |
21 #include <errno.h> | |
22 #include "common.h" | |
23 #include "dsputil.h" | |
24 #include "avcodec.h" | |
80 | 25 #ifdef HAVE_MALLOC_H |
26 #include <malloc.h> | |
27 #else | |
28 #include <stdlib.h> | |
29 #endif | |
0 | 30 |
31 /* memory alloc */ | |
32 void *av_mallocz(int size) | |
33 { | |
34 void *ptr; | |
77 | 35 #if defined ( ARCH_X86 ) && defined ( HAVE_MEMALIGN ) |
36 ptr = memalign(64,size); | |
37 /* Why 64? | |
38 Indeed, we should align it: | |
39 on 4 for 386 | |
40 on 16 for 486 | |
41 on 32 for 586, PPro - k6-III | |
42 on 64 for K7 (maybe for P3 too). | |
43 Because L1 and L2 caches are aligned on those values. | |
44 But I don't want to code such logic here! | |
45 */ | |
46 #else | |
0 | 47 ptr = malloc(size); |
77 | 48 #endif |
0 | 49 if (!ptr) |
50 return NULL; | |
51 memset(ptr, 0, size); | |
52 return ptr; | |
53 } | |
54 | |
55 /* encoder management */ | |
56 AVCodec *first_avcodec; | |
57 | |
58 void register_avcodec(AVCodec *format) | |
59 { | |
60 AVCodec **p; | |
61 p = &first_avcodec; | |
62 while (*p != NULL) p = &(*p)->next; | |
63 *p = format; | |
64 format->next = NULL; | |
65 } | |
66 | |
67 int avcodec_open(AVCodecContext *avctx, AVCodec *codec) | |
68 { | |
69 int ret; | |
70 | |
71 avctx->codec = codec; | |
72 avctx->frame_number = 0; | |
73 avctx->priv_data = av_mallocz(codec->priv_data_size); | |
74 if (!avctx->priv_data) | |
75 return -ENOMEM; | |
76 ret = avctx->codec->init(avctx); | |
77 if (ret < 0) { | |
78 free(avctx->priv_data); | |
79 avctx->priv_data = NULL; | |
80 return ret; | |
81 } | |
82 return 0; | |
83 } | |
84 | |
85 int avcodec_encode_audio(AVCodecContext *avctx, UINT8 *buf, int buf_size, | |
86 const short *samples) | |
87 { | |
88 int ret; | |
89 | |
90 ret = avctx->codec->encode(avctx, buf, buf_size, (void *)samples); | |
91 avctx->frame_number++; | |
92 return ret; | |
93 } | |
94 | |
95 int avcodec_encode_video(AVCodecContext *avctx, UINT8 *buf, int buf_size, | |
96 const AVPicture *pict) | |
97 { | |
98 int ret; | |
99 | |
100 ret = avctx->codec->encode(avctx, buf, buf_size, (void *)pict); | |
101 avctx->frame_number++; | |
102 return ret; | |
103 } | |
104 | |
105 /* decode a frame. return -1 if error, otherwise return the number of | |
106 bytes used. If no frame could be decompressed, *got_picture_ptr is | |
107 zero. Otherwise, it is non zero */ | |
108 int avcodec_decode_video(AVCodecContext *avctx, AVPicture *picture, | |
109 int *got_picture_ptr, | |
110 UINT8 *buf, int buf_size) | |
111 { | |
112 int ret; | |
113 | |
114 ret = avctx->codec->decode(avctx, picture, got_picture_ptr, | |
115 buf, buf_size); | |
267
e10840e4f773
- Bug fix MPEG-2 decoder to handle "repeat_first_field" (Telecine)
pulento
parents:
260
diff
changeset
|
116 if (*got_picture_ptr) |
e10840e4f773
- Bug fix MPEG-2 decoder to handle "repeat_first_field" (Telecine)
pulento
parents:
260
diff
changeset
|
117 avctx->frame_number++; |
0 | 118 return ret; |
119 } | |
120 | |
121 /* decode an audio frame. return -1 if error, otherwise return the | |
122 *number of bytes used. If no frame could be decompressed, | |
123 *frame_size_ptr is zero. Otherwise, it is the decompressed frame | |
124 *size in BYTES. */ | |
125 int avcodec_decode_audio(AVCodecContext *avctx, INT16 *samples, | |
126 int *frame_size_ptr, | |
127 UINT8 *buf, int buf_size) | |
128 { | |
129 int ret; | |
130 | |
131 ret = avctx->codec->decode(avctx, samples, frame_size_ptr, | |
132 buf, buf_size); | |
133 avctx->frame_number++; | |
134 return ret; | |
135 } | |
136 | |
137 int avcodec_close(AVCodecContext *avctx) | |
138 { | |
139 if (avctx->codec->close) | |
140 avctx->codec->close(avctx); | |
141 free(avctx->priv_data); | |
142 avctx->priv_data = NULL; | |
143 avctx->codec = NULL; | |
144 return 0; | |
145 } | |
146 | |
147 AVCodec *avcodec_find_encoder(enum CodecID id) | |
148 { | |
149 AVCodec *p; | |
150 p = first_avcodec; | |
151 while (p) { | |
152 if (p->encode != NULL && p->id == id) | |
153 return p; | |
154 p = p->next; | |
155 } | |
156 return NULL; | |
157 } | |
158 | |
177 | 159 AVCodec *avcodec_find_encoder_by_name(const char *name) |
160 { | |
161 AVCodec *p; | |
162 p = first_avcodec; | |
163 while (p) { | |
164 if (p->encode != NULL && strcmp(name,p->name) == 0) | |
165 return p; | |
166 p = p->next; | |
167 } | |
168 return NULL; | |
169 } | |
170 | |
0 | 171 AVCodec *avcodec_find_decoder(enum CodecID id) |
172 { | |
173 AVCodec *p; | |
174 p = first_avcodec; | |
175 while (p) { | |
176 if (p->decode != NULL && p->id == id) | |
177 return p; | |
178 p = p->next; | |
179 } | |
180 return NULL; | |
181 } | |
182 | |
183 AVCodec *avcodec_find_decoder_by_name(const char *name) | |
184 { | |
185 AVCodec *p; | |
186 p = first_avcodec; | |
187 while (p) { | |
188 if (p->decode != NULL && strcmp(name,p->name) == 0) | |
189 return p; | |
190 p = p->next; | |
191 } | |
192 return NULL; | |
193 } | |
194 | |
195 AVCodec *avcodec_find(enum CodecID id) | |
196 { | |
197 AVCodec *p; | |
198 p = first_avcodec; | |
199 while (p) { | |
200 if (p->id == id) | |
201 return p; | |
202 p = p->next; | |
203 } | |
204 return NULL; | |
205 } | |
206 | |
55 | 207 const char *pix_fmt_str[] = { |
315 | 208 "??", |
55 | 209 "yuv420p", |
210 "yuv422", | |
211 "rgb24", | |
212 "bgr24", | |
213 "yuv422p", | |
214 "yuv444p", | |
215 }; | |
216 | |
0 | 217 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode) |
218 { | |
219 const char *codec_name; | |
220 AVCodec *p; | |
221 char buf1[32]; | |
92 | 222 int bitrate; |
0 | 223 |
224 if (encode) | |
225 p = avcodec_find_encoder(enc->codec_id); | |
226 else | |
227 p = avcodec_find_decoder(enc->codec_id); | |
228 | |
229 if (p) { | |
230 codec_name = p->name; | |
231 } else if (enc->codec_name[0] != '\0') { | |
232 codec_name = enc->codec_name; | |
233 } else { | |
234 /* output avi tags */ | |
235 if (enc->codec_type == CODEC_TYPE_VIDEO) { | |
236 snprintf(buf1, sizeof(buf1), "%c%c%c%c", | |
237 enc->codec_tag & 0xff, | |
238 (enc->codec_tag >> 8) & 0xff, | |
239 (enc->codec_tag >> 16) & 0xff, | |
240 (enc->codec_tag >> 24) & 0xff); | |
241 } else { | |
242 snprintf(buf1, sizeof(buf1), "0x%04x", enc->codec_tag); | |
243 } | |
244 codec_name = buf1; | |
245 } | |
246 | |
247 switch(enc->codec_type) { | |
248 case CODEC_TYPE_VIDEO: | |
249 snprintf(buf, buf_size, | |
250 "Video: %s%s", | |
251 codec_name, enc->flags & CODEC_FLAG_HQ ? " (hq)" : ""); | |
55 | 252 if (enc->codec_id == CODEC_ID_RAWVIDEO) { |
253 snprintf(buf + strlen(buf), buf_size - strlen(buf), | |
254 ", %s", | |
255 pix_fmt_str[enc->pix_fmt]); | |
256 } | |
0 | 257 if (enc->width) { |
258 snprintf(buf + strlen(buf), buf_size - strlen(buf), | |
259 ", %dx%d, %0.2f fps", | |
260 enc->width, enc->height, | |
261 (float)enc->frame_rate / FRAME_RATE_BASE); | |
262 } | |
315 | 263 snprintf(buf + strlen(buf), buf_size - strlen(buf), |
264 ", q=%d-%d", enc->qmin, enc->qmax); | |
265 | |
92 | 266 bitrate = enc->bit_rate; |
0 | 267 break; |
268 case CODEC_TYPE_AUDIO: | |
269 snprintf(buf, buf_size, | |
270 "Audio: %s", | |
271 codec_name); | |
272 if (enc->sample_rate) { | |
273 snprintf(buf + strlen(buf), buf_size - strlen(buf), | |
274 ", %d Hz, %s", | |
275 enc->sample_rate, | |
276 enc->channels == 2 ? "stereo" : "mono"); | |
277 } | |
92 | 278 /* for PCM codecs, compute bitrate directly */ |
279 switch(enc->codec_id) { | |
280 case CODEC_ID_PCM_S16LE: | |
281 case CODEC_ID_PCM_S16BE: | |
282 case CODEC_ID_PCM_U16LE: | |
283 case CODEC_ID_PCM_U16BE: | |
94 | 284 bitrate = enc->sample_rate * enc->channels * 16; |
92 | 285 break; |
286 case CODEC_ID_PCM_S8: | |
287 case CODEC_ID_PCM_U8: | |
288 case CODEC_ID_PCM_ALAW: | |
289 case CODEC_ID_PCM_MULAW: | |
94 | 290 bitrate = enc->sample_rate * enc->channels * 8; |
92 | 291 break; |
292 default: | |
293 bitrate = enc->bit_rate; | |
294 break; | |
295 } | |
0 | 296 break; |
297 default: | |
298 abort(); | |
299 } | |
92 | 300 if (bitrate != 0) { |
0 | 301 snprintf(buf + strlen(buf), buf_size - strlen(buf), |
92 | 302 ", %d kb/s", bitrate / 1000); |
0 | 303 } |
304 } | |
305 | |
55 | 306 /* Picture field are filled with 'ptr' addresses */ |
307 void avpicture_fill(AVPicture *picture, UINT8 *ptr, | |
308 int pix_fmt, int width, int height) | |
309 { | |
310 int size; | |
311 | |
312 size = width * height; | |
313 switch(pix_fmt) { | |
314 case PIX_FMT_YUV420P: | |
315 picture->data[0] = ptr; | |
316 picture->data[1] = picture->data[0] + size; | |
317 picture->data[2] = picture->data[1] + size / 4; | |
318 picture->linesize[0] = width; | |
319 picture->linesize[1] = width / 2; | |
320 picture->linesize[2] = width / 2; | |
321 break; | |
322 case PIX_FMT_YUV422P: | |
323 picture->data[0] = ptr; | |
324 picture->data[1] = picture->data[0] + size; | |
325 picture->data[2] = picture->data[1] + size / 2; | |
326 picture->linesize[0] = width; | |
327 picture->linesize[1] = width / 2; | |
328 picture->linesize[2] = width / 2; | |
329 break; | |
330 case PIX_FMT_YUV444P: | |
331 picture->data[0] = ptr; | |
332 picture->data[1] = picture->data[0] + size; | |
333 picture->data[2] = picture->data[1] + size; | |
334 picture->linesize[0] = width; | |
335 picture->linesize[1] = width; | |
336 picture->linesize[2] = width; | |
337 break; | |
338 case PIX_FMT_RGB24: | |
339 case PIX_FMT_BGR24: | |
340 picture->data[0] = ptr; | |
341 picture->data[1] = NULL; | |
342 picture->data[2] = NULL; | |
343 picture->linesize[0] = width * 3; | |
344 break; | |
345 case PIX_FMT_YUV422: | |
346 picture->data[0] = ptr; | |
347 picture->data[1] = NULL; | |
348 picture->data[2] = NULL; | |
349 picture->linesize[0] = width * 2; | |
350 break; | |
351 default: | |
352 picture->data[0] = NULL; | |
353 picture->data[1] = NULL; | |
354 picture->data[2] = NULL; | |
355 break; | |
356 } | |
357 } | |
358 | |
359 int avpicture_get_size(int pix_fmt, int width, int height) | |
360 { | |
361 int size; | |
362 | |
363 size = width * height; | |
364 switch(pix_fmt) { | |
365 case PIX_FMT_YUV420P: | |
366 size = (size * 3) / 2; | |
367 break; | |
368 case PIX_FMT_YUV422P: | |
369 size = (size * 2); | |
370 break; | |
371 case PIX_FMT_YUV444P: | |
372 size = (size * 3); | |
373 break; | |
374 case PIX_FMT_RGB24: | |
375 case PIX_FMT_BGR24: | |
376 size = (size * 3); | |
377 break; | |
378 case PIX_FMT_YUV422: | |
379 size = (size * 2); | |
380 break; | |
381 default: | |
382 size = -1; | |
383 break; | |
384 } | |
385 return size; | |
386 } | |
387 | |
388 | |
0 | 389 /* must be called before any other functions */ |
390 void avcodec_init(void) | |
391 { | |
303
9a931fd8d06c
multiple init bugfix (patch by Alex Beregszaszi <alex@naxine.org>)
michaelni
parents:
267
diff
changeset
|
392 static int inited = 0; |
9a931fd8d06c
multiple init bugfix (patch by Alex Beregszaszi <alex@naxine.org>)
michaelni
parents:
267
diff
changeset
|
393 |
9a931fd8d06c
multiple init bugfix (patch by Alex Beregszaszi <alex@naxine.org>)
michaelni
parents:
267
diff
changeset
|
394 if (inited != 0) |
9a931fd8d06c
multiple init bugfix (patch by Alex Beregszaszi <alex@naxine.org>)
michaelni
parents:
267
diff
changeset
|
395 return; |
9a931fd8d06c
multiple init bugfix (patch by Alex Beregszaszi <alex@naxine.org>)
michaelni
parents:
267
diff
changeset
|
396 inited = 1; |
9a931fd8d06c
multiple init bugfix (patch by Alex Beregszaszi <alex@naxine.org>)
michaelni
parents:
267
diff
changeset
|
397 |
0 | 398 dsputil_init(); |
399 } | |
400 | |
401 /* simple call to use all the codecs */ | |
402 void avcodec_register_all(void) | |
403 { | |
303
9a931fd8d06c
multiple init bugfix (patch by Alex Beregszaszi <alex@naxine.org>)
michaelni
parents:
267
diff
changeset
|
404 static int inited = 0; |
9a931fd8d06c
multiple init bugfix (patch by Alex Beregszaszi <alex@naxine.org>)
michaelni
parents:
267
diff
changeset
|
405 |
9a931fd8d06c
multiple init bugfix (patch by Alex Beregszaszi <alex@naxine.org>)
michaelni
parents:
267
diff
changeset
|
406 if (inited != 0) |
9a931fd8d06c
multiple init bugfix (patch by Alex Beregszaszi <alex@naxine.org>)
michaelni
parents:
267
diff
changeset
|
407 return; |
9a931fd8d06c
multiple init bugfix (patch by Alex Beregszaszi <alex@naxine.org>)
michaelni
parents:
267
diff
changeset
|
408 inited = 1; |
9a931fd8d06c
multiple init bugfix (patch by Alex Beregszaszi <alex@naxine.org>)
michaelni
parents:
267
diff
changeset
|
409 |
3
1bdbd869c1f0
added CONFIG_AC3, CONFIG_MPGLIB, CONFIG_DECODERS and CONFIG_ENCODERS (Arpi: don't forget to put CONFIG_DECODERS in mplayer)
glantau
parents:
0
diff
changeset
|
410 /* encoders */ |
1bdbd869c1f0
added CONFIG_AC3, CONFIG_MPGLIB, CONFIG_DECODERS and CONFIG_ENCODERS (Arpi: don't forget to put CONFIG_DECODERS in mplayer)
glantau
parents:
0
diff
changeset
|
411 #ifdef CONFIG_ENCODERS |
0 | 412 register_avcodec(&ac3_encoder); |
413 register_avcodec(&mp2_encoder); | |
260
e1bacfb3f51f
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
177
diff
changeset
|
414 #ifdef CONFIG_MP3LAME |
e1bacfb3f51f
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
177
diff
changeset
|
415 register_avcodec(&mp3lame_encoder); |
e1bacfb3f51f
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
177
diff
changeset
|
416 #endif |
0 | 417 register_avcodec(&mpeg1video_encoder); |
418 register_avcodec(&h263_encoder); | |
419 register_avcodec(&h263p_encoder); | |
420 register_avcodec(&rv10_encoder); | |
421 register_avcodec(&mjpeg_encoder); | |
71 | 422 register_avcodec(&mpeg4_encoder); |
307 | 423 register_avcodec(&msmpeg4v1_encoder); |
424 register_avcodec(&msmpeg4v2_encoder); | |
425 register_avcodec(&msmpeg4v3_encoder); | |
3
1bdbd869c1f0
added CONFIG_AC3, CONFIG_MPGLIB, CONFIG_DECODERS and CONFIG_ENCODERS (Arpi: don't forget to put CONFIG_DECODERS in mplayer)
glantau
parents:
0
diff
changeset
|
426 #endif /* CONFIG_ENCODERS */ |
0 | 427 register_avcodec(&rawvideo_codec); |
3
1bdbd869c1f0
added CONFIG_AC3, CONFIG_MPGLIB, CONFIG_DECODERS and CONFIG_ENCODERS (Arpi: don't forget to put CONFIG_DECODERS in mplayer)
glantau
parents:
0
diff
changeset
|
428 |
0 | 429 /* decoders */ |
3
1bdbd869c1f0
added CONFIG_AC3, CONFIG_MPGLIB, CONFIG_DECODERS and CONFIG_ENCODERS (Arpi: don't forget to put CONFIG_DECODERS in mplayer)
glantau
parents:
0
diff
changeset
|
430 #ifdef CONFIG_DECODERS |
0 | 431 register_avcodec(&h263_decoder); |
71 | 432 register_avcodec(&mpeg4_decoder); |
307 | 433 register_avcodec(&msmpeg4v1_decoder); |
434 register_avcodec(&msmpeg4v2_decoder); | |
435 register_avcodec(&msmpeg4v3_decoder); | |
311 | 436 register_avcodec(&wmv1_decoder); |
0 | 437 register_avcodec(&mpeg_decoder); |
438 register_avcodec(&h263i_decoder); | |
439 register_avcodec(&rv10_decoder); | |
24 | 440 register_avcodec(&mjpeg_decoder); |
3
1bdbd869c1f0
added CONFIG_AC3, CONFIG_MPGLIB, CONFIG_DECODERS and CONFIG_ENCODERS (Arpi: don't forget to put CONFIG_DECODERS in mplayer)
glantau
parents:
0
diff
changeset
|
441 register_avcodec(&mp3_decoder); |
1bdbd869c1f0
added CONFIG_AC3, CONFIG_MPGLIB, CONFIG_DECODERS and CONFIG_ENCODERS (Arpi: don't forget to put CONFIG_DECODERS in mplayer)
glantau
parents:
0
diff
changeset
|
442 #ifdef CONFIG_AC3 |
1bdbd869c1f0
added CONFIG_AC3, CONFIG_MPGLIB, CONFIG_DECODERS and CONFIG_ENCODERS (Arpi: don't forget to put CONFIG_DECODERS in mplayer)
glantau
parents:
0
diff
changeset
|
443 register_avcodec(&ac3_decoder); |
1bdbd869c1f0
added CONFIG_AC3, CONFIG_MPGLIB, CONFIG_DECODERS and CONFIG_ENCODERS (Arpi: don't forget to put CONFIG_DECODERS in mplayer)
glantau
parents:
0
diff
changeset
|
444 #endif |
1bdbd869c1f0
added CONFIG_AC3, CONFIG_MPGLIB, CONFIG_DECODERS and CONFIG_ENCODERS (Arpi: don't forget to put CONFIG_DECODERS in mplayer)
glantau
parents:
0
diff
changeset
|
445 #endif /* CONFIG_DECODERS */ |
92 | 446 |
447 /* pcm codecs */ | |
448 | |
449 #define PCM_CODEC(id, name) \ | |
450 register_avcodec(& name ## _encoder); \ | |
451 register_avcodec(& name ## _decoder); \ | |
452 | |
453 PCM_CODEC(CODEC_ID_PCM_S16LE, pcm_s16le); | |
454 PCM_CODEC(CODEC_ID_PCM_S16BE, pcm_s16be); | |
455 PCM_CODEC(CODEC_ID_PCM_U16LE, pcm_u16le); | |
456 PCM_CODEC(CODEC_ID_PCM_U16BE, pcm_u16be); | |
457 PCM_CODEC(CODEC_ID_PCM_S8, pcm_s8); | |
458 PCM_CODEC(CODEC_ID_PCM_U8, pcm_u8); | |
459 PCM_CODEC(CODEC_ID_PCM_ALAW, pcm_alaw); | |
460 PCM_CODEC(CODEC_ID_PCM_MULAW, pcm_mulaw); | |
461 | |
462 #undef PCM_CODEC | |
0 | 463 } |
464 | |
465 static int encode_init(AVCodecContext *s) | |
466 { | |
467 return 0; | |
468 } | |
469 | |
470 static int decode_frame(AVCodecContext *avctx, | |
471 void *data, int *data_size, | |
472 UINT8 *buf, int buf_size) | |
473 { | |
474 return -1; | |
475 } | |
476 | |
477 static int encode_frame(AVCodecContext *avctx, | |
478 unsigned char *frame, int buf_size, void *data) | |
479 { | |
480 return -1; | |
481 } | |
482 | |
483 AVCodec rawvideo_codec = { | |
484 "rawvideo", | |
485 CODEC_TYPE_VIDEO, | |
486 CODEC_ID_RAWVIDEO, | |
487 0, | |
488 encode_init, | |
489 encode_frame, | |
490 NULL, | |
491 decode_frame, | |
492 }; |