strenc.c 92 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370
  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 "strcodec.h"
  29. #include "encode.h"
  30. #include "strTransform.h"
  31. #include <math.h>
  32. #include "perfTimer.h"
  33. #ifdef MEM_TRACE
  34. #define TRACE_MALLOC 1
  35. #define TRACE_NEW 0
  36. #define TRACE_HEAP 0
  37. #include "memtrace.h"
  38. #endif
  39. #ifdef ADI_SYS_OPT
  40. extern char L1WW[];
  41. #endif
  42. #ifdef X86OPT_INLINE
  43. #define _FORCEINLINE __forceinline
  44. #else // X86OPT_INLINE
  45. #define _FORCEINLINE
  46. #endif // X86OPT_INLINE
  47. Int inputMBRow(CWMImageStrCodec *);
  48. #if defined(WMP_OPT_SSE2) || defined(WMP_OPT_CC_ENC) || defined(WMP_OPT_TRFM_ENC)
  49. void StrEncOpt(CWMImageStrCodec* pSC);
  50. #endif // OPT defined
  51. #define MINIMUM_PACKET_LENGTH 4 // as long as packet header - skipped if data is not accessed (happens only for flexbits)
  52. Void writeQuantizer(CWMIQuantizer * pQuantizer[MAX_CHANNELS], BitIOInfo * pIO, U8 cChMode, size_t cChannel, size_t iPos)
  53. {
  54. if(cChMode > 2)
  55. cChMode = 2;
  56. if(cChannel > 1)
  57. putBit16(pIO, cChMode, 2); // Channel mode
  58. else
  59. cChMode = 0;
  60. putBit16(pIO, pQuantizer[0][iPos].iIndex, 8); // Y
  61. if(cChMode == 1) // MIXED
  62. putBit16(pIO, pQuantizer[1][iPos].iIndex, 8); // UV
  63. else if(cChMode > 0){ // INDEPENDENT
  64. size_t i;
  65. for(i = 1; i < cChannel; i ++)
  66. putBit16(pIO, pQuantizer[i][iPos].iIndex, 8); // UV
  67. }
  68. }
  69. // packet header: 00000000 00000000 00000001 ?????xxx
  70. // xxx: 000(spatial) 001(DC) 010(AD) 011(AC) 100(FL) 101-111(reserved)
  71. // ?????: (iTileY * cNumOfSliceV + iTileX)
  72. Void writePacketHeader(BitIOInfo * pIO, U8 ptPacketType, U8 pID)
  73. {
  74. putBit16(pIO, 0, 8);
  75. putBit16(pIO, 0, 8);
  76. putBit16(pIO, 1, 8);
  77. putBit16(pIO, (pID << 3) + (ptPacketType & 7), 8);
  78. }
  79. Int writeTileHeaderDC(CWMImageStrCodec * pSC, BitIOInfo * pIO)
  80. {
  81. size_t iTile, j = (pSC->m_pNextSC == NULL ? 1U : 2U);
  82. for(; j > 0; j --){
  83. if((pSC->m_param.uQPMode & 1) != 0){ // not DC uniform
  84. CWMITile * pTile = pSC->pTile + pSC->cTileColumn;
  85. size_t i;
  86. pTile->cChModeDC = (U8)(rand() & 3); // channel mode, just for concept proofing!
  87. if(pSC->cTileRow + pSC->cTileColumn == 0) // allocate DC QP info
  88. for(iTile = 0; iTile <= pSC->WMISCP.cNumOfSliceMinus1V; iTile ++)
  89. if(allocateQuantizer(pSC->pTile[iTile].pQuantizerDC, pSC->m_param.cNumChannels, 1) != ICERR_OK)
  90. return ICERR_ERROR;
  91. for(i = 0; i < pSC->m_param.cNumChannels; i ++)
  92. pTile->pQuantizerDC[i]->iIndex = (U8)((rand() & 0x2f) + 1); // QP indexes, just for concept proofing!
  93. formatQuantizer(pTile->pQuantizerDC, pTile->cChModeDC, pSC->m_param.cNumChannels, 0, TRUE, pSC->m_param.bScaledArith);
  94. for(i = 0; i < pSC->m_param.cNumChannels; i ++)
  95. pTile->pQuantizerDC[i]->iOffset = (pTile->pQuantizerDC[i]->iQP >> 1);
  96. writeQuantizer(pTile->pQuantizerDC, pIO, pTile->cChModeDC, pSC->m_param.cNumChannels, 0);
  97. }
  98. pSC = pSC->m_pNextSC;
  99. }
  100. return ICERR_OK;
  101. }
  102. Int writeTileHeaderLP(CWMImageStrCodec * pSC, BitIOInfo * pIO)
  103. {
  104. size_t k = (pSC->m_pNextSC == NULL ? 1U : 2U);
  105. for(; k > 0; k --){
  106. if(pSC->WMISCP.sbSubband != SB_DC_ONLY && (pSC->m_param.uQPMode & 2) != 0){ // not LP uniform
  107. CWMITile * pTile = pSC->pTile + pSC->cTileColumn;
  108. U8 i, j;
  109. pTile->bUseDC = ((rand() & 1) == 0 ? TRUE : FALSE); // use DC quantizer?
  110. putBit16(pIO, pTile->bUseDC == TRUE ? 1 : 0, 1);
  111. pTile->cBitsLP = 0;
  112. pTile->cNumQPLP = (pTile->bUseDC == TRUE ? 1 : (U8)((rand() & 0xf) + 1)); // # of LP QPs
  113. if(pSC->cTileRow > 0)
  114. freeQuantizer(pTile->pQuantizerLP);
  115. if(allocateQuantizer(pTile->pQuantizerLP, pSC->m_param.cNumChannels, pTile->cNumQPLP) != ICERR_OK)
  116. return ICERR_ERROR;
  117. if(pTile->bUseDC == TRUE)
  118. useDCQuantizer(pSC, pSC->cTileColumn);
  119. else{
  120. putBit16(pIO, pTile->cNumQPLP - 1, 4);
  121. pTile->cBitsLP = dquantBits(pTile->cNumQPLP);
  122. for(i = 0; i < pTile->cNumQPLP; i ++){
  123. pTile->cChModeLP[i] = (U8)(rand() & 3); // channel mode, just for concept proofing!
  124. for(j = 0; j < pSC->m_param.cNumChannels; j ++)
  125. pTile->pQuantizerLP[j][i].iIndex = (U8)((rand() & 0xfe) + 1); // QP indexes, just for concept proofing!
  126. formatQuantizer(pTile->pQuantizerLP, pTile->cChModeLP[i], pSC->m_param.cNumChannels, i, TRUE, pSC->m_param.bScaledArith);
  127. writeQuantizer(pTile->pQuantizerLP, pIO, pTile->cChModeLP[i], pSC->m_param.cNumChannels, i);
  128. }
  129. }
  130. }
  131. pSC = pSC->m_pNextSC;
  132. }
  133. return ICERR_OK;
  134. }
  135. Int writeTileHeaderHP(CWMImageStrCodec * pSC, BitIOInfo * pIO)
  136. {
  137. size_t k = (pSC->m_pNextSC == NULL ? 1U : 2U);
  138. for(; k > 0; k --){
  139. if(pSC->WMISCP.sbSubband != SB_DC_ONLY && pSC->WMISCP.sbSubband != SB_NO_HIGHPASS && (pSC->m_param.uQPMode & 4) != 0){ // not HP uniform
  140. CWMITile * pTile = pSC->pTile + pSC->cTileColumn;
  141. U8 i, j;
  142. pTile->bUseLP = ((rand() & 1) == 0 ? TRUE : FALSE); // use LP quantizer?
  143. putBit16(pIO, pTile->bUseLP == TRUE ? 1 : 0, 1);
  144. pTile->cBitsHP = 0;
  145. pTile->cNumQPHP = (pTile->bUseLP == TRUE ? pTile->cNumQPLP : (U8)((rand() & 0xf) + 1)); // # of LP QPs
  146. if(pSC->cTileRow > 0)
  147. freeQuantizer(pTile->pQuantizerHP);
  148. if(allocateQuantizer(pTile->pQuantizerHP, pSC->m_param.cNumChannels, pTile->cNumQPHP) != ICERR_OK)
  149. return ICERR_ERROR;
  150. if(pTile->bUseLP == TRUE)
  151. useLPQuantizer(pSC, pTile->cNumQPHP, pSC->cTileColumn);
  152. else{
  153. putBit16(pIO, pTile->cNumQPHP - 1, 4);
  154. pTile->cBitsHP = dquantBits(pTile->cNumQPHP);
  155. for(i = 0; i < pTile->cNumQPHP; i ++){
  156. pTile->cChModeHP[i] = (U8)(rand() & 3); // channel mode, just for concept proofing!
  157. for(j = 0; j < pSC->m_param.cNumChannels; j ++)
  158. pTile->pQuantizerHP[j][i].iIndex = (U8)((rand() & 0xfe) + 1); // QP indexes, just for concept proofing!
  159. formatQuantizer(pTile->pQuantizerHP, pTile->cChModeHP[i], pSC->m_param.cNumChannels, i, FALSE, pSC->m_param.bScaledArith);
  160. writeQuantizer(pTile->pQuantizerHP, pIO, pTile->cChModeHP[i], pSC->m_param.cNumChannels, i);
  161. }
  162. }
  163. }
  164. pSC = pSC->m_pNextSC;
  165. }
  166. return ICERR_OK;
  167. }
  168. Int encodeMB(CWMImageStrCodec * pSC, Int iMBX, Int iMBY)
  169. {
  170. CCodingContext * pContext = &pSC->m_pCodingContext[pSC->cTileColumn];
  171. if(pSC->m_bCtxLeft && pSC->m_bCtxTop && pSC->m_bSecondary == FALSE && pSC->m_param.bTranscode == FALSE){ // write packet headers
  172. U8 pID = (U8)((pSC->cTileRow * (pSC->WMISCP.cNumOfSliceMinus1V + 1) + pSC->cTileColumn) & 0x1F);
  173. if(pSC->WMISCP.bfBitstreamFormat == SPATIAL) {
  174. writePacketHeader(pContext->m_pIODC, 0, pID);
  175. if (pSC->m_param.bTrimFlexbitsFlag)
  176. putBit16(pContext->m_pIODC, pContext->m_iTrimFlexBits, 4);
  177. writeTileHeaderDC(pSC, pContext->m_pIODC);
  178. writeTileHeaderLP(pSC, pContext->m_pIODC);
  179. writeTileHeaderHP(pSC, pContext->m_pIODC);
  180. }
  181. else{
  182. writePacketHeader(pContext->m_pIODC, 1, pID);
  183. writeTileHeaderDC(pSC, pContext->m_pIODC);
  184. if(pSC->cSB > 1){
  185. writePacketHeader(pContext->m_pIOLP, 2, pID);
  186. writeTileHeaderLP(pSC, pContext->m_pIOLP);
  187. }
  188. if(pSC->cSB > 2){
  189. writePacketHeader(pContext->m_pIOAC, 3, pID);
  190. writeTileHeaderHP(pSC, pContext->m_pIOAC);
  191. }
  192. if(pSC->cSB > 3) {
  193. writePacketHeader(pContext->m_pIOFL, 4, pID);
  194. if (pSC->m_param.bTrimFlexbitsFlag)
  195. putBit16(pContext->m_pIOFL, pContext->m_iTrimFlexBits, 4);
  196. }
  197. }
  198. }
  199. if(EncodeMacroblockDC(pSC, pContext, iMBX, iMBY) != ICERR_OK)
  200. return ICERR_ERROR;
  201. if(pSC->WMISCP.sbSubband != SB_DC_ONLY)
  202. if(EncodeMacroblockLowpass(pSC, pContext, iMBX, iMBY) != ICERR_OK)
  203. return ICERR_ERROR;
  204. if(pSC->WMISCP.sbSubband != SB_DC_ONLY && pSC->WMISCP.sbSubband != SB_NO_HIGHPASS)
  205. if(EncodeMacroblockHighpass(pSC, pContext, iMBX, iMBY) != ICERR_OK)
  206. return ICERR_ERROR;
  207. if(iMBX + 1 == (int) pSC->cmbWidth && (iMBY + 1 == (int) pSC->cmbHeight ||
  208. (pSC->cTileRow < pSC->WMISCP.cNumOfSliceMinus1H && iMBY == (int) pSC->WMISCP.uiTileY[pSC->cTileRow + 1] - 1)))
  209. { // end of a horizontal slice
  210. size_t k, l;
  211. // get sizes of each packet and update index table
  212. if (pSC->m_pNextSC == NULL || pSC->m_bSecondary) {
  213. for(k = 0; k < pSC->cNumBitIO; k ++){
  214. fillToByte(pSC->m_ppBitIO[k]);
  215. pSC->ppWStream[k]->GetPos(pSC->ppWStream[k], &l);
  216. pSC->pIndexTable[pSC->cNumBitIO * pSC->cTileRow + k] = l + getSizeWrite(pSC->m_ppBitIO[k]); // offset
  217. }
  218. }
  219. // reset coding contexts
  220. if(iMBY + 1 != (int) pSC->cmbHeight){
  221. for(k = 0; k <= pSC->WMISCP.cNumOfSliceMinus1V; k ++)
  222. ResetCodingContextEnc(&pSC->m_pCodingContext[k]);
  223. }
  224. }
  225. return ICERR_OK;
  226. }
  227. /*************************************************************************
  228. Top level function for processing a macroblock worth of input
  229. *************************************************************************/
  230. Int processMacroblock(CWMImageStrCodec *pSC)
  231. {
  232. Bool topORleft = (pSC->cColumn == 0 || pSC->cRow == 0);
  233. ERR_CODE result = ICERR_OK;
  234. size_t j, jend = (pSC->m_pNextSC != NULL);
  235. for (j = 0; j <= jend; j++) {
  236. transformMacroblock(pSC);
  237. if(!topORleft){
  238. getTilePos(pSC, (Int)pSC->cColumn - 1, (Int)pSC->cRow - 1);
  239. if(jend){
  240. pSC->m_pNextSC->cTileRow = pSC->cTileRow;
  241. pSC->m_pNextSC->cTileColumn = pSC->cTileColumn;
  242. }
  243. if ((result = encodeMB(pSC, (Int)pSC->cColumn - 1, (Int)pSC->cRow - 1)) != ICERR_OK)
  244. return result;
  245. }
  246. if (jend) {
  247. pSC->m_pNextSC->cRow = pSC->cRow;
  248. pSC->m_pNextSC->cColumn = pSC->cColumn;
  249. pSC = pSC->m_pNextSC;
  250. }
  251. }
  252. return ICERR_OK;
  253. }
  254. /*************************************************************************
  255. forwardRGBE: forward conversion from RGBE to RGB
  256. *************************************************************************/
  257. static _FORCEINLINE PixelI forwardRGBE (PixelI RGB, PixelI E)
  258. {
  259. PixelI iResult = 0, iAppend = 1;
  260. if (E == 0)
  261. return 0;
  262. assert (E!=0);
  263. E--;
  264. while (((RGB & 0x80) == 0) && (E > 0)) {
  265. RGB = (RGB << 1) + iAppend;
  266. iAppend = 0;
  267. E--;
  268. }
  269. // result will always be one of 3 cases
  270. // E RGB convert to
  271. // 0 [0.x] [0 x]
  272. // 0 [1.x] [1 x]
  273. // e [1.x] [e+1 x]
  274. if (E == 0) {
  275. iResult = RGB;
  276. }
  277. else {
  278. E++;
  279. iResult = (RGB & 0x7f) + (E << 7);
  280. }
  281. return iResult;
  282. }
  283. /*************************************************************************
  284. convert float-32 into float with (c, lm)!!
  285. *************************************************************************/
  286. static _FORCEINLINE PixelI float2pixel (float f, const char _c, const unsigned char _lm)
  287. {
  288. union uif
  289. {
  290. I32 i;
  291. float f;
  292. } x;
  293. PixelI _h, e, e1, m, s;
  294. if (f == 0)
  295. {
  296. _h = 0;
  297. }
  298. else
  299. {
  300. x.f = f;
  301. e = (x.i >> 23) & 0x000000ff;//here set e as e, not s! e includes s: [s e] 9 bits [31..23]
  302. m = (x.i & 0x007fffff) | 0x800000; // actual mantissa, with normalizer
  303. if (e == 0) { // denormal-land
  304. m ^= 0x800000; // actual mantissa, removing normalizer
  305. e++; // actual exponent -126
  306. }
  307. e1 = e - 127 + _c; // this is basically a division or quantization to a different exponent
  308. // note: _c cannot be greater than 127, so e1 cannot be greater than e
  309. //assert (_c <= 127);
  310. if (e1 <= 1) { // denormal-land
  311. if (e1 < 1)
  312. m >>= (1 - e1); // shift mantissa right to make exponent 1
  313. e1 = 1;
  314. if ((m & 0x800000) == 0) // if denormal, set e1 to zero else to 1
  315. e1 = 0;
  316. }
  317. m &= 0x007fffff;
  318. //for float-22:
  319. _h = (e1 << _lm) + ((m + (1 << (23 - _lm - 1))) >> (23 - _lm));//take 23-bit m, shift (23-lm), get lm-bit m for float22
  320. s = ((PixelI) x.i) >> 31;
  321. //padding to int-32:
  322. _h = (_h ^ s) - s;
  323. }
  324. return _h;
  325. }
  326. /*************************************************************************
  327. convert Half-16 to internal format, only need to handle sign bit
  328. *************************************************************************/
  329. static _FORCEINLINE PixelI forwardHalf (PixelI hHalf)
  330. {
  331. PixelI s;
  332. s = hHalf >> 31;
  333. hHalf = ((hHalf & 0x7fff) ^ s) - s;
  334. return hHalf;
  335. }
  336. //================================================================
  337. // Color Conversion
  338. // functions to get image data from input buffer
  339. // this inlcudes necessary color conversion and boundary padding
  340. //================================================================
  341. #define _CC(r, g, b) (b -= r, r += ((b + 1) >> 1) - g, g += ((r + 0) >> 1))
  342. #define _CC_CMYK(c, m, y, k) (y -= c, c += ((y + 1) >> 1) - m, m += (c >> 1) - k, k += ((m + 1) >> 1))
  343. //================================================================
  344. // BitIOInfo init/term for encoding
  345. const size_t MAX_MEMORY_SIZE_IN_WORDS = 64 << 20; // 1 << 20 \approx 1 million
  346. Int StrIOEncInit(CWMImageStrCodec* pSC)
  347. {
  348. pSC->m_param.bIndexTable = !(pSC->WMISCP.bfBitstreamFormat == SPATIAL && pSC->WMISCP.cNumOfSliceMinus1H + pSC->WMISCP.cNumOfSliceMinus1V == 0);
  349. if(allocateBitIOInfo(pSC) != ICERR_OK){
  350. return ICERR_ERROR;
  351. }
  352. attachISWrite(pSC->pIOHeader, pSC->WMISCP.pWStream);
  353. if(pSC->cNumBitIO > 0){
  354. size_t i;
  355. #if defined(_WINDOWS_) || defined(UNDER_CE) // tmpnam does not exist in VS2005 WinCE CRT
  356. TCHAR szPath[MAX_PATH];
  357. DWORD cSize, j, k;
  358. #endif
  359. char * pFilename;
  360. pSC->ppWStream = (struct WMPStream **)malloc(pSC->cNumBitIO * sizeof(struct WMPStream *));
  361. if(pSC->ppWStream == NULL) return ICERR_ERROR;
  362. memset(pSC->ppWStream, 0, pSC->cNumBitIO * sizeof(struct WMPStream *));
  363. if (pSC->cmbHeight * pSC->cmbWidth * pSC->WMISCP.cChannel >= MAX_MEMORY_SIZE_IN_WORDS) {
  364. #ifdef _WINDOWS_
  365. pSC->ppTempFile = (TCHAR **)malloc(pSC->cNumBitIO * sizeof(TCHAR *));
  366. if(pSC->ppTempFile == NULL) return ICERR_ERROR;
  367. memset(pSC->ppTempFile, 0, pSC->cNumBitIO * sizeof(TCHAR *));
  368. #else
  369. pSC->ppTempFile = (char **)malloc(pSC->cNumBitIO * sizeof(char *));
  370. if(pSC->ppTempFile == NULL) return ICERR_ERROR;
  371. memset(pSC->ppTempFile, 0, pSC->cNumBitIO * sizeof(char *));
  372. #endif
  373. }
  374. for(i = 0; i < pSC->cNumBitIO; i ++){
  375. if (pSC->cmbHeight * pSC->cmbWidth * pSC->WMISCP.cChannel >= MAX_MEMORY_SIZE_IN_WORDS) {
  376. #if defined(_WINDOWS_) || defined(UNDER_CE) // tmpnam does not exist in VS2005 WinCE CRT
  377. Bool bUnicode = sizeof(TCHAR) == 2;
  378. pSC->ppTempFile[i] = (TCHAR *)malloc(MAX_PATH * sizeof(TCHAR));
  379. if(pSC->ppTempFile[i] == NULL) return ICERR_ERROR;
  380. pFilename = (char *)pSC->ppTempFile[i];
  381. cSize = GetTempPath(MAX_PATH, szPath);
  382. if(cSize == 0 || cSize >= MAX_PATH)
  383. return ICERR_ERROR;
  384. if(!GetTempFileName(szPath, TEXT("wdp"), 0, pSC->ppTempFile[i]))
  385. return ICERR_ERROR;
  386. if(bUnicode){ // unicode file name
  387. for(k = j = cSize = 0; cSize < MAX_PATH; cSize ++, j += 2){
  388. if(pSC->ppTempFile[i][cSize] == '\0')
  389. break;
  390. if(pFilename[j] != '\0')
  391. pFilename[k ++] = pFilename[j];
  392. if(pFilename[j + 1] != '\0')
  393. pFilename[k ++] = pFilename[j + 1];
  394. }
  395. pFilename[cSize] = '\0';
  396. }
  397. #else //DPK needs to support ANSI
  398. pSC->ppTempFile[i] = (char *)malloc(FILENAME_MAX * sizeof(char));
  399. if(pSC->ppTempFile[i] == NULL) return ICERR_ERROR;
  400. if ((pFilename = tmpnam(NULL)) == NULL)
  401. return ICERR_ERROR;
  402. strcpy(pSC->ppTempFile[i], pFilename);
  403. #endif
  404. if(CreateWS_File(pSC->ppWStream + i, pFilename, "w+b") != ICERR_OK) return ICERR_ERROR;
  405. }
  406. else {
  407. if(CreateWS_List(pSC->ppWStream + i) != ICERR_OK) return ICERR_ERROR;
  408. }
  409. attachISWrite(pSC->m_ppBitIO[i], pSC->ppWStream[i]);
  410. }
  411. }
  412. return ICERR_OK;
  413. }
  414. #define PUTBITS putBit16
  415. /*************************************************************************
  416. Write variable length byte aligned integer
  417. *************************************************************************/
  418. static Void PutVLWordEsc(BitIOInfo* pIO, Int iEscape, size_t s)
  419. {
  420. if (iEscape) {
  421. assert(iEscape <= 0xff && iEscape > 0xfc); // fd,fe,ff are the only valid escapes
  422. PUTBITS(pIO, iEscape, 8);
  423. }
  424. else if (s < 0xfb00) {
  425. PUTBITS(pIO, (U32) s, 16);
  426. }
  427. else {
  428. size_t t = s >> 16;
  429. if ((t >> 16) == 0) {
  430. PUTBITS(pIO, 0xfb, 8);
  431. }
  432. else {
  433. t >>= 16;
  434. PUTBITS(pIO, 0xfc, 8);
  435. PUTBITS(pIO, (U32)(t >> 16) & 0xffff, 16);
  436. PUTBITS(pIO, (U32) t & 0xffff, 16);
  437. }
  438. PUTBITS(pIO, (U32) t & 0xffff, 16);
  439. PUTBITS(pIO, (U32) s & 0xffff, 16);
  440. }
  441. }
  442. /*************************************************************************
  443. Write index table at start (null index table)
  444. *************************************************************************/
  445. Int writeIndexTableNull(CWMImageStrCodec * pSC)
  446. {
  447. if(pSC->cNumBitIO == 0){
  448. BitIOInfo* pIO = pSC->pIOHeader;
  449. fillToByte(pIO);
  450. /* Profile / Level info */
  451. PutVLWordEsc(pIO, 0, 4); // 4 bytes
  452. PUTBITS(pIO, 111, 8); // default profile idc
  453. PUTBITS(pIO, 255, 8); // default level idc
  454. PUTBITS(pIO, 1, 16); // LAST_FLAG
  455. }
  456. return ICERR_OK;
  457. }
  458. /*************************************************************************
  459. Write index table
  460. *************************************************************************/
  461. Int writeIndexTable(CWMImageStrCodec * pSC)
  462. {
  463. if(pSC->cNumBitIO > 0){
  464. BitIOInfo* pIO = pSC->pIOHeader;
  465. size_t *pTable = pSC->pIndexTable, iSize[4] = { 0 };
  466. I32 iEntry = (I32)pSC->cNumBitIO * (pSC->WMISCP.cNumOfSliceMinus1H + 1), i, k, l;
  467. // write index table header [0x0001] - 2 bytes
  468. PUTBITS(pIO, 1, 16);
  469. for(i = pSC->WMISCP.cNumOfSliceMinus1H; i>= 0 && pSC->bTileExtraction == FALSE; i --){
  470. for(k = 0; k < (int)pSC->cNumBitIO; ){
  471. for(l = 0; l < (pSC->WMISCP.bfBitstreamFormat == FREQUENCY && pSC->WMISCP.bProgressiveMode ? pSC->cSB : 1); l ++, k ++)
  472. {
  473. if (i > 0)
  474. pTable[pSC->cNumBitIO * i + k] -= pSC->pIndexTable[pSC->cNumBitIO * (i - 1) + k]; // packet length
  475. iSize[l] += pTable[pSC->cNumBitIO * i + k];
  476. }
  477. }
  478. }
  479. iSize[3] = iSize[2] + iSize[1] + iSize[0];
  480. iSize[2] = iSize[1] + iSize[0];
  481. iSize[1] = iSize[0];
  482. iSize[0] = 0;
  483. for(i = 0; i < iEntry; ){
  484. for(l = 0; l < (pSC->WMISCP.bfBitstreamFormat == FREQUENCY && pSC->WMISCP.bProgressiveMode ? pSC->cSB : 1); l ++, i ++)
  485. {
  486. writeIS_L1(pSC, pIO);
  487. PutVLWordEsc(pIO, (pTable[i] <= MINIMUM_PACKET_LENGTH) ? 0xff : 0, iSize[l]);
  488. iSize[l] += (pTable[i] <= MINIMUM_PACKET_LENGTH) ? 0 : pTable[i];
  489. }
  490. }
  491. writeIS_L1(pSC, pIO);
  492. PutVLWordEsc(pIO, 0xff, 0); // escape to end
  493. fillToByte(pIO);
  494. }
  495. return ICERR_OK;
  496. }
  497. Int copyTo(struct WMPStream * pSrc, struct WMPStream * pDst, size_t iBytes)
  498. {
  499. char pData[PACKETLENGTH];
  500. if (iBytes <= MINIMUM_PACKET_LENGTH){
  501. pSrc->Read(pSrc, pData, iBytes);
  502. return ICERR_OK;
  503. }
  504. while(iBytes > PACKETLENGTH){
  505. pSrc->Read(pSrc, pData, PACKETLENGTH);
  506. pDst->Write(pDst, pData, PACKETLENGTH);
  507. iBytes -= PACKETLENGTH;
  508. }
  509. pSrc->Read(pSrc, pData, iBytes);
  510. pDst->Write(pDst, pData, iBytes);
  511. return ICERR_OK;
  512. }
  513. Int StrIOEncTerm(CWMImageStrCodec* pSC)
  514. {
  515. BitIOInfo * pIO = pSC->pIOHeader;
  516. fillToByte(pIO);
  517. if(pSC->WMISCP.bVerbose){
  518. U32 i, j;
  519. printf("\n%d horizontal tiles:\n", pSC->WMISCP.cNumOfSliceMinus1H + 1);
  520. for(i = 0; i <= pSC->WMISCP.cNumOfSliceMinus1H; i ++){
  521. printf(" offset of tile %d in MBs: %d\n", i, pSC->WMISCP.uiTileY[i]);
  522. }
  523. printf("\n%d vertical tiles:\n", pSC->WMISCP.cNumOfSliceMinus1V + 1);
  524. for(i = 0; i <= pSC->WMISCP.cNumOfSliceMinus1V; i ++){
  525. printf(" offset of tile %d in MBs: %d\n", i, pSC->WMISCP.uiTileX[i]);
  526. }
  527. if(pSC->WMISCP.bfBitstreamFormat == SPATIAL){
  528. printf("\nSpatial order bitstream\n");
  529. }
  530. else{
  531. printf("\nFrequency order bitstream\n");
  532. }
  533. if(!pSC->m_param.bIndexTable){
  534. printf("\nstreaming mode, no index table.\n");
  535. }
  536. else if(pSC->WMISCP.bfBitstreamFormat == SPATIAL){
  537. for(j = 0; j <= pSC->WMISCP.cNumOfSliceMinus1H; j ++){
  538. for(i = 0; i <= pSC->WMISCP.cNumOfSliceMinus1V; i ++){
  539. printf("bitstream size for tile (%d, %d): %d.\n", j, i, (int) pSC->pIndexTable[j * (pSC->WMISCP.cNumOfSliceMinus1V + 1) + i]);
  540. }
  541. }
  542. }
  543. else{
  544. for(j = 0; j <= pSC->WMISCP.cNumOfSliceMinus1H; j ++){
  545. for(i = 0; i <= pSC->WMISCP.cNumOfSliceMinus1V; i ++){
  546. size_t * p = &pSC->pIndexTable[(j * (pSC->WMISCP.cNumOfSliceMinus1V + 1) + i) * 4];
  547. printf("bitstream size of (DC, LP, AC, FL) for tile (%d, %d): %d %d %d %d.\n", j, i,
  548. (int) p[0], (int) p[1], (int) p[2], (int) p[3]);
  549. }
  550. }
  551. }
  552. }
  553. writeIndexTable(pSC); // write index table to the header
  554. detachISWrite(pSC, pIO);
  555. if(pSC->cNumBitIO > 0){
  556. size_t i, j, k, l;
  557. struct WMPStream * pDst = pSC->WMISCP.pWStream;
  558. size_t * pTable = pSC->pIndexTable;
  559. for(i = 0; i < pSC->cNumBitIO; i ++){
  560. detachISWrite(pSC, pSC->m_ppBitIO[i]);
  561. }
  562. for(i = 0; i < pSC->cNumBitIO; i ++){
  563. pSC->ppWStream[i]->SetPos(pSC->ppWStream[i], 0); // seek back for read
  564. }
  565. for(l = 0; l < (size_t)(pSC->WMISCP.bfBitstreamFormat == FREQUENCY && pSC->WMISCP.bProgressiveMode ? pSC->cSB : 1); l ++){
  566. for(i = 0, k = l; i <= pSC->WMISCP.cNumOfSliceMinus1H; i ++){ // loop through tiles
  567. for(j = 0; j <= pSC->WMISCP.cNumOfSliceMinus1V; j ++){
  568. if(pSC->WMISCP.bfBitstreamFormat == SPATIAL)
  569. copyTo(pSC->ppWStream[j], pDst, pTable[k ++]);
  570. else if (!pSC->WMISCP.bProgressiveMode){
  571. copyTo(pSC->ppWStream[j * pSC->cSB + 0], pDst, pTable[k ++]);
  572. if(pSC->cSB > 1)
  573. copyTo(pSC->ppWStream[j * pSC->cSB + 1], pDst, pTable[k ++]);
  574. if(pSC->cSB > 2)
  575. copyTo(pSC->ppWStream[j * pSC->cSB + 2], pDst, pTable[k ++]);
  576. if(pSC->cSB > 3)
  577. copyTo(pSC->ppWStream[j * pSC->cSB + 3], pDst, pTable[k ++]);
  578. }
  579. else{
  580. copyTo(pSC->ppWStream[j * pSC->cSB + l], pDst, pTable[k]);
  581. k += pSC->cSB;
  582. }
  583. }
  584. }
  585. }
  586. if (pSC->cmbHeight * pSC->cmbWidth * pSC->WMISCP.cChannel >= MAX_MEMORY_SIZE_IN_WORDS){
  587. for(i = 0; i < pSC->cNumBitIO; i ++){
  588. if(pSC->ppWStream && pSC->ppWStream[i]){
  589. if((*(pSC->ppWStream + i))->state.file.pFile){
  590. fclose((*(pSC->ppWStream + i))->state.file.pFile);
  591. #ifdef _WINDOWS_
  592. if(DeleteFileA((LPCSTR)pSC->ppTempFile[i]) == 0)
  593. return ICERR_ERROR;
  594. #else
  595. if (remove(pSC->ppTempFile[i]) == -1)
  596. return ICERR_ERROR;
  597. #endif
  598. }
  599. if (*(pSC->ppWStream + i))
  600. free(*(pSC->ppWStream + i));
  601. }
  602. if(pSC->ppTempFile){
  603. if(pSC->ppTempFile[i])
  604. free(pSC->ppTempFile[i]);
  605. }
  606. }
  607. if(pSC->ppTempFile)
  608. free(pSC->ppTempFile);
  609. }
  610. else{
  611. for(i = 0; i < pSC->cNumBitIO; i ++){
  612. if(pSC->ppWStream && pSC->ppWStream[i])
  613. pSC->ppWStream[i]->Close(pSC->ppWStream + i);
  614. }
  615. }
  616. free(pSC->ppWStream);
  617. free(pSC->m_ppBitIO);
  618. free(pSC->pIndexTable);
  619. }
  620. return 0;
  621. }
  622. /*************************************************************************
  623. Write header of image plane
  624. *************************************************************************/
  625. Int WriteImagePlaneHeader(CWMImageStrCodec * pSC)
  626. {
  627. CWMImageInfo * pII = &pSC->WMII;
  628. CWMIStrCodecParam * pSCP = &pSC->WMISCP;
  629. BitIOInfo* pIO = pSC->pIOHeader;
  630. PUTBITS(pIO, (Int) pSC->m_param.cfColorFormat, 3); // internal color format
  631. PUTBITS(pIO, (Int) pSC->m_param.bScaledArith, 1); // lossless mode
  632. // subbands
  633. PUTBITS(pIO, (U32)pSCP->sbSubband, 4);
  634. // color parameters
  635. switch (pSC->m_param.cfColorFormat) {
  636. case YUV_420:
  637. case YUV_422:
  638. case YUV_444:
  639. PUTBITS(pIO, 0, 4);
  640. PUTBITS(pIO, 0, 4);
  641. break;
  642. case NCOMPONENT:
  643. PUTBITS(pIO, (Int) pSC->m_param.cNumChannels - 1, 4);
  644. PUTBITS(pIO, 0, 4);
  645. break;
  646. default:
  647. break;
  648. }
  649. // float and 32s additional parameters
  650. switch (pII->bdBitDepth) {
  651. case BD_16:
  652. case BD_16S:
  653. PUTBITS(pIO, pSCP->nLenMantissaOrShift, 8);
  654. break;
  655. case BD_32:
  656. case BD_32S:
  657. if(pSCP->nLenMantissaOrShift == 0)
  658. pSCP->nLenMantissaOrShift = 10;//default
  659. PUTBITS(pIO, pSCP->nLenMantissaOrShift, 8);
  660. break;
  661. case BD_32F:
  662. if(pSCP->nLenMantissaOrShift == 0)
  663. pSCP->nLenMantissaOrShift = 13;//default
  664. PUTBITS(pIO, pSCP->nLenMantissaOrShift, 8);//float conversion parameters
  665. PUTBITS(pIO, pSCP->nExpBias, 8);
  666. break;
  667. default:
  668. break;
  669. }
  670. // quantization
  671. PUTBITS(pIO, (pSC->m_param.uQPMode & 1) == 1 ? 0 : 1, 1); // DC frame uniform quantization?
  672. if((pSC->m_param.uQPMode & 1) == 0)
  673. writeQuantizer(pSC->pTile[0].pQuantizerDC, pIO, (pSC->m_param.uQPMode >> 3) & 3, pSC->m_param.cNumChannels, 0);
  674. if(pSC->WMISCP.sbSubband != SB_DC_ONLY){
  675. PUTBITS(pIO, (pSC->m_param.uQPMode & 0x200) == 0 ? 1 : 0, 1); // use DC quantization?
  676. if((pSC->m_param.uQPMode & 0x200) != 0){
  677. PUTBITS(pIO, (pSC->m_param.uQPMode & 2) == 2 ? 0 : 1, 1); // LP frame uniform quantization?
  678. if((pSC->m_param.uQPMode & 2) == 0)
  679. writeQuantizer(pSC->pTile[0].pQuantizerLP, pIO, (pSC->m_param.uQPMode >> 5) & 3, pSC->m_param.cNumChannels, 0);
  680. }
  681. if(pSC->WMISCP.sbSubband != SB_NO_HIGHPASS){
  682. PUTBITS(pIO, (pSC->m_param.uQPMode & 0x400) == 0 ? 1 : 0, 1); // use LP quantization?
  683. if((pSC->m_param.uQPMode & 0x400) != 0){
  684. PUTBITS(pIO, (pSC->m_param.uQPMode & 4) == 4 ? 0 : 1, 1); // HP frame uniform quantization?
  685. if((pSC->m_param.uQPMode & 4) == 0)
  686. writeQuantizer(pSC->pTile[0].pQuantizerHP, pIO, (pSC->m_param.uQPMode >> 7) & 3, pSC->m_param.cNumChannels, 0);
  687. }
  688. }
  689. }
  690. fillToByte(pIO); // remove this later
  691. return ICERR_OK;
  692. }
  693. /*************************************************************************
  694. Write header to buffer
  695. *************************************************************************/
  696. Int WriteWMIHeader(CWMImageStrCodec * pSC)
  697. {
  698. CWMImageInfo * pII = &pSC->WMII;
  699. CWMIStrCodecParam * pSCP = &pSC->WMISCP;
  700. CCoreParameters * pCoreParam = &pSC->m_param;
  701. BitIOInfo* pIO = pSC->pIOHeader;
  702. U32 /*iSizeOfSize = 2,*/ i;
  703. // temporary assignments / reserved words
  704. // const Int HEADERSIZE = 0;
  705. Bool bInscribed = FALSE;
  706. Bool bAbbreviatedHeader = (((pII->cWidth + 15) / 16 > 255 || (pII->cHeight + 15) / 16 > 255) ? FALSE : TRUE);
  707. if(pCoreParam->bTranscode == FALSE)
  708. pCoreParam->cExtraPixelsTop = pCoreParam->cExtraPixelsLeft = pCoreParam->cExtraPixelsRight = pCoreParam->cExtraPixelsBottom = 0;
  709. // num of extra boundary pixels due to compressed domain processing
  710. bInscribed = (pCoreParam->cExtraPixelsTop || pCoreParam->cExtraPixelsLeft || pCoreParam->cExtraPixelsBottom || pCoreParam->cExtraPixelsRight);
  711. // 0
  712. /** signature **/
  713. for (i = 0; i < 8; PUTBITS(pSC->pIOHeader, gGDISignature[i++], 8));
  714. // 8
  715. /** codec version and subversion **/
  716. PUTBITS(pIO, CODEC_VERSION, 4); // this should be changed to "profile" in RTM
  717. if (pSC->WMISCP.bUseHardTileBoundaries)
  718. PUTBITS(pIO, CODEC_SUBVERSION_NEWSCALING_HARD_TILES, 4);
  719. else
  720. PUTBITS(pIO, CODEC_SUBVERSION_NEWSCALING_SOFT_TILES, 4);
  721. // 9 primary parameters
  722. PUTBITS(pIO, (pSCP->cNumOfSliceMinus1V || pSCP->cNumOfSliceMinus1H) ? 1 : 0, 1); // tiling present
  723. PUTBITS(pIO, (Int) pSCP->bfBitstreamFormat, 1); // bitstream layout
  724. PUTBITS(pIO, pII->oOrientation, 3); // m_iRotateFlip
  725. PUTBITS(pIO, pSC->m_param.bIndexTable, 1); // index table present
  726. PUTBITS(pIO, pSCP->olOverlap, 2); // overlap
  727. // 10
  728. PUTBITS(pIO, bAbbreviatedHeader, 1); // short words for size and tiles
  729. PUTBITS(pIO, 1, 1); // long word length (use intelligence later)
  730. PUTBITS(pIO, bInscribed, 1); // windowing
  731. PUTBITS(pIO, pSC->m_param.bTrimFlexbitsFlag, 1); // trim flexbits flag sent
  732. PUTBITS(pIO, 0, 1); // tile stretching parameters (not enabled)
  733. PUTBITS(pIO, 0, 2); // reserved bits
  734. PUTBITS(pIO, (Int) pSC->m_param.bAlphaChannel, 1); // alpha channel present
  735. // 11 - informational
  736. PUTBITS(pIO, (Int) pII->cfColorFormat, 4); // source color format
  737. if(BD_1 == pII->bdBitDepth && pSCP->bBlackWhite)
  738. PUTBITS(pIO, (Int) BD_1alt, 4); // source bit depth
  739. else
  740. PUTBITS(pIO, (Int) pII->bdBitDepth, 4); // source bit depth
  741. // 12 - Variable length fields
  742. // size
  743. putBit32(pIO, (U32)(pII->cWidth - 1), bAbbreviatedHeader ? 16 : 32);
  744. putBit32(pIO, (U32)(pII->cHeight - 1), bAbbreviatedHeader ? 16 : 32);
  745. // tiling
  746. if (pSCP->cNumOfSliceMinus1V || pSCP->cNumOfSliceMinus1H) {
  747. PUTBITS(pIO, pSCP->cNumOfSliceMinus1V, LOG_MAX_TILES); // # of vertical slices
  748. PUTBITS(pIO, pSCP->cNumOfSliceMinus1H, LOG_MAX_TILES); // # of horizontal slices
  749. }
  750. // tile sizes
  751. for(i = 0; i < pSCP->cNumOfSliceMinus1V; i ++){ // width in MB of vertical slices, not needed for last slice!
  752. PUTBITS(pIO, pSCP->uiTileX[i + 1] - pSCP->uiTileX[i], bAbbreviatedHeader ? 8 : 16);
  753. }
  754. for(i = 0; i < pSCP->cNumOfSliceMinus1H; i ++){ // width in MB of horizontal slices, not needed for last slice!
  755. PUTBITS(pIO, pSCP->uiTileY[i + 1] - pSCP->uiTileY[i], bAbbreviatedHeader ? 8 : 16);
  756. }
  757. // window due to compressed domain processing
  758. if (bInscribed) {
  759. PUTBITS(pIO, (U32)pCoreParam->cExtraPixelsTop, 6);
  760. PUTBITS(pIO, (U32)pCoreParam->cExtraPixelsLeft, 6);
  761. PUTBITS(pIO, (U32)pCoreParam->cExtraPixelsBottom, 6);
  762. PUTBITS(pIO, (U32)pCoreParam->cExtraPixelsRight, 6);
  763. }
  764. fillToByte(pIO); // redundant
  765. // write image plane headers
  766. WriteImagePlaneHeader(pSC);
  767. return ICERR_OK;
  768. }
  769. // streaming codec init/term
  770. Int StrEncInit(CWMImageStrCodec* pSC)
  771. {
  772. COLORFORMAT cf = pSC->m_param.cfColorFormat;
  773. COLORFORMAT cfE = pSC->WMII.cfColorFormat;
  774. U16 iQPIndexY = 0, iQPIndexYLP = 0, iQPIndexYHP = 0;
  775. U16 iQPIndexU = 0, iQPIndexULP = 0, iQPIndexUHP = 0;
  776. U16 iQPIndexV = 0, iQPIndexVLP = 0, iQPIndexVHP = 0;
  777. size_t i;
  778. Bool b32bit = sizeof(size_t) == 4;
  779. /** color transcoding with resolution change **/
  780. pSC->m_bUVResolutionChange = (((cfE == CF_RGB || cfE == YUV_444 || cfE == CMYK || cfE == CF_RGBE) &&
  781. (cf == YUV_422 || cf == YUV_420))
  782. || (cfE == YUV_422 && cf == YUV_420)) && !pSC->WMISCP.bYUVData;
  783. if(pSC->m_bUVResolutionChange){
  784. size_t cSize = ((cfE == YUV_422 ? 128 : 256) + (cf == YUV_420 ? 32 : 0)) * pSC->cmbWidth + 256;
  785. if(b32bit){ // integer overlow/underflow check for 32-bit system
  786. if(((pSC->cmbWidth >> 16) * ((cfE == YUV_422 ? 128 : 256) + (cf == YUV_420 ? 32 : 0))) & 0xffff0000)
  787. return ICERR_ERROR;
  788. if(cSize >= 0x3fffffff)
  789. return ICERR_ERROR;
  790. }
  791. pSC->pResU = (PixelI *)malloc(cSize * sizeof(PixelI));
  792. pSC->pResV = (PixelI *)malloc(cSize * sizeof(PixelI));
  793. if(pSC->pResU == NULL || pSC->pResV == NULL){
  794. return ICERR_ERROR;
  795. }
  796. }
  797. pSC->cTileColumn = pSC->cTileRow = 0;
  798. if(allocateTileInfo(pSC) != ICERR_OK)
  799. return ICERR_ERROR;
  800. if(pSC->m_param.bTranscode == FALSE){
  801. pSC->m_param.uQPMode = 0x150; // 101010 000
  802. // 000 == uniform (not per tile) DC, LP, HP
  803. // 101010 == cChMode == 2 == independent (not same) DC, LP, HP
  804. /** lossless or Y component lossless condition: all subbands present, uniform quantization with QPIndex 1 **/
  805. pSC->m_param.bScaledArith = !((pSC->m_param.uQPMode & 7) == 0 &&
  806. 1 == pSC->WMISCP.uiDefaultQPIndex <= 1 &&
  807. pSC->WMISCP.sbSubband == SB_ALL &&
  808. pSC->m_bUVResolutionChange == FALSE) &&
  809. !pSC->WMISCP.bUnscaledArith;
  810. if (BD_32 == pSC->WMII.bdBitDepth || BD_32S == pSC->WMII.bdBitDepth || BD_32F == pSC->WMII.bdBitDepth) {
  811. pSC->m_param.bScaledArith = FALSE;
  812. }
  813. pSC->m_param.uQPMode |= 0x600; // don't use DC QP for LP, LP QP for HP
  814. // default QPs
  815. iQPIndexY = pSC->m_param.bAlphaChannel && pSC->m_param.cNumChannels == 1?
  816. pSC->WMISCP.uiDefaultQPIndexAlpha : pSC->WMISCP.uiDefaultQPIndex;
  817. // determine the U,V index
  818. iQPIndexU = pSC->WMISCP.uiDefaultQPIndexU!=0?
  819. pSC->WMISCP.uiDefaultQPIndexU: iQPIndexY;
  820. iQPIndexV = pSC->WMISCP.uiDefaultQPIndexV!=0?
  821. pSC->WMISCP.uiDefaultQPIndexV: iQPIndexY;
  822. // determine the QPIndexYLP
  823. iQPIndexYLP = pSC->m_param.bAlphaChannel && pSC->m_param.cNumChannels == 1 ?
  824. pSC->WMISCP.uiDefaultQPIndexAlpha :
  825. (pSC->WMISCP.uiDefaultQPIndexYLP == 0 ?
  826. pSC->WMISCP.uiDefaultQPIndex : pSC->WMISCP.uiDefaultQPIndexYLP); // default to QPIndex if not set
  827. // determine the QPIndexYHP
  828. iQPIndexYHP = pSC->m_param.bAlphaChannel && pSC->m_param.cNumChannels == 1 ?
  829. pSC->WMISCP.uiDefaultQPIndexAlpha :
  830. (pSC->WMISCP.uiDefaultQPIndexYHP == 0 ?
  831. pSC->WMISCP.uiDefaultQPIndex : pSC->WMISCP.uiDefaultQPIndexYHP); // default to QPIndex if not set
  832. // determine the U,V LP index
  833. iQPIndexULP = pSC->WMISCP.uiDefaultQPIndexULP!=0?
  834. pSC->WMISCP.uiDefaultQPIndexULP: iQPIndexU;
  835. iQPIndexVLP = pSC->WMISCP.uiDefaultQPIndexVLP!=0?
  836. pSC->WMISCP.uiDefaultQPIndexVLP: iQPIndexV;
  837. // determine the U,V HP index
  838. iQPIndexUHP = pSC->WMISCP.uiDefaultQPIndexUHP!=0?
  839. pSC->WMISCP.uiDefaultQPIndexUHP: iQPIndexU;
  840. iQPIndexVHP = pSC->WMISCP.uiDefaultQPIndexVHP!=0?
  841. pSC->WMISCP.uiDefaultQPIndexVHP: iQPIndexV;
  842. // clamp the QPIndex - 0 is lossless mode
  843. if(iQPIndexY < 2)
  844. iQPIndexY = 0;
  845. if (iQPIndexYLP < 2)
  846. iQPIndexYLP = 0;
  847. if (iQPIndexYHP < 2)
  848. iQPIndexYHP = 0;
  849. if(iQPIndexU < 2)
  850. iQPIndexU = 0;
  851. if (iQPIndexULP < 2)
  852. iQPIndexULP = 0;
  853. if (iQPIndexUHP < 2)
  854. iQPIndexUHP = 0;
  855. if(iQPIndexV < 2)
  856. iQPIndexV = 0;
  857. if (iQPIndexVLP < 2)
  858. iQPIndexVLP = 0;
  859. if (iQPIndexVHP < 2)
  860. iQPIndexVHP = 0;
  861. }
  862. if((pSC->m_param.uQPMode & 1) == 0){ // DC frame uniform quantization
  863. if(allocateQuantizer(pSC->pTile[0].pQuantizerDC, pSC->m_param.cNumChannels, 1) != ICERR_OK)
  864. return ICERR_ERROR;
  865. setUniformQuantizer(pSC, 0);
  866. for(i = 0; i < pSC->m_param.cNumChannels; i ++)
  867. if(pSC->m_param.bTranscode)
  868. pSC->pTile[0].pQuantizerDC[i]->iIndex = pSC->m_param.uiQPIndexDC[i];
  869. else
  870. pSC->pTile[0].pQuantizerDC[i]->iIndex = pSC->m_param.uiQPIndexDC[i] = (U8)(((i == 0 ? iQPIndexY : (i == 1) ? iQPIndexU: iQPIndexV)) & 0xff);
  871. formatQuantizer(pSC->pTile[0].pQuantizerDC, (pSC->m_param.uQPMode >> 3) & 3, pSC->m_param.cNumChannels, 0, TRUE, pSC->m_param.bScaledArith);
  872. for(i = 0; i < pSC->m_param.cNumChannels; i ++)
  873. pSC->pTile[0].pQuantizerDC[i]->iOffset = (pSC->pTile[0].pQuantizerDC[i]->iQP >> 1);
  874. }
  875. if(pSC->WMISCP.sbSubband != SB_DC_ONLY){
  876. if((pSC->m_param.uQPMode & 2) == 0){ // LP frame uniform quantization
  877. if(allocateQuantizer(pSC->pTile[0].pQuantizerLP, pSC->m_param.cNumChannels, 1) != ICERR_OK)
  878. return ICERR_ERROR;
  879. setUniformQuantizer(pSC, 1);
  880. for(i = 0; i < pSC->m_param.cNumChannels; i ++)
  881. if(pSC->m_param.bTranscode)
  882. pSC->pTile[0].pQuantizerLP[i]->iIndex = pSC->m_param.uiQPIndexLP[i];
  883. else
  884. pSC->pTile[0].pQuantizerLP[i]->iIndex = pSC->m_param.uiQPIndexLP[i] = (U8)(((i == 0 ? iQPIndexYLP : (i == 1) ? iQPIndexULP: iQPIndexVLP)) & 0xff);
  885. formatQuantizer(pSC->pTile[0].pQuantizerLP, (pSC->m_param.uQPMode >> 5) & 3, pSC->m_param.cNumChannels, 0, TRUE, pSC->m_param.bScaledArith);
  886. }
  887. if(pSC->WMISCP.sbSubband != SB_NO_HIGHPASS){
  888. if((pSC->m_param.uQPMode & 4) == 0){ // HP frame uniform quantization
  889. if(allocateQuantizer(pSC->pTile[0].pQuantizerHP, pSC->m_param.cNumChannels, 1) != ICERR_OK)
  890. return ICERR_ERROR;
  891. setUniformQuantizer(pSC, 2);
  892. for(i = 0; i < pSC->m_param.cNumChannels; i ++)
  893. if(pSC->m_param.bTranscode)
  894. pSC->pTile[0].pQuantizerHP[i]->iIndex = pSC->m_param.uiQPIndexHP[i];
  895. else
  896. pSC->pTile[0].pQuantizerHP[i]->iIndex = pSC->m_param.uiQPIndexHP[i] = (U8)(((i == 0 ? iQPIndexYHP : (i == 1) ? iQPIndexUHP: iQPIndexVHP)) & 0xff);
  897. formatQuantizer(pSC->pTile[0].pQuantizerHP, (pSC->m_param.uQPMode >> 7) & 3, pSC->m_param.cNumChannels, 0, FALSE, pSC->m_param.bScaledArith);
  898. }
  899. }
  900. }
  901. if(allocatePredInfo(pSC) != ICERR_OK){
  902. return ICERR_ERROR;
  903. }
  904. if(pSC->WMISCP.cNumOfSliceMinus1V >= MAX_TILES || AllocateCodingContextEnc (pSC, pSC->WMISCP.cNumOfSliceMinus1V + 1, pSC->WMISCP.uiTrimFlexBits) != ICERR_OK){
  905. return ICERR_ERROR;
  906. }
  907. if (pSC->m_bSecondary) {
  908. pSC->pIOHeader = pSC->m_pNextSC->pIOHeader;
  909. pSC->m_ppBitIO = pSC->m_pNextSC->m_ppBitIO;
  910. pSC->cNumBitIO = pSC->m_pNextSC->cNumBitIO;
  911. pSC->cSB = pSC->m_pNextSC->cSB;
  912. pSC->ppWStream = pSC->m_pNextSC->ppWStream;
  913. pSC->pIndexTable = pSC->m_pNextSC->pIndexTable;
  914. setBitIOPointers(pSC);
  915. }
  916. else {
  917. StrIOEncInit(pSC);
  918. setBitIOPointers(pSC);
  919. WriteWMIHeader(pSC);
  920. }
  921. return ICERR_OK;
  922. }
  923. static Int StrEncTerm(CTXSTRCODEC ctxSC)
  924. {
  925. CWMImageStrCodec* pSC = (CWMImageStrCodec*)ctxSC;
  926. size_t j, jend = (pSC->m_pNextSC != NULL);
  927. for (j = 0; j <= jend; j++) {
  928. if (sizeof(*pSC) != pSC->cbStruct) {
  929. return ICERR_ERROR;
  930. }
  931. if(pSC->m_bUVResolutionChange){
  932. if(pSC->pResU != NULL)
  933. free(pSC->pResU);
  934. if(pSC->pResV != NULL)
  935. free(pSC->pResV);
  936. }
  937. freePredInfo(pSC);
  938. if (j == 0)
  939. StrIOEncTerm(pSC);
  940. FreeCodingContextEnc(pSC);
  941. freeTileInfo(pSC);
  942. pSC->WMISCP.nExpBias -= 128; // reset
  943. pSC = pSC->m_pNextSC;
  944. }
  945. return 0;
  946. }
  947. U32 setUniformTiling(U32 * pTile, U32 cNumTile, U32 cNumMB)
  948. {
  949. U32 i, j;
  950. while((cNumMB + cNumTile - 1) / cNumTile > 65535) // too few tiles
  951. cNumTile ++;
  952. for(i = cNumTile, j = cNumMB; i > 1; i --){
  953. pTile[cNumTile - i] = (j + i - 1) / i;
  954. j -= pTile[cNumTile - i];
  955. }
  956. return cNumTile;
  957. }
  958. U32 validateTiling(U32 * pTile, U32 cNumTile, U32 cNumMB)
  959. {
  960. U32 i, cMBs;
  961. if(cNumTile == 0)
  962. cNumTile = 1;
  963. if(cNumTile > cNumMB) // too many tiles
  964. cNumTile = 1;
  965. if(cNumTile > MAX_TILES)
  966. cNumTile = MAX_TILES;
  967. for(i = cMBs = 0; i + 1 < cNumTile; i ++){
  968. if(pTile[i] == 0 || pTile[i] > 65535){ // invalid tile setting, resetting to uniform tiling
  969. cNumTile = setUniformTiling(pTile, cNumTile, cNumMB);
  970. break;
  971. }
  972. cMBs += pTile[i];
  973. if(cMBs >= cNumMB){
  974. cNumTile = i + 1;
  975. break;
  976. }
  977. }
  978. // last tile
  979. if(cNumMB - cMBs > 65536)
  980. cNumTile = setUniformTiling(pTile, cNumTile, cNumMB);
  981. for(i = 1; i < cNumTile; i ++)
  982. pTile[i] += pTile[i - 1];
  983. for(i = cNumTile - 1; i > 0; i --)
  984. pTile[i] = pTile[i - 1];
  985. pTile[0] = 0;
  986. return cNumTile;
  987. }
  988. /*************************************************************************
  989. Validate and adjust input params here
  990. *************************************************************************/
  991. Int ValidateArgs(CWMImageInfo* pII, CWMIStrCodecParam *pSCP)
  992. {
  993. int i;
  994. Bool bTooNarrowTile = FALSE;
  995. if(pII->cWidth > (1 << 28) || pII->cHeight > (1 << 28) || pII->cWidth == 0 || pII->cHeight == 0){
  996. printf("Unsurpported image size!\n");
  997. return ICERR_ERROR; // unsurpported image size
  998. }
  999. if (((pSCP->cfColorFormat == YUV_420) || (pSCP->cfColorFormat == YUV_422)) && (pSCP->olOverlap == OL_TWO) && ((Int)(((U32)pII->cWidth + 15) >> 4) < 2)) {
  1000. printf("Image width must be at least 2 MB wide for subsampled chroma and two levels of overlap!\n");
  1001. return ICERR_ERROR;
  1002. }
  1003. if(pSCP->sbSubband == SB_ISOLATED || pSCP->sbSubband >= SB_MAX) // not allowed
  1004. pSCP->sbSubband = SB_ALL;
  1005. if(pII->bdBitDepth == BD_5 && (pII->cfColorFormat != CF_RGB || pII->cBitsPerUnit != 16 || pII->cLeadingPadding != 0)){
  1006. printf("Unsupported BD_5 image format!\n");
  1007. return ICERR_ERROR; // BD_5 must be compact RGB!
  1008. }
  1009. if(pII->bdBitDepth == BD_565 && (pII->cfColorFormat != CF_RGB || pII->cBitsPerUnit != 16 || pII->cLeadingPadding != 0)){
  1010. printf("Unsupported BD_565 image format!\n");
  1011. return ICERR_ERROR; // BD_5 must be compact RGB!
  1012. }
  1013. if(pII->bdBitDepth == BD_10 && (pII->cfColorFormat != CF_RGB || pII->cBitsPerUnit != 32 || pII->cLeadingPadding != 0)){
  1014. printf("Unsupported BD_10 image format!\n");
  1015. return ICERR_ERROR; // BD_10 must be compact RGB!
  1016. }
  1017. if((pII->bdBitDepth == BD_5 || pII->bdBitDepth == BD_565 || pII->bdBitDepth == BD_10) &&
  1018. (pSCP->cfColorFormat != YUV_420 && pSCP->cfColorFormat != YUV_422 && pSCP->cfColorFormat != Y_ONLY))
  1019. pSCP->cfColorFormat = YUV_444;
  1020. if(BD_1 == pII->bdBitDepth){ // binary image
  1021. if(pII->cfColorFormat != Y_ONLY){
  1022. printf("BD_1 image must be black-and white!\n");
  1023. return ICERR_ERROR;
  1024. }
  1025. pSCP->cfColorFormat = Y_ONLY; // can only be black white
  1026. }
  1027. if(pSCP->bdBitDepth != BD_LONG)
  1028. pSCP->bdBitDepth = BD_LONG; // currently only support 32 bit internally
  1029. if(pSCP->uAlphaMode > 1 && (pII->cfColorFormat == YUV_420 || pII->cfColorFormat == YUV_422
  1030. || pII->bdBitDepth == BD_5 || pII->bdBitDepth == BD_10
  1031. || pII->bdBitDepth == BD_1))
  1032. {
  1033. printf("Alpha is not supported for this pixel format!\n");
  1034. return ICERR_ERROR;
  1035. }
  1036. if((pSCP->cfColorFormat == YUV_420 || pSCP->cfColorFormat == YUV_422) && (pII->bdBitDepth == BD_16F || pII->bdBitDepth == BD_32F || pII->cfColorFormat == CF_RGBE))
  1037. {
  1038. printf("Float or RGBE images must be encoded with YUV 444!\n");
  1039. return ICERR_ERROR;
  1040. }
  1041. // adjust tiling
  1042. pSCP->cNumOfSliceMinus1V = validateTiling(pSCP->uiTileX, pSCP->cNumOfSliceMinus1V + 1, (((U32)pII->cWidth + 15) >> 4)) - 1;
  1043. pSCP->cNumOfSliceMinus1H = validateTiling(pSCP->uiTileY, pSCP->cNumOfSliceMinus1H + 1, (((U32)pII->cHeight + 15) >> 4)) - 1;
  1044. if (pSCP->bUseHardTileBoundaries && ((pSCP->cfColorFormat == YUV_420) || (pSCP->cfColorFormat == YUV_422)) && (pSCP->olOverlap == OL_TWO)) {
  1045. for (i = 1; i < (int) (pSCP->cNumOfSliceMinus1H + 1); i++) {
  1046. if ((Int)(pSCP->uiTileY[i] - pSCP->uiTileY[i - 1]) < 2) {
  1047. bTooNarrowTile = TRUE;
  1048. break;
  1049. }
  1050. }
  1051. if ((Int)((((U32)pII->cWidth + 15) >> 4) - pSCP->uiTileY[pSCP->cNumOfSliceMinus1H]) < 2)
  1052. bTooNarrowTile = TRUE;
  1053. }
  1054. if (bTooNarrowTile) {
  1055. printf("Tile width must be at least 2 MB wide for hard tiles, subsampled chroma, and two levels of overlap!\n");
  1056. return ICERR_ERROR;
  1057. }
  1058. if(pSCP->cChannel > MAX_CHANNELS)
  1059. return ICERR_ERROR;
  1060. /** supported color transcoding **/
  1061. /** ARGB, RGB => YUV_444, YUV_422, YUV_420, Y_ONLY **/
  1062. /** YUV_444 => YUV_422, YUV_420, Y_ONLY **/
  1063. /** YUV_422 => YUV_420, Y_ONLY **/
  1064. /** YUV_420 => Y_ONLY **/
  1065. /** unsupported color transcoding **/
  1066. /** Y_ONLY, YUV_420, YUV_422 => YUV_444 **/
  1067. /** Y_ONLY, YUV_420 => YUV_422 **/
  1068. /** Y_ONLY => YUV_420 **/
  1069. if((pII->cfColorFormat == Y_ONLY && pSCP->cfColorFormat != Y_ONLY) ||
  1070. (pSCP->cfColorFormat == YUV_422 && (pII->cfColorFormat == YUV_420 || pII->cfColorFormat == Y_ONLY)) ||
  1071. (pSCP->cfColorFormat == YUV_444 && (pII->cfColorFormat == YUV_422 || pII->cfColorFormat == YUV_420 || pII->cfColorFormat == Y_ONLY))){
  1072. pSCP->cfColorFormat = pII->cfColorFormat; // force not to do color transcoding!
  1073. }
  1074. else if (pII->cfColorFormat == NCOMPONENT) {
  1075. pSCP->cfColorFormat = NCOMPONENT; // force not to do color transcoding!
  1076. }
  1077. if (CMYK == pII->cfColorFormat && pSCP->cfColorFormat == NCOMPONENT)
  1078. {
  1079. pSCP->cfColorFormat = CMYK;
  1080. }
  1081. if(pSCP->cfColorFormat != NCOMPONENT){
  1082. if(pSCP->cfColorFormat == Y_ONLY)
  1083. pSCP->cChannel = 1;
  1084. else if(pSCP->cfColorFormat == CMYK)
  1085. pSCP->cChannel = 4;
  1086. else
  1087. pSCP->cChannel = 3;
  1088. }
  1089. if(pSCP->sbSubband >= SB_MAX)
  1090. pSCP->sbSubband = SB_ALL;
  1091. pII->cChromaCenteringX = 0;
  1092. pII->cChromaCenteringY = 0;
  1093. return ICERR_OK;
  1094. }
  1095. /*************************************************************************
  1096. Initialization of CWMImageStrCodec struct
  1097. *************************************************************************/
  1098. static Void InitializeStrEnc(CWMImageStrCodec *pSC,
  1099. const CWMImageInfo* pII, const CWMIStrCodecParam *pSCP)
  1100. {
  1101. pSC->cbStruct = sizeof(*pSC);
  1102. pSC->WMII = *pII;
  1103. pSC->WMISCP = *pSCP;
  1104. // set nExpBias
  1105. if (pSC->WMISCP.nExpBias == 0)
  1106. pSC->WMISCP.nExpBias = 4 + 128;//default
  1107. pSC->WMISCP.nExpBias += 128; // rollover arithmetic
  1108. pSC->cRow = 0;
  1109. pSC->cColumn = 0;
  1110. pSC->cmbWidth = (pSC->WMII.cWidth + 15) / 16;
  1111. pSC->cmbHeight = (pSC->WMII.cHeight + 15) / 16;
  1112. pSC->Load = inputMBRow;
  1113. pSC->Quantize = quantizeMacroblock;
  1114. pSC->ProcessTopLeft = processMacroblock;
  1115. pSC->ProcessTop = processMacroblock;
  1116. pSC->ProcessTopRight = processMacroblock;
  1117. pSC->ProcessLeft = processMacroblock;
  1118. pSC->ProcessCenter = processMacroblock;
  1119. pSC->ProcessRight = processMacroblock;
  1120. pSC->ProcessBottomLeft = processMacroblock;
  1121. pSC->ProcessBottom = processMacroblock;
  1122. pSC->ProcessBottomRight = processMacroblock;
  1123. pSC->m_pNextSC = NULL;
  1124. pSC->m_bSecondary = FALSE;
  1125. }
  1126. /*************************************************************************
  1127. Streaming API init
  1128. *************************************************************************/
  1129. Int ImageStrEncInit(
  1130. CWMImageInfo* pII,
  1131. CWMIStrCodecParam *pSCP,
  1132. CTXSTRCODEC* pctxSC)
  1133. {
  1134. static size_t cbChannels[BD_MAX] = {2, 4};
  1135. size_t cbChannel = 0, cblkChroma = 0, i;
  1136. size_t cbMacBlockStride = 0, cbMacBlockChroma = 0, cMacBlock = 0;
  1137. CWMImageStrCodec* pSC = NULL, *pNextSC = NULL;
  1138. char* pb = NULL;
  1139. size_t cb = 0;
  1140. Bool b32bit = sizeof(size_t) == 4;
  1141. Int err;
  1142. if(ValidateArgs(pII, pSCP) != ICERR_OK){
  1143. goto ErrorExit;
  1144. }
  1145. //================================================
  1146. *pctxSC = NULL;
  1147. //================================================
  1148. cbChannel = cbChannels[pSCP->bdBitDepth];
  1149. cblkChroma = cblkChromas[pSCP->cfColorFormat];
  1150. cbMacBlockStride = cbChannel * 16 * 16;
  1151. cbMacBlockChroma = cbChannel * 16 * cblkChroma;
  1152. cMacBlock = (pII->cWidth + 15) / 16;
  1153. //================================================
  1154. cb = sizeof(*pSC) + (128 - 1) + (PACKETLENGTH * 4 - 1) + (PACKETLENGTH * 2 ) + sizeof(*pSC->pIOHeader);
  1155. i = cbMacBlockStride + cbMacBlockChroma * (pSCP->cChannel - 1);
  1156. if(b32bit) // integer overlow/underflow check for 32-bit system
  1157. if(((cMacBlock >> 15) * i) & 0xffff0000)
  1158. return ICERR_ERROR;
  1159. i *= cMacBlock * 2;
  1160. cb += i;
  1161. pb = malloc(cb);
  1162. if (NULL == pb)
  1163. {
  1164. goto ErrorExit;
  1165. }
  1166. memset(pb, 0, cb);
  1167. //================================================
  1168. pSC = (CWMImageStrCodec*)pb; pb += sizeof(*pSC);
  1169. // Set up perf timers
  1170. PERFTIMER_ONLY(pSC->m_fMeasurePerf = pSCP->fMeasurePerf);
  1171. PERFTIMER_NEW(pSC->m_fMeasurePerf, &pSC->m_ptEndToEndPerf);
  1172. PERFTIMER_NEW(pSC->m_fMeasurePerf, &pSC->m_ptEncDecPerf);
  1173. PERFTIMER_START(pSC->m_fMeasurePerf, pSC->m_ptEndToEndPerf);
  1174. PERFTIMER_START(pSC->m_fMeasurePerf, pSC->m_ptEncDecPerf);
  1175. PERFTIMER_COPYSTARTTIME(pSC->m_fMeasurePerf, pSC->m_ptEncDecPerf, pSC->m_ptEndToEndPerf);
  1176. pSC->m_param.cfColorFormat = pSCP->cfColorFormat;
  1177. pSC->m_param.bAlphaChannel = (pSCP->uAlphaMode == 3);
  1178. pSC->m_param.cNumChannels = pSCP->cChannel;
  1179. pSC->m_param.cExtraPixelsTop = pSC->m_param.cExtraPixelsBottom
  1180. = pSC->m_param.cExtraPixelsLeft = pSC->m_param.cExtraPixelsRight = 0;
  1181. pSC->cbChannel = cbChannel;
  1182. pSC->m_param.bTranscode = pSC->bTileExtraction = FALSE;
  1183. //================================================
  1184. InitializeStrEnc(pSC, pII, pSCP);
  1185. //================================================
  1186. // 2 Macro Row buffers for each channel
  1187. pb = ALIGNUP(pb, 128);
  1188. for (i = 0; i < pSC->m_param.cNumChannels; i++) {
  1189. pSC->a0MBbuffer[i] = (PixelI*)pb; pb += cbMacBlockStride * pSC->cmbWidth;
  1190. pSC->a1MBbuffer[i] = (PixelI*)pb; pb += cbMacBlockStride * pSC->cmbWidth;
  1191. cbMacBlockStride = cbMacBlockChroma;
  1192. }
  1193. //================================================
  1194. // lay 2 aligned IO buffers just below pIO struct
  1195. pb = (char*)ALIGNUP(pb, PACKETLENGTH * 4) + PACKETLENGTH * 2;
  1196. pSC->pIOHeader = (BitIOInfo*)pb;
  1197. //================================================
  1198. err = StrEncInit(pSC);
  1199. if (ICERR_OK != err)
  1200. goto ErrorExit;
  1201. // if interleaved alpha is needed
  1202. if (pSC->m_param.bAlphaChannel) {
  1203. cbMacBlockStride = cbChannel * 16 * 16;
  1204. // 1. allocate new pNextSC info
  1205. //================================================
  1206. cb = sizeof(*pNextSC) + (128 - 1) + cbMacBlockStride * cMacBlock * 2;
  1207. pb = malloc(cb);
  1208. if (NULL == pb)
  1209. {
  1210. goto ErrorExit;
  1211. }
  1212. memset(pb, 0, cb);
  1213. //================================================
  1214. pNextSC = (CWMImageStrCodec*)pb; pb += sizeof(*pNextSC);
  1215. // 2. initialize pNextSC
  1216. pNextSC->m_param.cfColorFormat = Y_ONLY;
  1217. pNextSC->m_param.cNumChannels = 1;
  1218. pNextSC->m_param.bAlphaChannel = TRUE;
  1219. pNextSC->cbChannel = cbChannel;
  1220. //================================================
  1221. // 3. initialize arrays
  1222. InitializeStrEnc(pNextSC, pII, pSCP);
  1223. //================================================
  1224. // 2 Macro Row buffers for each channel
  1225. pb = ALIGNUP(pb, 128);
  1226. pNextSC->a0MBbuffer[0] = (PixelI*)pb; pb += cbMacBlockStride * pNextSC->cmbWidth;
  1227. pNextSC->a1MBbuffer[0] = (PixelI*)pb; pb += cbMacBlockStride * pNextSC->cmbWidth;
  1228. //================================================
  1229. pNextSC->pIOHeader = pSC->pIOHeader;
  1230. //================================================
  1231. // 4. link pSC->pNextSC = pNextSC
  1232. pNextSC->m_pNextSC = pSC;
  1233. pNextSC->m_bSecondary = TRUE;
  1234. // 5. StrEncInit
  1235. StrEncInit(pNextSC);
  1236. // 6. Write header of image plane
  1237. WriteImagePlaneHeader(pNextSC);
  1238. }
  1239. pSC->m_pNextSC = pNextSC;
  1240. //================================================
  1241. *pctxSC = (CTXSTRCODEC)pSC;
  1242. writeIndexTableNull(pSC);
  1243. #if defined(WMP_OPT_SSE2) || defined(WMP_OPT_CC_ENC) || defined(WMP_OPT_TRFM_ENC)
  1244. StrEncOpt(pSC);
  1245. #endif // OPT defined
  1246. PERFTIMER_STOP(pSC->m_fMeasurePerf, pSC->m_ptEncDecPerf);
  1247. return ICERR_OK;
  1248. ErrorExit:
  1249. return ICERR_ERROR;
  1250. }
  1251. /*************************************************************************
  1252. Streaming API encode
  1253. *************************************************************************/
  1254. Int ImageStrEncEncode(
  1255. CTXSTRCODEC ctxSC,
  1256. const CWMImageBufferInfo* pBI)
  1257. {
  1258. CWMImageStrCodec* pSC = (CWMImageStrCodec*)ctxSC;
  1259. CWMImageStrCodec* pNextSC = pSC->m_pNextSC;
  1260. ImageDataProc ProcessLeft, ProcessCenter, ProcessRight;
  1261. if (sizeof(*pSC) != pSC->cbStruct)
  1262. {
  1263. return ICERR_ERROR;
  1264. }
  1265. //================================
  1266. PERFTIMER_START(pSC->m_fMeasurePerf, pSC->m_ptEncDecPerf);
  1267. pSC->WMIBI = *pBI;
  1268. pSC->cColumn = 0;
  1269. initMRPtr(pSC);
  1270. if (pNextSC)
  1271. pNextSC->WMIBI = *pBI;
  1272. if (0 == pSC->cRow) {
  1273. ProcessLeft = pSC->ProcessTopLeft;
  1274. ProcessCenter = pSC->ProcessTop;
  1275. ProcessRight = pSC->ProcessTopRight;
  1276. }
  1277. else {
  1278. ProcessLeft = pSC->ProcessLeft;
  1279. ProcessCenter = pSC->ProcessCenter;
  1280. ProcessRight = pSC->ProcessRight;
  1281. }
  1282. if( pSC->Load(pSC) != ICERR_OK )
  1283. return ICERR_ERROR;
  1284. if(ProcessLeft(pSC) != ICERR_OK)
  1285. return ICERR_ERROR;
  1286. advanceMRPtr(pSC);
  1287. //================================
  1288. for (pSC->cColumn = 1; pSC->cColumn < pSC->cmbWidth; ++pSC->cColumn) {
  1289. if(ProcessCenter(pSC) != ICERR_OK)
  1290. return ICERR_ERROR;
  1291. advanceMRPtr(pSC);
  1292. }
  1293. //================================
  1294. if(ProcessRight(pSC) != ICERR_OK)
  1295. return ICERR_ERROR;
  1296. if (pSC->cRow)
  1297. advanceOneMBRow(pSC);
  1298. ++pSC->cRow;
  1299. swapMRPtr(pSC);
  1300. PERFTIMER_STOP(pSC->m_fMeasurePerf, pSC->m_ptEncDecPerf);
  1301. return ICERR_OK;
  1302. }
  1303. /*************************************************************************
  1304. Streaming API term
  1305. *************************************************************************/
  1306. Int ImageStrEncTerm(
  1307. CTXSTRCODEC ctxSC)
  1308. {
  1309. CWMImageStrCodec* pSC = (CWMImageStrCodec*)ctxSC;
  1310. // CWMImageStrCodec *pNextSC = pSC->m_pNextSC;
  1311. if (sizeof(*pSC) != pSC->cbStruct)
  1312. {
  1313. return ICERR_ERROR;
  1314. }
  1315. //================================
  1316. PERFTIMER_START(pSC->m_fMeasurePerf, pSC->m_ptEncDecPerf);
  1317. pSC->cColumn = 0;
  1318. initMRPtr(pSC);
  1319. pSC->ProcessBottomLeft(pSC);
  1320. advanceMRPtr(pSC);
  1321. //================================
  1322. for (pSC->cColumn = 1; pSC->cColumn < pSC->cmbWidth; ++pSC->cColumn) {
  1323. pSC->ProcessBottom(pSC);
  1324. advanceMRPtr(pSC);
  1325. }
  1326. //================================
  1327. pSC->ProcessBottomRight(pSC);
  1328. //================================
  1329. StrEncTerm(pSC);
  1330. PERFTIMER_STOP(pSC->m_fMeasurePerf, pSC->m_ptEncDecPerf);
  1331. PERFTIMER_STOP(pSC->m_fMeasurePerf, pSC->m_ptEndToEndPerf);
  1332. PERFTIMER_REPORT(pSC->m_fMeasurePerf, pSC);
  1333. PERFTIMER_DELETE(pSC->m_fMeasurePerf, pSC->m_ptEncDecPerf);
  1334. PERFTIMER_DELETE(pSC->m_fMeasurePerf, pSC->m_ptEndToEndPerf);
  1335. free(pSC);
  1336. return ICERR_OK;
  1337. }
  1338. // centralized UV downsampling
  1339. #define DF_ODD ((((d1 + d2 + d3) << 2) + (d2 << 1) + d0 + d4 + 8) >> 4)
  1340. Void downsampleUV(CWMImageStrCodec * pSC)
  1341. {
  1342. const COLORFORMAT cfInt = pSC->m_param.cfColorFormat;
  1343. const COLORFORMAT cfExt = pSC->WMII.cfColorFormat;
  1344. PixelI * pSrc, * pDst;
  1345. PixelI d0, d1, d2, d3, d4;
  1346. size_t iChannel, iRow, iColumn;
  1347. for(iChannel = 1; iChannel < 3; iChannel ++){
  1348. if(cfExt != YUV_422){ // need to do horizontal downsampling, 444 => 422
  1349. const size_t cShift = (cfInt == YUV_422 ? 1 : 0);
  1350. pSrc = (iChannel == 1 ? pSC->pResU : pSC->pResV);
  1351. pDst = (cfInt == YUV_422 ? pSC->p1MBbuffer[iChannel] : pSrc);
  1352. for(iRow = 0; iRow < 16; iRow ++){
  1353. d0 = d4 = pSrc[idxCC[iRow][2]], d1 = d3 = pSrc[idxCC[iRow][1]], d2 = pSrc[idxCC[iRow][0]]; // left boundary
  1354. for(iColumn = 0; iColumn + 2 < pSC->cmbWidth * 16; iColumn += 2){
  1355. pDst[((iColumn >> 4) << (8 - cShift)) + idxCC[iRow][(iColumn & 15) >> cShift]] = DF_ODD;
  1356. d0 = d2, d1 = d3, d2 = d4;
  1357. d3 = pSrc[(((iColumn + 3) >> 4) << 8) + idxCC[iRow][(iColumn + 3) & 0xf]];
  1358. d4 = pSrc[(((iColumn + 4) >> 4) << 8) + idxCC[iRow][(iColumn + 4) & 0xf]];
  1359. }
  1360. d4 = d2; // right boundary
  1361. pDst[((iColumn >> 4) << (8 - cShift)) + idxCC[iRow][(iColumn & 15) >> cShift]] = DF_ODD;
  1362. }
  1363. }
  1364. if(cfInt == YUV_420){ // need to do vertical downsampling
  1365. const size_t cShift = (cfExt == YUV_422 ? 0 : 1);
  1366. PixelI * pBuf[4];
  1367. size_t mbOff, pxOff;
  1368. pDst = pSC->p1MBbuffer[iChannel];
  1369. pSrc = (iChannel == 1 ? pSC->pResU : pSC->pResV);
  1370. pBuf[0] = pSrc + (pSC->cmbWidth << (cfExt == YUV_422 ? 7 : 8));
  1371. pBuf[1] = pBuf[0] + pSC->cmbWidth * 8, pBuf[2] = pBuf[1] + pSC->cmbWidth * 8, pBuf[3] = pBuf[2] + pSC->cmbWidth * 8;
  1372. for(iColumn = 0; iColumn < pSC->cmbWidth * 8; iColumn ++){
  1373. mbOff = (iColumn >> 3) << (7 + cShift);
  1374. pxOff = (iColumn & 7) << cShift;
  1375. if(pSC->cRow == 0) // top image boundary
  1376. d0 = d4 = pSrc[mbOff + idxCC[2][pxOff]], d1 = d3 = pSrc[mbOff + idxCC[1][pxOff]], d2 = pSrc[mbOff + idxCC[0][pxOff]]; // top MB boundary
  1377. else{
  1378. // last row of previous MB row
  1379. d0 = pBuf[0][iColumn], d1 = pBuf[1][iColumn], d2 = pBuf[2][iColumn], d3 = pBuf[3][iColumn], d4 = pSrc[mbOff + idxCC[0][pxOff]];
  1380. pSC->p0MBbuffer[iChannel][((iColumn >> 3) << 6) + idxCC_420[7][iColumn & 7]] = DF_ODD;
  1381. // for first row of current MB
  1382. d0 = pBuf[2][iColumn], d1 = pBuf[3][iColumn];
  1383. d2 = pSrc[mbOff + idxCC[0][pxOff]], d3 = pSrc[mbOff + idxCC[1][pxOff]], d4 = pSrc[mbOff + idxCC[2][pxOff]];
  1384. }
  1385. for(iRow = 0; iRow < 12; iRow += 2){
  1386. pDst[((iColumn >> 3) << 6) + idxCC_420[iRow >> 1][iColumn & 7]] = DF_ODD;
  1387. d0 = d2, d1 = d3, d2 = d4;
  1388. d3 = pSrc[mbOff + idxCC[iRow + 3][pxOff]];
  1389. d4 = pSrc[mbOff + idxCC[iRow + 4][pxOff]];
  1390. }
  1391. //last row of current MB
  1392. pDst[((iColumn >> 3) << 6) + idxCC_420[6][iColumn & 7]] = DF_ODD;
  1393. d0 = d2, d1 = d3, d2 = d4;
  1394. d3 = pSrc[mbOff + idxCC[iRow + 3][pxOff]];
  1395. if(pSC->cRow + 1 == pSC->cmbHeight){ // bottom image boundary
  1396. d4 = d2;
  1397. pDst[((iColumn >> 3) << 6) + idxCC_420[7][iColumn & 7]] = DF_ODD;
  1398. }
  1399. else{
  1400. for(iRow = 0; iRow < 4; iRow ++)
  1401. pBuf[iRow][iColumn] = pSrc[mbOff + idxCC[iRow + 12][pxOff]];
  1402. }
  1403. }
  1404. }
  1405. }
  1406. }
  1407. // centralized horizontal padding
  1408. Void padHorizontally(CWMImageStrCodec * pSC)
  1409. {
  1410. if(pSC->WMII.cWidth != pSC->cmbWidth * 16){ // horizontal padding is necessary!
  1411. const COLORFORMAT cfExt = pSC->WMISCP.bYUVData ?
  1412. pSC->m_param.cfColorFormat : pSC->WMII.cfColorFormat;
  1413. size_t cFullChannel = pSC->WMISCP.cChannel;
  1414. size_t iLast = pSC->WMII.cWidth - 1;
  1415. PixelI * pCh[16];
  1416. size_t iChannel, iColumn, iRow;
  1417. if(cfExt == YUV_420 || cfExt == YUV_422 || cfExt == Y_ONLY)
  1418. cFullChannel = 1;
  1419. assert(cFullChannel <= 16);
  1420. assert(pSC->WMISCP.cChannel <= 16);
  1421. for(iChannel = 0; iChannel < pSC->WMISCP.cChannel; iChannel ++)
  1422. pCh[iChannel & 15] = pSC->p1MBbuffer[iChannel & 15];
  1423. if(pSC->m_bUVResolutionChange)
  1424. pCh[1] = pSC->pResU, pCh[2] = pSC->pResV;
  1425. // pad full resoluton channels
  1426. for(iRow = 0; iRow < 16; iRow ++){
  1427. const size_t iPosLast = ((iLast >> 4) << 8) + idxCC[iRow][iLast & 0xf];
  1428. for(iColumn = iLast + 1; iColumn < pSC->cmbWidth * 16; iColumn ++){
  1429. const size_t iPos = ((iColumn >> 4) << 8) + idxCC[iRow][iColumn & 0xf];
  1430. for(iChannel = 0; iChannel < cFullChannel; iChannel ++)
  1431. pCh[iChannel & 15][iPos] = pCh[iChannel & 15][iPosLast];
  1432. }
  1433. }
  1434. if(cfExt == YUV_422) // pad YUV_422 UV
  1435. for(iLast >>= 1, iRow = 0; iRow < 16; iRow ++){
  1436. const size_t iPosLast = ((iLast >> 3) << 7) + idxCC[iRow][iLast & 7];
  1437. for(iColumn = iLast + 1; iColumn < pSC->cmbWidth * 8; iColumn ++){
  1438. const size_t iPos = ((iColumn >> 3) << 7) + idxCC[iRow][iColumn & 7];
  1439. for(iChannel = 1; iChannel < 3; iChannel ++)
  1440. pCh[iChannel][iPos] = pCh[iChannel][iPosLast];
  1441. }
  1442. }
  1443. else if(cfExt == YUV_420) // pad YUV_420 UV
  1444. for(iLast >>= 1, iRow = 0; iRow < 8; iRow ++){
  1445. const size_t iPosLast = ((iLast >> 3) << 6) + idxCC_420[iRow][iLast & 7];
  1446. for(iColumn = iLast + 1; iColumn < pSC->cmbWidth * 8; iColumn ++){
  1447. const size_t iPos = ((iColumn >> 3) << 6) + idxCC_420[iRow][iColumn & 7];
  1448. for(iChannel = 1; iChannel < 3; iChannel ++)
  1449. pCh[iChannel][iPos] = pCh[iChannel][iPosLast];
  1450. }
  1451. }
  1452. }
  1453. }
  1454. // centralized alpha channel color conversion, small perf penalty
  1455. Int inputMBRowAlpha(CWMImageStrCodec* pSC)
  1456. {
  1457. if(pSC->m_bSecondary == FALSE && pSC->m_pNextSC != NULL){ // alpha channel is present
  1458. const size_t cShift = (pSC->m_pNextSC->m_param.bScaledArith ? (SHIFTZERO + QPFRACBITS) : 0);
  1459. const BITDEPTH_BITS bdExt = pSC->WMII.bdBitDepth;
  1460. const size_t iAlphaPos = pSC->WMII.cLeadingPadding + (pSC->WMII.cfColorFormat == CMYK ? 4 : 3);//only RGB and CMYK may have interleaved alpha
  1461. const size_t cRow = pSC->WMIBI.cLine;
  1462. const size_t cColumn = pSC->WMII.cWidth;
  1463. const U8 * pSrc0 = (U8 *)pSC->WMIBI.pv;
  1464. PixelI * pA = pSC->m_pNextSC->p1MBbuffer[0];
  1465. size_t iRow, iColumn;
  1466. for(iRow = 0; iRow < 16; iRow ++){
  1467. if(bdExt == BD_8){
  1468. const size_t cStride = (pSC->WMII.cBitsPerUnit >> 3);
  1469. const U8 * pSrc = pSrc0;
  1470. for(iColumn = 0; iColumn < cColumn; iColumn ++, pSrc += cStride)
  1471. pA[((iColumn >> 4) << 8) + idxCC[iRow][iColumn & 0xf]] = ((PixelI)pSrc[iAlphaPos] - (1 << 7)) << cShift;
  1472. }
  1473. else if(bdExt == BD_16){
  1474. const size_t cStride = (pSC->WMII.cBitsPerUnit >> 3) / sizeof(U16);
  1475. const U8 nLenMantissaOrShift = pSC->m_pNextSC->WMISCP.nLenMantissaOrShift;
  1476. const U16 * pSrc = (U16 *)pSrc0;
  1477. for(iColumn = 0; iColumn < cColumn; iColumn ++, pSrc += cStride)
  1478. pA[((iColumn >> 4) << 8) + idxCC[iRow][iColumn & 0xf]] = ((((PixelI)pSrc[iAlphaPos] - (1 << 15)) >> nLenMantissaOrShift) << cShift);
  1479. }
  1480. else if(bdExt == BD_16S){
  1481. const size_t cStride = (pSC->WMII.cBitsPerUnit >> 3) / sizeof(I16);
  1482. const U8 nLenMantissaOrShift = pSC->m_pNextSC->WMISCP.nLenMantissaOrShift;
  1483. const I16 * pSrc = (I16 *)pSrc0;
  1484. for(iColumn = 0; iColumn < cColumn; iColumn ++, pSrc += cStride)
  1485. pA[((iColumn >> 4) << 8) + idxCC[iRow][iColumn & 0xf]] = (((PixelI)pSrc[iAlphaPos] >> nLenMantissaOrShift) << cShift);
  1486. }
  1487. else if(bdExt == BD_16F){
  1488. const size_t cStride = (pSC->WMII.cBitsPerUnit >> 3) / sizeof(U16);
  1489. const I16 * pSrc = (I16 *)pSrc0;
  1490. for(iColumn = 0; iColumn < cColumn; iColumn ++, pSrc += cStride)
  1491. pA[((iColumn >> 4) << 8) + idxCC[iRow][iColumn & 0xf]] = forwardHalf (pSrc[iAlphaPos]) << cShift;
  1492. }
  1493. else if(bdExt == BD_32S){
  1494. const size_t cStride = (pSC->WMII.cBitsPerUnit >> 3) / sizeof(I32);
  1495. const U8 nLenMantissaOrShift = pSC->m_pNextSC->WMISCP.nLenMantissaOrShift;
  1496. const I32 * pSrc = (I32 *)pSrc0;
  1497. for(iColumn = 0; iColumn < cColumn; iColumn ++, pSrc += cStride)
  1498. pA[((iColumn >> 4) << 8) + idxCC[iRow][iColumn & 0xf]] = (((PixelI)pSrc[iAlphaPos] >> nLenMantissaOrShift) << cShift);
  1499. }
  1500. else if(bdExt == BD_32F){
  1501. const size_t cStride = (pSC->WMII.cBitsPerUnit >> 3) / sizeof(float);
  1502. const U8 nLen = pSC->m_pNextSC->WMISCP.nLenMantissaOrShift;
  1503. const I8 nExpBias = pSC->m_pNextSC->WMISCP.nExpBias;
  1504. const float * pSrc = (float *)pSrc0;
  1505. for(iColumn = 0; iColumn < cColumn; iColumn ++, pSrc += cStride)
  1506. pA[((iColumn >> 4) << 8) + idxCC[iRow][iColumn & 0xf]] = float2pixel (pSrc[iAlphaPos], nExpBias, nLen) << cShift;
  1507. }
  1508. else // not supported
  1509. return ICERR_ERROR;
  1510. if(iRow + 1 < cRow) // vertical padding!
  1511. pSrc0 += pSC->WMIBI.cbStride;
  1512. for(iColumn = cColumn; iColumn < pSC->cmbWidth * 16; iColumn ++) // horizontal padding
  1513. pA[((iColumn >> 4) << 8) + idxCC[iRow][iColumn & 0xf]] = pA[(((cColumn - 1) >> 4) << 8) + idxCC[iRow][(cColumn - 1) & 0xf]];
  1514. }
  1515. }
  1516. return ICERR_OK;
  1517. }
  1518. // input one MB row of image data from input buffer
  1519. Int inputMBRow(CWMImageStrCodec* pSC)
  1520. {
  1521. const size_t cShift = (pSC->m_param.bScaledArith ? (SHIFTZERO + QPFRACBITS) : 0);
  1522. const BITDEPTH_BITS bdExt = pSC->WMII.bdBitDepth;
  1523. COLORFORMAT cfExt = pSC->WMII.cfColorFormat;
  1524. const COLORFORMAT cfInt = pSC->m_param.cfColorFormat;
  1525. const size_t cPixelStride = (pSC->WMII.cBitsPerUnit >> 3);
  1526. const size_t iRowStride =
  1527. (cfExt == YUV_420 || (pSC->WMISCP.bYUVData && pSC->m_param.cfColorFormat==YUV_420)) ? 2 : 1;
  1528. const size_t cRow = pSC->WMIBI.cLine;
  1529. const size_t cColumn = pSC->WMII.cWidth;
  1530. const size_t iB = (pSC->WMII.bRGB ? 2 : 0);
  1531. const size_t iR = 2 - iB;
  1532. const U8 * pSrc0 = (U8 *)pSC->WMIBI.pv;
  1533. const U8 nLen = pSC->WMISCP.nLenMantissaOrShift;
  1534. const I8 nExpBias = pSC->WMISCP.nExpBias;
  1535. PixelI *pY = pSC->p1MBbuffer[0], *pU = pSC->p1MBbuffer[1], *pV = pSC->p1MBbuffer[2];
  1536. size_t iRow, iColumn, iPos;
  1537. // guard input buffer
  1538. if(checkImageBuffer(pSC, cColumn, cRow) != ICERR_OK)
  1539. return ICERR_ERROR;
  1540. if(pSC->m_bUVResolutionChange) // will do downsampling somewhere else!
  1541. pU = pSC->pResU, pV = pSC->pResV;
  1542. else if(cfInt == Y_ONLY) // xxx to Y_ONLY transcoding!
  1543. pU = pV = pY; // write pY AFTER pU and pV so Y will overwrite U&V
  1544. for(iRow = 0; iRow < 16; iRow += iRowStride){
  1545. if (pSC->WMISCP.bYUVData){
  1546. I32 * pSrc = (I32 *)pSrc0 + pSC->WMII.cLeadingPadding;
  1547. switch(pSC->m_param.cfColorFormat){
  1548. case Y_ONLY:
  1549. case YUV_444:
  1550. case NCOMPONENT:
  1551. {
  1552. const size_t cChannel = pSC->m_param.cNumChannels;
  1553. PixelI * pChannel[16];
  1554. size_t iChannel;
  1555. assert(cChannel <= 16);
  1556. for(iChannel = 0; iChannel < cChannel; iChannel ++)
  1557. pChannel[iChannel & 15] = pSC->p1MBbuffer[iChannel & 15];
  1558. if(pSC->m_bUVResolutionChange)
  1559. pChannel[1] = pSC->pResU, pChannel[2] = pSC->pResV;
  1560. for(iColumn = 0; iColumn < cColumn; iColumn ++, pSrc += cChannel){
  1561. iPos = ((iColumn >> 4) << 8) + idxCC[iRow][iColumn & 0xf];
  1562. for(iChannel = 0; iChannel < cChannel; iChannel ++)
  1563. pChannel[iChannel & 15][iPos] = (PixelI)pSrc[iChannel & 15];
  1564. }
  1565. }
  1566. break;
  1567. case YUV_422:
  1568. for(iColumn = 0; iColumn < cColumn; iColumn += 2, pSrc += 4){
  1569. if(cfInt != Y_ONLY){
  1570. iPos = ((iColumn >> 4) << 7) + idxCC[iRow][(iColumn >> 1) & 7];
  1571. pU[iPos] = (PixelI)pSrc[0];
  1572. pV[iPos] = (PixelI)pSrc[2];
  1573. }
  1574. pY[((iColumn >> 4) << 8) + idxCC[iRow][iColumn & 15]] = (PixelI)pSrc[1];
  1575. pY[(((iColumn + 1) >> 4) << 8) + idxCC[iRow][(iColumn + 1) & 15]] = (PixelI)pSrc[3];
  1576. }
  1577. break;
  1578. case YUV_420:
  1579. for(iColumn = 0; iColumn < cColumn; iColumn += 2, pSrc += 6){
  1580. if(cfInt != Y_ONLY){
  1581. iPos = ((iColumn >> 4) << 6) + idxCC_420[iRow >> 1][(iColumn >> 1) & 7];
  1582. pU[iPos] = (PixelI)pSrc[4];
  1583. pV[iPos] = (PixelI)pSrc[5];
  1584. }
  1585. pY[((iColumn >> 4) << 8) + idxCC[iRow][iColumn & 15]] = (PixelI)pSrc[0];
  1586. pY[(((iColumn + 1) >> 4) << 8) + idxCC[iRow][(iColumn + 1) & 15]] = (PixelI)pSrc[1];
  1587. pY[((iColumn >> 4) << 8) + idxCC[iRow + 1][iColumn & 15]] = (PixelI)pSrc[2];
  1588. pY[(((iColumn + 1) >> 4) << 8) + idxCC[iRow + 1][(iColumn + 1) & 15]] = (PixelI)pSrc[3];
  1589. }
  1590. break;
  1591. default:
  1592. assert(0);
  1593. break;
  1594. }
  1595. }
  1596. else if(bdExt == BD_8){
  1597. const U8 * pSrc = pSrc0 + pSC->WMII.cLeadingPadding;
  1598. const PixelI iOffset = (128 << cShift);
  1599. switch(cfExt){
  1600. case CF_RGB:
  1601. assert (pSC->m_bSecondary == FALSE);
  1602. for(iColumn = 0; iColumn < cColumn; iColumn ++, pSrc += cPixelStride){
  1603. PixelI r = ((PixelI)pSrc[iR]) << cShift, g = ((PixelI)pSrc[1]) << cShift, b = ((PixelI)pSrc[iB]) << cShift;
  1604. _CC(r, g, b); // color conversion
  1605. iPos = ((iColumn >> 4) << 8) + idxCC[iRow][iColumn & 0xf];
  1606. pU[iPos] = -r, pV[iPos] = b, pY[iPos] = g - iOffset;
  1607. }
  1608. break;
  1609. case Y_ONLY:
  1610. case YUV_444:
  1611. case NCOMPONENT:
  1612. {
  1613. const size_t cChannel = pSC->m_param.cNumChannels;
  1614. PixelI * pChannel[16];
  1615. size_t iChannel;
  1616. assert(cChannel <= 16);
  1617. for(iChannel = 0; iChannel < cChannel; iChannel ++)
  1618. pChannel[iChannel & 15] = pSC->p1MBbuffer[iChannel & 15];
  1619. if(pSC->m_bUVResolutionChange)
  1620. pChannel[1] = pSC->pResU, pChannel[2] = pSC->pResV;
  1621. for(iColumn = 0; iColumn < cColumn; iColumn ++, pSrc += cPixelStride){
  1622. iPos = ((iColumn >> 4) << 8) + idxCC[iRow][iColumn & 0xf];
  1623. for(iChannel = 0; iChannel < cChannel; iChannel ++)
  1624. pChannel[iChannel & 15][iPos] = (((PixelI)pSrc[iChannel & 15]) << cShift) - iOffset;
  1625. }
  1626. break;
  1627. }
  1628. case CF_RGBE:
  1629. for(iColumn = 0; iColumn < cColumn; iColumn ++, pSrc += cPixelStride){
  1630. PixelI iExp = (PixelI)pSrc[3];
  1631. PixelI r = forwardRGBE (pSrc[0], iExp) << cShift;
  1632. PixelI g = forwardRGBE (pSrc[1], iExp) << cShift;
  1633. PixelI b = forwardRGBE (pSrc[2], iExp) << cShift;
  1634. _CC(r, g, b);
  1635. iPos = ((iColumn >> 4) << 8) + idxCC[iRow][iColumn & 0xf];
  1636. pU[iPos] = -r, pV[iPos] = b, pY[iPos] = g;
  1637. }
  1638. break;
  1639. case CMYK:
  1640. {
  1641. PixelI * pK = (cfInt == CMYK ? pSC->p1MBbuffer[3] : pY); // CMYK -> YUV_xxx transcoding!
  1642. for(iColumn = 0; iColumn < cColumn; iColumn ++, pSrc += cPixelStride){
  1643. PixelI c = ((PixelI)pSrc[0]) << cShift;
  1644. PixelI m = ((PixelI)pSrc[1]) << cShift;
  1645. PixelI y = ((PixelI)pSrc[2]) << cShift;
  1646. PixelI k = ((PixelI)pSrc[3]) << cShift;
  1647. _CC_CMYK(c, m, y, k);
  1648. iPos = ((iColumn >> 4) << 8) + idxCC[iRow][iColumn & 0xf];
  1649. pU[iPos] = c, pV[iPos] = -y, pK[iPos] = k, pY[iPos] = iOffset - m;
  1650. }
  1651. break;
  1652. }
  1653. case YUV_422:
  1654. for(iColumn = 0; iColumn < cColumn; iColumn += 2, pSrc += cPixelStride){
  1655. if(cfInt != Y_ONLY){
  1656. iPos = ((iColumn >> 4) << 7) + idxCC[iRow][(iColumn >> 1) & 7];
  1657. pU[iPos] = (((PixelI)pSrc[0]) << cShift) - iOffset;
  1658. pV[iPos] = (((PixelI)pSrc[2]) << cShift) - iOffset;
  1659. }
  1660. pY[((iColumn >> 4) << 8) + idxCC[iRow][iColumn & 15]] = (((PixelI)pSrc[1]) << cShift) - iOffset;
  1661. pY[(((iColumn + 1) >> 4) << 8) + idxCC[iRow][(iColumn + 1) & 15]] = (((PixelI)pSrc[3]) << cShift) - iOffset;
  1662. }
  1663. break;
  1664. case YUV_420:
  1665. for(iColumn = 0; iColumn < cColumn; iColumn += 2, pSrc += cPixelStride){
  1666. if(cfInt != Y_ONLY){
  1667. iPos = ((iColumn >> 4) << 6) + idxCC_420[iRow >> 1][(iColumn >> 1) & 7];
  1668. pU[iPos] = (((PixelI)pSrc[4]) << cShift) - iOffset;
  1669. pV[iPos] = (((PixelI)pSrc[5]) << cShift) - iOffset;
  1670. }
  1671. pY[((iColumn >> 4) << 8) + idxCC[iRow][iColumn & 15]] = (((PixelI)pSrc[0]) << cShift) - iOffset;
  1672. pY[(((iColumn + 1) >> 4) << 8) + idxCC[iRow][(iColumn + 1) & 15]] = (((PixelI)pSrc[1]) << cShift) - iOffset;
  1673. pY[((iColumn >> 4) << 8) + idxCC[iRow + 1][iColumn & 15]] = (((PixelI)pSrc[2]) << cShift) - iOffset;
  1674. pY[(((iColumn + 1) >> 4) << 8) + idxCC[iRow + 1][(iColumn + 1) & 15]] = (((PixelI)pSrc[3]) << cShift) - iOffset;
  1675. }
  1676. break;
  1677. default:
  1678. assert(0);
  1679. break;
  1680. }
  1681. }
  1682. else if(bdExt == BD_16){
  1683. const U16 * pSrc = (U16 *)pSrc0 + pSC->WMII.cLeadingPadding;
  1684. const size_t cStride = cPixelStride / sizeof(U16);
  1685. const PixelI iOffset = ((1 << 15) >> nLen) << cShift;
  1686. switch(cfExt){
  1687. case CF_RGB:
  1688. for(iColumn = 0; iColumn < cColumn; iColumn ++, pSrc += cStride){
  1689. PixelI r = ((PixelI)pSrc[0] >> nLen) << cShift, g = ((PixelI)pSrc[1] >> nLen) << cShift, b = ((PixelI)pSrc[2] >> nLen) << cShift;
  1690. _CC(r, g, b); // color conversion
  1691. iPos = ((iColumn >> 4) << 8) + idxCC[iRow][iColumn & 0xf];
  1692. pU[iPos] = -r, pV[iPos] = b, pY[iPos] = g - iOffset;
  1693. }
  1694. break;
  1695. case Y_ONLY:
  1696. case YUV_444:
  1697. case NCOMPONENT:
  1698. {
  1699. const size_t cChannel = pSC->WMISCP.cChannel;
  1700. size_t iChannel;
  1701. for(iColumn = 0; iColumn < cColumn; iColumn ++, pSrc += cStride){
  1702. iPos = ((iColumn >> 4) << 8) + idxCC[iRow][iColumn & 0xf];
  1703. for(iChannel = 0; iChannel < cChannel; iChannel ++)
  1704. pSC->p1MBbuffer[iChannel][iPos] = (((PixelI)pSrc[iChannel] >> nLen) << cShift) - iOffset;
  1705. }
  1706. break;
  1707. }
  1708. case CMYK:
  1709. {
  1710. PixelI * pK = (cfInt == CMYK ? pSC->p1MBbuffer[3] : pY); // CMYK -> YUV_xxx transcoding!
  1711. for(iColumn = 0; iColumn < cColumn; iColumn ++, pSrc += cStride){
  1712. PixelI c = ((PixelI)pSrc[0] >> nLen) << cShift;
  1713. PixelI m = ((PixelI)pSrc[1] >> nLen) << cShift;
  1714. PixelI y = ((PixelI)pSrc[2] >> nLen) << cShift;
  1715. PixelI k = ((PixelI)pSrc[3] >> nLen) << cShift;
  1716. _CC_CMYK(c, m, y, k);
  1717. iPos = ((iColumn >> 4) << 8) + idxCC[iRow][iColumn & 0xf];
  1718. pU[iPos] = c, pV[iPos] = -y, pK[iPos] = k, pY[iPos] = iOffset - m;
  1719. }
  1720. break;
  1721. }
  1722. case YUV_422:
  1723. for(iColumn = 0; iColumn < cColumn; iColumn += 2, pSrc += cStride){
  1724. if(cfInt != Y_ONLY){
  1725. iPos = ((iColumn >> 4) << 7) + idxCC[iRow][(iColumn >> 1) & 7];
  1726. pU[iPos] = (((PixelI)pSrc[0]) << cShift) - iOffset;
  1727. pV[iPos] = (((PixelI)pSrc[2]) << cShift) - iOffset;
  1728. }
  1729. pY[((iColumn >> 4) << 8) + idxCC[iRow][iColumn & 15]] = (((PixelI)pSrc[1]) << cShift) - iOffset;
  1730. pY[(((iColumn + 1) >> 4) << 8) + idxCC[iRow][(iColumn + 1) & 15]] = (((PixelI)pSrc[3]) << cShift) - iOffset;
  1731. }
  1732. break;
  1733. case YUV_420:
  1734. for(iColumn = 0; iColumn < cColumn; iColumn += 2, pSrc += cStride){
  1735. if(cfInt != Y_ONLY){
  1736. iPos = ((iColumn >> 4) << 6) + idxCC_420[iRow >> 1][(iColumn >> 1) & 7];
  1737. pU[iPos] = (((PixelI)pSrc[4]) << cShift) - iOffset;
  1738. pV[iPos] = (((PixelI)pSrc[5]) << cShift) - iOffset;
  1739. }
  1740. pY[((iColumn >> 4) << 8) + idxCC[iRow][iColumn & 15]] = (((PixelI)pSrc[0]) << cShift) - iOffset;
  1741. pY[(((iColumn + 1) >> 4) << 8) + idxCC[iRow][(iColumn + 1) & 15]] = (((PixelI)pSrc[1]) << cShift) - iOffset;
  1742. pY[((iColumn >> 4) << 8) + idxCC[iRow + 1][iColumn & 15]] = (((PixelI)pSrc[2]) << cShift) - iOffset;
  1743. pY[(((iColumn + 1) >> 4) << 8) + idxCC[iRow + 1][(iColumn + 1) & 15]] = (((PixelI)pSrc[3]) << cShift) - iOffset;
  1744. }
  1745. break;
  1746. default:
  1747. assert(0);
  1748. break;
  1749. }
  1750. }
  1751. else if(bdExt == BD_16S){
  1752. const I16 * pSrc = (I16 *)pSrc0 + pSC->WMII.cLeadingPadding;
  1753. const size_t cStride = cPixelStride / sizeof(I16);
  1754. switch(cfExt){
  1755. case CF_RGB:
  1756. for(iColumn = 0; iColumn < cColumn; iColumn ++, pSrc += cStride){
  1757. PixelI r = ((PixelI)pSrc[0] >> nLen) << cShift, g = ((PixelI)pSrc[1] >> nLen) << cShift, b = ((PixelI)pSrc[2] >> nLen) << cShift;
  1758. _CC(r, g, b); // color conversion
  1759. iPos = ((iColumn >> 4) << 8) + idxCC[iRow][iColumn & 0xf];
  1760. pU[iPos] = -r, pV[iPos] = b, pY[iPos] = g;
  1761. }
  1762. break;
  1763. case Y_ONLY:
  1764. case YUV_444:
  1765. case NCOMPONENT:
  1766. {
  1767. const size_t cChannel = pSC->WMISCP.cChannel;
  1768. size_t iChannel;
  1769. for(iColumn = 0; iColumn < cColumn; iColumn ++, pSrc += cStride){
  1770. iPos = ((iColumn >> 4) << 8) + idxCC[iRow][iColumn & 0xf];
  1771. for(iChannel = 0; iChannel < cChannel; iChannel ++)
  1772. pSC->p1MBbuffer[iChannel][iPos] = (((PixelI)pSrc[iChannel] >> nLen) << cShift);
  1773. }
  1774. }
  1775. break;
  1776. case CMYK:
  1777. {
  1778. PixelI * pK = (cfInt == CMYK ? pSC->p1MBbuffer[3] : pY); // CMYK -> YUV_xxx transcoding!
  1779. for(iColumn = 0; iColumn < cColumn; iColumn ++, pSrc += cStride){
  1780. PixelI c = ((PixelI)pSrc[0] >> nLen) << cShift;
  1781. PixelI m = ((PixelI)pSrc[1] >> nLen) << cShift;
  1782. PixelI y = ((PixelI)pSrc[2] >> nLen) << cShift;
  1783. PixelI k = ((PixelI)pSrc[3] >> nLen) << cShift;
  1784. _CC_CMYK(c, m, y, k);
  1785. iPos = ((iColumn >> 4) << 8) + idxCC[iRow][iColumn & 0xf];
  1786. pU[iPos] = c, pV[iPos] = -y, pK[iPos] = k, pY[iPos] = -m;
  1787. }
  1788. }
  1789. break;
  1790. default:
  1791. assert(0);
  1792. break;
  1793. }
  1794. }
  1795. else if(bdExt == BD_16F){
  1796. const I16 * pSrc = (I16 *)pSrc0 + pSC->WMII.cLeadingPadding;
  1797. const size_t cStride = cPixelStride / sizeof(U16);
  1798. switch(cfExt){
  1799. case CF_RGB:
  1800. for(iColumn = 0; iColumn < cColumn; iColumn ++, pSrc += cStride){
  1801. PixelI r = forwardHalf (pSrc[0]) << cShift;
  1802. PixelI g = forwardHalf (pSrc[1]) << cShift;
  1803. PixelI b = forwardHalf (pSrc[2]) << cShift;
  1804. _CC(r, g, b); // color conversion
  1805. iPos = ((iColumn >> 4) << 8) + idxCC[iRow][iColumn & 0xf];
  1806. pU[iPos] = -r, pV[iPos] = b, pY[iPos] = g;
  1807. }
  1808. break;
  1809. case Y_ONLY:
  1810. case YUV_444:
  1811. case NCOMPONENT:
  1812. {
  1813. const size_t cChannel = pSC->WMISCP.cChannel; // check xxx => Y_ONLY transcoding!
  1814. size_t iChannel;
  1815. for(iColumn = 0; iColumn < cColumn; iColumn ++, pSrc += cStride){
  1816. iPos = ((iColumn >> 4) << 8) + idxCC[iRow][iColumn & 0xf];
  1817. for(iChannel = 0; iChannel < cChannel; iChannel ++)
  1818. pSC->p1MBbuffer[iChannel][iPos] = forwardHalf (pSrc[iChannel]) << cShift;
  1819. }
  1820. }
  1821. break;
  1822. default:
  1823. assert(0);
  1824. break;
  1825. }
  1826. }
  1827. else if(bdExt == BD_32){
  1828. const U32 * pSrc = (U32 *)pSrc0 + pSC->WMII.cLeadingPadding;
  1829. const size_t cStride = cPixelStride / sizeof(U32);
  1830. const PixelI iOffset = ((1 << 31) >> nLen) << cShift;
  1831. switch(cfExt){
  1832. case CF_RGB:
  1833. for(iColumn = 0; iColumn < cColumn; iColumn ++, pSrc += cStride){
  1834. PixelI r = (pSrc[0] >> nLen) << cShift, g = (pSrc[1] >> nLen) << cShift, b = (pSrc[2] >> nLen) << cShift;
  1835. _CC(r, g, b); // color conversion
  1836. iPos = ((iColumn >> 4) << 8) + idxCC[iRow][iColumn & 0xf];
  1837. pU[iPos] = -r, pV[iPos] = b, pY[iPos] = g - iOffset;
  1838. }
  1839. break;
  1840. case Y_ONLY:
  1841. case YUV_444:
  1842. case NCOMPONENT:
  1843. {
  1844. const size_t cChannel = pSC->WMISCP.cChannel;
  1845. size_t iChannel;
  1846. for(iColumn = 0; iColumn < cColumn; iColumn ++, pSrc += cStride){
  1847. iPos = ((iColumn >> 4) << 8) + idxCC[iRow][iColumn & 0xf];
  1848. for(iChannel = 0; iChannel < cChannel; iChannel ++)
  1849. pSC->p1MBbuffer[iChannel][iPos] = (pSrc[iChannel] >> nLen) << cShift;
  1850. }
  1851. break;
  1852. }
  1853. default:
  1854. assert(0);
  1855. break;
  1856. }
  1857. }
  1858. else if(bdExt == BD_32S){
  1859. const I32 * pSrc = (I32 *)pSrc0 + pSC->WMII.cLeadingPadding;
  1860. const size_t cStride = cPixelStride / sizeof(I32);
  1861. switch(cfExt){
  1862. case CF_RGB:
  1863. for(iColumn = 0; iColumn < cColumn; iColumn ++, pSrc += cStride){
  1864. PixelI r = (pSrc[0] >> nLen)<< cShift, g = (pSrc[1] >> nLen)<< cShift, b = (pSrc[2] >> nLen)<< cShift;
  1865. _CC(r, g, b); // color conversion
  1866. iPos = ((iColumn >> 4) << 8) + idxCC[iRow][iColumn & 0xf];
  1867. pU[iPos] = -r, pV[iPos] = b, pY[iPos] = g;
  1868. }
  1869. break;
  1870. case Y_ONLY:
  1871. case YUV_444:
  1872. case NCOMPONENT:
  1873. {
  1874. const size_t cChannel = pSC->WMISCP.cChannel; // check xxx => Y_ONLY transcoding!
  1875. size_t iChannel;
  1876. for(iColumn = 0; iColumn < cColumn; iColumn ++, pSrc += cStride){
  1877. iPos = ((iColumn >> 4) << 8) + idxCC[iRow][iColumn & 0xf];
  1878. for(iChannel = 0; iChannel < cChannel; iChannel ++)
  1879. pSC->p1MBbuffer[iChannel][iPos] = (pSrc[iChannel] >> nLen) << cShift;
  1880. }
  1881. }
  1882. break;
  1883. default:
  1884. assert(0);
  1885. break;
  1886. }
  1887. }
  1888. else if(bdExt == BD_32F){
  1889. const float * pSrc = (float *)pSrc0 + pSC->WMII.cLeadingPadding;
  1890. const size_t cStride = cPixelStride / sizeof(float);
  1891. switch(cfExt){
  1892. case CF_RGB:
  1893. for(iColumn = 0; iColumn < cColumn; iColumn ++, pSrc += cStride){
  1894. PixelI r = float2pixel (pSrc[0], nExpBias, nLen) << cShift;
  1895. PixelI g = float2pixel (pSrc[1], nExpBias, nLen) << cShift;
  1896. PixelI b = float2pixel (pSrc[2], nExpBias, nLen) << cShift;
  1897. _CC(r, g, b); // color conversion
  1898. iPos = ((iColumn >> 4) << 8) + idxCC[iRow][iColumn & 0xf];
  1899. pU[iPos] = -r, pV[iPos] = b, pY[iPos] = g;
  1900. }
  1901. break;
  1902. case Y_ONLY:
  1903. case YUV_444:
  1904. case NCOMPONENT:
  1905. {
  1906. const size_t cChannel = pSC->WMISCP.cChannel;
  1907. size_t iChannel;
  1908. for(iColumn = 0; iColumn < cColumn; iColumn ++, pSrc += cStride){
  1909. iPos = ((iColumn >> 4) << 8) + idxCC[iRow][iColumn & 0xf];
  1910. for(iChannel = 0; iChannel < cChannel; iChannel ++)
  1911. pSC->p1MBbuffer[iChannel][iPos] = float2pixel (pSrc[iChannel], nExpBias, nLen) << cShift;
  1912. }
  1913. }
  1914. break;
  1915. default:
  1916. assert(0);
  1917. break;
  1918. }
  1919. }
  1920. else if(bdExt == BD_5){ // RGB 555, work for both big endian and small endian!
  1921. const U8 * pSrc = pSrc0;
  1922. const PixelI iOffset = (16 << cShift);
  1923. assert(cfExt == CF_RGB);
  1924. for(iColumn = 0; iColumn < cColumn; iColumn ++, pSrc += cPixelStride){
  1925. PixelI r = (PixelI)pSrc[0], g = (PixelI)pSrc[1], b = ((g >> 2) & 0x1F) << cShift;
  1926. g = ((r >> 5) + ((g & 3) << 3)) << cShift, r = (r & 0x1F) << cShift;
  1927. _CC(r, g, b); // color conversion
  1928. iPos = ((iColumn >> 4) << 8) + idxCC[iRow][iColumn & 0xf];
  1929. pU[iPos] = -r, pV[iPos] = b, pY[iPos] = g - iOffset;
  1930. }
  1931. }
  1932. else if(bdExt == BD_565){ // RGB 555, work for both big endian and small endian!
  1933. const U8 * pSrc = pSrc0;
  1934. const PixelI iOffset = (32 << cShift);
  1935. assert(cfExt == CF_RGB);
  1936. for(iColumn = 0; iColumn < cColumn; iColumn ++, pSrc += cPixelStride){
  1937. PixelI r = (PixelI)pSrc[0], g = (PixelI)pSrc[1], b = (g >> 3) << (cShift + 1);
  1938. g = ((r >> 5) + ((g & 7) << 3)) << cShift, r = (r & 0x1F) << (cShift + 1);
  1939. _CC(r, g, b); // color conversion
  1940. iPos = ((iColumn >> 4) << 8) + idxCC[iRow][iColumn & 0xf];
  1941. pU[iPos] = -r, pV[iPos] = b, pY[iPos] = g - iOffset;
  1942. }
  1943. }
  1944. else if(bdExt == BD_10){ //RGB 101010, work for both big endian and small endian!
  1945. const U8 * pSrc = pSrc0;
  1946. const PixelI iOffset = (512 << cShift);
  1947. assert(cfExt == CF_RGB);
  1948. for(iColumn = 0; iColumn < cColumn; iColumn ++, pSrc += cPixelStride){
  1949. PixelI r = (PixelI)pSrc[0], g = (PixelI)pSrc[1], b = (PixelI)pSrc[2];
  1950. r = (r + ((g & 3) << 8)) << cShift, g = ((g >> 2) + ((b & 0xF) << 6)) << cShift;
  1951. b = ((b >> 4) + (((PixelI)pSrc[3] & 0x3F) << 4)) << cShift;
  1952. _CC(r, g, b); // color conversion
  1953. iPos = ((iColumn >> 4) << 8) + idxCC[iRow][iColumn & 0xf];
  1954. pU[iPos] = -r, pV[iPos] = b, pY[iPos] = g - iOffset;
  1955. }
  1956. }
  1957. else if(bdExt == BD_1){
  1958. assert(cfExt == Y_ONLY);
  1959. for(iColumn = 0; iColumn < cColumn; iColumn ++) {
  1960. pY[((iColumn >> 4) << 8) + idxCC[iRow][iColumn & 0xf]] = ((pSC->WMISCP.bBlackWhite + (pSrc0[iColumn >> 3] >> (7 - (iColumn & 7)))) & 1) << cShift;
  1961. }
  1962. }
  1963. if(iRow + iRowStride < cRow) // centralized vertical padding!
  1964. pSrc0 += pSC->WMIBI.cbStride;
  1965. }
  1966. padHorizontally(pSC); // centralized horizontal padding
  1967. // centralized down-sampling
  1968. if(pSC->m_bUVResolutionChange)
  1969. downsampleUV(pSC);
  1970. // centralized alpha channel handdling
  1971. if (pSC->WMISCP.uAlphaMode == 3)
  1972. if(inputMBRowAlpha(pSC) != ICERR_OK)
  1973. return ICERR_ERROR;
  1974. return ICERR_OK;
  1975. }