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.

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