]> git.sesse.net Git - vlc/blob - modules/codec/theora.c
* modules/codec/theora.c: new --sout-theora-quality option.
[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/sout.h>
30
31 #include <ogg/ogg.h>
32
33 #include <theora/theora.h>
34
35 /*****************************************************************************
36  * decoder_sys_t : theora decoder descriptor
37  *****************************************************************************/
38 struct decoder_sys_t
39 {
40     /* Module mode */
41     vlc_bool_t b_packetizer;
42
43     /*
44      * Input properties
45      */
46     int i_headers;
47
48     /*
49      * Theora properties
50      */
51     theora_info      ti;                        /* theora bitstream settings */
52     theora_comment   tc;                            /* theora comment header */
53     theora_state     td;                   /* theora bitstream user comments */
54
55     /*
56      * Common properties
57      */
58     mtime_t i_pts;
59 };
60
61 /*****************************************************************************
62  * Local prototypes
63  *****************************************************************************/
64 static int  OpenDecoder   ( vlc_object_t * );
65 static int  OpenPacketizer( vlc_object_t * );
66 static void CloseDecoder  ( vlc_object_t * );
67
68 static void *DecodeBlock  ( decoder_t *, block_t ** );
69 static void *ProcessPacket ( decoder_t *, ogg_packet *, block_t ** );
70
71 static picture_t *DecodePacket( decoder_t *, ogg_packet * );
72
73 static void ParseTheoraComments( decoder_t * );
74 static void theora_CopyPicture( decoder_t *, picture_t *, yuv_buffer * );
75
76 static int  OpenEncoder( vlc_object_t *p_this );
77 static void CloseEncoder( vlc_object_t *p_this );
78 static block_t *Headers( encoder_t *p_enc );
79 static block_t *Encode( encoder_t *p_enc, picture_t *p_pict );
80
81 /*****************************************************************************
82  * Module descriptor
83  *****************************************************************************/
84 #define ENC_QUALITY_TEXT N_("Encoding quality")
85 #define ENC_QUALITY_LONGTEXT N_( \
86   "Allows you to specify a quality between 1 (low) and 10 (high), instead " \
87   "of specifying a particular bitrate. This will produce a VBR stream." )
88
89 vlc_module_begin();
90     set_description( _("Theora video decoder") );
91     set_capability( "decoder", 100 );
92     set_callbacks( OpenDecoder, CloseDecoder );
93     add_shortcut( "theora" );
94
95     add_submodule();
96     set_description( _("Theora video packetizer") );
97     set_capability( "packetizer", 100 );
98     set_callbacks( OpenPacketizer, CloseDecoder );
99     add_shortcut( "theora" );
100
101     add_submodule();
102     set_description( _("Theora video encoder") );
103     set_capability( "encoder", 100 );
104     set_callbacks( OpenEncoder, CloseEncoder );
105     add_shortcut( "theora" );
106
107 #   define ENC_CFG_PREFIX "sout-theora-"
108     add_integer( ENC_CFG_PREFIX "quality", 2, NULL, ENC_QUALITY_TEXT,
109                  ENC_QUALITY_LONGTEXT, VLC_FALSE );
110 vlc_module_end();
111
112 static const char *ppsz_enc_options[] = {
113     "quality", NULL
114 };
115
116 /*****************************************************************************
117  * OpenDecoder: probe the decoder and return score
118  *****************************************************************************/
119 static int OpenDecoder( vlc_object_t *p_this )
120 {
121     decoder_t *p_dec = (decoder_t*)p_this;
122     decoder_sys_t *p_sys;
123
124     if( p_dec->fmt_in.i_codec != VLC_FOURCC('t','h','e','o') )
125     {
126         return VLC_EGENERIC;
127     }
128
129     /* Allocate the memory needed to store the decoder's structure */
130     if( ( p_dec->p_sys = p_sys =
131           (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
132     {
133         msg_Err( p_dec, "out of memory" );
134         return VLC_EGENERIC;
135     }
136     p_dec->p_sys->b_packetizer = VLC_FALSE;
137
138     p_sys->i_pts = 0;
139
140     /* Set output properties */
141     p_dec->fmt_out.i_cat = VIDEO_ES;
142     p_dec->fmt_out.i_codec = VLC_FOURCC('I','4','2','0');
143
144     /* Set callbacks */
145     p_dec->pf_decode_video = (picture_t *(*)(decoder_t *, block_t **))
146         DecodeBlock;
147     p_dec->pf_packetize    = (block_t *(*)(decoder_t *, block_t **))
148         DecodeBlock;
149
150     /* Init supporting Theora structures needed in header parsing */
151     theora_comment_init( &p_sys->tc );
152     theora_info_init( &p_sys->ti );
153
154     p_sys->i_headers = 0;
155
156     return VLC_SUCCESS;
157 }
158
159 static int OpenPacketizer( vlc_object_t *p_this )
160 {
161     decoder_t *p_dec = (decoder_t*)p_this;
162
163     int i_ret = OpenDecoder( p_this );
164
165     if( i_ret == VLC_SUCCESS )
166     {
167         p_dec->p_sys->b_packetizer = VLC_TRUE;
168         p_dec->fmt_out.i_codec = VLC_FOURCC( 't', 'h', 'e', 'o' );
169     }
170
171     return i_ret;
172 }
173
174 /****************************************************************************
175  * DecodeBlock: the whole thing
176  ****************************************************************************
177  * This function must be fed with ogg packets.
178  ****************************************************************************/
179 static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
180 {
181     decoder_sys_t *p_sys = p_dec->p_sys;
182     block_t *p_block;
183     ogg_packet oggpacket;
184
185     if( !pp_block || !*pp_block ) return NULL;
186
187     p_block = *pp_block;
188
189     /* Block to Ogg packet */
190     oggpacket.packet = p_block->p_buffer;
191     oggpacket.bytes = p_block->i_buffer;
192     oggpacket.granulepos = p_block->i_dts;
193     oggpacket.b_o_s = 0;
194     oggpacket.e_o_s = 0;
195     oggpacket.packetno = 0;
196
197     if( p_sys->i_headers == 0 )
198     {
199         /* Take care of the initial Theora header */
200
201         oggpacket.b_o_s = 1; /* yes this actually is a b_o_s packet :) */
202         if( theora_decode_header( &p_sys->ti, &p_sys->tc, &oggpacket ) < 0 )
203         {
204             msg_Err( p_dec, "This bitstream does not contain Theora "
205                      "video data." );
206             block_Release( p_block );
207             return NULL;
208         }
209         p_sys->i_headers++;
210
211         /* Set output properties */
212         p_dec->fmt_out.video.i_width = p_sys->ti.width;
213         p_dec->fmt_out.video.i_height = p_sys->ti.height;
214
215         if( p_sys->ti.aspect_denominator )
216             p_dec->fmt_out.video.i_aspect = ((int64_t)VOUT_ASPECT_FACTOR) *
217                 p_sys->ti.aspect_numerator / p_sys->ti.aspect_denominator;
218         else
219             p_dec->fmt_out.video.i_aspect = VOUT_ASPECT_FACTOR *
220                 p_sys->ti.frame_width / p_sys->ti.frame_height;
221
222         msg_Dbg( p_dec, "%dx%d %.02f fps video, frame content "
223                  "is %dx%d with offset (%d,%d).",
224                  p_sys->ti.width, p_sys->ti.height,
225                  (double)p_sys->ti.fps_numerator/p_sys->ti.fps_denominator,
226                  p_sys->ti.frame_width, p_sys->ti.frame_height,
227                  p_sys->ti.offset_x, p_sys->ti.offset_y );
228
229         return ProcessPacket( p_dec, &oggpacket, pp_block );
230     }
231
232     if( p_sys->i_headers == 1 )
233     {
234         /* The next packet in order is the comments header */
235         if( theora_decode_header( &p_sys->ti, &p_sys->tc, &oggpacket ) < 0 )
236         {
237             msg_Err( p_dec, "2nd Theora header is corrupted." );
238             return NULL;
239         }
240         p_sys->i_headers++;
241
242         ParseTheoraComments( p_dec );
243
244         return ProcessPacket( p_dec, &oggpacket, pp_block );
245     }
246
247     if( p_sys->i_headers == 2 )
248     {
249         /* The next packet in order is the codebooks header
250            We need to watch out that this packet is not missing as a
251            missing or corrupted header is fatal. */
252         if( theora_decode_header( &p_sys->ti, &p_sys->tc, &oggpacket ) < 0 )
253         {
254             msg_Err( p_dec, "3rd Theora header is corrupted." );
255             return NULL;
256         }
257         p_sys->i_headers++;
258
259         if( !p_sys->b_packetizer )
260         {
261             /* We have all the headers, initialize decoder */
262             theora_decode_init( &p_sys->td, &p_sys->ti );
263         }
264
265         return ProcessPacket( p_dec, &oggpacket, pp_block );
266     }
267
268     return ProcessPacket( p_dec, &oggpacket, pp_block );
269 }
270
271 /*****************************************************************************
272  * ProcessPacket: processes a theora packet.
273  *****************************************************************************/
274 static void *ProcessPacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
275                             block_t **pp_block )
276 {
277     decoder_sys_t *p_sys = p_dec->p_sys;
278     block_t *p_block = *pp_block;
279     void *p_buf;
280
281     /* Date management */
282     if( p_block->i_pts > 0 && p_block->i_pts != p_sys->i_pts )
283     {
284         p_sys->i_pts = p_block->i_pts;
285     }
286
287     if( p_sys->b_packetizer )
288     {
289         /* Date management */
290         p_block->i_dts = p_block->i_pts = p_sys->i_pts;
291
292         if( p_sys->i_headers >= 3 )
293             p_block->i_length = p_sys->i_pts - p_block->i_pts;
294         else
295             p_block->i_length = 0;
296
297         p_buf = p_block;
298     }
299     else
300     {
301         if( p_sys->i_headers >= 3 )
302             p_buf = DecodePacket( p_dec, p_oggpacket );
303         else
304             p_buf = NULL;
305
306         if( p_block )
307         {
308             block_Release( p_block );
309             *pp_block = NULL;
310         }
311     }
312
313     /* Date management */
314     p_sys->i_pts += ( I64C(1000000) * p_sys->ti.fps_denominator /
315                       p_sys->ti.fps_numerator ); /* 1 frame per packet */
316
317     return p_buf;
318 }
319
320 /*****************************************************************************
321  * DecodePacket: decodes a Theora packet.
322  *****************************************************************************/
323 static picture_t *DecodePacket( decoder_t *p_dec, ogg_packet *p_oggpacket )
324 {
325     decoder_sys_t *p_sys = p_dec->p_sys;
326     picture_t *p_pic;
327     yuv_buffer yuv;
328
329     theora_decode_packetin( &p_sys->td, p_oggpacket );
330
331     /* Decode */
332     theora_decode_YUVout( &p_sys->td, &yuv );
333
334     /* Get a new picture */
335     p_pic = p_dec->pf_vout_buffer_new( p_dec );
336     if( !p_pic ) return NULL;
337
338     theora_CopyPicture( p_dec, p_pic, &yuv );
339
340     p_pic->date = p_sys->i_pts;
341
342     return p_pic;
343 }
344
345 /*****************************************************************************
346  * ParseTheoraComments: FIXME should be done in demuxer
347  *****************************************************************************/
348 static void ParseTheoraComments( decoder_t *p_dec )
349 {
350     input_thread_t *p_input = (input_thread_t *)p_dec->p_parent;
351
352     int i = 0;
353     char *psz_name, *psz_value, *psz_comment;
354     while ( i < p_dec->p_sys->tc.comments )
355     {
356         psz_comment = strdup( p_dec->p_sys->tc.user_comments[i] );
357         if( !psz_comment )
358         {
359             msg_Warn( p_dec, "out of memory" );
360             break;
361         }
362         psz_name = psz_comment;
363         psz_value = strchr( psz_comment, '=' );
364         if( psz_value )
365         {
366             *psz_value = '\0';
367             psz_value++;
368             input_Control( p_input, INPUT_ADD_INFO, _("Theora comment"),
369                            psz_name, psz_value );
370         }
371         free( psz_comment );
372         i++;
373     }
374 }
375
376 /*****************************************************************************
377  * CloseDecoder: theora decoder destruction
378  *****************************************************************************/
379 static void CloseDecoder( vlc_object_t *p_this )
380 {
381     decoder_t *p_dec = (decoder_t *)p_this;
382     decoder_sys_t *p_sys = p_dec->p_sys;
383
384     theora_info_clear( &p_sys->ti );
385     theora_comment_clear( &p_sys->tc );
386
387     free( p_sys );
388 }
389
390 /*****************************************************************************
391  * theora_CopyPicture: copy a picture from theora internal buffers to a
392  *                     picture_t structure.
393  *****************************************************************************/
394 static void theora_CopyPicture( decoder_t *p_dec, picture_t *p_pic,
395                                 yuv_buffer *yuv )
396 {
397     int i_plane, i_line, i_width, i_dst_stride, i_src_stride;
398     int i_src_xoffset, i_src_yoffset;
399     uint8_t *p_dst, *p_src;
400
401     for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
402     {
403         p_dst = p_pic->p[i_plane].p_pixels;
404         p_src = i_plane ? (i_plane - 1 ? yuv->v : yuv->u ) : yuv->y;
405         i_width = p_pic->p[i_plane].i_visible_pitch;
406         i_dst_stride  = p_pic->p[i_plane].i_pitch;
407         i_src_stride  = i_plane ? yuv->uv_stride : yuv->y_stride;
408         i_src_xoffset = p_dec->p_sys->ti.offset_x;
409         i_src_yoffset = p_dec->p_sys->ti.offset_y;
410         if( i_plane )
411         {
412             i_src_xoffset /= 2;
413             i_src_yoffset /= 2;
414         }
415
416         p_src += (i_src_yoffset * i_src_stride + i_src_yoffset);
417
418         for( i_line = 0; i_line < p_pic->p[i_plane].i_lines; i_line++ )
419         {
420             p_dec->p_vlc->pf_memcpy( p_dst, p_src, i_width );
421             p_src += i_src_stride;
422             p_dst += i_dst_stride;
423         }
424     }
425 }
426
427 /*****************************************************************************
428  * encoder_sys_t : theora encoder descriptor
429  *****************************************************************************/
430 struct encoder_sys_t
431 {
432     /*
433      * Input properties
434      */
435     vlc_bool_t b_headers;
436
437     /*
438      * Theora properties
439      */
440     theora_info      ti;                        /* theora bitstream settings */
441     theora_comment   tc;                            /* theora comment header */
442     theora_state     td;                   /* theora bitstream user comments */
443
444     /*
445      * Common properties
446      */
447     mtime_t i_pts;
448 };
449
450 /*****************************************************************************
451  * OpenEncoder: probe the encoder and return score
452  *****************************************************************************/
453 static int OpenEncoder( vlc_object_t *p_this )
454 {
455     encoder_t *p_enc = (encoder_t *)p_this;
456     encoder_sys_t *p_sys = p_enc->p_sys;
457     vlc_value_t val;
458     int i_quality;
459
460     if( p_enc->fmt_out.i_codec != VLC_FOURCC('t','h','e','o') &&
461         !p_enc->b_force )
462     {
463         return VLC_EGENERIC;
464     }
465
466     if( p_enc->fmt_in.video.i_width % 16 ||
467         p_enc->fmt_in.video.i_height % 16 )
468     {
469         msg_Err( p_enc, "Theora video encoding requires dimensions which are "
470                  "multiples of 16. Which is not the case here (%dx%d).",
471                  p_enc->fmt_in.video.i_width, p_enc->fmt_in.video.i_height );
472         return VLC_EGENERIC;
473     }
474
475     /* Allocate the memory needed to store the decoder's structure */
476     if( ( p_sys = (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
477     {
478         msg_Err( p_enc, "out of memory" );
479         return VLC_EGENERIC;
480     }
481     p_enc->p_sys = p_sys;
482
483     p_enc->pf_header = Headers;
484     p_enc->pf_encode_video = Encode;
485     p_enc->fmt_in.i_codec = VLC_FOURCC('I','4','2','0');
486     p_enc->fmt_out.i_codec = VLC_FOURCC('t','h','e','o');
487
488     sout_ParseCfg( p_enc, ENC_CFG_PREFIX, ppsz_enc_options, p_enc->p_cfg );
489
490     var_Get( p_enc, ENC_CFG_PREFIX "quality", &val );
491     i_quality = val.i_int;
492     if( i_quality > 10 ) i_quality = 10;
493     if( i_quality < 0 ) i_quality = 0;
494
495 #define frame_x_offset 0
496 #define frame_y_offset 0
497 #define video_hzn 25
498 #define video_hzd 1
499 #define video_q 5
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 = frame_x_offset;
508     p_sys->ti.offset_y = frame_y_offset;
509     p_sys->ti.fps_numerator = video_hzn;
510     p_sys->ti.fps_denominator = video_hzd;
511
512     if( p_enc->fmt_in.video.i_aspect )
513     {
514         p_sys->ti.aspect_numerator = p_enc->fmt_in.video.i_aspect;
515         p_sys->ti.aspect_denominator = VOUT_ASPECT_FACTOR;
516     }
517     else
518     {
519         p_sys->ti.aspect_numerator = 4;
520         p_sys->ti.aspect_denominator = 3;
521     }
522
523     p_sys->ti.target_bitrate = p_enc->fmt_out.i_bitrate;
524     p_sys->ti.quality = ((float)i_quality) * 6.3;
525
526     p_sys->ti.dropframes_p = 0;
527     p_sys->ti.quick_p = 1;
528     p_sys->ti.keyframe_auto_p = 1;
529     p_sys->ti.keyframe_frequency = 64;
530     p_sys->ti.keyframe_frequency_force = 64;
531     p_sys->ti.keyframe_data_target_bitrate = p_enc->fmt_out.i_bitrate * 1.5;
532     p_sys->ti.keyframe_auto_threshold = 80;
533     p_sys->ti.keyframe_mindistance = 8;
534     p_sys->ti.noise_sensitivity = 1;
535
536     theora_encode_init( &p_sys->td, &p_sys->ti );
537     theora_info_clear( &p_sys->ti );
538     theora_comment_init( &p_sys->tc );
539
540     p_sys->b_headers = VLC_FALSE;
541
542     return VLC_SUCCESS;
543 }
544
545 /****************************************************************************
546  * Encode: the whole thing
547  ****************************************************************************
548  * This function spits out ogg packets.
549  ****************************************************************************/
550 static block_t *Headers( encoder_t *p_enc )
551 {
552     encoder_sys_t *p_sys = p_enc->p_sys;
553     block_t *p_chain = NULL;
554
555     /* Create theora headers */
556     if( !p_sys->b_headers )
557     {
558         ogg_packet oggpackets;
559         int i;
560         block_t *p_block;
561
562         /* Ogg packet to block */
563         for( i = 0; i < 3; i++ )
564         {
565             switch( i )
566             {
567             case 0:
568                 theora_encode_header( &p_sys->td, &oggpackets );
569                 break;
570             case 1:
571                 theora_encode_comment( &p_sys->tc, &oggpackets );
572                 break;
573             case 2:
574                 theora_encode_tables( &p_sys->td, &oggpackets );
575                 break;
576             }
577
578             p_block = block_New( p_enc, oggpackets.bytes );
579             memcpy( p_block->p_buffer, oggpackets.packet, oggpackets.bytes );
580             p_block->i_dts = p_block->i_pts = p_block->i_length = 0;
581             block_ChainAppend( &p_chain, p_block );
582         }
583
584         p_sys->b_headers = VLC_TRUE;
585     }
586
587     return p_chain;
588 }
589
590 /****************************************************************************
591  * Encode: the whole thing
592  ****************************************************************************
593  * This function spits out ogg packets.
594  ****************************************************************************/
595 static block_t *Encode( encoder_t *p_enc, picture_t *p_pict )
596 {
597     encoder_sys_t *p_sys = p_enc->p_sys;
598     ogg_packet oggpacket;
599     block_t *p_block;
600     yuv_buffer yuv;
601
602     /* Theora is a one-frame-in, one-frame-out system. Submit a frame
603      * for compression and pull out the packet. */
604
605     yuv.y_width  = p_pict->p[0].i_visible_pitch;
606     yuv.y_height = p_pict->p[0].i_lines;
607     yuv.y_stride = p_pict->p[0].i_pitch;
608
609     yuv.uv_width  = p_pict->p[1].i_visible_pitch;
610     yuv.uv_height = p_pict->p[1].i_lines;
611     yuv.uv_stride = p_pict->p[1].i_pitch;
612
613     yuv.y = p_pict->p[0].p_pixels;
614     yuv.u = p_pict->p[1].p_pixels;
615     yuv.v = p_pict->p[2].p_pixels;
616
617     if( theora_encode_YUVin( &p_sys->td, &yuv ) < 0 )
618     {
619         msg_Warn( p_enc, "failed encoding a frame" );
620         return NULL;
621     }
622
623     theora_encode_packetout( &p_sys->td, 0, &oggpacket );
624
625     /* Ogg packet to block */
626     p_block = block_New( p_enc, oggpacket.bytes );
627     memcpy( p_block->p_buffer, oggpacket.packet, oggpacket.bytes );
628     p_block->i_dts = p_block->i_pts = p_pict->date;;
629
630     return p_block;
631 }
632
633 /*****************************************************************************
634  * CloseEncoder: theora encoder destruction
635  *****************************************************************************/
636 static void CloseEncoder( vlc_object_t *p_this )
637 {
638     encoder_t *p_enc = (encoder_t *)p_this;
639     encoder_sys_t *p_sys = p_enc->p_sys;
640
641     theora_info_clear( &p_sys->ti );
642     theora_comment_clear( &p_sys->tc );
643
644     free( p_sys );
645 }