]> git.sesse.net Git - vlc/blob - modules/codec/tarkin.c
033bc99122570c7cdf38b764552603b25047134d
[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: tarkin.c,v 1.10 2003/11/23 03:55:01 fenrir Exp $
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_callbacks( OpenDecoder, CloseDecoder );
78     add_shortcut( "tarkin" );
79 vlc_module_end();
80
81 /*****************************************************************************
82  * OpenDecoder: probe the decoder and return score
83  *****************************************************************************/
84 static int OpenDecoder( vlc_object_t *p_this )
85 {
86     decoder_t *p_dec = (decoder_t*)p_this;
87     decoder_sys_t *p_sys;
88
89     if( p_dec->fmt_in.i_codec != VLC_FOURCC('t','a','r','k') )
90     {
91         return VLC_EGENERIC;
92     }
93
94     /* Allocate the memory needed to store the decoder's structure */
95     if( ( p_dec->p_sys = p_sys =
96           (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
97     {
98         msg_Err( p_dec, "out of memory" );
99         return VLC_EGENERIC;
100     }
101
102     /* Set output properties */
103     p_dec->fmt_out.i_cat = VIDEO_ES;
104     p_sys->i_headers = 0;
105
106     /* Set callbacks */
107     p_dec->pf_decode_video = (picture_t *(*)(decoder_t *, block_t **))
108         DecodeBlock;
109     p_dec->pf_packetize    = (block_t *(*)(decoder_t *, block_t **))
110         DecodeBlock;
111
112     /* Init supporting Tarkin structures needed in header parsing */
113     p_sys->tarkin_stream = tarkin_stream_new();
114     tarkin_info_init( &p_sys->ti );
115     tarkin_comment_init( &p_sys->tc );
116
117     return VLC_SUCCESS;
118 }
119
120 /****************************************************************************
121  * DecodeBlock: the whole thing
122  ****************************************************************************
123  * This function must be fed with ogg packets.
124  ****************************************************************************/
125 static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
126 {
127     decoder_sys_t *p_sys = p_dec->p_sys;
128     block_t *p_block;
129     ogg_packet oggpacket;
130
131     if( !pp_block ) return NULL;
132
133     if( *pp_block )
134     {
135         /* Block to Ogg packet */
136         oggpacket.packet = (*pp_block)->p_buffer;
137         oggpacket.bytes = (*pp_block)->i_buffer;
138     }
139     else
140     {
141         /* Block to Ogg packet */
142         oggpacket.packet = NULL;
143         oggpacket.bytes = 0;
144     }
145
146     p_block = *pp_block;
147
148     oggpacket.granulepos = -1;
149     oggpacket.b_o_s = 0;
150     oggpacket.e_o_s = 0;
151     oggpacket.packetno = 0;
152
153     if( p_sys->i_headers == 0 )
154     {
155         /* Take care of the initial Tarkin header */
156
157         oggpacket.b_o_s = 1; /* yes this actually is a b_o_s packet :) */
158         if( tarkin_synthesis_headerin( &p_sys->ti, &p_sys->tc, &oggpacket )
159             < 0 )
160         {
161             msg_Err( p_dec, "This bitstream does not contain Tarkin "
162                      "video data");
163             block_Release( p_block );
164             return NULL;
165         }
166         p_sys->i_headers++;
167
168         block_Release( p_block );
169         return NULL;
170     }
171
172     if( p_sys->i_headers == 1 )
173     {
174         if( tarkin_synthesis_headerin( &p_sys->ti, &p_sys->tc, &oggpacket )
175             < 0 )
176         {
177             msg_Err( p_dec, "2nd Tarkin header is corrupted" );
178             block_Release( p_block );
179             return NULL;
180         }
181         p_sys->i_headers++;
182         block_Release( p_block );
183         return NULL;
184     }
185
186     if( p_sys->i_headers == 2 )
187     {
188         if( tarkin_synthesis_headerin( &p_sys->ti, &p_sys->tc, &oggpacket )
189             < 0 )
190         {
191             msg_Err( p_dec, "3rd Tarkin header is corrupted" );
192             block_Release( p_block );
193             return NULL;
194         }
195         p_sys->i_headers++;
196
197         /* Initialize the tarkin decoder */
198         tarkin_synthesis_init( p_sys->tarkin_stream, &p_sys->ti );
199
200         msg_Err( p_dec, "Tarkin codec initialized");
201
202         block_Release( p_block );
203         return NULL;
204     }
205
206     return DecodePacket( p_dec, pp_block, &oggpacket );
207 }
208
209 /*****************************************************************************
210  * DecodePacket: decodes a Tarkin packet.
211  *****************************************************************************/
212 static picture_t *DecodePacket( decoder_t *p_dec, block_t **pp_block,
213                                 ogg_packet *p_oggpacket )
214 {
215     decoder_sys_t *p_sys = p_dec->p_sys;
216     uint8_t *rgb;
217
218     if( p_oggpacket->bytes )
219     {
220         tarkin_synthesis_packetin( p_sys->tarkin_stream, p_oggpacket );
221         //block_Release( *pp_block ); /* FIXME duplicate packet */
222         *pp_block = NULL;
223     }
224
225     if( tarkin_synthesis_frameout( p_sys->tarkin_stream,
226                                    &rgb, 0, &p_sys->tarkdate ) == 0 )
227     {
228         int i_width, i_height, i_chroma, i_stride;
229         picture_t *p_pic;
230
231         msg_Err( p_dec, "Tarkin frame decoded" );
232
233         i_width = p_sys->tarkin_stream->layer->desc.width;
234         i_height = p_sys->tarkin_stream->layer->desc.height;
235
236         switch( p_sys->tarkin_stream->layer->desc.format )
237         {
238         case TARKIN_RGB24:
239             i_chroma = VLC_FOURCC('R','V','2','4');
240             i_stride = i_width * 3;
241             break;
242         case TARKIN_RGB32:
243             i_chroma = VLC_FOURCC('R','V','3','2');
244             i_stride = i_width * 4;
245             break;
246         case TARKIN_RGBA:
247             i_chroma = VLC_FOURCC('R','G','B','A');
248             i_stride = i_width * 4;
249             break;
250         default:
251             i_chroma = VLC_FOURCC('I','4','2','0');
252             i_stride = i_width;
253             break;
254         }
255
256         /* Set output properties */
257         p_dec->fmt_out.video.i_width = i_width;
258         p_dec->fmt_out.video.i_height = i_height;
259
260         p_dec->fmt_out.video.i_aspect =
261             VOUT_ASPECT_FACTOR * i_width / i_height;
262         p_dec->fmt_out.i_codec = i_chroma;
263
264         /* Get a new picture */
265         if( (p_pic = p_dec->pf_vout_buffer_new( p_dec )) )
266         {
267             tarkin_CopyPicture( p_dec, p_pic, rgb, i_stride );
268
269             tarkin_synthesis_freeframe( p_sys->tarkin_stream, rgb );
270
271             p_pic->date = mdate() + DEFAULT_PTS_DELAY/*i_pts*/;
272
273             return p_pic;
274         }
275     }
276
277     return NULL;
278 }
279
280 /*****************************************************************************
281  * CloseDecoder: tarkin decoder destruction
282  *****************************************************************************/
283 static void CloseDecoder( vlc_object_t *p_this )
284 {
285     decoder_t *p_dec = (decoder_t *)p_this;
286     decoder_sys_t *p_sys = p_dec->p_sys;
287
288     tarkin_stream_destroy( p_sys->tarkin_stream );
289
290     free( p_sys );
291 }
292
293 /*****************************************************************************
294  * tarkin_CopyPicture: copy a picture from tarkin internal buffers to a
295  *                     picture_t structure.
296  *****************************************************************************/
297 static void tarkin_CopyPicture( decoder_t *p_dec, picture_t *p_pic,
298                                 uint8_t *p_src, int i_pitch )
299 {
300     int i_plane, i_line, i_src_stride, i_dst_stride;
301     uint8_t *p_dst;
302
303     for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
304     {
305         p_dst = p_pic->p[i_plane].p_pixels;
306         i_dst_stride = p_pic->p[i_plane].i_pitch;
307         i_src_stride = i_pitch;
308
309         for( i_line = 0; i_line < p_pic->p[i_plane].i_lines; i_line++ )
310         {
311             p_dec->p_vlc->pf_memcpy( p_dst, p_src, i_src_stride );
312
313             p_src += i_src_stride;
314             p_dst += i_dst_stride;
315         }
316     }
317 }