]> git.sesse.net Git - vlc/blob - modules/stream_out/transrate/frame.c
* modules/stream_out/transrate/frame.c: Fixed warnings (thanks fenrir)
[vlc] / modules / stream_out / transrate / frame.c
1 /*****************************************************************************
2  * frame.c: MPEG2 video transrating module
3  *****************************************************************************
4  * Copyright (C) 2003 VideoLAN
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: frame.c,v 1.2 2004/03/03 11:39:06 massiot Exp $
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 /* This is awful magic --Meuuh */
51 //#define REACT_DELAY (1024.0*128.0)
52 #define REACT_DELAY (256.0)
53
54 #define QUANT_I (1.7)
55
56 #define QUANT_P (1.4)
57
58 #define QUANT_P_INC (0.1)
59
60 #define B_HANDICAP 5
61
62 // notes:
63 //
64 // - intra block:
65 //      - the quantiser is increment by one step
66 //
67 // - non intra block:
68 //      - in P_FRAME we keep the original quantiser but drop the last coefficient
69 //        if there is more than one
70 //      - in B_FRAME we multiply the quantiser by a factor
71 //
72 // - I_FRAME is recoded when we're 5.0 * REACT_DELAY late
73 // - P_FRAME is recoded when we're 2.5 * REACT_DELAY late
74 // - B_FRAME are always recoded
75
76 // if we're getting *very* late (60 * REACT_DELAY)
77 //
78 // - intra blocks quantiser is incremented two step
79 // - drop a few coefficients but always keep the first one
80
81 // useful constants
82 enum
83 {
84     I_TYPE = 1,
85     P_TYPE = 2,
86     B_TYPE = 3
87 };
88
89
90 // gcc
91 #ifdef HAVE_BUILTIN_EXPECT
92 #define likely(x) __builtin_expect ((x) != 0, 1)
93 #define unlikely(x) __builtin_expect ((x) != 0, 0)
94 #else
95 #define likely(x) (x)
96 #define unlikely(x) (x)
97 #endif
98
99 #define BITS_IN_BUF (8)
100
101 #define LOG(msg) fprintf (stderr, msg)
102 #define LOGF(format, args...) fprintf (stderr, format, args)
103
104 static inline void bs_write( bs_transrate_t *s, unsigned int val, int n)
105 {
106     assert(n < 32);
107     assert(!(val & (0xffffffffU << n)));
108
109     while (unlikely(n >= s->i_bit_out))
110     {
111         s->p_w[0] = (s->i_bit_out_cache << s->i_bit_out ) | (val >> (n - s->i_bit_out));
112         s->p_w++;
113         n -= s->i_bit_out;
114         s->i_bit_out_cache = 0;
115         val &= ~(0xffffffffU << n);
116         s->i_bit_out = BITS_IN_BUF;
117     }
118
119     if (likely(n))
120     {
121         s->i_bit_out_cache = (s->i_bit_out_cache << n) | val;
122         s->i_bit_out -= n;
123     }
124
125     assert(s->i_bit_out > 0);
126     assert(s->i_bit_out <= BITS_IN_BUF);
127 }
128
129 static inline void bs_refill( bs_transrate_t *s )
130 {
131     assert((s->p_r - s->p_c) >= 1);
132     s->i_bit_in_cache |= s->p_c[0] << (24 - s->i_bit_in);
133     s->i_bit_in += 8;
134     s->p_c++;
135 }
136
137 static inline void bs_flush( bs_transrate_t *s, unsigned int n )
138 {
139     assert(s->i_bit_in >= n);
140
141     s->i_bit_in_cache <<= n;
142     s->i_bit_in -= n;
143
144     assert( (!n) || ((n>0) && !(s->i_bit_in_cache & 0x1)) );
145
146     while (unlikely(s->i_bit_in < 24)) bs_refill( s );
147 }
148
149 static inline unsigned int bs_read( bs_transrate_t *s, unsigned int n)
150 {
151     unsigned int Val = ((unsigned int)s->i_bit_in_cache) >> (32 - n);
152     bs_flush( s, n );
153     return Val;
154 }
155
156 static inline unsigned int bs_copy( bs_transrate_t *s, unsigned int n)
157 {
158     unsigned int Val = bs_read( s, n);
159     bs_write(s, Val, n);
160     return Val;
161 }
162
163 static inline void bs_flush_read( bs_transrate_t *s )
164 {
165     int i = s->i_bit_in & 0x7;
166     if( i )
167     {
168         assert(((unsigned int)bs->i_bit_in_cache) >> (32 - i) == 0);
169         s->i_bit_in_cache <<= i;
170         s->i_bit_in -= i;
171     }
172     s->p_c += -1 * (s->i_bit_in >> 3);
173     s->i_bit_in = 0;
174 }
175 static inline void bs_flush_write( bs_transrate_t *s )
176 {
177     if( s->i_bit_out != 8 ) bs_write(s, 0, s->i_bit_out);
178 }
179
180 /////---- begin ext mpeg code
181
182 const uint8_t non_linear_mquant_table[32] =
183 {
184     0, 1, 2, 3, 4, 5, 6, 7,
185     8,10,12,14,16,18,20,22,
186     24,28,32,36,40,44,48,52,
187     56,64,72,80,88,96,104,112
188 };
189 const uint8_t map_non_linear_mquant[113] =
190 {
191     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,
192     16,17,17,17,18,18,18,18,19,19,19,19,20,20,20,20,21,21,21,21,22,22,
193     22,22,23,23,23,23,24,24,24,24,24,24,24,25,25,25,25,25,25,25,26,26,
194     26,26,26,26,26,26,27,27,27,27,27,27,27,27,28,28,28,28,28,28,28,29,
195     29,29,29,29,29,29,29,29,29,30,30,30,30,30,30,30,31,31,31,31,31
196 };
197
198 static int scale_quant( unsigned int q_scale_type, double quant )
199 {
200     int iquant;
201     if (q_scale_type)
202     {
203         iquant = (int) floor(quant+0.5);
204
205         /* clip mquant to legal (linear) range */
206         if (iquant<1) iquant = 1;
207         if (iquant>112) iquant = 112;
208
209         iquant = non_linear_mquant_table[map_non_linear_mquant[iquant]];
210     }
211     else
212     {
213         /* clip mquant to legal (linear) range */
214         iquant = (int)floor(quant+0.5);
215         if (iquant<2) iquant = 2;
216         if (iquant>62) iquant = 62;
217         iquant = (iquant/2)*2; // Must be *even*
218     }
219     return iquant;
220 }
221
222 static int increment_quant( transrate_t *tr, int quant )
223 {
224     if( tr->q_scale_type )
225     {
226         assert(quant >= 1 && quant <= 112 );
227         quant = map_non_linear_mquant[quant] + 1;
228         if( tr->picture_coding_type == P_TYPE )
229             quant += tr->level_p;
230         if( quant > 31) quant = 31;
231         quant = non_linear_mquant_table[quant];
232     }
233     else
234     {
235         assert(!(quant & 1));
236         quant += 2;
237         if( tr->picture_coding_type == P_TYPE )
238             quant += 2 * tr->level_p;
239         if (quant > 62) quant = 62;
240     }
241     return quant;
242 }
243
244 static inline int intmax( register int x, register int y )
245 {
246     return x < y ? y : x;
247 }
248 static inline int intmin( register int x, register int y )
249 {
250     return x < y ? x : y;
251 }
252
253 static int getNewQuant( transrate_t *tr, int curQuant)
254 {
255     bs_transrate_t *bs = &tr->bs;
256
257     double calc_quant, quant_to_use;
258     int mquant = 0;
259
260     switch ( tr->picture_coding_type )
261     {
262         case I_TYPE:
263         case P_TYPE:
264             mquant = increment_quant( tr, curQuant );
265             break;
266
267         case B_TYPE:
268             tr->quant_corr = (((bs->i_byte_in - (bs->p_r - 4 - bs->p_c)) / tr->fact_x) - (bs->i_byte_out + (bs->p_w - bs->p_ow))) / REACT_DELAY + B_HANDICAP;
269             calc_quant = curQuant * tr->current_fact_x;
270             quant_to_use = calc_quant - tr->quant_corr;
271
272             mquant = intmax(scale_quant( tr->q_scale_type, quant_to_use), increment_quant( tr, curQuant) );
273             break;
274
275         default:
276             assert(0);
277             break;
278     }
279
280     /*
281         LOGF("type: %s orig_quant: %3i calc_quant: %7.1f quant_corr: %7.1f using_quant: %3i\n",
282         (picture_coding_type == I_TYPE ? "I_TYPE" : (picture_coding_type == P_TYPE ? "P_TYPE" : "B_TYPE")),
283         (int)curQuant, (float)calc_quant, (float)quant_corr, (int)mquant);
284     */
285
286     assert(mquant >= curQuant);
287
288     return mquant;
289 }
290
291 static inline int isNotEmpty(RunLevel *blk)
292 {
293     return (blk->level);
294 }
295
296 #include "putvlc.h"
297
298 static void putAC( bs_transrate_t *bs, int run, int signed_level, int vlcformat)
299 {
300     int level, len;
301     const VLCtable *ptab = NULL;
302
303     level = (signed_level<0) ? -signed_level : signed_level; /* abs(signed_level) */
304
305     assert(!(run<0 || run>63 || level==0 || level>2047));
306
307     len = 0;
308
309     if (run<2 && level<41)
310     {
311         if (vlcformat)  ptab = &dct_code_tab1a[run][level-1];
312         else ptab = &dct_code_tab1[run][level-1];
313         len = ptab->len;
314     }
315     else if (run<32 && level<6)
316     {
317         if (vlcformat) ptab = &dct_code_tab2a[run-2][level-1];
318         else ptab = &dct_code_tab2[run-2][level-1];
319         len = ptab->len;
320     }
321
322     if (len) /* a VLC code exists */
323     {
324         bs_write( bs, ptab->code, len);
325         bs_write( bs, signed_level<0, 1); /* sign */
326     }
327     else
328     {
329         bs_write( bs, 1l, 6); /* Escape */
330         bs_write( bs, run, 6); /* 6 bit code for run */
331         bs_write( bs, ((unsigned int)signed_level) & 0xFFF, 12);
332     }
333 }
334
335
336 static inline void putACfirst( bs_transrate_t *bs, int run, int val)
337 {
338     if (run==0 && (val==1 || val==-1)) bs_write( bs, 2|(val<0),2);
339     else putAC( bs, run,val,0);
340 }
341
342 static void putnonintrablk( bs_transrate_t *bs, RunLevel *blk)
343 {
344     assert(blk->level);
345
346     putACfirst( bs, blk->run, blk->level);
347     blk++;
348
349     while(blk->level)
350     {
351         putAC( bs, blk->run, blk->level, 0);
352         blk++;
353     }
354
355     bs_write( bs, 2,2);
356 }
357
358 #include "getvlc.h"
359
360 static const int non_linear_quantizer_scale [] =
361 {
362      0,  1,  2,  3,  4,  5,   6,   7,
363      8, 10, 12, 14, 16, 18,  20,  22,
364     24, 28, 32, 36, 40, 44,  48,  52,
365     56, 64, 72, 80, 88, 96, 104, 112
366 };
367
368 static inline int get_macroblock_modes( transrate_t *tr )
369 {
370     bs_transrate_t *bs = &tr->bs;
371
372     int macroblock_modes;
373     const MBtab * tab;
374
375     switch( tr->picture_coding_type)
376     {
377         case I_TYPE:
378
379             tab = MB_I + UBITS (bs->i_bit_in_cache, 1);
380             bs_flush( bs, tab->len );
381             macroblock_modes = tab->modes;
382
383             if ((! ( tr->frame_pred_frame_dct)) && ( tr->picture_structure == FRAME_PICTURE))
384             {
385                 macroblock_modes |= UBITS (bs->i_bit_in_cache, 1) * DCT_TYPE_INTERLACED;
386                 bs_flush( bs, 1 );
387             }
388
389             return macroblock_modes;
390
391         case P_TYPE:
392
393             tab = MB_P + UBITS (bs->i_bit_in_cache, 5);
394             bs_flush( bs, tab->len );
395             macroblock_modes = tab->modes;
396
397             if (tr->picture_structure != FRAME_PICTURE)
398             {
399                 if (macroblock_modes & MACROBLOCK_MOTION_FORWARD)
400                 {
401                     macroblock_modes |= UBITS (bs->i_bit_in_cache, 2) * MOTION_TYPE_BASE;
402                     bs_flush( bs, 2 );
403                 }
404                 return macroblock_modes;
405             }
406             else if (tr->frame_pred_frame_dct)
407             {
408                 if (macroblock_modes & MACROBLOCK_MOTION_FORWARD)
409                     macroblock_modes |= MC_FRAME;
410                 return macroblock_modes;
411             }
412             else
413             {
414                 if (macroblock_modes & MACROBLOCK_MOTION_FORWARD)
415                 {
416                     macroblock_modes |= UBITS (bs->i_bit_in_cache, 2) * MOTION_TYPE_BASE;
417                     bs_flush( bs, 2 );
418                 }
419                 if (macroblock_modes & (MACROBLOCK_INTRA | MACROBLOCK_PATTERN))
420                 {
421                     macroblock_modes |= UBITS (bs->i_bit_in_cache, 1) * DCT_TYPE_INTERLACED;
422                     bs_flush( bs, 1 );
423                 }
424                 return macroblock_modes;
425             }
426
427         case B_TYPE:
428
429             tab = MB_B + UBITS (bs->i_bit_in_cache, 6);
430             bs_flush( bs, tab->len );
431             macroblock_modes = tab->modes;
432
433             if( tr->picture_structure != FRAME_PICTURE)
434             {
435                 if (! (macroblock_modes & MACROBLOCK_INTRA))
436                 {
437                     macroblock_modes |= UBITS (bs->i_bit_in_cache, 2) * MOTION_TYPE_BASE;
438                     bs_flush( bs, 2 );
439                 }
440                 return macroblock_modes;
441             }
442             else if (tr->frame_pred_frame_dct)
443             {
444                 /* if (! (macroblock_modes & MACROBLOCK_INTRA)) */
445                 macroblock_modes |= MC_FRAME;
446                 return macroblock_modes;
447             }
448             else
449             {
450                 if (macroblock_modes & MACROBLOCK_INTRA) goto intra;
451                 macroblock_modes |= UBITS (bs->i_bit_in_cache, 2) * MOTION_TYPE_BASE;
452                 bs_flush( bs, 2 );
453                 if (macroblock_modes & (MACROBLOCK_INTRA | MACROBLOCK_PATTERN))
454                 {
455                     intra:
456                     macroblock_modes |= UBITS (bs->i_bit_in_cache, 1) * DCT_TYPE_INTERLACED;
457                     bs_flush( bs, 1 );
458                 }
459                 return macroblock_modes;
460             }
461
462         default:
463             return 0;
464     }
465
466 }
467
468 static inline int get_quantizer_scale( transrate_t *tr )
469 {
470     bs_transrate_t *bs = &tr->bs;
471
472     int quantizer_scale_code;
473
474     quantizer_scale_code = UBITS (bs->i_bit_in_cache, 5);
475     bs_flush( bs, 5 );
476
477     if( tr->q_scale_type )
478         return non_linear_quantizer_scale[quantizer_scale_code];
479     else
480         return quantizer_scale_code << 1;
481 }
482
483 static inline int get_motion_delta( bs_transrate_t *bs, const int f_code )
484 {
485     int delta;
486     int sign;
487     const MVtab * tab;
488
489     if (bs->i_bit_in_cache & 0x80000000)
490     {
491         bs_copy( bs, 1 );
492         return 0;
493     }
494     else if (bs->i_bit_in_cache >= 0x0c000000)
495     {
496
497         tab = MV_4 + UBITS (bs->i_bit_in_cache, 4);
498         delta = (tab->delta << f_code) + 1;
499         bs_copy( bs, tab->len);
500
501         sign = SBITS (bs->i_bit_in_cache, 1);
502         bs_copy( bs, 1 );
503
504         if (f_code) delta += UBITS (bs->i_bit_in_cache, f_code);
505         bs_copy( bs, f_code);
506
507         return (delta ^ sign) - sign;
508     }
509     else
510     {
511
512         tab = MV_10 + UBITS (bs->i_bit_in_cache, 10);
513         delta = (tab->delta << f_code) + 1;
514         bs_copy( bs, tab->len);
515
516         sign = SBITS (bs->i_bit_in_cache, 1);
517         bs_copy( bs, 1);
518
519         if (f_code)
520         {
521             delta += UBITS (bs->i_bit_in_cache, f_code);
522             bs_copy( bs, f_code);
523         }
524
525         return (delta ^ sign) - sign;
526     }
527 }
528
529
530 static inline int get_dmv( bs_transrate_t *bs )
531 {
532     const DMVtab * tab;
533
534     tab = DMV_2 + UBITS (bs->i_bit_in_cache, 2);
535     bs_copy( bs, tab->len);
536     return tab->dmv;
537 }
538
539 static inline int get_coded_block_pattern( bs_transrate_t *bs )
540 {
541     const CBPtab * tab;
542
543     if (bs->i_bit_in_cache >= 0x20000000)
544     {
545         tab = CBP_7 + (UBITS (bs->i_bit_in_cache, 7) - 16);
546         bs_flush( bs, tab->len );
547         return tab->cbp;
548     }
549     else
550     {
551         tab = CBP_9 + UBITS (bs->i_bit_in_cache, 9);
552         bs_flush( bs, tab->len );
553         return tab->cbp;
554     }
555 }
556
557 static inline int get_luma_dc_dct_diff( bs_transrate_t *bs )
558 {
559     const DCtab * tab;
560     int size;
561     int dc_diff;
562
563     if (bs->i_bit_in_cache < 0xf8000000)
564     {
565         tab = DC_lum_5 + UBITS (bs->i_bit_in_cache, 5);
566         size = tab->size;
567         if (size)
568         {
569             bs_copy( bs, tab->len);
570             //dc_diff = UBITS (bs->i_bit_in_cache, size) - UBITS (SBITS (~bs->i_bit_in_cache, 1), size);
571             dc_diff = UBITS (bs->i_bit_in_cache, size);
572             if (!(dc_diff >> (size - 1))) dc_diff = (dc_diff + 1) - (1 << size);
573             bs_copy( bs, size);
574             return dc_diff;
575         }
576         else
577         {
578             bs_copy( bs, 3);
579             return 0;
580         }
581     }
582     else
583     {
584         tab = DC_long + (UBITS (bs->i_bit_in_cache, 9) - 0x1e0);
585         size = tab->size;
586         bs_copy( bs, tab->len);
587         //dc_diff = UBITS (bs->i_bit_in_cache, size) - UBITS (SBITS (~bs->i_bit_in_cache, 1), size);
588         dc_diff = UBITS (bs->i_bit_in_cache, size);
589         if (!(dc_diff >> (size - 1))) dc_diff = (dc_diff + 1) - (1 << size);
590         bs_copy( bs, size);
591         return dc_diff;
592     }
593 }
594
595 static inline int get_chroma_dc_dct_diff( bs_transrate_t *bs )
596 {
597     const DCtab * tab;
598     int size;
599     int dc_diff;
600
601     if (bs->i_bit_in_cache < 0xf8000000)
602     {
603         tab = DC_chrom_5 + UBITS (bs->i_bit_in_cache, 5);
604         size = tab->size;
605         if (size)
606         {
607             bs_copy( bs, tab->len);
608             //dc_diff = UBITS (bs->i_bit_in_cache, size) - UBITS (SBITS (~bs->i_bit_in_cache, 1), size);
609             dc_diff = UBITS (bs->i_bit_in_cache, size);
610             if (!(dc_diff >> (size - 1))) dc_diff = (dc_diff + 1) - (1 << size);
611             bs_copy( bs, size);
612             return dc_diff;
613         } else
614         {
615             bs_copy( bs, 2);
616             return 0;
617         }
618     }
619     else
620     {
621         tab = DC_long + (UBITS (bs->i_bit_in_cache, 10) - 0x3e0);
622         size = tab->size;
623         bs_copy( bs, tab->len + 1);
624         //dc_diff = UBITS (bs->i_bit_in_cache, size) - UBITS (SBITS (~bs->i_bit_in_cache, 1), size);
625         dc_diff = UBITS (bs->i_bit_in_cache, size);
626         if (!(dc_diff >> (size - 1))) dc_diff = (dc_diff + 1) - (1 << size);
627         bs_copy( bs, size);
628         return dc_diff;
629     }
630 }
631
632 static void get_intra_block_B14( bs_transrate_t *bs, const int i_qscale, const int i_qscale_new )
633 {
634     int tst;
635     int i, li;
636     int val;
637     const DCTtab * tab;
638
639     /* Basic sanity check --Meuuh */
640     if( i_qscale == 0 )
641     {
642         return;
643     }
644
645     tst = i_qscale_new/i_qscale + ((i_qscale_new%i_qscale) ? 1 : 0);
646
647     li = i = 0;
648
649     for( ;; )
650     {
651         if (bs->i_bit_in_cache >= 0x28000000)
652         {
653             tab = DCT_B14AC_5 + (UBITS (bs->i_bit_in_cache, 5) - 5);
654
655             i += tab->run;
656             if (i >= 64) break; /* end of block */
657
658     normal_code:
659             bs_flush( bs, tab->len );
660             val = tab->level;
661             if (val >= tst)
662             {
663                 val = (val ^ SBITS (bs->i_bit_in_cache, 1)) - SBITS (bs->i_bit_in_cache, 1);
664                 putAC( bs, i - li - 1, (val * i_qscale) / i_qscale_new, 0);
665                 li = i;
666             }
667
668             bs_flush( bs, 1 );
669             continue;
670         }
671         else 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             i += (UBITS (bs->i_bit_in_cache, 12) & 0x3F) - 64;
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             if (abs(val) >= tst)
685             {
686                 putAC( bs, i - li - 1, (val * i_qscale) / i_qscale_new, 0);
687                 li = i;
688             }
689
690             bs_flush( bs, 12 );
691
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             i += tab->run;
717             if (i < 64 ) goto normal_code;
718         }
719         break;  /* illegal, check needed to avoid buffer overflow */
720     }
721
722     bs_copy( bs, 2);    /* end of block code */
723 }
724
725 static void get_intra_block_B15( bs_transrate_t *bs,  const int i_qscale, int const i_qscale_new )
726 {
727     int tst;
728     int i, li;
729     int val;
730     const DCTtab * tab;
731
732     /* Basic sanity check --Meuuh */
733     if( i_qscale == 0 )
734     {
735         return;
736     }
737
738     tst = i_qscale_new/i_qscale + ((i_qscale_new%i_qscale) ? 1 : 0);
739
740     li = i = 0;
741
742     for( ;; )
743     {
744         if (bs->i_bit_in_cache >= 0x04000000)
745         {
746             tab = DCT_B15_8 + (UBITS (bs->i_bit_in_cache, 8) - 4);
747
748             i += tab->run;
749             if (i < 64)
750             {
751     normal_code:
752                 bs_flush( bs, tab->len );
753
754                 val = tab->level;
755                 if (val >= tst)
756                 {
757                     val = (val ^ SBITS (bs->i_bit_in_cache, 1)) - SBITS (bs->i_bit_in_cache, 1);
758                     putAC( bs, i - li - 1, (val * i_qscale) / i_qscale_new, 1);
759                     li = i;
760                 }
761
762                 bs_flush( bs, 1 );
763                 continue;
764             }
765             else
766             {
767                 i += (UBITS (bs->i_bit_in_cache, 12) & 0x3F) - 64;
768
769                 if (i >= 64) break; /* illegal, check against buffer overflow */
770
771                 bs_flush( bs, 12 );
772                 val = SBITS (bs->i_bit_in_cache, 12);
773                 if (abs(val) >= tst)
774                 {
775                     putAC( bs, i - li - 1, (val * i_qscale) / i_qscale_new, 1);
776                     li = i;
777                 }
778
779                 bs_flush( bs, 12 );
780                 continue;
781             }
782         }
783         else if (bs->i_bit_in_cache >= 0x02000000)
784         {
785             tab = DCT_B15_10 + (UBITS (bs->i_bit_in_cache, 10) - 8);
786             i += tab->run;
787             if (i < 64) goto normal_code;
788         }
789         else if (bs->i_bit_in_cache >= 0x00800000)
790         {
791             tab = DCT_13 + (UBITS (bs->i_bit_in_cache, 13) - 16);
792             i += tab->run;
793             if (i < 64) goto normal_code;
794         }
795         else if (bs->i_bit_in_cache >= 0x00200000)
796         {
797             tab = DCT_15 + (UBITS (bs->i_bit_in_cache, 15) - 16);
798             i += tab->run;
799             if (i < 64) goto normal_code;
800         }
801         else
802         {
803             tab = DCT_16 + UBITS (bs->i_bit_in_cache, 16);
804             bs_flush( bs, 16 );
805             i += tab->run;
806             if (i < 64) goto normal_code;
807         }
808         break;  /* illegal, check needed to avoid buffer overflow */
809     }
810
811     bs_copy( bs, 4);    /* end of block code */
812 }
813
814
815 static int get_non_intra_block_drop( transrate_t *tr, RunLevel *blk)
816 {
817     bs_transrate_t *bs = &tr->bs;
818
819     int i, li;
820     int val;
821     const DCTtab * tab;
822     RunLevel *sblk = blk + 1;
823
824     li = i = -1;
825
826     if (bs->i_bit_in_cache >= 0x28000000)
827     {
828         tab = DCT_B14DC_5 + (UBITS (bs->i_bit_in_cache, 5) - 5);
829         goto entry_1;
830     }
831     else goto entry_2;
832
833     for( ;; )
834     {
835         if (bs->i_bit_in_cache >= 0x28000000)
836         {
837             tab = DCT_B14AC_5 + (UBITS (bs->i_bit_in_cache, 5) - 5);
838
839     entry_1:
840             i += tab->run;
841             if (i >= 64) break; /* end of block */
842
843     normal_code:
844
845             bs_flush( bs, tab->len );
846             val = tab->level;
847             val = (val ^ SBITS (bs->i_bit_in_cache, 1)) - SBITS (bs->i_bit_in_cache, 1); /* if (bitstream_get (1)) val = -val; */
848
849             blk->level = val;
850             blk->run = i - li - 1;
851             li = i;
852             blk++;
853
854             bs_flush( bs, 1 );
855             continue;
856         }
857
858     entry_2:
859
860         if (bs->i_bit_in_cache >= 0x04000000)
861         {
862             tab = DCT_B14_8 + (UBITS (bs->i_bit_in_cache, 8) - 4);
863
864             i += tab->run;
865             if (i < 64) goto normal_code;
866
867             /* escape code */
868
869             i += (UBITS (bs->i_bit_in_cache, 12) & 0x3F) - 64;
870
871             if (i >= 64) break; /* illegal, check needed to avoid buffer overflow */
872
873             bs_flush( bs, 12 );
874             val = SBITS (bs->i_bit_in_cache, 12);
875
876             blk->level = val;
877             blk->run = i - li - 1;
878             li = i;
879             blk++;
880
881             bs_flush( bs, 12 );
882             continue;
883         }
884         else if (bs->i_bit_in_cache >= 0x02000000)
885         {
886             tab = DCT_B14_10 + (UBITS (bs->i_bit_in_cache, 10) - 8);
887             i += tab->run;
888             if (i < 64) goto normal_code;
889         }
890         else if (bs->i_bit_in_cache >= 0x00800000)
891         {
892             tab = DCT_13 + (UBITS (bs->i_bit_in_cache, 13) - 16);
893             i += tab->run;
894             if (i < 64) goto normal_code;
895         }
896         else if (bs->i_bit_in_cache >= 0x00200000)
897         {
898             tab = DCT_15 + (UBITS (bs->i_bit_in_cache, 15) - 16);
899             i += tab->run;
900             if (i < 64) goto normal_code;
901         }
902         else
903         {
904             tab = DCT_16 + UBITS (bs->i_bit_in_cache, 16);
905             bs_flush( bs, 16 );
906             i += tab->run;
907             if (i < 64) goto normal_code;
908         }
909         break;  /* illegal, check needed to avoid buffer overflow */
910     }
911     bs_flush( bs, 2 ); /* dump end of block code */
912
913     // remove last coeff
914     if (blk != sblk)
915     {
916         blk--;
917     }
918
919     // remove more coeffs if very late
920     if (tr->level_p >= 4 && (blk != sblk))
921     {
922         blk--;
923         if (tr->level_p >= 5 && (blk != sblk))
924         {
925             blk--;
926             if (tr->level_p >= 6 && (blk != sblk))
927             {
928                 blk--;
929                 if (tr->level_p >= 7 && (blk != sblk))
930                     blk--;
931             }
932         }
933     }
934
935     blk->level = 0;
936
937     return i;
938 }
939
940 static int get_non_intra_block_rq( bs_transrate_t *bs, RunLevel *blk,  const int i_qscale, const int i_qscale_new )
941 {
942     int tst;
943     int i, li;
944     int val;
945     const DCTtab * tab;
946
947     /* Basic sanity check --Meuuh */
948     if( i_qscale == 0 )
949     {
950         return 0;
951     }
952
953     tst = i_qscale_new/i_qscale + ((i_qscale_new%i_qscale) ? 1 : 0);
954
955     li = i = -1;
956
957     if (bs->i_bit_in_cache >= 0x28000000)
958     {
959         tab = DCT_B14DC_5 + (UBITS (bs->i_bit_in_cache, 5) - 5);
960         goto entry_1;
961     }
962     else goto entry_2;
963
964     for( ;; )
965     {
966         if (bs->i_bit_in_cache >= 0x28000000)
967         {
968             tab = DCT_B14AC_5 + (UBITS (bs->i_bit_in_cache, 5) - 5);
969
970     entry_1:
971             i += tab->run;
972             if (i >= 64)
973             break;  /* end of block */
974
975     normal_code:
976
977             bs_flush( bs, tab->len );
978             val = tab->level;
979             if (val >= tst)
980             {
981                 val = (val ^ SBITS (bs->i_bit_in_cache, 1)) - SBITS (bs->i_bit_in_cache, 1);
982                 blk->level = (val * i_qscale) / i_qscale_new;
983                 blk->run = i - li - 1;
984                 li = i;
985                 blk++;
986             }
987
988             //if ( ((val) && (tab->level < tst)) || ((!val) && (tab->level >= tst)) )
989             //  LOGF("level: %i val: %i tst : %i q: %i nq : %i\n", tab->level, val, tst, q, nq);
990
991             bs_flush( bs, 1 );
992             continue;
993         }
994
995     entry_2:
996         if (bs->i_bit_in_cache >= 0x04000000)
997         {
998             tab = DCT_B14_8 + (UBITS (bs->i_bit_in_cache, 8) - 4);
999
1000             i += tab->run;
1001             if (i < 64) goto normal_code;
1002
1003             /* escape code */
1004
1005             i += (UBITS (bs->i_bit_in_cache, 12) & 0x3F) - 64;
1006
1007             if (i >= 64) break; /* illegal, check needed to avoid buffer overflow */
1008
1009             bs_flush( bs, 12 );
1010             val = SBITS (bs->i_bit_in_cache, 12);
1011             if (abs(val) >= tst)
1012             {
1013                 blk->level = (val * i_qscale) / i_qscale_new;
1014                 blk->run = i - li - 1;
1015                 li = i;
1016                 blk++;
1017             }
1018
1019             bs_flush( bs, 12 );
1020             continue;
1021         }
1022         else if (bs->i_bit_in_cache >= 0x02000000)
1023         {
1024             tab = DCT_B14_10 + (UBITS (bs->i_bit_in_cache, 10) - 8);
1025             i += tab->run;
1026             if (i < 64) goto normal_code;
1027         }
1028         else if (bs->i_bit_in_cache >= 0x00800000)
1029         {
1030             tab = DCT_13 + (UBITS (bs->i_bit_in_cache, 13) - 16);
1031             i += tab->run;
1032             if (i < 64) goto normal_code;
1033         }
1034         else if (bs->i_bit_in_cache >= 0x00200000)
1035         {
1036             tab = DCT_15 + (UBITS (bs->i_bit_in_cache, 15) - 16);
1037             i += tab->run;
1038             if (i < 64) goto normal_code;
1039         }
1040         else
1041         {
1042             tab = DCT_16 + UBITS (bs->i_bit_in_cache, 16);
1043             bs_flush( bs, 16 );
1044
1045             i += tab->run;
1046             if (i < 64) goto normal_code;
1047         }
1048         break;  /* illegal, check needed to avoid buffer overflow */
1049     }
1050     bs_flush( bs, 2 );    /* dump end of block code */
1051
1052     blk->level = 0;
1053
1054     return i;
1055 }
1056
1057 static void motion_fr_frame( bs_transrate_t *bs, unsigned int f_code[2] )
1058 {
1059     get_motion_delta( bs, f_code[0] );
1060     get_motion_delta( bs, f_code[1] );
1061 }
1062
1063 static void motion_fr_field( bs_transrate_t *bs, unsigned int f_code[2] )
1064 {
1065     bs_copy( bs, 1);
1066
1067     get_motion_delta( bs, f_code[0]);
1068     get_motion_delta( bs, f_code[1]);
1069
1070     bs_copy( bs, 1);
1071
1072     get_motion_delta( bs, f_code[0]);
1073     get_motion_delta( bs, f_code[1]);
1074 }
1075
1076 static void motion_fr_dmv( bs_transrate_t *bs, unsigned int f_code[2] )
1077 {
1078     get_motion_delta( bs, f_code[0]);
1079     get_dmv( bs );
1080
1081     get_motion_delta( bs, f_code[1]);
1082     get_dmv( bs );
1083 }
1084
1085 static void motion_fi_field( bs_transrate_t *bs, unsigned int f_code[2] )
1086 {
1087     bs_copy( bs, 1);
1088
1089     get_motion_delta( bs, f_code[0]);
1090     get_motion_delta( bs, f_code[1]);
1091 }
1092
1093 static void motion_fi_16x8( bs_transrate_t *bs, unsigned int f_code[2] )
1094 {
1095     bs_copy( bs, 1);
1096
1097     get_motion_delta( bs, f_code[0]);
1098     get_motion_delta( bs, f_code[1]);
1099
1100     bs_copy( bs, 1);
1101
1102     get_motion_delta( bs, f_code[0]);
1103     get_motion_delta( bs, f_code[1]);
1104 }
1105
1106 static void motion_fi_dmv( bs_transrate_t *bs, unsigned int f_code[2] )
1107 {
1108     get_motion_delta( bs, f_code[0]);
1109     get_dmv( bs );
1110
1111     get_motion_delta( bs, f_code[1]);
1112     get_dmv( bs );
1113 }
1114
1115
1116 #define MOTION_CALL(routine,direction)                      \
1117 do {                                                        \
1118     if ((direction) & MACROBLOCK_MOTION_FORWARD)            \
1119         routine( bs, tr->f_code[0]);                        \
1120     if ((direction) & MACROBLOCK_MOTION_BACKWARD)           \
1121         routine( bs, tr->f_code[1]);                        \
1122 } while (0)
1123
1124 #define NEXT_MACROBLOCK                                         \
1125 do {                                                            \
1126     tr->h_offset += 16;                                         \
1127     if( tr->h_offset == tr->horizontal_size_value)              \
1128     {                                                           \
1129         tr->v_offset += 16;                                         \
1130         if (tr->v_offset > (tr->vertical_size_value - 16)) return;      \
1131         tr->h_offset = 0;                                       \
1132     }                                                           \
1133 } while (0)
1134
1135 static void putmbdata( transrate_t *tr, int macroblock_modes )
1136 {
1137     bs_transrate_t *bs = &tr->bs;
1138
1139     bs_write( bs,
1140               mbtypetab[tr->picture_coding_type-1][macroblock_modes&0x1F].code,
1141               mbtypetab[tr->picture_coding_type-1][macroblock_modes&0x1F].len);
1142
1143     switch ( tr->picture_coding_type)
1144     {
1145         case I_TYPE:
1146             if ((! (tr->frame_pred_frame_dct)) && (tr->picture_structure == FRAME_PICTURE))
1147                 bs_write( bs, macroblock_modes & DCT_TYPE_INTERLACED ? 1 : 0, 1);
1148             break;
1149
1150         case P_TYPE:
1151             if (tr->picture_structure != FRAME_PICTURE)
1152             {
1153                 if (macroblock_modes & MACROBLOCK_MOTION_FORWARD)
1154                     bs_write( bs, (macroblock_modes & MOTION_TYPE_MASK) / MOTION_TYPE_BASE, 2);
1155                 break;
1156             }
1157             else if (tr->frame_pred_frame_dct) break;
1158             else
1159             {
1160                 if (macroblock_modes & MACROBLOCK_MOTION_FORWARD)
1161                     bs_write( bs, (macroblock_modes & MOTION_TYPE_MASK) / MOTION_TYPE_BASE, 2);
1162                 if (macroblock_modes & (MACROBLOCK_INTRA | MACROBLOCK_PATTERN))
1163                     bs_write( bs, macroblock_modes & DCT_TYPE_INTERLACED ? 1 : 0, 1);
1164                 break;
1165             }
1166
1167         case B_TYPE:
1168             if (tr->picture_structure != FRAME_PICTURE)
1169             {
1170                 if (! (macroblock_modes & MACROBLOCK_INTRA))
1171                     bs_write( bs, (macroblock_modes & MOTION_TYPE_MASK) / MOTION_TYPE_BASE, 2);
1172                 break;
1173             }
1174             else if (tr->frame_pred_frame_dct) break;
1175             else
1176             {
1177                 if (macroblock_modes & MACROBLOCK_INTRA) goto intra;
1178                 bs_write( bs, (macroblock_modes & MOTION_TYPE_MASK) / MOTION_TYPE_BASE, 2);
1179                 if (macroblock_modes & (MACROBLOCK_INTRA | MACROBLOCK_PATTERN))
1180                 {
1181                     intra:
1182                     bs_write( bs, macroblock_modes & DCT_TYPE_INTERLACED ? 1 : 0, 1);
1183                 }
1184                 break;
1185             }
1186     }
1187 }
1188
1189 static inline void put_quantiser( transrate_t *tr )
1190 {
1191     bs_transrate_t *bs = &tr->bs;
1192
1193     bs_write( bs, tr->q_scale_type ? map_non_linear_mquant[tr->new_quantizer_scale] : tr->new_quantizer_scale >> 1, 5);
1194     tr->last_coded_scale = tr->new_quantizer_scale;
1195 }
1196
1197 static int slice_init( transrate_t *tr,  int code)
1198 {
1199     bs_transrate_t *bs = &tr->bs;
1200     int offset;
1201     const MBAtab * mba;
1202
1203     tr->v_offset = (code - 1) * 16;
1204
1205     tr->quantizer_scale = get_quantizer_scale( tr );
1206     if ( tr->picture_coding_type == P_TYPE)
1207     {
1208         tr->new_quantizer_scale = tr->quantizer_scale;
1209     }
1210     else
1211     {
1212         tr->new_quantizer_scale = getNewQuant(tr, tr->quantizer_scale);
1213     }
1214     put_quantiser( tr );
1215
1216     /*LOGF("************************\nstart of slice %i in %s picture. ori quant: %i new quant: %i\n", code,
1217         (picture_coding_type == I_TYPE ? "I_TYPE" : (picture_coding_type == P_TYPE ? "P_TYPE" : "B_TYPE")),
1218         quantizer_scale, new_quantizer_scale);*/
1219
1220     /* ignore intra_slice and all the extra data */
1221     while (bs->i_bit_in_cache & 0x80000000)
1222     {
1223         bs_flush( bs, 9 );
1224     }
1225
1226     /* decode initial macroblock address increment */
1227     offset = 0;
1228     for( ;; )
1229     {
1230         if (bs->i_bit_in_cache >= 0x08000000)
1231         {
1232             mba = MBA_5 + (UBITS (bs->i_bit_in_cache, 6) - 2);
1233             break;
1234         }
1235         else if (bs->i_bit_in_cache >= 0x01800000)
1236         {
1237             mba = MBA_11 + (UBITS (bs->i_bit_in_cache, 12) - 24);
1238             break;
1239         }
1240         else if( UBITS (bs->i_bit_in_cache, 12 ) == 8 )
1241         {
1242             /* macroblock_escape */
1243             offset += 33;
1244             bs_copy( bs, 11);
1245         }
1246         else
1247         {
1248             return -1;
1249         }
1250     }
1251
1252     bs_copy( bs, mba->len + 1);
1253     tr->h_offset = (offset + mba->mba) << 4;
1254
1255     while( tr->h_offset - (int)tr->horizontal_size_value >= 0)
1256     {
1257         tr->h_offset -= tr->horizontal_size_value;
1258         tr->v_offset += 16;
1259     }
1260
1261     if( tr->v_offset > tr->vertical_size_value - 16 )
1262     {
1263         return -1;
1264     }
1265     return 0;
1266 }
1267
1268 static void mpeg2_slice( transrate_t *tr, const int code )
1269 {
1270     bs_transrate_t *bs = &tr->bs;
1271
1272     if( slice_init( tr, code ) )
1273     {
1274         return;
1275     }
1276
1277     for( ;; )
1278     {
1279         int macroblock_modes;
1280         int mba_inc;
1281         const MBAtab * mba;
1282
1283         macroblock_modes = get_macroblock_modes( tr );
1284         if (macroblock_modes & MACROBLOCK_QUANT) tr->quantizer_scale = get_quantizer_scale( tr );
1285
1286         //LOGF("blk %i : ", h_offset >> 4);
1287
1288         if (macroblock_modes & MACROBLOCK_INTRA)
1289         {
1290             //LOG("intra "); if (macroblock_modes & MACROBLOCK_QUANT) LOGF("got new quant: %i ", quantizer_scale);
1291
1292             tr->new_quantizer_scale = increment_quant( tr, tr->quantizer_scale);
1293             if (tr->last_coded_scale == tr->new_quantizer_scale) macroblock_modes &= 0xFFFFFFEF; // remove MACROBLOCK_QUANT
1294             else macroblock_modes |= MACROBLOCK_QUANT; //add MACROBLOCK_QUANT
1295             putmbdata( tr, macroblock_modes);
1296             if (macroblock_modes & MACROBLOCK_QUANT) put_quantiser( tr );
1297
1298             //if (macroblock_modes & MACROBLOCK_QUANT) LOGF("put new quant: %i ", new_quantizer_scale);
1299
1300             if (tr->concealment_motion_vectors)
1301             {
1302                 if (tr->picture_structure != FRAME_PICTURE)
1303                 {
1304                     bs_copy( bs, 1); /* remove field_select */
1305                 }
1306                 /* like motion_frame, but parsing without actual motion compensation */
1307                 get_motion_delta( bs, tr->f_code[0][0]);
1308                 get_motion_delta( bs, tr->f_code[0][1]);
1309
1310                 bs_copy( bs, 1); /* remove marker_bit */
1311             }
1312
1313             if( tr->intra_vlc_format )
1314             {
1315                 /* Luma */
1316                 get_luma_dc_dct_diff( bs );     get_intra_block_B15( bs, tr->quantizer_scale, tr->new_quantizer_scale );
1317                 get_luma_dc_dct_diff( bs );     get_intra_block_B15( bs, tr->quantizer_scale, tr->new_quantizer_scale );
1318                 get_luma_dc_dct_diff( bs );     get_intra_block_B15( bs, tr->quantizer_scale, tr->new_quantizer_scale );
1319                 get_luma_dc_dct_diff( bs );     get_intra_block_B15( bs, tr->quantizer_scale, tr->new_quantizer_scale );
1320                 /* Chroma */
1321                 get_chroma_dc_dct_diff( bs );   get_intra_block_B15( bs, tr->quantizer_scale, tr->new_quantizer_scale );
1322                 get_chroma_dc_dct_diff( bs );   get_intra_block_B15( bs, tr->quantizer_scale, tr->new_quantizer_scale );
1323             }
1324             else
1325             {
1326                 /* Luma */
1327                 get_luma_dc_dct_diff( bs );     get_intra_block_B14( bs, tr->quantizer_scale, tr->new_quantizer_scale );
1328                 get_luma_dc_dct_diff( bs );     get_intra_block_B14( bs, tr->quantizer_scale, tr->new_quantizer_scale );
1329                 get_luma_dc_dct_diff( bs );     get_intra_block_B14( bs, tr->quantizer_scale, tr->new_quantizer_scale );
1330                 get_luma_dc_dct_diff( bs );     get_intra_block_B14( bs, tr->quantizer_scale, tr->new_quantizer_scale );
1331                 /* Chroma */
1332                 get_chroma_dc_dct_diff( bs );   get_intra_block_B14( bs, tr->quantizer_scale, tr->new_quantizer_scale );
1333                 get_chroma_dc_dct_diff( bs );   get_intra_block_B14( bs, tr->quantizer_scale, tr->new_quantizer_scale );
1334             }
1335         }
1336         else
1337         {
1338             RunLevel block[6][65]; // terminated by level = 0, so we need 64+1
1339             int new_coded_block_pattern = 0;
1340
1341             // begin saving data
1342             int batb;
1343             uint8_t   p_n_ow[32], *p_n_w,
1344                     *p_o_ow = bs->p_ow, *p_o_w = bs->p_w;
1345             uint32_t  i_n_bit_out, i_n_bit_out_cache,
1346                     i_o_bit_out  = bs->i_bit_out, i_o_bit_out_cache = bs->i_bit_out_cache;
1347
1348             bs->i_bit_out_cache = 0; bs->i_bit_out = BITS_IN_BUF;
1349             bs->p_ow = bs->p_w = p_n_ow;
1350
1351             if (tr->picture_structure == FRAME_PICTURE)
1352                 switch (macroblock_modes & MOTION_TYPE_MASK)
1353                 {
1354                     case MC_FRAME: MOTION_CALL (motion_fr_frame, macroblock_modes); break;
1355                     case MC_FIELD: MOTION_CALL (motion_fr_field, macroblock_modes); break;
1356                     case MC_DMV: MOTION_CALL (motion_fr_dmv, MACROBLOCK_MOTION_FORWARD); break;
1357                 }
1358             else
1359                 switch (macroblock_modes & MOTION_TYPE_MASK)
1360                 {
1361                     case MC_FIELD: MOTION_CALL (motion_fi_field, macroblock_modes); break;
1362                     case MC_16X8: MOTION_CALL (motion_fi_16x8, macroblock_modes); break;
1363                     case MC_DMV: MOTION_CALL (motion_fi_dmv, MACROBLOCK_MOTION_FORWARD); break;
1364                 }
1365
1366             assert(bs->p_w - bs->p_ow < 32);
1367
1368             p_n_w = bs->p_w;
1369             i_n_bit_out = bs->i_bit_out;
1370             i_n_bit_out_cache = bs->i_bit_out_cache;
1371             assert(bs->p_ow == p_n_ow);
1372
1373             bs->i_bit_out = i_o_bit_out ;
1374             bs->i_bit_out_cache = i_o_bit_out_cache;
1375             bs->p_ow = p_o_ow;
1376             bs->p_w = p_o_w;
1377             // end saving data
1378
1379             if ( tr->picture_coding_type == P_TYPE) tr->new_quantizer_scale = tr->quantizer_scale;
1380             else tr->new_quantizer_scale = getNewQuant( tr, tr->quantizer_scale);
1381
1382             //LOG("non intra "); if (macroblock_modes & MACROBLOCK_QUANT) LOGF("got new quant: %i ", quantizer_scale);
1383
1384             if (macroblock_modes & MACROBLOCK_PATTERN)
1385             {
1386                 const int cbp = get_coded_block_pattern( bs );
1387
1388                 if( tr->picture_coding_type == P_TYPE )
1389                 {
1390                     if( cbp&0x20 ) get_non_intra_block_drop( tr, block[0] );
1391                     if( cbp&0x10 ) get_non_intra_block_drop( tr, block[1] );
1392                     if( cbp&0x08 ) get_non_intra_block_drop( tr, block[2] );
1393                     if( cbp&0x04 ) get_non_intra_block_drop( tr, block[3] );
1394                     if( cbp&0x02 ) get_non_intra_block_drop( tr, block[4] );
1395                     if( cbp&0x01 ) get_non_intra_block_drop( tr, block[5] );
1396
1397                     new_coded_block_pattern = cbp;
1398                 }
1399                 else
1400                 {
1401                     if( cbp&0x20 )
1402                     {
1403                         get_non_intra_block_rq( bs, block[0], tr->quantizer_scale, tr->new_quantizer_scale );
1404                         if( isNotEmpty( block[0] ) ) new_coded_block_pattern |= 0x20;
1405                     }
1406                     if( cbp&0x10 )
1407                     {
1408                         get_non_intra_block_rq( bs, block[1], tr->quantizer_scale, tr->new_quantizer_scale );
1409                         if( isNotEmpty( block[1] ) ) new_coded_block_pattern |= 0x10;
1410                     }
1411                     if( cbp&0x08 )
1412                     {
1413                         get_non_intra_block_rq( bs, block[2], tr->quantizer_scale, tr->new_quantizer_scale );
1414                         if( isNotEmpty( block[2] ) ) new_coded_block_pattern |= 0x08;
1415                     }
1416                     if( cbp&0x04 )
1417                     {
1418                         get_non_intra_block_rq( bs, block[3], tr->quantizer_scale, tr->new_quantizer_scale );
1419                         if( isNotEmpty( block[3] ) ) new_coded_block_pattern |= 0x04;
1420                     }
1421                     if( cbp&0x02 )
1422                     {
1423                         get_non_intra_block_rq( bs, block[4], tr->quantizer_scale, tr->new_quantizer_scale );
1424                         if( isNotEmpty( block[4] ) ) new_coded_block_pattern |= 0x02;
1425                     }
1426                     if( cbp&0x01 )
1427                     {
1428                         get_non_intra_block_rq( bs, block[5], tr->quantizer_scale, tr->new_quantizer_scale );
1429                         if( isNotEmpty( block[5] ) ) new_coded_block_pattern |= 0x01;
1430                     }
1431                     if( !new_coded_block_pattern) macroblock_modes &= 0xFFFFFFED; // remove MACROBLOCK_PATTERN and MACROBLOCK_QUANT flag
1432                 }
1433             }
1434
1435             if (tr->last_coded_scale == tr->new_quantizer_scale) macroblock_modes &= 0xFFFFFFEF; // remove MACROBLOCK_QUANT
1436             else if (macroblock_modes & MACROBLOCK_PATTERN) macroblock_modes |= MACROBLOCK_QUANT; //add MACROBLOCK_QUANT
1437             assert( (macroblock_modes & MACROBLOCK_PATTERN) || !(macroblock_modes & MACROBLOCK_QUANT) );
1438
1439             putmbdata( tr, macroblock_modes);
1440             if( macroblock_modes & MACROBLOCK_QUANT )
1441             {
1442                 put_quantiser( tr );
1443             }
1444
1445             // put saved motion data...
1446             for (batb = 0; batb < (p_n_w - p_n_ow); batb++)
1447             {
1448                 bs_write( bs, p_n_ow[batb], 8 );
1449             }
1450             bs_write( bs, i_n_bit_out_cache, BITS_IN_BUF - i_n_bit_out);
1451             // end saved motion data...
1452
1453             if (macroblock_modes & MACROBLOCK_PATTERN)
1454             {
1455                 /* Write CBP */
1456                 bs_write( bs, cbptable[new_coded_block_pattern].code,cbptable[new_coded_block_pattern].len);
1457
1458                 if (new_coded_block_pattern & 0x20) putnonintrablk( bs, block[0]);
1459                 if (new_coded_block_pattern & 0x10) putnonintrablk( bs, block[1]);
1460                 if (new_coded_block_pattern & 0x08) putnonintrablk( bs, block[2]);
1461                 if (new_coded_block_pattern & 0x04) putnonintrablk( bs, block[3]);
1462                 if (new_coded_block_pattern & 0x02) putnonintrablk( bs, block[4]);
1463                 if (new_coded_block_pattern & 0x01) putnonintrablk( bs, block[5]);
1464             }
1465         }
1466
1467         //LOGF("\n\to: %i c: %i n: %i\n", quantizer_scale, last_coded_scale, new_quantizer_scale);
1468
1469         NEXT_MACROBLOCK;
1470
1471         mba_inc = 0;
1472         for( ;; )
1473         {
1474             if (bs->i_bit_in_cache >= 0x10000000)
1475             {
1476                 mba = MBA_5 + (UBITS (bs->i_bit_in_cache, 5) - 2);
1477                 break;
1478             }
1479             else if (bs->i_bit_in_cache >= 0x03000000)
1480             {
1481                 mba = MBA_11 + (UBITS (bs->i_bit_in_cache, 11) - 24);
1482                 break;
1483             }
1484             else if( UBITS (bs->i_bit_in_cache, 11 ) == 8 )
1485             {
1486                 /* macroblock_escape */
1487                 mba_inc += 33;
1488                 bs_copy( bs, 11);
1489             }
1490             else
1491             {
1492                 /* EOS or error */
1493                 return;
1494             }
1495         }
1496         bs_copy( bs, mba->len);
1497         mba_inc += mba->mba;
1498
1499         while( mba_inc-- )
1500         {
1501             NEXT_MACROBLOCK;
1502         }
1503     }
1504 }
1505
1506 /////---- end ext mpeg code
1507
1508 static int do_next_start_code( transrate_t *tr )
1509 {
1510     bs_transrate_t *bs = &tr->bs;
1511     uint8_t ID;
1512
1513     // get start code
1514     ID = bs->p_c[0];
1515
1516     /* Copy one byte */
1517     *bs->p_w++ = *bs->p_c++;
1518
1519     if (ID == 0x00) // pic header
1520     {
1521         tr->picture_coding_type = (bs->p_c[1] >> 3) & 0x7;
1522         bs->p_c[1] |= 0x7; bs->p_c[2] = 0xFF; bs->p_c[3] |= 0xF8; // vbv_delay is now 0xFFFF
1523
1524         memcpy(bs->p_w, bs->p_c, 4);
1525         bs->p_c += 4;
1526         bs->p_w += 4;
1527     }
1528     else if (ID == 0xB3) // seq header
1529     {
1530         tr->horizontal_size_value = (bs->p_c[0] << 4) | (bs->p_c[1] >> 4);
1531         tr->vertical_size_value = ((bs->p_c[1] & 0xF) << 8) | bs->p_c[2];
1532         if(!tr->horizontal_size_value || !tr->vertical_size_value )
1533         {
1534             return -1;
1535         }
1536
1537         memcpy(bs->p_w, bs->p_c, 8 );
1538         bs->p_c += 8;
1539         bs->p_w += 8;
1540     }
1541     else if (ID == 0xB5) // extension
1542     {
1543         if ((bs->p_c[0] >> 4) == 0x8) // pic coding ext
1544         {
1545             tr->f_code[0][0] = (bs->p_c[0] & 0xF) - 1;
1546             tr->f_code[0][1] = (bs->p_c[1] >> 4) - 1;
1547             tr->f_code[1][0] = (bs->p_c[1] & 0xF) - 1;
1548             tr->f_code[1][1] = (bs->p_c[2] >> 4) - 1;
1549
1550             /* tr->intra_dc_precision = (bs->p_c[2] >> 2) & 0x3; */
1551             tr->picture_structure = bs->p_c[2] & 0x3;
1552             tr->frame_pred_frame_dct = (bs->p_c[3] >> 6) & 0x1;
1553             tr->concealment_motion_vectors = (bs->p_c[3] >> 5) & 0x1;
1554             tr->q_scale_type = (bs->p_c[3] >> 4) & 0x1;
1555             tr->intra_vlc_format = (bs->p_c[3] >> 3) & 0x1;
1556             /* tr->alternate_scan = (bs->p_c[3] >> 2) & 0x1; */
1557
1558
1559             memcpy(bs->p_w, bs->p_c, 5);
1560             bs->p_c += 5;
1561             bs->p_w += 5;
1562         }
1563         else
1564         {
1565             *bs->p_w++ = *bs->p_c++;
1566         }
1567     }
1568     else if (ID == 0xB8) // gop header
1569     {
1570         memcpy(bs->p_w, bs->p_c, 4);
1571         bs->p_c += 4;
1572         bs->p_w += 4;
1573     }
1574     else if ((ID >= 0x01) && (ID <= 0xAF)) // slice
1575     {
1576         uint8_t *outTemp = bs->p_w, *inTemp = bs->p_c;
1577
1578 #if 0
1579         if( ( tr->picture_coding_type == B_TYPE && tr->quant_corr <  2.5f ) || // don't recompress if we're in advance!
1580             ( tr->picture_coding_type == P_TYPE && tr->quant_corr < -2.5f ) ||
1581             ( tr->picture_coding_type == I_TYPE && tr->quant_corr < -5.0f ) )
1582 #else
1583         if( ( tr->picture_coding_type == B_TYPE ) ||
1584             ( tr->picture_coding_type == P_TYPE && tr->level_p ) ||
1585             ( tr->picture_coding_type == I_TYPE && tr->level_i ) )
1586 #endif
1587         {
1588             if( !tr->horizontal_size_value || !tr->vertical_size_value )
1589             {
1590                 return -1;
1591             }
1592
1593             // init bit buffer
1594             bs->i_bit_in_cache = 0; bs->i_bit_in = 0;
1595             bs->i_bit_out_cache = 0; bs->i_bit_out = BITS_IN_BUF;
1596
1597             // get 32 bits
1598             bs_refill( bs );
1599             bs_refill( bs );
1600             bs_refill( bs );
1601             bs_refill( bs );
1602
1603             // begin bit level recoding
1604             mpeg2_slice(tr, ID);
1605
1606             bs_flush_read( bs );
1607             bs_flush_write( bs );
1608             // end bit level recoding
1609
1610             /* Basic sanity checks --Meuuh */
1611             if (bs->p_c > bs->p_r || bs->p_w > bs->p_rw)
1612             {
1613                 return -1;
1614             }
1615
1616             /*LOGF("type: %s code: %02i in : %6i out : %6i diff : %6i fact: %2.2f\n",
1617             (picture_coding_type == I_TYPE ? "I_TYPE" : (picture_coding_type == P_TYPE ? "P_TYPE" : "B_TYPE")),
1618             ID,  bs->p_c - inTemp, bs->p_w - outTemp, (bs->p_w - outTemp) - (bs->p_c - inTemp), (float)(bs->p_c - inTemp) / (float)(bs->p_w - outTemp));*/
1619
1620             if (bs->p_w - outTemp > bs->p_c - inTemp) // yes that might happen, rarely
1621             {
1622                 /*LOGF("*** slice bigger than before !! (type: %s code: %i in : %i out : %i diff : %i)\n",
1623                 (picture_coding_type == I_TYPE ? "I_TYPE" : (picture_coding_type == P_TYPE ? "P_TYPE" : "B_TYPE")),
1624                 ID, bs->p_c - inTemp, bs->p_w - outTemp, (bs->p_w - outTemp) - (bs->p_c - inTemp));*/
1625
1626                 // in this case, we'll just use the original slice !
1627                 memcpy(outTemp, inTemp, bs->p_c - inTemp);
1628                 bs->p_w = outTemp + (bs->p_c - inTemp);
1629
1630                 // adjust bs->i_byte_out
1631                 bs->i_byte_out -= (bs->p_w - outTemp) - (bs->p_c - inTemp);
1632             }
1633         }
1634     }
1635     return 0;
1636 }
1637
1638 void E_(process_frame)( sout_stream_t *p_stream,
1639                     sout_stream_id_t *id, sout_buffer_t *in, sout_buffer_t **out )
1640 {
1641     transrate_t *tr = &id->tr;
1642     bs_transrate_t *bs = &tr->bs;
1643
1644     sout_buffer_t       *p_out;
1645
1646     double              next_fact_x = 1.0;
1647
1648     /* The output buffer can't be bigger than the input buffer. */
1649     p_out = sout_BufferNew( p_stream->p_sout, in->i_size );
1650
1651     p_out->i_length = in->i_length;
1652     p_out->i_dts    = in->i_dts;
1653     p_out->i_pts    = in->i_pts;
1654     p_out->i_flags  = in->i_flags;
1655
1656     sout_BufferChain( out, p_out );
1657
1658     bs->p_rw = bs->p_ow = bs->p_w = p_out->p_buffer;
1659     bs->p_c = bs->p_r = in->p_buffer;
1660     bs->p_r += in->i_size + 4;
1661     bs->p_rw += in->i_size;
1662     *(in->p_buffer + in->i_size) = 0;
1663     *(in->p_buffer + in->i_size + 1) = 0;
1664     *(in->p_buffer + in->i_size + 2) = 1;
1665     *(in->p_buffer + in->i_size + 3) = 0;
1666
1667     /* Calculate how late we are */
1668     tr->quant_corr = 0.0 + B_HANDICAP;
1669     tr->level_i = 0;
1670     tr->level_p = 0;
1671     bs->i_byte_in = in->i_size;
1672     bs->i_byte_out  = 0;
1673
1674     if (tr->i_current_gop_size - in->i_size > 100)
1675     {
1676         if (tr->i_wanted_gop_size == in->i_size)
1677         {
1678             next_fact_x = 1.0;
1679         }
1680         else if ( tr->i_wanted_gop_size < in->i_size )
1681         {
1682             /* We're really late */
1683             next_fact_x = 10.0;
1684         }
1685         else
1686         {
1687             next_fact_x = ((double)(tr->i_current_gop_size - in->i_size)) /
1688                           (tr->i_wanted_gop_size - in->i_size);
1689         }
1690
1691         if (next_fact_x > QUANT_I)
1692         {
1693             tr->level_i = 1;
1694         }
1695         if (next_fact_x > QUANT_P)
1696         {
1697             tr->level_p = 1 + (next_fact_x - QUANT_P) / (QUANT_P_INC);
1698         }
1699     }
1700     if ( tr->i_wanted_gop_size < 0 )
1701     {
1702         /* We're really late */
1703         tr->current_fact_x = 3.0;
1704     }
1705     else
1706     {
1707         tr->current_fact_x = ((double)(tr->i_current_gop_size) /
1708                               (tr->i_wanted_gop_size));
1709     }
1710
1711     for ( ; ; )
1712     {
1713         uint8_t *p_end = &in->p_buffer[in->i_size];
1714
1715         /* Search next start code */
1716         for( ;; )
1717         {
1718             if( bs->p_c < p_end - 3 && bs->p_c[0] == 0 && bs->p_c[1] == 0 && bs->p_c[2] == 1 )
1719             {
1720                 /* Next start code */
1721                 break;
1722             }
1723             else if( bs->p_c < p_end - 6 &&
1724                      bs->p_c[0] == 0 && bs->p_c[1] == 0 && bs->p_c[2] == 0 &&
1725                      bs->p_c[3] == 0 && bs->p_c[4] == 0 && bs->p_c[5] == 0 )
1726             {
1727                 /* remove stuffing (looking for 6 0x00 bytes) */
1728                 bs->p_c++;
1729             }
1730             else
1731             {
1732                 /* Copy */
1733                 *bs->p_w++ = *bs->p_c++;
1734             }
1735
1736             if( bs->p_c >= p_end)
1737             {
1738                 break;
1739             }
1740         }
1741
1742         if( bs->p_c >= p_end )
1743         {
1744             break;
1745         }
1746
1747         /* Copy the start code */
1748         memcpy(bs->p_w, bs->p_c, 3 );
1749         bs->p_c += 3;
1750         bs->p_w += 3;
1751
1752         if (do_next_start_code( tr ) )
1753         {
1754             /* Error */
1755             break;
1756         }
1757
1758         tr->quant_corr = (((bs->i_byte_in - (bs->p_r - 4 - bs->p_c)) / tr->fact_x) - (bs->i_byte_out + (bs->p_w - bs->p_ow))) / REACT_DELAY + B_HANDICAP;
1759     }
1760
1761     bs->i_byte_out += bs->p_w - bs->p_ow;
1762     p_out->i_size = bs->p_w - bs->p_ow;
1763     tr->i_current_gop_size -= in->i_size;
1764     tr->i_wanted_gop_size -= p_out->i_size;
1765     tr->i_new_gop_size += bs->i_byte_out;
1766
1767 #if 0
1768     msg_Dbg( p_stream, "%d: %d -> %d (r: %f, n:%f, corr:%f)",
1769              tr->picture_coding_type, in->i_size, p_out->i_size,
1770              (float)in->i_size / p_out->i_size,
1771              next_fact_x, tr->quant_corr);
1772 #endif
1773 }
1774
1775