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 $
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_comment tc; /* theora comment header */
54 theora_state td; /* theora bitstream user comments */
59 decoder_fifo_t *p_fifo; /* stores the PES stream data */
60 pes_packet_t *p_pes; /* current PES we are decoding */
65 vout_thread_t *p_vout;
69 /*****************************************************************************
71 *****************************************************************************/
72 static int OpenDecoder ( vlc_object_t * );
73 static int RunDecoder ( decoder_fifo_t * );
74 static void CloseDecoder ( dec_thread_t * );
76 static void DecodePacket ( dec_thread_t * );
77 static int GetOggPacket ( dec_thread_t *, ogg_packet *, mtime_t * );
79 static void theora_CopyPicture( dec_thread_t *, picture_t *, yuv_buffer * );
81 /*****************************************************************************
83 *****************************************************************************/
85 set_description( _("Theora video decoder") );
86 set_capability( "decoder", 100 );
87 set_callbacks( OpenDecoder, NULL );
88 add_shortcut( "theora" );
91 /*****************************************************************************
92 * OpenDecoder: probe the decoder and return score
93 *****************************************************************************/
94 static int OpenDecoder( vlc_object_t *p_this )
96 decoder_fifo_t *p_fifo = (decoder_fifo_t*) p_this;
98 if( p_fifo->i_fourcc != VLC_FOURCC('t','h','e','o') )
103 p_fifo->pf_run = RunDecoder;
106 /*****************************************************************************
107 * RunDecoder: the theora decoder
108 *****************************************************************************/
109 static int RunDecoder( decoder_fifo_t *p_fifo )
112 ogg_packet oggpacket;
113 int i_chroma, i_aspect;
116 /* Allocate the memory needed to store the thread's structure */
117 if( (p_dec = (dec_thread_t *)malloc (sizeof(dec_thread_t)) )
120 msg_Err( p_fifo, "out of memory" );
124 /* Initialize the thread properties */
125 memset( p_dec, 0, sizeof(dec_thread_t) );
126 p_dec->p_fifo = p_fifo;
128 p_dec->p_vout = NULL;
130 /* Init supporting Theora structures needed in header parsing */
131 theora_comment_init( &p_dec->tc );
132 theora_info_init( &p_dec->ti );
134 /* Take care of the initial Theora header */
135 if( GetOggPacket( p_dec, &oggpacket, &i_pts ) != VLC_SUCCESS )
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 )
141 msg_Err( p_dec->p_fifo, "This bitstream does not contain Theora "
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 )
152 if( theora_decode_header( &p_dec->ti, &p_dec->tc, &oggpacket ) < 0 )
154 msg_Err( p_dec->p_fifo, "2nd Theora header is corrupted" );
158 if( GetOggPacket( p_dec, &oggpacket, &i_pts ) != VLC_SUCCESS )
161 if( theora_decode_header( &p_dec->ti, &p_dec->tc, &oggpacket ) < 0 )
163 msg_Err( p_dec->p_fifo, "3rd Theora header is corrupted" );
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 );
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;
181 i_aspect = VOUT_ASPECT_FACTOR * p_dec->ti.width / p_dec->ti.height;
183 i_chroma = VLC_FOURCC('Y','V','1','2');
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 );
189 /* theora decoder thread's main loop */
190 while( (!p_dec->p_fifo->b_die) && (!p_dec->p_fifo->b_error) )
192 DecodePacket( p_dec );
195 /* If b_error is set, the theora decoder thread enters the error loop */
196 if( p_dec->p_fifo->b_error )
198 DecoderError( p_dec->p_fifo );
201 /* End of the theora decoder thread */
202 CloseDecoder( p_dec );
207 DecoderError( p_fifo );
211 p_dec->p_fifo->b_error = 1;
213 /* End of the theora decoder thread */
214 CloseDecoder( p_dec );
220 /*****************************************************************************
221 * DecodePacket: decodes a Theora packet.
222 *****************************************************************************/
223 static void DecodePacket( dec_thread_t *p_dec )
225 ogg_packet oggpacket;
230 if( GetOggPacket( p_dec, &oggpacket, &i_pts ) != VLC_SUCCESS )
232 /* This should mean an eos */
236 theora_decode_packetin( &p_dec->td, &oggpacket );
239 theora_decode_YUVout( &p_dec->td, &yuv );
241 /* Get a new picture */
242 while( !(p_pic = vout_CreatePicture( p_dec->p_vout, 0, 0, 0 ) ) )
244 if( p_dec->p_fifo->b_die || p_dec->p_fifo->b_error )
248 msleep( VOUT_OUTMEM_SLEEP );
253 theora_CopyPicture( p_dec, p_pic, &yuv );
255 vout_DatePicture( p_dec->p_vout, p_pic, i_pts );
256 vout_DisplayPicture( p_dec->p_vout, p_pic );
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,
268 if( p_dec->p_pes ) input_DeletePES( p_dec->p_fifo->p_packets_mgt,
271 input_ExtractPES( p_dec->p_fifo, &p_dec->p_pes );
272 if( !p_dec->p_pes ) return VLC_EGENERIC;
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;
281 *p_pts = p_dec->p_pes->i_pts;
286 /*****************************************************************************
287 * CloseDecoder: theora decoder destruction
288 *****************************************************************************/
289 static void CloseDecoder( dec_thread_t * p_dec )
295 input_DeletePES( p_dec->p_fifo->p_packets_mgt, p_dec->p_pes );
297 vout_Request( p_dec->p_fifo, p_dec->p_vout, 0, 0, 0, 0 );
299 theora_info_clear( &p_dec->ti );
300 theora_comment_clear( &p_dec->tc );
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,
313 int i_plane, i_line, i_width, i_dst_stride, i_src_stride;
316 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
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;
324 for( i_line = 0; i_line < p_pic->p[i_plane].i_lines; i_line++ )
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;