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.

1158 lines
38KB

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