Mercurial > libavcodec.hg
annotate utils.c @ 283:d94f9f58a2c5 libavcodec
fixing MV range in h263
author | michaelni |
---|---|
date | Sat, 23 Mar 2002 00:31:33 +0000 |
parents | e10840e4f773 |
children | 9a931fd8d06c |
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[] = { |
208 "yuv420p", | |
209 "yuv422", | |
210 "rgb24", | |
211 "bgr24", | |
212 "yuv422p", | |
213 "yuv444p", | |
214 }; | |
215 | |
0 | 216 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode) |
217 { | |
218 const char *codec_name; | |
219 AVCodec *p; | |
220 char buf1[32]; | |
92 | 221 int bitrate; |
0 | 222 |
223 if (encode) | |
224 p = avcodec_find_encoder(enc->codec_id); | |
225 else | |
226 p = avcodec_find_decoder(enc->codec_id); | |
227 | |
228 if (p) { | |
229 codec_name = p->name; | |
230 } else if (enc->codec_name[0] != '\0') { | |
231 codec_name = enc->codec_name; | |
232 } else { | |
233 /* output avi tags */ | |
234 if (enc->codec_type == CODEC_TYPE_VIDEO) { | |
235 snprintf(buf1, sizeof(buf1), "%c%c%c%c", | |
236 enc->codec_tag & 0xff, | |
237 (enc->codec_tag >> 8) & 0xff, | |
238 (enc->codec_tag >> 16) & 0xff, | |
239 (enc->codec_tag >> 24) & 0xff); | |
240 } else { | |
241 snprintf(buf1, sizeof(buf1), "0x%04x", enc->codec_tag); | |
242 } | |
243 codec_name = buf1; | |
244 } | |
245 | |
246 switch(enc->codec_type) { | |
247 case CODEC_TYPE_VIDEO: | |
248 snprintf(buf, buf_size, | |
249 "Video: %s%s", | |
250 codec_name, enc->flags & CODEC_FLAG_HQ ? " (hq)" : ""); | |
55 | 251 if (enc->codec_id == CODEC_ID_RAWVIDEO) { |
252 snprintf(buf + strlen(buf), buf_size - strlen(buf), | |
253 ", %s", | |
254 pix_fmt_str[enc->pix_fmt]); | |
255 } | |
0 | 256 if (enc->width) { |
257 snprintf(buf + strlen(buf), buf_size - strlen(buf), | |
258 ", %dx%d, %0.2f fps", | |
259 enc->width, enc->height, | |
260 (float)enc->frame_rate / FRAME_RATE_BASE); | |
261 } | |
92 | 262 bitrate = enc->bit_rate; |
0 | 263 break; |
264 case CODEC_TYPE_AUDIO: | |
265 snprintf(buf, buf_size, | |
266 "Audio: %s", | |
267 codec_name); | |
268 if (enc->sample_rate) { | |
269 snprintf(buf + strlen(buf), buf_size - strlen(buf), | |
270 ", %d Hz, %s", | |
271 enc->sample_rate, | |
272 enc->channels == 2 ? "stereo" : "mono"); | |
273 } | |
92 | 274 /* for PCM codecs, compute bitrate directly */ |
275 switch(enc->codec_id) { | |
276 case CODEC_ID_PCM_S16LE: | |
277 case CODEC_ID_PCM_S16BE: | |
278 case CODEC_ID_PCM_U16LE: | |
279 case CODEC_ID_PCM_U16BE: | |
94 | 280 bitrate = enc->sample_rate * enc->channels * 16; |
92 | 281 break; |
282 case CODEC_ID_PCM_S8: | |
283 case CODEC_ID_PCM_U8: | |
284 case CODEC_ID_PCM_ALAW: | |
285 case CODEC_ID_PCM_MULAW: | |
94 | 286 bitrate = enc->sample_rate * enc->channels * 8; |
92 | 287 break; |
288 default: | |
289 bitrate = enc->bit_rate; | |
290 break; | |
291 } | |
0 | 292 break; |
293 default: | |
294 abort(); | |
295 } | |
92 | 296 if (bitrate != 0) { |
0 | 297 snprintf(buf + strlen(buf), buf_size - strlen(buf), |
92 | 298 ", %d kb/s", bitrate / 1000); |
0 | 299 } |
300 } | |
301 | |
55 | 302 /* Picture field are filled with 'ptr' addresses */ |
303 void avpicture_fill(AVPicture *picture, UINT8 *ptr, | |
304 int pix_fmt, int width, int height) | |
305 { | |
306 int size; | |
307 | |
308 size = width * height; | |
309 switch(pix_fmt) { | |
310 case PIX_FMT_YUV420P: | |
311 picture->data[0] = ptr; | |
312 picture->data[1] = picture->data[0] + size; | |
313 picture->data[2] = picture->data[1] + size / 4; | |
314 picture->linesize[0] = width; | |
315 picture->linesize[1] = width / 2; | |
316 picture->linesize[2] = width / 2; | |
317 break; | |
318 case PIX_FMT_YUV422P: | |
319 picture->data[0] = ptr; | |
320 picture->data[1] = picture->data[0] + size; | |
321 picture->data[2] = picture->data[1] + size / 2; | |
322 picture->linesize[0] = width; | |
323 picture->linesize[1] = width / 2; | |
324 picture->linesize[2] = width / 2; | |
325 break; | |
326 case PIX_FMT_YUV444P: | |
327 picture->data[0] = ptr; | |
328 picture->data[1] = picture->data[0] + size; | |
329 picture->data[2] = picture->data[1] + size; | |
330 picture->linesize[0] = width; | |
331 picture->linesize[1] = width; | |
332 picture->linesize[2] = width; | |
333 break; | |
334 case PIX_FMT_RGB24: | |
335 case PIX_FMT_BGR24: | |
336 picture->data[0] = ptr; | |
337 picture->data[1] = NULL; | |
338 picture->data[2] = NULL; | |
339 picture->linesize[0] = width * 3; | |
340 break; | |
341 case PIX_FMT_YUV422: | |
342 picture->data[0] = ptr; | |
343 picture->data[1] = NULL; | |
344 picture->data[2] = NULL; | |
345 picture->linesize[0] = width * 2; | |
346 break; | |
347 default: | |
348 picture->data[0] = NULL; | |
349 picture->data[1] = NULL; | |
350 picture->data[2] = NULL; | |
351 break; | |
352 } | |
353 } | |
354 | |
355 int avpicture_get_size(int pix_fmt, int width, int height) | |
356 { | |
357 int size; | |
358 | |
359 size = width * height; | |
360 switch(pix_fmt) { | |
361 case PIX_FMT_YUV420P: | |
362 size = (size * 3) / 2; | |
363 break; | |
364 case PIX_FMT_YUV422P: | |
365 size = (size * 2); | |
366 break; | |
367 case PIX_FMT_YUV444P: | |
368 size = (size * 3); | |
369 break; | |
370 case PIX_FMT_RGB24: | |
371 case PIX_FMT_BGR24: | |
372 size = (size * 3); | |
373 break; | |
374 case PIX_FMT_YUV422: | |
375 size = (size * 2); | |
376 break; | |
377 default: | |
378 size = -1; | |
379 break; | |
380 } | |
381 return size; | |
382 } | |
383 | |
384 | |
0 | 385 /* must be called before any other functions */ |
386 void avcodec_init(void) | |
387 { | |
388 dsputil_init(); | |
389 } | |
390 | |
391 /* simple call to use all the codecs */ | |
392 void avcodec_register_all(void) | |
393 { | |
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 /* 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
|
395 #ifdef CONFIG_ENCODERS |
0 | 396 register_avcodec(&ac3_encoder); |
397 register_avcodec(&mp2_encoder); | |
260
e1bacfb3f51f
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
177
diff
changeset
|
398 #ifdef CONFIG_MP3LAME |
e1bacfb3f51f
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
177
diff
changeset
|
399 register_avcodec(&mp3lame_encoder); |
e1bacfb3f51f
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
177
diff
changeset
|
400 #endif |
0 | 401 register_avcodec(&mpeg1video_encoder); |
402 register_avcodec(&h263_encoder); | |
403 register_avcodec(&h263p_encoder); | |
404 register_avcodec(&rv10_encoder); | |
405 register_avcodec(&mjpeg_encoder); | |
71 | 406 register_avcodec(&mpeg4_encoder); |
0 | 407 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
|
408 #endif /* CONFIG_ENCODERS */ |
0 | 409 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
|
410 |
0 | 411 /* 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
|
412 #ifdef CONFIG_DECODERS |
0 | 413 register_avcodec(&h263_decoder); |
71 | 414 register_avcodec(&mpeg4_decoder); |
0 | 415 register_avcodec(&msmpeg4_decoder); |
416 register_avcodec(&mpeg_decoder); | |
417 register_avcodec(&h263i_decoder); | |
418 register_avcodec(&rv10_decoder); | |
24 | 419 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
|
420 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
|
421 #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
|
422 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
|
423 #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
|
424 #endif /* CONFIG_DECODERS */ |
92 | 425 |
426 /* pcm codecs */ | |
427 | |
428 #define PCM_CODEC(id, name) \ | |
429 register_avcodec(& name ## _encoder); \ | |
430 register_avcodec(& name ## _decoder); \ | |
431 | |
432 PCM_CODEC(CODEC_ID_PCM_S16LE, pcm_s16le); | |
433 PCM_CODEC(CODEC_ID_PCM_S16BE, pcm_s16be); | |
434 PCM_CODEC(CODEC_ID_PCM_U16LE, pcm_u16le); | |
435 PCM_CODEC(CODEC_ID_PCM_U16BE, pcm_u16be); | |
436 PCM_CODEC(CODEC_ID_PCM_S8, pcm_s8); | |
437 PCM_CODEC(CODEC_ID_PCM_U8, pcm_u8); | |
438 PCM_CODEC(CODEC_ID_PCM_ALAW, pcm_alaw); | |
439 PCM_CODEC(CODEC_ID_PCM_MULAW, pcm_mulaw); | |
440 | |
441 #undef PCM_CODEC | |
0 | 442 } |
443 | |
444 static int encode_init(AVCodecContext *s) | |
445 { | |
446 return 0; | |
447 } | |
448 | |
449 static int decode_frame(AVCodecContext *avctx, | |
450 void *data, int *data_size, | |
451 UINT8 *buf, int buf_size) | |
452 { | |
453 return -1; | |
454 } | |
455 | |
456 static int encode_frame(AVCodecContext *avctx, | |
457 unsigned char *frame, int buf_size, void *data) | |
458 { | |
459 return -1; | |
460 } | |
461 | |
462 AVCodec rawvideo_codec = { | |
463 "rawvideo", | |
464 CODEC_TYPE_VIDEO, | |
465 CODEC_ID_RAWVIDEO, | |
466 0, | |
467 encode_init, | |
468 encode_frame, | |
469 NULL, | |
470 decode_frame, | |
471 }; |