File: dpack.c

package info (click to toggle)
directfb 1.7.7-11
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 32,212 kB
  • sloc: ansic: 306,760; cpp: 46,357; sh: 11,720; makefile: 5,620; perl: 662; asm: 507; xml: 116
file content (460 lines) | stat: -rw-r--r-- 14,467 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
/*
   (c) Copyright 2012-2013  DirectFB integrated media GmbH
   (c) Copyright 2001-2013  The world wide DirectFB Open Source Community (directfb.org)
   (c) Copyright 2000-2004  Convergence (integrated media) GmbH

   All rights reserved.

   Written by Claudio Ciccani <klan@users.sf.net>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, write to the
   Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.
*/

#include <config.h>

#include <stdlib.h>
#include <string.h>

#include <direct/debug.h>
#include <direct/messages.h>
#include <direct/util.h>

#include "dpack.h"

/*
 * ABSTRACT:
 *    DPack (Delta Pack) is a loseless (or better, minimum-loss) compression method
 *    based on delta coding and bit packing. 
 *
 * BITSTREAM SYNTAX:
 *    The stream is encoded as a sequence of non-interleaved channels with the 1st
 *    channel staring at offset 0.
 *    Each channel if encoded as following:
 *      1 BYTE    - Header:
 *                       Bit 0-4: number of bits per absolute delta.
 *                       Bit 5  : 0 if unsigned deltas, 1 if signed;
 *                                add this bit to the previous value to
 *                                obtain the effective amount of bits per delta.
 *                       Bit 6  : deltas are encoded as the difference between
 *                                the effective delta and the minimum delta (MD).
 *                       Bit 7  : channels are coupled
 *
 *
 *      N BYTES   - First Sample:
 *                       N can be 1, 2 or 4, depending on the sample format.
 *
 *      IF [Header:6]
 *        N BYTES - Minimum Delta:
 *                       this is the minimum delta value for the whole packet;
 *                       N can be 1, 2 or 4, depending on the sample format.
 *      ENDIF
 *
 *      N BITS    - Deltas:
 *                       deltas are encoded using the following formalas
 *                        
 *                         ENCODING: IF [Header:7]
 *                                      I(t)[c] -= I(t)[c-1]
 *                                   ENDIF
 *
 *                                   D(t)[c] = (I(t)[c] - O(t-1)[c]) / 2
 *
 *                         DECODING: IF [Header:6]
 *                                      O(t)[c] = O(t-1)[c] + (D(t)[c] + MD) * 2
 *                                   ELSE
 *                                      O(t)[c] = O(t-1)[c] + D(t)[c] * 2
 *                                   ENDIF
 *
 *                                   IF [Header:7]
 *                                      O(t)[c] += O(t)[c-1]
 *                                   ENDIF
 *
 *                       where t is the frame index, c is the channel index,
 *                       D(t) is the delta at t, I(t) is the input sample
 *                       at t, O(t) is the output sample at t and MD is the
 *                       minimum delta;
 *                       the amount of bits used to store the delta is given by
 *                       the Header, while the effective number of deltas is an
 *                       input parameter for the decoder (i.e. you must know the
 *                       original length in frames of the packet to decode it).
 *
 *      N BITS    - Align:
 *                       empty bits used to align next channel to the byte boundary.
 *
 */

/**************************************************************************************************/

struct bitio {
     u8  *ptr;
     int  left;
};

static inline void
bitio_init( struct bitio *bio, void *ptr )
{
     bio->ptr  = ptr;
     bio->left = 8;
}

static inline void
bitio_put( struct bitio *bio, u32 val, int n )
{

     if (n >= 8) {
          if (!(bio->left & 7)) {
               do {
                    n -= 8;
                    *bio->ptr++ = val >> n;
               } while (n >= 8);
          }
          else {
               do {
                    u8 v;
                    n -= 8;
                    v = val >> n;
                    *(bio->ptr+0) |= v >> (8-bio->left);
                    *(bio->ptr+1)  = v << bio->left;
                    bio->ptr++;
               } while (n >= 8);
          }
     }

     while (n--) {
          if (!(bio->left & 7)) {
               *bio->ptr = ((val >> n) & 1) << 7;
               bio->left--;
          }
          else {
               *bio->ptr |= ((val >> n) & 1) << --bio->left;
               if (!bio->left) {
                    bio->left = 8;
                    bio->ptr++;
               }
          }
     }
}

static inline u32
bitio_get( struct bitio *bio, int n )
{
     u32 ret = 0;

     if (n >= 8) {
          if (!(bio->left & 7)) {
               do {
                    n -= 8;
                    ret |= *bio->ptr << n;
                    bio->ptr++;
               } while (n >= 8);
          }
          else {
               do {
                    n -= 8;
                    ret |= (u32)((u8)(*(bio->ptr+0) << (8-bio->left)) |
                                 (u8)(*(bio->ptr+1) >> bio->left)) << n;
                    bio->ptr++;
               } while (n >= 8);
          }
     }

     while (n--) {
          ret |= ((*bio->ptr >> --bio->left) & 1) << n;
          if (!bio->left) {
               bio->left = 8;
               bio->ptr++;
          }
     }
     
     return ret;
}

static inline s32
bitio_gets( struct bitio *bio, int n )
{
     s32 ret;

     if (n >= 8) {
          n -= 8;
          if (!(bio->left & 7)) {
               ret = *((s8*)bio->ptr) << n;
               bio->ptr++;
               while (n >= 8) {
                    n -= 8;
                    ret |= *bio->ptr << n;
                    bio->ptr++;
               } 
          }
          else {
               ret = (s32)((s8)(*(bio->ptr+0) << (8-bio->left)) |
                           (u8)(*(bio->ptr+1) >> bio->left)) << n;
               bio->ptr++;
               while (n >= 8) {
                    n -= 8;
                    ret |= (u32)((u8)(*(bio->ptr+0) << (8-bio->left)) |
                                 (u8)(*(bio->ptr+1) >> bio->left)) << n;
                    bio->ptr++;
               } 
          }
     }
     else {
          ret = -((*bio->ptr >> --bio->left) & 1) << --n;
          if (!bio->left) {
               bio->left = 8;
               bio->ptr++;
          }
     }

     while (n--) {
          ret |= ((*bio->ptr >> --bio->left) & 1) << n;
          if (!bio->left) {
               bio->left = 8;
               bio->ptr++;
          }
     }
     
     return ret;
}               

static inline void*
bitio_tell( struct bitio *bio )
{
     return (bio->left & 7) ? (bio->ptr+1) : bio->ptr;
}

/**************************************************************************************************/

static inline int 
bitcount( unsigned int val )
{
     int ret = 0;
     
     while (val & ~0xff) {
          val >>= 8;
          ret += 8;
     }
     
     while (val) {
          val >>= 1;
          ret++;
     }
     
     return ret;
}

/**************************************************************************************************/

typedef struct {
#ifdef WORDS_BIGENDIAN
     s8 c;
     u8 b;
     u8 a;
#else
     u8 a;
     u8 b;
     s8 c;
#endif
} __attribute__((packed)) s24;


/* Converted to S8 */
#define TYPE u8
#define SAMP_MAX 127
#define GET_SAMP(src) ((s8)(*((u8*)src) ^ 0x80))
#define PUT_SAMP(dst, s) *((u8*)dst) = (s) ^ 0x80
#define GET_CODED_SAMP(src) GET_SAMP(src)
#define PUT_CODED_SAMP(dst, s) PUT_SAMP(dst, s)
#include "dpack_proto.h"


#define TYPE s16
#define SAMP_MAX 32767
#define GET_SAMP(src) (*((s16*)src))
#define PUT_SAMP(dst, s) *((s16*)dst) = (s)
#define GET_CODED_SAMP(src) ((((s8*)src)[0] << 8) | ((u8*)src)[1])
#define PUT_CODED_SAMP(dst, s) ((s8*)dst)[0] = (s) >> 8; ((u8*)dst)[1] = (s)
#include "dpack_proto.h"


#define TYPE s24
#define SAMP_MAX 8388607
#define GET_SAMP(src) (((s24*)src)->a | (((s24*)src)->b << 8) | (((s24*)src)->c << 16))
#define PUT_SAMP(dst, s) ((s24*)dst)->a = (s); ((s24*)dst)->b = (s) >> 8; ((s24*)dst)->c = (s) >> 16
#define GET_CODED_SAMP(src) ((((s8*)src)[0] << 16) | (((u8*)src)[1] << 8) | ((u8*)src)[2])
#define PUT_CODED_SAMP(dst, s) ((s8*)dst)[0] = (s) >> 16; ((u8*)dst)[1] = (s) >> 8; ((u8*)dst)[2] = (s)
#include "dpack_proto.h"


/* Scaled down by 1 to prevent overflow */
#define TYPE s32
#define SAMP_MAX 1073741823
#define GET_SAMP(src) (*((s32*)src) >> 1)
#define PUT_SAMP(dst, s) *((s32*)dst) = (s) << 1
#define GET_CODED_SAMP(src) ((((s8*)src)[0]<<24) | (((u8*)src)[1]<<16) | (((u8*)src)[2]<<8) | ((u8*)src)[3])
#define PUT_CODED_SAMP(dst, s) ((s8*)dst)[0] = (s)>>24; ((u8*)dst)[1] = (s)>>16; ((u8*)dst)[2] = (s)>>8; ((u8*)dst)[3] = (s)
#include "dpack_proto.h"


