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 if( theora_packet_iskeyframe( p_oggpacket ) == 1 )
446 p_sys->b_decoded_first_keyframe = VLC_TRUE;
448 /* If we haven't seen a single keyframe yet, don't let Theora decode
449 * anything, otherwise we'll get display artifacts. (This is impossible
450 * in the general case, but can happen if e.g. we play a network stream
451 * using a timed URL, such that the server doesn't start the video with a
453 if( p_sys->b_decoded_first_keyframe )
454 theora_decode_YUVout( &p_sys->td, &yuv );
458 /* Get a new picture */
459 p_pic = p_dec->pf_vout_buffer_new( p_dec );
460 if( !p_pic ) return NULL;
462 theora_CopyPicture( p_dec, p_pic, &yuv );
464 p_pic->date = p_sys->i_pts;
469 /*****************************************************************************
470 * ParseTheoraComments: FIXME should be done in demuxer
471 *****************************************************************************/
472 static void ParseTheoraComments( decoder_t *p_dec )
474 input_thread_t *p_input = (input_thread_t *)p_dec->p_parent;
475 char *psz_name, *psz_value, *psz_comment;
478 if( p_input->i_object_type != VLC_OBJECT_INPUT ) return;
480 while ( i < p_dec->p_sys->tc.comments )
482 psz_comment = strdup( p_dec->p_sys->tc.user_comments[i] );
485 msg_Warn( p_dec, "out of memory" );
488 psz_name = psz_comment;
489 psz_value = strchr( psz_comment, '=' );
494 input_Control( p_input, INPUT_ADD_INFO, _("Theora comment"),
495 psz_name, psz_value );
502 /*****************************************************************************
503 * CloseDecoder: theora decoder destruction
504 *****************************************************************************/
505 static void CloseDecoder( vlc_object_t *p_this )
507 decoder_t *p_dec = (decoder_t *)p_this;
508 decoder_sys_t *p_sys = p_dec->p_sys;
510 theora_info_clear( &p_sys->ti );
511 theora_comment_clear( &p_sys->tc );
516 /*****************************************************************************
517 * theora_CopyPicture: copy a picture from theora internal buffers to a
518 * picture_t structure.
519 *****************************************************************************/
520 static void theora_CopyPicture( decoder_t *p_dec, picture_t *p_pic,
523 int i_plane, i_line, i_width, i_dst_stride, i_src_stride;
524 int i_src_xoffset, i_src_yoffset;
525 uint8_t *p_dst, *p_src;
527 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
529 p_dst = p_pic->p[i_plane].p_pixels;
530 p_src = i_plane ? (i_plane - 1 ? yuv->v : yuv->u ) : yuv->y;
531 i_width = p_pic->p[i_plane].i_visible_pitch;
532 i_dst_stride = p_pic->p[i_plane].i_pitch;
533 i_src_stride = i_plane ? yuv->uv_stride : yuv->y_stride;
534 i_src_xoffset = p_dec->p_sys->ti.offset_x;
535 i_src_yoffset = p_dec->p_sys->ti.offset_y;
542 p_src += (i_src_yoffset * i_src_stride + i_src_yoffset);
544 for( i_line = 0; i_line < p_pic->p[i_plane].i_visible_lines; i_line++ )
546 p_dec->p_vlc->pf_memcpy( p_dst, p_src, i_width );
547 p_src += i_src_stride;
548 p_dst += i_dst_stride;
553 /*****************************************************************************
554 * encoder_sys_t : theora encoder descriptor
555 *****************************************************************************/
561 vlc_bool_t b_headers;
566 theora_info ti; /* theora bitstream settings */
567 theora_comment tc; /* theora comment header */
568 theora_state td; /* theora bitstream user comments */
570 int i_width, i_height;
573 /*****************************************************************************
574 * OpenEncoder: probe the encoder and return score
575 *****************************************************************************/
576 static int OpenEncoder( vlc_object_t *p_this )
578 encoder_t *p_enc = (encoder_t *)p_this;
579 encoder_sys_t *p_sys = p_enc->p_sys;
585 if( p_enc->fmt_out.i_codec != VLC_FOURCC('t','h','e','o') &&
591 /* Allocate the memory needed to store the decoder's structure */
592 if( ( p_sys = (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
594 msg_Err( p_enc, "out of memory" );
597 p_enc->p_sys = p_sys;
599 p_enc->pf_encode_video = Encode;
600 p_enc->fmt_in.i_codec = VLC_FOURCC('I','4','2','0');
601 p_enc->fmt_out.i_codec = VLC_FOURCC('t','h','e','o');
603 sout_CfgParse( p_enc, ENC_CFG_PREFIX, ppsz_enc_options, p_enc->p_cfg );
605 var_Get( p_enc, ENC_CFG_PREFIX "quality", &val );
606 i_quality = val.i_int;
607 if( i_quality > 10 ) i_quality = 10;
608 if( i_quality < 0 ) i_quality = 0;
610 theora_info_init( &p_sys->ti );
612 p_sys->ti.width = p_enc->fmt_in.video.i_width;
613 p_sys->ti.height = p_enc->fmt_in.video.i_height;
615 if( p_sys->ti.width % 16 || p_sys->ti.height % 16 )
617 /* Pictures from the transcoder should always have a pitch
618 * which is a multiple of 16 */
619 p_sys->ti.width = (p_sys->ti.width + 15) >> 4 << 4;
620 p_sys->ti.height = (p_sys->ti.height + 15) >> 4 << 4;
622 msg_Dbg( p_enc, "padding video from %dx%d to %dx%d",
623 p_enc->fmt_in.video.i_width, p_enc->fmt_in.video.i_height,
624 p_sys->ti.width, p_sys->ti.height );
627 p_sys->ti.frame_width = p_enc->fmt_in.video.i_width;
628 p_sys->ti.frame_height = p_enc->fmt_in.video.i_height;
629 p_sys->ti.offset_x = 0 /*frame_x_offset*/;
630 p_sys->ti.offset_y = 0 /*frame_y_offset*/;
632 p_sys->i_width = p_sys->ti.width;
633 p_sys->i_height = p_sys->ti.height;
635 if( !p_enc->fmt_in.video.i_frame_rate ||
636 !p_enc->fmt_in.video.i_frame_rate_base )
638 p_sys->ti.fps_numerator = 25;
639 p_sys->ti.fps_denominator = 1;
643 p_sys->ti.fps_numerator = p_enc->fmt_in.video.i_frame_rate;
644 p_sys->ti.fps_denominator = p_enc->fmt_in.video.i_frame_rate_base;
647 if( p_enc->fmt_in.video.i_aspect )
649 int64_t i_num, i_den;
650 int i_dst_num, i_dst_den;
652 i_num = p_enc->fmt_in.video.i_aspect * (int64_t)p_sys->ti.height;
653 i_den = VOUT_ASPECT_FACTOR * p_sys->ti.width;
654 vlc_reduce( &i_dst_num, &i_dst_den, i_num, i_den, 0 );
655 p_sys->ti.aspect_numerator = i_dst_num;
656 p_sys->ti.aspect_denominator = i_dst_den;
660 p_sys->ti.aspect_numerator = 4;
661 p_sys->ti.aspect_denominator = 3;
664 p_sys->ti.target_bitrate = p_enc->fmt_out.i_bitrate;
665 p_sys->ti.quality = ((float)i_quality) * 6.3;
667 p_sys->ti.dropframes_p = 0;
668 p_sys->ti.quick_p = 1;
669 p_sys->ti.keyframe_auto_p = 1;
670 p_sys->ti.keyframe_frequency = 64;
671 p_sys->ti.keyframe_frequency_force = 64;
672 p_sys->ti.keyframe_data_target_bitrate = p_enc->fmt_out.i_bitrate * 1.5;
673 p_sys->ti.keyframe_auto_threshold = 80;
674 p_sys->ti.keyframe_mindistance = 8;
675 p_sys->ti.noise_sensitivity = 1;
677 theora_encode_init( &p_sys->td, &p_sys->ti );
678 theora_info_clear( &p_sys->ti );
679 theora_comment_init( &p_sys->tc );
681 /* Create and store headers */
682 p_enc->fmt_out.i_extra = 3 * 2;
683 for( i = 0; i < 3; i++ )
685 if( i == 0 ) theora_encode_header( &p_sys->td, &header );
686 else if( i == 1 ) theora_encode_comment( &p_sys->tc, &header );
687 else if( i == 2 ) theora_encode_tables( &p_sys->td, &header );
689 p_enc->fmt_out.p_extra =
690 realloc( p_enc->fmt_out.p_extra,
691 p_enc->fmt_out.i_extra + header.bytes );
692 p_extra = p_enc->fmt_out.p_extra;
693 p_extra += p_enc->fmt_out.i_extra + (i-3)*2;
694 p_enc->fmt_out.i_extra += header.bytes;
696 *(p_extra++) = header.bytes >> 8;
697 *(p_extra++) = header.bytes & 0xFF;
699 memcpy( p_extra, header.packet, header.bytes );
705 /****************************************************************************
706 * Encode: the whole thing
707 ****************************************************************************
708 * This function spits out ogg packets.
709 ****************************************************************************/
710 static block_t *Encode( encoder_t *p_enc, picture_t *p_pict )
712 encoder_sys_t *p_sys = p_enc->p_sys;
713 ogg_packet oggpacket;
719 if( p_pict->p[0].i_pitch < (int)p_sys->i_width ||
720 p_pict->p[0].i_lines < (int)p_sys->i_height )
722 msg_Warn( p_enc, "frame is smaller than encoding size"
723 "(%ix%i->%ix%i) -> dropping frame",
724 p_pict->p[0].i_pitch, p_pict->p[0].i_lines,
725 p_sys->i_width, p_sys->i_height );
730 if( p_pict->p[0].i_visible_pitch < (int)p_sys->i_width )
732 for( i = 0; i < p_sys->i_height; i++ )
734 memset( p_pict->p[0].p_pixels + i * p_pict->p[0].i_pitch +
735 p_pict->p[0].i_visible_pitch,
736 *( p_pict->p[0].p_pixels + i * p_pict->p[0].i_pitch +
737 p_pict->p[0].i_visible_pitch - 1 ),
738 p_sys->i_width - p_pict->p[0].i_visible_pitch );
740 for( i = 0; i < p_sys->i_height / 2; i++ )
742 memset( p_pict->p[1].p_pixels + i * p_pict->p[1].i_pitch +
743 p_pict->p[1].i_visible_pitch,
744 *( p_pict->p[1].p_pixels + i * p_pict->p[1].i_pitch +
745 p_pict->p[1].i_visible_pitch - 1 ),
746 p_sys->i_width / 2 - p_pict->p[1].i_visible_pitch );
747 memset( p_pict->p[2].p_pixels + i * p_pict->p[2].i_pitch +
748 p_pict->p[2].i_visible_pitch,
749 *( p_pict->p[2].p_pixels + i * p_pict->p[2].i_pitch +
750 p_pict->p[2].i_visible_pitch - 1 ),
751 p_sys->i_width / 2 - p_pict->p[2].i_visible_pitch );
755 if( p_pict->p[0].i_visible_lines < (int)p_sys->i_height )
757 for( i = p_pict->p[0].i_visible_lines; i < p_sys->i_height; i++ )
759 memset( p_pict->p[0].p_pixels + i * p_pict->p[0].i_pitch, 0,
762 for( i = p_pict->p[1].i_visible_lines; i < p_sys->i_height / 2; i++ )
764 memset( p_pict->p[1].p_pixels + i * p_pict->p[1].i_pitch, 0x80,
765 p_sys->i_width / 2 );
766 memset( p_pict->p[2].p_pixels + i * p_pict->p[2].i_pitch, 0x80,
767 p_sys->i_width / 2 );
771 /* Theora is a one-frame-in, one-frame-out system. Submit a frame
772 * for compression and pull out the packet. */
774 yuv.y_width = p_sys->i_width;
775 yuv.y_height = p_sys->i_height;
776 yuv.y_stride = p_pict->p[0].i_pitch;
778 yuv.uv_width = p_sys->i_width / 2;
779 yuv.uv_height = p_sys->i_height / 2;
780 yuv.uv_stride = p_pict->p[1].i_pitch;
782 yuv.y = p_pict->p[0].p_pixels;
783 yuv.u = p_pict->p[1].p_pixels;
784 yuv.v = p_pict->p[2].p_pixels;
786 if( theora_encode_YUVin( &p_sys->td, &yuv ) < 0 )
788 msg_Warn( p_enc, "failed encoding a frame" );
792 theora_encode_packetout( &p_sys->td, 0, &oggpacket );
794 /* Ogg packet to block */
795 p_block = block_New( p_enc, oggpacket.bytes );
796 memcpy( p_block->p_buffer, oggpacket.packet, oggpacket.bytes );
797 p_block->i_dts = p_block->i_pts = p_pict->date;
802 /*****************************************************************************
803 * CloseEncoder: theora encoder destruction
804 *****************************************************************************/
805 static void CloseEncoder( vlc_object_t *p_this )
807 encoder_t *p_enc = (encoder_t *)p_this;
808 encoder_sys_t *p_sys = p_enc->p_sys;
810 theora_info_clear( &p_sys->ti );
811 theora_comment_clear( &p_sys->tc );