Mercurial > libavcodec.hg
comparison mpeg12.c @ 714:fd0e997616fe libavcodec
optimizing mpeg2 decode block stuff
author | michaelni |
---|---|
date | Tue, 01 Oct 2002 23:11:40 +0000 |
parents | dcbcf9676c9f |
children | 8b3ccabfce4a |
comparison
equal
deleted
inserted
replaced
713:e74a563eb643 | 714:fd0e997616fe |
---|---|
56 DCTELEM *block, | 56 DCTELEM *block, |
57 int n); | 57 int n); |
58 static inline int mpeg1_decode_block_intra(MpegEncContext *s, | 58 static inline int mpeg1_decode_block_intra(MpegEncContext *s, |
59 DCTELEM *block, | 59 DCTELEM *block, |
60 int n); | 60 int n); |
61 static int mpeg2_decode_block_non_intra(MpegEncContext *s, | 61 static inline int mpeg2_decode_block_non_intra(MpegEncContext *s, |
62 DCTELEM *block, | 62 DCTELEM *block, |
63 int n); | 63 int n); |
64 static int mpeg2_decode_block_intra(MpegEncContext *s, | 64 static inline int mpeg2_decode_block_intra(MpegEncContext *s, |
65 DCTELEM *block, | 65 DCTELEM *block, |
66 int n); | 66 int n); |
67 static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred); | 67 static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred); |
68 | 68 |
69 static UINT16 mv_penalty[MAX_FCODE+1][MAX_MV*2+1]; | 69 static UINT16 mv_penalty[MAX_FCODE+1][MAX_MV*2+1]; |
1115 i = -1; | 1115 i = -1; |
1116 /* special case for the first coef. no need to add a second vlc table */ | 1116 /* special case for the first coef. no need to add a second vlc table */ |
1117 UPDATE_CACHE(re, &s->gb); | 1117 UPDATE_CACHE(re, &s->gb); |
1118 v= SHOW_UBITS(re, &s->gb, 2); | 1118 v= SHOW_UBITS(re, &s->gb, 2); |
1119 if (v & 2) { | 1119 if (v & 2) { |
1120 SKIP_BITS(re, &s->gb, 2); | 1120 LAST_SKIP_BITS(re, &s->gb, 2); |
1121 level= (3*qscale*quant_matrix[0])>>4; | 1121 level= (3*qscale*quant_matrix[0])>>4; |
1122 level= (level-1)|1; | 1122 level= (level-1)|1; |
1123 if(v&1) | 1123 if(v&1) |
1124 level= -level; | 1124 level= -level; |
1125 block[ scantable[0] ] = level; | 1125 block[0] = level; |
1126 i++; | 1126 i++; |
1127 } | 1127 } |
1128 CLOSE_READER(re, &s->gb); | 1128 |
1129 } | |
1130 { | |
1131 OPEN_READER(re, &s->gb); | |
1132 /* now quantify & encode AC coefs */ | 1129 /* now quantify & encode AC coefs */ |
1133 for(;;) { | 1130 for(;;) { |
1134 UPDATE_CACHE(re, &s->gb); | 1131 UPDATE_CACHE(re, &s->gb); |
1135 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2); | 1132 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2); |
1136 | 1133 |
1178 return 0; | 1175 return 0; |
1179 } | 1176 } |
1180 | 1177 |
1181 /* Also does unquantization here, since I will never support mpeg2 | 1178 /* Also does unquantization here, since I will never support mpeg2 |
1182 encoding */ | 1179 encoding */ |
1183 static int mpeg2_decode_block_non_intra(MpegEncContext *s, | 1180 static inline int mpeg2_decode_block_non_intra(MpegEncContext *s, |
1184 DCTELEM *block, | 1181 DCTELEM *block, |
1185 int n) | 1182 int n) |
1186 { | 1183 { |
1187 int level, i, j, run; | 1184 int level, i, j, run; |
1188 int code; | |
1189 RLTable *rl = &rl_mpeg1; | 1185 RLTable *rl = &rl_mpeg1; |
1190 const UINT8 *scan_table; | 1186 UINT8 * scantable; |
1191 const UINT16 *matrix; | 1187 const UINT16 *quant_matrix; |
1188 const int qscale= s->qscale; | |
1192 int mismatch; | 1189 int mismatch; |
1193 | 1190 |
1194 if (s->alternate_scan) | 1191 if (s->alternate_scan) |
1195 scan_table = s->intra_v_scantable.permutated; | 1192 scantable = s->intra_v_scantable.permutated; |
1196 else | 1193 else |
1197 scan_table = s->intra_scantable.permutated; | 1194 scantable = s->intra_scantable.permutated; |
1198 mismatch = 1; | 1195 mismatch = 1; |
1199 | 1196 |
1200 { | 1197 { |
1201 int v; | 1198 int v; |
1202 OPEN_READER(re, &s->gb); | 1199 OPEN_READER(re, &s->gb); |
1203 i = 0; | 1200 i = -1; |
1204 if (n < 4) | 1201 if (n < 4) |
1205 matrix = s->inter_matrix; | 1202 quant_matrix = s->inter_matrix; |
1206 else | 1203 else |
1207 matrix = s->chroma_inter_matrix; | 1204 quant_matrix = s->chroma_inter_matrix; |
1208 | 1205 |
1209 /* special case for the first coef. no need to add a second vlc table */ | 1206 /* special case for the first coef. no need to add a second vlc table */ |
1210 UPDATE_CACHE(re, &s->gb); | 1207 UPDATE_CACHE(re, &s->gb); |
1211 v= SHOW_UBITS(re, &s->gb, 2); | 1208 v= SHOW_UBITS(re, &s->gb, 2); |
1212 if (v & 2) { | 1209 if (v & 2) { |
1213 run = 0; | 1210 LAST_SKIP_BITS(re, &s->gb, 2); |
1214 level = 5 - (v << 1); | 1211 level= (3*qscale*quant_matrix[0])>>5; |
1215 SKIP_BITS(re, &s->gb, 2); | 1212 if(v&1) |
1216 CLOSE_READER(re, &s->gb); | 1213 level= -level; |
1217 goto add_coef; | 1214 block[0] = level; |
1215 mismatch ^= level; | |
1216 i++; | |
1217 } | |
1218 | |
1219 /* now quantify & encode AC coefs */ | |
1220 for(;;) { | |
1221 UPDATE_CACHE(re, &s->gb); | |
1222 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2); | |
1223 | |
1224 if(level == 127){ | |
1225 break; | |
1226 } else if(level != 0) { | |
1227 i += run; | |
1228 j = scantable[i]; | |
1229 level= ((level*2+1)*qscale*quant_matrix[j])>>5; | |
1230 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1); | |
1231 LAST_SKIP_BITS(re, &s->gb, 1); | |
1232 } else { | |
1233 /* escape */ | |
1234 run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6); | |
1235 UPDATE_CACHE(re, &s->gb); | |
1236 level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12); | |
1237 | |
1238 i += run; | |
1239 j = scantable[i]; | |
1240 if(level<0){ | |
1241 level= ((-level*2+1)*qscale*quant_matrix[j])>>5; | |
1242 level= -level; | |
1243 }else{ | |
1244 level= ((level*2+1)*qscale*quant_matrix[j])>>5; | |
1245 } | |
1246 } | |
1247 if (i > 63){ | |
1248 fprintf(stderr, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y); | |
1249 return -1; | |
1250 } | |
1251 | |
1252 mismatch ^= level; | |
1253 block[j] = level; | |
1218 } | 1254 } |
1219 CLOSE_READER(re, &s->gb); | 1255 CLOSE_READER(re, &s->gb); |
1220 } | 1256 } |
1221 | |
1222 /* now quantify & encode AC coefs */ | |
1223 for(;;) { | |
1224 code = get_vlc2(&s->gb, rl->vlc.table, TEX_VLC_BITS, 2); | |
1225 if (code < 0){ | |
1226 fprintf(stderr, "invalid ac code at %d %d\n", s->mb_x, s->mb_y); | |
1227 return -1; | |
1228 } | |
1229 if (code == 112) { | |
1230 break; | |
1231 } else if (code == 111) { | |
1232 /* escape */ | |
1233 run = get_bits(&s->gb, 6); | |
1234 level = get_bits(&s->gb, 12); | |
1235 level= (level + ((-1)<<11)) ^ ((-1)<<11); //sign extension | |
1236 } else { | |
1237 run = rl->table_run[code]; | |
1238 level = rl->table_level[code]; | |
1239 if (get_bits1(&s->gb)) | |
1240 level = -level; | |
1241 } | |
1242 i += run; | |
1243 if (i >= 64){ | |
1244 fprintf(stderr, "run too long at %d %d\n", s->mb_x, s->mb_y); | |
1245 return -1; | |
1246 } | |
1247 add_coef: | |
1248 j = scan_table[i]; | |
1249 dprintf("%d: run=%d level=%d\n", n, run, level); | |
1250 /* XXX: optimize */ | |
1251 if (level > 0) { | |
1252 level = ((level * 2 + 1) * s->qscale * matrix[j]) >> 5; | |
1253 } else { | |
1254 level = ((-level * 2 + 1) * s->qscale * matrix[j]) >> 5; | |
1255 level = -level; | |
1256 } | |
1257 /* XXX: is it really necessary to saturate since the encoder | |
1258 knows whats going on ? */ | |
1259 mismatch ^= level; | |
1260 block[j] = level; | |
1261 i++; | |
1262 } | |
1263 block[63] ^= (mismatch & 1); | 1257 block[63] ^= (mismatch & 1); |
1258 | |
1264 s->block_last_index[n] = i; | 1259 s->block_last_index[n] = i; |
1265 | |
1266 return 0; | 1260 return 0; |
1267 } | 1261 } |
1268 | 1262 |
1269 static int mpeg2_decode_block_intra(MpegEncContext *s, | 1263 static inline int mpeg2_decode_block_intra(MpegEncContext *s, |
1270 DCTELEM *block, | 1264 DCTELEM *block, |
1271 int n) | 1265 int n) |
1272 { | 1266 { |
1273 int level, dc, diff, i, j, run; | 1267 int level, dc, diff, i, j, run; |
1274 int code, component; | 1268 int component; |
1275 RLTable *rl; | 1269 RLTable *rl; |
1276 const UINT8 *scan_table; | 1270 UINT8 * scantable; |
1277 const UINT16 *matrix; | 1271 const UINT16 *quant_matrix; |
1272 const int qscale= s->qscale; | |
1278 int mismatch; | 1273 int mismatch; |
1279 | 1274 |
1280 if (s->alternate_scan) | 1275 if (s->alternate_scan) |
1281 scan_table = s->intra_v_scantable.permutated; | 1276 scantable = s->intra_v_scantable.permutated; |
1282 else | 1277 else |
1283 scan_table = s->intra_scantable.permutated; | 1278 scantable = s->intra_scantable.permutated; |
1284 | 1279 |
1285 /* DC coef */ | 1280 /* DC coef */ |
1286 component = (n <= 3 ? 0 : n - 4 + 1); | 1281 if (n < 4){ |
1282 quant_matrix = s->intra_matrix; | |
1283 component = 0; | |
1284 }else{ | |
1285 quant_matrix = s->chroma_intra_matrix; | |
1286 component = n - 3; | |
1287 } | |
1287 diff = decode_dc(s, component); | 1288 diff = decode_dc(s, component); |
1288 if (diff >= 0xffff) | 1289 if (diff >= 0xffff) |
1289 return -1; | 1290 return -1; |
1290 dc = s->last_dc[component]; | 1291 dc = s->last_dc[component]; |
1291 dc += diff; | 1292 dc += diff; |
1292 s->last_dc[component] = dc; | 1293 s->last_dc[component] = dc; |
1293 block[0] = dc << (3 - s->intra_dc_precision); | 1294 block[0] = dc << (3 - s->intra_dc_precision); |
1294 dprintf("dc=%d\n", block[0]); | 1295 dprintf("dc=%d\n", block[0]); |
1295 mismatch = block[0] ^ 1; | 1296 mismatch = block[0] ^ 1; |
1296 i = 1; | 1297 i = 0; |
1297 if (s->intra_vlc_format) | 1298 if (s->intra_vlc_format) |
1298 rl = &rl_mpeg2; | 1299 rl = &rl_mpeg2; |
1299 else | 1300 else |
1300 rl = &rl_mpeg1; | 1301 rl = &rl_mpeg1; |
1301 if (n < 4) | 1302 |
1302 matrix = s->intra_matrix; | 1303 { |
1303 else | 1304 OPEN_READER(re, &s->gb); |
1304 matrix = s->chroma_intra_matrix; | 1305 /* now quantify & encode AC coefs */ |
1305 | 1306 for(;;) { |
1306 /* now quantify & encode AC coefs */ | 1307 UPDATE_CACHE(re, &s->gb); |
1307 for(;;) { | 1308 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2); |
1308 code = get_vlc2(&s->gb, rl->vlc.table, TEX_VLC_BITS, 2); | 1309 |
1309 if (code < 0){ | 1310 if(level == 127){ |
1310 fprintf(stderr, "invalid ac code at %d %d\n", s->mb_x, s->mb_y); | 1311 break; |
1311 return -1; | 1312 } else if(level != 0) { |
1312 } | 1313 i += run; |
1313 if (code == 112) { | 1314 j = scantable[i]; |
1314 break; | 1315 level= (level*qscale*quant_matrix[j])>>4; |
1315 } else if (code == 111) { | 1316 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1); |
1316 /* escape */ | 1317 LAST_SKIP_BITS(re, &s->gb, 1); |
1317 run = get_bits(&s->gb, 6); | 1318 } else { |
1318 level = get_bits(&s->gb, 12); | 1319 /* escape */ |
1319 level= (level + ((-1)<<11)) ^ ((-1)<<11); //sign extension | 1320 run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6); |
1320 } else { | 1321 UPDATE_CACHE(re, &s->gb); |
1321 run = rl->table_run[code]; | 1322 level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12); |
1322 level = rl->table_level[code]; | 1323 i += run; |
1323 if (get_bits1(&s->gb)) | 1324 j = scantable[i]; |
1324 level = -level; | 1325 if(level<0){ |
1325 } | 1326 level= (-level*qscale*quant_matrix[j])>>4; |
1326 i += run; | 1327 level= -level; |
1327 if (i >= 64){ | 1328 }else{ |
1328 fprintf(stderr, "run too long at %d %d\n", s->mb_x, s->mb_y); | 1329 level= (level*qscale*quant_matrix[j])>>4; |
1329 return -1; | 1330 } |
1330 } | 1331 } |
1331 j = scan_table[i]; | 1332 if (i > 63){ |
1332 dprintf("%d: run=%d level=%d\n", n, run, level); | 1333 fprintf(stderr, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y); |
1333 level = (level * s->qscale * matrix[j]) / 16; | 1334 return -1; |
1334 /* XXX: is it really necessary to saturate since the encoder | 1335 } |
1335 knows whats going on ? */ | 1336 |
1336 mismatch ^= level; | 1337 mismatch^= level; |
1337 block[j] = level; | 1338 block[j] = level; |
1338 i++; | 1339 } |
1339 } | 1340 CLOSE_READER(re, &s->gb); |
1340 block[63] ^= (mismatch & 1); | 1341 } |
1342 block[63]^= mismatch&1; | |
1343 | |
1341 s->block_last_index[n] = i; | 1344 s->block_last_index[n] = i; |
1342 return 0; | 1345 return 0; |
1343 } | 1346 } |
1344 | 1347 |
1345 /* compressed picture size */ | 1348 /* compressed picture size */ |