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 *****************************************************************************/
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
34 #include <vlc_codec.h>
36 /*****************************************************************************
38 *****************************************************************************/
39 static int OpenDecoder ( vlc_object_t * );
40 static void CloseDecoder( vlc_object_t * );
43 set_description( N_("Cinepak video decoder") );
44 set_capability( "decoder", 50 );
45 set_category( CAT_INPUT );
46 set_subcategory( SUBCAT_INPUT_VCODEC );
47 set_callbacks( OpenDecoder, CloseDecoder );
51 /*****************************************************************************
53 *****************************************************************************/
54 #define CINEPAK_MAXSTRIP 32
65 int b_grayscale; /* force to grayscale */
68 unsigned int i_height;
73 uint8_t *p_y, *p_u, *p_v;
75 int i_stride[3]; /* our 3 planes */
79 cinepak_codebook_t codebook_v1[CINEPAK_MAXSTRIP][256];
80 cinepak_codebook_t codebook_v4[CINEPAK_MAXSTRIP][256];
84 /*****************************************************************************
85 * decoder_sys_t : decoder descriptor
86 *****************************************************************************/
92 cinepak_context_t context;
95 static picture_t *DecodeBlock ( decoder_t *, block_t ** );
97 static int cinepak_decode_frame( cinepak_context_t *, size_t, uint8_t * );
99 /*****************************************************************************
100 * OpenDecoder: probe the decoder and return score
101 *****************************************************************************
102 * Tries to launch a decoder and return score so that the interface is able
104 *****************************************************************************/
105 static int OpenDecoder( vlc_object_t *p_this )
107 decoder_t *p_dec = (decoder_t*)p_this;
108 decoder_sys_t *p_sys;
111 if( p_dec->fmt_in.i_codec != VLC_FOURCC('c','v','i','d') &&
112 p_dec->fmt_in.i_codec != VLC_FOURCC('C','V','I','D') )
117 /* Allocate the memory needed to store the decoder's structure */
118 if( ( p_dec->p_sys = p_sys = malloc(sizeof(decoder_sys_t)) ) == NULL )
120 memset( &p_sys->context, 0, sizeof( cinepak_context_t ) );
122 var_Create( p_dec, "grayscale", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
123 var_Get( p_dec, "grayscale", &val );
124 p_sys->context.b_grayscale = val.b_bool;
126 p_dec->pf_decode_video = DecodeBlock;
128 msg_Dbg( p_dec, "cinepak decoder started" );
133 /****************************************************************************
134 * DecodeBlock: the whole thing
135 ****************************************************************************
136 * This function must be fed with whole frames.
137 ****************************************************************************/
138 static picture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
140 decoder_sys_t *p_sys = p_dec->p_sys;
141 int i_status, i_plane;
142 uint8_t *p_dst, *p_src;
146 if( !pp_block || !*pp_block )
153 i_status = cinepak_decode_frame( &p_sys->context, p_block->i_buffer,
157 msg_Warn( p_dec, "cannot decode one frame (%zu bytes)",
159 block_Release( p_block );
163 p_dec->fmt_out.video.i_width = p_sys->context.i_width;
164 p_dec->fmt_out.video.i_height = p_sys->context.i_height;
165 p_dec->fmt_out.video.i_aspect = p_sys->context.i_width
166 * VOUT_ASPECT_FACTOR / p_sys->context.i_height;
167 p_dec->fmt_out.i_codec = VLC_FOURCC('I','4','2','0');
169 /* Get a new picture */
170 if( ( p_pic = p_dec->pf_vout_buffer_new( p_dec ) ) )
172 for( i_plane = 0; i_plane < 3; i_plane++ )
176 p_dst = p_pic->p[i_plane].p_pixels;
177 p_src = p_sys->context.p_pix[i_plane];
179 i_lines = __MIN( p_sys->context.i_lines[i_plane],
180 p_pic->p[i_plane].i_visible_lines );
181 for( i_line = 0; i_line < i_lines; i_line++ )
183 memcpy( p_dst, p_src,
184 __MIN( p_pic->p[i_plane].i_pitch,
185 p_sys->context.i_stride[i_plane] ) );
186 p_dst += p_pic->p[i_plane].i_pitch;
187 p_src += p_sys->context.i_stride[i_plane];
191 p_pic->date = p_block->i_pts ? p_block->i_pts : p_block->i_dts;
194 block_Release( p_block );
198 /*****************************************************************************
199 * CloseDecoder: decoder destruction
200 *****************************************************************************/
201 static void CloseDecoder( vlc_object_t *p_this )
203 decoder_t *p_dec = (decoder_t *)p_this;
204 decoder_sys_t *p_sys = p_dec->p_sys;
207 msg_Dbg( p_dec, "cinepak decoder stopped" );
209 for( i = 0; i < 3; i++ )
210 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,
282 size_t y_max = p_context->i_stride[0] * ( i_y + 5 ) + i_x + 5;
283 size_t u_max = p_context->i_stride[1] * ( ( i_y/2 ) + 2 ) + 2 + ( i_x / 2 );
284 size_t v_max = p_context->i_stride[2] * ( ( i_y/2 ) + 2 ) + 2 + ( i_x / 2 );
285 size_t y_siz = p_context->i_stride[0] * p_context->i_lines[0];
286 size_t u_siz = p_context->i_stride[1] * p_context->i_lines[1];
287 size_t v_siz = p_context->i_stride[2] * p_context->i_lines[2];
289 if( y_max >= y_siz || u_max >= u_siz || v_max >= v_siz )
292 uint8_t *p_dst_y, *p_dst_u, *p_dst_v;
293 #define PIX_SET_Y( x, y, v ) \
294 p_dst_y[(x) + (y)* p_context->i_stride[0]] = (v);
296 #define PIX_SET_UV( i, p, x, y, v ) \
297 p[(x) + (y)* (p_context->i_stride[i])] = (v);
299 for( i = 0; i < 4; i++ )
301 i_index[i] = *(p_data++);
305 p_dst_y = p_context->p_pix[0] + p_context->i_stride[0] * i_y + i_x;
306 p_dst_u = p_context->p_pix[1] + p_context->i_stride[1] * (i_y/2) + (i_x/2);
307 p_dst_v = p_context->p_pix[2] + p_context->i_stride[2] * (i_y/2) + (i_x/2);
309 for( i = 0; i < 2; i++ )
311 for( j = 0; j < 2; j ++ )
313 PIX_SET_Y( 2*j + 0, 2*i + 0,
314 p_context->codebook_v4[i_strip][i_index[j+2*i]].i_y[0]);
315 PIX_SET_Y( 2*j + 1, 2*i + 0,
316 p_context->codebook_v4[i_strip][i_index[j+2*i]].i_y[1]);
317 PIX_SET_Y( 2*j + 0, 2*i + 1,
318 p_context->codebook_v4[i_strip][i_index[j+2*i]].i_y[2]);
319 PIX_SET_Y( 2*j + 1, 2*i + 1,
320 p_context->codebook_v4[i_strip][i_index[j+2*i]].i_y[3]);
322 PIX_SET_UV( 1, p_dst_u, j, i,
323 p_context->codebook_v4[i_strip][i_index[j+2*i]].i_u );
324 PIX_SET_UV( 2, p_dst_v, j, i,
325 p_context->codebook_v4[i_strip][i_index[j+2*i]].i_v );
332 static void cinepak_Getv1( cinepak_context_t *p_context,
333 int i_strip, int i_x, int i_y,
339 size_t y_max = p_context->i_stride[0] * ( i_y + 5 ) + i_x + 5;
340 size_t u_max = p_context->i_stride[1] * ( ( i_y/2 ) + 2 ) + 2 + ( i_x / 2 );
341 size_t v_max = p_context->i_stride[2] * ( ( i_y/2 ) + 2 ) + 2 + ( i_x / 2 );
342 size_t y_siz = p_context->i_stride[0] * p_context->i_lines[0];
343 size_t u_siz = p_context->i_stride[1] * p_context->i_lines[1];
344 size_t v_siz = p_context->i_stride[2] * p_context->i_lines[2];
346 if( y_max >= y_siz || u_max >= u_siz || v_max >= v_siz )
349 uint8_t *p_dst_y, *p_dst_u, *p_dst_v;
350 #define PIX_SET_Y( x, y, v ) \
351 p_dst_y[(x) + (y)* p_context->i_stride[0]] = (v);
353 #define PIX_SET_UV( i,p, x, y, v ) \
354 p[(x) + (y)* (p_context->i_stride[i])] = (v);
356 i_index = *(p_data++);
359 p_dst_y = p_context->p_pix[0] + p_context->i_stride[0] * i_y + i_x;
360 p_dst_u = p_context->p_pix[1] + p_context->i_stride[1] * (i_y/2) + (i_x/2);
361 p_dst_v = p_context->p_pix[2] + p_context->i_stride[2] * (i_y/2) + (i_x/2);
363 for( i = 0; i < 2; i++ )
365 for( j = 0; j < 2; j ++ )
367 PIX_SET_Y( 2*j + 0, 2*i + 0,
368 p_context->codebook_v1[i_strip][i_index].i_y[2*i+j] );
369 PIX_SET_Y( 2*j + 1, 2*i + 0,
370 p_context->codebook_v1[i_strip][i_index].i_y[2*i+j] );
371 PIX_SET_Y( 2*j + 0, 2*i + 1,
372 p_context->codebook_v1[i_strip][i_index].i_y[2*i+j] );
373 PIX_SET_Y( 2*j + 1, 2*i + 1,
374 p_context->codebook_v1[i_strip][i_index].i_y[2*i+j] );
376 PIX_SET_UV( 1,p_dst_u, j, i,
377 p_context->codebook_v1[i_strip][i_index].i_u );
378 PIX_SET_UV( 2,p_dst_v, j, i,
379 p_context->codebook_v1[i_strip][i_index].i_v );
387 /*****************************************************************************
388 * The function that decode one frame
389 *****************************************************************************/
390 static int cinepak_decode_frame( cinepak_context_t *p_context,
391 size_t i_length, uint8_t *p_data )
395 int8_t i_frame_flags;
396 uint32_t i_frame_size;
397 uint16_t i_width, i_height;
398 uint16_t i_frame_strips;
400 int i_strip_x1 =0, i_strip_y1=0;
401 int i_strip_x2 =0, i_strip_y2=0;
405 /* Broken header or no data */
410 i_frame_flags = *(p_data++);
411 i_frame_size = GET3BYTES( p_data );
412 i_width = GET2BYTES( p_data );
413 i_height = GET2BYTES( p_data );
414 i_frame_strips = GET2BYTES( p_data );
416 if( !i_frame_size || !i_width || !i_height ||
417 i_width > 0xffff-3 || i_height > 0xffff-3)
423 /* Check if we have a picture buffer with good size */
424 if( ( p_context->i_width != i_width ) ||
425 ( p_context->i_height != i_height ) )
428 for( i = 0; i < 3; i++ )
430 free( p_context->p_pix[i] );
433 p_context->i_width = i_width;
434 p_context->i_height = i_height;
436 p_context->i_stride[0] = ( i_width + 3 ) & 0xfffc;
437 p_context->i_stride[1] = p_context->i_stride[2] =
438 p_context->i_stride[0] / 2;
440 p_context->i_lines[0] = ( i_height + 3 ) & 0xfffc;
441 p_context->i_lines[1] = p_context->i_lines[2] =
442 p_context->i_lines[0] /2;
444 for( i = 0; i < 3; i++ )
446 p_context->p_pix[i] = malloc( p_context->i_stride[i] *
447 p_context->i_lines[i] );
448 if( p_context->p_pix[i] == NULL )
450 /* Set it to all black */
451 memset( p_context->p_pix[i], ( i == 0 ) ? 0 : 128 ,
452 p_context->i_stride[i] * p_context->i_lines[i] );
456 if( i_frame_size != i_length )
458 i_length = __MIN( i_length, i_frame_size );
462 if( i_frame_strips >= CINEPAK_MAXSTRIP )
464 i_frame_strips = CINEPAK_MAXSTRIP;
467 /* Now decode each strip */
468 for( i_strip = 0; i_strip < i_frame_strips; i_strip++ )
470 uint16_t i_strip_size;
477 p_data += 2; /* int16_t i_strip_id = GET2BYTES( p_data ); */
479 i_strip_size = GET2BYTES( p_data );
480 i_strip_size = __MIN( i_strip_size, i_length );
481 /* FIXME I don't really understand how it works; */
482 i_strip_y1 = i_strip_y2 + GET2BYTES( p_data );
483 i_strip_x1 = GET2BYTES( p_data );
484 i_strip_y2 = i_strip_y2 + GET2BYTES( p_data );
485 i_strip_x2 = GET2BYTES( p_data );
487 i_length -= i_strip_size;
490 /* init codebook , if needed */
491 if( ( i_strip > 0 )&&( !(i_frame_flags&0x01) ) )
493 memcpy( &p_context->codebook_v1[i_strip],
494 &p_context->codebook_v1[i_strip-1],
495 sizeof(cinepak_codebook_t[256] ) );
497 memcpy( &p_context->codebook_v4[i_strip],
498 &p_context->codebook_v4[i_strip-1],
499 sizeof(cinepak_codebook_t[256] ) );
502 /* Now parse all chunk in this strip */
503 while( i_strip_size > 0 )
505 cinepak_codebook_t (*p_codebook)[CINEPAK_MAXSTRIP][256];
510 uint32_t i_vector_flags;
513 int i_x, i_y; /* (0,0) begin in fact at (x1,y1) ... */
515 i_chunk_id = GET2BYTES( p_data );
516 i_chunk_size = GET2BYTES( p_data );
517 i_chunk_size = __MIN( i_chunk_size, i_strip_size );
518 i_strip_size -= i_chunk_size;
524 if( i_chunk_size < 0 )
531 case( 0x2000 ): /* 12bits v4 Intra*/
532 case( 0x2200 ): /* 12bits v1 Intra*/
533 case( 0x2400 ): /* 8bits v4 Intra*/
534 case( 0x2600 ): /* 8bits v1 Intra */
535 i_mode = ( ( i_chunk_id&0x0400 ) == 0 );
536 p_codebook = ( i_chunk_id&0x0200 ) ?
537 &p_context->codebook_v1 :
538 &p_context->codebook_v4;
540 i_count = __MIN( i_chunk_size / ( i_mode ? 6 : 4 ), 256 );
542 for( i = 0; i < i_count; i++ )
544 cinepak_LoadCodebook( &((*p_codebook)[i_strip][i]),
546 i_mode&~p_context->b_grayscale );
547 p_data += i_mode ? 6 : 4;
548 i_chunk_size -= i_mode ? 6 : 4;
552 case( 0x2100 ): /* selective 12bits v4 Inter*/
553 case( 0x2300 ): /* selective 12bits v1 Inter*/
554 case( 0x2500 ): /* selective 8bits v4 Inter*/
555 case( 0x2700 ): /* selective 8bits v1 Inter*/
556 i_mode = ( ( i_chunk_id&0x0400 ) == 0 );
557 p_codebook = ( i_chunk_id&0x0200 ) ?
558 &p_context->codebook_v1 :
559 &p_context->codebook_v4;
562 while( (i_chunk_size > 4)&&(i_index<256))
564 i_vector_flags = GET4BYTES( p_data );
566 for( i = 0; i < 32; i++ )
568 if( ( i_chunk_size < ( i_mode ? 6 : 4 ) )
569 || (i_index >= 256 ) )
573 if( i_vector_flags&0x80000000UL )
575 cinepak_LoadCodebook(
576 &((*p_codebook)[i_strip][i_index]),
577 p_data, i_mode&~p_context->b_grayscale );
579 p_data += i_mode ? 6 : 4;
580 i_chunk_size -= i_mode ? 6 : 4;
583 i_vector_flags <<= 1;
588 case( 0x3000 ): /* load image Intra */
589 while( (i_chunk_size >= 4 )&&(i_y<i_strip_y2-i_strip_y1) )
591 i_vector_flags = GET4BYTES( p_data );
596 for( i = 0; i < 32; i++ )
598 if( ( i_y >= i_strip_y2 - i_strip_y1) ||
599 ( i_chunk_size<=0 ) )
603 if( i_vector_flags&0x80000000UL )
605 cinepak_Getv4( p_context,
615 cinepak_Getv1( p_context,
625 if( i_x >= i_strip_x2 - i_strip_x1 )
630 i_vector_flags <<= 1;
635 case( 0x3100 ): /* load image Inter */
636 while( ( i_chunk_size > 4 )&&( i_y < i_strip_y2 - i_strip_y1) )
639 i_vector_flags = GET4BYTES( p_data );
641 i_mask = 0x80000000UL;
643 while( (i_chunk_size > 0 ) && ( i_mask )
644 && ( i_y < i_strip_y2 - i_strip_y1 ) )
646 if( i_vector_flags&i_mask )
651 if( i_chunk_size < 4 )
655 i_vector_flags = GET4BYTES( p_data );
657 i_mask = 0x80000000UL;
659 if( i_vector_flags&i_mask )
661 if( i_chunk_size < 4 ) break;
662 cinepak_Getv4( p_context,
672 if( i_chunk_size < 1 ) break;
673 cinepak_Getv1( p_context,
685 if( i_x >= i_strip_x2 - i_strip_x1 )
694 case( 0x3200 ): /* load intra picture but all v1*/
695 while( ( i_chunk_size > 0 ) &&
696 ( i_y < i_strip_y2 - i_strip_y1 ) )
698 cinepak_Getv1( p_context,
707 if( i_x >= i_strip_x2 - i_strip_x1 )
719 p_data += i_chunk_size ; /* skip remains bytes */