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