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