Old RC: "rate" is a float nowadays (fix #4088)
[vlc/asuraparaju-public.git] / modules / codec / theora.c
blob07fb81045bed8c67e8e4a9a12b1be95c3abce412
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, NULL, 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 switch( p_sys->ti.pixelformat )
255 case OC_PF_420:
256 p_dec->fmt_out.i_codec = VLC_CODEC_I420;
257 break;
258 case OC_PF_422:
259 p_dec->fmt_out.i_codec = VLC_CODEC_I422;
260 break;
261 case OC_PF_444:
262 p_dec->fmt_out.i_codec = VLC_CODEC_I444;
263 break;
264 case OC_PF_RSVD:
265 default:
266 msg_Err( p_dec, "unknown chroma in theora sample" );
267 break;
269 p_dec->fmt_out.video.i_width = p_sys->ti.width;
270 p_dec->fmt_out.video.i_height = p_sys->ti.height;
271 if( p_sys->ti.frame_width && p_sys->ti.frame_height )
273 p_dec->fmt_out.video.i_visible_width = p_sys->ti.frame_width;
274 p_dec->fmt_out.video.i_visible_height = p_sys->ti.frame_height;
275 if( p_sys->ti.offset_x || p_sys->ti.offset_y )
277 p_dec->fmt_out.video.i_x_offset = p_sys->ti.offset_x;
278 p_dec->fmt_out.video.i_y_offset = p_sys->ti.offset_y;
282 if( p_sys->ti.aspect_denominator && p_sys->ti.aspect_numerator )
284 p_dec->fmt_out.video.i_sar_num = p_sys->ti.aspect_numerator;
285 p_dec->fmt_out.video.i_sar_den = p_sys->ti.aspect_denominator;
287 else
289 p_dec->fmt_out.video.i_sar_num = 1;
290 p_dec->fmt_out.video.i_sar_den = 1;
293 if( p_sys->ti.fps_numerator > 0 && p_sys->ti.fps_denominator > 0 )
295 p_dec->fmt_out.video.i_frame_rate = p_sys->ti.fps_numerator;
296 p_dec->fmt_out.video.i_frame_rate_base = p_sys->ti.fps_denominator;
299 msg_Dbg( p_dec, "%dx%d %.02f fps video, frame content "
300 "is %dx%d with offset (%d,%d)",
301 p_sys->ti.width, p_sys->ti.height,
302 (double)p_sys->ti.fps_numerator/p_sys->ti.fps_denominator,
303 p_sys->ti.frame_width, p_sys->ti.frame_height,
304 p_sys->ti.offset_x, p_sys->ti.offset_y );
306 /* Sanity check that seems necessary for some corrupted files */
307 if( p_sys->ti.width < p_sys->ti.frame_width ||
308 p_sys->ti.height < p_sys->ti.frame_height )
310 msg_Warn( p_dec, "trying to correct invalid theora header "
311 "(frame size (%dx%d) is smaller than frame content (%d,%d))",
312 p_sys->ti.width, p_sys->ti.height,
313 p_sys->ti.frame_width, p_sys->ti.frame_height );
315 if( p_sys->ti.width < p_sys->ti.frame_width )
316 p_sys->ti.width = p_sys->ti.frame_width;
317 if( p_sys->ti.height < p_sys->ti.frame_height )
318 p_sys->ti.height = p_sys->ti.frame_height;
321 /* The next packet in order is the comments header */
322 oggpacket.b_o_s = 0;
323 oggpacket.bytes = pi_size[1];
324 oggpacket.packet = pp_data[1];
325 if( theora_decode_header( &p_sys->ti, &p_sys->tc, &oggpacket ) < 0 )
327 msg_Err( p_dec, "2nd Theora header is corrupted" );
328 goto error;
331 ParseTheoraComments( p_dec );
333 /* The next packet in order is the codebooks header
334 * We need to watch out that this packet is not missing as a
335 * missing or corrupted header is fatal. */
336 oggpacket.b_o_s = 0;
337 oggpacket.bytes = pi_size[2];
338 oggpacket.packet = pp_data[2];
339 if( theora_decode_header( &p_sys->ti, &p_sys->tc, &oggpacket ) < 0 )
341 msg_Err( p_dec, "3rd Theora header is corrupted" );
342 goto error;
345 if( !p_sys->b_packetizer )
347 /* We have all the headers, initialize decoder */
348 theora_decode_init( &p_sys->td, &p_sys->ti );
350 else
352 p_dec->fmt_out.i_extra = p_dec->fmt_in.i_extra;
353 p_dec->fmt_out.p_extra = xrealloc( p_dec->fmt_out.p_extra,
354 p_dec->fmt_out.i_extra );
355 memcpy( p_dec->fmt_out.p_extra,
356 p_dec->fmt_in.p_extra, p_dec->fmt_out.i_extra );
359 for( unsigned i = 0; i < i_count; i++ )
360 free( pp_data[i] );
361 return VLC_SUCCESS;
363 error:
364 for( unsigned i = 0; i < i_count; i++ )
365 free( pp_data[i] );
366 return VLC_EGENERIC;
369 /*****************************************************************************
370 * ProcessPacket: processes a theora packet.
371 *****************************************************************************/
372 static void *ProcessPacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
373 block_t **pp_block )
375 decoder_sys_t *p_sys = p_dec->p_sys;
376 block_t *p_block = *pp_block;
377 void *p_buf;
379 if( ( p_block->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) ) != 0 )
381 /* Don't send the the first packet after a discontinuity to
382 * theora_decode, otherwise we get purple/green display artifacts
383 * appearing in the video output */
384 return NULL;
387 /* Date management */
388 if( p_block->i_pts > VLC_TS_INVALID && p_block->i_pts != p_sys->i_pts )
390 p_sys->i_pts = p_block->i_pts;
393 *pp_block = NULL; /* To avoid being fed the same packet again */
395 if( p_sys->b_packetizer )
397 /* Date management */
398 p_block->i_dts = p_block->i_pts = p_sys->i_pts;
400 p_block->i_length = p_sys->i_pts - p_block->i_pts;
402 p_buf = p_block;
404 else
406 p_buf = DecodePacket( p_dec, p_oggpacket );
407 if( p_block )
408 block_Release( p_block );
411 /* Date management */
412 p_sys->i_pts += ( INT64_C(1000000) * p_sys->ti.fps_denominator /
413 p_sys->ti.fps_numerator ); /* 1 frame per packet */
415 return p_buf;
418 /*****************************************************************************
419 * DecodePacket: decodes a Theora packet.
420 *****************************************************************************/
421 static picture_t *DecodePacket( decoder_t *p_dec, ogg_packet *p_oggpacket )
423 decoder_sys_t *p_sys = p_dec->p_sys;
424 picture_t *p_pic;
425 yuv_buffer yuv;
427 theora_decode_packetin( &p_sys->td, p_oggpacket );
429 /* Check for keyframe */
430 if( !(p_oggpacket->packet[0] & 0x80) /* data packet */ &&
431 !(p_oggpacket->packet[0] & 0x40) /* intra frame */ )
432 p_sys->b_decoded_first_keyframe = true;
434 /* If we haven't seen a single keyframe yet, don't let Theora decode
435 * anything, otherwise we'll get display artifacts. (This is impossible
436 * in the general case, but can happen if e.g. we play a network stream
437 * using a timed URL, such that the server doesn't start the video with a
438 * keyframe). */
439 if( p_sys->b_decoded_first_keyframe )
440 theora_decode_YUVout( &p_sys->td, &yuv );
441 else
442 return NULL;
444 /* Get a new picture */
445 p_pic = decoder_NewPicture( p_dec );
446 if( !p_pic ) return NULL;
448 theora_CopyPicture( p_pic, &yuv );
450 p_pic->date = p_sys->i_pts;
452 return p_pic;
455 /*****************************************************************************
456 * ParseTheoraComments:
457 *****************************************************************************/
458 static void ParseTheoraComments( decoder_t *p_dec )
460 char *psz_name, *psz_value, *psz_comment;
461 int i = 0;
463 while ( i < p_dec->p_sys->tc.comments )
465 psz_comment = strdup( p_dec->p_sys->tc.user_comments[i] );
466 if( !psz_comment )
467 break;
468 psz_name = psz_comment;
469 psz_value = strchr( psz_comment, '=' );
470 if( psz_value )
472 *psz_value = '\0';
473 psz_value++;
475 if( !p_dec->p_description )
476 p_dec->p_description = vlc_meta_New();
477 if( p_dec->p_description )
478 vlc_meta_AddExtra( p_dec->p_description, psz_name, psz_value );
480 free( psz_comment );
481 i++;
485 /*****************************************************************************
486 * CloseDecoder: theora decoder destruction
487 *****************************************************************************/
488 static void CloseDecoder( vlc_object_t *p_this )
490 decoder_t *p_dec = (decoder_t *)p_this;
491 decoder_sys_t *p_sys = p_dec->p_sys;
493 theora_info_clear( &p_sys->ti );
494 theora_comment_clear( &p_sys->tc );
496 free( p_sys );
499 /*****************************************************************************
500 * theora_CopyPicture: copy a picture from theora internal buffers to a
501 * picture_t structure.
502 *****************************************************************************/
503 static void theora_CopyPicture( picture_t *p_pic,
504 yuv_buffer *yuv )
506 int i_plane, i_line, i_dst_stride, i_src_stride;
507 uint8_t *p_dst, *p_src;
509 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
511 p_dst = p_pic->p[i_plane].p_pixels;
512 p_src = i_plane ? (i_plane - 1 ? yuv->v : yuv->u ) : yuv->y;
513 i_dst_stride = p_pic->p[i_plane].i_pitch;
514 i_src_stride = i_plane ? yuv->uv_stride : yuv->y_stride;
516 for( i_line = 0; i_line < p_pic->p[i_plane].i_lines; i_line++ )
518 vlc_memcpy( p_dst, p_src,
519 i_plane ? yuv->uv_width : yuv->y_width );
520 p_src += i_src_stride;
521 p_dst += i_dst_stride;
526 /*****************************************************************************
527 * encoder_sys_t : theora encoder descriptor
528 *****************************************************************************/
529 struct encoder_sys_t
532 * Input properties
534 bool b_headers;
537 * Theora properties
539 theora_info ti; /* theora bitstream settings */
540 theora_comment tc; /* theora comment header */
541 theora_state td; /* theora bitstream user comments */
543 int i_width, i_height;
546 /*****************************************************************************
547 * OpenEncoder: probe the encoder and return score
548 *****************************************************************************/
549 static int OpenEncoder( vlc_object_t *p_this )
551 encoder_t *p_enc = (encoder_t *)p_this;
552 encoder_sys_t *p_sys;
553 int i_quality;
555 if( p_enc->fmt_out.i_codec != VLC_CODEC_THEORA &&
556 !p_enc->b_force )
558 return VLC_EGENERIC;
561 /* Allocate the memory needed to store the decoder's structure */
562 if( ( p_sys = malloc(sizeof(encoder_sys_t)) ) == NULL )
563 return VLC_ENOMEM;
564 p_enc->p_sys = p_sys;
566 p_enc->pf_encode_video = Encode;
567 p_enc->fmt_in.i_codec = VLC_CODEC_I420;
568 p_enc->fmt_out.i_codec = VLC_CODEC_THEORA;
570 config_ChainParse( p_enc, ENC_CFG_PREFIX, ppsz_enc_options, p_enc->p_cfg );
572 i_quality = var_GetInteger( p_enc, ENC_CFG_PREFIX "quality" );
573 if( i_quality > 10 ) i_quality = 10;
574 if( i_quality < 0 ) i_quality = 0;
576 theora_info_init( &p_sys->ti );
578 p_sys->ti.width = p_enc->fmt_in.video.i_width;
579 p_sys->ti.height = p_enc->fmt_in.video.i_height;
581 if( p_sys->ti.width % 16 || p_sys->ti.height % 16 )
583 /* Pictures from the transcoder should always have a pitch
584 * which is a multiple of 16 */
585 p_sys->ti.width = (p_sys->ti.width + 15) >> 4 << 4;
586 p_sys->ti.height = (p_sys->ti.height + 15) >> 4 << 4;
588 msg_Dbg( p_enc, "padding video from %dx%d to %dx%d",
589 p_enc->fmt_in.video.i_width, p_enc->fmt_in.video.i_height,
590 p_sys->ti.width, p_sys->ti.height );
593 p_sys->ti.frame_width = p_enc->fmt_in.video.i_width;
594 p_sys->ti.frame_height = p_enc->fmt_in.video.i_height;
595 p_sys->ti.offset_x = 0 /*frame_x_offset*/;
596 p_sys->ti.offset_y = 0 /*frame_y_offset*/;
598 p_sys->i_width = p_sys->ti.width;
599 p_sys->i_height = p_sys->ti.height;
601 if( !p_enc->fmt_in.video.i_frame_rate ||
602 !p_enc->fmt_in.video.i_frame_rate_base )
604 p_sys->ti.fps_numerator = 25;
605 p_sys->ti.fps_denominator = 1;
607 else
609 p_sys->ti.fps_numerator = p_enc->fmt_in.video.i_frame_rate;
610 p_sys->ti.fps_denominator = p_enc->fmt_in.video.i_frame_rate_base;
613 if( p_enc->fmt_in.video.i_sar_num > 0 && p_enc->fmt_in.video.i_sar_den > 0 )
615 unsigned i_dst_num, i_dst_den;
616 vlc_ureduce( &i_dst_num, &i_dst_den,
617 p_enc->fmt_in.video.i_sar_num,
618 p_enc->fmt_in.video.i_sar_den, 0 );
619 p_sys->ti.aspect_numerator = i_dst_num;
620 p_sys->ti.aspect_denominator = i_dst_den;
622 else
624 p_sys->ti.aspect_numerator = 4;
625 p_sys->ti.aspect_denominator = 3;
628 p_sys->ti.target_bitrate = p_enc->fmt_out.i_bitrate;
629 p_sys->ti.quality = ((float)i_quality) * 6.3;
631 p_sys->ti.dropframes_p = 0;
632 p_sys->ti.quick_p = 1;
633 p_sys->ti.keyframe_auto_p = 1;
634 p_sys->ti.keyframe_frequency = 64;
635 p_sys->ti.keyframe_frequency_force = 64;
636 p_sys->ti.keyframe_data_target_bitrate = p_enc->fmt_out.i_bitrate * 1.5;
637 p_sys->ti.keyframe_auto_threshold = 80;
638 p_sys->ti.keyframe_mindistance = 8;
639 p_sys->ti.noise_sensitivity = 1;
641 theora_encode_init( &p_sys->td, &p_sys->ti );
642 theora_comment_init( &p_sys->tc );
644 /* Create and store headers */
645 for( int i = 0; i < 3; i++ )
647 ogg_packet header;
649 if( i == 0 )
650 theora_encode_header( &p_sys->td, &header );
651 else if( i == 1 )
652 theora_encode_comment( &p_sys->tc, &header );
653 else
654 theora_encode_tables( &p_sys->td, &header );
656 if( xiph_AppendHeaders( &p_enc->fmt_out.i_extra, &p_enc->fmt_out.p_extra,
657 header.bytes, header.packet ) )
659 p_enc->fmt_out.i_extra = 0;
660 p_enc->fmt_out.p_extra = NULL;
663 return VLC_SUCCESS;
666 /****************************************************************************
667 * Encode: the whole thing
668 ****************************************************************************
669 * This function spits out ogg packets.
670 ****************************************************************************/
671 static block_t *Encode( encoder_t *p_enc, picture_t *p_pict )
673 encoder_sys_t *p_sys = p_enc->p_sys;
674 ogg_packet oggpacket;
675 block_t *p_block;
676 yuv_buffer yuv;
677 int i;
679 if( !p_pict ) return NULL;
680 /* Sanity check */
681 if( p_pict->p[0].i_pitch < (int)p_sys->i_width ||
682 p_pict->p[0].i_lines < (int)p_sys->i_height )
684 msg_Warn( p_enc, "frame is smaller than encoding size"
685 "(%ix%i->%ix%i) -> dropping frame",
686 p_pict->p[0].i_pitch, p_pict->p[0].i_lines,
687 p_sys->i_width, p_sys->i_height );
688 return NULL;
691 /* Fill padding */
692 if( p_pict->p[0].i_visible_pitch < (int)p_sys->i_width )
694 for( i = 0; i < p_sys->i_height; i++ )
696 memset( p_pict->p[0].p_pixels + i * p_pict->p[0].i_pitch +
697 p_pict->p[0].i_visible_pitch,
698 *( p_pict->p[0].p_pixels + i * p_pict->p[0].i_pitch +
699 p_pict->p[0].i_visible_pitch - 1 ),
700 p_sys->i_width - p_pict->p[0].i_visible_pitch );
702 for( i = 0; i < p_sys->i_height / 2; i++ )
704 memset( p_pict->p[1].p_pixels + i * p_pict->p[1].i_pitch +
705 p_pict->p[1].i_visible_pitch,
706 *( p_pict->p[1].p_pixels + i * p_pict->p[1].i_pitch +
707 p_pict->p[1].i_visible_pitch - 1 ),
708 p_sys->i_width / 2 - p_pict->p[1].i_visible_pitch );
709 memset( p_pict->p[2].p_pixels + i * p_pict->p[2].i_pitch +
710 p_pict->p[2].i_visible_pitch,
711 *( p_pict->p[2].p_pixels + i * p_pict->p[2].i_pitch +
712 p_pict->p[2].i_visible_pitch - 1 ),
713 p_sys->i_width / 2 - p_pict->p[2].i_visible_pitch );
717 if( p_pict->p[0].i_visible_lines < (int)p_sys->i_height )
719 for( i = p_pict->p[0].i_visible_lines; i < p_sys->i_height; i++ )
721 memset( p_pict->p[0].p_pixels + i * p_pict->p[0].i_pitch, 0,
722 p_sys->i_width );
724 for( i = p_pict->p[1].i_visible_lines; i < p_sys->i_height / 2; i++ )
726 memset( p_pict->p[1].p_pixels + i * p_pict->p[1].i_pitch, 0x80,
727 p_sys->i_width / 2 );
728 memset( p_pict->p[2].p_pixels + i * p_pict->p[2].i_pitch, 0x80,
729 p_sys->i_width / 2 );
733 /* Theora is a one-frame-in, one-frame-out system. Submit a frame
734 * for compression and pull out the packet. */
736 yuv.y_width = p_sys->i_width;
737 yuv.y_height = p_sys->i_height;
738 yuv.y_stride = p_pict->p[0].i_pitch;
740 yuv.uv_width = p_sys->i_width / 2;
741 yuv.uv_height = p_sys->i_height / 2;
742 yuv.uv_stride = p_pict->p[1].i_pitch;
744 yuv.y = p_pict->p[0].p_pixels;
745 yuv.u = p_pict->p[1].p_pixels;
746 yuv.v = p_pict->p[2].p_pixels;
748 if( theora_encode_YUVin( &p_sys->td, &yuv ) < 0 )
750 msg_Warn( p_enc, "failed encoding a frame" );
751 return NULL;
754 theora_encode_packetout( &p_sys->td, 0, &oggpacket );
756 /* Ogg packet to block */
757 p_block = block_New( p_enc, oggpacket.bytes );
758 memcpy( p_block->p_buffer, oggpacket.packet, oggpacket.bytes );
759 p_block->i_dts = p_block->i_pts = p_pict->date;
761 if( theora_packet_iskeyframe( &oggpacket ) )
763 p_block->i_flags |= BLOCK_FLAG_TYPE_I;
766 return p_block;
769 /*****************************************************************************
770 * CloseEncoder: theora encoder destruction
771 *****************************************************************************/
772 static void CloseEncoder( vlc_object_t *p_this )
774 encoder_t *p_enc = (encoder_t *)p_this;
775 encoder_sys_t *p_sys = p_enc->p_sys;
777 theora_info_clear( &p_sys->ti );
778 theora_comment_clear( &p_sys->tc );
780 free( p_sys );