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