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