⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 t264.c

📁 T264是中国的视频编码自由组织合力开发的264编解码程序
💻 C
📖 第 1 页 / 共 2 页
字号:
/*****************************************************************************
*
*  T264 AVC CODEC
*
*  Copyright(C) 2004-2005 llcc <lcgate1@yahoo.com.cn>
*               2004-2005 visionany <visionany@yahoo.com.cn>
*
*  This program is free software ; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation ; either version 2 of the License, or
*  (at your option) any later version.
*
*  This program is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY ; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  You should have received a copy of the GNU General Public License
*  along with this program ; if not, write to the Free Software
*  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
*
****************************************************************************/

// T264.cpp : Defines the entry point for the console application.
//

#define USE_DISPLAY
#include "config.h"

#ifdef USE_DISPLAY
#ifndef __GCC__
#include "win.h"
#else
#include "display.h"
#endif
#endif

//Ti_DSP Platform ported by YouXiaoquan,HFUT-Ti United Lab,China
//YouXiaoquan@126.com
#include "stdio.h"
#ifndef CHIP_DM642
#include "sys/timeb.h"
#endif
#include "time.h"

#include "stdlib.h"
#ifndef CHIP_DM642
#include "memory.h"
#endif
#include "math.h"
#include "T264.h"
#include "utility.h"
#include "string.h"


// parameters begin
int32_t total_no = 300;
char src_path[256];
char out_path[256];
char rec_path[256];
// for decoder PSNR
char ref_path[256];
static int  ref_skip;

// parameters end

#ifdef USE_DISPLAY

void
winDisplay(T264_t* t, T264_frame_t* f)
{
    uint8_t* p;
    unsigned char* buffer1, *buffer2, *buffer3, *Y, *U, *V;
    unsigned char *src[3];
    int32_t i;

    src[0] = Y = buffer1 = malloc(t->width*t->height*sizeof(char));
    p = f->Y[0];
    for (i = 0 ; i < t->height ; i++)
    {
        memcpy(buffer1, p, t->width);
        buffer1 += t->width;
        p += t->edged_stride;
    }

    src[2] = V = buffer2 = malloc((t->width*t->height*sizeof(char))>>2);
    p = f->V;
    for (i = 0 ; i < (t->height >> 1); i++)
    {
        memcpy(buffer2, p, t->width >> 1);
        buffer2 += (t->width >> 1);
        p += t->edged_stride_uv;
    }

    src[1] = U = buffer3 = malloc((t->width*t->height*sizeof(char))>>2);
    p = f->U;
    for (i = 0 ; i < (t->height >> 1); i++)
    {
        memcpy(buffer3, p, t->width >> 1);
        buffer3 += (t->width >> 1);
        p += t->edged_stride_uv;
    }
#ifndef __GCC__
    displayImage(Y,V,U);
#else
    dither(src);
    free(src[1]);
    free(src[2]);
    free(src[0]);
#endif
}

void
uninit_display()
{
#ifndef __GCC__
    closeDisplay ();
#else
    exit_display();
#endif
}
#endif

