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