1 /*****************************************************************************
2 * theora.c: theora decoder module making use of libtheora.
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VideoLAN
7 * Authors: Gildas Bazin <gbazin@videolan.org>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
28 #include <vlc/decoder.h>
33 #include <theora/theora.h>
35 /*****************************************************************************
36 * decoder_sys_t : theora decoder descriptor
37 *****************************************************************************/
41 vlc_bool_t b_packetizer;
51 theora_info ti; /* theora bitstream settings */
52 theora_comment tc; /* theora comment header */
53 theora_state td; /* theora bitstream user comments */
61 /*****************************************************************************
63 *****************************************************************************/
64 static int OpenDecoder ( vlc_object_t * );
65 static int OpenPacketizer( vlc_object_t * );
66 static void CloseDecoder ( vlc_object_t * );
68 static void *DecodeBlock ( decoder_t *, block_t ** );
69 static void *ProcessPacket ( decoder_t *, ogg_packet *, block_t ** );
71 static picture_t *DecodePacket( decoder_t *, ogg_packet * );
73 static void ParseTheoraComments( decoder_t * );
74 static void theora_CopyPicture( decoder_t *, picture_t *, yuv_buffer * );
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 );
81 /*****************************************************************************
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." )
90 set_description( _("Theora video decoder") );
91 set_capability( "decoder", 100 );
92 set_callbacks( OpenDecoder, CloseDecoder );
93 add_shortcut( "theora" );
96 set_description( _("Theora video packetizer") );
97 set_capability( "packetizer", 100 );
98 set_callbacks( OpenPacketizer, CloseDecoder );
99 add_shortcut( "theora" );
102 set_description( _("Theora video encoder") );
103 set_capability( "encoder", 100 );
104 set_callbacks( OpenEncoder, CloseEncoder );
105 add_shortcut( "theora" );
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 );
112 static const char *ppsz_enc_options[] = {
116 /*****************************************************************************
117 * OpenDecoder: probe the decoder and return score
118 *****************************************************************************/
119 static int OpenDecoder( vlc_object_t *p_this )
121 decoder_t *p_dec = (decoder_t*)p_this;
122 decoder_sys_t *p_sys;
124 if( p_dec->fmt_in.i_codec != VLC_FOURCC('t','h','e','o') )
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 )
133 msg_Err( p_dec, "out of memory" );
136 p_dec->p_sys->b_packetizer = VLC_FALSE;
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');
145 p_dec->pf_decode_video = (picture_t *(*)(decoder_t *, block_t **))
147 p_dec->pf_packetize = (block_t *(*)(decoder_t *, block_t **))
150 /* Init supporting Theora structures needed in header parsing */
151 theora_comment_init( &p_sys->tc );
152 theora_info_init( &p_sys->ti );
154 p_sys->i_headers = 0;
159 static int OpenPacketizer( vlc_object_t *p_this )
161 decoder_t *p_dec = (decoder_t*)p_this;
163 int i_ret = OpenDecoder( p_this );
165 if( i_ret == VLC_SUCCESS )
167 p_dec->p_sys->b_packetizer = VLC_TRUE;
168 p_dec->fmt_out.i_codec = VLC_FOURCC( 't', 'h', 'e', 'o' );
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 )
181 decoder_sys_t *p_sys = p_dec->p_sys;
183 ogg_packet oggpacket;
185 if( !pp_block || !*pp_block ) return NULL;
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;
195 oggpacket.packetno = 0;
197 if( p_sys->i_headers == 0 )
199 /* Take care of the initial Theora header */
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 )
204 msg_Err( p_dec, "This bitstream does not contain Theora "
206 block_Release( p_block );
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;
215 if( p_sys->ti.aspect_denominator && p_sys->ti.aspect_numerator )
216 p_dec->fmt_out.video.i_aspect = ((int64_t)VOUT_ASPECT_FACTOR) *
217 p_sys->ti.aspect_numerator / p_sys->ti.aspect_denominator;
219 p_dec->fmt_out.video.i_aspect = VOUT_ASPECT_FACTOR *
220 p_sys->ti.frame_width / p_sys->ti.frame_height;
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 );
229 return ProcessPacket( p_dec, &oggpacket, pp_block );
232 if( p_sys->i_headers == 1 )
234 /* The next packet in order is the comments header */
235 if( theora_decode_header( &p_sys->ti, &p_sys->tc, &oggpacket ) < 0 )
237 msg_Err( p_dec, "2nd Theora header is corrupted." );
242 ParseTheoraComments( p_dec );
244 return ProcessPacket( p_dec, &oggpacket, pp_block );
247 if( p_sys->i_headers == 2 )
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 )
254 msg_Err( p_dec, "3rd Theora header is corrupted." );
259 if( !p_sys->b_packetizer )
261 /* We have all the headers, initialize decoder */
262 theora_decode_init( &p_sys->td, &p_sys->ti );
265 return ProcessPacket( p_dec, &oggpacket, pp_block );
268 return ProcessPacket( p_dec, &oggpacket, pp_block );
271 /*****************************************************************************
272 * ProcessPacket: processes a theora packet.
273 *****************************************************************************/
274 static void *ProcessPacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
277 decoder_sys_t *p_sys = p_dec->p_sys;
278 block_t *p_block = *pp_block;
281 /* Date management */
282 if( p_block->i_pts > 0 && p_block->i_pts != p_sys->i_pts )
284 p_sys->i_pts = p_block->i_pts;
287 if( p_sys->b_packetizer )
289 /* Date management */
290 p_block->i_dts = p_block->i_pts = p_sys->i_pts;
292 if( p_sys->i_headers >= 3 )
293 p_block->i_length = p_sys->i_pts - p_block->i_pts;
295 p_block->i_length = 0;
301 if( p_sys->i_headers >= 3 )
302 p_buf = DecodePacket( p_dec, p_oggpacket );
308 block_Release( p_block );
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 */
320 /*****************************************************************************
321 * DecodePacket: decodes a Theora packet.
322 *****************************************************************************/
323 static picture_t *DecodePacket( decoder_t *p_dec, ogg_packet *p_oggpacket )
325 decoder_sys_t *p_sys = p_dec->p_sys;
329 theora_decode_packetin( &p_sys->td, p_oggpacket );
332 theora_decode_YUVout( &p_sys->td, &yuv );
334 /* Get a new picture */
335 p_pic = p_dec->pf_vout_buffer_new( p_dec );
336 if( !p_pic ) return NULL;
338 theora_CopyPicture( p_dec, p_pic, &yuv );
340 p_pic->date = p_sys->i_pts;
345 /*****************************************************************************
346 * ParseTheoraComments: FIXME should be done in demuxer
347 *****************************************************************************/
348 static void ParseTheoraComments( decoder_t *p_dec )
350 input_thread_t *p_input = (input_thread_t *)p_dec->p_parent;
353 char *psz_name, *psz_value, *psz_comment;
354 while ( i < p_dec->p_sys->tc.comments )
356 psz_comment = strdup( p_dec->p_sys->tc.user_comments[i] );
359 msg_Warn( p_dec, "out of memory" );
362 psz_name = psz_comment;
363 psz_value = strchr( psz_comment, '=' );
368 input_Control( p_input, INPUT_ADD_INFO, _("Theora comment"),
369 psz_name, psz_value );
376 /*****************************************************************************
377 * CloseDecoder: theora decoder destruction
378 *****************************************************************************/
379 static void CloseDecoder( vlc_object_t *p_this )
381 decoder_t *p_dec = (decoder_t *)p_this;
382 decoder_sys_t *p_sys = p_dec->p_sys;
384 theora_info_clear( &p_sys->ti );
385 theora_comment_clear( &p_sys->tc );
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,
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;
401 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
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;
416 p_src += (i_src_yoffset * i_src_stride + i_src_yoffset);
418 for( i_line = 0; i_line < p_pic->p[i_plane].i_lines; i_line++ )
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;
427 /*****************************************************************************
428 * encoder_sys_t : theora encoder descriptor
429 *****************************************************************************/
435 vlc_bool_t b_headers;
440 theora_info ti; /* theora bitstream settings */
441 theora_comment tc; /* theora comment header */
442 theora_state td; /* theora bitstream user comments */
450 /*****************************************************************************
451 * OpenEncoder: probe the encoder and return score
452 *****************************************************************************/
453 static int OpenEncoder( vlc_object_t *p_this )
455 encoder_t *p_enc = (encoder_t *)p_this;
456 encoder_sys_t *p_sys = p_enc->p_sys;
460 if( p_enc->fmt_out.i_codec != VLC_FOURCC('t','h','e','o') &&
466 if( p_enc->fmt_in.video.i_width % 16 ||
467 p_enc->fmt_in.video.i_height % 16 )
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 );
475 /* Allocate the memory needed to store the decoder's structure */
476 if( ( p_sys = (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
478 msg_Err( p_enc, "out of memory" );
481 p_enc->p_sys = p_sys;
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');
488 sout_ParseCfg( p_enc, ENC_CFG_PREFIX, ppsz_enc_options, p_enc->p_cfg );
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;
495 theora_info_init( &p_sys->ti );
497 p_sys->ti.width = p_enc->fmt_in.video.i_width;
498 p_sys->ti.height = p_enc->fmt_in.video.i_height;
499 p_sys->ti.frame_width = p_enc->fmt_in.video.i_width;
500 p_sys->ti.frame_height = p_enc->fmt_in.video.i_height;
501 p_sys->ti.offset_x = 0 /*frame_x_offset*/;
502 p_sys->ti.offset_y = 0/*frame_y_offset*/;
504 if( !p_enc->fmt_in.video.i_frame_rate ||
505 !p_enc->fmt_in.video.i_frame_rate_base )
507 p_sys->ti.fps_numerator = 25;
508 p_sys->ti.fps_denominator = 1;
512 p_sys->ti.fps_numerator = p_enc->fmt_in.video.i_frame_rate;
513 p_sys->ti.fps_denominator = p_enc->fmt_in.video.i_frame_rate_base;
516 if( p_enc->fmt_in.video.i_aspect )
518 p_sys->ti.aspect_numerator = p_enc->fmt_in.video.i_aspect;
519 p_sys->ti.aspect_denominator = VOUT_ASPECT_FACTOR;
523 p_sys->ti.aspect_numerator = 4;
524 p_sys->ti.aspect_denominator = 3;
527 p_sys->ti.target_bitrate = p_enc->fmt_out.i_bitrate;
528 p_sys->ti.quality = ((float)i_quality) * 6.3;
530 p_sys->ti.dropframes_p = 0;
531 p_sys->ti.quick_p = 1;
532 p_sys->ti.keyframe_auto_p = 1;
533 p_sys->ti.keyframe_frequency = 64;
534 p_sys->ti.keyframe_frequency_force = 64;
535 p_sys->ti.keyframe_data_target_bitrate = p_enc->fmt_out.i_bitrate * 1.5;
536 p_sys->ti.keyframe_auto_threshold = 80;
537 p_sys->ti.keyframe_mindistance = 8;
538 p_sys->ti.noise_sensitivity = 1;
540 theora_encode_init( &p_sys->td, &p_sys->ti );
541 theora_info_clear( &p_sys->ti );
542 theora_comment_init( &p_sys->tc );
544 p_sys->b_headers = VLC_FALSE;
549 /****************************************************************************
550 * Encode: the whole thing
551 ****************************************************************************
552 * This function spits out ogg packets.
553 ****************************************************************************/
554 static block_t *Headers( encoder_t *p_enc )
556 encoder_sys_t *p_sys = p_enc->p_sys;
557 block_t *p_chain = NULL;
559 /* Create theora headers */
560 if( !p_sys->b_headers )
562 ogg_packet oggpackets;
566 /* Ogg packet to block */
567 for( i = 0; i < 3; i++ )
572 theora_encode_header( &p_sys->td, &oggpackets );
575 theora_encode_comment( &p_sys->tc, &oggpackets );
578 theora_encode_tables( &p_sys->td, &oggpackets );
582 p_block = block_New( p_enc, oggpackets.bytes );
583 memcpy( p_block->p_buffer, oggpackets.packet, oggpackets.bytes );
584 p_block->i_dts = p_block->i_pts = p_block->i_length = 0;
585 block_ChainAppend( &p_chain, p_block );
588 p_sys->b_headers = VLC_TRUE;
594 /****************************************************************************
595 * Encode: the whole thing
596 ****************************************************************************
597 * This function spits out ogg packets.
598 ****************************************************************************/
599 static block_t *Encode( encoder_t *p_enc, picture_t *p_pict )
601 encoder_sys_t *p_sys = p_enc->p_sys;
602 ogg_packet oggpacket;
606 /* Theora is a one-frame-in, one-frame-out system. Submit a frame
607 * for compression and pull out the packet. */
609 yuv.y_width = p_pict->p[0].i_visible_pitch;
610 yuv.y_height = p_pict->p[0].i_lines;
611 yuv.y_stride = p_pict->p[0].i_pitch;
613 yuv.uv_width = p_pict->p[1].i_visible_pitch;
614 yuv.uv_height = p_pict->p[1].i_lines;
615 yuv.uv_stride = p_pict->p[1].i_pitch;
617 yuv.y = p_pict->p[0].p_pixels;
618 yuv.u = p_pict->p[1].p_pixels;
619 yuv.v = p_pict->p[2].p_pixels;
621 if( theora_encode_YUVin( &p_sys->td, &yuv ) < 0 )
623 msg_Warn( p_enc, "failed encoding a frame" );
627 theora_encode_packetout( &p_sys->td, 0, &oggpacket );
629 /* Ogg packet to block */
630 p_block = block_New( p_enc, oggpacket.bytes );
631 memcpy( p_block->p_buffer, oggpacket.packet, oggpacket.bytes );
632 p_block->i_dts = p_block->i_pts = p_pict->date;
637 /*****************************************************************************
638 * CloseEncoder: theora encoder destruction
639 *****************************************************************************/
640 static void CloseEncoder( vlc_object_t *p_this )
642 encoder_t *p_enc = (encoder_t *)p_this;
643 encoder_sys_t *p_sys = p_enc->p_sys;
645 theora_info_clear( &p_sys->ti );
646 theora_comment_clear( &p_sys->tc );