]> git.sesse.net Git - vlc/blob - modules/codec/cinepak.c
3a41336cbc7e8b9a214de4074f5780f39b5b2d4d
[vlc] / modules / codec / cinepak.c
1 /*****************************************************************************
2  * cinepak.c: cinepak video decoder
3  *****************************************************************************
4  * Copyright (C) 1999-2001 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_vout.h>
34 #include <vlc_codec.h>
35
36 /*****************************************************************************
37  * Module descriptor
38  *****************************************************************************/
39 static int  OpenDecoder ( vlc_object_t * );
40 static void CloseDecoder( vlc_object_t * );
41
42 vlc_module_begin();
43     set_description( N_("Cinepak video decoder") );
44     set_capability( "decoder", 100 );
45     set_category( CAT_INPUT );
46     set_subcategory( SUBCAT_INPUT_VCODEC );
47     set_callbacks( OpenDecoder, CloseDecoder );
48 vlc_module_end();
49
50
51 /*****************************************************************************
52  * Local prototypes
53  *****************************************************************************/
54 #define CINEPAK_MAXSTRIP 32
55
56 typedef struct
57 {
58     uint8_t i_y[4];
59     uint8_t i_u, i_v;
60
61 } cinepak_codebook_t;
62
63 typedef struct
64 {
65     int b_grayscale; /* force to grayscale */
66
67     unsigned int i_width;
68     unsigned int i_height;
69
70     int i_stride_x;
71     int i_stride_y;
72
73     uint8_t *p_y, *p_u, *p_v;
74
75     int i_stride[3]; /* our 3 planes */
76     int i_lines[3];
77     uint8_t *p_pix[3];
78
79     cinepak_codebook_t codebook_v1[CINEPAK_MAXSTRIP][256];
80     cinepak_codebook_t codebook_v4[CINEPAK_MAXSTRIP][256];
81
82 } cinepak_context_t;
83
84 /*****************************************************************************
85  * decoder_sys_t : decoder descriptor
86  *****************************************************************************/
87 struct decoder_sys_t
88 {
89     /*
90      * Cinepak properties
91      */
92     cinepak_context_t context;
93 };
94
95 static picture_t *DecodeBlock ( decoder_t *, block_t ** );
96
97 static int cinepak_decode_frame( cinepak_context_t *, size_t, uint8_t * );
98
99 /*****************************************************************************
100  * OpenDecoder: probe the decoder and return score
101  *****************************************************************************
102  * Tries to launch a decoder and return score so that the interface is able
103  * to chose.
104  *****************************************************************************/
105 static int OpenDecoder( vlc_object_t *p_this )
106 {
107     decoder_t *p_dec = (decoder_t*)p_this;
108     decoder_sys_t *p_sys;
109     vlc_value_t val;
110
111     if( p_dec->fmt_in.i_codec != VLC_FOURCC('c','v','i','d') &&
112         p_dec->fmt_in.i_codec != VLC_FOURCC('C','V','I','D') )
113     {
114         return VLC_EGENERIC;
115     }
116
117     /* Allocate the memory needed to store the decoder's structure */
118     if( ( p_dec->p_sys = p_sys = malloc(sizeof(decoder_sys_t)) ) == NULL )
119         return VLC_ENOMEM;
120     memset( &p_sys->context, 0, sizeof( cinepak_context_t ) );
121
122     var_Create( p_dec, "grayscale", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
123     var_Get( p_dec, "grayscale", &val );
124     p_sys->context.b_grayscale = val.b_bool;
125
126     p_dec->pf_decode_video = DecodeBlock;
127
128     msg_Dbg( p_dec, "cinepak decoder started" );
129
130     return VLC_SUCCESS;
131 }
132
133 /****************************************************************************
134  * DecodeBlock: the whole thing
135  ****************************************************************************
136  * This function must be fed with whole frames.
137  ****************************************************************************/
138 static picture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
139 {
140     decoder_sys_t *p_sys = p_dec->p_sys;
141     int i_status, i_plane;
142     uint8_t *p_dst, *p_src;
143     picture_t *p_pic;
144     block_t *p_block;
145
146     if( !pp_block || !*pp_block )
147     {
148         return NULL;
149     }
150     p_block = *pp_block;
151     *pp_block = NULL;
152
153     i_status = cinepak_decode_frame( &p_sys->context, p_block->i_buffer,
154                                      p_block->p_buffer );
155     if( i_status < 0 )
156     {
157         msg_Warn( p_dec, "cannot decode one frame (%zu bytes)",
158                   p_block->i_buffer );
159         block_Release( p_block );
160         return NULL;
161     }
162
163     p_dec->fmt_out.video.i_width = p_sys->context.i_width;
164     p_dec->fmt_out.video.i_height = p_sys->context.i_height;
165     p_dec->fmt_out.video.i_aspect = p_sys->context.i_width
166         * VOUT_ASPECT_FACTOR / p_sys->context.i_height;
167     p_dec->fmt_out.i_codec = VLC_FOURCC('I','4','2','0');
168
169     /* Get a new picture */
170     if( ( p_pic = p_dec->pf_vout_buffer_new( p_dec ) ) )
171     {
172         for( i_plane = 0; i_plane < 3; i_plane++ )
173         {
174             int i_line, i_lines;
175
176             p_dst = p_pic->p[i_plane].p_pixels;
177             p_src = p_sys->context.p_pix[i_plane];
178
179             i_lines = __MIN( p_sys->context.i_lines[i_plane],
180                              p_pic->p[i_plane].i_visible_lines );
181             for( i_line = 0; i_line < i_lines; i_line++ )
182             {
183                 memcpy( p_dst, p_src,
184                         __MIN( p_pic->p[i_plane].i_pitch,
185                                p_sys->context.i_stride[i_plane] ) );
186                 p_dst += p_pic->p[i_plane].i_pitch;
187                 p_src += p_sys->context.i_stride[i_plane];
188             }
189         }
190
191         p_pic->date = p_block->i_pts ? p_block->i_pts : p_block->i_dts;
192     }
193
194     block_Release( p_block );
195     return p_pic;
196 }
197
198 /*****************************************************************************
199  * CloseDecoder: decoder destruction
200  *****************************************************************************/
201 static void CloseDecoder( vlc_object_t *p_this )
202 {
203     decoder_t     *p_dec = (decoder_t *)p_this;
204     decoder_sys_t *p_sys = p_dec->p_sys;
205     int i;
206
207     msg_Dbg( p_dec, "cinepak decoder stopped" );
208
209     for( i = 0; i < 3; i++ )
210         free( p_sys->context.p_pix[i] );
211
212     free( p_sys );
213 }
214
215 /*****************************************************************************
216  * local Functions
217  *****************************************************************************/
218
219 #define GET2BYTES( p ) \
220     GetWBE( p ); p+= 2;
221 /* FIXME */
222 #define GET3BYTES( p ) \
223     (GetDWBE( p ) >> 8); p+= 3;
224
225 #define GET4BYTES( p ) \
226     GetDWBE( p ); p+= 4;
227
228 static void cinepak_LoadCodebook( cinepak_codebook_t *p_codebook,
229                                   uint8_t *p_data, int b_grayscale )
230 {
231     int i, i_y[4], i_u, i_v, i_Cb, i_Cr;
232     int i_uv;
233 #define SCALEBITS 12
234 #define FIX( x ) ( (int)( (x) * ( 1L << SCALEBITS ) + 0.5 ) )
235
236     for( i = 0; i < 4; i++ )
237     {
238         i_y[i] = (uint8_t)( *(p_data++) );
239     }
240     if( b_grayscale )
241     {
242         i_u  = (int8_t)( *(p_data++) );
243         i_v  = (int8_t)( *(p_data++) );
244     }
245     else
246     {
247         i_u  = 0;
248         i_v  = 0;
249     }
250
251     /*
252           | Y  |   | 1 -0.0655  0.0110 | | CY |
253           | Cb | = | 0  1.1656 -0.0062 | | CU |
254           | Cr |   | 0  0.0467  1.4187 | | CV |
255      */
256     i_uv = ( FIX( -0.0655 ) * i_u + FIX( 0.0110 ) * i_v ) >> SCALEBITS;
257     for( i = 0; i < 4; i++ )
258     {
259         i_y[i] += i_uv;
260     }
261     i_Cb  = ( FIX( 1.1656 ) * i_u + FIX( -0.0062 ) * i_v ) >> SCALEBITS;
262     i_Cr  = ( FIX( 0.0467 ) * i_u + FIX(  1.4187 ) * i_v ) >> SCALEBITS;
263
264     for( i = 0; i < 4; i++ )
265     {
266         p_codebook->i_y[i] = __MIN( __MAX( 0, i_y[i] ), 255 );
267     }
268     p_codebook->i_u  = __MIN( __MAX( 0, i_Cb + 128 ), 255 );
269     p_codebook->i_v  = __MIN( __MAX( 0, i_Cr + 128 ), 255 );
270
271 #undef FIX
272 #undef SCALEBITS
273 }
274
275 static void cinepak_Getv4( cinepak_context_t *p_context,
276                            int i_strip, int i_x, int i_y,
277                            uint8_t *p_data )
278 {
279     uint8_t i_index[4];
280     int i,j;
281
282     size_t y_max = p_context->i_stride[0] * ( i_y + 5 ) + i_x + 5;
283     size_t u_max = p_context->i_stride[1] * ( ( i_y/2 ) + 2 ) + 2 + ( i_x / 2 );
284     size_t v_max = p_context->i_stride[2] * ( ( i_y/2 ) + 2 ) + 2 + ( i_x / 2 );
285     size_t y_siz = p_context->i_stride[0] * p_context->i_lines[0];
286     size_t u_siz = p_context->i_stride[1] * p_context->i_lines[1];
287     size_t v_siz = p_context->i_stride[2] * p_context->i_lines[2];
288     /* boundary check */
289     if( y_max >= y_siz || u_max >= u_siz || v_max >= v_siz )
290         return;
291
292     uint8_t *p_dst_y, *p_dst_u, *p_dst_v;
293 #define PIX_SET_Y( x, y, v ) \
294     p_dst_y[(x) + (y)* p_context->i_stride[0]] = (v);
295
296 #define PIX_SET_UV( i, p, x, y, v ) \
297     p[(x) + (y)* (p_context->i_stride[i])] = (v);
298
299     for( i = 0; i < 4; i++ )
300     {
301         i_index[i] = *(p_data++);
302     }
303
304     /* y plane */
305     p_dst_y = p_context->p_pix[0] + p_context->i_stride[0] * i_y + i_x;
306     p_dst_u = p_context->p_pix[1] + p_context->i_stride[1] * (i_y/2) + (i_x/2);
307     p_dst_v = p_context->p_pix[2] + p_context->i_stride[2] * (i_y/2) + (i_x/2);
308
309     for( i = 0; i < 2; i++ )
310     {
311         for( j = 0; j < 2; j ++ )
312         {
313             PIX_SET_Y( 2*j + 0, 2*i + 0,
314                        p_context->codebook_v4[i_strip][i_index[j+2*i]].i_y[0]);
315             PIX_SET_Y( 2*j + 1, 2*i + 0,
316                        p_context->codebook_v4[i_strip][i_index[j+2*i]].i_y[1]);
317             PIX_SET_Y( 2*j + 0, 2*i + 1,
318                        p_context->codebook_v4[i_strip][i_index[j+2*i]].i_y[2]);
319             PIX_SET_Y( 2*j + 1, 2*i + 1,
320                        p_context->codebook_v4[i_strip][i_index[j+2*i]].i_y[3]);
321
322             PIX_SET_UV( 1, p_dst_u, j, i,
323                         p_context->codebook_v4[i_strip][i_index[j+2*i]].i_u );
324             PIX_SET_UV( 2, p_dst_v, j, i,
325                         p_context->codebook_v4[i_strip][i_index[j+2*i]].i_v );
326         }
327     }
328 #undef PIX_SET_Y
329 #undef PIX_SET_UV
330 }
331
332 static void cinepak_Getv1( cinepak_context_t *p_context,
333                            int i_strip, int i_x,  int i_y,
334                            uint8_t *p_data )
335 {
336     uint8_t i_index;
337     int i,j;
338
339     size_t y_max = p_context->i_stride[0] * ( i_y + 5 ) + i_x + 5;
340     size_t u_max = p_context->i_stride[1] * ( ( i_y/2 ) + 2 ) + 2 + ( i_x / 2 );
341     size_t v_max = p_context->i_stride[2] * ( ( i_y/2 ) + 2 ) + 2 + ( i_x / 2 );
342     size_t y_siz = p_context->i_stride[0] * p_context->i_lines[0];
343     size_t u_siz = p_context->i_stride[1] * p_context->i_lines[1];
344     size_t v_siz = p_context->i_stride[2] * p_context->i_lines[2];
345     /* boundary check */
346     if( y_max >= y_siz || u_max >= u_siz || v_max >= v_siz )
347         return;
348
349     uint8_t *p_dst_y, *p_dst_u, *p_dst_v;
350 #define PIX_SET_Y( x, y, v ) \
351     p_dst_y[(x) + (y)* p_context->i_stride[0]] = (v);
352
353 #define PIX_SET_UV( i,p, x, y, v ) \
354     p[(x) + (y)* (p_context->i_stride[i])] = (v);
355
356     i_index = *(p_data++);
357
358     /* y plane */
359     p_dst_y = p_context->p_pix[0] + p_context->i_stride[0] * i_y + i_x;
360     p_dst_u = p_context->p_pix[1] + p_context->i_stride[1] * (i_y/2) + (i_x/2);
361     p_dst_v = p_context->p_pix[2] + p_context->i_stride[2] * (i_y/2) + (i_x/2);
362
363     for( i = 0; i < 2; i++ )
364     {
365         for( j = 0; j < 2; j ++ )
366         {
367             PIX_SET_Y( 2*j + 0, 2*i + 0,
368                        p_context->codebook_v1[i_strip][i_index].i_y[2*i+j] );
369             PIX_SET_Y( 2*j + 1, 2*i + 0,
370                        p_context->codebook_v1[i_strip][i_index].i_y[2*i+j] );
371             PIX_SET_Y( 2*j + 0, 2*i + 1,
372                        p_context->codebook_v1[i_strip][i_index].i_y[2*i+j] );
373             PIX_SET_Y( 2*j + 1, 2*i + 1,
374                        p_context->codebook_v1[i_strip][i_index].i_y[2*i+j] );
375
376             PIX_SET_UV( 1,p_dst_u, j, i,
377                         p_context->codebook_v1[i_strip][i_index].i_u );
378             PIX_SET_UV( 2,p_dst_v, j, i,
379                         p_context->codebook_v1[i_strip][i_index].i_v );
380         }
381     }
382
383 #undef PIX_SET_Y
384 #undef PIX_SET_UV
385 }
386
387 /*****************************************************************************
388  * The function that decode one frame
389  *****************************************************************************/
390 static int cinepak_decode_frame( cinepak_context_t *p_context,
391                                  size_t i_length, uint8_t *p_data )
392 {
393     int i_strip;
394
395     int8_t i_frame_flags;
396     uint32_t i_frame_size;
397     uint16_t i_width, i_height;
398     uint16_t i_frame_strips;
399     int i_index;
400     int i_strip_x1 =0, i_strip_y1=0;
401     int i_strip_x2 =0, i_strip_y2=0;
402
403     if( i_length <= 10 )
404     {
405         /* Broken header or no data */
406         return( -1 );
407     }
408
409     /* get header */
410     i_frame_flags  = *(p_data++);
411     i_frame_size = GET3BYTES( p_data );
412     i_width  = GET2BYTES( p_data );
413     i_height = GET2BYTES( p_data );
414     i_frame_strips = GET2BYTES( p_data );
415
416     if( !i_frame_size || !i_width || !i_height ||
417         i_width > 0xffff-3 || i_height > 0xffff-3)
418     {
419         /* Broken header */
420         return( -1 );
421     }
422
423     /* Check if we have a picture buffer with good size */
424     if( ( p_context->i_width != i_width ) ||
425         ( p_context->i_height != i_height ) )
426     {
427         int i;
428         for( i = 0; i < 3; i++ )
429         {
430             free( p_context->p_pix[i] );
431         }
432
433         p_context->i_width = i_width;
434         p_context->i_height = i_height;
435
436         p_context->i_stride[0] = ( i_width + 3 ) & 0xfffc;
437         p_context->i_stride[1] = p_context->i_stride[2] =
438                 p_context->i_stride[0] / 2;
439
440         p_context->i_lines[0] = ( i_height + 3 ) & 0xfffc;
441         p_context->i_lines[1] = p_context->i_lines[2] =
442                 p_context->i_lines[0] /2;
443
444         for( i = 0; i < 3; i++ )
445         {
446             p_context->p_pix[i] = malloc( p_context->i_stride[i] *
447                                           p_context->i_lines[i] );
448             if( p_context->p_pix[i] == NULL )
449                 return -1;
450             /* Set it to all black */
451             memset( p_context->p_pix[i], ( i == 0 ) ? 0 : 128 ,
452                     p_context->i_stride[i] * p_context->i_lines[i] );
453         }
454     }
455
456     if( i_frame_size != i_length )
457     {
458         i_length = __MIN( i_length, i_frame_size );
459     }
460     i_length -= 10;
461
462     if( i_frame_strips >= CINEPAK_MAXSTRIP )
463     {
464         i_frame_strips = CINEPAK_MAXSTRIP;
465     }
466
467     /* Now decode each strip */
468     for( i_strip = 0; i_strip < i_frame_strips; i_strip++ )
469     {
470         uint16_t i_strip_size;
471
472         if( i_length <= 12 )
473         {
474             break;
475         }
476
477         p_data += 2; /* int16_t i_strip_id   = GET2BYTES( p_data ); */
478
479         i_strip_size = GET2BYTES( p_data );
480         i_strip_size = __MIN( i_strip_size, i_length );
481         /* FIXME I don't really understand how it works; */
482         i_strip_y1  = i_strip_y2 + GET2BYTES( p_data );
483         i_strip_x1  = GET2BYTES( p_data );
484         i_strip_y2  = i_strip_y2 + GET2BYTES( p_data );
485         i_strip_x2  = GET2BYTES( p_data );
486
487         i_length -= i_strip_size;
488
489         i_strip_size -= 12;
490         /* init codebook , if needed */
491         if( ( i_strip > 0 )&&( !(i_frame_flags&0x01) ) )
492         {
493             memcpy( &p_context->codebook_v1[i_strip],
494                     &p_context->codebook_v1[i_strip-1],
495                     sizeof(cinepak_codebook_t[256] ) );
496
497             memcpy( &p_context->codebook_v4[i_strip],
498                     &p_context->codebook_v4[i_strip-1],
499                     sizeof(cinepak_codebook_t[256] ) );
500         }
501
502         /* Now parse all chunk in this strip */
503         while( i_strip_size > 0 )
504         {
505             cinepak_codebook_t (*p_codebook)[CINEPAK_MAXSTRIP][256];
506             int i_mode;
507
508             int i_chunk_id;
509             int i_chunk_size;
510             uint32_t i_vector_flags;
511             int i_count;
512             int i;
513             int i_x, i_y; /* (0,0) begin in fact at (x1,y1) ... */
514
515             i_chunk_id   = GET2BYTES( p_data );
516             i_chunk_size = GET2BYTES( p_data );
517             i_chunk_size  = __MIN( i_chunk_size, i_strip_size );
518             i_strip_size -= i_chunk_size;
519
520             i_chunk_size -= 4;
521
522             i_x = 0;
523             i_y = 0;
524             if( i_chunk_size < 0 )
525             {
526                 break;
527             }
528
529             switch( i_chunk_id )
530             {
531             case( 0x2000 ): /* 12bits v4 Intra*/
532             case( 0x2200 ): /* 12bits v1 Intra*/
533             case( 0x2400 ): /* 8bits v4 Intra*/
534             case( 0x2600 ): /* 8bits v1 Intra */
535                 i_mode = ( ( i_chunk_id&0x0400 ) == 0 );
536                 p_codebook = ( i_chunk_id&0x0200 ) ?
537                                &p_context->codebook_v1 :
538                                &p_context->codebook_v4;
539
540                 i_count = __MIN( i_chunk_size / ( i_mode ? 6 : 4 ), 256 );
541
542                 for( i = 0; i < i_count; i++ )
543                 {
544                     cinepak_LoadCodebook( &((*p_codebook)[i_strip][i]),
545                                           p_data,
546                                           i_mode&~p_context->b_grayscale );
547                     p_data += i_mode ? 6 : 4;
548                     i_chunk_size -= i_mode ? 6 : 4;
549                 }
550                 break;
551
552             case( 0x2100 ): /* selective 12bits v4 Inter*/
553             case( 0x2300 ): /* selective 12bits v1 Inter*/
554             case( 0x2500 ): /* selective 8bits v4 Inter*/
555             case( 0x2700 ): /* selective 8bits v1 Inter*/
556                 i_mode = ( ( i_chunk_id&0x0400 ) == 0 );
557                 p_codebook = ( i_chunk_id&0x0200 ) ?
558                                &p_context->codebook_v1 :
559                                &p_context->codebook_v4;
560
561                 i_index = 0;
562                 while( (i_chunk_size > 4)&&(i_index<256))
563                 {
564                     i_vector_flags = GET4BYTES( p_data );
565                     i_chunk_size -= 4;
566                     for( i = 0; i < 32; i++ )
567                     {
568                         if( ( i_chunk_size < ( i_mode ? 6 : 4 ) )
569                             || (i_index >= 256 ) )
570                         {
571                             break;
572                         }
573                         if( i_vector_flags&0x80000000UL )
574                         {
575                             cinepak_LoadCodebook(
576                                 &((*p_codebook)[i_strip][i_index]),
577                                 p_data, i_mode&~p_context->b_grayscale );
578
579                             p_data += i_mode ? 6 : 4;
580                             i_chunk_size -= i_mode ? 6 : 4;
581                         }
582                         i_index++;
583                         i_vector_flags <<= 1;
584                     }
585                 }
586                 break;
587
588             case( 0x3000 ): /* load image Intra */
589                 while( (i_chunk_size >= 4 )&&(i_y<i_strip_y2-i_strip_y1) )
590                 {
591                     i_vector_flags = GET4BYTES( p_data );
592                     i_chunk_size -= 4;
593                     i_strip_size -= 4;
594                     i_length     -= 4;
595
596                     for( i = 0; i < 32; i++ )
597                     {
598                         if( ( i_y >= i_strip_y2 - i_strip_y1) ||
599                             ( i_chunk_size<=0 ) )
600                         {
601                             break;
602                         }
603                         if( i_vector_flags&0x80000000UL )
604                         {
605                             cinepak_Getv4( p_context,
606                                            i_strip,
607                                            i_strip_x1 + i_x,
608                                            i_strip_y1 + i_y,
609                                            p_data );
610                             p_data += 4;
611                             i_chunk_size -= 4;
612                         }
613                         else
614                         {
615                             cinepak_Getv1( p_context,
616                                            i_strip,
617                                            i_strip_x1 + i_x,
618                                            i_strip_y1 + i_y,
619                                            p_data );
620                             p_data++;
621                             i_chunk_size--;
622                         }
623
624                         i_x += 4;
625                         if( i_x >= i_strip_x2 - i_strip_x1 )
626                         {
627                             i_x = 0;
628                             i_y += 4;
629                         }
630                         i_vector_flags <<= 1;
631                     }
632                 }
633                 break;
634
635             case( 0x3100 ): /* load image Inter */
636                 while( ( i_chunk_size > 4 )&&( i_y < i_strip_y2 - i_strip_y1) )
637                 {
638                     uint32_t i_mask;
639                     i_vector_flags = GET4BYTES( p_data );
640                     i_chunk_size -= 4;
641                     i_mask = 0x80000000UL;
642
643                     while( (i_chunk_size > 0 ) && ( i_mask )
644                            && ( i_y < i_strip_y2 - i_strip_y1 ) )
645                     {
646                         if( i_vector_flags&i_mask )
647                         {
648                             i_mask >>= 1;
649                             if( !i_mask )
650                             {
651                                 if( i_chunk_size < 4 )
652                                 {
653                                     break;
654                                 }
655                                 i_vector_flags = GET4BYTES( p_data );
656                                 i_chunk_size -= 4;
657                                 i_mask = 0x80000000UL;
658                             }
659                             if( i_vector_flags&i_mask )
660                             {
661                                 if( i_chunk_size < 4 ) break;
662                                 cinepak_Getv4( p_context,
663                                                i_strip,
664                                                i_strip_x1 + i_x,
665                                                i_strip_y1 + i_y,
666                                                p_data );
667                                 p_data += 4;
668                                 i_chunk_size -= 4;
669                             }
670                             else
671                             {
672                                 if( i_chunk_size < 1 ) break;
673                                 cinepak_Getv1( p_context,
674                                                i_strip,
675                                                i_strip_x1 + i_x,
676                                                i_strip_y1 + i_y,
677                                                p_data );
678                                 p_data++;
679                                 i_chunk_size--;
680                             }
681                         }
682                         i_mask >>= 1;
683
684                         i_x += 4;
685                         if( i_x >= i_strip_x2 - i_strip_x1 )
686                         {
687                             i_x = 0;
688                             i_y += 4;
689                         }
690                     }
691                 }
692                 break;
693
694             case( 0x3200 ): /* load intra picture but all v1*/
695                 while( ( i_chunk_size > 0 ) &&
696                        ( i_y < i_strip_y2 - i_strip_y1 ) )
697                 {
698                     cinepak_Getv1( p_context,
699                                    i_strip,
700                                    i_strip_x1 + i_x,
701                                    i_strip_y1 + i_y,
702                                    p_data );
703                     p_data++;
704                     i_chunk_size--;
705
706                     i_x += 4;
707                     if( i_x >= i_strip_x2 - i_strip_x1 )
708                     {
709                         i_x = 0;
710                         i_y += 4;
711                     }
712                 }
713                 break;
714
715             default:
716                 break;
717
718             }
719             p_data += i_chunk_size ; /* skip remains bytes */
720         }
721     }
722
723     return( 0 );
724 }