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.

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