theora.c 31.4 KB
Newer Older
Gildas Bazin's avatar
 
Gildas Bazin committed
1 2 3
/*****************************************************************************
 * theora.c: theora decoder module making use of libtheora.
 *****************************************************************************
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
4
 * Copyright (C) 1999-2012 VLC authors and VideoLAN
5
 * $Id$
Gildas Bazin's avatar
 
Gildas Bazin committed
6
 *
7
 * Authors: Gildas Bazin <gbazin@videolan.org>
Gildas Bazin's avatar
 
Gildas Bazin committed
8
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
9 10 11
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
Gildas Bazin's avatar
 
Gildas Bazin committed
12
 * (at your option) any later version.
Sam Hocevar's avatar
Sam Hocevar committed
13
 *
Gildas Bazin's avatar
 
Gildas Bazin committed
14 15
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
16 17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
Gildas Bazin's avatar
 
Gildas Bazin committed
18
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
19 20 21
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
Gildas Bazin's avatar
 
Gildas Bazin committed
22 23 24 25 26
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
27 28 29 30
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

31
#include <vlc_common.h>
32
#include <vlc_plugin.h>
Clément Stenac's avatar
Clément Stenac committed
33 34
#include <vlc_codec.h>
#include <vlc_sout.h>
Clément Stenac's avatar
Clément Stenac committed
35
#include <vlc_input.h>
36 37
#include "../demux/xiph.h"

Gildas Bazin's avatar
 
Gildas Bazin committed
38 39
#include <ogg/ogg.h>

40 41 42 43 44 45
#include <theora/codec.h>
#include <theora/theoradec.h>
#include <theora/theoraenc.h>

#include <assert.h>
#include <limits.h>
Gildas Bazin's avatar
 
Gildas Bazin committed
46 47

/*****************************************************************************
Gildas Bazin's avatar
 
Gildas Bazin committed
48
 * decoder_sys_t : theora decoder descriptor
Gildas Bazin's avatar
 
Gildas Bazin committed
49
 *****************************************************************************/
Gildas Bazin's avatar
 
Gildas Bazin committed
50
struct decoder_sys_t
Gildas Bazin's avatar
 
Gildas Bazin committed
51
{
Gildas Bazin's avatar
 
Gildas Bazin committed
52
    /* Module mode */
53
    bool b_packetizer;
Gildas Bazin's avatar
 
Gildas Bazin committed
54

Gildas Bazin's avatar
 
Gildas Bazin committed
55
    /*
Gildas Bazin's avatar
 
Gildas Bazin committed
56
     * Input properties
Gildas Bazin's avatar
 
Gildas Bazin committed
57
     */
58
    bool b_has_headers;
Gildas Bazin's avatar
 
Gildas Bazin committed
59 60 61 62

    /*
     * Theora properties
     */
63 64 65
    th_info          ti;       /* theora bitstream settings */
    th_comment       tc;       /* theora comment information */
    th_dec_ctx       *tcx;     /* theora decoder context */
Gildas Bazin's avatar
 
Gildas Bazin committed
66

67 68 69
    /*
     * Decoding properties
     */
70
    bool b_decoded_first_keyframe;
71

Gildas Bazin's avatar
 
Gildas Bazin committed
72 73 74 75 76
    /*
     * Common properties
     */
    mtime_t i_pts;
};
Gildas Bazin's avatar
 
Gildas Bazin committed
77 78 79 80

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
Gildas Bazin's avatar
 
Gildas Bazin committed
81 82 83
static int  OpenDecoder   ( vlc_object_t * );
static int  OpenPacketizer( vlc_object_t * );
static void CloseDecoder  ( vlc_object_t * );
Gildas Bazin's avatar
 
Gildas Bazin committed
84

Gildas Bazin's avatar
 
Gildas Bazin committed
85
static void *DecodeBlock  ( decoder_t *, block_t ** );
86
static int  ProcessHeaders( decoder_t * );
Gildas Bazin's avatar
 
Gildas Bazin committed
87
static void *ProcessPacket ( decoder_t *, ogg_packet *, block_t ** );
88
static void Flush( decoder_t * );
Gildas Bazin's avatar
 
Gildas Bazin committed
89

Gildas Bazin's avatar
 
Gildas Bazin committed
90
static picture_t *DecodePacket( decoder_t *, ogg_packet * );
Gildas Bazin's avatar
 
Gildas Bazin committed
91

Gildas Bazin's avatar
 
Gildas Bazin committed
92
static void ParseTheoraComments( decoder_t * );
93
static void theora_CopyPicture( picture_t *, th_ycbcr_buffer );
94

95
#ifdef ENABLE_SOUT
Gildas Bazin's avatar
 
Gildas Bazin committed
96 97 98
static int  OpenEncoder( vlc_object_t *p_this );
static void CloseEncoder( vlc_object_t *p_this );
static block_t *Encode( encoder_t *p_enc, picture_t *p_pict );
99
#endif
Gildas Bazin's avatar
 
Gildas Bazin committed
100

Gildas Bazin's avatar
 
Gildas Bazin committed
101 102 103
/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
104 105
#define ENC_QUALITY_TEXT N_("Encoding quality")
#define ENC_QUALITY_LONGTEXT N_( \
106
  "Enforce a quality between 1 (low) and 10 (high), instead " \
107 108
  "of specifying a particular bitrate. This will produce a VBR stream." )

109 110
#define ENC_POSTPROCESS_TEXT N_("Post processing quality")

111 112 113 114 115 116 117 118
vlc_module_begin ()
    set_category( CAT_INPUT )
    set_subcategory( SUBCAT_INPUT_VCODEC )
    set_shortname( "Theora" )
    set_description( N_("Theora video decoder") )
    set_capability( "decoder", 100 )
    set_callbacks( OpenDecoder, CloseDecoder )
    add_shortcut( "theora" )
119 120
#   define DEC_CFG_PREFIX "theora-"
    add_integer( DEC_CFG_PREFIX "postproc", -1, ENC_POSTPROCESS_TEXT, NULL, true )
121 122 123 124 125

    add_submodule ()
    set_description( N_("Theora video packetizer") )
    set_capability( "packetizer", 100 )
    set_callbacks( OpenPacketizer, CloseDecoder )
126
    add_shortcut( "theora" )
127

128
#ifdef ENABLE_SOUT
129 130 131 132
    add_submodule ()
    set_description( N_("Theora video encoder") )
    set_capability( "encoder", 150 )
    set_callbacks( OpenEncoder, CloseEncoder )
133
    add_shortcut( "theora" )
134 135

#   define ENC_CFG_PREFIX "sout-theora-"
136
    add_integer( ENC_CFG_PREFIX "quality", 2, ENC_QUALITY_TEXT,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
137
                 ENC_QUALITY_LONGTEXT, false )
138
#endif
139
vlc_module_end ()
Gildas Bazin's avatar
 
Gildas Bazin committed
140

141
static const char *const ppsz_enc_options[] = {
142 143 144
    "quality", NULL
};

Gildas Bazin's avatar
 
Gildas Bazin committed
145 146 147 148 149
/*****************************************************************************
 * OpenDecoder: probe the decoder and return score
 *****************************************************************************/
static int OpenDecoder( vlc_object_t *p_this )
{
Gildas Bazin's avatar
 
Gildas Bazin committed
150
    decoder_t *p_dec = (decoder_t*)p_this;
Gildas Bazin's avatar
 
Gildas Bazin committed
151
    decoder_sys_t *p_sys;
Gildas Bazin's avatar
 
Gildas Bazin committed
152

153
    if( p_dec->fmt_in.i_codec != VLC_CODEC_THEORA )
Gildas Bazin's avatar
 
Gildas Bazin committed
154 155 156 157
    {
        return VLC_EGENERIC;
    }

Gildas Bazin's avatar
 
Gildas Bazin committed
158
    /* Allocate the memory needed to store the decoder's structure */
159
    if( ( p_dec->p_sys = p_sys = malloc(sizeof(*p_sys)) ) == NULL )
160
        return VLC_ENOMEM;
161
    p_dec->p_sys->b_packetizer = false;
162
    p_sys->b_has_headers = false;
163
    p_sys->i_pts = VLC_TS_INVALID;
164
    p_sys->b_decoded_first_keyframe = false;
165
    p_sys->tcx = NULL;
Gildas Bazin's avatar
 
Gildas Bazin committed
166 167 168

    /* Set output properties */
    p_dec->fmt_out.i_cat = VIDEO_ES;
169
    p_dec->fmt_out.i_codec = VLC_CODEC_I420;
Gildas Bazin's avatar
 
Gildas Bazin committed
170 171 172 173 174 175

    /* Set callbacks */
    p_dec->pf_decode_video = (picture_t *(*)(decoder_t *, block_t **))
        DecodeBlock;
    p_dec->pf_packetize    = (block_t *(*)(decoder_t *, block_t **))
        DecodeBlock;
176
    p_dec->pf_flush        = Flush;
Gildas Bazin's avatar
 
Gildas Bazin committed
177 178

    /* Init supporting Theora structures needed in header parsing */
179 180
    th_comment_init( &p_sys->tc );
    th_info_init( &p_sys->ti );
Gildas Bazin's avatar
 
Gildas Bazin committed
181

Gildas Bazin's avatar
 
Gildas Bazin committed
182 183
    return VLC_SUCCESS;
}
Gildas Bazin's avatar
 
Gildas Bazin committed
184 185 186 187 188 189 190

static int OpenPacketizer( vlc_object_t *p_this )
{
    decoder_t *p_dec = (decoder_t*)p_this;

    int i_ret = OpenDecoder( p_this );

Gildas Bazin's avatar
 
Gildas Bazin committed
191 192
    if( i_ret == VLC_SUCCESS )
    {
193
        p_dec->p_sys->b_packetizer = true;
194
        p_dec->fmt_out.i_codec = VLC_CODEC_THEORA;
Gildas Bazin's avatar
 
Gildas Bazin committed
195
    }
Gildas Bazin's avatar
 
Gildas Bazin committed
196 197 198 199 200

    return i_ret;
}

/****************************************************************************
Gildas Bazin's avatar
 
Gildas Bazin committed
201
 * DecodeBlock: the whole thing
Gildas Bazin's avatar
 
Gildas Bazin committed
202 203 204
 ****************************************************************************
 * This function must be fed with ogg packets.
 ****************************************************************************/
Gildas Bazin's avatar
 
Gildas Bazin committed
205
static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
Gildas Bazin's avatar
 
Gildas Bazin committed
206 207
{
    decoder_sys_t *p_sys = p_dec->p_sys;
Gildas Bazin's avatar
 
Gildas Bazin committed
208
    block_t *p_block;
Gildas Bazin's avatar
 
Gildas Bazin committed
209
    ogg_packet oggpacket;
Gildas Bazin's avatar
 
Gildas Bazin committed
210 211 212 213

    if( !pp_block || !*pp_block ) return NULL;

    p_block = *pp_block;
Gildas Bazin's avatar
 
Gildas Bazin committed
214

Gildas Bazin's avatar
 
Gildas Bazin committed
215 216 217 218 219 220 221
    /* Block to Ogg packet */
    oggpacket.packet = p_block->p_buffer;
    oggpacket.bytes = p_block->i_buffer;
    oggpacket.granulepos = p_block->i_dts;
    oggpacket.b_o_s = 0;
    oggpacket.e_o_s = 0;
    oggpacket.packetno = 0;
Gildas Bazin's avatar
 
Gildas Bazin committed
222

223
    /* Check for headers */
224
    if( !p_sys->b_has_headers )
225
    {
226
        if( ProcessHeaders( p_dec ) )
Gildas Bazin's avatar
 
Gildas Bazin committed
227
        {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
228
            block_Release( p_block );
Gildas Bazin's avatar
 
Gildas Bazin committed
229
            return NULL;
Gildas Bazin's avatar
 
Gildas Bazin committed
230
        }
231
        p_sys->b_has_headers = true;
232
    }
Gildas Bazin's avatar
 
Gildas Bazin committed
233

234 235
    return ProcessPacket( p_dec, &oggpacket, pp_block );
}
Sam Hocevar's avatar
Sam Hocevar committed
236

237 238 239 240 241 242 243
/*****************************************************************************
 * ProcessHeaders: process Theora headers.
 *****************************************************************************/
