1、源码下载

http://download.videolan.org/x264/snapshots/

2、编译

./configure --prefix=./_install --enable-shared --enable-static

make

make install

3、demo 

在x264库里的x264_config.h中确定版本号,版本太混乱了,相差太远可能还不兼容

#define X264_POINTVER "0.157.x"

main.c

/**
* 最简单的基于X264的视频编码器
* Simplest X264 Encoder
* leixiaohua
*
* 本程序可以YUV格式的像素数据编码为H.264码流,是最简单的
* 基于libx264的视频编码器
*
* This software encode YUV data to H.264 bitstream.
* It's the simplest encoder example based on libx264.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
//#include <pthread.h> #if defined ( __cplusplus)
extern "C"
{
#include "x264.h"
};
#else
#include "x264.h"
#endif int csp=X264_CSP_I420;
int frame_num=0;
int width=640;
int height=360; FILE* fp_src;
FILE* fp_dst; x264_nal_t* pNals;
x264_t* pHandle;
x264_picture_t* pPic_in;
x264_picture_t* pPic_out;
x264_param_t* pParam; int y_size; int en_h264_init()
{ fp_src = fopen("./cuc_ieschool_640x360_yuv420p.yuv", "rb");
fp_dst = fopen("output.h264", "wb"); pNals = NULL;
pHandle = NULL;
pPic_in = (x264_picture_t*)malloc(sizeof(x264_picture_t));
pPic_out = (x264_picture_t*)malloc(sizeof(x264_picture_t));
pParam = (x264_param_t*)malloc(sizeof(x264_param_t)); //Check
if(fp_src==NULL||fp_dst==NULL){
printf("Error open files.\n");
return -1;
} x264_param_default(pParam);
x264_param_default_preset(pParam, "fast" , "zerolatency" ); pParam->i_csp=csp;
pParam->i_width = width;
pParam->i_height = height;
pParam->i_fps_num = 25;
pParam->i_fps_den = 1; pParam->i_threads = X264_SYNC_LOOKAHEAD_AUTO;
pParam->i_keyint_max = 10; pParam->rc.i_bitrate = 1200;
pParam->rc.i_rc_method = X264_RC_ABR; //set profile
x264_param_apply_profile(pParam, "baseline"); //open encoder
pHandle = x264_encoder_open(pParam); x264_picture_init(pPic_out);
x264_picture_alloc(pPic_in, csp, pParam->i_width, pParam->i_height); y_size = pParam->i_width * pParam->i_height;
//detect frame number
if(frame_num==0){
fseek(fp_src,0,SEEK_END);
frame_num=ftell(fp_src)/(y_size*3/2);
fseek(fp_src,0,SEEK_SET);
}
} int en_h264_run(char *y,char *u,char *v)
{
int i,j,ret;
int iNal = 0;
#if 1 fread(pPic_in->img.plane[0],y_size,1,fp_src); //Y
fread(pPic_in->img.plane[1],y_size/4,1,fp_src); //U
fread(pPic_in->img.plane[2],y_size/4,1,fp_src); //V #else
memcpy(pPic_in->img.plane[0],y,y_size);
memcpy(pPic_in->img.plane[1],u,y_size / 4);
memcpy(pPic_in->img.plane[2],v,y_size / 4);
#endif pPic_in->i_pts += 1; ret = x264_encoder_encode(pHandle, &pNals, &iNal, pPic_in, pPic_out);
if (ret< 0){
printf("Error.\n");
return -1;
} printf("Succeed encode frame: %5d\n",pPic_in->i_pts-1); for ( j = 0; j < iNal; ++j){
fwrite(pNals[j].p_payload, 1, pNals[j].i_payload, fp_dst);
} /* Flush delayed frames */
while( x264_encoder_delayed_frames( pHandle ) )
{
ret = x264_encoder_encode( pHandle, &pNals, &iNal, NULL, pPic_out );
if( ret )
{
fwrite( pNals[j].p_payload, 1, pNals[j].i_payload, fp_dst );
}
} } int en_h264_release()
{
x264_picture_clean(pPic_in);
x264_encoder_close(pHandle);
pHandle = NULL; free(pPic_in);
free(pPic_out);
free(pParam); fclose(fp_src);
fclose(fp_dst); return 0;
} int main(int argc, char** argv)
{
en_h264_init(); char y[640*360];
char u[640*360/4];
char v[640*360/4]; for(int i=0;i<frame_num;i++){
en_h264_run(y,u,v);
}
en_h264_release(); return 0;
}

