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 *****************************************************************************/
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( _("Theora video decoder") );
105 set_capability( "decoder", 100 );
106 set_callbacks( OpenDecoder, CloseDecoder );
107 add_shortcut( "theora" );
110 set_description( _("Theora video packetizer") );
111 set_capability( "packetizer", 100 );
112 set_callbacks( OpenPacketizer, CloseDecoder );
115 set_description( _("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 *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 msg_Err( p_dec, "out of memory" );
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_FOURCC('I','4','2','0');
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_FOURCC( 't', 'h', 'e', 'o' );
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 =
222 realloc( p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra +
223 oggpacket.bytes + 2 );
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 p_dec->fmt_out.video.i_width = p_sys->ti.width;
290 p_dec->fmt_out.video.i_height = p_sys->ti.height;
291 if( p_sys->ti.frame_width && p_sys->ti.frame_height )
293 p_dec->fmt_out.video.i_width = p_sys->ti.frame_width;
294 p_dec->fmt_out.video.i_height = p_sys->ti.frame_height;
297 if( p_sys->ti.aspect_denominator && p_sys->ti.aspect_numerator )
299 p_dec->fmt_out.video.i_aspect = ((int64_t)VOUT_ASPECT_FACTOR) *
300 ( p_sys->ti.aspect_numerator * p_dec->fmt_out.video.i_width ) /
301 ( p_sys->ti.aspect_denominator * p_dec->fmt_out.video.i_height );
305 p_dec->fmt_out.video.i_aspect = VOUT_ASPECT_FACTOR *
306 p_sys->ti.frame_width / p_sys->ti.frame_height;
309 if( p_sys->ti.fps_numerator > 0 && p_sys->ti.fps_denominator > 0 )
311 p_dec->fmt_out.video.i_frame_rate = p_sys->ti.fps_numerator;
312 p_dec->fmt_out.video.i_frame_rate_base = p_sys->ti.fps_denominator;
315 msg_Dbg( p_dec, "%dx%d %.02f fps video, frame content "
316 "is %dx%d with offset (%d,%d)",
317 p_sys->ti.width, p_sys->ti.height,
318 (double)p_sys->ti.fps_numerator/p_sys->ti.fps_denominator,
319 p_sys->ti.frame_width, p_sys->ti.frame_height,
320 p_sys->ti.offset_x, p_sys->ti.offset_y );
322 /* Sanity check that seems necessary for some corrupted files */
323 if( p_sys->ti.width < p_sys->ti.frame_width ||
324 p_sys->ti.height < p_sys->ti.frame_height )
326 msg_Warn( p_dec, "trying to correct invalid theora header "
327 "(frame size (%dx%d) is smaller than frame content (%d,%d))",
328 p_sys->ti.width, p_sys->ti.height,
329 p_sys->ti.frame_width, p_sys->ti.frame_height );
331 if( p_sys->ti.width < p_sys->ti.frame_width )
332 p_sys->ti.width = p_sys->ti.frame_width;
333 if( p_sys->ti.height < p_sys->ti.frame_height )
334 p_sys->ti.height = p_sys->ti.frame_height;
337 /* The next packet in order is the comments header */
339 oggpacket.bytes = *(p_extra++) << 8;
340 oggpacket.bytes |= (*(p_extra++) & 0xFF);
341 oggpacket.packet = p_extra;
342 p_extra += oggpacket.bytes;
343 i_extra -= (oggpacket.bytes + 2);
346 msg_Err( p_dec, "header data corrupted");
350 /* The next packet in order is the comments header */
351 if( theora_decode_header( &p_sys->ti, &p_sys->tc, &oggpacket ) < 0 )
353 msg_Err( p_dec, "2nd Theora header is corrupted" );
357 ParseTheoraComments( p_dec );
359 /* The next packet in order is the codebooks header
360 * We need to watch out that this packet is not missing as a
361 * missing or corrupted header is fatal. */
362 oggpacket.bytes = *(p_extra++) << 8;
363 oggpacket.bytes |= (*(p_extra++) & 0xFF);
364 oggpacket.packet = p_extra;
365 i_extra -= (oggpacket.bytes + 2);
368 msg_Err( p_dec, "header data corrupted");
372 /* The next packet in order is the codebooks header
373 * We need to watch out that this packet is not missing as a
374 * missing or corrupted header is fatal */
375 if( theora_decode_header( &p_sys->ti, &p_sys->tc, &oggpacket ) < 0 )
377 msg_Err( p_dec, "3rd Theora header is corrupted" );
381 if( !p_sys->b_packetizer )
383 /* We have all the headers, initialize decoder */
384 theora_decode_init( &p_sys->td, &p_sys->ti );
388 p_dec->fmt_out.i_extra = p_dec->fmt_in.i_extra;
389 p_dec->fmt_out.p_extra =
390 realloc( p_dec->fmt_out.p_extra, p_dec->fmt_out.i_extra );
391 memcpy( p_dec->fmt_out.p_extra,
392 p_dec->fmt_in.p_extra, p_dec->fmt_out.i_extra );
398 /*****************************************************************************
399 * ProcessPacket: processes a theora packet.
400 *****************************************************************************/
401 static void *ProcessPacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
404 decoder_sys_t *p_sys = p_dec->p_sys;
405 block_t *p_block = *pp_block;
408 if( ( p_block->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) ) != 0 )
410 /* Don't send the the first packet after a discontinuity to
411 * theora_decode, otherwise we get purple/green display artifacts
412 * appearing in the video output */
416 /* Date management */
417 if( p_block->i_pts > 0 && p_block->i_pts != p_sys->i_pts )
419 p_sys->i_pts = p_block->i_pts;
422 *pp_block = NULL; /* To avoid being fed the same packet again */
424 if( p_sys->b_packetizer )
426 /* Date management */
427 p_block->i_dts = p_block->i_pts = p_sys->i_pts;
429 if( p_sys->i_headers >= 3 )
430 p_block->i_length = p_sys->i_pts - p_block->i_pts;
432 p_block->i_length = 0;
438 if( p_sys->i_headers >= 3 )
439 p_buf = DecodePacket( p_dec, p_oggpacket );
443 if( p_block ) block_Release( p_block );
446 /* Date management */
447 p_sys->i_pts += ( INT64_C(1000000) * p_sys->ti.fps_denominator /
448 p_sys->ti.fps_numerator ); /* 1 frame per packet */
453 /*****************************************************************************
454 * DecodePacket: decodes a Theora packet.
455 *****************************************************************************/
456 static picture_t *DecodePacket( decoder_t *p_dec, ogg_packet *p_oggpacket )
458 decoder_sys_t *p_sys = p_dec->p_sys;
462 theora_decode_packetin( &p_sys->td, p_oggpacket );
464 /* Check for keyframe */
465 if( !(p_oggpacket->packet[0] & 0x80) /* data packet */ &&
466 !(p_oggpacket->packet[0] & 0x40) /* intra frame */ )
467 p_sys->b_decoded_first_keyframe = true;
469 /* If we haven't seen a single keyframe yet, don't let Theora decode
470 * anything, otherwise we'll get display artifacts. (This is impossible
471 * in the general case, but can happen if e.g. we play a network stream
472 * using a timed URL, such that the server doesn't start the video with a
474 if( p_sys->b_decoded_first_keyframe )
475 theora_decode_YUVout( &p_sys->td, &yuv );
479 /* Get a new picture */
480 p_pic = p_dec->pf_vout_buffer_new( p_dec );
481 if( !p_pic ) return NULL;
483 theora_CopyPicture( p_dec, p_pic, &yuv );
485 p_pic->date = p_sys->i_pts;
490 /*****************************************************************************
491 * ParseTheoraComments: FIXME should be done in demuxer
492 *****************************************************************************/
493 static void ParseTheoraComments( decoder_t *p_dec )
495 input_thread_t *p_input = (input_thread_t *)p_dec->p_parent;
496 char *psz_name, *psz_value, *psz_comment;
499 if( p_input->i_object_type != VLC_OBJECT_INPUT ) return;
501 while ( i < p_dec->p_sys->tc.comments )
503 psz_comment = strdup( p_dec->p_sys->tc.user_comments[i] );
506 msg_Warn( p_dec, "out of memory" );
509 psz_name = psz_comment;
510 psz_value = strchr( psz_comment, '=' );
515 input_Control( p_input, INPUT_ADD_INFO, _("Theora comment"),
516 psz_name, "%s", psz_value );
523 /*****************************************************************************
524 * CloseDecoder: theora decoder destruction
525 *****************************************************************************/
526 static void CloseDecoder( vlc_object_t *p_this )
528 decoder_t *p_dec = (decoder_t *)p_this;
529 decoder_sys_t *p_sys = p_dec->p_sys;
531 theora_info_clear( &p_sys->ti );
532 theora_comment_clear( &p_sys->tc );
537 /*****************************************************************************
538 * theora_CopyPicture: copy a picture from theora internal buffers to a
539 * picture_t structure.
540 *****************************************************************************/
541 static void theora_CopyPicture( decoder_t *p_dec, picture_t *p_pic,
544 int i_plane, i_line, i_width, i_dst_stride, i_src_stride;
545 int i_src_xoffset, i_src_yoffset;
546 uint8_t *p_dst, *p_src;
548 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
550 p_dst = p_pic->p[i_plane].p_pixels;
551 p_src = i_plane ? (i_plane - 1 ? yuv->v : yuv->u ) : yuv->y;
552 i_width = p_pic->p[i_plane].i_visible_pitch;
553 i_dst_stride = p_pic->p[i_plane].i_pitch;
554 i_src_stride = i_plane ? yuv->uv_stride : yuv->y_stride;
555 i_src_xoffset = p_dec->p_sys->ti.offset_x;
556 i_src_yoffset = p_dec->p_sys->ti.offset_y;
563 p_src += (i_src_yoffset * i_src_stride + i_src_xoffset);
565 for( i_line = 0; i_line < p_pic->p[i_plane].i_visible_lines; i_line++ )
567 vlc_memcpy( p_dst, p_src + i_src_xoffset,
568 i_plane ? yuv->uv_width : yuv->y_width );
569 p_src += i_src_stride;
570 p_dst += i_dst_stride;
575 /*****************************************************************************
576 * encoder_sys_t : theora encoder descriptor
577 *****************************************************************************/
588 theora_info ti; /* theora bitstream settings */
589 theora_comment tc; /* theora comment header */
590 theora_state td; /* theora bitstream user comments */
592 int i_width, i_height;
595 /*****************************************************************************
596 * OpenEncoder: probe the encoder and return score
597 *****************************************************************************/
598 static int OpenEncoder( vlc_object_t *p_this )
600 encoder_t *p_enc = (encoder_t *)p_this;
601 encoder_sys_t *p_sys = p_enc->p_sys;
607 if( p_enc->fmt_out.i_codec != VLC_FOURCC('t','h','e','o') &&
613 /* Allocate the memory needed to store the decoder's structure */
614 if( ( p_sys = (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
616 msg_Err( p_enc, "out of memory" );
619 p_enc->p_sys = p_sys;
621 p_enc->pf_encode_video = Encode;
622 p_enc->fmt_in.i_codec = VLC_FOURCC('I','4','2','0');
623 p_enc->fmt_out.i_codec = VLC_FOURCC('t','h','e','o');
625 config_ChainParse( p_enc, ENC_CFG_PREFIX, ppsz_enc_options, p_enc->p_cfg );
627 var_Get( p_enc, ENC_CFG_PREFIX "quality", &val );
628 i_quality = val.i_int;
629 if( i_quality > 10 ) i_quality = 10;
630 if( i_quality < 0 ) i_quality = 0;
632 theora_info_init( &p_sys->ti );
634 p_sys->ti.width = p_enc->fmt_in.video.i_width;
635 p_sys->ti.height = p_enc->fmt_in.video.i_height;
637 if( p_sys->ti.width % 16 || p_sys->ti.height % 16 )
639 /* Pictures from the transcoder should always have a pitch
640 * which is a multiple of 16 */
641 p_sys->ti.width = (p_sys->ti.width + 15) >> 4 << 4;
642 p_sys->ti.height = (p_sys->ti.height + 15) >> 4 << 4;
644 msg_Dbg( p_enc, "padding video from %dx%d to %dx%d",
645 p_enc->fmt_in.video.i_width, p_enc->fmt_in.video.i_height,
646 p_sys->ti.width, p_sys->ti.height );
649 p_sys->ti.frame_width = p_enc->fmt_in.video.i_width;
650 p_sys->ti.frame_height = p_enc->fmt_in.video.i_height;
651 p_sys->ti.offset_x = 0 /*frame_x_offset*/;
652 p_sys->ti.offset_y = 0 /*frame_y_offset*/;
654 p_sys->i_width = p_sys->ti.width;
655 p_sys->i_height = p_sys->ti.height;
657 if( !p_enc->fmt_in.video.i_frame_rate ||
658 !p_enc->fmt_in.video.i_frame_rate_base )
660 p_sys->ti.fps_numerator = 25;
661 p_sys->ti.fps_denominator = 1;
665 p_sys->ti.fps_numerator = p_enc->fmt_in.video.i_frame_rate;
666 p_sys->ti.fps_denominator = p_enc->fmt_in.video.i_frame_rate_base;
669 if( p_enc->fmt_in.video.i_aspect )
671 uint64_t i_num, i_den;
672 unsigned i_dst_num, i_dst_den;
674 i_num = p_enc->fmt_in.video.i_aspect * (int64_t)p_sys->ti.height;
675 i_den = VOUT_ASPECT_FACTOR * p_sys->ti.width;
676 vlc_ureduce( &i_dst_num, &i_dst_den, i_num, i_den, 0 );
677 p_sys->ti.aspect_numerator = i_dst_num;
678 p_sys->ti.aspect_denominator = i_dst_den;
682 p_sys->ti.aspect_numerator = 4;
683 p_sys->ti.aspect_denominator = 3;
686 p_sys->ti.target_bitrate = p_enc->fmt_out.i_bitrate;
687 p_sys->ti.quality = ((float)i_quality) * 6.3;
689 p_sys->ti.dropframes_p = 0;
690 p_sys->ti.quick_p = 1;
691 p_sys->ti.keyframe_auto_p = 1;
692 p_sys->ti.keyframe_frequency = 64;
693 p_sys->ti.keyframe_frequency_force = 64;
694 p_sys->ti.keyframe_data_target_bitrate = p_enc->fmt_out.i_bitrate * 1.5;
695 p_sys->ti.keyframe_auto_threshold = 80;
696 p_sys->ti.keyframe_mindistance = 8;
697 p_sys->ti.noise_sensitivity = 1;
699 theora_encode_init( &p_sys->td, &p_sys->ti );
700 theora_info_clear( &p_sys->ti );
701 theora_comment_init( &p_sys->tc );
703 /* Create and store headers */
704 p_enc->fmt_out.i_extra = 3 * 2;
705 for( i = 0; i < 3; i++ )
707 if( i == 0 ) theora_encode_header( &p_sys->td, &header );
708 else if( i == 1 ) theora_encode_comment( &p_sys->tc, &header );
709 else if( i == 2 ) theora_encode_tables( &p_sys->td, &header );
711 p_enc->fmt_out.p_extra =
712 realloc( p_enc->fmt_out.p_extra,
713 p_enc->fmt_out.i_extra + header.bytes );
714 p_extra = p_enc->fmt_out.p_extra;
715 p_extra += p_enc->fmt_out.i_extra + (i-3)*2;
716 p_enc->fmt_out.i_extra += header.bytes;
718 *(p_extra++) = header.bytes >> 8;
719 *(p_extra++) = header.bytes & 0xFF;
721 memcpy( p_extra, header.packet, header.bytes );
727 /****************************************************************************
728 * Encode: the whole thing
729 ****************************************************************************
730 * This function spits out ogg packets.
731 ****************************************************************************/
732 static block_t *Encode( encoder_t *p_enc, picture_t *p_pict )
734 encoder_sys_t *p_sys = p_enc->p_sys;
735 ogg_packet oggpacket;
741 if( p_pict->p[0].i_pitch < (int)p_sys->i_width ||
742 p_pict->p[0].i_lines < (int)p_sys->i_height )
744 msg_Warn( p_enc, "frame is smaller than encoding size"
745 "(%ix%i->%ix%i) -> dropping frame",
746 p_pict->p[0].i_pitch, p_pict->p[0].i_lines,
747 p_sys->i_width, p_sys->i_height );
752 if( p_pict->p[0].i_visible_pitch < (int)p_sys->i_width )
754 for( i = 0; i < p_sys->i_height; i++ )
756 memset( p_pict->p[0].p_pixels + i * p_pict->p[0].i_pitch +
757 p_pict->p[0].i_visible_pitch,
758 *( p_pict->p[0].p_pixels + i * p_pict->p[0].i_pitch +
759 p_pict->p[0].i_visible_pitch - 1 ),
760 p_sys->i_width - p_pict->p[0].i_visible_pitch );
762 for( i = 0; i < p_sys->i_height / 2; i++ )
764 memset( p_pict->p[1].p_pixels + i * p_pict->p[1].i_pitch +
765 p_pict->p[1].i_visible_pitch,
766 *( p_pict->p[1].p_pixels + i * p_pict->p[1].i_pitch +
767 p_pict->p[1].i_visible_pitch - 1 ),
768 p_sys->i_width / 2 - p_pict->p[1].i_visible_pitch );
769 memset( p_pict->p[2].p_pixels + i * p_pict->p[2].i_pitch +
770 p_pict->p[2].i_visible_pitch,
771 *( p_pict->p[2].p_pixels + i * p_pict->p[2].i_pitch +
772 p_pict->p[2].i_visible_pitch - 1 ),
773 p_sys->i_width / 2 - p_pict->p[2].i_visible_pitch );
777 if( p_pict->p[0].i_visible_lines < (int)p_sys->i_height )
779 for( i = p_pict->p[0].i_visible_lines; i < p_sys->i_height; i++ )
781 memset( p_pict->p[0].p_pixels + i * p_pict->p[0].i_pitch, 0,
784 for( i = p_pict->p[1].i_visible_lines; i < p_sys->i_height / 2; i++ )
786 memset( p_pict->p[1].p_pixels + i * p_pict->p[1].i_pitch, 0x80,
787 p_sys->i_width / 2 );
788 memset( p_pict->p[2].p_pixels + i * p_pict->p[2].i_pitch, 0x80,
789 p_sys->i_width / 2 );
793 /* Theora is a one-frame-in, one-frame-out system. Submit a frame
794 * for compression and pull out the packet. */
796 yuv.y_width = p_sys->i_width;
797 yuv.y_height = p_sys->i_height;
798 yuv.y_stride = p_pict->p[0].i_pitch;
800 yuv.uv_width = p_sys->i_width / 2;
801 yuv.uv_height = p_sys->i_height / 2;
802 yuv.uv_stride = p_pict->p[1].i_pitch;
804 yuv.y = p_pict->p[0].p_pixels;
805 yuv.u = p_pict->p[1].p_pixels;
806 yuv.v = p_pict->p[2].p_pixels;
808 if( theora_encode_YUVin( &p_sys->td, &yuv ) < 0 )
810 msg_Warn( p_enc, "failed encoding a frame" );
814 theora_encode_packetout( &p_sys->td, 0, &oggpacket );
816 /* Ogg packet to block */
817 p_block = block_New( p_enc, oggpacket.bytes );
818 memcpy( p_block->p_buffer, oggpacket.packet, oggpacket.bytes );
819 p_block->i_dts = p_block->i_pts = p_pict->date;
824 /*****************************************************************************
825 * CloseEncoder: theora encoder destruction
826 *****************************************************************************/
827 static void CloseEncoder( vlc_object_t *p_this )
829 encoder_t *p_enc = (encoder_t *)p_this;
830 encoder_sys_t *p_sys = p_enc->p_sys;
832 theora_info_clear( &p_sys->ti );
833 theora_comment_clear( &p_sys->tc );