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