#include <stdio.h>
#define BITSTREAM_READER_LE
+#include "libavutil/channel_layout.h"
#include "avcodec.h"
#include "get_bits.h"
-#include "dsputil.h"
+#include "internal.h"
#include "rdft.h"
#include "mpegaudiodsp.h"
#include "mpegaudio.h"
* QDM2 decoder context
*/
typedef struct {
- AVFrame frame;
-
/// Parameters from codec header, do not change during playback
int nb_channels; ///< number of channels
int channels; ///< number of channels
/// Parameters built from header parameters, do not change during playback
int group_order; ///< order of frame group
int fft_order; ///< order of FFT (actually fftorder+1)
- int fft_frame_size; ///< size of fft frame, in components (1 comples = re + im)
int frame_size; ///< size of data frame
int frequency_range;
int sub_sampling; ///< subsampling: 0=25%, 1=50%, 2=100% */
} QDM2Context;
-static uint8_t empty_buffer[FF_INPUT_BUFFER_PADDING_SIZE];
-
static VLC vlc_tab_level;
static VLC vlc_tab_diff;
static VLC vlc_tab_run;
int j,k;
int ch;
int run, case_val;
- int switchtable[23] = {0,5,1,5,5,5,5,5,2,5,5,5,5,5,5,5,3,5,5,5,5,5,4};
+ static const int switchtable[23] = {0,5,1,5,5,5,5,5,2,5,5,5,5,5,5,5,3,5,5,5,5,5,4};
for (ch = 0; ch < channels; ch++) {
for (j = 0; j < 64; ) {
int i, sb, ch, sb_used;
int tmp, tab;
- // This should never happen
- if (q->nb_channels <= 0)
- return;
-
for (ch = 0; ch < q->nb_channels; ch++)
for (sb = 0; sb < 30; sb++)
for (i = 0; i < 8; i++) {
int add1, add2, add3, add4;
int64_t multres;
- // This should never happen
- if (nb_channels <= 0)
- return;
-
if (!superblocktype_2_3) {
/* This case is untested, no samples available */
SAMPLES_NEEDED
break;
case 30:
- if (get_bits_left(gb) >= 4)
- samples[0] = type30_dequant[qdm2_get_vlc(gb, &vlc_tab_type30, 0, 1)];
- else
+ if (get_bits_left(gb) >= 4) {
+ unsigned index = qdm2_get_vlc(gb, &vlc_tab_type30, 0, 1);
+ if (index < FF_ARRAY_ELEMS(type30_dequant)) {
+ samples[0] = type30_dequant[index];
+ } else
+ samples[0] = SB_DITHERING_NOISE(sb,q->noise_idx);
+ } else
samples[0] = SB_DITHERING_NOISE(sb,q->noise_idx);
run = 1;
type34_predictor = samples[0];
type34_first = 0;
} else {
- samples[0] = type34_delta[qdm2_get_vlc(gb, &vlc_tab_type34, 0, 1)] / type34_div + type34_predictor;
- type34_predictor = samples[0];
+ unsigned index = qdm2_get_vlc(gb, &vlc_tab_type34, 0, 1);
+ if (index < FF_ARRAY_ELEMS(type34_delta)) {
+ samples[0] = type34_delta[index] / type34_div + type34_predictor;
+ type34_predictor = samples[0];
+ } else
+ samples[0] = SB_DITHERING_NOISE(sb,q->noise_idx);
}
} else {
samples[0] = SB_DITHERING_NOISE(sb,q->noise_idx);
*
* @param q context
* @param node pointer to node with packet
- * @param length packet length in bits
*/
-static void process_subpacket_10 (QDM2Context *q, QDM2SubPNode *node, int length)
+static void process_subpacket_10 (QDM2Context *q, QDM2SubPNode *node)
{
GetBitContext gb;
- init_get_bits(&gb, ((node == NULL) ? empty_buffer : node->packet->data), ((node == NULL) ? 0 : node->packet->size*8));
-
- if (length != 0) {
+ if (node) {
+ init_get_bits(&gb, node->packet->data, node->packet->size * 8);
init_tone_level_dequantization(q, &gb);
fill_tone_level_array(q, 1);
} else {
*
* @param q context
* @param node pointer to node with packet
- * @param length packet length in bit
*/
-static void process_subpacket_11 (QDM2Context *q, QDM2SubPNode *node, int length)
+static void process_subpacket_11 (QDM2Context *q, QDM2SubPNode *node)
{
GetBitContext gb;
+ int length = 0;
+
+ if (node) {
+ length = node->packet->size * 8;
+ init_get_bits(&gb, node->packet->data, length);
+ }
- init_get_bits(&gb, ((node == NULL) ? empty_buffer : node->packet->data), ((node == NULL) ? 0 : node->packet->size*8));
if (length >= 32) {
int c = get_bits (&gb, 13);
*
* @param q context
* @param node pointer to node with packet
- * @param length packet length in bits
*/
-static void process_subpacket_12 (QDM2Context *q, QDM2SubPNode *node, int length)
+static void process_subpacket_12 (QDM2Context *q, QDM2SubPNode *node)
{
GetBitContext gb;
+ int length = 0;
+
+ if (node) {
+ length = node->packet->size * 8;
+ init_get_bits(&gb, node->packet->data, length);
+ }
- init_get_bits(&gb, ((node == NULL) ? empty_buffer : node->packet->data), ((node == NULL) ? 0 : node->packet->size*8));
synthfilt_build_sb_samples(q, &gb, length, 8, QDM2_SB_USED(q->sub_sampling));
}
nodes[1] = qdm2_search_subpacket_type_in_list(list, 10);
if (nodes[1] != NULL)
- process_subpacket_10(q, nodes[1], nodes[1]->packet->size << 3);
+ process_subpacket_10(q, nodes[1]);
else
- process_subpacket_10(q, NULL, 0);
+ process_subpacket_10(q, NULL);
nodes[2] = qdm2_search_subpacket_type_in_list(list, 11);
if (nodes[0] != NULL && nodes[1] != NULL && nodes[2] != NULL)
- process_subpacket_11(q, nodes[2], (nodes[2]->packet->size << 3));
+ process_subpacket_11(q, nodes[2]);
else
- process_subpacket_11(q, NULL, 0);
+ process_subpacket_11(q, NULL);
nodes[3] = qdm2_search_subpacket_type_in_list(list, 12);
if (nodes[0] != NULL && nodes[1] != NULL && nodes[3] != NULL)
- process_subpacket_12(q, nodes[3], (nodes[3]->packet->size << 3));
+ process_subpacket_12(q, nodes[3]);
else
- process_subpacket_12(q, NULL, 0);
+ process_subpacket_12(q, NULL);
}
for (i = 0; packet_bytes > 0; i++) {
int j;
+ if (i >= FF_ARRAY_ELEMS(q->sub_packet_list_A)) {
+ SAMPLES_NEEDED_2("too many packet bytes");
+ return;
+ }
+
q->sub_packet_list_A[i].next = NULL;
if (i > 0) {
process_synthesis_subpackets(q, q->sub_packet_list_D);
q->do_synth_filter = 1;
} else if (q->do_synth_filter) {
- process_subpacket_10(q, NULL, 0);
- process_subpacket_11(q, NULL, 0);
- process_subpacket_12(q, NULL, 0);
+ process_subpacket_10(q, NULL);
+ process_subpacket_11(q, NULL);
+ process_subpacket_12(q, NULL);
}
/* **************************************************************** */
}
static void qdm2_calculate_fft (QDM2Context *q, int channel, int sub_packet)
{
const float gain = (q->channels == 1 && q->nb_channels == 2) ? 0.5f : 1.0f;
+ float *out = q->output_buffer + channel;
int i;
q->fft.complex[channel][0].re *= 2.0f;
q->fft.complex[channel][0].im = 0.0f;
q->rdft_ctx.rdft_calc(&q->rdft_ctx, (FFTSample *)q->fft.complex[channel]);
/* add samples to output buffer */
- for (i = 0; i < ((q->fft_frame_size + 15) & ~15); i++)
- q->output_buffer[q->channels * i + channel] += ((float *) q->fft.complex[channel])[i] * gain;
+ for (i = 0; i < FFALIGN(q->fft_size, 8); i++) {
+ out[0] += q->fft.complex[channel][i].re * gain;
+ out[q->channels] += q->fft.complex[channel][i].im * gain;
+ out += 2 * q->channels;
+ }
}
}
-#if 0
-static void dump_context(QDM2Context *q)
-{
- int i;
-#define PRINT(a,b) av_log(NULL,AV_LOG_DEBUG," %s = %d\n", a, b);
- PRINT("compressed_data",q->compressed_data);
- PRINT("compressed_size",q->compressed_size);
- PRINT("frame_size",q->frame_size);
- PRINT("checksum_size",q->checksum_size);
- PRINT("channels",q->channels);
- PRINT("nb_channels",q->nb_channels);
- PRINT("fft_frame_size",q->fft_frame_size);
- PRINT("fft_size",q->fft_size);
- PRINT("sub_sampling",q->sub_sampling);
- PRINT("fft_order",q->fft_order);
- PRINT("group_order",q->group_order);
- PRINT("group_size",q->group_size);
- PRINT("sub_packet",q->sub_packet);
- PRINT("frequency_range",q->frequency_range);
- PRINT("has_errors",q->has_errors);
- PRINT("fft_tone_end",q->fft_tone_end);
- PRINT("fft_tone_start",q->fft_tone_start);
- PRINT("fft_coefs_index",q->fft_coefs_index);
- PRINT("coeff_per_sb_select",q->coeff_per_sb_select);
- PRINT("cm_table_select",q->cm_table_select);
- PRINT("noise_idx",q->noise_idx);
-
- for (i = q->fft_tone_start; i < q->fft_tone_end; i++)
- {
- FFTTone *t = &q->fft_tones[i];
-
- av_log(NULL,AV_LOG_DEBUG,"Tone (%d) dump:\n", i);
- av_log(NULL,AV_LOG_DEBUG," level = %f\n", t->level);
-// PRINT(" level", t->level);
- PRINT(" phase", t->phase);
- PRINT(" phase_shift", t->phase_shift);
- PRINT(" duration", t->duration);
- PRINT(" samples_im", t->samples_im);
- PRINT(" samples_re", t->samples_re);
- PRINT(" table", t->table);
- }
-
-}
-#endif
-
-
/**
* Init parameters from codec extradata
*/
avctx->channels = s->nb_channels = s->channels = AV_RB32(extradata);
extradata += 4;
- if (s->channels > MPA_MAX_CHANNELS)
+ if (s->channels <= 0 || s->channels > MPA_MAX_CHANNELS)
return AVERROR_INVALIDDATA;
+ avctx->channel_layout = avctx->channels == 2 ? AV_CH_LAYOUT_STEREO :
+ AV_CH_LAYOUT_MONO;
avctx->sample_rate = AV_RB32(extradata);
extradata += 4;
}
s->fft_order = av_log2(s->fft_size) + 1;
- s->fft_frame_size = 2 * s->fft_size; // complex has two floats
// something like max decodable tones
s->group_order = av_log2(s->group_size) + 1;
avctx->sample_fmt = AV_SAMPLE_FMT_S16;
- avcodec_get_frame_defaults(&s->frame);
- avctx->coded_frame = &s->frame;
-
-// dump_context(s);
return 0;
}
q->compressed_data = in;
q->compressed_size = q->checksum_size;
-// dump_context(q);
-
/* copy old block, clear new block of output samples */
memmove(q->output_buffer, &q->output_buffer[frame_size], frame_size * sizeof(float));
memset(&q->output_buffer[frame_size], 0, frame_size * sizeof(float));
static int qdm2_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;
QDM2Context *s = avctx->priv_data;
return -1;
/* get output buffer */
- s->frame.nb_samples = 16 * s->frame_size;
- if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {
+ frame->nb_samples = 16 * s->frame_size;
+ if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
return ret;
}
- out = (int16_t *)s->frame.data[0];
+ out = (int16_t *)frame->data[0];
for (i = 0; i < 16; i++) {
if (qdm2_decode(s, buf, out) < 0)
out += s->channels * s->frame_size;
}
- *got_frame_ptr = 1;
- *(AVFrame *)data = s->frame;
+ *got_frame_ptr = 1;
return s->checksum_size;
}
{
.name = "qdm2",
.type = AVMEDIA_TYPE_AUDIO,
- .id = CODEC_ID_QDM2,
+ .id = AV_CODEC_ID_QDM2,
.priv_data_size = sizeof(QDM2Context),
.init = qdm2_decode_init,
.close = qdm2_decode_close,