Mercurial > libavcodec.hg
comparison h263.c @ 10752:56e57dc0fe19 libavcodec
cosmetics: Move H.263-related functions around to avoid forward declarations.
author | diego |
---|---|
date | Wed, 30 Dec 2009 14:34:14 +0000 |
parents | 1550e82f6183 |
children | 3790420a84b6 |
comparison
equal
deleted
inserted
replaced
10751:74e8feb81c6d | 10752:56e57dc0fe19 |
---|---|
54 #define MB_TYPE_B_VLC_BITS 4 | 54 #define MB_TYPE_B_VLC_BITS 4 |
55 #define TEX_VLC_BITS 9 | 55 #define TEX_VLC_BITS 9 |
56 #define H263_MBTYPE_B_VLC_BITS 6 | 56 #define H263_MBTYPE_B_VLC_BITS 6 |
57 #define CBPC_B_VLC_BITS 3 | 57 #define CBPC_B_VLC_BITS 3 |
58 | 58 |
59 static int h263_decode_motion(MpegEncContext * s, int pred, int fcode); | |
60 static int h263p_decode_umotion(MpegEncContext * s, int pred); | |
61 static int h263_decode_block(MpegEncContext * s, DCTELEM * block, | |
62 int n, int coded); | |
63 static inline int mpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr); | 59 static inline int mpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr); |
64 static inline int mpeg4_decode_block(MpegEncContext * s, DCTELEM * block, | 60 static inline int mpeg4_decode_block(MpegEncContext * s, DCTELEM * block, |
65 int n, int coded, int intra, int rvlc); | 61 int n, int coded, int intra, int rvlc); |
66 | 62 |
67 static void mpeg4_decode_sprite_trajectory(MpegEncContext * s, GetBitContext *gb); | 63 static void mpeg4_decode_sprite_trajectory(MpegEncContext * s, GetBitContext *gb); |
68 static inline int ff_mpeg4_pred_dc(MpegEncContext * s, int n, int level, int *dir_ptr, int encoding); | 64 static inline int ff_mpeg4_pred_dc(MpegEncContext * s, int n, int level, int *dir_ptr, int encoding); |
69 | 65 |
70 #if CONFIG_ENCODERS | 66 #if CONFIG_ENCODERS |
71 static void h263_encode_block(MpegEncContext *s, DCTELEM *block, int n); | |
72 static void h263p_encode_umotion(MpegEncContext *s, int val); | |
73 static int h263_pred_dc(MpegEncContext *s, int n, int16_t **dc_val_ptr); | |
74 | |
75 static void mpeg4_encode_visual_object_header(MpegEncContext *s); | 67 static void mpeg4_encode_visual_object_header(MpegEncContext *s); |
76 static void mpeg4_encode_vol_header(MpegEncContext *s, int vo_number, | 68 static void mpeg4_encode_vol_header(MpegEncContext *s, int vo_number, |
77 int vol_number); | 69 int vol_number); |
78 static inline void mpeg4_encode_block(MpegEncContext *s, DCTELEM *block, | 70 static inline void mpeg4_encode_block(MpegEncContext *s, DCTELEM *block, |
79 int n, int dc, uint8_t *scan_table, | 71 int n, int dc, uint8_t *scan_table, |
1242 if(s->ac_pred) | 1234 if(s->ac_pred) |
1243 restore_ac_coeffs(s, block, dir, scan_table, zigzag_last_index); | 1235 restore_ac_coeffs(s, block, dir, scan_table, zigzag_last_index); |
1244 } | 1236 } |
1245 } | 1237 } |
1246 | 1238 |
1239 /** | |
1240 * encodes a 8x8 block. | |
1241 * @param block the 8x8 block | |
1242 * @param n block index (0-3 are luma, 4-5 are chroma) | |
1243 */ | |
1244 static void h263_encode_block(MpegEncContext * s, DCTELEM * block, int n) | |
1245 { | |
1246 int level, run, last, i, j, last_index, last_non_zero, sign, slevel, code; | |
1247 RLTable *rl; | |
1248 | |
1249 rl = &rl_inter; | |
1250 if (s->mb_intra && !s->h263_aic) { | |
1251 /* DC coef */ | |
1252 level = block[0]; | |
1253 /* 255 cannot be represented, so we clamp */ | |
1254 if (level > 254) { | |
1255 level = 254; | |
1256 block[0] = 254; | |
1257 } | |
1258 /* 0 cannot be represented also */ | |
1259 else if (level < 1) { | |
1260 level = 1; | |
1261 block[0] = 1; | |
1262 } | |
1263 if (level == 128) //FIXME check rv10 | |
1264 put_bits(&s->pb, 8, 0xff); | |
1265 else | |
1266 put_bits(&s->pb, 8, level); | |
1267 i = 1; | |
1268 } else { | |
1269 i = 0; | |
1270 if (s->h263_aic && s->mb_intra) | |
1271 rl = &rl_intra_aic; | |
1272 | |
1273 if(s->alt_inter_vlc && !s->mb_intra){ | |
1274 int aic_vlc_bits=0; | |
1275 int inter_vlc_bits=0; | |
1276 int wrong_pos=-1; | |
1277 int aic_code; | |
1278 | |
1279 last_index = s->block_last_index[n]; | |
1280 last_non_zero = i - 1; | |
1281 for (; i <= last_index; i++) { | |
1282 j = s->intra_scantable.permutated[i]; | |
1283 level = block[j]; | |
1284 if (level) { | |
1285 run = i - last_non_zero - 1; | |
1286 last = (i == last_index); | |
1287 | |
1288 if(level<0) level= -level; | |
1289 | |
1290 code = get_rl_index(rl, last, run, level); | |
1291 aic_code = get_rl_index(&rl_intra_aic, last, run, level); | |
1292 inter_vlc_bits += rl->table_vlc[code][1]+1; | |
1293 aic_vlc_bits += rl_intra_aic.table_vlc[aic_code][1]+1; | |
1294 | |
1295 if (code == rl->n) { | |
1296 inter_vlc_bits += 1+6+8-1; | |
1297 } | |
1298 if (aic_code == rl_intra_aic.n) { | |
1299 aic_vlc_bits += 1+6+8-1; | |
1300 wrong_pos += run + 1; | |
1301 }else | |
1302 wrong_pos += wrong_run[aic_code]; | |
1303 last_non_zero = i; | |
1304 } | |
1305 } | |
1306 i = 0; | |
1307 if(aic_vlc_bits < inter_vlc_bits && wrong_pos > 63) | |
1308 rl = &rl_intra_aic; | |
1309 } | |
1310 } | |
1311 | |
1312 /* AC coefs */ | |
1313 last_index = s->block_last_index[n]; | |
1314 last_non_zero = i - 1; | |
1315 for (; i <= last_index; i++) { | |
1316 j = s->intra_scantable.permutated[i]; | |
1317 level = block[j]; | |
1318 if (level) { | |
1319 run = i - last_non_zero - 1; | |
1320 last = (i == last_index); | |
1321 sign = 0; | |
1322 slevel = level; | |
1323 if (level < 0) { | |
1324 sign = 1; | |
1325 level = -level; | |
1326 } | |
1327 code = get_rl_index(rl, last, run, level); | |
1328 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]); | |
1329 if (code == rl->n) { | |
1330 if(s->h263_flv <= 1){ | |
1331 put_bits(&s->pb, 1, last); | |
1332 put_bits(&s->pb, 6, run); | |
1333 | |
1334 assert(slevel != 0); | |
1335 | |
1336 if(level < 128) | |
1337 put_sbits(&s->pb, 8, slevel); | |
1338 else{ | |
1339 put_bits(&s->pb, 8, 128); | |
1340 put_sbits(&s->pb, 5, slevel); | |
1341 put_sbits(&s->pb, 6, slevel>>5); | |
1342 } | |
1343 }else{ | |
1344 if(level < 64) { // 7-bit level | |
1345 put_bits(&s->pb, 1, 0); | |
1346 put_bits(&s->pb, 1, last); | |
1347 put_bits(&s->pb, 6, run); | |
1348 | |
1349 put_sbits(&s->pb, 7, slevel); | |
1350 } else { | |
1351 /* 11-bit level */ | |
1352 put_bits(&s->pb, 1, 1); | |
1353 put_bits(&s->pb, 1, last); | |
1354 put_bits(&s->pb, 6, run); | |
1355 | |
1356 put_sbits(&s->pb, 11, slevel); | |
1357 } | |
1358 } | |
1359 } else { | |
1360 put_bits(&s->pb, 1, sign); | |
1361 } | |
1362 last_non_zero = i; | |
1363 } | |
1364 } | |
1365 } | |
1366 | |
1367 /* Encode MV differences on H.263+ with Unrestricted MV mode */ | |
1368 static void h263p_encode_umotion(MpegEncContext * s, int val) | |
1369 { | |
1370 short sval = 0; | |
1371 short i = 0; | |
1372 short n_bits = 0; | |
1373 short temp_val; | |
1374 int code = 0; | |
1375 int tcode; | |
1376 | |
1377 if ( val == 0) | |
1378 put_bits(&s->pb, 1, 1); | |
1379 else if (val == 1) | |
1380 put_bits(&s->pb, 3, 0); | |
1381 else if (val == -1) | |
1382 put_bits(&s->pb, 3, 2); | |
1383 else { | |
1384 | |
1385 sval = ((val < 0) ? (short)(-val):(short)val); | |
1386 temp_val = sval; | |
1387 | |
1388 while (temp_val != 0) { | |
1389 temp_val = temp_val >> 1; | |
1390 n_bits++; | |
1391 } | |
1392 | |
1393 i = n_bits - 1; | |
1394 while (i > 0) { | |
1395 tcode = (sval & (1 << (i-1))) >> (i-1); | |
1396 tcode = (tcode << 1) | 1; | |
1397 code = (code << 2) | tcode; | |
1398 i--; | |
1399 } | |
1400 code = ((code << 1) | (val < 0)) << 1; | |
1401 put_bits(&s->pb, (2*n_bits)+1, code); | |
1402 } | |
1403 } | |
1404 | |
1405 static int h263_pred_dc(MpegEncContext * s, int n, int16_t **dc_val_ptr) | |
1406 { | |
1407 int x, y, wrap, a, c, pred_dc; | |
1408 int16_t *dc_val; | |
1409 | |
1410 /* find prediction */ | |
1411 if (n < 4) { | |
1412 x = 2 * s->mb_x + (n & 1); | |
1413 y = 2 * s->mb_y + ((n & 2) >> 1); | |
1414 wrap = s->b8_stride; | |
1415 dc_val = s->dc_val[0]; | |
1416 } else { | |
1417 x = s->mb_x; | |
1418 y = s->mb_y; | |
1419 wrap = s->mb_stride; | |
1420 dc_val = s->dc_val[n - 4 + 1]; | |
1421 } | |
1422 /* B C | |
1423 * A X | |
1424 */ | |
1425 a = dc_val[(x - 1) + (y) * wrap]; | |
1426 c = dc_val[(x) + (y - 1) * wrap]; | |
1427 | |
1428 /* No prediction outside GOB boundary */ | |
1429 if(s->first_slice_line && n!=3){ | |
1430 if(n!=2) c= 1024; | |
1431 if(n!=1 && s->mb_x == s->resync_mb_x) a= 1024; | |
1432 } | |
1433 /* just DC prediction */ | |
1434 if (a != 1024 && c != 1024) | |
1435 pred_dc = (a + c) >> 1; | |
1436 else if (a != 1024) | |
1437 pred_dc = a; | |
1438 else | |
1439 pred_dc = c; | |
1440 | |
1441 /* we assume pred is positive */ | |
1442 *dc_val_ptr = &dc_val[x + y * wrap]; | |
1443 return pred_dc; | |
1444 } | |
1445 | |
1247 void h263_encode_mb(MpegEncContext * s, | 1446 void h263_encode_mb(MpegEncContext * s, |
1248 DCTELEM block[6][64], | 1447 DCTELEM block[6][64], |
1249 int motion_x, int motion_y) | 1448 int motion_x, int motion_y) |
1250 { | 1449 { |
1251 int cbpc, cbpy, i, cbp, pred_x, pred_y; | 1450 int cbpc, cbpy, i, cbp, pred_x, pred_y; |
1530 s->dsp.h263_h_loop_filter(dest_cr , uvlinesize, chroma_qp); | 1729 s->dsp.h263_h_loop_filter(dest_cr , uvlinesize, chroma_qp); |
1531 } | 1730 } |
1532 } | 1731 } |
1533 } | 1732 } |
1534 } | 1733 } |
1535 | |
1536 #if CONFIG_ENCODERS | |
1537 static int h263_pred_dc(MpegEncContext * s, int n, int16_t **dc_val_ptr) | |
1538 { | |
1539 int x, y, wrap, a, c, pred_dc; | |
1540 int16_t *dc_val; | |
1541 | |
1542 /* find prediction */ | |
1543 if (n < 4) { | |
1544 x = 2 * s->mb_x + (n & 1); | |
1545 y = 2 * s->mb_y + ((n & 2) >> 1); | |
1546 wrap = s->b8_stride; | |
1547 dc_val = s->dc_val[0]; | |
1548 } else { | |
1549 x = s->mb_x; | |
1550 y = s->mb_y; | |
1551 wrap = s->mb_stride; | |
1552 dc_val = s->dc_val[n - 4 + 1]; | |
1553 } | |
1554 /* B C | |
1555 * A X | |
1556 */ | |
1557 a = dc_val[(x - 1) + (y) * wrap]; | |
1558 c = dc_val[(x) + (y - 1) * wrap]; | |
1559 | |
1560 /* No prediction outside GOB boundary */ | |
1561 if(s->first_slice_line && n!=3){ | |
1562 if(n!=2) c= 1024; | |
1563 if(n!=1 && s->mb_x == s->resync_mb_x) a= 1024; | |
1564 } | |
1565 /* just DC prediction */ | |
1566 if (a != 1024 && c != 1024) | |
1567 pred_dc = (a + c) >> 1; | |
1568 else if (a != 1024) | |
1569 pred_dc = a; | |
1570 else | |
1571 pred_dc = c; | |
1572 | |
1573 /* we assume pred is positive */ | |
1574 *dc_val_ptr = &dc_val[x + y * wrap]; | |
1575 return pred_dc; | |
1576 } | |
1577 #endif /* CONFIG_ENCODERS */ | |
1578 | 1734 |
1579 static void h263_pred_acdc(MpegEncContext * s, DCTELEM *block, int n) | 1735 static void h263_pred_acdc(MpegEncContext * s, DCTELEM *block, int n) |
1580 { | 1736 { |
1581 int x, y, wrap, a, c, pred_dc, scale, i; | 1737 int x, y, wrap, a, c, pred_dc, scale, i; |
1582 int16_t *dc_val, *ac_val, *ac_val1; | 1738 int16_t *dc_val, *ac_val, *ac_val1; |
1749 put_bits(&s->pb, bit_size, bits); | 1905 put_bits(&s->pb, bit_size, bits); |
1750 } | 1906 } |
1751 } | 1907 } |
1752 } | 1908 } |
1753 | 1909 |
1754 /* Encode MV differences on H.263+ with Unrestricted MV mode */ | |
1755 static void h263p_encode_umotion(MpegEncContext * s, int val) | |
1756 { | |
1757 short sval = 0; | |
1758 short i = 0; | |
1759 short n_bits = 0; | |
1760 short temp_val; | |
1761 int code = 0; | |
1762 int tcode; | |
1763 | |
1764 if ( val == 0) | |
1765 put_bits(&s->pb, 1, 1); | |
1766 else if (val == 1) | |
1767 put_bits(&s->pb, 3, 0); | |
1768 else if (val == -1) | |
1769 put_bits(&s->pb, 3, 2); | |
1770 else { | |
1771 | |
1772 sval = ((val < 0) ? (short)(-val):(short)val); | |
1773 temp_val = sval; | |
1774 | |
1775 while (temp_val != 0) { | |
1776 temp_val = temp_val >> 1; | |
1777 n_bits++; | |
1778 } | |
1779 | |
1780 i = n_bits - 1; | |
1781 while (i > 0) { | |
1782 tcode = (sval & (1 << (i-1))) >> (i-1); | |
1783 tcode = (tcode << 1) | 1; | |
1784 code = (code << 2) | tcode; | |
1785 i--; | |
1786 } | |
1787 code = ((code << 1) | (val < 0)) << 1; | |
1788 put_bits(&s->pb, (2*n_bits)+1, code); | |
1789 } | |
1790 } | |
1791 | |
1792 static void init_mv_penalty_and_fcode(MpegEncContext *s) | 1910 static void init_mv_penalty_and_fcode(MpegEncContext *s) |
1793 { | 1911 { |
1794 int f_code; | 1912 int f_code; |
1795 int mv; | 1913 int mv; |
1796 | 1914 |
2095 default: //nothing needed - default table already set in mpegvideo.c | 2213 default: //nothing needed - default table already set in mpegvideo.c |
2096 s->min_qcoeff= -127; | 2214 s->min_qcoeff= -127; |
2097 s->max_qcoeff= 127; | 2215 s->max_qcoeff= 127; |
2098 s->y_dc_scale_table= | 2216 s->y_dc_scale_table= |
2099 s->c_dc_scale_table= ff_mpeg1_dc_scale_table; | 2217 s->c_dc_scale_table= ff_mpeg1_dc_scale_table; |
2100 } | |
2101 } | |
2102 | |
2103 /** | |
2104 * encodes a 8x8 block. | |
2105 * @param block the 8x8 block | |
2106 * @param n block index (0-3 are luma, 4-5 are chroma) | |
2107 */ | |
2108 static void h263_encode_block(MpegEncContext * s, DCTELEM * block, int n) | |
2109 { | |
2110 int level, run, last, i, j, last_index, last_non_zero, sign, slevel, code; | |
2111 RLTable *rl; | |
2112 | |
2113 rl = &rl_inter; | |
2114 if (s->mb_intra && !s->h263_aic) { | |
2115 /* DC coef */ | |
2116 level = block[0]; | |
2117 /* 255 cannot be represented, so we clamp */ | |
2118 if (level > 254) { | |
2119 level = 254; | |
2120 block[0] = 254; | |
2121 } | |
2122 /* 0 cannot be represented also */ | |
2123 else if (level < 1) { | |
2124 level = 1; | |
2125 block[0] = 1; | |
2126 } | |
2127 if (level == 128) //FIXME check rv10 | |
2128 put_bits(&s->pb, 8, 0xff); | |
2129 else | |
2130 put_bits(&s->pb, 8, level); | |
2131 i = 1; | |
2132 } else { | |
2133 i = 0; | |
2134 if (s->h263_aic && s->mb_intra) | |
2135 rl = &rl_intra_aic; | |
2136 | |
2137 if(s->alt_inter_vlc && !s->mb_intra){ | |
2138 int aic_vlc_bits=0; | |
2139 int inter_vlc_bits=0; | |
2140 int wrong_pos=-1; | |
2141 int aic_code; | |
2142 | |
2143 last_index = s->block_last_index[n]; | |
2144 last_non_zero = i - 1; | |
2145 for (; i <= last_index; i++) { | |
2146 j = s->intra_scantable.permutated[i]; | |
2147 level = block[j]; | |
2148 if (level) { | |
2149 run = i - last_non_zero - 1; | |
2150 last = (i == last_index); | |
2151 | |
2152 if(level<0) level= -level; | |
2153 | |
2154 code = get_rl_index(rl, last, run, level); | |
2155 aic_code = get_rl_index(&rl_intra_aic, last, run, level); | |
2156 inter_vlc_bits += rl->table_vlc[code][1]+1; | |
2157 aic_vlc_bits += rl_intra_aic.table_vlc[aic_code][1]+1; | |
2158 | |
2159 if (code == rl->n) { | |
2160 inter_vlc_bits += 1+6+8-1; | |
2161 } | |
2162 if (aic_code == rl_intra_aic.n) { | |
2163 aic_vlc_bits += 1+6+8-1; | |
2164 wrong_pos += run + 1; | |
2165 }else | |
2166 wrong_pos += wrong_run[aic_code]; | |
2167 last_non_zero = i; | |
2168 } | |
2169 } | |
2170 i = 0; | |
2171 if(aic_vlc_bits < inter_vlc_bits && wrong_pos > 63) | |
2172 rl = &rl_intra_aic; | |
2173 } | |
2174 } | |
2175 | |
2176 /* AC coefs */ | |
2177 last_index = s->block_last_index[n]; | |
2178 last_non_zero = i - 1; | |
2179 for (; i <= last_index; i++) { | |
2180 j = s->intra_scantable.permutated[i]; | |
2181 level = block[j]; | |
2182 if (level) { | |
2183 run = i - last_non_zero - 1; | |
2184 last = (i == last_index); | |
2185 sign = 0; | |
2186 slevel = level; | |
2187 if (level < 0) { | |
2188 sign = 1; | |
2189 level = -level; | |
2190 } | |
2191 code = get_rl_index(rl, last, run, level); | |
2192 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]); | |
2193 if (code == rl->n) { | |
2194 if(s->h263_flv <= 1){ | |
2195 put_bits(&s->pb, 1, last); | |
2196 put_bits(&s->pb, 6, run); | |
2197 | |
2198 assert(slevel != 0); | |
2199 | |
2200 if(level < 128) | |
2201 put_sbits(&s->pb, 8, slevel); | |
2202 else{ | |
2203 put_bits(&s->pb, 8, 128); | |
2204 put_sbits(&s->pb, 5, slevel); | |
2205 put_sbits(&s->pb, 6, slevel>>5); | |
2206 } | |
2207 }else{ | |
2208 if(level < 64) { // 7-bit level | |
2209 put_bits(&s->pb, 1, 0); | |
2210 put_bits(&s->pb, 1, last); | |
2211 put_bits(&s->pb, 6, run); | |
2212 | |
2213 put_sbits(&s->pb, 7, slevel); | |
2214 } else { | |
2215 /* 11-bit level */ | |
2216 put_bits(&s->pb, 1, 1); | |
2217 put_bits(&s->pb, 1, last); | |
2218 put_bits(&s->pb, 6, run); | |
2219 | |
2220 put_sbits(&s->pb, 11, slevel); | |
2221 } | |
2222 } | |
2223 } else { | |
2224 put_bits(&s->pb, 1, sign); | |
2225 } | |
2226 last_non_zero = i; | |
2227 } | |
2228 } | 2218 } |
2229 } | 2219 } |
2230 | 2220 |
2231 /***************************************************/ | 2221 /***************************************************/ |
2232 /** | 2222 /** |
3323 } | 3313 } |
3324 | 3314 |
3325 return -1; | 3315 return -1; |
3326 } | 3316 } |
3327 | 3317 |
3318 static int h263_decode_motion(MpegEncContext * s, int pred, int f_code) | |
3319 { | |
3320 int code, val, sign, shift, l; | |
3321 code = get_vlc2(&s->gb, mv_vlc.table, MV_VLC_BITS, 2); | |
3322 | |
3323 if (code == 0) | |
3324 return pred; | |
3325 if (code < 0) | |
3326 return 0xffff; | |
3327 | |
3328 sign = get_bits1(&s->gb); | |
3329 shift = f_code - 1; | |
3330 val = code; | |
3331 if (shift) { | |
3332 val = (val - 1) << shift; | |
3333 val |= get_bits(&s->gb, shift); | |
3334 val++; | |
3335 } | |
3336 if (sign) | |
3337 val = -val; | |
3338 val += pred; | |
3339 | |
3340 /* modulo decoding */ | |
3341 if (!s->h263_long_vectors) { | |
3342 l = INT_BIT - 5 - f_code; | |
3343 val = (val<<l)>>l; | |
3344 } else { | |
3345 /* horrible h263 long vector mode */ | |
3346 if (pred < -31 && val < -63) | |
3347 val += 64; | |
3348 if (pred > 32 && val > 63) | |
3349 val -= 64; | |
3350 | |
3351 } | |
3352 return val; | |
3353 } | |
3354 | |
3328 /** | 3355 /** |
3329 * gets the average motion vector for a GMC MB. | 3356 * gets the average motion vector for a GMC MB. |
3330 * @param n either 0 for the x component or 1 for y | 3357 * @param n either 0 for the x component or 1 for y |
3331 * @returns the average MV for a GMC MB | 3358 * @returns the average MV for a GMC MB |
3332 */ | 3359 */ |
3760 } | 3787 } |
3761 return SLICE_OK; | 3788 return SLICE_OK; |
3762 } | 3789 } |
3763 } | 3790 } |
3764 | 3791 |
3792 /* Decodes RVLC of H.263+ UMV */ | |
3793 static int h263p_decode_umotion(MpegEncContext * s, int pred) | |
3794 { | |
3795 int code = 0, sign; | |
3796 | |
3797 if (get_bits1(&s->gb)) /* Motion difference = 0 */ | |
3798 return pred; | |
3799 | |
3800 code = 2 + get_bits1(&s->gb); | |
3801 | |
3802 while (get_bits1(&s->gb)) | |
3803 { | |
3804 code <<= 1; | |
3805 code += get_bits1(&s->gb); | |
3806 } | |
3807 sign = code & 1; | |
3808 code >>= 1; | |
3809 | |
3810 code = (sign) ? (pred - code) : (pred + code); | |
3811 dprintf(s->avctx,"H.263+ UMV Motion = %d\n", code); | |
3812 return code; | |
3813 | |
3814 } | |
3815 | |
3765 /** | 3816 /** |
3766 * read the next MVs for OBMC. yes this is a ugly hack, feel free to send a patch :) | 3817 * read the next MVs for OBMC. yes this is a ugly hack, feel free to send a patch :) |
3767 */ | 3818 */ |
3768 static void preview_obmc(MpegEncContext *s){ | 3819 static void preview_obmc(MpegEncContext *s){ |
3769 GetBitContext gb= s->gb; | 3820 GetBitContext gb= s->gb; |
3866 else | 3917 else |
3867 s->qscale= get_bits(&s->gb, 5); | 3918 s->qscale= get_bits(&s->gb, 5); |
3868 }else | 3919 }else |
3869 s->qscale += quant_tab[get_bits(&s->gb, 2)]; | 3920 s->qscale += quant_tab[get_bits(&s->gb, 2)]; |
3870 ff_set_qscale(s, s->qscale); | 3921 ff_set_qscale(s, s->qscale); |
3871 } | |
3872 | |
3873 static int h263_skip_b_part(MpegEncContext *s, int cbp) | |
3874 { | |
3875 DECLARE_ALIGNED(16, DCTELEM, dblock[64]); | |
3876 int i, mbi; | |
3877 | |
3878 /* we have to set s->mb_intra to zero to decode B-part of PB-frame correctly | |
3879 * but real value should be restored in order to be used later (in OBMC condition) | |
3880 */ | |
3881 mbi = s->mb_intra; | |
3882 s->mb_intra = 0; | |
3883 for (i = 0; i < 6; i++) { | |
3884 if (h263_decode_block(s, dblock, i, cbp&32) < 0) | |
3885 return -1; | |
3886 cbp+=cbp; | |
3887 } | |
3888 s->mb_intra = mbi; | |
3889 return 0; | |
3890 } | |
3891 | |
3892 static int h263_get_modb(GetBitContext *gb, int pb_frame, int *cbpb) | |
3893 { | |
3894 int c, mv = 1; | |
3895 | |
3896 if (pb_frame < 3) { // h.263 Annex G and i263 PB-frame | |
3897 c = get_bits1(gb); | |
3898 if (pb_frame == 2 && c) | |
3899 mv = !get_bits1(gb); | |
3900 } else { // h.263 Annex M improved PB-frame | |
3901 mv = get_unary(gb, 0, 4) + 1; | |
3902 c = mv & 1; | |
3903 mv = !!(mv & 2); | |
3904 } | |
3905 if(c) | |
3906 *cbpb = get_bits(gb, 6); | |
3907 return mv; | |
3908 } | |
3909 | |
3910 int ff_h263_decode_mb(MpegEncContext *s, | |
3911 DCTELEM block[6][64]) | |
3912 { | |
3913 int cbpc, cbpy, i, cbp, pred_x, pred_y, mx, my, dquant; | |
3914 int16_t *mot_val; | |
3915 const int xy= s->mb_x + s->mb_y * s->mb_stride; | |
3916 int cbpb = 0, pb_mv_count = 0; | |
3917 | |
3918 assert(!s->h263_pred); | |
3919 | |
3920 if (s->pict_type == FF_P_TYPE) { | |
3921 do{ | |
3922 if (get_bits1(&s->gb)) { | |
3923 /* skip mb */ | |
3924 s->mb_intra = 0; | |
3925 for(i=0;i<6;i++) | |
3926 s->block_last_index[i] = -1; | |
3927 s->mv_dir = MV_DIR_FORWARD; | |
3928 s->mv_type = MV_TYPE_16X16; | |
3929 s->current_picture.mb_type[xy]= MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0; | |
3930 s->mv[0][0][0] = 0; | |
3931 s->mv[0][0][1] = 0; | |
3932 s->mb_skipped = !(s->obmc | s->loop_filter); | |
3933 goto end; | |
3934 } | |
3935 cbpc = get_vlc2(&s->gb, inter_MCBPC_vlc.table, INTER_MCBPC_VLC_BITS, 2); | |
3936 if (cbpc < 0){ | |
3937 av_log(s->avctx, AV_LOG_ERROR, "cbpc damaged at %d %d\n", s->mb_x, s->mb_y); | |
3938 return -1; | |
3939 } | |
3940 }while(cbpc == 20); | |
3941 | |
3942 s->dsp.clear_blocks(s->block[0]); | |
3943 | |
3944 dquant = cbpc & 8; | |
3945 s->mb_intra = ((cbpc & 4) != 0); | |
3946 if (s->mb_intra) goto intra; | |
3947 | |
3948 if(s->pb_frame && get_bits1(&s->gb)) | |
3949 pb_mv_count = h263_get_modb(&s->gb, s->pb_frame, &cbpb); | |
3950 cbpy = get_vlc2(&s->gb, cbpy_vlc.table, CBPY_VLC_BITS, 1); | |
3951 | |
3952 if(s->alt_inter_vlc==0 || (cbpc & 3)!=3) | |
3953 cbpy ^= 0xF; | |
3954 | |
3955 cbp = (cbpc & 3) | (cbpy << 2); | |
3956 if (dquant) { | |
3957 h263_decode_dquant(s); | |
3958 } | |
3959 | |
3960 s->mv_dir = MV_DIR_FORWARD; | |
3961 if ((cbpc & 16) == 0) { | |
3962 s->current_picture.mb_type[xy]= MB_TYPE_16x16 | MB_TYPE_L0; | |
3963 /* 16x16 motion prediction */ | |
3964 s->mv_type = MV_TYPE_16X16; | |
3965 h263_pred_motion(s, 0, 0, &pred_x, &pred_y); | |
3966 if (s->umvplus) | |
3967 mx = h263p_decode_umotion(s, pred_x); | |
3968 else | |
3969 mx = h263_decode_motion(s, pred_x, 1); | |
3970 | |
3971 if (mx >= 0xffff) | |
3972 return -1; | |
3973 | |
3974 if (s->umvplus) | |
3975 my = h263p_decode_umotion(s, pred_y); | |
3976 else | |
3977 my = h263_decode_motion(s, pred_y, 1); | |
3978 | |
3979 if (my >= 0xffff) | |
3980 return -1; | |
3981 s->mv[0][0][0] = mx; | |
3982 s->mv[0][0][1] = my; | |
3983 | |
3984 if (s->umvplus && (mx - pred_x) == 1 && (my - pred_y) == 1) | |
3985 skip_bits1(&s->gb); /* Bit stuffing to prevent PSC */ | |
3986 } else { | |
3987 s->current_picture.mb_type[xy]= MB_TYPE_8x8 | MB_TYPE_L0; | |
3988 s->mv_type = MV_TYPE_8X8; | |
3989 for(i=0;i<4;i++) { | |
3990 mot_val = h263_pred_motion(s, i, 0, &pred_x, &pred_y); | |
3991 if (s->umvplus) | |
3992 mx = h263p_decode_umotion(s, pred_x); | |
3993 else | |
3994 mx = h263_decode_motion(s, pred_x, 1); | |
3995 if (mx >= 0xffff) | |
3996 return -1; | |
3997 | |
3998 if (s->umvplus) | |
3999 my = h263p_decode_umotion(s, pred_y); | |
4000 else | |
4001 my = h263_decode_motion(s, pred_y, 1); | |
4002 if (my >= 0xffff) | |
4003 return -1; | |
4004 s->mv[0][i][0] = mx; | |
4005 s->mv[0][i][1] = my; | |
4006 if (s->umvplus && (mx - pred_x) == 1 && (my - pred_y) == 1) | |
4007 skip_bits1(&s->gb); /* Bit stuffing to prevent PSC */ | |
4008 mot_val[0] = mx; | |
4009 mot_val[1] = my; | |
4010 } | |
4011 } | |
4012 } else if(s->pict_type==FF_B_TYPE) { | |
4013 int mb_type; | |
4014 const int stride= s->b8_stride; | |
4015 int16_t *mot_val0 = s->current_picture.motion_val[0][ 2*(s->mb_x + s->mb_y*stride) ]; | |
4016 int16_t *mot_val1 = s->current_picture.motion_val[1][ 2*(s->mb_x + s->mb_y*stride) ]; | |
4017 // const int mv_xy= s->mb_x + 1 + s->mb_y * s->mb_stride; | |
4018 | |
4019 //FIXME ugly | |
4020 mot_val0[0 ]= mot_val0[2 ]= mot_val0[0+2*stride]= mot_val0[2+2*stride]= | |
4021 mot_val0[1 ]= mot_val0[3 ]= mot_val0[1+2*stride]= mot_val0[3+2*stride]= | |
4022 mot_val1[0 ]= mot_val1[2 ]= mot_val1[0+2*stride]= mot_val1[2+2*stride]= | |
4023 mot_val1[1 ]= mot_val1[3 ]= mot_val1[1+2*stride]= mot_val1[3+2*stride]= 0; | |
4024 | |
4025 do{ | |
4026 mb_type= get_vlc2(&s->gb, h263_mbtype_b_vlc.table, H263_MBTYPE_B_VLC_BITS, 2); | |
4027 if (mb_type < 0){ | |
4028 av_log(s->avctx, AV_LOG_ERROR, "b mb_type damaged at %d %d\n", s->mb_x, s->mb_y); | |
4029 return -1; | |
4030 } | |
4031 | |
4032 mb_type= h263_mb_type_b_map[ mb_type ]; | |
4033 }while(!mb_type); | |
4034 | |
4035 s->mb_intra = IS_INTRA(mb_type); | |
4036 if(HAS_CBP(mb_type)){ | |
4037 s->dsp.clear_blocks(s->block[0]); | |
4038 cbpc = get_vlc2(&s->gb, cbpc_b_vlc.table, CBPC_B_VLC_BITS, 1); | |
4039 if(s->mb_intra){ | |
4040 dquant = IS_QUANT(mb_type); | |
4041 goto intra; | |
4042 } | |
4043 | |
4044 cbpy = get_vlc2(&s->gb, cbpy_vlc.table, CBPY_VLC_BITS, 1); | |
4045 | |
4046 if (cbpy < 0){ | |
4047 av_log(s->avctx, AV_LOG_ERROR, "b cbpy damaged at %d %d\n", s->mb_x, s->mb_y); | |
4048 return -1; | |
4049 } | |
4050 | |
4051 if(s->alt_inter_vlc==0 || (cbpc & 3)!=3) | |
4052 cbpy ^= 0xF; | |
4053 | |
4054 cbp = (cbpc & 3) | (cbpy << 2); | |
4055 }else | |
4056 cbp=0; | |
4057 | |
4058 assert(!s->mb_intra); | |
4059 | |
4060 if(IS_QUANT(mb_type)){ | |
4061 h263_decode_dquant(s); | |
4062 } | |
4063 | |
4064 if(IS_DIRECT(mb_type)){ | |
4065 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT; | |
4066 mb_type |= ff_mpeg4_set_direct_mv(s, 0, 0); | |
4067 }else{ | |
4068 s->mv_dir = 0; | |
4069 s->mv_type= MV_TYPE_16X16; | |
4070 //FIXME UMV | |
4071 | |
4072 if(USES_LIST(mb_type, 0)){ | |
4073 int16_t *mot_val= h263_pred_motion(s, 0, 0, &mx, &my); | |
4074 s->mv_dir = MV_DIR_FORWARD; | |
4075 | |
4076 mx = h263_decode_motion(s, mx, 1); | |
4077 my = h263_decode_motion(s, my, 1); | |
4078 | |
4079 s->mv[0][0][0] = mx; | |
4080 s->mv[0][0][1] = my; | |
4081 mot_val[0 ]= mot_val[2 ]= mot_val[0+2*stride]= mot_val[2+2*stride]= mx; | |
4082 mot_val[1 ]= mot_val[3 ]= mot_val[1+2*stride]= mot_val[3+2*stride]= my; | |
4083 } | |
4084 | |
4085 if(USES_LIST(mb_type, 1)){ | |
4086 int16_t *mot_val= h263_pred_motion(s, 0, 1, &mx, &my); | |
4087 s->mv_dir |= MV_DIR_BACKWARD; | |
4088 | |
4089 mx = h263_decode_motion(s, mx, 1); | |
4090 my = h263_decode_motion(s, my, 1); | |
4091 | |
4092 s->mv[1][0][0] = mx; | |
4093 s->mv[1][0][1] = my; | |
4094 mot_val[0 ]= mot_val[2 ]= mot_val[0+2*stride]= mot_val[2+2*stride]= mx; | |
4095 mot_val[1 ]= mot_val[3 ]= mot_val[1+2*stride]= mot_val[3+2*stride]= my; | |
4096 } | |
4097 } | |
4098 | |
4099 s->current_picture.mb_type[xy]= mb_type; | |
4100 } else { /* I-Frame */ | |
4101 do{ | |
4102 cbpc = get_vlc2(&s->gb, intra_MCBPC_vlc.table, INTRA_MCBPC_VLC_BITS, 2); | |
4103 if (cbpc < 0){ | |
4104 av_log(s->avctx, AV_LOG_ERROR, "I cbpc damaged at %d %d\n", s->mb_x, s->mb_y); | |
4105 return -1; | |
4106 } | |
4107 }while(cbpc == 8); | |
4108 | |
4109 s->dsp.clear_blocks(s->block[0]); | |
4110 | |
4111 dquant = cbpc & 4; | |
4112 s->mb_intra = 1; | |
4113 intra: | |
4114 s->current_picture.mb_type[xy]= MB_TYPE_INTRA; | |
4115 if (s->h263_aic) { | |
4116 s->ac_pred = get_bits1(&s->gb); | |
4117 if(s->ac_pred){ | |
4118 s->current_picture.mb_type[xy]= MB_TYPE_INTRA | MB_TYPE_ACPRED; | |
4119 | |
4120 s->h263_aic_dir = get_bits1(&s->gb); | |
4121 } | |
4122 }else | |
4123 s->ac_pred = 0; | |
4124 | |
4125 if(s->pb_frame && get_bits1(&s->gb)) | |
4126 pb_mv_count = h263_get_modb(&s->gb, s->pb_frame, &cbpb); | |
4127 cbpy = get_vlc2(&s->gb, cbpy_vlc.table, CBPY_VLC_BITS, 1); | |
4128 if(cbpy<0){ | |
4129 av_log(s->avctx, AV_LOG_ERROR, "I cbpy damaged at %d %d\n", s->mb_x, s->mb_y); | |
4130 return -1; | |
4131 } | |
4132 cbp = (cbpc & 3) | (cbpy << 2); | |
4133 if (dquant) { | |
4134 h263_decode_dquant(s); | |
4135 } | |
4136 | |
4137 pb_mv_count += !!s->pb_frame; | |
4138 } | |
4139 | |
4140 while(pb_mv_count--){ | |
4141 h263_decode_motion(s, 0, 1); | |
4142 h263_decode_motion(s, 0, 1); | |
4143 } | |
4144 | |
4145 /* decode each block */ | |
4146 for (i = 0; i < 6; i++) { | |
4147 if (h263_decode_block(s, block[i], i, cbp&32) < 0) | |
4148 return -1; | |
4149 cbp+=cbp; | |
4150 } | |
4151 | |
4152 if(s->pb_frame && h263_skip_b_part(s, cbpb) < 0) | |
4153 return -1; | |
4154 if(s->obmc && !s->mb_intra){ | |
4155 if(s->pict_type == FF_P_TYPE && s->mb_x+1<s->mb_width && s->mb_num_left != 1) | |
4156 preview_obmc(s); | |
4157 } | |
4158 end: | |
4159 | |
4160 /* per-MB end of slice check */ | |
4161 { | |
4162 int v= show_bits(&s->gb, 16); | |
4163 | |
4164 if(get_bits_count(&s->gb) + 16 > s->gb.size_in_bits){ | |
4165 v>>= get_bits_count(&s->gb) + 16 - s->gb.size_in_bits; | |
4166 } | |
4167 | |
4168 if(v==0) | |
4169 return SLICE_END; | |
4170 } | |
4171 | |
4172 return SLICE_OK; | |
4173 } | |
4174 | |
4175 int ff_mpeg4_decode_mb(MpegEncContext *s, | |
4176 DCTELEM block[6][64]) | |
4177 { | |
4178 int cbpc, cbpy, i, cbp, pred_x, pred_y, mx, my, dquant; | |
4179 int16_t *mot_val; | |
4180 static int8_t quant_tab[4] = { -1, -2, 1, 2 }; | |
4181 const int xy= s->mb_x + s->mb_y * s->mb_stride; | |
4182 | |
4183 assert(s->h263_pred); | |
4184 | |
4185 if (s->pict_type == FF_P_TYPE || s->pict_type==FF_S_TYPE) { | |
4186 do{ | |
4187 if (get_bits1(&s->gb)) { | |
4188 /* skip mb */ | |
4189 s->mb_intra = 0; | |
4190 for(i=0;i<6;i++) | |
4191 s->block_last_index[i] = -1; | |
4192 s->mv_dir = MV_DIR_FORWARD; | |
4193 s->mv_type = MV_TYPE_16X16; | |
4194 if(s->pict_type==FF_S_TYPE && s->vol_sprite_usage==GMC_SPRITE){ | |
4195 s->current_picture.mb_type[xy]= MB_TYPE_SKIP | MB_TYPE_GMC | MB_TYPE_16x16 | MB_TYPE_L0; | |
4196 s->mcsel=1; | |
4197 s->mv[0][0][0]= get_amv(s, 0); | |
4198 s->mv[0][0][1]= get_amv(s, 1); | |
4199 | |
4200 s->mb_skipped = 0; | |
4201 }else{ | |
4202 s->current_picture.mb_type[xy]= MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0; | |
4203 s->mcsel=0; | |
4204 s->mv[0][0][0] = 0; | |
4205 s->mv[0][0][1] = 0; | |
4206 s->mb_skipped = 1; | |
4207 } | |
4208 goto end; | |
4209 } | |
4210 cbpc = get_vlc2(&s->gb, inter_MCBPC_vlc.table, INTER_MCBPC_VLC_BITS, 2); | |
4211 if (cbpc < 0){ | |
4212 av_log(s->avctx, AV_LOG_ERROR, "cbpc damaged at %d %d\n", s->mb_x, s->mb_y); | |
4213 return -1; | |
4214 } | |
4215 }while(cbpc == 20); | |
4216 | |
4217 s->dsp.clear_blocks(s->block[0]); | |
4218 dquant = cbpc & 8; | |
4219 s->mb_intra = ((cbpc & 4) != 0); | |
4220 if (s->mb_intra) goto intra; | |
4221 | |
4222 if(s->pict_type==FF_S_TYPE && s->vol_sprite_usage==GMC_SPRITE && (cbpc & 16) == 0) | |
4223 s->mcsel= get_bits1(&s->gb); | |
4224 else s->mcsel= 0; | |
4225 cbpy = get_vlc2(&s->gb, cbpy_vlc.table, CBPY_VLC_BITS, 1) ^ 0x0F; | |
4226 | |
4227 cbp = (cbpc & 3) | (cbpy << 2); | |
4228 if (dquant) { | |
4229 ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]); | |
4230 } | |
4231 if((!s->progressive_sequence) && (cbp || (s->workaround_bugs&FF_BUG_XVID_ILACE))) | |
4232 s->interlaced_dct= get_bits1(&s->gb); | |
4233 | |
4234 s->mv_dir = MV_DIR_FORWARD; | |
4235 if ((cbpc & 16) == 0) { | |
4236 if(s->mcsel){ | |
4237 s->current_picture.mb_type[xy]= MB_TYPE_GMC | MB_TYPE_16x16 | MB_TYPE_L0; | |
4238 /* 16x16 global motion prediction */ | |
4239 s->mv_type = MV_TYPE_16X16; | |
4240 mx= get_amv(s, 0); | |
4241 my= get_amv(s, 1); | |
4242 s->mv[0][0][0] = mx; | |
4243 s->mv[0][0][1] = my; | |
4244 }else if((!s->progressive_sequence) && get_bits1(&s->gb)){ | |
4245 s->current_picture.mb_type[xy]= MB_TYPE_16x8 | MB_TYPE_L0 | MB_TYPE_INTERLACED; | |
4246 /* 16x8 field motion prediction */ | |
4247 s->mv_type= MV_TYPE_FIELD; | |
4248 | |
4249 s->field_select[0][0]= get_bits1(&s->gb); | |
4250 s->field_select[0][1]= get_bits1(&s->gb); | |
4251 | |
4252 h263_pred_motion(s, 0, 0, &pred_x, &pred_y); | |
4253 | |
4254 for(i=0; i<2; i++){ | |
4255 mx = h263_decode_motion(s, pred_x, s->f_code); | |
4256 if (mx >= 0xffff) | |
4257 return -1; | |
4258 | |
4259 my = h263_decode_motion(s, pred_y/2, s->f_code); | |
4260 if (my >= 0xffff) | |
4261 return -1; | |
4262 | |
4263 s->mv[0][i][0] = mx; | |
4264 s->mv[0][i][1] = my; | |
4265 } | |
4266 }else{ | |
4267 s->current_picture.mb_type[xy]= MB_TYPE_16x16 | MB_TYPE_L0; | |
4268 /* 16x16 motion prediction */ | |
4269 s->mv_type = MV_TYPE_16X16; | |
4270 h263_pred_motion(s, 0, 0, &pred_x, &pred_y); | |
4271 mx = h263_decode_motion(s, pred_x, s->f_code); | |
4272 | |
4273 if (mx >= 0xffff) | |
4274 return -1; | |
4275 | |
4276 my = h263_decode_motion(s, pred_y, s->f_code); | |
4277 | |
4278 if (my >= 0xffff) | |
4279 return -1; | |
4280 s->mv[0][0][0] = mx; | |
4281 s->mv[0][0][1] = my; | |
4282 } | |
4283 } else { | |
4284 s->current_picture.mb_type[xy]= MB_TYPE_8x8 | MB_TYPE_L0; | |
4285 s->mv_type = MV_TYPE_8X8; | |
4286 for(i=0;i<4;i++) { | |
4287 mot_val = h263_pred_motion(s, i, 0, &pred_x, &pred_y); | |
4288 mx = h263_decode_motion(s, pred_x, s->f_code); | |
4289 if (mx >= 0xffff) | |
4290 return -1; | |
4291 | |
4292 my = h263_decode_motion(s, pred_y, s->f_code); | |
4293 if (my >= 0xffff) | |
4294 return -1; | |
4295 s->mv[0][i][0] = mx; | |
4296 s->mv[0][i][1] = my; | |
4297 mot_val[0] = mx; | |
4298 mot_val[1] = my; | |
4299 } | |
4300 } | |
4301 } else if(s->pict_type==FF_B_TYPE) { | |
4302 int modb1; // first bit of modb | |
4303 int modb2; // second bit of modb | |
4304 int mb_type; | |
4305 | |
4306 s->mb_intra = 0; //B-frames never contain intra blocks | |
4307 s->mcsel=0; // ... true gmc blocks | |
4308 | |
4309 if(s->mb_x==0){ | |
4310 for(i=0; i<2; i++){ | |
4311 s->last_mv[i][0][0]= | |
4312 s->last_mv[i][0][1]= | |
4313 s->last_mv[i][1][0]= | |
4314 s->last_mv[i][1][1]= 0; | |
4315 } | |
4316 } | |
4317 | |
4318 /* if we skipped it in the future P Frame than skip it now too */ | |
4319 s->mb_skipped= s->next_picture.mbskip_table[s->mb_y * s->mb_stride + s->mb_x]; // Note, skiptab=0 if last was GMC | |
4320 | |
4321 if(s->mb_skipped){ | |
4322 /* skip mb */ | |
4323 for(i=0;i<6;i++) | |
4324 s->block_last_index[i] = -1; | |
4325 | |
4326 s->mv_dir = MV_DIR_FORWARD; | |
4327 s->mv_type = MV_TYPE_16X16; | |
4328 s->mv[0][0][0] = 0; | |
4329 s->mv[0][0][1] = 0; | |
4330 s->mv[1][0][0] = 0; | |
4331 s->mv[1][0][1] = 0; | |
4332 s->current_picture.mb_type[xy]= MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0; | |
4333 goto end; | |
4334 } | |
4335 | |
4336 modb1= get_bits1(&s->gb); | |
4337 if(modb1){ | |
4338 mb_type= MB_TYPE_DIRECT2 | MB_TYPE_SKIP | MB_TYPE_L0L1; //like MB_TYPE_B_DIRECT but no vectors coded | |
4339 cbp=0; | |
4340 }else{ | |
4341 modb2= get_bits1(&s->gb); | |
4342 mb_type= get_vlc2(&s->gb, mb_type_b_vlc.table, MB_TYPE_B_VLC_BITS, 1); | |
4343 if(mb_type<0){ | |
4344 av_log(s->avctx, AV_LOG_ERROR, "illegal MB_type\n"); | |
4345 return -1; | |
4346 } | |
4347 mb_type= mb_type_b_map[ mb_type ]; | |
4348 if(modb2) cbp= 0; | |
4349 else{ | |
4350 s->dsp.clear_blocks(s->block[0]); | |
4351 cbp= get_bits(&s->gb, 6); | |
4352 } | |
4353 | |
4354 if ((!IS_DIRECT(mb_type)) && cbp) { | |
4355 if(get_bits1(&s->gb)){ | |
4356 ff_set_qscale(s, s->qscale + get_bits1(&s->gb)*4 - 2); | |
4357 } | |
4358 } | |
4359 | |
4360 if(!s->progressive_sequence){ | |
4361 if(cbp) | |
4362 s->interlaced_dct= get_bits1(&s->gb); | |
4363 | |
4364 if(!IS_DIRECT(mb_type) && get_bits1(&s->gb)){ | |
4365 mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED; | |
4366 mb_type &= ~MB_TYPE_16x16; | |
4367 | |
4368 if(USES_LIST(mb_type, 0)){ | |
4369 s->field_select[0][0]= get_bits1(&s->gb); | |
4370 s->field_select[0][1]= get_bits1(&s->gb); | |
4371 } | |
4372 if(USES_LIST(mb_type, 1)){ | |
4373 s->field_select[1][0]= get_bits1(&s->gb); | |
4374 s->field_select[1][1]= get_bits1(&s->gb); | |
4375 } | |
4376 } | |
4377 } | |
4378 | |
4379 s->mv_dir = 0; | |
4380 if((mb_type & (MB_TYPE_DIRECT2|MB_TYPE_INTERLACED)) == 0){ | |
4381 s->mv_type= MV_TYPE_16X16; | |
4382 | |
4383 if(USES_LIST(mb_type, 0)){ | |
4384 s->mv_dir = MV_DIR_FORWARD; | |
4385 | |
4386 mx = h263_decode_motion(s, s->last_mv[0][0][0], s->f_code); | |
4387 my = h263_decode_motion(s, s->last_mv[0][0][1], s->f_code); | |
4388 s->last_mv[0][1][0]= s->last_mv[0][0][0]= s->mv[0][0][0] = mx; | |
4389 s->last_mv[0][1][1]= s->last_mv[0][0][1]= s->mv[0][0][1] = my; | |
4390 } | |
4391 | |
4392 if(USES_LIST(mb_type, 1)){ | |
4393 s->mv_dir |= MV_DIR_BACKWARD; | |
4394 | |
4395 mx = h263_decode_motion(s, s->last_mv[1][0][0], s->b_code); | |
4396 my = h263_decode_motion(s, s->last_mv[1][0][1], s->b_code); | |
4397 s->last_mv[1][1][0]= s->last_mv[1][0][0]= s->mv[1][0][0] = mx; | |
4398 s->last_mv[1][1][1]= s->last_mv[1][0][1]= s->mv[1][0][1] = my; | |
4399 } | |
4400 }else if(!IS_DIRECT(mb_type)){ | |
4401 s->mv_type= MV_TYPE_FIELD; | |
4402 | |
4403 if(USES_LIST(mb_type, 0)){ | |
4404 s->mv_dir = MV_DIR_FORWARD; | |
4405 | |
4406 for(i=0; i<2; i++){ | |
4407 mx = h263_decode_motion(s, s->last_mv[0][i][0] , s->f_code); | |
4408 my = h263_decode_motion(s, s->last_mv[0][i][1]/2, s->f_code); | |
4409 s->last_mv[0][i][0]= s->mv[0][i][0] = mx; | |
4410 s->last_mv[0][i][1]= (s->mv[0][i][1] = my)*2; | |
4411 } | |
4412 } | |
4413 | |
4414 if(USES_LIST(mb_type, 1)){ | |
4415 s->mv_dir |= MV_DIR_BACKWARD; | |
4416 | |
4417 for(i=0; i<2; i++){ | |
4418 mx = h263_decode_motion(s, s->last_mv[1][i][0] , s->b_code); | |
4419 my = h263_decode_motion(s, s->last_mv[1][i][1]/2, s->b_code); | |
4420 s->last_mv[1][i][0]= s->mv[1][i][0] = mx; | |
4421 s->last_mv[1][i][1]= (s->mv[1][i][1] = my)*2; | |
4422 } | |
4423 } | |
4424 } | |
4425 } | |
4426 | |
4427 if(IS_DIRECT(mb_type)){ | |
4428 if(IS_SKIP(mb_type)) | |
4429 mx=my=0; | |
4430 else{ | |
4431 mx = h263_decode_motion(s, 0, 1); | |
4432 my = h263_decode_motion(s, 0, 1); | |
4433 } | |
4434 | |
4435 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT; | |
4436 mb_type |= ff_mpeg4_set_direct_mv(s, mx, my); | |
4437 } | |
4438 s->current_picture.mb_type[xy]= mb_type; | |
4439 } else { /* I-Frame */ | |
4440 do{ | |
4441 cbpc = get_vlc2(&s->gb, intra_MCBPC_vlc.table, INTRA_MCBPC_VLC_BITS, 2); | |
4442 if (cbpc < 0){ | |
4443 av_log(s->avctx, AV_LOG_ERROR, "I cbpc damaged at %d %d\n", s->mb_x, s->mb_y); | |
4444 return -1; | |
4445 } | |
4446 }while(cbpc == 8); | |
4447 | |
4448 dquant = cbpc & 4; | |
4449 s->mb_intra = 1; | |
4450 intra: | |
4451 s->ac_pred = get_bits1(&s->gb); | |
4452 if(s->ac_pred) | |
4453 s->current_picture.mb_type[xy]= MB_TYPE_INTRA | MB_TYPE_ACPRED; | |
4454 else | |
4455 s->current_picture.mb_type[xy]= MB_TYPE_INTRA; | |
4456 | |
4457 cbpy = get_vlc2(&s->gb, cbpy_vlc.table, CBPY_VLC_BITS, 1); | |
4458 if(cbpy<0){ | |
4459 av_log(s->avctx, AV_LOG_ERROR, "I cbpy damaged at %d %d\n", s->mb_x, s->mb_y); | |
4460 return -1; | |
4461 } | |
4462 cbp = (cbpc & 3) | (cbpy << 2); | |
4463 | |
4464 s->use_intra_dc_vlc= s->qscale < s->intra_dc_threshold; | |
4465 | |
4466 if (dquant) { | |
4467 ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]); | |
4468 } | |
4469 | |
4470 if(!s->progressive_sequence) | |
4471 s->interlaced_dct= get_bits1(&s->gb); | |
4472 | |
4473 s->dsp.clear_blocks(s->block[0]); | |
4474 /* decode each block */ | |
4475 for (i = 0; i < 6; i++) { | |
4476 if (mpeg4_decode_block(s, block[i], i, cbp&32, 1, 0) < 0) | |
4477 return -1; | |
4478 cbp+=cbp; | |
4479 } | |
4480 goto end; | |
4481 } | |
4482 | |
4483 /* decode each block */ | |
4484 for (i = 0; i < 6; i++) { | |
4485 if (mpeg4_decode_block(s, block[i], i, cbp&32, 0, 0) < 0) | |
4486 return -1; | |
4487 cbp+=cbp; | |
4488 } | |
4489 end: | |
4490 | |
4491 /* per-MB end of slice check */ | |
4492 if(s->codec_id==CODEC_ID_MPEG4){ | |
4493 if(mpeg4_is_resync(s)){ | |
4494 const int delta= s->mb_x + 1 == s->mb_width ? 2 : 1; | |
4495 if(s->pict_type==FF_B_TYPE && s->next_picture.mbskip_table[xy + delta]) | |
4496 return SLICE_OK; | |
4497 return SLICE_END; | |
4498 } | |
4499 } | |
4500 | |
4501 return SLICE_OK; | |
4502 } | |
4503 | |
4504 static int h263_decode_motion(MpegEncContext * s, int pred, int f_code) | |
4505 { | |
4506 int code, val, sign, shift, l; | |
4507 code = get_vlc2(&s->gb, mv_vlc.table, MV_VLC_BITS, 2); | |
4508 | |
4509 if (code == 0) | |
4510 return pred; | |
4511 if (code < 0) | |
4512 return 0xffff; | |
4513 | |
4514 sign = get_bits1(&s->gb); | |
4515 shift = f_code - 1; | |
4516 val = code; | |
4517 if (shift) { | |
4518 val = (val - 1) << shift; | |
4519 val |= get_bits(&s->gb, shift); | |
4520 val++; | |
4521 } | |
4522 if (sign) | |
4523 val = -val; | |
4524 val += pred; | |
4525 | |
4526 /* modulo decoding */ | |
4527 if (!s->h263_long_vectors) { | |
4528 l = INT_BIT - 5 - f_code; | |
4529 val = (val<<l)>>l; | |
4530 } else { | |
4531 /* horrible h263 long vector mode */ | |
4532 if (pred < -31 && val < -63) | |
4533 val += 64; | |
4534 if (pred > 32 && val > 63) | |
4535 val -= 64; | |
4536 | |
4537 } | |
4538 return val; | |
4539 } | |
4540 | |
4541 /* Decodes RVLC of H.263+ UMV */ | |
4542 static int h263p_decode_umotion(MpegEncContext * s, int pred) | |
4543 { | |
4544 int code = 0, sign; | |
4545 | |
4546 if (get_bits1(&s->gb)) /* Motion difference = 0 */ | |
4547 return pred; | |
4548 | |
4549 code = 2 + get_bits1(&s->gb); | |
4550 | |
4551 while (get_bits1(&s->gb)) | |
4552 { | |
4553 code <<= 1; | |
4554 code += get_bits1(&s->gb); | |
4555 } | |
4556 sign = code & 1; | |
4557 code >>= 1; | |
4558 | |
4559 code = (sign) ? (pred - code) : (pred + code); | |
4560 dprintf(s->avctx,"H.263+ UMV Motion = %d\n", code); | |
4561 return code; | |
4562 | |
4563 } | 3922 } |
4564 | 3923 |
4565 static int h263_decode_block(MpegEncContext * s, DCTELEM * block, | 3924 static int h263_decode_block(MpegEncContext * s, DCTELEM * block, |
4566 int n, int coded) | 3925 int n, int coded) |
4567 { | 3926 { |
4688 h263_pred_acdc(s, block, n); | 4047 h263_pred_acdc(s, block, n); |
4689 i = 63; | 4048 i = 63; |
4690 } | 4049 } |
4691 s->block_last_index[n] = i; | 4050 s->block_last_index[n] = i; |
4692 return 0; | 4051 return 0; |
4052 } | |
4053 | |
4054 static int h263_skip_b_part(MpegEncContext *s, int cbp) | |
4055 { | |
4056 DECLARE_ALIGNED(16, DCTELEM, dblock[64]); | |
4057 int i, mbi; | |
4058 | |
4059 /* we have to set s->mb_intra to zero to decode B-part of PB-frame correctly | |
4060 * but real value should be restored in order to be used later (in OBMC condition) | |
4061 */ | |
4062 mbi = s->mb_intra; | |
4063 s->mb_intra = 0; | |
4064 for (i = 0; i < 6; i++) { | |
4065 if (h263_decode_block(s, dblock, i, cbp&32) < 0) | |
4066 return -1; | |
4067 cbp+=cbp; | |
4068 } | |
4069 s->mb_intra = mbi; | |
4070 return 0; | |
4071 } | |
4072 | |
4073 static int h263_get_modb(GetBitContext *gb, int pb_frame, int *cbpb) | |
4074 { | |
4075 int c, mv = 1; | |
4076 | |
4077 if (pb_frame < 3) { // h.263 Annex G and i263 PB-frame | |
4078 c = get_bits1(gb); | |
4079 if (pb_frame == 2 && c) | |
4080 mv = !get_bits1(gb); | |
4081 } else { // h.263 Annex M improved PB-frame | |
4082 mv = get_unary(gb, 0, 4) + 1; | |
4083 c = mv & 1; | |
4084 mv = !!(mv & 2); | |
4085 } | |
4086 if(c) | |
4087 *cbpb = get_bits(gb, 6); | |
4088 return mv; | |
4089 } | |
4090 | |
4091 int ff_h263_decode_mb(MpegEncContext *s, | |
4092 DCTELEM block[6][64]) | |
4093 { | |
4094 int cbpc, cbpy, i, cbp, pred_x, pred_y, mx, my, dquant; | |
4095 int16_t *mot_val; | |
4096 const int xy= s->mb_x + s->mb_y * s->mb_stride; | |
4097 int cbpb = 0, pb_mv_count = 0; | |
4098 | |
4099 assert(!s->h263_pred); | |
4100 | |
4101 if (s->pict_type == FF_P_TYPE) { | |
4102 do{ | |
4103 if (get_bits1(&s->gb)) { | |
4104 /* skip mb */ | |
4105 s->mb_intra = 0; | |
4106 for(i=0;i<6;i++) | |
4107 s->block_last_index[i] = -1; | |
4108 s->mv_dir = MV_DIR_FORWARD; | |
4109 s->mv_type = MV_TYPE_16X16; | |
4110 s->current_picture.mb_type[xy]= MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0; | |
4111 s->mv[0][0][0] = 0; | |
4112 s->mv[0][0][1] = 0; | |
4113 s->mb_skipped = !(s->obmc | s->loop_filter); | |
4114 goto end; | |
4115 } | |
4116 cbpc = get_vlc2(&s->gb, inter_MCBPC_vlc.table, INTER_MCBPC_VLC_BITS, 2); | |
4117 if (cbpc < 0){ | |
4118 av_log(s->avctx, AV_LOG_ERROR, "cbpc damaged at %d %d\n", s->mb_x, s->mb_y); | |
4119 return -1; | |
4120 } | |
4121 }while(cbpc == 20); | |
4122 | |
4123 s->dsp.clear_blocks(s->block[0]); | |
4124 | |
4125 dquant = cbpc & 8; | |
4126 s->mb_intra = ((cbpc & 4) != 0); | |
4127 if (s->mb_intra) goto intra; | |
4128 | |
4129 if(s->pb_frame && get_bits1(&s->gb)) | |
4130 pb_mv_count = h263_get_modb(&s->gb, s->pb_frame, &cbpb); | |
4131 cbpy = get_vlc2(&s->gb, cbpy_vlc.table, CBPY_VLC_BITS, 1); | |
4132 | |
4133 if(s->alt_inter_vlc==0 || (cbpc & 3)!=3) | |
4134 cbpy ^= 0xF; | |
4135 | |
4136 cbp = (cbpc & 3) | (cbpy << 2); | |
4137 if (dquant) { | |
4138 h263_decode_dquant(s); | |
4139 } | |
4140 | |
4141 s->mv_dir = MV_DIR_FORWARD; | |
4142 if ((cbpc & 16) == 0) { | |
4143 s->current_picture.mb_type[xy]= MB_TYPE_16x16 | MB_TYPE_L0; | |
4144 /* 16x16 motion prediction */ | |
4145 s->mv_type = MV_TYPE_16X16; | |
4146 h263_pred_motion(s, 0, 0, &pred_x, &pred_y); | |
4147 if (s->umvplus) | |
4148 mx = h263p_decode_umotion(s, pred_x); | |
4149 else | |
4150 mx = h263_decode_motion(s, pred_x, 1); | |
4151 | |
4152 if (mx >= 0xffff) | |
4153 return -1; | |
4154 | |
4155 if (s->umvplus) | |
4156 my = h263p_decode_umotion(s, pred_y); | |
4157 else | |
4158 my = h263_decode_motion(s, pred_y, 1); | |
4159 | |
4160 if (my >= 0xffff) | |
4161 return -1; | |
4162 s->mv[0][0][0] = mx; | |
4163 s->mv[0][0][1] = my; | |
4164 | |
4165 if (s->umvplus && (mx - pred_x) == 1 && (my - pred_y) == 1) | |
4166 skip_bits1(&s->gb); /* Bit stuffing to prevent PSC */ | |
4167 } else { | |
4168 s->current_picture.mb_type[xy]= MB_TYPE_8x8 | MB_TYPE_L0; | |
4169 s->mv_type = MV_TYPE_8X8; | |
4170 for(i=0;i<4;i++) { | |
4171 mot_val = h263_pred_motion(s, i, 0, &pred_x, &pred_y); | |
4172 if (s->umvplus) | |
4173 mx = h263p_decode_umotion(s, pred_x); | |
4174 else | |
4175 mx = h263_decode_motion(s, pred_x, 1); | |
4176 if (mx >= 0xffff) | |
4177 return -1; | |
4178 | |
4179 if (s->umvplus) | |
4180 my = h263p_decode_umotion(s, pred_y); | |
4181 else | |
4182 my = h263_decode_motion(s, pred_y, 1); | |
4183 if (my >= 0xffff) | |
4184 return -1; | |
4185 s->mv[0][i][0] = mx; | |
4186 s->mv[0][i][1] = my; | |
4187 if (s->umvplus && (mx - pred_x) == 1 && (my - pred_y) == 1) | |
4188 skip_bits1(&s->gb); /* Bit stuffing to prevent PSC */ | |
4189 mot_val[0] = mx; | |
4190 mot_val[1] = my; | |
4191 } | |
4192 } | |
4193 } else if(s->pict_type==FF_B_TYPE) { | |
4194 int mb_type; | |
4195 const int stride= s->b8_stride; | |
4196 int16_t *mot_val0 = s->current_picture.motion_val[0][ 2*(s->mb_x + s->mb_y*stride) ]; | |
4197 int16_t *mot_val1 = s->current_picture.motion_val[1][ 2*(s->mb_x + s->mb_y*stride) ]; | |
4198 // const int mv_xy= s->mb_x + 1 + s->mb_y * s->mb_stride; | |
4199 | |
4200 //FIXME ugly | |
4201 mot_val0[0 ]= mot_val0[2 ]= mot_val0[0+2*stride]= mot_val0[2+2*stride]= | |
4202 mot_val0[1 ]= mot_val0[3 ]= mot_val0[1+2*stride]= mot_val0[3+2*stride]= | |
4203 mot_val1[0 ]= mot_val1[2 ]= mot_val1[0+2*stride]= mot_val1[2+2*stride]= | |
4204 mot_val1[1 ]= mot_val1[3 ]= mot_val1[1+2*stride]= mot_val1[3+2*stride]= 0; | |
4205 | |
4206 do{ | |
4207 mb_type= get_vlc2(&s->gb, h263_mbtype_b_vlc.table, H263_MBTYPE_B_VLC_BITS, 2); | |
4208 if (mb_type < 0){ | |
4209 av_log(s->avctx, AV_LOG_ERROR, "b mb_type damaged at %d %d\n", s->mb_x, s->mb_y); | |
4210 return -1; | |
4211 } | |
4212 | |
4213 mb_type= h263_mb_type_b_map[ mb_type ]; | |
4214 }while(!mb_type); | |
4215 | |
4216 s->mb_intra = IS_INTRA(mb_type); | |
4217 if(HAS_CBP(mb_type)){ | |
4218 s->dsp.clear_blocks(s->block[0]); | |
4219 cbpc = get_vlc2(&s->gb, cbpc_b_vlc.table, CBPC_B_VLC_BITS, 1); | |
4220 if(s->mb_intra){ | |
4221 dquant = IS_QUANT(mb_type); | |
4222 goto intra; | |
4223 } | |
4224 | |
4225 cbpy = get_vlc2(&s->gb, cbpy_vlc.table, CBPY_VLC_BITS, 1); | |
4226 | |
4227 if (cbpy < 0){ | |
4228 av_log(s->avctx, AV_LOG_ERROR, "b cbpy damaged at %d %d\n", s->mb_x, s->mb_y); | |
4229 return -1; | |
4230 } | |
4231 | |
4232 if(s->alt_inter_vlc==0 || (cbpc & 3)!=3) | |
4233 cbpy ^= 0xF; | |
4234 | |
4235 cbp = (cbpc & 3) | (cbpy << 2); | |
4236 }else | |
4237 cbp=0; | |
4238 | |
4239 assert(!s->mb_intra); | |
4240 | |
4241 if(IS_QUANT(mb_type)){ | |
4242 h263_decode_dquant(s); | |
4243 } | |
4244 | |
4245 if(IS_DIRECT(mb_type)){ | |
4246 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT; | |
4247 mb_type |= ff_mpeg4_set_direct_mv(s, 0, 0); | |
4248 }else{ | |
4249 s->mv_dir = 0; | |
4250 s->mv_type= MV_TYPE_16X16; | |
4251 //FIXME UMV | |
4252 | |
4253 if(USES_LIST(mb_type, 0)){ | |
4254 int16_t *mot_val= h263_pred_motion(s, 0, 0, &mx, &my); | |
4255 s->mv_dir = MV_DIR_FORWARD; | |
4256 | |
4257 mx = h263_decode_motion(s, mx, 1); | |
4258 my = h263_decode_motion(s, my, 1); | |
4259 | |
4260 s->mv[0][0][0] = mx; | |
4261 s->mv[0][0][1] = my; | |
4262 mot_val[0 ]= mot_val[2 ]= mot_val[0+2*stride]= mot_val[2+2*stride]= mx; | |
4263 mot_val[1 ]= mot_val[3 ]= mot_val[1+2*stride]= mot_val[3+2*stride]= my; | |
4264 } | |
4265 | |
4266 if(USES_LIST(mb_type, 1)){ | |
4267 int16_t *mot_val= h263_pred_motion(s, 0, 1, &mx, &my); | |
4268 s->mv_dir |= MV_DIR_BACKWARD; | |
4269 | |
4270 mx = h263_decode_motion(s, mx, 1); | |
4271 my = h263_decode_motion(s, my, 1); | |
4272 | |
4273 s->mv[1][0][0] = mx; | |
4274 s->mv[1][0][1] = my; | |
4275 mot_val[0 ]= mot_val[2 ]= mot_val[0+2*stride]= mot_val[2+2*stride]= mx; | |
4276 mot_val[1 ]= mot_val[3 ]= mot_val[1+2*stride]= mot_val[3+2*stride]= my; | |
4277 } | |
4278 } | |
4279 | |
4280 s->current_picture.mb_type[xy]= mb_type; | |
4281 } else { /* I-Frame */ | |
4282 do{ | |
4283 cbpc = get_vlc2(&s->gb, intra_MCBPC_vlc.table, INTRA_MCBPC_VLC_BITS, 2); | |
4284 if (cbpc < 0){ | |
4285 av_log(s->avctx, AV_LOG_ERROR, "I cbpc damaged at %d %d\n", s->mb_x, s->mb_y); | |
4286 return -1; | |
4287 } | |
4288 }while(cbpc == 8); | |
4289 | |
4290 s->dsp.clear_blocks(s->block[0]); | |
4291 | |
4292 dquant = cbpc & 4; | |
4293 s->mb_intra = 1; | |
4294 intra: | |
4295 s->current_picture.mb_type[xy]= MB_TYPE_INTRA; | |
4296 if (s->h263_aic) { | |
4297 s->ac_pred = get_bits1(&s->gb); | |
4298 if(s->ac_pred){ | |
4299 s->current_picture.mb_type[xy]= MB_TYPE_INTRA | MB_TYPE_ACPRED; | |
4300 | |
4301 s->h263_aic_dir = get_bits1(&s->gb); | |
4302 } | |
4303 }else | |
4304 s->ac_pred = 0; | |
4305 | |
4306 if(s->pb_frame && get_bits1(&s->gb)) | |
4307 pb_mv_count = h263_get_modb(&s->gb, s->pb_frame, &cbpb); | |
4308 cbpy = get_vlc2(&s->gb, cbpy_vlc.table, CBPY_VLC_BITS, 1); | |
4309 if(cbpy<0){ | |
4310 av_log(s->avctx, AV_LOG_ERROR, "I cbpy damaged at %d %d\n", s->mb_x, s->mb_y); | |
4311 return -1; | |
4312 } | |
4313 cbp = (cbpc & 3) | (cbpy << 2); | |
4314 if (dquant) { | |
4315 h263_decode_dquant(s); | |
4316 } | |
4317 | |
4318 pb_mv_count += !!s->pb_frame; | |
4319 } | |
4320 | |
4321 while(pb_mv_count--){ | |
4322 h263_decode_motion(s, 0, 1); | |
4323 h263_decode_motion(s, 0, 1); | |
4324 } | |
4325 | |
4326 /* decode each block */ | |
4327 for (i = 0; i < 6; i++) { | |
4328 if (h263_decode_block(s, block[i], i, cbp&32) < 0) | |
4329 return -1; | |
4330 cbp+=cbp; | |
4331 } | |
4332 | |
4333 if(s->pb_frame && h263_skip_b_part(s, cbpb) < 0) | |
4334 return -1; | |
4335 if(s->obmc && !s->mb_intra){ | |
4336 if(s->pict_type == FF_P_TYPE && s->mb_x+1<s->mb_width && s->mb_num_left != 1) | |
4337 preview_obmc(s); | |
4338 } | |
4339 end: | |
4340 | |
4341 /* per-MB end of slice check */ | |
4342 { | |
4343 int v= show_bits(&s->gb, 16); | |
4344 | |
4345 if(get_bits_count(&s->gb) + 16 > s->gb.size_in_bits){ | |
4346 v>>= get_bits_count(&s->gb) + 16 - s->gb.size_in_bits; | |
4347 } | |
4348 | |
4349 if(v==0) | |
4350 return SLICE_END; | |
4351 } | |
4352 | |
4353 return SLICE_OK; | |
4354 } | |
4355 | |
4356 int ff_mpeg4_decode_mb(MpegEncContext *s, | |
4357 DCTELEM block[6][64]) | |
4358 { | |
4359 int cbpc, cbpy, i, cbp, pred_x, pred_y, mx, my, dquant; | |
4360 int16_t *mot_val; | |
4361 static int8_t quant_tab[4] = { -1, -2, 1, 2 }; | |
4362 const int xy= s->mb_x + s->mb_y * s->mb_stride; | |
4363 | |
4364 assert(s->h263_pred); | |
4365 | |
4366 if (s->pict_type == FF_P_TYPE || s->pict_type==FF_S_TYPE) { | |
4367 do{ | |
4368 if (get_bits1(&s->gb)) { | |
4369 /* skip mb */ | |
4370 s->mb_intra = 0; | |
4371 for(i=0;i<6;i++) | |
4372 s->block_last_index[i] = -1; | |
4373 s->mv_dir = MV_DIR_FORWARD; | |
4374 s->mv_type = MV_TYPE_16X16; | |
4375 if(s->pict_type==FF_S_TYPE && s->vol_sprite_usage==GMC_SPRITE){ | |
4376 s->current_picture.mb_type[xy]= MB_TYPE_SKIP | MB_TYPE_GMC | MB_TYPE_16x16 | MB_TYPE_L0; | |
4377 s->mcsel=1; | |
4378 s->mv[0][0][0]= get_amv(s, 0); | |
4379 s->mv[0][0][1]= get_amv(s, 1); | |
4380 | |
4381 s->mb_skipped = 0; | |
4382 }else{ | |
4383 s->current_picture.mb_type[xy]= MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0; | |
4384 s->mcsel=0; | |
4385 s->mv[0][0][0] = 0; | |
4386 s->mv[0][0][1] = 0; | |
4387 s->mb_skipped = 1; | |
4388 } | |
4389 goto end; | |
4390 } | |
4391 cbpc = get_vlc2(&s->gb, inter_MCBPC_vlc.table, INTER_MCBPC_VLC_BITS, 2); | |
4392 if (cbpc < 0){ | |
4393 av_log(s->avctx, AV_LOG_ERROR, "cbpc damaged at %d %d\n", s->mb_x, s->mb_y); | |
4394 return -1; | |
4395 } | |
4396 }while(cbpc == 20); | |
4397 | |
4398 s->dsp.clear_blocks(s->block[0]); | |
4399 dquant = cbpc & 8; | |
4400 s->mb_intra = ((cbpc & 4) != 0); | |
4401 if (s->mb_intra) goto intra; | |
4402 | |
4403 if(s->pict_type==FF_S_TYPE && s->vol_sprite_usage==GMC_SPRITE && (cbpc & 16) == 0) | |
4404 s->mcsel= get_bits1(&s->gb); | |
4405 else s->mcsel= 0; | |
4406 cbpy = get_vlc2(&s->gb, cbpy_vlc.table, CBPY_VLC_BITS, 1) ^ 0x0F; | |
4407 | |
4408 cbp = (cbpc & 3) | (cbpy << 2); | |
4409 if (dquant) { | |
4410 ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]); | |
4411 } | |
4412 if((!s->progressive_sequence) && (cbp || (s->workaround_bugs&FF_BUG_XVID_ILACE))) | |
4413 s->interlaced_dct= get_bits1(&s->gb); | |
4414 | |
4415 s->mv_dir = MV_DIR_FORWARD; | |
4416 if ((cbpc & 16) == 0) { | |
4417 if(s->mcsel){ | |
4418 s->current_picture.mb_type[xy]= MB_TYPE_GMC | MB_TYPE_16x16 | MB_TYPE_L0; | |
4419 /* 16x16 global motion prediction */ | |
4420 s->mv_type = MV_TYPE_16X16; | |
4421 mx= get_amv(s, 0); | |
4422 my= get_amv(s, 1); | |
4423 s->mv[0][0][0] = mx; | |
4424 s->mv[0][0][1] = my; | |
4425 }else if((!s->progressive_sequence) && get_bits1(&s->gb)){ | |
4426 s->current_picture.mb_type[xy]= MB_TYPE_16x8 | MB_TYPE_L0 | MB_TYPE_INTERLACED; | |
4427 /* 16x8 field motion prediction */ | |
4428 s->mv_type= MV_TYPE_FIELD; | |
4429 | |
4430 s->field_select[0][0]= get_bits1(&s->gb); | |
4431 s->field_select[0][1]= get_bits1(&s->gb); | |
4432 | |
4433 h263_pred_motion(s, 0, 0, &pred_x, &pred_y); | |
4434 | |
4435 for(i=0; i<2; i++){ | |
4436 mx = h263_decode_motion(s, pred_x, s->f_code); | |
4437 if (mx >= 0xffff) | |
4438 return -1; | |
4439 | |
4440 my = h263_decode_motion(s, pred_y/2, s->f_code); | |
4441 if (my >= 0xffff) | |
4442 return -1; | |
4443 | |
4444 s->mv[0][i][0] = mx; | |
4445 s->mv[0][i][1] = my; | |
4446 } | |
4447 }else{ | |
4448 s->current_picture.mb_type[xy]= MB_TYPE_16x16 | MB_TYPE_L0; | |
4449 /* 16x16 motion prediction */ | |
4450 s->mv_type = MV_TYPE_16X16; | |
4451 h263_pred_motion(s, 0, 0, &pred_x, &pred_y); | |
4452 mx = h263_decode_motion(s, pred_x, s->f_code); | |
4453 | |
4454 if (mx >= 0xffff) | |
4455 return -1; | |
4456 | |
4457 my = h263_decode_motion(s, pred_y, s->f_code); | |
4458 | |
4459 if (my >= 0xffff) | |
4460 return -1; | |
4461 s->mv[0][0][0] = mx; | |
4462 s->mv[0][0][1] = my; | |
4463 } | |
4464 } else { | |
4465 s->current_picture.mb_type[xy]= MB_TYPE_8x8 | MB_TYPE_L0; | |
4466 s->mv_type = MV_TYPE_8X8; | |
4467 for(i=0;i<4;i++) { | |
4468 mot_val = h263_pred_motion(s, i, 0, &pred_x, &pred_y); | |
4469 mx = h263_decode_motion(s, pred_x, s->f_code); | |
4470 if (mx >= 0xffff) | |
4471 return -1; | |
4472 | |
4473 my = h263_decode_motion(s, pred_y, s->f_code); | |
4474 if (my >= 0xffff) | |
4475 return -1; | |
4476 s->mv[0][i][0] = mx; | |
4477 s->mv[0][i][1] = my; | |
4478 mot_val[0] = mx; | |
4479 mot_val[1] = my; | |
4480 } | |
4481 } | |
4482 } else if(s->pict_type==FF_B_TYPE) { | |
4483 int modb1; // first bit of modb | |
4484 int modb2; // second bit of modb | |
4485 int mb_type; | |
4486 | |
4487 s->mb_intra = 0; //B-frames never contain intra blocks | |
4488 s->mcsel=0; // ... true gmc blocks | |
4489 | |
4490 if(s->mb_x==0){ | |
4491 for(i=0; i<2; i++){ | |
4492 s->last_mv[i][0][0]= | |
4493 s->last_mv[i][0][1]= | |
4494 s->last_mv[i][1][0]= | |
4495 s->last_mv[i][1][1]= 0; | |
4496 } | |
4497 } | |
4498 | |
4499 /* if we skipped it in the future P Frame than skip it now too */ | |
4500 s->mb_skipped= s->next_picture.mbskip_table[s->mb_y * s->mb_stride + s->mb_x]; // Note, skiptab=0 if last was GMC | |
4501 | |
4502 if(s->mb_skipped){ | |
4503 /* skip mb */ | |
4504 for(i=0;i<6;i++) | |
4505 s->block_last_index[i] = -1; | |
4506 | |
4507 s->mv_dir = MV_DIR_FORWARD; | |
4508 s->mv_type = MV_TYPE_16X16; | |
4509 s->mv[0][0][0] = 0; | |
4510 s->mv[0][0][1] = 0; | |
4511 s->mv[1][0][0] = 0; | |
4512 s->mv[1][0][1] = 0; | |
4513 s->current_picture.mb_type[xy]= MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0; | |
4514 goto end; | |
4515 } | |
4516 | |
4517 modb1= get_bits1(&s->gb); | |
4518 if(modb1){ | |
4519 mb_type= MB_TYPE_DIRECT2 | MB_TYPE_SKIP | MB_TYPE_L0L1; //like MB_TYPE_B_DIRECT but no vectors coded | |
4520 cbp=0; | |
4521 }else{ | |
4522 modb2= get_bits1(&s->gb); | |
4523 mb_type= get_vlc2(&s->gb, mb_type_b_vlc.table, MB_TYPE_B_VLC_BITS, 1); | |
4524 if(mb_type<0){ | |
4525 av_log(s->avctx, AV_LOG_ERROR, "illegal MB_type\n"); | |
4526 return -1; | |
4527 } | |
4528 mb_type= mb_type_b_map[ mb_type ]; | |
4529 if(modb2) cbp= 0; | |
4530 else{ | |
4531 s->dsp.clear_blocks(s->block[0]); | |
4532 cbp= get_bits(&s->gb, 6); | |
4533 } | |
4534 | |
4535 if ((!IS_DIRECT(mb_type)) && cbp) { | |
4536 if(get_bits1(&s->gb)){ | |
4537 ff_set_qscale(s, s->qscale + get_bits1(&s->gb)*4 - 2); | |
4538 } | |
4539 } | |
4540 | |
4541 if(!s->progressive_sequence){ | |
4542 if(cbp) | |
4543 s->interlaced_dct= get_bits1(&s->gb); | |
4544 | |
4545 if(!IS_DIRECT(mb_type) && get_bits1(&s->gb)){ | |
4546 mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED; | |
4547 mb_type &= ~MB_TYPE_16x16; | |
4548 | |
4549 if(USES_LIST(mb_type, 0)){ | |
4550 s->field_select[0][0]= get_bits1(&s->gb); | |
4551 s->field_select[0][1]= get_bits1(&s->gb); | |
4552 } | |
4553 if(USES_LIST(mb_type, 1)){ | |
4554 s->field_select[1][0]= get_bits1(&s->gb); | |
4555 s->field_select[1][1]= get_bits1(&s->gb); | |
4556 } | |
4557 } | |
4558 } | |
4559 | |
4560 s->mv_dir = 0; | |
4561 if((mb_type & (MB_TYPE_DIRECT2|MB_TYPE_INTERLACED)) == 0){ | |
4562 s->mv_type= MV_TYPE_16X16; | |
4563 | |
4564 if(USES_LIST(mb_type, 0)){ | |
4565 s->mv_dir = MV_DIR_FORWARD; | |
4566 | |
4567 mx = h263_decode_motion(s, s->last_mv[0][0][0], s->f_code); | |
4568 my = h263_decode_motion(s, s->last_mv[0][0][1], s->f_code); | |
4569 s->last_mv[0][1][0]= s->last_mv[0][0][0]= s->mv[0][0][0] = mx; | |
4570 s->last_mv[0][1][1]= s->last_mv[0][0][1]= s->mv[0][0][1] = my; | |
4571 } | |
4572 | |
4573 if(USES_LIST(mb_type, 1)){ | |
4574 s->mv_dir |= MV_DIR_BACKWARD; | |
4575 | |
4576 mx = h263_decode_motion(s, s->last_mv[1][0][0], s->b_code); | |
4577 my = h263_decode_motion(s, s->last_mv[1][0][1], s->b_code); | |
4578 s->last_mv[1][1][0]= s->last_mv[1][0][0]= s->mv[1][0][0] = mx; | |
4579 s->last_mv[1][1][1]= s->last_mv[1][0][1]= s->mv[1][0][1] = my; | |
4580 } | |
4581 }else if(!IS_DIRECT(mb_type)){ | |
4582 s->mv_type= MV_TYPE_FIELD; | |
4583 | |
4584 if(USES_LIST(mb_type, 0)){ | |
4585 s->mv_dir = MV_DIR_FORWARD; | |
4586 | |
4587 for(i=0; i<2; i++){ | |
4588 mx = h263_decode_motion(s, s->last_mv[0][i][0] , s->f_code); | |
4589 my = h263_decode_motion(s, s->last_mv[0][i][1]/2, s->f_code); | |
4590 s->last_mv[0][i][0]= s->mv[0][i][0] = mx; | |
4591 s->last_mv[0][i][1]= (s->mv[0][i][1] = my)*2; | |
4592 } | |
4593 } | |
4594 | |
4595 if(USES_LIST(mb_type, 1)){ | |
4596 s->mv_dir |= MV_DIR_BACKWARD; | |
4597 | |
4598 for(i=0; i<2; i++){ | |
4599 mx = h263_decode_motion(s, s->last_mv[1][i][0] , s->b_code); | |
4600 my = h263_decode_motion(s, s->last_mv[1][i][1]/2, s->b_code); | |
4601 s->last_mv[1][i][0]= s->mv[1][i][0] = mx; | |
4602 s->last_mv[1][i][1]= (s->mv[1][i][1] = my)*2; | |
4603 } | |
4604 } | |
4605 } | |
4606 } | |
4607 | |
4608 if(IS_DIRECT(mb_type)){ | |
4609 if(IS_SKIP(mb_type)) | |
4610 mx=my=0; | |
4611 else{ | |
4612 mx = h263_decode_motion(s, 0, 1); | |
4613 my = h263_decode_motion(s, 0, 1); | |
4614 } | |
4615 | |
4616 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT; | |
4617 mb_type |= ff_mpeg4_set_direct_mv(s, mx, my); | |
4618 } | |
4619 s->current_picture.mb_type[xy]= mb_type; | |
4620 } else { /* I-Frame */ | |
4621 do{ | |
4622 cbpc = get_vlc2(&s->gb, intra_MCBPC_vlc.table, INTRA_MCBPC_VLC_BITS, 2); | |
4623 if (cbpc < 0){ | |
4624 av_log(s->avctx, AV_LOG_ERROR, "I cbpc damaged at %d %d\n", s->mb_x, s->mb_y); | |
4625 return -1; | |
4626 } | |
4627 }while(cbpc == 8); | |
4628 | |
4629 dquant = cbpc & 4; | |
4630 s->mb_intra = 1; | |
4631 intra: | |
4632 s->ac_pred = get_bits1(&s->gb); | |
4633 if(s->ac_pred) | |
4634 s->current_picture.mb_type[xy]= MB_TYPE_INTRA | MB_TYPE_ACPRED; | |
4635 else | |
4636 s->current_picture.mb_type[xy]= MB_TYPE_INTRA; | |
4637 | |
4638 cbpy = get_vlc2(&s->gb, cbpy_vlc.table, CBPY_VLC_BITS, 1); | |
4639 if(cbpy<0){ | |
4640 av_log(s->avctx, AV_LOG_ERROR, "I cbpy damaged at %d %d\n", s->mb_x, s->mb_y); | |
4641 return -1; | |
4642 } | |
4643 cbp = (cbpc & 3) | (cbpy << 2); | |
4644 | |
4645 s->use_intra_dc_vlc= s->qscale < s->intra_dc_threshold; | |
4646 | |
4647 if (dquant) { | |
4648 ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]); | |
4649 } | |
4650 | |
4651 if(!s->progressive_sequence) | |
4652 s->interlaced_dct= get_bits1(&s->gb); | |
4653 | |
4654 s->dsp.clear_blocks(s->block[0]); | |
4655 /* decode each block */ | |
4656 for (i = 0; i < 6; i++) { | |
4657 if (mpeg4_decode_block(s, block[i], i, cbp&32, 1, 0) < 0) | |
4658 return -1; | |
4659 cbp+=cbp; | |
4660 } | |
4661 goto end; | |
4662 } | |
4663 | |
4664 /* decode each block */ | |
4665 for (i = 0; i < 6; i++) { | |
4666 if (mpeg4_decode_block(s, block[i], i, cbp&32, 0, 0) < 0) | |
4667 return -1; | |
4668 cbp+=cbp; | |
4669 } | |
4670 end: | |
4671 | |
4672 /* per-MB end of slice check */ | |
4673 if(s->codec_id==CODEC_ID_MPEG4){ | |
4674 if(mpeg4_is_resync(s)){ | |
4675 const int delta= s->mb_x + 1 == s->mb_width ? 2 : 1; | |
4676 if(s->pict_type==FF_B_TYPE && s->next_picture.mbskip_table[xy + delta]) | |
4677 return SLICE_OK; | |
4678 return SLICE_END; | |
4679 } | |
4680 } | |
4681 | |
4682 return SLICE_OK; | |
4693 } | 4683 } |
4694 | 4684 |
4695 /** | 4685 /** |
4696 * decodes the dc value. | 4686 * decodes the dc value. |
4697 * @param n block index (0-3 are luma, 4-5 are chroma) | 4687 * @param n block index (0-3 are luma, 4-5 are chroma) |