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.

1148 lines
36KB

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