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