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