├── .gitignore ├── .idea ├── compiler.xml ├── copyright │ └── profiles_settings.xml ├── encodings.xml ├── gradle.xml ├── misc.xml ├── modules.xml ├── runConfigurations.xml └── vcs.xml ├── README.md ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── settings.gradle └── x264-android ├── .gitignore ├── bintrayv1.gradle ├── build.gradle ├── installv1.gradle ├── proguard-rules.pro └── src └── main ├── AndroidManifest.xml ├── cpp ├── .gitignore ├── Android.mk ├── Application.mk ├── build_x264.sh └── libx264_jni.cpp ├── java └── com │ └── github │ └── bakaoh │ └── x264 │ ├── X264EncodeResult.java │ ├── X264Encoder.java │ ├── X264InitResult.java │ └── X264Params.java └── res └── values └── strings.xml /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | .DS_Store 7 | /build 8 | /captures 9 | .externalNativeBuild 10 | -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 17 | 18 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 19 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 46 | 47 | 48 | 49 | 50 | 1.8 51 | 52 | 57 | 58 | 59 | 60 | 61 | 62 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # X264Android # 2 | 3 | ## Description ## 4 | 5 | x264 jni for android 6 | 7 | ## Using ## 8 | 9 | * Make sure you have the jcenter repository included in the `build.gradle` file in the root of your project: 10 | 11 | ``` 12 | repositories { 13 | jcenter() 14 | } 15 | ``` 16 | 17 | * Include the following in your module's `build.gradle` file: 18 | 19 | ``` 20 | compile 'com.github.bakaoh:x264-android:X.X.X' 21 | ``` 22 | 23 | for the version X.X.X, see the project on [Bintray](https://bintray.com/bakaoh/maven/x264-android). 24 | 25 | * Create `X264Encoder` and `X264Params` 26 | 27 | ``` 28 | X264Encoder encoder = new X264Encoder(); 29 | 30 | X264Params params = new X264Params(); 31 | params.width = 1280; 32 | params.height = 720; 33 | params.bitrate = 1000 * 1024; 34 | params.fps = 25; 35 | params.gop = params.fps * 2; 36 | params.preset = "ultrafast"; 37 | params.profile = "baseline"; 38 | ``` 39 | 40 | * Init encoder 41 | 42 | ``` 43 | X264InitResult initRs = encoder.initEncoder(params); 44 | 45 | if (initRs.err == 0) { 46 | // process initRs.sps, initRs.pps 47 | } 48 | ``` 49 | 50 | * Encode frame 51 | 52 | ``` 53 | X264EncodeResult encodedFrame = encoder.encodeFrame(inputFrame, X264Params.CSP_NV21, presentationTimestamp); 54 | 55 | if (encodedFrame.err == 0) { 56 | // process encodedFrame.data 57 | } 58 | ``` 59 | 60 | ## Build instructions ## 61 | 62 | * Set the following environment variable: 63 | 64 | ``` 65 | X264_ANDROID_ROOT="" 66 | ``` 67 | 68 | * Download the [Android NDK][] and set its location in an environment variable: 69 | 70 | [Android NDK]: https://developer.android.com/tools/sdk/ndk/index.html 71 | 72 | ``` 73 | NDK_PATH="" 74 | ``` 75 | 76 | * Fetch and build libx264. 77 | 78 | ``` 79 | cd "${X264_ANDROID_ROOT}/x264/src/main/cpp" && \ 80 | git clone http://git.videolan.org/git/x264.git libx264 && \ 81 | ./build_x264.sh 82 | ``` 83 | 84 | * Open project in Android Studio to build the JNI native libraries. 85 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | 3 | buildscript { 4 | repositories { 5 | jcenter() 6 | } 7 | dependencies { 8 | classpath 'com.android.tools.build:gradle:2.2.2' 9 | classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.3' 10 | classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5' 11 | } 12 | } 13 | 14 | allprojects { 15 | repositories { 16 | jcenter() 17 | } 18 | } 19 | 20 | task clean(type: Delete) { 21 | delete rootProject.buildDir 22 | } 23 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | 3 | # IDE (e.g. Android Studio) users: 4 | # Gradle settings configured through the IDE *will override* 5 | # any settings specified in this file. 6 | 7 | # For more details on how to configure your build environment visit 8 | # http://www.gradle.org/docs/current/userguide/build_environment.html 9 | 10 | # Specifies the JVM arguments used for the daemon process. 11 | # The setting is particularly useful for tweaking memory settings. 12 | org.gradle.jvmargs=-Xmx1536m 13 | 14 | # When configured, Gradle will run in incubating parallel mode. 15 | # This option should only be used with decoupled projects. More details, visit 16 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 17 | # org.gradle.parallel=true 18 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bakaoh/X264Android/7bbb5854a3bd77eb3e4a52ab88b04a7d1c172953/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Mon Dec 28 10:00:20 PST 2015 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # Attempt to set APP_HOME 46 | # Resolve links: $0 may be a link 47 | PRG="$0" 48 | # Need this for relative symlinks. 49 | while [ -h "$PRG" ] ; do 50 | ls=`ls -ld "$PRG"` 51 | link=`expr "$ls" : '.*-> \(.*\)$'` 52 | if expr "$link" : '/.*' > /dev/null; then 53 | PRG="$link" 54 | else 55 | PRG=`dirname "$PRG"`"/$link" 56 | fi 57 | done 58 | SAVED="`pwd`" 59 | cd "`dirname \"$PRG\"`/" >/dev/null 60 | APP_HOME="`pwd -P`" 61 | cd "$SAVED" >/dev/null 62 | 63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 64 | 65 | # Determine the Java command to use to start the JVM. 66 | if [ -n "$JAVA_HOME" ] ; then 67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 68 | # IBM's JDK on AIX uses strange locations for the executables 69 | JAVACMD="$JAVA_HOME/jre/sh/java" 70 | else 71 | JAVACMD="$JAVA_HOME/bin/java" 72 | fi 73 | if [ ! -x "$JAVACMD" ] ; then 74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 75 | 76 | Please set the JAVA_HOME variable in your environment to match the 77 | location of your Java installation." 78 | fi 79 | else 80 | JAVACMD="java" 81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 82 | 83 | Please set the JAVA_HOME variable in your environment to match the 84 | location of your Java installation." 85 | fi 86 | 87 | # Increase the maximum file descriptors if we can. 88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 89 | MAX_FD_LIMIT=`ulimit -H -n` 90 | if [ $? -eq 0 ] ; then 91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 92 | MAX_FD="$MAX_FD_LIMIT" 93 | fi 94 | ulimit -n $MAX_FD 95 | if [ $? -ne 0 ] ; then 96 | warn "Could not set maximum file descriptor limit: $MAX_FD" 97 | fi 98 | else 99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 100 | fi 101 | fi 102 | 103 | # For Darwin, add options to specify how the application appears in the dock 104 | if $darwin; then 105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 106 | fi 107 | 108 | # For Cygwin, switch paths to Windows format before running java 109 | if $cygwin ; then 110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 112 | JAVACMD=`cygpath --unix "$JAVACMD"` 113 | 114 | # We build the pattern for arguments to be converted via cygpath 115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 116 | SEP="" 117 | for dir in $ROOTDIRSRAW ; do 118 | ROOTDIRS="$ROOTDIRS$SEP$dir" 119 | SEP="|" 120 | done 121 | OURCYGPATTERN="(^($ROOTDIRS))" 122 | # Add a user-defined pattern to the cygpath arguments 123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 125 | fi 126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 127 | i=0 128 | for arg in "$@" ; do 129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 131 | 132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 134 | else 135 | eval `echo args$i`="\"$arg\"" 136 | fi 137 | i=$((i+1)) 138 | done 139 | case $i in 140 | (0) set -- ;; 141 | (1) set -- "$args0" ;; 142 | (2) set -- "$args0" "$args1" ;; 143 | (3) set -- "$args0" "$args1" "$args2" ;; 144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 150 | esac 151 | fi 152 | 153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 154 | function splitJvmOpts() { 155 | JVM_OPTS=("$@") 156 | } 157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 159 | 160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 161 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':x264-android' 2 | -------------------------------------------------------------------------------- /x264-android/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /x264-android/bintrayv1.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.jfrog.bintray' 2 | 3 | version = libraryVersion 4 | 5 | if (project.hasProperty("android")) { // Android libraries 6 | task sourcesJar(type: Jar) { 7 | classifier = 'sources' 8 | from android.sourceSets.main.java.srcDirs 9 | } 10 | 11 | task javadoc(type: Javadoc) { 12 | source = android.sourceSets.main.java.srcDirs 13 | classpath += project.files(android.getBootClasspath().join(File.pathSeparator)) 14 | } 15 | } else { // Java libraries 16 | task sourcesJar(type: Jar, dependsOn: classes) { 17 | classifier = 'sources' 18 | from sourceSets.main.allSource 19 | } 20 | } 21 | 22 | task javadocJar(type: Jar, dependsOn: javadoc) { 23 | classifier = 'javadoc' 24 | from javadoc.destinationDir 25 | } 26 | 27 | artifacts { 28 | archives javadocJar 29 | archives sourcesJar 30 | } 31 | 32 | // Bintray 33 | Properties properties = new Properties() 34 | properties.load(project.rootProject.file('local.properties').newDataInputStream()) 35 | 36 | bintray { 37 | user = properties.getProperty("bintray.user") 38 | key = properties.getProperty("bintray.apikey") 39 | 40 | configurations = ['archives'] 41 | pkg { 42 | repo = bintrayRepo 43 | name = bintrayName 44 | desc = libraryDescription 45 | websiteUrl = siteUrl 46 | vcsUrl = gitUrl 47 | licenses = allLicenses 48 | publish = true 49 | publicDownloadNumbers = true 50 | version { 51 | desc = libraryDescription 52 | gpg { 53 | sign = true //Determines whether to GPG sign the files. The default is false 54 | passphrase = properties.getProperty("bintray.gpg.password") 55 | //Optional. The passphrase for GPG signing' 56 | } 57 | } 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /x264-android/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | 3 | ext { 4 | bintrayRepo = 'maven' 5 | bintrayName = 'x264-android' 6 | 7 | publishedGroupId = 'com.github.bakaoh' 8 | libraryName = 'X264Android' 9 | artifact = 'x264-android' 10 | 11 | libraryDescription = 'Simple x264 encoder jni for android' 12 | 13 | siteUrl = 'https://github.com/bakaoh/X264Android' 14 | gitUrl = 'https://github.com/bakaoh/X264Android.git' 15 | 16 | libraryVersion = '1.0.0' 17 | 18 | developerId = 'bakaoh' 19 | developerName = 'Tata Tata' 20 | developerEmail = 'tatattai@gmail.com' 21 | 22 | licenseName = 'The Apache Software License, Version 2.0' 23 | licenseUrl = 'http://www.apache.org/licenses/LICENSE-2.0.txt' 24 | allLicenses = ["Apache-2.0"] 25 | } 26 | 27 | android { 28 | compileSdkVersion 25 29 | buildToolsVersion "25.0.2" 30 | 31 | defaultConfig { 32 | minSdkVersion 15 33 | targetSdkVersion 25 34 | versionCode 1 35 | versionName "1.0" 36 | 37 | externalNativeBuild { 38 | ndkBuild { 39 | cppFlags "" 40 | } 41 | } 42 | ndk { 43 | abiFilters 'x86', 'armeabi-v7a', 'arm64-v8a' 44 | } 45 | } 46 | buildTypes { 47 | release { 48 | minifyEnabled false 49 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 50 | externalNativeBuild { 51 | ndkBuild { 52 | arguments "LOCAL_LDFLAGS:=-Wl,-s" 53 | } 54 | } 55 | } 56 | } 57 | externalNativeBuild { 58 | ndkBuild { 59 | path "src/main/cpp/Android.mk" 60 | } 61 | } 62 | } 63 | 64 | dependencies { 65 | compile fileTree(dir: 'libs', include: ['*.jar']) 66 | } 67 | 68 | apply from: 'installv1.gradle' 69 | apply from: 'bintrayv1.gradle' -------------------------------------------------------------------------------- /x264-android/installv1.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.github.dcendents.android-maven' 2 | 3 | group = publishedGroupId // Maven Group ID for the artifact 4 | 5 | install { 6 | repositories.mavenInstaller { 7 | // This generates POM.xml with proper parameters 8 | pom { 9 | project { 10 | packaging 'aar' 11 | groupId publishedGroupId 12 | artifactId artifact 13 | 14 | // Add your description here 15 | name libraryName 16 | description libraryDescription 17 | url siteUrl 18 | 19 | // Set your license 20 | licenses { 21 | license { 22 | name licenseName 23 | url licenseUrl 24 | } 25 | } 26 | developers { 27 | developer { 28 | id developerId 29 | name developerName 30 | email developerEmail 31 | } 32 | } 33 | scm { 34 | connection gitUrl 35 | developerConnection gitUrl 36 | url siteUrl 37 | 38 | } 39 | } 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /x264-android/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /home/taitt/Android/Sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /x264-android/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /x264-android/src/main/cpp/.gitignore: -------------------------------------------------------------------------------- 1 | /libx264 2 | /prebuilt 3 | -------------------------------------------------------------------------------- /x264-android/src/main/cpp/Android.mk: -------------------------------------------------------------------------------- 1 | LOCAL_PATH := $(call my-dir) 2 | 3 | MY_PREBUILT := $(LOCAL_PATH)/prebuilt/$(TARGET_ARCH_ABI) 4 | 5 | include $(CLEAR_VARS) 6 | LOCAL_MODULE := libx264 7 | LOCAL_SRC_FILES := $(MY_PREBUILT)/lib/$(LOCAL_MODULE).a 8 | include $(PREBUILT_STATIC_LIBRARY) 9 | 10 | include $(CLEAR_VARS) 11 | LOCAL_MODULE := x264a 12 | LOCAL_SRC_FILES := libx264_jni.cpp 13 | LOCAL_CFLAGS := 14 | LOCAL_LDLIBS := -llog 15 | LOCAL_C_INCLUDES := $(LOCAL_C_INCLUDES) $(MY_PREBUILT)/include 16 | LOCAL_STATIC_LIBRARIES := libx264 17 | LOCAL_DISABLE_FORMAT_STRING_CHECKS := true 18 | LOCAL_DISABLE_FATAL_LINKER_WARNINGS := true 19 | include $(BUILD_SHARED_LIBRARY) 20 | -------------------------------------------------------------------------------- /x264-android/src/main/cpp/Application.mk: -------------------------------------------------------------------------------- 1 | APP_STL := gnustl_static 2 | APP_ABI := x86 armeabi-v7a arm64-v8a 3 | APP_PLATFORM := android-9 4 | APP_OPTIM := release 5 | -------------------------------------------------------------------------------- /x264-android/src/main/cpp/build_x264.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -e 4 | 5 | ARM_PLATFORM=$NDK_PATH/platforms/android-9/arch-arm/ 6 | ARM_PREBUILT=$NDK_PATH/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64 7 | AARCH64_PLATFORM=$NDK_PATH/platforms/android-21/arch-arm64/ 8 | AARCH64_PREBUILT=$NDK_PATH/toolchains/aarch64-linux-android-4.9/prebuilt/linux-x86_64 9 | X86_PLATFORM=$NDK_PATH/platforms/android-9/arch-x86/ 10 | X86_PREBUILT=$NDK_PATH/toolchains/x86-4.9/prebuilt/linux-x86_64 11 | 12 | function build_one 13 | { 14 | if [ $ARCH == "arm" ] 15 | then 16 | SYSROOT=$ARM_PLATFORM 17 | PREBUILT=$ARM_PREBUILT 18 | CROSS_PREFIX=$PREBUILT/bin/arm-linux-androideabi- 19 | HOST=arm-linux 20 | elif [ $ARCH == "aarch64" ] 21 | then 22 | SYSROOT=$AARCH64_PLATFORM 23 | PREBUILT=$AARCH64_PREBUILT 24 | CROSS_PREFIX=$PREBUILT/bin/aarch64-linux-android- 25 | HOST=aarch64-linux 26 | else 27 | SYSROOT=$X86_PLATFORM 28 | PREBUILT=$X86_PREBUILT 29 | CROSS_PREFIX=$PREBUILT/bin/i686-linux-android- 30 | HOST=i686-linux 31 | fi 32 | 33 | pushd libx264 34 | ./configure --prefix=$PREFIX \ 35 | --host=$HOST \ 36 | --sysroot=$SYSROOT \ 37 | --cross-prefix=$CROSS_PREFIX \ 38 | --extra-cflags="$OPTIMIZE_CFLAGS" \ 39 | --extra-ldflags="-nostdlib" \ 40 | --enable-pic \ 41 | --enable-static \ 42 | --enable-strip \ 43 | --disable-cli \ 44 | --disable-win32thread \ 45 | --disable-avs \ 46 | --disable-swscale \ 47 | --disable-lavf \ 48 | --disable-ffms \ 49 | --disable-gpac \ 50 | --disable-lsmash 51 | 52 | make clean 53 | make -j4 install V=1 54 | popd 55 | } 56 | 57 | #arm v7vfpv3 58 | CPU=armv7-a 59 | ARCH=arm 60 | OPTIMIZE_CFLAGS="-mfloat-abi=softfp -mfpu=vfpv3-d16 -marm -march=$CPU " 61 | PREFIX=`pwd`/prebuilt/armeabi-v7a 62 | build_one 63 | 64 | #arm64-v8a 65 | CPU=arm64-v8a 66 | ARCH=aarch64 67 | OPTIMIZE_CFLAGS="-march=armv8-a" 68 | PREFIX=`pwd`/prebuilt/arm64-v8a 69 | build_one 70 | 71 | #x86 72 | CPU=i686 73 | ARCH=i686 74 | OPTIMIZE_CFLAGS= 75 | PREFIX=`pwd`/prebuilt/x86 76 | build_one 77 | -------------------------------------------------------------------------------- /x264-android/src/main/cpp/libx264_jni.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #define LOGE(...) ((void)__android_log_print(ANDROID_LOG_ERROR, "x264_jni", __VA_ARGS__)) 7 | 8 | #define X264A_OK 0 9 | #define X264A_ERR_APPLY_PROFILE -2 10 | #define X264A_ERR_OPEN_ENCODER -3 11 | #define X264A_ERR_NOT_SUPPORT_CPS -4 12 | #define X264A_ERR_ENCODE_FRAME -5 13 | #define X264A_PACKAGE "com/github/bakaoh/x264/" 14 | 15 | extern "C" 16 | 17 | typedef struct EncoderContext { 18 | x264_param_t params; 19 | x264_t *encoder; 20 | x264_picture_t input_picture; 21 | } EncoderContext; 22 | 23 | static void set_ctx(JNIEnv *env, jobject thiz, void *ctx) { 24 | jclass cls = env->GetObjectClass(thiz); 25 | jfieldID fid = env->GetFieldID(cls, "ctx", "J"); 26 | env->SetLongField(thiz, fid, (jlong) (uintptr_t) ctx); 27 | } 28 | 29 | static void *get_ctx(JNIEnv *env, jobject thiz) { 30 | jclass cls = env->GetObjectClass(thiz); 31 | jfieldID fid = env->GetFieldID(cls, "ctx", "J"); 32 | return (void *) (uintptr_t) env->GetLongField(thiz, fid); 33 | } 34 | 35 | /** 36 | * Create a new encoder. 37 | */ 38 | JNIEXPORT jobject initEncoder(JNIEnv *env, jobject thiz, jobject params) { 39 | EncoderContext *ctx = (EncoderContext *) calloc(1, sizeof(EncoderContext)); 40 | set_ctx(env, thiz, ctx); 41 | jclass rsCls = env->FindClass(X264A_PACKAGE"X264InitResult"); 42 | jmethodID rsInit = env->GetMethodID(rsCls, "", "(I[B[B)V"); 43 | 44 | // init params 45 | jclass paramsCls = env->GetObjectClass(params); 46 | 47 | jstring preset = (jstring) env->GetObjectField 48 | (params, env->GetFieldID(paramsCls, "preset", "Ljava/lang/String;")); 49 | const char *c_preset = env->GetStringUTFChars(preset, NULL); 50 | x264_param_default_preset(&ctx->params, c_preset, "zerolatency"); 51 | env->ReleaseStringUTFChars(preset, c_preset); 52 | 53 | ctx->params.i_width = env->GetIntField(params, env->GetFieldID(paramsCls, "width", "I")); 54 | ctx->params.i_height = env->GetIntField(params, env->GetFieldID(paramsCls, "height", "I")); 55 | ctx->params.rc.i_bitrate = 56 | env->GetIntField(params, env->GetFieldID(paramsCls, "bitrate", "I")) / 1000; 57 | ctx->params.rc.i_rc_method = X264_RC_ABR; 58 | ctx->params.i_fps_num = env->GetIntField(params, env->GetFieldID(paramsCls, "fps", "I")); 59 | ctx->params.i_fps_den = 1; 60 | ctx->params.i_keyint_max = env->GetIntField(params, env->GetFieldID(paramsCls, "gop", "I")); 61 | ctx->params.b_repeat_headers = 0; 62 | 63 | jstring profile = (jstring) env->GetObjectField 64 | (params, env->GetFieldID(paramsCls, "profile", "Ljava/lang/String;")); 65 | const char *c_profile = env->GetStringUTFChars(profile, NULL); 66 | int apply_profile = x264_param_apply_profile(&ctx->params, c_profile); 67 | env->ReleaseStringUTFChars(preset, c_profile); 68 | if (apply_profile < 0) 69 | return env->NewObject(rsCls, rsInit, X264A_ERR_APPLY_PROFILE, NULL, NULL); 70 | 71 | ctx->encoder = x264_encoder_open(&ctx->params); 72 | if (ctx->encoder == NULL) 73 | return env->NewObject(rsCls, rsInit, X264A_ERR_OPEN_ENCODER, NULL, NULL); 74 | 75 | // return the SPS and PPS that will be used for the whole stream 76 | int pi_nal; 77 | x264_nal_t *pp_nal; 78 | x264_encoder_headers(ctx->encoder, &pp_nal, &pi_nal); 79 | jbyteArray sps = env->NewByteArray(pp_nal[0].i_payload); 80 | env->SetByteArrayRegion(sps, 0, pp_nal[0].i_payload, (jbyte *) pp_nal[0].p_payload); 81 | jbyteArray pps = env->NewByteArray(pp_nal[1].i_payload); 82 | env->SetByteArrayRegion(pps, 0, pp_nal[1].i_payload, (jbyte *) pp_nal[1].p_payload); 83 | 84 | return env->NewObject(rsCls, rsInit, X264A_OK, sps, pps); 85 | } 86 | 87 | /** 88 | * Free up resources used by the encoder instance. 89 | * Make sure to call this even if initEncoder fail. 90 | */ 91 | JNIEXPORT void releaseEncoder(JNIEnv *env, jobject thiz) { 92 | EncoderContext *ctx = (EncoderContext *) get_ctx(env, thiz); 93 | 94 | int nnal; 95 | x264_nal_t *nal; 96 | x264_picture_t pic_out; 97 | 98 | if (ctx->encoder != NULL) { 99 | while (x264_encoder_delayed_frames(ctx->encoder)) { 100 | x264_encoder_encode(ctx->encoder, &nal, &nnal, NULL, &pic_out); 101 | } 102 | x264_encoder_close(ctx->encoder); 103 | ctx->encoder = NULL; 104 | } 105 | free(ctx); 106 | } 107 | 108 | /** 109 | * Encode one frame. 110 | **/ 111 | JNIEXPORT jobject encodeFrame(JNIEnv *env, jobject thiz, jbyteArray frame, jint csp, jlong pts) { 112 | EncoderContext *ctx = (EncoderContext *) get_ctx(env, thiz); 113 | jclass rsCls = env->FindClass(X264A_PACKAGE"X264EncodeResult"); 114 | jmethodID rsInit = env->GetMethodID(rsCls, "", "(I[BJZ)V"); 115 | 116 | jbyte *input_frame = env->GetByteArrayElements(frame, NULL); 117 | 118 | // encode frame 119 | int nnal; 120 | x264_nal_t *nal; 121 | x264_picture_t out_pic; 122 | int y_size = ctx->params.i_width * ctx->params.i_height; 123 | 124 | ctx->input_picture.img.i_csp = csp; 125 | ctx->input_picture.i_pts = pts; 126 | ctx->input_picture.i_type = X264_TYPE_AUTO; 127 | ctx->input_picture.img.plane[0] = (uint8_t *) input_frame; 128 | ctx->input_picture.img.i_stride[0] = ctx->params.i_width; 129 | switch (csp) { 130 | case X264_CSP_NV21: 131 | case X264_CSP_NV12: 132 | ctx->input_picture.img.i_plane = 2; 133 | ctx->input_picture.img.plane[1] = ctx->input_picture.img.plane[0] + y_size; 134 | ctx->input_picture.img.i_stride[1] = ctx->params.i_width; 135 | break; 136 | case X264_CSP_I420: 137 | case X264_CSP_YV12: 138 | ctx->input_picture.img.i_plane = 3; 139 | ctx->input_picture.img.plane[1] = ctx->input_picture.img.plane[0] + y_size; 140 | ctx->input_picture.img.i_stride[1] = ctx->params.i_width / 2; 141 | ctx->input_picture.img.plane[2] = ctx->input_picture.img.plane[1] + y_size / 4; 142 | ctx->input_picture.img.i_stride[2] = ctx->params.i_width / 2; 143 | break; 144 | default: 145 | return env->NewObject(rsCls, rsInit, X264A_ERR_NOT_SUPPORT_CPS, NULL, 0, false); 146 | } 147 | 148 | int len = x264_encoder_encode(ctx->encoder, &nal, &nnal, &ctx->input_picture, &out_pic); 149 | if (len < 0) return env->NewObject(rsCls, rsInit, X264A_ERR_ENCODE_FRAME, NULL, 0, false); 150 | 151 | // return encoded data 152 | jbyteArray output_frame = env->NewByteArray(len); 153 | env->SetByteArrayRegion(output_frame, 0, len, (jbyte *) nal[0].p_payload); 154 | 155 | env->ReleaseByteArrayElements(frame, input_frame, JNI_ABORT); 156 | return env->NewObject(rsCls, rsInit, 157 | X264A_OK, output_frame, out_pic.i_pts, out_pic.i_type == X264_TYPE_IDR); 158 | } 159 | 160 | JNIEXPORT jstring getVersion(JNIEnv *env, jobject thiz) { 161 | return env->NewStringUTF("1.3.0"); 162 | } 163 | 164 | static JNINativeMethod methods[] = { 165 | {"initEncoder", "(L"X264A_PACKAGE"X264Params;)L"X264A_PACKAGE"X264InitResult;", (void *) initEncoder}, 166 | {"releaseEncoder", "()V", (void *) releaseEncoder}, 167 | {"encodeFrame", "([BIJ)L"X264A_PACKAGE"X264EncodeResult;", (void *) encodeFrame}, 168 | {"getVersion", "()Ljava/lang/String;", (void *) getVersion}, 169 | }; 170 | 171 | JNIEXPORT jint JNI_OnLoad(JavaVM *vm, void *reserved) { 172 | JNIEnv *env; 173 | 174 | if (vm->GetEnv((void **) &env, JNI_VERSION_1_6) != JNI_OK) { 175 | LOGE("JNI_OnLoad GetEnv error."); 176 | return JNI_ERR; 177 | } 178 | 179 | jclass clz = env->FindClass(X264A_PACKAGE"X264Encoder"); 180 | if (clz == NULL) { 181 | LOGE("JNI_OnLoad FindClass error."); 182 | return JNI_ERR; 183 | } 184 | 185 | if (env->RegisterNatives(clz, methods, sizeof(methods) / sizeof(methods[0]))) { 186 | LOGE("JNI_OnLoad RegisterNatives error."); 187 | return JNI_ERR; 188 | } 189 | 190 | return JNI_VERSION_1_6; 191 | } -------------------------------------------------------------------------------- /x264-android/src/main/java/com/github/bakaoh/x264/X264EncodeResult.java: -------------------------------------------------------------------------------- 1 | package com.github.bakaoh.x264; 2 | 3 | /** 4 | * Created by taitt on 12/01/2017. 5 | */ 6 | 7 | public class X264EncodeResult { 8 | 9 | public int err; 10 | public byte[] data; 11 | public long pts; 12 | public boolean isKey; 13 | 14 | public X264EncodeResult(int err, byte[] data, long pts, boolean isKey) { 15 | this.err = err; 16 | this.data = data; 17 | this.pts = pts; 18 | this.isKey = isKey; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /x264-android/src/main/java/com/github/bakaoh/x264/X264Encoder.java: -------------------------------------------------------------------------------- 1 | package com.github.bakaoh.x264; 2 | 3 | /** 4 | * Created by taitt on 09/01/2017. 5 | */ 6 | 7 | public class X264Encoder { 8 | 9 | public native X264InitResult initEncoder(X264Params params); 10 | 11 | public native void releaseEncoder(); 12 | 13 | public native X264EncodeResult encodeFrame(byte[] frame, int colorFormat, long pts); 14 | 15 | public native String getVersion(); 16 | 17 | private long ctx; 18 | 19 | static { 20 | System.loadLibrary("x264a"); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /x264-android/src/main/java/com/github/bakaoh/x264/X264InitResult.java: -------------------------------------------------------------------------------- 1 | package com.github.bakaoh.x264; 2 | 3 | /** 4 | * Created by taitt on 12/01/2017. 5 | */ 6 | 7 | public class X264InitResult { 8 | 9 | public int err; 10 | public byte[] sps; 11 | public byte[] pps; 12 | 13 | public X264InitResult(int err, byte[] sps, byte[] pps) { 14 | this.err = err; 15 | this.sps = sps; 16 | this.pps = pps; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /x264-android/src/main/java/com/github/bakaoh/x264/X264Params.java: -------------------------------------------------------------------------------- 1 | package com.github.bakaoh.x264; 2 | 3 | /** 4 | * Created by taitt on 10/01/2017. 5 | */ 6 | 7 | public class X264Params { 8 | 9 | public static final int CSP_I420 = 0x0001; // yuv 4:2:0 planar 10 | public static final int CSP_YV12 = 0x0002; // yvu 4:2:0 planar 11 | public static final int CSP_NV12 = 0x0003; // yuv 4:2:0, with one y plane and one packed u+v 12 | public static final int CSP_NV21 = 0x0004; // yuv 4:2:0, with one y plane and one packed v+u 13 | public int width = 1280; 14 | public int height = 720; 15 | public int bitrate = 500; 16 | public int fps = 24; 17 | public int gop = 48; 18 | public String profile = "baseline"; 19 | public String preset = "ultrafast"; 20 | } 21 | -------------------------------------------------------------------------------- /x264-android/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | x264 3 | 4 | --------------------------------------------------------------------------------