1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
|
/* putvlc.c, generation of variable length codes */
/* Copyright (C) 1996, MPEG Software Simulation Group. All Rights Reserved. */
/*
* Disclaimer of Warranty
*
* These software programs are available to the user without any license fee or
* royalty on an "as is" basis. The MPEG Software Simulation Group disclaims
* any and all warranties, whether express, implied, or statuary, including any
* implied warranties or merchantability or of fitness for a particular
* purpose. In no event shall the copyright-holder be liable for any
* incidental, punitive, or consequential damages of any kind whatsoever
* arising from the use of these programs.
*
* This disclaimer of warranty extends to the user of these programs and user's
* customers, employees, agents, transferees, successors, and assigns.
*
* The MPEG Software Simulation Group does not represent or warrant that the
* programs furnished hereunder are free of infringement of any third-party
* patents.
*
* Commercial implementations of MPEG-1 and MPEG-2 video, including shareware,
* are subject to royalty fees to patent holders. Many of these patents are
* general enough such that they are unavoidable regardless of implementation
* design.
*
*/
#include <stdio.h>
#include "mpeg2enc_config.h"
#include "mpeg2enc_global.h"
#include "vlc.h"
/* private prototypes */
static void putDC _ANSI_ARGS_((sVLCtable *tab, int val,struct MPEG2_structure *mpeg2_struct));
/* generate variable length code for luminance DC coefficient */
void MPEG2_putDClum(val,mpeg2_struct)
int val;
struct MPEG2_structure *mpeg2_struct;
{
putDC(DClumtab,val,mpeg2_struct);
}
/* generate variable length code for chrominance DC coefficient */
void MPEG2_putDCchrom(val,mpeg2_struct)
int val;
struct MPEG2_structure *mpeg2_struct;
{
putDC(DCchromtab,val,mpeg2_struct);
}
/* generate variable length code for DC coefficient (7.2.1) */
static void putDC(tab,val,mpeg2_struct)
sVLCtable *tab;
int val;
struct MPEG2_structure *mpeg2_struct;
{
int absval, size;
absval = (val<0) ? -val : val; /* abs(val) */
if (absval>2047 || (mpeg2_struct->mpeg1 && absval>255))
{
/* should never happen */
sprintf(mpeg2_struct->errortext,"DC value out of range (%d)\n",val);
(*(mpeg2_struct->report_error))(mpeg2_struct->errortext);
}
/* compute dct_dc_size */
size = 0;
while (absval)
{
absval >>= 1;
size++;
}
/* generate VLC for dct_dc_size (Table B-12 or B-13) */
MPEG2_putbits(tab[size].code,tab[size].len,mpeg2_struct);
/* append fixed length code (dc_dct_differential) */
if (size!=0)
{
if (val>=0)
absval = val;
else
absval = val + (1<<size) - 1; /* val + (2 ^ size) - 1 */
MPEG2_putbits(absval,size,mpeg2_struct);
}
}
/* generate variable length code for first coefficient
* of a non-intra block (7.2.2.2) */
void MPEG2_putACfirst(run,val,mpeg2_struct)
int run,val;
struct MPEG2_structure *mpeg2_struct;
{
if (run==0 && (val==1 || val==-1)) /* these are treated differently */
MPEG2_putbits(2|(val<0),2,mpeg2_struct); /* generate '1s' (s=sign), (Table B-14, line 2) */
else
MPEG2_putAC(run,val,0,mpeg2_struct); /* no difference for all others */
}
/* generate variable length code for other DCT coefficients (7.2.2) */
void MPEG2_putAC(run,signed_level,vlcformat,mpeg2_struct)
int run,signed_level,vlcformat;
struct MPEG2_structure *mpeg2_struct;
{
int level, len;
VLCtable *ptab;
level = (signed_level<0) ? -signed_level : signed_level; /* abs(signed_level) */
/* make sure run and level are valid */
if (run<0 || run>63 || level==0 || level>2047 || (mpeg2_struct->mpeg1 && level>255))
{
sprintf(mpeg2_struct->errortext,"AC value out of range (run=%d, signed_level=%d)\n",
run,signed_level);
(*(mpeg2_struct->report_error))(mpeg2_struct->errortext);
}
len = 0;
if (run<2 && level<41)
{
/* vlcformat selects either of Table B-14 / B-15 */
if (vlcformat)
ptab = &dct_code_tab1a[run][level-1];
else
ptab = &dct_code_tab1[run][level-1];
len = ptab->len;
}
else if (run<32 && level<6)
{
/* vlcformat selects either of Table B-14 / B-15 */
if (vlcformat)
ptab = &dct_code_tab2a[run-2][level-1];
else
ptab = &dct_code_tab2[run-2][level-1];
len = ptab->len;
}
if (len!=0) /* a VLC code exists */
{
MPEG2_putbits(ptab->code,len,mpeg2_struct);
MPEG2_putbits(signed_level<0,1,mpeg2_struct); /* sign */
}
else
{
/* no VLC for this (run, level) combination: use escape coding (7.2.2.3) */
MPEG2_putbits(1l,6,mpeg2_struct); /* Escape */
MPEG2_putbits(run,6,mpeg2_struct); /* 6 bit code for run */
if (mpeg2_struct->mpeg1)
{
/* ISO/IEC 11172-2 uses a 8 or 16 bit code */
if (signed_level>127)
MPEG2_putbits(0,8,mpeg2_struct);
if (signed_level<-127)
MPEG2_putbits(128,8,mpeg2_struct);
MPEG2_putbits(signed_level,8,mpeg2_struct);
}
else
{
/* ISO/IEC 13818-2 uses a 12 bit code, Table B-16 */
MPEG2_putbits(signed_level,12,mpeg2_struct);
}
}
}
/* generate variable length code for macroblock_address_increment (6.3.16) */
void MPEG2_putaddrinc(addrinc,mpeg2_struct)
int addrinc;
struct MPEG2_structure *mpeg2_struct;
{
while (addrinc>33)
{
MPEG2_putbits(0x08,11,mpeg2_struct); /* macroblock_escape */
addrinc-= 33;
}
MPEG2_putbits(addrinctab[addrinc-1].code,addrinctab[addrinc-1].len,mpeg2_struct);
}
/* generate variable length code for macroblock_type (6.3.16.1) */
void MPEG2_putmbtype( int pict_type, int mb_type,struct MPEG2_structure *mpeg2_struct )
{
MPEG2_putbits(mbtypetab[pict_type-1][mb_type].code,
mbtypetab[pict_type-1][mb_type].len,mpeg2_struct);
}
/* generate variable length code for motion_code (6.3.16.3) */
void MPEG2_putmotioncode(motion_code,mpeg2_struct)
int motion_code;
struct MPEG2_structure *mpeg2_struct;
{
int abscode;
abscode = (motion_code>=0) ? motion_code : -motion_code; /* abs(motion_code) */
MPEG2_putbits(motionvectab[abscode].code,motionvectab[abscode].len,mpeg2_struct);
if (motion_code!=0)
MPEG2_putbits(motion_code<0,1,mpeg2_struct); /* sign, 0=positive, 1=negative */
}
/* generate variable length code for dmvector[t] (6.3.16.3), Table B-11 */
void MPEG2_putdmv(dmv,mpeg2_struct)
int dmv;
struct MPEG2_structure *mpeg2_struct;
{
if (dmv==0)
MPEG2_putbits(0,1,mpeg2_struct);
else if (dmv>0)
MPEG2_putbits(2,2,mpeg2_struct);
else
MPEG2_putbits(3,2,mpeg2_struct);
}
/* generate variable length code for coded_block_pattern (6.3.16.4)
*
* 4:2:2, 4:4:4 not implemented
*/
void MPEG2_putcbp(cbp,mpeg2_struct)
int cbp;
struct MPEG2_structure *mpeg2_struct;
{
MPEG2_putbits(cbptable[cbp].code,cbptable[cbp].len,mpeg2_struct);
}
|