日期:2014-05-16 浏览次数:20936 次
#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