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>
29 #include <vlc/input.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 */
59 vlc_bool_t b_decoded_first_keyframe;
67 /*****************************************************************************
69 *****************************************************************************/
70 static int OpenDecoder ( vlc_object_t * );
71 static int OpenPacketizer( vlc_object_t * );
72 static void CloseDecoder ( vlc_object_t * );
74 static void *DecodeBlock ( decoder_t *, block_t ** );
75 static int ProcessHeaders( decoder_t * );
76 static void *ProcessPacket ( decoder_t *, ogg_packet *, block_t ** );
78 static picture_t *DecodePacket( decoder_t *, ogg_packet * );
80 static void ParseTheoraComments( decoder_t * );
81 static void theora_CopyPicture( decoder_t *, picture_t *, yuv_buffer * );
83 static int OpenEncoder( vlc_object_t *p_this );
84 static void CloseEncoder( vlc_object_t *p_this );
85 static block_t *Encode( encoder_t *p_enc, picture_t *p_pict );
87 /*****************************************************************************
89 *****************************************************************************/
90 #define ENC_QUALITY_TEXT N_("Encoding quality")
91 #define ENC_QUALITY_LONGTEXT N_( \
92 "Allows you to specify a quality between 1 (low) and 10 (high), instead " \
93 "of specifying a particular bitrate. This will produce a VBR stream." )
96 set_category( CAT_INPUT );
97 set_subcategory( SUBCAT_INPUT_VCODEC );
98 set_description( _("Theora video decoder") );
99 set_capability( "decoder", 100 );
100 set_callbacks( OpenDecoder, CloseDecoder );
101 add_shortcut( "theora" );
104 set_description( _("Theora video packetizer") );
105 set_capability( "packetizer", 100 );
106 set_callbacks( OpenPacketizer, CloseDecoder );
107 add_shortcut( "theora" );
110 set_description( _("Theora video encoder") );
111 set_capability( "encoder", 100 );
112 set_callbacks( OpenEncoder, CloseEncoder );
113 add_shortcut( "theora" );
115 # define ENC_CFG_PREFIX "sout-theora-"
116 add_integer( ENC_CFG_PREFIX "quality", 2, NULL, ENC_QUALITY_TEXT,
117 ENC_QUALITY_LONGTEXT, VLC_FALSE );
120 static const char *ppsz_enc_options[] = {
124 /*****************************************************************************
125 * OpenDecoder: probe the decoder and return score
126 *****************************************************************************/
127 static int OpenDecoder( vlc_object_t *p_this )
129 decoder_t *p_dec = (decoder_t*)p_this;
130 decoder_sys_t *p_sys;
132 if( p_dec->fmt_in.i_codec != VLC_FOURCC('t','h','e','o') )
137 /* Allocate the memory needed to store the decoder's structure */
138 if( ( p_dec->p_sys = p_sys =
139 (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
141 msg_Err( p_dec, "out of memory" );
144 p_dec->p_sys->b_packetizer = VLC_FALSE;
147 p_sys->b_decoded_first_keyframe = VLC_FALSE;
149 /* Set output properties */
150 p_dec->fmt_out.i_cat = VIDEO_ES;
151 p_dec->fmt_out.i_codec = VLC_FOURCC('I','4','2','0');
154 p_dec->pf_decode_video = (picture_t *(*)(decoder_t *, block_t **))
156 p_dec->pf_packetize = (block_t *(*)(decoder_t *, block_t **))
159 /* Init supporting Theora structures needed in header parsing */
160 theora_comment_init( &p_sys->tc );
161 theora_info_init( &p_sys->ti );
163 p_sys->i_headers = 0;
168 static int OpenPacketizer( vlc_object_t *p_this )
170 decoder_t *p_dec = (decoder_t*)p_this;
172 int i_ret = OpenDecoder( p_this );
174 if( i_ret == VLC_SUCCESS )
176 p_dec->p_sys->b_packetizer = VLC_TRUE;
177 p_dec->fmt_out.i_codec = VLC_FOURCC( 't', 'h', 'e', 'o' );
183 /****************************************************************************
184 * DecodeBlock: the whole thing
185 ****************************************************************************
186 * This function must be fed with ogg packets.
187 ****************************************************************************/
188 static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
190 decoder_sys_t *p_sys = p_dec->p_sys;
192 ogg_packet oggpacket;
194 if( !pp_block || !*pp_block ) return NULL;
198 /* Block to Ogg packet */
199 oggpacket.packet = p_block->p_buffer;
200 oggpacket.bytes = p_block->i_buffer;
201 oggpacket.granulepos = p_block->i_dts;
204 oggpacket.packetno = 0;
206 /* Check for headers */
207 if( p_sys->i_headers == 0 && p_dec->fmt_in.i_extra )
209 /* Headers already available as extra data */
210 p_sys->i_headers = 3;
212 else if( oggpacket.bytes && p_sys->i_headers < 3 )
214 /* Backup headers as extra data */
217 p_dec->fmt_in.p_extra =
218 realloc( p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra +
219 oggpacket.bytes + 2 );
220 p_extra = p_dec->fmt_in.p_extra + p_dec->fmt_in.i_extra;
221 *(p_extra++) = oggpacket.bytes >> 8;
222 *(p_extra++) = oggpacket.bytes & 0xFF;
224 memcpy( p_extra, oggpacket.packet, oggpacket.bytes );
225 p_dec->fmt_in.i_extra += oggpacket.bytes + 2;
227 block_Release( *pp_block );
232 if( p_sys->i_headers == 3 )
234 if( ProcessHeaders( p_dec ) != VLC_SUCCESS )
236 p_sys->i_headers = 0;
237 p_dec->fmt_in.i_extra = 0;
238 block_Release( *pp_block );
241 else p_sys->i_headers++;
244 return ProcessPacket( p_dec, &oggpacket, pp_block );
247 /*****************************************************************************
248 * ProcessHeaders: process Theora headers.
249 *****************************************************************************/
250 static int ProcessHeaders( decoder_t *p_dec )
252 decoder_sys_t *p_sys = p_dec->p_sys;
253 ogg_packet oggpacket;
257 if( !p_dec->fmt_in.i_extra ) return VLC_EGENERIC;
259 oggpacket.granulepos = -1;
260 oggpacket.b_o_s = 1; /* yes this actually is a b_o_s packet :) */
262 oggpacket.packetno = 0;
263 p_extra = p_dec->fmt_in.p_extra;
264 i_extra = p_dec->fmt_in.i_extra;
266 /* Take care of the initial Vorbis header */
267 oggpacket.bytes = *(p_extra++) << 8;
268 oggpacket.bytes |= (*(p_extra++) & 0xFF);
269 oggpacket.packet = p_extra;
270 p_extra += oggpacket.bytes;
271 i_extra -= (oggpacket.bytes + 2);
274 msg_Err( p_dec, "header data corrupted");
278 if( theora_decode_header( &p_sys->ti, &p_sys->tc, &oggpacket ) < 0 )
280 msg_Err( p_dec, "This bitstream does not contain Theora video data" );
284 /* Set output properties */
285 p_dec->fmt_out.video.i_width = p_sys->ti.width;
286 p_dec->fmt_out.video.i_height = p_sys->ti.height;
287 if( p_sys->ti.frame_width && p_sys->ti.frame_height )
289 p_dec->fmt_out.video.i_width = p_sys->ti.frame_width;
290 p_dec->fmt_out.video.i_height = p_sys->ti.frame_height;
293 if( p_sys->ti.aspect_denominator && p_sys->ti.aspect_numerator )
295 p_dec->fmt_out.video.i_aspect = ((int64_t)VOUT_ASPECT_FACTOR) *
296 ( p_sys->ti.aspect_numerator * p_sys->ti.width ) /
297 ( p_sys->ti.aspect_denominator * p_sys->ti.height );
301 p_dec->fmt_out.video.i_aspect = VOUT_ASPECT_FACTOR *
302 p_sys->ti.frame_width / p_sys->ti.frame_height;
305 if( p_sys->ti.fps_numerator > 0 && p_sys->ti.fps_denominator > 0 )
307 p_dec->fmt_out.video.i_frame_rate = p_sys->ti.fps_numerator;
308 p_dec->fmt_out.video.i_frame_rate_base = p_sys->ti.fps_denominator;
311 msg_Dbg( p_dec, "%dx%d %.02f fps video, frame content "
312 "is %dx%d with offset (%d,%d)",
313 p_sys->ti.width, p_sys->ti.height,
314 (double)p_sys->ti.fps_numerator/p_sys->ti.fps_denominator,
315 p_sys->ti.frame_width, p_sys->ti.frame_height,
316 p_sys->ti.offset_x, p_sys->ti.offset_y );
318 /* The next packet in order is the comments header */
320 oggpacket.bytes = *(p_extra++) << 8;
321 oggpacket.bytes |= (*(p_extra++) & 0xFF);
322 oggpacket.packet = p_extra;
323 p_extra += oggpacket.bytes;
324 i_extra -= (oggpacket.bytes + 2);
327 msg_Err( p_dec, "header data corrupted");
331 /* The next packet in order is the comments header */
332 if( theora_decode_header( &p_sys->ti, &p_sys->tc, &oggpacket ) < 0 )
334 msg_Err( p_dec, "2nd Theora header is corrupted" );
338 ParseTheoraComments( p_dec );
340 /* The next packet in order is the codebooks header
341 * We need to watch out that this packet is not missing as a
342 * missing or corrupted header is fatal. */
343 oggpacket.bytes = *(p_extra++) << 8;
344 oggpacket.bytes |= (*(p_extra++) & 0xFF);
345 oggpacket.packet = p_extra;
346 i_extra -= (oggpacket.bytes + 2);
349 msg_Err( p_dec, "header data corrupted");
353 /* The next packet in order is the codebooks header
354 * We need to watch out that this packet is not missing as a
355 * missing or corrupted header is fatal */
356 if( theora_decode_header( &p_sys->ti, &p_sys->tc, &oggpacket ) < 0 )
358 msg_Err( p_dec, "3rd Theora header is corrupted" );
362 if( !p_sys->b_packetizer )
364 /* We have all the headers, initialize decoder */
365 theora_decode_init( &p_sys->td, &p_sys->ti );
369 p_dec->fmt_out.i_extra = p_dec->fmt_in.i_extra;
370 p_dec->fmt_out.p_extra =
371 realloc( p_dec->fmt_out.p_extra, p_dec->fmt_out.i_extra );
372 memcpy( p_dec->fmt_out.p_extra,
373 p_dec->fmt_in.p_extra, p_dec->fmt_out.i_extra );
379 /*****************************************************************************
380 * ProcessPacket: processes a theora packet.
381 *****************************************************************************/
382 static void *ProcessPacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
385 decoder_sys_t *p_sys = p_dec->p_sys;
386 block_t *p_block = *pp_block;
389 if( ( p_block->i_flags & BLOCK_FLAG_DISCONTINUITY ) != 0 )
391 /* Don't send the the first packet after a discontinuity to
392 * theora_decode, otherwise we get purple/green display artifacts
393 * appearing in the video output */
397 /* Date management */
398 if( p_block->i_pts > 0 && p_block->i_pts != p_sys->i_pts )
400 p_sys->i_pts = p_block->i_pts;
403 *pp_block = NULL; /* To avoid being fed the same packet again */
405 if( p_sys->b_packetizer )
407 /* Date management */
408 p_block->i_dts = p_block->i_pts = p_sys->i_pts;
410 if( p_sys->i_headers >= 3 )
411 p_block->i_length = p_sys->i_pts - p_block->i_pts;
413 p_block->i_length = 0;
419 if( p_sys->i_headers >= 3 )
420 p_buf = DecodePacket( p_dec, p_oggpacket );
424 if( p_block ) block_Release( p_block );
427 /* Date management */
428 p_sys->i_pts += ( I64C(1000000) * p_sys->ti.fps_denominator /
429 p_sys->ti.fps_numerator ); /* 1 frame per packet */
434 /*****************************************************************************
435 * DecodePacket: decodes a Theora packet.
436 *****************************************************************************/
437 static picture_t *DecodePacket( decoder_t *p_dec, ogg_packet *p_oggpacket )
439 decoder_sys_t *p_sys = p_dec->p_sys;
443 theora_decode_packetin( &p_sys->td, p_oggpacket );
445 /* Check for keyframe */
446 if( !(p_oggpacket->packet[0] & 0x80) /* data packet */ &&
447 !(p_oggpacket->packet[0] & 0x40) /* intra frame */ )
448 p_sys->b_decoded_first_keyframe = VLC_TRUE;
450 /* If we haven't seen a single keyframe yet, don't let Theora decode
451 * anything, otherwise we'll get display artifacts. (This is impossible
452 * in the general case, but can happen if e.g. we play a network stream
453 * using a timed URL, such that the server doesn't start the video with a
455 if( p_sys->b_decoded_first_keyframe )
456 theora_decode_YUVout( &p_sys->td, &yuv );
460 /* Get a new picture */
461 p_pic = p_dec->pf_vout_buffer_new( p_dec );
462 if( !p_pic ) return NULL;
464 theora_CopyPicture( p_dec, p_pic, &yuv );
466 p_pic->date = p_sys->i_pts;
471 /*****************************************************************************
472 * ParseTheoraComments: FIXME should be done in demuxer
473 *****************************************************************************/
474 static void ParseTheoraComments( decoder_t *p_dec )
476 input_thread_t *p_input = (input_thread_t *)p_dec->p_parent;
477 char *psz_name, *psz_value, *psz_comment;
480 if( p_input->i_object_type != VLC_OBJECT_INPUT ) return;
482 while ( i < p_dec->p_sys->tc.comments )
484 psz_comment = strdup( p_dec->p_sys->tc.user_comments[i] );
487 msg_Warn( p_dec, "out of memory" );
490 psz_name = psz_comment;
491 psz_value = strchr( psz_comment, '=' );
496 input_Control( p_input, INPUT_ADD_INFO, _("Theora comment"),
497 psz_name, psz_value );
504 /*****************************************************************************
505 * CloseDecoder: theora decoder destruction
506 *****************************************************************************/
507 static void CloseDecoder( vlc_object_t *p_this )
509 decoder_t *p_dec = (decoder_t *)p_this;
510 decoder_sys_t *p_sys = p_dec->p_sys;
512 theora_info_clear( &p_sys->ti );
513 theora_comment_clear( &p_sys->tc );
518 /*****************************************************************************
519 * theora_CopyPicture: copy a picture from theora internal buffers to a
520 * picture_t structure.
521 *****************************************************************************/
522 static void theora_CopyPicture( decoder_t *p_dec, picture_t *p_pic,
525 int i_plane, i_line, i_width, i_dst_stride, i_src_stride;
526 int i_src_xoffset, i_src_yoffset;
527 uint8_t *p_dst, *p_src;
529 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
531 p_dst = p_pic->p[i_plane].p_pixels;
532 p_src = i_plane ? (i_plane - 1 ? yuv->v : yuv->u ) : yuv->y;
533 i_width = p_pic->p[i_plane].i_visible_pitch;
534 i_dst_stride = p_pic->p[i_plane].i_pitch;
535 i_src_stride = i_plane ? yuv->uv_stride : yuv->y_stride;
536 i_src_xoffset = p_dec->p_sys->ti.offset_x;
537 i_src_yoffset = p_dec->p_sys->ti.offset_y;
544 p_src += (i_src_yoffset * i_src_stride + i_src_yoffset);
546 for( i_line = 0; i_line < p_pic->p[i_plane].i_visible_lines; i_line++ )
548 p_dec->p_vlc->pf_memcpy( p_dst, p_src + i_src_xoffset,
549 i_plane ? yuv->uv_width : yuv->y_width );
550 p_src += i_src_stride;
551 p_dst += i_dst_stride;
556 /*****************************************************************************
557 * encoder_sys_t : theora encoder descriptor
558 *****************************************************************************/
564 vlc_bool_t b_headers;
569 theora_info ti; /* theora bitstream settings */
570 theora_comment tc; /* theora comment header */
571 theora_state td; /* theora bitstream user comments */
573 int i_width, i_height;
576 /*****************************************************************************
577 * OpenEncoder: probe the encoder and return score
578 *****************************************************************************/
579 static int OpenEncoder( vlc_object_t *p_this )
581 encoder_t *p_enc = (encoder_t *)p_this;
582 encoder_sys_t *p_sys = p_enc->p_sys;
588 if( p_enc->fmt_out.i_codec != VLC_FOURCC('t','h','e','o') &&
594 /* Allocate the memory needed to store the decoder's structure */
595 if( ( p_sys = (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
597 msg_Err( p_enc, "out of memory" );
600 p_enc->p_sys = p_sys;
602 p_enc->pf_encode_video = Encode;
603 p_enc->fmt_in.i_codec = VLC_FOURCC('I','4','2','0');
604 p_enc->fmt_out.i_codec = VLC_FOURCC('t','h','e','o');
606 sout_CfgParse( p_enc, ENC_CFG_PREFIX, ppsz_enc_options, p_enc->p_cfg );
608 var_Get( p_enc, ENC_CFG_PREFIX "quality", &val );
609 i_quality = val.i_int;
610 if( i_quality > 10 ) i_quality = 10;
611 if( i_quality < 0 ) i_quality = 0;
613 theora_info_init( &p_sys->ti );
615 p_sys->ti.width = p_enc->fmt_in.video.i_width;
616 p_sys->ti.height = p_enc->fmt_in.video.i_height;
618 if( p_sys->ti.width % 16 || p_sys->ti.height % 16 )
620 /* Pictures from the transcoder should always have a pitch
621 * which is a multiple of 16 */
622 p_sys->ti.width = (p_sys->ti.width + 15) >> 4 << 4;
623 p_sys->ti.height = (p_sys->ti.height + 15) >> 4 << 4;
625 msg_Dbg( p_enc, "padding video from %dx%d to %dx%d",
626 p_enc->fmt_in.video.i_width, p_enc->fmt_in.video.i_height,
627 p_sys->ti.width, p_sys->ti.height );
630 p_sys->ti.frame_width = p_enc->fmt_in.video.i_width;
631 p_sys->ti.frame_height = p_enc->fmt_in.video.i_height;
632 p_sys->ti.offset_x = 0 /*frame_x_offset*/;
633 p_sys->ti.offset_y = 0 /*frame_y_offset*/;
635 p_sys->i_width = p_sys->ti.width;
636 p_sys->i_height = p_sys->ti.height;
638 if( !p_enc->fmt_in.video.i_frame_rate ||
639 !p_enc->fmt_in.video.i_frame_rate_base )
641 p_sys->ti.fps_numerator = 25;
642 p_sys->ti.fps_denominator = 1;
646 p_sys->ti.fps_numerator = p_enc->fmt_in.video.i_frame_rate;
647 p_sys->ti.fps_denominator = p_enc->fmt_in.video.i_frame_rate_base;
650 if( p_enc->fmt_in.video.i_aspect )
652 int64_t i_num, i_den;
653 int i_dst_num, i_dst_den;
655 i_num = p_enc->fmt_in.video.i_aspect * (int64_t)p_sys->ti.height;
656 i_den = VOUT_ASPECT_FACTOR * p_sys->ti.width;
657 vlc_reduce( &i_dst_num, &i_dst_den, i_num, i_den, 0 );
658 p_sys->ti.aspect_numerator = i_dst_num;
659 p_sys->ti.aspect_denominator = i_dst_den;
663 p_sys->ti.aspect_numerator = 4;
664 p_sys->ti.aspect_denominator = 3;
667 p_sys->ti.target_bitrate = p_enc->fmt_out.i_bitrate;
668 p_sys->ti.quality = ((float)i_quality) * 6.3;
670 p_sys->ti.dropframes_p = 0;
671 p_sys->ti.quick_p = 1;
672 p_sys->ti.keyframe_auto_p = 1;
673 p_sys->ti.keyframe_frequency = 64;
674 p_sys->ti.keyframe_frequency_force = 64;
675 p_sys->ti.keyframe_data_target_bitrate = p_enc->fmt_out.i_bitrate * 1.5;
676 p_sys->ti.keyframe_auto_threshold = 80;
677 p_sys->ti.keyframe_mindistance = 8;
678 p_sys->ti.noise_sensitivity = 1;
680 theora_encode_init( &p_sys->td, &p_sys->ti );
681 theora_info_clear( &p_sys->ti );
682 theora_comment_init( &p_sys->tc );
684 /* Create and store headers */
685 p_enc->fmt_out.i_extra = 3 * 2;
686 for( i = 0; i < 3; i++ )
688 if( i == 0 ) theora_encode_header( &p_sys->td, &header );
689 else if( i == 1 ) theora_encode_comment( &p_sys->tc, &header );
690 else if( i == 2 ) theora_encode_tables( &p_sys->td, &header );
692 p_enc->fmt_out.p_extra =
693 realloc( p_enc->fmt_out.p_extra,
694 p_enc->fmt_out.i_extra + header.bytes );
695 p_extra = p_enc->fmt_out.p_extra;
696 p_extra += p_enc->fmt_out.i_extra + (i-3)*2;
697 p_enc->fmt_out.i_extra += header.bytes;
699 *(p_extra++) = header.bytes >> 8;
700 *(p_extra++) = header.bytes & 0xFF;
702 memcpy( p_extra, header.packet, header.bytes );
708 /****************************************************************************
709 * Encode: the whole thing
710 ****************************************************************************
711 * This function spits out ogg packets.
712 ****************************************************************************/
713 static block_t *Encode( encoder_t *p_enc, picture_t *p_pict )
715 encoder_sys_t *p_sys = p_enc->p_sys;
716 ogg_packet oggpacket;
722 if( p_pict->p[0].i_pitch < (int)p_sys->i_width ||
723 p_pict->p[0].i_lines < (int)p_sys->i_height )
725 msg_Warn( p_enc, "frame is smaller than encoding size"
726 "(%ix%i->%ix%i) -> dropping frame",
727 p_pict->p[0].i_pitch, p_pict->p[0].i_lines,
728 p_sys->i_width, p_sys->i_height );
733 if( p_pict->p[0].i_visible_pitch < (int)p_sys->i_width )
735 for( i = 0; i < p_sys->i_height; i++ )
737 memset( p_pict->p[0].p_pixels + i * p_pict->p[0].i_pitch +
738 p_pict->p[0].i_visible_pitch,
739 *( p_pict->p[0].p_pixels + i * p_pict->p[0].i_pitch +
740 p_pict->p[0].i_visible_pitch - 1 ),
741 p_sys->i_width - p_pict->p[0].i_visible_pitch );
743 for( i = 0; i < p_sys->i_height / 2; i++ )
745 memset( p_pict->p[1].p_pixels + i * p_pict->p[1].i_pitch +
746 p_pict->p[1].i_visible_pitch,
747 *( p_pict->p[1].p_pixels + i * p_pict->p[1].i_pitch +
748 p_pict->p[1].i_visible_pitch - 1 ),
749 p_sys->i_width / 2 - p_pict->p[1].i_visible_pitch );
750 memset( p_pict->p[2].p_pixels + i * p_pict->p[2].i_pitch +
751 p_pict->p[2].i_visible_pitch,
752 *( p_pict->p[2].p_pixels + i * p_pict->p[2].i_pitch +
753 p_pict->p[2].i_visible_pitch - 1 ),
754 p_sys->i_width / 2 - p_pict->p[2].i_visible_pitch );
758 if( p_pict->p[0].i_visible_lines < (int)p_sys->i_height )
760 for( i = p_pict->p[0].i_visible_lines; i < p_sys->i_height; i++ )
762 memset( p_pict->p[0].p_pixels + i * p_pict->p[0].i_pitch, 0,
765 for( i = p_pict->p[1].i_visible_lines; i < p_sys->i_height / 2; i++ )
767 memset( p_pict->p[1].p_pixels + i * p_pict->p[1].i_pitch, 0x80,
768 p_sys->i_width / 2 );
769 memset( p_pict->p[2].p_pixels + i * p_pict->p[2].i_pitch, 0x80,
770 p_sys->i_width / 2 );
774 /* Theora is a one-frame-in, one-frame-out system. Submit a frame
775 * for compression and pull out the packet. */
777 yuv.y_width = p_sys->i_width;
778 yuv.y_height = p_sys->i_height;
779 yuv.y_stride = p_pict->p[0].i_pitch;
781 yuv.uv_width = p_sys->i_width / 2;
782 yuv.uv_height = p_sys->i_height / 2;
783 yuv.uv_stride = p_pict->p[1].i_pitch;
785 yuv.y = p_pict->p[0].p_pixels;
786 yuv.u = p_pict->p[1].p_pixels;
787 yuv.v = p_pict->p[2].p_pixels;
789 if( theora_encode_YUVin( &p_sys->td, &yuv ) < 0 )
791 msg_Warn( p_enc, "failed encoding a frame" );
795 theora_encode_packetout( &p_sys->td, 0, &oggpacket );
797 /* Ogg packet to block */
798 p_block = block_New( p_enc, oggpacket.bytes );
799 memcpy( p_block->p_buffer, oggpacket.packet, oggpacket.bytes );
800 p_block->i_dts = p_block->i_pts = p_pict->date;
805 /*****************************************************************************
806 * CloseEncoder: theora encoder destruction
807 *****************************************************************************/
808 static void CloseEncoder( vlc_object_t *p_this )
810 encoder_t *p_enc = (encoder_t *)p_this;
811 encoder_sys_t *p_sys = p_enc->p_sys;
813 theora_info_clear( &p_sys->ti );
814 theora_comment_clear( &p_sys->tc );