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