FFmpeg
apv_entropy.c
Go to the documentation of this file.
1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * FFmpeg is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with FFmpeg; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #include "apv.h"
20 #include "apv_decode.h"
21 
22 
24 {
25  const int code_len = APV_VLC_LUT_BITS;
26  const int lut_size = APV_VLC_LUT_SIZE;
27 
28  for (int k = 0; k <= 5; k++) {
29  for (unsigned int code = 0; code < lut_size; code++) {
30  APVVLCLUTEntry *ent = &decode_lut->lut[k][code];
31  unsigned int first_bit = code & (1 << code_len - 1);
32  unsigned int remaining_bits = code ^ first_bit;
33 
34  if (first_bit) {
35  ent->consume = 1 + k;
36  ent->result = remaining_bits >> (code_len - k - 1);
37  ent->more = 0;
38  } else {
39  unsigned int second_bit = code & (1 << code_len - 2);
40  remaining_bits ^= second_bit;
41 
42  if (second_bit) {
43  unsigned int bits_left = code_len - 2;
44  unsigned int first_set = bits_left - av_log2(remaining_bits);
45  unsigned int last_bits = first_set - 1 + k;
46 
47  if (first_set + last_bits <= bits_left) {
48  // Whole code fits here.
49  ent->consume = 2 + first_set + last_bits;
50  ent->result = ((2 << k) +
51  (((1 << first_set - 1) - 1) << k) +
52  ((code >> bits_left - first_set - last_bits) & (1 << last_bits) - 1));
53  ent->more = 0;
54  } else {
55  // Need to read more, collapse to default.
56  ent->consume = 2;
57  ent->more = 1;
58  }
59  } else {
60  ent->consume = 2 + k;
61  ent->result = (1 << k) + (remaining_bits >> (code_len - k - 2));
62  ent->more = 0;
63  }
64  }
65  }
66  }
67 }
68 
70 static unsigned int apv_read_vlc(GetBitContext *gbc, int k_param,
71  const APVVLCLUT *lut)
72 {
73  unsigned int next_bits;
74  const APVVLCLUTEntry *ent;
75 
76  next_bits = show_bits(gbc, APV_VLC_LUT_BITS);
77  ent = &lut->lut[k_param][next_bits];
78 
79  if (ent->more) {
80  unsigned int leading_zeroes;
81 
82  skip_bits(gbc, ent->consume);
83 
84  next_bits = show_bits(gbc, 16);
85  leading_zeroes = 15 - av_log2(next_bits);
86 
87  skip_bits(gbc, leading_zeroes + 1);
88 
89  return (2 << k_param) +
90  ((1 << leading_zeroes) - 1) * (1 << k_param) +
91  get_bits(gbc, leading_zeroes + k_param);
92  } else {
93  skip_bits(gbc, ent->consume);
94  return ent->result;
95  }
96 }
97 
98 unsigned int ff_apv_read_vlc(GetBitContext *gbc, int k_param,
99  const APVVLCLUT *lut)
100 {
101  return apv_read_vlc(gbc, k_param, lut);
102 }
103 
105  GetBitContext *gbc,
107 {
108  const APVVLCLUT *lut = state->decode_lut;
109  int k_param;
110 
111  // DC coefficient.
112  {
113  int abs_dc_coeff_diff;
114  int sign_dc_coeff_diff;
115  int dc_coeff;
116 
117  k_param = av_clip(state->prev_dc_diff >> 1, 0, 5);
118  abs_dc_coeff_diff = apv_read_vlc(gbc, k_param, lut);
119 
120  if (abs_dc_coeff_diff > 0)
121  sign_dc_coeff_diff = get_bits1(gbc);
122  else
123  sign_dc_coeff_diff = 0;
124 
125  if (sign_dc_coeff_diff)
126  dc_coeff = state->prev_dc - abs_dc_coeff_diff;
127  else
128  dc_coeff = state->prev_dc + abs_dc_coeff_diff;
129 
130  if (dc_coeff < APV_MIN_TRANS_COEFF ||
131  dc_coeff > APV_MAX_TRANS_COEFF) {
132  av_log(state->log_ctx, AV_LOG_ERROR,
133  "Out-of-range DC coefficient value: %d "
134  "(from prev_dc %d abs_dc_coeff_diff %d sign_dc_coeff_diff %d)\n",
135  dc_coeff, state->prev_dc, abs_dc_coeff_diff, sign_dc_coeff_diff);
136  return AVERROR_INVALIDDATA;
137  }
138 
139  coeff[0] = dc_coeff;
140 
141  state->prev_dc = dc_coeff;
142  state->prev_dc_diff = abs_dc_coeff_diff;
143  }
144 
145  // AC coefficients.
146  {
147  int scan_pos = 1;
148  int first_ac = 1;
149  int prev_level = state->prev_1st_ac_level;
150  int prev_run = 0;
151 
152  do {
153  int coeff_zero_run;
154 
155  k_param = av_clip(prev_run >> 2, 0, 2);
156  coeff_zero_run = apv_read_vlc(gbc, k_param, lut);
157 
158  if (coeff_zero_run > APV_BLK_COEFFS - scan_pos) {
159  av_log(state->log_ctx, AV_LOG_ERROR,
160  "Out-of-range zero-run value: %d (at scan pos %d)\n",
161  coeff_zero_run, scan_pos);
162  return AVERROR_INVALIDDATA;
163  }
164 
165  for (int i = 0; i < coeff_zero_run; i++) {
166  coeff[ff_zigzag_direct[scan_pos]] = 0;
167  ++scan_pos;
168  }
169  prev_run = coeff_zero_run;
170 
171  if (scan_pos < APV_BLK_COEFFS) {
172  int abs_ac_coeff_minus1;
173  int sign_ac_coeff;
174  int level;
175 
176  k_param = av_clip(prev_level >> 2, 0, 4);
177  abs_ac_coeff_minus1 = apv_read_vlc(gbc, k_param, lut);
178  sign_ac_coeff = get_bits(gbc, 1);
179 
180  if (sign_ac_coeff)
181  level = -abs_ac_coeff_minus1 - 1;
182  else
183  level = abs_ac_coeff_minus1 + 1;
184 
185  coeff[ff_zigzag_direct[scan_pos]] = level;
186 
187  prev_level = abs_ac_coeff_minus1 + 1;
188  if (first_ac) {
189  state->prev_1st_ac_level = prev_level;
190  first_ac = 0;
191  }
192 
193  ++scan_pos;
194  }
195 
196  } while (scan_pos < APV_BLK_COEFFS);
197  }
198 
199  return 0;
200 }
APV_BLK_COEFFS
@ APV_BLK_COEFFS
Definition: apv.h:55
level
uint8_t level
Definition: svq3.c:205
av_clip
#define av_clip
Definition: common.h:100
apv_decode.h
state
static struct @501 state
APVVLCLUTEntry
Definition: apv_decode.h:36
skip_bits
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:364
get_bits
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:318
APVVLCLUT::lut
APVVLCLUTEntry lut[6][APV_VLC_LUT_SIZE]
Definition: apv_decode.h:43
GetBitContext
Definition: get_bits.h:108
APVVLCLUTEntry::consume
uint8_t consume
Definition: apv_decode.h:38
remaining_bits
static int remaining_bits(WmallDecodeCtx *s, GetBitContext *gb)
Calculate remaining input buffer length.
Definition: wmalosslessdec.c:1128
apv_read_vlc
static av_always_inline unsigned int apv_read_vlc(GetBitContext *gbc, int k_param, const APVVLCLUT *lut)
Definition: apv_entropy.c:70
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:210
ff_apv_read_vlc
unsigned int ff_apv_read_vlc(GetBitContext *gbc, int k_param, const APVVLCLUT *lut)
Read a single APV VLC code.
Definition: apv_entropy.c:98
APV_VLC_LUT_SIZE
#define APV_VLC_LUT_SIZE
Definition: apv_decode.h:34
ff_apv_entropy_build_decode_lut
void ff_apv_entropy_build_decode_lut(APVVLCLUT *decode_lut)
Build the decoder VLC look-up table.
Definition: apv_entropy.c:23
bits_left
#define bits_left
Definition: bitstream.h:114
APV_MAX_TRANS_COEFF
@ APV_MAX_TRANS_COEFF
Definition: apv.h:57
APVVLCLUTEntry::result
uint16_t result
Definition: apv_decode.h:37
get_bits1
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:371
ff_apv_entropy_decode_block
int ff_apv_entropy_decode_block(int16_t *coeff, GetBitContext *gbc, APVEntropyState *state)
Entropy decode a single 8x8 block to coefficients.
Definition: apv_entropy.c:104
APV_VLC_LUT_BITS
#define APV_VLC_LUT_BITS
Definition: apv_decode.h:33
APVEntropyState
Definition: apv_decode.h:46
apv.h
APVVLCLUTEntry::more
uint8_t more
Definition: apv_decode.h:39
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
code
and forward the test the status of outputs and forward it to the corresponding return FFERROR_NOT_READY If the filters stores internally one or a few frame for some it can consider them to be part of the FIFO and delay acknowledging a status change accordingly Example code
Definition: filter_design.txt:178
show_bits
static unsigned int show_bits(GetBitContext *s, int n)
Show 1-25 bits.
Definition: get_bits.h:354
av_always_inline
#define av_always_inline
Definition: attributes.h:49
ff_zigzag_direct
const uint8_t ff_zigzag_direct[64]
Definition: mathtables.c:98
APV_MIN_TRANS_COEFF
@ APV_MIN_TRANS_COEFF
Definition: apv.h:56
APVVLCLUT
Definition: apv_decode.h:42
coeff
static const double coeff[2][5]
Definition: vf_owdenoise.c:80
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
av_log2
int av_log2(unsigned v)
Definition: intmath.c:26