To celebrate the success of Android video collection + h264 Encoding

Source: Internet
Author: User

The encoder uses the x264 open-source library,

 

Easy to understand

A jni library is encapsulated

 

The csdn resources of the encoding library in BBS are too difficult to use.

Http://www.eoeandroid.com/forum.php? MoD = viewthread & tid = 52739 & extra =

 

Compilation and release method of x264

 

Export arm_root = $ android_ndk_root
Export arm_inc = $ arm_root/build/platforms/Android-5/arch-arm/usr/include/
Export arm_lib = $ arm_root/build/platforms/Android-5/arch-arm/usr/lib/
Export arm_tool = $ arm_root/build/prebuilt/Windows/arm-eabi-4.4.0.
Export arm_libo = $ arm_tool/lib/GCC/ARM-Eabi/4.4.0
Export Path = $ arm_tool/bin: $ path
Export arm_pre = arm-Eabi

./Configure -- prefix =/home/egmkang/libx264 -- enable-shared/
-Disable-ASM -- Host = arm-Linux -- Cross-Prefix = arm-Eabi -/
-- Extra-cflags = "-I $ arm_inc-FPIC-dandroid-FPIC-mthumb-interwork-ffunction-sections-funwind-tables-fstack-protector-fno-short-enums-d__arm_arch_5 _-blank _-d1_arm_arch_5e _-d1_arm_arch_5te _-wno-psabi-March = armv5te-mtune = XScale-msoft-float-mthumb-OS-fomit-frame-pointer -fno-strict-aliasing-Finline-Limit = 64-dandroid-wa, -- noexecstack-MMD-MP "/
-- Extra-ldflags = "-nostdlib-bdynamic-wl, -- no-undefined-wl,-Z, noexecstack-wl,-Z, nocopyreloc-wl,-soname, /system/lib/libz. so-wl,-rpath-link = $ arm_lib, -Dynamic-linker =/system/bin/linker-L $ arm_lib-nostdlib $ arm_lib/crtbegin_dynamic.o $ arm_lib/crtend_android.o-LC-lm-LDL-lgcc"

Here the static library x264 is generated.

 

The only trouble for the entire project is that an error is reported when a JNI dynamic library is generated ..

 

Later, I found that a library is missing,

Therefore, you can add some configuration in Android. mk according to the compilation method of x264. Of course, this is a difficult issue. I found a lot on the Internet and found no results.

 

Modify the parameters of some directories.

I put the previously generated libx264.a and x264.h files in the libx264 directory of JNI. If you have any questions, please adjust them.

 

Local_path: = $ (call my-DIR)

Include $ (clear_vars)
Local_c_includes + = $ (local_path)/libx264/include
Local_module: = hsf-android
Local_src_files: = hsf-android. c
Local_ldflags + = $ (local_path)/libx264/lib/libx264.a
Local_ldlibs: =-L $ (sysroot)/usr/lib-lgcc


Include $ (build_shared_library)

 

 

 

It is estimated that many people will find that they are very familiar with this.

Http://www.cnblogs.com/mcodec/articles/1780598.html

The file name has not been changed !! Relatively lazy

 

 

 

In addition, the encoding efficiency is very low.

 

 

Androidvideo. Java

