~ubuntu-branches/debian/wheezy/vlc/wheezy

« back to all changes in this revision

Viewing changes to extras/faad2/common/mp4av/audio.cpp

Tags: upstream-0.7.2.final
ImportĀ upstreamĀ versionĀ 0.7.2.final

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * The contents of this file are subject to the Mozilla Public
 
3
 * License Version 1.1 (the "License"); you may not use this file
 
4
 * except in compliance with the License. You may obtain a copy of
 
5
 * the License at http://www.mozilla.org/MPL/
 
6
 * 
 
7
 * Software distributed under the License is distributed on an "AS
 
8
 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
 
9
 * implied. See the License for the specific language governing
 
10
 * rights and limitations under the License.
 
11
 * 
 
12
 * The Original Code is MPEG4IP.
 
13
 * 
 
14
 * The Initial Developer of the Original Code is Cisco Systems Inc.
 
15
 * Portions created by Cisco Systems Inc. are
 
16
 * Copyright (C) Cisco Systems Inc. 2000-2002.  All Rights Reserved.
 
17
 * 
 
18
 * Contributor(s): 
 
19
 *              Dave Mackie             dmackie@cisco.com
 
20
 */
 
21
 
 
22
/* 
 
23
 * Notes:
 
24
 *  - file formatted with tabstops == 4 spaces 
 
25
 */
 
26
 
 
27
#include <mp4av_common.h>
 
28
 
 
29
static MP4AV_Mp3Header GetMp3Header(
 
30
        MP4FileHandle mp4File, 
 
31
        MP4TrackId audioTrackId)
 
32
{
 
33
        u_int8_t* pMp3Frame = NULL;
 
34
        u_int32_t mp3FrameLength = 0;
 
35
 
 
36
        bool rc = MP4ReadSample(
 
37
                mp4File,
 
38
                audioTrackId,
 
39
                1,
 
40
                &pMp3Frame,
 
41
                &mp3FrameLength);
 
42
 
 
43
        if (!rc || mp3FrameLength < 4) {
 
44
                return 0;
 
45
        }
 
46
 
 
47
        MP4AV_Mp3Header mp3Hdr =
 
48
                MP4AV_Mp3HeaderFromBytes(pMp3Frame);
 
49
        free(pMp3Frame);
 
50
 
 
51
        return mp3Hdr;
 
52
}
 
53
 
 
54
extern "C" u_int8_t MP4AV_AudioGetChannels(
 
55
        MP4FileHandle mp4File, 
 
56
        MP4TrackId audioTrackId)
 
57
{
 
58
        u_int8_t audioType = 
 
59
                MP4GetTrackEsdsObjectTypeId(mp4File, audioTrackId);
 
60
 
 
61
        if (audioType == MP4_INVALID_AUDIO_TYPE) {
 
62
                return 0;
 
63
        }
 
64
 
 
65
        if (MP4_IS_MP3_AUDIO_TYPE(audioType)) {
 
66
                MP4AV_Mp3Header mp3Hdr =
 
67
                        GetMp3Header(mp4File, audioTrackId);
 
68
 
 
69
                if (mp3Hdr == 0) {
 
70
                        return 0;
 
71
                }
 
72
                return MP4AV_Mp3GetChannels(mp3Hdr);
 
73
 
 
74
        } else if (MP4_IS_AAC_AUDIO_TYPE(audioType)) {
 
75
                u_int8_t* pAacConfig = NULL;
 
76
                u_int32_t aacConfigLength;
 
77
 
 
78
                MP4GetTrackESConfiguration(
 
79
                        mp4File, 
 
80
                        audioTrackId,
 
81
                        &pAacConfig,
 
82
                        &aacConfigLength);
 
83
 
 
84
                if (pAacConfig == NULL || aacConfigLength < 2) {
 
85
                        return 0;
 
86
                }
 
87
 
 
88
                u_int8_t channels =
 
89
                        MP4AV_AacConfigGetChannels(pAacConfig);
 
90
 
 
91
                free(pAacConfig);
 
92
 
 
93
                return channels;
 
94
 
 
95
        } else if ((audioType == MP4_PCM16_LITTLE_ENDIAN_AUDIO_TYPE) ||
 
96
        (audioType == MP4_PCM16_BIG_ENDIAN_AUDIO_TYPE)) {
 
97
                u_int32_t samplesPerFrame =
 
98
                        MP4GetSampleSize(mp4File, audioTrackId, 1) / 2;
 
99
 
 
100
                MP4Duration frameDuration =
 
101
                        MP4GetSampleDuration(mp4File, audioTrackId, 1);
 
102
 
 
103
                if (frameDuration == 0) {
 
104
                        return 0;
 
105
                }
 
106
 
 
107
                // assumes track time scale == sampling rate
 
108
                return samplesPerFrame / frameDuration;
 
109
        }
 
110
 
 
111
        return 0;
 
112
}
 
