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 *****************************************************************************/
33 #include <vlc_codec.h>
35 /*****************************************************************************
37 *****************************************************************************/
38 static int OpenDecoder ( vlc_object_t * );
39 static void CloseDecoder( vlc_object_t * );
42 set_description( _("Cinepak video decoder") );
43 set_capability( "decoder", 100 );
44 set_category( CAT_INPUT );
45 set_subcategory( SUBCAT_INPUT_VCODEC );
46 set_callbacks( OpenDecoder, CloseDecoder );
50 /*****************************************************************************
52 *****************************************************************************/
53 #define CINEPAK_MAXSTRIP 32
64 int b_grayscale; /* force to grayscale */
72 uint8_t *p_y, *p_u, *p_v;
74 int i_stride[3]; /* our 3 planes */
78 cinepak_codebook_t codebook_v1[CINEPAK_MAXSTRIP][256];
79 cinepak_codebook_t codebook_v4[CINEPAK_MAXSTRIP][256];
83 /*****************************************************************************
84 * decoder_sys_t : decoder descriptor
85 *****************************************************************************/
91 cinepak_context_t context;
94 static picture_t *DecodeBlock ( decoder_t *, block_t ** );
96 static int cinepak_decode_frame( cinepak_context_t *, int, uint8_t * );
98 /*****************************************************************************
99 * OpenDecoder: probe the decoder and return score
100 *****************************************************************************
101 * Tries to launch a decoder and return score so that the interface is able
103 *****************************************************************************/
104 static int OpenDecoder( vlc_object_t *p_this )
106 decoder_t *p_dec = (decoder_t*)p_this;
107 decoder_sys_t *p_sys;
110 if( p_dec->fmt_in.i_codec != VLC_FOURCC('c','v','i','d') &&
111 p_dec->fmt_in.i_codec != 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 = malloc(sizeof(decoder_sys_t)) ) == NULL )
119 msg_Err( p_dec, "out of memory" );
122 memset( &p_sys->context, 0, sizeof( cinepak_context_t ) );
124 var_Create( p_dec, "grayscale", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
125 var_Get( p_dec, "grayscale", &val );
126 p_sys->context.b_grayscale = val.b_bool;
128 p_dec->pf_decode_video = DecodeBlock;
130 msg_Dbg( p_dec, "cinepak decoder started" );
135 /****************************************************************************
136 * DecodeBlock: the whole thing
137 ****************************************************************************
138 * This function must be fed with whole frames.
139 ****************************************************************************/
140 static picture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
142 decoder_sys_t *p_sys = p_dec->p_sys;
143 int i_status, i_plane;
144 uint8_t *p_dst, *p_src;
148 if( !pp_block || !*pp_block )
155 i_status = cinepak_decode_frame( &p_sys->context, p_block->i_buffer,
159 msg_Warn( p_dec, "cannot decode one frame (%d bytes)",
161 block_Release( p_block );
165 p_dec->fmt_out.video.i_width = p_sys->context.i_width;
166 p_dec->fmt_out.video.i_height = p_sys->context.i_height;
167 p_dec->fmt_out.video.i_aspect = p_sys->context.i_width
168 * VOUT_ASPECT_FACTOR / p_sys->context.i_height;
169 p_dec->fmt_out.i_codec = VLC_FOURCC('I','4','2','0');
171 /* Get a new picture */
172 if( ( p_pic = p_dec->pf_vout_buffer_new( p_dec ) ) )
174 for( i_plane = 0; i_plane < 3; i_plane++ )
178 p_dst = p_pic->p[i_plane].p_pixels;
179 p_src = p_sys->context.p_pix[i_plane];
181 i_lines = __MIN( p_sys->context.i_lines[i_plane],
182 p_pic->p[i_plane].i_visible_lines );
183 for( i_line = 0; i_line < i_lines; i_line++ )
185 memcpy( p_dst, p_src,
186 __MIN( p_pic->p[i_plane].i_pitch,
187 p_sys->context.i_stride[i_plane] ) );
188 p_dst += p_pic->p[i_plane].i_pitch;
189 p_src += p_sys->context.i_stride[i_plane];
193 p_pic->date = p_block->i_pts ? p_block->i_pts : p_block->i_dts;
196 block_Release( p_block );
200 /*****************************************************************************
201 * CloseDecoder: decoder destruction
202 *****************************************************************************/
203 static void CloseDecoder( vlc_object_t *p_this )
205 decoder_t *p_dec = (decoder_t *)p_this;
206 decoder_sys_t *p_sys = p_dec->p_sys;
209 msg_Dbg( p_dec, "cinepak decoder stopped" );
211 for( i = 0; i < 3; i++ )
213 if( p_sys->context.p_pix[i] ) free( p_sys->context.p_pix[i] );
219 /*****************************************************************************
221 *****************************************************************************/
223 #define GET2BYTES( p ) \
226 #define GET3BYTES( p ) \
227 (GetDWBE( p ) >> 8); p+= 3;
229 #define GET4BYTES( p ) \
232 static void cinepak_LoadCodebook( cinepak_codebook_t *p_codebook,
233 uint8_t *p_data, int b_grayscale )
235 int i, i_y[4], i_u, i_v, i_Cb, i_Cr;
238 #define FIX( x ) ( (int)( (x) * ( 1L << SCALEBITS ) + 0.5 ) )
240 for( i = 0; i < 4; i++ )
242 i_y[i] = (uint8_t)( *(p_data++) );
246 i_u = (int8_t)( *(p_data++) );
247 i_v = (int8_t)( *(p_data++) );
256 | Y | | 1 -0.0655 0.0110 | | CY |
257 | Cb | = | 0 1.1656 -0.0062 | | CU |
258 | Cr | | 0 0.0467 1.4187 | | CV |
260 i_uv = ( FIX( -0.0655 ) * i_u + FIX( 0.0110 ) * i_v ) >> SCALEBITS;
261 for( i = 0; i < 4; i++ )
265 i_Cb = ( FIX( 1.1656 ) * i_u + FIX( -0.0062 ) * i_v ) >> SCALEBITS;
266 i_Cr = ( FIX( 0.0467 ) * i_u + FIX( 1.4187 ) * i_v ) >> SCALEBITS;
268 for( i = 0; i < 4; i++ )
270 p_codebook->i_y[i] = __MIN( __MAX( 0, i_y[i] ), 255 );
272 p_codebook->i_u = __MIN( __MAX( 0, i_Cb + 128 ), 255 );
273 p_codebook->i_v = __MIN( __MAX( 0, i_Cr + 128 ), 255 );
279 static void cinepak_Getv4( cinepak_context_t *p_context,
280 int i_strip, int i_x, int i_y,
286 uint8_t *p_dst_y, *p_dst_u, *p_dst_v;
287 #define PIX_SET_Y( x, y, v ) \
288 p_dst_y[(x) + (y)* p_context->i_stride[0]] = (v);
290 #define PIX_SET_UV( i, p, x, y, v ) \
291 p[(x) + (y)* (p_context->i_stride[i])] = (v);
293 for( i = 0; i < 4; i++ )
295 i_index[i] = *(p_data++);
299 p_dst_y = p_context->p_pix[0] + p_context->i_stride[0] * i_y + i_x;
300 p_dst_u = p_context->p_pix[1] + p_context->i_stride[1] * (i_y/2) + (i_x/2);
301 p_dst_v = p_context->p_pix[2] + p_context->i_stride[2] * (i_y/2) + (i_x/2);
303 for( i = 0; i < 2; i++ )
305 for( j = 0; j < 2; j ++ )
307 PIX_SET_Y( 2*j + 0, 2*i + 0,
308 p_context->codebook_v4[i_strip][i_index[j+2*i]].i_y[0]);
309 PIX_SET_Y( 2*j + 1, 2*i + 0,
310 p_context->codebook_v4[i_strip][i_index[j+2*i]].i_y[1]);
311 PIX_SET_Y( 2*j + 0, 2*i + 1,
312 p_context->codebook_v4[i_strip][i_index[j+2*i]].i_y[2]);
313 PIX_SET_Y( 2*j + 1, 2*i + 1,
314 p_context->codebook_v4[i_strip][i_index[j+2*i]].i_y[3]);
316 PIX_SET_UV( 1, p_dst_u, j, i,
317 p_context->codebook_v4[i_strip][i_index[j+2*i]].i_u );
318 PIX_SET_UV( 2, p_dst_v, j, i,
319 p_context->codebook_v4[i_strip][i_index[j+2*i]].i_v );
326 static void cinepak_Getv1( cinepak_context_t *p_context,
327 int i_strip, int i_x, int i_y,
333 uint8_t *p_dst_y, *p_dst_u, *p_dst_v;
334 #define PIX_SET_Y( x, y, v ) \
335 p_dst_y[(x) + (y)* p_context->i_stride[0]] = (v);
337 #define PIX_SET_UV( i,p, x, y, v ) \
338 p[(x) + (y)* (p_context->i_stride[i])] = (v);
340 i_index = *(p_data++);
343 p_dst_y = p_context->p_pix[0] + p_context->i_stride[0] * i_y + i_x;
344 p_dst_u = p_context->p_pix[1] + p_context->i_stride[1] * (i_y/2) + (i_x/2);
345 p_dst_v = p_context->p_pix[2] + p_context->i_stride[2] * (i_y/2) + (i_x/2);
347 for( i = 0; i < 2; i++ )
349 for( j = 0; j < 2; j ++ )
351 PIX_SET_Y( 2*j + 0, 2*i + 0,
352 p_context->codebook_v1[i_strip][i_index].i_y[2*i+j] );
353 PIX_SET_Y( 2*j + 1, 2*i + 0,
354 p_context->codebook_v1[i_strip][i_index].i_y[2*i+j] );
355 PIX_SET_Y( 2*j + 0, 2*i + 1,
356 p_context->codebook_v1[i_strip][i_index].i_y[2*i+j] );
357 PIX_SET_Y( 2*j + 1, 2*i + 1,
358 p_context->codebook_v1[i_strip][i_index].i_y[2*i+j] );
360 PIX_SET_UV( 1,p_dst_u, j, i,
361 p_context->codebook_v1[i_strip][i_index].i_u );
362 PIX_SET_UV( 2,p_dst_v, j, i,
363 p_context->codebook_v1[i_strip][i_index].i_v );
371 /*****************************************************************************
372 * The function that decode one frame
373 *****************************************************************************/
374 static int cinepak_decode_frame( cinepak_context_t *p_context,
375 int i_length, uint8_t *p_data )
381 int i_width, i_height;
384 int i_strip_x1 =0, i_strip_y1=0;
385 int i_strip_x2 =0, i_strip_y2=0;
389 /* Broken header or no data */
394 i_frame_flags = *(p_data++);
395 i_frame_size = GET3BYTES( p_data );
396 i_width = GET2BYTES( p_data );
397 i_height = GET2BYTES( p_data );
398 i_frame_strips = GET2BYTES( p_data );
400 if( !i_frame_size || !i_width || !i_height )
406 /* Check if we have a picture buffer with good size */
407 if( ( p_context->i_width != i_width ) ||
408 ( p_context->i_height != i_height ) )
411 for( i = 0; i < 3; i++ )
413 free( p_context->p_pix[i] );
416 p_context->i_width = i_width;
417 p_context->i_height = i_height;
419 p_context->i_stride[0] = ( i_width + 3 ) & 0xfffc;
420 p_context->i_stride[1] = p_context->i_stride[2] =
421 p_context->i_stride[0] / 2;
423 p_context->i_lines[0] = ( i_height + 3 ) & 0xfffc;
424 p_context->i_lines[1] = p_context->i_lines[2] =
425 p_context->i_lines[0] /2;
427 for( i = 0; i < 3; i++ )
429 p_context->p_pix[i] = malloc( p_context->i_stride[i] *
430 p_context->i_lines[i] );
431 /* Set it to all black */
432 memset( p_context->p_pix[i], ( i == 0 ) ? 0 : 128 ,
433 p_context->i_stride[i] * p_context->i_lines[i] );
437 if( i_frame_size != i_length )
439 i_length = __MIN( i_length, i_frame_size );
443 if( i_frame_strips >= CINEPAK_MAXSTRIP )
445 i_frame_strips = CINEPAK_MAXSTRIP;
448 /* Now decode each strip */
449 for( i_strip = 0; i_strip < i_frame_strips; i_strip++ )
459 i_strip_id = GET2BYTES( p_data );
460 i_strip_size = GET2BYTES( p_data );
461 i_strip_size = __MIN( i_strip_size, i_length );
462 /* FIXME I don't really understand how it works; */
463 i_strip_y1 = i_strip_y2 + GET2BYTES( p_data );
464 i_strip_x1 = GET2BYTES( p_data );
465 i_strip_y2 = i_strip_y2 + GET2BYTES( p_data );
466 i_strip_x2 = GET2BYTES( p_data );
468 i_length -= i_strip_size;
471 /* init codebook , if needed */
472 if( ( i_strip > 0 )&&( !(i_frame_flags&0x01) ) )
474 memcpy( &p_context->codebook_v1[i_strip],
475 &p_context->codebook_v1[i_strip-1],
476 sizeof(cinepak_codebook_t[256] ) );
478 memcpy( &p_context->codebook_v4[i_strip],
479 &p_context->codebook_v4[i_strip-1],
480 sizeof(cinepak_codebook_t[256] ) );
483 /* Now parse all chunk in this strip */
484 while( i_strip_size > 0 )
486 cinepak_codebook_t (*p_codebook)[CINEPAK_MAXSTRIP][256];
491 uint32_t i_vector_flags;
494 int i_x, i_y; /* (0,0) begin in fact at (x1,y1) ... */
496 i_chunk_id = GET2BYTES( p_data );
497 i_chunk_size = GET2BYTES( p_data );
498 i_chunk_size = __MIN( i_chunk_size, i_strip_size );
499 i_strip_size -= i_chunk_size;
505 if( i_chunk_size < 0 )
512 case( 0x2000 ): /* 12bits v4 Intra*/
513 case( 0x2200 ): /* 12bits v1 Intra*/
514 case( 0x2400 ): /* 8bits v4 Intra*/
515 case( 0x2600 ): /* 8bits v1 Intra */
516 i_mode = ( ( i_chunk_id&0x0400 ) == 0 );
517 p_codebook = ( i_chunk_id&0x0200 ) ?
518 &p_context->codebook_v1 :
519 &p_context->codebook_v4;
521 i_count = __MIN( i_chunk_size / ( i_mode ? 6 : 4 ), 256 );
523 for( i = 0; i < i_count; i++ )
525 cinepak_LoadCodebook( &((*p_codebook)[i_strip][i]),
527 i_mode&~p_context->b_grayscale );
528 p_data += i_mode ? 6 : 4;
529 i_chunk_size -= i_mode ? 6 : 4;
533 case( 0x2100 ): /* selective 12bits v4 Inter*/
534 case( 0x2300 ): /* selective 12bits v1 Inter*/
535 case( 0x2500 ): /* selective 8bits v4 Inter*/
536 case( 0x2700 ): /* selective 8bits v1 Inter*/
537 i_mode = ( ( i_chunk_id&0x0400 ) == 0 );
538 p_codebook = ( i_chunk_id&0x0200 ) ?
539 &p_context->codebook_v1 :
540 &p_context->codebook_v4;
543 while( (i_chunk_size > 4)&&(i_index<256))
545 i_vector_flags = GET4BYTES( p_data );
547 for( i = 0; i < 32; i++ )
549 if( ( i_chunk_size < ( i_mode ? 6 : 4 ) )
550 || (i_index >= 256 ) )
554 if( i_vector_flags&0x80000000UL )
556 cinepak_LoadCodebook(
557 &((*p_codebook)[i_strip][i_index]),
558 p_data, i_mode&~p_context->b_grayscale );
560 p_data += i_mode ? 6 : 4;
561 i_chunk_size -= i_mode ? 6 : 4;
564 i_vector_flags <<= 1;
569 case( 0x3000 ): /* load image Intra */
570 while( (i_chunk_size >= 4 )&&(i_y<i_strip_y2-i_strip_y1) )
572 i_vector_flags = GET4BYTES( p_data );
577 for( i = 0; i < 32; i++ )
579 if( ( i_y >= i_strip_y2 - i_strip_y1) ||
580 ( i_chunk_size<=0 ) )
584 if( i_vector_flags&0x80000000UL )
586 cinepak_Getv4( p_context,
596 cinepak_Getv1( p_context,
606 if( i_x >= i_strip_x2 - i_strip_x1 )
611 i_vector_flags <<= 1;
616 case( 0x3100 ): /* load image Inter */
617 while( ( i_chunk_size > 4 )&&( i_y < i_strip_y2 - i_strip_y1) )
620 i_vector_flags = GET4BYTES( p_data );
622 i_mask = 0x80000000UL;
624 while( (i_chunk_size > 0 ) && ( i_mask )
625 && ( i_y < i_strip_y2 - i_strip_y1 ) )
627 if( i_vector_flags&i_mask )
632 if( i_chunk_size < 4 )
636 i_vector_flags = GET4BYTES( p_data );
638 i_mask = 0x80000000UL;
640 if( i_vector_flags&i_mask )
642 if( i_chunk_size < 4 ) break;
643 cinepak_Getv4( p_context,
653 if( i_chunk_size < 1 ) break;
654 cinepak_Getv1( p_context,
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,
688 if( i_x >= i_strip_x2 - i_strip_x1 )
700 p_data += i_chunk_size ; /* skip remains bytes */