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.

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