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