h.265 Code Library x265 Instance

Source: Internet
Author: User
Tags flush fread goto

x265 is an open source h.265 encoding library. API interface is very similar to x264. This article describes its compilation methods in x86 Linux and Windows 7, and gives examples. First, compile 1. Linux Platform

x265 compilation needs to be used to yasm, to http://www.tortall.net/projects/yasm/releases/download, the latest version is 1.3.1. The compile command is as follows:

#./configure;make;make Install

x265 Library Download: https://bitbucket.org/multicoreware/x265/downloads, the latest version is 1.7.

After extracting, go to the source directory's build directory and choose Linux.

# CD X265_1.7/build/linux
#./make-makefiles.bash

The configuration is as follows:
bin_install_dir/home/latelee/bin/x265-1.7
Checked_build OFF
Cmake_build_type Release
cmake_install_prefix/home/latelee/bin/x265-1.7
Detailed_cu_stats OFF
Enable_aggressive_checks OFF
Enable_assembly on
ENABLE_CLI on
Enable_pic OFF
Enable_ppa OFF
enable_shared OFF # no Dynamic library
Enable_tests OFF
Enable_vtune OFF

Enable_shared:build SHARED Library
Press [Enter] to edit option CMake Version 3.3.1
Press [C] to configure
Press [h] for help press [Q] to quit without generating
Press [T] to toggle Advanced Mode (currently OFF)

Press C to configure, then press G to save and exit.


2. Windows platform

0, install CMake version of Windows, Address: http://www.cmake.org/download/. During the installation process remember to add cmake to path.
1, enter the build directory, according to the compiler version to choose different Engineering directory, such as 64bit VS2010, into the vc10-x86_64 directory. Double-click: Build-all.bat.
2, after the configuration interface appears, click Configure, then click Generate, and then close the window. Other different to do.
3. Wait for the cmd window to be compiled and close automatically when finished.
4, found in the Debug or release directory h265 encoder X265.exe, in addition to DLL and LIB files. The header file to the source code to find.

Note: Compiling the Windows platform is purely to get the X265,exe file.
Second, examples

The example that is used is given below.

/** x265 Library encoding to use the following library-LPTHREAD-LDL ffprobe information: Input #0, HEVC, from ' test.h265 ': duration:n/a, bitrate:n/a Stream #0 : 0:video:hevc (Main), yuv420p (TV), 176x144, FPS, TBR, 1200k TBN, TBC */#include <stdio.h> #include &lt ;stdlib.h> #include "x265.h" int x265_encode (const char* infile, int width, int height, int type, const char* outfile
	{FILE *fp_src = NULL;
    FILE *fp_dst = NULL;
    unsigned int luma_size = 0;
	unsigned int chroma_size = 0;
	int buff_size = 0;
	char *buff = NULL;
    uint32_t i_nal=0;
    int i_frame=0;

    int ret = 0;
    X265_param param;
	X265_nal *nal = NULL;
	X265_encoder *handle = NULL;

    X265_picture *pic_in = NULL; int CSP = type;

	x265_csp_i420;
	FP_SRC = fopen (infile, "RB");
	FP_DST = fopen (outfile, "WB");
		if (Fp_src==null | | fp_dst==null) {perror ("Error Open YUV Files:");
	return-1;
	} x265_param_default (¶m);
	Param.brepeatheaders = 1;//write Sps,pps before keyframe param.internalcsp = CSP; ParAm.sourcewidth = width;
	Param.sourceheight = height; Param.fpsnum = 25; Frame rate param.fpsdenom = 1;
	Frame rate handle = X265_encoder_open (¶m);
		if (handle = = NULL) {printf ("X265_encoder_open err\n");
	Goto out;
    } pic_in = X265_picture_alloc ();
    if (pic_in = = NULL) {goto out;

    } x265_picture_init (¶m, pic_in);
    Y component Size luma_size = Param.sourcewidth * param.sourceheight;
        Component one frame YUV space switch (CSP) {Case X265_csp_i444:buff = (char *) malloc (luma_size*3);
        Pic_in->planes[0] = buff;
        PIC_IN-&GT;PLANES[1] = buff + luma_size;
        PIC_IN-&GT;PLANES[2] = buff + luma_size*2;
        Pic_in->stride[0] = width;
        PIC_IN-&GT;STRIDE[1] = width;
        PIC_IN-&GT;STRIDE[2] = width;
    Break
        Case X265_csp_i420:buff = (char *) malloc (LUMA_SIZE*3/2);
        Pic_in->planes[0] = buff;
        PIC_IN-&GT;PLANES[1] = buff + luma_size;
        PIC_IN-&GT;PLANES[2] = buff + LUMA_SIZE*5/4; Pic_in->stride[0] = width;
        PIC_IN-&GT;STRIDE[1] = WIDTH/2;
        PIC_IN-&GT;STRIDE[2] = WIDTH/2;
    Break
        default:printf ("ColorSpace not support.\n");
	Goto out;
    }//Calculates the total number of frames fseek (fp_src, 0, seek_end);
        Switch (CSP) {Case x265_csp_i444:i_frame = Ftell (FP_SRC)/(LUMA_SIZE*3);
        Chroma_size = luma_size;
    Break
        Case x265_csp_i420:i_frame = Ftell (FP_SRC)/(LUMA_SIZE*3/2);
        Chroma_size = LUMA_SIZE/4;
    Break
        default:printf ("ColorSpace not support.\n");
    return-1;
    } fseek (Fp_src,0,seek_set);

	printf ("framecnt:%d, y:%d u:%d\n", I_frame, Luma_size, chroma_size); for (int i=0, i < i_frame; i++) {switch (CSP) {case X265_csp_i444:case x265_csp_i420:if (
			Fread (Pic_in->planes[0], 1, luma_size, fp_src)! = luma_size) break;
           if (Fread (pic_in->planes[1], 1, chroma_size, fp_src)! = chroma_size)     Break
			if (Fread (pic_in->planes[2], 1, chroma_size, fp_src)! = chroma_size) break;
		Break
			default:printf ("ColorSpace not support.\n");
		Goto out;	
		} ret = X265_encoder_encode (handle, &nal, &i_nal, pic_in, NULL);
        printf ("Encode frame:%5d framesize:%d nal:%d\n", i+1, ret, i_nal);
            if (Ret < 0) {printf ("Error encode frame:%d.\n", i+1);
        Goto out;
		} for (uint32_t j = 0; J < I_nal; J + +) {fwrite (nal[j].payload, 1, nal[j].sizebytes, FP_DST); }}//Flush Decoder while (ret = X265_encoder_encode (handle, &nal, &i_nal, NULL, NULL))) {static
		int cnt = 1;
		printf ("Flush frame:%d\n", cnt++);
		for (uint32_t j=0; j < I_nal; J + +) {fwrite (nal[j].payload, 1, nal[j].sizebytes, FP_DST);
	}} out:x265_encoder_close (handle);
    X265_picture_free (pic_in);

	if (buff) free (buff);
	Fclose (FP_SRC);
	
	Fclose (FP_DST);
return 0; }

How to use:

X265_encode (".. /src/suzie_qcif_176x144_yuv420p.yuv ", 176, 144, 1," test.h265 ");

Makefile See the previous article.

Li Yu 2015.9.14 Noon

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.