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. 一、WebApi模型验证实践项目使用

    一.启语 前面我们说到,模型验证的原理(包含1.项目创建,2.模型创建,3.走通测试模型验证,4.在过滤器中处理返回json格式(非控制器内))-完全是新手理解使用的,新番理解 通常情况下,对于那些经 ...

  2. 让docker容器使用主机系统时间(挂入/etc/localtime)

    -v挂入这个文件就可以了: -v /etc/localtime:/etc/localtime:ro

  3. Codeforces Round #420 (Div. 2) - A

    题目链接:http://codeforces.com/contest/821/problem/A 题意:给定一个n*n的矩阵. 问你这个矩阵是否满足矩阵里的元素除了1以外,其他元素都可以在该元素的行和 ...

  4. 牛客小白月赛16 D 小阳买水果 (思维题)

    链接:https://ac.nowcoder.com/acm/contest/949/D来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言52428 ...

  5. Flutter的flutter_calendar日曆的使用

    效果: 添加依賴: flutter_calendar: ^0.0.1 項目中導入 import 'package:flutter_calendar/flutter_calendar.dart'; 例子 ...

  6. NCRE训练二

    package com.fei.ncre; import java.io.RandomAccessFile; /** * 该程序的功能是将本程序代码打印输出 */ public class Java_ ...

  7. python将文件导入字典

    a={}i=0f = open("filepath","r")for line in f.readlines(): a[i] =line i=i+1 a是字典, ...

  8. 1.WCF学习--创建简单的WCF服务

    一.基本了解WCF 1.面向服务代表的是一种设计理念,和面向对象.面向组件一样,体现的是一种对关注点进行分解的思想,面向服务是和技术无关的 2.WCF需要依存一个运行着的宿主进程,服务寄宿就是为服务指 ...

  9. notepad++ 安装 hex_editor 十六进制查看插件

    1.到 https://github.com/chcg/NPP_HexEdit/releases 去下载相应的文件,注意32位和64位的区别 2.点击notepad++的设置--->导入--&g ...

  10. 一次服务器CPU占用100%的问题排查

    今天写了一段垃圾代码,然后上服务器上运行,cpu瞬间飙到了100%,现记录一下问题排除过程~ 1. 问题代码 package qinfeng.zheng.mockmvcdemo; import org ...