static int ProcessHeaders( decoder_t *p_dec )
{
    decoder_sys_t *p_sys = p_dec->p_sys;
    ogg_packet oggpacket;
244
    th_setup_info *ts = NULL; /* theora setup information */
245
    int i_max_pp, i_pp;
Gildas Bazin's avatar
 
Gildas Bazin committed
246

247 248 249 250 251 252 253
    unsigned pi_size[XIPH_MAX_HEADER_COUNT];
    void     *pp_data[XIPH_MAX_HEADER_COUNT];
    unsigned i_count;
    if( xiph_SplitHeaders( pi_size, pp_data, &i_count,
                           p_dec->fmt_in.i_extra, p_dec->fmt_in.p_extra) )
        return VLC_EGENERIC;
    if( i_count < 3 )
254
        return VLC_EGENERIC;
Gildas Bazin's avatar
 
Gildas Bazin committed
255

256 257 258 259 260
    oggpacket.granulepos = -1;
    oggpacket.e_o_s = 0;
    oggpacket.packetno = 0;

    /* Take care of the initial Vorbis header */
261 262 263
    oggpacket.b_o_s  = 1; /* yes this actually is a b_o_s packet :) */
    oggpacket.bytes  = pi_size[0];
    oggpacket.packet = pp_data[0];
264
    if( th_decode_headerin( &p_sys->ti, &p_sys->tc, &ts, &oggpacket ) < 0 )
Gildas Bazin's avatar
 
Gildas Bazin committed
265
    {
266
        msg_Err( p_dec, "this bitstream does not contain Theora video data" );
267
        goto error;
268
    }
Sam Hocevar's avatar
Sam Hocevar committed
269

270
    /* Set output properties */
271
    if( !p_sys->b_packetizer )
272 273

    switch( p_sys->ti.pixel_fmt )
274
    {
275
      case TH_PF_420:
276
        p_dec->fmt_out.i_codec = VLC_CODEC_I420;
277
        break;
278
      case TH_PF_422:
279
        p_dec->fmt_out.i_codec = VLC_CODEC_I422;
280
        break;
281
      case TH_PF_444:
282
        p_dec->fmt_out.i_codec = VLC_CODEC_I444;
283
        break;
284
      case TH_PF_RSVD:
285 286 287 288
      default:
        msg_Err( p_dec, "unknown chroma in theora sample" );
        break;
    }
289 290 291 292

    p_dec->fmt_out.video.i_width = p_sys->ti.frame_width;
    p_dec->fmt_out.video.i_height = p_sys->ti.frame_height;
    if( p_sys->ti.pic_width && p_sys->ti.pic_height )
293
    {
294 295 296 297
        p_dec->fmt_out.video.i_visible_width = p_sys->ti.pic_width;
        p_dec->fmt_out.video.i_visible_height = p_sys->ti.pic_height;

        if( p_sys->ti.pic_x || p_sys->ti.pic_y )
298
        {
299 300
            p_dec->fmt_out.video.i_x_offset = p_sys->ti.pic_x;
            p_dec->fmt_out.video.i_y_offset = p_sys->ti.pic_y;
301
        }
302
    }
Gildas Bazin's avatar
 
Gildas Bazin committed
303

304 305
    if( p_sys->ti.aspect_denominator && p_sys->ti.aspect_numerator )
    {
306 307
        p_dec->fmt_out.video.i_sar_num = p_sys->ti.aspect_numerator;
        p_dec->fmt_out.video.i_sar_den = p_sys->ti.aspect_denominator;
Gildas Bazin's avatar
 
Gildas Bazin committed
308
    }
309 310
    else
    {
311 312
        p_dec->fmt_out.video.i_sar_num = 1;
        p_dec->fmt_out.video.i_sar_den = 1;
313 314
    }

315 316 317 318 319 320
    if( p_sys->ti.fps_numerator > 0 && p_sys->ti.fps_denominator > 0 )
    {
        p_dec->fmt_out.video.i_frame_rate = p_sys->ti.fps_numerator;
        p_dec->fmt_out.video.i_frame_rate_base = p_sys->ti.fps_denominator;
    }

321 322 323
    msg_Dbg( p_dec, "%dx%d %.02f fps video, frame content "
             "is %dx%d with offset (%d,%d)",
             p_sys->ti.frame_width, p_sys->ti.frame_height,
324 325 326 327 328 329 330 331 332 333 334
             (double)p_sys->ti.fps_numerator/p_sys->ti.fps_denominator,
             p_sys->ti.pic_width, p_sys->ti.pic_height,
             p_sys->ti.pic_x, p_sys->ti.pic_y );

    /* Some assertions based on the documentation.  These are mandatory restrictions. */
    assert( p_sys->ti.frame_height % 16 == 0 && p_sys->ti.frame_height < 1048576 );
    assert( p_sys->ti.frame_width % 16 == 0 && p_sys->ti.frame_width < 1048576 );
    assert( p_sys->ti.keyframe_granule_shift >= 0 && p_sys->ti.keyframe_granule_shift <= 31 );
    assert( p_sys->ti.pic_x <= __MIN( p_sys->ti.frame_width - p_sys->ti.pic_width, 255 ) );
    assert( p_sys->ti.pic_y <= p_sys->ti.frame_height - p_sys->ti.pic_height);
    assert( p_sys->ti.frame_height - p_sys->ti.pic_height - p_sys->ti.pic_y <= 255 );
Gildas Bazin's avatar
 
Gildas Bazin committed
335

336
    /* Sanity check that seems necessary for some corrupted files */
337 338
    if( p_sys->ti.frame_width < p_sys->ti.pic_width ||
        p_sys->ti.frame_height < p_sys->ti.pic_height )
339 340 341
    {
        msg_Warn( p_dec, "trying to correct invalid theora header "
                  "(frame size (%dx%d) is smaller than frame content (%d,%d))",
342 343
                  p_sys->ti.frame_width, p_sys->ti.frame_height,
                  p_sys->ti.pic_width, p_sys->ti.pic_height );
344

345 346 347 348
        if( p_sys->ti.frame_width < p_sys->ti.pic_width )
          p_sys->ti.frame_width = p_sys->ti.pic_width;
        if( p_sys->ti.frame_height < p_sys->ti.pic_height )
            p_sys->ti.frame_height = p_sys->ti.pic_height;
349 350
    }

351
    /* The next packet in order is the comments header */
352 353 354
    oggpacket.b_o_s  = 0;
    oggpacket.bytes  = pi_size[1];
    oggpacket.packet = pp_data[1];
355 356

    if( th_decode_headerin( &p_sys->ti, &p_sys->tc, &ts, &oggpacket ) < 0 )
357 358
    {
        msg_Err( p_dec, "2nd Theora header is corrupted" );
359
        goto error;
360 361 362
    }

    ParseTheoraComments( p_dec );
Gildas Bazin's avatar
 
Gildas Bazin committed
363

364 365 366
    /* The next packet in order is the codebooks header
     * We need to watch out that this packet is not missing as a
     * missing or corrupted header is fatal. */
367 368 369
    oggpacket.b_o_s  = 0;
    oggpacket.bytes  = pi_size[2];
    oggpacket.packet = pp_data[2];
370
    if( th_decode_headerin( &p_sys->ti, &p_sys->tc, &ts, &oggpacket ) < 0 )
371 372
    {
        msg_Err( p_dec, "3rd Theora header is corrupted" );
373
        goto error;
374 375 376 377 378
    }

    if( !p_sys->b_packetizer )
    {
        /* We have all the headers, initialize decoder */
379 380 381 382 383
        if ( ( p_sys->tcx = th_decode_alloc( &p_sys->ti, ts ) ) == NULL )
        {
            msg_Err( p_dec, "Could not allocate Theora decoder" );
            goto error;
        }
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398

        i_pp = var_InheritInteger( p_dec, DEC_CFG_PREFIX "postproc" );
        if ( i_pp >= 0 && !th_decode_ctl( p_sys->tcx,
                    TH_DECCTL_GET_PPLEVEL_MAX, &i_max_pp, sizeof(int) ) )
        {
            i_pp = __MIN( i_pp, i_max_pp );
            if ( th_decode_ctl( p_sys->tcx, TH_DECCTL_SET_PPLEVEL,
                                &i_pp, sizeof(int) ) )
                msg_Err( p_dec, "Failed to set post processing level to %d",
                         i_pp );
            else
                msg_Dbg( p_dec, "Set post processing level to %d / %d",
                         i_pp, i_max_pp );
        }

399 400 401
    }
    else
    {
402 403 404 405 406 407 408 409 410
        void* p_extra = realloc( p_dec->fmt_out.p_extra,
                                 p_dec->fmt_in.i_extra );
        if( unlikely( p_extra == NULL ) )
        {
            /* Clean up the decoder setup info... we're done with it */
            th_setup_free( ts );
            return VLC_ENOMEM;
        }
        p_dec->fmt_out.p_extra = p_extra;
411 412 413 414 415
        p_dec->fmt_out.i_extra = p_dec->fmt_in.i_extra;
        memcpy( p_dec->fmt_out.p_extra,
                p_dec->fmt_in.p_extra, p_dec->fmt_out.i_extra );
    }

416 417
    /* Clean up the decoder setup info... we're done with it */
    th_setup_free( ts );
418
    return VLC_SUCCESS;
419 420

error:
421 422
    /* Clean up the decoder setup info... we're done with it */
    th_setup_free( ts );
423
    return VLC_EGENERIC;
Gildas Bazin's avatar
 
Gildas Bazin committed
424
}
Gildas Bazin's avatar
 
