FFmpeg
Data Structures | Macros | Typedefs | Functions | Variables
qdm2.c File Reference
#include <math.h>
#include <stddef.h>
#include <stdio.h>
#include "libavutil/channel_layout.h"
#include "libavutil/mem_internal.h"
#include "libavutil/thread.h"
#include "avcodec.h"
#include "get_bits.h"
#include "bytestream.h"
#include "internal.h"
#include "mpegaudio.h"
#include "mpegaudiodsp.h"
#include "rdft.h"
#include "qdm2_tablegen.h"

Go to the source code of this file.

Data Structures

struct  QDM2SubPacket
 Subpacket. More...
 
struct  QDM2SubPNode
 A node in the subpacket list. More...
 
struct  QDM2Complex
 
struct  FFTTone
 
struct  FFTCoefficient
 
struct  QDM2FFT
 
struct  QDM2Context
 QDM2 decoder context. More...
 

Macros

#define BITSTREAM_READER_LE
 
#define QDM2_LIST_ADD(list, size, packet)
 
#define QDM2_SB_USED(sub_sampling)   (((sub_sampling) >= 2) ? 30 : 8 << (sub_sampling))
 
#define FIX_NOISE_IDX(noise_idx)
 
#define SB_DITHERING_NOISE(sb, noise_idx)   (noise_table[(noise_idx)++] * sb_noise_attenuation[(sb)])
 
#define SAMPLES_NEEDED   av_log (NULL,AV_LOG_INFO,"This file triggers some untested code. Please contact the developers.\n");
 
#define SAMPLES_NEEDED_2(why)   av_log (NULL,AV_LOG_INFO,"This file triggers some missing code. Please contact the developers.\nPosition: %s\n",why);
 
#define QDM2_MAX_FRAME_SIZE   512
 

Typedefs

typedef int8_t sb_int8_array[2][30][64]
 

Functions

static int qdm2_get_vlc (GetBitContext *gb, const VLC *vlc, int flag, int depth)
 
static int qdm2_get_se_vlc (const VLC *vlc, GetBitContext *gb, int depth)
 
static uint16_t qdm2_packet_checksum (const uint8_t *data, int length, int value)
 QDM2 checksum. More...
 
static void qdm2_decode_sub_packet_header (GetBitContext *gb, QDM2SubPacket *sub_packet)
 Fill a QDM2SubPacket structure with packet type, size, and data pointer. More...
 
static QDM2SubPNodeqdm2_search_subpacket_type_in_list (QDM2SubPNode *list, int type)
 Return node pointer to first packet of requested type in list. More...
 
static void average_quantized_coeffs (QDM2Context *q)
 Replace 8 elements with their average value. More...
 
static void build_sb_samples_from_noise (QDM2Context *q, int sb)
 Build subband samples with noise weighted by q->tone_level. More...
 
static int fix_coding_method_array (int sb, int channels, sb_int8_array coding_method)
 Called while processing data from subpackets 11 and 12. More...
 
static void fill_tone_level_array (QDM2Context *q, int flag)
 Related to synthesis filter Called by process_subpacket_10. More...
 
static void fill_coding_method_array (sb_int8_array tone_level_idx, sb_int8_array tone_level_idx_temp, sb_int8_array coding_method, int nb_channels, int c, int superblocktype_2_3, int cm_table_select)
 Related to synthesis filter Called by process_subpacket_11 c is built with data from subpacket 11 Most of this function is used only if superblock_type_2_3 == 0, never seen it in samples. More...
 
static int synthfilt_build_sb_samples (QDM2Context *q, GetBitContext *gb, int length, int sb_min, int sb_max)
 Called by process_subpacket_11 to process more data from subpacket 11 with sb 0-8. More...
 
static int init_quantized_coeffs_elem0 (int8_t *quantized_coeffs, GetBitContext *gb)
 Init the first element of a channel in quantized_coeffs with data from packet 10 (quantized_coeffs[ch][0]). More...
 
static void init_tone_level_dequantization (QDM2Context *q, GetBitContext *gb)
 Related to synthesis filter, process data from packet 10 Init part of quantized_coeffs via function init_quantized_coeffs_elem0 Init tone_level_idx_hi1, tone_level_idx_hi2, tone_level_idx_mid with data from packet 10. More...
 
