JXRTranscode.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991
  1. //*@@@+++@@@@******************************************************************
  2. //
  3. // Copyright © Microsoft Corp.
  4. // All rights reserved.
  5. //
  6. // Redistribution and use in source and binary forms, with or without
  7. // modification, are permitted provided that the following conditions are met:
  8. //
  9. // • Redistributions of source code must retain the above copyright notice,
  10. // this list of conditions and the following disclaimer.
  11. // • Redistributions in binary form must reproduce the above copyright notice,
  12. // this list of conditions and the following disclaimer in the documentation
  13. // and/or other materials provided with the distribution.
  14. //
  15. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  19. // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  20. // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  21. // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  22. // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  23. // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  24. // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  25. // POSSIBILITY OF SUCH DAMAGE.
  26. //
  27. //*@@@---@@@@******************************************************************
  28. #include "windowsmediaphoto.h"
  29. #include "strcodec.h"
  30. #include "decode.h"
  31. EXTERN_C Void freePredInfo(CWMImageStrCodec *);
  32. EXTERN_C Int ReadWMIHeader(CWMImageInfo *, CWMIStrCodecParam *, CCoreParameters *);
  33. EXTERN_C Int StrIODecInit(CWMImageStrCodec *);
  34. EXTERN_C Int StrDecInit(CWMImageStrCodec *);
  35. EXTERN_C Int readPackets(CWMImageStrCodec *);
  36. EXTERN_C Int DecodeMacroblockDC(CWMImageStrCodec *, CCodingContext *, Int, Int);
  37. EXTERN_C Int DecodeMacroblockLowpass(CWMImageStrCodec *, CCodingContext *, Int, Int);
  38. EXTERN_C Int DecodeMacroblockHighpass(CWMImageStrCodec *, CCodingContext *, Int, Int);
  39. EXTERN_C Void predDCACDec(CWMImageStrCodec *);
  40. EXTERN_C Void predACDec(CWMImageStrCodec *);
  41. EXTERN_C Void StrIODecTerm(CWMImageStrCodec *);
  42. EXTERN_C Void FreeCodingContextDec(CWMImageStrCodec *);
  43. EXTERN_C Int StrEncInit(CWMImageStrCodec *);
  44. EXTERN_C Void StrIOEncTerm(CWMImageStrCodec *);
  45. EXTERN_C Void FreeCodingContextEnc(CWMImageStrCodec *);
  46. EXTERN_C Int encodeMB(CWMImageStrCodec *, Int, Int);
  47. EXTERN_C Int writeIndexTableNull(CWMImageStrCodec *);
  48. EXTERN_C Void writePacketHeader(BitIOInfo *, U8, U8);
  49. EXTERN_C Int WriteWMIHeader(CWMImageStrCodec *);
  50. EXTERN_C Int ReadImagePlaneHeader(CWMImageInfo *, CWMIStrCodecParam *, CCoreParameters *, SimpleBitIO *);
  51. EXTERN_C Int WriteImagePlaneHeader(CWMImageStrCodec *);
  52. EXTERN_C Int writeIndexTable(CWMImageStrCodec *);
  53. EXTERN_C Int copyTo(struct WMPStream *, struct WMPStream *, size_t);
  54. const static Bool bFlipV[O_MAX] = {FALSE, TRUE , FALSE, TRUE, TRUE , TRUE, FALSE, FALSE};
  55. const static Bool bFlipH[O_MAX] = {FALSE, FALSE, TRUE , TRUE, FALSE, TRUE, FALSE, TRUE};
  56. typedef struct CTileQPInfo
  57. {
  58. U8 dcMode;
  59. U8 dcIndex[MAX_CHANNELS];
  60. Bool bUseDC;
  61. U8 lpNum;
  62. Bool bUseDCAlpha;
  63. U8 lpNumAlpha;
  64. U8 lpMode[16];
  65. U8 lpIndex[16][MAX_CHANNELS];
  66. Bool bUseLP;
  67. U8 hpNum;
  68. Bool bUseLPAlpha;
  69. U8 hpNumAlpha;
  70. U8 hpMode[16];
  71. U8 hpIndex[16][MAX_CHANNELS];
  72. } CTileQPInfo;
  73. Void transcodeQuantizer(BitIOInfo * pIO, U8 cIndex[MAX_CHANNELS], U8 cChMode, size_t cChannel)
  74. {
  75. if(cChMode > 2)
  76. cChMode = 2;
  77. if(cChannel > 1)
  78. putBit16(pIO, cChMode, 2); // Channel mode
  79. else
  80. cChMode = 0;
  81. putBit16(pIO, cIndex[0], 8); // Y
  82. if(cChMode == 1) // MIXED
  83. putBit16(pIO, cIndex[1], 8); // UV
  84. else if(cChMode > 0){ // INDEPENDENT
  85. size_t i;
  86. for(i = 1; i < cChannel; i ++)
  87. putBit16(pIO, cIndex[i], 8); // UV
  88. }
  89. }
  90. Void transcodeQuantizers(BitIOInfo * pIO, U8 cIndex[16][MAX_CHANNELS], U8 cChMode[16], U32 cNum, size_t cChannel, Bool bCopy)
  91. {
  92. putBit16(pIO, bCopy == TRUE ? 1 : 0, 1);
  93. if(bCopy == FALSE){
  94. U32 i;
  95. putBit16(pIO, cNum - 1, 4);
  96. for(i = 0; i < cNum; i ++)
  97. transcodeQuantizer(pIO, cIndex[i], cChMode[i], cChannel);
  98. }
  99. }
  100. Void transcodeQuantizersAlpha(BitIOInfo * pIO, U8 cIndex[16][MAX_CHANNELS], U32 cNum, size_t iChannel, Bool bCopy)
  101. {
  102. putBit16(pIO, bCopy == TRUE ? 1 : 0, 1);
  103. if(bCopy == FALSE){
  104. U32 i;
  105. putBit16(pIO, cNum - 1, 4);
  106. for(i = 0; i < cNum; i ++)
  107. putBit16(pIO, cIndex[i][iChannel], 8);
  108. }
  109. }
  110. Void transcodeTileHeader(CWMImageStrCodec * pSC, CTileQPInfo * pTileQPInfo)
  111. {
  112. if(pSC->m_bCtxLeft && pSC->m_bCtxTop && pSC->m_bSecondary == FALSE){ // write packet headers
  113. CCodingContext * pContext = &pSC->m_pCodingContext[pSC->cTileColumn];
  114. CWMITile * pTile = pSC->pTile + pSC->cTileColumn;
  115. U8 pID = (U8)((pSC->cTileRow * (pSC->WMISCP.cNumOfSliceMinus1V + 1) + pSC->cTileColumn) & 0x1F);
  116. CWMImageStrCodec * pSCAlpha = (pSC->m_param.bAlphaChannel ? pSC->m_pNextSC : NULL);
  117. const size_t iAlphaPos = pSC->m_param.cNumChannels;
  118. writePacketHeader(pContext->m_pIODC, pSC->WMISCP.bfBitstreamFormat == SPATIAL ? 0 : 1, pID);
  119. if (pSC->m_param.bTrimFlexbitsFlag && pSC->WMISCP.bfBitstreamFormat == SPATIAL)
  120. putBit16(pContext->m_pIODC, pContext->m_iTrimFlexBits, 4);
  121. if((pSC->m_param.uQPMode & 1) != 0) // not DC uniform
  122. transcodeQuantizer(pContext->m_pIODC, pTileQPInfo->dcIndex, pTileQPInfo->dcMode, pSC->WMISCP.cChannel);
  123. if(pSCAlpha != NULL && (pSCAlpha->m_param.uQPMode & 1) != 0) // not DC uniform
  124. putBit16(pContext->m_pIODC, pTileQPInfo->dcIndex[iAlphaPos], 8);
  125. if(pSC->WMISCP.bfBitstreamFormat == SPATIAL) {
  126. if(pSC->WMISCP.sbSubband != SB_DC_ONLY){
  127. if((pSC->m_param.uQPMode & 2) != 0) // not LP uniform
  128. transcodeQuantizers(pContext->m_pIODC, pTileQPInfo->lpIndex, pTileQPInfo->lpMode, pTileQPInfo->lpNum, pSC->WMISCP.cChannel, pTileQPInfo->bUseDC);
  129. if(pSCAlpha != NULL && (pSCAlpha->m_param.uQPMode & 2) != 0) // not LP uniform
  130. transcodeQuantizersAlpha(pContext->m_pIODC, pTileQPInfo->lpIndex, pTileQPInfo->lpNumAlpha, iAlphaPos, pTileQPInfo->bUseDCAlpha);
  131. if(pSC->WMISCP.sbSubband != SB_NO_HIGHPASS){
  132. if((pSC->m_param.uQPMode & 4) != 0) // not HP uniform
  133. transcodeQuantizers(pContext->m_pIODC, pTileQPInfo->hpIndex, pTileQPInfo->hpMode, pTileQPInfo->hpNum, pSC->WMISCP.cChannel, pTileQPInfo->bUseLP);
  134. if(pSCAlpha != NULL && (pSCAlpha->m_param.uQPMode & 4) != 0) // not HP uniform
  135. transcodeQuantizersAlpha(pContext->m_pIODC, pTileQPInfo->hpIndex, pTileQPInfo->hpNumAlpha, iAlphaPos, pTileQPInfo->bUseLPAlpha);
  136. }
  137. }
  138. }
  139. else{
  140. if(pSC->WMISCP.sbSubband != SB_DC_ONLY){
  141. writePacketHeader(pContext->m_pIOLP, 2, pID);
  142. if((pSC->m_param.uQPMode & 2) != 0) // not LP uniform
  143. transcodeQuantizers(pContext->m_pIOLP, pTileQPInfo->lpIndex, pTileQPInfo->lpMode, pTileQPInfo->lpNum, pSC->WMISCP.cChannel, pTileQPInfo->bUseDC);
  144. if(pSCAlpha != NULL && (pSCAlpha->m_param.uQPMode & 2) != 0) // not LP uniform
  145. transcodeQuantizersAlpha(pContext->m_pIOLP, pTileQPInfo->lpIndex, pTileQPInfo->lpNumAlpha, iAlphaPos, pTileQPInfo->bUseDCAlpha);
  146. if(pSC->WMISCP.sbSubband != SB_NO_HIGHPASS){
  147. writePacketHeader(pContext->m_pIOAC, 3, pID);
  148. if((pSC->m_param.uQPMode & 4) != 0) // not HP uniform
  149. transcodeQuantizers(pContext->m_pIOAC, pTileQPInfo->hpIndex, pTileQPInfo->hpMode, pTileQPInfo->hpNum, pSC->WMISCP.cChannel, pTileQPInfo->bUseLP);
  150. if(pSCAlpha != NULL && (pSCAlpha->m_param.uQPMode & 4) != 0) // not HP uniform
  151. transcodeQuantizersAlpha(pContext->m_pIOAC, pTileQPInfo->hpIndex, pTileQPInfo->hpNumAlpha, iAlphaPos, pTileQPInfo->bUseLPAlpha);
  152. if(pSC->WMISCP.sbSubband != SB_NO_FLEXBITS){
  153. writePacketHeader(pContext->m_pIOFL, 4, pID);
  154. if (pSC->m_param.bTrimFlexbitsFlag)
  155. putBit16(pContext->m_pIOFL, pContext->m_iTrimFlexBits, 4);
  156. }
  157. }
  158. }
  159. }
  160. pTile->cBitsLP = (pTileQPInfo->bUseDC ? 0 : dquantBits(pTileQPInfo->lpNum));
  161. pTile->cBitsHP = (pTileQPInfo->bUseLP ? 0 : dquantBits(pTileQPInfo->hpNum));
  162. if(pSCAlpha != NULL){
  163. pTile = pSCAlpha->pTile + pSC->cTileColumn;
  164. pTile->cBitsLP = (pTileQPInfo->bUseDCAlpha ? 0 : dquantBits(pTileQPInfo->lpNumAlpha));
  165. pTile->cBitsHP = (pTileQPInfo->bUseLPAlpha ? 0 : dquantBits(pTileQPInfo->hpNumAlpha));
  166. }
  167. }
  168. }
  169. Void transformDCBlock(PixelI * pOrg, PixelI * pDst, ORIENTATION oOrientation)
  170. {
  171. size_t i;
  172. if(bFlipV[oOrientation])
  173. for(i = 0; i < 16; i += 4)
  174. pOrg[i + 1] = -pOrg[i + 1], pOrg[i + 3] = -pOrg[i + 3];
  175. if(bFlipH[oOrientation])
  176. for(i = 0; i < 4; i ++)
  177. pOrg[i + 4] = -pOrg[i + 4], pOrg[i + 12] = -pOrg[i + 12];
  178. if(oOrientation < O_RCW)
  179. memcpy(pDst, pOrg, 16 * sizeof(PixelI));
  180. else
  181. for(i = 0; i < 16; i ++)
  182. pDst[i] = pOrg[(i >> 2) + ((i & 3) << 2)];
  183. }
  184. Void transformDCBlock422(PixelI * pOrg, PixelI * pDst, ORIENTATION oOrientation)
  185. {
  186. assert(oOrientation < O_RCW);
  187. if(bFlipV[oOrientation])
  188. pOrg[1] = -pOrg[1], pOrg[3] = -pOrg[3], pOrg[4] = -pOrg[4], pOrg[5] = -pOrg[5], pOrg[7] = -pOrg[7];
  189. if(bFlipH[oOrientation])
  190. pOrg[2] = -pOrg[2], pOrg[3] = -pOrg[3], pOrg[6] = -pOrg[6], pOrg[7] = -pOrg[7];
  191. if(bFlipV[oOrientation])
  192. pDst[0] = pOrg[0], pDst[1] = pOrg[5], pDst[2] = pOrg[6], pDst[3] = pOrg[7], pDst[4] = pOrg[4], pDst[5] = pOrg[1], pDst[6] = pOrg[2], pDst[7] = pOrg[3];
  193. else
  194. memcpy(pDst, pOrg, 8 * sizeof(PixelI));
  195. }
  196. Void transformDCBlock420(PixelI * pOrg, PixelI * pDst, ORIENTATION oOrientation)
  197. {
  198. if(bFlipV[oOrientation])
  199. pOrg[1] = -pOrg[1], pOrg[3] = -pOrg[3];
  200. if(bFlipH[oOrientation])
  201. pOrg[2] = -pOrg[2], pOrg[3] = -pOrg[3];
  202. pDst[0] = pOrg[0], pDst[3] = pOrg[3];
  203. if(oOrientation < O_RCW)
  204. pDst[1] = pOrg[1], pDst[2] = pOrg[2];
  205. else
  206. pDst[1] = pOrg[2], pDst[2] = pOrg[1];
  207. }
  208. Void transformACBlocks(PixelI * pOrg, PixelI * pDst, ORIENTATION oOrientation)
  209. {
  210. PixelI * pO, * pD;
  211. const Int * pT = dctIndex[0];
  212. size_t i, j, k;
  213. for(j = 0, pO = pOrg; j < 16; j ++, pO += 16){
  214. if(bFlipV[oOrientation])
  215. for(i = 0; i < 16; i += 4)
  216. pO[pT[i + 1]] = -pO[pT[i + 1]], pO[pT[i + 3]] = -pO[pT[i + 3]];
  217. if(bFlipH[oOrientation])
  218. for(i = 0; i < 4; i ++)
  219. pO[pT[i + 4]] = -pO[pT[i + 4]], pO[pT[i + 12]] = -pO[pT[i + 12]];
  220. }
  221. for(j = 0; j < 4; j ++)
  222. for(i = 0; i < 4; i ++){
  223. size_t ii = (bFlipV[oOrientation] ? 3 - i : i);
  224. size_t jj = (bFlipH[oOrientation] ? 3 - j : j);
  225. if(oOrientation < O_RCW)
  226. memcpy(pDst + (jj * 4 + ii) * 16, pOrg + (j * 4 + i) * 16, 16 * sizeof(PixelI));
  227. else{
  228. pO = pOrg + (j * 4 + i) * 16;
  229. pD = pDst + (ii * 4 + jj) * 16;
  230. for(k = 1; k < 16; k ++)
  231. pD[pT[k]] = pO[pT[(k >> 2) + ((k & 3) << 2)]];
  232. }
  233. }
  234. }
  235. Void transformACBlocks422(PixelI * pOrg, PixelI * pDst, ORIENTATION oOrientation)
  236. {
  237. PixelI * pO;
  238. const Int * pT = dctIndex[0];
  239. size_t i, j;
  240. assert(oOrientation < O_RCW);
  241. for(j = 0, pO = pOrg; j < 8; j ++, pO += 16){
  242. if(bFlipV[oOrientation])
  243. for(i = 0; i < 16; i += 4)
  244. pO[pT[i + 1]] = -pO[pT[i + 1]], pO[pT[i + 3]] = -pO[pT[i + 3]];
  245. if(bFlipH[oOrientation])
  246. for(i = 0; i < 4; i ++)
  247. pO[pT[i + 4]] = -pO[pT[i + 4]], pO[pT[i + 12]] = -pO[pT[i + 12]];
  248. }
  249. for(j = 0; j < 2; j ++)
  250. for(i = 0; i < 4; i ++){
  251. size_t ii = (bFlipV[oOrientation] ? 3 - i : i);
  252. size_t jj = (bFlipH[oOrientation] ? 1 - j : j);
  253. memcpy(pDst + (jj * 4 + ii) * 16, pOrg + (j * 4 + i) * 16, 16 * sizeof(PixelI));
  254. }
  255. }
  256. Void transformACBlocks420(PixelI * pOrg, PixelI * pDst, ORIENTATION oOrientation)
  257. {
  258. PixelI * pO, * pD;
  259. const Int * pT = dctIndex[0];
  260. size_t i, j, k;
  261. for(j = 0, pO = pOrg; j < 4; j ++, pO += 16){
  262. if(bFlipV[oOrientation])
  263. for(i = 0; i < 16; i += 4)
  264. pO[pT[i + 1]] = -pO[pT[i + 1]], pO[pT[i + 3]] = -pO[pT[i + 3]];
  265. if(bFlipH[oOrientation])
  266. for(i = 0; i < 4; i ++)
  267. pO[pT[i + 4]] = -pO[pT[i + 4]], pO[pT[i + 12]] = -pO[pT[i + 12]];
  268. }
  269. for(j = 0; j < 2; j ++)
  270. for(i = 0; i < 2; i ++){
  271. size_t ii = (bFlipV[oOrientation] ? 1 - i : i);
  272. size_t jj = (bFlipH[oOrientation] ? 1 - j : j);
  273. if(oOrientation < O_RCW)
  274. memcpy(pDst + (jj * 2 + ii) * 16, pOrg + (j * 2 + i) * 16, 16 * sizeof(PixelI));
  275. else{
  276. pO = pOrg + (j * 2 + i) * 16;
  277. pD = pDst + (ii * 2 + jj) * 16;
  278. for(k = 1; k < 16; k ++)
  279. pD[pT[k]] = pO[pT[(k >> 2) + ((k & 3) << 2)]];
  280. }
  281. }
  282. }
  283. Int getROI(CWMImageInfo * pII, CCoreParameters * pCore, CWMIStrCodecParam * pSCP, CWMTranscodingParam * pParam)
  284. {
  285. const ORIENTATION oO = pParam->oOrientation;
  286. size_t iLeft, iTop, cWidth, cHeight, i, j;
  287. size_t mbLeft, mbRight, mbTop, mbBottom;
  288. size_t * iTile = (size_t *)malloc(MAX_TILES * sizeof(size_t));
  289. if(iTile == NULL)
  290. return ICERR_ERROR;
  291. if(pParam->cLeftX + pParam->cWidth > pII->cWidth || pParam->cTopY + pParam->cHeight > pII->cHeight) // invalid region
  292. return ICERR_ERROR;
  293. cWidth = pParam->cWidth, cHeight = pParam->cHeight;
  294. iLeft = pParam->cLeftX + pCore->cExtraPixelsLeft, iTop = pParam->cTopY + pCore->cExtraPixelsTop;
  295. if(pSCP->olOverlap != OL_NONE && pParam->bIgnoreOverlap == FALSE){ // include pixels borrowed
  296. size_t cBlurred = (pSCP->olOverlap == OL_TWO ? 10 : 2);
  297. if(iLeft > cBlurred)
  298. iLeft -= cBlurred, cWidth += cBlurred;
  299. else
  300. cWidth += iLeft, iLeft = 0;
  301. if(iTop > cBlurred)
  302. iTop -= cBlurred, cHeight += cBlurred;
  303. else
  304. cHeight += iTop, iTop = 0;
  305. cWidth += cBlurred, cHeight += cBlurred;
  306. if(iLeft + cWidth > pII->cWidth + pCore->cExtraPixelsLeft + pCore->cExtraPixelsRight)
  307. cWidth = pII->cWidth + pCore->cExtraPixelsLeft + pCore->cExtraPixelsRight - iLeft;
  308. if(iTop + cHeight > pII->cHeight + pCore->cExtraPixelsTop + pCore->cExtraPixelsBottom)
  309. cHeight = pII->cHeight + pCore->cExtraPixelsTop + pCore->cExtraPixelsBottom - iTop;
  310. }
  311. mbTop = (iTop >> 4), mbLeft = (iLeft >> 4);
  312. mbBottom = (iTop + cHeight + 15) >> 4, mbRight = (iLeft + cWidth + 15) >> 4;
  313. pCore->cExtraPixelsLeft += pParam->cLeftX - (mbLeft << 4);
  314. pCore->cExtraPixelsRight = ((mbRight - mbLeft) << 4) - pParam->cWidth - pCore->cExtraPixelsLeft;
  315. pCore->cExtraPixelsTop += pParam->cTopY - (mbTop << 4);
  316. pCore->cExtraPixelsBottom = ((mbBottom - mbTop) << 4) - pParam->cHeight - pCore->cExtraPixelsTop;
  317. pII->cWidth = ((mbRight - mbLeft) << 4) - pCore->cExtraPixelsLeft - pCore->cExtraPixelsRight;
  318. pII->cHeight = ((mbBottom - mbTop) << 4) - pCore->cExtraPixelsTop - pCore->cExtraPixelsBottom;
  319. pParam->cLeftX = iLeft, pParam->cTopY = iTop;
  320. pParam->cWidth = cWidth, pParam->cHeight = cHeight;
  321. // extra pixels in transformed space
  322. #define SWAP(a, b) i = a, a = b, b = i
  323. if(oO == O_FLIPH || oO == O_FLIPVH || oO == O_RCW_FLIPV || oO == O_RCW_FLIPVH)
  324. SWAP(pCore->cExtraPixelsLeft, pCore->cExtraPixelsRight);
  325. if(oO == O_FLIPV || oO == O_FLIPVH || oO == O_RCW || oO == O_RCW_FLIPV)
  326. SWAP(pCore->cExtraPixelsTop, pCore->cExtraPixelsBottom);
  327. if(oO >= O_RCW){
  328. SWAP(pCore->cExtraPixelsLeft, pCore->cExtraPixelsTop);
  329. SWAP(pCore->cExtraPixelsRight, pCore->cExtraPixelsBottom);
  330. }
  331. // adjust tiling
  332. for(i = 0, j = 0, iTile[0] = 0; i <= (size_t)pSCP->cNumOfSliceMinus1V; i ++)
  333. if((size_t)pSCP->uiTileX[i] >= mbLeft && (size_t)pSCP->uiTileX[i] < mbRight){
  334. if(j >= MAX_TILES)
  335. j = MAX_TILES - 1;
  336. iTile[j] = (size_t)pSCP->uiTileX[i] - mbLeft, j ++;
  337. }
  338. if(iTile[0] == 0)
  339. for(i = 0, pSCP->cNumOfSliceMinus1V = (j == 0 ? 0 : (U32)(j - 1)); i < j; i ++)
  340. pSCP->uiTileX[i] = (U32)iTile[i];
  341. else
  342. for(i = 1, pSCP->uiTileX[0] = 0, pSCP->cNumOfSliceMinus1V = (U32)j; i <= j; i ++)
  343. pSCP->uiTileX[i] = (U32)iTile[i - 1];
  344. if(oO == O_FLIPH || oO == O_FLIPVH || oO == O_RCW_FLIPV || oO == O_RCW_FLIPVH){ // reverse order
  345. for(i = 0; i <= (size_t)pSCP->cNumOfSliceMinus1V; i ++)
  346. iTile[i] = mbRight - mbLeft - (size_t)pSCP->uiTileX[i];
  347. for(i = 1, pSCP->uiTileX[0] = 0; i <= (size_t)pSCP->cNumOfSliceMinus1V; i ++)
  348. pSCP->uiTileX[i] = (U32)(iTile[(size_t)pSCP->cNumOfSliceMinus1V - i + 1]);
  349. }
  350. for(i = 0, j = 0, iTile[0] = 0; i <= (size_t)pSCP->cNumOfSliceMinus1H; i ++)
  351. if(pSCP->uiTileY[i] >= mbTop && pSCP->uiTileY[i] < mbBottom){
  352. if(j >= MAX_TILES)
  353. j = MAX_TILES - 1;
  354. iTile[j] = (size_t)pSCP->uiTileY[i] - mbTop, j ++;
  355. }
  356. if(iTile[0] == 0)
  357. for(i = 0, pSCP->cNumOfSliceMinus1H = (j == 0 ? 0 : (U32)(j - 1)); i < j; i ++)
  358. pSCP->uiTileY[i] = (U32)iTile[i];
  359. else
  360. for(i = 1, pSCP->uiTileY[0] = 0, pSCP->cNumOfSliceMinus1H = (U32)j; i <= j; i ++)
  361. pSCP->uiTileY[i] = (U32)iTile[i - 1];
  362. if(oO == O_FLIPV || oO == O_FLIPVH || oO == O_RCW || oO == O_RCW_FLIPV){ // reverse order
  363. for(i = 0; i <= (size_t)pSCP->cNumOfSliceMinus1H; i ++)
  364. iTile[i] = mbBottom - mbTop - (size_t)pSCP->uiTileY[i];
  365. for(i = 1, pSCP->uiTileY[0] = 0; i <= (size_t)pSCP->cNumOfSliceMinus1H; i ++)
  366. pSCP->uiTileY[i] = (U32)(iTile[(size_t)pSCP->cNumOfSliceMinus1H - i + 1]);
  367. }
  368. if(oO >= O_RCW){ // switch X & Y
  369. for(i = 0; i <= (size_t)pSCP->cNumOfSliceMinus1V; i ++)
  370. iTile[i] = (size_t)pSCP->uiTileX[i];
  371. for(i = 0; i <= (size_t)pSCP->cNumOfSliceMinus1H; i ++)
  372. pSCP->uiTileX[i] = pSCP->uiTileY[i];
  373. for(i = 0; i <= (size_t)pSCP->cNumOfSliceMinus1V; i ++)
  374. pSCP->uiTileY[i] = (U32)iTile[i];
  375. i = (size_t)pSCP->cNumOfSliceMinus1H, pSCP->cNumOfSliceMinus1H = pSCP->cNumOfSliceMinus1V, pSCP->cNumOfSliceMinus1V = (U32)i;
  376. }
  377. free(iTile);
  378. return ICERR_OK;
  379. }
  380. Bool isTileBoundary(U32 * pTilePos, U32 cTiles, U32 cMBs, U32 iPos)
  381. {
  382. U32 i;
  383. for(i = 0; i < cTiles; i ++)
  384. if(iPos == pTilePos[i] * 16)
  385. break;
  386. return ((i < cTiles || (iPos + 15) / 16 >= cMBs) ? TRUE : FALSE);
  387. }
  388. Bool isTileExtraction(CWMImageStrCodec * pSC, CWMTranscodingParam * pParam)
  389. {
  390. if(pParam->bIgnoreOverlap == FALSE && pSC->WMISCP.olOverlap == OL_NONE)
  391. pParam->bIgnoreOverlap = TRUE;
  392. if(pParam->bIgnoreOverlap == TRUE && pParam->oOrientation == O_NONE && pParam->bfBitstreamFormat == pSC->WMISCP.bfBitstreamFormat){
  393. if(pParam->bfBitstreamFormat == SPATIAL && pParam->sbSubband != pSC->WMISCP.sbSubband)
  394. return FALSE;
  395. return (isTileBoundary(pSC->WMISCP.uiTileX, pSC->WMISCP.cNumOfSliceMinus1V + 1, (U32)pSC->cmbWidth, (U32)(pParam->cLeftX + pSC->m_param.cExtraPixelsLeft)) &&
  396. isTileBoundary(pSC->WMISCP.uiTileY, pSC->WMISCP.cNumOfSliceMinus1H + 1, (U32)pSC->cmbHeight, (U32)(pParam->cTopY + pSC->m_param.cExtraPixelsTop)) &&
  397. isTileBoundary(pSC->WMISCP.uiTileX, pSC->WMISCP.cNumOfSliceMinus1V + 1, (U32)pSC->cmbWidth, (U32)(pParam->cLeftX + pParam->cWidth + pSC->m_param.cExtraPixelsLeft)) &&
  398. isTileBoundary(pSC->WMISCP.uiTileY, pSC->WMISCP.cNumOfSliceMinus1H + 1, (U32)pSC->cmbHeight, (U32)(pParam->cTopY + pParam->cHeight + pSC->m_param.cExtraPixelsTop)));
  399. }
  400. return FALSE;
  401. }
  402. Int WMPhotoTranscode(struct WMPStream * pStreamIn, struct WMPStream * pStreamOut, CWMTranscodingParam * pParam)
  403. {
  404. PixelI * pMBBuf, MBBufAlpha[256]; // shared buffer, decoder <=> encoder bridge
  405. PixelI * pFrameBuf = NULL, * pFrameBufAlpha = NULL;
  406. CWMIMBInfo * pMBInfo = NULL, * pMBInfoAlpha = NULL;
  407. CWMImageStrCodec * pSCDec, * pSCEnc, * pSC;
  408. CWMDecoderParameters aDecoderParam = {0};
  409. U8 * pIOHeaderDec, * pIOHeaderEnc;
  410. CCodingContext * pContext;
  411. CTileQPInfo * pTileQPInfo = NULL;
  412. ORIENTATION oO = pParam->oOrientation;
  413. size_t iAlphaPos = 0;
  414. size_t cUnit;
  415. size_t i, j, mbLeft, mbRight, mbTop, mbBottom, mbWidth, mbHeight;
  416. if(pStreamIn == NULL || pStreamOut == NULL || pParam == NULL)
  417. return ICERR_ERROR;
  418. // initialize decoder
  419. if((pSCDec = (CWMImageStrCodec *)malloc(sizeof(CWMImageStrCodec))) == NULL)
  420. return ICERR_ERROR;
  421. memset(pSCDec, 0, sizeof(CWMImageStrCodec));
  422. pSCDec->WMISCP.pWStream = pStreamIn;
  423. if(ReadWMIHeader(&pSCDec->WMII, &pSCDec->WMISCP, &pSCDec->m_param) != ICERR_OK)
  424. return ICERR_ERROR;
  425. if(pSCDec->WMISCP.cfColorFormat == YUV_422 && oO >= O_RCW)
  426. pParam->oOrientation = oO = O_NONE; // Can not rotate 422 in compressed domain!
  427. pSCDec->cmbWidth = (pSCDec->WMII.cWidth + pSCDec->m_param.cExtraPixelsLeft + pSCDec->m_param.cExtraPixelsRight + 15) / 16;
  428. pSCDec->cmbHeight = (pSCDec->WMII.cHeight + pSCDec->m_param.cExtraPixelsTop + pSCDec->m_param.cExtraPixelsBottom + 15) / 16;
  429. pSCDec->m_param.cNumChannels = pSCDec->WMISCP.cChannel;
  430. pSCDec->m_Dparam = &aDecoderParam;
  431. pSCDec->m_Dparam->bSkipFlexbits = (pSCDec->WMISCP.sbSubband == SB_NO_FLEXBITS);
  432. pSCDec->m_param.bTranscode = TRUE;
  433. pParam->bIgnoreOverlap = isTileExtraction(pSCDec, pParam);
  434. cUnit = (pSCDec->m_param.cfColorFormat == YUV_420 ? 384 : (pSCDec->m_param.cfColorFormat == YUV_422 ? 512 : 256 * pSCDec->m_param.cNumChannels));
  435. if(cUnit > 256 * MAX_CHANNELS)
  436. return ICERR_ERROR;
  437. pSCDec->p1MBbuffer[0] = pMBBuf = (PixelI *)malloc(cUnit * sizeof(PixelI));
  438. if(pMBBuf == NULL)
  439. return ICERR_ERROR;
  440. pSCDec->p1MBbuffer[1] = pSCDec->p1MBbuffer[0] + 256;
  441. for(i = 2; i < pSCDec->m_param.cNumChannels; i ++)
  442. pSCDec->p1MBbuffer[i] = pSCDec->p1MBbuffer[i - 1] + (pSCDec->m_param.cfColorFormat == YUV_420 ? 64 : (pSCDec->m_param.cfColorFormat == YUV_422 ? 128 : 256));
  443. if(pSCDec->m_param.bAlphaChannel){ // alpha channel
  444. SimpleBitIO SB = {0};
  445. iAlphaPos = pSCDec->m_param.cNumChannels;
  446. if((pSCDec->m_pNextSC = (CWMImageStrCodec *)malloc(sizeof(CWMImageStrCodec))) == NULL)
  447. return ICERR_ERROR;
  448. *pSCDec->m_pNextSC = *pSCDec;
  449. pSCDec->m_pNextSC->p1MBbuffer[0] = MBBufAlpha;
  450. pSCDec->m_pNextSC->WMISCP.cfColorFormat = pSCDec->m_pNextSC->WMII.cfColorFormat = pSCDec->m_pNextSC->m_param.cfColorFormat = Y_ONLY;
  451. pSCDec->m_pNextSC->WMISCP.cChannel = pSCDec->m_pNextSC->m_param.cNumChannels = 1;
  452. pSCDec->m_pNextSC->m_bSecondary = TRUE;
  453. pSCDec->m_pNextSC->m_pNextSC = pSCDec;
  454. // read plane header of second image plane
  455. if(attach_SB(&SB, pSCDec->WMISCP.pWStream) != ICERR_OK)
  456. return ICERR_ERROR;
  457. ReadImagePlaneHeader(&pSCDec->m_pNextSC->WMII, &pSCDec->m_pNextSC->WMISCP, &pSCDec->m_pNextSC->m_param, &SB);
  458. detach_SB(&SB);
  459. if(StrDecInit(pSCDec->m_pNextSC) != ICERR_OK)
  460. return ICERR_ERROR;
  461. }
  462. else
  463. pParam->uAlphaMode = 0;
  464. pIOHeaderDec = (U8 *)malloc((PACKETLENGTH * 4 - 1) + PACKETLENGTH * 4 + sizeof(BitIOInfo));
  465. if(pIOHeaderDec == NULL)
  466. return ICERR_ERROR;
  467. memset(pIOHeaderDec, 0, (PACKETLENGTH * 4 - 1) + PACKETLENGTH * 4 + sizeof(BitIOInfo));
  468. pSCDec->pIOHeader = (BitIOInfo *)((U8 *)ALIGNUP(pIOHeaderDec, PACKETLENGTH * 4) + PACKETLENGTH * 2);
  469. if(StrIODecInit(pSCDec) != ICERR_OK)
  470. return ICERR_ERROR;
  471. if(StrDecInit(pSCDec) != ICERR_OK)
  472. return ICERR_ERROR;
  473. if(pSCDec->m_param.bAlphaChannel){ // alpha channel
  474. if(StrDecInit(pSCDec->m_pNextSC) != ICERR_OK)
  475. return ICERR_ERROR;
  476. }
  477. // initialize encoder
  478. if((pSCEnc = (CWMImageStrCodec *)malloc(sizeof(CWMImageStrCodec))) == NULL)
  479. return ICERR_ERROR;
  480. memset(pSCEnc, 0, sizeof(CWMImageStrCodec));
  481. pSCEnc->WMII = pSCDec->WMII;
  482. pSCEnc->WMISCP = pSCDec->WMISCP;
  483. pSCEnc->m_param = pSCDec->m_param;
  484. pSCEnc->WMISCP.pWStream = pStreamOut;
  485. pSCEnc->WMISCP.bfBitstreamFormat = pParam->bfBitstreamFormat;
  486. // pSCEnc->m_param.cfColorFormat = pSCEnc->WMISCP.cfColorFormat = pParam->cfColorFormat;
  487. pSCEnc->m_param.cfColorFormat = pSCEnc->WMISCP.cfColorFormat;
  488. pSCEnc->m_param.cNumChannels = (pSCEnc->WMISCP.cfColorFormat == Y_ONLY ? 1 : (pSCEnc->WMISCP.cfColorFormat == YUV_444 ? 3 : pSCEnc->WMISCP.cChannel));
  489. pSCEnc->m_param.bAlphaChannel = (pParam->uAlphaMode > 0);
  490. pSCEnc->m_param.bTranscode = TRUE;
  491. if(pParam->sbSubband >= SB_MAX)
  492. pParam->sbSubband = SB_ALL;
  493. if(pParam->sbSubband > pSCEnc->WMISCP.sbSubband)
  494. pSCEnc->WMISCP.sbSubband = pParam->sbSubband;
  495. pSCEnc->m_bSecondary = FALSE;
  496. pIOHeaderEnc = (U8 *)malloc((PACKETLENGTH * 4 - 1) + PACKETLENGTH * 4 + sizeof(BitIOInfo));
  497. if(pIOHeaderEnc == NULL)
  498. return ICERR_ERROR;
  499. memset(pIOHeaderEnc, 0, (PACKETLENGTH * 4 - 1) + PACKETLENGTH * 4 + sizeof(BitIOInfo));
  500. pSCEnc->pIOHeader = (BitIOInfo *)((U8 *)ALIGNUP(pIOHeaderEnc, PACKETLENGTH * 4) + PACKETLENGTH * 2);
  501. for(i = 0; i < pSCEnc->m_param.cNumChannels; i ++)
  502. pSCEnc->pPlane[i] = pSCDec->p1MBbuffer[i];
  503. for(i = 1; i < pSCDec->cNumBitIO * (pSCDec->WMISCP.cNumOfSliceMinus1H + 1); i ++){
  504. if(pSCDec->pIndexTable[i] == 0 && i + 1 != pSCDec->cNumBitIO * (pSCDec->WMISCP.cNumOfSliceMinus1H + 1)) // empty packet
  505. pSCDec->pIndexTable[i] = pSCDec->pIndexTable[i + 1];
  506. if(pSCDec->pIndexTable[i] != 0 && pSCDec->pIndexTable[i] < pSCDec->pIndexTable[i - 1]) // out of order bitstream, can not do fast tile extraction!
  507. pParam->bIgnoreOverlap = FALSE;
  508. }
  509. if(getROI(&pSCEnc->WMII, &pSCEnc->m_param, &pSCEnc->WMISCP, pParam) != ICERR_OK)
  510. return ICERR_ERROR;
  511. mbLeft = (pParam->cLeftX >> 4);
  512. mbRight = ((pParam->cLeftX + pParam->cWidth + 15) >> 4);
  513. mbTop = (pParam->cTopY >> 4);
  514. mbBottom = ((pParam->cTopY + pParam->cHeight + 15) >> 4);
  515. if(pSCDec->WMISCP.uiTileX[pSCDec->WMISCP.cNumOfSliceMinus1V] >= mbLeft && pSCDec->WMISCP.uiTileX[pSCDec->WMISCP.cNumOfSliceMinus1V] <= mbRight &&
  516. pSCDec->WMISCP.uiTileY[pSCDec->WMISCP.cNumOfSliceMinus1H] >= mbTop && pSCDec->WMISCP.uiTileY[pSCDec->WMISCP.cNumOfSliceMinus1H] <= mbBottom)
  517. pParam->bIgnoreOverlap = FALSE;
  518. pSCEnc->bTileExtraction = pParam->bIgnoreOverlap;
  519. mbWidth = pSCEnc->cmbWidth = mbRight - mbLeft;
  520. mbHeight = pSCEnc->cmbHeight = mbBottom - mbTop;
  521. if(oO >= O_RCW){
  522. SWAP(pSCEnc->WMII.cWidth, pSCEnc->WMII.cHeight);
  523. SWAP(pSCEnc->cmbWidth, pSCEnc->cmbHeight);
  524. }
  525. if(oO != O_NONE){
  526. pFrameBuf = (PixelI *)malloc(pSCEnc->cmbWidth * pSCEnc->cmbHeight * cUnit * sizeof(PixelI));
  527. if(pFrameBuf == NULL || (pSCEnc->cmbWidth * pSCEnc->cmbHeight * cUnit * sizeof(PixelI) < pSCEnc->cmbWidth * pSCEnc->cmbHeight * cUnit))
  528. return ICERR_ERROR;
  529. pMBInfo = (CWMIMBInfo *)malloc(pSCEnc->cmbWidth * pSCEnc->cmbHeight * sizeof(CWMIMBInfo));
  530. if(pMBInfo == NULL || (pSCEnc->cmbWidth * pSCEnc->cmbHeight * sizeof(CWMIMBInfo) < pSCEnc->cmbWidth * pSCEnc->cmbHeight))
  531. return ICERR_ERROR;
  532. if(pParam->uAlphaMode > 0){ // alpha channel
  533. pFrameBufAlpha = (PixelI *)malloc(pSCEnc->cmbWidth * pSCEnc->cmbHeight * 256 * sizeof(PixelI));
  534. if(pFrameBufAlpha == NULL || (pSCEnc->cmbWidth * pSCEnc->cmbHeight * 256 * sizeof(PixelI) < pSCEnc->cmbWidth * pSCEnc->cmbHeight * 256))
  535. return ICERR_ERROR;
  536. pMBInfoAlpha = (CWMIMBInfo *)malloc(pSCEnc->cmbWidth * pSCEnc->cmbHeight * sizeof(CWMIMBInfo));
  537. if(pMBInfoAlpha == NULL || (pSCEnc->cmbWidth * pSCEnc->cmbHeight * sizeof(CWMIMBInfo) < pSCEnc->cmbWidth * pSCEnc->cmbHeight))
  538. return ICERR_ERROR;
  539. }
  540. }
  541. if(oO < O_RCW && pSCEnc->WMII.oOrientation < O_RCW)
  542. pSCEnc->WMII.oOrientation ^= oO;
  543. else if(oO >= O_RCW && pSCEnc->WMII.oOrientation >= O_RCW){
  544. pSCEnc->WMII.oOrientation ^= oO;
  545. pSCEnc->WMII.oOrientation = (pSCEnc->WMII.oOrientation & 1) * 2 + (pSCEnc->WMII.oOrientation >> 1);
  546. }
  547. else if(oO >= O_RCW && pSCEnc->WMII.oOrientation < O_RCW)
  548. pSCEnc->WMII.oOrientation = oO ^ ((pSCEnc->WMII.oOrientation & 1) * 2 + (pSCEnc->WMII.oOrientation >> 1));
  549. else
  550. pSCEnc->WMII.oOrientation ^= ((oO & 1) * 2 + (oO >> 1));
  551. // pSCEnc->WMISCP.nExpBias += 128;
  552. if(pParam->bIgnoreOverlap == TRUE){
  553. attachISWrite(pSCEnc->pIOHeader, pSCEnc->WMISCP.pWStream);
  554. pSCEnc->pTile = pSCDec->pTile;
  555. if(pSCEnc->WMISCP.cNumOfSliceMinus1H + pSCEnc->WMISCP.cNumOfSliceMinus1V == 0 && pSCEnc->WMISCP.bfBitstreamFormat == SPATIAL)
  556. pSCEnc->m_param.bIndexTable = FALSE;
  557. WriteWMIHeader(pSCEnc);
  558. }
  559. else{
  560. pTileQPInfo = (CTileQPInfo *)malloc((oO == O_NONE ? 1 : (pSCEnc->WMISCP.cNumOfSliceMinus1H + 1) * (pSCEnc->WMISCP.cNumOfSliceMinus1V + 1)) * sizeof( CTileQPInfo));
  561. if(pTileQPInfo == NULL || ((oO == O_NONE ? 1 : (pSCEnc->WMISCP.cNumOfSliceMinus1H + 1) * (pSCEnc->WMISCP.cNumOfSliceMinus1V + 1)) * sizeof( CTileQPInfo) < (oO == O_NONE ? 1 : (pSCEnc->WMISCP.cNumOfSliceMinus1H + 1) * (pSCEnc->WMISCP.cNumOfSliceMinus1V + 1))))
  562. return ICERR_ERROR;
  563. if(StrEncInit(pSCEnc) != ICERR_OK)
  564. return ICERR_ERROR;
  565. }
  566. if(pParam->uAlphaMode > 0){ // alpha channel
  567. // pSCEnc->WMISCP.nExpBias -= 128;
  568. if((pSCEnc->m_pNextSC = (CWMImageStrCodec *)malloc(sizeof(CWMImageStrCodec))) == NULL)
  569. return ICERR_ERROR;
  570. *pSCEnc->m_pNextSC = *pSCEnc;
  571. pSCEnc->m_pNextSC->pPlane[0] = pSCDec->m_pNextSC->p1MBbuffer[0];
  572. pSCEnc->m_pNextSC->WMISCP.cfColorFormat = pSCEnc->m_pNextSC->WMII.cfColorFormat = pSCEnc->m_pNextSC->m_param.cfColorFormat = Y_ONLY;
  573. pSCEnc->m_pNextSC->WMISCP.cChannel = pSCEnc->m_pNextSC->m_param.cNumChannels = 1;
  574. pSCEnc->m_pNextSC->m_bSecondary = TRUE;
  575. pSCEnc->m_pNextSC->m_pNextSC = pSCEnc;
  576. pSCEnc->m_pNextSC->m_param = pSCDec->m_pNextSC->m_param;
  577. pSCEnc->m_param.bAlphaChannel = TRUE;
  578. if(pParam->bIgnoreOverlap == TRUE)
  579. pSCEnc->m_pNextSC->pTile = pSCDec->m_pNextSC->pTile;
  580. else if(StrEncInit(pSCEnc->m_pNextSC) != ICERR_OK)
  581. return ICERR_ERROR;
  582. WriteImagePlaneHeader(pSCEnc->m_pNextSC);
  583. }
  584. if(pParam->bIgnoreOverlap == TRUE){
  585. SUBBAND sbEnc = pSCEnc->WMISCP.sbSubband, sbDec = pSCDec->WMISCP.sbSubband;
  586. size_t cfEnc = ((pSCEnc->WMISCP.bfBitstreamFormat == SPATIAL || sbEnc == SB_DC_ONLY) ? 1 : (sbEnc == SB_NO_HIGHPASS ? 2 : (sbEnc == SB_NO_FLEXBITS ? 3 : 4)));
  587. size_t cfDec = ((pSCDec->WMISCP.bfBitstreamFormat == SPATIAL || sbDec == SB_DC_ONLY) ? 1 : (sbDec == SB_NO_HIGHPASS ? 2 : (sbDec == SB_NO_FLEXBITS ? 3 : 4)));
  588. size_t k, l = 0;
  589. pSCEnc->pIndexTable = (size_t *)malloc(sizeof(size_t) * (pSCEnc->WMISCP.cNumOfSliceMinus1H + 1) * (pSCEnc->WMISCP.cNumOfSliceMinus1V + 1) * cfEnc);
  590. if(pSCEnc->pIndexTable == NULL || cfEnc > cfDec)
  591. return ICERR_ERROR;
  592. pSCEnc->cNumBitIO = cfEnc * (pSCEnc->WMISCP.cNumOfSliceMinus1V + 1);
  593. for(j = 0; j <= pSCDec->WMISCP.cNumOfSliceMinus1H; j ++){
  594. for(i = 0; i <= pSCDec->WMISCP.cNumOfSliceMinus1V; i ++)
  595. if(pSCDec->WMISCP.uiTileX[i] >= mbLeft && pSCDec->WMISCP.uiTileX[i] < mbRight &&
  596. pSCDec->WMISCP.uiTileY[j] >= mbTop && pSCDec->WMISCP.uiTileY[j] < mbBottom){
  597. for(k = 0; k < cfEnc; k ++, l ++)
  598. pSCEnc->pIndexTable[l] = pSCDec->pIndexTable[(j * (pSCDec->WMISCP.cNumOfSliceMinus1V + 1) + i) * cfDec + k + 1] - pSCDec->pIndexTable[(j * (pSCDec->WMISCP.cNumOfSliceMinus1V + 1) + i) * cfDec + k];
  599. }
  600. }
  601. if(pSCEnc->WMISCP.cNumOfSliceMinus1H + pSCEnc->WMISCP.cNumOfSliceMinus1V == 0 && pSCEnc->WMISCP.bfBitstreamFormat == SPATIAL){
  602. pSCEnc->m_param.bIndexTable = FALSE;
  603. pSCEnc->cNumBitIO = 0;
  604. writeIndexTableNull(pSCEnc);
  605. }
  606. else
  607. writeIndexTable(pSCEnc);
  608. detachISWrite(pSCEnc, pSCEnc->pIOHeader);
  609. for(j = l = 0; j <= pSCDec->WMISCP.cNumOfSliceMinus1H; j ++){
  610. for(i = 0; i <= pSCDec->WMISCP.cNumOfSliceMinus1V; i ++)
  611. if(pSCDec->WMISCP.uiTileX[i] >= mbLeft && pSCDec->WMISCP.uiTileX[i] < mbRight &&
  612. pSCDec->WMISCP.uiTileY[j] >= mbTop && pSCDec->WMISCP.uiTileY[j] < mbBottom){
  613. for(k = 0; k < cfEnc; k ++){
  614. pSCDec->WMISCP.pWStream->SetPos(pSCDec->WMISCP.pWStream, pSCDec->pIndexTable[(j * (pSCDec->WMISCP.cNumOfSliceMinus1V + 1) + i) * cfDec + k] + pSCDec->cHeaderSize);
  615. copyTo(pSCDec->WMISCP.pWStream, pSCEnc->WMISCP.pWStream, pSCEnc->pIndexTable[l++]);
  616. }
  617. }
  618. }
  619. free(pSCEnc->pIndexTable);
  620. }
  621. else
  622. writeIndexTableNull(pSCEnc);
  623. for(pSCDec->cRow = 0; pSCDec->cRow < mbBottom && pParam->bIgnoreOverlap == FALSE; pSCDec->cRow ++){
  624. for(pSCDec->cColumn = 0; pSCDec->cColumn < pSCDec->cmbWidth; pSCDec->cColumn ++){
  625. Int cRow = (Int)pSCDec->cRow, cColumn = (Int)pSCDec->cColumn;
  626. CWMITile * pTile;
  627. memset(pMBBuf, 0, sizeof(PixelI) * cUnit);
  628. if(pSCDec->m_param.bAlphaChannel){ // alpha channel
  629. memset(pSCDec->m_pNextSC->p1MBbuffer[0], 0, sizeof(PixelI) * 256);
  630. pSCDec->m_pNextSC->cRow = pSCDec->cRow;
  631. pSCDec->m_pNextSC->cColumn = pSCDec->cColumn;
  632. }
  633. // decode
  634. pSC = pSCDec;
  635. for(i = (pSCDec->m_param.bAlphaChannel ? 2 : 1); i > 0; i --){
  636. getTilePos(pSCDec, cColumn, cRow);
  637. if(i == 2){
  638. pSCDec->m_pNextSC->cTileColumn = pSCDec->cTileColumn;
  639. pSCDec->m_pNextSC->cTileRow = pSCDec->cTileRow;
  640. }
  641. if(readPackets(pSCDec) != ICERR_OK)
  642. return ICERR_ERROR;
  643. pContext = &pSCDec->m_pCodingContext[pSCDec->cTileColumn];
  644. if(DecodeMacroblockDC(pSCDec, pContext, cColumn, cRow) != ICERR_OK)
  645. return ICERR_ERROR;
  646. if(pSCDec->cSB > 1)
  647. if(DecodeMacroblockLowpass(pSCDec, pContext, cColumn, cRow) != ICERR_OK)
  648. return ICERR_ERROR;
  649. predDCACDec(pSCDec);
  650. if(pSCDec->cSB > 2)
  651. if(DecodeMacroblockHighpass(pSCDec, pContext, cColumn, cRow) != ICERR_OK)
  652. return ICERR_ERROR;
  653. predACDec(pSCDec);
  654. updatePredInfo(pSCDec, &pSCDec->MBInfo, cColumn, pSCDec->WMISCP.cfColorFormat);
  655. pSCDec = pSCDec->m_pNextSC;
  656. }
  657. pSCDec = pSC;
  658. if(pSCDec->cRow >= mbTop && pSCDec->cColumn >= mbLeft && pSCDec->cColumn < mbRight){
  659. cRow = (Int)(pSCDec->cRow - mbTop);
  660. if(bFlipV[oO])
  661. cRow = (Int)mbHeight - cRow - 1;
  662. cColumn = (Int)(pSCDec->cColumn - mbLeft);
  663. if(bFlipH[oO])
  664. cColumn = (Int)mbWidth - cColumn - 1;
  665. pSCEnc->m_bCtxLeft = pSCEnc->m_bCtxTop = FALSE;
  666. for(i = 0; i <= pSCEnc->WMISCP.cNumOfSliceMinus1H; i ++)
  667. if(pSCEnc->WMISCP.uiTileY[i] == (U32)(oO < O_RCW ? cRow : cColumn)){
  668. pSCEnc->cTileRow = i;
  669. pSCEnc->m_bCtxTop = TRUE;
  670. break;
  671. }
  672. for(i = 0; i <= pSCEnc->WMISCP.cNumOfSliceMinus1V; i ++)
  673. if(pSCEnc->WMISCP.uiTileX[i] == (U32)(oO < O_RCW ? cColumn : cRow)){
  674. pSCEnc->cTileColumn = i;
  675. pSCEnc->m_bCtxLeft = TRUE;
  676. break;
  677. }
  678. if(pSCEnc->m_bCtxLeft && pSCEnc->m_bCtxTop){ // a new tile, buffer tile DQuant info
  679. CTileQPInfo * pTmp = pTileQPInfo;
  680. pTile = pSCDec->pTile + pSCDec->cTileColumn;
  681. if(oO != O_NONE)
  682. pTmp += pSCEnc->cTileRow * (pSCEnc->WMISCP.cNumOfSliceMinus1V + 1) + pSCEnc->cTileColumn;
  683. pTmp->dcMode = pTile->cChModeDC;
  684. for(i = 0; i < pSCEnc->WMISCP.cChannel; i ++)
  685. pTmp->dcIndex[i] = pTile->pQuantizerDC[i][0].iIndex;
  686. if(pSCEnc->WMISCP.sbSubband != SB_DC_ONLY){
  687. pTmp->bUseDC = pTile->bUseDC;
  688. pTmp->lpNum = pTile->cNumQPLP;
  689. if(pTmp->bUseDC == FALSE)
  690. for(j = 0; j < pTmp->lpNum; j ++){
  691. pTmp->lpMode[j] = pTile->cChModeLP[j];
  692. for(i = 0; i < pSCEnc->WMISCP.cChannel; i ++)
  693. pTmp->lpIndex[j][i] = pTile->pQuantizerLP[i][j].iIndex;
  694. }
  695. if(pSCEnc->WMISCP.sbSubband != SB_NO_HIGHPASS){
  696. pTmp->bUseLP = pTile->bUseLP;
  697. pTmp->hpNum = pTile->cNumQPHP;
  698. if(pTmp->bUseLP == FALSE)
  699. for(j = 0; j < pTmp->hpNum; j ++){
  700. pTmp->hpMode[j] = pTile->cChModeHP[j];
  701. for(i = 0; i < pSCEnc->WMISCP.cChannel; i ++)
  702. pTmp->hpIndex[j][i] = pTile->pQuantizerHP[i][j].iIndex;
  703. }
  704. }
  705. }
  706. if(pParam->uAlphaMode > 0){
  707. pTile = pSCDec->m_pNextSC->pTile + pSCDec->cTileColumn;
  708. pTmp->dcIndex[iAlphaPos] = pTile->pQuantizerDC[0][0].iIndex;
  709. if(pSCEnc->WMISCP.sbSubband != SB_DC_ONLY){
  710. pTmp->bUseDCAlpha = pTile->bUseDC;
  711. pTmp->lpNumAlpha = pTile->cNumQPLP;
  712. if(pTmp->bUseDCAlpha == FALSE)
  713. for(j = 0; j < pTmp->lpNumAlpha; j ++)
  714. pTmp->lpIndex[j][iAlphaPos] = pTile->pQuantizerLP[0][j].iIndex;
  715. if(pSCEnc->WMISCP.sbSubband != SB_NO_HIGHPASS){
  716. pTmp->bUseLPAlpha = pTile->bUseLP;
  717. pTmp->hpNumAlpha = pTile->cNumQPHP;
  718. if(pTmp->bUseLPAlpha == FALSE)
  719. for(j = 0; j < pTmp->hpNumAlpha; j ++)
  720. pTmp->hpIndex[j][iAlphaPos] = pTile->pQuantizerHP[0][j].iIndex;
  721. }
  722. }
  723. }
  724. }
  725. if(oO == O_NONE){
  726. // encode
  727. pSCEnc->cColumn = pSCDec->cColumn - mbLeft + 1;
  728. pSCEnc->cRow = pSCDec->cRow + 1 - mbTop;
  729. pSCEnc->MBInfo = pSCDec->MBInfo;
  730. getTilePos(pSCEnc, cColumn, cRow);
  731. if(pSCEnc->m_bCtxLeft && pSCEnc->m_bCtxTop)
  732. transcodeTileHeader(pSCEnc, pTileQPInfo);
  733. if(encodeMB(pSCEnc, cColumn, cRow) != ICERR_OK)
  734. return ICERR_ERROR;
  735. if(pParam->uAlphaMode > 0){
  736. pSCEnc->m_pNextSC->cColumn = pSCDec->cColumn - mbLeft + 1;
  737. pSCEnc->m_pNextSC->cRow = pSCDec->cRow + 1 - mbTop;
  738. getTilePos(pSCEnc->m_pNextSC, cColumn, cRow);
  739. pSCEnc->m_pNextSC->MBInfo = pSCDec->m_pNextSC->MBInfo;
  740. if(encodeMB(pSCEnc->m_pNextSC, cColumn, cRow) != ICERR_OK)
  741. return ICERR_ERROR;
  742. }
  743. }
  744. else{
  745. size_t cOff = (oO < O_RCW ? (size_t)cRow * mbWidth + (size_t)cColumn : (size_t)cRow + mbHeight * (size_t)cColumn);
  746. pMBInfo[cOff] = pSCDec->MBInfo;
  747. memcpy(&pFrameBuf[cOff * cUnit], pMBBuf, cUnit * sizeof(PixelI));
  748. if(pParam->uAlphaMode > 0){
  749. pMBInfoAlpha[cOff] = pSCDec->m_pNextSC->MBInfo;
  750. memcpy(&pFrameBufAlpha[cOff * 256], MBBufAlpha, 256 * sizeof(PixelI));
  751. }
  752. }
  753. }
  754. }
  755. advanceOneMBRow(pSCDec);
  756. if(oO == O_NONE)
  757. advanceOneMBRow(pSCEnc);
  758. }
  759. if(oO != O_NONE){
  760. for(pSCEnc->cRow = 1; pSCEnc->cRow <= pSCEnc->cmbHeight; pSCEnc->cRow ++){
  761. for(pSCEnc->cColumn = 1; pSCEnc->cColumn <= pSCEnc->cmbWidth; pSCEnc->cColumn ++){
  762. Int cRow, cColumn;
  763. size_t cOff = (pSCEnc->cRow - 1) * pSCEnc->cmbWidth + pSCEnc->cColumn - 1;
  764. for(i = 0; i < ((pSCEnc->m_param.cfColorFormat == YUV_420 || pSCEnc->m_param.cfColorFormat == YUV_422) ? 1 : pSCEnc->m_param.cNumChannels); i ++){
  765. transformDCBlock(pMBInfo[cOff].iBlockDC[i], pSCEnc->MBInfo.iBlockDC[i], oO);
  766. transformACBlocks(pFrameBuf + cOff * cUnit + i * 256, pMBBuf + 256 * i, oO);
  767. }
  768. if(pSCEnc->WMISCP.cfColorFormat == YUV_420)
  769. for(i = 0; i < 2; i ++){
  770. transformDCBlock420(pMBInfo[cOff].iBlockDC[i + 1], pSCEnc->MBInfo.iBlockDC[i + 1], oO);
  771. transformACBlocks420(pFrameBuf + cOff * cUnit + 256 + i * 64, pMBBuf + 256 + i * 64, oO);
  772. }
  773. else if(pSCEnc->WMISCP.cfColorFormat == YUV_422)
  774. for(i = 0; i < 2; i ++){
  775. transformDCBlock422(pMBInfo[cOff].iBlockDC[i + 1], pSCEnc->MBInfo.iBlockDC[i + 1], oO);
  776. transformACBlocks422(pFrameBuf + cOff * cUnit + 256 + i * 128, pMBBuf + 256 + i * 128, oO);
  777. }
  778. pSCEnc->MBInfo.iQIndexLP = pMBInfo[cOff].iQIndexLP;
  779. pSCEnc->MBInfo.iQIndexHP = pMBInfo[cOff].iQIndexHP;
  780. cRow = (Int)pSCEnc->cRow - 1;
  781. cColumn = (Int)pSCEnc->cColumn - 1;
  782. getTilePos(pSCEnc, cColumn, cRow);
  783. if(pSCEnc->m_bCtxLeft && pSCEnc->m_bCtxTop)
  784. transcodeTileHeader(pSCEnc, pTileQPInfo + pSCEnc->cTileRow * (pSCEnc->WMISCP.cNumOfSliceMinus1V + 1) + pSCEnc->cTileColumn);
  785. if(encodeMB(pSCEnc, cColumn, cRow) != ICERR_OK)
  786. return ICERR_ERROR;
  787. if(pParam->uAlphaMode > 0){
  788. pSCEnc->m_pNextSC->cColumn = pSCEnc->cColumn;
  789. pSCEnc->m_pNextSC->cRow = pSCEnc->cRow;
  790. getTilePos(pSCEnc->m_pNextSC, cColumn, cRow);
  791. pSCEnc->m_pNextSC->MBInfo = pSCDec->m_pNextSC->MBInfo;
  792. transformDCBlock(pMBInfoAlpha[cOff].iBlockDC[0], pSCEnc->m_pNextSC->MBInfo.iBlockDC[0], oO);
  793. transformACBlocks(pFrameBufAlpha + cOff * 256, MBBufAlpha, oO);
  794. pSCEnc->m_pNextSC->MBInfo.iQIndexLP = pMBInfoAlpha[cOff].iQIndexLP;
  795. pSCEnc->m_pNextSC->MBInfo.iQIndexHP = pMBInfoAlpha[cOff].iQIndexHP;
  796. if(encodeMB(pSCEnc->m_pNextSC, cColumn, cRow) != ICERR_OK)
  797. return ICERR_ERROR;
  798. }
  799. }
  800. advanceOneMBRow(pSCEnc);
  801. }
  802. }
  803. free(pMBBuf);
  804. if(oO != O_NONE){
  805. free(pFrameBuf);
  806. free(pMBInfo);
  807. if(pParam->uAlphaMode > 0){ // alpha channel
  808. free(pFrameBufAlpha);
  809. free(pMBInfoAlpha);
  810. }
  811. }
  812. freePredInfo(pSCDec);
  813. freeTileInfo(pSCDec);
  814. StrIODecTerm(pSCDec);
  815. FreeCodingContextDec(pSCDec);
  816. if(pSCDec->m_param.bAlphaChannel)
  817. free(pSCDec->m_pNextSC);
  818. free(pSCDec);
  819. free(pIOHeaderDec);
  820. if(pParam->bIgnoreOverlap == FALSE){
  821. freePredInfo(pSCEnc);
  822. freeTileInfo(pSCEnc);
  823. StrIOEncTerm(pSCEnc);
  824. free(pTileQPInfo);
  825. FreeCodingContextEnc(pSCEnc);
  826. }
  827. free(pSCEnc);
  828. free(pIOHeaderEnc);
  829. return ICERR_OK;
  830. }