2010-02-21 15:28:46 +02:00
|
|
|
/*
|
|
|
|
* Bink video decoder
|
|
|
|
* Copyright (c) 2009 Konstantin Shishkov
|
2011-02-11 14:31:32 +02:00
|
|
|
* Copyright (C) 2011 Peter Ross <pross@xvid.org>
|
2010-02-21 15:28:46 +02:00
|
|
|
*
|
2011-03-18 19:35:10 +02:00
|
|
|
* This file is part of Libav.
|
2010-02-21 15:28:46 +02:00
|
|
|
*
|
2011-03-18 19:35:10 +02:00
|
|
|
* Libav is free software; you can redistribute it and/or
|
2010-02-21 15:28:46 +02:00
|
|
|
* 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.
|
|
|
|
*
|
2011-03-18 19:35:10 +02:00
|
|
|
* Libav is distributed in the hope that it will be useful,
|
2010-02-21 15:28:46 +02:00
|
|
|
* 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
|
2011-03-18 19:35:10 +02:00
|
|
|
* License along with Libav; if not, write to the Free Software
|
2010-02-21 15:28:46 +02:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
2013-02-01 12:31:59 +03:00
|
|
|
#include "libavutil/attributes.h"
|
2011-02-07 15:37:08 +02:00
|
|
|
#include "libavutil/imgutils.h"
|
2013-02-08 02:31:13 +03:00
|
|
|
#include "libavutil/internal.h"
|
2016-06-04 15:07:30 +02:00
|
|
|
|
|
|
|
#define BITSTREAM_READER_LE
|
2010-02-21 15:28:46 +02:00
|
|
|
#include "avcodec.h"
|
|
|
|
#include "binkdata.h"
|
2011-07-23 16:46:35 +03:00
|
|
|
#include "binkdsp.h"
|
2016-04-09 15:59:51 +02:00
|
|
|
#include "bitstream.h"
|
2014-01-14 12:33:47 +03:00
|
|
|
#include "blockdsp.h"
|
2013-03-10 19:12:01 +03:00
|
|
|
#include "hpeldsp.h"
|
2012-11-10 15:22:56 +03:00
|
|
|
#include "internal.h"
|
2010-02-21 15:28:46 +02:00
|
|
|
#include "mathops.h"
|
2017-04-29 02:34:11 +02:00
|
|
|
#include "vlc.h"
|
2010-02-21 15:28:46 +02:00
|
|
|
|
2010-02-23 08:39:23 +02:00
|
|
|
#define BINK_FLAG_ALPHA 0x00100000
|
|
|
|
#define BINK_FLAG_GRAY 0x00020000
|
|
|
|
|
2010-02-21 15:28:46 +02:00
|
|
|
static VLC bink_trees[16];
|
|
|
|
|
2011-02-11 14:31:32 +02:00
|
|
|
/**
|
|
|
|
* IDs for different data types used in old version of Bink video codec
|
|
|
|
*/
|
|
|
|
enum OldSources {
|
|
|
|
BINKB_SRC_BLOCK_TYPES = 0, ///< 8x8 block types
|
|
|
|
BINKB_SRC_COLORS, ///< pixel values used for different block types
|
|
|
|
BINKB_SRC_PATTERN, ///< 8-bit values for 2-colour pattern fill
|
|
|
|
BINKB_SRC_X_OFF, ///< X components of motion value
|
|
|
|
BINKB_SRC_Y_OFF, ///< Y components of motion value
|
|
|
|
BINKB_SRC_INTRA_DC, ///< DC values for intrablocks with DCT
|
|
|
|
BINKB_SRC_INTER_DC, ///< DC values for interblocks with DCT
|
|
|
|
BINKB_SRC_INTRA_Q, ///< quantizer values for intrablocks with DCT
|
|
|
|
BINKB_SRC_INTER_Q, ///< quantizer values for interblocks with DCT
|
|
|
|
BINKB_SRC_INTER_COEFS, ///< number of coefficients for residue blocks
|
|
|
|
|
|
|
|
BINKB_NB_SRC
|
|
|
|
};
|
|
|
|
|
|
|
|
static const int binkb_bundle_sizes[BINKB_NB_SRC] = {
|
|
|
|
4, 8, 8, 5, 5, 11, 11, 4, 4, 7
|
|
|
|
};
|
|
|
|
|
|
|
|
static const int binkb_bundle_signed[BINKB_NB_SRC] = {
|
|
|
|
0, 0, 0, 1, 1, 0, 1, 0, 0, 0
|
|
|
|
};
|
|
|
|
|
2011-07-23 16:46:35 +03:00
|
|
|
static int32_t binkb_intra_quant[16][64];
|
|
|
|
static int32_t binkb_inter_quant[16][64];
|
2011-02-11 14:31:32 +02:00
|
|
|
|
2010-02-21 15:28:46 +02:00
|
|
|
/**
|
|
|
|
* IDs for different data types used in Bink video codec
|
|
|
|
*/
|
|
|
|
enum Sources {
|
|
|
|
BINK_SRC_BLOCK_TYPES = 0, ///< 8x8 block types
|
|
|
|
BINK_SRC_SUB_BLOCK_TYPES, ///< 16x16 block types (a subset of 8x8 block types)
|
|
|
|
BINK_SRC_COLORS, ///< pixel values used for different block types
|
|
|
|
BINK_SRC_PATTERN, ///< 8-bit values for 2-colour pattern fill
|
|
|
|
BINK_SRC_X_OFF, ///< X components of motion value
|
|
|
|
BINK_SRC_Y_OFF, ///< Y components of motion value
|
|
|
|
BINK_SRC_INTRA_DC, ///< DC values for intrablocks with DCT
|
2010-03-21 12:26:09 +02:00
|
|
|
BINK_SRC_INTER_DC, ///< DC values for interblocks with DCT
|
2010-02-21 15:28:46 +02:00
|
|
|
BINK_SRC_RUN, ///< run lengths for special fill block
|
|
|
|
|
|
|
|
BINK_NB_SRC
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* data needed to decode 4-bit Huffman-coded value
|
|
|
|
*/
|
|
|
|
typedef struct Tree {
|
|
|
|
int vlc_num; ///< tree number (in bink_trees[])
|
|
|
|
uint8_t syms[16]; ///< leaf value to symbol mapping
|
|
|
|
} Tree;
|
|
|
|
|
2016-04-09 15:59:51 +02:00
|
|
|
#define GET_HUFF(bc, tree) \
|
|
|
|
(tree).syms[bitstream_read_vlc(bc, bink_trees[(tree).vlc_num].table, \
|
|
|
|
bink_trees[(tree).vlc_num].bits, 1)]
|
2010-02-21 15:28:46 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* data structure used for decoding single Bink data type
|
|
|
|
*/
|
|
|
|
typedef struct Bundle {
|
|
|
|
int len; ///< length of number of entries to decode (in bits)
|
|
|
|
Tree tree; ///< Huffman tree-related data
|
|
|
|
uint8_t *data; ///< buffer for decoded symbols
|
|
|
|
uint8_t *data_end; ///< buffer end
|
|
|
|
uint8_t *cur_dec; ///< pointer to the not yet decoded part of the buffer
|
|
|
|
uint8_t *cur_ptr; ///< pointer to the data that is not read from buffer yet
|
|
|
|
} Bundle;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Decoder context
|
|
|
|
*/
|
|
|
|
typedef struct BinkContext {
|
|
|
|
AVCodecContext *avctx;
|
2014-01-14 12:33:47 +03:00
|
|
|
BlockDSPContext bdsp;
|
2013-03-10 19:12:01 +03:00
|
|
|
HpelDSPContext hdsp;
|
2014-02-13 18:32:41 +03:00
|
|
|
BinkDSPContext binkdsp;
|
2012-11-21 23:34:46 +03:00
|
|
|
AVFrame *last;
|
2010-02-21 15:28:46 +02:00
|
|
|
int version; ///< internal Bink file version
|
2010-02-22 16:59:51 +02:00
|
|
|
int has_alpha;
|
2010-02-21 15:28:46 +02:00
|
|
|
int swap_planes;
|
|
|
|
|
2011-02-11 14:31:32 +02:00
|
|
|
Bundle bundle[BINKB_NB_SRC]; ///< bundles for decoding all data types
|
2010-02-21 15:28:46 +02:00
|
|
|
Tree col_high[16]; ///< trees for decoding high nibble in "colours" data type
|
|
|
|
int col_lastval; ///< value of last decoded high nibble in "colours" data type
|
|
|
|
} BinkContext;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Bink video block types
|
|
|
|
*/
|
|
|
|
enum BlockTypes {
|
|
|
|
SKIP_BLOCK = 0, ///< skipped block
|
|
|
|
SCALED_BLOCK, ///< block has size 16x16
|
|
|
|
MOTION_BLOCK, ///< block is copied from previous frame with some offset
|
|
|
|
RUN_BLOCK, ///< block is composed from runs of colours with custom scan order
|
|
|
|
RESIDUE_BLOCK, ///< motion block with some difference added
|
|
|
|
INTRA_BLOCK, ///< intra DCT block
|
|
|
|
FILL_BLOCK, ///< block is filled with single colour
|
|
|
|
INTER_BLOCK, ///< motion block with DCT applied to the difference
|
|
|
|
PATTERN_BLOCK, ///< block is filled with two colours following custom pattern
|
|
|
|
RAW_BLOCK, ///< uncoded 8x8 block
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2010-06-30 18:38:06 +03:00
|
|
|
* Initialize length length in all bundles.
|
2010-02-21 15:28:46 +02:00
|
|
|
*
|
|
|
|
* @param c decoder context
|
|
|
|
* @param width plane width
|
|
|
|
* @param bw plane width in 8x8 blocks
|
|
|
|
*/
|
|
|
|
static void init_lengths(BinkContext *c, int width, int bw)
|
|
|
|
{
|
2012-03-20 20:43:00 +03:00
|
|
|
width = FFALIGN(width, 8);
|
2012-03-20 09:53:41 +03:00
|
|
|
|
2010-02-21 15:28:46 +02:00
|
|
|
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;
|
|
|
|
|
2011-01-15 15:29:14 +02:00
|
|
|
c->bundle[BINK_SRC_COLORS].len = av_log2(bw*64 + 511) + 1;
|
2010-02-21 15:28:46 +02:00
|
|
|
|
|
|
|
c->bundle[BINK_SRC_INTRA_DC].len =
|
|
|
|
c->bundle[BINK_SRC_INTER_DC].len =
|
|
|
|
c->bundle[BINK_SRC_X_OFF].len =
|
|
|
|
c->bundle[BINK_SRC_Y_OFF].len = av_log2((width >> 3) + 511) + 1;
|
|
|
|
|
|
|
|
c->bundle[BINK_SRC_PATTERN].len = av_log2((bw << 3) + 511) + 1;
|
|
|
|
|
2011-01-15 15:29:14 +02:00
|
|
|
c->bundle[BINK_SRC_RUN].len = av_log2(bw*48 + 511) + 1;
|
2010-02-21 15:28:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-06-30 18:38:06 +03:00
|
|
|
* Allocate memory for bundles.
|
2010-02-21 15:28:46 +02:00
|
|
|
*
|
|
|
|
* @param c decoder context
|
|
|
|
*/
|
|
|
|
static av_cold void init_bundles(BinkContext *c)
|
|
|
|
{
|
|
|
|
int bw, bh, blocks;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
bw = (c->avctx->width + 7) >> 3;
|
|
|
|
bh = (c->avctx->height + 7) >> 3;
|
|
|
|
blocks = bw * bh;
|
|
|
|
|
2011-02-11 14:31:32 +02:00
|
|
|
for (i = 0; i < BINKB_NB_SRC; i++) {
|
2010-02-21 15:28:46 +02:00
|
|
|
c->bundle[i].data = av_malloc(blocks * 64);
|
|
|
|
c->bundle[i].data_end = c->bundle[i].data + blocks * 64;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-06-30 18:38:06 +03:00
|
|
|
* Free memory used by bundles.
|
2010-02-21 15:28:46 +02:00
|
|
|
*
|
|
|
|
* @param c decoder context
|
|
|
|
*/
|
|
|
|
static av_cold void free_bundles(BinkContext *c)
|
|
|
|
{
|
|
|
|
int i;
|
2011-02-11 14:31:32 +02:00
|
|
|
for (i = 0; i < BINKB_NB_SRC; i++)
|
2010-02-21 15:28:46 +02:00
|
|
|
av_freep(&c->bundle[i].data);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-06-30 18:38:06 +03:00
|
|
|
* Merge two consequent lists of equal size depending on bits read.
|
2010-02-21 15:28:46 +02:00
|
|
|
*
|
2016-04-09 15:59:51 +02:00
|
|
|
* @param bc context for reading bits
|
2010-02-21 15:28:46 +02:00
|
|
|
* @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
|
|
|
|
*/
|
2016-04-09 15:59:51 +02:00
|
|
|
static void merge(BitstreamContext *bc, uint8_t *dst, uint8_t *src, int size)
|
2010-02-21 15:28:46 +02:00
|
|
|
{
|
|
|
|
uint8_t *src2 = src + size;
|
|
|
|
int size2 = size;
|
|
|
|
|
|
|
|
do {
|
2016-04-09 15:59:51 +02:00
|
|
|
if (!bitstream_read_bit(bc)) {
|
2010-02-21 15:28:46 +02:00
|
|
|
*dst++ = *src++;
|
|
|
|
size--;
|
|
|
|
} else {
|
|
|
|
*dst++ = *src2++;
|
|
|
|
size2--;
|
|
|
|
}
|
|
|
|
} while (size && size2);
|
|
|
|
|
|
|
|
while (size--)
|
|
|
|
*dst++ = *src++;
|
|
|
|
while (size2--)
|
|
|
|
*dst++ = *src2++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-06-30 18:38:06 +03:00
|
|
|
* Read information about Huffman tree used to decode data.
|
2010-02-21 15:28:46 +02:00
|
|
|
*
|
2016-04-09 15:59:51 +02:00
|
|
|
* @param bc context for reading bits
|
2010-02-21 15:28:46 +02:00
|
|
|
* @param tree pointer for storing tree data
|
|
|
|
*/
|
2016-04-09 15:59:51 +02:00
|
|
|
static void read_tree(BitstreamContext *bc, Tree *tree)
|
2010-02-21 15:28:46 +02:00
|
|
|
{
|
2011-06-06 15:13:02 +03:00
|
|
|
uint8_t tmp1[16] = { 0 }, tmp2[16], *in = tmp1, *out = tmp2;
|
2010-02-21 15:28:46 +02:00
|
|
|
int i, t, len;
|
|
|
|
|
2016-04-09 15:59:51 +02:00
|
|
|
tree->vlc_num = bitstream_read(bc, 4);
|
2010-02-21 15:28:46 +02:00
|
|
|
if (!tree->vlc_num) {
|
|
|
|
for (i = 0; i < 16; i++)
|
|
|
|
tree->syms[i] = i;
|
|
|
|
return;
|
|
|
|
}
|
2016-04-09 15:59:51 +02:00
|
|
|
if (bitstream_read_bit(bc)) {
|
|
|
|
len = bitstream_read(bc, 3);
|
2010-02-21 15:28:46 +02:00
|
|
|
for (i = 0; i <= len; i++) {
|
2016-04-09 15:59:51 +02:00
|
|
|
tree->syms[i] = bitstream_read(bc, 4);
|
2010-02-21 15:28:46 +02:00
|
|
|
tmp1[tree->syms[i]] = 1;
|
|
|
|
}
|
2011-09-27 15:16:41 +03:00
|
|
|
for (i = 0; i < 16 && len < 16 - 1; i++)
|
2010-02-21 15:28:46 +02:00
|
|
|
if (!tmp1[i])
|
|
|
|
tree->syms[++len] = i;
|
|
|
|
} else {
|
2016-04-09 15:59:51 +02:00
|
|
|
len = bitstream_read(bc, 2);
|
2010-02-21 15:28:46 +02:00
|
|
|
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)
|
2016-04-09 15:59:51 +02:00
|
|
|
merge(bc, out + t, in + t, size);
|
2010-02-21 15:28:46 +02:00
|
|
|
FFSWAP(uint8_t*, in, out);
|
|
|
|
}
|
|
|
|
memcpy(tree->syms, in, 16);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-06-30 18:38:06 +03:00
|
|
|
* Prepare bundle for decoding data.
|
2010-02-21 15:28:46 +02:00
|
|
|
*
|
2016-04-09 15:59:51 +02:00
|
|
|
* @param bc context for reading bits
|
2010-02-21 15:28:46 +02:00
|
|
|
* @param c decoder context
|
|
|
|
* @param bundle_num number of the bundle to initialize
|
|
|
|
*/
|
2016-04-09 15:59:51 +02:00
|
|
|
static void read_bundle(BitstreamContext *bc, BinkContext *c, int bundle_num)
|
2010-02-21 15:28:46 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (bundle_num == BINK_SRC_COLORS) {
|
|
|
|
for (i = 0; i < 16; i++)
|
2016-04-09 15:59:51 +02:00
|
|
|
read_tree(bc, &c->col_high[i]);
|
2010-02-21 15:28:46 +02:00
|
|
|
c->col_lastval = 0;
|
|
|
|
}
|
|
|
|
if (bundle_num != BINK_SRC_INTRA_DC && bundle_num != BINK_SRC_INTER_DC)
|
2016-04-09 15:59:51 +02:00
|
|
|
read_tree(bc, &c->bundle[bundle_num].tree);
|
2010-02-21 15:28:46 +02:00
|
|
|
c->bundle[bundle_num].cur_dec =
|
|
|
|
c->bundle[bundle_num].cur_ptr = c->bundle[bundle_num].data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* common check before starting decoding bundle data
|
|
|
|
*
|
2016-04-09 15:59:51 +02:00
|
|
|
* @param bc context for reading bits
|
2010-02-21 15:28:46 +02:00
|
|
|
* @param b bundle
|
|
|
|
* @param t variable where number of elements to decode will be stored
|
|
|
|
*/
|
2016-04-09 15:59:51 +02:00
|
|
|
#define CHECK_READ_VAL(bc, b, t) \
|
2010-02-21 15:28:46 +02:00
|
|
|
if (!b->cur_dec || (b->cur_dec > b->cur_ptr)) \
|
|
|
|
return 0; \
|
2016-04-09 15:59:51 +02:00
|
|
|
t = bitstream_read(bc, b->len); \
|
2010-02-21 15:28:46 +02:00
|
|
|
if (!t) { \
|
|
|
|
b->cur_dec = NULL; \
|
|
|
|
return 0; \
|
|
|
|
} \
|
|
|
|
|
2016-04-09 15:59:51 +02:00
|
|
|
static int read_runs(AVCodecContext *avctx, BitstreamContext *bc, Bundle *b)
|
2010-02-21 15:28:46 +02:00
|
|
|
{
|
|
|
|
int t, v;
|
|
|
|
const uint8_t *dec_end;
|
|
|
|
|
2016-04-09 15:59:51 +02:00
|
|
|
CHECK_READ_VAL(bc, b, t);
|
2010-02-21 15:28:46 +02:00
|
|
|
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");
|
2012-11-14 23:47:37 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
2010-02-21 15:28:46 +02:00
|
|
|
}
|
2016-04-09 15:59:51 +02:00
|
|
|
if (bitstream_read_bit(bc)) {
|
|
|
|
v = bitstream_read(bc, 4);
|
2010-02-21 15:28:46 +02:00
|
|
|
memset(b->cur_dec, v, t);
|
|
|
|
b->cur_dec += t;
|
|
|
|
} else {
|
|
|
|
while (b->cur_dec < dec_end)
|
2016-04-09 15:59:51 +02:00
|
|
|
*b->cur_dec++ = GET_HUFF(bc, b->tree);
|
2010-02-21 15:28:46 +02:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-04-09 15:59:51 +02:00
|
|
|
static int read_motion_values(AVCodecContext *avctx, BitstreamContext *bc, Bundle *b)
|
2010-02-21 15:28:46 +02:00
|
|
|
{
|
2016-04-09 15:59:51 +02:00
|
|
|
int t, v;
|
2010-02-21 15:28:46 +02:00
|
|
|
const uint8_t *dec_end;
|
|
|
|
|
2016-04-09 15:59:51 +02:00
|
|
|
CHECK_READ_VAL(bc, b, t);
|
2010-02-21 15:28:46 +02:00
|
|
|
dec_end = b->cur_dec + t;
|
|
|
|
if (dec_end > b->data_end) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Too many motion values\n");
|
2012-11-14 23:47:37 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
2010-02-21 15:28:46 +02:00
|
|
|
}
|
2016-04-09 15:59:51 +02:00
|
|
|
if (bitstream_read_bit(bc)) {
|
|
|
|
v = bitstream_read(bc, 4);
|
2010-02-21 15:28:46 +02:00
|
|
|
if (v) {
|
2016-04-09 15:59:51 +02:00
|
|
|
v = bitstream_apply_sign(bc, v);
|
2010-02-21 15:28:46 +02:00
|
|
|
}
|
|
|
|
memset(b->cur_dec, v, t);
|
|
|
|
b->cur_dec += t;
|
|
|
|
} else {
|
2011-09-27 15:16:41 +03:00
|
|
|
while (b->cur_dec < dec_end) {
|
2016-04-09 15:59:51 +02:00
|
|
|
v = GET_HUFF(bc, b->tree);
|
2010-02-21 15:28:46 +02:00
|
|
|
if (v) {
|
2016-04-09 15:59:51 +02:00
|
|
|
v = bitstream_apply_sign(bc, v);
|
2010-02-21 15:28:46 +02:00
|
|
|
}
|
|
|
|
*b->cur_dec++ = v;
|
2011-09-27 15:16:41 +03:00
|
|
|
}
|
2010-02-21 15:28:46 +02:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-10-30 22:51:46 +03:00
|
|
|
static const uint8_t bink_rlelens[4] = { 4, 8, 12, 32 };
|
2010-02-21 15:28:46 +02:00
|
|
|
|
2016-04-09 15:59:51 +02:00
|
|
|
static int read_block_types(AVCodecContext *avctx, BitstreamContext *bc, Bundle *b)
|
2010-02-21 15:28:46 +02:00
|
|
|
{
|
|
|
|
int t, v;
|
|
|
|
int last = 0;
|
|
|
|
const uint8_t *dec_end;
|
|
|
|
|
2016-04-09 15:59:51 +02:00
|
|
|
CHECK_READ_VAL(bc, b, t);
|
2010-02-21 15:28:46 +02:00
|
|
|
dec_end = b->cur_dec + t;
|
|
|
|
if (dec_end > b->data_end) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Too many block type values\n");
|
2012-11-14 23:47:37 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
2010-02-21 15:28:46 +02:00
|
|
|
}
|
2016-04-09 15:59:51 +02:00
|
|
|
if (bitstream_read_bit(bc)) {
|
|
|
|
v = bitstream_read(bc, 4);
|
2010-02-21 15:28:46 +02:00
|
|
|
memset(b->cur_dec, v, t);
|
|
|
|
b->cur_dec += t;
|
|
|
|
} else {
|
2011-09-27 15:16:41 +03:00
|
|
|
while (b->cur_dec < dec_end) {
|
2016-04-09 15:59:51 +02:00
|
|
|
v = GET_HUFF(bc, b->tree);
|
2010-02-21 15:28:46 +02:00
|
|
|
if (v < 12) {
|
|
|
|
last = v;
|
|
|
|
*b->cur_dec++ = v;
|
|
|
|
} else {
|
|
|
|
int run = bink_rlelens[v - 12];
|
|
|
|
|
2011-09-27 15:16:41 +03:00
|
|
|
if (dec_end - b->cur_dec < run)
|
2012-11-14 23:47:37 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
2010-02-21 15:28:46 +02:00
|
|
|
memset(b->cur_dec, last, run);
|
|
|
|
b->cur_dec += run;
|
|
|
|
}
|
2011-09-27 15:16:41 +03:00
|
|
|
}
|
2010-02-21 15:28:46 +02:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-04-09 15:59:51 +02:00
|
|
|
static int read_patterns(AVCodecContext *avctx, BitstreamContext *bc, Bundle *b)
|
2010-02-21 15:28:46 +02:00
|
|
|
{
|
|
|
|
int t, v;
|
|
|
|
const uint8_t *dec_end;
|
|
|
|
|
2016-04-09 15:59:51 +02:00
|
|
|
CHECK_READ_VAL(bc, b, t);
|
2010-02-21 15:28:46 +02:00
|
|
|
dec_end = b->cur_dec + t;
|
|
|
|
if (dec_end > b->data_end) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Too many pattern values\n");
|
2012-11-14 23:47:37 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
2010-02-21 15:28:46 +02:00
|
|
|
}
|
|
|
|
while (b->cur_dec < dec_end) {
|
2016-04-09 15:59:51 +02:00
|
|
|
v = GET_HUFF(bc, b->tree);
|
|
|
|
v |= GET_HUFF(bc, b->tree) << 4;
|
2010-02-21 15:28:46 +02:00
|
|
|
*b->cur_dec++ = v;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-04-09 15:59:51 +02:00
|
|
|
static int read_colors(BitstreamContext *bc, Bundle *b, BinkContext *c)
|
2010-02-21 15:28:46 +02:00
|
|
|
{
|
|
|
|
int t, sign, v;
|
|
|
|
const uint8_t *dec_end;
|
|
|
|
|
2016-04-09 15:59:51 +02:00
|
|
|
CHECK_READ_VAL(bc, b, t);
|
2010-02-21 15:28:46 +02:00
|
|
|
dec_end = b->cur_dec + t;
|
|
|
|
if (dec_end > b->data_end) {
|
|
|
|
av_log(c->avctx, AV_LOG_ERROR, "Too many color values\n");
|
2012-11-14 23:47:37 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
2010-02-21 15:28:46 +02:00
|
|
|
}
|
2016-04-09 15:59:51 +02:00
|
|
|
if (bitstream_read_bit(bc)) {
|
|
|
|
c->col_lastval = GET_HUFF(bc, c->col_high[c->col_lastval]);
|
|
|
|
v = GET_HUFF(bc, b->tree);
|
2010-02-21 15:28:46 +02:00
|
|
|
v = (c->col_lastval << 4) | v;
|
|
|
|
if (c->version < 'i') {
|
|
|
|
sign = ((int8_t) v) >> 7;
|
|
|
|
v = ((v & 0x7F) ^ sign) - sign;
|
|
|
|
v += 0x80;
|
|
|
|
}
|
|
|
|
memset(b->cur_dec, v, t);
|
|
|
|
b->cur_dec += t;
|
|
|
|
} else {
|
|
|
|
while (b->cur_dec < dec_end) {
|
2016-04-09 15:59:51 +02:00
|
|
|
c->col_lastval = GET_HUFF(bc, c->col_high[c->col_lastval]);
|
|
|
|
v = GET_HUFF(bc, b->tree);
|
2010-02-21 15:28:46 +02:00
|
|
|
v = (c->col_lastval << 4) | v;
|
|
|
|
if (c->version < 'i') {
|
|
|
|
sign = ((int8_t) v) >> 7;
|
|
|
|
v = ((v & 0x7F) ^ sign) - sign;
|
|
|
|
v += 0x80;
|
|
|
|
}
|
|
|
|
*b->cur_dec++ = v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** number of bits used to store first DC value in bundle */
|
|
|
|
#define DC_START_BITS 11
|
|
|
|
|
2016-04-09 15:59:51 +02:00
|
|
|
static int read_dcs(AVCodecContext *avctx, BitstreamContext *bc, Bundle *b,
|
2010-02-21 15:28:46 +02:00
|
|
|
int start_bits, int has_sign)
|
|
|
|
{
|
2016-04-09 15:59:51 +02:00
|
|
|
int i, j, len, len2, bsize, v, v2;
|
2011-09-27 15:16:41 +03:00
|
|
|
int16_t *dst = (int16_t*)b->cur_dec;
|
|
|
|
int16_t *dst_end = (int16_t*)b->data_end;
|
2010-02-21 15:28:46 +02:00
|
|
|
|
2016-04-09 15:59:51 +02:00
|
|
|
CHECK_READ_VAL(bc, b, len);
|
|
|
|
v = bitstream_read(bc, start_bits - has_sign);
|
2010-02-21 15:28:46 +02:00
|
|
|
if (v && has_sign) {
|
2016-04-09 15:59:51 +02:00
|
|
|
v = bitstream_apply_sign(bc, v);
|
2010-02-21 15:28:46 +02:00
|
|
|
}
|
2011-09-27 15:16:41 +03:00
|
|
|
if (dst_end - dst < 1)
|
2012-11-14 23:47:37 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
2010-02-21 15:28:46 +02:00
|
|
|
*dst++ = v;
|
|
|
|
len--;
|
|
|
|
for (i = 0; i < len; i += 8) {
|
|
|
|
len2 = FFMIN(len - i, 8);
|
2011-09-27 15:16:41 +03:00
|
|
|
if (dst_end - dst < len2)
|
2012-11-14 23:47:37 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
2016-04-09 15:59:51 +02:00
|
|
|
bsize = bitstream_read(bc, 4);
|
2010-02-21 15:28:46 +02:00
|
|
|
if (bsize) {
|
|
|
|
for (j = 0; j < len2; j++) {
|
2016-04-09 15:59:51 +02:00
|
|
|
v2 = bitstream_read(bc, bsize);
|
2010-02-21 15:28:46 +02:00
|
|
|
if (v2) {
|
2016-04-09 15:59:51 +02:00
|
|
|
v2 = bitstream_apply_sign(bc, v2);
|
2010-02-21 15:28:46 +02:00
|
|
|
}
|
|
|
|
v += v2;
|
|
|
|
*dst++ = v;
|
|
|
|
if (v < -32768 || v > 32767) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "DC value went out of bounds: %d\n", v);
|
2012-11-14 23:47:37 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
2010-02-21 15:28:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (j = 0; j < len2; j++)
|
|
|
|
*dst++ = v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
b->cur_dec = (uint8_t*)dst;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-06-30 18:38:06 +03:00
|
|
|
* Retrieve next value from bundle.
|
2010-02-21 15:28:46 +02:00
|
|
|
*
|
|
|
|
* @param c decoder context
|
|
|
|
* @param bundle bundle number
|
|
|
|
*/
|
|
|
|
static inline int get_value(BinkContext *c, int bundle)
|
|
|
|
{
|
2011-02-11 14:31:32 +02:00
|
|
|
int ret;
|
2010-02-21 15:28:46 +02:00
|
|
|
|
|
|
|
if (bundle < BINK_SRC_X_OFF || bundle == BINK_SRC_RUN)
|
|
|
|
return *c->bundle[bundle].cur_ptr++;
|
|
|
|
if (bundle == BINK_SRC_X_OFF || bundle == BINK_SRC_Y_OFF)
|
|
|
|
return (int8_t)*c->bundle[bundle].cur_ptr++;
|
|
|
|
ret = *(int16_t*)c->bundle[bundle].cur_ptr;
|
|
|
|
c->bundle[bundle].cur_ptr += 2;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-02-01 12:31:59 +03:00
|
|
|
static av_cold void binkb_init_bundle(BinkContext *c, int bundle_num)
|
2011-02-11 14:31:32 +02:00
|
|
|
{
|
|
|
|
c->bundle[bundle_num].cur_dec =
|
|
|
|
c->bundle[bundle_num].cur_ptr = c->bundle[bundle_num].data;
|
|
|
|
c->bundle[bundle_num].len = 13;
|
|
|
|
}
|
|
|
|
|
2013-02-01 12:31:59 +03:00
|
|
|
static av_cold void binkb_init_bundles(BinkContext *c)
|
2011-02-11 14:31:32 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < BINKB_NB_SRC; i++)
|
|
|
|
binkb_init_bundle(c, i);
|
|
|
|
}
|
|
|
|
|
2016-04-09 15:59:51 +02:00
|
|
|
static int binkb_read_bundle(BinkContext *c, BitstreamContext *bc, int bundle_num)
|
2011-02-11 14:31:32 +02:00
|
|
|
{
|
|
|
|
const int bits = binkb_bundle_sizes[bundle_num];
|
|
|
|
const int mask = 1 << (bits - 1);
|
|
|
|
const int issigned = binkb_bundle_signed[bundle_num];
|
|
|
|
Bundle *b = &c->bundle[bundle_num];
|
|
|
|
int i, len;
|
|
|
|
|
2016-04-09 15:59:51 +02:00
|
|
|
CHECK_READ_VAL(bc, b, len);
|
2011-09-27 15:16:41 +03:00
|
|
|
if (b->data_end - b->cur_dec < len * (1 + (bits > 8)))
|
2012-11-14 23:47:37 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
2011-02-11 14:31:32 +02:00
|
|
|
if (bits <= 8) {
|
|
|
|
if (!issigned) {
|
|
|
|
for (i = 0; i < len; i++)
|
2016-04-09 15:59:51 +02:00
|
|
|
*b->cur_dec++ = bitstream_read(bc, bits);
|
2011-02-11 14:31:32 +02:00
|
|
|
} else {
|
|
|
|
for (i = 0; i < len; i++)
|
2016-04-09 15:59:51 +02:00
|
|
|
*b->cur_dec++ = bitstream_read(bc, bits) - mask;
|
2011-02-11 14:31:32 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
int16_t *dst = (int16_t*)b->cur_dec;
|
|
|
|
|
|
|
|
if (!issigned) {
|
|
|
|
for (i = 0; i < len; i++)
|
2016-04-09 15:59:51 +02:00
|
|
|
*dst++ = bitstream_read(bc, bits);
|
2011-02-11 14:31:32 +02:00
|
|
|
} else {
|
|
|
|
for (i = 0; i < len; i++)
|
2016-04-09 15:59:51 +02:00
|
|
|
*dst++ = bitstream_read(bc, bits) - mask;
|
2011-02-11 14:31:32 +02:00
|
|
|
}
|
|
|
|
b->cur_dec = (uint8_t*)dst;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int binkb_get_value(BinkContext *c, int bundle_num)
|
|
|
|
{
|
|
|
|
int16_t ret;
|
|
|
|
const int bits = binkb_bundle_sizes[bundle_num];
|
|
|
|
|
|
|
|
if (bits <= 8) {
|
|
|
|
int val = *c->bundle[bundle_num].cur_ptr++;
|
|
|
|
return binkb_bundle_signed[bundle_num] ? (int8_t)val : val;
|
|
|
|
}
|
|
|
|
ret = *(int16_t*)c->bundle[bundle_num].cur_ptr;
|
|
|
|
c->bundle[bundle_num].cur_ptr += 2;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-02-21 15:28:46 +02:00
|
|
|
/**
|
2010-06-30 18:38:06 +03:00
|
|
|
* Read 8x8 block of DCT coefficients.
|
2010-02-21 15:28:46 +02:00
|
|
|
*
|
2016-04-09 15:59:51 +02:00
|
|
|
* @param bc context for reading bits
|
2010-02-21 15:28:46 +02:00
|
|
|
* @param block place for storing coefficients
|
|
|
|
* @param scan scan order table
|
2011-02-13 05:33:49 +02:00
|
|
|
* @param quant_matrices quantization matrices
|
2010-02-21 15:28:46 +02:00
|
|
|
* @return 0 for success, negative value in other cases
|
|
|
|
*/
|
2016-04-09 15:59:51 +02:00
|
|
|
static int read_dct_coeffs(BitstreamContext *bc, int32_t block[64],
|
|
|
|
const uint8_t *scan,
|
2011-07-23 16:46:35 +03:00
|
|
|
const int32_t quant_matrices[16][64], int q)
|
2010-02-21 15:28:46 +02:00
|
|
|
{
|
|
|
|
int coef_list[128];
|
|
|
|
int mode_list[128];
|
2016-04-09 15:59:51 +02:00
|
|
|
int i, t, bits, ccoef, mode;
|
2010-02-21 15:28:46 +02:00
|
|
|
int list_start = 64, list_end = 64, list_pos;
|
|
|
|
int coef_count = 0;
|
|
|
|
int coef_idx[64];
|
|
|
|
int quant_idx;
|
2011-07-23 16:46:35 +03:00
|
|
|
const int32_t *quant;
|
2010-02-21 15:28:46 +02:00
|
|
|
|
|
|
|
coef_list[list_end] = 4; mode_list[list_end++] = 0;
|
|
|
|
coef_list[list_end] = 24; mode_list[list_end++] = 0;
|
|
|
|
coef_list[list_end] = 44; mode_list[list_end++] = 0;
|
|
|
|
coef_list[list_end] = 1; mode_list[list_end++] = 3;
|
|
|
|
coef_list[list_end] = 2; mode_list[list_end++] = 3;
|
|
|
|
coef_list[list_end] = 3; mode_list[list_end++] = 3;
|
|
|
|
|
2016-04-09 15:59:51 +02:00
|
|
|
for (bits = bitstream_read(bc, 4) - 1; bits >= 0; bits--) {
|
2010-02-21 15:28:46 +02:00
|
|
|
list_pos = list_start;
|
|
|
|
while (list_pos < list_end) {
|
2016-04-09 15:59:51 +02:00
|
|
|
if (!(mode_list[list_pos] | coef_list[list_pos]) || !bitstream_read_bit(bc)) {
|
2010-02-21 15:28:46 +02:00
|
|
|
list_pos++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
ccoef = coef_list[list_pos];
|
|
|
|
mode = mode_list[list_pos];
|
|
|
|
switch (mode) {
|
|
|
|
case 0:
|
|
|
|
coef_list[list_pos] = ccoef + 4;
|
|
|
|
mode_list[list_pos] = 1;
|
|
|
|
case 2:
|
|
|
|
if (mode == 2) {
|
|
|
|
coef_list[list_pos] = 0;
|
|
|
|
mode_list[list_pos++] = 0;
|
|
|
|
}
|
|
|
|
for (i = 0; i < 4; i++, ccoef++) {
|
2016-04-09 15:59:51 +02:00
|
|
|
if (bitstream_read_bit(bc)) {
|
2010-02-21 15:28:46 +02:00
|
|
|
coef_list[--list_start] = ccoef;
|
|
|
|
mode_list[ list_start] = 3;
|
|
|
|
} else {
|
|
|
|
if (!bits) {
|
2016-04-09 15:59:51 +02:00
|
|
|
t = 1 - (bitstream_read_bit(bc) << 1);
|
2010-02-21 15:28:46 +02:00
|
|
|
} else {
|
2016-04-09 15:59:51 +02:00
|
|
|
t = bitstream_read(bc, bits) | 1 << bits;
|
|
|
|
t = bitstream_apply_sign(bc, t);
|
2010-02-21 15:28:46 +02:00
|
|
|
}
|
|
|
|
block[scan[ccoef]] = t;
|
|
|
|
coef_idx[coef_count++] = ccoef;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
mode_list[list_pos] = 2;
|
|
|
|
for (i = 0; i < 3; i++) {
|
|
|
|
ccoef += 4;
|
|
|
|
coef_list[list_end] = ccoef;
|
|
|
|
mode_list[list_end++] = 2;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
if (!bits) {
|
2016-04-09 15:59:51 +02:00
|
|
|
t = 1 - (bitstream_read_bit(bc) << 1);
|
2010-02-21 15:28:46 +02:00
|
|
|
} else {
|
2016-04-09 15:59:51 +02:00
|
|
|
t = bitstream_read(bc, bits) | 1 << bits;
|
|
|
|
t = bitstream_apply_sign(bc, t);
|
2010-02-21 15:28:46 +02:00
|
|
|
}
|
|
|
|
block[scan[ccoef]] = t;
|
|
|
|
coef_idx[coef_count++] = ccoef;
|
|
|
|
coef_list[list_pos] = 0;
|
|
|
|
mode_list[list_pos++] = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-11 14:31:32 +02:00
|
|
|
if (q == -1) {
|
2016-04-09 15:59:51 +02:00
|
|
|
quant_idx = bitstream_read(bc, 4);
|
2011-02-11 14:31:32 +02:00
|
|
|
} else {
|
|
|
|
quant_idx = q;
|
|
|
|
}
|
|
|
|
|
2013-08-04 19:48:20 +03:00
|
|
|
if (quant_idx >= 16)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
2011-02-13 05:33:49 +02:00
|
|
|
quant = quant_matrices[quant_idx];
|
2011-02-11 14:31:32 +02:00
|
|
|
|
2010-02-21 15:28:46 +02:00
|
|
|
block[0] = (block[0] * quant[0]) >> 11;
|
|
|
|
for (i = 0; i < coef_count; i++) {
|
|
|
|
int idx = coef_idx[i];
|
|
|
|
block[scan[idx]] = (block[scan[idx]] * quant[idx]) >> 11;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-06-30 18:38:06 +03:00
|
|
|
* Read 8x8 block with residue after motion compensation.
|
2010-02-21 15:28:46 +02:00
|
|
|
*
|
2016-04-09 15:59:51 +02:00
|
|
|
* @param bc context for reading bits
|
2010-02-21 15:28:46 +02:00
|
|
|
* @param block place to store read data
|
|
|
|
* @param masks_count number of masks to decode
|
|
|
|
* @return 0 on success, negative value in other cases
|
|
|
|
*/
|
2016-04-09 15:59:51 +02:00
|
|
|
static int read_residue(BitstreamContext *bc, int16_t block[64], int masks_count)
|
2010-02-21 15:28:46 +02:00
|
|
|
{
|
|
|
|
int coef_list[128];
|
|
|
|
int mode_list[128];
|
2016-04-09 15:59:51 +02:00
|
|
|
int i, mask, ccoef, mode;
|
2010-02-21 15:28:46 +02:00
|
|
|
int list_start = 64, list_end = 64, list_pos;
|
|
|
|
int nz_coeff[64];
|
|
|
|
int nz_coeff_count = 0;
|
|
|
|
|
|
|
|
coef_list[list_end] = 4; mode_list[list_end++] = 0;
|
|
|
|
coef_list[list_end] = 24; mode_list[list_end++] = 0;
|
|
|
|
coef_list[list_end] = 44; mode_list[list_end++] = 0;
|
|
|
|
coef_list[list_end] = 0; mode_list[list_end++] = 2;
|
|
|
|
|
2016-04-09 15:59:51 +02:00
|
|
|
for (mask = 1 << bitstream_read(bc, 3); mask; mask >>= 1) {
|
2010-02-21 15:28:46 +02:00
|
|
|
for (i = 0; i < nz_coeff_count; i++) {
|
2016-04-09 15:59:51 +02:00
|
|
|
if (!bitstream_read_bit(bc))
|
2010-02-21 15:28:46 +02:00
|
|
|
continue;
|
|
|
|
if (block[nz_coeff[i]] < 0)
|
|
|
|
block[nz_coeff[i]] -= mask;
|
|
|
|
else
|
|
|
|
block[nz_coeff[i]] += mask;
|
|
|
|
masks_count--;
|
|
|
|
if (masks_count < 0)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
list_pos = list_start;
|
|
|
|
while (list_pos < list_end) {
|
2016-04-09 15:59:51 +02:00
|
|
|
if (!(coef_list[list_pos] | mode_list[list_pos]) || !bitstream_read_bit(bc)) {
|
2010-02-21 15:28:46 +02:00
|
|
|
list_pos++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
ccoef = coef_list[list_pos];
|
|
|
|
mode = mode_list[list_pos];
|
|
|
|
switch (mode) {
|
|
|
|
case 0:
|
|
|
|
coef_list[list_pos] = ccoef + 4;
|
|
|
|
mode_list[list_pos] = 1;
|
|
|
|
case 2:
|
|
|
|
if (mode == 2) {
|
|
|
|
coef_list[list_pos] = 0;
|
|
|
|
mode_list[list_pos++] = 0;
|
|
|
|
}
|
|
|
|
for (i = 0; i < 4; i++, ccoef++) {
|
2016-04-09 15:59:51 +02:00
|
|
|
if (bitstream_read_bit(bc)) {
|
2010-02-21 15:28:46 +02:00
|
|
|
coef_list[--list_start] = ccoef;
|
|
|
|
mode_list[ list_start] = 3;
|
|
|
|
} else {
|
|
|
|
nz_coeff[nz_coeff_count++] = bink_scan[ccoef];
|
2016-04-09 15:59:51 +02:00
|
|
|
block[bink_scan[ccoef]] = bitstream_apply_sign(bc, mask);
|
2010-02-21 15:28:46 +02:00
|
|
|
masks_count--;
|
|
|
|
if (masks_count < 0)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
mode_list[list_pos] = 2;
|
|
|
|
for (i = 0; i < 3; i++) {
|
|
|
|
ccoef += 4;
|
|
|
|
coef_list[list_end] = ccoef;
|
|
|
|
mode_list[list_end++] = 2;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
nz_coeff[nz_coeff_count++] = bink_scan[ccoef];
|
2016-04-09 15:59:51 +02:00
|
|
|
block[bink_scan[ccoef]] = bitstream_apply_sign(bc, mask);
|
2010-02-21 15:28:46 +02:00
|
|
|
coef_list[list_pos] = 0;
|
|
|
|
mode_list[list_pos++] = 0;
|
|
|
|
masks_count--;
|
|
|
|
if (masks_count < 0)
|
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-02-11 14:31:32 +02:00
|
|
|
/**
|
|
|
|
* Copy 8x8 block from source to destination, where src and dst may be overlapped
|
|
|
|
*/
|
|
|
|
static inline void put_pixels8x8_overlapped(uint8_t *dst, uint8_t *src, int stride)
|
|
|
|
{
|
|
|
|
uint8_t tmp[64];
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < 8; i++)
|
|
|
|
memcpy(tmp + i*8, src + i*stride, 8);
|
|
|
|
for (i = 0; i < 8; i++)
|
|
|
|
memcpy(dst + i*stride, tmp + i*8, 8);
|
|
|
|
}
|
|
|
|
|
2016-04-09 15:59:51 +02:00
|
|
|
static int binkb_decode_plane(BinkContext *c, AVFrame *frame, BitstreamContext *bc,
|
2012-11-21 23:34:46 +03:00
|
|
|
int plane_idx, int is_key, int is_chroma)
|
2011-02-11 14:31:32 +02:00
|
|
|
{
|
2012-11-14 23:47:37 +03:00
|
|
|
int blk, ret;
|
2011-02-11 14:31:32 +02:00
|
|
|
int i, j, bx, by;
|
|
|
|
uint8_t *dst, *ref, *ref_start, *ref_end;
|
|
|
|
int v, col[2];
|
|
|
|
const uint8_t *scan;
|
|
|
|
int xoff, yoff;
|
2013-01-20 03:02:29 +03:00
|
|
|
LOCAL_ALIGNED_16(int16_t, block, [64]);
|
2011-07-23 16:46:35 +03:00
|
|
|
LOCAL_ALIGNED_16(int32_t, dctblock, [64]);
|
2011-02-11 14:31:32 +02:00
|
|
|
int coordmap[64];
|
|
|
|
int ybias = is_key ? -15 : 0;
|
|
|
|
int qp;
|
|
|
|
|
2012-11-21 23:34:46 +03:00
|
|
|
const int stride = frame->linesize[plane_idx];
|
2011-02-11 14:31:32 +02:00
|
|
|
int bw = is_chroma ? (c->avctx->width + 15) >> 4 : (c->avctx->width + 7) >> 3;
|
|
|
|
int bh = is_chroma ? (c->avctx->height + 15) >> 4 : (c->avctx->height + 7) >> 3;
|
|
|
|
|
|
|
|
binkb_init_bundles(c);
|
2012-11-21 23:34:46 +03:00
|
|
|
ref_start = frame->data[plane_idx];
|
|
|
|
ref_end = frame->data[plane_idx] + (bh * frame->linesize[plane_idx] + bw) * 8;
|
2011-02-11 14:31:32 +02:00
|
|
|
|
|
|
|
for (i = 0; i < 64; i++)
|
|
|
|
coordmap[i] = (i & 7) + (i >> 3) * stride;
|
|
|
|
|
|
|
|
for (by = 0; by < bh; by++) {
|
|
|
|
for (i = 0; i < BINKB_NB_SRC; i++) {
|
2016-04-09 15:59:51 +02:00
|
|
|
if ((ret = binkb_read_bundle(c, bc, i)) < 0)
|
2012-11-14 23:47:37 +03:00
|
|
|
return ret;
|
2011-02-11 14:31:32 +02:00
|
|
|
}
|
|
|
|
|
2012-11-21 23:34:46 +03:00
|
|
|
dst = frame->data[plane_idx] + 8*by*stride;
|
2011-02-11 14:31:32 +02:00
|
|
|
for (bx = 0; bx < bw; bx++, dst += 8) {
|
|
|
|
blk = binkb_get_value(c, BINKB_SRC_BLOCK_TYPES);
|
|
|
|
switch (blk) {
|
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
case 1:
|
2016-04-09 15:59:51 +02:00
|
|
|
scan = bink_patterns[bitstream_read(bc, 4)];
|
2011-02-11 14:31:32 +02:00
|
|
|
i = 0;
|
|
|
|
do {
|
2016-04-09 15:59:51 +02:00
|
|
|
int mode = bitstream_read_bit(bc);
|
|
|
|
int run = bitstream_read(bc, binkb_runbits[i]) + 1;
|
2011-02-11 14:31:32 +02:00
|
|
|
|
|
|
|
i += run;
|
|
|
|
if (i > 64) {
|
|
|
|
av_log(c->avctx, AV_LOG_ERROR, "Run went out of bounds\n");
|
2012-11-14 23:47:37 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
2011-02-11 14:31:32 +02:00
|
|
|
}
|
|
|
|
if (mode) {
|
|
|
|
v = binkb_get_value(c, BINKB_SRC_COLORS);
|
|
|
|
for (j = 0; j < run; j++)
|
|
|
|
dst[coordmap[*scan++]] = v;
|
|
|
|
} else {
|
|
|
|
for (j = 0; j < run; j++)
|
|
|
|
dst[coordmap[*scan++]] = binkb_get_value(c, BINKB_SRC_COLORS);
|
|
|
|
}
|
|
|
|
} while (i < 63);
|
|
|
|
if (i == 63)
|
|
|
|
dst[coordmap[*scan++]] = binkb_get_value(c, BINKB_SRC_COLORS);
|
|
|
|
break;
|
|
|
|
case 2:
|
2011-07-23 16:46:35 +03:00
|
|
|
memset(dctblock, 0, sizeof(*dctblock) * 64);
|
|
|
|
dctblock[0] = binkb_get_value(c, BINKB_SRC_INTRA_DC);
|
2011-02-11 14:31:32 +02:00
|
|
|
qp = binkb_get_value(c, BINKB_SRC_INTRA_Q);
|
2016-04-09 15:59:51 +02:00
|
|
|
read_dct_coeffs(bc, dctblock, bink_scan, binkb_intra_quant, qp);
|
2014-02-13 18:32:41 +03:00
|
|
|
c->binkdsp.idct_put(dst, stride, dctblock);
|
2011-02-11 14:31:32 +02:00
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
xoff = binkb_get_value(c, BINKB_SRC_X_OFF);
|
|
|
|
yoff = binkb_get_value(c, BINKB_SRC_Y_OFF) + ybias;
|
|
|
|
ref = dst + xoff + yoff * stride;
|
|
|
|
if (ref < ref_start || ref + 8*stride > ref_end) {
|
|
|
|
av_log(c->avctx, AV_LOG_WARNING, "Reference block is out of bounds\n");
|
|
|
|
} else if (ref + 8*stride < dst || ref >= dst + 8*stride) {
|
2013-03-10 19:12:01 +03:00
|
|
|
c->hdsp.put_pixels_tab[1][0](dst, ref, stride, 8);
|
2011-02-11 14:31:32 +02:00
|
|
|
} else {
|
|
|
|
put_pixels8x8_overlapped(dst, ref, stride);
|
|
|
|
}
|
2014-01-14 12:33:47 +03:00
|
|
|
c->bdsp.clear_block(block);
|
2011-02-11 14:31:32 +02:00
|
|
|
v = binkb_get_value(c, BINKB_SRC_INTER_COEFS);
|
2016-04-09 15:59:51 +02:00
|
|
|
read_residue(bc, block, v);
|
2014-02-13 18:32:41 +03:00
|
|
|
c->binkdsp.add_pixels8(dst, block, stride);
|
2011-02-11 14:31:32 +02:00
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
xoff = binkb_get_value(c, BINKB_SRC_X_OFF);
|
|
|
|
yoff = binkb_get_value(c, BINKB_SRC_Y_OFF) + ybias;
|
|
|
|
ref = dst + xoff + yoff * stride;
|
|
|
|
if (ref < ref_start || ref + 8 * stride > ref_end) {
|
|
|
|
av_log(c->avctx, AV_LOG_WARNING, "Reference block is out of bounds\n");
|
|
|
|
} else if (ref + 8*stride < dst || ref >= dst + 8*stride) {
|
2013-03-10 19:12:01 +03:00
|
|
|
c->hdsp.put_pixels_tab[1][0](dst, ref, stride, 8);
|
2011-02-11 14:31:32 +02:00
|
|
|
} else {
|
|
|
|
put_pixels8x8_overlapped(dst, ref, stride);
|
|
|
|
}
|
2011-07-23 16:46:35 +03:00
|
|
|
memset(dctblock, 0, sizeof(*dctblock) * 64);
|
|
|
|
dctblock[0] = binkb_get_value(c, BINKB_SRC_INTER_DC);
|
2011-02-11 14:31:32 +02:00
|
|
|
qp = binkb_get_value(c, BINKB_SRC_INTER_Q);
|
2016-04-09 15:59:51 +02:00
|
|
|
read_dct_coeffs(bc, dctblock, bink_scan, binkb_inter_quant, qp);
|
2014-02-13 18:32:41 +03:00
|
|
|
c->binkdsp.idct_add(dst, stride, dctblock);
|
2011-02-11 14:31:32 +02:00
|
|
|
break;
|
|
|
|
case 5:
|
|
|
|
v = binkb_get_value(c, BINKB_SRC_COLORS);
|
2014-01-14 12:33:47 +03:00
|
|
|
c->bdsp.fill_block_tab[1](dst, v, stride, 8);
|
2011-02-11 14:31:32 +02:00
|
|
|
break;
|
|
|
|
case 6:
|
|
|
|
for (i = 0; i < 2; i++)
|
|
|
|
col[i] = binkb_get_value(c, BINKB_SRC_COLORS);
|
|
|
|
for (i = 0; i < 8; i++) {
|
|
|
|
v = binkb_get_value(c, BINKB_SRC_PATTERN);
|
|
|
|
for (j = 0; j < 8; j++, v >>= 1)
|
|
|
|
dst[i*stride + j] = col[v & 1];
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 7:
|
|
|
|
xoff = binkb_get_value(c, BINKB_SRC_X_OFF);
|
|
|
|
yoff = binkb_get_value(c, BINKB_SRC_Y_OFF) + ybias;
|
|
|
|
ref = dst + xoff + yoff * stride;
|
|
|
|
if (ref < ref_start || ref + 8 * stride > ref_end) {
|
|
|
|
av_log(c->avctx, AV_LOG_WARNING, "Reference block is out of bounds\n");
|
|
|
|
} else if (ref + 8*stride < dst || ref >= dst + 8*stride) {
|
2013-03-10 19:12:01 +03:00
|
|
|
c->hdsp.put_pixels_tab[1][0](dst, ref, stride, 8);
|
2011-02-11 14:31:32 +02:00
|
|
|
} else {
|
|
|
|
put_pixels8x8_overlapped(dst, ref, stride);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 8:
|
|
|
|
for (i = 0; i < 8; i++)
|
|
|
|
memcpy(dst + i*stride, c->bundle[BINKB_SRC_COLORS].cur_ptr + i*8, 8);
|
|
|
|
c->bundle[BINKB_SRC_COLORS].cur_ptr += 64;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
av_log(c->avctx, AV_LOG_ERROR, "Unknown block type %d\n", blk);
|
2012-11-14 23:47:37 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
2011-02-11 14:31:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-04-09 15:59:51 +02:00
|
|
|
if (bitstream_tell(bc) & 0x1F) // next plane data starts at 32-bit boundary
|
|
|
|
bitstream_skip(bc, 32 - (bitstream_tell(bc) & 0x1F));
|
2011-02-11 14:31:32 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-06-03 02:09:31 +02:00
|
|
|
static int bink_put_pixels(BinkContext *c,
|
|
|
|
uint8_t *dst, uint8_t *prev, int stride,
|
|
|
|
uint8_t *ref_start,
|
|
|
|
uint8_t *ref_end)
|
|
|
|
{
|
|
|
|
int xoff = get_value(c, BINK_SRC_X_OFF);
|
|
|
|
int yoff = get_value(c, BINK_SRC_Y_OFF);
|
|
|
|
uint8_t *ref = prev + xoff + yoff * stride;
|
|
|
|
if (ref < ref_start || ref > ref_end) {
|
|
|
|
av_log(c->avctx, AV_LOG_ERROR, "Copy out of bounds @%d, %d\n",
|
|
|
|
xoff, yoff);
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
c->hdsp.put_pixels_tab[1][0](dst, ref, stride, 8);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-04-09 15:59:51 +02:00
|
|
|
static int bink_decode_plane(BinkContext *c, AVFrame *frame, BitstreamContext *bc,
|
2012-11-21 23:34:46 +03:00
|
|
|
int plane_idx, int is_chroma)
|
2010-02-21 15:28:46 +02:00
|
|
|
{
|
2012-11-14 23:47:37 +03:00
|
|
|
int blk, ret;
|
2010-02-23 09:00:46 +02:00
|
|
|
int i, j, bx, by;
|
2015-06-03 02:09:31 +02:00
|
|
|
uint8_t *dst, *prev, *ref_start, *ref_end;
|
2010-02-21 15:28:46 +02:00
|
|
|
int v, col[2];
|
|
|
|
const uint8_t *scan;
|
2013-01-20 03:02:29 +03:00
|
|
|
LOCAL_ALIGNED_16(int16_t, block, [64]);
|
2010-06-02 18:22:21 +03:00
|
|
|
LOCAL_ALIGNED_16(uint8_t, ublock, [64]);
|
2011-07-23 16:46:35 +03:00
|
|
|
LOCAL_ALIGNED_16(int32_t, dctblock, [64]);
|
2010-02-21 15:28:46 +02:00
|
|
|
int coordmap[64];
|
|
|
|
|
2012-11-21 23:34:46 +03:00
|
|
|
const int stride = frame->linesize[plane_idx];
|
2010-02-23 09:01:13 +02:00
|
|
|
int bw = is_chroma ? (c->avctx->width + 15) >> 4 : (c->avctx->width + 7) >> 3;
|
|
|
|
int bh = is_chroma ? (c->avctx->height + 15) >> 4 : (c->avctx->height + 7) >> 3;
|
|
|
|
int width = c->avctx->width >> is_chroma;
|
2010-02-21 15:28:46 +02:00
|
|
|
|
2010-02-23 09:01:13 +02:00
|
|
|
init_lengths(c, FFMAX(width, 8), bw);
|
|
|
|
for (i = 0; i < BINK_NB_SRC; i++)
|
2016-04-09 15:59:51 +02:00
|
|
|
read_bundle(bc, c, i);
|
2010-02-23 09:01:13 +02:00
|
|
|
|
2012-11-14 10:23:11 +03:00
|
|
|
ref_start = c->last->data[plane_idx] ? c->last->data[plane_idx]
|
2012-11-21 23:34:46 +03:00
|
|
|
: frame->data[plane_idx];
|
2011-09-28 01:15:31 +03:00
|
|
|
ref_end = ref_start
|
2012-11-14 10:23:11 +03:00
|
|
|
+ (bw - 1 + c->last->linesize[plane_idx] * (bh - 1)) * 8;
|
2010-02-23 09:01:13 +02:00
|
|
|
|
|
|
|
for (i = 0; i < 64; i++)
|
|
|
|
coordmap[i] = (i & 7) + (i >> 3) * stride;
|
|
|
|
|
|
|
|
for (by = 0; by < bh; by++) {
|
2016-04-09 15:59:51 +02:00
|
|
|
if ((ret = read_block_types(c->avctx, bc, &c->bundle[BINK_SRC_BLOCK_TYPES])) < 0)
|
2012-11-14 23:47:37 +03:00
|
|
|
return ret;
|
2016-04-09 15:59:51 +02:00
|
|
|
if ((ret = read_block_types(c->avctx, bc, &c->bundle[BINK_SRC_SUB_BLOCK_TYPES])) < 0)
|
2012-11-14 23:47:37 +03:00
|
|
|
return ret;
|
2016-04-09 15:59:51 +02:00
|
|
|
if ((ret = read_colors(bc, &c->bundle[BINK_SRC_COLORS], c)) < 0)
|
2012-11-14 23:47:37 +03:00
|
|
|
return ret;
|
2016-04-09 15:59:51 +02:00
|
|
|
if ((ret = read_patterns(c->avctx, bc, &c->bundle[BINK_SRC_PATTERN])) < 0)
|
2012-11-14 23:47:37 +03:00
|
|
|
return ret;
|
2016-04-09 15:59:51 +02:00
|
|
|
if ((ret = read_motion_values(c->avctx, bc, &c->bundle[BINK_SRC_X_OFF])) < 0)
|
2012-11-14 23:47:37 +03:00
|
|
|
return ret;
|
2016-04-09 15:59:51 +02:00
|
|
|
if ((ret = read_motion_values(c->avctx, bc, &c->bundle[BINK_SRC_Y_OFF])) < 0)
|
2012-11-14 23:47:37 +03:00
|
|
|
return ret;
|
2016-04-09 15:59:51 +02:00
|
|
|
if ((ret = read_dcs(c->avctx, bc, &c->bundle[BINK_SRC_INTRA_DC], DC_START_BITS, 0)) < 0)
|
2012-11-14 23:47:37 +03:00
|
|
|
return ret;
|
2016-04-09 15:59:51 +02:00
|
|
|
if ((ret = read_dcs(c->avctx, bc, &c->bundle[BINK_SRC_INTER_DC], DC_START_BITS, 1)) < 0)
|
2012-11-14 23:47:37 +03:00
|
|
|
return ret;
|
2016-04-09 15:59:51 +02:00
|
|
|
if ((ret = read_runs(c->avctx, bc, &c->bundle[BINK_SRC_RUN])) < 0)
|
2012-11-14 23:47:37 +03:00
|
|
|
return ret;
|
2010-02-23 09:01:13 +02:00
|
|
|
|
|
|
|
if (by == bh)
|
|
|
|
break;
|
2012-11-21 23:34:46 +03:00
|
|
|
dst = frame->data[plane_idx] + 8*by*stride;
|
2012-11-14 10:23:11 +03:00
|
|
|
prev = (c->last->data[plane_idx] ? c->last->data[plane_idx]
|
2012-11-21 23:34:46 +03:00
|
|
|
: frame->data[plane_idx]) + 8*by*stride;
|
2010-02-23 09:01:13 +02:00
|
|
|
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
|
|
|
|
if ((by & 1) && blk == SCALED_BLOCK) {
|
|
|
|
bx++;
|
|
|
|
dst += 8;
|
|
|
|
prev += 8;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
switch (blk) {
|
|
|
|
case SKIP_BLOCK:
|
2013-03-10 19:12:01 +03:00
|
|
|
c->hdsp.put_pixels_tab[1][0](dst, prev, stride, 8);
|
2010-02-21 15:28:46 +02:00
|
|
|
break;
|
2010-02-23 09:01:13 +02:00
|
|
|
case SCALED_BLOCK:
|
|
|
|
blk = get_value(c, BINK_SRC_SUB_BLOCK_TYPES);
|
2010-02-21 15:28:46 +02:00
|
|
|
switch (blk) {
|
|
|
|
case RUN_BLOCK:
|
2016-04-09 15:59:51 +02:00
|
|
|
scan = bink_patterns[bitstream_read(bc, 4)];
|
2010-02-21 15:28:46 +02:00
|
|
|
i = 0;
|
|
|
|
do {
|
|
|
|
int run = get_value(c, BINK_SRC_RUN) + 1;
|
|
|
|
|
|
|
|
i += run;
|
|
|
|
if (i > 64) {
|
2010-02-23 09:00:46 +02:00
|
|
|
av_log(c->avctx, AV_LOG_ERROR, "Run went out of bounds\n");
|
2012-11-14 23:47:37 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
2010-02-21 15:28:46 +02:00
|
|
|
}
|
2016-04-09 15:59:51 +02:00
|
|
|
if (bitstream_read_bit(bc)) {
|
2010-02-21 15:28:46 +02:00
|
|
|
v = get_value(c, BINK_SRC_COLORS);
|
|
|
|
for (j = 0; j < run; j++)
|
2010-02-23 09:01:13 +02:00
|
|
|
ublock[*scan++] = v;
|
2010-02-21 15:28:46 +02:00
|
|
|
} else {
|
|
|
|
for (j = 0; j < run; j++)
|
2010-02-23 09:01:13 +02:00
|
|
|
ublock[*scan++] = get_value(c, BINK_SRC_COLORS);
|
2010-02-21 15:28:46 +02:00
|
|
|
}
|
|
|
|
} while (i < 63);
|
|
|
|
if (i == 63)
|
2010-02-23 09:01:13 +02:00
|
|
|
ublock[*scan++] = get_value(c, BINK_SRC_COLORS);
|
2010-02-21 15:28:46 +02:00
|
|
|
break;
|
|
|
|
case INTRA_BLOCK:
|
2011-07-23 16:46:35 +03:00
|
|
|
memset(dctblock, 0, sizeof(*dctblock) * 64);
|
|
|
|
dctblock[0] = get_value(c, BINK_SRC_INTRA_DC);
|
2016-04-09 15:59:51 +02:00
|
|
|
read_dct_coeffs(bc, dctblock, bink_scan, bink_intra_quant, -1);
|
2014-02-13 18:32:41 +03:00
|
|
|
c->binkdsp.idct_put(ublock, 8, dctblock);
|
2010-02-21 15:28:46 +02:00
|
|
|
break;
|
|
|
|
case FILL_BLOCK:
|
|
|
|
v = get_value(c, BINK_SRC_COLORS);
|
2014-01-14 12:33:47 +03:00
|
|
|
c->bdsp.fill_block_tab[0](dst, v, stride, 16);
|
2010-02-21 15:28:46 +02:00
|
|
|
break;
|
|
|
|
case PATTERN_BLOCK:
|
|
|
|
for (i = 0; i < 2; i++)
|
|
|
|
col[i] = get_value(c, BINK_SRC_COLORS);
|
2010-02-23 09:01:13 +02:00
|
|
|
for (j = 0; j < 8; j++) {
|
2010-02-21 15:28:46 +02:00
|
|
|
v = get_value(c, BINK_SRC_PATTERN);
|
2010-02-23 09:01:13 +02:00
|
|
|
for (i = 0; i < 8; i++, v >>= 1)
|
|
|
|
ublock[i + j*8] = col[v & 1];
|
2010-02-21 15:28:46 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case RAW_BLOCK:
|
2010-02-23 09:01:13 +02:00
|
|
|
for (j = 0; j < 8; j++)
|
|
|
|
for (i = 0; i < 8; i++)
|
|
|
|
ublock[i + j*8] = get_value(c, BINK_SRC_COLORS);
|
2010-02-21 15:28:46 +02:00
|
|
|
break;
|
|
|
|
default:
|
2010-02-23 09:01:13 +02:00
|
|
|
av_log(c->avctx, AV_LOG_ERROR, "Incorrect 16x16 block type %d\n", blk);
|
2012-11-14 23:47:37 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
2010-02-23 09:01:13 +02:00
|
|
|
}
|
|
|
|
if (blk != FILL_BLOCK)
|
2014-02-13 18:32:41 +03:00
|
|
|
c->binkdsp.scale_block(ublock, dst, stride);
|
2010-02-23 09:01:13 +02:00
|
|
|
bx++;
|
|
|
|
dst += 8;
|
|
|
|
prev += 8;
|
|
|
|
break;
|
|
|
|
case MOTION_BLOCK:
|
2015-06-03 02:09:31 +02:00
|
|
|
ret = bink_put_pixels(c, dst, prev, stride,
|
|
|
|
ref_start, ref_end);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2010-02-23 09:01:13 +02:00
|
|
|
break;
|
|
|
|
case RUN_BLOCK:
|
2016-04-09 15:59:51 +02:00
|
|
|
scan = bink_patterns[bitstream_read(bc, 4)];
|
2010-02-23 09:01:13 +02:00
|
|
|
i = 0;
|
|
|
|
do {
|
|
|
|
int run = get_value(c, BINK_SRC_RUN) + 1;
|
|
|
|
|
|
|
|
i += run;
|
|
|
|
if (i > 64) {
|
|
|
|
av_log(c->avctx, AV_LOG_ERROR, "Run went out of bounds\n");
|
2012-11-14 23:47:37 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
2010-02-23 09:01:13 +02:00
|
|
|
}
|
2016-04-09 15:59:51 +02:00
|
|
|
if (bitstream_read_bit(bc)) {
|
2010-02-23 09:01:13 +02:00
|
|
|
v = get_value(c, BINK_SRC_COLORS);
|
|
|
|
for (j = 0; j < run; j++)
|
|
|
|
dst[coordmap[*scan++]] = v;
|
|
|
|
} else {
|
|
|
|
for (j = 0; j < run; j++)
|
|
|
|
dst[coordmap[*scan++]] = get_value(c, BINK_SRC_COLORS);
|
|
|
|
}
|
|
|
|
} while (i < 63);
|
|
|
|
if (i == 63)
|
|
|
|
dst[coordmap[*scan++]] = get_value(c, BINK_SRC_COLORS);
|
|
|
|
break;
|
|
|
|
case RESIDUE_BLOCK:
|
2015-06-03 02:09:31 +02:00
|
|
|
ret = bink_put_pixels(c, dst, prev, stride,
|
|
|
|
ref_start, ref_end);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2014-01-14 12:33:47 +03:00
|
|
|
c->bdsp.clear_block(block);
|
2016-04-09 15:59:51 +02:00
|
|
|
v = bitstream_read(bc, 7);
|
|
|
|
read_residue(bc, block, v);
|
2014-02-13 18:32:41 +03:00
|
|
|
c->binkdsp.add_pixels8(dst, block, stride);
|
2010-02-23 09:01:13 +02:00
|
|
|
break;
|
|
|
|
case INTRA_BLOCK:
|
2011-07-23 16:46:35 +03:00
|
|
|
memset(dctblock, 0, sizeof(*dctblock) * 64);
|
|
|
|
dctblock[0] = get_value(c, BINK_SRC_INTRA_DC);
|
2016-04-09 15:59:51 +02:00
|
|
|
read_dct_coeffs(bc, dctblock, bink_scan, bink_intra_quant, -1);
|
2014-02-13 18:32:41 +03:00
|
|
|
c->binkdsp.idct_put(dst, stride, dctblock);
|
2010-02-23 09:01:13 +02:00
|
|
|
break;
|
|
|
|
case FILL_BLOCK:
|
|
|
|
v = get_value(c, BINK_SRC_COLORS);
|
2014-01-14 12:33:47 +03:00
|
|
|
c->bdsp.fill_block_tab[1](dst, v, stride, 8);
|
2010-02-23 09:01:13 +02:00
|
|
|
break;
|
|
|
|
case INTER_BLOCK:
|
2015-06-03 02:09:31 +02:00
|
|
|
ret = bink_put_pixels(c, dst, prev, stride,
|
|
|
|
ref_start, ref_end);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2011-07-23 16:46:35 +03:00
|
|
|
memset(dctblock, 0, sizeof(*dctblock) * 64);
|
|
|
|
dctblock[0] = get_value(c, BINK_SRC_INTER_DC);
|
2016-04-09 15:59:51 +02:00
|
|
|
read_dct_coeffs(bc, dctblock, bink_scan, bink_inter_quant, -1);
|
2014-02-13 18:32:41 +03:00
|
|
|
c->binkdsp.idct_add(dst, stride, dctblock);
|
2010-02-23 09:01:13 +02:00
|
|
|
break;
|
|
|
|
case PATTERN_BLOCK:
|
|
|
|
for (i = 0; i < 2; i++)
|
|
|
|
col[i] = get_value(c, BINK_SRC_COLORS);
|
|
|
|
for (i = 0; i < 8; i++) {
|
|
|
|
v = get_value(c, BINK_SRC_PATTERN);
|
|
|
|
for (j = 0; j < 8; j++, v >>= 1)
|
|
|
|
dst[i*stride + j] = col[v & 1];
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case RAW_BLOCK:
|
|
|
|
for (i = 0; i < 8; i++)
|
|
|
|
memcpy(dst + i*stride, c->bundle[BINK_SRC_COLORS].cur_ptr + i*8, 8);
|
|
|
|
c->bundle[BINK_SRC_COLORS].cur_ptr += 64;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
av_log(c->avctx, AV_LOG_ERROR, "Unknown block type %d\n", blk);
|
2012-11-14 23:47:37 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
2010-02-21 15:28:46 +02:00
|
|
|
}
|
|
|
|
}
|
2010-02-23 09:01:13 +02:00
|
|
|
}
|
2016-04-09 15:59:51 +02:00
|
|
|
if (bitstream_tell(bc) & 0x1F) // next plane data starts at 32-bit boundary
|
|
|
|
bitstream_skip(bc, 32 - (bitstream_tell(bc) & 0x1F));
|
2010-02-23 09:00:46 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-11-13 21:35:22 +03:00
|
|
|
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *pkt)
|
2010-02-23 09:00:46 +02:00
|
|
|
{
|
|
|
|
BinkContext * const c = avctx->priv_data;
|
2012-11-21 23:34:46 +03:00
|
|
|
AVFrame *frame = data;
|
2016-04-09 15:59:51 +02:00
|
|
|
BitstreamContext bc;
|
2012-11-14 23:47:37 +03:00
|
|
|
int plane, plane_idx, ret;
|
2010-02-23 09:00:46 +02:00
|
|
|
int bits_count = pkt->size << 3;
|
|
|
|
|
2011-02-11 14:31:32 +02:00
|
|
|
if (c->version > 'b') {
|
2012-11-21 23:34:46 +03:00
|
|
|
if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0) {
|
2011-02-09 14:05:19 +02:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
|
2012-11-14 23:47:37 +03:00
|
|
|
return ret;
|
2011-02-09 14:05:19 +02:00
|
|
|
}
|
2011-02-11 14:31:32 +02:00
|
|
|
} else {
|
2012-11-21 23:34:46 +03:00
|
|
|
if ((ret = ff_reget_buffer(avctx, c->last)) < 0) {
|
2011-02-11 14:31:32 +02:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
|
2012-11-14 23:47:37 +03:00
|
|
|
return ret;
|
2011-02-11 14:31:32 +02:00
|
|
|
}
|
2012-11-21 23:34:46 +03:00
|
|
|
if ((ret = av_frame_ref(frame, c->last)) < 0)
|
|
|
|
return ret;
|
2011-02-11 14:31:32 +02:00
|
|
|
}
|
2010-02-23 09:00:46 +02:00
|
|
|
|
2016-04-09 15:59:51 +02:00
|
|
|
bitstream_init(&bc, pkt->data, bits_count);
|
2010-02-23 09:00:46 +02:00
|
|
|
if (c->has_alpha) {
|
2010-02-23 09:07:54 +02:00
|
|
|
if (c->version >= 'i')
|
2016-04-09 15:59:51 +02:00
|
|
|
bitstream_skip(&bc, 32);
|
|
|
|
if ((ret = bink_decode_plane(c, frame, &bc, 3, 0)) < 0)
|
2012-11-14 23:47:37 +03:00
|
|
|
return ret;
|
2010-02-23 09:00:46 +02:00
|
|
|
}
|
|
|
|
if (c->version >= 'i')
|
2016-04-09 15:59:51 +02:00
|
|
|
bitstream_skip(&bc, 32);
|
2010-02-23 09:00:46 +02:00
|
|
|
|
|
|
|
for (plane = 0; plane < 3; plane++) {
|
|
|
|
plane_idx = (!plane || !c->swap_planes) ? plane : (plane ^ 3);
|
|
|
|
|
2011-02-11 14:31:32 +02:00
|
|
|
if (c->version > 'b') {
|
2016-04-09 15:59:51 +02:00
|
|
|
if ((ret = bink_decode_plane(c, frame, &bc, plane_idx, !!plane)) < 0)
|
2012-11-14 23:47:37 +03:00
|
|
|
return ret;
|
2011-02-11 14:31:32 +02:00
|
|
|
} else {
|
2016-04-09 15:59:51 +02:00
|
|
|
if ((ret = binkb_decode_plane(c, frame, &bc, plane_idx,
|
2013-01-31 22:58:27 +03:00
|
|
|
!avctx->frame_number, !!plane)) < 0)
|
2012-11-14 23:47:37 +03:00
|
|
|
return ret;
|
2011-02-11 14:31:32 +02:00
|
|
|
}
|
2016-04-09 15:59:51 +02:00
|
|
|
if (bitstream_tell(&bc) >= bits_count)
|
2010-02-22 14:35:12 +02:00
|
|
|
break;
|
2010-02-21 15:28:46 +02:00
|
|
|
}
|
|
|
|
emms_c();
|
|
|
|
|
2012-11-21 23:34:46 +03:00
|
|
|
if (c->version > 'b') {
|
|
|
|
av_frame_unref(c->last);
|
|
|
|
if ((ret = av_frame_ref(c->last, frame)) < 0)
|
|
|
|
return ret;
|
|
|
|
}
|
2010-02-21 15:28:46 +02:00
|
|
|
|
2012-11-21 23:34:46 +03:00
|
|
|
*got_frame = 1;
|
2010-02-21 15:28:46 +02:00
|
|
|
|
|
|
|
/* always report that the buffer was completely consumed */
|
|
|
|
return pkt->size;
|
|
|
|
}
|
|
|
|
|
2011-02-11 14:31:32 +02:00
|
|
|
/**
|
2016-04-27 19:45:23 +02:00
|
|
|
* Calculate quantization tables for version b
|
2011-02-11 14:31:32 +02:00
|
|
|
*/
|
2011-05-26 14:37:13 +03:00
|
|
|
static av_cold void binkb_calc_quant(void)
|
2011-02-11 14:31:32 +02:00
|
|
|
{
|
2011-02-13 11:36:29 +02:00
|
|
|
uint8_t inv_bink_scan[64];
|
2011-02-12 11:15:58 +02:00
|
|
|
double s[64];
|
2011-02-11 14:31:32 +02:00
|
|
|
int i, j;
|
|
|
|
|
|
|
|
for (j = 0; j < 8; j++) {
|
|
|
|
for (i = 0; i < 8; i++) {
|
|
|
|
if (j && j != 4)
|
|
|
|
if (i && i != 4)
|
2011-02-12 11:15:58 +02:00
|
|
|
s[j*8 + i] = cos(j * M_PI/16.0) * cos(i * M_PI/16.0) * 2.0;
|
2011-02-11 14:31:32 +02:00
|
|
|
else
|
2011-02-12 11:15:58 +02:00
|
|
|
s[j*8 + i] = cos(j * M_PI/16.0) * sqrt(2.0);
|
2011-02-11 14:31:32 +02:00
|
|
|
else
|
|
|
|
if (i && i != 4)
|
2011-02-12 11:15:58 +02:00
|
|
|
s[j*8 + i] = cos(i * M_PI/16.0) * sqrt(2.0);
|
2011-02-11 14:31:32 +02:00
|
|
|
else
|
2011-02-12 11:15:58 +02:00
|
|
|
s[j*8 + i] = 1.0;
|
2011-02-11 14:31:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-13 11:36:29 +02:00
|
|
|
for (i = 0; i < 64; i++)
|
|
|
|
inv_bink_scan[bink_scan[i]] = i;
|
|
|
|
|
2011-02-11 14:31:32 +02:00
|
|
|
for (j = 0; j < 16; j++) {
|
|
|
|
for (i = 0; i < 64; i++) {
|
2011-02-13 11:36:29 +02:00
|
|
|
int k = inv_bink_scan[i];
|
2011-02-12 11:15:58 +02:00
|
|
|
if (s[i] == 1.0) {
|
2011-02-13 11:36:29 +02:00
|
|
|
binkb_intra_quant[j][k] = (1L << 12) * binkb_intra_seed[i] *
|
2011-02-12 11:15:58 +02:00
|
|
|
binkb_num[j]/binkb_den[j];
|
2011-02-13 11:36:29 +02:00
|
|
|
binkb_inter_quant[j][k] = (1L << 12) * binkb_inter_seed[i] *
|
2011-02-12 11:15:58 +02:00
|
|
|
binkb_num[j]/binkb_den[j];
|
|
|
|
} else {
|
2011-02-13 11:36:29 +02:00
|
|
|
binkb_intra_quant[j][k] = (1L << 12) * binkb_intra_seed[i] * s[i] *
|
2011-02-12 11:15:58 +02:00
|
|
|
binkb_num[j]/(double)binkb_den[j];
|
2011-02-13 11:36:29 +02:00
|
|
|
binkb_inter_quant[j][k] = (1L << 12) * binkb_inter_seed[i] * s[i] *
|
2011-02-12 11:15:58 +02:00
|
|
|
binkb_num[j]/(double)binkb_den[j];
|
|
|
|
}
|
2011-02-11 14:31:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-21 15:28:46 +02:00
|
|
|
static av_cold int decode_init(AVCodecContext *avctx)
|
|
|
|
{
|
|
|
|
BinkContext * const c = avctx->priv_data;
|
|
|
|
static VLC_TYPE table[16 * 128][2];
|
2011-02-11 14:31:32 +02:00
|
|
|
static int binkb_initialised = 0;
|
2012-11-14 23:47:37 +03:00
|
|
|
int i, ret;
|
2010-02-23 08:39:23 +02:00
|
|
|
int flags;
|
2010-02-21 15:28:46 +02:00
|
|
|
|
|
|
|
c->version = avctx->codec_tag >> 24;
|
2010-02-23 08:39:23 +02:00
|
|
|
if (avctx->extradata_size < 4) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Extradata missing or too short\n");
|
2012-11-14 23:47:37 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
2010-02-23 08:39:23 +02:00
|
|
|
}
|
|
|
|
flags = AV_RL32(avctx->extradata);
|
|
|
|
c->has_alpha = flags & BINK_FLAG_ALPHA;
|
2010-03-06 17:09:14 +02:00
|
|
|
c->swap_planes = c->version >= 'h';
|
2010-02-21 15:28:46 +02:00
|
|
|
if (!bink_trees[15].table) {
|
|
|
|
for (i = 0; i < 16; i++) {
|
|
|
|
const int maxbits = bink_tree_lens[i][15];
|
|
|
|
bink_trees[i].table = table + i*128;
|
|
|
|
bink_trees[i].table_allocated = 1 << maxbits;
|
|
|
|
init_vlc(&bink_trees[i], maxbits, 16,
|
|
|
|
bink_tree_lens[i], 1, 1,
|
|
|
|
bink_tree_bits[i], 1, 1, INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c->avctx = avctx;
|
|
|
|
|
2012-11-21 23:34:46 +03:00
|
|
|
c->last = av_frame_alloc();
|
|
|
|
if (!c->last)
|
2012-11-14 10:23:11 +03:00
|
|
|
return AVERROR(ENOMEM);
|
2010-02-21 15:28:46 +02:00
|
|
|
|
2012-11-14 23:47:37 +03:00
|
|
|
if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0)
|
|
|
|
return ret;
|
|
|
|
|
2012-10-06 13:10:34 +03:00
|
|
|
avctx->pix_fmt = c->has_alpha ? AV_PIX_FMT_YUVA420P : AV_PIX_FMT_YUV420P;
|
2010-02-21 15:28:46 +02:00
|
|
|
|
2016-12-07 09:18:41 +02:00
|
|
|
ff_blockdsp_init(&c->bdsp);
|
2013-03-10 19:12:01 +03:00
|
|
|
ff_hpeldsp_init(&c->hdsp, avctx->flags);
|
2014-02-13 18:32:41 +03:00
|
|
|
ff_binkdsp_init(&c->binkdsp);
|
2010-02-21 15:28:46 +02:00
|
|
|
|
|
|
|
init_bundles(c);
|
|
|
|
|
2011-02-11 14:31:32 +02:00
|
|
|
if (c->version == 'b') {
|
|
|
|
if (!binkb_initialised) {
|
|
|
|
binkb_calc_quant();
|
|
|
|
binkb_initialised = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-21 15:28:46 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static av_cold int decode_end(AVCodecContext *avctx)
|
|
|
|
{
|
|
|
|
BinkContext * const c = avctx->priv_data;
|
|
|
|
|
2012-11-21 23:34:46 +03:00
|
|
|
av_frame_free(&c->last);
|
2010-02-21 15:28:46 +02:00
|
|
|
|
|
|
|
free_bundles(c);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-01-25 23:40:11 +02:00
|
|
|
AVCodec ff_bink_decoder = {
|
2011-07-17 13:54:31 +03:00
|
|
|
.name = "binkvideo",
|
2013-10-03 23:57:53 +03:00
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("Bink video"),
|
2011-07-17 13:54:31 +03:00
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
2012-08-05 12:11:04 +03:00
|
|
|
.id = AV_CODEC_ID_BINKVIDEO,
|
2011-07-17 13:54:31 +03:00
|
|
|
.priv_data_size = sizeof(BinkContext),
|
|
|
|
.init = decode_init,
|
|
|
|
.close = decode_end,
|
|
|
|
.decode = decode_frame,
|
2015-07-07 02:41:27 +02:00
|
|
|
.capabilities = AV_CODEC_CAP_DR1,
|
2010-02-21 15:28:46 +02:00
|
|
|
};
|