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
0de6bc86
authored
May 20, 2021
by
olly
Committed by
Oliver Woodman
May 20, 2021
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
Format *_jni.cc files
PiperOrigin-RevId: 374830877
parent
9e4315f4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
88 additions
and
91 deletions
extensions/ffmpeg/src/main/jni/ffmpeg_jni.cc
extensions/flac/src/main/jni/flac_jni.cc
extensions/opus/src/main/jni/opus_jni.cc
extensions/vp9/src/main/jni/vpx_jni.cc
extensions/ffmpeg/src/main/jni/ffmpeg_jni.cc
View file @
0de6bc86
...
...
@@ -13,9 +13,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <android/log.h>
#include <jni.h>
#include <stdlib.h>
#include <android/log.h>
extern
"C"
{
#ifdef __cplusplus
...
...
@@ -33,8 +33,8 @@ extern "C" {
}
#define LOG_TAG "ffmpeg_jni"
#define LOGE(...)
((void)__android_log_print(ANDROID_LOG_ERROR, LOG_TAG,
\
__VA_ARGS__))
#define LOGE(...) \
((void)__android_log_print(ANDROID_LOG_ERROR, LOG_TAG,
__VA_ARGS__))
#define LIBRARY_FUNC(RETURN_TYPE, NAME, ...) \
extern "C" { \
...
...
@@ -69,7 +69,7 @@ static const int AUDIO_DECODER_ERROR_OTHER = -2;
/**
* Returns the AVCodec with the specified name, or NULL if it is not available.
*/
AVCodec
*
getCodecByName
(
JNIEnv
*
env
,
jstring
codecName
);
AVCodec
*
getCodecByName
(
JNIEnv
*
env
,
jstring
codecName
);
/**
* Allocates and opens a new AVCodecContext for the specified codec, passing the
...
...
@@ -100,7 +100,7 @@ void releaseContext(AVCodecContext *context);
jint
JNI_OnLoad
(
JavaVM
*
vm
,
void
*
reserved
)
{
JNIEnv
*
env
;
if
(
vm
->
GetEnv
(
reinterpret_cast
<
void
**>
(
&
env
),
JNI_VERSION_1_6
)
!=
JNI_OK
)
{
if
(
vm
->
GetEnv
(
reinterpret_cast
<
void
**>
(
&
env
),
JNI_VERSION_1_6
)
!=
JNI_OK
)
{
return
-
1
;
}
avcodec_register_all
();
...
...
@@ -149,13 +149,13 @@ AUDIO_DECODER_FUNC(jint, ffmpegDecode, jlong context, jobject inputData,
LOGE
(
"Invalid output buffer length: %d"
,
outputSize
);
return
-
1
;
}
uint8_t
*
inputBuffer
=
(
uint8_t
*
)
env
->
GetDirectBufferAddress
(
inputData
);
uint8_t
*
outputBuffer
=
(
uint8_t
*
)
env
->
GetDirectBufferAddress
(
outputData
);
uint8_t
*
inputBuffer
=
(
uint8_t
*
)
env
->
GetDirectBufferAddress
(
inputData
);
uint8_t
*
outputBuffer
=
(
uint8_t
*
)
env
->
GetDirectBufferAddress
(
outputData
);
AVPacket
packet
;
av_init_packet
(
&
packet
);
packet
.
data
=
inputBuffer
;
packet
.
size
=
inputSize
;
return
decodePacket
((
AVCodecContext
*
)
context
,
&
packet
,
outputBuffer
,
return
decodePacket
((
AVCodecContext
*
)
context
,
&
packet
,
outputBuffer
,
outputSize
);
}
...
...
@@ -164,7 +164,7 @@ AUDIO_DECODER_FUNC(jint, ffmpegGetChannelCount, jlong context) {
LOGE
(
"Context must be non-NULL."
);
return
-
1
;
}
return
((
AVCodecContext
*
)
context
)
->
channels
;
return
((
AVCodecContext
*
)
context
)
->
channels
;
}
AUDIO_DECODER_FUNC
(
jint
,
ffmpegGetSampleRate
,
jlong
context
)
{
...
...
@@ -172,11 +172,11 @@ AUDIO_DECODER_FUNC(jint, ffmpegGetSampleRate, jlong context) {
LOGE
(
"Context must be non-NULL."
);
return
-
1
;
}
return
((
AVCodecContext
*
)
context
)
->
sample_rate
;
return
((
AVCodecContext
*
)
context
)
->
sample_rate
;
}
AUDIO_DECODER_FUNC
(
jlong
,
ffmpegReset
,
jlong
jContext
,
jbyteArray
extraData
)
{
AVCodecContext
*
context
=
(
AVCodecContext
*
)
jContext
;
AVCodecContext
*
context
=
(
AVCodecContext
*
)
jContext
;
if
(
!
context
)
{
LOGE
(
"Tried to reset without a context."
);
return
0L
;
...
...
@@ -200,16 +200,16 @@ AUDIO_DECODER_FUNC(jlong, ffmpegReset, jlong jContext, jbyteArray extraData) {
}
avcodec_flush_buffers
(
context
);
return
(
jlong
)
context
;
return
(
jlong
)
context
;
}
AUDIO_DECODER_FUNC
(
void
,
ffmpegRelease
,
jlong
context
)
{
if
(
context
)
{
releaseContext
((
AVCodecContext
*
)
context
);
releaseContext
((
AVCodecContext
*
)
context
);
}
}
AVCodec
*
getCodecByName
(
JNIEnv
*
env
,
jstring
codecName
)
{
AVCodec
*
getCodecByName
(
JNIEnv
*
env
,
jstring
codecName
)
{
if
(
!
codecName
)
{
return
NULL
;
}
...
...
@@ -233,13 +233,13 @@ AVCodecContext *createContext(JNIEnv *env, AVCodec *codec, jbyteArray extraData,
jsize
size
=
env
->
GetArrayLength
(
extraData
);
context
->
extradata_size
=
size
;
context
->
extradata
=
(
uint8_t
*
)
av_malloc
(
size
+
AV_INPUT_BUFFER_PADDING_SIZE
);
(
uint8_t
*
)
av_malloc
(
size
+
AV_INPUT_BUFFER_PADDING_SIZE
);
if
(
!
context
->
extradata
)
{
LOGE
(
"Failed to allocate extradata."
);
releaseContext
(
context
);
return
NULL
;
}
env
->
GetByteArrayRegion
(
extraData
,
0
,
size
,
(
jbyte
*
)
context
->
extradata
);
env
->
GetByteArrayRegion
(
extraData
,
0
,
size
,
(
jbyte
*
)
context
->
extradata
);
}
if
(
context
->
codec_id
==
AV_CODEC_ID_PCM_MULAW
||
context
->
codec_id
==
AV_CODEC_ID_PCM_ALAW
)
{
...
...
@@ -299,14 +299,14 @@ int decodePacket(AVCodecContext *context, AVPacket *packet,
resampleContext
=
(
SwrContext
*
)
context
->
opaque
;
}
else
{
resampleContext
=
swr_alloc
();
av_opt_set_int
(
resampleContext
,
"in_channel_layout"
,
channelLayout
,
0
);
av_opt_set_int
(
resampleContext
,
"in_channel_layout"
,
channelLayout
,
0
);
av_opt_set_int
(
resampleContext
,
"out_channel_layout"
,
channelLayout
,
0
);
av_opt_set_int
(
resampleContext
,
"in_sample_rate"
,
sampleRate
,
0
);
av_opt_set_int
(
resampleContext
,
"out_sample_rate"
,
sampleRate
,
0
);
av_opt_set_int
(
resampleContext
,
"in_sample_fmt"
,
sampleFormat
,
0
);
// The output format is always the requested format.
av_opt_set_int
(
resampleContext
,
"out_sample_fmt"
,
context
->
request_sample_fmt
,
0
);
context
->
request_sample_fmt
,
0
);
result
=
swr_init
(
resampleContext
);
if
(
result
<
0
)
{
logError
(
"swr_init"
,
result
);
...
...
@@ -345,7 +345,7 @@ int decodePacket(AVCodecContext *context, AVPacket *packet,
}
void
logError
(
const
char
*
functionName
,
int
errorNumber
)
{
char
*
buffer
=
(
char
*
)
malloc
(
ERROR_STRING_BUFFER_LENGTH
*
sizeof
(
char
));
char
*
buffer
=
(
char
*
)
malloc
(
ERROR_STRING_BUFFER_LENGTH
*
sizeof
(
char
));
av_strerror
(
errorNumber
,
buffer
,
ERROR_STRING_BUFFER_LENGTH
);
LOGE
(
"Error in %s: %s"
,
functionName
,
buffer
);
free
(
buffer
);
...
...
@@ -362,4 +362,3 @@ void releaseContext(AVCodecContext *context) {
}
avcodec_free_context
(
&
context
);
}
extensions/flac/src/main/jni/flac_jni.cc
View file @
0de6bc86
...
...
@@ -29,14 +29,15 @@
#define ALOGV(...) \
((void)__android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__))
#define DECODER_FUNC(RETURN_TYPE, NAME, ...) \
extern "C" { \
JNIEXPORT RETURN_TYPE \
Java_com_google_android_exoplayer2_ext_flac_FlacDecoderJni_##NAME( \
JNIEnv *env, jobject thiz, ##__VA_ARGS__); \
} \
JNIEXPORT RETURN_TYPE \
Java_com_google_android_exoplayer2_ext_flac_FlacDecoderJni_##NAME( \
#define DECODER_FUNC(RETURN_TYPE, NAME, ...) \
extern "C" { \
JNIEXPORT RETURN_TYPE \
Java_com_google_android_exoplayer2_ext_flac_FlacDecoderJni_##NAME(JNIEnv *env, \
jobject thiz, \
##__VA_ARGS__); \
} \
JNIEXPORT RETURN_TYPE \
Java_com_google_android_exoplayer2_ext_flac_FlacDecoderJni_##NAME( \
JNIEnv *env, jobject thiz, ##__VA_ARGS__)
class
JavaDataSource
:
public
DataSource
{
...
...
extensions/opus/src/main/jni/opus_jni.cc
View file @
0de6bc86
...
...
@@ -14,38 +14,37 @@
* limitations under the License.
*/
#include <jni.h>
#include <android/log.h>
#include <jni.h>
#include <cstdlib>
#include "opus.h" // NOLINT
#include "opus.h"
// NOLINT
#include "opus_multistream.h" // NOLINT
#define LOG_TAG "opus_jni"
#define LOGE(...)
((void)__android_log_print(ANDROID_LOG_ERROR, LOG_TAG,
\
__VA_ARGS__))
#define DECODER_FUNC(RETURN_TYPE, NAME, ...) \
extern "C" { \
JNIEXPORT RETURN_TYPE \
Java_com_google_android_exoplayer2_ext_opus_OpusDecoder_ ## NAME
\
(JNIEnv* env, jobject thiz, ##__VA_ARGS__);
\
} \
JNIEXPORT RETURN_TYPE \
Java_com_google_android_exoplayer2_ext_opus_OpusDecoder_ ## NAME
\
(JNIEnv* env, jobject thiz, ##__VA_ARGS__)\
#define LIBRARY_FUNC(RETURN_TYPE, NAME, ...) \
extern "C" { \
JNIEXPORT RETURN_TYPE \
Java_com_google_android_exoplayer2_ext_opus_OpusLibrary_ ## NAME
\
(JNIEnv* env, jobject thiz, ##__VA_ARGS__);
\
} \
JNIEXPORT RETURN_TYPE \
Java_com_google_android_exoplayer2_ext_opus_OpusLibrary_ ## NAME
\
(JNIEnv* env, jobject thiz, ##__VA_ARGS__)\
#define LOGE(...) \
((void)__android_log_print(ANDROID_LOG_ERROR, LOG_TAG,
__VA_ARGS__))
#define DECODER_FUNC(RETURN_TYPE, NAME, ...)
\
extern "C" {
\
JNIEXPORT RETURN_TYPE
\
Java_com_google_android_exoplayer2_ext_opus_OpusDecoder_##NAME(
\
JNIEnv* env, jobject thiz, ##__VA_ARGS__);
\
}
\
JNIEXPORT RETURN_TYPE
\
Java_com_google_android_exoplayer2_ext_opus_OpusDecoder_##NAME(
\
JNIEnv* env, jobject thiz, ##__VA_ARGS__)
#define LIBRARY_FUNC(RETURN_TYPE, NAME, ...)
\
extern "C" {
\
JNIEXPORT RETURN_TYPE
\
Java_com_google_android_exoplayer2_ext_opus_OpusLibrary_##NAME(
\
JNIEnv* env, jobject thiz, ##__VA_ARGS__);
\
}
\
JNIEXPORT RETURN_TYPE
\
Java_com_google_android_exoplayer2_ext_opus_OpusLibrary_##NAME(
\
JNIEnv* env, jobject thiz, ##__VA_ARGS__)
// JNI references for SimpleOutputBuffer class.
static
jmethodID
outputBufferInit
;
...
...
@@ -66,7 +65,8 @@ static int errorCode;
static
bool
outputFloat
=
false
;
DECODER_FUNC
(
jlong
,
opusInit
,
jint
sampleRate
,
jint
channelCount
,
jint
numStreams
,
jint
numCoupled
,
jint
gain
,
jbyteArray
jStreamMap
)
{
jint
numStreams
,
jint
numCoupled
,
jint
gain
,
jbyteArray
jStreamMap
)
{
int
status
=
OPUS_INVALID_STATE
;
::
channelCount
=
channelCount
;
errorCode
=
0
;
...
...
@@ -88,21 +88,20 @@ DECODER_FUNC(jlong, opusInit, jint sampleRate, jint channelCount,
// Populate JNI References.
const
jclass
outputBufferClass
=
env
->
FindClass
(
"com/google/android/exoplayer2/decoder/SimpleOutputBuffer"
);
outputBufferInit
=
env
->
GetMethodID
(
outputBufferClass
,
"init"
,
"(JI)Ljava/nio/ByteBuffer;"
);
outputBufferInit
=
env
->
GetMethodID
(
outputBufferClass
,
"init"
,
"(JI)Ljava/nio/ByteBuffer;"
);
return
reinterpret_cast
<
intptr_t
>
(
decoder
);
}
DECODER_FUNC
(
jint
,
opusDecode
,
jlong
jDecoder
,
jlong
jTimeUs
,
jobject
jInputBuffer
,
jint
inputSize
,
jobject
jOutputBuffer
)
{
jobject
jInputBuffer
,
jint
inputSize
,
jobject
jOutputBuffer
)
{
OpusMSDecoder
*
decoder
=
reinterpret_cast
<
OpusMSDecoder
*>
(
jDecoder
);
const
uint8_t
*
inputBuffer
=
reinterpret_cast
<
const
uint8_t
*>
(
env
->
GetDirectBufferAddress
(
jInputBuffer
));
const
uint8_t
*
inputBuffer
=
reinterpret_cast
<
const
uint8_t
*>
(
env
->
GetDirectBufferAddress
(
jInputBuffer
));
const
int
byteSizePerSample
=
outputFloat
?
kBytesPerFloatSample
:
kBytesPerIntPcmSample
;
const
int
byteSizePerSample
=
outputFloat
?
kBytesPerFloatSample
:
kBytesPerIntPcmSample
;
const
jint
outputSize
=
kMaxOpusOutputPacketSizeSamples
*
byteSizePerSample
*
channelCount
;
...
...
@@ -111,8 +110,8 @@ DECODER_FUNC(jint, opusDecode, jlong jDecoder, jlong jTimeUs,
// Exception is thrown in Java when returning from the native call.
return
-
1
;
}
const
jobject
jOutputBufferData
=
env
->
CallObjectMethod
(
jOutputBuffer
,
outputBufferInit
,
jTimeUs
,
outputSize
);
const
jobject
jOutputBufferData
=
env
->
CallObjectMethod
(
jOutputBuffer
,
outputBufferInit
,
jTimeUs
,
outputSize
);
if
(
env
->
ExceptionCheck
())
{
// Exception is thrown in Java when returning from the native call.
return
-
1
;
...
...
@@ -122,26 +121,28 @@ DECODER_FUNC(jint, opusDecode, jlong jDecoder, jlong jTimeUs,
if
(
outputFloat
)
{
float
*
outputBufferData
=
reinterpret_cast
<
float
*>
(
env
->
GetDirectBufferAddress
(
jOutputBufferData
));
sampleCount
=
opus_multistream_decode_float
(
decoder
,
inputBuffer
,
inputSize
,
outputBufferData
,
kMaxOpusOutputPacketSizeSamples
,
0
);
sampleCount
=
opus_multistream_decode_float
(
decoder
,
inputBuffer
,
inputSize
,
outputBufferData
,
kMaxOpusOutputPacketSizeSamples
,
0
);
}
else
{
int16_t
*
outputBufferData
=
reinterpret_cast
<
int16_t
*>
(
env
->
GetDirectBufferAddress
(
jOutputBufferData
));
sampleCount
=
opus_multistream_decode
(
decoder
,
inputBuffer
,
inputSize
,
outputBufferData
,
kMaxOpusOutputPacketSizeSamples
,
0
);
outputBufferData
,
kMaxOpusOutputPacketSizeSamples
,
0
);
}
// record error code
errorCode
=
(
sampleCount
<
0
)
?
sampleCount
:
0
;
return
(
sampleCount
<
0
)
?
sampleCount
:
sampleCount
*
byteSizePerSample
*
channelCount
;
:
sampleCount
*
byteSizePerSample
*
channelCount
;
}
DECODER_FUNC
(
jint
,
opusSecureDecode
,
jlong
jDecoder
,
jlong
jTimeUs
,
jobject
jInputBuffer
,
jint
inputSize
,
jobject
jOutputBuffer
,
jint
sampleRate
,
jobject
mediaCrypto
,
jint
inputMode
,
jbyteArray
key
,
jbyteArray
javaIv
,
jint
inputNumSubSamples
,
jintArray
numBytesOfClearData
,
jintArray
numBytesOfEncryptedData
)
{
jobject
jInputBuffer
,
jint
inputSize
,
jobject
jOutputBuffer
,
jint
sampleRate
,
jobject
mediaCrypto
,
jint
inputMode
,
jbyteArray
key
,
jbyteArray
javaIv
,
jint
inputNumSubSamples
,
jintArray
numBytesOfClearData
,
jintArray
numBytesOfEncryptedData
)
{
// Doesn't support
// Java client should have checked vpxSupportSecureDecode
// and avoid calling this
...
...
@@ -163,13 +164,9 @@ DECODER_FUNC(jstring, opusGetErrorMessage, jlong jContext) {
return
env
->
NewStringUTF
(
opus_strerror
(
errorCode
));
}
DECODER_FUNC
(
jint
,
opusGetErrorCode
,
jlong
jContext
)
{
return
errorCode
;
}
DECODER_FUNC
(
jint
,
opusGetErrorCode
,
jlong
jContext
)
{
return
errorCode
;
}
DECODER_FUNC
(
void
,
opusSetFloatOutput
)
{
outputFloat
=
true
;
}
DECODER_FUNC
(
void
,
opusSetFloatOutput
)
{
outputFloat
=
true
;
}
LIBRARY_FUNC
(
jstring
,
opusIsSecureDecodeSupported
)
{
// Doesn't support
...
...
extensions/vp9/src/main/jni/vpx_jni.cc
View file @
0de6bc86
...
...
@@ -18,12 +18,12 @@
#ifdef __ARM_NEON__
#include <arm_neon.h>
#endif
#include <jni.h>
#include <android/log.h>
#include <android/native_window.h>
#include <android/native_window_jni.h>
#include <jni.h>
#include <pthread.h>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
...
...
@@ -31,12 +31,12 @@
#include <new>
#define VPX_CODEC_DISABLE_COMPAT 1
#include "vpx/vpx_decoder.h"
#include "vpx/vp8dx.h"
#include "vpx/vpx_decoder.h"
#define LOG_TAG "vpx_jni"
#define LOGE(...)
((void)__android_log_print(ANDROID_LOG_ERROR, LOG_TAG,
\
__VA_ARGS__))
#define LOGE(...) \
((void)__android_log_print(ANDROID_LOG_ERROR, LOG_TAG,
__VA_ARGS__))
#define DECODER_FUNC(RETURN_TYPE, NAME, ...) \
extern "C" { \
...
...
@@ -480,12 +480,12 @@ DECODER_FUNC(jlong, vpxInit, jboolean disableLoopFilter,
// Populate JNI References.
const
jclass
outputBufferClass
=
env
->
FindClass
(
"com/google/android/exoplayer2/video/VideoDecoderOutputBuffer"
);
initForYuvFrame
=
env
->
GetMethodID
(
outputBufferClass
,
"initForYuvFrame"
,
"(IIIII)Z"
);
initForYuvFrame
=
env
->
GetMethodID
(
outputBufferClass
,
"initForYuvFrame"
,
"(IIIII)Z"
);
initForPrivateFrame
=
env
->
GetMethodID
(
outputBufferClass
,
"initForPrivateFrame"
,
"(II)V"
);
dataField
=
env
->
GetFieldID
(
outputBufferClass
,
"data"
,
"Ljava/nio/ByteBuffer;"
);
dataField
=
env
->
GetFieldID
(
outputBufferClass
,
"data"
,
"Ljava/nio/ByteBuffer;"
);
outputModeField
=
env
->
GetFieldID
(
outputBufferClass
,
"mode"
,
"I"
);
decoderPrivateField
=
env
->
GetFieldID
(
outputBufferClass
,
"decoderPrivate"
,
"I"
);
...
...
@@ -508,9 +508,9 @@ DECODER_FUNC(jlong, vpxDecode, jlong jContext, jobject encoded, jint len) {
}
DECODER_FUNC
(
jlong
,
vpxSecureDecode
,
jlong
jContext
,
jobject
encoded
,
jint
len
,
jobject
mediaCrypto
,
jint
inputMode
,
jbyteArray
&
,
jbyteArray
&
,
jint
inputNumSubSamples
,
jintArray
numBytesOfClearData
,
jintArray
numBytesOfEncryptedData
)
{
jobject
mediaCrypto
,
jint
inputMode
,
jbyteArray
&
,
jbyteArray
&
,
jint
inputNumSubSamples
,
jintArray
numBytesOfClearData
,
jintArray
numBytesOfEncryptedData
)
{
// Doesn't support
// Java client should have checked vpxSupportSecureDecode
// and avoid calling this
...
...
@@ -552,7 +552,7 @@ DECODER_FUNC(jint, vpxGetFrame, jlong jContext, jobject jOutputBuffer) {
case
VPX_CS_BT_709
:
colorspace
=
kColorspaceBT709
;
break
;
case
VPX_CS_BT_2020
:
case
VPX_CS_BT_2020
:
colorspace
=
kColorspaceBT2020
;
break
;
default
:
...
...
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