日期:2014-05-16  浏览次数:20764 次

H264 X264 linux下的安装?库?lx264?
这几天在弄视频编码这个东东,在网上搜了好多高人的资料,可惜我是在太笨了,不知道怎么用啊,然后根据一个牛人的程序改改,编译,结果出现个各种奇怪的症状···命苦啊····
求大神来救救我啊!!!!
是这样的,我的程序是这样的,就是想把已经保存好的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 =&n