Skip to content

Instantly share code, notes, and snippets.

@courville
Created December 12, 2019 08:50
Show Gist options
  • Save courville/f64cb4cd6e6f644c52042de2ca5f1b6a to your computer and use it in GitHub Desktop.
Save courville/f64cb4cd6e6f644c52042de2ca5f1b6a to your computer and use it in GitHub Desktop.
attempt scale to fit with crop for good aspect ratio on amlogic
diff --git a/external/android/libsfdec/sfdec_ndkmediacodec.cpp b/external/android/libsfdec/sfdec_ndkmediacodec.cpp
index 57f6c7e..6c14586 100644
--- a/external/android/libsfdec/sfdec_ndkmediacodec.cpp
+++ b/external/android/libsfdec/sfdec_ndkmediacodec.cpp
@@ -19,7 +19,7 @@
#ifdef __ANDROID_API__
#undef __ANDROID_API__
-#define __ANDROID_API__ 21
+#define __ANDROID_API__ 26
#endif
#include <media/NdkMediaCodec.h>
@@ -42,6 +42,7 @@ struct sfdec_mediacodec
{
ANativeWindow *mNativeWindow;
AMediaFormat *mFormat;
+ AMediaFormat *mScaling;
AMediaCodec *mCodec;
int32_t width;
int32_t height;
@@ -145,6 +146,13 @@ static sfdec_priv_t *sfdec_init(sfdec_codec_t codec,
CHECK_STATUS(err);
sfdec->started = true;
+ // Try to impose scale to fit with crop on some SoC that fail to comply (namely amlogic) and result in bad aspect ratio
+ sfdec->mScaling = AMediaFormat_new();
+ CHECK(sfdec->mScaling != NULL);
+ // 2 = scale to fit with crop (check all values) 1 scale to fit
+ // scale to fit with crop should be ok for all configuration but to be checked
+ AMediaFormat_setInt32(sfdec->mScaling, "android._video-scaling", 2);
+
if (extradata && (codec == SFDEC_VIDEO_HEVC || codec == SFDEC_VIDEO_WMV))
sfdec_send_input2(sfdec, extradata, extradata_size, 0, 0, 1, 2/* BUFFER_FLAG_CODECCONFIG (not exported)*/);
return sfdec;
@@ -268,13 +276,17 @@ static int sfdec_read(sfdec_priv_t *sfdec, int64_t seek, sfdec_read_out_t *read_
return 0;
} else if (index == AMEDIACODEC_INFO_OUTPUT_FORMAT_CHANGED) {
+ // Apply scale to scale to fit with crop for correct aspect ratio on amlogic (at least)
+ AMediaCodec_setParameters(sfdec->mCodec, sfdec->mScaling);
+ // to be done at each buffer output format change --> log
+
if (init_renderer(sfdec))
continue;
read_out->flag |= SFDEC_READ_SIZE;
read_out->size.width = sfdec->width;
read_out->size.height = sfdec->height;
read_out->size.interlaced = 0;
- DBG LOG("INFO_FORMAT_CHANGED: %dx%d", sfdec->width, sfdec->height);
+ LOG("INFO_FORMAT_CHANGED: %dx%d", sfdec->width, sfdec->height);
return 0;
} else if (index == AMEDIACODEC_INFO_OUTPUT_BUFFERS_CHANGED) {
DBG LOG("INFO_OUTPUT_BUFFERS_CHANGED");
diff --git a/jni/Application.mk b/jni/Application.mk
index 4b4eb42..e192d1d 100644
--- a/jni/Application.mk
+++ b/jni/Application.mk
@@ -19,5 +19,5 @@ APP_ABI := armeabi-v7a
else
APP_ABI := $(NDK_APP_ABI)
endif
-APP_PLATFORM := android-21
+APP_PLATFORM := android-26
APP_STL := c++_shared
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment