1 /*
2 ** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
3 ** Copyright (C) 2003 M. Bakker, Ahead Software AG, http://www.nero.com
4 **
5 ** This program is free software; you can redistribute it and/or modify
6 ** it under the terms of the GNU General Public License as published by
7 ** the Free Software Foundation; either version 2 of the License, or
8 ** (at your option) any later version.
9 **
10 ** This program is distributed in the hope that it will be useful,
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 ** GNU General Public License for more details.
14 **
15 ** You should have received a copy of the GNU General Public License
16 ** along with this program; if not, write to the Free Software
17 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 **
19 ** Any non-GPL usage of this software or parts of this software is strictly
20 ** forbidden.
21 **
22 ** Commercial non-GPL licensing of this software is possible.
23 ** For more info contact Ahead Software through Mpeg4AAClicense@nero.com.
24 **
25 ** $Id: utils.c,v 1.3 2003/12/06 04:24:17 rjamorim Exp $
26 **/
27 
28 #define WIN32_LEAN_AND_MEAN
29 #include <windows.h>
30 #include <mp4.h>
31 #include <faad.h>
32 #include "utils.h"
33 
StringComp(char const * str1,char const * str2,unsigned long len)34 int StringComp(char const *str1, char const *str2, unsigned long len)
35 {
36     signed int c1 = 0, c2 = 0;
37 
38     while (len--)
39     {
40         c1 = tolower(*str1++);
41         c2 = tolower(*str2++);
42 
43         if (c1 == 0 || c1 != c2)
44             break;
45     }
46 
47     return c1 - c2;
48 }
49 
GetAACTrack(MP4FileHandle infile)50 int GetAACTrack(MP4FileHandle infile)
51 {
52     /* find AAC track */
53     int i, rc;
54 	int numTracks = MP4GetNumberOfTracks(infile, NULL, 0);
55 
56 	for (i = 0; i < numTracks; i++)
57     {
58         MP4TrackId trackId = MP4FindTrackId(infile, i, NULL, 0);
59         const char* trackType = MP4GetTrackType(infile, trackId);
60 
61         if (!strcmp(trackType, MP4_AUDIO_TRACK_TYPE))
62         {
63             unsigned char *buff = NULL;
64             int buff_size = 0;
65             mp4AudioSpecificConfig mp4ASC;
66 
67             MP4GetTrackESConfiguration(infile, trackId, &buff, &buff_size);
68 
69             if (buff)
70             {
71                 rc = AudioSpecificConfig(buff, buff_size, &mp4ASC);
72                 free(buff);
73 
74                 if (rc < 0)
75                     return -1;
76                 return trackId;
77             }
78         }
79     }
80 
81     /* can't decode this */
82     return -1;
83 }
84 
GetAudioTrack(MP4FileHandle infile)85 int GetAudioTrack(MP4FileHandle infile)
86 {
87     /* find AAC track */
88     int i;
89 	int numTracks = MP4GetNumberOfTracks(infile, NULL, 0);
90 
91 	for (i = 0; i < numTracks; i++)
92     {
93         MP4TrackId trackId = MP4FindTrackId(infile, i, NULL, 0);
94         const char* trackType = MP4GetTrackType(infile, trackId);
95 
96         if (!strcmp(trackType, MP4_AUDIO_TRACK_TYPE))
97         {
98             return trackId;
99         }
100     }
101 
102     /* can't decode this */
103     return -1;
104 }
105 
GetVideoTrack(MP4FileHandle infile)106 int GetVideoTrack(MP4FileHandle infile)
107 {
108     /* find AAC track */
109     int i;
110 	int numTracks = MP4GetNumberOfTracks(infile, NULL, 0);
111 
112 	for (i = 0; i < numTracks; i++)
113     {
114         MP4TrackId trackId = MP4FindTrackId(infile, i, NULL, 0);
115         const char* trackType = MP4GetTrackType(infile, trackId);
116 
117         if (!strcmp(trackType, MP4_VIDEO_TRACK_TYPE))
118         {
119             return trackId;
120         }
121     }
122 
123     /* can't decode this */
124     return -1;
125 }
126 
PathFindFileName(LPCTSTR pPath)127 LPTSTR PathFindFileName(LPCTSTR pPath)
128 {
129     LPCTSTR pT;
130 
131     for (pT = pPath; *pPath; pPath = CharNext(pPath)) {
132         if ((pPath[0] == TEXT('\\') || pPath[0] == TEXT(':')) && pPath[1] && (pPath[1] != TEXT('\\')))
133             pT = pPath + 1;
134     }
135 
136     return (LPTSTR)pT;   // const -> non const
137 }
138 
convert3in4to3in3(void * sample_buffer,int samples)139 char *convert3in4to3in3(void *sample_buffer, int samples)
140 {
141     int i;
142     long *sample_buffer24 = (long*)sample_buffer;
143     char *data = malloc(samples*3*sizeof(char));
144 
145     for (i = 0; i < samples; i++)
146     {
147         data[i*3] = sample_buffer24[i] & 0xFF;
148         data[i*3+1] = (sample_buffer24[i] >> 8) & 0xFF;
149         data[i*3+2] = (sample_buffer24[i] >> 16) & 0xFF;
150     }
151 
152     return data;
153 }
154