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. GSL+DevC++使用

    在DEV C++中配置GSL1.8库 前面写了如何在vs2005中添加gsl,本文所所述为在dev c++中使用gsl库,由实践总结而得. 准备软件: 1.Orwell Dev C++ 5.6.2 N ...

  2. python2和python3同时存在电脑时,安装包时的的命令行

    若是在Python2中使用pip操作时,用pip2或是pip2.7相关命令. 例:给Python2安装selenium,在cmd中输入 pip2 install selenium 或是 pip2.7 ...

  3. 解决swagger跨项目或跨程序集注释不显示问题

    背景 我们在使用Swagger生成.NET Core Web Api 项目接口文档时候,发现接口的入参和出参的注释是看不见的,如下: 但是我想要结果是这样: 原因分析以及方案 为什么没有显示注释呢,注 ...

  4. 【BZOJ3052&UOJ58】糖果公园(树上带修莫队)

    题意:给定一个n个点的树,每个结点上有一种颜色c[i] 定义一条简单路径的偷税指数为simga (sigma w[i](i=0..a[j]))*v[j](j=0..m),其中a[i]为第i种颜色在路径 ...

  5. 【HDOJ6579】Operation(线性基)

    题意:给定一个数列a,给定两种操作: 1.询问[l,r]区间内最大的xor和 2.n++,a[n]赋值为x 要求强制在线 n,m<=5e5,a[i]<2^30 思路:同CF1100F 固定 ...

  6. SQL注入的简单认识

    写在前面 MYSQL5.0之后的版本,默认在数据库中存放一个information_schema的数据库,其中应该记住里面的三个表SCHEMATA.TABLES.COLUMNS SCHEMATA表:存 ...

  7. flutter中的路由跳转

    在前面的基本路由和命名路由中,都演示了如何进行路由跳转,并且在路由跳转以后,可以借用系统自带的按钮就行返回上一级,当然了,也可以自定义按钮返回上一级. 返回上一级 在前面的例子中,当从Home.dar ...

  8. AcWing 248. 窗内的星星 (扫描线)打卡

    题目:https://www.acwing.com/problem/content/250/ 题意:给你n个点,现在问你能每个点都有个权值,问你能覆盖最多的权值是多少,边界不算 思路:这个其实和我之前 ...

  9. SQLmap注入

    一.安装 先安装Python2.7 下载SQLmap:http://sqlmap.org/ 下载文件解压到Python文件目录下 然后设置环境变量:D:\Python27\sqlmap 在cmd查看是 ...

  10. Win7系统取消登录界面的两种方法(图文)

    windows7系统设置电脑密码后,即使取消密码,也会出现登录界面 ,每次都要点击用户图标才能进入系统,这样比较麻烦.那么有什么办法可以取消登录界面呢?方法当然是有的,阅读下文教程,我们一起来看下Wi ...