summaryrefslogtreecommitdiff
path: root/vp9/decoder/vp9_decodframe.c
diff options
context:
space:
mode:
Diffstat (limited to 'vp9/decoder/vp9_decodframe.c')
-rw-r--r--vp9/decoder/vp9_decodframe.c147
1 files changed, 100 insertions, 47 deletions
diff --git a/vp9/decoder/vp9_decodframe.c b/vp9/decoder/vp9_decodframe.c
index 20d4f19c0..4af921872 100644
--- a/vp9/decoder/vp9_decodframe.c
+++ b/vp9/decoder/vp9_decodframe.c
@@ -65,6 +65,69 @@ static TXFM_MODE read_txfm_mode(vp9_reader *r) {
return mode;
}
+static int get_unsigned_bits(unsigned int num_values) {
+ int cat = 0;
+ if (num_values <= 1)
+ return 0;
+ num_values--;
+ while (num_values > 0) {
+ cat++;
+ num_values >>= 1;
+ }
+ return cat;
+}
+
+static int inv_recenter_nonneg(int v, int m) {
+ if (v > (m << 1))
+ return v;
+ else if ((v & 1) == 0)
+ return (v >> 1) + m;
+ else
+ return m - ((v + 1) >> 1);
+}
+
+static int decode_uniform(BOOL_DECODER *br, int n) {
+ int v;
+ const int l = get_unsigned_bits(n);
+ const int m = (1 << l) - n;
+ if (!l)
+ return 0;
+
+ v = vp9_read_literal(br, l - 1);
+ return v < m ? v : (v << 1) - m + vp9_read_bit(br);
+}
+
+static int decode_term_subexp(BOOL_DECODER *br, int k, int num_syms) {
+ int i = 0, mk = 0, word;
+ while (1) {
+ const int b = i ? k + i - 1 : k;
+ const int a = 1 << b;
+ if (num_syms <= mk + 3 * a) {
+ word = decode_uniform(br, num_syms - mk) + mk;
+ break;
+ } else {
+ if (vp9_read_bit(br)) {
+ i++;
+ mk += a;
+ } else {
+ word = vp9_read_literal(br, b) + mk;
+ break;
+ }
+ }
+ }
+ return word;
+}
+
+static int decode_unsigned_max(BOOL_DECODER *br, int max) {
+ int data = 0, bit = 0, lmax = max;
+
+ while (lmax) {
+ data |= vp9_read_bit(br) << bit++;
+ lmax >>= 1;
+ }
+ return data > max ? max : data;
+}
+
static int merge_index(int v, int n, int modulus) {
int max1 = (n - 1 - modulus / 2) / modulus + 1;
if (v < max1) v = v * modulus + modulus / 2;
@@ -85,14 +148,14 @@ static int inv_remap_prob(int v, int m) {
v = merge_index(v, n - 1, modulus);
if ((m << 1) <= n) {
- return vp9_inv_recenter_nonneg(v + 1, m);
+ return inv_recenter_nonneg(v + 1, m);
} else {
- return n - 1 - vp9_inv_recenter_nonneg(v + 1, n - 1 - m);
+ return n - 1 - inv_recenter_nonneg(v + 1, n - 1 - m);
}
}
static vp9_prob read_prob_diff_update(vp9_reader *const bc, int oldp) {
- int delp = vp9_decode_term_subexp(bc, SUBEXP_PARAM, 255);
+ int delp = decode_term_subexp(bc, SUBEXP_PARAM, 255);
return (vp9_prob)inv_remap_prob(delp, oldp);
}
@@ -102,15 +165,15 @@ void vp9_init_de_quantizer(VP9D_COMP *pbi) {
VP9_COMMON *const pc = &pbi->common;
for (q = 0; q < QINDEX_RANGE; q++) {
- pc->y_dequant[q][0] = (int16_t)vp9_dc_quant(q, pc->y1dc_delta_q);
- pc->uv_dequant[q][0] = (int16_t)vp9_dc_uv_quant(q, pc->uvdc_delta_q);
+ pc->y_dequant[q][0] = (int16_t)vp9_dc_quant(q, pc->y_dc_delta_q);
+ pc->uv_dequant[q][0] = (int16_t)vp9_dc_uv_quant(q, pc->uv_dc_delta_q);
/* all the ac values =; */
for (i = 1; i < 16; i++) {
const int rc = vp9_default_zig_zag1d_4x4[i];
pc->y_dequant[q][rc] = (int16_t)vp9_ac_yquant(q);
- pc->uv_dequant[q][rc] = (int16_t)vp9_ac_uv_quant(q, pc->uvac_delta_q);
+ pc->uv_dequant[q][rc] = (int16_t)vp9_ac_uv_quant(q, pc->uv_ac_delta_q);
}
}
}
@@ -868,16 +931,18 @@ static void decode_modes_sb(VP9D_COMP *pbi, int mb_row, int mb_col,
}
/* Decode a row of Superblocks (4x4 region of MBs) */
-static void decode_sb_row(VP9D_COMP *pbi, int mb_row, vp9_reader* r) {
+static void decode_tile(VP9D_COMP *pbi, vp9_reader* r) {
VP9_COMMON *const pc = &pbi->common;
- int mb_col;
-
- // For a SB there are 2 left contexts, each pertaining to a MB row within
- vpx_memset(pc->left_context, 0, sizeof(pc->left_context));
-
- for (mb_col = pc->cur_tile_mb_col_start;
- mb_col < pc->cur_tile_mb_col_end; mb_col += 4) {
- decode_modes_sb(pbi, mb_row, mb_col, r, BLOCK_SIZE_SB64X64);
+ int mb_row, mb_col;
+
+ for (mb_row = pc->cur_tile_mb_row_start;
+ mb_row < pc->cur_tile_mb_row_end; mb_row += 4) {
+ // For a SB there are 2 left contexts, each pertaining to a MB row within
+ vpx_memset(pc->left_context, 0, sizeof(pc->left_context));
+ for (mb_col = pc->cur_tile_mb_col_start;
+ mb_col < pc->cur_tile_mb_col_end; mb_col += 4) {
+ decode_modes_sb(pbi, mb_row, mb_col, r, BLOCK_SIZE_SB64X64);
+ }
}
}
@@ -1132,7 +1197,7 @@ static void setup_segmentation(VP9_COMMON *pc, MACROBLOCKD *xd, vp9_reader *r) {
const int feature_enabled = vp9_read_bit(r);
if (feature_enabled) {
vp9_enable_segfeature(xd, i, j);
- data = vp9_decode_unsigned_max(r, vp9_seg_feature_data_max(j));
+ data = decode_unsigned_max(r, vp9_seg_feature_data_max(j));
if (vp9_is_segfeature_signed(j))
data = vp9_read_and_apply_sign(r, data);
}
@@ -1148,9 +1213,9 @@ static void setup_pred_probs(VP9_COMMON *pc, vp9_reader *r) {
// reference frame
if (pc->frame_type == KEY_FRAME) {
// Set the prediction probabilities to defaults
- pc->ref_pred_probs[0] = 120;
- pc->ref_pred_probs[1] = 80;
- pc->ref_pred_probs[2] = 40;
+ pc->ref_pred_probs[0] = DEFAULT_PRED_PROB_0;
+ pc->ref_pred_probs[1] = DEFAULT_PRED_PROB_1;
+ pc->ref_pred_probs[2] = DEFAULT_PRED_PROB_2;
} else {
int i;
for (i = 0; i < PREDICTION_PROBS; ++i)
@@ -1203,9 +1268,9 @@ static void setup_quantization(VP9D_COMP *pbi, vp9_reader *r) {
VP9_COMMON *const pc = &pbi->common;
pc->base_qindex = vp9_read_literal(r, QINDEX_BITS);
- if (get_delta_q(r, &pc->y1dc_delta_q) |
- get_delta_q(r, &pc->uvdc_delta_q) |
- get_delta_q(r, &pc->uvac_delta_q))
+ if (get_delta_q(r, &pc->y_dc_delta_q) |
+ get_delta_q(r, &pc->uv_dc_delta_q) |
+ get_delta_q(r, &pc->uv_ac_delta_q))
vp9_init_de_quantizer(pbi);
mb_init_dequantizer(pbi, &pbi->mb); // MB level dequantizer setup
@@ -1215,8 +1280,17 @@ static const uint8_t *read_frame_size(VP9_COMMON *const pc, const uint8_t *data,
const uint8_t *data_end,
int *width, int *height) {
if (data + 4 < data_end) {
- *width = read_le16(data);
- *height = read_le16(data + 2);
+ const int w = read_le16(data);
+ const int h = read_le16(data + 2);
+ if (w <= 0)
+ vpx_internal_error(&pc->error, VPX_CODEC_CORRUPT_FRAME,
+ "Invalid frame width");
+
+ if (h <= 0)
+ vpx_internal_error(&pc->error, VPX_CODEC_CORRUPT_FRAME,
+ "Invalid frame height");
+ *width = w;
+ *height = h;
data += 4;
} else {
vpx_internal_error(&pc->error, VPX_CODEC_CORRUPT_FRAME,
@@ -1242,14 +1316,6 @@ static const uint8_t *setup_frame_size(VP9D_COMP *pbi, int scaling_active,
data = read_frame_size(pc, data, data_end, &width, &height);
if (pc->width != width || pc->height != height) {
- if (width <= 0)
- vpx_internal_error(&pc->error, VPX_CODEC_CORRUPT_FRAME,
- "Invalid frame width");
-
- if (height <= 0)
- vpx_internal_error(&pc->error, VPX_CODEC_CORRUPT_FRAME,
- "Invalid frame height");
-
if (!pbi->initial_width || !pbi->initial_height) {
if (vp9_alloc_frame_buffers(pc, width, height))
vpx_internal_error(&pc->error, VPX_CODEC_MEM_ERROR,
@@ -1342,7 +1408,6 @@ static void decode_tiles(VP9D_COMP *pbi,
const uint8_t *data_ptr = data + first_partition_size;
int tile_row, tile_col, delta_log2_tiles;
- int mb_row;
vp9_get_tile_n_bits(pc, &pc->log2_tile_columns, &delta_log2_tiles);
while (delta_log2_tiles--) {
@@ -1388,13 +1453,7 @@ static void decode_tiles(VP9D_COMP *pbi,
for (tile_col = n_cols - 1; tile_col >= 0; tile_col--) {
vp9_get_tile_col_offsets(pc, tile_col);
setup_token_decoder(pbi, data_ptr2[tile_row][tile_col], residual_bc);
-
- // Decode a row of superblocks
- for (mb_row = pc->cur_tile_mb_row_start;
- mb_row < pc->cur_tile_mb_row_end; mb_row += 4) {
- decode_sb_row(pbi, mb_row, residual_bc);
- }
-
+ decode_tile(pbi, residual_bc);
if (tile_row == pc->tile_rows - 1 && tile_col == n_cols - 1)
bc_bak = *residual_bc;
}
@@ -1411,14 +1470,8 @@ static void decode_tiles(VP9D_COMP *pbi,
has_more = tile_col < pc->tile_columns - 1 ||
tile_row < pc->tile_rows - 1;
- // Setup decoder
setup_token_decoder(pbi, data_ptr + (has_more ? 4 : 0), residual_bc);
-
- // Decode a row of superblocks
- for (mb_row = pc->cur_tile_mb_row_start;
- mb_row < pc->cur_tile_mb_row_end; mb_row += 4) {
- decode_sb_row(pbi, mb_row, residual_bc);
- }
+ decode_tile(pbi, residual_bc);
if (has_more) {
const int size = read_le32(data_ptr);