DVBCore  20.3.0
DVBCore Documentation
stbhuffman.c
Go to the documentation of this file.
1 /*******************************************************************************
2  * Copyright © 2014 The DTVKit Open Software Foundation Ltd (www.dtvkit.org)
3  * Copyright © 2010 Ocean Blue Software Ltd
4  *
5  * This file is part of a DTVKit Software Component
6  * You are permitted to copy, modify or distribute this file subject to the terms
7  * of the DTVKit 1.0 Licence which can be found in licence.txt or at www.dtvkit.org
8  *
9  * THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
10  * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES
11  * OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
12  *
13  * If you or your organisation is not a member of DTVKit then you have access
14  * to this source code outside of the terms of the licence agreement
15  * and you are expected to delete this and any associated files immediately.
16  * Further information on DTVKit, membership and terms can be found at www.dtvkit.org
17  *******************************************************************************/
25 /*#define ENABLE_DEBUG*/
26 
27 /*---includes for this file--------------------------------------------------*/
28 
29 /* compiler library header files */
30 
31 /* third party header files */
32 
33 /* DVBCore header files */
34 #include "techtype.h"
35 #include "dbgfuncs.h"
36 
37 #include <huffman_table1.h>
38 #include <huffman_table2.h>
39 
40 /*---constant definitions for this file--------------------------------------*/
41 
42 #ifdef ENABLE_DEBUG
43 #define DBG(x) STB_SPDebugWrite x
44 #else
45 #define DBG(x)
46 #endif
47 
48 /*---local typedef structs for this file-------------------------------------*/
49 
50 /*---local (static) variable declarations for this file----------------------*/
51 /* (internal variables declared static to make them local) */
52 
53 /*---local function prototypes for this file---------------------------------*/
54 /* (internal functions declared static to make them local) */
55 
56 /*---global function definitions---------------------------------------------*/
57 
58 /*!**************************************************************************
59  * @brief Decompresses the input buffer according to the BBC's Huffman algorithm
60  * as defined in the DTG D-Book 6.1 and using the licensed tables provided
61  * by the BBC that relate to the algorithm.
62  * @param encoding_type - Defines which table should be used
63  * @param input - start of buffer containing the encoded data
64  * @param output - allocated buffer into which the decoded data will be placed
65  * @param output_size - size of the supplied output buffer
66  * @return Number of bytes decompressed into the output buffer, 0 when the
67  * decompression failes.
68  ****************************************************************************/
69 U16BIT STB_HuffmanDecompress(U8BIT encoding_type, U8BIT *input, U8BIT *output, U16BIT output_size)
70 {
71  U8BIT *table = NULL;
72  U8BIT prev_char;
73  U8BIT value;
74  U8BIT mask;
75  U8BIT i, mask_pos;
76  U16BIT byte_offset;
77  U16BIT num_output_bytes = 0;
78  BOOLEAN finished;
79  BOOLEAN char_decoded;
80  BOOLEAN compressed_char;
81 
82  FUNCTION_START(STB_HuffmanDecompress);
83 
84  if ((encoding_type == 1) || (encoding_type == 2))
85  {
86  if (encoding_type == 1)
87  {
88 #ifndef HUFFMAN_TABLE_1_MISSING
89  table = huffman_table_1;
90 #endif
91  }
92  else
93  {
94 #ifndef HUFFMAN_TABLE_2_MISSING
95  table = huffman_table_2;
96 #endif
97  }
98 
99  if (table != NULL)
100  {
101  /* When starting, the previous byte is always 0 */
102  prev_char = 0;
103  finished = FALSE;
104  mask = 0x80;
105  mask_pos = 7;
106  compressed_char = FALSE;
107 
108  for (finished = FALSE; !finished && (num_output_bytes < output_size); )
109  {
110  if (((prev_char == 0x1b) && compressed_char) || ((prev_char >= 0x80) && !compressed_char))
111  {
112  compressed_char = FALSE;
113  for (i = 0, prev_char = 0; i < 8; i++)
114  {
115  prev_char <<= 1;
116  prev_char |= ((*input & mask) >> mask_pos);
117 
118  mask >>= 1;
119  mask_pos--;
120  if (mask == 0)
121  {
122  mask = 0x80;
123  mask_pos = 7;
124  input++;
125  }
126  }
127 
128  *output = prev_char;
129 
130  if (prev_char == 0)
131  {
132  finished = TRUE;
133  }
134  else
135  {
136  output++;
137  }
138 
139  num_output_bytes++;
140  }
141  else
142  {
143  compressed_char = TRUE;
144  byte_offset = (table[2 * prev_char] << 8) + table[2 * prev_char + 1];
145 
146  if ((*input & mask) == 0)
147  {
148  value = table[byte_offset];
149  }
150  else
151  {
152  value = table[byte_offset + 1];
153  }
154 
155  mask >>= 1;
156  mask_pos--;
157  if (mask == 0)
158  {
159  mask = 0x80;
160  mask_pos = 7;
161  input++;
162  }
163 
164  char_decoded = FALSE;
165 
166  while (!char_decoded)
167  {
168  if ((value & 0x80) == 0)
169  {
170  if ((*input & mask) == 0)
171  {
172  value = table[byte_offset + 2 * (value & 0x7f)];
173  }
174  else
175  {
176  value = table[byte_offset + 2 * (value & 0x7f) + 1];
177  }
178 
179  mask >>= 1;
180  mask_pos--;
181  if (mask == 0)
182  {
183  mask = 0x80;
184  mask_pos = 7;
185  input++;
186  }
187  }
188  else
189  {
190  prev_char = value & 0x7f;
191 
192  if (prev_char != 0x1b)
193  {
194  *output = prev_char;
195 
196  if (prev_char == 0)
197  {
198  finished = TRUE;
199  }
200  else
201  {
202  output++;
203  }
204 
205  num_output_bytes++;
206  }
207 
208  char_decoded = TRUE;
209  }
210  }
211  }
212  }
213 
214  if (!finished)
215  {
216  /* Failed to decompress the whole string. Put a NULL char on the end */
217  *(output - 1) = '\0';
218  }
219  }
220  }
221 
222  FUNCTION_FINISH(STB_HuffmanDecompress);
223 
224  return(num_output_bytes);
225 }
226 
Debug functions header file.
System Wide Global Technical Data Type Definitions.
U16BIT STB_HuffmanDecompress(U8BIT encoding_type, U8BIT *input, U8BIT *output, U16BIT output_size)
Decompresses the input buffer according to the BBC&#39;s Huffman algorithm as defined in the DTG D-Book 6...
Definition: stbhuffman.c:69
The data for this file is subject to the signing of a license agreement that can be obtained by conta...
The data for this file is subject to the signing of a license agreement that can be obtained by conta...