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.

1152 lines
37KB

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