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.

1279 lines
42KB

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