Main Page | Modules | Class Hierarchy | Class List | Directories | File List | Class Members | File Members | Related Pages

theora.c

00001 /*****************************************************************************
00002  * theora.c: theora decoder module making use of libtheora.
00003  *****************************************************************************
00004  * Copyright (C) 1999-2001 the VideoLAN team
00005  * $Id: theora.c 11724 2005-07-13 17:51:13Z courmisch $
00006  *
00007  * Authors: Gildas Bazin <[email protected]>
00008  *
00009  * This program is free software; you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation; either version 2 of the License, or
00012  * (at your option) any later version.
00013  *
00014  * This program is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  * GNU General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU General Public License
00020  * along with this program; if not, write to the Free Software
00021  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
00022  *****************************************************************************/
00023 
00024 /*****************************************************************************
00025  * Preamble
00026  *****************************************************************************/
00027 #include <vlc/vlc.h>
00028 #include <vlc/decoder.h>
00029 #include <vlc/input.h>
00030 #include <vlc/sout.h>
00031 
00032 #include <ogg/ogg.h>
00033 
00034 #include <theora/theora.h>
00035 
00036 /*****************************************************************************
00037  * decoder_sys_t : theora decoder descriptor
00038  *****************************************************************************/
00039 struct decoder_sys_t
00040 {
00041     /* Module mode */
00042     vlc_bool_t b_packetizer;
00043 
00044     /*
00045      * Input properties
00046      */
00047     int i_headers;
00048 
00049     /*
00050      * Theora properties
00051      */
00052     theora_info      ti;                        /* theora bitstream settings */
00053     theora_comment   tc;                            /* theora comment header */
00054     theora_state     td;                   /* theora bitstream user comments */
00055 
00056     /*
00057      * Decoding properties
00058      */
00059     vlc_bool_t b_decoded_first_keyframe;
00060 
00061     /*
00062      * Common properties
00063      */
00064     mtime_t i_pts;
00065 };
00066 
00067 /*****************************************************************************
00068  * Local prototypes
00069  *****************************************************************************/
00070 static int  OpenDecoder   ( vlc_object_t * );
00071 static int  OpenPacketizer( vlc_object_t * );
00072 static void CloseDecoder  ( vlc_object_t * );
00073 
00074 static void *DecodeBlock  ( decoder_t *, block_t ** );
00075 static int  ProcessHeaders( decoder_t * );
00076 static void *ProcessPacket ( decoder_t *, ogg_packet *, block_t ** );
00077 
00078 static picture_t *DecodePacket( decoder_t *, ogg_packet * );
00079 
00080 static void ParseTheoraComments( decoder_t * );
00081 static void theora_CopyPicture( decoder_t *, picture_t *, yuv_buffer * );
00082 
00083 static int  OpenEncoder( vlc_object_t *p_this );
00084 static void CloseEncoder( vlc_object_t *p_this );
00085 static block_t *Encode( encoder_t *p_enc, picture_t *p_pict );
00086 
00087 /*****************************************************************************
00088  * Module descriptor
00089  *****************************************************************************/
00090 #define ENC_QUALITY_TEXT N_("Encoding quality")
00091 #define ENC_QUALITY_LONGTEXT N_( \
00092   "Allows you to specify a quality between 1 (low) and 10 (high), instead " \
00093   "of specifying a particular bitrate. This will produce a VBR stream." )
00094 
00095 vlc_module_begin();
00096     set_category( CAT_INPUT );
00097     set_subcategory( SUBCAT_INPUT_VCODEC );
00098     set_shortname( "Theora" );
00099     set_description( _("Theora video decoder") );
00100     set_capability( "decoder", 100 );
00101     set_callbacks( OpenDecoder, CloseDecoder );
00102     add_shortcut( "theora" );
00103 
00104     add_submodule();
00105     set_description( _("Theora video packetizer") );
00106     set_capability( "packetizer", 100 );
00107     set_callbacks( OpenPacketizer, CloseDecoder );
00108     add_shortcut( "theora" );
00109 
00110     add_submodule();
00111     set_description( _("Theora video encoder") );
00112     set_capability( "encoder", 150 );
00113     set_callbacks( OpenEncoder, CloseEncoder );
00114     add_shortcut( "theora" );
00115 
00116 #   define ENC_CFG_PREFIX "sout-theora-"
00117     add_integer( ENC_CFG_PREFIX "quality", 2, NULL, ENC_QUALITY_TEXT,
00118                  ENC_QUALITY_LONGTEXT, VLC_FALSE );
00119 vlc_module_end();
00120 
00121 static const char *ppsz_enc_options[] = {
00122     "quality", NULL
00123 };
00124 
00125 /*****************************************************************************
00126  * OpenDecoder: probe the decoder and return score
00127  *****************************************************************************/
00128 static int OpenDecoder( vlc_object_t *p_this )
00129 {
00130     decoder_t *p_dec = (decoder_t*)p_this;
00131     decoder_sys_t *p_sys;
00132 
00133     if( p_dec->fmt_in.i_codec != VLC_FOURCC('t','h','e','o') )
00134     {
00135         return VLC_EGENERIC;
00136     }
00137 
00138     /* Allocate the memory needed to store the decoder's structure */
00139     if( ( p_dec->p_sys = p_sys =
00140           (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
00141     {
00142         msg_Err( p_dec, "out of memory" );
00143         return VLC_EGENERIC;
00144     }
00145     p_dec->p_sys->b_packetizer = VLC_FALSE;
00146 
00147     p_sys->i_pts = 0;
00148     p_sys->b_decoded_first_keyframe = VLC_FALSE;
00149 
00150     /* Set output properties */
00151     p_dec->fmt_out.i_cat = VIDEO_ES;
00152     p_dec->fmt_out.i_codec = VLC_FOURCC('I','4','2','0');
00153 
00154     /* Set callbacks */
00155     p_dec->pf_decode_video = (picture_t *(*)(decoder_t *, block_t **))
00156         DecodeBlock;
00157     p_dec->pf_packetize    = (block_t *(*)(decoder_t *, block_t **))
00158         DecodeBlock;
00159 
00160     /* Init supporting Theora structures needed in header parsing */
00161     theora_comment_init( &p_sys->tc );
00162     theora_info_init( &p_sys->ti );
00163 
00164     p_sys->i_headers = 0;
00165 
00166     return VLC_SUCCESS;
00167 }
00168 
00169 static int OpenPacketizer( vlc_object_t *p_this )
00170 {
00171     decoder_t *p_dec = (decoder_t*)p_this;
00172 
00173     int i_ret = OpenDecoder( p_this );
00174 
00175     if( i_ret == VLC_SUCCESS )
00176     {
00177         p_dec->p_sys->b_packetizer = VLC_TRUE;
00178         p_dec->fmt_out.i_codec = VLC_FOURCC( 't', 'h', 'e', 'o' );
00179     }
00180 
00181     return i_ret;
00182 }
00183 
00184 /****************************************************************************
00185  * DecodeBlock: the whole thing
00186  ****************************************************************************
00187  * This function must be fed with ogg packets.
00188  ****************************************************************************/
00189 static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
00190 {
00191     decoder_sys_t *p_sys = p_dec->p_sys;
00192     block_t *p_block;
00193     ogg_packet oggpacket;
00194 
00195     if( !pp_block || !*pp_block ) return NULL;
00196 
00197     p_block = *pp_block;
00198 
00199     /* Block to Ogg packet */
00200     oggpacket.packet = p_block->p_buffer;
00201     oggpacket.bytes = p_block->i_buffer;
00202     oggpacket.granulepos = p_block->i_dts;
00203     oggpacket.b_o_s = 0;
00204     oggpacket.e_o_s = 0;
00205     oggpacket.packetno = 0;
00206 
00207     /* Check for headers */
00208     if( p_sys->i_headers == 0 && p_dec->fmt_in.i_extra )
00209     {
00210         /* Headers already available as extra data */
00211         p_sys->i_headers = 3;
00212     }
00213     else if( oggpacket.bytes && p_sys->i_headers < 3 )
00214     {
00215         /* Backup headers as extra data */
00216         uint8_t *p_extra;
00217 
00218         p_dec->fmt_in.p_extra =
00219             realloc( p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra +
00220                      oggpacket.bytes + 2 );
00221         p_extra = p_dec->fmt_in.p_extra + p_dec->fmt_in.i_extra;
00222         *(p_extra++) = oggpacket.bytes >> 8;
00223         *(p_extra++) = oggpacket.bytes & 0xFF;
00224 
00225         memcpy( p_extra, oggpacket.packet, oggpacket.bytes );
00226         p_dec->fmt_in.i_extra += oggpacket.bytes + 2;
00227 
00228         block_Release( *pp_block );
00229         p_sys->i_headers++;
00230         return NULL;
00231     }
00232 
00233     if( p_sys->i_headers == 3 )
00234     {
00235         if( ProcessHeaders( p_dec ) != VLC_SUCCESS )
00236         {
00237             p_sys->i_headers = 0;
00238             p_dec->fmt_in.i_extra = 0;
00239             block_Release( *pp_block );
00240             return NULL;
00241         }
00242         else p_sys->i_headers++;
00243     }
00244 
00245     return ProcessPacket( p_dec, &oggpacket, pp_block );
00246 }
00247 
00248 /*****************************************************************************
00249  * ProcessHeaders: process Theora headers.
00250  *****************************************************************************/
00251 static int ProcessHeaders( decoder_t *p_dec )
00252 {
00253     decoder_sys_t *p_sys = p_dec->p_sys;
00254     ogg_packet oggpacket;
00255     uint8_t *p_extra;
00256     int i_extra;
00257 
00258     if( !p_dec->fmt_in.i_extra ) return VLC_EGENERIC;
00259 
00260     oggpacket.granulepos = -1;
00261     oggpacket.b_o_s = 1; /* yes this actually is a b_o_s packet :) */
00262     oggpacket.e_o_s = 0;
00263     oggpacket.packetno = 0;
00264     p_extra = p_dec->fmt_in.p_extra;
00265     i_extra = p_dec->fmt_in.i_extra;
00266 
00267     /* Take care of the initial Vorbis header */
00268     oggpacket.bytes = *(p_extra++) << 8;
00269     oggpacket.bytes |= (*(p_extra++) & 0xFF);
00270     oggpacket.packet = p_extra;
00271     p_extra += oggpacket.bytes;
00272     i_extra -= (oggpacket.bytes + 2);
00273     if( i_extra < 0 )
00274     {
00275         msg_Err( p_dec, "header data corrupted");
00276         return VLC_EGENERIC;
00277     }
00278 
00279     if( theora_decode_header( &p_sys->ti, &p_sys->tc, &oggpacket ) < 0 )
00280     {
00281         msg_Err( p_dec, "This bitstream does not contain Theora video data" );
00282         return VLC_EGENERIC;
00283     }
00284 
00285     /* Set output properties */
00286     p_dec->fmt_out.video.i_width = p_sys->ti.width;
00287     p_dec->fmt_out.video.i_height = p_sys->ti.height;
00288     if( p_sys->ti.frame_width && p_sys->ti.frame_height )
00289     {
00290         p_dec->fmt_out.video.i_width = p_sys->ti.frame_width;
00291         p_dec->fmt_out.video.i_height = p_sys->ti.frame_height;
00292     }
00293 
00294     if( p_sys->ti.aspect_denominator && p_sys->ti.aspect_numerator )
00295     {
00296         p_dec->fmt_out.video.i_aspect = ((int64_t)VOUT_ASPECT_FACTOR) *
00297             ( p_sys->ti.aspect_numerator * p_sys->ti.width ) /
00298             ( p_sys->ti.aspect_denominator * p_sys->ti.height );
00299     }
00300     else
00301     {
00302         p_dec->fmt_out.video.i_aspect = VOUT_ASPECT_FACTOR *
00303             p_sys->ti.frame_width / p_sys->ti.frame_height;
00304     }
00305 
00306     if( p_sys->ti.fps_numerator > 0 && p_sys->ti.fps_denominator > 0 )
00307     {
00308         p_dec->fmt_out.video.i_frame_rate = p_sys->ti.fps_numerator;
00309         p_dec->fmt_out.video.i_frame_rate_base = p_sys->ti.fps_denominator;
00310     }
00311 
00312     msg_Dbg( p_dec, "%dx%d %.02f fps video, frame content "
00313              "is %dx%d with offset (%d,%d)",
00314              p_sys->ti.width, p_sys->ti.height,
00315              (double)p_sys->ti.fps_numerator/p_sys->ti.fps_denominator,
00316              p_sys->ti.frame_width, p_sys->ti.frame_height,
00317              p_sys->ti.offset_x, p_sys->ti.offset_y );
00318 
00319     /* Sanity check that seems necessary for some corrupted files */
00320     if( p_sys->ti.width < p_sys->ti.frame_width ||
00321         p_sys->ti.height < p_sys->ti.frame_height )
00322     {
00323         msg_Warn( p_dec, "trying to correct invalid theora header "
00324                   "(frame size (%dx%d) is smaller than frame content (%d,%d))",
00325                   p_sys->ti.width, p_sys->ti.height,
00326                   p_sys->ti.frame_width, p_sys->ti.frame_height );
00327 
00328         if( p_sys->ti.width < p_sys->ti.frame_width )
00329             p_sys->ti.width = p_sys->ti.frame_width;
00330         if( p_sys->ti.height < p_sys->ti.frame_height )
00331             p_sys->ti.height = p_sys->ti.frame_height;
00332     }
00333 
00334     /* The next packet in order is the comments header */
00335     oggpacket.b_o_s = 0;
00336     oggpacket.bytes = *(p_extra++) << 8;
00337     oggpacket.bytes |= (*(p_extra++) & 0xFF);
00338     oggpacket.packet = p_extra;
00339     p_extra += oggpacket.bytes;
00340     i_extra -= (oggpacket.bytes + 2);
00341     if( i_extra < 0 )
00342     {
00343         msg_Err( p_dec, "header data corrupted");
00344         return VLC_EGENERIC;
00345     }
00346 
00347     /* The next packet in order is the comments header */
00348     if( theora_decode_header( &p_sys->ti, &p_sys->tc, &oggpacket ) < 0 )
00349     {
00350         msg_Err( p_dec, "2nd Theora header is corrupted" );
00351         return VLC_EGENERIC;
00352     }
00353 
00354     ParseTheoraComments( p_dec );
00355 
00356     /* The next packet in order is the codebooks header
00357      * We need to watch out that this packet is not missing as a
00358      * missing or corrupted header is fatal. */
00359     oggpacket.bytes = *(p_extra++) << 8;
00360     oggpacket.bytes |= (*(p_extra++) & 0xFF);
00361     oggpacket.packet = p_extra;
00362     i_extra -= (oggpacket.bytes + 2);
00363     if( i_extra < 0 )
00364     {
00365         msg_Err( p_dec, "header data corrupted");
00366         return VLC_EGENERIC;
00367     }
00368 
00369     /* The next packet in order is the codebooks header
00370      * We need to watch out that this packet is not missing as a
00371      * missing or corrupted header is fatal */
00372     if( theora_decode_header( &p_sys->ti, &p_sys->tc, &oggpacket ) < 0 )
00373     {
00374         msg_Err( p_dec, "3rd Theora header is corrupted" );
00375         return VLC_EGENERIC;
00376     }
00377 
00378     if( !p_sys->b_packetizer )
00379     {
00380         /* We have all the headers, initialize decoder */
00381         theora_decode_init( &p_sys->td, &p_sys->ti );
00382     }
00383     else
00384     {
00385         p_dec->fmt_out.i_extra = p_dec->fmt_in.i_extra;
00386         p_dec->fmt_out.p_extra =
00387             realloc( p_dec->fmt_out.p_extra, p_dec->fmt_out.i_extra );
00388         memcpy( p_dec->fmt_out.p_extra,
00389                 p_dec->fmt_in.p_extra, p_dec->fmt_out.i_extra );
00390     }
00391 
00392     return VLC_SUCCESS;
00393 }
00394 
00395 /*****************************************************************************
00396  * ProcessPacket: processes a theora packet.
00397  *****************************************************************************/
00398 static void *ProcessPacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
00399                             block_t **pp_block )
00400 {
00401     decoder_sys_t *p_sys = p_dec->p_sys;
00402     block_t *p_block = *pp_block;
00403     void *p_buf;
00404 
00405     if( ( p_block->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) ) != 0 )
00406     {
00407         /* Don't send the the first packet after a discontinuity to
00408          * theora_decode, otherwise we get purple/green display artifacts
00409          * appearing in the video output */
00410         return NULL;
00411     }
00412 
00413     /* Date management */
00414     if( p_block->i_pts > 0 && p_block->i_pts != p_sys->i_pts )
00415     {
00416         p_sys->i_pts = p_block->i_pts;
00417     }
00418 
00419     *pp_block = NULL; /* To avoid being fed the same packet again */
00420 
00421     if( p_sys->b_packetizer )
00422     {
00423         /* Date management */
00424         p_block->i_dts = p_block->i_pts = p_sys->i_pts;
00425 
00426         if( p_sys->i_headers >= 3 )
00427             p_block->i_length = p_sys->i_pts - p_block->i_pts;
00428         else
00429             p_block->i_length = 0;
00430 
00431         p_buf = p_block;
00432     }
00433     else
00434     {
00435         if( p_sys->i_headers >= 3 )
00436             p_buf = DecodePacket( p_dec, p_oggpacket );
00437         else
00438             p_buf = NULL;
00439 
00440         if( p_block ) block_Release( p_block );
00441     }
00442 
00443     /* Date management */
00444     p_sys->i_pts += ( I64C(1000000) * p_sys->ti.fps_denominator /
00445                       p_sys->ti.fps_numerator ); /* 1 frame per packet */
00446 
00447     return p_buf;
00448 }
00449 
00450 /*****************************************************************************
00451  * DecodePacket: decodes a Theora packet.
00452  *****************************************************************************/
00453 static picture_t *DecodePacket( decoder_t *p_dec, ogg_packet *p_oggpacket )
00454 {
00455     decoder_sys_t *p_sys = p_dec->p_sys;
00456     picture_t *p_pic;
00457     yuv_buffer yuv;
00458 
00459     theora_decode_packetin( &p_sys->td, p_oggpacket );
00460 
00461     /* Check for keyframe */
00462     if( !(p_oggpacket->packet[0] & 0x80) /* data packet */ &&
00463         !(p_oggpacket->packet[0] & 0x40) /* intra frame */ )
00464         p_sys->b_decoded_first_keyframe = VLC_TRUE;
00465 
00466     /* If we haven't seen a single keyframe yet, don't let Theora decode
00467      * anything, otherwise we'll get display artifacts.  (This is impossible
00468      * in the general case, but can happen if e.g. we play a network stream
00469      * using a timed URL, such that the server doesn't start the video with a
00470      * keyframe). */
00471     if( p_sys->b_decoded_first_keyframe )
00472         theora_decode_YUVout( &p_sys->td, &yuv );
00473     else
00474         return NULL;
00475 
00476     /* Get a new picture */
00477     p_pic = p_dec->pf_vout_buffer_new( p_dec );
00478     if( !p_pic ) return NULL;
00479 
00480     theora_CopyPicture( p_dec, p_pic, &yuv );
00481 
00482     p_pic->date = p_sys->i_pts;
00483 
00484     return p_pic;
00485 }
00486 
00487 /*****************************************************************************
00488  * ParseTheoraComments: FIXME should be done in demuxer
00489  *****************************************************************************/
00490 static void ParseTheoraComments( decoder_t *p_dec )
00491 {
00492     input_thread_t *p_input = (input_thread_t *)p_dec->p_parent;
00493     char *psz_name, *psz_value, *psz_comment;
00494     int i = 0;
00495 
00496     if( p_input->i_object_type != VLC_OBJECT_INPUT ) return;
00497 
00498     while ( i < p_dec->p_sys->tc.comments )
00499     {
00500         psz_comment = strdup( p_dec->p_sys->tc.user_comments[i] );
00501         if( !psz_comment )
00502         {
00503             msg_Warn( p_dec, "out of memory" );
00504             break;
00505         }
00506         psz_name = psz_comment;
00507         psz_value = strchr( psz_comment, '=' );
00508         if( psz_value )
00509         {
00510             *psz_value = '\0';
00511             psz_value++;
00512             input_Control( p_input, INPUT_ADD_INFO, _("Theora comment"),
00513                            psz_name, psz_value );
00514         }
00515         free( psz_comment );
00516         i++;
00517     }
00518 }
00519 
00520 /*****************************************************************************
00521  * CloseDecoder: theora decoder destruction
00522  *****************************************************************************/
00523 static void CloseDecoder( vlc_object_t *p_this )
00524 {
00525     decoder_t *p_dec = (decoder_t *)p_this;
00526     decoder_sys_t *p_sys = p_dec->p_sys;
00527 
00528     theora_info_clear( &p_sys->ti );
00529     theora_comment_clear( &p_sys->tc );
00530 
00531     free( p_sys );
00532 }
00533 
00534 /*****************************************************************************
00535  * theora_CopyPicture: copy a picture from theora internal buffers to a
00536  *                     picture_t structure.
00537  *****************************************************************************/
00538 static void theora_CopyPicture( decoder_t *p_dec, picture_t *p_pic,
00539                                 yuv_buffer *yuv )
00540 {
00541     int i_plane, i_line, i_width, i_dst_stride, i_src_stride;
00542     int i_src_xoffset, i_src_yoffset;
00543     uint8_t *p_dst, *p_src;
00544 
00545     for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
00546     {
00547         p_dst = p_pic->p[i_plane].p_pixels;
00548         p_src = i_plane ? (i_plane - 1 ? yuv->v : yuv->u ) : yuv->y;
00549         i_width = p_pic->p[i_plane].i_visible_pitch;
00550         i_dst_stride  = p_pic->p[i_plane].i_pitch;
00551         i_src_stride  = i_plane ? yuv->uv_stride : yuv->y_stride;
00552         i_src_xoffset = p_dec->p_sys->ti.offset_x;
00553         i_src_yoffset = p_dec->p_sys->ti.offset_y;
00554         if( i_plane )
00555         {
00556             i_src_xoffset /= 2;
00557             i_src_yoffset /= 2;
00558         }
00559 
00560         p_src += (i_src_yoffset * i_src_stride + i_src_yoffset);
00561 
00562         for( i_line = 0; i_line < p_pic->p[i_plane].i_visible_lines; i_line++ )
00563         {
00564             p_dec->p_vlc->pf_memcpy( p_dst, p_src + i_src_xoffset,
00565                                      i_plane ? yuv->uv_width : yuv->y_width );
00566             p_src += i_src_stride;
00567             p_dst += i_dst_stride;
00568         }
00569     }
00570 }
00571 
00572 /*****************************************************************************
00573  * encoder_sys_t : theora encoder descriptor
00574  *****************************************************************************/
00575 struct encoder_sys_t
00576 {
00577     /*
00578      * Input properties
00579      */
00580     vlc_bool_t b_headers;
00581 
00582     /*
00583      * Theora properties
00584      */
00585     theora_info      ti;                        /* theora bitstream settings */
00586     theora_comment   tc;                            /* theora comment header */
00587     theora_state     td;                   /* theora bitstream user comments */
00588 
00589     int i_width, i_height;
00590 };
00591 
00592 /*****************************************************************************
00593  * OpenEncoder: probe the encoder and return score
00594  *****************************************************************************/
00595 static int OpenEncoder( vlc_object_t *p_this )
00596 {
00597     encoder_t *p_enc = (encoder_t *)p_this;
00598     encoder_sys_t *p_sys = p_enc->p_sys;
00599     ogg_packet header;
00600     uint8_t *p_extra;
00601     vlc_value_t val;
00602     int i_quality, i;
00603 
00604     if( p_enc->fmt_out.i_codec != VLC_FOURCC('t','h','e','o') &&
00605         !p_enc->b_force )
00606     {
00607         return VLC_EGENERIC;
00608     }
00609 
00610     /* Allocate the memory needed to store the decoder's structure */
00611     if( ( p_sys = (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
00612     {
00613         msg_Err( p_enc, "out of memory" );
00614         return VLC_EGENERIC;
00615     }
00616     p_enc->p_sys = p_sys;
00617 
00618     p_enc->pf_encode_video = Encode;
00619     p_enc->fmt_in.i_codec = VLC_FOURCC('I','4','2','0');
00620     p_enc->fmt_out.i_codec = VLC_FOURCC('t','h','e','o');
00621 
00622     sout_CfgParse( p_enc, ENC_CFG_PREFIX, ppsz_enc_options, p_enc->p_cfg );
00623 
00624     var_Get( p_enc, ENC_CFG_PREFIX "quality", &val );
00625     i_quality = val.i_int;
00626     if( i_quality > 10 ) i_quality = 10;
00627     if( i_quality < 0 ) i_quality = 0;
00628 
00629     theora_info_init( &p_sys->ti );
00630 
00631     p_sys->ti.width = p_enc->fmt_in.video.i_width;
00632     p_sys->ti.height = p_enc->fmt_in.video.i_height;
00633 
00634     if( p_sys->ti.width % 16 || p_sys->ti.height % 16 )
00635     {
00636         /* Pictures from the transcoder should always have a pitch
00637          * which is a multiple of 16 */
00638         p_sys->ti.width = (p_sys->ti.width + 15) >> 4 << 4;
00639         p_sys->ti.height = (p_sys->ti.height + 15) >> 4 << 4;
00640 
00641         msg_Dbg( p_enc, "padding video from %dx%d to %dx%d",
00642                  p_enc->fmt_in.video.i_width, p_enc->fmt_in.video.i_height,
00643                  p_sys->ti.width, p_sys->ti.height );
00644     }
00645 
00646     p_sys->ti.frame_width = p_enc->fmt_in.video.i_width;
00647     p_sys->ti.frame_height = p_enc->fmt_in.video.i_height;
00648     p_sys->ti.offset_x = 0 /*frame_x_offset*/;
00649     p_sys->ti.offset_y = 0 /*frame_y_offset*/;
00650 
00651     p_sys->i_width = p_sys->ti.width;
00652     p_sys->i_height = p_sys->ti.height;
00653 
00654     if( !p_enc->fmt_in.video.i_frame_rate ||
00655         !p_enc->fmt_in.video.i_frame_rate_base )
00656     {
00657         p_sys->ti.fps_numerator = 25;
00658         p_sys->ti.fps_denominator = 1;
00659     }
00660     else
00661     {
00662         p_sys->ti.fps_numerator = p_enc->fmt_in.video.i_frame_rate;
00663         p_sys->ti.fps_denominator = p_enc->fmt_in.video.i_frame_rate_base;
00664     }
00665 
00666     if( p_enc->fmt_in.video.i_aspect )
00667     {
00668         uint64_t i_num, i_den;
00669         unsigned i_dst_num, i_dst_den;
00670 
00671         i_num = p_enc->fmt_in.video.i_aspect * (int64_t)p_sys->ti.height;
00672         i_den = VOUT_ASPECT_FACTOR * p_sys->ti.width;
00673         vlc_ureduce( &i_dst_num, &i_dst_den, i_num, i_den, 0 );
00674         p_sys->ti.aspect_numerator = i_dst_num;
00675         p_sys->ti.aspect_denominator = i_dst_den;
00676     }
00677     else
00678     {
00679         p_sys->ti.aspect_numerator = 4;
00680         p_sys->ti.aspect_denominator = 3;
00681     }
00682 
00683     p_sys->ti.target_bitrate = p_enc->fmt_out.i_bitrate;
00684     p_sys->ti.quality = ((float)i_quality) * 6.3;
00685 
00686     p_sys->ti.dropframes_p = 0;
00687     p_sys->ti.quick_p = 1;
00688     p_sys->ti.keyframe_auto_p = 1;
00689     p_sys->ti.keyframe_frequency = 64;
00690     p_sys->ti.keyframe_frequency_force = 64;
00691     p_sys->ti.keyframe_data_target_bitrate = p_enc->fmt_out.i_bitrate * 1.5;
00692     p_sys->ti.keyframe_auto_threshold = 80;
00693     p_sys->ti.keyframe_mindistance = 8;
00694     p_sys->ti.noise_sensitivity = 1;
00695 
00696     theora_encode_init( &p_sys->td, &p_sys->ti );
00697     theora_info_clear( &p_sys->ti );
00698     theora_comment_init( &p_sys->tc );
00699 
00700     /* Create and store headers */
00701     p_enc->fmt_out.i_extra = 3 * 2;
00702     for( i = 0; i < 3; i++ )
00703     {
00704         if( i == 0 ) theora_encode_header( &p_sys->td, &header );
00705         else if( i == 1 ) theora_encode_comment( &p_sys->tc, &header );
00706         else if( i == 2 ) theora_encode_tables( &p_sys->td, &header );
00707 
00708         p_enc->fmt_out.p_extra =
00709             realloc( p_enc->fmt_out.p_extra,
00710                      p_enc->fmt_out.i_extra + header.bytes );
00711         p_extra = p_enc->fmt_out.p_extra;
00712         p_extra += p_enc->fmt_out.i_extra + (i-3)*2;
00713         p_enc->fmt_out.i_extra += header.bytes;
00714 
00715         *(p_extra++) = header.bytes >> 8;
00716         *(p_extra++) = header.bytes & 0xFF;
00717 
00718         memcpy( p_extra, header.packet, header.bytes );
00719     }
00720 
00721     return VLC_SUCCESS;
00722 }
00723 
00724 /****************************************************************************
00725  * Encode: the whole thing
00726  ****************************************************************************
00727  * This function spits out ogg packets.
00728  ****************************************************************************/
00729 static block_t *Encode( encoder_t *p_enc, picture_t *p_pict )
00730 {
00731     encoder_sys_t *p_sys = p_enc->p_sys;
00732     ogg_packet oggpacket;
00733     block_t *p_block;
00734     yuv_buffer yuv;
00735     int i;
00736 
00737     /* Sanity check */
00738     if( p_pict->p[0].i_pitch < (int)p_sys->i_width ||
00739         p_pict->p[0].i_lines < (int)p_sys->i_height )
00740     {
00741         msg_Warn( p_enc, "frame is smaller than encoding size"
00742                   "(%ix%i->%ix%i) -> dropping frame",
00743                   p_pict->p[0].i_pitch, p_pict->p[0].i_lines,
00744                   p_sys->i_width, p_sys->i_height );
00745         return NULL;
00746     }
00747 
00748     /* Fill padding */
00749     if( p_pict->p[0].i_visible_pitch < (int)p_sys->i_width )
00750     {
00751         for( i = 0; i < p_sys->i_height; i++ )
00752         {
00753             memset( p_pict->p[0].p_pixels + i * p_pict->p[0].i_pitch +
00754                     p_pict->p[0].i_visible_pitch,
00755                     *( p_pict->p[0].p_pixels + i * p_pict->p[0].i_pitch +
00756                        p_pict->p[0].i_visible_pitch - 1 ),
00757                     p_sys->i_width - p_pict->p[0].i_visible_pitch );
00758         }
00759         for( i = 0; i < p_sys->i_height / 2; i++ )
00760         {
00761             memset( p_pict->p[1].p_pixels + i * p_pict->p[1].i_pitch +
00762                     p_pict->p[1].i_visible_pitch,
00763                     *( p_pict->p[1].p_pixels + i * p_pict->p[1].i_pitch +
00764                        p_pict->p[1].i_visible_pitch - 1 ),
00765                     p_sys->i_width / 2 - p_pict->p[1].i_visible_pitch );
00766             memset( p_pict->p[2].p_pixels + i * p_pict->p[2].i_pitch +
00767                     p_pict->p[2].i_visible_pitch,
00768                     *( p_pict->p[2].p_pixels + i * p_pict->p[2].i_pitch +
00769                        p_pict->p[2].i_visible_pitch - 1 ),
00770                     p_sys->i_width / 2 - p_pict->p[2].i_visible_pitch );
00771         }
00772     }
00773 
00774     if( p_pict->p[0].i_visible_lines < (int)p_sys->i_height )
00775     {
00776         for( i = p_pict->p[0].i_visible_lines; i < p_sys->i_height; i++ )
00777         {
00778             memset( p_pict->p[0].p_pixels + i * p_pict->p[0].i_pitch, 0,
00779                     p_sys->i_width );
00780         }
00781         for( i = p_pict->p[1].i_visible_lines; i < p_sys->i_height / 2; i++ )
00782         {
00783             memset( p_pict->p[1].p_pixels + i * p_pict->p[1].i_pitch, 0x80,
00784                     p_sys->i_width / 2 );
00785             memset( p_pict->p[2].p_pixels + i * p_pict->p[2].i_pitch, 0x80,
00786                     p_sys->i_width / 2 );
00787         }
00788     }
00789 
00790     /* Theora is a one-frame-in, one-frame-out system. Submit a frame
00791      * for compression and pull out the packet. */
00792 
00793     yuv.y_width  = p_sys->i_width;
00794     yuv.y_height = p_sys->i_height;
00795     yuv.y_stride = p_pict->p[0].i_pitch;
00796 
00797     yuv.uv_width  = p_sys->i_width / 2;
00798     yuv.uv_height = p_sys->i_height / 2;
00799     yuv.uv_stride = p_pict->p[1].i_pitch;
00800 
00801     yuv.y = p_pict->p[0].p_pixels;
00802     yuv.u = p_pict->p[1].p_pixels;
00803     yuv.v = p_pict->p[2].p_pixels;
00804 
00805     if( theora_encode_YUVin( &p_sys->td, &yuv ) < 0 )
00806     {
00807         msg_Warn( p_enc, "failed encoding a frame" );
00808         return NULL;
00809     }
00810 
00811     theora_encode_packetout( &p_sys->td, 0, &oggpacket );
00812 
00813     /* Ogg packet to block */
00814     p_block = block_New( p_enc, oggpacket.bytes );
00815     memcpy( p_block->p_buffer, oggpacket.packet, oggpacket.bytes );
00816     p_block->i_dts = p_block->i_pts = p_pict->date;
00817 
00818     return p_block;
00819 }
00820 
00821 /*****************************************************************************
00822  * CloseEncoder: theora encoder destruction
00823  *****************************************************************************/
00824 static void CloseEncoder( vlc_object_t *p_this )
00825 {
00826     encoder_t *p_enc = (encoder_t *)p_this;
00827     encoder_sys_t *p_sys = p_enc->p_sys;
00828 
00829     theora_info_clear( &p_sys->ti );
00830     theora_comment_clear( &p_sys->tc );
00831 
00832     free( p_sys );
00833 }

Generated on Tue Dec 20 10:14:30 2005 for vlc-0.8.4a by  doxygen 1.4.2