3 * Copyright (c) 2001 Fabrice Bellard.
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.
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.
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
21 #include "mpegvideo.h"
23 void *av_mallocz(unsigned int size)
27 ptr = av_malloc(size);
34 char *av_strdup(const char *s)
47 * realloc which does nothing if the block is large enough
49 void *av_fast_realloc(void *ptr, int *size, int min_size)
54 *size= min_size + 10*1024;
56 return av_realloc(ptr, *size);
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)
66 int l = (last_static + grow_static) & ~(grow_static - 1);
67 void *ptr = av_mallocz(size);
74 array_static = av_realloc(array_static, l);
75 array_static[last_static++] = (char**) location;
80 /* free all static arrays and reset pointers to 0 */
86 for (i = 0; i < last_static; i++)
88 av_free(*array_static[i]);
89 *array_static[i] = NULL;
91 av_free(array_static);
97 /* cannot call it directly because of 'void **' casting is not automatic */
98 void __av_freep(void **ptr)
104 /* encoder management */
105 AVCodec *first_avcodec;
107 void register_avcodec(AVCodec *format)
111 while (*p != NULL) p = &(*p)->next;
116 typedef struct DefaultPicOpaque{
121 int avcodec_default_get_buffer(AVCodecContext *s, AVFrame *pic){
123 const int width = s->width;
124 const int height= s->height;
125 DefaultPicOpaque *opaque;
127 assert(pic->data[0]==NULL);
128 assert(pic->type==0 || pic->type==FF_TYPE_INTERNAL);
131 opaque= (DefaultPicOpaque *)pic->opaque;
133 pic->data[i]= opaque->data[i];
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);
140 int align, h_chroma_shift, v_chroma_shift;
141 int w, h, pixel_size;
143 avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
160 if(s->codec_id==CODEC_ID_SVQ1) align=63;
163 w= (width +align)&~align;
164 h= (height+align)&~align;
166 if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
171 opaque= av_mallocz(sizeof(DefaultPicOpaque));
172 if(opaque==NULL) return -1;
175 opaque->last_pic_num= -256*256*256*64;
178 int h_shift= i==0 ? 0 : h_chroma_shift;
179 int v_shift= i==0 ? 0 : v_chroma_shift;
181 pic->linesize[i]= pixel_size*w>>h_shift;
183 pic->base[i]= av_mallocz((pic->linesize[i]*h>>v_shift)+16); //FIXME 16
184 if(pic->base[i]==NULL) return -1;
186 memset(pic->base[i], 128, pic->linesize[i]*h>>v_shift);
188 if(s->flags&CODEC_FLAG_EMU_EDGE)
189 pic->data[i] = pic->base[i] + 16; //FIXME 16
191 pic->data[i] = pic->base[i] + (pic->linesize[i]*EDGE_WIDTH>>v_shift) + (EDGE_WIDTH>>h_shift) + 16; //FIXME 16
193 opaque->data[i]= pic->data[i];
195 pic->age= 256*256*256*64;
196 pic->type= FF_BUFFER_TYPE_INTERNAL;
202 void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
205 assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
209 //printf("R%X\n", pic->opaque);
212 enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, enum PixelFormat * fmt){
216 void avcodec_get_context_defaults(AVCodecContext *s){
217 s->bit_rate= 800*1000;
218 s->bit_rate_tolerance= s->bit_rate*10;
223 s->rc_eq= "tex^qComp";
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;
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;
243 * allocates a AVCodecContext and set it to defaults.
244 * this can be deallocated by simply calling free()
246 AVCodecContext *avcodec_alloc_context(void){
247 AVCodecContext *avctx= av_mallocz(sizeof(AVCodecContext));
249 if(avctx==NULL) return NULL;
251 avcodec_get_context_defaults(avctx);
257 * allocates a AVPFrame and set it to defaults.
258 * this can be deallocated by simply calling free()
260 AVFrame *avcodec_alloc_frame(void){
261 AVFrame *pic= av_mallocz(sizeof(AVFrame));
266 int avcodec_open(AVCodecContext *avctx, AVCodec *codec)
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)
278 avctx->priv_data = NULL;
280 ret = avctx->codec->init(avctx);
282 av_freep(&avctx->priv_data);
288 int avcodec_encode_audio(AVCodecContext *avctx, UINT8 *buf, int buf_size,
289 const short *samples)
293 ret = avctx->codec->encode(avctx, buf, buf_size, (void *)samples);
294 avctx->frame_number++;
298 int avcodec_encode_video(AVCodecContext *avctx, UINT8 *buf, int buf_size,
303 ret = avctx->codec->encode(avctx, buf, buf_size, (void *)pict);
305 emms_c(); //needed to avoid a emms_c() call before every return;
307 avctx->frame_number++;
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)
320 ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
323 emms_c(); //needed to avoid a emms_c() call before every return;
325 if (*got_picture_ptr)
326 avctx->frame_number++;
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
334 int avcodec_decode_audio(AVCodecContext *avctx, INT16 *samples,
336 UINT8 *buf, int buf_size)
340 ret = avctx->codec->decode(avctx, samples, frame_size_ptr,
342 avctx->frame_number++;
346 int avcodec_close(AVCodecContext *avctx)
348 if (avctx->codec->close)
349 avctx->codec->close(avctx);
350 av_freep(&avctx->priv_data);
355 AVCodec *avcodec_find_encoder(enum CodecID id)
360 if (p->encode != NULL && p->id == id)
367 AVCodec *avcodec_find_encoder_by_name(const char *name)
372 if (p->encode != NULL && strcmp(name,p->name) == 0)
379 AVCodec *avcodec_find_decoder(enum CodecID id)
384 if (p->decode != NULL && p->id == id)
391 AVCodec *avcodec_find_decoder_by_name(const char *name)
396 if (p->decode != NULL && strcmp(name,p->name) == 0)
403 AVCodec *avcodec_find(enum CodecID id)
415 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
417 const char *codec_name;
420 char channels_str[100];
424 p = avcodec_find_encoder(enc->codec_id);
426 p = avcodec_find_decoder(enc->codec_id);
429 codec_name = p->name;
430 } else if (enc->codec_name[0] != '\0') {
431 codec_name = enc->codec_name;
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);
441 snprintf(buf1, sizeof(buf1), "0x%04x", enc->codec_tag);
446 switch(enc->codec_type) {
447 case CODEC_TYPE_VIDEO:
448 snprintf(buf, buf_size,
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),
454 avcodec_get_pix_fmt_name(enc->pix_fmt));
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);
463 snprintf(buf + strlen(buf), buf_size - strlen(buf),
464 ", q=%d-%d", enc->qmin, enc->qmax);
466 bitrate = enc->bit_rate;
468 case CODEC_TYPE_AUDIO:
469 snprintf(buf, buf_size,
472 switch (enc->channels) {
474 strcpy(channels_str, "mono");
477 strcpy(channels_str, "stereo");
480 strcpy(channels_str, "5:1");
483 sprintf(channels_str, "%d channels", enc->channels);
486 if (enc->sample_rate) {
487 snprintf(buf + strlen(buf), buf_size - strlen(buf),
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;
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;
508 bitrate = enc->bit_rate;
516 if (enc->flags & CODEC_FLAG_PASS1)
517 snprintf(buf + strlen(buf), buf_size - strlen(buf),
519 if (enc->flags & CODEC_FLAG_PASS2)
520 snprintf(buf + strlen(buf), buf_size - strlen(buf),
524 snprintf(buf + strlen(buf), buf_size - strlen(buf),
525 ", %d kb/s", bitrate / 1000);
529 unsigned avcodec_version( void )
531 return LIBAVCODEC_VERSION_INT;
534 unsigned avcodec_build( void )
536 return LIBAVCODEC_BUILD;
539 /* must be called before any other functions */
540 void avcodec_init(void)
542 static int inited = 0;
551 /* this can be called after seeking and before trying to decode the next keyframe */
552 void avcodec_flush_buffers(AVCodecContext *avctx)
555 MpegEncContext *s = avctx->priv_data;
557 switch(avctx->codec_id){
558 case CODEC_ID_MPEG1VIDEO:
562 case CODEC_ID_MJPEGB:
564 case CODEC_ID_MSMPEG4V1:
565 case CODEC_ID_MSMPEG4V2:
566 case CODEC_ID_MSMPEG4V3:
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]);
577 s->last_picture.data[0] = s->next_picture.data[0] = NULL;
585 static int raw_encode_init(AVCodecContext *s)
590 static int raw_decode_frame(AVCodecContext *avctx,
591 void *data, int *data_size,
592 UINT8 *buf, int buf_size)
597 static int raw_encode_frame(AVCodecContext *avctx,
598 unsigned char *frame, int buf_size, void *data)
603 AVCodec rawvideo_codec = {