]> git.sesse.net Git - vlc/blob - modules/stream_out/transrate/block.c
Make Zorglub less unhappy
[vlc] / modules / stream_out / transrate / block.c
1 /*****************************************************************************
2  * block.c: MPEG2 video transrating module
3  *****************************************************************************
4  * Copyright (C) 2003-2004 the VideoLAN team
5  * Copyright (C) 2003 Antoine Missout
6  * Copyright (C) 2000-2003 Michel Lespinasse <walken@zoy.org>
7  * Copyright (C) 1999-2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
8  * $Id$
9  *
10  * Authors: Christophe Massiot <massiot@via.ecp.fr>
11  *          Laurent Aimar <fenrir@via.ecp.fr>
12  *          Antoine Missout
13  *          Michel Lespinasse <walken@zoy.org>
14  *          Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
15  *
16  * This program is free software; you can redistribute it and/or modify
17  * it under the terms of the GNU General Public License as published by
18  * the Free Software Foundation; either version 2 of the License, or
19  * (at your option) any later version.
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  * GNU General Public License for more details.
25  *
26  * You should have received a copy of the GNU General Public License
27  * along with this program; if not, write to the Free Software
28  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
29  *****************************************************************************/
30
31 /*****************************************************************************
32  * Preamble
33  *****************************************************************************/
34 #include <stdio.h>
35 #include <stdlib.h>
36 #define NDEBUG 1
37 #include <assert.h>
38 #include <math.h>
39
40 #include <vlc/vlc.h>
41 #include <vlc/sout.h>
42 #include <vlc/input.h>
43
44 #include "transrate.h"
45
46 /****************************************************************************
47  * transrater, code from M2VRequantizer http://www.metakine.com/
48  ****************************************************************************/
49
50 /////---- begin ext mpeg code
51
52 #include "getvlc.h"
53 #include "putvlc.h"
54
55 static inline int saturate( int i_value )
56 {
57     if ( i_value > 2047 )
58         return 2047;
59     if ( i_value < -2048 )
60         return -2048;
61     return i_value;
62 }
63
64 static int64_t get_score( const RunLevel *blk, RunLevel *new_blk, int i_qscale, int i_qscale_new )
65 {
66     int64_t score = 0;
67     int i1 = -1, i2 = -1;
68
69     while ( new_blk->level )
70     {
71         int new_level = new_blk->level;
72         int level = blk->level;
73         if ( i1 > 64 || i2 > 64 || !blk->run || !new_blk->run ) return score;
74         if ( i1 + blk->run == i2 + new_blk->run )
75         {
76             int64_t tmp = saturate(level * i_qscale) 
77                             - saturate(new_level * i_qscale_new);
78             i1 += blk->run;
79             i2 += new_blk->run;
80             score += tmp * tmp;
81             blk++;
82             new_blk++;
83         }
84         else
85         {
86             int64_t tmp = saturate(level * i_qscale);
87             i1 += blk->run;
88             score += tmp * tmp;
89             blk++;
90         }
91     }
92
93     while ( blk->level )
94     {
95         int level = blk->level;
96         int64_t tmp = saturate(level * i_qscale);
97         i1 += blk->run;
98         score += tmp * tmp;
99         blk++;
100     }
101
102     return score;
103 }
104
105 static void change_qscale( const RunLevel *blk, RunLevel *new_blk, int i_qscale, int i_qscale_new, int intra )
106 {
107     int i = 0, li = 0;
108     int rounding;
109     if ( intra )
110         rounding = i_qscale_new / 3;
111     else
112         rounding = i_qscale_new / 6;
113
114     while ( blk->level )
115     {
116         int level = blk->level > 0 ? blk->level : -blk->level;
117         int new_level = saturate(level * i_qscale) / i_qscale_new;
118         i += blk->run;
119
120         if ( new_level )
121         {
122             new_blk->run = i - li;
123             new_blk->level = blk->level > 0 ? new_level : -new_level;
124             new_blk++;
125             li = i;
126         }
127         blk++;
128     }
129     new_blk->level = 0;
130 }
131
132 static const uint8_t non_linear_mquant_table[32] =
133 {
134     0, 1, 2, 3, 4, 5, 6, 7,
135     8,10,12,14,16,18,20,22,
136     24,28,32,36,40,44,48,52,
137     56,64,72,80,88,96,104,112
138 };
139 static const uint8_t map_non_linear_mquant[113] =
140 {
141     0,1,2,3,4,5,6,7,8,8,9,9,10,10,11,11,12,12,13,13,14,14,15,15,16,16,
142     16,17,17,17,18,18,18,18,19,19,19,19,20,20,20,20,21,21,21,21,22,22,
143     22,22,23,23,23,23,24,24,24,24,24,24,24,25,25,25,25,25,25,25,26,26,
144     26,26,26,26,26,26,27,27,27,27,27,27,27,27,28,28,28,28,28,28,28,29,
145     29,29,29,29,29,29,29,29,29,30,30,30,30,30,30,30,31,31,31,31,31
146 };
147
148 int scale_quant( transrate_t *tr, double qrate )
149 {
150     int i_quant = (int)floor( tr->quantizer_scale * qrate + 0.5 );
151
152     if ( tr->q_scale_type )
153     {
154         if ( i_quant < 1 )
155             i_quant = 1;
156         if ( i_quant > 112 )
157             i_quant = 112;
158         i_quant = non_linear_mquant_table[map_non_linear_mquant[i_quant]];
159     }
160     else
161     {
162         if ( i_quant < 2 )
163             i_quant = 2;
164         if ( i_quant > 62 )
165             i_quant = 62;
166         i_quant = (i_quant / 2) * 2; // Must be *even*
167     }
168
169     return i_quant;
170 }
171
172 int increment_quant( transrate_t *tr, int i_quant )
173 {
174     if ( tr->q_scale_type )
175     {
176         assert( i_quant >= 1 && i_quant <= 112 );
177         i_quant = map_non_linear_mquant[i_quant] + 1;
178         if ( i_quant > 31 )
179             i_quant = 31;
180         i_quant = non_linear_mquant_table[i_quant];
181     }
182     else
183     {
184         assert(!(i_quant & 1));
185         i_quant += 2;
186         if ( i_quant > 62 )
187             i_quant = 62;
188     }
189     return i_quant;
190 }
191
192
193 static int decrement_quant( transrate_t *tr, int i_quant )
194 {
195     if ( tr->q_scale_type )
196     {
197         assert( i_quant >= 1 && i_quant <= 112 );
198         i_quant = map_non_linear_mquant[i_quant] - 1;
199         if ( i_quant < 1 )
200             i_quant = 1;
201         i_quant = non_linear_mquant_table[i_quant];
202     }
203     else
204     {
205         assert(!(i_quant & 1));
206         i_quant -= 2;
207         if ( i_quant < 2 )
208             i_quant = 2;
209     }
210     return i_quant;
211 }
212
213 static void quantize_block( transrate_t *tr, RunLevel *new_blk, int intra )
214 {
215     RunLevel old_blk[65];
216     RunLevel *blk = old_blk;
217     const uint8_t *old_matrix, *new_matrix;
218     int i = 0, li = 0;
219
220     memcpy( blk, new_blk, 65 * sizeof(RunLevel) );
221     if ( intra )
222     {
223         old_matrix = tr->intra_quantizer_matrix;
224         new_matrix = mpeg4_default_intra_matrix;
225     }
226     else
227     {
228         old_matrix = tr->non_intra_quantizer_matrix;
229         new_matrix = mpeg4_default_non_intra_matrix;
230     }
231
232     while ( blk->level )
233     {
234         int level = blk->level > 0 ? blk->level : -blk->level;
235         int new_level = (level * old_matrix[i] + new_matrix[i]/2)
236                             / new_matrix[i];
237         i += blk->run;
238
239         if (new_level)
240         {
241             new_blk->run = i - li;
242             new_blk->level = blk->level > 0 ? new_level : -new_level;
243             new_blk++;
244             li = i;
245         }
246         blk++;
247     }
248     new_blk->level = 0;
249 }
250
251 int transrate_mb( transrate_t *tr, RunLevel blk[6][65], RunLevel new_blk[6][65],
252                   int i_cbp, int intra )
253 {
254     int i_qscale = tr->quantizer_scale;
255     int i_guessed_qscale = tr->new_quantizer_scale;
256     int64_t i_last_error = 0;
257     int i_last_qscale;
258     int i_last_qscale_same_error = 0;
259     int i_direction = 0;
260     int i_new_cbp;
261     int i_nb_blocks = 0;
262     int i_nb_coeffs = 0;
263     int i;
264
265     for ( i = 0; i < 6; i++ )
266     {
267         if ( i_cbp & (1 << (5 - i)) )
268         {
269             RunLevel *cur_blk = blk[i];
270             i_nb_blocks++;
271             while ( cur_blk->level )
272             {
273                 cur_blk++;
274                 i_nb_coeffs++;
275             }
276         }
277     }
278
279     /* See if we can change quantizer scale */
280     for ( ; ; )
281     {
282         int64_t i_error = 0;
283         i_new_cbp = 0;
284
285         for ( i = 0; i < 6; i++ )
286         {
287             if ( i_cbp & (1 << (5 - i)) )
288             {
289                 int64_t i_block_error;
290                 change_qscale( blk[i], new_blk[i], i_qscale, i_guessed_qscale,
291                                intra );
292                 i_block_error = get_score( blk[i], new_blk[i],
293                                            i_qscale, i_guessed_qscale );
294                 if ( i > 3 ) i_block_error *= 4;
295                 if ( i_block_error > i_error )
296                     i_error = i_block_error;
297                 if ( new_blk[i]->level )
298                     i_new_cbp |= (1 << (5 - i));
299             }
300         }
301
302         if ( i_error >= (int64_t)tr->i_minimum_error
303                 && i_error <= (int64_t)tr->i_admissible_error )
304         {
305             break;
306         }
307         if ( i_nb_coeffs <= 15 && i_error <= (int64_t)tr->i_admissible_error )
308         {
309             /* There is no interest in changing the qscale (takes up 5 bits
310              * we won't regain) */
311             break;
312         }
313
314         if ( !i_direction )
315         {
316             if ( i_error > (int64_t)tr->i_admissible_error )
317             {
318                 i_direction = -1;
319                 i_last_qscale = i_guessed_qscale;
320                 i_guessed_qscale = decrement_quant( tr, i_guessed_qscale );
321             }
322             else
323             {
324                 i_direction = +1;
325                 i_last_qscale = i_guessed_qscale;
326                 i_guessed_qscale = increment_quant( tr, i_guessed_qscale );
327                 i_last_error = i_error;
328                 i_last_qscale_same_error = i_last_qscale;
329             }
330             if ( i_guessed_qscale == i_last_qscale )
331                 break;
332         }
333         else if ( i_direction < 0 )
334         {
335             if ( i_error > (int64_t)tr->i_admissible_error )
336             {
337                 i_last_qscale = i_guessed_qscale;
338                 i_guessed_qscale = decrement_quant( tr, i_guessed_qscale );
339                 if ( i_guessed_qscale == i_last_qscale )
340                     break;
341             }
342             else
343             {
344                 break;
345             }
346         }
347         else
348         {
349             if ( i_error < (int64_t)tr->i_minimum_error )
350             {
351                 i_last_qscale = i_guessed_qscale;
352                 i_guessed_qscale = increment_quant( tr, i_guessed_qscale );
353                 if ( i_error > i_last_error )
354                 {
355                     i_last_error = i_error;
356                     i_last_qscale_same_error = i_last_qscale;
357                 }
358                 if ( i_guessed_qscale == i_last_qscale )
359                 {
360                     if ( i_last_error == i_error )
361                     {
362                         i_guessed_qscale = i_last_qscale_same_error;
363                         if ( i_guessed_qscale == i_qscale )
364                         {
365                             memcpy( new_blk, blk, sizeof(RunLevel)*65*6 );
366                             i_new_cbp = i_cbp;
367                         }
368                         else
369                         {
370                             i_new_cbp = 0;
371                             for ( i = 0; i < 6; i++ )
372                             {
373                                 if ( i_cbp & (1 << (5 - i)) )
374                                 {
375                                     change_qscale( blk[i], new_blk[i],
376                                                    i_qscale, i_guessed_qscale,
377                                                    intra );
378                                     if ( new_blk[i]->level )
379                                         i_new_cbp |= (1 << (5 - i));
380                                 }
381                             }
382                         }
383                     }
384                     break;
385                 }
386             }
387             else
388             {
389                 if ( i_error > (int64_t)tr->i_admissible_error
390                         || i_last_error == i_error )
391                 {
392                     i_guessed_qscale = i_last_qscale_same_error;
393                     if ( i_guessed_qscale == i_qscale )
394                     {
395                         memcpy( new_blk, blk, sizeof(RunLevel)*65*6 );
396                         i_new_cbp = i_cbp;
397                     }
398                     else
399                     {
400                         i_new_cbp = 0;
401                         for ( i = 0; i < 6; i++ )
402                         {
403                             if ( i_cbp & (1 << (5 - i)) )
404                             {
405                                 change_qscale( blk[i], new_blk[i],
406                                                i_qscale, i_guessed_qscale,
407                                                intra );
408                                 if ( new_blk[i]->level )
409                                     i_new_cbp |= (1 << (5 - i));
410                             }
411                         }
412                     }
413                 }
414                 break;
415             }
416         }
417     }
418
419     tr->new_quantizer_scale = i_guessed_qscale;
420
421 #if 0
422     /* Now see if we can drop coeffs */
423     for ( i = 0; i < 6; i++ )
424     {
425         if ( i_new_cbp & (1 << (5 - i)) )
426         {
427             for ( ; ; )
428             {
429                 RunLevel *last_blk = new_blk[i];
430                 uint8_t old_level;
431
432                 while ( last_blk[1].level )
433                     last_blk++;
434                 if ( last_blk == new_blk[i] )
435                     break;
436                 old_level = last_blk->level;
437                 last_blk->level = 0;
438                 i_error = get_score( blk[i], new_blk[i],
439                                      i_qscale, i_guessed_qscale );
440                 if ( i_error > tr->i_admissible_error )
441                 {
442                     last_blk->level = old_level;
443                     break;
444                 }
445             }
446         }
447     }
448 #endif
449
450     return i_new_cbp;
451 }
452
453 void get_intra_block_B14( transrate_t *tr, RunLevel *blk )
454 {
455     bs_transrate_t *bs = &tr->bs;
456     int i, li;
457     int val;
458     const DCTtab * tab;
459
460     li = i = 0;
461
462     for( ;; )
463     {
464         if (bs->i_bit_in_cache >= 0x28000000)
465         {
466             tab = DCT_B14AC_5 + (UBITS (bs->i_bit_in_cache, 5) - 5);
467
468             i += tab->run;
469             if (i >= 64) break; /* end of block */
470
471     normal_code:
472             bs_flush( bs, tab->len );
473             val = tab->level;
474             val = (val ^ SBITS (bs->i_bit_in_cache, 1)) - SBITS (bs->i_bit_in_cache, 1);
475             blk->level = val;
476             blk->run = i - li - 1;
477             li = i;
478             blk++;
479
480             bs_flush( bs, 1 );
481             continue;
482         }
483         else if (bs->i_bit_in_cache >= 0x04000000)
484         {
485             tab = DCT_B14_8 + (UBITS (bs->i_bit_in_cache, 8) - 4);
486
487             i += tab->run;
488             if (i < 64) goto normal_code;
489
490             /* escape code */
491             i += (UBITS (bs->i_bit_in_cache, 12) & 0x3F) - 64;
492             if (i >= 64) break; /* illegal, check needed to avoid buffer overflow */
493
494             bs_flush( bs, 12 );
495             val = SBITS (bs->i_bit_in_cache, 12);
496             blk->level = val;
497             blk->run = i - li - 1;
498             li = i;
499             blk++;
500
501             bs_flush( bs, 12 );
502
503             continue;
504         }
505         else if (bs->i_bit_in_cache >= 0x02000000)
506         {
507             tab = DCT_B14_10 + (UBITS (bs->i_bit_in_cache, 10) - 8);
508             i += tab->run;
509             if (i < 64 ) goto normal_code;
510         }
511         else if (bs->i_bit_in_cache >= 0x00800000)
512         {
513             tab = DCT_13 + (UBITS (bs->i_bit_in_cache, 13) - 16);
514             i += tab->run;
515             if (i < 64 ) goto normal_code;
516         }
517         else if (bs->i_bit_in_cache >= 0x00200000)
518         {
519             tab = DCT_15 + (UBITS (bs->i_bit_in_cache, 15) - 16);
520             i += tab->run;
521             if (i < 64 ) goto normal_code;
522         }
523         else
524         {
525             tab = DCT_16 + UBITS (bs->i_bit_in_cache, 16);
526             bs_flush( bs, 16 );
527             i += tab->run;
528             if (i < 64 ) goto normal_code;
529         }
530         fprintf(stderr, "Err in B14\n");
531         tr->b_error = 1;
532         break;  /* illegal, check needed to avoid buffer overflow */
533     }
534     bs_flush( bs, 2 );    /* dump end of block code */
535     blk->level = 0;
536
537     if ( tr->mpeg4_matrix )
538         quantize_block( tr, blk, 1 );
539 }
540
541 void get_intra_block_B15( transrate_t *tr, RunLevel *blk )
542 {
543     bs_transrate_t *bs = &tr->bs;
544     int i, li;
545     int val;
546     const DCTtab * tab;
547
548     li = i = 0;
549
550     for( ;; )
551     {
552         if (bs->i_bit_in_cache >= 0x04000000)
553         {
554             tab = DCT_B15_8 + (UBITS (bs->i_bit_in_cache, 8) - 4);
555
556             i += tab->run;
557             if (i < 64)
558             {
559     normal_code:
560                 bs_flush( bs, tab->len );
561
562                 val = tab->level;
563                 val = (val ^ SBITS (bs->i_bit_in_cache, 1)) - SBITS (bs->i_bit_in_cache, 1);
564                 blk->level = val;
565                 blk->run = i - li - 1;
566                 li = i;
567                 blk++;
568
569                 bs_flush( bs, 1 );
570                 continue;
571             }
572             else
573             {
574                 i += (UBITS (bs->i_bit_in_cache, 12) & 0x3F) - 64;
575
576                 if (i >= 64) break; /* illegal, check against buffer overflow */
577
578                 bs_flush( bs, 12 );
579                 val = SBITS (bs->i_bit_in_cache, 12);
580                 blk->level = val;
581                 blk->run = i - li - 1;
582                 li = i;
583                 blk++;
584
585                 bs_flush( bs, 12 );
586                 continue;
587             }
588         }
589         else if (bs->i_bit_in_cache >= 0x02000000)
590         {
591             tab = DCT_B15_10 + (UBITS (bs->i_bit_in_cache, 10) - 8);
592             i += tab->run;
593             if (i < 64) goto normal_code;
594         }
595         else if (bs->i_bit_in_cache >= 0x00800000)
596         {
597             tab = DCT_13 + (UBITS (bs->i_bit_in_cache, 13) - 16);
598             i += tab->run;
599             if (i < 64) goto normal_code;
600         }
601         else if (bs->i_bit_in_cache >= 0x00200000)
602         {
603             tab = DCT_15 + (UBITS (bs->i_bit_in_cache, 15) - 16);
604             i += tab->run;
605             if (i < 64) goto normal_code;
606         }
607         else
608         {
609             tab = DCT_16 + UBITS (bs->i_bit_in_cache, 16);
610             bs_flush( bs, 16 );
611             i += tab->run;
612             if (i < 64) goto normal_code;
613         }
614         fprintf(stderr, "Err in B15\n");
615         tr->b_error = 1;
616         break;  /* illegal, check needed to avoid buffer overflow */
617     }
618     bs_flush( bs, 4 );    /* dump end of block code */
619     blk->level = 0;
620
621     if ( tr->mpeg4_matrix )
622         quantize_block( tr, blk, 1 );
623 }
624
625
626 int get_non_intra_block( transrate_t *tr, RunLevel *blk )
627 {
628     bs_transrate_t *bs = &tr->bs;
629     int i, li;
630     int val;
631     const DCTtab * tab;
632
633     li = i = -1;
634
635     if (bs->i_bit_in_cache >= 0x28000000)
636     {
637         tab = DCT_B14DC_5 + (UBITS (bs->i_bit_in_cache, 5) - 5);
638         goto entry_1;
639     }
640     else goto entry_2;
641
642     for( ;; )
643     {
644         if (bs->i_bit_in_cache >= 0x28000000)
645         {
646             tab = DCT_B14AC_5 + (UBITS (bs->i_bit_in_cache, 5) - 5);
647
648     entry_1:
649             i += tab->run;
650             if (i >= 64)
651             break;  /* end of block */
652
653     normal_code:
654
655             bs_flush( bs, tab->len );
656             val = tab->level;
657             val = (val ^ SBITS (bs->i_bit_in_cache, 1)) - SBITS (bs->i_bit_in_cache, 1);
658             blk->level = val;
659             blk->run = i - li - 1;
660             li = i;
661             blk++;
662
663             //if ( ((val) && (tab->level < tst)) || ((!val) && (tab->level >= tst)) )
664             //  LOGF("level: %i val: %i tst : %i q: %i nq : %i\n", tab->level, val, tst, q, nq);
665
666             bs_flush( bs, 1 );
667             continue;
668         }
669
670     entry_2:
671         if (bs->i_bit_in_cache >= 0x04000000)
672         {
673             tab = DCT_B14_8 + (UBITS (bs->i_bit_in_cache, 8) - 4);
674
675             i += tab->run;
676             if (i < 64) goto normal_code;
677
678             /* escape code */
679
680             i += (UBITS (bs->i_bit_in_cache, 12) & 0x3F) - 64;
681
682             if (i >= 64) break; /* illegal, check needed to avoid buffer overflow */
683
684             bs_flush( bs, 12 );
685             val = SBITS (bs->i_bit_in_cache, 12);
686             blk->level = val;
687             blk->run = i - li - 1;
688             li = i;
689             blk++;
690
691             bs_flush( bs, 12 );
692             continue;
693         }
694         else if (bs->i_bit_in_cache >= 0x02000000)
695         {
696             tab = DCT_B14_10 + (UBITS (bs->i_bit_in_cache, 10) - 8);
697             i += tab->run;
698             if (i < 64) goto normal_code;
699         }
700         else if (bs->i_bit_in_cache >= 0x00800000)
701         {
702             tab = DCT_13 + (UBITS (bs->i_bit_in_cache, 13) - 16);
703             i += tab->run;
704             if (i < 64) goto normal_code;
705         }
706         else if (bs->i_bit_in_cache >= 0x00200000)
707         {
708             tab = DCT_15 + (UBITS (bs->i_bit_in_cache, 15) - 16);
709             i += tab->run;
710             if (i < 64) goto normal_code;
711         }
712         else
713         {
714             tab = DCT_16 + UBITS (bs->i_bit_in_cache, 16);
715             bs_flush( bs, 16 );
716
717             i += tab->run;
718             if (i < 64) goto normal_code;
719         }
720         fprintf(stderr, "Err in non-intra\n");
721         tr->b_error = 1;
722         break;  /* illegal, check needed to avoid buffer overflow */
723     }
724     bs_flush( bs, 2 );    /* dump end of block code */
725     blk->level = 0;
726
727     if ( tr->mpeg4_matrix )
728         quantize_block( tr, blk, 0 );
729
730     return i;
731 }
732
733 static inline void putAC( bs_transrate_t *bs, int run, int signed_level, int vlcformat)
734 {
735     int level, len;
736     const VLCtable *ptab = NULL;
737
738     level = (signed_level<0) ? -signed_level : signed_level; /* abs(signed_level) */
739
740     assert(!(run<0 || run>63 || level==0 || level>2047));
741
742     len = 0;
743
744     if (run<2 && level<41)
745     {
746         if (vlcformat)  ptab = &dct_code_tab1a[run][level-1];
747         else ptab = &dct_code_tab1[run][level-1];
748         len = ptab->len;
749     }
750     else if (run<32 && level<6)
751     {
752         if (vlcformat) ptab = &dct_code_tab2a[run-2][level-1];
753         else ptab = &dct_code_tab2[run-2][level-1];
754         len = ptab->len;
755     }
756
757     if (len) /* a VLC code exists */
758     {
759         bs_write( bs, ptab->code, len);
760         bs_write( bs, signed_level<0, 1); /* sign */
761     }
762     else
763     {
764         bs_write( bs, 1l, 6); /* Escape */
765         bs_write( bs, run, 6); /* 6 bit code for run */
766         bs_write( bs, ((unsigned int)signed_level) & 0xFFF, 12);
767     }
768 }
769
770
771 static inline void putACfirst( bs_transrate_t *bs, int run, int val)
772 {
773     if (run==0 && (val==1 || val==-1)) bs_write( bs, 2|(val<0), 2 );
774     else putAC( bs, run, val, 0);
775 }
776
777 void putnonintrablk( bs_transrate_t *bs, RunLevel *blk)
778 {
779     assert(blk->level);
780
781     putACfirst( bs, blk->run, blk->level );
782     blk++;
783
784     while (blk->level)
785     {
786         putAC( bs, blk->run, blk->level, 0 );
787         blk++;
788     }
789
790     bs_write( bs, 2, 2 );
791 }
792
793 void putintrablk( bs_transrate_t *bs, RunLevel *blk, int vlcformat)
794 {
795     while (blk->level)
796     {
797         putAC( bs, blk->run, blk->level, vlcformat );
798         blk++;
799     }
800
801     if (vlcformat)
802         bs_write( bs, 6, 4 );
803     else
804         bs_write( bs, 2, 2 );
805 }
806