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 "mpegutils.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->framerate.num, s->avctx->framerate.den
  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. av_cold void ff_h263_decode_init_vlc(void)
  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 *restrict p, const uint8_t * restrict end)
  184. {
  185. assert(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->avctx->priv_data);
  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->avctx->priv_data);
  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. assert(s->pict_type == AV_PICTURE_TYPE_P);
  299. do{
  300. if (get_bits1(&s->gb)) {
  301. /* skip mb */
  302. mot_val = s->current_picture.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.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.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.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.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, int16_t * 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 (CONFIG_RV10_DECODER && s->codec_id == AV_CODEC_ID_RV10) {
  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. }else{
  417. level = get_bits(&s->gb, 8);
  418. if((level&0x7F) == 0){
  419. av_log(s->avctx, AV_LOG_ERROR, "illegal dc %d at %d %d\n", level, s->mb_x, s->mb_y);
  420. if(s->err_recognition & AV_EF_BITSTREAM)
  421. return -1;
  422. }
  423. if (level == 255)
  424. level = 128;
  425. }
  426. block[0] = level;
  427. i = 1;
  428. } else {
  429. i = 0;
  430. }
  431. if (!coded) {
  432. if (s->mb_intra && s->h263_aic)
  433. goto not_coded;
  434. s->block_last_index[n] = i - 1;
  435. return 0;
  436. }
  437. retry:
  438. for(;;) {
  439. code = get_vlc2(&s->gb, rl->vlc.table, TEX_VLC_BITS, 2);
  440. if (code < 0){
  441. av_log(s->avctx, AV_LOG_ERROR, "illegal ac vlc code at %dx%d\n", s->mb_x, s->mb_y);
  442. return -1;
  443. }
  444. if (code == rl->n) {
  445. /* escape */
  446. if (CONFIG_FLV_DECODER && s->h263_flv > 1) {
  447. ff_flv2_decode_ac_esc(&s->gb, &level, &run, &last);
  448. } else {
  449. last = get_bits1(&s->gb);
  450. run = get_bits(&s->gb, 6);
  451. level = (int8_t)get_bits(&s->gb, 8);
  452. if(level == -128){
  453. if (s->codec_id == AV_CODEC_ID_RV10) {
  454. /* XXX: should patch encoder too */
  455. level = get_sbits(&s->gb, 12);
  456. }else{
  457. level = get_bits(&s->gb, 5);
  458. level |= get_sbits(&s->gb, 6)<<5;
  459. }
  460. }
  461. }
  462. } else {
  463. run = rl->table_run[code];
  464. level = rl->table_level[code];
  465. last = code >= rl->last;
  466. if (get_bits1(&s->gb))
  467. level = -level;
  468. }
  469. i += run;
  470. if (i >= 64){
  471. if(s->alt_inter_vlc && rl == &ff_h263_rl_inter && !s->mb_intra){
  472. //Looks like a hack but no, it's the way it is supposed to work ...
  473. rl = &ff_rl_intra_aic;
  474. i = 0;
  475. s->gb= gb;
  476. s->bdsp.clear_block(block);
  477. goto retry;
  478. }
  479. av_log(s->avctx, AV_LOG_ERROR, "run overflow at %dx%d i:%d\n", s->mb_x, s->mb_y, s->mb_intra);
  480. return -1;
  481. }
  482. j = scan_table[i];
  483. block[j] = level;
  484. if (last)
  485. break;
  486. i++;
  487. }
  488. not_coded:
  489. if (s->mb_intra && s->h263_aic) {
  490. ff_h263_pred_acdc(s, block, n);
  491. i = 63;
  492. }
  493. s->block_last_index[n] = i;
  494. return 0;
  495. }
  496. static int h263_skip_b_part(MpegEncContext *s, int cbp)
  497. {
  498. LOCAL_ALIGNED_16(int16_t, dblock, [64]);
  499. int i, mbi;
  500. /* we have to set s->mb_intra to zero to decode B-part of PB-frame correctly
  501. * but real value should be restored in order to be used later (in OBMC condition)
  502. */
  503. mbi = s->mb_intra;
  504. s->mb_intra = 0;
  505. for (i = 0; i < 6; i++) {
  506. if (h263_decode_block(s, dblock, i, cbp&32) < 0)
  507. return -1;
  508. cbp+=cbp;
  509. }
  510. s->mb_intra = mbi;
  511. return 0;
  512. }
  513. static int h263_get_modb(GetBitContext *gb, int pb_frame, int *cbpb)
  514. {
  515. int c, mv = 1;
  516. if (pb_frame < 3) { // h.263 Annex G and i263 PB-frame
  517. c = get_bits1(gb);
  518. if (pb_frame == 2 && c)
  519. mv = !get_bits1(gb);
  520. } else { // h.263 Annex M improved PB-frame
  521. mv = get_unary(gb, 0, 4) + 1;
  522. c = mv & 1;
  523. mv = !!(mv & 2);
  524. }
  525. if(c)
  526. *cbpb = get_bits(gb, 6);
  527. return mv;
  528. }
  529. int ff_h263_decode_mb(MpegEncContext *s,
  530. int16_t block[6][64])
  531. {
  532. int cbpc, cbpy, i, cbp, pred_x, pred_y, mx, my, dquant;
  533. int16_t *mot_val;
  534. const int xy= s->mb_x + s->mb_y * s->mb_stride;
  535. int cbpb = 0, pb_mv_count = 0;
  536. assert(!s->h263_pred);
  537. if (s->pict_type == AV_PICTURE_TYPE_P) {
  538. do{
  539. if (get_bits1(&s->gb)) {
  540. /* skip mb */
  541. s->mb_intra = 0;
  542. for(i=0;i<6;i++)
  543. s->block_last_index[i] = -1;
  544. s->mv_dir = MV_DIR_FORWARD;
  545. s->mv_type = MV_TYPE_16X16;
  546. s->current_picture.mb_type[xy] = MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
  547. s->mv[0][0][0] = 0;
  548. s->mv[0][0][1] = 0;
  549. s->mb_skipped = !(s->obmc | s->loop_filter);
  550. goto end;
  551. }
  552. cbpc = get_vlc2(&s->gb, ff_h263_inter_MCBPC_vlc.table, INTER_MCBPC_VLC_BITS, 2);
  553. if (cbpc < 0){
  554. av_log(s->avctx, AV_LOG_ERROR, "cbpc damaged at %d %d\n", s->mb_x, s->mb_y);
  555. return -1;
  556. }
  557. }while(cbpc == 20);
  558. s->bdsp.clear_blocks(s->block[0]);
  559. dquant = cbpc & 8;
  560. s->mb_intra = ((cbpc & 4) != 0);
  561. if (s->mb_intra) goto intra;
  562. if(s->pb_frame && get_bits1(&s->gb))
  563. pb_mv_count = h263_get_modb(&s->gb, s->pb_frame, &cbpb);
  564. cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
  565. if(s->alt_inter_vlc==0 || (cbpc & 3)!=3)
  566. cbpy ^= 0xF;
  567. cbp = (cbpc & 3) | (cbpy << 2);
  568. if (dquant) {
  569. h263_decode_dquant(s);
  570. }
  571. s->mv_dir = MV_DIR_FORWARD;
  572. if ((cbpc & 16) == 0) {
  573. s->current_picture.mb_type[xy] = MB_TYPE_16x16 | MB_TYPE_L0;
  574. /* 16x16 motion prediction */
  575. s->mv_type = MV_TYPE_16X16;
  576. ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
  577. if (s->umvplus)
  578. mx = h263p_decode_umotion(s, pred_x);
  579. else
  580. mx = ff_h263_decode_motion(s, pred_x, 1);
  581. if (mx >= 0xffff)
  582. return -1;
  583. if (s->umvplus)
  584. my = h263p_decode_umotion(s, pred_y);
  585. else
  586. my = ff_h263_decode_motion(s, pred_y, 1);
  587. if (my >= 0xffff)
  588. return -1;
  589. s->mv[0][0][0] = mx;
  590. s->mv[0][0][1] = my;
  591. if (s->umvplus && (mx - pred_x) == 1 && (my - pred_y) == 1)
  592. skip_bits1(&s->gb); /* Bit stuffing to prevent PSC */
  593. } else {
  594. s->current_picture.mb_type[xy] = MB_TYPE_8x8 | MB_TYPE_L0;
  595. s->mv_type = MV_TYPE_8X8;
  596. for(i=0;i<4;i++) {
  597. mot_val = ff_h263_pred_motion(s, i, 0, &pred_x, &pred_y);
  598. if (s->umvplus)
  599. mx = h263p_decode_umotion(s, pred_x);
  600. else
  601. mx = ff_h263_decode_motion(s, pred_x, 1);
  602. if (mx >= 0xffff)
  603. return -1;
  604. if (s->umvplus)
  605. my = h263p_decode_umotion(s, pred_y);
  606. else
  607. my = ff_h263_decode_motion(s, pred_y, 1);
  608. if (my >= 0xffff)
  609. return -1;
  610. s->mv[0][i][0] = mx;
  611. s->mv[0][i][1] = my;
  612. if (s->umvplus && (mx - pred_x) == 1 && (my - pred_y) == 1)
  613. skip_bits1(&s->gb); /* Bit stuffing to prevent PSC */
  614. mot_val[0] = mx;
  615. mot_val[1] = my;
  616. }
  617. }
  618. } else if(s->pict_type==AV_PICTURE_TYPE_B) {
  619. int mb_type;
  620. const int stride= s->b8_stride;
  621. int16_t *mot_val0 = s->current_picture.motion_val[0][2 * (s->mb_x + s->mb_y * stride)];
  622. int16_t *mot_val1 = s->current_picture.motion_val[1][2 * (s->mb_x + s->mb_y * stride)];
  623. // const int mv_xy= s->mb_x + 1 + s->mb_y * s->mb_stride;
  624. //FIXME ugly
  625. mot_val0[0 ]= mot_val0[2 ]= mot_val0[0+2*stride]= mot_val0[2+2*stride]=
  626. mot_val0[1 ]= mot_val0[3 ]= mot_val0[1+2*stride]= mot_val0[3+2*stride]=
  627. mot_val1[0 ]= mot_val1[2 ]= mot_val1[0+2*stride]= mot_val1[2+2*stride]=
  628. mot_val1[1 ]= mot_val1[3 ]= mot_val1[1+2*stride]= mot_val1[3+2*stride]= 0;
  629. do{
  630. mb_type= get_vlc2(&s->gb, h263_mbtype_b_vlc.table, H263_MBTYPE_B_VLC_BITS, 2);
  631. if (mb_type < 0){
  632. av_log(s->avctx, AV_LOG_ERROR, "b mb_type damaged at %d %d\n", s->mb_x, s->mb_y);
  633. return -1;
  634. }
  635. mb_type= h263_mb_type_b_map[ mb_type ];
  636. }while(!mb_type);
  637. s->mb_intra = IS_INTRA(mb_type);
  638. if(HAS_CBP(mb_type)){
  639. s->bdsp.clear_blocks(s->block[0]);
  640. cbpc = get_vlc2(&s->gb, cbpc_b_vlc.table, CBPC_B_VLC_BITS, 1);
  641. if(s->mb_intra){
  642. dquant = IS_QUANT(mb_type);
  643. goto intra;
  644. }
  645. cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
  646. if (cbpy < 0){
  647. av_log(s->avctx, AV_LOG_ERROR, "b cbpy damaged at %d %d\n", s->mb_x, s->mb_y);
  648. return -1;
  649. }
  650. if(s->alt_inter_vlc==0 || (cbpc & 3)!=3)
  651. cbpy ^= 0xF;
  652. cbp = (cbpc & 3) | (cbpy << 2);
  653. }else
  654. cbp=0;
  655. assert(!s->mb_intra);
  656. if(IS_QUANT(mb_type)){
  657. h263_decode_dquant(s);
  658. }
  659. if(IS_DIRECT(mb_type)){
  660. if (!s->pp_time)
  661. return AVERROR_INVALIDDATA;
  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.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->bdsp.clear_blocks(s->block[0]);
  699. dquant = cbpc & 4;
  700. s->mb_intra = 1;
  701. intra:
  702. s->current_picture.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.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->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->framerate = (AVRational){ 30000, 1001 };
  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->framerate.num = 1800000;
  903. s->avctx->framerate.den = 1000 + get_bits1(&s->gb);
  904. s->avctx->framerate.den *= get_bits(&s->gb, 7);
  905. if(s->avctx->framerate.den == 0){
  906. av_log(s, AV_LOG_ERROR, "zero framerate\n");
  907. return -1;
  908. }
  909. gcd= av_gcd(s->avctx->framerate.den, s->avctx->framerate.num);
  910. s->avctx->framerate.den /= gcd;
  911. s->avctx->framerate.num /= gcd;
  912. }else{
  913. s->avctx->framerate = (AVRational){ 30000, 1001 };
  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. }