static int process_subpacket_9 (QDM2Context *q, QDM2SubPNode *node)
 Process subpacket 9, init quantized_coeffs with data from it. More...
 
static void process_subpacket_10 (QDM2Context *q, QDM2SubPNode *node)
 Process subpacket 10 if not null, else. More...
 
static void process_subpacket_11 (QDM2Context *q, QDM2SubPNode *node)
 Process subpacket 11. More...
 
static void process_subpacket_12 (QDM2Context *q, QDM2SubPNode *node)
 Process subpacket 12. More...
 
static void process_synthesis_subpackets (QDM2Context *q, QDM2SubPNode *list)
 Process new subpackets for synthesis filter. More...
 
static void qdm2_decode_super_block (QDM2Context *q)
 Decode superblock, fill packet lists. More...
 
static void qdm2_fft_init_coefficient (QDM2Context *q, int sub_packet, int offset, int duration, int channel, int exp, int phase)
 
static void qdm2_fft_decode_tones (QDM2Context *q, int duration, GetBitContext *gb, int b)
 
static void qdm2_decode_fft_packets (QDM2Context *q)
 
static void qdm2_fft_generate_tone (QDM2Context *q, FFTTone *tone)
 
static void qdm2_fft_tone_synthesizer (QDM2Context *q, int sub_packet)
 
static void qdm2_calculate_fft (QDM2Context *q, int channel, int sub_packet)
 
static void qdm2_synthesis_filter (QDM2Context *q, int index)
 
static av_cold void qdm2_init_static_data (void)
 Init static data (does not depend on specific file) More...
 
static av_cold int qdm2_decode_init (AVCodecContext *avctx)
 Init parameters from codec extradata. More...
 
static av_cold int qdm2_decode_close (AVCodecContext *avctx)
 
static int qdm2_decode (QDM2Context *q, const uint8_t *in, int16_t *out)
 
static int qdm2_decode_frame (AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt)
 

Variables

static const int switchtable [23]
 
const AVCodec ff_qdm2_decoder
 

Detailed Description

QDM2 decoder

Author
Ewald Snel, Benjamin Larsson, Alex Beregszaszi, Roberto Togni

The decoder is not perfect yet, there are still some distortions especially on files encoded with 16 or 8 subbands.

Definition in file qdm2.c.

Macro Definition Documentation

◆ BITSTREAM_READER_LE

#define BITSTREAM_READER_LE

Definition at line 42 of file qdm2.c.

◆ QDM2_LIST_ADD

#define QDM2_LIST_ADD (   list,
  size,
  packet 
)
Value:
do { \
if (size > 0) { \
list[size - 1].next = &list[size]; \
} \
list[size].packet = packet; \
list[size].next = NULL; \
size++; \
} while(0)

Definition at line 53 of file qdm2.c.

◆ QDM2_SB_USED

#define QDM2_SB_USED (   sub_sampling)    (((sub_sampling) >= 2) ? 30 : 8 << (sub_sampling))

Definition at line 64 of file qdm2.c.

◆ FIX_NOISE_IDX

#define FIX_NOISE_IDX (   noise_idx)
Value:
if ((noise_idx) >= 3840) \
(noise_idx) -= 3840; \

Definition at line 66 of file qdm2.c.

◆ SB_DITHERING_NOISE

#define SB_DITHERING_NOISE (   sb,
  noise_idx 
)    (noise_table[(noise_idx)++] * sb_noise_attenuation[(sb)])

Definition at line 70 of file qdm2.c.

◆ SAMPLES_NEEDED

#define SAMPLES_NEEDED   av_log (NULL,AV_LOG_INFO,"This file triggers some untested code. Please contact the developers.\n");

Definition at line 72 of file qdm2.c.

◆ SAMPLES_NEEDED_2

#define SAMPLES_NEEDED_2 (   why)    av_log (NULL,AV_LOG_INFO,"This file triggers some missing code. Please contact the developers.\nPosition: %s\n",why);

Definition at line 75 of file qdm2.c.

◆ QDM2_MAX_FRAME_SIZE

#define QDM2_MAX_FRAME_SIZE   512

Definition at line 78 of file qdm2.c.

