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 $
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <stdlib.h> /* malloc(), free() */
31 #include <vlc/decoder.h>
32 #include <vlc/input.h>
35 #include <unistd.h> /* getpid() */
41 #ifdef HAVE_SYS_TIMES_H
42 # include <sys/times.h>
47 /*****************************************************************************
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 * );
56 /*****************************************************************************
58 *****************************************************************************/
60 set_description( "Cinepak video decoder" );
61 set_capability( "decoder", 70 );
62 set_callbacks( OpenDecoder, NULL );
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
70 *****************************************************************************/
71 static int OpenDecoder( vlc_object_t *p_this )
73 decoder_fifo_t *p_fifo = (decoder_fifo_t*) p_this;
75 switch( p_fifo->i_fourcc )
77 case VLC_FOURCC('c','v','i','d'):
78 case VLC_FOURCC('C','V','I','D'):
79 p_fifo->pf_run = RunDecoder;
86 /*****************************************************************************
87 * RunDecoder: this function is called just after the thread is created
88 *****************************************************************************/
89 static int RunDecoder( decoder_fifo_t *p_fifo )
91 videodec_thread_t *p_vdec;
94 if ( !(p_vdec = (videodec_thread_t*)malloc( sizeof(videodec_thread_t))) )
96 msg_Err( p_fifo, "out of memory" );
97 DecoderError( p_fifo );
100 memset( p_vdec, 0, sizeof( videodec_thread_t ) );
102 p_vdec->p_fifo = p_fifo;
104 if( InitThread( p_vdec ) != 0 )
106 DecoderError( p_fifo );
110 while( (!p_vdec->p_fifo->b_die) && (!p_vdec->p_fifo->b_error) )
112 DecodeThread( p_vdec );
115 if( ( b_error = p_vdec->p_fifo->b_error ) )
117 DecoderError( p_vdec->p_fifo );
131 /*****************************************************************************
133 *****************************************************************************/
135 static inline u16 GetWBE( u8 *p_buff )
137 return( (p_buff[0]<<8) + p_buff[1] );
140 static inline u32 GetDWBE( u8 *p_buff )
142 return( (p_buff[0] << 24) + ( p_buff[1] <<16 ) +
143 ( p_buff[2] <<8 ) + p_buff[3] );
146 #define GET2BYTES( p ) \
149 #define GET3BYTES( p ) \
150 (GetDWBE( p ) >> 8); p+= 3;
152 #define GET4BYTES( p ) \
159 static void GetPESData( u8 *p_buf, int i_max, pes_packet_t *p_pes )
164 data_packet_t *p_data;
167 p_data = p_pes->p_first;
168 while( p_data != NULL && i_count < i_max )
171 i_copy = __MIN( p_data->p_payload_end - p_data->p_payload_start,
177 p_data->p_payload_start,
181 p_data = p_data->p_next;
186 if( i_count < i_max )
188 memset( p_buf, 0, i_max - i_count );
192 static int cinepak_CheckVout( vout_thread_t *p_vout,
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) )
216 static vout_thread_t *cinepak_CreateVout( videodec_thread_t *p_vdec,
220 vout_thread_t *p_vout;
222 if( (!i_width)||(!i_height) )
224 return( NULL ); /* Can't create a new vout without display size */
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,
233 p_vout = vlc_object_find( p_vdec->p_fifo, VLC_OBJECT_VOUT,
239 if( !cinepak_CheckVout( p_vout, i_width, i_height ) )
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 );
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 );
258 msg_Dbg( p_vdec->p_fifo, "no vout present, spawning one" );
260 p_vout = vout_CreateThread( p_vdec->p_fifo,
263 VLC_FOURCC('I','4','2','0'),
264 VOUT_ASPECT_FACTOR * i_width / i_height );
270 void cinepak_LoadCodebook( cinepak_codebook_t *p_codebook,
274 int i, i_y[4], i_u, i_v, i_Cb, i_Cr;
277 #define FIX( x ) ( (int)( (x) * ( 1L << SCALEBITS ) + 0.5 ) )
279 for( i = 0; i < 4; i++ )
281 i_y[i] = (u8)( *(p_data++) );
285 i_u = (s8)( *(p_data++) );
286 i_v = (s8)( *(p_data++) );
295 | Y | | 1 -0.0655 0.0110 | | CY |
296 | Cb | = | 0 1.1656 -0.0062 | | CU |
297 | Cr | | 0 0.0467 1.4187 | | CV |
299 i_uv = ( FIX( -0.0655 ) * i_u + FIX( 0.0110 ) * i_v ) >> SCALEBITS;
300 for( i = 0; i < 4; i++ )
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;
307 for( i = 0; i < 4; i++ )
309 p_codebook->i_y[i] = __MIN( __MAX( 0, i_y[i] ), 255 );
311 p_codebook->i_u = __MIN( __MAX( 0, i_Cb + 128 ), 255 );
312 p_codebook->i_v = __MIN( __MAX( 0, i_Cr + 128 ), 255 );
318 void cinepak_Getv4( cinepak_context_t *p_context,
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);
331 #define PIX_SET_UV( i, p, x, y, v ) \
332 p[(x) + (y)* (p_context->i_stride[i])] = (v);
334 for( i = 0; i < 4; i++ )
336 i_index[i] = *(p_data++);
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);
344 for( i = 0; i < 2; i++ )
346 for( j = 0; j < 2; j ++ )
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]);
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 );
367 void cinepak_Getv1( cinepak_context_t *p_context,
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);
380 #define PIX_SET_UV( i,p, x, y, v ) \
381 p[(x) + (y)* (p_context->i_stride[i])] = (v);
383 i_index = *(p_data++);
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);
390 for( i = 0; i < 2; i++ )
392 for( j = 0; j < 2; j ++ )
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] );
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 );
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 )
424 int i_width, i_height;
427 int i_strip_x1 =0, i_strip_y1=0;
428 int i_strip_x2 =0, i_strip_y2=0;
432 /* Broken header or no data */
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 );
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 ) )
448 for( i = 0; i < 3; i++ )
450 FREE( p_context->p_pix[i] );
453 p_context->i_width = i_width;
454 p_context->i_height = i_height;
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;
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;
464 for( i = 0; i < 3; i++ )
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] );
474 if( i_frame_size != i_length )
476 i_length = __MIN( i_length, i_frame_size );
480 if( i_frame_strips >= CINEPAK_MAXSTRIP )
482 i_frame_strips = CINEPAK_MAXSTRIP;
485 /* Now decode each strip */
487 for( i_strip = 0; i_strip < i_frame_strips; i_strip++ )
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 );
507 i_length -= i_strip_size;
510 /* init codebook , if needed */
511 if( ( i_strip > 0 )&&( !(i_frame_flags&0x01) ) )
513 memcpy( &p_context->codebook_v1[i_strip],
514 &p_context->codebook_v1[i_strip-1],
515 sizeof(cinepak_codebook_t[256] ) );
517 memcpy( &p_context->codebook_v4[i_strip],
518 &p_context->codebook_v4[i_strip-1],
519 sizeof(cinepak_codebook_t[256] ) );
522 /* Now parse all chunk in this strip */
523 while( i_strip_size > 0 )
525 cinepak_codebook_t (*p_codebook)[CINEPAK_MAXSTRIP][256];
533 int i_x, i_y; /* (0,0) begin in fact at (x1,y1) ... */
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;
544 if( i_chunk_size < 0 )
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;
560 i_count = __MIN( i_chunk_size / ( i_mode ? 6 : 4 ), 256 );
562 for( i = 0; i < i_count; i++ )
564 cinepak_LoadCodebook( &((*p_codebook)[i_strip][i]),
566 i_mode&~p_context->b_grayscale );
567 p_data += i_mode ? 6 : 4;
568 i_chunk_size -= i_mode ? 6 : 4;
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;
582 while( (i_chunk_size > 4)&&(i_index<256))
584 i_vector_flags = GET4BYTES( p_data );
586 for( i = 0; i < 32; i++ )
588 if( ( i_chunk_size < ( i_mode ? 6 : 4 ) )||(i_index >= 256 ))
592 if( i_vector_flags&0x80000000UL )
594 cinepak_LoadCodebook( &((*p_codebook)[i_strip][i_index]),
596 i_mode&~p_context->b_grayscale );
598 p_data += i_mode ? 6 : 4;
599 i_chunk_size -= i_mode ? 6 : 4;
602 i_vector_flags <<= 1;
607 case( 0x3000 ): /* load image Intra */
608 while( (i_chunk_size >= 4 )&&(i_y<i_strip_y2-i_strip_y1) )
610 i_vector_flags = GET4BYTES( p_data );
615 for( i = 0; i < 32; i++ )
617 if( ( i_y >= i_strip_y2 - i_strip_y1)||
622 if( i_vector_flags&0x80000000UL )
624 cinepak_Getv4( p_context,
628 i_strip_x2, i_strip_y2,
635 cinepak_Getv1( p_context,
639 i_strip_x2, i_strip_y2,
646 if( i_x >= i_strip_x2 - i_strip_x1 )
651 i_vector_flags <<= 1;
656 case( 0x3100 ): /* load image Inter */
657 while( ( i_chunk_size > 4 )&&( i_y < i_strip_y2 - i_strip_y1) )
660 i_vector_flags = GET4BYTES( p_data );
662 i_mask = 0x80000000UL;
664 while((i_chunk_size > 0 )&&( i_mask )&&( i_y < i_strip_y2 - i_strip_y1 ))
666 if( i_vector_flags&i_mask)
671 if( i_chunk_size < 4 )
675 i_vector_flags = GET4BYTES( p_data );
677 i_mask = 0x80000000UL;
679 if( i_vector_flags&i_mask )
681 if( i_chunk_size < 4 ) break;
682 cinepak_Getv4( p_context,
686 i_strip_x2, i_strip_y2,
693 if( i_chunk_size < 1 ) break;
694 cinepak_Getv1( p_context,
698 i_strip_x2, i_strip_y2,
707 if( i_x >= i_strip_x2 - i_strip_x1 )
716 case( 0x3200 ): /* load intra picture but all v1*/
717 while( ( i_chunk_size > 0 )&&
718 ( i_y < i_strip_y2 - i_strip_y1) )
720 cinepak_Getv1( p_context,
724 i_strip_x2, i_strip_y2,
730 if( i_x >= i_strip_x2 - i_strip_x1 )
745 p_data += i_chunk_size ; /* skip remains bytes */
755 /*****************************************************************************
757 * Functions that initialize, decode and end the decoding process
759 *****************************************************************************/
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 *****************************************************************************/
769 static int InitThread( videodec_thread_t *p_vdec )
772 /* This will be created after the first decoded frame */
773 if( !(p_vdec->p_context = malloc( sizeof( cinepak_context_t ) ) ) )
775 msg_Err( p_vdec->p_fifo, "out of memory" );
777 memset( p_vdec->p_context, 0, sizeof( cinepak_context_t ) );
779 if( config_GetInt( p_vdec->p_fifo, "grayscale" ) )
781 p_vdec->p_context->b_grayscale = 1;
785 p_vdec->p_context->b_grayscale = 0;
788 p_vdec->p_vout = NULL;
789 msg_Dbg( p_vdec->p_fifo, "cinepak decoder started" );
794 /*****************************************************************************
795 * DecodeThread: Called for decode one frame
796 *****************************************************************************/
797 static void DecodeThread( videodec_thread_t *p_vdec )
805 picture_t *p_pic; /* videolan picture */
809 input_ExtractPES( p_vdec->p_fifo, &p_pes );
812 p_vdec->p_fifo->b_error = 1;
815 p_vdec->i_pts = p_pes->i_pts;
816 i_frame_size = p_pes->i_pes_size;
818 if( i_frame_size > 0 )
820 if( p_vdec->i_buffer < i_frame_size + 16 )
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;
827 GetPESData( p_vdec->p_buffer, p_vdec->i_buffer, p_pes );
829 input_DeletePES( p_vdec->p_fifo->p_packets_mgt, p_pes );
830 } while( i_frame_size <= 0 );
833 i_status = cinepak_decode_frame( p_vdec->p_context,
839 msg_Warn( p_vdec->p_fifo, "cannot decode one frame (%d bytes)",
845 if( !cinepak_CheckVout( p_vdec->p_vout,
846 p_vdec->p_context->i_width,
847 p_vdec->p_context->i_height ) )
850 cinepak_CreateVout( p_vdec,
851 p_vdec->p_context->i_width,
852 p_vdec->p_context->i_height );
854 if( !p_vdec->p_vout )
856 msg_Err( p_vdec->p_fifo, "cannot create vout" );
857 p_vdec->p_fifo->b_error = 1; /* abort */
862 /* Send decoded frame to vout */
863 while( !(p_pic = vout_CreatePicture( p_vdec->p_vout, 0, 0, 0 ) ) )
865 if( p_vdec->p_fifo->b_die || p_vdec->p_fifo->b_error )
869 msleep( VOUT_OUTMEM_SLEEP );
872 for( i_plane = 0; i_plane < 3; i_plane++ )
876 p_dst = p_pic->p[i_plane].p_pixels;
877 p_src = p_vdec->p_context->p_pix[i_plane];
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++ )
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];
892 vout_DatePicture( p_vdec->p_vout, p_pic, p_vdec->i_pts);
893 vout_DisplayPicture( p_vdec->p_vout, p_pic );
899 /*****************************************************************************
900 * EndThread: thread destruction
901 *****************************************************************************
902 * This function is called when the thread ends after a sucessful
904 *****************************************************************************/
905 static void EndThread( videodec_thread_t *p_vdec )
913 msg_Dbg( p_vdec->p_fifo, "cinepak decoder stopped" );
915 for( i = 0; i < 3; i++ )
917 FREE( p_vdec->p_context->p_pix[i] );
920 free( p_vdec->p_context );
922 if( p_vdec->p_vout != NULL )
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 );