113
 
 
114
extern "C" u_int32_t MP4AV_AudioGetSamplingRate(
 
115
        MP4FileHandle mp4File, 
 
116
        MP4TrackId audioTrackId)
 
117
{
 
118
        u_int8_t audioType = 
 
119
                MP4GetTrackEsdsObjectTypeId(mp4File, audioTrackId);
 
120
 
 
121
        if (audioType == MP4_INVALID_AUDIO_TYPE) {
 
122
                return 0;
 
123
        }
 
124
 
 
125
        if (MP4_IS_MP3_AUDIO_TYPE(audioType)) {
 
126
                MP4AV_Mp3Header mp3Hdr =
 
127
                        GetMp3Header(mp4File, audioTrackId);
 
128
 
 
129
                if (mp3Hdr == 0) {
 
130
                        return 0;
 
131
                }
 
132
                return MP4AV_Mp3GetHdrSamplingRate(mp3Hdr);
 
133
 
 
134
        } else if (MP4_IS_AAC_AUDIO_TYPE(audioType)) {
 
135
                u_int8_t* pAacConfig = NULL;
 
136
                u_int32_t aacConfigLength;
 
137
 
 
138
                MP4GetTrackESConfiguration(
 
139
                        mp4File, 
 
140
                        audioTrackId,
 
141
                        &pAacConfig,
 
142
                        &aacConfigLength);
 
143
 
 
144
                if (pAacConfig == NULL || aacConfigLength < 2) {
 
145
                        return 0;
 
146
                }
 
147
 
 
148
                u_int32_t samplingRate =
 
149
                        MP4AV_AacConfigGetSamplingRate(pAacConfig);
 
150
 
 
151
                free(pAacConfig);
 
152
 
 
153
                return samplingRate;
 
154
 
 
155
        } else if ((audioType == MP4_PCM16_LITTLE_ENDIAN_AUDIO_TYPE)||
 
156
        (audioType == MP4_PCM16_BIG_ENDIAN_AUDIO_TYPE)) {
 
157
                return MP4GetTrackTimeScale(mp4File, audioTrackId);
 
158
        }
 
159
 
 
160
        return 0;
 
161
}
 
162
 
 
163
extern "C" u_int16_t MP4AV_AudioGetSamplingWindow(
 
164
        MP4FileHandle mp4File, 
 
165
        MP4TrackId audioTrackId)
 
166
{
 
167
        u_int8_t audioType = 
 
168
                MP4GetTrackEsdsObjectTypeId(mp4File, audioTrackId);
 
169
 
 
170
        if (audioType == MP4_INVALID_AUDIO_TYPE) {
 
171
                return 0;
 
172
        }
 
173
 
 
174
        if (MP4_IS_MP3_AUDIO_TYPE(audioType)) {
 
175
                MP4AV_Mp3Header mp3Hdr =
 
176
                        GetMp3Header(mp4File, audioTrackId);
 
177
 
 
178
                return MP4AV_Mp3GetHdrSamplingWindow(mp3Hdr);
 
179
 
 
180
        } else if (MP4_IS_AAC_AUDIO_TYPE(audioType)) {
 
181
                u_int8_t* pAacConfig = NULL;
 
182
                u_int32_t aacConfigLength;
 
183
 
 
184
                MP4GetTrackESConfiguration(
 
185
                        mp4File, 
 
186
                        audioTrackId,
 
187
                        &pAacConfig,
 
188
                        &aacConfigLength);
 
189
 
 
190
                if (pAacConfig == NULL || aacConfigLength < 2) {
 
191
                        return 0;
 
192
                }
 
193
 
 
194
                u_int32_t samplingWindow =
 
195
                        MP4AV_AacConfigGetSamplingWindow(pAacConfig);
 
196
 
 
197
                free(pAacConfig);
 
198
 
 
199
                return samplingWindow;
 
200
 
 
201
        } else if ((audioType == MP4_PCM16_LITTLE_ENDIAN_AUDIO_TYPE)||
 
202
        (audioType == MP4_PCM16_BIG_ENDIAN_AUDIO_TYPE)) {
 
203
                MP4Duration frameDuration =
 
204
                        MP4GetSampleDuration(mp4File, audioTrackId, 1);
 
205
 
 
206
                // assumes track time scale == sampling rate
 
207
                // and constant frame size was used
 
208
                return frameDuration;
 
209
        }
 
210
 
 
211
        return 0;
 
212
}