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. sendmail 出现 My unqualified host name的解决办法

    有"My unqualified host name"错误 修改/etc/hosts, 在本机的ip那一行, 在xxxhostname后面加上"  xxxhostname ...

  2. java类使用

    package java04; /* * 通常情况下,一个类不能直接使用,需要创建一个对象,才能使用 * *步骤: * 1.导包:就是指出需要使用的类在什么位置 * import 包名称.类名称: * ...

  3. jenkins持续集成(三): jenkins配置邮件通知

    完成基于jenkins的持续集成部署后,任务构建执行完成,测试结果需要通知到相关人员.这篇博客,介绍如何在jenkins中配置邮件通知的方法... 一.安装邮件插件 由于Jenkins自带的邮件功能比 ...

  4. 使用jstack排查线程问题

    以一个例子来演示排查服务器cpu占用率过高的问题. 准备 将下面的代码文件上传到服务器上,然后使用javac编译,并使用java命令将程序跑起来. public class JStackCase { ...

  5. $NOIP2018$ 爆踩全场记

    NOIP2018 Day-1 路还很长. 这里就是起点. 这是最简单的一步,但这是最关键的一步. 联赛就在眼前了,一切好像都已经准备好了,一切好像又都没准备好. 相信自己吧,\(mona\),这绝对不 ...

  6. 25.Java锁的深度化

    Java锁的深度化 悲观锁.乐观锁.排他锁 场景 当多个请求同时操作数据库时,首先将订单状态改为已支付,在金额加上200,在同时并发场景查询条件下,会造成重复通知. SQL: Update 悲观锁与乐 ...

  7. fat文件系统

    在主引导区后面就是FAT表.从上面可以得知一个FAT表是229个扇区.它里边的内容很简单,里边的内容就是指出下一个簇在哪里.你的盘有多少个簇,那么它的FAT表就要有多少个项来描述它们.因为FAT16是 ...

  8. webpackES6语法转ES5语法

    安装babel-loader: npm install --save-dev babel-loader@7 babel-core babel-preset-es2015 webpack.config. ...

  9. click和blur冲突的问题

    昨天在前端群里讨论到一个问题,大家平时做表单验证的时候一般都有个input框和删除按钮,然后习惯性在失去焦点的时候去验证输入的内容是否正确,做验证,发请求等等.这个时候,那个点击删除按钮往往也就触发了 ...

  10. Android Studio使用tips

    安装位置:C:\Users\xxx\AppData\Local\Android\sdk https://developer.android.com/topic/libraries/support-li ...