/libavcodec/ituh263dec.c

http://github.com/FFmpeg/FFmpeg · C · 1298 lines · 1031 code · 163 blank · 104 comment · 324 complexity · 7c41967e611fdf067f8204ea8f09b10b MD5 · raw file

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