Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1075693
  • 博文数量: 77
  • 博客积分: 821
  • 博客等级: 军士长
  • 技术积分: 1905
  • 用 户 组: 普通用户
  • 注册时间: 2011-10-23 16:17
个人简介

学校:上海交通大学软件工程 学历:硕士 行业:从事流媒体移动开发 QQ: 412595942 邮箱:yiikai1987910@gmail.com

文章分类

全部博文(77)

文章存档

2016年(4)

2015年(15)

2014年(16)

2013年(12)

2012年(21)

2011年(9)

分类: C/C++

2014-10-28 22:29:24

    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. }

阅读(9598) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~