1 /*****************************************************************************
2 * theora.c: theora decoder module making use of libtheora.
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VideoLAN
5 * $Id: theora.c,v 1.16 2003/12/07 17:09:33 gbazin 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"
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 *****************************************************************************/
85 set_description( _("Theora video decoder") );
86 set_capability( "decoder", 100 );
87 set_callbacks( OpenDecoder, CloseDecoder );
88 add_shortcut( "theora" );
91 set_description( _("Theora video packetizer") );
92 set_capability( "packetizer", 100 );
93 set_callbacks( OpenPacketizer, CloseDecoder );
94 add_shortcut( "theora" );
97 set_description( _("Theora video encoder") );
98 set_capability( "encoder", 100 );
99 set_callbacks( OpenEncoder, CloseEncoder );
100 add_shortcut( "theora" );
103 /*****************************************************************************
104 * OpenDecoder: probe the decoder and return score
105 *****************************************************************************/
106 static int OpenDecoder( vlc_object_t *p_this )
108 decoder_t *p_dec = (decoder_t*)p_this;
109 decoder_sys_t *p_sys;
111 if( p_dec->fmt_in.i_codec != VLC_FOURCC('t','h','e','o') )
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 )
120 msg_Err( p_dec, "out of memory" );
123 p_dec->p_sys->b_packetizer = VLC_FALSE;
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');
132 p_dec->pf_decode_video = (picture_t *(*)(decoder_t *, block_t **))
134 p_dec->pf_packetize = (block_t *(*)(decoder_t *, block_t **))
137 /* Init supporting Theora structures needed in header parsing */
138 theora_comment_init( &p_sys->tc );
139 theora_info_init( &p_sys->ti );
141 p_sys->i_headers = 0;
146 static int OpenPacketizer( vlc_object_t *p_this )
148 decoder_t *p_dec = (decoder_t*)p_this;
150 int i_ret = OpenDecoder( p_this );
152 if( i_ret == VLC_SUCCESS )
154 p_dec->p_sys->b_packetizer = VLC_TRUE;
155 p_dec->fmt_out.i_codec = VLC_FOURCC( 't', 'h', 'e', 'o' );
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 )
168 decoder_sys_t *p_sys = p_dec->p_sys;
170 ogg_packet oggpacket;
172 if( !pp_block || !*pp_block ) return NULL;
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;
182 oggpacket.packetno = 0;
184 if( p_sys->i_headers == 0 )
186 /* Take care of the initial Theora header */
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 )
191 msg_Err( p_dec, "This bitstream does not contain Theora "
193 block_Release( p_block );
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;
202 if( p_sys->ti.aspect_denominator )
203 p_dec->fmt_out.video.i_aspect = VOUT_ASPECT_FACTOR *
204 p_sys->ti.aspect_numerator / p_sys->ti.aspect_denominator;
206 p_dec->fmt_out.video.i_aspect = VOUT_ASPECT_FACTOR *
207 p_sys->ti.frame_width / p_sys->ti.frame_height;
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 );
216 return ProcessPacket( p_dec, &oggpacket, pp_block );
219 if( p_sys->i_headers == 1 )
221 /* The next packet in order is the comments header */
222 if( theora_decode_header( &p_sys->ti, &p_sys->tc, &oggpacket ) < 0 )
224 msg_Err( p_dec, "2nd Theora header is corrupted" );
229 ParseTheoraComments( p_dec );
231 return ProcessPacket( p_dec, &oggpacket, pp_block );
234 if( p_sys->i_headers == 2 )
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 )
241 msg_Err( p_dec, "3rd Theora header is corrupted" );
246 if( !p_sys->b_packetizer )
248 /* We have all the headers, initialize decoder */
249 theora_decode_init( &p_sys->td, &p_sys->ti );
252 return ProcessPacket( p_dec, &oggpacket, pp_block );
255 return ProcessPacket( p_dec, &oggpacket, pp_block );
258 /*****************************************************************************
259 * ProcessPacket: processes a theora packet.
260 *****************************************************************************/
261 static void *ProcessPacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
264 decoder_sys_t *p_sys = p_dec->p_sys;
265 block_t *p_block = *pp_block;
268 /* Date management */
269 if( p_block->i_pts > 0 && p_block->i_pts != p_sys->i_pts )
271 p_sys->i_pts = p_block->i_pts;
274 if( p_sys->b_packetizer )
276 /* Date management */
277 p_block->i_dts = p_block->i_pts = p_sys->i_pts;
279 if( p_sys->i_headers >= 3 )
280 p_block->i_length = p_sys->i_pts - p_block->i_pts;
282 p_block->i_length = 0;
288 if( p_sys->i_headers >= 3 )
289 p_buf = DecodePacket( p_dec, p_oggpacket );
295 block_Release( p_block );
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 */
307 /*****************************************************************************
308 * DecodePacket: decodes a Theora packet.
309 *****************************************************************************/
310 static picture_t *DecodePacket( decoder_t *p_dec, ogg_packet *p_oggpacket )
312 decoder_sys_t *p_sys = p_dec->p_sys;
316 theora_decode_packetin( &p_sys->td, p_oggpacket );
319 theora_decode_YUVout( &p_sys->td, &yuv );
321 /* Get a new picture */
322 p_pic = p_dec->pf_vout_buffer_new( p_dec );
323 if( !p_pic ) return NULL;
325 theora_CopyPicture( p_dec, p_pic, &yuv );
327 p_pic->date = p_sys->i_pts;
332 /*****************************************************************************
333 * ParseTheoraComments: FIXME should be done in demuxer
334 *****************************************************************************/
335 static void ParseTheoraComments( decoder_t *p_dec )
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") );
341 char *psz_name, *psz_value, *psz_comment;
342 while ( i < p_dec->p_sys->tc.comments )
344 psz_comment = strdup( p_dec->p_sys->tc.user_comments[i] );
347 msg_Warn( p_dec, "Out of memory" );
350 psz_name = psz_comment;
351 psz_value = strchr( psz_comment, '=' );
356 input_AddInfo( p_cat, psz_name, psz_value );
363 /*****************************************************************************
364 * CloseDecoder: theora decoder destruction
365 *****************************************************************************/
366 static void CloseDecoder( vlc_object_t *p_this )
368 decoder_t *p_dec = (decoder_t *)p_this;
369 decoder_sys_t *p_sys = p_dec->p_sys;
371 theora_info_clear( &p_sys->ti );
372 theora_comment_clear( &p_sys->tc );
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,
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;
388 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
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;
403 p_src += (i_src_yoffset * i_src_stride + i_src_yoffset);
405 for( i_line = 0; i_line < p_pic->p[i_plane].i_lines; i_line++ )
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;
414 /*****************************************************************************
415 * encoder_sys_t : theora encoder descriptor
416 *****************************************************************************/
422 vlc_bool_t b_headers;
427 theora_info ti; /* theora bitstream settings */
428 theora_comment tc; /* theora comment header */
429 theora_state td; /* theora bitstream user comments */
437 /*****************************************************************************
438 * OpenEncoder: probe the encoder and return score
439 *****************************************************************************/
440 static int OpenEncoder( vlc_object_t *p_this )
442 encoder_t *p_enc = (encoder_t *)p_this;
443 encoder_sys_t *p_sys = p_enc->p_sys;
445 if( p_enc->fmt_out.i_codec != VLC_FOURCC('t','h','e','o') )
450 /* Allocate the memory needed to store the decoder's structure */
451 if( ( p_sys = (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
453 msg_Err( p_enc, "out of memory" );
456 p_enc->p_sys = p_sys;
458 p_enc->pf_header = Headers;
459 p_enc->pf_encode_video = Encode;
460 p_enc->fmt_in.i_codec = VLC_FOURCC('I','4','2','0');
462 #define frame_x_offset 0
463 #define frame_y_offset 0
470 theora_info_init( &p_sys->ti );
472 p_sys->ti.width = p_enc->fmt_in.video.i_width;
473 p_sys->ti.height = p_enc->fmt_in.video.i_height;
474 p_sys->ti.frame_width = p_enc->fmt_in.video.i_width;
475 p_sys->ti.frame_height = p_enc->fmt_in.video.i_height;
476 p_sys->ti.offset_x = frame_x_offset;
477 p_sys->ti.offset_y = frame_y_offset;
478 p_sys->ti.fps_numerator = video_hzn;
479 p_sys->ti.fps_denominator = video_hzd;
480 p_sys->ti.aspect_numerator = video_an;
481 p_sys->ti.aspect_denominator = video_ad;
482 p_sys->ti.colorspace = not_specified;
483 p_sys->ti.target_bitrate = p_enc->fmt_out.i_bitrate;
484 p_sys->ti.quality = video_q;
486 p_sys->ti.dropframes_p = 0;
487 p_sys->ti.quick_p = 1;
488 p_sys->ti.keyframe_auto_p = 1;
489 p_sys->ti.keyframe_frequency = 64;
490 p_sys->ti.keyframe_frequency_force = 64;
491 p_sys->ti.keyframe_data_target_bitrate = p_enc->fmt_out.i_bitrate * 1.5;
492 p_sys->ti.keyframe_auto_threshold = 80;
493 p_sys->ti.keyframe_mindistance = 8;
494 p_sys->ti.noise_sensitivity = 1;
496 theora_encode_init( &p_sys->td, &p_sys->ti );
497 theora_info_clear( &p_sys->ti );
498 theora_comment_init( &p_sys->tc );
500 p_sys->b_headers = VLC_FALSE;
505 /****************************************************************************
506 * Encode: the whole thing
507 ****************************************************************************
508 * This function spits out ogg packets.
509 ****************************************************************************/
510 static block_t *Headers( encoder_t *p_enc )
512 encoder_sys_t *p_sys = p_enc->p_sys;
513 block_t *p_chain = NULL;
515 /* Create theora headers */
516 if( !p_sys->b_headers )
518 ogg_packet oggpackets;
521 /* Ogg packet to block */
522 for( i = 0; i < 3; i++ )
527 theora_encode_header( &p_sys->td, &oggpackets );
530 theora_encode_comment( &p_sys->tc, &oggpackets );
533 theora_encode_tables( &p_sys->td, &oggpackets );
537 block_t *p_block = block_New( p_enc, oggpackets.bytes );
538 memcpy( p_block->p_buffer, oggpackets.packet, oggpackets.bytes );
539 p_block->i_dts = p_block->i_pts = p_block->i_length = 0;
540 block_ChainAppend( &p_chain, p_block );
543 p_sys->b_headers = VLC_TRUE;
549 /****************************************************************************
550 * Encode: the whole thing
551 ****************************************************************************
552 * This function spits out ogg packets.
553 ****************************************************************************/
554 static block_t *Encode( encoder_t *p_enc, picture_t *p_pict )
556 encoder_sys_t *p_sys = p_enc->p_sys;
557 ogg_packet oggpacket;
561 /* Theora is a one-frame-in, one-frame-out system. Submit a frame
562 * for compression and pull out the packet. */
564 yuv.y_width = p_pict->p[0].i_visible_pitch;
565 yuv.y_height = p_pict->p[0].i_lines;
566 yuv.y_stride = p_pict->p[0].i_pitch;
568 yuv.uv_width = p_pict->p[1].i_visible_pitch;
569 yuv.uv_height = p_pict->p[1].i_lines;
570 yuv.uv_stride = p_pict->p[1].i_pitch;
572 yuv.y = p_pict->p[0].p_pixels;
573 yuv.u = p_pict->p[1].p_pixels;
574 yuv.v = p_pict->p[2].p_pixels;
576 if( theora_encode_YUVin( &p_sys->td, &yuv ) < 0 )
578 msg_Warn( p_enc, "failed encoding a frame" );
582 theora_encode_packetout( &p_sys->td, 0, &oggpacket );
584 /* Ogg packet to block */
585 p_block = block_New( p_enc, oggpacket.bytes );
586 memcpy( p_block->p_buffer, oggpacket.packet, oggpacket.bytes );
587 p_block->i_dts = p_block->i_pts = p_pict->date;;
592 /*****************************************************************************
593 * CloseEncoder: theora encoder destruction
594 *****************************************************************************/
595 static void CloseEncoder( vlc_object_t *p_this )
597 encoder_t *p_enc = (encoder_t *)p_this;
598 encoder_sys_t *p_sys = p_enc->p_sys;
600 theora_info_clear( &p_sys->ti );
601 theora_comment_clear( &p_sys->tc );