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