You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1151 lines
37KB

  1. /*
  2. * ITU H.263 bitstream decoder
  3. * Copyright (c) 2000,2001 Fabrice Bellard
  4. * H.263+ support.
  5. * Copyright (c) 2001 Juan J. Sierralta P
  6. * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
  7. *
  8. * This file is part of Libav.
  9. *
  10. * Libav is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2.1 of the License, or (at your option) any later version.
  14. *
  15. * Libav is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with Libav; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. */
  24. /**
  25. * @file
  26. * H.263 decoder.
  27. */
  28. #include <limits.h>
  29. #include "libavutil/attributes.h"
  30. #include "libavutil/imgutils.h"
  31. #include "libavutil/internal.h"
  32. #include "libavutil/mathematics.h"
  33. #include "avcodec.h"
  34. #include "mpegvideo.h"
  35. #include "h263.h"
  36. #include "h263data.h"
  37. #include "internal.h"
  38. #include "mathops.h"
  39. #include "mpegutils.h"
  40. #include "unary.h"
  41. #include "flv.h"
  42. #include "rv10.h"
  43. #include "mpeg4video.h"
  44. #include "mpegvideodata.h"
  45. // The defines below define the number of bits that are read at once for
  46. // reading vlc values. Changing these may improve speed and data cache needs
  47. // be aware though that decreasing them may need the number of stages that is
  48. // passed to get_vlc* to be increased.
  49. #define MV_VLC_BITS 9
  50. #define H263_MBTYPE_B_VLC_BITS 6
  51. #define CBPC_B_VLC_BITS 3
  52. static const int h263_mb_type_b_map[15]= {
  53. MB_TYPE_DIRECT2 | MB_TYPE_L0L1,
  54. MB_TYPE_DIRECT2 | MB_TYPE_L0L1 | MB_TYPE_CBP,
  55. MB_TYPE_DIRECT2 | MB_TYPE_L0L1 | MB_TYPE_CBP | MB_TYPE_QUANT,
  56. MB_TYPE_L0 | MB_TYPE_16x16,
  57. MB_TYPE_L0 | MB_TYPE_CBP | MB_TYPE_16x16,
  58. MB_TYPE_L0 | MB_TYPE_CBP | MB_TYPE_QUANT | MB_TYPE_16x16,
  59. MB_TYPE_L1 | MB_TYPE_16x16,
  60. MB_TYPE_L1 | MB_TYPE_CBP | MB_TYPE_16x16,
  61. MB_TYPE_L1 | MB_TYPE_CBP | MB_TYPE_QUANT | MB_TYPE_16x16,
  62. MB_TYPE_L0L1 | MB_TYPE_16x16,
  63. MB_TYPE_L0L1 | MB_TYPE_CBP | MB_TYPE_16x16,
  64. MB_TYPE_L0L1 | MB_TYPE_CBP | MB_TYPE_QUANT | MB_TYPE_16x16,
  65. 0, //stuffing
  66. MB_TYPE_INTRA4x4 | MB_TYPE_CBP,
  67. MB_TYPE_INTRA4x4 | MB_TYPE_CBP | MB_TYPE_QUANT,
  68. };
  69. void ff_h263_show_pict_info(MpegEncContext *s){
  70. if(s->avctx->debug&FF_DEBUG_PICT_INFO){
  71. av_log(s->avctx, AV_LOG_DEBUG, "qp:%d %c size:%d rnd:%d%s%s%s%s%s%s%s%s%s %d/%d\n",
  72. s->qscale, av_get_picture_type_char(s->pict_type),
  73. s->gb.size_in_bits, 1-s->no_rounding,
  74. s->obmc ? " AP" : "",
  75. s->umvplus ? " UMV" : "",
  76. s->h263_long_vectors ? " LONG" : "",
  77. s->h263_plus ? " +" : "",
  78. s->h263_aic ? " AIC" : "",
  79. s->alt_inter_vlc ? " AIV" : "",
  80. s->modified_quant ? " MQ" : "",
  81. s->loop_filter ? " LOOP" : "",
  82. s->h263_slice_structured ? " SS" : "",
  83. s->avctx->framerate.num, s->avctx->framerate.den
  84. );
  85. }
  86. }
  87. /***********************************************/
  88. /* decoding */
  89. VLC ff_h263_intra_MCBPC_vlc;
  90. VLC ff_h263_inter_MCBPC_vlc;
  91. VLC ff_h263_cbpy_vlc;
  92. static VLC mv_vlc;
  93. static VLC h263_mbtype_b_vlc;
  94. static VLC cbpc_b_vlc;
  95. /* init vlcs */
  96. /* XXX: find a better solution to handle static init */
  97. av_cold void ff_h263_decode_init_vlc(void)
  98. {
  99. static int done = 0;
  100. if (!done) {
  101. done = 1;
  102. INIT_VLC_STATIC(&ff_h263_intra_MCBPC_vlc, INTRA_MCBPC_VLC_BITS, 9,
  103. ff_h263_intra_MCBPC_bits, 1, 1,
  104. ff_h263_intra_MCBPC_code, 1, 1, 72);
  105. INIT_VLC_STATIC(&ff_h263_inter_MCBPC_vlc, INTER_MCBPC_VLC_BITS, 28,
  106. ff_h263_inter_MCBPC_bits, 1, 1,
  107. ff_h263_inter_MCBPC_code, 1, 1, 198);
  108. INIT_VLC_STATIC(&ff_h263_cbpy_vlc, CBPY_VLC_BITS, 16,
  109. &ff_h263_cbpy_tab[0][1], 2, 1,
  110. &ff_h263_cbpy_tab[0][0], 2, 1, 64);
  111. INIT_VLC_STATIC(&mv_vlc, MV_VLC_BITS, 33,
  112. &ff_mvtab[0][1], 2, 1,
  113. &ff_mvtab[0][0], 2, 1, 538);
  114. ff_rl_init(&ff_h263_rl_inter, ff_h263_static_rl_table_store[0]);
  115. ff_rl_init(&ff_rl_intra_aic, ff_h263_static_rl_table_store[1]);
  116. INIT_VLC_RL(ff_h263_rl_inter, 554);
  117. INIT_VLC_RL(ff_rl_intra_aic, 554);
  118. INIT_VLC_STATIC(&h263_mbtype_b_vlc, H263_MBTYPE_B_VLC_BITS, 15,
  119. &ff_h263_mbtype_b_tab[0][1], 2, 1,
  120. &ff_h263_mbtype_b_tab[0][0], 2, 1, 80);
  121. INIT_VLC_STATIC(&cbpc_b_vlc, CBPC_B_VLC_BITS, 4,
  122. &ff_cbpc_b_tab[0][1], 2, 1,
  123. &ff_cbpc_b_tab[0][0], 2, 1, 8);
  124. }
  125. }
  126. int ff_h263_decode_mba(MpegEncContext *s)
  127. {
  128. int i, mb_pos;
  129. for (i = 0; i < 6; i++)
  130. if (s->mb_num - 1 <= ff_mba_max[i])
  131. break;
  132. mb_pos = get_bits(&s->gb, ff_mba_length[i]);
  133. s->mb_x = mb_pos % s->mb_width;
  134. s->mb_y = mb_pos / s->mb_width;
  135. return mb_pos;
  136. }
  137. /**
  138. * Decode the group of blocks header or slice header.
  139. * @return <0 if an error occurred
  140. */
  141. static int h263_decode_gob_header(MpegEncContext *s)
  142. {
  143. unsigned int val, gob_number;
  144. int left;
  145. /* Check for GOB Start Code */
  146. val = show_bits(&s->gb, 16);
  147. if(val)
  148. return -1;
  149. /* We have a GBSC probably with GSTUFF */
  150. skip_bits(&s->gb, 16); /* Drop the zeros */
  151. left= get_bits_left(&s->gb);
  152. //MN: we must check the bits left or we might end in a infinite loop (or segfault)
  153. for(;left>13; left--){
  154. if(get_bits1(&s->gb)) break; /* Seek the '1' bit */
  155. }
  156. if(left<=13)
  157. return -1;
  158. if(s->h263_slice_structured){
  159. if(get_bits1(&s->gb)==0)
  160. return -1;
  161. ff_h263_decode_mba(s);
  162. if(s->mb_num > 1583)
  163. if(get_bits1(&s->gb)==0)
  164. return -1;
  165. s->qscale = get_bits(&s->gb, 5); /* SQUANT */
  166. if(get_bits1(&s->gb)==0)
  167. return -1;
  168. skip_bits(&s->gb, 2); /* GFID */
  169. }else{
  170. gob_number = get_bits(&s->gb, 5); /* GN */
  171. s->mb_x= 0;
  172. s->mb_y= s->gob_index* gob_number;
  173. skip_bits(&s->gb, 2); /* GFID */
  174. s->qscale = get_bits(&s->gb, 5); /* GQUANT */
  175. }
  176. if(s->mb_y >= s->mb_height)
  177. return -1;
  178. if(s->qscale==0)
  179. return -1;
  180. return 0;
  181. }
  182. /**
  183. * Find the next resync_marker.
  184. * @param p pointer to buffer to scan
  185. * @param end pointer to the end of the buffer
  186. * @return pointer to the next resync_marker, or end if none was found
  187. */
  188. const uint8_t *ff_h263_find_resync_marker(const uint8_t *restrict p, const uint8_t * restrict end)
  189. {
  190. assert(p < end);
  191. end-=2;
  192. p++;
  193. for(;p<end; p+=2){
  194. if(!*p){
  195. if (!p[-1] && p[1]) return p - 1;
  196. else if(!p[ 1] && p[2]) return p;
  197. }
  198. }
  199. return end+2;
  200. }
  201. /**
  202. * Decode the group of blocks / video packet header.
  203. * @return bit position of the resync_marker, or <0 if none was found
  204. */
  205. int ff_h263_resync(MpegEncContext *s){
  206. int left, pos, ret;
  207. if(s->codec_id==AV_CODEC_ID_MPEG4){
  208. skip_bits1(&s->gb);
  209. align_get_bits(&s->gb);
  210. }
  211. if(show_bits(&s->gb, 16)==0){
  212. pos= get_bits_count(&s->gb);
  213. if(CONFIG_MPEG4_DECODER && s->codec_id==AV_CODEC_ID_MPEG4)
  214. ret= ff_mpeg4_decode_video_packet_header(s->avctx->priv_data);
  215. else
  216. ret= h263_decode_gob_header(s);
  217. if(ret>=0)
  218. return pos;
  219. }
  220. //OK, it's not where it is supposed to be ...
  221. s->gb= s->last_resync_gb;
  222. align_get_bits(&s->gb);
  223. left= get_bits_left(&s->gb);
  224. for(;left>16+1+5+5; left-=8){
  225. if(show_bits(&s->gb, 16)==0){
  226. GetBitContext bak= s->gb;
  227. pos= get_bits_count(&s->gb);
  228. if(CONFIG_MPEG4_DECODER && s->codec_id==AV_CODEC_ID_MPEG4)
  229. ret= ff_mpeg4_decode_video_packet_header(s->avctx->priv_data);
  230. else
  231. ret= h263_decode_gob_header(s);
  232. if(ret>=0)
  233. return pos;
  234. s->gb= bak;
  235. }
  236. skip_bits(&s->gb, 8);
  237. }
  238. return -1;
  239. }
  240. int ff_h263_decode_motion(MpegEncContext * s, int pred, int f_code)
  241. {
  242. int code, val, sign, shift;
  243. code = get_vlc2(&s->gb, mv_vlc.table, MV_VLC_BITS, 2);
  244. if (code == 0)
  245. return pred;
  246. if (code < 0)
  247. return 0xffff;
  248. sign = get_bits1(&s->gb);
  249. shift = f_code - 1;
  250. val = code;
  251. if (shift) {
  252. val = (val - 1) << shift;
  253. val |= get_bits(&s->gb, shift);
  254. val++;
  255. }
  256. if (sign)
  257. val = -val;
  258. val += pred;
  259. /* modulo decoding */
  260. if (!s->h263_long_vectors) {
  261. val = sign_extend(val, 5 + f_code);
  262. } else {
  263. /* horrible H.263 long vector mode */
  264. if (pred < -31 && val < -63)
  265. val += 64;
  266. if (pred > 32 && val > 63)
  267. val -= 64;
  268. }
  269. return val;
  270. }
  271. /* Decode RVLC of H.263+ UMV */
  272. static int h263p_decode_umotion(MpegEncContext * s, int pred)
  273. {
  274. int code = 0, sign;
  275. if (get_bits1(&s->gb)) /* Motion difference = 0 */
  276. return pred;
  277. code = 2 + get_bits1(&s->gb);
  278. while (get_bits1(&s->gb))
  279. {
  280. code <<= 1;
  281. code += get_bits1(&s->gb);
  282. }
  283. sign = code & 1;
  284. code >>= 1;
  285. code = (sign) ? (pred - code) : (pred + code);
  286. ff_dlog(s->avctx,"H.263+ UMV Motion = %d\n", code);
  287. return code;
  288. }
  289. /**
  290. * read the next MVs for OBMC. yes this is a ugly hack, feel free to send a patch :)
  291. */
  292. static void preview_obmc(MpegEncContext *s){
  293. GetBitContext gb= s->gb;
  294. int cbpc, i, pred_x, pred_y, mx, my;
  295. int16_t *mot_val;
  296. const int xy= s->mb_x + 1 + s->mb_y * s->mb_stride;
  297. const int stride= s->b8_stride*2;
  298. for(i=0; i<4; i++)
  299. s->block_index[i]+= 2;
  300. for(i=4; i<6; i++)
  301. s->block_index[i]+= 1;
  302. s->mb_x++;
  303. assert(s->pict_type == AV_PICTURE_TYPE_P);
  304. do{
  305. if (get_bits1(&s->gb)) {
  306. /* skip mb */
  307. mot_val = s->current_picture.motion_val[0][s->block_index[0]];
  308. mot_val[0 ]= mot_val[2 ]=
  309. mot_val[0+stride]= mot_val[2+stride]= 0;
  310. mot_val[1 ]= mot_val[3 ]=
  311. mot_val[1+stride]= mot_val[3+stride]= 0;
  312. s->current_picture.mb_type[xy] = MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
  313. goto end;
  314. }
  315. cbpc = get_vlc2(&s->gb, ff_h263_inter_MCBPC_vlc.table, INTER_MCBPC_VLC_BITS, 2);
  316. }while(cbpc == 20);
  317. if(cbpc & 4){
  318. s->current_picture.mb_type[xy] = MB_TYPE_INTRA;
  319. }else{
  320. get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
  321. if (cbpc & 8) {
  322. if(s->modified_quant){
  323. if(get_bits1(&s->gb)) skip_bits(&s->gb, 1);
  324. else skip_bits(&s->gb, 5);
  325. }else
  326. skip_bits(&s->gb, 2);
  327. }
  328. if ((cbpc & 16) == 0) {
  329. s->current_picture.mb_type[xy] = MB_TYPE_16x16 | MB_TYPE_L0;
  330. /* 16x16 motion prediction */
  331. mot_val= ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
  332. if (s->umvplus)
  333. mx = h263p_decode_umotion(s, pred_x);
  334. else
  335. mx = ff_h263_decode_motion(s, pred_x, 1);
  336. if (s->umvplus)
  337. my = h263p_decode_umotion(s, pred_y);
  338. else
  339. my = ff_h263_decode_motion(s, pred_y, 1);
  340. mot_val[0 ]= mot_val[2 ]=
  341. mot_val[0+stride]= mot_val[2+stride]= mx;
  342. mot_val[1 ]= mot_val[3 ]=
  343. mot_val[1+stride]= mot_val[3+stride]= my;
  344. } else {
  345. s->current_picture.mb_type[xy] = MB_TYPE_8x8 | MB_TYPE_L0;
  346. for(i=0;i<4;i++) {
  347. mot_val = ff_h263_pred_motion(s, i, 0, &pred_x, &pred_y);
  348. if (s->umvplus)
  349. mx = h263p_decode_umotion(s, pred_x);
  350. else
  351. mx = ff_h263_decode_motion(s, pred_x, 1);
  352. if (s->umvplus)
  353. my = h263p_decode_umotion(s, pred_y);
  354. else
  355. my = ff_h263_decode_motion(s, pred_y, 1);
  356. if (s->umvplus && (mx - pred_x) == 1 && (my - pred_y) == 1)
  357. skip_bits1(&s->gb); /* Bit stuffing to prevent PSC */
  358. mot_val[0] = mx;
  359. mot_val[1] = my;
  360. }
  361. }
  362. }
  363. end:
  364. for(i=0; i<4; i++)
  365. s->block_index[i]-= 2;
  366. for(i=4; i<6; i++)
  367. s->block_index[i]-= 1;
  368. s->mb_x--;
  369. s->gb= gb;
  370. }
  371. static void h263_decode_dquant(MpegEncContext *s){
  372. static const int8_t quant_tab[4] = { -1, -2, 1, 2 };
  373. if(s->modified_quant){
  374. if(get_bits1(&s->gb))
  375. s->qscale= ff_modified_quant_tab[get_bits1(&s->gb)][ s->qscale ];
  376. else
  377. s->qscale= get_bits(&s->gb, 5);
  378. }else
  379. s->qscale += quant_tab[get_bits(&s->gb, 2)];
  380. ff_set_qscale(s, s->qscale);
  381. }
  382. static int h263_decode_block(MpegEncContext * s, int16_t * block,
  383. int n, int coded)
  384. {
  385. int code, level, i, j, last, run;
  386. RLTable *rl = &ff_h263_rl_inter;
  387. const uint8_t *scan_table;
  388. GetBitContext gb= s->gb;
  389. scan_table = s->intra_scantable.permutated;
  390. if (s->h263_aic && s->mb_intra) {
  391. rl = &ff_rl_intra_aic;
  392. i = 0;
  393. if (s->ac_pred) {
  394. if (s->h263_aic_dir)
  395. scan_table = s->intra_v_scantable.permutated; /* left */
  396. else
  397. scan_table = s->intra_h_scantable.permutated; /* top */
  398. }
  399. } else if (s->mb_intra) {
  400. /* DC coef */
  401. if (CONFIG_RV10_DECODER && s->codec_id == AV_CODEC_ID_RV10) {
  402. if (s->rv10_version == 3 && s->pict_type == AV_PICTURE_TYPE_I) {
  403. int component, diff;
  404. component = (n <= 3 ? 0 : n - 4 + 1);
  405. level = s->last_dc[component];
  406. if (s->rv10_first_dc_coded[component]) {
  407. diff = ff_rv_decode_dc(s, n);
  408. if (diff == 0xffff)
  409. return -1;
  410. level += diff;
  411. level = level & 0xff; /* handle wrap round */
  412. s->last_dc[component] = level;
  413. } else {
  414. s->rv10_first_dc_coded[component] = 1;
  415. }
  416. } else {
  417. level = get_bits(&s->gb, 8);
  418. if (level == 255)
  419. level = 128;
  420. }
  421. }else{
  422. level = get_bits(&s->gb, 8);
  423. if((level&0x7F) == 0){
  424. av_log(s->avctx, AV_LOG_ERROR, "illegal dc %d at %d %d\n", level, s->mb_x, s->mb_y);
  425. if (s->avctx->err_recognition & AV_EF_BITSTREAM)
  426. return -1;
  427. }
  428. if (level == 255)
  429. level = 128;
  430. }
  431. block[0] = level;
  432. i = 1;
  433. } else {
  434. i = 0;
  435. }
  436. if (!coded) {
  437. if (s->mb_intra && s->h263_aic)
  438. goto not_coded;
  439. s->block_last_index[n] = i - 1;
  440. return 0;
  441. }
  442. retry:
  443. for(;;) {
  444. code = get_vlc2(&s->gb, rl->vlc.table, TEX_VLC_BITS, 2);
  445. if (code < 0){
  446. av_log(s->avctx, AV_LOG_ERROR, "illegal ac vlc code at %dx%d\n", s->mb_x, s->mb_y);
  447. return -1;
  448. }
  449. if (code == rl->n) {
  450. /* escape */
  451. if (CONFIG_FLV_DECODER && s->h263_flv > 1) {
  452. ff_flv2_decode_ac_esc(&s->gb, &level, &run, &last);
  453. } else {
  454. last = get_bits1(&s->gb);
  455. run = get_bits(&s->gb, 6);
  456. level = (int8_t)get_bits(&s->gb, 8);
  457. if(level == -128){
  458. if (s->codec_id == AV_CODEC_ID_RV10) {
  459. /* XXX: should patch encoder too */
  460. level = get_sbits(&s->gb, 12);
  461. }else{
  462. level = get_bits(&s->gb, 5);
  463. level |= get_sbits(&s->gb, 6)<<5;
  464. }
  465. }
  466. }
  467. } else {
  468. run = rl->table_run[code];
  469. level = rl->table_level[code];
  470. last = code >= rl->last;
  471. if (get_bits1(&s->gb))
  472. level = -level;
  473. }
  474. i += run;
  475. if (i >= 64){
  476. if(s->alt_inter_vlc && rl == &ff_h263_rl_inter && !s->mb_intra){
  477. //Looks like a hack but no, it's the way it is supposed to work ...
  478. rl = &ff_rl_intra_aic;
  479. i = 0;
  480. s->gb= gb;
  481. s->bdsp.clear_block(block);
  482. goto retry;
  483. }
  484. av_log(s->avctx, AV_LOG_ERROR, "run overflow at %dx%d i:%d\n", s->mb_x, s->mb_y, s->mb_intra);
  485. return -1;
  486. }
  487. j = scan_table[i];
  488. block[j] = level;
  489. if (last)
  490. break;
  491. i++;
  492. }
  493. not_coded:
  494. if (s->mb_intra && s->h263_aic) {
  495. ff_h263_pred_acdc(s, block, n);
  496. i = 63;
  497. }
  498. s->block_last_index[n] = i;
  499. return 0;
  500. }
  501. static int h263_skip_b_part(MpegEncContext *s, int cbp)
  502. {
  503. LOCAL_ALIGNED_16(int16_t, dblock, [64]);
  504. int i, mbi;
  505. /* we have to set s->mb_intra to zero to decode B-part of PB-frame correctly
  506. * but real value should be restored in order to be used later (in OBMC condition)
  507. */
  508. mbi = s->mb_intra;
  509. s->mb_intra = 0;
  510. for (i = 0; i < 6; i++) {
  511. if (h263_decode_block(s, dblock, i, cbp&32) < 0)
  512. return -1;
  513. cbp+=cbp;
  514. }
  515. s->mb_intra = mbi;
  516. return 0;
  517. }
  518. static int h263_get_modb(GetBitContext *gb, int pb_frame, int *cbpb)
  519. {
  520. int c, mv = 1;
  521. if (pb_frame < 3) { // h.263 Annex G and i263 PB-frame
  522. c = get_bits1(gb);
  523. if (pb_frame == 2 && c)
  524. mv = !get_bits1(gb);
  525. } else { // h.263 Annex M improved PB-frame
  526. mv = get_unary(gb, 0, 4) + 1;
  527. c = mv & 1;
  528. mv = !!(mv & 2);
  529. }
  530. if(c)
  531. *cbpb = get_bits(gb, 6);
  532. return mv;
  533. }
  534. int ff_h263_decode_mb(MpegEncContext *s,
  535. int16_t block[6][64])
  536. {
  537. int cbpc, cbpy, i, cbp, pred_x, pred_y, mx, my, dquant;
  538. int16_t *mot_val;
  539. const int xy= s->mb_x + s->mb_y * s->mb_stride;
  540. int cbpb = 0, pb_mv_count = 0;
  541. assert(!s->h263_pred);
  542. if (s->pict_type == AV_PICTURE_TYPE_P) {
  543. do{
  544. if (get_bits1(&s->gb)) {
  545. /* skip mb */
  546. s->mb_intra = 0;
  547. for(i=0;i<6;i++)
  548. s->block_last_index[i] = -1;
  549. s->mv_dir = MV_DIR_FORWARD;
  550. s->mv_type = MV_TYPE_16X16;
  551. s->current_picture.mb_type[xy] = MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
  552. s->mv[0][0][0] = 0;
  553. s->mv[0][0][1] = 0;
  554. s->mb_skipped = !(s->obmc | s->loop_filter);
  555. goto end;
  556. }
  557. cbpc = get_vlc2(&s->gb, ff_h263_inter_MCBPC_vlc.table, INTER_MCBPC_VLC_BITS, 2);
  558. if (cbpc < 0){
  559. av_log(s->avctx, AV_LOG_ERROR, "cbpc damaged at %d %d\n", s->mb_x, s->mb_y);
  560. return -1;
  561. }
  562. }while(cbpc == 20);
  563. s->bdsp.clear_blocks(s->block[0]);
  564. dquant = cbpc & 8;
  565. s->mb_intra = ((cbpc & 4) != 0);
  566. if (s->mb_intra) goto intra;
  567. if(s->pb_frame && get_bits1(&s->gb))
  568. pb_mv_count = h263_get_modb(&s->gb, s->pb_frame, &cbpb);
  569. cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
  570. if(s->alt_inter_vlc==0 || (cbpc & 3)!=3)
  571. cbpy ^= 0xF;
  572. cbp = (cbpc & 3) | (cbpy << 2);
  573. if (dquant) {
  574. h263_decode_dquant(s);
  575. }
  576. s->mv_dir = MV_DIR_FORWARD;
  577. if ((cbpc & 16) == 0) {
  578. s->current_picture.mb_type[xy] = MB_TYPE_16x16 | MB_TYPE_L0;
  579. /* 16x16 motion prediction */
  580. s->mv_type = MV_TYPE_16X16;
  581. ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
  582. if (s->umvplus)
  583. mx = h263p_decode_umotion(s, pred_x);
  584. else
  585. mx = ff_h263_decode_motion(s, pred_x, 1);
  586. if (mx >= 0xffff)
  587. return -1;
  588. if (s->umvplus)
  589. my = h263p_decode_umotion(s, pred_y);
  590. else
  591. my = ff_h263_decode_motion(s, pred_y, 1);
  592. if (my >= 0xffff)
  593. return -1;
  594. s->mv[0][0][0] = mx;
  595. s->mv[0][0][1] = my;
  596. if (s->umvplus && (mx - pred_x) == 1 && (my - pred_y) == 1)
  597. skip_bits1(&s->gb); /* Bit stuffing to prevent PSC */
  598. } else {
  599. s->current_picture.mb_type[xy] = MB_TYPE_8x8 | MB_TYPE_L0;
  600. s->mv_type = MV_TYPE_8X8;
  601. for(i=0;i<4;i++) {
  602. mot_val = ff_h263_pred_motion(s, i, 0, &pred_x, &pred_y);
  603. if (s->umvplus)
  604. mx = h263p_decode_umotion(s, pred_x);
  605. else
  606. mx = ff_h263_decode_motion(s, pred_x, 1);
  607. if (mx >= 0xffff)
  608. return -1;
  609. if (s->umvplus)
  610. my = h263p_decode_umotion(s, pred_y);
  611. else
  612. my = ff_h263_decode_motion(s, pred_y, 1);
  613. if (my >= 0xffff)
  614. return -1;
  615. s->mv[0][i][0] = mx;
  616. s->mv[0][i][1] = my;
  617. if (s->umvplus && (mx - pred_x) == 1 && (my - pred_y) == 1)
  618. skip_bits1(&s->gb); /* Bit stuffing to prevent PSC */
  619. mot_val[0] = mx;
  620. mot_val[1] = my;
  621. }
  622. }
  623. } else if(s->pict_type==AV_PICTURE_TYPE_B) {
  624. int mb_type;
  625. const int stride= s->b8_stride;
  626. int16_t *mot_val0 = s->current_picture.motion_val[0][2 * (s->mb_x + s->mb_y * stride)];
  627. int16_t *mot_val1 = s->current_picture.motion_val[1][2 * (s->mb_x + s->mb_y * stride)];
  628. // const int mv_xy= s->mb_x + 1 + s->mb_y * s->mb_stride;
  629. //FIXME ugly
  630. mot_val0[0 ]= mot_val0[2 ]= mot_val0[0+2*stride]= mot_val0[2+2*stride]=
  631. mot_val0[1 ]= mot_val0[3 ]= mot_val0[1+2*stride]= mot_val0[3+2*stride]=
  632. mot_val1[0 ]= mot_val1[2 ]= mot_val1[0+2*stride]= mot_val1[2+2*stride]=
  633. mot_val1[1 ]= mot_val1[3 ]= mot_val1[1+2*stride]= mot_val1[3+2*stride]= 0;
  634. do{
  635. mb_type= get_vlc2(&s->gb, h263_mbtype_b_vlc.table, H263_MBTYPE_B_VLC_BITS, 2);
  636. if (mb_type < 0){
  637. av_log(s->avctx, AV_LOG_ERROR, "b mb_type damaged at %d %d\n", s->mb_x, s->mb_y);
  638. return -1;
  639. }
  640. mb_type= h263_mb_type_b_map[ mb_type ];
  641. }while(!mb_type);
  642. s->mb_intra = IS_INTRA(mb_type);
  643. if(HAS_CBP(mb_type)){
  644. s->bdsp.clear_blocks(s->block[0]);
  645. cbpc = get_vlc2(&s->gb, cbpc_b_vlc.table, CBPC_B_VLC_BITS, 1);
  646. if(s->mb_intra){
  647. dquant = IS_QUANT(mb_type);
  648. goto intra;
  649. }
  650. cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
  651. if (cbpy < 0){
  652. av_log(s->avctx, AV_LOG_ERROR, "b cbpy damaged at %d %d\n", s->mb_x, s->mb_y);
  653. return -1;
  654. }
  655. if(s->alt_inter_vlc==0 || (cbpc & 3)!=3)
  656. cbpy ^= 0xF;
  657. cbp = (cbpc & 3) | (cbpy << 2);
  658. }else
  659. cbp=0;
  660. assert(!s->mb_intra);
  661. if(IS_QUANT(mb_type)){
  662. h263_decode_dquant(s);
  663. }
  664. if(IS_DIRECT(mb_type)){
  665. if (!s->pp_time)
  666. return AVERROR_INVALIDDATA;
  667. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
  668. mb_type |= ff_mpeg4_set_direct_mv(s, 0, 0);
  669. }else{
  670. s->mv_dir = 0;
  671. s->mv_type= MV_TYPE_16X16;
  672. //FIXME UMV
  673. if(USES_LIST(mb_type, 0)){
  674. int16_t *mot_val= ff_h263_pred_motion(s, 0, 0, &mx, &my);
  675. s->mv_dir = MV_DIR_FORWARD;
  676. mx = ff_h263_decode_motion(s, mx, 1);
  677. my = ff_h263_decode_motion(s, my, 1);
  678. s->mv[0][0][0] = mx;
  679. s->mv[0][0][1] = my;
  680. mot_val[0 ]= mot_val[2 ]= mot_val[0+2*stride]= mot_val[2+2*stride]= mx;
  681. mot_val[1 ]= mot_val[3 ]= mot_val[1+2*stride]= mot_val[3+2*stride]= my;
  682. }
  683. if(USES_LIST(mb_type, 1)){
  684. int16_t *mot_val= ff_h263_pred_motion(s, 0, 1, &mx, &my);
  685. s->mv_dir |= MV_DIR_BACKWARD;
  686. mx = ff_h263_decode_motion(s, mx, 1);
  687. my = ff_h263_decode_motion(s, my, 1);
  688. s->mv[1][0][0] = mx;
  689. s->mv[1][0][1] = my;
  690. mot_val[0 ]= mot_val[2 ]= mot_val[0+2*stride]= mot_val[2+2*stride]= mx;
  691. mot_val[1 ]= mot_val[3 ]= mot_val[1+2*stride]= mot_val[3+2*stride]= my;
  692. }
  693. }
  694. s->current_picture.mb_type[xy] = mb_type;
  695. } else { /* I-Frame */
  696. do{
  697. cbpc = get_vlc2(&s->gb, ff_h263_intra_MCBPC_vlc.table, INTRA_MCBPC_VLC_BITS, 2);
  698. if (cbpc < 0){
  699. av_log(s->avctx, AV_LOG_ERROR, "I cbpc damaged at %d %d\n", s->mb_x, s->mb_y);
  700. return -1;
  701. }
  702. }while(cbpc == 8);
  703. s->bdsp.clear_blocks(s->block[0]);
  704. dquant = cbpc & 4;
  705. s->mb_intra = 1;
  706. intra:
  707. s->current_picture.mb_type[xy] = MB_TYPE_INTRA;
  708. if (s->h263_aic) {
  709. s->ac_pred = get_bits1(&s->gb);
  710. if(s->ac_pred){
  711. s->current_picture.mb_type[xy] = MB_TYPE_INTRA | MB_TYPE_ACPRED;
  712. s->h263_aic_dir = get_bits1(&s->gb);
  713. }
  714. }else
  715. s->ac_pred = 0;
  716. if(s->pb_frame && get_bits1(&s->gb))
  717. pb_mv_count = h263_get_modb(&s->gb, s->pb_frame, &cbpb);
  718. cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
  719. if(cbpy<0){
  720. av_log(s->avctx, AV_LOG_ERROR, "I cbpy damaged at %d %d\n", s->mb_x, s->mb_y);
  721. return -1;
  722. }
  723. cbp = (cbpc & 3) | (cbpy << 2);
  724. if (dquant) {
  725. h263_decode_dquant(s);
  726. }
  727. pb_mv_count += !!s->pb_frame;
  728. }
  729. while(pb_mv_count--){
  730. ff_h263_decode_motion(s, 0, 1);
  731. ff_h263_decode_motion(s, 0, 1);
  732. }
  733. /* decode each block */
  734. for (i = 0; i < 6; i++) {
  735. if (h263_decode_block(s, block[i], i, cbp&32) < 0)
  736. return -1;
  737. cbp+=cbp;
  738. }
  739. if(s->pb_frame && h263_skip_b_part(s, cbpb) < 0)
  740. return -1;
  741. if(s->obmc && !s->mb_intra){
  742. if(s->pict_type == AV_PICTURE_TYPE_P && s->mb_x+1<s->mb_width && s->mb_num_left != 1)
  743. preview_obmc(s);
  744. }
  745. end:
  746. /* per-MB end of slice check */
  747. {
  748. int v= show_bits(&s->gb, 16);
  749. if (get_bits_left(&s->gb) < 16) {
  750. v >>= 16 - get_bits_left(&s->gb);
  751. }
  752. if(v==0)
  753. return SLICE_END;
  754. }
  755. return SLICE_OK;
  756. }
  757. /* Most is hardcoded; should extend to handle all H.263 streams. */
  758. int ff_h263_decode_picture_header(MpegEncContext *s)
  759. {
  760. int format, width, height, i, ret;
  761. uint32_t startcode;
  762. align_get_bits(&s->gb);
  763. startcode= get_bits(&s->gb, 22-8);
  764. for(i= get_bits_left(&s->gb); i>24; i-=8) {
  765. startcode = ((startcode << 8) | get_bits(&s->gb, 8)) & 0x003FFFFF;
  766. if(startcode == 0x20)
  767. break;
  768. }
  769. if (startcode != 0x20) {
  770. av_log(s->avctx, AV_LOG_ERROR, "Bad picture start code\n");
  771. return -1;
  772. }
  773. /* temporal reference */
  774. i = get_bits(&s->gb, 8); /* picture timestamp */
  775. if( (s->picture_number&~0xFF)+i < s->picture_number)
  776. i+= 256;
  777. s->picture_number= (s->picture_number&~0xFF) + i;
  778. /* PTYPE starts here */
  779. if (get_bits1(&s->gb) != 1) {
  780. /* marker */
  781. av_log(s->avctx, AV_LOG_ERROR, "Bad marker\n");
  782. return -1;
  783. }
  784. if (get_bits1(&s->gb) != 0) {
  785. av_log(s->avctx, AV_LOG_ERROR, "Bad H.263 id\n");
  786. return -1; /* H.263 id */
  787. }
  788. skip_bits1(&s->gb); /* split screen off */
  789. skip_bits1(&s->gb); /* camera off */
  790. skip_bits1(&s->gb); /* freeze picture release off */
  791. format = get_bits(&s->gb, 3);
  792. /*
  793. 0 forbidden
  794. 1 sub-QCIF
  795. 10 QCIF
  796. 7 extended PTYPE (PLUSPTYPE)
  797. */
  798. if (format != 7 && format != 6) {
  799. s->h263_plus = 0;
  800. /* H.263v1 */
  801. width = ff_h263_format[format][0];
  802. height = ff_h263_format[format][1];
  803. s->pict_type = AV_PICTURE_TYPE_I + get_bits1(&s->gb);
  804. s->h263_long_vectors = get_bits1(&s->gb);
  805. if (get_bits1(&s->gb) != 0) {
  806. av_log(s->avctx, AV_LOG_ERROR, "H.263 SAC not supported\n");
  807. return -1; /* SAC: off */
  808. }
  809. s->obmc= get_bits1(&s->gb); /* Advanced prediction mode */
  810. s->unrestricted_mv = s->h263_long_vectors || s->obmc;
  811. s->pb_frame = get_bits1(&s->gb);
  812. s->chroma_qscale= s->qscale = get_bits(&s->gb, 5);
  813. skip_bits1(&s->gb); /* Continuous Presence Multipoint mode: off */
  814. s->width = width;
  815. s->height = height;
  816. s->avctx->sample_aspect_ratio= (AVRational){12,11};
  817. s->avctx->framerate = (AVRational){ 30000, 1001 };
  818. } else {
  819. int ufep;
  820. /* H.263v2 */
  821. s->h263_plus = 1;
  822. ufep = get_bits(&s->gb, 3); /* Update Full Extended PTYPE */
  823. /* ufep other than 0 and 1 are reserved */
  824. if (ufep == 1) {
  825. /* OPPTYPE */
  826. format = get_bits(&s->gb, 3);
  827. ff_dlog(s->avctx, "ufep=1, format: %d\n", format);
  828. s->custom_pcf= get_bits1(&s->gb);
  829. s->umvplus = get_bits1(&s->gb); /* Unrestricted Motion Vector */
  830. if (get_bits1(&s->gb) != 0) {
  831. av_log(s->avctx, AV_LOG_ERROR, "Syntax-based Arithmetic Coding (SAC) not supported\n");
  832. }
  833. s->obmc= get_bits1(&s->gb); /* Advanced prediction mode */
  834. s->h263_aic = get_bits1(&s->gb); /* Advanced Intra Coding (AIC) */
  835. s->loop_filter= get_bits1(&s->gb);
  836. s->unrestricted_mv = s->umvplus || s->obmc || s->loop_filter;
  837. s->h263_slice_structured= get_bits1(&s->gb);
  838. if (get_bits1(&s->gb) != 0) {
  839. av_log(s->avctx, AV_LOG_ERROR, "Reference Picture Selection not supported\n");
  840. }
  841. if (get_bits1(&s->gb) != 0) {
  842. av_log(s->avctx, AV_LOG_ERROR, "Independent Segment Decoding not supported\n");
  843. }
  844. s->alt_inter_vlc= get_bits1(&s->gb);
  845. s->modified_quant= get_bits1(&s->gb);
  846. if(s->modified_quant)
  847. s->chroma_qscale_table= ff_h263_chroma_qscale_table;
  848. skip_bits(&s->gb, 1); /* Prevent start code emulation */
  849. skip_bits(&s->gb, 3); /* Reserved */
  850. } else if (ufep != 0) {
  851. av_log(s->avctx, AV_LOG_ERROR, "Bad UFEP type (%d)\n", ufep);
  852. return -1;
  853. }
  854. /* MPPTYPE */
  855. s->pict_type = get_bits(&s->gb, 3);
  856. switch(s->pict_type){
  857. case 0: s->pict_type= AV_PICTURE_TYPE_I;break;
  858. case 1: s->pict_type= AV_PICTURE_TYPE_P;break;
  859. case 2: s->pict_type= AV_PICTURE_TYPE_P;s->pb_frame = 3;break;
  860. case 3: s->pict_type= AV_PICTURE_TYPE_B;break;
  861. case 7: s->pict_type= AV_PICTURE_TYPE_I;break; //ZYGO
  862. default:
  863. return -1;
  864. }
  865. skip_bits(&s->gb, 2);
  866. s->no_rounding = get_bits1(&s->gb);
  867. skip_bits(&s->gb, 4);
  868. /* Get the picture dimensions */
  869. if (ufep) {
  870. if (format == 6) {
  871. /* Custom Picture Format (CPFMT) */
  872. s->aspect_ratio_info = get_bits(&s->gb, 4);
  873. ff_dlog(s->avctx, "aspect: %d\n", s->aspect_ratio_info);
  874. /* aspect ratios:
  875. 0 - forbidden
  876. 1 - 1:1
  877. 2 - 12:11 (CIF 4:3)
  878. 3 - 10:11 (525-type 4:3)
  879. 4 - 16:11 (CIF 16:9)
  880. 5 - 40:33 (525-type 16:9)
  881. 6-14 - reserved
  882. */
  883. width = (get_bits(&s->gb, 9) + 1) * 4;
  884. skip_bits1(&s->gb);
  885. height = get_bits(&s->gb, 9) * 4;
  886. ff_dlog(s->avctx, "\nH.263+ Custom picture: %dx%d\n",width,height);
  887. if (s->aspect_ratio_info == FF_ASPECT_EXTENDED) {
  888. /* expected dimensions */
  889. s->avctx->sample_aspect_ratio.num= get_bits(&s->gb, 8);
  890. s->avctx->sample_aspect_ratio.den= get_bits(&s->gb, 8);
  891. }else{
  892. s->avctx->sample_aspect_ratio= ff_h263_pixel_aspect[s->aspect_ratio_info];
  893. }
  894. } else {
  895. width = ff_h263_format[format][0];
  896. height = ff_h263_format[format][1];
  897. s->avctx->sample_aspect_ratio= (AVRational){12,11};
  898. }
  899. if ((width == 0) || (height == 0))
  900. return -1;
  901. s->width = width;
  902. s->height = height;
  903. if(s->custom_pcf){
  904. int gcd;
  905. s->avctx->framerate.num = 1800000;
  906. s->avctx->framerate.den = 1000 + get_bits1(&s->gb);
  907. s->avctx->framerate.den *= get_bits(&s->gb, 7);
  908. if(s->avctx->framerate.den == 0){
  909. av_log(s, AV_LOG_ERROR, "zero framerate\n");
  910. return -1;
  911. }
  912. gcd= av_gcd(s->avctx->framerate.den, s->avctx->framerate.num);
  913. s->avctx->framerate.den /= gcd;
  914. s->avctx->framerate.num /= gcd;
  915. }else{
  916. s->avctx->framerate = (AVRational){ 30000, 1001 };
  917. }
  918. }
  919. if(s->custom_pcf){
  920. skip_bits(&s->gb, 2); //extended Temporal reference
  921. }
  922. if (ufep) {
  923. if (s->umvplus) {
  924. if(get_bits1(&s->gb)==0) /* Unlimited Unrestricted Motion Vectors Indicator (UUI) */
  925. skip_bits1(&s->gb);
  926. }
  927. if(s->h263_slice_structured){
  928. if (get_bits1(&s->gb) != 0) {
  929. av_log(s->avctx, AV_LOG_ERROR, "rectangular slices not supported\n");
  930. }
  931. if (get_bits1(&s->gb) != 0) {
  932. av_log(s->avctx, AV_LOG_ERROR, "unordered slices not supported\n");
  933. }
  934. }
  935. }
  936. s->qscale = get_bits(&s->gb, 5);
  937. }
  938. if ((ret = av_image_check_size(s->width, s->height, 0, s)) < 0)
  939. return ret;
  940. s->mb_width = (s->width + 15) / 16;
  941. s->mb_height = (s->height + 15) / 16;
  942. s->mb_num = s->mb_width * s->mb_height;
  943. if (s->pb_frame) {
  944. skip_bits(&s->gb, 3); /* Temporal reference for B-pictures */
  945. if (s->custom_pcf)
  946. skip_bits(&s->gb, 2); //extended Temporal reference
  947. skip_bits(&s->gb, 2); /* Quantization information for B-pictures */
  948. }
  949. if (s->pict_type!=AV_PICTURE_TYPE_B) {
  950. s->time = s->picture_number;
  951. s->pp_time = s->time - s->last_non_b_time;
  952. s->last_non_b_time = s->time;
  953. }else{
  954. s->time = s->picture_number;
  955. s->pb_time = s->pp_time - (s->last_non_b_time - s->time);
  956. if (s->pp_time <=s->pb_time ||
  957. s->pp_time <= s->pp_time - s->pb_time ||
  958. s->pp_time <= 0){
  959. s->pp_time = 2;
  960. s->pb_time = 1;
  961. }
  962. ff_mpeg4_init_direct_mv(s);
  963. }
  964. /* PEI */
  965. while (get_bits1(&s->gb) != 0) {
  966. skip_bits(&s->gb, 8);
  967. }
  968. if(s->h263_slice_structured){
  969. if (get_bits1(&s->gb) != 1) {
  970. av_log(s->avctx, AV_LOG_ERROR, "SEPB1 marker missing\n");
  971. return -1;
  972. }
  973. ff_h263_decode_mba(s);
  974. if (get_bits1(&s->gb) != 1) {
  975. av_log(s->avctx, AV_LOG_ERROR, "SEPB2 marker missing\n");
  976. return -1;
  977. }
  978. }
  979. s->f_code = 1;
  980. if(s->h263_aic){
  981. s->y_dc_scale_table=
  982. s->c_dc_scale_table= ff_aic_dc_scale_table;
  983. }else{
  984. s->y_dc_scale_table=
  985. s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
  986. }
  987. ff_h263_show_pict_info(s);
  988. if (s->pict_type == AV_PICTURE_TYPE_I && s->codec_tag == AV_RL32("ZYGO")){
  989. int i,j;
  990. for(i=0; i<85; i++) av_log(s->avctx, AV_LOG_DEBUG, "%d", get_bits1(&s->gb));
  991. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  992. for(i=0; i<13; i++){
  993. for(j=0; j<3; j++){
  994. int v= get_bits(&s->gb, 8);
  995. v |= get_sbits(&s->gb, 8)<<8;
  996. av_log(s->avctx, AV_LOG_DEBUG, " %5d", v);
  997. }
  998. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  999. }
  1000. for(i=0; i<50; i++) av_log(s->avctx, AV_LOG_DEBUG, "%d", get_bits1(&s->gb));
  1001. }
  1002. return 0;
  1003. }