Mercurial > libavcodec.hg
annotate utils.c @ 495:6f47eef8879f libavcodec
moved code to mem.c and allcodecs.c
author | bellard |
---|---|
date | Tue, 11 Jun 2002 13:45:17 +0000 |
parents | 000aeeac27a2 |
children | 9c66b5183ab3 |
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", | |
202 }; | |
203 | |
0 | 204 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode) |
205 { | |
206 const char *codec_name; | |
207 AVCodec *p; | |
208 char buf1[32]; | |
337 | 209 char channels_str[100]; |
92 | 210 int bitrate; |
0 | 211 |
212 if (encode) | |
213 p = avcodec_find_encoder(enc->codec_id); | |
214 else | |
215 p = avcodec_find_decoder(enc->codec_id); | |
216 | |
217 if (p) { | |
218 codec_name = p->name; | |
219 } else if (enc->codec_name[0] != '\0') { | |
220 codec_name = enc->codec_name; | |
221 } else { | |
222 /* output avi tags */ | |
223 if (enc->codec_type == CODEC_TYPE_VIDEO) { | |
224 snprintf(buf1, sizeof(buf1), "%c%c%c%c", | |
225 enc->codec_tag & 0xff, | |
226 (enc->codec_tag >> 8) & 0xff, | |
227 (enc->codec_tag >> 16) & 0xff, | |
228 (enc->codec_tag >> 24) & 0xff); | |
229 } else { | |
230 snprintf(buf1, sizeof(buf1), "0x%04x", enc->codec_tag); | |
231 } | |
232 codec_name = buf1; | |
233 } | |
234 | |
235 switch(enc->codec_type) { | |
236 case CODEC_TYPE_VIDEO: | |
237 snprintf(buf, buf_size, | |
238 "Video: %s%s", | |
239 codec_name, enc->flags & CODEC_FLAG_HQ ? " (hq)" : ""); | |
55 | 240 if (enc->codec_id == CODEC_ID_RAWVIDEO) { |
241 snprintf(buf + strlen(buf), buf_size - strlen(buf), | |
242 ", %s", | |
243 pix_fmt_str[enc->pix_fmt]); | |
244 } | |
0 | 245 if (enc->width) { |
246 snprintf(buf + strlen(buf), buf_size - strlen(buf), | |
247 ", %dx%d, %0.2f fps", | |
248 enc->width, enc->height, | |
249 (float)enc->frame_rate / FRAME_RATE_BASE); | |
250 } | |
315 | 251 snprintf(buf + strlen(buf), buf_size - strlen(buf), |
252 ", q=%d-%d", enc->qmin, enc->qmax); | |
253 | |
92 | 254 bitrate = enc->bit_rate; |
0 | 255 break; |
256 case CODEC_TYPE_AUDIO: | |
257 snprintf(buf, buf_size, | |
258 "Audio: %s", | |
259 codec_name); | |
318
21697f35a9ca
- Fixed AC3 decoding for 5:1 AC3 streams. Now when calling av_audio_decode for
pulento
parents:
315
diff
changeset
|
260 switch (enc->channels) { |
21697f35a9ca
- Fixed AC3 decoding for 5:1 AC3 streams. Now when calling av_audio_decode for
pulento
parents:
315
diff
changeset
|
261 case 1: |
337 | 262 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
|
263 break; |
21697f35a9ca
- Fixed AC3 decoding for 5:1 AC3 streams. Now when calling av_audio_decode for
pulento
parents:
315
diff
changeset
|
264 case 2: |
337 | 265 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
|
266 break; |
21697f35a9ca
- Fixed AC3 decoding for 5:1 AC3 streams. Now when calling av_audio_decode for
pulento
parents:
315
diff
changeset
|
267 case 6: |
337 | 268 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
|
269 break; |
21697f35a9ca
- Fixed AC3 decoding for 5:1 AC3 streams. Now when calling av_audio_decode for
pulento
parents:
315
diff
changeset
|
270 default: |
21697f35a9ca
- Fixed AC3 decoding for 5:1 AC3 streams. Now when calling av_audio_decode for
pulento
parents:
315
diff
changeset
|
271 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
|
272 break; |
21697f35a9ca
- Fixed AC3 decoding for 5:1 AC3 streams. Now when calling av_audio_decode for
pulento
parents:
315
diff
changeset
|
273 } |
0 | 274 if (enc->sample_rate) { |
275 snprintf(buf + strlen(buf), buf_size - strlen(buf), | |
276 ", %d Hz, %s", | |
277 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
|
278 channels_str); |
0 | 279 } |
318
21697f35a9ca
- Fixed AC3 decoding for 5:1 AC3 streams. Now when calling av_audio_decode for
pulento
parents:
315
diff
changeset
|
280 |
92 | 281 /* for PCM codecs, compute bitrate directly */ |
282 switch(enc->codec_id) { | |
283 case CODEC_ID_PCM_S16LE: | |
284 case CODEC_ID_PCM_S16BE: | |
285 case CODEC_ID_PCM_U16LE: | |
286 case CODEC_ID_PCM_U16BE: | |
94 | 287 bitrate = enc->sample_rate * enc->channels * 16; |
92 | 288 break; |
289 case CODEC_ID_PCM_S8: | |
290 case CODEC_ID_PCM_U8: | |
291 case CODEC_ID_PCM_ALAW: | |
292 case CODEC_ID_PCM_MULAW: | |
94 | 293 bitrate = enc->sample_rate * enc->channels * 8; |
92 | 294 break; |
295 default: | |
296 bitrate = enc->bit_rate; | |
297 break; | |
298 } | |
0 | 299 break; |
300 default: | |
301 abort(); | |
302 } | |
92 | 303 if (bitrate != 0) { |
0 | 304 snprintf(buf + strlen(buf), buf_size - strlen(buf), |
92 | 305 ", %d kb/s", bitrate / 1000); |
0 | 306 } |
307 } | |
308 | |
55 | 309 /* Picture field are filled with 'ptr' addresses */ |
310 void avpicture_fill(AVPicture *picture, UINT8 *ptr, | |
311 int pix_fmt, int width, int height) | |
312 { | |
313 int size; | |
314 | |
315 size = width * height; | |
316 switch(pix_fmt) { | |
317 case PIX_FMT_YUV420P: | |
318 picture->data[0] = ptr; | |
319 picture->data[1] = picture->data[0] + size; | |
320 picture->data[2] = picture->data[1] + size / 4; | |
321 picture->linesize[0] = width; | |
322 picture->linesize[1] = width / 2; | |
323 picture->linesize[2] = width / 2; | |
324 break; | |
325 case PIX_FMT_YUV422P: | |
326 picture->data[0] = ptr; | |
327 picture->data[1] = picture->data[0] + size; | |
328 picture->data[2] = picture->data[1] + size / 2; | |
329 picture->linesize[0] = width; | |
330 picture->linesize[1] = width / 2; | |
331 picture->linesize[2] = width / 2; | |
332 break; | |
333 case PIX_FMT_YUV444P: | |
334 picture->data[0] = ptr; | |
335 picture->data[1] = picture->data[0] + size; | |
336 picture->data[2] = picture->data[1] + size; | |
337 picture->linesize[0] = width; | |
338 picture->linesize[1] = width; | |
339 picture->linesize[2] = width; | |
340 break; | |
341 case PIX_FMT_RGB24: | |
342 case PIX_FMT_BGR24: | |
343 picture->data[0] = ptr; | |
344 picture->data[1] = NULL; | |
345 picture->data[2] = NULL; | |
346 picture->linesize[0] = width * 3; | |
347 break; | |
348 case PIX_FMT_YUV422: | |
349 picture->data[0] = ptr; | |
350 picture->data[1] = NULL; | |
351 picture->data[2] = NULL; | |
352 picture->linesize[0] = width * 2; | |
353 break; | |
354 default: | |
355 picture->data[0] = NULL; | |
356 picture->data[1] = NULL; | |
357 picture->data[2] = NULL; | |
358 break; | |
359 } | |
360 } | |
361 | |
362 int avpicture_get_size(int pix_fmt, int width, int height) | |
363 { | |
364 int size; | |
365 | |
366 size = width * height; | |
367 switch(pix_fmt) { | |
368 case PIX_FMT_YUV420P: | |
369 size = (size * 3) / 2; | |
370 break; | |
371 case PIX_FMT_YUV422P: | |
372 size = (size * 2); | |
373 break; | |
374 case PIX_FMT_YUV444P: | |
375 size = (size * 3); | |
376 break; | |
377 case PIX_FMT_RGB24: | |
378 case PIX_FMT_BGR24: | |
379 size = (size * 3); | |
380 break; | |
381 case PIX_FMT_YUV422: | |
382 size = (size * 2); | |
383 break; | |
384 default: | |
385 size = -1; | |
386 break; | |
387 } | |
388 return size; | |
389 } | |
390 | |
362 | 391 unsigned avcodec_version( void ) |
392 { | |
393 return LIBAVCODEC_VERSION_INT; | |
394 } | |
55 | 395 |
379 | 396 unsigned avcodec_build( void ) |
397 { | |
398 return LIBAVCODEC_BUILD; | |
399 } | |
400 | |
0 | 401 /* must be called before any other functions */ |
402 void avcodec_init(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 |
0 | 410 dsputil_init(); |
411 } | |
412 | |
341 | 413 /* this should be called after seeking and before trying to decode the next frame */ |
414 void avcodec_flush_buffers(AVCodecContext *avctx) | |
415 { | |
416 MpegEncContext *s = avctx->priv_data; | |
417 s->num_available_buffers=0; | |
418 } | |
419 | |
420 | |
440
000aeeac27a2
* started to cleanup name clashes for onetime compilation
kabi
parents:
429
diff
changeset
|
421 static int raw_encode_init(AVCodecContext *s) |
0 | 422 { |
423 return 0; | |
424 } | |
425 | |
440
000aeeac27a2
* started to cleanup name clashes for onetime compilation
kabi
parents:
429
diff
changeset
|
426 static int raw_decode_frame(AVCodecContext *avctx, |
000aeeac27a2
* started to cleanup name clashes for onetime compilation
kabi
parents:
429
diff
changeset
|
427 void *data, int *data_size, |
000aeeac27a2
* started to cleanup name clashes for onetime compilation
kabi
parents:
429
diff
changeset
|
428 UINT8 *buf, int buf_size) |
0 | 429 { |
430 return -1; | |
431 } | |
432 | |
440
000aeeac27a2
* started to cleanup name clashes for onetime compilation
kabi
parents:
429
diff
changeset
|
433 static int raw_encode_frame(AVCodecContext *avctx, |
000aeeac27a2
* started to cleanup name clashes for onetime compilation
kabi
parents:
429
diff
changeset
|
434 unsigned char *frame, int buf_size, void *data) |
0 | 435 { |
436 return -1; | |
437 } | |
438 | |
439 AVCodec rawvideo_codec = { | |
440 "rawvideo", | |
441 CODEC_TYPE_VIDEO, | |
442 CODEC_ID_RAWVIDEO, | |
443 0, | |
440
000aeeac27a2
* started to cleanup name clashes for onetime compilation
kabi
parents:
429
diff
changeset
|
444 raw_encode_init, |
000aeeac27a2
* started to cleanup name clashes for onetime compilation
kabi
parents:
429
diff
changeset
|
445 raw_encode_frame, |
0 | 446 NULL, |
440
000aeeac27a2
* started to cleanup name clashes for onetime compilation
kabi
parents:
429
diff
changeset
|
447 raw_decode_frame, |
0 | 448 }; |