| 6 |
| * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 |
| 17 | #include <android/log.h> |
| 18 | #include <android/native_window.h> |
| 19 | #include <android/native_window_jni.h> |
| 20 | #include <jni.h> |
| 21 | #include <stdlib.h> |
| 22 |
| 23 | #include <cstring> |
| 24 | #include <new> |
| 25 |
| 26 | #include <libyuv.h> |
| 27 | #include <libyuv/scale.h> |
| 28 |
| 29 | extern "C" { |
| 30 | #ifdef __cplusplus |
| 31 | #define __STDC_CONSTANT_MACROS |
| 32 | #ifdef _STDINT_H |
| 33 | #undef _STDINT_H |
| 34 | #endif |
| 35 | #include <stdint.h> |
| 36 | #endif |
| 37 |
| 38 | #include <libavcodec/avcodec.h> |
| 39 | #include <libavutil/channel_layout.h> |
| 40 | #include <libavutil/error.h> |
| 41 | #include <libavutil/mem.h> |
| 42 | #include <libavutil/opt.h> |
| 43 | #include <libswresample/swresample.h> |
| 44 | #include <libswscale/swscale.h> |
| 45 | } |
| 46 |
| 47 | #define LOG_TAG "ffmpeg_jni" |
| 48 | #define LOGE(...) \ |
| 49 | ((void)__android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)) |
| 50 | #define LOGW(...) \ |
| 51 | ((void)__android_log_print(ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__)) |
| 52 | #define LOGD(...) \ |
| 53 | ((void)__android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)) |
| 54 |
| 55 | #define LIBRARY_FUNC(RETURN_TYPE, NAME, ...) \ |
| 56 | extern "C" { \ |
| 57 | JNIEXPORT RETURN_TYPE \ |
| 58 | Java_androidx_media3_decoder_ffmpeg_FfmpegLibrary_##NAME(JNIEnv* env, \ |
| 59 | jobject thiz, \ |
| 60 | ##__VA_ARGS__); \ |
| 61 | } \ |
| 62 | JNIEXPORT RETURN_TYPE \ |
| 63 | Java_androidx_media3_decoder_ffmpeg_FfmpegLibrary_##NAME( \ |
| 64 | JNIEnv* env, jobject thiz, ##__VA_ARGS__) |
| 65 |
| 66 | #define AUDIO_DECODER_FUNC(RETURN_TYPE, NAME, ...) \ |
| 67 | extern "C" { \ |
| 68 | JNIEXPORT RETURN_TYPE \ |
| 69 | Java_androidx_media3_decoder_ffmpeg_FfmpegAudioDecoder_##NAME( \ |
| 70 | JNIEnv* env, jobject thiz, ##__VA_ARGS__); \ |
| 71 | } \ |
| 72 | JNIEXPORT RETURN_TYPE \ |
| 73 | Java_androidx_media3_decoder_ffmpeg_FfmpegAudioDecoder_##NAME( \ |
| 74 | JNIEnv* env, jobject thiz, ##__VA_ARGS__) |
| 75 |
| 76 | #define VIDEO_DECODER_FUNC(RETURN_TYPE, NAME, ...) \ |
| 77 | extern "C" { \ |
| 78 | JNIEXPORT RETURN_TYPE \ |
| 79 | Java_androidx_media3_decoder_ffmpeg_ExperimentalFfmpegVideoDecoder_##NAME( \ |
| 80 | JNIEnv* env, jobject thiz, ##__VA_ARGS__); \ |
| 81 | } \ |
| 82 | JNIEXPORT RETURN_TYPE \ |
| 83 | Java_androidx_media3_decoder_ffmpeg_ExperimentalFfmpegVideoDecoder_##NAME( \ |
| 84 | JNIEnv* env, jobject thiz, ##__VA_ARGS__) |
| 85 |
| 86 | #define ERROR_STRING_BUFFER_LENGTH 256 |
| 87 |
| 88 | // Output format corresponding to AudioFormat.ENCODING_PCM_16BIT. |
| 89 | static const AVSampleFormat OUTPUT_FORMAT_PCM_16BIT = AV_SAMPLE_FMT_S16; |
| 90 | // Output format corresponding to AudioFormat.ENCODING_PCM_FLOAT. |
| 91 | static const AVSampleFormat OUTPUT_FORMAT_PCM_FLOAT = AV_SAMPLE_FMT_FLT; |
| 92 |
| 93 | // LINT.IfChange |
| 94 | static const int AUDIO_DECODER_ERROR_INVALID_DATA = -1; |
| 95 | static const int AUDIO_DECODER_ERROR_OTHER = -2; |
| 96 | // LINT.ThenChange(../java/androidx/media3/decoder/ffmpeg/FfmpegAudioDecoder.java) |
| 97 |
| 98 | // LINT.IfChange |
| 99 | static const int VIDEO_DECODER_ERROR_SURFACE = -4; |
| 100 | static const int VIDEO_DECODER_SUCCESS = 0; |
| 101 | static const int VIDEO_DECODER_ERROR_INVALID_DATA = -1; |
| 102 | static const int VIDEO_DECODER_ERROR_OTHER = -2; |
| 103 | static const int VIDEO_DECODER_ERROR_READ_FRAME = -3; |
| 104 | // LINT.ThenChange(../java/androidx/media3/decoder/ffmpeg/ExperimentalFfmpegVideoDecoder.java) |
| 105 |
| 106 | static jmethodID growOutputBufferMethod; |
| 107 |
| 108 | /** |
| 109 | * Returns the AVCodec with the specified name, or NULL if it is not available. |
| 110 | */ |
| 111 | const AVCodec* getCodecByName(JNIEnv* env, jstring codecName); |
| 112 |
| 113 | /** |
| 114 | * Allocates and opens a new AVCodecContext for the specified codec, passing the |
| 115 | * provided extraData as initialization data for the decoder if it is non-NULL. |
| 116 | * Returns the created context. |
| 117 | */ |
| 118 | AVCodecContext* createContext(JNIEnv* env, |
| 119 | const AVCodec* codec, |
| 120 | jbyteArray extraData, |
| 121 | jboolean outputFloat, |
| 122 | jint rawSampleRate, |
| 123 | jint rawChannelCount); |
| 124 |
| 125 | struct GrowOutputBufferCallback { |
| 126 | uint8_t* operator()(int requiredSize) const; |
| 127 | JNIEnv* env; |
| 128 | jobject thiz; |
| 129 | jobject decoderOutputBuffer; |
| 130 | }; |
| 131 |
| 132 | /** |
| 133 | * Decodes the packet into the output buffer, returning the number of bytes |
| 134 | * written, or a negative AUDIO_DECODER_ERROR constant value in the case of an |
| 135 | * error. |
| 136 | */ |
| 137 | int decodePacket(AVCodecContext* context, |
| 138 | AVPacket* packet, |
| 139 | uint8_t* outputBuffer, |
| 140 | int outputSize, |
| 141 | GrowOutputBufferCallback growBuffer); |
| 142 |
| 143 | /** |
| 144 | * Transforms ffmpeg AVERROR into a negative AUDIO_DECODER_ERROR constant value. |
| 145 | */ |
| 146 | int transformError(int errorNumber); |
| 147 |
| 148 | /** |
| 149 | * Outputs a log message describing the avcodec error number. |
| 150 | */ |
| 151 | void logError(const char* functionName, int errorNumber); |
| 152 |
| 153 | /** |
| 154 | * Releases the specified context. |
| 155 | */ |
| 156 | void releaseContext(AVCodecContext* context); |
| 157 |
| 158 | jstring ffmpegGetVersion(JNIEnv* env, jobject thiz) { |
| 159 | return env->NewStringUTF(LIBAVCODEC_IDENT); |
| 160 | } |
| 161 |
| 162 | jint ffmpegGetInputBufferPaddingSize(JNIEnv* env, jobject thiz) { |
| 163 | return static_cast<jint>(AV_INPUT_BUFFER_PADDING_SIZE); |
| 164 | } |
| 165 |
| 166 | jboolean ffmpegHasDecoder(JNIEnv* env, jobject thiz, jstring codecName) { |
| 167 | return getCodecByName(env, codecName) != NULL; |
| 168 | } |
| 169 |
| 170 | jstring ffmpegGetAv1DecoderName(JNIEnv* env, jobject thiz) { |
| 171 | if (avcodec_find_decoder_by_name("libdav1d")) { |
| 172 | return env->NewStringUTF("libdav1d"); |
| 173 | } |
| 174 | if (avcodec_find_decoder_by_name("libaom-av1")) { |
| 175 | return env->NewStringUTF("libaom-av1"); |
| 176 | } |
| 177 | return NULL; |
| 178 | } |
| 179 |
| 180 | jlong ffmpegInitialize(JNIEnv* env, |
| 181 | jobject thiz, |
| 182 | jstring codecName, |
| 183 | jbyteArray extraData, |
| 184 | jboolean outputFloat, |
| 185 | jint rawSampleRate, |
| 186 | jint rawChannelCount) { |
| 187 | const AVCodec* codec = getCodecByName(env, codecName); |
| 188 | if (!codec) { |
| 189 | LOGE("Codec not found."); |
| 190 | return 0L; |
| 191 | } |
| 192 | return reinterpret_cast<jlong>( |
| 193 | createContext(env, codec, extraData, outputFloat, rawSampleRate, |
| 194 | rawChannelCount)); |
| 195 | } |
| 196 |
| 197 | jint ffmpegDecode(JNIEnv* env, |
| 198 | jobject thiz, |
| 199 | jlong context, |
| 200 | jobject inputData, |
| 201 | jint inputSize, |
| 202 | jobject decoderOutputBuffer, |
| 203 | jobject outputData, |
| 204 | jint outputSize) { |
| 205 | if (!context) { |
| 206 | LOGE("Context must be non-NULL."); |
| 207 | return -1; |
| 208 | } |
| 209 | if (!inputData || !decoderOutputBuffer || !outputData) { |
| 210 | LOGE("Input and output buffers must be non-NULL."); |
| 211 | return -1; |
| 212 | } |
| 213 | if (inputSize < 0) { |
| 214 | LOGE("Invalid input buffer size: %d.", inputSize); |
| 215 | return -1; |
| 216 | } |
| 217 | if (outputSize < 0) { |
| 218 | LOGE("Invalid output buffer length: %d", outputSize); |
| 219 | return -1; |
| 220 | } |
| 221 |
| 222 | uint8_t* inputBuffer = |
| 223 | static_cast<uint8_t*>(env->GetDirectBufferAddress(inputData)); |
| 224 | uint8_t* outputBuffer = |
| 225 | static_cast<uint8_t*>(env->GetDirectBufferAddress(outputData)); |
| 226 |
| 227 | AVPacket* packet = av_packet_alloc(); |
| 228 | if (!packet) { |
| 229 | LOGE("Failed to allocate packet."); |
| 230 | return -1; |
| 231 | } |
| 232 |
| 233 | packet->data = inputBuffer; |
| 234 | packet->size = inputSize; |
| 235 |
| 236 | const int ret = |
| 237 | decodePacket(reinterpret_cast<AVCodecContext*>(context), packet, |
| 238 | outputBuffer, outputSize, |
| 239 | GrowOutputBufferCallback{env, thiz, decoderOutputBuffer}); |
| 240 | av_packet_free(&packet); |
| 241 | return ret; |
| 242 | } |
| 243 |
| 244 | uint8_t* GrowOutputBufferCallback::operator()(int requiredSize) const { |
| 245 | jobject newOutputData = env->CallObjectMethod( |
| 246 | thiz, growOutputBufferMethod, decoderOutputBuffer, requiredSize); |
| 247 | if (env->ExceptionCheck()) { |
| 248 | LOGE("growOutputBuffer() failed"); |
| 249 | env->ExceptionDescribe(); |
| 250 | return nullptr; |
| 251 | } |
| 252 | return static_cast<uint8_t*>(env->GetDirectBufferAddress(newOutputData)); |
| 253 | } |
| 254 |
| 255 | jint ffmpegGetChannelCount(JNIEnv* env, jobject thiz, jlong context) { |
| 256 | if (!context) { |
| 257 | LOGE("Context must be non-NULL."); |
| 258 | return -1; |
| 259 | } |
| 260 | return reinterpret_cast<AVCodecContext*>(context)->ch_layout.nb_channels; |
| 261 | } |
| 262 |
| 263 | jint ffmpegGetSampleRate(JNIEnv* env, jobject thiz, jlong context) { |
| 264 | if (!context) { |
| 265 | LOGE("Context must be non-NULL."); |
| 266 | return -1; |
| 267 | } |
| 268 | return reinterpret_cast<AVCodecContext*>(context)->sample_rate; |
| 269 | } |
| 270 |
| 271 | jlong ffmpegReset(JNIEnv* env, |
| 272 | jobject thiz, |
| 273 | jlong jContext, |
| 274 | jbyteArray extraData) { |
| 275 | AVCodecContext* context = reinterpret_cast<AVCodecContext*>(jContext); |
| 276 | if (!context) { |
| 277 | LOGE("Tried to reset without a context."); |
| 278 | return 0L; |
| 279 | } |
| 280 |
| 281 | AVCodecID codecId = context->codec_id; |
| 282 | if (codecId == AV_CODEC_ID_TRUEHD) { |
| 283 | jboolean outputFloat = |
| 284 | static_cast<jboolean>(context->request_sample_fmt == OUTPUT_FORMAT_PCM_FLOAT); |
| 285 |
| 286 | // Release and recreate the context if the codec is TrueHD. |
| 287 | // TODO: Figure out why flushing doesn't work for this codec. |
| 288 | releaseContext(context); |
| 289 |
| 290 | const AVCodec* codec = avcodec_find_decoder(codecId); |
| 291 | if (!codec) { |
| 292 | LOGE("Unexpected error finding codec %d.", codecId); |
| 293 | return 0L; |
| 294 | } |
| 295 |
| 296 | return reinterpret_cast<jlong>( |
| 297 | createContext(env, codec, extraData, outputFloat, |
| 298 | /* rawSampleRate= */ -1, |
| 299 | /* rawChannelCount= */ -1)); |
| 300 | } |
| 301 |
| 302 | avcodec_flush_buffers(context); |
| 303 | return reinterpret_cast<jlong>(context); |
| 304 | } |
| 305 |
| 306 | void ffmpegRelease(JNIEnv* env, jobject thiz, jlong context) { |
| 307 | if (context) { |
| 308 | releaseContext(reinterpret_cast<AVCodecContext*>(context)); |
| 309 | } |
| 310 | } |
| 311 |
| 312 | const AVCodec* getCodecByName(JNIEnv* env, jstring codecName) { |
| 313 | if (!codecName) { |
| 314 | return NULL; |
| 315 | } |
| 316 |
| 317 | const char* codecNameChars = env->GetStringUTFChars(codecName, NULL); |
| 318 | if (!codecNameChars) { |
| 319 | return NULL; |
| 320 | } |
| 321 |
| 322 | const AVCodec* codec = avcodec_find_decoder_by_name(codecNameChars); |
| 323 | env->ReleaseStringUTFChars(codecName, codecNameChars); |
| 324 | return codec; |
| 325 | } |
| 326 |
| 327 | AVCodecContext* createContext(JNIEnv* env, |
| 328 | const AVCodec* codec, |
| 329 | jbyteArray extraData, |
| 330 | jboolean outputFloat, |
| 331 | jint rawSampleRate, |
| 332 | jint rawChannelCount) { |
| 333 | AVCodecContext* context = avcodec_alloc_context3(codec); |
| 334 | if (!context) { |
| 335 | LOGE("Failed to allocate context."); |
| 336 | return NULL; |
| 337 | } |
| 338 |
| 339 | context->request_sample_fmt = |
| 340 | outputFloat ? OUTPUT_FORMAT_PCM_FLOAT : OUTPUT_FORMAT_PCM_16BIT; |
| 341 |
| 342 | if (extraData) { |
| 343 | const jsize size = env->GetArrayLength(extraData); |
| 344 | context->extradata_size = size; |
| 345 |
| 346 | // FFmpeg requires AV_INPUT_BUFFER_PADDING_SIZE zero bytes after extradata. |
| 347 | context->extradata = static_cast<uint8_t*>( |
| 348 | av_mallocz(static_cast<size_t>(size) + AV_INPUT_BUFFER_PADDING_SIZE)); |
| 349 | if (!context->extradata) { |
| 350 | LOGE("Failed to allocate extradata."); |
| 351 | releaseContext(context); |
| 352 | return NULL; |
| 353 | } |
| 354 |
| 355 | env->GetByteArrayRegion( |
| 356 | extraData, 0, size, reinterpret_cast<jbyte*>(context->extradata)); |
| 357 | if (env->ExceptionCheck()) { |
| 358 | releaseContext(context); |
| 359 | return NULL; |
| 360 | } |
| 361 | } |
| 362 |
| 363 | if (context->codec_id == AV_CODEC_ID_PCM_MULAW || |
| 364 | context->codec_id == AV_CODEC_ID_PCM_ALAW) { |
| 365 | context->sample_rate = rawSampleRate; |
| 366 | av_channel_layout_default(&context->ch_layout, rawChannelCount); |
| 367 | } |
| 368 |
| 369 | context->err_recognition = AV_EF_IGNORE_ERR; |
| 370 |
| 371 | const int result = avcodec_open2(context, codec, NULL); |
| 372 | if (result < 0) { |
| 373 | logError("avcodec_open2", result); |
| 374 | releaseContext(context); |
| 375 | return NULL; |
| 376 | } |
| 377 |
| 378 | return context; |
| 379 | } |
| 380 |
| 381 | int decodePacket(AVCodecContext* context, |
| 382 | AVPacket* packet, |
| 383 | uint8_t* outputBuffer, |
| 384 | int outputSize, |
| 385 | GrowOutputBufferCallback growBuffer) { |
| 386 | int result = avcodec_send_packet(context, packet); |
| 387 | if (result) { |
| 388 | logError("avcodec_send_packet", result); |
| 389 | return transformError(result); |
| 390 | } |
| 391 |
| 392 | int outSize = 0; |
| 393 | while (true) { |
| 394 | AVFrame* frame = av_frame_alloc(); |
| 395 | if (!frame) { |
| 396 | LOGE("Failed to allocate output frame."); |
| 397 | return AUDIO_DECODER_ERROR_INVALID_DATA; |
| 398 | } |
| 399 |
| 400 | result = avcodec_receive_frame(context, frame); |
| 401 | if (result) { |
| 402 | av_frame_free(&frame); |
| 403 | if (result == AVERROR(EAGAIN)) { |
| 404 | break; |
| 405 | } |
| 406 | logError("avcodec_receive_frame", result); |
| 407 | return transformError(result); |
| 408 | } |
| 409 |
| 410 | AVSampleFormat sampleFormat = context->sample_fmt; |
| 411 | int channelCount = context->ch_layout.nb_channels; |
| 412 | int sampleRate = context->sample_rate; |
| 413 | int sampleCount = frame->nb_samples; |
| 414 |
| 415 | SwrContext* resampleContext = |
| 416 | static_cast<SwrContext*>(context->opaque); |
| 417 | if (!resampleContext) { |
| 418 | result = |
| 419 | swr_alloc_set_opts2(&resampleContext, |
| 420 | &context->ch_layout, |
| 421 | context->request_sample_fmt, |
| 422 | sampleRate, |
| 423 | &context->ch_layout, |
| 424 | sampleFormat, |
| 425 | sampleRate, |
| 426 | 0, |
| 427 | NULL); |
| 428 | if (result < 0) { |
| 429 | logError("swr_alloc_set_opts2", result); |
| 430 | av_frame_free(&frame); |
| 431 | return transformError(result); |
| 432 | } |
| 433 |
| 434 | result = swr_init(resampleContext); |
| 435 | if (result < 0) { |
| 436 | logError("swr_init", result); |
| 437 | swr_free(&resampleContext); |
| 438 | av_frame_free(&frame); |
| 439 | return transformError(result); |
| 440 | } |
| 441 |
| 442 | context->opaque = resampleContext; |
| 443 | } |
| 444 |
| 445 | const int outSampleSize = |
| 446 | av_get_bytes_per_sample(context->request_sample_fmt); |
| 447 | const int outSamples = |
| 448 | swr_get_out_samples(resampleContext, sampleCount); |
| 449 | const int bufferOutSize = |
| 450 | outSampleSize * channelCount * outSamples; |
| 451 |
| 452 | if (outSize + bufferOutSize > outputSize) { |
| 453 | LOGD( |
| 454 | "Output buffer size (%d) too small for output data (%d), " |
| 455 | "reallocating buffer.", |
| 456 | outputSize, outSize + bufferOutSize); |
| 457 |
| 458 | outputSize = outSize + bufferOutSize; |
| 459 | outputBuffer = growBuffer(outputSize); |
| 460 | if (!outputBuffer) { |
| 461 | LOGE("Failed to reallocate output buffer."); |
| 462 | av_frame_free(&frame); |
| 463 | return AUDIO_DECODER_ERROR_OTHER; |
| 464 | } |
| 465 | } |
| 466 |
| 467 | result = swr_convert( |
| 468 | resampleContext, |
| 469 | &outputBuffer, |
| 470 | bufferOutSize, |
| 471 | const_cast<const uint8_t**>(frame->data), |
| 472 | frame->nb_samples); |
| 473 |
| 474 | av_frame_free(&frame); |
| 475 |
| 476 | if (result < 0) { |
| 477 | logError("swr_convert", result); |
| 478 | return AUDIO_DECODER_ERROR_INVALID_DATA; |
| 479 | } |
| 480 |
| 481 | const int available = swr_get_out_samples(resampleContext, 0); |
| 482 | if (available != 0) { |
| 483 | LOGE("Expected no samples remaining after resampling, but found %d.", |
| 484 | available); |
| 485 | return AUDIO_DECODER_ERROR_INVALID_DATA; |
| 486 | } |
| 487 |
| 488 | outputBuffer += bufferOutSize; |
| 489 | outSize += bufferOutSize; |
| 490 | } |
| 491 |
| 492 | return outSize; |
| 493 | } |
| 494 |
| 495 | int transformError(int errorNumber) { |
| 496 | return errorNumber == AVERROR_INVALIDDATA |
| 497 | ? AUDIO_DECODER_ERROR_INVALID_DATA |
| 498 | : AUDIO_DECODER_ERROR_OTHER; |
| 499 | } |
| 500 |
| 501 | void logError(const char* functionName, int errorNumber) { |
| 502 | char buffer[ERROR_STRING_BUFFER_LENGTH]; |
| 503 | av_strerror(errorNumber, buffer, ERROR_STRING_BUFFER_LENGTH); |
| 504 | LOGE("Error in %s: %s", functionName, buffer); |
| 505 | } |
| 506 |
| 507 | void releaseContext(AVCodecContext* context) { |
| 508 | if (!context) { |
| 509 | return; |
| 510 | } |
| 511 |
| 512 | SwrContext* swrContext = |
| 513 | static_cast<SwrContext*>(context->opaque); |
| 514 | if (swrContext) { |
| 515 | swr_free(&swrContext); |
| 516 | context->opaque = NULL; |
| 517 | } |
| 518 |
| 519 | avcodec_free_context(&context); |
| 520 | } |
| 521 |
| 522 | // ----------------------------------------------------------------------------- |
| 523 | // Video |
| 524 | // ----------------------------------------------------------------------------- |
| 525 |
| 526 | // Android YUV format. See: |
| 527 | // https://developer.android.com/reference/android/graphics/ImageFormat.html#YV12. |
| 528 | static const int kImageFormatYV12 = 0x32315659; |
| 529 |
| 530 | struct JniContext { |
| 531 | ~JniContext() { |
| 532 | if (native_window) { |
| 533 | ANativeWindow_release(native_window); |
| 534 | native_window = nullptr; |
| 535 | } |
| 536 | } |
| 537 |
| 538 | bool MaybeAcquireNativeWindow(JNIEnv* env, jobject new_surface) { |
| 539 | if (!new_surface) { |
| 540 | LOGE("Surface must be non-null."); |
| 541 | return false; |
| 542 | } |
| 543 |
| 544 | if (surface != nullptr && |
| 545 | native_window != nullptr && |
| 546 | env->IsSameObject(surface, new_surface)) { |
| 547 | return true; |
| 548 | } |
| 549 |
| 550 | if (native_window) { |
| 551 | ANativeWindow_release(native_window); |
| 552 | native_window = nullptr; |
| 553 | } |
| 554 |
| 555 | native_window_width = 0; |
| 556 | native_window_height = 0; |
| 557 | native_window_format = 0; |
| 558 |
| 559 | if (surface != nullptr) { |
| 560 | env->DeleteGlobalRef(surface); |
| 561 | surface = nullptr; |
| 562 | } |
| 563 |
| 564 | native_window = ANativeWindow_fromSurface(env, new_surface); |
| 565 | if (!native_window) { |
| 566 | LOGE("ANativeWindow_fromSurface failed."); |
| 567 | return false; |
| 568 | } |
| 569 |
| 570 | surface = env->NewGlobalRef(new_surface); |
| 571 | if (!surface) { |
| 572 | LOGE("NewGlobalRef(Surface) failed."); |
| 573 | ANativeWindow_release(native_window); |
| 574 | native_window = nullptr; |
| 575 | return false; |
| 576 | } |
| 577 |
| 578 | return true; |
| 579 | } |
| 580 |
| 581 | jfieldID data_field = nullptr; |
| 582 | jfieldID width_field = nullptr; |
| 583 | jfieldID height_field = nullptr; |
| 584 | jfieldID pts_field = nullptr; |
| 585 | jfieldID colorspace_field = nullptr; |
| 586 | jfieldID y_stride_field = nullptr; |
| 587 | jfieldID uv_stride_field = nullptr; |
| 588 | jmethodID init_for_yuv_frame_method = nullptr; |
| 589 |
| 590 | AVCodecContext* codecContext = nullptr; |
| 591 | SwsContext* swsContext = nullptr; |
| 592 |
| 593 | ANativeWindow* native_window = nullptr; |
| 594 | jobject surface = nullptr; |
| 595 |
| 596 | int rotate_degree = 0; |
| 597 | int native_window_width = 0; |
| 598 | int native_window_height = 0; |
| 599 | int native_window_format = 0; |
| 600 | bool use_rgba_fallback = false; |
| 601 | }; |
| 602 |
| 603 | constexpr int AlignTo16(int value) { |
| 604 | return (value + 15) & ~15; |
| 605 | } |
| 606 |
| 607 | /** |
| 608 | * Converts AVFrame color space to VideoDecoderOutputBuffer#COLORSPACE_*. |
| 609 | */ |
| 610 | constexpr int cvt_colorspace(AVColorSpace colorSpace) { |
| 611 | switch (colorSpace) { |
| 612 | case AVCOL_SPC_BT470BG: |
| 613 | case AVCOL_SPC_SMPTE170M: |
| 614 | case AVCOL_SPC_SMPTE240M: |
| 615 | return 1; // COLORSPACE_BT601 |
| 616 | case AVCOL_SPC_BT709: |
| 617 | return 2; // COLORSPACE_BT709 |
| 618 | case AVCOL_SPC_BT2020_NCL: |
| 619 | case AVCOL_SPC_BT2020_CL: |
| 620 | return 3; // COLORSPACE_BT2020 |
| 621 | default: |
| 622 | return 0; // COLORSPACE_UNKNOWN |
| 623 | } |
| 624 | } |
| 625 |
| 626 | const int* cvt_colorspace_coefficients(AVColorSpace colorSpace) { |
| 627 | switch (colorSpace) { |
| 628 | case AVCOL_SPC_BT709: |
| 629 | return sws_getCoefficients(SWS_CS_ITU709); |
| 630 |
| 631 | case AVCOL_SPC_BT2020_NCL: |
| 632 | case AVCOL_SPC_BT2020_CL: |
| 633 | return sws_getCoefficients(SWS_CS_BT2020); |
| 634 |
| 635 | case AVCOL_SPC_BT470BG: |
| 636 | case AVCOL_SPC_SMPTE170M: |
| 637 | case AVCOL_SPC_SMPTE240M: |
| 638 | default: |
| 639 | return sws_getCoefficients(SWS_CS_ITU601); |
| 640 | } |
| 641 | } |
| 642 |
| 643 | /** |
| 644 | * Converts a decoded frame to the requested pixel format and size. |
| 645 | */ |
| 646 | AVFrame* cvt_format(JniContext* jniContext, |
| 647 | AVFrame* src, |
| 648 | AVPixelFormat dst_format, |
| 649 | int dst_width, |
| 650 | int dst_height) { |
| 651 | if (!jniContext || !src || dst_width <= 0 || dst_height <= 0) { |
| 652 | LOGE("Invalid arguments for pixel format conversion."); |
| 653 | return nullptr; |
| 654 | } |
| 655 |
| 656 | const AVPixelFormat src_format = |
| 657 | static_cast<AVPixelFormat>(src->format); |
| 658 |
| 659 | SwsContext* swsContext = |
| 660 | sws_getCachedContext( |
| 661 | jniContext->swsContext, |
| 662 | src->width, |
| 663 | src->height, |
| 664 | src_format, |
| 665 | dst_width, |
| 666 | dst_height, |
| 667 | dst_format, |
| 668 | SWS_BILINEAR, |
| 669 | NULL, |
| 670 | NULL, |
| 671 | NULL); |
| 672 |
| 673 | if (!swsContext) { |
| 674 | LOGE("Failed to allocate swsContext."); |
| 675 | return nullptr; |
| 676 | } |
| 677 |
| 678 | jniContext->swsContext = swsContext; |
| 679 |
| 680 | // Preserve the decoded frame's matrix/range while changing bit depth/format. |
| 681 | const int src_range = |
| 682 | src->color_range == AVCOL_RANGE_JPEG ? 1 : 0; |
| 683 | const int* coefficients = |
| 684 | cvt_colorspace_coefficients(src->colorspace); |
| 685 |
| 686 | const int colorspace_result = |
| 687 | sws_setColorspaceDetails( |
| 688 | swsContext, |
| 689 | coefficients, |
| 690 | src_range, |
| 691 | coefficients, |
| 692 | src_range, |
| 693 | 0, |
| 694 | 1 << 16, |
| 695 | 1 << 16); |
| 696 |
| 697 | if (colorspace_result < 0) { |
| 698 | logError("sws_setColorspaceDetails", colorspace_result); |
| 699 | return nullptr; |
| 700 | } |
| 701 |
| 702 | AVFrame* dst = av_frame_alloc(); |
| 703 | if (!dst) { |
| 704 | LOGE("Failed to allocate converted AVFrame."); |
| 705 | return nullptr; |
| 706 | } |
| 707 |
| 708 | const int props_result = av_frame_copy_props(dst, src); |
| 709 | if (props_result < 0) { |
| 710 | logError("av_frame_copy_props", props_result); |
| 711 | av_frame_free(&dst); |
| 712 | return nullptr; |
| 713 | } |
| 714 |
| 715 | dst->width = dst_width; |
| 716 | dst->height = dst_height; |
| 717 | dst->format = dst_format; |
| 718 |
| 719 | const int alloc_result = av_frame_get_buffer(dst, 0); |
| 720 | if (alloc_result < 0) { |
| 721 | logError("av_frame_get_buffer", alloc_result); |
| 722 | av_frame_free(&dst); |
| 723 | return nullptr; |
| 724 | } |
| 725 |
| 726 | const int scale_result = |
| 727 | sws_scale( |
| 728 | swsContext, |
| 729 | src->data, |
| 730 | src->linesize, |
| 731 | 0, |
| 732 | src->height, |
| 733 | dst->data, |
| 734 | dst->linesize); |
| 735 |
| 736 | if (scale_result <= 0) { |
| 737 | if (scale_result < 0) { |
| 738 | logError("sws_scale", scale_result); |
| 739 | } else { |
| 740 | LOGE("sws_scale produced no output."); |
| 741 | } |
| 742 | av_frame_free(&dst); |
| 743 | return nullptr; |
| 744 | } |
| 745 |
| 746 | return dst; |
| 747 | } |
| 748 |
| 749 | libyuv::RotationMode cvt_rotate(int degree) { |
| 750 | switch (degree) { |
| 751 | case 90: |
| 752 | return libyuv::kRotate90; |
| 753 | case 180: |
| 754 | return libyuv::kRotate180; |
| 755 | case 270: |
| 756 | return libyuv::kRotate270; |
| 757 | default: |
| 758 | return libyuv::kRotate0; |
| 759 | } |
| 760 | } |
| 761 |
| 762 | JniContext* createVideoContext(JNIEnv* env, |
| 763 | const AVCodec* codec, |
| 764 | jbyteArray extraData, |
| 765 | jint threads, |
| 766 | jint degree, |
| 767 | jint width, |
| 768 | jint height) { |
| 769 | JniContext* jniContext = new (std::nothrow) JniContext(); |
| 770 | if (!jniContext) { |
| 771 | LOGE("Failed to allocate JniContext."); |
| 772 | return NULL; |
| 773 | } |
| 774 |
| 775 | AVCodecContext* codecContext = avcodec_alloc_context3(codec); |
| 776 | if (!codecContext) { |
| 777 | LOGE("Failed to allocate video codec context."); |
| 778 | delete jniContext; |
| 779 | return NULL; |
| 780 | } |
| 781 |
| 782 | jniContext->rotate_degree = degree; |
| 783 |
| 784 | if (extraData) { |
| 785 | const jsize size = env->GetArrayLength(extraData); |
| 786 | codecContext->extradata_size = size; |
| 787 |
| 788 | // FFmpeg requires zeroed padding after extradata. |
| 789 | codecContext->extradata = static_cast<uint8_t*>( |
| 790 | av_mallocz(static_cast<size_t>(size) + AV_INPUT_BUFFER_PADDING_SIZE)); |
| 791 | if (!codecContext->extradata) { |
| 792 | LOGE("Failed to allocate video extradata."); |
| 793 | releaseContext(codecContext); |
| 794 | delete jniContext; |
| 795 | return NULL; |
| 796 | } |
| 797 |
| 798 | env->GetByteArrayRegion( |
| 799 | extraData, 0, size, |
| 800 | reinterpret_cast<jbyte*>(codecContext->extradata)); |
| 801 | if (env->ExceptionCheck()) { |
| 802 | releaseContext(codecContext); |
| 803 | delete jniContext; |
| 804 | return NULL; |
| 805 | } |
| 806 | } |
| 807 |
| 808 | codecContext->skip_loop_filter = AVDISCARD_DEFAULT; |
| 809 | codecContext->skip_frame = AVDISCARD_DEFAULT; |
| 810 | codecContext->thread_count = threads; |
| 811 | codecContext->thread_type = FF_THREAD_FRAME; |
| 812 | codecContext->err_recognition = AV_EF_IGNORE_ERR; |
| 813 |
| 814 | AVDictionary* opts = NULL; |
| 815 |
| 816 | if (codec->id == AV_CODEC_ID_AV1 && |
| 817 | strcmp(codec->name, "libdav1d") == 0) { |
| 818 | av_dict_set(&opts, "max_frame_delay", "1", 0); |
| 819 | } |
| 820 |
| 821 | if (width > 0 && height > 0) { |
| 822 | codecContext->width = width; |
| 823 | codecContext->height = height; |
| 824 | } |
| 825 |
| 826 | const int result = avcodec_open2(codecContext, codec, &opts); |
| 827 | av_dict_free(&opts); |
| 828 |
| 829 | if (result < 0) { |
| 830 | logError("avcodec_open2", result); |
| 831 | releaseContext(codecContext); |
| 832 | delete jniContext; |
| 833 | return NULL; |
| 834 | } |
| 835 |
| 836 | jniContext->codecContext = codecContext; |
| 837 |
| 838 | const jclass outputBufferClass = |
| 839 | env->FindClass("androidx/media3/decoder/VideoDecoderOutputBuffer"); |
| 840 | if (!outputBufferClass) { |
| 841 | LOGE("Failed to find VideoDecoderOutputBuffer class."); |
| 842 | releaseContext(codecContext); |
| 843 | jniContext->codecContext = nullptr; |
| 844 | delete jniContext; |
| 845 | return NULL; |
| 846 | } |
| 847 |
| 848 | jniContext->data_field = |
| 849 | env->GetFieldID( |
| 850 | outputBufferClass, "data", "Ljava/nio/ByteBuffer;"); |
| 851 | jniContext->width_field = |
| 852 | env->GetFieldID(outputBufferClass, "width", "I"); |
| 853 | jniContext->height_field = |
| 854 | env->GetFieldID(outputBufferClass, "height", "I"); |
| 855 | jniContext->pts_field = |
| 856 | env->GetFieldID(outputBufferClass, "timeUs", "J"); |
| 857 | jniContext->colorspace_field = |
| 858 | env->GetFieldID(outputBufferClass, "colorspace", "I"); |
| 859 | jniContext->y_stride_field = |
| 860 | env->GetFieldID(outputBufferClass, "yStride", "I"); |
| 861 | jniContext->uv_stride_field = |
| 862 | env->GetFieldID(outputBufferClass, "uvStride", "I"); |
| 863 | jniContext->init_for_yuv_frame_method = |
| 864 | env->GetMethodID( |
| 865 | outputBufferClass, "initForYuvFrame", "(IIIII)Z"); |
| 866 |
| 867 | const bool fields_ok = |
| 868 | jniContext->data_field && |
| 869 | jniContext->width_field && |
| 870 | jniContext->height_field && |
| 871 | jniContext->pts_field && |
| 872 | jniContext->colorspace_field && |
| 873 | jniContext->y_stride_field && |
| 874 | jniContext->uv_stride_field && |
| 875 | jniContext->init_for_yuv_frame_method; |
| 876 |
| 877 | env->DeleteLocalRef(outputBufferClass); |
| 878 |
| 879 | if (!fields_ok || env->ExceptionCheck()) { |
| 880 | LOGE("Failed to resolve VideoDecoderOutputBuffer JNI members."); |
| 881 | releaseContext(codecContext); |
| 882 | jniContext->codecContext = nullptr; |
| 883 | delete jniContext; |
| 884 | return NULL; |
| 885 | } |
| 886 |
| 887 | return jniContext; |
| 888 | } |
| 889 |
| 890 | VIDEO_DECODER_FUNC( |
| 891 | jlong, |
| 892 | ffmpegInitialize, |
| 893 | jstring codecName, |
| 894 | jbyteArray extraData, |
| 895 | jint threads, |
| 896 | jint degree, |
| 897 | jint width, |
| 898 | jint height) { |
| 899 | const AVCodec* codec = getCodecByName(env, codecName); |
| 900 | if (!codec) { |
| 901 | LOGE("Codec not found."); |
| 902 | return 0L; |
| 903 | } |
| 904 |
| 905 | return reinterpret_cast<jlong>( |
| 906 | createVideoContext( |
| 907 | env, codec, extraData, threads, degree, width, height)); |
| 908 | } |
| 909 |
| 910 | VIDEO_DECODER_FUNC(jlong, ffmpegReset, jlong jContext) { |
| 911 | JniContext* const jniContext = |
| 912 | reinterpret_cast<JniContext*>(jContext); |
| 913 | if (!jniContext || !jniContext->codecContext) { |
| 914 | LOGE("Tried to reset without a context."); |
| 915 | return 0L; |
| 916 | } |
| 917 |
| 918 | avcodec_flush_buffers(jniContext->codecContext); |
| 919 | return reinterpret_cast<jlong>(jniContext); |
| 920 | } |
| 921 |
| 922 | VIDEO_DECODER_FUNC(void, ffmpegRelease, jlong jContext) { |
| 923 | JniContext* const jniContext = |
| 924 | reinterpret_cast<JniContext*>(jContext); |
| 925 | if (!jniContext) { |
| 926 | return; |
| 927 | } |
| 928 |
| 929 | if (jniContext->codecContext) { |
| 930 | avcodec_free_context(&jniContext->codecContext); |
| 931 | } |
| 932 |
| 933 | if (jniContext->swsContext) { |
| 934 | sws_freeContext(jniContext->swsContext); |
| 935 | jniContext->swsContext = nullptr; |
| 936 | } |
| 937 |
| 938 | if (jniContext->surface != nullptr) { |
| 939 | env->DeleteGlobalRef(jniContext->surface); |
| 940 | jniContext->surface = nullptr; |
| 941 | } |
| 942 |
| 943 | // ~JniContext releases ANativeWindow. |
| 944 | delete jniContext; |
| 945 | } |
| 946 |
| 947 | VIDEO_DECODER_FUNC( |
| 948 | jint, |
| 949 | ffmpegSendPacket, |
| 950 | jlong jContext, |
| 951 | jobject encodedData, |
| 952 | jint length, |
| 953 | jlong inputTimeUs) { |
| 954 | JniContext* const jniContext = |
| 955 | reinterpret_cast<JniContext*>(jContext); |
| 956 |
| 957 | if (!jniContext || |
| 958 | !jniContext->codecContext || |
| 959 | !encodedData || |
| 960 | length < 0) { |
| 961 | LOGE("Invalid context or input buffer."); |
| 962 | return VIDEO_DECODER_ERROR_OTHER; |
| 963 | } |
| 964 |
| 965 | uint8_t* inputBuffer = |
| 966 | static_cast<uint8_t*>( |
| 967 | env->GetDirectBufferAddress(encodedData)); |
| 968 | const jlong inputCapacity = |
| 969 | env->GetDirectBufferCapacity(encodedData); |
| 970 |
| 971 | if (!inputBuffer || |
| 972 | inputCapacity < 0 || |
| 973 | inputCapacity < static_cast<jlong>(length)) { |
| 974 | LOGE("Invalid direct input buffer: capacity=%lld length=%d", |
| 975 | static_cast<long long>(inputCapacity), length); |
| 976 | return VIDEO_DECODER_ERROR_OTHER; |
| 977 | } |
| 978 |
| 979 | AVPacket* packet = av_packet_alloc(); |
| 980 | if (!packet) { |
| 981 | LOGE("Failed to allocate packet."); |
| 982 | return VIDEO_DECODER_ERROR_OTHER; |
| 983 | } |
| 984 |
| 985 | // Do not point AVPacket directly at Java memory. |
| 986 | // |
| 987 | // The normal DecoderInputBuffer is padded, but PendingInput in the PR is |
| 988 | // copied into a direct ByteBuffer whose capacity is exactly the payload |
| 989 | // length. FFmpeg parsers may legally read AV_INPUT_BUFFER_PADDING_SIZE bytes |
| 990 | // past packet->data + packet->size. av_new_packet() allocates that padding |
| 991 | // and zeroes it. |
| 992 | const int allocationResult = av_new_packet(packet, length); |
| 993 | if (allocationResult < 0) { |
| 994 | logError("av_new_packet", allocationResult); |
| 995 | av_packet_free(&packet); |
| 996 | return VIDEO_DECODER_ERROR_OTHER; |
| 997 | } |
| 998 |
| 999 | if (length > 0) { |
| 1000 | std::memcpy( |
| 1001 | packet->data, |
| 1002 | inputBuffer, |
| 1003 | static_cast<size_t>(length)); |
| 1004 | } |
| 1005 |
| 1006 | packet->pts = inputTimeUs; |
| 1007 |
| 1008 | const int result = |
| 1009 | avcodec_send_packet(jniContext->codecContext, packet); |
| 1010 |
| 1011 | av_packet_free(&packet); |
| 1012 |
| 1013 | if (result == 0) { |
| 1014 | return VIDEO_DECODER_SUCCESS; |
| 1015 | } |
| 1016 |
| 1017 | if (result == AVERROR_INVALIDDATA) { |
| 1018 | logError("avcodec_send_packet", result); |
| 1019 | return VIDEO_DECODER_ERROR_INVALID_DATA; |
| 1020 | } |
| 1021 |
| 1022 | if (result == AVERROR(EAGAIN)) { |
| 1023 | return VIDEO_DECODER_ERROR_READ_FRAME; |
| 1024 | } |
| 1025 |
| 1026 | logError("avcodec_send_packet", result); |
| 1027 | return VIDEO_DECODER_ERROR_OTHER; |
| 1028 | } |
| 1029 |
| 1030 | VIDEO_DECODER_FUNC( |
| 1031 | jint, |
| 1032 | ffmpegReceiveFrame, |
| 1033 | jlong jContext, |
| 1034 | jint outputMode, |
| 1035 | jobject jOutputBuffer, |
| 1036 | jboolean decodeOnly) { |
| 1037 | JniContext* const jniContext = |
| 1038 | reinterpret_cast<JniContext*>(jContext); |
| 1039 |
| 1040 | if (!jniContext || |
| 1041 | !jniContext->codecContext || |
| 1042 | !jOutputBuffer) { |
| 1043 | LOGE("Invalid context or output buffer."); |
| 1044 | return VIDEO_DECODER_ERROR_OTHER; |
| 1045 | } |
| 1046 |
| 1047 | AVFrame* rawFrame = av_frame_alloc(); |
| 1048 | if (!rawFrame) { |
| 1049 | LOGE("Failed to allocate output frame."); |
| 1050 | return VIDEO_DECODER_ERROR_OTHER; |
| 1051 | } |
| 1052 |
| 1053 | const int receiveResult = |
| 1054 | avcodec_receive_frame( |
| 1055 | jniContext->codecContext, rawFrame); |
| 1056 |
| 1057 | if (receiveResult == AVERROR(EAGAIN)) { |
| 1058 | av_frame_free(&rawFrame); |
| 1059 | return VIDEO_DECODER_ERROR_INVALID_DATA; |
| 1060 | } |
| 1061 |
| 1062 | if (receiveResult != 0) { |
| 1063 | logError("avcodec_receive_frame", receiveResult); |
| 1064 | av_frame_free(&rawFrame); |
| 1065 | return VIDEO_DECODER_ERROR_OTHER; |
| 1066 | } |
| 1067 |
| 1068 | // The frame must still be dequeued from FFmpeg for decode-only input, but |
| 1069 | // there is no need to allocate/convert/copy it into the Java output buffer. |
| 1070 | if (decodeOnly) { |
| 1071 | av_frame_free(&rawFrame); |
| 1072 | return VIDEO_DECODER_ERROR_INVALID_DATA; |
| 1073 | } |
| 1074 |
| 1075 | AVFrame* frame = rawFrame; |
| 1076 |
| 1077 | if (rawFrame->format != AV_PIX_FMT_YUV420P) { |
| 1078 | frame = |
| 1079 | cvt_format( |
| 1080 | jniContext, |
| 1081 | rawFrame, |
| 1082 | AV_PIX_FMT_YUV420P, |
| 1083 | rawFrame->width, |
| 1084 | rawFrame->height); |
| 1085 |
| 1086 | if (!frame) { |
| 1087 | av_frame_free(&rawFrame); |
| 1088 | LOGW("Convert to YUV420P failed."); |
| 1089 | return VIDEO_DECODER_ERROR_OTHER; |
| 1090 | } |
| 1091 |
| 1092 | av_frame_free(&rawFrame); |
| 1093 | } |
| 1094 |
| 1095 | const int decodedWidth = frame->width; |
| 1096 | const int decodedHeight = frame->height; |
| 1097 |
| 1098 | if (decodedWidth <= 0 || decodedHeight <= 0) { |
| 1099 | LOGE("Invalid decoded frame dimensions: %dx%d", |
| 1100 | decodedWidth, decodedHeight); |
| 1101 | av_frame_free(&frame); |
| 1102 | return VIDEO_DECODER_ERROR_OTHER; |
| 1103 | } |
| 1104 |
| 1105 | int outputWidth = decodedWidth; |
| 1106 | int outputHeight = decodedHeight; |
| 1107 |
| 1108 | if (jniContext->rotate_degree == 90 || |
| 1109 | jniContext->rotate_degree == 270) { |
| 1110 | outputWidth = decodedHeight; |
| 1111 | outputHeight = decodedWidth; |
| 1112 | } |
| 1113 |
| 1114 | const int colorSpace = |
| 1115 | cvt_colorspace(frame->colorspace); |
| 1116 |
| 1117 | // We store the intermediate Java buffer as tightly packed I420. |
| 1118 | const int strideY = outputWidth; |
| 1119 | const int strideUv = (outputWidth + 1) / 2; |
| 1120 |
| 1121 | const int oldWidth = |
| 1122 | env->GetIntField( |
| 1123 | jOutputBuffer, jniContext->width_field); |
| 1124 | const int oldHeight = |
| 1125 | env->GetIntField( |
| 1126 | jOutputBuffer, jniContext->height_field); |
| 1127 |
| 1128 | if (oldWidth != outputWidth || |
| 1129 | oldHeight != outputHeight) { |
| 1130 | const jboolean initResult = |
| 1131 | env->CallBooleanMethod( |
| 1132 | jOutputBuffer, |
| 1133 | jniContext->init_for_yuv_frame_method, |
| 1134 | outputWidth, |
| 1135 | outputHeight, |
| 1136 | strideY, |
| 1137 | strideUv, |
| 1138 | colorSpace); |
| 1139 |
| 1140 | if (env->ExceptionCheck()) { |
| 1141 | av_frame_free(&frame); |
| 1142 | return VIDEO_DECODER_ERROR_OTHER; |
| 1143 | } |
| 1144 |
| 1145 | if (!initResult) { |
| 1146 | av_frame_free(&frame); |
| 1147 | return VIDEO_DECODER_ERROR_OTHER; |
| 1148 | } |
| 1149 | } |
| 1150 |
| 1151 | // Color space is frame metadata and can change without a resolution change. |
| 1152 | env->SetIntField( |
| 1153 | jOutputBuffer, |
| 1154 | jniContext->colorspace_field, |
| 1155 | colorSpace); |
| 1156 |
| 1157 | if (frame->pts != AV_NOPTS_VALUE) { |
| 1158 | env->SetLongField( |
| 1159 | jOutputBuffer, |
| 1160 | jniContext->pts_field, |
| 1161 | frame->pts); |
| 1162 | } |
| 1163 |
| 1164 | if (env->ExceptionCheck()) { |
| 1165 | av_frame_free(&frame); |
| 1166 | return VIDEO_DECODER_ERROR_OTHER; |
| 1167 | } |
| 1168 |
| 1169 | jobject dataObject = |
| 1170 | env->GetObjectField( |
| 1171 | jOutputBuffer, |
| 1172 | jniContext->data_field); |
| 1173 |
| 1174 | if (!dataObject) { |
| 1175 | LOGE("Output data buffer is null."); |
| 1176 | av_frame_free(&frame); |
| 1177 | return VIDEO_DECODER_ERROR_OTHER; |
| 1178 | } |
| 1179 |
| 1180 | uint8_t* data = |
| 1181 | static_cast<uint8_t*>( |
| 1182 | env->GetDirectBufferAddress(dataObject)); |
| 1183 | const jlong dataCapacity = |
| 1184 | env->GetDirectBufferCapacity(dataObject); |
| 1185 |
| 1186 | const int uvHeight = (outputHeight + 1) / 2; |
| 1187 |
| 1188 | const size_t lengthY = |
| 1189 | static_cast<size_t>(strideY) * |
| 1190 | static_cast<size_t>(outputHeight); |
| 1191 | const size_t lengthUv = |
| 1192 | static_cast<size_t>(strideUv) * |
| 1193 | static_cast<size_t>(uvHeight); |
| 1194 | const size_t requiredSize = |
| 1195 | lengthY + 2u * lengthUv; |
| 1196 |
| 1197 | if (!data || |
| 1198 | dataCapacity < 0 || |
| 1199 | requiredSize > static_cast<size_t>(dataCapacity)) { |
| 1200 | LOGE("Invalid output YUV buffer: capacity=%lld required=%zu", |
| 1201 | static_cast<long long>(dataCapacity), |
| 1202 | requiredSize); |
| 1203 |
| 1204 | env->DeleteLocalRef(dataObject); |
| 1205 | av_frame_free(&frame); |
| 1206 | return VIDEO_DECODER_ERROR_OTHER; |
| 1207 | } |
| 1208 |
| 1209 | const libyuv::RotationMode rotate = |
| 1210 | cvt_rotate(jniContext->rotate_degree); |
| 1211 |
| 1212 | const int rotateResult = |
| 1213 | libyuv::I420Rotate( |
| 1214 | frame->data[0], |
| 1215 | frame->linesize[0], |
| 1216 | frame->data[1], |
| 1217 | frame->linesize[1], |
| 1218 | frame->data[2], |
| 1219 | frame->linesize[2], |
| 1220 | data, |
| 1221 | strideY, |
| 1222 | data + lengthY, |
| 1223 | strideUv, |
| 1224 | data + lengthY + lengthUv, |
| 1225 | strideUv, |
| 1226 | decodedWidth, |
| 1227 | decodedHeight, |
| 1228 | rotate); |
| 1229 |
| 1230 | env->DeleteLocalRef(dataObject); |
| 1231 | av_frame_free(&frame); |
| 1232 |
| 1233 | if (rotateResult != 0) { |
| 1234 | LOGE("I420Rotate failed: %d", rotateResult); |
| 1235 | return VIDEO_DECODER_ERROR_OTHER; |
| 1236 | } |
| 1237 |
| 1238 | return VIDEO_DECODER_SUCCESS; |
| 1239 | } |
| 1240 |
| 1241 | int I420ToRgba8888(int colorSpace, |
| 1242 | const uint8_t* srcY, |
| 1243 | int srcStrideY, |
| 1244 | const uint8_t* srcU, |
| 1245 | int srcStrideU, |
| 1246 | const uint8_t* srcV, |
| 1247 | int srcStrideV, |
| 1248 | uint8_t* dstRgba, |
| 1249 | int dstStrideRgba, |
| 1250 | int width, |
| 1251 | int height) { |
| 1252 | // libyuv names formats by register order. On little-endian Android, |
| 1253 | // I420ToABGR writes R,G,B,A bytes in memory, which matches RGBA_8888. |
| 1254 | switch (colorSpace) { |
| 1255 | case 2: // COLORSPACE_BT709 |
| 1256 | return libyuv::H420ToABGR( |
| 1257 | srcY, srcStrideY, |
| 1258 | srcU, srcStrideU, |
| 1259 | srcV, srcStrideV, |
| 1260 | dstRgba, dstStrideRgba, |
| 1261 | width, height); |
| 1262 | case 3: // COLORSPACE_BT2020 |
| 1263 | return libyuv::U420ToABGR( |
| 1264 | srcY, srcStrideY, |
| 1265 | srcU, srcStrideU, |
| 1266 | srcV, srcStrideV, |
| 1267 | dstRgba, dstStrideRgba, |
| 1268 | width, height); |
| 1269 | case 1: // COLORSPACE_BT601 |
| 1270 | default: |
| 1271 | return libyuv::I420ToABGR( |
| 1272 | srcY, srcStrideY, |
| 1273 | srcU, srcStrideU, |
| 1274 | srcV, srcStrideV, |
| 1275 | dstRgba, dstStrideRgba, |
| 1276 | width, height); |
| 1277 | } |
| 1278 | } |
| 1279 |
| 1280 | VIDEO_DECODER_FUNC( |
| 1281 | jint, |
| 1282 | ffmpegRenderFrame, |
| 1283 | jlong jContext, |
| 1284 | jobject jSurface, |
| 1285 | jobject jOutputBuffer, |
| 1286 | jint displayedWidth, |
| 1287 | jint displayedHeight) { |
| 1288 | JniContext* const jniContext = |
| 1289 | reinterpret_cast<JniContext*>(jContext); |
| 1290 |
| 1291 | if (!jniContext || |
| 1292 | !jniContext->codecContext || |
| 1293 | !jSurface || |
| 1294 | !jOutputBuffer || |
| 1295 | displayedWidth <= 0 || |
| 1296 | displayedHeight <= 0) { |
| 1297 | LOGE("Invalid render arguments."); |
| 1298 | return VIDEO_DECODER_ERROR_OTHER; |
| 1299 | } |
| 1300 |
| 1301 | if (!jniContext->MaybeAcquireNativeWindow(env, jSurface)) { |
| 1302 | return VIDEO_DECODER_ERROR_OTHER; |
| 1303 | } |
| 1304 |
| 1305 | ANativeWindow* const nativeWindow = jniContext->native_window; |
| 1306 | if (!nativeWindow) { |
| 1307 | return VIDEO_DECODER_ERROR_OTHER; |
| 1308 | } |
| 1309 |
| 1310 | jobject dataObject = |
| 1311 | env->GetObjectField(jOutputBuffer, jniContext->data_field); |
| 1312 | if (!dataObject) { |
| 1313 | LOGE("Output data buffer is null."); |
| 1314 | return VIDEO_DECODER_ERROR_OTHER; |
| 1315 | } |
| 1316 |
| 1317 | uint8_t* data = static_cast<uint8_t*>( |
| 1318 | env->GetDirectBufferAddress(dataObject)); |
| 1319 | const jlong dataCapacity = env->GetDirectBufferCapacity(dataObject); |
| 1320 |
| 1321 | const int frameWidth = |
| 1322 | env->GetIntField(jOutputBuffer, jniContext->width_field); |
| 1323 | const int frameHeight = |
| 1324 | env->GetIntField(jOutputBuffer, jniContext->height_field); |
| 1325 | const int srcStrideY = |
| 1326 | env->GetIntField(jOutputBuffer, jniContext->y_stride_field); |
| 1327 | const int srcStrideUv = |
| 1328 | env->GetIntField(jOutputBuffer, jniContext->uv_stride_field); |
| 1329 | const int colorSpace = |
| 1330 | env->GetIntField(jOutputBuffer, jniContext->colorspace_field); |
| 1331 |
| 1332 | if (!data || dataCapacity < 0) { |
| 1333 | LOGE("Output data is not a direct ByteBuffer."); |
| 1334 | env->DeleteLocalRef(dataObject); |
| 1335 | return VIDEO_DECODER_ERROR_OTHER; |
| 1336 | } |
| 1337 |
| 1338 | if (frameWidth != displayedWidth || frameHeight != displayedHeight) { |
| 1339 | LOGE("Frame/window size mismatch: frame=%dx%d window=%dx%d", |
| 1340 | frameWidth, frameHeight, displayedWidth, displayedHeight); |
| 1341 | env->DeleteLocalRef(dataObject); |
| 1342 | return VIDEO_DECODER_ERROR_OTHER; |
| 1343 | } |
| 1344 |
| 1345 | if (srcStrideY < frameWidth || srcStrideUv < (frameWidth + 1) / 2) { |
| 1346 | LOGE("Invalid source strides: frame=%dx%d y=%d uv=%d", |
| 1347 | frameWidth, frameHeight, srcStrideY, srcStrideUv); |
| 1348 | env->DeleteLocalRef(dataObject); |
| 1349 | return VIDEO_DECODER_ERROR_OTHER; |
| 1350 | } |
| 1351 |
| 1352 | const int srcUvHeight = (frameHeight + 1) / 2; |
| 1353 | const size_t srcLengthY = |
| 1354 | static_cast<size_t>(srcStrideY) * static_cast<size_t>(frameHeight); |
| 1355 | const size_t srcLengthUv = |
| 1356 | static_cast<size_t>(srcStrideUv) * static_cast<size_t>(srcUvHeight); |
| 1357 | const size_t srcRequiredSize = srcLengthY + 2u * srcLengthUv; |
| 1358 |
| 1359 | if (srcRequiredSize > static_cast<size_t>(dataCapacity)) { |
| 1360 | LOGE("Source YUV buffer too small: capacity=%lld required=%zu", |
| 1361 | static_cast<long long>(dataCapacity), srcRequiredSize); |
| 1362 | env->DeleteLocalRef(dataObject); |
| 1363 | return VIDEO_DECODER_ERROR_OTHER; |
| 1364 | } |
| 1365 |
| 1366 | const uint8_t* srcY = data; |
| 1367 | const uint8_t* srcU = data + srcLengthY; |
| 1368 | const uint8_t* srcV = srcU + srcLengthUv; |
| 1369 |
| 1370 | // YV12 cannot represent odd dimensions. Go directly to the packed RGBA |
| 1371 | // fallback instead of trying to invent a YV12 layout. |
| 1372 | if ((displayedWidth & 1) != 0 || (displayedHeight & 1) != 0) { |
| 1373 | jniContext->use_rgba_fallback = true; |
| 1374 | } |
| 1375 |
| 1376 | // At most two attempts are needed. The first one tries the cheap YV12 path. |
| 1377 | // If the vendor returns an unusable legacy stride (notably stride == 0), |
| 1378 | // switch this decoder context permanently to RGBA_8888 and retry the same |
| 1379 | // frame without guessing the YUV plane layout. |
| 1380 | for (int attempt = 0; attempt < 2; ++attempt) { |
| 1381 | const bool rgba = jniContext->use_rgba_fallback; |
| 1382 | const int windowFormat = |
| 1383 | rgba ? WINDOW_FORMAT_RGBA_8888 : kImageFormatYV12; |
| 1384 |
| 1385 | if (jniContext->native_window_width != displayedWidth || |
| 1386 | jniContext->native_window_height != displayedHeight || |
| 1387 | jniContext->native_window_format != windowFormat) { |
| 1388 | const int geometryResult = |
| 1389 | ANativeWindow_setBuffersGeometry( |
| 1390 | nativeWindow, |
| 1391 | displayedWidth, |
| 1392 | displayedHeight, |
| 1393 | windowFormat); |
| 1394 | if (geometryResult != 0) { |
| 1395 | LOGE("ANativeWindow_setBuffersGeometry failed: %d format=0x%x", |
| 1396 | geometryResult, windowFormat); |
| 1397 | env->DeleteLocalRef(dataObject); |
| 1398 | return VIDEO_DECODER_ERROR_OTHER; |
| 1399 | } |
| 1400 |
| 1401 | jniContext->native_window_width = displayedWidth; |
| 1402 | jniContext->native_window_height = displayedHeight; |
| 1403 | jniContext->native_window_format = windowFormat; |
| 1404 | } |
| 1405 |
| 1406 | ANativeWindow_Buffer windowBuffer; |
| 1407 | const int lockResult = |
| 1408 | ANativeWindow_lock(nativeWindow, &windowBuffer, nullptr); |
| 1409 |
| 1410 | if (lockResult == -19) { |
| 1411 | if (jniContext->surface != nullptr) { |
| 1412 | env->DeleteGlobalRef(jniContext->surface); |
| 1413 | jniContext->surface = nullptr; |
| 1414 | } |
| 1415 | jniContext->native_window_width = 0; |
| 1416 | jniContext->native_window_height = 0; |
| 1417 | jniContext->native_window_format = 0; |
| 1418 | env->DeleteLocalRef(dataObject); |
| 1419 | return VIDEO_DECODER_ERROR_SURFACE; |
| 1420 | } |
| 1421 |
| 1422 | if (lockResult != 0 || !windowBuffer.bits) { |
| 1423 | LOGE("ANativeWindow_lock failed: %d", lockResult); |
| 1424 | env->DeleteLocalRef(dataObject); |
| 1425 | return VIDEO_DECODER_ERROR_OTHER; |
| 1426 | } |
| 1427 |
| 1428 | if (windowBuffer.width != displayedWidth || |
| 1429 | windowBuffer.height != displayedHeight) { |
| 1430 | LOGE("Unexpected ANativeWindow size: buffer=%dx%d requested=%dx%d", |
| 1431 | windowBuffer.width, windowBuffer.height, |
| 1432 | displayedWidth, displayedHeight); |
| 1433 | ANativeWindow_unlockAndPost(nativeWindow); |
| 1434 | jniContext->native_window_width = 0; |
| 1435 | jniContext->native_window_height = 0; |
| 1436 | jniContext->native_window_format = 0; |
| 1437 | env->DeleteLocalRef(dataObject); |
| 1438 | return VIDEO_DECODER_ERROR_OTHER; |
| 1439 | } |
| 1440 |
| 1441 | if (windowBuffer.format != windowFormat) { |
| 1442 | LOGE("Unexpected ANativeWindow format: got=0x%x requested=0x%x", |
| 1443 | windowBuffer.format, windowFormat); |
| 1444 | ANativeWindow_unlockAndPost(nativeWindow); |
| 1445 | jniContext->native_window_width = 0; |
| 1446 | jniContext->native_window_height = 0; |
| 1447 | jniContext->native_window_format = 0; |
| 1448 | env->DeleteLocalRef(dataObject); |
| 1449 | return VIDEO_DECODER_ERROR_OTHER; |
| 1450 | } |
| 1451 |
| 1452 | if (!rgba && windowBuffer.stride == 0) { |
| 1453 | // Some gralloc implementations expose no meaningful legacy stride for |
| 1454 | // a YUV buffer through ANativeWindow_Buffer. There is no public NDK API |
| 1455 | // that exposes the individual Y/U/V plane layouts of this already locked |
| 1456 | // ANativeWindow buffer. Do not guess ALIGN(width, 16): a vendor is free |
| 1457 | // to use a larger allocation stride. |
| 1458 | LOGW("YV12 ANativeWindow returned stride=0; switching to RGBA fallback " |
| 1459 | "for this decoder context (size=%dx%d)", |
| 1460 | windowBuffer.width, windowBuffer.height); |
| 1461 |
| 1462 | const int unlockResult = ANativeWindow_unlockAndPost(nativeWindow); |
| 1463 | if (unlockResult != 0) { |
| 1464 | LOGE("ANativeWindow_unlockAndPost failed while switching format: %d", |
| 1465 | unlockResult); |
| 1466 | env->DeleteLocalRef(dataObject); |
| 1467 | return VIDEO_DECODER_ERROR_OTHER; |
| 1468 | } |
| 1469 |
| 1470 | jniContext->use_rgba_fallback = true; |
| 1471 | jniContext->native_window_width = 0; |
| 1472 | jniContext->native_window_height = 0; |
| 1473 | jniContext->native_window_format = 0; |
| 1474 | continue; |
| 1475 | } |
| 1476 |
| 1477 | int renderResult = VIDEO_DECODER_SUCCESS; |
| 1478 |
| 1479 | if (!rgba) { |
| 1480 | if (windowBuffer.stride < windowBuffer.width || |
| 1481 | (windowBuffer.stride & 15) != 0) { |
| 1482 | LOGE("Invalid YV12 stride: width=%d stride=%d", |
| 1483 | windowBuffer.width, windowBuffer.stride); |
| 1484 | renderResult = VIDEO_DECODER_ERROR_OTHER; |
| 1485 | } else { |
| 1486 | const int dstStrideY = windowBuffer.stride; |
| 1487 | const int dstStrideUv = AlignTo16(dstStrideY / 2); |
| 1488 | const int dstUvHeight = windowBuffer.height / 2; |
| 1489 |
| 1490 | const size_t dstLengthY = |
| 1491 | static_cast<size_t>(dstStrideY) * |
| 1492 | static_cast<size_t>(windowBuffer.height); |
| 1493 | const size_t dstLengthUv = |
| 1494 | static_cast<size_t>(dstStrideUv) * |
| 1495 | static_cast<size_t>(dstUvHeight); |
| 1496 |
| 1497 | uint8_t* dstY = static_cast<uint8_t*>(windowBuffer.bits); |
| 1498 | uint8_t* dstV = dstY + dstLengthY; |
| 1499 | uint8_t* dstU = dstV + dstLengthUv; |
| 1500 |
| 1501 | const int copyResult = |
| 1502 | libyuv::I420Copy( |
| 1503 | srcY, srcStrideY, |
| 1504 | srcU, srcStrideUv, |
| 1505 | srcV, srcStrideUv, |
| 1506 | dstY, dstStrideY, |
| 1507 | dstU, dstStrideUv, |
| 1508 | dstV, dstStrideUv, |
| 1509 | frameWidth, frameHeight); |
| 1510 |
| 1511 | if (copyResult != 0) { |
| 1512 | LOGE("I420Copy failed: %d", copyResult); |
| 1513 | renderResult = VIDEO_DECODER_ERROR_OTHER; |
| 1514 | } |
| 1515 | } |
| 1516 | } else { |
| 1517 | if (windowBuffer.stride < windowBuffer.width) { |
| 1518 | LOGE("Invalid RGBA stride: width=%d stride=%d", |
| 1519 | windowBuffer.width, windowBuffer.stride); |
| 1520 | renderResult = VIDEO_DECODER_ERROR_OTHER; |
| 1521 | } else { |
| 1522 | const int dstStrideBytes = windowBuffer.stride * 4; |
| 1523 | uint8_t* dstRgba = static_cast<uint8_t*>(windowBuffer.bits); |
| 1524 |
| 1525 | const int convertResult = |
| 1526 | I420ToRgba8888( |
| 1527 | colorSpace, |
| 1528 | srcY, srcStrideY, |
| 1529 | srcU, srcStrideUv, |
| 1530 | srcV, srcStrideUv, |
| 1531 | dstRgba, dstStrideBytes, |
| 1532 | frameWidth, frameHeight); |
| 1533 |
| 1534 | if (convertResult != 0) { |
| 1535 | LOGE("I420ToRgba8888 failed: %d", convertResult); |
| 1536 | renderResult = VIDEO_DECODER_ERROR_OTHER; |
| 1537 | } |
| 1538 | } |
| 1539 | } |
| 1540 |
| 1541 | const int unlockResult = |
| 1542 | ANativeWindow_unlockAndPost(nativeWindow); |
| 1543 | if (unlockResult != 0) { |
| 1544 | LOGE("ANativeWindow_unlockAndPost failed: %d", unlockResult); |
| 1545 | env->DeleteLocalRef(dataObject); |
| 1546 | return VIDEO_DECODER_ERROR_OTHER; |
| 1547 | } |
| 1548 |
| 1549 | env->DeleteLocalRef(dataObject); |
| 1550 | return renderResult; |
| 1551 | } |
| 1552 |
| 1553 | env->DeleteLocalRef(dataObject); |
| 1554 | return VIDEO_DECODER_ERROR_OTHER; |
| 1555 | } |
| 1556 |
| 1557 | extern "C" int ffmpegOnJNILoad(JavaVM *vm, JNIEnv *env) { |
| 1558 | jclass clazz = |
| 1559 | env->FindClass( |
| 1560 | "androidx/media3/decoder/ffmpeg/FfmpegAudioDecoder"); |
| 1561 | if (!clazz) { |
| 1562 | LOGE("JNI_OnLoad: FindClass failed"); |
| 1563 | return -1; |
| 1564 | } |
| 1565 |
| 1566 | growOutputBufferMethod = |
| 1567 | env->GetMethodID( |
| 1568 | clazz, |
| 1569 | "growOutputBuffer", |
| 1570 | "(Landroidx/media3/decoder/" |
| 1571 | "SimpleDecoderOutputBuffer;I)Ljava/nio/ByteBuffer;"); |
| 1572 | if (!growOutputBufferMethod) { |
| 1573 | LOGE("JNI_OnLoad: GetMethodID failed"); |
| 1574 | return -1; |
| 1575 | } |
| 1576 |
| 1577 | static const JNINativeMethod kFfmpegAudioDecoderMethods[] = { |
| 1578 | {"ffmpegInitialize", "(Ljava/lang/String;[BZII)J", |
| 1579 | reinterpret_cast<void*>(ffmpegInitialize)}, |
| 1580 | {"ffmpegDecode", |
| 1581 | "(JLjava/nio/ByteBuffer;ILandroidx/media3/decoder/" |
| 1582 | "SimpleDecoderOutputBuffer;Ljava/nio/ByteBuffer;I)I", |
| 1583 | reinterpret_cast<void*>(ffmpegDecode)}, |
| 1584 | {"ffmpegGetChannelCount", "(J)I", |
| 1585 | reinterpret_cast<void*>(ffmpegGetChannelCount)}, |
| 1586 | {"ffmpegGetSampleRate", "(J)I", |
| 1587 | reinterpret_cast<void*>(ffmpegGetSampleRate)}, |
| 1588 | {"ffmpegReset", "(J[B)J", |
| 1589 | reinterpret_cast<void*>(ffmpegReset)}, |
| 1590 | {"ffmpegRelease", "(J)V", |
| 1591 | reinterpret_cast<void*>(ffmpegRelease)}, |
| 1592 | }; |
| 1593 |
| 1594 | if (env->RegisterNatives( |
| 1595 | clazz, |
| 1596 | kFfmpegAudioDecoderMethods, |
| 1597 | sizeof(kFfmpegAudioDecoderMethods) / |
| 1598 | sizeof(kFfmpegAudioDecoderMethods[0])) < 0) { |
| 1599 | LOGE("JNI_OnLoad: RegisterNatives failed for FfmpegAudioDecoder"); |
| 1600 | return -1; |
| 1601 | } |
| 1602 |
| 1603 | jclass libraryClazz = |
| 1604 | env->FindClass( |
| 1605 | "androidx/media3/decoder/ffmpeg/FfmpegLibrary"); |
| 1606 | if (!libraryClazz) { |
| 1607 | LOGE("JNI_OnLoad: FindClass failed for FfmpegLibrary"); |
| 1608 | return -1; |
| 1609 | } |
| 1610 |
| 1611 | static const JNINativeMethod kFfmpegLibraryMethods[] = { |
| 1612 | {"ffmpegGetVersion", "()Ljava/lang/String;", |
| 1613 | reinterpret_cast<void*>(ffmpegGetVersion)}, |
| 1614 | {"ffmpegGetInputBufferPaddingSize", "()I", |
| 1615 | reinterpret_cast<void*>(ffmpegGetInputBufferPaddingSize)}, |
| 1616 | {"ffmpegHasDecoder", "(Ljava/lang/String;)Z", |
| 1617 | reinterpret_cast<void*>(ffmpegHasDecoder)}, |
| 1618 | {"ffmpegGetAv1DecoderName", "()Ljava/lang/String;", |
| 1619 | reinterpret_cast<void*>(ffmpegGetAv1DecoderName)}, |
| 1620 | }; |
| 1621 |
| 1622 | if (env->RegisterNatives( |
| 1623 | libraryClazz, |
| 1624 | kFfmpegLibraryMethods, |
| 1625 | sizeof(kFfmpegLibraryMethods) / |
| 1626 | sizeof(kFfmpegLibraryMethods[0])) < 0) { |
| 1627 | LOGE("JNI_OnLoad: RegisterNatives failed for FfmpegLibrary"); |
| 1628 | return -1; |
| 1629 | } |
| 1630 |
| 1631 | return JNI_TRUE; |
| 1632 | } |
| 1633 |