• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* ------------------------------------------------------------------
2  * Copyright (C) 1998-2009 PacketVideo
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
13  * express or implied.
14  * See the License for the specific language governing permissions
15  * and limitations under the License.
16  * -------------------------------------------------------------------
17  */
18 /*
19 ------------------------------------------------------------------------------
20 
21    PacketVideo Corp.
22    MP3 Decoder Library
23 
24    Filename: pvmp3_stereo_proc.cpp
25 
26    Functions:
27 
28     pvmp3_st_mid_side
29     pvmp3_st_intensity
30     pvmp3_stereo_proc
31 
32 ------------------------------------------------------------------------------
33 
34 pvmp3_st_mid_side
35 
36  INPUT AND OUTPUT DEFINITIONS
37 
38 Input
39 
40    int32 xr[],      input channel
41    int32 xl[],
42    int32 Start,     Location of first element where stereo intensity is applied
43    int32 Number     number of elements affected
44 
45  Returns
46 
47    int32 xl[],      generated stereo channel
48 
49 
50 ------------------------------------------------------------------------------
51 
52 pvmp3_st_intensity
53 
54  INPUT AND OUTPUT DEFINITIONS
55 
56 Input
57 
58    int32 xr[],      input channel
59    int32 xl[],
60    int32 is_pos,    index to table is_ratio_factor[]
61    int32 Start,     Location of first element where stereo intensity is applied
62    int32 Number     number of elements affected
63 
64  Returns
65 
66    int32 xl[],      generated stereo channel
67 
68 
69 ------------------------------------------------------------------------------
70 
71 pvmp3_stereo_proc
72 
73  INPUT AND OUTPUT DEFINITIONS
74 
75 Input
76 
77    int32 xr[],                    input channel
78    int32 xl[],
79    mp3ScaleFactors  *scalefac,    scale factors structure
80    struct gr_info_s *gr_info,     granule structure
81    mp3Header *info                mp3 header info
82  Returns
83 
84    int32 xl[],      generated stereo channel
85 
86 
87 ------------------------------------------------------------------------------
88  FUNCTION DESCRIPTION
89 
90     stereo processing for mpeg1 layer III
91     After requantization, the reconstructed values are processed for ms_stereo
92     or intensity_stereo modes or both, before passing them to the synthesis
93     filterbank
94 
95     In ms_stereo mode the values of the normalized middle/side channels
96     M[l] and S[l] are transmitted instead of the left/right channel values
97     L[l] and R[l]. From here, L[l] and R[l] are reconstructed
98 
99     Intensity_stereo is done by specifying the magnitude (via the
100     scalefactors of the left channel) and a stereo position is_pos[sfb],
101     which is transmitted instead of scalefactors of the right channel.
102     The stereo position is used to derive the left and right channel signals
103 
104 ------------------------------------------------------------------------------
105  REQUIREMENTS
106 
107 
108 ------------------------------------------------------------------------------
109  REFERENCES
110 
111  [1] ISO MPEG Audio Subgroup Software Simulation Group (1996)
112      ISO 13818-3 MPEG-2 Audio Decoder - Lower Sampling Frequency Extension
113 
114 ------------------------------------------------------------------------------
115  PSEUDO-CODE
116 
117 ------------------------------------------------------------------------------
118 */
119 
120 
121 /*----------------------------------------------------------------------------
122 ; INCLUDES
123 ----------------------------------------------------------------------------*/
124 
125 #include "pvmp3_stereo_proc.h"
126 #include "pv_mp3dec_fxd_op.h"
127 #include "pvmp3_tables.h"
128 
129 
130 /*----------------------------------------------------------------------------
131 ; MACROS
132 ; Define module specific macros here
133 ----------------------------------------------------------------------------*/
134 
135 
136 /*----------------------------------------------------------------------------
137 ; DEFINES
138 ; Include all pre-processor statements here. Include conditional
139 ; compile variables also.
140 ----------------------------------------------------------------------------*/
141 #define N31 31
142 
143 #define Q31_fmt(a)    (int32(double(0x7FFFFFFF)*(a)))
144 
145 /*----------------------------------------------------------------------------
146 ; LOCAL FUNCTION DEFINITIONS
147 ; Function Prototype declaration
148 ----------------------------------------------------------------------------*/
149 
150 /*----------------------------------------------------------------------------
151 ; LOCAL STORE/BUFFER/POINTER DEFINITIONS
152 ; Variable declaration - defined here and used outside this module
153 ----------------------------------------------------------------------------*/
154 /*
155  *  TmpFac= tan(is_pos * (PI /12));
156  *
157  *  TmpFac /= (1 + TmpFac);
158  *
159  */
160 
161 const int32  is_ratio_factor[8] = {0,
162                                    Q31_fmt(0.21132486540519),   Q31_fmt(0.36602540378444),   Q31_fmt(0.50000000000000),
163                                    Q31_fmt(0.63397459621556),   Q31_fmt(0.78867513459481),   Q31_fmt(1.00000000000000),
164                                    0
165                                   };
166 
167 /*----------------------------------------------------------------------------
168 ; EXTERNAL FUNCTION REFERENCES
169 ; Declare functions defined elsewhere and referenced in this module
170 ----------------------------------------------------------------------------*/
171 
172 /*----------------------------------------------------------------------------
173 ; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES
174 ; Declare variables used in this module but defined elsewhere
175 ----------------------------------------------------------------------------*/
176 
177 /*----------------------------------------------------------------------------
178 ; FUNCTION CODE
179 ----------------------------------------------------------------------------*/
180 
181 #if __has_attribute(no_sanitize)
182 // deliberately playing near overflow points of int32
183 __attribute__((no_sanitize("integer")))
184 #endif
pvmp3_st_mid_side(int32 xr[SUBBANDS_NUMBER * FILTERBANK_BANDS],int32 xl[SUBBANDS_NUMBER * FILTERBANK_BANDS],int32 Start,int32 Number)185 void pvmp3_st_mid_side(int32 xr[SUBBANDS_NUMBER*FILTERBANK_BANDS],
186                        int32 xl[SUBBANDS_NUMBER*FILTERBANK_BANDS],
187                        int32 Start,
188                        int32 Number)
189 {
190 
191     int32 *pt_xr  = &xr[Start];
192     int32 *pt_xl  = &xl[Start];
193 
194     for (int32 i = Number >> 1; i != 0; i--)
195     {
196         int32 xxr = *(pt_xr) << 1;
197         int32 xxl = *(pt_xl) << 1;
198         *(pt_xr++)  = fxp_mul32_Q32((xxr + xxl), Q31_fmt(0.70710678118655));   /* Sum */
199         *(pt_xl++)  = fxp_mul32_Q32((xxr - xxl), Q31_fmt(0.70710678118655));   /* Diff */
200         xxr = *(pt_xr) << 1;
201         xxl = *(pt_xl) << 1;
202         *(pt_xr++)  = fxp_mul32_Q32((xxr + xxl), Q31_fmt(0.70710678118655));   /* Sum */
203         *(pt_xl++)  = fxp_mul32_Q32((xxr - xxl), Q31_fmt(0.70710678118655));   /* Diff */
204     }
205 
206 
207     if (Number&1)
208     {
209         int32 xxr = *(pt_xr) << 1;
210         int32 xxl = *(pt_xl) << 1;
211         *(pt_xr)  = fxp_mul32_Q32((xxr + xxl), Q31_fmt(0.70710678118655));   /* Sum */
212         *(pt_xl)  = fxp_mul32_Q32((xxr - xxl), Q31_fmt(0.70710678118655));   /* Diff */
213     }
214 
215 }
216 
217 
218 /*----------------------------------------------------------------------------
219 ; FUNCTION CODE
220 ----------------------------------------------------------------------------*/
221 
pvmp3_st_intensity(int32 xr[SUBBANDS_NUMBER * FILTERBANK_BANDS],int32 xl[SUBBANDS_NUMBER * FILTERBANK_BANDS],int32 is_pos,int32 Start,int32 Number)222 void pvmp3_st_intensity(int32 xr[SUBBANDS_NUMBER*FILTERBANK_BANDS],
223                         int32 xl[SUBBANDS_NUMBER*FILTERBANK_BANDS],
224                         int32 is_pos,
225                         int32 Start,
226                         int32 Number)
227 {
228 
229     int32 TmpFac = is_ratio_factor[ is_pos & 7];
230 
231     int32 *pt_xr  = &xr[Start];
232     int32 *pt_xl  = &xl[Start];
233 
234     for (int32 i = Number >> 1; i != 0; i--)
235     {
236         int32 tmp = fxp_mul32_Q32((*pt_xr) << 1, TmpFac);
237         *(pt_xl++) = (*pt_xr) - tmp;
238         *(pt_xr++) = tmp;
239         tmp = fxp_mul32_Q32((*pt_xr) << 1, TmpFac);
240         *(pt_xl++) = (*pt_xr) - tmp;
241         *(pt_xr++) = tmp;
242     }
243 
244     if (Number&1)
245     {
246         int32 tmp = fxp_mul32_Q32((*pt_xr) << 1, TmpFac);
247         *(pt_xl) = (*pt_xr) - tmp;
248         *(pt_xr) = tmp;
249     }
250 
251 }
252 
253 /*----------------------------------------------------------------------------
254 ; FUNCTION CODE
255 ----------------------------------------------------------------------------*/
pvmp3_stereo_proc(int32 xr[SUBBANDS_NUMBER * FILTERBANK_BANDS],int32 xl[SUBBANDS_NUMBER * FILTERBANK_BANDS],mp3ScaleFactors * scalefac,granuleInfo * gr_info,int32 used_freq_lines,mp3Header * info)256 void pvmp3_stereo_proc(int32 xr[SUBBANDS_NUMBER*FILTERBANK_BANDS],
257                        int32 xl[SUBBANDS_NUMBER*FILTERBANK_BANDS],
258                        mp3ScaleFactors *scalefac,
259                        granuleInfo *gr_info,
260                        int32 used_freq_lines,
261                        mp3Header *info)
262 {
263 
264 
265     int32 sb;
266     int32 ss;
267     int32 sfbNo;
268     int32 sfbStart;
269 
270     int32 sfb;
271     int32 sfbTemp;
272     int32 i;
273     int32 j;
274 
275 
276     int32 i_stereo  = (info->mode == MPG_MD_JOINT_STEREO) &&
277                       (info->mode_ext & 0x1);
278 
279     int32 ms_stereo = (info->mode == MPG_MD_JOINT_STEREO) &&
280                       (info->mode_ext & 0x2);
281 
282     int32 sfreq  = info->version_x + (info->version_x << 1);
283     sfreq += info->sampling_frequency;
284 
285 
286 
287 
288     if (i_stereo)
289     {
290         if (gr_info->window_switching_flag && (gr_info->block_type == 2))
291         {
292             if (gr_info->mixed_block_flag)
293             {
294                 /*
295                  * mixed blocks processing
296                  */
297                 i = 31;
298                 ss = 17;
299                 sb = 0;
300                 while (i >= 0)
301                 {
302                     if (xl[(i*FILTERBANK_BANDS) + ss])
303                     {
304                         sb = (i << 4) + (i << 1) + ss;
305                         i = -1;
306                     }
307                     else
308                     {
309                         ss--;
310                         if (ss < 0)
311                         {
312                             i--;
313                             ss = 17;
314                         }
315                     }
316                 }
317 
318                 if (sb < 36)
319                 {
320                     /*
321                      * mixed blocks processing: intensity bound inside long blocks
322                      */
323                     /* 1. long blocks up to intensity border: not intensity */
324 
325                     if (mp3_sfBandIndex[sfreq].l[4] <= sb)
326                     {
327                         sfb = 4;
328                     }
329                     else
330                     {
331                         sfb = 0;
332                     }
333 
334                     while (mp3_sfBandIndex[sfreq].l[sfb] < sb)
335                     {
336                         sfb++;
337                     }
338 
339                     /* from that sfb on intensity stereo */
340                     sfbTemp = sfb;  /* save for later use */
341 
342                     sfbStart = mp3_sfBandIndex[sfreq].l[sfb];
343 
344                     /* from 0 up to sfbStart do ms_stereo or normal stereo */
345 
346                     if (ms_stereo)
347                     {
348                         pvmp3_st_mid_side(xr, xl, 0, sfbStart);
349                     }
350 
351                     /* 2. long blocks from intensity border up to sfb band 8: intensity */
352                     /* calc. is_ratio */
353 
354 
355                     /* Start of intensity stereo of remaining sfc bands: */
356                     for (; sfbTemp < 8; sfbTemp++)
357                     {
358                         sfbStart = mp3_sfBandIndex[sfreq].l[sfbTemp];  /* = Start in 0 ... 575 */
359                         sfbNo = mp3_sfBandIndex[sfreq].l[sfbTemp+1] - mp3_sfBandIndex[sfreq].l[sfbTemp]; /* No of lines to process */
360 
361                         if (scalefac->l[sfbTemp] != 7)
362                         {
363                             pvmp3_st_intensity(xr, xl, scalefac->l[sfbTemp], sfbStart, sfbNo);
364                         }
365                         else if (ms_stereo)
366                         {
367                             pvmp3_st_mid_side(xr, xl, sfbStart, sfbNo);
368                         }
369 
370                     }  /* for (; sfbTemp < 8; sfbTemp++) */
371 
372                     for (j = 0; j < 3; j++)
373                     {
374                         /* 3. short blocks from sfbcnt to last sfb do intensity stereo */
375                         for (sfbTemp = 3; sfbTemp < 13; sfbTemp++)
376                         {
377                             sfbNo = mp3_sfBandIndex[sfreq].s[sfbTemp+1] - mp3_sfBandIndex[sfreq].s[sfbTemp]; /* No of lines to process */
378                             sfbStart = 3 * mp3_sfBandIndex[sfreq].s[sfbTemp] + j * sfbNo;
379 
380                             if (scalefac->s[j][sfbTemp] != 7)
381                             {
382                                 pvmp3_st_intensity(xr, xl, scalefac->s[j][sfbTemp], sfbStart, sfbNo);
383                             }
384                             else if (ms_stereo)
385                             {
386                                 pvmp3_st_mid_side(xr, xl, sfbStart, sfbNo);
387                             }
388 
389                         }  /* for (; sfbTemp < 22; sfbTemp++) */
390                     } /* for (j = 0; j < 3; j++) */
391                 }
392                 else   /* else for (sb >= 36) */
393                 {
394                     /*
395                      * mixed blocks processing: intensity bound outside long blocks
396                      */
397 
398 
399                     /*
400                      * 2. short blocks from sfb band 3 up to intensity border: normal stereo, ms stereo and intensity
401                      */
402                     for (j = 0; j < 3; j++)
403                     {
404                         int32 sfbcnt;
405                         sfbcnt = -1;
406 
407                         for (sfb = 12; sfb >= 3; sfb--)
408                         {
409                             int32 lines;
410                             lines = mp3_sfBandIndex[sfreq].s[sfb+1] - mp3_sfBandIndex[sfreq].s[sfb];
411                             i = 3 * mp3_sfBandIndex[sfreq].s[sfb] + (j + 1) * lines - 1;
412 
413                             while (lines > 0)
414                             {
415                                 if (xl[i])
416                                 {
417                                     sfbcnt = sfb;
418                                     sfb = -10;
419                                     lines = -10;
420                                 }
421                                 lines--;
422                                 i--;
423                             }
424                         }
425 
426                         sfbcnt += 1;
427                         if (sfbcnt < 3)
428                         {
429                             sfbcnt = 3;
430                         }
431 
432                         sfbTemp = sfbcnt;        /* for later use */
433 
434 
435                         /*
436                          *   do normal stereo or MS stereo from sfb 3 to < sfbcnt:
437                          */
438                         for (sb = 3; sb < sfbcnt; sb++)
439                         {
440                             sfbNo = mp3_sfBandIndex[sfreq].s[sb+1] - mp3_sfBandIndex[sfreq].s[sb];
441                             sfbStart = 3 * mp3_sfBandIndex[sfreq].s[sb] + j * sfbNo;
442 
443                             if (ms_stereo)
444                             {
445                                 pvmp3_st_mid_side(xr, xl, sfbStart, sfbNo);
446                             }
447 
448                         }
449 
450                         /* from sfbcnt to last sfb do intensity stereo */
451                         for (; sfbTemp < 13; sfbTemp++)
452                         {
453                             sfbNo = mp3_sfBandIndex[sfreq].s[sfbTemp+1] - mp3_sfBandIndex[sfreq].s[sfbTemp]; /* No of lines to process */
454                             sfbStart = 3 * mp3_sfBandIndex[sfreq].s[sfbTemp] + j * sfbNo;
455 
456                             if (scalefac->s[j][sfbTemp] != 7)
457                             {
458                                 pvmp3_st_intensity(xr, xl, scalefac->s[j][sfbTemp], sfbStart, sfbNo);
459                             }
460                             else if (ms_stereo)
461                             {
462                                 pvmp3_st_mid_side(xr, xl, sfbStart, sfbNo);
463                             }
464 
465                         }  /* for (; sfbTemp < 22; sfbTemp++) */
466 
467                     } /* for (j = 0; j < 3; j++) */
468 
469                     /* 1. long blocks up to sfb band 8: not intensity */
470                     /* from 0 to sfb 8 ms_stereo or normal stereo */
471 
472                     sfbStart = mp3_sfBandIndex[sfreq].l[8];
473 
474                     if (ms_stereo)
475                     {
476                         pvmp3_st_mid_side(xr, xl, 0, sfbStart);
477                     }
478 
479                 }
480             }  /* if (gr_info->mixed_block_flag) */
481             else
482             {
483                 /*
484                  * short block processing
485                  */
486                 for (j = 0; j < 3; j++)
487                 {
488                     int32 sfbcnt = -1;
489 
490                     for (sfb = 12; sfb >= 0; sfb--)
491                     {
492                         int32 lines = mp3_sfBandIndex[sfreq].s[sfb+1] - mp3_sfBandIndex[sfreq].s[sfb];
493                         i = 3 * mp3_sfBandIndex[sfreq].s[sfb] + (j + 1) * lines - 1;
494 
495                         while (lines > 0)
496                         {
497                             if (xl[i])
498                             {
499                                 sfbcnt = sfb;
500                                 sfb = -10;
501                                 lines = -10;
502                             }
503                             lines--;
504                             i--;
505                         }
506                     }
507 
508                     sfbcnt += 1;
509                     sfbTemp = sfbcnt;        /* for later use */
510 
511                     /* do normal stereo or MS stereo from 0 to sfbcnt */
512                     for (sb = 0; sb < sfbcnt; sb++)
513                     {
514                         sfbNo = mp3_sfBandIndex[sfreq].s[sb+1] - mp3_sfBandIndex[sfreq].s[sb];
515                         sfbStart = 3 * mp3_sfBandIndex[sfreq].s[sb] + j * sfbNo;
516 
517                         if (ms_stereo)
518                         {
519                             pvmp3_st_mid_side(xr, xl, sfbStart, sfbNo);
520                         }
521                     }
522 
523 
524                     /* from sfbcnt to last sfb do intensity stereo */
525                     for (; sfbTemp < 13; sfbTemp++)
526                     {
527                         sfbNo = mp3_sfBandIndex[sfreq].s[sfbTemp+1] - mp3_sfBandIndex[sfreq].s[sfbTemp]; /* No of lines to process */
528                         sfbStart = 3 * mp3_sfBandIndex[sfreq].s[sfbTemp] + j * sfbNo;
529 
530                         if (scalefac->s[j][sfbTemp] != 7)
531                         {
532                             pvmp3_st_intensity(xr, xl, scalefac->s[j][sfbTemp], sfbStart, sfbNo);
533                         }
534                         else if (ms_stereo)
535                         {
536                             pvmp3_st_mid_side(xr, xl, sfbStart, sfbNo);
537                         }
538 
539                     }  /* for (; sfbTemp < 22; sfbTemp++) */
540 
541                 } /* for (j = 0; j < 3; j++) */
542 
543             } /* if( gr_info->mixed_block_flag) */
544 
545 
546 
547         }  /* if (gr_info->window_switching_flag && (gr_info->block_type == 2)) */
548         else
549         {
550             /*
551              *   long block processing
552              */
553             i = 31;
554             ss = 17;
555             sb = 0;
556 
557             while (i >= 0)
558             {
559                 if (xl[(i*FILTERBANK_BANDS) + ss] != 0)
560                 {
561                     sb = (i << 4) + (i << 1) + ss;
562                     i = -2;
563                 }
564                 else
565                 {
566                     ss--;
567                     if (ss < 0)
568                     {
569                         i--;
570                         ss = 17;
571                     }
572                 }
573             }
574 
575             if (sb)
576             {
577                 if (mp3_sfBandIndex[sfreq].l[14] <= sb)
578                 {
579                     sfb = 14;
580                 }
581                 else if (mp3_sfBandIndex[sfreq].l[7] <= sb)
582                 {
583                     sfb = 7;
584                 }
585                 else
586                 {
587                     sfb = 0;
588                 }
589 
590 
591                 while (mp3_sfBandIndex[sfreq].l[sfb] <= sb)
592                 {
593                     sfb++;
594                 }
595             }
596             else
597             {
598                 if (i == -1)
599                 {
600                     /*  all xr[1][][] are 0: set IS bound sfb to 0  */
601                     sfb = 0;
602                 }
603                 else
604                 {
605                     /*  xr[1][0][0] is unequal 0 and all others are 0: set IS bound sfb to 1 */
606                     sfb = 1;
607                 }
608             }
609 
610             sfbTemp = sfb;  /* save for later use */
611 
612 
613             sfbStart = mp3_sfBandIndex[sfreq].l[sfb];
614 
615             /* from 0 to sfbStart ms_stereo or normal stereo */
616             if (ms_stereo)
617             {
618                 pvmp3_st_mid_side(xr, xl, 0, sfbStart);
619             }
620 
621             /* now intensity stereo of the remaining sfb's: */
622             for (; sfb < 21; sfb++)
623             {
624                 sfbStart = mp3_sfBandIndex[sfreq].l[sfb];
625                 sfbNo = mp3_sfBandIndex[sfreq].l[sfb+1] - mp3_sfBandIndex[sfreq].l[sfb]; /* No of lines to process */
626 
627                 if (scalefac->l[sfb] != 7)
628                 {
629                     pvmp3_st_intensity(xr, xl, scalefac->l[sfb], sfbStart, sfbNo);
630                 }
631                 else if (ms_stereo)
632                 {
633                     pvmp3_st_mid_side(xr, xl, sfbStart, sfbNo);
634                 }
635 
636             }  /* for (; sfbTemp < 22; sfbTemp++) */
637 
638 
639 
640             sfbStart = mp3_sfBandIndex[sfreq].l[21];
641             sfbNo = mp3_sfBandIndex[sfreq].l[22] - mp3_sfBandIndex[sfreq].l[21]; /* No of lines to process */
642 
643             if (scalefac->l[21] != 7)
644             {
645                 if (sfbTemp < 21)
646                 {
647                     sfbTemp = scalefac->l[20];
648                 }
649                 else
650                 {
651                     sfbTemp = 0;  /* if scalefac[20] is not an intensity position, is_pos = 0 */
652                 }
653 
654                 pvmp3_st_intensity(xr, xl, sfbTemp, sfbStart, sfbNo);
655             }
656             else if (ms_stereo)
657             {
658                 pvmp3_st_mid_side(xr, xl, sfbStart, sfbNo);
659             }
660 
661         }  /* if (gr_info->window_switching_flag && (gr_info->block_type == 2)) */
662 
663 
664     }  /* if (i_stereo)  */
665     else
666     {
667         /*
668          * normal or ms stereo processing
669          */
670         if (ms_stereo)
671         {
672 
673             pvmp3_st_mid_side(xr, xl, 0, used_freq_lines);
674 
675         }
676 
677     } /* if (i_stereo) */
678 
679 }
680 
681