Gildas Bazin committed
425

426 427 428 429 430 431 432 433 434 435
/*****************************************************************************
 * Flush:
 *****************************************************************************/
static void Flush( decoder_t *p_dec )
{
    decoder_sys_t *p_sys = p_dec->p_sys;

    p_sys->i_pts = VLC_TS_INVALID;
}

Gildas Bazin's avatar
 
Gildas Bazin committed
436
/*****************************************************************************
Gildas Bazin's avatar
 
Gildas Bazin committed
437
 * ProcessPacket: processes a theora packet.
Gildas Bazin's avatar
 
Gildas Bazin committed
438
 *****************************************************************************/
Gildas Bazin's avatar
 
Gildas Bazin committed
439 440
static void *ProcessPacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
                            block_t **pp_block )
Gildas Bazin's avatar
 
Gildas Bazin committed
441 442
{
    decoder_sys_t *p_sys = p_dec->p_sys;
Gildas Bazin's avatar
 
Gildas Bazin committed
443 444
    block_t *p_block = *pp_block;
    void *p_buf;
Gildas Bazin's avatar
 
Gildas Bazin committed
445

446 447 448 449 450 451 452 453 454
    *pp_block = NULL; /* To avoid being fed the same packet again */

    if( !p_block )
        return NULL;

    if( ( p_block->i_flags & BLOCK_FLAG_DISCONTINUITY ) != 0 )
        p_sys->i_pts = p_block->i_pts;

    if( ( p_block->i_flags & BLOCK_FLAG_CORRUPTED ) != 0 )
455
    {
456
        /* Don't send the a corrupted packet to
457 458
         * theora_decode, otherwise we get purple/green display artifacts
         * appearing in the video output */
459
        block_Release(p_block);
460 461 462
        return NULL;
    }

Gildas Bazin's avatar
 
Gildas Bazin committed
463
    /* Date management */
464
    if( p_block->i_pts > VLC_TS_INVALID && p_block->i_pts != p_sys->i_pts )
Gildas Bazin's avatar
 
Gildas Bazin committed
465
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
466
        p_sys->i_pts = p_block->i_pts;
Gildas Bazin's avatar
 
Gildas Bazin committed
467 468
    }

Gildas Bazin's avatar
 
Gildas Bazin committed
469 470
    if( p_sys->b_packetizer )
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
471 472 473
        /* Date management */
        p_block->i_dts = p_block->i_pts = p_sys->i_pts;

474
        p_block->i_length = p_sys->i_pts - p_block->i_pts;
Gildas Bazin's avatar
 
Gildas Bazin committed
475 476

        p_buf = p_block;
Gildas Bazin's avatar
 
Gildas Bazin committed
477 478 479
    }
    else
    {
480
        p_buf = DecodePacket( p_dec, p_oggpacket );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
481
        block_Release( p_block );
Gildas Bazin's avatar
 
Gildas Bazin committed
482
    }
Gildas Bazin's avatar
 
Gildas Bazin committed
483 484

    /* Date management */
Tristan Matthews's avatar
Tristan Matthews committed
485
    p_sys->i_pts += ( CLOCK_FREQ * p_sys->ti.fps_denominator /
Gildas Bazin's avatar
 
Gildas Bazin committed
486
                      p_sys->ti.fps_numerator ); /* 1 frame per packet */
Gildas Bazin's avatar
 
Gildas Bazin committed
487

Gildas Bazin's avatar
 
Gildas Bazin committed
488
    return p_buf;
Gildas Bazin's avatar
 
Gildas Bazin committed
489 490 491
}

/*****************************************************************************
Gildas Bazin's avatar
 
Gildas Bazin committed
492
 * DecodePacket: decodes a Theora packet.
Gildas Bazin's avatar
 
Gildas Bazin committed
493
 *****************************************************************************/
