Mercurial > libavcodec.hg
annotate utils.c @ 681:b3869ffff47a libavcodec
avcodec_alloc_context()
author | michaelni |
---|---|
date | Thu, 19 Sep 2002 09:13:43 +0000 |
parents | d6955d0d7d27 |
children | 44a1dab0205c |
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 | |
681 | 52 void avcodec_get_context_defaults(AVCodecContext *s){ |
53 s->qmin= 2; | |
54 s->qmax= 31; | |
55 s->rc_eq= "tex^qComp"; | |
56 s->qcompress= 0.5; | |
57 } | |
58 | |
59 /** | |
60 * allocates a AVCodecContext and set it to defaults. | |
61 * this can be deallocated by simply calling free() | |
62 */ | |
63 AVCodecContext *avcodec_alloc_context(){ | |
64 AVCodecContext *avctx= av_mallocz(sizeof(AVCodecContext)); | |
65 | |
66 if(avctx==NULL) return NULL; | |
67 | |
68 avcodec_get_context_defaults(avctx); | |
69 | |
70 return avctx; | |
71 } | |
72 | |
0 | 73 int avcodec_open(AVCodecContext *avctx, AVCodec *codec) |
74 { | |
75 int ret; | |
76 | |
77 avctx->codec = codec; | |
78 avctx->frame_number = 0; | |
374
02147e22f8c8
* Don't allocate 0 bytes of memory. It upsets electricFence!
philipjsg
parents:
362
diff
changeset
|
79 if (codec->priv_data_size > 0) { |
02147e22f8c8
* Don't allocate 0 bytes of memory. It upsets electricFence!
philipjsg
parents:
362
diff
changeset
|
80 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
|
81 if (!avctx->priv_data) |
02147e22f8c8
* Don't allocate 0 bytes of memory. It upsets electricFence!
philipjsg
parents:
362
diff
changeset
|
82 return -ENOMEM; |
02147e22f8c8
* Don't allocate 0 bytes of memory. It upsets electricFence!
philipjsg
parents:
362
diff
changeset
|
83 } else { |
02147e22f8c8
* Don't allocate 0 bytes of memory. It upsets electricFence!
philipjsg
parents:
362
diff
changeset
|
84 avctx->priv_data = NULL; |
02147e22f8c8
* Don't allocate 0 bytes of memory. It upsets electricFence!
philipjsg
parents:
362
diff
changeset
|
85 } |
0 | 86 ret = avctx->codec->init(avctx); |
87 if (ret < 0) { | |
394 | 88 av_freep(&avctx->priv_data); |
0 | 89 return ret; |
90 } | |
91 return 0; | |
92 } | |
93 | |
94 int avcodec_encode_audio(AVCodecContext *avctx, UINT8 *buf, int buf_size, | |
95 const short *samples) | |
96 { | |
97 int ret; | |
98 | |
99 ret = avctx->codec->encode(avctx, buf, buf_size, (void *)samples); | |
100 avctx->frame_number++; | |
101 return ret; | |
102 } | |
103 | |
104 int avcodec_encode_video(AVCodecContext *avctx, UINT8 *buf, int buf_size, | |
105 const AVPicture *pict) | |
106 { | |
107 int ret; | |
108 | |
109 ret = avctx->codec->encode(avctx, buf, buf_size, (void *)pict); | |
110 avctx->frame_number++; | |
111 return ret; | |
112 } | |
113 | |
114 /* decode a frame. return -1 if error, otherwise return the number of | |
115 bytes used. If no frame could be decompressed, *got_picture_ptr is | |
116 zero. Otherwise, it is non zero */ | |
117 int avcodec_decode_video(AVCodecContext *avctx, AVPicture *picture, | |
118 int *got_picture_ptr, | |
119 UINT8 *buf, int buf_size) | |
120 { | |
121 int ret; | |
122 | |
123 ret = avctx->codec->decode(avctx, picture, got_picture_ptr, | |
124 buf, buf_size); | |
267
e10840e4f773
- Bug fix MPEG-2 decoder to handle "repeat_first_field" (Telecine)
pulento
parents:
260
diff
changeset
|
125 if (*got_picture_ptr) |
e10840e4f773
- Bug fix MPEG-2 decoder to handle "repeat_first_field" (Telecine)
pulento
parents:
260
diff
changeset
|
126 avctx->frame_number++; |
0 | 127 return ret; |
128 } | |
129 | |
130 /* decode an audio frame. return -1 if error, otherwise return the | |
131 *number of bytes used. If no frame could be decompressed, | |
132 *frame_size_ptr is zero. Otherwise, it is the decompressed frame | |
133 *size in BYTES. */ | |
134 int avcodec_decode_audio(AVCodecContext *avctx, INT16 *samples, | |
135 int *frame_size_ptr, | |
136 UINT8 *buf, int buf_size) | |
137 { | |
138 int ret; | |
139 | |
140 ret = avctx->codec->decode(avctx, samples, frame_size_ptr, | |
141 buf, buf_size); | |
142 avctx->frame_number++; | |
143 return ret; | |
144 } | |
145 | |
146 int avcodec_close(AVCodecContext *avctx) | |
147 { | |
148 if (avctx->codec->close) | |
149 avctx->codec->close(avctx); | |
394 | 150 av_freep(&avctx->priv_data); |
0 | 151 avctx->codec = NULL; |
152 return 0; | |
153 } | |
154 | |
155 AVCodec *avcodec_find_encoder(enum CodecID id) | |
156 { | |
157 AVCodec *p; | |
158 p = first_avcodec; | |
159 while (p) { | |
160 if (p->encode != NULL && p->id == id) | |
161 return p; | |
162 p = p->next; | |
163 } | |
164 return NULL; | |
165 } | |
166 | |
177 | 167 AVCodec *avcodec_find_encoder_by_name(const char *name) |
168 { | |
169 AVCodec *p; | |
170 p = first_avcodec; | |
171 while (p) { | |
172 if (p->encode != NULL && strcmp(name,p->name) == 0) | |
173 return p; | |
174 p = p->next; | |
175 } | |
176 return NULL; | |
177 } | |
178 | |
0 | 179 AVCodec *avcodec_find_decoder(enum CodecID id) |
180 { | |
181 AVCodec *p; | |
182 p = first_avcodec; | |
183 while (p) { | |
184 if (p->decode != NULL && p->id == id) | |
185 return p; | |
186 p = p->next; | |
187 } | |
188 return NULL; | |
189 } | |
190 | |
191 AVCodec *avcodec_find_decoder_by_name(const char *name) | |
192 { | |
193 AVCodec *p; | |
194 p = first_avcodec; | |
195 while (p) { | |
196 if (p->decode != NULL && strcmp(name,p->name) == 0) | |
197 return p; | |
198 p = p->next; | |
199 } | |
200 return NULL; | |
201 } | |
202 | |
203 AVCodec *avcodec_find(enum CodecID id) | |
204 { | |
205 AVCodec *p; | |
206 p = first_avcodec; | |
207 while (p) { | |
208 if (p->id == id) | |
209 return p; | |
210 p = p->next; | |
211 } | |
212 return NULL; | |
213 } | |
214 | |
55 | 215 const char *pix_fmt_str[] = { |
315 | 216 "??", |
55 | 217 "yuv420p", |
218 "yuv422", | |
219 "rgb24", | |
220 "bgr24", | |
221 "yuv422p", | |
222 "yuv444p", | |
583 | 223 "rgba32", |
224 "bgra32", | |
521 | 225 "yuv410p" |
55 | 226 }; |
227 | |
0 | 228 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode) |
229 { | |
230 const char *codec_name; | |
231 AVCodec *p; | |
232 char buf1[32]; | |
337 | 233 char channels_str[100]; |
92 | 234 int bitrate; |
0 | 235 |
236 if (encode) | |
237 p = avcodec_find_encoder(enc->codec_id); | |
238 else | |
239 p = avcodec_find_decoder(enc->codec_id); | |
240 | |
241 if (p) { | |
242 codec_name = p->name; | |
243 } else if (enc->codec_name[0] != '\0') { | |
244 codec_name = enc->codec_name; | |
245 } else { | |
246 /* output avi tags */ | |
247 if (enc->codec_type == CODEC_TYPE_VIDEO) { | |
248 snprintf(buf1, sizeof(buf1), "%c%c%c%c", | |
249 enc->codec_tag & 0xff, | |
250 (enc->codec_tag >> 8) & 0xff, | |
251 (enc->codec_tag >> 16) & 0xff, | |
252 (enc->codec_tag >> 24) & 0xff); | |
253 } else { | |
254 snprintf(buf1, sizeof(buf1), "0x%04x", enc->codec_tag); | |
255 } | |
256 codec_name = buf1; | |
257 } | |
258 | |
259 switch(enc->codec_type) { | |
260 case CODEC_TYPE_VIDEO: | |
261 snprintf(buf, buf_size, | |
262 "Video: %s%s", | |
263 codec_name, enc->flags & CODEC_FLAG_HQ ? " (hq)" : ""); | |
55 | 264 if (enc->codec_id == CODEC_ID_RAWVIDEO) { |
265 snprintf(buf + strlen(buf), buf_size - strlen(buf), | |
266 ", %s", | |
267 pix_fmt_str[enc->pix_fmt]); | |
268 } | |
0 | 269 if (enc->width) { |
270 snprintf(buf + strlen(buf), buf_size - strlen(buf), | |
271 ", %dx%d, %0.2f fps", | |
272 enc->width, enc->height, | |
273 (float)enc->frame_rate / FRAME_RATE_BASE); | |
274 } | |
315 | 275 snprintf(buf + strlen(buf), buf_size - strlen(buf), |
276 ", q=%d-%d", enc->qmin, enc->qmax); | |
277 | |
92 | 278 bitrate = enc->bit_rate; |
0 | 279 break; |
280 case CODEC_TYPE_AUDIO: | |
281 snprintf(buf, buf_size, | |
282 "Audio: %s", | |
283 codec_name); | |
318
21697f35a9ca
- Fixed AC3 decoding for 5:1 AC3 streams. Now when calling av_audio_decode for
pulento
parents:
315
diff
changeset
|
284 switch (enc->channels) { |
21697f35a9ca
- Fixed AC3 decoding for 5:1 AC3 streams. Now when calling av_audio_decode for
pulento
parents:
315
diff
changeset
|
285 case 1: |
337 | 286 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
|
287 break; |
21697f35a9ca
- Fixed AC3 decoding for 5:1 AC3 streams. Now when calling av_audio_decode for
pulento
parents:
315
diff
changeset
|
288 case 2: |
337 | 289 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
|
290 break; |
21697f35a9ca
- Fixed AC3 decoding for 5:1 AC3 streams. Now when calling av_audio_decode for
pulento
parents:
315
diff
changeset
|
291 case 6: |
337 | 292 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
|
293 break; |
21697f35a9ca
- Fixed AC3 decoding for 5:1 AC3 streams. Now when calling av_audio_decode for
pulento
parents:
315
diff
changeset
|
294 default: |
21697f35a9ca
- Fixed AC3 decoding for 5:1 AC3 streams. Now when calling av_audio_decode for
pulento
parents:
315
diff
changeset
|
295 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
|
296 break; |
21697f35a9ca
- Fixed AC3 decoding for 5:1 AC3 streams. Now when calling av_audio_decode for
pulento
parents:
315
diff
changeset
|
297 } |
0 | 298 if (enc->sample_rate) { |
299 snprintf(buf + strlen(buf), buf_size - strlen(buf), | |
300 ", %d Hz, %s", | |
301 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
|
302 channels_str); |
0 | 303 } |
318
21697f35a9ca
- Fixed AC3 decoding for 5:1 AC3 streams. Now when calling av_audio_decode for
pulento
parents:
315
diff
changeset
|
304 |
92 | 305 /* for PCM codecs, compute bitrate directly */ |
306 switch(enc->codec_id) { | |
307 case CODEC_ID_PCM_S16LE: | |
308 case CODEC_ID_PCM_S16BE: | |
309 case CODEC_ID_PCM_U16LE: | |
310 case CODEC_ID_PCM_U16BE: | |
94 | 311 bitrate = enc->sample_rate * enc->channels * 16; |
92 | 312 break; |
313 case CODEC_ID_PCM_S8: | |
314 case CODEC_ID_PCM_U8: | |
315 case CODEC_ID_PCM_ALAW: | |
316 case CODEC_ID_PCM_MULAW: | |
94 | 317 bitrate = enc->sample_rate * enc->channels * 8; |
92 | 318 break; |
319 default: | |
320 bitrate = enc->bit_rate; | |
321 break; | |
322 } | |
0 | 323 break; |
324 default: | |
583 | 325 av_abort(); |
0 | 326 } |
92 | 327 if (bitrate != 0) { |
0 | 328 snprintf(buf + strlen(buf), buf_size - strlen(buf), |
92 | 329 ", %d kb/s", bitrate / 1000); |
0 | 330 } |
331 } | |
332 | |
55 | 333 /* Picture field are filled with 'ptr' addresses */ |
334 void avpicture_fill(AVPicture *picture, UINT8 *ptr, | |
335 int pix_fmt, int width, int height) | |
336 { | |
337 int size; | |
338 | |
339 size = width * height; | |
340 switch(pix_fmt) { | |
341 case PIX_FMT_YUV420P: | |
342 picture->data[0] = ptr; | |
343 picture->data[1] = picture->data[0] + size; | |
344 picture->data[2] = picture->data[1] + size / 4; | |
345 picture->linesize[0] = width; | |
346 picture->linesize[1] = width / 2; | |
347 picture->linesize[2] = width / 2; | |
348 break; | |
349 case PIX_FMT_YUV422P: | |
350 picture->data[0] = ptr; | |
351 picture->data[1] = picture->data[0] + size; | |
352 picture->data[2] = picture->data[1] + size / 2; | |
353 picture->linesize[0] = width; | |
354 picture->linesize[1] = width / 2; | |
355 picture->linesize[2] = width / 2; | |
356 break; | |
357 case PIX_FMT_YUV444P: | |
358 picture->data[0] = ptr; | |
359 picture->data[1] = picture->data[0] + size; | |
360 picture->data[2] = picture->data[1] + size; | |
361 picture->linesize[0] = width; | |
362 picture->linesize[1] = width; | |
363 picture->linesize[2] = width; | |
364 break; | |
365 case PIX_FMT_RGB24: | |
366 case PIX_FMT_BGR24: | |
367 picture->data[0] = ptr; | |
368 picture->data[1] = NULL; | |
369 picture->data[2] = NULL; | |
370 picture->linesize[0] = width * 3; | |
371 break; | |
583 | 372 case PIX_FMT_RGBA32: |
373 case PIX_FMT_BGRA32: | |
374 picture->data[0] = ptr; | |
375 picture->data[1] = NULL; | |
376 picture->data[2] = NULL; | |
377 picture->linesize[0] = width * 4; | |
378 break; | |
55 | 379 case PIX_FMT_YUV422: |
380 picture->data[0] = ptr; | |
381 picture->data[1] = NULL; | |
382 picture->data[2] = NULL; | |
383 picture->linesize[0] = width * 2; | |
384 break; | |
385 default: | |
386 picture->data[0] = NULL; | |
387 picture->data[1] = NULL; | |
388 picture->data[2] = NULL; | |
389 break; | |
390 } | |
391 } | |
392 | |
393 int avpicture_get_size(int pix_fmt, int width, int height) | |
394 { | |
395 int size; | |
396 | |
397 size = width * height; | |
398 switch(pix_fmt) { | |
399 case PIX_FMT_YUV420P: | |
400 size = (size * 3) / 2; | |
401 break; | |
402 case PIX_FMT_YUV422P: | |
403 size = (size * 2); | |
404 break; | |
405 case PIX_FMT_YUV444P: | |
406 size = (size * 3); | |
407 break; | |
408 case PIX_FMT_RGB24: | |
409 case PIX_FMT_BGR24: | |
410 size = (size * 3); | |
411 break; | |
583 | 412 case PIX_FMT_RGBA32: |
413 case PIX_FMT_BGRA32: | |
414 size = (size * 4); | |
415 break; | |
55 | 416 case PIX_FMT_YUV422: |
417 size = (size * 2); | |
418 break; | |
419 default: | |
420 size = -1; | |
421 break; | |
422 } | |
423 return size; | |
424 } | |
425 | |
362 | 426 unsigned avcodec_version( void ) |
427 { | |
428 return LIBAVCODEC_VERSION_INT; | |
429 } | |
55 | 430 |
379 | 431 unsigned avcodec_build( void ) |
432 { | |
433 return LIBAVCODEC_BUILD; | |
434 } | |
435 | |
0 | 436 /* must be called before any other functions */ |
437 void avcodec_init(void) | |
438 { | |
303
9a931fd8d06c
multiple init bugfix (patch by Alex Beregszaszi <alex@naxine.org>)
michaelni
parents:
267
diff
changeset
|
439 static int inited = 0; |
9a931fd8d06c
multiple init bugfix (patch by Alex Beregszaszi <alex@naxine.org>)
michaelni
parents:
267
diff
changeset
|
440 |
9a931fd8d06c
multiple init bugfix (patch by Alex Beregszaszi <alex@naxine.org>)
michaelni
parents:
267
diff
changeset
|
441 if (inited != 0) |
9a931fd8d06c
multiple init bugfix (patch by Alex Beregszaszi <alex@naxine.org>)
michaelni
parents:
267
diff
changeset
|
442 return; |
9a931fd8d06c
multiple init bugfix (patch by Alex Beregszaszi <alex@naxine.org>)
michaelni
parents:
267
diff
changeset
|
443 inited = 1; |
9a931fd8d06c
multiple init bugfix (patch by Alex Beregszaszi <alex@naxine.org>)
michaelni
parents:
267
diff
changeset
|
444 |
0 | 445 dsputil_init(); |
446 } | |
447 | |
341 | 448 /* this should be called after seeking and before trying to decode the next frame */ |
449 void avcodec_flush_buffers(AVCodecContext *avctx) | |
450 { | |
451 MpegEncContext *s = avctx->priv_data; | |
452 s->num_available_buffers=0; | |
453 } | |
454 | |
455 | |
440
000aeeac27a2
* started to cleanup name clashes for onetime compilation
kabi
parents:
429
diff
changeset
|
456 static int raw_encode_init(AVCodecContext *s) |
0 | 457 { |
458 return 0; | |
459 } | |
460 | |
440
000aeeac27a2
* started to cleanup name clashes for onetime compilation
kabi
parents:
429
diff
changeset
|
461 static int raw_decode_frame(AVCodecContext *avctx, |
000aeeac27a2
* started to cleanup name clashes for onetime compilation
kabi
parents:
429
diff
changeset
|
462 void *data, int *data_size, |
000aeeac27a2
* started to cleanup name clashes for onetime compilation
kabi
parents:
429
diff
changeset
|
463 UINT8 *buf, int buf_size) |
0 | 464 { |
465 return -1; | |
466 } | |
467 | |
440
000aeeac27a2
* started to cleanup name clashes for onetime compilation
kabi
parents:
429
diff
changeset
|
468 static int raw_encode_frame(AVCodecContext *avctx, |
000aeeac27a2
* started to cleanup name clashes for onetime compilation
kabi
parents:
429
diff
changeset
|
469 unsigned char *frame, int buf_size, void *data) |
0 | 470 { |
471 return -1; | |
472 } | |
473 | |
474 AVCodec rawvideo_codec = { | |
475 "rawvideo", | |
476 CODEC_TYPE_VIDEO, | |
477 CODEC_ID_RAWVIDEO, | |
478 0, | |
440
000aeeac27a2
* started to cleanup name clashes for onetime compilation
kabi
parents:
429
diff
changeset
|
479 raw_encode_init, |
000aeeac27a2
* started to cleanup name clashes for onetime compilation
kabi
parents:
429
diff
changeset
|
480 raw_encode_frame, |
0 | 481 NULL, |
440
000aeeac27a2
* started to cleanup name clashes for onetime compilation
kabi
parents:
429
diff
changeset
|
482 raw_decode_frame, |
0 | 483 }; |