1 /*****************************************************************************
2 * tarkin.c: tarkin decoder module making use of libtarkin.
3 *****************************************************************************
4 * Copyright (C) 2001-2003 VideoLAN
7 * Authors: Gildas Bazin <gbazin@netcourrier.com>
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 *****************************************************************************/
28 #include <vlc/decoder.h>
33 // use 16 bit signed integers as wavelet coefficients
35 // we'll actually use TYPE_BITS bits of type (e.g. 9 magnitude + 1 sign)
37 // use the rle entropy coder
42 /*****************************************************************************
43 * decoder_sys_t : tarkin decoder descriptor
44 *****************************************************************************/
50 TarkinStream *tarkin_stream;
52 TarkinInfo ti; /* tarkin bitstream settings */
53 TarkinComment tc; /* tarkin bitstream user comments */
60 /*****************************************************************************
62 *****************************************************************************/
63 static int OpenDecoder ( vlc_object_t * );
64 static void CloseDecoder ( vlc_object_t * );
66 static void *DecodeBlock ( decoder_t *, block_t ** );
67 static picture_t *DecodePacket ( decoder_t *, block_t **, ogg_packet * );
69 static void tarkin_CopyPicture( decoder_t *, picture_t *, uint8_t *, int );
71 /*****************************************************************************
73 *****************************************************************************/
75 set_description( _("Tarkin decoder module") );
76 set_capability( "decoder", 100 );
77 set_category( CAT_INPUT );
78 set_subcategory( SUBCAT_INPUT_VCODEC );
79 set_callbacks( OpenDecoder, CloseDecoder );
80 add_shortcut( "tarkin" );
83 /*****************************************************************************
84 * OpenDecoder: probe the decoder and return score
85 *****************************************************************************/
86 static int OpenDecoder( vlc_object_t *p_this )
88 decoder_t *p_dec = (decoder_t*)p_this;
91 if( p_dec->fmt_in.i_codec != VLC_FOURCC('t','a','r','k') )
96 /* Allocate the memory needed to store the decoder's structure */
97 if( ( p_dec->p_sys = p_sys =
98 (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
100 msg_Err( p_dec, "out of memory" );
104 /* Set output properties */
105 p_dec->fmt_out.i_cat = VIDEO_ES;
106 p_sys->i_headers = 0;
109 p_dec->pf_decode_video = (picture_t *(*)(decoder_t *, block_t **))
111 p_dec->pf_packetize = (block_t *(*)(decoder_t *, block_t **))
114 /* Init supporting Tarkin structures needed in header parsing */
115 p_sys->tarkin_stream = tarkin_stream_new();
116 tarkin_info_init( &p_sys->ti );
117 tarkin_comment_init( &p_sys->tc );
122 /****************************************************************************
123 * DecodeBlock: the whole thing
124 ****************************************************************************
125 * This function must be fed with ogg packets.
126 ****************************************************************************/
127 static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
129 decoder_sys_t *p_sys = p_dec->p_sys;
131 ogg_packet oggpacket;
133 if( !pp_block ) return NULL;
137 /* Block to Ogg packet */
138 oggpacket.packet = (*pp_block)->p_buffer;
139 oggpacket.bytes = (*pp_block)->i_buffer;
143 /* Block to Ogg packet */
144 oggpacket.packet = NULL;
150 oggpacket.granulepos = -1;
153 oggpacket.packetno = 0;
155 if( p_sys->i_headers == 0 )
157 /* Take care of the initial Tarkin header */
159 oggpacket.b_o_s = 1; /* yes this actually is a b_o_s packet :) */
160 if( tarkin_synthesis_headerin( &p_sys->ti, &p_sys->tc, &oggpacket )
163 msg_Err( p_dec, "This bitstream does not contain Tarkin "
165 block_Release( p_block );
170 block_Release( p_block );
174 if( p_sys->i_headers == 1 )
176 if( tarkin_synthesis_headerin( &p_sys->ti, &p_sys->tc, &oggpacket )
179 msg_Err( p_dec, "2nd Tarkin header is corrupted." );
180 block_Release( p_block );
184 block_Release( p_block );
188 if( p_sys->i_headers == 2 )
190 if( tarkin_synthesis_headerin( &p_sys->ti, &p_sys->tc, &oggpacket )
193 msg_Err( p_dec, "3rd Tarkin header is corrupted." );
194 block_Release( p_block );
199 /* Initialize the tarkin decoder */
200 tarkin_synthesis_init( p_sys->tarkin_stream, &p_sys->ti );
202 msg_Err( p_dec, "Tarkin codec initialized");
204 block_Release( p_block );
208 return DecodePacket( p_dec, pp_block, &oggpacket );
211 /*****************************************************************************
212 * DecodePacket: decodes a Tarkin packet.
213 *****************************************************************************/
214 static picture_t *DecodePacket( decoder_t *p_dec, block_t **pp_block,
215 ogg_packet *p_oggpacket )
217 decoder_sys_t *p_sys = p_dec->p_sys;
220 if( p_oggpacket->bytes )
222 tarkin_synthesis_packetin( p_sys->tarkin_stream, p_oggpacket );
223 //block_Release( *pp_block ); /* FIXME duplicate packet */
227 if( tarkin_synthesis_frameout( p_sys->tarkin_stream,
228 &rgb, 0, &p_sys->tarkdate ) == 0 )
230 int i_width, i_height, i_chroma, i_stride;
233 msg_Err( p_dec, "Tarkin frame decoded" );
235 i_width = p_sys->tarkin_stream->layer->desc.width;
236 i_height = p_sys->tarkin_stream->layer->desc.height;
238 switch( p_sys->tarkin_stream->layer->desc.format )
241 i_chroma = VLC_FOURCC('R','V','2','4');
242 i_stride = i_width * 3;
245 i_chroma = VLC_FOURCC('R','V','3','2');
246 i_stride = i_width * 4;
249 i_chroma = VLC_FOURCC('R','G','B','A');
250 i_stride = i_width * 4;
253 i_chroma = VLC_FOURCC('I','4','2','0');
258 /* Set output properties */
259 p_dec->fmt_out.video.i_width = i_width;
260 p_dec->fmt_out.video.i_height = i_height;
262 p_dec->fmt_out.video.i_aspect =
263 VOUT_ASPECT_FACTOR * i_width / i_height;
264 p_dec->fmt_out.i_codec = i_chroma;
266 /* Get a new picture */
267 if( (p_pic = p_dec->pf_vout_buffer_new( p_dec )) )
269 tarkin_CopyPicture( p_dec, p_pic, rgb, i_stride );
271 tarkin_synthesis_freeframe( p_sys->tarkin_stream, rgb );
273 p_pic->date = mdate() + DEFAULT_PTS_DELAY/*i_pts*/;
282 /*****************************************************************************
283 * CloseDecoder: tarkin decoder destruction
284 *****************************************************************************/
285 static void CloseDecoder( vlc_object_t *p_this )
287 decoder_t *p_dec = (decoder_t *)p_this;
288 decoder_sys_t *p_sys = p_dec->p_sys;
290 tarkin_stream_destroy( p_sys->tarkin_stream );
295 /*****************************************************************************
296 * tarkin_CopyPicture: copy a picture from tarkin internal buffers to a
297 * picture_t structure.
298 *****************************************************************************/
299 static void tarkin_CopyPicture( decoder_t *p_dec, picture_t *p_pic,
300 uint8_t *p_src, int i_pitch )
302 int i_plane, i_line, i_src_stride, i_dst_stride;
305 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
307 p_dst = p_pic->p[i_plane].p_pixels;
308 i_dst_stride = p_pic->p[i_plane].i_pitch;
309 i_src_stride = i_pitch;
311 for( i_line = 0; i_line < p_pic->p[i_plane].i_visible_lines; i_line++ )
313 p_dec->p_vlc->pf_memcpy( p_dst, p_src, i_src_stride );
315 p_src += i_src_stride;
316 p_dst += i_dst_stride;