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 "libavutil/thread.h"
25 #include "aacps.h"
26 #include "get_bits.h"
27 #include "aacpsdata.c"
28 
29 static const int8_t num_env_tab[2][4] = {
30  { 0, 1, 2, 4, },
31  { 1, 2, 3, 4, },
32 };
33 
34 static const int8_t nr_iidicc_par_tab[] = {
35  10, 20, 34, 10, 20, 34,
36 };
37 
38 static const int8_t nr_iidopd_par_tab[] = {
39  5, 11, 17, 5, 11, 17,
40 };
41 
42 enum {
53 };
54 
55 static const int huff_iid[] = {
60 };
61 
62 static VLC vlc_ps[10];
63 
64 #define READ_PAR_DATA(PAR, OFFSET, MASK, ERR_CONDITION, NB_BITS, MAX_DEPTH) \
65 /** \
66  * Read Inter-channel Intensity Difference/Inter-Channel Coherence/ \
67  * Inter-channel Phase Difference/Overall Phase Difference parameters from the \
68  * bitstream. \
69  * \
70  * @param avctx contains the current codec context \
71  * @param gb pointer to the input bitstream \
72  * @param ps pointer to the Parametric Stereo context \
73  * @param PAR pointer to the parameter to be read \
74  * @param e envelope to decode \
75  * @param dt 1: time delta-coded, 0: frequency delta-coded \
76  */ \
77 static int read_ ## PAR ## _data(AVCodecContext *avctx, GetBitContext *gb, PSCommonContext *ps, \
78  int8_t (*PAR)[PS_MAX_NR_IIDICC], int table_idx, int e, int dt) \
79 { \
80  int b, num = ps->nr_ ## PAR ## _par; \
81  VLC_TYPE (*vlc_table)[2] = vlc_ps[table_idx].table; \
82  if (dt) { \
83  int e_prev = e ? e - 1 : ps->num_env_old - 1; \
84  e_prev = FFMAX(e_prev, 0); \
85  for (b = 0; b < num; b++) { \
86  int val = PAR[e_prev][b] + get_vlc2(gb, vlc_table, NB_BITS, MAX_DEPTH) - OFFSET; \
87  if (MASK) val &= MASK; \
88  PAR[e][b] = val; \
89  if (ERR_CONDITION) \
90  goto err; \
91  } \
92  } else { \
93  int val = 0; \
94  for (b = 0; b < num; b++) { \
95  val += get_vlc2(gb, vlc_table, NB_BITS, MAX_DEPTH) - OFFSET; \
96  if (MASK) val &= MASK; \
97  PAR[e][b] = val; \
98  if (ERR_CONDITION) \
99  goto err; \
100  } \
101  } \
102  return 0; \
103 err: \
104  av_log(avctx, AV_LOG_ERROR, "illegal "#PAR"\n"); \
105  return AVERROR_INVALIDDATA; \
106 }
107 
108 READ_PAR_DATA(iid, huff_offset[table_idx], 0, FFABS(ps->iid_par[e][b]) > 7 + 8 * ps->iid_quant, 9, 3)
109 READ_PAR_DATA(icc, huff_offset[table_idx], 0, ps->icc_par[e][b] > 7U, 9, 2)
110 READ_PAR_DATA(ipdopd, 0, 0x07, 0, 5, 1)
111 
113  int ps_extension_id)
114 {
115  int e;
116  int count = get_bits_count(gb);
117 
118  if (ps_extension_id)
119  return 0;
120 
121  ps->enable_ipdopd = get_bits1(gb);
122  if (ps->enable_ipdopd) {
123  for (e = 0; e < ps->num_env; e++) {
124  int dt = get_bits1(gb);
125  read_ipdopd_data(NULL, gb, ps, ps->ipd_par, dt ? huff_ipd_dt : huff_ipd_df, e, dt);
126  dt = get_bits1(gb);
127  read_ipdopd_data(NULL, gb, ps, ps->opd_par, dt ? huff_opd_dt : huff_opd_df, e, dt);
128  }
129  }
130  skip_bits1(gb); //reserved_ps
131  return get_bits_count(gb) - count;
132 }
133 
135  PSCommonContext *ps, int bits_left)
136 {
137  int e;
138  int bit_count_start = get_bits_count(gb_host);
139  int header;
140  int bits_consumed;
141  GetBitContext gbc = *gb_host, *gb = &gbc;
142 
143  header = get_bits1(gb);
144  if (header) { //enable_ps_header
145  ps->enable_iid = get_bits1(gb);
146  if (ps->enable_iid) {
147  int iid_mode = get_bits(gb, 3);
148  if (iid_mode > 5) {
149  av_log(avctx, AV_LOG_ERROR, "iid_mode %d is reserved.\n",
150  iid_mode);
151  goto err;
152  }
153  ps->nr_iid_par = nr_iidicc_par_tab[iid_mode];
154  ps->iid_quant = iid_mode > 2;
155  ps->nr_ipdopd_par = nr_iidopd_par_tab[iid_mode];
156  }
157  ps->enable_icc = get_bits1(gb);
158  if (ps->enable_icc) {
159  ps->icc_mode = get_bits(gb, 3);
160  if (ps->icc_mode > 5) {
161  av_log(avctx, AV_LOG_ERROR, "icc_mode %d is reserved.\n",
162  ps->icc_mode);
163  goto err;
164  }
166  }
167  ps->enable_ext = get_bits1(gb);
168  }
169 
170  ps->frame_class = get_bits1(gb);
171  ps->num_env_old = ps->num_env;
172  ps->num_env = num_env_tab[ps->frame_class][get_bits(gb, 2)];
173 
174  ps->border_position[0] = -1;
175  if (ps->frame_class) {
176  for (e = 1; e <= ps->num_env; e++) {
177  ps->border_position[e] = get_bits(gb, 5);
178  if (ps->border_position[e] < ps->border_position[e-1]) {
179  av_log(avctx, AV_LOG_ERROR, "border_position non monotone.\n");
180  goto err;
181  }
182  }
183  } else
184  for (e = 1; e <= ps->num_env; e++)
185  ps->border_position[e] = (e * numQMFSlots >> ff_log2_tab[ps->num_env]) - 1;
186 
187  if (ps->enable_iid) {
188  for (e = 0; e < ps->num_env; e++) {
189  int dt = get_bits1(gb);
190  if (read_iid_data(avctx, gb, ps, ps->iid_par, huff_iid[2*dt+ps->iid_quant], e, dt))
191  goto err;
192  }
193  } else
194  memset(ps->iid_par, 0, sizeof(ps->iid_par));
195 
196  if (ps->enable_icc)
197  for (e = 0; e < ps->num_env; e++) {
198  int dt = get_bits1(gb);
199  if (read_icc_data(avctx, gb, ps, ps->icc_par, dt ? huff_icc_dt : huff_icc_df, e, dt))
200  goto err;
201  }
202  else
203  memset(ps->icc_par, 0, sizeof(ps->icc_par));
204 
205  if (ps->enable_ext) {
206  int cnt = get_bits(gb, 4);
207  if (cnt == 15) {
208  cnt += get_bits(gb, 8);
209  }
210  cnt *= 8;
211  while (cnt > 7) {
212  int ps_extension_id = get_bits(gb, 2);
213  cnt -= 2 + ps_read_extension_data(gb, ps, ps_extension_id);
214  }
215  if (cnt < 0) {
216  av_log(avctx, AV_LOG_ERROR, "ps extension overflow %d\n", cnt);
217  goto err;
218  }
219  skip_bits(gb, cnt);
220  }
221 
222  ps->enable_ipdopd &= !PS_BASELINE;
223 
224  //Fix up envelopes
225  if (!ps->num_env || ps->border_position[ps->num_env] < numQMFSlots - 1) {
226  //Create a fake envelope
227  int source = ps->num_env ? ps->num_env - 1 : ps->num_env_old - 1;
228  int b;
229  if (source >= 0 && source != ps->num_env) {
230  if (ps->enable_iid) {
231  memcpy(ps->iid_par+ps->num_env, ps->iid_par+source, sizeof(ps->iid_par[0]));
232  }
233  if (ps->enable_icc) {
234  memcpy(ps->icc_par+ps->num_env, ps->icc_par+source, sizeof(ps->icc_par[0]));
235  }
236  if (ps->enable_ipdopd) {
237  memcpy(ps->ipd_par+ps->num_env, ps->ipd_par+source, sizeof(ps->ipd_par[0]));
238  memcpy(ps->opd_par+ps->num_env, ps->opd_par+source, sizeof(ps->opd_par[0]));
239  }
240  }
241  if (ps->enable_iid){
242  for (b = 0; b < ps->nr_iid_par; b++) {
243  if (FFABS(ps->iid_par[ps->num_env][b]) > 7 + 8 * ps->iid_quant) {
244  av_log(avctx, AV_LOG_ERROR, "iid_par invalid\n");
245  goto err;
246  }
247  }
248  }
249  if (ps->enable_icc){
250  for (b = 0; b < ps->nr_iid_par; b++) {
251  if (ps->icc_par[ps->num_env][b] > 7U) {
252  av_log(avctx, AV_LOG_ERROR, "icc_par invalid\n");
253  goto err;
254  }
255  }
256  }
257  ps->num_env++;
258  ps->border_position[ps->num_env] = numQMFSlots - 1;
259  }
260 
261 
262  ps->is34bands_old = ps->is34bands;
263  if (!PS_BASELINE && (ps->enable_iid || ps->enable_icc))
264  ps->is34bands = (ps->enable_iid && ps->nr_iid_par == 34) ||
265  (ps->enable_icc && ps->nr_icc_par == 34);
266 
267  //Baseline
268  if (!ps->enable_ipdopd) {
269  memset(ps->ipd_par, 0, sizeof(ps->ipd_par));
270  memset(ps->opd_par, 0, sizeof(ps->opd_par));
271  }
272 
273  if (header)
274  ps->start = 1;
275 
276  bits_consumed = get_bits_count(gb) - bit_count_start;
277  if (bits_consumed <= bits_left) {
278  skip_bits_long(gb_host, bits_consumed);
279  return bits_consumed;
280  }
281  av_log(avctx, AV_LOG_ERROR, "Expected to read %d PS bits actually read %d.\n", bits_left, bits_consumed);
282 err:
283  ps->start = 0;
284  skip_bits_long(gb_host, bits_left);
285  memset(ps->iid_par, 0, sizeof(ps->iid_par));
286  memset(ps->icc_par, 0, sizeof(ps->icc_par));
287  memset(ps->ipd_par, 0, sizeof(ps->ipd_par));
288  memset(ps->opd_par, 0, sizeof(ps->opd_par));
289  return bits_left;
290 }
291 
292 #define PS_INIT_VLC_STATIC(num, nb_bits, size) \
293  INIT_VLC_STATIC(&vlc_ps[num], nb_bits, ps_tmp[num].table_size / ps_tmp[num].elem_size, \
294  ps_tmp[num].ps_bits, 1, 1, \
295  ps_tmp[num].ps_codes, ps_tmp[num].elem_size, ps_tmp[num].elem_size, \
296  size);
297 
298 #define PS_VLC_ROW(name) \
299  { name ## _codes, name ## _bits, sizeof(name ## _codes), sizeof(name ## _codes[0]) }
300 
301 static av_cold void ps_init_common(void)
302 {
303  // Syntax initialization
304  static const struct {
305  const void *ps_codes, *ps_bits;
306  const unsigned int table_size, elem_size;
307  } ps_tmp[] = {
318  };
319 
320  PS_INIT_VLC_STATIC(0, 9, 1544);
321  PS_INIT_VLC_STATIC(1, 9, 832);
322  PS_INIT_VLC_STATIC(2, 9, 1024);
323  PS_INIT_VLC_STATIC(3, 9, 1036);
324  PS_INIT_VLC_STATIC(4, 9, 544);
325  PS_INIT_VLC_STATIC(5, 9, 544);
326  PS_INIT_VLC_STATIC(6, 5, 32);
327  PS_INIT_VLC_STATIC(7, 5, 32);
328  PS_INIT_VLC_STATIC(8, 5, 32);
329  PS_INIT_VLC_STATIC(9, 5, 32);
330 }
331 
333 {
334  static AVOnce init_static_once = AV_ONCE_INIT;
335  ff_thread_once(&init_static_once, ps_init_common);
336 }
static int read_ipdopd_data(AVCodecContext *avctx, GetBitContext *gb, PSCommonContext *ps, int8_t(*ipdopd)[34], int table_idx, int e, int dt)
\ * Read Inter-channel Intensity Difference/Inter-Channel Coherence/ \ * Inter-channel Phase Differen...
Definition: aacps_common.c:110
int enable_ipdopd
Definition: aacps.h:61
int icc_mode
Definition: aacps.h:55
#define NULL
Definition: coverity.c:32
av_cold void ff_ps_init_common(void)
Definition: aacps_common.c:332
static const int8_t nr_iidicc_par_tab[]
Definition: aacps_common.c:34
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:379
static void skip_bits_long(GetBitContext *s, int n)
Skips the specified number of bits.
Definition: get_bits.h:291
#define READ_PAR_DATA(PAR, OFFSET, MASK, ERR_CONDITION, NB_BITS, MAX_DEPTH)
Definition: aacps_common.c:64
static const int8_t huff_offset[]
Definition: aacpsdata.c:136
#define PS_INIT_VLC_STATIC(num, nb_bits, size)
Definition: aacps_common.c:292
#define numQMFSlots
Definition: aacps.h:46
int8_t opd_par[PS_MAX_NUM_ENV][PS_MAX_NR_IIDICC]
Overall Phase Difference Parameters.
Definition: aacps.h:67
static int read_iid_data(AVCodecContext *avctx, GetBitContext *gb, PSCommonContext *ps, int8_t(*iid)[34], int table_idx, int e, int dt)
\ * Read Inter-channel Intensity Difference/Inter-Channel Coherence/ \ * Inter-channel Phase Differen...
Definition: aacps_common.c:108
#define av_cold
Definition: attributes.h:88
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:219
int nr_ipdopd_par
Definition: aacps.h:53
bitstream reader API header.
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
static const uint8_t header[24]
Definition: sdr2.c:67
#define AVOnce
Definition: thread.h:172
#define av_log(a,...)
int enable_ext
Definition: aacps.h:57
#define U(x)
Definition: vp56_arith.h:37
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
int is34bands_old
Definition: aacps.h:69
GLsizei count
Definition: opengl_enc.c:108
Definition: vlc.h:26
#define b
Definition: input.c:41
static av_cold void ps_init_common(void)
Definition: aacps_common.c:301
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:72
int nr_icc_par
Definition: aacps.h:56
static int read_icc_data(AVCodecContext *avctx, GetBitContext *gb, PSCommonContext *ps, int8_t(*icc)[34], int table_idx, int e, int dt)
\ * Read Inter-channel Intensity Difference/Inter-Channel Coherence/ \ * Inter-channel Phase Differen...
Definition: aacps_common.c:109
const uint8_t ff_log2_tab[256]
Definition: log2_tab.c:23
int8_t iid_par[PS_MAX_NUM_ENV][PS_MAX_NR_IIDICC]
Inter-channel Intensity Difference Parameters.
Definition: aacps.h:63
int ff_ps_read_data(AVCodecContext *avctx, GetBitContext *gb_host, PSCommonContext *ps, int bits_left)
Definition: aacps_common.c:134
#define AV_ONCE_INIT
Definition: thread.h:173
int frame_class
Definition: aacps.h:58
int is34bands
Definition: aacps.h:68
main external API structure.
Definition: avcodec.h:531
int num_env
Definition: aacps.h:60
static const int huff_iid[]
Definition: aacps_common.c:55
static const int8_t nr_iidopd_par_tab[]
Definition: aacps_common.c:38
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:498
static void skip_bits1(GetBitContext *s)
Definition: get_bits.h:538
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:467
#define PS_VLC_ROW(name)
Definition: aacps_common.c:298
int num_env_old
Definition: aacps.h:59
static const int8_t num_env_tab[2][4]
Definition: aacps_common.c:29
#define PS_BASELINE
Operate in Baseline PS mode.
Definition: aacps.h:42
common internal and external API header
int nr_iid_par
Definition: aacps.h:52
int8_t ipd_par[PS_MAX_NUM_ENV][PS_MAX_NR_IIDICC]
Inter-channel Phase Difference Parameters.
Definition: aacps.h:66
int enable_icc
Definition: aacps.h:54
int8_t icc_par[PS_MAX_NUM_ENV][PS_MAX_NR_IIDICC]
Inter-Channel Coherence Parameters.
Definition: aacps.h:64
static int ff_thread_once(char *control, void(*routine)(void))
Definition: thread.h:175
int iid_quant
Definition: aacps.h:51
static VLC vlc_ps[10]
Definition: aacps_common.c:62
int enable_iid
Definition: aacps.h:50
int border_position[PS_MAX_NUM_ENV+1]
Definition: aacps.h:62
static int ps_read_extension_data(GetBitContext *gb, PSCommonContext *ps, int ps_extension_id)
Definition: aacps_common.c:112