]> git.sesse.net Git - vlc/blob - modules/codec/theora.c
Do not check file size coherency on multi-parts rar.
[vlc] / modules / codec / theora.c
1 /*****************************************************************************
2  * theora.c: theora decoder module making use of libtheora.
3  *****************************************************************************
4  * Copyright (C) 1999-2001 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Gildas Bazin <gbazin@videolan.org>
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_codec.h>
34 #include <vlc_sout.h>
35 #include <vlc_input.h>
36 #include "../demux/xiph.h"
37
38 #include <ogg/ogg.h>
39
40 #include <theora/theora.h>
41
42 /*****************************************************************************
43  * decoder_sys_t : theora decoder descriptor
44  *****************************************************************************/
45 struct decoder_sys_t
46 {
47     /* Module mode */
48     bool b_packetizer;
49
50     /*
51      * Input properties
52      */
53     bool b_has_headers;
54
55     /*
56      * Theora properties
57      */
58     theora_info      ti;                        /* theora bitstream settings */
59     theora_comment   tc;                            /* theora comment header */
60     theora_state     td;                   /* theora bitstream user comments */
61
62     /*
63      * Decoding properties
64      */
65     bool b_decoded_first_keyframe;
66
67     /*
68      * Common properties
69      */
70     mtime_t i_pts;
71 };
72
73 /*****************************************************************************
74  * Local prototypes
75  *****************************************************************************/
76 static int  OpenDecoder   ( vlc_object_t * );
77 static int  OpenPacketizer( vlc_object_t * );
78 static void CloseDecoder  ( vlc_object_t * );
79
80 static void *DecodeBlock  ( decoder_t *, block_t ** );
81 static int  ProcessHeaders( decoder_t * );
82 static void *ProcessPacket ( decoder_t *, ogg_packet *, block_t ** );
83
84 static picture_t *DecodePacket( decoder_t *, ogg_packet * );
85
86 static void ParseTheoraComments( decoder_t * );
87 static void theora_CopyPicture( picture_t *, yuv_buffer * );
88
89 static int  OpenEncoder( vlc_object_t *p_this );
90 static void CloseEncoder( vlc_object_t *p_this );
91 static block_t *Encode( encoder_t *p_enc, picture_t *p_pict );
92
93 /*****************************************************************************
94  * Module descriptor
95  *****************************************************************************/
96 #define ENC_QUALITY_TEXT N_("Encoding quality")
97 #define ENC_QUALITY_LONGTEXT N_( \
98   "Enforce a quality between 1 (low) and 10 (high), instead " \
99   "of specifying a particular bitrate. This will produce a VBR stream." )
100
101 vlc_module_begin ()
102     set_category( CAT_INPUT )
103     set_subcategory( SUBCAT_INPUT_VCODEC )
104     set_shortname( "Theora" )
105     set_description( N_("Theora video decoder") )
106     set_capability( "decoder", 100 )
107     set_callbacks( OpenDecoder, CloseDecoder )
108     add_shortcut( "theora" )
109
110     add_submodule ()
111     set_description( N_("Theora video packetizer") )
112     set_capability( "packetizer", 100 )
113     set_callbacks( OpenPacketizer, CloseDecoder )
114     add_shortcut( "theora" )
115
116     add_submodule ()
117     set_description( N_("Theora video encoder") )
118     set_capability( "encoder", 150 )
119     set_callbacks( OpenEncoder, CloseEncoder )
120     add_shortcut( "theora" )
121
122 #   define ENC_CFG_PREFIX "sout-theora-"
123     add_integer( ENC_CFG_PREFIX "quality", 2, ENC_QUALITY_TEXT,
124                  ENC_QUALITY_LONGTEXT, false )
125 vlc_module_end ()
126
127 static const char *const ppsz_enc_options[] = {
128     "quality", NULL
129 };
130
131 /*****************************************************************************
132  * OpenDecoder: probe the decoder and return score
133  *****************************************************************************/
134 static int OpenDecoder( vlc_object_t *p_this )
135 {
136     decoder_t *p_dec = (decoder_t*)p_this;
137     decoder_sys_t *p_sys;
138
139     if( p_dec->fmt_in.i_codec != VLC_CODEC_THEORA )
140     {
141         return VLC_EGENERIC;
142     }
143
144     /* Allocate the memory needed to store the decoder's structure */
145     if( ( p_dec->p_sys = p_sys = malloc(sizeof(*p_sys)) ) == NULL )
146         return VLC_ENOMEM;
147     p_dec->p_sys->b_packetizer = false;
148     p_sys->b_has_headers = false;
149     p_sys->i_pts = VLC_TS_INVALID;
150     p_sys->b_decoded_first_keyframe = false;
151
152     /* Set output properties */
153     p_dec->fmt_out.i_cat = VIDEO_ES;
154     p_dec->fmt_out.i_codec = VLC_CODEC_I420;
155
156     /* Set callbacks */
157     p_dec->pf_decode_video = (picture_t *(*)(decoder_t *, block_t **))
158         DecodeBlock;
159     p_dec->pf_packetize    = (block_t *(*)(decoder_t *, block_t **))
160         DecodeBlock;
161
162     /* Init supporting Theora structures needed in header parsing */
163     theora_comment_init( &p_sys->tc );
164     theora_info_init( &p_sys->ti );
165
166     return VLC_SUCCESS;
167 }
168
169 static int OpenPacketizer( vlc_object_t *p_this )
170 {
171     decoder_t *p_dec = (decoder_t*)p_this;
172
173     int i_ret = OpenDecoder( p_this );
174
175     if( i_ret == VLC_SUCCESS )
176     {
177         p_dec->p_sys->b_packetizer = true;
178         p_dec->fmt_out.i_codec = VLC_CODEC_THEORA;
179     }
180
181     return i_ret;
182 }
183
184 /****************************************************************************
185  * DecodeBlock: the whole thing
186  ****************************************************************************
187  * This function must be fed with ogg packets.
188  ****************************************************************************/
189 static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
190 {
191     decoder_sys_t *p_sys = p_dec->p_sys;
192     block_t *p_block;
193     ogg_packet oggpacket;
194
195     if( !pp_block || !*pp_block ) return NULL;
196
197     p_block = *pp_block;
198
199     /* Block to Ogg packet */
200     oggpacket.packet = p_block->p_buffer;
201     oggpacket.bytes = p_block->i_buffer;
202     oggpacket.granulepos = p_block->i_dts;
203     oggpacket.b_o_s = 0;
204     oggpacket.e_o_s = 0;
205     oggpacket.packetno = 0;
206
207     /* Check for headers */
208     if( !p_sys->b_has_headers )
209     {
210         if( ProcessHeaders( p_dec ) )
211         {
212             block_Release( *pp_block );
213             return NULL;
214         }
215         p_sys->b_has_headers = true;
216     }
217
218     return ProcessPacket( p_dec, &oggpacket, pp_block );
219 }
220
221 /*****************************************************************************
222  * ProcessHeaders: process Theora headers.
223  *****************************************************************************/
224 static int ProcessHeaders( decoder_t *p_dec )
225 {
226     decoder_sys_t *p_sys = p_dec->p_sys;
227     ogg_packet oggpacket;
228
229     unsigned pi_size[XIPH_MAX_HEADER_COUNT];
230     void     *pp_data[XIPH_MAX_HEADER_COUNT];
231     unsigned i_count;
232     if( xiph_SplitHeaders( pi_size, pp_data, &i_count,
233                            p_dec->fmt_in.i_extra, p_dec->fmt_in.p_extra) )
234         return VLC_EGENERIC;
235     if( i_count < 3 )
236         goto error;
237
238     oggpacket.granulepos = -1;
239     oggpacket.e_o_s = 0;
240     oggpacket.packetno = 0;
241
242     /* Take care of the initial Vorbis header */
243     oggpacket.b_o_s  = 1; /* yes this actually is a b_o_s packet :) */
244     oggpacket.bytes  = pi_size[0];
245     oggpacket.packet = pp_data[0];
246     if( theora_decode_header( &p_sys->ti, &p_sys->tc, &oggpacket ) < 0 )
247     {
248         msg_Err( p_dec, "this bitstream does not contain Theora video data" );
249         goto error;
250     }
251
252     /* Set output properties */
253     if( !p_sys->b_packetizer )
254     switch( p_sys->ti.pixelformat )
255     {
256       case OC_PF_420:
257         p_dec->fmt_out.i_codec = VLC_CODEC_I420;
258         break;
259       case OC_PF_422:
260         p_dec->fmt_out.i_codec = VLC_CODEC_I422;
261         break;
262       case OC_PF_444:
263         p_dec->fmt_out.i_codec = VLC_CODEC_I444;
264         break;
265       case OC_PF_RSVD:
266       default:
267         msg_Err( p_dec, "unknown chroma in theora sample" );
268         break;
269     }
270     p_dec->fmt_out.video.i_width = p_sys->ti.width;
271     p_dec->fmt_out.video.i_height = p_sys->ti.height;
272     if( p_sys->ti.frame_width && p_sys->ti.frame_height )
273     {
274         p_dec->fmt_out.video.i_visible_width = p_sys->ti.frame_width;
275         p_dec->fmt_out.video.i_visible_height = p_sys->ti.frame_height;
276         if( p_sys->ti.offset_x || p_sys->ti.offset_y )
277         {
278             p_dec->fmt_out.video.i_x_offset = p_sys->ti.offset_x;
279             p_dec->fmt_out.video.i_y_offset = p_sys->ti.offset_y;
280         }
281     }
282
283     if( p_sys->ti.aspect_denominator && p_sys->ti.aspect_numerator )
284     {
285         p_dec->fmt_out.video.i_sar_num = p_sys->ti.aspect_numerator;
286         p_dec->fmt_out.video.i_sar_den = p_sys->ti.aspect_denominator;
287     }
288     else
289     {
290         p_dec->fmt_out.video.i_sar_num = 1;
291         p_dec->fmt_out.video.i_sar_den = 1;
292     }
293
294     if( p_sys->ti.fps_numerator > 0 && p_sys->ti.fps_denominator > 0 )
295     {
296         p_dec->fmt_out.video.i_frame_rate = p_sys->ti.fps_numerator;
297         p_dec->fmt_out.video.i_frame_rate_base = p_sys->ti.fps_denominator;
298     }
299
300     msg_Dbg( p_dec, "%dx%d %.02f fps video, frame content "
301              "is %dx%d with offset (%d,%d)",
302              p_sys->ti.width, p_sys->ti.height,
303              (double)p_sys->ti.fps_numerator/p_sys->ti.fps_denominator,
304              p_sys->ti.frame_width, p_sys->ti.frame_height,
305              p_sys->ti.offset_x, p_sys->ti.offset_y );
306
307     /* Sanity check that seems necessary for some corrupted files */
308     if( p_sys->ti.width < p_sys->ti.frame_width ||
309         p_sys->ti.height < p_sys->ti.frame_height )
310     {
311         msg_Warn( p_dec, "trying to correct invalid theora header "
312                   "(frame size (%dx%d) is smaller than frame content (%d,%d))",
313                   p_sys->ti.width, p_sys->ti.height,
314                   p_sys->ti.frame_width, p_sys->ti.frame_height );
315
316         if( p_sys->ti.width < p_sys->ti.frame_width )
317             p_sys->ti.width = p_sys->ti.frame_width;
318         if( p_sys->ti.height < p_sys->ti.frame_height )
319             p_sys->ti.height = p_sys->ti.frame_height;
320     }
321
322     /* The next packet in order is the comments header */
323     oggpacket.b_o_s  = 0;
324     oggpacket.bytes  = pi_size[1];
325     oggpacket.packet = pp_data[1];
326     if( theora_decode_header( &p_sys->ti, &p_sys->tc, &oggpacket ) < 0 )
327     {
328         msg_Err( p_dec, "2nd Theora header is corrupted" );
329         goto error;
330     }
331
332     ParseTheoraComments( p_dec );
333
334     /* The next packet in order is the codebooks header
335      * We need to watch out that this packet is not missing as a
336      * missing or corrupted header is fatal. */
337     oggpacket.b_o_s  = 0;
338     oggpacket.bytes  = pi_size[2];
339     oggpacket.packet = pp_data[2];
340     if( theora_decode_header( &p_sys->ti, &p_sys->tc, &oggpacket ) < 0 )
341     {
342         msg_Err( p_dec, "3rd Theora header is corrupted" );
343         goto error;
344     }
345
346     if( !p_sys->b_packetizer )
347     {
348         /* We have all the headers, initialize decoder */
349         theora_decode_init( &p_sys->td, &p_sys->ti );
350     }
351     else
352     {
353         p_dec->fmt_out.i_extra = p_dec->fmt_in.i_extra;
354         p_dec->fmt_out.p_extra = xrealloc( p_dec->fmt_out.p_extra,
355                                                   p_dec->fmt_out.i_extra );
356         memcpy( p_dec->fmt_out.p_extra,
357                 p_dec->fmt_in.p_extra, p_dec->fmt_out.i_extra );
358     }
359
360     for( unsigned i = 0; i < i_count; i++ )
361         free( pp_data[i] );
362     return VLC_SUCCESS;
363
364 error:
365     for( unsigned i = 0; i < i_count; i++ )
366         free( pp_data[i] );
367     return VLC_EGENERIC;
368 }
369
370 /*****************************************************************************
371  * ProcessPacket: processes a theora packet.
372  *****************************************************************************/
373 static void *ProcessPacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
374                             block_t **pp_block )
375 {
376     decoder_sys_t *p_sys = p_dec->p_sys;
377     block_t *p_block = *pp_block;
378     void *p_buf;
379
380     if( ( p_block->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) ) != 0 )
381     {
382         /* Don't send the the first packet after a discontinuity to
383          * theora_decode, otherwise we get purple/green display artifacts
384          * appearing in the video output */
385         return NULL;
386     }
387
388     /* Date management */
389     if( p_block->i_pts > VLC_TS_INVALID && p_block->i_pts != p_sys->i_pts )
390     {
391         p_sys->i_pts = p_block->i_pts;
392     }
393
394     *pp_block = NULL; /* To avoid being fed the same packet again */
395
396     if( p_sys->b_packetizer )
397     {
398         /* Date management */
399         p_block->i_dts = p_block->i_pts = p_sys->i_pts;
400
401         p_block->i_length = p_sys->i_pts - p_block->i_pts;
402
403         p_buf = p_block;
404     }
405     else
406     {
407         p_buf = DecodePacket( p_dec, p_oggpacket );
408         if( p_block )
409             block_Release( p_block );
410     }
411
412     /* Date management */
413     p_sys->i_pts += ( INT64_C(1000000) * p_sys->ti.fps_denominator /
414                       p_sys->ti.fps_numerator ); /* 1 frame per packet */
415
416     return p_buf;
417 }
418
419 /*****************************************************************************
420  * DecodePacket: decodes a Theora packet.
421  *****************************************************************************/
422 static picture_t *DecodePacket( decoder_t *p_dec, ogg_packet *p_oggpacket )
423 {
424     decoder_sys_t *p_sys = p_dec->p_sys;
425     picture_t *p_pic;
426     yuv_buffer yuv;
427
428     theora_decode_packetin( &p_sys->td, p_oggpacket );
429
430     /* Check for keyframe */
431     if( !(p_oggpacket->packet[0] & 0x80) /* data packet */ &&
432         !(p_oggpacket->packet[0] & 0x40) /* intra frame */ )
433         p_sys->b_decoded_first_keyframe = true;
434
435     /* If we haven't seen a single keyframe yet, don't let Theora decode
436      * anything, otherwise we'll get display artifacts.  (This is impossible
437      * in the general case, but can happen if e.g. we play a network stream
438      * using a timed URL, such that the server doesn't start the video with a
439      * keyframe). */
440     if( p_sys->b_decoded_first_keyframe )
441         theora_decode_YUVout( &p_sys->td, &yuv );
442     else
443         return NULL;
444
445     /* Get a new picture */
446     p_pic = decoder_NewPicture( p_dec );
447     if( !p_pic ) return NULL;
448
449     theora_CopyPicture( p_pic, &yuv );
450
451     p_pic->date = p_sys->i_pts;
452
453     return p_pic;
454 }
455
456 /*****************************************************************************
457  * ParseTheoraComments:
458  *****************************************************************************/
459 static void ParseTheoraComments( decoder_t *p_dec )
460 {
461     char *psz_name, *psz_value, *psz_comment;
462     int i = 0;
463
464     while ( i < p_dec->p_sys->tc.comments )
465     {
466         psz_comment = strdup( p_dec->p_sys->tc.user_comments[i] );
467         if( !psz_comment )
468             break;
469         psz_name = psz_comment;
470         psz_value = strchr( psz_comment, '=' );
471         if( psz_value )
472         {
473             *psz_value = '\0';
474             psz_value++;
475
476             if( !p_dec->p_description )
477                 p_dec->p_description = vlc_meta_New();
478             if( p_dec->p_description )
479                 vlc_meta_AddExtra( p_dec->p_description, psz_name, psz_value );
480         }
481         free( psz_comment );
482         i++;
483     }
484 }
485
486 /*****************************************************************************
487  * CloseDecoder: theora decoder destruction
488  *****************************************************************************/
489 static void CloseDecoder( vlc_object_t *p_this )
490 {
491     decoder_t *p_dec = (decoder_t *)p_this;
492     decoder_sys_t *p_sys = p_dec->p_sys;
493
494     theora_info_clear( &p_sys->ti );
495     theora_comment_clear( &p_sys->tc );
496
497     free( p_sys );
498 }
499
500 /*****************************************************************************
501  * theora_CopyPicture: copy a picture from theora internal buffers to a
502  *                     picture_t structure.
503  *****************************************************************************/
504 static void theora_CopyPicture( picture_t *p_pic,
505                                 yuv_buffer *yuv )
506 {
507     int i_plane, i_line, i_dst_stride, i_src_stride;
508     uint8_t *p_dst, *p_src;
509
510     for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
511     {
512         p_dst = p_pic->p[i_plane].p_pixels;
513         p_src = i_plane ? (i_plane - 1 ? yuv->v : yuv->u ) : yuv->y;
514         i_dst_stride  = p_pic->p[i_plane].i_pitch;
515         i_src_stride  = i_plane ? yuv->uv_stride : yuv->y_stride;
516
517         for( i_line = 0; i_line < p_pic->p[i_plane].i_lines; i_line++ )
518         {
519             vlc_memcpy( p_dst, p_src,
520                         i_plane ? yuv->uv_width : yuv->y_width );
521             p_src += i_src_stride;
522             p_dst += i_dst_stride;
523         }
524     }
525 }
526
527 /*****************************************************************************
528  * encoder_sys_t : theora encoder descriptor
529  *****************************************************************************/
530 struct encoder_sys_t
531 {
532     /*
533      * Input properties
534      */
535     bool b_headers;
536
537     /*
538      * Theora properties
539      */
540     theora_info      ti;                        /* theora bitstream settings */
541     theora_comment   tc;                            /* theora comment header */
542     theora_state     td;                   /* theora bitstream user comments */
543
544     int i_width, i_height;
545 };
546
547 /*****************************************************************************
548  * OpenEncoder: probe the encoder and return score
549  *****************************************************************************/
550 static int OpenEncoder( vlc_object_t *p_this )
551 {
552     encoder_t *p_enc = (encoder_t *)p_this;
553     encoder_sys_t *p_sys;
554     int i_quality;
555
556     if( p_enc->fmt_out.i_codec != VLC_CODEC_THEORA &&
557         !p_enc->b_force )
558     {
559         return VLC_EGENERIC;
560     }
561
562     /* Allocate the memory needed to store the decoder's structure */
563     if( ( p_sys = malloc(sizeof(encoder_sys_t)) ) == NULL )
564         return VLC_ENOMEM;
565     p_enc->p_sys = p_sys;
566
567     p_enc->pf_encode_video = Encode;
568     p_enc->fmt_in.i_codec = VLC_CODEC_I420;
569     p_enc->fmt_out.i_codec = VLC_CODEC_THEORA;
570
571     config_ChainParse( p_enc, ENC_CFG_PREFIX, ppsz_enc_options, p_enc->p_cfg );
572
573     i_quality = var_GetInteger( p_enc, ENC_CFG_PREFIX "quality" );
574     if( i_quality > 10 ) i_quality = 10;
575     if( i_quality < 0 ) i_quality = 0;
576
577     theora_info_init( &p_sys->ti );
578
579     p_sys->ti.width = p_enc->fmt_in.video.i_width;
580     p_sys->ti.height = p_enc->fmt_in.video.i_height;
581
582     if( p_sys->ti.width % 16 || p_sys->ti.height % 16 )
583     {
584         /* Pictures from the transcoder should always have a pitch
585          * which is a multiple of 16 */
586         p_sys->ti.width = (p_sys->ti.width + 15) >> 4 << 4;
587         p_sys->ti.height = (p_sys->ti.height + 15) >> 4 << 4;
588
589         msg_Dbg( p_enc, "padding video from %dx%d to %dx%d",
590                  p_enc->fmt_in.video.i_width, p_enc->fmt_in.video.i_height,
591                  p_sys->ti.width, p_sys->ti.height );
592     }
593
594     p_sys->ti.frame_width = p_enc->fmt_in.video.i_width;
595     p_sys->ti.frame_height = p_enc->fmt_in.video.i_height;
596     p_sys->ti.offset_x = 0 /*frame_x_offset*/;
597     p_sys->ti.offset_y = 0 /*frame_y_offset*/;
598
599     p_sys->i_width = p_sys->ti.width;
600     p_sys->i_height = p_sys->ti.height;
601
602     if( !p_enc->fmt_in.video.i_frame_rate ||
603         !p_enc->fmt_in.video.i_frame_rate_base )
604     {
605         p_sys->ti.fps_numerator = 25;
606         p_sys->ti.fps_denominator = 1;
607     }
608     else
609     {
610         p_sys->ti.fps_numerator = p_enc->fmt_in.video.i_frame_rate;
611         p_sys->ti.fps_denominator = p_enc->fmt_in.video.i_frame_rate_base;
612     }
613
614     if( p_enc->fmt_in.video.i_sar_num > 0 && p_enc->fmt_in.video.i_sar_den > 0 )
615     {
616         unsigned i_dst_num, i_dst_den;
617         vlc_ureduce( &i_dst_num, &i_dst_den,
618                      p_enc->fmt_in.video.i_sar_num,
619                      p_enc->fmt_in.video.i_sar_den, 0 );
620         p_sys->ti.aspect_numerator = i_dst_num;
621         p_sys->ti.aspect_denominator = i_dst_den;
622     }
623     else
624     {
625         p_sys->ti.aspect_numerator = 4;
626         p_sys->ti.aspect_denominator = 3;
627     }
628
629     p_sys->ti.target_bitrate = p_enc->fmt_out.i_bitrate;
630     p_sys->ti.quality = ((float)i_quality) * 6.3;
631
632     p_sys->ti.dropframes_p = 0;
633     p_sys->ti.quick_p = 1;
634     p_sys->ti.keyframe_auto_p = 1;
635     p_sys->ti.keyframe_frequency = 64;
636     p_sys->ti.keyframe_frequency_force = 64;
637     p_sys->ti.keyframe_data_target_bitrate = p_enc->fmt_out.i_bitrate * 1.5;
638     p_sys->ti.keyframe_auto_threshold = 80;
639     p_sys->ti.keyframe_mindistance = 8;
640     p_sys->ti.noise_sensitivity = 1;
641
642     theora_encode_init( &p_sys->td, &p_sys->ti );
643     theora_comment_init( &p_sys->tc );
644
645     /* Create and store headers */
646     for( int i = 0; i < 3; i++ )
647     {
648         ogg_packet header;
649
650         if( i == 0 )
651             theora_encode_header( &p_sys->td, &header );
652         else if( i == 1 )
653             theora_encode_comment( &p_sys->tc, &header );
654         else
655             theora_encode_tables( &p_sys->td, &header );
656
657         if( xiph_AppendHeaders( &p_enc->fmt_out.i_extra, &p_enc->fmt_out.p_extra,
658                                 header.bytes, header.packet ) )
659         {
660             p_enc->fmt_out.i_extra = 0;
661             p_enc->fmt_out.p_extra = NULL;
662         }
663     }
664     return VLC_SUCCESS;
665 }
666
667 /****************************************************************************
668  * Encode: the whole thing
669  ****************************************************************************
670  * This function spits out ogg packets.
671  ****************************************************************************/
672 static block_t *Encode( encoder_t *p_enc, picture_t *p_pict )
673 {
674     encoder_sys_t *p_sys = p_enc->p_sys;
675     ogg_packet oggpacket;
676     block_t *p_block;
677     yuv_buffer yuv;
678     int i;
679
680     if( !p_pict ) return NULL;
681     /* Sanity check */
682     if( p_pict->p[0].i_pitch < (int)p_sys->i_width ||
683         p_pict->p[0].i_lines < (int)p_sys->i_height )
684     {
685         msg_Warn( p_enc, "frame is smaller than encoding size"
686                   "(%ix%i->%ix%i) -> dropping frame",
687                   p_pict->p[0].i_pitch, p_pict->p[0].i_lines,
688                   p_sys->i_width, p_sys->i_height );
689         return NULL;
690     }
691
692     /* Fill padding */
693     if( p_pict->p[0].i_visible_pitch < (int)p_sys->i_width )
694     {
695         for( i = 0; i < p_sys->i_height; i++ )
696         {
697             memset( p_pict->p[0].p_pixels + i * p_pict->p[0].i_pitch +
698                     p_pict->p[0].i_visible_pitch,
699                     *( p_pict->p[0].p_pixels + i * p_pict->p[0].i_pitch +
700                        p_pict->p[0].i_visible_pitch - 1 ),
701                     p_sys->i_width - p_pict->p[0].i_visible_pitch );
702         }
703         for( i = 0; i < p_sys->i_height / 2; i++ )
704         {
705             memset( p_pict->p[1].p_pixels + i * p_pict->p[1].i_pitch +
706                     p_pict->p[1].i_visible_pitch,
707                     *( p_pict->p[1].p_pixels + i * p_pict->p[1].i_pitch +
708                        p_pict->p[1].i_visible_pitch - 1 ),
709                     p_sys->i_width / 2 - p_pict->p[1].i_visible_pitch );
710             memset( p_pict->p[2].p_pixels + i * p_pict->p[2].i_pitch +
711                     p_pict->p[2].i_visible_pitch,
712                     *( p_pict->p[2].p_pixels + i * p_pict->p[2].i_pitch +
713                        p_pict->p[2].i_visible_pitch - 1 ),
714                     p_sys->i_width / 2 - p_pict->p[2].i_visible_pitch );
715         }
716     }
717
718     if( p_pict->p[0].i_visible_lines < (int)p_sys->i_height )
719     {
720         for( i = p_pict->p[0].i_visible_lines; i < p_sys->i_height; i++ )
721         {
722             memset( p_pict->p[0].p_pixels + i * p_pict->p[0].i_pitch, 0,
723                     p_sys->i_width );
724         }
725         for( i = p_pict->p[1].i_visible_lines; i < p_sys->i_height / 2; i++ )
726         {
727             memset( p_pict->p[1].p_pixels + i * p_pict->p[1].i_pitch, 0x80,
728                     p_sys->i_width / 2 );
729             memset( p_pict->p[2].p_pixels + i * p_pict->p[2].i_pitch, 0x80,
730                     p_sys->i_width / 2 );
731         }
732     }
733
734     /* Theora is a one-frame-in, one-frame-out system. Submit a frame
735      * for compression and pull out the packet. */
736
737     yuv.y_width  = p_sys->i_width;
738     yuv.y_height = p_sys->i_height;
739     yuv.y_stride = p_pict->p[0].i_pitch;
740
741     yuv.uv_width  = p_sys->i_width / 2;
742     yuv.uv_height = p_sys->i_height / 2;
743     yuv.uv_stride = p_pict->p[1].i_pitch;
744
745     yuv.y = p_pict->p[0].p_pixels;
746     yuv.u = p_pict->p[1].p_pixels;
747     yuv.v = p_pict->p[2].p_pixels;
748
749     if( theora_encode_YUVin( &p_sys->td, &yuv ) < 0 )
750     {
751         msg_Warn( p_enc, "failed encoding a frame" );
752         return NULL;
753     }
754
755     theora_encode_packetout( &p_sys->td, 0, &oggpacket );
756
757     /* Ogg packet to block */
758     p_block = block_New( p_enc, oggpacket.bytes );
759     memcpy( p_block->p_buffer, oggpacket.packet, oggpacket.bytes );
760     p_block->i_dts = p_block->i_pts = p_pict->date;
761
762     if( theora_packet_iskeyframe( &oggpacket ) )
763     {
764         p_block->i_flags |= BLOCK_FLAG_TYPE_I;
765     }
766
767     return p_block;
768 }
769
770 /*****************************************************************************
771  * CloseEncoder: theora encoder destruction
772  *****************************************************************************/
773 static void CloseEncoder( vlc_object_t *p_this )
774 {
775     encoder_t *p_enc = (encoder_t *)p_this;
776     encoder_sys_t *p_sys = p_enc->p_sys;
777
778     theora_info_clear( &p_sys->ti );
779     theora_comment_clear( &p_sys->tc );
780
781     free( p_sys );
782 }