1 /*****************************************************************************
2 * cinepak.c: cinepak video decoder
3 *****************************************************************************
4 * Copyright (C) 1999-2001 the VideoLAN team
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
29 #include <vlc_codec.h>
31 /*****************************************************************************
33 *****************************************************************************/
34 static int OpenDecoder ( vlc_object_t * );
35 static void CloseDecoder( vlc_object_t * );
38 set_description( _("Cinepak video decoder") );
39 set_capability( "decoder", 100 );
40 set_category( CAT_INPUT );
41 set_subcategory( SUBCAT_INPUT_VCODEC );
42 set_callbacks( OpenDecoder, CloseDecoder );
46 /*****************************************************************************
48 *****************************************************************************/
49 #define CINEPAK_MAXSTRIP 32
60 int b_grayscale; /* force to grayscale */
68 uint8_t *p_y, *p_u, *p_v;
70 int i_stride[3]; /* our 3 planes */
74 cinepak_codebook_t codebook_v1[CINEPAK_MAXSTRIP][256];
75 cinepak_codebook_t codebook_v4[CINEPAK_MAXSTRIP][256];
79 /*****************************************************************************
80 * decoder_sys_t : decoder descriptor
81 *****************************************************************************/
87 cinepak_context_t context;
90 static picture_t *DecodeBlock ( decoder_t *, block_t ** );
92 static int cinepak_decode_frame( cinepak_context_t *, int, uint8_t * );
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 if( p_dec->fmt_in.i_codec != VLC_FOURCC('c','v','i','d') &&
107 p_dec->fmt_in.i_codec != VLC_FOURCC('C','V','I','D') )
112 /* Allocate the memory needed to store the decoder's structure */
113 if( ( p_dec->p_sys = p_sys = malloc(sizeof(decoder_sys_t)) ) == NULL )
115 msg_Err( p_dec, "out of memory" );
118 memset( &p_sys->context, 0, sizeof( cinepak_context_t ) );
120 var_Create( p_dec, "grayscale", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
121 var_Get( p_dec, "grayscale", &val );
122 p_sys->context.b_grayscale = val.b_bool;
124 p_dec->pf_decode_video = DecodeBlock;
126 msg_Dbg( p_dec, "cinepak decoder started" );
131 /****************************************************************************
132 * DecodeBlock: the whole thing
133 ****************************************************************************
134 * This function must be fed with whole frames.
135 ****************************************************************************/
136 static picture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
138 decoder_sys_t *p_sys = p_dec->p_sys;
139 int i_status, i_plane;
140 uint8_t *p_dst, *p_src;
144 if( !pp_block || !*pp_block )
151 i_status = cinepak_decode_frame( &p_sys->context, p_block->i_buffer,
155 msg_Warn( p_dec, "cannot decode one frame (%d bytes)",
157 block_Release( p_block );
161 p_dec->fmt_out.video.i_width = p_sys->context.i_width;
162 p_dec->fmt_out.video.i_height = p_sys->context.i_height;
163 p_dec->fmt_out.video.i_aspect = p_sys->context.i_width
164 * VOUT_ASPECT_FACTOR / p_sys->context.i_height;
165 p_dec->fmt_out.i_codec = VLC_FOURCC('I','4','2','0');
167 /* Get a new picture */
168 if( ( p_pic = p_dec->pf_vout_buffer_new( p_dec ) ) )
170 for( i_plane = 0; i_plane < 3; i_plane++ )
174 p_dst = p_pic->p[i_plane].p_pixels;
175 p_src = p_sys->context.p_pix[i_plane];
177 i_lines = __MIN( p_sys->context.i_lines[i_plane],
178 p_pic->p[i_plane].i_visible_lines );
179 for( i_line = 0; i_line < i_lines; i_line++ )
181 memcpy( p_dst, p_src,
182 __MIN( p_pic->p[i_plane].i_pitch,
183 p_sys->context.i_stride[i_plane] ) );
184 p_dst += p_pic->p[i_plane].i_pitch;
185 p_src += p_sys->context.i_stride[i_plane];
189 p_pic->date = p_block->i_pts ? p_block->i_pts : p_block->i_dts;
192 block_Release( p_block );
196 /*****************************************************************************
197 * CloseDecoder: decoder destruction
198 *****************************************************************************/
199 static void CloseDecoder( vlc_object_t *p_this )
201 decoder_t *p_dec = (decoder_t *)p_this;
202 decoder_sys_t *p_sys = p_dec->p_sys;
205 msg_Dbg( p_dec, "cinepak decoder stopped" );
207 for( i = 0; i < 3; i++ )
209 if( p_sys->context.p_pix[i] ) free( p_sys->context.p_pix[i] );
215 /*****************************************************************************
217 *****************************************************************************/
219 #define GET2BYTES( p ) \
222 #define GET3BYTES( p ) \
223 (GetDWBE( p ) >> 8); p+= 3;
225 #define GET4BYTES( p ) \
228 static void cinepak_LoadCodebook( cinepak_codebook_t *p_codebook,
229 uint8_t *p_data, int b_grayscale )
231 int i, i_y[4], i_u, i_v, i_Cb, i_Cr;
234 #define FIX( x ) ( (int)( (x) * ( 1L << SCALEBITS ) + 0.5 ) )
236 for( i = 0; i < 4; i++ )
238 i_y[i] = (uint8_t)( *(p_data++) );
242 i_u = (int8_t)( *(p_data++) );
243 i_v = (int8_t)( *(p_data++) );
252 | Y | | 1 -0.0655 0.0110 | | CY |
253 | Cb | = | 0 1.1656 -0.0062 | | CU |
254 | Cr | | 0 0.0467 1.4187 | | CV |
256 i_uv = ( FIX( -0.0655 ) * i_u + FIX( 0.0110 ) * i_v ) >> SCALEBITS;
257 for( i = 0; i < 4; i++ )
261 i_Cb = ( FIX( 1.1656 ) * i_u + FIX( -0.0062 ) * i_v ) >> SCALEBITS;
262 i_Cr = ( FIX( 0.0467 ) * i_u + FIX( 1.4187 ) * i_v ) >> SCALEBITS;
264 for( i = 0; i < 4; i++ )
266 p_codebook->i_y[i] = __MIN( __MAX( 0, i_y[i] ), 255 );
268 p_codebook->i_u = __MIN( __MAX( 0, i_Cb + 128 ), 255 );
269 p_codebook->i_v = __MIN( __MAX( 0, i_Cr + 128 ), 255 );
275 static void cinepak_Getv4( cinepak_context_t *p_context,
276 int i_strip, int i_x, int i_y,
277 int i_x2, int i_y2, uint8_t *p_data )
282 uint8_t *p_dst_y, *p_dst_u, *p_dst_v;
283 #define PIX_SET_Y( x, y, v ) \
284 p_dst_y[(x) + (y)* p_context->i_stride[0]] = (v);
286 #define PIX_SET_UV( i, p, x, y, v ) \
287 p[(x) + (y)* (p_context->i_stride[i])] = (v);
289 for( i = 0; i < 4; i++ )
291 i_index[i] = *(p_data++);
295 p_dst_y = p_context->p_pix[0] + p_context->i_stride[0] * i_y + i_x;
296 p_dst_u = p_context->p_pix[1] + p_context->i_stride[1] * (i_y/2) + (i_x/2);
297 p_dst_v = p_context->p_pix[2] + p_context->i_stride[2] * (i_y/2) + (i_x/2);
299 for( i = 0; i < 2; i++ )
301 for( j = 0; j < 2; j ++ )
303 PIX_SET_Y( 2*j + 0, 2*i + 0,
304 p_context->codebook_v4[i_strip][i_index[j+2*i]].i_y[0]);
305 PIX_SET_Y( 2*j + 1, 2*i + 0,
306 p_context->codebook_v4[i_strip][i_index[j+2*i]].i_y[1]);
307 PIX_SET_Y( 2*j + 0, 2*i + 1,
308 p_context->codebook_v4[i_strip][i_index[j+2*i]].i_y[2]);
309 PIX_SET_Y( 2*j + 1, 2*i + 1,
310 p_context->codebook_v4[i_strip][i_index[j+2*i]].i_y[3]);
312 PIX_SET_UV( 1, p_dst_u, j, i,
313 p_context->codebook_v4[i_strip][i_index[j+2*i]].i_u );
314 PIX_SET_UV( 2, p_dst_v, j, i,
315 p_context->codebook_v4[i_strip][i_index[j+2*i]].i_v );
322 static void cinepak_Getv1( cinepak_context_t *p_context,
323 int i_strip, int i_x, int i_y,
324 int i_x2, int i_y2, uint8_t *p_data )
329 uint8_t *p_dst_y, *p_dst_u, *p_dst_v;
330 #define PIX_SET_Y( x, y, v ) \
331 p_dst_y[(x) + (y)* p_context->i_stride[0]] = (v);
333 #define PIX_SET_UV( i,p, x, y, v ) \
334 p[(x) + (y)* (p_context->i_stride[i])] = (v);
336 i_index = *(p_data++);
339 p_dst_y = p_context->p_pix[0] + p_context->i_stride[0] * i_y + i_x;
340 p_dst_u = p_context->p_pix[1] + p_context->i_stride[1] * (i_y/2) + (i_x/2);
341 p_dst_v = p_context->p_pix[2] + p_context->i_stride[2] * (i_y/2) + (i_x/2);
343 for( i = 0; i < 2; i++ )
345 for( j = 0; j < 2; j ++ )
347 PIX_SET_Y( 2*j + 0, 2*i + 0,
348 p_context->codebook_v1[i_strip][i_index].i_y[2*i+j] );
349 PIX_SET_Y( 2*j + 1, 2*i + 0,
350 p_context->codebook_v1[i_strip][i_index].i_y[2*i+j] );
351 PIX_SET_Y( 2*j + 0, 2*i + 1,
352 p_context->codebook_v1[i_strip][i_index].i_y[2*i+j] );
353 PIX_SET_Y( 2*j + 1, 2*i + 1,
354 p_context->codebook_v1[i_strip][i_index].i_y[2*i+j] );
356 PIX_SET_UV( 1,p_dst_u, j, i,
357 p_context->codebook_v1[i_strip][i_index].i_u );
358 PIX_SET_UV( 2,p_dst_v, j, i,
359 p_context->codebook_v1[i_strip][i_index].i_v );
367 /*****************************************************************************
368 * The function that decode one frame
369 *****************************************************************************/
370 static int cinepak_decode_frame( cinepak_context_t *p_context,
371 int i_length, uint8_t *p_data )
377 int i_width, i_height;
380 int i_strip_x1 =0, i_strip_y1=0;
381 int i_strip_x2 =0, i_strip_y2=0;
385 /* Broken header or no data */
390 i_frame_flags = *(p_data++);
391 i_frame_size = GET3BYTES( p_data );
392 i_width = GET2BYTES( p_data );
393 i_height = GET2BYTES( p_data );
394 i_frame_strips = GET2BYTES( p_data );
396 if( !i_frame_size || !i_width || !i_height )
402 /* Check if we have a picture buffer with good size */
403 if( ( p_context->i_width != i_width ) ||
404 ( p_context->i_height != i_height ) )
407 for( i = 0; i < 3; i++ )
409 free( p_context->p_pix[i] );
412 p_context->i_width = i_width;
413 p_context->i_height = i_height;
415 p_context->i_stride[0] = ( i_width + 3 ) & 0xfffc;
416 p_context->i_stride[1] = p_context->i_stride[2] =
417 p_context->i_stride[0] / 2;
419 p_context->i_lines[0] = ( i_height + 3 ) & 0xfffc;
420 p_context->i_lines[1] = p_context->i_lines[2] =
421 p_context->i_lines[0] /2;
423 for( i = 0; i < 3; i++ )
425 p_context->p_pix[i] = malloc( p_context->i_stride[i] *
426 p_context->i_lines[i] );
427 /* Set it to all black */
428 memset( p_context->p_pix[i], ( i == 0 ) ? 0 : 128 ,
429 p_context->i_stride[i] * p_context->i_lines[i] );
433 if( i_frame_size != i_length )
435 i_length = __MIN( i_length, i_frame_size );
439 if( i_frame_strips >= CINEPAK_MAXSTRIP )
441 i_frame_strips = CINEPAK_MAXSTRIP;
444 /* Now decode each strip */
445 for( i_strip = 0; i_strip < i_frame_strips; i_strip++ )
455 i_strip_id = GET2BYTES( p_data );
456 i_strip_size = GET2BYTES( p_data );
457 i_strip_size = __MIN( i_strip_size, i_length );
458 /* FIXME I don't really understand how it works; */
459 i_strip_y1 = i_strip_y2 + GET2BYTES( p_data );
460 i_strip_x1 = GET2BYTES( p_data );
461 i_strip_y2 = i_strip_y2 + GET2BYTES( p_data );
462 i_strip_x2 = GET2BYTES( p_data );
464 i_length -= i_strip_size;
467 /* init codebook , if needed */
468 if( ( i_strip > 0 )&&( !(i_frame_flags&0x01) ) )
470 memcpy( &p_context->codebook_v1[i_strip],
471 &p_context->codebook_v1[i_strip-1],
472 sizeof(cinepak_codebook_t[256] ) );
474 memcpy( &p_context->codebook_v4[i_strip],
475 &p_context->codebook_v4[i_strip-1],
476 sizeof(cinepak_codebook_t[256] ) );
479 /* Now parse all chunk in this strip */
480 while( i_strip_size > 0 )
482 cinepak_codebook_t (*p_codebook)[CINEPAK_MAXSTRIP][256];
487 uint32_t i_vector_flags;
490 int i_x, i_y; /* (0,0) begin in fact at (x1,y1) ... */
492 i_chunk_id = GET2BYTES( p_data );
493 i_chunk_size = GET2BYTES( p_data );
494 i_chunk_size = __MIN( i_chunk_size, i_strip_size );
495 i_strip_size -= i_chunk_size;
501 if( i_chunk_size < 0 )
508 case( 0x2000 ): /* 12bits v4 Intra*/
509 case( 0x2200 ): /* 12bits v1 Intra*/
510 case( 0x2400 ): /* 8bits v4 Intra*/
511 case( 0x2600 ): /* 8bits v1 Intra */
512 i_mode = ( ( i_chunk_id&0x0400 ) == 0 );
513 p_codebook = ( i_chunk_id&0x0200 ) ?
514 &p_context->codebook_v1 :
515 &p_context->codebook_v4;
517 i_count = __MIN( i_chunk_size / ( i_mode ? 6 : 4 ), 256 );
519 for( i = 0; i < i_count; i++ )
521 cinepak_LoadCodebook( &((*p_codebook)[i_strip][i]),
523 i_mode&~p_context->b_grayscale );
524 p_data += i_mode ? 6 : 4;
525 i_chunk_size -= i_mode ? 6 : 4;
529 case( 0x2100 ): /* selective 12bits v4 Inter*/
530 case( 0x2300 ): /* selective 12bits v1 Inter*/
531 case( 0x2500 ): /* selective 8bits v4 Inter*/
532 case( 0x2700 ): /* selective 8bits v1 Inter*/
533 i_mode = ( ( i_chunk_id&0x0400 ) == 0 );
534 p_codebook = ( i_chunk_id&0x0200 ) ?
535 &p_context->codebook_v1 :
536 &p_context->codebook_v4;
539 while( (i_chunk_size > 4)&&(i_index<256))
541 i_vector_flags = GET4BYTES( p_data );
543 for( i = 0; i < 32; i++ )
545 if( ( i_chunk_size < ( i_mode ? 6 : 4 ) )
546 || (i_index >= 256 ) )
550 if( i_vector_flags&0x80000000UL )
552 cinepak_LoadCodebook(
553 &((*p_codebook)[i_strip][i_index]),
554 p_data, i_mode&~p_context->b_grayscale );
556 p_data += i_mode ? 6 : 4;
557 i_chunk_size -= i_mode ? 6 : 4;
560 i_vector_flags <<= 1;
565 case( 0x3000 ): /* load image Intra */
566 while( (i_chunk_size >= 4 )&&(i_y<i_strip_y2-i_strip_y1) )
568 i_vector_flags = GET4BYTES( p_data );
573 for( i = 0; i < 32; i++ )
575 if( ( i_y >= i_strip_y2 - i_strip_y1) ||
576 ( i_chunk_size<=0 ) )
580 if( i_vector_flags&0x80000000UL )
582 cinepak_Getv4( p_context,
586 i_strip_x2, i_strip_y2,
593 cinepak_Getv1( p_context,
597 i_strip_x2, i_strip_y2,
604 if( i_x >= i_strip_x2 - i_strip_x1 )
609 i_vector_flags <<= 1;
614 case( 0x3100 ): /* load image Inter */
615 while( ( i_chunk_size > 4 )&&( i_y < i_strip_y2 - i_strip_y1) )
618 i_vector_flags = GET4BYTES( p_data );
620 i_mask = 0x80000000UL;
622 while( (i_chunk_size > 0 ) && ( i_mask )
623 && ( i_y < i_strip_y2 - i_strip_y1 ) )
625 if( i_vector_flags&i_mask )
630 if( i_chunk_size < 4 )
634 i_vector_flags = GET4BYTES( p_data );
636 i_mask = 0x80000000UL;
638 if( i_vector_flags&i_mask )
640 if( i_chunk_size < 4 ) break;
641 cinepak_Getv4( p_context,
645 i_strip_x2, i_strip_y2,
652 if( i_chunk_size < 1 ) break;
653 cinepak_Getv1( p_context,
657 i_strip_x2, i_strip_y2,
666 if( i_x >= i_strip_x2 - i_strip_x1 )
675 case( 0x3200 ): /* load intra picture but all v1*/
676 while( ( i_chunk_size > 0 ) &&
677 ( i_y < i_strip_y2 - i_strip_y1 ) )
679 cinepak_Getv1( p_context,
683 i_strip_x2, i_strip_y2,
689 if( i_x >= i_strip_x2 - i_strip_x1 )
701 p_data += i_chunk_size ; /* skip remains bytes */