Mercurial > libavcodec.hg
annotate utils.c @ 550:b746a7d75ce6 libavcodec
Force inlining on get_vlc2.
author | mellum |
---|---|
date | Sat, 13 Jul 2002 19:31:15 +0000 |
parents | 9c66b5183ab3 |
children | d6955d0d7d27 |
rev | line source |
---|---|
0 | 1 /* |
2 * utils for libavcodec | |
429 | 3 * Copyright (c) 2001 Fabrice Bellard. |
0 | 4 * |
429 | 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. | |
0 | 9 * |
429 | 10 * This library is distributed in the hope that it will be useful, |
0 | 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
429 | 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 * Lesser General Public License for more details. | |
0 | 14 * |
429 | 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 | |
0 | 18 */ |
394 | 19 #include "avcodec.h" |
0 | 20 #include "dsputil.h" |
341 | 21 #include "mpegvideo.h" |
0 | 22 |
394 | 23 void *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 | |
400 | 33 /* cannot call it directly because of 'void **' casting is not automatic */ |
34 void __av_freep(void **ptr) | |
35 { | |
36 av_free(*ptr); | |
37 *ptr = NULL; | |
38 } | |
39 | |
0 | 40 /* encoder management */ |
41 AVCodec *first_avcodec; | |
42 | |
43 void 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 | |
52 int avcodec_open(AVCodecContext *avctx, AVCodec *codec) | |
53 { | |
54 int ret; | |
55 | |
56 avctx->codec = codec; | |
57 avctx->frame_number = 0; | |
374
02147e22f8c8
* Don't allocate 0 bytes of memory. It upsets electricFence!
philipjsg
parents:
362
diff
changeset
|
58 if (codec->priv_data_size > 0) { |
02147e22f8c8
* Don't allocate 0 bytes of memory. It upsets electricFence!
philipjsg
parents:
362
diff
changeset
|
59 avctx->priv_data = av_mallocz(codec->priv_data_size); |
02147e22f8c8
* Don't allocate 0 bytes of memory. It upsets electricFence!
philipjsg
parents:
362
diff
changeset
|
60 if (!avctx->priv_data) |
02147e22f8c8
* Don't allocate 0 bytes of memory. It upsets electricFence!
philipjsg
parents:
362
diff
changeset
|
61 return -ENOMEM; |
02147e22f8c8
* Don't allocate 0 bytes of memory. It upsets electricFence!
philipjsg
parents:
362
diff
changeset
|
62 } else { |
02147e22f8c8
* Don't allocate 0 bytes of memory. It upsets electricFence!
philipjsg
parents:
362
diff
changeset
|
63 avctx->priv_data = NULL; |
02147e22f8c8
* Don't allocate 0 bytes of memory. It upsets electricFence!
philipjsg
parents:
362
diff
changeset
|
64 } |
0 | 65 ret = avctx->codec->init(avctx); |
66 if (ret < 0) { | |
394 | 67 av_freep(&avctx->priv_data); |
0 | 68 return ret; |
69 } | |
70 return 0; | |
71 } | |
72 | |
73 int 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 | |
83 int 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 */ | |
96 int 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); | |
267
e10840e4f773
- Bug fix MPEG-2 decoder to handle "repeat_first_field" (Telecine)
pulento
parents:
260
diff
changeset
|
104 if (*got_picture_ptr) |
e10840e4f773
- Bug fix MPEG-2 decoder to handle "repeat_first_field" (Telecine)
pulento
parents:
260
diff
changeset
|
105 avctx->frame_number++; |
0 | 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. */ | |
113 int 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 | |
125 int avcodec_close(AVCodecContext *avctx) | |
126 { | |
127 if (avctx->codec->close) | |
128 avctx->codec->close(avctx); | |
394 | 129 av_freep(&avctx->priv_data); |
0 | 130 avctx->codec = NULL; |
131 return 0; | |
132 } | |
133 | |
134 AVCodec *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 | |
177 | 146 AVCodec *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 | |
0 | 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[] = { |
315 | 195 "??", |
55 | 196 "yuv420p", |
197 "yuv422", | |
198 "rgb24", | |
199 "bgr24", | |
200 "yuv422p", | |
201 "yuv444p", | |
521 | 202 "yuv410p" |
55 | 203 }; |
204 | |
0 | 205 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode) |
206 { | |
207 const char *codec_name; | |
208 AVCodec *p; | |
209 char buf1[32]; | |
337 | 210 char channels_str[100]; |
92 | 211 int bitrate; |
0 | 212 |
213 if (encode) | |
214 p = avcodec_find_encoder(enc->codec_id); | |
215 else | |
216 p = avcodec_find_decoder(enc->codec_id); | |
217 | |
218 if (p) { | |
219 codec_name = p->name; | |
220 } else if (enc->codec_name[0] != '\0') { | |
221 codec_name = enc->codec_name; | |
222 } else { | |
223 /* output avi tags */ | |
224 if (enc->codec_type == CODEC_TYPE_VIDEO) { | |
225 snprintf(buf1, sizeof(buf1), "%c%c%c%c", | |
226 enc->codec_tag & 0xff, | |
227 (enc->codec_tag >> 8) & 0xff, | |
228 (enc->codec_tag >> 16) & 0xff, | |
229 (enc->codec_tag >> 24) & 0xff); | |
230 } else { | |
231 snprintf(buf1, sizeof(buf1), "0x%04x", enc->codec_tag); | |
232 } | |
233 codec_name = buf1; | |
234 } | |
235 | |
236 switch(enc->codec_type) { | |
237 case CODEC_TYPE_VIDEO: | |
238 snprintf(buf, buf_size, | |
239 "Video: %s%s", | |
240 codec_name, enc->flags & CODEC_FLAG_HQ ? " (hq)" : ""); | |
55 | 241 if (enc->codec_id == CODEC_ID_RAWVIDEO) { |
242 snprintf(buf + strlen(buf), buf_size - strlen(buf), | |
243 ", %s", | |
244 pix_fmt_str[enc->pix_fmt]); | |
245 } | |
0 | 246 if (enc->width) { |
247 snprintf(buf + strlen(buf), buf_size - strlen(buf), | |
248 ", %dx%d, %0.2f fps", | |
249 enc->width, enc->height, | |
250 (float)enc->frame_rate / FRAME_RATE_BASE); | |
251 } | |
315 | 252 snprintf(buf + strlen(buf), buf_size - strlen(buf), |
253 ", q=%d-%d", enc->qmin, enc->qmax); | |
254 | |
92 | 255 bitrate = enc->bit_rate; |
0 | 256 break; |
257 case CODEC_TYPE_AUDIO: | |
258 snprintf(buf, buf_size, | |
259 "Audio: %s", | |
260 codec_name); | |
318
21697f35a9ca
- Fixed AC3 decoding for 5:1 AC3 streams. Now when calling av_audio_decode for
pulento
parents:
315
diff
changeset
|
261 switch (enc->channels) { |
21697f35a9ca
- Fixed AC3 decoding for 5:1 AC3 streams. Now when calling av_audio_decode for
pulento
parents:
315
diff
changeset
|
262 case 1: |
337 | 263 strcpy(channels_str, "mono"); |
318
21697f35a9ca
- Fixed AC3 decoding for 5:1 AC3 streams. Now when calling av_audio_decode for
pulento
parents:
315
diff
changeset
|
264 break; |
21697f35a9ca
- Fixed AC3 decoding for 5:1 AC3 streams. Now when calling av_audio_decode for
pulento
parents:
315
diff
changeset
|
265 case 2: |
337 | 266 strcpy(channels_str, "stereo"); |
318
21697f35a9ca
- Fixed AC3 decoding for 5:1 AC3 streams. Now when calling av_audio_decode for
pulento
parents:
315
diff
changeset
|
267 break; |
21697f35a9ca
- Fixed AC3 decoding for 5:1 AC3 streams. Now when calling av_audio_decode for
pulento
parents:
315
diff
changeset
|
268 case 6: |
337 | 269 strcpy(channels_str, "5:1"); |
318
21697f35a9ca
- Fixed AC3 decoding for 5:1 AC3 streams. Now when calling av_audio_decode for
pulento
parents:
315
diff
changeset
|
270 break; |
21697f35a9ca
- Fixed AC3 decoding for 5:1 AC3 streams. Now when calling av_audio_decode for
pulento
parents:
315
diff
changeset
|
271 default: |
21697f35a9ca
- Fixed AC3 decoding for 5:1 AC3 streams. Now when calling av_audio_decode for
pulento
parents:
315
diff
changeset
|
272 sprintf(channels_str, "%d channels", enc->channels); |
21697f35a9ca
- Fixed AC3 decoding for 5:1 AC3 streams. Now when calling av_audio_decode for
pulento
parents:
315
diff
changeset
|
273 break; |
21697f35a9ca
- Fixed AC3 decoding for 5:1 AC3 streams. Now when calling av_audio_decode for
pulento
parents:
315
diff
changeset
|
274 } |
0 | 275 if (enc->sample_rate) { |
276 snprintf(buf + strlen(buf), buf_size - strlen(buf), | |
277 ", %d Hz, %s", | |
278 enc->sample_rate, | |
318
21697f35a9ca
- Fixed AC3 decoding for 5:1 AC3 streams. Now when calling av_audio_decode for
pulento
parents:
315
diff
changeset
|
279 channels_str); |
0 | 280 } |
318
21697f35a9ca
- Fixed AC3 decoding for 5:1 AC3 streams. Now when calling av_audio_decode for
pulento
parents:
315
diff
changeset
|
281 |
92 | 282 /* for PCM codecs, compute bitrate directly */ |
283 switch(enc->codec_id) { | |
284 case CODEC_ID_PCM_S16LE: | |
285 case CODEC_ID_PCM_S16BE: | |
286 case CODEC_ID_PCM_U16LE: | |
287 case CODEC_ID_PCM_U16BE: | |
94 | 288 bitrate = enc->sample_rate * enc->channels * 16; |
92 | 289 break; |
290 case CODEC_ID_PCM_S8: | |
291 case CODEC_ID_PCM_U8: | |
292 case CODEC_ID_PCM_ALAW: | |
293 case CODEC_ID_PCM_MULAW: | |
94 | 294 bitrate = enc->sample_rate * enc->channels * 8; |
92 | 295 break; |
296 default: | |
297 bitrate = enc->bit_rate; | |
298 break; | |
299 } | |
0 | 300 break; |
301 default: | |
302 abort(); | |
303 } | |
92 | 304 if (bitrate != 0) { |
0 | 305 snprintf(buf + strlen(buf), buf_size - strlen(buf), |
92 | 306 ", %d kb/s", bitrate / 1000); |
0 | 307 } |
308 } | |
309 | |
55 | 310 /* Picture field are filled with 'ptr' addresses */ |
311 void avpicture_fill(AVPicture *picture, UINT8 *ptr, | |
312 int pix_fmt, int width, int height) | |
313 { | |
314 int size; | |
315 | |
316 size = width * height; | |
317 switch(pix_fmt) { | |
318 case PIX_FMT_YUV420P: | |
319 picture->data[0] = ptr; | |
320 picture->data[1] = picture->data[0] + size; | |
321 picture->data[2] = picture->data[1] + size / 4; | |
322 picture->linesize[0] = width; | |
323 picture->linesize[1] = width / 2; | |
324 picture->linesize[2] = width / 2; | |
325 break; | |
326 case PIX_FMT_YUV422P: | |
327 picture->data[0] = ptr; | |
328 picture->data[1] = picture->data[0] + size; | |
329 picture->data[2] = picture->data[1] + size / 2; | |
330 picture->linesize[0] = width; | |
331 picture->linesize[1] = width / 2; | |
332 picture->linesize[2] = width / 2; | |
333 break; | |
334 case PIX_FMT_YUV444P: | |
335 picture->data[0] = ptr; | |
336 picture->data[1] = picture->data[0] + size; | |
337 picture->data[2] = picture->data[1] + size; | |
338 picture->linesize[0] = width; | |
339 picture->linesize[1] = width; | |
340 picture->linesize[2] = width; | |
341 break; | |
342 case PIX_FMT_RGB24: | |
343 case PIX_FMT_BGR24: | |
344 picture->data[0] = ptr; | |
345 picture->data[1] = NULL; | |
346 picture->data[2] = NULL; | |
347 picture->linesize[0] = width * 3; | |
348 break; | |
349 case PIX_FMT_YUV422: | |
350 picture->data[0] = ptr; | |
351 picture->data[1] = NULL; | |
352 picture->data[2] = NULL; | |
353 picture->linesize[0] = width * 2; | |
354 break; | |
355 default: | |
356 picture->data[0] = NULL; | |
357 picture->data[1] = NULL; | |
358 picture->data[2] = NULL; | |
359 break; | |
360 } | |
361 } | |
362 | |
363 int avpicture_get_size(int pix_fmt, int width, int height) | |
364 { | |
365 int size; | |
366 | |
367 size = width * height; | |
368 switch(pix_fmt) { | |
369 case PIX_FMT_YUV420P: | |
370 size = (size * 3) / 2; | |
371 break; | |
372 case PIX_FMT_YUV422P: | |
373 size = (size * 2); | |
374 break; | |
375 case PIX_FMT_YUV444P: | |
376 size = (size * 3); | |
377 break; | |
378 case PIX_FMT_RGB24: | |
379 case PIX_FMT_BGR24: | |
380 size = (size * 3); | |
381 break; | |
382 case PIX_FMT_YUV422: | |
383 size = (size * 2); | |
384 break; | |
385 default: | |
386 size = -1; | |
387 break; | |
388 } | |
389 return size; | |
390 } | |
391 | |
362 | 392 unsigned avcodec_version( void ) |
393 { | |
394 return LIBAVCODEC_VERSION_INT; | |
395 } | |
55 | 396 |
379 | 397 unsigned avcodec_build( void ) |
398 { | |
399 return LIBAVCODEC_BUILD; | |
400 } | |
401 | |
0 | 402 /* must be called before any other functions */ |
403 void avcodec_init(void) | |
404 { | |
303
9a931fd8d06c
multiple init bugfix (patch by Alex Beregszaszi <alex@naxine.org>)
michaelni
parents:
267
diff
changeset
|
405 static int inited = 0; |
9a931fd8d06c
multiple init bugfix (patch by Alex Beregszaszi <alex@naxine.org>)
michaelni
parents:
267
diff
changeset
|
406 |
9a931fd8d06c
multiple init bugfix (patch by Alex Beregszaszi <alex@naxine.org>)
michaelni
parents:
267
diff
changeset
|
407 if (inited != 0) |
9a931fd8d06c
multiple init bugfix (patch by Alex Beregszaszi <alex@naxine.org>)
michaelni
parents:
267
diff
changeset
|
408 return; |
9a931fd8d06c
multiple init bugfix (patch by Alex Beregszaszi <alex@naxine.org>)
michaelni
parents:
267
diff
changeset
|
409 inited = 1; |
9a931fd8d06c
multiple init bugfix (patch by Alex Beregszaszi <alex@naxine.org>)
michaelni
parents:
267
diff
changeset
|
410 |
0 | 411 dsputil_init(); |
412 } | |
413 | |
341 | 414 /* this should be called after seeking and before trying to decode the next frame */ |
415 void avcodec_flush_buffers(AVCodecContext *avctx) | |
416 { | |
417 MpegEncContext *s = avctx->priv_data; | |
418 s->num_available_buffers=0; | |
419 } | |
420 | |
421 | |
440
000aeeac27a2
* started to cleanup name clashes for onetime compilation
kabi
parents:
429
diff
changeset
|
422 static int raw_encode_init(AVCodecContext *s) |
0 | 423 { |
424 return 0; | |
425 } | |
426 | |
440
000aeeac27a2
* started to cleanup name clashes for onetime compilation
kabi
parents:
429
diff
changeset
|
427 static int raw_decode_frame(AVCodecContext *avctx, |
000aeeac27a2
* started to cleanup name clashes for onetime compilation
kabi
parents:
429
diff
changeset
|
428 void *data, int *data_size, |
000aeeac27a2
* started to cleanup name clashes for onetime compilation
kabi
parents:
429
diff
changeset
|
429 UINT8 *buf, int buf_size) |
0 | 430 { |
431 return -1; | |
432 } | |
433 | |
440
000aeeac27a2
* started to cleanup name clashes for onetime compilation
kabi
parents:
429
diff
changeset
|
434 static int raw_encode_frame(AVCodecContext *avctx, |
000aeeac27a2
* started to cleanup name clashes for onetime compilation
kabi
parents:
429
diff
changeset
|
435 unsigned char *frame, int buf_size, void *data) |
0 | 436 { |
437 return -1; | |
438 } | |
439 | |
440 AVCodec rawvideo_codec = { | |
441 "rawvideo", | |
442 CODEC_TYPE_VIDEO, | |
443 CODEC_ID_RAWVIDEO, | |
444 0, | |
440
000aeeac27a2
* started to cleanup name clashes for onetime compilation
kabi
parents:
429
diff
changeset
|
445 raw_encode_init, |
000aeeac27a2
* started to cleanup name clashes for onetime compilation
kabi
parents:
429
diff
changeset
|
446 raw_encode_frame, |
0 | 447 NULL, |
440
000aeeac27a2
* started to cleanup name clashes for onetime compilation
kabi
parents:
429
diff
changeset
|
448 raw_decode_frame, |
0 | 449 }; |