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