Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement an FfmpegVideoRenderer #7132

Open
wants to merge 7 commits into
base: dev-v2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix ffmpeg library android.mk compilation error
  • Loading branch information
haohaozaici committed May 6, 2020
commit c80189f230073fbafa2a1cd355a8812d8a5b120d
12 changes: 6 additions & 6 deletions extensions/ffmpeg/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ android {
// Debug CMake build type causes video frames to drop,
// so native library should always use Release build type.
arguments "-DCMAKE_BUILD_TYPE=Release"
targets "ffmpegJNI"
targets "ffmpeg"
}
}
}
Expand All @@ -52,13 +52,13 @@ android {
}
}

// This option resolves the problem of finding libffmpegJNI.so
// This option resolves the problem of finding libffmpeg.so
// on multiple paths. The first one found is picked.
packagingOptions {
pickFirst 'lib/arm64-v8a/libffmpegJNI.so'
pickFirst 'lib/armeabi-v7a/libffmpegJNI.so'
pickFirst 'lib/x86/libffmpegJNI.so'
pickFirst 'lib/x86_64/libffmpegJNI.so'
pickFirst 'lib/arm64-v8a/libffmpeg.so'
pickFirst 'lib/armeabi-v7a/libffmpeg.so'
pickFirst 'lib/x86/libffmpeg.so'
pickFirst 'lib/x86_64/libffmpeg.so'
}

