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