Format *_jni.cc files

PiperOrigin-RevId: 374830877
This commit is contained in:
olly 2021-05-20 11:00:13 +01:00 committed by Oliver Woodman
parent 9e4315f48d
commit 0de6bc861a
4 changed files with 86 additions and 89 deletions

View File

@ -13,9 +13,9 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
#include <android/log.h>
#include <jni.h> #include <jni.h>
#include <stdlib.h> #include <stdlib.h>
#include <android/log.h>
extern "C" { extern "C" {
#ifdef __cplusplus #ifdef __cplusplus
@ -33,8 +33,8 @@ extern "C" {
} }
#define LOG_TAG "ffmpeg_jni" #define LOG_TAG "ffmpeg_jni"
#define LOGE(...) ((void)__android_log_print(ANDROID_LOG_ERROR, LOG_TAG, \ #define LOGE(...) \
__VA_ARGS__)) ((void)__android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__))
#define LIBRARY_FUNC(RETURN_TYPE, NAME, ...) \ #define LIBRARY_FUNC(RETURN_TYPE, NAME, ...) \
extern "C" { \ 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. * 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 * 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) { jint JNI_OnLoad(JavaVM *vm, void *reserved) {
JNIEnv *env; 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; return -1;
} }
avcodec_register_all(); avcodec_register_all();
@ -149,13 +149,13 @@ AUDIO_DECODER_FUNC(jint, ffmpegDecode, jlong context, jobject inputData,
LOGE("Invalid output buffer length: %d", outputSize); LOGE("Invalid output buffer length: %d", outputSize);
return -1; return -1;
} }
uint8_t *inputBuffer = (uint8_t *) env->GetDirectBufferAddress(inputData); uint8_t *inputBuffer = (uint8_t *)env->GetDirectBufferAddress(inputData);
uint8_t *outputBuffer = (uint8_t *) env->GetDirectBufferAddress(outputData); uint8_t *outputBuffer = (uint8_t *)env->GetDirectBufferAddress(outputData);
AVPacket packet; AVPacket packet;
av_init_packet(&packet); av_init_packet(&packet);
packet.data = inputBuffer; packet.data = inputBuffer;
packet.size = inputSize; packet.size = inputSize;
return decodePacket((AVCodecContext *) context, &packet, outputBuffer, return decodePacket((AVCodecContext *)context, &packet, outputBuffer,
outputSize); outputSize);
} }
@ -164,7 +164,7 @@ AUDIO_DECODER_FUNC(jint, ffmpegGetChannelCount, jlong context) {
LOGE("Context must be non-NULL."); LOGE("Context must be non-NULL.");
return -1; return -1;
} }
return ((AVCodecContext *) context)->channels; return ((AVCodecContext *)context)->channels;
} }
AUDIO_DECODER_FUNC(jint, ffmpegGetSampleRate, jlong context) { AUDIO_DECODER_FUNC(jint, ffmpegGetSampleRate, jlong context) {
@ -172,11 +172,11 @@ AUDIO_DECODER_FUNC(jint, ffmpegGetSampleRate, jlong context) {
LOGE("Context must be non-NULL."); LOGE("Context must be non-NULL.");
return -1; return -1;
} }
return ((AVCodecContext *) context)->sample_rate; return ((AVCodecContext *)context)->sample_rate;
} }
AUDIO_DECODER_FUNC(jlong, ffmpegReset, jlong jContext, jbyteArray extraData) { AUDIO_DECODER_FUNC(jlong, ffmpegReset, jlong jContext, jbyteArray extraData) {
AVCodecContext *context = (AVCodecContext *) jContext; AVCodecContext *context = (AVCodecContext *)jContext;
if (!context) { if (!context) {
LOGE("Tried to reset without a context."); LOGE("Tried to reset without a context.");
return 0L; return 0L;
@ -200,16 +200,16 @@ AUDIO_DECODER_FUNC(jlong, ffmpegReset, jlong jContext, jbyteArray extraData) {
} }
avcodec_flush_buffers(context); avcodec_flush_buffers(context);
return (jlong) context; return (jlong)context;
} }
AUDIO_DECODER_FUNC(void, ffmpegRelease, jlong context) { AUDIO_DECODER_FUNC(void, ffmpegRelease, jlong context) {
if (context) { if (context) {
releaseContext((AVCodecContext *) context); releaseContext((AVCodecContext *)context);
} }
} }
AVCodec *getCodecByName(JNIEnv* env, jstring codecName) { AVCodec *getCodecByName(JNIEnv *env, jstring codecName) {
if (!codecName) { if (!codecName) {
return NULL; return NULL;
} }
@ -233,13 +233,13 @@ AVCodecContext *createContext(JNIEnv *env, AVCodec *codec, jbyteArray extraData,
jsize size = env->GetArrayLength(extraData); jsize size = env->GetArrayLength(extraData);
context->extradata_size = size; context->extradata_size = size;
context->extradata = 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) { if (!context->extradata) {
LOGE("Failed to allocate extradata."); LOGE("Failed to allocate extradata.");
releaseContext(context); releaseContext(context);
return NULL; 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 || if (context->codec_id == AV_CODEC_ID_PCM_MULAW ||
context->codec_id == AV_CODEC_ID_PCM_ALAW) { context->codec_id == AV_CODEC_ID_PCM_ALAW) {
@ -345,7 +345,7 @@ int decodePacket(AVCodecContext *context, AVPacket *packet,
} }
void logError(const char *functionName, int errorNumber) { 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); av_strerror(errorNumber, buffer, ERROR_STRING_BUFFER_LENGTH);
LOGE("Error in %s: %s", functionName, buffer); LOGE("Error in %s: %s", functionName, buffer);
free(buffer); free(buffer);
@ -362,4 +362,3 @@ void releaseContext(AVCodecContext *context) {
} }
avcodec_free_context(&context); avcodec_free_context(&context);
} }

View File

@ -32,8 +32,9 @@
#define DECODER_FUNC(RETURN_TYPE, NAME, ...) \ #define DECODER_FUNC(RETURN_TYPE, NAME, ...) \
extern "C" { \ extern "C" { \
JNIEXPORT RETURN_TYPE \ JNIEXPORT RETURN_TYPE \
Java_com_google_android_exoplayer2_ext_flac_FlacDecoderJni_##NAME( \ Java_com_google_android_exoplayer2_ext_flac_FlacDecoderJni_##NAME(JNIEnv *env, \
JNIEnv *env, jobject thiz, ##__VA_ARGS__); \ jobject thiz, \
##__VA_ARGS__); \
} \ } \
JNIEXPORT RETURN_TYPE \ JNIEXPORT RETURN_TYPE \
Java_com_google_android_exoplayer2_ext_flac_FlacDecoderJni_##NAME( \ Java_com_google_android_exoplayer2_ext_flac_FlacDecoderJni_##NAME( \

View File

@ -14,9 +14,8 @@
* limitations under the License. * limitations under the License.
*/ */
#include <jni.h>
#include <android/log.h> #include <android/log.h>
#include <jni.h>
#include <cstdlib> #include <cstdlib>
@ -24,28 +23,28 @@
#include "opus_multistream.h" // NOLINT #include "opus_multistream.h" // NOLINT
#define LOG_TAG "opus_jni" #define LOG_TAG "opus_jni"
#define LOGE(...) ((void)__android_log_print(ANDROID_LOG_ERROR, LOG_TAG, \ #define LOGE(...) \
__VA_ARGS__)) ((void)__android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__))
#define DECODER_FUNC(RETURN_TYPE, NAME, ...) \ #define DECODER_FUNC(RETURN_TYPE, NAME, ...) \
extern "C" { \ extern "C" { \
JNIEXPORT RETURN_TYPE \ JNIEXPORT RETURN_TYPE \
Java_com_google_android_exoplayer2_ext_opus_OpusDecoder_ ## NAME \ Java_com_google_android_exoplayer2_ext_opus_OpusDecoder_##NAME( \
(JNIEnv* env, jobject thiz, ##__VA_ARGS__);\ JNIEnv* env, jobject thiz, ##__VA_ARGS__); \
} \ } \
JNIEXPORT RETURN_TYPE \ JNIEXPORT RETURN_TYPE \
Java_com_google_android_exoplayer2_ext_opus_OpusDecoder_ ## NAME \ Java_com_google_android_exoplayer2_ext_opus_OpusDecoder_##NAME( \
(JNIEnv* env, jobject thiz, ##__VA_ARGS__)\ JNIEnv* env, jobject thiz, ##__VA_ARGS__)
#define LIBRARY_FUNC(RETURN_TYPE, NAME, ...) \ #define LIBRARY_FUNC(RETURN_TYPE, NAME, ...) \
extern "C" { \ extern "C" { \
JNIEXPORT RETURN_TYPE \ JNIEXPORT RETURN_TYPE \
Java_com_google_android_exoplayer2_ext_opus_OpusLibrary_ ## NAME \ Java_com_google_android_exoplayer2_ext_opus_OpusLibrary_##NAME( \
(JNIEnv* env, jobject thiz, ##__VA_ARGS__);\ JNIEnv* env, jobject thiz, ##__VA_ARGS__); \
} \ } \
JNIEXPORT RETURN_TYPE \ JNIEXPORT RETURN_TYPE \
Java_com_google_android_exoplayer2_ext_opus_OpusLibrary_ ## NAME \ Java_com_google_android_exoplayer2_ext_opus_OpusLibrary_##NAME( \
(JNIEnv* env, jobject thiz, ##__VA_ARGS__)\ JNIEnv* env, jobject thiz, ##__VA_ARGS__)
// JNI references for SimpleOutputBuffer class. // JNI references for SimpleOutputBuffer class.
static jmethodID outputBufferInit; static jmethodID outputBufferInit;
@ -66,7 +65,8 @@ static int errorCode;
static bool outputFloat = false; static bool outputFloat = false;
DECODER_FUNC(jlong, opusInit, jint sampleRate, jint channelCount, 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; int status = OPUS_INVALID_STATE;
::channelCount = channelCount; ::channelCount = channelCount;
errorCode = 0; errorCode = 0;
@ -88,8 +88,8 @@ DECODER_FUNC(jlong, opusInit, jint sampleRate, jint channelCount,
// Populate JNI References. // Populate JNI References.
const jclass outputBufferClass = env->FindClass( const jclass outputBufferClass = env->FindClass(
"com/google/android/exoplayer2/decoder/SimpleOutputBuffer"); "com/google/android/exoplayer2/decoder/SimpleOutputBuffer");
outputBufferInit = env->GetMethodID(outputBufferClass, "init", outputBufferInit =
"(JI)Ljava/nio/ByteBuffer;"); env->GetMethodID(outputBufferClass, "init", "(JI)Ljava/nio/ByteBuffer;");
return reinterpret_cast<intptr_t>(decoder); return reinterpret_cast<intptr_t>(decoder);
} }
@ -97,12 +97,11 @@ DECODER_FUNC(jlong, opusInit, jint sampleRate, jint channelCount,
DECODER_FUNC(jint, opusDecode, jlong jDecoder, jlong jTimeUs, 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); OpusMSDecoder* decoder = reinterpret_cast<OpusMSDecoder*>(jDecoder);
const uint8_t* inputBuffer = const uint8_t* inputBuffer = reinterpret_cast<const uint8_t*>(
reinterpret_cast<const uint8_t*>(
env->GetDirectBufferAddress(jInputBuffer)); env->GetDirectBufferAddress(jInputBuffer));
const int byteSizePerSample = outputFloat ? const int byteSizePerSample =
kBytesPerFloatSample : kBytesPerIntPcmSample; outputFloat ? kBytesPerFloatSample : kBytesPerIntPcmSample;
const jint outputSize = const jint outputSize =
kMaxOpusOutputPacketSizeSamples * byteSizePerSample * channelCount; 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. // Exception is thrown in Java when returning from the native call.
return -1; return -1;
} }
const jobject jOutputBufferData = env->CallObjectMethod(jOutputBuffer, const jobject jOutputBufferData = env->CallObjectMethod(
outputBufferInit, jTimeUs, outputSize); jOutputBuffer, outputBufferInit, jTimeUs, outputSize);
if (env->ExceptionCheck()) { if (env->ExceptionCheck()) {
// Exception is thrown in Java when returning from the native call. // Exception is thrown in Java when returning from the native call.
return -1; return -1;
@ -122,13 +121,15 @@ DECODER_FUNC(jint, opusDecode, jlong jDecoder, jlong jTimeUs,
if (outputFloat) { if (outputFloat) {
float* outputBufferData = reinterpret_cast<float*>( float* outputBufferData = reinterpret_cast<float*>(
env->GetDirectBufferAddress(jOutputBufferData)); env->GetDirectBufferAddress(jOutputBufferData));
sampleCount = opus_multistream_decode_float(decoder, inputBuffer, inputSize, sampleCount = opus_multistream_decode_float(
outputBufferData, kMaxOpusOutputPacketSizeSamples, 0); decoder, inputBuffer, inputSize, outputBufferData,
kMaxOpusOutputPacketSizeSamples, 0);
} else { } else {
int16_t* outputBufferData = reinterpret_cast<int16_t*>( int16_t* outputBufferData = reinterpret_cast<int16_t*>(
env->GetDirectBufferAddress(jOutputBufferData)); env->GetDirectBufferAddress(jOutputBufferData));
sampleCount = opus_multistream_decode(decoder, inputBuffer, inputSize, sampleCount = opus_multistream_decode(decoder, inputBuffer, inputSize,
outputBufferData, kMaxOpusOutputPacketSizeSamples, 0); outputBufferData,
kMaxOpusOutputPacketSizeSamples, 0);
} }
// record error code // record error code
@ -139,9 +140,9 @@ DECODER_FUNC(jint, opusDecode, jlong jDecoder, jlong jTimeUs,
DECODER_FUNC(jint, opusSecureDecode, jlong jDecoder, jlong jTimeUs, DECODER_FUNC(jint, opusSecureDecode, jlong jDecoder, jlong jTimeUs,
jobject jInputBuffer, jint inputSize, jobject jOutputBuffer, jobject jInputBuffer, jint inputSize, jobject jOutputBuffer,
jint sampleRate, jobject mediaCrypto, jint inputMode, jbyteArray key, jint sampleRate, jobject mediaCrypto, jint inputMode,
jbyteArray javaIv, jint inputNumSubSamples, jintArray numBytesOfClearData, jbyteArray key, jbyteArray javaIv, jint inputNumSubSamples,
jintArray numBytesOfEncryptedData) { jintArray numBytesOfClearData, jintArray numBytesOfEncryptedData) {
// Doesn't support // Doesn't support
// Java client should have checked vpxSupportSecureDecode // Java client should have checked vpxSupportSecureDecode
// and avoid calling this // and avoid calling this
@ -163,13 +164,9 @@ DECODER_FUNC(jstring, opusGetErrorMessage, jlong jContext) {
return env->NewStringUTF(opus_strerror(errorCode)); return env->NewStringUTF(opus_strerror(errorCode));
} }
DECODER_FUNC(jint, opusGetErrorCode, jlong jContext) { DECODER_FUNC(jint, opusGetErrorCode, jlong jContext) { return errorCode; }
return errorCode;
}
DECODER_FUNC(void, opusSetFloatOutput) { DECODER_FUNC(void, opusSetFloatOutput) { outputFloat = true; }
outputFloat = true;
}
LIBRARY_FUNC(jstring, opusIsSecureDecodeSupported) { LIBRARY_FUNC(jstring, opusIsSecureDecodeSupported) {
// Doesn't support // Doesn't support

View File

@ -18,12 +18,12 @@
#ifdef __ARM_NEON__ #ifdef __ARM_NEON__
#include <arm_neon.h> #include <arm_neon.h>
#endif #endif
#include <jni.h>
#include <android/log.h> #include <android/log.h>
#include <android/native_window.h> #include <android/native_window.h>
#include <android/native_window_jni.h> #include <android/native_window_jni.h>
#include <jni.h>
#include <pthread.h> #include <pthread.h>
#include <algorithm> #include <algorithm>
#include <cstdio> #include <cstdio>
#include <cstdlib> #include <cstdlib>
@ -31,12 +31,12 @@
#include <new> #include <new>
#define VPX_CODEC_DISABLE_COMPAT 1 #define VPX_CODEC_DISABLE_COMPAT 1
#include "vpx/vpx_decoder.h"
#include "vpx/vp8dx.h" #include "vpx/vp8dx.h"
#include "vpx/vpx_decoder.h"
#define LOG_TAG "vpx_jni" #define LOG_TAG "vpx_jni"
#define LOGE(...) ((void)__android_log_print(ANDROID_LOG_ERROR, LOG_TAG, \ #define LOGE(...) \
__VA_ARGS__)) ((void)__android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__))
#define DECODER_FUNC(RETURN_TYPE, NAME, ...) \ #define DECODER_FUNC(RETURN_TYPE, NAME, ...) \
extern "C" { \ extern "C" { \
@ -480,12 +480,12 @@ DECODER_FUNC(jlong, vpxInit, jboolean disableLoopFilter,
// Populate JNI References. // Populate JNI References.
const jclass outputBufferClass = env->FindClass( const jclass outputBufferClass = env->FindClass(
"com/google/android/exoplayer2/video/VideoDecoderOutputBuffer"); "com/google/android/exoplayer2/video/VideoDecoderOutputBuffer");
initForYuvFrame = env->GetMethodID(outputBufferClass, "initForYuvFrame", initForYuvFrame =
"(IIIII)Z"); env->GetMethodID(outputBufferClass, "initForYuvFrame", "(IIIII)Z");
initForPrivateFrame = initForPrivateFrame =
env->GetMethodID(outputBufferClass, "initForPrivateFrame", "(II)V"); env->GetMethodID(outputBufferClass, "initForPrivateFrame", "(II)V");
dataField = env->GetFieldID(outputBufferClass, "data", dataField =
"Ljava/nio/ByteBuffer;"); env->GetFieldID(outputBufferClass, "data", "Ljava/nio/ByteBuffer;");
outputModeField = env->GetFieldID(outputBufferClass, "mode", "I"); outputModeField = env->GetFieldID(outputBufferClass, "mode", "I");
decoderPrivateField = decoderPrivateField =
env->GetFieldID(outputBufferClass, "decoderPrivate", "I"); env->GetFieldID(outputBufferClass, "decoderPrivate", "I");