* Copyright (c) 2009 Konstantin Shishkov
* Copyright (C) 2011 Peter Ross <pross@xvid.org>
*
- * This file is part of FFmpeg.
+ * This file is part of Libav.
*
- * FFmpeg is free software; you can redistribute it and/or
+ * 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.
*
- * FFmpeg is distributed in the hope that it will be useful,
+ * 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 FFmpeg; if not, write to the Free Software
+ * License along with Libav; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
-#include "libavcore/imgutils.h"
+#include "libavutil/imgutils.h"
#include "avcodec.h"
#include "dsputil.h"
#include "binkdata.h"
+#include "binkdsp.h"
#include "mathops.h"
-#define ALT_BITSTREAM_READER_LE
+#define BITSTREAM_READER_LE
#include "get_bits.h"
#define BINK_FLAG_ALPHA 0x00100000
0, 0, 0, 1, 1, 0, 1, 0, 0, 0
};
-static uint32_t binkb_intra_quant[16][64];
-static uint32_t binkb_inter_quant[16][64];
+static int32_t binkb_intra_quant[16][64];
+static int32_t binkb_inter_quant[16][64];
/**
* IDs for different data types used in Bink video codec
typedef struct BinkContext {
AVCodecContext *avctx;
DSPContext dsp;
+ BinkDSPContext bdsp;
AVFrame pic, last;
int version; ///< internal Bink file version
int has_alpha;
int swap_planes;
- ScanTable scantable; ///< permutated scantable for DCT coeffs decoding
Bundle bundle[BINKB_NB_SRC]; ///< bundles for decoding all data types
Tree col_high[16]; ///< trees for decoding high nibble in "colours" data type
*/
static void init_lengths(BinkContext *c, int width, int bw)
{
+ width = FFALIGN(width, 8);
+
c->bundle[BINK_SRC_BLOCK_TYPES].len = av_log2((width >> 3) + 511) + 1;
c->bundle[BINK_SRC_SUB_BLOCK_TYPES].len = av_log2((width >> 4) + 511) + 1;
tree->syms[i] = get_bits(gb, 4);
tmp1[tree->syms[i]] = 1;
}
- for (i = 0; i < 16; i++)
+ for (i = 0; i < 16 && len < 16 - 1; i++)
if (!tmp1[i])
tree->syms[++len] = i;
} else {
memset(b->cur_dec, v, t);
b->cur_dec += t;
} else {
- do {
+ while (b->cur_dec < dec_end) {
v = GET_HUFF(gb, b->tree);
if (v) {
sign = -get_bits1(gb);
v = (v ^ sign) - sign;
}
*b->cur_dec++ = v;
- } while (b->cur_dec < dec_end);
+ }
}
return 0;
}
memset(b->cur_dec, v, t);
b->cur_dec += t;
} else {
- do {
+ while (b->cur_dec < dec_end) {
v = GET_HUFF(gb, b->tree);
if (v < 12) {
last = v;
} else {
int run = bink_rlelens[v - 12];
+ if (dec_end - b->cur_dec < run)
+ return -1;
memset(b->cur_dec, last, run);
b->cur_dec += run;
}
- } while (b->cur_dec < dec_end);
+ }
}
return 0;
}
int start_bits, int has_sign)
{
int i, j, len, len2, bsize, sign, v, v2;
- int16_t *dst = (int16_t*)b->cur_dec;
+ 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);
sign = -get_bits1(gb);
v = (v ^ sign) - sign;
}
+ if (dst_end - dst < 1)
+ return -1;
*dst++ = v;
len--;
for (i = 0; i < len; i += 8) {
len2 = FFMIN(len - i, 8);
+ if (dst_end - dst < len2)
+ return -1;
bsize = get_bits(gb, 4);
if (bsize) {
for (j = 0; j < len2; j++) {
int i, len;
CHECK_READ_VAL(gb, b, len);
+ if (b->data_end - b->cur_dec < len * (1 + (bits > 8)))
+ return -1;
if (bits <= 8) {
if (!issigned) {
for (i = 0; i < len; i++)
* @param quant_matrices quantization matrices
* @return 0 for success, negative value in other cases
*/
-static int read_dct_coeffs(GetBitContext *gb, DCTELEM block[64], const uint8_t *scan,
- const uint32_t quant_matrices[16][64], int q)
+static int read_dct_coeffs(GetBitContext *gb, 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, mask, bits, ccoef, mode, sign;
+ int i, t, bits, ccoef, mode, sign;
int list_start = 64, list_end = 64, list_pos;
int coef_count = 0;
int coef_idx[64];
int quant_idx;
- const uint32_t *quant;
+ const int32_t *quant;
coef_list[list_end] = 4; mode_list[list_end++] = 0;
coef_list[list_end] = 24; mode_list[list_end++] = 0;
coef_list[list_end] = 2; mode_list[list_end++] = 3;
coef_list[list_end] = 3; mode_list[list_end++] = 3;
- bits = get_bits(gb, 4) - 1;
- for (mask = 1 << bits; bits >= 0; mask >>= 1, bits--) {
+ for (bits = get_bits(gb, 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)) {
coef_list[--list_start] = ccoef;
mode_list[ list_start] = 3;
} else {
- int t;
if (!bits) {
t = 1 - (get_bits1(gb) << 1);
} else {
- t = get_bits(gb, bits) | mask;
+ t = get_bits(gb, bits) | 1 << bits;
sign = -get_bits1(gb);
t = (t ^ sign) - sign;
}
if (!bits) {
t = 1 - (get_bits1(gb) << 1);
} else {
- t = get_bits(gb, bits) | mask;
+ t = get_bits(gb, bits) | 1 << bits;
sign = -get_bits1(gb);
t = (t ^ sign) - sign;
}
int v, col[2];
const uint8_t *scan;
int xoff, yoff;
- DECLARE_ALIGNED(16, DCTELEM, block[64]);
+ LOCAL_ALIGNED_16(DCTELEM, block, [64]);
+ LOCAL_ALIGNED_16(int32_t, dctblock, [64]);
int coordmap[64];
int ybias = is_key ? -15 : 0;
int qp;
dst[coordmap[*scan++]] = binkb_get_value(c, BINKB_SRC_COLORS);
break;
case 2:
- c->dsp.clear_block(block);
- block[0] = binkb_get_value(c, BINKB_SRC_INTRA_DC);
+ 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, block, c->scantable.permutated, binkb_intra_quant, qp);
- c->dsp.idct_put(dst, stride, block);
+ read_dct_coeffs(gb, dctblock, bink_scan, binkb_intra_quant, qp);
+ c->bdsp.idct_put(dst, stride, dctblock);
break;
case 3:
xoff = binkb_get_value(c, BINKB_SRC_X_OFF);
} else {
put_pixels8x8_overlapped(dst, ref, stride);
}
- c->dsp.clear_block(block);
- block[0] = binkb_get_value(c, BINKB_SRC_INTER_DC);
+ 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, block, c->scantable.permutated, binkb_inter_quant, qp);
- c->dsp.idct_add(dst, stride, block);
+ read_dct_coeffs(gb, dctblock, bink_scan, binkb_inter_quant, qp);
+ c->bdsp.idct_add(dst, stride, dctblock);
break;
case 5:
v = binkb_get_value(c, BINKB_SRC_COLORS);
int xoff, yoff;
LOCAL_ALIGNED_16(DCTELEM, block, [64]);
LOCAL_ALIGNED_16(uint8_t, ublock, [64]);
+ LOCAL_ALIGNED_16(int32_t, dctblock, [64]);
int coordmap[64];
const int stride = c->pic.linesize[plane_idx];
for (i = 0; i < BINK_NB_SRC; i++)
read_bundle(gb, c, i);
- ref_start = c->last.data[plane_idx];
- ref_end = c->last.data[plane_idx]
+ ref_start = c->last.data[plane_idx] ? c->last.data[plane_idx]
+ : c->pic.data[plane_idx];
+ ref_end = ref_start
+ (bw - 1 + c->last.linesize[plane_idx] * (bh - 1)) * 8;
for (i = 0; i < 64; i++)
if (by == bh)
break;
dst = c->pic.data[plane_idx] + 8*by*stride;
- prev = c->last.data[plane_idx] + 8*by*stride;
+ prev = (c->last.data[plane_idx] ? c->last.data[plane_idx]
+ : c->pic.data[plane_idx]) + 8*by*stride;
for (bx = 0; bx < bw; bx++, dst += 8, prev += 8) {
blk = get_value(c, BINK_SRC_BLOCK_TYPES);
// 16x16 block type on odd line means part of the already decoded block, so skip it
ublock[*scan++] = get_value(c, BINK_SRC_COLORS);
break;
case INTRA_BLOCK:
- c->dsp.clear_block(block);
- block[0] = get_value(c, BINK_SRC_INTRA_DC);
- read_dct_coeffs(gb, block, c->scantable.permutated, bink_intra_quant, -1);
- c->dsp.idct(block);
- c->dsp.put_pixels_nonclamped(block, ublock, 8);
+ 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);
+ c->bdsp.idct_put(ublock, 8, dctblock);
break;
case FILL_BLOCK:
v = get_value(c, BINK_SRC_COLORS);
return -1;
}
if (blk != FILL_BLOCK)
- c->dsp.scale_block(ublock, dst, stride);
+ c->bdsp.scale_block(ublock, dst, stride);
bx++;
dst += 8;
prev += 8;
c->dsp.add_pixels8(dst, block, stride);
break;
case INTRA_BLOCK:
- c->dsp.clear_block(block);
- block[0] = get_value(c, BINK_SRC_INTRA_DC);
- read_dct_coeffs(gb, block, c->scantable.permutated, bink_intra_quant, -1);
- c->dsp.idct_put(dst, stride, 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);
+ c->bdsp.idct_put(dst, stride, dctblock);
break;
case FILL_BLOCK:
v = get_value(c, BINK_SRC_COLORS);
yoff = get_value(c, BINK_SRC_Y_OFF);
ref = prev + xoff + yoff * stride;
c->dsp.put_pixels_tab[1][0](dst, ref, stride, 8);
- c->dsp.clear_block(block);
- block[0] = get_value(c, BINK_SRC_INTER_DC);
- read_dct_coeffs(gb, block, c->scantable.permutated, bink_inter_quant, -1);
- c->dsp.idct_add(dst, stride, block);
+ 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);
+ c->bdsp.idct_add(dst, stride, dctblock);
break;
case PATTERN_BLOCK:
for (i = 0; i < 2; i++)
/**
* Caclulate quantization tables for version b
*/
-static av_cold void binkb_calc_quant()
+static av_cold void binkb_calc_quant(void)
{
uint8_t inv_bink_scan[64];
double s[64];
avctx->pix_fmt = c->has_alpha ? PIX_FMT_YUVA420P : PIX_FMT_YUV420P;
avctx->idct_algo = FF_IDCT_BINK;
- dsputil_init(&c->dsp, avctx);
- ff_init_scantable(c->dsp.idct_permutation, &c->scantable, bink_scan);
+ ff_dsputil_init(&c->dsp, avctx);
+ ff_binkdsp_init(&c->bdsp);
init_bundles(c);
}
AVCodec ff_bink_decoder = {
- "binkvideo",
- AVMEDIA_TYPE_VIDEO,
- CODEC_ID_BINKVIDEO,
- sizeof(BinkContext),
- decode_init,
- NULL,
- decode_end,
- decode_frame,
+ .name = "binkvideo",
+ .type = AVMEDIA_TYPE_VIDEO,
+ .id = CODEC_ID_BINKVIDEO,
+ .priv_data_size = sizeof(BinkContext),
+ .init = decode_init,
+ .close = decode_end,
+ .decode = decode_frame,
.long_name = NULL_IF_CONFIG_SMALL("Bink video"),
};