Typedef Documentation

◆ sb_int8_array

typedef int8_t sb_int8_array[2][30][64]

Definition at line 80 of file qdm2.c.

Function Documentation

◆ qdm2_get_vlc()

static int qdm2_get_vlc ( GetBitContext gb,
const VLC vlc,
int  flag,
int  depth 
)
static

◆ qdm2_get_se_vlc()

static int qdm2_get_se_vlc ( const VLC vlc,
GetBitContext gb,
int  depth 
)
static

Definition at line 232 of file qdm2.c.

Referenced by init_quantized_coeffs_elem0(), and process_subpacket_9().

◆ qdm2_packet_checksum()

static uint16_t qdm2_packet_checksum ( const uint8_t *  data,
int  length,
int  value 
)
static

QDM2 checksum.

Parameters
datapointer to data to be checksummed
lengthdata length
valuechecksum value
Returns
0 if checksum is OK

Definition at line 248 of file qdm2.c.

Referenced by qdm2_decode_super_block().

◆ qdm2_decode_sub_packet_header()

static void qdm2_decode_sub_packet_header ( GetBitContext gb,
QDM2SubPacket sub_packet 
)
static

Fill a QDM2SubPacket structure with packet type, size, and data pointer.

Parameters
gbbitreader context
sub_packetpacket under analysis

Definition at line 264 of file qdm2.c.

Referenced by qdm2_decode_super_block().

◆ qdm2_search_subpacket_type_in_list()

static QDM2SubPNode* qdm2_search_subpacket_type_in_list ( QDM2SubPNode list,
int  type 
)
static

Return node pointer to first packet of requested type in list.

Parameters
listlist of subpackets to be scanned
typetype of searched subpacket
Returns
node pointer for subpacket if found, else NULL

Definition at line 299 of file qdm2.c.

Referenced by process_synthesis_subpackets().

◆ average_quantized_coeffs()

static void average_quantized_coeffs ( QDM2Context q)
static

Replace 8 elements with their average value.

Called by qdm2_decode_superblock before starting subblock decoding.

Parameters
qcontext

Definition at line 316 of file qdm2.c.

Referenced by qdm2_decode_super_block().

◆ build_sb_samples_from_noise()

static void build_sb_samples_from_noise ( QDM2Context q,
int  sb 
)
static

Build subband samples with noise weighted by q->tone_level.

Called by synthfilt_build_sb_samples.

Parameters
qcontext
sbsubband index

Definition at line 345 of file qdm2.c.

Referenced by synthfilt_build_sb_samples().

◆ fix_coding_method_array()

static int fix_coding_method_array ( int  sb,
int  channels,
sb_int8_array  coding_method 
)
static

Called while processing data from subpackets 11 and 12.

Used after making changes to coding_method array.

Parameters
sbsubband index
channelsnumber of channels
coding_methodq->coding_method[0][0][0]

Definition at line 372 of file qdm2.c.

Referenced by fill_coding_method_array(), and synthfilt_build_sb_samples().

◆ fill_tone_level_array()

static void fill_tone_level_array ( QDM2Context q,
int  flag 
)
static

Related to synthesis filter Called by process_subpacket_10.

Parameters
qcontext
flag1 if called after getting data from subpacket 10, 0 if no subpacket 10

Definition at line 443 of file qdm2.c.

Referenced by process_subpacket_10().

◆ fill_coding_method_array()

static void fill_coding_method_array ( sb_int8_array  tone_level_idx,
sb_int8_array  tone_level_idx_temp,
sb_int8_array  coding_method,
int  nb_channels,
int  c,
int  superblocktype_2_3,
int  cm_table_select 
)
static

Related to synthesis filter Called by process_subpacket_11 c is built with data from subpacket 11 Most of this function is used only if superblock_type_2_3 == 0, never seen it in samples.

Parameters
tone_level_idx
tone_level_idx_temp
coding_methodq->coding_method[0][0][0]
nb_channelsnumber of channels
ccoming from subpacket 11, passed as 8*c
superblocktype_2_3flag based on superblock packet type
cm_table_selectq->cm_table_select

Definition at line 532 of file qdm2.c.

Referenced by process_subpacket_11().

◆ synthfilt_build_sb_samples()

