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); min_size = FFMAX(17 * min_size / 16 + 32, min_size);
ptr = av_realloc(ptr, 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; min_size = 0;
*size = min_size; *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); min_size = FFMAX(17 * min_size / 16 + 32, min_size);
av_free(*p); av_free(*p);
*p = av_malloc(min_size); *p = av_malloc(min_size);
if (!*p) min_size = 0; if (!*p)
min_size = 0;
*size = min_size; *size = min_size;
} }
@@ -98,8 +102,10 @@ static AVCodec *first_avcodec = NULL;
AVCodec *av_codec_next(const AVCodec *c) AVCodec *av_codec_next(const AVCodec *c)
{ {
if(c) return c->next; if (c)
else return first_avcodec; return c->next;
else
return first_avcodec;
} }
static void avcodec_init(void) static void avcodec_init(void)
@@ -128,7 +134,8 @@ void avcodec_register(AVCodec *codec)
AVCodec **p; AVCodec **p;
avcodec_init(); avcodec_init();
p = &first_avcodec; p = &first_avcodec;
while (*p != NULL) p = &(*p)->next; while (*p != NULL)
p = &(*p)->next;
*p = codec; *p = codec;
codec->next = NULL; codec->next = NULL;
@@ -141,7 +148,8 @@ unsigned avcodec_get_edge_width(void)
return EDGE_WIDTH; 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_width = width;
s->coded_height = height; s->coded_height = height;
s->width = width; s->width = width;
@@ -216,7 +224,8 @@ void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
} }
break; break;
case PIX_FMT_BGR24: 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; w_align = 4;
h_align = 4; h_align = 4;
} }
@@ -230,16 +239,19 @@ void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
*width = FFALIGN(*width, w_align); *width = FFALIGN(*width, w_align);
*height = FFALIGN(*height, h_align); *height = FFALIGN(*height, h_align);
if (s->codec_id == AV_CODEC_ID_H264) 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++) for (i = 0; i < 4; i++)
linesize_align[i] = STRIDE_ALIGN; 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 chroma_shift = av_pix_fmt_descriptors[s->pix_fmt].log2_chroma_w;
int linesize_align[AV_NUM_DATA_POINTERS]; int linesize_align[AV_NUM_DATA_POINTERS];
int align; int align;
avcodec_align_dimensions2(s, width, height, linesize_align); avcodec_align_dimensions2(s, width, height, linesize_align);
align = FFMAX(linesize_align[0], linesize_align[3]); align = FFMAX(linesize_align[0], linesize_align[3]);
linesize_align[1] <<= chroma_shift; linesize_align[1] <<= chroma_shift;
@@ -305,7 +317,7 @@ static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
buf = avci->buffer; buf = avci->buffer;
/* if there is a previously-used internal buffer, check its size and /* 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 (buf->extended_data) {
/* if current buffer is too small, free it */ /* if current buffer is too small, free it */
if (buf->extended_data[0] && buf_size > buf->audio_data_size) { 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; buf->data[0] = NULL;
} }
/* if number of channels has changed, reset and/or free extended data /* 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->nb_channels != avctx->channels) {
if (buf->extended_data != buf->data) if (buf->extended_data != buf->data)
av_free(buf->extended_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 /* 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->extended_data) {
if (!buf->data[0]) { if (!buf->data[0]) {
if (!(buf->data[0] = av_mallocz(buf_size))) 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; frame->type = FF_BUFFER_TYPE_INTERNAL;
if (avctx->pkt) frame->pkt_pts = avctx->pkt->pts; if (avctx->pkt)
else frame->pkt_pts = AV_NOPTS_VALUE; frame->pkt_pts = avctx->pkt->pts;
else
frame->pkt_pts = AV_NOPTS_VALUE;
frame->reordered_opaque = avctx->reordered_opaque; frame->reordered_opaque = avctx->reordered_opaque;
frame->sample_rate = avctx->sample_rate; frame->sample_rate = avctx->sample_rate;
@@ -428,9 +442,8 @@ static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
w += w & ~(w - 1); w += w & ~(w - 1);
unaligned = 0; unaligned = 0;
for (i=0; i<4; i++){ for (i = 0; i < 4; i++)
unaligned |= picture.linesize[i] % stride_align[i]; unaligned |= picture.linesize[i] % stride_align[i];
}
} while (unaligned); } while (unaligned);
tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize); 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->linesize[i] = picture.linesize[i];
buf->base[i] = av_malloc(size[i] + 16); //FIXME 16 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]); memset(buf->base[i], 128, size[i]);
// no edge if EDGE EMU or not planar YUV // 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->format = buf->pix_fmt;
pic->sample_aspect_ratio = s->sample_aspect_ratio; pic->sample_aspect_ratio = s->sample_aspect_ratio;
if(s->pkt) pic->pkt_pts= s->pkt->pts; if (s->pkt)
else pic->pkt_pts= AV_NOPTS_VALUE; pic->pkt_pts = s->pkt->pts;
else
pic->pkt_pts = AV_NOPTS_VALUE;
pic->reordered_opaque = s->reordered_opaque; pic->reordered_opaque = s->reordered_opaque;
if (s->debug & FF_DEBUG_BUFFERS) 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; int i;
InternalBuffer *buf, *last; InternalBuffer *buf, *last;
AVCodecInternal *avci = s->internal; AVCodecInternal *avci = s->internal;
@@ -532,10 +549,9 @@ void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
FFSWAP(InternalBuffer, *buf, *last); 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->data[i] = NULL;
// pic->base[i]=NULL; // pic->base[i]=NULL;
}
//printf("R%X\n", pic->opaque); //printf("R%X\n", pic->opaque);
if (s->debug & FF_DEBUG_BUFFERS) 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); "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; AVFrame temp_pic;
int i; int i;
@@ -560,8 +577,10 @@ int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
/* If internal buffer type return the same buffer */ /* If internal buffer type return the same buffer */
if (pic->type == FF_BUFFER_TYPE_INTERNAL) { if (pic->type == FF_BUFFER_TYPE_INTERNAL) {
if(s->pkt) pic->pkt_pts= s->pkt->pts; if (s->pkt)
else pic->pkt_pts= AV_NOPTS_VALUE; pic->pkt_pts = s->pkt->pts;
else
pic->pkt_pts = AV_NOPTS_VALUE;
pic->reordered_opaque = s->reordered_opaque; pic->reordered_opaque = s->reordered_opaque;
return 0; return 0;
} }
@@ -583,33 +602,39 @@ int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
return 0; 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; int i;
for (i = 0; i < count; i++) { for (i = 0; i < count; i++) {
int r = func(c, (char *)arg + i * size); int r = func(c, (char *)arg + i * size);
if(ret) ret[i]= r; if (ret)
ret[i] = r;
} }
return 0; 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; int i;
for (i = 0; i < count; i++) { for (i = 0; i < count; i++) {
int r = func(c, arg, i, 0); int r = func(c, arg, i, 0);
if(ret) ret[i]= r; if (ret)
ret[i] = r;
} }
return 0; 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)) while (*fmt != PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
++fmt; ++fmt;
return fmt[0]; return fmt[0];
} }
void avcodec_get_frame_defaults(AVFrame *pic){ void avcodec_get_frame_defaults(AVFrame *pic)
{
memset(pic, 0, sizeof(AVFrame)); memset(pic, 0, sizeof(AVFrame));
pic->pts = AV_NOPTS_VALUE; pic->pts = AV_NOPTS_VALUE;
@@ -618,10 +643,12 @@ void avcodec_get_frame_defaults(AVFrame *pic){
pic->format = -1; /* unknown */ pic->format = -1; /* unknown */
} }
AVFrame *avcodec_alloc_frame(void){ AVFrame *avcodec_alloc_frame(void)
{
AVFrame *pic = av_malloc(sizeof(AVFrame)); AVFrame *pic = av_malloc(sizeof(AVFrame));
if(pic==NULL) return NULL; if (pic == NULL)
return NULL;
avcodec_get_frame_defaults(pic); 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, /* 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)) if (av_codec_is_decoder(codec))
av_freep(&avctx->subtitle_header); 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, /* 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 * this needs to be moved to the encoders, but for now we can do it
here to simplify things */ * here to simplify things */
avpkt->flags |= AV_PKT_FLAG_KEY; avpkt->flags |= AV_PKT_FLAG_KEY;
if (padded_frame) { if (padded_frame) {
@@ -1026,7 +1053,7 @@ int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
frame->nb_samples = avctx->frame_size; frame->nb_samples = avctx->frame_size;
} else { } else {
/* if frame_size is not set, the number of samples must be /* 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; int64_t nb_samples;
if (!av_get_bits_per_sample(avctx->codec_id)) { if (!av_get_bits_per_sample(avctx->codec_id)) {
av_log(avctx, AV_LOG_ERROR, "avcodec_encode_audio() does not " 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 /* 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, samples_size = av_samples_get_buffer_size(NULL, avctx->channels,
frame->nb_samples, frame->nb_samples,
avctx->sample_fmt, 1); avctx->sample_fmt, 1);
@@ -1053,8 +1080,8 @@ int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
return ret; return ret;
/* fabricate frame pts from sample count. /* fabricate frame pts from sample count.
this is needed because the avcodec_encode_audio() API does not have * this is needed because the avcodec_encode_audio() API does not have
a way for the user to provide pts */ * a way for the user to provide pts */
frame->pts = ff_samples_to_time_base(avctx, frame->pts = ff_samples_to_time_base(avctx,
avctx->internal->sample_count); avctx->internal->sample_count);
avctx->internal->sample_count += frame->nb_samples; 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; return ret ? ret : pkt.size;
} }
#endif #endif
#if FF_API_OLD_ENCODE_VIDEO #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; return ret ? ret : pkt.size;
} }
#endif #endif
int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx, 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; return ret;
} }
#endif #endif
int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx, int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
@@ -1353,8 +1383,7 @@ void avsubtitle_free(AVSubtitle *sub)
{ {
int i; 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[0]);
av_freep(&sub->rects[i]->pict.data[1]); av_freep(&sub->rects[i]->pict.data[1]);
av_freep(&sub->rects[i]->pict.data[2]); 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); profile = av_get_profile_name(p, enc->profile);
} else if (enc->codec_id == AV_CODEC_ID_MPEG2TS) { } else if (enc->codec_id == AV_CODEC_ID_MPEG2TS) {
/* fake mpeg2 transport stream codec (currently not /* fake mpeg2 transport stream codec (currently not
registered) */ * registered) */
codec_name = "mpeg2ts"; codec_name = "mpeg2ts";
} else if (enc->codec_name[0] != '\0') { } else if (enc->codec_name[0] != '\0') {
codec_name = enc->codec_name; codec_name = enc->codec_name;
@@ -1923,9 +1952,11 @@ int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
} }
#if !HAVE_THREADS #if !HAVE_THREADS
int ff_thread_init(AVCodecContext *s){ int ff_thread_init(AVCodecContext *s)
{
return -1; return -1;
} }
#endif #endif
unsigned int av_xiphlacing(unsigned char *s, unsigned int v) 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; 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; int i;
for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ; for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
return i; return i;
@@ -1993,11 +2025,10 @@ AVHWAccel *ff_find_hwaccel(enum AVCodecID codec_id, enum PixelFormat pix_fmt)
{ {
AVHWAccel *hwaccel = NULL; AVHWAccel *hwaccel = NULL;
while((hwaccel= av_hwaccel_next(hwaccel))){ while ((hwaccel = av_hwaccel_next(hwaccel)))
if (hwaccel->id == codec_id if (hwaccel->id == codec_id
&& hwaccel->pix_fmt == pix_fmt) && hwaccel->pix_fmt == pix_fmt)
return hwaccel; return hwaccel;
}
return NULL; return NULL;
} }