1 /*---------------------------------------------------------------------------*
2   Project:  Horizon
3   File:     cx_StreamingUncompression.h
4 
5   Copyright (C)2009-2010 Nintendo Co., Ltd./HAL Laboratory, Inc.  All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain
8   proprietary information of Nintendo of America Inc. and/or Nintendo
9   Company Ltd., and are protected by Federal copyright law.  They may
10   not be disclosed to third parties or copied or duplicated in any form,
11   in whole or in part, without the prior written consent of Nintendo.
12 
13   $Revision: 27173 $
14  *---------------------------------------------------------------------------*/
15 
16 #ifndef NN_CX_STREAMING_UNCOMPRESSION_H_
17 #define NN_CX_STREAMING_UNCOMPRESSION_H_
18 
19 #include <nn/types.h>
20 #include <nn/assert.h>
21 
22 namespace nn {
23 namespace cx {
24 
25 /*!
26     @brief      ランレングス圧縮データ展開用コンテキスト
27  */
28 struct UncompContextRL
29 {
30     u8 *destp;                         //!< 書き込み先ポインタ
31     s32 destCount;                     //!< 残り書き込みサイズ
32     s32 forceDestCount;                //!< 強制的に展開先サイズを設定
33     u16 length;                        //!< 連続書き込み残りサイズ
34     u8  flags;                         //!< 圧縮フラグ
35     u8  headerSize;                    //!< 読み込み途中のヘッダサイズ
36 };
37 
38 
39 /*!
40     @brief      LZ77 圧縮データ展開用コンテキスト
41  */
42 struct UncompContextLZ
43 {
44     u8 *destp;                         //!< 書き込み先ポインタ
45     s32 destCount;                     //!< 残り書き込みサイズ
46     s32 forceDestCount;                //!< 強制的に展開先サイズを設定
47     s32 length;                        //!< 連続書き込み残り長
48     u8  lengthFlg;                     //!< length取得済フラグ
49     u8  flags;                         //!< 圧縮フラグ
50     u8  flagIndex;                     //!< カレント圧縮フラグインデックス
51     u8  headerSize;                    //!< 読み込み途中のヘッダサイズ
52     u8  exFormat;                      //!< LZ77圧縮拡張オプション
53     NN_PADDING3;
54 };
55 
56 
57 /*!
58     @brief      ハフマン圧縮データ展開用コンテキスト
59  */
60 struct UncompContextHuffman
61 {
62     u8 *destp;                         //!< 書き込み先ポインタ
63     s32 destCount;                     //!< 残り書き込みサイズ
64     s32 forceDestCount;                //!< 強制的に展開先サイズを設定
65     u8 *treep;                         //!< ハフマン符号テーブル、カレントポインタ
66     u32 srcTmp;                        //!< 読み込み途中のデータ
67     u32 destTmp;                       //!< 復号化途中のデータ
68     s16 treeSize;                      //!< ハフマン符号テーブルサイズ
69     u8  srcTmpCnt;                     //!< 読み込み途中のデータサイズ
70     u8  destTmpCnt;                    //!< 復号化済のビット数
71     u8  bitSize;                       //!< 符号化ビットサイズ
72     u8  headerSize;                    //!< 読み込み途中のヘッダサイズ
73     NN_PADDING2;
74     u8  tree[0x200];                   //!< ハフマン符号テーブル
75 };
76 
77 
78 /*!
79     @brief      LZ-ハフマン複合圧縮データ展開用コンテキスト
80  */
81 struct UncompContextLH
82 {
83     u8   *destp;                         //!< 書き込み先ポインタ
84     s32  destCount;                      //!< 残り書き込みサイズ
85     s32  forceDestCount;                 //!< 強制的に展開先サイズを設定
86     u16  huffTable9 [ 1 << (9 + 1) ];    //!< ハフマン符号テーブル
87     u16  huffTable12[ 1 << (5 + 1) ];    //!< ハフマン符号テーブル
88     u16  *nodep;                         //!< ハフマンテーブルの探索中ノード
89     s32  tableSize9;                     //!< 読み込み途中のテーブルサイズ
90     s32  tableSize12;                    //!< 読み込み途中のテーブルサイズ
91     u32  tableIdx;                       //!< テーブル読み込み位置インデクス
92     u32  stream;                         //!< 読み込み用ビットストリーム
93     u32  stream_len;                     //!< 読み込み用ストリームの有効ビット数
94     u16  length;                         //!< LZ圧縮の読み取り長
95     s8   offset_bits;                    //!< オフセット情報のビット長
96     u8   headerSize;                     //!< 読み込み途中のヘッダサイズ
97 };
98 
99 
100 /*!
101     @brief      LZ-RangeCoder 複合圧縮データ展開用コンテキスト
102  */
103 struct UncompContextLRC
104 {
105     u8   *destp;                         //!< 書き込み先ポインタ
106     s32  destCount;                      //!< 残り書き込みサイズ
107     s32  forceDestCount;                 //!< 強制的に展開先サイズを設定
108     u32  freq9    [ 1 << 9  ];           //!< codeデータ頻度テーブル
109     u32  low_cnt9 [ 1 << 9  ];           //!< codeデータlow_cntテーブル
110     u32  freq12   [ 1 << 12 ];           //!< offsetデータ頻度テーブル
111     u32  low_cnt12[ 1 << 12 ];           //!< offsetデータlow_cntテーブル
112     u32  total9;                         //!< codeデータtotal値
113     u32  total12;                        //!< offsetデータtotal値
114     u32  range;                          //!< レンジコーダrange状態
115     u32  code;                           //!< レンジコーダcode状態
116     u32  low;                            //!< レンジコーダlow状態
117     u32  carry_cnt;                      //!< レンジコーダキャリー桁数
118     u8   carry;                          //!< レンジコーダキャリー状態
119     u8   codeLen;                        //!< レンジコーダ必要コード長
120     u16  length;                         //!< LZ圧縮の読み取り長
121     u8   headerSize;                     //!< 読み込み途中のヘッダサイズ
122     NN_PADDING3;
123 };
124 
125 
126 /*!
127     @brief      ランレングス圧縮データのストリーミング展開コンテキストを初期化します。
128     @param[in]  context     展開コンテキストへのポインタ
129     @param[in]  dest        展開先アドレス
130  */
131 void InitUncompContextRL( UncompContextRL *context, void *dest );
132 
133 
134 /*!
135     @brief      LZ77 圧縮データのストリーミング展開コンテキストを初期化します。
136 
137                 本 API は LZ 圧縮(旧互換)、LZ 拡張圧縮の両方に対応しています。
138 
139     @param[in]  context     展開コンテキストへのポインタ
140     @param[in]  dest        展開先アドレス
141  */
142 void InitUncompContextLZ( UncompContextLZ *context, void *dest );
143 
144 
145 /*!
146     @brief      ハフマン圧縮データのストリーミング展開コンテキストを初期化します。
147     @param[in]  context     展開コンテキストへのポインタ
148     @param[in]  dest        展開先アドレス
149  */
150 void InitUncompContextHuffman( UncompContextHuffman    *context,
151                                  void                      *dest );
152 
153 /*!
154     @brief      LZ-ハフマン複合圧縮データのストリーミング展開コンテキストを初期化します。
155     @param[in]  context     展開コンテキストへのポインタ
156     @param[in]  dest        展開先アドレス
157  */
158 void InitUncompContextLH( UncompContextLH * context, void* dest );
159 
160 
161 /*!
162     @brief      LZ-RangeCoder 複合圧縮データのストリーミング展開コンテキストを初期化します。
163     @param[in]  context     展開コンテキストへのポインタ
164     @param[in]  dest        展開先アドレス
165  */
166 void InitUncompContextLRC( UncompContextLRC * context, void* dest );
167 
168 
169 /*!
170     @brief      ランレングス圧縮データをストリーミング展開します。
171     @param[in]  context     展開コンテキストへのポインタ
172     @param[in]  data        圧縮データへのポインタ
173     @param[in]  len         データサイズ
174     @return     残り展開データサイズを返します。
175  */
176 s32 ReadUncompRL( UncompContextRL *context, const void *data, u32 len );
177 
178 
179 /*!
180     @brief      LZ77 圧縮データをストリーミング展開します。
181 
182                 本 API は LZ 圧縮(旧互換)、LZ 拡張圧縮の両方に対応しています。
183 
184     @param[in]  context     展開コンテキストへのポインタ
185     @param[in]  data        圧縮データへのポインタ
186     @param[in]  len         データサイズ
187     @return     残り展開データサイズを返します。
188  */
189 s32 ReadUncompLZ( UncompContextLZ *context, const void *data, u32 len );
190 
191 
192 /*!
193     @brief      ハフマン圧縮データをストリーミング展開します。
194     @param[in]  context     展開コンテキストへのポインタ
195     @param[in]  data        圧縮データへのポインタ
196     @param[in]  len         データサイズ
197     @return     残り展開データサイズを返します。
198  */
199 s32 ReadUncompHuffman( UncompContextHuffman *context, const void *data, u32 len );
200 
201 
202 /*!
203     @brief      LZ-ハフマン複合圧縮データをストリーミング展開します。
204     @param[in]  context     展開コンテキストへのポインタ
205     @param[in]  data        圧縮データへのポインタ
206     @param[in]  len         データサイズ
207     @return     残り展開データサイズを返します。
208  */
209 s32 ReadUncompLH( UncompContextLH *context, const void* data, u32 len );
210 
211 
212 /*!
213     @brief      LZ-RangeCoder 複合圧縮データをストリーミング展開します。
214     @param[in]  context     展開コンテキストへのポインタ
215     @param[in]  data        圧縮データへのポインタ
216     @param[in]  len         データサイズ
217     @return     残り展開データサイズを返します。
218  */
219 s32 ReadUncompLRC( UncompContextLRC *context, const void* data, u32 len );
220 
221 
222 /*!
223     @brief      圧縮のストリーミング展開が完了しているかどうかを判定します。
224     @param[in]  context     展開コンテキストへのポインタ
225     @return     展開が完了している場合には true を、
226                 まだ読み込むべきデータが残っている場合には false を返します
227  */
228 template <typename UncompContext>
IsFinishedUncompRL(const UncompContext * context)229 inline bool IsFinishedUncompRL( const UncompContext *context )
230 {
231     return (context->destCount > 0 || context->headerSize > 0)? false : true;
232 }
233 
234 
235 /*!
236     @brief      ランレングス圧縮データのストリーミング展開コンテキストを、
237                 展開サイズを指定して初期化します。
238     @param[in]  context     展開コンテキストへのポインタ
239     @param[in]  dest        展開先アドレス
240     @param[in]  destSize    展開サイズ
241  */
InitUncompContextRLFront(UncompContextRL * context,void * dest,s32 destSize)242 inline void InitUncompContextRLFront( UncompContextRL *context, void *dest, s32 destSize )
243 {
244     NN_ASSERT( destSize > 0 );
245     InitUncompContextRL( context, dest );
246     context->forceDestCount = destSize;
247 }
248 
249 
250 /*!
251     @brief      LZ77 圧縮データのストリーミング展開コンテキストを、
252                 展開サイズを指定して初期化します。
253 
254                 本 API は LZ 圧縮(旧互換)、LZ 拡張圧縮の両方に対応しています。
255 
256     @param[in]  context     展開コンテキストへのポインタ
257     @param[in]  dest        展開先アドレス
258     @param[in]  destSize    展開サイズ
259  */
InitUncompContextLZFront(UncompContextLZ * context,void * dest,s32 destSize)260 inline void InitUncompContextLZFront( UncompContextLZ *context, void *dest, s32 destSize )
261 {
262     NN_ASSERT( destSize > 0 );
263     InitUncompContextLZ( context, dest );
264     context->forceDestCount = destSize;
265 }
266 
267 
268 /*!
269     @brief      ハフマン圧縮データのストリーミング展開コンテキストを、
270                 展開サイズを指定して初期化します。
271     @param[in]  context     展開コンテキストへのポインタ
272     @param[in]  dest        展開先アドレス
273     @param[in]  destSize    展開サイズ
274  */
InitUncompContextHuffmanFront(UncompContextHuffman * context,void * dest,s32 destSize)275 inline void InitUncompContextHuffmanFront( UncompContextHuffman *context, void *dest, s32 destSize )
276 {
277     NN_ASSERT( destSize > 0 );
278     InitUncompContextHuffman( context, dest );
279     context->forceDestCount = destSize;
280 }
281 
282 
283 /*!
284     @brief      LZ-ハフマン複合圧縮データのストリーミング展開コンテキストを、
285                 展開サイズを指定して初期化します。
286     @param[in]  context     展開コンテキストへのポインタ
287     @param[in]  dest        展開先アドレス
288     @param[in]  destSize    展開サイズ
289  */
InitUncompContextLHFront(UncompContextLH * context,void * dest,s32 destSize)290 inline void InitUncompContextLHFront( UncompContextLH *context, void *dest, s32 destSize )
291 {
292     NN_ASSERT( destSize > 0 );
293     InitUncompContextLH( context, dest );
294     context->forceDestCount = destSize;
295 }
296 
297 
298 /*!
299     @brief      LZ-RangeCoder 複合圧縮データのストリーミング展開コンテキストを、
300                 展開サイズを指定して初期化します。
301     @param[in]  context     展開コンテキストへのポインタ
302     @param[in]  dest        展開先アドレス
303     @param[in]  destSize    展開サイズ
304  */
InitUncompContextLRCFront(UncompContextLRC * context,void * dest,s32 destSize)305 inline void InitUncompContextLRCFront( UncompContextLRC *context, void *dest, s32 destSize )
306 {
307     NN_ASSERT( destSize > 0 );
308     InitUncompContextLRC( context, dest );
309     context->forceDestCount = destSize;
310 }
311 
312 }   // namespace cx
313 }   // namespace nn
314 
315 /* NN_CX_STREAMING_UNCOMPRESSION_H_ */
316 #endif
317