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