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