]> git.sesse.net Git - vlc/blob - modules/codec/theora.c
* theora.c: Eliminate display artifacts when the very first frame of the
[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     if( theora_packet_iskeyframe( p_oggpacket ) == 1 )
446         p_sys->b_decoded_first_keyframe = VLC_TRUE;
447
448     /* If we haven't seen a single keyframe yet, don't let Theora decode
449      * anything, otherwise we'll get display artifacts.  (This is impossible
450      * in the general case, but can happen if e.g. we play a network stream
451      * using a timed URL, such that the server doesn't start the video with a
452      * keyframe). */
453     if( p_sys->b_decoded_first_keyframe )
454         theora_decode_YUVout( &p_sys->td, &yuv );
455     else
456         return NULL;
457
458     /* Get a new picture */
459     p_pic = p_dec->pf_vout_buffer_new( p_dec );
460     if( !p_pic ) return NULL;
461
462     theora_CopyPicture( p_dec, p_pic, &yuv );
463
464     p_pic->date = p_sys->i_pts;
465
466     return p_pic;
467 }
468
469 /*****************************************************************************
470  * ParseTheoraComments: FIXME should be done in demuxer
471  *****************************************************************************/
472 static void ParseTheoraComments( decoder_t *p_dec )
473 {
474     input_thread_t *p_input = (input_thread_t *)p_dec->p_parent;
475     char *psz_name, *psz_value, *psz_comment;
476     int i = 0;
477
478     if( p_input->i_object_type != VLC_OBJECT_INPUT ) return;
479
480     while ( i < p_dec->p_sys->tc.comments )
481     {
482         psz_comment = strdup( p_dec->p_sys->tc.user_comments[i] );
483         if( !psz_comment )
484         {
485             msg_Warn( p_dec, "out of memory" );
486             break;
487         }
488         psz_name = psz_comment;
489         psz_value = strchr( psz_comment, '=' );
490         if( psz_value )
491         {
492             *psz_value = '\0';
493             psz_value++;
494             input_Control( p_input, INPUT_ADD_INFO, _("Theora comment"),
495                            psz_name, psz_value );
496         }
497         free( psz_comment );
498         i++;
499     }
500 }
501
502 /*****************************************************************************
503  * CloseDecoder: theora decoder destruction
504  *****************************************************************************/
505 static void CloseDecoder( vlc_object_t *p_this )
506 {
507     decoder_t *p_dec = (decoder_t *)p_this;
508     decoder_sys_t *p_sys = p_dec->p_sys;
509
510     theora_info_clear( &p_sys->ti );
511     theora_comment_clear( &p_sys->tc );
512
513     free( p_sys );
514 }
515
516 /*****************************************************************************
517  * theora_CopyPicture: copy a picture from theora internal buffers to a
518  *                     picture_t structure.
519  *****************************************************************************/
520 static void theora_CopyPicture( decoder_t *p_dec, picture_t *p_pic,
521                                 yuv_buffer *yuv )
522 {
523     int i_plane, i_line, i_width, i_dst_stride, i_src_stride;
524     int i_src_xoffset, i_src_yoffset;
525     uint8_t *p_dst, *p_src;
526
527     for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
528     {
529         p_dst = p_pic->p[i_plane].p_pixels;
530         p_src = i_plane ? (i_plane - 1 ? yuv->v : yuv->u ) : yuv->y;
531         i_width = p_pic->p[i_plane].i_visible_pitch;
532         i_dst_stride  = p_pic->p[i_plane].i_pitch;
533         i_src_stride  = i_plane ? yuv->uv_stride : yuv->y_stride;
534         i_src_xoffset = p_dec->p_sys->ti.offset_x;
535         i_src_yoffset = p_dec->p_sys->ti.offset_y;
536         if( i_plane )
537         {
538             i_src_xoffset /= 2;
539             i_src_yoffset /= 2;
540         }
541
542         p_src += (i_src_yoffset * i_src_stride + i_src_yoffset);
543
544         for( i_line = 0; i_line < p_pic->p[i_plane].i_visible_lines; i_line++ )
545         {
546             p_dec->p_vlc->pf_memcpy( p_dst, p_src, i_width );
547             p_src += i_src_stride;
548             p_dst += i_dst_stride;
549         }
550     }
551 }
552
553 /*****************************************************************************
554  * encoder_sys_t : theora encoder descriptor
555  *****************************************************************************/
556 struct encoder_sys_t
557 {
558     /*
559      * Input properties
560      */
561     vlc_bool_t b_headers;
562
563     /*
564      * Theora properties
565      */
566     theora_info      ti;                        /* theora bitstream settings */
567     theora_comment   tc;                            /* theora comment header */
568     theora_state     td;                   /* theora bitstream user comments */
569
570     int i_width, i_height;
571 };
572
573 /*****************************************************************************
574  * OpenEncoder: probe the encoder and return score
575  *****************************************************************************/
576 static int OpenEncoder( vlc_object_t *p_this )
577 {
578     encoder_t *p_enc = (encoder_t *)p_this;
579     encoder_sys_t *p_sys = p_enc->p_sys;
580     ogg_packet header;
581     uint8_t *p_extra;
582     vlc_value_t val;
583     int i_quality, i;
584
585     if( p_enc->fmt_out.i_codec != VLC_FOURCC('t','h','e','o') &&
586         !p_enc->b_force )
587     {
588         return VLC_EGENERIC;
589     }
590
591     /* Allocate the memory needed to store the decoder's structure */
592     if( ( p_sys = (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
593     {
594         msg_Err( p_enc, "out of memory" );
595         return VLC_EGENERIC;
596     }
597     p_enc->p_sys = p_sys;
598
599     p_enc->pf_encode_video = Encode;
600     p_enc->fmt_in.i_codec = VLC_FOURCC('I','4','2','0');
601     p_enc->fmt_out.i_codec = VLC_FOURCC('t','h','e','o');
602
603     sout_CfgParse( p_enc, ENC_CFG_PREFIX, ppsz_enc_options, p_enc->p_cfg );
604
605     var_Get( p_enc, ENC_CFG_PREFIX "quality", &val );
606     i_quality = val.i_int;
607     if( i_quality > 10 ) i_quality = 10;
608     if( i_quality < 0 ) i_quality = 0;
609
610     theora_info_init( &p_sys->ti );
611
612     p_sys->ti.width = p_enc->fmt_in.video.i_width;
613     p_sys->ti.height = p_enc->fmt_in.video.i_height;
614
615     if( p_sys->ti.width % 16 || p_sys->ti.height % 16 )
616     {
617         /* Pictures from the transcoder should always have a pitch
618          * which is a multiple of 16 */
619         p_sys->ti.width = (p_sys->ti.width + 15) >> 4 << 4;
620         p_sys->ti.height = (p_sys->ti.height + 15) >> 4 << 4;
621
622         msg_Dbg( p_enc, "padding video from %dx%d to %dx%d",
623                  p_enc->fmt_in.video.i_width, p_enc->fmt_in.video.i_height,
624                  p_sys->ti.width, p_sys->ti.height );
625     }
626
627     p_sys->ti.frame_width = p_enc->fmt_in.video.i_width;
628     p_sys->ti.frame_height = p_enc->fmt_in.video.i_height;
629     p_sys->ti.offset_x = 0 /*frame_x_offset*/;
630     p_sys->ti.offset_y = 0 /*frame_y_offset*/;
631
632     p_sys->i_width = p_sys->ti.width;
633     p_sys->i_height = p_sys->ti.height;
634
635     if( !p_enc->fmt_in.video.i_frame_rate ||
636         !p_enc->fmt_in.video.i_frame_rate_base )
637     {
638         p_sys->ti.fps_numerator = 25;
639         p_sys->ti.fps_denominator = 1;
640     }
641     else
642     {
643         p_sys->ti.fps_numerator = p_enc->fmt_in.video.i_frame_rate;
644         p_sys->ti.fps_denominator = p_enc->fmt_in.video.i_frame_rate_base;
645     }
646
647     if( p_enc->fmt_in.video.i_aspect )
648     {
649         int64_t i_num, i_den;
650         int i_dst_num, i_dst_den;
651
652         i_num = p_enc->fmt_in.video.i_aspect * (int64_t)p_sys->ti.height;
653         i_den = VOUT_ASPECT_FACTOR * p_sys->ti.width;
654         vlc_reduce( &i_dst_num, &i_dst_den, i_num, i_den, 0 );
655         p_sys->ti.aspect_numerator = i_dst_num;
656         p_sys->ti.aspect_denominator = i_dst_den;
657     }
658     else
659     {
660         p_sys->ti.aspect_numerator = 4;
661         p_sys->ti.aspect_denominator = 3;
662     }
663
664     p_sys->ti.target_bitrate = p_enc->fmt_out.i_bitrate;
665     p_sys->ti.quality = ((float)i_quality) * 6.3;
666
667     p_sys->ti.dropframes_p = 0;
668     p_sys->ti.quick_p = 1;
669     p_sys->ti.keyframe_auto_p = 1;
670     p_sys->ti.keyframe_frequency = 64;
671     p_sys->ti.keyframe_frequency_force = 64;
672     p_sys->ti.keyframe_data_target_bitrate = p_enc->fmt_out.i_bitrate * 1.5;
673     p_sys->ti.keyframe_auto_threshold = 80;
674     p_sys->ti.keyframe_mindistance = 8;
675     p_sys->ti.noise_sensitivity = 1;
676
677     theora_encode_init( &p_sys->td, &p_sys->ti );
678     theora_info_clear( &p_sys->ti );
679     theora_comment_init( &p_sys->tc );
680
681     /* Create and store headers */
682     p_enc->fmt_out.i_extra = 3 * 2;
683     for( i = 0; i < 3; i++ )
684     {
685         if( i == 0 ) theora_encode_header( &p_sys->td, &header );
686         else if( i == 1 ) theora_encode_comment( &p_sys->tc, &header );
687         else if( i == 2 ) theora_encode_tables( &p_sys->td, &header );
688
689         p_enc->fmt_out.p_extra =
690             realloc( p_enc->fmt_out.p_extra,
691                      p_enc->fmt_out.i_extra + header.bytes );
692         p_extra = p_enc->fmt_out.p_extra;
693         p_extra += p_enc->fmt_out.i_extra + (i-3)*2;
694         p_enc->fmt_out.i_extra += header.bytes;
695
696         *(p_extra++) = header.bytes >> 8;
697         *(p_extra++) = header.bytes & 0xFF;
698
699         memcpy( p_extra, header.packet, header.bytes );
700     }
701
702     return VLC_SUCCESS;
703 }
704
705 /****************************************************************************
706  * Encode: the whole thing
707  ****************************************************************************
708  * This function spits out ogg packets.
709  ****************************************************************************/
710 static block_t *Encode( encoder_t *p_enc, picture_t *p_pict )
711 {
712     encoder_sys_t *p_sys = p_enc->p_sys;
713     ogg_packet oggpacket;
714     block_t *p_block;
715     yuv_buffer yuv;
716     int i;
717
718     /* Sanity check */
719     if( p_pict->p[0].i_pitch < (int)p_sys->i_width ||
720         p_pict->p[0].i_lines < (int)p_sys->i_height )
721     {
722         msg_Warn( p_enc, "frame is smaller than encoding size"
723                   "(%ix%i->%ix%i) -> dropping frame",
724                   p_pict->p[0].i_pitch, p_pict->p[0].i_lines,
725                   p_sys->i_width, p_sys->i_height );
726         return NULL;
727     }
728
729     /* Fill padding */
730     if( p_pict->p[0].i_visible_pitch < (int)p_sys->i_width )
731     {
732         for( i = 0; i < p_sys->i_height; i++ )
733         {
734             memset( p_pict->p[0].p_pixels + i * p_pict->p[0].i_pitch +
735                     p_pict->p[0].i_visible_pitch,
736                     *( p_pict->p[0].p_pixels + i * p_pict->p[0].i_pitch +
737                        p_pict->p[0].i_visible_pitch - 1 ),
738                     p_sys->i_width - p_pict->p[0].i_visible_pitch );
739         }
740         for( i = 0; i < p_sys->i_height / 2; i++ )
741         {
742             memset( p_pict->p[1].p_pixels + i * p_pict->p[1].i_pitch +
743                     p_pict->p[1].i_visible_pitch,
744                     *( p_pict->p[1].p_pixels + i * p_pict->p[1].i_pitch +
745                        p_pict->p[1].i_visible_pitch - 1 ),
746                     p_sys->i_width / 2 - p_pict->p[1].i_visible_pitch );
747             memset( p_pict->p[2].p_pixels + i * p_pict->p[2].i_pitch +
748                     p_pict->p[2].i_visible_pitch,
749                     *( p_pict->p[2].p_pixels + i * p_pict->p[2].i_pitch +
750                        p_pict->p[2].i_visible_pitch - 1 ),
751                     p_sys->i_width / 2 - p_pict->p[2].i_visible_pitch );
752         }
753     }
754
755     if( p_pict->p[0].i_visible_lines < (int)p_sys->i_height )
756     {
757         for( i = p_pict->p[0].i_visible_lines; i < p_sys->i_height; i++ )
758         {
759             memset( p_pict->p[0].p_pixels + i * p_pict->p[0].i_pitch, 0,
760                     p_sys->i_width );
761         }
762         for( i = p_pict->p[1].i_visible_lines; i < p_sys->i_height / 2; i++ )
763         {
764             memset( p_pict->p[1].p_pixels + i * p_pict->p[1].i_pitch, 0x80,
765                     p_sys->i_width / 2 );
766             memset( p_pict->p[2].p_pixels + i * p_pict->p[2].i_pitch, 0x80,
767                     p_sys->i_width / 2 );
768         }
769     }
770
771     /* Theora is a one-frame-in, one-frame-out system. Submit a frame
772      * for compression and pull out the packet. */
773
774     yuv.y_width  = p_sys->i_width;
775     yuv.y_height = p_sys->i_height;
776     yuv.y_stride = p_pict->p[0].i_pitch;
777
778     yuv.uv_width  = p_sys->i_width / 2;
779     yuv.uv_height = p_sys->i_height / 2;
780     yuv.uv_stride = p_pict->p[1].i_pitch;
781
782     yuv.y = p_pict->p[0].p_pixels;
783     yuv.u = p_pict->p[1].p_pixels;
784     yuv.v = p_pict->p[2].p_pixels;
785
786     if( theora_encode_YUVin( &p_sys->td, &yuv ) < 0 )
787     {
788         msg_Warn( p_enc, "failed encoding a frame" );
789         return NULL;
790     }
791
792     theora_encode_packetout( &p_sys->td, 0, &oggpacket );
793
794     /* Ogg packet to block */
795     p_block = block_New( p_enc, oggpacket.bytes );
796     memcpy( p_block->p_buffer, oggpacket.packet, oggpacket.bytes );
797     p_block->i_dts = p_block->i_pts = p_pict->date;
798
799     return p_block;
800 }
801
802 /*****************************************************************************
803  * CloseEncoder: theora encoder destruction
804  *****************************************************************************/
805 static void CloseEncoder( vlc_object_t *p_this )
806 {
807     encoder_t *p_enc = (encoder_t *)p_this;
808     encoder_sys_t *p_sys = p_enc->p_sys;
809
810     theora_info_clear( &p_sys->ti );
811     theora_comment_clear( &p_sys->tc );
812
813     free( p_sys );
814 }