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.

1149 lines
36KB

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