Gildas Bazin's avatar
 
Gildas Bazin committed
494
static picture_t *DecodePacket( decoder_t *p_dec, ogg_packet *p_oggpacket )
Gildas Bazin's avatar
 
Gildas Bazin committed
495
{
Gildas Bazin's avatar
 
Gildas Bazin committed
496
    decoder_sys_t *p_sys = p_dec->p_sys;
Gildas Bazin's avatar
 
Gildas Bazin committed
497
    picture_t *p_pic;
498
    th_ycbcr_buffer ycbcr;
Gildas Bazin's avatar
 
Gildas Bazin committed
499

500 501
    /* TODO: Implement _granpos (3rd parameter here) and add the
     * call to TH_DECCTL_SET_GRANDPOS after seek */
.'s avatar
. committed
502 503 504
    /* TODO: If the return is TH_DUPFRAME, we don't need to display a new
     * frame, but we do need to keep displaying the previous one. */
    if (th_decode_packetin( p_sys->tcx, p_oggpacket, NULL ) < 0)
505
        return NULL; /* bad packet */
Gildas Bazin's avatar
 
Gildas Bazin committed
506

507 508 509
    /* Check for keyframe */
    if( !(p_oggpacket->packet[0] & 0x80) /* data packet */ &&
        !(p_oggpacket->packet[0] & 0x40) /* intra frame */ )
510
        p_sys->b_decoded_first_keyframe = true;
511 512 513 514 515 516

    /* If we haven't seen a single keyframe yet, don't let Theora decode
     * anything, otherwise we'll get display artifacts.  (This is impossible
     * in the general case, but can happen if e.g. we play a network stream
     * using a timed URL, such that the server doesn't start the video with a
     * keyframe). */
517 518 519 520
    if( !p_sys->b_decoded_first_keyframe )
        return NULL; /* Wait until we've decoded the first keyframe */

    if( th_decode_ycbcr_out( p_sys->tcx, ycbcr ) ) /* returns 0 on success */
521
        return NULL;
Gildas Bazin's avatar
 
Gildas Bazin committed
522

Gildas Bazin's avatar
 
Gildas Bazin committed
523
    /* Get a new picture */
524
    p_pic = decoder_NewPicture( p_dec );
Gildas Bazin's avatar
 
Gildas Bazin committed
525
    if( !p_pic ) return NULL;
Gildas Bazin's avatar
 
Gildas Bazin committed
526

527
    theora_CopyPicture( p_pic, ycbcr );
Gildas Bazin's avatar
 
Gildas Bazin committed
528

Gildas Bazin's avatar
 
Gildas Bazin committed
529
    p_pic->date = p_sys->i_pts;
530
    p_pic->b_progressive = true;
Gildas Bazin's avatar
 
Gildas Bazin committed
531

Gildas Bazin's avatar
 
Gildas Bazin committed
532
    return p_pic;
Gildas Bazin's avatar
 
Gildas Bazin committed
533 534 535
}

/*****************************************************************************
536
 * ParseTheoraComments:
Gildas Bazin's avatar
 
Gildas Bazin committed
537
 *****************************************************************************/
Gildas Bazin's avatar
 
Gildas Bazin committed
538
static void ParseTheoraComments( decoder_t *p_dec )
Gildas Bazin's avatar
 
Gildas Bazin committed
539
{
Gildas Bazin's avatar
 
Gildas Bazin committed
540
    char *psz_name, *psz_value, *psz_comment;
541
    int i = 0;
542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
    /* Regarding the th_comment structure: */

    /* The metadata is stored as a series of (tag, value) pairs, in
       length-encoded string vectors. The first occurrence of the '='
       character delimits the tag and value. A particular tag may
       occur more than once, and order is significant. The character
       set encoding for the strings is always UTF-8, but the tag names
       are limited to ASCII, and treated as case-insensitive. See the
       Theora specification, Section 6.3.3 for details. */

    /* In filling in this structure, th_decode_headerin() will
       null-terminate the user_comment strings for safety. However,
       the bitstream format itself treats them as 8-bit clean vectors,
       possibly containing null characters, and so the length array
       should be treated as their authoritative length. */
Gildas Bazin's avatar
 
Gildas Bazin committed
557
    while ( i < p_dec->p_sys->tc.comments )
Gildas Bazin's avatar
 
Gildas Bazin committed
558
    {
559 560 561
        int clen = p_dec->p_sys->tc.comment_lengths[i];
        if ( clen <= 0 || clen >= INT_MAX ) { i++; continue; }
        psz_comment = (char *)malloc( clen + 1 );
Gildas Bazin's avatar
 
Gildas Bazin committed
562 563
        if( !psz_comment )
            break;
564
        memcpy( (void*)psz_comment, (void*)p_dec->p_sys->tc.user_comments[i], clen + 1 );
Gildas Bazin's avatar
 
Gildas Bazin committed
565 566 567 568 569 570
        psz_name = psz_comment;
        psz_value = strchr( psz_comment, '=' );
        if( psz_value )
        {
            *psz_value = '\0';
            psz_value++;
571 572 573

            if( !p_dec->p_description )
                p_dec->p_description = vlc_meta_New();
574 575
            /* TODO:  Since psz_value can contain NULLs see if there is an
             * instance where we need to preserve the full length of this string */
576 577
            if( p_dec->p_description )
                vlc_meta_AddExtra( p_dec->p_description, psz_name, psz_value );
Gildas Bazin's avatar
 
Gildas Bazin committed
578 579 580 581 582 583 584
        }
        free( psz_comment );
        i++;
    }
}

/*****************************************************************************
Gildas Bazin's avatar
 
Gildas Bazin committed
585
 * CloseDecoder: theora decoder destruction
Gildas Bazin's avatar
 
Gildas Bazin committed
586
 *****************************************************************************/
Gildas Bazin's avatar
 
Gildas Bazin committed
587
static void CloseDecoder( vlc_object_t *p_this )
Gildas Bazin's avatar
 
Gildas Bazin committed
588
{
Gildas Bazin's avatar
 
Gildas Bazin committed
589
    decoder_t *p_dec = (decoder_t *)p_this;
Gildas Bazin's avatar
 
Gildas Bazin committed
590
    decoder_sys_t *p_sys = p_dec->p_sys;
Gildas Bazin's avatar
 
Gildas Bazin committed
591

592 593 594
    th_info_clear(&p_sys->ti);
    th_comment_clear(&p_sys->tc);
    th_decode_free(p_sys->tcx);
Gildas Bazin's avatar
 
Gildas Bazin committed
595
    free( p_sys );
Gildas Bazin's avatar
 
Gildas Bazin committed
596 597 598 599 600 601
}

