1 //
2 // C++ Implementation: ADM_MP3Info
3 //
4 // Description:
5 //		Decode an mp3 frame an fill the info field
6 //			The second is a template to check we do not do bogus frame detection
7 //
8 // Author: mean <fixounet@free.fr>, (C) 2004
9 //
10 // Copyright: See COPYING file that comes with this distribution
11 //
12 //
13 /***************************************************************************
14  *                                                                         *
15  *   This program is free software; you can redistribute it and/or modify  *
16  *   it under the terms of the GNU General Public License as published by  *
17  *   the Free Software Foundation; either version 2 of the License, or     *
18  *   (at your option) any later version.                                   *
19  *                                                                         *
20  ***************************************************************************/
21 
22 
23 #include <math.h>
24 
25 
26 #include "ADM_default.h"
27 
28 #include "ADM_mp3info.h"
29 
30 static  uint32_t MP3Fq[4] = { 44100, 48000, 32000, 0 };
31 static uint32_t MP2Fq[4] = { 22050, 24000, 16000, 0 };
32 static uint32_t Bitrate[8][16]=
33 {
34 	// Level 1 / Layer 0
35 	{0,0,0,0,0,0,0,0   ,0,0,0,0,0,0,0,0},
36 	// Level 1/ Layer 1
37 	{0,32,64,96,128,160,192,224   ,256,288,320,352,384,416,448,0},
38 	// Level 1/Layer 2
39 	{ 0, 32, 48, 56,64, 80, 96, 112,     128, 160, 192, 224,   256, 320, 384, 0},
40 	//Level 1 / Layer 3
41 	{ 0, 32, 40, 48, 56, 64, 80, 96,         112, 128, 160, 192,  224, 256, 320, 0},
42     //-----------------------
43 	// Level 2/Layer 0
44 	{0,0,0,0,0,0,0,0   ,0,0,0,0,0,0,0,0},
45 	// Level 2 Layer 1
46 	{ 0, 32, 48, 56,64, 80, 96, 112,          128, 144, 160,176,  192, 224, 256, 0},
47 	// Level 2 Layer 2
48 	{ 0, 8, 16, 24,  32, 40, 48, 56,    64, 80, 96, 112,    128, 144, 160, 0 },
49 	// Id for Layer 3
50 	{ 0, 8, 16, 24,  32, 40, 48, 56,    64, 80, 96, 112,    128, 144, 160, 0 },
51 };
52 
53 
getMpegFrameInfo(const uint8_t * stream,uint32_t maxSearch,MpegAudioInfo * mpegInfo,MpegAudioInfo * templ,uint32_t * offset)54 uint8_t	getMpegFrameInfo(const uint8_t *stream,uint32_t maxSearch, MpegAudioInfo *mpegInfo,MpegAudioInfo *templ,uint32_t *offset)
55 {
56 uint32_t start=0,found=0;
57 uint8_t  a[4];
58 uint32_t nfq,fqindex,brindex,index;
59 			memcpy(a+1,stream,3);
60 			do
61 			{
62 				memmove(a,a+1,3);
63 				a[3]=stream[start+3];
64 				if(start>=maxSearch-3) break;
65 				start++;
66 				if(a[0]==0xff && ((a[1]&0xE0)==0xE0))
67 				{
68 					// Layer
69 					mpegInfo->layer=4-((a[1]>>1)&3);
70 					mpegInfo->level=4-((a[1]>>3)&3);
71 					if(mpegInfo->level==3) continue;
72 					if(mpegInfo->level==4)
73                     {
74                             mpegInfo->level=3; // mpeg 2.5
75                     }
76 					mpegInfo->protect=(a[1]&1)^1;
77 					mpegInfo->padding=(a[2]>>1)&1;
78                                         mpegInfo->privatebit=(a[2]&1);
79 					mpegInfo->mode=(a[3])>>6;
80                                         mpegInfo->mode_extension=((a[3])>>4)&3;
81 
82 					fqindex=(a[2]>>2)&3;
83 					brindex=(a[2]>>4);
84 
85 					// Remove impossible case
86 					if(mpegInfo->layer==4) continue;
87 					// Check fq
88                                         if((a[1]>>4)&1)
89                                         {
90                                           mpegInfo->lsf=0;
91                                         }
92                                         else
93                                           mpegInfo->lsf=1;
94                                         //
95 					switch(mpegInfo->level)
96 					{
97 						case 1: mpegInfo->samplerate=MP3Fq[fqindex];break;
98 						case 2: mpegInfo->samplerate=MP2Fq[fqindex];break;
99 						case 3: mpegInfo->samplerate=MP2Fq[fqindex]>>1;break;
100 						default: mpegInfo->samplerate=0;break;
101 					}
102 					// impossible fq
103 					if(!mpegInfo->samplerate) continue;
104 					// Bitrate now
105 
106 					// Compute bitrate
107 					switch(mpegInfo->level)
108 					{
109 						case 2:
110 						case 3:
111 							index=4+mpegInfo->layer;
112 							break;
113 						case 1:
114 							index=mpegInfo->layer;
115 							break;
116 						default:
117 							continue;
118 					}
119 
120 					mpegInfo->bitrate=Bitrate[index][brindex];
121 					if(!mpegInfo->bitrate) continue;
122 
123 					// Check consistency
124 					if(templ)
125 					{
126 						if(templ->samplerate!=mpegInfo->samplerate)
127                                                 {
128                                                   printf("[MP3]samplerate does not match\n");
129                                                   continue;
130                                                 }
131 
132 					}
133 					found=1;
134 				}
135 
136 			}while(!found && start<maxSearch-4);
137 			if(!found)
138 				{
139 					return 0;
140 				}
141 /*	*/
142 #if 0
143 			// Sample in the packet
144 			if(mpegInfo->level==1)
145 			{
146 				if(1==mpegInfo->layer)
147 					mpegInfo->samples=384;
148 				else
149 					mpegInfo->samples=1152;
150 				*offset=start-1;
151 			}
152 			else
153 			{	// Mpeg2/2.5
154 				if(1==mpegInfo->layer)
155 					mpegInfo->samples=384;
156 				else
157 					mpegInfo->samples=576;
158 				*offset=start-1;
159 
160 			}
161 #else
162             switch(mpegInfo->layer)
163             {
164                 case 1: mpegInfo->samples=384;break;
165                 case 2: mpegInfo->samples=1152;break;
166                 default:
167                 case 3: if(mpegInfo->level==1)  // mpeg 2.5
168                                 mpegInfo->samples=1152;
169                         else
170                                 mpegInfo->samples=576;
171                         break;
172             }
173             *offset=start-1;
174 #endif
175 			// Packet size
176 			//L1:FrameLengthInBytes = (12 * BitRate / SampleRate + Padding) * 4
177 
178 			switch(mpegInfo->layer)
179 			{
180 				case 1:
181 					mpegInfo->size=((12*1000*mpegInfo->bitrate)/mpegInfo->samplerate)
182 								+mpegInfo->padding;
183 					mpegInfo->size=mpegInfo->size*4;
184 					break;
185 				default:
186 				//FrameLengthInBytes = 144 * BitRate / SampleRate + Padding
187                   uint32_t slot_per_frame;
188                         if(mpegInfo->layer==3 && (mpegInfo->level&2)==2)  slot_per_frame=72;
189                                 else slot_per_frame=144;
190                         mpegInfo->size=(slot_per_frame*mpegInfo->bitrate*1000)/mpegInfo->samplerate;
191 					mpegInfo->size+=mpegInfo->padding;
192 			}
193 			if(*offset)
194 				{
195 					printf("MP3: Skipped %" PRIu32" bytes\n",*offset);
196 
197 				}
198 #if 0
199 			printf("%02x %02x %02x %02x\n",a[0],a[1],a[2],a[3]);
200 			printf("Packet found : at :%d level:%d layer:%d fq:%d bitrate:%d mode:%d\n",
201 					start-1,mpegInfo->level,mpegInfo->layer,mpegInfo->samplerate,
202 					mpegInfo->bitrate,mpegInfo->mode);
203                         printf("Private :%d cop:%d ext:%d ",mpegInfo->privatebit,
204                                0,mpegInfo->mode_extension);
205 			printf("Padd:%lu, crc on:%lu size:%lu\n",mpegInfo->padding,mpegInfo->protect,
206 								mpegInfo->size);
207 #endif
208 
209 			return 1;
210 
211 
212 
213 
214 }
215 //____________
216 
217