~ubuntu-branches/ubuntu/feisty/avidemux/feisty

« back to all changes in this revision

Viewing changes to avidemux/ADM_audio/ADM_mp3info.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Christian Marillat
  • Date: 2005-05-25 13:02:29 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20050525130229-jw94cav0yhmg7vjw
Tags: 1:2.0.40-0.0
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
#include "config.h"
22
 
 
23
 
#include <stdio.h>
24
 
#include <stdlib.h>
25
 
#include <string.h>
26
 
#include <ADM_assert.h>
27
 
#include <math.h>
28
 
 
29
 
 
30
 
#include "ADM_library/default.h"
31
 
#include "aviaudio.hxx"
32
 
#include "ADM_audio/ADM_mp3info.h"
33
 
 
34
 
static  uint32_t MP3Fq[4] = { 44100, 48000, 32000, 0 };       
35
 
static uint32_t MP2Fq[4] = { 22050, 24000, 16000, 0 };       
36
 
static uint32_t Bitrate[8][16]=
37
 
{
38
 
        // Level 1 / Layer 0
39
 
        {0,0,0,0,0,0,0,0   ,0,0,0,0,0,0,0,0},
40
 
        // Level 1/ Layer 1
41
 
        {0,32,64,96,128,160,192,224   ,256,288,320,352,384,416,448,0},
42
 
        // Level 1/Layer 2
43
 
        { 0, 32, 48, 56,64, 80, 96, 112,     128, 160, 192, 224,   256, 320, 384, 0},
44
 
        //Level 1 / Layer 3
45
 
        { 0, 32, 40, 48, 56, 64, 80, 96,         112, 128, 160, 192,  224, 256, 320, 0},
46
 
        // Level 2/Layer 0
47
 
        {0,0,0,0,0,0,0,0   ,0,0,0,0,0,0,0,0},
48
 
        // Level 2 Layer 1
49
 
        { 0, 32, 48, 56,64, 80, 96, 112,          128, 144, 160,176,  192, 224, 256, 0},
50
 
        // Level 2 Layer 2
51
 
        { 0, 8, 16, 24,  32, 40, 48, 56,    64, 80, 96, 112,    128, 144, 160, 0 },
52
 
        // Id for Layer 3
53
 
        { 0, 8, 16, 24,  32, 40, 48, 56,    64, 80, 96, 112,    128, 144, 160, 0 },
54
 
};
55
 
 
56
 
 
57
 
uint8_t getMpegFrameInfo(uint8_t *stream,uint32_t maxSearch, MpegAudioInfo *mpegInfo,MpegAudioInfo *templ,uint32_t *offset)
58
 
