forked from fyhertz/libstreaming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMP4Parser.java
More file actions
251 lines (213 loc) · 6.68 KB
/
MP4Parser.java
File metadata and controls
251 lines (213 loc) · 6.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
* Copyright (C) 2011 GUIGUI Simon, [email protected]
*
* This file is part of Spydroid (http://code.google.com/p/spydroid-ipcamera/)
*
* Spydroid is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This source code is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this source code; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package net.majorkernelpanic.streaming.mp4;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.util.HashMap;
import android.util.Base64;
import android.util.Log;
/**
* Parse an mp4 file.
* An mp4 file contains a tree where each node has a name and a size.
* This class is used by H264Stream.java to determine the SPS and PPS parameters of a short video recorded by the phone.
*/
class MP4Parser {
private static final String TAG = "MP4Parser";
private HashMap<String, Long> boxes = new HashMap<String, Long>();
private final RandomAccessFile file;
private long pos = 0;
public MP4Parser(final String path) throws IOException, FileNotFoundException {
this.file = new RandomAccessFile(new File(path), "r");
}
/** Parses the mp4 file. **/
public void parse() throws IOException {
long length = 0;
try {
length = file.length();
} catch (IOException e) {
throw new IOException("Wrong size");
}
try {
parse("",length);
} catch (IOException e) {
throw new IOException("Parse error: malformed mp4 file");
}
}
/** Close the file opened when creating the MP4Parser. **/
public void close() {
try {
file.close();
} catch (IOException ignore) {}
}
public long getBoxPos(String box) throws IOException {
Long r = boxes.get(box);
if (r==null) throw new IOException("Box not found: "+box);
return boxes.get(box);
}
public StsdBox getStsdBox() throws IOException {
try {
return new StsdBox(file,getBoxPos("/moov/trak/mdia/minf/stbl/stsd"));
} catch (IOException e) {
throw new IOException("stsd box could not be found");
}
}
private void parse(String path, long len) throws IOException {
byte[] buffer = new byte[8];
String name="";
long sum = 0, newlen = 0;
if(!path.equals(""))
boxes.put(path, pos-8);
while (sum<len) {
file.read(buffer,0,8);
sum += 8;
pos += 8;
if (validBoxName(buffer)) {
ByteBuffer byteBuffer = ByteBuffer.wrap(buffer,0,4);
newlen = byteBuffer.getInt()-8;
// a) 1061109559+8 correspond to "????" in ASCII the HTC Desire S seems to write that sometimes, maybe other phones do
// b) wide atom would produce a newlen == 0, and we shouldn't throw an exception because of that
if (newlen < 0 || newlen == 1061109559) throw new IOException();
name = new String(buffer,4,4);
Log.d(TAG,"Atom -> name: "+name+" newlen: "+newlen+" pos: "+pos);
sum += newlen;
parse(path+'/'+name,newlen);
}
else {
if( len < 8){
file.seek(file.getFilePointer() - 8 + len);
sum += len-8;
} else {
int skipped = file.skipBytes((int)(len-8));
if (skipped < ((int)(len-8))) {
throw new IOException();
}
pos += len-8;
sum += len-8;
}
}
}
}
private boolean validBoxName(byte[] buffer) {
for (int i=0;i<4;i++) {
// If the next 4 bytes are neither lowercase letters nor numbers
if ((buffer[i+4]< 'a' || buffer[i+4]>'z') && (buffer[i+4]<'0'|| buffer[i+4]>'9') ) return false;
}
return true;
}
}
class StsdBox {
private RandomAccessFile fis;
private byte[] buffer = new byte[4];
private long pos = 0;
private byte[] pps;
private byte[] sps;
private int spsLength, ppsLength;
/** Parse the sdsd box in an mp4 file
* fis: proper mp4 file
* pos: stsd box's position in the file
*/
public StsdBox (RandomAccessFile fis, long pos) {
this.fis = fis;
this.pos = pos;
findBoxAvcc();
findSPSandPPS();
}
public String getProfileLevel() {
return toHexString(sps,1,3);
}
public String getB64PPS() {
return Base64.encodeToString(pps, 0, ppsLength, Base64.NO_WRAP);
}
public String getB64SPS() {
return Base64.encodeToString(sps, 0, spsLength, Base64.NO_WRAP);
}
private boolean findSPSandPPS() {
/*
* SPS and PPS parameters are stored in the avcC box
* You may find really useful information about this box
* in the document ISO-IEC 14496-15, part 5.2.4.1.1
* The box's structure is described there
*
* aligned(8) class AVCDecoderConfigurationRecord {
* unsigned int(8) configurationVersion = 1;
* unsigned int(8) AVCProfileIndication;
* unsigned int(8) profile_compatibility;
* unsigned int(8) AVCLevelIndication;
* bit(6) reserved = ‘111111’b;
* unsigned int(2) lengthSizeMinusOne;
* bit(3) reserved = ‘111’b;
* unsigned int(5) numOfSequenceParameterSets;
* for (i=0; i< numOfSequenceParameterSets; i++) {
* unsigned int(16) sequenceParameterSetLength ;
* bit(8*sequenceParameterSetLength) sequenceParameterSetNALUnit;
* }
* unsigned int(8) numOfPictureParameterSets;
* for (i=0; i< numOfPictureParameterSets; i++) {
* unsigned int(16) pictureParameterSetLength;
* bit(8*pictureParameterSetLength) pictureParameterSetNALUnit;
* }
* }
*
*
*
*/
try {
// TODO: Here we assume that numOfSequenceParameterSets = 1, numOfPictureParameterSets = 1 !
// Here we extract the SPS parameter
fis.skipBytes(7);
spsLength = 0xFF&fis.readByte();
sps = new byte[spsLength];
fis.read(sps,0,spsLength);
// Here we extract the PPS parameter
fis.skipBytes(2);
ppsLength = 0xFF&fis.readByte();
pps = new byte[ppsLength];
fis.read(pps,0,ppsLength);
} catch (IOException e) {
return false;
}
return true;
}
private boolean findBoxAvcc() {
try {
fis.seek(pos+8);
while (true) {
while (fis.read() != 'a');
fis.read(buffer,0,3);
if (buffer[0] == 'v' && buffer[1] == 'c' && buffer[2] == 'C') break;
}
} catch (IOException e) {
return false;
}
return true;
}
static private String toHexString(byte[] buffer,int start, int len) {
String c;
StringBuilder s = new StringBuilder();
for (int i=start;i<start+len;i++) {
c = Integer.toHexString(buffer[i]&0xFF);
s.append( c.length()<2 ? "0"+c : c );
}
return s.toString();
}
}