Avcodec: deactivate DR for Camtasia
[vlc.git] / modules / codec / theora.c
blob6493f35027c012ee4612cf2b04de22d90ca02a88
1 /*****************************************************************************
2 * theora.c: theora decoder module making use of libtheora.
3 *****************************************************************************
4 * Copyright (C) 1999-2001 the VideoLAN team
5 * $Id$
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 /*****************************************************************************
25 * Preamble
26 *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_codec.h>
34 #include <vlc_sout.h>
35 #include <vlc_input.h>
36 #include "../demux/xiph.h"
38 #include <ogg/ogg.h>
40 #include <theora/theora.h>
42 /*****************************************************************************
43 * decoder_sys_t : theora decoder descriptor
44 *****************************************************************************/
45 struct decoder_sys_t
47 /* Module mode */
48 bool b_packetizer;
51 * Input properties
53 bool b_has_headers;
56 * Theora properties
58 theora_info ti; /* theora bitstream settings */
59 theora_comment tc; /* theora comment header */
60 theora_state td; /* theora bitstream user comments */
63 * Decoding properties
65 bool b_decoded_first_keyframe;
68 * Common properties
70 mtime_t i_pts;
73 /*****************************************************************************
74 * Local prototypes
75 *****************************************************************************/
76 static int OpenDecoder ( vlc_object_t * );
77 static int OpenPacketizer( vlc_object_t * );
78 static void CloseDecoder ( vlc_object_t * );
80 static void *DecodeBlock ( decoder_t *, block_t ** );
81 static int ProcessHeaders( decoder_t * );
82 static void *ProcessPacket ( decoder_t *, ogg_packet *, block_t ** );
84 static picture_t *DecodePacket( decoder_t *, ogg_packet * );
86 static void ParseTheoraComments( decoder_t * );
87 static void theora_CopyPicture( picture_t *, yuv_buffer * );
89 static int OpenEncoder( vlc_object_t *p_this );
90 static void CloseEncoder( vlc_object_t *p_this );
91 static block_t *Encode( encoder_t *p_enc, picture_t *p_pict );
93 /*****************************************************************************
94 * Module descriptor
95 *****************************************************************************/
96 #define ENC_QUALITY_TEXT N_("Encoding quality")
97 #define ENC_QUALITY_LONGTEXT N_( \
98 "Enforce a quality between 1 (low) and 10 (high), instead " \
99 "of specifying a particular bitrate. This will produce a VBR stream." )
101 vlc_module_begin ()
102 set_category( CAT_INPUT )
103 set_subcategory( SUBCAT_INPUT_VCODEC )
104 set_shortname( "Theora" )
105 set_description( N_("Theora video decoder") )
106 set_capability( "decoder", 100 )
107 set_callbacks( OpenDecoder, CloseDecoder )
108 add_shortcut( "theora" )
110 add_submodule ()
111 set_description( N_("Theora video packetizer") )
112 set_capability( "packetizer", 100 )
113 set_callbacks( OpenPacketizer, CloseDecoder )
114 add_shortcut( "theora" )
116 add_submodule ()
117 set_description( N_("Theora video encoder") )
118 set_capability( "encoder", 150 )
119 set_callbacks( OpenEncoder, CloseEncoder )
120 add_shortcut( "theora" )
122 # define ENC_CFG_PREFIX "sout-theora-"
123 add_integer( ENC_CFG_PREFIX "quality", 2, ENC_QUALITY_TEXT,
124 ENC_QUALITY_LONGTEXT, false )
125 vlc_module_end ()
127 static const char *const ppsz_enc_options[] = {
128 "quality", NULL
131 /*****************************************************************************
132 * OpenDecoder: probe the decoder and return score
133 *****************************************************************************/
134 static int OpenDecoder( vlc_object_t *p_this )
136 decoder_t *p_dec = (decoder_t*)p_this;
137 decoder_sys_t *p_sys;
139 if( p_dec->fmt_in.i_codec != VLC_CODEC_THEORA )
141 return VLC_EGENERIC;
144 /* Allocate the memory needed to store the decoder's structure */
145 if( ( p_dec->p_sys = p_sys = malloc(sizeof(*p_sys)) ) == NULL )
146 return VLC_ENOMEM;
147 p_dec->p_sys->b_packetizer = false;
148 p_sys->b_has_headers = false;
149 p_sys->i_pts = VLC_TS_INVALID;
150 p_sys->b_decoded_first_keyframe = false;
152 /* Set output properties */
153 p_dec->fmt_out.i_cat = VIDEO_ES;
154 p_dec->fmt_out.i_codec = VLC_CODEC_I420;
156 /* Set callbacks */
157 p_dec->pf_decode_video = (picture_t *(*)(decoder_t *, block_t **))
158 DecodeBlock;
159 p_dec->pf_packetize = (block_t *(*)(decoder_t *, block_t **))
160 DecodeBlock;
162 /* Init supporting Theora structures needed in header parsing */
163 theora_comment_init( &p_sys->tc );
164 theora_info_init( &p_sys->ti );
166 return VLC_SUCCESS;
169 static int OpenPacketizer( vlc_object_t *p_this )
171 decoder_t *p_dec = (decoder_t*)p_this;
173 int i_ret = OpenDecoder( p_this );
175 if( i_ret == VLC_SUCCESS )
177 p_dec->p_sys->b_packetizer = true;
178 p_dec->fmt_out.i_codec = VLC_CODEC_THEORA;
181 return i_ret;
184 /****************************************************************************
185 * DecodeBlock: the whole thing
186 ****************************************************************************
187 * This function must be fed with ogg packets.
188 ****************************************************************************/
189 static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
191 decoder_sys_t *p_sys = p_dec->p_sys;
192 block_t *p_block;
193 ogg_packet oggpacket;
195 if( !pp_block || !*pp_block ) return NULL;
197 p_block = *pp_block;
199 /* Block to Ogg packet */
200 oggpacket.packet = p_block->p_buffer;
201 oggpacket.bytes = p_block->i_buffer;
202 oggpacket.granulepos = p_block->i_dts;
203 oggpacket.b_o_s = 0;
204 oggpacket.e_o_s = 0;
205 oggpacket.packetno = 0;
207 /* Check for headers */
208 if( !p_sys->b_has_headers )
210 if( ProcessHeaders( p_dec ) )
212 block_Release( *pp_block );
213 return NULL;
215 p_sys->b_has_headers = true;
218 return ProcessPacket( p_dec, &oggpacket, pp_block );
221 /*****************************************************************************
222 * ProcessHeaders: process Theora headers.
223 *****************************************************************************/
224 static int ProcessHeaders( decoder_t *p_dec )
226 decoder_sys_t *p_sys = p_dec->p_sys;
227 ogg_packet oggpacket;
229 unsigned pi_size[XIPH_MAX_HEADER_COUNT];
230 void *pp_data[XIPH_MAX_HEADER_COUNT];
231 unsigned i_count;
232 if( xiph_SplitHeaders( pi_size, pp_data, &i_count,
233 p_dec->fmt_in.i_extra, p_dec->fmt_in.p_extra) )
234 return VLC_EGENERIC;
235 if( i_count < 3 )
236 goto error;
238 oggpacket.granulepos = -1;
239 oggpacket.e_o_s = 0;
240 oggpacket.packetno = 0;
242 /* Take care of the initial Vorbis header */
243 oggpacket.b_o_s = 1; /* yes this actually is a b_o_s packet :) */
244 oggpacket.bytes = pi_size[0];
245 oggpacket.packet = pp_data[0];
246 if( theora_decode_header( &p_sys->ti, &p_sys->tc, &oggpacket ) < 0 )
248 msg_Err( p_dec, "this bitstream does not contain Theora video data" );
249 goto error;
252 /* Set output properties */
253 if( !p_sys->b_packetizer )
254 switch( p_sys->ti.pixelformat )
256 case OC_PF_420:
257 p_dec->fmt_out.i_codec = VLC_CODEC_I420;
258 break;
259 case OC_PF_422:
260 p_dec->fmt_out.i_codec = VLC_CODEC_I422;
261 break;
262 case OC_PF_444:
263 p_dec->fmt_out.i_codec = VLC_CODEC_I444;
264 break;
265 case OC_PF_RSVD:
266 default:
267 msg_Err( p_dec, "unknown chroma in theora sample" );
268 break;
270 p_dec->fmt_out.video.i_width = p_sys->ti.width;
271 p_dec->fmt_out.video.i_height = p_sys->ti.height;
272 if( p_sys->ti.frame_width && p_sys->ti.frame_height )
274 p_dec->fmt_out.video.i_visible_width = p_sys->ti.frame_width;
275 p_dec->fmt_out.video.i_visible_height = p_sys->ti.frame_height;
276 if( p_sys->ti.offset_x || p_sys->ti.offset_y )
278 p_dec->fmt_out.video.i_x_offset = p_sys->ti.offset_x;
279 p_dec->fmt_out.video.i_y_offset = p_sys->ti.offset_y;
283 if( p_sys->ti.aspect_denominator && p_sys->ti.aspect_numerator )
285 p_dec->fmt_out.video.i_sar_num = p_sys->ti.aspect_numerator;
286 p_dec->fmt_out.video.i_sar_den = p_sys->ti.aspect_denominator;
288 else
290 p_dec->fmt_out.video.i_sar_num = 1;
291 p_dec->fmt_out.video.i_sar_den = 1;
294 if( p_sys->ti.fps_numerator > 0 && p_sys->ti.fps_denominator > 0 )
296 p_dec->fmt_out.video.i_frame_rate = p_sys->ti.fps_numerator;
297 p_dec->fmt_out.video.i_frame_rate_base = p_sys->ti.fps_denominator;
300 msg_Dbg( p_dec, "%dx%d %.02f fps video, frame content "
301 "is %dx%d with offset (%d,%d)",
302 p_sys->ti.width, p_sys->ti.height,
303 (double)p_sys->ti.fps_numerator/p_sys->ti.fps_denominator,
304 p_sys->ti.frame_width, p_sys->ti.frame_height,
305 p_sys->ti.offset_x, p_sys->ti.offset_y );
307 /* Sanity check that seems necessary for some corrupted files */
308 if( p_sys->ti.width < p_sys->ti.frame_width ||
309 p_sys->ti.height < p_sys->ti.frame_height )
311 msg_Warn( p_dec, "trying to correct invalid theora header "
312 "(frame size (%dx%d) is smaller than frame content (%d,%d))",
313 p_sys->ti.width, p_sys->ti.height,
314 p_sys->ti.frame_width, p_sys->ti.frame_height );
316 if( p_sys->ti.width < p_sys->ti.frame_width )
317 p_sys->ti.width = p_sys->ti.frame_width;
318 if( p_sys->ti.height < p_sys->ti.frame_height )
319 p_sys->ti.height = p_sys->ti.frame_height;
322 /* The next packet in order is the comments header */
323 oggpacket.b_o_s = 0;
324 oggpacket.bytes = pi_size[1];
325 oggpacket.packet = pp_data[1];
326 if( theora_decode_header( &p_sys->ti, &p_sys->tc, &oggpacket ) < 0 )
328 msg_Err( p_dec, "2nd Theora header is corrupted" );
329 goto error;
332 ParseTheoraComments( p_dec );
334 /* The next packet in order is the codebooks header
335 * We need to watch out that this packet is not missing as a
336 * missing or corrupted header is fatal. */
337 oggpacket.b_o_s = 0;
338 oggpacket.bytes = pi_size[2];
339 oggpacket.packet = pp_data[2];
340 if( theora_decode_header( &p_sys->ti, &p_sys->tc, &oggpacket ) < 0 )
342 msg_Err( p_dec, "3rd Theora header is corrupted" );
343 goto error;
346 if( !p_sys->b_packetizer )
348 /* We have all the headers, initialize decoder */
349 theora_decode_init( &p_sys->td, &p_sys->ti );
351 else
353 p_dec->fmt_out.i_extra = p_dec->fmt_in.i_extra;
354 p_dec->fmt_out.p_extra = xrealloc( p_dec->fmt_out.p_extra,
355 p_dec->fmt_out.i_extra );
356 memcpy( p_dec->fmt_out.p_extra,
357 p_dec->fmt_in.p_extra, p_dec->fmt_out.i_extra );
360 for( unsigned i = 0; i < i_count; i++ )
361 free( pp_data[i] );
362 return VLC_SUCCESS;
364 error:
365 for( unsigned i = 0; i < i_count; i++ )
366 free( pp_data[i] );
367 return VLC_EGENERIC;
370 /*****************************************************************************
371 * ProcessPacket: processes a theora packet.
372 *****************************************************************************/
373 static void *ProcessPacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
374 block_t **pp_block )
376 decoder_sys_t *p_sys = p_dec->p_sys;
377 block_t *p_block = *pp_block;
378 void *p_buf;
380 if( ( p_block->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) ) != 0 )
382 /* Don't send the the first packet after a discontinuity to
383 * theora_decode, otherwise we get purple/green display artifacts
384 * appearing in the video output */
385 return NULL;
388 /* Date management */
389 if( p_block->i_pts > VLC_TS_INVALID && p_block->i_pts != p_sys->i_pts )
391 p_sys->i_pts = p_block->i_pts;
394 *pp_block = NULL; /* To avoid being fed the same packet again */
396 if( p_sys->b_packetizer )
398 /* Date management */
399 p_block->i_dts = p_block->i_pts = p_sys->i_pts;
401 p_block->i_length = p_sys->i_pts - p_block->i_pts;
403 p_buf = p_block;
405 else
407 p_buf = DecodePacket( p_dec, p_oggpacket );
408 if( p_block )
409 block_Release( p_block );
412 /* Date management */
413 p_sys->i_pts += ( INT64_C(1000000) * p_sys->ti.fps_denominator /
414 p_sys->ti.fps_numerator ); /* 1 frame per packet */
416 return p_buf;
419 /*****************************************************************************
420 * DecodePacket: decodes a Theora packet.
421 *****************************************************************************/
422 static picture_t *DecodePacket( decoder_t *p_dec, ogg_packet *p_oggpacket )
424 decoder_sys_t *p_sys = p_dec->p_sys;
425 picture_t *p_pic;
426 yuv_buffer yuv;
428 theora_decode_packetin( &p_sys->td, p_oggpacket );
430 /* Check for keyframe */
431 if( !(p_oggpacket->packet[0] & 0x80) /* data packet */ &&
432 !(p_oggpacket->packet[0] & 0x40) /* intra frame */ )
433 p_sys->b_decoded_first_keyframe = true;
435 /* If we haven't seen a single keyframe yet, don't let Theora decode
436 * anything, otherwise we'll get display artifacts. (This is impossible
437 * in the general case, but can happen if e.g. we play a network stream
438 * using a timed URL, such that the server doesn't start the video with a
439 * keyframe). */
440 if( p_sys->b_decoded_first_keyframe )
441 theora_decode_YUVout( &p_sys->td, &yuv );
442 else
443 return NULL;
445 /* Get a new picture */
446 p_pic = decoder_NewPicture( p_dec );
447 if( !p_pic ) return NULL;
449 theora_CopyPicture( p_pic, &yuv );
451 p_pic->date = p_sys->i_pts;
453 return p_pic;
456 /*****************************************************************************
457 * ParseTheoraComments:
458 *****************************************************************************/
459 static void ParseTheoraComments( decoder_t *p_dec )
461 char *psz_name, *psz_value, *psz_comment;
462 int i = 0;
464 while ( i < p_dec->p_sys->tc.comments )
466 psz_comment = strdup( p_dec->p_sys->tc.user_comments[i] );
467 if( !psz_comment )
468 break;
469 psz_name = psz_comment;
470 psz_value = strchr( psz_comment, '=' );
471 if( psz_value )
473 *psz_value = '\0';
474 psz_value++;
476 if( !p_dec->p_description )
477 p_dec->p_description = vlc_meta_New();
478 if( p_dec->p_description )
479 vlc_meta_AddExtra( p_dec->p_description, psz_name, psz_value );
481 free( psz_comment );
482 i++;
486 /*****************************************************************************
487 * CloseDecoder: theora decoder destruction
488 *****************************************************************************/
489 static void CloseDecoder( vlc_object_t *p_this )
491 decoder_t *p_dec = (decoder_t *)p_this;
492 decoder_sys_t *p_sys = p_dec->p_sys;
494 theora_info_clear( &p_sys->ti );
495 theora_comment_clear( &p_sys->tc );
497 free( p_sys );
500 /*****************************************************************************
501 * theora_CopyPicture: copy a picture from theora internal buffers to a
502 * picture_t structure.
503 *****************************************************************************/
504 static void theora_CopyPicture( picture_t *p_pic,
505 yuv_buffer *yuv )
507 int i_plane, i_line, i_dst_stride, i_src_stride;
508 uint8_t *p_dst, *p_src;
510 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
512 p_dst = p_pic->p[i_plane].p_pixels;
513 p_src = i_plane ? (i_plane - 1 ? yuv->v : yuv->u ) : yuv->y;
514 i_dst_stride = p_pic->p[i_plane].i_pitch;
515 i_src_stride = i_plane ? yuv->uv_stride : yuv->y_stride;
517 for( i_line = 0; i_line < p_pic->p[i_plane].i_lines; i_line++ )
519 vlc_memcpy( p_dst, p_src,
520 i_plane ? yuv->uv_width : yuv->y_width );
521 p_src += i_src_stride;
522 p_dst += i_dst_stride;
527 /*****************************************************************************
528 * encoder_sys_t : theora encoder descriptor
529 *****************************************************************************/
530 struct encoder_sys_t
533 * Input properties
535 bool b_headers;
538 * Theora properties
540 theora_info ti; /* theora bitstream settings */
541 theora_comment tc; /* theora comment header */
542 theora_state td; /* theora bitstream user comments */
544 int i_width, i_height;
547 /*****************************************************************************
548 * OpenEncoder: probe the encoder and return score
549 *****************************************************************************/
550 static int OpenEncoder( vlc_object_t *p_this )
552 encoder_t *p_enc = (encoder_t *)p_this;
553 encoder_sys_t *p_sys;
554 int i_quality;
556 if( p_enc->fmt_out.i_codec != VLC_CODEC_THEORA &&
557 !p_enc->b_force )
559 return VLC_EGENERIC;
562 /* Allocate the memory needed to store the decoder's structure */
563 if( ( p_sys = malloc(sizeof(encoder_sys_t)) ) == NULL )
564 return VLC_ENOMEM;
565 p_enc->p_sys = p_sys;
567 p_enc->pf_encode_video = Encode;
568 p_enc->fmt_in.i_codec = VLC_CODEC_I420;
569 p_enc->fmt_out.i_codec = VLC_CODEC_THEORA;
571 config_ChainParse( p_enc, ENC_CFG_PREFIX, ppsz_enc_options, p_enc->p_cfg );
573 i_quality = var_GetInteger( p_enc, ENC_CFG_PREFIX "quality" );
574 if( i_quality > 10 ) i_quality = 10;
575 if( i_quality < 0 ) i_quality = 0;
577 theora_info_init( &p_sys->ti );
579 p_sys->ti.width = p_enc->fmt_in.video.i_width;
580 p_sys->ti.height = p_enc->fmt_in.video.i_height;
582 if( p_sys->ti.width % 16 || p_sys->ti.height % 16 )
584 /* Pictures from the transcoder should always have a pitch
585 * which is a multiple of 16 */
586 p_sys->ti.width = (p_sys->ti.width + 15) >> 4 << 4;
587 p_sys->ti.height = (p_sys->ti.height + 15) >> 4 << 4;
589 msg_Dbg( p_enc, "padding video from %dx%d to %dx%d",
590 p_enc->fmt_in.video.i_width, p_enc->fmt_in.video.i_height,
591 p_sys->ti.width, p_sys->ti.height );
594 p_sys->ti.frame_width = p_enc->fmt_in.video.i_width;
595 p_sys->ti.frame_height = p_enc->fmt_in.video.i_height;
596 p_sys->ti.offset_x = 0 /*frame_x_offset*/;
597 p_sys->ti.offset_y = 0 /*frame_y_offset*/;
599 p_sys->i_width = p_sys->ti.width;
600 p_sys->i_height = p_sys->ti.height;
602 if( !p_enc->fmt_in.video.i_frame_rate ||
603 !p_enc->fmt_in.video.i_frame_rate_base )
605 p_sys->ti.fps_numerator = 25;
606 p_sys->ti.fps_denominator = 1;
608 else
610 p_sys->ti.fps_numerator = p_enc->fmt_in.video.i_frame_rate;
611 p_sys->ti.fps_denominator = p_enc->fmt_in.video.i_frame_rate_base;
614 if( p_enc->fmt_in.video.i_sar_num > 0 && p_enc->fmt_in.video.i_sar_den > 0 )
616 unsigned i_dst_num, i_dst_den;
617 vlc_ureduce( &i_dst_num, &i_dst_den,
618 p_enc->fmt_in.video.i_sar_num,
619 p_enc->fmt_in.video.i_sar_den, 0 );
620 p_sys->ti.aspect_numerator = i_dst_num;
621 p_sys->ti.aspect_denominator = i_dst_den;
623 else
625 p_sys->ti.aspect_numerator = 4;
626 p_sys->ti.aspect_denominator = 3;
629 p_sys->ti.target_bitrate = p_enc->fmt_out.i_bitrate;
630 p_sys->ti.quality = ((float)i_quality) * 6.3;
632 p_sys->ti.dropframes_p = 0;
633 p_sys->ti.quick_p = 1;
634 p_sys->ti.keyframe_auto_p = 1;
635 p_sys->ti.keyframe_frequency = 64;
636 p_sys->ti.keyframe_frequency_force = 64;
637 p_sys->ti.keyframe_data_target_bitrate = p_enc->fmt_out.i_bitrate * 1.5;
638 p_sys->ti.keyframe_auto_threshold = 80;
639 p_sys->ti.keyframe_mindistance = 8;
640 p_sys->ti.noise_sensitivity = 1;
642 theora_encode_init( &p_sys->td, &p_sys->ti );
643 theora_comment_init( &p_sys->tc );
645 /* Create and store headers */
646 for( int i = 0; i < 3; i++ )
648 ogg_packet header;
650 if( i == 0 )
651 theora_encode_header( &p_sys->td, &header );
652 else if( i == 1 )
653 theora_encode_comment( &p_sys->tc, &header );
654 else
655 theora_encode_tables( &p_sys->td, &header );
657 if( xiph_AppendHeaders( &p_enc->fmt_out.i_extra, &p_enc->fmt_out.p_extra,
658 header.bytes, header.packet ) )
660 p_enc->fmt_out.i_extra = 0;
661 p_enc->fmt_out.p_extra = NULL;
664 return VLC_SUCCESS;
667 /****************************************************************************
668 * Encode: the whole thing
669 ****************************************************************************
670 * This function spits out ogg packets.
671 ****************************************************************************/
672 static block_t *Encode( encoder_t *p_enc, picture_t *p_pict )
674 encoder_sys_t *p_sys = p_enc->p_sys;
675 ogg_packet oggpacket;
676 block_t *p_block;
677 yuv_buffer yuv;
678 int i;
680 if( !p_pict ) return NULL;
681 /* Sanity check */
682 if( p_pict->p[0].i_pitch < (int)p_sys->i_width ||
683 p_pict->p[0].i_lines < (int)p_sys->i_height )
685 msg_Warn( p_enc, "frame is smaller than encoding size"
686 "(%ix%i->%ix%i) -> dropping frame",
687 p_pict->p[0].i_pitch, p_pict->p[0].i_lines,
688 p_sys->i_width, p_sys->i_height );
689 return NULL;
692 /* Fill padding */
693 if( p_pict->p[0].i_visible_pitch < (int)p_sys->i_width )
695 for( i = 0; i < p_sys->i_height; i++ )
697 memset( p_pict->p[0].p_pixels + i * p_pict->p[0].i_pitch +
698 p_pict->p[0].i_visible_pitch,
699 *( p_pict->p[0].p_pixels + i * p_pict->p[0].i_pitch +
700 p_pict->p[0].i_visible_pitch - 1 ),
701 p_sys->i_width - p_pict->p[0].i_visible_pitch );
703 for( i = 0; i < p_sys->i_height / 2; i++ )
705 memset( p_pict->p[1].p_pixels + i * p_pict->p[1].i_pitch +
706 p_pict->p[1].i_visible_pitch,
707 *( p_pict->p[1].p_pixels + i * p_pict->p[1].i_pitch +
708 p_pict->p[1].i_visible_pitch - 1 ),
709 p_sys->i_width / 2 - p_pict->p[1].i_visible_pitch );
710 memset( p_pict->p[2].p_pixels + i * p_pict->p[2].i_pitch +
711 p_pict->p[2].i_visible_pitch,
712 *( p_pict->p[2].p_pixels + i * p_pict->p[2].i_pitch +
713 p_pict->p[2].i_visible_pitch - 1 ),
714 p_sys->i_width / 2 - p_pict->p[2].i_visible_pitch );
718 if( p_pict->p[0].i_visible_lines < (int)p_sys->i_height )
720 for( i = p_pict->p[0].i_visible_lines; i < p_sys->i_height; i++ )
722 memset( p_pict->p[0].p_pixels + i * p_pict->p[0].i_pitch, 0,
723 p_sys->i_width );
725 for( i = p_pict->p[1].i_visible_lines; i < p_sys->i_height / 2; i++ )
727 memset( p_pict->p[1].p_pixels + i * p_pict->p[1].i_pitch, 0x80,
728 p_sys->i_width / 2 );
729 memset( p_pict->p[2].p_pixels + i * p_pict->p[2].i_pitch, 0x80,
730 p_sys->i_width / 2 );
734 /* Theora is a one-frame-in, one-frame-out system. Submit a frame
735 * for compression and pull out the packet. */
737 yuv.y_width = p_sys->i_width;
738 yuv.y_height = p_sys->i_height;
739 yuv.y_stride = p_pict->p[0].i_pitch;
741 yuv.uv_width = p_sys->i_width / 2;
742 yuv.uv_height = p_sys->i_height / 2;
743 yuv.uv_stride = p_pict->p[1].i_pitch;
745 yuv.y = p_pict->p[0].p_pixels;
746 yuv.u = p_pict->p[1].p_pixels;
747 yuv.v = p_pict->p[2].p_pixels;
749 if( theora_encode_YUVin( &p_sys->td, &yuv ) < 0 )
751 msg_Warn( p_enc, "failed encoding a frame" );
752 return NULL;
755 theora_encode_packetout( &p_sys->td, 0, &oggpacket );
757 /* Ogg packet to block */
758 p_block = block_New( p_enc, oggpacket.bytes );
759 memcpy( p_block->p_buffer, oggpacket.packet, oggpacket.bytes );
760 p_block->i_dts = p_block->i_pts = p_pict->date;
762 if( theora_packet_iskeyframe( &oggpacket ) )
764 p_block->i_flags |= BLOCK_FLAG_TYPE_I;
767 return p_block;
770 /*****************************************************************************
771 * CloseEncoder: theora encoder destruction
772 *****************************************************************************/
773 static void CloseEncoder( vlc_object_t *p_this )
775 encoder_t *p_enc = (encoder_t *)p_this;
776 encoder_sys_t *p_sys = p_enc->p_sys;
778 theora_info_clear( &p_sys->ti );
779 theora_comment_clear( &p_sys->tc );
781 free( p_sys );