static MacroBlock decode_macroblock(Escape124Context* s, GetBitContext* gb, int* codebook_index, int superblock_index) { // This function reads a maximum of 22 bits; the callers // guard this function appropriately unsigned block_index, depth; int value = get_bits1(gb); if (value) { static const char transitions[3][2] = { {2, 1}, {0, 2}, {1, 0} }; value = get_bits1(gb); *codebook_index = transitions[*codebook_index][value]; } depth = s->codebooks[*codebook_index].depth; // depth = 0 means that this shouldn't read any bits; // in theory, this is the same as get_bits(gb, 0), but // that doesn't actually work. block_index = get_bitsz(gb, depth); if (*codebook_index == 1) { block_index += superblock_index << s->codebooks[1].depth; } // This condition can occur with invalid bitstreams and // *codebook_index == 2 if (block_index >= s->codebooks[*codebook_index].size) return (MacroBlock) { { 0 } }; return s->codebooks[*codebook_index].blocks[block_index]; }
static av_always_inline int get_tail(GetBitContext *gb, int k) { int p, e, res; if (k < 1) return 0; p = av_log2(k); e = (1 << (p + 1)) - k - 1; res = get_bitsz(gb, p); if (res >= e) res = (res << 1) - e + get_bits1(gb); return res; }
static int build_huffman(AVCodecContext *avctx, GetBitContext *gbit, int max) { MagicYUVContext *s = avctx->priv_data; int i = 0, j = 0, k; memset(s->len, 0, sizeof(s->len)); while (get_bits_left(gbit) >= 8) { int b = get_bits(gbit, 1); int x = get_bits(gbit, 7); int l = get_bitsz(gbit, b * 8) + 1; for (k = 0; k < l; k++) if (j + k < max) s->len[i][j + k] = x; j += l; if (j == max) { j = 0; if (s->huff_build(&s->vlc[i], s->len[i])) { av_log(avctx, AV_LOG_ERROR, "Cannot build Huffman codes\n"); return AVERROR_INVALIDDATA; } i++; if (i == s->planes) { break; } } else if (j > max) { av_log(avctx, AV_LOG_ERROR, "Invalid Huffman codes\n"); return AVERROR_INVALIDDATA; } } if (i != s->planes) { av_log(avctx, AV_LOG_ERROR, "Huffman tables too short\n"); return AVERROR_INVALIDDATA; } return 0; }