1 /*****************************************************************************
2 * cinepak.c: cinepak video decoder
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VideoLAN
5 * $Id: cinepak.c,v 1.3 2003/11/16 21:07:30 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() */
32 #include <vlc/decoder.h>
33 #include <vlc/input.h>
35 #define CINEPAK_MAXSTRIP 32
37 typedef struct cinepak_codebook_s
44 typedef struct cinepak_context_s
46 int b_grayscale; /* force to grayscale */
54 uint8_t *p_y, *p_u, *p_v;
56 int i_stride[3]; /* our 3 planes */
60 cinepak_codebook_t codebook_v1[CINEPAK_MAXSTRIP][256];
61 cinepak_codebook_t codebook_v4[CINEPAK_MAXSTRIP][256];
65 /*****************************************************************************
66 * decoder_sys_t : decoder descriptor
67 *****************************************************************************/
73 cinepak_context_t *p_context;
76 /*****************************************************************************
78 *****************************************************************************/
79 static int OpenDecoder( vlc_object_t * );
80 static void CloseDecoder( vlc_object_t * );
81 static picture_t *DecodeBlock ( decoder_t *, block_t ** );
83 static int cinepak_decode_frame( cinepak_context_t *, int, uint8_t * );
85 /*****************************************************************************
87 *****************************************************************************/
89 set_description( _("Cinepak video decoder") );
90 set_capability( "decoder", 70 );
91 set_callbacks( OpenDecoder, CloseDecoder );
94 /*****************************************************************************
95 * OpenDecoder: probe the decoder and return score
96 *****************************************************************************
97 * Tries to launch a decoder and return score so that the interface is able
99 *****************************************************************************/
100 static int OpenDecoder( vlc_object_t *p_this )
102 decoder_t *p_dec = (decoder_t*)p_this;
103 decoder_sys_t *p_sys;
106 switch( p_dec->fmt_in.i_codec )
108 case VLC_FOURCC('c','v','i','d'):
109 case VLC_FOURCC('C','V','I','D'):
116 /* Allocate the memory needed to store the decoder's structure */
117 if( ( p_dec->p_sys = p_sys =
118 (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
120 msg_Err( p_dec, "out of memory" );
124 if( !(p_sys->p_context = malloc( sizeof( cinepak_context_t ) ) ) )
126 msg_Err( p_dec, "out of memory" );
128 memset( p_sys->p_context, 0, sizeof( cinepak_context_t ) );
130 var_Create( p_dec, "grayscale", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
131 var_Get( p_dec, "grayscale", &val );
132 p_sys->p_context->b_grayscale = val.b_bool;
134 p_dec->pf_decode_video = DecodeBlock;
136 msg_Dbg( p_dec, "cinepak decoder started" );
141 /****************************************************************************
142 * DecodeBlock: the whole thing
143 ****************************************************************************
144 * This function must be fed with whole frames.
145 ****************************************************************************/
146 static picture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
148 decoder_sys_t *p_sys = p_dec->p_sys;
149 int i_status, i_plane;
150 uint8_t *p_dst, *p_src;
154 if( !pp_block || !*pp_block ) return NULL;
158 i_status = cinepak_decode_frame( p_sys->p_context, p_block->i_buffer,
162 msg_Warn( p_dec, "cannot decode one frame (%d bytes)",
164 block_Release( p_block );
168 p_dec->fmt_out.video.i_width = p_sys->p_context->i_width;
169 p_dec->fmt_out.video.i_height = p_sys->p_context->i_height;
170 p_dec->fmt_out.video.i_aspect = p_sys->p_context->i_width
171 * VOUT_ASPECT_FACTOR / p_sys->p_context->i_height;
172 p_dec->fmt_out.i_codec = VLC_FOURCC('I','4','2','0');
174 /* Get a new picture */
175 p_pic = p_dec->pf_vout_buffer_new( p_dec );
179 block_Release( p_block );
183 for( i_plane = 0; i_plane < 3; i_plane++ )
187 p_dst = p_pic->p[i_plane].p_pixels;
188 p_src = p_sys->p_context->p_pix[i_plane];
190 i_lines = __MIN( p_sys->p_context->i_lines[i_plane],
191 p_pic->p[i_plane].i_lines );
192 for( i_line = 0; i_line < i_lines; i_line++ )
194 memcpy( p_dst, p_src,
195 __MIN( p_pic->p[i_plane].i_pitch,
196 p_sys->p_context->i_stride[i_plane] ) );
197 p_dst += p_pic->p[i_plane].i_pitch;
198 p_src += p_sys->p_context->i_stride[i_plane];
202 p_pic->date = p_block->i_pts;
203 block_Release( p_block );
208 /*****************************************************************************
209 * CloseDecoder: decoder destruction
210 *****************************************************************************/
211 static void CloseDecoder( vlc_object_t *p_this )
213 decoder_t *p_dec = (decoder_t *)p_this;
214 decoder_sys_t *p_sys = p_dec->p_sys;
217 msg_Dbg( p_dec, "cinepak decoder stopped" );
219 for( i = 0; i < 3; i++ )
221 if( p_sys->p_context->p_pix[i] ) free( p_sys->p_context->p_pix[i] );
224 free( p_sys->p_context );
229 /*****************************************************************************
231 *****************************************************************************/
233 #define GET2BYTES( p ) \
236 #define GET3BYTES( p ) \
237 (GetDWBE( p ) >> 8); p+= 3;
239 #define GET4BYTES( p ) \
245 static void cinepak_LoadCodebook( cinepak_codebook_t *p_codebook,
246 uint8_t *p_data, int b_grayscale )
248 int i, i_y[4], i_u, i_v, i_Cb, i_Cr;
251 #define FIX( x ) ( (int)( (x) * ( 1L << SCALEBITS ) + 0.5 ) )
253 for( i = 0; i < 4; i++ )
255 i_y[i] = (uint8_t)( *(p_data++) );
259 i_u = (int8_t)( *(p_data++) );
260 i_v = (int8_t)( *(p_data++) );
269 | Y | | 1 -0.0655 0.0110 | | CY |
270 | Cb | = | 0 1.1656 -0.0062 | | CU |
271 | Cr | | 0 0.0467 1.4187 | | CV |
273 i_uv = ( FIX( -0.0655 ) * i_u + FIX( 0.0110 ) * i_v ) >> SCALEBITS;
274 for( i = 0; i < 4; i++ )
278 i_Cb = ( FIX( 1.1656 ) * i_u + FIX( -0.0062 ) * i_v ) >> SCALEBITS;
279 i_Cr = ( FIX( 0.0467 ) * i_u + FIX( 1.4187 ) * i_v ) >> SCALEBITS;
281 for( i = 0; i < 4; i++ )
283 p_codebook->i_y[i] = __MIN( __MAX( 0, i_y[i] ), 255 );
285 p_codebook->i_u = __MIN( __MAX( 0, i_Cb + 128 ), 255 );
286 p_codebook->i_v = __MIN( __MAX( 0, i_Cr + 128 ), 255 );
292 static void cinepak_Getv4( cinepak_context_t *p_context,
293 int i_strip, int i_x, int i_y,
294 int i_x2, int i_y2, uint8_t *p_data )
299 uint8_t *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 for( i = 0; i < 4; i++ )
308 i_index[i] = *(p_data++);
312 p_dst_y = p_context->p_pix[0] + p_context->i_stride[0] * i_y + i_x;
313 p_dst_u = p_context->p_pix[1] + p_context->i_stride[1] * (i_y/2) + (i_x/2);
314 p_dst_v = p_context->p_pix[2] + p_context->i_stride[2] * (i_y/2) + (i_x/2);
316 for( i = 0; i < 2; i++ )
318 for( j = 0; j < 2; j ++ )
320 PIX_SET_Y( 2*j + 0, 2*i + 0,
321 p_context->codebook_v4[i_strip][i_index[j+2*i]].i_y[0]);
322 PIX_SET_Y( 2*j + 1, 2*i + 0,
323 p_context->codebook_v4[i_strip][i_index[j+2*i]].i_y[1]);
324 PIX_SET_Y( 2*j + 0, 2*i + 1,
325 p_context->codebook_v4[i_strip][i_index[j+2*i]].i_y[2]);
326 PIX_SET_Y( 2*j + 1, 2*i + 1,
327 p_context->codebook_v4[i_strip][i_index[j+2*i]].i_y[3]);
329 PIX_SET_UV( 1, p_dst_u, j, i,
330 p_context->codebook_v4[i_strip][i_index[j+2*i]].i_u );
331 PIX_SET_UV( 2, p_dst_v, j, i,
332 p_context->codebook_v4[i_strip][i_index[j+2*i]].i_v );
339 static void cinepak_Getv1( cinepak_context_t *p_context,
340 int i_strip, int i_x, int i_y,
341 int i_x2, int i_y2, uint8_t *p_data )
346 uint8_t *p_dst_y, *p_dst_u, *p_dst_v;
347 #define PIX_SET_Y( x, y, v ) \
348 p_dst_y[(x) + (y)* p_context->i_stride[0]] = (v);
350 #define PIX_SET_UV( i,p, x, y, v ) \
351 p[(x) + (y)* (p_context->i_stride[i])] = (v);
353 i_index = *(p_data++);
356 p_dst_y = p_context->p_pix[0] + p_context->i_stride[0] * i_y + i_x;
357 p_dst_u = p_context->p_pix[1] + p_context->i_stride[1] * (i_y/2) + (i_x/2);
358 p_dst_v = p_context->p_pix[2] + p_context->i_stride[2] * (i_y/2) + (i_x/2);
360 for( i = 0; i < 2; i++ )
362 for( j = 0; j < 2; j ++ )
364 PIX_SET_Y( 2*j + 0, 2*i + 0,
365 p_context->codebook_v1[i_strip][i_index].i_y[2*i+j] );
366 PIX_SET_Y( 2*j + 1, 2*i + 0,
367 p_context->codebook_v1[i_strip][i_index].i_y[2*i+j] );
368 PIX_SET_Y( 2*j + 0, 2*i + 1,
369 p_context->codebook_v1[i_strip][i_index].i_y[2*i+j] );
370 PIX_SET_Y( 2*j + 1, 2*i + 1,
371 p_context->codebook_v1[i_strip][i_index].i_y[2*i+j] );
373 PIX_SET_UV( 1,p_dst_u, j, i,
374 p_context->codebook_v1[i_strip][i_index].i_u );
375 PIX_SET_UV( 2,p_dst_v, j, i,
376 p_context->codebook_v1[i_strip][i_index].i_v );
384 /*****************************************************************************
385 * The function that decode one frame
386 *****************************************************************************/
387 static int cinepak_decode_frame( cinepak_context_t *p_context,
388 int i_length, uint8_t *p_data )
394 int i_width, i_height;
397 int i_strip_x1 =0, i_strip_y1=0;
398 int i_strip_x2 =0, i_strip_y2=0;
402 /* Broken header or no data */
407 i_frame_flags = *(p_data++);
408 i_frame_size = GET3BYTES( p_data );
409 i_width = GET2BYTES( p_data );
410 i_height = GET2BYTES( p_data );
411 i_frame_strips = GET2BYTES( p_data );
413 if( !i_frame_size || !i_width || !i_height )
419 /* Check if we have a picture buffer with good size */
420 if( ( p_context->i_width != i_width ) ||
421 ( p_context->i_height != i_height ) )
424 for( i = 0; i < 3; i++ )
426 FREE( p_context->p_pix[i] );
429 p_context->i_width = i_width;
430 p_context->i_height = i_height;
432 p_context->i_stride[0] = ( i_width + 3 ) & 0xfffc;
433 p_context->i_stride[1] = p_context->i_stride[2] =
434 p_context->i_stride[0] / 2;
436 p_context->i_lines[0] = ( i_height + 3 ) & 0xfffc;
437 p_context->i_lines[1] = p_context->i_lines[2] =
438 p_context->i_lines[0] /2;
440 for( i = 0; i < 3; i++ )
442 p_context->p_pix[i] = malloc( p_context->i_stride[i] *
443 p_context->i_lines[i] );
444 /* Set it to all black */
445 memset( p_context->p_pix[i], ( i == 0 ) ? 0 : 128 ,
446 p_context->i_stride[i] * p_context->i_lines[i] );
450 if( i_frame_size != i_length )
452 i_length = __MIN( i_length, i_frame_size );
456 if( i_frame_strips >= CINEPAK_MAXSTRIP )
458 i_frame_strips = CINEPAK_MAXSTRIP;
461 /* Now decode each strip */
462 for( i_strip = 0; i_strip < i_frame_strips; i_strip++ )
472 i_strip_id = GET2BYTES( p_data );
473 i_strip_size = GET2BYTES( p_data );
474 i_strip_size = __MIN( i_strip_size, i_length );
475 /* FIXME I don't really understand how it's work; */
476 i_strip_y1 = i_strip_y2 + GET2BYTES( p_data );
477 i_strip_x1 = GET2BYTES( p_data );
478 i_strip_y2 = i_strip_y2 + GET2BYTES( p_data );
479 i_strip_x2 = GET2BYTES( p_data );
481 i_length -= i_strip_size;
484 /* init codebook , if needed */
485 if( ( i_strip > 0 )&&( !(i_frame_flags&0x01) ) )
487 memcpy( &p_context->codebook_v1[i_strip],
488 &p_context->codebook_v1[i_strip-1],
489 sizeof(cinepak_codebook_t[256] ) );
491 memcpy( &p_context->codebook_v4[i_strip],
492 &p_context->codebook_v4[i_strip-1],
493 sizeof(cinepak_codebook_t[256] ) );
496 /* Now parse all chunk in this strip */
497 while( i_strip_size > 0 )
499 cinepak_codebook_t (*p_codebook)[CINEPAK_MAXSTRIP][256];
504 uint32_t i_vector_flags;
507 int i_x, i_y; /* (0,0) begin in fact at (x1,y1) ... */
509 i_chunk_id = GET2BYTES( p_data );
510 i_chunk_size = GET2BYTES( p_data );
511 i_chunk_size = __MIN( i_chunk_size, i_strip_size );
512 i_strip_size -= i_chunk_size;
518 if( i_chunk_size < 0 )
525 case( 0x2000 ): /* 12bits v4 Intra*/
526 case( 0x2200 ): /* 12bits v1 Intra*/
527 case( 0x2400 ): /* 8bits v4 Intra*/
528 case( 0x2600 ): /* 8bits v1 Intra */
529 i_mode = ( ( i_chunk_id&0x0400 ) == 0 );
530 p_codebook = ( i_chunk_id&0x0200 ) ?
531 &p_context->codebook_v1 :
532 &p_context->codebook_v4;
534 i_count = __MIN( i_chunk_size / ( i_mode ? 6 : 4 ), 256 );
536 for( i = 0; i < i_count; i++ )
538 cinepak_LoadCodebook( &((*p_codebook)[i_strip][i]),
540 i_mode&~p_context->b_grayscale );
541 p_data += i_mode ? 6 : 4;
542 i_chunk_size -= i_mode ? 6 : 4;
546 case( 0x2100 ): /* selective 12bits v4 Inter*/
547 case( 0x2300 ): /* selective 12bits v1 Inter*/
548 case( 0x2500 ): /* selective 8bits v4 Inter*/
549 case( 0x2700 ): /* selective 8bits v1 Inter*/
550 i_mode = ( ( i_chunk_id&0x0400 ) == 0 );
551 p_codebook = ( i_chunk_id&0x0200 ) ?
552 &p_context->codebook_v1 :
553 &p_context->codebook_v4;
556 while( (i_chunk_size > 4)&&(i_index<256))
558 i_vector_flags = GET4BYTES( p_data );
560 for( i = 0; i < 32; i++ )
562 if( ( i_chunk_size < ( i_mode ? 6 : 4 ) )
563 || (i_index >= 256 ) )
567 if( i_vector_flags&0x80000000UL )
569 cinepak_LoadCodebook(
570 &((*p_codebook)[i_strip][i_index]),
571 p_data, i_mode&~p_context->b_grayscale );
573 p_data += i_mode ? 6 : 4;
574 i_chunk_size -= i_mode ? 6 : 4;
577 i_vector_flags <<= 1;
582 case( 0x3000 ): /* load image Intra */
583 while( (i_chunk_size >= 4 )&&(i_y<i_strip_y2-i_strip_y1) )
585 i_vector_flags = GET4BYTES( p_data );
590 for( i = 0; i < 32; i++ )
592 if( ( i_y >= i_strip_y2 - i_strip_y1) ||
593 ( i_chunk_size<=0 ) )
597 if( i_vector_flags&0x80000000UL )
599 cinepak_Getv4( p_context,
603 i_strip_x2, i_strip_y2,
610 cinepak_Getv1( p_context,
614 i_strip_x2, i_strip_y2,
621 if( i_x >= i_strip_x2 - i_strip_x1 )
626 i_vector_flags <<= 1;
631 case( 0x3100 ): /* load image Inter */
632 while( ( i_chunk_size > 4 )&&( i_y < i_strip_y2 - i_strip_y1) )
635 i_vector_flags = GET4BYTES( p_data );
637 i_mask = 0x80000000UL;
639 while( (i_chunk_size > 0 ) && ( i_mask )
640 && ( i_y < i_strip_y2 - i_strip_y1 ) )
642 if( i_vector_flags&i_mask )
647 if( i_chunk_size < 4 )
651 i_vector_flags = GET4BYTES( p_data );
653 i_mask = 0x80000000UL;
655 if( i_vector_flags&i_mask )
657 if( i_chunk_size < 4 ) break;
658 cinepak_Getv4( p_context,
662 i_strip_x2, i_strip_y2,
669 if( i_chunk_size < 1 ) break;
670 cinepak_Getv1( p_context,
674 i_strip_x2, i_strip_y2,
683 if( i_x >= i_strip_x2 - i_strip_x1 )
692 case( 0x3200 ): /* load intra picture but all v1*/
693 while( ( i_chunk_size > 0 ) &&
694 ( i_y < i_strip_y2 - i_strip_y1 ) )
696 cinepak_Getv1( p_context,
700 i_strip_x2, i_strip_y2,
706 if( i_x >= i_strip_x2 - i_strip_x1 )
718 p_data += i_chunk_size ; /* skip remains bytes */