30 #define BITSTREAM_READER_LE
33 #define BINK_FLAG_ALPHA 0x00100000
34 #define BINK_FLAG_GRAY 0x00020000
57 4, 8, 8, 5, 5, 11, 11, 4, 4, 7
61 0, 0, 0, 1, 1, 0, 1, 0, 0, 0
92 #define GET_HUFF(gb, tree) (tree).syms[get_vlc2(gb, bink_trees[(tree).vlc_num].table,\
93 bink_trees[(tree).vlc_num].bits, 1)]
207 uint8_t *src2 = src +
size;
218 }
while (size && size2);
234 uint8_t tmp1[16], tmp2[16], *in = tmp1, *out = tmp2;
239 for (i = 0; i < 16; i++)
245 memset(tmp1, 0,
sizeof(tmp1));
246 for (i = 0; i <=
len; i++) {
248 tmp1[tree->
syms[i]] = 1;
250 for (i = 0; i < 16 && len < 16 - 1; i++)
255 for (i = 0; i < 16; i++)
257 for (i = 0; i <=
len; i++) {
259 for (t = 0; t < 16; t += size << 1)
260 merge(gb, out + t, in + t, size);
261 FFSWAP(uint8_t*, in, out);
263 memcpy(tree->
syms, in, 16);
279 for (i = 0; i < 16; i++)
296 #define CHECK_READ_VAL(gb, b, t) \
297 if (!b->cur_dec || (b->cur_dec > b->cur_ptr)) \
299 t = get_bits(gb, b->len); \
308 const uint8_t *dec_end;
330 const uint8_t *dec_end;
342 v = (v ^ sign) - sign;
351 v = (v ^ sign) - sign;
365 const uint8_t *dec_end;
386 if (dec_end - b->
cur_dec < run)
399 const uint8_t *dec_end;
419 const uint8_t *dec_end;
432 sign = ((int8_t) v) >> 7;
433 v = ((v & 0x7F) ^ sign) - sign;
444 sign = ((int8_t) v) >> 7;
445 v = ((v & 0x7F) ^ sign) - sign;
455 #define DC_START_BITS 11
458 int start_bits,
int has_sign)
460 int i, j,
len, len2, bsize, sign,
v, v2;
461 int16_t *dst = (int16_t*)b->
cur_dec;
462 int16_t *dst_end = (int16_t*)b->
data_end;
465 v =
get_bits(gb, start_bits - has_sign);
468 v = (v ^ sign) - sign;
470 if (dst_end - dst < 1)
474 for (i = 0; i <
len; i += 8) {
475 len2 =
FFMIN(len - i, 8);
476 if (dst_end - dst < len2)
480 for (j = 0; j < len2; j++) {
484 v2 = (v2 ^ sign) - sign;
488 if (v < -32768 || v > 32767) {
494 for (j = 0; j < len2; j++)
539 const int mask = 1 << (bits - 1);
549 for (i = 0; i <
len; i++)
552 for (i = 0; i <
len; i++)
556 int16_t *dst = (int16_t*)b->
cur_dec;
559 for (i = 0; i <
len; i++)
562 for (i = 0; i <
len; i++)
594 const int32_t quant_matrices[16][64],
int q)
598 int i,
t,
bits, ccoef, mode, sign;
599 int list_start = 64, list_end = 64, list_pos;
603 const int32_t *
quant;
605 coef_list[list_end] = 4; mode_list[list_end++] = 0;
606 coef_list[list_end] = 24; mode_list[list_end++] = 0;
607 coef_list[list_end] = 44; mode_list[list_end++] = 0;
608 coef_list[list_end] = 1; mode_list[list_end++] = 3;
609 coef_list[list_end] = 2; mode_list[list_end++] = 3;
610 coef_list[list_end] = 3; mode_list[list_end++] = 3;
612 for (bits =
get_bits(gb, 4) - 1; bits >= 0; bits--) {
613 list_pos = list_start;
614 while (list_pos < list_end) {
615 if (!(mode_list[list_pos] | coef_list[list_pos]) || !
get_bits1(gb)) {
619 ccoef = coef_list[list_pos];
620 mode = mode_list[list_pos];
623 coef_list[list_pos] = ccoef + 4;
624 mode_list[list_pos] = 1;
627 coef_list[list_pos] = 0;
628 mode_list[list_pos++] = 0;
630 for (i = 0; i < 4; i++, ccoef++) {
632 coef_list[--list_start] = ccoef;
633 mode_list[ list_start] = 3;
640 t = (t ^ sign) - sign;
642 block[scan[ccoef]] =
t;
643 coef_idx[coef_count++] = ccoef;
648 mode_list[list_pos] = 2;
649 for (i = 0; i < 3; i++) {
651 coef_list[list_end] = ccoef;
652 mode_list[list_end++] = 2;
661 t = (t ^ sign) - sign;
663 block[scan[ccoef]] =
t;
664 coef_idx[coef_count++] = ccoef;
665 coef_list[list_pos] = 0;
666 mode_list[list_pos++] = 0;
681 quant = quant_matrices[quant_idx];
683 block[0] = (block[0] * quant[0]) >> 11;
684 for (i = 0; i < coef_count; i++) {
685 int idx = coef_idx[i];
686 block[scan[idx]] = (block[scan[idx]] * quant[idx]) >> 11;
704 int i, sign,
mask, ccoef, mode;
705 int list_start = 64, list_end = 64, list_pos;
707 int nz_coeff_count = 0;
709 coef_list[list_end] = 4; mode_list[list_end++] = 0;
710 coef_list[list_end] = 24; mode_list[list_end++] = 0;
711 coef_list[list_end] = 44; mode_list[list_end++] = 0;
712 coef_list[list_end] = 0; mode_list[list_end++] = 2;
715 for (i = 0; i < nz_coeff_count; i++) {
718 if (block[nz_coeff[i]] < 0)
719 block[nz_coeff[i]] -=
mask;
721 block[nz_coeff[i]] +=
mask;
726 list_pos = list_start;
727 while (list_pos < list_end) {
728 if (!(coef_list[list_pos] | mode_list[list_pos]) || !
get_bits1(gb)) {
732 ccoef = coef_list[list_pos];
733 mode = mode_list[list_pos];
736 coef_list[list_pos] = ccoef + 4;
737 mode_list[list_pos] = 1;
740 coef_list[list_pos] = 0;
741 mode_list[list_pos++] = 0;
743 for (i = 0; i < 4; i++, ccoef++) {
745 coef_list[--list_start] = ccoef;
746 mode_list[ list_start] = 3;
748 nz_coeff[nz_coeff_count++] =
bink_scan[ccoef];
750 block[
bink_scan[ccoef]] = (mask ^ sign) - sign;
758 mode_list[list_pos] = 2;
759 for (i = 0; i < 3; i++) {
761 coef_list[list_end] = ccoef;
762 mode_list[list_end++] = 2;
766 nz_coeff[nz_coeff_count++] =
bink_scan[ccoef];
768 block[
bink_scan[ccoef]] = (mask ^ sign) - sign;
769 coef_list[list_pos] = 0;
770 mode_list[list_pos++] = 0;
789 for (i = 0; i < 8; i++)
790 memcpy(tmp + i*8, src + i*stride, 8);
791 for (i = 0; i < 8; i++)
792 memcpy(dst + i*stride, tmp + i*8, 8);
796 int is_key,
int is_chroma)
800 uint8_t *dst, *ref, *ref_start, *ref_end;
807 int ybias = is_key ? -15 : 0;
815 ref_start = c->
pic.
data[plane_idx];
818 for (i = 0; i < 64; i++)
819 coordmap[i] = (i & 7) + (i >> 3) * stride;
821 for (by = 0; by < bh; by++) {
828 for (bx = 0; bx < bw; bx++, dst += 8) {
849 for (j = 0; j <
run; j++)
850 dst[coordmap[*scan++]] = v;
852 for (j = 0; j <
run; j++)
860 memset(dctblock, 0,
sizeof(*dctblock) * 64);
869 ref = dst + xoff + yoff *
stride;
870 if (ref < ref_start || ref + 8*stride > ref_end) {
872 }
else if (ref + 8*stride < dst || ref >= dst + 8*stride) {
885 ref = dst + xoff + yoff *
stride;
886 if (ref < ref_start || ref + 8 * stride > ref_end) {
888 }
else if (ref + 8*stride < dst || ref >= dst + 8*stride) {
893 memset(dctblock, 0,
sizeof(*dctblock) * 64);
904 for (i = 0; i < 2; i++)
906 for (i = 0; i < 8; i++) {
908 for (j = 0; j < 8; j++, v >>= 1)
909 dst[i*stride + j] = col[v & 1];
915 ref = dst + xoff + yoff *
stride;
916 if (ref < ref_start || ref + 8 * stride > ref_end) {
918 }
else if (ref + 8*stride < dst || ref >= dst + 8*stride) {
925 for (i = 0; i < 8; i++)
946 uint8_t *dst, *prev, *ref, *ref_start, *ref_end;
967 + (bw - 1 + c->
last.
linesize[plane_idx] * (bh - 1)) * 8;
969 for (i = 0; i < 64; i++)
970 coordmap[i] = (i & 7) + (i >> 3) * stride;
972 for (by = 0; by < bh; by++) {
996 : c->
pic.
data[plane_idx]) + 8*by*stride;
997 for (bx = 0; bx < bw; bx++, dst += 8, prev += 8) {
1026 for (j = 0; j <
run; j++)
1027 ublock[*scan++] = v;
1029 for (j = 0; j <
run; j++)
1037 memset(dctblock, 0,
sizeof(*dctblock) * 64);
1047 for (i = 0; i < 2; i++)
1049 for (j = 0; j < 8; j++) {
1051 for (i = 0; i < 8; i++, v >>= 1)
1052 ublock[i + j*8] = col[v & 1];
1056 for (j = 0; j < 8; j++)
1057 for (i = 0; i < 8; i++)
1073 ref = prev + xoff + yoff *
stride;
1074 if (ref < ref_start || ref > ref_end) {
1076 bx*8 + xoff, by*8 + yoff);
1094 for (j = 0; j <
run; j++)
1095 dst[coordmap[*scan++]] = v;
1097 for (j = 0; j <
run; j++)
1107 ref = prev + xoff + yoff *
stride;
1108 if (ref < ref_start || ref > ref_end) {
1110 bx*8 + xoff, by*8 + yoff);
1120 memset(dctblock, 0,
sizeof(*dctblock) * 64);
1132 ref = prev + xoff + yoff *
stride;
1134 memset(dctblock, 0,
sizeof(*dctblock) * 64);
1140 for (i = 0; i < 2; i++)
1142 for (i = 0; i < 8; i++) {
1144 for (j = 0; j < 8; j++, v >>= 1)
1145 dst[i*stride + j] = col[v & 1];
1149 for (i = 0; i < 8; i++)
1169 int plane, plane_idx;
1170 int bits_count = pkt->
size << 3;
1197 for (plane = 0; plane < 3; plane++) {
1198 plane_idx = (!plane || !c->
swap_planes) ? plane : (plane ^ 3);
1227 uint8_t inv_bink_scan[64];
1231 for (j = 0; j < 8; j++) {
1232 for (i = 0; i < 8; i++) {
1235 s[j*8 + i] = cos(j *
M_PI/16.0) * cos(i *
M_PI/16.0) * 2.0;
1237 s[j*8 + i] = cos(j *
M_PI/16.0) * sqrt(2.0);
1240 s[j*8 + i] = cos(i *
M_PI/16.0) * sqrt(2.0);
1246 for (i = 0; i < 64; i++)
1249 for (j = 0; j < 16; j++) {
1250 for (i = 0; i < 64; i++) {
1251 int k = inv_bink_scan[i];
1270 static VLC_TYPE table[16 * 128][2];
1271 static int binkb_initialised = 0;
1283 if (!bink_trees[15].table) {
1284 for (i = 0; i < 16; i++) {
1286 bink_trees[i].
table = table + i*128;
1288 init_vlc(&bink_trees[i], maxbits, 16,
1310 if (!binkb_initialised) {
1312 binkb_initialised = 1;
1333 .
name =
"binkvideo",
int(* get_buffer)(struct AVCodecContext *c, AVFrame *pic)
Called at the beginning of each frame to get a buffer for it.
enum PixelFormat pix_fmt
Pixel format, see PIX_FMT_xxx.
op_pixels_func put_pixels_tab[4][4]
Halfpel motion compensation with rounding (a+b+1)>>1.
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
static const uint8_t bink_tree_lens[16][16]
8-bit values for 2-colour pattern fill
static const uint8_t bink_tree_bits[16][16]
#define CHECK_READ_VAL(gb, b, t)
common check before starting decoding bundle data
static int bink_decode_plane(BinkContext *c, GetBitContext *gb, int plane_idx, int is_chroma)
void(* release_buffer)(struct AVCodecContext *c, AVFrame *pic)
Called to release buffers which were allocated with get_buffer.
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
void(* scale_block)(const uint8_t src[64], uint8_t *dst, int linesize)
static void skip_bits_long(GetBitContext *s, int n)
static void binkb_init_bundle(BinkContext *c, int bundle_num)
#define AV_LOG_WARNING
Something somehow does not look correct.
static const uint8_t binkb_den[16]
8-bit values for 2-colour pattern fill
static const uint8_t bink_scan[64]
Bink DCT and residue 8x8 block scan order.
Tree col_high[16]
trees for decoding high nibble in "colours" data type
16x16 block types (a subset of 8x8 block types)
int len
length of number of entries to decode (in bits)
int(* reget_buffer)(struct AVCodecContext *c, AVFrame *pic)
Called at the beginning of a frame to get cr buffer for it.
static int32_t binkb_inter_quant[16][64]
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
quantizer values for interblocks with DCT
static int read_patterns(AVCodecContext *avctx, GetBitContext *gb, Bundle *b)
static int decode(MimicContext *ctx, int quality, int num_coeffs, int is_iframe)
X components of motion value.
motion block with DCT applied to the difference
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
static int binkb_get_value(BinkContext *c, int bundle_num)
static void put_pixels8x8_overlapped(uint8_t *dst, uint8_t *src, int stride)
Copy 8x8 block from source to destination, where src and dst may be overlapped.
Tree tree
Huffman tree-related data.
struct Tree Tree
data needed to decode 4-bit Huffman-coded value
BlockTypes
Bink video block types.
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
static const int32_t bink_inter_quant[16][64]
static int get_bits_count(const GetBitContext *s)
int vlc_num
tree number (in bink_trees[])
static av_cold void init_bundles(BinkContext *c)
Allocate memory for bundles.
bitstream reader API header.
static int read_runs(AVCodecContext *avctx, GetBitContext *gb, Bundle *b)
data structure used for decoding single Bink data type
#define LOCAL_ALIGNED_16(t, v,...)
static const uint8_t bink_patterns[16][64]
av_cold void dsputil_init(DSPContext *c, AVCodecContext *avctx)
static int init(AVCodecParserContext *s)
uint8_t * data
buffer for decoded symbols
block is copied from previous frame with some offset
static int read_dct_coeffs(GetBitContext *gb, int32_t block[64], const uint8_t *scan, const int32_t quant_matrices[16][64], int q)
Read 8x8 block of DCT coefficients.
uint8_t * data_end
buffer end
static const uint16_t mask[17]
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
static av_cold void free_bundles(BinkContext *c)
Free memory used by bundles.
void av_log(void *avcl, int level, const char *fmt,...)
const char * name
Name of the codec implementation.
int col_lastval
value of last decoded high nibble in "colours" data type
void(* clear_block)(DCTELEM *block)
DC values for interblocks with DCT.
void(* add_pixels8)(uint8_t *pixels, DCTELEM *block, int line_size)
static av_always_inline void emms_c(void)
Empty mmx state.
block is composed from runs of colours with custom scan order
block is filled with single colour
static av_cold void binkb_calc_quant(void)
Caclulate quantization tables for version b.
int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
Check if the given dimension of an image is valid, meaning that all bytes of the image can be address...
int width
picture width / height.
struct AVFrame AVFrame
Audio Video Frame.
int idct_algo
IDCT algorithm, see FF_IDCT_* below.
Y components of motion value.
number of coefficients for residue blocks
static const uint8_t binkb_inter_seed[64]
#define INIT_VLC_USE_NEW_STATIC
static const uint8_t binkb_runbits[64]
data needed to decode 4-bit Huffman-coded value
uint8_t * cur_dec
pointer to the not yet decoded part of the buffer
quantizer values for intrablocks with DCT
int linesize[AV_NUM_DATA_POINTERS]
Size, in bytes, of the data for each picture/channel plane.
#define DC_START_BITS
number of bits used to store first DC value in bundle
main external API structure.
static void close(AVCodecParserContext *s)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
op_fill_func fill_block_tab[2]
#define init_vlc(vlc, nb_bits, nb_codes,bits, bits_wrap, bits_size,codes, codes_wrap, codes_size,flags)
static av_cold int decode_init(AVCodecContext *avctx)
static unsigned int get_bits1(GetBitContext *s)
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
static const int32_t bink_intra_quant[16][64]
DC values for interblocks with DCT.
Sources
IDs for different data types used in Bink video codec.
block is filled with two colours following custom pattern
static int32_t binkb_intra_quant[16][64]
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
static int read_residue(GetBitContext *gb, DCTELEM block[64], int masks_count)
Read 8x8 block with residue after motion compensation.
static const uint8_t binkb_num[16]
void ff_binkdsp_init(BinkDSPContext *c)
#define GET_HUFF(gb, tree)
run lengths for special fill block
Y components of motion value.
static VLC bink_trees[16]
struct Bundle Bundle
data structure used for decoding single Bink data type
static void merge(GetBitContext *gb, uint8_t *dst, uint8_t *src, int size)
Merge two consequent lists of equal size depending on bits read.
Bundle bundle[BINKB_NB_SRC]
bundles for decoding all data types
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
pixel values used for different block types
void(* idct_add)(uint8_t *dest, int line_size, int32_t *block)
X components of motion value.
uint8_t * cur_ptr
pointer to the data that is not read from buffer yet
static const uint8_t binkb_intra_seed[64]
static int binkb_read_bundle(BinkContext *c, GetBitContext *gb, int bundle_num)
static void init_lengths(BinkContext *c, int width, int bw)
Initialize length length in all bundles.
static int read_colors(GetBitContext *gb, Bundle *b, BinkContext *c)
static int read_block_types(AVCodecContext *avctx, GetBitContext *gb, Bundle *b)
static void read_tree(GetBitContext *gb, Tree *tree)
Read information about Huffman tree used to decode data.
static void binkb_init_bundles(BinkContext *c)
uint8_t syms[16]
leaf value to symbol mapping
static int read_dcs(AVCodecContext *avctx, GetBitContext *gb, Bundle *b, int start_bits, int has_sign)
DC values for intrablocks with DCT.
static int read_motion_values(AVCodecContext *avctx, GetBitContext *gb, Bundle *b)
struct BinkContext BinkContext
motion block with some difference added
static int binkb_decode_plane(BinkContext *c, GetBitContext *gb, int plane_idx, int is_key, int is_chroma)
VLC_TYPE(* table)[2]
code, bits
void(* idct_put)(uint8_t *dest, int line_size, int32_t *block)
static const int binkb_bundle_sizes[BINKB_NB_SRC]
int version
internal Bink file version
static const int binkb_bundle_signed[BINKB_NB_SRC]
static av_cold int decode_end(AVCodecContext *avctx)
#define FFSWAP(type, a, b)
static void read_bundle(GetBitContext *gb, BinkContext *c, int bundle_num)
Prepare bundle for decoding data.
static const uint8_t bink_rlelens[4]
pixel values used for different block types
static int get_value(BinkContext *c, int bundle)
Retrieve next value from bundle.
static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *pkt)
DC values for intrablocks with DCT.
OldSources
IDs for different data types used in old version of Bink video codec.
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)