static int synthfilt_build_sb_samples ( QDM2Context q,
GetBitContext gb,
int  length,
int  sb_min,
int  sb_max 
)
static

Called by process_subpacket_11 to process more data from subpacket 11 with sb 0-8.

Called by process_subpacket_12 to process data from subpacket 12 with sb 8-sb_used.

Parameters
qcontext
gbbitreader context
lengthpacket length in bits
sb_minlower subband processed (sb_min included)
sb_maxhigher subband processed (sb_max excluded)

Definition at line 659 of file qdm2.c.

Referenced by process_subpacket_11(), and process_subpacket_12().

◆ init_quantized_coeffs_elem0()

static int init_quantized_coeffs_elem0 ( int8_t *  quantized_coeffs,
GetBitContext gb 
)
static

Init the first element of a channel in quantized_coeffs with data from packet 10 (quantized_coeffs[ch][0]).

This is similar to process_subpacket_9, but for a single channel and for element [0] same VLC tables as process_subpacket_9 are used.

Parameters
quantized_coeffspointer to quantized_coeffs[ch][0]
gbbitreader context

Definition at line 875 of file qdm2.c.

Referenced by init_tone_level_dequantization().

◆ init_tone_level_dequantization()

static void init_tone_level_dequantization ( QDM2Context q,
GetBitContext gb 
)
static

Related to synthesis filter, process data from packet 10 Init part of quantized_coeffs via function init_quantized_coeffs_elem0 Init tone_level_idx_hi1, tone_level_idx_hi2, tone_level_idx_mid with data from packet 10.

Parameters
qcontext
gbbitreader context

Definition at line 916 of file qdm2.c.

Referenced by process_subpacket_10().

◆ process_subpacket_9()

static int process_subpacket_9 ( QDM2Context q,
QDM2SubPNode node 
)
static

Process subpacket 9, init quantized_coeffs with data from it.

Parameters
qcontext
nodepointer to node with packet

Definition at line 979 of file qdm2.c.

Referenced by process_synthesis_subpackets().

◆ process_subpacket_10()

static void process_subpacket_10 ( QDM2Context q,
QDM2SubPNode node 
)
static

Process subpacket 10 if not null, else.

Parameters
qcontext
nodepointer to node with packet

Definition at line 1021 of file qdm2.c.

Referenced by process_synthesis_subpackets(), and qdm2_decode_super_block().

◆ process_subpacket_11()

static void process_subpacket_11 ( QDM2Context q,
QDM2SubPNode node 
)
static

Process subpacket 11.

Parameters
qcontext
nodepointer to node with packet

Definition at line 1040 of file qdm2.c.

Referenced by process_synthesis_subpackets(), and qdm2_decode_super_block().

◆ process_subpacket_12()

static void process_subpacket_12 ( QDM2Context q,
QDM2SubPNode node 
)
static

Process subpacket 12.

Parameters
qcontext
nodepointer to node with packet

Definition at line 1069 of file qdm2.c.

Referenced by process_synthesis_subpackets(), and qdm2_decode_super_block().

◆ process_synthesis_subpackets()

static void process_synthesis_subpackets ( QDM2Context q,
QDM2SubPNode list 
)
static

Process new subpackets for synthesis filter.

Parameters
qcontext
listlist with synthesis filter packets (list D)

Definition at line 1088 of file qdm2.c.

Referenced by qdm2_decode_super_block().

◆ qdm2_decode_super_block()

static void qdm2_decode_super_block ( QDM2Context q)
static

Decode superblock, fill packet lists.

Parameters
qcontext

Definition at line 1120 of file qdm2.c.

Referenced by qdm2_decode().

◆ qdm2_fft_init_coefficient()

static void qdm2_fft_init_coefficient ( QDM2Context q,
int  sub_packet,
int  offset,
int  duration,
int  channel,
int  exp,
int  phase 
)
static

Definition at line 1244 of file qdm2.c.

Referenced by qdm2_fft_decode_tones().

◆ qdm2_fft_decode_tones()

static void qdm2_fft_decode_tones ( QDM2Context q,
int  duration,
GetBitContext gb,
int  b 
)
static

Definition at line 1260 of file qdm2.c.

