Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
221 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
app/src/main/java/com/mushuichuan/mediacodecdemo/Mp4/FtypBox.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.mushuichuan.mediacodecdemo.Mp4; | ||
|
||
/** | ||
* Created by yanshun.li on 10/23/16. | ||
*/ | ||
|
||
public class FtypBox extends Mp4Box { | ||
|
||
public FtypBox() { | ||
hasSubBox = false; | ||
headSize = 12; | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
app/src/main/java/com/mushuichuan/mediacodecdemo/Mp4/Mp4Box.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package com.mushuichuan.mediacodecdemo.Mp4; | ||
|
||
import com.mushuichuan.mediacodecdemo.Logger; | ||
|
||
import java.util.ArrayList; | ||
|
||
/** | ||
* head: | ||
* 4 bytes size | ||
* 4 bytes type | ||
* 1 bytes version for ftyp | ||
* 3 bytes flags for ftyp | ||
* 8 bytes largesize (if siz==0) | ||
* 16 bytes UUIDs (if type==uuid) | ||
* Created by yanshun.li on 10/20/16. | ||
*/ | ||
|
||
public class Mp4Box { | ||
public int headSize = 8; | ||
public int size; | ||
public String type; | ||
public long largeSize; | ||
public ArrayList<Mp4Box> subBox = new ArrayList<>(); | ||
public boolean hasSubBox = false; | ||
|
||
public Mp4Box() { | ||
} | ||
|
||
public Mp4Box parse(byte[] byteBuffer, int start, int end) { | ||
size = end - start; | ||
if (size == 1) { | ||
largeSize = Util.get64LongFromBuffer(byteBuffer, 8); | ||
headSize += 8; | ||
} else if (size == 0) { | ||
//box extends to the end of file | ||
} | ||
String type = Util.getStringFromBuffer(byteBuffer, start + 4, 4); | ||
if (type.equals("uuid")) { | ||
headSize += 16; | ||
} | ||
Logger.d(type + " size:" + size); | ||
|
||
|
||
// there is no sub boxs | ||
if (!hasSubBox) { | ||
return this; | ||
} | ||
|
||
start += headSize; | ||
// String test = Util.getStringFromBuffer(byteBuffer, start, 1024); | ||
long totalLength = 0; | ||
do { | ||
int len = Util.get32IntFromBuffer(byteBuffer, start); | ||
type = Util.getStringFromBuffer(byteBuffer, start + 4, 4); | ||
if (len > 8) { | ||
subBox.add(new Mp4Box().parse(byteBuffer, start, start + len)); | ||
totalLength += len; | ||
start += len; | ||
} else { | ||
break; | ||
} | ||
} while (totalLength < end); | ||
return this; | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
app/src/main/java/com/mushuichuan/mediacodecdemo/Mp4/Mp4Extractor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.mushuichuan.mediacodecdemo.Mp4; | ||
|
||
/** | ||
* Created by yanshun.li on 10/23/16. | ||
*/ | ||
|
||
public class Mp4Extractor { | ||
|
||
public Mp4Extractor() { | ||
|
||
} | ||
|
||
public void setDataSource(String path){ | ||
byte[] ftyps = Util.getBoxBuffer(path, "ftyp"); | ||
Mp4Box ftypBox = new FtypBox().parse(ftyps, 0, ftyps.length); | ||
|
||
byte[] moovs = Util.getBoxBuffer(path, "moov"); | ||
Mp4Box moovBox = new Mp4Box().parse(moovs, 0, moovs.length); | ||
} | ||
} |
95 changes: 95 additions & 0 deletions
95
app/src/main/java/com/mushuichuan/mediacodecdemo/Mp4/Util.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package com.mushuichuan.mediacodecdemo.Mp4; | ||
|
||
import com.mushuichuan.mediacodecdemo.Logger; | ||
|
||
import java.io.FileInputStream; | ||
import java.io.FileNotFoundException; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
|
||
/** | ||
* Created by yanshun.li on 10/20/16. | ||
*/ | ||
|
||
public class Util { | ||
public static final int FOUR_BYTES = 4; | ||
public static final int EIGHT_BYTES = 8; | ||
public static final int BITS_PER_BYTE = 8; | ||
|
||
public static byte[] getBoxBuffer(String path, String type) { | ||
InputStream in; | ||
byte[] boxBuffer = new byte[0]; | ||
try { | ||
in = new FileInputStream(path); | ||
} catch (FileNotFoundException e) { | ||
return null; | ||
} | ||
try { | ||
while (true) { | ||
//read length and type | ||
byte[] lenTypeBuffer = new byte[FOUR_BYTES * 2]; | ||
in.read(lenTypeBuffer); | ||
|
||
int length = get32IntFromBuffer(lenTypeBuffer, 0); | ||
String boxType = getStringFromBuffer(lenTypeBuffer, FOUR_BYTES, FOUR_BYTES); | ||
|
||
if (type.equals(boxType)) { | ||
boxBuffer = new byte[length]; | ||
System.arraycopy(lenTypeBuffer, 0, boxBuffer, 0, lenTypeBuffer.length); | ||
int reade = lenTypeBuffer.length; | ||
|
||
do { | ||
int len = in.read(boxBuffer, reade, length - reade); | ||
reade += len; | ||
} while (reade < length); | ||
break; | ||
} else { | ||
in.skip(length - FOUR_BYTES * 2); | ||
continue; | ||
} | ||
|
||
} | ||
} catch (IOException e) { | ||
Logger.e(e); | ||
} | ||
|
||
return boxBuffer; | ||
} | ||
|
||
public static int get32IntFromBuffer(byte[] buffer, int start) { | ||
int result = 0; | ||
if (buffer.length - start >= FOUR_BYTES) { | ||
for (int i = 0; i < FOUR_BYTES; i++) { | ||
result |= (buffer[start + i] & 0xff) << BITS_PER_BYTE * (FOUR_BYTES - i - 1); | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
public static long get64LongFromBuffer(byte[] buffer, int start) { | ||
long result = 0; | ||
if (buffer.length - start >= FOUR_BYTES) { | ||
for (int i = 0; i < EIGHT_BYTES; i++) { | ||
result |= (buffer[start + i] & 0xff) << BITS_PER_BYTE * (EIGHT_BYTES - i - 1); | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
/** | ||
* @param buffer | ||
* @param start | ||
* @param size | ||
* @return | ||
*/ | ||
public static String getStringFromBuffer(byte[] buffer, int start, int size) { | ||
char[] result = new char[size]; | ||
if (buffer.length - start >= size) { | ||
for (int i = 0; i < size; i++) { | ||
result[i] = (char) (buffer[start + i] & 0xff); | ||
} | ||
} | ||
return String.valueOf(result); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters