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