mpeg_decode.c
上传用户:wstnjxml
上传日期:2014-04-03
资源大小:7248k
文件大小:33k
源码类别:

Windows CE

开发平台:

C/C++

  1. /*****************************************************************************
  2.  *
  3.  * This program is free software ; you can redistribute it and/or modify
  4.  * it under the terms of the GNU General Public License as published by
  5.  * the Free Software Foundation; either version 2 of the License, or
  6.  * (at your option) any later version.
  7.  *
  8.  * This program is distributed in the hope that it will be useful,
  9.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11.  * GNU General Public License for more details.
  12.  *
  13.  * You should have received a copy of the GNU General Public License
  14.  * along with this program; if not, write to the Free Software
  15.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
  16.  *
  17.  * $Id: mpeg_decode.c 603 2006-01-19 13:00:33Z picard $
  18.  *
  19.  * The Core Pocket Media Player
  20.  * Copyright (c) 2004-2005 Gabor Kovacs
  21.  *
  22.  ****************************************************************************/
  23. #include "../common/common.h"
  24. #include "mpeg1.h"
  25. #include "mpeg_decode.h"
  26. #include "mpeg_stream.h"
  27. static _CONST uint8_t zigzag[64] = 
  28. {
  29.  0,  1,  8, 16,  9,  2,  3, 10, 
  30. 17, 24, 32, 25, 18, 11,  4,  5,
  31. 12, 19, 26, 33, 40, 48, 41, 34, 
  32. 27, 20, 13,  6,  7, 14, 21, 28, 
  33. 35, 42, 49, 56, 57, 50, 43, 36, 
  34. 29, 22, 15, 23, 30, 37, 44, 51, 
  35. 58, 59, 52, 45, 38, 31, 39, 46, 
  36. 53, 60, 61, 54, 47, 55, 62, 63
  37. };
  38. static int UpdateSize(mpeg_decode* dec)
  39. {
  40. if (dec->Codec.IDCT.Width > (MB_X-1)*16 || dec->Codec.IDCT.Height > (MB_Y-1)*16)
  41. {
  42. if (!dec->ErrorShowed)
  43. {
  44. dec->ErrorShowed = 1;
  45. ShowError(dec->Codec.Node.Class,VOUT_CLASS,VOUT_ERROR_SIZE,(MB_X-1)*16,(MB_Y-1)*16);
  46. }
  47. return ERR_NOT_SUPPORTED;
  48. }
  49. dec->mb_xsize = (dec->Codec.IDCT.Width + 15) / 16; 
  50. dec->mb_ysize = (dec->Codec.IDCT.Height + 15) / 16;
  51. dec->pos_end = dec->mb_ysize * MB_X;
  52. return ERR_NONE;
  53. }
  54. static int Discontinuity( mpeg_decode* dec )
  55. {
  56. dec->Frame = 0;
  57. return ERR_NONE;
  58. }
  59. static int Flush( mpeg_decode* dec )
  60. {
  61. dec->StartState = -1;
  62. dec->SliceFound = 0;
  63. return ERR_NONE;
  64. }
  65. static int UpdateInput( mpeg_decode* dec )
  66. {
  67. int Result = ERR_NONE;
  68. IDCT_BLOCK_PREPARE(dec,dec->blockptr);
  69. dec->mb_xsize = 0; 
  70. dec->mb_ysize = 0;
  71. dec->pos_end = 0;
  72. dec->refframe = 0;
  73. dec->Frame = 0;
  74. dec->ValidSeq = 0;
  75. dec->ErrorShowed = 0;
  76. dec->OnlyIVOP = 1;
  77. memcpy(dec->zigzag,zigzag,sizeof(zigzag));
  78. if (dec->Codec.In.Format.Type == PACKET_VIDEO)
  79. Result = CodecIDCTSetFormat(&dec->Codec,PF_YUV420,dec->Codec.In.Format.Format.Video.Width,dec->Codec.In.Format.Format.Video.Height,
  80.                 dec->Codec.In.Format.Format.Video.Width,dec->Codec.In.Format.Format.Video.Height,
  81.   dec->Codec.In.Format.Format.Video.Aspect);
  82. return Result;
  83. }
  84. static bool_t FindNext( mpeg_decode* dec )
  85. {
  86. const uint8_t* Ptr = dec->Codec.Buffer.Data;
  87. int Len = dec->Codec.Buffer.WritePos;
  88. int Pos = dec->Codec.FrameEnd;
  89. int32_t v = dec->StartState;
  90. for (;Pos<Len;++Pos)
  91. {
  92. v = (v<<8) | Ptr[Pos];
  93. if ((v & ~0xFF) == 0x100)
  94. {
  95.             if (v < SLICE_MIN_START_CODE || v > SLICE_MAX_START_CODE)
  96. {
  97. if (dec->SliceFound)
  98. {
  99. dec->Codec.FrameEnd = Pos-3;
  100. dec->SliceFound = 0;
  101. dec->StartState = -1;
  102. return 1;
  103. }
  104. }
  105. else
  106. dec->SliceFound=1;
  107. }
  108. }
  109. dec->Codec.FrameEnd = Pos;
  110. dec->StartState = v;
  111. return 0;
  112. }
  113. static _CONST int FrameRate[16] = 
  114. {
  115.         0, 24000, 24024, 25025,
  116.     30000, 30030, 50050, 60000,
  117.     60060, 15015,  5005, 10010,
  118.     12012, 15015, 25025, 25025,
  119. };
  120. static _CONST int8_t MPEG1_IntraMatrix[64] = 
  121. {
  122. 8,  16, 19, 22, 26, 27, 29, 34,
  123. 16, 16, 22, 24, 27, 29, 34, 37,
  124. 19, 22, 26, 27, 29, 34, 34, 38,
  125. 22, 22, 26, 27, 29, 34, 37, 40,
  126. 22, 26, 27, 29, 32, 35, 40, 48,
  127. 26, 27, 29, 32, 35, 40, 48, 58,
  128. 26, 27, 29, 34, 38, 46, 56, 69,
  129. 27, 29, 35, 38, 46, 56, 69, 83
  130. };
  131. static void initbits(mpeg_decode* dec,const uint8_t *stream, int len)
  132. {
  133. dec->bits = 0;
  134. dec->bitpos = 32;
  135. dec->bitptr = stream;
  136. dec->bitend = stream+len+4;
  137. }
  138. static void loadbits( mpeg_decode* dec )
  139. {
  140. int bitpos = dec->bitpos;
  141. loadbits_pos(dec,bitpos);
  142. dec->bitpos = bitpos;
  143. }
  144. #ifndef NDEBUG
  145. static int showbitslarge( mpeg_decode* dec, int n )
  146. {
  147. int i = showbits(dec,n);
  148. i |= *dec->bitptr >> (40-n-dec->bitpos);
  149. return i;
  150. }
  151. #endif
  152. static void Sequence( mpeg_decode* dec )
  153. {
  154. int i;
  155. loadbits(dec);
  156. dec->width = getbits(dec,12);
  157. dec->height = getbits(dec,12);
  158. loadbits(dec);
  159.     dec->aspect = getbits(dec,4);
  160. dec->Codec.In.Format.PacketRate.Den = 1001;
  161. dec->Codec.In.Format.PacketRate.Num = FrameRate[getbits(dec,4)];
  162. dec->Codec.FrameTime = Scale(TICKSPERSEC,dec->Codec.In.Format.PacketRate.Den,dec->Codec.In.Format.PacketRate.Num);
  163. loadbits(dec);
  164.     dec->Codec.In.Format.ByteRate = getbits(dec,18) * 50;
  165. if (dec->Codec.In.Format.ByteRate > 8*1024*1024)
  166. dec->Codec.In.Format.ByteRate = 0;
  167.     flushbits(dec,1); // marker
  168.     flushbits(dec,10); // vbv_buffer_size
  169.     flushbits(dec,1);
  170. loadbits(dec);
  171.     if (getbits1(dec)) 
  172. {
  173.         for (i=0;i<64;i++) 
  174. {
  175. loadbits(dec);
  176.             dec->IntraMatrix[i] = (uint8_t)getbits(dec,8);
  177.         }
  178.     } 
  179. else 
  180. {
  181.         for (i=0;i<64;i++) 
  182.             dec->IntraMatrix[i] = MPEG1_IntraMatrix[dec->zigzag[i]];
  183.     }
  184.     if (getbits1(dec)) 
  185. {
  186.         for (i=0;i<64;i++) 
  187. {
  188. loadbits(dec);
  189.             dec->InterMatrix[i] = (uint8_t)getbits(dec, 8);
  190.         }
  191.     } 
  192. else 
  193. {
  194.         for (i=0;i<64;i++) 
  195.             dec->InterMatrix[i] = 16;
  196.     }
  197. dec->ValidSeq = 1;
  198. }
  199. static void Picture( mpeg_decode* dec )
  200. {
  201. static const int16_t aspect[16] = 
  202. {
  203.     0,10000, 6735, 7031,
  204.  7615, 8055, 8437, 8935,
  205.  9157, 9815,10255,10695,
  206. 10950,11575,12015,    0,
  207. };
  208. if (!dec->Codec.In.Format.Format.Video.Aspect && aspect[dec->aspect])
  209. dec->Codec.In.Format.Format.Video.Aspect = Scale(ASPECT_ONE,10000,aspect[dec->aspect]);
  210. CodecIDCTSetFormat(&dec->Codec,PF_YUV420,dec->width,dec->height,dec->width,dec->height,dec->Codec.In.Format.Format.Video.Aspect);
  211.     flushbits(dec,10); // temporal ref
  212.     loadbits(dec);
  213. dec->prediction_type = getbits(dec,3);
  214.     flushbits(dec,16); // non constant bit rate
  215. loadbits(dec);
  216.     if (dec->prediction_type == P_VOP || dec->prediction_type == B_VOP)
  217. {
  218.         dec->full_pixel[0] = getbits(dec,1);
  219.         dec->fcode[0] = getbits(dec,3)-1;
  220.     }
  221.     if (dec->prediction_type == B_VOP) 
  222. {
  223.         dec->full_pixel[1] = getbits(dec,1);
  224.         dec->fcode[1] = getbits(dec,3)-1;
  225.     }
  226.     dec->frame_state = 1;
  227. DEBUG_MSG1(DEBUG_VCODEC,T("MPEG Picture:%d"),dec->prediction_type);
  228. }
  229. static int StartFrame( mpeg_decode* dec )
  230. {
  231. int result = ERR_NONE;
  232. switch (dec->prediction_type) 
  233. {
  234. case P_VOP:
  235. if (dec->Frame < 1)
  236. {
  237. if (dec->Codec.Dropping)
  238. dec->Codec.IDCT.Ptr->Null(dec->Codec.IDCT.Ptr,NULL,0);
  239. return ERR_INVALID_DATA;
  240. }
  241. // no break
  242. case I_VOP:
  243. dec->refframe ^= 1;
  244. if (dec->OnlyIVOP && dec->Codec.State.DropLevel)
  245. {
  246. dec->Codec.IDCT.Ptr->Null(dec->Codec.IDCT.Ptr,NULL,0);
  247. return ERR_NONE;
  248. }
  249. if (dec->Codec.IDCT.Count>=3)
  250. dec->Codec.State.DropLevel = 0;
  251. if (dec->Codec.IDCT.Count<3 || dec->Frame==0)
  252. dec->Codec.Show = dec->refframe; // show this refframe
  253. else
  254. dec->Codec.Show = dec->refframe ^ 1; // show last refframe 
  255. if (dec->prediction_type == I_VOP)
  256. {
  257. result = dec->Codec.IDCT.Ptr->FrameStart(dec->Codec.IDCT.Ptr,dec->Frame,&dec->bufframe,dec->refframe,-1,-1,dec->Codec.Show,0);
  258. }
  259. else 
  260. {
  261. dec->OnlyIVOP = 0;
  262. result = dec->Codec.IDCT.Ptr->FrameStart(dec->Codec.IDCT.Ptr,dec->Frame,&dec->bufframe,dec->refframe,dec->refframe^1,-1,dec->Codec.Show,dec->Codec.State.DropLevel);
  263. }
  264. if (result != ERR_NONE)
  265. dec->Codec.Show = -1;
  266. break;
  267. case B_VOP:
  268. if (dec->Frame < 2)
  269. {
  270. if (dec->Codec.Dropping)
  271. dec->Codec.IDCT.Ptr->Null(dec->Codec.IDCT.Ptr,NULL,0);
  272. return ERR_INVALID_DATA;
  273. }
  274. if (dec->Codec.State.DropLevel)
  275. {
  276. dec->Codec.IDCT.Ptr->Null(dec->Codec.IDCT.Ptr,NULL,0);
  277. return ERR_NONE;
  278. }
  279. if (dec->Codec.IDCT.Count<3)
  280. {
  281. CodecIDCTSetCount(&dec->Codec,3);
  282. if (dec->Codec.IDCT.Count<3)
  283. {
  284. dec->Codec.IDCT.Ptr->Null(dec->Codec.IDCT.Ptr,NULL,0);
  285. return ERR_INVALID_DATA;
  286. }
  287. }
  288. dec->OnlyIVOP = 0;
  289. dec->Codec.Show = 2;
  290. result = dec->Codec.IDCT.Ptr->FrameStart(dec->Codec.IDCT.Ptr,-dec->Frame,&dec->bufframe,2,dec->refframe^1,dec->refframe,2,0);
  291. break;
  292. default:
  293. result = ERR_INVALID_DATA;
  294. break;
  295. }
  296. return result;
  297. }
  298. // max 9 bits
  299. static INLINE int getDCsizeLum( mpeg_decode* dec )
  300. {
  301. int i,code;
  302. if (!getbits1(dec))
  303. return getbits(dec,1)+1;
  304. if (!getbits1(dec))
  305. return getbits(dec,1)*3;
  306. code = showbits(dec,7);
  307. for (i=1;i<8;i++,code<<=1)
  308. if (!(code & 64))
  309. {
  310. flushbits(dec,i);
  311. return i+3;
  312. }
  313. flushbits(dec,7);
  314. return 11;
  315. }
  316. // max 10 bits
  317. static INLINE int getDCsizeChr( mpeg_decode* dec )
  318. {
  319. int i,code;
  320. if (!getbits1(dec))
  321. return getbits(dec,1);
  322. code = showbits(dec,9);
  323. for (i=1;i<10;i++,code<<=1)
  324. if (!(code & 256))
  325. {
  326. flushbits(dec,i);
  327. return i+1;
  328. }
  329. flushbits(dec,9);
  330. return 11;
  331. }
  332. // max 11bits
  333. static INLINE int getDCdiff(int dct_dc_size, mpeg_decode* dec)
  334. {
  335. int code = showbits(dec,32); //we need only dct_dc_size bits (but in the higher bits)
  336. int adj = 0;
  337. flushbits(dec,dct_dc_size);
  338. if (code >= 0)
  339. adj = (-1 << dct_dc_size) + 1;
  340. return adj + ((uint32_t)code >> (32-dct_dc_size));
  341. }
  342. #define TABLE_1 0
  343. #define TABLE_2 252
  344. #define TABLE_3 252+112
  345. #define TABLE_END 252+112+112
  346. static _CONST uint16_t vld_mpeg1[252+112+112] = 
  347. {
  348. 0x403e,0x403e,0x403e,0x403e,0x5082,0x5082,0x5241,0x5241,
  349. 0x5004,0x5004,0x5201,0x5201,0x41c1,0x41c1,0x41c1,0x41c1,
  350. 0x4181,0x4181,0x4181,0x4181,0x4042,0x4042,0x4042,0x4042,
  351. 0x4141,0x4141,0x4141,0x4141,0x6341,0x6006,0x6301,0x62c1,
  352. 0x60c2,0x6043,0x6005,0x6281,0x3003,0x3003,0x3003,0x3003,
  353. 0x3003,0x3003,0x3003,0x3003,0x3101,0x3101,0x3101,0x3101,
  354. 0x3101,0x3101,0x3101,0x3101,0x30c1,0x30c1,0x30c1,0x30c1,
  355. 0x30c1,0x30c1,0x30c1,0x30c1,0x2002,0x2002,0x2002,0x2002,
  356. 0x2002,0x2002,0x2002,0x2002,0x2002,0x2002,0x2002,0x2002,
  357. 0x2002,0x2002,0x2002,0x2002,0x2081,0x2081,0x2081,0x2081,
  358. 0x2081,0x2081,0x2081,0x2081,0x2081,0x2081,0x2081,0x2081,
  359. 0x2081,0x2081,0x2081,0x2081,0x1041,0x1041,0x1041,0x1041,
  360. 0x1041,0x1041,0x1041,0x1041,0x1041,0x1041,0x1041,0x1041,
  361. 0x1041,0x1041,0x1041,0x1041,0x1041,0x1041,0x1041,0x1041,
  362. 0x1041,0x1041,0x1041,0x1041,0x1041,0x1041,0x1041,0x1041,
  363. 0x1041,0x1041,0x1041,0x1041,0x003f,0x003f,0x003f,0x003f,
  364. 0x003f,0x003f,0x003f,0x003f,0x003f,0x003f,0x003f,0x003f,
  365. 0x003f,0x003f,0x003f,0x003f,0x003f,0x003f,0x003f,0x003f,
  366. 0x003f,0x003f,0x003f,0x003f,0x003f,0x003f,0x003f,0x003f,
  367. 0x003f,0x003f,0x003f,0x003f,0x003f,0x003f,0x003f,0x003f,
  368. 0x003f,0x003f,0x003f,0x003f,0x003f,0x003f,0x003f,0x003f,
  369. 0x003f,0x003f,0x003f,0x003f,0x003f,0x003f,0x003f,0x003f,
  370. 0x003f,0x003f,0x003f,0x003f,0x003f,0x003f,0x003f,0x003f,
  371. 0x003f,0x003f,0x003f,0x003f,0x0001,0x0001,0x0001,0x0001,
  372. 0x0001,0x0001,0x0001,0x0001,0x0001,0x0001,0x0001,0x0001,
  373. 0x0001,0x0001,0x0001,0x0001,0x0001,0x0001,0x0001,0x0001,
  374. 0x0001,0x0001,0x0001,0x0001,0x0001,0x0001,0x0001,0x0001,
  375. 0x0001,0x0001,0x0001,0x0001,0x0001,0x0001,0x0001,0x0001,
  376. 0x0001,0x0001,0x0001,0x0001,0x0001,0x0001,0x0001,0x0001,
  377. 0x0001,0x0001,0x0001,0x0001,0x0001,0x0001,0x0001,0x0001,
  378. 0x0001,0x0001,0x0001,0x0001,0x0001,0x0001,0x0001,0x0001,
  379. 0x0001,0x0001,0x0001,0x0001,0xb282,0xb242,0xb143,0xb0c4,
  380. 0xb085,0xb047,0xb046,0xb00f,0xb00e,0xb00d,0xb00c,0xb681,
  381. 0xb641,0xb601,0xb5c1,0xb581,0xa00b,0xa00b,0xa202,0xa202,
  382. 0xa103,0xa103,0xa00a,0xa00a,0xa084,0xa084,0xa1c2,0xa1c2,
  383. 0xa541,0xa541,0xa501,0xa501,0xa009,0xa009,0xa4c1,0xa4c1,
  384. 0xa481,0xa481,0xa045,0xa045,0xa0c3,0xa0c3,0xa008,0xa008,
  385. 0xa182,0xa182,0xa441,0xa441,0x8401,0x8401,0x8401,0x8401,
  386. 0x8401,0x8401,0x8401,0x8401,0x8142,0x8142,0x8142,0x8142,
  387. 0x8142,0x8142,0x8142,0x8142,0x8007,0x8007,0x8007,0x8007,
  388. 0x8007,0x8007,0x8007,0x8007,0x8083,0x8083,0x8083,0x8083,
  389. 0x8083,0x8083,0x8083,0x8083,0x8044,0x8044,0x8044,0x8044,
  390. 0x8044,0x8044,0x8044,0x8044,0x83c1,0x83c1,0x83c1,0x83c1,
  391. 0x83c1,0x83c1,0x83c1,0x83c1,0x8381,0x8381,0x8381,0x8381,
  392. 0x8381,0x8381,0x8381,0x8381,0x8102,0x8102,0x8102,0x8102,
  393. 0x8102,0x8102,0x8102,0x8102,0xe052,0xe051,0xe050,0xe04f,
  394. 0xe183,0xe402,0xe3c2,0xe382,0xe342,0xe302,0xe2c2,0xe7c1,
  395. 0xe781,0xe741,0xe701,0xe6c1,0xd028,0xd028,0xd027,0xd027,
  396. 0xd026,0xd026,0xd025,0xd025,0xd024,0xd024,0xd023,0xd023,
  397. 0xd022,0xd022,0xd021,0xd021,0xd020,0xd020,0xd04e,0xd04e,
  398. 0xd04d,0xd04d,0xd04c,0xd04c,0xd04b,0xd04b,0xd04a,0xd04a,
  399. 0xd049,0xd049,0xd048,0xd048,0xc01f,0xc01f,0xc01f,0xc01f,
  400. 0xc01e,0xc01e,0xc01e,0xc01e,0xc01d,0xc01d,0xc01d,0xc01d,
  401. 0xc01c,0xc01c,0xc01c,0xc01c,0xc01b,0xc01b,0xc01b,0xc01b,
  402. 0xc01a,0xc01a,0xc01a,0xc01a,0xc019,0xc019,0xc019,0xc019,
  403. 0xc018,0xc018,0xc018,0xc018,0xc017,0xc017,0xc017,0xc017,
  404. 0xc016,0xc016,0xc016,0xc016,0xc015,0xc015,0xc015,0xc015,
  405. 0xc014,0xc014,0xc014,0xc014,0xc013,0xc013,0xc013,0xc013,
  406. 0xc012,0xc012,0xc012,0xc012,0xc011,0xc011,0xc011,0xc011,
  407. 0xc010,0xc010,0xc010,0xc010
  408. };
  409. // size:4 run:6 level:6 
  410. // size:4 escape    62
  411. // size:4 end 63
  412. #define vld_code
  413. code = showbits_pos(dec,bitpos,16);
  414. if (code >> 10)
  415. code = (code >> 8) - 4 + TABLE_1;
  416. else if (code >> 7)
  417. code = (code >> 3) - 16 + TABLE_2;
  418. else /* if (code >= 16)  but we don't care about invalid huffman codes */ 
  419. code = code - 16 + TABLE_3;
  420. code = table[code];
  421. flushbits_pos(dec,bitpos,2+(code >> 12));
  422. static void blockIntra( mpeg_decode* dec, int pos )
  423. {
  424. int j;
  425. idct_block_t *block;
  426. const uint16_t *table = vld_mpeg1;
  427. int qscale;
  428. dec->Codec.IDCT.Ptr->Process(dec->Codec.IDCT.Ptr,POSX(pos),POSY(pos));
  429. block = dec->blockptr;
  430. qscale = dec->qscale;
  431. for (j=0;j<6;++j)
  432. {
  433. int bitpos;
  434. int len;
  435. ClearBlock(block);
  436. loadbits(dec);
  437. {
  438. int dct_dc_size, dct_dc_diff;
  439. dct_dc_diff = 0;
  440. dct_dc_size = j<4 ? getDCsizeLum(dec) : getDCsizeChr(dec); //max11bit
  441. if (dct_dc_size)
  442. dct_dc_diff = getDCdiff(dct_dc_size,dec);
  443. dct_dc_size = j<4 ? 0 : j-4+1;
  444. DEBUG_MSG2(DEBUG_VCODEC2,T("dc=%d diff=%d"), dec->last_dc[dct_dc_size]+dct_dc_diff, dct_dc_diff );
  445. dct_dc_diff += dec->last_dc[dct_dc_size];
  446. dec->last_dc[dct_dc_size] = dct_dc_diff;
  447. *block = (idct_block_t)(dct_dc_diff << 3);
  448. len = 1;
  449. }
  450. bitpos = dec->bitpos;
  451. for (;;) // event vld
  452. {
  453. int code,level;
  454. loadbits_pos(dec,bitpos);
  455. code = showbits_pos(dec,bitpos,16);
  456. vld_code;
  457. level = code & 63;
  458. if (level < 62) 
  459. {
  460. level *= qscale;
  461. code >>= 6;
  462. code &= 63;
  463. len += code; // run
  464. if (len >= 64)
  465. break;
  466. code = dec->zigzag[len];
  467. level *= dec->IntraMatrix[len];
  468. level >>= 3;
  469. level = (level-1)|1;
  470. if (getbits1_pos(dec,bitpos)) 
  471. level = -level;
  472. block[code] = (idct_block_t)level;
  473. ++len;
  474. else 
  475. {
  476. if (level==63)
  477. break;
  478. // this value is escaped
  479. loadbits_pos(dec,bitpos);
  480. len += showbits_pos(dec,bitpos,6); flushbits_pos(dec,bitpos,6);
  481. if (len >= 64)
  482. break;
  483. code = showbits_pos(dec,bitpos,8); flushbits_pos(dec,bitpos,8);
  484. level = (code << 24) >> 24; //sign extend the lower 8 bits
  485. code = dec->zigzag[len];
  486. if (level == -128)
  487. {
  488. level = showbits_pos(dec,bitpos,8)-256; flushbits_pos(dec,bitpos,8);
  489. }
  490. else 
  491. if (level == 0)
  492. {
  493. level = showbits_pos(dec,bitpos,8); flushbits_pos(dec,bitpos,8);
  494. }
  495. if (level<0)
  496. {
  497. level= -level;
  498. level *= qscale * dec->IntraMatrix[len];
  499. level >>= 3;
  500. level= (level-1)|1;
  501. level= -level;
  502. block[code] = (idct_block_t)level;
  503. ++len;
  504. }
  505. else
  506. {
  507. level *= qscale * dec->IntraMatrix[len];
  508. level >>= 3;
  509. level = (level-1)|1;
  510. block[code] = (idct_block_t)level;
  511. ++len;
  512. }
  513. }
  514. DEBUG_MSG2(DEBUG_VCODEC2,T("intra_block[%i] %i"), code, level );
  515. }
  516. dec->bitpos = bitpos;
  517. dec->Codec.IDCT.Ptr->Intra8x8(dec->Codec.IDCT.Ptr,block,len,IDCTSCAN_ZIGZAG);
  518. }
  519. dec->fmv[0] = dec->bmv[0] = 0;
  520. }
  521. static INLINE int readqscale( mpeg_decode* dec )
  522. {
  523.     return getbits(dec,5);
  524. }
  525. static _CONST uint8_t mcbp_p[32*2+128*2] = 
  526. {
  527. 0x00,0x00,0x08,0x39,0x08,0x2b,0x08,0x29,0x07,0x22,0x07,0x21,0x06,0x3f,0x06,0x24,
  528. 0x05,0x3e,0x05,0x02,0x05,0x3d,0x05,0x01,0x05,0x38,0x05,0x34,0x05,0x2c,0x05,0x1c,
  529. 0x05,0x28,0x05,0x14,0x05,0x30,0x05,0x0c,0x04,0x20,0x04,0x20,0x04,0x10,0x04,0x10,
  530. 0x04,0x08,0x04,0x08,0x04,0x04,0x04,0x04,0x03,0x3c,0x03,0x3c,0x03,0x3c,0x03,0x3c,
  531. 0x00,0x00,0x00,0x00,0x09,0x27,0x09,0x1b,0x09,0x3b,0x09,0x37,0x09,0x2f,0x09,0x1f,
  532. 0x08,0x3a,0x08,0x3a,0x08,0x36,0x08,0x36,0x08,0x2e,0x08,0x2e,0x08,0x1e,0x08,0x1e,
  533. 0x08,0x39,0x08,0x39,0x08,0x35,0x08,0x35,0x08,0x2d,0x08,0x2d,0x08,0x1d,0x08,0x1d,
  534. 0x08,0x26,0x08,0x26,0x08,0x1a,0x08,0x1a,0x08,0x25,0x08,0x25,0x08,0x19,0x08,0x19,
  535. 0x08,0x2b,0x08,0x2b,0x08,0x17,0x08,0x17,0x08,0x33,0x08,0x33,0x08,0x0f,0x08,0x0f,
  536. 0x08,0x2a,0x08,0x2a,0x08,0x16,0x08,0x16,0x08,0x32,0x08,0x32,0x08,0x0e,0x08,0x0e,
  537. 0x08,0x29,0x08,0x29,0x08,0x15,0x08,0x15,0x08,0x31,0x08,0x31,0x08,0x0d,0x08,0x0d,
  538. 0x08,0x23,0x08,0x23,0x08,0x13,0x08,0x13,0x08,0x0b,0x08,0x0b,0x08,0x07,0x08,0x07,
  539. 0x07,0x22,0x07,0x22,0x07,0x22,0x07,0x22,0x07,0x12,0x07,0x12,0x07,0x12,0x07,0x12,
  540. 0x07,0x0a,0x07,0x0a,0x07,0x0a,0x07,0x0a,0x07,0x06,0x07,0x06,0x07,0x06,0x07,0x06,
  541. 0x07,0x21,0x07,0x21,0x07,0x21,0x07,0x21,0x07,0x11,0x07,0x11,0x07,0x11,0x07,0x11,
  542. 0x07,0x09,0x07,0x09,0x07,0x09,0x07,0x09,0x07,0x05,0x07,0x05,0x07,0x05,0x07,0x05,
  543. 0x06,0x3f,0x06,0x3f,0x06,0x3f,0x06,0x3f,0x06,0x3f,0x06,0x3f,0x06,0x3f,0x06,0x3f,
  544. 0x06,0x03,0x06,0x03,0x06,0x03,0x06,0x03,0x06,0x03,0x06,0x03,0x06,0x03,0x06,0x03,
  545. 0x06,0x24,0x06,0x24,0x06,0x24,0x06,0x24,0x06,0x24,0x06,0x24,0x06,0x24,0x06,0x24,
  546. 0x06,0x18,0x06,0x18,0x06,0x18,0x06,0x18,0x06,0x18,0x06,0x18,0x06,0x18,0x06,0x18
  547. };
  548. static _CONST uint8_t mb_type_p[64] =
  549. {
  550. 0x00,0xd1,0xa9,0xa9,0xab,0xab,0xb0,0xb0,0x62,0x62,0x62,0x62,0x62,0x62,0x62,0x62,
  551. 0x48,0x48,0x48,0x48,0x48,0x48,0x48,0x48,0x48,0x48,0x48,0x48,0x48,0x48,0x48,0x48,
  552. 0x2a,0x2a,0x2a,0x2a,0x2a,0x2a,0x2a,0x2a,0x2a,0x2a,0x2a,0x2a,0x2a,0x2a,0x2a,0x2a,
  553. 0x2a,0x2a,0x2a,0x2a,0x2a,0x2a,0x2a,0x2a,0x2a,0x2a,0x2a,0x2a,0x2a,0x2a,0x2a,0x2a
  554. };
  555. static _CONST uint8_t mb_type_b[64] =
  556. {
  557. 0x00,0xd1,0xcd,0xcb,0xaf,0xaf,0xb0,0xb0,0x82,0x82,0x82,0x82,0x8a,0x8a,0x8a,0x8a,
  558. 0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x6c,0x6c,0x6c,0x6c,0x6c,0x6c,0x6c,0x6c,
  559. 0x46,0x46,0x46,0x46,0x46,0x46,0x46,0x46,0x46,0x46,0x46,0x46,0x46,0x46,0x46,0x46,
  560. 0x4e,0x4e,0x4e,0x4e,0x4e,0x4e,0x4e,0x4e,0x4e,0x4e,0x4e,0x4e,0x4e,0x4e,0x4e,0x4e
  561. };
  562. #define SKIP_1  0
  563. #define SKIP_2  16
  564. #define SKIP_3  (16+8)
  565. #define SKIP_END (16+8+64)
  566. static _CONST uint8_t skip[SKIP_END*2] = 
  567. {
  568. 0x0a,0x22,0x07,0x0c,0x04,0x06,0x04,0x05,0x03,0x04,0x03,0x04,0x03,0x03,0x03,0x03,
  569. 0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x01,0x02,0x01,0x02,0x01,0x02,0x01,
  570. 0x07,0x0c,0x07,0x0b,0x07,0x0a,0x07,0x09,0x06,0x08,0x06,0x08,0x06,0x07,0x06,0x07,
  571. 0x0a,0x22,0x0a,0x22,0x0a,0x22,0x0a,0x22,0x0a,0x22,0x0a,0x22,0x0a,0x22,0x0a,0x22,
  572. 0x0a,0x21,0x0a,0x22,0x0a,0x22,0x0a,0x22,0x0a,0x22,0x0a,0x22,0x0a,0x22,0x0a,0x22,
  573. 0x0a,0x22,0x0a,0x22,0x0a,0x22,0x0a,0x22,0x0a,0x22,0x0a,0x22,0x0a,0x22,0x0a,0x22,
  574. 0x0a,0x20,0x0a,0x1f,0x0a,0x1e,0x0a,0x1d,0x0a,0x1c,0x0a,0x1b,0x0a,0x1a,0x0a,0x19,
  575. 0x0a,0x18,0x0a,0x17,0x0a,0x16,0x0a,0x15,0x09,0x14,0x09,0x14,0x09,0x13,0x09,0x13,
  576. 0x09,0x12,0x09,0x12,0x09,0x11,0x09,0x11,0x09,0x10,0x09,0x10,0x09,0x0f,0x09,0x0f,
  577. 0x07,0x0e,0x07,0x0e,0x07,0x0e,0x07,0x0e,0x07,0x0e,0x07,0x0e,0x07,0x0e,0x07,0x0e,
  578. 0x07,0x0d,0x07,0x0d,0x07,0x0d,0x07,0x0d,0x07,0x0d,0x07,0x0d,0x07,0x0d,0x07,0x0d
  579. };
  580. static INLINE void readskip( mpeg_decode* dec )
  581. {
  582. int i;
  583. dec->skip = 0;
  584. while (!getbits1(dec))
  585. {
  586. inlineloadbits(dec);
  587. i = showbits(dec,10);
  588. if (i >> 7)
  589. i = (i >> 6) + SKIP_1;
  590. else
  591. if (i >> 6)
  592. i = (i >> 3)-8 + SKIP_2;
  593. else
  594. i = i + SKIP_3;
  595. i <<= 1;
  596. flushbits(dec,skip[i]);
  597. i = skip[i+1];
  598. if (i<33)
  599. {
  600. dec->skip += i;
  601. break;
  602. }
  603. if (i==33)
  604. dec->skip += 33;
  605. if (eofbits(dec))
  606. break;
  607. }
  608. }
  609. static _CONST uint8_t mv_tab[64] = 
  610. {
  611. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x84,0x7c,0x74,0x6c,
  612. 0x64,0x5c,0x53,0x53,0x4b,0x4b,0x43,0x43,0x39,0x39,0x39,0x39,0x39,0x39,0x39,0x39,
  613. 0x31,0x31,0x31,0x31,0x31,0x31,0x31,0x31,0x29,0x29,0x29,0x29,0x29,0x29,0x29,0x29,
  614. 0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20
  615. };
  616. #define GetMVData( dec, table, shift, shift2, last, bitpos )
  617. {
  618. int code,v;
  619. loadbits_pos(dec,bitpos);
  620. code = showbits_pos(dec,bitpos,10);
  621. if (code >= 512)
  622. {
  623. flushbits_pos(dec,bitpos,1);
  624. }
  625. else
  626. {
  627. if (code >= 64)
  628. {
  629. v = 4;
  630. if (code >= 128) --v;
  631. if (code >= 256) --v;
  632. flushbits_pos(dec,bitpos,v);
  633. code = v-1;
  634. }
  635. else
  636. {
  637. code = table[code];
  638. v = (code & 7)+6;
  639. flushbits_pos(dec,bitpos,v);
  640. code >>= 3;
  641. }
  642. /* 14bits left */
  643. v = showbits_pos(dec,bitpos,1);
  644. flushbits_pos(dec,bitpos,1);
  645. if (shift) /* max 6bits */
  646. {
  647. code = (code - 1) << shift;
  648. code |= showbits_pos(dec,bitpos,shift);
  649. flushbits_pos(dec,bitpos,shift);
  650. code++;
  651. }
  652. if (v)
  653. code = -code;
  654. last += code;
  655. last = ((last + shift2) & (shift2*2-1)) - shift2;
  656. }
  657. }
  658. static void GetMV(int fcode, int* mv, int full, mpeg_decode* dec )
  659. {
  660. int mv_x = ((int16_t*)mv)[MVXIDX];
  661. int mv_y = ((int16_t*)mv)[MVYIDX];
  662. int bitpos = dec->bitpos;
  663. int fcode2 = 1 << (fcode+4);
  664. _CONST uint8_t* tab = mv_tab;
  665. GetMVData(dec,tab,fcode,fcode2,mv_x,bitpos);
  666. GetMVData(dec,tab,fcode,fcode2,mv_y,bitpos);
  667. dec->bitpos = bitpos;
  668. ((int16_t*)mv)[MVXIDX] = (int16_t)mv_x;
  669. ((int16_t*)mv)[MVYIDX] = (int16_t)mv_y;
  670. if (full)
  671. {
  672. mv_x <<= 1;
  673. mv_y <<= 1;
  674. }
  675. mv_x &= 0xFFFF;
  676. mv_y <<= 16;
  677. mv_y |= mv_x;
  678. mv[1] = mv_y;
  679. mv[2] = mv_y;
  680. mv[3] = mv_y;
  681. mv[4] = mv_y;
  682. mv_y >>= 1; //shift
  683. mv_y &= ~0x8000; //replace dx sign bit with old signed bit
  684. mv_x &= 0x8000;
  685. mv_y |= mv_x;
  686. mv[5] = mv_y;
  687. mv[6] = mv_y;
  688. }
  689. static int decodeInter( mpeg_decode* dec )
  690. {
  691. idct_block_t *block = dec->blockptr;
  692. const uint16_t *table = vld_mpeg1;
  693. int qscale;
  694. int bitpos;
  695. int len;
  696. int code,level;
  697. ClearBlock(block);
  698. bitpos = dec->bitpos;
  699. loadbits_pos(dec,bitpos);
  700. len = 0;
  701. qscale = dec->qscale;
  702. // special case for first
  703. code = showbits_pos(dec,bitpos,2);
  704.     if (code & 2) 
  705. {
  706. flushbits_pos(dec,bitpos,2);
  707.         level= (3 * qscale * dec->InterMatrix[0]) >> 4;
  708.         level= (level-1)|1;
  709.         if (code & 1)
  710. level= -level;
  711.         block[0] = (idct_block_t)level;
  712. DEBUG_MSG2(DEBUG_VCODEC2,T("inter_block[%i] %i"), 0, level );
  713.         ++len;
  714.     }
  715. for (;;) // event vld
  716. {
  717. loadbits_pos(dec,bitpos);
  718. code = showbits_pos(dec,bitpos,16);
  719. vld_code;
  720. level = code & 63;
  721. if (level < 62) 
  722. {
  723. level = level*2+1;
  724. level *= qscale;
  725. code >>= 6;
  726. code &= 63;
  727. len += code; // run
  728. if (len >= 64)
  729. break;
  730. code = dec->zigzag[len];
  731. level *= dec->InterMatrix[len];
  732. level >>= 4;
  733. level = (level-1)|1;
  734. if (getbits1_pos(dec,bitpos)) 
  735. level = -level;
  736. block[code] = (idct_block_t)level;
  737. ++len;
  738. else 
  739. {
  740. if (level==63)
  741. break;
  742. // this value is escaped
  743. loadbits_pos(dec,bitpos);
  744. len += showbits_pos(dec,bitpos,6); flushbits_pos(dec,bitpos,6);
  745. if (len >= 64)
  746. break;
  747. code = showbits_pos(dec,bitpos,8); flushbits_pos(dec,bitpos,8);
  748. level = (code << 24) >> 24; //sign extend the lower 8 bits
  749. code = dec->zigzag[len];
  750. if (level == -128)
  751. {
  752. level = showbits_pos(dec,bitpos,8)-256; flushbits_pos(dec,bitpos,8);
  753. }
  754. else 
  755. if (level == 0)
  756. {
  757. level = showbits_pos(dec,bitpos,8); flushbits_pos(dec,bitpos,8);
  758. }
  759. if (level<0)
  760. {
  761. level= -level;
  762. level = level*2+1;
  763. level *= qscale * dec->InterMatrix[len];
  764. level >>= 4;
  765. level= (level-1)|1;
  766. level= -level;
  767. block[code] = (idct_block_t)level;
  768. ++len;
  769. }
  770. else
  771. {
  772. level = level*2+1;
  773. level *= qscale * dec->InterMatrix[len];
  774. level >>= 4;
  775. level = (level-1)|1;
  776. block[code] = (idct_block_t)level;
  777. ++len;
  778. }
  779. }
  780. DEBUG_MSG2(DEBUG_VCODEC2,T("inter_block[%i] %i"), code, level );
  781. }
  782. dec->bitpos = bitpos;
  783. return len;
  784. }
  785. static void blockInter( mpeg_decode* dec, int mb_type, int pos )
  786. {
  787. int j;
  788. idct* IDCT = dec->Codec.IDCT.Ptr;
  789.   IDCT->Process(IDCT,POSX(pos),POSY(pos));
  790. IDCT->MComp16x16(IDCT,
  791. dec->fmv[1]==NOMV?NULL:dec->fmv+1,
  792. dec->bmv[1]==NOMV?NULL:dec->bmv+1);
  793. if (mb_type & MB_PAT)
  794. {
  795. int len,cbp;
  796. loadbits(dec);
  797. len = showbits(dec,9);
  798. if (len >= 128)
  799. len >>= 4;
  800. else
  801. len += 32;
  802. len <<= 1;
  803. flushbits(dec,mcbp_p[len]);
  804. cbp = mcbp_p[len+1];
  805. for (j = 0; j < 6; j++, cbp+=cbp)
  806. {
  807. len = 0;
  808. if (cbp & 32)
  809. len = decodeInter(dec);
  810. IDCT->Inter8x8(IDCT,dec->blockptr,len);
  811. }
  812. }
  813. else
  814. {
  815. for (j = 0; j < 6; j++)
  816. IDCT->Inter8x8(IDCT,dec->blockptr,0);
  817. }
  818. dec->last_dc[2] =
  819. dec->last_dc[1] =
  820. dec->last_dc[0] = 128;
  821. }
  822. static int IVOP_Slice( mpeg_decode* dec, int pos )
  823. {
  824. dec->lastrefframe = dec->Frame;
  825. dec->mapofs = dec->Frame;
  826. memset(dec->framemap,0,dec->pos_end); // set all block to current frame
  827. for (;pos<dec->pos_end;pos+=MB_X-dec->mb_xsize) 
  828. {
  829. for (;POSX(pos)<dec->mb_xsize;++pos) 
  830. {
  831. if (!getbits1(dec))
  832. {
  833. flushbits(dec,1); // should be 1
  834.             dec->qscale = readqscale(dec);
  835. }
  836. blockIntra(dec, pos);
  837. inlineloadbits(dec);
  838. if (showbits(dec,8)==0) // eof slice
  839. return ERR_NONE;
  840. if (!getbits1(dec))
  841. return ERR_NONE; // skip invalid with IVOP
  842. }
  843. }
  844. return ERR_NONE;
  845. }
  846. static int PVOP_Slice( mpeg_decode* dec, int pos )
  847. {
  848. dec->currframemap = (dec->Frame - dec->mapofs) << 1;
  849. dec->lastrefframe = dec->Frame;
  850. dec->bmv[1] = NOMV;
  851. for (;pos<dec->pos_end;pos+=MB_X-dec->mb_xsize) 
  852. {
  853. for (;POSX(pos)<dec->mb_xsize;++pos) 
  854. {
  855. if (!dec->skip)
  856. {
  857. int mb_type;
  858. DEBUG_MSG3(DEBUG_VCODEC2,T("macro %d,%d %08x"),POSX(pos),POSY(pos),showbitslarge((loadbits(dec),dec),32));
  859. dec->framemap[pos] = (uint8_t)dec->currframemap;
  860. mb_type = mb_type_p[showbits(dec,6)];
  861. flushbits(dec,mb_type >> 5); // after this 6bits left
  862. if (mb_type & MB_QUANT)
  863.             dec->qscale = readqscale(dec);
  864. if (mb_type & MB_INTRA)
  865. blockIntra(dec, pos);
  866. else
  867. {
  868. if (mb_type & MB_FOR)
  869. GetMV(dec->fcode[0],dec->fmv,dec->full_pixel[0],dec);
  870. else
  871. dec->fmv[6] = dec->fmv[5] = dec->fmv[4] = dec->fmv[3] = 
  872. dec->fmv[2] = dec->fmv[1] = dec->fmv[0] = 0;
  873. DEBUG_MSG3(DEBUG_VCODEC2,T("mv%d %d:%d"),0,MVX(dec->fmv[1]),MVY(dec->fmv[1]));
  874. blockInter( dec, mb_type, pos );
  875. }
  876. inlineloadbits(dec);
  877. if (showbits(dec,8)==0) // eof slice
  878. return ERR_NONE;
  879. readskip(dec); // after this 12bits left
  880. }
  881. else
  882. {
  883. int n;
  884. dec->skip--;
  885. // not coded macroblock
  886. n = dec->framemap[pos];
  887. // copy needed or the buffer already has this block?
  888. if (dec->bufframe < dec->mapofs+(n>>1))
  889. dec->Codec.IDCT.Ptr->Copy16x16(dec->Codec.IDCT.Ptr,POSX(pos),POSY(pos),0);
  890. dec->fmv[0] = 0;
  891. dec->last_dc[2] =
  892. dec->last_dc[1] =
  893. dec->last_dc[0] = 128;
  894. }
  895. }
  896. }
  897. return ERR_NONE;
  898. }
  899. static int BVOP_Slice( mpeg_decode* dec, int pos )
  900. {
  901. for (;pos<dec->pos_end;pos+=MB_X-dec->mb_xsize) 
  902. {
  903. for (;POSX(pos)<dec->mb_xsize;++pos) 
  904. {
  905. if (!dec->skip)
  906. {
  907. int mb_type;
  908. DEBUG_MSG3(DEBUG_VCODEC2,T("macro %d,%d %08x"),POSX(pos),POSY(pos),showbitslarge((loadbits(dec),dec),32));
  909. //dec->framemap[pos] |= 1;
  910. mb_type = mb_type_b[showbits(dec,6)];
  911. flushbits(dec,mb_type >> 5); // after this 6bits left
  912. if (mb_type & MB_QUANT)
  913.             dec->qscale = readqscale(dec);
  914. if (mb_type & MB_INTRA)
  915. blockIntra(dec, pos);
  916. else
  917. {
  918. if (mb_type & MB_FOR)
  919. {
  920. GetMV(dec->fcode[0],dec->fmv,dec->full_pixel[0],dec);
  921. DEBUG_MSG3(DEBUG_VCODEC2,T("mv%d %d:%d"),0,MVX(dec->fmv[1]),MVY(dec->fmv[1]));
  922. }
  923. else
  924. dec->fmv[1] = NOMV;
  925. if (mb_type & MB_BACK)
  926. {
  927. GetMV(dec->fcode[1],dec->bmv,dec->full_pixel[1],dec);
  928. DEBUG_MSG3(DEBUG_VCODEC2,T("mv%d %d:%d"),1,MVX(dec->bmv[1]),MVY(dec->bmv[1]));
  929. }
  930. else
  931. dec->bmv[1] = NOMV;
  932. blockInter( dec, mb_type, pos );
  933. }
  934. inlineloadbits(dec);
  935. if (showbits(dec,8)==0) // eof slice
  936. return ERR_NONE;
  937. readskip(dec); // after this 12bits left
  938. }
  939. else
  940. {
  941. // not coded macroblock, use last motion compensation vectors
  942. dec->skip--;
  943. if (dec->fmv[1]==0 && dec->bmv[1]==NOMV)
  944. {
  945. dec->Codec.IDCT.Ptr->Copy16x16(dec->Codec.IDCT.Ptr,POSX(pos),POSY(pos),0);
  946. dec->last_dc[2] =
  947. dec->last_dc[1] =
  948. dec->last_dc[0] = 128;
  949. }
  950. else
  951. if (dec->bmv[1]==0 && dec->fmv[1]==NOMV)
  952. {
  953. dec->Codec.IDCT.Ptr->Copy16x16(dec->Codec.IDCT.Ptr,POSX(pos),POSY(pos),1);
  954. dec->last_dc[2] =
  955. dec->last_dc[1] =
  956. dec->last_dc[0] = 128;
  957. }
  958. else
  959. blockInter(dec, 0, pos );
  960. }
  961. }
  962. }
  963. return ERR_NONE;
  964. }
  965. static int Frame( mpeg_decode* dec, const uint8_t* Ptr, int Len )
  966. {
  967. int Result;
  968. int Code;
  969. if (Len == 0)
  970. return ERR_INVALID_DATA;
  971. initbits(dec,Ptr,Len);
  972. Result = ERR_NONE;
  973. DEBUG_MSG1(DEBUG_VCODEC,T("MPEG Frame Length:%d"),Len);
  974. dec->frame_state = -1;
  975. do
  976. {
  977. bytealign(dec);
  978. loadbits(dec);
  979. if (eofbits(dec))
  980. break;
  981. Code = showbits(dec,32);
  982. if ((Code & ~0xFF)==0x100)
  983. {
  984. flushbits(dec,32);
  985. if (Code >= SLICE_MIN_START_CODE && Code <= SLICE_MAX_START_CODE)
  986. {
  987. // found a slice
  988. Code = (Code-1) & 0xFF;
  989. if (Code >= dec->mb_ysize) 
  990. {
  991. Result = ERR_INVALID_DATA;
  992. break;
  993. }
  994. if (dec->frame_state)
  995. {
  996. if (dec->frame_state<0 || dec->Codec.IDCT.Count<2)
  997. {
  998. Result = ERR_INVALID_DATA; // no picture header
  999. break;
  1000. }
  1001. Result = StartFrame(dec);
  1002. if (Result != ERR_NONE || dec->Codec.Show < 0)
  1003. break;
  1004. dec->frame_state = 0;
  1005. }
  1006. loadbits(dec);
  1007. dec->qscale = readqscale(dec);
  1008. while (getbits1(dec)) 
  1009. {
  1010. flushbits(dec,8);
  1011. loadbits(dec);
  1012. }
  1013. readskip(dec);
  1014. Code = MB_X*Code + dec->skip;
  1015. dec->skip = 0;
  1016. dec->last_dc[2] =
  1017. dec->last_dc[1] =
  1018. dec->last_dc[0] = 128;
  1019. dec->fmv[0] = dec->bmv[0] = 0;
  1020. loadbits(dec);
  1021. switch (dec->prediction_type) 
  1022. {
  1023. case P_VOP:
  1024. Result = PVOP_Slice(dec,Code);
  1025. break;
  1026. case I_VOP:
  1027. Result = IVOP_Slice(dec,Code);
  1028. break;
  1029. case B_VOP:
  1030. Result = BVOP_Slice(dec,Code);
  1031. break;
  1032. }
  1033. }
  1034. else
  1035. if (Code == PICTURE_START_CODE)
  1036. {
  1037. if (dec->ValidSeq)
  1038. Picture(dec);
  1039. else
  1040. Result = ERR_INVALID_DATA;
  1041. }
  1042. else
  1043. if (Code == SEQ_START_CODE)
  1044. Sequence(dec);
  1045. else
  1046. if (Code == EXT_START_CODE)
  1047. {
  1048. if (!dec->ErrorShowed)
  1049. {
  1050. pin Pin;
  1051. Pin.No = CODECIDCT_INPUT;
  1052. Pin.Node = &dec->Codec.Node;
  1053. dec->ErrorShowed = 1;
  1054. if (!dec->Codec.NotSupported.Node || 
  1055. dec->Codec.NotSupported.Node->Set(dec->Codec.NotSupported.Node,dec->Codec.NotSupported.No,
  1056. &Pin,sizeof(pin))!=ERR_NONE)
  1057. ShowError(dec->Codec.Node.Class,MPEG1_ID,MPEG2_NOT_SUPPORTED);
  1058. else
  1059. Result = ERR_INVALID_DATA;
  1060. }
  1061. }
  1062. }
  1063. else
  1064. flushbits(dec,8);
  1065. }
  1066. while (Result == ERR_NONE);
  1067. if (dec->frame_state==0)
  1068. {
  1069. dec->Codec.IDCT.Ptr->FrameEnd(dec->Codec.IDCT.Ptr);
  1070. dec->Frame++;
  1071. // possible (uint8) framemap overflow?
  1072. if ((dec->Frame - dec->mapofs) >= 128) 
  1073. {
  1074. int pos;
  1075. for (pos=0;pos<dec->pos_end;pos+=MB_X-dec->mb_xsize)
  1076. for (;POSX(pos)<dec->mb_xsize;++pos)
  1077. {
  1078. int i = dec->framemap[pos];
  1079. if (i >= (120<<1))
  1080. i -= 120<<1;
  1081. else
  1082. i &= 1;
  1083. dec->framemap[pos] = (uint8_t)i;
  1084. }
  1085. dec->mapofs += 120;
  1086. }
  1087. }
  1088. return Result;
  1089. }
  1090. static int Create( mpeg_decode* p )
  1091. {
  1092. p->Codec.MinCount = 2;
  1093. p->Codec.DefCount = 3; // default assuming B-frames (avoid reinit when reaching first B-frame)
  1094. p->Codec.FindNext = (codecidctnext)FindNext;
  1095. p->Codec.Frame = (codecidctframe)Frame;
  1096. p->Codec.UpdateSize = (nodefunc)UpdateSize;
  1097. p->Codec.UpdateInput = (nodefunc)UpdateInput;
  1098. p->Codec.Discontinuity = (nodefunc)Discontinuity;
  1099. p->Codec.Flush = (nodefunc)Flush;
  1100. return ERR_NONE;
  1101. }
  1102. static const nodedef MPEG1 =
  1103. {
  1104. sizeof(mpeg_decode),
  1105. MPEG1_ID,
  1106. CODECIDCT_CLASS,
  1107. PRI_DEFAULT,
  1108. (nodecreate)Create,
  1109. NULL,
  1110. };
  1111. void MPEG1_Init()
  1112. {
  1113. NodeRegisterClass(&MPEG1);
  1114. }
  1115. void MPEG1_Done()
  1116. {
  1117. NodeUnRegisterClass(MPEG1_ID);
  1118. }