Mercurial > libavcodec.hg
annotate utils.c @ 1031:19de1445beb2 libavcodec
use av_malloc() functions - added av_strdup and av_realloc()
author | bellard |
---|---|
date | Thu, 23 Jan 2003 23:03:09 +0000 |
parents | 6129c88a6393 |
children | 8f440ca8e0b0 |
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 |
862 | 23 void *av_mallocz(unsigned int size) |
394 | 24 { |
25 void *ptr; | |
908 | 26 |
27 if(size == 0) fprintf(stderr, "Warning, allocating 0 bytes\n"); | |
28 | |
394 | 29 ptr = av_malloc(size); |
30 if (!ptr) | |
31 return NULL; | |
32 memset(ptr, 0, size); | |
33 return ptr; | |
34 } | |
35 | |
1031
19de1445beb2
use av_malloc() functions - added av_strdup and av_realloc()
bellard
parents:
998
diff
changeset
|
36 char *av_strdup(const char *s) |
19de1445beb2
use av_malloc() functions - added av_strdup and av_realloc()
bellard
parents:
998
diff
changeset
|
37 { |
19de1445beb2
use av_malloc() functions - added av_strdup and av_realloc()
bellard
parents:
998
diff
changeset
|
38 char *ptr; |
19de1445beb2
use av_malloc() functions - added av_strdup and av_realloc()
bellard
parents:
998
diff
changeset
|
39 int len; |
19de1445beb2
use av_malloc() functions - added av_strdup and av_realloc()
bellard
parents:
998
diff
changeset
|
40 len = strlen(s) + 1; |
19de1445beb2
use av_malloc() functions - added av_strdup and av_realloc()
bellard
parents:
998
diff
changeset
|
41 ptr = av_malloc(len); |
19de1445beb2
use av_malloc() functions - added av_strdup and av_realloc()
bellard
parents:
998
diff
changeset
|
42 if (!ptr) |
19de1445beb2
use av_malloc() functions - added av_strdup and av_realloc()
bellard
parents:
998
diff
changeset
|
43 return NULL; |
19de1445beb2
use av_malloc() functions - added av_strdup and av_realloc()
bellard
parents:
998
diff
changeset
|
44 memcpy(ptr, s, len); |
19de1445beb2
use av_malloc() functions - added av_strdup and av_realloc()
bellard
parents:
998
diff
changeset
|
45 return ptr; |
19de1445beb2
use av_malloc() functions - added av_strdup and av_realloc()
bellard
parents:
998
diff
changeset
|
46 } |
19de1445beb2
use av_malloc() functions - added av_strdup and av_realloc()
bellard
parents:
998
diff
changeset
|
47 |
19de1445beb2
use av_malloc() functions - added av_strdup and av_realloc()
bellard
parents:
998
diff
changeset
|
48 /** |
19de1445beb2
use av_malloc() functions - added av_strdup and av_realloc()
bellard
parents:
998
diff
changeset
|
49 * realloc which does nothing if the block is large enough |
19de1445beb2
use av_malloc() functions - added av_strdup and av_realloc()
bellard
parents:
998
diff
changeset
|
50 */ |
19de1445beb2
use av_malloc() functions - added av_strdup and av_realloc()
bellard
parents:
998
diff
changeset
|
51 void *av_fast_realloc(void *ptr, int *size, int min_size) |
19de1445beb2
use av_malloc() functions - added av_strdup and av_realloc()
bellard
parents:
998
diff
changeset
|
52 { |
19de1445beb2
use av_malloc() functions - added av_strdup and av_realloc()
bellard
parents:
998
diff
changeset
|
53 if(min_size < *size) |
19de1445beb2
use av_malloc() functions - added av_strdup and av_realloc()
bellard
parents:
998
diff
changeset
|
54 return ptr; |
19de1445beb2
use av_malloc() functions - added av_strdup and av_realloc()
bellard
parents:
998
diff
changeset
|
55 |
19de1445beb2
use av_malloc() functions - added av_strdup and av_realloc()
bellard
parents:
998
diff
changeset
|
56 *size= min_size + 10*1024; |
19de1445beb2
use av_malloc() functions - added av_strdup and av_realloc()
bellard
parents:
998
diff
changeset
|
57 |
19de1445beb2
use av_malloc() functions - added av_strdup and av_realloc()
bellard
parents:
998
diff
changeset
|
58 return av_realloc(ptr, *size); |
19de1445beb2
use av_malloc() functions - added av_strdup and av_realloc()
bellard
parents:
998
diff
changeset
|
59 } |
19de1445beb2
use av_malloc() functions - added av_strdup and av_realloc()
bellard
parents:
998
diff
changeset
|
60 |
19de1445beb2
use av_malloc() functions - added av_strdup and av_realloc()
bellard
parents:
998
diff
changeset
|
61 |
902
6acc8394960d
* two functions to handle allocation of static data more simple
kabi
parents:
862
diff
changeset
|
62 /* allocation of static arrays - do not use for normal allocation */ |
6acc8394960d
* two functions to handle allocation of static data more simple
kabi
parents:
862
diff
changeset
|
63 static unsigned int last_static = 0; |
6acc8394960d
* two functions to handle allocation of static data more simple
kabi
parents:
862
diff
changeset
|
64 static char*** array_static = NULL; |
6acc8394960d
* two functions to handle allocation of static data more simple
kabi
parents:
862
diff
changeset
|
65 static const unsigned int grow_static = 64; // ^2 |
6acc8394960d
* two functions to handle allocation of static data more simple
kabi
parents:
862
diff
changeset
|
66 void *__av_mallocz_static(void** location, unsigned int size) |
6acc8394960d
* two functions to handle allocation of static data more simple
kabi
parents:
862
diff
changeset
|
67 { |
6acc8394960d
* two functions to handle allocation of static data more simple
kabi
parents:
862
diff
changeset
|
68 int l = (last_static + grow_static) & ~(grow_static - 1); |
6acc8394960d
* two functions to handle allocation of static data more simple
kabi
parents:
862
diff
changeset
|
69 void *ptr = av_mallocz(size); |
6acc8394960d
* two functions to handle allocation of static data more simple
kabi
parents:
862
diff
changeset
|
70 if (!ptr) |
6acc8394960d
* two functions to handle allocation of static data more simple
kabi
parents:
862
diff
changeset
|
71 return NULL; |
6acc8394960d
* two functions to handle allocation of static data more simple
kabi
parents:
862
diff
changeset
|
72 |
6acc8394960d
* two functions to handle allocation of static data more simple
kabi
parents:
862
diff
changeset
|
73 if (location) |
6acc8394960d
* two functions to handle allocation of static data more simple
kabi
parents:
862
diff
changeset
|
74 { |
6acc8394960d
* two functions to handle allocation of static data more simple
kabi
parents:
862
diff
changeset
|
75 if (l > last_static) |
1031
19de1445beb2
use av_malloc() functions - added av_strdup and av_realloc()
bellard
parents:
998
diff
changeset
|
76 array_static = av_realloc(array_static, l); |
902
6acc8394960d
* two functions to handle allocation of static data more simple
kabi
parents:
862
diff
changeset
|
77 array_static[last_static++] = (char**) location; |
6acc8394960d
* two functions to handle allocation of static data more simple
kabi
parents:
862
diff
changeset
|
78 *location = ptr; |
6acc8394960d
* two functions to handle allocation of static data more simple
kabi
parents:
862
diff
changeset
|
79 } |
6acc8394960d
* two functions to handle allocation of static data more simple
kabi
parents:
862
diff
changeset
|
80 return ptr; |
6acc8394960d
* two functions to handle allocation of static data more simple
kabi
parents:
862
diff
changeset
|
81 } |
6acc8394960d
* two functions to handle allocation of static data more simple
kabi
parents:
862
diff
changeset
|
82 /* free all static arrays and reset pointers to 0 */ |
6acc8394960d
* two functions to handle allocation of static data more simple
kabi
parents:
862
diff
changeset
|
83 void av_free_static() |
6acc8394960d
* two functions to handle allocation of static data more simple
kabi
parents:
862
diff
changeset
|
84 { |
6acc8394960d
* two functions to handle allocation of static data more simple
kabi
parents:
862
diff
changeset
|
85 if (array_static) |
6acc8394960d
* two functions to handle allocation of static data more simple
kabi
parents:
862
diff
changeset
|
86 { |
6acc8394960d
* two functions to handle allocation of static data more simple
kabi
parents:
862
diff
changeset
|
87 unsigned i; |
6acc8394960d
* two functions to handle allocation of static data more simple
kabi
parents:
862
diff
changeset
|
88 for (i = 0; i < last_static; i++) |
6acc8394960d
* two functions to handle allocation of static data more simple
kabi
parents:
862
diff
changeset
|
89 { |
1031
19de1445beb2
use av_malloc() functions - added av_strdup and av_realloc()
bellard
parents:
998
diff
changeset
|
90 av_free(*array_static[i]); |
902
6acc8394960d
* two functions to handle allocation of static data more simple
kabi
parents:
862
diff
changeset
|
91 *array_static[i] = NULL; |
6acc8394960d
* two functions to handle allocation of static data more simple
kabi
parents:
862
diff
changeset
|
92 } |
1031
19de1445beb2
use av_malloc() functions - added av_strdup and av_realloc()
bellard
parents:
998
diff
changeset
|
93 av_free(array_static); |
902
6acc8394960d
* two functions to handle allocation of static data more simple
kabi
parents:
862
diff
changeset
|
94 array_static = 0; |
6acc8394960d
* two functions to handle allocation of static data more simple
kabi
parents:
862
diff
changeset
|
95 } |
6acc8394960d
* two functions to handle allocation of static data more simple
kabi
parents:
862
diff
changeset
|
96 last_static = 0; |
6acc8394960d
* two functions to handle allocation of static data more simple
kabi
parents:
862
diff
changeset
|
97 } |
6acc8394960d
* two functions to handle allocation of static data more simple
kabi
parents:
862
diff
changeset
|
98 |
400 | 99 /* cannot call it directly because of 'void **' casting is not automatic */ |
100 void __av_freep(void **ptr) | |
101 { | |
102 av_free(*ptr); | |
103 *ptr = NULL; | |
104 } | |
105 | |
0 | 106 /* encoder management */ |
107 AVCodec *first_avcodec; | |
108 | |
109 void register_avcodec(AVCodec *format) | |
110 { | |
111 AVCodec **p; | |
112 p = &first_avcodec; | |
113 while (*p != NULL) p = &(*p)->next; | |
114 *p = format; | |
115 format->next = NULL; | |
116 } | |
117 | |
903 | 118 typedef struct DefaultPicOpaque{ |
119 int last_pic_num; | |
120 uint8_t *data[4]; | |
121 }DefaultPicOpaque; | |
122 | |
925 | 123 int avcodec_default_get_buffer(AVCodecContext *s, AVFrame *pic){ |
903 | 124 int i; |
125 const int width = s->width; | |
126 const int height= s->height; | |
127 DefaultPicOpaque *opaque; | |
924 | 128 |
129 assert(pic->data[0]==NULL); | |
130 assert(pic->type==0 || pic->type==FF_TYPE_INTERNAL); | |
903 | 131 |
132 if(pic->opaque){ | |
133 opaque= (DefaultPicOpaque *)pic->opaque; | |
134 for(i=0; i<3; i++) | |
135 pic->data[i]= opaque->data[i]; | |
136 | |
137 // printf("get_buffer %X coded_pic_num:%d last:%d\n", pic->opaque, pic->coded_picture_number, opaque->last_pic_num); | |
138 pic->age= pic->coded_picture_number - opaque->last_pic_num; | |
139 opaque->last_pic_num= pic->coded_picture_number; | |
140 //printf("age: %d %d %d\n", pic->age, c->picture_number, pic->coded_picture_number); | |
141 }else{ | |
142 int align, h_chroma_shift, v_chroma_shift; | |
143 int w, h, pixel_size; | |
144 | |
145 avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift); | |
146 | |
147 switch(s->pix_fmt){ | |
148 case PIX_FMT_YUV422: | |
149 pixel_size=2; | |
150 break; | |
151 case PIX_FMT_RGB24: | |
152 case PIX_FMT_BGR24: | |
153 pixel_size=3; | |
154 break; | |
155 case PIX_FMT_RGBA32: | |
156 pixel_size=4; | |
157 break; | |
158 default: | |
159 pixel_size=1; | |
160 } | |
161 | |
162 if(s->codec_id==CODEC_ID_SVQ1) align=63; | |
163 else align=15; | |
164 | |
165 w= (width +align)&~align; | |
166 h= (height+align)&~align; | |
167 | |
168 if(!(s->flags&CODEC_FLAG_EMU_EDGE)){ | |
169 w+= EDGE_WIDTH*2; | |
170 h+= EDGE_WIDTH*2; | |
171 } | |
172 | |
173 opaque= av_mallocz(sizeof(DefaultPicOpaque)); | |
174 if(opaque==NULL) return -1; | |
175 | |
176 pic->opaque= opaque; | |
177 opaque->last_pic_num= -256*256*256*64; | |
178 | |
179 for(i=0; i<3; i++){ | |
180 int h_shift= i==0 ? 0 : h_chroma_shift; | |
181 int v_shift= i==0 ? 0 : v_chroma_shift; | |
182 | |
183 pic->linesize[i]= pixel_size*w>>h_shift; | |
184 | |
185 pic->base[i]= av_mallocz((pic->linesize[i]*h>>v_shift)+16); //FIXME 16 | |
186 if(pic->base[i]==NULL) return -1; | |
187 | |
188 memset(pic->base[i], 128, pic->linesize[i]*h>>v_shift); | |
189 | |
190 if(s->flags&CODEC_FLAG_EMU_EDGE) | |
924 | 191 pic->data[i] = pic->base[i] + 16; //FIXME 16 |
903 | 192 else |
924 | 193 pic->data[i] = pic->base[i] + (pic->linesize[i]*EDGE_WIDTH>>v_shift) + (EDGE_WIDTH>>h_shift) + 16; //FIXME 16 |
903 | 194 |
195 opaque->data[i]= pic->data[i]; | |
196 } | |
197 pic->age= 256*256*256*64; | |
924 | 198 pic->type= FF_BUFFER_TYPE_INTERNAL; |
903 | 199 } |
200 | |
201 return 0; | |
202 } | |
203 | |
925 | 204 void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){ |
903 | 205 int i; |
206 | |
924 | 207 assert(pic->type==FF_BUFFER_TYPE_INTERNAL); |
208 | |
903 | 209 for(i=0; i<3; i++) |
210 pic->data[i]=NULL; | |
211 //printf("R%X\n", pic->opaque); | |
212 } | |
213 | |
998 | 214 enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, enum PixelFormat * fmt){ |
215 return fmt[0]; | |
216 } | |
217 | |
681 | 218 void avcodec_get_context_defaults(AVCodecContext *s){ |
685 | 219 s->bit_rate= 800*1000; |
220 s->bit_rate_tolerance= s->bit_rate*10; | |
681 | 221 s->qmin= 2; |
222 s->qmax= 31; | |
932 | 223 s->mb_qmin= 2; |
224 s->mb_qmax= 31; | |
681 | 225 s->rc_eq= "tex^qComp"; |
226 s->qcompress= 0.5; | |
685 | 227 s->max_qdiff= 3; |
228 s->b_quant_factor=1.25; | |
229 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
|
230 s->i_quant_factor=-0.8; |
685 | 231 s->i_quant_offset=0.0; |
745
25d7fb7c89be
better/cleaner error resilience (done in a 2nd pass after decoding)
michaelni
parents:
742
diff
changeset
|
232 s->error_concealment= 3; |
762 | 233 s->error_resilience= 1; |
745
25d7fb7c89be
better/cleaner error resilience (done in a 2nd pass after decoding)
michaelni
parents:
742
diff
changeset
|
234 s->workaround_bugs= FF_BUG_AUTODETECT; |
762 | 235 s->frame_rate = 25 * FRAME_RATE_BASE; |
236 s->gop_size= 50; | |
237 s->me_method= ME_EPZS; | |
903 | 238 s->get_buffer= avcodec_default_get_buffer; |
239 s->release_buffer= avcodec_default_release_buffer; | |
998 | 240 s->get_format= avcodec_default_get_format; |
954 | 241 s->me_subpel_quality=8; |
681 | 242 } |
243 | |
244 /** | |
245 * allocates a AVCodecContext and set it to defaults. | |
246 * this can be deallocated by simply calling free() | |
247 */ | |
703 | 248 AVCodecContext *avcodec_alloc_context(void){ |
681 | 249 AVCodecContext *avctx= av_mallocz(sizeof(AVCodecContext)); |
250 | |
251 if(avctx==NULL) return NULL; | |
252 | |
253 avcodec_get_context_defaults(avctx); | |
254 | |
255 return avctx; | |
256 } | |
257 | |
903 | 258 /** |
925 | 259 * allocates a AVPFrame and set it to defaults. |
903 | 260 * this can be deallocated by simply calling free() |
261 */ | |
925 | 262 AVFrame *avcodec_alloc_frame(void){ |
263 AVFrame *pic= av_mallocz(sizeof(AVFrame)); | |
903 | 264 |
265 return pic; | |
266 } | |
267 | |
0 | 268 int avcodec_open(AVCodecContext *avctx, AVCodec *codec) |
269 { | |
270 int ret; | |
271 | |
272 avctx->codec = codec; | |
927 | 273 avctx->codec_id = codec->id; |
0 | 274 avctx->frame_number = 0; |
374
02147e22f8c8
* Don't allocate 0 bytes of memory. It upsets electricFence!
philipjsg
parents:
362
diff
changeset
|
275 if (codec->priv_data_size > 0) { |
02147e22f8c8
* Don't allocate 0 bytes of memory. It upsets electricFence!
philipjsg
parents:
362
diff
changeset
|
276 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
|
277 if (!avctx->priv_data) |
02147e22f8c8
* Don't allocate 0 bytes of memory. It upsets electricFence!
philipjsg
parents:
362
diff
changeset
|
278 return -ENOMEM; |
02147e22f8c8
* Don't allocate 0 bytes of memory. It upsets electricFence!
philipjsg
parents:
362
diff
changeset
|
279 } else { |
02147e22f8c8
* Don't allocate 0 bytes of memory. It upsets electricFence!
philipjsg
parents:
362
diff
changeset
|
280 avctx->priv_data = NULL; |
02147e22f8c8
* Don't allocate 0 bytes of memory. It upsets electricFence!
philipjsg
parents:
362
diff
changeset
|
281 } |
0 | 282 ret = avctx->codec->init(avctx); |
283 if (ret < 0) { | |
394 | 284 av_freep(&avctx->priv_data); |
0 | 285 return ret; |
286 } | |
287 return 0; | |
288 } | |
289 | |
290 int avcodec_encode_audio(AVCodecContext *avctx, UINT8 *buf, int buf_size, | |
291 const short *samples) | |
292 { | |
293 int ret; | |
294 | |
295 ret = avctx->codec->encode(avctx, buf, buf_size, (void *)samples); | |
296 avctx->frame_number++; | |
297 return ret; | |
298 } | |
299 | |
300 int avcodec_encode_video(AVCodecContext *avctx, UINT8 *buf, int buf_size, | |
925 | 301 const AVFrame *pict) |
0 | 302 { |
303 int ret; | |
304 | |
305 ret = avctx->codec->encode(avctx, buf, buf_size, (void *)pict); | |
814 | 306 |
307 emms_c(); //needed to avoid a emms_c() call before every return; | |
308 | |
0 | 309 avctx->frame_number++; |
310 return ret; | |
311 } | |
312 | |
313 /* decode a frame. return -1 if error, otherwise return the number of | |
314 bytes used. If no frame could be decompressed, *got_picture_ptr is | |
315 zero. Otherwise, it is non zero */ | |
925 | 316 int avcodec_decode_video(AVCodecContext *avctx, AVFrame *picture, |
0 | 317 int *got_picture_ptr, |
318 UINT8 *buf, int buf_size) | |
319 { | |
320 int ret; | |
903 | 321 |
0 | 322 ret = avctx->codec->decode(avctx, picture, got_picture_ptr, |
323 buf, buf_size); | |
814 | 324 |
325 emms_c(); //needed to avoid a emms_c() call before every return; | |
903 | 326 |
267
e10840e4f773
- Bug fix MPEG-2 decoder to handle "repeat_first_field" (Telecine)
pulento
parents:
260
diff
changeset
|
327 if (*got_picture_ptr) |
e10840e4f773
- Bug fix MPEG-2 decoder to handle "repeat_first_field" (Telecine)
pulento
parents:
260
diff
changeset
|
328 avctx->frame_number++; |
0 | 329 return ret; |
330 } | |
331 | |
332 /* decode an audio frame. return -1 if error, otherwise return the | |
333 *number of bytes used. If no frame could be decompressed, | |
334 *frame_size_ptr is zero. Otherwise, it is the decompressed frame | |
335 *size in BYTES. */ | |
336 int avcodec_decode_audio(AVCodecContext *avctx, INT16 *samples, | |
337 int *frame_size_ptr, | |
338 UINT8 *buf, int buf_size) | |
339 { | |
340 int ret; | |
341 | |
342 ret = avctx->codec->decode(avctx, samples, frame_size_ptr, | |
343 buf, buf_size); | |
344 avctx->frame_number++; | |
345 return ret; | |
346 } | |
347 | |
348 int avcodec_close(AVCodecContext *avctx) | |
349 { | |
350 if (avctx->codec->close) | |
351 avctx->codec->close(avctx); | |
394 | 352 av_freep(&avctx->priv_data); |
0 | 353 avctx->codec = NULL; |
354 return 0; | |
355 } | |
356 | |
357 AVCodec *avcodec_find_encoder(enum CodecID id) | |
358 { | |
359 AVCodec *p; | |
360 p = first_avcodec; | |
361 while (p) { | |
362 if (p->encode != NULL && p->id == id) | |
363 return p; | |
364 p = p->next; | |
365 } | |
366 return NULL; | |
367 } | |
368 | |
177 | 369 AVCodec *avcodec_find_encoder_by_name(const char *name) |
370 { | |
371 AVCodec *p; | |
372 p = first_avcodec; | |
373 while (p) { | |
374 if (p->encode != NULL && strcmp(name,p->name) == 0) | |
375 return p; | |
376 p = p->next; | |
377 } | |
378 return NULL; | |
379 } | |
380 | |
0 | 381 AVCodec *avcodec_find_decoder(enum CodecID id) |
382 { | |
383 AVCodec *p; | |
384 p = first_avcodec; | |
385 while (p) { | |
386 if (p->decode != NULL && p->id == id) | |
387 return p; | |
388 p = p->next; | |
389 } | |
390 return NULL; | |
391 } | |
392 | |
393 AVCodec *avcodec_find_decoder_by_name(const char *name) | |
394 { | |
395 AVCodec *p; | |
396 p = first_avcodec; | |
397 while (p) { | |
398 if (p->decode != NULL && strcmp(name,p->name) == 0) | |
399 return p; | |
400 p = p->next; | |
401 } | |
402 return NULL; | |
403 } | |
404 | |
405 AVCodec *avcodec_find(enum CodecID id) | |
406 { | |
407 AVCodec *p; | |
408 p = first_avcodec; | |
409 while (p) { | |
410 if (p->id == id) | |
411 return p; | |
412 p = p->next; | |
413 } | |
414 return NULL; | |
415 } | |
416 | |
417 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode) | |
418 { | |
419 const char *codec_name; | |
420 AVCodec *p; | |
421 char buf1[32]; | |
337 | 422 char channels_str[100]; |
92 | 423 int bitrate; |
0 | 424 |
425 if (encode) | |
426 p = avcodec_find_encoder(enc->codec_id); | |
427 else | |
428 p = avcodec_find_decoder(enc->codec_id); | |
429 | |
430 if (p) { | |
431 codec_name = p->name; | |
432 } else if (enc->codec_name[0] != '\0') { | |
433 codec_name = enc->codec_name; | |
434 } else { | |
435 /* output avi tags */ | |
436 if (enc->codec_type == CODEC_TYPE_VIDEO) { | |
437 snprintf(buf1, sizeof(buf1), "%c%c%c%c", | |
438 enc->codec_tag & 0xff, | |
439 (enc->codec_tag >> 8) & 0xff, | |
440 (enc->codec_tag >> 16) & 0xff, | |
441 (enc->codec_tag >> 24) & 0xff); | |
442 } else { | |
443 snprintf(buf1, sizeof(buf1), "0x%04x", enc->codec_tag); | |
444 } | |
445 codec_name = buf1; | |
446 } | |
447 | |
448 switch(enc->codec_type) { | |
449 case CODEC_TYPE_VIDEO: | |
450 snprintf(buf, buf_size, | |
451 "Video: %s%s", | |
452 codec_name, enc->flags & CODEC_FLAG_HQ ? " (hq)" : ""); | |
55 | 453 if (enc->codec_id == CODEC_ID_RAWVIDEO) { |
454 snprintf(buf + strlen(buf), buf_size - strlen(buf), | |
455 ", %s", | |
988
001b7d3045e5
moved avcodec_get_chroma_sub_sample() to imgconvert.c
bellard
parents:
963
diff
changeset
|
456 avcodec_get_pix_fmt_name(enc->pix_fmt)); |
55 | 457 } |
0 | 458 if (enc->width) { |
459 snprintf(buf + strlen(buf), buf_size - strlen(buf), | |
460 ", %dx%d, %0.2f fps", | |
461 enc->width, enc->height, | |
462 (float)enc->frame_rate / FRAME_RATE_BASE); | |
463 } | |
741 | 464 if (encode) { |
465 snprintf(buf + strlen(buf), buf_size - strlen(buf), | |
466 ", q=%d-%d", enc->qmin, enc->qmax); | |
467 } | |
92 | 468 bitrate = enc->bit_rate; |
0 | 469 break; |
470 case CODEC_TYPE_AUDIO: | |
471 snprintf(buf, buf_size, | |
472 "Audio: %s", | |
473 codec_name); | |
318
21697f35a9ca
- Fixed AC3 decoding for 5:1 AC3 streams. Now when calling av_audio_decode for
pulento
parents:
315
diff
changeset
|
474 switch (enc->channels) { |
21697f35a9ca
- Fixed AC3 decoding for 5:1 AC3 streams. Now when calling av_audio_decode for
pulento
parents:
315
diff
changeset
|
475 case 1: |
337 | 476 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
|
477 break; |
21697f35a9ca
- Fixed AC3 decoding for 5:1 AC3 streams. Now when calling av_audio_decode for
pulento
parents:
315
diff
changeset
|
478 case 2: |
337 | 479 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
|
480 break; |
21697f35a9ca
- Fixed AC3 decoding for 5:1 AC3 streams. Now when calling av_audio_decode for
pulento
parents:
315
diff
changeset
|
481 case 6: |
337 | 482 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
|
483 break; |
21697f35a9ca
- Fixed AC3 decoding for 5:1 AC3 streams. Now when calling av_audio_decode for
pulento
parents:
315
diff
changeset
|
484 default: |
21697f35a9ca
- Fixed AC3 decoding for 5:1 AC3 streams. Now when calling av_audio_decode for
pulento
parents:
315
diff
changeset
|
485 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
|
486 break; |
21697f35a9ca
- Fixed AC3 decoding for 5:1 AC3 streams. Now when calling av_audio_decode for
pulento
parents:
315
diff
changeset
|
487 } |
0 | 488 if (enc->sample_rate) { |
489 snprintf(buf + strlen(buf), buf_size - strlen(buf), | |
490 ", %d Hz, %s", | |
491 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
|
492 channels_str); |
0 | 493 } |
318
21697f35a9ca
- Fixed AC3 decoding for 5:1 AC3 streams. Now when calling av_audio_decode for
pulento
parents:
315
diff
changeset
|
494 |
92 | 495 /* for PCM codecs, compute bitrate directly */ |
496 switch(enc->codec_id) { | |
497 case CODEC_ID_PCM_S16LE: | |
498 case CODEC_ID_PCM_S16BE: | |
499 case CODEC_ID_PCM_U16LE: | |
500 case CODEC_ID_PCM_U16BE: | |
94 | 501 bitrate = enc->sample_rate * enc->channels * 16; |
92 | 502 break; |
503 case CODEC_ID_PCM_S8: | |
504 case CODEC_ID_PCM_U8: | |
505 case CODEC_ID_PCM_ALAW: | |
506 case CODEC_ID_PCM_MULAW: | |
94 | 507 bitrate = enc->sample_rate * enc->channels * 8; |
92 | 508 break; |
509 default: | |
510 bitrate = enc->bit_rate; | |
511 break; | |
512 } | |
0 | 513 break; |
514 default: | |
583 | 515 av_abort(); |
0 | 516 } |
741 | 517 if (encode) { |
518 if (enc->flags & CODEC_FLAG_PASS1) | |
519 snprintf(buf + strlen(buf), buf_size - strlen(buf), | |
520 ", pass 1"); | |
521 if (enc->flags & CODEC_FLAG_PASS2) | |
522 snprintf(buf + strlen(buf), buf_size - strlen(buf), | |
523 ", pass 2"); | |
524 } | |
92 | 525 if (bitrate != 0) { |
0 | 526 snprintf(buf + strlen(buf), buf_size - strlen(buf), |
92 | 527 ", %d kb/s", bitrate / 1000); |
0 | 528 } |
529 } | |
530 | |
362 | 531 unsigned avcodec_version( void ) |
532 { | |
533 return LIBAVCODEC_VERSION_INT; | |
534 } | |
55 | 535 |
379 | 536 unsigned avcodec_build( void ) |
537 { | |
538 return LIBAVCODEC_BUILD; | |
539 } | |
540 | |
0 | 541 /* must be called before any other functions */ |
542 void avcodec_init(void) | |
543 { | |
303
9a931fd8d06c
multiple init bugfix (patch by Alex Beregszaszi <alex@naxine.org>)
michaelni
parents:
267
diff
changeset
|
544 static int inited = 0; |
9a931fd8d06c
multiple init bugfix (patch by Alex Beregszaszi <alex@naxine.org>)
michaelni
parents:
267
diff
changeset
|
545 |
9a931fd8d06c
multiple init bugfix (patch by Alex Beregszaszi <alex@naxine.org>)
michaelni
parents:
267
diff
changeset
|
546 if (inited != 0) |
9a931fd8d06c
multiple init bugfix (patch by Alex Beregszaszi <alex@naxine.org>)
michaelni
parents:
267
diff
changeset
|
547 return; |
9a931fd8d06c
multiple init bugfix (patch by Alex Beregszaszi <alex@naxine.org>)
michaelni
parents:
267
diff
changeset
|
548 inited = 1; |
9a931fd8d06c
multiple init bugfix (patch by Alex Beregszaszi <alex@naxine.org>)
michaelni
parents:
267
diff
changeset
|
549 |
851 | 550 //dsputil_init(); |
0 | 551 } |
552 | |
924 | 553 /* this can be called after seeking and before trying to decode the next keyframe */ |
341 | 554 void avcodec_flush_buffers(AVCodecContext *avctx) |
555 { | |
924 | 556 int i; |
341 | 557 MpegEncContext *s = avctx->priv_data; |
924 | 558 |
559 switch(avctx->codec_id){ | |
560 case CODEC_ID_MPEG1VIDEO: | |
561 case CODEC_ID_H263: | |
562 case CODEC_ID_RV10: | |
563 case CODEC_ID_MJPEG: | |
564 case CODEC_ID_MJPEGB: | |
565 case CODEC_ID_MPEG4: | |
566 case CODEC_ID_MSMPEG4V1: | |
567 case CODEC_ID_MSMPEG4V2: | |
568 case CODEC_ID_MSMPEG4V3: | |
569 case CODEC_ID_WMV1: | |
570 case CODEC_ID_WMV2: | |
571 case CODEC_ID_H263P: | |
572 case CODEC_ID_H263I: | |
573 case CODEC_ID_SVQ1: | |
574 for(i=0; i<MAX_PICTURE_COUNT; i++){ | |
575 if(s->picture[i].data[0] && ( s->picture[i].type == FF_BUFFER_TYPE_INTERNAL | |
576 || s->picture[i].type == FF_BUFFER_TYPE_USER)) | |
925 | 577 avctx->release_buffer(avctx, (AVFrame*)&s->picture[i]); |
963 | 578 } |
579 s->last_picture.data[0] = s->next_picture.data[0] = NULL; | |
924 | 580 break; |
581 default: | |
582 //FIXME | |
583 break; | |
584 } | |
341 | 585 } |
586 | |
440
000aeeac27a2
* started to cleanup name clashes for onetime compilation
kabi
parents:
429
diff
changeset
|
587 static int raw_encode_init(AVCodecContext *s) |
0 | 588 { |
589 return 0; | |
590 } | |
591 | |
440
000aeeac27a2
* started to cleanup name clashes for onetime compilation
kabi
parents:
429
diff
changeset
|
592 static int raw_decode_frame(AVCodecContext *avctx, |
000aeeac27a2
* started to cleanup name clashes for onetime compilation
kabi
parents:
429
diff
changeset
|
593 void *data, int *data_size, |
000aeeac27a2
* started to cleanup name clashes for onetime compilation
kabi
parents:
429
diff
changeset
|
594 UINT8 *buf, int buf_size) |
0 | 595 { |
596 return -1; | |
597 } | |
598 | |
440
000aeeac27a2
* started to cleanup name clashes for onetime compilation
kabi
parents:
429
diff
changeset
|
599 static int raw_encode_frame(AVCodecContext *avctx, |
000aeeac27a2
* started to cleanup name clashes for onetime compilation
kabi
parents:
429
diff
changeset
|
600 unsigned char *frame, int buf_size, void *data) |
0 | 601 { |
602 return -1; | |
603 } | |
604 | |
605 AVCodec rawvideo_codec = { | |
606 "rawvideo", | |
607 CODEC_TYPE_VIDEO, | |
608 CODEC_ID_RAWVIDEO, | |
609 0, | |
440
000aeeac27a2
* started to cleanup name clashes for onetime compilation
kabi
parents:
429
diff
changeset
|
610 raw_encode_init, |
000aeeac27a2
* started to cleanup name clashes for onetime compilation
kabi
parents:
429
diff
changeset
|
611 raw_encode_frame, |
0 | 612 NULL, |
440
000aeeac27a2
* started to cleanup name clashes for onetime compilation
kabi
parents:
429
diff
changeset
|
613 raw_decode_frame, |
0 | 614 }; |