Mercurial > libavcodec.hg
annotate utils.c @ 122:56e34b70ef5d libavcodec
* prevent crash in decoder
author | kabi |
---|---|
date | Tue, 23 Oct 2001 19:02:55 +0000 |
parents | 7e263a256a6f |
children | 53da914d6f46 |
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); | |
116 avctx->frame_number++; | |
117 return ret; | |
118 } | |
119 | |
120 /* decode an audio frame. return -1 if error, otherwise return the | |
121 *number of bytes used. If no frame could be decompressed, | |
122 *frame_size_ptr is zero. Otherwise, it is the decompressed frame | |
123 *size in BYTES. */ | |
124 int avcodec_decode_audio(AVCodecContext *avctx, INT16 *samples, | |
125 int *frame_size_ptr, | |
126 UINT8 *buf, int buf_size) | |
127 { | |
128 int ret; | |
129 | |
130 ret = avctx->codec->decode(avctx, samples, frame_size_ptr, | |
131 buf, buf_size); | |
132 avctx->frame_number++; | |
133 return ret; | |
134 } | |
135 | |
136 int avcodec_close(AVCodecContext *avctx) | |
137 { | |
138 if (avctx->codec->close) | |
139 avctx->codec->close(avctx); | |
140 free(avctx->priv_data); | |
141 avctx->priv_data = NULL; | |
142 avctx->codec = NULL; | |
143 return 0; | |
144 } | |
145 | |
146 AVCodec *avcodec_find_encoder(enum CodecID id) | |
147 { | |
148 AVCodec *p; | |
149 p = first_avcodec; | |
150 while (p) { | |
151 if (p->encode != NULL && p->id == id) | |
152 return p; | |
153 p = p->next; | |
154 } | |
155 return NULL; | |
156 } | |
157 | |
158 AVCodec *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 | |
170 AVCodec *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 | |
182 AVCodec *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 | |
55 | 194 const char *pix_fmt_str[] = { |
195 "yuv420p", | |
196 "yuv422", | |
197 "rgb24", | |
198 "bgr24", | |
199 "yuv422p", | |
200 "yuv444p", | |
201 }; | |
202 | |
0 | 203 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode) |
204 { | |
205 const char *codec_name; | |
206 AVCodec *p; | |
207 char buf1[32]; | |
92 | 208 int bitrate; |
0 | 209 |
210 if (encode) | |
211 p = avcodec_find_encoder(enc->codec_id); | |
212 else | |
213 p = avcodec_find_decoder(enc->codec_id); | |
214 | |
215 if (p) { | |
216 codec_name = p->name; | |
217 } else if (enc->codec_name[0] != '\0') { | |
218 codec_name = enc->codec_name; | |
219 } else { | |
220 /* output avi tags */ | |
221 if (enc->codec_type == CODEC_TYPE_VIDEO) { | |
222 snprintf(buf1, sizeof(buf1), "%c%c%c%c", | |
223 enc->codec_tag & 0xff, | |
224 (enc->codec_tag >> 8) & 0xff, | |
225 (enc->codec_tag >> 16) & 0xff, | |
226 (enc->codec_tag >> 24) & 0xff); | |
227 } else { | |
228 snprintf(buf1, sizeof(buf1), "0x%04x", enc->codec_tag); | |
229 } | |
230 codec_name = buf1; | |
231 } | |
232 | |
233 switch(enc->codec_type) { | |
234 case CODEC_TYPE_VIDEO: | |
235 snprintf(buf, buf_size, | |
236 "Video: %s%s", | |
237 codec_name, enc->flags & CODEC_FLAG_HQ ? " (hq)" : ""); | |
55 | 238 if (enc->codec_id == CODEC_ID_RAWVIDEO) { |
239 snprintf(buf + strlen(buf), buf_size - strlen(buf), | |
240 ", %s", | |
241 pix_fmt_str[enc->pix_fmt]); | |
242 } | |
0 | 243 if (enc->width) { |
244 snprintf(buf + strlen(buf), buf_size - strlen(buf), | |
245 ", %dx%d, %0.2f fps", | |
246 enc->width, enc->height, | |
247 (float)enc->frame_rate / FRAME_RATE_BASE); | |
248 } | |
92 | 249 bitrate = enc->bit_rate; |
0 | 250 break; |
251 case CODEC_TYPE_AUDIO: | |
252 snprintf(buf, buf_size, | |
253 "Audio: %s", | |
254 codec_name); | |
255 if (enc->sample_rate) { | |
256 snprintf(buf + strlen(buf), buf_size - strlen(buf), | |
257 ", %d Hz, %s", | |
258 enc->sample_rate, | |
259 enc->channels == 2 ? "stereo" : "mono"); | |
260 } | |
92 | 261 /* for PCM codecs, compute bitrate directly */ |
262 switch(enc->codec_id) { | |
263 case CODEC_ID_PCM_S16LE: | |
264 case CODEC_ID_PCM_S16BE: | |
265 case CODEC_ID_PCM_U16LE: | |
266 case CODEC_ID_PCM_U16BE: | |
94 | 267 bitrate = enc->sample_rate * enc->channels * 16; |
92 | 268 break; |
269 case CODEC_ID_PCM_S8: | |
270 case CODEC_ID_PCM_U8: | |
271 case CODEC_ID_PCM_ALAW: | |
272 case CODEC_ID_PCM_MULAW: | |
94 | 273 bitrate = enc->sample_rate * enc->channels * 8; |
92 | 274 break; |
275 default: | |
276 bitrate = enc->bit_rate; | |
277 break; | |
278 } | |
0 | 279 break; |
280 default: | |
281 abort(); | |
282 } | |
92 | 283 if (bitrate != 0) { |
0 | 284 snprintf(buf + strlen(buf), buf_size - strlen(buf), |
92 | 285 ", %d kb/s", bitrate / 1000); |
0 | 286 } |
287 } | |
288 | |
55 | 289 /* Picture field are filled with 'ptr' addresses */ |
290 void avpicture_fill(AVPicture *picture, UINT8 *ptr, | |
291 int pix_fmt, int width, int height) | |
292 { | |
293 int size; | |
294 | |
295 size = width * height; | |
296 switch(pix_fmt) { | |
297 case PIX_FMT_YUV420P: | |
298 picture->data[0] = ptr; | |
299 picture->data[1] = picture->data[0] + size; | |
300 picture->data[2] = picture->data[1] + size / 4; | |
301 picture->linesize[0] = width; | |
302 picture->linesize[1] = width / 2; | |
303 picture->linesize[2] = width / 2; | |
304 break; | |
305 case PIX_FMT_YUV422P: | |
306 picture->data[0] = ptr; | |
307 picture->data[1] = picture->data[0] + size; | |
308 picture->data[2] = picture->data[1] + size / 2; | |
309 picture->linesize[0] = width; | |
310 picture->linesize[1] = width / 2; | |
311 picture->linesize[2] = width / 2; | |
312 break; | |
313 case PIX_FMT_YUV444P: | |
314 picture->data[0] = ptr; | |
315 picture->data[1] = picture->data[0] + size; | |
316 picture->data[2] = picture->data[1] + size; | |
317 picture->linesize[0] = width; | |
318 picture->linesize[1] = width; | |
319 picture->linesize[2] = width; | |
320 break; | |
321 case PIX_FMT_RGB24: | |
322 case PIX_FMT_BGR24: | |
323 picture->data[0] = ptr; | |
324 picture->data[1] = NULL; | |
325 picture->data[2] = NULL; | |
326 picture->linesize[0] = width * 3; | |
327 break; | |
328 case PIX_FMT_YUV422: | |
329 picture->data[0] = ptr; | |
330 picture->data[1] = NULL; | |
331 picture->data[2] = NULL; | |
332 picture->linesize[0] = width * 2; | |
333 break; | |
334 default: | |
335 picture->data[0] = NULL; | |
336 picture->data[1] = NULL; | |
337 picture->data[2] = NULL; | |
338 break; | |
339 } | |
340 } | |
341 | |
342 int avpicture_get_size(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 size = (size * 3) / 2; | |
350 break; | |
351 case PIX_FMT_YUV422P: | |
352 size = (size * 2); | |
353 break; | |
354 case PIX_FMT_YUV444P: | |
355 size = (size * 3); | |
356 break; | |
357 case PIX_FMT_RGB24: | |
358 case PIX_FMT_BGR24: | |
359 size = (size * 3); | |
360 break; | |
361 case PIX_FMT_YUV422: | |
362 size = (size * 2); | |
363 break; | |
364 default: | |
365 size = -1; | |
366 break; | |
367 } | |
368 return size; | |
369 } | |
370 | |
371 | |
0 | 372 /* must be called before any other functions */ |
373 void avcodec_init(void) | |
374 { | |
375 dsputil_init(); | |
376 } | |
377 | |
378 /* simple call to use all the codecs */ | |
379 void avcodec_register_all(void) | |
380 { | |
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
|
381 /* 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
|
382 #ifdef CONFIG_ENCODERS |
0 | 383 register_avcodec(&ac3_encoder); |
384 register_avcodec(&mp2_encoder); | |
385 register_avcodec(&mpeg1video_encoder); | |
386 register_avcodec(&h263_encoder); | |
387 register_avcodec(&h263p_encoder); | |
388 register_avcodec(&rv10_encoder); | |
389 register_avcodec(&mjpeg_encoder); | |
71 | 390 register_avcodec(&mpeg4_encoder); |
0 | 391 register_avcodec(&msmpeg4_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
|
392 #endif /* CONFIG_ENCODERS */ |
0 | 393 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
|
394 |
0 | 395 /* 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
|
396 #ifdef CONFIG_DECODERS |
0 | 397 register_avcodec(&h263_decoder); |
71 | 398 register_avcodec(&mpeg4_decoder); |
0 | 399 register_avcodec(&msmpeg4_decoder); |
400 register_avcodec(&mpeg_decoder); | |
401 register_avcodec(&h263i_decoder); | |
402 register_avcodec(&rv10_decoder); | |
24 | 403 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
|
404 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
|
405 #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
|
406 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
|
407 #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
|
408 #endif /* CONFIG_DECODERS */ |
92 | 409 |
410 /* pcm codecs */ | |
411 | |
412 #define PCM_CODEC(id, name) \ | |
413 register_avcodec(& name ## _encoder); \ | |
414 register_avcodec(& name ## _decoder); \ | |
415 | |
416 PCM_CODEC(CODEC_ID_PCM_S16LE, pcm_s16le); | |
417 PCM_CODEC(CODEC_ID_PCM_S16BE, pcm_s16be); | |
418 PCM_CODEC(CODEC_ID_PCM_U16LE, pcm_u16le); | |
419 PCM_CODEC(CODEC_ID_PCM_U16BE, pcm_u16be); | |
420 PCM_CODEC(CODEC_ID_PCM_S8, pcm_s8); | |
421 PCM_CODEC(CODEC_ID_PCM_U8, pcm_u8); | |
422 PCM_CODEC(CODEC_ID_PCM_ALAW, pcm_alaw); | |
423 PCM_CODEC(CODEC_ID_PCM_MULAW, pcm_mulaw); | |
424 | |
425 #undef PCM_CODEC | |
0 | 426 } |
427 | |
428 static int encode_init(AVCodecContext *s) | |
429 { | |
430 return 0; | |
431 } | |
432 | |
433 static int decode_frame(AVCodecContext *avctx, | |
434 void *data, int *data_size, | |
435 UINT8 *buf, int buf_size) | |
436 { | |
437 return -1; | |
438 } | |
439 | |
440 static int encode_frame(AVCodecContext *avctx, | |
441 unsigned char *frame, int buf_size, void *data) | |
442 { | |
443 return -1; | |
444 } | |
445 | |
446 AVCodec rawvideo_codec = { | |
447 "rawvideo", | |
448 CODEC_TYPE_VIDEO, | |
449 CODEC_ID_RAWVIDEO, | |
450 0, | |
451 encode_init, | |
452 encode_frame, | |
453 NULL, | |
454 decode_frame, | |
455 }; |