3 * Copyright (c) 2001 Fabrice Bellard.
4 * Copyright (c) 2003 Michel Bardiaux for the av_log API
5 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #include "mpegvideo.h"
34 const uint8_t ff_sqrt_tab
[128]={
35 0, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5,
36 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
37 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
38 9, 9, 9, 9,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,11,11,11,11,11,11,11
41 const uint8_t ff_log2_tab
[256]={
42 0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
43 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
44 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
45 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
46 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
47 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
48 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
49 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7
52 const uint8_t ff_reverse
[256]={
53 0x00,0x80,0x40,0xC0,0x20,0xA0,0x60,0xE0,0x10,0x90,0x50,0xD0,0x30,0xB0,0x70,0xF0,
54 0x08,0x88,0x48,0xC8,0x28,0xA8,0x68,0xE8,0x18,0x98,0x58,0xD8,0x38,0xB8,0x78,0xF8,
55 0x04,0x84,0x44,0xC4,0x24,0xA4,0x64,0xE4,0x14,0x94,0x54,0xD4,0x34,0xB4,0x74,0xF4,
56 0x0C,0x8C,0x4C,0xCC,0x2C,0xAC,0x6C,0xEC,0x1C,0x9C,0x5C,0xDC,0x3C,0xBC,0x7C,0xFC,
57 0x02,0x82,0x42,0xC2,0x22,0xA2,0x62,0xE2,0x12,0x92,0x52,0xD2,0x32,0xB2,0x72,0xF2,
58 0x0A,0x8A,0x4A,0xCA,0x2A,0xAA,0x6A,0xEA,0x1A,0x9A,0x5A,0xDA,0x3A,0xBA,0x7A,0xFA,
59 0x06,0x86,0x46,0xC6,0x26,0xA6,0x66,0xE6,0x16,0x96,0x56,0xD6,0x36,0xB6,0x76,0xF6,
60 0x0E,0x8E,0x4E,0xCE,0x2E,0xAE,0x6E,0xEE,0x1E,0x9E,0x5E,0xDE,0x3E,0xBE,0x7E,0xFE,
61 0x01,0x81,0x41,0xC1,0x21,0xA1,0x61,0xE1,0x11,0x91,0x51,0xD1,0x31,0xB1,0x71,0xF1,
62 0x09,0x89,0x49,0xC9,0x29,0xA9,0x69,0xE9,0x19,0x99,0x59,0xD9,0x39,0xB9,0x79,0xF9,
63 0x05,0x85,0x45,0xC5,0x25,0xA5,0x65,0xE5,0x15,0x95,0x55,0xD5,0x35,0xB5,0x75,0xF5,
64 0x0D,0x8D,0x4D,0xCD,0x2D,0xAD,0x6D,0xED,0x1D,0x9D,0x5D,0xDD,0x3D,0xBD,0x7D,0xFD,
65 0x03,0x83,0x43,0xC3,0x23,0xA3,0x63,0xE3,0x13,0x93,0x53,0xD3,0x33,0xB3,0x73,0xF3,
66 0x0B,0x8B,0x4B,0xCB,0x2B,0xAB,0x6B,0xEB,0x1B,0x9B,0x5B,0xDB,0x3B,0xBB,0x7B,0xFB,
67 0x07,0x87,0x47,0xC7,0x27,0xA7,0x67,0xE7,0x17,0x97,0x57,0xD7,0x37,0xB7,0x77,0xF7,
68 0x0F,0x8F,0x4F,0xCF,0x2F,0xAF,0x6F,0xEF,0x1F,0x9F,0x5F,0xDF,0x3F,0xBF,0x7F,0xFF,
71 static int volatile entangled_thread_counter
=0;
73 void avcodec_default_free_buffers(AVCodecContext
*s
);
75 void *av_mallocz(unsigned int size
)
79 ptr
= av_malloc(size
);
86 char *av_strdup(const char *s
)
99 * realloc which does nothing if the block is large enough
101 void *av_fast_realloc(void *ptr
, unsigned int *size
, unsigned int min_size
)
106 *size
= FFMAX(17*min_size
/16 + 32, min_size
);
108 return av_realloc(ptr
, *size
);
112 static unsigned int last_static
= 0;
113 static unsigned int allocated_static
= 0;
114 static void** array_static
= NULL
;
117 * allocation of static arrays - do not use for normal allocation.
119 void *av_mallocz_static(unsigned int size
)
121 void *ptr
= av_mallocz(size
);
124 array_static
=av_fast_realloc(array_static
, &allocated_static
, sizeof(void*)*(last_static
+1));
127 array_static
[last_static
++] = ptr
;
134 * same as above, but does realloc
137 void *av_realloc_static(void *ptr
, unsigned int size
)
141 return av_mallocz_static(size
);
142 /* Look for the old ptr */
143 for(i
= 0; i
< last_static
; i
++) {
144 if(array_static
[i
] == ptr
) {
145 array_static
[i
] = av_realloc(array_static
[i
], size
);
146 return array_static
[i
];
154 * free all static arrays and reset pointers to 0.
156 void av_free_static(void)
159 av_freep(&array_static
[--last_static
]);
161 av_freep(&array_static
);
165 * Frees memory and sets the pointer to NULL.
166 * @param arg pointer to the pointer which should be freed
168 void av_freep(void *arg
)
170 void **ptr
= (void**)arg
;
175 /* encoder management */
176 AVCodec
*first_avcodec
= NULL
;
178 void register_avcodec(AVCodec
*format
)
182 while (*p
!= NULL
) p
= &(*p
)->next
;
187 void avcodec_set_dimensions(AVCodecContext
*s
, int width
, int height
){
188 s
->coded_width
= width
;
189 s
->coded_height
= height
;
190 s
->width
= -((-width
)>>s
->lowres
);
191 s
->height
= -((-height
)>>s
->lowres
);
194 typedef struct InternalBuffer
{
201 #define INTERNAL_BUFFER_SIZE 32
203 #define ALIGN(x, a) (((x)+(a)-1)&~((a)-1))
205 void avcodec_align_dimensions(AVCodecContext
*s
, int *width
, int *height
){
210 case PIX_FMT_YUV420P
:
212 case PIX_FMT_UYVY422
:
213 case PIX_FMT_YUV422P
:
214 case PIX_FMT_YUV444P
:
216 case PIX_FMT_YUVJ420P
:
217 case PIX_FMT_YUVJ422P
:
218 case PIX_FMT_YUVJ444P
:
219 w_align
= 16; //FIXME check for non mpeg style codecs and use less alignment
222 case PIX_FMT_YUV411P
:
223 case PIX_FMT_UYVY411
:
227 case PIX_FMT_YUV410P
:
228 if(s
->codec_id
== CODEC_ID_SVQ1
){
233 if(s
->codec_id
== CODEC_ID_RPZA
){
238 if(s
->codec_id
== CODEC_ID_SMC
){
244 if((s
->codec_id
== CODEC_ID_MSZH
) || (s
->codec_id
== CODEC_ID_ZLIB
)){
255 *width
= ALIGN(*width
, w_align
);
256 *height
= ALIGN(*height
, h_align
);
259 int avcodec_check_dimensions(void *av_log_ctx
, unsigned int w
, unsigned int h
){
260 if((int)w
>0 && (int)h
>0 && (w
+128)*(uint64_t)(h
+128) < INT_MAX
/4)
263 av_log(av_log_ctx
, AV_LOG_ERROR
, "picture size invalid (%ux%u)\n", w
, h
);
267 int avcodec_default_get_buffer(AVCodecContext
*s
, AVFrame
*pic
){
274 assert(pic
->data
[0]==NULL
);
275 assert(INTERNAL_BUFFER_SIZE
> s
->internal_buffer_count
);
277 if(avcodec_check_dimensions(s
,w
,h
))
280 if(s
->internal_buffer
==NULL
){
281 s
->internal_buffer
= av_mallocz(INTERNAL_BUFFER_SIZE
*sizeof(InternalBuffer
));
284 s
->internal_buffer
= av_fast_realloc(
286 &s
->internal_buffer_size
,
287 sizeof(InternalBuffer
)*FFMAX(99, s
->internal_buffer_count
+1)/*FIXME*/
291 buf
= &((InternalBuffer
*)s
->internal_buffer
)[s
->internal_buffer_count
];
292 picture_number
= &(((InternalBuffer
*)s
->internal_buffer
)[INTERNAL_BUFFER_SIZE
-1]).last_pic_num
; //FIXME ugly hack
296 pic
->age
= *picture_number
- buf
->last_pic_num
;
297 buf
->last_pic_num
= *picture_number
;
299 int h_chroma_shift
, v_chroma_shift
;
302 avcodec_get_chroma_sub_sample(s
->pix_fmt
, &h_chroma_shift
, &v_chroma_shift
);
308 case PIX_FMT_UYVY422
:
322 avcodec_align_dimensions(s
, &w
, &h
);
324 if(!(s
->flags
&CODEC_FLAG_EMU_EDGE
)){
329 buf
->last_pic_num
= -256*256*256*64;
332 const int h_shift
= i
==0 ? 0 : h_chroma_shift
;
333 const int v_shift
= i
==0 ? 0 : v_chroma_shift
;
335 if(s
->pix_fmt
== PIX_FMT_PAL8
&& i
== 1)
337 buf
->base
[i
] = av_malloc(256 * 4);
338 if(buf
->base
[i
] == NULL
)
340 buf
->data
[i
] = buf
->base
[i
];
344 //FIXME next ensures that linesize= 2^x uvlinesize, thats needed because some MC code assumes it
345 buf
->linesize
[i
]= ALIGN(pixel_size
*w
>>h_shift
, STRIDE_ALIGN
<<(h_chroma_shift
-h_shift
));
347 buf
->base
[i
]= av_malloc((buf
->linesize
[i
]*h
>>v_shift
)+16); //FIXME 16
348 if(buf
->base
[i
]==NULL
) return -1;
349 memset(buf
->base
[i
], 128, buf
->linesize
[i
]*h
>>v_shift
);
351 if(s
->flags
&CODEC_FLAG_EMU_EDGE
)
352 buf
->data
[i
] = buf
->base
[i
];
354 buf
->data
[i
] = buf
->base
[i
] + ALIGN((buf
->linesize
[i
]*EDGE_WIDTH
>>v_shift
) + (EDGE_WIDTH
>>h_shift
), STRIDE_ALIGN
);
356 pic
->age
= 256*256*256*64;
358 pic
->type
= FF_BUFFER_TYPE_INTERNAL
;
361 pic
->base
[i
]= buf
->base
[i
];
362 pic
->data
[i
]= buf
->data
[i
];
363 pic
->linesize
[i
]= buf
->linesize
[i
];
365 s
->internal_buffer_count
++;
370 void avcodec_default_release_buffer(AVCodecContext
*s
, AVFrame
*pic
){
372 InternalBuffer
*buf
, *last
, temp
;
374 assert(pic
->type
==FF_BUFFER_TYPE_INTERNAL
);
375 assert(s
->internal_buffer_count
);
377 buf
= NULL
; /* avoids warning */
378 for(i
=0; i
<s
->internal_buffer_count
; i
++){ //just 3-5 checks so is not worth to optimize
379 buf
= &((InternalBuffer
*)s
->internal_buffer
)[i
];
380 if(buf
->data
[0] == pic
->data
[0])
383 assert(i
< s
->internal_buffer_count
);
384 s
->internal_buffer_count
--;
385 last
= &((InternalBuffer
*)s
->internal_buffer
)[s
->internal_buffer_count
];
393 // pic->base[i]=NULL;
395 //printf("R%X\n", pic->opaque);
398 int avcodec_default_reget_buffer(AVCodecContext
*s
, AVFrame
*pic
){
402 /* If no picture return a new buffer */
403 if(pic
->data
[0] == NULL
) {
404 /* We will copy from buffer, so must be readable */
405 pic
->buffer_hints
|= FF_BUFFER_HINTS_READABLE
;
406 return s
->get_buffer(s
, pic
);
409 /* If internal buffer type return the same buffer */
410 if(pic
->type
== FF_BUFFER_TYPE_INTERNAL
)
414 * Not internal type and reget_buffer not overridden, emulate cr buffer
417 for(i
= 0; i
< 4; i
++)
418 pic
->data
[i
] = pic
->base
[i
] = NULL
;
420 /* Allocate new frame */
421 if (s
->get_buffer(s
, pic
))
423 /* Copy image data from old buffer to new buffer */
424 img_copy((AVPicture
*)pic
, (AVPicture
*)&temp_pic
, s
->pix_fmt
, s
->width
,
426 s
->release_buffer(s
, &temp_pic
); // Release old frame
430 int avcodec_default_execute(AVCodecContext
*c
, int (*func
)(AVCodecContext
*c2
, void *arg2
),void **arg
, int *ret
, int count
){
433 for(i
=0; i
<count
; i
++){
434 int r
= func(c
, arg
[i
]);
440 enum PixelFormat
avcodec_default_get_format(struct AVCodecContext
*s
, const enum PixelFormat
* fmt
){
444 static const char* context_to_name(void* ptr
) {
445 AVCodecContext
*avc
= ptr
;
447 if(avc
&& avc
->codec
&& avc
->codec
->name
)
448 return avc
->codec
->name
;
453 static AVClass av_codec_context_class
= { "AVCodecContext", context_to_name
};
455 void avcodec_get_context_defaults(AVCodecContext
*s
){
456 memset(s
, 0, sizeof(AVCodecContext
));
458 s
->av_class
= &av_codec_context_class
;
459 s
->bit_rate
= 800*1000;
460 s
->bit_rate_tolerance
= s
->bit_rate
*10;
463 s
->mb_lmin
= FF_QP2LAMBDA
* 2;
464 s
->mb_lmax
= FF_QP2LAMBDA
* 31;
465 s
->rc_eq
= "tex^qComp";
468 s
->b_quant_factor
=1.25;
469 s
->b_quant_offset
=1.25;
470 s
->i_quant_factor
=-0.8;
471 s
->i_quant_offset
=0.0;
472 s
->error_concealment
= 3;
473 s
->error_resilience
= 1;
474 s
->workaround_bugs
= FF_BUG_AUTODETECT
;
475 s
->time_base
= (AVRational
){0,1};
477 s
->me_method
= ME_EPZS
;
478 s
->get_buffer
= avcodec_default_get_buffer
;
479 s
->release_buffer
= avcodec_default_release_buffer
;
480 s
->get_format
= avcodec_default_get_format
;
481 s
->execute
= avcodec_default_execute
;
483 s
->me_subpel_quality
=8;
484 s
->lmin
= FF_QP2LAMBDA
* s
->qmin
;
485 s
->lmax
= FF_QP2LAMBDA
* s
->qmax
;
486 s
->sample_aspect_ratio
= (AVRational
){0,1};
487 s
->ildct_cmp
= FF_CMP_VSAD
;
488 s
->profile
= FF_PROFILE_UNKNOWN
;
489 s
->level
= FF_LEVEL_UNKNOWN
;
490 s
->me_penalty_compensation
= 256;
491 s
->pix_fmt
= PIX_FMT_NONE
;
493 s
->intra_quant_bias
= FF_DEFAULT_QUANT_BIAS
;
494 s
->inter_quant_bias
= FF_DEFAULT_QUANT_BIAS
;
496 s
->reget_buffer
= avcodec_default_reget_buffer
;
500 * allocates a AVCodecContext and set it to defaults.
501 * this can be deallocated by simply calling free()
503 AVCodecContext
*avcodec_alloc_context(void){
504 AVCodecContext
*avctx
= av_malloc(sizeof(AVCodecContext
));
506 if(avctx
==NULL
) return NULL
;
508 avcodec_get_context_defaults(avctx
);
513 void avcodec_get_frame_defaults(AVFrame
*pic
){
514 memset(pic
, 0, sizeof(AVFrame
));
516 pic
->pts
= AV_NOPTS_VALUE
;
521 * allocates a AVPFrame and set it to defaults.
522 * this can be deallocated by simply calling free()
524 AVFrame
*avcodec_alloc_frame(void){
525 AVFrame
*pic
= av_malloc(sizeof(AVFrame
));
527 if(pic
==NULL
) return NULL
;
529 avcodec_get_frame_defaults(pic
);
534 int avcodec_open(AVCodecContext
*avctx
, AVCodec
*codec
)
538 entangled_thread_counter
++;
539 if(entangled_thread_counter
!= 1){
540 av_log(avctx
, AV_LOG_ERROR
, "insufficient thread locking around avcodec_open/close()\n");
547 avctx
->codec
= codec
;
548 avctx
->codec_id
= codec
->id
;
549 avctx
->frame_number
= 0;
550 if (codec
->priv_data_size
> 0) {
551 avctx
->priv_data
= av_mallocz(codec
->priv_data_size
);
552 if (!avctx
->priv_data
)
555 avctx
->priv_data
= NULL
;
558 if(avctx
->coded_width
&& avctx
->coded_height
)
559 avcodec_set_dimensions(avctx
, avctx
->coded_width
, avctx
->coded_height
);
560 else if(avctx
->width
&& avctx
->height
)
561 avcodec_set_dimensions(avctx
, avctx
->width
, avctx
->height
);
563 if((avctx
->coded_width
||avctx
->coded_height
) && avcodec_check_dimensions(avctx
,avctx
->coded_width
,avctx
->coded_height
)){
564 av_freep(&avctx
->priv_data
);
568 ret
= avctx
->codec
->init(avctx
);
570 av_freep(&avctx
->priv_data
);
575 entangled_thread_counter
--;
579 int avcodec_encode_audio(AVCodecContext
*avctx
, uint8_t *buf
, int buf_size
,
580 const short *samples
)
582 if(buf_size
< FF_MIN_BUFFER_SIZE
&& 0){
583 av_log(avctx
, AV_LOG_ERROR
, "buffer smaller then minimum size\n");
586 if((avctx
->codec
->capabilities
& CODEC_CAP_DELAY
) || samples
){
587 int ret
= avctx
->codec
->encode(avctx
, buf
, buf_size
, (void *)samples
);
588 avctx
->frame_number
++;
594 int avcodec_encode_video(AVCodecContext
*avctx
, uint8_t *buf
, int buf_size
,
597 if(buf_size
< FF_MIN_BUFFER_SIZE
){
598 av_log(avctx
, AV_LOG_ERROR
, "buffer smaller then minimum size\n");
601 if(avcodec_check_dimensions(avctx
,avctx
->width
,avctx
->height
))
603 if((avctx
->codec
->capabilities
& CODEC_CAP_DELAY
) || pict
){
604 int ret
= avctx
->codec
->encode(avctx
, buf
, buf_size
, (void *)pict
);
605 avctx
->frame_number
++;
606 emms_c(); //needed to avoid an emms_c() call before every return;
613 int avcodec_encode_subtitle(AVCodecContext
*avctx
, uint8_t *buf
, int buf_size
,
614 const AVSubtitle
*sub
)
617 ret
= avctx
->codec
->encode(avctx
, buf
, buf_size
, (void *)sub
);
618 avctx
->frame_number
++;
624 * @param buf bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE larger then the actual read bytes
625 * because some optimized bitstream readers read 32 or 64 bit at once and could read over the end
626 * @param buf_size the size of the buffer in bytes
627 * @param got_picture_ptr zero if no frame could be decompressed, Otherwise, it is non zero
628 * @return -1 if error, otherwise return the number of
631 int avcodec_decode_video(AVCodecContext
*avctx
, AVFrame
*picture
,
632 int *got_picture_ptr
,
633 uint8_t *buf
, int buf_size
)
638 if((avctx
->coded_width
||avctx
->coded_height
) && avcodec_check_dimensions(avctx
,avctx
->coded_width
,avctx
->coded_height
))
640 if((avctx
->codec
->capabilities
& CODEC_CAP_DELAY
) || buf_size
){
641 ret
= avctx
->codec
->decode(avctx
, picture
, got_picture_ptr
,
644 emms_c(); //needed to avoid an emms_c() call before every return;
646 if (*got_picture_ptr
)
647 avctx
->frame_number
++;
654 /* decode an audio frame. return -1 if error, otherwise return the
655 *number of bytes used. If no frame could be decompressed,
656 *frame_size_ptr is zero. Otherwise, it is the decompressed frame
658 int avcodec_decode_audio(AVCodecContext
*avctx
, int16_t *samples
,
660 uint8_t *buf
, int buf_size
)
665 if((avctx
->codec
->capabilities
& CODEC_CAP_DELAY
) || buf_size
){
666 ret
= avctx
->codec
->decode(avctx
, samples
, frame_size_ptr
,
668 avctx
->frame_number
++;
674 /* decode a subtitle message. return -1 if error, otherwise return the
675 *number of bytes used. If no subtitle could be decompressed,
676 *got_sub_ptr is zero. Otherwise, the subtitle is stored in *sub. */
677 int avcodec_decode_subtitle(AVCodecContext
*avctx
, AVSubtitle
*sub
,
679 const uint8_t *buf
, int buf_size
)
684 ret
= avctx
->codec
->decode(avctx
, sub
, got_sub_ptr
,
685 (uint8_t *)buf
, buf_size
);
687 avctx
->frame_number
++;
691 int avcodec_close(AVCodecContext
*avctx
)
693 entangled_thread_counter
++;
694 if(entangled_thread_counter
!= 1){
695 av_log(avctx
, AV_LOG_ERROR
, "insufficient thread locking around avcodec_open/close()\n");
696 entangled_thread_counter
--;
700 if (avctx
->codec
->close
)
701 avctx
->codec
->close(avctx
);
702 avcodec_default_free_buffers(avctx
);
703 av_freep(&avctx
->priv_data
);
705 entangled_thread_counter
--;
709 AVCodec
*avcodec_find_encoder(enum CodecID id
)
714 if (p
->encode
!= NULL
&& p
->id
== id
)
721 AVCodec
*avcodec_find_encoder_by_name(const char *name
)
726 if (p
->encode
!= NULL
&& strcmp(name
,p
->name
) == 0)
733 AVCodec
*avcodec_find_decoder(enum CodecID id
)
738 if (p
->decode
!= NULL
&& p
->id
== id
)
745 AVCodec
*avcodec_find_decoder_by_name(const char *name
)
750 if (p
->decode
!= NULL
&& strcmp(name
,p
->name
) == 0)
757 void avcodec_string(char *buf
, int buf_size
, AVCodecContext
*enc
, int encode
)
759 const char *codec_name
;
762 char channels_str
[100];
766 p
= avcodec_find_encoder(enc
->codec_id
);
768 p
= avcodec_find_decoder(enc
->codec_id
);
771 codec_name
= p
->name
;
772 if (!encode
&& enc
->codec_id
== CODEC_ID_MP3
) {
773 if (enc
->sub_id
== 2)
775 else if (enc
->sub_id
== 1)
778 } else if (enc
->codec_id
== CODEC_ID_MPEG2TS
) {
779 /* fake mpeg2 transport stream codec (currently not
781 codec_name
= "mpeg2ts";
782 } else if (enc
->codec_name
[0] != '\0') {
783 codec_name
= enc
->codec_name
;
785 /* output avi tags */
786 if (enc
->codec_type
== CODEC_TYPE_VIDEO
) {
787 snprintf(buf1
, sizeof(buf1
), "%c%c%c%c",
788 enc
->codec_tag
& 0xff,
789 (enc
->codec_tag
>> 8) & 0xff,
790 (enc
->codec_tag
>> 16) & 0xff,
791 (enc
->codec_tag
>> 24) & 0xff);
793 snprintf(buf1
, sizeof(buf1
), "0x%04x", enc
->codec_tag
);
798 switch(enc
->codec_type
) {
799 case CODEC_TYPE_VIDEO
:
800 snprintf(buf
, buf_size
,
802 codec_name
, enc
->mb_decision
? " (hq)" : "");
803 if (enc
->pix_fmt
!= PIX_FMT_NONE
) {
804 snprintf(buf
+ strlen(buf
), buf_size
- strlen(buf
),
806 avcodec_get_pix_fmt_name(enc
->pix_fmt
));
809 snprintf(buf
+ strlen(buf
), buf_size
- strlen(buf
),
810 ", %dx%d, %0.2f fps",
811 enc
->width
, enc
->height
,
812 1/av_q2d(enc
->time_base
));
815 snprintf(buf
+ strlen(buf
), buf_size
- strlen(buf
),
816 ", q=%d-%d", enc
->qmin
, enc
->qmax
);
818 bitrate
= enc
->bit_rate
;
820 case CODEC_TYPE_AUDIO
:
821 snprintf(buf
, buf_size
,
824 switch (enc
->channels
) {
826 strcpy(channels_str
, "mono");
829 strcpy(channels_str
, "stereo");
832 strcpy(channels_str
, "5:1");
835 snprintf(channels_str
, sizeof(channels_str
), "%d channels", enc
->channels
);
838 if (enc
->sample_rate
) {
839 snprintf(buf
+ strlen(buf
), buf_size
- strlen(buf
),
845 /* for PCM codecs, compute bitrate directly */
846 switch(enc
->codec_id
) {
847 case CODEC_ID_PCM_S16LE
:
848 case CODEC_ID_PCM_S16BE
:
849 case CODEC_ID_PCM_U16LE
:
850 case CODEC_ID_PCM_U16BE
:
851 bitrate
= enc
->sample_rate
* enc
->channels
* 16;
853 case CODEC_ID_PCM_S8
:
854 case CODEC_ID_PCM_U8
:
855 case CODEC_ID_PCM_ALAW
:
856 case CODEC_ID_PCM_MULAW
:
857 bitrate
= enc
->sample_rate
* enc
->channels
* 8;
860 bitrate
= enc
->bit_rate
;
864 case CODEC_TYPE_DATA
:
865 snprintf(buf
, buf_size
, "Data: %s", codec_name
);
866 bitrate
= enc
->bit_rate
;
868 case CODEC_TYPE_SUBTITLE
:
869 snprintf(buf
, buf_size
, "Subtitle: %s", codec_name
);
870 bitrate
= enc
->bit_rate
;
873 snprintf(buf
, buf_size
, "Invalid Codec type %d", enc
->codec_type
);
877 if (enc
->flags
& CODEC_FLAG_PASS1
)
878 snprintf(buf
+ strlen(buf
), buf_size
- strlen(buf
),
880 if (enc
->flags
& CODEC_FLAG_PASS2
)
881 snprintf(buf
+ strlen(buf
), buf_size
- strlen(buf
),
885 snprintf(buf
+ strlen(buf
), buf_size
- strlen(buf
),
886 ", %d kb/s", bitrate
/ 1000);
890 unsigned avcodec_version( void )
892 return LIBAVCODEC_VERSION_INT
;
895 unsigned avcodec_build( void )
897 return LIBAVCODEC_BUILD
;
900 /* must be called before any other functions */
901 void avcodec_init(void)
903 static int inited
= 0;
909 dsputil_static_init();
913 * Flush buffers, should be called when seeking or when swicthing to a different stream.
915 void avcodec_flush_buffers(AVCodecContext
*avctx
)
917 if(avctx
->codec
->flush
)
918 avctx
->codec
->flush(avctx
);
921 void avcodec_default_free_buffers(AVCodecContext
*s
){
924 if(s
->internal_buffer
==NULL
) return;
926 for(i
=0; i
<INTERNAL_BUFFER_SIZE
; i
++){
927 InternalBuffer
*buf
= &((InternalBuffer
*)s
->internal_buffer
)[i
];
929 av_freep(&buf
->base
[j
]);
933 av_freep(&s
->internal_buffer
);
935 s
->internal_buffer_count
=0;
938 char av_get_pict_type_char(int pict_type
){
940 case I_TYPE
: return 'I';
941 case P_TYPE
: return 'P';
942 case B_TYPE
: return 'B';
943 case S_TYPE
: return 'S';
944 case SI_TYPE
:return 'i';
945 case SP_TYPE
:return 'p';
950 int av_reduce(int *dst_nom
, int *dst_den
, int64_t nom
, int64_t den
, int64_t max
){
951 AVRational a0
={0,1}, a1
={1,0};
952 int sign
= (nom
<0) ^ (den
<0);
953 int64_t gcd
= ff_gcd(ABS(nom
), ABS(den
));
957 if(nom
<=max
&& den
<=max
){
958 a1
= (AVRational
){nom
, den
};
963 int64_t x
= nom
/ den
;
964 int64_t next_den
= nom
- den
*x
;
965 int64_t a2n
= x
*a1
.num
+ a0
.num
;
966 int64_t a2d
= x
*a1
.den
+ a0
.den
;
968 if(a2n
> max
|| a2d
> max
) break;
971 a1
= (AVRational
){a2n
, a2d
};
975 assert(ff_gcd(a1
.num
, a1
.den
) == 1);
977 *dst_nom
= sign
? -a1
.num
: a1
.num
;
983 int64_t av_rescale_rnd(int64_t a
, int64_t b
, int64_t c
, enum AVRounding rnd
){
988 assert(rnd
>=0 && rnd
<=5 && rnd
!=4);
990 if(a
<0 && a
!= INT64_MIN
) return -av_rescale_rnd(-a
, b
, c
, rnd
^ ((rnd
>>1)&1));
992 if(rnd
==AV_ROUND_NEAR_INF
) r
= c
/2;
993 else if(rnd
&1) r
= c
-1;
995 if(b
<=INT_MAX
&& c
<=INT_MAX
){
997 return (a
* b
+ r
)/c
;
999 return a
/c
*b
+ (a
%c
*b
+ r
)/c
;
1002 ai
= av_mul_i(av_int2i(a
), av_int2i(b
));
1003 ai
= av_add_i(ai
, av_int2i(r
));
1005 return av_i2int(av_div_i(ai
, av_int2i(c
)));
1008 int64_t av_rescale(int64_t a
, int64_t b
, int64_t c
){
1009 return av_rescale_rnd(a
, b
, c
, AV_ROUND_NEAR_INF
);
1012 int64_t av_rescale_q(int64_t a
, AVRational bq
, AVRational cq
){
1013 int64_t b
= bq
.num
* (int64_t)cq
.den
;
1014 int64_t c
= cq
.num
* (int64_t)bq
.den
;
1015 return av_rescale_rnd(a
, b
, c
, AV_ROUND_NEAR_INF
);
1018 int64_t ff_gcd(int64_t a
, int64_t b
){
1019 if(b
) return ff_gcd(b
, a
%b
);
1023 double av_int2dbl(int64_t v
){
1024 if(v
+v
> 0xFFELLU
<<52)
1026 return ldexp(((v
&(1LL<<52)-1) + (1LL<<52)) * (v
>>63|1), (v
>>52&0x7FF)-1075);
1029 float av_int2flt(int32_t v
){
1030 if(v
+v
> 0xFF000000U
)
1032 return ldexp(((v
&0x7FFFFF) + (1<<23)) * (v
>>31|1), (v
>>23&0xFF)-150);
1035 int64_t av_dbl2int(double d
){
1038 else if(d
-d
) return 0x7FF0000000000000LL
+ ((int64_t)(d
<0)<<63) + (d
!=d
);
1040 return (int64_t)(d
<0)<<63 | (e
+1022LL)<<52 | (int64_t)((fabs(d
)-0.5)*(1LL<<53));
1043 int32_t av_flt2int(float d
){
1046 else if(d
-d
) return 0x7F800000 + ((d
<0)<<31) + (d
!=d
);
1048 return (d
<0)<<31 | (e
+126)<<23 | (int64_t)((fabs(d
)-0.5)*(1<<24));
1053 static int av_log_level
= AV_LOG_INFO
;
1055 static void av_log_default_callback(void* ptr
, int level
, const char* fmt
, va_list vl
)
1057 static int print_prefix
=1;
1058 AVClass
* avc
= ptr
? *(AVClass
**)ptr
: NULL
;
1059 if(level
>av_log_level
)
1062 if(print_prefix
&& avc
) {
1063 fprintf(stderr
, "[%s @ %p]", avc
->item_name(ptr
), avc
);
1065 #define fprintf please_use_av_log
1067 print_prefix
= strstr(fmt
, "\n") != NULL
;
1069 vfprintf(stderr
, fmt
, vl
);
1072 static void (*av_log_callback
)(void*, int, const char*, va_list) = av_log_default_callback
;
1074 void av_log(void* avcl
, int level
, const char *fmt
, ...)
1078 av_vlog(avcl
, level
, fmt
, vl
);
1082 void av_vlog(void* avcl
, int level
, const char *fmt
, va_list vl
)
1084 av_log_callback(avcl
, level
, fmt
, vl
);
1087 int av_log_get_level(void)
1089 return av_log_level
;
1092 void av_log_set_level(int level
)
1094 av_log_level
= level
;
1097 void av_log_set_callback(void (*callback
)(void*, int, const char*, va_list))
1099 av_log_callback
= callback
;
1102 #if !defined(HAVE_THREADS)
1103 int avcodec_thread_init(AVCodecContext
*s
, int thread_count
){
1108 unsigned int av_xiphlacing(unsigned char *s
, unsigned int v
)