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