1 /*****************************************************************************
2 * theora.c: theora decoder module making use of libtheora.
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VideoLAN
5 * $Id: theora.c,v 1.1 2002/11/20 14:09:57 gbazin Exp $
7 * Authors: Gildas Bazin <gbazin@netcourrier.com>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
29 #include <vlc/input.h>
30 #include <vlc/decoder.h>
32 #include <stdlib.h> /* malloc(), free() */
33 #include <string.h> /* memcpy(), memset() */
37 #include <theora/theora.h>
39 /*****************************************************************************
40 * dec_thread_t : theora decoder thread descriptor
41 *****************************************************************************/
42 typedef struct dec_thread_t
47 vlc_thread_t thread_id; /* id for thread functions */
52 theora_info ti; /* theora bitstream settings */
53 theora_state td; /* theora bitstream user comments */
58 decoder_fifo_t *p_fifo; /* stores the PES stream data */
59 pes_packet_t *p_pes; /* current PES we are decoding */
64 vout_thread_t *p_vout;
68 /*****************************************************************************
70 *****************************************************************************/
71 static int OpenDecoder ( vlc_object_t * );
72 static int RunDecoder ( decoder_fifo_t * );
73 static void CloseDecoder ( dec_thread_t * );
75 static void DecodePacket ( dec_thread_t * );
76 static int GetOggPacket ( dec_thread_t *, ogg_packet *, mtime_t * );
78 static void theora_CopyPicture( dec_thread_t *, picture_t *, yuv_buffer * );
79 static vout_thread_t *theora_SpawnVout( dec_thread_t *, int, int, int, int );
80 /*****************************************************************************
82 *****************************************************************************/
84 set_description( _("Theora decoder module") );
85 set_capability( "decoder", 100 );
86 set_callbacks( OpenDecoder, NULL );
87 add_shortcut( "theora" );
90 /*****************************************************************************
91 * OpenDecoder: probe the decoder and return score
92 *****************************************************************************/
93 static int OpenDecoder( vlc_object_t *p_this )
95 decoder_fifo_t *p_fifo = (decoder_fifo_t*) p_this;
97 if( p_fifo->i_fourcc != VLC_FOURCC('t','h','e','o') )
102 p_fifo->pf_run = RunDecoder;
105 /*****************************************************************************
106 * RunDecoder: the theora decoder
107 *****************************************************************************/
108 static int RunDecoder( decoder_fifo_t *p_fifo )
111 ogg_packet oggpacket;
112 int i_chroma, i_aspect;
115 /* Allocate the memory needed to store the thread's structure */
116 if( (p_dec = (dec_thread_t *)malloc (sizeof(dec_thread_t)) )
119 msg_Err( p_fifo, "out of memory" );
123 /* Initialize the thread properties */
124 memset( p_dec, 0, sizeof(dec_thread_t) );
125 p_dec->p_fifo = p_fifo;
128 /* Take care of the initial Theora header */
129 if( GetOggPacket( p_dec, &oggpacket, &i_pts ) != VLC_SUCCESS )
132 oggpacket.b_o_s = 1; /* yes this actually is a b_o_s packet :) */
133 if( theora_decode_header( &p_dec->ti, &oggpacket ) < 0 )
135 msg_Err( p_dec->p_fifo, "This bitstream does not contain Theora "
140 /* Initialize decoder */
141 theora_decode_init( &p_dec->td, &p_dec->ti );
142 msg_Dbg( p_dec->p_fifo, "%dx%d %.02f fps video",
143 p_dec->ti.width, p_dec->ti.height,
144 (double)p_dec->ti.fps_numerator/p_dec->ti.fps_denominator);
146 /* Initialize video output */
147 if( p_dec->ti.aspect_denominator )
148 i_aspect = VOUT_ASPECT_FACTOR * p_dec->ti.aspect_numerator /
149 p_dec->ti.aspect_denominator;
151 i_aspect = VOUT_ASPECT_FACTOR * p_dec->ti.width / p_dec->ti.height;
153 i_chroma = VLC_FOURCC('Y','V','1','2');
155 p_dec->p_vout = theora_SpawnVout( p_dec, p_dec->ti.width, p_dec->ti.height,
156 i_aspect, i_chroma );
158 /* theora decoder thread's main loop */
159 while( (!p_dec->p_fifo->b_die) && (!p_dec->p_fifo->b_error) )
161 DecodePacket( p_dec );
164 /* If b_error is set, the theora decoder thread enters the error loop */
165 if( p_dec->p_fifo->b_error )
167 DecoderError( p_dec->p_fifo );
170 /* End of the theora decoder thread */
171 CloseDecoder( p_dec );
176 DecoderError( p_fifo );
180 p_dec->p_fifo->b_error = 1;
182 /* End of the theora decoder thread */
183 CloseDecoder( p_dec );
189 /*****************************************************************************
190 * DecodePacket: decodes a Theora packet.
191 *****************************************************************************/
192 static void DecodePacket( dec_thread_t *p_dec )
194 ogg_packet oggpacket;
199 if( GetOggPacket( p_dec, &oggpacket, &i_pts ) != VLC_SUCCESS )
201 /* This should mean an eos */
205 theora_decode_packetin( &p_dec->td, &oggpacket );
208 theora_decode_YUVout( &p_dec->td, &yuv );
210 /* Get a new picture */
211 while( !(p_pic = vout_CreatePicture( p_dec->p_vout, 0, 0, 0 ) ) )
213 if( p_dec->p_fifo->b_die || p_dec->p_fifo->b_error )
217 msleep( VOUT_OUTMEM_SLEEP );
222 theora_CopyPicture( p_dec, p_pic, &yuv );
224 vout_DatePicture( p_dec->p_vout, p_pic, i_pts );
225 vout_DisplayPicture( p_dec->p_vout, p_pic );
228 /*****************************************************************************
229 * GetOggPacket: get the following theora packet from the stream and send back
230 * the result in an ogg packet (for easy decoding by libtheora).
231 *****************************************************************************
232 * Returns VLC_EGENERIC in case of eof.
233 *****************************************************************************/
234 static int GetOggPacket( dec_thread_t *p_dec, ogg_packet *p_oggpacket,
237 if( p_dec->p_pes ) input_DeletePES( p_dec->p_fifo->p_packets_mgt,
240 input_ExtractPES( p_dec->p_fifo, &p_dec->p_pes );
241 if( !p_dec->p_pes ) return VLC_EGENERIC;
243 p_oggpacket->packet = p_dec->p_pes->p_first->p_payload_start;
244 p_oggpacket->bytes = p_dec->p_pes->i_pes_size;
245 p_oggpacket->granulepos = p_dec->p_pes->i_dts;
246 p_oggpacket->b_o_s = 0;
247 p_oggpacket->e_o_s = 0;
248 p_oggpacket->packetno = 0;
250 *p_pts = p_dec->p_pes->i_pts;
255 /*****************************************************************************
256 * CloseDecoder: theora decoder destruction
257 *****************************************************************************/
258 static void CloseDecoder( dec_thread_t * p_dec )
264 input_DeletePES( p_dec->p_fifo->p_packets_mgt, p_dec->p_pes );
268 vlc_object_detach( p_dec->p_vout );
269 vout_DestroyThread( p_dec->p_vout );
276 /*****************************************************************************
277 * theora_SpawnVout: creates a new video output
278 *****************************************************************************/
279 static vout_thread_t *theora_SpawnVout( dec_thread_t *p_dec,
285 vout_thread_t *p_vout;
287 if( !i_width || !i_height )
293 /* Spawn a video output if there is none. First we look for our children,
294 * then we look for any other vout that might be available. */
295 p_vout = vlc_object_find( p_dec->p_fifo, VLC_OBJECT_VOUT,
299 p_vout = vlc_object_find( p_dec->p_fifo, VLC_OBJECT_VOUT,
305 if( p_vout->render.i_width != i_width
306 || p_vout->render.i_height != i_height
307 || p_vout->render.i_chroma != i_chroma
308 || p_vout->render.i_aspect != i_aspect )
310 /* We are not interested in this format, close this vout */
311 vlc_object_detach( p_vout );
312 vlc_object_release( p_vout );
313 vout_DestroyThread( p_vout );
318 /* This video output is cool! Hijack it. */
319 vlc_object_detach( p_vout );
320 vlc_object_attach( p_vout, p_dec->p_fifo );
321 vlc_object_release( p_vout );
327 msg_Dbg( p_dec->p_fifo, "no vout present, spawning one" );
329 p_vout = vout_CreateThread( p_dec->p_fifo,
331 i_chroma, i_aspect );
337 /*****************************************************************************
338 * theora_CopyPicture: copy a picture from theora internal buffers to a
339 * picture_t structure.
340 *****************************************************************************/
341 static void theora_CopyPicture( dec_thread_t *p_dec, picture_t *p_pic,
344 int i_plane, i_line, i_width, i_dst_stride, i_src_stride;
347 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
349 p_dst = p_pic->p[i_plane].p_pixels;
350 p_src = i_plane ? (i_plane - 1 ? yuv->v : yuv->u ) : yuv->y;
351 i_width = p_pic->p[i_plane].i_visible_pitch;
352 i_dst_stride = p_pic->p[i_plane].i_pitch;
353 i_src_stride = i_plane ? yuv->uv_stride : yuv->y_stride;
355 for( i_line = 0; i_line < p_pic->p[i_plane].i_lines; i_line++ )
357 p_dec->p_fifo->p_vlc->pf_memcpy( p_dst, p_src, i_width );
358 p_src += i_src_stride;
359 p_dst += i_dst_stride;