* Dedicated to the mastermind behind it, Ralph Wiggum.
*/
+#include "libavutil/attributes.h"
+#include "libavutil/channel_layout.h"
+
#include "avcodec.h"
-#include "get_bits.h"
+#include "bitstream.h"
#include "golomb.h"
+#include "internal.h"
#include "unary.h"
-#include "libavutil/audioconvert.h"
+#include "vlc.h"
#include "ralfdata.h"
#define FILTER_NONE 0
#define RALF_MAX_PKT_SIZE 8192
typedef struct RALFContext {
- AVFrame frame;
-
int version;
int max_frame_size;
VLCSet sets[3];
#define MAX_ELEMS 644 // no RALF table uses more than that
-static int init_ralf_vlc(VLC *vlc, const uint8_t *data, int elems)
+static av_cold int init_ralf_vlc(VLC *vlc, const uint8_t *data, int elems)
{
uint8_t lens[MAX_ELEMS];
uint16_t codes[MAX_ELEMS];
ctx->version = AV_RB16(avctx->extradata + 4);
if (ctx->version != 0x103) {
- av_log_ask_for_sample(avctx, "unknown version %X\n", ctx->version);
+ avpriv_request_sample(avctx, "Unknown version %X", ctx->version);
return AVERROR_PATCHWELCOME;
}
avctx->sample_rate, avctx->channels);
return AVERROR_INVALIDDATA;
}
- avctx->sample_fmt = AV_SAMPLE_FMT_S16;
+ avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
avctx->channel_layout = (avctx->channels == 2) ? AV_CH_LAYOUT_STEREO
: AV_CH_LAYOUT_MONO;
- avcodec_get_frame_defaults(&ctx->frame);
- avctx->coded_frame = &ctx->frame;
-
ctx->max_frame_size = AV_RB32(avctx->extradata + 16);
if (ctx->max_frame_size > (1 << 20) || !ctx->max_frame_size) {
av_log(avctx, AV_LOG_ERROR, "invalid frame size %d\n",
return 0;
}
-static inline int extend_code(GetBitContext *gb, int val, int range, int bits)
+static inline int extend_code(BitstreamContext *bc, int val, int range, int bits)
{
if (val == 0) {
- val = -range - get_ue_golomb(gb);
+ val = -range - get_ue_golomb(bc);
} else if (val == range * 2) {
- val = range + get_ue_golomb(gb);
+ val = range + get_ue_golomb(bc);
} else {
val -= range;
}
if (bits)
- val = (val << bits) | get_bits(gb, bits);
+ val = (val << bits) | bitstream_read(bc, bits);
return val;
}
-static int decode_channel(RALFContext *ctx, GetBitContext *gb, int ch,
+static int decode_channel(RALFContext *ctx, BitstreamContext *bc, int ch,
int length, int mode, int bits)
{
int i, t;
VLC *code_vlc; int range, range2, add_bits;
int *dst = ctx->channel_data[ch];
- ctx->filter_params = get_vlc2(gb, set->filter_params.table, 9, 2);
+ ctx->filter_params = bitstream_read_vlc(bc, set->filter_params.table, 9, 2);
ctx->filter_bits = (ctx->filter_params - 2) >> 6;
ctx->filter_length = ctx->filter_params - (ctx->filter_bits << 6) - 1;
if (ctx->filter_params == FILTER_RAW) {
for (i = 0; i < length; i++)
- dst[i] = get_bits(gb, bits);
+ dst[i] = bitstream_read(bc, bits);
ctx->bias[ch] = 0;
return 0;
}
- ctx->bias[ch] = get_vlc2(gb, set->bias.table, 9, 2);
- ctx->bias[ch] = extend_code(gb, ctx->bias[ch], 127, 4);
+ ctx->bias[ch] = bitstream_read_vlc(bc, set->bias.table, 9, 2);
+ ctx->bias[ch] = extend_code(bc, ctx->bias[ch], 127, 4);
if (ctx->filter_params == FILTER_NONE) {
memset(dst, 0, sizeof(*dst) * length);
add_bits = ctx->filter_bits;
for (i = 0; i < ctx->filter_length; i++) {
- t = get_vlc2(gb, vlc[cmode].table, vlc[cmode].bits, 2);
- t = extend_code(gb, t, 21, add_bits);
+ t = bitstream_read_vlc(bc, vlc[cmode].table, vlc[cmode].bits, 2);
+ t = extend_code(bc, t, 21, add_bits);
if (!cmode)
coeff -= 12 << add_bits;
coeff = t - coeff;
}
}
- code_params = get_vlc2(gb, set->coding_mode.table, set->coding_mode.bits, 2);
+ code_params = bitstream_read_vlc(bc, set->coding_mode.table, set->coding_mode.bits, 2);
if (code_params >= 15) {
add_bits = av_clip((code_params / 5 - 3) / 2, 0, 10);
if (add_bits > 9 && (code_params % 5) != 2)
for (i = 0; i < length; i += 2) {
int code1, code2;
- t = get_vlc2(gb, code_vlc->table, code_vlc->bits, 2);
+ t = bitstream_read_vlc(bc, code_vlc->table, code_vlc->bits, 2);
code1 = t / range2;
code2 = t % range2;
- dst[i] = extend_code(gb, code1, range, 0) << add_bits;
- dst[i + 1] = extend_code(gb, code2, range, 0) << add_bits;
+ dst[i] = extend_code(bc, code1, range, 0) << add_bits;
+ dst[i + 1] = extend_code(bc, code2, range, 0) << add_bits;
if (add_bits) {
- dst[i] |= get_bits(gb, add_bits);
- dst[i + 1] |= get_bits(gb, add_bits);
+ dst[i] |= bitstream_read(bc, add_bits);
+ dst[i + 1] |= bitstream_read(bc, add_bits);
}
}
}
}
-static int decode_block(AVCodecContext *avctx, GetBitContext *gb, int16_t *dst)
+static int decode_block(AVCodecContext *avctx, BitstreamContext *bc,
+ int16_t *dst0, int16_t *dst1)
{
RALFContext *ctx = avctx->priv_data;
int len, ch, ret;
int *ch0, *ch1;
int i, t, t2;
- len = 12 - get_unary(gb, 0, 6);
+ len = 12 - get_unary(bc, 0, 6);
if (len <= 7) len ^= 1; // codes for length = 6 and 7 are swapped
len = 1 << len;
}
if (avctx->channels > 1)
- dmode = get_bits(gb, 2) + 1;
+ dmode = bitstream_read(bc, 2) + 1;
else
dmode = 0;
bits[1] = (mode[1] == 2) ? 17 : 16;
for (ch = 0; ch < avctx->channels; ch++) {
- if ((ret = decode_channel(ctx, gb, ch, len, mode[ch], bits[ch])) < 0)
+ if ((ret = decode_channel(ctx, bc, ch, len, mode[ch], bits[ch])) < 0)
return ret;
if (ctx->filter_params > 1 && ctx->filter_params != FILTER_RAW) {
ctx->filter_bits += 3;
apply_lpc(ctx, ch, len, bits[ch]);
}
- if (get_bits_left(gb) < 0)
+ if (bitstream_bits_left(bc) < 0)
return AVERROR_INVALIDDATA;
}
ch0 = ctx->channel_data[0];
switch (dmode) {
case 0:
for (i = 0; i < len; i++)
- *dst++ = ch0[i] + ctx->bias[0];
+ dst0[i] = ch0[i] + ctx->bias[0];
break;
case 1:
for (i = 0; i < len; i++) {
- *dst++ = ch0[i] + ctx->bias[0];
- *dst++ = ch1[i] + ctx->bias[1];
+ dst0[i] = ch0[i] + ctx->bias[0];
+ dst1[i] = ch1[i] + ctx->bias[1];
}
break;
case 2:
for (i = 0; i < len; i++) {
ch0[i] += ctx->bias[0];
- *dst++ = ch0[i];
- *dst++ = ch0[i] - (ch1[i] + ctx->bias[1]);
+ dst0[i] = ch0[i];
+ dst1[i] = ch0[i] - (ch1[i] + ctx->bias[1]);
}
break;
case 3:
for (i = 0; i < len; i++) {
t = ch0[i] + ctx->bias[0];
t2 = ch1[i] + ctx->bias[1];
- *dst++ = t + t2;
- *dst++ = t;
+ dst0[i] = t + t2;
+ dst1[i] = t;
}
break;
case 4:
for (i = 0; i < len; i++) {
t = ch1[i] + ctx->bias[1];
t2 = ((ch0[i] + ctx->bias[0]) << 1) | (t & 1);
- *dst++ = (t2 + t) / 2;
- *dst++ = (t2 - t) / 2;
+ dst0[i] = (t2 + t) / 2;
+ dst1[i] = (t2 - t) / 2;
}
break;
}
AVPacket *avpkt)
{
RALFContext *ctx = avctx->priv_data;
- int16_t *samples;
+ AVFrame *frame = data;
+ int16_t *samples0;
+ int16_t *samples1;
int ret;
- GetBitContext gb;
+ BitstreamContext bc;
int table_size, table_bytes, i;
const uint8_t *src, *block_pointer;
int src_size;
src_size = avpkt->size;
}
- ctx->frame.nb_samples = ctx->max_frame_size;
- if ((ret = avctx->get_buffer(avctx, &ctx->frame)) < 0) {
+ frame->nb_samples = ctx->max_frame_size;
+ if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
av_log(avctx, AV_LOG_ERROR, "Me fail get_buffer()? That's unpossible!\n");
return ret;
}
- samples = (int16_t*)ctx->frame.data[0];
+ samples0 = (int16_t *)frame->data[0];
+ samples1 = (int16_t *)frame->data[1];
if (src_size < 5) {
av_log(avctx, AV_LOG_ERROR, "too short packets are too short!\n");
av_log(avctx, AV_LOG_ERROR, "short packets are short!\n");
return AVERROR_INVALIDDATA;
}
- init_get_bits(&gb, src + 2, table_size);
+ bitstream_init(&bc, src + 2, table_size);
ctx->num_blocks = 0;
- while (get_bits_left(&gb) > 0) {
- ctx->block_size[ctx->num_blocks] = get_bits(&gb, 15);
- if (get_bits1(&gb)) {
- ctx->block_pts[ctx->num_blocks] = get_bits(&gb, 9);
+ while (bitstream_bits_left(&bc) > 0) {
+ ctx->block_size[ctx->num_blocks] = bitstream_read(&bc, 15);
+ if (bitstream_read_bit(&bc)) {
+ ctx->block_pts[ctx->num_blocks] = bitstream_read(&bc, 9);
} else {
ctx->block_pts[ctx->num_blocks] = 0;
}
av_log(avctx, AV_LOG_ERROR, "I'm pedaling backwards\n");
break;
}
- init_get_bits(&gb, block_pointer, ctx->block_size[i] * 8);
- if (decode_block(avctx, &gb, samples + ctx->sample_offset
- * avctx->channels) < 0) {
+ bitstream_init8(&bc, block_pointer, ctx->block_size[i]);
+ if (decode_block(avctx, &bc, samples0 + ctx->sample_offset,
+ samples1 + ctx->sample_offset) < 0) {
av_log(avctx, AV_LOG_ERROR, "Sir, I got carsick in your office. Not decoding the rest of packet.\n");
break;
}
bytes_left -= ctx->block_size[i];
}
- ctx->frame.nb_samples = ctx->sample_offset;
- *got_frame_ptr = ctx->sample_offset > 0;
- *(AVFrame*)data = ctx->frame;
+ frame->nb_samples = ctx->sample_offset;
+ *got_frame_ptr = ctx->sample_offset > 0;
return avpkt->size;
}
AVCodec ff_ralf_decoder = {
.name = "ralf",
+ .long_name = NULL_IF_CONFIG_SMALL("RealAudio Lossless"),
.type = AVMEDIA_TYPE_AUDIO,
.id = AV_CODEC_ID_RALF,
.priv_data_size = sizeof(RALFContext),
.close = decode_close,
.decode = decode_frame,
.flush = decode_flush,
- .capabilities = CODEC_CAP_DR1,
- .long_name = NULL_IF_CONFIG_SMALL("RealAudio Lossless"),
+ .capabilities = AV_CODEC_CAP_DR1,
+ .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16P,
+ AV_SAMPLE_FMT_NONE },
};