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