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