added CONFIG_AC3, CONFIG_MPGLIB, CONFIG_DECODERS and CONFIG_ENCODERS (Arpi: don't...
[libav.git] / libavcodec / utils.c
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
177 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
178 {
179 const char *codec_name;
180 AVCodec *p;
181 char buf1[32];
182
183 if (encode)
184 p = avcodec_find_encoder(enc->codec_id);
185 else
186 p = avcodec_find_decoder(enc->codec_id);
187
188 if (p) {
189 codec_name = p->name;
190 } else if (enc->codec_name[0] != '\0') {
191 codec_name = enc->codec_name;
192 } else {
193 /* output avi tags */
194 if (enc->codec_type == CODEC_TYPE_VIDEO) {
195 snprintf(buf1, sizeof(buf1), "%c%c%c%c",
196 enc->codec_tag & 0xff,
197 (enc->codec_tag >> 8) & 0xff,
198 (enc->codec_tag >> 16) & 0xff,
199 (enc->codec_tag >> 24) & 0xff);
200 } else {
201 snprintf(buf1, sizeof(buf1), "0x%04x", enc->codec_tag);
202 }
203 codec_name = buf1;
204 }
205
206 switch(enc->codec_type) {
207 case CODEC_TYPE_VIDEO:
208 snprintf(buf, buf_size,
209 "Video: %s%s",
210 codec_name, enc->flags & CODEC_FLAG_HQ ? " (hq)" : "");
211 if (enc->width) {
212 snprintf(buf + strlen(buf), buf_size - strlen(buf),
213 ", %dx%d, %0.2f fps",
214 enc->width, enc->height,
215 (float)enc->frame_rate / FRAME_RATE_BASE);
216 }
217 break;
218 case CODEC_TYPE_AUDIO:
219 snprintf(buf, buf_size,
220 "Audio: %s",
221 codec_name);
222 if (enc->sample_rate) {
223 snprintf(buf + strlen(buf), buf_size - strlen(buf),
224 ", %d Hz, %s",
225 enc->sample_rate,
226 enc->channels == 2 ? "stereo" : "mono");
227 }
228 break;
229 default:
230 abort();
231 }
232 if (enc->bit_rate != 0) {
233 snprintf(buf + strlen(buf), buf_size - strlen(buf),
234 ", %d kb/s", enc->bit_rate / 1000);
235 }
236 }
237
238 /* must be called before any other functions */
239 void avcodec_init(void)
240 {
241 dsputil_init();
242 }
243
244 /* simple call to use all the codecs */
245 void avcodec_register_all(void)
246 {
247 /* encoders */
248 #ifdef CONFIG_ENCODERS
249 register_avcodec(&ac3_encoder);
250 register_avcodec(&mp2_encoder);
251 register_avcodec(&mpeg1video_encoder);
252 register_avcodec(&h263_encoder);
253 register_avcodec(&h263p_encoder);
254 register_avcodec(&rv10_encoder);
255 register_avcodec(&mjpeg_encoder);
256 register_avcodec(&opendivx_encoder);
257 register_avcodec(&msmpeg4_encoder);
258 #endif /* CONFIG_ENCODERS */
259 register_avcodec(&pcm_codec);
260 register_avcodec(&rawvideo_codec);
261
262 /* decoders */
263 #ifdef CONFIG_DECODERS
264 register_avcodec(&h263_decoder);
265 register_avcodec(&opendivx_decoder);
266 register_avcodec(&msmpeg4_decoder);
267 register_avcodec(&mpeg_decoder);
268 register_avcodec(&h263i_decoder);
269 register_avcodec(&rv10_decoder);
270 #ifdef CONFIG_MPGLIB
271 register_avcodec(&mp3_decoder);
272 #endif
273 #ifdef CONFIG_AC3
274 register_avcodec(&ac3_decoder);
275 #endif
276 #endif /* CONFIG_DECODERS */
277 }
278
279 static int encode_init(AVCodecContext *s)
280 {
281 return 0;
282 }
283
284 static int decode_frame(AVCodecContext *avctx,
285 void *data, int *data_size,
286 UINT8 *buf, int buf_size)
287 {
288 return -1;
289 }
290
291 static int encode_frame(AVCodecContext *avctx,
292 unsigned char *frame, int buf_size, void *data)
293 {
294 return -1;
295 }
296
297 /* dummy pcm codec */
298 AVCodec pcm_codec = {
299 "pcm",
300 CODEC_TYPE_AUDIO,
301 CODEC_ID_PCM,
302 0,
303 encode_init,
304 encode_frame,
305 NULL,
306 decode_frame,
307 };
308
309 AVCodec rawvideo_codec = {
310 "rawvideo",
311 CODEC_TYPE_VIDEO,
312 CODEC_ID_RAWVIDEO,
313 0,
314 encode_init,
315 encode_frame,
316 NULL,
317 decode_frame,
318 };