Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 PDFium Authors. All rights reserved. | 1 // Copyright 2014 PDFium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 | 6 |
| 7 #include <map> | |
| 8 #include <list> | |
| 7 #include "JBig2_Context.h" | 9 #include "JBig2_Context.h" |
| 10 | |
| 11 // Implement a very small least recently used (LRU) cache. It is very | |
| 12 // common for a JBIG2 dictionary to span multiple page in a PDF file, | |
| 13 // and we do not want to decode the same dictionary over an over | |
| 14 // again. We key off of the memory location of the dictionary. The | |
| 15 // list keeps track of the freshness of enties, with freshest ones are | |
| 16 // at the front. Even a tiny cache size like 2 makes a dramatic | |
| 17 // difference for typical JBIG2 documents. | |
| 18 const int kSymbolDictCacheMaxSize = 2; | |
| 19 static list<pair<FX_BYTE*, CJBig2_SymbolDict*> > symbol_dict_cache; | |
|
Lei Zhang
2014/12/12 00:01:20
You need std::
Lei Zhang
2014/12/12 00:01:20
I'd add a typedef for "pair<FX_BYTE*, CJBig2_Symbo
jab
2014/12/13 01:46:40
Done.
jab
2014/12/13 01:46:41
Done.
| |
| 20 | |
| 8 void OutputBitmap(CJBig2_Image* pImage) | 21 void OutputBitmap(CJBig2_Image* pImage) |
| 9 { | 22 { |
| 10 if(!pImage) { | 23 if(!pImage) { |
| 11 return; | 24 return; |
| 12 } | 25 } |
| 13 } | 26 } |
| 14 CJBig2_Context *CJBig2_Context::CreateContext(CJBig2_Module *pModule, FX_BYTE *p GlobalData, FX_DWORD dwGlobalLength, | 27 CJBig2_Context *CJBig2_Context::CreateContext(CJBig2_Module *pModule, FX_BYTE *p GlobalData, FX_DWORD dwGlobalLength, |
| 15 FX_BYTE *pData, FX_DWORD dwLength, FX_INT32 nStreamType, IFX_Pause* pPau se) | 28 FX_BYTE *pData, FX_DWORD dwLength, FX_INT32 nStreamType, IFX_Pause* pPau se) |
| 16 { | 29 { |
| 17 return new(pModule) CJBig2_Context(pGlobalData, dwGlobalLength, pData, dwLen gth, nStreamType, pPause); | 30 return new(pModule) CJBig2_Context(pGlobalData, dwGlobalLength, pData, dwLen gth, nStreamType, pPause); |
| (...skipping 589 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 607 FX_BYTE cSDHUFFDH, cSDHUFFDW, cSDHUFFBMSIZE, cSDHUFFAGGINST; | 620 FX_BYTE cSDHUFFDH, cSDHUFFDW, cSDHUFFBMSIZE, cSDHUFFAGGINST; |
| 608 CJBig2_HuffmanTable *Table_B1 = NULL, *Table_B2 = NULL, *Table_B3 = NULL, *T able_B4 = NULL, *Table_B5 = NULL; | 621 CJBig2_HuffmanTable *Table_B1 = NULL, *Table_B2 = NULL, *Table_B3 = NULL, *T able_B4 = NULL, *Table_B5 = NULL; |
| 609 FX_INT32 i, nIndex, nRet; | 622 FX_INT32 i, nIndex, nRet; |
| 610 CJBig2_Segment *pSeg = NULL, *pLRSeg = NULL; | 623 CJBig2_Segment *pSeg = NULL, *pLRSeg = NULL; |
| 611 FX_BOOL bUsed; | 624 FX_BOOL bUsed; |
| 612 CJBig2_Image ** SDINSYMS = NULL; | 625 CJBig2_Image ** SDINSYMS = NULL; |
| 613 CJBig2_SDDProc *pSymbolDictDecoder; | 626 CJBig2_SDDProc *pSymbolDictDecoder; |
| 614 JBig2ArithCtx *gbContext = NULL, *grContext = NULL; | 627 JBig2ArithCtx *gbContext = NULL, *grContext = NULL; |
| 615 CJBig2_ArithDecoder *pArithDecoder; | 628 CJBig2_ArithDecoder *pArithDecoder; |
| 616 JBIG2_ALLOC(pSymbolDictDecoder, CJBig2_SDDProc()); | 629 JBIG2_ALLOC(pSymbolDictDecoder, CJBig2_SDDProc()); |
| 630 FX_BYTE *key = pSegment->m_pData; | |
| 631 FX_BOOL cache_hit = false; | |
| 617 if(m_pStream->readShortInteger(&wFlags) != 0) { | 632 if(m_pStream->readShortInteger(&wFlags) != 0) { |
| 618 m_pModule->JBig2_Error("symbol dictionary segment : data header too shor t."); | 633 m_pModule->JBig2_Error("symbol dictionary segment : data header too shor t."); |
| 619 nRet = JBIG2_ERROR_TOO_SHORT; | 634 nRet = JBIG2_ERROR_TOO_SHORT; |
| 620 goto failed; | 635 goto failed; |
| 621 } | 636 } |
| 622 pSymbolDictDecoder->SDHUFF = wFlags & 0x0001; | 637 pSymbolDictDecoder->SDHUFF = wFlags & 0x0001; |
| 623 pSymbolDictDecoder->SDREFAGG = (wFlags >> 1) & 0x0001; | 638 pSymbolDictDecoder->SDREFAGG = (wFlags >> 1) & 0x0001; |
| 624 pSymbolDictDecoder->SDTEMPLATE = (wFlags >> 10) & 0x0003; | 639 pSymbolDictDecoder->SDTEMPLATE = (wFlags >> 10) & 0x0003; |
| 625 pSymbolDictDecoder->SDRTEMPLATE = (wFlags >> 12) & 0x0003; | 640 pSymbolDictDecoder->SDRTEMPLATE = (wFlags >> 12) & 0x0003; |
| 626 cSDHUFFDH = (wFlags >> 2) & 0x0003; | 641 cSDHUFFDH = (wFlags >> 2) & 0x0003; |
| (...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 784 gbContext = (JBig2ArithCtx*)m_pModule->JBig2_Malloc2(sizeof(JBig2Ari thCtx), dwTemp); | 799 gbContext = (JBig2ArithCtx*)m_pModule->JBig2_Malloc2(sizeof(JBig2Ari thCtx), dwTemp); |
| 785 JBIG2_memset(gbContext, 0, sizeof(JBig2ArithCtx)*dwTemp); | 800 JBIG2_memset(gbContext, 0, sizeof(JBig2ArithCtx)*dwTemp); |
| 786 } | 801 } |
| 787 if (pSymbolDictDecoder->SDREFAGG == 1) { | 802 if (pSymbolDictDecoder->SDREFAGG == 1) { |
| 788 dwTemp = pSymbolDictDecoder->SDRTEMPLATE ? 1 << 10 : 1 << 13; | 803 dwTemp = pSymbolDictDecoder->SDRTEMPLATE ? 1 << 10 : 1 << 13; |
| 789 grContext = (JBig2ArithCtx*)m_pModule->JBig2_Malloc2(sizeof(JBig2Ari thCtx), dwTemp); | 804 grContext = (JBig2ArithCtx*)m_pModule->JBig2_Malloc2(sizeof(JBig2Ari thCtx), dwTemp); |
| 790 JBIG2_memset(grContext, 0, sizeof(JBig2ArithCtx)*dwTemp); | 805 JBIG2_memset(grContext, 0, sizeof(JBig2ArithCtx)*dwTemp); |
| 791 } | 806 } |
| 792 } | 807 } |
| 793 pSegment->m_nResultType = JBIG2_SYMBOL_DICT_POINTER; | 808 pSegment->m_nResultType = JBIG2_SYMBOL_DICT_POINTER; |
| 794 if(pSymbolDictDecoder->SDHUFF == 0) { | 809 for(std::list<pair<FX_BYTE*, CJBig2_SymbolDict*> >::iterator it = |
| 795 JBIG2_ALLOC(pArithDecoder, CJBig2_ArithDecoder(m_pStream)); | 810 symbol_dict_cache.begin(); it != symbol_dict_cache.end(); ++it) { |
| 796 pSegment->m_Result.sd = pSymbolDictDecoder->decode_Arith(pArithDecoder, gbContext, grContext); | 811 if (it->first == key) { |
| 797 delete pArithDecoder; | 812 pSegment->m_Result.sd = it->second->DeepCopy(); |
| 798 if(pSegment->m_Result.sd == NULL) { | 813 symbol_dict_cache.erase(it); |
| 799 nRet = JBIG2_ERROR_FETAL; | 814 symbol_dict_cache.push_front(*it); |
| 800 goto failed; | 815 cache_hit = true; |
| 816 break; | |
| 801 } | 817 } |
| 802 m_pStream->alignByte(); | 818 } |
| 803 m_pStream->offset(2); | 819 if (!cache_hit) { |
| 804 } else { | 820 if(pSymbolDictDecoder->SDHUFF == 0) { |
| 805 pSegment->m_Result.sd = pSymbolDictDecoder->decode_Huffman(m_pStream, gb Context, grContext, pPause); | 821 JBIG2_ALLOC(pArithDecoder, CJBig2_ArithDecoder(m_pStream)); |
| 806 if(pSegment->m_Result.sd == NULL) { | 822 pSegment->m_Result.sd = pSymbolDictDecoder->decode_Arith(pArithDecod er, gbContext, grContext); |
| 807 nRet = JBIG2_ERROR_FETAL; | 823 delete pArithDecoder; |
| 808 goto failed; | 824 if(pSegment->m_Result.sd == NULL) { |
| 825 nRet = JBIG2_ERROR_FETAL; | |
| 826 goto failed; | |
| 827 } | |
| 828 m_pStream->alignByte(); | |
| 829 m_pStream->offset(2); | |
| 830 } else { | |
| 831 pSegment->m_Result.sd = pSymbolDictDecoder->decode_Huffman(m_pStream , gbContext, grContext, pPause); | |
| 832 if(pSegment->m_Result.sd == NULL) { | |
| 833 nRet = JBIG2_ERROR_FETAL; | |
| 834 goto failed; | |
| 835 } | |
| 836 m_pStream->alignByte(); | |
| 809 } | 837 } |
| 810 m_pStream->alignByte(); | 838 CJBig2_SymbolDict *value = pSegment->m_Result.sd->DeepCopy(); |
| 839 if (value) { | |
| 840 while (symbol_dict_cache.size() >= kSymbolDictCacheMaxSize) { | |
| 841 delete symbol_dict_cache.back().second; | |
| 842 symbol_dict_cache.pop_back(); | |
| 843 } | |
| 844 symbol_dict_cache.push_front( | |
| 845 std::pair<FX_BYTE*, CJBig2_SymbolDict*>(key, value)); | |
| 846 } | |
| 811 } | 847 } |
| 812 if(wFlags & 0x0200) { | 848 if(wFlags & 0x0200) { |
| 813 pSegment->m_Result.sd->m_bContextRetained = TRUE; | 849 pSegment->m_Result.sd->m_bContextRetained = TRUE; |
| 814 if(pSymbolDictDecoder->SDHUFF == 0) { | 850 if(pSymbolDictDecoder->SDHUFF == 0) { |
| 815 pSegment->m_Result.sd->m_gbContext = gbContext; | 851 pSegment->m_Result.sd->m_gbContext = gbContext; |
| 816 } | 852 } |
| 817 if(pSymbolDictDecoder->SDREFAGG == 1) { | 853 if(pSymbolDictDecoder->SDREFAGG == 1) { |
| 818 pSegment->m_Result.sd->m_grContext = grContext; | 854 pSegment->m_Result.sd->m_grContext = grContext; |
| 819 } | 855 } |
| 820 bUsed = TRUE; | 856 bUsed = TRUE; |
| (...skipping 982 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1803 SBSYMCODES[CURTEMP].code = CURCODE; | 1839 SBSYMCODES[CURTEMP].code = CURCODE; |
| 1804 CURCODE = CURCODE + 1; | 1840 CURCODE = CURCODE + 1; |
| 1805 } | 1841 } |
| 1806 CURTEMP = CURTEMP + 1; | 1842 CURTEMP = CURTEMP + 1; |
| 1807 } | 1843 } |
| 1808 CURLEN = CURLEN + 1; | 1844 CURLEN = CURLEN + 1; |
| 1809 } | 1845 } |
| 1810 m_pModule->JBig2_Free(LENCOUNT); | 1846 m_pModule->JBig2_Free(LENCOUNT); |
| 1811 m_pModule->JBig2_Free(FIRSTCODE); | 1847 m_pModule->JBig2_Free(FIRSTCODE); |
| 1812 } | 1848 } |
| OLD | NEW |