root/modules/stream_out/transrate/frame.c

Revision 814c571d39987ed207bc8efcb0918814cef6ec4b, 41.6 kB (checked in by Laurent Aimar <fenrir@videolan.org>, 3 months ago)

Fixed config.h include order.

  • Property mode set to 100644
Line 
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