其实x264源码包里自带的demo更好,x264-snapshot-20190512-2245/example.c

/*****************************************************************************
* example.c: libx264 API usage example
*****************************************************************************
* Copyright (C) 2014-2019 x264 project
*
* Authors: Anton Mitrofanov <BugMaster@narod.ru>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
*
* This program is also available under a commercial proprietary license.
* For more information, contact us at licensing@x264.com.
*****************************************************************************/ #ifdef _WIN32
#include <io.h> /* _setmode() */
#include <fcntl.h> /* _O_BINARY */
#endif #include <stdint.h>
#include <stdio.h>
#include "x264.h" #include <math.h> #define FAIL_IF_ERROR( cond, ... )\
do\
{\
if( cond )\
{\
fprintf( stderr, __VA_ARGS__ );\
goto fail;\
}\
} while( 0 ) int main( int argc, char **argv )
{ x264_param_t param;
x264_picture_t pic;
x264_picture_t pic_out;
x264_t *h;
int i_frame = 0;
int i_frame_size;
x264_nal_t *nal;
int i_nal; FILE* fp_src = fopen("./cuc_ieschool_640x360_yuv420p.yuv", "rb");
FILE* fp_dst = fopen("cuc_ieschool.h264", "wb"); int width=640,height=360; /* Get default params for preset/tuning */
if( x264_param_default_preset( &param, "medium", NULL ) < 0 )
goto fail; /* Configure non-default params */
param.i_bitdepth = 8;
param.i_csp = X264_CSP_I420;
param.i_width = width;
param.i_height = height;
param.b_vfr_input = 0;
param.b_repeat_headers = 1;
param.b_annexb = 1; /* Apply profile restrictions. */
if( x264_param_apply_profile( &param, "high" ) < 0 )
goto fail; if( x264_picture_alloc( &pic, param.i_csp, param.i_width, param.i_height ) < 0 )
goto fail;
#undef fail
#define fail fail2 h = x264_encoder_open( &param );
if( !h )
goto fail;
#undef fail
#define fail fail3 int luma_size = width * height;
int chroma_size = luma_size / 4;
/* Encode frames */
for( ;; i_frame++ )
{
/* Read input frame */
if( fread( pic.img.plane[0], 1, luma_size, fp_src ) != luma_size )
break;
if( fread( pic.img.plane[1], 1, chroma_size, fp_src ) != chroma_size )
break;
if( fread( pic.img.plane[2], 1, chroma_size, fp_src ) != chroma_size )
break; pic.i_pts = i_frame;
i_frame_size = x264_encoder_encode( h, &nal, &i_nal, &pic, &pic_out );
if( i_frame_size < 0 )
goto fail;
else if( i_frame_size )
{
if( !fwrite( nal->p_payload, i_frame_size, 1, fp_dst ) )
goto fail;
}
}
/* Flush delayed frames */
while( x264_encoder_delayed_frames( h ) )
{
i_frame_size = x264_encoder_encode( h, &nal, &i_nal, NULL, &pic_out );
if( i_frame_size < 0 )
goto fail;
else if( i_frame_size )
{
if( !fwrite( nal->p_payload, i_frame_size, 1, fp_dst ) )
goto fail;
}
} x264_encoder_close( h );
x264_picture_clean( &pic );
return 0; #undef fail
fail3:
x264_encoder_close( h );
fail2:
x264_picture_clean( &pic );
fail:
return -1;
}

