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.

1145 lines
36KB

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