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.

1137 lines
36KB

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