void
init_param(T264_param_t* param, const char* file)
{
    FILE* fd; 
    char line[255];
    int32_t b;
    if (!(fd = fopen(file,"r")))
    {
        printf("Couldn't open parameter file %s.\n", file);
        exit(-1);
    }

    memset(param, 0, sizeof(*param));
    fgets(line, 254, fd); sscanf(line,"%d", &b);
    if (b != 4)
    {
        printf("wrong param file version, expect v4.0\n");
        exit(-1);
    }
    fgets(line, 254, fd); sscanf(line,"%d", &param->width);
    fgets(line, 254, fd); sscanf(line,"%d", &param->height);
    fgets(line, 254, fd); sscanf(line,"%d", &param->search_x);
    fgets(line, 254, fd); sscanf(line,"%d", &param->search_y);
    fgets(line, 254, fd); sscanf(line,"%d", &total_no);
    fgets(line, 254, fd); sscanf(line,"%d", &param->iframe);
    fgets(line, 254, fd); sscanf(line,"%d", &param->idrframe);
    fgets(line, 254, fd); sscanf(line,"%d", &param->b_num);
    fgets(line, 254, fd); sscanf(line,"%d", &param->ref_num);
    fgets(line, 254, fd); sscanf(line,"%d", &param->enable_rc);
    fgets(line, 254, fd); sscanf(line,"%d", &param->bitrate);
    fgets(line, 254, fd); sscanf(line,"%f", &param->framerate);
    fgets(line, 254, fd); sscanf(line,"%d", &param->qp);
    fgets(line, 254, fd); sscanf(line,"%d", &param->min_qp);
    fgets(line, 254, fd); sscanf(line,"%d", &param->max_qp);
    fgets(line, 254, fd); sscanf(line,"%d", &param->enable_stat);
    fgets(line, 254, fd); sscanf(line,"%d", &param->disable_filter);
    fgets(line, 254, fd); sscanf(line,"%d", &param->aspect_ratio);
    fgets(line, 254, fd); sscanf(line,"%d", &param->video_format);
    fgets(line, 254, fd); sscanf(line,"%d", &param->luma_coeff_cost);
    fgets(line, 254, fd); sscanf(line,"%d", &b);
    param->flags |= (USE_INTRA16x16) * (!!b);
    fgets(line, 254, fd); sscanf(line,"%d", &b);
    param->flags |= (USE_INTRA4x4) * (!!b);
    fgets(line, 254, fd); sscanf(line,"%d", &b);
    param->flags |= (USE_INTRAININTER) * (!!b);
    fgets(line, 254, fd); sscanf(line,"%d", &b);
    param->flags |= (USE_HALFPEL) * (!!b);
    fgets(line, 254, fd); sscanf(line,"%d", &b);
    param->flags |= (USE_QUARTPEL) * (!!b);
    fgets(line, 254, fd); sscanf(line,"%d", &b);
    param->flags |= (USE_SUBBLOCK) * (!!b);
    fgets(line, 254, fd); sscanf(line,"%d", &b);
    param->flags |= (USE_FULLSEARCH) * (!!b);
    fgets(line, 254, fd); sscanf(line,"%d", &b);
    param->flags |= (USE_DIAMONDSEACH) * (!!b);
    fgets(line, 254, fd); sscanf(line,"%d", &b);
    param->flags |= (USE_FORCEBLOCKSIZE) * (!!b);
    fgets(line, 254, fd); sscanf(line,"%d", &b);
    param->flags |= (USE_FASTINTERPOLATE) * (!!b);
    fgets(line, 254, fd); sscanf(line,"%d", &b);
    param->flags |= (USE_SAD) * (!!b);
    fgets(line, 254, fd); sscanf(line,"%d", &b);
    param->flags |= (USE_EXTRASUBPELSEARCH) * (!!b);
    fgets(line, 254, fd); sscanf(line,"%d", &b);
    param->flags |= (USE_SCENEDETECT) * (!!b);
    fgets(line, 254, fd); sscanf(line,"%d", &b);
    param->block_size |= (SEARCH_16x16P) * (!!b);
    fgets(line, 254, fd); sscanf(line,"%d", &b);
    param->block_size |= (SEARCH_16x8P) * (!!b);
    fgets(line, 254, fd); sscanf(line,"%d", &b);
    param->block_size |= (SEARCH_8x16P) * (!!b);
    fgets(line, 254, fd); sscanf(line,"%d", &b);
    param->block_size |= (SEARCH_8x8P) * (!!b);
    fgets(line, 254, fd); sscanf(line,"%d", &b);
    param->block_size |= (SEARCH_8x4P) * (!!b);
    fgets(line, 254, fd); sscanf(line,"%d", &b);
    param->block_size |= (SEARCH_4x8P) * (!!b);
    fgets(line, 254, fd); sscanf(line,"%d", &b);
    param->block_size |= (SEARCH_4x4P) * (!!b);
    fgets(line, 254, fd); sscanf(line,"%d", &b);
    param->block_size |= (SEARCH_16x16B) * (!!b);
    fgets(line, 254, fd); sscanf(line,"%d", &b);
    param->block_size |= (SEARCH_16x8B) * (!!b);
    fgets(line, 254, fd); sscanf(line,"%d", &b);
    param->block_size |= (SEARCH_8x16B) * (!!b);
    fgets(line, 254, fd); sscanf(line,"%d", &b);
    param->block_size |= (SEARCH_8x8B) * (!!b);
    fgets(line, 254, fd); sscanf(line,"%d", &param->cpu);
    fgets(line, 254, fd); sscanf(line, "%d", &param->cabac);

    fgets(line, 254, fd); sscanf(line,"%s", src_path);
    fgets(line, 254, fd); sscanf(line,"%s", out_path);
    fgets(line, 254, fd); sscanf(line,"%s", rec_path);
    param->rec_name = rec_path;

    fclose(fd);
}

