Chinaunix首页 | 论坛 | 博客
  • 博客访问: 7151
  • 博文数量: 3
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 22
  • 用 户 组: 普通用户
  • 注册时间: 2013-08-28 14:59
文章分类

全部博文(3)

文章存档

2017年(1)

2015年(1)

2013年(1)

我的朋友

分类: C/C++

2017-01-06 20:26:01

原文地址:最简单的x264编码h.264 作者:DBOYaoao

    ffmepg中的压缩264直接也是用的x264库,直接研究了x264 , 果然高效简单,不过还有很多概念有待研究。记录最简单的实现,直接上代码
  

点击(此处)折叠或打开

  1. #include <stdint.h>
  2. #include "x264.h"
  3. #include "x264_config.h"
  4. #include <stdio.h>
  5. int main()
  6. {
  7.     int width = 480;
  8.     int height = 272;
  9.     int fps = 25;
  10.     size_t yuv_size = width * height *3/2;
  11.     x264_t *encoder;
  12.     x264_picture_t pic_in,pic_out;
  13.     int inf,outf;
  14.     uint8_t *yuv_buffer;
  15.  
  16.      x264_param_t m_param;
  17.      x264_param_default_preset(&m_param,"veryfast","zerolatency");
  18.      m_param.i_threads = 1;
  19.      m_param.i_width = width;
  20.      m_param.i_height = height;
  21.      m_param.i_fps_num = fps;
  22.      m_param.i_bframe = 10;
  23.      m_param.i_fps_den = 1;
  24.      m_param.i_keyint_max = 25;
  25.      m_param.b_intra_refresh = 1;
  26.      m_param.b_annexb = 1;
  27.      x264_param_apply_profile(&m_param,"high422");
  28.      encoder = x264_encoder_open(&m_param);

  29.     x264_encoder_parameters( encoder, &m_param );
  30.     
  31.      x264_picture_alloc(&pic_in, X264_CSP_I420, width, height);

  32.      yuv_buffer = malloc(yuv_size);

  33.      pic_in.img.plane[0] = yuv_buffer;
  34.      pic_in.img.plane[1] = pic_in.img.plane[0] + width * height;
  35.      pic_in.img.plane[2] = pic_in.img.plane[1] + width * height / 4;

  36.     FILE* infile = fopen("ds_480x272.yuv","rb");
  37.     FILE* outfile = fopen("out.h264","ab");
  38.     if(!infile || !outfile)
  39.         {
  40.             printf("open file error\n");
  41.             return 0;
  42.         }
  43.     int64_t i_pts = 0;
  44.  
  45.       x264_nal_t *nals;
  46.       int nnal;
  47.       while (fread(yuv_buffer, 1,yuv_size, infile) > 0) {
  48.           pic_in.i_pts = i_pts++;
  49.           x264_encoder_encode(encoder, &nals, &nnal, &pic_in, &pic_out);
  50.           x264_nal_t *nal;
  51.           for (nal = nals; nal < nals + nnal; nal++) {
  52.              fwrite(nal->p_payload, 1,nal->i_payload,outfile);
  53.          }
  54.      }
  55.     x264_encoder_close(encoder);
  56.  close(infile);
  57.  close(outfile);
  58.  free(yuv_buffer);

  59.     return 0;
  60. }

阅读(447) | 评论(0) | 转发(0) |
0

上一篇: VMware下Ubuntu与宿主机XP共享文件

下一篇:没有了

给主人留下些什么吧!~~