Libav 0.7.1
|
00001 /* 00002 * H.26L/H.264/AVC/JVT/14496-10/... encoder/decoder 00003 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at> 00004 * 00005 * This file is part of Libav. 00006 * 00007 * Libav is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU Lesser General Public 00009 * License as published by the Free Software Foundation; either 00010 * version 2.1 of the License, or (at your option) any later version. 00011 * 00012 * Libav is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 * Lesser General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public 00018 * License along with Libav; if not, write to the Free Software 00019 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00020 */ 00021 00027 #include <string.h> 00028 00029 #include "libavutil/common.h" 00030 #include "get_bits.h" 00031 #include "cabac.h" 00032 00033 static const uint8_t lps_range[64][4]= { 00034 {128,176,208,240}, {128,167,197,227}, {128,158,187,216}, {123,150,178,205}, 00035 {116,142,169,195}, {111,135,160,185}, {105,128,152,175}, {100,122,144,166}, 00036 { 95,116,137,158}, { 90,110,130,150}, { 85,104,123,142}, { 81, 99,117,135}, 00037 { 77, 94,111,128}, { 73, 89,105,122}, { 69, 85,100,116}, { 66, 80, 95,110}, 00038 { 62, 76, 90,104}, { 59, 72, 86, 99}, { 56, 69, 81, 94}, { 53, 65, 77, 89}, 00039 { 51, 62, 73, 85}, { 48, 59, 69, 80}, { 46, 56, 66, 76}, { 43, 53, 63, 72}, 00040 { 41, 50, 59, 69}, { 39, 48, 56, 65}, { 37, 45, 54, 62}, { 35, 43, 51, 59}, 00041 { 33, 41, 48, 56}, { 32, 39, 46, 53}, { 30, 37, 43, 50}, { 29, 35, 41, 48}, 00042 { 27, 33, 39, 45}, { 26, 31, 37, 43}, { 24, 30, 35, 41}, { 23, 28, 33, 39}, 00043 { 22, 27, 32, 37}, { 21, 26, 30, 35}, { 20, 24, 29, 33}, { 19, 23, 27, 31}, 00044 { 18, 22, 26, 30}, { 17, 21, 25, 28}, { 16, 20, 23, 27}, { 15, 19, 22, 25}, 00045 { 14, 18, 21, 24}, { 14, 17, 20, 23}, { 13, 16, 19, 22}, { 12, 15, 18, 21}, 00046 { 12, 14, 17, 20}, { 11, 14, 16, 19}, { 11, 13, 15, 18}, { 10, 12, 15, 17}, 00047 { 10, 12, 14, 16}, { 9, 11, 13, 15}, { 9, 11, 12, 14}, { 8, 10, 12, 14}, 00048 { 8, 9, 11, 13}, { 7, 9, 11, 12}, { 7, 9, 10, 12}, { 7, 8, 10, 11}, 00049 { 6, 8, 9, 11}, { 6, 7, 9, 10}, { 6, 7, 8, 9}, { 2, 2, 2, 2}, 00050 }; 00051 00052 uint8_t ff_h264_mlps_state[4*64]; 00053 uint8_t ff_h264_lps_range[4*2*64]; 00054 uint8_t ff_h264_lps_state[2*64]; 00055 uint8_t ff_h264_mps_state[2*64]; 00056 00057 static const uint8_t mps_state[64]= { 00058 1, 2, 3, 4, 5, 6, 7, 8, 00059 9,10,11,12,13,14,15,16, 00060 17,18,19,20,21,22,23,24, 00061 25,26,27,28,29,30,31,32, 00062 33,34,35,36,37,38,39,40, 00063 41,42,43,44,45,46,47,48, 00064 49,50,51,52,53,54,55,56, 00065 57,58,59,60,61,62,62,63, 00066 }; 00067 00068 static const uint8_t lps_state[64]= { 00069 0, 0, 1, 2, 2, 4, 4, 5, 00070 6, 7, 8, 9, 9,11,11,12, 00071 13,13,15,15,16,16,18,18, 00072 19,19,21,21,22,22,23,24, 00073 24,25,26,26,27,27,28,29, 00074 29,30,30,30,31,32,32,33, 00075 33,33,34,34,35,35,35,36, 00076 36,36,37,37,37,38,38,63, 00077 }; 00078 #if 0 00079 const uint8_t ff_h264_norm_shift_old[128]= { 00080 7,6,5,5,4,4,4,4,3,3,3,3,3,3,3,3, 00081 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 00082 1,1,1,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,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 00107 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 00108 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 00109 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 00110 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 00111 }; 00112 00117 void ff_init_cabac_encoder(CABACContext *c, uint8_t *buf, int buf_size){ 00118 init_put_bits(&c->pb, buf, buf_size); 00119 00120 c->low= 0; 00121 c->range= 0x1FE; 00122 c->outstanding_count= 0; 00123 #ifdef STRICT_LIMITS 00124 c->sym_count =0; 00125 #endif 00126 00127 c->pb.bit_left++; //avoids firstBitFlag 00128 } 00129 00134 void ff_init_cabac_decoder(CABACContext *c, const uint8_t *buf, int buf_size){ 00135 c->bytestream_start= 00136 c->bytestream= buf; 00137 c->bytestream_end= buf + buf_size; 00138 00139 #if CABAC_BITS == 16 00140 c->low = (*c->bytestream++)<<18; 00141 c->low+= (*c->bytestream++)<<10; 00142 #else 00143 c->low = (*c->bytestream++)<<10; 00144 #endif 00145 c->low+= ((*c->bytestream++)<<2) + 2; 00146 c->range= 0x1FE; 00147 } 00148 00149 void ff_init_cabac_states(CABACContext *c){ 00150 int i, j; 00151 00152 for(i=0; i<64; i++){ 00153 for(j=0; j<4; j++){ //FIXME check if this is worth the 1 shift we save 00154 ff_h264_lps_range[j*2*64+2*i+0]= 00155 ff_h264_lps_range[j*2*64+2*i+1]= lps_range[i][j]; 00156 } 00157 00158 ff_h264_mlps_state[128+2*i+0]= 00159 ff_h264_mps_state[2*i+0]= 2*mps_state[i]+0; 00160 ff_h264_mlps_state[128+2*i+1]= 00161 ff_h264_mps_state[2*i+1]= 2*mps_state[i]+1; 00162 00163 if( i ){ 00164 #ifdef BRANCHLESS_CABAC_DECODER 00165 ff_h264_mlps_state[128-2*i-1]= 2*lps_state[i]+0; 00166 ff_h264_mlps_state[128-2*i-2]= 2*lps_state[i]+1; 00167 }else{ 00168 ff_h264_mlps_state[128-2*i-1]= 1; 00169 ff_h264_mlps_state[128-2*i-2]= 0; 00170 #else 00171 ff_h264_lps_state[2*i+0]= 2*lps_state[i]+0; 00172 ff_h264_lps_state[2*i+1]= 2*lps_state[i]+1; 00173 }else{ 00174 ff_h264_lps_state[2*i+0]= 1; 00175 ff_h264_lps_state[2*i+1]= 0; 00176 #endif 00177 } 00178 } 00179 } 00180 00181 #ifdef TEST 00182 #define SIZE 10240 00183 00184 #include "libavutil/lfg.h" 00185 #include "avcodec.h" 00186 #include "cabac.h" 00187 00188 int main(void){ 00189 CABACContext c; 00190 uint8_t b[9*SIZE]; 00191 uint8_t r[9*SIZE]; 00192 int i; 00193 uint8_t state[10]= {0}; 00194 AVLFG prng; 00195 00196 av_lfg_init(&prng, 1); 00197 ff_init_cabac_encoder(&c, b, SIZE); 00198 ff_init_cabac_states(&c); 00199 00200 for(i=0; i<SIZE; i++){ 00201 r[i] = av_lfg_get(&prng) % 7; 00202 } 00203 00204 for(i=0; i<SIZE; i++){ 00205 START_TIMER 00206 put_cabac_bypass(&c, r[i]&1); 00207 STOP_TIMER("put_cabac_bypass") 00208 } 00209 00210 for(i=0; i<SIZE; i++){ 00211 START_TIMER 00212 put_cabac(&c, state, r[i]&1); 00213 STOP_TIMER("put_cabac") 00214 } 00215 00216 for(i=0; i<SIZE; i++){ 00217 START_TIMER 00218 put_cabac_u(&c, state, r[i], 6, 3, i&1); 00219 STOP_TIMER("put_cabac_u") 00220 } 00221 00222 for(i=0; i<SIZE; i++){ 00223 START_TIMER 00224 put_cabac_ueg(&c, state, r[i], 3, 0, 1, 2); 00225 STOP_TIMER("put_cabac_ueg") 00226 } 00227 00228 put_cabac_terminate(&c, 1); 00229 00230 ff_init_cabac_decoder(&c, b, SIZE); 00231 00232 memset(state, 0, sizeof(state)); 00233 00234 for(i=0; i<SIZE; i++){ 00235 START_TIMER 00236 if( (r[i]&1) != get_cabac_bypass(&c) ) 00237 av_log(NULL, AV_LOG_ERROR, "CABAC bypass failure at %d\n", i); 00238 STOP_TIMER("get_cabac_bypass") 00239 } 00240 00241 for(i=0; i<SIZE; i++){ 00242 START_TIMER 00243 if( (r[i]&1) != get_cabac(&c, state) ) 00244 av_log(NULL, AV_LOG_ERROR, "CABAC failure at %d\n", i); 00245 STOP_TIMER("get_cabac") 00246 } 00247 #if 0 00248 for(i=0; i<SIZE; i++){ 00249 START_TIMER 00250 if( r[i] != get_cabac_u(&c, state, (i&1) ? 6 : 7, 3, i&1) ) 00251 av_log(NULL, AV_LOG_ERROR, "CABAC unary (truncated) binarization failure at %d\n", i); 00252 STOP_TIMER("get_cabac_u") 00253 } 00254 00255 for(i=0; i<SIZE; i++){ 00256 START_TIMER 00257 if( r[i] != get_cabac_ueg(&c, state, 3, 0, 1, 2)) 00258 av_log(NULL, AV_LOG_ERROR, "CABAC unary (truncated) binarization failure at %d\n", i); 00259 STOP_TIMER("get_cabac_ueg") 00260 } 00261 #endif 00262 if(!get_cabac_terminate(&c)) 00263 av_log(NULL, AV_LOG_ERROR, "where's the Terminator?\n"); 00264 00265 return 0; 00266 } 00267 00268 #endif /* TEST */