]> git.sesse.net Git - vlc/blob - modules/codec/cinepak/cinepak.c
* src/input/input_ext-dec.c, include/input_ext-dec.h, modules/codec/araw.c:
[vlc] / modules / codec / cinepak / cinepak.c
1 /*****************************************************************************
2  * cinepak.c: cinepak video decoder 
3  *****************************************************************************
4  * Copyright (C) 1999-2001 VideoLAN
5  * $Id: cinepak.c,v 1.5 2002/10/24 09:37:48 gbazin Exp $
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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                      /* malloc(), free() */
28
29 #include <vlc/vlc.h>
30 #include <vlc/vout.h>
31 #include <vlc/decoder.h>
32 #include <vlc/input.h>
33
34 #ifdef HAVE_UNISTD_H
35 #include <unistd.h>                                              /* getpid() */
36 #endif
37
38 #include <errno.h>
39 #include <string.h>
40
41 #ifdef HAVE_SYS_TIMES_H
42 #   include <sys/times.h>
43 #endif
44
45 #include "cinepak.h"
46
47 /*****************************************************************************
48  * Local prototypes
49  *****************************************************************************/
50 static int      OpenDecoder     ( vlc_object_t * );
51 static int      RunDecoder      ( decoder_fifo_t * );
52 static int      InitThread      ( videodec_thread_t * );
53 static void     EndThread       ( videodec_thread_t * );
54 static void     DecodeThread    ( videodec_thread_t * );
55
56 /*****************************************************************************
57  * Module descriptor
58  *****************************************************************************/
59 vlc_module_begin();
60     set_description( "Cinepak video decoder" );
61     set_capability( "decoder", 70 );
62     set_callbacks( OpenDecoder, NULL );
63 vlc_module_end();
64
65 /*****************************************************************************
66  * OpenDecoder: probe the decoder and return score
67  *****************************************************************************
68  * Tries to launch a decoder and return score so that the interface is able 
69  * to chose.
70  *****************************************************************************/
71 static int OpenDecoder( vlc_object_t *p_this )
72 {
73     decoder_fifo_t *p_fifo = (decoder_fifo_t*) p_this;
74     
75     switch( p_fifo->i_fourcc )
76     {
77         case VLC_FOURCC('c','v','i','d'):
78         case VLC_FOURCC('C','V','I','D'):
79             p_fifo->pf_run = RunDecoder;
80             return VLC_SUCCESS;
81     }
82
83     return VLC_EGENERIC;
84 }
85
86 /*****************************************************************************
87  * RunDecoder: this function is called just after the thread is created
88  *****************************************************************************/
89 static int RunDecoder( decoder_fifo_t *p_fifo )
90 {   
91     videodec_thread_t   *p_vdec;
92     int b_error;
93     
94     if ( !(p_vdec = (videodec_thread_t*)malloc( sizeof(videodec_thread_t))) )
95     {
96         msg_Err( p_fifo, "out of memory" );
97         DecoderError( p_fifo );
98         return( -1 );
99     }
100     memset( p_vdec, 0, sizeof( videodec_thread_t ) );
101
102     p_vdec->p_fifo = p_fifo;
103
104     if( InitThread( p_vdec ) != 0 )
105     {
106         DecoderError( p_fifo );
107         return( -1 );
108     }
109      
110     while( (!p_vdec->p_fifo->b_die) && (!p_vdec->p_fifo->b_error) )
111     {
112         DecodeThread( p_vdec );
113     }
114
115     if( ( b_error = p_vdec->p_fifo->b_error ) )
116     {
117         DecoderError( p_vdec->p_fifo );
118     }
119
120     EndThread( p_vdec );
121
122     if( b_error )
123     {
124         return( -1 );
125     }
126    
127     return( 0 );
128
129
130
131 /*****************************************************************************
132  * locales Functions
133  *****************************************************************************/
134
135 static inline u16 GetWBE( u8 *p_buff )
136 {
137     return( (p_buff[0]<<8) + p_buff[1] );
138 }
139
140 static inline u32 GetDWBE( u8 *p_buff )
141 {
142     return( (p_buff[0] << 24) + ( p_buff[1] <<16 ) +
143             ( p_buff[2] <<8 ) + p_buff[3] );
144 }
145
146 #define GET2BYTES( p ) \
147     GetWBE( p ); p+= 2;
148 /* FIXME */
149 #define GET3BYTES( p ) \
150     (GetDWBE( p ) >> 8); p+= 3;
151
152 #define GET4BYTES( p ) \
153     GetDWBE( p ); p+= 4;
154
155 #define FREE( p ) \
156     if( p ) free( p )
157
158
159 static inline void __GetFrame( videodec_thread_t *p_vdec )
160 {
161     pes_packet_t  *p_pes;
162     data_packet_t *p_data;
163     byte_t        *p_buffer;
164
165     p_pes = GetPES( p_vdec->p_fifo );
166     p_vdec->i_pts = p_pes->i_pts;
167
168     while( ( !p_pes->i_nb_data )||( !p_pes->i_pes_size ) )
169     {
170         p_pes = NextPES( p_vdec->p_fifo );
171     }
172     p_vdec->i_framesize = p_pes->i_pes_size;
173     if( p_pes->i_nb_data == 1 )
174     {
175         p_vdec->p_framedata = p_pes->p_first->p_payload_start;
176         return;    
177     }
178     /* get a buffer and gather all data packet */
179     p_vdec->p_framedata = p_buffer = malloc( p_pes->i_pes_size );
180     p_data = p_pes->p_first;
181     do
182     {
183         p_vdec->p_fifo->p_vlc->pf_memcpy( p_buffer, p_data->p_payload_start, 
184                      p_data->p_payload_end - p_data->p_payload_start );
185         p_buffer += p_data->p_payload_end - p_data->p_payload_start;
186         p_data = p_data->p_next;
187     } while( p_data );
188 }
189
190 static inline void __NextFrame( videodec_thread_t *p_vdec )
191 {
192     pes_packet_t  *p_pes;
193
194     p_pes = GetPES( p_vdec->p_fifo );
195     if( p_pes->i_nb_data != 1 )
196     {
197         free( p_vdec->p_framedata ); /* FIXME keep this buffer */
198     }
199     NextPES( p_vdec->p_fifo );
200 }
201
202 static int cinepak_CheckVout( vout_thread_t *p_vout,
203                               int i_width,
204                               int i_height )
205 {
206     if( !p_vout )
207     {
208         return( 0 );
209     }
210     
211     if( ( p_vout->render.i_width != i_width )||
212         ( p_vout->render.i_height != i_height )||
213         ( p_vout->render.i_chroma != VLC_FOURCC('I','4','2','0') )||
214         ( p_vout->render.i_aspect != VOUT_ASPECT_FACTOR * i_width / i_height) )
215     {
216         return( 0 );
217     }
218     else
219     {
220         return( 1 );
221     }
222 }
223
224 /* Return a Vout */
225
226 static vout_thread_t *cinepak_CreateVout( videodec_thread_t *p_vdec,
227                                          int i_width,
228                                          int i_height )
229 {
230     vout_thread_t *p_vout;
231
232     if( (!i_width)||(!i_height) )
233     {
234         return( NULL ); /* Can't create a new vout without display size */
235     }
236
237     /* Spawn a video output if there is none. First we look for our children,
238      * then we look for any other vout that might be available. */
239     p_vout = vlc_object_find( p_vdec->p_fifo, VLC_OBJECT_VOUT,
240                                               FIND_CHILD );
241     if( !p_vout )
242     {
243         p_vout = vlc_object_find( p_vdec->p_fifo, VLC_OBJECT_VOUT,
244                                                   FIND_ANYWHERE );
245     }
246
247     if( p_vout )
248     {
249         if( !cinepak_CheckVout( p_vout, i_width, i_height ) )
250         {
251             /* We are not interested in this format, close this vout */
252             vlc_object_detach( p_vout );
253             vlc_object_release( p_vout );
254             vout_DestroyThread( p_vout );
255             p_vout = NULL;
256         }
257         else
258         {
259             /* This video output is cool! Hijack it. */
260             vlc_object_detach( p_vout );
261             vlc_object_attach( p_vout, p_vdec->p_fifo );
262             vlc_object_release( p_vout );
263         }
264     }
265
266     if( p_vout == NULL )
267     {
268         msg_Dbg( p_vdec->p_fifo, "no vout present, spawning one" );
269     
270         p_vout = vout_CreateThread( p_vdec->p_fifo,
271                                     i_width,
272                                     i_height,
273                                     VLC_FOURCC('I','4','2','0'),
274                                     VOUT_ASPECT_FACTOR * i_width / i_height );
275     }
276
277     return( p_vout );
278 }
279
280 void cinepak_LoadCodebook( cinepak_codebook_t *p_codebook,
281                            u8 *p_data,
282                            int b_grayscale )
283 {
284     int i, i_y[4], i_u, i_v, i_Cb, i_Cr;
285     int i_uv;
286 #define SCALEBITS 12
287 #define FIX( x ) ( (int)( (x) * ( 1L << SCALEBITS ) + 0.5 ) )
288     
289     for( i = 0; i < 4; i++ )
290     {
291         i_y[i] = (u8)( *(p_data++) );
292     }
293     if( b_grayscale )
294     {
295         i_u  = (s8)( *(p_data++) );
296         i_v  = (s8)( *(p_data++) );
297     }
298     else
299     {
300         i_u  = 0;
301         i_v  = 0;
302     }
303     
304     /*
305           | Y  |   | 1 -0.0655  0.0110 | | CY |
306           | Cb | = | 0  1.1656 -0.0062 | | CU |
307           | Cr |   | 0  0.0467  1.4187 | | CV |
308      */
309     i_uv = ( FIX( -0.0655 ) * i_u + FIX( 0.0110 ) * i_v ) >> SCALEBITS;
310     for( i = 0; i < 4; i++ )
311     {
312         i_y[i] += i_uv;
313     }
314     i_Cb  = ( FIX( 1.1656 ) * i_u + FIX( -0.0062 ) * i_v ) >> SCALEBITS;
315     i_Cr  = ( FIX( 0.0467 ) * i_u + FIX(  1.4187 ) * i_v ) >> SCALEBITS;
316     
317     for( i = 0; i < 4; i++ )
318     {
319         p_codebook->i_y[i] = __MIN( __MAX( 0, i_y[i] ), 255 );
320     }
321     p_codebook->i_u  = __MIN( __MAX( 0, i_Cb + 128 ), 255 );
322     p_codebook->i_v  = __MIN( __MAX( 0, i_Cr + 128 ), 255 );
323    
324 #undef FIX
325 #undef SCALEBITS
326 }
327
328 void cinepak_Getv4( cinepak_context_t *p_context,
329                     int i_strip,
330                     int i_x,  int i_y,
331                     int i_x2, int i_y2,
332                     u8 *p_data )
333 {
334     u8 i_index[4];
335     int i,j;
336     
337     u8 *p_dst_y, *p_dst_u, *p_dst_v;
338 #define PIX_SET_Y( x, y, v ) \
339     p_dst_y[(x) + (y)* p_context->i_stride[0]] = (v);
340     
341 #define PIX_SET_UV( i, p, x, y, v ) \
342     p[(x) + (y)* (p_context->i_stride[i])] = (v);
343    
344     for( i = 0; i < 4; i++ )
345     {
346         i_index[i] = *(p_data++);
347     }
348
349     /* y plane */
350     p_dst_y = p_context->p_pix[0] + p_context->i_stride[0] * i_y + i_x;
351     p_dst_u = p_context->p_pix[1] + p_context->i_stride[1] * (i_y/2) + (i_x/2);
352     p_dst_v = p_context->p_pix[2] + p_context->i_stride[2] * (i_y/2) + (i_x/2);
353     
354     for( i = 0; i < 2; i++ )
355     {
356         for( j = 0; j < 2; j ++ )
357         {
358             PIX_SET_Y( 2*j + 0, 2*i + 0, 
359                        p_context->codebook_v4[i_strip][i_index[j+2*i]].i_y[0]);
360             PIX_SET_Y( 2*j + 1, 2*i + 0, 
361                        p_context->codebook_v4[i_strip][i_index[j+2*i]].i_y[1]);
362             PIX_SET_Y( 2*j + 0, 2*i + 1, 
363                        p_context->codebook_v4[i_strip][i_index[j+2*i]].i_y[2]);
364             PIX_SET_Y( 2*j + 1, 2*i + 1, 
365                        p_context->codebook_v4[i_strip][i_index[j+2*i]].i_y[3]);
366
367             PIX_SET_UV( 1, p_dst_u, j, i, 
368                         p_context->codebook_v4[i_strip][i_index[j+2*i]].i_u );
369             PIX_SET_UV( 2, p_dst_v, j, i, 
370                         p_context->codebook_v4[i_strip][i_index[j+2*i]].i_v );
371         }
372     }
373 #undef PIX_SET_Y
374 #undef PIX_SET_UV
375 }
376
377 void cinepak_Getv1( cinepak_context_t *p_context,
378                     int i_strip,
379                     int i_x,  int i_y,
380                     int i_x2, int i_y2,
381                     u8 *p_data )
382 {
383     u8 i_index;
384     int i,j;
385     
386     u8 *p_dst_y, *p_dst_u, *p_dst_v;
387 #define PIX_SET_Y( x, y, v ) \
388     p_dst_y[(x) + (y)* p_context->i_stride[0]] = (v);
389     
390 #define PIX_SET_UV( i,p, x, y, v ) \
391     p[(x) + (y)* (p_context->i_stride[i])] = (v);
392    
393     i_index = *(p_data++);
394
395     /* y plane */
396     p_dst_y = p_context->p_pix[0] + p_context->i_stride[0] * i_y + i_x;
397     p_dst_u = p_context->p_pix[1] + p_context->i_stride[1] * (i_y/2) + (i_x/2);
398     p_dst_v = p_context->p_pix[2] + p_context->i_stride[2] * (i_y/2) + (i_x/2);
399
400     for( i = 0; i < 2; i++ )
401     {
402         for( j = 0; j < 2; j ++ )
403         {
404             PIX_SET_Y( 2*j + 0, 2*i + 0, 
405                        p_context->codebook_v1[i_strip][i_index].i_y[0] );
406             PIX_SET_Y( 2*j + 1, 2*i + 0, 
407                        p_context->codebook_v1[i_strip][i_index].i_y[1] );
408             PIX_SET_Y( 2*j + 0, 2*i + 1, 
409                        p_context->codebook_v1[i_strip][i_index].i_y[2] );
410             PIX_SET_Y( 2*j + 1, 2*i + 1, 
411                        p_context->codebook_v1[i_strip][i_index].i_y[3] );
412
413             PIX_SET_UV( 1,p_dst_u, j, i, 
414                         p_context->codebook_v1[i_strip][i_index].i_u );
415             PIX_SET_UV( 2,p_dst_v, j, i, 
416                         p_context->codebook_v1[i_strip][i_index].i_v );
417         }
418     }
419
420 #undef PIX_SET_Y
421 #undef PIX_SET_UV
422 }
423
424 /*****************************************************************************
425  * The function that decode one frame
426  *****************************************************************************/
427 int cinepak_decode_frame( cinepak_context_t *p_context, 
428                           int i_length, u8 *p_data )
429 {
430     int i_strip;
431
432     int i_frame_flags;
433     int i_frame_size;
434     int i_width, i_height;
435     int i_frame_strips;
436     int i_index;
437     int i_strip_x1 =0, i_strip_y1=0;
438     int i_strip_x2 =0, i_strip_y2=0;
439    
440     if( i_length <= 10 )
441     {
442         /* Broken header or no data */
443         return( -1 );
444     }
445   
446     /* get header */
447     i_frame_flags  = *(p_data++);
448     i_frame_size = GET3BYTES( p_data );
449     i_width  = GET2BYTES( p_data );
450     i_height = GET2BYTES( p_data );
451     i_frame_strips = GET2BYTES( p_data );
452     
453     /* Check if we have a picture buffer with good size */
454     if( ( p_context->i_width != i_width )||
455         ( p_context->i_height != i_height ) )
456     {
457         int i;
458         for( i = 0; i < 3; i++ )
459         {
460             FREE( p_context->p_pix[i] );
461         }
462
463         p_context->i_width = i_width;
464         p_context->i_height = i_height;
465
466         p_context->i_stride[0] = ( i_width + 3)&0xfffc;
467         p_context->i_stride[1] = p_context->i_stride[2] = 
468                 p_context->i_stride[0] / 2;
469
470         p_context->i_lines[0] = ( i_height + 3 )&0xfffc;
471         p_context->i_lines[1] = p_context->i_lines[2] =
472                 p_context->i_lines[0] /2;
473         
474         for( i = 0; i < 3; i++ )
475         {
476             p_context->p_pix[i] = malloc( p_context->i_stride[i] * 
477                                           p_context->i_lines[i] );
478             /* Set it to all black */
479             memset( p_context->p_pix[i], ( i == 0 ) ? 0 : 128 ,
480                     p_context->i_stride[i] * p_context->i_lines[i] );
481         }
482     }
483
484     if( i_frame_size != i_length )
485     {
486         i_length = __MIN( i_length, i_frame_size );
487     }
488     i_length -= 10;
489
490     if( i_frame_strips >= CINEPAK_MAXSTRIP )
491     {
492         i_frame_strips = CINEPAK_MAXSTRIP;
493     }
494
495     /* Now decode each strip */
496
497     for( i_strip = 0; i_strip < i_frame_strips; i_strip++ )
498     {
499         int i_strip_id;
500         int i_strip_size;
501
502         if( i_length <= 12 )
503         {
504             break;
505         }
506         
507            
508         i_strip_id   = GET2BYTES( p_data );
509         i_strip_size = GET2BYTES( p_data );
510         i_strip_size = __MIN( i_strip_size, i_length );
511         /* FIXME I don't really understand how it's work; */
512         i_strip_y1  = i_strip_y2 + GET2BYTES( p_data );
513         i_strip_x1  = GET2BYTES( p_data );
514         i_strip_y2  = i_strip_y2 + GET2BYTES( p_data );
515         i_strip_x2  = GET2BYTES( p_data );
516
517         i_length -= i_strip_size;
518
519         i_strip_size -= 12;
520         /* init codebook , if needed */
521         if( ( i_strip > 0 )&&( !(i_frame_flags&0x01) ) )
522         {
523             memcpy( &p_context->codebook_v1[i_strip], 
524                     &p_context->codebook_v1[i_strip-1],
525                     sizeof(cinepak_codebook_t[256] ) );
526
527             memcpy( &p_context->codebook_v4[i_strip], 
528                     &p_context->codebook_v4[i_strip-1],
529                     sizeof(cinepak_codebook_t[256] ) );
530         }
531
532         /* Now parse all chunk in this strip */
533         while( i_strip_size > 0 )
534         {
535             cinepak_codebook_t (*p_codebook)[CINEPAK_MAXSTRIP][256];
536             int i_mode;
537
538             int i_chunk_id;
539             int i_chunk_size;
540             u32 i_vector_flags;
541             int i_count;
542             int i;
543             int i_x, i_y; /* (0,0) begin in fact at (x1,y1) ... */
544
545             i_chunk_id   = GET2BYTES( p_data );
546             i_chunk_size = GET2BYTES( p_data );
547             i_chunk_size  = __MIN( i_chunk_size, i_strip_size ); 
548             i_strip_size -= i_chunk_size;
549             
550             i_chunk_size -= 4;
551
552             i_x = 0; 
553             i_y = 0;
554             if( i_chunk_size < 0 )
555             {
556                 break;
557             }
558             
559             switch( i_chunk_id )
560             {
561                 case( 0x2000 ): /* 12bits v4 Intra*/
562                 case( 0x2200 ): /* 12bits v1 Intra*/
563                 case( 0x2400 ): /* 8bits v4 Intra*/
564                 case( 0x2600 ): /* 8bits v1 Intra */
565                     i_mode = ( ( i_chunk_id&0x0400 ) == 0 );
566                     p_codebook = ( i_chunk_id&0x0200 ) ?
567                                     &p_context->codebook_v1 :
568                                     &p_context->codebook_v4;
569                     
570                     i_count = __MIN( i_chunk_size / ( i_mode ? 6 : 4 ), 256 );
571
572                     for( i = 0; i < i_count; i++ )
573                     {
574                         cinepak_LoadCodebook( &((*p_codebook)[i_strip][i]), 
575                                               p_data, 
576                                        i_mode&~p_context->b_grayscale );
577                         p_data += i_mode ? 6 : 4;
578                         i_chunk_size -= i_mode ? 6 : 4;
579                     }
580                     break;
581
582                 case( 0x2100 ): /* selective 12bits v4 Inter*/
583                 case( 0x2300 ): /* selective 12bits v1 Inter*/
584                 case( 0x2500 ): /* selective 8bits v4 Inter*/
585                 case( 0x2700 ): /* selective 8bits v1 Inter*/
586                     i_mode = ( ( i_chunk_id&0x0400 ) == 0 );
587                     p_codebook = ( i_chunk_id&0x0200 ) ?
588                                     &p_context->codebook_v1 :
589                                     &p_context->codebook_v4;
590                     
591                     i_index = 0;
592                     while( (i_chunk_size > 4)&&(i_index<256))
593                     {
594                         i_vector_flags = GET4BYTES( p_data );
595                         i_chunk_size -= 4;
596                         for( i = 0; i < 32; i++ )
597                         {
598                             if( ( i_chunk_size < ( i_mode ? 6 : 4 ) )||(i_index >= 256 ))
599                             {
600                                 break;
601                             }
602                             if( i_vector_flags&0x80000000UL )
603                             {
604                                 cinepak_LoadCodebook( &((*p_codebook)[i_strip][i_index]),
605                                                       p_data, 
606                                             i_mode&~p_context->b_grayscale );
607
608                                 p_data += i_mode ? 6 : 4;
609                                 i_chunk_size -= i_mode ? 6 : 4;
610                             }
611                             i_index++;
612                             i_vector_flags <<= 1;
613                         }
614                     }
615                     break;
616
617                 case( 0x3000 ): /* load image Intra */
618                     while( (i_chunk_size >= 4 )&&(i_y<i_strip_y2-i_strip_y1) )
619                     {
620                         i_vector_flags = GET4BYTES( p_data );
621                         i_chunk_size -= 4;
622                         i_strip_size -= 4;
623                         i_length     -= 4;
624
625                         for( i = 0; i < 32; i++ )
626                         {
627                             if( ( i_y >= i_strip_y2 - i_strip_y1)||
628                                     ( i_chunk_size<=0) )
629                             {
630                                 break;
631                             }
632                             if( i_vector_flags&0x80000000UL )
633                             {
634                                 cinepak_Getv4( p_context,
635                                                i_strip,
636                                                i_strip_x1 + i_x, 
637                                                i_strip_y1 + i_y,
638                                                i_strip_x2, i_strip_y2,
639                                                p_data );
640                                 p_data += 4;
641                                 i_chunk_size -= 4;
642                             }
643                             else
644                             {
645                                 cinepak_Getv1( p_context,
646                                                i_strip,
647                                                i_strip_x1 + i_x, 
648                                                i_strip_y1 + i_y,
649                                                i_strip_x2, i_strip_y2,
650                                                p_data );
651                                 p_data++;
652                                 i_chunk_size--;
653                             }
654
655                             i_x += 4;
656                             if( i_x >= i_strip_x2 - i_strip_x1 )
657                             {
658                                 i_x = 0;
659                                 i_y += 4;
660                             }
661                             i_vector_flags <<= 1;
662                         }
663                     } 
664                     break;
665
666                 case( 0x3100 ): /* load image Inter */
667                     while( ( i_chunk_size > 4 )&&( i_y < i_strip_y2 - i_strip_y1) )
668                     {
669                         u32 i_mask;
670                         i_vector_flags = GET4BYTES( p_data );
671                         i_chunk_size -= 4;
672                         i_mask = 0x80000000UL;
673
674                         while((i_chunk_size > 0 )&&( i_mask )&&( i_y < i_strip_y2 - i_strip_y1 ))
675                         {
676                             if( i_vector_flags&i_mask)
677                             {
678                                 i_mask >>= 1;
679                                 if( !i_mask )
680                                 {
681                                     if( i_chunk_size < 4 )
682                                     {
683                                         break;
684                                     } 
685                                     i_vector_flags = GET4BYTES( p_data );
686                                     i_chunk_size -= 4;
687                                     i_mask = 0x80000000UL;
688                                 }
689                                 if( i_vector_flags&i_mask )
690                                 {
691                                     if( i_chunk_size < 4 ) break;
692                                     cinepak_Getv4( p_context,
693                                                    i_strip,
694                                                    i_strip_x1 + i_x, 
695                                                    i_strip_y1 + i_y,
696                                                    i_strip_x2, i_strip_y2,
697                                                    p_data );
698                                     p_data += 4;
699                                     i_chunk_size -= 4;
700                                 }
701                                 else
702                                 {
703                                     if( i_chunk_size < 1 ) break;
704                                     cinepak_Getv1( p_context,
705                                                    i_strip,
706                                                    i_strip_x1 + i_x, 
707                                                    i_strip_y1 + i_y,
708                                                    i_strip_x2, i_strip_y2,
709                                                    p_data );
710                                     p_data++;
711                                     i_chunk_size--;
712                                 }
713                             }
714                             i_mask >>= 1;
715
716                             i_x += 4;
717                             if( i_x >= i_strip_x2 - i_strip_x1 )
718                             {
719                                 i_x = 0;
720                                 i_y += 4;
721                             }
722                         }
723                     } 
724                     break;
725
726                 case( 0x3200 ): /* load intra picture but all v1*/
727                     while( ( i_chunk_size > 0 )&&
728                            ( i_y < i_strip_y2 - i_strip_y1) )
729                     {
730                         cinepak_Getv1( p_context,
731                                        i_strip,
732                                        i_strip_x1 + i_x, 
733                                        i_strip_y1 + i_y,
734                                        i_strip_x2, i_strip_y2,
735                                        p_data );
736                         p_data++;
737                         i_chunk_size--;
738
739                         i_x += 4;
740                         if( i_x >= i_strip_x2 - i_strip_x1 )
741                         {
742                             i_x = 0;
743                             i_y += 4;
744                         }
745                         
746                     } 
747                     break;
748                     
749
750
751                 default:
752                     break;
753
754             }
755             p_data += i_chunk_size ; /* skip remains bytes */
756             
757         }
758     }
759
760     
761     return( 0 );
762 }
763
764
765 /*****************************************************************************
766  *
767  * Functions that initialize, decode and end the decoding process
768  *
769  *****************************************************************************/
770
771 /*****************************************************************************
772  * InitThread: initialize vdec output thread
773  *****************************************************************************
774  * This function is called from decoder_Run and performs the second step 
775  * of the initialization. It returns 0 on success. Note that the thread's 
776  * flag are not modified inside this function.
777  *****************************************************************************/
778
779 static int InitThread( videodec_thread_t *p_vdec )
780 {
781
782     /* This will be created after the first decoded frame */
783     if( !(p_vdec->p_context = malloc( sizeof( cinepak_context_t ) ) ) )
784     {
785         msg_Err( p_vdec->p_fifo, "out of memory" );
786     }
787     memset( p_vdec->p_context, 0, sizeof( cinepak_context_t ) );
788
789     if( config_GetInt( p_vdec->p_fifo, "grayscale" ) )
790     {
791         p_vdec->p_context->b_grayscale = 1;
792     }
793     else
794     {
795         p_vdec->p_context->b_grayscale = 0;
796     }
797     
798     p_vdec->p_vout = NULL;
799     msg_Dbg( p_vdec->p_fifo, "cinepak decoder started" );
800     return( 0 );
801 }
802
803
804 /*****************************************************************************
805  * DecodeThread: Called for decode one frame
806  *****************************************************************************/
807 static void  DecodeThread( videodec_thread_t *p_vdec )
808 {
809     int     i_status;
810     
811     int i_plane;
812     u8 *p_dst, *p_src;
813     picture_t *p_pic; /* videolan picture */
814
815     __GetFrame( p_vdec );
816
817     i_status = cinepak_decode_frame( p_vdec->p_context,
818                                      p_vdec->i_framesize,
819                                      p_vdec->p_framedata );
820     __NextFrame( p_vdec );
821                                          
822     if( i_status < 0 )
823     {
824         msg_Warn( p_vdec->p_fifo, "cannot decode one frame (%d bytes)",
825                                   p_vdec->i_framesize );
826         return;
827     }
828     
829     /* Check our vout */
830     if( !cinepak_CheckVout( p_vdec->p_vout,
831                             p_vdec->p_context->i_width,
832                             p_vdec->p_context->i_height ) )
833     {
834         p_vdec->p_vout = 
835           cinepak_CreateVout( p_vdec,
836                               p_vdec->p_context->i_width,
837                               p_vdec->p_context->i_height );
838
839         if( !p_vdec->p_vout )
840         {
841             msg_Err( p_vdec->p_fifo, "cannot create vout" );
842             p_vdec->p_fifo->b_error = 1; /* abort */
843             return;
844         }
845     }
846
847     /* Send decoded frame to vout */
848     while( !(p_pic = vout_CreatePicture( p_vdec->p_vout, 0, 0, 0 ) ) )
849     {
850         if( p_vdec->p_fifo->b_die || p_vdec->p_fifo->b_error )
851         {
852             return;
853         }
854         msleep( VOUT_OUTMEM_SLEEP );
855     }
856     
857     for( i_plane = 0; i_plane < 3; i_plane++ )
858     {
859         int i_line, i_lines;
860
861         p_dst = p_pic->p[i_plane].p_pixels;
862         p_src = p_vdec->p_context->p_pix[i_plane];
863
864         i_lines = __MIN( p_vdec->p_context->i_lines[i_plane],
865                          p_pic->p[i_plane].i_lines );
866         for( i_line = 0; i_line < i_lines; i_line++ )
867         {
868             memcpy( p_dst, 
869                     p_src, 
870                     __MIN( p_pic->p[i_plane].i_pitch,
871                            p_vdec->p_context->i_stride[i_plane] ) );
872             p_dst += p_pic->p[i_plane].i_pitch;
873             p_src += p_vdec->p_context->i_stride[i_plane];
874         }
875     }
876
877     vout_DatePicture( p_vdec->p_vout, p_pic, p_vdec->i_pts);
878     vout_DisplayPicture( p_vdec->p_vout, p_pic );
879     
880     return;
881 }
882
883
884 /*****************************************************************************
885  * EndThread: thread destruction
886  *****************************************************************************
887  * This function is called when the thread ends after a sucessful
888  * initialization.
889  *****************************************************************************/
890 static void EndThread( videodec_thread_t *p_vdec )
891 {
892     int i;
893     
894     if( !p_vdec )
895     {
896         return;
897     }
898     msg_Dbg( p_vdec->p_fifo, "cinepak decoder stopped" );
899
900     for( i = 0; i < 3; i++ )
901     {
902         FREE( p_vdec->p_context->p_pix[i] );
903     }
904     
905     free( p_vdec->p_context );
906     
907     if( p_vdec->p_vout != NULL )
908     {
909         /* We are about to die. Reattach video output to p_vlc. */
910         vlc_object_detach( p_vdec->p_vout );
911         vlc_object_attach( p_vdec->p_vout, p_vdec->p_fifo->p_vlc );
912     }
913     
914     free( p_vdec );
915 }
916
917