Referenced by qdm2_decode_fft_packets().

◆ qdm2_decode_fft_packets()

static void qdm2_decode_fft_packets ( QDM2Context q)
static

Definition at line 1353 of file qdm2.c.

Referenced by qdm2_decode().

◆ qdm2_fft_generate_tone()

static void qdm2_fft_generate_tone ( QDM2Context q,
FFTTone tone 
)
static

Definition at line 1427 of file qdm2.c.

Referenced by qdm2_fft_tone_synthesizer().

◆ qdm2_fft_tone_synthesizer()

static void qdm2_fft_tone_synthesizer ( QDM2Context q,
int  sub_packet 
)
static

Definition at line 1473 of file qdm2.c.

Referenced by qdm2_decode().

◆ qdm2_calculate_fft()

static void qdm2_calculate_fft ( QDM2Context q,
int  channel,
int  sub_packet 
)
static

Definition at line 1544 of file qdm2.c.

Referenced by qdm2_decode().

◆ qdm2_synthesis_filter()

static void qdm2_synthesis_filter ( QDM2Context q,
int  index 
)
static
Parameters
qcontext
indexsubpacket number

Definition at line 1564 of file qdm2.c.

Referenced by qdm2_decode().

◆ qdm2_init_static_data()

static av_cold void qdm2_init_static_data ( void  )
static

Init static data (does not depend on specific file)

Definition at line 1600 of file qdm2.c.

Referenced by qdm2_decode_init().

◆ qdm2_decode_init()

static av_cold int qdm2_decode_init ( AVCodecContext avctx)
static

Init parameters from codec extradata.

Definition at line 1612 of file qdm2.c.

◆ qdm2_decode_close()

static av_cold int qdm2_decode_close ( AVCodecContext avctx)
static

Definition at line 1769 of file qdm2.c.

◆ qdm2_decode()

static int qdm2_decode ( QDM2Context q,
const uint8_t *  in,
int16_t *  out 
)
static

Definition at line 1778 of file qdm2.c.

Referenced by qdm2_decode_frame().

◆ qdm2_decode_frame()

static int qdm2_decode_frame ( AVCodecContext avctx,
void *  data,
int got_frame_ptr,
AVPacket avpkt 
)
static

Definition at line 1840 of file qdm2.c.

Variable Documentation

◆ switchtable

const int switchtable[23]
static
Initial value:
= {
0, 5, 1, 5, 5, 5, 5, 5, 2, 5, 5, 5, 5, 5, 5, 5, 3, 5, 5, 5, 5, 5, 4
}

Definition at line 199 of file qdm2.c.

Referenced by fix_coding_method_array().

◆ ff_qdm2_decoder

const AVCodec ff_qdm2_decoder
Initial value:
= {
.name = "qdm2",
.long_name = NULL_IF_CONFIG_SMALL("QDesign Music Codec 2"),
.priv_data_size = sizeof(QDM2Context),
.caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
}

Definition at line 1872 of file qdm2.c.

FF_CODEC_CAP_INIT_THREADSAFE
#define FF_CODEC_CAP_INIT_THREADSAFE
The codec does not modify any global variables in the init function, allowing to call the init functi...
Definition: internal.h:42
qdm2_decode_frame
static int qdm2_decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt)
Definition: qdm2.c:1840
init
static int init
Definition: av_tx.c:47
decode
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:71
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
NULL
#define NULL
Definition: coverity.c:32
qdm2_decode_close
static av_cold int qdm2_decode_close(AVCodecContext *avctx)
Definition: qdm2.c:1769
list
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining list
Definition: filter_design.txt:25
AV_CODEC_ID_QDM2
@ AV_CODEC_ID_QDM2
Definition: codec_id.h:442
AV_CODEC_CAP_CHANNEL_CONF
#define AV_CODEC_CAP_CHANNEL_CONF
Codec should fill in channel configuration and samplerate instead of container.
Definition: codec.h:109
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
size
int size
Definition: twinvq_data.h:10344
qdm2_decode_init
static av_cold int qdm2_decode_init(AVCodecContext *avctx)
Init parameters from codec extradata.
Definition: qdm2.c:1612
QDM2Context
QDM2 decoder context.
Definition: qdm2.c:130