]> git.sesse.net Git - vlc/blob - modules/codec/theora.c
* modules/*: sanitization of the modules description strings.
[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.4 2003/03/30 18:14:36 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_state     td;                   /* theora bitstream user comments */
54
55     /*
56      * Input properties
57      */
58     decoder_fifo_t         *p_fifo;            /* stores the PES stream data */
59     pes_packet_t           *p_pes;            /* current PES we are decoding */
60
61     /*
62      * Output properties
63      */
64     vout_thread_t *p_vout;
65
66 } dec_thread_t;
67
68 /*****************************************************************************
69  * Local prototypes
70  *****************************************************************************/
71 static int  OpenDecoder  ( vlc_object_t * );
72 static int  RunDecoder   ( decoder_fifo_t * );
73 static void CloseDecoder ( dec_thread_t * );
74
75 static void DecodePacket ( dec_thread_t * );
76 static int  GetOggPacket ( dec_thread_t *, ogg_packet *, mtime_t * );
77
78 static void theora_CopyPicture( dec_thread_t *, picture_t *, yuv_buffer * );
79
80 /*****************************************************************************
81  * Module descriptor
82  *****************************************************************************/
83 vlc_module_begin();
84     set_description( _("Theora video decoder") );
85     set_capability( "decoder", 100 );
86     set_callbacks( OpenDecoder, NULL );
87     add_shortcut( "theora" );
88 vlc_module_end();
89
90 /*****************************************************************************
91  * OpenDecoder: probe the decoder and return score
92  *****************************************************************************/
93 static int OpenDecoder( vlc_object_t *p_this )
94 {
95     decoder_fifo_t *p_fifo = (decoder_fifo_t*) p_this;
96
97     if( p_fifo->i_fourcc != VLC_FOURCC('t','h','e','o') )
98     {
99         return VLC_EGENERIC;
100     }
101
102     p_fifo->pf_run = RunDecoder;
103     return VLC_SUCCESS;
104 }
105 /*****************************************************************************
106  * RunDecoder: the theora decoder
107  *****************************************************************************/
108 static int RunDecoder( decoder_fifo_t *p_fifo )
109 {
110     dec_thread_t *p_dec;
111     ogg_packet oggpacket;
112     int i_chroma, i_aspect;
113     mtime_t i_pts;
114
115     /* Allocate the memory needed to store the thread's structure */
116     if( (p_dec = (dec_thread_t *)malloc (sizeof(dec_thread_t)) )
117             == NULL)
118     {
119         msg_Err( p_fifo, "out of memory" );
120         goto error;
121     }
122
123     /* Initialize the thread properties */
124     memset( p_dec, 0, sizeof(dec_thread_t) );
125     p_dec->p_fifo = p_fifo;
126     p_dec->p_pes  = NULL;
127     p_dec->p_vout = NULL;
128
129     /* Take care of the initial Theora header */
130     if( GetOggPacket( p_dec, &oggpacket, &i_pts ) != VLC_SUCCESS )
131         goto error;
132
133     oggpacket.b_o_s = 1; /* yes this actually is a b_o_s packet :) */
134     if( theora_decode_header( &p_dec->ti, &oggpacket ) < 0 )
135     {
136         msg_Err( p_dec->p_fifo, "This bitstream does not contain Theora "
137                  "video data");
138         goto error;
139     }
140
141     /* Initialize decoder */
142     theora_decode_init( &p_dec->td, &p_dec->ti );
143     msg_Dbg( p_dec->p_fifo, "%dx%d %.02f fps video",
144              p_dec->ti.width, p_dec->ti.height,
145              (double)p_dec->ti.fps_numerator/p_dec->ti.fps_denominator);
146
147     /* Initialize video output */
148     if( p_dec->ti.aspect_denominator )
149         i_aspect = VOUT_ASPECT_FACTOR * p_dec->ti.aspect_numerator /
150                     p_dec->ti.aspect_denominator;
151     else
152         i_aspect = VOUT_ASPECT_FACTOR * p_dec->ti.width / p_dec->ti.height;
153
154     i_chroma = VLC_FOURCC('Y','V','1','2');
155
156     p_dec->p_vout = vout_Request( p_dec->p_fifo, p_dec->p_vout,
157                                   p_dec->ti.width, p_dec->ti.height,
158                                   i_chroma, i_aspect );
159
160     /* theora decoder thread's main loop */
161     while( (!p_dec->p_fifo->b_die) && (!p_dec->p_fifo->b_error) )
162     {
163         DecodePacket( p_dec );
164     }
165
166     /* If b_error is set, the theora decoder thread enters the error loop */
167     if( p_dec->p_fifo->b_error )
168     {
169         DecoderError( p_dec->p_fifo );
170     }
171
172     /* End of the theora decoder thread */
173     CloseDecoder( p_dec );
174
175     return 0;
176
177  error:
178     DecoderError( p_fifo );
179     if( p_dec )
180     {
181         if( p_dec->p_fifo )
182             p_dec->p_fifo->b_error = 1;
183
184         /* End of the theora decoder thread */
185         CloseDecoder( p_dec );
186     }
187
188     return -1;
189 }
190
191 /*****************************************************************************
192  * DecodePacket: decodes a Theora packet.
193  *****************************************************************************/
194 static void DecodePacket( dec_thread_t *p_dec )
195 {
196     ogg_packet oggpacket;
197     picture_t *p_pic;
198     mtime_t i_pts;
199     yuv_buffer yuv;
200
201     if( GetOggPacket( p_dec, &oggpacket, &i_pts ) != VLC_SUCCESS )
202     {
203         /* This should mean an eos */
204         return;
205     }
206
207     theora_decode_packetin( &p_dec->td, &oggpacket );
208
209     /* Decode */
210     theora_decode_YUVout( &p_dec->td, &yuv );
211
212     /* Get a new picture */
213     while( !(p_pic = vout_CreatePicture( p_dec->p_vout, 0, 0, 0 ) ) )
214     {
215         if( p_dec->p_fifo->b_die || p_dec->p_fifo->b_error )
216         {
217             return;
218         }
219         msleep( VOUT_OUTMEM_SLEEP );
220     }
221     if( !p_pic )
222         return;
223
224     theora_CopyPicture( p_dec, p_pic, &yuv );
225
226     vout_DatePicture( p_dec->p_vout, p_pic, i_pts );
227     vout_DisplayPicture( p_dec->p_vout, p_pic );
228 }
229
230 /*****************************************************************************
231  * GetOggPacket: get the following theora packet from the stream and send back
232  *               the result in an ogg packet (for easy decoding by libtheora).
233  *****************************************************************************
234  * Returns VLC_EGENERIC in case of eof.
235  *****************************************************************************/
236 static int GetOggPacket( dec_thread_t *p_dec, ogg_packet *p_oggpacket,
237                          mtime_t *p_pts )
238 {
239     if( p_dec->p_pes ) input_DeletePES( p_dec->p_fifo->p_packets_mgt,
240                                         p_dec->p_pes );
241
242     input_ExtractPES( p_dec->p_fifo, &p_dec->p_pes );
243     if( !p_dec->p_pes ) return VLC_EGENERIC;
244
245     p_oggpacket->packet = p_dec->p_pes->p_first->p_payload_start;
246     p_oggpacket->bytes = p_dec->p_pes->i_pes_size;
247     p_oggpacket->granulepos = p_dec->p_pes->i_dts;
248     p_oggpacket->b_o_s = 0;
249     p_oggpacket->e_o_s = 0;
250     p_oggpacket->packetno = 0;
251
252     *p_pts = p_dec->p_pes->i_pts;
253
254     return VLC_SUCCESS;
255 }
256
257 /*****************************************************************************
258  * CloseDecoder: theora decoder destruction
259  *****************************************************************************/
260 static void CloseDecoder( dec_thread_t * p_dec )
261 {
262
263     if( p_dec )
264     {
265         if( p_dec->p_pes )
266             input_DeletePES( p_dec->p_fifo->p_packets_mgt, p_dec->p_pes );
267
268         vout_Request( p_dec->p_fifo, p_dec->p_vout, 0, 0, 0, 0 );
269
270         free( p_dec );
271     }
272 }
273
274 /*****************************************************************************
275  * theora_CopyPicture: copy a picture from theora internal buffers to a
276  *                     picture_t structure.
277  *****************************************************************************/
278 static void theora_CopyPicture( dec_thread_t *p_dec, picture_t *p_pic,
279                                 yuv_buffer *yuv )
280 {
281     int i_plane, i_line, i_width, i_dst_stride, i_src_stride;
282     u8  *p_dst, *p_src;
283
284     for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
285     {
286         p_dst = p_pic->p[i_plane].p_pixels;
287         p_src = i_plane ? (i_plane - 1 ? yuv->v : yuv->u ) : yuv->y;
288         i_width = p_pic->p[i_plane].i_visible_pitch;
289         i_dst_stride = p_pic->p[i_plane].i_pitch;
290         i_src_stride = i_plane ? yuv->uv_stride : yuv->y_stride;
291
292         for( i_line = 0; i_line < p_pic->p[i_plane].i_lines; i_line++ )
293         {
294             p_dec->p_fifo->p_vlc->pf_memcpy( p_dst, p_src, i_width );
295             p_src += i_src_stride;
296             p_dst += i_dst_stride;
297         }
298     }
299 }