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_codec.h>
35 #include <vlc_input.h>
38 #include <theora/theora.h>
40 /*****************************************************************************
41 * decoder_sys_t : theora decoder descriptor
42 *****************************************************************************/
46 vlc_bool_t b_packetizer;
56 theora_info ti; /* theora bitstream settings */
57 theora_comment tc; /* theora comment header */
58 theora_state td; /* theora bitstream user comments */
63 vlc_bool_t b_decoded_first_keyframe;
71 /*****************************************************************************
73 *****************************************************************************/
74 static int OpenDecoder ( vlc_object_t * );
75 static int OpenPacketizer( vlc_object_t * );
76 static void CloseDecoder ( vlc_object_t * );
78 static void *DecodeBlock ( decoder_t *, block_t ** );
79 static int ProcessHeaders( decoder_t * );
80 static void *ProcessPacket ( decoder_t *, ogg_packet *, block_t ** );
82 static picture_t *DecodePacket( decoder_t *, ogg_packet * );
84 static void ParseTheoraComments( decoder_t * );
85 static void theora_CopyPicture( decoder_t *, picture_t *, yuv_buffer * );
87 static int OpenEncoder( vlc_object_t *p_this );
88 static void CloseEncoder( vlc_object_t *p_this );
89 static block_t *Encode( encoder_t *p_enc, picture_t *p_pict );
91 /*****************************************************************************
93 *****************************************************************************/
94 #define ENC_QUALITY_TEXT N_("Encoding quality")
95 #define ENC_QUALITY_LONGTEXT N_( \
96 "Enforce a quality between 1 (low) and 10 (high), instead " \
97 "of specifying a particular bitrate. This will produce a VBR stream." )
100 set_category( CAT_INPUT );
101 set_subcategory( SUBCAT_INPUT_VCODEC );
102 set_shortname( "Theora" );
103 set_description( _("Theora video decoder") );
104 set_capability( "decoder", 100 );
105 set_callbacks( OpenDecoder, CloseDecoder );
106 add_shortcut( "theora" );
109 set_description( _("Theora video packetizer") );
110 set_capability( "packetizer", 100 );
111 set_callbacks( OpenPacketizer, CloseDecoder );
114 set_description( _("Theora video encoder") );
115 set_capability( "encoder", 150 );
116 set_callbacks( OpenEncoder, CloseEncoder );
118 # define ENC_CFG_PREFIX "sout-theora-"
119 add_integer( ENC_CFG_PREFIX "quality", 2, NULL, ENC_QUALITY_TEXT,
120 ENC_QUALITY_LONGTEXT, VLC_FALSE );
123 static const char *ppsz_enc_options[] = {
127 /*****************************************************************************
128 * OpenDecoder: probe the decoder and return score
129 *****************************************************************************/
130 static int OpenDecoder( vlc_object_t *p_this )
132 decoder_t *p_dec = (decoder_t*)p_this;
133 decoder_sys_t *p_sys;
135 if( p_dec->fmt_in.i_codec != VLC_FOURCC('t','h','e','o') )
140 /* Allocate the memory needed to store the decoder's structure */
141 if( ( p_dec->p_sys = p_sys =
142 (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
144 msg_Err( p_dec, "out of memory" );
147 p_dec->p_sys->b_packetizer = VLC_FALSE;
150 p_sys->b_decoded_first_keyframe = VLC_FALSE;
152 /* Set output properties */
153 p_dec->fmt_out.i_cat = VIDEO_ES;
154 p_dec->fmt_out.i_codec = VLC_FOURCC('I','4','2','0');
157 p_dec->pf_decode_video = (picture_t *(*)(decoder_t *, block_t **))
159 p_dec->pf_packetize = (block_t *(*)(decoder_t *, block_t **))
162 /* Init supporting Theora structures needed in header parsing */
163 theora_comment_init( &p_sys->tc );
164 theora_info_init( &p_sys->ti );
166 p_sys->i_headers = 0;
171 static int OpenPacketizer( vlc_object_t *p_this )
173 decoder_t *p_dec = (decoder_t*)p_this;
175 int i_ret = OpenDecoder( p_this );
177 if( i_ret == VLC_SUCCESS )
179 p_dec->p_sys->b_packetizer = VLC_TRUE;
180 p_dec->fmt_out.i_codec = VLC_FOURCC( 't', 'h', 'e', 'o' );
186 /****************************************************************************
187 * DecodeBlock: the whole thing
188 ****************************************************************************
189 * This function must be fed with ogg packets.
190 ****************************************************************************/
191 static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
193 decoder_sys_t *p_sys = p_dec->p_sys;
195 ogg_packet oggpacket;
197 if( !pp_block || !*pp_block ) return NULL;
201 /* Block to Ogg packet */
202 oggpacket.packet = p_block->p_buffer;
203 oggpacket.bytes = p_block->i_buffer;
204 oggpacket.granulepos = p_block->i_dts;
207 oggpacket.packetno = 0;
209 /* Check for headers */
210 if( p_sys->i_headers == 0 && p_dec->fmt_in.i_extra )
212 /* Headers already available as extra data */
213 p_sys->i_headers = 3;
215 else if( oggpacket.bytes && p_sys->i_headers < 3 )
217 /* Backup headers as extra data */
220 p_dec->fmt_in.p_extra =
221 realloc( p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra +
222 oggpacket.bytes + 2 );
223 p_extra = ((uint8_t *)p_dec->fmt_in.p_extra) + p_dec->fmt_in.i_extra;
224 *(p_extra++) = oggpacket.bytes >> 8;
225 *(p_extra++) = oggpacket.bytes & 0xFF;
227 memcpy( p_extra, oggpacket.packet, oggpacket.bytes );
228 p_dec->fmt_in.i_extra += oggpacket.bytes + 2;
230 block_Release( *pp_block );
235 if( p_sys->i_headers == 3 )
237 if( ProcessHeaders( p_dec ) != VLC_SUCCESS )
239 p_sys->i_headers = 0;
240 p_dec->fmt_in.i_extra = 0;
241 block_Release( *pp_block );
244 else p_sys->i_headers++;
247 return ProcessPacket( p_dec, &oggpacket, pp_block );
250 /*****************************************************************************
251 * ProcessHeaders: process Theora headers.
252 *****************************************************************************/
253 static int ProcessHeaders( decoder_t *p_dec )
255 decoder_sys_t *p_sys = p_dec->p_sys;
256 ogg_packet oggpacket;
260 if( !p_dec->fmt_in.i_extra ) return VLC_EGENERIC;
262 oggpacket.granulepos = -1;
263 oggpacket.b_o_s = 1; /* yes this actually is a b_o_s packet :) */
265 oggpacket.packetno = 0;
266 p_extra = p_dec->fmt_in.p_extra;
267 i_extra = p_dec->fmt_in.i_extra;
269 /* Take care of the initial Vorbis header */
270 oggpacket.bytes = *(p_extra++) << 8;
271 oggpacket.bytes |= (*(p_extra++) & 0xFF);
272 oggpacket.packet = p_extra;
273 p_extra += oggpacket.bytes;
274 i_extra -= (oggpacket.bytes + 2);
277 msg_Err( p_dec, "header data corrupted");
281 if( theora_decode_header( &p_sys->ti, &p_sys->tc, &oggpacket ) < 0 )
283 msg_Err( p_dec, "this bitstream does not contain Theora video data" );
287 /* Set output properties */
288 p_dec->fmt_out.video.i_width = p_sys->ti.width;
289 p_dec->fmt_out.video.i_height = p_sys->ti.height;
290 if( p_sys->ti.frame_width && p_sys->ti.frame_height )
292 p_dec->fmt_out.video.i_width = p_sys->ti.frame_width;
293 p_dec->fmt_out.video.i_height = p_sys->ti.frame_height;
296 if( p_sys->ti.aspect_denominator && p_sys->ti.aspect_numerator )
298 p_dec->fmt_out.video.i_aspect = ((int64_t)VOUT_ASPECT_FACTOR) *
299 ( p_sys->ti.aspect_numerator * p_dec->fmt_out.video.i_width ) /
300 ( p_sys->ti.aspect_denominator * p_dec->fmt_out.video.i_height );
304 p_dec->fmt_out.video.i_aspect = VOUT_ASPECT_FACTOR *
305 p_sys->ti.frame_width / p_sys->ti.frame_height;
308 if( p_sys->ti.fps_numerator > 0 && p_sys->ti.fps_denominator > 0 )
310 p_dec->fmt_out.video.i_frame_rate = p_sys->ti.fps_numerator;
311 p_dec->fmt_out.video.i_frame_rate_base = p_sys->ti.fps_denominator;
314 msg_Dbg( p_dec, "%dx%d %.02f fps video, frame content "
315 "is %dx%d with offset (%d,%d)",
316 p_sys->ti.width, p_sys->ti.height,
317 (double)p_sys->ti.fps_numerator/p_sys->ti.fps_denominator,
318 p_sys->ti.frame_width, p_sys->ti.frame_height,
319 p_sys->ti.offset_x, p_sys->ti.offset_y );
321 /* Sanity check that seems necessary for some corrupted files */
322 if( p_sys->ti.width < p_sys->ti.frame_width ||
323 p_sys->ti.height < p_sys->ti.frame_height )
325 msg_Warn( p_dec, "trying to correct invalid theora header "
326 "(frame size (%dx%d) is smaller than frame content (%d,%d))",
327 p_sys->ti.width, p_sys->ti.height,
328 p_sys->ti.frame_width, p_sys->ti.frame_height );
330 if( p_sys->ti.width < p_sys->ti.frame_width )
331 p_sys->ti.width = p_sys->ti.frame_width;
332 if( p_sys->ti.height < p_sys->ti.frame_height )
333 p_sys->ti.height = p_sys->ti.frame_height;
336 /* The next packet in order is the comments header */
338 oggpacket.bytes = *(p_extra++) << 8;
339 oggpacket.bytes |= (*(p_extra++) & 0xFF);
340 oggpacket.packet = p_extra;
341 p_extra += oggpacket.bytes;
342 i_extra -= (oggpacket.bytes + 2);
345 msg_Err( p_dec, "header data corrupted");
349 /* The next packet in order is the comments header */
350 if( theora_decode_header( &p_sys->ti, &p_sys->tc, &oggpacket ) < 0 )
352 msg_Err( p_dec, "2nd Theora header is corrupted" );
356 ParseTheoraComments( p_dec );
358 /* The next packet in order is the codebooks header
359 * We need to watch out that this packet is not missing as a
360 * missing or corrupted header is fatal. */
361 oggpacket.bytes = *(p_extra++) << 8;
362 oggpacket.bytes |= (*(p_extra++) & 0xFF);
363 oggpacket.packet = p_extra;
364 i_extra -= (oggpacket.bytes + 2);
367 msg_Err( p_dec, "header data corrupted");
371 /* The next packet in order is the codebooks header
372 * We need to watch out that this packet is not missing as a
373 * missing or corrupted header is fatal */
374 if( theora_decode_header( &p_sys->ti, &p_sys->tc, &oggpacket ) < 0 )
376 msg_Err( p_dec, "3rd Theora header is corrupted" );
380 if( !p_sys->b_packetizer )
382 /* We have all the headers, initialize decoder */
383 theora_decode_init( &p_sys->td, &p_sys->ti );
387 p_dec->fmt_out.i_extra = p_dec->fmt_in.i_extra;
388 p_dec->fmt_out.p_extra =
389 realloc( p_dec->fmt_out.p_extra, p_dec->fmt_out.i_extra );
390 memcpy( p_dec->fmt_out.p_extra,
391 p_dec->fmt_in.p_extra, p_dec->fmt_out.i_extra );
397 /*****************************************************************************
398 * ProcessPacket: processes a theora packet.
399 *****************************************************************************/
400 static void *ProcessPacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
403 decoder_sys_t *p_sys = p_dec->p_sys;
404 block_t *p_block = *pp_block;
407 if( ( p_block->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) ) != 0 )
409 /* Don't send the the first packet after a discontinuity to
410 * theora_decode, otherwise we get purple/green display artifacts
411 * appearing in the video output */
415 /* Date management */
416 if( p_block->i_pts > 0 && p_block->i_pts != p_sys->i_pts )
418 p_sys->i_pts = p_block->i_pts;
421 *pp_block = NULL; /* To avoid being fed the same packet again */
423 if( p_sys->b_packetizer )
425 /* Date management */
426 p_block->i_dts = p_block->i_pts = p_sys->i_pts;
428 if( p_sys->i_headers >= 3 )
429 p_block->i_length = p_sys->i_pts - p_block->i_pts;
431 p_block->i_length = 0;
437 if( p_sys->i_headers >= 3 )
438 p_buf = DecodePacket( p_dec, p_oggpacket );
442 if( p_block ) block_Release( p_block );
445 /* Date management */
446 p_sys->i_pts += ( I64C(1000000) * p_sys->ti.fps_denominator /
447 p_sys->ti.fps_numerator ); /* 1 frame per packet */
452 /*****************************************************************************
453 * DecodePacket: decodes a Theora packet.
454 *****************************************************************************/
455 static picture_t *DecodePacket( decoder_t *p_dec, ogg_packet *p_oggpacket )
457 decoder_sys_t *p_sys = p_dec->p_sys;
461 theora_decode_packetin( &p_sys->td, p_oggpacket );
463 /* Check for keyframe */
464 if( !(p_oggpacket->packet[0] & 0x80) /* data packet */ &&
465 !(p_oggpacket->packet[0] & 0x40) /* intra frame */ )
466 p_sys->b_decoded_first_keyframe = VLC_TRUE;
468 /* If we haven't seen a single keyframe yet, don't let Theora decode
469 * anything, otherwise we'll get display artifacts. (This is impossible
470 * in the general case, but can happen if e.g. we play a network stream
471 * using a timed URL, such that the server doesn't start the video with a
473 if( p_sys->b_decoded_first_keyframe )
474 theora_decode_YUVout( &p_sys->td, &yuv );
478 /* Get a new picture */
479 p_pic = p_dec->pf_vout_buffer_new( p_dec );
480 if( !p_pic ) return NULL;
482 theora_CopyPicture( p_dec, p_pic, &yuv );
484 p_pic->date = p_sys->i_pts;
489 /*****************************************************************************
490 * ParseTheoraComments: FIXME should be done in demuxer
491 *****************************************************************************/
492 static void ParseTheoraComments( decoder_t *p_dec )
494 input_thread_t *p_input = (input_thread_t *)p_dec->p_parent;
495 char *psz_name, *psz_value, *psz_comment;
498 if( p_input->i_object_type != VLC_OBJECT_INPUT ) return;
500 while ( i < p_dec->p_sys->tc.comments )
502 psz_comment = strdup( p_dec->p_sys->tc.user_comments[i] );
505 msg_Warn( p_dec, "out of memory" );
508 psz_name = psz_comment;
509 psz_value = strchr( psz_comment, '=' );
514 input_Control( p_input, INPUT_ADD_INFO, _("Theora comment"),
515 psz_name, "%s", psz_value );
522 /*****************************************************************************
523 * CloseDecoder: theora decoder destruction
524 *****************************************************************************/
525 static void CloseDecoder( vlc_object_t *p_this )
527 decoder_t *p_dec = (decoder_t *)p_this;
528 decoder_sys_t *p_sys = p_dec->p_sys;
530 theora_info_clear( &p_sys->ti );
531 theora_comment_clear( &p_sys->tc );
536 /*****************************************************************************
537 * theora_CopyPicture: copy a picture from theora internal buffers to a
538 * picture_t structure.
539 *****************************************************************************/
540 static void theora_CopyPicture( decoder_t *p_dec, picture_t *p_pic,
543 int i_plane, i_line, i_width, i_dst_stride, i_src_stride;
544 int i_src_xoffset, i_src_yoffset;
545 uint8_t *p_dst, *p_src;
547 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
549 p_dst = p_pic->p[i_plane].p_pixels;
550 p_src = i_plane ? (i_plane - 1 ? yuv->v : yuv->u ) : yuv->y;
551 i_width = p_pic->p[i_plane].i_visible_pitch;
552 i_dst_stride = p_pic->p[i_plane].i_pitch;
553 i_src_stride = i_plane ? yuv->uv_stride : yuv->y_stride;
554 i_src_xoffset = p_dec->p_sys->ti.offset_x;
555 i_src_yoffset = p_dec->p_sys->ti.offset_y;
562 p_src += (i_src_yoffset * i_src_stride + i_src_xoffset);
564 for( i_line = 0; i_line < p_pic->p[i_plane].i_visible_lines; i_line++ )
566 p_dec->p_libvlc->pf_memcpy( p_dst, p_src + i_src_xoffset,
567 i_plane ? yuv->uv_width : yuv->y_width );
568 p_src += i_src_stride;
569 p_dst += i_dst_stride;
574 /*****************************************************************************
575 * encoder_sys_t : theora encoder descriptor
576 *****************************************************************************/
582 vlc_bool_t b_headers;
587 theora_info ti; /* theora bitstream settings */
588 theora_comment tc; /* theora comment header */
589 theora_state td; /* theora bitstream user comments */
591 int i_width, i_height;
594 /*****************************************************************************
595 * OpenEncoder: probe the encoder and return score
596 *****************************************************************************/
597 static int OpenEncoder( vlc_object_t *p_this )
599 encoder_t *p_enc = (encoder_t *)p_this;
600 encoder_sys_t *p_sys = p_enc->p_sys;
606 if( p_enc->fmt_out.i_codec != VLC_FOURCC('t','h','e','o') &&
612 /* Allocate the memory needed to store the decoder's structure */
613 if( ( p_sys = (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
615 msg_Err( p_enc, "out of memory" );
618 p_enc->p_sys = p_sys;
620 p_enc->pf_encode_video = Encode;
621 p_enc->fmt_in.i_codec = VLC_FOURCC('I','4','2','0');
622 p_enc->fmt_out.i_codec = VLC_FOURCC('t','h','e','o');
624 config_ChainParse( p_enc, ENC_CFG_PREFIX, ppsz_enc_options, p_enc->p_cfg );
626 var_Get( p_enc, ENC_CFG_PREFIX "quality", &val );
627 i_quality = val.i_int;
628 if( i_quality > 10 ) i_quality = 10;
629 if( i_quality < 0 ) i_quality = 0;
631 theora_info_init( &p_sys->ti );
633 p_sys->ti.width = p_enc->fmt_in.video.i_width;
634 p_sys->ti.height = p_enc->fmt_in.video.i_height;
636 if( p_sys->ti.width % 16 || p_sys->ti.height % 16 )
638 /* Pictures from the transcoder should always have a pitch
639 * which is a multiple of 16 */
640 p_sys->ti.width = (p_sys->ti.width + 15) >> 4 << 4;
641 p_sys->ti.height = (p_sys->ti.height + 15) >> 4 << 4;
643 msg_Dbg( p_enc, "padding video from %dx%d to %dx%d",
644 p_enc->fmt_in.video.i_width, p_enc->fmt_in.video.i_height,
645 p_sys->ti.width, p_sys->ti.height );
648 p_sys->ti.frame_width = p_enc->fmt_in.video.i_width;
649 p_sys->ti.frame_height = p_enc->fmt_in.video.i_height;
650 p_sys->ti.offset_x = 0 /*frame_x_offset*/;
651 p_sys->ti.offset_y = 0 /*frame_y_offset*/;
653 p_sys->i_width = p_sys->ti.width;
654 p_sys->i_height = p_sys->ti.height;
656 if( !p_enc->fmt_in.video.i_frame_rate ||
657 !p_enc->fmt_in.video.i_frame_rate_base )
659 p_sys->ti.fps_numerator = 25;
660 p_sys->ti.fps_denominator = 1;
664 p_sys->ti.fps_numerator = p_enc->fmt_in.video.i_frame_rate;
665 p_sys->ti.fps_denominator = p_enc->fmt_in.video.i_frame_rate_base;
668 if( p_enc->fmt_in.video.i_aspect )
670 uint64_t i_num, i_den;
671 unsigned i_dst_num, i_dst_den;
673 i_num = p_enc->fmt_in.video.i_aspect * (int64_t)p_sys->ti.height;
674 i_den = VOUT_ASPECT_FACTOR * p_sys->ti.width;
675 vlc_ureduce( &i_dst_num, &i_dst_den, i_num, i_den, 0 );
676 p_sys->ti.aspect_numerator = i_dst_num;
677 p_sys->ti.aspect_denominator = i_dst_den;
681 p_sys->ti.aspect_numerator = 4;
682 p_sys->ti.aspect_denominator = 3;
685 p_sys->ti.target_bitrate = p_enc->fmt_out.i_bitrate;
686 p_sys->ti.quality = ((float)i_quality) * 6.3;
688 p_sys->ti.dropframes_p = 0;
689 p_sys->ti.quick_p = 1;
690 p_sys->ti.keyframe_auto_p = 1;
691 p_sys->ti.keyframe_frequency = 64;
692 p_sys->ti.keyframe_frequency_force = 64;
693 p_sys->ti.keyframe_data_target_bitrate = p_enc->fmt_out.i_bitrate * 1.5;
694 p_sys->ti.keyframe_auto_threshold = 80;
695 p_sys->ti.keyframe_mindistance = 8;
696 p_sys->ti.noise_sensitivity = 1;
698 theora_encode_init( &p_sys->td, &p_sys->ti );
699 theora_info_clear( &p_sys->ti );
700 theora_comment_init( &p_sys->tc );
702 /* Create and store headers */
703 p_enc->fmt_out.i_extra = 3 * 2;
704 for( i = 0; i < 3; i++ )
706 if( i == 0 ) theora_encode_header( &p_sys->td, &header );
707 else if( i == 1 ) theora_encode_comment( &p_sys->tc, &header );
708 else if( i == 2 ) theora_encode_tables( &p_sys->td, &header );
710 p_enc->fmt_out.p_extra =
711 realloc( p_enc->fmt_out.p_extra,
712 p_enc->fmt_out.i_extra + header.bytes );
713 p_extra = p_enc->fmt_out.p_extra;
714 p_extra += p_enc->fmt_out.i_extra + (i-3)*2;
715 p_enc->fmt_out.i_extra += header.bytes;
717 *(p_extra++) = header.bytes >> 8;
718 *(p_extra++) = header.bytes & 0xFF;
720 memcpy( p_extra, header.packet, header.bytes );
726 /****************************************************************************
727 * Encode: the whole thing
728 ****************************************************************************
729 * This function spits out ogg packets.
730 ****************************************************************************/
731 static block_t *Encode( encoder_t *p_enc, picture_t *p_pict )
733 encoder_sys_t *p_sys = p_enc->p_sys;
734 ogg_packet oggpacket;
740 if( p_pict->p[0].i_pitch < (int)p_sys->i_width ||
741 p_pict->p[0].i_lines < (int)p_sys->i_height )
743 msg_Warn( p_enc, "frame is smaller than encoding size"
744 "(%ix%i->%ix%i) -> dropping frame",
745 p_pict->p[0].i_pitch, p_pict->p[0].i_lines,
746 p_sys->i_width, p_sys->i_height );
751 if( p_pict->p[0].i_visible_pitch < (int)p_sys->i_width )
753 for( i = 0; i < p_sys->i_height; i++ )
755 memset( p_pict->p[0].p_pixels + i * p_pict->p[0].i_pitch +
756 p_pict->p[0].i_visible_pitch,
757 *( p_pict->p[0].p_pixels + i * p_pict->p[0].i_pitch +
758 p_pict->p[0].i_visible_pitch - 1 ),
759 p_sys->i_width - p_pict->p[0].i_visible_pitch );
761 for( i = 0; i < p_sys->i_height / 2; i++ )
763 memset( p_pict->p[1].p_pixels + i * p_pict->p[1].i_pitch +
764 p_pict->p[1].i_visible_pitch,
765 *( p_pict->p[1].p_pixels + i * p_pict->p[1].i_pitch +
766 p_pict->p[1].i_visible_pitch - 1 ),
767 p_sys->i_width / 2 - p_pict->p[1].i_visible_pitch );
768 memset( p_pict->p[2].p_pixels + i * p_pict->p[2].i_pitch +
769 p_pict->p[2].i_visible_pitch,
770 *( p_pict->p[2].p_pixels + i * p_pict->p[2].i_pitch +
771 p_pict->p[2].i_visible_pitch - 1 ),
772 p_sys->i_width / 2 - p_pict->p[2].i_visible_pitch );
776 if( p_pict->p[0].i_visible_lines < (int)p_sys->i_height )
778 for( i = p_pict->p[0].i_visible_lines; i < p_sys->i_height; i++ )
780 memset( p_pict->p[0].p_pixels + i * p_pict->p[0].i_pitch, 0,
783 for( i = p_pict->p[1].i_visible_lines; i < p_sys->i_height / 2; i++ )
785 memset( p_pict->p[1].p_pixels + i * p_pict->p[1].i_pitch, 0x80,
786 p_sys->i_width / 2 );
787 memset( p_pict->p[2].p_pixels + i * p_pict->p[2].i_pitch, 0x80,
788 p_sys->i_width / 2 );
792 /* Theora is a one-frame-in, one-frame-out system. Submit a frame
793 * for compression and pull out the packet. */
795 yuv.y_width = p_sys->i_width;
796 yuv.y_height = p_sys->i_height;
797 yuv.y_stride = p_pict->p[0].i_pitch;
799 yuv.uv_width = p_sys->i_width / 2;
800 yuv.uv_height = p_sys->i_height / 2;
801 yuv.uv_stride = p_pict->p[1].i_pitch;
803 yuv.y = p_pict->p[0].p_pixels;
804 yuv.u = p_pict->p[1].p_pixels;
805 yuv.v = p_pict->p[2].p_pixels;
807 if( theora_encode_YUVin( &p_sys->td, &yuv ) < 0 )
809 msg_Warn( p_enc, "failed encoding a frame" );
813 theora_encode_packetout( &p_sys->td, 0, &oggpacket );
815 /* Ogg packet to block */
816 p_block = block_New( p_enc, oggpacket.bytes );
817 memcpy( p_block->p_buffer, oggpacket.packet, oggpacket.bytes );
818 p_block->i_dts = p_block->i_pts = p_pict->date;
823 /*****************************************************************************
824 * CloseEncoder: theora encoder destruction
825 *****************************************************************************/
826 static void CloseEncoder( vlc_object_t *p_this )
828 encoder_t *p_enc = (encoder_t *)p_this;
829 encoder_sys_t *p_sys = p_enc->p_sys;
831 theora_info_clear( &p_sys->ti );
832 theora_comment_clear( &p_sys->tc );