int32_t 
encode(const char* paramfile)
{
    T264_param_t param;
    T264_t* t;
    uint8_t* buf, *dst;
    int32_t size;
    FILE* in_file, *out_file;
    uint32_t len;
    int32_t frame_no;
    int32_t bs_len = 0;

    uint8_t* rec;

    float total_time;
#ifdef _WIN32
    struct _timeb beg, end;
#endif

#ifdef CHIP_DM642 
	clock_t start_T=0,end_T=0,total_T=0;
#endif
    init_param(&param, paramfile);

    param.direct_flag = 1;
    // NOTE: currently we force p reference frame num = 1
    t = T264_open(&param);

    /* YV12p */
    size = param.height * param.width + (param.height * param.width >> 1);
    buf = T264_malloc(size, CACHE_SIZE);
    in_file = fopen(src_path, "rb");
    if (!in_file)
    {
        printf("cannot open input file.");
        return 0;
    }

    out_file = fopen(out_path, "wb");
    if (!out_file)
    {
        printf("cannot write 264 file.\n");
        return 0;
    }

    dst = T264_malloc(size, CACHE_SIZE);
    rec = T264_malloc(size, CACHE_SIZE);

#ifdef _WIN32
    _ftime(&beg);
#endif
    printf("frame poc length qp Y U V\n");
    for(frame_no = 0; frame_no < total_no; frame_no++)
    {        
        if (fread(buf, size, 1, in_file) <= 0)
            printf("cannot read from input file.");
#ifdef CHIP_DM642
		start_T=clock();
#endif
        len = T264_encode(t, buf, dst, size);
#ifdef CHIP_DM642 
		end_T=clock();
		total_T=total_T+end_T-start_T-192;
#endif    
        bs_len += len;

        if (fwrite(dst, len, 1, out_file) < 0)
            printf("cannot write 264 file.\n");
    }
#ifndef CHIP_DM642
#ifdef _WIN32
    _ftime(&end);
    total_time = (float)(end.time - beg.time) + (float)(end.millitm - beg.millitm) / 1000;
#endif
    printf("fps: %.2ffps, Length of Bitstream = %d Compact Ratio = %.2f\n", (float)(total_no) / total_time, bs_len, 1.0 * total_no * size / bs_len);
#endif
#ifdef CHIP_DM642
	printf("fps: %.2ffps with cpu 600MHz, Length of Bitstream = %d Compact Ratio = %.2f\n", ((float)total_no /((float)total_T/(float)600000000)),  bs_len, 1.0 * total_no * size / bs_len);
#endif
    fclose(in_file);
    fclose(out_file);
    T264_free(dst);
    T264_close(t);

    return 0;
}

void
write_frame(T264_t* t,T264_frame_t *frame,FILE *f_rec)
{
    int	i;
    uint8_t* p;

    if (f_rec)
    {
        p = frame->Y[0];
        for(i = 0 ; i < t->height ; i ++)
        {
            fwrite(p, t->width, 1, f_rec);
            p += t->edged_stride;
        }
        p = frame->U;
        for(i = 0 ; i < t->height >> 1 ; i ++)
        {
            fwrite(p, t->width >> 1, 1, f_rec);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -