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