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 $
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_t *p_dec = (decoder_t*)p_this;
75 switch( p_dec->p_fifo->i_fourcc )
77 case VLC_FOURCC('c','v','i','d'):
78 case VLC_FOURCC('C','V','I','D'):
79 p_dec->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 #define GET2BYTES( p ) \
138 #define GET3BYTES( p ) \
139 (GetDWBE( p ) >> 8); p+= 3;
141 #define GET4BYTES( p ) \
148 static void GetPESData( u8 *p_buf, int i_max, pes_packet_t *p_pes )
153 data_packet_t *p_data;
156 p_data = p_pes->p_first;
157 while( p_data != NULL && i_count < i_max )
160 i_copy = __MIN( p_data->p_payload_end - p_data->p_payload_start,
166 p_data->p_payload_start,
170 p_data = p_data->p_next;
175 if( i_count < i_max )
177 memset( p_buf, 0, i_max - i_count );
182 void cinepak_LoadCodebook( cinepak_codebook_t *p_codebook,
186 int i, i_y[4], i_u, i_v, i_Cb, i_Cr;
189 #define FIX( x ) ( (int)( (x) * ( 1L << SCALEBITS ) + 0.5 ) )
191 for( i = 0; i < 4; i++ )
193 i_y[i] = (u8)( *(p_data++) );
197 i_u = (s8)( *(p_data++) );
198 i_v = (s8)( *(p_data++) );
207 | Y | | 1 -0.0655 0.0110 | | CY |
208 | Cb | = | 0 1.1656 -0.0062 | | CU |
209 | Cr | | 0 0.0467 1.4187 | | CV |
211 i_uv = ( FIX( -0.0655 ) * i_u + FIX( 0.0110 ) * i_v ) >> SCALEBITS;
212 for( i = 0; i < 4; i++ )
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;
219 for( i = 0; i < 4; i++ )
221 p_codebook->i_y[i] = __MIN( __MAX( 0, i_y[i] ), 255 );
223 p_codebook->i_u = __MIN( __MAX( 0, i_Cb + 128 ), 255 );
224 p_codebook->i_v = __MIN( __MAX( 0, i_Cr + 128 ), 255 );
230 void cinepak_Getv4( cinepak_context_t *p_context,
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);
243 #define PIX_SET_UV( i, p, x, y, v ) \
244 p[(x) + (y)* (p_context->i_stride[i])] = (v);
246 for( i = 0; i < 4; i++ )
248 i_index[i] = *(p_data++);
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);
256 for( i = 0; i < 2; i++ )
258 for( j = 0; j < 2; j ++ )
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]);
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 );
279 void cinepak_Getv1( cinepak_context_t *p_context,
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);
292 #define PIX_SET_UV( i,p, x, y, v ) \
293 p[(x) + (y)* (p_context->i_stride[i])] = (v);
295 i_index = *(p_data++);
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);
302 for( i = 0; i < 2; i++ )
304 for( j = 0; j < 2; j ++ )
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] );
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 );
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 )
336 int i_width, i_height;
339 int i_strip_x1 =0, i_strip_y1=0;
340 int i_strip_x2 =0, i_strip_y2=0;
344 /* Broken header or no data */
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 );
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 ) )
360 for( i = 0; i < 3; i++ )
362 FREE( p_context->p_pix[i] );
365 p_context->i_width = i_width;
366 p_context->i_height = i_height;
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;
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;
376 for( i = 0; i < 3; i++ )
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] );
386 if( i_frame_size != i_length )
388 i_length = __MIN( i_length, i_frame_size );
392 if( i_frame_strips >= CINEPAK_MAXSTRIP )
394 i_frame_strips = CINEPAK_MAXSTRIP;
397 /* Now decode each strip */
399 for( i_strip = 0; i_strip < i_frame_strips; i_strip++ )
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 );
419 i_length -= i_strip_size;
422 /* init codebook , if needed */
423 if( ( i_strip > 0 )&&( !(i_frame_flags&0x01) ) )
425 memcpy( &p_context->codebook_v1[i_strip],
426 &p_context->codebook_v1[i_strip-1],
427 sizeof(cinepak_codebook_t[256] ) );
429 memcpy( &p_context->codebook_v4[i_strip],
430 &p_context->codebook_v4[i_strip-1],
431 sizeof(cinepak_codebook_t[256] ) );
434 /* Now parse all chunk in this strip */
435 while( i_strip_size > 0 )
437 cinepak_codebook_t (*p_codebook)[CINEPAK_MAXSTRIP][256];
445 int i_x, i_y; /* (0,0) begin in fact at (x1,y1) ... */
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;
456 if( i_chunk_size < 0 )
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;
472 i_count = __MIN( i_chunk_size / ( i_mode ? 6 : 4 ), 256 );
474 for( i = 0; i < i_count; i++ )
476 cinepak_LoadCodebook( &((*p_codebook)[i_strip][i]),
478 i_mode&~p_context->b_grayscale );
479 p_data += i_mode ? 6 : 4;
480 i_chunk_size -= i_mode ? 6 : 4;
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;
494 while( (i_chunk_size > 4)&&(i_index<256))
496 i_vector_flags = GET4BYTES( p_data );
498 for( i = 0; i < 32; i++ )
500 if( ( i_chunk_size < ( i_mode ? 6 : 4 ) )||(i_index >= 256 ))
504 if( i_vector_flags&0x80000000UL )
506 cinepak_LoadCodebook( &((*p_codebook)[i_strip][i_index]),
508 i_mode&~p_context->b_grayscale );
510 p_data += i_mode ? 6 : 4;
511 i_chunk_size -= i_mode ? 6 : 4;
514 i_vector_flags <<= 1;
519 case( 0x3000 ): /* load image Intra */
520 while( (i_chunk_size >= 4 )&&(i_y<i_strip_y2-i_strip_y1) )
522 i_vector_flags = GET4BYTES( p_data );
527 for( i = 0; i < 32; i++ )
529 if( ( i_y >= i_strip_y2 - i_strip_y1)||
534 if( i_vector_flags&0x80000000UL )
536 cinepak_Getv4( p_context,
540 i_strip_x2, i_strip_y2,
547 cinepak_Getv1( p_context,
551 i_strip_x2, i_strip_y2,
558 if( i_x >= i_strip_x2 - i_strip_x1 )
563 i_vector_flags <<= 1;
568 case( 0x3100 ): /* load image Inter */
569 while( ( i_chunk_size > 4 )&&( i_y < i_strip_y2 - i_strip_y1) )
572 i_vector_flags = GET4BYTES( p_data );
574 i_mask = 0x80000000UL;
576 while((i_chunk_size > 0 )&&( i_mask )&&( i_y < i_strip_y2 - i_strip_y1 ))
578 if( i_vector_flags&i_mask)
583 if( i_chunk_size < 4 )
587 i_vector_flags = GET4BYTES( p_data );
589 i_mask = 0x80000000UL;
591 if( i_vector_flags&i_mask )
593 if( i_chunk_size < 4 ) break;
594 cinepak_Getv4( p_context,
598 i_strip_x2, i_strip_y2,
605 if( i_chunk_size < 1 ) break;
606 cinepak_Getv1( p_context,
610 i_strip_x2, i_strip_y2,
619 if( i_x >= i_strip_x2 - i_strip_x1 )
628 case( 0x3200 ): /* load intra picture but all v1*/
629 while( ( i_chunk_size > 0 )&&
630 ( i_y < i_strip_y2 - i_strip_y1) )
632 cinepak_Getv1( p_context,
636 i_strip_x2, i_strip_y2,
642 if( i_x >= i_strip_x2 - i_strip_x1 )
657 p_data += i_chunk_size ; /* skip remains bytes */
667 /*****************************************************************************
669 * Functions that initialize, decode and end the decoding process
671 *****************************************************************************/
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 *****************************************************************************/
681 static int InitThread( videodec_thread_t *p_vdec )
684 /* This will be created after the first decoded frame */
685 if( !(p_vdec->p_context = malloc( sizeof( cinepak_context_t ) ) ) )
687 msg_Err( p_vdec->p_fifo, "out of memory" );
689 memset( p_vdec->p_context, 0, sizeof( cinepak_context_t ) );
691 if( config_GetInt( p_vdec->p_fifo, "grayscale" ) )
693 p_vdec->p_context->b_grayscale = 1;
697 p_vdec->p_context->b_grayscale = 0;
700 p_vdec->p_vout = NULL;
701 msg_Dbg( p_vdec->p_fifo, "cinepak decoder started" );
706 /*****************************************************************************
707 * DecodeThread: Called for decode one frame
708 *****************************************************************************/
709 static void DecodeThread( videodec_thread_t *p_vdec )
717 picture_t *p_pic; /* videolan picture */
721 input_ExtractPES( p_vdec->p_fifo, &p_pes );
724 p_vdec->p_fifo->b_error = 1;
727 p_vdec->i_pts = p_pes->i_pts;
728 i_frame_size = p_pes->i_pes_size;
730 if( i_frame_size > 0 )
732 if( p_vdec->i_buffer < i_frame_size + 16 )
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;
739 GetPESData( p_vdec->p_buffer, p_vdec->i_buffer, p_pes );
741 input_DeletePES( p_vdec->p_fifo->p_packets_mgt, p_pes );
742 } while( i_frame_size <= 0 );
745 i_status = cinepak_decode_frame( p_vdec->p_context,
751 msg_Warn( p_vdec->p_fifo, "cannot decode one frame (%d bytes)",
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
763 / p_vdec->p_context->i_height );
765 if( !p_vdec->p_vout )
767 msg_Err( p_vdec->p_fifo, "cannot create vout" );
768 p_vdec->p_fifo->b_error = VLC_TRUE; /* abort */
772 /* Send decoded frame to vout */
773 while( !(p_pic = vout_CreatePicture( p_vdec->p_vout, 0, 0, 0 ) ) )
775 if( p_vdec->p_fifo->b_die || p_vdec->p_fifo->b_error )
779 msleep( VOUT_OUTMEM_SLEEP );
782 for( i_plane = 0; i_plane < 3; i_plane++ )
786 p_dst = p_pic->p[i_plane].p_pixels;
787 p_src = p_vdec->p_context->p_pix[i_plane];
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++ )
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];
802 vout_DatePicture( p_vdec->p_vout, p_pic, p_vdec->i_pts);
803 vout_DisplayPicture( p_vdec->p_vout, p_pic );
809 /*****************************************************************************
810 * EndThread: thread destruction
811 *****************************************************************************
812 * This function is called when the thread ends after a sucessful
814 *****************************************************************************/
815 static void EndThread( videodec_thread_t *p_vdec )
823 msg_Dbg( p_vdec->p_fifo, "cinepak decoder stopped" );
825 for( i = 0; i < 3; i++ )
827 FREE( p_vdec->p_context->p_pix[i] );
830 free( p_vdec->p_context );
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 );