Import Java. io. file; <br/> Import Java. io. randomaccessfile; <br/> Import android. app. activity; <br/> Import android. content. intent; <br/> Import android. OS. bundle; <br/> Import android. view. view; <br/> Import android. content. res. configuration; <br/> Import android. OS. bundle; <br/> Import android. util. log; <br/> Import android. view. surfaceholder; <br/> Import android. view. surfaceview; <br/> Import android. view. Window; <br/> Import android. view. windowmanager; <br/> Import android. view. surfaceholder. callback; <br/> Import android. graphics. pixelformat; <br/> Import android. hardware. camera; <br/> public class androidvideo extends activity implements callback, <br/> camera. picturecallback {<br/> private surfaceview msurfaceview = NULL; <br/> private surfaceholder msurfaceholder = NULL; <br/> private camera mcamera = NULL; <br/> private Boolean mpreviewrunning = false; <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> getwindow (). setformat (pixelformat. translucent); <br/> requestwindowfeature (window. feature_no_title); <br/> getwindow (). setflags (windowmanager. layoutparams. flag_fullscreen, <br/> windowmanager. layoutparams. flag_fullscreen); <br/> setcontentview (R. lay Out. camera); <br/> msurfaceview = (surfaceview) This. findviewbyid (R. id. surface_camera); <br/> msurfaceholder = msurfaceview. getholder (); <br/> msurfaceholder. addcallback (this); <br/> msurfaceholder. settype (surfaceholder. surface_type_push_buffers); <br/>}</P> <p> @ override <br/> Public void onpicturetaken (byte [] data, camera) {<br/>}< br/> @ override <br/> Public void surfacechanged (surfaceholder Ho Lder, int format, int width, <br/> int height) {<br/> If (mpreviewrunning) {<br/> mcamera. stoppreview (); <br/>}< br/> camera. parameters P = mcamera. getparameters (); <br/> P. setpreviewsize (352,288); <br/> mcamera. setpreviewcallback (New hsf-encoder (352,288); <br/> mcamera. setparameters (p); <br/> try {<br/> mcamera. setpreviewdisplay (holder); <br/>}catch (exception ex) {<br/>}< br/> mcamera. startpreview (); <Br/> mpreviewrunning = true; <br/>}< br/> @ override <br/> Public void surfacecreated (surfaceholder holder) {<br/> mcamera = camera. open (); <br/>}< br/> @ override <br/> Public void surfacedestroyed (surfaceholder holder) {<br/> If (mcamera! = NULL) {<br/> mcamera. setpreviewcallback (null); <br/> mcamera. stoppreview (); <br/> mpreviewrunning = false; <br/> mcamera. release (); <br/> mcamera = NULL; <br/>}< br/> Public void onconfigurationchanged (configuration newconfig) {<br/> try {<br/> super. onconfigurationchanged (newconfig); <br/> If (this. getresources (). getconfiguration (). orientation = configuration. orientation_landscape) {<br/>} el Se if (this. getresources (). getconfiguration (). orientation = configuration. orientation_portrait) {<br/>}< br/>} catch (exception ex) {<br/>}< br/> class hsf-encoder implements camera. previewcallback {<br/> long encoder = 0; <br/> randomaccessfile RAF = NULL; <br/> byte [] hsf-buff = NULL; <br/> static {<br/> system. loadlibrary ("hsf-android"); <br/>}< br/> private hsf-encoder () {}; </P> <p> Public Hsf-encoder (INT width, int height) {<br/> encoder = compressbegin (width, height); <br/> hsf-buff = new byte [width * height * 8]; <br/> try {<br/> file = new file ("/sdcard/camera. h264 "); <br/> RAF = new randomaccessfile (file," RW "); <br/>} catch (exception ex) {<br/> log. V ("system. out ", Ex. tostring (); <br/>}</P> <p >}; <br/> protected void finalize () <br/>{< br/> compressend (encoder); <br/> If (null! = RAF) <br/>{< br/> try {<br/> Raf. close (); <br/>}catch (exception ex) {<br/> log. V ("system. out ", Ex. tostring (); <br/>}< br/> try {<br/> super. finalize (); <br/>}catch (throwable e) {<br/> // todo auto-generated Catch Block <br/> E. printstacktrace (); <br/>}< br/> private native long compressbegin (INT width, int height ); <br/> private native int compressbuffer (long encoder, int type, byte [] In, int insize, byte [] Out ); <br/> private native int compressend (long encoder); <br/> @ override <br/> Public void onpreviewframe (byte [] data, camera) {</P> <p> int result = compressbuffer (encoder,-1, data, data. length, hsf-buff); <br/> try {<br/> If (result> 0) <br/> Raf. write (hsf-buff, 0, result); <br/>} catch (exception ex) {<br/> log. V ("system. out ", Ex. tostring (); <br/>}</P> <p>}

 

 

Hsf-android. c

# Include <string. h> <br/> # include <JNI. h> <br/> # include <stdio. h> <br/> # include <stdlib. h> <br/> # include <ARPA/inet. h> <br/> # include <x264.h> <br/> # define data_max 3000000 <br/> # define hsf-_mtu 1024 <br/> typestrudef CT <br/> {<br /> x1__param_t * Param; <br/> x264_t * handle; <br/> x264_picture_t * picture; <br/> x264_nal_t * nal; <br/>} encoder; <br/> jlong java_hsf-_com_hsf-encoder_compressbegin (jnienv * ENV, jobject thiz, <br/> jint width, jint height) {<br/> encoder * en = (encoder *) malloc (sizeof (encoder); <br/> en-> param = (x1__param_t *) malloc (sizeof (x1__param_t )); <br/> en-> picture = (x264_param_t *) malloc (sizeof (x264_picture_t); <br/> x264_param_default (En-> param ); // set default Param <br/> // en-> param-> RC. I _rc_method = x264_rc_cqp; <br/> en-> param-> I _log_level = x1__log_none; <br/> en-> param-> I _width = width; // set frame width <br/> en-> param-> I _height = height; // set frame height <br/> en-> param-> RC. I _lookahead = 0; <br/> en-> param-> I _bframe = 0; <br/> en-> param-> I _fps_num = 5; <br/> en-> param-> I _fps_den = 1; <br/> If (En-> handle = x264_encoder_open (En-> param) = 0) {<br/> return 0; <br/>}< br/>/* Create a new pic */<br/> x1__picture_alloc (En-> picture, x1__csp_i420, en-> param-> I _width, <br/> en-> param-> I _height); <br/> return (jlong) en; <br/>}< br/> jint java_h1__com_h1_encoder_compressend (jnienv * ENV, jobject thiz, jlong handle) <br/>{< br/> encoder * en = (encoder *) handle; <br/> If (En-> picture) <br/>{< br/> x__picture_clean (En-> picture ); <br/> free (En-> picture); <br/> en-> picture = 0; <br/>}< br/> If (En-> param) <br/>{< br/> free (En-> param); <br/> en-> param = 0; <br/>}< br/> If (En-> handle) <br/>{< br/> x1__encoder_close (En-> handle ); <br/>}< br/> free (en); <br/> return 0; <br/>}< br/> jint java_h1__com_h1_encoder_compressbuffer (jnienv * ENV, jobject thiz, jlong handle, jint type, jbytearray in, jint insize, jbytearray out) <br/>{< br/> encoder * en = (encoder *) handle; <br/> x264_picture_t pic_out; <br/> int I _data = 0; <br/> int NNAL =-1; <br/> int result = 0; <br/> int I = 0, j = 0; <br/> int npix = 0; <br/> jbyte * Buf = (jbyte *) (* env) -> getbytearrayelements (ENV, In, 0); <br/> jbyte * hsf-buf = (jbyte *) (* env)-> getbytearrayelements (ENV, out, 0 ); <br/> unsigned char * ptmpout = hsf-buf; <br/> int npicsize = en-> param-> I _width * En-> param-> I _height; <br/>/* <br/> Y data is all taken from the same block, UV data is stored in interleave mode <br/> yyyy <br/> uvuv <br/> */<br/> jbyte * Y = en-> picture-> IMG. plane [0]; <br/> jbyte * V = en-> picture-> IMG. plane [1]; <br/> jbyte * u = en-> picture-> IMG. plane [2]; <br/> memcpy (En-> picture-> IMG. plane [0], Buf, npicsize); <br/> for (I = 0; I <npicsize/4; I ++) <br/> {<br/> * (U + I) = * (BUF + npicsize + I * 2); <br/> * (V + I) = * (BUF + npicsize + I * 2 + 1); <br/>}< br/> switch (type) <br/>{< br/> case 0: <br/> en-> picture-> I _type = x1__type_p; <br/> break; <br/> case 1: <br/> en-> picture-> I _type = x1__type_idr; <br/> break; <br/> case 2: <br/> en-> picture-> I _type = x1__type_ I; <br/> break; <br/> default: <br/> en-> picture-> I _type = x1__type_auto; <br/> break; <br/>}< br/> If (x1__encoder_encode (En-> handle, & (En-> NAL), & NNAL, en-> picture, & pic_out) <0) <br/>{< br/> return-1; <br/>}< br/> for (I = 0; I <NNAL; I ++) {<br/> memcpy (ptmpout, en-> nal [I]. p_payload, en-> nal [I]. I _payload); <br/> ptmpout + = en-> nal [I]. I _payload; <br/> result + = en-> nal [I]. I _payload; <br/>}< br/> return result; <br/>}< br/>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.