[x264-devel] newbie: how to do x264 encoding on android?

Lucas Zhang zhangqing1987 at gmail.com
Fri Aug 19 07:50:14 CEST 2011


* I just read some comments of x264.h** *
*
*

/* Optional low-level callback for low-latency encoding.  Called for each
output NAL unit
     * immediately after the NAL unit is finished encoding.  This allows the
calling application
     * to begin processing video data (e.g. by sending packets over a
network) before the frame
     * is done encoding.
     *
     * This callback MUST do the following in order to work correctly:
     * 1) Have available an output buffer of at least size
nal->i_payload*3/2 + 5 + 16.
     * 2) Call x264_nal_encode( h, dst, nal ), where dst is the output
buffer.
     * After these steps, the content of nal is valid and can be used in the
same way as if
     * the NAL unit were output by x264_encoder_encode.
     *
     * This does not need to be synchronous with the encoding process: the
data pointed to
     * by nal (both before and after x264_nal_encode) will remain valid
until the next
     * x264_encoder_encode call.  The callback must be re-entrant.
     *
     * This callback does not work with frame-based threads; threads must be
disabled
     * or sliced-threads enabled.  This callback also does not work as one
would expect
     * with HRD -- since the buffering period SEI cannot be calculated until
the frame
     * is finished encoding, it will not be sent via this callback.
     *
     * Note also that the NALs are not necessarily returned in order when
sliced threads is
     * enabled.  Accordingly, the variable i_first_mb and i_last_mb are
available in
     * x264_nal_t to help the calling application reorder the slices if
necessary.
     *
     * When this callback is enabled, x264_encoder_encode does not return
valid NALs;
     * the calling application is expected to acquire all output NALs
through the callback.
     *
     * It is generally sensible to combine this callback with a use of
slice-max-mbs or
     * slice-max-size. */


*Is it means x264_nal_encode must be called and then keep calling
x264_encoder_encode until the end?
And I have found a example but without x264_nal_encode, which is not working
well [image: :-\] Is there some tutorial about how to use these function?
Thank you!** *
*
*

typedef struct
{
x264_param_t * param;
    x264_t *handle;
    x264_picture_t * picture;
    x264_nal_t  *nal;
} Encoder;


//====================================================

jlong Java_h264_com_H264Encoder_CompressBegin(JNIEnv* env, jobject thiz,
 jint width, jint height) {
Encoder * en = (Encoder *) malloc(sizeof(Encoder));
en->param = (x264_param_t *) malloc(sizeof(x264_param_t));
en->picture = (x264_param_t *) malloc(sizeof(x264_picture_t));
x264_param_default(en->param); //set default param
//x264_param_apply_profile(en->param,"baseline");
//en->param->rc.i_rc_method = X264_RC_CQP;
en->param->i_log_level = X264_LOG_NONE;
en->param->i_width = width; //set frame width
en->param->i_height = height; //set frame height
en->param->rc.i_lookahead =0;

en->param->i_fps_num =15;
en->param->i_fps_den = 1;
x264_param_apply_profile(en->param,"baseline");
if ((en->handle = x264_encoder_open(en->param)) == 0) {
 return 0;
}
/* Create a new pic */
x264_picture_alloc(en->picture, X264_CSP_I420, en->param->i_width,
 en->param->i_height);
return (jlong) en;
}

jint Java_h264_com_H264Encoder_CompressEnd(JNIEnv* env, jobject thiz,jlong
handle)
{
Encoder * en = (Encoder *) handle;
if(en->picture)
{
 x264_picture_clean(en->picture);
 free(en->picture);
 en->picture = 0;
}
if(en->param)
{
 free(en->param);
 en->param=0;
}
if(en->handle)
{
 x264_encoder_close(en->handle);
}
free(en);
return 0;
}
jint Java_h264_com_H264Encoder_CompressBuffer(JNIEnv* env, jobject
thiz,jlong handle,jint type,jbyteArray in, jint insize,jbyteArray out)
{
Encoder * en = (Encoder *) handle;
x264_picture_t pic_out;


    int i_data=0;
int nNal=-1;
int result=0;
int i=0,j=0;
int nPix=0;

jbyte * Buf = (jbyte*)(*env)->GetByteArrayElements(env, in, 0);
jbyte * h264Buf = (jbyte*)(*env)->GetByteArrayElements(env, out, 0);
unsigned char * pTmpOut = h264Buf;


int nPicSize=en->param->i_width*en->param->i_height;
/*
YYYY
YYYY
UVUV
 */
jbyte * y=en->picture->img.plane[0];
jbyte * v=en->picture->img.plane[1];
jbyte * u=en->picture->img.plane[2];
memcpy(en->picture->img.plane[0],Buf,nPicSize);
for (i=0;i<nPicSize/4;i++)
{
 *(u+i)=*(Buf+nPicSize+i*2);
 *(v+i)=*(Buf+nPicSize+i*2+1);
}

switch (type)
{
case 0:
 en->picture->i_type = X264_TYPE_P;
 break;
case 1:
 en->picture->i_type = X264_TYPE_IDR;
 break;
case 2:
 en->picture->i_type = X264_TYPE_I;
 break;
default:
 en->picture->i_type = X264_TYPE_AUTO;
 break;
}


    if( x264_encoder_encode( en->handle, &(en->nal), &nNal, en->picture
,&pic_out) < 0 )
    {
        return -1;
    }
    for (i = 0; i < nNal; i++){
          memcpy(pTmpOut, en->nal[i].p_payload, en->nal[i].i_payload);
          pTmpOut += en->nal[i].i_payload;
          result+=en->nal[i].i_payload;
    }
return result;
}


On Thu, Aug 18, 2011 at 10:02 PM, Lucas Zhang <zhangqing1987 at gmail.com>wrote:

> Hi all,
>
>     I plan to video capture x264 encoding on android/ios/symbian, and first
> begin at android. I have finished compiling the x264 lib for android, but
> cannot find any more useful documents about how to use x264 to encode the
> video capture on android. I know something about jni, but don't know how to
> call and use the method of x264 lib to finish the encoding. I will be
> grateful if you give me an example or something useful :-) Thank you!
>
> --
> Zhang Qing
>



-- 
Zhang Qing
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.videolan.org/pipermail/x264-devel/attachments/20110819/c00188c2/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: x264.h
Type: text/x-chdr
Size: 39118 bytes
Desc: not available
URL: <http://mailman.videolan.org/pipermail/x264-devel/attachments/20110819/c00188c2/attachment-0001.h>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: H264Android.c
Type: text/x-csrc
Size: 3656 bytes
Desc: not available
URL: <http://mailman.videolan.org/pipermail/x264-devel/attachments/20110819/c00188c2/attachment-0001.c>


More information about the x264-devel mailing list