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