/*****************************************************************************
 * theora_CopyPicture: copy a picture from theora internal buffers to a
 *                     picture_t structure.
 *****************************************************************************/
602
static void theora_CopyPicture( picture_t *p_pic,
603
                                th_ycbcr_buffer ycbcr )
Gildas Bazin's avatar
 
Gildas Bazin committed
604
{
605
    int i_plane, i_planes, i_line, i_dst_stride, i_src_stride;
Sam Hocevar's avatar
Sam Hocevar committed
606
    uint8_t *p_dst, *p_src;
607 608 609 610 611 612 613 614 615
    /* th_img_plane
       int  width   The width of this plane.
       int  height  The height of this plane.
       int  stride  The offset in bytes between successive rows.
       unsigned char *data  A pointer to the beginning of the first row.

       Detailed Description

       A buffer for a single color plane in an uncompressed image.
Gildas Bazin's avatar
 
Gildas Bazin committed
616

617 618 619 620 621 622 623 624 625 626 627 628 629
       This contains the image data in a left-to-right, top-down
       format. Each row of pixels is stored contiguously in memory,
       but successive rows need not be. Use stride to compute the
       offset of the next row. The encoder accepts both positive
       stride values (top-down in memory) and negative (bottom-up in
       memory). The decoder currently always generates images with
       positive strides.

       typedef th_img_plane th_ycbcr_buffer[3]
    */

    i_planes = p_pic->i_planes < 3 ? p_pic->i_planes : 3;
    for( i_plane = 0; i_plane < i_planes; i_plane++ )
Gildas Bazin's avatar
 
Gildas Bazin committed
630 631
    {
        p_dst = p_pic->p[i_plane].p_pixels;
632
        p_src = ycbcr[i_plane].data;
Gildas Bazin's avatar
 
Gildas Bazin committed
633
        i_dst_stride  = p_pic->p[i_plane].i_pitch;
634 635 636 637
        i_src_stride  = ycbcr[i_plane].stride;
        for( i_line = 0;
             i_line < __MIN(p_pic->p[i_plane].i_lines, ycbcr[i_plane].height);
             i_line++ )
Gildas Bazin's avatar
 
Gildas Bazin committed
638
        {
Rafaël Carré's avatar
Rafaël Carré committed
639
            memcpy( p_dst, p_src, ycbcr[i_plane].width );
Gildas Bazin's avatar
 
Gildas Bazin committed
640 641 642 643 644
            p_src += i_src_stride;
            p_dst += i_dst_stride;
        }
    }
}
Gildas Bazin's avatar
 
Gildas Bazin committed
645

646
#ifdef ENABLE_SOUT
Gildas Bazin's avatar
 
Gildas Bazin committed
647 648 649 650 651 652 653 654
/*****************************************************************************
 * encoder_sys_t : theora encoder descriptor
 *****************************************************************************/
struct encoder_sys_t
{
    /*
     * Input properties
     */
655
    bool b_headers;
Gildas Bazin's avatar
 
Gildas Bazin committed
656 657 658 659

    /*
     * Theora properties
     */
660 661 662
    th_info      ti;                     /* theora bitstream settings */
    th_comment   tc;                     /* theora comment header */
    th_enc_ctx   *tcx;                   /* theora context */
Gildas Bazin's avatar
 
Gildas Bazin committed
663 664 665 666 667 668 669 670
};

/*****************************************************************************
 * OpenEncoder: probe the encoder and return score
 *****************************************************************************/
