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 *****************************************************************************/
28 #include <vlc_codec.h>
31 #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 "Enforce 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_shortname( "Theora" );
99 set_description( _("Theora video decoder") );
100 set_capability( "decoder", 100 );
101 set_callbacks( OpenDecoder, CloseDecoder );
102 add_shortcut( "theora" );
105 set_description( _("Theora video packetizer") );
106 set_capability( "packetizer", 100 );
107 set_callbacks( OpenPacketizer, CloseDecoder );
110 set_description( _("Theora video encoder") );
111 set_capability( "encoder", 150 );
112 set_callbacks( OpenEncoder, CloseEncoder );
114 # define ENC_CFG_PREFIX "sout-theora-"
115 add_integer( ENC_CFG_PREFIX "quality", 2, NULL, ENC_QUALITY_TEXT,
116 ENC_QUALITY_LONGTEXT, VLC_FALSE );
119 static const char *ppsz_enc_options[] = {
123 /*****************************************************************************
124 * OpenDecoder: probe the decoder and return score
125 *****************************************************************************/
126 static int OpenDecoder( vlc_object_t *p_this )
128 decoder_t *p_dec = (decoder_t*)p_this;
129 decoder_sys_t *p_sys;
131 if( p_dec->fmt_in.i_codec != VLC_FOURCC('t','h','e','o') )
136 /* Allocate the memory needed to store the decoder's structure */
137 if( ( p_dec->p_sys = p_sys =
138 (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
140 msg_Err( p_dec, "out of memory" );
143 p_dec->p_sys->b_packetizer = VLC_FALSE;
146 p_sys->b_decoded_first_keyframe = VLC_FALSE;
148 /* Set output properties */
149 p_dec->fmt_out.i_cat = VIDEO_ES;
150 p_dec->fmt_out.i_codec = VLC_FOURCC('I','4','2','0');
153 p_dec->pf_decode_video = (picture_t *(*)(decoder_t *, block_t **))
155 p_dec->pf_packetize = (block_t *(*)(decoder_t *, block_t **))
158 /* Init supporting Theora structures needed in header parsing */
159 theora_comment_init( &p_sys->tc );
160 theora_info_init( &p_sys->ti );
162 p_sys->i_headers = 0;
167 static int OpenPacketizer( vlc_object_t *p_this )
169 decoder_t *p_dec = (decoder_t*)p_this;
171 int i_ret = OpenDecoder( p_this );
173 if( i_ret == VLC_SUCCESS )
175 p_dec->p_sys->b_packetizer = VLC_TRUE;
176 p_dec->fmt_out.i_codec = VLC_FOURCC( 't', 'h', 'e', 'o' );
182 /****************************************************************************
183 * DecodeBlock: the whole thing
184 ****************************************************************************
185 * This function must be fed with ogg packets.
186 ****************************************************************************/
187 static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
189 decoder_sys_t *p_sys = p_dec->p_sys;
191 ogg_packet oggpacket;
193 if( !pp_block || !*pp_block ) return NULL;
197 /* Block to Ogg packet */
198 oggpacket.packet = p_block->p_buffer;
199 oggpacket.bytes = p_block->i_buffer;
200 oggpacket.granulepos = p_block->i_dts;
203 oggpacket.packetno = 0;
205 /* Check for headers */
206 if( p_sys->i_headers == 0 && p_dec->fmt_in.i_extra )
208 /* Headers already available as extra data */
209 p_sys->i_headers = 3;
211 else if( oggpacket.bytes && p_sys->i_headers < 3 )
213 /* Backup headers as extra data */
216 p_dec->fmt_in.p_extra =
217 realloc( p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra +
218 oggpacket.bytes + 2 );
219 p_extra = p_dec->fmt_in.p_extra + p_dec->fmt_in.i_extra;
220 *(p_extra++) = oggpacket.bytes >> 8;
221 *(p_extra++) = oggpacket.bytes & 0xFF;
223 memcpy( p_extra, oggpacket.packet, oggpacket.bytes );
224 p_dec->fmt_in.i_extra += oggpacket.bytes + 2;
226 block_Release( *pp_block );
231 if( p_sys->i_headers == 3 )
233 if( ProcessHeaders( p_dec ) != VLC_SUCCESS )
235 p_sys->i_headers = 0;
236 p_dec->fmt_in.i_extra = 0;
237 block_Release( *pp_block );
240 else p_sys->i_headers++;
243 return ProcessPacket( p_dec, &oggpacket, pp_block );
246 /*****************************************************************************
247 * ProcessHeaders: process Theora headers.
248 *****************************************************************************/
249 static int ProcessHeaders( decoder_t *p_dec )
251 decoder_sys_t *p_sys = p_dec->p_sys;
252 ogg_packet oggpacket;
256 if( !p_dec->fmt_in.i_extra ) return VLC_EGENERIC;
258 oggpacket.granulepos = -1;
259 oggpacket.b_o_s = 1; /* yes this actually is a b_o_s packet :) */
261 oggpacket.packetno = 0;
262 p_extra = p_dec->fmt_in.p_extra;
263 i_extra = p_dec->fmt_in.i_extra;
265 /* Take care of the initial Vorbis header */
266 oggpacket.bytes = *(p_extra++) << 8;
267 oggpacket.bytes |= (*(p_extra++) & 0xFF);
268 oggpacket.packet = p_extra;
269 p_extra += oggpacket.bytes;
270 i_extra -= (oggpacket.bytes + 2);
273 msg_Err( p_dec, "header data corrupted");
277 if( theora_decode_header( &p_sys->ti, &p_sys->tc, &oggpacket ) < 0 )
279 msg_Err( p_dec, "this bitstream does not contain Theora video data" );
283 /* Set output properties */
284 p_dec->fmt_out.video.i_width = p_sys->ti.width;
285 p_dec->fmt_out.video.i_height = p_sys->ti.height;
286 if( p_sys->ti.frame_width && p_sys->ti.frame_height )
288 p_dec->fmt_out.video.i_width = p_sys->ti.frame_width;
289 p_dec->fmt_out.video.i_height = p_sys->ti.frame_height;
292 if( p_sys->ti.aspect_denominator && p_sys->ti.aspect_numerator )
294 p_dec->fmt_out.video.i_aspect = ((int64_t)VOUT_ASPECT_FACTOR) *
295 ( p_sys->ti.aspect_numerator * p_dec->fmt_out.video.i_width ) /
296 ( p_sys->ti.aspect_denominator * p_dec->fmt_out.video.i_height );
300 p_dec->fmt_out.video.i_aspect = VOUT_ASPECT_FACTOR *
301 p_sys->ti.frame_width / p_sys->ti.frame_height;
304 if( p_sys->ti.fps_numerator > 0 && p_sys->ti.fps_denominator > 0 )
306 p_dec->fmt_out.video.i_frame_rate = p_sys->ti.fps_numerator;
307 p_dec->fmt_out.video.i_frame_rate_base = p_sys->ti.fps_denominator;
310 msg_Dbg( p_dec, "%dx%d %.02f fps video, frame content "
311 "is %dx%d with offset (%d,%d)",
312 p_sys->ti.width, p_sys->ti.height,
313 (double)p_sys->ti.fps_numerator/p_sys->ti.fps_denominator,
314 p_sys->ti.frame_width, p_sys->ti.frame_height,
315 p_sys->ti.offset_x, p_sys->ti.offset_y );
317 /* Sanity check that seems necessary for some corrupted files */
318 if( p_sys->ti.width < p_sys->ti.frame_width ||
319 p_sys->ti.height < p_sys->ti.frame_height )
321 msg_Warn( p_dec, "trying to correct invalid theora header "
322 "(frame size (%dx%d) is smaller than frame content (%d,%d))",
323 p_sys->ti.width, p_sys->ti.height,
324 p_sys->ti.frame_width, p_sys->ti.frame_height );
326 if( p_sys->ti.width < p_sys->ti.frame_width )
327 p_sys->ti.width = p_sys->ti.frame_width;
328 if( p_sys->ti.height < p_sys->ti.frame_height )
329 p_sys->ti.height = p_sys->ti.frame_height;
332 /* The next packet in order is the comments header */
334 oggpacket.bytes = *(p_extra++) << 8;
335 oggpacket.bytes |= (*(p_extra++) & 0xFF);
336 oggpacket.packet = p_extra;
337 p_extra += oggpacket.bytes;
338 i_extra -= (oggpacket.bytes + 2);
341 msg_Err( p_dec, "header data corrupted");
345 /* The next packet in order is the comments header */
346 if( theora_decode_header( &p_sys->ti, &p_sys->tc, &oggpacket ) < 0 )
348 msg_Err( p_dec, "2nd Theora header is corrupted" );
352 ParseTheoraComments( p_dec );
354 /* The next packet in order is the codebooks header
355 * We need to watch out that this packet is not missing as a
356 * missing or corrupted header is fatal. */
357 oggpacket.bytes = *(p_extra++) << 8;
358 oggpacket.bytes |= (*(p_extra++) & 0xFF);
359 oggpacket.packet = p_extra;
360 i_extra -= (oggpacket.bytes + 2);
363 msg_Err( p_dec, "header data corrupted");
367 /* The next packet in order is the codebooks header
368 * We need to watch out that this packet is not missing as a
369 * missing or corrupted header is fatal */
370 if( theora_decode_header( &p_sys->ti, &p_sys->tc, &oggpacket ) < 0 )
372 msg_Err( p_dec, "3rd Theora header is corrupted" );
376 if( !p_sys->b_packetizer )
378 /* We have all the headers, initialize decoder */
379 theora_decode_init( &p_sys->td, &p_sys->ti );
383 p_dec->fmt_out.i_extra = p_dec->fmt_in.i_extra;
384 p_dec->fmt_out.p_extra =
385 realloc( p_dec->fmt_out.p_extra, p_dec->fmt_out.i_extra );
386 memcpy( p_dec->fmt_out.p_extra,
387 p_dec->fmt_in.p_extra, p_dec->fmt_out.i_extra );
393 /*****************************************************************************
394 * ProcessPacket: processes a theora packet.
395 *****************************************************************************/
396 static void *ProcessPacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
399 decoder_sys_t *p_sys = p_dec->p_sys;
400 block_t *p_block = *pp_block;
403 if( ( p_block->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) ) != 0 )
405 /* Don't send the the first packet after a discontinuity to
406 * theora_decode, otherwise we get purple/green display artifacts
407 * appearing in the video output */
411 /* Date management */
412 if( p_block->i_pts > 0 && p_block->i_pts != p_sys->i_pts )
414 p_sys->i_pts = p_block->i_pts;
417 *pp_block = NULL; /* To avoid being fed the same packet again */
419 if( p_sys->b_packetizer )
421 /* Date management */
422 p_block->i_dts = p_block->i_pts = p_sys->i_pts;
424 if( p_sys->i_headers >= 3 )
425 p_block->i_length = p_sys->i_pts - p_block->i_pts;
427 p_block->i_length = 0;
433 if( p_sys->i_headers >= 3 )
434 p_buf = DecodePacket( p_dec, p_oggpacket );
438 if( p_block ) block_Release( p_block );
441 /* Date management */
442 p_sys->i_pts += ( I64C(1000000) * p_sys->ti.fps_denominator /
443 p_sys->ti.fps_numerator ); /* 1 frame per packet */
448 /*****************************************************************************
449 * DecodePacket: decodes a Theora packet.
450 *****************************************************************************/
451 static picture_t *DecodePacket( decoder_t *p_dec, ogg_packet *p_oggpacket )
453 decoder_sys_t *p_sys = p_dec->p_sys;
457 theora_decode_packetin( &p_sys->td, p_oggpacket );
459 /* Check for keyframe */
460 if( !(p_oggpacket->packet[0] & 0x80) /* data packet */ &&
461 !(p_oggpacket->packet[0] & 0x40) /* intra frame */ )
462 p_sys->b_decoded_first_keyframe = VLC_TRUE;
464 /* If we haven't seen a single keyframe yet, don't let Theora decode
465 * anything, otherwise we'll get display artifacts. (This is impossible
466 * in the general case, but can happen if e.g. we play a network stream
467 * using a timed URL, such that the server doesn't start the video with a
469 if( p_sys->b_decoded_first_keyframe )
470 theora_decode_YUVout( &p_sys->td, &yuv );
474 /* Get a new picture */
475 p_pic = p_dec->pf_vout_buffer_new( p_dec );
476 if( !p_pic ) return NULL;
478 theora_CopyPicture( p_dec, p_pic, &yuv );
480 p_pic->date = p_sys->i_pts;
485 /*****************************************************************************
486 * ParseTheoraComments: FIXME should be done in demuxer
487 *****************************************************************************/
488 static void ParseTheoraComments( decoder_t *p_dec )
490 input_thread_t *p_input = (input_thread_t *)p_dec->p_parent;
491 char *psz_name, *psz_value, *psz_comment;
494 if( p_input->i_object_type != VLC_OBJECT_INPUT ) return;
496 while ( i < p_dec->p_sys->tc.comments )
498 psz_comment = strdup( p_dec->p_sys->tc.user_comments[i] );
501 msg_Warn( p_dec, "out of memory" );
504 psz_name = psz_comment;
505 psz_value = strchr( psz_comment, '=' );
510 input_Control( p_input, INPUT_ADD_INFO, _("Theora comment"),
511 psz_name, "%s", psz_value );
518 /*****************************************************************************
519 * CloseDecoder: theora decoder destruction
520 *****************************************************************************/
521 static void CloseDecoder( vlc_object_t *p_this )
523 decoder_t *p_dec = (decoder_t *)p_this;
524 decoder_sys_t *p_sys = p_dec->p_sys;
526 theora_info_clear( &p_sys->ti );
527 theora_comment_clear( &p_sys->tc );
532 /*****************************************************************************
533 * theora_CopyPicture: copy a picture from theora internal buffers to a
534 * picture_t structure.
535 *****************************************************************************/
536 static void theora_CopyPicture( decoder_t *p_dec, picture_t *p_pic,
539 int i_plane, i_line, i_width, i_dst_stride, i_src_stride;
540 int i_src_xoffset, i_src_yoffset;
541 uint8_t *p_dst, *p_src;
543 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
545 p_dst = p_pic->p[i_plane].p_pixels;
546 p_src = i_plane ? (i_plane - 1 ? yuv->v : yuv->u ) : yuv->y;
547 i_width = p_pic->p[i_plane].i_visible_pitch;
548 i_dst_stride = p_pic->p[i_plane].i_pitch;
549 i_src_stride = i_plane ? yuv->uv_stride : yuv->y_stride;
550 i_src_xoffset = p_dec->p_sys->ti.offset_x;
551 i_src_yoffset = p_dec->p_sys->ti.offset_y;
558 p_src += (i_src_yoffset * i_src_stride + i_src_xoffset);
560 for( i_line = 0; i_line < p_pic->p[i_plane].i_visible_lines; i_line++ )
562 p_dec->p_libvlc->pf_memcpy( p_dst, p_src + i_src_xoffset,
563 i_plane ? yuv->uv_width : yuv->y_width );
564 p_src += i_src_stride;
565 p_dst += i_dst_stride;
570 /*****************************************************************************
571 * encoder_sys_t : theora encoder descriptor
572 *****************************************************************************/
578 vlc_bool_t b_headers;
583 theora_info ti; /* theora bitstream settings */
584 theora_comment tc; /* theora comment header */
585 theora_state td; /* theora bitstream user comments */
587 int i_width, i_height;
590 /*****************************************************************************
591 * OpenEncoder: probe the encoder and return score
592 *****************************************************************************/
593 static int OpenEncoder( vlc_object_t *p_this )
595 encoder_t *p_enc = (encoder_t *)p_this;
596 encoder_sys_t *p_sys = p_enc->p_sys;
602 if( p_enc->fmt_out.i_codec != VLC_FOURCC('t','h','e','o') &&
608 /* Allocate the memory needed to store the decoder's structure */
609 if( ( p_sys = (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
611 msg_Err( p_enc, "out of memory" );
614 p_enc->p_sys = p_sys;
616 p_enc->pf_encode_video = Encode;
617 p_enc->fmt_in.i_codec = VLC_FOURCC('I','4','2','0');
618 p_enc->fmt_out.i_codec = VLC_FOURCC('t','h','e','o');
620 config_ChainParse( p_enc, ENC_CFG_PREFIX, ppsz_enc_options, p_enc->p_cfg );
622 var_Get( p_enc, ENC_CFG_PREFIX "quality", &val );
623 i_quality = val.i_int;
624 if( i_quality > 10 ) i_quality = 10;
625 if( i_quality < 0 ) i_quality = 0;
627 theora_info_init( &p_sys->ti );
629 p_sys->ti.width = p_enc->fmt_in.video.i_width;
630 p_sys->ti.height = p_enc->fmt_in.video.i_height;
632 if( p_sys->ti.width % 16 || p_sys->ti.height % 16 )
634 /* Pictures from the transcoder should always have a pitch
635 * which is a multiple of 16 */
636 p_sys->ti.width = (p_sys->ti.width + 15) >> 4 << 4;
637 p_sys->ti.height = (p_sys->ti.height + 15) >> 4 << 4;
639 msg_Dbg( p_enc, "padding video from %dx%d to %dx%d",
640 p_enc->fmt_in.video.i_width, p_enc->fmt_in.video.i_height,
641 p_sys->ti.width, p_sys->ti.height );
644 p_sys->ti.frame_width = p_enc->fmt_in.video.i_width;
645 p_sys->ti.frame_height = p_enc->fmt_in.video.i_height;
646 p_sys->ti.offset_x = 0 /*frame_x_offset*/;
647 p_sys->ti.offset_y = 0 /*frame_y_offset*/;
649 p_sys->i_width = p_sys->ti.width;
650 p_sys->i_height = p_sys->ti.height;
652 if( !p_enc->fmt_in.video.i_frame_rate ||
653 !p_enc->fmt_in.video.i_frame_rate_base )
655 p_sys->ti.fps_numerator = 25;
656 p_sys->ti.fps_denominator = 1;
660 p_sys->ti.fps_numerator = p_enc->fmt_in.video.i_frame_rate;
661 p_sys->ti.fps_denominator = p_enc->fmt_in.video.i_frame_rate_base;
664 if( p_enc->fmt_in.video.i_aspect )
666 uint64_t i_num, i_den;
667 unsigned i_dst_num, i_dst_den;
669 i_num = p_enc->fmt_in.video.i_aspect * (int64_t)p_sys->ti.height;
670 i_den = VOUT_ASPECT_FACTOR * p_sys->ti.width;
671 vlc_ureduce( &i_dst_num, &i_dst_den, i_num, i_den, 0 );
672 p_sys->ti.aspect_numerator = i_dst_num;
673 p_sys->ti.aspect_denominator = i_dst_den;
677 p_sys->ti.aspect_numerator = 4;
678 p_sys->ti.aspect_denominator = 3;
681 p_sys->ti.target_bitrate = p_enc->fmt_out.i_bitrate;
682 p_sys->ti.quality = ((float)i_quality) * 6.3;
684 p_sys->ti.dropframes_p = 0;
685 p_sys->ti.quick_p = 1;
686 p_sys->ti.keyframe_auto_p = 1;
687 p_sys->ti.keyframe_frequency = 64;
688 p_sys->ti.keyframe_frequency_force = 64;
689 p_sys->ti.keyframe_data_target_bitrate = p_enc->fmt_out.i_bitrate * 1.5;
690 p_sys->ti.keyframe_auto_threshold = 80;
691 p_sys->ti.keyframe_mindistance = 8;
692 p_sys->ti.noise_sensitivity = 1;
694 theora_encode_init( &p_sys->td, &p_sys->ti );
695 theora_info_clear( &p_sys->ti );
696 theora_comment_init( &p_sys->tc );
698 /* Create and store headers */
699 p_enc->fmt_out.i_extra = 3 * 2;
700 for( i = 0; i < 3; i++ )
702 if( i == 0 ) theora_encode_header( &p_sys->td, &header );
703 else if( i == 1 ) theora_encode_comment( &p_sys->tc, &header );
704 else if( i == 2 ) theora_encode_tables( &p_sys->td, &header );
706 p_enc->fmt_out.p_extra =
707 realloc( p_enc->fmt_out.p_extra,
708 p_enc->fmt_out.i_extra + header.bytes );
709 p_extra = p_enc->fmt_out.p_extra;
710 p_extra += p_enc->fmt_out.i_extra + (i-3)*2;
711 p_enc->fmt_out.i_extra += header.bytes;
713 *(p_extra++) = header.bytes >> 8;
714 *(p_extra++) = header.bytes & 0xFF;
716 memcpy( p_extra, header.packet, header.bytes );
722 /****************************************************************************
723 * Encode: the whole thing
724 ****************************************************************************
725 * This function spits out ogg packets.
726 ****************************************************************************/
727 static block_t *Encode( encoder_t *p_enc, picture_t *p_pict )
729 encoder_sys_t *p_sys = p_enc->p_sys;
730 ogg_packet oggpacket;
736 if( p_pict->p[0].i_pitch < (int)p_sys->i_width ||
737 p_pict->p[0].i_lines < (int)p_sys->i_height )
739 msg_Warn( p_enc, "frame is smaller than encoding size"
740 "(%ix%i->%ix%i) -> dropping frame",
741 p_pict->p[0].i_pitch, p_pict->p[0].i_lines,
742 p_sys->i_width, p_sys->i_height );
747 if( p_pict->p[0].i_visible_pitch < (int)p_sys->i_width )
749 for( i = 0; i < p_sys->i_height; i++ )
751 memset( p_pict->p[0].p_pixels + i * p_pict->p[0].i_pitch +
752 p_pict->p[0].i_visible_pitch,
753 *( p_pict->p[0].p_pixels + i * p_pict->p[0].i_pitch +
754 p_pict->p[0].i_visible_pitch - 1 ),
755 p_sys->i_width - p_pict->p[0].i_visible_pitch );
757 for( i = 0; i < p_sys->i_height / 2; i++ )
759 memset( p_pict->p[1].p_pixels + i * p_pict->p[1].i_pitch +
760 p_pict->p[1].i_visible_pitch,
761 *( p_pict->p[1].p_pixels + i * p_pict->p[1].i_pitch +
762 p_pict->p[1].i_visible_pitch - 1 ),
763 p_sys->i_width / 2 - p_pict->p[1].i_visible_pitch );
764 memset( p_pict->p[2].p_pixels + i * p_pict->p[2].i_pitch +
765 p_pict->p[2].i_visible_pitch,
766 *( p_pict->p[2].p_pixels + i * p_pict->p[2].i_pitch +
767 p_pict->p[2].i_visible_pitch - 1 ),
768 p_sys->i_width / 2 - p_pict->p[2].i_visible_pitch );
772 if( p_pict->p[0].i_visible_lines < (int)p_sys->i_height )
774 for( i = p_pict->p[0].i_visible_lines; i < p_sys->i_height; i++ )
776 memset( p_pict->p[0].p_pixels + i * p_pict->p[0].i_pitch, 0,
779 for( i = p_pict->p[1].i_visible_lines; i < p_sys->i_height / 2; i++ )
781 memset( p_pict->p[1].p_pixels + i * p_pict->p[1].i_pitch, 0x80,
782 p_sys->i_width / 2 );
783 memset( p_pict->p[2].p_pixels + i * p_pict->p[2].i_pitch, 0x80,
784 p_sys->i_width / 2 );
788 /* Theora is a one-frame-in, one-frame-out system. Submit a frame
789 * for compression and pull out the packet. */
791 yuv.y_width = p_sys->i_width;
792 yuv.y_height = p_sys->i_height;
793 yuv.y_stride = p_pict->p[0].i_pitch;
795 yuv.uv_width = p_sys->i_width / 2;
796 yuv.uv_height = p_sys->i_height / 2;
797 yuv.uv_stride = p_pict->p[1].i_pitch;
799 yuv.y = p_pict->p[0].p_pixels;
800 yuv.u = p_pict->p[1].p_pixels;
801 yuv.v = p_pict->p[2].p_pixels;
803 if( theora_encode_YUVin( &p_sys->td, &yuv ) < 0 )
805 msg_Warn( p_enc, "failed encoding a frame" );
809 theora_encode_packetout( &p_sys->td, 0, &oggpacket );
811 /* Ogg packet to block */
812 p_block = block_New( p_enc, oggpacket.bytes );
813 memcpy( p_block->p_buffer, oggpacket.packet, oggpacket.bytes );
814 p_block->i_dts = p_block->i_pts = p_pict->date;
819 /*****************************************************************************
820 * CloseEncoder: theora encoder destruction
821 *****************************************************************************/
822 static void CloseEncoder( vlc_object_t *p_this )
824 encoder_t *p_enc = (encoder_t *)p_this;
825 encoder_sys_t *p_sys = p_enc->p_sys;
827 theora_info_clear( &p_sys->ti );
828 theora_comment_clear( &p_sys->tc );