input: add an input_item_t arg to input_CreateFilename()
[vlc.git] / modules / codec / theora.c
blobda2d38fe3eaa5bcba6f2b95ae7163ef4563e2807
1 /*****************************************************************************
2 * theora.c: theora decoder module making use of libtheora.
3 *****************************************************************************
4 * Copyright (C) 1999-2012 VLC authors and VideoLAN
5 * $Id$
7 * Authors: Gildas Bazin <gbazin@videolan.org>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * 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/codec.h>
41 #include <theora/theoradec.h>
42 #include <theora/theoraenc.h>
44 #include <assert.h>
45 #include <limits.h>
47 /*****************************************************************************
48 * decoder_sys_t : theora decoder descriptor
49 *****************************************************************************/
50 typedef struct
52 /* Module mode */
53 bool b_packetizer;
56 * Input properties
58 bool b_has_headers;
61 * Theora properties
63 th_info ti; /* theora bitstream settings */
64 th_comment tc; /* theora comment information */
65 th_dec_ctx *tcx; /* theora decoder context */
68 * Decoding properties
70 bool b_decoded_first_keyframe;
73 * Common properties
75 vlc_tick_t i_pts;
76 } decoder_sys_t;
78 /*****************************************************************************
79 * Local prototypes
80 *****************************************************************************/
81 static int OpenDecoder ( vlc_object_t * );
82 static int OpenPacketizer( vlc_object_t * );
83 static void CloseDecoder ( vlc_object_t * );
85 static int DecodeVideo ( decoder_t *, block_t * );
86 static block_t *Packetize ( decoder_t *, block_t ** );
87 static int ProcessHeaders( decoder_t * );
88 static void *ProcessPacket ( decoder_t *, ogg_packet *, block_t * );
89 static void Flush( decoder_t * );
91 static picture_t *DecodePacket( decoder_t *, ogg_packet * );
93 static void ParseTheoraComments( decoder_t * );
94 static void theora_CopyPicture( picture_t *, th_ycbcr_buffer );
96 #ifdef ENABLE_SOUT
97 static int OpenEncoder( vlc_object_t *p_this );
98 static void CloseEncoder( vlc_object_t *p_this );
99 static block_t *Encode( encoder_t *p_enc, picture_t *p_pict );
100 #endif
102 /*****************************************************************************
103 * Module descriptor
104 *****************************************************************************/
105 #define ENC_QUALITY_TEXT N_("Encoding quality")
106 #define ENC_QUALITY_LONGTEXT N_( \
107 "Enforce a quality between 1 (low) and 10 (high), instead " \
108 "of specifying a particular bitrate. This will produce a VBR stream." )
110 #define ENC_POSTPROCESS_TEXT N_("Post processing quality")
112 vlc_module_begin ()
113 set_category( CAT_INPUT )
114 set_subcategory( SUBCAT_INPUT_VCODEC )
115 set_shortname( "Theora" )
116 set_description( N_("Theora video decoder") )
117 set_capability( "video decoder", 100 )
118 set_callbacks( OpenDecoder, CloseDecoder )
119 add_shortcut( "theora" )
120 # define DEC_CFG_PREFIX "theora-"
121 add_integer( DEC_CFG_PREFIX "postproc", -1, ENC_POSTPROCESS_TEXT, NULL, true )
123 add_submodule ()
124 set_description( N_("Theora video packetizer") )
125 set_capability( "packetizer", 100 )
126 set_callbacks( OpenPacketizer, CloseDecoder )
127 add_shortcut( "theora" )
129 #ifdef ENABLE_SOUT
130 add_submodule ()
131 set_description( N_("Theora video encoder") )
132 set_capability( "encoder", 150 )
133 set_callbacks( OpenEncoder, CloseEncoder )
134 add_shortcut( "theora" )
136 # define ENC_CFG_PREFIX "sout-theora-"
137 add_integer( ENC_CFG_PREFIX "quality", 2, ENC_QUALITY_TEXT,
138 ENC_QUALITY_LONGTEXT, false )
139 #endif
140 vlc_module_end ()
142 static const char *const ppsz_enc_options[] = {
143 "quality", NULL
146 static int OpenCommon( vlc_object_t *p_this, bool b_packetizer )
148 decoder_t *p_dec = (decoder_t*)p_this;
149 decoder_sys_t *p_sys;
151 if( p_dec->fmt_in.i_codec != VLC_CODEC_THEORA )
153 return VLC_EGENERIC;
156 /* Allocate the memory needed to store the decoder's structure */
157 if( ( p_dec->p_sys = p_sys = malloc(sizeof(*p_sys)) ) == NULL )
158 return VLC_ENOMEM;
159 p_sys->b_packetizer = b_packetizer;
160 p_sys->b_has_headers = false;
161 p_sys->i_pts = VLC_TICK_INVALID;
162 p_sys->b_decoded_first_keyframe = false;
163 p_sys->tcx = NULL;
165 if( b_packetizer )
167 p_dec->fmt_out.i_codec = VLC_CODEC_THEORA;
168 p_dec->pf_packetize = Packetize;
170 else
172 p_dec->fmt_out.i_codec = VLC_CODEC_I420;
173 p_dec->pf_decode = DecodeVideo;
175 p_dec->pf_flush = Flush;
177 /* Init supporting Theora structures needed in header parsing */
178 th_comment_init( &p_sys->tc );
179 th_info_init( &p_sys->ti );
181 return VLC_SUCCESS;
184 /*****************************************************************************
185 * OpenDecoder: probe the decoder and return score
186 *****************************************************************************/
187 static int OpenDecoder( vlc_object_t *p_this )
189 return OpenCommon( p_this, false );
192 static int OpenPacketizer( vlc_object_t *p_this )
194 return OpenCommon( p_this, true );
197 /****************************************************************************
198 * DecodeBlock: the whole thing
199 ****************************************************************************
200 * This function must be fed with ogg packets.
201 ****************************************************************************/
202 static void *DecodeBlock( decoder_t *p_dec, block_t *p_block )
204 decoder_sys_t *p_sys = p_dec->p_sys;
205 ogg_packet oggpacket;
207 /* Block to Ogg packet */
208 oggpacket.packet = p_block->p_buffer;
209 oggpacket.bytes = p_block->i_buffer;
210 oggpacket.granulepos = p_block->i_dts;
211 oggpacket.b_o_s = 0;
212 oggpacket.e_o_s = 0;
213 oggpacket.packetno = 0;
215 /* Check for headers */
216 if( !p_sys->b_has_headers )
218 if( ProcessHeaders( p_dec ) )
220 block_Release( p_block );
221 return NULL;
223 p_sys->b_has_headers = true;
226 return ProcessPacket( p_dec, &oggpacket, p_block );
229 static int DecodeVideo( decoder_t *p_dec, block_t *p_block )
231 if( p_block == NULL ) /* No Drain */
232 return VLCDEC_SUCCESS;
234 picture_t *p_pic = DecodeBlock( p_dec, p_block );
235 if( p_pic != NULL )
236 decoder_QueueVideo( p_dec, p_pic );
237 return VLCDEC_SUCCESS;
240 static block_t *Packetize( decoder_t *p_dec, block_t **pp_block )
242 if( pp_block == NULL ) /* No Drain */
243 return NULL;
244 block_t *p_block = *pp_block; *pp_block = NULL;
245 if( p_block == NULL )
246 return NULL;
247 return DecodeBlock( p_dec, p_block );
250 /*****************************************************************************
251 * ProcessHeaders: process Theora headers.
252 *****************************************************************************/
253 static int ProcessHeaders( decoder_t *p_dec )
255 decoder_sys_t *p_sys = p_dec->p_sys;
256 ogg_packet oggpacket;
257 th_setup_info *ts = NULL; /* theora setup information */
258 int i_max_pp, i_pp;
260 unsigned pi_size[XIPH_MAX_HEADER_COUNT];
261 void *pp_data[XIPH_MAX_HEADER_COUNT];
262 unsigned i_count;
263 if( xiph_SplitHeaders( pi_size, pp_data, &i_count,
264 p_dec->fmt_in.i_extra, p_dec->fmt_in.p_extra) )
265 return VLC_EGENERIC;
266 if( i_count < 3 )
267 return VLC_EGENERIC;
269 oggpacket.granulepos = -1;
270 oggpacket.e_o_s = 0;
271 oggpacket.packetno = 0;
273 /* Take care of the initial Vorbis header */
274 oggpacket.b_o_s = 1; /* yes this actually is a b_o_s packet :) */
275 oggpacket.bytes = pi_size[0];
276 oggpacket.packet = pp_data[0];
277 if( th_decode_headerin( &p_sys->ti, &p_sys->tc, &ts, &oggpacket ) < 0 )
279 msg_Err( p_dec, "this bitstream does not contain Theora video data" );
280 goto error;
283 /* Set output properties */
284 if( !p_sys->b_packetizer )
286 switch( p_sys->ti.pixel_fmt )
288 case TH_PF_420:
289 p_dec->fmt_out.i_codec = VLC_CODEC_I420;
290 break;
291 case TH_PF_422:
292 p_dec->fmt_out.i_codec = VLC_CODEC_I422;
293 break;
294 case TH_PF_444:
295 p_dec->fmt_out.i_codec = VLC_CODEC_I444;
296 break;
297 case TH_PF_RSVD:
298 default:
299 msg_Err( p_dec, "unknown chroma in theora sample" );
300 break;
303 p_dec->fmt_out.video.i_width = p_sys->ti.frame_width;
304 p_dec->fmt_out.video.i_height = p_sys->ti.frame_height;
305 if( p_sys->ti.pic_width && p_sys->ti.pic_height )
307 p_dec->fmt_out.video.i_visible_width = p_sys->ti.pic_width;
308 p_dec->fmt_out.video.i_visible_height = p_sys->ti.pic_height;
310 p_dec->fmt_out.video.i_x_offset = p_sys->ti.pic_x;
311 p_dec->fmt_out.video.i_y_offset = p_sys->ti.pic_y;
314 if( p_sys->ti.aspect_denominator && p_sys->ti.aspect_numerator )
316 p_dec->fmt_out.video.i_sar_num = p_sys->ti.aspect_numerator;
317 p_dec->fmt_out.video.i_sar_den = p_sys->ti.aspect_denominator;
319 else
321 p_dec->fmt_out.video.i_sar_num = 1;
322 p_dec->fmt_out.video.i_sar_den = 1;
325 if( p_sys->ti.fps_numerator > 0 && p_sys->ti.fps_denominator > 0 )
327 p_dec->fmt_out.video.i_frame_rate = p_sys->ti.fps_numerator;
328 p_dec->fmt_out.video.i_frame_rate_base = p_sys->ti.fps_denominator;
331 msg_Dbg( p_dec, "%dx%d %u/%u fps video, frame content "
332 "is %dx%d with offset (%d,%d)",
333 p_sys->ti.frame_width, p_sys->ti.frame_height,
334 p_sys->ti.fps_numerator, p_sys->ti.fps_denominator,
335 p_sys->ti.pic_width, p_sys->ti.pic_height,
336 p_sys->ti.pic_x, p_sys->ti.pic_y );
338 /* Some assertions based on the documentation. These are mandatory restrictions. */
339 assert( p_sys->ti.frame_height % 16 == 0 && p_sys->ti.frame_height < 1048576 );
340 assert( p_sys->ti.frame_width % 16 == 0 && p_sys->ti.frame_width < 1048576 );
341 assert( p_sys->ti.keyframe_granule_shift >= 0 && p_sys->ti.keyframe_granule_shift <= 31 );
342 assert( p_sys->ti.pic_x <= __MIN( p_sys->ti.frame_width - p_sys->ti.pic_width, 255 ) );
343 assert( p_sys->ti.pic_y <= p_sys->ti.frame_height - p_sys->ti.pic_height);
344 assert( p_sys->ti.frame_height - p_sys->ti.pic_height - p_sys->ti.pic_y <= 255 );
346 /* Sanity check that seems necessary for some corrupted files */
347 if( p_sys->ti.frame_width < p_sys->ti.pic_width ||
348 p_sys->ti.frame_height < p_sys->ti.pic_height )
350 msg_Warn( p_dec, "trying to correct invalid theora header "
351 "(frame size (%dx%d) is smaller than frame content (%d,%d))",
352 p_sys->ti.frame_width, p_sys->ti.frame_height,
353 p_sys->ti.pic_width, p_sys->ti.pic_height );
355 if( p_sys->ti.frame_width < p_sys->ti.pic_width )
356 p_sys->ti.frame_width = p_sys->ti.pic_width;
357 if( p_sys->ti.frame_height < p_sys->ti.pic_height )
358 p_sys->ti.frame_height = p_sys->ti.pic_height;
361 /* The next packet in order is the comments header */
362 oggpacket.b_o_s = 0;
363 oggpacket.bytes = pi_size[1];
364 oggpacket.packet = pp_data[1];
366 if( th_decode_headerin( &p_sys->ti, &p_sys->tc, &ts, &oggpacket ) < 0 )
368 msg_Err( p_dec, "2nd Theora header is corrupted" );
369 goto error;
372 ParseTheoraComments( p_dec );
374 /* The next packet in order is the codebooks header
375 * We need to watch out that this packet is not missing as a
376 * missing or corrupted header is fatal. */
377 oggpacket.b_o_s = 0;
378 oggpacket.bytes = pi_size[2];
379 oggpacket.packet = pp_data[2];
380 if( th_decode_headerin( &p_sys->ti, &p_sys->tc, &ts, &oggpacket ) < 0 )
382 msg_Err( p_dec, "3rd Theora header is corrupted" );
383 goto error;
386 if( !p_sys->b_packetizer )
388 /* We have all the headers, initialize decoder */
389 if ( ( p_sys->tcx = th_decode_alloc( &p_sys->ti, ts ) ) == NULL )
391 msg_Err( p_dec, "Could not allocate Theora decoder" );
392 goto error;
395 i_pp = var_InheritInteger( p_dec, DEC_CFG_PREFIX "postproc" );
396 if ( i_pp >= 0 && !th_decode_ctl( p_sys->tcx,
397 TH_DECCTL_GET_PPLEVEL_MAX, &i_max_pp, sizeof(int) ) )
399 i_pp = __MIN( i_pp, i_max_pp );
400 if ( th_decode_ctl( p_sys->tcx, TH_DECCTL_SET_PPLEVEL,
401 &i_pp, sizeof(int) ) )
402 msg_Err( p_dec, "Failed to set post processing level to %d",
403 i_pp );
404 else
405 msg_Dbg( p_dec, "Set post processing level to %d / %d",
406 i_pp, i_max_pp );
410 else
412 void* p_extra = realloc( p_dec->fmt_out.p_extra,
413 p_dec->fmt_in.i_extra );
414 if( unlikely( p_extra == NULL ) )
416 /* Clean up the decoder setup info... we're done with it */
417 th_setup_free( ts );
418 return VLC_ENOMEM;
420 p_dec->fmt_out.p_extra = p_extra;
421 p_dec->fmt_out.i_extra = p_dec->fmt_in.i_extra;
422 memcpy( p_dec->fmt_out.p_extra,
423 p_dec->fmt_in.p_extra, p_dec->fmt_out.i_extra );
426 /* Clean up the decoder setup info... we're done with it */
427 th_setup_free( ts );
428 return VLC_SUCCESS;
430 error:
431 /* Clean up the decoder setup info... we're done with it */
432 th_setup_free( ts );
433 return VLC_EGENERIC;
436 /*****************************************************************************
437 * Flush:
438 *****************************************************************************/
439 static void Flush( decoder_t *p_dec )
441 decoder_sys_t *p_sys = p_dec->p_sys;
443 p_sys->i_pts = VLC_TICK_INVALID;
446 /*****************************************************************************
447 * ProcessPacket: processes a theora packet.
448 *****************************************************************************/
449 static void *ProcessPacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
450 block_t *p_block )
452 decoder_sys_t *p_sys = p_dec->p_sys;
453 void *p_buf;
455 if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
457 Flush( p_dec );
458 if( p_block->i_flags & BLOCK_FLAG_CORRUPTED )
460 /* Don't send the a corrupted packet to
461 * theora_decode, otherwise we get purple/green display artifacts
462 * appearing in the video output */
463 block_Release(p_block);
464 return NULL;
468 /* Date management */
469 if( p_block->i_pts != VLC_TICK_INVALID && p_block->i_pts != p_sys->i_pts )
471 p_sys->i_pts = p_block->i_pts;
474 if( p_sys->b_packetizer )
476 /* Date management */
477 p_block->i_dts = p_block->i_pts = p_sys->i_pts;
479 p_block->i_length = p_sys->i_pts - p_block->i_pts;
481 p_buf = p_block;
483 else
485 p_buf = DecodePacket( p_dec, p_oggpacket );
486 block_Release( p_block );
489 /* Date management */
490 p_sys->i_pts += ( CLOCK_FREQ * p_sys->ti.fps_denominator /
491 p_sys->ti.fps_numerator ); /* 1 frame per packet */
493 return p_buf;
496 /*****************************************************************************
497 * DecodePacket: decodes a Theora packet.
498 *****************************************************************************/
499 static picture_t *DecodePacket( decoder_t *p_dec, ogg_packet *p_oggpacket )
501 decoder_sys_t *p_sys = p_dec->p_sys;
502 picture_t *p_pic;
503 th_ycbcr_buffer ycbcr;
505 /* TODO: Implement _granpos (3rd parameter here) and add the
506 * call to TH_DECCTL_SET_GRANDPOS after seek */
507 /* TODO: If the return is TH_DUPFRAME, we don't need to display a new
508 * frame, but we do need to keep displaying the previous one. */
509 if (th_decode_packetin( p_sys->tcx, p_oggpacket, NULL ) < 0)
510 return NULL; /* bad packet */
512 /* Check for keyframe */
513 if( !(p_oggpacket->packet[0] & 0x80) /* data packet */ &&
514 !(p_oggpacket->packet[0] & 0x40) /* intra frame */ )
515 p_sys->b_decoded_first_keyframe = true;
517 /* If we haven't seen a single keyframe yet, don't let Theora decode
518 * anything, otherwise we'll get display artifacts. (This is impossible
519 * in the general case, but can happen if e.g. we play a network stream
520 * using a timed URL, such that the server doesn't start the video with a
521 * keyframe). */
522 if( !p_sys->b_decoded_first_keyframe )
523 return NULL; /* Wait until we've decoded the first keyframe */
525 if( th_decode_ycbcr_out( p_sys->tcx, ycbcr ) ) /* returns 0 on success */
526 return NULL;
528 /* Get a new picture */
529 if( decoder_UpdateVideoFormat( p_dec ) )
530 return NULL;
531 p_pic = decoder_NewPicture( p_dec );
532 if( !p_pic ) return NULL;
534 theora_CopyPicture( p_pic, ycbcr );
536 p_pic->date = p_sys->i_pts;
537 p_pic->b_progressive = true;
539 return p_pic;
542 /*****************************************************************************
543 * ParseTheoraComments:
544 *****************************************************************************/
545 static void ParseTheoraComments( decoder_t *p_dec )
547 char *psz_name, *psz_value, *psz_comment;
548 int i = 0;
550 decoder_sys_t *p_sys = p_dec->p_sys;
552 /* Regarding the th_comment structure: */
554 /* The metadata is stored as a series of (tag, value) pairs, in
555 length-encoded string vectors. The first occurrence of the '='
556 character delimits the tag and value. A particular tag may
557 occur more than once, and order is significant. The character
558 set encoding for the strings is always UTF-8, but the tag names
559 are limited to ASCII, and treated as case-insensitive. See the
560 Theora specification, Section 6.3.3 for details. */
562 /* In filling in this structure, th_decode_headerin() will
563 null-terminate the user_comment strings for safety. However,
564 the bitstream format itself treats them as 8-bit clean vectors,
565 possibly containing null characters, and so the length array
566 should be treated as their authoritative length. */
567 while ( i < p_sys->tc.comments )
569 int clen = p_sys->tc.comment_lengths[i];
570 if ( clen <= 0 || clen >= INT_MAX ) { i++; continue; }
571 psz_comment = (char *)malloc( clen + 1 );
572 if( !psz_comment )
573 break;
574 memcpy( (void*)psz_comment, (void*)p_sys->tc.user_comments[i], clen + 1 );
575 psz_name = psz_comment;
576 psz_value = strchr( psz_comment, '=' );
577 if( psz_value )
579 *psz_value = '\0';
580 psz_value++;
582 if( !p_dec->p_description )
583 p_dec->p_description = vlc_meta_New();
584 /* TODO: Since psz_value can contain NULLs see if there is an
585 * instance where we need to preserve the full length of this string */
586 if( p_dec->p_description )
587 vlc_meta_AddExtra( p_dec->p_description, psz_name, psz_value );
589 free( psz_comment );
590 i++;
594 /*****************************************************************************
595 * CloseDecoder: theora decoder destruction
596 *****************************************************************************/
597 static void CloseDecoder( vlc_object_t *p_this )
599 decoder_t *p_dec = (decoder_t *)p_this;
600 decoder_sys_t *p_sys = p_dec->p_sys;
602 th_info_clear(&p_sys->ti);
603 th_comment_clear(&p_sys->tc);
604 th_decode_free(p_sys->tcx);
605 free( p_sys );
608 /*****************************************************************************
609 * theora_CopyPicture: copy a picture from theora internal buffers to a
610 * picture_t structure.
611 *****************************************************************************/
612 static void theora_CopyPicture( picture_t *p_pic,
613 th_ycbcr_buffer ycbcr )
615 int i_plane, i_planes;
616 /* th_img_plane
617 int width The width of this plane.
618 int height The height of this plane.
619 int stride The offset in bytes between successive rows.
620 unsigned char *data A pointer to the beginning of the first row.
622 Detailed Description
624 A buffer for a single color plane in an uncompressed image.
626 This contains the image data in a left-to-right, top-down
627 format. Each row of pixels is stored contiguously in memory,
628 but successive rows need not be. Use stride to compute the
629 offset of the next row. The encoder accepts both positive
630 stride values (top-down in memory) and negative (bottom-up in
631 memory). The decoder currently always generates images with
632 positive strides.
634 typedef th_img_plane th_ycbcr_buffer[3]
637 i_planes = __MIN(p_pic->i_planes, 3);
638 for( i_plane = 0; i_plane < i_planes; i_plane++ )
640 plane_t src;
641 src.i_lines = ycbcr[i_plane].height;
642 src.p_pixels = ycbcr[i_plane].data;
643 src.i_pitch = ycbcr[i_plane].stride;
644 src.i_visible_pitch = src.i_pitch;
645 src.i_visible_lines = src.i_lines;
646 plane_CopyPixels( &p_pic->p[i_plane], &src );
650 #ifdef ENABLE_SOUT
651 /*****************************************************************************
652 * encoder_sys_t : theora encoder descriptor
653 *****************************************************************************/
654 typedef struct
657 * Input properties
659 bool b_headers;
662 * Theora properties
664 th_info ti; /* theora bitstream settings */
665 th_comment tc; /* theora comment header */
666 th_enc_ctx *tcx; /* theora context */
667 } encoder_sys_t;
669 /*****************************************************************************
670 * OpenEncoder: probe the encoder and return score
671 *****************************************************************************/
672 static int OpenEncoder( vlc_object_t *p_this )
674 encoder_t *p_enc = (encoder_t *)p_this;
675 encoder_sys_t *p_sys;
676 int i_quality;
677 int t_flags;
678 int max_enc_level = 0;
679 int keyframe_freq_force = 64;
680 ogg_packet header;
681 int status;
683 if( p_enc->fmt_out.i_codec != VLC_CODEC_THEORA &&
684 !p_enc->obj.force )
686 return VLC_EGENERIC;
689 /* Allocate the memory needed to store the encoder's structure */
690 if( ( p_sys = malloc(sizeof(encoder_sys_t)) ) == NULL )
691 return VLC_ENOMEM;
692 p_enc->p_sys = p_sys;
694 p_enc->pf_encode_video = Encode;
695 p_enc->fmt_in.i_codec = VLC_CODEC_I420;
696 p_enc->fmt_out.i_codec = VLC_CODEC_THEORA;
698 config_ChainParse( p_enc, ENC_CFG_PREFIX, ppsz_enc_options, p_enc->p_cfg );
700 i_quality = var_GetInteger( p_enc, ENC_CFG_PREFIX "quality" );
701 if( i_quality > 10 ) i_quality = 10;
702 if( i_quality < 0 ) i_quality = 0;
704 th_info_init( &p_sys->ti );
706 p_sys->ti.frame_width = p_enc->fmt_in.video.i_visible_width;
707 p_sys->ti.frame_height = p_enc->fmt_in.video.i_visible_height;
709 if( p_sys->ti.frame_width % 16 || p_sys->ti.frame_height % 16 )
711 /* Pictures from the transcoder should always have a pitch
712 * which is a multiple of 16 */
713 p_sys->ti.frame_width = (p_sys->ti.frame_width + 15) >> 4 << 4;
714 p_sys->ti.frame_height = (p_sys->ti.frame_height + 15) >> 4 << 4;
716 msg_Dbg( p_enc, "padding video from %dx%d to %dx%d",
717 p_enc->fmt_in.video.i_visible_width, p_enc->fmt_in.video.i_visible_height,
718 p_sys->ti.frame_width, p_sys->ti.frame_height );
721 p_sys->ti.pic_width = p_enc->fmt_in.video.i_visible_width;
722 p_sys->ti.pic_height = p_enc->fmt_in.video.i_visible_height;
723 p_sys->ti.pic_x = 0 /*frame_x_offset*/;
724 p_sys->ti.pic_y = 0 /*frame_y_offset*/;
726 if( !p_enc->fmt_in.video.i_frame_rate ||
727 !p_enc->fmt_in.video.i_frame_rate_base )
729 p_sys->ti.fps_numerator = 25;
730 p_sys->ti.fps_denominator = 1;
732 else
734 p_sys->ti.fps_numerator = p_enc->fmt_in.video.i_frame_rate;
735 p_sys->ti.fps_denominator = p_enc->fmt_in.video.i_frame_rate_base;
738 if( p_enc->fmt_in.video.i_sar_num > 0 && p_enc->fmt_in.video.i_sar_den > 0 )
740 unsigned i_dst_num, i_dst_den;
741 vlc_ureduce( &i_dst_num, &i_dst_den,
742 p_enc->fmt_in.video.i_sar_num,
743 p_enc->fmt_in.video.i_sar_den, 0 );
744 p_sys->ti.aspect_numerator = i_dst_num;
745 p_sys->ti.aspect_denominator = i_dst_den;
747 else
749 p_sys->ti.aspect_numerator = 4;
750 p_sys->ti.aspect_denominator = 3;
753 p_sys->ti.target_bitrate = p_enc->fmt_out.i_bitrate;
754 p_sys->ti.quality = ((float)i_quality) * 6.3f;
757 p_sys->tcx = th_encode_alloc( &p_sys->ti );
758 th_comment_init( &p_sys->tc );
760 /* These are no longer supported here: */
762 p_sys->ti.dropframes_p = 0;
763 p_sys->ti.quick_p = 1;
764 p_sys->ti.keyframe_auto_p = 1;
765 p_sys->ti.keyframe_frequency = 64;
766 p_sys->ti.keyframe_frequency_force = 64;
767 p_sys->ti.keyframe_data_target_bitrate = p_enc->fmt_out.i_bitrate * 1.5;
768 p_sys->ti.keyframe_auto_threshold = 80;
769 p_sys->ti.keyframe_mindistance = 8;
770 p_sys->ti.noise_sensitivity = 1;
773 t_flags = TH_RATECTL_CAP_OVERFLOW; /* default is TH_RATECTL_CAP_OVERFLOW | TL_RATECTL_DROP_FRAMES */
774 /* Turn off dropframes */
775 th_encode_ctl( p_sys->tcx, TH_ENCCTL_SET_RATE_FLAGS, &t_flags, sizeof(t_flags) );
777 /* turn on fast encoding */
778 if ( !th_encode_ctl( p_sys->tcx, TH_ENCCTL_GET_SPLEVEL_MAX, &max_enc_level,
779 sizeof(max_enc_level) ) ) /* returns 0 on success */
780 th_encode_ctl( p_sys->tcx, TH_ENCCTL_SET_SPLEVEL, &max_enc_level, sizeof(max_enc_level) );
782 /* Set forced distance between key frames */
783 th_encode_ctl( p_sys->tcx, TH_ENCCTL_SET_KEYFRAME_FREQUENCY_FORCE,
784 &keyframe_freq_force, sizeof(keyframe_freq_force) );
786 /* Create and store headers */
787 while ( ( status = th_encode_flushheader( p_sys->tcx, &p_sys->tc, &header ) ) )
789 if ( status < 0 ) return VLC_EGENERIC;
790 if( xiph_AppendHeaders( &p_enc->fmt_out.i_extra, &p_enc->fmt_out.p_extra,
791 header.bytes, header.packet ) )
793 p_enc->fmt_out.i_extra = 0;
794 p_enc->fmt_out.p_extra = NULL;
797 return VLC_SUCCESS;
800 /****************************************************************************
801 * Encode: the whole thing
802 ****************************************************************************
803 * This function spits out ogg packets.
804 ****************************************************************************/
805 static block_t *Encode( encoder_t *p_enc, picture_t *p_pict )
807 encoder_sys_t *p_sys = p_enc->p_sys;
808 ogg_packet oggpacket;
809 block_t *p_block;
810 th_ycbcr_buffer ycbcr;
811 unsigned i;
813 if( !p_pict ) return NULL;
814 /* Sanity check */
815 if( p_pict->p[0].i_pitch < (int)p_sys->ti.frame_width ||
816 p_pict->p[0].i_lines < (int)p_sys->ti.frame_height )
818 msg_Warn( p_enc, "frame is smaller than encoding size"
819 "(%ix%i->%ix%i) -> dropping frame",
820 p_pict->p[0].i_pitch, p_pict->p[0].i_lines,
821 p_sys->ti.frame_width, p_sys->ti.frame_height );
822 return NULL;
825 /* Fill padding */
826 if( p_pict->p[0].i_visible_pitch < (int)p_sys->ti.frame_width )
828 for( i = 0; i < p_sys->ti.frame_height; i++ )
830 memset( p_pict->p[0].p_pixels + i * p_pict->p[0].i_pitch +
831 p_pict->p[0].i_visible_pitch,
832 *( p_pict->p[0].p_pixels + i * p_pict->p[0].i_pitch +
833 p_pict->p[0].i_visible_pitch - 1 ),
834 p_sys->ti.frame_width - p_pict->p[0].i_visible_pitch );
836 for( i = 0; i < p_sys->ti.frame_height / 2; i++ )
838 memset( p_pict->p[1].p_pixels + i * p_pict->p[1].i_pitch +
839 p_pict->p[1].i_visible_pitch,
840 *( p_pict->p[1].p_pixels + i * p_pict->p[1].i_pitch +
841 p_pict->p[1].i_visible_pitch - 1 ),
842 p_sys->ti.frame_width / 2 - p_pict->p[1].i_visible_pitch );
843 memset( p_pict->p[2].p_pixels + i * p_pict->p[2].i_pitch +
844 p_pict->p[2].i_visible_pitch,
845 *( p_pict->p[2].p_pixels + i * p_pict->p[2].i_pitch +
846 p_pict->p[2].i_visible_pitch - 1 ),
847 p_sys->ti.frame_width / 2 - p_pict->p[2].i_visible_pitch );
851 if( p_pict->p[0].i_visible_lines < (int)p_sys->ti.frame_height )
853 for( i = p_pict->p[0].i_visible_lines; i < p_sys->ti.frame_height; i++ )
855 memset( p_pict->p[0].p_pixels + i * p_pict->p[0].i_pitch, 0,
856 p_sys->ti.frame_width );
858 for( i = p_pict->p[1].i_visible_lines; i < p_sys->ti.frame_height / 2; i++ )
860 memset( p_pict->p[1].p_pixels + i * p_pict->p[1].i_pitch, 0x80,
861 p_sys->ti.frame_width / 2 );
862 memset( p_pict->p[2].p_pixels + i * p_pict->p[2].i_pitch, 0x80,
863 p_sys->ti.frame_width / 2 );
867 /* Theora is a one-frame-in, one-frame-out system. Submit a frame
868 * for compression and pull out the packet. */
870 ycbcr[0].width = p_sys->ti.frame_width;
871 ycbcr[0].height = p_sys->ti.frame_height;
872 ycbcr[0].stride = p_pict->p[0].i_pitch;
873 ycbcr[0].data = p_pict->p[0].p_pixels;
875 ycbcr[1].width = p_sys->ti.frame_width / 2;
876 ycbcr[1].height = p_sys->ti.frame_height / 2;
877 ycbcr[1].stride = p_pict->p[1].i_pitch;
878 ycbcr[1].data = p_pict->p[1].p_pixels;
880 ycbcr[2].width = p_sys->ti.frame_width / 2;
881 ycbcr[2].height = p_sys->ti.frame_height / 2;
882 ycbcr[2].stride = p_pict->p[1].i_pitch;
883 ycbcr[2].data = p_pict->p[2].p_pixels;
885 if( th_encode_ycbcr_in( p_sys->tcx, ycbcr ) < 0 )
887 msg_Warn( p_enc, "failed encoding a frame" );
888 return NULL;
891 th_encode_packetout( p_sys->tcx, 0, &oggpacket );
893 /* Ogg packet to block */
894 p_block = block_Alloc( oggpacket.bytes );
895 memcpy( p_block->p_buffer, oggpacket.packet, oggpacket.bytes );
896 p_block->i_dts = p_block->i_pts = p_pict->date;
898 if( th_packet_iskeyframe( &oggpacket ) )
900 p_block->i_flags |= BLOCK_FLAG_TYPE_I;
903 return p_block;
906 /*****************************************************************************
907 * CloseEncoder: theora encoder destruction
908 *****************************************************************************/
909 static void CloseEncoder( vlc_object_t *p_this )
911 encoder_t *p_enc = (encoder_t *)p_this;
912 encoder_sys_t *p_sys = p_enc->p_sys;
914 th_info_clear(&p_sys->ti);
915 th_comment_clear(&p_sys->tc);
916 th_encode_free(p_sys->tcx);
917 free( p_sys );
919 #endif