1 Star 1 Fork 1

fangbaolei / yuyvToX264file

forked from cM / yuyvToX264file 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
h264encoder.c 3.09 KB
一键复制 编辑 原始数据 按行查看 历史
daixj 提交于 2014-12-10 17:06 . 简单ok
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "h264encoder.h"
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->i_csp = X264_CSP_I422;/*格式一致 默认是420格式*/
//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 is_y = 1, is_u = 1;
int y_index = 0, u_index = 0, v_index = 0;
int yuv422_length = 2 * en->param->i_width * en->param->i_height;
//序列为YU YV YU YV,一个yuv422帧的长度 width * height * 2 个字节
for (i = 0; i < yuv422_length; ++i) {
if (is_y) {
*(y + y_index) = *(in + i);
++y_index;
is_y = 0;
} else {
if (is_u) {
*(u + u_index) = *(in + i);
++u_index;
is_u = 0;
} else {
*(v + v_index) = *(in + i);
++v_index;
is_u = 1;
}
is_y = 1;
}
}
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;
}
en->picture->i_pts++;/*这里严重影响到压缩后的文件大小*/
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->picture = 0;
}
if (en->param) {
free(en->param);
en->param = 0;
}
if (en->handle) {
x264_encoder_close(en->handle);
}
//free(en);
}
1
https://gitee.com/fangshi/yuyvToX264file.git
git@gitee.com:fangshi/yuyvToX264file.git
fangshi
yuyvToX264file
yuyvToX264file
master

搜索帮助