Telegram-Android/TMessagesProj/jni/exoplayer/ffmpeg_jni.cc

365 lines
12 KiB
C++
Raw Normal View History

2018-07-30 04:07:02 +02:00
/*
* Copyright (C) 2016 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.
*/
2023-04-03 15:15:52 +02:00
#include <android/log.h>
2018-07-30 04:07:02 +02:00
#include <jni.h>
#include <stdlib.h>
extern "C" {
#ifdef __cplusplus
2023-04-03 15:15:52 +02:00
#define __STDC_CONSTANT_MACROS
2018-07-30 04:07:02 +02:00
#ifdef _STDINT_H
#undef _STDINT_H
#endif
#include <stdint.h>
#endif
#include <libavcodec/avcodec.h>
2019-01-23 18:03:33 +01:00
#include <libavutil/channel_layout.h>
2018-07-30 04:07:02 +02:00
#include <libavutil/error.h>
#include <libavutil/opt.h>
2023-04-03 15:15:52 +02:00
#include <libswresample/swresample.h>
2018-07-30 04:07:02 +02:00
}
#define LOG_TAG "ffmpeg_jni"
2023-04-03 15:15:52 +02:00
#define LOGE(...) \
((void)__android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__))
#define LIBRARY_FUNC(RETURN_TYPE, NAME, ...) \
extern "C" { \
JNIEXPORT RETURN_TYPE \
Java_com_google_android_exoplayer2_ext_ffmpeg_FfmpegLibrary_##NAME( \
JNIEnv *env, jobject thiz, ##__VA_ARGS__); \
} \
JNIEXPORT RETURN_TYPE \
Java_com_google_android_exoplayer2_ext_ffmpeg_FfmpegLibrary_##NAME( \
JNIEnv *env, jobject thiz, ##__VA_ARGS__)
#define AUDIO_DECODER_FUNC(RETURN_TYPE, NAME, ...) \
extern "C" { \
JNIEXPORT RETURN_TYPE \
Java_com_google_android_exoplayer2_ext_ffmpeg_FfmpegAudioDecoder_##NAME( \
JNIEnv *env, jobject thiz, ##__VA_ARGS__); \
} \
JNIEXPORT RETURN_TYPE \
Java_com_google_android_exoplayer2_ext_ffmpeg_FfmpegAudioDecoder_##NAME( \
JNIEnv *env, jobject thiz, ##__VA_ARGS__)
2018-07-30 04:07:02 +02:00
#define ERROR_STRING_BUFFER_LENGTH 256
// Output format corresponding to AudioFormat.ENCODING_PCM_16BIT.
static const AVSampleFormat OUTPUT_FORMAT_PCM_16BIT = AV_SAMPLE_FMT_S16;
// Output format corresponding to AudioFormat.ENCODING_PCM_FLOAT.
static const AVSampleFormat OUTPUT_FORMAT_PCM_FLOAT = AV_SAMPLE_FMT_FLT;
2023-04-03 15:15:52 +02:00
static const int AUDIO_DECODER_ERROR_INVALID_DATA = -1;
static const int AUDIO_DECODER_ERROR_OTHER = -2;
2019-01-23 18:03:33 +01:00
2018-07-30 04:07:02 +02:00
/**
* Returns the AVCodec with the specified name, or NULL if it is not available.
*/
2023-04-03 15:15:52 +02:00
AVCodec *getCodecByName(JNIEnv *env, jstring codecName);
2018-07-30 04:07:02 +02:00
/**
* Allocates and opens a new AVCodecContext for the specified codec, passing the
* provided extraData as initialization data for the decoder if it is non-NULL.
* Returns the created context.
*/
2018-08-27 10:33:11 +02:00
AVCodecContext *createContext(JNIEnv *env, AVCodec *codec, jbyteArray extraData,
jboolean outputFloat, jint rawSampleRate,
jint rawChannelCount);
2018-07-30 04:07:02 +02:00
/**
* Decodes the packet into the output buffer, returning the number of bytes
2023-04-03 15:15:52 +02:00
* written, or a negative AUDIO_DECODER_ERROR constant value in the case of an
* error.
2018-07-30 04:07:02 +02:00
*/
int decodePacket(AVCodecContext *context, AVPacket *packet,
uint8_t *outputBuffer, int outputSize);
2023-04-03 15:15:52 +02:00
/**
* Transforms ffmpeg AVERROR into a negative AUDIO_DECODER_ERROR constant value.
*/
int transformError(int errorNumber);
2018-07-30 04:07:02 +02:00
/**
* Outputs a log message describing the avcodec error number.
*/
void logError(const char *functionName, int errorNumber);
/**
* Releases the specified context.
*/
void releaseContext(AVCodecContext *context);
LIBRARY_FUNC(jstring, ffmpegGetVersion) {
return env->NewStringUTF(LIBAVCODEC_IDENT);
}
2023-04-03 15:15:52 +02:00
LIBRARY_FUNC(jint, ffmpegGetInputBufferPaddingSize) {
return (jint)AV_INPUT_BUFFER_PADDING_SIZE;
}
2018-07-30 04:07:02 +02:00
LIBRARY_FUNC(jboolean, ffmpegHasDecoder, jstring codecName) {
return getCodecByName(env, codecName) != NULL;
}
2023-04-03 15:15:52 +02:00
AUDIO_DECODER_FUNC(jlong, ffmpegInitialize, jstring codecName,
jbyteArray extraData, jboolean outputFloat,
jint rawSampleRate, jint rawChannelCount) {
2018-07-30 04:07:02 +02:00
AVCodec *codec = getCodecByName(env, codecName);
if (!codec) {
LOGE("Codec not found.");
return 0L;
}
2018-08-27 10:33:11 +02:00
return (jlong)createContext(env, codec, extraData, outputFloat, rawSampleRate,
rawChannelCount);
2018-07-30 04:07:02 +02:00
}
2023-04-03 15:15:52 +02:00
AUDIO_DECODER_FUNC(jint, ffmpegDecode, jlong context, jobject inputData,
jint inputSize, jobject outputData, jint outputSize) {
2018-07-30 04:07:02 +02:00
if (!context) {
LOGE("Context must be non-NULL.");
return -1;
}
if (!inputData || !outputData) {
LOGE("Input and output buffers must be non-NULL.");
return -1;
}
if (inputSize < 0) {
LOGE("Invalid input buffer size: %d.", inputSize);
return -1;
}
if (outputSize < 0) {
LOGE("Invalid output buffer length: %d", outputSize);
return -1;
}
2023-04-03 15:15:52 +02:00
uint8_t *inputBuffer = (uint8_t *)env->GetDirectBufferAddress(inputData);
uint8_t *outputBuffer = (uint8_t *)env->GetDirectBufferAddress(outputData);
2018-07-30 04:07:02 +02:00
AVPacket packet;
av_init_packet(&packet);
packet.data = inputBuffer;
packet.size = inputSize;
2023-04-03 15:15:52 +02:00
return decodePacket((AVCodecContext *)context, &packet, outputBuffer,
2018-07-30 04:07:02 +02:00
outputSize);
}
2023-04-03 15:15:52 +02:00
AUDIO_DECODER_FUNC(jint, ffmpegGetChannelCount, jlong context) {
2018-07-30 04:07:02 +02:00
if (!context) {
LOGE("Context must be non-NULL.");
return -1;
}
2023-04-03 15:15:52 +02:00
return ((AVCodecContext *)context)->channels;
2018-07-30 04:07:02 +02:00
}
2023-04-03 15:15:52 +02:00
AUDIO_DECODER_FUNC(jint, ffmpegGetSampleRate, jlong context) {
2018-07-30 04:07:02 +02:00
if (!context) {
LOGE("Context must be non-NULL.");
return -1;
}
2023-04-03 15:15:52 +02:00
return ((AVCodecContext *)context)->sample_rate;
2018-07-30 04:07:02 +02:00
}
2023-04-03 15:15:52 +02:00
AUDIO_DECODER_FUNC(jlong, ffmpegReset, jlong jContext, jbyteArray extraData) {
AVCodecContext *context = (AVCodecContext *)jContext;
2018-07-30 04:07:02 +02:00
if (!context) {
LOGE("Tried to reset without a context.");
return 0L;
}
AVCodecID codecId = context->codec_id;
if (codecId == AV_CODEC_ID_TRUEHD) {
// Release and recreate the context if the codec is TrueHD.
// TODO: Figure out why flushing doesn't work for this codec.
releaseContext(context);
AVCodec *codec = avcodec_find_decoder(codecId);
if (!codec) {
LOGE("Unexpected error finding codec %d.", codecId);
return 0L;
}
2018-08-27 10:33:11 +02:00
jboolean outputFloat =
2023-04-03 15:15:52 +02:00
(jboolean)(context->request_sample_fmt == OUTPUT_FORMAT_PCM_FLOAT);
2018-08-27 10:33:11 +02:00
return (jlong)createContext(env, codec, extraData, outputFloat,
2023-04-03 15:15:52 +02:00
/* rawSampleRate= */ -1,
/* rawChannelCount= */ -1);
2018-07-30 04:07:02 +02:00
}
avcodec_flush_buffers(context);
2023-04-03 15:15:52 +02:00
return (jlong)context;
2018-07-30 04:07:02 +02:00
}
2023-04-03 15:15:52 +02:00
AUDIO_DECODER_FUNC(void, ffmpegRelease, jlong context) {
2018-07-30 04:07:02 +02:00
if (context) {
2023-04-03 15:15:52 +02:00
releaseContext((AVCodecContext *)context);
2018-07-30 04:07:02 +02:00
}
}
2023-04-03 15:15:52 +02:00
AVCodec *getCodecByName(JNIEnv *env, jstring codecName) {
2018-07-30 04:07:02 +02:00
if (!codecName) {
return NULL;
}
const char *codecNameChars = env->GetStringUTFChars(codecName, NULL);
AVCodec *codec = avcodec_find_decoder_by_name(codecNameChars);
env->ReleaseStringUTFChars(codecName, codecNameChars);
return codec;
}
2018-08-27 10:33:11 +02:00
AVCodecContext *createContext(JNIEnv *env, AVCodec *codec, jbyteArray extraData,
jboolean outputFloat, jint rawSampleRate,
jint rawChannelCount) {
2018-07-30 04:07:02 +02:00
AVCodecContext *context = avcodec_alloc_context3(codec);
if (!context) {
LOGE("Failed to allocate context.");
return NULL;
}
context->request_sample_fmt =
2023-04-03 15:15:52 +02:00
outputFloat ? OUTPUT_FORMAT_PCM_FLOAT : OUTPUT_FORMAT_PCM_16BIT;
2018-07-30 04:07:02 +02:00
if (extraData) {
jsize size = env->GetArrayLength(extraData);
context->extradata_size = size;
context->extradata =
2023-04-03 15:15:52 +02:00
(uint8_t *)av_malloc(size + AV_INPUT_BUFFER_PADDING_SIZE);
2018-07-30 04:07:02 +02:00
if (!context->extradata) {
LOGE("Failed to allocate extradata.");
releaseContext(context);
return NULL;
}
2023-04-03 15:15:52 +02:00
env->GetByteArrayRegion(extraData, 0, size, (jbyte *)context->extradata);
2018-07-30 04:07:02 +02:00
}
2018-08-27 10:33:11 +02:00
if (context->codec_id == AV_CODEC_ID_PCM_MULAW ||
context->codec_id == AV_CODEC_ID_PCM_ALAW) {
context->sample_rate = rawSampleRate;
context->channels = rawChannelCount;
context->channel_layout = av_get_default_channel_layout(rawChannelCount);
}
2019-01-23 18:03:33 +01:00
context->err_recognition = AV_EF_IGNORE_ERR;
2018-07-30 04:07:02 +02:00
int result = avcodec_open2(context, codec, NULL);
if (result < 0) {
logError("avcodec_open2", result);
releaseContext(context);
return NULL;
}
return context;
}
int decodePacket(AVCodecContext *context, AVPacket *packet,
uint8_t *outputBuffer, int outputSize) {
int result = 0;
// Queue input data.
result = avcodec_send_packet(context, packet);
if (result) {
logError("avcodec_send_packet", result);
2023-04-03 15:15:52 +02:00
return transformError(result);
2018-07-30 04:07:02 +02:00
}
// Dequeue output data until it runs out.
int outSize = 0;
while (true) {
AVFrame *frame = av_frame_alloc();
if (!frame) {
LOGE("Failed to allocate output frame.");
2023-04-03 15:15:52 +02:00
return AUDIO_DECODER_ERROR_INVALID_DATA;
2018-07-30 04:07:02 +02:00
}
result = avcodec_receive_frame(context, frame);
if (result) {
av_frame_free(&frame);
if (result == AVERROR(EAGAIN)) {
break;
}
logError("avcodec_receive_frame", result);
2023-04-03 15:15:52 +02:00
return transformError(result);
2018-07-30 04:07:02 +02:00
}
// Resample output.
AVSampleFormat sampleFormat = context->sample_fmt;
int channelCount = context->channels;
2018-08-27 10:33:11 +02:00
int channelLayout = context->channel_layout;
2018-07-30 04:07:02 +02:00
int sampleRate = context->sample_rate;
int sampleCount = frame->nb_samples;
int dataSize = av_samples_get_buffer_size(NULL, channelCount, sampleCount,
sampleFormat, 1);
2023-04-03 15:15:52 +02:00
SwrContext *resampleContext;
2018-07-30 04:07:02 +02:00
if (context->opaque) {
2023-04-03 15:15:52 +02:00
resampleContext = (SwrContext *)context->opaque;
2018-07-30 04:07:02 +02:00
} else {
2023-04-03 15:15:52 +02:00
resampleContext = swr_alloc();
av_opt_set_int(resampleContext, "in_channel_layout", channelLayout, 0);
2018-07-30 04:07:02 +02:00
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",
2023-04-03 15:15:52 +02:00
context->request_sample_fmt, 0);
result = swr_init(resampleContext);
2018-07-30 04:07:02 +02:00
if (result < 0) {
2023-04-03 15:15:52 +02:00
logError("swr_init", result);
2018-07-30 04:07:02 +02:00
av_frame_free(&frame);
2023-04-03 15:15:52 +02:00
return transformError(result);
2018-07-30 04:07:02 +02:00
}
context->opaque = resampleContext;
}
int inSampleSize = av_get_bytes_per_sample(sampleFormat);
int outSampleSize = av_get_bytes_per_sample(context->request_sample_fmt);
2023-04-03 15:15:52 +02:00
int outSamples = swr_get_out_samples(resampleContext, sampleCount);
2018-07-30 04:07:02 +02:00
int bufferOutSize = outSampleSize * channelCount * outSamples;
if (outSize + bufferOutSize > outputSize) {
LOGE("Output buffer size (%d) too small for output data (%d).",
outputSize, outSize + bufferOutSize);
av_frame_free(&frame);
2023-04-03 15:15:52 +02:00
return AUDIO_DECODER_ERROR_INVALID_DATA;
2018-07-30 04:07:02 +02:00
}
2023-04-03 15:15:52 +02:00
result = swr_convert(resampleContext, &outputBuffer, bufferOutSize,
(const uint8_t **)frame->data, frame->nb_samples);
2018-07-30 04:07:02 +02:00
av_frame_free(&frame);
if (result < 0) {
2023-04-03 15:15:52 +02:00
logError("swr_convert", result);
return AUDIO_DECODER_ERROR_INVALID_DATA;
2018-07-30 04:07:02 +02:00
}
2023-04-03 15:15:52 +02:00
int available = swr_get_out_samples(resampleContext, 0);
2018-07-30 04:07:02 +02:00
if (available != 0) {
LOGE("Expected no samples remaining after resampling, but found %d.",
available);
2023-04-03 15:15:52 +02:00
return AUDIO_DECODER_ERROR_INVALID_DATA;
2018-07-30 04:07:02 +02:00
}
outputBuffer += bufferOutSize;
outSize += bufferOutSize;
}
return outSize;
}
2023-04-03 15:15:52 +02:00
int transformError(int errorNumber) {
return errorNumber == AVERROR_INVALIDDATA ? AUDIO_DECODER_ERROR_INVALID_DATA
: AUDIO_DECODER_ERROR_OTHER;
}
2018-07-30 04:07:02 +02:00
void logError(const char *functionName, int errorNumber) {
2023-04-03 15:15:52 +02:00
char *buffer = (char *)malloc(ERROR_STRING_BUFFER_LENGTH * sizeof(char));
2018-07-30 04:07:02 +02:00
av_strerror(errorNumber, buffer, ERROR_STRING_BUFFER_LENGTH);
LOGE("Error in %s: %s", functionName, buffer);
free(buffer);
}
void releaseContext(AVCodecContext *context) {
if (!context) {
return;
}
2023-04-03 15:15:52 +02:00
SwrContext *swrContext;
if ((swrContext = (SwrContext *)context->opaque)) {
swr_free(&swrContext);
2018-07-30 04:07:02 +02:00
context->opaque = NULL;
}
avcodec_free_context(&context);
}