Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
SDK
/
exoplayer
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
f133524c
authored
Oct 26, 2015
by
ojw28
Browse files
Options
_('Browse Files')
Download
Plain Diff
Merge pull request #828 from jeoliva/flv-experiment
FLV (H.264 + AAC) support
parents
0545c58d
3e36f529
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
961 additions
and
1 deletions
demo/src/main/java/com/google/android/exoplayer/demo/Samples.java
library/src/main/java/com/google/android/exoplayer/extractor/ExtractorSampleSource.java
library/src/main/java/com/google/android/exoplayer/extractor/flv/AudioTagPayloadReader.java
library/src/main/java/com/google/android/exoplayer/extractor/flv/FlvExtractor.java
library/src/main/java/com/google/android/exoplayer/extractor/flv/ScriptTagPayloadReader.java
library/src/main/java/com/google/android/exoplayer/extractor/flv/TagPayloadReader.java
library/src/main/java/com/google/android/exoplayer/extractor/flv/VideoTagPayloadReader.java
demo/src/main/java/com/google/android/exoplayer/demo/Samples.java
View file @
f133524c
...
...
@@ -145,6 +145,9 @@ import java.util.Locale;
"http://storage.googleapis.com/exoplayer-test-media-0/play.mp3"
,
PlayerActivity
.
TYPE_OTHER
),
new
Sample
(
"Google Glass (WebM Video with Vorbis Audio)"
,
"http://demos.webmproject.org/exoplayer/glass_vp9_vorbis.webm"
,
PlayerActivity
.
TYPE_OTHER
),
new
Sample
(
"Big Buck Bunny (FLV Video)"
,
"http://vod.leasewebcdn.com/bbb.flv?ri=1024&rs=150&start=0"
,
PlayerActivity
.
TYPE_OTHER
),
};
private
Samples
()
{}
...
...
library/src/main/java/com/google/android/exoplayer/extractor/ExtractorSampleSource.java
View file @
f133524c
...
...
@@ -58,9 +58,10 @@ import java.util.List;
* <li>MP3 ({@link com.google.android.exoplayer.extractor.mp3.Mp3Extractor})</li>
* <li>AAC ({@link com.google.android.exoplayer.extractor.ts.AdtsExtractor})</li>
* <li>MPEG TS ({@link com.google.android.exoplayer.extractor.ts.TsExtractor}</li>
* <li>FLV ({@link com.google.android.exoplayer.extractor.flv.FlvExtractor}</li>
* </ul>
*
* <p>Seeking in AAC
and MPEG TS
streams is not supported.
* <p>Seeking in AAC
, MPEG TS and FLV
streams is not supported.
*
* <p>To override the default extractors, pass one or more {@link Extractor} instances to the
* constructor. When reading a new stream, the first {@link Extractor} that returns {@code true}
...
...
@@ -146,6 +147,13 @@ public final class ExtractorSampleSource implements SampleSource, SampleSourceRe
}
catch
(
ClassNotFoundException
e
)
{
// Extractor not found.
}
try
{
DEFAULT_EXTRACTOR_CLASSES
.
add
(
Class
.
forName
(
"com.google.android.exoplayer.extractor.flv.FlvExtractor"
)
.
asSubclass
(
Extractor
.
class
));
}
catch
(
ClassNotFoundException
e
)
{
// Extractor not found.
}
}
private
final
ExtractorHolder
extractorHolder
;
...
...
library/src/main/java/com/google/android/exoplayer/extractor/flv/AudioTagPayloadReader.java
0 → 100644
View file @
f133524c
/*
* Copyright (C) 2014 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
com
.
google
.
android
.
exoplayer
.
extractor
.
flv
;
import
android.util.Pair
;
import
com.google.android.exoplayer.C
;
import
com.google.android.exoplayer.MediaFormat
;
import
com.google.android.exoplayer.extractor.TrackOutput
;
import
com.google.android.exoplayer.util.CodecSpecificDataUtil
;
import
com.google.android.exoplayer.util.MimeTypes
;
import
com.google.android.exoplayer.util.ParsableBitArray
;
import
com.google.android.exoplayer.util.ParsableByteArray
;
import
java.util.Collections
;
/**
* Parses audio tags of from an FLV stream and extracts AAC frames.
*/
final
class
AudioTagPayloadReader
extends
TagPayloadReader
{
// Sound format
private
static
final
int
AUDIO_FORMAT_AAC
=
10
;
// AAC PACKET TYPE
private
static
final
int
AAC_PACKET_TYPE_SEQUENCE_HEADER
=
0
;
private
static
final
int
AAC_PACKET_TYPE_AAC_RAW
=
1
;
// SAMPLING RATES
private
static
final
int
[]
AUDIO_SAMPLING_RATE_TABLE
=
new
int
[]
{
5500
,
11000
,
22000
,
44000
};
// State variables
private
boolean
hasParsedAudioDataHeader
;
private
boolean
hasOutputFormat
;
public
AudioTagPayloadReader
(
TrackOutput
output
)
{
super
(
output
);
}
@Override
public
void
seek
()
{
}
@Override
protected
boolean
parseHeader
(
ParsableByteArray
data
)
throws
UnsupportedTrack
{
// Parse audio data header, if it was not done, to extract information
// about the audio codec and audio configuration.
if
(!
hasParsedAudioDataHeader
)
{
int
header
=
data
.
readUnsignedByte
();
int
soundFormat
=
(
header
>>
4
)
&
0x0F
;
int
sampleRateIndex
=
(
header
>>
2
)
&
0x03
;
int
bitsPerSample
=
(
header
&
0x02
)
==
0x02
?
16
:
8
;
int
channels
=
(
header
&
0x01
)
+
1
;
if
(
sampleRateIndex
<
0
||
sampleRateIndex
>=
AUDIO_SAMPLING_RATE_TABLE
.
length
)
{
throw
new
UnsupportedTrack
(
"Invalid sample rate for the audio track"
);
}
if
(!
hasOutputFormat
)
{
// TODO: Adds support for MP3 and PCM
if
(
soundFormat
!=
AUDIO_FORMAT_AAC
)
{
throw
new
UnsupportedTrack
(
"Audio track not supported. Format: "
+
soundFormat
+
", Sample rate: "
+
sampleRateIndex
+
", bps: "
+
bitsPerSample
+
", channels: "
+
channels
);
}
}
hasParsedAudioDataHeader
=
true
;
}
else
{
// Skip header if it was parsed previously.
data
.
skipBytes
(
1
);
}
// In all the cases we will be managing AAC format (otherwise an exception would be
// fired so we can just always return true
return
true
;
}
@Override
protected
void
parsePayload
(
ParsableByteArray
data
,
long
timeUs
)
{
int
packetType
=
data
.
readUnsignedByte
();
// Parse sequence header just in case it was not done before.
if
(
packetType
==
AAC_PACKET_TYPE_SEQUENCE_HEADER
&&
!
hasOutputFormat
)
{
ParsableBitArray
adtsScratch
=
new
ParsableBitArray
(
new
byte
[
data
.
bytesLeft
()]);
data
.
readBytes
(
adtsScratch
.
data
,
0
,
data
.
bytesLeft
());
int
audioObjectType
=
adtsScratch
.
readBits
(
5
);
int
sampleRateIndex
=
adtsScratch
.
readBits
(
4
);
int
channelConfig
=
adtsScratch
.
readBits
(
4
);
byte
[]
audioSpecificConfig
=
CodecSpecificDataUtil
.
buildAacAudioSpecificConfig
(
audioObjectType
,
sampleRateIndex
,
channelConfig
);
Pair
<
Integer
,
Integer
>
audioParams
=
CodecSpecificDataUtil
.
parseAacAudioSpecificConfig
(
audioSpecificConfig
);
MediaFormat
mediaFormat
=
MediaFormat
.
createAudioFormat
(
MimeTypes
.
AUDIO_AAC
,
MediaFormat
.
NO_VALUE
,
MediaFormat
.
NO_VALUE
,
durationUs
,
audioParams
.
second
,
audioParams
.
first
,
Collections
.
singletonList
(
audioSpecificConfig
),
null
);
output
.
format
(
mediaFormat
);
hasOutputFormat
=
true
;
}
else
if
(
packetType
==
AAC_PACKET_TYPE_AAC_RAW
)
{
// Sample audio AAC frames
int
bytesToWrite
=
data
.
bytesLeft
();
output
.
sampleData
(
data
,
bytesToWrite
);
output
.
sampleMetadata
(
timeUs
,
C
.
SAMPLE_FLAG_SYNC
,
bytesToWrite
,
0
,
null
);
}
}
}
library/src/main/java/com/google/android/exoplayer/extractor/flv/FlvExtractor.java
0 → 100644
View file @
f133524c
This diff is collapsed.
Click to expand it.
library/src/main/java/com/google/android/exoplayer/extractor/flv/ScriptTagPayloadReader.java
0 → 100644
View file @
f133524c
/*
* Copyright (C) 2014 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
com
.
google
.
android
.
exoplayer
.
extractor
.
flv
;
import
com.google.android.exoplayer.C
;
import
com.google.android.exoplayer.extractor.TrackOutput
;
import
com.google.android.exoplayer.util.ParsableByteArray
;
import
java.nio.ByteBuffer
;
import
java.util.ArrayList
;
import
java.util.Date
;
import
java.util.HashMap
;
import
java.util.Map
;
/**
* Parses Script Data tags from an FLV stream and extracts metadata information.
*/
final
class
ScriptTagPayloadReader
extends
TagPayloadReader
{
// AMF object types
private
static
final
int
AMF_TYPE_UNKNOWN
=
-
1
;
private
static
final
int
AMF_TYPE_NUMBER
=
0
;
private
static
final
int
AMF_TYPE_BOOLEAN
=
1
;
private
static
final
int
AMF_TYPE_STRING
=
2
;
private
static
final
int
AMF_TYPE_OBJECT
=
3
;
private
static
final
int
AMF_TYPE_ECMA_ARRAY
=
8
;
private
static
final
int
AMF_TYPE_END_MARKER
=
9
;
private
static
final
int
AMF_TYPE_STRICT_ARRAY
=
10
;
private
static
final
int
AMF_TYPE_DATE
=
11
;
/**
* @param output A {@link TrackOutput} to which samples should be written.
*/
public
ScriptTagPayloadReader
(
TrackOutput
output
)
{
super
(
output
);
}
@Override
public
void
seek
()
{
}
@Override
protected
boolean
parseHeader
(
ParsableByteArray
data
)
throws
UnsupportedTrack
{
return
true
;
}
@SuppressWarnings
(
"unchecked"
)
@Override
protected
void
parsePayload
(
ParsableByteArray
data
,
long
timeUs
)
{
// Read message name (don't storing it as we are not going to give it any use)
readAMFData
(
data
,
AMF_TYPE_UNKNOWN
);
Object
obj
=
readAMFData
(
data
,
AMF_TYPE_UNKNOWN
);
if
(
obj
instanceof
Map
)
{
Map
<
String
,
Object
>
extractedMetadata
=
(
Map
<
String
,
Object
>)
obj
;
for
(
Map
.
Entry
<
String
,
Object
>
entry
:
extractedMetadata
.
entrySet
())
{
if
(
entry
.
getValue
()
==
null
)
{
continue
;
}
switch
(
entry
.
getKey
())
{
case
"duration"
:
this
.
durationUs
=
(
long
)(
C
.
MICROS_PER_SECOND
*
(
Double
)(
entry
.
getValue
()));
break
;
default
:
break
;
}
}
}
}
private
Object
readAMFData
(
ParsableByteArray
data
,
int
type
)
{
if
(
type
==
AMF_TYPE_UNKNOWN
)
{
type
=
data
.
readUnsignedByte
();
}
switch
(
type
)
{
case
AMF_TYPE_NUMBER:
return
readAMFDouble
(
data
);
case
AMF_TYPE_BOOLEAN:
return
readAMFBoolean
(
data
);
case
AMF_TYPE_STRING:
return
readAMFString
(
data
);
case
AMF_TYPE_OBJECT:
return
readAMFObject
(
data
);
case
AMF_TYPE_ECMA_ARRAY:
return
readAMFEcmaArray
(
data
);
case
AMF_TYPE_STRICT_ARRAY:
return
readAMFStrictArray
(
data
);
case
AMF_TYPE_DATE:
return
readAMFDate
(
data
);
default
:
return
null
;
}
}
/**
* Read a boolean from an AMF encoded buffer
* @param data Buffer
* @return Boolean value read from the buffer
*/
private
Boolean
readAMFBoolean
(
ParsableByteArray
data
)
{
return
data
.
readUnsignedByte
()
==
1
;
}
/**
* Read a double number from an AMF encoded buffer
* @param data Buffer
* @return Double number read from the buffer
*/
private
Double
readAMFDouble
(
ParsableByteArray
data
)
{
byte
[]
b
=
new
byte
[
8
];
data
.
readBytes
(
b
,
0
,
b
.
length
);
return
ByteBuffer
.
wrap
(
b
).
getDouble
();
}
/**
* Read a string from an AMF encoded buffer
* @param data Buffer
* @return String read from the buffer
*/
private
String
readAMFString
(
ParsableByteArray
data
)
{
int
size
=
data
.
readUnsignedShort
();
byte
[]
b
=
new
byte
[
size
];
data
.
readBytes
(
b
,
0
,
b
.
length
);
return
new
String
(
b
);
}
/**
* Read an array from an AMF encoded buffer
* @param data Buffer
* @return Array read from the buffer
*/
private
Object
readAMFStrictArray
(
ParsableByteArray
data
)
{
long
count
=
data
.
readUnsignedInt
();
ArrayList
<
Object
>
list
=
new
ArrayList
<>();
for
(
int
i
=
0
;
i
<
count
;
i
++)
{
list
.
add
(
readAMFData
(
data
,
AMF_TYPE_UNKNOWN
));
}
return
list
;
}
/**
* Read an object from an AMF encoded buffer
* @param data Buffer
* @return Object read from the buffer
*/
private
Object
readAMFObject
(
ParsableByteArray
data
)
{
HashMap
<
String
,
Object
>
array
=
new
HashMap
<>();
while
(
true
)
{
String
key
=
readAMFString
(
data
);
int
type
=
data
.
readUnsignedByte
();
if
(
type
==
AMF_TYPE_END_MARKER
)
{
break
;
}
array
.
put
(
key
,
readAMFData
(
data
,
type
));
}
return
array
;
}
/**
* Read am ecma array from an AMF encoded buffer
* @param data Buffer
* @return Ecma array read from the buffer
*/
private
Object
readAMFEcmaArray
(
ParsableByteArray
data
)
{
long
count
=
data
.
readUnsignedInt
();
HashMap
<
String
,
Object
>
array
=
new
HashMap
<>();
for
(
int
i
=
0
;
i
<
count
;
i
++)
{
String
key
=
readAMFString
(
data
);
int
type
=
data
.
readUnsignedByte
();
array
.
put
(
key
,
readAMFData
(
data
,
type
));
}
return
array
;
}
/**
* Read a date from an AMF encoded buffer
* @param data Buffer
* @return Date read from the buffer
*/
private
Date
readAMFDate
(
ParsableByteArray
data
)
{
final
Date
date
=
new
Date
((
long
)
readAMFDouble
(
data
).
doubleValue
());
data
.
readUnsignedShort
();
return
date
;
}
}
library/src/main/java/com/google/android/exoplayer/extractor/flv/TagPayloadReader.java
0 → 100644
View file @
f133524c
/*
* Copyright (C) 2014 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
com
.
google
.
android
.
exoplayer
.
extractor
.
flv
;
import
com.google.android.exoplayer.C
;
import
com.google.android.exoplayer.extractor.TrackOutput
;
import
com.google.android.exoplayer.util.ParsableByteArray
;
/**
* Extracts individual samples from FLV tags, preserving original order.
*/
/* package */
abstract
class
TagPayloadReader
{
protected
final
TrackOutput
output
;
// Duration of the track
protected
long
durationUs
;
/**
* @param output A {@link TrackOutput} to which samples should be written.
*/
protected
TagPayloadReader
(
TrackOutput
output
)
{
this
.
output
=
output
;
this
.
durationUs
=
C
.
UNKNOWN_TIME_US
;
}
/**
* Notifies the reader that a seek has occurred.
* <p>
* Following a call to this method, the data passed to the next invocation of
* {@link #consume(ParsableByteArray, long)} will not be a continuation of the data that
* was previously passed. Hence the reader should reset any internal state.
*/
public
abstract
void
seek
();
/**
* Parses tag header
* @param data Buffer where the tag header is stored
* @return True if header was parsed successfully and then payload should be read;
* Otherwise, false
* @throws UnsupportedTrack
*/
protected
abstract
boolean
parseHeader
(
ParsableByteArray
data
)
throws
UnsupportedTrack
;
/**
* Parses tag payload
* @param data Buffer where tag payload is stored
* @param timeUs Time position of the frame
*/
protected
abstract
void
parsePayload
(
ParsableByteArray
data
,
long
timeUs
);
/**
* Consumes payload data.
*
* @param data The payload data to consume.
* @param timeUs The timestamp associated with the payload.
*/
public
void
consume
(
ParsableByteArray
data
,
long
timeUs
)
throws
UnsupportedTrack
{
if
(
parseHeader
(
data
))
{
parsePayload
(
data
,
timeUs
);
}
}
/**
* Sets duration in microseconds
* @param durationUs duration in microseconds
*/
public
void
setDurationUs
(
long
durationUs
)
{
this
.
durationUs
=
durationUs
;
}
public
long
getDurationUs
()
{
return
durationUs
;
}
/**
* Thrown when format described in the AudioTrack is not supported
*/
public
static
final
class
UnsupportedTrack
extends
Exception
{
public
UnsupportedTrack
(
String
msg
)
{
super
(
msg
);
}
}
}
library/src/main/java/com/google/android/exoplayer/extractor/flv/VideoTagPayloadReader.java
0 → 100644
View file @
f133524c
/*
* Copyright (C) 2014 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
com
.
google
.
android
.
exoplayer
.
extractor
.
flv
;
import
android.util.Log
;
import
com.google.android.exoplayer.C
;
import
com.google.android.exoplayer.MediaFormat
;
import
com.google.android.exoplayer.ParserException
;
import
com.google.android.exoplayer.extractor.TrackOutput
;
import
com.google.android.exoplayer.util.Assertions
;
import
com.google.android.exoplayer.util.CodecSpecificDataUtil
;
import
com.google.android.exoplayer.util.MimeTypes
;
import
com.google.android.exoplayer.util.NalUnitUtil
;
import
com.google.android.exoplayer.util.ParsableBitArray
;
import
com.google.android.exoplayer.util.ParsableByteArray
;
import
java.util.ArrayList
;
import
java.util.List
;
/**
* Parses video tags from an FLV stream and extracts H.264 nal units.
*/
final
class
VideoTagPayloadReader
extends
TagPayloadReader
{
private
static
final
String
TAG
=
"VideoTagPayloadReader"
;
// Video codec
private
static
final
int
VIDEO_CODEC_AVC
=
7
;
// FRAME TYPE
private
static
final
int
VIDEO_FRAME_KEYFRAME
=
1
;
private
static
final
int
VIDEO_FRAME_VIDEO_INFO
=
5
;
// PACKET TYPE
private
static
final
int
AVC_PACKET_TYPE_SEQUENCE_HEADER
=
0
;
private
static
final
int
AVC_PACKET_TYPE_AVC_NALU
=
1
;
private
static
final
int
AVC_PACKET_TYPE_AVC_END_OF_SEQUENCE
=
2
;
// Temporary arrays.
private
final
ParsableByteArray
nalStartCode
;
private
final
ParsableByteArray
nalLength
;
private
int
nalUnitsLength
;
// State variables.
private
boolean
hasOutputFormat
;
private
int
frameType
;
/**
* @param output A {@link TrackOutput} to which samples should be written.
*/
public
VideoTagPayloadReader
(
TrackOutput
output
)
{
super
(
output
);
nalStartCode
=
new
ParsableByteArray
(
NalUnitUtil
.
NAL_START_CODE
);
nalLength
=
new
ParsableByteArray
(
4
);
}
@Override
public
void
seek
()
{
}
@Override
protected
boolean
parseHeader
(
ParsableByteArray
data
)
throws
UnsupportedTrack
{
int
header
=
data
.
readUnsignedByte
();
int
frameType
=
(
header
>>
4
)
&
0x0F
;
int
videoCodec
=
(
header
&
0x0F
);
// Support just H.264 encoded content.
if
(
videoCodec
!=
VIDEO_CODEC_AVC
)
{
throw
new
UnsupportedTrack
(
"Video codec not supported. Codec: "
+
videoCodec
);
}
this
.
frameType
=
frameType
;
return
(
frameType
!=
VIDEO_FRAME_VIDEO_INFO
);
}
@Override
protected
void
parsePayload
(
ParsableByteArray
data
,
long
timeUs
)
{
int
packetType
=
data
.
readUnsignedByte
();
int
compositionTime
=
data
.
readUnsignedInt24
();
// If there is a composition time, adjust timeUs accordingly
// Note: compositionTime within AVCVIDEOPACKET is provided in milliseconds
// and timeUs is in microseconds.
if
(
compositionTime
>
0
)
{
timeUs
+=
compositionTime
*
1000
;
}
// Parse avc sequence header in case this was not done before.
if
(
packetType
==
AVC_PACKET_TYPE_SEQUENCE_HEADER
&&
!
hasOutputFormat
)
{
ParsableByteArray
videoSequence
=
new
ParsableByteArray
(
new
byte
[
data
.
bytesLeft
()]);
data
.
readBytes
(
videoSequence
.
data
,
0
,
data
.
bytesLeft
());
AvcSequenceHeaderData
avcData
;
try
{
avcData
=
parseAvcCodecPrivate
(
videoSequence
);
nalUnitsLength
=
avcData
.
nalUnitLengthFieldLength
;
}
catch
(
ParserException
e
)
{
e
.
printStackTrace
();
return
;
}
// Construct and output the format.
MediaFormat
mediaFormat
=
MediaFormat
.
createVideoFormat
(
MimeTypes
.
VIDEO_H264
,
MediaFormat
.
NO_VALUE
,
MediaFormat
.
NO_VALUE
,
durationUs
,
avcData
.
width
,
avcData
.
height
,
avcData
.
initializationData
,
MediaFormat
.
NO_VALUE
,
avcData
.
pixelWidthAspectRatio
);
output
.
format
(
mediaFormat
);
hasOutputFormat
=
true
;
}
else
if
(
packetType
==
AVC_PACKET_TYPE_AVC_NALU
)
{
// TODO: Deduplicate with Mp4Extractor.
// Zero the top three bytes of the array that we'll use to parse nal unit lengths, in case
// they're only 1 or 2 bytes long.
byte
[]
nalLengthData
=
nalLength
.
data
;
nalLengthData
[
0
]
=
0
;
nalLengthData
[
1
]
=
0
;
nalLengthData
[
2
]
=
0
;
int
nalUnitLengthFieldLength
=
nalUnitsLength
;
int
nalUnitLengthFieldLengthDiff
=
4
-
nalUnitsLength
;
// NAL units are length delimited, but the decoder requires start code delimited units.
// Loop until we've written the sample to the track output, replacing length delimiters with
// start codes as we encounter them.
int
bytesWritten
=
0
;
int
bytesToWrite
;
while
(
data
.
bytesLeft
()
>
0
)
{
// Read the NAL length so that we know where we find the next one.
data
.
readBytes
(
nalLength
.
data
,
nalUnitLengthFieldLengthDiff
,
nalUnitLengthFieldLength
);
nalLength
.
setPosition
(
0
);
bytesToWrite
=
nalLength
.
readUnsignedIntToInt
();
// First, write nal start code (replacing length field by nal delimiter codes)
nalStartCode
.
setPosition
(
0
);
output
.
sampleData
(
nalStartCode
,
4
);
bytesWritten
+=
4
;
// Then write nal unit itsef
output
.
sampleData
(
data
,
bytesToWrite
);
bytesWritten
+=
bytesToWrite
;
}
output
.
sampleMetadata
(
timeUs
,
frameType
==
VIDEO_FRAME_KEYFRAME
?
C
.
SAMPLE_FLAG_SYNC
:
0
,
bytesWritten
,
0
,
null
);
}
else
if
(
packetType
==
AVC_PACKET_TYPE_AVC_END_OF_SEQUENCE
)
{
Log
.
d
(
TAG
,
"End of seq!!!"
);
}
}
/**
* Builds initialization data for a {@link MediaFormat} from H.264 (AVC) codec private data.
*
* @return The AvcSequenceHeader data with all the information needed to initialize
* the video codec.
* @throws ParserException If the initialization data could not be built.
*/
private
AvcSequenceHeaderData
parseAvcCodecPrivate
(
ParsableByteArray
buffer
)
throws
ParserException
{
try
{
// TODO: Deduplicate with AtomParsers.parseAvcCFromParent.
buffer
.
setPosition
(
4
);
int
nalUnitLengthFieldLength
=
(
buffer
.
readUnsignedByte
()
&
0x03
)
+
1
;
Assertions
.
checkState
(
nalUnitLengthFieldLength
!=
3
);
List
<
byte
[]>
initializationData
=
new
ArrayList
<>();
int
numSequenceParameterSets
=
buffer
.
readUnsignedByte
()
&
0x1F
;
for
(
int
i
=
0
;
i
<
numSequenceParameterSets
;
i
++)
{
initializationData
.
add
(
NalUnitUtil
.
parseChildNalUnit
(
buffer
));
}
int
numPictureParameterSets
=
buffer
.
readUnsignedByte
();
for
(
int
j
=
0
;
j
<
numPictureParameterSets
;
j
++)
{
initializationData
.
add
(
NalUnitUtil
.
parseChildNalUnit
(
buffer
));
}
float
pixelWidthAspectRatio
=
1
;
int
width
=
MediaFormat
.
NO_VALUE
;
int
height
=
MediaFormat
.
NO_VALUE
;
if
(
numSequenceParameterSets
>
0
)
{
// Parse the first sequence parameter set to obtain pixelWidthAspectRatio.
ParsableBitArray
spsDataBitArray
=
new
ParsableBitArray
(
initializationData
.
get
(
0
));
// Skip the NAL header consisting of the nalUnitLengthField and the type (1 byte).
spsDataBitArray
.
setPosition
(
8
*
(
nalUnitLengthFieldLength
+
1
));
CodecSpecificDataUtil
.
SpsData
sps
=
CodecSpecificDataUtil
.
parseSpsNalUnit
(
spsDataBitArray
);
width
=
sps
.
width
;
height
=
sps
.
height
;
pixelWidthAspectRatio
=
sps
.
pixelWidthAspectRatio
;
}
return
new
AvcSequenceHeaderData
(
initializationData
,
nalUnitLengthFieldLength
,
width
,
height
,
pixelWidthAspectRatio
);
}
catch
(
ArrayIndexOutOfBoundsException
e
)
{
throw
new
ParserException
(
"Error parsing AVC codec private"
);
}
}
/**
* Holds data parsed from an Sequence Header video tag atom.
*/
private
static
final
class
AvcSequenceHeaderData
{
public
final
List
<
byte
[]>
initializationData
;
public
final
int
nalUnitLengthFieldLength
;
public
final
float
pixelWidthAspectRatio
;
public
final
int
width
;
public
final
int
height
;
public
AvcSequenceHeaderData
(
List
<
byte
[]>
initializationData
,
int
nalUnitLengthFieldLength
,
int
width
,
int
height
,
float
pixelWidthAspectRatio
)
{
this
.
initializationData
=
initializationData
;
this
.
nalUnitLengthFieldLength
=
nalUnitLengthFieldLength
;
this
.
pixelWidthAspectRatio
=
pixelWidthAspectRatio
;
this
.
width
=
width
;
this
.
height
=
height
;
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment