summaryrefslogtreecommitdiffstats
path: root/media/libstagefright/codecs/on2/h264dec/omxdl/arm_neon/api/armCOMM_BitDec_s.h
blob: c738f72d8125c03c57d13e6250d1ba034f89abb5 (plain)
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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
;//
;// 
;// File Name:  armCOMM_BitDec_s.h
;// OpenMAX DL: v1.0.2
;// Revision:   12290
;// Date:       Wednesday, April 9, 2008
;// 
;// (c) Copyright 2007-2008 ARM Limited. All Rights Reserved.
;// 
;// 
;// 
;// OpenMAX optimized bitstream decode module
;//
;// You must include armCOMM_s.h before including this file
;//
;// This module provides macros to perform assembly optimized fixed and
;// variable length decoding from a read-only bitstream. The variable
;// length decode modules take as input a pointer to a table of 16-bit
;// entries of the following format.
;//
;// VLD Table Entry format
;//
;//        15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00
;//       +------------------------------------------------+
;//       |  Len   |               Symbol              | 1 |
;//       +------------------------------------------------+
;//       |                Offset                      | 0 |
;//       +------------------------------------------------+
;//
;// If the table entry is a leaf entry then bit 0 set:
;//    Len    = Number of bits overread (0 to 7)
;//    Symbol = Symbol payload (unsigned 12 bits)
;//
;// If the table entry is an internal node then bit 0 is clear:
;//    Offset = Number of (16-bit) half words from the table
;//             start to the next table node
;//
;// The table is accessed by successive lookup up on the
;// next Step bits of the input bitstream until a leaf node
;// is obtained. The Step sizes are supplied to the VLD macro.
;//
;// USAGE:
;//
;// To use any of the macros in this package, first call:
;//
;//    M_BD_INIT ppBitStream, pBitOffset, pBitStream, RBitBuffer, RBitCount, Tmp
;//
;// This caches the current bitstream position and next available
;// bits in registers pBitStream, RBitBuffer, RBitCount. These registers
;// are reserved for use by the bitstream decode package until you
;// call M_BD_FINI.
;//
;// Next call the following macro(s) as many times as you need:
;//
;//    M_BD_LOOK8       - Look ahead constant 1<=N<=8  bits into the bitstream
;//    M_BD_LOOK16      - Look ahead constant 1<=N<=16 bits into the bitstream
;//    M_BD_READ8       - Read constant 1<=N<=8  bits from the bitstream
;//    M_BD_READ16      - Read constant 1<=N<=16 bits from the bitstream
;//    M_BD_VREAD8      - Read variable 1<=N<=8  bits from the bitstream
;//    M_BD_VREAD16     - Read variable 1<=N<=16 bits from the bitstream
;//    M_BD_VLD         - Perform variable length decode using lookup table
;//
;// Finally call the macro:
;//
;//    M_BD_FINI ppBitStream, pBitOffset
;//
;// This writes the bitstream state back to memory.
;//
;// The three bitstream cache register names are assigned to the following global
;// variables:
;//

        GBLS    pBitStream  ;// Register name for pBitStream
        GBLS    BitBuffer   ;// Register name for BitBuffer
        GBLS    BitCount    ;// Register name for BitCount
   
