3
3
* Copyright (c) 2009 Konstantin Shishkov
5
* This file is part of FFmpeg.
7
* FFmpeg is free software; you can redistribute it and/or
4
* Copyright (C) 2011 Peter Ross <pross@xvid.org>
6
* This file is part of Libav.
8
* Libav is free software; you can redistribute it and/or
8
9
* modify it under the terms of the GNU Lesser General Public
9
10
* License as published by the Free Software Foundation; either
10
11
* version 2.1 of the License, or (at your option) any later version.
12
* FFmpeg is distributed in the hope that it will be useful,
13
* Libav is distributed in the hope that it will be useful,
13
14
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
15
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
16
* Lesser General Public License for more details.
17
18
* You should have received a copy of the GNU Lesser General Public
18
* License along with FFmpeg; if not, write to the Free Software
19
* License along with Libav; if not, write to the Free Software
19
20
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23
#include "libavutil/imgutils.h"
22
24
#include "avcodec.h"
23
25
#include "dsputil.h"
24
26
#include "binkdata.h"
33
35
static VLC bink_trees[16];
38
* IDs for different data types used in old version of Bink video codec
41
BINKB_SRC_BLOCK_TYPES = 0, ///< 8x8 block types
42
BINKB_SRC_COLORS, ///< pixel values used for different block types
43
BINKB_SRC_PATTERN, ///< 8-bit values for 2-colour pattern fill
44
BINKB_SRC_X_OFF, ///< X components of motion value
45
BINKB_SRC_Y_OFF, ///< Y components of motion value
46
BINKB_SRC_INTRA_DC, ///< DC values for intrablocks with DCT
47
BINKB_SRC_INTER_DC, ///< DC values for interblocks with DCT
48
BINKB_SRC_INTRA_Q, ///< quantizer values for intrablocks with DCT
49
BINKB_SRC_INTER_Q, ///< quantizer values for interblocks with DCT
50
BINKB_SRC_INTER_COEFS, ///< number of coefficients for residue blocks
55
static const int binkb_bundle_sizes[BINKB_NB_SRC] = {
56
4, 8, 8, 5, 5, 11, 11, 4, 4, 7
59
static const int binkb_bundle_signed[BINKB_NB_SRC] = {
60
0, 0, 0, 1, 1, 0, 1, 0, 0, 0
63
static uint32_t binkb_intra_quant[16][64];
64
static uint32_t binkb_inter_quant[16][64];
36
67
* IDs for different data types used in Bink video codec
514
static void binkb_init_bundle(BinkContext *c, int bundle_num)
516
c->bundle[bundle_num].cur_dec =
517
c->bundle[bundle_num].cur_ptr = c->bundle[bundle_num].data;
518
c->bundle[bundle_num].len = 13;
521
static void binkb_init_bundles(BinkContext *c)
524
for (i = 0; i < BINKB_NB_SRC; i++)
525
binkb_init_bundle(c, i);
528
static int binkb_read_bundle(BinkContext *c, GetBitContext *gb, int bundle_num)
530
const int bits = binkb_bundle_sizes[bundle_num];
531
const int mask = 1 << (bits - 1);
532
const int issigned = binkb_bundle_signed[bundle_num];
533
Bundle *b = &c->bundle[bundle_num];
536
CHECK_READ_VAL(gb, b, len);
539
for (i = 0; i < len; i++)
540
*b->cur_dec++ = get_bits(gb, bits);
542
for (i = 0; i < len; i++)
543
*b->cur_dec++ = get_bits(gb, bits) - mask;
546
int16_t *dst = (int16_t*)b->cur_dec;
549
for (i = 0; i < len; i++)
550
*dst++ = get_bits(gb, bits);
552
for (i = 0; i < len; i++)
553
*dst++ = get_bits(gb, bits) - mask;
555
b->cur_dec = (uint8_t*)dst;
560
static inline int binkb_get_value(BinkContext *c, int bundle_num)
563
const int bits = binkb_bundle_sizes[bundle_num];
566
int val = *c->bundle[bundle_num].cur_ptr++;
567
return binkb_bundle_signed[bundle_num] ? (int8_t)val : val;
569
ret = *(int16_t*)c->bundle[bundle_num].cur_ptr;
570
c->bundle[bundle_num].cur_ptr += 2;
484
* Reads 8x8 block of DCT coefficients.
575
* Read 8x8 block of DCT coefficients.
486
577
* @param gb context for reading bits
487
578
* @param block place for storing coefficients
488
579
* @param scan scan order table
489
* @param is_intra tells what set of quantizer matrices to use
580
* @param quant_matrices quantization matrices
490
581
* @return 0 for success, negative value in other cases
492
583
static int read_dct_coeffs(GetBitContext *gb, DCTELEM block[64], const uint8_t *scan,
584
const uint32_t quant_matrices[16][64], int q)
495
586
int coef_list[128];
496
587
int mode_list[128];
772
* Copy 8x8 block from source to destination, where src and dst may be overlapped
774
static inline void put_pixels8x8_overlapped(uint8_t *dst, uint8_t *src, int stride)
778
for (i = 0; i < 8; i++)
779
memcpy(tmp + i*8, src + i*stride, 8);
780
for (i = 0; i < 8; i++)
781
memcpy(dst + i*stride, tmp + i*8, 8);
784
static int binkb_decode_plane(BinkContext *c, GetBitContext *gb, int plane_idx,
785
int is_key, int is_chroma)
789
uint8_t *dst, *ref, *ref_start, *ref_end;
793
LOCAL_ALIGNED_16(DCTELEM, block, [64]);
795
int ybias = is_key ? -15 : 0;
798
const int stride = c->pic.linesize[plane_idx];
799
int bw = is_chroma ? (c->avctx->width + 15) >> 4 : (c->avctx->width + 7) >> 3;
800
int bh = is_chroma ? (c->avctx->height + 15) >> 4 : (c->avctx->height + 7) >> 3;
802
binkb_init_bundles(c);
803
ref_start = c->pic.data[plane_idx];
804
ref_end = c->pic.data[plane_idx] + (bh * c->pic.linesize[plane_idx] + bw) * 8;
806
for (i = 0; i < 64; i++)
807
coordmap[i] = (i & 7) + (i >> 3) * stride;
809
for (by = 0; by < bh; by++) {
810
for (i = 0; i < BINKB_NB_SRC; i++) {
811
if (binkb_read_bundle(c, gb, i) < 0)
815
dst = c->pic.data[plane_idx] + 8*by*stride;
816
for (bx = 0; bx < bw; bx++, dst += 8) {
817
blk = binkb_get_value(c, BINKB_SRC_BLOCK_TYPES);
822
scan = bink_patterns[get_bits(gb, 4)];
827
mode = get_bits1(gb);
828
run = get_bits(gb, binkb_runbits[i]) + 1;
832
av_log(c->avctx, AV_LOG_ERROR, "Run went out of bounds\n");
836
v = binkb_get_value(c, BINKB_SRC_COLORS);
837
for (j = 0; j < run; j++)
838
dst[coordmap[*scan++]] = v;
840
for (j = 0; j < run; j++)
841
dst[coordmap[*scan++]] = binkb_get_value(c, BINKB_SRC_COLORS);
845
dst[coordmap[*scan++]] = binkb_get_value(c, BINKB_SRC_COLORS);
848
c->dsp.clear_block(block);
849
block[0] = binkb_get_value(c, BINKB_SRC_INTRA_DC);
850
qp = binkb_get_value(c, BINKB_SRC_INTRA_Q);
851
read_dct_coeffs(gb, block, c->scantable.permutated, binkb_intra_quant, qp);
852
c->dsp.idct_put(dst, stride, block);
855
xoff = binkb_get_value(c, BINKB_SRC_X_OFF);
856
yoff = binkb_get_value(c, BINKB_SRC_Y_OFF) + ybias;
857
ref = dst + xoff + yoff * stride;
858
if (ref < ref_start || ref + 8*stride > ref_end) {
859
av_log(c->avctx, AV_LOG_WARNING, "Reference block is out of bounds\n");
860
} else if (ref + 8*stride < dst || ref >= dst + 8*stride) {
861
c->dsp.put_pixels_tab[1][0](dst, ref, stride, 8);
863
put_pixels8x8_overlapped(dst, ref, stride);
865
c->dsp.clear_block(block);
866
v = binkb_get_value(c, BINKB_SRC_INTER_COEFS);
867
read_residue(gb, block, v);
868
c->dsp.add_pixels8(dst, block, stride);
871
xoff = binkb_get_value(c, BINKB_SRC_X_OFF);
872
yoff = binkb_get_value(c, BINKB_SRC_Y_OFF) + ybias;
873
ref = dst + xoff + yoff * stride;
874
if (ref < ref_start || ref + 8 * stride > ref_end) {
875
av_log(c->avctx, AV_LOG_WARNING, "Reference block is out of bounds\n");
876
} else if (ref + 8*stride < dst || ref >= dst + 8*stride) {
877
c->dsp.put_pixels_tab[1][0](dst, ref, stride, 8);
879
put_pixels8x8_overlapped(dst, ref, stride);
881
c->dsp.clear_block(block);
882
block[0] = binkb_get_value(c, BINKB_SRC_INTER_DC);
883
qp = binkb_get_value(c, BINKB_SRC_INTER_Q);
884
read_dct_coeffs(gb, block, c->scantable.permutated, binkb_inter_quant, qp);
885
c->dsp.idct_add(dst, stride, block);
888
v = binkb_get_value(c, BINKB_SRC_COLORS);
889
c->dsp.fill_block_tab[1](dst, v, stride, 8);
892
for (i = 0; i < 2; i++)
893
col[i] = binkb_get_value(c, BINKB_SRC_COLORS);
894
for (i = 0; i < 8; i++) {
895
v = binkb_get_value(c, BINKB_SRC_PATTERN);
896
for (j = 0; j < 8; j++, v >>= 1)
897
dst[i*stride + j] = col[v & 1];
901
xoff = binkb_get_value(c, BINKB_SRC_X_OFF);
902
yoff = binkb_get_value(c, BINKB_SRC_Y_OFF) + ybias;
903
ref = dst + xoff + yoff * stride;
904
if (ref < ref_start || ref + 8 * stride > ref_end) {
905
av_log(c->avctx, AV_LOG_WARNING, "Reference block is out of bounds\n");
906
} else if (ref + 8*stride < dst || ref >= dst + 8*stride) {
907
c->dsp.put_pixels_tab[1][0](dst, ref, stride, 8);
909
put_pixels8x8_overlapped(dst, ref, stride);
913
for (i = 0; i < 8; i++)
914
memcpy(dst + i*stride, c->bundle[BINKB_SRC_COLORS].cur_ptr + i*8, 8);
915
c->bundle[BINKB_SRC_COLORS].cur_ptr += 64;
918
av_log(c->avctx, AV_LOG_ERROR, "Unknown block type %d\n", blk);
923
if (get_bits_count(gb) & 0x1F) //next plane data starts at 32-bit boundary
924
skip_bits_long(gb, 32 - (get_bits_count(gb) & 0x1F));
675
929
static int bink_decode_plane(BinkContext *c, GetBitContext *gb, int plane_idx,
932
1198
*data_size = sizeof(AVFrame);
933
1199
*(AVFrame*)data = c->pic;
935
FFSWAP(AVFrame, c->pic, c->last);
1201
if (c->version > 'b')
1202
FFSWAP(AVFrame, c->pic, c->last);
937
1204
/* always report that the buffer was completely consumed */
938
1205
return pkt->size;
1209
* Caclulate quantization tables for version b
1211
static av_cold void binkb_calc_quant()
1213
uint8_t inv_bink_scan[64];
1217
for (j = 0; j < 8; j++) {
1218
for (i = 0; i < 8; i++) {
1221
s[j*8 + i] = cos(j * M_PI/16.0) * cos(i * M_PI/16.0) * 2.0;
1223
s[j*8 + i] = cos(j * M_PI/16.0) * sqrt(2.0);
1226
s[j*8 + i] = cos(i * M_PI/16.0) * sqrt(2.0);
1232
for (i = 0; i < 64; i++)
1233
inv_bink_scan[bink_scan[i]] = i;
1235
for (j = 0; j < 16; j++) {
1236
for (i = 0; i < 64; i++) {
1237
int k = inv_bink_scan[i];
1239
binkb_intra_quant[j][k] = (1L << 12) * binkb_intra_seed[i] *
1240
binkb_num[j]/binkb_den[j];
1241
binkb_inter_quant[j][k] = (1L << 12) * binkb_inter_seed[i] *
1242
binkb_num[j]/binkb_den[j];
1244
binkb_intra_quant[j][k] = (1L << 12) * binkb_intra_seed[i] * s[i] *
1245
binkb_num[j]/(double)binkb_den[j];
1246
binkb_inter_quant[j][k] = (1L << 12) * binkb_inter_seed[i] * s[i] *
1247
binkb_num[j]/(double)binkb_den[j];
941
1253
static av_cold int decode_init(AVCodecContext *avctx)
943
1255
BinkContext * const c = avctx->priv_data;
944
1256
static VLC_TYPE table[16 * 128][2];
1257
static int binkb_initialised = 0;
948
1261
c->version = avctx->codec_tag >> 24;
949
if (c->version < 'c') {
950
av_log(avctx, AV_LOG_ERROR, "Too old version '%c'\n", c->version);
953
1262
if (avctx->extradata_size < 4) {
954
1263
av_log(avctx, AV_LOG_ERROR, "Extradata missing or too short\n");