1 /*****************************************************************************
2 * cinepak.c: cinepak video decoder
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VideoLAN
5 * $Id: cinepak.c,v 1.1 2002/08/04 17:23:42 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>
45 #include "vdec_ext-plugins.h"
48 /*****************************************************************************
50 *****************************************************************************/
51 static int OpenDecoder ( vlc_object_t * );
52 static int RunDecoder ( decoder_fifo_t * );
53 static int InitThread ( videodec_thread_t * );
54 static void EndThread ( videodec_thread_t * );
55 static void DecodeThread ( videodec_thread_t * );
57 /*****************************************************************************
59 *****************************************************************************/
61 set_description( "Cinepak video decoder" );
62 set_capability( "decoder", 70 );
63 set_callbacks( OpenDecoder, NULL );
66 /*****************************************************************************
67 * OpenDecoder: probe the decoder and return score
68 *****************************************************************************
69 * Tries to launch a decoder and return score so that the interface is able
71 *****************************************************************************/
72 static int OpenDecoder( vlc_object_t *p_this )
74 decoder_fifo_t *p_fifo = (decoder_fifo_t*) p_this;
76 switch( p_fifo->i_fourcc )
78 case VLC_FOURCC('c','v','i','d'):
79 case VLC_FOURCC('C','V','I','D'):
80 p_fifo->pf_run = RunDecoder;
87 /*****************************************************************************
88 * RunDecoder: this function is called just after the thread is created
89 *****************************************************************************/
90 static int RunDecoder( decoder_fifo_t *p_fifo )
92 videodec_thread_t *p_vdec;
95 if ( !(p_vdec = (videodec_thread_t*)malloc( sizeof(videodec_thread_t))) )
97 msg_Err( p_fifo, "out of memory" );
98 DecoderError( p_fifo );
101 memset( p_vdec, 0, sizeof( videodec_thread_t ) );
103 p_vdec->p_fifo = p_fifo;
105 if( InitThread( p_vdec ) != 0 )
107 DecoderError( p_fifo );
111 while( (!p_vdec->p_fifo->b_die) && (!p_vdec->p_fifo->b_error) )
113 DecodeThread( p_vdec );
116 if( ( b_error = p_vdec->p_fifo->b_error ) )
118 DecoderError( p_vdec->p_fifo );
132 /*****************************************************************************
134 *****************************************************************************/
136 static inline u16 GetWBE( u8 *p_buff )
138 return( (p_buff[0]<<8) + p_buff[1] );
141 static inline u32 GetDWBE( u8 *p_buff )
143 return( (p_buff[0] << 24) + ( p_buff[1] <<16 ) +
144 ( p_buff[2] <<8 ) + p_buff[3] );
147 #define GET2BYTES( p ) \
150 #define GET3BYTES( p ) \
151 (GetDWBE( p ) >> 8); p+= 3;
153 #define GET4BYTES( p ) \
159 /* get the first pes from fifo */
160 static pes_packet_t *__PES_GET( decoder_fifo_t *p_fifo )
164 vlc_mutex_lock( &p_fifo->data_lock );
166 /* if fifo is emty wait */
167 while( !p_fifo->p_first )
171 vlc_mutex_unlock( &p_fifo->data_lock );
174 vlc_cond_wait( &p_fifo->data_wait, &p_fifo->data_lock );
176 p_pes = p_fifo->p_first;
178 vlc_mutex_unlock( &p_fifo->data_lock );
183 /* free the first pes and go to next */
184 static void __PES_NEXT( decoder_fifo_t *p_fifo )
186 pes_packet_t *p_next;
188 vlc_mutex_lock( &p_fifo->data_lock );
190 p_next = p_fifo->p_first->p_next;
191 p_fifo->p_first->p_next = NULL;
192 input_DeletePES( p_fifo->p_packets_mgt, p_fifo->p_first );
193 p_fifo->p_first = p_next;
196 if( !p_fifo->p_first )
198 /* No PES in the fifo */
199 /* pp_last no longer valid */
200 p_fifo->pp_last = &p_fifo->p_first;
201 while( !p_fifo->p_first )
203 vlc_cond_signal( &p_fifo->data_wait );
204 vlc_cond_wait( &p_fifo->data_wait, &p_fifo->data_lock );
207 vlc_mutex_unlock( &p_fifo->data_lock );
210 static inline void __GetFrame( videodec_thread_t *p_vdec )
213 data_packet_t *p_data;
216 p_pes = __PES_GET( p_vdec->p_fifo );
217 p_vdec->i_pts = p_pes->i_pts;
219 while( ( !p_pes->i_nb_data )||( !p_pes->i_pes_size ) )
221 __PES_NEXT( p_vdec->p_fifo );
222 p_pes = __PES_GET( p_vdec->p_fifo );
224 p_vdec->i_framesize = p_pes->i_pes_size;
225 if( p_pes->i_nb_data == 1 )
227 p_vdec->p_framedata = p_pes->p_first->p_payload_start;
230 /* get a buffer and gather all data packet */
231 p_vdec->p_framedata = p_buffer = malloc( p_pes->i_pes_size );
232 p_data = p_pes->p_first;
235 p_vdec->p_fifo->p_vlc->pf_memcpy( p_buffer, p_data->p_payload_start,
236 p_data->p_payload_end - p_data->p_payload_start );
237 p_buffer += p_data->p_payload_end - p_data->p_payload_start;
238 p_data = p_data->p_next;
242 static inline void __NextFrame( videodec_thread_t *p_vdec )
246 p_pes = __PES_GET( p_vdec->p_fifo );
247 if( p_pes->i_nb_data != 1 )
249 free( p_vdec->p_framedata ); /* FIXME keep this buffer */
251 __PES_NEXT( p_vdec->p_fifo );
254 static int cinepak_CheckVout( vout_thread_t *p_vout,
263 if( ( p_vout->render.i_width != i_width )||
264 ( p_vout->render.i_height != i_height )||
265 ( p_vout->render.i_chroma != VLC_FOURCC('I','4','2','0') )||
266 ( p_vout->render.i_aspect != VOUT_ASPECT_FACTOR * i_width / i_height) )
278 static vout_thread_t *cinepak_CreateVout( videodec_thread_t *p_vdec,
282 vout_thread_t *p_vout;
284 if( (!i_width)||(!i_height) )
286 return( NULL ); /* Can't create a new vout without display size */
289 /* Spawn a video output if there is none. First we look for our children,
290 * then we look for any other vout that might be available. */
291 p_vout = vlc_object_find( p_vdec->p_fifo, VLC_OBJECT_VOUT,
295 p_vout = vlc_object_find( p_vdec->p_fifo, VLC_OBJECT_VOUT,
301 if( !cinepak_CheckVout( p_vout, i_width, i_height ) )
303 /* We are not interested in this format, close this vout */
304 vlc_object_detach_all( p_vout );
305 vlc_object_release( p_vout );
306 vout_DestroyThread( p_vout );
311 /* This video output is cool! Hijack it. */
312 vlc_object_detach_all( p_vout );
313 vlc_object_attach( p_vout, p_vdec->p_fifo );
314 vlc_object_release( p_vout );
320 msg_Dbg( p_vdec->p_fifo, "no vout present, spawning one" );
322 p_vout = vout_CreateThread( p_vdec->p_fifo,
325 VLC_FOURCC('I','4','2','0'),
326 VOUT_ASPECT_FACTOR * i_width / i_height );
332 void cinepak_LoadCodebook( cinepak_codebook_t *p_codebook,
336 int i, i_y[4], i_u, i_v, i_Cb, i_Cr;
339 #define FIX( x ) ( (int)( (x) * ( 1L << SCALEBITS ) + 0.5 ) )
341 for( i = 0; i < 4; i++ )
343 i_y[i] = (u8)( *(p_data++) );
347 i_u = (s8)( *(p_data++) );
348 i_v = (s8)( *(p_data++) );
357 | Y | | 1 -0.0655 0.0110 | | CY |
358 | Cb | = | 0 1.1656 -0.0062 | | CU |
359 | Cr | | 0 0.0467 1.4187 | | CV |
361 i_uv = ( FIX( -0.0655 ) * i_u + FIX( 0.0110 ) * i_v ) >> SCALEBITS;
362 for( i = 0; i < 4; i++ )
366 i_Cb = ( FIX( 1.1656 ) * i_u + FIX( -0.0062 ) * i_v ) >> SCALEBITS;
367 i_Cr = ( FIX( 0.0467 ) * i_u + FIX( 1.4187 ) * i_v ) >> SCALEBITS;
369 for( i = 0; i < 4; i++ )
371 p_codebook->i_y[i] = __MIN( __MAX( 0, i_y[i] ), 255 );
373 p_codebook->i_u = __MIN( __MAX( 0, i_Cb + 128 ), 255 );
374 p_codebook->i_v = __MIN( __MAX( 0, i_Cr + 128 ), 255 );
380 void cinepak_Getv4( cinepak_context_t *p_context,
389 u8 *p_dst_y, *p_dst_u, *p_dst_v;
390 #define PIX_SET_Y( x, y, v ) \
391 p_dst_y[(x) + (y)* p_context->i_stride[0]] = (v);
393 #define PIX_SET_UV( i, p, x, y, v ) \
394 p[(x) + (y)* (p_context->i_stride[i])] = (v);
396 for( i = 0; i < 4; i++ )
398 i_index[i] = *(p_data++);
402 p_dst_y = p_context->p_pix[0] + p_context->i_stride[0] * i_y + i_x;
403 p_dst_u = p_context->p_pix[1] + p_context->i_stride[1] * (i_y/2) + (i_x/2);
404 p_dst_v = p_context->p_pix[2] + p_context->i_stride[2] * (i_y/2) + (i_x/2);
406 for( i = 0; i < 2; i++ )
408 for( j = 0; j < 2; j ++ )
410 PIX_SET_Y( 2*j + 0, 2*i + 0,
411 p_context->codebook_v4[i_strip][i_index[j+2*i]].i_y[0]);
412 PIX_SET_Y( 2*j + 1, 2*i + 0,
413 p_context->codebook_v4[i_strip][i_index[j+2*i]].i_y[1]);
414 PIX_SET_Y( 2*j + 0, 2*i + 1,
415 p_context->codebook_v4[i_strip][i_index[j+2*i]].i_y[2]);
416 PIX_SET_Y( 2*j + 1, 2*i + 1,
417 p_context->codebook_v4[i_strip][i_index[j+2*i]].i_y[3]);
419 PIX_SET_UV( 1, p_dst_u, j, i,
420 p_context->codebook_v4[i_strip][i_index[j+2*i]].i_u );
421 PIX_SET_UV( 2, p_dst_v, j, i,
422 p_context->codebook_v4[i_strip][i_index[j+2*i]].i_v );
429 void cinepak_Getv1( cinepak_context_t *p_context,
438 u8 *p_dst_y, *p_dst_u, *p_dst_v;
439 #define PIX_SET_Y( x, y, v ) \
440 p_dst_y[(x) + (y)* p_context->i_stride[0]] = (v);
442 #define PIX_SET_UV( i,p, x, y, v ) \
443 p[(x) + (y)* (p_context->i_stride[i])] = (v);
445 i_index = *(p_data++);
448 p_dst_y = p_context->p_pix[0] + p_context->i_stride[0] * i_y + i_x;
449 p_dst_u = p_context->p_pix[1] + p_context->i_stride[1] * (i_y/2) + (i_x/2);
450 p_dst_v = p_context->p_pix[2] + p_context->i_stride[2] * (i_y/2) + (i_x/2);
452 for( i = 0; i < 2; i++ )
454 for( j = 0; j < 2; j ++ )
456 PIX_SET_Y( 2*j + 0, 2*i + 0,
457 p_context->codebook_v1[i_strip][i_index].i_y[0] );
458 PIX_SET_Y( 2*j + 1, 2*i + 0,
459 p_context->codebook_v1[i_strip][i_index].i_y[1] );
460 PIX_SET_Y( 2*j + 0, 2*i + 1,
461 p_context->codebook_v1[i_strip][i_index].i_y[2] );
462 PIX_SET_Y( 2*j + 1, 2*i + 1,
463 p_context->codebook_v1[i_strip][i_index].i_y[3] );
465 PIX_SET_UV( 1,p_dst_u, j, i,
466 p_context->codebook_v1[i_strip][i_index].i_u );
467 PIX_SET_UV( 2,p_dst_v, j, i,
468 p_context->codebook_v1[i_strip][i_index].i_v );
476 /*****************************************************************************
477 * The function that decode one frame
478 *****************************************************************************/
479 int cinepak_decode_frame( cinepak_context_t *p_context,
480 int i_length, u8 *p_data )
486 int i_width, i_height;
489 int i_strip_x1 =0, i_strip_y1=0;
490 int i_strip_x2 =0, i_strip_y2=0;
494 /* Broken header or no data */
499 i_frame_flags = *(p_data++);
500 i_frame_size = GET3BYTES( p_data );
501 i_width = GET2BYTES( p_data );
502 i_height = GET2BYTES( p_data );
503 i_frame_strips = GET2BYTES( p_data );
505 /* Check if we have a picture buffer with good size */
506 if( ( p_context->i_width != i_width )||
507 ( p_context->i_height != i_height ) )
510 for( i = 0; i < 3; i++ )
512 FREE( p_context->p_pix[i] );
515 p_context->i_width = i_width;
516 p_context->i_height = i_height;
518 p_context->i_stride[0] = ( i_width + 3)&0xfffc;
519 p_context->i_stride[1] = p_context->i_stride[2] =
520 p_context->i_stride[0] / 2;
522 p_context->i_lines[0] = ( i_height + 3 )&0xfffc;
523 p_context->i_lines[1] = p_context->i_lines[2] =
524 p_context->i_lines[0] /2;
526 for( i = 0; i < 3; i++ )
528 p_context->p_pix[i] = malloc( p_context->i_stride[i] *
529 p_context->i_lines[i] );
533 if( i_frame_size != i_length )
535 i_length = __MIN( i_length, i_frame_size );
539 if( i_frame_strips >= CINEPAK_MAXSTRIP )
541 i_frame_strips = CINEPAK_MAXSTRIP;
544 /* Now decode each strip */
546 for( i_strip = 0; i_strip < i_frame_strips; i_strip++ )
557 i_strip_id = GET2BYTES( p_data );
558 i_strip_size = GET2BYTES( p_data );
559 i_strip_size = __MIN( i_strip_size, i_length );
560 /* FIXME I don't really understand how it's work; */
561 i_strip_y1 = i_strip_y2 + GET2BYTES( p_data );
562 i_strip_x1 = GET2BYTES( p_data );
563 i_strip_y2 = i_strip_y2 + GET2BYTES( p_data );
564 i_strip_x2 = GET2BYTES( p_data );
566 i_length -= i_strip_size;
569 /* init codebook , if needed */
570 if( ( i_strip > 0 )&&( !(i_frame_flags&0x01) ) )
572 memcpy( &p_context->codebook_v1[i_strip],
573 &p_context->codebook_v1[i_strip-1],
574 sizeof(cinepak_codebook_t[256] ) );
576 memcpy( &p_context->codebook_v4[i_strip],
577 &p_context->codebook_v4[i_strip-1],
578 sizeof(cinepak_codebook_t[256] ) );
581 /* Now parse all chunk in this strip */
582 while( i_strip_size > 0 )
584 cinepak_codebook_t (*p_codebook)[CINEPAK_MAXSTRIP][256];
592 int i_x, i_y; /* (0,0) begin in fact at (x1,y1) ... */
594 i_chunk_id = GET2BYTES( p_data );
595 i_chunk_size = GET2BYTES( p_data );
596 i_chunk_size = __MIN( i_chunk_size, i_strip_size );
597 i_strip_size -= i_chunk_size;
603 if( i_chunk_size < 0 )
610 case( 0x2000 ): /* 12bits v4 Intra*/
611 case( 0x2200 ): /* 12bits v1 Intra*/
612 case( 0x2400 ): /* 8bits v4 Intra*/
613 case( 0x2600 ): /* 8bits v1 Intra */
614 i_mode = ( ( i_chunk_id&0x0400 ) == 0 );
615 p_codebook = ( i_chunk_id&0x0200 ) ?
616 &p_context->codebook_v1 :
617 &p_context->codebook_v4;
619 i_count = __MIN( i_chunk_size / ( i_mode ? 6 : 4 ), 256 );
621 for( i = 0; i < i_count; i++ )
623 cinepak_LoadCodebook( &((*p_codebook)[i_strip][i]),
625 i_mode&~p_context->b_grayscale );
626 p_data += i_mode ? 6 : 4;
627 i_chunk_size -= i_mode ? 6 : 4;
631 case( 0x2100 ): /* selective 12bits v4 Inter*/
632 case( 0x2300 ): /* selective 12bits v1 Inter*/
633 case( 0x2500 ): /* selective 8bits v4 Inter*/
634 case( 0x2700 ): /* selective 8bits v1 Inter*/
635 i_mode = ( ( i_chunk_id&0x0400 ) == 0 );
636 p_codebook = ( i_chunk_id&0x0200 ) ?
637 &p_context->codebook_v1 :
638 &p_context->codebook_v4;
641 while( (i_chunk_size > 4)&&(i_index<256))
643 i_vector_flags = GET4BYTES( p_data );
645 for( i = 0; i < 32; i++ )
647 if( ( i_chunk_size < ( i_mode ? 6 : 4 ) )||(i_index >= 256 ))
651 if( i_vector_flags&0x80000000UL )
653 cinepak_LoadCodebook( &((*p_codebook)[i_strip][i_index]),
655 i_mode&~p_context->b_grayscale );
657 p_data += i_mode ? 6 : 4;
658 i_chunk_size -= i_mode ? 6 : 4;
661 i_vector_flags <<= 1;
666 case( 0x3000 ): /* load image Intra */
667 while( (i_chunk_size >= 4 )&&(i_y<i_strip_y2-i_strip_y1) )
669 i_vector_flags = GET4BYTES( p_data );
674 for( i = 0; i < 32; i++ )
676 if( ( i_y >= i_strip_y2 - i_strip_y1)||
681 if( i_vector_flags&0x80000000UL )
683 cinepak_Getv4( p_context,
687 i_strip_x2, i_strip_y2,
694 cinepak_Getv1( p_context,
698 i_strip_x2, i_strip_y2,
705 if( i_x >= i_strip_x2 - i_strip_x1 )
710 i_vector_flags <<= 1;
715 case( 0x3100 ): /* load image Inter */
716 while( ( i_chunk_size > 4 )&&( i_y < i_strip_y2 - i_strip_y1) )
719 i_vector_flags = GET4BYTES( p_data );
721 i_mask = 0x80000000UL;
723 while((i_chunk_size > 0 )&&( i_mask )&&( i_y < i_strip_y2 - i_strip_y1 ))
725 if( i_vector_flags&i_mask)
730 if( i_chunk_size < 4 )
734 i_vector_flags = GET4BYTES( p_data );
736 i_mask = 0x80000000UL;
738 if( i_vector_flags&i_mask )
740 if( i_chunk_size < 4 ) break;
741 cinepak_Getv4( p_context,
745 i_strip_x2, i_strip_y2,
752 if( i_chunk_size < 1 ) break;
753 cinepak_Getv1( p_context,
757 i_strip_x2, i_strip_y2,
766 if( i_x >= i_strip_x2 - i_strip_x1 )
775 case( 0x3200 ): /* load intra picture but all v1*/
776 while( ( i_chunk_size > 0 )&&
777 ( i_y < i_strip_y2 - i_strip_y1) )
779 cinepak_Getv1( p_context,
783 i_strip_x2, i_strip_y2,
789 if( i_x >= i_strip_x2 - i_strip_x1 )
804 p_data += i_chunk_size ; /* skip remains bytes */
814 /*****************************************************************************
816 * Functions that initialize, decode and end the decoding process
818 *****************************************************************************/
820 /*****************************************************************************
821 * InitThread: initialize vdec output thread
822 *****************************************************************************
823 * This function is called from decoder_Run and performs the second step
824 * of the initialization. It returns 0 on success. Note that the thread's
825 * flag are not modified inside this function.
826 *****************************************************************************/
828 static int InitThread( videodec_thread_t *p_vdec )
831 /* This will be created after the first decoded frame */
832 if( !(p_vdec->p_context = malloc( sizeof( cinepak_context_t ) ) ) )
834 msg_Err( p_vdec->p_fifo, "out of memory" );
836 memset( p_vdec->p_context, 0, sizeof( cinepak_context_t ) );
838 if( config_GetInt( p_vdec->p_fifo, "grayscale" ) )
840 p_vdec->p_context->b_grayscale = 1;
844 p_vdec->p_context->b_grayscale = 0;
847 p_vdec->p_vout = NULL;
848 msg_Dbg( p_vdec->p_fifo, "cinepak decoder started" );
853 /*****************************************************************************
854 * DecodeThread: Called for decode one frame
855 *****************************************************************************/
856 static void DecodeThread( videodec_thread_t *p_vdec )
862 picture_t *p_pic; /* videolan picture */
864 __GetFrame( p_vdec );
866 i_status = cinepak_decode_frame( p_vdec->p_context,
868 p_vdec->p_framedata );
869 __NextFrame( p_vdec );
873 msg_Warn( p_vdec->p_fifo, "cannot decode one frame (%d bytes)",
874 p_vdec->i_framesize );
879 if( !cinepak_CheckVout( p_vdec->p_vout,
880 p_vdec->p_context->i_width,
881 p_vdec->p_context->i_height ) )
884 cinepak_CreateVout( p_vdec,
885 p_vdec->p_context->i_width,
886 p_vdec->p_context->i_height );
888 if( !p_vdec->p_vout )
890 msg_Err( p_vdec->p_fifo, "cannot create vout" );
891 p_vdec->p_fifo->b_error = 1; /* abort */
896 /* Send decoded frame to vout */
897 while( !(p_pic = vout_CreatePicture( p_vdec->p_vout, 0, 0, 0 ) ) )
899 if( p_vdec->p_fifo->b_die || p_vdec->p_fifo->b_error )
903 msleep( VOUT_OUTMEM_SLEEP );
906 for( i_plane = 0; i_plane < 3; i_plane++ )
910 p_dst = p_pic->p[i_plane].p_pixels;
911 p_src = p_vdec->p_context->p_pix[i_plane];
913 i_lines = __MIN( p_vdec->p_context->i_lines[i_plane],
914 p_pic->p[i_plane].i_lines );
915 for( i_line = 0; i_line < i_lines; i_line++ )
919 __MIN( p_pic->p[i_plane].i_pitch,
920 p_vdec->p_context->i_stride[i_plane] ) );
921 p_dst += p_pic->p[i_plane].i_pitch;
922 p_src += p_vdec->p_context->i_stride[i_plane];
926 vout_DatePicture( p_vdec->p_vout, p_pic, p_vdec->i_pts);
927 vout_DisplayPicture( p_vdec->p_vout, p_pic );
933 /*****************************************************************************
934 * EndThread: thread destruction
935 *****************************************************************************
936 * This function is called when the thread ends after a sucessful
938 *****************************************************************************/
939 static void EndThread( videodec_thread_t *p_vdec )
947 msg_Dbg( p_vdec->p_fifo, "cinepak decoder stopped" );
949 for( i = 0; i < 3; i++ )
951 FREE( p_vdec->p_context->p_pix[i] );
954 free( p_vdec->p_context );
956 if( p_vdec->p_vout != NULL )
958 /* We are about to die. Reattach video output to p_vlc. */
959 vlc_object_detach( p_vdec->p_vout, p_vdec->p_fifo );
960 vlc_object_attach( p_vdec->p_vout, p_vdec->p_fifo->p_vlc );