ituh263dec.c 45 KB

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