1 /*****************************************************************************
2 * theora.c: theora decoder module making use of libtheora.
3 *****************************************************************************
4 * Copyright (C) 1999-2001 the VideoLAN team
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_codec.h>
37 #include <vlc_input.h>
38 #include <vlc_memory.h>
41 #include <theora/theora.h>
43 /*****************************************************************************
44 * decoder_sys_t : theora decoder descriptor
45 *****************************************************************************/
59 theora_info ti; /* theora bitstream settings */
60 theora_comment tc; /* theora comment header */
61 theora_state td; /* theora bitstream user comments */
66 bool b_decoded_first_keyframe;
74 /*****************************************************************************
76 *****************************************************************************/
77 static int OpenDecoder ( vlc_object_t * );
78 static int OpenPacketizer( vlc_object_t * );
79 static void CloseDecoder ( vlc_object_t * );
81 static void *DecodeBlock ( decoder_t *, block_t ** );
82 static int ProcessHeaders( decoder_t * );
83 static void *ProcessPacket ( decoder_t *, ogg_packet *, block_t ** );
85 static picture_t *DecodePacket( decoder_t *, ogg_packet * );
87 static void ParseTheoraComments( decoder_t * );
88 static void theora_CopyPicture( picture_t *, yuv_buffer * );
90 static int OpenEncoder( vlc_object_t *p_this );
91 static void CloseEncoder( vlc_object_t *p_this );
92 static block_t *Encode( encoder_t *p_enc, picture_t *p_pict );
94 /*****************************************************************************
96 *****************************************************************************/
97 #define ENC_QUALITY_TEXT N_("Encoding quality")
98 #define ENC_QUALITY_LONGTEXT N_( \
99 "Enforce a quality between 1 (low) and 10 (high), instead " \
100 "of specifying a particular bitrate. This will produce a VBR stream." )
103 set_category( CAT_INPUT )
104 set_subcategory( SUBCAT_INPUT_VCODEC )
105 set_shortname( "Theora" )
106 set_description( N_("Theora video decoder") )
107 set_capability( "decoder", 100 )
108 set_callbacks( OpenDecoder, CloseDecoder )
109 add_shortcut( "theora" )
112 set_description( N_("Theora video packetizer") )
113 set_capability( "packetizer", 100 )
114 set_callbacks( OpenPacketizer, CloseDecoder )
115 add_shortcut( "theora" )
118 set_description( N_("Theora video encoder") )
119 set_capability( "encoder", 150 )
120 set_callbacks( OpenEncoder, CloseEncoder )
121 add_shortcut( "theora" )
123 # define ENC_CFG_PREFIX "sout-theora-"
124 add_integer( ENC_CFG_PREFIX "quality", 2, NULL, ENC_QUALITY_TEXT,
125 ENC_QUALITY_LONGTEXT, false )
128 static const char *const ppsz_enc_options[] = {
132 /*****************************************************************************
133 * OpenDecoder: probe the decoder and return score
134 *****************************************************************************/
135 static int OpenDecoder( vlc_object_t *p_this )
137 decoder_t *p_dec = (decoder_t*)p_this;
138 decoder_sys_t *p_sys;
140 if( p_dec->fmt_in.i_codec != VLC_CODEC_THEORA )
145 /* Allocate the memory needed to store the decoder's structure */
146 if( ( p_dec->p_sys = p_sys = malloc(sizeof(*p_sys)) ) == NULL )
148 p_dec->p_sys->b_packetizer = false;
151 p_sys->b_decoded_first_keyframe = false;
153 /* Set output properties */
154 p_dec->fmt_out.i_cat = VIDEO_ES;
155 p_dec->fmt_out.i_codec = VLC_CODEC_I420;
158 p_dec->pf_decode_video = (picture_t *(*)(decoder_t *, block_t **))
160 p_dec->pf_packetize = (block_t *(*)(decoder_t *, block_t **))
163 /* Init supporting Theora structures needed in header parsing */
164 theora_comment_init( &p_sys->tc );
165 theora_info_init( &p_sys->ti );
167 p_sys->i_headers = 0;
172 static int OpenPacketizer( vlc_object_t *p_this )
174 decoder_t *p_dec = (decoder_t*)p_this;
176 int i_ret = OpenDecoder( p_this );
178 if( i_ret == VLC_SUCCESS )
180 p_dec->p_sys->b_packetizer = true;
181 p_dec->fmt_out.i_codec = VLC_CODEC_THEORA;
187 /****************************************************************************
188 * DecodeBlock: the whole thing
189 ****************************************************************************
190 * This function must be fed with ogg packets.
191 ****************************************************************************/
192 static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
194 decoder_sys_t *p_sys = p_dec->p_sys;
196 ogg_packet oggpacket;
198 if( !pp_block || !*pp_block ) return NULL;
202 /* Block to Ogg packet */
203 oggpacket.packet = p_block->p_buffer;
204 oggpacket.bytes = p_block->i_buffer;
205 oggpacket.granulepos = p_block->i_dts;
208 oggpacket.packetno = 0;
210 /* Check for headers */
211 if( p_sys->i_headers == 0 && p_dec->fmt_in.i_extra )
213 /* Headers already available as extra data */
214 p_sys->i_headers = 3;
216 else if( oggpacket.bytes && p_sys->i_headers < 3 )
218 /* Backup headers as extra data */
221 p_dec->fmt_in.p_extra = realloc_or_free( p_dec->fmt_in.p_extra,
222 p_dec->fmt_in.i_extra + oggpacket.bytes + 2 );
223 assert( p_dec->fmt_in.p_extra );
224 p_extra = ((uint8_t *)p_dec->fmt_in.p_extra) + p_dec->fmt_in.i_extra;
225 *(p_extra++) = oggpacket.bytes >> 8;
226 *(p_extra++) = oggpacket.bytes & 0xFF;
228 memcpy( p_extra, oggpacket.packet, oggpacket.bytes );
229 p_dec->fmt_in.i_extra += oggpacket.bytes + 2;
231 block_Release( *pp_block );
236 if( p_sys->i_headers == 3 )
238 if( ProcessHeaders( p_dec ) != VLC_SUCCESS )
240 p_sys->i_headers = 0;
241 p_dec->fmt_in.i_extra = 0;
242 block_Release( *pp_block );
245 else p_sys->i_headers++;
248 return ProcessPacket( p_dec, &oggpacket, pp_block );
251 /*****************************************************************************
252 * ProcessHeaders: process Theora headers.
253 *****************************************************************************/
254 static int ProcessHeaders( decoder_t *p_dec )
256 decoder_sys_t *p_sys = p_dec->p_sys;
257 ogg_packet oggpacket;
261 if( !p_dec->fmt_in.i_extra ) return VLC_EGENERIC;
263 oggpacket.granulepos = -1;
264 oggpacket.b_o_s = 1; /* yes this actually is a b_o_s packet :) */
266 oggpacket.packetno = 0;
267 p_extra = p_dec->fmt_in.p_extra;
268 i_extra = p_dec->fmt_in.i_extra;
270 /* Take care of the initial Vorbis header */
271 oggpacket.bytes = *(p_extra++) << 8;
272 oggpacket.bytes |= (*(p_extra++) & 0xFF);
273 oggpacket.packet = p_extra;
274 p_extra += oggpacket.bytes;
275 i_extra -= (oggpacket.bytes + 2);
278 msg_Err( p_dec, "header data corrupted");
282 if( theora_decode_header( &p_sys->ti, &p_sys->tc, &oggpacket ) < 0 )
284 msg_Err( p_dec, "this bitstream does not contain Theora video data" );
288 /* Set output properties */
289 switch( p_sys->ti.pixelformat )
292 p_dec->fmt_out.i_codec = VLC_CODEC_I420;
295 p_dec->fmt_out.i_codec = VLC_CODEC_I422;
298 p_dec->fmt_out.i_codec = VLC_CODEC_I444;
302 msg_Err( p_dec, "unknown chroma in theora sample" );
305 p_dec->fmt_out.video.i_width = p_sys->ti.width;
306 p_dec->fmt_out.video.i_height = p_sys->ti.height;
307 if( p_sys->ti.frame_width && p_sys->ti.frame_height )
309 p_dec->fmt_out.video.i_visible_width = p_sys->ti.frame_width;
310 p_dec->fmt_out.video.i_visible_height = p_sys->ti.frame_height;
311 if( p_sys->ti.offset_x || p_sys->ti.offset_y )
313 p_dec->fmt_out.video.i_x_offset = p_sys->ti.offset_x;
314 p_dec->fmt_out.video.i_y_offset = p_sys->ti.offset_y;
318 if( p_sys->ti.aspect_denominator && p_sys->ti.aspect_numerator )
320 p_dec->fmt_out.video.i_aspect = ((int64_t)VOUT_ASPECT_FACTOR) *
321 ( p_sys->ti.aspect_numerator * p_dec->fmt_out.video.i_width ) /
322 ( p_sys->ti.aspect_denominator * p_dec->fmt_out.video.i_height );
326 p_dec->fmt_out.video.i_aspect = VOUT_ASPECT_FACTOR *
327 p_sys->ti.frame_width / p_sys->ti.frame_height;
330 if( p_sys->ti.fps_numerator > 0 && p_sys->ti.fps_denominator > 0 )
332 p_dec->fmt_out.video.i_frame_rate = p_sys->ti.fps_numerator;
333 p_dec->fmt_out.video.i_frame_rate_base = p_sys->ti.fps_denominator;
336 msg_Dbg( p_dec, "%dx%d %.02f fps video, frame content "
337 "is %dx%d with offset (%d,%d)",
338 p_sys->ti.width, p_sys->ti.height,
339 (double)p_sys->ti.fps_numerator/p_sys->ti.fps_denominator,
340 p_sys->ti.frame_width, p_sys->ti.frame_height,
341 p_sys->ti.offset_x, p_sys->ti.offset_y );
343 /* Sanity check that seems necessary for some corrupted files */
344 if( p_sys->ti.width < p_sys->ti.frame_width ||
345 p_sys->ti.height < p_sys->ti.frame_height )
347 msg_Warn( p_dec, "trying to correct invalid theora header "
348 "(frame size (%dx%d) is smaller than frame content (%d,%d))",
349 p_sys->ti.width, p_sys->ti.height,
350 p_sys->ti.frame_width, p_sys->ti.frame_height );
352 if( p_sys->ti.width < p_sys->ti.frame_width )
353 p_sys->ti.width = p_sys->ti.frame_width;
354 if( p_sys->ti.height < p_sys->ti.frame_height )
355 p_sys->ti.height = p_sys->ti.frame_height;
358 /* The next packet in order is the comments header */
360 oggpacket.bytes = *(p_extra++) << 8;
361 oggpacket.bytes |= (*(p_extra++) & 0xFF);
362 oggpacket.packet = p_extra;
363 p_extra += oggpacket.bytes;
364 i_extra -= (oggpacket.bytes + 2);
367 msg_Err( p_dec, "header data corrupted");
371 /* The next packet in order is the comments header */
372 if( theora_decode_header( &p_sys->ti, &p_sys->tc, &oggpacket ) < 0 )
374 msg_Err( p_dec, "2nd Theora header is corrupted" );
378 ParseTheoraComments( p_dec );
380 /* The next packet in order is the codebooks header
381 * We need to watch out that this packet is not missing as a
382 * missing or corrupted header is fatal. */
383 oggpacket.bytes = *(p_extra++) << 8;
384 oggpacket.bytes |= (*(p_extra++) & 0xFF);
385 oggpacket.packet = p_extra;
386 i_extra -= (oggpacket.bytes + 2);
389 msg_Err( p_dec, "header data corrupted");
393 /* The next packet in order is the codebooks header
394 * We need to watch out that this packet is not missing as a
395 * missing or corrupted header is fatal */
396 if( theora_decode_header( &p_sys->ti, &p_sys->tc, &oggpacket ) < 0 )
398 msg_Err( p_dec, "3rd Theora header is corrupted" );
402 if( !p_sys->b_packetizer )
404 /* We have all the headers, initialize decoder */
405 theora_decode_init( &p_sys->td, &p_sys->ti );
409 p_dec->fmt_out.i_extra = p_dec->fmt_in.i_extra;
410 p_dec->fmt_out.p_extra = realloc_or_free( p_dec->fmt_out.p_extra,
411 p_dec->fmt_out.i_extra );
412 assert( p_dec->fmt_out.p_extra );
413 memcpy( p_dec->fmt_out.p_extra,
414 p_dec->fmt_in.p_extra, p_dec->fmt_out.i_extra );
420 /*****************************************************************************
421 * ProcessPacket: processes a theora packet.
422 *****************************************************************************/
423 static void *ProcessPacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
426 decoder_sys_t *p_sys = p_dec->p_sys;
427 block_t *p_block = *pp_block;
430 if( ( p_block->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) ) != 0 )
432 /* Don't send the the first packet after a discontinuity to
433 * theora_decode, otherwise we get purple/green display artifacts
434 * appearing in the video output */
438 /* Date management */
439 if( p_block->i_pts > 0 && p_block->i_pts != p_sys->i_pts )
441 p_sys->i_pts = p_block->i_pts;
444 *pp_block = NULL; /* To avoid being fed the same packet again */
446 if( p_sys->b_packetizer )
448 /* Date management */
449 p_block->i_dts = p_block->i_pts = p_sys->i_pts;
451 if( p_sys->i_headers >= 3 )
452 p_block->i_length = p_sys->i_pts - p_block->i_pts;
454 p_block->i_length = 0;
460 if( p_sys->i_headers >= 3 )
461 p_buf = DecodePacket( p_dec, p_oggpacket );
465 if( p_block ) block_Release( p_block );
468 /* Date management */
469 p_sys->i_pts += ( INT64_C(1000000) * p_sys->ti.fps_denominator /
470 p_sys->ti.fps_numerator ); /* 1 frame per packet */
475 /*****************************************************************************
476 * DecodePacket: decodes a Theora packet.
477 *****************************************************************************/
478 static picture_t *DecodePacket( decoder_t *p_dec, ogg_packet *p_oggpacket )
480 decoder_sys_t *p_sys = p_dec->p_sys;
484 theora_decode_packetin( &p_sys->td, p_oggpacket );
486 /* Check for keyframe */
487 if( !(p_oggpacket->packet[0] & 0x80) /* data packet */ &&
488 !(p_oggpacket->packet[0] & 0x40) /* intra frame */ )
489 p_sys->b_decoded_first_keyframe = true;
491 /* If we haven't seen a single keyframe yet, don't let Theora decode
492 * anything, otherwise we'll get display artifacts. (This is impossible
493 * in the general case, but can happen if e.g. we play a network stream
494 * using a timed URL, such that the server doesn't start the video with a
496 if( p_sys->b_decoded_first_keyframe )
497 theora_decode_YUVout( &p_sys->td, &yuv );
501 /* Get a new picture */
502 p_pic = decoder_NewPicture( p_dec );
503 if( !p_pic ) return NULL;
505 theora_CopyPicture( p_pic, &yuv );
507 p_pic->date = p_sys->i_pts;
512 /*****************************************************************************
513 * ParseTheoraComments:
514 *****************************************************************************/
515 static void ParseTheoraComments( decoder_t *p_dec )
517 char *psz_name, *psz_value, *psz_comment;
520 while ( i < p_dec->p_sys->tc.comments )
522 psz_comment = strdup( p_dec->p_sys->tc.user_comments[i] );
525 psz_name = psz_comment;
526 psz_value = strchr( psz_comment, '=' );
532 if( !p_dec->p_description )
533 p_dec->p_description = vlc_meta_New();
534 if( p_dec->p_description )
535 vlc_meta_AddExtra( p_dec->p_description, psz_name, psz_value );
542 /*****************************************************************************
543 * CloseDecoder: theora decoder destruction
544 *****************************************************************************/
545 static void CloseDecoder( vlc_object_t *p_this )
547 decoder_t *p_dec = (decoder_t *)p_this;
548 decoder_sys_t *p_sys = p_dec->p_sys;
550 theora_info_clear( &p_sys->ti );
551 theora_comment_clear( &p_sys->tc );
556 /*****************************************************************************
557 * theora_CopyPicture: copy a picture from theora internal buffers to a
558 * picture_t structure.
559 *****************************************************************************/
560 static void theora_CopyPicture( picture_t *p_pic,
563 int i_plane, i_line, i_dst_stride, i_src_stride;
564 uint8_t *p_dst, *p_src;
566 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
568 p_dst = p_pic->p[i_plane].p_pixels;
569 p_src = i_plane ? (i_plane - 1 ? yuv->v : yuv->u ) : yuv->y;
570 i_dst_stride = p_pic->p[i_plane].i_pitch;
571 i_src_stride = i_plane ? yuv->uv_stride : yuv->y_stride;
573 for( i_line = 0; i_line < p_pic->p[i_plane].i_lines; i_line++ )
575 vlc_memcpy( p_dst, p_src,
576 i_plane ? yuv->uv_width : yuv->y_width );
577 p_src += i_src_stride;
578 p_dst += i_dst_stride;
583 /*****************************************************************************
584 * encoder_sys_t : theora encoder descriptor
585 *****************************************************************************/
596 theora_info ti; /* theora bitstream settings */
597 theora_comment tc; /* theora comment header */
598 theora_state td; /* theora bitstream user comments */
600 int i_width, i_height;
603 /*****************************************************************************
604 * OpenEncoder: probe the encoder and return score
605 *****************************************************************************/
606 static int OpenEncoder( vlc_object_t *p_this )
608 encoder_t *p_enc = (encoder_t *)p_this;
609 encoder_sys_t *p_sys;
614 if( p_enc->fmt_out.i_codec != VLC_CODEC_THEORA &&
620 /* Allocate the memory needed to store the decoder's structure */
621 if( ( p_sys = malloc(sizeof(encoder_sys_t)) ) == NULL )
623 p_enc->p_sys = p_sys;
625 p_enc->pf_encode_video = Encode;
626 p_enc->fmt_in.i_codec = VLC_CODEC_I420;
627 p_enc->fmt_out.i_codec = VLC_CODEC_THEORA;
629 config_ChainParse( p_enc, ENC_CFG_PREFIX, ppsz_enc_options, p_enc->p_cfg );
631 i_quality = var_GetInteger( p_enc, ENC_CFG_PREFIX "quality" );
632 if( i_quality > 10 ) i_quality = 10;
633 if( i_quality < 0 ) i_quality = 0;
635 theora_info_init( &p_sys->ti );
637 p_sys->ti.width = p_enc->fmt_in.video.i_width;
638 p_sys->ti.height = p_enc->fmt_in.video.i_height;
640 if( p_sys->ti.width % 16 || p_sys->ti.height % 16 )
642 /* Pictures from the transcoder should always have a pitch
643 * which is a multiple of 16 */
644 p_sys->ti.width = (p_sys->ti.width + 15) >> 4 << 4;
645 p_sys->ti.height = (p_sys->ti.height + 15) >> 4 << 4;
647 msg_Dbg( p_enc, "padding video from %dx%d to %dx%d",
648 p_enc->fmt_in.video.i_width, p_enc->fmt_in.video.i_height,
649 p_sys->ti.width, p_sys->ti.height );
652 p_sys->ti.frame_width = p_enc->fmt_in.video.i_width;
653 p_sys->ti.frame_height = p_enc->fmt_in.video.i_height;
654 p_sys->ti.offset_x = 0 /*frame_x_offset*/;
655 p_sys->ti.offset_y = 0 /*frame_y_offset*/;
657 p_sys->i_width = p_sys->ti.width;
658 p_sys->i_height = p_sys->ti.height;
660 if( !p_enc->fmt_in.video.i_frame_rate ||
661 !p_enc->fmt_in.video.i_frame_rate_base )
663 p_sys->ti.fps_numerator = 25;
664 p_sys->ti.fps_denominator = 1;
668 p_sys->ti.fps_numerator = p_enc->fmt_in.video.i_frame_rate;
669 p_sys->ti.fps_denominator = p_enc->fmt_in.video.i_frame_rate_base;
672 if( p_enc->fmt_in.video.i_aspect )
674 uint64_t i_num, i_den;
675 unsigned i_dst_num, i_dst_den;
677 i_num = p_enc->fmt_in.video.i_aspect * (int64_t)p_sys->ti.height;
678 i_den = VOUT_ASPECT_FACTOR * p_sys->ti.width;
679 vlc_ureduce( &i_dst_num, &i_dst_den, i_num, i_den, 0 );
680 p_sys->ti.aspect_numerator = i_dst_num;
681 p_sys->ti.aspect_denominator = i_dst_den;
685 p_sys->ti.aspect_numerator = 4;
686 p_sys->ti.aspect_denominator = 3;
689 p_sys->ti.target_bitrate = p_enc->fmt_out.i_bitrate;
690 p_sys->ti.quality = ((float)i_quality) * 6.3;
692 p_sys->ti.dropframes_p = 0;
693 p_sys->ti.quick_p = 1;
694 p_sys->ti.keyframe_auto_p = 1;
695 p_sys->ti.keyframe_frequency = 64;
696 p_sys->ti.keyframe_frequency_force = 64;
697 p_sys->ti.keyframe_data_target_bitrate = p_enc->fmt_out.i_bitrate * 1.5;
698 p_sys->ti.keyframe_auto_threshold = 80;
699 p_sys->ti.keyframe_mindistance = 8;
700 p_sys->ti.noise_sensitivity = 1;
702 theora_encode_init( &p_sys->td, &p_sys->ti );
703 theora_comment_init( &p_sys->tc );
705 /* Create and store headers */
706 p_enc->fmt_out.i_extra = 3 * 2;
707 for( i = 0; i < 3; i++ )
709 if( i == 0 ) theora_encode_header( &p_sys->td, &header );
710 else if( i == 1 ) theora_encode_comment( &p_sys->tc, &header );
711 else if( i == 2 ) theora_encode_tables( &p_sys->td, &header );
713 p_enc->fmt_out.p_extra = realloc_or_free( p_enc->fmt_out.p_extra,
714 p_enc->fmt_out.i_extra + header.bytes );
715 assert( p_enc->fmt_out.p_extra );
716 p_extra = p_enc->fmt_out.p_extra;
717 p_extra += p_enc->fmt_out.i_extra + (i-3)*2;
718 p_enc->fmt_out.i_extra += header.bytes;
720 *(p_extra++) = header.bytes >> 8;
721 *(p_extra++) = header.bytes & 0xFF;
723 memcpy( p_extra, header.packet, header.bytes );
729 /****************************************************************************
730 * Encode: the whole thing
731 ****************************************************************************
732 * This function spits out ogg packets.
733 ****************************************************************************/
734 static block_t *Encode( encoder_t *p_enc, picture_t *p_pict )
736 encoder_sys_t *p_sys = p_enc->p_sys;
737 ogg_packet oggpacket;
743 if( p_pict->p[0].i_pitch < (int)p_sys->i_width ||
744 p_pict->p[0].i_lines < (int)p_sys->i_height )
746 msg_Warn( p_enc, "frame is smaller than encoding size"
747 "(%ix%i->%ix%i) -> dropping frame",
748 p_pict->p[0].i_pitch, p_pict->p[0].i_lines,
749 p_sys->i_width, p_sys->i_height );
754 if( p_pict->p[0].i_visible_pitch < (int)p_sys->i_width )
756 for( i = 0; i < p_sys->i_height; i++ )
758 memset( p_pict->p[0].p_pixels + i * p_pict->p[0].i_pitch +
759 p_pict->p[0].i_visible_pitch,
760 *( p_pict->p[0].p_pixels + i * p_pict->p[0].i_pitch +
761 p_pict->p[0].i_visible_pitch - 1 ),
762 p_sys->i_width - p_pict->p[0].i_visible_pitch );
764 for( i = 0; i < p_sys->i_height / 2; i++ )
766 memset( p_pict->p[1].p_pixels + i * p_pict->p[1].i_pitch +
767 p_pict->p[1].i_visible_pitch,
768 *( p_pict->p[1].p_pixels + i * p_pict->p[1].i_pitch +
769 p_pict->p[1].i_visible_pitch - 1 ),
770 p_sys->i_width / 2 - p_pict->p[1].i_visible_pitch );
771 memset( p_pict->p[2].p_pixels + i * p_pict->p[2].i_pitch +
772 p_pict->p[2].i_visible_pitch,
773 *( p_pict->p[2].p_pixels + i * p_pict->p[2].i_pitch +
774 p_pict->p[2].i_visible_pitch - 1 ),
775 p_sys->i_width / 2 - p_pict->p[2].i_visible_pitch );
779 if( p_pict->p[0].i_visible_lines < (int)p_sys->i_height )
781 for( i = p_pict->p[0].i_visible_lines; i < p_sys->i_height; i++ )
783 memset( p_pict->p[0].p_pixels + i * p_pict->p[0].i_pitch, 0,
786 for( i = p_pict->p[1].i_visible_lines; i < p_sys->i_height / 2; i++ )
788 memset( p_pict->p[1].p_pixels + i * p_pict->p[1].i_pitch, 0x80,
789 p_sys->i_width / 2 );
790 memset( p_pict->p[2].p_pixels + i * p_pict->p[2].i_pitch, 0x80,
791 p_sys->i_width / 2 );
795 /* Theora is a one-frame-in, one-frame-out system. Submit a frame
796 * for compression and pull out the packet. */
798 yuv.y_width = p_sys->i_width;
799 yuv.y_height = p_sys->i_height;
800 yuv.y_stride = p_pict->p[0].i_pitch;
802 yuv.uv_width = p_sys->i_width / 2;
803 yuv.uv_height = p_sys->i_height / 2;
804 yuv.uv_stride = p_pict->p[1].i_pitch;
806 yuv.y = p_pict->p[0].p_pixels;
807 yuv.u = p_pict->p[1].p_pixels;
808 yuv.v = p_pict->p[2].p_pixels;
810 if( theora_encode_YUVin( &p_sys->td, &yuv ) < 0 )
812 msg_Warn( p_enc, "failed encoding a frame" );
816 theora_encode_packetout( &p_sys->td, 0, &oggpacket );
818 /* Ogg packet to block */
819 p_block = block_New( p_enc, oggpacket.bytes );
820 memcpy( p_block->p_buffer, oggpacket.packet, oggpacket.bytes );
821 p_block->i_dts = p_block->i_pts = p_pict->date;
823 if( theora_packet_iskeyframe( &oggpacket ) )
825 p_block->i_flags |= BLOCK_FLAG_TYPE_I;
831 /*****************************************************************************
832 * CloseEncoder: theora encoder destruction
833 *****************************************************************************/
834 static void CloseEncoder( vlc_object_t *p_this )
836 encoder_t *p_enc = (encoder_t *)p_this;
837 encoder_sys_t *p_sys = p_enc->p_sys;
839 theora_info_clear( &p_sys->ti );
840 theora_comment_clear( &p_sys->tc );