📄 pjmplayerdlg.cpp
字号:
// PJMPlayerDlg.cpp : implementation file
//
#include "stdafx.h"
#include "PJMPlayer.h"
#include "PJMPlayerDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//支持JM的包含文件以及全局变量函数
#include "contributors.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <assert.h>
extern "C"
{
#include "global.h"
#include "rtp.h"
#include "memalloc.h"
#include "mbuffer.h"
#include "leaky_bucket.h"
#include "fmo.h"
#include "annexb.h"
#include "output.h"
#include "cabac.h"
#include "erc_api.h"
}
#define JM "10 (FRExt)"
#define VERSION "10.1"
#define EXT_VERSION "(FRExt)"
#define LOGFILE "log.dec"
#define DATADECFILE "dataDec.txt"
#define TRACEFILE "trace_dec.txt"
//C++方式编译的函数和C方式编译函数连接的符号不同
extern objectBuffer_t *erc_object_list;
extern "C" ercVariables_t *erc_errorVar;
extern "C" ColocatedParams *Co_located;
// I have started to move the inp and img structures into global variables.
// They are declared in the following lines. Since inp is defined in conio.h
// and cannot be overridden globally, it is defined here as input
//
// Everywhere, input-> and img-> can now be used either globally or with
// the local override through the formal parameter mechanism
extern FILE* bits;
extern "C" StorablePicture* dec_picture;
struct inp_par *input; //!< input parameters from input configuration file
struct snr_par *snr; //!< statistics
struct img_par *img; //!< image parameters
char displaybuf[80000];
char Ready;
int global_init_done = 0;
/************************************************************************
* \brief
* Allocates a stand-alone partition structure. Structure should
* be freed by FreePartition();
* data structures
*
* \par Input:
* n: number of partitions in the array
* \par return
* pointer to DataPartition Structure, zero-initialized
************************************************************************
*/
DataPartition *AllocPartition(int n)
{
DataPartition *partArr, *dataPart;
int i;
partArr = (DataPartition *) calloc(n, sizeof(DataPartition));
if (partArr == NULL)
{
snprintf(errortext, ET_SIZE, "AllocPartition: Memory allocation for Data Partition failed");
error(errortext, 100);
}
for (i=0; i<n; i++) // loop over all data partitions
{
dataPart = &(partArr[i]);
dataPart->bitstream = (Bitstream *) calloc(1, sizeof(Bitstream));
if (dataPart->bitstream == NULL)
{
snprintf(errortext, ET_SIZE, "AllocPartition: Memory allocation for Bitstream failed");
error(errortext, 100);
}
dataPart->bitstream->streamBuffer = (byte *) calloc(MAX_CODED_FRAME_SIZE, sizeof(byte));
if (dataPart->bitstream->streamBuffer == NULL)
{
snprintf(errortext, ET_SIZE, "AllocPartition: Memory allocation for streamBuffer failed");
error(errortext, 100);
}
}
return partArr;
}
/*!
************************************************************************
* \brief
* Frees a partition structure (array).
*
* \par Input:
* Partition to be freed, size of partition Array (Number of Partitions)
*
* \par return
* None
*
* \note
* n must be the same as for the corresponding call of AllocPartition
************************************************************************
*/
void FreePartition (DataPartition *dp, int n)
{
int i;
assert (dp != NULL);
assert (dp->bitstream != NULL);
assert (dp->bitstream->streamBuffer != NULL);
for (i=0; i<n; i++)
{
free (dp[i].bitstream->streamBuffer);
free (dp[i].bitstream);
}
free (dp);
}
/*!
************************************************************************
* \brief
* Allocates the slice structure along with its dependent
* data structures
*
* \par Input:
* Input Parameters struct inp_par *inp, struct img_par *img
************************************************************************
*/
void malloc_slice(struct inp_par *inp, struct img_par *img)
{
Slice *currSlice;
img->currentSlice = (Slice *) calloc(1, sizeof(Slice));
if ( (currSlice = img->currentSlice) == NULL)
{
snprintf(errortext, ET_SIZE, "Memory allocation for Slice datastruct in NAL-mode %d failed", inp->FileFormat);
error(errortext,100);
}
// img->currentSlice->rmpni_buffer=NULL;
//! you don't know whether we do CABAC hre, hence initialize CABAC anyway
// if (inp->symbol_mode == CABAC)
if (1)
{
// create all context models
currSlice->mot_ctx = create_contexts_MotionInfo();
currSlice->tex_ctx = create_contexts_TextureInfo();
}
currSlice->max_part_nr = 3; //! assume data partitioning (worst case) for the following mallocs()
currSlice->partArr = AllocPartition(currSlice->max_part_nr);
}
/*!
************************************************************************
* \brief
* Memory frees of the Slice structure and of its dependent
* data structures
*
* \par Input:
* Input Parameters struct inp_par *inp, struct img_par *img
************************************************************************
*/
void free_slice(struct inp_par *inp, struct img_par *img)
{
Slice *currSlice = img->currentSlice;
FreePartition (currSlice->partArr, 3);
// if (inp->symbol_mode == CABAC)
if (1)
{
// delete all context models
delete_contexts_MotionInfo(currSlice->mot_ctx);
delete_contexts_TextureInfo(currSlice->tex_ctx);
}
free(img->currentSlice);
currSlice = NULL;
}
/*!
************************************************************************
* \brief
* Dynamic memory allocation of frame size related global buffers
* buffers are defined in global.h, allocated memory must be freed in
* void free_global_buffers()
*
* \par Input:
* Input Parameters struct inp_par *inp, Image Parameters struct img_par *img
*
* \par Output:
* Number of allocated bytes
***********************************************************************
*/
int init_global_buffers()
{
int memory_size=0;
int quad_range, i;
if (global_init_done)
{
free_global_buffers();
}
// allocate memory for reference frame in find_snr
memory_size += get_mem2Dpel(&imgY_ref, img->height, img->width);
if (active_sps->chroma_format_idc != YUV400)
memory_size += get_mem3Dpel(&imgUV_ref, 2, img->height_cr, img->width_cr);
else
imgUV_ref=NULL;
// allocate memory in structure img
if(((img->mb_data) = (Macroblock *) calloc(img->FrameSizeInMbs, sizeof(Macroblock))) == NULL)
no_mem_exit("init_global_buffers: img->mb_data");
if(((img->intra_block) = (int*)calloc(img->FrameSizeInMbs, sizeof(int))) == NULL)
no_mem_exit("init_global_buffers: img->intra_block");
memory_size += get_mem2Dint(&(img->ipredmode), 4*img->PicWidthInMbs , 4*img->FrameHeightInMbs);
memory_size += get_mem2Dint(&(img->field_anchor),4*img->FrameHeightInMbs, 4*img->PicWidthInMbs);
memory_size += get_mem3Dint(&(img->wp_weight), 2, MAX_REFERENCE_PICTURES, 3);
memory_size += get_mem3Dint(&(img->wp_offset), 6, MAX_REFERENCE_PICTURES, 3);
memory_size += get_mem4Dint(&(img->wbp_weight), 6, MAX_REFERENCE_PICTURES, MAX_REFERENCE_PICTURES, 3);
// CAVLC mem
memory_size += get_mem3Dint(&(img->nz_coeff), img->FrameSizeInMbs, 4, 4 + img->num_blk8x8_uv);
memory_size += get_mem2Dint(&(img->siblock),img->PicWidthInMbs , img->FrameHeightInMbs);
if(img->max_imgpel_value > img->max_imgpel_value_uv || active_sps->chroma_format_idc == YUV400)
quad_range = (img->max_imgpel_value + 1) * 2;
else
quad_range = (img->max_imgpel_value_uv + 1) * 2;
if ((img->quad = (int*)calloc (quad_range, sizeof(int))) == NULL)
no_mem_exit ("init_img: img->quad");
for (i=0; i < quad_range/2; ++i)
{
img->quad[i]=i*i;
}
global_init_done = 1;
img->oldFrameSizeInMbs = img->FrameSizeInMbs;
return (memory_size);
}
/*!
************************************************************************
* \brief
* Free allocated memory of frame size related global buffers
* buffers are defined in global.h, allocated memory is allocated in
* int init_global_buffers()
*
* \par Input:
* Input Parameters struct inp_par *inp, Image Parameters struct img_par *img
*
* \par Output:
* none
*
************************************************************************
*/
void free_global_buffers()
{
free_mem2Dpel (imgY_ref);
if (imgUV_ref)
free_mem3Dpel (imgUV_ref,2);
// CAVLC free mem
free_mem3Dint(img->nz_coeff, img->oldFrameSizeInMbs);
free_mem2Dint(img->siblock);
// free mem, allocated for structure img
if (img->mb_data != NULL) free(img->mb_data);
free (img->intra_block);
free_mem2Dint (img->ipredmode);
free_mem2Dint(img->field_anchor);
free_mem3Dint(img->wp_weight, 2);
free_mem3Dint(img->wp_offset, 6);
free_mem4Dint(img->wbp_weight, 6, MAX_REFERENCE_PICTURES);
free (img->quad);
global_init_done = 0;
}
void Configure(int ac, char *av[])
{
int CLcount;
char *config_filename=NULL;
CLcount = 1;
strcpy(input->infile,"test.264"); //! set default bitstream name
strcpy(input->outfile,"test_dec.yuv"); //! set default output file name
strcpy(input->reffile,"test_rec.yuv"); //! set default reference file name
input->FileFormat = PAR_OF_ANNEXB;
input->ref_offset=0;
input->poc_scale=1;
#ifdef _LEAKYBUCKET_
input->R_decoder=500000; //! Decoder rate
input->B_decoder=104000; //! Decoder buffer size
input->F_decoder=73000; //! Decoder initial delay
strcpy(input->LeakyBucketParamFile,"leakybucketparam.cfg"); // file where Leaky Bucket params (computed by encoder) are stored
#endif
/*
if (ac==2)
{
if (0 == strncmp (av[1], "-h", 2))
{
JMDecHelpExit();
}
else
{
config_filename=av[1];
init_conf(input, av[1]);
}
CLcount=2;
}
if (ac>=3)
{
if (0 == strncmp (av[1], "-i", 2))
{
strcpy(input->infile,av[2]);
CLcount = 3;
}
if (0 == strncmp (av[1], "-h", 2))
{
JMDecHelpExit();
}
}
// Parse the command line
while (CLcount < ac)
{
if (0 == strncmp (av[CLcount], "-h", 2))
{
JMDecHelpExit();
}
if (0 == strncmp (av[CLcount], "-i", 2)) //! Input file
{
strcpy(input->infile,av[CLcount+1]);
CLcount += 2;
}
else if (0 == strncmp (av[CLcount], "-o", 2)) //! Output File
{
strcpy(input->outfile,av[CLcount+1]);
CLcount += 2;
}
else if (0 == strncmp (av[CLcount], "-r", 2)) //! Reference File
{
strcpy(input->reffile,av[CLcount+1]);
CLcount += 2;
}
else if (0 == strncmp (av[CLcount], "-uv", 2)) //! indicate UV writing for 4:0:0
{
input->write_uv = 1;
CLcount ++;
}
else
{
//config_filename=av[CLcount];
//init_conf(input, config_filename);
snprintf(errortext, ET_SIZE, "Invalid syntax. Use ldecod -h for proper usage");
error(errortext, 300);
}
}
*/
#if TRACE
if ((p_trace=fopen(TRACEFILE,"w"))==0) // append new statistic at the end
{
snprintf(errortext, ET_SIZE, "Error open file %s!",TRACEFILE);
error(errortext,500);
}
#endif
p_out=open(input->outfile, "wb");
if (p_out==0)
{
snprintf(errortext, ET_SIZE, "Error open file %s ",input->outfile);
error(errortext,500);
}
/* if ((p_out2=fopen("out.yuv","wb"))==0)
{
snprintf(errortext, ET_SIZE, "Error open file %s ",input->outfile);
error(errortext,500);
}*/
fprintf(stdout,"----------------------------- JM %s %s -----------------------------\n", VERSION, EXT_VERSION);
fprintf(stdout," Decoder config file : %s \n",config_filename);
fprintf(stdout,"--------------------------------------------------------------------------\n");
fprintf(stdout," Input H.264 bitstream : %s \n",input->infile);
fprintf(stdout," Output decoded YUV : %s \n",input->outfile);
fprintf(stdout," Output status file : %s \n",LOGFILE);
p_ref=open(input->reffile,"rb");
if (!p_ref)
{
fprintf(stdout," Input reference file : %s does not exist \n",input->reffile);
fprintf(stdout," SNR values are not available\n");
}
else
fprintf(stdout," Input reference file : %s \n",input->reffile);
fprintf(stdout,"--------------------------------------------------------------------------\n");
#ifdef _LEAKYBUCKET_
fprintf(stdout," Rate_decoder : %8ld \n",input->R_decoder);
fprintf(stdout," B_decoder : %8ld \n",input->B_decoder);
fprintf(stdout," F_decoder : %8ld \n",input->F_decoder);
fprintf(stdout," LeakyBucketParamFile: %s \n",input->LeakyBucketParamFile); // Leaky Bucket Param file
calc_buffer(input);
fprintf(stdout,"--------------------------------------------------------------------------\n");
#endif
fprintf(stdout,"POC must = frame# or field# for SNRs to be correct\n");
fprintf(stdout,"--------------------------------------------------------------------------\n");
fprintf(stdout," Frame POC Pic# QP SnrY SnrU SnrV Y:U:V Time(ms)\n");
fprintf(stdout,"--------------------------------------------------------------------------\n");
}
/*!
***********************************************************************
* \brief
* Initilize some arrays
***********************************************************************
*/
void init(struct img_par *img) //!< image parameters
{
img->oldFrameSizeInMbs = -1;
imgY_ref = NULL;
imgUV_ref = NULL;
}
/*!
***********************************************************************
* \brief
* Initilize FREXT variables
***********************************************************************
*/
void init_frext(struct img_par *img) //!< image parameters
{
//pel bitdepth init
img->bitdepth_luma_qp_scale = 6*(img->bitdepth_luma - 8);
if(img->bitdepth_luma > img->bitdepth_chroma || active_sps->chroma_format_idc == YUV400)
img->pic_unit_bitsize_on_disk = (img->bitdepth_luma > 8)? 16:8;
else
img->pic_unit_bitsize_on_disk = (img->bitdepth_chroma > 8)? 16:8;
img->dc_pred_value = 1<<(img->bitdepth_luma - 1);
img->max_imgpel_value = (1<<img->bitdepth_luma) - 1;
if (active_sps->chroma_format_idc != YUV400)
{
//for chrominance part
img->bitdepth_chroma_qp_scale = 6*(img->bitdepth_chroma - 8);
img->max_imgpel_value_uv = (1<<img->bitdepth_chroma) - 1;
img->num_blk8x8_uv = (1<<active_sps->chroma_format_idc)&(~(0x1));
img->num_cdc_coeff = img->num_blk8x8_uv<<1;
img->mb_cr_size_x = (active_sps->chroma_format_idc==YUV420 || active_sps->chroma_format_idc==YUV422)? 8:16;
img->mb_cr_size_y = (active_sps->chroma_format_idc==YUV444 || active_sps->chroma_format_idc==YUV422)? 16:8;
// Residue Color Transform
if(img->residue_transform_flag)
img->bitdepth_chroma_qp_scale += 6;
}
else
{
img->bitdepth_chroma_qp_scale = 0;
img->max_imgpel_value_uv = 0;
img->num_blk8x8_uv = 0;
img->num_cdc_coeff = 0;
img->mb_cr_size_x = 0;
img->mb_cr_size_y = 0;
}
}
/*!
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -