From dd3d7ddf2a508b15877bb978c2309f777853a228 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandra=20H=C3=A1jkov=C3=A1?= Date: Fri, 22 Apr 2016 09:43:52 +0200 Subject: [PATCH 01/35] lavc: add a new bitstream reader to replace get_bits The new bit reader features a simpler API and an implementation without stacks of nested macros. Signed-off-by: Anton Khirnov --- libavcodec/bitstream.h | 387 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 387 insertions(+) create mode 100644 libavcodec/bitstream.h diff --git a/libavcodec/bitstream.h b/libavcodec/bitstream.h new file mode 100644 index 0000000000..996e32e83b --- /dev/null +++ b/libavcodec/bitstream.h @@ -0,0 +1,387 @@ +/* + * Copyright (c) 2016 Alexandra Hájková + * + * This file is part of Libav. + * + * Libav is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * Libav is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Libav; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/** + * @file + * functions for reading bits from a buffer + */ + +#ifndef AVCODEC_BITSTREAM_H +#define AVCODEC_BITSTREAM_H + +#include + +#include "libavutil/common.h" +#include "libavutil/intreadwrite.h" +#include "libavutil/log.h" + +#include "mathops.h" +#include "vlc.h" + +typedef struct BitstreamContext { + uint64_t bits; // stores bits read from the buffer + const uint8_t *buffer, *buffer_end; + const uint8_t *ptr; // position inside a buffer + unsigned bits_left; // number of bits left in bits field + unsigned size_in_bits; +} BitstreamContext; + +static inline void refill_64(BitstreamContext *bc) +{ + if (bc->ptr >= bc->buffer_end) + return; + +#ifdef BITSTREAM_READER_LE + bc->bits = AV_RL64(bc->ptr); +#else + bc->bits = AV_RB64(bc->ptr); +#endif + bc->ptr += 8; + bc->bits_left = 64; +} + +static inline void refill_32(BitstreamContext *bc) +{ + if (bc->ptr >= bc->buffer_end) + return; + +#ifdef BITSTREAM_READER_LE + bc->bits = (uint64_t)AV_RL32(bc->ptr) << bc->bits_left | bc->bits; +#else + bc->bits = bc->bits | (uint64_t)AV_RB32(bc->ptr) << (32 - bc->bits_left); +#endif + bc->ptr += 4; + bc->bits_left += 32; +} + +/* Initialize BitstreamContext. Input buffer must have an additional zero + * padding of AV_INPUT_BUFFER_PADDING_SIZE bytes at the end. */ +static inline int bitstream_init(BitstreamContext *bc, const uint8_t *buffer, + unsigned bit_size) +{ + unsigned buffer_size; + + if (bit_size > INT_MAX - 7 || !buffer) { + buffer = + bc->buffer = + bc->ptr = NULL; + bc->bits_left = 0; + return AVERROR_INVALIDDATA; + } + + buffer_size = (bit_size + 7) >> 3; + + bc->buffer = buffer; + bc->buffer_end = buffer + buffer_size; + bc->ptr = bc->buffer; + bc->size_in_bits = bit_size; + bc->bits_left = 0; + bc->bits = 0; + + refill_64(bc); + + return 0; +} + +/* Initialize BitstreamContext with buffer size in bytes instead of bits. */ +static inline int bitstream_init8(BitstreamContext *bc, const uint8_t *buffer, + unsigned byte_size) +{ + if (byte_size > INT_MAX / 8) + return AVERROR_INVALIDDATA; + return bitstream_init(bc, buffer, byte_size * 8); +} + +/* Return number of bits already read. */ +static inline int bitstream_tell(const BitstreamContext *bc) +{ + return (bc->ptr - bc->buffer) * 8 - bc->bits_left; +} + +/* Return buffer size in bits. */ +static inline int bitstream_tell_size(const BitstreamContext *bc) +{ + return bc->size_in_bits; +} + +/* Return the number of the bits left in a buffer. */ +static inline int bitstream_bits_left(const BitstreamContext *bc) +{ + return (bc->buffer - bc->ptr) * 8 + bc->size_in_bits + bc->bits_left; +} + +static inline uint64_t get_val(BitstreamContext *bc, unsigned n) +{ +#ifdef BITSTREAM_READER_LE + uint64_t ret = bc->bits & ((UINT64_C(1) << n) - 1); + bc->bits >>= n; +#else + uint64_t ret = bc->bits >> (64 - n); + bc->bits <<= n; +#endif + bc->bits_left -= n; + + return ret; +} + +/* Return one bit from the buffer. */ +static inline unsigned bitstream_read_bit(BitstreamContext *bc) +{ + if (!bc->bits_left) + refill_64(bc); + + return get_val(bc, 1); +} + +/* Return n bits from the buffer. n has to be in the 0-63 range. */ +static inline uint64_t bitstream_read_63(BitstreamContext *bc, unsigned n) +{ + uint64_t ret = 0; +#ifdef BITSTREAM_READER_LE + uint64_t left = 0; +#endif + + if (!n) + return 0; + + if (n > bc->bits_left) { + n -= bc->bits_left; +#ifdef BITSTREAM_READER_LE + left = bc->bits_left; +#endif + ret = get_val(bc, bc->bits_left); + refill_64(bc); + } + +#ifdef BITSTREAM_READER_LE + ret = get_val(bc, n) << left | ret; +#else + ret = get_val(bc, n) | ret << n; +#endif + + return ret; +} + +/* Return n bits from the buffer. n has to be in the 0-32 range. */ +static inline uint32_t bitstream_read(BitstreamContext *bc, unsigned n) +{ + if (!n) + return 0; + + if (n > bc->bits_left) { + refill_32(bc); + if (bc->bits_left < 32) + bc->bits_left = n; + } + + return get_val(bc, n); +} + +/* Return n bits from the buffer as a signed integer. + * n has to be in the 0-32 range. */ +static inline int32_t bitstream_read_signed(BitstreamContext *bc, unsigned n) +{ + return sign_extend(bitstream_read(bc, n), n); +} + +static inline unsigned show_val(const BitstreamContext *bc, unsigned n) +{ +#ifdef BITSTREAM_READER_LE + return bc->bits & ((UINT64_C(1) << n) - 1); +#else + return bc->bits >> (64 - n); +#endif +} + +/* Return n bits from the buffer, but do not change the buffer state. + * n has to be in the 0-32 range. */ +static inline unsigned bitstream_peek(BitstreamContext *bc, unsigned n) +{ + if (n > bc->bits_left) + refill_32(bc); + + return show_val(bc, n); +} + +/* Return n bits from the buffer as a signed integer, but do not change the + * buffer state. n has to be in the 0-32 range. */ +static inline int bitstream_peek_signed(BitstreamContext *bc, unsigned n) +{ + return sign_extend(bitstream_peek(bc, n), n); +} + +static inline void skip_remaining(BitstreamContext *bc, unsigned n) +{ +#ifdef BITSTREAM_READER_LE + bc->bits >>= n; +#else + bc->bits <<= n; +#endif + bc->bits_left -= n; +} + +/* Skip n bits in the buffer. */ +static inline void bitstream_skip(BitstreamContext *bc, unsigned n) +{ + if (n <= bc->bits_left) + skip_remaining(bc, n); + else { + n -= bc->bits_left; + skip_remaining(bc, bc->bits_left); + if (n >= 64) { + unsigned skip = n / 8; + + n -= skip * 8; + bc->ptr += skip; + } + refill_64(bc); + if (n) + skip_remaining(bc, n); + } +} + +/* Seek to the given bit position. */ +static inline void bitstream_seek(BitstreamContext *bc, unsigned pos) +{ + bc->ptr = bc->buffer; + bc->bits = 0; + bc->bits_left = 0; + + bitstream_skip(bc, pos); +} + +/* Skip bits to a byte boundary. */ +static inline const uint8_t *bitstream_align(BitstreamContext *bc) +{ + unsigned n = -bitstream_tell(bc) & 7; + if (n) + bitstream_skip(bc, n); + return bc->buffer + (bitstream_tell(bc) >> 3); +} + +/* Read MPEG-1 dc-style VLC (sign bit + mantissa with no MSB). + * If MSB not set it is negative. */ +static inline int bitstream_read_xbits(BitstreamContext *bc, unsigned length) +{ + int32_t cache = bitstream_peek(bc, 32); + int sign = ~cache >> 31; + skip_remaining(bc, length); + + return ((((uint32_t)(sign ^ cache)) >> (32 - length)) ^ sign) - sign; +} + +/* Return the LUT element for the given bitstream configuration. */ +static inline int set_idx(BitstreamContext *bc, int code, int *n, int *nb_bits, + VLC_TYPE (*table)[2]) +{ + unsigned idx; + + *nb_bits = -*n; + idx = bitstream_peek(bc, *nb_bits) + code; + *n = table[idx][1]; + + return table[idx][0]; +} + +/** + * Parse a VLC code. + * @param bits is the number of bits which will be read at once, must be + * identical to nb_bits in init_vlc() + * @param max_depth is the number of times bits bits must be read to completely + * read the longest VLC code + * = (max_vlc_length + bits - 1) / bits + * If the VLC code is invalid and max_depth = 1, then no bits will be removed. + * If the VLC code is invalid and max_depth > 1, then the number of bits removed + * is undefined. */ +static inline int bitstream_read_vlc(BitstreamContext *bc, VLC_TYPE (*table)[2], + int bits, int max_depth) +{ + int nb_bits; + unsigned idx = bitstream_peek(bc, bits); + int code = table[idx][0]; + int n = table[idx][1]; + + if (max_depth > 1 && n < 0) { + skip_remaining(bc, bits); + code = set_idx(bc, code, &n, &nb_bits, table); + if (max_depth > 2 && n < 0) { + skip_remaining(bc, nb_bits); + code = set_idx(bc, code, &n, &nb_bits, table); + } + } + skip_remaining(bc, n); + + return code; +} + +#define BITSTREAM_RL_VLC(level, run, bc, table, bits, max_depth) \ + do { \ + int n, nb_bits; \ + unsigned index = bitstream_peek(bc, bits); \ + level = table[index].level; \ + n = table[index].len; \ + \ + if (max_depth > 1 && n < 0) { \ + bitstream_skip(bc, bits); \ + \ + nb_bits = -n; \ + \ + index = bitstream_peek(bc, nb_bits) + level; \ + level = table[index].level; \ + n = table[index].len; \ + if (max_depth > 2 && n < 0) { \ + bitstream_skip(bc, nb_bits); \ + nb_bits = -n; \ + \ + index = bitstream_peek(bc, nb_bits) + level; \ + level = table[index].level; \ + n = table[index].len; \ + } \ + } \ + run = table[index].run; \ + bitstream_skip(bc, n); \ + } while (0) + +/* Return decoded truncated unary code for the values 0, 1, 2. */ +static inline int bitstream_decode012(BitstreamContext *bc) +{ + if (!bitstream_read_bit(bc)) + return 0; + else + return bitstream_read_bit(bc) + 1; +} + +/* Return decoded truncated unary code for the values 2, 1, 0. */ +static inline int bitstream_decode210(BitstreamContext *bc) +{ + if (bitstream_read_bit(bc)) + return 0; + else + return 2 - bitstream_read_bit(bc); +} + +/* Read sign bit and flip the sign of the provided value accordingly. */ +static inline int bitstream_apply_sign(BitstreamContext *bc, int val) +{ + int sign = bitstream_read_signed(bc, 1); + return (val ^ sign) - sign; +} + +#endif /* AVCODEC_BITSTREAM_H */ From d182d8a6d3203f7fbab11179e88f26b8befe899e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandra=20H=C3=A1jkov=C3=A1?= Date: Sat, 6 Feb 2016 21:41:31 +0100 Subject: [PATCH 02/35] cllc: Convert to the new bitstream reader Signed-off-by: Anton Khirnov --- libavcodec/cllc.c | 88 +++++++++++++++++++---------------------------- 1 file changed, 36 insertions(+), 52 deletions(-) diff --git a/libavcodec/cllc.c b/libavcodec/cllc.c index cdbed74474..bac2b730e1 100644 --- a/libavcodec/cllc.c +++ b/libavcodec/cllc.c @@ -23,11 +23,13 @@ #include #include "libavutil/intreadwrite.h" + +#include "bitstream.h" #include "bswapdsp.h" #include "canopus.h" -#include "get_bits.h" #include "avcodec.h" #include "internal.h" +#include "vlc.h" typedef struct CLLCContext { AVCodecContext *avctx; @@ -37,7 +39,7 @@ typedef struct CLLCContext { int swapped_buf_size; } CLLCContext; -static int read_code_table(CLLCContext *ctx, GetBitContext *gb, VLC *vlc) +static int read_code_table(CLLCContext *ctx, BitstreamContext *bc, VLC *vlc) { uint8_t symbols[256]; uint8_t bits[256]; @@ -49,10 +51,10 @@ static int read_code_table(CLLCContext *ctx, GetBitContext *gb, VLC *vlc) count = 0; num_codes_sum = 0; - num_lens = get_bits(gb, 5); + num_lens = bitstream_read(bc, 5); for (i = 0; i < num_lens; i++) { - num_codes = get_bits(gb, 9); + num_codes = bitstream_read(bc, 9); num_codes_sum += num_codes; if (num_codes_sum > 256) { @@ -64,7 +66,7 @@ static int read_code_table(CLLCContext *ctx, GetBitContext *gb, VLC *vlc) } for (j = 0; j < num_codes; j++) { - symbols[count] = get_bits(gb, 8); + symbols[count] = bitstream_read(bc, 8); bits[count] = i + 1; codes[count] = prefix++; @@ -82,7 +84,7 @@ static int read_code_table(CLLCContext *ctx, GetBitContext *gb, VLC *vlc) * Unlike the RGB24 read/restore, which reads in a component at a time, * ARGB read/restore reads in ARGB quads. */ -static int read_argb_line(CLLCContext *ctx, GetBitContext *gb, int *top_left, +static int read_argb_line(CLLCContext *ctx, BitstreamContext *bc, int *top_left, VLC *vlc, uint8_t *outbuf) { uint8_t *dst; @@ -90,8 +92,6 @@ static int read_argb_line(CLLCContext *ctx, GetBitContext *gb, int *top_left, int code; int i; - OPEN_READER(bits, gb); - dst = outbuf; pred[0] = top_left[0]; pred[1] = top_left[1]; @@ -100,8 +100,7 @@ static int read_argb_line(CLLCContext *ctx, GetBitContext *gb, int *top_left, for (i = 0; i < ctx->avctx->width; i++) { /* Always get the alpha component */ - UPDATE_CACHE(bits, gb); - GET_VLC(code, bits, gb, vlc[0].table, 7, 2); + code = bitstream_read_vlc(bc, vlc[0].table, 7, 2); pred[0] += code; dst[0] = pred[0]; @@ -109,22 +108,19 @@ static int read_argb_line(CLLCContext *ctx, GetBitContext *gb, int *top_left, /* Skip the components if they are entirely transparent */ if (dst[0]) { /* Red */ - UPDATE_CACHE(bits, gb); - GET_VLC(code, bits, gb, vlc[1].table, 7, 2); + code = bitstream_read_vlc(bc, vlc[1].table, 7, 2); pred[1] += code; dst[1] = pred[1]; /* Green */ - UPDATE_CACHE(bits, gb); - GET_VLC(code, bits, gb, vlc[2].table, 7, 2); + code = bitstream_read_vlc(bc, vlc[2].table, 7, 2); pred[2] += code; dst[2] = pred[2]; /* Blue */ - UPDATE_CACHE(bits, gb); - GET_VLC(code, bits, gb, vlc[3].table, 7, 2); + code = bitstream_read_vlc(bc, vlc[3].table, 7, 2); pred[3] += code; dst[3] = pred[3]; @@ -137,8 +133,6 @@ static int read_argb_line(CLLCContext *ctx, GetBitContext *gb, int *top_left, dst += 4; } - CLOSE_READER(bits, gb); - top_left[0] = outbuf[0]; /* Only stash components if they are not transparent */ @@ -151,65 +145,55 @@ static int read_argb_line(CLLCContext *ctx, GetBitContext *gb, int *top_left, return 0; } -static int read_rgb24_component_line(CLLCContext *ctx, GetBitContext *gb, +static int read_rgb24_component_line(CLLCContext *ctx, BitstreamContext *bc, int *top_left, VLC *vlc, uint8_t *outbuf) { uint8_t *dst; int pred, code; int i; - OPEN_READER(bits, gb); - dst = outbuf; pred = *top_left; /* Simultaneously read and restore the line */ for (i = 0; i < ctx->avctx->width; i++) { - UPDATE_CACHE(bits, gb); - GET_VLC(code, bits, gb, vlc->table, 7, 2); + code = bitstream_read_vlc(bc, vlc->table, 7, 2); pred += code; dst[0] = pred; dst += 3; } - CLOSE_READER(bits, gb); - /* Stash the first pixel */ *top_left = outbuf[0]; return 0; } -static int read_yuv_component_line(CLLCContext *ctx, GetBitContext *gb, +static int read_yuv_component_line(CLLCContext *ctx, BitstreamContext *bc, int *top_left, VLC *vlc, uint8_t *outbuf, int is_chroma) { int pred, code; int i; - OPEN_READER(bits, gb); - pred = *top_left; /* Simultaneously read and restore the line */ for (i = 0; i < ctx->avctx->width >> is_chroma; i++) { - UPDATE_CACHE(bits, gb); - GET_VLC(code, bits, gb, vlc->table, 7, 2); + code = bitstream_read_vlc(bc, vlc->table, 7, 2); pred += code; outbuf[i] = pred; } - CLOSE_READER(bits, gb); - /* Stash the first pixel */ *top_left = outbuf[0]; return 0; } -static int decode_argb_frame(CLLCContext *ctx, GetBitContext *gb, AVFrame *pic) +static int decode_argb_frame(CLLCContext *ctx, BitstreamContext *bc, AVFrame *pic) { AVCodecContext *avctx = ctx->avctx; uint8_t *dst; @@ -225,11 +209,11 @@ static int decode_argb_frame(CLLCContext *ctx, GetBitContext *gb, AVFrame *pic) dst = pic->data[0]; - skip_bits(gb, 16); + bitstream_skip(bc, 16); /* Read in code table for each plane */ for (i = 0; i < 4; i++) { - ret = read_code_table(ctx, gb, &vlc[i]); + ret = read_code_table(ctx, bc, &vlc[i]); if (ret < 0) { for (j = 0; j <= i; j++) ff_free_vlc(&vlc[j]); @@ -242,7 +226,7 @@ static int decode_argb_frame(CLLCContext *ctx, GetBitContext *gb, AVFrame *pic) /* Read in and restore every line */ for (i = 0; i < avctx->height; i++) { - read_argb_line(ctx, gb, pred, vlc, dst); + read_argb_line(ctx, bc, pred, vlc, dst); dst += pic->linesize[0]; } @@ -253,7 +237,7 @@ static int decode_argb_frame(CLLCContext *ctx, GetBitContext *gb, AVFrame *pic) return 0; } -static int decode_rgb24_frame(CLLCContext *ctx, GetBitContext *gb, AVFrame *pic) +static int decode_rgb24_frame(CLLCContext *ctx, BitstreamContext *bc, AVFrame *pic) { AVCodecContext *avctx = ctx->avctx; uint8_t *dst; @@ -268,11 +252,11 @@ static int decode_rgb24_frame(CLLCContext *ctx, GetBitContext *gb, AVFrame *pic) dst = pic->data[0]; - skip_bits(gb, 16); + bitstream_skip(bc, 16); /* Read in code table for each plane */ for (i = 0; i < 3; i++) { - ret = read_code_table(ctx, gb, &vlc[i]); + ret = read_code_table(ctx, bc, &vlc[i]); if (ret < 0) { for (j = 0; j <= i; j++) ff_free_vlc(&vlc[j]); @@ -286,7 +270,7 @@ static int decode_rgb24_frame(CLLCContext *ctx, GetBitContext *gb, AVFrame *pic) /* Read in and restore every line */ for (i = 0; i < avctx->height; i++) { for (j = 0; j < 3; j++) - read_rgb24_component_line(ctx, gb, &pred[j], &vlc[j], &dst[j]); + read_rgb24_component_line(ctx, bc, &pred[j], &vlc[j], &dst[j]); dst += pic->linesize[0]; } @@ -297,7 +281,7 @@ static int decode_rgb24_frame(CLLCContext *ctx, GetBitContext *gb, AVFrame *pic) return 0; } -static int decode_yuv_frame(CLLCContext *ctx, GetBitContext *gb, AVFrame *pic) +static int decode_yuv_frame(CLLCContext *ctx, BitstreamContext *bc, AVFrame *pic) { AVCodecContext *avctx = ctx->avctx; uint8_t block; @@ -315,9 +299,9 @@ static int decode_yuv_frame(CLLCContext *ctx, GetBitContext *gb, AVFrame *pic) dst[1] = pic->data[1]; dst[2] = pic->data[2]; - skip_bits(gb, 8); + bitstream_skip(bc, 8); - block = get_bits(gb, 8); + block = bitstream_read(bc, 8); if (block) { avpriv_request_sample(ctx->avctx, "Blocked YUV"); return AVERROR_PATCHWELCOME; @@ -325,7 +309,7 @@ static int decode_yuv_frame(CLLCContext *ctx, GetBitContext *gb, AVFrame *pic) /* Read in code table for luma and chroma */ for (i = 0; i < 2; i++) { - ret = read_code_table(ctx, gb, &vlc[i]); + ret = read_code_table(ctx, bc, &vlc[i]); if (ret < 0) { for (j = 0; j <= i; j++) ff_free_vlc(&vlc[j]); @@ -338,9 +322,9 @@ static int decode_yuv_frame(CLLCContext *ctx, GetBitContext *gb, AVFrame *pic) /* Read in and restore every line */ for (i = 0; i < avctx->height; i++) { - read_yuv_component_line(ctx, gb, &pred[0], &vlc[0], dst[0], 0); /* Y */ - read_yuv_component_line(ctx, gb, &pred[1], &vlc[1], dst[1], 1); /* U */ - read_yuv_component_line(ctx, gb, &pred[2], &vlc[1], dst[2], 1); /* V */ + read_yuv_component_line(ctx, bc, &pred[0], &vlc[0], dst[0], 0); /* Y */ + read_yuv_component_line(ctx, bc, &pred[1], &vlc[1], dst[1], 1); /* U */ + read_yuv_component_line(ctx, bc, &pred[2], &vlc[1], dst[2], 1); /* V */ for (j = 0; j < 3; j++) dst[j] += pic->linesize[j]; @@ -360,7 +344,7 @@ static int cllc_decode_frame(AVCodecContext *avctx, void *data, uint8_t *src = avpkt->data; uint32_t info_tag, info_offset; int data_size; - GetBitContext gb; + BitstreamContext bc; int coding_type, ret; if (avpkt->size < 4 + 4) { @@ -398,7 +382,7 @@ static int cllc_decode_frame(AVCodecContext *avctx, void *data, ctx->bdsp.bswap16_buf((uint16_t *) ctx->swapped_buf, (uint16_t *) src, data_size / 2); - init_get_bits(&gb, ctx->swapped_buf, data_size * 8); + bitstream_init8(&bc, ctx->swapped_buf, data_size); /* * Read in coding type. The types are as follows: @@ -422,7 +406,7 @@ static int cllc_decode_frame(AVCodecContext *avctx, void *data, return ret; } - ret = decode_yuv_frame(ctx, &gb, pic); + ret = decode_yuv_frame(ctx, &bc, pic); if (ret < 0) return ret; @@ -438,7 +422,7 @@ static int cllc_decode_frame(AVCodecContext *avctx, void *data, return ret; } - ret = decode_rgb24_frame(ctx, &gb, pic); + ret = decode_rgb24_frame(ctx, &bc, pic); if (ret < 0) return ret; @@ -453,7 +437,7 @@ static int cllc_decode_frame(AVCodecContext *avctx, void *data, return ret; } - ret = decode_argb_frame(ctx, &gb, pic); + ret = decode_argb_frame(ctx, &bc, pic); if (ret < 0) return ret; From adb1ebb36cdfa787e97eb705a73aa551caed39b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandra=20H=C3=A1jkov=C3=A1?= Date: Wed, 17 Feb 2016 18:16:06 +0100 Subject: [PATCH 03/35] eamad: Convert to the new bitstream reader Signed-off-by: Anton Khirnov --- libavcodec/eamad.c | 42 +++++++++++++++++++----------------------- 1 file changed, 19 insertions(+), 23 deletions(-) diff --git a/libavcodec/eamad.c b/libavcodec/eamad.c index 070cfdb355..7509c2ddd4 100644 --- a/libavcodec/eamad.c +++ b/libavcodec/eamad.c @@ -29,16 +29,17 @@ */ #include "avcodec.h" +#include "bitstream.h" #include "blockdsp.h" #include "bytestream.h" #include "bswapdsp.h" -#include "get_bits.h" #include "aandcttab.h" #include "eaidct.h" #include "idctdsp.h" #include "internal.h" #include "mpeg12data.h" #include "mpeg12vlc.h" +#include "vlc.h" #define EA_PREAMBLE_SIZE 8 #define MADk_TAG MKTAG('M', 'A', 'D', 'k') /* MAD I-frame */ @@ -51,7 +52,7 @@ typedef struct MadContext { BswapDSPContext bbdsp; IDCTDSPContext idsp; AVFrame *last_frame; - GetBitContext gb; + BitstreamContext bc; void *bitstream_buf; unsigned int bitstream_buf_size; DECLARE_ALIGNED(16, int16_t, block)[64]; @@ -129,17 +130,15 @@ static inline void decode_block_intra(MadContext *s, int16_t * block) const uint8_t *scantable = s->scantable.permutated; int16_t *quant_matrix = s->quant_matrix; - block[0] = (128 + get_sbits(&s->gb, 8)) * quant_matrix[0]; + block[0] = (128 + bitstream_read_signed(&s->bc, 8)) * quant_matrix[0]; /* The RL decoder is derived from mpeg1_decode_block_intra; Escaped level and run values a decoded differently */ i = 0; { - OPEN_READER(re, &s->gb); /* now quantify & encode AC coefficients */ for (;;) { - UPDATE_CACHE(re, &s->gb); - GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0); + BITSTREAM_RL_VLC(level, run, &s->bc, rl->rl_vlc[0], TEX_VLC_BITS, 2); if (level == 127) { break; @@ -153,15 +152,12 @@ static inline void decode_block_intra(MadContext *s, int16_t * block) j = scantable[i]; level = (level*quant_matrix[j]) >> 4; level = (level-1)|1; - level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1); - LAST_SKIP_BITS(re, &s->gb, 1); + level = bitstream_apply_sign(&s->bc, level); } else { /* escape */ - UPDATE_CACHE(re, &s->gb); - level = SHOW_SBITS(re, &s->gb, 10); SKIP_BITS(re, &s->gb, 10); + level = bitstream_read_signed(&s->bc, 10); - UPDATE_CACHE(re, &s->gb); - run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6); + run = bitstream_read(&s->bc, 6) + 1; i += run; if (i > 63) { @@ -183,17 +179,17 @@ static inline void decode_block_intra(MadContext *s, int16_t * block) block[j] = level; } - CLOSE_READER(re, &s->gb); } } -static int decode_motion(GetBitContext *gb) +static int decode_motion(BitstreamContext *bc) { int value = 0; - if (get_bits1(gb)) { - if (get_bits1(gb)) + + if (bitstream_read_bit(bc)) { + if (bitstream_read_bit(bc)) value = -17; - value += get_bits(gb, 4) + 1; + value += bitstream_read(bc, 4) + 1; } return value; } @@ -205,11 +201,11 @@ static void decode_mb(MadContext *s, AVFrame *frame, int inter) int j; if (inter) { - int v = decode210(&s->gb); + int v = bitstream_decode210(&s->bc); if (v < 2) { - mv_map = v ? get_bits(&s->gb, 6) : 63; - mv_x = decode_motion(&s->gb); - mv_y = decode_motion(&s->gb); + mv_map = v ? bitstream_read(&s->bc, 6) : 63; + mv_x = decode_motion(&s->bc); + mv_y = decode_motion(&s->bc); } else { mv_map = 0; } @@ -217,7 +213,7 @@ static void decode_mb(MadContext *s, AVFrame *frame, int inter) for (j=0; j<6; j++) { if (mv_map & (1<gb); + int add = 2 * decode_motion(&s->bc); comp_block(s, frame, s->mb_x, s->mb_y, j, mv_x, mv_y, add); } else { s->bdsp.clear_block(s->block); @@ -299,7 +295,7 @@ static int decode_frame(AVCodecContext *avctx, return AVERROR(ENOMEM); s->bbdsp.bswap16_buf(s->bitstream_buf, (const uint16_t *)(buf + bytestream2_tell(&gb)), bytestream2_get_bytes_left(&gb) / 2); - init_get_bits(&s->gb, s->bitstream_buf, 8*(bytestream2_get_bytes_left(&gb))); + bitstream_init8(&s->bc, s->bitstream_buf, bytestream2_get_bytes_left(&gb)); for (s->mb_y=0; s->mb_y < (avctx->height+15)/16; s->mb_y++) for (s->mb_x=0; s->mb_x < (avctx->width +15)/16; s->mb_x++) From 7d957b3f477a8cb333f8ef2697c9aa5363ad0c9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandra=20H=C3=A1jkov=C3=A1?= Date: Sat, 9 Apr 2016 20:31:47 +0200 Subject: [PATCH 04/35] ea: Convert to the new bitstream reader Signed-off-by: Anton Khirnov --- libavcodec/eatgq.c | 34 +++++++++++++++++----------------- libavcodec/eatgv.c | 22 +++++++++++----------- 2 files changed, 28 insertions(+), 28 deletions(-) diff --git a/libavcodec/eatgq.c b/libavcodec/eatgq.c index 835547125e..9abedcc783 100644 --- a/libavcodec/eatgq.c +++ b/libavcodec/eatgq.c @@ -31,9 +31,9 @@ #define BITSTREAM_READER_LE #include "aandcttab.h" #include "avcodec.h" +#include "bitstream.h" #include "bytestream.h" #include "eaidct.h" -#include "get_bits.h" #include "idctdsp.h" #include "internal.h" @@ -58,44 +58,44 @@ static av_cold int tgq_decode_init(AVCodecContext *avctx) return 0; } -static void tgq_decode_block(TgqContext *s, int16_t block[64], GetBitContext *gb) +static void tgq_decode_block(TgqContext *s, int16_t block[64], BitstreamContext *bc) { uint8_t *perm = s->scantable.permutated; int i, j, value; - block[0] = get_sbits(gb, 8) * s->qtable[0]; + block[0] = bitstream_read_signed(bc, 8) * s->qtable[0]; for (i = 1; i < 64;) { - switch (show_bits(gb, 3)) { + switch (bitstream_peek(bc, 3)) { case 4: block[perm[i++]] = 0; case 0: block[perm[i++]] = 0; - skip_bits(gb, 3); + bitstream_skip(bc, 3); break; case 5: case 1: - skip_bits(gb, 2); - value = get_bits(gb, 6); + bitstream_skip(bc, 2); + value = bitstream_read(bc, 6); for (j = 0; j < value; j++) block[perm[i++]] = 0; break; case 6: - skip_bits(gb, 3); + bitstream_skip(bc, 3); block[perm[i]] = -s->qtable[perm[i]]; i++; break; case 2: - skip_bits(gb, 3); + bitstream_skip(bc, 3); block[perm[i]] = s->qtable[perm[i]]; i++; break; case 7: // 111b case 3: // 011b - skip_bits(gb, 2); - if (show_bits(gb, 6) == 0x3F) { - skip_bits(gb, 6); - block[perm[i]] = get_sbits(gb, 8) * s->qtable[perm[i]]; + bitstream_skip(bc, 2); + if (bitstream_peek(bc, 6) == 0x3F) { + bitstream_skip(bc, 6); + block[perm[i]] = bitstream_read_signed(bc, 8) * s->qtable[perm[i]]; } else { - block[perm[i]] = get_sbits(gb, 6) * s->qtable[perm[i]]; + block[perm[i]] = bitstream_read_signed(bc, 6) * s->qtable[perm[i]]; } i++; break; @@ -156,10 +156,10 @@ static void tgq_decode_mb(TgqContext *s, AVFrame *frame, int mb_y, int mb_x) mode = bytestream2_get_byte(&s->gb); if (mode > 12) { - GetBitContext gb; - init_get_bits(&gb, s->gb.buffer, FFMIN(s->gb.buffer_end - s->gb.buffer, mode) * 8); + BitstreamContext bc; + bitstream_init(&bc, s->gb.buffer, FFMIN(s->gb.buffer_end - s->gb.buffer, mode) * 8); for (i = 0; i < 6; i++) - tgq_decode_block(s, s->block[i], &gb); + tgq_decode_block(s, s->block[i], &bc); tgq_idct_put_mb(s, s->block, frame, mb_x, mb_y); bytestream2_skip(&s->gb, mode); } else { diff --git a/libavcodec/eatgv.c b/libavcodec/eatgv.c index 7a50d010d4..549b5b6d3c 100644 --- a/libavcodec/eatgv.c +++ b/libavcodec/eatgv.c @@ -33,7 +33,7 @@ #define BITSTREAM_READER_LE #include "avcodec.h" -#include "get_bits.h" +#include "bitstream.h" #include "internal.h" #define EA_PREAMBLE_SIZE 8 @@ -153,7 +153,7 @@ static int tgv_decode_inter(TgvContext *s, AVFrame *frame, int num_blocks_packed; int vector_bits; int i,j,x,y; - GetBitContext gb; + BitstreamContext bc; int mvbits; const uint8_t *blocks_raw; @@ -166,7 +166,7 @@ static int tgv_decode_inter(TgvContext *s, AVFrame *frame, vector_bits = AV_RL16(&buf[6]); buf += 12; - if (vector_bits > MIN_CACHE_BITS || !vector_bits) { + if (vector_bits > 32 || !vector_bits) { av_log(s->avctx, AV_LOG_ERROR, "Invalid value for motion vector bits: %d\n", vector_bits); return AVERROR_INVALIDDATA; @@ -195,10 +195,10 @@ static int tgv_decode_inter(TgvContext *s, AVFrame *frame, if (buf + (mvbits >> 3) + 16 * num_blocks_raw + 8 * num_blocks_packed > buf_end) return AVERROR_INVALIDDATA; - init_get_bits(&gb, buf, mvbits); + bitstream_init(&bc, buf, mvbits); for (i = 0; i < num_mvs; i++) { - s->mv_codebook[i][0] = get_sbits(&gb, 10); - s->mv_codebook[i][1] = get_sbits(&gb, 10); + s->mv_codebook[i][0] = bitstream_read_signed(&bc, 10); + s->mv_codebook[i][1] = bitstream_read_signed(&bc, 10); } buf += mvbits >> 3; @@ -207,23 +207,23 @@ static int tgv_decode_inter(TgvContext *s, AVFrame *frame, buf += num_blocks_raw * 16; /* read compressed blocks */ - init_get_bits(&gb, buf, (buf_end - buf) << 3); + bitstream_init(&bc, buf, (buf_end - buf) << 3); for (i = 0; i < num_blocks_packed; i++) { int tmp[4]; for (j = 0; j < 4; j++) - tmp[j] = get_bits(&gb, 8); + tmp[j] = bitstream_read(&bc, 8); for (j = 0; j < 16; j++) - s->block_codebook[i][15-j] = tmp[get_bits(&gb, 2)]; + s->block_codebook[i][15-j] = tmp[bitstream_read(&bc, 2)]; } - if (get_bits_left(&gb) < vector_bits * + if (bitstream_bits_left(&bc) < vector_bits * (s->avctx->height / 4) * (s->avctx->width / 4)) return AVERROR_INVALIDDATA; /* read vectors and build frame */ for (y = 0; y < s->avctx->height / 4; y++) for (x = 0; x < s->avctx->width / 4; x++) { - unsigned int vector = get_bits(&gb, vector_bits); + unsigned int vector = bitstream_read(&bc, vector_bits); const uint8_t *src; ptrdiff_t src_stride; From b25180801bd0a544e5c49efb92dbe78904289a8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandra=20H=C3=A1jkov=C3=A1?= Date: Mon, 21 Mar 2016 20:46:58 +0100 Subject: [PATCH 05/35] on2avc: Convert to the new bitstream reader Signed-off-by: Anton Khirnov --- libavcodec/on2avc.c | 64 +++++++++++++++++++++++---------------------- 1 file changed, 33 insertions(+), 31 deletions(-) diff --git a/libavcodec/on2avc.c b/libavcodec/on2avc.c index 2a528c6f3b..1b81980f7d 100644 --- a/libavcodec/on2avc.c +++ b/libavcodec/on2avc.c @@ -22,11 +22,13 @@ #include "libavutil/channel_layout.h" #include "libavutil/float_dsp.h" + #include "avcodec.h" +#include "bitstream.h" #include "bytestream.h" #include "fft.h" -#include "get_bits.h" #include "internal.h" +#include "vlc.h" #include "on2avcdata.h" @@ -84,11 +86,11 @@ typedef struct On2AVCContext { DECLARE_ALIGNED(32, float, short_win)[ON2AVC_SUBFRAME_SIZE / 8]; } On2AVCContext; -static void on2avc_read_ms_info(On2AVCContext *c, GetBitContext *gb) +static void on2avc_read_ms_info(On2AVCContext *c, BitstreamContext *bc) { int w, b, band_off = 0; - c->ms_present = get_bits1(gb); + c->ms_present = bitstream_read_bit(bc); if (!c->ms_present) return; for (w = 0; w < c->num_windows; w++) { @@ -100,12 +102,12 @@ static void on2avc_read_ms_info(On2AVCContext *c, GetBitContext *gb) continue; } for (b = 0; b < c->num_bands; b++) - c->ms_info[band_off++] = get_bits1(gb); + c->ms_info[band_off++] = bitstream_read_bit(bc); } } // do not see Table 17 in ISO/IEC 13818-7 -static int on2avc_decode_band_types(On2AVCContext *c, GetBitContext *gb) +static int on2avc_decode_band_types(On2AVCContext *c, BitstreamContext *bc) { int bits_per_sect = c->is_long ? 5 : 3; int esc_val = (1 << bits_per_sect) - 1; @@ -113,10 +115,10 @@ static int on2avc_decode_band_types(On2AVCContext *c, GetBitContext *gb) int band = 0, i, band_type, run_len, run; while (band < num_bands) { - band_type = get_bits(gb, 4); + band_type = bitstream_read(bc, 4); run_len = 1; do { - run = get_bits(gb, bits_per_sect); + run = bitstream_read(bc, bits_per_sect); run_len += run; } while (run == esc_val); if (band + run_len > num_bands) { @@ -135,7 +137,7 @@ static int on2avc_decode_band_types(On2AVCContext *c, GetBitContext *gb) // completely not like Table 18 in ISO/IEC 13818-7 // (no intensity stereo, different coding for the first coefficient) -static int on2avc_decode_band_scales(On2AVCContext *c, GetBitContext *gb) +static int on2avc_decode_band_scales(On2AVCContext *c, BitstreamContext *bc) { int w, w2, b, scale, first = 1; int band_off = 0; @@ -165,10 +167,10 @@ static int on2avc_decode_band_scales(On2AVCContext *c, GetBitContext *gb) } } if (first) { - scale = get_bits(gb, 7); + scale = bitstream_read(bc, 7); first = 0; } else { - scale += get_vlc2(gb, c->scale_diff.table, 9, 3) - 60; + scale += bitstream_read_vlc(bc, c->scale_diff.table, 9, 3) - 60; } if (scale < 0 || scale > 127) { av_log(c->avctx, AV_LOG_ERROR, "Invalid scale value %d\n", @@ -188,13 +190,13 @@ static inline float on2avc_scale(int v, float scale) } // spectral data is coded completely differently - there are no unsigned codebooks -static int on2avc_decode_quads(On2AVCContext *c, GetBitContext *gb, float *dst, +static int on2avc_decode_quads(On2AVCContext *c, BitstreamContext *bc, float *dst, int dst_size, int type, float band_scale) { int i, j, val, val1; for (i = 0; i < dst_size; i += 4) { - val = get_vlc2(gb, c->cb_vlc[type].table, 9, 3); + val = bitstream_read_vlc(bc, c->cb_vlc[type].table, 9, 3); for (j = 0; j < 4; j++) { val1 = sign_extend((val >> (12 - j * 4)) & 0xF, 4); @@ -205,11 +207,11 @@ static int on2avc_decode_quads(On2AVCContext *c, GetBitContext *gb, float *dst, return 0; } -static inline int get_egolomb(GetBitContext *gb) +static inline int get_egolomb(BitstreamContext *bc) { int v = 4; - while (get_bits1(gb)) { + while (bitstream_read_bit(bc)) { v++; if (v > 30) { av_log(NULL, AV_LOG_WARNING, "Too large golomb code in get_egolomb.\n"); @@ -218,27 +220,27 @@ static inline int get_egolomb(GetBitContext *gb) } } - return (1 << v) + get_bits_long(gb, v); + return (1 << v) + bitstream_read(bc, v); } -static int on2avc_decode_pairs(On2AVCContext *c, GetBitContext *gb, float *dst, +static int on2avc_decode_pairs(On2AVCContext *c, BitstreamContext *bc, float *dst, int dst_size, int type, float band_scale) { int i, val, val1, val2, sign; for (i = 0; i < dst_size; i += 2) { - val = get_vlc2(gb, c->cb_vlc[type].table, 9, 3); + val = bitstream_read_vlc(bc, c->cb_vlc[type].table, 9, 3); val1 = sign_extend(val >> 8, 8); val2 = sign_extend(val & 0xFF, 8); if (type == ON2AVC_ESC_CB) { if (val1 <= -16 || val1 >= 16) { sign = 1 - (val1 < 0) * 2; - val1 = sign * get_egolomb(gb); + val1 = sign * get_egolomb(bc); } if (val2 <= -16 || val2 >= 16) { sign = 1 - (val2 < 0) * 2; - val2 = sign * get_egolomb(gb); + val2 = sign * get_egolomb(bc); } } @@ -249,15 +251,15 @@ static int on2avc_decode_pairs(On2AVCContext *c, GetBitContext *gb, float *dst, return 0; } -static int on2avc_read_channel_data(On2AVCContext *c, GetBitContext *gb, int ch) +static int on2avc_read_channel_data(On2AVCContext *c, BitstreamContext *bc, int ch) { int ret; int w, b, band_idx; float *coeff_ptr; - if ((ret = on2avc_decode_band_types(c, gb)) < 0) + if ((ret = on2avc_decode_band_types(c, bc)) < 0) return ret; - if ((ret = on2avc_decode_band_scales(c, gb)) < 0) + if ((ret = on2avc_decode_band_scales(c, bc)) < 0) return ret; coeff_ptr = c->coeffs[ch]; @@ -273,10 +275,10 @@ static int on2avc_read_channel_data(On2AVCContext *c, GetBitContext *gb, int ch) continue; } if (band_type < 9) - on2avc_decode_quads(c, gb, coeff_ptr, band_size, band_type, + on2avc_decode_quads(c, bc, coeff_ptr, band_size, band_type, c->band_scales[band_idx + b]); else - on2avc_decode_pairs(c, gb, coeff_ptr, band_size, band_type, + on2avc_decode_pairs(c, bc, coeff_ptr, band_size, band_type, c->band_scales[band_idx + b]); coeff_ptr += band_size; } @@ -797,16 +799,16 @@ static int on2avc_reconstruct_channel(On2AVCContext *c, int channel, static int on2avc_decode_subframe(On2AVCContext *c, const uint8_t *buf, int buf_size, AVFrame *dst, int offset) { - GetBitContext gb; + BitstreamContext bc; int i, ret; - init_get_bits(&gb, buf, buf_size * 8); - if (get_bits1(&gb)) { + bitstream_init8(&bc, buf, buf_size); + if (bitstream_read_bit(&bc)) { av_log(c->avctx, AV_LOG_ERROR, "enh bit set\n"); return AVERROR_INVALIDDATA; } c->prev_window_type = c->window_type; - c->window_type = get_bits(&gb, 3); + c->window_type = bitstream_read(&bc, 3); if (c->window_type >= WINDOW_TYPE_EXT4 && c->avctx->channels == 1) { av_log(c->avctx, AV_LOG_ERROR, "stereo mode window for mono audio\n"); return AVERROR_INVALIDDATA; @@ -819,11 +821,11 @@ static int on2avc_decode_subframe(On2AVCContext *c, const uint8_t *buf, c->grouping[0] = 1; for (i = 1; i < c->num_windows; i++) - c->grouping[i] = !get_bits1(&gb); + c->grouping[i] = !bitstream_read_bit(&bc); - on2avc_read_ms_info(c, &gb); + on2avc_read_ms_info(c, &bc); for (i = 0; i < c->avctx->channels; i++) - if ((ret = on2avc_read_channel_data(c, &gb, i)) < 0) + if ((ret = on2avc_read_channel_data(c, &bc, i)) < 0) return AVERROR_INVALIDDATA; if (c->avctx->channels == 2 && c->ms_present) on2avc_apply_ms(c); From ed006ae4e2534084552a791a6fe9ebce1fa27a23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandra=20H=C3=A1jkov=C3=A1?= Date: Thu, 7 Apr 2016 12:32:52 +0200 Subject: [PATCH 06/35] 4xm: Convert to the new bitstream reader Signed-off-by: Anton Khirnov --- libavcodec/4xm.c | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/libavcodec/4xm.c b/libavcodec/4xm.c index b2d4db26b0..ee9d0205d5 100644 --- a/libavcodec/4xm.c +++ b/libavcodec/4xm.c @@ -29,11 +29,12 @@ #include "libavutil/frame.h" #include "libavutil/imgutils.h" #include "libavutil/intreadwrite.h" + #include "avcodec.h" +#include "bitstream.h" #include "blockdsp.h" #include "bswapdsp.h" #include "bytestream.h" -#include "get_bits.h" #include "internal.h" #define BLOCK_TYPE_VLC_BITS 5 @@ -136,8 +137,8 @@ typedef struct FourXContext { BswapDSPContext bbdsp; uint16_t *frame_buffer; uint16_t *last_frame_buffer; - GetBitContext pre_gb; ///< ac/dc prefix - GetBitContext gb; + BitstreamContext pre_bc; // ac/dc prefix + BitstreamContext bc; GetByteContext g; GetByteContext g2; int mv[256]; @@ -352,8 +353,8 @@ static int decode_p_block(FourXContext *f, uint16_t *dst, uint16_t *src, return AVERROR_INVALIDDATA; h = 1 << log2h; - code = get_vlc2(&f->gb, block_type_vlc[1 - (f->version > 1)][index].table, - BLOCK_TYPE_VLC_BITS, 1); + code = bitstream_read_vlc(&f->bc, block_type_vlc[1 - (f->version > 1)][index].table, + BLOCK_TYPE_VLC_BITS, 1); if (code < 0 || code > 6) return AVERROR_INVALIDDATA; @@ -453,7 +454,7 @@ static int decode_p_frame(FourXContext *f, const uint8_t *buf, int length) bitstream_size / 4); memset((uint8_t*)f->bitstream_buffer + bitstream_size, 0, AV_INPUT_BUFFER_PADDING_SIZE); - init_get_bits(&f->gb, f->bitstream_buffer, 8 * bitstream_size); + bitstream_init(&f->bc, f->bitstream_buffer, 8 * bitstream_size); wordstream_offset = extra + bitstream_size; bytestream_offset = extra + bitstream_size + wordstream_size; @@ -484,19 +485,19 @@ static int decode_i_block(FourXContext *f, int16_t *block) int code, i, j, level, val; /* DC coef */ - val = get_vlc2(&f->pre_gb, f->pre_vlc.table, ACDC_VLC_BITS, 3); + val = bitstream_read_vlc(&f->pre_bc, f->pre_vlc.table, ACDC_VLC_BITS, 3); if (val >> 4) av_log(f->avctx, AV_LOG_ERROR, "error dc run != 0\n"); if (val) - val = get_xbits(&f->gb, val); + val = bitstream_read_xbits(&f->bc, val); val = val * dequant_table[0] + f->last_dc; f->last_dc = block[0] = val; /* AC coefs */ i = 1; for (;;) { - code = get_vlc2(&f->pre_gb, f->pre_vlc.table, ACDC_VLC_BITS, 3); + code = bitstream_read_vlc(&f->pre_bc, f->pre_vlc.table, ACDC_VLC_BITS, 3); /* EOB */ if (code == 0) @@ -504,7 +505,7 @@ static int decode_i_block(FourXContext *f, int16_t *block) if (code == 0xf0) { i += 16; } else { - level = get_xbits(&f->gb, code & 0xf); + level = bitstream_read_xbits(&f->bc, code & 0xf); i += code >> 4; if (i >= 64) { av_log(f->avctx, AV_LOG_ERROR, "run %d oveflow\n", i); @@ -764,7 +765,7 @@ static int decode_i_frame(FourXContext *f, const uint8_t *buf, int length) return AVERROR_INVALIDDATA; } - init_get_bits(&f->gb, buf + 4, 8 * bitstream_size); + bitstream_init(&f->bc, buf + 4, 8 * bitstream_size); prestream_size = length + buf - prestream; @@ -776,7 +777,7 @@ static int decode_i_frame(FourXContext *f, const uint8_t *buf, int length) prestream_size / 4); memset((uint8_t*)f->bitstream_buffer + prestream_size, 0, AV_INPUT_BUFFER_PADDING_SIZE); - init_get_bits(&f->pre_gb, f->bitstream_buffer, 8 * prestream_size); + bitstream_init(&f->pre_bc, f->bitstream_buffer, 8 * prestream_size); f->last_dc = 0 * 128 * 8 * 8; @@ -789,7 +790,7 @@ static int decode_i_frame(FourXContext *f, const uint8_t *buf, int length) } } - if (get_vlc2(&f->pre_gb, f->pre_vlc.table, ACDC_VLC_BITS, 3) != 256) + if (bitstream_read_vlc(&f->pre_bc, f->pre_vlc.table, ACDC_VLC_BITS, 3) != 256) av_log(f->avctx, AV_LOG_ERROR, "end mismatch\n"); return 0; From 012c4511539f9f8e252c49d08c7e528973a22193 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandra=20H=C3=A1jkov=C3=A1?= Date: Thu, 7 Apr 2016 21:38:52 +0200 Subject: [PATCH 07/35] adpcm: Convert to the new bitstream header Signed-off-by: Anton Khirnov --- libavcodec/adpcm.c | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/libavcodec/adpcm.c b/libavcodec/adpcm.c index 3ab16dd0b4..fe51c0d008 100644 --- a/libavcodec/adpcm.c +++ b/libavcodec/adpcm.c @@ -29,8 +29,9 @@ * License along with Libav; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ + #include "avcodec.h" -#include "get_bits.h" +#include "bitstream.h" #include "put_bits.h" #include "bytestream.h" #include "adpcm.h" @@ -366,32 +367,33 @@ static int xa_decode(AVCodecContext *avctx, int16_t *out0, int16_t *out1, static void adpcm_swf_decode(AVCodecContext *avctx, const uint8_t *buf, int buf_size, int16_t *samples) { ADPCMDecodeContext *c = avctx->priv_data; - GetBitContext gb; + BitstreamContext bc; const int *table; int k0, signmask, nb_bits, count; int size = buf_size*8; int i; - init_get_bits(&gb, buf, size); + bitstream_init(&bc, buf, size); //read bits & initial values - nb_bits = get_bits(&gb, 2)+2; + nb_bits = bitstream_read(&bc, 2)+2; table = swf_index_tables[nb_bits-2]; k0 = 1 << (nb_bits-2); signmask = 1 << (nb_bits-1); - while (get_bits_count(&gb) <= size - 22*avctx->channels) { + while (bitstream_tell(&bc) <= size - 22 * avctx->channels) { for (i = 0; i < avctx->channels; i++) { - *samples++ = c->status[i].predictor = get_sbits(&gb, 16); - c->status[i].step_index = get_bits(&gb, 6); + *samples++ = + c->status[i].predictor = bitstream_read_signed(&bc, 16); + c->status[i].step_index = bitstream_read(&bc, 6); } - for (count = 0; get_bits_count(&gb) <= size - nb_bits*avctx->channels && count < 4095; count++) { + for (count = 0; bitstream_tell(&bc) <= size - nb_bits * avctx->channels && count < 4095; count++) { int i; for (i = 0; i < avctx->channels; i++) { // similar to IMA adpcm - int delta = get_bits(&gb, nb_bits); + int delta = bitstream_read(&bc, nb_bits); int step = ff_adpcm_step_table[c->status[i].step_index]; long vpdiff = 0; // vpdiff = (delta+0.5)*step/4 int k = k0; From 41679be1a2d82847a834a839356dfddc162bbb3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandra=20H=C3=A1jkov=C3=A1?= Date: Fri, 8 Apr 2016 20:13:19 +0200 Subject: [PATCH 08/35] asvdec: Convert to the new bitstream reader Signed-off-by: Anton Khirnov --- libavcodec/asv.h | 4 ++-- libavcodec/asvdec.c | 54 ++++++++++++++++++++++----------------------- 2 files changed, 29 insertions(+), 29 deletions(-) diff --git a/libavcodec/asv.h b/libavcodec/asv.h index 18f7a9571d..7c4e4fd92a 100644 --- a/libavcodec/asv.h +++ b/libavcodec/asv.h @@ -31,11 +31,11 @@ #include "libavutil/mem.h" #include "avcodec.h" +#include "bitstream.h" #include "blockdsp.h" #include "bswapdsp.h" #include "fdctdsp.h" #include "idctdsp.h" -#include "get_bits.h" #include "pixblockdsp.h" #include "put_bits.h" @@ -47,7 +47,7 @@ typedef struct ASV1Context { IDCTDSPContext idsp; PixblockDSPContext pdsp; PutBitContext pb; - GetBitContext gb; + BitstreamContext bc; ScanTable scantable; int inv_qscale; int mb_width; diff --git a/libavcodec/asvdec.c b/libavcodec/asvdec.c index f17f06465c..cbda63d3b0 100644 --- a/libavcodec/asvdec.c +++ b/libavcodec/asvdec.c @@ -70,27 +70,27 @@ static av_cold void init_vlcs(ASV1Context *a) } // FIXME write a reversed bitstream reader to avoid the double reverse -static inline int asv2_get_bits(GetBitContext *gb, int n) +static inline int asv2_get_bits(BitstreamContext *bc, int n) { - return ff_reverse[get_bits(gb, n) << (8 - n)]; + return ff_reverse[bitstream_read(bc, n) << (8 - n)]; } -static inline int asv1_get_level(GetBitContext *gb) +static inline int asv1_get_level(BitstreamContext *bc) { - int code = get_vlc2(gb, level_vlc.table, VLC_BITS, 1); + int code = bitstream_read_vlc(bc, level_vlc.table, VLC_BITS, 1); if (code == 3) - return get_sbits(gb, 8); + return bitstream_read_signed(bc, 8); else return code - 3; } -static inline int asv2_get_level(GetBitContext *gb) +static inline int asv2_get_level(BitstreamContext *bc) { - int code = get_vlc2(gb, asv2_level_vlc.table, ASV2_LEVEL_VLC_BITS, 1); + int code = bitstream_read_vlc(bc, asv2_level_vlc.table, ASV2_LEVEL_VLC_BITS, 1); if (code == 31) - return (int8_t) asv2_get_bits(gb, 8); + return (int8_t) asv2_get_bits(bc, 8); else return code - 31; } @@ -99,10 +99,10 @@ static inline int asv1_decode_block(ASV1Context *a, int16_t block[64]) { int i; - block[0] = 8 * get_bits(&a->gb, 8); + block[0] = 8 * bitstream_read(&a->bc, 8); for (i = 0; i < 11; i++) { - const int ccp = get_vlc2(&a->gb, ccp_vlc.table, VLC_BITS, 1); + const int ccp = bitstream_read_vlc(&a->bc, ccp_vlc.table, VLC_BITS, 1); if (ccp) { if (ccp == 16) @@ -113,13 +113,13 @@ static inline int asv1_decode_block(ASV1Context *a, int16_t block[64]) } if (ccp & 8) - block[a->scantable.permutated[4 * i + 0]] = (asv1_get_level(&a->gb) * a->intra_matrix[4 * i + 0]) >> 4; + block[a->scantable.permutated[4 * i + 0]] = (asv1_get_level(&a->bc) * a->intra_matrix[4 * i + 0]) >> 4; if (ccp & 4) - block[a->scantable.permutated[4 * i + 1]] = (asv1_get_level(&a->gb) * a->intra_matrix[4 * i + 1]) >> 4; + block[a->scantable.permutated[4 * i + 1]] = (asv1_get_level(&a->bc) * a->intra_matrix[4 * i + 1]) >> 4; if (ccp & 2) - block[a->scantable.permutated[4 * i + 2]] = (asv1_get_level(&a->gb) * a->intra_matrix[4 * i + 2]) >> 4; + block[a->scantable.permutated[4 * i + 2]] = (asv1_get_level(&a->bc) * a->intra_matrix[4 * i + 2]) >> 4; if (ccp & 1) - block[a->scantable.permutated[4 * i + 3]] = (asv1_get_level(&a->gb) * a->intra_matrix[4 * i + 3]) >> 4; + block[a->scantable.permutated[4 * i + 3]] = (asv1_get_level(&a->bc) * a->intra_matrix[4 * i + 3]) >> 4; } } @@ -130,32 +130,32 @@ static inline int asv2_decode_block(ASV1Context *a, int16_t block[64]) { int i, count, ccp; - count = asv2_get_bits(&a->gb, 4); + count = asv2_get_bits(&a->bc, 4); - block[0] = 8 * asv2_get_bits(&a->gb, 8); + block[0] = 8 * asv2_get_bits(&a->bc, 8); - ccp = get_vlc2(&a->gb, dc_ccp_vlc.table, VLC_BITS, 1); + ccp = bitstream_read_vlc(&a->bc, dc_ccp_vlc.table, VLC_BITS, 1); if (ccp) { if (ccp & 4) - block[a->scantable.permutated[1]] = (asv2_get_level(&a->gb) * a->intra_matrix[1]) >> 4; + block[a->scantable.permutated[1]] = (asv2_get_level(&a->bc) * a->intra_matrix[1]) >> 4; if (ccp & 2) - block[a->scantable.permutated[2]] = (asv2_get_level(&a->gb) * a->intra_matrix[2]) >> 4; + block[a->scantable.permutated[2]] = (asv2_get_level(&a->bc) * a->intra_matrix[2]) >> 4; if (ccp & 1) - block[a->scantable.permutated[3]] = (asv2_get_level(&a->gb) * a->intra_matrix[3]) >> 4; + block[a->scantable.permutated[3]] = (asv2_get_level(&a->bc) * a->intra_matrix[3]) >> 4; } for (i = 1; i < count + 1; i++) { - const int ccp = get_vlc2(&a->gb, ac_ccp_vlc.table, VLC_BITS, 1); + const int ccp = bitstream_read_vlc(&a->bc, ac_ccp_vlc.table, VLC_BITS, 1); if (ccp) { if (ccp & 8) - block[a->scantable.permutated[4 * i + 0]] = (asv2_get_level(&a->gb) * a->intra_matrix[4 * i + 0]) >> 4; + block[a->scantable.permutated[4 * i + 0]] = (asv2_get_level(&a->bc) * a->intra_matrix[4 * i + 0]) >> 4; if (ccp & 4) - block[a->scantable.permutated[4 * i + 1]] = (asv2_get_level(&a->gb) * a->intra_matrix[4 * i + 1]) >> 4; + block[a->scantable.permutated[4 * i + 1]] = (asv2_get_level(&a->bc) * a->intra_matrix[4 * i + 1]) >> 4; if (ccp & 2) - block[a->scantable.permutated[4 * i + 2]] = (asv2_get_level(&a->gb) * a->intra_matrix[4 * i + 2]) >> 4; + block[a->scantable.permutated[4 * i + 2]] = (asv2_get_level(&a->bc) * a->intra_matrix[4 * i + 2]) >> 4; if (ccp & 1) - block[a->scantable.permutated[4 * i + 3]] = (asv2_get_level(&a->gb) * a->intra_matrix[4 * i + 3]) >> 4; + block[a->scantable.permutated[4 * i + 3]] = (asv2_get_level(&a->bc) * a->intra_matrix[4 * i + 3]) >> 4; } } @@ -232,7 +232,7 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, a->bitstream_buffer[i] = ff_reverse[buf[i]]; } - init_get_bits(&a->gb, a->bitstream_buffer, buf_size * 8); + bitstream_init(&a->bc, a->bitstream_buffer, buf_size * 8); for (mb_y = 0; mb_y < a->mb_height2; mb_y++) { for (mb_x = 0; mb_x < a->mb_width2; mb_x++) { @@ -267,7 +267,7 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, emms_c(); - return (get_bits_count(&a->gb) + 31) / 32 * 4; + return (bitstream_tell(&a->bc) + 31) / 32 * 4; } static av_cold int decode_init(AVCodecContext *avctx) From 027211920222b2b63ca07d712b3771a14add9aab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandra=20H=C3=A1jkov=C3=A1?= Date: Sun, 17 Apr 2016 17:06:09 +0200 Subject: [PATCH 09/35] atrac: Convert to the new bitstream reader Signed-off-by: Anton Khirnov --- libavcodec/atrac1.c | 34 ++++++++-------- libavcodec/atrac3.c | 97 +++++++++++++++++++++++---------------------- 2 files changed, 67 insertions(+), 64 deletions(-) diff --git a/libavcodec/atrac1.c b/libavcodec/atrac1.c index e938976d88..60be853588 100644 --- a/libavcodec/atrac1.c +++ b/libavcodec/atrac1.c @@ -33,8 +33,9 @@ #include #include "libavutil/float_dsp.h" + #include "avcodec.h" -#include "get_bits.h" +#include "bitstream.h" #include "fft.h" #include "internal.h" #include "sinewin.h" @@ -164,30 +165,31 @@ static int at1_imdct_block(AT1SUCtx* su, AT1Ctx *q) * Parse the block size mode byte */ -static int at1_parse_bsm(GetBitContext* gb, int log2_block_cnt[AT1_QMF_BANDS]) +static int at1_parse_bsm(BitstreamContext *bc, + int log2_block_cnt[AT1_QMF_BANDS]) { int log2_block_count_tmp, i; for (i = 0; i < 2; i++) { /* low and mid band */ - log2_block_count_tmp = get_bits(gb, 2); + log2_block_count_tmp = bitstream_read(bc, 2); if (log2_block_count_tmp & 1) return AVERROR_INVALIDDATA; log2_block_cnt[i] = 2 - log2_block_count_tmp; } /* high band */ - log2_block_count_tmp = get_bits(gb, 2); + log2_block_count_tmp = bitstream_read(bc, 2); if (log2_block_count_tmp != 0 && log2_block_count_tmp != 3) return AVERROR_INVALIDDATA; log2_block_cnt[IDX_HIGH_BAND] = 3 - log2_block_count_tmp; - skip_bits(gb, 2); + bitstream_skip(bc, 2); return 0; } -static int at1_unpack_dequant(GetBitContext* gb, AT1SUCtx* su, +static int at1_unpack_dequant(BitstreamContext *bc, AT1SUCtx *su, float spec[AT1_SU_SAMPLES]) { int bits_used, band_num, bfu_num, i; @@ -195,22 +197,22 @@ static int at1_unpack_dequant(GetBitContext* gb, AT1SUCtx* su, uint8_t idsfs[AT1_MAX_BFU]; ///< the scalefactor indexes for each BFU /* parse the info byte (2nd byte) telling how much BFUs were coded */ - su->num_bfus = bfu_amount_tab1[get_bits(gb, 3)]; + su->num_bfus = bfu_amount_tab1[bitstream_read(bc, 3)]; /* calc number of consumed bits: num_BFUs * (idwl(4bits) + idsf(6bits)) + log2_block_count(8bits) + info_byte(8bits) + info_byte_copy(8bits) + log2_block_count_copy(8bits) */ bits_used = su->num_bfus * 10 + 32 + - bfu_amount_tab2[get_bits(gb, 2)] + - (bfu_amount_tab3[get_bits(gb, 3)] << 1); + bfu_amount_tab2[bitstream_read(bc, 2)] + + (bfu_amount_tab3[bitstream_read(bc, 3)] << 1); /* get word length index (idwl) for each BFU */ for (i = 0; i < su->num_bfus; i++) - idwls[i] = get_bits(gb, 4); + idwls[i] = bitstream_read(bc, 4); /* get scalefactor index (idsf) for each BFU */ for (i = 0; i < su->num_bfus; i++) - idsfs[i] = get_bits(gb, 6); + idsfs[i] = bitstream_read(bc, 6); /* zero idwl/idsf for empty BFUs */ for (i = su->num_bfus; i < AT1_MAX_BFU; i++) @@ -240,7 +242,7 @@ static int at1_unpack_dequant(GetBitContext* gb, AT1SUCtx* su, /* read in a quantized spec and convert it to * signed int and then inverse quantization */ - spec[pos+i] = get_sbits(gb, word_len) * scale_factor * max_quant; + spec[pos+i] = bitstream_read_signed(bc, word_len) * scale_factor * max_quant; } } else { /* word_len = 0 -> empty BFU, zero all specs in the emty BFU */ memset(&spec[pos], 0, num_specs * sizeof(float)); @@ -277,7 +279,7 @@ static int atrac1_decode_frame(AVCodecContext *avctx, void *data, int buf_size = avpkt->size; AT1Ctx *q = avctx->priv_data; int ch, ret; - GetBitContext gb; + BitstreamContext bc; if (buf_size < 212 * avctx->channels) { @@ -295,14 +297,14 @@ static int atrac1_decode_frame(AVCodecContext *avctx, void *data, for (ch = 0; ch < avctx->channels; ch++) { AT1SUCtx* su = &q->SUs[ch]; - init_get_bits(&gb, &buf[212 * ch], 212 * 8); + bitstream_init(&bc, &buf[212 * ch], 212 * 8); /* parse block_size_mode, 1st byte */ - ret = at1_parse_bsm(&gb, su->log2_block_count); + ret = at1_parse_bsm(&bc, su->log2_block_count); if (ret < 0) return ret; - ret = at1_unpack_dequant(&gb, su, q->spec); + ret = at1_unpack_dequant(&bc, su, q->spec); if (ret < 0) return ret; diff --git a/libavcodec/atrac3.c b/libavcodec/atrac3.c index 2e1fd3c133..be32a0ec86 100644 --- a/libavcodec/atrac3.c +++ b/libavcodec/atrac3.c @@ -38,10 +38,11 @@ #include "libavutil/attributes.h" #include "libavutil/float_dsp.h" + #include "avcodec.h" +#include "bitstream.h" #include "bytestream.h" #include "fft.h" -#include "get_bits.h" #include "internal.h" #include "atrac.h" @@ -80,7 +81,7 @@ typedef struct ChannelUnit { } ChannelUnit; typedef struct ATRAC3Context { - GetBitContext gb; + BitstreamContext bc; //@{ /** stream data */ int coding_mode; @@ -203,7 +204,7 @@ static av_cold int atrac3_decode_close(AVCodecContext *avctx) * @param mantissas mantissa output table * @param num_codes number of values to get */ -static void read_quant_spectral_coeffs(GetBitContext *gb, int selector, +static void read_quant_spectral_coeffs(BitstreamContext *bc, int selector, int coding_flag, int *mantissas, int num_codes) { @@ -219,7 +220,7 @@ static void read_quant_spectral_coeffs(GetBitContext *gb, int selector, if (selector > 1) { for (i = 0; i < num_codes; i++) { if (num_bits) - code = get_sbits(gb, num_bits); + code = bitstream_read_signed(bc, num_bits); else code = 0; mantissas[i] = code; @@ -227,7 +228,7 @@ static void read_quant_spectral_coeffs(GetBitContext *gb, int selector, } else { for (i = 0; i < num_codes; i++) { if (num_bits) - code = get_bits(gb, num_bits); // num_bits is always 4 in this case + code = bitstream_read(bc, num_bits); // num_bits is always 4 in this case else code = 0; mantissas[i * 2 ] = mantissa_clc_tab[code >> 2]; @@ -238,8 +239,8 @@ static void read_quant_spectral_coeffs(GetBitContext *gb, int selector, /* variable length coding (VLC) */ if (selector != 1) { for (i = 0; i < num_codes; i++) { - huff_symb = get_vlc2(gb, spectral_coeff_tab[selector-1].table, - spectral_coeff_tab[selector-1].bits, 3); + huff_symb = bitstream_read_vlc(bc, spectral_coeff_tab[selector-1].table, + spectral_coeff_tab[selector-1].bits, 3); huff_symb += 1; code = huff_symb >> 1; if (huff_symb & 1) @@ -248,8 +249,8 @@ static void read_quant_spectral_coeffs(GetBitContext *gb, int selector, } } else { for (i = 0; i < num_codes; i++) { - huff_symb = get_vlc2(gb, spectral_coeff_tab[selector - 1].table, - spectral_coeff_tab[selector - 1].bits, 3); + huff_symb = bitstream_read_vlc(bc, spectral_coeff_tab[selector - 1].table, + spectral_coeff_tab[selector - 1].bits, 3); mantissas[i * 2 ] = mantissa_vlc_tab[huff_symb * 2 ]; mantissas[i * 2 + 1] = mantissa_vlc_tab[huff_symb * 2 + 1]; } @@ -262,24 +263,24 @@ static void read_quant_spectral_coeffs(GetBitContext *gb, int selector, * * @return subband count, fix for broken specification/files */ -static int decode_spectrum(GetBitContext *gb, float *output) +static int decode_spectrum(BitstreamContext *bc, float *output) { int num_subbands, coding_mode, i, j, first, last, subband_size; int subband_vlc_index[32], sf_index[32]; int mantissas[128]; float scale_factor; - num_subbands = get_bits(gb, 5); // number of coded subbands - coding_mode = get_bits1(gb); // coding Mode: 0 - VLC/ 1-CLC + num_subbands = bitstream_read(bc, 5); // number of coded subbands + coding_mode = bitstream_read_bit(bc); // coding Mode: 0 - VLC/ 1 - CLC /* get the VLC selector table for the subbands, 0 means not coded */ for (i = 0; i <= num_subbands; i++) - subband_vlc_index[i] = get_bits(gb, 3); + subband_vlc_index[i] = bitstream_read(bc, 3); /* read the scale factor indexes from the stream */ for (i = 0; i <= num_subbands; i++) { if (subband_vlc_index[i] != 0) - sf_index[i] = get_bits(gb, 6); + sf_index[i] = bitstream_read(bc, 6); } for (i = 0; i <= num_subbands; i++) { @@ -292,7 +293,7 @@ static int decode_spectrum(GetBitContext *gb, float *output) /* decode spectral coefficients for this subband */ /* TODO: This can be done faster is several blocks share the * same VLC selector (subband_vlc_index) */ - read_quant_spectral_coeffs(gb, subband_vlc_index[i], coding_mode, + read_quant_spectral_coeffs(bc, subband_vlc_index[i], coding_mode, mantissas, subband_size); /* decode the scale factor for this subband */ @@ -320,7 +321,7 @@ static int decode_spectrum(GetBitContext *gb, float *output) * @param components tonal components * @param num_bands number of coded bands */ -static int decode_tonal_components(GetBitContext *gb, +static int decode_tonal_components(BitstreamContext *bc, TonalComponent *components, int num_bands) { int i, b, c, m; @@ -328,13 +329,13 @@ static int decode_tonal_components(GetBitContext *gb, int band_flags[4], mantissa[8]; int component_count = 0; - nb_components = get_bits(gb, 5); + nb_components = bitstream_read(bc, 5); /* no tonal components */ if (nb_components == 0) return 0; - coding_mode_selector = get_bits(gb, 2); + coding_mode_selector = bitstream_read(bc, 2); if (coding_mode_selector == 2) return AVERROR_INVALIDDATA; @@ -344,16 +345,16 @@ static int decode_tonal_components(GetBitContext *gb, int coded_values_per_component, quant_step_index; for (b = 0; b <= num_bands; b++) - band_flags[b] = get_bits1(gb); + band_flags[b] = bitstream_read_bit(bc); - coded_values_per_component = get_bits(gb, 3); + coded_values_per_component = bitstream_read(bc, 3); - quant_step_index = get_bits(gb, 3); + quant_step_index = bitstream_read(bc, 3); if (quant_step_index <= 1) return AVERROR_INVALIDDATA; if (coding_mode_selector == 3) - coding_mode = get_bits1(gb); + coding_mode = bitstream_read_bit(bc); for (b = 0; b < (num_bands + 1) * 4; b++) { int coded_components; @@ -361,18 +362,18 @@ static int decode_tonal_components(GetBitContext *gb, if (band_flags[b >> 2] == 0) continue; - coded_components = get_bits(gb, 3); + coded_components = bitstream_read(bc, 3); for (c = 0; c < coded_components; c++) { TonalComponent *cmp = &components[component_count]; int sf_index, coded_values, max_coded_values; float scale_factor; - sf_index = get_bits(gb, 6); + sf_index = bitstream_read(bc, 6); if (component_count >= 64) return AVERROR_INVALIDDATA; - cmp->pos = b * 64 + get_bits(gb, 6); + cmp->pos = b * 64 + bitstream_read(bc, 6); max_coded_values = SAMPLES_PER_FRAME - cmp->pos; coded_values = coded_values_per_component + 1; @@ -381,7 +382,7 @@ static int decode_tonal_components(GetBitContext *gb, scale_factor = ff_atrac_sf_table[sf_index] * inv_max_quant[quant_step_index]; - read_quant_spectral_coeffs(gb, quant_step_index, coding_mode, + read_quant_spectral_coeffs(bc, quant_step_index, coding_mode, mantissa, coded_values); cmp->num_coefs = coded_values; @@ -404,7 +405,7 @@ static int decode_tonal_components(GetBitContext *gb, * @param block the gainblock for the current band * @param num_bands amount of coded bands */ -static int decode_gain_control(GetBitContext *gb, GainBlock *block, +static int decode_gain_control(BitstreamContext *bc, GainBlock *block, int num_bands) { int i, j; @@ -413,13 +414,13 @@ static int decode_gain_control(GetBitContext *gb, GainBlock *block, AtracGainInfo *gain = block->g_block; for (i = 0; i <= num_bands; i++) { - gain[i].num_points = get_bits(gb, 3); + gain[i].num_points = bitstream_read(bc, 3); level = gain[i].lev_code; loc = gain[i].loc_code; for (j = 0; j < gain[i].num_points; j++) { - level[j] = get_bits(gb, 4); - loc[j] = get_bits(gb, 5); + level[j] = bitstream_read(bc, 4); + loc[j] = bitstream_read(bc, 5); if (j && loc[j] <= loc[j - 1]) return AVERROR_INVALIDDATA; } @@ -567,7 +568,7 @@ static void channel_weighting(float *su1, float *su2, int *p3) * @param channel_num channel number * @param coding_mode the coding mode (JOINT_STEREO or regular stereo/mono) */ -static int decode_channel_sound_unit(ATRAC3Context *q, GetBitContext *gb, +static int decode_channel_sound_unit(ATRAC3Context *q, BitstreamContext *bc, ChannelUnit *snd, float *output, int channel_num, int coding_mode) { @@ -576,30 +577,30 @@ static int decode_channel_sound_unit(ATRAC3Context *q, GetBitContext *gb, GainBlock *gain2 = &snd->gain_block[1 - snd->gc_blk_switch]; if (coding_mode == JOINT_STEREO && channel_num == 1) { - if (get_bits(gb, 2) != 3) { + if (bitstream_read(bc, 2) != 3) { av_log(NULL,AV_LOG_ERROR,"JS mono Sound Unit id != 3.\n"); return AVERROR_INVALIDDATA; } } else { - if (get_bits(gb, 6) != 0x28) { + if (bitstream_read(bc, 6) != 0x28) { av_log(NULL,AV_LOG_ERROR,"Sound Unit id != 0x28.\n"); return AVERROR_INVALIDDATA; } } /* number of coded QMF bands */ - snd->bands_coded = get_bits(gb, 2); + snd->bands_coded = bitstream_read(bc, 2); - ret = decode_gain_control(gb, gain2, snd->bands_coded); + ret = decode_gain_control(bc, gain2, snd->bands_coded); if (ret) return ret; - snd->num_components = decode_tonal_components(gb, snd->components, + snd->num_components = decode_tonal_components(bc, snd->components, snd->bands_coded); if (snd->num_components < 0) return snd->num_components; - num_subbands = decode_spectrum(gb, snd->spectrum); + num_subbands = decode_spectrum(bc, snd->spectrum); /* Merge the decoded spectrum and tonal components. */ last_tonal = add_tonal_components(snd->spectrum, snd->num_components, @@ -644,9 +645,9 @@ static int decode_frame(AVCodecContext *avctx, const uint8_t *databuf, if (q->coding_mode == JOINT_STEREO) { /* channel coupling mode */ /* decode Sound Unit 1 */ - init_get_bits(&q->gb, databuf, avctx->block_align * 8); + bitstream_init(&q->bc, databuf, avctx->block_align * 8); - ret = decode_channel_sound_unit(q, &q->gb, q->units, out_samples[0], 0, + ret = decode_channel_sound_unit(q, &q->bc, q->units, out_samples[0], 0, JOINT_STEREO); if (ret != 0) return ret; @@ -673,22 +674,22 @@ static int decode_frame(AVCodecContext *avctx, const uint8_t *databuf, /* set the bitstream reader at the start of the second Sound Unit*/ - init_get_bits(&q->gb, ptr1, (avctx->block_align - i) * 8); + bitstream_init(&q->bc, ptr1, (avctx->block_align - i) * 8); /* Fill the Weighting coeffs delay buffer */ memmove(q->weighting_delay, &q->weighting_delay[2], 4 * sizeof(*q->weighting_delay)); - q->weighting_delay[4] = get_bits1(&q->gb); - q->weighting_delay[5] = get_bits(&q->gb, 3); + q->weighting_delay[4] = bitstream_read_bit(&q->bc); + q->weighting_delay[5] = bitstream_read(&q->bc, 3); for (i = 0; i < 4; i++) { q->matrix_coeff_index_prev[i] = q->matrix_coeff_index_now[i]; q->matrix_coeff_index_now[i] = q->matrix_coeff_index_next[i]; - q->matrix_coeff_index_next[i] = get_bits(&q->gb, 2); + q->matrix_coeff_index_next[i] = bitstream_read(&q->bc, 2); } /* Decode Sound Unit 2. */ - ret = decode_channel_sound_unit(q, &q->gb, &q->units[1], + ret = decode_channel_sound_unit(q, &q->bc, &q->units[1], out_samples[1], 1, JOINT_STEREO); if (ret != 0) return ret; @@ -704,11 +705,11 @@ static int decode_frame(AVCodecContext *avctx, const uint8_t *databuf, /* Decode the channel sound units. */ for (i = 0; i < avctx->channels; i++) { /* Set the bitstream reader at the start of a channel sound unit. */ - init_get_bits(&q->gb, - databuf + i * avctx->block_align / avctx->channels, - avctx->block_align * 8 / avctx->channels); + bitstream_init(&q->bc, + databuf + i * avctx->block_align / avctx->channels, + avctx->block_align * 8 / avctx->channels); - ret = decode_channel_sound_unit(q, &q->gb, &q->units[i], + ret = decode_channel_sound_unit(q, &q->bc, &q->units[i], out_samples[i], i, q->coding_mode); if (ret != 0) return ret; From edd4c19a781124cbdb3598f94ec3a0d9ff7058cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandra=20H=C3=A1jkov=C3=A1?= Date: Sat, 9 Apr 2016 14:47:04 +0200 Subject: [PATCH 10/35] atrac3plus: Convert to the new bitstream reader Signed-off-by: Anton Khirnov --- libavcodec/atrac3plus.c | 525 +++++++++++++++++++------------------ libavcodec/atrac3plus.h | 7 +- libavcodec/atrac3plusdec.c | 15 +- 3 files changed, 275 insertions(+), 272 deletions(-) diff --git a/libavcodec/atrac3plus.c b/libavcodec/atrac3plus.c index 076fb844a7..2731a80725 100644 --- a/libavcodec/atrac3plus.c +++ b/libavcodec/atrac3plus.c @@ -26,8 +26,9 @@ */ #include "libavutil/avassert.h" + #include "avcodec.h" -#include "get_bits.h" +#include "bitstream.h" #include "atrac3plus.h" #include "atrac3plus_data.h" @@ -212,20 +213,20 @@ av_cold void ff_atrac3p_init_vlcs(AVCodec *codec) /** * Decode number of coded quantization units. * - * @param[in] gb the GetBit context + * @param[in] bc the Bitstream context * @param[in,out] chan ptr to the channel parameters * @param[in,out] ctx ptr to the channel unit context * @param[in] avctx ptr to the AVCodecContext * @return result code: 0 = OK, otherwise - error code */ -static int num_coded_units(GetBitContext *gb, Atrac3pChanParams *chan, +static int num_coded_units(BitstreamContext *bc, Atrac3pChanParams *chan, Atrac3pChanUnitCtx *ctx, AVCodecContext *avctx) { - chan->fill_mode = get_bits(gb, 2); + chan->fill_mode = bitstream_read(bc, 2); if (!chan->fill_mode) { chan->num_coded_vals = ctx->num_quant_units; } else { - chan->num_coded_vals = get_bits(gb, 5); + chan->num_coded_vals = bitstream_read(bc, 5); if (chan->num_coded_vals > ctx->num_quant_units) { av_log(avctx, AV_LOG_ERROR, "Invalid number of transmitted units!\n"); @@ -233,7 +234,7 @@ static int num_coded_units(GetBitContext *gb, Atrac3pChanParams *chan, } if (chan->fill_mode == 3) - chan->split_point = get_bits(gb, 2) + (chan->ch_num << 1) + 1; + chan->split_point = bitstream_read(bc, 2) + (chan->ch_num << 1) + 1; } return 0; @@ -318,21 +319,21 @@ static inline void unpack_vq_shape(int start_val, const int8_t *shape_vec, } } -#define UNPACK_SF_VQ_SHAPE(gb, dst, num_vals) \ - start_val = get_bits((gb), 6); \ - unpack_vq_shape(start_val, &atrac3p_sf_shapes[get_bits((gb), 6)][0], \ +#define UNPACK_SF_VQ_SHAPE(bc, dst, num_vals) \ + start_val = bitstream_read((bc), 6); \ + unpack_vq_shape(start_val, &atrac3p_sf_shapes[bitstream_read((bc), 6)][0], \ (dst), (num_vals)) /** * Decode word length for each quantization unit of a channel. * - * @param[in] gb the GetBit context + * @param[in] bc the Bitstream context * @param[in,out] ctx ptr to the channel unit context * @param[in] ch_num channel to process * @param[in] avctx ptr to the AVCodecContext * @return result code: 0 = OK, otherwise - error code */ -static int decode_channel_wordlen(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, +static int decode_channel_wordlen(BitstreamContext *bc, Atrac3pChanUnitCtx *ctx, int ch_num, AVCodecContext *avctx) { int i, weight_idx = 0, delta, diff, pos, delta_bits, min_val, flag, @@ -343,107 +344,107 @@ static int decode_channel_wordlen(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, chan->fill_mode = 0; - switch (get_bits(gb, 2)) { /* switch according to coding mode */ + switch (bitstream_read(bc, 2)) { /* switch according to coding mode */ case 0: /* coded using constant number of bits */ for (i = 0; i < ctx->num_quant_units; i++) - chan->qu_wordlen[i] = get_bits(gb, 3); + chan->qu_wordlen[i] = bitstream_read(bc, 3); break; case 1: if (ch_num) { - if ((ret = num_coded_units(gb, chan, ctx, avctx)) < 0) + if ((ret = num_coded_units(bc, chan, ctx, avctx)) < 0) return ret; if (chan->num_coded_vals) { - vlc_tab = &wl_vlc_tabs[get_bits(gb, 2)]; + vlc_tab = &wl_vlc_tabs[bitstream_read(bc, 2)]; for (i = 0; i < chan->num_coded_vals; i++) { - delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1); + delta = bitstream_read_vlc(bc, vlc_tab->table, vlc_tab->bits, 1); chan->qu_wordlen[i] = (ref_chan->qu_wordlen[i] + delta) & 7; } } } else { - weight_idx = get_bits(gb, 2); - if ((ret = num_coded_units(gb, chan, ctx, avctx)) < 0) + weight_idx = bitstream_read(bc, 2); + if ((ret = num_coded_units(bc, chan, ctx, avctx)) < 0) return ret; if (chan->num_coded_vals) { - pos = get_bits(gb, 5); + pos = bitstream_read(bc, 5); if (pos > chan->num_coded_vals) { av_log(avctx, AV_LOG_ERROR, "WL mode 1: invalid position!\n"); return AVERROR_INVALIDDATA; } - delta_bits = get_bits(gb, 2); - min_val = get_bits(gb, 3); + delta_bits = bitstream_read(bc, 2); + min_val = bitstream_read(bc, 3); for (i = 0; i < pos; i++) - chan->qu_wordlen[i] = get_bits(gb, 3); + chan->qu_wordlen[i] = bitstream_read(bc, 3); for (i = pos; i < chan->num_coded_vals; i++) - chan->qu_wordlen[i] = (min_val + get_bitsz(gb, delta_bits)) & 7; + chan->qu_wordlen[i] = (min_val + bitstream_read(bc, delta_bits)) & 7; } } break; case 2: - if ((ret = num_coded_units(gb, chan, ctx, avctx)) < 0) + if ((ret = num_coded_units(bc, chan, ctx, avctx)) < 0) return ret; if (ch_num && chan->num_coded_vals) { - vlc_tab = &wl_vlc_tabs[get_bits(gb, 2)]; - delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1); + vlc_tab = &wl_vlc_tabs[bitstream_read(bc, 2)]; + delta = bitstream_read_vlc(bc, vlc_tab->table, vlc_tab->bits, 1); chan->qu_wordlen[0] = (ref_chan->qu_wordlen[0] + delta) & 7; for (i = 1; i < chan->num_coded_vals; i++) { diff = ref_chan->qu_wordlen[i] - ref_chan->qu_wordlen[i - 1]; - delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1); + delta = bitstream_read_vlc(bc, vlc_tab->table, vlc_tab->bits, 1); chan->qu_wordlen[i] = (chan->qu_wordlen[i - 1] + diff + delta) & 7; } } else if (chan->num_coded_vals) { - flag = get_bits(gb, 1); - vlc_tab = &wl_vlc_tabs[get_bits(gb, 1)]; + flag = bitstream_read(bc, 1); + vlc_tab = &wl_vlc_tabs[bitstream_read(bc, 1)]; - start_val = get_bits(gb, 3); + start_val = bitstream_read(bc, 3); unpack_vq_shape(start_val, - &atrac3p_wl_shapes[start_val][get_bits(gb, 4)][0], + &atrac3p_wl_shapes[start_val][bitstream_read(bc, 4)][0], chan->qu_wordlen, chan->num_coded_vals); if (!flag) { for (i = 0; i < chan->num_coded_vals; i++) { - delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1); + delta = bitstream_read_vlc(bc, vlc_tab->table, vlc_tab->bits, 1); chan->qu_wordlen[i] = (chan->qu_wordlen[i] + delta) & 7; } } else { for (i = 0; i < (chan->num_coded_vals & - 2); i += 2) - if (!get_bits1(gb)) { + if (!bitstream_read_bit(bc)) { chan->qu_wordlen[i] = (chan->qu_wordlen[i] + - get_vlc2(gb, vlc_tab->table, - vlc_tab->bits, 1)) & 7; + bitstream_read_vlc(bc, vlc_tab->table, + vlc_tab->bits, 1)) & 7; chan->qu_wordlen[i + 1] = (chan->qu_wordlen[i + 1] + - get_vlc2(gb, vlc_tab->table, - vlc_tab->bits, 1)) & 7; + bitstream_read_vlc(bc, vlc_tab->table, + vlc_tab->bits, 1)) & 7; } if (chan->num_coded_vals & 1) chan->qu_wordlen[i] = (chan->qu_wordlen[i] + - get_vlc2(gb, vlc_tab->table, - vlc_tab->bits, 1)) & 7; + bitstream_read_vlc(bc, vlc_tab->table, + vlc_tab->bits, 1)) & 7; } } break; case 3: - weight_idx = get_bits(gb, 2); - if ((ret = num_coded_units(gb, chan, ctx, avctx)) < 0) + weight_idx = bitstream_read(bc, 2); + if ((ret = num_coded_units(bc, chan, ctx, avctx)) < 0) return ret; if (chan->num_coded_vals) { - vlc_tab = &wl_vlc_tabs[get_bits(gb, 2)]; + vlc_tab = &wl_vlc_tabs[bitstream_read(bc, 2)]; /* first coefficient is coded directly */ - chan->qu_wordlen[0] = get_bits(gb, 3); + chan->qu_wordlen[0] = bitstream_read(bc, 3); for (i = 1; i < chan->num_coded_vals; i++) { - delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1); + delta = bitstream_read_vlc(bc, vlc_tab->table, vlc_tab->bits, 1); chan->qu_wordlen[i] = (chan->qu_wordlen[i - 1] + delta) & 7; } } @@ -452,7 +453,7 @@ static int decode_channel_wordlen(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, if (chan->fill_mode == 2) { for (i = chan->num_coded_vals; i < ctx->num_quant_units; i++) - chan->qu_wordlen[i] = ch_num ? get_bits1(gb) : 1; + chan->qu_wordlen[i] = ch_num ? bitstream_read_bit(bc) : 1; } else if (chan->fill_mode == 3) { pos = ch_num ? chan->num_coded_vals + chan->split_point : ctx->num_quant_units - chan->split_point; @@ -469,13 +470,13 @@ static int decode_channel_wordlen(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, /** * Decode scale factor indexes for each quant unit of a channel. * - * @param[in] gb the GetBit context + * @param[in] bc the Bitstream context * @param[in,out] ctx ptr to the channel unit context * @param[in] ch_num channel to process * @param[in] avctx ptr to the AVCodecContext * @return result code: 0 = OK, otherwise - error code */ -static int decode_channel_sf_idx(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, +static int decode_channel_sf_idx(BitstreamContext *bc, Atrac3pChanUnitCtx *ctx, int ch_num, AVCodecContext *avctx) { int i, weight_idx = 0, delta, diff, num_long_vals, @@ -484,40 +485,40 @@ static int decode_channel_sf_idx(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, Atrac3pChanParams *chan = &ctx->channels[ch_num]; Atrac3pChanParams *ref_chan = &ctx->channels[0]; - switch (get_bits(gb, 2)) { /* switch according to coding mode */ + switch (bitstream_read(bc, 2)) { /* switch according to coding mode */ case 0: /* coded using constant number of bits */ for (i = 0; i < ctx->used_quant_units; i++) - chan->qu_sf_idx[i] = get_bits(gb, 6); + chan->qu_sf_idx[i] = bitstream_read(bc, 6); break; case 1: if (ch_num) { - vlc_tab = &sf_vlc_tabs[get_bits(gb, 2)]; + vlc_tab = &sf_vlc_tabs[bitstream_read(bc, 2)]; for (i = 0; i < ctx->used_quant_units; i++) { - delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1); + delta = bitstream_read_vlc(bc, vlc_tab->table, vlc_tab->bits, 1); chan->qu_sf_idx[i] = (ref_chan->qu_sf_idx[i] + delta) & 0x3F; } } else { - weight_idx = get_bits(gb, 2); + weight_idx = bitstream_read(bc, 2); if (weight_idx == 3) { - UNPACK_SF_VQ_SHAPE(gb, chan->qu_sf_idx, ctx->used_quant_units); + UNPACK_SF_VQ_SHAPE(bc, chan->qu_sf_idx, ctx->used_quant_units); - num_long_vals = get_bits(gb, 5); - delta_bits = get_bits(gb, 2); - min_val = get_bits(gb, 4) - 7; + num_long_vals = bitstream_read(bc, 5); + delta_bits = bitstream_read(bc, 2); + min_val = bitstream_read(bc, 4) - 7; for (i = 0; i < num_long_vals; i++) chan->qu_sf_idx[i] = (chan->qu_sf_idx[i] + - get_bits(gb, 4) - 7) & 0x3F; + bitstream_read(bc, 4) - 7) & 0x3F; /* all others are: min_val + delta */ for (i = num_long_vals; i < ctx->used_quant_units; i++) chan->qu_sf_idx[i] = (chan->qu_sf_idx[i] + min_val + - get_bitsz(gb, delta_bits)) & 0x3F; + bitstream_read(bc, delta_bits)) & 0x3F; } else { - num_long_vals = get_bits(gb, 5); - delta_bits = get_bits(gb, 3); - min_val = get_bits(gb, 6); + num_long_vals = bitstream_read(bc, 5); + delta_bits = bitstream_read(bc, 3); + min_val = bitstream_read(bc, 6); if (num_long_vals > ctx->used_quant_units || delta_bits == 7) { av_log(avctx, AV_LOG_ERROR, "SF mode 1: invalid parameters!\n"); @@ -526,34 +527,34 @@ static int decode_channel_sf_idx(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, /* read full-precision SF indexes */ for (i = 0; i < num_long_vals; i++) - chan->qu_sf_idx[i] = get_bits(gb, 6); + chan->qu_sf_idx[i] = bitstream_read(bc, 6); /* all others are: min_val + delta */ for (i = num_long_vals; i < ctx->used_quant_units; i++) chan->qu_sf_idx[i] = (min_val + - get_bitsz(gb, delta_bits)) & 0x3F; + bitstream_read(bc, delta_bits)) & 0x3F; } } break; case 2: if (ch_num) { - vlc_tab = &sf_vlc_tabs[get_bits(gb, 2)]; + vlc_tab = &sf_vlc_tabs[bitstream_read(bc, 2)]; - delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1); + delta = bitstream_read_vlc(bc, vlc_tab->table, vlc_tab->bits, 1); chan->qu_sf_idx[0] = (ref_chan->qu_sf_idx[0] + delta) & 0x3F; for (i = 1; i < ctx->used_quant_units; i++) { diff = ref_chan->qu_sf_idx[i] - ref_chan->qu_sf_idx[i - 1]; - delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1); + delta = bitstream_read_vlc(bc, vlc_tab->table, vlc_tab->bits, 1); chan->qu_sf_idx[i] = (chan->qu_sf_idx[i - 1] + diff + delta) & 0x3F; } } else { - vlc_tab = &sf_vlc_tabs[get_bits(gb, 2) + 4]; + vlc_tab = &sf_vlc_tabs[bitstream_read(bc, 2) + 4]; - UNPACK_SF_VQ_SHAPE(gb, chan->qu_sf_idx, ctx->used_quant_units); + UNPACK_SF_VQ_SHAPE(bc, chan->qu_sf_idx, ctx->used_quant_units); for (i = 0; i < ctx->used_quant_units; i++) { - delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1); + delta = bitstream_read_vlc(bc, vlc_tab->table, vlc_tab->bits, 1); chan->qu_sf_idx[i] = (chan->qu_sf_idx[i] + sign_extend(delta, 4)) & 0x3F; } @@ -565,29 +566,29 @@ static int decode_channel_sf_idx(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, for (i = 0; i < ctx->used_quant_units; i++) chan->qu_sf_idx[i] = ref_chan->qu_sf_idx[i]; } else { - weight_idx = get_bits(gb, 2); - vlc_sel = get_bits(gb, 2); + weight_idx = bitstream_read(bc, 2); + vlc_sel = bitstream_read(bc, 2); vlc_tab = &sf_vlc_tabs[vlc_sel]; if (weight_idx == 3) { vlc_tab = &sf_vlc_tabs[vlc_sel + 4]; - UNPACK_SF_VQ_SHAPE(gb, chan->qu_sf_idx, ctx->used_quant_units); + UNPACK_SF_VQ_SHAPE(bc, chan->qu_sf_idx, ctx->used_quant_units); - diff = (get_bits(gb, 4) + 56) & 0x3F; - chan->qu_sf_idx[0] = (chan->qu_sf_idx[0] + diff) & 0x3F; + diff = (bitstream_read(bc, 4) + 56) & 0x3F; + chan->qu_sf_idx[0] = (chan->qu_sf_idx[0] + diff) & 0x3F; for (i = 1; i < ctx->used_quant_units; i++) { - delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1); + delta = bitstream_read_vlc(bc, vlc_tab->table, vlc_tab->bits, 1); diff = (diff + sign_extend(delta, 4)) & 0x3F; chan->qu_sf_idx[i] = (diff + chan->qu_sf_idx[i]) & 0x3F; } } else { /* 1st coefficient is coded directly */ - chan->qu_sf_idx[0] = get_bits(gb, 6); + chan->qu_sf_idx[0] = bitstream_read(bc, 6); for (i = 1; i < ctx->used_quant_units; i++) { - delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1); + delta = bitstream_read_vlc(bc, vlc_tab->table, vlc_tab->bits, 1); chan->qu_sf_idx[i] = (chan->qu_sf_idx[i - 1] + delta) & 0x3F; } } @@ -604,13 +605,13 @@ static int decode_channel_sf_idx(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, /** * Decode word length information for each channel. * - * @param[in] gb the GetBit context + * @param[in] bc the Bitstream context * @param[in,out] ctx ptr to the channel unit context * @param[in] num_channels number of channels to process * @param[in] avctx ptr to the AVCodecContext * @return result code: 0 = OK, otherwise - error code */ -static int decode_quant_wordlen(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, +static int decode_quant_wordlen(BitstreamContext *bc, Atrac3pChanUnitCtx *ctx, int num_channels, AVCodecContext *avctx) { int ch_num, i, ret; @@ -619,7 +620,7 @@ static int decode_quant_wordlen(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, memset(ctx->channels[ch_num].qu_wordlen, 0, sizeof(ctx->channels[ch_num].qu_wordlen)); - if ((ret = decode_channel_wordlen(gb, ctx, ch_num, avctx)) < 0) + if ((ret = decode_channel_wordlen(bc, ctx, ch_num, avctx)) < 0) return ret; } @@ -637,13 +638,13 @@ static int decode_quant_wordlen(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, /** * Decode scale factor indexes for each channel. * - * @param[in] gb the GetBit context + * @param[in] bc the Bitstream context * @param[in,out] ctx ptr to the channel unit context * @param[in] num_channels number of channels to process * @param[in] avctx ptr to the AVCodecContext * @return result code: 0 = OK, otherwise - error code */ -static int decode_scale_factors(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, +static int decode_scale_factors(BitstreamContext *bc, Atrac3pChanUnitCtx *ctx, int num_channels, AVCodecContext *avctx) { int ch_num, ret; @@ -655,7 +656,7 @@ static int decode_scale_factors(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, memset(ctx->channels[ch_num].qu_sf_idx, 0, sizeof(ctx->channels[ch_num].qu_sf_idx)); - if ((ret = decode_channel_sf_idx(gb, ctx, ch_num, avctx)) < 0) + if ((ret = decode_channel_sf_idx(bc, ctx, ch_num, avctx)) < 0) return ret; } @@ -665,18 +666,18 @@ static int decode_scale_factors(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, /** * Decode number of code table values. * - * @param[in] gb the GetBit context + * @param[in] bc the Bitstream context * @param[in,out] ctx ptr to the channel unit context * @param[in] avctx ptr to the AVCodecContext * @return result code: 0 = OK, otherwise - error code */ -static int get_num_ct_values(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, +static int get_num_ct_values(BitstreamContext *bc, Atrac3pChanUnitCtx *ctx, AVCodecContext *avctx) { int num_coded_vals; - if (get_bits1(gb)) { - num_coded_vals = get_bits(gb, 5); + if (bitstream_read_bit(bc)) { + num_coded_vals = bitstream_read(bc, 5); if (num_coded_vals > ctx->used_quant_units) { av_log(avctx, AV_LOG_ERROR, "Invalid number of code table indexes: %d!\n", num_coded_vals); @@ -688,7 +689,7 @@ static int get_num_ct_values(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, } #define DEC_CT_IDX_COMMON(OP) \ - num_vals = get_num_ct_values(gb, ctx, avctx); \ + num_vals = get_num_ct_values(bc, ctx, avctx); \ if (num_vals < 0) \ return num_vals; \ \ @@ -697,33 +698,33 @@ static int get_num_ct_values(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, chan->qu_tab_idx[i] = OP; \ } else if (ch_num && ref_chan->qu_wordlen[i]) \ /* get clone master flag */ \ - chan->qu_tab_idx[i] = get_bits1(gb); \ + chan->qu_tab_idx[i] = bitstream_read_bit(bc); \ } -#define CODING_DIRECT get_bits(gb, num_bits) +#define CODING_DIRECT bitstream_read(bc, num_bits) -#define CODING_VLC get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1) +#define CODING_VLC bitstream_read_vlc(bc, vlc_tab->table, vlc_tab->bits, 1) #define CODING_VLC_DELTA \ (!i) ? CODING_VLC \ - : (pred + get_vlc2(gb, delta_vlc->table, \ - delta_vlc->bits, 1)) & mask; \ + : (pred + bitstream_read_vlc(bc, delta_vlc->table, \ + delta_vlc->bits, 1)) & mask; \ pred = chan->qu_tab_idx[i] #define CODING_VLC_DIFF \ (ref_chan->qu_tab_idx[i] + \ - get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1)) & mask + bitstream_read_vlc(bc, vlc_tab->table, vlc_tab->bits, 1)) & mask /** * Decode code table indexes for each quant unit of a channel. * - * @param[in] gb the GetBit context + * @param[in] bc the Bitstream context * @param[in,out] ctx ptr to the channel unit context * @param[in] ch_num channel to process * @param[in] avctx ptr to the AVCodecContext * @return result code: 0 = OK, otherwise - error code */ -static int decode_channel_code_tab(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, +static int decode_channel_code_tab(BitstreamContext *bc, Atrac3pChanUnitCtx *ctx, int ch_num, AVCodecContext *avctx) { int i, num_vals, num_bits, pred; @@ -732,9 +733,9 @@ static int decode_channel_code_tab(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, Atrac3pChanParams *chan = &ctx->channels[ch_num]; Atrac3pChanParams *ref_chan = &ctx->channels[0]; - chan->table_type = get_bits1(gb); + chan->table_type = bitstream_read_bit(bc); - switch (get_bits(gb, 2)) { /* switch according to coding mode */ + switch (bitstream_read(bc, 2)) { /* switch according to coding mode */ case 0: /* directly coded */ num_bits = ctx->use_full_table + 2; DEC_CT_IDX_COMMON(CODING_DIRECT); @@ -770,13 +771,13 @@ static int decode_channel_code_tab(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, /** * Decode code table indexes for each channel. * - * @param[in] gb the GetBit context + * @param[in] bc the Bitstream context * @param[in,out] ctx ptr to the channel unit context * @param[in] num_channels number of channels to process * @param[in] avctx ptr to the AVCodecContext * @return result code: 0 = OK, otherwise - error code */ -static int decode_code_table_indexes(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, +static int decode_code_table_indexes(BitstreamContext *bc, Atrac3pChanUnitCtx *ctx, int num_channels, AVCodecContext *avctx) { int ch_num, ret; @@ -784,13 +785,13 @@ static int decode_code_table_indexes(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, if (!ctx->used_quant_units) return 0; - ctx->use_full_table = get_bits1(gb); + ctx->use_full_table = bitstream_read_bit(bc); for (ch_num = 0; ch_num < num_channels; ch_num++) { memset(ctx->channels[ch_num].qu_tab_idx, 0, sizeof(ctx->channels[ch_num].qu_tab_idx)); - if ((ret = decode_channel_code_tab(gb, ctx, ch_num, avctx)) < 0) + if ((ret = decode_channel_code_tab(bc, ctx, ch_num, avctx)) < 0) return ret; } @@ -803,13 +804,13 @@ static int decode_code_table_indexes(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, * This is a generalized version for all known coding modes. * Its speed can be improved by creating separate functions for each mode. * - * @param[in] gb the GetBit context + * @param[in] bc the Bitstream context * @param[in] tab code table telling how to decode spectral lines * @param[in] vlc_tab ptr to the huffman table associated with the code table * @param[out] out pointer to buffer where decoded data should be stored * @param[in] num_specs number of spectral lines to decode */ -static void decode_qu_spectra(GetBitContext *gb, const Atrac3pSpecCodeTab *tab, +static void decode_qu_spectra(BitstreamContext *bc, const Atrac3pSpecCodeTab *tab, VLC *vlc_tab, int16_t *out, const int num_specs) { int i, j, pos, cf; @@ -820,15 +821,15 @@ static void decode_qu_spectra(GetBitContext *gb, const Atrac3pSpecCodeTab *tab, unsigned val, mask = (1 << bits) - 1; for (pos = 0; pos < num_specs;) { - if (group_size == 1 || get_bits1(gb)) { + if (group_size == 1 || bitstream_read_bit(bc)) { for (j = 0; j < group_size; j++) { - val = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1); + val = bitstream_read_vlc(bc, vlc_tab->table, vlc_tab->bits, 1); for (i = 0; i < num_coeffs; i++) { cf = val & mask; if (is_signed) cf = sign_extend(cf, bits); - else if (cf && get_bits1(gb)) + else if (cf && bitstream_read_bit(bc)) cf = -cf; out[pos++] = cf; @@ -843,12 +844,12 @@ static void decode_qu_spectra(GetBitContext *gb, const Atrac3pSpecCodeTab *tab, /** * Decode huffman-coded IMDCT spectrum for all channels. * - * @param[in] gb the GetBit context + * @param[in] bc the Bitstream context * @param[in,out] ctx ptr to the channel unit context * @param[in] num_channels number of channels to process * @param[in] avctx ptr to the AVCodecContext */ -static void decode_spectrum(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, +static void decode_spectrum(BitstreamContext *bc, Atrac3pChanUnitCtx *ctx, int num_channels, AVCodecContext *avctx) { int i, ch_num, qu, wordlen, codetab, tab_index, num_specs; @@ -880,7 +881,7 @@ static void decode_spectrum(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, if (tab->redirect >= 0) tab_index = tab->redirect; - decode_qu_spectra(gb, tab, &spec_vlc_tabs[tab_index], + decode_qu_spectra(bc, tab, &spec_vlc_tabs[tab_index], &chan->spectrum[ff_atrac3p_qu_to_spec_pos[qu]], num_specs); } else if (ch_num && ctx->channels[0].qu_wordlen[qu] && !codetab) { @@ -900,7 +901,7 @@ static void decode_spectrum(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, if (ctx->used_quant_units > 2) { num_specs = atrac3p_subband_to_num_powgrps[ctx->num_coded_subbands - 1]; for (i = 0; i < num_specs; i++) - chan->power_levs[i] = get_bits(gb, 4); + chan->power_levs[i] = bitstream_read(bc, 4); } } } @@ -913,22 +914,22 @@ static void decode_spectrum(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, * Otherwise, all necessary bits will be directly stored * prefixed by two signal bits = 1,1. * - * @param[in] gb ptr to the GetBitContext + * @param[in] bc ptr to the BitstreamContext * @param[out] out where to place decoded flags * @param[in] num_flags number of flags to process * @return: 0 = all flag bits are zero, 1 = there is at least one non-zero flag bit */ -static int get_subband_flags(GetBitContext *gb, uint8_t *out, int num_flags) +static int get_subband_flags(BitstreamContext *bc, uint8_t *out, int num_flags) { int i, result; memset(out, 0, num_flags); - result = get_bits1(gb); + result = bitstream_read_bit(bc); if (result) { - if (get_bits1(gb)) + if (bitstream_read_bit(bc)) for (i = 0; i < num_flags; i++) - out[i] = get_bits1(gb); + out[i] = bitstream_read_bit(bc); else memset(out, 1, num_flags); } @@ -939,63 +940,63 @@ static int get_subband_flags(GetBitContext *gb, uint8_t *out, int num_flags) /** * Decode mdct window shape flags for all channels. * - * @param[in] gb the GetBit context + * @param[in] bc the Bitstream context * @param[in,out] ctx ptr to the channel unit context * @param[in] num_channels number of channels to process */ -static void decode_window_shape(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, +static void decode_window_shape(BitstreamContext *bc, Atrac3pChanUnitCtx *ctx, int num_channels) { int ch_num; for (ch_num = 0; ch_num < num_channels; ch_num++) - get_subband_flags(gb, ctx->channels[ch_num].wnd_shape, + get_subband_flags(bc, ctx->channels[ch_num].wnd_shape, ctx->num_subbands); } /** * Decode number of gain control points. * - * @param[in] gb the GetBit context + * @param[in] bc the Bitstream context * @param[in,out] ctx ptr to the channel unit context * @param[in] ch_num channel to process * @param[in] coded_subbands number of subbands to process * @return result code: 0 = OK, otherwise - error code */ -static int decode_gainc_npoints(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, +static int decode_gainc_npoints(BitstreamContext *bc, Atrac3pChanUnitCtx *ctx, int ch_num, int coded_subbands) { int i, delta, delta_bits, min_val; Atrac3pChanParams *chan = &ctx->channels[ch_num]; Atrac3pChanParams *ref_chan = &ctx->channels[0]; - switch (get_bits(gb, 2)) { /* switch according to coding mode */ + switch (bitstream_read(bc, 2)) { /* switch according to coding mode */ case 0: /* fixed-length coding */ for (i = 0; i < coded_subbands; i++) - chan->gain_data[i].num_points = get_bits(gb, 3); + chan->gain_data[i].num_points = bitstream_read(bc, 3); break; case 1: /* variable-length coding */ for (i = 0; i < coded_subbands; i++) chan->gain_data[i].num_points = - get_vlc2(gb, gain_vlc_tabs[0].table, - gain_vlc_tabs[0].bits, 1); + bitstream_read_vlc(bc, gain_vlc_tabs[0].table, + gain_vlc_tabs[0].bits, 1); break; case 2: if (ch_num) { /* VLC modulo delta to master channel */ for (i = 0; i < coded_subbands; i++) { - delta = get_vlc2(gb, gain_vlc_tabs[1].table, - gain_vlc_tabs[1].bits, 1); + delta = bitstream_read_vlc(bc, gain_vlc_tabs[1].table, + gain_vlc_tabs[1].bits, 1); chan->gain_data[i].num_points = (ref_chan->gain_data[i].num_points + delta) & 7; } } else { /* VLC modulo delta to previous */ chan->gain_data[0].num_points = - get_vlc2(gb, gain_vlc_tabs[0].table, - gain_vlc_tabs[0].bits, 1); + bitstream_read_vlc(bc, gain_vlc_tabs[0].table, + gain_vlc_tabs[0].bits, 1); for (i = 1; i < coded_subbands; i++) { - delta = get_vlc2(gb, gain_vlc_tabs[1].table, - gain_vlc_tabs[1].bits, 1); + delta = bitstream_read_vlc(bc, gain_vlc_tabs[1].table, + gain_vlc_tabs[1].bits, 1); chan->gain_data[i].num_points = (chan->gain_data[i - 1].num_points + delta) & 7; } @@ -1007,11 +1008,11 @@ static int decode_gainc_npoints(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, chan->gain_data[i].num_points = ref_chan->gain_data[i].num_points; } else { /* shorter delta to min */ - delta_bits = get_bits(gb, 2); - min_val = get_bits(gb, 3); + delta_bits = bitstream_read(bc, 2); + min_val = bitstream_read(bc, 3); for (i = 0; i < coded_subbands; i++) { - chan->gain_data[i].num_points = min_val + get_bitsz(gb, delta_bits); + chan->gain_data[i].num_points = min_val + bitstream_read(bc, delta_bits); if (chan->gain_data[i].num_points > 7) return AVERROR_INVALIDDATA; } @@ -1038,23 +1039,23 @@ static inline void gainc_level_mode3s(AtracGainInfo *dst, AtracGainInfo *ref) /** * Implements coding mode 1 (master) for gain compensation levels. * - * @param[in] gb the GetBit context + * @param[in] bc the Bitstream context * @param[in] ctx ptr to the channel unit context * @param[out] dst ptr to the output array */ -static inline void gainc_level_mode1m(GetBitContext *gb, +static inline void gainc_level_mode1m(BitstreamContext *bc, Atrac3pChanUnitCtx *ctx, AtracGainInfo *dst) { int i, delta; if (dst->num_points > 0) - dst->lev_code[0] = get_vlc2(gb, gain_vlc_tabs[2].table, - gain_vlc_tabs[2].bits, 1); + dst->lev_code[0] = bitstream_read_vlc(bc, gain_vlc_tabs[2].table, + gain_vlc_tabs[2].bits, 1); for (i = 1; i < dst->num_points; i++) { - delta = get_vlc2(gb, gain_vlc_tabs[3].table, - gain_vlc_tabs[3].bits, 1); + delta = bitstream_read_vlc(bc, gain_vlc_tabs[3].table, + gain_vlc_tabs[3].bits, 1); dst->lev_code[i] = (dst->lev_code[i - 1] + delta) & 0xF; } } @@ -1062,58 +1063,58 @@ static inline void gainc_level_mode1m(GetBitContext *gb, /** * Decode level code for each gain control point. * - * @param[in] gb the GetBit context + * @param[in] bc the Bitstream context * @param[in,out] ctx ptr to the channel unit context * @param[in] ch_num channel to process * @param[in] coded_subbands number of subbands to process * @return result code: 0 = OK, otherwise - error code */ -static int decode_gainc_levels(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, +static int decode_gainc_levels(BitstreamContext *bc, Atrac3pChanUnitCtx *ctx, int ch_num, int coded_subbands) { int sb, i, delta, delta_bits, min_val, pred; Atrac3pChanParams *chan = &ctx->channels[ch_num]; Atrac3pChanParams *ref_chan = &ctx->channels[0]; - switch (get_bits(gb, 2)) { /* switch according to coding mode */ + switch (bitstream_read(bc, 2)) { /* switch according to coding mode */ case 0: /* fixed-length coding */ for (sb = 0; sb < coded_subbands; sb++) for (i = 0; i < chan->gain_data[sb].num_points; i++) - chan->gain_data[sb].lev_code[i] = get_bits(gb, 4); + chan->gain_data[sb].lev_code[i] = bitstream_read(bc, 4); break; case 1: if (ch_num) { /* VLC modulo delta to master channel */ for (sb = 0; sb < coded_subbands; sb++) for (i = 0; i < chan->gain_data[sb].num_points; i++) { - delta = get_vlc2(gb, gain_vlc_tabs[5].table, - gain_vlc_tabs[5].bits, 1); + delta = bitstream_read_vlc(bc, gain_vlc_tabs[5].table, + gain_vlc_tabs[5].bits, 1); pred = (i >= ref_chan->gain_data[sb].num_points) ? 7 : ref_chan->gain_data[sb].lev_code[i]; chan->gain_data[sb].lev_code[i] = (pred + delta) & 0xF; } } else { /* VLC modulo delta to previous */ for (sb = 0; sb < coded_subbands; sb++) - gainc_level_mode1m(gb, ctx, &chan->gain_data[sb]); + gainc_level_mode1m(bc, ctx, &chan->gain_data[sb]); } break; case 2: if (ch_num) { /* VLC modulo delta to previous or clone master */ for (sb = 0; sb < coded_subbands; sb++) if (chan->gain_data[sb].num_points > 0) { - if (get_bits1(gb)) - gainc_level_mode1m(gb, ctx, &chan->gain_data[sb]); + if (bitstream_read_bit(bc)) + gainc_level_mode1m(bc, ctx, &chan->gain_data[sb]); else gainc_level_mode3s(&chan->gain_data[sb], &ref_chan->gain_data[sb]); } } else { /* VLC modulo delta to lev_codes of previous subband */ if (chan->gain_data[0].num_points > 0) - gainc_level_mode1m(gb, ctx, &chan->gain_data[0]); + gainc_level_mode1m(bc, ctx, &chan->gain_data[0]); for (sb = 1; sb < coded_subbands; sb++) for (i = 0; i < chan->gain_data[sb].num_points; i++) { - delta = get_vlc2(gb, gain_vlc_tabs[4].table, - gain_vlc_tabs[4].bits, 1); + delta = bitstream_read_vlc(bc, gain_vlc_tabs[4].table, + gain_vlc_tabs[4].bits, 1); pred = (i >= chan->gain_data[sb - 1].num_points) ? 7 : chan->gain_data[sb - 1].lev_code[i]; chan->gain_data[sb].lev_code[i] = (pred + delta) & 0xF; @@ -1126,12 +1127,12 @@ static int decode_gainc_levels(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, gainc_level_mode3s(&chan->gain_data[sb], &ref_chan->gain_data[sb]); } else { /* shorter delta to min */ - delta_bits = get_bits(gb, 2); - min_val = get_bits(gb, 4); + delta_bits = bitstream_read(bc, 2); + min_val = bitstream_read(bc, 4); for (sb = 0; sb < coded_subbands; sb++) for (i = 0; i < chan->gain_data[sb].num_points; i++) { - chan->gain_data[sb].lev_code[i] = min_val + get_bitsz(gb, delta_bits); + chan->gain_data[sb].lev_code[i] = min_val + bitstream_read(bc, delta_bits); if (chan->gain_data[sb].lev_code[i] > 15) return AVERROR_INVALIDDATA; } @@ -1145,35 +1146,35 @@ static int decode_gainc_levels(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, /** * Implements coding mode 0 for gain compensation locations. * - * @param[in] gb the GetBit context + * @param[in] bc the Bitstream context * @param[in] ctx ptr to the channel unit context * @param[out] dst ptr to the output array * @param[in] pos position of the value to be processed */ -static inline void gainc_loc_mode0(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, +static inline void gainc_loc_mode0(BitstreamContext *bc, Atrac3pChanUnitCtx *ctx, AtracGainInfo *dst, int pos) { int delta_bits; if (!pos || dst->loc_code[pos - 1] < 15) - dst->loc_code[pos] = get_bits(gb, 5); + dst->loc_code[pos] = bitstream_read(bc, 5); else if (dst->loc_code[pos - 1] >= 30) dst->loc_code[pos] = 31; else { delta_bits = av_log2(30 - dst->loc_code[pos - 1]) + 1; dst->loc_code[pos] = dst->loc_code[pos - 1] + - get_bits(gb, delta_bits) + 1; + bitstream_read(bc, delta_bits) + 1; } } /** * Implements coding mode 1 for gain compensation locations. * - * @param[in] gb the GetBit context + * @param[in] bc the Bitstream context * @param[in] ctx ptr to the channel unit context * @param[out] dst ptr to the output array */ -static inline void gainc_loc_mode1(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, +static inline void gainc_loc_mode1(BitstreamContext *bc, Atrac3pChanUnitCtx *ctx, AtracGainInfo *dst) { int i; @@ -1181,7 +1182,7 @@ static inline void gainc_loc_mode1(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, if (dst->num_points > 0) { /* 1st coefficient is stored directly */ - dst->loc_code[0] = get_bits(gb, 5); + dst->loc_code[0] = bitstream_read(bc, 5); for (i = 1; i < dst->num_points; i++) { /* switch VLC according to the curve direction @@ -1190,7 +1191,7 @@ static inline void gainc_loc_mode1(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, ? &gain_vlc_tabs[7] : &gain_vlc_tabs[9]; dst->loc_code[i] = dst->loc_code[i - 1] + - get_vlc2(gb, tab->table, tab->bits, 1); + bitstream_read_vlc(bc, tab->table, tab->bits, 1); } } } @@ -1198,14 +1199,14 @@ static inline void gainc_loc_mode1(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, /** * Decode location code for each gain control point. * - * @param[in] gb the GetBit context + * @param[in] bc the Bitstream context * @param[in,out] ctx ptr to the channel unit context * @param[in] ch_num channel to process * @param[in] coded_subbands number of subbands to process * @param[in] avctx ptr to the AVCodecContext * @return result code: 0 = OK, otherwise - error code */ -static int decode_gainc_loc_codes(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, +static int decode_gainc_loc_codes(BitstreamContext *bc, Atrac3pChanUnitCtx *ctx, int ch_num, int coded_subbands, AVCodecContext *avctx) { @@ -1215,11 +1216,11 @@ static int decode_gainc_loc_codes(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, Atrac3pChanParams *chan = &ctx->channels[ch_num]; Atrac3pChanParams *ref_chan = &ctx->channels[0]; - switch (get_bits(gb, 2)) { /* switch according to coding mode */ + switch (bitstream_read(bc, 2)) { /* switch according to coding mode */ case 0: /* sequence of numbers in ascending order */ for (sb = 0; sb < coded_subbands; sb++) for (i = 0; i < chan->gain_data[sb].num_points; i++) - gainc_loc_mode0(gb, ctx, &chan->gain_data[sb], i); + gainc_loc_mode0(bc, ctx, &chan->gain_data[sb], i); break; case 1: if (ch_num) { @@ -1230,8 +1231,8 @@ static int decode_gainc_loc_codes(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, ref = &ref_chan->gain_data[sb]; /* 1st value is vlc-coded modulo delta to master */ - delta = get_vlc2(gb, gain_vlc_tabs[10].table, - gain_vlc_tabs[10].bits, 1); + delta = bitstream_read_vlc(bc, gain_vlc_tabs[10].table, + gain_vlc_tabs[10].bits, 1); pred = ref->num_points > 0 ? ref->loc_code[0] : 0; dst->loc_code[0] = (pred + delta) & 0x1F; @@ -1241,19 +1242,19 @@ static int decode_gainc_loc_codes(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, /* ascending curve */ if (more_than_ref) { delta = - get_vlc2(gb, gain_vlc_tabs[9].table, - gain_vlc_tabs[9].bits, 1); + bitstream_read_vlc(bc, gain_vlc_tabs[9].table, + gain_vlc_tabs[9].bits, 1); dst->loc_code[i] = dst->loc_code[i - 1] + delta; } else { - if (get_bits1(gb)) - gainc_loc_mode0(gb, ctx, dst, i); // direct coding + if (bitstream_read_bit(bc)) + gainc_loc_mode0(bc, ctx, dst, i); // direct coding else dst->loc_code[i] = ref->loc_code[i]; // clone master } } else { /* descending curve */ tab = more_than_ref ? &gain_vlc_tabs[7] : &gain_vlc_tabs[10]; - delta = get_vlc2(gb, tab->table, tab->bits, 1); + delta = bitstream_read_vlc(bc, tab->table, tab->bits, 1); if (more_than_ref) dst->loc_code[i] = dst->loc_code[i - 1] + delta; else @@ -1263,7 +1264,7 @@ static int decode_gainc_loc_codes(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, } } else /* VLC delta to previous */ for (sb = 0; sb < coded_subbands; sb++) - gainc_loc_mode1(gb, ctx, &chan->gain_data[sb]); + gainc_loc_mode1(bc, ctx, &chan->gain_data[sb]); break; case 2: if (ch_num) { @@ -1272,8 +1273,8 @@ static int decode_gainc_loc_codes(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, continue; dst = &chan->gain_data[sb]; ref = &ref_chan->gain_data[sb]; - if (dst->num_points > ref->num_points || get_bits1(gb)) - gainc_loc_mode1(gb, ctx, dst); + if (dst->num_points > ref->num_points || bitstream_read_bit(bc)) + gainc_loc_mode1(bc, ctx, dst); else /* clone master for the whole subband */ for (i = 0; i < chan->gain_data[sb].num_points; i++) dst->loc_code[i] = ref->loc_code[i]; @@ -1281,7 +1282,7 @@ static int decode_gainc_loc_codes(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, } else { /* data for the first subband is coded directly */ for (i = 0; i < chan->gain_data[0].num_points; i++) - gainc_loc_mode0(gb, ctx, &chan->gain_data[0], i); + gainc_loc_mode0(bc, ctx, &chan->gain_data[0], i); for (sb = 1; sb < coded_subbands; sb++) { if (chan->gain_data[sb].num_points <= 0) @@ -1290,8 +1291,8 @@ static int decode_gainc_loc_codes(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, /* 1st value is vlc-coded modulo delta to the corresponding * value of the previous subband if any or zero */ - delta = get_vlc2(gb, gain_vlc_tabs[6].table, - gain_vlc_tabs[6].bits, 1); + delta = bitstream_read_vlc(bc, gain_vlc_tabs[6].table, + gain_vlc_tabs[6].bits, 1); pred = dst[-1].num_points > 0 ? dst[-1].loc_code[0] : 0; dst->loc_code[0] = (pred + delta) & 0x1F; @@ -1302,7 +1303,7 @@ static int decode_gainc_loc_codes(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, * presence of prediction. */ tab = &gain_vlc_tabs[(dst->lev_code[i] > dst->lev_code[i - 1]) * 2 + more_than_ref + 6]; - delta = get_vlc2(gb, tab->table, tab->bits, 1); + delta = bitstream_read_vlc(bc, tab->table, tab->bits, 1); if (more_than_ref) dst->loc_code[i] = dst->loc_code[i - 1] + delta; else @@ -1316,19 +1317,19 @@ static int decode_gainc_loc_codes(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, for (sb = 0; sb < coded_subbands; sb++) for (i = 0; i < chan->gain_data[sb].num_points; i++) { if (i >= ref_chan->gain_data[sb].num_points) - gainc_loc_mode0(gb, ctx, &chan->gain_data[sb], i); + gainc_loc_mode0(bc, ctx, &chan->gain_data[sb], i); else chan->gain_data[sb].loc_code[i] = ref_chan->gain_data[sb].loc_code[i]; } } else { /* shorter delta to min */ - delta_bits = get_bits(gb, 2) + 1; - min_val = get_bits(gb, 5); + delta_bits = bitstream_read(bc, 2) + 1; + min_val = bitstream_read(bc, 5); for (sb = 0; sb < coded_subbands; sb++) for (i = 0; i < chan->gain_data[sb].num_points; i++) chan->gain_data[sb].loc_code[i] = min_val + i + - get_bits(gb, delta_bits); + bitstream_read(bc, delta_bits); } break; } @@ -1353,13 +1354,13 @@ static int decode_gainc_loc_codes(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, /** * Decode gain control data for all channels. * - * @param[in] gb the GetBit context + * @param[in] bc the Bitstream context * @param[in,out] ctx ptr to the channel unit context * @param[in] num_channels number of channels to process * @param[in] avctx ptr to the AVCodecContext * @return result code: 0 = OK, otherwise - error code */ -static int decode_gainc_data(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, +static int decode_gainc_data(BitstreamContext *bc, Atrac3pChanUnitCtx *ctx, int num_channels, AVCodecContext *avctx) { int ch_num, coded_subbands, sb, ret; @@ -1368,16 +1369,16 @@ static int decode_gainc_data(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, memset(ctx->channels[ch_num].gain_data, 0, sizeof(*ctx->channels[ch_num].gain_data) * ATRAC3P_SUBBANDS); - if (get_bits1(gb)) { /* gain control data present? */ - coded_subbands = get_bits(gb, 4) + 1; - if (get_bits1(gb)) /* is high band gain data replication on? */ - ctx->channels[ch_num].num_gain_subbands = get_bits(gb, 4) + 1; + if (bitstream_read_bit(bc)) { /* gain control data present? */ + coded_subbands = bitstream_read(bc, 4) + 1; + if (bitstream_read_bit(bc)) /* is high band gain data replication on? */ + ctx->channels[ch_num].num_gain_subbands = bitstream_read(bc, 4) + 1; else ctx->channels[ch_num].num_gain_subbands = coded_subbands; - if ((ret = decode_gainc_npoints(gb, ctx, ch_num, coded_subbands)) < 0 || - (ret = decode_gainc_levels(gb, ctx, ch_num, coded_subbands)) < 0 || - (ret = decode_gainc_loc_codes(gb, ctx, ch_num, coded_subbands, avctx)) < 0) + if ((ret = decode_gainc_npoints(bc, ctx, ch_num, coded_subbands)) < 0 || + (ret = decode_gainc_levels(bc, ctx, ch_num, coded_subbands)) < 0 || + (ret = decode_gainc_loc_codes(bc, ctx, ch_num, coded_subbands, avctx)) < 0) return ret; if (coded_subbands > 0) { /* propagate gain data if requested */ @@ -1396,29 +1397,29 @@ static int decode_gainc_data(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, /** * Decode envelope for all tones of a channel. * - * @param[in] gb the GetBit context + * @param[in] bc the Bitstream context * @param[in,out] ctx ptr to the channel unit context * @param[in] ch_num channel to process * @param[in] band_has_tones ptr to an array of per-band-flags: * 1 - tone data present */ -static void decode_tones_envelope(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, +static void decode_tones_envelope(BitstreamContext *bc, Atrac3pChanUnitCtx *ctx, int ch_num, int band_has_tones[]) { int sb; Atrac3pWavesData *dst = ctx->channels[ch_num].tones_info; Atrac3pWavesData *ref = ctx->channels[0].tones_info; - if (!ch_num || !get_bits1(gb)) { /* mode 0: fixed-length coding */ + if (!ch_num || !bitstream_read_bit(bc)) { /* mode 0: fixed-length coding */ for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) { if (!band_has_tones[sb]) continue; - dst[sb].pend_env.has_start_point = get_bits1(gb); + dst[sb].pend_env.has_start_point = bitstream_read_bit(bc); dst[sb].pend_env.start_pos = dst[sb].pend_env.has_start_point - ? get_bits(gb, 5) : -1; - dst[sb].pend_env.has_stop_point = get_bits1(gb); + ? bitstream_read(bc, 5) : -1; + dst[sb].pend_env.has_stop_point = bitstream_read_bit(bc); dst[sb].pend_env.stop_pos = dst[sb].pend_env.has_stop_point - ? get_bits(gb, 5) : 32; + ? bitstream_read(bc, 5) : 32; } } else { /* mode 1(slave only): copy master */ for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) { @@ -1435,7 +1436,7 @@ static void decode_tones_envelope(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, /** * Decode number of tones for each subband of a channel. * - * @param[in] gb the GetBit context + * @param[in] bc the Bitstream context * @param[in,out] ctx ptr to the channel unit context * @param[in] ch_num channel to process * @param[in] band_has_tones ptr to an array of per-band-flags: @@ -1443,7 +1444,7 @@ static void decode_tones_envelope(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, * @param[in] avctx ptr to the AVCodecContext * @return result code: 0 = OK, otherwise - error code */ -static int decode_band_numwavs(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, +static int decode_band_numwavs(BitstreamContext *bc, Atrac3pChanUnitCtx *ctx, int ch_num, int band_has_tones[], AVCodecContext *avctx) { @@ -1451,25 +1452,25 @@ static int decode_band_numwavs(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, Atrac3pWavesData *dst = ctx->channels[ch_num].tones_info; Atrac3pWavesData *ref = ctx->channels[0].tones_info; - mode = get_bits(gb, ch_num + 1); + mode = bitstream_read(bc, ch_num + 1); switch (mode) { case 0: /** fixed-length coding */ for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) if (band_has_tones[sb]) - dst[sb].num_wavs = get_bits(gb, 4); + dst[sb].num_wavs = bitstream_read(bc, 4); break; case 1: /** variable-length coding */ for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) if (band_has_tones[sb]) dst[sb].num_wavs = - get_vlc2(gb, tone_vlc_tabs[1].table, - tone_vlc_tabs[1].bits, 1); + bitstream_read_vlc(bc, tone_vlc_tabs[1].table, + tone_vlc_tabs[1].bits, 1); break; case 2: /** VLC modulo delta to master (slave only) */ for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) if (band_has_tones[sb]) { - delta = get_vlc2(gb, tone_vlc_tabs[2].table, - tone_vlc_tabs[2].bits, 1); + delta = bitstream_read_vlc(bc, tone_vlc_tabs[2].table, + tone_vlc_tabs[2].bits, 1); delta = sign_extend(delta, 3); dst[sb].num_wavs = (ref[sb].num_wavs + delta) & 0xF; } @@ -1501,13 +1502,13 @@ static int decode_band_numwavs(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, /** * Decode frequency information for each subband of a channel. * - * @param[in] gb the GetBit context + * @param[in] bc the Bitstream context * @param[in,out] ctx ptr to the channel unit context * @param[in] ch_num channel to process * @param[in] band_has_tones ptr to an array of per-band-flags: * 1 - tone data present */ -static void decode_tones_frequency(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, +static void decode_tones_frequency(BitstreamContext *bc, Atrac3pChanUnitCtx *ctx, int ch_num, int band_has_tones[]) { int sb, i, direction, nbits, pred, delta; @@ -1515,26 +1516,26 @@ static void decode_tones_frequency(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, Atrac3pWavesData *dst = ctx->channels[ch_num].tones_info; Atrac3pWavesData *ref = ctx->channels[0].tones_info; - if (!ch_num || !get_bits1(gb)) { /* mode 0: fixed-length coding */ + if (!ch_num || !bitstream_read_bit(bc)) { /* mode 0: fixed-length coding */ for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) { if (!band_has_tones[sb] || !dst[sb].num_wavs) continue; iwav = &ctx->waves_info->waves[dst[sb].start_index]; - direction = (dst[sb].num_wavs > 1) ? get_bits1(gb) : 0; + direction = (dst[sb].num_wavs > 1) ? bitstream_read_bit(bc) : 0; if (direction) { /** packed numbers in descending order */ if (dst[sb].num_wavs) - iwav[dst[sb].num_wavs - 1].freq_index = get_bits(gb, 10); + iwav[dst[sb].num_wavs - 1].freq_index = bitstream_read(bc, 10); for (i = dst[sb].num_wavs - 2; i >= 0 ; i--) { nbits = av_log2(iwav[i+1].freq_index) + 1; - iwav[i].freq_index = get_bits(gb, nbits); + iwav[i].freq_index = bitstream_read(bc, nbits); } } else { /** packed numbers in ascending order */ for (i = 0; i < dst[sb].num_wavs; i++) { if (!i || iwav[i - 1].freq_index < 512) - iwav[i].freq_index = get_bits(gb, 10); + iwav[i].freq_index = bitstream_read(bc, 10); else { nbits = av_log2(1023 - iwav[i - 1].freq_index) + 1; - iwav[i].freq_index = get_bits(gb, nbits) + + iwav[i].freq_index = bitstream_read(bc, nbits) + 1024 - (1 << nbits); } } @@ -1547,8 +1548,8 @@ static void decode_tones_frequency(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, iwav = &ctx->waves_info->waves[ref[sb].start_index]; owav = &ctx->waves_info->waves[dst[sb].start_index]; for (i = 0; i < dst[sb].num_wavs; i++) { - delta = get_vlc2(gb, tone_vlc_tabs[6].table, - tone_vlc_tabs[6].bits, 1); + delta = bitstream_read_vlc(bc, tone_vlc_tabs[6].table, + tone_vlc_tabs[6].bits, 1); delta = sign_extend(delta, 8); pred = (i < ref[sb].num_wavs) ? iwav[i].freq_index : (ref[sb].num_wavs ? iwav[ref[sb].num_wavs - 1].freq_index : 0); @@ -1561,13 +1562,13 @@ static void decode_tones_frequency(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, /** * Decode amplitude information for each subband of a channel. * - * @param[in] gb the GetBit context + * @param[in] bc the Bitstream context * @param[in,out] ctx ptr to the channel unit context * @param[in] ch_num channel to process * @param[in] band_has_tones ptr to an array of per-band-flags: * 1 - tone data present */ -static void decode_tones_amplitude(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, +static void decode_tones_amplitude(BitstreamContext *bc, Atrac3pChanUnitCtx *ctx, int ch_num, int band_has_tones[]) { int mode, sb, j, i, diff, maxdiff, fi, delta, pred; @@ -1601,7 +1602,7 @@ static void decode_tones_amplitude(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, } } - mode = get_bits(gb, ch_num + 1); + mode = bitstream_read(bc, ch_num + 1); switch (mode) { case 0: /** fixed-length coding */ @@ -1610,9 +1611,9 @@ static void decode_tones_amplitude(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, continue; if (ctx->waves_info->amplitude_mode) for (i = 0; i < dst[sb].num_wavs; i++) - ctx->waves_info->waves[dst[sb].start_index + i].amp_sf = get_bits(gb, 6); + ctx->waves_info->waves[dst[sb].start_index + i].amp_sf = bitstream_read(bc, 6); else - ctx->waves_info->waves[dst[sb].start_index].amp_sf = get_bits(gb, 6); + ctx->waves_info->waves[dst[sb].start_index].amp_sf = bitstream_read(bc, 6); } break; case 1: /** min + VLC delta */ @@ -1622,12 +1623,12 @@ static void decode_tones_amplitude(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, if (ctx->waves_info->amplitude_mode) for (i = 0; i < dst[sb].num_wavs; i++) ctx->waves_info->waves[dst[sb].start_index + i].amp_sf = - get_vlc2(gb, tone_vlc_tabs[3].table, - tone_vlc_tabs[3].bits, 1) + 20; + bitstream_read_vlc(bc, tone_vlc_tabs[3].table, + tone_vlc_tabs[3].bits, 1) + 20; else ctx->waves_info->waves[dst[sb].start_index].amp_sf = - get_vlc2(gb, tone_vlc_tabs[4].table, - tone_vlc_tabs[4].bits, 1) + 24; + bitstream_read_vlc(bc, tone_vlc_tabs[4].table, + tone_vlc_tabs[4].bits, 1) + 24; } break; case 2: /** VLC modulo delta to master (slave only) */ @@ -1635,8 +1636,8 @@ static void decode_tones_amplitude(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, if (!band_has_tones[sb] || !dst[sb].num_wavs) continue; for (i = 0; i < dst[sb].num_wavs; i++) { - delta = get_vlc2(gb, tone_vlc_tabs[5].table, - tone_vlc_tabs[5].bits, 1); + delta = bitstream_read_vlc(bc, tone_vlc_tabs[5].table, + tone_vlc_tabs[5].bits, 1); delta = sign_extend(delta, 5); pred = refwaves[dst[sb].start_index + i] >= 0 ? ctx->waves_info->waves[refwaves[dst[sb].start_index + i]].amp_sf : 34; @@ -1661,13 +1662,13 @@ static void decode_tones_amplitude(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, /** * Decode phase information for each subband of a channel. * - * @param[in] gb the GetBit context + * @param[in] bc the Bitstream context * @param[in,out] ctx ptr to the channel unit context * @param[in] ch_num channel to process * @param[in] band_has_tones ptr to an array of per-band-flags: * 1 - tone data present */ -static void decode_tones_phase(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, +static void decode_tones_phase(BitstreamContext *bc, Atrac3pChanUnitCtx *ctx, int ch_num, int band_has_tones[]) { int sb, i; @@ -1679,20 +1680,20 @@ static void decode_tones_phase(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, continue; wparam = &ctx->waves_info->waves[dst[sb].start_index]; for (i = 0; i < dst[sb].num_wavs; i++) - wparam[i].phase_index = get_bits(gb, 5); + wparam[i].phase_index = bitstream_read(bc, 5); } } /** * Decode tones info for all channels. * - * @param[in] gb the GetBit context + * @param[in] bc the Bitstream context * @param[in,out] ctx ptr to the channel unit context * @param[in] num_channels number of channels to process * @param[in] avctx ptr to the AVCodecContext * @return result code: 0 = OK, otherwise - error code */ -static int decode_tones_info(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, +static int decode_tones_info(BitstreamContext *bc, Atrac3pChanUnitCtx *ctx, int num_channels, AVCodecContext *avctx) { int ch_num, i, ret; @@ -1702,26 +1703,26 @@ static int decode_tones_info(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, memset(ctx->channels[ch_num].tones_info, 0, sizeof(*ctx->channels[ch_num].tones_info) * ATRAC3P_SUBBANDS); - ctx->waves_info->tones_present = get_bits1(gb); + ctx->waves_info->tones_present = bitstream_read_bit(bc); if (!ctx->waves_info->tones_present) return 0; memset(ctx->waves_info->waves, 0, sizeof(ctx->waves_info->waves)); - ctx->waves_info->amplitude_mode = get_bits1(gb); + ctx->waves_info->amplitude_mode = bitstream_read_bit(bc); if (!ctx->waves_info->amplitude_mode) { avpriv_report_missing_feature(avctx, "GHA amplitude mode 0"); return AVERROR_PATCHWELCOME; } ctx->waves_info->num_tone_bands = - get_vlc2(gb, tone_vlc_tabs[0].table, - tone_vlc_tabs[0].bits, 1) + 1; + bitstream_read_vlc(bc, tone_vlc_tabs[0].table, + tone_vlc_tabs[0].bits, 1) + 1; if (num_channels == 2) { - get_subband_flags(gb, ctx->waves_info->tone_sharing, ctx->waves_info->num_tone_bands); - get_subband_flags(gb, ctx->waves_info->tone_master, ctx->waves_info->num_tone_bands); - if (get_subband_flags(gb, ctx->waves_info->phase_shift, + get_subband_flags(bc, ctx->waves_info->tone_sharing, ctx->waves_info->num_tone_bands); + get_subband_flags(bc, ctx->waves_info->tone_master, ctx->waves_info->num_tone_bands); + if (get_subband_flags(bc, ctx->waves_info->phase_shift, ctx->waves_info->num_tone_bands)) { avpriv_report_missing_feature(avctx, "GHA Phase shifting"); return AVERROR_PATCHWELCOME; @@ -1734,14 +1735,14 @@ static int decode_tones_info(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, for (i = 0; i < ctx->waves_info->num_tone_bands; i++) band_has_tones[i] = !ch_num ? 1 : !ctx->waves_info->tone_sharing[i]; - decode_tones_envelope(gb, ctx, ch_num, band_has_tones); - if ((ret = decode_band_numwavs(gb, ctx, ch_num, band_has_tones, + decode_tones_envelope(bc, ctx, ch_num, band_has_tones); + if ((ret = decode_band_numwavs(bc, ctx, ch_num, band_has_tones, avctx)) < 0) return ret; - decode_tones_frequency(gb, ctx, ch_num, band_has_tones); - decode_tones_amplitude(gb, ctx, ch_num, band_has_tones); - decode_tones_phase(gb, ctx, ch_num, band_has_tones); + decode_tones_frequency(bc, ctx, ch_num, band_has_tones); + decode_tones_amplitude(bc, ctx, ch_num, band_has_tones); + decode_tones_phase(bc, ctx, ch_num, band_has_tones); } if (num_channels == 2) { @@ -1758,13 +1759,13 @@ static int decode_tones_info(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, return 0; } -int ff_atrac3p_decode_channel_unit(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, +int ff_atrac3p_decode_channel_unit(BitstreamContext *bc, Atrac3pChanUnitCtx *ctx, int num_channels, AVCodecContext *avctx) { int ret; /* parse sound header */ - ctx->num_quant_units = get_bits(gb, 5) + 1; + ctx->num_quant_units = bitstream_read(bc, 5) + 1; if (ctx->num_quant_units > 28 && ctx->num_quant_units < 32) { av_log(avctx, AV_LOG_ERROR, "Invalid number of quantization units: %d!\n", @@ -1772,10 +1773,10 @@ int ff_atrac3p_decode_channel_unit(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, return AVERROR_INVALIDDATA; } - ctx->mute_flag = get_bits1(gb); + ctx->mute_flag = bitstream_read_bit(bc); /* decode various sound parameters */ - if ((ret = decode_quant_wordlen(gb, ctx, num_channels, avctx)) < 0) + if ((ret = decode_quant_wordlen(bc, ctx, num_channels, avctx)) < 0) return ret; ctx->num_subbands = atrac3p_qu_to_subband[ctx->num_quant_units - 1] + 1; @@ -1783,32 +1784,32 @@ int ff_atrac3p_decode_channel_unit(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, ? atrac3p_qu_to_subband[ctx->used_quant_units - 1] + 1 : 0; - if ((ret = decode_scale_factors(gb, ctx, num_channels, avctx)) < 0) + if ((ret = decode_scale_factors(bc, ctx, num_channels, avctx)) < 0) return ret; - if ((ret = decode_code_table_indexes(gb, ctx, num_channels, avctx)) < 0) + if ((ret = decode_code_table_indexes(bc, ctx, num_channels, avctx)) < 0) return ret; - decode_spectrum(gb, ctx, num_channels, avctx); + decode_spectrum(bc, ctx, num_channels, avctx); if (num_channels == 2) { - get_subband_flags(gb, ctx->swap_channels, ctx->num_coded_subbands); - get_subband_flags(gb, ctx->negate_coeffs, ctx->num_coded_subbands); + get_subband_flags(bc, ctx->swap_channels, ctx->num_coded_subbands); + get_subband_flags(bc, ctx->negate_coeffs, ctx->num_coded_subbands); } - decode_window_shape(gb, ctx, num_channels); + decode_window_shape(bc, ctx, num_channels); - if ((ret = decode_gainc_data(gb, ctx, num_channels, avctx)) < 0) + if ((ret = decode_gainc_data(bc, ctx, num_channels, avctx)) < 0) return ret; - if ((ret = decode_tones_info(gb, ctx, num_channels, avctx)) < 0) + if ((ret = decode_tones_info(bc, ctx, num_channels, avctx)) < 0) return ret; /* decode global noise info */ - ctx->noise_present = get_bits1(gb); + ctx->noise_present = bitstream_read_bit(bc); if (ctx->noise_present) { - ctx->noise_level_index = get_bits(gb, 4); - ctx->noise_table_index = get_bits(gb, 4); + ctx->noise_level_index = bitstream_read(bc, 4); + ctx->noise_table_index = bitstream_read(bc, 4); } return 0; diff --git a/libavcodec/atrac3plus.h b/libavcodec/atrac3plus.h index e56c4441b6..cca399adab 100644 --- a/libavcodec/atrac3plus.h +++ b/libavcodec/atrac3plus.h @@ -31,10 +31,11 @@ #include #include "libavutil/float_dsp.h" + #include "atrac.h" +#include "bitstream.h" #include "avcodec.h" #include "fft.h" -#include "get_bits.h" /** Global unit sizes */ #define ATRAC3P_SUBBANDS 16 ///< number of PQF subbands @@ -163,13 +164,13 @@ void ff_atrac3p_init_vlcs(AVCodec *codec); /** * Decode bitstream data of a channel unit. * - * @param[in] gb the GetBit context + * @param[in] bc the Bitstream context * @param[in,out] ctx ptr to the channel unit context * @param[in] num_channels number of channels to process * @param[in] avctx ptr to the AVCodecContext * @return result code: 0 = OK, otherwise - error code */ -int ff_atrac3p_decode_channel_unit(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, +int ff_atrac3p_decode_channel_unit(BitstreamContext *bc, Atrac3pChanUnitCtx *ctx, int num_channels, AVCodecContext *avctx); /** diff --git a/libavcodec/atrac3plusdec.c b/libavcodec/atrac3plusdec.c index 4a742c159e..17774d5657 100644 --- a/libavcodec/atrac3plusdec.c +++ b/libavcodec/atrac3plusdec.c @@ -39,14 +39,15 @@ #include "libavutil/channel_layout.h" #include "libavutil/float_dsp.h" + #include "avcodec.h" -#include "get_bits.h" +#include "bitstream.h" #include "internal.h" #include "atrac.h" #include "atrac3plus.h" typedef struct ATRAC3PContext { - GetBitContext gb; + BitstreamContext bc; AVFloatDSPContext fdsp; DECLARE_ALIGNED(32, float, samples)[2][ATRAC3P_FRAME_SAMPLES]; ///< quantized MDCT spectrum @@ -334,16 +335,16 @@ static int atrac3p_decode_frame(AVCodecContext *avctx, void *data, return ret; } - if ((ret = init_get_bits8(&ctx->gb, avpkt->data, avpkt->size)) < 0) + if ((ret = bitstream_init8(&ctx->bc, avpkt->data, avpkt->size)) < 0) return ret; - if (get_bits1(&ctx->gb)) { + if (bitstream_read_bit(&ctx->bc)) { av_log(avctx, AV_LOG_ERROR, "Invalid start bit!\n"); return AVERROR_INVALIDDATA; } - while (get_bits_left(&ctx->gb) >= 2 && - (ch_unit_id = get_bits(&ctx->gb, 2)) != CH_UNIT_TERMINATOR) { + while (bitstream_bits_left(&ctx->bc) >= 2 && + (ch_unit_id = bitstream_read(&ctx->bc, 2)) != CH_UNIT_TERMINATOR) { if (ch_unit_id == CH_UNIT_EXTENSION) { avpriv_report_missing_feature(avctx, "Channel unit extension"); return AVERROR_PATCHWELCOME; @@ -358,7 +359,7 @@ static int atrac3p_decode_frame(AVCodecContext *avctx, void *data, ctx->ch_units[ch_block].unit_type = ch_unit_id; channels_to_process = ch_unit_id + 1; - if ((ret = ff_atrac3p_decode_channel_unit(&ctx->gb, + if ((ret = ff_atrac3p_decode_channel_unit(&ctx->bc, &ctx->ch_units[ch_block], channels_to_process, avctx)) < 0) From dae9b0b9c6dbe3349b6350fd02d60810660e7bf6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandra=20H=C3=A1jkov=C3=A1?= Date: Sat, 9 Apr 2016 15:51:58 +0200 Subject: [PATCH 11/35] avs: Convert to the new bitstream reader Signed-off-by: Anton Khirnov --- libavcodec/avs.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/libavcodec/avs.c b/libavcodec/avs.c index 0d127f85d1..bea01a2656 100644 --- a/libavcodec/avs.c +++ b/libavcodec/avs.c @@ -20,7 +20,7 @@ */ #include "avcodec.h" -#include "get_bits.h" +#include "bitstream.h" #include "internal.h" typedef struct AvsContext { @@ -57,7 +57,7 @@ avs_decode_frame(AVCodecContext * avctx, int i, j, x, y, stride, ret, vect_w = 3, vect_h = 3; AvsVideoSubType sub_type; AvsBlockType type; - GetBitContext change_map; + BitstreamContext change_map; if ((ret = ff_reget_buffer(avctx, p)) < 0) { av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n"); @@ -125,13 +125,13 @@ avs_decode_frame(AVCodecContext * avctx, int map_size = ((318 / vect_w + 7) / 8) * (198 / vect_h); if (buf_end - table < map_size) return AVERROR_INVALIDDATA; - init_get_bits(&change_map, table, map_size * 8); + bitstream_init(&change_map, table, map_size * 8); table += map_size; } for (y=0; y<198; y+=vect_h) { for (x=0; x<318; x+=vect_w) { - if (sub_type == AVS_I_FRAME || get_bits1(&change_map)) { + if (sub_type == AVS_I_FRAME || bitstream_read_bit(&change_map)) { if (buf_end - table < 1) return AVERROR_INVALIDDATA; vect = &buf[*table++ * (vect_w * vect_h)]; @@ -145,7 +145,7 @@ avs_decode_frame(AVCodecContext * avctx, } } if (sub_type != AVS_I_FRAME) - align_get_bits(&change_map); + bitstream_align(&change_map); } if ((ret = av_frame_ref(picture, p)) < 0) From 9a23b599431cf24ebf976a30d8c7a070ce57c3e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandra=20H=C3=A1jkov=C3=A1?= Date: Sat, 9 Apr 2016 15:59:51 +0200 Subject: [PATCH 12/35] bink: Convert to the new bitstream reader Signed-off-by: Anton Khirnov --- libavcodec/bink.c | 264 ++++++++++++++++++++++------------------------ 1 file changed, 128 insertions(+), 136 deletions(-) diff --git a/libavcodec/bink.c b/libavcodec/bink.c index 74336974d0..74db80a2be 100644 --- a/libavcodec/bink.c +++ b/libavcodec/bink.c @@ -28,8 +28,8 @@ #include "avcodec.h" #include "binkdata.h" #include "binkdsp.h" +#include "bitstream.h" #include "blockdsp.h" -#include "get_bits.h" #include "hpeldsp.h" #include "internal.h" #include "mathops.h" @@ -93,8 +93,9 @@ typedef struct Tree { uint8_t syms[16]; ///< leaf value to symbol mapping } Tree; -#define GET_HUFF(gb, tree) (tree).syms[get_vlc2(gb, bink_trees[(tree).vlc_num].table,\ - bink_trees[(tree).vlc_num].bits, 1)] +#define GET_HUFF(bc, tree) \ + (tree).syms[bitstream_read_vlc(bc, bink_trees[(tree).vlc_num].table, \ + bink_trees[(tree).vlc_num].bits, 1)] /** * data structure used for decoding single Bink data type @@ -204,18 +205,18 @@ static av_cold void free_bundles(BinkContext *c) /** * Merge two consequent lists of equal size depending on bits read. * - * @param gb context for reading bits + * @param bc context for reading bits * @param dst buffer where merged list will be written to * @param src pointer to the head of the first list (the second lists starts at src+size) * @param size input lists size */ -static void merge(GetBitContext *gb, uint8_t *dst, uint8_t *src, int size) +static void merge(BitstreamContext *bc, uint8_t *dst, uint8_t *src, int size) { uint8_t *src2 = src + size; int size2 = size; do { - if (!get_bits1(gb)) { + if (!bitstream_read_bit(bc)) { *dst++ = *src++; size--; } else { @@ -233,37 +234,37 @@ static void merge(GetBitContext *gb, uint8_t *dst, uint8_t *src, int size) /** * Read information about Huffman tree used to decode data. * - * @param gb context for reading bits + * @param bc context for reading bits * @param tree pointer for storing tree data */ -static void read_tree(GetBitContext *gb, Tree *tree) +static void read_tree(BitstreamContext *bc, Tree *tree) { uint8_t tmp1[16] = { 0 }, tmp2[16], *in = tmp1, *out = tmp2; int i, t, len; - tree->vlc_num = get_bits(gb, 4); + tree->vlc_num = bitstream_read(bc, 4); if (!tree->vlc_num) { for (i = 0; i < 16; i++) tree->syms[i] = i; return; } - if (get_bits1(gb)) { - len = get_bits(gb, 3); + if (bitstream_read_bit(bc)) { + len = bitstream_read(bc, 3); for (i = 0; i <= len; i++) { - tree->syms[i] = get_bits(gb, 4); + tree->syms[i] = bitstream_read(bc, 4); tmp1[tree->syms[i]] = 1; } for (i = 0; i < 16 && len < 16 - 1; i++) if (!tmp1[i]) tree->syms[++len] = i; } else { - len = get_bits(gb, 2); + len = bitstream_read(bc, 2); for (i = 0; i < 16; i++) in[i] = i; for (i = 0; i <= len; i++) { int size = 1 << i; for (t = 0; t < 16; t += size << 1) - merge(gb, out + t, in + t, size); + merge(bc, out + t, in + t, size); FFSWAP(uint8_t*, in, out); } memcpy(tree->syms, in, 16); @@ -273,21 +274,21 @@ static void read_tree(GetBitContext *gb, Tree *tree) /** * Prepare bundle for decoding data. * - * @param gb context for reading bits + * @param bc context for reading bits * @param c decoder context * @param bundle_num number of the bundle to initialize */ -static void read_bundle(GetBitContext *gb, BinkContext *c, int bundle_num) +static void read_bundle(BitstreamContext *bc, BinkContext *c, int bundle_num) { int i; if (bundle_num == BINK_SRC_COLORS) { for (i = 0; i < 16; i++) - read_tree(gb, &c->col_high[i]); + read_tree(bc, &c->col_high[i]); c->col_lastval = 0; } if (bundle_num != BINK_SRC_INTRA_DC && bundle_num != BINK_SRC_INTER_DC) - read_tree(gb, &c->bundle[bundle_num].tree); + read_tree(bc, &c->bundle[bundle_num].tree); c->bundle[bundle_num].cur_dec = c->bundle[bundle_num].cur_ptr = c->bundle[bundle_num].data; } @@ -295,66 +296,64 @@ static void read_bundle(GetBitContext *gb, BinkContext *c, int bundle_num) /** * common check before starting decoding bundle data * - * @param gb context for reading bits + * @param bc context for reading bits * @param b bundle * @param t variable where number of elements to decode will be stored */ -#define CHECK_READ_VAL(gb, b, t) \ +#define CHECK_READ_VAL(bc, b, t) \ if (!b->cur_dec || (b->cur_dec > b->cur_ptr)) \ return 0; \ - t = get_bits(gb, b->len); \ + t = bitstream_read(bc, b->len); \ if (!t) { \ b->cur_dec = NULL; \ return 0; \ } \ -static int read_runs(AVCodecContext *avctx, GetBitContext *gb, Bundle *b) +static int read_runs(AVCodecContext *avctx, BitstreamContext *bc, Bundle *b) { int t, v; const uint8_t *dec_end; - CHECK_READ_VAL(gb, b, t); + CHECK_READ_VAL(bc, b, t); dec_end = b->cur_dec + t; if (dec_end > b->data_end) { av_log(avctx, AV_LOG_ERROR, "Run value went out of bounds\n"); return AVERROR_INVALIDDATA; } - if (get_bits1(gb)) { - v = get_bits(gb, 4); + if (bitstream_read_bit(bc)) { + v = bitstream_read(bc, 4); memset(b->cur_dec, v, t); b->cur_dec += t; } else { while (b->cur_dec < dec_end) - *b->cur_dec++ = GET_HUFF(gb, b->tree); + *b->cur_dec++ = GET_HUFF(bc, b->tree); } return 0; } -static int read_motion_values(AVCodecContext *avctx, GetBitContext *gb, Bundle *b) +static int read_motion_values(AVCodecContext *avctx, BitstreamContext *bc, Bundle *b) { - int t, sign, v; + int t, v; const uint8_t *dec_end; - CHECK_READ_VAL(gb, b, t); + CHECK_READ_VAL(bc, b, t); dec_end = b->cur_dec + t; if (dec_end > b->data_end) { av_log(avctx, AV_LOG_ERROR, "Too many motion values\n"); return AVERROR_INVALIDDATA; } - if (get_bits1(gb)) { - v = get_bits(gb, 4); + if (bitstream_read_bit(bc)) { + v = bitstream_read(bc, 4); if (v) { - sign = -get_bits1(gb); - v = (v ^ sign) - sign; + v = bitstream_apply_sign(bc, v); } memset(b->cur_dec, v, t); b->cur_dec += t; } else { while (b->cur_dec < dec_end) { - v = GET_HUFF(gb, b->tree); + v = GET_HUFF(bc, b->tree); if (v) { - sign = -get_bits1(gb); - v = (v ^ sign) - sign; + v = bitstream_apply_sign(bc, v); } *b->cur_dec++ = v; } @@ -364,25 +363,25 @@ static int read_motion_values(AVCodecContext *avctx, GetBitContext *gb, Bundle * static const uint8_t bink_rlelens[4] = { 4, 8, 12, 32 }; -static int read_block_types(AVCodecContext *avctx, GetBitContext *gb, Bundle *b) +static int read_block_types(AVCodecContext *avctx, BitstreamContext *bc, Bundle *b) { int t, v; int last = 0; const uint8_t *dec_end; - CHECK_READ_VAL(gb, b, t); + CHECK_READ_VAL(bc, b, t); dec_end = b->cur_dec + t; if (dec_end > b->data_end) { av_log(avctx, AV_LOG_ERROR, "Too many block type values\n"); return AVERROR_INVALIDDATA; } - if (get_bits1(gb)) { - v = get_bits(gb, 4); + if (bitstream_read_bit(bc)) { + v = bitstream_read(bc, 4); memset(b->cur_dec, v, t); b->cur_dec += t; } else { while (b->cur_dec < dec_end) { - v = GET_HUFF(gb, b->tree); + v = GET_HUFF(bc, b->tree); if (v < 12) { last = v; *b->cur_dec++ = v; @@ -399,40 +398,40 @@ static int read_block_types(AVCodecContext *avctx, GetBitContext *gb, Bundle *b) return 0; } -static int read_patterns(AVCodecContext *avctx, GetBitContext *gb, Bundle *b) +static int read_patterns(AVCodecContext *avctx, BitstreamContext *bc, Bundle *b) { int t, v; const uint8_t *dec_end; - CHECK_READ_VAL(gb, b, t); + CHECK_READ_VAL(bc, b, t); dec_end = b->cur_dec + t; if (dec_end > b->data_end) { av_log(avctx, AV_LOG_ERROR, "Too many pattern values\n"); return AVERROR_INVALIDDATA; } while (b->cur_dec < dec_end) { - v = GET_HUFF(gb, b->tree); - v |= GET_HUFF(gb, b->tree) << 4; + v = GET_HUFF(bc, b->tree); + v |= GET_HUFF(bc, b->tree) << 4; *b->cur_dec++ = v; } return 0; } -static int read_colors(GetBitContext *gb, Bundle *b, BinkContext *c) +static int read_colors(BitstreamContext *bc, Bundle *b, BinkContext *c) { int t, sign, v; const uint8_t *dec_end; - CHECK_READ_VAL(gb, b, t); + CHECK_READ_VAL(bc, b, t); dec_end = b->cur_dec + t; if (dec_end > b->data_end) { av_log(c->avctx, AV_LOG_ERROR, "Too many color values\n"); return AVERROR_INVALIDDATA; } - if (get_bits1(gb)) { - c->col_lastval = GET_HUFF(gb, c->col_high[c->col_lastval]); - v = GET_HUFF(gb, b->tree); + if (bitstream_read_bit(bc)) { + c->col_lastval = GET_HUFF(bc, c->col_high[c->col_lastval]); + v = GET_HUFF(bc, b->tree); v = (c->col_lastval << 4) | v; if (c->version < 'i') { sign = ((int8_t) v) >> 7; @@ -443,8 +442,8 @@ static int read_colors(GetBitContext *gb, Bundle *b, BinkContext *c) b->cur_dec += t; } else { while (b->cur_dec < dec_end) { - c->col_lastval = GET_HUFF(gb, c->col_high[c->col_lastval]); - v = GET_HUFF(gb, b->tree); + c->col_lastval = GET_HUFF(bc, c->col_high[c->col_lastval]); + v = GET_HUFF(bc, b->tree); v = (c->col_lastval << 4) | v; if (c->version < 'i') { sign = ((int8_t) v) >> 7; @@ -460,18 +459,17 @@ static int read_colors(GetBitContext *gb, Bundle *b, BinkContext *c) /** number of bits used to store first DC value in bundle */ #define DC_START_BITS 11 -static int read_dcs(AVCodecContext *avctx, GetBitContext *gb, Bundle *b, +static int read_dcs(AVCodecContext *avctx, BitstreamContext *bc, Bundle *b, int start_bits, int has_sign) { - int i, j, len, len2, bsize, sign, v, v2; + int i, j, len, len2, bsize, v, v2; int16_t *dst = (int16_t*)b->cur_dec; int16_t *dst_end = (int16_t*)b->data_end; - CHECK_READ_VAL(gb, b, len); - v = get_bits(gb, start_bits - has_sign); + CHECK_READ_VAL(bc, b, len); + v = bitstream_read(bc, start_bits - has_sign); if (v && has_sign) { - sign = -get_bits1(gb); - v = (v ^ sign) - sign; + v = bitstream_apply_sign(bc, v); } if (dst_end - dst < 1) return AVERROR_INVALIDDATA; @@ -481,13 +479,12 @@ static int read_dcs(AVCodecContext *avctx, GetBitContext *gb, Bundle *b, len2 = FFMIN(len - i, 8); if (dst_end - dst < len2) return AVERROR_INVALIDDATA; - bsize = get_bits(gb, 4); + bsize = bitstream_read(bc, 4); if (bsize) { for (j = 0; j < len2; j++) { - v2 = get_bits(gb, bsize); + v2 = bitstream_read(bc, bsize); if (v2) { - sign = -get_bits1(gb); - v2 = (v2 ^ sign) - sign; + v2 = bitstream_apply_sign(bc, v2); } v += v2; *dst++ = v; @@ -539,7 +536,7 @@ static av_cold void binkb_init_bundles(BinkContext *c) binkb_init_bundle(c, i); } -static int binkb_read_bundle(BinkContext *c, GetBitContext *gb, int bundle_num) +static int binkb_read_bundle(BinkContext *c, BitstreamContext *bc, int bundle_num) { const int bits = binkb_bundle_sizes[bundle_num]; const int mask = 1 << (bits - 1); @@ -547,26 +544,26 @@ static int binkb_read_bundle(BinkContext *c, GetBitContext *gb, int bundle_num) Bundle *b = &c->bundle[bundle_num]; int i, len; - CHECK_READ_VAL(gb, b, len); + CHECK_READ_VAL(bc, b, len); if (b->data_end - b->cur_dec < len * (1 + (bits > 8))) return AVERROR_INVALIDDATA; if (bits <= 8) { if (!issigned) { for (i = 0; i < len; i++) - *b->cur_dec++ = get_bits(gb, bits); + *b->cur_dec++ = bitstream_read(bc, bits); } else { for (i = 0; i < len; i++) - *b->cur_dec++ = get_bits(gb, bits) - mask; + *b->cur_dec++ = bitstream_read(bc, bits) - mask; } } else { int16_t *dst = (int16_t*)b->cur_dec; if (!issigned) { for (i = 0; i < len; i++) - *dst++ = get_bits(gb, bits); + *dst++ = bitstream_read(bc, bits); } else { for (i = 0; i < len; i++) - *dst++ = get_bits(gb, bits) - mask; + *dst++ = bitstream_read(bc, bits) - mask; } b->cur_dec = (uint8_t*)dst; } @@ -590,18 +587,19 @@ static inline int binkb_get_value(BinkContext *c, int bundle_num) /** * Read 8x8 block of DCT coefficients. * - * @param gb context for reading bits + * @param bc context for reading bits * @param block place for storing coefficients * @param scan scan order table * @param quant_matrices quantization matrices * @return 0 for success, negative value in other cases */ -static int read_dct_coeffs(GetBitContext *gb, int32_t block[64], const uint8_t *scan, +static int read_dct_coeffs(BitstreamContext *bc, int32_t block[64], + const uint8_t *scan, const int32_t quant_matrices[16][64], int q) { int coef_list[128]; int mode_list[128]; - int i, t, bits, ccoef, mode, sign; + int i, t, bits, ccoef, mode; int list_start = 64, list_end = 64, list_pos; int coef_count = 0; int coef_idx[64]; @@ -615,10 +613,10 @@ static int read_dct_coeffs(GetBitContext *gb, int32_t block[64], const uint8_t * coef_list[list_end] = 2; mode_list[list_end++] = 3; coef_list[list_end] = 3; mode_list[list_end++] = 3; - for (bits = get_bits(gb, 4) - 1; bits >= 0; bits--) { + for (bits = bitstream_read(bc, 4) - 1; bits >= 0; bits--) { list_pos = list_start; while (list_pos < list_end) { - if (!(mode_list[list_pos] | coef_list[list_pos]) || !get_bits1(gb)) { + if (!(mode_list[list_pos] | coef_list[list_pos]) || !bitstream_read_bit(bc)) { list_pos++; continue; } @@ -634,16 +632,15 @@ static int read_dct_coeffs(GetBitContext *gb, int32_t block[64], const uint8_t * mode_list[list_pos++] = 0; } for (i = 0; i < 4; i++, ccoef++) { - if (get_bits1(gb)) { + if (bitstream_read_bit(bc)) { coef_list[--list_start] = ccoef; mode_list[ list_start] = 3; } else { if (!bits) { - t = 1 - (get_bits1(gb) << 1); + t = 1 - (bitstream_read_bit(bc) << 1); } else { - t = get_bits(gb, bits) | 1 << bits; - sign = -get_bits1(gb); - t = (t ^ sign) - sign; + t = bitstream_read(bc, bits) | 1 << bits; + t = bitstream_apply_sign(bc, t); } block[scan[ccoef]] = t; coef_idx[coef_count++] = ccoef; @@ -660,11 +657,10 @@ static int read_dct_coeffs(GetBitContext *gb, int32_t block[64], const uint8_t * break; case 3: if (!bits) { - t = 1 - (get_bits1(gb) << 1); + t = 1 - (bitstream_read_bit(bc) << 1); } else { - t = get_bits(gb, bits) | 1 << bits; - sign = -get_bits1(gb); - t = (t ^ sign) - sign; + t = bitstream_read(bc, bits) | 1 << bits; + t = bitstream_apply_sign(bc, t); } block[scan[ccoef]] = t; coef_idx[coef_count++] = ccoef; @@ -676,7 +672,7 @@ static int read_dct_coeffs(GetBitContext *gb, int32_t block[64], const uint8_t * } if (q == -1) { - quant_idx = get_bits(gb, 4); + quant_idx = bitstream_read(bc, 4); } else { quant_idx = q; } @@ -698,16 +694,16 @@ static int read_dct_coeffs(GetBitContext *gb, int32_t block[64], const uint8_t * /** * Read 8x8 block with residue after motion compensation. * - * @param gb context for reading bits + * @param bc context for reading bits * @param block place to store read data * @param masks_count number of masks to decode * @return 0 on success, negative value in other cases */ -static int read_residue(GetBitContext *gb, int16_t block[64], int masks_count) +static int read_residue(BitstreamContext *bc, int16_t block[64], int masks_count) { int coef_list[128]; int mode_list[128]; - int i, sign, mask, ccoef, mode; + int i, mask, ccoef, mode; int list_start = 64, list_end = 64, list_pos; int nz_coeff[64]; int nz_coeff_count = 0; @@ -717,9 +713,9 @@ static int read_residue(GetBitContext *gb, int16_t block[64], int masks_count) coef_list[list_end] = 44; mode_list[list_end++] = 0; coef_list[list_end] = 0; mode_list[list_end++] = 2; - for (mask = 1 << get_bits(gb, 3); mask; mask >>= 1) { + for (mask = 1 << bitstream_read(bc, 3); mask; mask >>= 1) { for (i = 0; i < nz_coeff_count; i++) { - if (!get_bits1(gb)) + if (!bitstream_read_bit(bc)) continue; if (block[nz_coeff[i]] < 0) block[nz_coeff[i]] -= mask; @@ -731,7 +727,7 @@ static int read_residue(GetBitContext *gb, int16_t block[64], int masks_count) } list_pos = list_start; while (list_pos < list_end) { - if (!(coef_list[list_pos] | mode_list[list_pos]) || !get_bits1(gb)) { + if (!(coef_list[list_pos] | mode_list[list_pos]) || !bitstream_read_bit(bc)) { list_pos++; continue; } @@ -747,13 +743,12 @@ static int read_residue(GetBitContext *gb, int16_t block[64], int masks_count) mode_list[list_pos++] = 0; } for (i = 0; i < 4; i++, ccoef++) { - if (get_bits1(gb)) { + if (bitstream_read_bit(bc)) { coef_list[--list_start] = ccoef; mode_list[ list_start] = 3; } else { nz_coeff[nz_coeff_count++] = bink_scan[ccoef]; - sign = -get_bits1(gb); - block[bink_scan[ccoef]] = (mask ^ sign) - sign; + block[bink_scan[ccoef]] = bitstream_apply_sign(bc, mask); masks_count--; if (masks_count < 0) return 0; @@ -770,8 +765,7 @@ static int read_residue(GetBitContext *gb, int16_t block[64], int masks_count) break; case 3: nz_coeff[nz_coeff_count++] = bink_scan[ccoef]; - sign = -get_bits1(gb); - block[bink_scan[ccoef]] = (mask ^ sign) - sign; + block[bink_scan[ccoef]] = bitstream_apply_sign(bc, mask); coef_list[list_pos] = 0; mode_list[list_pos++] = 0; masks_count--; @@ -798,7 +792,7 @@ static inline void put_pixels8x8_overlapped(uint8_t *dst, uint8_t *src, int stri memcpy(dst + i*stride, tmp + i*8, 8); } -static int binkb_decode_plane(BinkContext *c, AVFrame *frame, GetBitContext *gb, +static int binkb_decode_plane(BinkContext *c, AVFrame *frame, BitstreamContext *bc, int plane_idx, int is_key, int is_chroma) { int blk, ret; @@ -826,7 +820,7 @@ static int binkb_decode_plane(BinkContext *c, AVFrame *frame, GetBitContext *gb, for (by = 0; by < bh; by++) { for (i = 0; i < BINKB_NB_SRC; i++) { - if ((ret = binkb_read_bundle(c, gb, i)) < 0) + if ((ret = binkb_read_bundle(c, bc, i)) < 0) return ret; } @@ -837,13 +831,11 @@ static int binkb_decode_plane(BinkContext *c, AVFrame *frame, GetBitContext *gb, case 0: break; case 1: - scan = bink_patterns[get_bits(gb, 4)]; + scan = bink_patterns[bitstream_read(bc, 4)]; i = 0; do { - int mode, run; - - mode = get_bits1(gb); - run = get_bits(gb, binkb_runbits[i]) + 1; + int mode = bitstream_read_bit(bc); + int run = bitstream_read(bc, binkb_runbits[i]) + 1; i += run; if (i > 64) { @@ -866,7 +858,7 @@ static int binkb_decode_plane(BinkContext *c, AVFrame *frame, GetBitContext *gb, memset(dctblock, 0, sizeof(*dctblock) * 64); dctblock[0] = binkb_get_value(c, BINKB_SRC_INTRA_DC); qp = binkb_get_value(c, BINKB_SRC_INTRA_Q); - read_dct_coeffs(gb, dctblock, bink_scan, binkb_intra_quant, qp); + read_dct_coeffs(bc, dctblock, bink_scan, binkb_intra_quant, qp); c->binkdsp.idct_put(dst, stride, dctblock); break; case 3: @@ -882,7 +874,7 @@ static int binkb_decode_plane(BinkContext *c, AVFrame *frame, GetBitContext *gb, } c->bdsp.clear_block(block); v = binkb_get_value(c, BINKB_SRC_INTER_COEFS); - read_residue(gb, block, v); + read_residue(bc, block, v); c->binkdsp.add_pixels8(dst, block, stride); break; case 4: @@ -899,7 +891,7 @@ static int binkb_decode_plane(BinkContext *c, AVFrame *frame, GetBitContext *gb, memset(dctblock, 0, sizeof(*dctblock) * 64); dctblock[0] = binkb_get_value(c, BINKB_SRC_INTER_DC); qp = binkb_get_value(c, BINKB_SRC_INTER_Q); - read_dct_coeffs(gb, dctblock, bink_scan, binkb_inter_quant, qp); + read_dct_coeffs(bc, dctblock, bink_scan, binkb_inter_quant, qp); c->binkdsp.idct_add(dst, stride, dctblock); break; case 5: @@ -938,8 +930,8 @@ static int binkb_decode_plane(BinkContext *c, AVFrame *frame, GetBitContext *gb, } } } - if (get_bits_count(gb) & 0x1F) //next plane data starts at 32-bit boundary - skip_bits_long(gb, 32 - (get_bits_count(gb) & 0x1F)); + if (bitstream_tell(bc) & 0x1F) // next plane data starts at 32-bit boundary + bitstream_skip(bc, 32 - (bitstream_tell(bc) & 0x1F)); return 0; } @@ -962,7 +954,7 @@ static int bink_put_pixels(BinkContext *c, return 0; } -static int bink_decode_plane(BinkContext *c, AVFrame *frame, GetBitContext *gb, +static int bink_decode_plane(BinkContext *c, AVFrame *frame, BitstreamContext *bc, int plane_idx, int is_chroma) { int blk, ret; @@ -982,7 +974,7 @@ static int bink_decode_plane(BinkContext *c, AVFrame *frame, GetBitContext *gb, init_lengths(c, FFMAX(width, 8), bw); for (i = 0; i < BINK_NB_SRC; i++) - read_bundle(gb, c, i); + read_bundle(bc, c, i); ref_start = c->last->data[plane_idx] ? c->last->data[plane_idx] : frame->data[plane_idx]; @@ -993,23 +985,23 @@ static int bink_decode_plane(BinkContext *c, AVFrame *frame, GetBitContext *gb, coordmap[i] = (i & 7) + (i >> 3) * stride; for (by = 0; by < bh; by++) { - if ((ret = read_block_types(c->avctx, gb, &c->bundle[BINK_SRC_BLOCK_TYPES])) < 0) + if ((ret = read_block_types(c->avctx, bc, &c->bundle[BINK_SRC_BLOCK_TYPES])) < 0) return ret; - if ((ret = read_block_types(c->avctx, gb, &c->bundle[BINK_SRC_SUB_BLOCK_TYPES])) < 0) + if ((ret = read_block_types(c->avctx, bc, &c->bundle[BINK_SRC_SUB_BLOCK_TYPES])) < 0) return ret; - if ((ret = read_colors(gb, &c->bundle[BINK_SRC_COLORS], c)) < 0) + if ((ret = read_colors(bc, &c->bundle[BINK_SRC_COLORS], c)) < 0) return ret; - if ((ret = read_patterns(c->avctx, gb, &c->bundle[BINK_SRC_PATTERN])) < 0) + if ((ret = read_patterns(c->avctx, bc, &c->bundle[BINK_SRC_PATTERN])) < 0) return ret; - if ((ret = read_motion_values(c->avctx, gb, &c->bundle[BINK_SRC_X_OFF])) < 0) + if ((ret = read_motion_values(c->avctx, bc, &c->bundle[BINK_SRC_X_OFF])) < 0) return ret; - if ((ret = read_motion_values(c->avctx, gb, &c->bundle[BINK_SRC_Y_OFF])) < 0) + if ((ret = read_motion_values(c->avctx, bc, &c->bundle[BINK_SRC_Y_OFF])) < 0) return ret; - if ((ret = read_dcs(c->avctx, gb, &c->bundle[BINK_SRC_INTRA_DC], DC_START_BITS, 0)) < 0) + if ((ret = read_dcs(c->avctx, bc, &c->bundle[BINK_SRC_INTRA_DC], DC_START_BITS, 0)) < 0) return ret; - if ((ret = read_dcs(c->avctx, gb, &c->bundle[BINK_SRC_INTER_DC], DC_START_BITS, 1)) < 0) + if ((ret = read_dcs(c->avctx, bc, &c->bundle[BINK_SRC_INTER_DC], DC_START_BITS, 1)) < 0) return ret; - if ((ret = read_runs(c->avctx, gb, &c->bundle[BINK_SRC_RUN])) < 0) + if ((ret = read_runs(c->avctx, bc, &c->bundle[BINK_SRC_RUN])) < 0) return ret; if (by == bh) @@ -1034,7 +1026,7 @@ static int bink_decode_plane(BinkContext *c, AVFrame *frame, GetBitContext *gb, blk = get_value(c, BINK_SRC_SUB_BLOCK_TYPES); switch (blk) { case RUN_BLOCK: - scan = bink_patterns[get_bits(gb, 4)]; + scan = bink_patterns[bitstream_read(bc, 4)]; i = 0; do { int run = get_value(c, BINK_SRC_RUN) + 1; @@ -1044,7 +1036,7 @@ static int bink_decode_plane(BinkContext *c, AVFrame *frame, GetBitContext *gb, av_log(c->avctx, AV_LOG_ERROR, "Run went out of bounds\n"); return AVERROR_INVALIDDATA; } - if (get_bits1(gb)) { + if (bitstream_read_bit(bc)) { v = get_value(c, BINK_SRC_COLORS); for (j = 0; j < run; j++) ublock[*scan++] = v; @@ -1059,7 +1051,7 @@ static int bink_decode_plane(BinkContext *c, AVFrame *frame, GetBitContext *gb, case INTRA_BLOCK: memset(dctblock, 0, sizeof(*dctblock) * 64); dctblock[0] = get_value(c, BINK_SRC_INTRA_DC); - read_dct_coeffs(gb, dctblock, bink_scan, bink_intra_quant, -1); + read_dct_coeffs(bc, dctblock, bink_scan, bink_intra_quant, -1); c->binkdsp.idct_put(ublock, 8, dctblock); break; case FILL_BLOCK: @@ -1097,7 +1089,7 @@ static int bink_decode_plane(BinkContext *c, AVFrame *frame, GetBitContext *gb, return ret; break; case RUN_BLOCK: - scan = bink_patterns[get_bits(gb, 4)]; + scan = bink_patterns[bitstream_read(bc, 4)]; i = 0; do { int run = get_value(c, BINK_SRC_RUN) + 1; @@ -1107,7 +1099,7 @@ static int bink_decode_plane(BinkContext *c, AVFrame *frame, GetBitContext *gb, av_log(c->avctx, AV_LOG_ERROR, "Run went out of bounds\n"); return AVERROR_INVALIDDATA; } - if (get_bits1(gb)) { + if (bitstream_read_bit(bc)) { v = get_value(c, BINK_SRC_COLORS); for (j = 0; j < run; j++) dst[coordmap[*scan++]] = v; @@ -1125,14 +1117,14 @@ static int bink_decode_plane(BinkContext *c, AVFrame *frame, GetBitContext *gb, if (ret < 0) return ret; c->bdsp.clear_block(block); - v = get_bits(gb, 7); - read_residue(gb, block, v); + v = bitstream_read(bc, 7); + read_residue(bc, block, v); c->binkdsp.add_pixels8(dst, block, stride); break; case INTRA_BLOCK: memset(dctblock, 0, sizeof(*dctblock) * 64); dctblock[0] = get_value(c, BINK_SRC_INTRA_DC); - read_dct_coeffs(gb, dctblock, bink_scan, bink_intra_quant, -1); + read_dct_coeffs(bc, dctblock, bink_scan, bink_intra_quant, -1); c->binkdsp.idct_put(dst, stride, dctblock); break; case FILL_BLOCK: @@ -1146,7 +1138,7 @@ static int bink_decode_plane(BinkContext *c, AVFrame *frame, GetBitContext *gb, return ret; memset(dctblock, 0, sizeof(*dctblock) * 64); dctblock[0] = get_value(c, BINK_SRC_INTER_DC); - read_dct_coeffs(gb, dctblock, bink_scan, bink_inter_quant, -1); + read_dct_coeffs(bc, dctblock, bink_scan, bink_inter_quant, -1); c->binkdsp.idct_add(dst, stride, dctblock); break; case PATTERN_BLOCK: @@ -1169,8 +1161,8 @@ static int bink_decode_plane(BinkContext *c, AVFrame *frame, GetBitContext *gb, } } } - if (get_bits_count(gb) & 0x1F) //next plane data starts at 32-bit boundary - skip_bits_long(gb, 32 - (get_bits_count(gb) & 0x1F)); + if (bitstream_tell(bc) & 0x1F) // next plane data starts at 32-bit boundary + bitstream_skip(bc, 32 - (bitstream_tell(bc) & 0x1F)); return 0; } @@ -1179,7 +1171,7 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPac { BinkContext * const c = avctx->priv_data; AVFrame *frame = data; - GetBitContext gb; + BitstreamContext bc; int plane, plane_idx, ret; int bits_count = pkt->size << 3; @@ -1197,28 +1189,28 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPac return ret; } - init_get_bits(&gb, pkt->data, bits_count); + bitstream_init(&bc, pkt->data, bits_count); if (c->has_alpha) { if (c->version >= 'i') - skip_bits_long(&gb, 32); - if ((ret = bink_decode_plane(c, frame, &gb, 3, 0)) < 0) + bitstream_skip(&bc, 32); + if ((ret = bink_decode_plane(c, frame, &bc, 3, 0)) < 0) return ret; } if (c->version >= 'i') - skip_bits_long(&gb, 32); + bitstream_skip(&bc, 32); for (plane = 0; plane < 3; plane++) { plane_idx = (!plane || !c->swap_planes) ? plane : (plane ^ 3); if (c->version > 'b') { - if ((ret = bink_decode_plane(c, frame, &gb, plane_idx, !!plane)) < 0) + if ((ret = bink_decode_plane(c, frame, &bc, plane_idx, !!plane)) < 0) return ret; } else { - if ((ret = binkb_decode_plane(c, frame, &gb, plane_idx, + if ((ret = binkb_decode_plane(c, frame, &bc, plane_idx, !avctx->frame_number, !!plane)) < 0) return ret; } - if (get_bits_count(&gb) >= bits_count) + if (bitstream_tell(&bc) >= bits_count) break; } emms_c(); From 0977a7c2f6302508e5180f0f82139c8c8cf4a131 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandra=20H=C3=A1jkov=C3=A1?= Date: Sat, 9 Apr 2016 16:11:42 +0200 Subject: [PATCH 13/35] binkaudio: Convert to the new bitstream reader Signed-off-by: Anton Khirnov --- libavcodec/binkaudio.c | 59 +++++++++++++++++++++--------------------- 1 file changed, 30 insertions(+), 29 deletions(-) diff --git a/libavcodec/binkaudio.c b/libavcodec/binkaudio.c index 2638eb2b04..cf61135529 100644 --- a/libavcodec/binkaudio.c +++ b/libavcodec/binkaudio.c @@ -33,8 +33,8 @@ #define BITSTREAM_READER_LE #include "avcodec.h" +#include "bitstream.h" #include "dct.h" -#include "get_bits.h" #include "internal.h" #include "rdft.h" #include "wma_freqs.h" @@ -45,7 +45,7 @@ static float quant_table[96]; #define BINK_BLOCK_MAX_SIZE (MAX_CHANNELS << 11) typedef struct BinkAudioContext { - GetBitContext gb; + BitstreamContext bc; int version_b; ///< Bink version 'b' int first; int channels; @@ -143,11 +143,11 @@ static av_cold int decode_init(AVCodecContext *avctx) return 0; } -static float get_float(GetBitContext *gb) +static float get_float(BitstreamContext *bc) { - int power = get_bits(gb, 5); - float f = ldexpf(get_bits_long(gb, 23), power - 23); - if (get_bits1(gb)) + int power = bitstream_read(bc, 5); + float f = ldexpf(bitstream_read(bc, 23), power - 23); + if (bitstream_read_bit(bc)) f = -f; return f; } @@ -166,30 +166,30 @@ static int decode_block(BinkAudioContext *s, float **out, int use_dct) int ch, i, j, k; float q, quant[25]; int width, coeff; - GetBitContext *gb = &s->gb; + BitstreamContext *bc = &s->bc; if (use_dct) - skip_bits(gb, 2); + bitstream_skip(bc, 2); for (ch = 0; ch < s->channels; ch++) { FFTSample *coeffs = out[ch]; if (s->version_b) { - if (get_bits_left(gb) < 64) + if (bitstream_bits_left(bc) < 64) return AVERROR_INVALIDDATA; - coeffs[0] = av_int2float(get_bits_long(gb, 32)) * s->root; - coeffs[1] = av_int2float(get_bits_long(gb, 32)) * s->root; + coeffs[0] = av_int2float(bitstream_read(bc, 32)) * s->root; + coeffs[1] = av_int2float(bitstream_read(bc, 32)) * s->root; } else { - if (get_bits_left(gb) < 58) + if (bitstream_bits_left(bc) < 58) return AVERROR_INVALIDDATA; - coeffs[0] = get_float(gb) * s->root; - coeffs[1] = get_float(gb) * s->root; + coeffs[0] = get_float(bc) * s->root; + coeffs[1] = get_float(bc) * s->root; } - if (get_bits_left(gb) < s->num_bands * 8) + if (bitstream_bits_left(bc) < s->num_bands * 8) return AVERROR_INVALIDDATA; for (i = 0; i < s->num_bands; i++) { - int value = get_bits(gb, 8); + int value = bitstream_read(bc, 8); quant[i] = quant_table[FFMIN(value, 95)]; } @@ -202,9 +202,9 @@ static int decode_block(BinkAudioContext *s, float **out, int use_dct) if (s->version_b) { j = i + 16; } else { - int v = get_bits1(gb); + int v = bitstream_read_bit(bc); if (v) { - v = get_bits(gb, 4); + v = bitstream_read(bc, 4); j = i + rle_length_tab[v] * 8; } else { j = i + 8; @@ -213,7 +213,7 @@ static int decode_block(BinkAudioContext *s, float **out, int use_dct) j = FFMIN(j, s->frame_len); - width = get_bits(gb, 4); + width = bitstream_read(bc, 4); if (width == 0) { memset(coeffs + i, 0, (j - i) * sizeof(*coeffs)); i = j; @@ -223,10 +223,10 @@ static int decode_block(BinkAudioContext *s, float **out, int use_dct) while (i < j) { if (s->bands[k] == i) q = quant[k++]; - coeff = get_bits(gb, width); + coeff = bitstream_read(bc, width); if (coeff) { int v; - v = get_bits1(gb); + v = bitstream_read_bit(bc); if (v) coeffs[i] = -q * coeff; else @@ -278,10 +278,11 @@ static av_cold int decode_end(AVCodecContext *avctx) return 0; } -static void get_bits_align32(GetBitContext *s) +static void get_bits_align32(BitstreamContext *s) { - int n = (-get_bits_count(s)) & 31; - if (n) skip_bits(s, n); + int n = (-bitstream_tell(s)) & 31; + if (n) + bitstream_skip(s, n); } static int decode_frame(AVCodecContext *avctx, void *data, @@ -289,10 +290,10 @@ static int decode_frame(AVCodecContext *avctx, void *data, { BinkAudioContext *s = avctx->priv_data; AVFrame *frame = data; - GetBitContext *gb = &s->gb; + BitstreamContext *bc = &s->bc; int ret, consumed = 0; - if (!get_bits_left(gb)) { + if (!bitstream_bits_left(bc)) { uint8_t *buf; /* handle end-of-stream */ if (!avpkt->size) { @@ -308,11 +309,11 @@ static int decode_frame(AVCodecContext *avctx, void *data, return AVERROR(ENOMEM); s->packet_buffer = buf; memcpy(s->packet_buffer, avpkt->data, avpkt->size); - init_get_bits(gb, s->packet_buffer, avpkt->size * 8); + bitstream_init(bc, s->packet_buffer, avpkt->size * 8); consumed = avpkt->size; /* skip reported size */ - skip_bits_long(gb, 32); + bitstream_skip(bc, 32); } /* get output buffer */ @@ -327,7 +328,7 @@ static int decode_frame(AVCodecContext *avctx, void *data, av_log(avctx, AV_LOG_ERROR, "Incomplete packet\n"); return AVERROR_INVALIDDATA; } - get_bits_align32(gb); + get_bits_align32(bc); frame->nb_samples = s->block_size / avctx->channels; *got_frame_ptr = 1; From b4c0daa83c0c5e7505703af65de95fb22d6cbe72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandra=20H=C3=A1jkov=C3=A1?= Date: Sat, 9 Apr 2016 16:20:13 +0200 Subject: [PATCH 14/35] cdxl: Convert to the new bitstream reader Signed-off-by: Anton Khirnov --- libavcodec/cdxl.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/libavcodec/cdxl.c b/libavcodec/cdxl.c index 99e96eb502..4c0410dd21 100644 --- a/libavcodec/cdxl.c +++ b/libavcodec/cdxl.c @@ -21,8 +21,9 @@ #include "libavutil/intreadwrite.h" #include "libavutil/imgutils.h" + #include "avcodec.h" -#include "get_bits.h" +#include "bitstream.h" #include "internal.h" #define BIT_PLANAR 0x00 @@ -69,30 +70,30 @@ static void import_palette(CDXLVideoContext *c, uint32_t *new_palette) static void bitplanar2chunky(CDXLVideoContext *c, int linesize, uint8_t *out) { - GetBitContext gb; + BitstreamContext bc; int x, y, plane; - init_get_bits(&gb, c->video, c->video_size * 8); + bitstream_init(&bc, c->video, c->video_size * 8); for (plane = 0; plane < c->bpp; plane++) { for (y = 0; y < c->avctx->height; y++) { for (x = 0; x < c->avctx->width; x++) - out[linesize * y + x] |= get_bits1(&gb) << plane; - skip_bits(&gb, c->padded_bits); + out[linesize * y + x] |= bitstream_read_bit(&bc) << plane; + bitstream_skip(&bc, c->padded_bits); } } } static void bitline2chunky(CDXLVideoContext *c, int linesize, uint8_t *out) { - GetBitContext gb; + BitstreamContext bc; int x, y, plane; - init_get_bits(&gb, c->video, c->video_size * 8); + bitstream_init(&bc, c->video, c->video_size * 8); for (y = 0; y < c->avctx->height; y++) { for (plane = 0; plane < c->bpp; plane++) { for (x = 0; x < c->avctx->width; x++) - out[linesize * y + x] |= get_bits1(&gb) << plane; - skip_bits(&gb, c->padded_bits); + out[linesize * y + x] |= bitstream_read_bit(&bc) << plane; + bitstream_skip(&bc, c->padded_bits); } } } From e561146611f5cf410df78b53a4ca5cded2272fd3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandra=20H=C3=A1jkov=C3=A1?= Date: Sat, 9 Apr 2016 17:34:03 +0200 Subject: [PATCH 15/35] cljrdec: Convert to the new bitstream reader Signed-off-by: Anton Khirnov --- libavcodec/cljrdec.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/libavcodec/cljrdec.c b/libavcodec/cljrdec.c index 33d8023429..833707b099 100644 --- a/libavcodec/cljrdec.c +++ b/libavcodec/cljrdec.c @@ -25,7 +25,7 @@ */ #include "avcodec.h" -#include "get_bits.h" +#include "bitstream.h" #include "internal.h" static int decode_frame(AVCodecContext *avctx, @@ -34,7 +34,7 @@ static int decode_frame(AVCodecContext *avctx, { const uint8_t *buf = avpkt->data; int buf_size = avpkt->size; - GetBitContext gb; + BitstreamContext bc; AVFrame * const p = data; int x, y, ret; @@ -56,20 +56,20 @@ static int decode_frame(AVCodecContext *avctx, p->pict_type = AV_PICTURE_TYPE_I; p->key_frame = 1; - init_get_bits(&gb, buf, buf_size * 8); + bitstream_init(&bc, buf, buf_size * 8); for (y = 0; y < avctx->height; y++) { uint8_t *luma = &p->data[0][y * p->linesize[0]]; uint8_t *cb = &p->data[1][y * p->linesize[1]]; uint8_t *cr = &p->data[2][y * p->linesize[2]]; for (x = 0; x < avctx->width; x += 4) { - luma[3] = get_bits(&gb, 5) << 3; - luma[2] = get_bits(&gb, 5) << 3; - luma[1] = get_bits(&gb, 5) << 3; - luma[0] = get_bits(&gb, 5) << 3; + luma[3] = bitstream_read(&bc, 5) << 3; + luma[2] = bitstream_read(&bc, 5) << 3; + luma[1] = bitstream_read(&bc, 5) << 3; + luma[0] = bitstream_read(&bc, 5) << 3; luma += 4; - *(cb++) = get_bits(&gb, 6) << 2; - *(cr++) = get_bits(&gb, 6) << 2; + *(cb++) = bitstream_read(&bc, 6) << 2; + *(cr++) = bitstream_read(&bc, 6) << 2; } } From 942e84d2a3c41a9392743ea6b1cb24ca8163d678 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandra=20H=C3=A1jkov=C3=A1?= Date: Sat, 9 Apr 2016 18:00:37 +0200 Subject: [PATCH 16/35] cook: Convert to the new bitstream reader Signed-off-by: Anton Khirnov --- libavcodec/cook.c | 53 ++++++++++++++++++++++++----------------------- 1 file changed, 27 insertions(+), 26 deletions(-) diff --git a/libavcodec/cook.c b/libavcodec/cook.c index c990333a7c..c3304eae2e 100644 --- a/libavcodec/cook.c +++ b/libavcodec/cook.c @@ -47,7 +47,7 @@ #include "audiodsp.h" #include "avcodec.h" -#include "get_bits.h" +#include "bitstream.h" #include "bytestream.h" #include "fft.h" #include "internal.h" @@ -124,7 +124,7 @@ typedef struct cook { AVCodecContext* avctx; AudioDSPContext adsp; - GetBitContext gb; + BitstreamContext bc; /* stream data */ int num_vectors; int samples_per_channel; @@ -325,23 +325,23 @@ static av_cold int cook_decode_close(AVCodecContext *avctx) /** * Fill the gain array for the timedomain quantization. * - * @param gb pointer to the GetBitContext + * @param bc pointer to the BitstreamContext * @param gaininfo array[9] of gain indexes */ -static void decode_gain_info(GetBitContext *gb, int *gaininfo) +static void decode_gain_info(BitstreamContext *bc, int *gaininfo) { int i, n; - while (get_bits1(gb)) { + while (bitstream_read_bit(bc)) { /* NOTHING */ } - n = get_bits_count(gb) - 1; // amount of elements*2 to update + n = bitstream_tell(bc) - 1; // amount of elements * 2 to update i = 0; while (n--) { - int index = get_bits(gb, 3); - int gain = get_bits1(gb) ? get_bits(gb, 4) - 7 : -1; + int index = bitstream_read(bc, 3); + int gain = bitstream_read_bit(bc) ? bitstream_read(bc, 4) - 7 : -1; while (i <= index) gaininfo[i++] = gain; @@ -361,7 +361,7 @@ static int decode_envelope(COOKContext *q, COOKSubpacket *p, { int i, j, vlc_index; - quant_index_table[0] = get_bits(&q->gb, 6) - 6; // This is used later in categorize + quant_index_table[0] = bitstream_read(&q->bc, 6) - 6; // This is used later in categorize for (i = 1; i < p->total_subbands; i++) { vlc_index = i; @@ -375,8 +375,8 @@ static int decode_envelope(COOKContext *q, COOKSubpacket *p, if (vlc_index > 13) vlc_index = 13; // the VLC tables >13 are identical to No. 13 - j = get_vlc2(&q->gb, q->envelope_quant_index[vlc_index - 1].table, - q->envelope_quant_index[vlc_index - 1].bits, 2); + j = bitstream_read_vlc(&q->bc, q->envelope_quant_index[vlc_index - 1].table, + q->envelope_quant_index[vlc_index - 1].bits, 2); quant_index_table[i] = quant_index_table[i - 1] + j - 12; // differential encoding if (quant_index_table[i] > 63 || quant_index_table[i] < -63) { av_log(q->avctx, AV_LOG_ERROR, @@ -408,7 +408,7 @@ static void categorize(COOKContext *q, COOKSubpacket *p, int *quant_index_table, int tmp_categorize_array1_idx = p->numvector_size; int tmp_categorize_array2_idx = p->numvector_size; - bits_left = p->bits_per_subpacket - get_bits_count(&q->gb); + bits_left = p->bits_per_subpacket - bitstream_tell(&q->bc); if (bits_left > q->samples_per_channel) bits_left = q->samples_per_channel + @@ -554,8 +554,8 @@ static int unpack_SQVH(COOKContext *q, COOKSubpacket *p, int category, vd = vd_tab[category]; result = 0; for (i = 0; i < vpr_tab[category]; i++) { - vlc = get_vlc2(&q->gb, q->sqvh[category].table, q->sqvh[category].bits, 3); - if (p->bits_per_subpacket < get_bits_count(&q->gb)) { + vlc = bitstream_read_vlc(&q->bc, q->sqvh[category].table, q->sqvh[category].bits, 3); + if (p->bits_per_subpacket < bitstream_tell(&q->bc)) { vlc = 0; result = 1; } @@ -566,8 +566,8 @@ static int unpack_SQVH(COOKContext *q, COOKSubpacket *p, int category, } for (j = 0; j < vd; j++) { if (subband_coef_index[i * vd + j]) { - if (get_bits_count(&q->gb) < p->bits_per_subpacket) { - subband_coef_sign[i * vd + j] = get_bits1(&q->gb); + if (bitstream_tell(&q->bc) < p->bits_per_subpacket) { + subband_coef_sign[i * vd + j] = bitstream_read_bit(&q->bc); } else { result = 1; subband_coef_sign[i * vd + j] = 0; @@ -634,7 +634,7 @@ static int mono_decode(COOKContext *q, COOKSubpacket *p, float *mlt_buffer) if ((res = decode_envelope(q, p, quant_index_table)) < 0) return res; - q->num_vectors = get_bits(&q->gb, p->log2_numvector_size); + q->num_vectors = bitstream_read(&q->bc, p->log2_numvector_size); categorize(q, p, quant_index_table, category, category_index); expand_category(q, category, category_index); decode_vectors(q, p, category, quant_index_table, mlt_buffer); @@ -739,7 +739,7 @@ static void imlt_gain(COOKContext *q, float *inbuffer, static void decouple_info(COOKContext *q, COOKSubpacket *p, int *decouple_tab) { int i; - int vlc = get_bits1(&q->gb); + int vlc = bitstream_read_bit(&q->bc); int start = cplband[p->js_subband_start]; int end = cplband[p->subbands - 1]; int length = end - start + 1; @@ -749,12 +749,13 @@ static void decouple_info(COOKContext *q, COOKSubpacket *p, int *decouple_tab) if (vlc) for (i = 0; i < length; i++) - decouple_tab[start + i] = get_vlc2(&q->gb, - p->channel_coupling.table, - p->channel_coupling.bits, 2); + decouple_tab[start + i] = + bitstream_read_vlc(&q->bc, + p->channel_coupling.table, + p->channel_coupling.bits, 2); else for (i = 0; i < length; i++) - decouple_tab[start + i] = get_bits(&q->gb, p->js_vlc_bits); + decouple_tab[start + i] = bitstream_read(&q->bc, p->js_vlc_bits); } /* @@ -850,9 +851,9 @@ static inline void decode_bytes_and_gain(COOKContext *q, COOKSubpacket *p, offset = decode_bytes(inbuffer, q->decoded_bytes_buffer, p->bits_per_subpacket / 8); - init_get_bits(&q->gb, q->decoded_bytes_buffer + offset, - p->bits_per_subpacket); - decode_gain_info(&q->gb, gains_ptr->now); + bitstream_init(&q->bc, q->decoded_bytes_buffer + offset, + p->bits_per_subpacket); + decode_gain_info(&q->bc, gains_ptr->now); /* Swap current and previous gains */ FFSWAP(int *, gains_ptr->now, gains_ptr->previous); @@ -994,7 +995,7 @@ static int cook_decode_frame(AVCodecContext *avctx, void *data, offset += q->subpacket[i].size; chidx += q->subpacket[i].num_channels; av_log(avctx, AV_LOG_DEBUG, "subpacket[%i] %i %i\n", - i, q->subpacket[i].size * 8, get_bits_count(&q->gb)); + i, q->subpacket[i].size * 8, bitstream_tell(&q->bc)); } /* Discard the first two frames: no valid audio. */ From 928f8c7ce360f464f1c5d3a363b2d4b1eb7c471f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandra=20H=C3=A1jkov=C3=A1?= Date: Sat, 9 Apr 2016 18:58:25 +0200 Subject: [PATCH 17/35] dss_sp: Convert to the new bitstream reader Signed-off-by: Anton Khirnov --- libavcodec/dss_sp.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/libavcodec/dss_sp.c b/libavcodec/dss_sp.c index 20b05287a7..44d98d8cf9 100644 --- a/libavcodec/dss_sp.c +++ b/libavcodec/dss_sp.c @@ -25,7 +25,7 @@ #include "libavutil/opt.h" #include "avcodec.h" -#include "get_bits.h" +#include "bitstream.h" #include "internal.h" #define SUBFRAMES 4 @@ -302,7 +302,7 @@ static av_cold int dss_sp_decode_init(AVCodecContext *avctx) static void dss_sp_unpack_coeffs(DssSpContext *p, const uint8_t *src) { - GetBitContext gb; + BitstreamContext bc; DssSpFrame *fparam = &p->fparam; int i; int subframe_idx; @@ -315,24 +315,24 @@ static void dss_sp_unpack_coeffs(DssSpContext *p, const uint8_t *src) p->bits[i + 1] = src[i]; } - init_get_bits(&gb, p->bits, DSS_SP_FRAME_SIZE * 8); + bitstream_init(&bc, p->bits, DSS_SP_FRAME_SIZE * 8); for (i = 0; i < 2; i++) - fparam->filter_idx[i] = get_bits(&gb, 5); + fparam->filter_idx[i] = bitstream_read(&bc, 5); for (; i < 8; i++) - fparam->filter_idx[i] = get_bits(&gb, 4); + fparam->filter_idx[i] = bitstream_read(&bc, 4); for (; i < 14; i++) - fparam->filter_idx[i] = get_bits(&gb, 3); + fparam->filter_idx[i] = bitstream_read(&bc, 3); for (subframe_idx = 0; subframe_idx < 4; subframe_idx++) { - fparam->sf_adaptive_gain[subframe_idx] = get_bits(&gb, 5); + fparam->sf_adaptive_gain[subframe_idx] = bitstream_read(&bc, 5); - fparam->sf[subframe_idx].combined_pulse_pos = get_bits_long(&gb, 31); + fparam->sf[subframe_idx].combined_pulse_pos = bitstream_read(&bc, 31); - fparam->sf[subframe_idx].gain = get_bits(&gb, 6); + fparam->sf[subframe_idx].gain = bitstream_read(&bc, 6); for (i = 0; i < 7; i++) - fparam->sf[subframe_idx].pulse_val[i] = get_bits(&gb, 3); + fparam->sf[subframe_idx].pulse_val[i] = bitstream_read(&bc, 3); } for (subframe_idx = 0; subframe_idx < 4; subframe_idx++) { @@ -394,7 +394,7 @@ static void dss_sp_unpack_coeffs(DssSpContext *p, const uint8_t *src) } } - combined_pitch = get_bits(&gb, 24); + combined_pitch = bitstream_read(&bc, 24); fparam->pitch_lag[0] = (combined_pitch % 151) + 36; From d8618570beb52001c0e8960777bab6158a60b3f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandra=20H=C3=A1jkov=C3=A1?= Date: Sat, 9 Apr 2016 19:05:56 +0200 Subject: [PATCH 18/35] dvdsubdec: Convert to the new bitstream reader Signed-off-by: Anton Khirnov --- libavcodec/dvdsubdec.c | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/libavcodec/dvdsubdec.c b/libavcodec/dvdsubdec.c index 86c287391f..b02bb6b93f 100644 --- a/libavcodec/dvdsubdec.c +++ b/libavcodec/dvdsubdec.c @@ -20,7 +20,7 @@ */ #include "avcodec.h" -#include "get_bits.h" +#include "bitstream.h" #include "internal.h" #include "libavutil/attributes.h" @@ -50,13 +50,13 @@ static void yuv_a_to_rgba(const uint8_t *ycbcr, const uint8_t *alpha, uint32_t * } } -static int decode_run_2bit(GetBitContext *gb, int *color) +static int decode_run_2bit(BitstreamContext *bc, int *color) { unsigned int v, t; v = 0; for (t = 1; v < t && t <= 0x40; t <<= 2) - v = (v << 4) | get_bits(gb, 4); + v = (v << 4) | bitstream_read(bc, 4); *color = v & 3; if (v < 4) { /* Code for fill rest of line */ return INT_MAX; @@ -64,23 +64,23 @@ static int decode_run_2bit(GetBitContext *gb, int *color) return v >> 2; } -static int decode_run_8bit(GetBitContext *gb, int *color) +static int decode_run_8bit(BitstreamContext *bc, int *color) { int len; - int has_run = get_bits1(gb); - if (get_bits1(gb)) - *color = get_bits(gb, 8); + int has_run = bitstream_read_bit(bc); + if (bitstream_read_bit(bc)) + *color = bitstream_read(bc, 8); else - *color = get_bits(gb, 2); + *color = bitstream_read(bc, 2); if (has_run) { - if (get_bits1(gb)) { - len = get_bits(gb, 7); + if (bitstream_read_bit(bc)) { + len = bitstream_read(bc, 7); if (len == 0) len = INT_MAX; else len += 9; } else - len = get_bits(gb, 3) + 2; + len = bitstream_read(bc, 3) + 2; } else len = 1; return len; @@ -89,24 +89,24 @@ static int decode_run_8bit(GetBitContext *gb, int *color) static int decode_rle(uint8_t *bitmap, int linesize, int w, int h, const uint8_t *buf, int start, int buf_size, int is_8bit) { - GetBitContext gb; + BitstreamContext bc; int bit_len; int x, y, len, color; uint8_t *d; bit_len = (buf_size - start) * 8; - init_get_bits(&gb, buf + start, bit_len); + bitstream_init(&bc, buf + start, bit_len); x = 0; y = 0; d = bitmap; for(;;) { - if (get_bits_count(&gb) > bit_len) + if (bitstream_tell(&bc) > bit_len) return -1; if (is_8bit) - len = decode_run_8bit(&gb, &color); + len = decode_run_8bit(&bc, &color); else - len = decode_run_2bit(&gb, &color); + len = decode_run_2bit(&bc, &color); len = FFMIN(len, w - x); memset(d + x, color, len); x += len; @@ -117,7 +117,7 @@ static int decode_rle(uint8_t *bitmap, int linesize, int w, int h, d += linesize; x = 0; /* byte align */ - align_get_bits(&gb); + bitstream_align(&bc); } } return 0; From c43eb731721ac1009c2b4ca13f90ef00280119c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandra=20H=C3=A1jkov=C3=A1?= Date: Sat, 9 Apr 2016 20:36:57 +0200 Subject: [PATCH 19/35] escape124: Convert to the new bitstream reader Signed-off-by: Anton Khirnov --- libavcodec/escape124.c | 85 ++++++++++++++++++++---------------------- 1 file changed, 41 insertions(+), 44 deletions(-) diff --git a/libavcodec/escape124.c b/libavcodec/escape124.c index 6d1b487d1b..879f00a73c 100644 --- a/libavcodec/escape124.c +++ b/libavcodec/escape124.c @@ -21,7 +21,7 @@ #define BITSTREAM_READER_LE #include "avcodec.h" -#include "get_bits.h" +#include "bitstream.h" #include "internal.h" typedef union MacroBlock { @@ -48,8 +48,9 @@ typedef struct Escape124Context { CodeBook codebooks[3]; } Escape124Context; -static int can_safely_read(GetBitContext* gb, int bits) { - return get_bits_left(gb) >= bits; +static int can_safely_read(BitstreamContext *bc, int bits) +{ + return bitstream_bits_left(bc) >= bits; } /** @@ -86,13 +87,13 @@ static av_cold int escape124_decode_close(AVCodecContext *avctx) return 0; } -static CodeBook unpack_codebook(GetBitContext* gb, unsigned depth, +static CodeBook unpack_codebook(BitstreamContext *bc, unsigned depth, unsigned size) { unsigned i, j; CodeBook cb = { 0 }; - if (!can_safely_read(gb, size * 34)) + if (!can_safely_read(bc, size * 34)) return cb; if (size >= INT_MAX / sizeof(MacroBlock)) @@ -104,9 +105,9 @@ static CodeBook unpack_codebook(GetBitContext* gb, unsigned depth, cb.depth = depth; cb.size = size; for (i = 0; i < size; i++) { - unsigned mask_bits = get_bits(gb, 4); - unsigned color0 = get_bits(gb, 15); - unsigned color1 = get_bits(gb, 15); + unsigned mask_bits = bitstream_read(bc, 4); + unsigned color0 = bitstream_read(bc, 15); + unsigned color1 = bitstream_read(bc, 15); for (j = 0; j < 4; j++) { if (mask_bits & (1 << j)) @@ -118,47 +119,43 @@ static CodeBook unpack_codebook(GetBitContext* gb, unsigned depth, return cb; } -static unsigned decode_skip_count(GetBitContext* gb) +static unsigned decode_skip_count(BitstreamContext *bc) { unsigned value; // This function reads a maximum of 23 bits, // which is within the padding space - if (!can_safely_read(gb, 1)) + if (!can_safely_read(bc, 1)) return -1; - value = get_bits1(gb); + value = bitstream_read_bit(bc); if (!value) return value; - value += get_bits(gb, 3); + value += bitstream_read(bc, 3); if (value != (1 + ((1 << 3) - 1))) return value; - value += get_bits(gb, 7); + value += bitstream_read(bc, 7); if (value != (1 + ((1 << 3) - 1)) + ((1 << 7) - 1)) return value; - return value + get_bits(gb, 12); + return value + bitstream_read(bc, 12); } -static MacroBlock decode_macroblock(Escape124Context* s, GetBitContext* gb, - int* codebook_index, int superblock_index) +static MacroBlock decode_macroblock(Escape124Context *s, BitstreamContext *bc, + int *codebook_index, int superblock_index) { // This function reads a maximum of 22 bits; the callers // guard this function appropriately unsigned block_index, depth; - int value = get_bits1(gb); + int value = bitstream_read_bit(bc); if (value) { static const char transitions[3][2] = { {2, 1}, {0, 2}, {1, 0} }; - value = get_bits1(gb); + value = bitstream_read_bit(bc); *codebook_index = transitions[*codebook_index][value]; } depth = s->codebooks[*codebook_index].depth; - - // depth = 0 means that this shouldn't read any bits; - // in theory, this is the same as get_bits(gb, 0), but - // that doesn't actually work. - block_index = get_bitsz(gb, depth); + block_index = bitstream_read(bc, depth); if (*codebook_index == 1) { block_index += superblock_index << s->codebooks[1].depth; @@ -208,7 +205,7 @@ static int escape124_decode_frame(AVCodecContext *avctx, Escape124Context *s = avctx->priv_data; AVFrame *frame = data; - GetBitContext gb; + BitstreamContext bc; unsigned frame_flags, frame_size; unsigned i; @@ -220,15 +217,15 @@ static int escape124_decode_frame(AVCodecContext *avctx, unsigned old_stride, new_stride; int ret; - init_get_bits(&gb, buf, buf_size * 8); + bitstream_init(&bc, buf, buf_size * 8); // This call also guards the potential depth reads for the // codebook unpacking. - if (!can_safely_read(&gb, 64)) + if (!can_safely_read(&bc, 64)) return -1; - frame_flags = get_bits_long(&gb, 32); - frame_size = get_bits_long(&gb, 32); + frame_flags = bitstream_read(&bc, 32); + frame_size = bitstream_read(&bc, 32); // Leave last frame unchanged // FIXME: Is this necessary? I haven't seen it in any real samples @@ -251,10 +248,10 @@ static int escape124_decode_frame(AVCodecContext *avctx, if (i == 2) { // This codebook can be cut off at places other than // powers of 2, leaving some of the entries undefined. - cb_size = get_bits_long(&gb, 20); + cb_size = bitstream_read(&bc, 20); cb_depth = av_log2(cb_size - 1) + 1; } else { - cb_depth = get_bits(&gb, 4); + cb_depth = bitstream_read(&bc, 4); if (i == 0) { // This is the most basic codebook: pow(2,depth) entries // for a depth-length key @@ -267,7 +264,7 @@ static int escape124_decode_frame(AVCodecContext *avctx, } } av_free(s->codebooks[i].blocks); - s->codebooks[i] = unpack_codebook(&gb, cb_depth, cb_size); + s->codebooks[i] = unpack_codebook(&bc, cb_depth, cb_size); if (!s->codebooks[i].blocks) return -1; } @@ -292,7 +289,7 @@ static int escape124_decode_frame(AVCodecContext *avctx, if (skip == -1) { // Note that this call will make us skip the rest of the blocks // if the frame prematurely ends - skip = decode_skip_count(&gb); + skip = decode_skip_count(&bc); } if (skip) { @@ -302,10 +299,10 @@ static int escape124_decode_frame(AVCodecContext *avctx, copy_superblock(sb.pixels, 8, old_frame_data, old_stride); - while (can_safely_read(&gb, 1) && !get_bits1(&gb)) { + while (can_safely_read(&bc, 1) && !bitstream_read_bit(&bc)) { unsigned mask; - mb = decode_macroblock(s, &gb, &cb_index, superblock_index); - mask = get_bits(&gb, 16); + mb = decode_macroblock(s, &bc, &cb_index, superblock_index); + mask = bitstream_read(&bc, 16); multi_mask |= mask; for (i = 0; i < 16; i++) { if (mask & mask_matrix[i]) { @@ -314,29 +311,29 @@ static int escape124_decode_frame(AVCodecContext *avctx, } } - if (can_safely_read(&gb, 1) && !get_bits1(&gb)) { - unsigned inv_mask = get_bits(&gb, 4); + if (can_safely_read(&bc, 1) && !bitstream_read_bit(&bc)) { + unsigned inv_mask = bitstream_read(&bc, 4); for (i = 0; i < 4; i++) { if (inv_mask & (1 << i)) { multi_mask ^= 0xF << i*4; } else { - multi_mask ^= get_bits(&gb, 4) << i*4; + multi_mask ^= bitstream_read(&bc, 4) << i * 4; } } for (i = 0; i < 16; i++) { if (multi_mask & mask_matrix[i]) { - if (!can_safely_read(&gb, 1)) + if (!can_safely_read(&bc, 1)) break; - mb = decode_macroblock(s, &gb, &cb_index, + mb = decode_macroblock(s, &bc, &cb_index, superblock_index); insert_mb_into_sb(&sb, mb, i); } } } else if (frame_flags & (1 << 16)) { - while (can_safely_read(&gb, 1) && !get_bits1(&gb)) { - mb = decode_macroblock(s, &gb, &cb_index, superblock_index); - insert_mb_into_sb(&sb, mb, get_bits(&gb, 4)); + while (can_safely_read(&bc, 1) && !bitstream_read_bit(&bc)) { + mb = decode_macroblock(s, &bc, &cb_index, superblock_index); + insert_mb_into_sb(&sb, mb, bitstream_read(&bc, 4)); } } @@ -358,7 +355,7 @@ static int escape124_decode_frame(AVCodecContext *avctx, av_log(NULL, AV_LOG_DEBUG, "Escape sizes: %i, %i, %i\n", - frame_size, buf_size, get_bits_count(&gb) / 8); + frame_size, buf_size, bitstream_tell(&bc) / 8); av_frame_unref(s->frame); if ((ret = av_frame_ref(s->frame, frame)) < 0) From 2906d8dcb39751fc16d5585f3751da3174b8e2bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandra=20H=C3=A1jkov=C3=A1?= Date: Sat, 9 Apr 2016 20:47:04 +0200 Subject: [PATCH 20/35] escape130: Convert to the new bitstream reader Signed-off-by: Anton Khirnov --- libavcodec/escape130.c | 46 +++++++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/libavcodec/escape130.c b/libavcodec/escape130.c index bfc1f3f09e..544f36d1ac 100644 --- a/libavcodec/escape130.c +++ b/libavcodec/escape130.c @@ -24,7 +24,7 @@ #define BITSTREAM_READER_LE #include "avcodec.h" -#include "get_bits.h" +#include "bitstream.h" #include "internal.h" typedef struct Escape130Context { @@ -163,23 +163,23 @@ static av_cold int escape130_decode_close(AVCodecContext *avctx) return 0; } -static int decode_skip_count(GetBitContext* gb) +static int decode_skip_count(BitstreamContext *bc) { int value; - value = get_bits1(gb); + value = bitstream_read_bit(bc); if (value) return 0; - value = get_bits(gb, 3); + value = bitstream_read(bc, 3); if (value) return value; - value = get_bits(gb, 8); + value = bitstream_read(bc, 8); if (value) return value + 7; - value = get_bits(gb, 15); + value = bitstream_read(bc, 15); if (value) return value + 262; @@ -193,7 +193,7 @@ static int escape130_decode_frame(AVCodecContext *avctx, void *data, int buf_size = avpkt->size; Escape130Context *s = avctx->priv_data; AVFrame *pic = data; - GetBitContext gb; + BitstreamContext bc; int ret; uint8_t *old_y, *old_cb, *old_cr, @@ -216,7 +216,7 @@ static int escape130_decode_frame(AVCodecContext *avctx, void *data, if ((ret = ff_get_buffer(avctx, pic, 0)) < 0) return ret; - init_get_bits(&gb, buf + 16, (buf_size - 16) * 8); + bitstream_init(&bc, buf + 16, (buf_size - 16) * 8); new_y = s->new_y; new_cb = s->new_u; @@ -235,7 +235,7 @@ static int escape130_decode_frame(AVCodecContext *avctx, void *data, // Note that this call will make us skip the rest of the blocks // if the frame ends prematurely. if (skip == -1) - skip = decode_skip_count(&gb); + skip = decode_skip_count(&bc); if (skip == -1) { av_log(avctx, AV_LOG_ERROR, "Error decoding skip value\n"); return AVERROR_INVALIDDATA; @@ -250,31 +250,31 @@ static int escape130_decode_frame(AVCodecContext *avctx, void *data, cb = old_cb[0]; cr = old_cr[0]; } else { - if (get_bits1(&gb)) { - unsigned sign_selector = get_bits(&gb, 6); - unsigned difference_selector = get_bits(&gb, 2); - y_avg = 2 * get_bits(&gb, 5); + if (bitstream_read_bit(&bc)) { + unsigned sign_selector = bitstream_read(&bc, 6); + unsigned difference_selector = bitstream_read(&bc, 2); + y_avg = 2 * bitstream_read(&bc, 5); for (i = 0; i < 4; i++) { y[i] = av_clip(y_avg + offset_table[difference_selector] * sign_table[sign_selector][i], 0, 63); } - } else if (get_bits1(&gb)) { - if (get_bits1(&gb)) { - y_avg = get_bits(&gb, 6); + } else if (bitstream_read_bit(&bc)) { + if (bitstream_read_bit(&bc)) { + y_avg = bitstream_read(&bc, 6); } else { - unsigned adjust_index = get_bits(&gb, 3); + unsigned adjust_index = bitstream_read(&bc, 3); y_avg = (y_avg + luma_adjust[adjust_index]) & 63; } for (i = 0; i < 4; i++) y[i] = y_avg; } - if (get_bits1(&gb)) { - if (get_bits1(&gb)) { - cb = get_bits(&gb, 5); - cr = get_bits(&gb, 5); + if (bitstream_read_bit(&bc)) { + if (bitstream_read_bit(&bc)) { + cb = bitstream_read(&bc, 5); + cr = bitstream_read(&bc, 5); } else { - unsigned adjust_index = get_bits(&gb, 3); + unsigned adjust_index = bitstream_read(&bc, 3); cb = (cb + chroma_adjust[0][adjust_index]) & 31; cr = (cr + chroma_adjust[1][adjust_index]) & 31; } @@ -333,7 +333,7 @@ static int escape130_decode_frame(AVCodecContext *avctx, void *data, } ff_dlog(avctx, "Frame data: provided %d bytes, used %d bytes\n", - buf_size, get_bits_count(&gb) >> 3); + buf_size, bitstream_tell(&bc) >> 3); FFSWAP(uint8_t*, s->old_y, s->new_y); FFSWAP(uint8_t*, s->old_u, s->new_u); From 8df1ac6b785a1a159cdffed1eec1ab2a8df66460 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandra=20H=C3=A1jkov=C3=A1?= Date: Sat, 9 Apr 2016 21:00:43 +0200 Subject: [PATCH 21/35] exr: Convert to the new bitstream reader Signed-off-by: Anton Khirnov --- libavcodec/exr.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/libavcodec/exr.c b/libavcodec/exr.c index d10841d8d6..28cee84134 100644 --- a/libavcodec/exr.c +++ b/libavcodec/exr.c @@ -39,8 +39,8 @@ #include "libavutil/opt.h" #include "avcodec.h" +#include "bitstream.h" #include "bytestream.h" -#include "get_bits.h" #include "internal.h" #include "mathops.h" #include "thread.h" @@ -379,16 +379,16 @@ static void huf_canonical_code_table(uint64_t *hcode) static int huf_unpack_enc_table(GetByteContext *gb, int32_t im, int32_t iM, uint64_t *hcode) { - GetBitContext gbit; - int ret = init_get_bits8(&gbit, gb->buffer, bytestream2_get_bytes_left(gb)); + BitstreamContext bc; + int ret = bitstream_init8(&bc, gb->buffer, bytestream2_get_bytes_left(gb)); if (ret < 0) return ret; for (; im <= iM; im++) { - uint64_t l = hcode[im] = get_bits(&gbit, 6); + uint64_t l = hcode[im] = bitstream_read(&bc, 6); if (l == LONG_ZEROCODE_RUN) { - int zerun = get_bits(&gbit, 8) + SHORTEST_LONG_RUN; + int zerun = bitstream_read(&bc, 8) + SHORTEST_LONG_RUN; if (im + zerun > iM + 1) return AVERROR_INVALIDDATA; @@ -410,7 +410,7 @@ static int huf_unpack_enc_table(GetByteContext *gb, } } - bytestream2_skip(gb, (get_bits_count(&gbit) + 7) / 8); + bytestream2_skip(gb, (bitstream_tell(&bc) + 7) / 8); huf_canonical_code_table(hcode); return 0; From 418ccdd7034e4cbbb991a4fb47753d76286998f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandra=20H=C3=A1jkov=C3=A1?= Date: Sat, 9 Apr 2016 21:04:29 +0200 Subject: [PATCH 22/35] faxcompr: Convert to the new bitstream reader Signed-off-by: Anton Khirnov --- libavcodec/faxcompr.c | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/libavcodec/faxcompr.c b/libavcodec/faxcompr.c index 4cbda3f126..8a9010d40c 100644 --- a/libavcodec/faxcompr.c +++ b/libavcodec/faxcompr.c @@ -25,7 +25,7 @@ * @author Konstantin Shishkov */ #include "avcodec.h" -#include "get_bits.h" +#include "bitstream.h" #include "put_bits.h" #include "faxcompr.h" @@ -123,7 +123,7 @@ av_cold void ff_ccitt_unpack_init(void) } -static int decode_group3_1d_line(AVCodecContext *avctx, GetBitContext *gb, +static int decode_group3_1d_line(AVCodecContext *avctx, BitstreamContext *bc, unsigned int pix_left, int *runs, const int *runend) { @@ -131,7 +131,7 @@ static int decode_group3_1d_line(AVCodecContext *avctx, GetBitContext *gb, unsigned int run = 0; unsigned int t; for (;;) { - t = get_vlc2(gb, ccitt_vlc[mode].table, 9, 2); + t = bitstream_read_vlc(bc, ccitt_vlc[mode].table, 9, 2); run += t; if (t < 64) { *runs++ = run; @@ -157,7 +157,7 @@ static int decode_group3_1d_line(AVCodecContext *avctx, GetBitContext *gb, return 0; } -static int decode_group3_2d_line(AVCodecContext *avctx, GetBitContext *gb, +static int decode_group3_2d_line(AVCodecContext *avctx, BitstreamContext *bc, unsigned int width, int *runs, const int *runend, const int *ref) { @@ -168,7 +168,7 @@ static int decode_group3_2d_line(AVCodecContext *avctx, GetBitContext *gb, runend--; // for the last written 0 while (offs < width) { - int cmode = get_vlc2(gb, ccitt_group3_2d_vlc.table, 9, 1); + int cmode = bitstream_read_vlc(bc, ccitt_group3_2d_vlc.table, 9, 1); if (cmode == -1) { av_log(avctx, AV_LOG_ERROR, "Incorrect mode VLC\n"); return AVERROR_INVALIDDATA; @@ -188,7 +188,7 @@ static int decode_group3_2d_line(AVCodecContext *avctx, GetBitContext *gb, for (k = 0; k < 2; k++) { run = 0; for (;;) { - t = get_vlc2(gb, ccitt_vlc[mode].table, 9, 2); + t = bitstream_read_vlc(bc, ccitt_vlc[mode].table, 9, 2); if (t == -1) { av_log(avctx, AV_LOG_ERROR, "Incorrect code\n"); return AVERROR_INVALIDDATA; @@ -258,12 +258,12 @@ static void put_line(uint8_t *dst, int size, int width, const int *runs) flush_put_bits(&pb); } -static int find_group3_syncmarker(GetBitContext *gb, int srcsize) +static int find_group3_syncmarker(BitstreamContext *bc, int srcsize) { unsigned int state = -1; - srcsize -= get_bits_count(gb); + srcsize -= bitstream_tell(bc); while (srcsize-- > 0) { - state += state + get_bits1(gb); + state += state + bitstream_read_bit(bc); if ((state & 0xFFF) == 1) return 0; } @@ -275,7 +275,7 @@ int ff_ccitt_unpack(AVCodecContext *avctx, const uint8_t *src, int srcsize, enum TiffCompr compr, int opts) { int j; - GetBitContext gb; + BitstreamContext bc; int *runs, *ref = NULL, *runend; int ret; int runsize = avctx->width + 2; @@ -289,27 +289,27 @@ int ff_ccitt_unpack(AVCodecContext *avctx, const uint8_t *src, int srcsize, ref[0] = avctx->width; ref[1] = 0; ref[2] = 0; - init_get_bits(&gb, src, srcsize * 8); + bitstream_init(&bc, src, srcsize * 8); for (j = 0; j < height; j++) { runend = runs + runsize; if (compr == TIFF_G4) { - ret = decode_group3_2d_line(avctx, &gb, avctx->width, runs, runend, + ret = decode_group3_2d_line(avctx, &bc, avctx->width, runs, runend, ref); if (ret < 0) goto fail; } else { int g3d1 = (compr == TIFF_G3) && !(opts & 1); if (compr != TIFF_CCITT_RLE && - find_group3_syncmarker(&gb, srcsize * 8) < 0) + find_group3_syncmarker(&bc, srcsize * 8) < 0) break; - if (compr == TIFF_CCITT_RLE || g3d1 || get_bits1(&gb)) - ret = decode_group3_1d_line(avctx, &gb, avctx->width, runs, + if (compr == TIFF_CCITT_RLE || g3d1 || bitstream_read_bit(&bc)) + ret = decode_group3_1d_line(avctx, &bc, avctx->width, runs, runend); else - ret = decode_group3_2d_line(avctx, &gb, avctx->width, runs, + ret = decode_group3_2d_line(avctx, &bc, avctx->width, runs, runend, ref); if (compr == TIFF_CCITT_RLE) - align_get_bits(&gb); + bitstream_align(&bc); } if (avctx->err_recognition & AV_EF_EXPLODE && ret < 0) goto fail; From 692ba4fe6445581cee4d414ee29538f1dce7fd8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandra=20H=C3=A1jkov=C3=A1?= Date: Sun, 10 Apr 2016 11:08:09 +0200 Subject: [PATCH 23/35] flashsv: Convert to the new bitstream reader Signed-off-by: Anton Khirnov --- libavcodec/flashsv.c | 57 ++++++++++++++++++++++---------------------- 1 file changed, 29 insertions(+), 28 deletions(-) diff --git a/libavcodec/flashsv.c b/libavcodec/flashsv.c index 2cf8f3f584..20fa7bc1af 100644 --- a/libavcodec/flashsv.c +++ b/libavcodec/flashsv.c @@ -38,9 +38,10 @@ #include #include "libavutil/intreadwrite.h" + #include "avcodec.h" +#include "bitstream.h" #include "bytestream.h" -#include "get_bits.h" #include "internal.h" typedef struct BlockInfo { @@ -175,7 +176,7 @@ static int flashsv2_prime(FlashSVContext *s, uint8_t *src, int size) } static int flashsv_decode_block(AVCodecContext *avctx, AVPacket *avpkt, - GetBitContext *gb, int block_size, + BitstreamContext *bc, int block_size, int width, int height, int x_pos, int y_pos, int blk_idx) { @@ -194,7 +195,7 @@ static int flashsv_decode_block(AVCodecContext *avctx, AVPacket *avpkt, if (ret < 0) return ret; } - s->zstream.next_in = avpkt->data + get_bits_count(gb) / 8; + s->zstream.next_in = avpkt->data + bitstream_tell(bc) / 8; s->zstream.avail_in = block_size; s->zstream.next_out = s->tmpblock; s->zstream.avail_out = s->block_size * 3; @@ -210,7 +211,7 @@ static int flashsv_decode_block(AVCodecContext *avctx, AVPacket *avpkt, } if (s->is_keyframe) { - s->blocks[blk_idx].pos = s->keyframedata + (get_bits_count(gb) / 8); + s->blocks[blk_idx].pos = s->keyframedata + (bitstream_tell(bc) / 8); s->blocks[blk_idx].size = block_size; } @@ -233,7 +234,7 @@ static int flashsv_decode_block(AVCodecContext *avctx, AVPacket *avpkt, x_pos, s->diff_height, width, s->frame->linesize[0], s->pal); } - skip_bits_long(gb, 8 * block_size); /* skip the consumed bits */ + bitstream_skip(bc, 8 * block_size); /* skip the consumed bits */ return 0; } @@ -259,7 +260,7 @@ static int flashsv_decode_frame(AVCodecContext *avctx, void *data, int buf_size = avpkt->size; FlashSVContext *s = avctx->priv_data; int h_blocks, v_blocks, h_part, v_part, i, j, ret; - GetBitContext gb; + BitstreamContext bc; /* no supplementary picture */ if (buf_size == 0) @@ -267,21 +268,21 @@ static int flashsv_decode_frame(AVCodecContext *avctx, void *data, if (buf_size < 4) return -1; - init_get_bits(&gb, avpkt->data, buf_size * 8); + bitstream_init(&bc, avpkt->data, buf_size * 8); /* start to parse the bitstream */ - s->block_width = 16 * (get_bits(&gb, 4) + 1); - s->image_width = get_bits(&gb, 12); - s->block_height = 16 * (get_bits(&gb, 4) + 1); - s->image_height = get_bits(&gb, 12); + s->block_width = 16 * (bitstream_read(&bc, 4) + 1); + s->image_width = bitstream_read(&bc, 12); + s->block_height = 16 * (bitstream_read(&bc, 4) + 1); + s->image_height = bitstream_read(&bc, 12); if (s->ver == 2) { - skip_bits(&gb, 6); - if (get_bits1(&gb)) { + bitstream_skip(&bc, 6); + if (bitstream_read_bit(&bc)) { avpriv_request_sample(avctx, "iframe"); return AVERROR_PATCHWELCOME; } - if (get_bits1(&gb)) { + if (bitstream_read_bit(&bc)) { avpriv_request_sample(avctx, "Custom palette"); return AVERROR_PATCHWELCOME; } @@ -371,7 +372,7 @@ static int flashsv_decode_frame(AVCodecContext *avctx, void *data, int has_diff = 0; /* get the size of the compressed zlib chunk */ - int size = get_bits(&gb, 16); + int size = bitstream_read(&bc, 16); s->color_depth = 0; s->zlibprime_curr = 0; @@ -379,17 +380,17 @@ static int flashsv_decode_frame(AVCodecContext *avctx, void *data, s->diff_start = 0; s->diff_height = cur_blk_height; - if (8 * size > get_bits_left(&gb)) { + if (8 * size > bitstream_bits_left(&bc)) { av_frame_unref(s->frame); return AVERROR_INVALIDDATA; } if (s->ver == 2 && size) { - skip_bits(&gb, 3); - s->color_depth = get_bits(&gb, 2); - has_diff = get_bits1(&gb); - s->zlibprime_curr = get_bits1(&gb); - s->zlibprime_prev = get_bits1(&gb); + bitstream_skip(&bc, 3); + s->color_depth = bitstream_read(&bc, 2); + has_diff = bitstream_read_bit(&bc); + s->zlibprime_curr = bitstream_read_bit(&bc); + s->zlibprime_prev = bitstream_read_bit(&bc); if (s->color_depth != 0 && s->color_depth != 2) { av_log(avctx, AV_LOG_ERROR, @@ -404,8 +405,8 @@ static int flashsv_decode_frame(AVCodecContext *avctx, void *data, "Inter frame without keyframe\n"); return AVERROR_INVALIDDATA; } - s->diff_start = get_bits(&gb, 8); - s->diff_height = get_bits(&gb, 8); + s->diff_start = bitstream_read(&bc, 8); + s->diff_height = bitstream_read(&bc, 8); if (s->diff_start + s->diff_height > cur_blk_height) { av_log(avctx, AV_LOG_ERROR, "Block parameters invalid: %d + %d > %d\n", @@ -422,8 +423,8 @@ static int flashsv_decode_frame(AVCodecContext *avctx, void *data, av_log(avctx, AV_LOG_DEBUG, "%dx%d zlibprime_prev\n", i, j); if (s->zlibprime_curr) { - int col = get_bits(&gb, 8); - int row = get_bits(&gb, 8); + int col = bitstream_read(&bc, 8); + int row = bitstream_read(&bc, 8); av_log(avctx, AV_LOG_DEBUG, "%dx%d zlibprime_curr %dx%d\n", i, j, col, row); size -= 2; @@ -451,7 +452,7 @@ static int flashsv_decode_frame(AVCodecContext *avctx, void *data, /* skip unchanged blocks, which have size 0 */ if (size) { - if (flashsv_decode_block(avctx, avpkt, &gb, size, + if (flashsv_decode_block(avctx, avpkt, &bc, size, cur_blk_width, cur_blk_height, x_pos, y_pos, i + j * (h_blocks + !!h_part))) @@ -477,9 +478,9 @@ static int flashsv_decode_frame(AVCodecContext *avctx, void *data, *got_frame = 1; - if ((get_bits_count(&gb) / 8) != buf_size) + if ((bitstream_tell(&bc) / 8) != buf_size) av_log(avctx, AV_LOG_ERROR, "buffer not fully consumed (%d != %d)\n", - buf_size, (get_bits_count(&gb) / 8)); + buf_size, (bitstream_tell(&bc) / 8)); /* report that the buffer was completely consumed */ return buf_size; From b37b681f7734533dd6dc2ede8aa9d5c2607e0c23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandra=20H=C3=A1jkov=C3=A1?= Date: Sun, 10 Apr 2016 11:12:27 +0200 Subject: [PATCH 24/35] fraps: Convert to the new bitstream reader Signed-off-by: Anton Khirnov --- libavcodec/fraps.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/libavcodec/fraps.c b/libavcodec/fraps.c index 55051ffe7e..2237991133 100644 --- a/libavcodec/fraps.c +++ b/libavcodec/fraps.c @@ -32,7 +32,7 @@ */ #include "avcodec.h" -#include "get_bits.h" +#include "bitstream.h" #include "huffman.h" #include "bytestream.h" #include "bswapdsp.h" @@ -94,7 +94,7 @@ static int fraps2_decode_plane(FrapsContext *s, uint8_t *dst, int stride, int w, const int step) { int i, j, ret; - GetBitContext gb; + BitstreamContext bc; VLC vlc; Node nodes[512]; @@ -111,10 +111,10 @@ static int fraps2_decode_plane(FrapsContext *s, uint8_t *dst, int stride, int w, s->bdsp.bswap_buf((uint32_t *) s->tmpbuf, (const uint32_t *) src, size >> 2); - init_get_bits(&gb, s->tmpbuf, size * 8); + bitstream_init(&bc, s->tmpbuf, size * 8); for (j = 0; j < h; j++) { for (i = 0; i < w*step; i += step) { - dst[i] = get_vlc2(&gb, vlc.table, VLC_BITS, 3); + dst[i] = bitstream_read_vlc(&bc, vlc.table, VLC_BITS, 3); /* lines are stored as deltas between previous lines * and we need to add 0x80 to the first lines of chroma planes */ @@ -122,7 +122,7 @@ static int fraps2_decode_plane(FrapsContext *s, uint8_t *dst, int stride, int w, dst[i] += dst[i - stride]; else if (Uoff) dst[i] += 0x80; - if (get_bits_left(&gb) < 0) { + if (bitstream_bits_left(&bc) < 0) { ff_free_vlc(&vlc); return AVERROR_INVALIDDATA; } From 799703c3ea3d20c2bdf0f7d8b5015bd3069b2801 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandra=20H=C3=A1jkov=C3=A1?= Date: Sun, 10 Apr 2016 11:16:15 +0200 Subject: [PATCH 25/35] g2meet: Convert to the new bitstream reader Signed-off-by: Anton Khirnov --- libavcodec/g2meet.c | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/libavcodec/g2meet.c b/libavcodec/g2meet.c index 7e90916274..4a7f5a3666 100644 --- a/libavcodec/g2meet.c +++ b/libavcodec/g2meet.c @@ -31,10 +31,10 @@ #include "libavutil/intreadwrite.h" #include "avcodec.h" +#include "bitstream.h" #include "blockdsp.h" #include "bytestream.h" #include "elsdec.h" -#include "get_bits.h" #include "idctdsp.h" #include "internal.h" #include "jpegtables.h" @@ -236,7 +236,7 @@ static void jpg_unescape(const uint8_t *src, int src_size, *dst_size = dst - dst_start; } -static int jpg_decode_block(JPGContext *c, GetBitContext *gb, +static int jpg_decode_block(JPGContext *c, BitstreamContext *bc, int plane, int16_t *block) { int dc, val, pos; @@ -244,18 +244,18 @@ static int jpg_decode_block(JPGContext *c, GetBitContext *gb, const uint8_t *qmat = is_chroma ? chroma_quant : luma_quant; c->bdsp.clear_block(block); - dc = get_vlc2(gb, c->dc_vlc[is_chroma].table, 9, 3); + dc = bitstream_read_vlc(bc, c->dc_vlc[is_chroma].table, 9, 3); if (dc < 0) return AVERROR_INVALIDDATA; if (dc) - dc = get_xbits(gb, dc); + dc = bitstream_read_xbits(bc, dc); dc = dc * qmat[0] + c->prev_dc[plane]; block[0] = dc; c->prev_dc[plane] = dc; pos = 0; while (pos < 63) { - val = get_vlc2(gb, c->ac_vlc[is_chroma].table, 9, 3); + val = bitstream_read_vlc(bc, c->ac_vlc[is_chroma].table, 9, 3); if (val < 0) return AVERROR_INVALIDDATA; pos += val >> 4; @@ -265,7 +265,7 @@ static int jpg_decode_block(JPGContext *c, GetBitContext *gb, if (val) { int nbits = val; - val = get_xbits(gb, nbits); + val = bitstream_read_xbits(bc, nbits); val *= qmat[ff_zigzag_direct[pos]]; block[c->scantable.permutated[pos]] = val; } @@ -286,7 +286,7 @@ static int jpg_decode_data(JPGContext *c, int width, int height, const uint8_t *mask, int mask_stride, int num_mbs, int swapuv) { - GetBitContext gb; + BitstreamContext bc; int mb_w, mb_h, mb_x, mb_y, i, j; int bx, by; int unesc_size; @@ -298,7 +298,7 @@ static int jpg_decode_data(JPGContext *c, int width, int height, return ret; jpg_unescape(src, src_size, c->buf, &unesc_size); memset(c->buf + unesc_size, 0, AV_INPUT_BUFFER_PADDING_SIZE); - init_get_bits(&gb, c->buf, unesc_size * 8); + bitstream_init(&bc, c->buf, unesc_size * 8); width = FFALIGN(width, 16); mb_w = width >> 4; @@ -325,14 +325,14 @@ static int jpg_decode_data(JPGContext *c, int width, int height, if (mask && !mask[mb_x * 2 + i + j * mask_stride]) continue; num_mbs--; - if ((ret = jpg_decode_block(c, &gb, 0, + if ((ret = jpg_decode_block(c, &bc, 0, c->block[i + j * 2])) != 0) return ret; c->idsp.idct(c->block[i + j * 2]); } } for (i = 1; i < 3; i++) { - if ((ret = jpg_decode_block(c, &gb, i, c->block[i + 3])) != 0) + if ((ret = jpg_decode_block(c, &bc, i, c->block[i + 3])) != 0) return ret; c->idsp.idct(c->block[i + 3]); } @@ -1011,11 +1011,11 @@ static void kempf_restore_buf(const uint8_t *src, int len, int width, int height, const uint8_t *pal, int npal, int tidx) { - GetBitContext gb; + BitstreamContext bc; int i, j, nb, col; int align_width = FFALIGN(width, 16); - init_get_bits(&gb, src, len * 8); + bitstream_init(&bc, src, len * 8); if (npal <= 2) nb = 1; else if (npal <= 4) nb = 2; @@ -1023,16 +1023,16 @@ static void kempf_restore_buf(const uint8_t *src, int len, else nb = 8; for (j = 0; j < height; j++, dst += stride, jpeg_tile += tile_stride) { - if (get_bits(&gb, 8)) + if (bitstream_read(&bc, 8)) continue; for (i = 0; i < width; i++) { - col = get_bits(&gb, nb); + col = bitstream_read(&bc, nb); if (col != tidx) memcpy(dst + i * 3, pal + col * 3, 3); else memcpy(dst + i * 3, jpeg_tile + i * 3, 3); } - skip_bits_long(&gb, nb * (align_width - width)); + bitstream_skip(&bc, nb * (align_width - width)); } } From 2188d53906015ea5dd5b2e446f8e5374beacbffe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandra=20H=C3=A1jkov=C3=A1?= Date: Mon, 18 Apr 2016 10:38:43 +0200 Subject: [PATCH 26/35] g72x: Convert to the new bitstream reader Signed-off-by: Anton Khirnov --- libavcodec/g722dec.c | 13 ++++---- libavcodec/g723_1dec.c | 70 +++++++++++++++++++++--------------------- libavcodec/g726.c | 11 ++++--- 3 files changed, 48 insertions(+), 46 deletions(-) diff --git a/libavcodec/g722dec.c b/libavcodec/g722dec.c index c4c0ec8fee..bfd4b420b2 100644 --- a/libavcodec/g722dec.c +++ b/libavcodec/g722dec.c @@ -36,8 +36,9 @@ #include "libavutil/channel_layout.h" #include "libavutil/opt.h" + #include "avcodec.h" -#include "get_bits.h" +#include "bitstream.h" #include "g722.h" #include "internal.h" @@ -92,7 +93,7 @@ static int g722_decode_frame(AVCodecContext *avctx, void *data, int j, ret; const int skip = 8 - c->bits_per_codeword; const int16_t *quantizer_table = low_inv_quants[skip]; - GetBitContext gb; + BitstreamContext bc; /* get output buffer */ frame->nb_samples = avpkt->size * 2; @@ -102,15 +103,15 @@ static int g722_decode_frame(AVCodecContext *avctx, void *data, } out_buf = (int16_t *)frame->data[0]; - init_get_bits(&gb, avpkt->data, avpkt->size * 8); + bitstream_init(&bc, avpkt->data, avpkt->size * 8); for (j = 0; j < avpkt->size; j++) { int ilow, ihigh, rlow, rhigh, dhigh; int xout[2]; - ihigh = get_bits(&gb, 2); - ilow = get_bits(&gb, 6 - skip); - skip_bits(&gb, skip); + ihigh = bitstream_read(&bc, 2); + ilow = bitstream_read(&bc, 6 - skip); + bitstream_skip(&bc, skip); rlow = av_clip_intp2((c->band[0].scale_factor * quantizer_table[ilow] >> 10) + c->band[0].s_predictor, 14); diff --git a/libavcodec/g723_1dec.c b/libavcodec/g723_1dec.c index f50bed134e..2ea3bbffa5 100644 --- a/libavcodec/g723_1dec.c +++ b/libavcodec/g723_1dec.c @@ -32,8 +32,8 @@ #define BITSTREAM_READER_LE #include "acelp_vectors.h" #include "avcodec.h" +#include "bitstream.h" #include "celp_filters.h" -#include "get_bits.h" #include "internal.h" #include "g723_1.h" @@ -68,14 +68,14 @@ static av_cold int g723_1_decode_init(AVCodecContext *avctx) static int unpack_bitstream(G723_1_Context *p, const uint8_t *buf, int buf_size) { - GetBitContext gb; + BitstreamContext bc; int ad_cb_len; int temp, info_bits, i; - init_get_bits(&gb, buf, buf_size * 8); + bitstream_init(&bc, buf, buf_size * 8); /* Extract frame type and rate info */ - info_bits = get_bits(&gb, 2); + info_bits = bitstream_read(&bc, 2); if (info_bits == 3) { p->cur_frame_type = UNTRANSMITTED_FRAME; @@ -83,13 +83,13 @@ static int unpack_bitstream(G723_1_Context *p, const uint8_t *buf, } /* Extract 24 bit lsp indices, 8 bit for each band */ - p->lsp_index[2] = get_bits(&gb, 8); - p->lsp_index[1] = get_bits(&gb, 8); - p->lsp_index[0] = get_bits(&gb, 8); + p->lsp_index[2] = bitstream_read(&bc, 8); + p->lsp_index[1] = bitstream_read(&bc, 8); + p->lsp_index[0] = bitstream_read(&bc, 8); if (info_bits == 2) { p->cur_frame_type = SID_FRAME; - p->subframe[0].amp_index = get_bits(&gb, 6); + p->subframe[0].amp_index = bitstream_read(&bc, 6); return 0; } @@ -97,23 +97,23 @@ static int unpack_bitstream(G723_1_Context *p, const uint8_t *buf, p->cur_rate = info_bits ? RATE_5300 : RATE_6300; p->cur_frame_type = ACTIVE_FRAME; - p->pitch_lag[0] = get_bits(&gb, 7); + p->pitch_lag[0] = bitstream_read(&bc, 7); if (p->pitch_lag[0] > 123) /* test if forbidden code */ return -1; p->pitch_lag[0] += PITCH_MIN; - p->subframe[1].ad_cb_lag = get_bits(&gb, 2); + p->subframe[1].ad_cb_lag = bitstream_read(&bc, 2); - p->pitch_lag[1] = get_bits(&gb, 7); + p->pitch_lag[1] = bitstream_read(&bc, 7); if (p->pitch_lag[1] > 123) return -1; p->pitch_lag[1] += PITCH_MIN; - p->subframe[3].ad_cb_lag = get_bits(&gb, 2); + p->subframe[3].ad_cb_lag = bitstream_read(&bc, 2); p->subframe[0].ad_cb_lag = 1; p->subframe[2].ad_cb_lag = 1; for (i = 0; i < SUBFRAMES; i++) { /* Extract combined gain */ - temp = get_bits(&gb, 12); + temp = bitstream_read(&bc, 12); ad_cb_len = 170; p->subframe[i].dirac_train = 0; if (p->cur_rate == RATE_6300 && p->pitch_lag[i >> 1] < SUBFRAME_LEN - 2) { @@ -130,16 +130,16 @@ static int unpack_bitstream(G723_1_Context *p, const uint8_t *buf, } } - p->subframe[0].grid_index = get_bits(&gb, 1); - p->subframe[1].grid_index = get_bits(&gb, 1); - p->subframe[2].grid_index = get_bits(&gb, 1); - p->subframe[3].grid_index = get_bits(&gb, 1); + p->subframe[0].grid_index = bitstream_read(&bc, 1); + p->subframe[1].grid_index = bitstream_read(&bc, 1); + p->subframe[2].grid_index = bitstream_read(&bc, 1); + p->subframe[3].grid_index = bitstream_read(&bc, 1); if (p->cur_rate == RATE_6300) { - skip_bits(&gb, 1); /* skip reserved bit */ + bitstream_skip(&bc, 1); /* skip reserved bit */ /* Compute pulse_pos index using the 13-bit combined position index */ - temp = get_bits(&gb, 13); + temp = bitstream_read(&bc, 13); p->subframe[0].pulse_pos = temp / 810; temp -= p->subframe[0].pulse_pos * 810; @@ -150,28 +150,28 @@ static int unpack_bitstream(G723_1_Context *p, const uint8_t *buf, p->subframe[3].pulse_pos = temp - p->subframe[2].pulse_pos * 9; p->subframe[0].pulse_pos = (p->subframe[0].pulse_pos << 16) + - get_bits(&gb, 16); + bitstream_read(&bc, 16); p->subframe[1].pulse_pos = (p->subframe[1].pulse_pos << 14) + - get_bits(&gb, 14); + bitstream_read(&bc, 14); p->subframe[2].pulse_pos = (p->subframe[2].pulse_pos << 16) + - get_bits(&gb, 16); + bitstream_read(&bc, 16); p->subframe[3].pulse_pos = (p->subframe[3].pulse_pos << 14) + - get_bits(&gb, 14); + bitstream_read(&bc, 14); - p->subframe[0].pulse_sign = get_bits(&gb, 6); - p->subframe[1].pulse_sign = get_bits(&gb, 5); - p->subframe[2].pulse_sign = get_bits(&gb, 6); - p->subframe[3].pulse_sign = get_bits(&gb, 5); + p->subframe[0].pulse_sign = bitstream_read(&bc, 6); + p->subframe[1].pulse_sign = bitstream_read(&bc, 5); + p->subframe[2].pulse_sign = bitstream_read(&bc, 6); + p->subframe[3].pulse_sign = bitstream_read(&bc, 5); } else { /* 5300 bps */ - p->subframe[0].pulse_pos = get_bits(&gb, 12); - p->subframe[1].pulse_pos = get_bits(&gb, 12); - p->subframe[2].pulse_pos = get_bits(&gb, 12); - p->subframe[3].pulse_pos = get_bits(&gb, 12); + p->subframe[0].pulse_pos = bitstream_read(&bc, 12); + p->subframe[1].pulse_pos = bitstream_read(&bc, 12); + p->subframe[2].pulse_pos = bitstream_read(&bc, 12); + p->subframe[3].pulse_pos = bitstream_read(&bc, 12); - p->subframe[0].pulse_sign = get_bits(&gb, 4); - p->subframe[1].pulse_sign = get_bits(&gb, 4); - p->subframe[2].pulse_sign = get_bits(&gb, 4); - p->subframe[3].pulse_sign = get_bits(&gb, 4); + p->subframe[0].pulse_sign = bitstream_read(&bc, 4); + p->subframe[1].pulse_sign = bitstream_read(&bc, 4); + p->subframe[2].pulse_sign = bitstream_read(&bc, 4); + p->subframe[3].pulse_sign = bitstream_read(&bc, 4); } return 0; diff --git a/libavcodec/g726.c b/libavcodec/g726.c index e783e74c90..007cdb63cc 100644 --- a/libavcodec/g726.c +++ b/libavcodec/g726.c @@ -25,9 +25,10 @@ #include "libavutil/channel_layout.h" #include "libavutil/opt.h" + #include "avcodec.h" +#include "bitstream.h" #include "internal.h" -#include "get_bits.h" #include "put_bits.h" /** @@ -429,7 +430,7 @@ static int g726_decode_frame(AVCodecContext *avctx, void *data, int buf_size = avpkt->size; G726Context *c = avctx->priv_data; int16_t *samples; - GetBitContext gb; + BitstreamContext bc; int out_samples, ret; out_samples = buf_size * 8 / c->code_size; @@ -442,12 +443,12 @@ static int g726_decode_frame(AVCodecContext *avctx, void *data, } samples = (int16_t *)frame->data[0]; - init_get_bits(&gb, buf, buf_size * 8); + bitstream_init(&bc, buf, buf_size * 8); while (out_samples--) - *samples++ = g726_decode(c, get_bits(&gb, c->code_size)); + *samples++ = g726_decode(c, bitstream_read(&bc, c->code_size)); - if (get_bits_left(&gb) > 0) + if (bitstream_bits_left(&bc) > 0) av_log(avctx, AV_LOG_ERROR, "Frame invalidly split, missing parser?\n"); *got_frame_ptr = 1; From b2c56301f9458207cfd16c66dc2f3ce15a336651 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandra=20H=C3=A1jkov=C3=A1?= Date: Sun, 10 Apr 2016 11:34:09 +0200 Subject: [PATCH 27/35] gsm: Convert to the new bitstream reader Signed-off-by: Anton Khirnov --- libavcodec/gsmdec.c | 11 ++++++----- libavcodec/gsmdec_template.c | 34 +++++++++++++++++----------------- libavcodec/msgsmdec.c | 9 +++++---- 3 files changed, 28 insertions(+), 26 deletions(-) diff --git a/libavcodec/gsmdec.c b/libavcodec/gsmdec.c index a333e58bdc..d727cf9ccf 100644 --- a/libavcodec/gsmdec.c +++ b/libavcodec/gsmdec.c @@ -25,8 +25,9 @@ */ #include "libavutil/channel_layout.h" + #include "avcodec.h" -#include "get_bits.h" +#include "bitstream.h" #include "internal.h" #include "msgsmdec.h" @@ -67,7 +68,7 @@ static int gsm_decode_frame(AVCodecContext *avctx, void *data, { AVFrame *frame = data; int res; - GetBitContext gb; + BitstreamContext bc; const uint8_t *buf = avpkt->data; int buf_size = avpkt->size; int16_t *samples; @@ -87,10 +88,10 @@ static int gsm_decode_frame(AVCodecContext *avctx, void *data, switch (avctx->codec_id) { case AV_CODEC_ID_GSM: - init_get_bits(&gb, buf, buf_size * 8); - if (get_bits(&gb, 4) != 0xd) + bitstream_init(&bc, buf, buf_size * 8); + if (bitstream_read(&bc, 4) != 0xd) av_log(avctx, AV_LOG_WARNING, "Missing GSM magic!\n"); - res = gsm_decode_block(avctx, samples, &gb, GSM_13000); + res = gsm_decode_block(avctx, samples, &bc, GSM_13000); if (res < 0) return res; break; diff --git a/libavcodec/gsmdec_template.c b/libavcodec/gsmdec_template.c index 2794bd1132..7437908ae5 100644 --- a/libavcodec/gsmdec_template.c +++ b/libavcodec/gsmdec_template.c @@ -24,17 +24,17 @@ * GSM decoder */ -#include "get_bits.h" +#include "bitstream.h" #include "gsm.h" #include "gsmdec_data.h" -static void apcm_dequant_add(GetBitContext *gb, int16_t *dst, const int *frame_bits) +static void apcm_dequant_add(BitstreamContext *bc, int16_t *dst, const int *frame_bits) { int i, val; - int maxidx = get_bits(gb, 6); + int maxidx = bitstream_read(bc, 6); const int16_t *tab = ff_gsm_dequant_tab[maxidx]; for (i = 0; i < 13; i++) { - val = get_bits(gb, frame_bits[i]); + val = bitstream_read(bc, frame_bits[i]); dst[3 * i] += tab[ff_gsm_requant_tab[frame_bits[i]][val]]; } } @@ -120,28 +120,28 @@ static int postprocess(int16_t *data, int msr) } static int gsm_decode_block(AVCodecContext *avctx, int16_t *samples, - GetBitContext *gb, int mode) + BitstreamContext *bc, int mode) { GSMContext *ctx = avctx->priv_data; int i; int16_t *ref_dst = ctx->ref_buf + 120; int *lar = ctx->lar[ctx->lar_idx]; - lar[0] = decode_log_area(get_bits(gb, 6), 13107, 1 << 15); - lar[1] = decode_log_area(get_bits(gb, 6), 13107, 1 << 15); - lar[2] = decode_log_area(get_bits(gb, 5), 13107, (1 << 14) + 2048*2); - lar[3] = decode_log_area(get_bits(gb, 5), 13107, (1 << 14) - 2560*2); - lar[4] = decode_log_area(get_bits(gb, 4), 19223, (1 << 13) + 94*2); - lar[5] = decode_log_area(get_bits(gb, 4), 17476, (1 << 13) - 1792*2); - lar[6] = decode_log_area(get_bits(gb, 3), 31454, (1 << 12) - 341*2); - lar[7] = decode_log_area(get_bits(gb, 3), 29708, (1 << 12) - 1144*2); + lar[0] = decode_log_area(bitstream_read(bc, 6), 13107, 1 << 15); + lar[1] = decode_log_area(bitstream_read(bc, 6), 13107, 1 << 15); + lar[2] = decode_log_area(bitstream_read(bc, 5), 13107, (1 << 14) + 2048 * 2); + lar[3] = decode_log_area(bitstream_read(bc, 5), 13107, (1 << 14) - 2560 * 2); + lar[4] = decode_log_area(bitstream_read(bc, 4), 19223, (1 << 13) + 94 * 2); + lar[5] = decode_log_area(bitstream_read(bc, 4), 17476, (1 << 13) - 1792 * 2); + lar[6] = decode_log_area(bitstream_read(bc, 3), 31454, (1 << 12) - 341 * 2); + lar[7] = decode_log_area(bitstream_read(bc, 3), 29708, (1 << 12) - 1144 * 2); for (i = 0; i < 4; i++) { - int lag = get_bits(gb, 7); - int gain_idx = get_bits(gb, 2); - int offset = get_bits(gb, 2); + int lag = bitstream_read(bc, 7); + int gain_idx = bitstream_read(bc, 2); + int offset = bitstream_read(bc, 2); lag = av_clip(lag, 40, 120); long_term_synth(ref_dst, lag, gain_idx); - apcm_dequant_add(gb, ref_dst + offset, ff_gsm_apcm_bits[mode][i]); + apcm_dequant_add(bc, ref_dst + offset, ff_gsm_apcm_bits[mode][i]); ref_dst += 40; } memcpy(ctx->ref_buf, ctx->ref_buf + 160, 120 * sizeof(*ctx->ref_buf)); diff --git a/libavcodec/msgsmdec.c b/libavcodec/msgsmdec.c index 92b5ae6be8..c26efa9fae 100644 --- a/libavcodec/msgsmdec.c +++ b/libavcodec/msgsmdec.c @@ -21,6 +21,7 @@ #define BITSTREAM_READER_LE #include "avcodec.h" +#include "bitstream.h" #include "gsm.h" #include "msgsmdec.h" @@ -30,10 +31,10 @@ int ff_msgsm_decode_block(AVCodecContext *avctx, int16_t *samples, const uint8_t *buf, int mode) { int res; - GetBitContext gb; - init_get_bits(&gb, buf, GSM_MS_BLOCK_SIZE * 8); - res = gsm_decode_block(avctx, samples, &gb, mode); + BitstreamContext bc; + bitstream_init(&bc, buf, GSM_MS_BLOCK_SIZE * 8); + res = gsm_decode_block(avctx, samples, &bc, mode); if (res < 0) return res; - return gsm_decode_block(avctx, samples + GSM_FRAME_SIZE, &gb, mode); + return gsm_decode_block(avctx, samples + GSM_FRAME_SIZE, &bc, mode); } From c5e01d91702b082ac1b5c2101f1d84dd5017e4ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandra=20H=C3=A1jkov=C3=A1?= Date: Sun, 10 Apr 2016 12:00:56 +0200 Subject: [PATCH 28/35] hq_hqa: Convert to the new bitstream reader Signed-off-by: Anton Khirnov --- libavcodec/hq_hqa.c | 48 ++++++++++++++++++++++----------------------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/libavcodec/hq_hqa.c b/libavcodec/hq_hqa.c index 98bd596439..0d03e593f3 100644 --- a/libavcodec/hq_hqa.c +++ b/libavcodec/hq_hqa.c @@ -24,8 +24,8 @@ #include "libavutil/intreadwrite.h" #include "avcodec.h" +#include "bitstream.h" #include "canopus.h" -#include "get_bits.h" #include "internal.h" #include "hq_hqa.h" @@ -59,7 +59,7 @@ static inline void put_blocks(HQContext *c, AVFrame *pic, pic->linesize[plane] << ilace, block1); } -static int hq_decode_block(HQContext *c, GetBitContext *gb, int16_t block[64], +static int hq_decode_block(HQContext *c, BitstreamContext *bc, int16_t block[64], int qsel, int is_chroma, int is_hqa) { const int32_t *q; @@ -68,15 +68,15 @@ static int hq_decode_block(HQContext *c, GetBitContext *gb, int16_t block[64], memset(block, 0, 64 * sizeof(*block)); if (!is_hqa) { - block[0] = get_sbits(gb, 9) << 6; - q = ff_hq_quants[qsel][is_chroma][get_bits(gb, 2)]; + block[0] = bitstream_read_signed(bc, 9) << 6; + q = ff_hq_quants[qsel][is_chroma][bitstream_read(bc, 2)]; } else { - q = ff_hq_quants[qsel][is_chroma][get_bits(gb, 2)]; - block[0] = get_sbits(gb, 9) << 6; + q = ff_hq_quants[qsel][is_chroma][bitstream_read(bc, 2)]; + block[0] = bitstream_read_signed(bc, 9) << 6; } for (;;) { - val = get_vlc2(gb, c->hq_ac_vlc.table, 9, 2); + val = bitstream_read_vlc(bc, c->hq_ac_vlc.table, 9, 2); if (val < 0) return AVERROR_INVALIDDATA; @@ -91,16 +91,16 @@ static int hq_decode_block(HQContext *c, GetBitContext *gb, int16_t block[64], } static int hq_decode_mb(HQContext *c, AVFrame *pic, - GetBitContext *gb, int x, int y) + BitstreamContext *bc, int x, int y) { int qgroup, flag; int i, ret; - qgroup = get_bits(gb, 4); - flag = get_bits1(gb); + qgroup = bitstream_read(bc, 4); + flag = bitstream_read_bit(bc); for (i = 0; i < 8; i++) { - ret = hq_decode_block(c, gb, c->block[i], qgroup, i >= 4, 0); + ret = hq_decode_block(c, bc, c->block[i], qgroup, i >= 4, 0); if (ret < 0) return ret; } @@ -117,7 +117,7 @@ static int hq_decode_frame(HQContext *ctx, AVFrame *pic, int prof_num, size_t data_size) { const HQProfile *profile; - GetBitContext gb; + BitstreamContext bc; const uint8_t *perm, *src = ctx->gbc.buffer; uint32_t slice_off[21]; int slice, start_off, next_off, i, ret; @@ -160,11 +160,11 @@ static int hq_decode_frame(HQContext *ctx, AVFrame *pic, "Invalid slice size %zu.\n", data_size); break; } - init_get_bits(&gb, src + slice_off[slice], - (slice_off[slice + 1] - slice_off[slice]) * 8); + bitstream_init(&bc, src + slice_off[slice], + (slice_off[slice + 1] - slice_off[slice]) * 8); for (i = 0; i < (next_off - start_off) * profile->tab_w; i++) { - ret = hq_decode_mb(ctx, pic, &gb, perm[0] * 16, perm[1] * 16); + ret = hq_decode_mb(ctx, pic, &bc, perm[0] * 16, perm[1] * 16); if (ret < 0) { av_log(ctx->avctx, AV_LOG_ERROR, "Error decoding macroblock %d at slice %d.\n", i, slice); @@ -178,12 +178,12 @@ static int hq_decode_frame(HQContext *ctx, AVFrame *pic, } static int hqa_decode_mb(HQContext *c, AVFrame *pic, int qgroup, - GetBitContext *gb, int x, int y) + BitstreamContext *bc, int x, int y) { int flag = 0; int i, ret, cbp; - cbp = get_vlc2(gb, c->hqa_cbp_vlc.table, 5, 1); + cbp = bitstream_read_vlc(bc, c->hqa_cbp_vlc.table, 5, 1); for (i = 0; i < 12; i++) memset(c->block[i], 0, sizeof(*c->block)); @@ -191,7 +191,7 @@ static int hqa_decode_mb(HQContext *c, AVFrame *pic, int qgroup, c->block[i][0] = -128 * (1 << 6); if (cbp) { - flag = get_bits1(gb); + flag = bitstream_read_bit(bc); cbp |= cbp << 4; if (cbp & 0x3) @@ -201,7 +201,7 @@ static int hqa_decode_mb(HQContext *c, AVFrame *pic, int qgroup, for (i = 0; i < 12; i++) { if (!(cbp & (1 << i))) continue; - ret = hq_decode_block(c, gb, c->block[i], qgroup, i >= 8, 1); + ret = hq_decode_block(c, bc, c->block[i], qgroup, i >= 8, 1); if (ret < 0) return ret; } @@ -217,7 +217,7 @@ static int hqa_decode_mb(HQContext *c, AVFrame *pic, int qgroup, return 0; } -static int hqa_decode_slice(HQContext *ctx, AVFrame *pic, GetBitContext *gb, +static int hqa_decode_slice(HQContext *ctx, AVFrame *pic, BitstreamContext *bc, int quant, int slice_no, int w, int h) { int i, j, off; @@ -226,7 +226,7 @@ static int hqa_decode_slice(HQContext *ctx, AVFrame *pic, GetBitContext *gb, for (i = 0; i < h; i += 16) { off = (slice_no * 16 + i * 3) & 0x70; for (j = off; j < w; j += 128) { - ret = hqa_decode_mb(ctx, pic, quant, gb, j, i); + ret = hqa_decode_mb(ctx, pic, quant, bc, j, i); if (ret < 0) { av_log(ctx->avctx, AV_LOG_ERROR, "Error decoding macroblock at %dx%d.\n", i, j); @@ -240,7 +240,7 @@ static int hqa_decode_slice(HQContext *ctx, AVFrame *pic, GetBitContext *gb, static int hqa_decode_frame(HQContext *ctx, AVFrame *pic, size_t data_size) { - GetBitContext gb; + BitstreamContext bc; const int num_slices = 8; uint32_t slice_off[9]; int i, slice, ret; @@ -285,10 +285,10 @@ static int hqa_decode_frame(HQContext *ctx, AVFrame *pic, size_t data_size) "Invalid slice size %zu.\n", data_size); break; } - init_get_bits(&gb, src + slice_off[slice], + bitstream_init(&bc, src + slice_off[slice], (slice_off[slice + 1] - slice_off[slice]) * 8); - ret = hqa_decode_slice(ctx, pic, &gb, quant, slice, width, height); + ret = hqa_decode_slice(ctx, pic, &bc, quant, slice, width, height); if (ret < 0) return ret; } From 1df549bfa2bacceb6f119f2aa1146c22ac841243 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandra=20H=C3=A1jkov=C3=A1?= Date: Sun, 10 Apr 2016 12:09:58 +0200 Subject: [PATCH 29/35] hqx: Convert to the new bitstream header Signed-off-by: Anton Khirnov --- libavcodec/hqx.c | 64 ++++++++++++++++++++++++------------------------ libavcodec/hqx.h | 5 ++-- 2 files changed, 35 insertions(+), 34 deletions(-) diff --git a/libavcodec/hqx.c b/libavcodec/hqx.c index 7411d3f252..3c359e3863 100644 --- a/libavcodec/hqx.c +++ b/libavcodec/hqx.c @@ -24,8 +24,8 @@ #include "libavutil/intreadwrite.h" #include "avcodec.h" +#include "bitstream.h" #include "canopus.h" -#include "get_bits.h" #include "internal.h" #include "hqx.h" @@ -95,23 +95,23 @@ static inline void put_blocks(HQXContext *ctx, int plane, lsize * fields, block1, quant); } -static inline void hqx_get_ac(GetBitContext *gb, const HQXAC *ac, +static inline void hqx_get_ac(BitstreamContext *bc, const HQXAC *ac, int *run, int *lev) { int val; - val = show_bits(gb, ac->lut_bits); + val = bitstream_peek(bc, ac->lut_bits); if (ac->lut[val].bits == -1) { - GetBitContext gb2 = *gb; - skip_bits(&gb2, ac->lut_bits); - val = ac->lut[val].lev + show_bits(&gb2, ac->extra_bits); + BitstreamContext bc2 = *bc; + bitstream_skip(&bc2, ac->lut_bits); + val = ac->lut[val].lev + bitstream_peek(&bc2, ac->extra_bits); } *run = ac->lut[val].run; *lev = ac->lut[val].lev; - skip_bits(gb, ac->lut[val].bits); + bitstream_skip(bc, ac->lut[val].bits); } -static int decode_block(GetBitContext *gb, VLC *vlc, +static int decode_block(BitstreamContext *bc, VLC *vlc, const int *quants, int dcb, int16_t block[64], int *last_dc) { @@ -120,14 +120,14 @@ static int decode_block(GetBitContext *gb, VLC *vlc, int run, lev, pos = 1; memset(block, 0, 64 * sizeof(*block)); - dc = get_vlc2(gb, vlc->table, HQX_DC_VLC_BITS, 2); + dc = bitstream_read_vlc(bc, vlc->table, HQX_DC_VLC_BITS, 2); if (dc < 0) return AVERROR_INVALIDDATA; *last_dc += dc; block[0] = sign_extend(*last_dc << (12 - dcb), 12); - q = quants[get_bits(gb, 2)]; + q = quants[bitstream_read(bc, 2)]; if (q >= 128) ac_idx = HQX_AC_Q128; else if (q >= 64) @@ -142,7 +142,7 @@ static int decode_block(GetBitContext *gb, VLC *vlc, ac_idx = HQX_AC_Q0; do { - hqx_get_ac(gb, &ff_hqx_ac[ac_idx], &run, &lev); + hqx_get_ac(bc, &ff_hqx_ac[ac_idx], &run, &lev); pos += run; if (pos >= 64) break; @@ -155,24 +155,24 @@ static int decode_block(GetBitContext *gb, VLC *vlc, static int hqx_decode_422(HQXContext *ctx, int slice_no, int x, int y) { HQXSlice *slice = &ctx->slice[slice_no]; - GetBitContext *gb = &slice->gb; + BitstreamContext *bc = &slice->bc; const int *quants; int flag; int last_dc; int i, ret; if (ctx->interlaced) - flag = get_bits1(gb); + flag = bitstream_read_bit(bc); else flag = 0; - quants = hqx_quants[get_bits(gb, 4)]; + quants = hqx_quants[bitstream_read(bc, 4)]; for (i = 0; i < 8; i++) { int vlc_index = ctx->dcb - 9; if (i == 0 || i == 4 || i == 6) last_dc = 0; - ret = decode_block(gb, &ctx->dc_vlc[vlc_index], quants, + ret = decode_block(bc, &ctx->dc_vlc[vlc_index], quants, ctx->dcb, slice->block[i], &last_dc); if (ret < 0) return ret; @@ -189,14 +189,14 @@ static int hqx_decode_422(HQXContext *ctx, int slice_no, int x, int y) static int hqx_decode_422a(HQXContext *ctx, int slice_no, int x, int y) { HQXSlice *slice = &ctx->slice[slice_no]; - GetBitContext *gb = &slice->gb; + BitstreamContext *bc = &slice->bc; const int *quants; int flag = 0; int last_dc; int i, ret; int cbp; - cbp = get_vlc2(gb, ctx->cbp_vlc.table, ctx->cbp_vlc.bits, 1); + cbp = bitstream_read_vlc(bc, ctx->cbp_vlc.table, ctx->cbp_vlc.bits, 1); for (i = 0; i < 12; i++) memset(slice->block[i], 0, sizeof(**slice->block) * 64); @@ -204,9 +204,9 @@ static int hqx_decode_422a(HQXContext *ctx, int slice_no, int x, int y) slice->block[i][0] = -0x800; if (cbp) { if (ctx->interlaced) - flag = get_bits1(gb); + flag = bitstream_read_bit(bc); - quants = hqx_quants[get_bits(gb, 4)]; + quants = hqx_quants[bitstream_read(bc, 4)]; cbp |= cbp << 4; // alpha CBP if (cbp & 0x3) // chroma CBP - top @@ -218,7 +218,7 @@ static int hqx_decode_422a(HQXContext *ctx, int slice_no, int x, int y) last_dc = 0; if (cbp & (1 << i)) { int vlc_index = ctx->dcb - 9; - ret = decode_block(gb, &ctx->dc_vlc[vlc_index], quants, + ret = decode_block(bc, &ctx->dc_vlc[vlc_index], quants, ctx->dcb, slice->block[i], &last_dc); if (ret < 0) return ret; @@ -239,24 +239,24 @@ static int hqx_decode_422a(HQXContext *ctx, int slice_no, int x, int y) static int hqx_decode_444(HQXContext *ctx, int slice_no, int x, int y) { HQXSlice *slice = &ctx->slice[slice_no]; - GetBitContext *gb = &slice->gb; + BitstreamContext *bc = &slice->bc; const int *quants; int flag; int last_dc; int i, ret; if (ctx->interlaced) - flag = get_bits1(gb); + flag = bitstream_read_bit(bc); else flag = 0; - quants = hqx_quants[get_bits(gb, 4)]; + quants = hqx_quants[bitstream_read(bc, 4)]; for (i = 0; i < 12; i++) { int vlc_index = ctx->dcb - 9; if (i == 0 || i == 4 || i == 8) last_dc = 0; - ret = decode_block(gb, &ctx->dc_vlc[vlc_index], quants, + ret = decode_block(bc, &ctx->dc_vlc[vlc_index], quants, ctx->dcb, slice->block[i], &last_dc); if (ret < 0) return ret; @@ -275,14 +275,14 @@ static int hqx_decode_444(HQXContext *ctx, int slice_no, int x, int y) static int hqx_decode_444a(HQXContext *ctx, int slice_no, int x, int y) { HQXSlice *slice = &ctx->slice[slice_no]; - GetBitContext *gb = &slice->gb; + BitstreamContext *bc = &slice->bc; const int *quants; int flag = 0; int last_dc; int i, ret; int cbp; - cbp = get_vlc2(gb, ctx->cbp_vlc.table, ctx->cbp_vlc.bits, 1); + cbp = bitstream_read_vlc(bc, ctx->cbp_vlc.table, ctx->cbp_vlc.bits, 1); for (i = 0; i < 16; i++) memset(slice->block[i], 0, sizeof(**slice->block) * 64); @@ -290,9 +290,9 @@ static int hqx_decode_444a(HQXContext *ctx, int slice_no, int x, int y) slice->block[i][0] = -0x800; if (cbp) { if (ctx->interlaced) - flag = get_bits1(gb); + flag = bitstream_read_bit(bc); - quants = hqx_quants[get_bits(gb, 4)]; + quants = hqx_quants[bitstream_read(bc, 4)]; cbp |= cbp << 4; // alpha CBP cbp |= cbp << 8; // chroma CBP @@ -301,7 +301,7 @@ static int hqx_decode_444a(HQXContext *ctx, int slice_no, int x, int y) last_dc = 0; if (cbp & (1 << i)) { int vlc_index = ctx->dcb - 9; - ret = decode_block(gb, &ctx->dc_vlc[vlc_index], quants, + ret = decode_block(bc, &ctx->dc_vlc[vlc_index], quants, ctx->dcb, slice->block[i], &last_dc); if (ret < 0) return ret; @@ -392,9 +392,9 @@ static int decode_slice_thread(AVCodecContext *avctx, void *arg, return AVERROR_INVALIDDATA; } - ret = init_get_bits8(&ctx->slice[slice_no].gb, - ctx->src + slice_off[slice_no], - slice_off[slice_no + 1] - slice_off[slice_no]); + ret = bitstream_init8(&ctx->slice[slice_no].bc, + ctx->src + slice_off[slice_no], + slice_off[slice_no + 1] - slice_off[slice_no]); if (ret < 0) return ret; diff --git a/libavcodec/hqx.h b/libavcodec/hqx.h index 7f329712fd..e8f7c627fb 100644 --- a/libavcodec/hqx.h +++ b/libavcodec/hqx.h @@ -25,7 +25,8 @@ #include "libavutil/frame.h" #include "libavutil/mem.h" -#include "get_bits.h" + +#include "bitstream.h" #include "hqxdsp.h" enum HQXACMode { @@ -55,7 +56,7 @@ typedef int (*mb_decode_func)(struct HQXContext *ctx, int slice_no, int x, int y); typedef struct HQXSlice { - GetBitContext gb; + BitstreamContext bc; DECLARE_ALIGNED(16, int16_t, block)[16][64]; } HQXSlice; From 15d4dbfd4ade51eb55737f33236f142f88206d1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandra=20H=C3=A1jkov=C3=A1?= Date: Tue, 12 Apr 2016 17:24:55 +0200 Subject: [PATCH 30/35] jvdec: Convert to the new bitstream reader Signed-off-by: Anton Khirnov --- libavcodec/jvdec.c | 52 +++++++++++++++++++++++----------------------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/libavcodec/jvdec.c b/libavcodec/jvdec.c index c532b75a25..37a2770063 100644 --- a/libavcodec/jvdec.c +++ b/libavcodec/jvdec.c @@ -28,8 +28,8 @@ #include "libavutil/intreadwrite.h" #include "avcodec.h" +#include "bitstream.h" #include "blockdsp.h" -#include "get_bits.h" #include "internal.h" typedef struct JvContext { @@ -62,84 +62,84 @@ static av_cold int decode_init(AVCodecContext *avctx) /** * Decode 2x2 block */ -static inline void decode2x2(GetBitContext *gb, uint8_t *dst, int linesize) +static inline void decode2x2(BitstreamContext *bc, uint8_t *dst, int linesize) { int i, j, v[2]; - switch (get_bits(gb, 2)) { + switch (bitstream_read(bc, 2)) { case 1: - v[0] = get_bits(gb, 8); + v[0] = bitstream_read(bc, 8); for (j = 0; j < 2; j++) memset(dst + j * linesize, v[0], 2); break; case 2: - v[0] = get_bits(gb, 8); - v[1] = get_bits(gb, 8); + v[0] = bitstream_read(bc, 8); + v[1] = bitstream_read(bc, 8); for (j = 0; j < 2; j++) for (i = 0; i < 2; i++) - dst[j * linesize + i] = v[get_bits1(gb)]; + dst[j * linesize + i] = v[bitstream_read_bit(bc)]; break; case 3: for (j = 0; j < 2; j++) for (i = 0; i < 2; i++) - dst[j * linesize + i] = get_bits(gb, 8); + dst[j * linesize + i] = bitstream_read(bc, 8); } } /** * Decode 4x4 block */ -static inline void decode4x4(GetBitContext *gb, uint8_t *dst, int linesize) +static inline void decode4x4(BitstreamContext *bc, uint8_t *dst, int linesize) { int i, j, v[2]; - switch (get_bits(gb, 2)) { + switch (bitstream_read(bc, 2)) { case 1: - v[0] = get_bits(gb, 8); + v[0] = bitstream_read(bc, 8); for (j = 0; j < 4; j++) memset(dst + j * linesize, v[0], 4); break; case 2: - v[0] = get_bits(gb, 8); - v[1] = get_bits(gb, 8); + v[0] = bitstream_read(bc, 8); + v[1] = bitstream_read(bc, 8); for (j = 2; j >= 0; j -= 2) { for (i = 0; i < 4; i++) - dst[j * linesize + i] = v[get_bits1(gb)]; + dst[j * linesize + i] = v[bitstream_read_bit(bc)]; for (i = 0; i < 4; i++) - dst[(j + 1) * linesize + i] = v[get_bits1(gb)]; + dst[(j + 1) * linesize + i] = v[bitstream_read_bit(bc)]; } break; case 3: for (j = 0; j < 4; j += 2) for (i = 0; i < 4; i += 2) - decode2x2(gb, dst + j * linesize + i, linesize); + decode2x2(bc, dst + j * linesize + i, linesize); } } /** * Decode 8x8 block */ -static inline void decode8x8(GetBitContext *gb, uint8_t *dst, int linesize, +static inline void decode8x8(BitstreamContext *bc, uint8_t *dst, int linesize, BlockDSPContext *bdsp) { int i, j, v[2]; - switch (get_bits(gb, 2)) { + switch (bitstream_read(bc, 2)) { case 1: - v[0] = get_bits(gb, 8); + v[0] = bitstream_read(bc, 8); bdsp->fill_block_tab[1](dst, v[0], linesize, 8); break; case 2: - v[0] = get_bits(gb, 8); - v[1] = get_bits(gb, 8); + v[0] = bitstream_read(bc, 8); + v[1] = bitstream_read(bc, 8); for (j = 7; j >= 0; j--) for (i = 0; i < 8; i++) - dst[j * linesize + i] = v[get_bits1(gb)]; + dst[j * linesize + i] = v[bitstream_read_bit(bc)]; break; case 3: for (j = 0; j < 8; j += 4) for (i = 0; i < 8; i += 4) - decode4x4(gb, dst + j * linesize + i, linesize); + decode4x4(bc, dst + j * linesize + i, linesize); } } @@ -163,12 +163,12 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, } if (video_type == 0 || video_type == 1) { - GetBitContext gb; - init_get_bits(&gb, buf, 8 * FFMIN(video_size, buf_end - buf)); + BitstreamContext bc; + bitstream_init(&bc, buf, 8 * FFMIN(video_size, buf_end - buf)); for (j = 0; j < avctx->height; j += 8) for (i = 0; i < avctx->width; i += 8) - decode8x8(&gb, + decode8x8(&bc, s->frame->data[0] + j * s->frame->linesize[0] + i, s->frame->linesize[0], &s->bdsp); From 6f94a64bd6ae14d592835369a954e1378b79d786 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandra=20H=C3=A1jkov=C3=A1?= Date: Tue, 12 Apr 2016 18:24:27 +0200 Subject: [PATCH 31/35] nellymoser: Convert to the new bitstream reader Signed-off-by: Anton Khirnov --- libavcodec/nellymoserdec.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/libavcodec/nellymoserdec.c b/libavcodec/nellymoserdec.c index 355935f97c..390872c89a 100644 --- a/libavcodec/nellymoserdec.c +++ b/libavcodec/nellymoserdec.c @@ -38,8 +38,8 @@ #define BITSTREAM_READER_LE #include "avcodec.h" +#include "bitstream.h" #include "fft.h" -#include "get_bits.h" #include "internal.h" #include "nellymoser.h" #include "sinewin.h" @@ -48,7 +48,7 @@ typedef struct NellyMoserDecodeContext { AVCodecContext* avctx; AVLFG random_state; - GetBitContext gb; + BitstreamContext bc; float scale_bias; AVFloatDSPContext fdsp; FFTContext imdct_ctx; @@ -67,14 +67,14 @@ static void nelly_decode_block(NellyMoserDecodeContext *s, int bits[NELLY_BUF_LEN]; unsigned char v; - init_get_bits(&s->gb, block, NELLY_BLOCK_LEN * 8); + bitstream_init(&s->bc, block, NELLY_BLOCK_LEN * 8); bptr = buf; pptr = pows; - val = ff_nelly_init_table[get_bits(&s->gb, 6)]; + val = ff_nelly_init_table[bitstream_read(&s->bc, 6)]; for (i=0 ; i 0) - val += ff_nelly_delta_table[get_bits(&s->gb, 5)]; + val += ff_nelly_delta_table[bitstream_read(&s->bc, 5)]; pval = -pow(2, val/2048) * s->scale_bias; for (j = 0; j < ff_nelly_band_sizes_table[i]; j++) { *bptr++ = val; @@ -88,8 +88,8 @@ static void nelly_decode_block(NellyMoserDecodeContext *s, for (i = 0; i < 2; i++) { aptr = audio + i * NELLY_BUF_LEN; - init_get_bits(&s->gb, block, NELLY_BLOCK_LEN * 8); - skip_bits_long(&s->gb, NELLY_HEADER_BITS + i*NELLY_DETAIL_BITS); + bitstream_init(&s->bc, block, NELLY_BLOCK_LEN * 8); + bitstream_skip(&s->bc, NELLY_HEADER_BITS + i * NELLY_DETAIL_BITS); for (j = 0; j < NELLY_FILL_LEN; j++) { if (bits[j] <= 0) { @@ -97,7 +97,7 @@ static void nelly_decode_block(NellyMoserDecodeContext *s, if (av_lfg_get(&s->random_state) & 1) aptr[j] *= -1.0; } else { - v = get_bits(&s->gb, bits[j]); + v = bitstream_read(&s->bc, bits[j]); aptr[j] = ff_nelly_dequantization_table[(1< Date: Tue, 12 Apr 2016 18:28:14 +0200 Subject: [PATCH 32/35] opus: Convert to the new bitstream reader Signed-off-by: Anton Khirnov --- libavcodec/opus.h | 6 +++--- libavcodec/opusdec.c | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/libavcodec/opus.h b/libavcodec/opus.h index 55c91fa012..fbf67c9b9f 100644 --- a/libavcodec/opus.h +++ b/libavcodec/opus.h @@ -32,7 +32,7 @@ #include "libavresample/avresample.h" #include "avcodec.h" -#include "get_bits.h" +#include "bitstream.h" #define MAX_FRAME_SIZE 1275 #define MAX_FRAMES 48 @@ -92,7 +92,7 @@ typedef struct RawBitsContext { } RawBitsContext; typedef struct OpusRangeCoder { - GetBitContext gb; + BitstreamContext bc; RawBitsContext rb; unsigned int range; unsigned int value; @@ -196,7 +196,7 @@ typedef struct OpusContext { static av_always_inline void opus_rc_normalize(OpusRangeCoder *rc) { while (rc->range <= 1<<23) { - rc->value = ((rc->value << 8) | (get_bits(&rc->gb, 8) ^ 0xFF)) & ((1u << 31) - 1); + rc->value = ((rc->value << 8) | (bitstream_read(&rc->bc, 8) ^ 0xFF)) & ((1u << 31) - 1); rc->range <<= 8; rc->total_read_bits += 8; } diff --git a/libavcodec/opusdec.c b/libavcodec/opusdec.c index 92e651c787..163f0d5ed5 100644 --- a/libavcodec/opusdec.c +++ b/libavcodec/opusdec.c @@ -43,9 +43,9 @@ #include "libavresample/avresample.h" #include "avcodec.h" +#include "bitstream.h" #include "celp_filters.h" #include "fft.h" -#include "get_bits.h" #include "internal.h" #include "mathops.h" #include "opus.h" @@ -80,12 +80,12 @@ static int get_silk_samplerate(int config) */ static int opus_rc_init(OpusRangeCoder *rc, const uint8_t *data, int size) { - int ret = init_get_bits8(&rc->gb, data, size); + int ret = bitstream_init8(&rc->bc, data, size); if (ret < 0) return ret; rc->range = 128; - rc->value = 127 - get_bits(&rc->gb, 7); + rc->value = 127 - bitstream_read(&rc->bc, 7); rc->total_read_bits = 9; opus_rc_normalize(rc); From 770406d1e8291dbfd846210d08c1b322d30c8142 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandra=20H=C3=A1jkov=C3=A1?= Date: Tue, 12 Apr 2016 18:32:50 +0200 Subject: [PATCH 33/35] pcx: Convert to the new bitstream reader Signed-off-by: Anton Khirnov --- libavcodec/pcx.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/libavcodec/pcx.c b/libavcodec/pcx.c index a2d49b454d..ece885e6e0 100644 --- a/libavcodec/pcx.c +++ b/libavcodec/pcx.c @@ -23,9 +23,10 @@ */ #include "libavutil/imgutils.h" + #include "avcodec.h" +#include "bitstream.h" #include "bytestream.h" -#include "get_bits.h" #include "internal.h" #define PCX_HEADER_SIZE 128 @@ -179,15 +180,15 @@ static int pcx_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, goto end; } } else if (nplanes == 1) { /* all packed formats, max. 16 colors */ - GetBitContext s; + BitstreamContext s; for (y = 0; y < h; y++) { - init_get_bits(&s, scanline, bytes_per_scanline << 3); + bitstream_init(&s, scanline, bytes_per_scanline << 3); pcx_rle_decode(&gb, scanline, bytes_per_scanline, compressed); for (x = 0; x < w; x++) - ptr[x] = get_bits(&s, bits_per_pixel); + ptr[x] = bitstream_read(&s, bits_per_pixel); ptr += stride; } } else { /* planar, 4, 8 or 16 colors */ From 0dabd329e84a6a7b074fa55da844fc827a47aa01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandra=20H=C3=A1jkov=C3=A1?= Date: Tue, 12 Apr 2016 18:39:16 +0200 Subject: [PATCH 34/35] qcelp: Convert to the new bitstream reader Signed-off-by: Anton Khirnov --- libavcodec/qcelpdec.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/libavcodec/qcelpdec.c b/libavcodec/qcelpdec.c index e9e73475c3..9d5e13a117 100644 --- a/libavcodec/qcelpdec.c +++ b/libavcodec/qcelpdec.c @@ -31,9 +31,10 @@ #include "libavutil/channel_layout.h" #include "libavutil/float_dsp.h" + #include "avcodec.h" +#include "bitstream.h" #include "internal.h" -#include "get_bits.h" #include "qcelpdata.h" #include "celp_filters.h" #include "acelp_filters.h" @@ -53,7 +54,7 @@ typedef enum { } qcelp_packet_rate; typedef struct QCELPContext { - GetBitContext gb; + BitstreamContext bc; qcelp_packet_rate bitrate; QCELPFrame frame; /**< unpacked data frame */ @@ -718,12 +719,12 @@ static int qcelp_decode_frame(AVCodecContext *avctx, void *data, qcelp_unpacking_bitmaps_lengths[q->bitrate]; uint8_t *unpacked_data = (uint8_t *)&q->frame; - init_get_bits(&q->gb, buf, 8 * buf_size); + bitstream_init(&q->bc, buf, 8 * buf_size); memset(&q->frame, 0, sizeof(QCELPFrame)); for (; bitmaps < bitmaps_end; bitmaps++) - unpacked_data[bitmaps->index] |= get_bits(&q->gb, bitmaps->bitlen) << bitmaps->bitpos; + unpacked_data[bitmaps->index] |= bitstream_read(&q->bc, bitmaps->bitlen) << bitmaps->bitpos; // Check for erasures/blanks on rates 1, 1/4 and 1/8. if (q->frame.reserved) { From 0b5a26e8bcd219efe5da3a6d39b588fabf91f2b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandra=20H=C3=A1jkov=C3=A1?= Date: Tue, 12 Apr 2016 18:50:57 +0200 Subject: [PATCH 35/35] qdm2: Convert to the new bitstream reader Signed-off-by: Anton Khirnov --- libavcodec/qdm2.c | 200 +++++++++++++++++++++++----------------------- 1 file changed, 100 insertions(+), 100 deletions(-) diff --git a/libavcodec/qdm2.c b/libavcodec/qdm2.c index 7a7c149c2b..781999aa10 100644 --- a/libavcodec/qdm2.c +++ b/libavcodec/qdm2.c @@ -39,7 +39,7 @@ #define BITSTREAM_READER_LE #include "avcodec.h" -#include "get_bits.h" +#include "bitstream.h" #include "internal.h" #include "mpegaudio.h" #include "mpegaudiodsp.h" @@ -361,31 +361,31 @@ static av_cold void qdm2_init_vlc(void) INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE); } -static int qdm2_get_vlc(GetBitContext *gb, VLC *vlc, int flag, int depth) +static int qdm2_get_vlc(BitstreamContext *bc, VLC *vlc, int flag, int depth) { int value; - value = get_vlc2(gb, vlc->table, vlc->bits, depth); + value = bitstream_read_vlc(bc, vlc->table, vlc->bits, depth); /* stage-2, 3 bits exponent escape sequence */ if (value-- == 0) - value = get_bits(gb, get_bits(gb, 3) + 1); + value = bitstream_read(bc, bitstream_read(bc, 3) + 1); /* stage-3, optional */ if (flag) { int tmp = vlc_stage3_values[value]; if ((value & ~3) > 0) - tmp += get_bits(gb, (value >> 2)); + tmp += bitstream_read(bc, value >> 2); value = tmp; } return value; } -static int qdm2_get_se_vlc(VLC *vlc, GetBitContext *gb, int depth) +static int qdm2_get_se_vlc(VLC *vlc, BitstreamContext *bc, int depth) { - int value = qdm2_get_vlc(gb, vlc, 0, depth); + int value = qdm2_get_vlc(bc, vlc, 0, depth); return (value & 1) ? ((value + 1) >> 1) : -(value >> 1); } @@ -412,35 +412,35 @@ static uint16_t qdm2_packet_checksum(const uint8_t *data, int length, int value) /** * Fill a QDM2SubPacket structure with packet type, size, and data pointer. * - * @param gb bitreader context + * @param bc bitreader context * @param sub_packet packet under analysis */ -static void qdm2_decode_sub_packet_header(GetBitContext *gb, +static void qdm2_decode_sub_packet_header(BitstreamContext *bc, QDM2SubPacket *sub_packet) { - sub_packet->type = get_bits(gb, 8); + sub_packet->type = bitstream_read(bc, 8); if (sub_packet->type == 0) { sub_packet->size = 0; sub_packet->data = NULL; } else { - sub_packet->size = get_bits(gb, 8); + sub_packet->size = bitstream_read(bc, 8); if (sub_packet->type & 0x80) { sub_packet->size <<= 8; - sub_packet->size |= get_bits(gb, 8); + sub_packet->size |= bitstream_read(bc, 8); sub_packet->type &= 0x7f; } if (sub_packet->type == 0x7f) - sub_packet->type |= (get_bits(gb, 8) << 8); + sub_packet->type |= bitstream_read(bc, 8) << 8; // FIXME: this depends on bitreader-internal data - sub_packet->data = &gb->buffer[get_bits_count(gb) / 8]; + sub_packet->data = &bc->buffer[bitstream_tell(bc) / 8]; } av_log(NULL, AV_LOG_DEBUG, "Subpacket: type=%d size=%d start_offs=%x\n", - sub_packet->type, sub_packet->size, get_bits_count(gb) / 8); + sub_packet->type, sub_packet->size, bitstream_tell(bc) / 8); } /** @@ -799,12 +799,12 @@ static void fill_coding_method_array(sb_int8_array tone_level_idx, * sb 8-sb_used. * * @param q context - * @param gb bitreader context + * @param bc bitreader context * @param length packet length in bits * @param sb_min lower subband processed (sb_min included) * @param sb_max higher subband processed (sb_max excluded) */ -static void synthfilt_build_sb_samples(QDM2Context *q, GetBitContext *gb, +static void synthfilt_build_sb_samples(QDM2Context *q, BitstreamContext *bc, int length, int sb_min, int sb_max) { int sb, j, k, n, ch, run, channels; @@ -830,12 +830,12 @@ static void synthfilt_build_sb_samples(QDM2Context *q, GetBitContext *gb, else if (sb >= 24) joined_stereo = 1; else - joined_stereo = (get_bits_left(gb) >= 1) ? get_bits1(gb) : 0; + joined_stereo = (bitstream_bits_left(bc) >= 1) ? bitstream_read_bit(bc) : 0; if (joined_stereo) { - if (get_bits_left(gb) >= 16) + if (bitstream_bits_left(bc) >= 16) for (j = 0; j < 16; j++) - sign_bits[j] = get_bits1(gb); + sign_bits[j] = bitstream_read_bit(bc); for (j = 0; j < 64; j++) if (q->coding_method[1][sb][j] > q->coding_method[0][sb][j]) @@ -851,22 +851,22 @@ static void synthfilt_build_sb_samples(QDM2Context *q, GetBitContext *gb, for (ch = 0; ch < channels; ch++) { FIX_NOISE_IDX(q->noise_idx); - zero_encoding = (get_bits_left(gb) >= 1) ? get_bits1(gb) : 0; + zero_encoding = (bitstream_bits_left(bc) >= 1) ? bitstream_read_bit(bc) : 0; type34_predictor = 0.0; type34_first = 1; for (j = 0; j < 128; ) { switch (q->coding_method[ch][sb][j / 2]) { case 8: - if (get_bits_left(gb) >= 10) { + if (bitstream_bits_left(bc) >= 10) { if (zero_encoding) { for (k = 0; k < 5; k++) { if ((j + 2 * k) >= 128) break; - samples[2 * k] = get_bits1(gb) ? dequant_1bit[joined_stereo][2 * get_bits1(gb)] : 0; + samples[2 * k] = bitstream_read_bit(bc) ? dequant_1bit[joined_stereo][2 * bitstream_read_bit(bc)] : 0; } } else { - n = get_bits(gb, 8); + n = bitstream_read(bc, 8); for (k = 0; k < 5; k++) samples[2 * k] = dequant_1bit[joined_stereo][random_dequant_index[n][k]]; } @@ -880,10 +880,10 @@ static void synthfilt_build_sb_samples(QDM2Context *q, GetBitContext *gb, break; case 10: - if (get_bits_left(gb) >= 1) { + if (bitstream_bits_left(bc) >= 1) { float f = 0.81; - if (get_bits1(gb)) + if (bitstream_read_bit(bc)) f = -f; f -= noise_samples[((sb + 1) * (j +5 * ch + 1)) & 127] * 9.0 / 40.0; samples[0] = f; @@ -894,15 +894,15 @@ static void synthfilt_build_sb_samples(QDM2Context *q, GetBitContext *gb, break; case 16: - if (get_bits_left(gb) >= 10) { + if (bitstream_bits_left(bc) >= 10) { if (zero_encoding) { for (k = 0; k < 5; k++) { if ((j + k) >= 128) break; - samples[k] = (get_bits1(gb) == 0) ? 0 : dequant_1bit[joined_stereo][2 * get_bits1(gb)]; + samples[k] = (bitstream_read_bit(bc) == 0) ? 0 : dequant_1bit[joined_stereo][2 * bitstream_read_bit(bc)]; } } else { - n = get_bits (gb, 8); + n = bitstream_read (bc, 8); for (k = 0; k < 5; k++) samples[k] = dequant_1bit[joined_stereo][random_dequant_index[n][k]]; } @@ -914,8 +914,8 @@ static void synthfilt_build_sb_samples(QDM2Context *q, GetBitContext *gb, break; case 24: - if (get_bits_left(gb) >= 7) { - n = get_bits(gb, 7); + if (bitstream_bits_left(bc) >= 7) { + n = bitstream_read(bc, 7); for (k = 0; k < 3; k++) samples[k] = (random_dequant_type24[n][k] - 2.0) * 0.5; } else { @@ -926,8 +926,8 @@ static void synthfilt_build_sb_samples(QDM2Context *q, GetBitContext *gb, break; case 30: - if (get_bits_left(gb) >= 4) { - unsigned index = qdm2_get_vlc(gb, &vlc_tab_type30, 0, 1); + if (bitstream_bits_left(bc) >= 4) { + unsigned index = qdm2_get_vlc(bc, &vlc_tab_type30, 0, 1); if (index < FF_ARRAY_ELEMS(type30_dequant)) { samples[0] = type30_dequant[index]; } else @@ -939,14 +939,14 @@ static void synthfilt_build_sb_samples(QDM2Context *q, GetBitContext *gb, break; case 34: - if (get_bits_left(gb) >= 7) { + if (bitstream_bits_left(bc) >= 7) { if (type34_first) { - type34_div = (float)(1 << get_bits(gb, 2)); - samples[0] = ((float)get_bits(gb, 5) - 16.0) / 15.0; + type34_div = (float)(1 << bitstream_read(bc, 2)); + samples[0] = ((float)bitstream_read(bc, 5) - 16.0) / 15.0; type34_predictor = samples[0]; type34_first = 0; } else { - unsigned index = qdm2_get_vlc(gb, &vlc_tab_type34, 0, 1); + unsigned index = qdm2_get_vlc(bc, &vlc_tab_type34, 0, 1); if (index < FF_ARRAY_ELEMS(type34_delta)) { samples[0] = type34_delta[index] / type34_div + type34_predictor; type34_predictor = samples[0]; @@ -998,27 +998,27 @@ static void synthfilt_build_sb_samples(QDM2Context *q, GetBitContext *gb, * same VLC tables as process_subpacket_9 are used. * * @param quantized_coeffs pointer to quantized_coeffs[ch][0] - * @param gb bitreader context + * @param bc bitreader context */ static void init_quantized_coeffs_elem0(int8_t *quantized_coeffs, - GetBitContext *gb) + BitstreamContext *bc) { int i, k, run, level, diff; - if (get_bits_left(gb) < 16) + if (bitstream_bits_left(bc) < 16) return; - level = qdm2_get_vlc(gb, &vlc_tab_level, 0, 2); + level = qdm2_get_vlc(bc, &vlc_tab_level, 0, 2); quantized_coeffs[0] = level; for (i = 0; i < 7; ) { - if (get_bits_left(gb) < 16) + if (bitstream_bits_left(bc) < 16) break; - run = qdm2_get_vlc(gb, &vlc_tab_run, 0, 1) + 1; + run = qdm2_get_vlc(bc, &vlc_tab_run, 0, 1) + 1; - if (get_bits_left(gb) < 16) + if (bitstream_bits_left(bc) < 16) break; - diff = qdm2_get_se_vlc(&vlc_tab_diff, gb, 2); + diff = qdm2_get_se_vlc(&vlc_tab_diff, bc, 2); for (k = 1; k <= run; k++) quantized_coeffs[i + k] = (level + ((k * diff) / run)); @@ -1035,16 +1035,16 @@ static void init_quantized_coeffs_elem0(int8_t *quantized_coeffs, * data from packet 10 * * @param q context - * @param gb bitreader context + * @param bc bitreader context */ -static void init_tone_level_dequantization(QDM2Context *q, GetBitContext *gb) +static void init_tone_level_dequantization(QDM2Context *q, BitstreamContext *bc) { int sb, j, k, n, ch; for (ch = 0; ch < q->nb_channels; ch++) { - init_quantized_coeffs_elem0(q->quantized_coeffs[ch][0], gb); + init_quantized_coeffs_elem0(q->quantized_coeffs[ch][0], bc); - if (get_bits_left(gb) < 16) { + if (bitstream_bits_left(bc) < 16) { memset(q->quantized_coeffs[ch][0], 0, 8); break; } @@ -1055,13 +1055,13 @@ static void init_tone_level_dequantization(QDM2Context *q, GetBitContext *gb) for (sb = 0; sb < n; sb++) for (ch = 0; ch < q->nb_channels; ch++) for (j = 0; j < 8; j++) { - if (get_bits_left(gb) < 1) + if (bitstream_bits_left(bc) < 1) break; - if (get_bits1(gb)) { + if (bitstream_read_bit(bc)) { for (k=0; k < 8; k++) { - if (get_bits_left(gb) < 16) + if (bitstream_bits_left(bc) < 16) break; - q->tone_level_idx_hi1[ch][sb][j][k] = qdm2_get_vlc(gb, &vlc_tab_tone_level_idx_hi1, 0, 2); + q->tone_level_idx_hi1[ch][sb][j][k] = qdm2_get_vlc(bc, &vlc_tab_tone_level_idx_hi1, 0, 2); } } else { for (k=0; k < 8; k++) @@ -1073,9 +1073,9 @@ static void init_tone_level_dequantization(QDM2Context *q, GetBitContext *gb) for (sb = 0; sb < n; sb++) for (ch = 0; ch < q->nb_channels; ch++) { - if (get_bits_left(gb) < 16) + if (bitstream_bits_left(bc) < 16) break; - q->tone_level_idx_hi2[ch][sb] = qdm2_get_vlc(gb, &vlc_tab_tone_level_idx_hi2, 0, 2); + q->tone_level_idx_hi2[ch][sb] = qdm2_get_vlc(bc, &vlc_tab_tone_level_idx_hi2, 0, 2); if (sb > 19) q->tone_level_idx_hi2[ch][sb] -= 16; else @@ -1088,9 +1088,9 @@ static void init_tone_level_dequantization(QDM2Context *q, GetBitContext *gb) for (sb = 0; sb < n; sb++) for (ch = 0; ch < q->nb_channels; ch++) for (j = 0; j < 8; j++) { - if (get_bits_left(gb) < 16) + if (bitstream_bits_left(bc) < 16) break; - q->tone_level_idx_mid[ch][sb][j] = qdm2_get_vlc(gb, &vlc_tab_tone_level_idx_mid, 0, 2) - 32; + q->tone_level_idx_mid[ch][sb][j] = qdm2_get_vlc(bc, &vlc_tab_tone_level_idx_mid, 0, 2) - 32; } } @@ -1102,21 +1102,21 @@ static void init_tone_level_dequantization(QDM2Context *q, GetBitContext *gb) */ static void process_subpacket_9(QDM2Context *q, QDM2SubPNode *node) { - GetBitContext gb; + BitstreamContext bc; int i, j, k, n, ch, run, level, diff; - init_get_bits(&gb, node->packet->data, node->packet->size * 8); + bitstream_init(&bc, node->packet->data, node->packet->size * 8); n = coeff_per_sb_for_avg[q->coeff_per_sb_select][QDM2_SB_USED(q->sub_sampling) - 1] + 1; for (i = 1; i < n; i++) for (ch = 0; ch < q->nb_channels; ch++) { - level = qdm2_get_vlc(&gb, &vlc_tab_level, 0, 2); + level = qdm2_get_vlc(&bc, &vlc_tab_level, 0, 2); q->quantized_coeffs[ch][i][0] = level; for (j = 0; j < (8 - 1); ) { - run = qdm2_get_vlc(&gb, &vlc_tab_run, 0, 1) + 1; - diff = qdm2_get_se_vlc(&vlc_tab_diff, &gb, 2); + run = qdm2_get_vlc(&bc, &vlc_tab_run, 0, 1) + 1; + diff = qdm2_get_se_vlc(&vlc_tab_diff, &bc, 2); for (k = 1; k <= run; k++) q->quantized_coeffs[ch][i][j + k] = (level + ((k * diff) / run)); @@ -1139,11 +1139,11 @@ static void process_subpacket_9(QDM2Context *q, QDM2SubPNode *node) */ static void process_subpacket_10(QDM2Context *q, QDM2SubPNode *node) { - GetBitContext gb; + BitstreamContext bc; if (node) { - init_get_bits(&gb, node->packet->data, node->packet->size * 8); - init_tone_level_dequantization(q, &gb); + bitstream_init(&bc, node->packet->data, node->packet->size * 8); + init_tone_level_dequantization(q, &bc); fill_tone_level_array(q, 1); } else { fill_tone_level_array(q, 0); @@ -1158,16 +1158,16 @@ static void process_subpacket_10(QDM2Context *q, QDM2SubPNode *node) */ static void process_subpacket_11(QDM2Context *q, QDM2SubPNode *node) { - GetBitContext gb; + BitstreamContext bc; int length = 0; if (node) { length = node->packet->size * 8; - init_get_bits(&gb, node->packet->data, length); + bitstream_init(&bc, node->packet->data, length); } if (length >= 32) { - int c = get_bits(&gb, 13); + int c = bitstream_read(&bc, 13); if (c > 3) fill_coding_method_array(q->tone_level_idx, @@ -1176,7 +1176,7 @@ static void process_subpacket_11(QDM2Context *q, QDM2SubPNode *node) q->superblocktype_2_3, q->cm_table_select); } - synthfilt_build_sb_samples(q, &gb, length, 0, 8); + synthfilt_build_sb_samples(q, &bc, length, 0, 8); } /** @@ -1187,15 +1187,15 @@ static void process_subpacket_11(QDM2Context *q, QDM2SubPNode *node) */ static void process_subpacket_12(QDM2Context *q, QDM2SubPNode *node) { - GetBitContext gb; + BitstreamContext bc; int length = 0; if (node) { length = node->packet->size * 8; - init_get_bits(&gb, node->packet->data, length); + bitstream_init(&bc, node->packet->data, length); } - synthfilt_build_sb_samples(q, &gb, length, 8, QDM2_SB_USED(q->sub_sampling)); + synthfilt_build_sb_samples(q, &bc, length, 8, QDM2_SB_USED(q->sub_sampling)); } /* @@ -1238,7 +1238,7 @@ static void process_synthesis_subpackets(QDM2Context *q, QDM2SubPNode *list) */ static void qdm2_decode_super_block(QDM2Context *q) { - GetBitContext gb; + BitstreamContext bc; QDM2SubPacket header, *packet; int i, packet_bytes, sub_packet_size, sub_packets_D; unsigned int next_index = 0; @@ -1252,8 +1252,8 @@ static void qdm2_decode_super_block(QDM2Context *q) average_quantized_coeffs(q); // average elements in quantized_coeffs[max_ch][10][8] - init_get_bits(&gb, q->compressed_data, q->compressed_size * 8); - qdm2_decode_sub_packet_header(&gb, &header); + bitstream_init(&bc, q->compressed_data, q->compressed_size * 8); + qdm2_decode_sub_packet_header(&bc, &header); if (header.type < 2 || header.type >= 8) { q->has_errors = 1; @@ -1262,13 +1262,13 @@ static void qdm2_decode_super_block(QDM2Context *q) } q->superblocktype_2_3 = (header.type == 2 || header.type == 3); - packet_bytes = (q->compressed_size - get_bits_count(&gb) / 8); + packet_bytes = (q->compressed_size - bitstream_tell(&bc) / 8); - init_get_bits(&gb, header.data, header.size * 8); + bitstream_init(&bc, header.data, header.size * 8); if (header.type == 2 || header.type == 4 || header.type == 5) { - int csum = 257 * get_bits(&gb, 8); - csum += 2 * get_bits(&gb, 8); + int csum = 257 * bitstream_read(&bc, 8); + csum += 2 * bitstream_read(&bc, 8); csum = qdm2_packet_checksum(q->compressed_data, q->checksum_size, csum); @@ -1300,8 +1300,8 @@ static void qdm2_decode_super_block(QDM2Context *q) q->sub_packet_list_A[i - 1].next = &q->sub_packet_list_A[i]; /* seek to next block */ - init_get_bits(&gb, header.data, header.size * 8); - skip_bits(&gb, next_index * 8); + bitstream_init(&bc, header.data, header.size * 8); + bitstream_skip(&bc, next_index * 8); if (next_index >= header.size) break; @@ -1309,8 +1309,8 @@ static void qdm2_decode_super_block(QDM2Context *q) /* decode subpacket */ packet = &q->sub_packets[i]; - qdm2_decode_sub_packet_header(&gb, packet); - next_index = packet->size + get_bits_count(&gb) / 8; + qdm2_decode_sub_packet_header(&bc, packet); + next_index = packet->size + bitstream_tell(&bc) / 8; sub_packet_size = ((packet->size > 0xff) ? 1 : 0) + packet->size + 2; if (packet->type == 0) @@ -1336,10 +1336,10 @@ static void qdm2_decode_super_block(QDM2Context *q) QDM2_LIST_ADD(q->sub_packet_list_D, sub_packets_D, packet); } else if (packet->type == 13) { for (j = 0; j < 6; j++) - q->fft_level_exp[j] = get_bits(&gb, 6); + q->fft_level_exp[j] = bitstream_read(&bc, 6); } else if (packet->type == 14) { for (j = 0; j < 6; j++) - q->fft_level_exp[j] = qdm2_get_vlc(&gb, &fft_level_exp_vlc, 0, 2); + q->fft_level_exp[j] = qdm2_get_vlc(&bc, &fft_level_exp_vlc, 0, 2); } else if (packet->type == 15) { SAMPLES_NEEDED_2("packet type 15") return; @@ -1377,7 +1377,7 @@ static void qdm2_fft_init_coefficient(QDM2Context *q, int sub_packet, } static void qdm2_fft_decode_tones(QDM2Context *q, int duration, - GetBitContext *gb, int b) + BitstreamContext *bc, int b) { int channel, stereo, phase, exp; int local_int_4, local_int_8, stereo_phase, local_int_10; @@ -1393,7 +1393,7 @@ static void qdm2_fft_decode_tones(QDM2Context *q, int duration, while (1) { if (q->superblocktype_2_3) { - while ((n = qdm2_get_vlc(gb, &vlc_tab_fft_tone_offset[local_int_8], 1, 2)) < 2) { + while ((n = qdm2_get_vlc(bc, &vlc_tab_fft_tone_offset[local_int_8], 1, 2)) < 2) { offset = 1; if (n == 0) { local_int_4 += local_int_10; @@ -1405,7 +1405,7 @@ static void qdm2_fft_decode_tones(QDM2Context *q, int duration, } offset += (n - 2); } else { - offset += qdm2_get_vlc(gb, &vlc_tab_fft_tone_offset[local_int_8], 1, 2); + offset += qdm2_get_vlc(bc, &vlc_tab_fft_tone_offset[local_int_8], 1, 2); while (offset >= (local_int_10 - 1)) { offset += (1 - (local_int_10 - 1)); local_int_4 += local_int_10; @@ -1421,24 +1421,24 @@ static void qdm2_fft_decode_tones(QDM2Context *q, int duration, return; if (q->nb_channels > 1) { - channel = get_bits1(gb); - stereo = get_bits1(gb); + channel = bitstream_read_bit(bc); + stereo = bitstream_read_bit(bc); } else { channel = 0; stereo = 0; } - exp = qdm2_get_vlc(gb, (b ? &fft_level_exp_vlc : &fft_level_exp_alt_vlc), 0, 2); + exp = qdm2_get_vlc(bc, (b ? &fft_level_exp_vlc : &fft_level_exp_alt_vlc), 0, 2); exp += q->fft_level_exp[fft_level_index_table[local_int_14]]; exp = (exp < 0) ? 0 : exp; - phase = get_bits(gb, 3); + phase = bitstream_read(bc, 3); stereo_exp = 0; stereo_phase = 0; if (stereo) { - stereo_exp = (exp - qdm2_get_vlc(gb, &fft_stereo_exp_vlc, 0, 1)); - stereo_phase = (phase - qdm2_get_vlc(gb, &fft_stereo_phase_vlc, 0, 1)); + stereo_exp = (exp - qdm2_get_vlc(bc, &fft_stereo_exp_vlc, 0, 1)); + stereo_phase = (phase - qdm2_get_vlc(bc, &fft_stereo_phase_vlc, 0, 1)); if (stereo_phase < 0) stereo_phase += 8; } @@ -1460,7 +1460,7 @@ static void qdm2_fft_decode_tones(QDM2Context *q, int duration, static void qdm2_decode_fft_packets(QDM2Context *q) { int i, j, min, max, value, type, unknown_flag; - GetBitContext gb; + BitstreamContext bc; if (!q->sub_packet_list_B[0].packet) return; @@ -1495,7 +1495,7 @@ static void qdm2_decode_fft_packets(QDM2Context *q) return; /* decode FFT tones */ - init_get_bits(&gb, packet->data, packet->size * 8); + bitstream_init(&bc, packet->data, packet->size * 8); if (packet->type >= 32 && packet->type < 48 && !fft_subpackets[packet->type - 16]) unknown_flag = 1; @@ -1508,15 +1508,15 @@ static void qdm2_decode_fft_packets(QDM2Context *q) int duration = q->sub_sampling + 5 - (type & 15); if (duration >= 0 && duration < 4) - qdm2_fft_decode_tones(q, duration, &gb, unknown_flag); + qdm2_fft_decode_tones(q, duration, &bc, unknown_flag); } else if (type == 31) { for (j = 0; j < 4; j++) - qdm2_fft_decode_tones(q, j, &gb, unknown_flag); + qdm2_fft_decode_tones(q, j, &bc, unknown_flag); } else if (type == 46) { for (j = 0; j < 6; j++) - q->fft_level_exp[j] = get_bits(&gb, 6); + q->fft_level_exp[j] = bitstream_read(&bc, 6); for (j = 0; j < 4; j++) - qdm2_fft_decode_tones(q, j, &gb, unknown_flag); + qdm2_fft_decode_tones(q, j, &bc, unknown_flag); } } // Loop on B packets