📄 ztscan_enc.cpp
字号:
/* $Id: ztscan_enc.cpp,v 1.1 2003/05/05 21:24:12 wmaycisco Exp $ *//****************************************************************************//* MPEG4 Visual Texture Coding (VTC) Mode Software *//* *//* This software was jointly developed by the following participants: *//* *//* Single-quant, multi-quant and flow control *//* are provided by Sarnoff Corporation *//* Iraj Sodagar (iraj@sarnoff.com) *//* Hung-Ju Lee (hjlee@sarnoff.com) *//* Paul Hatrack (hatrack@sarnoff.com) *//* Shipeng Li (shipeng@sarnoff.com) *//* Bing-Bing Chai (bchai@sarnoff.com) *//* B.S. Srinivas (bsrinivas@sarnoff.com) *//* *//* Bi-level is provided by Texas Instruments *//* Jie Liang (liang@ti.com) *//* *//* Shape Coding is provided by OKI Electric Industry Co., Ltd. *//* Zhixiong Wu (sgo@hlabs.oki.co.jp) *//* Yoshihiro Ueda (yueda@hlabs.oki.co.jp) *//* Toshifumi Kanamaru (kanamaru@hlabs.oki.co.jp) *//* *//* OKI, Sharp, Sarnoff, TI and Microsoft contributed to bitstream *//* exchange and bug fixing. *//* *//* *//* In the course of development of the MPEG-4 standard, this software *//* module is an implementation of a part of one or more MPEG-4 tools as *//* specified by the MPEG-4 standard. *//* *//* The copyright of this software belongs to ISO/IEC. ISO/IEC gives use *//* of the MPEG-4 standard free license to use this software module or *//* modifications thereof for hardware or software products claiming *//* conformance to the MPEG-4 standard. *//* *//* Those intending to use this software module in hardware or software *//* products are advised that use may infringe existing patents. The *//* original developers of this software module and their companies, the *//* subsequent editors and their companies, and ISO/IEC have no liability *//* and ISO/IEC have no liability for use of this software module or *//* modification thereof in an implementation. *//* *//* Permission is granted to MPEG members to use, copy, modify, *//* and distribute the software modules ( or portions thereof ) *//* for standardization activity within ISO/IEC JTC1/SC29/WG11. *//* *//* Copyright 1995, 1996, 1997, 1998 ISO/IEC *//****************************************************************************//************************************************************//* Sarnoff Very Low Bit Rate Still Image Coder *//* Copyright 1995, 1996, 1997, 1998 Sarnoff Corporation *//************************************************************//************************************************************//* Filename: ztscan_enc.c *//* Author: Bing-Bing Chai *//* Date: Dec. 4, 1997 *//* *//* Descriptions: *//* This file contains the routines that performs *//* zero tree scanning and entropy encoding. *//* *//************************************************************/#include <stdio.h>#include <stdlib.h>#ifndef WIN32#include <unistd.h>#endif#include <ctype.h>#include <string.h>#include <math.h>#include "basic.hpp"#include "startcode.hpp"#include "dataStruct.hpp"#include "states.hpp"#include "globals.hpp"#include "errorHandler.hpp"#include "ac.hpp"#include "bitpack.hpp"#include "msg.hpp"#include "ztscan_common.hpp"#include "ztscanUtil.hpp"// added for FDAM1 by Samsung AIT on 2000/02/03#ifndef _SHAPE_#define _SHAPE_#endif// ~added for FDAM1 by Samsung AIT 2000/02/03/* local global variables */static ac_encoder ace;static int bit_stream_length;static SInt **dc_coeff;/* added by Z. Wu @ OKI for SA-prediction */static Char **dc_mask;static COEFFINFO **coeffinfo;static Int color;static Int height, width;/******************************************************************//**************************** DC ********************************//******************************************************************//*******************************************************//************** Forward DC Prediction ****************//*******************************************************//******************************************************** Function Name ------------- static DATA DC_pred_pix(Int i, Int j) Arguments --------- Int i, Int j: Index of wavelet coefficient (row, col) Description ----------- DPCM prediction for a DC coefficient, refer to syntax for algorithm. Functions Called ---------------- None. Return Value ------------ prediction for coeffinfo[i][j].quantized_value********************************************************/ SInt CVTCEncoder::DC_pred_pix(Int i, Int j){//Added by Sarnoff for error resilience, 3/5/99#ifdef _DC_PACKET_ if(!mzte_codec.m_usErrResiDisable) // error resilience doesn't use prediction return 0;#endif //End: Added by Sarnoff for error resilience, 3/5/99 /* modified by Z. Wu @ OKI */ Int pred_i, pred_j, pred_d; if ( i==0 || dc_mask[i-1][j] == 0 ) pred_i = 0; else pred_i = dc_coeff[i-1][j]; if ( j==0 || dc_mask[i][j-1] == 0 ) pred_j = 0; else pred_j = dc_coeff[i][j-1]; if ( i==0 || j== 0 || dc_mask[i-1][j-1] == 0 ) pred_d = 0; else pred_d = dc_coeff[i-1][j-1]; if ( abs(pred_d-pred_j) < abs(pred_d-pred_i)) return(pred_i); else return(pred_j);}/***************************************************** Function Name ------------- Void DC_predict() Arguments --------- None Description ----------- control program for DC prediction Functions Called ---------------- DC_pred_pix(i,j). Return Value ------------ none******************************************************/Void CVTCEncoder::DC_predict(Int color){ Int i,j,dc_h,dc_w,offset_dc,max_dc; dc_h=mzte_codec.m_iDCHeight; dc_w=mzte_codec.m_iDCWidth; dc_coeff=(SInt **)calloc(dc_h,sizeof(SInt *)); for(i=0;i<dc_h;i++) dc_coeff[i]=(SInt *)calloc(dc_w,sizeof(SInt)); dc_mask=(Char **)calloc(dc_h,sizeof(Char *)); for(i=0;i<dc_h;i++) dc_mask[i]=(Char *)calloc(dc_w,sizeof(Char)); coeffinfo=mzte_codec.m_SPlayer[color].coeffinfo; for(i=0;i<dc_h;i++) for(j=0;j<dc_w;j++) { dc_coeff[i][j]=coeffinfo[i][j].quantized_value; dc_mask [i][j]=coeffinfo[i][j].mask; } /* prediction */ offset_dc=0; for(i=0;i<dc_h;i++) for(j=0;j<dc_w;j++){ if ( dc_mask[i][j] != 0 ) { if(offset_dc>(coeffinfo[i][j].quantized_value-=DC_pred_pix(i,j))) offset_dc=coeffinfo[i][j].quantized_value; } } if(offset_dc>0) offset_dc=0; /* adjust coeff's by offset_dc */ max_dc=0; for(i=0;i<dc_h;i++) for(j=0;j<dc_w;j++){ if ( dc_mask[i][j] != 0 ) { coeffinfo[i][j].quantized_value -=offset_dc; /* find max_dc */ if (max_dc<coeffinfo[i][j].quantized_value) max_dc=coeffinfo[i][j].quantized_value; } } mzte_codec.m_iOffsetDC=offset_dc; mzte_codec.m_iMaxDC=max_dc; /* hjlee */ noteDebug("DC pred: offset=%d, max_dc=%d", mzte_codec.m_iOffsetDC,mzte_codec.m_iMaxDC); for(i=0;i<dc_h;i++) { free(dc_coeff[i]); free(dc_mask[i]); } free(dc_coeff); free(dc_mask);}/******************************************************** Function Name ------------- Void wavelet_dc_encode(Int c) Arguments --------- Int c - color component. Description ----------- Control program for encode DC information for one color component. Functions Called ---------------- None. DC_predict() put_param() cacll_encode() Return Value ------------ None.********************************************************/ Void CVTCEncoder::wavelet_dc_encode(Int c){ noteDetail("Encoding DC (wavelet_dc_encode)...."); color=c;//Added by Sarnoff for error resilience, 3/5/99#ifdef _DC_PACKET_ if(!mzte_codec.m_usErrResiDisable) acmSGMK.Max_frequency = Bitplane_Max_frequency;#endif//End: Added by Sarnoff for error resilience, 3/5/99 emit_bits((UShort)mzte_codec.m_iMean[color], 8); put_param((UShort)mzte_codec.m_iQDC[color], 7); /* emit_bits(mzte_codec.Qdc[color], 8); */ DC_predict(color); put_param(-mzte_codec.m_iOffsetDC,7); put_param(mzte_codec.m_iMaxDC,7); /* hjlee */ /* put_param(mzte_codec.m_iMaxDC+mzte_codec.m_iOffsetDC,7); */ /* hjlee */ //printf("%d %d %d %d\n", mzte_codec.m_iMean[color],mzte_codec.m_iQDC[color],-mzte_codec.m_iOffsetDC,mzte_codec.m_iMaxDC); cacll_encode(); noteDetail("Completed encoding DC.");}/******************************************************** Function Name ------------- static Void cacll_encode() Arguments --------- None. Description ----------- Encode DC information for one color component. Functions Called ---------------- mzte_ac_encoder_init() mzte_ac_model_init() mzte_ac_encode_symbol() mzte_ac_model_done() mzte_ac_encoder_done() Return Value ------------ None.********************************************************/ Void CVTCEncoder::cacll_encode(){ Int dc_h, dc_w,i,j; Int numBP, bp; // 1124 dc_w=mzte_codec.m_iDCWidth; dc_h=mzte_codec.m_iDCHeight; // 1124 /* init arithmetic coder */ numBP = ceilLog2(mzte_codec.m_iMaxDC+1); // modified by Sharp (99/2/16) mzte_ac_encoder_init(&ace); if ((acm_bpdc=(ac_model *)calloc(numBP,sizeof(ac_model)))==NULL) errorHandler("Can't allocate memory for prob model."); for (i=0; i<numBP; i++) { acm_bpdc[i].Max_frequency = Bitplane_Max_frequency; mzte_ac_model_init(&(acm_bpdc[i]),2,NULL,ADAPT,1); } coeffinfo=mzte_codec.m_SPlayer[color].coeffinfo;//Modified by Sarnoff for error resilience, 3/5/99 if(mzte_codec.m_usErrResiDisable){ //no error resi case for (bp=numBP-1; bp>=0; bp--) { for(i=0;i<dc_h;i++) for(j=0;j<dc_w;j++){ // printf("%d %d \n", i,j); if( coeffinfo[i][j].mask == 1) mzte_ac_encode_symbol(&ace, &(acm_bpdc[bp]), ((coeffinfo[i][j].quantized_value)>>bp)&1); } } /* close arithmetic coder */ for (i=0; i<numBP; i++) mzte_ac_model_done(&(acm_bpdc[i])); free(acm_bpdc); bit_stream_length=mzte_ac_encoder_done(&ace); } else{ //error resilience case#ifdef _DC_PACKET_ TU_max_dc += numBP; mzte_ac_model_init(&acmType[color][0][CONTEXT_INIT],NUMCHAR_TYPE, NULL,ADAPT,1); for (bp=numBP-1; bp>=0; bp--) { for(i=0;i<dc_h;i++){ for(j=0;j<dc_w;j++){ if( coeffinfo[i][j].mask == 1) mzte_ac_encode_symbol(&ace, &(acm_bpdc[bp]), ((coeffinfo[i][j].quantized_value)>>bp)&1); } check_segment_size(color); } check_end_of_DC_packet(numBP); } /* close arithmetic coder */ if (packet_size+ace.bitCount+ace.followBits>0){ for (i=0; i<numBP; i++) mzte_ac_model_done(&(acm_bpdc[i])); mzte_ac_model_done(&(acmType[color][0][CONTEXT_INIT])); free(acm_bpdc); bit_stream_length=mzte_ac_encoder_done(&ace); }#else //same as no error resi case for (bp=numBP-1; bp>=0; bp--) { for(i=0;i<dc_h;i++) for(j=0;j<dc_w;j++){ // printf("%d %d \n", i,j); if( coeffinfo[i][j].mask == 1) mzte_ac_encode_symbol(&ace, &(acm_bpdc[bp]), ((coeffinfo[i][j].quantized_value)>>bp)&1); } } /* close arithmetic coder */ for (i=0; i<numBP; i++) mzte_ac_model_done(&(acm_bpdc[i])); free(acm_bpdc); bit_stream_length=mzte_ac_encoder_done(&ace);#endif }//End: Added by Sarnoff for error resilience, 3/5/99}/*********************************************************************//***************************** AC **********************************//********************* Single and Multi Quant **********************//*********************************************************************/Void CVTCEncoder::bitplane_encode(Int val,Int l,Int max_bplane){ register int i,k=0; for(i=max_bplane-1;i>=0;i--,k++) mzte_ac_encode_symbol(&ace,&acm_bpmag[l][k],(val>>i)&1);}/*********************************************************************//***************************** AC **********************************//************************* Single quant ****************************//*********************************************************************//******************************************************* The following single quant routines are for band by band scan order.*******************************************************//******************************************************** Function Name ------------- Void wavelet_higher_bands_encode_SQ_band(Int col) Arguments --------- None. Description ----------- Control program for encoding AC information for one color component. Single quant mode. Functions Called ---------------- cachb_encode_SQ_band() mzte_ac_encoder_init() mzte_ac_model_init() mzte_ac_model_done() mzte_ac_encoder_done() Return Value ------------ None.********************************************************/ Void CVTCEncoder::wavelet_higher_bands_encode_SQ_band(Int col){ SNR_IMAGE *snr_image; noteDetail("Encoding AC (wavelet_higher_bands_encode_SQ)...."); color=col; snr_image=&(mzte_codec.m_SPlayer[color].SNRlayer.snr_image); //Modified by Sarnoff for error resilience, 3/5/99 if(mzte_codec.m_usErrResiDisable){ //no error resi case /* init arithmetic coder */ mzte_ac_encoder_init(&ace); probModelInitSQ(color); // hjlee 0901 cachb_encode_SQ_band(snr_image); probModelFreeSQ(color); // hjlee 0901 bit_stream_length=mzte_ac_encoder_done(&ace); } else{ //error resilience case /* init arithmetic coder */ init_arith_encoder_model(color); cachb_encode_SQ_band(snr_image); if (packet_size+ace.bitCount>0) { /* if color is V then write packet header */ TU_last--; close_arith_encoder_model(color, mzte_codec.m_iCurSpatialLev==0 || color==2); if (mzte_codec.m_iCurSpatialLev==0 || color==2) force_end_of_packet(); else TU_last++; } }//End modified by Sarnoff for error resilience, 3/5/99 noteDetail("Completed encoding AC.");}/********************************************************
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -