]> git.sesse.net Git - vlc/blob - modules/codec/tarkin.c
3ddcdf1afb842c7b090ea04f767cb3583dd6a8ee
[vlc] / modules / codec / tarkin.c
1 /*****************************************************************************
2  * tarkin.c: tarkin decoder module making use of libtarkin.
3  *****************************************************************************
4  * Copyright (C) 1999-2001 VideoLAN
5  * $Id: tarkin.c,v 1.2 2002/11/20 14:09:57 gbazin 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/vout.h>
29 #include <vlc/input.h>
30 #include <vlc/decoder.h>
31
32 #include <stdlib.h>                                      /* malloc(), free() */
33 #include <string.h>                                    /* memcpy(), memset() */
34
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  * dec_thread_t : tarkin decoder thread descriptor
49  *****************************************************************************/
50 typedef struct dec_thread_t
51 {
52     /*
53      * Thread properties
54      */
55     vlc_thread_t        thread_id;                /* id for thread functions */
56
57     /*
58      * Tarkin properties
59      */
60     TarkinStream *tarkin_stream;
61
62     TarkinInfo       ti;                        /* tarkin bitstream settings */
63     TarkinComment    tc;                   /* tarkin bitstream user comments */
64     TarkinTime           tarkdate;
65
66     /*
67      * Input properties
68      */
69     decoder_fifo_t         *p_fifo;            /* stores the PES stream data */
70     pes_packet_t           *p_pes;            /* current PES we are decoding */
71
72     /*
73      * Output properties
74      */
75     vout_thread_t *p_vout;
76
77 } dec_thread_t;
78
79 /*****************************************************************************
80  * Local prototypes
81  *****************************************************************************/
82 static int  OpenDecoder  ( vlc_object_t * );
83 static int  RunDecoder   ( decoder_fifo_t * );
84 static void CloseDecoder ( dec_thread_t * );
85
86 static void DecodePacket ( dec_thread_t * );
87 static int  GetOggPacket ( dec_thread_t *, ogg_packet *, mtime_t * );
88
89 static void tarkin_CopyPicture( dec_thread_t *, picture_t *, uint8_t * );
90 static vout_thread_t *tarkin_SpawnVout( dec_thread_t *, int, int, int, int );
91 /*****************************************************************************
92  * Module descriptor
93  *****************************************************************************/
94 vlc_module_begin();
95     set_description( _("Tarkin decoder module") );
96     set_capability( "decoder", 100 );
97     set_callbacks( OpenDecoder, NULL );
98     add_shortcut( "tarkin" );
99 vlc_module_end();
100
101 /*****************************************************************************
102  * OpenDecoder: probe the decoder and return score
103  *****************************************************************************/
104 static int OpenDecoder( vlc_object_t *p_this )
105 {
106     decoder_fifo_t *p_fifo = (decoder_fifo_t*) p_this;
107
108     if( p_fifo->i_fourcc != VLC_FOURCC('t','a','r','k') )
109     {
110         return VLC_EGENERIC;
111     }
112
113     p_fifo->pf_run = RunDecoder;
114     return VLC_SUCCESS;
115 }
116
117 /*****************************************************************************
118  * RunDecoder: the tarkin decoder
119  *****************************************************************************/
120 static int RunDecoder( decoder_fifo_t *p_fifo )
121 {
122     dec_thread_t *p_dec;
123     ogg_packet oggpacket;
124     mtime_t i_pts;
125
126     /* Allocate the memory needed to store the thread's structure */
127     if( (p_dec = (dec_thread_t *)malloc (sizeof(dec_thread_t)) )
128             == NULL)
129     {
130         msg_Err( p_fifo, "out of memory" );
131         goto error;
132     }
133
134     /* Initialize the thread properties */
135     memset( p_dec, 0, sizeof(dec_thread_t) );
136     p_dec->p_fifo = p_fifo;
137     p_dec->p_pes  = NULL;
138
139     /* Take care of the initial Tarkin header */
140     p_dec->tarkin_stream = tarkin_stream_new();
141     tarkin_info_init(&p_dec->ti);
142     tarkin_comment_init(&p_dec->tc);
143
144     if( GetOggPacket( p_dec, &oggpacket, &i_pts ) != VLC_SUCCESS )
145         goto error;
146
147     oggpacket.b_o_s = 1; /* yes this actually is a b_o_s packet :) */
148     if( tarkin_synthesis_headerin( &p_dec->ti, &p_dec->tc, &oggpacket ) < 0 )
149     {
150         msg_Err( p_dec->p_fifo, "This bitstream does not contain Tarkin "
151                  "video data");
152         goto error;
153     }
154
155     /* The next two packets in order are the comment and codebook headers.
156        We need to watch out that these packets are not missing as a
157        missing or corrupted header is fatal. */
158     if( GetOggPacket( p_dec, &oggpacket, &i_pts ) != VLC_SUCCESS )
159         goto error;
160
161     if( tarkin_synthesis_headerin( &p_dec->ti, &p_dec->tc, &oggpacket ) < 0 )
162     {
163         msg_Err( p_dec->p_fifo, "2nd Tarkin header is corrupted" );
164         goto error;
165     }
166
167     if( GetOggPacket( p_dec, &oggpacket, &i_pts ) != VLC_SUCCESS )
168         goto error;
169
170     if( tarkin_synthesis_headerin( &p_dec->ti, &p_dec->tc, &oggpacket ) < 0 )
171     {
172         msg_Err( p_dec->p_fifo, "3rd Tarkin header is corrupted" );
173         goto error;
174     }
175
176     /* Initialize the tarkin decoder */
177     tarkin_synthesis_init( p_dec->tarkin_stream, &p_dec->ti );
178
179     /* tarkin decoder thread's main loop */
180     while( (!p_dec->p_fifo->b_die) && (!p_dec->p_fifo->b_error) )
181     {
182         DecodePacket( p_dec );
183     }
184
185     /* If b_error is set, the tarkin decoder thread enters the error loop */
186     if( p_dec->p_fifo->b_error )
187     {
188         DecoderError( p_dec->p_fifo );
189     }
190
191     /* End of the tarkin decoder thread */
192     CloseDecoder( p_dec );
193
194     return 0;
195
196  error:
197     DecoderError( p_fifo );
198     if( p_dec )
199     {
200         if( p_dec->p_fifo )
201             p_dec->p_fifo->b_error = 1;
202
203         /* End of the tarkin decoder thread */
204         CloseDecoder( p_dec );
205     }
206
207     return -1;
208 }
209
210 /*****************************************************************************
211  * DecodePacket: decodes a Tarkin packet.
212  *****************************************************************************/
213 static void DecodePacket( dec_thread_t *p_dec )
214 {
215     ogg_packet oggpacket;
216     picture_t *p_pic;
217     mtime_t i_pts;
218     int i_width, i_height, i_chroma, i_aspect;
219     uint32_t frame = 0;
220     uint8_t *rgb;
221
222     if( GetOggPacket( p_dec, &oggpacket, &i_pts ) != VLC_SUCCESS )
223     {
224         /* This should mean an eos */
225         return;
226     }
227
228     tarkin_synthesis_packetin( p_dec->tarkin_stream, &oggpacket );
229
230     while( tarkin_synthesis_frameout( p_dec->tarkin_stream,
231                                       &rgb, 0, &p_dec->tarkdate ) == 0 )
232     {
233
234         i_width = p_dec->tarkin_stream->layer->desc.width;
235         i_height = p_dec->tarkin_stream->layer->desc.height;
236         switch( p_dec->tarkin_stream->layer->desc.format )
237         {
238         case TARKIN_RGB24:
239             i_chroma = VLC_FOURCC('R','V','2','4');
240             break;
241         case TARKIN_RGB32:
242             i_chroma = VLC_FOURCC('R','V','3','2');
243             break;
244         case TARKIN_RGBA:
245             i_chroma = VLC_FOURCC('R','G','B','A');
246             break;
247         default:
248             i_chroma = VLC_FOURCC('Y','V','1','2');
249             break;
250         }
251         i_aspect = VOUT_ASPECT_FACTOR * i_width / i_height;
252         p_dec->p_vout = tarkin_SpawnVout( p_dec, i_width, i_height,
253                                           i_aspect, i_chroma );
254
255         /* Get a new picture */
256         while( !(p_pic = vout_CreatePicture( p_dec->p_vout, 0, 0, 0 ) ) )
257         {
258             if( p_dec->p_fifo->b_die || p_dec->p_fifo->b_error )
259             {
260                 return;
261             }
262             msleep( VOUT_OUTMEM_SLEEP );
263         }
264         if( !p_pic )
265             break;
266
267         tarkin_CopyPicture( p_dec, p_pic, rgb );
268
269         tarkin_synthesis_freeframe( p_dec->tarkin_stream, rgb );
270
271         vout_DatePicture( p_dec->p_vout, p_pic, mdate()+DEFAULT_PTS_DELAY/*i_pts*/ );
272         vout_DisplayPicture( p_dec->p_vout, p_pic );
273
274     }
275 }
276
277 /*****************************************************************************
278  * GetOggPacket: get the following tarkin packet from the stream and send back
279  *               the result in an ogg packet (for easy decoding by libtarkin).
280  *****************************************************************************
281  * Returns VLC_EGENERIC in case of eof.
282  *****************************************************************************/
283 static int GetOggPacket( dec_thread_t *p_dec, ogg_packet *p_oggpacket,
284                          mtime_t *p_pts )
285 {
286     if( p_dec->p_pes ) input_DeletePES( p_dec->p_fifo->p_packets_mgt,
287                                         p_dec->p_pes );
288
289     input_ExtractPES( p_dec->p_fifo, &p_dec->p_pes );
290     if( !p_dec->p_pes ) return VLC_EGENERIC;
291
292     p_oggpacket->packet = p_dec->p_pes->p_first->p_payload_start;
293     p_oggpacket->bytes = p_dec->p_pes->i_pes_size;
294     p_oggpacket->granulepos = p_dec->p_pes->i_dts;
295     p_oggpacket->b_o_s = 0;
296     p_oggpacket->e_o_s = 0;
297     p_oggpacket->packetno = 0;
298
299     *p_pts = p_dec->p_pes->i_pts;
300
301     return VLC_SUCCESS;
302 }
303
304 /*****************************************************************************
305  * CloseDecoder: tarkin decoder destruction
306  *****************************************************************************/
307 static void CloseDecoder( dec_thread_t * p_dec )
308 {
309
310     if( p_dec )
311     {
312         if( p_dec->p_pes )
313             input_DeletePES( p_dec->p_fifo->p_packets_mgt, p_dec->p_pes );
314
315         if( p_dec->p_vout )
316         {
317             vlc_object_detach( p_dec->p_vout );
318             vout_DestroyThread( p_dec->p_vout );
319         }
320
321         if( p_dec->tarkin_stream )
322             tarkin_stream_destroy( p_dec->tarkin_stream );
323
324         free( p_dec );
325     }
326 }
327
328 /*****************************************************************************
329  * tarkin_SpawnVout: creates a new video output
330  *****************************************************************************/
331 static vout_thread_t *tarkin_SpawnVout( dec_thread_t *p_dec,
332                                         int i_width,
333                                         int i_height,
334                                         int i_aspect,
335                                         int i_chroma )
336 {
337     vout_thread_t *p_vout;
338
339     if( !i_width || !i_height )
340         return NULL;
341
342     if( !i_chroma )
343         return NULL;
344
345     /* Spawn a video output if there is none. First we look for our children,
346      * then we look for any other vout that might be available. */
347     p_vout = vlc_object_find( p_dec->p_fifo, VLC_OBJECT_VOUT,
348                                               FIND_CHILD );
349     if( !p_vout )
350     {
351         p_vout = vlc_object_find( p_dec->p_fifo, VLC_OBJECT_VOUT,
352                                                   FIND_ANYWHERE );
353     }
354
355     if( p_vout )
356     {
357         if( p_vout->render.i_width != i_width
358             || p_vout->render.i_height != i_height
359             || p_vout->render.i_chroma != i_chroma
360             || p_vout->render.i_aspect != i_aspect )
361         {
362             /* We are not interested in this format, close this vout */
363             vlc_object_detach( p_vout );
364             vlc_object_release( p_vout );
365             vout_DestroyThread( p_vout );
366             p_vout = NULL;
367         }
368         else
369         {
370             /* This video output is cool! Hijack it. */
371             vlc_object_detach( p_vout );
372             vlc_object_attach( p_vout, p_dec->p_fifo );
373             vlc_object_release( p_vout );
374         }
375     }
376
377     if( p_vout == NULL )
378     {
379         msg_Dbg( p_dec->p_fifo, "no vout present, spawning one" );
380
381         p_vout = vout_CreateThread( p_dec->p_fifo,
382                                     i_width, i_height,
383                                     i_chroma, i_aspect );
384     }
385
386     return( p_vout );
387 }
388
389 /*****************************************************************************
390  * tarkin_CopyPicture: copy a picture from tarkin internal buffers to a
391  *                     picture_t structure.
392  *****************************************************************************/
393 static void tarkin_CopyPicture( dec_thread_t *p_dec, picture_t *p_pic,
394                                 uint8_t *p_src )
395 {
396     int i_plane, i_line, i_width, i_dst_stride;
397     u8  *p_dst;
398
399     for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
400     {
401         p_dst = p_pic->p[i_plane].p_pixels;
402         i_width = p_pic->p[i_plane].i_visible_pitch;
403         i_dst_stride = p_pic->p[i_plane].i_pitch;
404
405         for( i_line = 0; i_line < p_pic->p[i_plane].i_lines; i_line++ )
406         {
407             p_dec->p_fifo->p_vlc->pf_memcpy( p_dst, p_src, i_width );
408             p_src += i_width;
409             p_dst += i_dst_stride;
410         }
411     }
412 }