1 /*****************************************************************************
2 * theora.c: theora decoder module making use of libtheora.
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VideoLAN
5 * $Id: theora.c,v 1.8 2003/09/28 21:54:20 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 *****************************************************************************/
27 #include <stdlib.h> /* malloc(), free() */
28 #include <string.h> /* memcpy(), memset() */
32 #include <vlc/decoder.h>
33 #include <vlc/input.h>
35 #include <input_ext-dec.h>
39 #include <theora/theora.h>
41 /*****************************************************************************
42 * decoder_sys_t : theora decoder descriptor
43 *****************************************************************************/
47 vlc_bool_t b_packetizer;
57 theora_info ti; /* theora bitstream settings */
58 theora_comment tc; /* theora comment header */
59 theora_state td; /* theora bitstream user comments */
64 vout_thread_t *p_vout;
67 * Packetizer output properties
69 sout_packetizer_input_t *p_sout_input;
70 sout_format_t sout_format;
78 /*****************************************************************************
80 *****************************************************************************/
81 static int OpenDecoder ( vlc_object_t * );
82 static int OpenPacketizer( vlc_object_t * );
84 static int InitDecoder ( decoder_t * );
85 static int RunDecoder ( decoder_t *, block_t * );
86 static int EndDecoder ( decoder_t * );
88 static int ProcessPacket ( decoder_t *, ogg_packet *, mtime_t );
89 static int DecodePacket ( decoder_t *, ogg_packet * );
90 static int SendPacket ( decoder_t *, ogg_packet * );
92 static void ParseTheoraComments( decoder_t * );
94 static void theora_CopyPicture( decoder_t *, picture_t *, yuv_buffer * );
96 /*****************************************************************************
98 *****************************************************************************/
100 set_description( _("Theora video decoder") );
101 set_capability( "decoder", 100 );
102 set_callbacks( OpenDecoder, NULL );
103 add_shortcut( "theora" );
106 set_description( _("Theora video packetizer") );
107 set_capability( "packetizer", 100 );
108 set_callbacks( OpenPacketizer, NULL );
109 add_shortcut( "theora" );
112 /*****************************************************************************
113 * OpenDecoder: probe the decoder and return score
114 *****************************************************************************/
115 static int OpenDecoder( vlc_object_t *p_this )
117 decoder_t *p_dec = (decoder_t*)p_this;
119 if( p_dec->p_fifo->i_fourcc != VLC_FOURCC('t','h','e','o') )
124 p_dec->pf_init = InitDecoder;
125 p_dec->pf_decode = RunDecoder;
126 p_dec->pf_end = EndDecoder;
128 /* Allocate the memory needed to store the decoder's structure */
130 (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
132 msg_Err( p_dec, "out of memory" );
135 p_dec->p_sys->b_packetizer = VLC_FALSE;
140 static int OpenPacketizer( vlc_object_t *p_this )
142 decoder_t *p_dec = (decoder_t*)p_this;
144 int i_ret = OpenDecoder( p_this );
146 if( i_ret == VLC_SUCCESS ) p_dec->p_sys->b_packetizer = VLC_TRUE;
151 /*****************************************************************************
152 * InitDecoder: Initalize the decoder
153 *****************************************************************************/
154 static int InitDecoder( decoder_t *p_dec )
156 decoder_sys_t *p_sys = p_dec->p_sys;
160 p_sys->p_sout_input = NULL;
161 p_sys->sout_format.i_cat = VIDEO_ES;
162 p_sys->sout_format.i_fourcc = VLC_FOURCC( 't', 'h', 'e', 'o' );
163 p_sys->sout_format.i_width = 0;
164 p_sys->sout_format.i_height = 0;
165 p_sys->sout_format.i_bitrate = 0;
166 p_sys->sout_format.i_extra_data = 0;
167 p_sys->sout_format.p_extra_data = NULL;
169 /* Init supporting Theora structures needed in header parsing */
170 theora_comment_init( &p_sys->tc );
171 theora_info_init( &p_sys->ti );
173 p_sys->i_headers = 0;
178 /****************************************************************************
179 * RunDecoder: the whole thing
180 ****************************************************************************
181 * This function must be fed with ogg packets.
182 ****************************************************************************/
183 static int RunDecoder( decoder_t *p_dec, block_t *p_block )
185 decoder_sys_t *p_sys = p_dec->p_sys;
186 ogg_packet oggpacket;
189 /* Block to Ogg packet */
190 oggpacket.packet = p_block->p_buffer;
191 oggpacket.bytes = p_block->i_buffer;
192 oggpacket.granulepos = p_block->i_dts;
195 oggpacket.packetno = 0;
197 if( p_sys->i_headers == 0 )
199 /* Take care of the initial Theora header */
201 oggpacket.b_o_s = 1; /* yes this actually is a b_o_s packet :) */
202 if( theora_decode_header( &p_sys->ti, &p_sys->tc, &oggpacket ) < 0 )
204 msg_Err( p_dec->p_fifo, "This bitstream does not contain Theora "
206 block_Release( p_block );
212 if( p_sys->b_packetizer )
214 /* add a input for the stream ouput */
215 p_sys->sout_format.i_width = p_sys->ti.width;
216 p_sys->sout_format.i_height = p_sys->ti.height;
218 p_sys->p_sout_input =
219 sout_InputNew( p_dec, &p_sys->sout_format );
221 if( !p_sys->p_sout_input )
223 msg_Err( p_dec, "cannot add a new stream" );
224 block_Release( p_block );
230 /* Initialize video output */
231 int i_chroma, i_aspect;
233 if( p_sys->ti.aspect_denominator )
234 i_aspect = VOUT_ASPECT_FACTOR * p_sys->ti.aspect_numerator /
235 p_sys->ti.aspect_denominator;
237 i_aspect = VOUT_ASPECT_FACTOR *
238 p_sys->ti.frame_width / p_sys->ti.frame_height;
240 i_chroma = VLC_FOURCC('Y','V','1','2');
243 vout_Request( p_dec, NULL,
244 p_sys->ti.frame_width, p_sys->ti.frame_height,
245 i_chroma, i_aspect );
246 if( p_sys->p_vout == NULL )
248 msg_Err( p_dec, "failed to create video output" );
249 block_Release( p_block );
254 msg_Dbg( p_dec, "%dx%d %.02f fps video, frame content "
255 "is %dx%d with offset (%d,%d)",
256 p_sys->ti.width, p_sys->ti.height,
257 (double)p_sys->ti.fps_numerator/p_sys->ti.fps_denominator,
258 p_sys->ti.frame_width, p_sys->ti.frame_height,
259 p_sys->ti.offset_x, p_sys->ti.offset_y );
261 if( p_sys->b_packetizer )
263 i_ret = SendPacket( p_dec, &oggpacket );
264 block_Release( p_block );
269 block_Release( p_block );
274 if( p_sys->i_headers == 1 )
276 /* The next packet in order is the comments header */
277 if( theora_decode_header( &p_sys->ti, &p_sys->tc, &oggpacket ) < 0 )
279 msg_Err( p_dec, "2nd Theora header is corrupted" );
284 ParseTheoraComments( p_dec );
286 if( p_sys->b_packetizer )
288 i_ret = SendPacket( p_dec, &oggpacket );
289 block_Release( p_block );
294 block_Release( p_block );
299 if( p_sys->i_headers == 2 )
301 /* The next packet in order is the codebooks header
302 We need to watch out that this packet is not missing as a
303 missing or corrupted header is fatal. */
304 if( theora_decode_header( &p_sys->ti, &p_sys->tc, &oggpacket ) < 0 )
306 msg_Err( p_dec, "3rd Theora header is corrupted" );
311 if( !p_sys->b_packetizer )
313 /* We have all the headers, initialize decoder */
314 theora_decode_init( &p_sys->td, &p_sys->ti );
317 if( p_sys->b_packetizer )
319 i_ret = SendPacket( p_dec, &oggpacket );
320 block_Release( p_block );
325 block_Release( p_block );
330 i_ret = ProcessPacket( p_dec, &oggpacket, p_block->i_pts );
331 block_Release( p_block );
335 /*****************************************************************************
336 * ProcessPacket: processes a Vorbis packet.
337 *****************************************************************************/
338 static int ProcessPacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
341 decoder_sys_t *p_sys = p_dec->p_sys;
343 /* Date management */
344 if( i_pts > 0 && i_pts != p_sys->i_pts )
346 p_sys->i_pts = i_pts;
349 if( p_sys->b_packetizer )
351 return SendPacket( p_dec, p_oggpacket );
355 return DecodePacket( p_dec, p_oggpacket );
359 /*****************************************************************************
360 * DecodePacket: decodes a Theora packet.
361 *****************************************************************************/
362 static int DecodePacket( decoder_t *p_dec, ogg_packet *p_oggpacket )
367 decoder_sys_t *p_sys = p_dec->p_sys;
369 theora_decode_packetin( &p_sys->td, p_oggpacket );
372 theora_decode_YUVout( &p_sys->td, &yuv );
374 /* Get a new picture */
375 while( !(p_pic = vout_CreatePicture( p_sys->p_vout, 0, 0, 0 ) ) )
377 if( p_dec->p_fifo->b_die || p_dec->p_fifo->b_error )
381 msleep( VOUT_OUTMEM_SLEEP );
383 if( !p_pic ) return VLC_EGENERIC;
385 theora_CopyPicture( p_dec, p_pic, &yuv );
387 vout_DatePicture( p_sys->p_vout, p_pic, p_sys->i_pts );
388 vout_DisplayPicture( p_sys->p_vout, p_pic );
390 /* Date management */
391 p_sys->i_pts += ( I64C(1000000) * p_sys->ti.fps_denominator /
392 p_sys->ti.fps_numerator ); /* 1 frame per packet */
397 /*****************************************************************************
398 * SendPacket: send an ogg packet to the stream output.
399 *****************************************************************************/
400 static int SendPacket( decoder_t *p_dec, ogg_packet *p_oggpacket )
402 decoder_sys_t *p_sys = p_dec->p_sys;
404 sout_buffer_t *p_sout_buffer =
405 sout_BufferNew( p_sys->p_sout_input->p_sout, p_oggpacket->bytes );
407 if( !p_sout_buffer ) return VLC_EGENERIC;
409 p_dec->p_vlc->pf_memcpy( p_sout_buffer->p_buffer,
411 p_oggpacket->bytes );
413 /* Date management */
414 p_sout_buffer->i_dts = p_sout_buffer->i_pts = p_sys->i_pts;
415 p_sys->i_pts += ( I64C(1000000) * p_sys->ti.fps_denominator /
416 p_sys->ti.fps_numerator ); /* 1 frame per packet */
418 if( p_sys->i_headers >= 3 )
419 p_sout_buffer->i_length = p_sys->i_pts - p_sout_buffer->i_pts;
421 p_sout_buffer->i_length = 0;
423 sout_InputSendBuffer( p_sys->p_sout_input, p_sout_buffer );
428 /*****************************************************************************
429 * ParseTheoraComments: FIXME should be done in demuxer
430 *****************************************************************************/
431 static void ParseTheoraComments( decoder_t *p_dec )
433 input_thread_t *p_input = (input_thread_t *)p_dec->p_parent;
434 input_info_category_t *p_cat =
435 input_InfoCategory( p_input, _("Theora Comment") );
437 char *psz_name, *psz_value, *psz_comment;
438 while ( i < p_dec->p_sys->tc.comments )
440 psz_comment = strdup( p_dec->p_sys->tc.user_comments[i] );
443 msg_Warn( p_dec, "Out of memory" );
446 psz_name = psz_comment;
447 psz_value = strchr( psz_comment, '=' );
452 input_AddInfo( p_cat, psz_name, psz_value );
459 /*****************************************************************************
460 * EndDecoder: theora decoder destruction
461 *****************************************************************************/
462 static int EndDecoder( decoder_t *p_dec )
464 decoder_sys_t *p_sys = p_dec->p_sys;
466 if( !p_sys->b_packetizer )
467 vout_Request( p_dec, p_sys->p_vout, 0, 0, 0, 0 );
469 if( p_sys->p_sout_input != NULL )
471 sout_InputDelete( p_sys->p_sout_input );
474 theora_info_clear( &p_sys->ti );
475 theora_comment_clear( &p_sys->tc );
482 /*****************************************************************************
483 * theora_CopyPicture: copy a picture from theora internal buffers to a
484 * picture_t structure.
485 *****************************************************************************/
486 static void theora_CopyPicture( decoder_t *p_dec, picture_t *p_pic,
489 int i_plane, i_line, i_width, i_dst_stride, i_src_stride;
490 int i_src_xoffset, i_src_yoffset;
493 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
495 p_dst = p_pic->p[i_plane].p_pixels;
496 p_src = i_plane ? (i_plane - 1 ? yuv->v : yuv->u ) : yuv->y;
497 i_width = p_pic->p[i_plane].i_visible_pitch;
498 i_dst_stride = p_pic->p[i_plane].i_pitch;
499 i_src_stride = i_plane ? yuv->uv_stride : yuv->y_stride;
500 i_src_xoffset = p_dec->p_sys->ti.offset_x;
501 i_src_yoffset = p_dec->p_sys->ti.offset_y;
508 p_src += (i_src_yoffset * i_src_stride + i_src_yoffset);
510 for( i_line = 0; i_line < p_pic->p[i_plane].i_lines; i_line++ )
512 p_dec->p_vlc->pf_memcpy( p_dst, p_src, i_width );
513 p_src += i_src_stride;
514 p_dst += i_dst_stride;