From b627c3010b9bd3fa064750e3ea3e6fb45126a847 Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Mon, 19 Nov 2012 18:48:15 +0100 Subject: [PATCH 1/4] loco: cosmetics, reformat --- libavcodec/loco.c | 78 ++++++++++++++++++++++++++++------------------- 1 file changed, 47 insertions(+), 31 deletions(-) diff --git a/libavcodec/loco.c b/libavcodec/loco.c index e9ce482533..d2b2e88f90 100644 --- a/libavcodec/loco.c +++ b/libavcodec/loco.c @@ -30,29 +30,39 @@ #include "internal.h" #include "mathops.h" -enum LOCO_MODE {LOCO_UNKN=0, LOCO_CYUY2=-1, LOCO_CRGB=-2, LOCO_CRGBA=-3, LOCO_CYV12=-4, - LOCO_YUY2=1, LOCO_UYVY=2, LOCO_RGB=3, LOCO_RGBA=4, LOCO_YV12=5}; +enum LOCO_MODE { + LOCO_UNKN = 0, + LOCO_CYUY2 = -1, + LOCO_CRGB = -2, + LOCO_CRGBA = -3, + LOCO_CYV12 = -4, + LOCO_YUY2 = 1, + LOCO_UYVY = 2, + LOCO_RGB = 3, + LOCO_RGBA = 4, + LOCO_YV12 = 5, +}; -typedef struct LOCOContext{ +typedef struct LOCOContext { AVCodecContext *avctx; AVFrame pic; int lossy; int mode; } LOCOContext; -typedef struct RICEContext{ +typedef struct RICEContext { GetBitContext gb; int save, run, run2; /* internal rice decoder state */ int sum, count; /* sum and count for getting rice parameter */ int lossy; -}RICEContext; +} RICEContext; static int loco_get_rice_param(RICEContext *r) { int cnt = 0; int val = r->count; - while(r->sum > val && cnt < 9) { + while (r->sum > val && cnt < 9) { val <<= 1; cnt++; } @@ -65,8 +75,8 @@ static inline void loco_update_rice_param(RICEContext *r, int val) r->sum += val; r->count++; - if(r->count == 16) { - r->sum >>= 1; + if (r->count == 16) { + r->sum >>= 1; r->count >>= 1; } } @@ -80,19 +90,18 @@ static inline int loco_get_rice(RICEContext *r) return 0; } v = get_ur_golomb_jpegls(&r->gb, loco_get_rice_param(r), INT_MAX, 0); - loco_update_rice_param(r, (v+1)>>1); + loco_update_rice_param(r, (v + 1) >> 1); if (!v) { if (r->save >= 0) { r->run = get_ur_golomb_jpegls(&r->gb, 2, INT_MAX, 0); - if(r->run > 1) + if (r->run > 1) r->save += r->run + 1; else r->save -= 3; - } - else + } else r->run2++; } else { - v = ((v>>1) + r->lossy) ^ -(v&1); + v = ((v >> 1) + r->lossy) ^ -(v & 1); if (r->run2 > 0) { if (r->run2 > 2) r->save += r->run2; @@ -125,16 +134,16 @@ static int loco_decode_plane(LOCOContext *l, uint8_t *data, int width, int heigh int i, j; init_get_bits(&rc.gb, buf, buf_size*8); - rc.save = 0; - rc.run = 0; - rc.run2 = 0; + rc.save = 0; + rc.run = 0; + rc.run2 = 0; rc.lossy = l->lossy; - rc.sum = 8; + rc.sum = 8; rc.count = 1; /* restore top left pixel */ - val = loco_get_rice(&rc); + val = loco_get_rice(&rc); data[0] = 128 + val; /* restore top line */ for (i = 1; i < width; i++) { @@ -161,13 +170,13 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt) { - const uint8_t *buf = avpkt->data; - int buf_size = avpkt->size; LOCOContext * const l = avctx->priv_data; - AVFrame * const p = &l->pic; + const uint8_t *buf = avpkt->data; + int buf_size = avpkt->size; + AVFrame * const p = &l->pic; int decoded, ret; - if(p->data[0]) + if (p->data[0]) avctx->release_buffer(avctx, p); p->reference = 0; @@ -229,7 +238,8 @@ static int decode_frame(AVCodecContext *avctx, return buf_size; } -static av_cold int decode_init(AVCodecContext *avctx){ +static av_cold int decode_init(AVCodecContext *avctx) +{ LOCOContext * const l = avctx->priv_data; int version; @@ -240,7 +250,7 @@ static av_cold int decode_init(AVCodecContext *avctx){ return AVERROR_INVALIDDATA; } version = AV_RL32(avctx->extradata); - switch(version) { + switch (version) { case 1: l->lossy = 0; break; @@ -253,30 +263,36 @@ static av_cold int decode_init(AVCodecContext *avctx){ } l->mode = AV_RL32(avctx->extradata + 4); - switch(l->mode) { - case LOCO_CYUY2: case LOCO_YUY2: case LOCO_UYVY: + switch (l->mode) { + case LOCO_CYUY2: + case LOCO_YUY2: + case LOCO_UYVY: avctx->pix_fmt = AV_PIX_FMT_YUV422P; break; - case LOCO_CRGB: case LOCO_RGB: + case LOCO_CRGB: + case LOCO_RGB: avctx->pix_fmt = AV_PIX_FMT_BGR24; break; - case LOCO_CYV12: case LOCO_YV12: + case LOCO_CYV12: + case LOCO_YV12: avctx->pix_fmt = AV_PIX_FMT_YUV420P; break; - case LOCO_CRGBA: case LOCO_RGBA: + case LOCO_CRGBA: + case LOCO_RGBA: avctx->pix_fmt = AV_PIX_FMT_RGB32; break; default: av_log(avctx, AV_LOG_INFO, "Unknown colorspace, index = %i\n", l->mode); return AVERROR_INVALIDDATA; } - if(avctx->debug & FF_DEBUG_PICT_INFO) + if (avctx->debug & FF_DEBUG_PICT_INFO) av_log(avctx, AV_LOG_INFO, "lossy:%i, version:%i, mode: %i\n", l->lossy, version, l->mode); return 0; } -static av_cold int decode_end(AVCodecContext *avctx){ +static av_cold int decode_end(AVCodecContext *avctx) +{ LOCOContext * const l = avctx->priv_data; AVFrame *pic = &l->pic; From ac1e93f555aad54e5a73cf12774c07af59c1d9f1 Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Tue, 20 Nov 2012 07:13:49 +0100 Subject: [PATCH 2/4] tscc: return meaningful error codes. --- libavcodec/tscc.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/libavcodec/tscc.c b/libavcodec/tscc.c index 5b1c7be16e..d9e51ad131 100644 --- a/libavcodec/tscc.c +++ b/libavcodec/tscc.c @@ -79,22 +79,22 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, CamtasiaContext * const c = avctx->priv_data; const unsigned char *encoded = buf; int zret; // Zlib return code - int len = buf_size; + int ret, len = buf_size; if(c->pic.data[0]) avctx->release_buffer(avctx, &c->pic); c->pic.reference = 1; c->pic.buffer_hints = FF_BUFFER_HINTS_VALID; - if(ff_get_buffer(avctx, &c->pic) < 0){ + if ((ret = ff_get_buffer(avctx, &c->pic)) < 0){ av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n"); - return -1; + return ret; } zret = inflateReset(&c->zstream); if (zret != Z_OK) { av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret); - return -1; + return AVERROR_UNKNOWN; } c->zstream.next_in = encoded; c->zstream.avail_in = len; @@ -104,7 +104,7 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, // Z_DATA_ERROR means empty picture if ((zret != Z_OK) && (zret != Z_STREAM_END) && (zret != Z_DATA_ERROR)) { av_log(avctx, AV_LOG_ERROR, "Inflate error: %d\n", zret); - return -1; + return AVERROR_UNKNOWN; } @@ -158,7 +158,7 @@ static av_cold int decode_init(AVCodecContext *avctx) break; case 32: avctx->pix_fmt = AV_PIX_FMT_RGB32; break; default: av_log(avctx, AV_LOG_ERROR, "Camtasia error: unknown depth %i bpp\n", avctx->bits_per_coded_sample); - return -1; + return AVERROR_INVALIDDATA; } c->bpp = avctx->bits_per_coded_sample; // buffer size for RLE 'best' case when 2-byte code precedes each pixel and there may be padding after it too @@ -168,7 +168,7 @@ static av_cold int decode_init(AVCodecContext *avctx) if (c->decomp_size) { if ((c->decomp_buf = av_malloc(c->decomp_size)) == NULL) { av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n"); - return 1; + return AVERROR(ENOMEM); } } @@ -178,7 +178,7 @@ static av_cold int decode_init(AVCodecContext *avctx) zret = inflateInit(&c->zstream); if (zret != Z_OK) { av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret); - return 1; + return AVERROR_UNKNOWN; } return 0; From a4a26f518856fabd87679064b21e217d67031609 Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Tue, 20 Nov 2012 07:14:51 +0100 Subject: [PATCH 3/4] tscc: remove some pointless comments and empty lines. --- libavcodec/tscc.c | 23 ----------------------- 1 file changed, 23 deletions(-) diff --git a/libavcodec/tscc.c b/libavcodec/tscc.c index d9e51ad131..0b3c7f1a60 100644 --- a/libavcodec/tscc.c +++ b/libavcodec/tscc.c @@ -44,10 +44,6 @@ #include - -/* - * Decoder context - */ typedef struct TsccContext { AVCodecContext *avctx; @@ -66,11 +62,6 @@ typedef struct TsccContext { uint32_t pal[256]; } CamtasiaContext; -/* - * - * Decode a frame - * - */ static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt) { @@ -132,13 +123,6 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, return buf_size; } - - -/* - * - * Init tscc decoder - * - */ static av_cold int decode_init(AVCodecContext *avctx) { CamtasiaContext * const c = avctx->priv_data; @@ -184,13 +168,6 @@ static av_cold int decode_init(AVCodecContext *avctx) return 0; } - - -/* - * - * Uninit tscc decoder - * - */ static av_cold int decode_end(AVCodecContext *avctx) { CamtasiaContext * const c = avctx->priv_data; From df9036830b15997a3e9c3f2c632ed98d64f9deef Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Tue, 20 Nov 2012 07:31:08 +0100 Subject: [PATCH 4/4] truemotion2: return meaningful error codes. --- libavcodec/truemotion2.c | 67 +++++++++++++++++++--------------------- 1 file changed, 32 insertions(+), 35 deletions(-) diff --git a/libavcodec/truemotion2.c b/libavcodec/truemotion2.c index 2d7a5102c2..fc61acaa79 100644 --- a/libavcodec/truemotion2.c +++ b/libavcodec/truemotion2.c @@ -90,9 +90,10 @@ typedef struct TM2Huff{ static int tm2_read_tree(TM2Context *ctx, uint32_t prefix, int length, TM2Huff *huff) { + int ret; if(length > huff->max_bits) { av_log(ctx->avctx, AV_LOG_ERROR, "Tree exceeded its given depth (%i)\n", huff->max_bits); - return -1; + return AVERROR_INVALIDDATA; } if(!get_bits1(&ctx->gb)) { /* literal */ @@ -101,7 +102,7 @@ static int tm2_read_tree(TM2Context *ctx, uint32_t prefix, int length, TM2Huff * } if(huff->num >= huff->max_num) { av_log(ctx->avctx, AV_LOG_DEBUG, "Too many literals\n"); - return -1; + return AVERROR_INVALIDDATA; } huff->nums[huff->num] = get_bits_long(&ctx->gb, huff->val_bits); huff->bits[huff->num] = prefix; @@ -109,10 +110,10 @@ static int tm2_read_tree(TM2Context *ctx, uint32_t prefix, int length, TM2Huff * huff->num++; return 0; } else { /* non-terminal node */ - if(tm2_read_tree(ctx, prefix << 1, length + 1, huff) == -1) - return -1; - if(tm2_read_tree(ctx, (prefix << 1) | 1, length + 1, huff) == -1) - return -1; + if ((ret = tm2_read_tree(ctx, prefix << 1, length + 1, huff)) < 0) + return ret; + if ((ret = tm2_read_tree(ctx, (prefix << 1) | 1, length + 1, huff)) < 0) + return ret; } return 0; } @@ -133,11 +134,11 @@ static int tm2_build_huff_table(TM2Context *ctx, TM2Codes *code) (huff.max_bits < 0) || (huff.max_bits > 25)) { av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect tree parameters - literal length: %i, max code length: %i\n", huff.val_bits, huff.max_bits); - return -1; + return AVERROR_INVALIDDATA; } if((huff.nodes <= 0) || (huff.nodes > 0x10000)) { av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect number of Huffman tree nodes: %i\n", huff.nodes); - return -1; + return AVERROR_INVALIDDATA; } /* one-node tree */ if(huff.max_bits == 0) @@ -149,28 +150,24 @@ static int tm2_build_huff_table(TM2Context *ctx, TM2Codes *code) huff.bits = av_mallocz(huff.max_num * sizeof(uint32_t)); huff.lens = av_mallocz(huff.max_num * sizeof(int)); - if(tm2_read_tree(ctx, 0, 0, &huff) == -1) - res = -1; + res = tm2_read_tree(ctx, 0, 0, &huff); - if(huff.num != huff.max_num) { + if (huff.num != huff.max_num) { av_log(ctx->avctx, AV_LOG_ERROR, "Got less codes than expected: %i of %i\n", huff.num, huff.max_num); - res = -1; + res = AVERROR_INVALIDDATA; } /* convert codes to vlc_table */ - if(res != -1) { + if(res >= 0) { int i; res = init_vlc(&code->vlc, huff.max_bits, huff.max_num, huff.lens, sizeof(int), sizeof(int), huff.bits, sizeof(uint32_t), sizeof(uint32_t), 0); - if(res < 0) { + if(res < 0) av_log(ctx->avctx, AV_LOG_ERROR, "Cannot build VLC table\n"); - res = -1; - } else - res = 0; - if(res != -1) { + else { code->bits = huff.max_bits; code->length = huff.max_num; code->recode = av_malloc(code->length * sizeof(int)); @@ -228,7 +225,7 @@ static int tm2_read_deltas(TM2Context *ctx, int stream_id) { if((d < 1) || (d > TM2_DELTAS) || (mb < 1) || (mb > 32)) { av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect delta table: %i deltas x %i bits\n", d, mb); - return -1; + return AVERROR_INVALIDDATA; } for(i = 0; i < d; i++) { @@ -246,7 +243,7 @@ static int tm2_read_deltas(TM2Context *ctx, int stream_id) { static int tm2_read_stream(TM2Context *ctx, const uint8_t *buf, int stream_id, int buf_size) { - int i; + int i, ret; int skip = 0; int len, toks, pos; TM2Codes codes; @@ -262,7 +259,7 @@ static int tm2_read_stream(TM2Context *ctx, const uint8_t *buf, int stream_id, i if (len >= INT_MAX/4-1 || len < 0 || len > buf_size) { av_log(ctx->avctx, AV_LOG_ERROR, "Error, invalid stream size.\n"); - return -1; + return AVERROR_INVALIDDATA; } toks = bytestream2_get_be32(&gb); @@ -274,10 +271,10 @@ static int tm2_read_stream(TM2Context *ctx, const uint8_t *buf, int stream_id, i if(len > 0) { pos = bytestream2_tell(&gb); if (skip <= pos) - return -1; + return AVERROR_INVALIDDATA; init_get_bits(&ctx->gb, buf + pos, (skip - pos) * 8); - if(tm2_read_deltas(ctx, stream_id) == -1) - return -1; + if ((ret = tm2_read_deltas(ctx, stream_id)) < 0) + return ret; bytestream2_skip(&gb, ((get_bits_count(&ctx->gb) + 31) >> 5) << 2); } } @@ -291,10 +288,10 @@ static int tm2_read_stream(TM2Context *ctx, const uint8_t *buf, int stream_id, i pos = bytestream2_tell(&gb); if (skip <= pos) - return -1; + return AVERROR_INVALIDDATA; init_get_bits(&ctx->gb, buf + pos, (skip - pos) * 8); - if(tm2_build_huff_table(ctx, &codes) == -1) - return -1; + if ((ret = tm2_build_huff_table(ctx, &codes)) < 0) + return ret; bytestream2_skip(&gb, ((get_bits_count(&ctx->gb) + 31) >> 5) << 2); toks >>= 1; @@ -302,7 +299,7 @@ static int tm2_read_stream(TM2Context *ctx, const uint8_t *buf, int stream_id, i if((toks < 0) || (toks > 0xFFFFFF)){ av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect number of tokens: %i\n", toks); tm2_free_codes(&codes); - return -1; + return AVERROR_INVALIDDATA; } ctx->tokens[stream_id] = av_realloc(ctx->tokens[stream_id], toks * sizeof(int)); ctx->tok_lens[stream_id] = toks; @@ -310,12 +307,12 @@ static int tm2_read_stream(TM2Context *ctx, const uint8_t *buf, int stream_id, i if(len > 0) { pos = bytestream2_tell(&gb); if (skip <= pos) - return -1; + return AVERROR_INVALIDDATA; init_get_bits(&ctx->gb, buf + pos, (skip - pos) * 8); for(i = 0; i < toks; i++) { if (get_bits_left(&ctx->gb) <= 0) { av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect number of tokens: %i\n", toks); - return -1; + return AVERROR_INVALIDDATA; } ctx->tokens[stream_id][i] = tm2_get_token(&ctx->gb, &codes); if (stream_id <= TM2_MOT && ctx->tokens[stream_id][i] >= TM2_DELTAS) { @@ -700,7 +697,7 @@ static int tm2_decode_blocks(TM2Context *ctx, AVFrame *p) if (ctx->tok_lens[TM2_TYPE]avctx,AV_LOG_ERROR,"Got %i tokens for %i blocks\n",ctx->tok_lens[TM2_TYPE],bw*bh); - return -1; + return AVERROR_INVALIDDATA; } memset(ctx->last, 0, 4 * bw * sizeof(int)); @@ -822,14 +819,14 @@ static int decode_frame(AVCodecContext *avctx, swbuf = av_malloc(buf_size + FF_INPUT_BUFFER_PADDING_SIZE); if(!swbuf){ av_log(avctx, AV_LOG_ERROR, "Cannot allocate temporary buffer\n"); - return -1; + return AVERROR(ENOMEM); } p->reference = 1; p->buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE; - if(avctx->reget_buffer(avctx, p) < 0){ + if ((ret = avctx->reget_buffer(avctx, p)) < 0) { av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n"); av_free(swbuf); - return -1; + return ret; } l->dsp.bswap_buf((uint32_t*)swbuf, (const uint32_t*)buf, buf_size >> 2); @@ -872,7 +869,7 @@ static av_cold int decode_init(AVCodecContext *avctx){ if((avctx->width & 3) || (avctx->height & 3)){ av_log(avctx, AV_LOG_ERROR, "Width and height must be multiple of 4\n"); - return -1; + return AVERROR(EINVAL); } l->avctx = avctx;