{
59
 
uint32_t start=0,found=0;
60
 
uint8_t  a[4];
61
 
uint32_t nfq,fqindex,brindex,index;
62
 
                        memcpy(a+1,stream,3);
63
 
                        do
64
 
                        {
65
 
                                
66
 
                                memmove(a,a+1,3);
67
 
                                a[3]=stream[start+3];
68
 
                                if(start>=maxSearch-3) break;
69
 
                                start++;
70
 
                                if(a[0]==0xff && ((a[1]&0xF0)==0xF0))
71
 
                                {
72
 
                                        // Layer
73
 
                                        mpegInfo->layer=4-(a[1]>>1)&3;  
74
 
                                        mpegInfo->level=4-(a[1]>>3)&3;
75
 
                                        if(mpegInfo->level==3) continue;
76
 
                                        if(mpegInfo->level==4) mpegInfo->level=3;
77
 
                                        mpegInfo->protect=(a[1]&1)^1;
78
 
                                        mpegInfo->padding=(a[2]>>1)&1;
79
 
                                        mpegInfo->mode=(a[3])>>6;
80
 
                                                                                
81
 
                                        fqindex=(a[2]>>2)&3;
82
 
                                        brindex=(a[2]>>4);
83
 
                                        
84
 
                                        // Remove impossible case
85
 
                                        if(mpegInfo->layer==0) continue;
86
 
                                        // Check fq
87
 
                                        switch(mpegInfo->level)
88
 
                                        {
89
 
                                                case 1: mpegInfo->samplerate=MP3Fq[fqindex];break;
90
 
                                                case 2: mpegInfo->samplerate=MP2Fq[fqindex];break;
91
 
                                                case 3: mpegInfo->samplerate=MP2Fq[fqindex]>>1;break;
92
 
                                                default: mpegInfo->samplerate=0;break;
93
 
                                        }
94
 
                                        // impossible fq
95
 
                                        if(!mpegInfo->samplerate) continue;
96
 
                                        // Bitrate now
97
 
                                        
98
 
                                        // Compute bitrate
99
 
                                        switch(mpegInfo->level)
100
 
                                        {
101
 
                                                case 2:
102
 
                                                case 3:
103
 
                                                        index=4+mpegInfo->layer;
104
 
                                                        break;
105
 
                                                case 1:
106
 
                                                        index=mpegInfo->layer;  
107
 
                                                        break;
108
 
                                                default:
109
 
                                                        continue;
110
 
                                        }
111
 
                                        
112
 
                                        mpegInfo->bitrate=Bitrate[index][brindex];
113
 
                                        if(!mpegInfo->bitrate) continue;
114
 
                                        
115
 
                                        // Check consistency
116
 
                                        if(templ)
117
 
                                        {
118
 
                                                if(templ->samplerate!=mpegInfo->samplerate) continue;
119
 
                                        
120
 
                                        }
121
 
                                        found=1;
122
 
                                }
123
 
                                
124
 
                        }while(!found && start<maxSearch-4);
125
 
                        if(!found)
126
 
                                {       
127
 
                                        return 0;
128
 
                                }
129
 
/*      */
130
 
                        // Sample in the packet
131
 
                        if(mpegInfo->level==1)
132
 
                        {
133
 
                                if(1==mpegInfo->layer) 
134
 
                                        mpegInfo->samples=384;
135
 
                                else
136
 
                                        mpegInfo->samples=1152;
137
 
                                *offset=start-1;
138
 
                        }
139
 
                        else
140
 
                        {       // Mpeg2/2.5
141
 
                                if(1==mpegInfo->layer) 
142
 
                                        mpegInfo->samples=384;
143
 
                                else
144
 
                                        mpegInfo->samples=576;
145
 
                                *offset=start-1;
146
 
                        
147
 
                        }
148
 
                        
149
 
                        // Packet size
150
 
                        //L1:FrameLengthInBytes = (12 * BitRate / SampleRate + Padding) * 4
151
 
                
152
 
                        switch(mpegInfo->layer)
153
 
                        {
154
 
                                case 1:
155
 
                                        mpegInfo->size=((12*1000*mpegInfo->bitrate)/mpegInfo->samplerate)
156
 
                                                                +mpegInfo->padding;
157
 
                                        mpegInfo->size=mpegInfo->size*4;
158
 
                                        break;
159
 
                                default:
160
 
                                //FrameLengthInBytes = 144 * BitRate / SampleRate + Padding
161
 
                                        mpegInfo->size=(144*mpegInfo->bitrate*1000)/mpegInfo->samplerate;
162
 
                                        mpegInfo->size+=mpegInfo->padding;                      
163
 
                        }
164
 
                        if(*offset)
165
 
                                {
166
 
                                        printf("MP3: Skipped %lu bytes\n",*offset);
167
 
                                        
168
 
                                }
169
 
#if 0                   
170
 
                        printf("%02x %02x %02x %02x\n",a[0],a[1],a[2],a[3]);
171
 
                        printf("Packet found : at :%d level:%d layer:%d fq:%d bitrate:%d mode:%d\n",
172
 
                                        start-1,mpegInfo->level,mpegInfo->layer,mpegInfo->samplerate,
173
 
                                        mpegInfo->bitrate,mpegInfo->mode);
174
 
                        printf("Padd:%lu, crc on:%lu size:%lu\n",mpegInfo->padding,mpegInfo->protect,
175
 
                                                                mpegInfo->size);
176
 
#endif                          
177
 
                        
178
 
                        return 1;
179
 
                        
180
 
 
181
 
        
182
 
 
183
 
}
184
 
//____________
185
 
 
 
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
#include "config.h"
 
22
 
 
23
#include <stdio.h>
 
24
#include <stdlib.h>
 
25
#include <string.h>
 
26
#include <ADM_assert.h>
 
27
#include <math.h>
 
