1 /*****************************************************************************
2 * theora.c: theora decoder module making use of libtheora.
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VideoLAN
5 * $Id: theora.c,v 1.24 2004/01/29 17:51:07 zorglub Exp $
7 * Authors: Gildas Bazin <gbazin@netcourrier.com>
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>
29 #include "input_ext-plugins.h"
30 #include "vlc_playlist.h"
34 #include <theora/theora.h>
36 /*****************************************************************************
37 * decoder_sys_t : theora decoder descriptor
38 *****************************************************************************/
42 vlc_bool_t b_packetizer;
52 theora_info ti; /* theora bitstream settings */
53 theora_comment tc; /* theora comment header */
54 theora_state td; /* theora bitstream user comments */
62 /*****************************************************************************
64 *****************************************************************************/
65 static int OpenDecoder ( vlc_object_t * );
66 static int OpenPacketizer( vlc_object_t * );
67 static void CloseDecoder ( vlc_object_t * );
69 static void *DecodeBlock ( decoder_t *, block_t ** );
70 static void *ProcessPacket ( decoder_t *, ogg_packet *, block_t ** );
72 static picture_t *DecodePacket( decoder_t *, ogg_packet * );
74 static void ParseTheoraComments( decoder_t * );
75 static void theora_CopyPicture( decoder_t *, picture_t *, yuv_buffer * );
77 static int OpenEncoder( vlc_object_t *p_this );
78 static void CloseEncoder( vlc_object_t *p_this );
79 static block_t *Headers( encoder_t *p_enc );
80 static block_t *Encode( encoder_t *p_enc, picture_t *p_pict );
82 /*****************************************************************************
84 *****************************************************************************/
86 set_description( _("Theora video decoder") );
87 set_capability( "decoder", 100 );
88 set_callbacks( OpenDecoder, CloseDecoder );
89 add_shortcut( "theora" );
92 set_description( _("Theora video packetizer") );
93 set_capability( "packetizer", 100 );
94 set_callbacks( OpenPacketizer, CloseDecoder );
95 add_shortcut( "theora" );
98 set_description( _("Theora video encoder") );
99 set_capability( "encoder", 100 );
100 set_callbacks( OpenEncoder, CloseEncoder );
101 add_shortcut( "theora" );
104 /*****************************************************************************
105 * OpenDecoder: probe the decoder and return score
106 *****************************************************************************/
107 static int OpenDecoder( vlc_object_t *p_this )
109 decoder_t *p_dec = (decoder_t*)p_this;
110 decoder_sys_t *p_sys;
112 if( p_dec->fmt_in.i_codec != VLC_FOURCC('t','h','e','o') )
117 /* Allocate the memory needed to store the decoder's structure */
118 if( ( p_dec->p_sys = p_sys =
119 (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
121 msg_Err( p_dec, "out of memory" );
124 p_dec->p_sys->b_packetizer = VLC_FALSE;
128 /* Set output properties */
129 p_dec->fmt_out.i_cat = VIDEO_ES;
130 p_dec->fmt_out.i_codec = VLC_FOURCC('I','4','2','0');
133 p_dec->pf_decode_video = (picture_t *(*)(decoder_t *, block_t **))
135 p_dec->pf_packetize = (block_t *(*)(decoder_t *, block_t **))
138 /* Init supporting Theora structures needed in header parsing */
139 theora_comment_init( &p_sys->tc );
140 theora_info_init( &p_sys->ti );
142 p_sys->i_headers = 0;
147 static int OpenPacketizer( vlc_object_t *p_this )
149 decoder_t *p_dec = (decoder_t*)p_this;
151 int i_ret = OpenDecoder( p_this );
153 if( i_ret == VLC_SUCCESS )
155 p_dec->p_sys->b_packetizer = VLC_TRUE;
156 p_dec->fmt_out.i_codec = VLC_FOURCC( 't', 'h', 'e', 'o' );
162 /****************************************************************************
163 * DecodeBlock: the whole thing
164 ****************************************************************************
165 * This function must be fed with ogg packets.
166 ****************************************************************************/
167 static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
169 decoder_sys_t *p_sys = p_dec->p_sys;
171 ogg_packet oggpacket;
173 if( !pp_block || !*pp_block ) return NULL;
177 /* Block to Ogg packet */
178 oggpacket.packet = p_block->p_buffer;
179 oggpacket.bytes = p_block->i_buffer;
180 oggpacket.granulepos = p_block->i_dts;
183 oggpacket.packetno = 0;
185 if( p_sys->i_headers == 0 )
187 /* Take care of the initial Theora header */
189 oggpacket.b_o_s = 1; /* yes this actually is a b_o_s packet :) */
190 if( theora_decode_header( &p_sys->ti, &p_sys->tc, &oggpacket ) < 0 )
192 msg_Err( p_dec, "This bitstream does not contain Theora "
194 block_Release( p_block );
199 /* Set output properties */
200 p_dec->fmt_out.video.i_width = p_sys->ti.width;
201 p_dec->fmt_out.video.i_height = p_sys->ti.height;
203 if( p_sys->ti.aspect_denominator )
204 p_dec->fmt_out.video.i_aspect = ((int64_t)VOUT_ASPECT_FACTOR) *
205 p_sys->ti.aspect_numerator / p_sys->ti.aspect_denominator;
207 p_dec->fmt_out.video.i_aspect = VOUT_ASPECT_FACTOR *
208 p_sys->ti.frame_width / p_sys->ti.frame_height;
210 msg_Dbg( p_dec, "%dx%d %.02f fps video, frame content "
211 "is %dx%d with offset (%d,%d).",
212 p_sys->ti.width, p_sys->ti.height,
213 (double)p_sys->ti.fps_numerator/p_sys->ti.fps_denominator,
214 p_sys->ti.frame_width, p_sys->ti.frame_height,
215 p_sys->ti.offset_x, p_sys->ti.offset_y );
217 return ProcessPacket( p_dec, &oggpacket, pp_block );
220 if( p_sys->i_headers == 1 )
222 /* The next packet in order is the comments header */
223 if( theora_decode_header( &p_sys->ti, &p_sys->tc, &oggpacket ) < 0 )
225 msg_Err( p_dec, "2nd Theora header is corrupted." );
230 ParseTheoraComments( p_dec );
232 return ProcessPacket( p_dec, &oggpacket, pp_block );
235 if( p_sys->i_headers == 2 )
237 /* The next packet in order is the codebooks header
238 We need to watch out that this packet is not missing as a
239 missing or corrupted header is fatal. */
240 if( theora_decode_header( &p_sys->ti, &p_sys->tc, &oggpacket ) < 0 )
242 msg_Err( p_dec, "3rd Theora header is corrupted." );
247 if( !p_sys->b_packetizer )
249 /* We have all the headers, initialize decoder */
250 theora_decode_init( &p_sys->td, &p_sys->ti );
253 return ProcessPacket( p_dec, &oggpacket, pp_block );
256 return ProcessPacket( p_dec, &oggpacket, pp_block );
259 /*****************************************************************************
260 * ProcessPacket: processes a theora packet.
261 *****************************************************************************/
262 static void *ProcessPacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
265 decoder_sys_t *p_sys = p_dec->p_sys;
266 block_t *p_block = *pp_block;
269 /* Date management */
270 if( p_block->i_pts > 0 && p_block->i_pts != p_sys->i_pts )
272 p_sys->i_pts = p_block->i_pts;
275 if( p_sys->b_packetizer )
277 /* Date management */
278 p_block->i_dts = p_block->i_pts = p_sys->i_pts;
280 if( p_sys->i_headers >= 3 )
281 p_block->i_length = p_sys->i_pts - p_block->i_pts;
283 p_block->i_length = 0;
289 if( p_sys->i_headers >= 3 )
290 p_buf = DecodePacket( p_dec, p_oggpacket );
296 block_Release( p_block );
301 /* Date management */
302 p_sys->i_pts += ( I64C(1000000) * p_sys->ti.fps_denominator /
303 p_sys->ti.fps_numerator ); /* 1 frame per packet */
308 /*****************************************************************************
309 * DecodePacket: decodes a Theora packet.
310 *****************************************************************************/
311 static picture_t *DecodePacket( decoder_t *p_dec, ogg_packet *p_oggpacket )
313 decoder_sys_t *p_sys = p_dec->p_sys;
317 theora_decode_packetin( &p_sys->td, p_oggpacket );
320 theora_decode_YUVout( &p_sys->td, &yuv );
322 /* Get a new picture */
323 p_pic = p_dec->pf_vout_buffer_new( p_dec );
324 if( !p_pic ) return NULL;
326 theora_CopyPicture( p_dec, p_pic, &yuv );
328 p_pic->date = p_sys->i_pts;
333 /*****************************************************************************
334 * ParseTheoraComments: FIXME should be done in demuxer
335 *****************************************************************************/
336 static void ParseTheoraComments( decoder_t *p_dec )
338 input_thread_t *p_input = (input_thread_t *)p_dec->p_parent;
339 input_info_category_t *p_cat =
340 input_InfoCategory( p_input, _("Theora comment") );
341 playlist_t *p_playlist = vlc_object_find( p_dec, VLC_OBJECT_PLAYLIST,
343 playlist_item_t *p_item;
345 char *psz_name, *psz_value, *psz_comment;
346 while ( i < p_dec->p_sys->tc.comments )
348 psz_comment = strdup( p_dec->p_sys->tc.user_comments[i] );
351 msg_Warn( p_dec, "out of memory" );
354 psz_name = psz_comment;
355 psz_value = strchr( psz_comment, '=' );
360 input_AddInfo( p_cat, psz_name, psz_value );
361 vlc_mutex_lock( &p_playlist->object_lock );
362 p_item = playlist_ItemGetByPos( p_playlist, -1 );
363 vlc_mutex_unlock( &p_playlist->object_lock );
366 msg_Err(p_dec, "unable to find item" );
369 vlc_mutex_lock( &p_item->lock );
370 playlist_ItemAddInfo( p_item, _("Theora comment") ,
371 psz_name, psz_value );
372 vlc_mutex_unlock( &p_item->lock );
377 if( p_playlist) vlc_object_release( p_playlist );
380 /*****************************************************************************
381 * CloseDecoder: theora decoder destruction
382 *****************************************************************************/
383 static void CloseDecoder( vlc_object_t *p_this )
385 decoder_t *p_dec = (decoder_t *)p_this;
386 decoder_sys_t *p_sys = p_dec->p_sys;
388 theora_info_clear( &p_sys->ti );
389 theora_comment_clear( &p_sys->tc );
394 /*****************************************************************************
395 * theora_CopyPicture: copy a picture from theora internal buffers to a
396 * picture_t structure.
397 *****************************************************************************/
398 static void theora_CopyPicture( decoder_t *p_dec, picture_t *p_pic,
401 int i_plane, i_line, i_width, i_dst_stride, i_src_stride;
402 int i_src_xoffset, i_src_yoffset;
403 uint8_t *p_dst, *p_src;
405 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
407 p_dst = p_pic->p[i_plane].p_pixels;
408 p_src = i_plane ? (i_plane - 1 ? yuv->v : yuv->u ) : yuv->y;
409 i_width = p_pic->p[i_plane].i_visible_pitch;
410 i_dst_stride = p_pic->p[i_plane].i_pitch;
411 i_src_stride = i_plane ? yuv->uv_stride : yuv->y_stride;
412 i_src_xoffset = p_dec->p_sys->ti.offset_x;
413 i_src_yoffset = p_dec->p_sys->ti.offset_y;
420 p_src += (i_src_yoffset * i_src_stride + i_src_yoffset);
422 for( i_line = 0; i_line < p_pic->p[i_plane].i_lines; i_line++ )
424 p_dec->p_vlc->pf_memcpy( p_dst, p_src, i_width );
425 p_src += i_src_stride;
426 p_dst += i_dst_stride;
431 /*****************************************************************************
432 * encoder_sys_t : theora encoder descriptor
433 *****************************************************************************/
439 vlc_bool_t b_headers;
444 theora_info ti; /* theora bitstream settings */
445 theora_comment tc; /* theora comment header */
446 theora_state td; /* theora bitstream user comments */
454 /*****************************************************************************
455 * OpenEncoder: probe the encoder and return score
456 *****************************************************************************/
457 static int OpenEncoder( vlc_object_t *p_this )
459 encoder_t *p_enc = (encoder_t *)p_this;
460 encoder_sys_t *p_sys = p_enc->p_sys;
462 if( p_enc->fmt_out.i_codec != VLC_FOURCC('t','h','e','o') )
467 if( p_enc->fmt_in.video.i_width % 16 ||
468 p_enc->fmt_in.video.i_height % 16 )
470 msg_Err( p_enc, "Theora video encoding requires dimensions which are "
471 "multiples of 16. Which is not the case here (%dx%d).",
472 p_enc->fmt_in.video.i_width, p_enc->fmt_in.video.i_height );
476 /* Allocate the memory needed to store the decoder's structure */
477 if( ( p_sys = (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
479 msg_Err( p_enc, "out of memory" );
482 p_enc->p_sys = p_sys;
484 p_enc->pf_header = Headers;
485 p_enc->pf_encode_video = Encode;
486 p_enc->fmt_in.i_codec = VLC_FOURCC('I','4','2','0');
488 #define frame_x_offset 0
489 #define frame_y_offset 0
494 theora_info_init( &p_sys->ti );
496 p_sys->ti.width = p_enc->fmt_in.video.i_width;
497 p_sys->ti.height = p_enc->fmt_in.video.i_height;
498 p_sys->ti.frame_width = p_enc->fmt_in.video.i_width;
499 p_sys->ti.frame_height = p_enc->fmt_in.video.i_height;
500 p_sys->ti.offset_x = frame_x_offset;
501 p_sys->ti.offset_y = frame_y_offset;
502 p_sys->ti.fps_numerator = video_hzn;
503 p_sys->ti.fps_denominator = video_hzd;
505 if( p_enc->fmt_in.video.i_aspect )
507 p_sys->ti.aspect_numerator = p_enc->fmt_in.video.i_aspect;
508 p_sys->ti.aspect_denominator = VOUT_ASPECT_FACTOR;
512 p_sys->ti.aspect_numerator = 4;
513 p_sys->ti.aspect_denominator = 3;
516 p_sys->ti.target_bitrate = p_enc->fmt_out.i_bitrate;
517 p_sys->ti.quality = video_q;
519 p_sys->ti.dropframes_p = 0;
520 p_sys->ti.quick_p = 1;
521 p_sys->ti.keyframe_auto_p = 1;
522 p_sys->ti.keyframe_frequency = 64;
523 p_sys->ti.keyframe_frequency_force = 64;
524 p_sys->ti.keyframe_data_target_bitrate = p_enc->fmt_out.i_bitrate * 1.5;
525 p_sys->ti.keyframe_auto_threshold = 80;
526 p_sys->ti.keyframe_mindistance = 8;
527 p_sys->ti.noise_sensitivity = 1;
529 theora_encode_init( &p_sys->td, &p_sys->ti );
530 theora_info_clear( &p_sys->ti );
531 theora_comment_init( &p_sys->tc );
533 p_sys->b_headers = VLC_FALSE;
538 /****************************************************************************
539 * Encode: the whole thing
540 ****************************************************************************
541 * This function spits out ogg packets.
542 ****************************************************************************/
543 static block_t *Headers( encoder_t *p_enc )
545 encoder_sys_t *p_sys = p_enc->p_sys;
546 block_t *p_chain = NULL;
548 /* Create theora headers */
549 if( !p_sys->b_headers )
551 ogg_packet oggpackets;
555 /* Ogg packet to block */
556 for( i = 0; i < 3; i++ )
561 theora_encode_header( &p_sys->td, &oggpackets );
564 theora_encode_comment( &p_sys->tc, &oggpackets );
567 theora_encode_tables( &p_sys->td, &oggpackets );
571 p_block = block_New( p_enc, oggpackets.bytes );
572 memcpy( p_block->p_buffer, oggpackets.packet, oggpackets.bytes );
573 p_block->i_dts = p_block->i_pts = p_block->i_length = 0;
574 block_ChainAppend( &p_chain, p_block );
577 p_sys->b_headers = VLC_TRUE;
583 /****************************************************************************
584 * Encode: the whole thing
585 ****************************************************************************
586 * This function spits out ogg packets.
587 ****************************************************************************/
588 static block_t *Encode( encoder_t *p_enc, picture_t *p_pict )
590 encoder_sys_t *p_sys = p_enc->p_sys;
591 ogg_packet oggpacket;
595 /* Theora is a one-frame-in, one-frame-out system. Submit a frame
596 * for compression and pull out the packet. */
598 yuv.y_width = p_pict->p[0].i_visible_pitch;
599 yuv.y_height = p_pict->p[0].i_lines;
600 yuv.y_stride = p_pict->p[0].i_pitch;
602 yuv.uv_width = p_pict->p[1].i_visible_pitch;
603 yuv.uv_height = p_pict->p[1].i_lines;
604 yuv.uv_stride = p_pict->p[1].i_pitch;
606 yuv.y = p_pict->p[0].p_pixels;
607 yuv.u = p_pict->p[1].p_pixels;
608 yuv.v = p_pict->p[2].p_pixels;
610 if( theora_encode_YUVin( &p_sys->td, &yuv ) < 0 )
612 msg_Warn( p_enc, "failed encoding a frame" );
616 theora_encode_packetout( &p_sys->td, 0, &oggpacket );
618 /* Ogg packet to block */
619 p_block = block_New( p_enc, oggpacket.bytes );
620 memcpy( p_block->p_buffer, oggpacket.packet, oggpacket.bytes );
621 p_block->i_dts = p_block->i_pts = p_pict->date;;
626 /*****************************************************************************
627 * CloseEncoder: theora encoder destruction
628 *****************************************************************************/
629 static void CloseEncoder( vlc_object_t *p_this )
631 encoder_t *p_enc = (encoder_t *)p_this;
632 encoder_sys_t *p_sys = p_enc->p_sys;
634 theora_info_clear( &p_sys->ti );
635 theora_comment_clear( &p_sys->tc );