static int OpenEncoder( vlc_object_t *p_this )
{
    encoder_t *p_enc = (encoder_t *)p_this;
671
    encoder_sys_t *p_sys;
Laurent Aimar's avatar
Laurent Aimar committed
672
    int i_quality;
673 674 675 676 677
    int t_flags;
    int max_enc_level = 0;
    int keyframe_freq_force = 64;
    ogg_packet header;
    int status;
Gildas Bazin's avatar
 
Gildas Bazin committed
678

679
    if( p_enc->fmt_out.i_codec != VLC_CODEC_THEORA &&
680
        !p_enc->obj.force )
Gildas Bazin's avatar
 
Gildas Bazin committed
681 682 683 684
    {
        return VLC_EGENERIC;
    }

Tristan Matthews's avatar
Tristan Matthews committed
685
    /* Allocate the memory needed to store the encoder's structure */
686
    if( ( p_sys = malloc(sizeof(encoder_sys_t)) ) == NULL )
687
        return VLC_ENOMEM;
Gildas Bazin's avatar
 
Gildas Bazin committed
688 689 690
    p_enc->p_sys = p_sys;

    p_enc->pf_encode_video = Encode;
691 692
    p_enc->fmt_in.i_codec = VLC_CODEC_I420;
    p_enc->fmt_out.i_codec = VLC_CODEC_THEORA;
Gildas Bazin's avatar
 
Gildas Bazin committed
693

694
    config_ChainParse( p_enc, ENC_CFG_PREFIX, ppsz_enc_options, p_enc->p_cfg );
695

696
    i_quality = var_GetInteger( p_enc, ENC_CFG_PREFIX "quality" );
697 698 699
    if( i_quality > 10 ) i_quality = 10;
    if( i_quality < 0 ) i_quality = 0;

700
    th_info_init( &p_sys->ti );
Gildas Bazin's avatar
 
Gildas Bazin committed
701

702 703
    p_sys->ti.frame_width = p_enc->fmt_in.video.i_visible_width;
    p_sys->ti.frame_height = p_enc->fmt_in.video.i_visible_height;
704

705
    if( p_sys->ti.frame_width % 16 || p_sys->ti.frame_height % 16 )
706 707 708
    {
        /* Pictures from the transcoder should always have a pitch
         * which is a multiple of 16 */
709 710
        p_sys->ti.frame_width = (p_sys->ti.frame_width + 15) >> 4 << 4;
        p_sys->ti.frame_height = (p_sys->ti.frame_height + 15) >> 4 << 4;
711 712

        msg_Dbg( p_enc, "padding video from %dx%d to %dx%d",
713
                 p_enc->fmt_in.video.i_visible_width, p_enc->fmt_in.video.i_visible_height,
714
                 p_sys->ti.frame_width, p_sys->ti.frame_height );
715 716
    }

717 718
    p_sys->ti.pic_width = p_enc->fmt_in.video.i_visible_width;
    p_sys->ti.pic_height = p_enc->fmt_in.video.i_visible_height;
719 720
    p_sys->ti.pic_x = 0 /*frame_x_offset*/;
    p_sys->ti.pic_y = 0 /*frame_y_offset*/;
721 722 723 724 725 726 727 728 729 730 731 732

    if( !p_enc->fmt_in.video.i_frame_rate ||
        !p_enc->fmt_in.video.i_frame_rate_base )
    {
        p_sys->ti.fps_numerator = 25;
        p_sys->ti.fps_denominator = 1;
    }
    else
    {
        p_sys->ti.fps_numerator = p_enc->fmt_in.video.i_frame_rate;
        p_sys->ti.fps_denominator = p_enc->fmt_in.video.i_frame_rate_base;
    }
Gildas Bazin's avatar
 
Gildas Bazin committed
733

734
    if( p_enc->fmt_in.video.i_sar_num > 0 && p_enc->fmt_in.video.i_sar_den > 0 )
Gildas Bazin's avatar
 
Gildas Bazin committed
735
    {
736
        unsigned i_dst_num, i_dst_den;
737 738 739
        vlc_ureduce( &i_dst_num, &i_dst_den,
                     p_enc->fmt_in.video.i_sar_num,
                     p_enc->fmt_in.video.i_sar_den, 0 );
740 741
        p_sys->ti.aspect_numerator = i_dst_num;
        p_sys->ti.aspect_denominator = i_dst_den;
Gildas Bazin's avatar
 
Gildas Bazin committed
742 743 744 745 746 747 748
    }
    else
    {
        p_sys->ti.aspect_numerator = 4;
        p_sys->ti.aspect_denominator = 3;
    }

Gildas Bazin's avatar
 
Gildas Bazin committed
749
    p_sys->ti.target_bitrate = p_enc->fmt_out.i_bitrate;
750
    p_sys->ti.quality = ((float)i_quality) * 6.3f;
Gildas Bazin's avatar
 
Gildas Bazin committed
751

752 753 754 755 756 757

    p_sys->tcx = th_encode_alloc( &p_sys->ti );
    th_comment_init( &p_sys->tc );

    /* These are no longer supported here: */
    /*
Gildas Bazin's avatar
 
Gildas Bazin committed
758 759 760 761 762
    p_sys->ti.dropframes_p = 0;
    p_sys->ti.quick_p = 1;
    p_sys->ti.keyframe_auto_p = 1;
    p_sys->ti.keyframe_frequency = 64;
    p_sys->ti.keyframe_frequency_force = 64;
Gildas Bazin's avatar
 
Gildas Bazin committed
763
    p_sys->ti.keyframe_data_target_bitrate = p_enc->fmt_out.i_bitrate * 1.5;
Gildas Bazin's avatar
 
Gildas Bazin committed
764 765 766
    p_sys->ti.keyframe_auto_threshold = 80;
    p_sys->ti.keyframe_mindistance = 8;
    p_sys->ti.noise_sensitivity = 1;
767
    */
Gildas Bazin's avatar
 
Gildas Bazin committed
768

769 770 771
    t_flags = TH_RATECTL_CAP_OVERFLOW; /* default is TH_RATECTL_CAP_OVERFLOW | TL_RATECTL_DROP_FRAMES */
    /* Turn off dropframes */
    th_encode_ctl( p_sys->tcx, TH_ENCCTL_SET_RATE_FLAGS, &t_flags, sizeof(t_flags) );
Gildas Bazin's avatar
 
Gildas Bazin committed
772

773 774 775 776
    /* turn on fast encoding */
    if ( !th_encode_ctl( p_sys->tcx, TH_ENCCTL_GET_SPLEVEL_MAX, &max_enc_level,
                sizeof(max_enc_level) ) ) /* returns 0 on success */
        th_encode_ctl( p_sys->tcx, TH_ENCCTL_SET_SPLEVEL, &max_enc_level, sizeof(max_enc_level) );
777

778 779 780
    /* Set forced distance between key frames */
    th_encode_ctl( p_sys->tcx, TH_ENCCTL_SET_KEYFRAME_FREQUENCY_FORCE,
                   &keyframe_freq_force, sizeof(keyframe_freq_force) );
781

782 783 784 785
    /* Create and store headers */
    while ( ( status = th_encode_flushheader( p_sys->tcx, &p_sys->tc, &header ) ) )
    {
        if ( status < 0 ) return VLC_EGENERIC;
Laurent Aimar's avatar
Laurent Aimar committed
786 787 788 789 790 791
        if( xiph_AppendHeaders( &p_enc->fmt_out.i_extra, &p_enc->fmt_out.p_extra,
                                header.bytes, header.packet ) )
        {
            p_enc->fmt_out.i_extra = 0;
            p_enc->fmt_out.p_extra = NULL;
        }
Gildas Bazin's avatar
 
Gildas Bazin committed
792
    }
793
    return VLC_SUCCESS;
Gildas Bazin's avatar
 
Gildas Bazin committed
794 795 796 797 798 799 800 801 802 803 804 805
}

/****************************************************************************
 * Encode: the whole thing
 ****************************************************************************
 * This function spits out ogg packets.
 ****************************************************************************/
