3 * Copyright (c) 2001 Fabrice Bellard
4 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
6 * This file is part of FFmpeg.
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 * @file libavcodec/utils.c
28 /* needed for mkstemp() */
29 #define _XOPEN_SOURCE 600
31 #include "libavutil/avstring.h"
32 #include "libavutil/integer.h"
33 #include "libavutil/crc.h"
34 #include "libavutil/pixdesc.h"
38 #include "imgconvert.h"
39 #include "audioconvert.h"
40 #include "libxvid_internal.h"
50 static int volatile entangled_thread_counter
=0;
51 int (*ff_lockmgr_cb
)(void **mutex
, enum AVLockOp op
);
52 static void *codec_mutex
;
54 void *av_fast_realloc(void *ptr
, unsigned int *size
, unsigned int min_size
)
59 *size
= FFMAX(17*min_size
/16 + 32, min_size
);
61 ptr
= av_realloc(ptr
, *size
);
62 if(!ptr
) //we could set this to the unmodified min_size but this is safer if the user lost the ptr and uses NULL now
68 void av_fast_malloc(void *ptr
, unsigned int *size
, unsigned int min_size
)
73 *size
= FFMAX(17*min_size
/16 + 32, min_size
);
75 *p
= av_malloc(*size
);
79 /* encoder management */
80 static AVCodec
*first_avcodec
= NULL
;
82 AVCodec
*av_codec_next(AVCodec
*c
){
84 else return first_avcodec
;
87 void avcodec_register(AVCodec
*codec
)
92 while (*p
!= NULL
) p
= &(*p
)->next
;
97 #if LIBAVCODEC_VERSION_MAJOR < 53
98 void register_avcodec(AVCodec
*codec
)
100 avcodec_register(codec
);
104 unsigned avcodec_get_edge_width(void)
109 void avcodec_set_dimensions(AVCodecContext
*s
, int width
, int height
){
110 s
->coded_width
= width
;
111 s
->coded_height
= height
;
112 s
->width
= -((-width
)>>s
->lowres
);
113 s
->height
= -((-height
)>>s
->lowres
);
116 typedef struct InternalBuffer
{
122 enum PixelFormat pix_fmt
;
125 #define INTERNAL_BUFFER_SIZE 32
127 void avcodec_align_dimensions2(AVCodecContext
*s
, int *width
, int *height
, int linesize_align
[4]){
132 case PIX_FMT_YUV420P
:
133 case PIX_FMT_YUYV422
:
134 case PIX_FMT_UYVY422
:
135 case PIX_FMT_YUV422P
:
136 case PIX_FMT_YUV440P
:
137 case PIX_FMT_YUV444P
:
139 case PIX_FMT_GRAY16BE
:
140 case PIX_FMT_GRAY16LE
:
141 case PIX_FMT_YUVJ420P
:
142 case PIX_FMT_YUVJ422P
:
143 case PIX_FMT_YUVJ440P
:
144 case PIX_FMT_YUVJ444P
:
145 case PIX_FMT_YUVA420P
:
146 w_align
= 16; //FIXME check for non mpeg style codecs and use less alignment
148 if(s
->codec_id
== CODEC_ID_MPEG2VIDEO
|| s
->codec_id
== CODEC_ID_MJPEG
|| s
->codec_id
== CODEC_ID_AMV
|| s
->codec_id
== CODEC_ID_THP
)
149 h_align
= 32; // interlaced is rounded up to 2 MBs
151 case PIX_FMT_YUV411P
:
152 case PIX_FMT_UYYVYY411
:
156 case PIX_FMT_YUV410P
:
157 if(s
->codec_id
== CODEC_ID_SVQ1
){
162 if(s
->codec_id
== CODEC_ID_RPZA
){
169 if(s
->codec_id
== CODEC_ID_SMC
){
175 if((s
->codec_id
== CODEC_ID_MSZH
) || (s
->codec_id
== CODEC_ID_ZLIB
)){
186 *width
= FFALIGN(*width
, w_align
);
187 *height
= FFALIGN(*height
, h_align
);
188 if(s
->codec_id
== CODEC_ID_H264
)
189 *height
+=2; // some of the optimized chroma MC reads one line too much
194 linesize_align
[3] = STRIDE_ALIGN
;
195 //STRIDE_ALIGN is 8 for SSE* but this does not work for SVQ1 chroma planes
196 //we could change STRIDE_ALIGN to 16 for x86/sse but it would increase the
197 //picture size unneccessarily in some cases. The solution here is not
198 //pretty and better ideas are welcome!
200 if(s
->codec_id
== CODEC_ID_SVQ1
|| s
->codec_id
== CODEC_ID_VP5
||
201 s
->codec_id
== CODEC_ID_VP6
|| s
->codec_id
== CODEC_ID_VP6F
||
202 s
->codec_id
== CODEC_ID_VP6A
) {
205 linesize_align
[2] = 16;
210 void avcodec_align_dimensions(AVCodecContext
*s
, int *width
, int *height
){
211 int chroma_shift
= av_pix_fmt_descriptors
[s
->pix_fmt
].log2_chroma_w
;
212 int linesize_align
[4];
214 avcodec_align_dimensions2(s
, width
, height
, linesize_align
);
215 align
= FFMAX(linesize_align
[0], linesize_align
[3]);
216 linesize_align
[1] <<= chroma_shift
;
217 linesize_align
[2] <<= chroma_shift
;
218 align
= FFMAX3(align
, linesize_align
[1], linesize_align
[2]);
219 *width
=FFALIGN(*width
, align
);
222 int avcodec_check_dimensions(void *av_log_ctx
, unsigned int w
, unsigned int h
){
223 if((int)w
>0 && (int)h
>0 && (w
+128)*(uint64_t)(h
+128) < INT_MAX
/8)
226 av_log(av_log_ctx
, AV_LOG_ERROR
, "picture size invalid (%ux%u)\n", w
, h
);
230 int avcodec_default_get_buffer(AVCodecContext
*s
, AVFrame
*pic
){
237 if(pic
->data
[0]!=NULL
) {
238 av_log(s
, AV_LOG_ERROR
, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
241 if(s
->internal_buffer_count
>= INTERNAL_BUFFER_SIZE
) {
242 av_log(s
, AV_LOG_ERROR
, "internal_buffer_count overflow (missing release_buffer?)\n");
246 if(avcodec_check_dimensions(s
,w
,h
))
249 if(s
->internal_buffer
==NULL
){
250 s
->internal_buffer
= av_mallocz((INTERNAL_BUFFER_SIZE
+1)*sizeof(InternalBuffer
));
253 s
->internal_buffer
= av_fast_realloc(
255 &s
->internal_buffer_size
,
256 sizeof(InternalBuffer
)*FFMAX(99, s
->internal_buffer_count
+1)/*FIXME*/
260 buf
= &((InternalBuffer
*)s
->internal_buffer
)[s
->internal_buffer_count
];
261 picture_number
= &(((InternalBuffer
*)s
->internal_buffer
)[INTERNAL_BUFFER_SIZE
]).last_pic_num
; //FIXME ugly hack
264 if(buf
->base
[0] && (buf
->width
!= w
|| buf
->height
!= h
|| buf
->pix_fmt
!= s
->pix_fmt
)){
266 av_freep(&buf
->base
[i
]);
272 pic
->age
= *picture_number
- buf
->last_pic_num
;
273 buf
->last_pic_num
= *picture_number
;
275 int h_chroma_shift
, v_chroma_shift
;
282 avcodec_get_chroma_sub_sample(s
->pix_fmt
, &h_chroma_shift
, &v_chroma_shift
);
284 avcodec_align_dimensions2(s
, &w
, &h
, stride_align
);
286 if(!(s
->flags
&CODEC_FLAG_EMU_EDGE
)){
292 // NOTE: do not align linesizes individually, this breaks e.g. assumptions
293 // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
294 ff_fill_linesize(&picture
, s
->pix_fmt
, w
);
295 // increase alignment of w for next try (rhs gives the lowest bit set in w)
300 unaligned
|= picture
.linesize
[i
] % stride_align
[i
];
304 tmpsize
= ff_fill_pointer(&picture
, NULL
, s
->pix_fmt
, h
);
308 for (i
=0; i
<3 && picture
.data
[i
+1]; i
++)
309 size
[i
] = picture
.data
[i
+1] - picture
.data
[i
];
310 size
[i
] = tmpsize
- (picture
.data
[i
] - picture
.data
[0]);
312 buf
->last_pic_num
= -256*256*256*64;
313 memset(buf
->base
, 0, sizeof(buf
->base
));
314 memset(buf
->data
, 0, sizeof(buf
->data
));
316 for(i
=0; i
<4 && size
[i
]; i
++){
317 const int h_shift
= i
==0 ?
0 : h_chroma_shift
;
318 const int v_shift
= i
==0 ?
0 : v_chroma_shift
;
320 buf
->linesize
[i
]= picture
.linesize
[i
];
322 buf
->base
[i
]= av_malloc(size
[i
]+16); //FIXME 16
323 if(buf
->base
[i
]==NULL
) return -1;
324 memset(buf
->base
[i
], 128, size
[i
]);
326 // no edge if EDEG EMU or not planar YUV
327 if((s
->flags
&CODEC_FLAG_EMU_EDGE
) || !size
[2])
328 buf
->data
[i
] = buf
->base
[i
];
330 buf
->data
[i
] = buf
->base
[i
] + FFALIGN((buf
->linesize
[i
]*EDGE_WIDTH
>>v_shift
) + (EDGE_WIDTH
>>h_shift
), stride_align
[i
]);
332 if(size
[1] && !size
[2])
333 ff_set_systematic_pal((uint32_t*)buf
->data
[1], s
->pix_fmt
);
334 buf
->width
= s
->width
;
335 buf
->height
= s
->height
;
336 buf
->pix_fmt
= s
->pix_fmt
;
337 pic
->age
= 256*256*256*64;
339 pic
->type
= FF_BUFFER_TYPE_INTERNAL
;
342 pic
->base
[i
]= buf
->base
[i
];
343 pic
->data
[i
]= buf
->data
[i
];
344 pic
->linesize
[i
]= buf
->linesize
[i
];
346 s
->internal_buffer_count
++;
348 pic
->reordered_opaque
= s
->reordered_opaque
;
350 if(s
->debug
&FF_DEBUG_BUFFERS
)
351 av_log(s
, AV_LOG_DEBUG
, "default_get_buffer called on pic %p, %d buffers used\n", pic
, s
->internal_buffer_count
);
356 void avcodec_default_release_buffer(AVCodecContext
*s
, AVFrame
*pic
){
358 InternalBuffer
*buf
, *last
;
360 assert(pic
->type
==FF_BUFFER_TYPE_INTERNAL
);
361 assert(s
->internal_buffer_count
);
363 buf
= NULL
; /* avoids warning */
364 for(i
=0; i
<s
->internal_buffer_count
; i
++){ //just 3-5 checks so is not worth to optimize
365 buf
= &((InternalBuffer
*)s
->internal_buffer
)[i
];
366 if(buf
->data
[0] == pic
->data
[0])
369 assert(i
< s
->internal_buffer_count
);
370 s
->internal_buffer_count
--;
371 last
= &((InternalBuffer
*)s
->internal_buffer
)[s
->internal_buffer_count
];
373 FFSWAP(InternalBuffer
, *buf
, *last
);
377 // pic->base[i]=NULL;
379 //printf("R%X\n", pic->opaque);
381 if(s
->debug
&FF_DEBUG_BUFFERS
)
382 av_log(s
, AV_LOG_DEBUG
, "default_release_buffer called on pic %p, %d buffers used\n", pic
, s
->internal_buffer_count
);
385 int avcodec_default_reget_buffer(AVCodecContext
*s
, AVFrame
*pic
){
389 /* If no picture return a new buffer */
390 if(pic
->data
[0] == NULL
) {
391 /* We will copy from buffer, so must be readable */
392 pic
->buffer_hints
|= FF_BUFFER_HINTS_READABLE
;
393 return s
->get_buffer(s
, pic
);
396 /* If internal buffer type return the same buffer */
397 if(pic
->type
== FF_BUFFER_TYPE_INTERNAL
) {
398 pic
->reordered_opaque
= s
->reordered_opaque
;
403 * Not internal type and reget_buffer not overridden, emulate cr buffer
406 for(i
= 0; i
< 4; i
++)
407 pic
->data
[i
] = pic
->base
[i
] = NULL
;
409 /* Allocate new frame */
410 if (s
->get_buffer(s
, pic
))
412 /* Copy image data from old buffer to new buffer */
413 av_picture_copy((AVPicture
*)pic
, (AVPicture
*)&temp_pic
, s
->pix_fmt
, s
->width
,
415 s
->release_buffer(s
, &temp_pic
); // Release old frame
419 int avcodec_default_execute(AVCodecContext
*c
, int (*func
)(AVCodecContext
*c2
, void *arg2
),void *arg
, int *ret
, int count
, int size
){
422 for(i
=0; i
<count
; i
++){
423 int r
= func(c
, (char*)arg
+ i
*size
);
429 int avcodec_default_execute2(AVCodecContext
*c
, int (*func
)(AVCodecContext
*c2
, void *arg2
, int jobnr
, int threadnr
),void *arg
, int *ret
, int count
){
432 for(i
=0; i
<count
; i
++){
433 int r
= func(c
, arg
, i
, 0);
439 enum PixelFormat
avcodec_default_get_format(struct AVCodecContext
*s
, const enum PixelFormat
*fmt
){
440 while (*fmt
!= PIX_FMT_NONE
&& ff_is_hwaccel_pix_fmt(*fmt
))
445 void avcodec_get_frame_defaults(AVFrame
*pic
){
446 memset(pic
, 0, sizeof(AVFrame
));
448 pic
->pts
= AV_NOPTS_VALUE
;
452 AVFrame
*avcodec_alloc_frame(void){
453 AVFrame
*pic
= av_malloc(sizeof(AVFrame
));
455 if(pic
==NULL
) return NULL
;
457 avcodec_get_frame_defaults(pic
);
462 int attribute_align_arg
avcodec_open(AVCodecContext
*avctx
, AVCodec
*codec
)
466 /* If there is a user-supplied mutex locking routine, call it. */
468 if ((*ff_lockmgr_cb
)(&codec_mutex
, AV_LOCK_OBTAIN
))
472 entangled_thread_counter
++;
473 if(entangled_thread_counter
!= 1){
474 av_log(avctx
, AV_LOG_ERROR
, "insufficient thread locking around avcodec_open/close()\n");
478 if(avctx
->codec
|| !codec
)
481 if (codec
->priv_data_size
> 0) {
482 avctx
->priv_data
= av_mallocz(codec
->priv_data_size
);
483 if (!avctx
->priv_data
) {
484 ret
= AVERROR(ENOMEM
);
488 avctx
->priv_data
= NULL
;
491 if(avctx
->coded_width
&& avctx
->coded_height
)
492 avcodec_set_dimensions(avctx
, avctx
->coded_width
, avctx
->coded_height
);
493 else if(avctx
->width
&& avctx
->height
)
494 avcodec_set_dimensions(avctx
, avctx
->width
, avctx
->height
);
496 #define SANE_NB_CHANNELS 128U
497 if (((avctx
->coded_width
|| avctx
->coded_height
)
498 && avcodec_check_dimensions(avctx
, avctx
->coded_width
, avctx
->coded_height
))
499 || avctx
->channels
> SANE_NB_CHANNELS
) {
500 ret
= AVERROR(EINVAL
);
504 avctx
->codec
= codec
;
505 if ((avctx
->codec_type
== AVMEDIA_TYPE_UNKNOWN
|| avctx
->codec_type
== codec
->type
) &&
506 avctx
->codec_id
== CODEC_ID_NONE
) {
507 avctx
->codec_type
= codec
->type
;
508 avctx
->codec_id
= codec
->id
;
510 if(avctx
->codec_id
!= codec
->id
|| avctx
->codec_type
!= codec
->type
){
511 av_log(avctx
, AV_LOG_ERROR
, "codec type or id mismatches\n");
514 avctx
->frame_number
= 0;
515 if(avctx
->codec
->init
){
516 ret
= avctx
->codec
->init(avctx
);
523 entangled_thread_counter
--;
525 /* Release any user-supplied mutex. */
527 (*ff_lockmgr_cb
)(&codec_mutex
, AV_LOCK_RELEASE
);
531 av_freep(&avctx
->priv_data
);
536 int attribute_align_arg
avcodec_encode_audio(AVCodecContext
*avctx
, uint8_t *buf
, int buf_size
,
537 const short *samples
)
539 if(buf_size
< FF_MIN_BUFFER_SIZE
&& 0){
540 av_log(avctx
, AV_LOG_ERROR
, "buffer smaller than minimum size\n");
543 if((avctx
->codec
->capabilities
& CODEC_CAP_DELAY
) || samples
){
544 int ret
= avctx
->codec
->encode(avctx
, buf
, buf_size
, samples
);
545 avctx
->frame_number
++;
551 int attribute_align_arg
avcodec_encode_video(AVCodecContext
*avctx
, uint8_t *buf
, int buf_size
,
554 if(buf_size
< FF_MIN_BUFFER_SIZE
){
555 av_log(avctx
, AV_LOG_ERROR
, "buffer smaller than minimum size\n");
558 if(avcodec_check_dimensions(avctx
,avctx
->width
,avctx
->height
))
560 if((avctx
->codec
->capabilities
& CODEC_CAP_DELAY
) || pict
){
561 int ret
= avctx
->codec
->encode(avctx
, buf
, buf_size
, pict
);
562 avctx
->frame_number
++;
563 emms_c(); //needed to avoid an emms_c() call before every return;
570 int avcodec_encode_subtitle(AVCodecContext
*avctx
, uint8_t *buf
, int buf_size
,
571 const AVSubtitle
*sub
)
574 if(sub
->start_display_time
) {
575 av_log(avctx
, AV_LOG_ERROR
, "start_display_time must be 0.\n");
578 if(sub
->num_rects
== 0 || !sub
->rects
)
580 ret
= avctx
->codec
->encode(avctx
, buf
, buf_size
, sub
);
581 avctx
->frame_number
++;
585 #if LIBAVCODEC_VERSION_MAJOR < 53
586 int attribute_align_arg
avcodec_decode_video(AVCodecContext
*avctx
, AVFrame
*picture
,
587 int *got_picture_ptr
,
588 const uint8_t *buf
, int buf_size
)
591 av_init_packet(&avpkt
);
593 avpkt
.size
= buf_size
;
594 // HACK for CorePNG to decode as normal PNG by default
595 avpkt
.flags
= AV_PKT_FLAG_KEY
;
597 return avcodec_decode_video2(avctx
, picture
, got_picture_ptr
, &avpkt
);
601 int attribute_align_arg
avcodec_decode_video2(AVCodecContext
*avctx
, AVFrame
*picture
,
602 int *got_picture_ptr
,
608 if((avctx
->coded_width
||avctx
->coded_height
) && avcodec_check_dimensions(avctx
,avctx
->coded_width
,avctx
->coded_height
))
610 if((avctx
->codec
->capabilities
& CODEC_CAP_DELAY
) || avpkt
->size
){
611 ret
= avctx
->codec
->decode(avctx
, picture
, got_picture_ptr
,
614 emms_c(); //needed to avoid an emms_c() call before every return;
616 if (*got_picture_ptr
)
617 avctx
->frame_number
++;
624 #if LIBAVCODEC_VERSION_MAJOR < 53
625 int attribute_align_arg
avcodec_decode_audio2(AVCodecContext
*avctx
, int16_t *samples
,
627 const uint8_t *buf
, int buf_size
)
630 av_init_packet(&avpkt
);
632 avpkt
.size
= buf_size
;
634 return avcodec_decode_audio3(avctx
, samples
, frame_size_ptr
, &avpkt
);
638 int attribute_align_arg
avcodec_decode_audio3(AVCodecContext
*avctx
, int16_t *samples
,
644 if((avctx
->codec
->capabilities
& CODEC_CAP_DELAY
) || avpkt
->size
){
645 //FIXME remove the check below _after_ ensuring that all audio check that the available space is enough
646 if(*frame_size_ptr
< AVCODEC_MAX_AUDIO_FRAME_SIZE
){
647 av_log(avctx
, AV_LOG_ERROR
, "buffer smaller than AVCODEC_MAX_AUDIO_FRAME_SIZE\n");
650 if(*frame_size_ptr
< FF_MIN_BUFFER_SIZE
||
651 *frame_size_ptr
< avctx
->channels
* avctx
->frame_size
* sizeof(int16_t)){
652 av_log(avctx
, AV_LOG_ERROR
, "buffer %d too small\n", *frame_size_ptr
);
656 ret
= avctx
->codec
->decode(avctx
, samples
, frame_size_ptr
, avpkt
);
657 avctx
->frame_number
++;
665 #if LIBAVCODEC_VERSION_MAJOR < 53
666 int avcodec_decode_subtitle(AVCodecContext
*avctx
, AVSubtitle
*sub
,
668 const uint8_t *buf
, int buf_size
)
671 av_init_packet(&avpkt
);
673 avpkt
.size
= buf_size
;
675 return avcodec_decode_subtitle2(avctx
, sub
, got_sub_ptr
, &avpkt
);
679 int avcodec_decode_subtitle2(AVCodecContext
*avctx
, AVSubtitle
*sub
,
686 ret
= avctx
->codec
->decode(avctx
, sub
, got_sub_ptr
, avpkt
);
688 avctx
->frame_number
++;
692 av_cold
int avcodec_close(AVCodecContext
*avctx
)
694 /* If there is a user-supplied mutex locking routine, call it. */
696 if ((*ff_lockmgr_cb
)(&codec_mutex
, AV_LOCK_OBTAIN
))
700 entangled_thread_counter
++;
701 if(entangled_thread_counter
!= 1){
702 av_log(avctx
, AV_LOG_ERROR
, "insufficient thread locking around avcodec_open/close()\n");
703 entangled_thread_counter
--;
707 if (HAVE_THREADS
&& avctx
->thread_opaque
)
708 avcodec_thread_free(avctx
);
709 if (avctx
->codec
&& avctx
->codec
->close
)
710 avctx
->codec
->close(avctx
);
711 avcodec_default_free_buffers(avctx
);
712 av_freep(&avctx
->priv_data
);
713 if(avctx
->codec
&& avctx
->codec
->encode
)
714 av_freep(&avctx
->extradata
);
716 entangled_thread_counter
--;
718 /* Release any user-supplied mutex. */
720 (*ff_lockmgr_cb
)(&codec_mutex
, AV_LOCK_RELEASE
);
725 AVCodec
*avcodec_find_encoder(enum CodecID id
)
730 if (p
->encode
!= NULL
&& p
->id
== id
)
737 AVCodec
*avcodec_find_encoder_by_name(const char *name
)
744 if (p
->encode
!= NULL
&& strcmp(name
,p
->name
) == 0)
751 AVCodec
*avcodec_find_decoder(enum CodecID id
)
756 if (p
->decode
!= NULL
&& p
->id
== id
)
763 AVCodec
*avcodec_find_decoder_by_name(const char *name
)
770 if (p
->decode
!= NULL
&& strcmp(name
,p
->name
) == 0)
777 static int get_bit_rate(AVCodecContext
*ctx
)
782 switch(ctx
->codec_type
) {
783 case AVMEDIA_TYPE_VIDEO
:
784 bit_rate
= ctx
->bit_rate
;
786 case AVMEDIA_TYPE_AUDIO
:
787 bits_per_sample
= av_get_bits_per_sample(ctx
->codec_id
);
788 bit_rate
= bits_per_sample ? ctx
->sample_rate
* ctx
->channels
* bits_per_sample
: ctx
->bit_rate
;
790 case AVMEDIA_TYPE_DATA
:
791 bit_rate
= ctx
->bit_rate
;
793 case AVMEDIA_TYPE_SUBTITLE
:
794 bit_rate
= ctx
->bit_rate
;
796 case AVMEDIA_TYPE_ATTACHMENT
:
797 bit_rate
= ctx
->bit_rate
;
806 void avcodec_string(char *buf
, int buf_size
, AVCodecContext
*enc
, int encode
)
808 const char *codec_name
;
812 AVRational display_aspect_ratio
;
815 p
= avcodec_find_encoder(enc
->codec_id
);
817 p
= avcodec_find_decoder(enc
->codec_id
);
820 codec_name
= p
->name
;
821 } else if (enc
->codec_id
== CODEC_ID_MPEG2TS
) {
822 /* fake mpeg2 transport stream codec (currently not
824 codec_name
= "mpeg2ts";
825 } else if (enc
->codec_name
[0] != '\0') {
826 codec_name
= enc
->codec_name
;
828 /* output avi tags */
829 if( isprint(enc
->codec_tag
&0xFF) && isprint((enc
->codec_tag
>>8)&0xFF)
830 && isprint((enc
->codec_tag
>>16)&0xFF) && isprint((enc
->codec_tag
>>24)&0xFF)){
831 snprintf(buf1
, sizeof(buf1
), "%c%c%c%c / 0x%04X",
832 enc
->codec_tag
& 0xff,
833 (enc
->codec_tag
>> 8) & 0xff,
834 (enc
->codec_tag
>> 16) & 0xff,
835 (enc
->codec_tag
>> 24) & 0xff,
838 snprintf(buf1
, sizeof(buf1
), "0x%04x", enc
->codec_tag
);
843 switch(enc
->codec_type
) {
844 case AVMEDIA_TYPE_VIDEO
:
845 snprintf(buf
, buf_size
,
847 codec_name
, enc
->mb_decision ?
" (hq)" : "");
848 if (enc
->pix_fmt
!= PIX_FMT_NONE
) {
849 snprintf(buf
+ strlen(buf
), buf_size
- strlen(buf
),
851 avcodec_get_pix_fmt_name(enc
->pix_fmt
));
854 snprintf(buf
+ strlen(buf
), buf_size
- strlen(buf
),
856 enc
->width
, enc
->height
);
857 if (enc
->sample_aspect_ratio
.num
) {
858 av_reduce(&display_aspect_ratio
.num
, &display_aspect_ratio
.den
,
859 enc
->width
*enc
->sample_aspect_ratio
.num
,
860 enc
->height
*enc
->sample_aspect_ratio
.den
,
862 snprintf(buf
+ strlen(buf
), buf_size
- strlen(buf
),
863 " [PAR %d:%d DAR %d:%d]",
864 enc
->sample_aspect_ratio
.num
, enc
->sample_aspect_ratio
.den
,
865 display_aspect_ratio
.num
, display_aspect_ratio
.den
);
867 if(av_log_get_level() >= AV_LOG_DEBUG
){
868 int g
= av_gcd(enc
->time_base
.num
, enc
->time_base
.den
);
869 snprintf(buf
+ strlen(buf
), buf_size
- strlen(buf
),
871 enc
->time_base
.num
/g
, enc
->time_base
.den
/g
);
875 snprintf(buf
+ strlen(buf
), buf_size
- strlen(buf
),
876 ", q=%d-%d", enc
->qmin
, enc
->qmax
);
879 case AVMEDIA_TYPE_AUDIO
:
880 snprintf(buf
, buf_size
,
883 if (enc
->sample_rate
) {
884 snprintf(buf
+ strlen(buf
), buf_size
- strlen(buf
),
885 ", %d Hz", enc
->sample_rate
);
887 av_strlcat(buf
, ", ", buf_size
);
888 avcodec_get_channel_layout_string(buf
+ strlen(buf
), buf_size
- strlen(buf
), enc
->channels
, enc
->channel_layout
);
889 if (enc
->sample_fmt
!= SAMPLE_FMT_NONE
) {
890 snprintf(buf
+ strlen(buf
), buf_size
- strlen(buf
),
891 ", %s", avcodec_get_sample_fmt_name(enc
->sample_fmt
));
894 case AVMEDIA_TYPE_DATA
:
895 snprintf(buf
, buf_size
, "Data: %s", codec_name
);
897 case AVMEDIA_TYPE_SUBTITLE
:
898 snprintf(buf
, buf_size
, "Subtitle: %s", codec_name
);
900 case AVMEDIA_TYPE_ATTACHMENT
:
901 snprintf(buf
, buf_size
, "Attachment: %s", codec_name
);
904 snprintf(buf
, buf_size
, "Invalid Codec type %d", enc
->codec_type
);
908 if (enc
->flags
& CODEC_FLAG_PASS1
)
909 snprintf(buf
+ strlen(buf
), buf_size
- strlen(buf
),
911 if (enc
->flags
& CODEC_FLAG_PASS2
)
912 snprintf(buf
+ strlen(buf
), buf_size
- strlen(buf
),
915 bitrate
= get_bit_rate(enc
);
917 snprintf(buf
+ strlen(buf
), buf_size
- strlen(buf
),
918 ", %d kb/s", bitrate
/ 1000);
922 unsigned avcodec_version( void )
924 return LIBAVCODEC_VERSION_INT
;
927 const char *avcodec_configuration(void)
929 return FFMPEG_CONFIGURATION
;
932 const char *avcodec_license(void)
934 #define LICENSE_PREFIX "libavcodec license: "
935 return LICENSE_PREFIX FFMPEG_LICENSE
+ sizeof(LICENSE_PREFIX
) - 1;
938 void avcodec_init(void)
940 static int initialized
= 0;
942 if (initialized
!= 0)
946 dsputil_static_init();
949 void avcodec_flush_buffers(AVCodecContext
*avctx
)
951 if(avctx
->codec
->flush
)
952 avctx
->codec
->flush(avctx
);
955 void avcodec_default_free_buffers(AVCodecContext
*s
){
958 if(s
->internal_buffer
==NULL
) return;
960 if (s
->internal_buffer_count
)
961 av_log(s
, AV_LOG_WARNING
, "Found %i unreleased buffers!\n", s
->internal_buffer_count
);
962 for(i
=0; i
<INTERNAL_BUFFER_SIZE
; i
++){
963 InternalBuffer
*buf
= &((InternalBuffer
*)s
->internal_buffer
)[i
];
965 av_freep(&buf
->base
[j
]);
969 av_freep(&s
->internal_buffer
);
971 s
->internal_buffer_count
=0;
974 char av_get_pict_type_char(int pict_type
){
976 case FF_I_TYPE
: return 'I';
977 case FF_P_TYPE
: return 'P';
978 case FF_B_TYPE
: return 'B';
979 case FF_S_TYPE
: return 'S';
980 case FF_SI_TYPE
:return 'i';
981 case FF_SP_TYPE
:return 'p';
982 case FF_BI_TYPE
:return 'b';
987 int av_get_bits_per_sample(enum CodecID codec_id
){
989 case CODEC_ID_ADPCM_SBPRO_2
:
991 case CODEC_ID_ADPCM_SBPRO_3
:
993 case CODEC_ID_ADPCM_SBPRO_4
:
994 case CODEC_ID_ADPCM_CT
:
995 case CODEC_ID_ADPCM_IMA_WAV
:
996 case CODEC_ID_ADPCM_MS
:
997 case CODEC_ID_ADPCM_YAMAHA
:
999 case CODEC_ID_PCM_ALAW
:
1000 case CODEC_ID_PCM_MULAW
:
1001 case CODEC_ID_PCM_S8
:
1002 case CODEC_ID_PCM_U8
:
1003 case CODEC_ID_PCM_ZORK
:
1005 case CODEC_ID_PCM_S16BE
:
1006 case CODEC_ID_PCM_S16LE
:
1007 case CODEC_ID_PCM_S16LE_PLANAR
:
1008 case CODEC_ID_PCM_U16BE
:
1009 case CODEC_ID_PCM_U16LE
:
1011 case CODEC_ID_PCM_S24DAUD
:
1012 case CODEC_ID_PCM_S24BE
:
1013 case CODEC_ID_PCM_S24LE
:
1014 case CODEC_ID_PCM_U24BE
:
1015 case CODEC_ID_PCM_U24LE
:
1017 case CODEC_ID_PCM_S32BE
:
1018 case CODEC_ID_PCM_S32LE
:
1019 case CODEC_ID_PCM_U32BE
:
1020 case CODEC_ID_PCM_U32LE
:
1021 case CODEC_ID_PCM_F32BE
:
1022 case CODEC_ID_PCM_F32LE
:
1024 case CODEC_ID_PCM_F64BE
:
1025 case CODEC_ID_PCM_F64LE
:
1032 int av_get_bits_per_sample_format(enum SampleFormat sample_fmt
) {
1033 switch (sample_fmt
) {
1036 case SAMPLE_FMT_S16
:
1038 case SAMPLE_FMT_S32
:
1039 case SAMPLE_FMT_FLT
:
1041 case SAMPLE_FMT_DBL
:
1049 int avcodec_thread_init(AVCodecContext
*s
, int thread_count
){
1050 s
->thread_count
= thread_count
;
1055 unsigned int av_xiphlacing(unsigned char *s
, unsigned int v
)
1069 /* Wrapper to work around the lack of mkstemp() on mingw/cygin.
1070 * Also, tries to create file in /tmp first, if possible.
1071 * *prefix can be a character constant; *filename will be allocated internally.
1072 * Returns file descriptor of opened file (or -1 on error)
1073 * and opened file name in **filename. */
1074 int av_tempfile(char *prefix
, char **filename
) {
1077 *filename
= tempnam(".", prefix
);
1079 size_t len
= strlen(prefix
) + 12; /* room for "/tmp/" and "XXXXXX\0" */
1080 *filename
= av_malloc(len
);
1082 /* -----common section-----*/
1083 if (*filename
== NULL
) {
1084 av_log(NULL
, AV_LOG_ERROR
, "ff_tempfile: Cannot allocate file name\n");
1088 fd
= open(*filename
, O_RDWR
| O_BINARY
| O_CREAT
, 0444);
1090 snprintf(*filename
, len
, "/tmp/%sXXXXXX", prefix
);
1091 fd
= mkstemp(*filename
);
1093 snprintf(*filename
, len
, "./%sXXXXXX", prefix
);
1094 fd
= mkstemp(*filename
);
1097 /* -----common section-----*/
1099 av_log(NULL
, AV_LOG_ERROR
, "ff_tempfile: Cannot open temporary file %s\n", *filename
);
1102 return fd
; /* success */
1108 } VideoFrameSizeAbbr
;
1112 int rate_num
, rate_den
;
1113 } VideoFrameRateAbbr
;
1115 static const VideoFrameSizeAbbr video_frame_size_abbrs
[] = {
1116 { "ntsc", 720, 480 },
1117 { "pal", 720, 576 },
1118 { "qntsc", 352, 240 }, /* VCD compliant NTSC */
1119 { "qpal", 352, 288 }, /* VCD compliant PAL */
1120 { "sntsc", 640, 480 }, /* square pixel NTSC */
1121 { "spal", 768, 576 }, /* square pixel PAL */
1122 { "film", 352, 240 },
1123 { "ntsc-film", 352, 240 },
1124 { "sqcif", 128, 96 },
1125 { "qcif", 176, 144 },
1126 { "cif", 352, 288 },
1127 { "4cif", 704, 576 },
1128 { "16cif", 1408,1152 },
1129 { "qqvga", 160, 120 },
1130 { "qvga", 320, 240 },
1131 { "vga", 640, 480 },
1132 { "svga", 800, 600 },
1133 { "xga", 1024, 768 },
1134 { "uxga", 1600,1200 },
1135 { "qxga", 2048,1536 },
1136 { "sxga", 1280,1024 },
1137 { "qsxga", 2560,2048 },
1138 { "hsxga", 5120,4096 },
1139 { "wvga", 852, 480 },
1140 { "wxga", 1366, 768 },
1141 { "wsxga", 1600,1024 },
1142 { "wuxga", 1920,1200 },
1143 { "woxga", 2560,1600 },
1144 { "wqsxga", 3200,2048 },
1145 { "wquxga", 3840,2400 },
1146 { "whsxga", 6400,4096 },
1147 { "whuxga", 7680,4800 },
1148 { "cga", 320, 200 },
1149 { "ega", 640, 350 },
1150 { "hd480", 852, 480 },
1151 { "hd720", 1280, 720 },
1152 { "hd1080", 1920,1080 },
1155 static const VideoFrameRateAbbr video_frame_rate_abbrs
[]= {
1156 { "ntsc", 30000, 1001 },
1158 { "qntsc", 30000, 1001 }, /* VCD compliant NTSC */
1159 { "qpal", 25, 1 }, /* VCD compliant PAL */
1160 { "sntsc", 30000, 1001 }, /* square pixel NTSC */
1161 { "spal", 25, 1 }, /* square pixel PAL */
1163 { "ntsc-film", 24000, 1001 },
1166 int av_parse_video_frame_size(int *width_ptr
, int *height_ptr
, const char *str
)
1169 int n
= FF_ARRAY_ELEMS(video_frame_size_abbrs
);
1171 int frame_width
= 0, frame_height
= 0;
1174 if (!strcmp(video_frame_size_abbrs
[i
].abbr
, str
)) {
1175 frame_width
= video_frame_size_abbrs
[i
].width
;
1176 frame_height
= video_frame_size_abbrs
[i
].height
;
1182 frame_width
= strtol(p
, &p
, 10);
1185 frame_height
= strtol(p
, &p
, 10);
1187 if (frame_width
<= 0 || frame_height
<= 0)
1189 *width_ptr
= frame_width
;
1190 *height_ptr
= frame_height
;
1194 int av_parse_video_frame_rate(AVRational
*frame_rate
, const char *arg
)
1197 int n
= FF_ARRAY_ELEMS(video_frame_rate_abbrs
);
1200 /* First, we check our abbreviation table */
1201 for (i
= 0; i
< n
; ++i
)
1202 if (!strcmp(video_frame_rate_abbrs
[i
].abbr
, arg
)) {
1203 frame_rate
->num
= video_frame_rate_abbrs
[i
].rate_num
;
1204 frame_rate
->den
= video_frame_rate_abbrs
[i
].rate_den
;
1208 /* Then, we try to parse it as fraction */
1209 cp
= strchr(arg
, '/');
1211 cp
= strchr(arg
, ':');
1214 frame_rate
->num
= strtol(arg
, &cpp
, 10);
1215 if (cpp
!= arg
|| cpp
== cp
)
1216 frame_rate
->den
= strtol(cp
+1, &cpp
, 10);
1218 frame_rate
->num
= 0;
1221 /* Finally we give up and parse it as double */
1222 AVRational time_base
= av_d2q(strtod(arg
, 0), 1001000);
1223 frame_rate
->den
= time_base
.den
;
1224 frame_rate
->num
= time_base
.num
;
1226 if (!frame_rate
->num
|| !frame_rate
->den
)
1232 int ff_match_2uint16(const uint16_t (*tab
)[2], int size
, int a
, int b
){
1234 for(i
=0; i
<size
&& !(tab
[i
][0]==a
&& tab
[i
][1]==b
); i
++);
1238 void av_log_missing_feature(void *avc
, const char *feature
, int want_sample
)
1240 av_log(avc
, AV_LOG_WARNING
, "%s not implemented. Update your FFmpeg "
1241 "version to the newest one from SVN. If the problem still "
1242 "occurs, it means that your file has a feature which has not "
1243 "been implemented.", feature
);
1245 av_log_ask_for_sample(avc
, NULL
);
1247 av_log(avc
, AV_LOG_WARNING
, "\n");
1250 void av_log_ask_for_sample(void *avc
, const char *msg
)
1253 av_log(avc
, AV_LOG_WARNING
, "%s ", msg
);
1254 av_log(avc
, AV_LOG_WARNING
, "If you want to help, upload a sample "
1255 "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
1256 "and contact the ffmpeg-devel mailing list.\n");
1259 static AVHWAccel
*first_hwaccel
= NULL
;
1261 void av_register_hwaccel(AVHWAccel
*hwaccel
)
1263 AVHWAccel
**p
= &first_hwaccel
;
1267 hwaccel
->next
= NULL
;
1270 AVHWAccel
*av_hwaccel_next(AVHWAccel
*hwaccel
)
1272 return hwaccel ? hwaccel
->next
: first_hwaccel
;
1275 AVHWAccel
*ff_find_hwaccel(enum CodecID codec_id
, enum PixelFormat pix_fmt
)
1277 AVHWAccel
*hwaccel
=NULL
;
1279 while((hwaccel
= av_hwaccel_next(hwaccel
))){
1280 if ( hwaccel
->id
== codec_id
1281 && hwaccel
->pix_fmt
== pix_fmt
)
1287 int av_lockmgr_register(int (*cb
)(void **mutex
, enum AVLockOp op
))
1289 if (ff_lockmgr_cb
) {
1290 if (ff_lockmgr_cb(&codec_mutex
, AV_LOCK_DESTROY
))
1296 if (ff_lockmgr_cb
) {
1297 if (ff_lockmgr_cb(&codec_mutex
, AV_LOCK_CREATE
))