sourceSets.main {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public final class FfmpegLibrary {
private static final String TAG = "FfmpegLibrary";

private static final LibraryLoader LOADER =
new LibraryLoader("avutil", "swresample", "avcodec", "ffmpegJNI");
new LibraryLoader("avutil", "swresample", "avcodec", "ffmpeg");

private FfmpegLibrary() {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,13 @@
extends
SimpleDecoder<VideoDecoderInputBuffer, VideoDecoderOutputBuffer, FfmpegDecoderException> {

private static final String TAG = "FfmpegVideoDecoder";

// LINT.IfChange
private static final int VIDEO_DECODER_SUCCESS = 0;
private static final int VIDEO_DECODER_ERROR_INVALID_DATA = -1;
private static final int VIDEO_DECODER_ERROR_OTHER = -2;
private static final int VIDEO_DECODER_ERROR_READ_FRAME = -3;
private static final int VIDEO_DECODER_ERROR_SEND_PACKET = -4;
// LINT.ThenChange(../../../../../../../jni/ffmpeg_jni.cc)

private final String codecName;
Expand Down Expand Up @@ -141,15 +142,14 @@ protected FfmpegDecoderException decode(
ByteBuffer inputData = Util.castNonNull(inputBuffer.data);
int inputSize = inputData.limit();
// enqueue origin data
boolean needSendAgain = false;
int sendPacketResult = ffmpegSendPacket(nativeContext, inputData, inputSize,
inputBuffer.timeUs);
if (sendPacketResult == VIDEO_DECODER_ERROR_INVALID_DATA) {
outputBuffer.setFlags(C.BUFFER_FLAG_DECODE_ONLY);
return null;
} else if (sendPacketResult == VIDEO_DECODER_ERROR_READ_FRAME) {
// need read frame
needSendAgain = true;
Log.d(TAG, "VIDEO_DECODER_ERROR_READ_FRAME: " + "timeUs=" + inputBuffer.timeUs);
} else if (sendPacketResult == VIDEO_DECODER_ERROR_OTHER) {
return new FfmpegDecoderException("ffmpegDecode error: (see logcat)");
}
Expand All @@ -159,9 +159,7 @@ protected FfmpegDecoderException decode(
// We need to dequeue the decoded frame from the decoder even when the input data is
// decode-only.
int getFrameResult = ffmpegReceiveFrame(nativeContext, outputMode, outputBuffer, decodeOnly);
if (getFrameResult == VIDEO_DECODER_ERROR_SEND_PACKET) {
return null;
} else if (getFrameResult == VIDEO_DECODER_ERROR_OTHER) {
if (getFrameResult == VIDEO_DECODER_ERROR_OTHER) {
return new FfmpegDecoderException("ffmpegDecode error: (see logcat)");
}

Expand All @@ -173,10 +171,6 @@ protected FfmpegDecoderException decode(
outputBuffer.colorInfo = inputBuffer.colorInfo;
}

if (needSendAgain) {
Log.e("ffmpeg_jni", "timeUs=" + inputBuffer.timeUs + ", " + "nendSendAagin");
}

return null;
}

Expand Down
2 changes: 1 addition & 1 deletion extensions/ffmpeg/src/main/jni/Android.mk
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@ LOCAL_MODULE := ffmpeg
LOCAL_SRC_FILES := ffmpeg_jni.cc
LOCAL_C_INCLUDES := ffmpeg
LOCAL_SHARED_LIBRARIES := libavcodec libswresample libavutil
LOCAL_LDLIBS := -Lffmpeg/android-libs/$(TARGET_ARCH_ABI) -llog
LOCAL_LDLIBS := -Lffmpeg/android-libs/$(TARGET_ARCH_ABI) -llog -landroid
include $(BUILD_SHARED_LIBRARY)
12 changes: 6 additions & 6 deletions extensions/ffmpeg/src/main/jni/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# libgav1JNI requires modern CMake.
# libffmpegJNI requires modern CMake.
cmake_minimum_required(VERSION 3.7.1 FATAL_ERROR)

# libgav1JNI requires C++11.
# libffmpegJNI requires C++11.
set(CMAKE_CXX_STANDARD 11)

project(libffmpegJNI C CXX)
project(libffmpeg C CXX)

set(libgffmpeg_jni_root "${CMAKE_CURRENT_SOURCE_DIR}")
set(libgffmpeg_jni_build "${CMAKE_BINARY_DIR}")
Expand Down Expand Up @@ -39,7 +39,7 @@ set_target_properties(
${libgffmpeg_jni_output_directory}/libavcodec.so)

# Build libgffmpegJNI.
add_library(ffmpegJNI
add_library(ffmpeg
SHARED
ffmpeg_jni.cc)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
Expand All @@ -48,14 +48,14 @@ include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
find_library(android_log_lib log)

# Link libgffmpegJNI against used libraries.
target_link_libraries(ffmpegJNI
target_link_libraries(ffmpeg
PRIVATE android
PRIVATE avutil
PRIVATE swresample
PRIVATE avcodec
PRIVATE ${android_log_lib})

# Specify output directory for libgffmpegJNI.
set_target_properties(ffmpegJNI PROPERTIES
set_target_properties(ffmpeg PROPERTIES
LIBRARY_OUTPUT_DIRECTORY
${libgffmpeg_jni_output_directory})
24 changes: 9 additions & 15 deletions extensions/ffmpeg/src/main/jni/ffmpeg_jni.cc
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ static const int VIDEO_DECODER_SUCCESS = 0;
static const int VIDEO_DECODER_ERROR_INVALID_DATA = -1;
static const int VIDEO_DECODER_ERROR_OTHER = -2;
static const int VIDEO_DECODER_ERROR_READ_FRAME = -3;
static const int VIDEO_DECODER_ERROR_SEND_PACKET = -4;
// LINT.ThenChange(../java/com/google/android/exoplayer2/ext/ffmpeg/FfmpegVideoDecoder.java)

/**
Expand Down Expand Up @@ -448,11 +447,6 @@ void CopyPlane(const uint8_t *source, int source_stride, uint8_t *destination,

constexpr int AlignTo16(int value) { return (value + 15) & (~15); }

JniContext *createVideoContext(JNIEnv *env,
AVCodec *codec,
jbyteArray extraData,
jint threads);

JniContext *createVideoContext(JNIEnv *env,
AVCodec *codec,
jbyteArray extraData,
Expand Down Expand Up @@ -651,9 +645,12 @@ VIDEO_DECODER_FUNC(jint, ffmpegRenderFrame, jlong jContext, jobject jSurface,
}

ANativeWindow_Buffer native_window_buffer;
if (ANativeWindow_lock(jniContext->native_window, &native_window_buffer,
/*inOutDirtyBounds=*/nullptr) ||
native_window_buffer.bits == nullptr) {
int result = ANativeWindow_lock(jniContext->native_window, &native_window_buffer, nullptr);
if (result == -19) {
// Surface: dequeueBuffer failed (No such device)
jniContext->surface = nullptr;
return VIDEO_DECODER_SUCCESS;
} else if (result || native_window_buffer.bits == nullptr) {
LOGE("kJniStatusANativeWindowError");
return VIDEO_DECODER_ERROR_OTHER;
}
Expand Down Expand Up @@ -683,12 +680,9 @@ VIDEO_DECODER_FUNC(jint, ffmpegRenderFrame, jlong jContext, jobject jSurface,
displayedWidth,
displayedHeight);

const int y_plane_size =
native_window_buffer.stride * native_window_buffer.height;
const int32_t native_window_buffer_uv_height =
(native_window_buffer.height + 1) / 2;
const int native_window_buffer_uv_stride =
AlignTo16(native_window_buffer.stride / 2);
const int y_plane_size = native_window_buffer.stride * native_window_buffer.height;
const int32_t native_window_buffer_uv_height = (native_window_buffer.height + 1) / 2;
const int native_window_buffer_uv_stride = AlignTo16(native_window_buffer.stride / 2);

// TODO(b/140606738): Handle monochrome videos.

Expand Down