美文网首页
LibX264编码库

LibX264编码库

作者: 何亮hook_8285 | 来源:发表于2022-03-15 22:37 被阅读0次

    H264简介

    H.264从1999年开始,到2003年形成草案,最后在2007年定稿有待核实。在ITU的标准里称为H.264,在MPEG的标准里是MPEG-4的一个组成部分–MPEG-4 Part 10,又叫Advanced Video Codec,因此常常称为MPEG-4 AVC或直接叫AVC。

    yuv编码H264

    #include <iostream>
    #include <string.h>
    #include <stdlib.h>
    #include <stdio.h>
    extern "C" {
    #include <x264.h>
    }
    
    #define IN_FILE_NAME "out.yuv"
    #define OUT_FILE_NAME "1.h264"
    
    using namespace std;
    
    
    int main() {
    
        x264_t *handle = NULL;
        x264_nal_t *p_nal = NULL;
        int nal = 0;
        x264_param_t param;
        x264_picture_t in_pic;
        x264_picture_t out_pic;
    
        x264_param_default_preset(&param, "medium", "film");
        x264_param_apply_profile(&param, "main");
        param.i_width = 1610;
        param.i_height = 1030;
    
        x264_picture_init(&in_pic);
        x264_picture_init(&out_pic);
    
        FILE *in_fp = fopen(IN_FILE_NAME, "rb");
        FILE *out_fp = fopen(OUT_FILE_NAME, "wb");
    
        x264_picture_alloc(&in_pic, X264_CSP_I420, param.i_width, param.i_height);
    
        handle = x264_encoder_open(&param);
        int iSize = param.i_width * param.i_height;
        int uv = iSize / 4;
        int ret = 0;
        int j = 0;
        while (1) {
    
            //Y
            ret = fread(in_pic.img.plane[0], 1, iSize, in_fp);
            cout << "y = " << ret << endl;
            if (ret != iSize) {
                break;
            }
    
            //U
            ret = fread(in_pic.img.plane[1], 1, uv, in_fp);
            cout << "u = " << ret << endl;
            if (ret != uv)
                break;
    
            //V
            ret = fread(in_pic.img.plane[2], 1, uv, in_fp);
            cout << "v = " << ret << endl;
            if (ret != uv)
                break;
    
            in_pic.i_pts = j++;
    
            int iret = x264_encoder_encode(handle, &p_nal, &nal, &in_pic, &out_pic);
    
            for (int i = 0; i < nal; ++i) {
                fwrite(p_nal[i].p_payload, 1, p_nal[i].i_payload, out_fp);
            }
    
            printf("Succeed encode frame: %5d,in = %d, out = %d\n", j, iret, out_pic.i_dts);
        }
    
        while (x264_encoder_delayed_frames(handle)) {
    
            int iret = x264_encoder_encode(handle, &p_nal, &nal, NULL, &out_pic);
    
            for (int i = 0; i < nal; ++i) {
                fwrite(p_nal[i].p_payload, 1, p_nal[i].i_payload, out_fp);
            }
        }
    
        fclose(in_fp);
        fclose(out_fp);
        x264_encoder_close(handle);
        x264_picture_clean(&in_pic);
    //    x264_picture_clean(&out_pic);
    
        cin.get();
        return 0;
    }
    
    

    相关文章

      网友评论

          本文标题:LibX264编码库

          本文链接:https://www.haomeiwen.com/subject/yrrudrtx.html