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