android4.0 USB Camera Instance (vi) FFMPEG MPEG encoding

Source: Internet
Author: User

In front of this is to do h264 coding study for two days found in FFmpeg h264 code seems to rely on third-party library x264 or how simple how to come to the whole of a MPEG code ffmpeg transplant Front I have a ffmpeg decoding has given a specific link in this http:/ /blog.csdn.net/hclydao/article/details/18546757

How to use that inside also already said here is mainly through the ffmpeg to convert the yuv422 format to RGB and then yuv422 to MPEG format after the first few obtained to the yuv422 data in order to be able to display the data interface function is converted to rgb565 as follows

/** YUV to Rgb*/jniexport jint jnicall Java_com_hclydao_webcam_ffmpeg_yuvtorgb (jnienv * env, jclass obj,const JbyteArray y Uvdata, Jbytearray rgbdata,const jint dwidth,const jint dheight) {jbyte *ydata = (jbyte*) (*env)->getbytearrayelements (env, yuvdata, 0); Jbyte *rdata = (jbyte*) (*env)->getbytearrayelements (env, rgbdata, 0); Avframe * RPICTURE=NULL; Avframe * ypicture=null;struct Swscontext *swsctx = Null;rpicture=avcodec_alloc_frame (); ypicture=avcodec_alloc_frame (); Avpicture_fill ((Avpicture *) rpicture, (uint8_t *) rdata, pix_fmt_rgb565,dwidth,dheight); Avpicture_fill (( Avpicture *) Ypicture, (uint8_t *) Ydata, av_pix_fmt_yuyv422,mwidth,mheight); swsctx = Sws_getcontext (Mwidth,mheight, AV _pix_fmt_yuyv422,dwidth, dheight,pix_fmt_rgb565, sws_bicubic, NULL, NULL, NULL), Sws_scale (Swsctx, (const uint8_t* const*) ypicture->data,ypicture->linesize,0,mheight,rpicture->data,rpicture->linesize); Sws_ Freecontext (SWSCTX); Av_free (rpicture); Av_free (ypicture);(*env)->releasebytearrayelemenTS (env, Yuvdata, Ydata, 0);(*env)->releasebytearrayelements (env, Rgbdata, rdata, 0); return 0;} 

Then is the MPEG encoded online said ffmpeg can only be yuv420p encoding, so we have to first turn yuv422 into yuv420p after the encoding related interface functions as follows

Avcodeccontext *pcodecctx= NULL; Avpacket avpkt; FILE * video_file;unsigned char *outbuf=null;unsigned char *yuv420buf=null;static int outsize=0;/** encording init*/ Jniexport jint jnicall java_com_hclydao_webcam_ffmpeg_videoinit (jnienv * env, jclass obj,jbytearray filename) {LOGI ("%s \ n ", __func__); Avcodec * Pcodec=null;avcodec_register_all ();p codec=avcodec_find_encoder (Av_codec_id_mpeg1video); if (PCodec = = NULL     {LOGE ("++++++++++++codec not found\n"); return-1;}    PCODECCTX=AVCODEC_ALLOC_CONTEXT3 (PCODEC);        if (Pcodecctx = = NULL) {LOGE ("++++++could not allocate video codec context\n");    return-1;    }/* Put sample parameters */pcodecctx->bit_rate = 400000;    /* Resolution must be a multiple of both */pcodecctx->width = mwidth;    Pcodecctx->height = Mheight;    /* Frames per second */pcodecctx->time_base= (avrational) {1,25}; Pcodecctx->gop_size = 10;    /* Emit one intra frame every ten frames */pcodecctx->max_b_frames=1; pcodecctx-&GT;PIX_FMT = av_pix_fmt_yuv420p;//av_pix_fmt_yuyv422;        /* OPEN it */if (Avcodec_open2 (Pcodecctx, Pcodec, NULL) < 0) {LOGE ("+++++++could not open codec\n");    return-1; }outsize = mwidth * mheight*2;outbuf = malloc (outsize*sizeof (char)); yuv420buf = malloc (outsize*sizeof (char)); Jbyte *  Filedir = (jbyte*) (*env)->getbytearrayelements (env, filename, 0);    if (Video_file = fopen (Filedir, "wb") = = = NULL) {LOGE ("++++++++++++open%s failed\n", Filedir);  return-1; } (*ENV)->releasebytearrayelements (env, filename, filedir, 0); return 1;} Jniexport jint jnicall Java_com_hclydao_webcam_ffmpeg_videostart (jnienv * env, Jclass Obj,jbytearray yuvdata) {int Framefinished=0,size=0;jbyte *ydata = (jbyte*) (*env)->getbytearrayelements (env, yuvdata, 0); Avframe * YUV420PFRAME=NULL; Avframe * yuv422frame=null;struct Swscontext *swsctx = Null;yuv420pframe=avcodec_alloc_frame (); yuv422frame=avcodec_ Alloc_frame (); Avpicture_fill ((Avpicture *) Yuv420pframe, (uint8_t *) Yuv420buf, Av_pix_fmt_yuv420p,mwidth,mheight); Avpicture_fill ((Avpicture *) Yuv422frame, (uint8_t *) Ydata, AV_PIX_FMT_YUYV422, Mwidth,mheight); swsctx = Sws_getcontext (Mwidth,mheight, Av_pix_fmt_yuyv422,mwidth, mheight,av_pix_fmt_yuv420p, SWS_ Bicubic, NULL, NULL, NULL), Sws_scale (Swsctx, (const uint8_t* const*) yuv422frame->data,yuv422frame->linesize,0, mheight,yuv420pframe->data,yuv420pframe->linesize); Av_init_packet (&avpkt); size = Avcodec_encode_video2    (Pcodecctx, &avpkt, Yuv420pframe, &framefinished);        if (Size < 0) {LOGE ("+++++error encoding frame\n");    return-1; }if (framefinished) fwrite (avpkt.data,avpkt.size,1,video_file); Av_free_packet (&AMP;AVPKT); Sws_freecontext (Swsctx ); Av_free (yuv420pframe); Av_free (yuv422frame);(*env)->releasebytearrayelements (env, Yuvdata, Ydata, 0);} Jniexport jint jnicall java_com_hclydao_webcam_ffmpeg_videoclose (jnienv * env, Jclass obj) {fclose (video_file); Avcodec _close (PCODECCTX); Av_free (PCODECCTX); free (outbuf);}
The last recorded video is can be played with the total sense of the code as if the error is not always the process and the principle of the other easy to understand

Here's what I taped to this camera.

This test, I've re-built another project.

The entire project download link please go to my resources. I uploaded it, and it didn't show up. That's about 20 m.

This bug a lot of not how serious to write if the principle and the process of the hope that people point out


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.