]> git.sesse.net Git - vlc/blob - modules/codec/daala.c
upnp: change item b_net and i_type
[vlc] / modules / codec / daala.c
1 /*****************************************************************************
2  * daala.c: daala codec module making use of libdaala.
3  *****************************************************************************
4  * Copyright (C) 2014 VLC authors and VideoLAN
5  *
6  * Authors: Tristan Matthews <le.businessman@gmail.com>
7  *   * Based on theora.c by: Gildas Bazin <gbazin@videolan.org>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_codec.h>
34 #include <vlc_input.h>
35 #include "../demux/xiph.h"
36
37 #include <ogg/ogg.h>
38
39 #include <daala/codec.h>
40 #include <daala/daaladec.h>
41 #ifdef ENABLE_SOUT
42 #include <daala/daalaenc.h>
43 #endif
44
45 #include <limits.h>
46
47 /*****************************************************************************
48  * decoder_sys_t : daala decoder descriptor
49  *****************************************************************************/
50 struct decoder_sys_t
51 {
52     /* Module mode */
53     bool b_packetizer;
54
55     /*
56      * Input properties
57      */
58     bool b_has_headers;
59
60     /*
61      * Daala properties
62      */
63     daala_info          di;       /* daala bitstream settings */
64     daala_comment       dc;       /* daala comment information */
65     daala_dec_ctx       *dcx;     /* daala decoder context */
66
67     /*
68      * Decoding properties
69      */
70     bool b_decoded_first_keyframe;
71
72     /*
73      * Common properties
74      */
75     mtime_t i_pts;
76 };
77
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 * );
84
85 static void *DecodeBlock  ( decoder_t *, block_t ** );
86 static int  ProcessHeaders( decoder_t * );
87 static void *ProcessPacket ( decoder_t *, ogg_packet *, block_t ** );
88
89 static picture_t *DecodePacket( decoder_t *, ogg_packet * );
90
91 static void ParseDaalaComments( decoder_t * );
92 static void daala_CopyPicture( picture_t *, od_img * );
93
94 #ifdef ENABLE_SOUT
95 static int  OpenEncoder( vlc_object_t *p_this );
96 static void CloseEncoder( vlc_object_t *p_this );
97 static block_t *Encode( encoder_t *p_enc, picture_t *p_pict );
98
99 static const char *const enc_chromafmt_list[] = {
100     "420", "444"
101 };
102 static const char *const enc_chromafmt_list_text[] = {
103     "4:2:0", "4:4:4"
104 };
105 #endif
106
107 /*****************************************************************************
108  * Module descriptor
109  *****************************************************************************/
110 #define ENC_QUALITY_TEXT N_("Encoding quality")
111 #define ENC_QUALITY_LONGTEXT N_( \
112   "Enforce a quality between 0 (lossless) and 511 (worst)." )
113 #define ENC_KEYINT_TEXT N_("Keyframe interval")
114 #define ENC_KEYINT_LONGTEXT N_( \
115   "Enforce a keyframe interval between 1 and 1000." )
116
117 vlc_module_begin ()
118     set_category( CAT_INPUT )
119     set_subcategory( SUBCAT_INPUT_VCODEC )
120     set_shortname( "Daala" )
121     set_description( N_("Daala video decoder") )
122     set_capability( "decoder", 100 )
123     set_callbacks( OpenDecoder, CloseDecoder )
124     add_shortcut( "daala" )
125     add_submodule ()
126     set_description( N_("Daala video packetizer") )
127     set_capability( "packetizer", 100 )
128     set_callbacks( OpenPacketizer, CloseDecoder )
129     add_shortcut( "daala" )
130
131 #ifdef ENABLE_SOUT
132     add_submodule ()
133     set_description( N_("Daala video encoder") )
134     set_capability( "encoder", 150 )
135     set_callbacks( OpenEncoder, CloseEncoder )
136     add_shortcut( "daala" )
137
138 #   define ENC_CFG_PREFIX "sout-daala-"
139     add_integer_with_range( ENC_CFG_PREFIX "quality", 30, 0, 511,
140                  ENC_QUALITY_TEXT, ENC_QUALITY_LONGTEXT, false )
141     add_integer_with_range( ENC_CFG_PREFIX "keyint", 256, 1, 1000,
142                  ENC_KEYINT_TEXT, ENC_KEYINT_LONGTEXT, false )
143
144 #   define ENC_CHROMAFMT_TEXT N_("Chroma format")
145 #   define ENC_CHROMAFMT_LONGTEXT N_("Picking chroma format will force a " \
146                                      "conversion of the video into that format")
147
148     add_string( ENC_CFG_PREFIX "chroma-fmt", "420", ENC_CHROMAFMT_TEXT,
149                 ENC_CHROMAFMT_LONGTEXT, false )
150     change_string_list( enc_chromafmt_list, enc_chromafmt_list_text )
151 vlc_module_end ()
152
153 static const char *const ppsz_enc_options[] = {
154     "quality", "keyint", "chroma-fmt", NULL
155 };
156 #endif
157
158 /*****************************************************************************
159  * OpenDecoder: probe the decoder and return score
160  *****************************************************************************/
161 static int OpenDecoder( vlc_object_t *p_this )
162 {
163     decoder_t *p_dec = (decoder_t*)p_this;
164     decoder_sys_t *p_sys;
165
166     if( p_dec->fmt_in.i_codec != VLC_CODEC_DAALA )
167     {
168         return VLC_EGENERIC;
169     }
170
171     /* Allocate the memory needed to store the decoder's structure */
172     p_sys = malloc(sizeof(*p_sys));
173     if( p_sys == NULL )
174         return VLC_ENOMEM;
175
176     p_dec->p_sys = p_sys;
177     p_dec->p_sys->b_packetizer = false;
178     p_sys->b_has_headers = false;
179     p_sys->i_pts = VLC_TS_INVALID;
180     p_sys->b_decoded_first_keyframe = false;
181     p_sys->dcx = NULL;
182
183     /* Set output properties */
184     p_dec->fmt_out.i_cat = VIDEO_ES;
185     p_dec->fmt_out.i_codec = VLC_CODEC_I420;
186
187     /* Set callbacks */
188     p_dec->pf_decode_video = (picture_t *(*)(decoder_t *, block_t **))
189         DecodeBlock;
190     p_dec->pf_packetize    = (block_t *(*)(decoder_t *, block_t **))
191         DecodeBlock;
192
193     /* Init supporting Daala structures needed in header parsing */
194     daala_comment_init( &p_sys->dc );
195     daala_info_init( &p_sys->di );
196
197     return VLC_SUCCESS;
198 }
199
200 static int OpenPacketizer( vlc_object_t *p_this )
201 {
202     decoder_t *p_dec = (decoder_t*)p_this;
203
204     int i_ret = OpenDecoder( p_this );
205
206     if( i_ret == VLC_SUCCESS )
207     {
208         p_dec->p_sys->b_packetizer = true;
209         p_dec->fmt_out.i_codec = VLC_CODEC_DAALA;
210     }
211
212     return i_ret;
213 }
214
215 /****************************************************************************
216  * DecodeBlock: the whole thing
217  ****************************************************************************
218  * This function must be fed with ogg packets.
219  ****************************************************************************/
220 static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
221 {
222     decoder_sys_t *p_sys = p_dec->p_sys;
223     block_t *p_block;
224     ogg_packet oggpacket;
225
226     if( !pp_block || !*pp_block ) return NULL;
227
228     p_block = *pp_block;
229
230     /* Block to Ogg packet */
231     oggpacket.packet = p_block->p_buffer;
232     oggpacket.bytes = p_block->i_buffer;
233     oggpacket.granulepos = p_block->i_dts;
234     oggpacket.b_o_s = 0;
235     oggpacket.e_o_s = 0;
236     oggpacket.packetno = 0;
237
238     /* Check for headers */
239     if( !p_sys->b_has_headers )
240     {
241         if( ProcessHeaders( p_dec ) )
242         {
243             block_Release( p_block );
244             return NULL;
245         }
246         p_sys->b_has_headers = true;
247     }
248
249     /* If we haven't seen a single keyframe yet, set to preroll,
250      * otherwise we'll get display artifacts.  (This is impossible
251      * in the general case, but can happen if e.g. we play a network stream
252      * using a timed URL, such that the server doesn't start the video with a
253      * keyframe). */
254     if( !p_sys->b_decoded_first_keyframe )
255         p_block->i_flags |= BLOCK_FLAG_PREROLL; /* Wait until we've decoded the first keyframe */
256
257     return ProcessPacket( p_dec, &oggpacket, pp_block );
258 }
259
260 /*****************************************************************************
261  * ProcessHeaders: process Daala headers.
262  *****************************************************************************/
263 static int ProcessHeaders( decoder_t *p_dec )
264 {
265     int ret = VLC_SUCCESS;
266     decoder_sys_t *p_sys = p_dec->p_sys;
267     ogg_packet oggpacket;
268     daala_setup_info *ds = NULL; /* daala setup information */
269
270     unsigned pi_size[XIPH_MAX_HEADER_COUNT];
271     void     *pp_data[XIPH_MAX_HEADER_COUNT];
272     unsigned i_count;
273     if( xiph_SplitHeaders( pi_size, pp_data, &i_count,
274                            p_dec->fmt_in.i_extra, p_dec->fmt_in.p_extra) )
275         return VLC_EGENERIC;
276     if( i_count < 3 )
277         return VLC_EGENERIC;
278
279     oggpacket.granulepos = -1;
280     oggpacket.e_o_s = 0;
281     oggpacket.packetno = 0;
282
283     /* Take care of the initial info header */
284     oggpacket.b_o_s  = 1; /* yes this actually is a b_o_s packet :) */
285     oggpacket.bytes  = pi_size[0];
286     oggpacket.packet = pp_data[0];
287     if( daala_decode_header_in( &p_sys->di, &p_sys->dc, &ds, &oggpacket ) < 0 )
288     {
289         msg_Err( p_dec, "this bitstream does not contain Daala video data" );
290         ret = VLC_EGENERIC;
291         goto cleanup;
292     }
293
294     /* Set output properties */
295     if( !p_sys->b_packetizer )
296     {
297         if( p_sys->di.plane_info[0].xdec == 0 && p_sys->di.plane_info[0].ydec == 0 &&
298             p_sys->di.plane_info[1].xdec == 1 && p_sys->di.plane_info[1].ydec == 1 &&
299             p_sys->di.plane_info[2].xdec == 1 && p_sys->di.plane_info[2].ydec == 1 )
300         {
301             p_dec->fmt_out.i_codec = VLC_CODEC_I420;
302         }
303         else if( p_sys->di.plane_info[0].xdec == 0 && p_sys->di.plane_info[0].ydec == 0 &&
304                  p_sys->di.plane_info[1].xdec == 0 && p_sys->di.plane_info[1].ydec == 0 &&
305                  p_sys->di.plane_info[2].xdec == 0 && p_sys->di.plane_info[2].ydec == 0 )
306         {
307             p_dec->fmt_out.i_codec = VLC_CODEC_I444;
308         }
309         else
310         {
311             msg_Err( p_dec, "unknown chroma in daala sample" );
312         }
313     }
314
315     p_dec->fmt_out.video.i_width = p_sys->di.pic_width;
316     p_dec->fmt_out.video.i_height = p_sys->di.pic_height;
317     if( p_sys->di.pic_width && p_sys->di.pic_height )
318     {
319         p_dec->fmt_out.video.i_visible_width = p_sys->di.pic_width;
320         p_dec->fmt_out.video.i_visible_height = p_sys->di.pic_height;
321     }
322
323     if( p_sys->di.pixel_aspect_denominator && p_sys->di.pixel_aspect_numerator )
324     {
325         p_dec->fmt_out.video.i_sar_num = p_sys->di.pixel_aspect_numerator;
326         p_dec->fmt_out.video.i_sar_den = p_sys->di.pixel_aspect_denominator;
327     }
328     else
329     {
330         p_dec->fmt_out.video.i_sar_num = 1;
331         p_dec->fmt_out.video.i_sar_den = 1;
332     }
333
334     if( p_sys->di.timebase_numerator > 0 && p_sys->di.timebase_denominator > 0 )
335     {
336         p_dec->fmt_out.video.i_frame_rate = p_sys->di.timebase_numerator;
337         p_dec->fmt_out.video.i_frame_rate_base = p_sys->di.timebase_denominator;
338     }
339
340     msg_Dbg( p_dec, "%dx%d %.02f fps video, frame content ",
341              p_sys->di.pic_width, p_sys->di.pic_height,
342              (double)p_sys->di.timebase_numerator/p_sys->di.timebase_denominator );
343
344     /* The next packet in order is the comments header */
345     oggpacket.b_o_s  = 0;
346     oggpacket.bytes  = pi_size[1];
347     oggpacket.packet = pp_data[1];
348
349     if( daala_decode_header_in( &p_sys->di, &p_sys->dc, &ds, &oggpacket ) < 0 )
350     {
351         msg_Err( p_dec, "Daala comment header is corrupted" );
352         ret = VLC_EGENERIC;
353         goto cleanup;
354     }
355
356     ParseDaalaComments( p_dec );
357
358     /* The next packet in order is the setup header
359      * We need to watch out that this packet is not missing as a
360      * missing or corrupted header is fatal. */
361     oggpacket.b_o_s  = 0;
362     oggpacket.bytes  = pi_size[2];
363     oggpacket.packet = pp_data[2];
364     if( daala_decode_header_in( &p_sys->di, &p_sys->dc, &ds, &oggpacket ) < 0 )
365     {
366         msg_Err( p_dec, "Daala setup header is corrupted" );
367         ret = VLC_EGENERIC;
368         goto cleanup;
369     }
370
371     if( !p_sys->b_packetizer )
372     {
373         /* We have all the headers, initialize decoder */
374         if ( ( p_sys->dcx = daala_decode_alloc( &p_sys->di, ds ) ) == NULL )
375         {
376             msg_Err( p_dec, "Could not allocate Daala decoder" );
377             ret = VLC_EGENERIC;
378             goto cleanup;
379         }
380     }
381     else
382     {
383         p_dec->fmt_out.i_extra = p_dec->fmt_in.i_extra;
384         p_dec->fmt_out.p_extra = xrealloc( p_dec->fmt_out.p_extra,
385                 p_dec->fmt_out.i_extra );
386         memcpy( p_dec->fmt_out.p_extra,
387                 p_dec->fmt_in.p_extra, p_dec->fmt_out.i_extra );
388     }
389
390 cleanup:
391     /* Clean up the decoder setup info... we're done with it */
392     daala_setup_free( ds );
393
394     return ret;
395 }
396
397 /*****************************************************************************
398  * ProcessPacket: processes a daala packet.
399  *****************************************************************************/
400 static void *ProcessPacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
401                             block_t **pp_block )
402 {
403     decoder_sys_t *p_sys = p_dec->p_sys;
404     block_t *p_block = *pp_block;
405     void *p_buf;
406
407     if( ( p_block->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) ) != 0 )
408     {
409         /* Don't send the the first packet after a discontinuity to
410          * daala_decode, otherwise we get purple/green display artifacts
411          * appearing in the video output */
412         block_Release(p_block);
413         return NULL;
414     }
415
416     /* Date management */
417     if( p_block->i_pts > VLC_TS_INVALID && p_block->i_pts != p_sys->i_pts )
418     {
419         p_sys->i_pts = p_block->i_pts;
420     }
421
422     *pp_block = NULL; /* To avoid being fed the same packet again */
423
424     if( p_sys->b_packetizer )
425     {
426         /* Date management */
427         p_block->i_dts = p_block->i_pts = p_sys->i_pts;
428
429         p_block->i_length = p_sys->i_pts - p_block->i_pts;
430
431         p_buf = p_block;
432     }
433     else
434     {
435         p_buf = DecodePacket( p_dec, p_oggpacket );
436         block_Release( p_block );
437     }
438
439     /* Date management */
440     p_sys->i_pts += ( CLOCK_FREQ * p_sys->di.timebase_denominator /
441                       p_sys->di.timebase_numerator ); /* 1 frame per packet */
442
443     return p_buf;
444 }
445
446 /*****************************************************************************
447  * DecodePacket: decodes a Daala packet.
448  *****************************************************************************/
449 static picture_t *DecodePacket( decoder_t *p_dec, ogg_packet *p_oggpacket )
450 {
451     decoder_sys_t *p_sys = p_dec->p_sys;
452     picture_t *p_pic;
453     od_img ycbcr;
454
455     if (daala_decode_packet_in( p_sys->dcx, &ycbcr, p_oggpacket ) < 0)
456         return NULL; /* bad packet */
457
458     /* Check for keyframe */
459     if( daala_packet_iskeyframe( p_oggpacket->packet, p_oggpacket->bytes ) )
460         p_sys->b_decoded_first_keyframe = true;
461
462     /* Get a new picture */
463     p_pic = decoder_NewPicture( p_dec );
464     if( !p_pic ) return NULL;
465
466     daala_CopyPicture( p_pic, &ycbcr );
467
468     p_pic->date = p_sys->i_pts;
469
470     return p_pic;
471 }
472
473 /*****************************************************************************
474  * ParseDaalaComments:
475  *****************************************************************************/
476 static void ParseDaalaComments( decoder_t *p_dec )
477 {
478     char *psz_name, *psz_value, *psz_comment;
479     /* Regarding the daala_comment structure: */
480
481     /* The metadata is stored as a series of (tag, value) pairs, in
482        length-encoded string vectors. The first occurrence of the '='
483        character delimits the tag and value. A particular tag may
484        occur more than once, and order is significant. The character
485        set encoding for the strings is always UTF-8, but the tag names
486        are limited to ASCII, and treated as case-insensitive. See the
487        Theora specification, Section 6.3.3 for details. */
488
489     /* In filling in this structure, daala_decode_header_in() will
490        null-terminate the user_comment strings for safety. However,
491        the bitstream format itself treats them as 8-bit clean vectors,
492        possibly containing null characters, and so the length array
493        should be treated as their authoritative length. */
494     for ( int i = 0; i < p_dec->p_sys->dc.comments; i++ )
495     {
496         int clen = p_dec->p_sys->dc.comment_lengths[i];
497         if ( clen <= 0 || clen >= INT_MAX ) { continue; }
498         psz_comment = malloc( clen + 1 );
499         if( !psz_comment )
500             break;
501         memcpy( (void*)psz_comment, (void*)p_dec->p_sys->dc.user_comments[i], clen + 1 );
502         psz_comment[clen] = '\0';
503
504         psz_name = psz_comment;
505         psz_value = strchr( psz_comment, '=' );
506         if( psz_value )
507         {
508             *psz_value = '\0';
509             psz_value++;
510
511             if( !p_dec->p_description )
512                 p_dec->p_description = vlc_meta_New();
513             /* TODO:  Since psz_value can contain NULLs see if there is an
514              * instance where we need to preserve the full length of this string */
515             if( p_dec->p_description )
516                 vlc_meta_AddExtra( p_dec->p_description, psz_name, psz_value );
517         }
518         free( psz_comment );
519     }
520 }
521
522 /*****************************************************************************
523  * CloseDecoder: daala decoder destruction
524  *****************************************************************************/
525 static void CloseDecoder( vlc_object_t *p_this )
526 {
527     decoder_t *p_dec = (decoder_t *)p_this;
528     decoder_sys_t *p_sys = p_dec->p_sys;
529
530     daala_info_clear(&p_sys->di);
531     daala_comment_clear(&p_sys->dc);
532     daala_decode_free(p_sys->dcx);
533     free( p_sys );
534 }
535
536 /*****************************************************************************
537  * daala_CopyPicture: copy a picture from daala internal buffers to a
538  *                     picture_t structure.
539  *****************************************************************************/
540 static void daala_CopyPicture( picture_t *p_pic,
541                                od_img *ycbcr )
542 {
543     const int i_planes = p_pic->i_planes < 3 ? p_pic->i_planes : 3;
544     for( int i_plane = 0; i_plane < i_planes; i_plane++ )
545     {
546         const int i_total_lines = __MIN(p_pic->p[i_plane].i_lines,
547                 ycbcr->height >> ycbcr->planes[i_plane].ydec);
548         uint8_t *p_dst = p_pic->p[i_plane].p_pixels;
549         uint8_t *p_src = ycbcr->planes[i_plane].data;
550         const int i_dst_stride  = p_pic->p[i_plane].i_pitch;
551         const int i_src_stride  = ycbcr->planes[i_plane].ystride;
552         for( int i_line = 0; i_line < i_total_lines; i_line++ )
553         {
554             memcpy( p_dst, p_src, i_src_stride );
555             p_src += i_src_stride;
556             p_dst += i_dst_stride;
557         }
558     }
559 }
560
561 #ifdef ENABLE_SOUT
562 struct encoder_sys_t
563 {
564     daala_info      di;                     /* daala bitstream settings */
565     daala_comment   dc;                     /* daala comment header */
566     daala_enc_ctx   *dcx;                   /* daala context */
567 };
568
569 static int OpenEncoder( vlc_object_t *p_this )
570 {
571     encoder_t *p_enc = (encoder_t *)p_this;
572     encoder_sys_t *p_sys;
573     ogg_packet header;
574     int status;
575
576     if( p_enc->fmt_out.i_codec != VLC_CODEC_DAALA &&
577         !p_enc->b_force )
578     {
579         return VLC_EGENERIC;
580     }
581
582     /* Allocate the memory needed to store the encoder's structure */
583     p_sys = malloc( sizeof( encoder_sys_t ) );
584     if( !p_sys )
585         return VLC_ENOMEM;
586     p_enc->p_sys = p_sys;
587
588     p_enc->pf_encode_video = Encode;
589     p_enc->fmt_in.i_codec = VLC_CODEC_I420;
590     p_enc->fmt_out.i_codec = VLC_CODEC_DAALA;
591
592     config_ChainParse( p_enc, ENC_CFG_PREFIX, ppsz_enc_options, p_enc->p_cfg );
593
594     char *psz_tmp = var_GetString( p_enc, ENC_CFG_PREFIX "chroma-fmt" );
595     uint32_t i_codec;
596     if( !psz_tmp ) {
597         free(p_sys);
598         return VLC_ENOMEM;
599     } else {
600         if( !strcmp( psz_tmp, "420" ) ) {
601             i_codec = VLC_CODEC_I420;
602         }
603         else if( !strcmp( psz_tmp, "444" ) ) {
604             i_codec = VLC_CODEC_I444;
605         }
606         else {
607             msg_Err( p_enc, "Invalid chroma format: %s", psz_tmp );
608             free( psz_tmp );
609             free( p_sys );
610             return VLC_EGENERIC;
611         }
612         free( psz_tmp );
613         p_enc->fmt_in.i_codec = i_codec;
614         /* update bits_per_pixel */
615         video_format_Setup(&p_enc->fmt_in.video, i_codec,
616                 p_enc->fmt_in.video.i_width,
617                 p_enc->fmt_in.video.i_height,
618                 p_enc->fmt_in.video.i_visible_width,
619                 p_enc->fmt_in.video.i_visible_height,
620                 p_enc->fmt_in.video.i_sar_num,
621                 p_enc->fmt_in.video.i_sar_den);
622     }
623
624     daala_info_init( &p_sys->di );
625
626     p_sys->di.pic_width = p_enc->fmt_in.video.i_visible_width;
627     p_sys->di.pic_height = p_enc->fmt_in.video.i_visible_height;
628
629     p_sys->di.nplanes = 3;
630     for (int i = 0; i < p_sys->di.nplanes; i++)
631     {
632         p_sys->di.plane_info[i].xdec = i > 0 && i_codec != VLC_CODEC_I444;
633         p_sys->di.plane_info[i].ydec = i_codec == VLC_CODEC_I420 ?
634             p_sys->di.plane_info[i].xdec : 0;
635     }
636     p_sys->di.frame_duration = 1;
637
638     if( !p_enc->fmt_in.video.i_frame_rate ||
639         !p_enc->fmt_in.video.i_frame_rate_base )
640     {
641         p_sys->di.timebase_numerator = 25;
642         p_sys->di.timebase_denominator = 1;
643     }
644     else
645     {
646         p_sys->di.timebase_numerator = p_enc->fmt_in.video.i_frame_rate;
647         p_sys->di.timebase_denominator = p_enc->fmt_in.video.i_frame_rate_base;
648     }
649
650     if( p_enc->fmt_in.video.i_sar_num > 0 && p_enc->fmt_in.video.i_sar_den > 0 )
651     {
652         unsigned i_dst_num, i_dst_den;
653         vlc_ureduce( &i_dst_num, &i_dst_den,
654                      p_enc->fmt_in.video.i_sar_num,
655                      p_enc->fmt_in.video.i_sar_den, 0 );
656         p_sys->di.pixel_aspect_numerator = i_dst_num;
657         p_sys->di.pixel_aspect_denominator = i_dst_den;
658     }
659     else
660     {
661         p_sys->di.pixel_aspect_numerator = 4;
662         p_sys->di.pixel_aspect_denominator = 3;
663     }
664
665     p_sys->di.keyframe_rate = var_GetInteger( p_enc, ENC_CFG_PREFIX "keyint" );
666
667     daala_enc_ctx *dcx;
668     p_sys->dcx = dcx = daala_encode_create( &p_sys->di );
669     if( !dcx )
670     {
671         free( p_sys );
672         return VLC_ENOMEM;
673     }
674
675     daala_comment_init( &p_sys->dc );
676
677     int i_quality = var_GetInteger( p_enc, ENC_CFG_PREFIX "quality" );
678     daala_encode_ctl( dcx, OD_SET_QUANT, &i_quality, sizeof(i_quality) );
679
680     /* Create and store headers */
681     while( ( status = daala_encode_flush_header( dcx, &p_sys->dc, &header ) ) )
682     {
683         if ( status < 0 )
684         {
685             CloseEncoder( p_this );
686             return VLC_EGENERIC;
687         }
688         if( xiph_AppendHeaders( &p_enc->fmt_out.i_extra,
689                                 &p_enc->fmt_out.p_extra, header.bytes,
690                                 header.packet ) )
691         {
692             p_enc->fmt_out.i_extra = 0;
693             p_enc->fmt_out.p_extra = NULL;
694         }
695     }
696     return VLC_SUCCESS;
697 }
698
699 static block_t *Encode( encoder_t *p_enc, picture_t *p_pict )
700 {
701     encoder_sys_t *p_sys = p_enc->p_sys;
702     ogg_packet oggpacket;
703     block_t *p_block;
704     od_img img;
705
706     if( !p_pict ) return NULL;
707
708     const int i_width = p_sys->di.pic_width;
709     const int i_height = p_sys->di.pic_height;
710
711     /* Sanity check */
712     if( p_pict->p[0].i_pitch < i_width ||
713         p_pict->p[0].i_lines < i_height )
714     {
715         msg_Err( p_enc, "frame is smaller than encoding size"
716                  "(%ix%i->%ix%i) -> dropping frame",
717                  p_pict->p[0].i_pitch, p_pict->p[0].i_lines,
718                  i_width, i_height );
719         return NULL;
720     }
721
722     /* Daala is a one-frame-in, one-frame-out system. Submit a frame
723      * for compression and pull out the packet. */
724
725     img.nplanes = p_sys->di.nplanes;
726     img.width = i_width;
727     img.height = i_height;
728     for( int i = 0; i < img.nplanes; i++ )
729     {
730         img.planes[i].data = p_pict->p[i].p_pixels;
731         img.planes[i].xdec = p_sys->di.plane_info[i].xdec;
732         img.planes[i].ydec = p_sys->di.plane_info[i].ydec;
733         img.planes[i].xstride = 1;
734         img.planes[i].ystride = p_pict->p[i].i_pitch;
735     }
736
737     if( daala_encode_img_in( p_sys->dcx, &img, 0 ) < 0 )
738     {
739         msg_Warn( p_enc, "failed encoding a frame" );
740         return NULL;
741     }
742
743     daala_encode_packet_out( p_sys->dcx, 0, &oggpacket );
744
745     /* Ogg packet to block */
746     p_block = block_Alloc( oggpacket.bytes );
747     memcpy( p_block->p_buffer, oggpacket.packet, oggpacket.bytes );
748     p_block->i_dts = p_block->i_pts = p_pict->date;
749
750     if( daala_packet_iskeyframe( oggpacket.packet, oggpacket.bytes ) )
751         p_block->i_flags |= BLOCK_FLAG_TYPE_I;
752
753     return p_block;
754 }
755
756 static void CloseEncoder( vlc_object_t *p_this )
757 {
758     encoder_t *p_enc = (encoder_t *)p_this;
759     encoder_sys_t *p_sys = p_enc->p_sys;
760
761     daala_info_clear(&p_sys->di);
762     daala_comment_clear(&p_sys->dc);
763     daala_encode_free(p_sys->dcx);
764     free( p_sys );
765 }
766 #endif