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