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