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 *****************************************************************************/
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_codec.h>
35 #include <vlc_input.h>
36 #include "../demux/xiph.h"
40 #include <theora/theora.h>
42 /*****************************************************************************
43 * decoder_sys_t : theora decoder descriptor
44 *****************************************************************************/
58 theora_info ti
; /* theora bitstream settings */
59 theora_comment tc
; /* theora comment header */
60 theora_state td
; /* theora bitstream user comments */
65 bool b_decoded_first_keyframe
;
73 /*****************************************************************************
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 /*****************************************************************************
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." )
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" )
111 set_description( N_("Theora video packetizer") )
112 set_capability( "packetizer", 100 )
113 set_callbacks( OpenPacketizer
, CloseDecoder
)
114 add_shortcut( "theora" )
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 )
127 static const char *const ppsz_enc_options
[] = {
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
)
144 /* Allocate the memory needed to store the decoder's structure */
145 if( ( p_dec
->p_sys
= p_sys
= malloc(sizeof(*p_sys
)) ) == NULL
)
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
;
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
);
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
;
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
;
193 ogg_packet oggpacket
;
195 if( !pp_block
|| !*pp_block
) return NULL
;
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
;
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
);
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
];
232 if( xiph_SplitHeaders( pi_size
, pp_data
, &i_count
,
233 p_dec
->fmt_in
.i_extra
, p_dec
->fmt_in
.p_extra
) )
238 oggpacket
.granulepos
= -1;
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" );
252 /* Set output properties */
253 switch( p_sys
->ti
.pixelformat
)
256 p_dec
->fmt_out
.i_codec
= VLC_CODEC_I420
;
259 p_dec
->fmt_out
.i_codec
= VLC_CODEC_I422
;
262 p_dec
->fmt_out
.i_codec
= VLC_CODEC_I444
;
266 msg_Err( p_dec
, "unknown chroma in theora sample" );
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
;
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 */
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" );
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. */
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" );
345 if( !p_sys
->b_packetizer
)
347 /* We have all the headers, initialize decoder */
348 theora_decode_init( &p_sys
->td
, &p_sys
->ti
);
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
++ )
364 for( unsigned i
= 0; i
< i_count
; i
++ )
369 /*****************************************************************************
370 * ProcessPacket: processes a theora packet.
371 *****************************************************************************/
372 static void *ProcessPacket( decoder_t
*p_dec
, ogg_packet
*p_oggpacket
,
375 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
376 block_t
*p_block
= *pp_block
;
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 */
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
;
406 p_buf
= DecodePacket( p_dec
, p_oggpacket
);
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 */
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
;
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
439 if( p_sys
->b_decoded_first_keyframe
)
440 theora_decode_YUVout( &p_sys
->td
, &yuv
);
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
;
455 /*****************************************************************************
456 * ParseTheoraComments:
457 *****************************************************************************/
458 static void ParseTheoraComments( decoder_t
*p_dec
)
460 char *psz_name
, *psz_value
, *psz_comment
;
463 while ( i
< p_dec
->p_sys
->tc
.comments
)
465 psz_comment
= strdup( p_dec
->p_sys
->tc
.user_comments
[i
] );
468 psz_name
= psz_comment
;
469 psz_value
= strchr( psz_comment
, '=' );
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
);
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
);
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
,
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 *****************************************************************************/
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
;
555 if( p_enc
->fmt_out
.i_codec
!= VLC_CODEC_THEORA
&&
561 /* Allocate the memory needed to store the decoder's structure */
562 if( ( p_sys
= malloc(sizeof(encoder_sys_t
)) ) == NULL
)
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;
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
;
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
++ )
650 theora_encode_header( &p_sys
->td
, &header
);
652 theora_encode_comment( &p_sys
->tc
, &header
);
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
;
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
;
679 if( !p_pict
) return NULL
;
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
);
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,
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" );
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
;
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
);