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.

1158 lines
37KB

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