28
 
 
29
 
 
30
#include "ADM_library/default.h"
 
31
#include "aviaudio.hxx"
 
32
#include "ADM_audio/ADM_mp3info.h"
 
33
 
 
34
static  uint32_t MP3Fq[4] = { 44100, 48000, 32000, 0 };       
 
35
static uint32_t MP2Fq[4] = { 22050, 24000, 16000, 0 };       
 
36
static uint32_t Bitrate[8][16]=
 
37
{
 
38
        // Level 1 / Layer 0
 
39
        {0,0,0,0,0,0,0,0   ,0,0,0,0,0,0,0,0},
 
40
        // Level 1/ Layer 1
 
41
        {0,32,64,96,128,160,192,224   ,256,288,320,352,384,416,448,0},
 
42
        // Level 1/Layer 2
 
43
        { 0, 32, 48, 56,64, 80, 96, 112,     128, 160, 192, 224,   256, 320, 384, 0},
 
44
        //Level 1 / Layer 3
 
45
        { 0, 32, 40, 48, 56, 64, 80, 96,         112, 128, 160, 192,  224, 256, 320, 0},
 
46
        // Level 2/Layer 0
 
47
        {0,0,0,0,0,0,0,0   ,0,0,0,0,0,0,0,0},
 
48
        // Level 2 Layer 1
 
49
        { 0, 32, 48, 56,64, 80, 96, 112,          128, 144, 160,176,  192, 224, 256, 0},
 
50
        // Level 2 Layer 2
 
51
        { 0, 8, 16, 24,  32, 40, 48, 56,    64, 80, 96, 112,    128, 144, 160, 0 },
 
52
        // Id for Layer 3
 
53
        { 0, 8, 16, 24,  32, 40, 48, 56,    64, 80, 96, 112,    128, 144, 160, 0 },
 
54
};
 
55
 
 
56
 
 
57
uint8_t getMpegFrameInfo(uint8_t *stream,uint32_t maxSearch, MpegAudioInfo *mpegInfo,MpegAudioInfo *templ,uint32_t *offset)
 
