comparison mlpdec.c @ 7555:8d00a2dfcb7a libavcodec

mlpdec: Split channel parameters from context into their own struct.
author ramiro
date Wed, 13 Aug 2008 01:36:01 +0000
parents b5f8d814a206
children a8ddede2248f
comparison
equal deleted inserted replaced
7554:96d57e3b78e5 7555:8d00a2dfcb7a
151 151
152 int32_t coeff[MAX_FILTER_ORDER]; 152 int32_t coeff[MAX_FILTER_ORDER];
153 int32_t state[MAX_FILTER_ORDER]; 153 int32_t state[MAX_FILTER_ORDER];
154 } FilterParams; 154 } FilterParams;
155 155
156 /** sample data coding information */
157 typedef struct {
158 FilterParams filter_params[NUM_FILTERS];
159
160 //! Offset to apply to residual values.
161 int16_t huff_offset;
162 //! sign/rounding-corrected version of huff_offset
163 int32_t sign_huff_offset;
164 //! Which VLC codebook to use to read residuals.
165 uint8_t codebook;
166 //! Size of residual suffix not encoded using VLC.
167 uint8_t huff_lsbs;
168 } ChannelParams;
169
156 typedef struct MLPDecodeContext { 170 typedef struct MLPDecodeContext {
157 AVCodecContext *avctx; 171 AVCodecContext *avctx;
158 172
159 //! Set if a valid major sync block has been read. Otherwise no decoding is possible. 173 //! Set if a valid major sync block has been read. Otherwise no decoding is possible.
160 uint8_t params_valid; 174 uint8_t params_valid;
170 //! next power of two above the number of samples in each frame 184 //! next power of two above the number of samples in each frame
171 int access_unit_size_pow2; 185 int access_unit_size_pow2;
172 186
173 SubStream substream[MAX_SUBSTREAMS]; 187 SubStream substream[MAX_SUBSTREAMS];
174 188
175 FilterParams filter_params[MAX_CHANNELS][NUM_FILTERS]; 189 ChannelParams channel_params[MAX_CHANNELS];
176
177 //@{
178 /** sample data coding information */
179 //! Offset to apply to residual values.
180 int16_t huff_offset[MAX_CHANNELS];
181 //! sign/rounding-corrected version of huff_offset
182 int32_t sign_huff_offset[MAX_CHANNELS];
183 //! Which VLC codebook to use to read residuals.
184 uint8_t codebook[MAX_CHANNELS];
185 //! Size of residual suffix not encoded using VLC.
186 uint8_t huff_lsbs[MAX_CHANNELS];
187 //@}
188 190
189 int8_t noise_buffer[MAX_BLOCKSIZE_POW2]; 191 int8_t noise_buffer[MAX_BLOCKSIZE_POW2];
190 int8_t bypassed_lsbs[MAX_BLOCKSIZE][MAX_CHANNELS]; 192 int8_t bypassed_lsbs[MAX_BLOCKSIZE][MAX_CHANNELS];
191 int32_t sample_buffer[MAX_BLOCKSIZE][MAX_CHANNELS+2]; 193 int32_t sample_buffer[MAX_BLOCKSIZE][MAX_CHANNELS+2];
192 } MLPDecodeContext; 194 } MLPDecodeContext;
276 } 278 }
277 279
278 static inline int32_t calculate_sign_huff(MLPDecodeContext *m, 280 static inline int32_t calculate_sign_huff(MLPDecodeContext *m,
279 unsigned int substr, unsigned int ch) 281 unsigned int substr, unsigned int ch)
280 { 282 {
283 ChannelParams *cp = &m->channel_params[ch];
281 SubStream *s = &m->substream[substr]; 284 SubStream *s = &m->substream[substr];
282 int lsb_bits = m->huff_lsbs[ch] - s->quant_step_size[ch]; 285 int lsb_bits = cp->huff_lsbs - s->quant_step_size[ch];
283 int sign_shift = lsb_bits + (m->codebook[ch] ? 2 - m->codebook[ch] : -1); 286 int sign_shift = lsb_bits + (cp->codebook ? 2 - cp->codebook : -1);
284 int32_t sign_huff_offset = m->huff_offset[ch]; 287 int32_t sign_huff_offset = cp->huff_offset;
285 288
286 if (m->codebook[ch] > 0) 289 if (cp->codebook > 0)
287 sign_huff_offset -= 7 << lsb_bits; 290 sign_huff_offset -= 7 << lsb_bits;
288 291
289 if (sign_shift >= 0) 292 if (sign_shift >= 0)
290 sign_huff_offset -= 1 << sign_shift; 293 sign_huff_offset -= 1 << sign_shift;
291 294
304 for (mat = 0; mat < s->num_primitive_matrices; mat++) 307 for (mat = 0; mat < s->num_primitive_matrices; mat++)
305 if (s->lsb_bypass[mat]) 308 if (s->lsb_bypass[mat])
306 m->bypassed_lsbs[pos + s->blockpos][mat] = get_bits1(gbp); 309 m->bypassed_lsbs[pos + s->blockpos][mat] = get_bits1(gbp);
307 310
308 for (channel = s->min_channel; channel <= s->max_channel; channel++) { 311 for (channel = s->min_channel; channel <= s->max_channel; channel++) {
309 int codebook = m->codebook[channel]; 312 ChannelParams *cp = &m->channel_params[channel];
313 int codebook = cp->codebook;
310 int quant_step_size = s->quant_step_size[channel]; 314 int quant_step_size = s->quant_step_size[channel];
311 int lsb_bits = m->huff_lsbs[channel] - quant_step_size; 315 int lsb_bits = cp->huff_lsbs - quant_step_size;
312 int result = 0; 316 int result = 0;
313 317
314 if (codebook > 0) 318 if (codebook > 0)
315 result = get_vlc2(gbp, huff_vlc[codebook-1].table, 319 result = get_vlc2(gbp, huff_vlc[codebook-1].table,
316 VLC_BITS, (9 + VLC_BITS - 1) / VLC_BITS); 320 VLC_BITS, (9 + VLC_BITS - 1) / VLC_BITS);
319 return -1; 323 return -1;
320 324
321 if (lsb_bits > 0) 325 if (lsb_bits > 0)
322 result = (result << lsb_bits) + get_bits(gbp, lsb_bits); 326 result = (result << lsb_bits) + get_bits(gbp, lsb_bits);
323 327
324 result += m->sign_huff_offset[channel]; 328 result += cp->sign_huff_offset;
325 result <<= quant_step_size; 329 result <<= quant_step_size;
326 330
327 m->sample_buffer[pos + s->blockpos][channel] = result; 331 m->sample_buffer[pos + s->blockpos][channel] = result;
328 } 332 }
329 333
521 525
522 memset(s->output_shift , 0, sizeof(s->output_shift )); 526 memset(s->output_shift , 0, sizeof(s->output_shift ));
523 memset(s->quant_step_size, 0, sizeof(s->quant_step_size)); 527 memset(s->quant_step_size, 0, sizeof(s->quant_step_size));
524 528
525 for (ch = s->min_channel; ch <= s->max_channel; ch++) { 529 for (ch = s->min_channel; ch <= s->max_channel; ch++) {
526 m->filter_params[ch][FIR].order = 0; 530 ChannelParams *cp = &m->channel_params[ch];
527 m->filter_params[ch][IIR].order = 0; 531 cp->filter_params[FIR].order = 0;
528 m->filter_params[ch][FIR].shift = 0; 532 cp->filter_params[IIR].order = 0;
529 m->filter_params[ch][IIR].shift = 0; 533 cp->filter_params[FIR].shift = 0;
534 cp->filter_params[IIR].shift = 0;
530 535
531 /* Default audio coding is 24-bit raw PCM. */ 536 /* Default audio coding is 24-bit raw PCM. */
532 m->huff_offset [ch] = 0; 537 cp->huff_offset = 0;
533 m->sign_huff_offset[ch] = (-1) << 23; 538 cp->sign_huff_offset = (-1) << 23;
534 m->codebook [ch] = 0; 539 cp->codebook = 0;
535 m->huff_lsbs [ch] = 24; 540 cp->huff_lsbs = 24;
536 } 541 }
537 542
538 if (substr == m->max_decoded_substream) { 543 if (substr == m->max_decoded_substream) {
539 m->avctx->channels = s->max_channel + 1; 544 m->avctx->channels = s->max_channel + 1;
540 } 545 }
545 /** Read parameters for one of the prediction filters. */ 550 /** Read parameters for one of the prediction filters. */
546 551
547 static int read_filter_params(MLPDecodeContext *m, GetBitContext *gbp, 552 static int read_filter_params(MLPDecodeContext *m, GetBitContext *gbp,
548 unsigned int channel, unsigned int filter) 553 unsigned int channel, unsigned int filter)
549 { 554 {
550 FilterParams *fp = &m->filter_params[channel][filter]; 555 FilterParams *fp = &m->channel_params[channel].filter_params[filter];
551 const char fchar = filter ? 'I' : 'F'; 556 const char fchar = filter ? 'I' : 'F';
552 int i, order; 557 int i, order;
553 558
554 // Filter is 0 for FIR, 1 for IIR. 559 // Filter is 0 for FIR, 1 for IIR.
555 assert(filter < 2); 560 assert(filter < 2);
681 } 686 }
682 687
683 if (s->param_presence_flags & PARAM_QUANTSTEP) 688 if (s->param_presence_flags & PARAM_QUANTSTEP)
684 if (get_bits1(gbp)) 689 if (get_bits1(gbp))
685 for (ch = 0; ch <= s->max_channel; ch++) { 690 for (ch = 0; ch <= s->max_channel; ch++) {
691 ChannelParams *cp = &m->channel_params[ch];
692
686 s->quant_step_size[ch] = get_bits(gbp, 4); 693 s->quant_step_size[ch] = get_bits(gbp, 4);
687 /* TODO: validate */ 694 /* TODO: validate */
688 695
689 m->sign_huff_offset[ch] = calculate_sign_huff(m, substr, ch); 696 cp->sign_huff_offset = calculate_sign_huff(m, substr, ch);
690 } 697 }
691 698
692 for (ch = s->min_channel; ch <= s->max_channel; ch++) 699 for (ch = s->min_channel; ch <= s->max_channel; ch++)
693 if (get_bits1(gbp)) { 700 if (get_bits1(gbp)) {
694 FilterParams *fir = &m->filter_params[ch][FIR]; 701 ChannelParams *cp = &m->channel_params[ch];
695 FilterParams *iir = &m->filter_params[ch][IIR]; 702 FilterParams *fir = &cp->filter_params[FIR];
703 FilterParams *iir = &cp->filter_params[IIR];
696 704
697 if (s->param_presence_flags & PARAM_FIR) 705 if (s->param_presence_flags & PARAM_FIR)
698 if (get_bits1(gbp)) 706 if (get_bits1(gbp))
699 if (read_filter_params(m, gbp, ch, FIR) < 0) 707 if (read_filter_params(m, gbp, ch, FIR) < 0)
700 return -1; 708 return -1;
718 if (!fir->order && iir->order) 726 if (!fir->order && iir->order)
719 fir->shift = iir->shift; 727 fir->shift = iir->shift;
720 728
721 if (s->param_presence_flags & PARAM_HUFFOFFSET) 729 if (s->param_presence_flags & PARAM_HUFFOFFSET)
722 if (get_bits1(gbp)) 730 if (get_bits1(gbp))
723 m->huff_offset[ch] = get_sbits(gbp, 15); 731 cp->huff_offset = get_sbits(gbp, 15);
724 732
725 m->codebook [ch] = get_bits(gbp, 2); 733 cp->codebook = get_bits(gbp, 2);
726 m->huff_lsbs[ch] = get_bits(gbp, 5); 734 cp->huff_lsbs = get_bits(gbp, 5);
727 735
728 m->sign_huff_offset[ch] = calculate_sign_huff(m, substr, ch); 736 cp->sign_huff_offset = calculate_sign_huff(m, substr, ch);
729 737
730 /* TODO: validate */ 738 /* TODO: validate */
731 } 739 }
732 740
733 return 0; 741 return 0;
741 static void filter_channel(MLPDecodeContext *m, unsigned int substr, 749 static void filter_channel(MLPDecodeContext *m, unsigned int substr,
742 unsigned int channel) 750 unsigned int channel)
743 { 751 {
744 SubStream *s = &m->substream[substr]; 752 SubStream *s = &m->substream[substr];
745 int32_t filter_state_buffer[NUM_FILTERS][MAX_BLOCKSIZE + MAX_FILTER_ORDER]; 753 int32_t filter_state_buffer[NUM_FILTERS][MAX_BLOCKSIZE + MAX_FILTER_ORDER];
746 FilterParams *fp[NUM_FILTERS] = { &m->filter_params[channel][FIR], 754 FilterParams *fp[NUM_FILTERS] = { &m->channel_params[channel].filter_params[FIR],
747 &m->filter_params[channel][IIR], }; 755 &m->channel_params[channel].filter_params[IIR], };
748 unsigned int filter_shift = fp[FIR]->shift; 756 unsigned int filter_shift = fp[FIR]->shift;
749 int32_t mask = MSB_MASK(s->quant_step_size[channel]); 757 int32_t mask = MSB_MASK(s->quant_step_size[channel]);
750 int index = MAX_BLOCKSIZE; 758 int index = MAX_BLOCKSIZE;
751 int j, i; 759 int j, i;
752 760