comparison utils.c @ 0:986e461dc072 libavcodec

Initial revision
author glantau
date Sun, 22 Jul 2001 14:18:56 +0000
parents
children 1bdbd869c1f0
comparison
equal deleted inserted replaced
-1:000000000000 0:986e461dc072
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 register_avcodec(&ac3_encoder);
248 register_avcodec(&mp2_encoder);
249 register_avcodec(&mpeg1video_encoder);
250 register_avcodec(&h263_encoder);
251 register_avcodec(&h263p_encoder);
252 register_avcodec(&rv10_encoder);
253 register_avcodec(&mjpeg_encoder);
254 register_avcodec(&opendivx_encoder);
255 register_avcodec(&msmpeg4_encoder);
256 register_avcodec(&pcm_codec);
257 register_avcodec(&rawvideo_codec);
258 /* decoders */
259 register_avcodec(&h263_decoder);
260 register_avcodec(&opendivx_decoder);
261 register_avcodec(&msmpeg4_decoder);
262 register_avcodec(&mpeg_decoder);
263 register_avcodec(&h263i_decoder);
264 register_avcodec(&rv10_decoder);
265 }
266
267 static int encode_init(AVCodecContext *s)
268 {
269 return 0;
270 }
271
272 static int decode_frame(AVCodecContext *avctx,
273 void *data, int *data_size,
274 UINT8 *buf, int buf_size)
275 {
276 return -1;
277 }
278
279 static int encode_frame(AVCodecContext *avctx,
280 unsigned char *frame, int buf_size, void *data)
281 {
282 return -1;
283 }
284
285 /* dummy pcm codec */
286 AVCodec pcm_codec = {
287 "pcm",
288 CODEC_TYPE_AUDIO,
289 CODEC_ID_PCM,
290 0,
291 encode_init,
292 encode_frame,
293 NULL,
294 decode_frame,
295 };
296
297 AVCodec rawvideo_codec = {
298 "rawvideo",
299 CODEC_TYPE_VIDEO,
300 CODEC_ID_RAWVIDEO,
301 0,
302 encode_init,
303 encode_frame,
304 NULL,
305 decode_frame,
306 };