FFmpeg
aacps_common.c
Go to the documentation of this file.
1 /*
2  * Functions common to fixed/float MPEG-4 Parametric Stereo decoding
3  * Copyright (c) 2010 Alex Converse <alex.converse@gmail.com>
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include <stdint.h>
23 #include "libavutil/common.h"
24 #include "aacps.h"
25 #include "get_bits.h"
26 #include "aacpsdata.c"
27 
28 static const int8_t num_env_tab[2][4] = {
29  { 0, 1, 2, 4, },
30  { 1, 2, 3, 4, },
31 };
32 
33 static const int8_t nr_iidicc_par_tab[] = {
34  10, 20, 34, 10, 20, 34,
35 };
36 
37 static const int8_t nr_iidopd_par_tab[] = {
38  5, 11, 17, 5, 11, 17,
39 };
40 
41 enum {
52 };
53 
54 static const int huff_iid[] = {
59 };
60 
61 static const VLCElem *vlc_ps[10];
62 
63 #define READ_PAR_DATA(PAR, MASK, ERR_CONDITION, NB_BITS, MAX_DEPTH) \
64 /** \
65  * Read Inter-channel Intensity Difference/Inter-Channel Coherence/ \
66  * Inter-channel Phase Difference/Overall Phase Difference parameters from the \
67  * bitstream. \
68  * \
69  * @param logctx a context for logging \
70  * @param gb pointer to the input bitstream \
71  * @param ps pointer to the Parametric Stereo context \
72  * @param PAR pointer to the parameter to be read \
73  * @param e envelope to decode \
74  * @param dt 1: time delta-coded, 0: frequency delta-coded \
75  */ \
76 static int read_ ## PAR ## _data(void *logctx, GetBitContext *gb, PSCommonContext *ps, \
77  int8_t (*PAR)[PS_MAX_NR_IIDICC], int table_idx, int e, int dt) \
78 { \
79  int b, num = ps->nr_ ## PAR ## _par; \
80  const VLCElem *vlc_table = vlc_ps[table_idx]; \
81  if (dt) { \
82  int e_prev = e ? e - 1 : ps->num_env_old - 1; \
83  e_prev = FFMAX(e_prev, 0); \
84  for (b = 0; b < num; b++) { \
85  int val = PAR[e_prev][b] + get_vlc2(gb, vlc_table, NB_BITS, MAX_DEPTH); \
86  if (MASK) val &= MASK; \
87  PAR[e][b] = val; \
88  if (ERR_CONDITION) \
89  goto err; \
90  } \
91  } else { \
92  int val = 0; \
93  for (b = 0; b < num; b++) { \
94  val += get_vlc2(gb, vlc_table, NB_BITS, MAX_DEPTH); \
95  if (MASK) val &= MASK; \
96  PAR[e][b] = val; \
97  if (ERR_CONDITION) \
98  goto err; \
99  } \
100  } \
101  return 0; \
102 err: \
103  av_log(logctx, AV_LOG_ERROR, "illegal "#PAR"\n"); \
104  return AVERROR_INVALIDDATA; \
105 }
106 
107 READ_PAR_DATA(iid, 0, FFABS(ps->iid_par[e][b]) > 7 + 8 * ps->iid_quant, 9, 3)
108 READ_PAR_DATA(icc, 0, ps->icc_par[e][b] > 7U, 9, 2)
109 READ_PAR_DATA(ipdopd, 0x07, 0, 5, 1)
110 
112  int ps_extension_id)
113 {
114  int e;
115  int count = get_bits_count(gb);
116 
117  if (ps_extension_id)
118  return 0;
119 
120  ps->enable_ipdopd = get_bits1(gb);
121  if (ps->enable_ipdopd) {
122  for (e = 0; e < ps->num_env; e++) {
123  int dt = get_bits1(gb);
124  read_ipdopd_data(NULL, gb, ps, ps->ipd_par, dt ? huff_ipd_dt : huff_ipd_df, e, dt);
125  dt = get_bits1(gb);
126  read_ipdopd_data(NULL, gb, ps, ps->opd_par, dt ? huff_opd_dt : huff_opd_df, e, dt);
127  }
128  }
129  skip_bits1(gb); //reserved_ps
130  return get_bits_count(gb) - count;
131 }
132 
133 int ff_ps_read_data(void *logctx, GetBitContext *gb_host,
134  PSCommonContext *ps, int bits_left)
135 {
136  int e;
137  int bit_count_start = get_bits_count(gb_host);
138  int header;
139  int bits_consumed;
140  GetBitContext gbc = *gb_host, *gb = &gbc;
141 
142  header = get_bits1(gb);
143  if (header) { //enable_ps_header
144  ps->enable_iid = get_bits1(gb);
145  if (ps->enable_iid) {
146  int iid_mode = get_bits(gb, 3);
147  if (iid_mode > 5) {
148  av_log(logctx, AV_LOG_ERROR, "iid_mode %d is reserved.\n",
149  iid_mode);
150  goto err;
151  }
152  ps->nr_iid_par = nr_iidicc_par_tab[iid_mode];
153  ps->iid_quant = iid_mode > 2;
154  ps->nr_ipdopd_par = nr_iidopd_par_tab[iid_mode];
155  }
156  ps->enable_icc = get_bits1(gb);
157  if (ps->enable_icc) {
158  ps->icc_mode = get_bits(gb, 3);
159  if (ps->icc_mode > 5) {
160  av_log(logctx, AV_LOG_ERROR, "icc_mode %d is reserved.\n",
161  ps->icc_mode);
162  goto err;
163  }
165  }
166  ps->enable_ext = get_bits1(gb);
167  }
168 
169  ps->frame_class = get_bits1(gb);
170  ps->num_env_old = ps->num_env;
171  ps->num_env = num_env_tab[ps->frame_class][get_bits(gb, 2)];
172 
173  ps->border_position[0] = -1;
174  if (ps->frame_class) {
175  for (e = 1; e <= ps->num_env; e++) {
176  ps->border_position[e] = get_bits(gb, 5);
177  if (ps->border_position[e] < ps->border_position[e-1]) {
178  av_log(logctx, AV_LOG_ERROR, "border_position non monotone.\n");
179  goto err;
180  }
181  }
182  } else
183  for (e = 1; e <= ps->num_env; e++)
184  ps->border_position[e] = (e * numQMFSlots >> ff_log2_tab[ps->num_env]) - 1;
185 
186  if (ps->enable_iid) {
187  for (e = 0; e < ps->num_env; e++) {
188  int dt = get_bits1(gb);
189  if (read_iid_data(logctx, gb, ps, ps->iid_par, huff_iid[2*dt+ps->iid_quant], e, dt))
190  goto err;
191  }
192  } else
193  memset(ps->iid_par, 0, sizeof(ps->iid_par));
194 
195  if (ps->enable_icc)
196  for (e = 0; e < ps->num_env; e++) {
197  int dt = get_bits1(gb);
198  if (read_icc_data(logctx, gb, ps, ps->icc_par, dt ? huff_icc_dt : huff_icc_df, e, dt))
199  goto err;
200  }
201  else
202  memset(ps->icc_par, 0, sizeof(ps->icc_par));
203 
204  if (ps->enable_ext) {
205  int cnt = get_bits(gb, 4);
206  if (cnt == 15) {
207  cnt += get_bits(gb, 8);
208  }
209  cnt *= 8;
210  while (cnt > 7) {
211  int ps_extension_id = get_bits(gb, 2);
212  cnt -= 2 + ps_read_extension_data(gb, ps, ps_extension_id);
213  }
214  if (cnt < 0) {
215  av_log(logctx, AV_LOG_ERROR, "ps extension overflow %d\n", cnt);
216  goto err;
217  }
218  skip_bits(gb, cnt);
219  }
220 
221  ps->enable_ipdopd &= !PS_BASELINE;
222 
223  //Fix up envelopes
224  if (!ps->num_env || ps->border_position[ps->num_env] < numQMFSlots - 1) {
225  //Create a fake envelope
226  int source = ps->num_env ? ps->num_env - 1 : ps->num_env_old - 1;
227  int b;
228  if (source >= 0 && source != ps->num_env) {
229  if (ps->enable_iid) {
230  memcpy(ps->iid_par+ps->num_env, ps->iid_par+source, sizeof(ps->iid_par[0]));
231  }
232  if (ps->enable_icc) {
233  memcpy(ps->icc_par+ps->num_env, ps->icc_par+source, sizeof(ps->icc_par[0]));
234  }
235  if (ps->enable_ipdopd) {
236  memcpy(ps->ipd_par+ps->num_env, ps->ipd_par+source, sizeof(ps->ipd_par[0]));
237  memcpy(ps->opd_par+ps->num_env, ps->opd_par+source, sizeof(ps->opd_par[0]));
238  }
239  }
240  if (ps->enable_iid){
241  for (b = 0; b < ps->nr_iid_par; b++) {
242  if (FFABS(ps->iid_par[ps->num_env][b]) > 7 + 8 * ps->iid_quant) {
243  av_log(logctx, AV_LOG_ERROR, "iid_par invalid\n");
244  goto err;
245  }
246  }
247  }
248  if (ps->enable_icc){
249  for (b = 0; b < ps->nr_iid_par; b++) {
250  if (ps->icc_par[ps->num_env][b] > 7U) {
251  av_log(logctx, AV_LOG_ERROR, "icc_par invalid\n");
252  goto err;
253  }
254  }
255  }
256  ps->num_env++;
257  ps->border_position[ps->num_env] = numQMFSlots - 1;
258  }
259 
260 
261  ps->is34bands_old = ps->is34bands;
262  if (!PS_BASELINE && (ps->enable_iid || ps->enable_icc))
263  ps->is34bands = (ps->enable_iid && ps->nr_iid_par == 34) ||
264  (ps->enable_icc && ps->nr_icc_par == 34);
265 
266  //Baseline
267  if (!ps->enable_ipdopd) {
268  memset(ps->ipd_par, 0, sizeof(ps->ipd_par));
269  memset(ps->opd_par, 0, sizeof(ps->opd_par));
270  }
271 
272  if (header)
273  ps->start = 1;
274 
275  bits_consumed = get_bits_count(gb) - bit_count_start;
276  if (bits_consumed <= bits_left) {
277  skip_bits_long(gb_host, bits_consumed);
278  return bits_consumed;
279  }
280  av_log(logctx, AV_LOG_ERROR, "Expected to read %d PS bits actually read %d.\n", bits_left, bits_consumed);
281 err:
282  ps->start = 0;
283  skip_bits_long(gb_host, bits_left);
284  memset(ps->iid_par, 0, sizeof(ps->iid_par));
285  memset(ps->icc_par, 0, sizeof(ps->icc_par));
286  memset(ps->ipd_par, 0, sizeof(ps->ipd_par));
287  memset(ps->opd_par, 0, sizeof(ps->opd_par));
288  return bits_left;
289 }
290 
291 av_cold void ff_ps_init_common(void)
292 {
293  static VLCElem vlc_buf[(1544 + 832 + 1024 + 1036) +
294  (544 + 544) + (32 + 32 + 32 + 32)];
295  VLCInitState state = VLC_INIT_STATE(vlc_buf);
296  const uint8_t (*tab)[2] = aacps_huff_tabs;
297 
298  for (int i = 0; i < FF_ARRAY_ELEMS(vlc_ps); i++) {
299  vlc_ps[i] =
301  &tab[0][1], 2,
302  &tab[0][0], 2, 1,
303  huff_offset[i], 0);
304  tab += huff_sizes[i];
305  }
306 }
skip_bits_long
static void skip_bits_long(GetBitContext *s, int n)
Skips the specified number of bits.
Definition: get_bits.h:278
read_ipdopd_data
static int read_ipdopd_data(void *logctx, GetBitContext *gb, PSCommonContext *ps, int8_t(*ipdopd)[34], int table_idx, int e, int dt)
Definition: aacps_common.c:98
read_iid_data
static int read_iid_data(void *logctx, GetBitContext *gb, PSCommonContext *ps, int8_t(*iid)[34], int table_idx, int e, int dt)
Definition: aacps_common.c:96
get_bits_count
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:266
PSCommonContext
Definition: aacps.h:47
ps_read_extension_data
static int ps_read_extension_data(GetBitContext *gb, PSCommonContext *ps, int ps_extension_id)
Definition: aacps_common.c:100
b
#define b
Definition: input.c:41
huff_sizes
static const uint8_t huff_sizes[]
Definition: aacpsdata.c:24
PSCommonContext::start
int start
Definition: aacps.h:48
PSCommonContext::is34bands
int is34bands
Definition: aacps.h:67
ff_ps_init_common
av_cold void ff_ps_init_common(void)
Definition: aacps_common.c:280
PSCommonContext::border_position
int border_position[PS_MAX_NUM_ENV+1]
Definition: aacps.h:61
PSCommonContext::opd_par
int8_t opd_par[PS_MAX_NUM_ENV][PS_MAX_NR_IIDICC]
Overall Phase Difference Parameters.
Definition: aacps.h:66
huff_iid_dt1
@ huff_iid_dt1
Definition: aacps_common.c:43
skip_bits
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:381
huff_iid_dt0
@ huff_iid_dt0
Definition: aacps_common.c:45
get_bits
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:335
aacps.h
GetBitContext
Definition: get_bits.h:108
tab
static const struct twinvq_data tab
Definition: twinvq_data.h:10345
PSCommonContext::num_env_old
int num_env_old
Definition: aacps.h:58
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
av_cold
#define av_cold
Definition: attributes.h:90
VLCInitState
For static VLCs, the number of bits can often be hardcoded at each get_vlc2() callsite.
Definition: vlc.h:209
PSCommonContext::num_env
int num_env
Definition: aacps.h:59
PSCommonContext::enable_ipdopd
int enable_ipdopd
Definition: aacps.h:60
huff_ipd_dt
@ huff_ipd_dt
Definition: aacps_common.c:49
PSCommonContext::nr_ipdopd_par
int nr_ipdopd_par
Definition: aacps.h:52
get_bits.h
FFABS
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:72
huff_opd_df
@ huff_opd_df
Definition: aacps_common.c:50
huff_offset
static const int8_t huff_offset[]
Definition: aacpsdata.c:91
NULL
#define NULL
Definition: coverity.c:32
bits_left
#define bits_left
Definition: bitstream.h:114
get_bits1
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:388
state
static struct @385 state
ff_log2_tab
const uint8_t ff_log2_tab[256]
Definition: log2_tab.c:23
PSCommonContext::nr_icc_par
int nr_icc_par
Definition: aacps.h:55
source
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a source
Definition: filter_design.txt:255
nr_iidopd_par_tab
static const int8_t nr_iidopd_par_tab[]
Definition: aacps_common.c:37
READ_PAR_DATA
#define READ_PAR_DATA(PAR, MASK, ERR_CONDITION, NB_BITS, MAX_DEPTH)
Definition: aacps_common.c:63
huff_icc_dt
@ huff_icc_dt
Definition: aacps_common.c:47
huff_iid_df0
@ huff_iid_df0
Definition: aacps_common.c:44
huff_opd_dt
@ huff_opd_dt
Definition: aacps_common.c:51
VLCElem
Definition: vlc.h:32
huff_ipd_df
@ huff_ipd_df
Definition: aacps_common.c:48
PSCommonContext::nr_iid_par
int nr_iid_par
Definition: aacps.h:51
header
static const uint8_t header[24]
Definition: sdr2.c:68
huff_iid
static const int huff_iid[]
Definition: aacps_common.c:54
skip_bits1
static void skip_bits1(GetBitContext *s)
Definition: get_bits.h:413
aacps_huff_tabs
static const uint8_t aacps_huff_tabs[][2]
Definition: aacpsdata.c:26
huff_icc_df
@ huff_icc_df
Definition: aacps_common.c:46
ff_ps_read_data
int ff_ps_read_data(void *logctx, GetBitContext *gb_host, PSCommonContext *ps, int bits_left)
Definition: aacps_common.c:122
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:255
PSCommonContext::is34bands_old
int is34bands_old
Definition: aacps.h:68
common.h
PSCommonContext::ipd_par
int8_t ipd_par[PS_MAX_NUM_ENV][PS_MAX_NR_IIDICC]
Inter-channel Phase Difference Parameters.
Definition: aacps.h:65
read_icc_data
static int read_icc_data(void *logctx, GetBitContext *gb, PSCommonContext *ps, int8_t(*icc)[34], int table_idx, int e, int dt)
Definition: aacps_common.c:97
aacpsdata.c
PSCommonContext::enable_iid
int enable_iid
Definition: aacps.h:49
U
#define U(x)
Definition: vpx_arith.h:37
PSCommonContext::enable_ext
int enable_ext
Definition: aacps.h:56
PSCommonContext::iid_par
int8_t iid_par[PS_MAX_NUM_ENV][PS_MAX_NR_IIDICC]
Inter-channel Intensity Difference Parameters.
Definition: aacps.h:62
ff_vlc_init_tables_from_lengths
const av_cold VLCElem * ff_vlc_init_tables_from_lengths(VLCInitState *state, int nb_bits, int nb_codes, const int8_t *lens, int lens_wrap, const void *symbols, int symbols_wrap, int symbols_size, int offset, int flags)
Definition: vlc.c:366
VLC_INIT_STATE
#define VLC_INIT_STATE(_table)
Definition: vlc.h:214
num_env_tab
static const int8_t num_env_tab[2][4]
Definition: aacps_common.c:28
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
PSCommonContext::enable_icc
int enable_icc
Definition: aacps.h:53
PSCommonContext::icc_mode
int icc_mode
Definition: aacps.h:54
PS_BASELINE
#define PS_BASELINE
Operate in Baseline PS mode.
Definition: aacps.h:41
vlc_ps
static const VLCElem * vlc_ps[10]
Definition: aacps_common.c:61
PSCommonContext::iid_quant
int iid_quant
Definition: aacps.h:50
PSCommonContext::icc_par
int8_t icc_par[PS_MAX_NUM_ENV][PS_MAX_NR_IIDICC]
Inter-Channel Coherence Parameters.
Definition: aacps.h:63
numQMFSlots
#define numQMFSlots
Definition: aacps.h:45
nr_iidicc_par_tab
static const int8_t nr_iidicc_par_tab[]
Definition: aacps_common.c:33
PSCommonContext::frame_class
int frame_class
Definition: aacps.h:57
huff_iid_df1
@ huff_iid_df1
Definition: aacps_common.c:42