;//        
;// These register variables must have a certain defined state on entry to every bitstream
;// macro (except M_BD_INIT) and on exit from every bitstream macro (except M_BD_FINI).
;// The state may depend on implementation.
;//
;// For the default (ARM11) implementation the following hold:
;//    pBitStream - points to the first byte not held in the BitBuffer
;//    BitBuffer  - is a cache of (4 bytes) 32 bits, bit 31 the first bit
;//    BitCount   - is offset (from the top bit) to the next unused bitstream bit
;//    0<=BitCount<=15 (so BitBuffer holds at least 17 unused bits)
;//
;//

        ;// Bitstream Decode initialise
        ;//
        ;// Initialises the bitstream decode global registers from
        ;// bitstream pointers. This macro is split into 3 parts to enable
        ;// scheduling.
        ;//
        ;// Input Registers:
        ;//
        ;// $ppBitStream    - pointer to pointer to the next bitstream byte
        ;// $pBitOffset     - pointer to the number of bits used in the current byte (0..7)
        ;// $RBitStream     - register to use for pBitStream (can be $ppBitStream)
        ;// $RBitBuffer     - register to use for BitBuffer
        ;// $RBitCount      - register to use for BitCount   (can be $pBitOffset)
        ;//
        ;// Output Registers:
        ;//
        ;// $T1,$T2,$T3     - registers that must be preserved between calls to
        ;//                   M_BD_INIT1 and M_BD_INIT2
        ;// $pBitStream     \ 
        ;// $BitBuffer       } See description above.
        ;// $BitCount       / 
        ;//
        MACRO
        M_BD_INIT0  $ppBitStream, $pBitOffset, $RBitStream, $RBitBuffer, $RBitCount

