]> git.sesse.net Git - vlc/blob - modules/codec/tarkin.c
Improvements to preferences
[vlc] / modules / codec / tarkin.c
1 /*****************************************************************************
2  * tarkin.c: tarkin decoder module making use of libtarkin.
3  *****************************************************************************
4  * Copyright (C) 2001-2003 VideoLAN
5  * $Id$
6  *
7  * Authors: Gildas Bazin <gbazin@netcourrier.com>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <vlc/vlc.h>
28 #include <vlc/decoder.h>
29
30 #include <ogg/ogg.h>
31
32 /* FIXME */
33 // use 16 bit signed integers as wavelet coefficients
34 #define TYPE int16_t
35 // we'll actually use TYPE_BITS bits of type (e.g. 9 magnitude + 1 sign)
36 #define TYPE_BITS 10
37 // use the rle entropy coder
38 #define RLECODER 1
39
40 #include <tarkin.h>
41
42 /*****************************************************************************
43  * decoder_sys_t : tarkin decoder descriptor
44  *****************************************************************************/
45 struct decoder_sys_t
46 {
47     /*
48      * Tarkin properties
49      */
50     TarkinStream *tarkin_stream;
51
52     TarkinInfo       ti;                        /* tarkin bitstream settings */
53     TarkinComment    tc;                   /* tarkin bitstream user comments */
54     TarkinTime       tarkdate;
55
56     int i_headers;
57     mtime_t i_pts;
58 };
59
60 /*****************************************************************************
61  * Local prototypes
62  *****************************************************************************/
63 static int  OpenDecoder  ( vlc_object_t * );
64 static void CloseDecoder ( vlc_object_t * );
65
66 static void *DecodeBlock ( decoder_t *, block_t ** );
67 static picture_t *DecodePacket ( decoder_t *, block_t **, ogg_packet * );
68
69 static void tarkin_CopyPicture( decoder_t *, picture_t *, uint8_t *, int );
70
71 /*****************************************************************************
72  * Module descriptor
73  *****************************************************************************/
74 vlc_module_begin();
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" );
81 vlc_module_end();
82
83 /*****************************************************************************
84  * OpenDecoder: probe the decoder and return score
85  *****************************************************************************/
86 static int OpenDecoder( vlc_object_t *p_this )
87 {
88     decoder_t *p_dec = (decoder_t*)p_this;
89     decoder_sys_t *p_sys;
90
91     if( p_dec->fmt_in.i_codec != VLC_FOURCC('t','a','r','k') )
92     {
93         return VLC_EGENERIC;
94     }
95
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 )
99     {
100         msg_Err( p_dec, "out of memory" );
101         return VLC_EGENERIC;
102     }
103
104     /* Set output properties */
105     p_dec->fmt_out.i_cat = VIDEO_ES;
106     p_sys->i_headers = 0;
107
108     /* Set callbacks */
109     p_dec->pf_decode_video = (picture_t *(*)(decoder_t *, block_t **))
110         DecodeBlock;
111     p_dec->pf_packetize    = (block_t *(*)(decoder_t *, block_t **))
112         DecodeBlock;
113
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 );
118
119     return VLC_SUCCESS;
120 }
121
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 )
128 {
129     decoder_sys_t *p_sys = p_dec->p_sys;
130     block_t *p_block;
131     ogg_packet oggpacket;
132
133     if( !pp_block ) return NULL;
134
135     if( *pp_block )
136     {
137         /* Block to Ogg packet */
138         oggpacket.packet = (*pp_block)->p_buffer;
139         oggpacket.bytes = (*pp_block)->i_buffer;
140     }
141     else
142     {
143         /* Block to Ogg packet */
144         oggpacket.packet = NULL;
145         oggpacket.bytes = 0;
146     }
147
148     p_block = *pp_block;
149
150     oggpacket.granulepos = -1;
151     oggpacket.b_o_s = 0;
152     oggpacket.e_o_s = 0;
153     oggpacket.packetno = 0;
154
155     if( p_sys->i_headers == 0 )
156     {
157         /* Take care of the initial Tarkin header */
158
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 )
161             < 0 )
162         {
163             msg_Err( p_dec, "This bitstream does not contain Tarkin "
164                      "video data.");
165             block_Release( p_block );
166             return NULL;
167         }
168         p_sys->i_headers++;
169
170         block_Release( p_block );
171         return NULL;
172     }
173
174     if( p_sys->i_headers == 1 )
175     {
176         if( tarkin_synthesis_headerin( &p_sys->ti, &p_sys->tc, &oggpacket )
177             < 0 )
178         {
179             msg_Err( p_dec, "2nd Tarkin header is corrupted." );
180             block_Release( p_block );
181             return NULL;
182         }
183         p_sys->i_headers++;
184         block_Release( p_block );
185         return NULL;
186     }
187
188     if( p_sys->i_headers == 2 )
189     {
190         if( tarkin_synthesis_headerin( &p_sys->ti, &p_sys->tc, &oggpacket )
191             < 0 )
192         {
193             msg_Err( p_dec, "3rd Tarkin header is corrupted." );
194             block_Release( p_block );
195             return NULL;
196         }
197         p_sys->i_headers++;
198
199         /* Initialize the tarkin decoder */
200         tarkin_synthesis_init( p_sys->tarkin_stream, &p_sys->ti );
201
202         msg_Err( p_dec, "Tarkin codec initialized");
203
204         block_Release( p_block );
205         return NULL;
206     }
207
208     return DecodePacket( p_dec, pp_block, &oggpacket );
209 }
210
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 )
216 {
217     decoder_sys_t *p_sys = p_dec->p_sys;
218     uint8_t *rgb;
219
220     if( p_oggpacket->bytes )
221     {
222         tarkin_synthesis_packetin( p_sys->tarkin_stream, p_oggpacket );
223         //block_Release( *pp_block ); /* FIXME duplicate packet */
224         *pp_block = NULL;
225     }
226
227     if( tarkin_synthesis_frameout( p_sys->tarkin_stream,
228                                    &rgb, 0, &p_sys->tarkdate ) == 0 )
229     {
230         int i_width, i_height, i_chroma, i_stride;
231         picture_t *p_pic;
232
233         msg_Err( p_dec, "Tarkin frame decoded" );
234
235         i_width = p_sys->tarkin_stream->layer->desc.width;
236         i_height = p_sys->tarkin_stream->layer->desc.height;
237
238         switch( p_sys->tarkin_stream->layer->desc.format )
239         {
240         case TARKIN_RGB24:
241             i_chroma = VLC_FOURCC('R','V','2','4');
242             i_stride = i_width * 3;
243             break;
244         case TARKIN_RGB32:
245             i_chroma = VLC_FOURCC('R','V','3','2');
246             i_stride = i_width * 4;
247             break;
248         case TARKIN_RGBA:
249             i_chroma = VLC_FOURCC('R','G','B','A');
250             i_stride = i_width * 4;
251             break;
252         default:
253             i_chroma = VLC_FOURCC('I','4','2','0');
254             i_stride = i_width;
255             break;
256         }
257
258         /* Set output properties */
259         p_dec->fmt_out.video.i_width = i_width;
260         p_dec->fmt_out.video.i_height = i_height;
261
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;
265
266         /* Get a new picture */
267         if( (p_pic = p_dec->pf_vout_buffer_new( p_dec )) )
268         {
269             tarkin_CopyPicture( p_dec, p_pic, rgb, i_stride );
270
271             tarkin_synthesis_freeframe( p_sys->tarkin_stream, rgb );
272
273             p_pic->date = mdate() + DEFAULT_PTS_DELAY/*i_pts*/;
274
275             return p_pic;
276         }
277     }
278
279     return NULL;
280 }
281
282 /*****************************************************************************
283  * CloseDecoder: tarkin decoder destruction
284  *****************************************************************************/
285 static void CloseDecoder( vlc_object_t *p_this )
286 {
287     decoder_t *p_dec = (decoder_t *)p_this;
288     decoder_sys_t *p_sys = p_dec->p_sys;
289
290     tarkin_stream_destroy( p_sys->tarkin_stream );
291
292     free( p_sys );
293 }
294
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 )
301 {
302     int i_plane, i_line, i_src_stride, i_dst_stride;
303     uint8_t *p_dst;
304
305     for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
306     {
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;
310
311         for( i_line = 0; i_line < p_pic->p[i_plane].i_visible_lines; i_line++ )
312         {
313             p_dec->p_vlc->pf_memcpy( p_dst, p_src, i_src_stride );
314
315             p_src += i_src_stride;
316             p_dst += i_dst_stride;
317         }
318     }
319 }