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