58
{
 
59
uint32_t start=0,found=0;
 
60
uint8_t  a[4];
 
61
uint32_t nfq,fqindex,brindex,index;
 
62
                        memcpy(a+1,stream,3);
 
63
                        do
 
64
                        {
 
65
                                
 
66
                                memmove(a,a+1,3);
 
67
                                a[3]=stream[start+3];
 
68
                                if(start>=maxSearch-3) break;
 
69
                                start++;
 
70
                                if(a[0]==0xff && ((a[1]&0xF0)==0xF0))
 
71
                                {
 
72
                                        // Layer
 
73
                                        mpegInfo->layer=4-(a[1]>>1)&3;  
 
74
                                        mpegInfo->level=4-(a[1]>>3)&3;
 
75
                                        if(mpegInfo->level==3) continue;
 
76
                                        if(mpegInfo->level==4) mpegInfo->level=3;
 
77
                                        mpegInfo->protect=(a[1]&1)^1;
 
78
                                        mpegInfo->padding=(a[2]>>1)&1;
 
79
                                        mpegInfo->privatebit=(a[2]&1);
 
80
                                        mpegInfo->mode=(a[3])>>6;
 
81
                                        mpegInfo->mode_extension=((a[3])>>4)&3;
 
82
                                                                                
 
83
                                        fqindex=(a[2]>>2)&3;
 
84
                                        brindex=(a[2]>>4);
 
85
                                        
 
86
                                        // Remove impossible case
 
87
                                        if(mpegInfo->layer==0) continue;
 
88
                                        // Check fq
 
89
                                        if((a[1]>>4)&1)
 
90
                                        {
 
91
                                          mpegInfo->lsf=0;                                          
 
92
                                        }
 
93
                                        else
 
94
                                          mpegInfo->lsf=1;
 
95
                                        //
 
96
                                        switch(mpegInfo->level)
 
97
                                        {
 
98
                                                case 1: mpegInfo->samplerate=MP3Fq[fqindex];break;
 
99
                                                case 2: mpegInfo->samplerate=MP2Fq[fqindex];break;
 
100
                                                case 3: mpegInfo->samplerate=MP2Fq[fqindex]>>1;break;
 
101
                                                default: mpegInfo->samplerate=0;break;
 
102
                                        }
 
103
                                        // impossible fq
 
104
                                        if(!mpegInfo->samplerate) continue;
 
105
                                        // Bitrate now
 
106
                                        
 
107
                                        // Compute bitrate
 
108
                                        switch(mpegInfo->level)
 
109
                                        {
 
110
                                                case 2:
 
111
                                                case 3:
 
112
                                                        index=4+mpegInfo->layer;
 
113
                                                        break;
 
114
                                                case 1:
 
115
                                                        index=mpegInfo->layer;  
 
116
                                                        break;
 
117
                                                default:
 
118
                                                        continue;
 
119
                                        }
 
120
                                        
 
121
                                        mpegInfo->bitrate=Bitrate[index][brindex];
 
122
                                        if(!mpegInfo->bitrate) continue;
 
123
                                        
 
124
                                        // Check consistency
 
125
                                        if(templ)
 
126
                                        {
 
127
                                                if(templ->samplerate!=mpegInfo->samplerate) continue;
 
128
                                        
 
129
                                        }
 
130
                                        found=1;
 
131
                                }
 
132
                                
 
133
                        }while(!found && start<maxSearch-4);
 
134
                        if(!found)
 
135
                                {       
 
136
                                        return 0;
 
137
                                }
 
138
/*      */
 
139
                        // Sample in the packet
 
140
                        if(mpegInfo->level==1)
 
141
                        {
 
142
                                if(1==mpegInfo->layer) 
 
143
                                        mpegInfo->samples=384;
 
144
                                else
 
145
                                        mpegInfo->samples=1152;
 
146
                                *offset=start-1;
 
147
                        }
 
148
                        else
 
149
                        {       // Mpeg2/2.5
 
150
                                if(1==mpegInfo->layer) 
 
151
                                        mpegInfo->samples=384;
 
152
                                else
 
153
                                        mpegInfo->samples=576;
 
154
                                *offset=start-1;
 
155
                        
 
156
                        }
 
157
                        
 
158
                        // Packet size
 
159
                        //L1:FrameLengthInBytes = (12 * BitRate / SampleRate + Padding) * 4
 
160
                
 
161
                        switch(mpegInfo->layer)
 
162
                        {
 
163
                                case 1:
 
164
                                        mpegInfo->size=((12*1000*mpegInfo->bitrate)/mpegInfo->samplerate)
 
165
                                                                +mpegInfo->padding;
 
166
                                        mpegInfo->size=mpegInfo->size*4;
 
167
                                        break;
 
168
                                default:
 
169
                                //FrameLengthInBytes = 144 * BitRate / SampleRate + Padding
 
170
                                  uint32_t slot_per_frame;
 
171
                                        if(mpegInfo->layer==3 && mpegInfo->level==2)  slot_per_frame=72; 
 
172
                                                else slot_per_frame=144;
 
173
                                        mpegInfo->size=(slot_per_frame*mpegInfo->bitrate*1000)/mpegInfo->samplerate;
 
174
                                        mpegInfo->size+=mpegInfo->padding;                      
 
175
                        }
 
176
                        if(*offset)
 
177
                                {
 
178
                                        printf("MP3: Skipped %lu bytes\n",*offset);
 
179
                                        
 
180
                                }
 
181
#if 0           
 
182
                        printf("%02x %02x %02x %02x\n",a[0],a[1],a[2],a[3]);
 
183
                        printf("Packet found : at :%d level:%d layer:%d fq:%d bitrate:%d mode:%d\n",
 
184
                                        start-1,mpegInfo->level,mpegInfo->layer,mpegInfo->samplerate,
 
185
                                        mpegInfo->bitrate,mpegInfo->mode);
 
186
                        printf("Private :%d cop:%d ext:%d ",mpegInfo->privatebit,
 
187
                               0,mpegInfo->mode_extension);
 
188
                        printf("Padd:%lu, crc on:%lu size:%lu\n",mpegInfo->padding,mpegInfo->protect,
 
189
                                                                mpegInfo->size);
 
190
#endif                          
 
191
                        
 
192
                        return 1;
 
193
                        
 
194
 
 
195
        
 
196
 
 
197
}
 
198
//____________
 
199