]> git.sesse.net Git - vlc/blob - modules/codec/theora.c
* modules/codec/theora.c: oops, introduced a bug in pts calculation.
[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.7 2003/09/02 22:36:55 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 <stdlib.h>                                      /* malloc(), free() */
28 #include <string.h>                                    /* memcpy(), memset() */
29
30 #include <vlc/vlc.h>
31 #include <vlc/vout.h>
32 #include <vlc/decoder.h>
33 #include <vlc/input.h>
34 #include <vlc/sout.h>
35 #include <input_ext-dec.h>
36
37 #include <ogg/ogg.h>
38
39 #include <theora/theora.h>
40
41 /*****************************************************************************
42  * decoder_sys_t : theora decoder descriptor
43  *****************************************************************************/
44 struct decoder_sys_t
45 {
46     /* Module mode */
47     vlc_bool_t b_packetizer;
48
49     /*
50      * Input properties
51      */
52     int i_headers;
53
54     /*
55      * Theora properties
56      */
57     theora_info      ti;                        /* theora bitstream settings */
58     theora_comment   tc;                            /* theora comment header */
59     theora_state     td;                   /* theora bitstream user comments */
60
61     /*
62      * Output properties
63      */
64     vout_thread_t *p_vout;
65
66     /*
67      * Packetizer output properties
68      */
69     sout_packetizer_input_t *p_sout_input;
70     sout_format_t           sout_format;
71
72     /*
73      * Common properties
74      */
75     mtime_t i_pts;
76 };
77
78 /*****************************************************************************
79  * Local prototypes
80  *****************************************************************************/
81 static int OpenDecoder   ( vlc_object_t * );
82 static int OpenPacketizer( vlc_object_t * );
83
84 static int InitDecoder   ( decoder_t * );
85 static int RunDecoder    ( decoder_t *, block_t * );
86 static int EndDecoder    ( decoder_t * );
87
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 * );
91
92 static void ParseTheoraComments( decoder_t * );
93
94 static void theora_CopyPicture( decoder_t *, picture_t *, yuv_buffer * );
95
96 /*****************************************************************************
97  * Module descriptor
98  *****************************************************************************/
99 vlc_module_begin();
100     set_description( _("Theora video decoder") );
101     set_capability( "decoder", 100 );
102     set_callbacks( OpenDecoder, NULL );
103     add_shortcut( "theora" );
104
105     add_submodule();
106     set_description( _("Theora video packetizer") );
107     set_capability( "packetizer", 100 );
108     set_callbacks( OpenPacketizer, NULL );
109     add_shortcut( "theora" );
110 vlc_module_end();
111
112 /*****************************************************************************
113  * OpenDecoder: probe the decoder and return score
114  *****************************************************************************/
115 static int OpenDecoder( vlc_object_t *p_this )
116 {
117     decoder_t *p_dec = (decoder_t*)p_this;
118
119     if( p_dec->p_fifo->i_fourcc != VLC_FOURCC('t','h','e','o') )
120     {
121         return VLC_EGENERIC;
122     }
123
124     p_dec->pf_init = InitDecoder;
125     p_dec->pf_decode = RunDecoder;
126     p_dec->pf_end = EndDecoder;
127
128     /* Allocate the memory needed to store the decoder's structure */
129     if( ( p_dec->p_sys =
130           (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
131     {
132         msg_Err( p_dec, "out of memory" );
133         return VLC_EGENERIC;
134     }
135     p_dec->p_sys->b_packetizer = VLC_FALSE;
136
137     return VLC_SUCCESS;
138 }
139
140 static int OpenPacketizer( vlc_object_t *p_this )
141 {
142     decoder_t *p_dec = (decoder_t*)p_this;
143
144     int i_ret = OpenDecoder( p_this );
145
146     if( i_ret == VLC_SUCCESS ) p_dec->p_sys->b_packetizer = VLC_TRUE;
147
148     return i_ret;
149 }
150
151 /*****************************************************************************
152  * InitDecoder: Initalize the decoder
153  *****************************************************************************/
154 static int InitDecoder( decoder_t *p_dec )
155 {
156     decoder_sys_t *p_sys = p_dec->p_sys;
157
158     p_sys->i_pts = 0;
159
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;
168
169     /* Init supporting Theora structures needed in header parsing */
170     theora_comment_init( &p_sys->tc );
171     theora_info_init( &p_sys->ti );
172
173     p_sys->i_headers = 0;
174
175     return VLC_SUCCESS;
176 }
177
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 )
184 {
185     decoder_sys_t *p_sys = p_dec->p_sys;
186     ogg_packet oggpacket;
187     int i_ret;
188
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;
193     oggpacket.b_o_s = 0;
194     oggpacket.e_o_s = 0;
195     oggpacket.packetno = 0;
196
197     if( p_sys->i_headers == 0 )
198     {
199         /* Take care of the initial Theora header */
200
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 )
203         {
204             msg_Err( p_dec->p_fifo, "This bitstream does not contain Theora "
205                      "video data" );
206             block_Release( p_block );
207             return VLC_EGENERIC;
208         }
209         p_sys->i_headers++;
210
211  
212         if( p_sys->b_packetizer )
213         {
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;
217
218             p_sys->p_sout_input =
219                 sout_InputNew( p_dec, &p_sys->sout_format );
220
221             if( !p_sys->p_sout_input )
222             {
223                 msg_Err( p_dec, "cannot add a new stream" );
224                 block_Release( p_block );
225                 return VLC_EGENERIC;
226             }
227         }
228         else
229         {
230             /* Initialize video output */
231             int i_chroma, i_aspect;
232
233             if( p_sys->ti.aspect_denominator )
234                 i_aspect = VOUT_ASPECT_FACTOR * p_sys->ti.aspect_numerator /
235                     p_sys->ti.aspect_denominator;
236             else
237                 i_aspect = VOUT_ASPECT_FACTOR *
238                     p_sys->ti.frame_width / p_sys->ti.frame_height;
239
240             i_chroma = VLC_FOURCC('Y','V','1','2');
241
242             p_sys->p_vout =
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 )
247             {
248                 msg_Err( p_dec, "failed to create video output" );
249                 block_Release( p_block );
250                 return VLC_EGENERIC;
251             }
252         }
253
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 );
260
261         if( p_sys->b_packetizer )
262         {
263             i_ret = SendPacket( p_dec, &oggpacket );
264             block_Release( p_block );
265             return i_ret;
266         }
267         else
268         {
269             block_Release( p_block );
270             return VLC_SUCCESS;
271         }
272     }
273
274     if( p_sys->i_headers == 1 )
275     {
276         /* The next packet in order is the comments header */
277         if( theora_decode_header( &p_sys->ti, &p_sys->tc, &oggpacket ) < 0 )
278         {
279             msg_Err( p_dec, "2nd Theora header is corrupted" );
280             return VLC_EGENERIC;
281         }
282         p_sys->i_headers++;
283     
284         ParseTheoraComments( p_dec );
285
286         if( p_sys->b_packetizer )
287         {
288             i_ret = SendPacket( p_dec, &oggpacket );
289             block_Release( p_block );
290             return i_ret;
291         }
292         else
293         {
294             block_Release( p_block );
295             return VLC_SUCCESS;
296         }
297     }
298
299     if( p_sys->i_headers == 2 )
300     {
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 )
305         {
306             msg_Err( p_dec, "3rd Theora header is corrupted" );
307             return VLC_EGENERIC;
308         }
309         p_sys->i_headers++;
310     
311         if( !p_sys->b_packetizer )
312         {
313             /* We have all the headers, initialize decoder */
314             theora_decode_init( &p_sys->td, &p_sys->ti );
315         }
316
317         if( p_sys->b_packetizer )
318         {
319             i_ret = SendPacket( p_dec, &oggpacket );
320             block_Release( p_block );
321             return i_ret;
322         }
323         else
324         {
325             block_Release( p_block );
326             return VLC_SUCCESS;
327         }
328     }
329
330     i_ret = ProcessPacket( p_dec, &oggpacket, p_block->i_pts );
331     block_Release( p_block );
332     return i_ret;
333 }
334
335 /*****************************************************************************
336  * ProcessPacket: processes a Vorbis packet.
337  *****************************************************************************/
338 static int ProcessPacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
339                           mtime_t i_pts )
340 {
341     decoder_sys_t *p_sys = p_dec->p_sys;
342
343     /* Date management */
344     if( i_pts > 0 && i_pts != p_sys->i_pts )
345     {
346         p_sys->i_pts = i_pts;
347     }
348
349     if( p_sys->b_packetizer )
350     {
351         return SendPacket( p_dec, p_oggpacket );
352     }
353     else
354     {
355         return DecodePacket( p_dec, p_oggpacket );
356     }
357 }
358
359 /*****************************************************************************
360  * DecodePacket: decodes a Theora packet.
361  *****************************************************************************/
362 static int DecodePacket( decoder_t *p_dec, ogg_packet *p_oggpacket )
363 {
364     picture_t *p_pic;
365     yuv_buffer yuv;
366
367     decoder_sys_t *p_sys = p_dec->p_sys;
368
369     theora_decode_packetin( &p_sys->td, p_oggpacket );
370
371     /* Decode */
372     theora_decode_YUVout( &p_sys->td, &yuv );
373
374     /* Get a new picture */
375     while( !(p_pic = vout_CreatePicture( p_sys->p_vout, 0, 0, 0 ) ) )
376     {
377         if( p_dec->p_fifo->b_die || p_dec->p_fifo->b_error )
378         {
379             return VLC_EGENERIC;
380         }
381         msleep( VOUT_OUTMEM_SLEEP );
382     }
383     if( !p_pic ) return VLC_EGENERIC;
384
385     theora_CopyPicture( p_dec, p_pic, &yuv );
386
387     vout_DatePicture( p_sys->p_vout, p_pic, p_sys->i_pts );
388     vout_DisplayPicture( p_sys->p_vout, p_pic );
389
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 */
393
394     return VLC_SUCCESS;
395 }
396
397 /*****************************************************************************
398  * SendPacket: send an ogg packet to the stream output.
399  *****************************************************************************/
400 static int SendPacket( decoder_t *p_dec, ogg_packet *p_oggpacket )
401 {
402     decoder_sys_t *p_sys = p_dec->p_sys;
403
404     sout_buffer_t *p_sout_buffer =
405         sout_BufferNew( p_sys->p_sout_input->p_sout, p_oggpacket->bytes );
406
407     if( !p_sout_buffer ) return VLC_EGENERIC;
408
409     p_dec->p_vlc->pf_memcpy( p_sout_buffer->p_buffer,
410                              p_oggpacket->packet,
411                              p_oggpacket->bytes );
412
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 */
417
418     if( p_sys->i_headers >= 3 )
419         p_sout_buffer->i_length = p_sys->i_pts - p_sout_buffer->i_pts;
420     else
421         p_sout_buffer->i_length = 0;
422
423     sout_InputSendBuffer( p_sys->p_sout_input, p_sout_buffer );
424
425     return VLC_SUCCESS;
426 }
427
428 /*****************************************************************************
429  * ParseTheoraComments: FIXME should be done in demuxer
430  *****************************************************************************/
431 static void ParseTheoraComments( decoder_t *p_dec )
432 {
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") );
436     int i = 0;
437     char *psz_name, *psz_value, *psz_comment;
438     while ( i < p_dec->p_sys->tc.comments )
439     {
440         psz_comment = strdup( p_dec->p_sys->tc.user_comments[i] );
441         if( !psz_comment )
442         {
443             msg_Warn( p_dec, "Out of memory" );
444             break;
445         }
446         psz_name = psz_comment;
447         psz_value = strchr( psz_comment, '=' );
448         if( psz_value )
449         {
450             *psz_value = '\0';
451             psz_value++;
452             input_AddInfo( p_cat, psz_name, psz_value );
453         }
454         free( psz_comment );
455         i++;
456     }
457 }
458
459 /*****************************************************************************
460  * EndDecoder: theora decoder destruction
461  *****************************************************************************/
462 static int EndDecoder( decoder_t *p_dec )
463 {
464     decoder_sys_t *p_sys = p_dec->p_sys;
465
466     if( !p_sys->b_packetizer )
467         vout_Request( p_dec, p_sys->p_vout, 0, 0, 0, 0 );
468
469     theora_info_clear( &p_sys->ti );
470     theora_comment_clear( &p_sys->tc );
471
472     free( p_sys );
473
474     return VLC_SUCCESS;
475 }
476
477 /*****************************************************************************
478  * theora_CopyPicture: copy a picture from theora internal buffers to a
479  *                     picture_t structure.
480  *****************************************************************************/
481 static void theora_CopyPicture( decoder_t *p_dec, picture_t *p_pic,
482                                 yuv_buffer *yuv )
483 {
484     int i_plane, i_line, i_width, i_dst_stride, i_src_stride;
485     int i_src_xoffset, i_src_yoffset;
486     u8  *p_dst, *p_src;
487
488     for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
489     {
490         p_dst = p_pic->p[i_plane].p_pixels;
491         p_src = i_plane ? (i_plane - 1 ? yuv->v : yuv->u ) : yuv->y;
492         i_width = p_pic->p[i_plane].i_visible_pitch;
493         i_dst_stride  = p_pic->p[i_plane].i_pitch;
494         i_src_stride  = i_plane ? yuv->uv_stride : yuv->y_stride;
495         i_src_xoffset = p_dec->p_sys->ti.offset_x;
496         i_src_yoffset = p_dec->p_sys->ti.offset_y;
497         if( i_plane )
498         {
499             i_src_xoffset /= 2;
500             i_src_yoffset /= 2;
501         } 
502
503         p_src += (i_src_yoffset * i_src_stride + i_src_yoffset);
504
505         for( i_line = 0; i_line < p_pic->p[i_plane].i_lines; i_line++ )
506         {
507             p_dec->p_vlc->pf_memcpy( p_dst, p_src, i_width );
508             p_src += i_src_stride;
509             p_dst += i_dst_stride;
510         }
511     }
512 }