Skip to content

Commit 683c4e6

Browse files
committed
update to 9.5.8
1 parent 7e6d40a commit 683c4e6

25 files changed

+408
-345
lines changed

‎TMessagesProj/jni/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ target_include_directories(breakpad PUBLIC
435435
#voip
436436
include(${CMAKE_HOME_DIRECTORY}/voip/CMakeLists.txt)
437437

438-
set(NATIVE_LIB "tmessages.43")
438+
set(NATIVE_LIB "tmessages.44")
439439

440440
#tmessages
441441
add_library(${NATIVE_LIB} SHARED

‎TMessagesProj/jni/exoplayer/ffmpeg_jni.cc

+91-78
Original file line numberDiff line numberDiff line change
@@ -13,47 +13,48 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
#include <android/log.h>
1617
#include <jni.h>
1718
#include <stdlib.h>
18-
#include <android/log.h>
1919

2020
extern "C" {
2121
#ifdef __cplusplus
22+
#define __STDC_CONSTANT_MACROS
2223
#ifdef _STDINT_H
2324
#undef _STDINT_H
2425
#endif
2526
#include <stdint.h>
2627
#endif
2728
#include <libavcodec/avcodec.h>
28-
#include <libavresample/avresample.h>
2929
#include <libavutil/channel_layout.h>
3030
#include <libavutil/error.h>
3131
#include <libavutil/opt.h>
32+
#include <libswresample/swresample.h>
3233
}
3334

3435
#define LOG_TAG "ffmpeg_jni"
35-
#define LOGE(...) ((void)__android_log_print(ANDROID_LOG_ERROR, LOG_TAG, \
36-
__VA_ARGS__))
37-
38-
#define DECODER_FUNC(RETURN_TYPE, NAME, ...) \
39-
extern "C" { \
40-
JNIEXPORT RETURN_TYPE \
41-
Java_com_google_android_exoplayer2_ext_ffmpeg_FfmpegDecoder_ ## NAME \
42-
(JNIEnv* env, jobject thiz, ##__VA_ARGS__);\
43-
} \
44-
JNIEXPORT RETURN_TYPE \
45-
Java_com_google_android_exoplayer2_ext_ffmpeg_FfmpegDecoder_ ## NAME \
46-
(JNIEnv* env, jobject thiz, ##__VA_ARGS__)\
47-
48-
#define LIBRARY_FUNC(RETURN_TYPE, NAME, ...) \
49-
extern "C" { \
50-
JNIEXPORT RETURN_TYPE \
51-
Java_com_google_android_exoplayer2_ext_ffmpeg_FfmpegLibrary_ ## NAME \
52-
(JNIEnv* env, jobject thiz, ##__VA_ARGS__);\
53-
} \
54-
JNIEXPORT RETURN_TYPE \
55-
Java_com_google_android_exoplayer2_ext_ffmpeg_FfmpegLibrary_ ## NAME \
56-
(JNIEnv* env, jobject thiz, ##__VA_ARGS__)\
36+
#define LOGE(...) \
37+
((void)__android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__))
38+
39+
#define LIBRARY_FUNC(RETURN_TYPE, NAME, ...) \
40+
extern "C" { \
41+
JNIEXPORT RETURN_TYPE \
42+
Java_com_google_android_exoplayer2_ext_ffmpeg_FfmpegLibrary_##NAME( \
43+
JNIEnv *env, jobject thiz, ##__VA_ARGS__); \
44+
} \
45+
JNIEXPORT RETURN_TYPE \
46+
Java_com_google_android_exoplayer2_ext_ffmpeg_FfmpegLibrary_##NAME( \
47+
JNIEnv *env, jobject thiz, ##__VA_ARGS__)
48+
49+
#define AUDIO_DECODER_FUNC(RETURN_TYPE, NAME, ...) \
50+
extern "C" { \
51+
JNIEXPORT RETURN_TYPE \
52+
Java_com_google_android_exoplayer2_ext_ffmpeg_FfmpegAudioDecoder_##NAME( \
53+
JNIEnv *env, jobject thiz, ##__VA_ARGS__); \
54+
} \
55+
JNIEXPORT RETURN_TYPE \
56+
Java_com_google_android_exoplayer2_ext_ffmpeg_FfmpegAudioDecoder_##NAME( \
57+
JNIEnv *env, jobject thiz, ##__VA_ARGS__)
5758

5859
#define ERROR_STRING_BUFFER_LENGTH 256
5960

@@ -62,14 +63,13 @@ static const AVSampleFormat OUTPUT_FORMAT_PCM_16BIT = AV_SAMPLE_FMT_S16;
6263
// Output format corresponding to AudioFormat.ENCODING_PCM_FLOAT.
6364
static const AVSampleFormat OUTPUT_FORMAT_PCM_FLOAT = AV_SAMPLE_FMT_FLT;
6465

65-
// Error codes matching FfmpegDecoder.java.
66-
static const int DECODER_ERROR_INVALID_DATA = -1;
67-
static const int DECODER_ERROR_OTHER = -2;
66+
static const int AUDIO_DECODER_ERROR_INVALID_DATA = -1;
67+
static const int AUDIO_DECODER_ERROR_OTHER = -2;
6868

6969
/**
7070
* Returns the AVCodec with the specified name, or NULL if it is not available.
7171
*/
72-
AVCodec *getCodecByName(JNIEnv* env, jstring codecName);
72+
AVCodec *getCodecByName(JNIEnv *env, jstring codecName);
7373

7474
/**
7575
* Allocates and opens a new AVCodecContext for the specified codec, passing the
@@ -82,11 +82,17 @@ AVCodecContext *createContext(JNIEnv *env, AVCodec *codec, jbyteArray extraData,
8282

8383
/**
8484
* Decodes the packet into the output buffer, returning the number of bytes
85-
* written, or a negative DECODER_ERROR constant value in the case of an error.
85+
* written, or a negative AUDIO_DECODER_ERROR constant value in the case of an
86+
* error.
8687
*/
8788
int decodePacket(AVCodecContext *context, AVPacket *packet,
8889
uint8_t *outputBuffer, int outputSize);
8990

91+
/**
92+
* Transforms ffmpeg AVERROR into a negative AUDIO_DECODER_ERROR constant value.
93+
*/
94+
int transformError(int errorNumber);
95+
9096
/**
9197
* Outputs a log message describing the avcodec error number.
9298
*/
@@ -101,12 +107,17 @@ LIBRARY_FUNC(jstring, ffmpegGetVersion) {
101107
return env->NewStringUTF(LIBAVCODEC_IDENT);
102108
}
103109

110+
LIBRARY_FUNC(jint, ffmpegGetInputBufferPaddingSize) {
111+
return (jint)AV_INPUT_BUFFER_PADDING_SIZE;
112+
}
113+
104114
LIBRARY_FUNC(jboolean, ffmpegHasDecoder, jstring codecName) {
105115
return getCodecByName(env, codecName) != NULL;
106116
}
107117

108-
DECODER_FUNC(jlong, ffmpegInitialize, jstring codecName, jbyteArray extraData,
109-
jboolean outputFloat, jint rawSampleRate, jint rawChannelCount) {
118+
AUDIO_DECODER_FUNC(jlong, ffmpegInitialize, jstring codecName,
119+
jbyteArray extraData, jboolean outputFloat,
120+
jint rawSampleRate, jint rawChannelCount) {
110121
AVCodec *codec = getCodecByName(env, codecName);
111122
if (!codec) {
112123
LOGE("Codec not found.");
@@ -116,8 +127,8 @@ DECODER_FUNC(jlong, ffmpegInitialize, jstring codecName, jbyteArray extraData,
116127
rawChannelCount);
117128
}
118129

119-
DECODER_FUNC(jint, ffmpegDecode, jlong context, jobject inputData,
120-
jint inputSize, jobject outputData, jint outputSize) {
130+
AUDIO_DECODER_FUNC(jint, ffmpegDecode, jlong context, jobject inputData,
131+
jint inputSize, jobject outputData, jint outputSize) {
121132
if (!context) {
122133
LOGE("Context must be non-NULL.");
123134
return -1;
@@ -134,34 +145,34 @@ DECODER_FUNC(jint, ffmpegDecode, jlong context, jobject inputData,
134145
LOGE("Invalid output buffer length: %d", outputSize);
135146
return -1;
136147
}
137-
uint8_t *inputBuffer = (uint8_t *) env->GetDirectBufferAddress(inputData);
138-
uint8_t *outputBuffer = (uint8_t *) env->GetDirectBufferAddress(outputData);
148+
uint8_t *inputBuffer = (uint8_t *)env->GetDirectBufferAddress(inputData);
149+
uint8_t *outputBuffer = (uint8_t *)env->GetDirectBufferAddress(outputData);
139150
AVPacket packet;
140151
av_init_packet(&packet);
141152
packet.data = inputBuffer;
142153
packet.size = inputSize;
143-
return decodePacket((AVCodecContext *) context, &packet, outputBuffer,
154+
return decodePacket((AVCodecContext *)context, &packet, outputBuffer,
144155
outputSize);
145156
}
146157

147-
DECODER_FUNC(jint, ffmpegGetChannelCount, jlong context) {
158+
AUDIO_DECODER_FUNC(jint, ffmpegGetChannelCount, jlong context) {
148159
if (!context) {
149160
LOGE("Context must be non-NULL.");
150161
return -1;
151162
}
152-
return ((AVCodecContext *) context)->channels;
163+
return ((AVCodecContext *)context)->channels;
153164
}
154165

155-
DECODER_FUNC(jint, ffmpegGetSampleRate, jlong context) {
166+
AUDIO_DECODER_FUNC(jint, ffmpegGetSampleRate, jlong context) {
156167
if (!context) {
157168
LOGE("Context must be non-NULL.");
158169
return -1;
159170
}
160-
return ((AVCodecContext *) context)->sample_rate;
171+
return ((AVCodecContext *)context)->sample_rate;
161172
}
162173

163-
DECODER_FUNC(jlong, ffmpegReset, jlong jContext, jbyteArray extraData) {
164-
AVCodecContext *context = (AVCodecContext *) jContext;
174+
AUDIO_DECODER_FUNC(jlong, ffmpegReset, jlong jContext, jbyteArray extraData) {
175+
AVCodecContext *context = (AVCodecContext *)jContext;
165176
if (!context) {
166177
LOGE("Tried to reset without a context.");
167178
return 0L;
@@ -178,23 +189,23 @@ DECODER_FUNC(jlong, ffmpegReset, jlong jContext, jbyteArray extraData) {
178189
return 0L;
179190
}
180191
jboolean outputFloat =
181-
(jboolean)(context->request_sample_fmt == OUTPUT_FORMAT_PCM_FLOAT);
192+
(jboolean)(context->request_sample_fmt == OUTPUT_FORMAT_PCM_FLOAT);
182193
return (jlong)createContext(env, codec, extraData, outputFloat,
183-
/* rawSampleRate= */ -1,
184-
/* rawChannelCount= */ -1);
194+
/* rawSampleRate= */ -1,
195+
/* rawChannelCount= */ -1);
185196
}
186197

187198
avcodec_flush_buffers(context);
188-
return (jlong) context;
199+
return (jlong)context;
189200
}
190201

191-
DECODER_FUNC(void, ffmpegRelease, jlong context) {
202+
AUDIO_DECODER_FUNC(void, ffmpegRelease, jlong context) {
192203
if (context) {
193-
releaseContext((AVCodecContext *) context);
204+
releaseContext((AVCodecContext *)context);
194205
}
195206
}
196207

197-
AVCodec *getCodecByName(JNIEnv* env, jstring codecName) {
208+
AVCodec *getCodecByName(JNIEnv *env, jstring codecName) {
198209
if (!codecName) {
199210
return NULL;
200211
}
@@ -213,18 +224,18 @@ AVCodecContext *createContext(JNIEnv *env, AVCodec *codec, jbyteArray extraData,
213224
return NULL;
214225
}
215226
context->request_sample_fmt =
216-
outputFloat ? OUTPUT_FORMAT_PCM_FLOAT : OUTPUT_FORMAT_PCM_16BIT;
227+
outputFloat ? OUTPUT_FORMAT_PCM_FLOAT : OUTPUT_FORMAT_PCM_16BIT;
217228
if (extraData) {
218229
jsize size = env->GetArrayLength(extraData);
219230
context->extradata_size = size;
220231
context->extradata =
221-
(uint8_t *) av_malloc(size + AV_INPUT_BUFFER_PADDING_SIZE);
232+
(uint8_t *)av_malloc(size + AV_INPUT_BUFFER_PADDING_SIZE);
222233
if (!context->extradata) {
223234
LOGE("Failed to allocate extradata.");
224235
releaseContext(context);
225236
return NULL;
226237
}
227-
env->GetByteArrayRegion(extraData, 0, size, (jbyte *) context->extradata);
238+
env->GetByteArrayRegion(extraData, 0, size, (jbyte *)context->extradata);
228239
}
229240
if (context->codec_id == AV_CODEC_ID_PCM_MULAW ||
230241
context->codec_id == AV_CODEC_ID_PCM_ALAW) {
@@ -249,8 +260,7 @@ int decodePacket(AVCodecContext *context, AVPacket *packet,
249260
result = avcodec_send_packet(context, packet);
250261
if (result) {
251262
logError("avcodec_send_packet", result);
252-
return result == AVERROR_INVALIDDATA ? DECODER_ERROR_INVALID_DATA
253-
: DECODER_ERROR_OTHER;
263+
return transformError(result);
254264
}
255265

256266
// Dequeue output data until it runs out.
@@ -259,7 +269,7 @@ int decodePacket(AVCodecContext *context, AVPacket *packet,
259269
AVFrame *frame = av_frame_alloc();
260270
if (!frame) {
261271
LOGE("Failed to allocate output frame.");
262-
return -1;
272+
return AUDIO_DECODER_ERROR_INVALID_DATA;
263273
}
264274
result = avcodec_receive_frame(context, frame);
265275
if (result) {
@@ -268,7 +278,7 @@ int decodePacket(AVCodecContext *context, AVPacket *packet,
268278
break;
269279
}
270280
logError("avcodec_receive_frame", result);
271-
return result;
281+
return transformError(result);
272282
}
273283

274284
// Resample output.
@@ -279,59 +289,63 @@ int decodePacket(AVCodecContext *context, AVPacket *packet,
279289
int sampleCount = frame->nb_samples;
280290
int dataSize = av_samples_get_buffer_size(NULL, channelCount, sampleCount,
281291
sampleFormat, 1);
282-
AVAudioResampleContext *resampleContext;
292+
SwrContext *resampleContext;
283293
if (context->opaque) {
284-
resampleContext = (AVAudioResampleContext *) context->opaque;
294+
resampleContext = (SwrContext *)context->opaque;
285295
} else {
286-
resampleContext = avresample_alloc_context();
287-
av_opt_set_int(resampleContext, "in_channel_layout", channelLayout, 0);
296+
resampleContext = swr_alloc();
297+
av_opt_set_int(resampleContext, "in_channel_layout", channelLayout, 0);
288298
av_opt_set_int(resampleContext, "out_channel_layout", channelLayout, 0);
289299
av_opt_set_int(resampleContext, "in_sample_rate", sampleRate, 0);
290300
av_opt_set_int(resampleContext, "out_sample_rate", sampleRate, 0);
291301
av_opt_set_int(resampleContext, "in_sample_fmt", sampleFormat, 0);
292302
// The output format is always the requested format.
293303
av_opt_set_int(resampleContext, "out_sample_fmt",
294-
context->request_sample_fmt, 0);
295-
result = avresample_open(resampleContext);
304+
context->request_sample_fmt, 0);
305+
result = swr_init(resampleContext);
296306
if (result < 0) {
297-
logError("avresample_open", result);
307+
logError("swr_init", result);
298308
av_frame_free(&frame);
299-
return -1;
309+
return transformError(result);
300310
}
301311
context->opaque = resampleContext;
302312
}
303313
int inSampleSize = av_get_bytes_per_sample(sampleFormat);
304314
int outSampleSize = av_get_bytes_per_sample(context->request_sample_fmt);
305-
int outSamples = avresample_get_out_samples(resampleContext, sampleCount);
315+
int outSamples = swr_get_out_samples(resampleContext, sampleCount);
306316
int bufferOutSize = outSampleSize * channelCount * outSamples;
307317
if (outSize + bufferOutSize > outputSize) {
308318
LOGE("Output buffer size (%d) too small for output data (%d).",
309319
outputSize, outSize + bufferOutSize);
310320
av_frame_free(&frame);
311-
return -1;
321+
return AUDIO_DECODER_ERROR_INVALID_DATA;
312322
}
313-
result = avresample_convert(resampleContext, &outputBuffer, bufferOutSize,
314-
outSamples, frame->data, frame->linesize[0],
315-
sampleCount);
323+
result = swr_convert(resampleContext, &outputBuffer, bufferOutSize,
324+
(const uint8_t **)frame->data, frame->nb_samples);
316325
av_frame_free(&frame);
317326
if (result < 0) {
318-
logError("avresample_convert", result);
319-
return result;
327+
logError("swr_convert", result);
328+
return AUDIO_DECODER_ERROR_INVALID_DATA;
320329
}
321-
int available = avresample_available(resampleContext);
330+
int available = swr_get_out_samples(resampleContext, 0);
322331
if (available != 0) {
323332
LOGE("Expected no samples remaining after resampling, but found %d.",
324333
available);
325-
return -1;
334+
return AUDIO_DECODER_ERROR_INVALID_DATA;
326335
}
327336
outputBuffer += bufferOutSize;
328337
outSize += bufferOutSize;
329338
}
330339
return outSize;
331340
}
332341

342+
int transformError(int errorNumber) {
343+
return errorNumber == AVERROR_INVALIDDATA ? AUDIO_DECODER_ERROR_INVALID_DATA
344+
: AUDIO_DECODER_ERROR_OTHER;
345+
}
346+
333347
void logError(const char *functionName, int errorNumber) {
334-
char *buffer = (char *) malloc(ERROR_STRING_BUFFER_LENGTH * sizeof(char));
348+
char *buffer = (char *)malloc(ERROR_STRING_BUFFER_LENGTH * sizeof(char));
335349
av_strerror(errorNumber, buffer, ERROR_STRING_BUFFER_LENGTH);
336350
LOGE("Error in %s: %s", functionName, buffer);
337351
free(buffer);
@@ -341,11 +355,10 @@ void releaseContext(AVCodecContext *context) {
341355
if (!context) {
342356
return;
343357
}
344-
AVAudioResampleContext *resampleContext;
345-
if ((resampleContext = (AVAudioResampleContext *) context->opaque)) {
346-
avresample_free(&resampleContext);
358+
SwrContext *swrContext;
359+
if ((swrContext = (SwrContext *)context->opaque)) {
360+
swr_free(&swrContext);
347361
context->opaque = NULL;
348362
}
349363
avcodec_free_context(&context);
350364
}
351-

0 commit comments

Comments
 (0)