flac: Convert to the new bitstream reader

Signed-off-by: Diego Biurrun <diego@biurrun.de>
This commit is contained in:
Alexandra Hájková 2017-04-29 02:15:30 +00:00 committed by Diego Biurrun
parent 79f64f7ebc
commit 9c1e111406
5 changed files with 70 additions and 70 deletions

View File

@ -22,8 +22,9 @@
#include "libavutil/channel_layout.h"
#include "libavutil/crc.h"
#include "libavutil/log.h"
#include "bitstream.h"
#include "bytestream.h"
#include "get_bits.h"
#include "flac.h"
#include "flacdata.h"
@ -40,33 +41,33 @@ static const uint64_t flac_channel_layouts[8] = {
AV_CH_LAYOUT_7POINT1
};
static int64_t get_utf8(GetBitContext *gb)
static int64_t get_utf8(BitstreamContext *bc)
{
int64_t val;
GET_UTF8(val, get_bits(gb, 8), return -1;)
GET_UTF8(val, bitstream_read(bc, 8), return -1;)
return val;
}
int ff_flac_decode_frame_header(AVCodecContext *avctx, GetBitContext *gb,
int ff_flac_decode_frame_header(AVCodecContext *avctx, BitstreamContext *bc,
FLACFrameInfo *fi, int log_level_offset)
{
int bs_code, sr_code, bps_code;
/* frame sync code */
if ((get_bits(gb, 15) & 0x7FFF) != 0x7FFC) {
if ((bitstream_read(bc, 15) & 0x7FFF) != 0x7FFC) {
av_log(avctx, AV_LOG_ERROR + log_level_offset, "invalid sync code\n");
return AVERROR_INVALIDDATA;
}
/* variable block size stream code */
fi->is_var_size = get_bits1(gb);
fi->is_var_size = bitstream_read_bit(bc);
/* block size and sample rate codes */
bs_code = get_bits(gb, 4);
sr_code = get_bits(gb, 4);
bs_code = bitstream_read(bc, 4);
sr_code = bitstream_read(bc, 4);
/* channels and decorrelation */
fi->ch_mode = get_bits(gb, 4);
fi->ch_mode = bitstream_read(bc, 4);
if (fi->ch_mode < FLAC_MAX_CHANNELS) {
fi->channels = fi->ch_mode + 1;
fi->ch_mode = FLAC_CHMODE_INDEPENDENT;
@ -80,7 +81,7 @@ int ff_flac_decode_frame_header(AVCodecContext *avctx, GetBitContext *gb,
}
/* bits per sample */
bps_code = get_bits(gb, 3);
bps_code = bitstream_read(bc, 3);
if (bps_code == 3 || bps_code == 7) {
av_log(avctx, AV_LOG_ERROR + log_level_offset,
"invalid sample size code (%d)\n",
@ -90,14 +91,14 @@ int ff_flac_decode_frame_header(AVCodecContext *avctx, GetBitContext *gb,
fi->bps = sample_size_table[bps_code];
/* reserved bit */
if (get_bits1(gb)) {
if (bitstream_read_bit(bc)) {
av_log(avctx, AV_LOG_ERROR + log_level_offset,
"broken stream, invalid padding\n");
return AVERROR_INVALIDDATA;
}
/* sample or frame count */
fi->frame_or_sample_num = get_utf8(gb);
fi->frame_or_sample_num = get_utf8(bc);
if (fi->frame_or_sample_num < 0) {
av_log(avctx, AV_LOG_ERROR + log_level_offset,
"sample/frame number invalid; utf8 fscked\n");
@ -110,9 +111,9 @@ int ff_flac_decode_frame_header(AVCodecContext *avctx, GetBitContext *gb,
"reserved blocksize code: 0\n");
return AVERROR_INVALIDDATA;
} else if (bs_code == 6) {
fi->blocksize = get_bits(gb, 8) + 1;
fi->blocksize = bitstream_read(bc, 8) + 1;
} else if (bs_code == 7) {
fi->blocksize = get_bits(gb, 16) + 1;
fi->blocksize = bitstream_read(bc, 16) + 1;
} else {
fi->blocksize = ff_flac_blocksize_table[bs_code];
}
@ -121,11 +122,11 @@ int ff_flac_decode_frame_header(AVCodecContext *avctx, GetBitContext *gb,
if (sr_code < 12) {
fi->samplerate = ff_flac_sample_rate_table[sr_code];
} else if (sr_code == 12) {
fi->samplerate = get_bits(gb, 8) * 1000;
fi->samplerate = bitstream_read(bc, 8) * 1000;
} else if (sr_code == 13) {
fi->samplerate = get_bits(gb, 16);
fi->samplerate = bitstream_read(bc, 16);
} else if (sr_code == 14) {
fi->samplerate = get_bits(gb, 16) * 10;
fi->samplerate = bitstream_read(bc, 16) * 10;
} else {
av_log(avctx, AV_LOG_ERROR + log_level_offset,
"illegal sample rate code %d\n",
@ -134,9 +135,9 @@ int ff_flac_decode_frame_header(AVCodecContext *avctx, GetBitContext *gb,
}
/* header CRC-8 check */
skip_bits(gb, 8);
if (av_crc(av_crc_get_table(AV_CRC_8_ATM), 0, gb->buffer,
get_bits_count(gb)/8)) {
bitstream_skip(bc, 8);
if (av_crc(av_crc_get_table(AV_CRC_8_ATM), 0, bc->buffer,
bitstream_tell(bc) / 8)) {
av_log(avctx, AV_LOG_ERROR + log_level_offset,
"header crc mismatch\n");
return AVERROR_INVALIDDATA;
@ -204,23 +205,22 @@ void ff_flac_set_channel_layout(AVCodecContext *avctx)
void ff_flac_parse_streaminfo(AVCodecContext *avctx, struct FLACStreaminfo *s,
const uint8_t *buffer)
{
GetBitContext gb;
init_get_bits(&gb, buffer, FLAC_STREAMINFO_SIZE*8);
BitstreamContext bc;
bitstream_init8(&bc, buffer, FLAC_STREAMINFO_SIZE);
skip_bits(&gb, 16); /* skip min blocksize */
s->max_blocksize = get_bits(&gb, 16);
bitstream_skip(&bc, 16); /* skip min blocksize */
s->max_blocksize = bitstream_read(&bc, 16);
if (s->max_blocksize < FLAC_MIN_BLOCKSIZE) {
av_log(avctx, AV_LOG_WARNING, "invalid max blocksize: %d\n",
s->max_blocksize);
s->max_blocksize = 16;
}
skip_bits(&gb, 24); /* skip min frame size */
s->max_framesize = get_bits_long(&gb, 24);
s->samplerate = get_bits_long(&gb, 20);
s->channels = get_bits(&gb, 3) + 1;
s->bps = get_bits(&gb, 5) + 1;
bitstream_skip(&bc, 24); /* skip min frame size */
s->max_framesize = bitstream_read(&bc, 24);
s->samplerate = bitstream_read(&bc, 20);
s->channels = bitstream_read(&bc, 3) + 1;
s->bps = bitstream_read(&bc, 5) + 1;
avctx->channels = s->channels;
avctx->sample_rate = s->samplerate;
@ -230,11 +230,11 @@ void ff_flac_parse_streaminfo(AVCodecContext *avctx, struct FLACStreaminfo *s,
av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels)
ff_flac_set_channel_layout(avctx);
s->samples = get_bits_long(&gb, 32) << 4;
s->samples |= get_bits(&gb, 4);
s->samples = bitstream_read(&bc, 32) << 4;
s->samples |= bitstream_read(&bc, 4);
skip_bits_long(&gb, 64); /* md5 sum */
skip_bits_long(&gb, 64); /* md5 sum */
bitstream_skip(&bc, 64); /* md5 sum */
bitstream_skip(&bc, 64); /* md5 sum */
}
#if LIBAVCODEC_VERSION_MAJOR < 57

View File

@ -28,8 +28,8 @@
#define AVCODEC_FLAC_H
#include "avcodec.h"
#include "bitstream.h"
#include "bytestream.h"
#include "get_bits.h"
#define FLAC_STREAMINFO_SIZE 34
#define FLAC_MAX_CHANNELS 8
@ -129,12 +129,12 @@ int ff_flac_get_max_frame_size(int blocksize, int ch, int bps);
/**
* Validate and decode a frame header.
* @param avctx AVCodecContext to use as av_log() context
* @param gb GetBitContext from which to read frame header
* @param bc BitstreamContext from which to read frame header
* @param[out] fi frame information
* @param log_level_offset log level offset. can be used to silence error messages.
* @return non-zero on error, 0 if ok
*/
int ff_flac_decode_frame_header(AVCodecContext *avctx, GetBitContext *gb,
int ff_flac_decode_frame_header(AVCodecContext *avctx, BitstreamContext *bc,
FLACFrameInfo *fi, int log_level_offset);
void ff_flac_set_channel_layout(AVCodecContext *avctx);

View File

@ -92,9 +92,9 @@ typedef struct FLACParseContext {
static int frame_header_is_valid(AVCodecContext *avctx, const uint8_t *buf,
FLACFrameInfo *fi)
{
GetBitContext gb;
init_get_bits(&gb, buf, MAX_FRAME_HEADER_SIZE * 8);
return !ff_flac_decode_frame_header(avctx, &gb, fi, 127);
BitstreamContext bc;
bitstream_init8(&bc, buf, MAX_FRAME_HEADER_SIZE);
return !ff_flac_decode_frame_header(avctx, &bc, fi, 127);
}
/**

View File

@ -34,10 +34,10 @@
#include <limits.h>
#include "avcodec.h"
#include "bitstream.h"
#include "internal.h"
#include "get_bits.h"
#include "bytestream.h"
#include "golomb_legacy.h"
#include "golomb.h"
#include "flac.h"
#include "flacdata.h"
#include "flacdsp.h"
@ -46,7 +46,7 @@ typedef struct FLACContext {
FLACSTREAMINFO
AVCodecContext *avctx; ///< parent AVCodecContext
GetBitContext gb; ///< GetBitContext initialized to start at the current frame
BitstreamContext bc; ///< BitstreamContext initialized to start at the current frame
int blocksize; ///< number of samples in the current frame
int sample_shift; ///< shift required to make output samples 16-bit or 32-bit
@ -203,14 +203,14 @@ static int decode_residuals(FLACContext *s, int32_t *decoded, int pred_order)
int rice_bits, rice_esc;
int samples;
method_type = get_bits(&s->gb, 2);
method_type = bitstream_read(&s->bc, 2);
if (method_type > 1) {
av_log(s->avctx, AV_LOG_ERROR, "illegal residual coding method %d\n",
method_type);
return AVERROR_INVALIDDATA;
}
rice_order = get_bits(&s->gb, 4);
rice_order = bitstream_read(&s->bc, 4);
samples= s->blocksize >> rice_order;
if (pred_order > samples) {
@ -225,14 +225,14 @@ static int decode_residuals(FLACContext *s, int32_t *decoded, int pred_order)
decoded += pred_order;
i= pred_order;
for (partition = 0; partition < (1 << rice_order); partition++) {
tmp = get_bits(&s->gb, rice_bits);
tmp = bitstream_read(&s->bc, rice_bits);
if (tmp == rice_esc) {
tmp = get_bits(&s->gb, 5);
tmp = bitstream_read(&s->bc, 5);
for (; i < samples; i++)
*decoded++ = get_sbits_long(&s->gb, tmp);
*decoded++ = bitstream_read_signed(&s->bc, tmp);
} else {
for (; i < samples; i++) {
*decoded++ = get_sr_golomb_flac(&s->gb, tmp, INT_MAX, 0);
*decoded++ = get_sr_golomb_flac(&s->bc, tmp, INT_MAX, 0);
}
}
i= 0;
@ -249,7 +249,7 @@ static int decode_subframe_fixed(FLACContext *s, int32_t *decoded,
/* warm up samples */
for (i = 0; i < pred_order; i++) {
decoded[i] = get_sbits_long(&s->gb, bps);
decoded[i] = bitstream_read_signed(&s->bc, bps);
}
if ((ret = decode_residuals(s, decoded, pred_order)) < 0)
@ -300,15 +300,15 @@ static int decode_subframe_lpc(FLACContext *s, int32_t *decoded, int pred_order,
/* warm up samples */
for (i = 0; i < pred_order; i++) {
decoded[i] = get_sbits_long(&s->gb, bps);
decoded[i] = bitstream_read_signed(&s->bc, bps);
}
coeff_prec = get_bits(&s->gb, 4) + 1;
coeff_prec = bitstream_read(&s->bc, 4) + 1;
if (coeff_prec == 16) {
av_log(s->avctx, AV_LOG_ERROR, "invalid coeff precision\n");
return AVERROR_INVALIDDATA;
}
qlevel = get_sbits(&s->gb, 5);
qlevel = bitstream_read_signed(&s->bc, 5);
if (qlevel < 0) {
av_log(s->avctx, AV_LOG_ERROR, "qlevel %d not supported, maybe buggy stream\n",
qlevel);
@ -316,7 +316,7 @@ static int decode_subframe_lpc(FLACContext *s, int32_t *decoded, int pred_order,
}
for (i = 0; i < pred_order; i++) {
coeffs[pred_order - i - 1] = get_sbits(&s->gb, coeff_prec);
coeffs[pred_order - i - 1] = bitstream_read_signed(&s->bc, coeff_prec);
}
if ((ret = decode_residuals(s, decoded, pred_order)) < 0)
@ -342,24 +342,24 @@ static inline int decode_subframe(FLACContext *s, int channel)
bps++;
}
if (get_bits1(&s->gb)) {
if (bitstream_read_bit(&s->bc)) {
av_log(s->avctx, AV_LOG_ERROR, "invalid subframe padding\n");
return AVERROR_INVALIDDATA;
}
type = get_bits(&s->gb, 6);
type = bitstream_read(&s->bc, 6);
if (get_bits1(&s->gb)) {
int left = get_bits_left(&s->gb);
if (bitstream_read_bit(&s->bc)) {
int left = bitstream_bits_left(&s->bc);
wasted = 1;
if ( left < 0 ||
(left < bps && !show_bits_long(&s->gb, left)) ||
!show_bits_long(&s->gb, bps)) {
(left < bps && !bitstream_peek(&s->bc, left)) ||
!bitstream_peek(&s->bc, bps)) {
av_log(s->avctx, AV_LOG_ERROR,
"Invalid number of wasted bits > available bits (%d) - left=%d\n",
bps, left);
return AVERROR_INVALIDDATA;
}
while (!get_bits1(&s->gb))
while (!bitstream_read_bit(&s->bc))
wasted++;
bps -= wasted;
}
@ -370,12 +370,12 @@ static inline int decode_subframe(FLACContext *s, int channel)
//FIXME use av_log2 for types
if (type == 0) {
tmp = get_sbits_long(&s->gb, bps);
tmp = bitstream_read_signed(&s->bc, bps);
for (i = 0; i < s->blocksize; i++)
decoded[i] = tmp;
} else if (type == 1) {
for (i = 0; i < s->blocksize; i++)
decoded[i] = get_sbits_long(&s->gb, bps);
decoded[i] = bitstream_read_signed(&s->bc, bps);
} else if ((type >= 8) && (type <= 12)) {
if ((ret = decode_subframe_fixed(s, decoded, type & ~0x8, bps)) < 0)
return ret;
@ -399,10 +399,10 @@ static inline int decode_subframe(FLACContext *s, int channel)
static int decode_frame(FLACContext *s)
{
int i, ret;
GetBitContext *gb = &s->gb;
BitstreamContext *bc = &s->bc;
FLACFrameInfo fi;
if ((ret = ff_flac_decode_frame_header(s->avctx, gb, &fi, 0)) < 0) {
if ((ret = ff_flac_decode_frame_header(s->avctx, bc, &fi, 0)) < 0) {
av_log(s->avctx, AV_LOG_ERROR, "invalid frame header\n");
return ret;
}
@ -471,10 +471,10 @@ static int decode_frame(FLACContext *s)
return ret;
}
align_get_bits(gb);
bitstream_align(bc);
/* frame footer */
skip_bits(gb, 16); /* data crc */
bitstream_skip(bc, 16); /* data crc */
return 0;
}
@ -513,12 +513,12 @@ static int flac_decode_frame(AVCodecContext *avctx, void *data,
}
/* decode frame */
init_get_bits(&s->gb, buf, buf_size*8);
bitstream_init8(&s->bc, buf, buf_size);
if ((ret = decode_frame(s)) < 0) {
av_log(s->avctx, AV_LOG_ERROR, "decode_frame() failed\n");
return ret;
}
bytes_read = (get_bits_count(&s->gb)+7)/8;
bytes_read = (bitstream_tell(&s->bc) + 7) / 8;
/* get output buffer */
frame->nb_samples = s->blocksize;

View File

@ -26,7 +26,7 @@
#include "avcodec.h"
#include "bswapdsp.h"
#include "golomb_legacy.h"
#include "golomb.h"
#include "internal.h"
#include "lpc.h"
#include "flac.h"