4、编译

1) 务必先设置好库文件环境变量

export LD_LIBRARY_PATH=~/gb28181/x264-snapshot-20190512-2245/_install/lib:$LD_LIBRARY_PATH

gcc -o main main.c -I ./_install/include -L ./_install/lib -lx264 -lpthread -std=c99

gcc -o example example.c -I ./_install/include -L ./_install/lib -lx264 -lpthread -std=c99

#gcc -o main main.c -I ./_install/include ./_install/lib/libx264.a -lpthread -std=c99

不知道什么原因,用静态库编译会出错

2) 也可以不配置环境变量,将x264安装到ubuntu默认路径,直接gcc -o main main.c -lx264

5、yuv/rgb <---> h264

ffmpeg-4.1.tar.bz2

./configure
--target-os=linux --cc=arm-linux-gnueabihf-gcc --arch=arm
--enable-shared --enable-cross-compile
--cross-prefix=arm-linux-gnueabihf- --enable-gpl --enable-ffplay
--enable-libx264 --enable-postproc --prefix=$(pwd)/_install
--extra-cflags=-I$(pwd)/_install/include
--extra-ldflags=-L$(pwd)/_install/lib
make
make install

A simple C program that convert the raw RGB stream to H264 stream and vice versa

https://github.com/lctseng/H264-RGB-Encode-Decode

版本不一致,编译不过,改成以上ffmpeg-4.1.tar.bz2和x264-snapshot-20190512-2245了

https://files.cnblogs.com/files/dong1/rgb_h264_demo.zip

6、交叉编译

./configure --prefix=${PWD}/_install --enable-shared --disable-asm --disable-cli --host=arm-linux-gnueabihf

更改config.mak
CC=arm-linux-gnueabihf-gcc
LD=arm-linux-gnueabihf-gcc -o
AR=arm-linux-gnueabihf-gcc-ar rc
RANLIB=arm-linux-gnueabihf-gcc-ranlib

make

make install

7、参考设计

libx264编码---YUV图像数据编码为h.264码流
https://blog.csdn.net/qq_41248872/article/details/83068869

各种音视频测试文件

http://www.live555.com/liveMedia/public/

http://samples.mplayerhq.hu/

使用libyuv对yuv数据进行缩放,旋转,镜像,裁剪等操作

https://github.com/lemenkov/libyuv

