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 *****************************************************************************/
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_codec.h>
36 #include <vlc_input.h>
39 #include <theora/theora.h>
41 /*****************************************************************************
42 * decoder_sys_t : theora decoder descriptor
43 *****************************************************************************/
57 theora_info ti; /* theora bitstream settings */
58 theora_comment tc; /* theora comment header */
59 theora_state td; /* theora bitstream user comments */
64 bool b_decoded_first_keyframe;
72 /*****************************************************************************
74 *****************************************************************************/
75 static int OpenDecoder ( vlc_object_t * );
76 static int OpenPacketizer( vlc_object_t * );
77 static void CloseDecoder ( vlc_object_t * );
79 static void *DecodeBlock ( decoder_t *, block_t ** );
80 static int ProcessHeaders( decoder_t * );
81 static void *ProcessPacket ( decoder_t *, ogg_packet *, block_t ** );
83 static picture_t *DecodePacket( decoder_t *, ogg_packet * );
85 static void ParseTheoraComments( decoder_t * );
86 static void theora_CopyPicture( decoder_t *, picture_t *, yuv_buffer * );
88 static int OpenEncoder( vlc_object_t *p_this );
89 static void CloseEncoder( vlc_object_t *p_this );
90 static block_t *Encode( encoder_t *p_enc, picture_t *p_pict );
92 /*****************************************************************************
94 *****************************************************************************/
95 #define ENC_QUALITY_TEXT N_("Encoding quality")
96 #define ENC_QUALITY_LONGTEXT N_( \
97 "Enforce a quality between 1 (low) and 10 (high), instead " \
98 "of specifying a particular bitrate. This will produce a VBR stream." )
101 set_category( CAT_INPUT );
102 set_subcategory( SUBCAT_INPUT_VCODEC );
103 set_shortname( "Theora" );
104 set_description( N_("Theora video decoder") );
105 set_capability( "decoder", 100 );
106 set_callbacks( OpenDecoder, CloseDecoder );
107 add_shortcut( "theora" );
110 set_description( N_("Theora video packetizer") );
111 set_capability( "packetizer", 100 );
112 set_callbacks( OpenPacketizer, CloseDecoder );
115 set_description( N_("Theora video encoder") );
116 set_capability( "encoder", 150 );
117 set_callbacks( OpenEncoder, CloseEncoder );
119 # define ENC_CFG_PREFIX "sout-theora-"
120 add_integer( ENC_CFG_PREFIX "quality", 2, NULL, ENC_QUALITY_TEXT,
121 ENC_QUALITY_LONGTEXT, false );
124 static const char *const ppsz_enc_options[] = {
128 /*****************************************************************************
129 * OpenDecoder: probe the decoder and return score
130 *****************************************************************************/
131 static int OpenDecoder( vlc_object_t *p_this )
133 decoder_t *p_dec = (decoder_t*)p_this;
134 decoder_sys_t *p_sys;
136 if( p_dec->fmt_in.i_codec != VLC_FOURCC('t','h','e','o') )
141 /* Allocate the memory needed to store the decoder's structure */
142 if( ( p_dec->p_sys = p_sys =
143 (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
145 p_dec->p_sys->b_packetizer = false;
148 p_sys->b_decoded_first_keyframe = false;
150 /* Set output properties */
151 p_dec->fmt_out.i_cat = VIDEO_ES;
152 p_dec->fmt_out.i_codec = VLC_FOURCC('I','4','2','0');
155 p_dec->pf_decode_video = (picture_t *(*)(decoder_t *, block_t **))
157 p_dec->pf_packetize = (block_t *(*)(decoder_t *, block_t **))
160 /* Init supporting Theora structures needed in header parsing */
161 theora_comment_init( &p_sys->tc );
162 theora_info_init( &p_sys->ti );
164 p_sys->i_headers = 0;
169 static int OpenPacketizer( vlc_object_t *p_this )
171 decoder_t *p_dec = (decoder_t*)p_this;
173 int i_ret = OpenDecoder( p_this );
175 if( i_ret == VLC_SUCCESS )
177 p_dec->p_sys->b_packetizer = true;
178 p_dec->fmt_out.i_codec = VLC_FOURCC( 't', 'h', 'e', 'o' );
184 /****************************************************************************
185 * DecodeBlock: the whole thing
186 ****************************************************************************
187 * This function must be fed with ogg packets.
188 ****************************************************************************/
189 static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
191 decoder_sys_t *p_sys = p_dec->p_sys;
193 ogg_packet oggpacket;
195 if( !pp_block || !*pp_block ) return NULL;
199 /* Block to Ogg packet */
200 oggpacket.packet = p_block->p_buffer;
201 oggpacket.bytes = p_block->i_buffer;
202 oggpacket.granulepos = p_block->i_dts;
205 oggpacket.packetno = 0;
207 /* Check for headers */
208 if( p_sys->i_headers == 0 && p_dec->fmt_in.i_extra )
210 /* Headers already available as extra data */
211 p_sys->i_headers = 3;
213 else if( oggpacket.bytes && p_sys->i_headers < 3 )
215 /* Backup headers as extra data */
218 p_dec->fmt_in.p_extra =
219 realloc( p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra +
220 oggpacket.bytes + 2 );
221 p_extra = ((uint8_t *)p_dec->fmt_in.p_extra) + p_dec->fmt_in.i_extra;
222 *(p_extra++) = oggpacket.bytes >> 8;
223 *(p_extra++) = oggpacket.bytes & 0xFF;
225 memcpy( p_extra, oggpacket.packet, oggpacket.bytes );
226 p_dec->fmt_in.i_extra += oggpacket.bytes + 2;
228 block_Release( *pp_block );
233 if( p_sys->i_headers == 3 )
235 if( ProcessHeaders( p_dec ) != VLC_SUCCESS )
237 p_sys->i_headers = 0;
238 p_dec->fmt_in.i_extra = 0;
239 block_Release( *pp_block );
242 else p_sys->i_headers++;
245 return ProcessPacket( p_dec, &oggpacket, pp_block );
248 /*****************************************************************************
249 * ProcessHeaders: process Theora headers.
250 *****************************************************************************/
251 static int ProcessHeaders( decoder_t *p_dec )
253 decoder_sys_t *p_sys = p_dec->p_sys;
254 ogg_packet oggpacket;
258 if( !p_dec->fmt_in.i_extra ) return VLC_EGENERIC;
260 oggpacket.granulepos = -1;
261 oggpacket.b_o_s = 1; /* yes this actually is a b_o_s packet :) */
263 oggpacket.packetno = 0;
264 p_extra = p_dec->fmt_in.p_extra;
265 i_extra = p_dec->fmt_in.i_extra;
267 /* Take care of the initial Vorbis header */
268 oggpacket.bytes = *(p_extra++) << 8;
269 oggpacket.bytes |= (*(p_extra++) & 0xFF);
270 oggpacket.packet = p_extra;
271 p_extra += oggpacket.bytes;
272 i_extra -= (oggpacket.bytes + 2);
275 msg_Err( p_dec, "header data corrupted");
279 if( theora_decode_header( &p_sys->ti, &p_sys->tc, &oggpacket ) < 0 )
281 msg_Err( p_dec, "this bitstream does not contain Theora video data" );
285 /* Set output properties */
286 switch( p_sys->ti.pixelformat )
289 p_dec->fmt_out.i_codec = VLC_FOURCC( 'I','4','2','0' );
292 p_dec->fmt_out.i_codec = VLC_FOURCC( 'I','4','2','2' );
295 p_dec->fmt_out.i_codec = VLC_FOURCC( 'I','4','4','4' );
299 msg_Err( p_dec, "unknown chroma in theora sample" );
302 p_dec->fmt_out.video.i_width = p_sys->ti.width;
303 p_dec->fmt_out.video.i_height = p_sys->ti.height;
304 if( p_sys->ti.frame_width && p_sys->ti.frame_height )
306 p_dec->fmt_out.video.i_visible_width = p_sys->ti.frame_width;
307 p_dec->fmt_out.video.i_visible_height = p_sys->ti.frame_height;
308 if( p_sys->ti.offset_x || p_sys->ti.offset_y )
310 p_dec->fmt_out.video.i_x_offset = p_sys->ti.offset_x;
311 p_dec->fmt_out.video.i_y_offset = p_sys->ti.offset_y;
315 if( p_sys->ti.aspect_denominator && p_sys->ti.aspect_numerator )
317 p_dec->fmt_out.video.i_aspect = ((int64_t)VOUT_ASPECT_FACTOR) *
318 ( p_sys->ti.aspect_numerator * p_dec->fmt_out.video.i_width ) /
319 ( p_sys->ti.aspect_denominator * p_dec->fmt_out.video.i_height );
323 p_dec->fmt_out.video.i_aspect = VOUT_ASPECT_FACTOR *
324 p_sys->ti.frame_width / p_sys->ti.frame_height;
327 if( p_sys->ti.fps_numerator > 0 && p_sys->ti.fps_denominator > 0 )
329 p_dec->fmt_out.video.i_frame_rate = p_sys->ti.fps_numerator;
330 p_dec->fmt_out.video.i_frame_rate_base = p_sys->ti.fps_denominator;
333 msg_Dbg( p_dec, "%dx%d %.02f fps video, frame content "
334 "is %dx%d with offset (%d,%d)",
335 p_sys->ti.width, p_sys->ti.height,
336 (double)p_sys->ti.fps_numerator/p_sys->ti.fps_denominator,
337 p_sys->ti.frame_width, p_sys->ti.frame_height,
338 p_sys->ti.offset_x, p_sys->ti.offset_y );
340 /* Sanity check that seems necessary for some corrupted files */
341 if( p_sys->ti.width < p_sys->ti.frame_width ||
342 p_sys->ti.height < p_sys->ti.frame_height )
344 msg_Warn( p_dec, "trying to correct invalid theora header "
345 "(frame size (%dx%d) is smaller than frame content (%d,%d))",
346 p_sys->ti.width, p_sys->ti.height,
347 p_sys->ti.frame_width, p_sys->ti.frame_height );
349 if( p_sys->ti.width < p_sys->ti.frame_width )
350 p_sys->ti.width = p_sys->ti.frame_width;
351 if( p_sys->ti.height < p_sys->ti.frame_height )
352 p_sys->ti.height = p_sys->ti.frame_height;
355 /* The next packet in order is the comments header */
357 oggpacket.bytes = *(p_extra++) << 8;
358 oggpacket.bytes |= (*(p_extra++) & 0xFF);
359 oggpacket.packet = p_extra;
360 p_extra += oggpacket.bytes;
361 i_extra -= (oggpacket.bytes + 2);
364 msg_Err( p_dec, "header data corrupted");
368 /* The next packet in order is the comments header */
369 if( theora_decode_header( &p_sys->ti, &p_sys->tc, &oggpacket ) < 0 )
371 msg_Err( p_dec, "2nd Theora header is corrupted" );
375 ParseTheoraComments( p_dec );
377 /* The next packet in order is the codebooks header
378 * We need to watch out that this packet is not missing as a
379 * missing or corrupted header is fatal. */
380 oggpacket.bytes = *(p_extra++) << 8;
381 oggpacket.bytes |= (*(p_extra++) & 0xFF);
382 oggpacket.packet = p_extra;
383 i_extra -= (oggpacket.bytes + 2);
386 msg_Err( p_dec, "header data corrupted");
390 /* The next packet in order is the codebooks header
391 * We need to watch out that this packet is not missing as a
392 * missing or corrupted header is fatal */
393 if( theora_decode_header( &p_sys->ti, &p_sys->tc, &oggpacket ) < 0 )
395 msg_Err( p_dec, "3rd Theora header is corrupted" );
399 if( !p_sys->b_packetizer )
401 /* We have all the headers, initialize decoder */
402 theora_decode_init( &p_sys->td, &p_sys->ti );
406 p_dec->fmt_out.i_extra = p_dec->fmt_in.i_extra;
407 p_dec->fmt_out.p_extra =
408 realloc( p_dec->fmt_out.p_extra, p_dec->fmt_out.i_extra );
409 memcpy( p_dec->fmt_out.p_extra,
410 p_dec->fmt_in.p_extra, p_dec->fmt_out.i_extra );
416 /*****************************************************************************
417 * ProcessPacket: processes a theora packet.
418 *****************************************************************************/
419 static void *ProcessPacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
422 decoder_sys_t *p_sys = p_dec->p_sys;
423 block_t *p_block = *pp_block;
426 if( ( p_block->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) ) != 0 )
428 /* Don't send the the first packet after a discontinuity to
429 * theora_decode, otherwise we get purple/green display artifacts
430 * appearing in the video output */
434 /* Date management */
435 if( p_block->i_pts > 0 && p_block->i_pts != p_sys->i_pts )
437 p_sys->i_pts = p_block->i_pts;
440 *pp_block = NULL; /* To avoid being fed the same packet again */
442 if( p_sys->b_packetizer )
444 /* Date management */
445 p_block->i_dts = p_block->i_pts = p_sys->i_pts;
447 if( p_sys->i_headers >= 3 )
448 p_block->i_length = p_sys->i_pts - p_block->i_pts;
450 p_block->i_length = 0;
456 if( p_sys->i_headers >= 3 )
457 p_buf = DecodePacket( p_dec, p_oggpacket );
461 if( p_block ) block_Release( p_block );
464 /* Date management */
465 p_sys->i_pts += ( INT64_C(1000000) * p_sys->ti.fps_denominator /
466 p_sys->ti.fps_numerator ); /* 1 frame per packet */
471 /*****************************************************************************
472 * DecodePacket: decodes a Theora packet.
473 *****************************************************************************/
474 static picture_t *DecodePacket( decoder_t *p_dec, ogg_packet *p_oggpacket )
476 decoder_sys_t *p_sys = p_dec->p_sys;
480 theora_decode_packetin( &p_sys->td, p_oggpacket );
482 /* Check for keyframe */
483 if( !(p_oggpacket->packet[0] & 0x80) /* data packet */ &&
484 !(p_oggpacket->packet[0] & 0x40) /* intra frame */ )
485 p_sys->b_decoded_first_keyframe = true;
487 /* If we haven't seen a single keyframe yet, don't let Theora decode
488 * anything, otherwise we'll get display artifacts. (This is impossible
489 * in the general case, but can happen if e.g. we play a network stream
490 * using a timed URL, such that the server doesn't start the video with a
492 if( p_sys->b_decoded_first_keyframe )
493 theora_decode_YUVout( &p_sys->td, &yuv );
497 /* Get a new picture */
498 p_pic = p_dec->pf_vout_buffer_new( p_dec );
499 if( !p_pic ) return NULL;
501 theora_CopyPicture( p_dec, p_pic, &yuv );
503 p_pic->date = p_sys->i_pts;
508 /*****************************************************************************
509 * ParseTheoraComments: FIXME should be done in demuxer
510 *****************************************************************************/
511 static void ParseTheoraComments( decoder_t *p_dec )
513 input_thread_t *p_input = (input_thread_t *)p_dec->p_parent;
514 char *psz_name, *psz_value, *psz_comment;
517 if( p_input->i_object_type != VLC_OBJECT_INPUT ) return;
519 while ( i < p_dec->p_sys->tc.comments )
521 psz_comment = strdup( p_dec->p_sys->tc.user_comments[i] );
524 msg_Warn( p_dec, "out of memory" );
527 psz_name = psz_comment;
528 psz_value = strchr( psz_comment, '=' );
533 input_Control( p_input, INPUT_ADD_INFO, _("Theora comment"),
534 psz_name, "%s", psz_value );
541 /*****************************************************************************
542 * CloseDecoder: theora decoder destruction
543 *****************************************************************************/
544 static void CloseDecoder( vlc_object_t *p_this )
546 decoder_t *p_dec = (decoder_t *)p_this;
547 decoder_sys_t *p_sys = p_dec->p_sys;
549 theora_info_clear( &p_sys->ti );
550 theora_comment_clear( &p_sys->tc );
555 /*****************************************************************************
556 * theora_CopyPicture: copy a picture from theora internal buffers to a
557 * picture_t structure.
558 *****************************************************************************/
559 static void theora_CopyPicture( decoder_t *p_dec, picture_t *p_pic,
562 int i_plane, i_line, i_dst_stride, i_src_stride;
563 uint8_t *p_dst, *p_src;
565 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
567 p_dst = p_pic->p[i_plane].p_pixels;
568 p_src = i_plane ? (i_plane - 1 ? yuv->v : yuv->u ) : yuv->y;
569 i_dst_stride = p_pic->p[i_plane].i_pitch;
570 i_src_stride = i_plane ? yuv->uv_stride : yuv->y_stride;
572 for( i_line = 0; i_line < p_pic->p[i_plane].i_lines; i_line++ )
574 vlc_memcpy( p_dst, p_src,
575 i_plane ? yuv->uv_width : yuv->y_width );
576 p_src += i_src_stride;
577 p_dst += i_dst_stride;
582 /*****************************************************************************
583 * encoder_sys_t : theora encoder descriptor
584 *****************************************************************************/
595 theora_info ti; /* theora bitstream settings */
596 theora_comment tc; /* theora comment header */
597 theora_state td; /* theora bitstream user comments */
599 int i_width, i_height;
602 /*****************************************************************************
603 * OpenEncoder: probe the encoder and return score
604 *****************************************************************************/
605 static int OpenEncoder( vlc_object_t *p_this )
607 encoder_t *p_enc = (encoder_t *)p_this;
608 encoder_sys_t *p_sys = p_enc->p_sys;
614 if( p_enc->fmt_out.i_codec != VLC_FOURCC('t','h','e','o') &&
620 /* Allocate the memory needed to store the decoder's structure */
621 if( ( p_sys = (encoder_sys_t *)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_FOURCC('I','4','2','0');
627 p_enc->fmt_out.i_codec = VLC_FOURCC('t','h','e','o');
629 config_ChainParse( p_enc, ENC_CFG_PREFIX, ppsz_enc_options, p_enc->p_cfg );
631 var_Get( p_enc, ENC_CFG_PREFIX "quality", &val );
632 i_quality = val.i_int;
633 if( i_quality > 10 ) i_quality = 10;
634 if( i_quality < 0 ) i_quality = 0;
636 theora_info_init( &p_sys->ti );
638 p_sys->ti.width = p_enc->fmt_in.video.i_width;
639 p_sys->ti.height = p_enc->fmt_in.video.i_height;
641 if( p_sys->ti.width % 16 || p_sys->ti.height % 16 )
643 /* Pictures from the transcoder should always have a pitch
644 * which is a multiple of 16 */
645 p_sys->ti.width = (p_sys->ti.width + 15) >> 4 << 4;
646 p_sys->ti.height = (p_sys->ti.height + 15) >> 4 << 4;
648 msg_Dbg( p_enc, "padding video from %dx%d to %dx%d",
649 p_enc->fmt_in.video.i_width, p_enc->fmt_in.video.i_height,
650 p_sys->ti.width, p_sys->ti.height );
653 p_sys->ti.frame_width = p_enc->fmt_in.video.i_width;
654 p_sys->ti.frame_height = p_enc->fmt_in.video.i_height;
655 p_sys->ti.offset_x = 0 /*frame_x_offset*/;
656 p_sys->ti.offset_y = 0 /*frame_y_offset*/;
658 p_sys->i_width = p_sys->ti.width;
659 p_sys->i_height = p_sys->ti.height;
661 if( !p_enc->fmt_in.video.i_frame_rate ||
662 !p_enc->fmt_in.video.i_frame_rate_base )
664 p_sys->ti.fps_numerator = 25;
665 p_sys->ti.fps_denominator = 1;
669 p_sys->ti.fps_numerator = p_enc->fmt_in.video.i_frame_rate;
670 p_sys->ti.fps_denominator = p_enc->fmt_in.video.i_frame_rate_base;
673 if( p_enc->fmt_in.video.i_aspect )
675 uint64_t i_num, i_den;
676 unsigned i_dst_num, i_dst_den;
678 i_num = p_enc->fmt_in.video.i_aspect * (int64_t)p_sys->ti.height;
679 i_den = VOUT_ASPECT_FACTOR * p_sys->ti.width;
680 vlc_ureduce( &i_dst_num, &i_dst_den, i_num, i_den, 0 );
681 p_sys->ti.aspect_numerator = i_dst_num;
682 p_sys->ti.aspect_denominator = i_dst_den;
686 p_sys->ti.aspect_numerator = 4;
687 p_sys->ti.aspect_denominator = 3;
690 p_sys->ti.target_bitrate = p_enc->fmt_out.i_bitrate;
691 p_sys->ti.quality = ((float)i_quality) * 6.3;
693 p_sys->ti.dropframes_p = 0;
694 p_sys->ti.quick_p = 1;
695 p_sys->ti.keyframe_auto_p = 1;
696 p_sys->ti.keyframe_frequency = 64;
697 p_sys->ti.keyframe_frequency_force = 64;
698 p_sys->ti.keyframe_data_target_bitrate = p_enc->fmt_out.i_bitrate * 1.5;
699 p_sys->ti.keyframe_auto_threshold = 80;
700 p_sys->ti.keyframe_mindistance = 8;
701 p_sys->ti.noise_sensitivity = 1;
703 theora_encode_init( &p_sys->td, &p_sys->ti );
704 theora_info_clear( &p_sys->ti );
705 theora_comment_init( &p_sys->tc );
707 /* Create and store headers */
708 p_enc->fmt_out.i_extra = 3 * 2;
709 for( i = 0; i < 3; i++ )
711 if( i == 0 ) theora_encode_header( &p_sys->td, &header );
712 else if( i == 1 ) theora_encode_comment( &p_sys->tc, &header );
713 else if( i == 2 ) theora_encode_tables( &p_sys->td, &header );
715 p_enc->fmt_out.p_extra =
716 realloc( p_enc->fmt_out.p_extra,
717 p_enc->fmt_out.i_extra + header.bytes );
718 p_extra = p_enc->fmt_out.p_extra;
719 p_extra += p_enc->fmt_out.i_extra + (i-3)*2;
720 p_enc->fmt_out.i_extra += header.bytes;
722 *(p_extra++) = header.bytes >> 8;
723 *(p_extra++) = header.bytes & 0xFF;
725 memcpy( p_extra, header.packet, header.bytes );
731 /****************************************************************************
732 * Encode: the whole thing
733 ****************************************************************************
734 * This function spits out ogg packets.
735 ****************************************************************************/
736 static block_t *Encode( encoder_t *p_enc, picture_t *p_pict )
738 encoder_sys_t *p_sys = p_enc->p_sys;
739 ogg_packet oggpacket;
745 if( p_pict->p[0].i_pitch < (int)p_sys->i_width ||
746 p_pict->p[0].i_lines < (int)p_sys->i_height )
748 msg_Warn( p_enc, "frame is smaller than encoding size"
749 "(%ix%i->%ix%i) -> dropping frame",
750 p_pict->p[0].i_pitch, p_pict->p[0].i_lines,
751 p_sys->i_width, p_sys->i_height );
756 if( p_pict->p[0].i_visible_pitch < (int)p_sys->i_width )
758 for( i = 0; i < p_sys->i_height; i++ )
760 memset( p_pict->p[0].p_pixels + i * p_pict->p[0].i_pitch +
761 p_pict->p[0].i_visible_pitch,
762 *( p_pict->p[0].p_pixels + i * p_pict->p[0].i_pitch +
763 p_pict->p[0].i_visible_pitch - 1 ),
764 p_sys->i_width - p_pict->p[0].i_visible_pitch );
766 for( i = 0; i < p_sys->i_height / 2; i++ )
768 memset( p_pict->p[1].p_pixels + i * p_pict->p[1].i_pitch +
769 p_pict->p[1].i_visible_pitch,
770 *( p_pict->p[1].p_pixels + i * p_pict->p[1].i_pitch +
771 p_pict->p[1].i_visible_pitch - 1 ),
772 p_sys->i_width / 2 - p_pict->p[1].i_visible_pitch );
773 memset( p_pict->p[2].p_pixels + i * p_pict->p[2].i_pitch +
774 p_pict->p[2].i_visible_pitch,
775 *( p_pict->p[2].p_pixels + i * p_pict->p[2].i_pitch +
776 p_pict->p[2].i_visible_pitch - 1 ),
777 p_sys->i_width / 2 - p_pict->p[2].i_visible_pitch );
781 if( p_pict->p[0].i_visible_lines < (int)p_sys->i_height )
783 for( i = p_pict->p[0].i_visible_lines; i < p_sys->i_height; i++ )
785 memset( p_pict->p[0].p_pixels + i * p_pict->p[0].i_pitch, 0,
788 for( i = p_pict->p[1].i_visible_lines; i < p_sys->i_height / 2; i++ )
790 memset( p_pict->p[1].p_pixels + i * p_pict->p[1].i_pitch, 0x80,
791 p_sys->i_width / 2 );
792 memset( p_pict->p[2].p_pixels + i * p_pict->p[2].i_pitch, 0x80,
793 p_sys->i_width / 2 );
797 /* Theora is a one-frame-in, one-frame-out system. Submit a frame
798 * for compression and pull out the packet. */
800 yuv.y_width = p_sys->i_width;
801 yuv.y_height = p_sys->i_height;
802 yuv.y_stride = p_pict->p[0].i_pitch;
804 yuv.uv_width = p_sys->i_width / 2;
805 yuv.uv_height = p_sys->i_height / 2;
806 yuv.uv_stride = p_pict->p[1].i_pitch;
808 yuv.y = p_pict->p[0].p_pixels;
809 yuv.u = p_pict->p[1].p_pixels;
810 yuv.v = p_pict->p[2].p_pixels;
812 if( theora_encode_YUVin( &p_sys->td, &yuv ) < 0 )
814 msg_Warn( p_enc, "failed encoding a frame" );
818 theora_encode_packetout( &p_sys->td, 0, &oggpacket );
820 /* Ogg packet to block */
821 p_block = block_New( p_enc, oggpacket.bytes );
822 memcpy( p_block->p_buffer, oggpacket.packet, oggpacket.bytes );
823 p_block->i_dts = p_block->i_pts = p_pict->date;
828 /*****************************************************************************
829 * CloseEncoder: theora encoder destruction
830 *****************************************************************************/
831 static void CloseEncoder( vlc_object_t *p_this )
833 encoder_t *p_enc = (encoder_t *)p_this;
834 encoder_sys_t *p_sys = p_enc->p_sys;
836 theora_info_clear( &p_sys->ti );
837 theora_comment_clear( &p_sys->tc );