static block_t *Encode( encoder_t *p_enc, picture_t *p_pict )
{
    encoder_sys_t *p_sys = p_enc->p_sys;
    ogg_packet oggpacket;
    block_t *p_block;
806
    th_ycbcr_buffer ycbcr;
Tristan Matthews's avatar
Tristan Matthews committed
807
    unsigned i;
808

809
    if( !p_pict ) return NULL;
810
    /* Sanity check */
Tristan Matthews's avatar
Tristan Matthews committed
811 812
    if( p_pict->p[0].i_pitch < (int)p_sys->ti.frame_width ||
        p_pict->p[0].i_lines < (int)p_sys->ti.frame_height )
813 814 815 816
    {
        msg_Warn( p_enc, "frame is smaller than encoding size"
                  "(%ix%i->%ix%i) -> dropping frame",
                  p_pict->p[0].i_pitch, p_pict->p[0].i_lines,
Tristan Matthews's avatar
Tristan Matthews committed
817
                  p_sys->ti.frame_width, p_sys->ti.frame_height );
818 819 820 821
        return NULL;
    }

    /* Fill padding */
Tristan Matthews's avatar
Tristan Matthews committed
822
    if( p_pict->p[0].i_visible_pitch < (int)p_sys->ti.frame_width )
823
    {
Tristan Matthews's avatar
Tristan Matthews committed
824
        for( i = 0; i < p_sys->ti.frame_height; i++ )
825 826 827 828 829
        {
            memset( p_pict->p[0].p_pixels + i * p_pict->p[0].i_pitch +
                    p_pict->p[0].i_visible_pitch,
                    *( p_pict->p[0].p_pixels + i * p_pict->p[0].i_pitch +
                       p_pict->p[0].i_visible_pitch - 1 ),
Tristan Matthews's avatar
Tristan Matthews committed
830
                    p_sys->ti.frame_width - p_pict->p[0].i_visible_pitch );
831
        }
Tristan Matthews's avatar
Tristan Matthews committed
832
        for( i = 0; i < p_sys->ti.frame_height / 2; i++ )
833 834 835 836 837
        {
            memset( p_pict->p[1].p_pixels + i * p_pict->p[1].i_pitch +
                    p_pict->p[1].i_visible_pitch,
                    *( p_pict->p[1].p_pixels + i * p_pict->p[1].i_pitch +
                       p_pict->p[1].i_visible_pitch - 1 ),
Tristan Matthews's avatar
Tristan Matthews committed
838
                    p_sys->ti.frame_width / 2 - p_pict->p[1].i_visible_pitch );
839 840 841 842
            memset( p_pict->p[2].p_pixels + i * p_pict->p[2].i_pitch +
                    p_pict->p[2].i_visible_pitch,
                    *( p_pict->p[2].p_pixels + i * p_pict->p[2].i_pitch +
                       p_pict->p[2].i_visible_pitch - 1 ),
Tristan Matthews's avatar
Tristan Matthews committed
843
                    p_sys->ti.frame_width / 2 - p_pict->p[2].i_visible_pitch );
844 845 846
        }
    }

Tristan Matthews's avatar
Tristan Matthews committed
847
    if( p_pict->p[0].i_visible_lines < (int)p_sys->ti.frame_height )
848
    {
Tristan Matthews's avatar
Tristan Matthews committed
849
        for( i = p_pict->p[0].i_visible_lines; i < p_sys->ti.frame_height; i++ )
850 851
        {
            memset( p_pict->p[0].p_pixels + i * p_pict->p[0].i_pitch, 0,
Tristan Matthews's avatar
Tristan Matthews committed
852
                    p_sys->ti.frame_width );
853
        }
Tristan Matthews's avatar
Tristan Matthews committed
854
        for( i = p_pict->p[1].i_visible_lines; i < p_sys->ti.frame_height / 2; i++ )
855 856
        {
            memset( p_pict->p[1].p_pixels + i * p_pict->p[1].i_pitch, 0x80,
Tristan Matthews's avatar
Tristan Matthews committed
857
                    p_sys->ti.frame_width / 2 );
858
            memset( p_pict->p[2].p_pixels + i * p_pict->p[2].i_pitch, 0x80,
Tristan Matthews's avatar
Tristan Matthews committed
859
                    p_sys->ti.frame_width / 2 );
860 861
        }
    }
Gildas Bazin's avatar
 
Gildas Bazin committed
862 863 864 865

    /* Theora is a one-frame-in, one-frame-out system. Submit a frame
     * for compression and pull out the packet. */

Tristan Matthews's avatar
Tristan Matthews committed
866 867
    ycbcr[0].width = p_sys->ti.frame_width;
    ycbcr[0].height = p_sys->ti.frame_height;
868 869
    ycbcr[0].stride = p_pict->p[0].i_pitch;
    ycbcr[0].data = p_pict->p[0].p_pixels;
Gildas Bazin's avatar
 
Gildas Bazin committed
870

Tristan Matthews's avatar
Tristan Matthews committed
871 872
    ycbcr[1].width = p_sys->ti.frame_width / 2;
    ycbcr[1].height = p_sys->ti.frame_height / 2;
873 874
    ycbcr[1].stride = p_pict->p[1].i_pitch;
    ycbcr[1].data = p_pict->p[1].p_pixels;
Gildas Bazin's avatar
 
Gildas Bazin committed
875

Tristan Matthews's avatar
Tristan Matthews committed
876 877
    ycbcr[2].width = p_sys->ti.frame_width / 2;
    ycbcr[2].height = p_sys->ti.frame_height / 2;
878 879
    ycbcr[2].stride = p_pict->p[1].i_pitch;
    ycbcr[2].data = p_pict->p[2].p_pixels;
Gildas Bazin's avatar
 
Gildas Bazin committed
880

881
    if( th_encode_ycbcr_in( p_sys->tcx, ycbcr ) < 0 )
Gildas Bazin's avatar
 
Gildas Bazin committed
882 883 884 885
    {
        msg_Warn( p_enc, "failed encoding a frame" );
        return NULL;
    }
Gildas Bazin's avatar
 
Gildas Bazin committed
886

887
    th_encode_packetout( p_sys->tcx, 0, &oggpacket );
Gildas Bazin's avatar
 
Gildas Bazin committed
888 889

    /* Ogg packet to block */
890
    p_block = block_Alloc( oggpacket.bytes );
Gildas Bazin's avatar
 
Gildas Bazin committed
891
    memcpy( p_block->p_buffer, oggpacket.packet, oggpacket.bytes );
892
    p_block->i_dts = p_block->i_pts = p_pict->date;
Gildas Bazin's avatar
 
Gildas Bazin committed
893

894
    if( th_packet_iskeyframe( &oggpacket ) )
ogg.k.ogg.k's avatar
ogg.k.ogg.k committed
895 896 897 898
    {
        p_block->i_flags |= BLOCK_FLAG_TYPE_I;
    }

Gildas Bazin's avatar
 
Gildas Bazin committed
899 900 901 902 903 904 905 906 907 908 909
    return p_block;
}

/*****************************************************************************
 * CloseEncoder: theora encoder destruction
 *****************************************************************************/
static void CloseEncoder( vlc_object_t *p_this )
{
    encoder_t *p_enc = (encoder_t *)p_this;
    encoder_sys_t *p_sys = p_enc->p_sys;

910 911 912
    th_info_clear(&p_sys->ti);
    th_comment_clear(&p_sys->tc);
    th_encode_free(p_sys->tcx);
Gildas Bazin's avatar
 
Gildas Bazin committed
913 914
    free( p_sys );
}
915
#endif