Mercurial > libavcodec.hg
annotate utils.c @ 55:6064342168f4 libavcodec
picture utils
author | glantau |
---|---|
date | Sat, 11 Aug 2001 19:04:31 +0000 |
parents | 08265a63313e |
children | 79be2c581c01 |
rev | line source |
---|---|
0 | 1 /* |
2 * utils for libavcodec | |
3 * Copyright (c) 2001 Gerard Lantau. | |
4 * | |
5 * This program is free software; you can redistribute it and/or modify | |
6 * it under the terms of the GNU General Public License as published by | |
7 * the Free Software Foundation; either version 2 of the License, or | |
8 * (at your option) any later version. | |
9 * | |
10 * This program is distributed in the hope that it will be useful, | |
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 * GNU General Public License for more details. | |
14 * | |
15 * You should have received a copy of the GNU General Public License | |
16 * along with this program; if not, write to the Free Software | |
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
18 */ | |
19 #include <stdlib.h> | |
20 #include <stdio.h> | |
21 #include <string.h> | |
22 #include <errno.h> | |
23 #include "common.h" | |
24 #include "dsputil.h" | |
25 #include "avcodec.h" | |
26 | |
27 /* memory alloc */ | |
28 void *av_mallocz(int size) | |
29 { | |
30 void *ptr; | |
31 ptr = malloc(size); | |
32 if (!ptr) | |
33 return NULL; | |
34 memset(ptr, 0, size); | |
35 return ptr; | |
36 } | |
37 | |
38 /* encoder management */ | |
39 AVCodec *first_avcodec; | |
40 | |
41 void register_avcodec(AVCodec *format) | |
42 { | |
43 AVCodec **p; | |
44 p = &first_avcodec; | |
45 while (*p != NULL) p = &(*p)->next; | |
46 *p = format; | |
47 format->next = NULL; | |
48 } | |
49 | |
50 int avcodec_open(AVCodecContext *avctx, AVCodec *codec) | |
51 { | |
52 int ret; | |
53 | |
54 avctx->codec = codec; | |
55 avctx->frame_number = 0; | |
56 avctx->priv_data = av_mallocz(codec->priv_data_size); | |
57 if (!avctx->priv_data) | |
58 return -ENOMEM; | |
59 ret = avctx->codec->init(avctx); | |
60 if (ret < 0) { | |
61 free(avctx->priv_data); | |
62 avctx->priv_data = NULL; | |
63 return ret; | |
64 } | |
65 return 0; | |
66 } | |
67 | |
68 int avcodec_encode_audio(AVCodecContext *avctx, UINT8 *buf, int buf_size, | |
69 const short *samples) | |
70 { | |
71 int ret; | |
72 | |
73 ret = avctx->codec->encode(avctx, buf, buf_size, (void *)samples); | |
74 avctx->frame_number++; | |
75 return ret; | |
76 } | |
77 | |
78 int avcodec_encode_video(AVCodecContext *avctx, UINT8 *buf, int buf_size, | |
79 const AVPicture *pict) | |
80 { | |
81 int ret; | |
82 | |
83 ret = avctx->codec->encode(avctx, buf, buf_size, (void *)pict); | |
84 avctx->frame_number++; | |
85 return ret; | |
86 } | |
87 | |
88 /* decode a frame. return -1 if error, otherwise return the number of | |
89 bytes used. If no frame could be decompressed, *got_picture_ptr is | |
90 zero. Otherwise, it is non zero */ | |
91 int avcodec_decode_video(AVCodecContext *avctx, AVPicture *picture, | |
92 int *got_picture_ptr, | |
93 UINT8 *buf, int buf_size) | |
94 { | |
95 int ret; | |
96 | |
97 ret = avctx->codec->decode(avctx, picture, got_picture_ptr, | |
98 buf, buf_size); | |
99 avctx->frame_number++; | |
100 return ret; | |
101 } | |
102 | |
103 /* decode an audio frame. return -1 if error, otherwise return the | |
104 *number of bytes used. If no frame could be decompressed, | |
105 *frame_size_ptr is zero. Otherwise, it is the decompressed frame | |
106 *size in BYTES. */ | |
107 int avcodec_decode_audio(AVCodecContext *avctx, INT16 *samples, | |
108 int *frame_size_ptr, | |
109 UINT8 *buf, int buf_size) | |
110 { | |
111 int ret; | |
112 | |
113 ret = avctx->codec->decode(avctx, samples, frame_size_ptr, | |
114 buf, buf_size); | |
115 avctx->frame_number++; | |
116 return ret; | |
117 } | |
118 | |
119 int avcodec_close(AVCodecContext *avctx) | |
120 { | |
121 if (avctx->codec->close) | |
122 avctx->codec->close(avctx); | |
123 free(avctx->priv_data); | |
124 avctx->priv_data = NULL; | |
125 avctx->codec = NULL; | |
126 return 0; | |
127 } | |
128 | |
129 AVCodec *avcodec_find_encoder(enum CodecID id) | |
130 { | |
131 AVCodec *p; | |
132 p = first_avcodec; | |
133 while (p) { | |
134 if (p->encode != NULL && p->id == id) | |
135 return p; | |
136 p = p->next; | |
137 } | |
138 return NULL; | |
139 } | |
140 | |
141 AVCodec *avcodec_find_decoder(enum CodecID id) | |
142 { | |
143 AVCodec *p; | |
144 p = first_avcodec; | |
145 while (p) { | |
146 if (p->decode != NULL && p->id == id) | |
147 return p; | |
148 p = p->next; | |
149 } | |
150 return NULL; | |
151 } | |
152 | |
153 AVCodec *avcodec_find_decoder_by_name(const char *name) | |
154 { | |
155 AVCodec *p; | |
156 p = first_avcodec; | |
157 while (p) { | |
158 if (p->decode != NULL && strcmp(name,p->name) == 0) | |
159 return p; | |
160 p = p->next; | |
161 } | |
162 return NULL; | |
163 } | |
164 | |
165 AVCodec *avcodec_find(enum CodecID id) | |
166 { | |
167 AVCodec *p; | |
168 p = first_avcodec; | |
169 while (p) { | |
170 if (p->id == id) | |
171 return p; | |
172 p = p->next; | |
173 } | |
174 return NULL; | |
175 } | |
176 | |
55 | 177 const char *pix_fmt_str[] = { |
178 "yuv420p", | |
179 "yuv422", | |
180 "rgb24", | |
181 "bgr24", | |
182 "yuv422p", | |
183 "yuv444p", | |
184 }; | |
185 | |
0 | 186 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode) |
187 { | |
188 const char *codec_name; | |
189 AVCodec *p; | |
190 char buf1[32]; | |
191 | |
192 if (encode) | |
193 p = avcodec_find_encoder(enc->codec_id); | |
194 else | |
195 p = avcodec_find_decoder(enc->codec_id); | |
196 | |
197 if (p) { | |
198 codec_name = p->name; | |
199 } else if (enc->codec_name[0] != '\0') { | |
200 codec_name = enc->codec_name; | |
201 } else { | |
202 /* output avi tags */ | |
203 if (enc->codec_type == CODEC_TYPE_VIDEO) { | |
204 snprintf(buf1, sizeof(buf1), "%c%c%c%c", | |
205 enc->codec_tag & 0xff, | |
206 (enc->codec_tag >> 8) & 0xff, | |
207 (enc->codec_tag >> 16) & 0xff, | |
208 (enc->codec_tag >> 24) & 0xff); | |
209 } else { | |
210 snprintf(buf1, sizeof(buf1), "0x%04x", enc->codec_tag); | |
211 } | |
212 codec_name = buf1; | |
213 } | |
214 | |
215 switch(enc->codec_type) { | |
216 case CODEC_TYPE_VIDEO: | |
217 snprintf(buf, buf_size, | |
218 "Video: %s%s", | |
219 codec_name, enc->flags & CODEC_FLAG_HQ ? " (hq)" : ""); | |
55 | 220 if (enc->codec_id == CODEC_ID_RAWVIDEO) { |
221 snprintf(buf + strlen(buf), buf_size - strlen(buf), | |
222 ", %s", | |
223 pix_fmt_str[enc->pix_fmt]); | |
224 } | |
0 | 225 if (enc->width) { |
226 snprintf(buf + strlen(buf), buf_size - strlen(buf), | |
227 ", %dx%d, %0.2f fps", | |
228 enc->width, enc->height, | |
229 (float)enc->frame_rate / FRAME_RATE_BASE); | |
230 } | |
231 break; | |
232 case CODEC_TYPE_AUDIO: | |
233 snprintf(buf, buf_size, | |
234 "Audio: %s", | |
235 codec_name); | |
236 if (enc->sample_rate) { | |
237 snprintf(buf + strlen(buf), buf_size - strlen(buf), | |
238 ", %d Hz, %s", | |
239 enc->sample_rate, | |
240 enc->channels == 2 ? "stereo" : "mono"); | |
241 } | |
242 break; | |
243 default: | |
244 abort(); | |
245 } | |
246 if (enc->bit_rate != 0) { | |
247 snprintf(buf + strlen(buf), buf_size - strlen(buf), | |
248 ", %d kb/s", enc->bit_rate / 1000); | |
249 } | |
250 } | |
251 | |
55 | 252 /* Picture field are filled with 'ptr' addresses */ |
253 void avpicture_fill(AVPicture *picture, UINT8 *ptr, | |
254 int pix_fmt, int width, int height) | |
255 { | |
256 int size; | |
257 | |
258 size = width * height; | |
259 switch(pix_fmt) { | |
260 case PIX_FMT_YUV420P: | |
261 picture->data[0] = ptr; | |
262 picture->data[1] = picture->data[0] + size; | |
263 picture->data[2] = picture->data[1] + size / 4; | |
264 picture->linesize[0] = width; | |
265 picture->linesize[1] = width / 2; | |
266 picture->linesize[2] = width / 2; | |
267 break; | |
268 case PIX_FMT_YUV422P: | |
269 picture->data[0] = ptr; | |
270 picture->data[1] = picture->data[0] + size; | |
271 picture->data[2] = picture->data[1] + size / 2; | |
272 picture->linesize[0] = width; | |
273 picture->linesize[1] = width / 2; | |
274 picture->linesize[2] = width / 2; | |
275 break; | |
276 case PIX_FMT_YUV444P: | |
277 picture->data[0] = ptr; | |
278 picture->data[1] = picture->data[0] + size; | |
279 picture->data[2] = picture->data[1] + size; | |
280 picture->linesize[0] = width; | |
281 picture->linesize[1] = width; | |
282 picture->linesize[2] = width; | |
283 break; | |
284 case PIX_FMT_RGB24: | |
285 case PIX_FMT_BGR24: | |
286 picture->data[0] = ptr; | |
287 picture->data[1] = NULL; | |
288 picture->data[2] = NULL; | |
289 picture->linesize[0] = width * 3; | |
290 break; | |
291 case PIX_FMT_YUV422: | |
292 picture->data[0] = ptr; | |
293 picture->data[1] = NULL; | |
294 picture->data[2] = NULL; | |
295 picture->linesize[0] = width * 2; | |
296 break; | |
297 default: | |
298 picture->data[0] = NULL; | |
299 picture->data[1] = NULL; | |
300 picture->data[2] = NULL; | |
301 break; | |
302 } | |
303 } | |
304 | |
305 int avpicture_get_size(int pix_fmt, int width, int height) | |
306 { | |
307 int size; | |
308 | |
309 size = width * height; | |
310 switch(pix_fmt) { | |
311 case PIX_FMT_YUV420P: | |
312 size = (size * 3) / 2; | |
313 break; | |
314 case PIX_FMT_YUV422P: | |
315 size = (size * 2); | |
316 break; | |
317 case PIX_FMT_YUV444P: | |
318 size = (size * 3); | |
319 break; | |
320 case PIX_FMT_RGB24: | |
321 case PIX_FMT_BGR24: | |
322 size = (size * 3); | |
323 break; | |
324 case PIX_FMT_YUV422: | |
325 size = (size * 2); | |
326 break; | |
327 default: | |
328 size = -1; | |
329 break; | |
330 } | |
331 return size; | |
332 } | |
333 | |
334 | |
0 | 335 /* must be called before any other functions */ |
336 void avcodec_init(void) | |
337 { | |
338 dsputil_init(); | |
339 } | |
340 | |
341 /* simple call to use all the codecs */ | |
342 void avcodec_register_all(void) | |
343 { | |
3
1bdbd869c1f0
added CONFIG_AC3, CONFIG_MPGLIB, CONFIG_DECODERS and CONFIG_ENCODERS (Arpi: don't forget to put CONFIG_DECODERS in mplayer)
glantau
parents:
0
diff
changeset
|
344 /* encoders */ |
1bdbd869c1f0
added CONFIG_AC3, CONFIG_MPGLIB, CONFIG_DECODERS and CONFIG_ENCODERS (Arpi: don't forget to put CONFIG_DECODERS in mplayer)
glantau
parents:
0
diff
changeset
|
345 #ifdef CONFIG_ENCODERS |
0 | 346 register_avcodec(&ac3_encoder); |
347 register_avcodec(&mp2_encoder); | |
348 register_avcodec(&mpeg1video_encoder); | |
349 register_avcodec(&h263_encoder); | |
350 register_avcodec(&h263p_encoder); | |
351 register_avcodec(&rv10_encoder); | |
352 register_avcodec(&mjpeg_encoder); | |
353 register_avcodec(&opendivx_encoder); | |
354 register_avcodec(&msmpeg4_encoder); | |
3
1bdbd869c1f0
added CONFIG_AC3, CONFIG_MPGLIB, CONFIG_DECODERS and CONFIG_ENCODERS (Arpi: don't forget to put CONFIG_DECODERS in mplayer)
glantau
parents:
0
diff
changeset
|
355 #endif /* CONFIG_ENCODERS */ |
0 | 356 register_avcodec(&pcm_codec); |
357 register_avcodec(&rawvideo_codec); | |
3
1bdbd869c1f0
added CONFIG_AC3, CONFIG_MPGLIB, CONFIG_DECODERS and CONFIG_ENCODERS (Arpi: don't forget to put CONFIG_DECODERS in mplayer)
glantau
parents:
0
diff
changeset
|
358 |
0 | 359 /* decoders */ |
3
1bdbd869c1f0
added CONFIG_AC3, CONFIG_MPGLIB, CONFIG_DECODERS and CONFIG_ENCODERS (Arpi: don't forget to put CONFIG_DECODERS in mplayer)
glantau
parents:
0
diff
changeset
|
360 #ifdef CONFIG_DECODERS |
0 | 361 register_avcodec(&h263_decoder); |
362 register_avcodec(&opendivx_decoder); | |
363 register_avcodec(&msmpeg4_decoder); | |
364 register_avcodec(&mpeg_decoder); | |
365 register_avcodec(&h263i_decoder); | |
366 register_avcodec(&rv10_decoder); | |
24 | 367 register_avcodec(&mjpeg_decoder); |
3
1bdbd869c1f0
added CONFIG_AC3, CONFIG_MPGLIB, CONFIG_DECODERS and CONFIG_ENCODERS (Arpi: don't forget to put CONFIG_DECODERS in mplayer)
glantau
parents:
0
diff
changeset
|
368 #ifdef CONFIG_MPGLIB |
1bdbd869c1f0
added CONFIG_AC3, CONFIG_MPGLIB, CONFIG_DECODERS and CONFIG_ENCODERS (Arpi: don't forget to put CONFIG_DECODERS in mplayer)
glantau
parents:
0
diff
changeset
|
369 register_avcodec(&mp3_decoder); |
1bdbd869c1f0
added CONFIG_AC3, CONFIG_MPGLIB, CONFIG_DECODERS and CONFIG_ENCODERS (Arpi: don't forget to put CONFIG_DECODERS in mplayer)
glantau
parents:
0
diff
changeset
|
370 #endif |
1bdbd869c1f0
added CONFIG_AC3, CONFIG_MPGLIB, CONFIG_DECODERS and CONFIG_ENCODERS (Arpi: don't forget to put CONFIG_DECODERS in mplayer)
glantau
parents:
0
diff
changeset
|
371 #ifdef CONFIG_AC3 |
1bdbd869c1f0
added CONFIG_AC3, CONFIG_MPGLIB, CONFIG_DECODERS and CONFIG_ENCODERS (Arpi: don't forget to put CONFIG_DECODERS in mplayer)
glantau
parents:
0
diff
changeset
|
372 register_avcodec(&ac3_decoder); |
1bdbd869c1f0
added CONFIG_AC3, CONFIG_MPGLIB, CONFIG_DECODERS and CONFIG_ENCODERS (Arpi: don't forget to put CONFIG_DECODERS in mplayer)
glantau
parents:
0
diff
changeset
|
373 #endif |
1bdbd869c1f0
added CONFIG_AC3, CONFIG_MPGLIB, CONFIG_DECODERS and CONFIG_ENCODERS (Arpi: don't forget to put CONFIG_DECODERS in mplayer)
glantau
parents:
0
diff
changeset
|
374 #endif /* CONFIG_DECODERS */ |
0 | 375 } |
376 | |
377 static int encode_init(AVCodecContext *s) | |
378 { | |
379 return 0; | |
380 } | |
381 | |
382 static int decode_frame(AVCodecContext *avctx, | |
383 void *data, int *data_size, | |
384 UINT8 *buf, int buf_size) | |
385 { | |
386 return -1; | |
387 } | |
388 | |
389 static int encode_frame(AVCodecContext *avctx, | |
390 unsigned char *frame, int buf_size, void *data) | |
391 { | |
392 return -1; | |
393 } | |
394 | |
395 /* dummy pcm codec */ | |
396 AVCodec pcm_codec = { | |
397 "pcm", | |
398 CODEC_TYPE_AUDIO, | |
399 CODEC_ID_PCM, | |
400 0, | |
401 encode_init, | |
402 encode_frame, | |
403 NULL, | |
404 decode_frame, | |
405 }; | |
406 | |
407 AVCodec rawvideo_codec = { | |
408 "rawvideo", | |
409 CODEC_TYPE_VIDEO, | |
410 CODEC_ID_RAWVIDEO, | |
411 0, | |
412 encode_init, | |
413 encode_frame, | |
414 NULL, | |
415 decode_frame, | |
416 }; |