• 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 
222 // deliberately plays near overflow points of int32
223 #if __has_attribute(no_sanitize)
224 __attribute__((no_sanitize("integer")))
225 #endif
pvmp3_st_intensity(int32 xr[SUBBANDS_NUMBER * FILTERBANK_BANDS],int32 xl[SUBBANDS_NUMBER * FILTERBANK_BANDS],int32 is_pos,int32 Start,int32 Number)226 void pvmp3_st_intensity(int32 xr[SUBBANDS_NUMBER*FILTERBANK_BANDS],
227                         int32 xl[SUBBANDS_NUMBER*FILTERBANK_BANDS],
228                         int32 is_pos,
229                         int32 Start,
230                         int32 Number)
231 {
232 
233     int32 TmpFac = is_ratio_factor[ is_pos & 7];
234 
235     int32 *pt_xr  = &xr[Start];
236     int32 *pt_xl  = &xl[Start];
237 
238     for (int32 i = Number >> 1; i != 0; i--)
239     {
240         int32 tmp = fxp_mul32_Q32((*pt_xr) << 1, TmpFac);
241         *(pt_xl++) = (*pt_xr) - tmp;
242         *(pt_xr++) = tmp;
243         tmp = fxp_mul32_Q32((*pt_xr) << 1, TmpFac);
244         *(pt_xl++) = (*pt_xr) - tmp;
245         *(pt_xr++) = tmp;
246     }
247 
248     if (Number&1)
249     {
250         int32 tmp = fxp_mul32_Q32((*pt_xr) << 1, TmpFac);
251         *(pt_xl) = (*pt_xr) - tmp;
252         *(pt_xr) = tmp;
253     }
254 
255 }
256 
257 /*----------------------------------------------------------------------------
258 ; FUNCTION CODE
259 ----------------------------------------------------------------------------*/
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)260 void pvmp3_stereo_proc(int32 xr[SUBBANDS_NUMBER*FILTERBANK_BANDS],
261                        int32 xl[SUBBANDS_NUMBER*FILTERBANK_BANDS],
262                        mp3ScaleFactors *scalefac,
263                        granuleInfo *gr_info,
264                        int32 used_freq_lines,
265                        mp3Header *info)
266 {
267 
268 
269     int32 sb;
270     int32 ss;
271     int32 sfbNo;
272     int32 sfbStart;
273 
274     int32 sfb;
275     int32 sfbTemp;
276     int32 i;
277     int32 j;
278 
279 
280     int32 i_stereo  = (info->mode == MPG_MD_JOINT_STEREO) &&
281                       (info->mode_ext & 0x1);
282 
283     int32 ms_stereo = (info->mode == MPG_MD_JOINT_STEREO) &&
284                       (info->mode_ext & 0x2);
285 
286     int32 sfreq  = info->version_x + (info->version_x << 1);
287     sfreq += info->sampling_frequency;
288 
289 
290 
291 
292     if (i_stereo)
293     {
294         if (gr_info->window_switching_flag && (gr_info->block_type == 2))
295         {
296             if (gr_info->mixed_block_flag)
297             {
298                 /*
299                  * mixed blocks processing
300                  */
301                 i = 31;
302                 ss = 17;
303                 sb = 0;
304                 while (i >= 0)
305                 {
306                     if (xl[(i*FILTERBANK_BANDS) + ss])
307                     {
308                         sb = (i << 4) + (i << 1) + ss;
309                         i = -1;
310                     }
311                     else
312                     {
313                         ss--;
314                         if (ss < 0)
315                         {
316                             i--;
317                             ss = 17;
318                         }
319                     }
320                 }
321 
322                 if (sb < 36)
323                 {
324                     /*
325                      * mixed blocks processing: intensity bound inside long blocks
326                      */
327                     /* 1. long blocks up to intensity border: not intensity */
328 
329                     if (mp3_sfBandIndex[sfreq].l[4] <= sb)
330                     {
331                         sfb = 4;
332                     }
333                     else
334                     {
335                         sfb = 0;
336                     }
337 
338                     while (mp3_sfBandIndex[sfreq].l[sfb] < sb)
339                     {
340                         sfb++;
341                     }
342 
343                     /* from that sfb on intensity stereo */
344                     sfbTemp = sfb;  /* save for later use */
345 
346                     sfbStart = mp3_sfBandIndex[sfreq].l[sfb];
347 
348                     /* from 0 up to sfbStart do ms_stereo or normal stereo */
349 
350                     if (ms_stereo)
351                     {
352                         pvmp3_st_mid_side(xr, xl, 0, sfbStart);
353                     }
354 
355                     /* 2. long blocks from intensity border up to sfb band 8: intensity */
356                     /* calc. is_ratio */
357 
358 
359                     /* Start of intensity stereo of remaining sfc bands: */
360                     for (; sfbTemp < 8; sfbTemp++)
361                     {
362                         sfbStart = mp3_sfBandIndex[sfreq].l[sfbTemp];  /* = Start in 0 ... 575 */
363                         sfbNo = mp3_sfBandIndex[sfreq].l[sfbTemp+1] - mp3_sfBandIndex[sfreq].l[sfbTemp]; /* No of lines to process */
364 
365                         if (scalefac->l[sfbTemp] != 7)
366                         {
367                             pvmp3_st_intensity(xr, xl, scalefac->l[sfbTemp], sfbStart, sfbNo);
368                         }
369                         else if (ms_stereo)
370                         {
371                             pvmp3_st_mid_side(xr, xl, sfbStart, sfbNo);
372                         }
373 
374                     }  /* for (; sfbTemp < 8; sfbTemp++) */
375 
376                     for (j = 0; j < 3; j++)
377                     {
378                         /* 3. short blocks from sfbcnt to last sfb do intensity stereo */
379                         for (sfbTemp = 3; sfbTemp < 13; sfbTemp++)
380                         {
381                             sfbNo = mp3_sfBandIndex[sfreq].s[sfbTemp+1] - mp3_sfBandIndex[sfreq].s[sfbTemp]; /* No of lines to process */
382                             sfbStart = 3 * mp3_sfBandIndex[sfreq].s[sfbTemp] + j * sfbNo;
383 
384                             if (scalefac->s[j][sfbTemp] != 7)
385                             {
386                                 pvmp3_st_intensity(xr, xl, scalefac->s[j][sfbTemp], sfbStart, sfbNo);
387                             }
388                             else if (ms_stereo)
389                             {
390                                 pvmp3_st_mid_side(xr, xl, sfbStart, sfbNo);
391                             }
392 
393                         }  /* for (; sfbTemp < 22; sfbTemp++) */
394                     } /* for (j = 0; j < 3; j++) */
395                 }
396                 else   /* else for (sb >= 36) */
397                 {
398                     /*
399                      * mixed blocks processing: intensity bound outside long blocks
400                      */
401 
402 
403                     /*
404                      * 2. short blocks from sfb band 3 up to intensity border: normal stereo, ms stereo and intensity
405                      */
406                     for (j = 0; j < 3; j++)
407                     {
408                         int32 sfbcnt;
409                         sfbcnt = -1;
410 
411                         for (sfb = 12; sfb >= 3; sfb--)
412                         {
413                             int32 lines;
414                             lines = mp3_sfBandIndex[sfreq].s[sfb+1] - mp3_sfBandIndex[sfreq].s[sfb];
415                             i = 3 * mp3_sfBandIndex[sfreq].s[sfb] + (j + 1) * lines - 1;
416 
417                             while (lines > 0)
418                             {
419                                 if (xl[i])
420                                 {
421                                     sfbcnt = sfb;
422                                     sfb = -10;
423                                     lines = -10;
424                                 }
425                                 lines--;
426                                 i--;
427                             }
428                         }
429 
430                         sfbcnt += 1;
431                         if (sfbcnt < 3)
432                         {
433                             sfbcnt = 3;
434                         }
435 
436                         sfbTemp = sfbcnt;        /* for later use */
437 
438 
439                         /*
440                          *   do normal stereo or MS stereo from sfb 3 to < sfbcnt:
441                          */
442                         for (sb = 3; sb < sfbcnt; sb++)
443                         {
444                             sfbNo = mp3_sfBandIndex[sfreq].s[sb+1] - mp3_sfBandIndex[sfreq].s[sb];
445                             sfbStart = 3 * mp3_sfBandIndex[sfreq].s[sb] + j * sfbNo;
446 
447                             if (ms_stereo)
448                             {
449                                 pvmp3_st_mid_side(xr, xl, sfbStart, sfbNo);
450                             }
451 
452                         }
453 
454                         /* from sfbcnt to last sfb do intensity stereo */
455                         for (; sfbTemp < 13; sfbTemp++)
456                         {
457                             sfbNo = mp3_sfBandIndex[sfreq].s[sfbTemp+1] - mp3_sfBandIndex[sfreq].s[sfbTemp]; /* No of lines to process */
458                             sfbStart = 3 * mp3_sfBandIndex[sfreq].s[sfbTemp] + j * sfbNo;
459 
460                             if (scalefac->s[j][sfbTemp] != 7)
461                             {
462                                 pvmp3_st_intensity(xr, xl, scalefac->s[j][sfbTemp], sfbStart, sfbNo);
463                             }
464                             else if (ms_stereo)
465                             {
466                                 pvmp3_st_mid_side(xr, xl, sfbStart, sfbNo);
467                             }
468 
469                         }  /* for (; sfbTemp < 22; sfbTemp++) */
470 
471                     } /* for (j = 0; j < 3; j++) */
472 
473                     /* 1. long blocks up to sfb band 8: not intensity */
474                     /* from 0 to sfb 8 ms_stereo or normal stereo */
475 
476                     sfbStart = mp3_sfBandIndex[sfreq].l[8];
477 
478                     if (ms_stereo)
479                     {
480                         pvmp3_st_mid_side(xr, xl, 0, sfbStart);
481                     }
482 
483                 }
484             }  /* if (gr_info->mixed_block_flag) */
485             else
486             {
487                 /*
488                  * short block processing
489                  */
490                 for (j = 0; j < 3; j++)
491                 {
492                     int32 sfbcnt = -1;
493 
494                     for (sfb = 12; sfb >= 0; sfb--)
495                     {
496                         int32 lines = mp3_sfBandIndex[sfreq].s[sfb+1] - mp3_sfBandIndex[sfreq].s[sfb];
497                         i = 3 * mp3_sfBandIndex[sfreq].s[sfb] + (j + 1) * lines - 1;
498 
499                         while (lines > 0)
500                         {
501                             if (xl[i])
502                             {
503                                 sfbcnt = sfb;
504                                 sfb = -10;
505                                 lines = -10;
506                             }
507                             lines--;
508                             i--;
509                         }
510                     }
511 
512                     sfbcnt += 1;
513                     sfbTemp = sfbcnt;        /* for later use */
514 
515                     /* do normal stereo or MS stereo from 0 to sfbcnt */
516                     for (sb = 0; sb < sfbcnt; sb++)
517                     {
518                         sfbNo = mp3_sfBandIndex[sfreq].s[sb+1] - mp3_sfBandIndex[sfreq].s[sb];
519                         sfbStart = 3 * mp3_sfBandIndex[sfreq].s[sb] + j * sfbNo;
520 
521                         if (ms_stereo)
522                         {
523                             pvmp3_st_mid_side(xr, xl, sfbStart, sfbNo);
524                         }
525                     }
526 
527 
528                     /* from sfbcnt to last sfb do intensity stereo */
529                     for (; sfbTemp < 13; sfbTemp++)
530                     {
531                         sfbNo = mp3_sfBandIndex[sfreq].s[sfbTemp+1] - mp3_sfBandIndex[sfreq].s[sfbTemp]; /* No of lines to process */
532                         sfbStart = 3 * mp3_sfBandIndex[sfreq].s[sfbTemp] + j * sfbNo;
533 
534                         if (scalefac->s[j][sfbTemp] != 7)
535                         {
536                             pvmp3_st_intensity(xr, xl, scalefac->s[j][sfbTemp], sfbStart, sfbNo);
537                         }
538                         else if (ms_stereo)
539                         {
540                             pvmp3_st_mid_side(xr, xl, sfbStart, sfbNo);
541                         }
542 
543                     }  /* for (; sfbTemp < 22; sfbTemp++) */
544 
545                 } /* for (j = 0; j < 3; j++) */
546 
547             } /* if( gr_info->mixed_block_flag) */
548 
549 
550 
551         }  /* if (gr_info->window_switching_flag && (gr_info->block_type == 2)) */
552         else
553         {
554             /*
555              *   long block processing
556              */
557             i = 31;
558             ss = 17;
559             sb = 0;
560 
561             while (i >= 0)
562             {
563                 if (xl[(i*FILTERBANK_BANDS) + ss] != 0)
564                 {
565                     sb = (i << 4) + (i << 1) + ss;
566                     i = -2;
567                 }
568                 else
569                 {
570                     ss--;
571                     if (ss < 0)
572                     {
573                         i--;
574                         ss = 17;
575                     }
576                 }
577             }
578 
579             if (sb)
580             {
581                 if (mp3_sfBandIndex[sfreq].l[14] <= sb)
582                 {
583                     sfb = 14;
584                 }
585                 else if (mp3_sfBandIndex[sfreq].l[7] <= sb)
586                 {
587                     sfb = 7;
588                 }
589                 else
590                 {
591                     sfb = 0;
592                 }
593 
594 
595                 while (mp3_sfBandIndex[sfreq].l[sfb] <= sb)
596                 {
597                     sfb++;
598                 }
599             }
600             else
601             {
602                 if (i == -1)
603                 {
604                     /*  all xr[1][][] are 0: set IS bound sfb to 0  */
605                     sfb = 0;
606                 }
607                 else
608                 {
609                     /*  xr[1][0][0] is unequal 0 and all others are 0: set IS bound sfb to 1 */
610                     sfb = 1;
611                 }
612             }
613 
614             sfbTemp = sfb;  /* save for later use */
615 
616 
617             sfbStart = mp3_sfBandIndex[sfreq].l[sfb];
618 
619             /* from 0 to sfbStart ms_stereo or normal stereo */
620             if (ms_stereo)
621             {
622                 pvmp3_st_mid_side(xr, xl, 0, sfbStart);
623             }
624 
625             /* now intensity stereo of the remaining sfb's: */
626             for (; sfb < 21; sfb++)
627             {
628                 sfbStart = mp3_sfBandIndex[sfreq].l[sfb];
629                 sfbNo = mp3_sfBandIndex[sfreq].l[sfb+1] - mp3_sfBandIndex[sfreq].l[sfb]; /* No of lines to process */
630 
631                 if (scalefac->l[sfb] != 7)
632                 {
633                     pvmp3_st_intensity(xr, xl, scalefac->l[sfb], sfbStart, sfbNo);
634                 }
635                 else if (ms_stereo)
636                 {
637                     pvmp3_st_mid_side(xr, xl, sfbStart, sfbNo);
638                 }
639 
640             }  /* for (; sfbTemp < 22; sfbTemp++) */
641 
642 
643 
644             sfbStart = mp3_sfBandIndex[sfreq].l[21];
645             sfbNo = mp3_sfBandIndex[sfreq].l[22] - mp3_sfBandIndex[sfreq].l[21]; /* No of lines to process */
646 
647             if (scalefac->l[21] != 7)
648             {
649                 if (sfbTemp < 21)
650                 {
651                     sfbTemp = scalefac->l[20];
652                 }
653                 else
654                 {
655                     sfbTemp = 0;  /* if scalefac[20] is not an intensity position, is_pos = 0 */
656                 }
657 
658                 pvmp3_st_intensity(xr, xl, sfbTemp, sfbStart, sfbNo);
659             }
660             else if (ms_stereo)
661             {
662                 pvmp3_st_mid_side(xr, xl, sfbStart, sfbNo);
663             }
664 
665         }  /* if (gr_info->window_switching_flag && (gr_info->block_type == 2)) */
666 
667 
668     }  /* if (i_stereo)  */
669     else
670     {
671         /*
672          * normal or ms stereo processing
673          */
674         if (ms_stereo)
675         {
676 
677             pvmp3_st_mid_side(xr, xl, 0, used_freq_lines);
678 
679         }
680 
681     } /* if (i_stereo) */
682 
683 }
684 
685