]> git.sesse.net Git - vlc/blob - modules/stream_out/transrate/frame.c
f7f5d79d1b2b911b0e9b4a003da6d410635bc148
[vlc] / modules / stream_out / transrate / frame.c
1 /*****************************************************************************
2  * frame.c: MPEG2 video transrating module
3  *****************************************************************************
4  * Copyright (C) 2003-2004 the VideoLAN team
5  * Copyright (C) 2003 Antoine Missout <antoine.missout@metakine.com>
6  * Copyright (C) 2000-2003 Michel Lespinasse <walken@zoy.org>
7  * Copyright (C) 1999-2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
8  * $Id$
9  *
10  * Authors: Christophe Massiot <massiot@via.ecp.fr>
11  *          Laurent Aimar <fenrir@via.ecp.fr>
12  *          Antoine Missout <antoine.missout@metakine.com>
13  *          Michel Lespinasse <walken@zoy.org>
14  *          Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
15  *
16  * This program is free software; you can redistribute it and/or modify
17  * it under the terms of the GNU General Public License as published by
18  * the Free Software Foundation; either version 2 of the License, or
19  * (at your option) any later version.
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  * GNU General Public License for more details.
25  *
26  * You should have received a copy of the GNU General Public License
27  * along with this program; if not, write to the Free Software
28  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
29  *****************************************************************************/
30
31 /*****************************************************************************
32  * Preamble
33  *****************************************************************************/
34 #define NDEBUG 1
35 #include <assert.h>
36 #include <math.h>
37
38 #include <vlc/vlc.h>
39 #include <vlc_sout.h>
40 #include <vlc_codec.h>
41
42 #include "transrate.h"
43
44 /****************************************************************************
45  * transrater, code from M2VRequantizer http://www.metakine.com/
46  ****************************************************************************/
47
48 // useful constants
49 enum
50 {
51     I_TYPE = 1,
52     P_TYPE = 2,
53     B_TYPE = 3
54 };
55
56
57 /////---- begin ext mpeg code
58
59 #include "putvlc.h"
60
61 #include "getvlc.h"
62
63 static const int non_linear_quantizer_scale [] =
64 {
65      0,  1,  2,  3,  4,  5,   6,   7,
66      8, 10, 12, 14, 16, 18,  20,  22,
67     24, 28, 32, 36, 40, 44,  48,  52,
68     56, 64, 72, 80, 88, 96, 104, 112
69 };
70
71 static inline int get_macroblock_modes( transrate_t *tr )
72 {
73     bs_transrate_t *bs = &tr->bs;
74
75     int macroblock_modes;
76     const MBtab * tab;
77
78     switch( tr->picture_coding_type )
79     {
80         case I_TYPE:
81
82             tab = MB_I + UBITS (bs->i_bit_in_cache, 1);
83             bs_flush( bs, tab->len );
84             macroblock_modes = tab->modes;
85
86             if ((!(tr->frame_pred_frame_dct)) && (tr->picture_structure == FRAME_PICTURE))
87             {
88                 macroblock_modes |= UBITS (bs->i_bit_in_cache, 1) * DCT_TYPE_INTERLACED;
89                 bs_flush( bs, 1 );
90             }
91
92             return macroblock_modes;
93
94         case P_TYPE:
95
96             tab = MB_P + UBITS (bs->i_bit_in_cache, 5);
97             bs_flush( bs, tab->len );
98             macroblock_modes = tab->modes;
99
100             if (tr->picture_structure != FRAME_PICTURE)
101             {
102                 if (macroblock_modes & MACROBLOCK_MOTION_FORWARD)
103                 {
104                     macroblock_modes |= UBITS (bs->i_bit_in_cache, 2) * MOTION_TYPE_BASE;
105                     bs_flush( bs, 2 );
106                 }
107                 return macroblock_modes;
108             }
109             else if (tr->frame_pred_frame_dct)
110             {
111                 if (macroblock_modes & MACROBLOCK_MOTION_FORWARD)
112                     macroblock_modes |= MC_FRAME;
113                 return macroblock_modes;
114             }
115             else
116             {
117                 if (macroblock_modes & MACROBLOCK_MOTION_FORWARD)
118                 {
119                     macroblock_modes |= UBITS (bs->i_bit_in_cache, 2) * MOTION_TYPE_BASE;
120                     bs_flush( bs, 2 );
121                 }
122                 if (macroblock_modes & (MACROBLOCK_INTRA | MACROBLOCK_PATTERN))
123                 {
124                     macroblock_modes |= UBITS (bs->i_bit_in_cache, 1) * DCT_TYPE_INTERLACED;
125                     bs_flush( bs, 1 );
126                 }
127                 return macroblock_modes;
128             }
129
130         case B_TYPE:
131
132             tab = MB_B + UBITS (bs->i_bit_in_cache, 6);
133             bs_flush( bs, tab->len );
134             macroblock_modes = tab->modes;
135
136             if( tr->picture_structure != FRAME_PICTURE)
137             {
138                 if (! (macroblock_modes & MACROBLOCK_INTRA))
139                 {
140                     macroblock_modes |= UBITS (bs->i_bit_in_cache, 2) * MOTION_TYPE_BASE;
141                     bs_flush( bs, 2 );
142                 }
143                 return macroblock_modes;
144             }
145             else if (tr->frame_pred_frame_dct)
146             {
147                 /* if (! (macroblock_modes & MACROBLOCK_INTRA)) */
148                 macroblock_modes |= MC_FRAME;
149                 return macroblock_modes;
150             }
151             else
152             {
153                 if (macroblock_modes & MACROBLOCK_INTRA) goto intra;
154                 macroblock_modes |= UBITS (bs->i_bit_in_cache, 2) * MOTION_TYPE_BASE;
155                 bs_flush( bs, 2 );
156                 if (macroblock_modes & (MACROBLOCK_INTRA | MACROBLOCK_PATTERN))
157                 {
158                     intra:
159                     macroblock_modes |= UBITS (bs->i_bit_in_cache, 1) * DCT_TYPE_INTERLACED;
160                     bs_flush( bs, 1 );
161                 }
162                 return macroblock_modes;
163             }
164
165         default:
166             return 0;
167     }
168
169 }
170
171 static inline int get_quantizer_scale( transrate_t *tr )
172 {
173     bs_transrate_t *bs = &tr->bs;
174
175     int quantizer_scale_code;
176
177     quantizer_scale_code = UBITS (bs->i_bit_in_cache, 5);
178     bs_flush( bs, 5 );
179
180     if( tr->q_scale_type )
181         return non_linear_quantizer_scale[quantizer_scale_code];
182     else
183         return quantizer_scale_code << 1;
184 }
185
186 static inline int get_motion_delta( bs_transrate_t *bs, const int f_code )
187 {
188     int delta;
189     int sign;
190     const MVtab * tab;
191
192     if (bs->i_bit_in_cache & 0x80000000)
193     {
194         bs_copy( bs, 1 );
195         return 0;
196     }
197     else if (bs->i_bit_in_cache >= 0x0c000000)
198     {
199
200         tab = MV_4 + UBITS (bs->i_bit_in_cache, 4);
201         delta = (tab->delta << f_code) + 1;
202         bs_copy( bs, tab->len);
203
204         sign = SBITS (bs->i_bit_in_cache, 1);
205         bs_copy( bs, 1 );
206
207         if (f_code)
208         {
209             delta += UBITS (bs->i_bit_in_cache, f_code);
210             bs_copy( bs, f_code);
211         }
212
213         return (delta ^ sign) - sign;
214     }
215     else
216     {
217
218         tab = MV_10 + UBITS (bs->i_bit_in_cache, 10);
219         delta = (tab->delta << f_code) + 1;
220         bs_copy( bs, tab->len);
221
222         sign = SBITS (bs->i_bit_in_cache, 1);
223         bs_copy( bs, 1);
224
225         if (f_code)
226         {
227             delta += UBITS (bs->i_bit_in_cache, f_code);
228             bs_copy( bs, f_code);
229         }
230
231         return (delta ^ sign) - sign;
232     }
233 }
234
235
236 static inline int get_dmv( bs_transrate_t *bs )
237 {
238     const DMVtab * tab;
239
240     tab = DMV_2 + UBITS (bs->i_bit_in_cache, 2);
241     bs_copy( bs, tab->len);
242     return tab->dmv;
243 }
244
245 static inline int get_coded_block_pattern( bs_transrate_t *bs )
246 {
247     const CBPtab * tab;
248
249     if (bs->i_bit_in_cache >= 0x20000000)
250     {
251         tab = CBP_7 + (UBITS (bs->i_bit_in_cache, 7) - 16);
252         bs_flush( bs, tab->len );
253         return tab->cbp;
254     }
255     else
256     {
257         tab = CBP_9 + UBITS (bs->i_bit_in_cache, 9);
258         bs_flush( bs, tab->len );
259         return tab->cbp;
260     }
261 }
262
263 static inline int get_luma_dc_dct_diff( bs_transrate_t *bs, uint32_t *bits, uint8_t *len )
264 {
265     const DCtab * tab;
266     int size;
267     int dc_diff;
268
269     if (bs->i_bit_in_cache < 0xf8000000)
270     {
271         tab = DC_lum_5 + UBITS (bs->i_bit_in_cache, 5);
272         size = tab->size;
273         if (size)
274         {
275             *bits = bs_read( bs, tab->len );
276             *len = tab->len;
277             //dc_diff = UBITS (bs->i_bit_in_cache, size) - UBITS (SBITS (~bs->i_bit_in_cache, 1), size);
278             dc_diff = UBITS (bs->i_bit_in_cache, size);
279             if (!(dc_diff >> (size - 1))) dc_diff = (dc_diff + 1) - (1 << size);
280             *bits <<= size;
281             *bits |= bs_read( bs, size );
282             *len += size;
283             return dc_diff;
284         }
285         else
286         {
287             *bits = bs_read( bs, 3 );
288             *len = 3;
289             return 0;
290         }
291     }
292     else
293     {
294         tab = DC_long + (UBITS (bs->i_bit_in_cache, 9) - 0x1e0);
295         size = tab->size;
296         *bits = bs_read( bs, tab->len );
297         *len = tab->len;
298         //dc_diff = UBITS (bs->i_bit_in_cache, size) - UBITS (SBITS (~bs->i_bit_in_cache, 1), size);
299         dc_diff = UBITS (bs->i_bit_in_cache, size);
300         if (!(dc_diff >> (size - 1))) dc_diff = (dc_diff + 1) - (1 << size);
301         *bits <<= size;
302         *bits |= bs_read( bs, size );
303         *len += size;
304         return dc_diff;
305     }
306 }
307
308 static inline int get_chroma_dc_dct_diff( bs_transrate_t *bs, uint32_t *bits, uint8_t *len )
309 {
310     const DCtab * tab;
311     int size;
312     int dc_diff;
313
314     if (bs->i_bit_in_cache < 0xf8000000)
315     {
316         tab = DC_chrom_5 + UBITS (bs->i_bit_in_cache, 5);
317         size = tab->size;
318         if (size)
319         {
320             *bits = bs_read( bs, tab->len );
321             *len = tab->len;
322             //dc_diff = UBITS (bs->i_bit_in_cache, size) - UBITS (SBITS (~bs->i_bit_in_cache, 1), size);
323             dc_diff = UBITS (bs->i_bit_in_cache, size);
324             if (!(dc_diff >> (size - 1))) dc_diff = (dc_diff + 1) - (1 << size);
325             *bits <<= size;
326             *bits |= bs_read( bs, size );
327             *len += size;
328             return dc_diff;
329         }
330         else
331         {
332             *bits = bs_read( bs, 2 );
333             *len = 2;
334             return 0;
335         }
336     }
337     else
338     {
339         tab = DC_long + (UBITS (bs->i_bit_in_cache, 10) - 0x3e0);
340         size = tab->size;
341         *bits = bs_read( bs, tab->len + 1 );
342         *len = tab->len + 1;
343         //dc_diff = UBITS (bs->i_bit_in_cache, size) - UBITS (SBITS (~bs->i_bit_in_cache, 1), size);
344         dc_diff = UBITS (bs->i_bit_in_cache, size);
345         if (!(dc_diff >> (size - 1))) dc_diff = (dc_diff + 1) - (1 << size);
346         *bits <<= size;
347         *bits |= bs_read( bs, size );
348         *len += size;
349         return dc_diff;
350     }
351 }
352
353 static void motion_fr_frame( bs_transrate_t *bs, unsigned int f_code[2] )
354 {
355     get_motion_delta( bs, f_code[0] );
356     get_motion_delta( bs, f_code[1] );
357 }
358
359 static void motion_fr_field( bs_transrate_t *bs, unsigned int f_code[2] )
360 {
361     bs_copy( bs, 1);
362
363     get_motion_delta( bs, f_code[0]);
364     get_motion_delta( bs, f_code[1]);
365
366     bs_copy( bs, 1);
367
368     get_motion_delta( bs, f_code[0]);
369     get_motion_delta( bs, f_code[1]);
370 }
371
372 static void motion_fr_dmv( bs_transrate_t *bs, unsigned int f_code[2] )
373 {
374     get_motion_delta( bs, f_code[0]);
375     get_dmv( bs );
376
377     get_motion_delta( bs, f_code[1]);
378     get_dmv( bs );
379 }
380
381 static void motion_fi_field( bs_transrate_t *bs, unsigned int f_code[2] )
382 {
383     bs_copy( bs, 1);
384
385     get_motion_delta( bs, f_code[0]);
386     get_motion_delta( bs, f_code[1]);
387 }
388
389 static void motion_fi_16x8( bs_transrate_t *bs, unsigned int f_code[2] )
390 {
391     bs_copy( bs, 1);
392
393     get_motion_delta( bs, f_code[0]);
394     get_motion_delta( bs, f_code[1]);
395
396     bs_copy( bs, 1);
397
398     get_motion_delta( bs, f_code[0]);
399     get_motion_delta( bs, f_code[1]);
400 }
401
402 static void motion_fi_dmv( bs_transrate_t *bs, unsigned int f_code[2] )
403 {
404     get_motion_delta( bs, f_code[0]);
405     get_dmv( bs );
406
407     get_motion_delta( bs, f_code[1]);
408     get_dmv( bs );
409 }
410
411
412 #define MOTION_CALL(routine,direction)                      \
413 do {                                                        \
414     if ((direction) & MACROBLOCK_MOTION_FORWARD)            \
415         routine( bs, tr->f_code[0]);                        \
416     if ((direction) & MACROBLOCK_MOTION_BACKWARD)           \
417         routine( bs, tr->f_code[1]);                        \
418 } while (0)
419
420 #define NEXT_MACROBLOCK                                         \
421 do {                                                            \
422     tr->h_offset += 16;                                         \
423     if( tr->h_offset == tr->horizontal_size_value)              \
424     {                                                           \
425         tr->v_offset += 16;                                         \
426         if (tr->v_offset > (tr->vertical_size_value - 16)) return;      \
427         tr->h_offset = 0;                                       \
428     }                                                           \
429 } while (0)
430
431 static void putmbdata( transrate_t *tr, int macroblock_modes )
432 {
433     bs_transrate_t *bs = &tr->bs;
434
435     bs_write( bs,
436               mbtypetab[tr->picture_coding_type-1][macroblock_modes&0x1F].code,
437               mbtypetab[tr->picture_coding_type-1][macroblock_modes&0x1F].len);
438
439     switch ( tr->picture_coding_type )
440     {
441         case I_TYPE:
442             if ((! (tr->frame_pred_frame_dct)) && (tr->picture_structure == FRAME_PICTURE))
443                 bs_write( bs, macroblock_modes & DCT_TYPE_INTERLACED ? 1 : 0, 1);
444             break;
445
446         case P_TYPE:
447             if (tr->picture_structure != FRAME_PICTURE)
448             {
449                 if (macroblock_modes & MACROBLOCK_MOTION_FORWARD)
450                     bs_write( bs, (macroblock_modes & MOTION_TYPE_MASK) / MOTION_TYPE_BASE, 2);
451                 break;
452             }
453             else if (tr->frame_pred_frame_dct) break;
454             else
455             {
456                 if (macroblock_modes & MACROBLOCK_MOTION_FORWARD)
457                     bs_write( bs, (macroblock_modes & MOTION_TYPE_MASK) / MOTION_TYPE_BASE, 2);
458                 if (macroblock_modes & (MACROBLOCK_INTRA | MACROBLOCK_PATTERN))
459                     bs_write( bs, macroblock_modes & DCT_TYPE_INTERLACED ? 1 : 0, 1);
460                 break;
461             }
462
463         case B_TYPE:
464             if (tr->picture_structure != FRAME_PICTURE)
465             {
466                 if (! (macroblock_modes & MACROBLOCK_INTRA))
467                     bs_write( bs, (macroblock_modes & MOTION_TYPE_MASK) / MOTION_TYPE_BASE, 2);
468                 break;
469             }
470             else if (tr->frame_pred_frame_dct) break;
471             else
472             {
473                 if (macroblock_modes & MACROBLOCK_INTRA) goto intra;
474                 bs_write( bs, (macroblock_modes & MOTION_TYPE_MASK) / MOTION_TYPE_BASE, 2);
475                 if (macroblock_modes & (MACROBLOCK_INTRA | MACROBLOCK_PATTERN))
476                 {
477                     intra:
478                     bs_write( bs, macroblock_modes & DCT_TYPE_INTERLACED ? 1 : 0, 1);
479                 }
480                 break;
481             }
482     }
483 }
484
485 static const uint8_t map_non_linear_mquant[113] =
486 {
487     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,
488     16,17,17,17,18,18,18,18,19,19,19,19,20,20,20,20,21,21,21,21,22,22,
489     22,22,23,23,23,23,24,24,24,24,24,24,24,25,25,25,25,25,25,25,26,26,
490     26,26,26,26,26,26,27,27,27,27,27,27,27,27,28,28,28,28,28,28,28,29,
491     29,29,29,29,29,29,29,29,29,30,30,30,30,30,30,30,31,31,31,31,31
492 };
493 static inline void put_quantiser( transrate_t *tr )
494 {
495     bs_transrate_t *bs = &tr->bs;
496
497     bs_write( bs, tr->q_scale_type ? map_non_linear_mquant[tr->new_quantizer_scale] : tr->new_quantizer_scale >> 1, 5 );
498     tr->last_coded_scale = tr->new_quantizer_scale;
499 }
500
501 /* generate variable length code for macroblock_address_increment (6.3.16) */
502 static inline void putaddrinc( transrate_t *tr, int addrinc )
503 {
504     bs_transrate_t *bs = &tr->bs;
505
506     while ( addrinc >= 33 )
507     {
508         bs_write( bs, 0x08, 11 ); /* macroblock_escape */
509         addrinc -= 33;
510     }
511
512     bs_write( bs, addrinctab[addrinc].code, addrinctab[addrinc].len );
513 }
514
515 static int slice_init( transrate_t *tr,  int code )
516 {
517     bs_transrate_t *bs = &tr->bs;
518     int offset;
519     const MBAtab * mba;
520
521     tr->v_offset = (code - 1) * 16;
522
523     tr->quantizer_scale = get_quantizer_scale( tr );
524     if ( tr->new_quantizer_scale < tr->quantizer_scale )
525         tr->new_quantizer_scale = scale_quant( tr, tr->qrate );
526
527     /*LOGF("************************\nstart of slice %i in %s picture. ori quant: %i new quant: %i\n", code,
528         (picture_coding_type == I_TYPE ? "I_TYPE" : (picture_coding_type == P_TYPE ? "P_TYPE" : "B_TYPE")),
529         quantizer_scale, new_quantizer_scale);*/
530
531     /* ignore intra_slice and all the extra data */
532     while (bs->i_bit_in_cache & 0x80000000)
533     {
534         bs_flush( bs, 9 );
535     }
536
537     /* decode initial macroblock address increment */
538     offset = 0;
539     for( ;; )
540     {
541         if (bs->i_bit_in_cache >= 0x08000000)
542         {
543             mba = MBA_5 + (UBITS (bs->i_bit_in_cache, 6) - 2);
544             break;
545         }
546         else if (bs->i_bit_in_cache >= 0x01800000)
547         {
548             mba = MBA_11 + (UBITS (bs->i_bit_in_cache, 12) - 24);
549             break;
550         }
551         else if( UBITS (bs->i_bit_in_cache, 12 ) == 8 )
552         {
553             /* macroblock_escape */
554             offset += 33;
555             bs_flush(bs, 11);
556         }
557         else
558         {
559             return -1;
560         }
561     }
562
563     bs_flush(bs, mba->len + 1);
564     tr->h_offset = (offset + mba->mba) << 4;
565
566     while( tr->h_offset - (int)tr->horizontal_size_value >= 0)
567     {
568         tr->h_offset -= tr->horizontal_size_value;
569         tr->v_offset += 16;
570     }
571
572     if( tr->v_offset > tr->vertical_size_value - 16 )
573     {
574         return -1;
575     }
576     return (offset + mba->mba);
577 }
578
579 static void mpeg2_slice( transrate_t *tr, const int code )
580 {
581     bs_transrate_t *bs = &tr->bs;
582     int mba_inc;
583     int first_in_slice = 1;
584
585     if( (mba_inc = slice_init( tr, code )) < 0 )
586     {
587         return;
588     }
589
590     for( ;; )
591     {
592         const MBAtab * mba;
593         int macroblock_modes;
594         int mba_local;
595         int i;
596
597         while (unlikely(bs->i_bit_in < 24)) bs_refill( bs );
598
599         macroblock_modes = get_macroblock_modes( tr );
600         if (macroblock_modes & MACROBLOCK_QUANT)
601             tr->quantizer_scale = get_quantizer_scale( tr );
602         if (tr->new_quantizer_scale < tr->quantizer_scale)
603             tr->new_quantizer_scale = scale_quant( tr, tr->qrate );
604
605         //LOGF("blk %i : ", h_offset >> 4);
606
607         if (macroblock_modes & MACROBLOCK_INTRA)
608         {
609             RunLevel block[6][65]; // terminated by level = 0, so we need 64+1
610             RunLevel new_block[6][65]; // terminated by level = 0, so we need 64+1
611             uint32_t dc[6];
612             uint8_t  dc_len[6];
613
614             // begin saving data
615             int batb;
616             uint8_t   p_n_ow[32], *p_n_w,
617                     *p_o_ow = bs->p_ow, *p_o_w = bs->p_w;
618             uint32_t  i_n_bit_out, i_n_bit_out_cache,
619                     i_o_bit_out  = bs->i_bit_out, i_o_bit_out_cache = bs->i_bit_out_cache;
620
621             bs->i_bit_out_cache = 0; bs->i_bit_out = BITS_IN_BUF;
622             bs->p_ow = bs->p_w = p_n_ow;
623
624             //LOG("intra "); if (macroblock_modes & MACROBLOCK_QUANT) LOGF("got new quant: %i ", quantizer_scale);
625
626             if (tr->concealment_motion_vectors)
627             {
628                 if (tr->picture_structure != FRAME_PICTURE)
629                 {
630                     bs_copy(bs, 1); /* remove field_select */
631                 }
632                 /* like motion_frame, but parsing without actual motion compensation */
633                 get_motion_delta(bs, tr->f_code[0][0]);
634                 get_motion_delta(bs, tr->f_code[0][1]);
635
636                 bs_copy(bs, 1); /* remove marker_bit */
637             }
638
639             assert(bs->p_w - bs->p_ow < 32);
640
641             p_n_w = bs->p_w;
642             i_n_bit_out = bs->i_bit_out;
643             i_n_bit_out_cache = bs->i_bit_out_cache;
644             assert(bs->p_ow == p_n_ow);
645
646             bs->i_bit_out = i_o_bit_out ;
647             bs->i_bit_out_cache = i_o_bit_out_cache;
648             bs->p_ow = p_o_ow;
649             bs->p_w = p_o_w;
650             // end saving data
651
652             if( tr->intra_vlc_format )
653             {
654                 /* Luma */
655                 for ( i = 0; i < 4; i++ )
656                 {
657                     get_luma_dc_dct_diff( bs, dc + i, dc_len + i );
658                     get_intra_block_B15( tr, block[i] );
659                     if (tr->b_error) return;
660                 }
661                 /* Chroma */
662                 for ( ; i < 6; i++ )
663                 {
664                     get_chroma_dc_dct_diff( bs, dc + i, dc_len + i );
665                     get_intra_block_B15( tr, block[i] );
666                     if (tr->b_error) return;
667                 }
668             }
669             else
670             {
671                 /* Luma */
672                 for ( i = 0; i < 4; i++ )
673                 {
674                     get_luma_dc_dct_diff( bs, dc + i, dc_len + i );
675                     get_intra_block_B14( tr, block[i] );
676                     if (tr->b_error) return;
677                 }
678                 /* Chroma */
679                 for ( ; i < 6; i++ )
680                 {
681                     get_chroma_dc_dct_diff( bs, dc + i, dc_len + i );
682                     get_intra_block_B14( tr, block[i] );
683                     if (tr->b_error) return;
684                 }
685             }
686
687             transrate_mb( tr, block, new_block, 0x3f, 1 );
688
689             if (tr->last_coded_scale == tr->new_quantizer_scale)
690                 macroblock_modes &= ~MACROBLOCK_QUANT;
691
692             if ( first_in_slice )
693             {
694                 put_quantiser( tr );
695                 bs_write( bs, 0, 1 );
696                 macroblock_modes &= ~MACROBLOCK_QUANT;
697             }
698             putaddrinc( tr, mba_inc );
699             mba_inc = 0;
700             putmbdata( tr, macroblock_modes );
701             if( macroblock_modes & MACROBLOCK_QUANT )
702             {
703                 put_quantiser( tr );
704             }
705
706             // put saved motion data...
707             for (batb = 0; batb < (p_n_w - p_n_ow); batb++)
708             {
709                 bs_write( bs, p_n_ow[batb], 8 );
710             }
711             bs_write( bs, i_n_bit_out_cache, BITS_IN_BUF - i_n_bit_out );
712             // end saved motion data...
713
714             for ( i = 0; i < 6; i++ )
715             {
716                 bs_write( bs, *(dc + i), *(dc_len + i) );
717                 putintrablk( bs, new_block[i], tr->intra_vlc_format );
718             }
719     
720         }
721         else
722         {
723             RunLevel block[6][65]; // terminated by level = 0, so we need 64+1
724             RunLevel new_block[6][65]; // terminated by level = 0, so we need 64+1
725             int new_coded_block_pattern = 0;
726             int cbp = 0;
727
728             // begin saving data
729             int batb;
730             uint8_t   p_n_ow[32], *p_n_w,
731                     *p_o_ow = bs->p_ow, *p_o_w = bs->p_w;
732             uint32_t  i_n_bit_out, i_n_bit_out_cache,
733                     i_o_bit_out  = bs->i_bit_out, i_o_bit_out_cache = bs->i_bit_out_cache;
734
735             bs->i_bit_out_cache = 0; bs->i_bit_out = BITS_IN_BUF;
736             bs->p_ow = bs->p_w = p_n_ow;
737
738             if (tr->picture_structure == FRAME_PICTURE)
739                 switch (macroblock_modes & MOTION_TYPE_MASK)
740                 {
741                     case MC_FRAME: MOTION_CALL (motion_fr_frame, macroblock_modes); break;
742                     case MC_FIELD: MOTION_CALL (motion_fr_field, macroblock_modes); break;
743                     case MC_DMV: MOTION_CALL (motion_fr_dmv, MACROBLOCK_MOTION_FORWARD); break;
744                 }
745             else
746                 switch (macroblock_modes & MOTION_TYPE_MASK)
747                 {
748                     case MC_FIELD: MOTION_CALL (motion_fi_field, macroblock_modes); break;
749                     case MC_16X8: MOTION_CALL (motion_fi_16x8, macroblock_modes); break;
750                     case MC_DMV: MOTION_CALL (motion_fi_dmv, MACROBLOCK_MOTION_FORWARD); break;
751                 }
752
753             //LOG("non intra "); if (macroblock_modes & MACROBLOCK_QUANT) LOGF("got new quant: %i ", quantizer_scale);
754
755             if (macroblock_modes & MACROBLOCK_PATTERN)
756             {
757                 int last_in_slice;
758
759                 cbp = get_coded_block_pattern( bs );
760
761                 for ( i = 0; i < 6; i++ )
762                 {
763                     if ( cbp & (1 << (5 - i)) )
764                     {
765                         get_non_intra_block( tr, block[i] );
766                         if (tr->b_error) return;
767                     }
768                 }
769                 last_in_slice = !UBITS( bs->i_bit_in_cache, 11 );
770
771                 new_coded_block_pattern = transrate_mb( tr, block, new_block,
772                                                         cbp, 0 );
773
774                 if ( !new_coded_block_pattern &&
775                         !(macroblock_modes
776                             & (MACROBLOCK_MOTION_FORWARD
777                                 | MACROBLOCK_MOTION_BACKWARD))
778                         && (first_in_slice || last_in_slice) )
779                 {
780                     /* First mb in slice, just code a 0-mv mb.
781                      * This is wrong for last in slice, but it only shows
782                      * a few artefacts. */
783                     macroblock_modes |= MACROBLOCK_MOTION_FORWARD;
784                     if (tr->picture_structure == FRAME_PICTURE)
785                     {
786                         macroblock_modes |= MC_FRAME;
787                         bs_write( bs, 0x3, 2 ); /* motion vectors */
788                     }
789                     else
790                     {
791                         macroblock_modes |= MC_FIELD;
792                         bs_write( bs,
793                              (tr->picture_structure == BOTTOM_FIELD ? 1 : 0),
794                              1); /* motion field select */
795                         bs_write( bs, 0x3, 2 ); /* motion vectors */
796                     }
797                 }
798
799                 if ( !new_coded_block_pattern )
800                 {
801                     macroblock_modes &= ~MACROBLOCK_PATTERN;
802                     macroblock_modes &= ~MACROBLOCK_QUANT;
803                 }
804                 else
805                 {
806                     if ( tr->last_coded_scale == tr->new_quantizer_scale )
807                     {
808                         macroblock_modes &= ~MACROBLOCK_QUANT;
809                     }
810                     else
811                     {
812                         macroblock_modes |= MACROBLOCK_QUANT;
813                     }
814                 }
815             }
816
817             assert(bs->p_w - bs->p_ow < 32);
818
819             p_n_w = bs->p_w;
820             i_n_bit_out = bs->i_bit_out;
821             i_n_bit_out_cache = bs->i_bit_out_cache;
822             assert(bs->p_ow == p_n_ow);
823
824             bs->i_bit_out = i_o_bit_out ;
825             bs->i_bit_out_cache = i_o_bit_out_cache;
826             bs->p_ow = p_o_ow;
827             bs->p_w = p_o_w;
828             // end saving data
829
830             if ( macroblock_modes &
831                     (MACROBLOCK_MOTION_FORWARD | MACROBLOCK_MOTION_BACKWARD
832                       | MACROBLOCK_PATTERN) )
833             {
834                 if ( first_in_slice )
835                 {
836                     put_quantiser( tr );
837                     bs_write( bs, 0, 1 );
838                     macroblock_modes &= ~MACROBLOCK_QUANT;
839                 }
840                 putaddrinc( tr, mba_inc );
841                 mba_inc = 0;
842                 putmbdata( tr, macroblock_modes );
843                 if ( macroblock_modes & MACROBLOCK_QUANT )
844                 {
845                     put_quantiser( tr );
846                 }
847
848                 // put saved motion data...
849                 for (batb = 0; batb < (p_n_w - p_n_ow); batb++)
850                 {
851                     bs_write( bs, p_n_ow[batb], 8 );
852                 }
853                 bs_write( bs, i_n_bit_out_cache, BITS_IN_BUF - i_n_bit_out);
854                 // end saved motion data...
855
856                 if (macroblock_modes & MACROBLOCK_PATTERN)
857                 {
858                     /* Write CBP */
859                     bs_write( bs, cbptable[new_coded_block_pattern].code,
860                               cbptable[new_coded_block_pattern].len );
861
862                     for ( i = 0; i < 6; i++ )
863                     {
864                         if ( new_coded_block_pattern & (1 << (5 - i)) )
865                         {
866                             putnonintrablk( bs, new_block[i] );
867                         }
868                     }
869                 }
870             }
871             else
872             {
873                 /* skipped macroblock */
874                 mba_inc++;
875             }
876         }
877
878         if (bs->p_c > bs->p_r || bs->p_w > bs->p_rw)
879         {
880             tr->b_error = 1;
881             return;
882         }
883         //LOGF("\n\to: %i c: %i n: %i\n", quantizer_scale, last_coded_scale, new_quantizer_scale);
884
885         NEXT_MACROBLOCK;
886
887         first_in_slice = 0;
888         mba_local = 0;
889         for ( ; ; )
890         {
891             if ( bs->i_bit_in_cache >= 0x10000000 )
892             {
893                 mba = MBA_5 + (UBITS (bs->i_bit_in_cache, 5) - 2);
894                 break;
895             }
896             else if ( bs->i_bit_in_cache >= 0x03000000 )
897             {
898                 mba = MBA_11 + (UBITS (bs->i_bit_in_cache, 11) - 24);
899                 break;
900             }
901             else if ( UBITS( bs->i_bit_in_cache, 11 ) == 8 )
902             {
903                 /* macroblock_escape */
904                 mba_inc += 33;
905                 mba_local += 33;
906                 bs_flush(bs, 11);
907             }
908             else
909             {
910                 /* EOS or error */
911                 return;
912             }
913         }
914         bs_flush(bs, mba->len);
915         mba_inc += mba->mba;
916         mba_local += mba->mba;
917
918         while( mba_local-- )
919         {
920             NEXT_MACROBLOCK;
921         }
922     }
923 }
924
925 static const uint8_t mpeg2_scan_norm[64] ATTR_ALIGN(16) = {
926     /* Zig-Zag scan pattern */
927      0,  1,  8, 16,  9,  2,  3, 10, 17, 24, 32, 25, 18, 11,  4,  5,
928     12, 19, 26, 33, 40, 48, 41, 34, 27, 20, 13,  6,  7, 14, 21, 28,
929     35, 42, 49, 56, 57, 50, 43, 36, 29, 22, 15, 23, 30, 37, 44, 51,
930     58, 59, 52, 45, 38, 31, 39, 46, 53, 60, 61, 54, 47, 55, 62, 63
931 };
932
933 static const uint8_t mpeg2_scan_alt[64] ATTR_ALIGN(16) = {
934     /* Alternate scan pattern */
935      0, 8,  16, 24,  1,  9,  2, 10, 17, 25, 32, 40, 48, 56, 57, 49,
936     41, 33, 26, 18,  3, 11,  4, 12, 19, 27, 34, 42, 50, 58, 35, 43,
937     51, 59, 20, 28,  5, 13,  6, 14, 21, 29, 36, 44, 52, 60, 37, 45,
938     53, 61, 22, 30,  7, 15, 23, 31, 38, 46, 54, 62, 39, 47, 55, 63
939 };
940
941 static const int16_t default_intra_matrix[64] = {
942         8, 16, 19, 22, 26, 27, 29, 34,
943         16, 16, 22, 24, 27, 29, 34, 37,
944         19, 22, 26, 27, 29, 34, 34, 38,
945         22, 22, 26, 27, 29, 34, 37, 40,
946         22, 26, 27, 29, 32, 35, 40, 48,
947         26, 27, 29, 32, 35, 40, 48, 58,
948         26, 27, 29, 34, 38, 46, 56, 69,
949         27, 29, 35, 38, 46, 56, 69, 83
950 };
951
952 static int mpeg2_header_sequence( transrate_t * tr )
953 {
954     bs_transrate_t *bs = &tr->bs;
955     int has_intra = 0, has_non_intra = 0;
956     int i;
957
958     i = (bs->p_c[0] << 16) | (bs->p_c[1] << 8) | bs->p_c[2];
959     tr->horizontal_size_value = i >> 12;
960     tr->vertical_size_value = i & 0xfff;
961     tr->horizontal_size_value = (tr->horizontal_size_value + 15) & ~15;
962     tr->vertical_size_value = (tr->vertical_size_value + 15) & ~15;
963     if ( !tr->horizontal_size_value || !tr->vertical_size_value )
964     {
965         return -1;
966     }
967
968     if ( tr->mpeg4_matrix )
969     {
970         if (bs->p_c[7] & 2)
971         {
972             has_intra = 1;
973             for (i = 0; i < 64; i++)
974                 tr->intra_quantizer_matrix[mpeg2_scan_norm[i]] =
975                 (bs->p_c[i+7] << 7) | (bs->p_c[i+8] >> 1);
976         }
977         else
978         {
979             for (i = 0; i < 64; i++)
980                 tr->intra_quantizer_matrix[mpeg2_scan_norm[i]] =
981                 default_intra_matrix[i];
982         }
983
984         if (bs->p_c[7+64] & 1)
985         {
986             has_non_intra = 1;
987             for (i = 0; i < 64; i++)
988                 tr->non_intra_quantizer_matrix[mpeg2_scan_norm[i]] =
989                 bs->p_c[i+8+64];
990         }
991         else
992         {
993             for (i = 0; i < 64; i++)
994                 tr->non_intra_quantizer_matrix[i] = 16;
995         }
996     }
997
998     /* Write quantization matrices */
999     memcpy( bs->p_w, bs->p_c, 8 );
1000     bs->p_c += 8;
1001
1002     if ( tr->mpeg4_matrix )
1003     {
1004         memset( &bs->p_w[8], 0, 128 );
1005         bs->p_w[7] |= 2;
1006         bs->p_w[7] &= ~1;
1007         for (i = 0; i < 64; i++)
1008         {
1009             bs->p_w[i+7] |= mpeg4_default_intra_matrix[mpeg2_scan_norm[i]] >> 7;
1010             bs->p_w[i+8] |= mpeg4_default_intra_matrix[mpeg2_scan_norm[i]] << 1;
1011         }
1012
1013         bs->p_w[7+64] |= 1;
1014         for (i = 0; i < 64; i++)
1015         {
1016             bs->p_w[i+8+64] |= mpeg4_default_intra_matrix[mpeg2_scan_norm[i]];
1017         }
1018         bs->p_w += 8 + 128;
1019         bs->p_c += (has_intra + has_non_intra) * 64;
1020     }
1021     else
1022     {
1023         bs->p_w += 8;
1024     }
1025
1026     tr->scan = mpeg2_scan_norm;
1027
1028     return 0;
1029 }
1030
1031 /////---- end ext mpeg code
1032
1033 static int do_next_start_code( transrate_t *tr )
1034 {
1035     bs_transrate_t *bs = &tr->bs;
1036     uint8_t ID;
1037
1038     // get start code
1039     ID = bs->p_c[0];
1040
1041     /* Copy one byte */
1042     *bs->p_w++ = *bs->p_c++;
1043
1044     if (ID == 0x00) // pic header
1045     {
1046         tr->picture_coding_type = (bs->p_c[1] >> 3) & 0x7;
1047         bs->p_c[1] |= 0x7; bs->p_c[2] = 0xFF; bs->p_c[3] |= 0xF8; // vbv_delay is now 0xFFFF
1048
1049         memcpy(bs->p_w, bs->p_c, 4);
1050         bs->p_c += 4;
1051         bs->p_w += 4;
1052     }
1053     else if (ID == 0xB3) // seq header
1054     {
1055         mpeg2_header_sequence(tr);
1056     }
1057     else if (ID == 0xB5) // extension
1058     {
1059         if ((bs->p_c[0] >> 4) == 0x8) // pic coding ext
1060         {
1061             tr->f_code[0][0] = (bs->p_c[0] & 0xF) - 1;
1062             tr->f_code[0][1] = (bs->p_c[1] >> 4) - 1;
1063             tr->f_code[1][0] = (bs->p_c[1] & 0xF) - 1;
1064             tr->f_code[1][1] = (bs->p_c[2] >> 4) - 1;
1065
1066             /* tr->intra_dc_precision = (bs->p_c[2] >> 2) & 0x3; */
1067             tr->picture_structure = bs->p_c[2] & 0x3;
1068             tr->frame_pred_frame_dct = (bs->p_c[3] >> 6) & 0x1;
1069             tr->concealment_motion_vectors = (bs->p_c[3] >> 5) & 0x1;
1070             tr->q_scale_type = (bs->p_c[3] >> 4) & 0x1;
1071             tr->intra_vlc_format = (bs->p_c[3] >> 3) & 0x1;
1072             if ( (bs->p_c[3] >> 2) & 0x1 )
1073                 tr->scan = mpeg2_scan_alt;
1074
1075             memcpy(bs->p_w, bs->p_c, 5);
1076             bs->p_c += 5;
1077             bs->p_w += 5;
1078         }
1079         else
1080         {
1081             *bs->p_w++ = *bs->p_c++;
1082         }
1083     }
1084     else if (ID == 0xB8) // gop header
1085     {
1086         memcpy(bs->p_w, bs->p_c, 4);
1087         bs->p_c += 4;
1088         bs->p_w += 4;
1089     }
1090     else if ((ID >= 0x01) && (ID <= 0xAF)) // slice
1091     {
1092         uint8_t *outTemp = bs->p_w, *inTemp = bs->p_c;
1093
1094         if( tr->qrate != 1.0 )
1095         {
1096             if( !tr->horizontal_size_value || !tr->vertical_size_value )
1097             {
1098                 return -1;
1099             }
1100
1101             // init bit buffer
1102             bs->i_bit_in_cache = 0; bs->i_bit_in = 0;
1103             bs->i_bit_out_cache = 0; bs->i_bit_out = BITS_IN_BUF;
1104
1105             // get 32 bits
1106             bs_refill( bs );
1107             bs_refill( bs );
1108             bs_refill( bs );
1109             bs_refill( bs );
1110
1111             // begin bit level recoding
1112             mpeg2_slice(tr, ID);
1113             if (tr->b_error) return -1;
1114
1115             bs_flush_read( bs );
1116             bs_flush_write( bs );
1117             // end bit level recoding
1118
1119             /* Basic sanity checks --Meuuh */
1120             if (bs->p_c > bs->p_r || bs->p_w > bs->p_rw)
1121             {
1122                 return -1;
1123             }
1124
1125             /*LOGF("type: %s code: %02i in : %6i out : %6i diff : %6i fact: %2.2f\n",
1126             (picture_coding_type == I_TYPE ? "I_TYPE" : (picture_coding_type == P_TYPE ? "P_TYPE" : "B_TYPE")),
1127             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));*/
1128
1129             if (bs->p_w - outTemp > bs->p_c - inTemp) // yes that might happen, rarely
1130             {
1131
1132                 /*LOGF("*** slice bigger than before !! (type: %s code: %i in : %i out : %i diff : %i)\n",
1133                 (picture_coding_type == I_TYPE ? "I_TYPE" : (picture_coding_type == P_TYPE ? "P_TYPE" : "B_TYPE")),
1134                 ID, bs->p_c - inTemp, bs->p_w - outTemp, (bs->p_w - outTemp) - (bs->p_c - inTemp));*/
1135
1136                 if ( !tr->mpeg4_matrix )
1137                 {
1138                     // in this case, we'll just use the original slice !
1139                     memcpy(outTemp, inTemp, bs->p_c - inTemp);
1140                     bs->p_w = outTemp + (bs->p_c - inTemp);
1141
1142                     // adjust bs->i_byte_out
1143                     bs->i_byte_out -= (bs->p_w - outTemp) - (bs->p_c - inTemp);
1144                 }
1145                 else
1146                 {
1147                     fprintf(stderr, "bad choice for mpeg4-matrix...\n");
1148                 }
1149             }
1150         }
1151     }
1152     return 0;
1153 }
1154
1155 int process_frame( sout_stream_t *p_stream, sout_stream_id_t *id,
1156                    block_t *in, block_t **out, int i_handicap )
1157 {
1158     transrate_t *tr = &id->tr;
1159     bs_transrate_t *bs = &tr->bs;
1160
1161     block_t       *p_out;
1162
1163     double        f_drift, f_fact;
1164     int           i_drift;
1165
1166     p_out = block_New( p_stream, in->i_buffer * 3 );
1167
1168     p_out->i_length = in->i_length;
1169     p_out->i_dts    = in->i_dts;
1170     p_out->i_pts    = in->i_pts;
1171     p_out->i_flags  = in->i_flags;
1172
1173     bs->p_rw = bs->p_ow = bs->p_w = p_out->p_buffer;
1174     bs->p_c = bs->p_r = in->p_buffer;
1175     bs->p_r += in->i_buffer + 4;
1176     bs->p_rw += in->i_buffer * 2;
1177     *(in->p_buffer + in->i_buffer) = 0;
1178     *(in->p_buffer + in->i_buffer + 1) = 0;
1179     *(in->p_buffer + in->i_buffer + 2) = 1;
1180     *(in->p_buffer + in->i_buffer + 3) = 0;
1181
1182     /* Calculate how late we are */
1183     bs->i_byte_in = in->i_buffer;
1184     bs->i_byte_out  = 0;
1185
1186     i_drift = tr->i_current_output + tr->i_remaining_input
1187                 - tr->i_wanted_output;
1188     f_drift = (double)i_drift / tr->i_wanted_output;
1189     f_fact = (double)(tr->i_wanted_output - tr->i_current_output)
1190                     / tr->i_remaining_input;
1191
1192     if ( in->i_flags & BLOCK_FLAG_TYPE_I )
1193     {
1194         /* This is the last picture of the GOP ; only transrate if we're
1195          * very late. */
1196         if ( 0 && f_drift > 0.085 )
1197         {
1198             tr->i_minimum_error = (f_drift - 0.085) * 50.0 * 50.0;
1199             tr->i_admissible_error = (f_drift - 0.085) * 50.0 * 75.0;
1200             tr->qrate = 1.0 + (f_drift - 0.085) * 50.0;
1201             msg_Warn( p_stream, "transrating I %d/%d",
1202                       tr->i_minimum_error, tr->i_admissible_error );
1203         }
1204         else
1205         {
1206             tr->i_minimum_error = 0;
1207             tr->i_admissible_error = 0;
1208             tr->qrate = 1.0;
1209         }
1210     }
1211     else if ( in->i_flags & BLOCK_FLAG_TYPE_P )
1212     {
1213         if ( f_fact < 0.8 )
1214         {
1215             tr->i_minimum_error = (0.8 - f_fact) * 3000.0 + i_handicap;
1216             tr->i_admissible_error = (0.8 - f_fact) * 3500.0 + i_handicap;
1217             tr->qrate = 1.0 + (0.8 - f_fact) * 70.0;
1218         }
1219         else
1220         {
1221             tr->i_minimum_error = 0;
1222             tr->i_admissible_error = 0;
1223             tr->qrate = 1.0;
1224         }
1225     }
1226     else
1227     {
1228         if ( f_fact < 1.2 )
1229         {
1230             tr->i_minimum_error = (1.2 - f_fact) * 1750.0 + i_handicap;
1231             tr->i_admissible_error = (1.2 - f_fact) * 2250.0 + i_handicap;
1232             tr->qrate = 1.0 + (1.2 - f_fact) * 45.0;
1233         }
1234         else
1235         {
1236             tr->i_minimum_error = 0;
1237             tr->i_admissible_error = 0;
1238             tr->qrate = 1.0;
1239         }
1240     }
1241
1242     tr->new_quantizer_scale = 0;
1243     tr->b_error = 0;
1244
1245     for ( ; ; )
1246     {
1247         uint8_t *p_end = &in->p_buffer[in->i_buffer];
1248
1249         /* Search next start code */
1250         for( ;; )
1251         {
1252             if( bs->p_c < p_end - 3 && bs->p_c[0] == 0 && bs->p_c[1] == 0 && bs->p_c[2] == 1 )
1253             {
1254                 /* Next start code */
1255                 break;
1256             }
1257             else if( bs->p_c < p_end - 6 &&
1258                      bs->p_c[0] == 0 && bs->p_c[1] == 0 && bs->p_c[2] == 0 &&
1259                      bs->p_c[3] == 0 && bs->p_c[4] == 0 && bs->p_c[5] == 0 )
1260             {
1261                 /* remove stuffing (looking for 6 0x00 bytes) */
1262                 bs->p_c++;
1263             }
1264             else
1265             {
1266                 /* Copy */
1267                 *bs->p_w++ = *bs->p_c++;
1268             }
1269
1270             if( bs->p_c >= p_end )
1271             {
1272                 break;
1273             }
1274         }
1275
1276         if( bs->p_c >= p_end )
1277         {
1278             break;
1279         }
1280
1281         /* Copy the start code */
1282         memcpy( bs->p_w, bs->p_c, 3 );
1283         bs->p_c += 3;
1284         bs->p_w += 3;
1285
1286         if ( do_next_start_code( tr ) )
1287         {
1288             /* Error */
1289             msg_Err( p_stream, "error in do_next_start_code()" );
1290             block_Release( p_out );
1291             tr->i_remaining_input -= in->i_buffer;
1292             tr->i_current_output += in->i_buffer;
1293             return -1;
1294         }
1295     }
1296
1297     bs->i_byte_out += bs->p_w - bs->p_ow;
1298     p_out->i_buffer = bs->p_w - bs->p_ow;
1299
1300 #if 0
1301     if ( in->i_flags & BLOCK_FLAG_TYPE_P && f_fact < 0.8 )
1302     {
1303         double f_ratio = (in->i_buffer - p_out->i_buffer) / in->i_buffer;
1304         if ( f_ratio < (0.8 - f_fact) * 0.1 && i_handicap < 200 )
1305         {
1306             block_Release( p_out );
1307             return process_frame( p_stream, id, in, out, i_handicap + 50 );
1308         }
1309     }
1310
1311     if ( in->i_flags & BLOCK_FLAG_TYPE_B && f_fact < 1.1 )
1312     {
1313         double f_ratio = (double)(in->i_buffer - p_out->i_buffer)
1314                             / in->i_buffer;
1315         if ( f_ratio < (1.1 - f_fact) * 0.1 && i_handicap < 400 )
1316         {
1317 #ifdef DEBUG_TRANSRATER
1318             msg_Dbg( p_stream, "%d: %d -> %d big (f: %f d: %f)",
1319                      tr->picture_coding_type, in->i_buffer, p_out->i_buffer,
1320                      f_fact, f_drift);
1321 #endif
1322             block_Release( p_out );
1323             return process_frame( p_stream, id, in, out, i_handicap + 100 );
1324         }
1325     }
1326 #endif
1327
1328 #if 0
1329     {
1330         int toto;
1331         for ( toto = 0; toto < p_out->i_buffer; toto++ )
1332             if (in->p_buffer[toto] != p_out->p_buffer[toto])
1333                 msg_Dbg(p_stream, "toto %d %x %x", toto, in->p_buffer[toto], p_out->p_buffer[toto]);
1334     }
1335 #endif
1336
1337     block_ChainAppend( out, p_out );
1338     tr->i_remaining_input -= in->i_buffer;
1339     tr->i_current_output += p_out->i_buffer;
1340
1341 #ifdef DEBUG_TRANSRATER
1342     msg_Dbg( p_stream, "%d: %d -> %d (%d/%d)",
1343              tr->picture_coding_type, in->i_buffer, p_out->i_buffer,
1344              tr->i_minimum_error, tr->i_admissible_error );
1345 #endif
1346
1347     return 0;
1348 }
1349
1350