comparison mlpdec.c @ 7552:88ffd7c9c0ed libavcodec

mlpdec: Split filter parameters from context into their own struct.
author ramiro
date Tue, 12 Aug 2008 17:53:59 +0000
parents 85ab7655ad4d
children b5f8d814a206
comparison
equal deleted inserted replaced
7551:0a0208d7582f 7552:88ffd7c9c0ed
137 //! Running XOR of all output samples. 137 //! Running XOR of all output samples.
138 int32_t lossless_check_data; 138 int32_t lossless_check_data;
139 139
140 } SubStream; 140 } SubStream;
141 141
142 #define FIR 0
143 #define IIR 1
144
145 /** filter data */
146 typedef struct {
147 //! number of taps in filter
148 uint8_t order;
149 //! Right shift to apply to output of filter.
150 uint8_t shift;
151
152 int32_t coeff[MAX_FILTER_ORDER];
153 int32_t state[MAX_FILTER_ORDER];
154 } FilterParams;
155
142 typedef struct MLPDecodeContext { 156 typedef struct MLPDecodeContext {
143 AVCodecContext *avctx; 157 AVCodecContext *avctx;
144 158
145 //! Set if a valid major sync block has been read. Otherwise no decoding is possible. 159 //! Set if a valid major sync block has been read. Otherwise no decoding is possible.
146 uint8_t params_valid; 160 uint8_t params_valid;
156 //! next power of two above the number of samples in each frame 170 //! next power of two above the number of samples in each frame
157 int access_unit_size_pow2; 171 int access_unit_size_pow2;
158 172
159 SubStream substream[MAX_SUBSTREAMS]; 173 SubStream substream[MAX_SUBSTREAMS];
160 174
161 //@{ 175 FilterParams filter_params[MAX_CHANNELS][NUM_FILTERS];
162 /** filter data */
163 #define FIR 0
164 #define IIR 1
165 //! number of taps in filter
166 uint8_t filter_order[MAX_CHANNELS][NUM_FILTERS];
167 //! Right shift to apply to output of filter.
168 uint8_t filter_shift[MAX_CHANNELS][NUM_FILTERS];
169
170 int32_t filter_coeff[MAX_CHANNELS][NUM_FILTERS][MAX_FILTER_ORDER];
171 int32_t filter_state[MAX_CHANNELS][NUM_FILTERS][MAX_FILTER_ORDER];
172 //@}
173 176
174 //@{ 177 //@{
175 /** sample data coding information */ 178 /** sample data coding information */
176 //! Offset to apply to residual values. 179 //! Offset to apply to residual values.
177 int16_t huff_offset[MAX_CHANNELS]; 180 int16_t huff_offset[MAX_CHANNELS];
518 521
519 memset(s->output_shift , 0, sizeof(s->output_shift )); 522 memset(s->output_shift , 0, sizeof(s->output_shift ));
520 memset(s->quant_step_size, 0, sizeof(s->quant_step_size)); 523 memset(s->quant_step_size, 0, sizeof(s->quant_step_size));
521 524
522 for (ch = s->min_channel; ch <= s->max_channel; ch++) { 525 for (ch = s->min_channel; ch <= s->max_channel; ch++) {
523 m->filter_order[ch][FIR] = 0; 526 m->filter_params[ch][FIR].order = 0;
524 m->filter_order[ch][IIR] = 0; 527 m->filter_params[ch][IIR].order = 0;
525 m->filter_shift[ch][FIR] = 0; 528 m->filter_params[ch][FIR].shift = 0;
526 m->filter_shift[ch][IIR] = 0; 529 m->filter_params[ch][IIR].shift = 0;
527 530
528 /* Default audio coding is 24-bit raw PCM. */ 531 /* Default audio coding is 24-bit raw PCM. */
529 m->huff_offset [ch] = 0; 532 m->huff_offset [ch] = 0;
530 m->sign_huff_offset[ch] = (-1) << 23; 533 m->sign_huff_offset[ch] = (-1) << 23;
531 m->codebook [ch] = 0; 534 m->codebook [ch] = 0;
542 /** Read parameters for one of the prediction filters. */ 545 /** Read parameters for one of the prediction filters. */
543 546
544 static int read_filter_params(MLPDecodeContext *m, GetBitContext *gbp, 547 static int read_filter_params(MLPDecodeContext *m, GetBitContext *gbp,
545 unsigned int channel, unsigned int filter) 548 unsigned int channel, unsigned int filter)
546 { 549 {
550 FilterParams *fp = &m->filter_params[channel][filter];
547 const char fchar = filter ? 'I' : 'F'; 551 const char fchar = filter ? 'I' : 'F';
548 int i, order; 552 int i, order;
549 553
550 // Filter is 0 for FIR, 1 for IIR. 554 // Filter is 0 for FIR, 1 for IIR.
551 assert(filter < 2); 555 assert(filter < 2);
555 av_log(m->avctx, AV_LOG_ERROR, 559 av_log(m->avctx, AV_LOG_ERROR,
556 "%cIR filter order %d is greater than maximum %d.\n", 560 "%cIR filter order %d is greater than maximum %d.\n",
557 fchar, order, MAX_FILTER_ORDER); 561 fchar, order, MAX_FILTER_ORDER);
558 return -1; 562 return -1;
559 } 563 }
560 m->filter_order[channel][filter] = order; 564 fp->order = order;
561 565
562 if (order > 0) { 566 if (order > 0) {
563 int coeff_bits, coeff_shift; 567 int coeff_bits, coeff_shift;
564 568
565 m->filter_shift[channel][filter] = get_bits(gbp, 4); 569 fp->shift = get_bits(gbp, 4);
566 570
567 coeff_bits = get_bits(gbp, 5); 571 coeff_bits = get_bits(gbp, 5);
568 coeff_shift = get_bits(gbp, 3); 572 coeff_shift = get_bits(gbp, 3);
569 if (coeff_bits < 1 || coeff_bits > 16) { 573 if (coeff_bits < 1 || coeff_bits > 16) {
570 av_log(m->avctx, AV_LOG_ERROR, 574 av_log(m->avctx, AV_LOG_ERROR,
578 fchar); 582 fchar);
579 return -1; 583 return -1;
580 } 584 }
581 585
582 for (i = 0; i < order; i++) 586 for (i = 0; i < order; i++)
583 m->filter_coeff[channel][filter][i] = 587 fp->coeff[i] =
584 get_sbits(gbp, coeff_bits) << coeff_shift; 588 get_sbits(gbp, coeff_bits) << coeff_shift;
585 589
586 if (get_bits1(gbp)) { 590 if (get_bits1(gbp)) {
587 int state_bits, state_shift; 591 int state_bits, state_shift;
588 592
596 state_shift = get_bits(gbp, 4); 600 state_shift = get_bits(gbp, 4);
597 601
598 /* TODO: Check validity of state data. */ 602 /* TODO: Check validity of state data. */
599 603
600 for (i = 0; i < order; i++) 604 for (i = 0; i < order; i++)
601 m->filter_state[channel][filter][i] = 605 fp->state[i] =
602 get_sbits(gbp, state_bits) << state_shift; 606 get_sbits(gbp, state_bits) << state_shift;
603 } 607 }
604 } 608 }
605 609
606 return 0; 610 return 0;
687 m->sign_huff_offset[ch] = calculate_sign_huff(m, substr, ch); 691 m->sign_huff_offset[ch] = calculate_sign_huff(m, substr, ch);
688 } 692 }
689 693
690 for (ch = s->min_channel; ch <= s->max_channel; ch++) 694 for (ch = s->min_channel; ch <= s->max_channel; ch++)
691 if (get_bits1(gbp)) { 695 if (get_bits1(gbp)) {
696 FilterParams *fir = &m->filter_params[ch][FIR];
697 FilterParams *iir = &m->filter_params[ch][IIR];
698
692 if (s->param_presence_flags & PARAM_FIR) 699 if (s->param_presence_flags & PARAM_FIR)
693 if (get_bits1(gbp)) 700 if (get_bits1(gbp))
694 if (read_filter_params(m, gbp, ch, FIR) < 0) 701 if (read_filter_params(m, gbp, ch, FIR) < 0)
695 return -1; 702 return -1;
696 703
697 if (s->param_presence_flags & PARAM_IIR) 704 if (s->param_presence_flags & PARAM_IIR)
698 if (get_bits1(gbp)) 705 if (get_bits1(gbp))
699 if (read_filter_params(m, gbp, ch, IIR) < 0) 706 if (read_filter_params(m, gbp, ch, IIR) < 0)
700 return -1; 707 return -1;
701 708
702 if (m->filter_order[ch][FIR] && m->filter_order[ch][IIR] && 709 if (fir->order && iir->order &&
703 m->filter_shift[ch][FIR] != m->filter_shift[ch][IIR]) { 710 fir->shift != iir->shift) {
704 av_log(m->avctx, AV_LOG_ERROR, 711 av_log(m->avctx, AV_LOG_ERROR,
705 "FIR and IIR filters must use the same precision.\n"); 712 "FIR and IIR filters must use the same precision.\n");
706 return -1; 713 return -1;
707 } 714 }
708 /* The FIR and IIR filters must have the same precision. 715 /* The FIR and IIR filters must have the same precision.
709 * To simplify the filtering code, only the precision of the 716 * To simplify the filtering code, only the precision of the
710 * FIR filter is considered. If only the IIR filter is employed, 717 * FIR filter is considered. If only the IIR filter is employed,
711 * the FIR filter precision is set to that of the IIR filter, so 718 * the FIR filter precision is set to that of the IIR filter, so
712 * that the filtering code can use it. */ 719 * that the filtering code can use it. */
713 if (!m->filter_order[ch][FIR] && m->filter_order[ch][IIR]) 720 if (!fir->order && iir->order)
714 m->filter_shift[ch][FIR] = m->filter_shift[ch][IIR]; 721 fir->shift = iir->shift;
715 722
716 if (s->param_presence_flags & PARAM_HUFFOFFSET) 723 if (s->param_presence_flags & PARAM_HUFFOFFSET)
717 if (get_bits1(gbp)) 724 if (get_bits1(gbp))
718 m->huff_offset[ch] = get_sbits(gbp, 15); 725 m->huff_offset[ch] = get_sbits(gbp, 15);
719 726
736 static void filter_channel(MLPDecodeContext *m, unsigned int substr, 743 static void filter_channel(MLPDecodeContext *m, unsigned int substr,
737 unsigned int channel) 744 unsigned int channel)
738 { 745 {
739 SubStream *s = &m->substream[substr]; 746 SubStream *s = &m->substream[substr];
740 int32_t filter_state_buffer[NUM_FILTERS][MAX_BLOCKSIZE + MAX_FILTER_ORDER]; 747 int32_t filter_state_buffer[NUM_FILTERS][MAX_BLOCKSIZE + MAX_FILTER_ORDER];
741 unsigned int filter_shift = m->filter_shift[channel][FIR]; 748 FilterParams *fp[NUM_FILTERS] = { &m->filter_params[channel][FIR],
749 &m->filter_params[channel][IIR], };
750 unsigned int filter_shift = fp[FIR]->shift;
742 int32_t mask = MSB_MASK(s->quant_step_size[channel]); 751 int32_t mask = MSB_MASK(s->quant_step_size[channel]);
743 int index = MAX_BLOCKSIZE; 752 int index = MAX_BLOCKSIZE;
744 int j, i; 753 int j, i;
745 754
746 for (j = 0; j < NUM_FILTERS; j++) { 755 for (j = 0; j < NUM_FILTERS; j++) {
747 memcpy(& filter_state_buffer [j][MAX_BLOCKSIZE], 756 memcpy(& filter_state_buffer [j][MAX_BLOCKSIZE],
748 &m->filter_state[channel][j][0], 757 &fp[j]->state[0],
749 MAX_FILTER_ORDER * sizeof(int32_t)); 758 MAX_FILTER_ORDER * sizeof(int32_t));
750 } 759 }
751 760
752 for (i = 0; i < s->blocksize; i++) { 761 for (i = 0; i < s->blocksize; i++) {
753 int32_t residual = m->sample_buffer[i + s->blockpos][channel]; 762 int32_t residual = m->sample_buffer[i + s->blockpos][channel];
756 int32_t result; 765 int32_t result;
757 766
758 /* TODO: Move this code to DSPContext? */ 767 /* TODO: Move this code to DSPContext? */
759 768
760 for (j = 0; j < NUM_FILTERS; j++) 769 for (j = 0; j < NUM_FILTERS; j++)
761 for (order = 0; order < m->filter_order[channel][j]; order++) 770 for (order = 0; order < fp[j]->order; order++)
762 accum += (int64_t)filter_state_buffer[j][index + order] * 771 accum += (int64_t)filter_state_buffer[j][index + order] *
763 m->filter_coeff[channel][j][order]; 772 fp[j]->coeff[order];
764 773
765 accum = accum >> filter_shift; 774 accum = accum >> filter_shift;
766 result = (accum + residual) & mask; 775 result = (accum + residual) & mask;
767 776
768 --index; 777 --index;
772 781
773 m->sample_buffer[i + s->blockpos][channel] = result; 782 m->sample_buffer[i + s->blockpos][channel] = result;
774 } 783 }
775 784
776 for (j = 0; j < NUM_FILTERS; j++) { 785 for (j = 0; j < NUM_FILTERS; j++) {
777 memcpy(&m->filter_state[channel][j][0], 786 memcpy(&fp[j]->state[0],
778 & filter_state_buffer [j][index], 787 & filter_state_buffer [j][index],
779 MAX_FILTER_ORDER * sizeof(int32_t)); 788 MAX_FILTER_ORDER * sizeof(int32_t));
780 } 789 }
781 } 790 }
782 791