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