* modules/stream_out/transrate: New transrating module, based on GPL
[vlc.git] / modules / stream_out / transrate / transrate.c
1 /*****************************************************************************
2  * transrate.c
3  *****************************************************************************
4  * Copyright (C) 2003 VideoLAN
5  * Copyright (C) 2003 Freebox S.A.
6  * Copyright (C) 2003 Antoine Missout
7  * Copyright (C) 2000-2003 Michel Lespinasse <walken@zoy.org>
8  * Copyright (C) 1999-2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
9  * $Id: transrate.c,v 1.1 2003/11/07 21:30:52 massiot Exp $
10  *
11  * Authors: Christophe Massiot <massiot@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 #include <string.h>
37 #define NDEBUG 1
38 #include <assert.h>
39 #include <unistd.h>
40 #include <math.h>
41 #include <fcntl.h>
42
43 #include <vlc/vlc.h>
44 #include <vlc/input.h>
45 #include <vlc/sout.h>
46 #include <vlc/vout.h>
47 #include <vlc/decoder.h>
48
49 /*****************************************************************************
50  * Exported prototypes
51  *****************************************************************************/
52 static int      Open    ( vlc_object_t * );
53 static void     Close   ( vlc_object_t * );
54
55 static sout_stream_id_t *Add ( sout_stream_t *, sout_format_t * );
56 static int               Del ( sout_stream_t *, sout_stream_id_t * );
57 static int               Send( sout_stream_t *, sout_stream_id_t *, sout_buffer_t * );
58
59 static int  transrate_video_new    ( sout_stream_t *, sout_stream_id_t * );
60 static void transrate_video_close  ( sout_stream_t *, sout_stream_id_t * );
61 static int  transrate_video_process( sout_stream_t *, sout_stream_id_t *, sout_buffer_t *, sout_buffer_t ** );
62
63 /*****************************************************************************
64  * Module descriptor
65  *****************************************************************************/
66 vlc_module_begin();
67     set_description( _("Transrate stream") );
68     set_capability( "sout stream", 50 );
69     add_shortcut( "transrate" );
70     set_callbacks( Open, Close );
71 vlc_module_end();
72
73 struct sout_stream_sys_t
74 {
75     sout_stream_t   *p_out;
76
77     int             i_vbitrate;
78
79     mtime_t         i_first_frame;
80     mtime_t         i_dts, i_pts;
81 };
82
83 /*****************************************************************************
84  * Open:
85  *****************************************************************************/
86 static int Open( vlc_object_t *p_this )
87 {
88     sout_stream_t     *p_stream = (sout_stream_t*)p_this;
89     sout_stream_sys_t *p_sys;
90     char *val;
91
92     p_sys = malloc( sizeof( sout_stream_sys_t ) );
93     p_sys->p_out = sout_stream_new( p_stream->p_sout, p_stream->psz_next );
94
95     p_sys->i_vbitrate   = 0;
96
97     if( ( val = sout_cfg_find_value( p_stream->p_cfg, "vb" ) ) )
98     {
99         p_sys->i_vbitrate = atoi( val );
100         if( p_sys->i_vbitrate < 16000 )
101         {
102             p_sys->i_vbitrate *= 1000;
103         }
104     }
105     else
106     {
107         p_sys->i_vbitrate = 3000000;
108     }
109     p_sys->i_first_frame = 0;
110
111     msg_Dbg( p_stream, "codec video %dkb/s",
112              p_sys->i_vbitrate / 1024 );
113
114     if( !p_sys->p_out )
115     {
116         msg_Err( p_stream, "cannot create chain" );
117         free( p_sys );
118         return VLC_EGENERIC;
119     }
120     p_stream->pf_add    = Add;
121     p_stream->pf_del    = Del;
122     p_stream->pf_send   = Send;
123
124     p_stream->p_sys     = p_sys;
125
126     p_stream->p_sout->i_padding += 200;
127
128     return VLC_SUCCESS;
129 }
130
131 /*****************************************************************************
132  * Close:
133  *****************************************************************************/
134 static void Close( vlc_object_t * p_this )
135 {
136     sout_stream_t       *p_stream = (sout_stream_t *)p_this;
137     sout_stream_sys_t   *p_sys = p_stream->p_sys;
138
139     sout_stream_delete( p_sys->p_out );
140     free( p_sys );
141 }
142
143 struct sout_stream_id_t
144 {
145     void            *id;
146     vlc_bool_t      b_transrate;
147
148     sout_buffer_t   *p_current_buffer;
149     sout_buffer_t   *p_next_gop;
150     mtime_t         i_next_gop_duration;
151     size_t          i_next_gop_size;
152 };
153
154
155 static sout_stream_id_t * Add( sout_stream_t *p_stream, sout_format_t *p_fmt )
156 {
157     sout_stream_sys_t   *p_sys = p_stream->p_sys;
158     sout_stream_id_t    *id;
159
160     id = malloc( sizeof( sout_stream_id_t ) );
161     id->id = NULL;
162
163     if( p_fmt->i_cat == VIDEO_ES
164             && p_fmt->i_fourcc == VLC_FOURCC('m', 'p', 'g', 'v') )
165     {
166         msg_Dbg( p_stream,
167                  "creating video transrating for fcc=`%4.4s'",
168                  (char*)&p_fmt->i_fourcc );
169
170         /* build decoder -> filter -> encoder */
171         if( transrate_video_new( p_stream, id ) )
172         {
173             msg_Err( p_stream, "cannot create video chain" );
174             free( id );
175             return NULL;
176         }
177
178         /* open output stream */
179         id->id = p_sys->p_out->pf_add( p_sys->p_out, p_fmt );
180         id->b_transrate = VLC_TRUE;
181     }
182     else
183     {
184         msg_Dbg( p_stream, "not transrating a stream (fcc=`%4.4s')", (char*)&p_fmt->i_fourcc );
185         id->id = p_sys->p_out->pf_add( p_sys->p_out, p_fmt );
186         id->b_transrate = VLC_FALSE;
187
188         if( id->id == NULL )
189         {
190             free( id );
191             return NULL;
192         }
193     }
194
195     return id;
196 }
197
198 static int     Del      ( sout_stream_t *p_stream, sout_stream_id_t *id )
199 {
200     sout_stream_sys_t   *p_sys = p_stream->p_sys;
201
202     if( id->b_transrate )
203     {
204         transrate_video_close( p_stream, id );
205     }
206
207     if( id->id ) p_sys->p_out->pf_del( p_sys->p_out, id->id );
208     free( id );
209
210     return VLC_SUCCESS;
211 }
212
213 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
214                  sout_buffer_t *p_buffer )
215 {
216     sout_stream_sys_t   *p_sys = p_stream->p_sys;
217
218     if( id->b_transrate )
219     {
220         sout_buffer_t *p_buffer_out;
221         transrate_video_process( p_stream, id, p_buffer, &p_buffer_out );
222
223         if( p_buffer_out )
224         {
225             return p_sys->p_out->pf_send( p_sys->p_out, id->id, p_buffer_out );
226         }
227         return VLC_SUCCESS;
228     }
229     else if( id->id != NULL )
230     {
231         return p_sys->p_out->pf_send( p_sys->p_out, id->id, p_buffer );
232     }
233     else
234     {
235         sout_BufferDelete( p_stream->p_sout, p_buffer );
236         return VLC_EGENERIC;
237     }
238 }
239
240 /****************************************************************************
241  * transrater, code from M2VRequantizer http://www.metakine.com/
242  ****************************************************************************/
243
244 // toggles:
245 // #define NDEBUG // turns off asserts
246 #define REMOVE_BYTE_STUFFING    // removes 0x 00 00 00 00 00 00 used in cbr streams (look for 6 0x00 and remove 1 0x00)
247                                                                 /*      4 0x00 might be legit, for exemple:
248                                                                         00 00 01 b5 14 82 00 01 00 00 00 00 01 b8 .. ..
249                                                                                                  these two: -- -- are part of the seq. header ext.
250                                                                         AFAIK 5 0x00 should never happen except for byte stuffing but to be safe look for 6 */
251
252 /* This is awful magic --Meuuh */
253 //#define REACT_DELAY (1024.0*128.0)
254 #define REACT_DELAY (1024.0*4.0)
255
256 // notes:
257 //
258 // - intra block:
259 //              - the quantiser is increment by one step
260 //
261 // - non intra block:
262 //              - in P_FRAME we keep the original quantiser but drop the last coefficient
263 //                if there is more than one
264 //              - in B_FRAME we multiply the quantiser by a factor
265 //
266 // - I_FRAME is recoded when we're 5.0 * REACT_DELAY late
267 // - P_FRAME is recoded when we're 2.5 * REACT_DELAY late
268 // - B_FRAME are always recoded
269
270 // if we're getting *very* late (60 * REACT_DELAY)
271 //
272 // - intra blocks quantiser is incremented two step
273 // - drop a few coefficients but always keep the first one
274
275 // useful constants
276 #define I_TYPE 1
277 #define P_TYPE 2
278 #define B_TYPE 3
279
280 // gcc
281 #ifdef HAVE_BUILTIN_EXPECT
282 #define likely(x) __builtin_expect ((x) != 0, 1)
283 #define unlikely(x) __builtin_expect ((x) != 0, 0)
284 #else
285 #define likely(x) (x)
286 #define unlikely(x) (x)
287 #endif
288
289 // user defined types
290 //typedef unsigned int          uint;
291 typedef unsigned char           uint8;
292 typedef unsigned short          uint16;
293 typedef unsigned int            uint32;
294 typedef unsigned long long      uint64;
295
296 typedef char                            int8;
297 typedef short                           int16;
298 typedef int                                     int32;
299 typedef long long                       int64;
300
301 typedef signed int                      sint;
302 typedef signed char                     sint8;
303 typedef signed short            sint16;
304 typedef signed int                      sint32;
305 typedef signed long long        sint64;
306
307 #define BITS_IN_BUF (8)
308
309 // global variables
310 static uint8    *cbuf, *rbuf, *wbuf, *orbuf, *owbuf, *rwbuf;
311 static int              inbitcnt, outbitcnt;
312 static uint32   inbitbuf, outbitbuf;
313 static uint32   inbytecnt, outbytecnt;
314
315     static struct sout_stream_sys_t * p_sys;
316 // mpeg2 state
317         // seq header
318         static uint horizontal_size_value;
319         static uint vertical_size_value;
320         
321         // pic header
322         static uint picture_coding_type;
323         
324         // pic code ext
325         static uint f_code[2][2];
326         static uint intra_dc_precision;
327         static uint picture_structure;
328         static uint frame_pred_frame_dct;
329         static uint concealment_motion_vectors;
330         static uint q_scale_type;
331         static uint intra_vlc_format;
332         static uint alternate_scan;
333         
334         // slice or mb
335         // quantizer_scale_code
336         static uint quantizer_scale;
337         static uint new_quantizer_scale;
338         static uint last_coded_scale;
339         static int       h_offset, v_offset;
340         
341         // mb
342         static double quant_corr, fact_x;
343         
344         // block data
345         typedef struct
346         {
347                 uint8 run;
348                 short level;
349         } RunLevel;
350
351         static RunLevel block[6][65]; // terminated by level = 0, so we need 64+1
352 // end mpeg2 state
353
354 #define LOG(msg) fprintf (stderr, msg)
355 #define LOGF(format, args...) fprintf (stderr, format, args)
356
357 #define WRITE \
358         do { } while(0);
359
360 #define LOCK(x) \
361         do { } while(0);
362
363 #define COPY(x)\
364                 assert(x > 0); \
365                 memcpy(wbuf, cbuf, x);\
366                 cbuf += x; \
367                 wbuf += x;
368
369 #define SEEKR(x)\
370                 cbuf += x;
371         
372 #define SEEKW(x)\
373                 wbuf += x;
374
375 static inline void putbits(uint val, int n)
376 {
377         assert(n < 32);
378         assert(!(val & (0xffffffffU << n)));
379
380         while (unlikely(n >= outbitcnt))
381         {
382                 wbuf[0] = (outbitbuf << outbitcnt ) | (val >> (n - outbitcnt));
383                 SEEKW(1);
384                 n -= outbitcnt;
385                 outbitbuf = 0;
386                 val &= ~(0xffffffffU << n);
387                 outbitcnt = BITS_IN_BUF;
388         }
389         
390         if (likely(n))
391         {
392                 outbitbuf = (outbitbuf << n) | val;
393                 outbitcnt -= n;
394         }
395         
396         assert(outbitcnt > 0);
397         assert(outbitcnt <= BITS_IN_BUF);
398 }
399
400 static inline void Refill_bits(void)
401 {
402         assert((rbuf - cbuf) >= 1);
403         inbitbuf |= cbuf[0] << (24 - inbitcnt);
404         inbitcnt += 8;
405         SEEKR(1)
406 }
407
408 static inline void Flush_Bits(uint n)
409 {
410         assert(inbitcnt >= n);
411
412         inbitbuf <<= n;
413         inbitcnt -= n;
414
415         assert( (!n) || ((n>0) && !(inbitbuf & 0x1)) );
416
417         while (unlikely(inbitcnt < 24)) Refill_bits();
418 }
419
420 static inline uint Show_Bits(uint n)
421 {
422         return ((unsigned int)inbitbuf) >> (32 - n);
423 }
424
425 static inline uint Get_Bits(uint n)
426 {
427         uint Val = Show_Bits(n);
428         Flush_Bits(n);
429         return Val;
430 }
431
432 static inline uint Copy_Bits(uint n)
433 {
434         uint Val = Get_Bits(n);
435         putbits(Val, n);
436         return Val;
437 }
438
439 static inline void flush_read_buffer()
440 {
441         int i = inbitcnt & 0x7;
442         if (i)
443         {
444                 //uint val = Show_Bits(i);
445                 //putbits(val, i);
446                 assert(((unsigned int)inbitbuf) >> (32 - i) == 0);
447                 inbitbuf <<= i;
448                 inbitcnt -= i;
449         }
450         SEEKR(-1 * (inbitcnt >> 3));
451         inbitcnt = 0;
452 }
453
454
455 static inline void flush_write_buffer()
456 {
457         if (outbitcnt != 8) putbits(0, outbitcnt);
458 }
459
460 /////---- begin ext mpeg code
461
462 const uint8 non_linear_mquant_table[32] =
463 {
464         0, 1, 2, 3, 4, 5, 6, 7,
465         8,10,12,14,16,18,20,22,
466         24,28,32,36,40,44,48,52,
467         56,64,72,80,88,96,104,112
468 };
469 const uint8 map_non_linear_mquant[113] =
470 {
471         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,
472         16,17,17,17,18,18,18,18,19,19,19,19,20,20,20,20,21,21,21,21,22,22,
473         22,22,23,23,23,23,24,24,24,24,24,24,24,25,25,25,25,25,25,25,26,26,
474         26,26,26,26,26,26,27,27,27,27,27,27,27,27,28,28,28,28,28,28,28,29,
475         29,29,29,29,29,29,29,29,29,30,30,30,30,30,30,30,31,31,31,31,31
476 };
477
478 static int scale_quant(double quant )
479 {
480         int iquant;
481         if (q_scale_type)
482         {
483                 iquant = (int) floor(quant+0.5);
484
485                 /* clip mquant to legal (linear) range */
486                 if (iquant<1) iquant = 1;
487                 if (iquant>112) iquant = 112;
488
489                 iquant = non_linear_mquant_table[map_non_linear_mquant[iquant]];
490         }
491         else
492         {
493                 /* clip mquant to legal (linear) range */
494                 iquant = (int)floor(quant+0.5);
495                 if (iquant<2) iquant = 2;
496                 if (iquant>62) iquant = 62;
497                 iquant = (iquant/2)*2; // Must be *even*
498         }
499         return iquant;
500 }
501
502 static int increment_quant(int quant)
503 {
504         if (q_scale_type)
505         {
506                 assert(quant >= 1 && quant <= 112);
507                 quant = map_non_linear_mquant[quant] + 1;
508                 if (quant_corr < -60.0f) quant++;
509                 if (quant > 31) quant = 31;
510                 quant = non_linear_mquant_table[quant];
511         }
512         else
513         {
514                 assert(!(quant & 1));
515                 quant += 2;
516                 if (quant_corr < -60.0f) quant += 2;
517                 if (quant > 62) quant = 62;
518         }
519         return quant;
520 }
521
522 static inline int intmax( register int x, register int y )
523 { return x < y ? y : x; }
524
525 static inline int intmin( register int x, register int y )
526 { return x < y ? x : y; }
527
528 static int getNewQuant(int curQuant)
529 {
530         double calc_quant, quant_to_use;
531         int mquant = 0;
532         
533         quant_corr = (((inbytecnt - (rbuf - 4 - cbuf)) / fact_x) - (outbytecnt + (wbuf - owbuf))) / REACT_DELAY;
534         calc_quant = curQuant * fact_x;
535         quant_to_use = calc_quant - quant_corr;
536
537         switch (picture_coding_type)
538         {
539                 case I_TYPE:
540                 case P_TYPE:
541                         mquant = increment_quant(curQuant);
542                         break;
543                         
544                 case B_TYPE:
545                         mquant = intmax(scale_quant(quant_to_use), increment_quant(curQuant));
546                         break;
547                         
548                 default:
549                         assert(0);
550                         break;
551         }
552         
553         /*
554                 LOGF("type: %s orig_quant: %3i calc_quant: %7.1f quant_corr: %7.1f using_quant: %3i\n",
555                 (picture_coding_type == I_TYPE ? "I_TYPE" : (picture_coding_type == P_TYPE ? "P_TYPE" : "B_TYPE")),
556                 (int)curQuant, (float)calc_quant, (float)quant_corr, (int)mquant);
557         */
558                 
559         assert(mquant >= curQuant);
560         
561         return mquant;
562 }
563
564 static inline int isNotEmpty(RunLevel *blk)
565 {
566         return (blk->level);
567 }
568
569 #include "putvlc.h"
570
571 void putAC(int run, int signed_level, int vlcformat)
572 {
573         int level, len;
574         const VLCtable *ptab = NULL;
575         
576         level = (signed_level<0) ? -signed_level : signed_level; /* abs(signed_level) */
577         
578         assert(!(run<0 || run>63 || level==0 || level>2047));
579         
580         len = 0;
581         
582         if (run<2 && level<41)
583         {
584                 if (vlcformat)  ptab = &dct_code_tab1a[run][level-1];
585                 else ptab = &dct_code_tab1[run][level-1];
586                 len = ptab->len;
587         }
588         else if (run<32 && level<6)
589         {
590                 if (vlcformat) ptab = &dct_code_tab2a[run-2][level-1];
591                 else ptab = &dct_code_tab2[run-2][level-1];
592                 len = ptab->len;
593         }
594         
595         if (len) /* a VLC code exists */
596         {
597                 putbits(ptab->code, len);
598                 putbits(signed_level<0, 1); /* sign */
599         }
600         else
601         {
602                 putbits(1l, 6); /* Escape */
603                 putbits(run, 6); /* 6 bit code for run */
604                 putbits(((uint)signed_level) & 0xFFF, 12);
605         }
606 }
607
608
609 static inline void putACfirst(int run, int val)
610 {
611         if (run==0 && (val==1 || val==-1)) putbits(2|(val<0),2);
612         else putAC(run,val,0);
613 }
614
615 void putnonintrablk(RunLevel *blk)
616 {
617         assert(blk->level);
618         
619         putACfirst(blk->run, blk->level);
620         blk++;
621         
622         while(blk->level)
623         {
624                 putAC(blk->run, blk->level, 0);
625                 blk++;
626         }
627         
628         putbits(2,2);
629 }
630
631 static inline void putcbp(int cbp)
632 {
633         putbits(cbptable[cbp].code,cbptable[cbp].len);
634 }
635
636 void putmbtype(int mb_type)
637 {
638         putbits(mbtypetab[picture_coding_type-1][mb_type].code,
639                         mbtypetab[picture_coding_type-1][mb_type].len);
640 }
641
642 #include <inttypes.h>
643 #include "getvlc.h"
644
645 static int non_linear_quantizer_scale [] =
646 {
647      0,  1,  2,  3,  4,  5,   6,   7,
648      8, 10, 12, 14, 16, 18,  20,  22,
649     24, 28, 32, 36, 40, 44,  48,  52,
650     56, 64, 72, 80, 88, 96, 104, 112
651 };
652
653 static inline int get_macroblock_modes ()
654 {
655     int macroblock_modes;
656     const MBtab * tab;
657
658     switch (picture_coding_type)
659         {
660                 case I_TYPE:
661         
662                         tab = MB_I + UBITS (bit_buf, 1);
663                         DUMPBITS (bit_buf, bits, tab->len);
664                         macroblock_modes = tab->modes;
665                 
666                         if ((! (frame_pred_frame_dct)) && (picture_structure == FRAME_PICTURE))
667                         {
668                                 macroblock_modes |= UBITS (bit_buf, 1) * DCT_TYPE_INTERLACED;
669                                 DUMPBITS (bit_buf, bits, 1);
670                         }
671                 
672                         return macroblock_modes;
673         
674                 case P_TYPE:
675         
676                         tab = MB_P + UBITS (bit_buf, 5);
677                         DUMPBITS (bit_buf, bits, tab->len);
678                         macroblock_modes = tab->modes;
679                 
680                         if (picture_structure != FRAME_PICTURE)
681                         {
682                                 if (macroblock_modes & MACROBLOCK_MOTION_FORWARD)
683                                 {
684                                         macroblock_modes |= UBITS (bit_buf, 2) * MOTION_TYPE_BASE;
685                                         DUMPBITS (bit_buf, bits, 2);
686                                 }
687                                 return macroblock_modes;
688                         }
689                         else if (frame_pred_frame_dct)
690                         {
691                                 if (macroblock_modes & MACROBLOCK_MOTION_FORWARD)
692                                         macroblock_modes |= MC_FRAME;
693                                 return macroblock_modes;
694                         }
695                         else
696                         {
697                                 if (macroblock_modes & MACROBLOCK_MOTION_FORWARD)
698                                 {
699                                         macroblock_modes |= UBITS (bit_buf, 2) * MOTION_TYPE_BASE;
700                                         DUMPBITS (bit_buf, bits, 2);
701                                 }
702                                 if (macroblock_modes & (MACROBLOCK_INTRA | MACROBLOCK_PATTERN))
703                                 {
704                                         macroblock_modes |= UBITS (bit_buf, 1) * DCT_TYPE_INTERLACED;
705                                         DUMPBITS (bit_buf, bits, 1);
706                                 }
707                                 return macroblock_modes;
708                         }
709         
710                 case B_TYPE:
711         
712                         tab = MB_B + UBITS (bit_buf, 6);
713                         DUMPBITS (bit_buf, bits, tab->len);
714                         macroblock_modes = tab->modes;
715                 
716                         if (picture_structure != FRAME_PICTURE)
717                         {
718                                 if (! (macroblock_modes & MACROBLOCK_INTRA))
719                                 {
720                                         macroblock_modes |= UBITS (bit_buf, 2) * MOTION_TYPE_BASE;
721                                         DUMPBITS (bit_buf, bits, 2);
722                                 }
723                                 return macroblock_modes;
724                         }
725                         else if (frame_pred_frame_dct)
726                         {
727                                 /* if (! (macroblock_modes & MACROBLOCK_INTRA)) */
728                                 macroblock_modes |= MC_FRAME;
729                                 return macroblock_modes;
730                         }
731                         else
732                         {
733                                 if (macroblock_modes & MACROBLOCK_INTRA) goto intra;
734                                 macroblock_modes |= UBITS (bit_buf, 2) * MOTION_TYPE_BASE;
735                                 DUMPBITS (bit_buf, bits, 2);
736                                 if (macroblock_modes & (MACROBLOCK_INTRA | MACROBLOCK_PATTERN))
737                                 {
738                                         intra:
739                                         macroblock_modes |= UBITS (bit_buf, 1) * DCT_TYPE_INTERLACED;
740                                         DUMPBITS (bit_buf, bits, 1);
741                                 }
742                                 return macroblock_modes;
743                         }
744         
745                 default:
746                         return 0;
747     }
748
749 }
750
751 static inline int get_quantizer_scale ()
752 {
753     int quantizer_scale_code;
754
755     quantizer_scale_code = UBITS (bit_buf, 5);
756         DUMPBITS (bit_buf, bits, 5); 
757         
758         if (q_scale_type) return non_linear_quantizer_scale[quantizer_scale_code];
759     else return quantizer_scale_code << 1;
760 }
761
762 static inline int get_motion_delta (const int f_code)
763 {
764 #define bit_buf (inbitbuf)
765
766     int delta;
767     int sign;
768     const MVtab * tab;
769
770     if (bit_buf & 0x80000000)
771         {
772                 COPYBITS (bit_buf, bits, 1);
773                 return 0;
774     }
775         else if (bit_buf >= 0x0c000000)
776         {
777
778                 tab = MV_4 + UBITS (bit_buf, 4);
779                 delta = (tab->delta << f_code) + 1;
780                 COPYBITS (bit_buf, bits, tab->len);
781         
782                 sign = SBITS (bit_buf, 1);
783                 COPYBITS (bit_buf, bits, 1);
784         
785                 if (f_code) delta += UBITS (bit_buf, f_code);
786                 COPYBITS (bit_buf, bits, f_code);
787         
788                 return (delta ^ sign) - sign;
789     }
790         else
791         {
792
793                 tab = MV_10 + UBITS (bit_buf, 10);
794                 delta = (tab->delta << f_code) + 1;
795                 COPYBITS (bit_buf, bits, tab->len);
796         
797                 sign = SBITS (bit_buf, 1);
798                 COPYBITS (bit_buf, bits, 1);
799         
800                 if (f_code)
801                 {
802                         delta += UBITS (bit_buf, f_code);
803                         COPYBITS (bit_buf, bits, f_code);
804                 }
805         
806                 return (delta ^ sign) - sign;
807     }
808 }
809
810
811 static inline int get_dmv ()
812 {
813     const DMVtab * tab;
814
815     tab = DMV_2 + UBITS (bit_buf, 2);
816     COPYBITS (bit_buf, bits, tab->len);
817     return tab->dmv;
818 }
819
820 static inline int get_coded_block_pattern ()
821 {
822 #define bit_buf (inbitbuf)
823     const CBPtab * tab;
824
825     if (bit_buf >= 0x20000000)
826         {
827                 tab = CBP_7 + (UBITS (bit_buf, 7) - 16);
828                 DUMPBITS (bit_buf, bits, tab->len);
829                 return tab->cbp;
830     }
831         else
832         {
833                 tab = CBP_9 + UBITS (bit_buf, 9);
834                 DUMPBITS (bit_buf, bits, tab->len);
835                 return tab->cbp;
836     }
837 }
838
839 static inline int get_luma_dc_dct_diff ()
840 {
841 #define bit_buf (inbitbuf)
842     const DCtab * tab;
843     int size;
844     int dc_diff;
845
846     if (bit_buf < 0xf8000000)
847         {
848                 tab = DC_lum_5 + UBITS (bit_buf, 5);
849                 size = tab->size;
850                 if (size)
851                 {
852                         COPYBITS (bit_buf, bits, tab->len);
853                         //dc_diff = UBITS (bit_buf, size) - UBITS (SBITS (~bit_buf, 1), size);
854                         dc_diff = UBITS (bit_buf, size); if (!(dc_diff >> (size - 1))) dc_diff = (dc_diff + 1) - (1 << size);
855                         COPYBITS (bit_buf, bits, size);
856                         return dc_diff;
857                 }
858                 else
859                 {
860                         COPYBITS (bit_buf, bits, 3);
861                         return 0;
862                 }
863     }
864         else
865         {
866                 tab = DC_long + (UBITS (bit_buf, 9) - 0x1e0);
867                 size = tab->size;
868                 COPYBITS (bit_buf, bits, tab->len);
869                 //dc_diff = UBITS (bit_buf, size) - UBITS (SBITS (~bit_buf, 1), size);
870                 dc_diff = UBITS (bit_buf, size); if (!(dc_diff >> (size - 1))) dc_diff = (dc_diff + 1) - (1 << size);
871                 COPYBITS (bit_buf, bits, size);
872                 return dc_diff;
873     }
874 }
875
876 static inline int get_chroma_dc_dct_diff ()
877 {
878 #define bit_buf (inbitbuf)
879
880     const DCtab * tab;
881     int size;
882     int dc_diff;
883
884     if (bit_buf < 0xf8000000)
885         {
886                 tab = DC_chrom_5 + UBITS (bit_buf, 5);
887                 size = tab->size;
888                 if (size)
889                 {
890                         COPYBITS (bit_buf, bits, tab->len);
891                         //dc_diff = UBITS (bit_buf, size) - UBITS (SBITS (~bit_buf, 1), size);
892                         dc_diff = UBITS (bit_buf, size); if (!(dc_diff >> (size - 1))) dc_diff = (dc_diff + 1) - (1 << size);
893                         COPYBITS (bit_buf, bits, size);
894                         return dc_diff;
895                 } else
896                 {
897                         COPYBITS (bit_buf, bits, 2);
898                         return 0;
899                 }
900     }
901         else
902         {
903                 tab = DC_long + (UBITS (bit_buf, 10) - 0x3e0);
904                 size = tab->size;
905                 COPYBITS (bit_buf, bits, tab->len + 1);
906                 //dc_diff = UBITS (bit_buf, size) - UBITS (SBITS (~bit_buf, 1), size);
907                 dc_diff = UBITS (bit_buf, size); if (!(dc_diff >> (size - 1))) dc_diff = (dc_diff + 1) - (1 << size);
908                 COPYBITS (bit_buf, bits, size);
909                 return dc_diff;
910     }
911 }
912
913 static void get_intra_block_B14 ()
914 {
915 #define bit_buf (inbitbuf)
916         int q = quantizer_scale, nq = new_quantizer_scale, tst;
917     int i, li;
918     int val;
919     const DCTtab * tab;
920         
921     /* Basic sanity check --Meuuh */
922     if ( q == 0 )
923         return;
924
925     tst = (nq / q) + ((nq % q) ? 1 : 0);
926
927     li = i = 0;
928
929     while (1)
930         {
931                 if (bit_buf >= 0x28000000)
932                 {
933                         tab = DCT_B14AC_5 + (UBITS (bit_buf, 5) - 5);
934         
935                         i += tab->run;
936                         if (i >= 64) break;     /* end of block */
937         
938         normal_code:
939                         DUMPBITS (bit_buf, bits, tab->len);
940                         val = tab->level;
941                         if (val >= tst)
942                         {
943                                 val = (val ^ SBITS (bit_buf, 1)) - SBITS (bit_buf, 1);
944                                 putAC(i - li - 1, (val * q) / nq, 0);
945                                 li = i;
946                         }
947         
948                         DUMPBITS (bit_buf, bits, 1);
949         
950                         continue;
951                 }
952                 else if (bit_buf >= 0x04000000)
953                 {
954                         tab = DCT_B14_8 + (UBITS (bit_buf, 8) - 4);
955         
956                         i += tab->run;
957                         if (i < 64) goto normal_code;
958         
959                         /* escape code */
960                         i += (UBITS (bit_buf, 12) & 0x3F) - 64;
961                         if (i >= 64) break;     /* illegal, check needed to avoid buffer overflow */
962         
963                         DUMPBITS (bit_buf, bits, 12);
964                         val = SBITS (bit_buf, 12);
965                         if (abs(val) >= tst)
966                         {
967                                 putAC(i - li - 1, (val * q) / nq, 0);
968                                 li = i;
969                         }
970         
971                         DUMPBITS (bit_buf, bits, 12);
972         
973                         continue;
974                 }
975                 else if (bit_buf >= 0x02000000)
976                 {
977                         tab = DCT_B14_10 + (UBITS (bit_buf, 10) - 8);
978                         i += tab->run;
979                         if (i < 64) goto normal_code;
980                 }
981                 else if (bit_buf >= 0x00800000)
982                 {
983                         tab = DCT_13 + (UBITS (bit_buf, 13) - 16);
984                         i += tab->run;
985                         if (i < 64) goto normal_code;
986                 }
987                 else if (bit_buf >= 0x00200000)
988                 {
989                         tab = DCT_15 + (UBITS (bit_buf, 15) - 16);
990                         i += tab->run;
991                         if (i < 64) goto normal_code;
992                 }
993                 else
994                 {
995                         tab = DCT_16 + UBITS (bit_buf, 16);
996                         DUMPBITS (bit_buf, bits, 16);
997                         i += tab->run;
998                         if (i < 64) goto normal_code;
999                 }
1000                 break;  /* illegal, check needed to avoid buffer overflow */
1001         }
1002         
1003         COPYBITS (bit_buf, bits, 2);    /* end of block code */
1004 }
1005
1006 static void get_intra_block_B15 ()
1007 {
1008 #define bit_buf (inbitbuf)
1009         int q = quantizer_scale, nq = new_quantizer_scale, tst;
1010     int i, li;
1011     int val;
1012     const DCTtab * tab;
1013         
1014     /* Basic sanity check --Meuuh */
1015     if ( q == 0 )
1016         return;
1017
1018     tst = (nq / q) + ((nq % q) ? 1 : 0);
1019
1020     li = i = 0;
1021
1022     while (1)
1023         {
1024                 if (bit_buf >= 0x04000000)
1025                 {
1026                         tab = DCT_B15_8 + (UBITS (bit_buf, 8) - 4);
1027         
1028                         i += tab->run;
1029                         if (i < 64)
1030                         {
1031         normal_code:
1032                                 DUMPBITS (bit_buf, bits, tab->len);
1033                                 
1034                                 val = tab->level;
1035                                 if (val >= tst)
1036                                 {
1037                                         val = (val ^ SBITS (bit_buf, 1)) - SBITS (bit_buf, 1);
1038                                         putAC(i - li - 1, (val * q) / nq, 1);
1039                                         li = i;
1040                                 }
1041                 
1042                                 DUMPBITS (bit_buf, bits, 1);
1043                 
1044                                 continue;
1045                         }
1046                         else
1047                         {
1048                                 i += (UBITS (bit_buf, 12) & 0x3F) - 64;
1049                                 
1050                                 if (i >= 64) break;     /* illegal, check against buffer overflow */
1051                 
1052                                 DUMPBITS (bit_buf, bits, 12);
1053                                 val = SBITS (bit_buf, 12);
1054                                 if (abs(val) >= tst)
1055                                 {
1056                                         putAC(i - li - 1, (val * q) / nq, 1);
1057                                         li = i;
1058                                 }
1059                 
1060                                 DUMPBITS (bit_buf, bits, 12);
1061                 
1062                                 continue;
1063                         }
1064                 }
1065                 else if (bit_buf >= 0x02000000)
1066                 {
1067                         tab = DCT_B15_10 + (UBITS (bit_buf, 10) - 8);
1068                         i += tab->run;
1069                         if (i < 64) goto normal_code;
1070                 }
1071                 else if (bit_buf >= 0x00800000)
1072                 {
1073                         tab = DCT_13 + (UBITS (bit_buf, 13) - 16);
1074                         i += tab->run;
1075                         if (i < 64) goto normal_code;
1076                 }
1077                 else if (bit_buf >= 0x00200000)
1078                 {
1079                         tab = DCT_15 + (UBITS (bit_buf, 15) - 16);
1080                         i += tab->run;
1081                         if (i < 64) goto normal_code;
1082                 }
1083                 else
1084                 {
1085                         tab = DCT_16 + UBITS (bit_buf, 16);
1086                         DUMPBITS (bit_buf, bits, 16);
1087                         i += tab->run;
1088                         if (i < 64) goto normal_code;
1089                 }
1090                 break;  /* illegal, check needed to avoid buffer overflow */
1091         }
1092
1093         COPYBITS (bit_buf, bits, 4);    /* end of block code */
1094 }
1095
1096
1097 static int get_non_intra_block_drop (RunLevel *blk)
1098 {
1099 #define bit_buf (inbitbuf)
1100
1101     int i, li;
1102     int val;
1103     const DCTtab * tab;
1104         RunLevel *sblk = blk + 1;
1105
1106     li = i = -1;
1107
1108     if (bit_buf >= 0x28000000)
1109         {
1110                 tab = DCT_B14DC_5 + (UBITS (bit_buf, 5) - 5);
1111                 goto entry_1;
1112     }
1113         else goto entry_2;
1114
1115     while (1)
1116         {
1117                 if (bit_buf >= 0x28000000)
1118                 {
1119                         tab = DCT_B14AC_5 + (UBITS (bit_buf, 5) - 5);
1120         
1121         entry_1:
1122                         i += tab->run;
1123                         if (i >= 64) break;     /* end of block */
1124         
1125         normal_code:
1126         
1127                         DUMPBITS (bit_buf, bits, tab->len);
1128                         val = tab->level;
1129                         val = (val ^ SBITS (bit_buf, 1)) - SBITS (bit_buf, 1); /* if (bitstream_get (1)) val = -val; */
1130         
1131                         blk->level = val;
1132                         blk->run = i - li - 1;
1133                         li = i;
1134                         blk++;
1135         
1136                         DUMPBITS (bit_buf, bits, 1);
1137         
1138                         continue;
1139                 }
1140         
1141         entry_2:
1142         
1143                 if (bit_buf >= 0x04000000)
1144                 {
1145                         tab = DCT_B14_8 + (UBITS (bit_buf, 8) - 4);
1146         
1147                         i += tab->run;
1148                         if (i < 64) goto normal_code;
1149         
1150                         /* escape code */
1151         
1152                         i += (UBITS (bit_buf, 12) & 0x3F) - 64;
1153                         
1154                         if (i >= 64) break;     /* illegal, check needed to avoid buffer overflow */
1155         
1156                         DUMPBITS (bit_buf, bits, 12);
1157                         val = SBITS (bit_buf, 12);
1158
1159                         blk->level = val;
1160                         blk->run = i - li - 1;
1161                         li = i;
1162                         blk++;
1163                         
1164                         DUMPBITS (bit_buf, bits, 12);
1165         
1166                         continue;
1167                 }
1168                 else if (bit_buf >= 0x02000000)
1169                 {
1170                         tab = DCT_B14_10 + (UBITS (bit_buf, 10) - 8);
1171                         i += tab->run;
1172                         if (i < 64) goto normal_code;
1173                 }
1174                 else if (bit_buf >= 0x00800000)
1175                 {
1176                         tab = DCT_13 + (UBITS (bit_buf, 13) - 16);
1177                         i += tab->run;
1178                         if (i < 64) goto normal_code;
1179                 }
1180                 else if (bit_buf >= 0x00200000)
1181                 {
1182                         tab = DCT_15 + (UBITS (bit_buf, 15) - 16);
1183                         i += tab->run;
1184                         if (i < 64) goto normal_code;
1185                 }
1186                 else
1187                 {
1188                         tab = DCT_16 + UBITS (bit_buf, 16);
1189                         DUMPBITS (bit_buf, bits, 16);
1190                         i += tab->run;
1191                         if (i < 64) goto normal_code;
1192                 }
1193                 break;  /* illegal, check needed to avoid buffer overflow */
1194         }
1195     DUMPBITS (bit_buf, bits, 2);        /* dump end of block code */
1196         
1197         // remove last coeff
1198         if (blk != sblk) 
1199         {
1200                 blk--;
1201                 // remove more coeffs if very late
1202                 if ((quant_corr < -60.0f) && (blk != sblk))
1203                 {
1204                         blk--;
1205                         if ((quant_corr < -80.0f) && (blk != sblk))
1206                         {
1207                                 blk--;
1208                                 if ((quant_corr < -100.0f) && (blk != sblk))
1209                                 {
1210                                         blk--;
1211                                         if ((quant_corr < -120.0f) && (blk != sblk))
1212                                                 blk--;
1213                                 }
1214                         }
1215                 }
1216         }
1217
1218         blk->level = 0;
1219         
1220     return i;
1221 }
1222
1223 static int get_non_intra_block_rq (RunLevel *blk)
1224 {
1225 #define bit_buf (inbitbuf)
1226         int q = quantizer_scale, nq = new_quantizer_scale, tst;
1227     int i, li;
1228     int val;
1229     const DCTtab * tab;
1230
1231     /* Basic sanity check --Meuuh */
1232     if ( q == 0 )
1233         return 0;
1234
1235     tst = (nq / q) + ((nq % q) ? 1 : 0);
1236
1237     li = i = -1;
1238
1239     if (bit_buf >= 0x28000000)
1240         {
1241                 tab = DCT_B14DC_5 + (UBITS (bit_buf, 5) - 5);
1242                 goto entry_1;
1243     }
1244         else goto entry_2;
1245
1246     while (1)
1247         {
1248                 if (bit_buf >= 0x28000000)
1249                 {
1250                         tab = DCT_B14AC_5 + (UBITS (bit_buf, 5) - 5);
1251         
1252         entry_1:
1253                         i += tab->run;
1254                         if (i >= 64)
1255                         break;  /* end of block */
1256         
1257         normal_code:
1258         
1259                         DUMPBITS (bit_buf, bits, tab->len);
1260                         val = tab->level;
1261                         if (val >= tst)
1262                         {
1263                                 val = (val ^ SBITS (bit_buf, 1)) - SBITS (bit_buf, 1);
1264                                 blk->level = (val * q) / nq;
1265                                 blk->run = i - li - 1;
1266                                 li = i;
1267                                 blk++;
1268                         }
1269                         
1270                         //if ( ((val) && (tab->level < tst)) || ((!val) && (tab->level >= tst)) )
1271                         //      LOGF("level: %i val: %i tst : %i q: %i nq : %i\n", tab->level, val, tst, q, nq);
1272         
1273                         DUMPBITS (bit_buf, bits, 1);
1274         
1275                         continue;
1276                 }
1277         
1278         entry_2:
1279                 if (bit_buf >= 0x04000000)
1280                 {
1281                         tab = DCT_B14_8 + (UBITS (bit_buf, 8) - 4);
1282         
1283                         i += tab->run;
1284                         if (i < 64) goto normal_code;
1285         
1286                         /* escape code */
1287         
1288                         i += (UBITS (bit_buf, 12) & 0x3F) - 64;
1289                         
1290                         if (i >= 64) break;     /* illegal, check needed to avoid buffer overflow */
1291         
1292                         DUMPBITS (bit_buf, bits, 12);
1293                         val = SBITS (bit_buf, 12);
1294                         if (abs(val) >= tst)
1295                         {
1296                                 blk->level = (val * q) / nq;
1297                                 blk->run = i - li - 1;
1298                                 li = i;
1299                                 blk++;
1300                         }
1301                         
1302                         DUMPBITS (bit_buf, bits, 12);
1303         
1304                         continue;
1305                 }
1306                 else if (bit_buf >= 0x02000000)
1307                 {
1308                         tab = DCT_B14_10 + (UBITS (bit_buf, 10) - 8);
1309                         i += tab->run;
1310                         if (i < 64) goto normal_code;
1311                 }
1312                 else if (bit_buf >= 0x00800000)
1313                 {
1314                         tab = DCT_13 + (UBITS (bit_buf, 13) - 16);
1315                         i += tab->run;
1316                         if (i < 64) goto normal_code;
1317                 }
1318                 else if (bit_buf >= 0x00200000)
1319                 {
1320                         tab = DCT_15 + (UBITS (bit_buf, 15) - 16);
1321                         i += tab->run;
1322                         if (i < 64) goto normal_code;
1323                 }
1324                 else
1325                 {
1326                         tab = DCT_16 + UBITS (bit_buf, 16);
1327                         DUMPBITS (bit_buf, bits, 16);
1328                         
1329                         i += tab->run;
1330                         if (i < 64) goto normal_code;
1331                 }
1332                 break;  /* illegal, check needed to avoid buffer overflow */
1333         }
1334     DUMPBITS (bit_buf, bits, 2);        /* dump end of block code */
1335         
1336         blk->level = 0;
1337
1338     return i;
1339 }
1340
1341 static inline void slice_intra_DCT (const int cc)
1342 {
1343     if (cc == 0)        get_luma_dc_dct_diff ();
1344     else                        get_chroma_dc_dct_diff ();
1345
1346     if (intra_vlc_format) get_intra_block_B15 ();
1347     else get_intra_block_B14 ();
1348 }
1349
1350 static inline void slice_non_intra_DCT (int cur_block)
1351 {
1352         if (picture_coding_type == P_TYPE) get_non_intra_block_drop(block[cur_block]);
1353         else get_non_intra_block_rq(block[cur_block]);
1354 }
1355
1356 static void motion_fr_frame ( uint f_code[2] )
1357 {
1358         get_motion_delta (f_code[0]);
1359         get_motion_delta (f_code[1]);
1360 }
1361
1362 static void motion_fr_field ( uint f_code[2] )
1363 {
1364     COPYBITS (bit_buf, bits, 1);
1365
1366         get_motion_delta (f_code[0]);
1367         get_motion_delta (f_code[1]);
1368
1369     COPYBITS (bit_buf, bits, 1);
1370
1371         get_motion_delta (f_code[0]);
1372         get_motion_delta (f_code[1]);
1373 }
1374
1375 static void motion_fr_dmv ( uint f_code[2] )
1376 {
1377     get_motion_delta (f_code[0]);
1378         get_dmv ();
1379
1380         get_motion_delta (f_code[1]);
1381         get_dmv ();
1382 }
1383
1384 /* like motion_frame, but parsing without actual motion compensation */
1385 static void motion_fr_conceal ( )
1386 {
1387         get_motion_delta (f_code[0][0]);
1388         get_motion_delta (f_code[0][1]);
1389
1390     COPYBITS (bit_buf, bits, 1); /* remove marker_bit */
1391 }
1392
1393 static void motion_fi_field ( uint f_code[2] )
1394 {
1395     COPYBITS (bit_buf, bits, 1);
1396
1397         get_motion_delta (f_code[0]);
1398         get_motion_delta (f_code[1]);
1399 }
1400
1401 static void motion_fi_16x8 ( uint f_code[2] )
1402 {
1403     COPYBITS (bit_buf, bits, 1);
1404
1405         get_motion_delta (f_code[0]);
1406         get_motion_delta (f_code[1]);
1407
1408     COPYBITS (bit_buf, bits, 1);
1409
1410         get_motion_delta (f_code[0]);
1411         get_motion_delta (f_code[1]);
1412 }
1413
1414 static void motion_fi_dmv ( uint f_code[2] )
1415 {
1416         get_motion_delta (f_code[0]);
1417     get_dmv ();
1418
1419     get_motion_delta (f_code[1]);
1420         get_dmv ();
1421 }
1422
1423 static void motion_fi_conceal ()
1424 {
1425     COPYBITS (bit_buf, bits, 1); /* remove field_select */
1426
1427         get_motion_delta (f_code[0][0]);
1428         get_motion_delta (f_code[0][1]);
1429
1430     COPYBITS (bit_buf, bits, 1); /* remove marker_bit */
1431 }
1432
1433 #define MOTION_CALL(routine,direction)                                          \
1434 do {                                                                                                            \
1435     if ((direction) & MACROBLOCK_MOTION_FORWARD)                        \
1436                 routine (f_code[0]);                                                            \
1437     if ((direction) & MACROBLOCK_MOTION_BACKWARD)                       \
1438                 routine (f_code[1]);                                                            \
1439 } while (0)
1440
1441 #define NEXT_MACROBLOCK                                                                                 \
1442 do {                                                                                                                    \
1443     h_offset += 16;                                                                                             \
1444     if (h_offset == horizontal_size_value)                                              \
1445         {                                                                                                                       \
1446                 v_offset += 16;                                                                                 \
1447                 if (v_offset > (vertical_size_value - 16)) return;              \
1448                 h_offset = 0;                                                                                   \
1449     }                                                                                                                   \
1450 } while (0)
1451
1452 void putmbdata(int macroblock_modes)
1453 {
1454                 putmbtype(macroblock_modes & 0x1F);
1455                 
1456                 switch (picture_coding_type)
1457                 {
1458                         case I_TYPE:
1459                                 if ((! (frame_pred_frame_dct)) && (picture_structure == FRAME_PICTURE))
1460                                         putbits(macroblock_modes & DCT_TYPE_INTERLACED ? 1 : 0, 1);
1461                                 break;
1462                 
1463                         case P_TYPE:
1464                                 if (picture_structure != FRAME_PICTURE)
1465                                 {
1466                                         if (macroblock_modes & MACROBLOCK_MOTION_FORWARD)
1467                                                 putbits((macroblock_modes & MOTION_TYPE_MASK) / MOTION_TYPE_BASE, 2);
1468                                         break;
1469                                 }
1470                                 else if (frame_pred_frame_dct) break;
1471                                 else
1472                                 {
1473                                         if (macroblock_modes & MACROBLOCK_MOTION_FORWARD)
1474                                                 putbits((macroblock_modes & MOTION_TYPE_MASK) / MOTION_TYPE_BASE, 2);
1475                                         if (macroblock_modes & (MACROBLOCK_INTRA | MACROBLOCK_PATTERN))
1476                                                 putbits(macroblock_modes & DCT_TYPE_INTERLACED ? 1 : 0, 1);
1477                                         break;
1478                                 }
1479                 
1480                         case B_TYPE:
1481                                 if (picture_structure != FRAME_PICTURE)
1482                                 {
1483                                         if (! (macroblock_modes & MACROBLOCK_INTRA))
1484                                                 putbits((macroblock_modes & MOTION_TYPE_MASK) / MOTION_TYPE_BASE, 2);
1485                                         break;
1486                                 }
1487                                 else if (frame_pred_frame_dct) break;
1488                                 else
1489                                 {
1490                                         if (macroblock_modes & MACROBLOCK_INTRA) goto intra;
1491                                         putbits((macroblock_modes & MOTION_TYPE_MASK) / MOTION_TYPE_BASE, 2);
1492                                         if (macroblock_modes & (MACROBLOCK_INTRA | MACROBLOCK_PATTERN))
1493                                         {
1494                                                 intra:
1495                                                 putbits(macroblock_modes & DCT_TYPE_INTERLACED ? 1 : 0, 1);
1496                                         }
1497                                         break;
1498                                 }
1499                 }
1500
1501 }
1502
1503 static inline void put_quantiser(int quantiser)
1504 {
1505         putbits(q_scale_type ? map_non_linear_mquant[quantiser] : quantiser >> 1, 5);
1506         last_coded_scale = quantiser;
1507 }
1508
1509 static int slice_init (int code)
1510 {
1511 #define bit_buf (inbitbuf)
1512
1513     int offset;
1514     const MBAtab * mba;
1515
1516     v_offset = (code - 1) * 16;
1517
1518     quantizer_scale = get_quantizer_scale ();
1519         if (picture_coding_type == P_TYPE) new_quantizer_scale = quantizer_scale;
1520         else new_quantizer_scale = getNewQuant(quantizer_scale);
1521         put_quantiser(new_quantizer_scale);
1522         
1523         /*LOGF("************************\nstart of slice %i in %s picture. ori quant: %i new quant: %i\n", code,
1524                 (picture_coding_type == I_TYPE ? "I_TYPE" : (picture_coding_type == P_TYPE ? "P_TYPE" : "B_TYPE")),
1525                 quantizer_scale, new_quantizer_scale);*/
1526
1527     /* ignore intra_slice and all the extra data */
1528     while (bit_buf & 0x80000000)
1529         {
1530                 DUMPBITS (bit_buf, bits, 9);
1531     }
1532
1533     /* decode initial macroblock address increment */
1534     offset = 0;
1535     while (1)
1536         {
1537                 if (bit_buf >= 0x08000000)
1538                 {
1539                         mba = MBA_5 + (UBITS (bit_buf, 6) - 2);
1540                         break;
1541                 }
1542                 else if (bit_buf >= 0x01800000)
1543                 {
1544                         mba = MBA_11 + (UBITS (bit_buf, 12) - 24);
1545                         break;
1546                 }
1547                 else switch (UBITS (bit_buf, 12))
1548                 {
1549                         case 8:         /* macroblock_escape */
1550                                 offset += 33;
1551                                 COPYBITS (bit_buf, bits, 11);
1552                                 continue;
1553                         default:        /* error */
1554                                 return 1;
1555                 }
1556     }
1557
1558     COPYBITS (bit_buf, bits, mba->len + 1);
1559     h_offset = (offset + mba->mba) << 4;
1560
1561     while (h_offset - (int)horizontal_size_value >= 0)
1562         {
1563                 h_offset -= horizontal_size_value;
1564                 v_offset += 16;
1565     }
1566
1567     if (v_offset > (vertical_size_value - 16)) return 1;
1568
1569     return 0;
1570
1571 }
1572
1573 void mpeg2_slice ( const int code )
1574 {
1575 #define bit_buf (inbitbuf)
1576
1577     if (slice_init (code)) return;
1578
1579     while (1)
1580         {
1581                 int macroblock_modes;
1582                 int mba_inc;
1583                 const MBAtab * mba;
1584         
1585                 macroblock_modes = get_macroblock_modes ();
1586                 if (macroblock_modes & MACROBLOCK_QUANT) quantizer_scale = get_quantizer_scale ();
1587                 
1588                 //LOGF("blk %i : ", h_offset >> 4);
1589
1590                 if (macroblock_modes & MACROBLOCK_INTRA)
1591                 {
1592                         //LOG("intra "); if (macroblock_modes & MACROBLOCK_QUANT) LOGF("got new quant: %i ", quantizer_scale);
1593
1594                         new_quantizer_scale = increment_quant(quantizer_scale);
1595                         if (last_coded_scale == new_quantizer_scale) macroblock_modes &= 0xFFFFFFEF; // remove MACROBLOCK_QUANT
1596                         else macroblock_modes |= MACROBLOCK_QUANT; //add MACROBLOCK_QUANT
1597                         putmbdata(macroblock_modes);
1598                         if (macroblock_modes & MACROBLOCK_QUANT) put_quantiser(new_quantizer_scale);
1599                         
1600                         //if (macroblock_modes & MACROBLOCK_QUANT) LOGF("put new quant: %i ", new_quantizer_scale);
1601                 
1602                         if (concealment_motion_vectors)
1603                         {
1604                                 if (picture_structure == FRAME_PICTURE) motion_fr_conceal ();
1605                                 else motion_fi_conceal ();
1606                         }
1607                         
1608                         slice_intra_DCT ( 0);
1609                         slice_intra_DCT ( 0);
1610                         slice_intra_DCT ( 0);
1611                         slice_intra_DCT ( 0);
1612                         slice_intra_DCT ( 1);
1613                         slice_intra_DCT ( 2);
1614                 }
1615                 else
1616                 {
1617                         int new_coded_block_pattern = 0;
1618
1619                         // begin saving data
1620                         int batb;
1621                         uint8   n_owbuf[32], *n_wbuf,
1622                                         *o_owbuf = owbuf, *o_wbuf = wbuf;
1623                         uint32  n_outbitcnt, n_outbitbuf,
1624                                         o_outbitcnt = outbitcnt, o_outbitbuf = outbitbuf;
1625
1626                         outbitbuf = 0; outbitcnt = BITS_IN_BUF;
1627                         owbuf = wbuf = n_owbuf;
1628
1629                         if (picture_structure == FRAME_PICTURE)
1630                                 switch (macroblock_modes & MOTION_TYPE_MASK)
1631                                 {
1632                                         case MC_FRAME: MOTION_CALL (motion_fr_frame, macroblock_modes); break;
1633                                         case MC_FIELD: MOTION_CALL (motion_fr_field, macroblock_modes); break;
1634                                         case MC_DMV: MOTION_CALL (motion_fr_dmv, MACROBLOCK_MOTION_FORWARD); break;
1635                                 }
1636                         else
1637                                 switch (macroblock_modes & MOTION_TYPE_MASK)
1638                                 {
1639                                         case MC_FIELD: MOTION_CALL (motion_fi_field, macroblock_modes); break;
1640                                         case MC_16X8: MOTION_CALL (motion_fi_16x8, macroblock_modes); break;
1641                                         case MC_DMV: MOTION_CALL (motion_fi_dmv, MACROBLOCK_MOTION_FORWARD); break;
1642                                 }
1643                                 
1644                         assert(wbuf - owbuf < 32);
1645                         
1646                         n_wbuf = wbuf;
1647                         n_outbitcnt = outbitcnt;
1648                         n_outbitbuf = outbitbuf;
1649                         assert(owbuf == n_owbuf);
1650                         
1651                         outbitcnt = o_outbitcnt;
1652                         outbitbuf = o_outbitbuf;
1653                         owbuf = o_owbuf;
1654                         wbuf = o_wbuf;
1655                         // end saving data
1656                         
1657                         if (picture_coding_type == P_TYPE) new_quantizer_scale = quantizer_scale;
1658                         else new_quantizer_scale = getNewQuant(quantizer_scale);
1659                         
1660                         //LOG("non intra "); if (macroblock_modes & MACROBLOCK_QUANT) LOGF("got new quant: %i ", quantizer_scale);
1661         
1662                         if (macroblock_modes & MACROBLOCK_PATTERN)
1663                         {
1664                                 int coded_block_pattern = get_coded_block_pattern ();
1665                                 
1666                                 if (coded_block_pattern & 0x20) slice_non_intra_DCT(0);
1667                                 if (coded_block_pattern & 0x10) slice_non_intra_DCT(1);
1668                                 if (coded_block_pattern & 0x08) slice_non_intra_DCT(2);
1669                                 if (coded_block_pattern & 0x04) slice_non_intra_DCT(3);
1670                                 if (coded_block_pattern & 0x02) slice_non_intra_DCT(4);
1671                                 if (coded_block_pattern & 0x01) slice_non_intra_DCT(5);
1672                                 
1673                                 if (picture_coding_type == B_TYPE)
1674                                 {
1675                                         if (coded_block_pattern & 0x20) if (isNotEmpty(block[0])) new_coded_block_pattern |= 0x20;
1676                                         if (coded_block_pattern & 0x10) if (isNotEmpty(block[1])) new_coded_block_pattern |= 0x10;
1677                                         if (coded_block_pattern & 0x08) if (isNotEmpty(block[2])) new_coded_block_pattern |= 0x08;
1678                                         if (coded_block_pattern & 0x04) if (isNotEmpty(block[3])) new_coded_block_pattern |= 0x04;
1679                                         if (coded_block_pattern & 0x02) if (isNotEmpty(block[4])) new_coded_block_pattern |= 0x02;
1680                                         if (coded_block_pattern & 0x01) if (isNotEmpty(block[5])) new_coded_block_pattern |= 0x01;
1681                                         if (!new_coded_block_pattern) macroblock_modes &= 0xFFFFFFED; // remove MACROBLOCK_PATTERN and MACROBLOCK_QUANT flag
1682                                 }
1683                                 else new_coded_block_pattern = coded_block_pattern;
1684                         }
1685
1686                         if (last_coded_scale == new_quantizer_scale) macroblock_modes &= 0xFFFFFFEF; // remove MACROBLOCK_QUANT
1687                         else if (macroblock_modes & MACROBLOCK_PATTERN) macroblock_modes |= MACROBLOCK_QUANT; //add MACROBLOCK_QUANT
1688                         assert( (macroblock_modes & MACROBLOCK_PATTERN) || !(macroblock_modes & MACROBLOCK_QUANT) );
1689                         
1690                         putmbdata(macroblock_modes);
1691                         if (macroblock_modes & MACROBLOCK_QUANT) put_quantiser(new_quantizer_scale);
1692
1693                         //if (macroblock_modes & MACROBLOCK_PATTERN) LOG("coded ");
1694                         //if (macroblock_modes & MACROBLOCK_QUANT) LOGF("put new quant: %i ", new_quantizer_scale);
1695
1696                         // put saved motion data...
1697                         for (batb = 0; batb < (n_wbuf - n_owbuf); batb++) putbits(n_owbuf[batb], 8);
1698                         putbits(n_outbitbuf, BITS_IN_BUF - n_outbitcnt);
1699                         // end saved motion data...
1700                         
1701                         if (macroblock_modes & MACROBLOCK_PATTERN)
1702                         {
1703                                 putcbp(new_coded_block_pattern);
1704                                 
1705                                 if (new_coded_block_pattern & 0x20) putnonintrablk(block[0]);
1706                                 if (new_coded_block_pattern & 0x10) putnonintrablk(block[1]);
1707                                 if (new_coded_block_pattern & 0x08) putnonintrablk(block[2]);
1708                                 if (new_coded_block_pattern & 0x04) putnonintrablk(block[3]);
1709                                 if (new_coded_block_pattern & 0x02) putnonintrablk(block[4]);
1710                                 if (new_coded_block_pattern & 0x01) putnonintrablk(block[5]);
1711                         }
1712                 }
1713         
1714                 //LOGF("\n\to: %i c: %i n: %i\n", quantizer_scale, last_coded_scale, new_quantizer_scale);
1715         
1716                 NEXT_MACROBLOCK;
1717         
1718                 mba_inc = 0;
1719                 while (1)
1720                 {
1721                         if (bit_buf >= 0x10000000)
1722                         {
1723                                 mba = MBA_5 + (UBITS (bit_buf, 5) - 2);
1724                                 break;
1725                         }
1726                         else if (bit_buf >= 0x03000000)
1727                         {
1728                                 mba = MBA_11 + (UBITS (bit_buf, 11) - 24);
1729                                 break;
1730                         }
1731                         else
1732                                 switch (UBITS (bit_buf, 11))
1733                                 {
1734                                         case 8:         /* macroblock_escape */
1735                                                 mba_inc += 33;
1736                                                 COPYBITS (bit_buf, bits, 11);
1737                                                 continue;
1738                                         default:        /* end of slice, or error */
1739                                                 return;
1740                                 }
1741                 }
1742                 COPYBITS (bit_buf, bits, mba->len);
1743                 mba_inc += mba->mba;
1744         
1745                 if (mba_inc) do { NEXT_MACROBLOCK; } while (--mba_inc);
1746     }
1747
1748 }
1749
1750 /////---- end ext mpeg code
1751
1752 static int do_next_start_code(void)
1753 {
1754         uint8 ID;
1755
1756     // get start code
1757     LOCK(1)
1758     ID = cbuf[0];
1759     COPY(1)
1760
1761     if (ID == 0x00) // pic header
1762     {
1763         LOCK(4)
1764         picture_coding_type = (cbuf[1] >> 3) & 0x7;
1765         cbuf[1] |= 0x7; cbuf[2] = 0xFF; cbuf[3] |= 0xF8; // vbv_delay is now 0xFFFF
1766         COPY(4)
1767     }
1768     else if (ID == 0xB3) // seq header
1769     {
1770         LOCK(8)
1771         horizontal_size_value = (cbuf[0] << 4) | (cbuf[1] >> 4);
1772         vertical_size_value = ((cbuf[1] & 0xF) << 8) | cbuf[2];
1773         if (!horizontal_size_value || !vertical_size_value)
1774             return -1;
1775         COPY(8)
1776     }
1777     else if (ID == 0xB5) // extension
1778     {
1779         LOCK(1)
1780         if ((cbuf[0] >> 4) == 0x8) // pic coding ext
1781         {
1782             LOCK(5)
1783
1784             f_code[0][0] = (cbuf[0] & 0xF) - 1;
1785             f_code[0][1] = (cbuf[1] >> 4) - 1;
1786             f_code[1][0] = (cbuf[1] & 0xF) - 1;
1787             f_code[1][1] = (cbuf[2] >> 4) - 1;
1788             
1789             intra_dc_precision = (cbuf[2] >> 2) & 0x3;
1790             picture_structure = cbuf[2] & 0x3;
1791             frame_pred_frame_dct = (cbuf[3] >> 6) & 0x1;
1792             concealment_motion_vectors = (cbuf[3] >> 5) & 0x1;
1793             q_scale_type = (cbuf[3] >> 4) & 0x1;
1794             intra_vlc_format = (cbuf[3] >> 3) & 0x1;
1795             alternate_scan = (cbuf[3] >> 2) & 0x1;
1796             COPY(5)
1797         }
1798         else
1799         {
1800             COPY(1)
1801         }
1802     }
1803     else if (ID == 0xB8) // gop header
1804     {
1805         LOCK(4)
1806         COPY(4)
1807     }
1808     else if ((ID >= 0x01) && (ID <= 0xAF)) // slice
1809     {
1810         uint8 *outTemp = wbuf, *inTemp = cbuf;
1811     
1812         if      (               ((picture_coding_type == B_TYPE) && (quant_corr < 2.5f)) // don't recompress if we're in advance!
1813                 ||      ((picture_coding_type == P_TYPE) && (quant_corr < -2.5f))
1814                 ||      ((picture_coding_type == I_TYPE) && (quant_corr < -5.0f))
1815             )
1816         {
1817             uint8 *nsc = cbuf;
1818             int fsc = 0, toLock;
1819             
1820             if (!horizontal_size_value || !vertical_size_value)
1821                 return -1;
1822
1823             // lock all the slice
1824             while (!fsc)
1825             {
1826                 toLock = nsc - cbuf + 3;
1827                 LOCK(toLock)
1828
1829                 if ( (nsc[0] == 0) && (nsc[1] == 0) && (nsc[2] == 1) ) fsc = 1; // start code !
1830                 else nsc++; // continue search
1831             }
1832         
1833             // init bit buffer
1834             inbitbuf = 0; inbitcnt = 0;
1835             outbitbuf = 0; outbitcnt = BITS_IN_BUF;
1836             
1837             // get 32 bits
1838             Refill_bits();
1839             Refill_bits();
1840             Refill_bits();
1841             Refill_bits();
1842         
1843             // begin bit level recoding
1844             mpeg2_slice(ID);
1845             flush_read_buffer();
1846             flush_write_buffer();
1847             // end bit level recoding
1848
1849             /* Basic sanity checks --Meuuh */
1850             if (cbuf > rbuf || wbuf > rwbuf)
1851                 return -1;
1852             
1853             /*LOGF("type: %s code: %02i in : %6i out : %6i diff : %6i fact: %2.2f\n",
1854             (picture_coding_type == I_TYPE ? "I_TYPE" : (picture_coding_type == P_TYPE ? "P_TYPE" : "B_TYPE")),
1855             ID,  cbuf - inTemp, wbuf - outTemp, (wbuf - outTemp) - (cbuf - inTemp), (float)(cbuf - inTemp) / (float)(wbuf - outTemp));*/
1856             
1857             if (wbuf - outTemp > cbuf - inTemp) // yes that might happen, rarely
1858             {
1859                 /*LOGF("*** slice bigger than before !! (type: %s code: %i in : %i out : %i diff : %i)\n",
1860                 (picture_coding_type == I_TYPE ? "I_TYPE" : (picture_coding_type == P_TYPE ? "P_TYPE" : "B_TYPE")),
1861                 ID, cbuf - inTemp, wbuf - outTemp, (wbuf - outTemp) - (cbuf - inTemp));*/
1862             
1863                 // in this case, we'll just use the original slice !
1864                 memcpy(outTemp, inTemp, cbuf - inTemp);
1865                 wbuf = outTemp + (cbuf - inTemp);
1866                 
1867                 // adjust outbytecnt
1868                 outbytecnt -= (wbuf - outTemp) - (cbuf - inTemp);
1869             }
1870         }
1871     }
1872     return 0;
1873 }
1874
1875 static void process_frame( sout_stream_t *p_stream,
1876                 sout_stream_id_t *id, sout_buffer_t *in, sout_buffer_t **out )
1877 {
1878     uint8_t             found;
1879     sout_buffer_t       *p_out;
1880
1881     /* The output buffer can't be bigger than the input buffer. */
1882     p_out = sout_BufferNew( p_stream->p_sout, in->i_size );
1883
1884     p_out->i_length = in->i_length;
1885     p_out->i_dts    = in->i_dts;
1886     p_out->i_pts    = in->i_pts;
1887
1888     sout_BufferChain( out, p_out );
1889
1890     rwbuf = owbuf = wbuf = p_out->p_buffer;
1891     cbuf = rbuf = in->p_buffer;
1892     rbuf += in->i_size + 4;
1893     rwbuf += in->i_size;
1894     *(in->p_buffer + in->i_size) = 0;
1895     *(in->p_buffer + in->i_size + 1) = 0;
1896     *(in->p_buffer + in->i_size + 2) = 1;
1897     *(in->p_buffer + in->i_size + 3) = 0;
1898     inbytecnt += in->i_size;
1899
1900     for ( ; ; )
1901     {
1902         // get next start code prefix
1903         found = 0;
1904         while (!found)
1905         {
1906 #ifndef REMOVE_BYTE_STUFFING
1907             LOCK(3)
1908 #else
1909             LOCK(6)
1910             if          ( (cbuf[0] == 0) && (cbuf[1] == 0) && (cbuf[2] == 0) && (cbuf[3] == 0) && (cbuf[4] == 0) && (cbuf[5] == 0) ) { SEEKR(1) }
1911             else 
1912 #endif
1913             if ( (cbuf[0] == 0) && (cbuf[1] == 0) && (cbuf[2] == 1) ) found = 1; // start code !
1914             else { COPY(1) } // continue search
1915
1916             if (cbuf >= in->p_buffer + in->i_size)
1917                 break;
1918         }
1919         if (cbuf >= in->p_buffer + in->i_size)
1920             break;
1921         COPY(3)
1922         
1923         if (do_next_start_code() == -1)
1924             break;
1925
1926         quant_corr = (((inbytecnt - (rbuf - 4 - cbuf)) / fact_x) - (outbytecnt + (wbuf - owbuf))) / REACT_DELAY;
1927     }
1928
1929     outbytecnt += wbuf - owbuf;
1930     p_out->i_size = wbuf - owbuf;
1931 }
1932
1933 static int transrate_video_process( sout_stream_t *p_stream,
1934                sout_stream_id_t *id, sout_buffer_t *in, sout_buffer_t **out )
1935 {
1936     *out = NULL;
1937
1938     if ( in->i_flags & SOUT_BUFFER_FLAGS_GOP )
1939     {
1940         while ( id->p_current_buffer != NULL )
1941         {
1942             sout_buffer_t * p_next = id->p_current_buffer->p_next;
1943             if ( fact_x == 1.0 )
1944             {
1945                 outbytecnt += id->p_current_buffer->i_size;
1946                 id->p_current_buffer->p_next = NULL;
1947                 sout_BufferChain( out, id->p_current_buffer );
1948             }
1949             else
1950             {
1951                 process_frame( p_stream, id, id->p_current_buffer, out );
1952                 sout_BufferDelete(p_stream->p_sout, id->p_current_buffer);
1953             }
1954             id->p_current_buffer = p_next;
1955         }
1956             
1957         if ( id->i_next_gop_duration )
1958         {
1959             mtime_t i_bitrate = (mtime_t)id->i_next_gop_size * 8000
1960                                     / (id->i_next_gop_duration / 1000);
1961             static mtime_t i_old_bitrate = 0;
1962             static mtime_t i_old_duration = 0;
1963             if (i_old_bitrate)
1964             {
1965                 msg_Dbg(p_stream, "bitrate = %lld -> %lld", i_old_bitrate,
1966                         (mtime_t)outbytecnt * 8000 / (i_old_duration / 1000));
1967             }
1968             i_old_bitrate = i_bitrate;
1969             i_old_duration = id->i_next_gop_duration;
1970             if ( i_bitrate > p_sys->i_vbitrate )
1971             {
1972                 fact_x = (double)i_bitrate / p_sys->i_vbitrate;
1973             }
1974             else
1975             {
1976                 fact_x = 1.0;
1977             }
1978             msg_Dbg(p_stream, "new fact_x = %f", fact_x);
1979
1980             id->p_current_buffer = id->p_next_gop;
1981             id->p_next_gop = NULL;
1982             id->i_next_gop_duration = 0;
1983             id->i_next_gop_size = 0;
1984             inbytecnt = 0;
1985             outbytecnt  = 0;
1986         }
1987     }
1988
1989     /* Store the buffer for the next GOP. */
1990     sout_BufferChain( &id->p_next_gop, in );
1991     id->i_next_gop_duration += in->i_length;
1992     id->i_next_gop_size += in->i_size;
1993
1994     if ( id->p_current_buffer != NULL )
1995     {
1996         sout_buffer_t * p_next = id->p_current_buffer->p_next;
1997         if ( fact_x == 1.0 )
1998         {
1999             outbytecnt += id->p_current_buffer->i_size;
2000             id->p_current_buffer->p_next = NULL;
2001             sout_BufferChain( out, id->p_current_buffer );
2002         }
2003         else
2004         {
2005             process_frame( p_stream, id, id->p_current_buffer, out );
2006             sout_BufferDelete(p_stream->p_sout, id->p_current_buffer);
2007         }
2008         id->p_current_buffer = p_next;
2009     }
2010
2011     return VLC_SUCCESS;
2012 }
2013
2014 static int transrate_video_new( sout_stream_t *p_stream,
2015                                 sout_stream_id_t *id )
2016 {
2017     id->p_current_buffer = NULL;
2018     id->p_next_gop = NULL;
2019     id->i_next_gop_duration = 0;
2020     id->i_next_gop_size = 0;
2021
2022     p_sys = p_stream->p_sys;
2023
2024         inbytecnt = outbytecnt = 0;
2025
2026     quant_corr = 0.0;
2027     fact_x = 1.0;
2028
2029     return VLC_SUCCESS;
2030 }
2031
2032 static void transrate_video_close ( sout_stream_t *p_stream, sout_stream_id_t *id )
2033 {
2034 }