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 if(size == 0) fprintf(stderr, "Warning, allocating 0 bytes\n");
29 ptr = av_malloc(size);
36 /* allocation of static arrays - do not use for normal allocation */
37 static unsigned int last_static = 0;
38 static char*** array_static = NULL;
39 static const unsigned int grow_static = 64; // ^2
40 void *__av_mallocz_static(void** location, unsigned int size)
42 int l = (last_static + grow_static) & ~(grow_static - 1);
43 void *ptr = av_mallocz(size);
50 array_static = realloc(array_static, l);
51 array_static[last_static++] = (char**) location;
56 /* free all static arrays and reset pointers to 0 */
62 for (i = 0; i < last_static; i++)
64 free(*array_static[i]);
65 *array_static[i] = NULL;
73 /* cannot call it directly because of 'void **' casting is not automatic */
74 void __av_freep(void **ptr)
80 /* encoder management */
81 AVCodec *first_avcodec;
83 void register_avcodec(AVCodec *format)
87 while (*p != NULL) p = &(*p)->next;
92 typedef struct DefaultPicOpaque{
97 int avcodec_default_get_buffer(AVCodecContext *s, AVFrame *pic){
99 const int width = s->width;
100 const int height= s->height;
101 DefaultPicOpaque *opaque;
103 assert(pic->data[0]==NULL);
104 assert(pic->type==0 || pic->type==FF_TYPE_INTERNAL);
107 opaque= (DefaultPicOpaque *)pic->opaque;
109 pic->data[i]= opaque->data[i];
111 // printf("get_buffer %X coded_pic_num:%d last:%d\n", pic->opaque, pic->coded_picture_number, opaque->last_pic_num);
112 pic->age= pic->coded_picture_number - opaque->last_pic_num;
113 opaque->last_pic_num= pic->coded_picture_number;
114 //printf("age: %d %d %d\n", pic->age, c->picture_number, pic->coded_picture_number);
116 int align, h_chroma_shift, v_chroma_shift;
117 int w, h, pixel_size;
119 avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
136 if(s->codec_id==CODEC_ID_SVQ1) align=63;
139 w= (width +align)&~align;
140 h= (height+align)&~align;
142 if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
147 opaque= av_mallocz(sizeof(DefaultPicOpaque));
148 if(opaque==NULL) return -1;
151 opaque->last_pic_num= -256*256*256*64;
154 int h_shift= i==0 ? 0 : h_chroma_shift;
155 int v_shift= i==0 ? 0 : v_chroma_shift;
157 pic->linesize[i]= pixel_size*w>>h_shift;
159 pic->base[i]= av_mallocz((pic->linesize[i]*h>>v_shift)+16); //FIXME 16
160 if(pic->base[i]==NULL) return -1;
162 memset(pic->base[i], 128, pic->linesize[i]*h>>v_shift);
164 if(s->flags&CODEC_FLAG_EMU_EDGE)
165 pic->data[i] = pic->base[i] + 16; //FIXME 16
167 pic->data[i] = pic->base[i] + (pic->linesize[i]*EDGE_WIDTH>>v_shift) + (EDGE_WIDTH>>h_shift) + 16; //FIXME 16
169 opaque->data[i]= pic->data[i];
171 pic->age= 256*256*256*64;
172 pic->type= FF_BUFFER_TYPE_INTERNAL;
178 void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
181 assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
185 //printf("R%X\n", pic->opaque);
188 void avcodec_get_context_defaults(AVCodecContext *s){
189 s->bit_rate= 800*1000;
190 s->bit_rate_tolerance= s->bit_rate*10;
195 s->rc_eq= "tex^qComp";
198 s->b_quant_factor=1.25;
199 s->b_quant_offset=1.25;
200 s->i_quant_factor=-0.8;
201 s->i_quant_offset=0.0;
202 s->error_concealment= 3;
203 s->error_resilience= 1;
204 s->workaround_bugs= FF_BUG_AUTODETECT;
205 s->frame_rate = 25 * FRAME_RATE_BASE;
207 s->me_method= ME_EPZS;
208 s->get_buffer= avcodec_default_get_buffer;
209 s->release_buffer= avcodec_default_release_buffer;
210 s->me_subpel_quality=8;
214 * allocates a AVCodecContext and set it to defaults.
215 * this can be deallocated by simply calling free()
217 AVCodecContext *avcodec_alloc_context(void){
218 AVCodecContext *avctx= av_mallocz(sizeof(AVCodecContext));
220 if(avctx==NULL) return NULL;
222 avcodec_get_context_defaults(avctx);
228 * allocates a AVPFrame and set it to defaults.
229 * this can be deallocated by simply calling free()
231 AVFrame *avcodec_alloc_frame(void){
232 AVFrame *pic= av_mallocz(sizeof(AVFrame));
237 int avcodec_open(AVCodecContext *avctx, AVCodec *codec)
241 avctx->codec = codec;
242 avctx->codec_id = codec->id;
243 avctx->frame_number = 0;
244 if (codec->priv_data_size > 0) {
245 avctx->priv_data = av_mallocz(codec->priv_data_size);
246 if (!avctx->priv_data)
249 avctx->priv_data = NULL;
251 ret = avctx->codec->init(avctx);
253 av_freep(&avctx->priv_data);
259 int avcodec_encode_audio(AVCodecContext *avctx, UINT8 *buf, int buf_size,
260 const short *samples)
264 ret = avctx->codec->encode(avctx, buf, buf_size, (void *)samples);
265 avctx->frame_number++;
269 int avcodec_encode_video(AVCodecContext *avctx, UINT8 *buf, int buf_size,
274 ret = avctx->codec->encode(avctx, buf, buf_size, (void *)pict);
276 emms_c(); //needed to avoid a emms_c() call before every return;
278 avctx->frame_number++;
282 /* decode a frame. return -1 if error, otherwise return the number of
283 bytes used. If no frame could be decompressed, *got_picture_ptr is
284 zero. Otherwise, it is non zero */
285 int avcodec_decode_video(AVCodecContext *avctx, AVFrame *picture,
286 int *got_picture_ptr,
287 UINT8 *buf, int buf_size)
291 ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
294 emms_c(); //needed to avoid a emms_c() call before every return;
296 if (*got_picture_ptr)
297 avctx->frame_number++;
301 /* decode an audio frame. return -1 if error, otherwise return the
302 *number of bytes used. If no frame could be decompressed,
303 *frame_size_ptr is zero. Otherwise, it is the decompressed frame
305 int avcodec_decode_audio(AVCodecContext *avctx, INT16 *samples,
307 UINT8 *buf, int buf_size)
311 ret = avctx->codec->decode(avctx, samples, frame_size_ptr,
313 avctx->frame_number++;
317 int avcodec_close(AVCodecContext *avctx)
319 if (avctx->codec->close)
320 avctx->codec->close(avctx);
321 av_freep(&avctx->priv_data);
326 AVCodec *avcodec_find_encoder(enum CodecID id)
331 if (p->encode != NULL && p->id == id)
338 AVCodec *avcodec_find_encoder_by_name(const char *name)
343 if (p->encode != NULL && strcmp(name,p->name) == 0)
350 AVCodec *avcodec_find_decoder(enum CodecID id)
355 if (p->decode != NULL && p->id == id)
362 AVCodec *avcodec_find_decoder_by_name(const char *name)
367 if (p->decode != NULL && strcmp(name,p->name) == 0)
374 AVCodec *avcodec_find(enum CodecID id)
386 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
388 const char *codec_name;
391 char channels_str[100];
395 p = avcodec_find_encoder(enc->codec_id);
397 p = avcodec_find_decoder(enc->codec_id);
400 codec_name = p->name;
401 } else if (enc->codec_name[0] != '\0') {
402 codec_name = enc->codec_name;
404 /* output avi tags */
405 if (enc->codec_type == CODEC_TYPE_VIDEO) {
406 snprintf(buf1, sizeof(buf1), "%c%c%c%c",
407 enc->codec_tag & 0xff,
408 (enc->codec_tag >> 8) & 0xff,
409 (enc->codec_tag >> 16) & 0xff,
410 (enc->codec_tag >> 24) & 0xff);
412 snprintf(buf1, sizeof(buf1), "0x%04x", enc->codec_tag);
417 switch(enc->codec_type) {
418 case CODEC_TYPE_VIDEO:
419 snprintf(buf, buf_size,
421 codec_name, enc->flags & CODEC_FLAG_HQ ? " (hq)" : "");
422 if (enc->codec_id == CODEC_ID_RAWVIDEO) {
423 snprintf(buf + strlen(buf), buf_size - strlen(buf),
425 avcodec_get_pix_fmt_name(enc->pix_fmt));
428 snprintf(buf + strlen(buf), buf_size - strlen(buf),
429 ", %dx%d, %0.2f fps",
430 enc->width, enc->height,
431 (float)enc->frame_rate / FRAME_RATE_BASE);
434 snprintf(buf + strlen(buf), buf_size - strlen(buf),
435 ", q=%d-%d", enc->qmin, enc->qmax);
437 bitrate = enc->bit_rate;
439 case CODEC_TYPE_AUDIO:
440 snprintf(buf, buf_size,
443 switch (enc->channels) {
445 strcpy(channels_str, "mono");
448 strcpy(channels_str, "stereo");
451 strcpy(channels_str, "5:1");
454 sprintf(channels_str, "%d channels", enc->channels);
457 if (enc->sample_rate) {
458 snprintf(buf + strlen(buf), buf_size - strlen(buf),
464 /* for PCM codecs, compute bitrate directly */
465 switch(enc->codec_id) {
466 case CODEC_ID_PCM_S16LE:
467 case CODEC_ID_PCM_S16BE:
468 case CODEC_ID_PCM_U16LE:
469 case CODEC_ID_PCM_U16BE:
470 bitrate = enc->sample_rate * enc->channels * 16;
472 case CODEC_ID_PCM_S8:
473 case CODEC_ID_PCM_U8:
474 case CODEC_ID_PCM_ALAW:
475 case CODEC_ID_PCM_MULAW:
476 bitrate = enc->sample_rate * enc->channels * 8;
479 bitrate = enc->bit_rate;
487 if (enc->flags & CODEC_FLAG_PASS1)
488 snprintf(buf + strlen(buf), buf_size - strlen(buf),
490 if (enc->flags & CODEC_FLAG_PASS2)
491 snprintf(buf + strlen(buf), buf_size - strlen(buf),
495 snprintf(buf + strlen(buf), buf_size - strlen(buf),
496 ", %d kb/s", bitrate / 1000);
500 unsigned avcodec_version( void )
502 return LIBAVCODEC_VERSION_INT;
505 unsigned avcodec_build( void )
507 return LIBAVCODEC_BUILD;
510 /* must be called before any other functions */
511 void avcodec_init(void)
513 static int inited = 0;
522 /* this can be called after seeking and before trying to decode the next keyframe */
523 void avcodec_flush_buffers(AVCodecContext *avctx)
526 MpegEncContext *s = avctx->priv_data;
528 switch(avctx->codec_id){
529 case CODEC_ID_MPEG1VIDEO:
533 case CODEC_ID_MJPEGB:
535 case CODEC_ID_MSMPEG4V1:
536 case CODEC_ID_MSMPEG4V2:
537 case CODEC_ID_MSMPEG4V3:
543 for(i=0; i<MAX_PICTURE_COUNT; i++){
544 if(s->picture[i].data[0] && ( s->picture[i].type == FF_BUFFER_TYPE_INTERNAL
545 || s->picture[i].type == FF_BUFFER_TYPE_USER))
546 avctx->release_buffer(avctx, (AVFrame*)&s->picture[i]);
548 s->last_picture.data[0] = s->next_picture.data[0] = NULL;
556 static int raw_encode_init(AVCodecContext *s)
561 static int raw_decode_frame(AVCodecContext *avctx,
562 void *data, int *data_size,
563 UINT8 *buf, int buf_size)
568 static int raw_encode_frame(AVCodecContext *avctx,
569 unsigned char *frame, int buf_size, void *data)
574 AVCodec rawvideo_codec = {