wmaprodec.c
Go to the documentation of this file.
1 /*
2  * Wmapro compatible decoder
3  * Copyright (c) 2007 Baptiste Coudurier, Benjamin Larsson, Ulion
4  * Copyright (c) 2008 - 2011 Sascha Sommer, Benjamin Larsson
5  *
6  * This file is part of Libav.
7  *
8  * Libav is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * Libav is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with Libav; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
89 #include "libavutil/float_dsp.h"
90 #include "libavutil/intfloat.h"
91 #include "libavutil/intreadwrite.h"
92 #include "avcodec.h"
93 #include "internal.h"
94 #include "get_bits.h"
95 #include "put_bits.h"
96 #include "wmaprodata.h"
97 #include "dsputil.h"
98 #include "sinewin.h"
99 #include "wma.h"
100 #include "wma_common.h"
101 
103 #define WMAPRO_MAX_CHANNELS 8
104 #define MAX_SUBFRAMES 32
105 #define MAX_BANDS 29
106 #define MAX_FRAMESIZE 32768
107 
108 #define WMAPRO_BLOCK_MIN_BITS 6
109 #define WMAPRO_BLOCK_MAX_BITS 13
110 #define WMAPRO_BLOCK_MIN_SIZE (1 << WMAPRO_BLOCK_MIN_BITS)
111 #define WMAPRO_BLOCK_MAX_SIZE (1 << WMAPRO_BLOCK_MAX_BITS)
112 #define WMAPRO_BLOCK_SIZES (WMAPRO_BLOCK_MAX_BITS - WMAPRO_BLOCK_MIN_BITS + 1)
113 
114 
115 #define VLCBITS 9
116 #define SCALEVLCBITS 8
117 #define VEC4MAXDEPTH ((HUFF_VEC4_MAXBITS+VLCBITS-1)/VLCBITS)
118 #define VEC2MAXDEPTH ((HUFF_VEC2_MAXBITS+VLCBITS-1)/VLCBITS)
119 #define VEC1MAXDEPTH ((HUFF_VEC1_MAXBITS+VLCBITS-1)/VLCBITS)
120 #define SCALEMAXDEPTH ((HUFF_SCALE_MAXBITS+SCALEVLCBITS-1)/SCALEVLCBITS)
121 #define SCALERLMAXDEPTH ((HUFF_SCALE_RL_MAXBITS+VLCBITS-1)/VLCBITS)
122 
123 static VLC sf_vlc;
124 static VLC sf_rl_vlc;
125 static VLC vec4_vlc;
126 static VLC vec2_vlc;
127 static VLC vec1_vlc;
128 static VLC coef_vlc[2];
129 static float sin64[33];
130 
134 typedef struct {
135  int16_t prev_block_len;
138  uint16_t subframe_len[MAX_SUBFRAMES];
139  uint16_t subframe_offset[MAX_SUBFRAMES];
141  uint16_t decoded_samples;
144  int8_t reuse_sf;
147  int saved_scale_factors[2][MAX_BANDS];
151  float* coeffs;
152  uint16_t num_vec_coeffs;
155 
159 typedef struct {
161  int8_t transform;
162  int8_t transform_band[MAX_BANDS];
163  float decorrelation_matrix[WMAPRO_MAX_CHANNELS*WMAPRO_MAX_CHANNELS];
164  float* channel_data[WMAPRO_MAX_CHANNELS];
166 
170 typedef struct WMAProDecodeCtx {
171  /* generic decoder variables */
182 
183  /* frame size dependent frame information (set during initialization) */
184  uint32_t decode_flags;
188  uint16_t samples_per_frame;
189  uint16_t log2_frame_size;
190  int8_t lfe_channel;
199 
200  /* packet decode state */
210 
211  /* frame decode state */
212  uint32_t frame_num;
216  int8_t skip_frame;
218 
219  /* subframe/block decode state */
220  int16_t subframe_len;
223  int8_t num_bands;
225  int16_t* cur_sfb_offsets;
227  int8_t esc_len;
228 
231 
234 
235 
241 {
242 #define PRINT(a, b) av_log(s->avctx, AV_LOG_DEBUG, " %s = %d\n", a, b);
243 #define PRINT_HEX(a, b) av_log(s->avctx, AV_LOG_DEBUG, " %s = %x\n", a, b);
244 
245  PRINT("ed sample bit depth", s->bits_per_sample);
246  PRINT_HEX("ed decode flags", s->decode_flags);
247  PRINT("samples per frame", s->samples_per_frame);
248  PRINT("log2 frame size", s->log2_frame_size);
249  PRINT("max num subframes", s->max_num_subframes);
250  PRINT("len prefix", s->len_prefix);
251  PRINT("num channels", s->avctx->channels);
252 }
253 
260 {
261  WMAProDecodeCtx *s = avctx->priv_data;
262  int i;
263 
264  for (i = 0; i < WMAPRO_BLOCK_SIZES; i++)
265  ff_mdct_end(&s->mdct_ctx[i]);
266 
267  return 0;
268 }
269 
276 {
277  WMAProDecodeCtx *s = avctx->priv_data;
278  uint8_t *edata_ptr = avctx->extradata;
279  unsigned int channel_mask;
280  int i, bits;
281  int log2_max_num_subframes;
282  int num_possible_block_sizes;
283 
284  if (!avctx->block_align) {
285  av_log(avctx, AV_LOG_ERROR, "block_align is not set\n");
286  return AVERROR(EINVAL);
287  }
288 
289  s->avctx = avctx;
290  ff_dsputil_init(&s->dsp, avctx);
292 
294 
296 
297  if (avctx->extradata_size >= 18) {
298  s->decode_flags = AV_RL16(edata_ptr+14);
299  channel_mask = AV_RL32(edata_ptr+2);
300  s->bits_per_sample = AV_RL16(edata_ptr);
302  for (i = 0; i < avctx->extradata_size; i++)
303  av_dlog(avctx, "[%x] ", avctx->extradata[i]);
304  av_dlog(avctx, "\n");
305 
306  } else {
307  av_log_ask_for_sample(avctx, "Unknown extradata size\n");
308  return AVERROR_PATCHWELCOME;
309  }
310 
312  s->log2_frame_size = av_log2(avctx->block_align) + 4;
313 
315  s->skip_frame = 1; /* skip first frame */
316  s->packet_loss = 1;
317  s->len_prefix = (s->decode_flags & 0x40);
318 
320  bits = ff_wma_get_frame_len_bits(avctx->sample_rate, 3, s->decode_flags);
321  if (bits > WMAPRO_BLOCK_MAX_BITS) {
322  av_log_missing_feature(avctx, "14-bits block sizes", 1);
323  return AVERROR_PATCHWELCOME;
324  }
325  s->samples_per_frame = 1 << bits;
326 
328  log2_max_num_subframes = ((s->decode_flags & 0x38) >> 3);
329  s->max_num_subframes = 1 << log2_max_num_subframes;
330  if (s->max_num_subframes == 16 || s->max_num_subframes == 4)
331  s->max_subframe_len_bit = 1;
332  s->subframe_len_bits = av_log2(log2_max_num_subframes) + 1;
333 
334  num_possible_block_sizes = log2_max_num_subframes + 1;
336  s->dynamic_range_compression = (s->decode_flags & 0x80);
337 
338  if (s->max_num_subframes > MAX_SUBFRAMES) {
339  av_log(avctx, AV_LOG_ERROR, "invalid number of subframes %i\n",
340  s->max_num_subframes);
341  return AVERROR_INVALIDDATA;
342  }
343 
345  av_log(avctx, AV_LOG_ERROR, "Invalid minimum block size %i\n",
346  s->max_num_subframes);
347  return AVERROR_INVALIDDATA;
348  }
349 
350  if (s->avctx->sample_rate <= 0) {
351  av_log(avctx, AV_LOG_ERROR, "invalid sample rate\n");
352  return AVERROR_INVALIDDATA;
353  }
354 
355  if (avctx->channels < 0) {
356  av_log(avctx, AV_LOG_ERROR, "invalid number of channels %d\n",
357  avctx->channels);
358  return AVERROR_INVALIDDATA;
359  } else if (avctx->channels > WMAPRO_MAX_CHANNELS) {
360  av_log_ask_for_sample(avctx, "unsupported number of channels\n");
361  return AVERROR_PATCHWELCOME;
362  }
363 
365  for (i = 0; i < avctx->channels; i++)
367 
369  s->lfe_channel = -1;
370 
371  if (channel_mask & 8) {
372  unsigned int mask;
373  for (mask = 1; mask < 16; mask <<= 1) {
374  if (channel_mask & mask)
375  ++s->lfe_channel;
376  }
377  }
378 
380  scale_huffbits, 1, 1,
381  scale_huffcodes, 2, 2, 616);
382 
384  scale_rl_huffbits, 1, 1,
385  scale_rl_huffcodes, 4, 4, 1406);
386 
387  INIT_VLC_STATIC(&coef_vlc[0], VLCBITS, HUFF_COEF0_SIZE,
388  coef0_huffbits, 1, 1,
389  coef0_huffcodes, 4, 4, 2108);
390 
391  INIT_VLC_STATIC(&coef_vlc[1], VLCBITS, HUFF_COEF1_SIZE,
392  coef1_huffbits, 1, 1,
393  coef1_huffcodes, 4, 4, 3912);
394 
396  vec4_huffbits, 1, 1,
397  vec4_huffcodes, 2, 2, 604);
398 
400  vec2_huffbits, 1, 1,
401  vec2_huffcodes, 2, 2, 562);
402 
404  vec1_huffbits, 1, 1,
405  vec1_huffcodes, 2, 2, 562);
406 
409  for (i = 0; i < num_possible_block_sizes; i++) {
410  int subframe_len = s->samples_per_frame >> i;
411  int x;
412  int band = 1;
413 
414  s->sfb_offsets[i][0] = 0;
415 
416  for (x = 0; x < MAX_BANDS-1 && s->sfb_offsets[i][band - 1] < subframe_len; x++) {
417  int offset = (subframe_len * 2 * critical_freq[x])
418  / s->avctx->sample_rate + 2;
419  offset &= ~3;
420  if (offset > s->sfb_offsets[i][band - 1])
421  s->sfb_offsets[i][band++] = offset;
422  }
423  s->sfb_offsets[i][band - 1] = subframe_len;
424  s->num_sfb[i] = band - 1;
425  }
426 
427 
433  for (i = 0; i < num_possible_block_sizes; i++) {
434  int b;
435  for (b = 0; b < s->num_sfb[i]; b++) {
436  int x;
437  int offset = ((s->sfb_offsets[i][b]
438  + s->sfb_offsets[i][b + 1] - 1) << i) >> 1;
439  for (x = 0; x < num_possible_block_sizes; x++) {
440  int v = 0;
441  while (s->sfb_offsets[x][v + 1] << x < offset)
442  if (++v >= MAX_BANDS)
443  return AVERROR_INVALIDDATA;
444  s->sf_offsets[i][x][b] = v;
445  }
446  }
447  }
448 
450  for (i = 0; i < WMAPRO_BLOCK_SIZES; i++)
452  1.0 / (1 << (WMAPRO_BLOCK_MIN_BITS + i - 1))
453  / (1 << (s->bits_per_sample - 1)));
454 
456  for (i = 0; i < WMAPRO_BLOCK_SIZES; i++) {
457  const int win_idx = WMAPRO_BLOCK_MAX_BITS - i;
458  ff_init_ff_sine_windows(win_idx);
459  s->windows[WMAPRO_BLOCK_SIZES - i - 1] = ff_sine_windows[win_idx];
460  }
461 
463  for (i = 0; i < num_possible_block_sizes; i++) {
464  int block_size = s->samples_per_frame >> i;
465  int cutoff = (440*block_size + 3 * (s->avctx->sample_rate >> 1) - 1)
466  / s->avctx->sample_rate;
467  s->subwoofer_cutoffs[i] = av_clip(cutoff, 4, block_size);
468  }
469 
471  for (i = 0; i < 33; i++)
472  sin64[i] = sin(i*M_PI / 64.0);
473 
474  if (avctx->debug & FF_DEBUG_BITSTREAM)
475  dump_context(s);
476 
477  avctx->channel_layout = channel_mask;
478 
480  avctx->coded_frame = &s->frame;
481 
482  return 0;
483 }
484 
491 static int decode_subframe_length(WMAProDecodeCtx *s, int offset)
492 {
493  int frame_len_shift = 0;
494  int subframe_len;
495 
497  if (offset == s->samples_per_frame - s->min_samples_per_subframe)
498  return s->min_samples_per_subframe;
499 
501  if (s->max_subframe_len_bit) {
502  if (get_bits1(&s->gb))
503  frame_len_shift = 1 + get_bits(&s->gb, s->subframe_len_bits-1);
504  } else
505  frame_len_shift = get_bits(&s->gb, s->subframe_len_bits);
506 
507  subframe_len = s->samples_per_frame >> frame_len_shift;
508 
510  if (subframe_len < s->min_samples_per_subframe ||
511  subframe_len > s->samples_per_frame) {
512  av_log(s->avctx, AV_LOG_ERROR, "broken frame: subframe_len %i\n",
513  subframe_len);
514  return AVERROR_INVALIDDATA;
515  }
516  return subframe_len;
517 }
518 
540 {
541  uint16_t num_samples[WMAPRO_MAX_CHANNELS] = { 0 };
542  uint8_t contains_subframe[WMAPRO_MAX_CHANNELS];
543  int channels_for_cur_subframe = s->avctx->channels;
544  int fixed_channel_layout = 0;
545  int min_channel_len = 0;
546  int c;
547 
548  /* Should never consume more than 3073 bits (256 iterations for the
549  * while loop when always the minimum amount of 128 samples is subtracted
550  * from missing samples in the 8 channel case).
551  * 1 + BLOCK_MAX_SIZE * MAX_CHANNELS / BLOCK_MIN_SIZE * (MAX_CHANNELS + 4)
552  */
553 
555  for (c = 0; c < s->avctx->channels; c++)
556  s->channel[c].num_subframes = 0;
557 
558  if (s->max_num_subframes == 1 || get_bits1(&s->gb))
559  fixed_channel_layout = 1;
560 
562  do {
563  int subframe_len;
564 
566  for (c = 0; c < s->avctx->channels; c++) {
567  if (num_samples[c] == min_channel_len) {
568  if (fixed_channel_layout || channels_for_cur_subframe == 1 ||
569  (min_channel_len == s->samples_per_frame - s->min_samples_per_subframe))
570  contains_subframe[c] = 1;
571  else
572  contains_subframe[c] = get_bits1(&s->gb);
573  } else
574  contains_subframe[c] = 0;
575  }
576 
578  if ((subframe_len = decode_subframe_length(s, min_channel_len)) <= 0)
579  return AVERROR_INVALIDDATA;
580 
582  min_channel_len += subframe_len;
583  for (c = 0; c < s->avctx->channels; c++) {
584  WMAProChannelCtx* chan = &s->channel[c];
585 
586  if (contains_subframe[c]) {
587  if (chan->num_subframes >= MAX_SUBFRAMES) {
589  "broken frame: num subframes > 31\n");
590  return AVERROR_INVALIDDATA;
591  }
592  chan->subframe_len[chan->num_subframes] = subframe_len;
593  num_samples[c] += subframe_len;
594  ++chan->num_subframes;
595  if (num_samples[c] > s->samples_per_frame) {
596  av_log(s->avctx, AV_LOG_ERROR, "broken frame: "
597  "channel len > samples_per_frame\n");
598  return AVERROR_INVALIDDATA;
599  }
600  } else if (num_samples[c] <= min_channel_len) {
601  if (num_samples[c] < min_channel_len) {
602  channels_for_cur_subframe = 0;
603  min_channel_len = num_samples[c];
604  }
605  ++channels_for_cur_subframe;
606  }
607  }
608  } while (min_channel_len < s->samples_per_frame);
609 
610  for (c = 0; c < s->avctx->channels; c++) {
611  int i;
612  int offset = 0;
613  for (i = 0; i < s->channel[c].num_subframes; i++) {
614  av_dlog(s->avctx, "frame[%i] channel[%i] subframe[%i]"
615  " len %i\n", s->frame_num, c, i,
616  s->channel[c].subframe_len[i]);
617  s->channel[c].subframe_offset[i] = offset;
618  offset += s->channel[c].subframe_len[i];
619  }
620  }
621 
622  return 0;
623 }
624 
631  WMAProChannelGrp *chgroup)
632 {
633  int i;
634  int offset = 0;
635  int8_t rotation_offset[WMAPRO_MAX_CHANNELS * WMAPRO_MAX_CHANNELS];
636  memset(chgroup->decorrelation_matrix, 0, s->avctx->channels *
637  s->avctx->channels * sizeof(*chgroup->decorrelation_matrix));
638 
639  for (i = 0; i < chgroup->num_channels * (chgroup->num_channels - 1) >> 1; i++)
640  rotation_offset[i] = get_bits(&s->gb, 6);
641 
642  for (i = 0; i < chgroup->num_channels; i++)
643  chgroup->decorrelation_matrix[chgroup->num_channels * i + i] =
644  get_bits1(&s->gb) ? 1.0 : -1.0;
645 
646  for (i = 1; i < chgroup->num_channels; i++) {
647  int x;
648  for (x = 0; x < i; x++) {
649  int y;
650  for (y = 0; y < i + 1; y++) {
651  float v1 = chgroup->decorrelation_matrix[x * chgroup->num_channels + y];
652  float v2 = chgroup->decorrelation_matrix[i * chgroup->num_channels + y];
653  int n = rotation_offset[offset + x];
654  float sinv;
655  float cosv;
656 
657  if (n < 32) {
658  sinv = sin64[n];
659  cosv = sin64[32 - n];
660  } else {
661  sinv = sin64[64 - n];
662  cosv = -sin64[n - 32];
663  }
664 
665  chgroup->decorrelation_matrix[y + x * chgroup->num_channels] =
666  (v1 * sinv) - (v2 * cosv);
667  chgroup->decorrelation_matrix[y + i * chgroup->num_channels] =
668  (v1 * cosv) + (v2 * sinv);
669  }
670  }
671  offset += i;
672  }
673 }
674 
681 {
682  int i;
683  /* should never consume more than 1921 bits for the 8 channel case
684  * 1 + MAX_CHANNELS * (MAX_CHANNELS + 2 + 3 * MAX_CHANNELS * MAX_CHANNELS
685  * + MAX_CHANNELS + MAX_BANDS + 1)
686  */
687 
689  s->num_chgroups = 0;
690  if (s->avctx->channels > 1) {
691  int remaining_channels = s->channels_for_cur_subframe;
692 
693  if (get_bits1(&s->gb)) {
695  "unsupported channel transform bit\n");
696  return AVERROR_PATCHWELCOME;
697  }
698 
699  for (s->num_chgroups = 0; remaining_channels &&
701  WMAProChannelGrp* chgroup = &s->chgroup[s->num_chgroups];
702  float** channel_data = chgroup->channel_data;
703  chgroup->num_channels = 0;
704  chgroup->transform = 0;
705 
707  if (remaining_channels > 2) {
708  for (i = 0; i < s->channels_for_cur_subframe; i++) {
709  int channel_idx = s->channel_indexes_for_cur_subframe[i];
710  if (!s->channel[channel_idx].grouped
711  && get_bits1(&s->gb)) {
712  ++chgroup->num_channels;
713  s->channel[channel_idx].grouped = 1;
714  *channel_data++ = s->channel[channel_idx].coeffs;
715  }
716  }
717  } else {
718  chgroup->num_channels = remaining_channels;
719  for (i = 0; i < s->channels_for_cur_subframe; i++) {
720  int channel_idx = s->channel_indexes_for_cur_subframe[i];
721  if (!s->channel[channel_idx].grouped)
722  *channel_data++ = s->channel[channel_idx].coeffs;
723  s->channel[channel_idx].grouped = 1;
724  }
725  }
726 
728  if (chgroup->num_channels == 2) {
729  if (get_bits1(&s->gb)) {
730  if (get_bits1(&s->gb)) {
732  "unsupported channel transform type\n");
733  return AVERROR_PATCHWELCOME;
734  }
735  } else {
736  chgroup->transform = 1;
737  if (s->avctx->channels == 2) {
738  chgroup->decorrelation_matrix[0] = 1.0;
739  chgroup->decorrelation_matrix[1] = -1.0;
740  chgroup->decorrelation_matrix[2] = 1.0;
741  chgroup->decorrelation_matrix[3] = 1.0;
742  } else {
744  chgroup->decorrelation_matrix[0] = 0.70703125;
745  chgroup->decorrelation_matrix[1] = -0.70703125;
746  chgroup->decorrelation_matrix[2] = 0.70703125;
747  chgroup->decorrelation_matrix[3] = 0.70703125;
748  }
749  }
750  } else if (chgroup->num_channels > 2) {
751  if (get_bits1(&s->gb)) {
752  chgroup->transform = 1;
753  if (get_bits1(&s->gb)) {
754  decode_decorrelation_matrix(s, chgroup);
755  } else {
757  if (chgroup->num_channels > 6) {
759  "coupled channels > 6\n");
760  } else {
761  memcpy(chgroup->decorrelation_matrix,
763  chgroup->num_channels * chgroup->num_channels *
764  sizeof(*chgroup->decorrelation_matrix));
765  }
766  }
767  }
768  }
769 
771  if (chgroup->transform) {
772  if (!get_bits1(&s->gb)) {
773  int i;
775  for (i = 0; i < s->num_bands; i++) {
776  chgroup->transform_band[i] = get_bits1(&s->gb);
777  }
778  } else {
779  memset(chgroup->transform_band, 1, s->num_bands);
780  }
781  }
782  remaining_channels -= chgroup->num_channels;
783  }
784  }
785  return 0;
786 }
787 
794 static int decode_coeffs(WMAProDecodeCtx *s, int c)
795 {
796  /* Integers 0..15 as single-precision floats. The table saves a
797  costly int to float conversion, and storing the values as
798  integers allows fast sign-flipping. */
799  static const uint32_t fval_tab[16] = {
800  0x00000000, 0x3f800000, 0x40000000, 0x40400000,
801  0x40800000, 0x40a00000, 0x40c00000, 0x40e00000,
802  0x41000000, 0x41100000, 0x41200000, 0x41300000,
803  0x41400000, 0x41500000, 0x41600000, 0x41700000,
804  };
805  int vlctable;
806  VLC* vlc;
807  WMAProChannelCtx* ci = &s->channel[c];
808  int rl_mode = 0;
809  int cur_coeff = 0;
810  int num_zeros = 0;
811  const uint16_t* run;
812  const float* level;
813 
814  av_dlog(s->avctx, "decode coefficients for channel %i\n", c);
815 
816  vlctable = get_bits1(&s->gb);
817  vlc = &coef_vlc[vlctable];
818 
819  if (vlctable) {
820  run = coef1_run;
821  level = coef1_level;
822  } else {
823  run = coef0_run;
824  level = coef0_level;
825  }
826 
829  while ((s->transmit_num_vec_coeffs || !rl_mode) &&
830  (cur_coeff + 3 < ci->num_vec_coeffs)) {
831  uint32_t vals[4];
832  int i;
833  unsigned int idx;
834 
835  idx = get_vlc2(&s->gb, vec4_vlc.table, VLCBITS, VEC4MAXDEPTH);
836 
837  if (idx == HUFF_VEC4_SIZE - 1) {
838  for (i = 0; i < 4; i += 2) {
839  idx = get_vlc2(&s->gb, vec2_vlc.table, VLCBITS, VEC2MAXDEPTH);
840  if (idx == HUFF_VEC2_SIZE - 1) {
841  uint32_t v0, v1;
842  v0 = get_vlc2(&s->gb, vec1_vlc.table, VLCBITS, VEC1MAXDEPTH);
843  if (v0 == HUFF_VEC1_SIZE - 1)
844  v0 += ff_wma_get_large_val(&s->gb);
845  v1 = get_vlc2(&s->gb, vec1_vlc.table, VLCBITS, VEC1MAXDEPTH);
846  if (v1 == HUFF_VEC1_SIZE - 1)
847  v1 += ff_wma_get_large_val(&s->gb);
848  vals[i ] = av_float2int(v0);
849  vals[i+1] = av_float2int(v1);
850  } else {
851  vals[i] = fval_tab[symbol_to_vec2[idx] >> 4 ];
852  vals[i+1] = fval_tab[symbol_to_vec2[idx] & 0xF];
853  }
854  }
855  } else {
856  vals[0] = fval_tab[ symbol_to_vec4[idx] >> 12 ];
857  vals[1] = fval_tab[(symbol_to_vec4[idx] >> 8) & 0xF];
858  vals[2] = fval_tab[(symbol_to_vec4[idx] >> 4) & 0xF];
859  vals[3] = fval_tab[ symbol_to_vec4[idx] & 0xF];
860  }
861 
863  for (i = 0; i < 4; i++) {
864  if (vals[i]) {
865  uint32_t sign = get_bits1(&s->gb) - 1;
866  AV_WN32A(&ci->coeffs[cur_coeff], vals[i] ^ sign << 31);
867  num_zeros = 0;
868  } else {
869  ci->coeffs[cur_coeff] = 0;
872  rl_mode |= (++num_zeros > s->subframe_len >> 8);
873  }
874  ++cur_coeff;
875  }
876  }
877 
879  if (cur_coeff < s->subframe_len) {
880  memset(&ci->coeffs[cur_coeff], 0,
881  sizeof(*ci->coeffs) * (s->subframe_len - cur_coeff));
882  if (ff_wma_run_level_decode(s->avctx, &s->gb, vlc,
883  level, run, 1, ci->coeffs,
884  cur_coeff, s->subframe_len,
885  s->subframe_len, s->esc_len, 0))
886  return AVERROR_INVALIDDATA;
887  }
888 
889  return 0;
890 }
891 
898 {
899  int i;
900 
905  for (i = 0; i < s->channels_for_cur_subframe; i++) {
906  int c = s->channel_indexes_for_cur_subframe[i];
907  int* sf;
908  int* sf_end;
910  sf_end = s->channel[c].scale_factors + s->num_bands;
911 
917  if (s->channel[c].reuse_sf) {
918  const int8_t* sf_offsets = s->sf_offsets[s->table_idx][s->channel[c].table_idx];
919  int b;
920  for (b = 0; b < s->num_bands; b++)
921  s->channel[c].scale_factors[b] =
922  s->channel[c].saved_scale_factors[s->channel[c].scale_factor_idx][*sf_offsets++];
923  }
924 
925  if (!s->channel[c].cur_subframe || get_bits1(&s->gb)) {
926 
927  if (!s->channel[c].reuse_sf) {
928  int val;
930  s->channel[c].scale_factor_step = get_bits(&s->gb, 2) + 1;
931  val = 45 / s->channel[c].scale_factor_step;
932  for (sf = s->channel[c].scale_factors; sf < sf_end; sf++) {
933  val += get_vlc2(&s->gb, sf_vlc.table, SCALEVLCBITS, SCALEMAXDEPTH) - 60;
934  *sf = val;
935  }
936  } else {
937  int i;
939  for (i = 0; i < s->num_bands; i++) {
940  int idx;
941  int skip;
942  int val;
943  int sign;
944 
945  idx = get_vlc2(&s->gb, sf_rl_vlc.table, VLCBITS, SCALERLMAXDEPTH);
946 
947  if (!idx) {
948  uint32_t code = get_bits(&s->gb, 14);
949  val = code >> 6;
950  sign = (code & 1) - 1;
951  skip = (code & 0x3f) >> 1;
952  } else if (idx == 1) {
953  break;
954  } else {
955  skip = scale_rl_run[idx];
956  val = scale_rl_level[idx];
957  sign = get_bits1(&s->gb)-1;
958  }
959 
960  i += skip;
961  if (i >= s->num_bands) {
963  "invalid scale factor coding\n");
964  return AVERROR_INVALIDDATA;
965  }
966  s->channel[c].scale_factors[i] += (val ^ sign) - sign;
967  }
968  }
971  s->channel[c].table_idx = s->table_idx;
972  s->channel[c].reuse_sf = 1;
973  }
974 
977  for (sf = s->channel[c].scale_factors + 1; sf < sf_end; sf++) {
978  s->channel[c].max_scale_factor =
979  FFMAX(s->channel[c].max_scale_factor, *sf);
980  }
981 
982  }
983  return 0;
984 }
985 
991 {
992  int i;
993 
994  for (i = 0; i < s->num_chgroups; i++) {
995  if (s->chgroup[i].transform) {
996  float data[WMAPRO_MAX_CHANNELS];
997  const int num_channels = s->chgroup[i].num_channels;
998  float** ch_data = s->chgroup[i].channel_data;
999  float** ch_end = ch_data + num_channels;
1000  const int8_t* tb = s->chgroup[i].transform_band;
1001  int16_t* sfb;
1002 
1004  for (sfb = s->cur_sfb_offsets;
1005  sfb < s->cur_sfb_offsets + s->num_bands; sfb++) {
1006  int y;
1007  if (*tb++ == 1) {
1009  for (y = sfb[0]; y < FFMIN(sfb[1], s->subframe_len); y++) {
1010  const float* mat = s->chgroup[i].decorrelation_matrix;
1011  const float* data_end = data + num_channels;
1012  float* data_ptr = data;
1013  float** ch;
1014 
1015  for (ch = ch_data; ch < ch_end; ch++)
1016  *data_ptr++ = (*ch)[y];
1017 
1018  for (ch = ch_data; ch < ch_end; ch++) {
1019  float sum = 0;
1020  data_ptr = data;
1021  while (data_ptr < data_end)
1022  sum += *data_ptr++ * *mat++;
1023 
1024  (*ch)[y] = sum;
1025  }
1026  }
1027  } else if (s->avctx->channels == 2) {
1028  int len = FFMIN(sfb[1], s->subframe_len) - sfb[0];
1029  s->fdsp.vector_fmul_scalar(ch_data[0] + sfb[0],
1030  ch_data[0] + sfb[0],
1031  181.0 / 128, len);
1032  s->fdsp.vector_fmul_scalar(ch_data[1] + sfb[0],
1033  ch_data[1] + sfb[0],
1034  181.0 / 128, len);
1035  }
1036  }
1037  }
1038  }
1039 }
1040 
1046 {
1047  int i;
1048  for (i = 0; i < s->channels_for_cur_subframe; i++) {
1049  int c = s->channel_indexes_for_cur_subframe[i];
1050  float* window;
1051  int winlen = s->channel[c].prev_block_len;
1052  float* start = s->channel[c].coeffs - (winlen >> 1);
1053 
1054  if (s->subframe_len < winlen) {
1055  start += (winlen - s->subframe_len) >> 1;
1056  winlen = s->subframe_len;
1057  }
1058 
1059  window = s->windows[av_log2(winlen) - WMAPRO_BLOCK_MIN_BITS];
1060 
1061  winlen >>= 1;
1062 
1063  s->dsp.vector_fmul_window(start, start, start + winlen,
1064  window, winlen);
1065 
1066  s->channel[c].prev_block_len = s->subframe_len;
1067  }
1068 }
1069 
1076 {
1077  int offset = s->samples_per_frame;
1078  int subframe_len = s->samples_per_frame;
1079  int i;
1080  int total_samples = s->samples_per_frame * s->avctx->channels;
1081  int transmit_coeffs = 0;
1082  int cur_subwoofer_cutoff;
1083 
1084  s->subframe_offset = get_bits_count(&s->gb);
1085 
1090  for (i = 0; i < s->avctx->channels; i++) {
1091  s->channel[i].grouped = 0;
1092  if (offset > s->channel[i].decoded_samples) {
1093  offset = s->channel[i].decoded_samples;
1094  subframe_len =
1096  }
1097  }
1098 
1099  av_dlog(s->avctx,
1100  "processing subframe with offset %i len %i\n", offset, subframe_len);
1101 
1104  for (i = 0; i < s->avctx->channels; i++) {
1105  const int cur_subframe = s->channel[i].cur_subframe;
1107  total_samples -= s->channel[i].decoded_samples;
1108 
1110  if (offset == s->channel[i].decoded_samples &&
1111  subframe_len == s->channel[i].subframe_len[cur_subframe]) {
1112  total_samples -= s->channel[i].subframe_len[cur_subframe];
1113  s->channel[i].decoded_samples +=
1114  s->channel[i].subframe_len[cur_subframe];
1117  }
1118  }
1119 
1122  if (!total_samples)
1123  s->parsed_all_subframes = 1;
1124 
1125 
1126  av_dlog(s->avctx, "subframe is part of %i channels\n",
1128 
1130  s->table_idx = av_log2(s->samples_per_frame/subframe_len);
1131  s->num_bands = s->num_sfb[s->table_idx];
1133  cur_subwoofer_cutoff = s->subwoofer_cutoffs[s->table_idx];
1134 
1136  offset += s->samples_per_frame >> 1;
1137 
1138  for (i = 0; i < s->channels_for_cur_subframe; i++) {
1139  int c = s->channel_indexes_for_cur_subframe[i];
1140 
1141  s->channel[c].coeffs = &s->channel[c].out[offset];
1142  }
1143 
1144  s->subframe_len = subframe_len;
1145  s->esc_len = av_log2(s->subframe_len - 1) + 1;
1146 
1148  if (get_bits1(&s->gb)) {
1149  int num_fill_bits;
1150  if (!(num_fill_bits = get_bits(&s->gb, 2))) {
1151  int len = get_bits(&s->gb, 4);
1152  num_fill_bits = get_bits(&s->gb, len) + 1;
1153  }
1154 
1155  if (num_fill_bits >= 0) {
1156  if (get_bits_count(&s->gb) + num_fill_bits > s->num_saved_bits) {
1157  av_log(s->avctx, AV_LOG_ERROR, "invalid number of fill bits\n");
1158  return AVERROR_INVALIDDATA;
1159  }
1160 
1161  skip_bits_long(&s->gb, num_fill_bits);
1162  }
1163  }
1164 
1166  if (get_bits1(&s->gb)) {
1167  av_log_ask_for_sample(s->avctx, "reserved bit set\n");
1168  return AVERROR_PATCHWELCOME;
1169  }
1170 
1171 
1172  if (decode_channel_transform(s) < 0)
1173  return AVERROR_INVALIDDATA;
1174 
1175 
1176  for (i = 0; i < s->channels_for_cur_subframe; i++) {
1177  int c = s->channel_indexes_for_cur_subframe[i];
1178  if ((s->channel[c].transmit_coefs = get_bits1(&s->gb)))
1179  transmit_coeffs = 1;
1180  }
1181 
1182  if (transmit_coeffs) {
1183  int step;
1184  int quant_step = 90 * s->bits_per_sample >> 4;
1185 
1187  if ((s->transmit_num_vec_coeffs = get_bits1(&s->gb))) {
1188  int num_bits = av_log2((s->subframe_len + 3)/4) + 1;
1189  for (i = 0; i < s->channels_for_cur_subframe; i++) {
1190  int c = s->channel_indexes_for_cur_subframe[i];
1191  int num_vec_coeffs = get_bits(&s->gb, num_bits) << 2;
1192  if (num_vec_coeffs + offset > FF_ARRAY_ELEMS(s->channel[c].out)) {
1193  av_log(s->avctx, AV_LOG_ERROR, "num_vec_coeffs %d is too large\n", num_vec_coeffs);
1194  return AVERROR_INVALIDDATA;
1195  }
1196  s->channel[c].num_vec_coeffs = num_vec_coeffs;
1197  }
1198  } else {
1199  for (i = 0; i < s->channels_for_cur_subframe; i++) {
1200  int c = s->channel_indexes_for_cur_subframe[i];
1201  s->channel[c].num_vec_coeffs = s->subframe_len;
1202  }
1203  }
1205  step = get_sbits(&s->gb, 6);
1206  quant_step += step;
1207  if (step == -32 || step == 31) {
1208  const int sign = (step == 31) - 1;
1209  int quant = 0;
1210  while (get_bits_count(&s->gb) + 5 < s->num_saved_bits &&
1211  (step = get_bits(&s->gb, 5)) == 31) {
1212  quant += 31;
1213  }
1214  quant_step += ((quant + step) ^ sign) - sign;
1215  }
1216  if (quant_step < 0) {
1217  av_log(s->avctx, AV_LOG_DEBUG, "negative quant step\n");
1218  }
1219 
1222  if (s->channels_for_cur_subframe == 1) {
1223  s->channel[s->channel_indexes_for_cur_subframe[0]].quant_step = quant_step;
1224  } else {
1225  int modifier_len = get_bits(&s->gb, 3);
1226  for (i = 0; i < s->channels_for_cur_subframe; i++) {
1227  int c = s->channel_indexes_for_cur_subframe[i];
1228  s->channel[c].quant_step = quant_step;
1229  if (get_bits1(&s->gb)) {
1230  if (modifier_len) {
1231  s->channel[c].quant_step += get_bits(&s->gb, modifier_len) + 1;
1232  } else
1233  ++s->channel[c].quant_step;
1234  }
1235  }
1236  }
1237 
1239  if (decode_scale_factors(s) < 0)
1240  return AVERROR_INVALIDDATA;
1241  }
1242 
1243  av_dlog(s->avctx, "BITSTREAM: subframe header length was %i\n",
1244  get_bits_count(&s->gb) - s->subframe_offset);
1245 
1247  for (i = 0; i < s->channels_for_cur_subframe; i++) {
1248  int c = s->channel_indexes_for_cur_subframe[i];
1249  if (s->channel[c].transmit_coefs &&
1250  get_bits_count(&s->gb) < s->num_saved_bits) {
1251  decode_coeffs(s, c);
1252  } else
1253  memset(s->channel[c].coeffs, 0,
1254  sizeof(*s->channel[c].coeffs) * subframe_len);
1255  }
1256 
1257  av_dlog(s->avctx, "BITSTREAM: subframe length was %i\n",
1258  get_bits_count(&s->gb) - s->subframe_offset);
1259 
1260  if (transmit_coeffs) {
1261  FFTContext *mdct = &s->mdct_ctx[av_log2(subframe_len) - WMAPRO_BLOCK_MIN_BITS];
1264  for (i = 0; i < s->channels_for_cur_subframe; i++) {
1265  int c = s->channel_indexes_for_cur_subframe[i];
1266  const int* sf = s->channel[c].scale_factors;
1267  int b;
1268 
1269  if (c == s->lfe_channel)
1270  memset(&s->tmp[cur_subwoofer_cutoff], 0, sizeof(*s->tmp) *
1271  (subframe_len - cur_subwoofer_cutoff));
1272 
1274  for (b = 0; b < s->num_bands; b++) {
1275  const int end = FFMIN(s->cur_sfb_offsets[b+1], s->subframe_len);
1276  const int exp = s->channel[c].quant_step -
1277  (s->channel[c].max_scale_factor - *sf++) *
1278  s->channel[c].scale_factor_step;
1279  const float quant = pow(10.0, exp / 20.0);
1280  int start = s->cur_sfb_offsets[b];
1281  s->fdsp.vector_fmul_scalar(s->tmp + start,
1282  s->channel[c].coeffs + start,
1283  quant, end - start);
1284  }
1285 
1287  mdct->imdct_half(mdct, s->channel[c].coeffs, s->tmp);
1288  }
1289  }
1290 
1292  wmapro_window(s);
1293 
1295  for (i = 0; i < s->channels_for_cur_subframe; i++) {
1296  int c = s->channel_indexes_for_cur_subframe[i];
1297  if (s->channel[c].cur_subframe >= s->channel[c].num_subframes) {
1298  av_log(s->avctx, AV_LOG_ERROR, "broken subframe\n");
1299  return AVERROR_INVALIDDATA;
1300  }
1301  ++s->channel[c].cur_subframe;
1302  }
1303 
1304  return 0;
1305 }
1306 
1313 static int decode_frame(WMAProDecodeCtx *s, int *got_frame_ptr)
1314 {
1315  AVCodecContext *avctx = s->avctx;
1316  GetBitContext* gb = &s->gb;
1317  int more_frames = 0;
1318  int len = 0;
1319  int i, ret;
1320 
1322  if (s->len_prefix)
1323  len = get_bits(gb, s->log2_frame_size);
1324 
1325  av_dlog(s->avctx, "decoding frame with length %x\n", len);
1326 
1328  if (decode_tilehdr(s)) {
1329  s->packet_loss = 1;
1330  return 0;
1331  }
1332 
1334  if (s->avctx->channels > 1 && get_bits1(gb)) {
1335  if (get_bits1(gb)) {
1336  for (i = 0; i < avctx->channels * avctx->channels; i++)
1337  skip_bits(gb, 4);
1338  }
1339  }
1340 
1342  if (s->dynamic_range_compression) {
1343  s->drc_gain = get_bits(gb, 8);
1344  av_dlog(s->avctx, "drc_gain %i\n", s->drc_gain);
1345  }
1346 
1349  if (get_bits1(gb)) {
1350  int av_unused skip;
1351 
1353  if (get_bits1(gb)) {
1354  skip = get_bits(gb, av_log2(s->samples_per_frame * 2));
1355  av_dlog(s->avctx, "start skip: %i\n", skip);
1356  }
1357 
1359  if (get_bits1(gb)) {
1360  skip = get_bits(gb, av_log2(s->samples_per_frame * 2));
1361  av_dlog(s->avctx, "end skip: %i\n", skip);
1362  }
1363 
1364  }
1365 
1366  av_dlog(s->avctx, "BITSTREAM: frame header length was %i\n",
1367  get_bits_count(gb) - s->frame_offset);
1368 
1370  s->parsed_all_subframes = 0;
1371  for (i = 0; i < avctx->channels; i++) {
1372  s->channel[i].decoded_samples = 0;
1373  s->channel[i].cur_subframe = 0;
1374  s->channel[i].reuse_sf = 0;
1375  }
1376 
1378  while (!s->parsed_all_subframes) {
1379  if (decode_subframe(s) < 0) {
1380  s->packet_loss = 1;
1381  return 0;
1382  }
1383  }
1384 
1385  /* get output buffer */
1387  if ((ret = ff_get_buffer(avctx, &s->frame)) < 0) {
1388  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
1389  s->packet_loss = 1;
1390  return 0;
1391  }
1392 
1394  for (i = 0; i < avctx->channels; i++)
1395  memcpy(s->frame.extended_data[i], s->channel[i].out,
1396  s->samples_per_frame * sizeof(*s->channel[i].out));
1397 
1398  for (i = 0; i < avctx->channels; i++) {
1400  memcpy(&s->channel[i].out[0],
1401  &s->channel[i].out[s->samples_per_frame],
1402  s->samples_per_frame * sizeof(*s->channel[i].out) >> 1);
1403  }
1404 
1405  if (s->skip_frame) {
1406  s->skip_frame = 0;
1407  *got_frame_ptr = 0;
1408  } else {
1409  *got_frame_ptr = 1;
1410  }
1411 
1412  if (s->len_prefix) {
1413  if (len != (get_bits_count(gb) - s->frame_offset) + 2) {
1416  "frame[%i] would have to skip %i bits\n", s->frame_num,
1417  len - (get_bits_count(gb) - s->frame_offset) - 1);
1418  s->packet_loss = 1;
1419  return 0;
1420  }
1421 
1423  skip_bits_long(gb, len - (get_bits_count(gb) - s->frame_offset) - 1);
1424  } else {
1425  while (get_bits_count(gb) < s->num_saved_bits && get_bits1(gb) == 0) {
1426  }
1427  }
1428 
1430  more_frames = get_bits1(gb);
1431 
1432  ++s->frame_num;
1433  return more_frames;
1434 }
1435 
1443 {
1444  return s->buf_bit_size - get_bits_count(gb);
1445 }
1446 
1454 static void save_bits(WMAProDecodeCtx *s, GetBitContext* gb, int len,
1455  int append)
1456 {
1457  int buflen;
1458 
1463  if (!append) {
1464  s->frame_offset = get_bits_count(gb) & 7;
1465  s->num_saved_bits = s->frame_offset;
1467  }
1468 
1469  buflen = (s->num_saved_bits + len + 8) >> 3;
1470 
1471  if (len <= 0 || buflen > MAX_FRAMESIZE) {
1472  av_log_ask_for_sample(s->avctx, "input buffer too small\n");
1473  s->packet_loss = 1;
1474  return;
1475  }
1476 
1477  if (len > put_bits_left(&s->pb)) {
1479  "Cannot append %d bits, only %d bits available.\n",
1480  len, put_bits_left(&s->pb));
1481  s->packet_loss = 1;
1482  return;
1483  }
1484 
1485  s->num_saved_bits += len;
1486  if (!append) {
1487  avpriv_copy_bits(&s->pb, gb->buffer + (get_bits_count(gb) >> 3),
1488  s->num_saved_bits);
1489  } else {
1490  int align = 8 - (get_bits_count(gb) & 7);
1491  align = FFMIN(align, len);
1492  put_bits(&s->pb, align, get_bits(gb, align));
1493  len -= align;
1494  avpriv_copy_bits(&s->pb, gb->buffer + (get_bits_count(gb) >> 3), len);
1495  }
1496  skip_bits_long(gb, len);
1497 
1498  {
1499  PutBitContext tmp = s->pb;
1500  flush_put_bits(&tmp);
1501  }
1502 
1504  skip_bits(&s->gb, s->frame_offset);
1505 }
1506 
1514 static int decode_packet(AVCodecContext *avctx, void *data,
1515  int *got_frame_ptr, AVPacket* avpkt)
1516 {
1517  WMAProDecodeCtx *s = avctx->priv_data;
1518  GetBitContext* gb = &s->pgb;
1519  const uint8_t* buf = avpkt->data;
1520  int buf_size = avpkt->size;
1521  int num_bits_prev_frame;
1522  int packet_sequence_number;
1523 
1524  *got_frame_ptr = 0;
1525 
1526  if (s->packet_done || s->packet_loss) {
1527  s->packet_done = 0;
1528 
1530  if (buf_size < avctx->block_align) {
1531  av_log(avctx, AV_LOG_ERROR, "Input packet too small (%d < %d)\n",
1532  buf_size, avctx->block_align);
1533  return AVERROR_INVALIDDATA;
1534  }
1535 
1536  s->next_packet_start = buf_size - avctx->block_align;
1537  buf_size = avctx->block_align;
1538  s->buf_bit_size = buf_size << 3;
1539 
1541  init_get_bits(gb, buf, s->buf_bit_size);
1542  packet_sequence_number = get_bits(gb, 4);
1543  skip_bits(gb, 2);
1544 
1546  num_bits_prev_frame = get_bits(gb, s->log2_frame_size);
1547  av_dlog(avctx, "packet[%d]: nbpf %x\n", avctx->frame_number,
1548  num_bits_prev_frame);
1549 
1551  if (!s->packet_loss &&
1552  ((s->packet_sequence_number + 1) & 0xF) != packet_sequence_number) {
1553  s->packet_loss = 1;
1554  av_log(avctx, AV_LOG_ERROR, "Packet loss detected! seq %x vs %x\n",
1555  s->packet_sequence_number, packet_sequence_number);
1556  }
1557  s->packet_sequence_number = packet_sequence_number;
1558 
1559  if (num_bits_prev_frame > 0) {
1560  int remaining_packet_bits = s->buf_bit_size - get_bits_count(gb);
1561  if (num_bits_prev_frame >= remaining_packet_bits) {
1562  num_bits_prev_frame = remaining_packet_bits;
1563  s->packet_done = 1;
1564  }
1565 
1568  save_bits(s, gb, num_bits_prev_frame, 1);
1569  av_dlog(avctx, "accumulated %x bits of frame data\n",
1570  s->num_saved_bits - s->frame_offset);
1571 
1573  if (!s->packet_loss)
1574  decode_frame(s, got_frame_ptr);
1575  } else if (s->num_saved_bits - s->frame_offset) {
1576  av_dlog(avctx, "ignoring %x previously saved bits\n",
1577  s->num_saved_bits - s->frame_offset);
1578  }
1579 
1580  if (s->packet_loss) {
1584  s->num_saved_bits = 0;
1585  s->packet_loss = 0;
1586  }
1587 
1588  } else {
1589  int frame_size;
1590  s->buf_bit_size = (avpkt->size - s->next_packet_start) << 3;
1591  init_get_bits(gb, avpkt->data, s->buf_bit_size);
1592  skip_bits(gb, s->packet_offset);
1593  if (s->len_prefix && remaining_bits(s, gb) > s->log2_frame_size &&
1594  (frame_size = show_bits(gb, s->log2_frame_size)) &&
1595  frame_size <= remaining_bits(s, gb)) {
1596  save_bits(s, gb, frame_size, 0);
1597  s->packet_done = !decode_frame(s, got_frame_ptr);
1598  } else if (!s->len_prefix
1599  && s->num_saved_bits > get_bits_count(&s->gb)) {
1607  s->packet_done = !decode_frame(s, got_frame_ptr);
1608  } else
1609  s->packet_done = 1;
1610  }
1611 
1612  if (s->packet_done && !s->packet_loss &&
1613  remaining_bits(s, gb) > 0) {
1616  save_bits(s, gb, remaining_bits(s, gb), 0);
1617  }
1618 
1619  s->packet_offset = get_bits_count(gb) & 7;
1620  if (s->packet_loss)
1621  return AVERROR_INVALIDDATA;
1622 
1623  if (*got_frame_ptr)
1624  *(AVFrame *)data = s->frame;
1625 
1626  return get_bits_count(gb) >> 3;
1627 }
1628 
1633 static void flush(AVCodecContext *avctx)
1634 {
1635  WMAProDecodeCtx *s = avctx->priv_data;
1636  int i;
1639  for (i = 0; i < avctx->channels; i++)
1640  memset(s->channel[i].out, 0, s->samples_per_frame *
1641  sizeof(*s->channel[i].out));
1642  s->packet_loss = 1;
1643 }
1644 
1645 
1650  .name = "wmapro",
1651  .type = AVMEDIA_TYPE_AUDIO,
1652  .id = AV_CODEC_ID_WMAPRO,
1653  .priv_data_size = sizeof(WMAProDecodeCtx),
1654  .init = decode_init,
1655  .close = decode_end,
1656  .decode = decode_packet,
1657  .capabilities = CODEC_CAP_SUBFRAMES | CODEC_CAP_DR1,
1658  .flush = flush,
1659  .long_name = NULL_IF_CONFIG_SMALL("Windows Media Audio 9 Professional"),
1660  .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
1662 };
void(* vector_fmul_window)(float *dst, const float *src0, const float *src1, const float *win, int len)
Definition: dsputil.h:358
float * channel_data[WMAPRO_MAX_CHANNELS]
transformation coefficients
Definition: wmaprodec.c:164
uint8_t max_num_subframes
Definition: wmaprodec.c:191
av_cold void ff_dsputil_init(DSPContext *c, AVCodecContext *avctx)
Definition: dsputil.c:2656
static const uint16_t critical_freq[]
frequencies to divide the frequency spectrum into scale factor bands
Definition: wmaprodata.h:37
static int decode_tilehdr(WMAProDecodeCtx *s)
Decode how the data in the frame is split into subframes.
Definition: wmaprodec.c:539
This structure describes decoded (raw) audio or video data.
Definition: avcodec.h:989
#define VEC2MAXDEPTH
Definition: wmaprodec.c:118
int subframe_offset
subframe offset in the bit reservoir
Definition: wmaprodec.c:207
static const uint16_t vec1_huffcodes[HUFF_VEC1_SIZE]
Definition: wmaprodata.h:507
static const float coef0_level[HUFF_COEF0_SIZE]
Definition: wmaprodata.h:355
uint8_t table_idx
index for the num_sfb, sfb_offsets, sf_offsets and subwoofer_cutoffs tables
Definition: wmaprodec.c:226
static const uint32_t scale_rl_huffcodes[HUFF_SCALE_RL_SIZE]
Definition: wmaprodata.h:97
uint16_t num_vec_coeffs
number of vector coded coefficients
Definition: wmaprodec.c:152
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:237
#define WMAPRO_BLOCK_MIN_SIZE
minimum block size
Definition: wmaprodec.c:110
uint16_t min_samples_per_subframe
Definition: wmaprodec.c:194
static void skip_bits_long(GetBitContext *s, int n)
Definition: get_bits.h:197
AVFrame * coded_frame
the picture in the bitstream
Definition: avcodec.h:2725
AVCodecContext * avctx
codec context for av_log
Definition: wmaprodec.c:172
uint32_t decode_flags
used compression features
Definition: wmaprodec.c:184
int size
Definition: avcodec.h:916
static const uint16_t vec4_huffcodes[HUFF_VEC4_SIZE]
Definition: wmaprodata.h:421
void avpriv_copy_bits(PutBitContext *pb, const uint8_t *src, int length)
Copy the content of src to the bitstream.
Definition: bitstream.c:60
int8_t scale_factor_step
scaling step for the current subframe
Definition: wmaprodec.c:145
const uint8_t * buffer
Definition: get_bits.h:53
int ff_wma_run_level_decode(AVCodecContext *avctx, GetBitContext *gb, VLC *vlc, const float *level_table, const uint16_t *run_table, int version, WMACoef *ptr, int offset, int num_coefs, int block_len, int frame_len_bits, int coef_nb_bits)
Decode run level compressed coefficients.
Definition: wma.c:431
uint16_t log2_frame_size
Definition: wmaprodec.c:189
static const uint8_t scale_huffbits[HUFF_SCALE_SIZE]
Definition: wmaprodata.h:70
#define AV_RL16
Definition: intreadwrite.h:42
uint8_t table_idx
index in sf_offsets for the scale factor reference block
Definition: wmaprodec.c:150
av_dlog(ac->avr,"%d samples - audio_convert: %s to %s (%s)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic?ac->func_descr_generic:ac->func_descr)
int16_t * cur_sfb_offsets
sfb offsets for the current block
Definition: wmaprodec.c:225
#define VLCBITS
Definition: wmaprodec.c:115
uint8_t run
Definition: svq3.c:124
WMAProChannelGrp chgroup[WMAPRO_MAX_CHANNELS]
channel group information
Definition: wmaprodec.c:230
PutBitContext pb
context for filling the frame_data buffer
Definition: wmaprodec.c:178
#define SCALEVLCBITS
Definition: wmaprodec.c:116
AVCodec.
Definition: avcodec.h:2960
#define WMAPRO_MAX_CHANNELS
current decoder limitations
Definition: wmaprodec.c:103
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs...
Definition: avcodec.h:2141
#define AV_WN32A(p, v)
Definition: intreadwrite.h:458
static int get_sbits(GetBitContext *s, int n)
Definition: get_bits.h:223
static VLC vec1_vlc
1 coefficient per symbol
Definition: wmaprodec.c:127
static av_cold void dump_context(WMAProDecodeCtx *s)
helper function to print the most important members of the context
Definition: wmaprodec.c:240
static const uint8_t scale_rl_run[HUFF_SCALE_RL_SIZE]
Definition: wmaprodata.h:140
static int decode_packet(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt)
Decode a single WMA packet.
Definition: wmaprodec.c:1514
int frame_offset
frame offset in the bit reservoir
Definition: wmaprodec.c:206
static int decode(MimicContext *ctx, int quality, int num_coeffs, int is_iframe)
Definition: mimic.c:228
DSPContext dsp
accelerated DSP functions
Definition: wmaprodec.c:174
static const float coef1_level[HUFF_COEF1_SIZE]
Definition: wmaprodata.h:396
uint16_t subframe_offset[MAX_SUBFRAMES]
subframe positions in the current frame
Definition: wmaprodec.c:139
uint8_t bits
Definition: crc.c:31
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:2112
static void inverse_channel_transform(WMAProDecodeCtx *s)
Reconstruct the individual channel data.
Definition: wmaprodec.c:990
int16_t sfb_offsets[WMAPRO_BLOCK_SIZES][MAX_BANDS]
scale factor band offsets (multiples of 4)
Definition: wmaprodec.c:196
uint8_t
#define HUFF_COEF1_SIZE
Definition: wmaprodata.h:253
#define HUFF_VEC2_SIZE
Definition: wmaprodata.h:460
#define INIT_VLC_STATIC(vlc, bits, a, b, c, d, e, f, g, static_size)
Definition: get_bits.h:436
int8_t num_bands
number of scale factor bands
Definition: wmaprodec.c:223
#define SCALEMAXDEPTH
Definition: wmaprodec.c:120
uint8_t frame_data[MAX_FRAMESIZE+FF_INPUT_BUFFER_PADDING_SIZE]
compressed frame data
Definition: wmaprodec.c:177
#define b
Definition: input.c:52
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1454
uint8_t packet_sequence_number
current packet number
Definition: wmaprodec.c:204
uint8_t drc_gain
gain for the DRC tool
Definition: wmaprodec.c:215
uint8_t dynamic_range_compression
frame contains DRC data
Definition: wmaprodec.c:186
const char data[16]
Definition: mxf.c:66
static uint8_t * append(uint8_t *buf, const uint8_t *src, int size)
#define MAX_FRAMESIZE
maximum compressed frame size
Definition: wmaprodec.c:106
int16_t prev_block_len
length of the previous block
Definition: wmaprodec.c:135
WMAProChannelCtx channel[WMAPRO_MAX_CHANNELS]
per channel data
Definition: wmaprodec.c:232
GetBitContext gb
bitstream reader context
Definition: wmaprodec.c:213
uint8_t * data
Definition: avcodec.h:915
uint8_t grouped
channel is part of a group
Definition: wmaprodec.c:142
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:192
#define WMAPRO_BLOCK_MAX_BITS
log2 of max block size
Definition: wmaprodec.c:109
#define HUFF_COEF0_SIZE
Definition: wmaprodata.h:166
int * scale_factors
pointer to the scale factor values used for decoding
Definition: wmaprodec.c:149
bitstream reader API header.
#define HUFF_VEC1_SIZE
Definition: wmaprodata.h:505
int next_packet_start
start offset of the next wma packet in the demuxer packet
Definition: wmaprodec.c:202
int num_saved_bits
saved number of bits
Definition: wmaprodec.c:205
static const uint16_t scale_huffcodes[HUFF_SCALE_SIZE]
Definition: wmaprodata.h:51
int buf_bit_size
buffer size in bits
Definition: wmaprodec.c:214
static const uint32_t coef1_huffcodes[555]
Definition: wmadata.h:269
static int init(AVCodecParserContext *s)
Definition: h264_parser.c:335
float, planar
Definition: samplefmt.h:60
#define SCALERLMAXDEPTH
Definition: wmaprodec.c:121
static VLC vec4_vlc
4 coefficients per symbol
Definition: wmaprodec.c:125
static const uint8_t frame_size[4]
Definition: g723_1_data.h:47
static int decode_subframe(WMAProDecodeCtx *s)
Decode a single subframe (block).
Definition: wmaprodec.c:1075
uint8_t packet_loss
set in case of bitstream error
Definition: wmaprodec.c:208
void av_log_ask_for_sample(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message asking for a sample.
static int put_bits_left(PutBitContext *s)
Definition: put_bits.h:78
static const uint16_t mask[17]
Definition: lzw.c:38
#define MAX_SUBFRAMES
max number of subframes per channel
Definition: wmaprodec.c:104
#define HUFF_VEC4_SIZE
Definition: wmaprodata.h:419
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:88
int8_t channel_indexes_for_cur_subframe[WMAPRO_MAX_CHANNELS]
Definition: wmaprodec.c:222
int flags
CODEC_FLAG_*.
Definition: avcodec.h:1434
static const uint8_t coef0_huffbits[666]
Definition: wmadata.h:182
static float sin64[33]
sinus table for decorrelation
Definition: wmaprodec.c:129
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:146
#define HUFF_SCALE_SIZE
Definition: wmaprodata.h:49
int max_scale_factor
maximum scale factor for the current subframe
Definition: wmaprodec.c:146
const char * name
Name of the codec implementation.
Definition: avcodec.h:2967
static void put_bits(PutBitContext *s, int n, unsigned int value)
Write up to 31 bits into a bitstream.
Definition: put_bits.h:136
#define ff_mdct_init
Definition: fft.h:146
int8_t channels_for_cur_subframe
number of channels that contain the subframe
Definition: wmaprodec.c:221
AVCodec ff_wmapro_decoder
wmapro decoder
Definition: wmaprodec.c:1649
static int decode_subframe_length(WMAProDecodeCtx *s, int offset)
Decode the subframe length.
Definition: wmaprodec.c:491
static const uint8_t coef1_huffbits[555]
Definition: wmadata.h:342
float out[WMAPRO_BLOCK_MAX_SIZE+WMAPRO_BLOCK_MAX_SIZE/2]
output buffer
Definition: wmaprodec.c:153
int quant_step
quantization step for the current subframe
Definition: wmaprodec.c:143
static const uint16_t coef1_run[HUFF_COEF1_SIZE]
Definition: wmaprodata.h:379
Definition: get_bits.h:63
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:2165
frame specific decoder context for a single channel
Definition: wmaprodec.c:134
static VLC sf_vlc
scale factor DPCM vlc
Definition: wmaprodec.c:123
uint8_t bits_per_sample
integer audio sample size for the unscaled IMDCT output (used to scale to [-1.0, 1.0])
Definition: wmaprodec.c:187
static const uint8_t scale_rl_huffbits[HUFF_SCALE_RL_SIZE]
Definition: wmaprodata.h:118
static void flush(AVCodecContext *avctx)
Clear decoder buffers (for seeking).
Definition: wmaprodec.c:1633
static const uint16_t symbol_to_vec4[HUFF_VEC4_SIZE]
Definition: wmaprodata.h:540
struct WMAProDecodeCtx WMAProDecodeCtx
main decoder context
static int decode_coeffs(WMAProDecodeCtx *s, int c)
Extract the coefficients from the bitstream.
Definition: wmaprodec.c:794
uint8_t num_subframes
Definition: wmaprodec.c:137
Definition: fft.h:62
uint32_t frame_num
current frame number (not used for decoding)
Definition: wmaprodec.c:212
int8_t transform
transform on / off
Definition: wmaprodec.c:161
float tmp[WMAPRO_BLOCK_MAX_SIZE]
IMDCT output buffer.
Definition: wmaprodec.c:180
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame)
Get a buffer for a frame.
Definition: utils.c:464
main decoder context
Definition: wmaprodec.c:170
int8_t sf_offsets[WMAPRO_BLOCK_SIZES][WMAPRO_BLOCK_SIZES][MAX_BANDS]
scale factor resample matrix
Definition: wmaprodec.c:197
static unsigned int show_bits(GetBitContext *s, int n)
Show 1-25 bits.
Definition: get_bits.h:251
uint16_t decoded_samples
number of already processed samples
Definition: wmaprodec.c:141
static const float *const default_decorrelation[]
default decorrelation matrix offsets
Definition: wmaprodata.h:594
static void save_bits(WMAProDecodeCtx *s, GetBitContext *gb, int len, int append)
Fill the bit reservoir with a (partial) frame.
Definition: wmaprodec.c:1454
static av_always_inline int get_vlc2(GetBitContext *s, VLC_TYPE(*table)[2], int bits, int max_depth)
Parse a vlc code.
Definition: get_bits.h:515
int8_t transmit_num_vec_coeffs
number of vector coded coefficients is part of the bitstream
Definition: wmaprodec.c:224
void(* vector_fmul_scalar)(float *dst, const float *src, float mul, int len)
Multiply a vector of floats by a scalar float.
Definition: float_dsp.h:67
#define AV_RL32
Definition: intreadwrite.h:146
#define MAX_BANDS
max number of scale factor bands
Definition: wmaprodec.c:105
uint8_t subframe_len_bits
number of bits used for the subframe length
Definition: wmaprodec.c:192
int8_t transform_band[MAX_BANDS]
controls if the transform is enabled for a certain band
Definition: wmaprodec.c:162
int8_t lfe_channel
lfe channel index
Definition: wmaprodec.c:190
int16_t subwoofer_cutoffs[WMAPRO_BLOCK_SIZES]
subwoofer cutoff values
Definition: wmaprodec.c:198
static VLC vec2_vlc
2 coefficients per symbol
Definition: wmaprodec.c:126
int8_t parsed_all_subframes
all subframes decoded?
Definition: wmaprodec.c:217
external API header
channel group for channel transformations
Definition: wmaprodec.c:159
#define VEC1MAXDEPTH
Definition: wmaprodec.c:119
AV_SAMPLE_FMT_NONE
Definition: avconv_filter.c:63
int16_t subframe_len
current subframe length
Definition: wmaprodec.c:220
int sample_rate
samples per second
Definition: avcodec.h:2104
#define WMAPRO_BLOCK_SIZES
possible block sizes
Definition: wmaprodec.c:112
int debug
debug
Definition: avcodec.h:2568
static const uint8_t vec4_huffbits[HUFF_VEC4_SIZE]
Definition: wmaprodata.h:440
main external API structure.
Definition: avcodec.h:1339
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:326
static const uint8_t symbol_to_vec2[HUFF_VEC2_SIZE]
Definition: wmaprodata.h:557
tables for wmapro decoding
#define VEC4MAXDEPTH
Definition: wmaprodec.c:117
void(* imdct_half)(struct FFTContext *s, FFTSample *output, const FFTSample *input)
Definition: fft.h:82
int extradata_size
Definition: avcodec.h:1455
static VLC coef_vlc[2]
coefficient run length vlc codes
Definition: wmaprodec.c:128
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:268
void avcodec_get_frame_defaults(AVFrame *frame)
Set the fields of the given AVFrame to default values.
Definition: utils.c:602
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:260
int8_t reuse_sf
share scale factors between subframes
Definition: wmaprodec.c:144
void av_log_missing_feature(void *avc, const char *feature, int want_sample)
Log a generic warning message about a missing feature.
Definition: utils.c:2005
SINETABLE_CONST float *const ff_sine_windows[14]
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:372
uint8_t packet_offset
frame offset in the packet
Definition: wmaprodec.c:203
static int decode_scale_factors(WMAProDecodeCtx *s)
Extract scale factors from the bitstream.
Definition: wmaprodec.c:897
int saved_scale_factors[2][MAX_BANDS]
resampled and (previously) transmitted scale factor values
Definition: wmaprodec.c:147
uint8_t num_channels
number of channels in the group
Definition: wmaprodec.c:160
uint8_t transmit_coefs
Definition: wmaprodec.c:136
static VLC sf_rl_vlc
scale factor run length vlc
Definition: wmaprodec.c:124
float * coeffs
pointer to the subframe decode buffer
Definition: wmaprodec.c:151
static int step
Definition: avplay.c:252
float * windows[WMAPRO_BLOCK_SIZES]
windows for the different block sizes
Definition: wmaprodec.c:181
uint8_t cur_subframe
current subframe number
Definition: wmaprodec.c:140
static av_always_inline uint32_t av_float2int(float f)
Reinterpret a float as a 32-bit integer.
Definition: intfloat.h:50
static int remaining_bits(WMAProDecodeCtx *s, GetBitContext *gb)
Calculate remaining input buffer length.
Definition: wmaprodec.c:1442
const uint8_t * quant
static int decode_channel_transform(WMAProDecodeCtx *s)
Decode channel transformation parameters.
Definition: wmaprodec.c:680
av_cold int ff_wma_get_frame_len_bits(int sample_rate, int version, unsigned int decode_flags)
Get the samples per frame for this stream.
Definition: wma_common.c:31
FFTContext mdct_ctx[WMAPRO_BLOCK_SIZES]
MDCT context per block size.
Definition: wmaprodec.c:179
int8_t scale_factor_idx
index for the transmitted scale factor values (used for resampling)
Definition: wmaprodec.c:148
uint8_t level
Definition: svq3.c:125
#define PRINT(a, b)
#define WMAPRO_BLOCK_MAX_SIZE
maximum block size
Definition: wmaprodec.c:111
static av_cold int decode_init(AVCodecContext *avctx)
Initialize the decoder.
Definition: wmaprodec.c:275
#define v0
Definition: regdef.h:26
#define PRINT_HEX(a, b)
uint16_t subframe_len[MAX_SUBFRAMES]
subframe length in samples
Definition: wmaprodec.c:138
int8_t num_sfb[WMAPRO_BLOCK_SIZES]
scale factor bands per block size
Definition: wmaprodec.c:195
common internal api header.
static void decode_decorrelation_matrix(WMAProDecodeCtx *s, WMAProChannelGrp *chgroup)
Calculate a decorrelation matrix from the bitstream parameters.
Definition: wmaprodec.c:630
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:86
#define ff_mdct_end
Definition: fft.h:147
static int decode_frame(WMAProDecodeCtx *s, int *got_frame_ptr)
Decode one WMA frame.
Definition: wmaprodec.c:1313
AVSampleFormat
Audio Sample Formats.
Definition: samplefmt.h:49
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:52
static void wmapro_window(WMAProDecodeCtx *s)
Apply sine window and reconstruct the output buffer.
Definition: wmaprodec.c:1045
DSP utils.
#define WMAPRO_BLOCK_MIN_BITS
log2 of min block size
Definition: wmaprodec.c:108
static const uint8_t scale_rl_level[HUFF_SCALE_RL_SIZE]
Definition: wmaprodata.h:150
void * priv_data
Definition: avcodec.h:1382
int len
GetBitContext pgb
bitstream reader context for the packet
Definition: wmaprodec.c:201
int channels
number of audio channels
Definition: avcodec.h:2105
#define av_log2
Definition: intmath.h:85
VLC_TYPE(* table)[2]
code, bits
Definition: get_bits.h:65
unsigned int ff_wma_get_large_val(GetBitContext *gb)
Decode an uncompressed coefficient.
Definition: wma.c:398
static const uint32_t coef0_huffcodes[666]
Definition: wmadata.h:95
int frame_number
Frame counter, set by libavcodec.
Definition: avcodec.h:2135
AVFloatDSPContext fdsp
Definition: wmaprodec.c:175
uint16_t samples_per_frame
number of samples to output
Definition: wmaprodec.c:188
static const uint8_t vec1_huffbits[HUFF_VEC1_SIZE]
Definition: wmaprodata.h:523
static const uint16_t coef0_run[HUFF_COEF0_SIZE]
Definition: wmaprodata.h:332
static const uint16_t vec2_huffcodes[HUFF_VEC2_SIZE]
Definition: wmaprodata.h:462
int8_t esc_len
length of escaped coefficients
Definition: wmaprodec.c:227
uint8_t len_prefix
frame is prefixed with its length
Definition: wmaprodec.c:185
float decorrelation_matrix[WMAPRO_MAX_CHANNELS *WMAPRO_MAX_CHANNELS]
Definition: wmaprodec.c:163
void avpriv_float_dsp_init(AVFloatDSPContext *fdsp, int bit_exact)
Initialize a float DSP context.
Definition: float_dsp.c:55
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: avcodec.h:1028
static av_cold int decode_end(AVCodecContext *avctx)
Uninitialize the decoder and free all resources.
Definition: wmaprodec.c:259
AVFrame frame
AVFrame for decoded output.
Definition: wmaprodec.c:173
This structure stores compressed data.
Definition: avcodec.h:898
#define HUFF_SCALE_RL_SIZE
Definition: wmaprodata.h:95
uint8_t max_subframe_len_bit
flag indicating that the subframe is of maximum size when the first subframe length bit is 1 ...
Definition: wmaprodec.c:193
int nb_samples
number of audio samples (per channel) described by this frame
Definition: avcodec.h:1042
int8_t skip_frame
skip output step
Definition: wmaprodec.c:216
void ff_init_ff_sine_windows(int index)
initialize the specified entry of ff_sine_windows
static const uint8_t vec2_huffbits[HUFF_VEC2_SIZE]
Definition: wmaprodata.h:483
DSPContext.
Definition: dsputil.h:194
uint8_t num_chgroups
number of channel groups
Definition: wmaprodec.c:229
uint8_t packet_done
set when a packet is fully decoded
Definition: wmaprodec.c:209
if(!(ptr_align%ac->ptr_align)&&samples_align >=aligned_len)
bitstream writer API