]> git.sesse.net Git - vlc/blob - modules/codec/theora.c
Use var_InheritString for --decklink-video-connection.
[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, NULL, 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     switch( p_sys->ti.pixelformat )
254     {
255       case OC_PF_420:
256         p_dec->fmt_out.i_codec = VLC_CODEC_I420;
257         break;
258       case OC_PF_422:
259         p_dec->fmt_out.i_codec = VLC_CODEC_I422;
260         break;
261       case OC_PF_444:
262         p_dec->fmt_out.i_codec = VLC_CODEC_I444;
263         break;
264       case OC_PF_RSVD:
265       default:
266         msg_Err( p_dec, "unknown chroma in theora sample" );
267         break;
268     }
269     p_dec->fmt_out.video.i_width = p_sys->ti.width;
270     p_dec->fmt_out.video.i_height = p_sys->ti.height;
271     if( p_sys->ti.frame_width && p_sys->ti.frame_height )
272     {
273         p_dec->fmt_out.video.i_visible_width = p_sys->ti.frame_width;
274         p_dec->fmt_out.video.i_visible_height = p_sys->ti.frame_height;
275         if( p_sys->ti.offset_x || p_sys->ti.offset_y )
276         {
277             p_dec->fmt_out.video.i_x_offset = p_sys->ti.offset_x;
278             p_dec->fmt_out.video.i_y_offset = p_sys->ti.offset_y;
279         }
280     }
281
282     if( p_sys->ti.aspect_denominator && p_sys->ti.aspect_numerator )
283     {
284         p_dec->fmt_out.video.i_sar_num = p_sys->ti.aspect_numerator;
285         p_dec->fmt_out.video.i_sar_den = p_sys->ti.aspect_denominator;
286     }
287     else
288     {
289         p_dec->fmt_out.video.i_sar_num = 1;
290         p_dec->fmt_out.video.i_sar_den = 1;
291     }
292
293     if( p_sys->ti.fps_numerator > 0 && p_sys->ti.fps_denominator > 0 )
294     {
295         p_dec->fmt_out.video.i_frame_rate = p_sys->ti.fps_numerator;
296         p_dec->fmt_out.video.i_frame_rate_base = p_sys->ti.fps_denominator;
297     }
298
299     msg_Dbg( p_dec, "%dx%d %.02f fps video, frame content "
300              "is %dx%d with offset (%d,%d)",
301              p_sys->ti.width, p_sys->ti.height,
302              (double)p_sys->ti.fps_numerator/p_sys->ti.fps_denominator,
303              p_sys->ti.frame_width, p_sys->ti.frame_height,
304              p_sys->ti.offset_x, p_sys->ti.offset_y );
305
306     /* Sanity check that seems necessary for some corrupted files */
307     if( p_sys->ti.width < p_sys->ti.frame_width ||
308         p_sys->ti.height < p_sys->ti.frame_height )
309     {
310         msg_Warn( p_dec, "trying to correct invalid theora header "
311                   "(frame size (%dx%d) is smaller than frame content (%d,%d))",
312                   p_sys->ti.width, p_sys->ti.height,
313                   p_sys->ti.frame_width, p_sys->ti.frame_height );
314
315         if( p_sys->ti.width < p_sys->ti.frame_width )
316             p_sys->ti.width = p_sys->ti.frame_width;
317         if( p_sys->ti.height < p_sys->ti.frame_height )
318             p_sys->ti.height = p_sys->ti.frame_height;
319     }
320
321     /* The next packet in order is the comments header */
322     oggpacket.b_o_s  = 0;
323     oggpacket.bytes  = pi_size[1];
324     oggpacket.packet = pp_data[1];
325     if( theora_decode_header( &p_sys->ti, &p_sys->tc, &oggpacket ) < 0 )
326     {
327         msg_Err( p_dec, "2nd Theora header is corrupted" );
328         goto error;
329     }
330
331     ParseTheoraComments( p_dec );
332
333     /* The next packet in order is the codebooks header
334      * We need to watch out that this packet is not missing as a
335      * missing or corrupted header is fatal. */
336     oggpacket.b_o_s  = 0;
337     oggpacket.bytes  = pi_size[2];
338     oggpacket.packet = pp_data[2];
339     if( theora_decode_header( &p_sys->ti, &p_sys->tc, &oggpacket ) < 0 )
340     {
341         msg_Err( p_dec, "3rd Theora header is corrupted" );
342         goto error;
343     }
344
345     if( !p_sys->b_packetizer )
346     {
347         /* We have all the headers, initialize decoder */
348         theora_decode_init( &p_sys->td, &p_sys->ti );
349     }
350     else
351     {
352         p_dec->fmt_out.i_extra = p_dec->fmt_in.i_extra;
353         p_dec->fmt_out.p_extra = xrealloc( p_dec->fmt_out.p_extra,
354                                                   p_dec->fmt_out.i_extra );
355         memcpy( p_dec->fmt_out.p_extra,
356                 p_dec->fmt_in.p_extra, p_dec->fmt_out.i_extra );
357     }
358
359     for( unsigned i = 0; i < i_count; i++ )
360         free( pp_data[i] );
361     return VLC_SUCCESS;
362
363 error:
364     for( unsigned i = 0; i < i_count; i++ )
365         free( pp_data[i] );
366     return VLC_EGENERIC;
367 }
368
369 /*****************************************************************************
370  * ProcessPacket: processes a theora packet.
371  *****************************************************************************/
372 static void *ProcessPacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
373                             block_t **pp_block )
374 {
375     decoder_sys_t *p_sys = p_dec->p_sys;
376     block_t *p_block = *pp_block;
377     void *p_buf;
378
379     if( ( p_block->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) ) != 0 )
380     {
381         /* Don't send the the first packet after a discontinuity to
382          * theora_decode, otherwise we get purple/green display artifacts
383          * appearing in the video output */
384         return NULL;
385     }
386
387     /* Date management */
388     if( p_block->i_pts > VLC_TS_INVALID && p_block->i_pts != p_sys->i_pts )
389     {
390         p_sys->i_pts = p_block->i_pts;
391     }
392
393     *pp_block = NULL; /* To avoid being fed the same packet again */
394
395     if( p_sys->b_packetizer )
396     {
397         /* Date management */
398         p_block->i_dts = p_block->i_pts = p_sys->i_pts;
399
400         p_block->i_length = p_sys->i_pts - p_block->i_pts;
401
402         p_buf = p_block;
403     }
404     else
405     {
406         p_buf = DecodePacket( p_dec, p_oggpacket );
407         if( p_block )
408             block_Release( p_block );
409     }
410
411     /* Date management */
412     p_sys->i_pts += ( INT64_C(1000000) * p_sys->ti.fps_denominator /
413                       p_sys->ti.fps_numerator ); /* 1 frame per packet */
414
415     return p_buf;
416 }
417
418 /*****************************************************************************
419  * DecodePacket: decodes a Theora packet.
420  *****************************************************************************/
421 static picture_t *DecodePacket( decoder_t *p_dec, ogg_packet *p_oggpacket )
422 {
423     decoder_sys_t *p_sys = p_dec->p_sys;
424     picture_t *p_pic;
425     yuv_buffer yuv;
426
427     theora_decode_packetin( &p_sys->td, p_oggpacket );
428
429     /* Check for keyframe */
430     if( !(p_oggpacket->packet[0] & 0x80) /* data packet */ &&
431         !(p_oggpacket->packet[0] & 0x40) /* intra frame */ )
432         p_sys->b_decoded_first_keyframe = true;
433
434     /* If we haven't seen a single keyframe yet, don't let Theora decode
435      * anything, otherwise we'll get display artifacts.  (This is impossible
436      * in the general case, but can happen if e.g. we play a network stream
437      * using a timed URL, such that the server doesn't start the video with a
438      * keyframe). */
439     if( p_sys->b_decoded_first_keyframe )
440         theora_decode_YUVout( &p_sys->td, &yuv );
441     else
442         return NULL;
443
444     /* Get a new picture */
445     p_pic = decoder_NewPicture( p_dec );
446     if( !p_pic ) return NULL;
447
448     theora_CopyPicture( p_pic, &yuv );
449
450     p_pic->date = p_sys->i_pts;
451
452     return p_pic;
453 }
454
455 /*****************************************************************************
456  * ParseTheoraComments:
457  *****************************************************************************/
458 static void ParseTheoraComments( decoder_t *p_dec )
459 {
460     char *psz_name, *psz_value, *psz_comment;
461     int i = 0;
462
463     while ( i < p_dec->p_sys->tc.comments )
464     {
465         psz_comment = strdup( p_dec->p_sys->tc.user_comments[i] );
466         if( !psz_comment )
467             break;
468         psz_name = psz_comment;
469         psz_value = strchr( psz_comment, '=' );
470         if( psz_value )
471         {
472             *psz_value = '\0';
473             psz_value++;
474
475             if( !p_dec->p_description )
476                 p_dec->p_description = vlc_meta_New();
477             if( p_dec->p_description )
478                 vlc_meta_AddExtra( p_dec->p_description, psz_name, psz_value );
479         }
480         free( psz_comment );
481         i++;
482     }
483 }
484
485 /*****************************************************************************
486  * CloseDecoder: theora decoder destruction
487  *****************************************************************************/
488 static void CloseDecoder( vlc_object_t *p_this )
489 {
490     decoder_t *p_dec = (decoder_t *)p_this;
491     decoder_sys_t *p_sys = p_dec->p_sys;
492
493     theora_info_clear( &p_sys->ti );
494     theora_comment_clear( &p_sys->tc );
495
496     free( p_sys );
497 }
498
499 /*****************************************************************************
500  * theora_CopyPicture: copy a picture from theora internal buffers to a
501  *                     picture_t structure.
502  *****************************************************************************/
503 static void theora_CopyPicture( picture_t *p_pic,
504                                 yuv_buffer *yuv )
505 {
506     int i_plane, i_line, i_dst_stride, i_src_stride;
507     uint8_t *p_dst, *p_src;
508
509     for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
510     {
511         p_dst = p_pic->p[i_plane].p_pixels;
512         p_src = i_plane ? (i_plane - 1 ? yuv->v : yuv->u ) : yuv->y;
513         i_dst_stride  = p_pic->p[i_plane].i_pitch;
514         i_src_stride  = i_plane ? yuv->uv_stride : yuv->y_stride;
515
516         for( i_line = 0; i_line < p_pic->p[i_plane].i_lines; i_line++ )
517         {
518             vlc_memcpy( p_dst, p_src,
519                         i_plane ? yuv->uv_width : yuv->y_width );
520             p_src += i_src_stride;
521             p_dst += i_dst_stride;
522         }
523     }
524 }
525
526 /*****************************************************************************
527  * encoder_sys_t : theora encoder descriptor
528  *****************************************************************************/
529 struct encoder_sys_t
530 {
531     /*
532      * Input properties
533      */
534     bool b_headers;
535
536     /*
537      * Theora properties
538      */
539     theora_info      ti;                        /* theora bitstream settings */
540     theora_comment   tc;                            /* theora comment header */
541     theora_state     td;                   /* theora bitstream user comments */
542
543     int i_width, i_height;
544 };
545
546 /*****************************************************************************
547  * OpenEncoder: probe the encoder and return score
548  *****************************************************************************/
549 static int OpenEncoder( vlc_object_t *p_this )
550 {
551     encoder_t *p_enc = (encoder_t *)p_this;
552     encoder_sys_t *p_sys;
553     int i_quality;
554
555     if( p_enc->fmt_out.i_codec != VLC_CODEC_THEORA &&
556         !p_enc->b_force )
557     {
558         return VLC_EGENERIC;
559     }
560
561     /* Allocate the memory needed to store the decoder's structure */
562     if( ( p_sys = malloc(sizeof(encoder_sys_t)) ) == NULL )
563         return VLC_ENOMEM;
564     p_enc->p_sys = p_sys;
565
566     p_enc->pf_encode_video = Encode;
567     p_enc->fmt_in.i_codec = VLC_CODEC_I420;
568     p_enc->fmt_out.i_codec = VLC_CODEC_THEORA;
569
570     config_ChainParse( p_enc, ENC_CFG_PREFIX, ppsz_enc_options, p_enc->p_cfg );
571
572     i_quality = var_GetInteger( p_enc, ENC_CFG_PREFIX "quality" );
573     if( i_quality > 10 ) i_quality = 10;
574     if( i_quality < 0 ) i_quality = 0;
575
576     theora_info_init( &p_sys->ti );
577
578     p_sys->ti.width = p_enc->fmt_in.video.i_width;
579     p_sys->ti.height = p_enc->fmt_in.video.i_height;
580
581     if( p_sys->ti.width % 16 || p_sys->ti.height % 16 )
582     {
583         /* Pictures from the transcoder should always have a pitch
584          * which is a multiple of 16 */
585         p_sys->ti.width = (p_sys->ti.width + 15) >> 4 << 4;
586         p_sys->ti.height = (p_sys->ti.height + 15) >> 4 << 4;
587
588         msg_Dbg( p_enc, "padding video from %dx%d to %dx%d",
589                  p_enc->fmt_in.video.i_width, p_enc->fmt_in.video.i_height,
590                  p_sys->ti.width, p_sys->ti.height );
591     }
592
593     p_sys->ti.frame_width = p_enc->fmt_in.video.i_width;
594     p_sys->ti.frame_height = p_enc->fmt_in.video.i_height;
595     p_sys->ti.offset_x = 0 /*frame_x_offset*/;
596     p_sys->ti.offset_y = 0 /*frame_y_offset*/;
597
598     p_sys->i_width = p_sys->ti.width;
599     p_sys->i_height = p_sys->ti.height;
600
601     if( !p_enc->fmt_in.video.i_frame_rate ||
602         !p_enc->fmt_in.video.i_frame_rate_base )
603     {
604         p_sys->ti.fps_numerator = 25;
605         p_sys->ti.fps_denominator = 1;
606     }
607     else
608     {
609         p_sys->ti.fps_numerator = p_enc->fmt_in.video.i_frame_rate;
610         p_sys->ti.fps_denominator = p_enc->fmt_in.video.i_frame_rate_base;
611     }
612
613     if( p_enc->fmt_in.video.i_sar_num > 0 && p_enc->fmt_in.video.i_sar_den > 0 )
614     {
615         unsigned i_dst_num, i_dst_den;
616         vlc_ureduce( &i_dst_num, &i_dst_den,
617                      p_enc->fmt_in.video.i_sar_num,
618                      p_enc->fmt_in.video.i_sar_den, 0 );
619         p_sys->ti.aspect_numerator = i_dst_num;
620         p_sys->ti.aspect_denominator = i_dst_den;
621     }
622     else
623     {
624         p_sys->ti.aspect_numerator = 4;
625         p_sys->ti.aspect_denominator = 3;
626     }
627
628     p_sys->ti.target_bitrate = p_enc->fmt_out.i_bitrate;
629     p_sys->ti.quality = ((float)i_quality) * 6.3;
630
631     p_sys->ti.dropframes_p = 0;
632     p_sys->ti.quick_p = 1;
633     p_sys->ti.keyframe_auto_p = 1;
634     p_sys->ti.keyframe_frequency = 64;
635     p_sys->ti.keyframe_frequency_force = 64;
636     p_sys->ti.keyframe_data_target_bitrate = p_enc->fmt_out.i_bitrate * 1.5;
637     p_sys->ti.keyframe_auto_threshold = 80;
638     p_sys->ti.keyframe_mindistance = 8;
639     p_sys->ti.noise_sensitivity = 1;
640
641     theora_encode_init( &p_sys->td, &p_sys->ti );
642     theora_comment_init( &p_sys->tc );
643
644     /* Create and store headers */
645     for( int i = 0; i < 3; i++ )
646     {
647         ogg_packet header;
648
649         if( i == 0 )
650             theora_encode_header( &p_sys->td, &header );
651         else if( i == 1 )
652             theora_encode_comment( &p_sys->tc, &header );
653         else
654             theora_encode_tables( &p_sys->td, &header );
655
656         if( xiph_AppendHeaders( &p_enc->fmt_out.i_extra, &p_enc->fmt_out.p_extra,
657                                 header.bytes, header.packet ) )
658         {
659             p_enc->fmt_out.i_extra = 0;
660             p_enc->fmt_out.p_extra = NULL;
661         }
662     }
663     return VLC_SUCCESS;
664 }
665
666 /****************************************************************************
667  * Encode: the whole thing
668  ****************************************************************************
669  * This function spits out ogg packets.
670  ****************************************************************************/
671 static block_t *Encode( encoder_t *p_enc, picture_t *p_pict )
672 {
673     encoder_sys_t *p_sys = p_enc->p_sys;
674     ogg_packet oggpacket;
675     block_t *p_block;
676     yuv_buffer yuv;
677     int i;
678
679     if( !p_pict ) return NULL;
680     /* Sanity check */
681     if( p_pict->p[0].i_pitch < (int)p_sys->i_width ||
682         p_pict->p[0].i_lines < (int)p_sys->i_height )
683     {
684         msg_Warn( p_enc, "frame is smaller than encoding size"
685                   "(%ix%i->%ix%i) -> dropping frame",
686                   p_pict->p[0].i_pitch, p_pict->p[0].i_lines,
687                   p_sys->i_width, p_sys->i_height );
688         return NULL;
689     }
690
691     /* Fill padding */
692     if( p_pict->p[0].i_visible_pitch < (int)p_sys->i_width )
693     {
694         for( i = 0; i < p_sys->i_height; i++ )
695         {
696             memset( p_pict->p[0].p_pixels + i * p_pict->p[0].i_pitch +
697                     p_pict->p[0].i_visible_pitch,
698                     *( p_pict->p[0].p_pixels + i * p_pict->p[0].i_pitch +
699                        p_pict->p[0].i_visible_pitch - 1 ),
700                     p_sys->i_width - p_pict->p[0].i_visible_pitch );
701         }
702         for( i = 0; i < p_sys->i_height / 2; i++ )
703         {
704             memset( p_pict->p[1].p_pixels + i * p_pict->p[1].i_pitch +
705                     p_pict->p[1].i_visible_pitch,
706                     *( p_pict->p[1].p_pixels + i * p_pict->p[1].i_pitch +
707                        p_pict->p[1].i_visible_pitch - 1 ),
708                     p_sys->i_width / 2 - p_pict->p[1].i_visible_pitch );
709             memset( p_pict->p[2].p_pixels + i * p_pict->p[2].i_pitch +
710                     p_pict->p[2].i_visible_pitch,
711                     *( p_pict->p[2].p_pixels + i * p_pict->p[2].i_pitch +
712                        p_pict->p[2].i_visible_pitch - 1 ),
713                     p_sys->i_width / 2 - p_pict->p[2].i_visible_pitch );
714         }
715     }
716
717     if( p_pict->p[0].i_visible_lines < (int)p_sys->i_height )
718     {
719         for( i = p_pict->p[0].i_visible_lines; i < p_sys->i_height; i++ )
720         {
721             memset( p_pict->p[0].p_pixels + i * p_pict->p[0].i_pitch, 0,
722                     p_sys->i_width );
723         }
724         for( i = p_pict->p[1].i_visible_lines; i < p_sys->i_height / 2; i++ )
725         {
726             memset( p_pict->p[1].p_pixels + i * p_pict->p[1].i_pitch, 0x80,
727                     p_sys->i_width / 2 );
728             memset( p_pict->p[2].p_pixels + i * p_pict->p[2].i_pitch, 0x80,
729                     p_sys->i_width / 2 );
730         }
731     }
732
733     /* Theora is a one-frame-in, one-frame-out system. Submit a frame
734      * for compression and pull out the packet. */
735
736     yuv.y_width  = p_sys->i_width;
737     yuv.y_height = p_sys->i_height;
738     yuv.y_stride = p_pict->p[0].i_pitch;
739
740     yuv.uv_width  = p_sys->i_width / 2;
741     yuv.uv_height = p_sys->i_height / 2;
742     yuv.uv_stride = p_pict->p[1].i_pitch;
743
744     yuv.y = p_pict->p[0].p_pixels;
745     yuv.u = p_pict->p[1].p_pixels;
746     yuv.v = p_pict->p[2].p_pixels;
747
748     if( theora_encode_YUVin( &p_sys->td, &yuv ) < 0 )
749     {
750         msg_Warn( p_enc, "failed encoding a frame" );
751         return NULL;
752     }
753
754     theora_encode_packetout( &p_sys->td, 0, &oggpacket );
755
756     /* Ogg packet to block */
757     p_block = block_New( p_enc, oggpacket.bytes );
758     memcpy( p_block->p_buffer, oggpacket.packet, oggpacket.bytes );
759     p_block->i_dts = p_block->i_pts = p_pict->date;
760
761     if( theora_packet_iskeyframe( &oggpacket ) )
762     {
763         p_block->i_flags |= BLOCK_FLAG_TYPE_I;
764     }
765
766     return p_block;
767 }
768
769 /*****************************************************************************
770  * CloseEncoder: theora encoder destruction
771  *****************************************************************************/
772 static void CloseEncoder( vlc_object_t *p_this )
773 {
774     encoder_t *p_enc = (encoder_t *)p_this;
775     encoder_sys_t *p_sys = p_enc->p_sys;
776
777     theora_info_clear( &p_sys->ti );
778     theora_comment_clear( &p_sys->tc );
779
780     free( p_sys );
781 }