H264 X264 linux下的安装?库?lx264?

爆板流 2012-11-20 05:32:20
这几天在弄视频编码这个东东,在网上搜了好多高人的资料,可惜我是在太笨了,不知道怎么用啊,然后根据一个牛人的程序改改,编译,结果出现个各种奇怪的症状···命苦啊····
求大神来救救我啊!!!!
是这样的,我的程序是这样的,就是想把已经保存好的YUV420格式的图片,压缩下,看看行不行,网上说X264就是H264的一种,所以想用X264应该就行了啊,我的源码如下:

#include <asm/types.h> /* for videodev2.h */
#include <fcntl.h> /* low-level i/o */
#include <unistd.h>
#include <errno.h>
#include <malloc.h>
#include <sys/stat.h>
#include <sys/types.h>
//#include <sys/time.h>
#include <time.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


#include <assert.h>
#include <dirent.h>
#include "h264encoder.h"

#define CLEAR(x) memset (&(x), 0, sizeof (x))

typedef unsigned char uint8_t;

uint8_t *h264_buf;
unsigned int n_buffers = 0;
//int len = 640* 480* 3/2;
Encoder en;

void init_encoder() {
compress_begin(&en, 640, 480);
h264_buf = (uint8_t *) malloc(
sizeof(uint8_t)*640* 480* 3/2); // 设置缓冲区
}

void close_encoder() {
compress_end(&en);
free(h264_buf);
}

void encode_frame(char * yuv_frame, char * output_filename) {
int h264_length = 0;

h264_length = compress_frame(&en, -1, yuv_frame, h264_buf);
if (h264_length > 0) {
//写h264文件
fwrite(h264_buf, h264_length, 1, output_filename);
}
}

int read_and_encode_frame(char * input_filename,char * output_filename) {

FILE *input_file;
FILE *output_file;
if ((/*infile*/input_file = fopen(input_filename, "rb")) == NULL) {
fprintf(stderr, "can't open %s\n", input_filename);
return 0;
}

if ((/*infile*/output_file = fopen(output_filename, "wb")) == NULL) {
fprintf(stderr, "can't open %s\n", output_filename);
return 0;
}

encode_frame(input_file, output_file);

fclose(input_file);
fclose(output_file);
return 1;
}

int main()
{
init_encoder();
read_and_encode_frame("jpgimage1.yuv","jpgimage1.h264");
close_encoder

下面是encoder.h
#ifndef _H264ENCODER_H
#define _H264ENCODER_H

#include <stdint.h>
#include <stdio.h>
#include "x264.h"

typedef unsigned char uint8_t;

typedef struct {
x264_param_t *param;
x264_t *handle;
x264_picture_t *picture; //说明一个视频序列中每帧特点
x264_nal_t *nal;
} Encoder;

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;

// en->param->i_threads = 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; //表示i帧向前缓冲区
// en->param->i_bframe = 5; //两个参考帧之间b帧的数目

// 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 = 1024 * 10;//rate 为10 kbps
en->param->i_fps_num = 5; //帧率分子
en->param->i_fps_den = 1; //帧率分母
x264_param_apply_profile(en->param, x264_profile_names[0]); //使用baseline

if ((en->handle = x264_encoder_open(en->param)) == 0) {
return;
}
/* Create a new pic */
x264_picture_alloc(en->picture, X264_CSP_I422, en->param->i_width,
en->param->i_height);
en->picture->img.i_csp = X264_CSP_I422;
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;

char *y = en->picture->img.plane[0];
char *u = en->picture->img.plane[1];
char *v = en->picture->img.plane[2];

int y_index = 0, u_index = 0, v_index = 0;

int length0 = en->param->i_width * en->param->i_height;
int length1 = en->param->i_width * en->param->i_height/4;

//序列为YYYY YYYY UU VV,一个yuv420帧的长度 width * height * 3/2 个字节
for (i = 0; i < length0; ++i) {
*(y + y_index) = *(in + i);
++y_index;

if(i < length1){
*(u + u_index) = *(in + length0+i);
++u_index;
*(v + v_index) = *(in + length0 + length1 + i);
++v_index;
}
}

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;
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->p



然后呢,我在网上下载了一个包,如下图:
然后用arm-linux-gcc -o aa example14_h264.c
出错啦,说什么没定义啊,可是这些名名就是定义在x264.h里面的,难道什么问题??!
...全文
480 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
cfzhanxian 2014-03-14
  • 打赏
  • 举报
回复
需要安装x264
MrxMyx 2012-12-31
  • 打赏
  • 举报
回复
楼主弄好了吗?我也遇到这问题了,编译时提示找不到lx264
爆板流 2012-11-22
  • 打赏
  • 举报
回复
引用 1 楼 linlan999 的回复:
arm-linux-gcc -o aa example14_h264.c 需要连接x264的库的,也就是说需要加上 -lx264 这里的x264为使用arm-linux-gcc 编译的x264的库libx264.so.xxx
多谢了。找到方向了,现在要去弄库了~~~~
linlan999 2012-11-21
  • 打赏
  • 举报
回复
arm-linux-gcc -o aa example14_h264.c 需要连接x264的库的,也就是说需要加上 -lx264 这里的x264为使用arm-linux-gcc 编译的x264的库libx264.so.xxx

23,121

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