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 $
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 );
193 void cinepak_LoadCodebook( cinepak_codebook_t *p_codebook,
197 int i, i_y[4], i_u, i_v, i_Cb, i_Cr;
200 #define FIX( x ) ( (int)( (x) * ( 1L << SCALEBITS ) + 0.5 ) )
202 for( i = 0; i < 4; i++ )
204 i_y[i] = (u8)( *(p_data++) );
208 i_u = (s8)( *(p_data++) );
209 i_v = (s8)( *(p_data++) );
218 | Y | | 1 -0.0655 0.0110 | | CY |
219 | Cb | = | 0 1.1656 -0.0062 | | CU |
220 | Cr | | 0 0.0467 1.4187 | | CV |
222 i_uv = ( FIX( -0.0655 ) * i_u + FIX( 0.0110 ) * i_v ) >> SCALEBITS;
223 for( i = 0; i < 4; i++ )
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;
230 for( i = 0; i < 4; i++ )
232 p_codebook->i_y[i] = __MIN( __MAX( 0, i_y[i] ), 255 );
234 p_codebook->i_u = __MIN( __MAX( 0, i_Cb + 128 ), 255 );
235 p_codebook->i_v = __MIN( __MAX( 0, i_Cr + 128 ), 255 );
241 void cinepak_Getv4( cinepak_context_t *p_context,
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);
254 #define PIX_SET_UV( i, p, x, y, v ) \
255 p[(x) + (y)* (p_context->i_stride[i])] = (v);
257 for( i = 0; i < 4; i++ )
259 i_index[i] = *(p_data++);
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);
267 for( i = 0; i < 2; i++ )
269 for( j = 0; j < 2; j ++ )
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]);
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 );
290 void cinepak_Getv1( cinepak_context_t *p_context,
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);
303 #define PIX_SET_UV( i,p, x, y, v ) \
304 p[(x) + (y)* (p_context->i_stride[i])] = (v);
306 i_index = *(p_data++);
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);
313 for( i = 0; i < 2; i++ )
315 for( j = 0; j < 2; j ++ )
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] );
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 );
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 )
347 int i_width, i_height;
350 int i_strip_x1 =0, i_strip_y1=0;
351 int i_strip_x2 =0, i_strip_y2=0;
355 /* Broken header or no data */
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 );
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 ) )
371 for( i = 0; i < 3; i++ )
373 FREE( p_context->p_pix[i] );
376 p_context->i_width = i_width;
377 p_context->i_height = i_height;
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;
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;
387 for( i = 0; i < 3; i++ )
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] );
397 if( i_frame_size != i_length )
399 i_length = __MIN( i_length, i_frame_size );
403 if( i_frame_strips >= CINEPAK_MAXSTRIP )
405 i_frame_strips = CINEPAK_MAXSTRIP;
408 /* Now decode each strip */
410 for( i_strip = 0; i_strip < i_frame_strips; i_strip++ )
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 );
430 i_length -= i_strip_size;
433 /* init codebook , if needed */
434 if( ( i_strip > 0 )&&( !(i_frame_flags&0x01) ) )
436 memcpy( &p_context->codebook_v1[i_strip],
437 &p_context->codebook_v1[i_strip-1],
438 sizeof(cinepak_codebook_t[256] ) );
440 memcpy( &p_context->codebook_v4[i_strip],
441 &p_context->codebook_v4[i_strip-1],
442 sizeof(cinepak_codebook_t[256] ) );
445 /* Now parse all chunk in this strip */
446 while( i_strip_size > 0 )
448 cinepak_codebook_t (*p_codebook)[CINEPAK_MAXSTRIP][256];
456 int i_x, i_y; /* (0,0) begin in fact at (x1,y1) ... */
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;
467 if( i_chunk_size < 0 )
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;
483 i_count = __MIN( i_chunk_size / ( i_mode ? 6 : 4 ), 256 );
485 for( i = 0; i < i_count; i++ )
487 cinepak_LoadCodebook( &((*p_codebook)[i_strip][i]),
489 i_mode&~p_context->b_grayscale );
490 p_data += i_mode ? 6 : 4;
491 i_chunk_size -= i_mode ? 6 : 4;
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;
505 while( (i_chunk_size > 4)&&(i_index<256))
507 i_vector_flags = GET4BYTES( p_data );
509 for( i = 0; i < 32; i++ )
511 if( ( i_chunk_size < ( i_mode ? 6 : 4 ) )||(i_index >= 256 ))
515 if( i_vector_flags&0x80000000UL )
517 cinepak_LoadCodebook( &((*p_codebook)[i_strip][i_index]),
519 i_mode&~p_context->b_grayscale );
521 p_data += i_mode ? 6 : 4;
522 i_chunk_size -= i_mode ? 6 : 4;
525 i_vector_flags <<= 1;
530 case( 0x3000 ): /* load image Intra */
531 while( (i_chunk_size >= 4 )&&(i_y<i_strip_y2-i_strip_y1) )
533 i_vector_flags = GET4BYTES( p_data );
538 for( i = 0; i < 32; i++ )
540 if( ( i_y >= i_strip_y2 - i_strip_y1)||
545 if( i_vector_flags&0x80000000UL )
547 cinepak_Getv4( p_context,
551 i_strip_x2, i_strip_y2,
558 cinepak_Getv1( p_context,
562 i_strip_x2, i_strip_y2,
569 if( i_x >= i_strip_x2 - i_strip_x1 )
574 i_vector_flags <<= 1;
579 case( 0x3100 ): /* load image Inter */
580 while( ( i_chunk_size > 4 )&&( i_y < i_strip_y2 - i_strip_y1) )
583 i_vector_flags = GET4BYTES( p_data );
585 i_mask = 0x80000000UL;
587 while((i_chunk_size > 0 )&&( i_mask )&&( i_y < i_strip_y2 - i_strip_y1 ))
589 if( i_vector_flags&i_mask)
594 if( i_chunk_size < 4 )
598 i_vector_flags = GET4BYTES( p_data );
600 i_mask = 0x80000000UL;
602 if( i_vector_flags&i_mask )
604 if( i_chunk_size < 4 ) break;
605 cinepak_Getv4( p_context,
609 i_strip_x2, i_strip_y2,
616 if( i_chunk_size < 1 ) break;
617 cinepak_Getv1( p_context,
621 i_strip_x2, i_strip_y2,
630 if( i_x >= i_strip_x2 - i_strip_x1 )
639 case( 0x3200 ): /* load intra picture but all v1*/
640 while( ( i_chunk_size > 0 )&&
641 ( i_y < i_strip_y2 - i_strip_y1) )
643 cinepak_Getv1( p_context,
647 i_strip_x2, i_strip_y2,
653 if( i_x >= i_strip_x2 - i_strip_x1 )
668 p_data += i_chunk_size ; /* skip remains bytes */
678 /*****************************************************************************
680 * Functions that initialize, decode and end the decoding process
682 *****************************************************************************/
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 *****************************************************************************/
692 static int InitThread( videodec_thread_t *p_vdec )
695 /* This will be created after the first decoded frame */
696 if( !(p_vdec->p_context = malloc( sizeof( cinepak_context_t ) ) ) )
698 msg_Err( p_vdec->p_fifo, "out of memory" );
700 memset( p_vdec->p_context, 0, sizeof( cinepak_context_t ) );
702 if( config_GetInt( p_vdec->p_fifo, "grayscale" ) )
704 p_vdec->p_context->b_grayscale = 1;
708 p_vdec->p_context->b_grayscale = 0;
711 p_vdec->p_vout = NULL;
712 msg_Dbg( p_vdec->p_fifo, "cinepak decoder started" );
717 /*****************************************************************************
718 * DecodeThread: Called for decode one frame
719 *****************************************************************************/
720 static void DecodeThread( videodec_thread_t *p_vdec )
728 picture_t *p_pic; /* videolan picture */
732 input_ExtractPES( p_vdec->p_fifo, &p_pes );
735 p_vdec->p_fifo->b_error = 1;
738 p_vdec->i_pts = p_pes->i_pts;
739 i_frame_size = p_pes->i_pes_size;
741 if( i_frame_size > 0 )
743 if( p_vdec->i_buffer < i_frame_size + 16 )
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;
750 GetPESData( p_vdec->p_buffer, p_vdec->i_buffer, p_pes );
752 input_DeletePES( p_vdec->p_fifo->p_packets_mgt, p_pes );
753 } while( i_frame_size <= 0 );
756 i_status = cinepak_decode_frame( p_vdec->p_context,
762 msg_Warn( p_vdec->p_fifo, "cannot decode one frame (%d bytes)",
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
774 / p_vdec->p_context->i_height );
776 if( !p_vdec->p_vout )
778 msg_Err( p_vdec->p_fifo, "cannot create vout" );
779 p_vdec->p_fifo->b_error = VLC_TRUE; /* abort */
783 /* Send decoded frame to vout */
784 while( !(p_pic = vout_CreatePicture( p_vdec->p_vout, 0, 0, 0 ) ) )
786 if( p_vdec->p_fifo->b_die || p_vdec->p_fifo->b_error )
790 msleep( VOUT_OUTMEM_SLEEP );
793 for( i_plane = 0; i_plane < 3; i_plane++ )
797 p_dst = p_pic->p[i_plane].p_pixels;
798 p_src = p_vdec->p_context->p_pix[i_plane];
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++ )
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];
813 vout_DatePicture( p_vdec->p_vout, p_pic, p_vdec->i_pts);
814 vout_DisplayPicture( p_vdec->p_vout, p_pic );
820 /*****************************************************************************
821 * EndThread: thread destruction
822 *****************************************************************************
823 * This function is called when the thread ends after a sucessful
825 *****************************************************************************/
826 static void EndThread( videodec_thread_t *p_vdec )
834 msg_Dbg( p_vdec->p_fifo, "cinepak decoder stopped" );
836 for( i = 0; i < 3; i++ )
838 FREE( p_vdec->p_context->p_pix[i] );
841 free( p_vdec->p_context );
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 );