/* Converted to S31 */
#define TYPE float
#define SAMP_MAX 1073741823
#define GET_SAMP(src) ((s32)(*((float*)src) * 1073741823.f))
#define PUT_SAMP(dst, s) *((float*)dst) = (float)(s) / 1073741823.f
#define GET_CODED_SAMP(src) ((((s8*)src)[0]<<24) | (((u8*)src)[1]<<16) | (((u8*)src)[2]<<8) | ((u8*)src)[3])
#define PUT_CODED_SAMP(dst, s) ((s8*)dst)[0] = (s)>>24; ((u8*)dst)[1] = (s)>>16; ((u8*)dst)[2] = (s)>>8; ((u8*)dst)[3] = (s)
#include "dpack_proto.h"



int 
dpack_encode( const void *source, FSSampleFormat format, int channels, int length, u8 *dest )
{
     int frames = length;
     int bytes  = channels * FS_BYTES_PER_SAMPLE(format);
     int size   = 0;
#if D_DEBUG_ENABLED
     static unsigned long ctotal = 0;
     static unsigned long rtotal = 0;
#endif
     
     D_ASSERT( source != NULL );
     D_ASSERT( channels > 0 );
     D_ASSERT( length > 0 );
     D_ASSERT( dest != NULL );
     
     switch (format) {
          case FSSF_U8:
               while (frames) {
                    int num = MIN(frames, DPACK_FRAMES);
                    size   += dpack_encode_u8( source, channels, num, dest+size );
                    source += num * bytes;
                    frames -= num;
               }                    
               break;
               
          case FSSF_S16:
               while (frames) {
                    int num = MIN(frames, DPACK_FRAMES);
                    size   += dpack_encode_s16( source, channels, num, dest+size );
                    source += num * bytes;
                    frames -= num;
               }
               break;
               
          case FSSF_S24:
               while (frames) {
                    int num = MIN(frames, DPACK_FRAMES);
                    size   += dpack_encode_s24( source, channels, num, dest+size );
                    source += num * bytes;
                    frames -= num;
               }
               break;
               
          case FSSF_S32:
               while (frames) {
                    int num = MIN(frames, DPACK_FRAMES);
                    size   += dpack_encode_s32( source, channels, num, dest+size );
                    source += num * bytes;
                    frames -= num;
               }
               break;
               
          case FSSF_FLOAT:
               while (frames) {
                    int num = MIN(frames, DPACK_FRAMES);
                    size   += dpack_encode_float( source, channels, num, dest+size );
                    source += num * bytes;
                    frames -= num;
               }
               break;
               
          default:
               D_WARN( "unsupported sample format" );
               return 0;
     }
     
     D_DEBUG( "DPACK: raw=%6d encoded=%6d ratio=%02d%% avg=%02d%%\n",
              length * bytes, size, size * 100 / (length * bytes),
              (int)({ctotal += size; rtotal += length * bytes; ctotal*100/rtotal;}) );
              
     return size;
}

int 
dpack_decode( const u8 *source, FSSampleFormat format, int channels, int length, void *dest )
{
     int frames = length;
     int bytes  = channels * FS_BYTES_PER_SAMPLE(format);
     int size   = 0;
     
     D_ASSERT( source != NULL );
     D_ASSERT( channels > 0 );
     D_ASSERT( length > 0 );
     D_ASSERT( dest != NULL );  
     
     switch (format) {
          case FSSF_U8:
               while (frames) {
                    int num = MIN(frames, DPACK_FRAMES);
                    size   += dpack_decode_u8( source+size, channels, num, dest );
                    dest   += num * bytes;
                    frames -= num;
               }
               break;
               
          case FSSF_S16:
               while (frames) {
                    int num = MIN(frames, DPACK_FRAMES);
                    size   += dpack_decode_s16( source+size, channels, num, dest );
                    dest   += num * bytes;
                    frames -= num;
               }
               break;
               
          case FSSF_S24:
               while (frames) {
                    int num = MIN(frames, DPACK_FRAMES);
                    size   += dpack_decode_s24( source+size, channels, num, dest );
                    dest   += num * bytes;
                    frames -= num;
               }
               break;
               
          case FSSF_S32:
               while (frames) {
                    int num = MIN(frames, DPACK_FRAMES);
                    size   += dpack_decode_s32( source+size, channels, num, dest );
                    dest   += num * bytes;
                    frames -= num;
               }
               break;
               
          case FSSF_FLOAT:
               while (frames) {
                    int num = MIN(frames, DPACK_FRAMES);
                    size   += dpack_decode_float( source+size, channels, num, dest );
                    dest   += num * bytes;
                    frames -= num;
               }
               break;
               
          default:
               D_WARN( "unsupported sample format" );
               return 0;
     }
      
     return size;
}