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