YUV/RGB与H264之间的编解码的更多相关文章

  1. iOS8系统H264视频硬件编解码说明

    公司项目原因,接触了一下视频流H264的编解码知识,之前项目使用的是FFMpeg多媒体库,利用CPU做视频的编码和解码,俗称为软编软解.该方法比较通用,但是占用CPU资源,编解码效率不高.一般系统都会 ...

  2. 【知识点】H264, H265硬件编解码基础及码流分析

    前言 音视频开发需要你懂得音视频中一些基本概念,针对编解码而言,我们必须提前懂得编解码器的一些特性,码流的结构,码流中一些重要信息如sps,pps,vps,start code以及基本的工作原理,而大 ...

  3. FFmpeg音视频编解码实践总结

    PS:由于目前开发RTSP服务器传输模块时用到了h264文件,所以攻了一段时间去实现h264的视频编解码,借用FFmpeg SDK实现了任意文件格式之间的转换,并实现了流媒体实时播放,目前音视频同步需 ...

  4. 音视频处理基础知识扫盲:数字视频YUV像素表示法以及视频帧和编解码概念介绍

    专栏:Python基础教程目录 专栏:使用PyQt开发图形界面Python应用 专栏:PyQt+moviepy音视频剪辑实战 专栏:PyQt入门学习 老猿Python博文目录 老猿学5G博文目录 一. ...

  5. 转:关于视频H264编解码的应用实现

    转:http://blog.csdn.net/scalerzhangjie/article/details/8273410 项目要用到视频编解码,最近半个月都在搞,说实话真是走了很多弯路,浪费了很多时 ...

  6. 【FFMPEG】各种音视频编解码学习详解 h264 ,mpeg4 ,aac 等所有音视频格式

    目录(?)[-] 编解码学习笔记二codec类型 编解码学习笔记三Mpeg系列Mpeg 1和Mpeg 2 编解码学习笔记四Mpeg系列Mpeg 4 编解码学习笔记五Mpeg系列AAC音频 编解码学习笔 ...

  7. 各种音视频编解码学习详解 h264 ,mpeg4 ,aac 等所有音视频格式

    编解码学习笔记(一):基本概念 媒体业务是网络的主要业务之间.尤其移动互联网业务的兴起,在运营商和应用开发商中,媒体业务份量极重,其中媒体的编解码服务涉及需求分析.应用开发.释放 license收费等 ...

  8. 视频编解码学习之路(H264)

    学习视频编解码技术很难吗?视频编解码技术的未来是什么? 明了的说,无论是软件还是硬件设计,视频编解码技术有很多难点,都需要很长一段时间积累才行. 从一开始接触MPEG-2到最新的H.264标准,可算走 ...

  9. 80.YCrCb - YUV - RGB之间的介绍

    一,引言 YUV(亦称YCrCb)是被欧洲电视系统所采用的一种颜色编码方法(属于PAL).YUV主要用于优化彩色视频信号的传输,使其向后兼容老式黑白电视.与RGB视频信号传输相比,它最大的优点在于只需 ...

随机推荐

  1. Codeforces Round #394 (Div. 2) - B

    题目链接:http://codeforces.com/contest/761/problem/B 题意:给定一个环形跑道.里面有n个障碍,跑道长度为L.然后有两个人在两个起点(起点可能相同),每个人都 ...

  2. HTML5:Canvas-绘制图形

    到本文的最后,你将学会如何绘制矩形,三角形,直线,圆弧和曲线,变得熟悉这些基本的形状.绘制物体到Canvas前,需掌握路径,我们看看到底怎么做. 栅格 在我们开始画图之前,我们需要了解一下画布栅格(c ...

  3. 第02章 IOC和bean的配置

    第02章 IOC容器和Bean的配置 1.IOC和DI ①IOC(Inversion of Control):反转控制. 在应用程序中的组件需要获取资源时,传统的方式是组件主动的从容器中获取所需要的资 ...

  4. 在vscode中快速生成vue模板

    点击文件-->首选项-->用户代码片段-->输入vue,此时会打开vue.json文件,将下列代码复制进文件保存即可,新建一个vue文件,输入vue回车即可生成模板,$0表示生成模板 ...

  5. Spring---数据缓存Cache

    1.Spring缓存支持 1.1.Spring定义了org.springframework.cache.CacheManager类.org.springframework.cache.Cache类接口 ...

  6. Python的list中的选取范围

    a = [1,2,3,4,5,6,7,8,9,10] a[0:1] = [1] a[0:2] = [1,2] 包含开头,不包含结尾. a [:-1]: 从头一直到最后一个元素a[-1],但不包含最后一 ...

  7. I/O性能优化

    原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11525014.html Linux 系统的 I/O 栈图 I/O性能指标 根据指标找工具 根据工具查指 ...

  8. 调试web worker (动态生成的worker)

    1.在worker.js源码文件中 写下debugger关键词 2. F12打开控制台,重新刷新页面,加载worker.js文件(注意之前的缓存,chrome推荐使用 ctrl + F5 刷新) 3. ...

  9. visual studio code的使用

    1.添加代码片段 参考:https://blog.csdn.net/qq_36370731/article/details/83014439 2.在vscode上运行Git 先打开vscode内置的G ...

  10. 重写ArcGIS的TiledMapServiceLayer调用天地图瓦片

    require(["esri/layers/TiledMapServiceLayer"], function () { dojo.declare("com.StrongI ...