static float get_float(BitstreamContext *bc) { int power = bitstream_read(bc, 5); float f = ldexpf(bitstream_read(bc, 23), power - 23); if (bitstream_read_bit(bc)) f = -f; return f; }
enum huffman_error huffman_import_tree_rle(struct huffman_decoder* decoder, struct bitstream* bitbuf) { enum huffman_error error; /* bits per entry depends on the maxbits */ int numbits; int curnode; if (decoder->maxbits >= 16) numbits = 5; else if (decoder->maxbits >= 8) numbits = 4; else numbits = 3; /* loop until we read all the nodes */ for (curnode = 0; curnode < decoder->numcodes; ) { /* a non-one value is just raw */ int nodebits = bitstream_read(bitbuf, numbits); if (nodebits != 1) decoder->huffnode[curnode++].numbits = nodebits; /* a one value is an escape code */ else { /* a double 1 is just a single 1 */ nodebits = bitstream_read(bitbuf, numbits); if (nodebits == 1) decoder->huffnode[curnode++].numbits = nodebits; /* otherwise, we need one for value for the repeat count */ else { int repcount = bitstream_read(bitbuf, numbits) + 3; while (repcount--) decoder->huffnode[curnode++].numbits = nodebits; } } } /* make sure we ended up with the right number */ if (curnode != decoder->numcodes) return HUFFERR_INVALID_DATA; /* assign canonical codes for all nodes based on their code lengths */ error = huffman_assign_canonical_codes(decoder); if (error != HUFFERR_NONE) return error; /* build the lookup table */ huffman_build_lookup_table(decoder); /* determine final input length and report errors */ return bitstream_overflow(bitbuf) ? HUFFERR_INPUT_BUFFER_TOO_SMALL : HUFFERR_NONE; }
static void do_output_subblock(RA144Context *ractx, const uint16_t *lpc_coefs, int gval, BitstreamContext *bc) { int cba_idx = bitstream_read(bc, 7); // index of the adaptive CB, 0 if none int gain = bitstream_read(bc, 8); int cb1_idx = bitstream_read(bc, 7); int cb2_idx = bitstream_read(bc, 7); ff_subblock_synthesis(ractx, lpc_coefs, cba_idx, cb1_idx, cb2_idx, gval, gain); }
ssize_t th_unlzss( thtk_io_t* input, thtk_io_t* output, size_t output_size, thtk_error_t** error) { unsigned char dict[LZSS_DICTSIZE]; unsigned int dict_head = 1; unsigned int i; size_t bytes_written = 0; struct bitstream bs; if (!input || !output) { thtk_error_new(error, "input or output is NULL"); return -1; } bitstream_init(&bs, input); memset(dict, 0, sizeof(dict)); while (bytes_written < output_size) { if (bitstream_read1(&bs)) { unsigned char c = bitstream_read(&bs, 8); if (thtk_io_write(output, &c, 1, error) != 1) return -1; ++bytes_written; dict[dict_head] = c; dict_head = (dict_head + 1) & LZSS_DICTSIZE_MASK; } else { unsigned int match_offset = bitstream_read(&bs, 13); unsigned int match_len = bitstream_read(&bs, 4) + LZSS_MIN_MATCH; if (!match_offset) return bytes_written; for (i = 0; i < match_len; ++i) { unsigned char c = dict[(match_offset + i) & LZSS_DICTSIZE_MASK]; if (thtk_io_write(output, &c, 1, error) != 1) return -1; ++bytes_written; dict[dict_head] = c; dict_head = (dict_head + 1) & LZSS_DICTSIZE_MASK; } } } return bytes_written; }
int ff_cbs_read_unsigned(CodedBitstreamContext *ctx, BitstreamContext *bc, int width, const char *name, uint32_t *write_to, uint32_t range_min, uint32_t range_max) { uint32_t value; int position; av_assert0(width <= 32); if (bitstream_bits_left(bc) < width) { av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid value at " "%s: bitstream ended.\n", name); return AVERROR_INVALIDDATA; } if (ctx->trace_enable) position = bitstream_tell(bc); value = bitstream_read(bc, width); if (ctx->trace_enable) { char bits[33]; int i; for (i = 0; i < width; i++) bits[i] = value >> (width - i - 1) & 1 ? '1' : '0'; bits[i] = 0; ff_cbs_trace_syntax_element(ctx, position, name, bits, value); }
int lzw_decompress(lzw_t *lzw, read_func_t src_r, write_func_t dst_w, void *ctx) { bitstream_t bs_r; bitstream_init_r(&bs_r, src_r, ctx); uint8_t init_code_len; src_r(ctx, &init_code_len, 1); if (init_code_len>=32) return 1; lzw->code_len = init_code_len; lzw_resize_dict(lzw); int prev = -1; int done = 0; while (1) { int code = 0; for (int i = 0; i < lzw->code_len; i++) { int bit = bitstream_read(&bs_r); if (bit==-1) { done = 1; break; } code |= bit << i; } if (done) break; if (code==256) { lzw->code_len = init_code_len; lzw_resize_dict(lzw); } // put dict[prev]+'c' into the dictionary if (prev>=0) { lzw_dict_entry_t *di = &lzw->dict[lzw->dict_i]; di->size = lzw->dict[prev].size+1; di->data = malloc(di->size); uint8_t chr = lzw->dict[code==lzw->dict_i ? prev : code].data[0]; memcpy(di->data, lzw->dict[prev].data, lzw->dict[prev].size); di->data[di->size-1] = chr; lzw->dict_i++; if (lzw->dict_i==(1 << lzw->code_len)) { lzw->code_len++; lzw_resize_dict(lzw); } } if (code >= lzw->dict_size) return 1; // no entry for this code dst_w(ctx, lzw->dict[code].data, lzw->dict[code].size); prev = code; } return 0; }
static int parse_fmtp_config(AVStream *st, const char *value) { int len = ff_hex_to_data(NULL, value), i, ret = 0; BitstreamContext bc; uint8_t *config; int audio_mux_version, same_time_framing, num_programs, num_layers; /* Pad this buffer, too, to avoid out of bounds reads with get_bits below */ config = av_mallocz(len + AV_INPUT_BUFFER_PADDING_SIZE); if (!config) return AVERROR(ENOMEM); ff_hex_to_data(config, value); bitstream_init8(&bc, config, len); audio_mux_version = bitstream_read(&bc, 1); same_time_framing = bitstream_read(&bc, 1); bitstream_skip(&bc, 6); /* num_sub_frames */ num_programs = bitstream_read(&bc, 4); num_layers = bitstream_read(&bc, 3); if (audio_mux_version != 0 || same_time_framing != 1 || num_programs != 0 || num_layers != 0) { avpriv_report_missing_feature(NULL, "LATM config (%d,%d,%d,%d)", audio_mux_version, same_time_framing, num_programs, num_layers); ret = AVERROR_PATCHWELCOME; goto end; } av_freep(&st->codecpar->extradata); st->codecpar->extradata_size = (bitstream_bits_left(&bc) + 7) / 8; st->codecpar->extradata = av_mallocz(st->codecpar->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE); if (!st->codecpar->extradata) { ret = AVERROR(ENOMEM); goto end; } for (i = 0; i < st->codecpar->extradata_size; i++) st->codecpar->extradata[i] = bitstream_read(&bc, 8); end: av_free(config); return ret; }
static int decode_skip_count(BitstreamContext *bc) { int value; value = bitstream_read_bit(bc); if (value) return 0; value = bitstream_read(bc, 3); if (value) return value; value = bitstream_read(bc, 8); if (value) return value + 7; value = bitstream_read(bc, 15); if (value) return value + 262; return -1; }
static int gsm_decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt) { AVFrame *frame = data; int res; BitstreamContext bc; const uint8_t *buf = avpkt->data; int buf_size = avpkt->size; int16_t *samples; if (buf_size < avctx->block_align) { av_log(avctx, AV_LOG_ERROR, "Packet is too small\n"); return AVERROR_INVALIDDATA; } /* get output buffer */ frame->nb_samples = avctx->frame_size; if ((res = ff_get_buffer(avctx, frame, 0)) < 0) { av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n"); return res; } samples = (int16_t *)frame->data[0]; switch (avctx->codec_id) { case AV_CODEC_ID_GSM: bitstream_init(&bc, buf, buf_size * 8); if (bitstream_read(&bc, 4) != 0xd) av_log(avctx, AV_LOG_WARNING, "Missing GSM magic!\n"); res = gsm_decode_block(avctx, samples, &bc, GSM_13000); if (res < 0) return res; break; case AV_CODEC_ID_GSM_MS: res = ff_msgsm_decode_block(avctx, samples, buf, (GSM_MS_BLOCK_SIZE - avctx->block_align) / 3); if (res < 0) return res; } *got_frame_ptr = 1; return avctx->block_align; }
/** * Decode Bink Audio block * @param[out] out Output buffer (must contain s->block_size elements) * @return 0 on success, negative error code on failure */ static int decode_block(BinkAudioContext *s, float **out, int use_dct) { int ch, i, j, k; float q, quant[25]; int width, coeff; BitstreamContext *bc = &s->bc; if (use_dct) bitstream_skip(bc, 2); for (ch = 0; ch < s->channels; ch++) { FFTSample *coeffs = out[ch]; if (s->version_b) { if (bitstream_bits_left(bc) < 64) return AVERROR_INVALIDDATA; coeffs[0] = av_int2float(bitstream_read(bc, 32)) * s->root; coeffs[1] = av_int2float(bitstream_read(bc, 32)) * s->root; } else { if (bitstream_bits_left(bc) < 58) return AVERROR_INVALIDDATA; coeffs[0] = get_float(bc) * s->root; coeffs[1] = get_float(bc) * s->root; } if (bitstream_bits_left(bc) < s->num_bands * 8) return AVERROR_INVALIDDATA; for (i = 0; i < s->num_bands; i++) { int value = bitstream_read(bc, 8); quant[i] = quant_table[FFMIN(value, 95)]; } k = 0; q = quant[0]; // parse coefficients i = 2; while (i < s->frame_len) { if (s->version_b) { j = i + 16; } else { int v = bitstream_read_bit(bc); if (v) { v = bitstream_read(bc, 4); j = i + rle_length_tab[v] * 8; } else { j = i + 8; } } j = FFMIN(j, s->frame_len); width = bitstream_read(bc, 4); if (width == 0) { memset(coeffs + i, 0, (j - i) * sizeof(*coeffs)); i = j; while (s->bands[k] < i) q = quant[k++]; } else { while (i < j) { if (s->bands[k] == i) q = quant[k++]; coeff = bitstream_read(bc, width); if (coeff) { int v; v = bitstream_read_bit(bc); if (v) coeffs[i] = -q * coeff; else coeffs[i] = q * coeff; } else { coeffs[i] = 0.0f; } i++; } } } if (CONFIG_BINKAUDIO_DCT_DECODER && use_dct) { coeffs[0] /= 0.5; s->trans.dct.dct_calc(&s->trans.dct, coeffs); } else if (CONFIG_BINKAUDIO_RDFT_DECODER) s->trans.rdft.rdft_calc(&s->trans.rdft, coeffs); } for (ch = 0; ch < s->channels; ch++) { int j; int count = s->overlap_len * s->channels; if (!s->first) { j = ch; for (i = 0; i < s->overlap_len; i++, j += s->channels) out[ch][i] = (s->previous[ch][i] * (count - j) + out[ch][i] * j) / count; } memcpy(s->previous[ch], &out[ch][s->frame_len - s->overlap_len], s->overlap_len * sizeof(*s->previous[ch])); } s->first = 0; return 0; }
static int escape130_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt) { const uint8_t *buf = avpkt->data; int buf_size = avpkt->size; Escape130Context *s = avctx->priv_data; AVFrame *pic = data; BitstreamContext bc; int ret; uint8_t *old_y, *old_cb, *old_cr, *new_y, *new_cb, *new_cr; uint8_t *dstY, *dstU, *dstV; unsigned old_y_stride, old_cb_stride, old_cr_stride, new_y_stride, new_cb_stride, new_cr_stride; unsigned total_blocks = avctx->width * avctx->height / 4, block_index, block_x = 0; unsigned y[4] = { 0 }, cb = 0x10, cr = 0x10; int skip = -1, y_avg = 0, i, j; uint8_t *ya = s->old_y_avg; // first 16 bytes are header; no useful information in here if (buf_size <= 16) { av_log(avctx, AV_LOG_ERROR, "Insufficient frame data\n"); return AVERROR_INVALIDDATA; } if ((ret = ff_get_buffer(avctx, pic, 0)) < 0) return ret; bitstream_init(&bc, buf + 16, (buf_size - 16) * 8); new_y = s->new_y; new_cb = s->new_u; new_cr = s->new_v; new_y_stride = s->linesize[0]; new_cb_stride = s->linesize[1]; new_cr_stride = s->linesize[2]; old_y = s->old_y; old_cb = s->old_u; old_cr = s->old_v; old_y_stride = s->linesize[0]; old_cb_stride = s->linesize[1]; old_cr_stride = s->linesize[2]; for (block_index = 0; block_index < total_blocks; block_index++) { // Note that this call will make us skip the rest of the blocks // if the frame ends prematurely. if (skip == -1) skip = decode_skip_count(&bc); if (skip == -1) { av_log(avctx, AV_LOG_ERROR, "Error decoding skip value\n"); return AVERROR_INVALIDDATA; } if (skip) { y[0] = old_y[0]; y[1] = old_y[1]; y[2] = old_y[old_y_stride]; y[3] = old_y[old_y_stride + 1]; y_avg = ya[0]; cb = old_cb[0]; cr = old_cr[0]; } else { if (bitstream_read_bit(&bc)) { unsigned sign_selector = bitstream_read(&bc, 6); unsigned difference_selector = bitstream_read(&bc, 2); y_avg = 2 * bitstream_read(&bc, 5); for (i = 0; i < 4; i++) { y[i] = av_clip(y_avg + offset_table[difference_selector] * sign_table[sign_selector][i], 0, 63); } } else if (bitstream_read_bit(&bc)) { if (bitstream_read_bit(&bc)) { y_avg = bitstream_read(&bc, 6); } else { unsigned adjust_index = bitstream_read(&bc, 3); y_avg = (y_avg + luma_adjust[adjust_index]) & 63; } for (i = 0; i < 4; i++) y[i] = y_avg; } if (bitstream_read_bit(&bc)) { if (bitstream_read_bit(&bc)) { cb = bitstream_read(&bc, 5); cr = bitstream_read(&bc, 5); } else { unsigned adjust_index = bitstream_read(&bc, 3); cb = (cb + chroma_adjust[0][adjust_index]) & 31; cr = (cr + chroma_adjust[1][adjust_index]) & 31; } } } *ya++ = y_avg; new_y[0] = y[0]; new_y[1] = y[1]; new_y[new_y_stride] = y[2]; new_y[new_y_stride + 1] = y[3]; *new_cb = cb; *new_cr = cr; old_y += 2; old_cb++; old_cr++; new_y += 2; new_cb++; new_cr++; block_x++; if (block_x * 2 == avctx->width) { block_x = 0; old_y += old_y_stride * 2 - avctx->width; old_cb += old_cb_stride - avctx->width / 2; old_cr += old_cr_stride - avctx->width / 2; new_y += new_y_stride * 2 - avctx->width; new_cb += new_cb_stride - avctx->width / 2; new_cr += new_cr_stride - avctx->width / 2; } skip--; } new_y = s->new_y; new_cb = s->new_u; new_cr = s->new_v; dstY = pic->data[0]; dstU = pic->data[1]; dstV = pic->data[2]; for (j = 0; j < avctx->height; j++) { for (i = 0; i < avctx->width; i++) dstY[i] = new_y[i] << 2; dstY += pic->linesize[0]; new_y += new_y_stride; } for (j = 0; j < avctx->height / 2; j++) { for (i = 0; i < avctx->width / 2; i++) { dstU[i] = chroma_vals[new_cb[i]]; dstV[i] = chroma_vals[new_cr[i]]; } dstU += pic->linesize[1]; dstV += pic->linesize[2]; new_cb += new_cb_stride; new_cr += new_cr_stride; } ff_dlog(avctx, "Frame data: provided %d bytes, used %d bytes\n", buf_size, bitstream_tell(&bc) >> 3); FFSWAP(uint8_t*, s->old_y, s->new_y); FFSWAP(uint8_t*, s->old_u, s->new_u); FFSWAP(uint8_t*, s->old_v, s->new_v); *got_frame = 1; return buf_size; }
/** Uncompress one block (20 bytes -> 160*2 bytes). */ static int ra144_decode_frame(AVCodecContext * avctx, void *data, int *got_frame_ptr, AVPacket *avpkt) { AVFrame *frame = data; const uint8_t *buf = avpkt->data; int buf_size = avpkt->size; static const uint8_t sizes[LPC_ORDER] = {6, 5, 5, 4, 4, 3, 3, 3, 3, 2}; unsigned int refl_rms[NBLOCKS]; // RMS of the reflection coefficients uint16_t block_coefs[NBLOCKS][LPC_ORDER]; // LPC coefficients of each sub-block unsigned int lpc_refl[LPC_ORDER]; // LPC reflection coefficients of the frame int i, j; int ret; int16_t *samples; unsigned int energy; RA144Context *ractx = avctx->priv_data; BitstreamContext bc; if (buf_size < FRAMESIZE) { av_log(avctx, AV_LOG_ERROR, "Frame too small (%d bytes). Truncated file?\n", buf_size); *got_frame_ptr = 0; return AVERROR_INVALIDDATA; } /* get output buffer */ frame->nb_samples = NBLOCKS * BLOCKSIZE; if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) { av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n"); return ret; } samples = (int16_t *)frame->data[0]; bitstream_init8(&bc, buf, FRAMESIZE); for (i = 0; i < LPC_ORDER; i++) lpc_refl[i] = ff_lpc_refl_cb[i][bitstream_read(&bc, sizes[i])]; ff_eval_coefs(ractx->lpc_coef[0], lpc_refl); ractx->lpc_refl_rms[0] = ff_rms(lpc_refl); energy = ff_energy_tab[bitstream_read(&bc, 5)]; refl_rms[0] = ff_interp(ractx, block_coefs[0], 1, 1, ractx->old_energy); refl_rms[1] = ff_interp(ractx, block_coefs[1], 2, energy <= ractx->old_energy, ff_t_sqrt(energy*ractx->old_energy) >> 12); refl_rms[2] = ff_interp(ractx, block_coefs[2], 3, 0, energy); refl_rms[3] = ff_rescale_rms(ractx->lpc_refl_rms[0], energy); ff_int_to_int16(block_coefs[3], ractx->lpc_coef[0]); for (i=0; i < NBLOCKS; i++) { do_output_subblock(ractx, block_coefs[i], refl_rms[i], &bc); for (j=0; j < BLOCKSIZE; j++) *samples++ = av_clip_int16(ractx->curr_sblock[j + 10] << 2); } ractx->old_energy = energy; ractx->lpc_refl_rms[1] = ractx->lpc_refl_rms[0]; FFSWAP(unsigned int *, ractx->lpc_coef[0], ractx->lpc_coef[1]); *got_frame_ptr = 1; return FRAMESIZE; }
enum huffman_error huffman_import_tree_huffman(struct huffman_decoder* decoder, struct bitstream* bitbuf) { int last = 0; int curcode; uint32_t temp; enum huffman_error error; uint8_t rlefullbits = 0; int index, count = 0; int start; /* start by parsing the lengths for the small tree */ struct huffman_decoder* smallhuff = create_huffman_decoder(24, 6); smallhuff->huffnode[0].numbits = bitstream_read(bitbuf, 3); start = bitstream_read(bitbuf, 3) + 1; for (index = 1; index < 24; index++) { if (index < start || count == 7) smallhuff->huffnode[index].numbits = 0; else { count = bitstream_read(bitbuf, 3); smallhuff->huffnode[index].numbits = (count == 7) ? 0 : count; } } /* then regenerate the tree */ error = huffman_assign_canonical_codes(smallhuff); if (error != HUFFERR_NONE) return error; huffman_build_lookup_table(smallhuff); /* determine the maximum length of an RLE count */ temp = decoder->numcodes - 9; while (temp != 0) temp >>= 1, rlefullbits++; /* now process the rest of the data */ for (curcode = 0; curcode < decoder->numcodes; ) { int value = huffman_decode_one(smallhuff, bitbuf); if (value != 0) decoder->huffnode[curcode++].numbits = last = value - 1; else { int count = bitstream_read(bitbuf, 3) + 2; if (count == 7+2) count += bitstream_read(bitbuf, rlefullbits); for ( ; count != 0 && curcode < decoder->numcodes; count--) decoder->huffnode[curcode++].numbits = last; } } /* make sure we ended up with the right number */ if (curcode != decoder->numcodes) return HUFFERR_INVALID_DATA; /* assign canonical codes for all nodes based on their code lengths */ error = huffman_assign_canonical_codes(decoder); if (error != HUFFERR_NONE) return error; /* build the lookup table */ huffman_build_lookup_table(decoder); /* determine final input length and report errors */ return bitstream_overflow(bitbuf) ? HUFFERR_INPUT_BUFFER_TOO_SMALL : HUFFERR_NONE; }
static int atrac3p_decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt) { ATRAC3PContext *ctx = avctx->priv_data; AVFrame *frame = data; int i, ret, ch_unit_id, ch_block = 0, out_ch_index = 0, channels_to_process; float **samples_p = (float **)frame->extended_data; frame->nb_samples = ATRAC3P_FRAME_SAMPLES; if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) { av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n"); return ret; } if ((ret = bitstream_init8(&ctx->bc, avpkt->data, avpkt->size)) < 0) return ret; if (bitstream_read_bit(&ctx->bc)) { av_log(avctx, AV_LOG_ERROR, "Invalid start bit!\n"); return AVERROR_INVALIDDATA; } while (bitstream_bits_left(&ctx->bc) >= 2 && (ch_unit_id = bitstream_read(&ctx->bc, 2)) != CH_UNIT_TERMINATOR) { if (ch_unit_id == CH_UNIT_EXTENSION) { avpriv_report_missing_feature(avctx, "Channel unit extension"); return AVERROR_PATCHWELCOME; } if (ch_block >= ctx->num_channel_blocks || ctx->channel_blocks[ch_block] != ch_unit_id) { av_log(avctx, AV_LOG_ERROR, "Frame data doesn't match channel configuration!\n"); return AVERROR_INVALIDDATA; } ctx->ch_units[ch_block].unit_type = ch_unit_id; channels_to_process = ch_unit_id + 1; if ((ret = ff_atrac3p_decode_channel_unit(&ctx->bc, &ctx->ch_units[ch_block], channels_to_process, avctx)) < 0) return ret; decode_residual_spectrum(&ctx->ch_units[ch_block], ctx->samples, channels_to_process, avctx); reconstruct_frame(ctx, &ctx->ch_units[ch_block], channels_to_process, avctx); for (i = 0; i < channels_to_process; i++) memcpy(samples_p[out_ch_index + i], ctx->outp_buf[i], ATRAC3P_FRAME_SAMPLES * sizeof(**samples_p)); ch_block++; out_ch_index += channels_to_process; } *got_frame_ptr = 1; return avctx->block_align; }