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.

1149 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. // 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(const uint8_t *av_restrict p, const uint8_t *av_restrict end)
  184. {
  185. av_assert2(p < end);
  186. end-=2;
  187. p++;
  188. for(;p<end; p+=2){
  189. if(!*p){
  190. if (!p[-1] && p[1]) return p - 1;
  191. else if(!p[ 1] && p[2]) return p;
  192. }
  193. }
  194. return end+2;
  195. }
  196. /**
  197. * Decode the group of blocks / video packet header.
  198. * @return bit position of the resync_marker, or <0 if none was found
  199. */
  200. int ff_h263_resync(MpegEncContext *s){
  201. int left, pos, ret;
  202. if(s->codec_id==AV_CODEC_ID_MPEG4){
  203. skip_bits1(&s->gb);
  204. align_get_bits(&s->gb);
  205. }
  206. if(show_bits(&s->gb, 16)==0){
  207. pos= get_bits_count(&s->gb);
  208. if(CONFIG_MPEG4_DECODER && s->codec_id==AV_CODEC_ID_MPEG4)
  209. ret= ff_mpeg4_decode_video_packet_header(s);
  210. else
  211. ret= h263_decode_gob_header(s);
  212. if(ret>=0)
  213. return pos;
  214. }
  215. //OK, it's not where it is supposed to be ...
  216. s->gb= s->last_resync_gb;
  217. align_get_bits(&s->gb);
  218. left= get_bits_left(&s->gb);
  219. for(;left>16+1+5+5; left-=8){
  220. if(show_bits(&s->gb, 16)==0){
  221. GetBitContext bak= s->gb;
  222. pos= get_bits_count(&s->gb);
  223. if(CONFIG_MPEG4_DECODER && s->codec_id==AV_CODEC_ID_MPEG4)
  224. ret= ff_mpeg4_decode_video_packet_header(s);
  225. else
  226. ret= h263_decode_gob_header(s);
  227. if(ret>=0)
  228. return pos;
  229. s->gb= bak;
  230. }
  231. skip_bits(&s->gb, 8);
  232. }
  233. return -1;
  234. }
  235. int ff_h263_decode_motion(MpegEncContext * s, int pred, int f_code)
  236. {
  237. int code, val, sign, shift;
  238. code = get_vlc2(&s->gb, mv_vlc.table, MV_VLC_BITS, 2);
  239. if (code == 0)
  240. return pred;
  241. if (code < 0)
  242. return 0xffff;
  243. sign = get_bits1(&s->gb);
  244. shift = f_code - 1;
  245. val = code;
  246. if (shift) {
  247. val = (val - 1) << shift;
  248. val |= get_bits(&s->gb, shift);
  249. val++;
  250. }
  251. if (sign)
  252. val = -val;
  253. val += pred;
  254. /* modulo decoding */
  255. if (!s->h263_long_vectors) {
  256. val = sign_extend(val, 5 + f_code);
  257. } else {
  258. /* horrible h263 long vector mode */
  259. if (pred < -31 && val < -63)
  260. val += 64;
  261. if (pred > 32 && val > 63)
  262. val -= 64;
  263. }
  264. return val;
  265. }
  266. /* Decode RVLC of H.263+ UMV */
  267. static int h263p_decode_umotion(MpegEncContext * s, int pred)
  268. {
  269. int code = 0, sign;
  270. if (get_bits1(&s->gb)) /* Motion difference = 0 */
  271. return pred;
  272. code = 2 + get_bits1(&s->gb);
  273. while (get_bits1(&s->gb))
  274. {
  275. code <<= 1;
  276. code += get_bits1(&s->gb);
  277. }
  278. sign = code & 1;
  279. code >>= 1;
  280. code = (sign) ? (pred - code) : (pred + code);
  281. av_dlog(s->avctx,"H.263+ UMV Motion = %d\n", code);
  282. return code;
  283. }
  284. /**
  285. * read the next MVs for OBMC. yes this is a ugly hack, feel free to send a patch :)
  286. */
  287. static void preview_obmc(MpegEncContext *s){
  288. GetBitContext gb= s->gb;
  289. int cbpc, i, pred_x, pred_y, mx, my;
  290. int16_t *mot_val;
  291. const int xy= s->mb_x + 1 + s->mb_y * s->mb_stride;
  292. const int stride= s->b8_stride*2;
  293. for(i=0; i<4; i++)
  294. s->block_index[i]+= 2;
  295. for(i=4; i<6; i++)
  296. s->block_index[i]+= 1;
  297. s->mb_x++;
  298. av_assert2(s->pict_type == AV_PICTURE_TYPE_P);
  299. do{
  300. if (get_bits1(&s->gb)) {
  301. /* skip mb */
  302. mot_val = s->current_picture.f.motion_val[0][s->block_index[0]];
  303. mot_val[0 ]= mot_val[2 ]=
  304. mot_val[0+stride]= mot_val[2+stride]= 0;
  305. mot_val[1 ]= mot_val[3 ]=
  306. mot_val[1+stride]= mot_val[3+stride]= 0;
  307. s->current_picture.f.mb_type[xy] = MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
  308. goto end;
  309. }
  310. cbpc = get_vlc2(&s->gb, ff_h263_inter_MCBPC_vlc.table, INTER_MCBPC_VLC_BITS, 2);
  311. }while(cbpc == 20);
  312. if(cbpc & 4){
  313. s->current_picture.f.mb_type[xy] = MB_TYPE_INTRA;
  314. }else{
  315. get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
  316. if (cbpc & 8) {
  317. if(s->modified_quant){
  318. if(get_bits1(&s->gb)) skip_bits(&s->gb, 1);
  319. else skip_bits(&s->gb, 5);
  320. }else
  321. skip_bits(&s->gb, 2);
  322. }
  323. if ((cbpc & 16) == 0) {
  324. s->current_picture.f.mb_type[xy] = MB_TYPE_16x16 | MB_TYPE_L0;
  325. /* 16x16 motion prediction */
  326. mot_val= ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
  327. if (s->umvplus)
  328. mx = h263p_decode_umotion(s, pred_x);
  329. else
  330. mx = ff_h263_decode_motion(s, pred_x, 1);
  331. if (s->umvplus)
  332. my = h263p_decode_umotion(s, pred_y);
  333. else
  334. my = ff_h263_decode_motion(s, pred_y, 1);
  335. mot_val[0 ]= mot_val[2 ]=
  336. mot_val[0+stride]= mot_val[2+stride]= mx;
  337. mot_val[1 ]= mot_val[3 ]=
  338. mot_val[1+stride]= mot_val[3+stride]= my;
  339. } else {
  340. s->current_picture.f.mb_type[xy] = MB_TYPE_8x8 | MB_TYPE_L0;
  341. for(i=0;i<4;i++) {
  342. mot_val = ff_h263_pred_motion(s, i, 0, &pred_x, &pred_y);
  343. if (s->umvplus)
  344. mx = h263p_decode_umotion(s, pred_x);
  345. else
  346. mx = ff_h263_decode_motion(s, pred_x, 1);
  347. if (s->umvplus)
  348. my = h263p_decode_umotion(s, pred_y);
  349. else
  350. my = ff_h263_decode_motion(s, pred_y, 1);
  351. if (s->umvplus && (mx - pred_x) == 1 && (my - pred_y) == 1)
  352. skip_bits1(&s->gb); /* Bit stuffing to prevent PSC */
  353. mot_val[0] = mx;
  354. mot_val[1] = my;
  355. }
  356. }
  357. }
  358. end:
  359. for(i=0; i<4; i++)
  360. s->block_index[i]-= 2;
  361. for(i=4; i<6; i++)
  362. s->block_index[i]-= 1;
  363. s->mb_x--;
  364. s->gb= gb;
  365. }
  366. static void h263_decode_dquant(MpegEncContext *s){
  367. static const int8_t quant_tab[4] = { -1, -2, 1, 2 };
  368. if(s->modified_quant){
  369. if(get_bits1(&s->gb))
  370. s->qscale= ff_modified_quant_tab[get_bits1(&s->gb)][ s->qscale ];
  371. else
  372. s->qscale= get_bits(&s->gb, 5);
  373. }else
  374. s->qscale += quant_tab[get_bits(&s->gb, 2)];
  375. ff_set_qscale(s, s->qscale);
  376. }
  377. static int h263_decode_block(MpegEncContext * s, DCTELEM * block,
  378. int n, int coded)
  379. {
  380. int code, level, i, j, last, run;
  381. RLTable *rl = &ff_h263_rl_inter;
  382. const uint8_t *scan_table;
  383. GetBitContext gb= s->gb;
  384. scan_table = s->intra_scantable.permutated;
  385. if (s->h263_aic && s->mb_intra) {
  386. rl = &ff_rl_intra_aic;
  387. i = 0;
  388. if (s->ac_pred) {
  389. if (s->h263_aic_dir)
  390. scan_table = s->intra_v_scantable.permutated; /* left */
  391. else
  392. scan_table = s->intra_h_scantable.permutated; /* top */
  393. }
  394. } else if (s->mb_intra) {
  395. /* DC coef */
  396. if(s->codec_id == AV_CODEC_ID_RV10){
  397. #if CONFIG_RV10_DECODER
  398. if (s->rv10_version == 3 && s->pict_type == AV_PICTURE_TYPE_I) {
  399. int component, diff;
  400. component = (n <= 3 ? 0 : n - 4 + 1);
  401. level = s->last_dc[component];
  402. if (s->rv10_first_dc_coded[component]) {
  403. diff = ff_rv_decode_dc(s, n);
  404. if (diff == 0xffff)
  405. return -1;
  406. level += diff;
  407. level = level & 0xff; /* handle wrap round */
  408. s->last_dc[component] = level;
  409. } else {
  410. s->rv10_first_dc_coded[component] = 1;
  411. }
  412. } else {
  413. level = get_bits(&s->gb, 8);
  414. if (level == 255)
  415. level = 128;
  416. }
  417. #endif
  418. }else{
  419. level = get_bits(&s->gb, 8);
  420. if((level&0x7F) == 0){
  421. av_log(s->avctx, AV_LOG_ERROR, "illegal dc %d at %d %d\n", level, s->mb_x, s->mb_y);
  422. if(s->err_recognition & (AV_EF_BITSTREAM|AV_EF_COMPLIANT))
  423. return -1;
  424. }
  425. if (level == 255)
  426. level = 128;
  427. }
  428. block[0] = level;
  429. i = 1;
  430. } else {
  431. i = 0;
  432. }
  433. if (!coded) {
  434. if (s->mb_intra && s->h263_aic)
  435. goto not_coded;
  436. s->block_last_index[n] = i - 1;
  437. return 0;
  438. }
  439. retry:
  440. for(;;) {
  441. code = get_vlc2(&s->gb, rl->vlc.table, TEX_VLC_BITS, 2);
  442. if (code < 0){
  443. av_log(s->avctx, AV_LOG_ERROR, "illegal ac vlc code at %dx%d\n", s->mb_x, s->mb_y);
  444. return -1;
  445. }
  446. if (code == rl->n) {
  447. /* escape */
  448. if (CONFIG_FLV_DECODER && s->h263_flv > 1) {
  449. ff_flv2_decode_ac_esc(&s->gb, &level, &run, &last);
  450. } else {
  451. last = get_bits1(&s->gb);
  452. run = get_bits(&s->gb, 6);
  453. level = (int8_t)get_bits(&s->gb, 8);
  454. if(level == -128){
  455. if (s->codec_id == AV_CODEC_ID_RV10) {
  456. /* XXX: should patch encoder too */
  457. level = get_sbits(&s->gb, 12);
  458. }else{
  459. level = get_bits(&s->gb, 5);
  460. level |= get_sbits(&s->gb, 6)<<5;
  461. }
  462. }
  463. }
  464. } else {
  465. run = rl->table_run[code];
  466. level = rl->table_level[code];
  467. last = code >= rl->last;
  468. if (get_bits1(&s->gb))
  469. level = -level;
  470. }
  471. i += run;
  472. if (i >= 64){
  473. if(s->alt_inter_vlc && rl == &ff_h263_rl_inter && !s->mb_intra){
  474. //Looks like a hack but no, it's the way it is supposed to work ...
  475. rl = &ff_rl_intra_aic;
  476. i = 0;
  477. s->gb= gb;
  478. s->dsp.clear_block(block);
  479. goto retry;
  480. }
  481. av_log(s->avctx, AV_LOG_ERROR, "run overflow at %dx%d i:%d\n", s->mb_x, s->mb_y, s->mb_intra);
  482. return -1;
  483. }
  484. j = scan_table[i];
  485. block[j] = level;
  486. if (last)
  487. break;
  488. i++;
  489. }
  490. not_coded:
  491. if (s->mb_intra && s->h263_aic) {
  492. ff_h263_pred_acdc(s, block, n);
  493. i = 63;
  494. }
  495. s->block_last_index[n] = i;
  496. return 0;
  497. }
  498. static int h263_skip_b_part(MpegEncContext *s, int cbp)
  499. {
  500. LOCAL_ALIGNED_16(DCTELEM, dblock, [64]);
  501. int i, mbi;
  502. /* we have to set s->mb_intra to zero to decode B-part of PB-frame correctly
  503. * but real value should be restored in order to be used later (in OBMC condition)
  504. */
  505. mbi = s->mb_intra;
  506. s->mb_intra = 0;
  507. for (i = 0; i < 6; i++) {
  508. if (h263_decode_block(s, dblock, i, cbp&32) < 0)
  509. return -1;
  510. cbp+=cbp;
  511. }
  512. s->mb_intra = mbi;
  513. return 0;
  514. }
  515. static int h263_get_modb(GetBitContext *gb, int pb_frame, int *cbpb)
  516. {
  517. int c, mv = 1;
  518. if (pb_frame < 3) { // h.263 Annex G and i263 PB-frame
  519. c = get_bits1(gb);
  520. if (pb_frame == 2 && c)
  521. mv = !get_bits1(gb);
  522. } else { // h.263 Annex M improved PB-frame
  523. mv = get_unary(gb, 0, 4) + 1;
  524. c = mv & 1;
  525. mv = !!(mv & 2);
  526. }
  527. if(c)
  528. *cbpb = get_bits(gb, 6);
  529. return mv;
  530. }
  531. int ff_h263_decode_mb(MpegEncContext *s,
  532. DCTELEM block[6][64])
  533. {
  534. int cbpc, cbpy, i, cbp, pred_x, pred_y, mx, my, dquant;
  535. int16_t *mot_val;
  536. const int xy= s->mb_x + s->mb_y * s->mb_stride;
  537. int cbpb = 0, pb_mv_count = 0;
  538. av_assert2(!s->h263_pred);
  539. if (s->pict_type == AV_PICTURE_TYPE_P) {
  540. do{
  541. if (get_bits1(&s->gb)) {
  542. /* skip mb */
  543. s->mb_intra = 0;
  544. for(i=0;i<6;i++)
  545. s->block_last_index[i] = -1;
  546. s->mv_dir = MV_DIR_FORWARD;
  547. s->mv_type = MV_TYPE_16X16;
  548. s->current_picture.f.mb_type[xy] = MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
  549. s->mv[0][0][0] = 0;
  550. s->mv[0][0][1] = 0;
  551. s->mb_skipped = !(s->obmc | s->loop_filter);
  552. goto end;
  553. }
  554. cbpc = get_vlc2(&s->gb, ff_h263_inter_MCBPC_vlc.table, INTER_MCBPC_VLC_BITS, 2);
  555. if (cbpc < 0){
  556. av_log(s->avctx, AV_LOG_ERROR, "cbpc damaged at %d %d\n", s->mb_x, s->mb_y);
  557. return -1;
  558. }
  559. }while(cbpc == 20);
  560. s->dsp.clear_blocks(s->block[0]);
  561. dquant = cbpc & 8;
  562. s->mb_intra = ((cbpc & 4) != 0);
  563. if (s->mb_intra) goto intra;
  564. if(s->pb_frame && get_bits1(&s->gb))
  565. pb_mv_count = h263_get_modb(&s->gb, s->pb_frame, &cbpb);
  566. cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
  567. if(s->alt_inter_vlc==0 || (cbpc & 3)!=3)
  568. cbpy ^= 0xF;
  569. cbp = (cbpc & 3) | (cbpy << 2);
  570. if (dquant) {
  571. h263_decode_dquant(s);
  572. }
  573. s->mv_dir = MV_DIR_FORWARD;
  574. if ((cbpc & 16) == 0) {
  575. s->current_picture.f.mb_type[xy] = MB_TYPE_16x16 | MB_TYPE_L0;
  576. /* 16x16 motion prediction */
  577. s->mv_type = MV_TYPE_16X16;
  578. ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
  579. if (s->umvplus)
  580. mx = h263p_decode_umotion(s, pred_x);
  581. else
  582. mx = ff_h263_decode_motion(s, pred_x, 1);
  583. if (mx >= 0xffff)
  584. return -1;
  585. if (s->umvplus)
  586. my = h263p_decode_umotion(s, pred_y);
  587. else
  588. my = ff_h263_decode_motion(s, pred_y, 1);
  589. if (my >= 0xffff)
  590. return -1;
  591. s->mv[0][0][0] = mx;
  592. s->mv[0][0][1] = my;
  593. if (s->umvplus && (mx - pred_x) == 1 && (my - pred_y) == 1)
  594. skip_bits1(&s->gb); /* Bit stuffing to prevent PSC */
  595. } else {
  596. s->current_picture.f.mb_type[xy] = MB_TYPE_8x8 | MB_TYPE_L0;
  597. s->mv_type = MV_TYPE_8X8;
  598. for(i=0;i<4;i++) {
  599. mot_val = ff_h263_pred_motion(s, i, 0, &pred_x, &pred_y);
  600. if (s->umvplus)
  601. mx = h263p_decode_umotion(s, pred_x);
  602. else
  603. mx = ff_h263_decode_motion(s, pred_x, 1);
  604. if (mx >= 0xffff)
  605. return -1;
  606. if (s->umvplus)
  607. my = h263p_decode_umotion(s, pred_y);
  608. else
  609. my = ff_h263_decode_motion(s, pred_y, 1);
  610. if (my >= 0xffff)
  611. return -1;
  612. s->mv[0][i][0] = mx;
  613. s->mv[0][i][1] = my;
  614. if (s->umvplus && (mx - pred_x) == 1 && (my - pred_y) == 1)
  615. skip_bits1(&s->gb); /* Bit stuffing to prevent PSC */
  616. mot_val[0] = mx;
  617. mot_val[1] = my;
  618. }
  619. }
  620. } else if(s->pict_type==AV_PICTURE_TYPE_B) {
  621. int mb_type;
  622. const int stride= s->b8_stride;
  623. int16_t *mot_val0 = s->current_picture.f.motion_val[0][2 * (s->mb_x + s->mb_y * stride)];
  624. int16_t *mot_val1 = s->current_picture.f.motion_val[1][2 * (s->mb_x + s->mb_y * stride)];
  625. // const int mv_xy= s->mb_x + 1 + s->mb_y * s->mb_stride;
  626. //FIXME ugly
  627. mot_val0[0 ]= mot_val0[2 ]= mot_val0[0+2*stride]= mot_val0[2+2*stride]=
  628. mot_val0[1 ]= mot_val0[3 ]= mot_val0[1+2*stride]= mot_val0[3+2*stride]=
  629. mot_val1[0 ]= mot_val1[2 ]= mot_val1[0+2*stride]= mot_val1[2+2*stride]=
  630. mot_val1[1 ]= mot_val1[3 ]= mot_val1[1+2*stride]= mot_val1[3+2*stride]= 0;
  631. do{
  632. mb_type= get_vlc2(&s->gb, h263_mbtype_b_vlc.table, H263_MBTYPE_B_VLC_BITS, 2);
  633. if (mb_type < 0){
  634. av_log(s->avctx, AV_LOG_ERROR, "b mb_type damaged at %d %d\n", s->mb_x, s->mb_y);
  635. return -1;
  636. }
  637. mb_type= h263_mb_type_b_map[ mb_type ];
  638. }while(!mb_type);
  639. s->mb_intra = IS_INTRA(mb_type);
  640. if(HAS_CBP(mb_type)){
  641. s->dsp.clear_blocks(s->block[0]);
  642. cbpc = get_vlc2(&s->gb, cbpc_b_vlc.table, CBPC_B_VLC_BITS, 1);
  643. if(s->mb_intra){
  644. dquant = IS_QUANT(mb_type);
  645. goto intra;
  646. }
  647. cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
  648. if (cbpy < 0){
  649. av_log(s->avctx, AV_LOG_ERROR, "b cbpy damaged at %d %d\n", s->mb_x, s->mb_y);
  650. return -1;
  651. }
  652. if(s->alt_inter_vlc==0 || (cbpc & 3)!=3)
  653. cbpy ^= 0xF;
  654. cbp = (cbpc & 3) | (cbpy << 2);
  655. }else
  656. cbp=0;
  657. av_assert2(!s->mb_intra);
  658. if(IS_QUANT(mb_type)){
  659. h263_decode_dquant(s);
  660. }
  661. if(IS_DIRECT(mb_type)){
  662. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
  663. mb_type |= ff_mpeg4_set_direct_mv(s, 0, 0);
  664. }else{
  665. s->mv_dir = 0;
  666. s->mv_type= MV_TYPE_16X16;
  667. //FIXME UMV
  668. if(USES_LIST(mb_type, 0)){
  669. int16_t *mot_val= ff_h263_pred_motion(s, 0, 0, &mx, &my);
  670. s->mv_dir = MV_DIR_FORWARD;
  671. mx = ff_h263_decode_motion(s, mx, 1);
  672. my = ff_h263_decode_motion(s, my, 1);
  673. s->mv[0][0][0] = mx;
  674. s->mv[0][0][1] = my;
  675. mot_val[0 ]= mot_val[2 ]= mot_val[0+2*stride]= mot_val[2+2*stride]= mx;
  676. mot_val[1 ]= mot_val[3 ]= mot_val[1+2*stride]= mot_val[3+2*stride]= my;
  677. }
  678. if(USES_LIST(mb_type, 1)){
  679. int16_t *mot_val= ff_h263_pred_motion(s, 0, 1, &mx, &my);
  680. s->mv_dir |= MV_DIR_BACKWARD;
  681. mx = ff_h263_decode_motion(s, mx, 1);
  682. my = ff_h263_decode_motion(s, my, 1);
  683. s->mv[1][0][0] = mx;
  684. s->mv[1][0][1] = my;
  685. mot_val[0 ]= mot_val[2 ]= mot_val[0+2*stride]= mot_val[2+2*stride]= mx;
  686. mot_val[1 ]= mot_val[3 ]= mot_val[1+2*stride]= mot_val[3+2*stride]= my;
  687. }
  688. }
  689. s->current_picture.f.mb_type[xy] = mb_type;
  690. } else { /* I-Frame */
  691. do{
  692. cbpc = get_vlc2(&s->gb, ff_h263_intra_MCBPC_vlc.table, INTRA_MCBPC_VLC_BITS, 2);
  693. if (cbpc < 0){
  694. av_log(s->avctx, AV_LOG_ERROR, "I cbpc damaged at %d %d\n", s->mb_x, s->mb_y);
  695. return -1;
  696. }
  697. }while(cbpc == 8);
  698. s->dsp.clear_blocks(s->block[0]);
  699. dquant = cbpc & 4;
  700. s->mb_intra = 1;
  701. intra:
  702. s->current_picture.f.mb_type[xy] = MB_TYPE_INTRA;
  703. if (s->h263_aic) {
  704. s->ac_pred = get_bits1(&s->gb);
  705. if(s->ac_pred){
  706. s->current_picture.f.mb_type[xy] = MB_TYPE_INTRA | MB_TYPE_ACPRED;
  707. s->h263_aic_dir = get_bits1(&s->gb);
  708. }
  709. }else
  710. s->ac_pred = 0;
  711. if(s->pb_frame && get_bits1(&s->gb))
  712. pb_mv_count = h263_get_modb(&s->gb, s->pb_frame, &cbpb);
  713. cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
  714. if(cbpy<0){
  715. av_log(s->avctx, AV_LOG_ERROR, "I cbpy damaged at %d %d\n", s->mb_x, s->mb_y);
  716. return -1;
  717. }
  718. cbp = (cbpc & 3) | (cbpy << 2);
  719. if (dquant) {
  720. h263_decode_dquant(s);
  721. }
  722. pb_mv_count += !!s->pb_frame;
  723. }
  724. while(pb_mv_count--){
  725. ff_h263_decode_motion(s, 0, 1);
  726. ff_h263_decode_motion(s, 0, 1);
  727. }
  728. /* decode each block */
  729. for (i = 0; i < 6; i++) {
  730. if (h263_decode_block(s, block[i], i, cbp&32) < 0)
  731. return -1;
  732. cbp+=cbp;
  733. }
  734. if(s->pb_frame && h263_skip_b_part(s, cbpb) < 0)
  735. return -1;
  736. if(s->obmc && !s->mb_intra){
  737. if(s->pict_type == AV_PICTURE_TYPE_P && s->mb_x+1<s->mb_width && s->mb_num_left != 1)
  738. preview_obmc(s);
  739. }
  740. end:
  741. /* per-MB end of slice check */
  742. {
  743. int v= show_bits(&s->gb, 16);
  744. if (get_bits_left(&s->gb) < 16) {
  745. v >>= 16 - get_bits_left(&s->gb);
  746. }
  747. if(v==0)
  748. return SLICE_END;
  749. }
  750. return SLICE_OK;
  751. }
  752. /* most is hardcoded. should extend to handle all h263 streams */
  753. int ff_h263_decode_picture_header(MpegEncContext *s)
  754. {
  755. int format, width, height, i;
  756. uint32_t startcode;
  757. align_get_bits(&s->gb);
  758. startcode= get_bits(&s->gb, 22-8);
  759. for(i= get_bits_left(&s->gb); i>24; i-=8) {
  760. startcode = ((startcode << 8) | get_bits(&s->gb, 8)) & 0x003FFFFF;
  761. if(startcode == 0x20)
  762. break;
  763. }
  764. if (startcode != 0x20) {
  765. av_log(s->avctx, AV_LOG_ERROR, "Bad picture start code\n");
  766. return -1;
  767. }
  768. /* temporal reference */
  769. i = get_bits(&s->gb, 8); /* picture timestamp */
  770. if( (s->picture_number&~0xFF)+i < s->picture_number)
  771. i+= 256;
  772. s->current_picture_ptr->f.pts =
  773. s->picture_number= (s->picture_number&~0xFF) + i;
  774. /* PTYPE starts here */
  775. if (get_bits1(&s->gb) != 1) {
  776. /* marker */
  777. av_log(s->avctx, AV_LOG_ERROR, "Bad marker\n");
  778. return -1;
  779. }
  780. if (get_bits1(&s->gb) != 0) {
  781. av_log(s->avctx, AV_LOG_ERROR, "Bad H263 id\n");
  782. return -1; /* h263 id */
  783. }
  784. skip_bits1(&s->gb); /* split screen off */
  785. skip_bits1(&s->gb); /* camera off */
  786. skip_bits1(&s->gb); /* freeze picture release off */
  787. format = get_bits(&s->gb, 3);
  788. /*
  789. 0 forbidden
  790. 1 sub-QCIF
  791. 10 QCIF
  792. 7 extended PTYPE (PLUSPTYPE)
  793. */
  794. if (format != 7 && format != 6) {
  795. s->h263_plus = 0;
  796. /* H.263v1 */
  797. width = ff_h263_format[format][0];
  798. height = ff_h263_format[format][1];
  799. if (!width)
  800. return -1;
  801. s->pict_type = AV_PICTURE_TYPE_I + get_bits1(&s->gb);
  802. s->h263_long_vectors = get_bits1(&s->gb);
  803. if (get_bits1(&s->gb) != 0) {
  804. av_log(s->avctx, AV_LOG_ERROR, "H263 SAC not supported\n");
  805. return -1; /* SAC: off */
  806. }
  807. s->obmc= get_bits1(&s->gb); /* Advanced prediction mode */
  808. s->unrestricted_mv = s->h263_long_vectors || s->obmc;
  809. s->pb_frame = get_bits1(&s->gb);
  810. s->chroma_qscale= s->qscale = get_bits(&s->gb, 5);
  811. skip_bits1(&s->gb); /* Continuous Presence Multipoint mode: off */
  812. s->width = width;
  813. s->height = height;
  814. s->avctx->sample_aspect_ratio= (AVRational){12,11};
  815. s->avctx->time_base= (AVRational){1001, 30000};
  816. } else {
  817. int ufep;
  818. /* H.263v2 */
  819. s->h263_plus = 1;
  820. ufep = get_bits(&s->gb, 3); /* Update Full Extended PTYPE */
  821. /* ufep other than 0 and 1 are reserved */
  822. if (ufep == 1) {
  823. /* OPPTYPE */
  824. format = get_bits(&s->gb, 3);
  825. av_dlog(s->avctx, "ufep=1, format: %d\n", format);
  826. s->custom_pcf= get_bits1(&s->gb);
  827. s->umvplus = get_bits1(&s->gb); /* Unrestricted Motion Vector */
  828. if (get_bits1(&s->gb) != 0) {
  829. av_log(s->avctx, AV_LOG_ERROR, "Syntax-based Arithmetic Coding (SAC) not supported\n");
  830. }
  831. s->obmc= get_bits1(&s->gb); /* Advanced prediction mode */
  832. s->h263_aic = get_bits1(&s->gb); /* Advanced Intra Coding (AIC) */
  833. s->loop_filter= get_bits1(&s->gb);
  834. s->unrestricted_mv = s->umvplus || s->obmc || s->loop_filter;
  835. if(s->avctx->lowres)
  836. s->loop_filter = 0;
  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 ||
  955. s->pp_time <= s->pp_time - s->pb_time ||
  956. s->pp_time <= 0){
  957. s->pp_time = 2;
  958. s->pb_time = 1;
  959. }
  960. ff_mpeg4_init_direct_mv(s);
  961. }
  962. /* PEI */
  963. while (get_bits1(&s->gb) != 0) {
  964. skip_bits(&s->gb, 8);
  965. }
  966. if(s->h263_slice_structured){
  967. if (get_bits1(&s->gb) != 1) {
  968. av_log(s->avctx, AV_LOG_ERROR, "SEPB1 marker missing\n");
  969. return -1;
  970. }
  971. ff_h263_decode_mba(s);
  972. if (get_bits1(&s->gb) != 1) {
  973. av_log(s->avctx, AV_LOG_ERROR, "SEPB2 marker missing\n");
  974. return -1;
  975. }
  976. }
  977. s->f_code = 1;
  978. if(s->h263_aic){
  979. s->y_dc_scale_table=
  980. s->c_dc_scale_table= ff_aic_dc_scale_table;
  981. }else{
  982. s->y_dc_scale_table=
  983. s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
  984. }
  985. ff_h263_show_pict_info(s);
  986. if (s->pict_type == AV_PICTURE_TYPE_I && s->codec_tag == AV_RL32("ZYGO") && get_bits_left(&s->gb) >= 85 + 13*3*16 + 50){
  987. int i,j;
  988. for(i=0; i<85; i++) av_log(s->avctx, AV_LOG_DEBUG, "%d", get_bits1(&s->gb));
  989. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  990. for(i=0; i<13; i++){
  991. for(j=0; j<3; j++){
  992. int v= get_bits(&s->gb, 8);
  993. v |= get_sbits(&s->gb, 8)<<8;
  994. av_log(s->avctx, AV_LOG_DEBUG, " %5d", v);
  995. }
  996. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  997. }
  998. for(i=0; i<50; i++) av_log(s->avctx, AV_LOG_DEBUG, "%d", get_bits1(&s->gb));
  999. }
  1000. return 0;
  1001. }