]> git.sesse.net Git - vlc/blob - modules/codec/theora.c
ALL: a bunch of compilation fixes + disabled plugins that don't compile anymore.
[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 void *ProcessPacket ( decoder_t *, ogg_packet *, block_t ** );
71
72 static picture_t *DecodePacket( decoder_t *, ogg_packet * );
73
74 static void ParseTheoraComments( decoder_t * );
75 static void theora_CopyPicture( decoder_t *, picture_t *, yuv_buffer * );
76
77 static int  OpenEncoder( vlc_object_t *p_this );
78 static void CloseEncoder( vlc_object_t *p_this );
79 static block_t *Headers( encoder_t *p_enc );
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     if( p_sys->i_headers == 0 )
199     {
200         /* Take care of the initial Theora header */
201
202         oggpacket.b_o_s = 1; /* yes this actually is a b_o_s packet :) */
203         if( theora_decode_header( &p_sys->ti, &p_sys->tc, &oggpacket ) < 0 )
204         {
205             msg_Err( p_dec, "This bitstream does not contain Theora "
206                      "video data." );
207             block_Release( p_block );
208             return NULL;
209         }
210         p_sys->i_headers++;
211
212         /* Set output properties */
213         p_dec->fmt_out.video.i_width = p_sys->ti.width;
214         p_dec->fmt_out.video.i_height = p_sys->ti.height;
215
216         if( p_sys->ti.aspect_denominator && p_sys->ti.aspect_numerator )
217             p_dec->fmt_out.video.i_aspect = ((int64_t)VOUT_ASPECT_FACTOR) *
218                 p_sys->ti.aspect_numerator / p_sys->ti.aspect_denominator;
219         else
220             p_dec->fmt_out.video.i_aspect = VOUT_ASPECT_FACTOR *
221                 p_sys->ti.frame_width / p_sys->ti.frame_height;
222
223         msg_Dbg( p_dec, "%dx%d %.02f fps video, frame content "
224                  "is %dx%d with offset (%d,%d).",
225                  p_sys->ti.width, p_sys->ti.height,
226                  (double)p_sys->ti.fps_numerator/p_sys->ti.fps_denominator,
227                  p_sys->ti.frame_width, p_sys->ti.frame_height,
228                  p_sys->ti.offset_x, p_sys->ti.offset_y );
229
230         return ProcessPacket( p_dec, &oggpacket, pp_block );
231     }
232
233     if( p_sys->i_headers == 1 )
234     {
235         /* The next packet in order is the comments header */
236         if( theora_decode_header( &p_sys->ti, &p_sys->tc, &oggpacket ) < 0 )
237         {
238             msg_Err( p_dec, "2nd Theora header is corrupted." );
239             return NULL;
240         }
241         p_sys->i_headers++;
242
243         ParseTheoraComments( p_dec );
244
245         return ProcessPacket( p_dec, &oggpacket, pp_block );
246     }
247
248     if( p_sys->i_headers == 2 )
249     {
250         /* The next packet in order is the codebooks header
251            We need to watch out that this packet is not missing as a
252            missing or corrupted header is fatal. */
253         if( theora_decode_header( &p_sys->ti, &p_sys->tc, &oggpacket ) < 0 )
254         {
255             msg_Err( p_dec, "3rd Theora header is corrupted." );
256             return NULL;
257         }
258         p_sys->i_headers++;
259
260         if( !p_sys->b_packetizer )
261         {
262             /* We have all the headers, initialize decoder */
263             theora_decode_init( &p_sys->td, &p_sys->ti );
264         }
265
266         return ProcessPacket( p_dec, &oggpacket, pp_block );
267     }
268
269     return ProcessPacket( p_dec, &oggpacket, pp_block );
270 }
271
272 /*****************************************************************************
273  * ProcessPacket: processes a theora packet.
274  *****************************************************************************/
275 static void *ProcessPacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
276                             block_t **pp_block )
277 {
278     decoder_sys_t *p_sys = p_dec->p_sys;
279     block_t *p_block = *pp_block;
280     void *p_buf;
281
282     /* Date management */
283     if( p_block->i_pts > 0 && p_block->i_pts != p_sys->i_pts )
284     {
285         p_sys->i_pts = p_block->i_pts;
286     }
287
288     if( p_sys->b_packetizer )
289     {
290         /* Date management */
291         p_block->i_dts = p_block->i_pts = p_sys->i_pts;
292
293         if( p_sys->i_headers >= 3 )
294             p_block->i_length = p_sys->i_pts - p_block->i_pts;
295         else
296             p_block->i_length = 0;
297
298         p_buf = p_block;
299     }
300     else
301     {
302         if( p_sys->i_headers >= 3 )
303             p_buf = DecodePacket( p_dec, p_oggpacket );
304         else
305             p_buf = NULL;
306
307         if( p_block )
308         {
309             block_Release( p_block );
310             *pp_block = NULL;
311         }
312     }
313
314     /* Date management */
315     p_sys->i_pts += ( I64C(1000000) * p_sys->ti.fps_denominator /
316                       p_sys->ti.fps_numerator ); /* 1 frame per packet */
317
318     return p_buf;
319 }
320
321 /*****************************************************************************
322  * DecodePacket: decodes a Theora packet.
323  *****************************************************************************/
324 static picture_t *DecodePacket( decoder_t *p_dec, ogg_packet *p_oggpacket )
325 {
326     decoder_sys_t *p_sys = p_dec->p_sys;
327     picture_t *p_pic;
328     yuv_buffer yuv;
329
330     theora_decode_packetin( &p_sys->td, p_oggpacket );
331
332     /* Decode */
333     theora_decode_YUVout( &p_sys->td, &yuv );
334
335     /* Get a new picture */
336     p_pic = p_dec->pf_vout_buffer_new( p_dec );
337     if( !p_pic ) return NULL;
338
339     theora_CopyPicture( p_dec, p_pic, &yuv );
340
341     p_pic->date = p_sys->i_pts;
342
343     return p_pic;
344 }
345
346 /*****************************************************************************
347  * ParseTheoraComments: FIXME should be done in demuxer
348  *****************************************************************************/
349 static void ParseTheoraComments( decoder_t *p_dec )
350 {
351     input_thread_t *p_input = (input_thread_t *)p_dec->p_parent;
352
353     int i = 0;
354     char *psz_name, *psz_value, *psz_comment;
355     while ( i < p_dec->p_sys->tc.comments )
356     {
357         psz_comment = strdup( p_dec->p_sys->tc.user_comments[i] );
358         if( !psz_comment )
359         {
360             msg_Warn( p_dec, "out of memory" );
361             break;
362         }
363         psz_name = psz_comment;
364         psz_value = strchr( psz_comment, '=' );
365         if( psz_value )
366         {
367             *psz_value = '\0';
368             psz_value++;
369             input_Control( p_input, INPUT_ADD_INFO, _("Theora comment"),
370                            psz_name, psz_value );
371         }
372         free( psz_comment );
373         i++;
374     }
375 }
376
377 /*****************************************************************************
378  * CloseDecoder: theora decoder destruction
379  *****************************************************************************/
380 static void CloseDecoder( vlc_object_t *p_this )
381 {
382     decoder_t *p_dec = (decoder_t *)p_this;
383     decoder_sys_t *p_sys = p_dec->p_sys;
384
385     theora_info_clear( &p_sys->ti );
386     theora_comment_clear( &p_sys->tc );
387
388     free( p_sys );
389 }
390
391 /*****************************************************************************
392  * theora_CopyPicture: copy a picture from theora internal buffers to a
393  *                     picture_t structure.
394  *****************************************************************************/
395 static void theora_CopyPicture( decoder_t *p_dec, picture_t *p_pic,
396                                 yuv_buffer *yuv )
397 {
398     int i_plane, i_line, i_width, i_dst_stride, i_src_stride;
399     int i_src_xoffset, i_src_yoffset;
400     uint8_t *p_dst, *p_src;
401
402     for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
403     {
404         p_dst = p_pic->p[i_plane].p_pixels;
405         p_src = i_plane ? (i_plane - 1 ? yuv->v : yuv->u ) : yuv->y;
406         i_width = p_pic->p[i_plane].i_visible_pitch;
407         i_dst_stride  = p_pic->p[i_plane].i_pitch;
408         i_src_stride  = i_plane ? yuv->uv_stride : yuv->y_stride;
409         i_src_xoffset = p_dec->p_sys->ti.offset_x;
410         i_src_yoffset = p_dec->p_sys->ti.offset_y;
411         if( i_plane )
412         {
413             i_src_xoffset /= 2;
414             i_src_yoffset /= 2;
415         }
416
417         p_src += (i_src_yoffset * i_src_stride + i_src_yoffset);
418
419         for( i_line = 0; i_line < p_pic->p[i_plane].i_lines; i_line++ )
420         {
421             p_dec->p_vlc->pf_memcpy( p_dst, p_src, i_width );
422             p_src += i_src_stride;
423             p_dst += i_dst_stride;
424         }
425     }
426 }
427
428 /*****************************************************************************
429  * encoder_sys_t : theora encoder descriptor
430  *****************************************************************************/
431 struct encoder_sys_t
432 {
433     /*
434      * Input properties
435      */
436     vlc_bool_t b_headers;
437
438     /*
439      * Theora properties
440      */
441     theora_info      ti;                        /* theora bitstream settings */
442     theora_comment   tc;                            /* theora comment header */
443     theora_state     td;                   /* theora bitstream user comments */
444
445     /*
446      * Common properties
447      */
448     mtime_t i_pts;
449 };
450
451 /*****************************************************************************
452  * OpenEncoder: probe the encoder and return score
453  *****************************************************************************/
454 static int OpenEncoder( vlc_object_t *p_this )
455 {
456     encoder_t *p_enc = (encoder_t *)p_this;
457     encoder_sys_t *p_sys = p_enc->p_sys;
458     vlc_value_t val;
459     int i_quality;
460
461     if( p_enc->fmt_out.i_codec != VLC_FOURCC('t','h','e','o') &&
462         !p_enc->b_force )
463     {
464         return VLC_EGENERIC;
465     }
466
467     if( p_enc->fmt_in.video.i_width % 16 ||
468         p_enc->fmt_in.video.i_height % 16 )
469     {
470         msg_Err( p_enc, "Theora video encoding requires dimensions which are "
471                  "multiples of 16. Which is not the case here (%dx%d).",
472                  p_enc->fmt_in.video.i_width, p_enc->fmt_in.video.i_height );
473         return VLC_EGENERIC;
474     }
475
476     /* Allocate the memory needed to store the decoder's structure */
477     if( ( p_sys = (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
478     {
479         msg_Err( p_enc, "out of memory" );
480         return VLC_EGENERIC;
481     }
482     p_enc->p_sys = p_sys;
483
484     p_enc->pf_header = Headers;
485     p_enc->pf_encode_video = Encode;
486     p_enc->fmt_in.i_codec = VLC_FOURCC('I','4','2','0');
487     p_enc->fmt_out.i_codec = VLC_FOURCC('t','h','e','o');
488
489     sout_ParseCfg( p_enc, ENC_CFG_PREFIX, ppsz_enc_options, p_enc->p_cfg );
490
491     var_Get( p_enc, ENC_CFG_PREFIX "quality", &val );
492     i_quality = val.i_int;
493     if( i_quality > 10 ) i_quality = 10;
494     if( i_quality < 0 ) i_quality = 0;
495
496     theora_info_init( &p_sys->ti );
497
498     p_sys->ti.width = p_enc->fmt_in.video.i_width;
499     p_sys->ti.height = p_enc->fmt_in.video.i_height;
500     p_sys->ti.frame_width = p_enc->fmt_in.video.i_width;
501     p_sys->ti.frame_height = p_enc->fmt_in.video.i_height;
502     p_sys->ti.offset_x = 0 /*frame_x_offset*/;
503     p_sys->ti.offset_y = 0/*frame_y_offset*/;
504
505     if( !p_enc->fmt_in.video.i_frame_rate ||
506         !p_enc->fmt_in.video.i_frame_rate_base )
507     {
508         p_sys->ti.fps_numerator = 25;
509         p_sys->ti.fps_denominator = 1;
510     }
511     else
512     {
513         p_sys->ti.fps_numerator = p_enc->fmt_in.video.i_frame_rate;
514         p_sys->ti.fps_denominator = p_enc->fmt_in.video.i_frame_rate_base;
515     }
516
517     if( p_enc->fmt_in.video.i_aspect )
518     {
519         p_sys->ti.aspect_numerator = p_enc->fmt_in.video.i_aspect;
520         p_sys->ti.aspect_denominator = VOUT_ASPECT_FACTOR;
521     }
522     else
523     {
524         p_sys->ti.aspect_numerator = 4;
525         p_sys->ti.aspect_denominator = 3;
526     }
527
528     p_sys->ti.target_bitrate = p_enc->fmt_out.i_bitrate;
529     p_sys->ti.quality = ((float)i_quality) * 6.3;
530
531     p_sys->ti.dropframes_p = 0;
532     p_sys->ti.quick_p = 1;
533     p_sys->ti.keyframe_auto_p = 1;
534     p_sys->ti.keyframe_frequency = 64;
535     p_sys->ti.keyframe_frequency_force = 64;
536     p_sys->ti.keyframe_data_target_bitrate = p_enc->fmt_out.i_bitrate * 1.5;
537     p_sys->ti.keyframe_auto_threshold = 80;
538     p_sys->ti.keyframe_mindistance = 8;
539     p_sys->ti.noise_sensitivity = 1;
540
541     theora_encode_init( &p_sys->td, &p_sys->ti );
542     theora_info_clear( &p_sys->ti );
543     theora_comment_init( &p_sys->tc );
544
545     p_sys->b_headers = VLC_FALSE;
546
547     return VLC_SUCCESS;
548 }
549
550 /****************************************************************************
551  * Encode: the whole thing
552  ****************************************************************************
553  * This function spits out ogg packets.
554  ****************************************************************************/
555 static block_t *Headers( encoder_t *p_enc )
556 {
557     encoder_sys_t *p_sys = p_enc->p_sys;
558     block_t *p_chain = NULL;
559
560     /* Create theora headers */
561     if( !p_sys->b_headers )
562     {
563         ogg_packet oggpackets;
564         int i;
565         block_t *p_block;
566
567         /* Ogg packet to block */
568         for( i = 0; i < 3; i++ )
569         {
570             switch( i )
571             {
572             case 0:
573                 theora_encode_header( &p_sys->td, &oggpackets );
574                 break;
575             case 1:
576                 theora_encode_comment( &p_sys->tc, &oggpackets );
577                 break;
578             case 2:
579                 theora_encode_tables( &p_sys->td, &oggpackets );
580                 break;
581             }
582
583             p_block = block_New( p_enc, oggpackets.bytes );
584             memcpy( p_block->p_buffer, oggpackets.packet, oggpackets.bytes );
585             p_block->i_dts = p_block->i_pts = p_block->i_length = 0;
586             block_ChainAppend( &p_chain, p_block );
587         }
588
589         p_sys->b_headers = VLC_TRUE;
590     }
591
592     return p_chain;
593 }
594
595 /****************************************************************************
596  * Encode: the whole thing
597  ****************************************************************************
598  * This function spits out ogg packets.
599  ****************************************************************************/
600 static block_t *Encode( encoder_t *p_enc, picture_t *p_pict )
601 {
602     encoder_sys_t *p_sys = p_enc->p_sys;
603     ogg_packet oggpacket;
604     block_t *p_block;
605     yuv_buffer yuv;
606
607     /* Theora is a one-frame-in, one-frame-out system. Submit a frame
608      * for compression and pull out the packet. */
609
610     yuv.y_width  = p_pict->p[0].i_visible_pitch;
611     yuv.y_height = p_pict->p[0].i_lines;
612     yuv.y_stride = p_pict->p[0].i_pitch;
613
614     yuv.uv_width  = p_pict->p[1].i_visible_pitch;
615     yuv.uv_height = p_pict->p[1].i_lines;
616     yuv.uv_stride = p_pict->p[1].i_pitch;
617
618     yuv.y = p_pict->p[0].p_pixels;
619     yuv.u = p_pict->p[1].p_pixels;
620     yuv.v = p_pict->p[2].p_pixels;
621
622     if( theora_encode_YUVin( &p_sys->td, &yuv ) < 0 )
623     {
624         msg_Warn( p_enc, "failed encoding a frame" );
625         return NULL;
626     }
627
628     theora_encode_packetout( &p_sys->td, 0, &oggpacket );
629
630     /* Ogg packet to block */
631     p_block = block_New( p_enc, oggpacket.bytes );
632     memcpy( p_block->p_buffer, oggpacket.packet, oggpacket.bytes );
633     p_block->i_dts = p_block->i_pts = p_pict->date;
634
635     return p_block;
636 }
637
638 /*****************************************************************************
639  * CloseEncoder: theora encoder destruction
640  *****************************************************************************/
641 static void CloseEncoder( vlc_object_t *p_this )
642 {
643     encoder_t *p_enc = (encoder_t *)p_this;
644     encoder_sys_t *p_sys = p_enc->p_sys;
645
646     theora_info_clear( &p_sys->ti );
647     theora_comment_clear( &p_sys->tc );
648
649     free( p_sys );
650 }