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