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