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.

1157 lines
38KB

  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->framerate.num, s->avctx->framerate.den
  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(check_marker(&s->gb, "before MBA")==0)
  156. return -1;
  157. ff_h263_decode_mba(s);
  158. if(s->mb_num > 1583)
  159. if(check_marker(&s->gb, "after MBA")==0)
  160. return -1;
  161. s->qscale = get_bits(&s->gb, 5); /* SQUANT */
  162. if(check_marker(&s->gb, "after SQUANT")==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 level, i, j, 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 (CONFIG_RV10_DECODER && s->codec_id == AV_CODEC_ID_RV10) {
  379. if (s->rv10_version == 3 && s->pict_type == AV_PICTURE_TYPE_I) {
  380. int component, diff;
  381. component = (n <= 3 ? 0 : n - 4 + 1);
  382. level = s->last_dc[component];
  383. if (s->rv10_first_dc_coded[component]) {
  384. diff = ff_rv_decode_dc(s, n);
  385. if (diff == 0xffff)
  386. return -1;
  387. level += diff;
  388. level = level & 0xff; /* handle wrap round */
  389. s->last_dc[component] = level;
  390. } else {
  391. s->rv10_first_dc_coded[component] = 1;
  392. }
  393. } else {
  394. level = get_bits(&s->gb, 8);
  395. if (level == 255)
  396. level = 128;
  397. }
  398. }else{
  399. level = get_bits(&s->gb, 8);
  400. if((level&0x7F) == 0){
  401. av_log(s->avctx, AV_LOG_ERROR, "illegal dc %d at %d %d\n", level, s->mb_x, s->mb_y);
  402. if(s->err_recognition & (AV_EF_BITSTREAM|AV_EF_COMPLIANT))
  403. return -1;
  404. }
  405. if (level == 255)
  406. level = 128;
  407. }
  408. block[0] = level;
  409. i = 1;
  410. } else {
  411. i = 0;
  412. }
  413. if (!coded) {
  414. if (s->mb_intra && s->h263_aic)
  415. goto not_coded;
  416. s->block_last_index[n] = i - 1;
  417. return 0;
  418. }
  419. retry:
  420. {
  421. OPEN_READER(re, &s->gb);
  422. i--; // offset by -1 to allow direct indexing of scan_table
  423. for(;;) {
  424. UPDATE_CACHE(re, &s->gb);
  425. GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
  426. if (run == 66) {
  427. if (level){
  428. CLOSE_READER(re, &s->gb);
  429. av_log(s->avctx, AV_LOG_ERROR, "illegal ac vlc code at %dx%d\n", s->mb_x, s->mb_y);
  430. return -1;
  431. }
  432. /* escape */
  433. if (CONFIG_FLV_DECODER && s->h263_flv > 1) {
  434. int is11 = SHOW_UBITS(re, &s->gb, 1);
  435. SKIP_CACHE(re, &s->gb, 1);
  436. run = SHOW_UBITS(re, &s->gb, 7) + 1;
  437. if (is11) {
  438. SKIP_COUNTER(re, &s->gb, 1 + 7);
  439. UPDATE_CACHE(re, &s->gb);
  440. level = SHOW_SBITS(re, &s->gb, 11);
  441. SKIP_COUNTER(re, &s->gb, 11);
  442. } else {
  443. SKIP_CACHE(re, &s->gb, 7);
  444. level = SHOW_SBITS(re, &s->gb, 7);
  445. SKIP_COUNTER(re, &s->gb, 1 + 7 + 7);
  446. }
  447. } else {
  448. run = SHOW_UBITS(re, &s->gb, 7) + 1;
  449. SKIP_CACHE(re, &s->gb, 7);
  450. level = (int8_t)SHOW_UBITS(re, &s->gb, 8);
  451. SKIP_COUNTER(re, &s->gb, 7 + 8);
  452. if(level == -128){
  453. UPDATE_CACHE(re, &s->gb);
  454. if (s->codec_id == AV_CODEC_ID_RV10) {
  455. /* XXX: should patch encoder too */
  456. level = SHOW_SBITS(re, &s->gb, 12);
  457. SKIP_COUNTER(re, &s->gb, 12);
  458. }else{
  459. level = SHOW_UBITS(re, &s->gb, 5);
  460. SKIP_CACHE(re, &s->gb, 5);
  461. level |= SHOW_SBITS(re, &s->gb, 6)<<5;
  462. SKIP_COUNTER(re, &s->gb, 5 + 6);
  463. }
  464. }
  465. }
  466. } else {
  467. if (SHOW_UBITS(re, &s->gb, 1))
  468. level = -level;
  469. SKIP_COUNTER(re, &s->gb, 1);
  470. }
  471. i += run;
  472. if (i >= 64){
  473. CLOSE_READER(re, &s->gb);
  474. // redo update without last flag, revert -1 offset
  475. i = i - run + ((run-1)&63) + 1;
  476. if (i < 64) {
  477. // only last marker, no overrun
  478. block[scan_table[i]] = level;
  479. break;
  480. }
  481. if(s->alt_inter_vlc && rl == &ff_h263_rl_inter && !s->mb_intra){
  482. //Looks like a hack but no, it's the way it is supposed to work ...
  483. rl = &ff_rl_intra_aic;
  484. i = 0;
  485. s->gb= gb;
  486. s->bdsp.clear_block(block);
  487. goto retry;
  488. }
  489. av_log(s->avctx, AV_LOG_ERROR, "run overflow at %dx%d i:%d\n", s->mb_x, s->mb_y, s->mb_intra);
  490. return -1;
  491. }
  492. j = scan_table[i];
  493. block[j] = level;
  494. }
  495. }
  496. not_coded:
  497. if (s->mb_intra && s->h263_aic) {
  498. ff_h263_pred_acdc(s, block, n);
  499. i = 63;
  500. }
  501. s->block_last_index[n] = i;
  502. return 0;
  503. }
  504. static int h263_skip_b_part(MpegEncContext *s, int cbp)
  505. {
  506. LOCAL_ALIGNED_16(int16_t, dblock, [64]);
  507. int i, mbi;
  508. int bli[6];
  509. /* we have to set s->mb_intra to zero to decode B-part of PB-frame correctly
  510. * but real value should be restored in order to be used later (in OBMC condition)
  511. */
  512. mbi = s->mb_intra;
  513. memcpy(bli, s->block_last_index, sizeof(bli));
  514. s->mb_intra = 0;
  515. for (i = 0; i < 6; i++) {
  516. if (h263_decode_block(s, dblock, i, cbp&32) < 0)
  517. return -1;
  518. cbp+=cbp;
  519. }
  520. s->mb_intra = mbi;
  521. memcpy(s->block_last_index, bli, sizeof(bli));
  522. return 0;
  523. }
  524. static int h263_get_modb(GetBitContext *gb, int pb_frame, int *cbpb)
  525. {
  526. int c, mv = 1;
  527. if (pb_frame < 3) { // h.263 Annex G and i263 PB-frame
  528. c = get_bits1(gb);
  529. if (pb_frame == 2 && c)
  530. mv = !get_bits1(gb);
  531. } else { // h.263 Annex M improved PB-frame
  532. mv = get_unary(gb, 0, 4) + 1;
  533. c = mv & 1;
  534. mv = !!(mv & 2);
  535. }
  536. if(c)
  537. *cbpb = get_bits(gb, 6);
  538. return mv;
  539. }
  540. int ff_h263_decode_mb(MpegEncContext *s,
  541. int16_t block[6][64])
  542. {
  543. int cbpc, cbpy, i, cbp, pred_x, pred_y, mx, my, dquant;
  544. int16_t *mot_val;
  545. const int xy= s->mb_x + s->mb_y * s->mb_stride;
  546. int cbpb = 0, pb_mv_count = 0;
  547. av_assert2(!s->h263_pred);
  548. if (s->pict_type == AV_PICTURE_TYPE_P) {
  549. do{
  550. if (get_bits1(&s->gb)) {
  551. /* skip mb */
  552. s->mb_intra = 0;
  553. for(i=0;i<6;i++)
  554. s->block_last_index[i] = -1;
  555. s->mv_dir = MV_DIR_FORWARD;
  556. s->mv_type = MV_TYPE_16X16;
  557. s->current_picture.mb_type[xy] = MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
  558. s->mv[0][0][0] = 0;
  559. s->mv[0][0][1] = 0;
  560. s->mb_skipped = !(s->obmc | s->loop_filter);
  561. goto end;
  562. }
  563. cbpc = get_vlc2(&s->gb, ff_h263_inter_MCBPC_vlc.table, INTER_MCBPC_VLC_BITS, 2);
  564. if (cbpc < 0){
  565. av_log(s->avctx, AV_LOG_ERROR, "cbpc damaged at %d %d\n", s->mb_x, s->mb_y);
  566. return -1;
  567. }
  568. }while(cbpc == 20);
  569. s->bdsp.clear_blocks(s->block[0]);
  570. dquant = cbpc & 8;
  571. s->mb_intra = ((cbpc & 4) != 0);
  572. if (s->mb_intra) goto intra;
  573. if(s->pb_frame && get_bits1(&s->gb))
  574. pb_mv_count = h263_get_modb(&s->gb, s->pb_frame, &cbpb);
  575. cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
  576. if(s->alt_inter_vlc==0 || (cbpc & 3)!=3)
  577. cbpy ^= 0xF;
  578. cbp = (cbpc & 3) | (cbpy << 2);
  579. if (dquant) {
  580. h263_decode_dquant(s);
  581. }
  582. s->mv_dir = MV_DIR_FORWARD;
  583. if ((cbpc & 16) == 0) {
  584. s->current_picture.mb_type[xy] = MB_TYPE_16x16 | MB_TYPE_L0;
  585. /* 16x16 motion prediction */
  586. s->mv_type = MV_TYPE_16X16;
  587. ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
  588. if (s->umvplus)
  589. mx = h263p_decode_umotion(s, pred_x);
  590. else
  591. mx = ff_h263_decode_motion(s, pred_x, 1);
  592. if (mx >= 0xffff)
  593. return -1;
  594. if (s->umvplus)
  595. my = h263p_decode_umotion(s, pred_y);
  596. else
  597. my = ff_h263_decode_motion(s, pred_y, 1);
  598. if (my >= 0xffff)
  599. return -1;
  600. s->mv[0][0][0] = mx;
  601. s->mv[0][0][1] = my;
  602. if (s->umvplus && (mx - pred_x) == 1 && (my - pred_y) == 1)
  603. skip_bits1(&s->gb); /* Bit stuffing to prevent PSC */
  604. } else {
  605. s->current_picture.mb_type[xy] = MB_TYPE_8x8 | MB_TYPE_L0;
  606. s->mv_type = MV_TYPE_8X8;
  607. for(i=0;i<4;i++) {
  608. mot_val = ff_h263_pred_motion(s, i, 0, &pred_x, &pred_y);
  609. if (s->umvplus)
  610. mx = h263p_decode_umotion(s, pred_x);
  611. else
  612. mx = ff_h263_decode_motion(s, pred_x, 1);
  613. if (mx >= 0xffff)
  614. return -1;
  615. if (s->umvplus)
  616. my = h263p_decode_umotion(s, pred_y);
  617. else
  618. my = ff_h263_decode_motion(s, pred_y, 1);
  619. if (my >= 0xffff)
  620. return -1;
  621. s->mv[0][i][0] = mx;
  622. s->mv[0][i][1] = my;
  623. if (s->umvplus && (mx - pred_x) == 1 && (my - pred_y) == 1)
  624. skip_bits1(&s->gb); /* Bit stuffing to prevent PSC */
  625. mot_val[0] = mx;
  626. mot_val[1] = my;
  627. }
  628. }
  629. } else if(s->pict_type==AV_PICTURE_TYPE_B) {
  630. int mb_type;
  631. const int stride= s->b8_stride;
  632. int16_t *mot_val0 = s->current_picture.motion_val[0][2 * (s->mb_x + s->mb_y * stride)];
  633. int16_t *mot_val1 = s->current_picture.motion_val[1][2 * (s->mb_x + s->mb_y * stride)];
  634. // const int mv_xy= s->mb_x + 1 + s->mb_y * s->mb_stride;
  635. //FIXME ugly
  636. mot_val0[0 ]= mot_val0[2 ]= mot_val0[0+2*stride]= mot_val0[2+2*stride]=
  637. mot_val0[1 ]= mot_val0[3 ]= mot_val0[1+2*stride]= mot_val0[3+2*stride]=
  638. mot_val1[0 ]= mot_val1[2 ]= mot_val1[0+2*stride]= mot_val1[2+2*stride]=
  639. mot_val1[1 ]= mot_val1[3 ]= mot_val1[1+2*stride]= mot_val1[3+2*stride]= 0;
  640. do{
  641. mb_type= get_vlc2(&s->gb, h263_mbtype_b_vlc.table, H263_MBTYPE_B_VLC_BITS, 2);
  642. if (mb_type < 0){
  643. av_log(s->avctx, AV_LOG_ERROR, "b mb_type damaged at %d %d\n", s->mb_x, s->mb_y);
  644. return -1;
  645. }
  646. mb_type= h263_mb_type_b_map[ mb_type ];
  647. }while(!mb_type);
  648. s->mb_intra = IS_INTRA(mb_type);
  649. if(HAS_CBP(mb_type)){
  650. s->bdsp.clear_blocks(s->block[0]);
  651. cbpc = get_vlc2(&s->gb, cbpc_b_vlc.table, CBPC_B_VLC_BITS, 1);
  652. if(s->mb_intra){
  653. dquant = IS_QUANT(mb_type);
  654. goto intra;
  655. }
  656. cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
  657. if (cbpy < 0){
  658. av_log(s->avctx, AV_LOG_ERROR, "b cbpy damaged at %d %d\n", s->mb_x, s->mb_y);
  659. return -1;
  660. }
  661. if(s->alt_inter_vlc==0 || (cbpc & 3)!=3)
  662. cbpy ^= 0xF;
  663. cbp = (cbpc & 3) | (cbpy << 2);
  664. }else
  665. cbp=0;
  666. av_assert2(!s->mb_intra);
  667. if(IS_QUANT(mb_type)){
  668. h263_decode_dquant(s);
  669. }
  670. if(IS_DIRECT(mb_type)){
  671. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
  672. mb_type |= ff_mpeg4_set_direct_mv(s, 0, 0);
  673. }else{
  674. s->mv_dir = 0;
  675. s->mv_type= MV_TYPE_16X16;
  676. //FIXME UMV
  677. if(USES_LIST(mb_type, 0)){
  678. int16_t *mot_val= ff_h263_pred_motion(s, 0, 0, &mx, &my);
  679. s->mv_dir = MV_DIR_FORWARD;
  680. mx = ff_h263_decode_motion(s, mx, 1);
  681. my = ff_h263_decode_motion(s, my, 1);
  682. s->mv[0][0][0] = mx;
  683. s->mv[0][0][1] = my;
  684. mot_val[0 ]= mot_val[2 ]= mot_val[0+2*stride]= mot_val[2+2*stride]= mx;
  685. mot_val[1 ]= mot_val[3 ]= mot_val[1+2*stride]= mot_val[3+2*stride]= my;
  686. }
  687. if(USES_LIST(mb_type, 1)){
  688. int16_t *mot_val= ff_h263_pred_motion(s, 0, 1, &mx, &my);
  689. s->mv_dir |= MV_DIR_BACKWARD;
  690. mx = ff_h263_decode_motion(s, mx, 1);
  691. my = ff_h263_decode_motion(s, my, 1);
  692. s->mv[1][0][0] = mx;
  693. s->mv[1][0][1] = my;
  694. mot_val[0 ]= mot_val[2 ]= mot_val[0+2*stride]= mot_val[2+2*stride]= mx;
  695. mot_val[1 ]= mot_val[3 ]= mot_val[1+2*stride]= mot_val[3+2*stride]= my;
  696. }
  697. }
  698. s->current_picture.mb_type[xy] = mb_type;
  699. } else { /* I-Frame */
  700. do{
  701. cbpc = get_vlc2(&s->gb, ff_h263_intra_MCBPC_vlc.table, INTRA_MCBPC_VLC_BITS, 2);
  702. if (cbpc < 0){
  703. av_log(s->avctx, AV_LOG_ERROR, "I cbpc damaged at %d %d\n", s->mb_x, s->mb_y);
  704. return -1;
  705. }
  706. }while(cbpc == 8);
  707. s->bdsp.clear_blocks(s->block[0]);
  708. dquant = cbpc & 4;
  709. s->mb_intra = 1;
  710. intra:
  711. s->current_picture.mb_type[xy] = MB_TYPE_INTRA;
  712. if (s->h263_aic) {
  713. s->ac_pred = get_bits1(&s->gb);
  714. if(s->ac_pred){
  715. s->current_picture.mb_type[xy] = MB_TYPE_INTRA | MB_TYPE_ACPRED;
  716. s->h263_aic_dir = get_bits1(&s->gb);
  717. }
  718. }else
  719. s->ac_pred = 0;
  720. if(s->pb_frame && get_bits1(&s->gb))
  721. pb_mv_count = h263_get_modb(&s->gb, s->pb_frame, &cbpb);
  722. cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
  723. if(cbpy<0){
  724. av_log(s->avctx, AV_LOG_ERROR, "I cbpy damaged at %d %d\n", s->mb_x, s->mb_y);
  725. return -1;
  726. }
  727. cbp = (cbpc & 3) | (cbpy << 2);
  728. if (dquant) {
  729. h263_decode_dquant(s);
  730. }
  731. pb_mv_count += !!s->pb_frame;
  732. }
  733. while(pb_mv_count--){
  734. ff_h263_decode_motion(s, 0, 1);
  735. ff_h263_decode_motion(s, 0, 1);
  736. }
  737. /* decode each block */
  738. for (i = 0; i < 6; i++) {
  739. if (h263_decode_block(s, block[i], i, cbp&32) < 0)
  740. return -1;
  741. cbp+=cbp;
  742. }
  743. if(s->pb_frame && h263_skip_b_part(s, cbpb) < 0)
  744. return -1;
  745. if(s->obmc && !s->mb_intra){
  746. if(s->pict_type == AV_PICTURE_TYPE_P && s->mb_x+1<s->mb_width && s->mb_num_left != 1)
  747. preview_obmc(s);
  748. }
  749. end:
  750. /* per-MB end of slice check */
  751. {
  752. int v= show_bits(&s->gb, 16);
  753. if (get_bits_left(&s->gb) < 16) {
  754. v >>= 16 - get_bits_left(&s->gb);
  755. }
  756. if(v==0)
  757. return SLICE_END;
  758. }
  759. return SLICE_OK;
  760. }
  761. /* most is hardcoded. should extend to handle all h263 streams */
  762. int ff_h263_decode_picture_header(MpegEncContext *s)
  763. {
  764. int format, width, height, i;
  765. uint32_t startcode;
  766. align_get_bits(&s->gb);
  767. if (show_bits(&s->gb, 2) == 2 && s->avctx->frame_number == 0) {
  768. av_log(s->avctx, AV_LOG_WARNING, "Header looks like RTP instead of H.263\n");
  769. }
  770. startcode= get_bits(&s->gb, 22-8);
  771. for(i= get_bits_left(&s->gb); i>24; i-=8) {
  772. startcode = ((startcode << 8) | get_bits(&s->gb, 8)) & 0x003FFFFF;
  773. if(startcode == 0x20)
  774. break;
  775. }
  776. if (startcode != 0x20) {
  777. av_log(s->avctx, AV_LOG_ERROR, "Bad picture start code\n");
  778. return -1;
  779. }
  780. /* temporal reference */
  781. i = get_bits(&s->gb, 8); /* picture timestamp */
  782. if( (s->picture_number&~0xFF)+i < s->picture_number)
  783. i+= 256;
  784. s->picture_number= (s->picture_number&~0xFF) + i;
  785. /* PTYPE starts here */
  786. if (check_marker(&s->gb, "in PTYPE") != 1) {
  787. return -1;
  788. }
  789. if (get_bits1(&s->gb) != 0) {
  790. av_log(s->avctx, AV_LOG_ERROR, "Bad H263 id\n");
  791. return -1; /* h263 id */
  792. }
  793. skip_bits1(&s->gb); /* split screen off */
  794. skip_bits1(&s->gb); /* camera off */
  795. skip_bits1(&s->gb); /* freeze picture release off */
  796. format = get_bits(&s->gb, 3);
  797. /*
  798. 0 forbidden
  799. 1 sub-QCIF
  800. 10 QCIF
  801. 7 extended PTYPE (PLUSPTYPE)
  802. */
  803. if (format != 7 && format != 6) {
  804. s->h263_plus = 0;
  805. /* H.263v1 */
  806. width = ff_h263_format[format][0];
  807. height = ff_h263_format[format][1];
  808. if (!width)
  809. return -1;
  810. s->pict_type = AV_PICTURE_TYPE_I + get_bits1(&s->gb);
  811. s->h263_long_vectors = get_bits1(&s->gb);
  812. if (get_bits1(&s->gb) != 0) {
  813. av_log(s->avctx, AV_LOG_ERROR, "H263 SAC not supported\n");
  814. return -1; /* SAC: off */
  815. }
  816. s->obmc= get_bits1(&s->gb); /* Advanced prediction mode */
  817. s->unrestricted_mv = s->h263_long_vectors || s->obmc;
  818. s->pb_frame = get_bits1(&s->gb);
  819. s->chroma_qscale= s->qscale = get_bits(&s->gb, 5);
  820. skip_bits1(&s->gb); /* Continuous Presence Multipoint mode: off */
  821. s->width = width;
  822. s->height = height;
  823. s->avctx->sample_aspect_ratio= (AVRational){12,11};
  824. s->avctx->framerate = (AVRational){ 30000, 1001 };
  825. } else {
  826. int ufep;
  827. /* H.263v2 */
  828. s->h263_plus = 1;
  829. ufep = get_bits(&s->gb, 3); /* Update Full Extended PTYPE */
  830. /* ufep other than 0 and 1 are reserved */
  831. if (ufep == 1) {
  832. /* OPPTYPE */
  833. format = get_bits(&s->gb, 3);
  834. av_dlog(s->avctx, "ufep=1, format: %d\n", format);
  835. s->custom_pcf= get_bits1(&s->gb);
  836. s->umvplus = get_bits1(&s->gb); /* Unrestricted Motion Vector */
  837. if (get_bits1(&s->gb) != 0) {
  838. av_log(s->avctx, AV_LOG_ERROR, "Syntax-based Arithmetic Coding (SAC) not supported\n");
  839. }
  840. s->obmc= get_bits1(&s->gb); /* Advanced prediction mode */
  841. s->h263_aic = get_bits1(&s->gb); /* Advanced Intra Coding (AIC) */
  842. s->loop_filter= get_bits1(&s->gb);
  843. s->unrestricted_mv = s->umvplus || s->obmc || s->loop_filter;
  844. if(s->avctx->lowres)
  845. s->loop_filter = 0;
  846. s->h263_slice_structured= get_bits1(&s->gb);
  847. if (get_bits1(&s->gb) != 0) {
  848. av_log(s->avctx, AV_LOG_ERROR, "Reference Picture Selection not supported\n");
  849. }
  850. if (get_bits1(&s->gb) != 0) {
  851. av_log(s->avctx, AV_LOG_ERROR, "Independent Segment Decoding not supported\n");
  852. }
  853. s->alt_inter_vlc= get_bits1(&s->gb);
  854. s->modified_quant= get_bits1(&s->gb);
  855. if(s->modified_quant)
  856. s->chroma_qscale_table= ff_h263_chroma_qscale_table;
  857. skip_bits(&s->gb, 1); /* Prevent start code emulation */
  858. skip_bits(&s->gb, 3); /* Reserved */
  859. } else if (ufep != 0) {
  860. av_log(s->avctx, AV_LOG_ERROR, "Bad UFEP type (%d)\n", ufep);
  861. return -1;
  862. }
  863. /* MPPTYPE */
  864. s->pict_type = get_bits(&s->gb, 3);
  865. switch(s->pict_type){
  866. case 0: s->pict_type= AV_PICTURE_TYPE_I;break;
  867. case 1: s->pict_type= AV_PICTURE_TYPE_P;break;
  868. case 2: s->pict_type= AV_PICTURE_TYPE_P;s->pb_frame = 3;break;
  869. case 3: s->pict_type= AV_PICTURE_TYPE_B;break;
  870. case 7: s->pict_type= AV_PICTURE_TYPE_I;break; //ZYGO
  871. default:
  872. return -1;
  873. }
  874. skip_bits(&s->gb, 2);
  875. s->no_rounding = get_bits1(&s->gb);
  876. skip_bits(&s->gb, 4);
  877. /* Get the picture dimensions */
  878. if (ufep) {
  879. if (format == 6) {
  880. /* Custom Picture Format (CPFMT) */
  881. s->aspect_ratio_info = get_bits(&s->gb, 4);
  882. av_dlog(s->avctx, "aspect: %d\n", s->aspect_ratio_info);
  883. /* aspect ratios:
  884. 0 - forbidden
  885. 1 - 1:1
  886. 2 - 12:11 (CIF 4:3)
  887. 3 - 10:11 (525-type 4:3)
  888. 4 - 16:11 (CIF 16:9)
  889. 5 - 40:33 (525-type 16:9)
  890. 6-14 - reserved
  891. */
  892. width = (get_bits(&s->gb, 9) + 1) * 4;
  893. check_marker(&s->gb, "in dimensions");
  894. height = get_bits(&s->gb, 9) * 4;
  895. av_dlog(s->avctx, "\nH.263+ Custom picture: %dx%d\n",width,height);
  896. if (s->aspect_ratio_info == FF_ASPECT_EXTENDED) {
  897. /* aspected dimensions */
  898. s->avctx->sample_aspect_ratio.num= get_bits(&s->gb, 8);
  899. s->avctx->sample_aspect_ratio.den= get_bits(&s->gb, 8);
  900. }else{
  901. s->avctx->sample_aspect_ratio= ff_h263_pixel_aspect[s->aspect_ratio_info];
  902. }
  903. } else {
  904. width = ff_h263_format[format][0];
  905. height = ff_h263_format[format][1];
  906. s->avctx->sample_aspect_ratio= (AVRational){12,11};
  907. }
  908. s->avctx->sample_aspect_ratio.den <<= s->ehc_mode;
  909. if ((width == 0) || (height == 0))
  910. return -1;
  911. s->width = width;
  912. s->height = height;
  913. if(s->custom_pcf){
  914. int gcd;
  915. s->avctx->framerate.num = 1800000;
  916. s->avctx->framerate.den = 1000 + get_bits1(&s->gb);
  917. s->avctx->framerate.den *= get_bits(&s->gb, 7);
  918. if(s->avctx->framerate.den == 0){
  919. av_log(s, AV_LOG_ERROR, "zero framerate\n");
  920. return -1;
  921. }
  922. gcd= av_gcd(s->avctx->framerate.den, s->avctx->framerate.num);
  923. s->avctx->framerate.den /= gcd;
  924. s->avctx->framerate.num /= gcd;
  925. }else{
  926. s->avctx->framerate = (AVRational){ 30000, 1001 };
  927. }
  928. }
  929. if(s->custom_pcf){
  930. skip_bits(&s->gb, 2); //extended Temporal reference
  931. }
  932. if (ufep) {
  933. if (s->umvplus) {
  934. if(get_bits1(&s->gb)==0) /* Unlimited Unrestricted Motion Vectors Indicator (UUI) */
  935. skip_bits1(&s->gb);
  936. }
  937. if(s->h263_slice_structured){
  938. if (get_bits1(&s->gb) != 0) {
  939. av_log(s->avctx, AV_LOG_ERROR, "rectangular slices not supported\n");
  940. }
  941. if (get_bits1(&s->gb) != 0) {
  942. av_log(s->avctx, AV_LOG_ERROR, "unordered slices not supported\n");
  943. }
  944. }
  945. }
  946. s->qscale = get_bits(&s->gb, 5);
  947. }
  948. if (s->width == 0 || s->height == 0) {
  949. av_log(s->avctx, AV_LOG_ERROR, "dimensions 0\n");
  950. return -1;
  951. }
  952. s->mb_width = (s->width + 15) / 16;
  953. s->mb_height = (s->height + 15) / 16;
  954. s->mb_num = s->mb_width * s->mb_height;
  955. if (s->pb_frame) {
  956. skip_bits(&s->gb, 3); /* Temporal reference for B-pictures */
  957. if (s->custom_pcf)
  958. skip_bits(&s->gb, 2); //extended Temporal reference
  959. skip_bits(&s->gb, 2); /* Quantization information for B-pictures */
  960. }
  961. if (s->pict_type!=AV_PICTURE_TYPE_B) {
  962. s->time = s->picture_number;
  963. s->pp_time = s->time - s->last_non_b_time;
  964. s->last_non_b_time = s->time;
  965. }else{
  966. s->time = s->picture_number;
  967. s->pb_time = s->pp_time - (s->last_non_b_time - s->time);
  968. if (s->pp_time <=s->pb_time ||
  969. s->pp_time <= s->pp_time - s->pb_time ||
  970. s->pp_time <= 0){
  971. s->pp_time = 2;
  972. s->pb_time = 1;
  973. }
  974. ff_mpeg4_init_direct_mv(s);
  975. }
  976. /* PEI */
  977. if (skip_1stop_8data_bits(&s->gb) < 0)
  978. return AVERROR_INVALIDDATA;
  979. if(s->h263_slice_structured){
  980. if (check_marker(&s->gb, "SEPB1") != 1) {
  981. return -1;
  982. }
  983. ff_h263_decode_mba(s);
  984. if (check_marker(&s->gb, "SEPB2") != 1) {
  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. }