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