1
0
mirror of https://github.com/FFmpeg/FFmpeg.git synced 2024-12-23 12:43:46 +02:00

avcodec: cleanup utils.c

This commit is contained in:
Luca Barbato 2012-09-14 19:18:17 +02:00
parent ee90119e9e
commit 419ffb2390

View File

@ -60,7 +60,10 @@ void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
min_size = FFMAX(17 * min_size / 16 + 32, min_size);
ptr = av_realloc(ptr, min_size);
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
/* we could set this to the unmodified min_size but this is safer
* if the user lost the ptr and uses NULL now
*/
if (!ptr)
min_size = 0;
*size = min_size;
@ -76,7 +79,8 @@ void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
min_size = FFMAX(17 * min_size / 16 + 32, min_size);
av_free(*p);
*p = av_malloc(min_size);
if (!*p) min_size = 0;
if (!*p)
min_size = 0;
*size = min_size;
}
@ -98,8 +102,10 @@ static AVCodec *first_avcodec = NULL;
AVCodec *av_codec_next(const AVCodec *c)
{
if(c) return c->next;
else return first_avcodec;
if (c)
return c->next;
else
return first_avcodec;
}
static void avcodec_init(void)
@ -128,7 +134,8 @@ void avcodec_register(AVCodec *codec)
AVCodec **p;
avcodec_init();
p = &first_avcodec;
while (*p != NULL) p = &(*p)->next;
while (*p != NULL)
p = &(*p)->next;
*p = codec;
codec->next = NULL;
@ -141,7 +148,8 @@ unsigned avcodec_get_edge_width(void)
return EDGE_WIDTH;
}
void avcodec_set_dimensions(AVCodecContext *s, int width, int height){
void avcodec_set_dimensions(AVCodecContext *s, int width, int height)
{
s->coded_width = width;
s->coded_height = height;
s->width = width;
@ -216,7 +224,8 @@ void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
}
break;
case PIX_FMT_BGR24:
if((s->codec_id == AV_CODEC_ID_MSZH) || (s->codec_id == AV_CODEC_ID_ZLIB)){
if ((s->codec_id == AV_CODEC_ID_MSZH) ||
(s->codec_id == AV_CODEC_ID_ZLIB)) {
w_align = 4;
h_align = 4;
}
@ -230,16 +239,19 @@ void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
*width = FFALIGN(*width, w_align);
*height = FFALIGN(*height, h_align);
if (s->codec_id == AV_CODEC_ID_H264)
*height+=2; // some of the optimized chroma MC reads one line too much
// some of the optimized chroma MC reads one line too much
*height += 2;
for (i = 0; i < 4; i++)
linesize_align[i] = STRIDE_ALIGN;
}
void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height){
void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height)
{
int chroma_shift = av_pix_fmt_descriptors[s->pix_fmt].log2_chroma_w;
int linesize_align[AV_NUM_DATA_POINTERS];
int align;
avcodec_align_dimensions2(s, width, height, linesize_align);
align = FFMAX(linesize_align[0], linesize_align[3]);
linesize_align[1] <<= chroma_shift;
@ -305,7 +317,7 @@ static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
buf = avci->buffer;
/* if there is a previously-used internal buffer, check its size and
channel count to see if we can reuse it */
* channel count to see if we can reuse it */
if (buf->extended_data) {
/* if current buffer is too small, free it */
if (buf->extended_data[0] && buf_size > buf->audio_data_size) {
@ -316,7 +328,7 @@ static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
buf->data[0] = NULL;
}
/* if number of channels has changed, reset and/or free extended data
pointers but leave data buffer in buf->data[0] for reuse */
* pointers but leave data buffer in buf->data[0] for reuse */
if (buf->nb_channels != avctx->channels) {
if (buf->extended_data != buf->data)
av_free(buf->extended_data);
@ -325,7 +337,7 @@ static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
}
/* if there is no previous buffer or the previous buffer cannot be used
as-is, allocate a new buffer and/or rearrange the channel pointers */
* as-is, allocate a new buffer and/or rearrange the channel pointers */
if (!buf->extended_data) {
if (!buf->data[0]) {
if (!(buf->data[0] = av_mallocz(buf_size)))
@ -353,8 +365,10 @@ static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
frame->type = FF_BUFFER_TYPE_INTERNAL;
if (avctx->pkt) frame->pkt_pts = avctx->pkt->pts;
else frame->pkt_pts = AV_NOPTS_VALUE;
if (avctx->pkt)
frame->pkt_pts = avctx->pkt->pts;
else
frame->pkt_pts = AV_NOPTS_VALUE;
frame->reordered_opaque = avctx->reordered_opaque;
frame->sample_rate = avctx->sample_rate;
@ -428,9 +442,8 @@ static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
w += w & ~(w - 1);
unaligned = 0;
for (i=0; i<4; i++){
for (i = 0; i < 4; i++)
unaligned |= picture.linesize[i] % stride_align[i];
}
} while (unaligned);
tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
@ -451,7 +464,8 @@ static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
buf->linesize[i] = picture.linesize[i];
buf->base[i] = av_malloc(size[i] + 16); //FIXME 16
if(buf->base[i]==NULL) return -1;
if (buf->base[i] == NULL)
return -1;
memset(buf->base[i], 128, size[i]);
// no edge if EDGE EMU or not planar YUV
@ -484,8 +498,10 @@ static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
pic->format = buf->pix_fmt;
pic->sample_aspect_ratio = s->sample_aspect_ratio;
if(s->pkt) pic->pkt_pts= s->pkt->pts;
else pic->pkt_pts= AV_NOPTS_VALUE;
if (s->pkt)
pic->pkt_pts = s->pkt->pts;
else
pic->pkt_pts = AV_NOPTS_VALUE;
pic->reordered_opaque = s->reordered_opaque;
if (s->debug & FF_DEBUG_BUFFERS)
@ -507,7 +523,8 @@ int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
}
}
void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic)
{
int i;
InternalBuffer *buf, *last;
AVCodecInternal *avci = s->internal;
@ -532,10 +549,9 @@ void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
FFSWAP(InternalBuffer, *buf, *last);
}
for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
pic->data[i] = NULL;
// pic->base[i]=NULL;
}
//printf("R%X\n", pic->opaque);
if (s->debug & FF_DEBUG_BUFFERS)
@ -543,7 +559,8 @@ void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
"buffers used\n", pic, avci->buffer_count);
}
int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic)
{
AVFrame temp_pic;
int i;
@ -560,8 +577,10 @@ int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
/* If internal buffer type return the same buffer */
if (pic->type == FF_BUFFER_TYPE_INTERNAL) {
if(s->pkt) pic->pkt_pts= s->pkt->pts;
else pic->pkt_pts= AV_NOPTS_VALUE;
if (s->pkt)
pic->pkt_pts = s->pkt->pts;
else
pic->pkt_pts = AV_NOPTS_VALUE;
pic->reordered_opaque = s->reordered_opaque;
return 0;
}
@ -583,33 +602,39 @@ int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
return 0;
}
int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
{
int i;
for (i = 0; i < count; i++) {
int r = func(c, (char *)arg + i * size);
if(ret) ret[i]= r;
if (ret)
ret[i] = r;
}
return 0;
}
int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr),void *arg, int *ret, int count){
int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
{
int i;
for (i = 0; i < count; i++) {
int r = func(c, arg, i, 0);
if(ret) ret[i]= r;
if (ret)
ret[i] = r;
}
return 0;
}
enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat *fmt){
enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat *fmt)
{
while (*fmt != PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
++fmt;
return fmt[0];
}
void avcodec_get_frame_defaults(AVFrame *pic){
void avcodec_get_frame_defaults(AVFrame *pic)
{
memset(pic, 0, sizeof(AVFrame));
pic->pts = AV_NOPTS_VALUE;
@ -618,10 +643,12 @@ void avcodec_get_frame_defaults(AVFrame *pic){
pic->format = -1; /* unknown */
}
AVFrame *avcodec_alloc_frame(void){
AVFrame *avcodec_alloc_frame(void)
{
AVFrame *pic = av_malloc(sizeof(AVFrame));
if(pic==NULL) return NULL;
if (pic == NULL)
return NULL;
avcodec_get_frame_defaults(pic);
@ -706,7 +733,7 @@ int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *code
}
/* if the decoder init function was already called previously,
free the already allocated subtitle_header before overwriting it */
* free the already allocated subtitle_header before overwriting it */
if (av_codec_is_decoder(codec))
av_freep(&avctx->subtitle_header);
@ -990,8 +1017,8 @@ int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
}
/* NOTE: if we add any audio encoders which output non-keyframe packets,
this needs to be moved to the encoders, but for now we can do it
here to simplify things */
* this needs to be moved to the encoders, but for now we can do it
* here to simplify things */
avpkt->flags |= AV_PKT_FLAG_KEY;
if (padded_frame) {
@ -1026,7 +1053,7 @@ int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
frame->nb_samples = avctx->frame_size;
} else {
/* if frame_size is not set, the number of samples must be
calculated from the buffer size */
* calculated from the buffer size */
int64_t nb_samples;
if (!av_get_bits_per_sample(avctx->codec_id)) {
av_log(avctx, AV_LOG_ERROR, "avcodec_encode_audio() does not "
@ -1042,7 +1069,7 @@ int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
}
/* it is assumed that the samples buffer is large enough based on the
relevant parameters */
* relevant parameters */
samples_size = av_samples_get_buffer_size(NULL, avctx->channels,
frame->nb_samples,
avctx->sample_fmt, 1);
@ -1053,8 +1080,8 @@ int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
return ret;
/* fabricate frame pts from sample count.
this is needed because the avcodec_encode_audio() API does not have
a way for the user to provide pts */
* this is needed because the avcodec_encode_audio() API does not have
* a way for the user to provide pts */
frame->pts = ff_samples_to_time_base(avctx,
avctx->internal->sample_count);
avctx->internal->sample_count += frame->nb_samples;
@ -1082,6 +1109,7 @@ int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
return ret ? ret : pkt.size;
}
#endif
#if FF_API_OLD_ENCODE_VIDEO
@ -1117,6 +1145,7 @@ int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf
return ret ? ret : pkt.size;
}
#endif
int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
@ -1303,6 +1332,7 @@ int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *sa
}
return ret;
}
#endif
int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
@ -1353,8 +1383,7 @@ void avsubtitle_free(AVSubtitle *sub)
{
int i;
for (i = 0; i < sub->num_rects; i++)
{
for (i = 0; i < sub->num_rects; i++) {
av_freep(&sub->rects[i]->pict.data[0]);
av_freep(&sub->rects[i]->pict.data[1]);
av_freep(&sub->rects[i]->pict.data[2]);
@ -1526,7 +1555,7 @@ void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
profile = av_get_profile_name(p, enc->profile);
} else if (enc->codec_id == AV_CODEC_ID_MPEG2TS) {
/* fake mpeg2 transport stream codec (currently not
registered) */
* registered) */
codec_name = "mpeg2ts";
} else if (enc->codec_name[0] != '\0') {
codec_name = enc->codec_name;
@ -1923,9 +1952,11 @@ int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
}
#if !HAVE_THREADS
int ff_thread_init(AVCodecContext *s){
int ff_thread_init(AVCodecContext *s)
{
return -1;
}
#endif
unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
@ -1942,7 +1973,8 @@ unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
return n;
}
int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
{
int i;
for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
return i;
@ -1993,11 +2025,10 @@ AVHWAccel *ff_find_hwaccel(enum AVCodecID codec_id, enum PixelFormat pix_fmt)
{
AVHWAccel *hwaccel = NULL;
while((hwaccel= av_hwaccel_next(hwaccel))){
while ((hwaccel = av_hwaccel_next(hwaccel)))
if (hwaccel->id == codec_id
&& hwaccel->pix_fmt == pix_fmt)
return hwaccel;
}
return NULL;
}