Here reprinted an article, is through V4L2 to read the UVC camera video, With x264 code library real-time coding H.264 video method, the author of the source code is missing a part of the YUYV image format transcoding for yuv420p, but the author's article provides a yuyv2yuv420p algorithm.
Reprint if there is any improper place, please contact me, thank you.
The following are reproduced content:
In our video capture transmission equipment, first through the camera to collect color data to form a picture, that is, we often say a frame. Data formats can be YUV data or RGB data, and can be converted between them. The video we see is actually made up of a frame-by-frame image at a speed of 25 frames/sec, the film "Billiern in the middle of the war," the use of 120 frames/sec technology. If you encode the color of the camera directly into a video, the bandwidth required for the video is very, very high. 300,000 megapixel camera YUV420 format to calculate a frame of data size = long * wide * 1.5 = 640 * 1.5/1024 = 450 k, the video stream will be 450 k * 25/1024 = 9.88m/s. In order to make the video transmission more fluent, now the video is used in compression coding. Here we will introduce the use of the X264 encoder to encode YUV420 data into H264 format video. YUV Data acquisition has been introduced in the previous:
V4l2 video capture and H264 coding 1-V4L2 collect JPEG data
V4l2 video capture and H264 code 2-V4L2 collect YUV data
V4l2 video capture and H264 coding 3-x264 transplantation
Now directly on the code in this chapter, which is the code part:[OBJC] View Plain copy print? /*============================================================================= # FileName: h264encoder.c # desc: this program aim to get image from USB camera, # used the v4l2 interface . # author: licaibiao # Version: # lastchange: 2016-12-11 # History: =============================================================================* / #include <stdio.h> #include <stdlib.h> #include <string.h> #include "./include/h264encoder.h"   Void compress_begin (encoder *en, int width, int height) { en->param = (x264_param_t *) malloc (sizeof (x264_param_t)); en->picture = (x264_picture_t *) malloc (sizeof (x264_ picture_t)); x264_param_default (en->param); //set default param //en->param->rc.i_rc_method = X264_RC_CQP; // en->param->i_log_level = X264_LOG_NONE; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;EN->PARAM->I_THREADS&NBSP;&NBSP;=&NBSP;X264_SYNC_ lookahead_auto; en->param->i_width = width; //set frame width en->param->i_height = Height; //set frame height en->param->i_frame_total = 0; en->param->i_keyint_max = 10; en->param->rc.i_lookahead = 0; en- >param->i_bframe = 5; en->param->b_ open_gop = 0; en->param->i_bframe_pyramid = 0; en->param->i_bframe_adaptive = X264_B_ADAPT_TRELLIS; en->param->rc.i_bitrate = 11024 * 10;// rate 10 kbps en->param->i_fps_num = 25; en->param->i_fps_den = 1; x264_param_Apply_profile (En->param, x264_profile_names[0]); if ((En->handle = x264_encoder_open (En->param)) == 0) { return; } /* Create a new pic */ x264_picture_ Alloc (en->picture, x264_csp_i420, en->param->i_width, en->param->i_height); en->picture->img.i_csp = X264_CSP_I420; en-> picture->img.i_plane = 3; } Int compress_frame (Encoder *en, int type, uint8_t *in, uint8_t *out) { x264_picture_t pic_out; int nNal = -1; int result = 0; int i = 0; uint8_t *p_out = out; charchar *y = en->picture->img.plane[0]; charchar *u = en->picture->img.plane[1]; charchar *v = en->picture->img.plane[2]; //yuv420_length = 1.5 * en->param->i_width * en->param->i_height //int length = en- >param->i_width * en->param->i_height; //y = 640*480 ; u = 640*480*0.25, v = 640*480*0.25; memcpy (y,in,307200); memcpy (u,in+307200,76800); memcpy (v,in+384000,76800); 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; &NBSP;&Nbsp; 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 (p_out, en->nal[i].p_payload, en->nal[i].i_payload); p_out += en->nal[i].i_payload; result += en->nal[i].i_payload; } return result; } void compress_end ( Encoder *en) { 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); } function Compress_begin initialization of the meaning of the parameters, you can refer to the x264 important structure detailed description The above parameters I was randomly configured.
Compress_frame inside.
memcpy (y,in,307200);
memcpy (u,in+307200,76800);
memcpy (v,in+384000,76800);
The value is based on my previous V4L2 output format, and I set the YUV420 output format, where each pixel is a UV component for every 4 y component, so y = 4/4*width*hight;u = 1/4 *width*hight; V = 1/4 *width* Hight Specific can refer to: Graphic detailed YUV420 data format
The results of the compiler's execution to the Development Board are as follows:[OBJC] View Plain copy print? /tmp # /tmp # ls hostapd lib messages utmp x264_test /tmp # ls lib libx264.so.148 /tmp # export ld_library_path=/tmp/lib: $LD _library_path /tmp # ./x264_test camera driver name is : sunxi-vfe Camera device name is : sunxi-vfe camera bus information: sunxi_vfe vfe.2 n_buffer = 3 X264 [warning]: lookaheadless mb-tree requires intra refresh or infinite keyint X264&NBSP;[INFO]:&NBSP;USING&NBSP;CPU capabilities: none! X264 [info]: profile constrained baseline, level 3.0 x264 [warning]: non-strictly-monotonic pts Encode_ frame num = 0 x264 [warning]: non-strictly-monotonic pts encode_frame num = 1 .... .... x264 [warning]: non-strictly-monotonic pts encode_frame num = 98 x264 [info]: frame i:10 avg qp:23.29 size: 15057 X264 [info]: frame p:89 avg QP:26.69 size: 2080 x264 [info]: mb i i16 ... 4: 63.6% 0.0% 36.4% x264 [info]: mb p i16. 4: 10.6% 0.0% 0.2% p16.. 4: 39.0% 3.5% 1.8% 0.0% 0.0% skip:44.8% X264 [info]: coded y,uvdc,uvac intra: 17.2% 83.3% 39.6% inter: 4.0% 30.0% 0.1% X264 [info]: i16 v,h,dc,p: 49% 24% 13% 14% X264&NBSP;[INFO]:&NBSP;I4&NBSP;V,H,DC, Ddl,ddr,vr,hd,vl,hu: 1