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 $
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 * );
80 /*****************************************************************************
82 *****************************************************************************/
84 set_description( _("Theora video decoder") );
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;
127 p_dec->p_vout = NULL;
129 /* Take care of the initial Theora header */
130 if( GetOggPacket( p_dec, &oggpacket, &i_pts ) != VLC_SUCCESS )
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 )
136 msg_Err( p_dec->p_fifo, "This bitstream does not contain Theora "
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);
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;
152 i_aspect = VOUT_ASPECT_FACTOR * p_dec->ti.width / p_dec->ti.height;
154 i_chroma = VLC_FOURCC('Y','V','1','2');
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 );
160 /* theora decoder thread's main loop */
161 while( (!p_dec->p_fifo->b_die) && (!p_dec->p_fifo->b_error) )
163 DecodePacket( p_dec );
166 /* If b_error is set, the theora decoder thread enters the error loop */
167 if( p_dec->p_fifo->b_error )
169 DecoderError( p_dec->p_fifo );
172 /* End of the theora decoder thread */
173 CloseDecoder( p_dec );
178 DecoderError( p_fifo );
182 p_dec->p_fifo->b_error = 1;
184 /* End of the theora decoder thread */
185 CloseDecoder( p_dec );
191 /*****************************************************************************
192 * DecodePacket: decodes a Theora packet.
193 *****************************************************************************/
194 static void DecodePacket( dec_thread_t *p_dec )
196 ogg_packet oggpacket;
201 if( GetOggPacket( p_dec, &oggpacket, &i_pts ) != VLC_SUCCESS )
203 /* This should mean an eos */
207 theora_decode_packetin( &p_dec->td, &oggpacket );
210 theora_decode_YUVout( &p_dec->td, &yuv );
212 /* Get a new picture */
213 while( !(p_pic = vout_CreatePicture( p_dec->p_vout, 0, 0, 0 ) ) )
215 if( p_dec->p_fifo->b_die || p_dec->p_fifo->b_error )
219 msleep( VOUT_OUTMEM_SLEEP );
224 theora_CopyPicture( p_dec, p_pic, &yuv );
226 vout_DatePicture( p_dec->p_vout, p_pic, i_pts );
227 vout_DisplayPicture( p_dec->p_vout, p_pic );
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,
239 if( p_dec->p_pes ) input_DeletePES( p_dec->p_fifo->p_packets_mgt,
242 input_ExtractPES( p_dec->p_fifo, &p_dec->p_pes );
243 if( !p_dec->p_pes ) return VLC_EGENERIC;
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;
252 *p_pts = p_dec->p_pes->i_pts;
257 /*****************************************************************************
258 * CloseDecoder: theora decoder destruction
259 *****************************************************************************/
260 static void CloseDecoder( dec_thread_t * p_dec )
266 input_DeletePES( p_dec->p_fifo->p_packets_mgt, p_dec->p_pes );
268 vout_Request( p_dec->p_fifo, p_dec->p_vout, 0, 0, 0, 0 );
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,
281 int i_plane, i_line, i_width, i_dst_stride, i_src_stride;
284 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
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;
292 for( i_line = 0; i_line < p_pic->p[i_plane].i_lines; i_line++ )
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;