]> git.sesse.net Git - vlc/blob - modules/codec/theora.c
* modules/demux/ogg.c, modules/codec/theora.c: updated the ogg demuxer and theora...
[vlc] / modules / codec / theora.c
1 /*****************************************************************************
2  * theora.c: theora decoder module making use of libtheora.
3  *****************************************************************************
4  * Copyright (C) 1999-2001 VideoLAN
5  * $Id: theora.c,v 1.5 2003/06/11 15:53:50 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 #include <theora/theora.h>
38
39 /*****************************************************************************
40  * dec_thread_t : theora decoder thread descriptor
41  *****************************************************************************/
42 typedef struct dec_thread_t
43 {
44     /*
45      * Thread properties
46      */
47     vlc_thread_t        thread_id;                /* id for thread functions */
48
49     /*
50      * Theora properties
51      */
52     theora_info      ti;                        /* theora bitstream settings */
53     theora_comment   tc;                            /* theora comment header */
54     theora_state     td;                   /* theora bitstream user comments */
55
56     /*
57      * Input properties
58      */
59     decoder_fifo_t         *p_fifo;            /* stores the PES stream data */
60     pes_packet_t           *p_pes;            /* current PES we are decoding */
61
62     /*
63      * Output properties
64      */
65     vout_thread_t *p_vout;
66
67 } dec_thread_t;
68
69 /*****************************************************************************
70  * Local prototypes
71  *****************************************************************************/
72 static int  OpenDecoder  ( vlc_object_t * );
73 static int  RunDecoder   ( decoder_fifo_t * );
74 static void CloseDecoder ( dec_thread_t * );
75
76 static void DecodePacket ( dec_thread_t * );
77 static int  GetOggPacket ( dec_thread_t *, ogg_packet *, mtime_t * );
78
79 static void theora_CopyPicture( dec_thread_t *, picture_t *, yuv_buffer * );
80
81 /*****************************************************************************
82  * Module descriptor
83  *****************************************************************************/
84 vlc_module_begin();
85     set_description( _("Theora video decoder") );
86     set_capability( "decoder", 100 );
87     set_callbacks( OpenDecoder, NULL );
88     add_shortcut( "theora" );
89 vlc_module_end();
90
91 /*****************************************************************************
92  * OpenDecoder: probe the decoder and return score
93  *****************************************************************************/
94 static int OpenDecoder( vlc_object_t *p_this )
95 {
96     decoder_fifo_t *p_fifo = (decoder_fifo_t*) p_this;
97
98     if( p_fifo->i_fourcc != VLC_FOURCC('t','h','e','o') )
99     {
100         return VLC_EGENERIC;
101     }
102
103     p_fifo->pf_run = RunDecoder;
104     return VLC_SUCCESS;
105 }
106 /*****************************************************************************
107  * RunDecoder: the theora decoder
108  *****************************************************************************/
109 static int RunDecoder( decoder_fifo_t *p_fifo )
110 {
111     dec_thread_t *p_dec;
112     ogg_packet oggpacket;
113     int i_chroma, i_aspect;
114     mtime_t i_pts;
115
116     /* Allocate the memory needed to store the thread's structure */
117     if( (p_dec = (dec_thread_t *)malloc (sizeof(dec_thread_t)) )
118             == NULL)
119     {
120         msg_Err( p_fifo, "out of memory" );
121         goto error;
122     }
123
124     /* Initialize the thread properties */
125     memset( p_dec, 0, sizeof(dec_thread_t) );
126     p_dec->p_fifo = p_fifo;
127     p_dec->p_pes  = NULL;
128     p_dec->p_vout = NULL;
129
130     /* Init supporting Theora structures needed in header parsing */
131     theora_comment_init( &p_dec->tc );
132     theora_info_init( &p_dec->ti );
133
134     /* Take care of the initial Theora header */
135     if( GetOggPacket( p_dec, &oggpacket, &i_pts ) != VLC_SUCCESS )
136         goto error;
137
138     oggpacket.b_o_s = 1; /* yes this actually is a b_o_s packet :) */
139     if( theora_decode_header( &p_dec->ti, &p_dec->tc, &oggpacket ) < 0 )
140     {
141         msg_Err( p_dec->p_fifo, "This bitstream does not contain Theora "
142                  "video data" );
143         goto error;
144     }
145
146     /* The next two packets in order are the comment and codebook headers.
147        We need to watch out that these packets are not missing as a
148        missing or corrupted header is fatal. */
149     if( GetOggPacket( p_dec, &oggpacket, &i_pts ) != VLC_SUCCESS )
150         goto error;
151
152     if( theora_decode_header( &p_dec->ti, &p_dec->tc, &oggpacket ) < 0 )
153     {
154         msg_Err( p_dec->p_fifo, "2nd Theora header is corrupted" );
155         goto error;
156     }
157
158     if( GetOggPacket( p_dec, &oggpacket, &i_pts ) != VLC_SUCCESS )
159         goto error;
160
161     if( theora_decode_header( &p_dec->ti, &p_dec->tc, &oggpacket ) < 0 )
162     {
163         msg_Err( p_dec->p_fifo, "3rd Theora header is corrupted" );
164         goto error;
165     }
166
167     /* We have all the headers, initialize decoder */
168     theora_decode_init( &p_dec->td, &p_dec->ti );
169     msg_Dbg( p_dec->p_fifo, "%dx%d %.02f fps video, frame content is %dx%d "
170              "with offset (%d,%d)",
171              p_dec->ti.width, p_dec->ti.height,
172              (double)p_dec->ti.fps_numerator/p_dec->ti.fps_denominator,
173              p_dec->ti.frame_width, p_dec->ti.frame_height,
174              p_dec->ti.offset_x, p_dec->ti.offset_y );
175
176     /* Initialize video output */
177     if( p_dec->ti.aspect_denominator )
178         i_aspect = VOUT_ASPECT_FACTOR * p_dec->ti.aspect_numerator /
179                     p_dec->ti.aspect_denominator;
180     else
181         i_aspect = VOUT_ASPECT_FACTOR * p_dec->ti.width / p_dec->ti.height;
182
183     i_chroma = VLC_FOURCC('Y','V','1','2');
184
185     p_dec->p_vout = vout_Request( p_dec->p_fifo, p_dec->p_vout,
186                                   p_dec->ti.width, p_dec->ti.height,
187                                   i_chroma, i_aspect );
188
189     /* theora decoder thread's main loop */
190     while( (!p_dec->p_fifo->b_die) && (!p_dec->p_fifo->b_error) )
191     {
192         DecodePacket( p_dec );
193     }
194
195     /* If b_error is set, the theora decoder thread enters the error loop */
196     if( p_dec->p_fifo->b_error )
197     {
198         DecoderError( p_dec->p_fifo );
199     }
200
201     /* End of the theora decoder thread */
202     CloseDecoder( p_dec );
203
204     return 0;
205
206  error:
207     DecoderError( p_fifo );
208     if( p_dec )
209     {
210         if( p_dec->p_fifo )
211             p_dec->p_fifo->b_error = 1;
212
213         /* End of the theora decoder thread */
214         CloseDecoder( p_dec );
215     }
216
217     return -1;
218 }
219
220 /*****************************************************************************
221  * DecodePacket: decodes a Theora packet.
222  *****************************************************************************/
223 static void DecodePacket( dec_thread_t *p_dec )
224 {
225     ogg_packet oggpacket;
226     picture_t *p_pic;
227     mtime_t i_pts;
228     yuv_buffer yuv;
229
230     if( GetOggPacket( p_dec, &oggpacket, &i_pts ) != VLC_SUCCESS )
231     {
232         /* This should mean an eos */
233         return;
234     }
235
236     theora_decode_packetin( &p_dec->td, &oggpacket );
237
238     /* Decode */
239     theora_decode_YUVout( &p_dec->td, &yuv );
240
241     /* Get a new picture */
242     while( !(p_pic = vout_CreatePicture( p_dec->p_vout, 0, 0, 0 ) ) )
243     {
244         if( p_dec->p_fifo->b_die || p_dec->p_fifo->b_error )
245         {
246             return;
247         }
248         msleep( VOUT_OUTMEM_SLEEP );
249     }
250     if( !p_pic )
251         return;
252
253     theora_CopyPicture( p_dec, p_pic, &yuv );
254
255     vout_DatePicture( p_dec->p_vout, p_pic, i_pts );
256     vout_DisplayPicture( p_dec->p_vout, p_pic );
257 }
258
259 /*****************************************************************************
260  * GetOggPacket: get the following theora packet from the stream and send back
261  *               the result in an ogg packet (for easy decoding by libtheora).
262  *****************************************************************************
263  * Returns VLC_EGENERIC in case of eof.
264  *****************************************************************************/
265 static int GetOggPacket( dec_thread_t *p_dec, ogg_packet *p_oggpacket,
266                          mtime_t *p_pts )
267 {
268     if( p_dec->p_pes ) input_DeletePES( p_dec->p_fifo->p_packets_mgt,
269                                         p_dec->p_pes );
270
271     input_ExtractPES( p_dec->p_fifo, &p_dec->p_pes );
272     if( !p_dec->p_pes ) return VLC_EGENERIC;
273
274     p_oggpacket->packet = p_dec->p_pes->p_first->p_payload_start;
275     p_oggpacket->bytes = p_dec->p_pes->i_pes_size;
276     p_oggpacket->granulepos = p_dec->p_pes->i_dts;
277     p_oggpacket->b_o_s = 0;
278     p_oggpacket->e_o_s = 0;
279     p_oggpacket->packetno = 0;
280
281     *p_pts = p_dec->p_pes->i_pts;
282
283     return VLC_SUCCESS;
284 }
285
286 /*****************************************************************************
287  * CloseDecoder: theora decoder destruction
288  *****************************************************************************/
289 static void CloseDecoder( dec_thread_t * p_dec )
290 {
291
292     if( p_dec )
293     {
294         if( p_dec->p_pes )
295             input_DeletePES( p_dec->p_fifo->p_packets_mgt, p_dec->p_pes );
296
297         vout_Request( p_dec->p_fifo, p_dec->p_vout, 0, 0, 0, 0 );
298
299         theora_info_clear( &p_dec->ti );
300         theora_comment_clear( &p_dec->tc );
301
302         free( p_dec );
303     }
304 }
305
306 /*****************************************************************************
307  * theora_CopyPicture: copy a picture from theora internal buffers to a
308  *                     picture_t structure.
309  *****************************************************************************/
310 static void theora_CopyPicture( dec_thread_t *p_dec, picture_t *p_pic,
311                                 yuv_buffer *yuv )
312 {
313     int i_plane, i_line, i_width, i_dst_stride, i_src_stride;
314     u8  *p_dst, *p_src;
315
316     for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
317     {
318         p_dst = p_pic->p[i_plane].p_pixels;
319         p_src = i_plane ? (i_plane - 1 ? yuv->v : yuv->u ) : yuv->y;
320         i_width = p_pic->p[i_plane].i_visible_pitch;
321         i_dst_stride = p_pic->p[i_plane].i_pitch;
322         i_src_stride = i_plane ? yuv->uv_stride : yuv->y_stride;
323
324         for( i_line = 0; i_line < p_pic->p[i_plane].i_lines; i_line++ )
325         {
326             p_dec->p_fifo->p_vlc->pf_memcpy( p_dst, p_src, i_width );
327             p_src += i_src_stride;
328             p_dst += i_dst_stride;
329         }
330     }
331 }