pBitStream  SETS "$RBitStream"
BitBuffer   SETS "$RBitBuffer"
BitCount    SETS "$RBitCount"        
        
        ;// load inputs
        LDR     $pBitStream, [$ppBitStream]
        LDR     $BitCount, [$pBitOffset]
        MEND
        
        MACRO
        M_BD_INIT1  $T1, $T2, $T3
        LDRB    $T2, [$pBitStream, #2]
        LDRB    $T1, [$pBitStream, #1]
        LDRB    $BitBuffer,  [$pBitStream], #3
        ADD     $BitCount, $BitCount, #8
        MEND
        
        MACRO
        M_BD_INIT2  $T1, $T2, $T3
        ORR     $T2, $T2, $T1, LSL #8
        ORR     $BitBuffer, $T2, $BitBuffer, LSL #16
        MEND    
        
        ;//
        ;// Look ahead fixed 1<=N<=8 bits without consuming any bits
        ;// The next bits will be placed at bit 31..24 of destination register
        ;//
        ;// Input Registers:
        ;//
        ;// $N              - number of bits to look
        ;// $pBitStream     \ 
        ;// $BitBuffer       } See description above.
        ;// $BitCount       / 
        ;// 
        ;// Output Registers:
        ;//
        ;// $Symbol         - the next N bits of the bitstream
        ;// $T1             - corrupted temp/scratch register
        ;// $pBitStream     \ 
        ;// $BitBuffer       } See description above.
        ;// $BitCount       / 
        ;//
        MACRO
        M_BD_LOOK8  $Symbol, $N
        ASSERT  ($N>=1):LAND:($N<=8)
        MOV     $Symbol, $BitBuffer, LSL $BitCount
        MEND
        
        ;//
        ;// Look ahead fixed 1<=N<=16 bits without consuming any bits
        ;// The next bits will be placed at bit 31..16 of destination register
        ;//
        ;// Input Registers:
        ;//
        ;// $N              - number of bits to look
        ;// $pBitStream     \ 
        ;// $BitBuffer       } See description above.
        ;// $BitCount       / 
        ;// 
        ;// Output Registers:
        ;//
        ;// $Symbol         - the next N bits of the bitstream
        ;// $T1             - corrupted temp/scratch register
        ;// $pBitStream     \ 
        ;// $BitBuffer       } See description above.
        ;// $BitCount       / 
        ;//
        MACRO
        M_BD_LOOK16  $Symbol, $N, $T1
        ASSERT  ($N >= 1):LAND:($N <= 16)
        MOV     $Symbol, $BitBuffer, LSL $BitCount
        MEND
        
        ;//
        ;// Skips fixed 1<=N<=8 bits from the bitstream, advancing the bitstream pointer
        ;//
        ;// Input Registers:
        ;//
        ;// $N              - number of bits
        ;// $pBitStream     \ 
        ;// $BitBuffer       } See description above.
        ;// $BitCount       / 
        ;// 
        ;// Output Registers:
        ;//
        ;// $T1             - corrupted temp/scratch register
        ;// $pBitStream     \ 
        ;// $BitBuffer       } See description above.
        ;// $BitCount       / 
        ;//
        MACRO
        M_BD_SKIP8 $N, $T1
        ASSERT  ($N>=1):LAND:($N<=8)        
        SUBS    $BitCount, $BitCount, #(8-$N)
        LDRCSB  $T1, [$pBitStream], #1   
        ADDCC   $BitCount, $BitCount, #8
        ORRCS   $BitBuffer, $T1, $BitBuffer, LSL #8
        MEND
        
        
        ;//
        ;// Read fixed 1<=N<=8 bits from the bitstream, advancing the bitstream pointer
        ;//
        ;// Input Registers:
        ;//
        ;// $N              - number of bits to read
        ;// $pBitStream     \ 
        ;// $BitBuffer       } See description above.
        ;// $BitCount       / 
        ;// 
        ;// Output Registers:
        ;//
        ;// $Symbol         - the next N bits of the bitstream
        ;// $T1             - corrupted temp/scratch register
        ;// $pBitStream     \ 
        ;// $BitBuffer       } See description above.
        ;// $BitCount       / 
        ;//
        MACRO
        M_BD_READ8 $Symbol, $N, $T1
        ASSERT  ($N>=1):LAND:($N<=8)                
        MOVS    $Symbol, $BitBuffer, LSL $BitCount        
        SUBS    $BitCount, $BitCount, #(8-$N)
        LDRCSB  $T1, [$pBitStream], #1   
        ADDCC   $BitCount, $BitCount, #8
        MOV     $Symbol, $Symbol, LSR #(32-$N)
        ORRCS   $BitBuffer, $T1, $BitBuffer, LSL #8
        MEND

        ;//
        ;// Read fixed 1<=N<=16 bits from the bitstream, advancing the bitstream pointer
        ;//
        ;// Input Registers:
        ;//
        ;// $N              - number of bits to read
        ;// $pBitStream     \ 
        ;// $BitBuffer       } See description above.
        ;// $BitCount       / 
        ;// 
        ;// Output Registers:
        ;//
        ;// $Symbol         - the next N bits of the bitstream
        ;// $T1             - corrupted temp/scratch register
        ;// $T2             - corrupted temp/scratch register
        ;// $pBitStream     \ 
        ;// $BitBuffer       } See description above.
        ;// $BitCount       / 
        ;//
        MACRO
        M_BD_READ16 $Symbol, $N, $T1, $T2
        ASSERT  ($N>=1):LAND:($N<=16)
        ASSERT  $Symbol<>$T1
        IF ($N<=8)
            M_BD_READ8  $Symbol, $N, $T1
        ELSE        
            ;// N>8 so we will be able to refill at least one byte            
            LDRB    $T1, [$pBitStream], #1            
            MOVS    $Symbol, $BitBuffer, LSL $BitCount
            ORR     $BitBuffer, $T1, $BitBuffer, LSL #8                       
            SUBS    $BitCount, $BitCount, #(16-$N)
            LDRCSB  $T1, [$pBitStream], #1            
            MOV     $Symbol, $Symbol, LSR #(32-$N)
            ADDCC   $BitCount, $BitCount, #8
            ORRCS   $BitBuffer, $T1, $BitBuffer, LSL #8
        ENDIF
        MEND
        
        ;//
        ;// Skip variable 1<=N<=8 bits from the bitstream, advancing the bitstream pointer.
        ;//
        ;// Input Registers:
        ;//
        ;// $N              - number of bits. 1<=N<=8
        ;// $pBitStream     \ 
        ;// $BitBuffer       } See description above.
        ;// $BitCount       / 
        ;// 
        ;// Output Registers:
        ;//
        ;// $T1             - corrupted temp/scratch register
        ;// $T2             - corrupted temp/scratch register
        ;// $pBitStream     \ 
        ;// $BitBuffer       } See description above.
        ;// $BitCount       / 
        ;//
        MACRO
        M_BD_VSKIP8 $N, $T1
        ADD     $BitCount, $BitCount, $N
        SUBS    $BitCount, $BitCount, #8
        LDRCSB  $T1, [$pBitStream], #1        
        ADDCC   $BitCount, $BitCount, #8
        ORRCS   $BitBuffer, $T1, $BitBuffer, LSL #8
        MEND        
        
        ;//
        ;// Skip variable 1<=N<=16 bits from the bitstream, advancing the bitstream pointer.
        ;//
        ;// Input Registers:
        ;//
        ;// $N              - number of bits. 1<=N<=16
        ;// $pBitStream     \ 
        ;// $BitBuffer       } See description above.
        ;// $BitCount       / 
        ;// 
        ;// Output Registers:
        ;//
        ;// $T1             - corrupted temp/scratch register
        ;// $T2             - corrupted temp/scratch register
        ;// $pBitStream     \ 
        ;// $BitBuffer       } See description above.
        ;// $BitCount       / 
        ;//
        MACRO
        M_BD_VSKIP16 $N, $T1, $T2
        ADD     $BitCount, $BitCount, $N
        SUBS    $BitCount, $BitCount, #8
        LDRCSB  $T1, [$pBitStream], #1        
        ORRCS   $BitBuffer, $T1, $BitBuffer, LSL #8
        SUBCSS  $BitCount, $BitCount, #8        
        LDRCSB  $T1, [$pBitStream], #1
        ADDCC   $BitCount, $BitCount, #8
        ORRCS   $BitBuffer, $T1, $BitBuffer, LSL #8
        MEND        

        ;//
        ;// Read variable 1<=N<=8 bits from the bitstream, advancing the bitstream pointer.
        ;//
        ;// Input Registers:
        ;//
        ;// $N              - number of bits to read. 1<=N<=8
        ;// $pBitStream     \ 
        ;// $BitBuffer       } See description above.
        ;// $BitCount       / 
        ;// 
        ;// Output Registers:
        ;//
        ;// $Symbol         - the next N bits of the bitstream
        ;// $T1             - corrupted temp/scratch register
        ;// $T2             - corrupted temp/scratch register
        ;// $pBitStream     \ 
        ;// $BitBuffer       } See description above.
        ;// $BitCount       / 
        ;//
        MACRO
        M_BD_VREAD8 $Symbol, $N, $T1, $T2
        MOV     $Symbol, $BitBuffer, LSL $BitCount        
        ADD     $BitCount, $BitCount, $N
        SUBS    $BitCount, $BitCount, #8
        LDRCSB  $T1, [$pBitStream], #1        
        RSB     $T2, $N, #32        
        ADDCC   $BitCount, $BitCount, #8
        MOV     $Symbol, $Symbol, LSR $T2
        ORRCS   $BitBuffer, $T1, $BitBuffer, LSL #8
        MEND


        ;//
        ;// Read variable 1<=N<=16 bits from the bitstream, advancing the bitstream pointer.
        ;//
        ;// Input Registers:
        ;//
        ;// $N              - number of bits to read. 1<=N<=16
        ;// $pBitStream     \ 
        ;// $BitBuffer       } See description above.
        ;// $BitCount       / 
        ;// 
        ;// Output Registers:
        ;//
        ;// $Symbol         - the next N bits of the bitstream
        ;// $T1             - corrupted temp/scratch register
        ;// $T2             - corrupted temp/scratch register
        ;// $pBitStream     \ 
        ;// $BitBuffer       } See description above.
        ;// $BitCount       / 
        ;//
        MACRO
        M_BD_VREAD16 $Symbol, $N, $T1, $T2
        MOV     $Symbol, $BitBuffer, LSL $BitCount        
        ADD     $BitCount, $BitCount, $N
        SUBS    $BitCount, $BitCount, #8
        LDRCSB  $T1, [$pBitStream], #1        
        RSB     $T2, $N, #32        
        ORRCS   $BitBuffer, $T1, $BitBuffer, LSL #8
        SUBCSS  $BitCount, $BitCount, #8        
        LDRCSB  $T1, [$pBitStream], #1
        ADDCC   $BitCount, $BitCount, #8
        MOV     $Symbol, $Symbol, LSR $T2
        ORRCS   $BitBuffer, $T1, $BitBuffer, LSL #8
        MEND


        ;//
        ;// Decode a code of the form 0000...001 where there
        ;// are N zeros before the 1 and N<=15 (code length<=16)
        ;//
        ;// Input Registers:
        ;//
        ;// $pBitStream     \ 
        ;// $BitBuffer       } See description above.
        ;// $BitCount       / 
        ;// 
        ;// Output Registers:
        ;//
        ;// $Symbol         - the number of zeros before the next 1
        ;//                   >=16 is an illegal code
        ;// $T1             - corrupted temp/scratch register
        ;// $T2             - corrupted temp/scratch register
        ;// $pBitStream     \ 
        ;// $BitBuffer       } See description above.
        ;// $BitCount       / 
        ;//        
        MACRO
        M_BD_CLZ16 $Symbol, $T1, $T2
        MOVS    $Symbol, $BitBuffer, LSL $BitCount
        CLZ     $Symbol, $Symbol                
        ADD     $BitCount, $BitCount, $Symbol
        SUBS    $BitCount, $BitCount, #7        ;// length is Symbol+1
        LDRCSB  $T1, [$pBitStream], #1
        ORRCS   $BitBuffer, $T1, $BitBuffer, LSL #8
        SUBCSS  $BitCount, $BitCount, #8        
        LDRCSB  $T1, [$pBitStream], #1
        ADDCC   $BitCount, $BitCount, #8
        ORRCS   $BitBuffer, $T1, $BitBuffer, LSL #8
        MEND  

        ;//
        ;// Decode a code of the form 1111...110 where there
        ;// are N ones before the 0 and N<=15 (code length<=16)
        ;//
        ;// Input Registers:
        ;//
        ;// $pBitStream     \ 
        ;// $BitBuffer       } See description above.
        ;// $BitCount       / 
        ;// 
        ;// Output Registers:
        ;//
        ;// $Symbol         - the number of zeros before the next 1
        ;//                   >=16 is an illegal code
        ;// $T1             - corrupted temp/scratch register
        ;// $T2             - corrupted temp/scratch register
        ;// $pBitStream     \ 
        ;// $BitBuffer       } See description above.
        ;// $BitCount       / 
        ;//        
        MACRO
        M_BD_CLO16 $Symbol, $T1, $T2
        MOV     $Symbol, $BitBuffer, LSL $BitCount
        MVN     $Symbol, $Symbol
        CLZ     $Symbol, $Symbol                
        ADD     $BitCount, $BitCount, $Symbol
        SUBS    $BitCount, $BitCount, #7        ;// length is Symbol+1
        LDRCSB  $T1, [$pBitStream], #1
        ORRCS   $BitBuffer, $T1, $BitBuffer, LSL #8
        SUBCSS  $BitCount, $BitCount, #8        
        LDRCSB  $T1, [$pBitStream], #1
        ADDCC   $BitCount, $BitCount, #8
        ORRCS   $BitBuffer, $T1, $BitBuffer, LSL #8
        MEND  


        ;//
        ;// Variable Length Decode module
        ;//
        ;// Decodes one VLD Symbol from a bitstream and refill the bitstream
        ;// buffer.
        ;//
        ;// Input Registers:
        ;//
        ;// $pVLDTable      - pointer to VLD decode table of 16-bit entries.
        ;//                   The format is described above at the start of
        ;//                   this file.
        ;// $S0             - The number of bits to look up for the first step
        ;//                   1<=$S0<=8
        ;// $S1             - The number of bits to look up for each subsequent
        ;//                   step 1<=$S1<=$S0.
        ;//
        ;// $pBitStream     \ 
        ;// $BitBuffer       } See description above.
        ;// $BitCount       / 
        ;// 
        ;// Output Registers:
        ;//
        ;// $Symbol         - decoded VLD symbol value
        ;// $T1             - corrupted temp/scratch register
        ;// $T2             - corrupted temp/scratch register
        ;// $pBitStream     \ 
        ;// $BitBuffer       } See description above.
        ;// $BitCount       / 
        ;//
        MACRO
        M_BD_VLD $Symbol, $T1, $T2, $pVLDTable, $S0, $S1
        ASSERT (1<=$S0):LAND:($S0<=8)
        ASSERT (1<=$S1):LAND:($S1<=$S0)
        
        ;// Note 0<=BitCount<=15 on entry and exit
        
        MOVS    $T1, $BitBuffer, LSL $BitCount       ;// left align next bits
        MOVS    $Symbol, #(2<<$S0)-2                 ;// create mask
        AND     $Symbol, $Symbol, $T1, LSR #(31-$S0) ;// 2*(next $S0 bits)
        SUBS    $BitCount, $BitCount, #8             ;// CS if buffer can be filled
01
        LDRCSB  $T1, [$pBitStream], #1               ;// load refill byte
        LDRH    $Symbol, [$pVLDTable, $Symbol]       ;// load table entry
        ADDCC   $BitCount, $BitCount, #8             ;// refill not possible
        ADD     $BitCount, $BitCount, #$S0           ;// assume $S0 bits used
        ORRCS   $BitBuffer, $T1, $BitBuffer, LSL #8  ;// merge in refill byte
        MOVS    $T1, $Symbol, LSR #1                 ;// CS=leaf entry
        BCS     %FT02
        
        MOVS    $T1, $BitBuffer, LSL $BitCount       ;// left align next bit
        IF (2*$S0-$S1<=8)
            ;// Can combine refill check and -S0+S1 and keep $BitCount<=15
            SUBS    $BitCount, $BitCount, #8+($S0-$S1)
        ELSE
            ;// Separate refill check and -S0+S1 offset
            SUBS  $BitCount, $BitCount, #8
            SUB   $BitCount, $BitCount, #($S0-$S1)
        ENDIF
        ADD     $Symbol, $Symbol, $T1, LSR #(31-$S1) ;// add 2*(next $S1 bits) to
        BIC     $Symbol, $Symbol, #1                 ;//   table offset
        B       %BT01                                ;// load next table entry
02
        ;// BitCount range now depend on the route here
        ;// if (first step)       S0 <= BitCount <= 7+S0        <=15
        ;// else if (2*S0-S1<=8)  S0 <= BitCount <= 7+(2*S0-S1) <=15
        ;// else                  S1 <= BitCount <= 7+S1        <=15
        
        SUB     $BitCount, $BitCount, $Symbol, LSR#13
        BIC     $Symbol, $T1, #0xF000
        MEND
        

        ;// Add an offset number of bits
        ;//
        ;// Outputs destination byte and bit index values which corresponds to an offset number of bits 
        ;// from the current location. This is used to compare bitstream positions using. M_BD_CMP.
        ;//
        ;// Input Registers:
        ;//
        ;// $Offset         - Offset to be added in bits.
        ;// $pBitStream     \ 
        ;// $BitBuffer       } See description above.
        ;// $BitCount       / 
        ;//
        ;// Output Registers:
        ;//
        ;// $ByteIndex      - Destination pBitStream pointer after adding the Offset. 
        ;//                   This value will be 4 byte ahead and needs to subtract by 4 to get exact 
        ;//                   pointer (as in M_BD_FINI). But for using with M_BD_CMP subtract is not needed.
        ;// $BitIndex       - Destination BitCount after the addition of Offset number of bits
        ;//
        MACRO
        M_BD_ADD  $ByteIndex, $BitIndex, $Offset

        ;// ($ByteIndex,$BitIndex) = Current position + $Offset bits
        ADD     $Offset, $Offset, $BitCount
        AND     $BitIndex, $Offset, #7
        ADD     $ByteIndex, $pBitStream, $Offset, ASR #3        
        MEND

        ;// Move bitstream pointers to the location given
        ;//
        ;// Outputs destination byte and bit index values which corresponds to  
        ;// the current location given (calculated using M_BD_ADD). 
        ;//
        ;// Input Registers:
        ;//
        ;// $pBitStream     \ 
        ;// $BitBuffer       } See description above.
        ;// $BitCount       / 
        ;// $ByteIndex      - Destination pBitStream pointer after move. 
        ;//                   This value will be 4 byte ahead and needs to subtract by 4 to get exact 
        ;//                   pointer (as in M_BD_FINI).
        ;// $BitIndex       - Destination BitCount after the move
        ;//
        ;// Output Registers:
        ;//
        ;// $pBitStream     \ 
        ;//                  } See description above.  
        ;// $BitCount       / 
        ;//
        MACRO
        M_BD_MOV  $ByteIndex, $BitIndex

        ;// ($pBitStream, $Offset) = ($ByteIndex,$BitIndex)
        MOV     $BitCount, $BitIndex
        MOV     $pBitStream, $ByteIndex
        MEND

        ;// Bitstream Compare
        ;//
        ;// Compares bitstream position with that of a destination position. Destination position 
        ;// is held in two input registers which are calculated using M_BD_ADD macro
        ;//
        ;// Input Registers:
        ;//
        ;// $ByteIndex      - Destination pBitStream pointer, (4 byte ahead as described in M_BD_ADD)
        ;// $BitIndex       - Destination BitCount
        ;// $pBitStream     \ 
        ;// $BitBuffer       } See description above.
        ;// $BitCount       / 
        ;//
        ;// Output Registers:
        ;//
        ;// FLAGS           - GE if destination is reached, LT = is destination is ahead
        ;// $T1             - corrupted temp/scratch register
        ;//
        MACRO
        M_BD_CMP  $ByteIndex, $BitIndex, $T1
        
        ;// Return flags set by (current positon)-($ByteIndex,$BitIndex)
        ;// so GE means that we have reached the indicated position

        ADD         $T1, $pBitStream, $BitCount, LSR #3
        CMP         $T1, $ByteIndex
        AND         $T1, $BitCount, #7
        CMPEQ       $T1, $BitIndex        
        MEND

        
        ;// Bitstream Decode finalise
        ;//
        ;// Writes back the bitstream state to the bitstream pointers
        ;//
        ;// Input Registers:
        ;//
        ;// $pBitStream     \ 
        ;// $BitBuffer       } See description above.
        ;// $BitCount       / 
        ;//
        ;// Output Registers:
        ;//
        ;// $ppBitStream    - pointer to pointer to the next bitstream byte
        ;// $pBitOffset     - pointer to the number of bits used in the current byte (0..7)
        ;// $pBitStream     \ 
        ;// $BitBuffer       } these register are corrupted
        ;// $BitCount       / 
        ;//
        MACRO
        M_BD_FINI  $ppBitStream, $pBitOffset
        
        ;// Advance pointer by the number of free bits in the buffer
        ADD     $pBitStream, $pBitStream, $BitCount, LSR#3
        AND     $BitCount, $BitCount, #7
        
        ;// Now move back 32 bits to reach the first usued bit
        SUB     $pBitStream, $pBitStream, #4
        
        ;// Store out bitstream state
        STR     $BitCount, [$pBitOffset]
        STR     $pBitStream, [$ppBitStream]
        MEND
        
        END