]> git.sesse.net Git - vlc/blob - modules/codec/dirac.c
Upgrade dirac support to 0.6.0 (close #1129)
[vlc] / modules / codec / dirac.c
1 /*****************************************************************************
2  * dirac.c: Dirac decoder/encoder module making use of libdirac.
3  *          (http://www.bbc.co.uk/rd/projects/dirac/index.shtml)
4  *****************************************************************************
5  * Copyright (C) 1999-2001 the VideoLAN team
6  * $Id$
7  *
8  * Authors: Gildas Bazin <gbazin@videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <vlc/vlc.h>
29 #include <vlc_codec.h>
30 #include <vlc_sout.h>
31 #include <vlc_vout.h>
32
33 #include <libdirac_decoder/dirac_parser.h>
34 #include <libdirac_encoder/dirac_encoder.h>
35
36 /*****************************************************************************
37  * decoder_sys_t : theora decoder descriptor
38  *****************************************************************************/
39 struct decoder_sys_t
40 {
41     /*
42      * Dirac properties
43      */
44     dirac_decoder_t *p_dirac;
45 };
46
47 /*****************************************************************************
48  * Local prototypes
49  *****************************************************************************/
50 static int        OpenDecoder  ( vlc_object_t * );
51 static void       CloseDecoder ( vlc_object_t * );
52 static picture_t *DecodeBlock  ( decoder_t *p_dec, block_t **pp_block );
53
54 static int  OpenEncoder( vlc_object_t *p_this );
55 static void CloseEncoder( vlc_object_t *p_this );
56 static block_t *Encode( encoder_t *p_enc, picture_t *p_pict );
57
58 #define ENC_CFG_PREFIX "sout-dirac-"
59
60 static const char *ppsz_enc_options[] = {
61     "quality", NULL
62 };
63
64 /*****************************************************************************
65  * Module descriptor
66  *****************************************************************************/
67 #define ENC_QUALITY_TEXT N_("Encoding quality")
68 #define ENC_QUALITY_LONGTEXT N_( \
69   "Quality of the encoding between 1.0 (low) and 10.0 (high)." )
70
71 vlc_module_begin();
72     set_category( CAT_INPUT );
73     set_subcategory( SUBCAT_INPUT_VCODEC );
74     set_description( _("Dirac video decoder") );
75     set_capability( "decoder", 100 );
76     set_callbacks( OpenDecoder, CloseDecoder );
77     add_shortcut( "dirac" );
78
79     add_submodule();
80     set_description( _("Dirac video encoder") );
81     set_capability( "encoder", 100 );
82     set_callbacks( OpenEncoder, CloseEncoder );
83     add_shortcut( "dirac" );
84     add_float( ENC_CFG_PREFIX "quality", 7.0, NULL, ENC_QUALITY_TEXT,
85                ENC_QUALITY_LONGTEXT, VLC_FALSE );
86
87 vlc_module_end();
88
89 /*****************************************************************************
90  * OpenDecoder: probe the decoder and return score
91  *****************************************************************************/
92 static int OpenDecoder( vlc_object_t *p_this )
93 {
94     decoder_t *p_dec = (decoder_t*)p_this;
95     decoder_sys_t *p_sys;
96     dirac_decoder_t *p_dirac;
97
98     if( p_dec->fmt_in.i_codec != VLC_FOURCC('d','r','a','c') )
99     {
100         return VLC_EGENERIC;
101     }
102
103     /* Initialise the dirac decoder */
104     if( !(p_dirac = dirac_decoder_init(0)) ) return VLC_EGENERIC;
105
106     /* Allocate the memory needed to store the decoder's structure */
107     if( ( p_dec->p_sys = p_sys =
108           (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
109     {
110         msg_Err( p_dec, "out of memory" );
111         return VLC_EGENERIC;
112     }
113
114     p_sys->p_dirac = p_dirac;
115
116     /* Set output properties */
117     p_dec->fmt_out.i_cat = VIDEO_ES;
118     p_dec->fmt_out.i_codec = VLC_FOURCC('I','4','2','0');
119
120     /* Set callbacks */
121     p_dec->pf_decode_video = DecodeBlock;
122
123     return VLC_SUCCESS;
124 }
125
126 static void FreeFrameBuffer( dirac_decoder_t *p_dirac )
127 {
128     if( p_dirac->fbuf )
129     {
130         int i;
131         for( i = 0; i < 3; i++ )
132         {
133             if( p_dirac->fbuf->buf[i] ) free( p_dirac->fbuf->buf[i] );
134             p_dirac->fbuf->buf[i] = 0;
135         }
136     }
137 }
138
139 /*****************************************************************************
140  * GetNewPicture: Get a new picture from the vout and copy the decoder output
141  *****************************************************************************/
142 static picture_t *GetNewPicture( decoder_t *p_dec )
143 {
144     decoder_sys_t *p_sys = p_dec->p_sys;
145     picture_t *p_pic;
146     int i_plane;
147
148     switch( p_sys->p_dirac->seq_params.chroma )
149     {
150     case format420: p_dec->fmt_out.i_codec = VLC_FOURCC('I','4','2','0'); break;
151     case format422: p_dec->fmt_out.i_codec = VLC_FOURCC('I','4','2','2'); break;
152     case format444: p_dec->fmt_out.i_codec = VLC_FOURCC('I','4','4','4'); break;    // XXX 0.6 ?
153     default:        
154         p_dec->fmt_out.i_codec = 0;
155         break;
156     }
157
158     p_dec->fmt_out.video.i_visible_width =
159     p_dec->fmt_out.video.i_width = p_sys->p_dirac->seq_params.width;
160     p_dec->fmt_out.video.i_visible_height =
161     p_dec->fmt_out.video.i_height = p_sys->p_dirac->seq_params.height;
162     p_dec->fmt_out.video.i_aspect = VOUT_ASPECT_FACTOR * 4 / 3;
163
164     p_dec->fmt_out.video.i_frame_rate =
165         p_sys->p_dirac->src_params.frame_rate.numerator;
166     p_dec->fmt_out.video.i_frame_rate_base =
167         p_sys->p_dirac->src_params.frame_rate.denominator;
168
169     /* Get a new picture */
170     p_pic = p_dec->pf_vout_buffer_new( p_dec );
171
172     if( p_pic == NULL ) return NULL;
173     p_pic->b_progressive = !p_sys->p_dirac->src_params.interlace;
174     p_pic->b_top_field_first = p_sys->p_dirac->src_params.topfieldfirst;
175
176     p_pic->i_nb_fields = 2;
177
178     /* Copy picture stride by stride */
179     for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
180     {
181         int i_line, i_width, i_dst_stride;
182         uint8_t *p_src = p_sys->p_dirac->fbuf->buf[i_plane];
183         uint8_t *p_dst = p_pic->p[i_plane].p_pixels;
184
185         i_width = p_pic->p[i_plane].i_visible_pitch;
186         i_dst_stride = p_pic->p[i_plane].i_pitch;
187
188         for( i_line = 0; i_line < p_pic->p[i_plane].i_visible_lines; i_line++ )
189         {
190             p_dec->p_libvlc->pf_memcpy( p_dst, p_src, i_width );
191             p_src += i_width;
192             p_dst += i_dst_stride;
193         }
194     }
195
196     return p_pic;
197 }
198
199 /*****************************************************************************
200  * CloseDecoder: decoder destruction
201  *****************************************************************************/
202 static void CloseDecoder( vlc_object_t *p_this )
203 {
204     decoder_t *p_dec = (decoder_t *)p_this;
205     decoder_sys_t *p_sys = p_dec->p_sys;
206
207     FreeFrameBuffer( p_sys->p_dirac );
208     dirac_decoder_close( p_sys->p_dirac );
209     free( p_sys );
210 }
211
212 /****************************************************************************
213  * DecodeBlock: the whole thing
214  ****************************************************************************
215  * This function must be fed with complete frames.
216  ****************************************************************************/
217 static picture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
218 {
219     decoder_sys_t *p_sys = p_dec->p_sys;
220     dirac_decoder_state_t state;
221     picture_t *p_pic;
222     block_t *p_block;
223
224     if( !pp_block || !*pp_block ) return NULL;
225
226     p_block = *pp_block;
227
228     while( 1 )
229     {
230         state = dirac_parse( p_sys->p_dirac );
231
232         switch( state )
233         {
234         case STATE_BUFFER:
235             if( !p_block->i_buffer )
236             {
237                 block_Release( p_block );
238                 return NULL;
239             }
240
241             msg_Dbg( p_dec, "STATE_BUFFER" );
242             dirac_buffer( p_sys->p_dirac, p_block->p_buffer,
243                           p_block->p_buffer + p_block->i_buffer );
244
245             p_block->i_buffer = 0;
246             break;
247
248         case STATE_SEQUENCE:
249         {
250             /* Initialize video output */
251             uint8_t *buf[3];
252
253             msg_Dbg( p_dec, "%dx%d, chroma %i, %f fps",
254                      p_sys->p_dirac->seq_params.width,
255                      p_sys->p_dirac->seq_params.height,
256                      p_sys->p_dirac->seq_params.chroma,
257                      (float)p_sys->p_dirac->src_params.frame_rate.numerator/
258                      p_sys->p_dirac->src_params.frame_rate.denominator );
259
260             FreeFrameBuffer( p_sys->p_dirac );
261             buf[0] = malloc( p_sys->p_dirac->seq_params.width *
262                              p_sys->p_dirac->seq_params.height );
263             buf[1] = malloc( p_sys->p_dirac->seq_params.chroma_width *
264                              p_sys->p_dirac->seq_params.chroma_height );
265             buf[2] = malloc( p_sys->p_dirac->seq_params.chroma_width *
266                              p_sys->p_dirac->seq_params.chroma_height );
267
268             dirac_set_buf( p_sys->p_dirac, buf, NULL );
269             break;
270         }
271
272         case STATE_SEQUENCE_END:
273             msg_Dbg( p_dec, "SEQUENCE_END" );
274             FreeFrameBuffer( p_sys->p_dirac );
275             break;
276
277         case STATE_PICTURE_START:
278             msg_Dbg( p_dec, "PICTURE_START: frame_type=%i frame_num=%d",
279                      p_sys->p_dirac->frame_params.ftype,
280                      p_sys->p_dirac->frame_params.fnum );
281             break;
282
283         case STATE_PICTURE_AVAIL:
284             msg_Dbg( p_dec, "PICTURE_AVAI : frame_type=%i frame_num=%d",
285                      p_sys->p_dirac->frame_params.ftype,
286                      p_sys->p_dirac->frame_params.fnum );
287
288             /* Picture available for display */
289             p_pic = GetNewPicture( p_dec );
290             p_pic->date = p_block->i_pts > 0 ? p_block->i_pts : p_block->i_dts;
291             p_pic->b_force = 1; // HACK
292             return p_pic;
293             break;
294
295         case STATE_INVALID:
296             msg_Dbg( p_dec, "STATE_INVALID" );
297             break;
298
299         default:
300             break;
301         }
302     }
303
304     /* Never reached */
305     return NULL;
306 }
307
308 /*****************************************************************************
309  * encoder_sys_t : dirac encoder descriptor
310  *****************************************************************************/
311 #define ENC_BUFSIZE 1024*1024
312 struct encoder_sys_t
313 {
314     /*
315      * Dirac properties
316      */
317     dirac_encoder_t *p_dirac;
318     dirac_encoder_context_t ctx;
319
320     uint8_t *p_buffer_in;
321     int i_buffer_in;
322
323     uint8_t p_buffer_out[ENC_BUFSIZE];
324 };
325
326 /*****************************************************************************
327  * OpenEncoder: probe the encoder and return score
328  *****************************************************************************/
329 static int OpenEncoder( vlc_object_t *p_this )
330 {
331     encoder_t *p_enc = (encoder_t *)p_this;
332     encoder_sys_t *p_sys = p_enc->p_sys;
333     vlc_value_t val;
334     float f_quality;
335
336     if( p_enc->fmt_out.i_codec != VLC_FOURCC('d','r','a','c') &&
337         !p_enc->b_force )
338     {
339         return VLC_EGENERIC;
340     }
341
342     /* Allocate the memory needed to store the decoder's structure */
343     if( ( p_sys = (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
344     {
345         msg_Err( p_enc, "out of memory" );
346         return VLC_EGENERIC;
347     }
348     memset( p_sys, 0, sizeof(encoder_sys_t) );
349     p_enc->p_sys = p_sys;
350
351     p_enc->pf_encode_video = Encode;
352     p_enc->fmt_in.i_codec = VLC_FOURCC('I','4','2','0');
353     p_enc->fmt_in.video.i_bits_per_pixel = 12;
354     p_enc->fmt_out.i_codec = VLC_FOURCC('d','r','a','c');
355
356     config_ChainParse( p_enc, ENC_CFG_PREFIX, ppsz_enc_options, p_enc->p_cfg );
357
358     dirac_encoder_context_init( &p_sys->ctx, VIDEO_FORMAT_CUSTOM );
359     /* */
360     p_sys->ctx.seq_params.width = p_enc->fmt_in.video.i_width;
361     p_sys->ctx.seq_params.height = p_enc->fmt_in.video.i_height;
362     p_sys->ctx.seq_params.chroma = format420;
363     /* */
364     p_sys->ctx.src_params.frame_rate.numerator =
365         p_enc->fmt_in.video.i_frame_rate;
366     p_sys->ctx.src_params.frame_rate.denominator =
367         p_enc->fmt_in.video.i_frame_rate_base;
368     p_sys->ctx.src_params.interlace = 0;
369     p_sys->ctx.src_params.topfieldfirst = 0;
370
371     var_Get( p_enc, ENC_CFG_PREFIX "quality", &val );
372     f_quality = val.f_float;
373     if( f_quality > 10 ) f_quality = 10;
374     if( f_quality < 1 ) f_quality = 1;
375     p_sys->ctx.enc_params.qf = f_quality;
376
377     /* Initialise the encoder with the encoder context */
378     p_sys->p_dirac = dirac_encoder_init( &p_sys->ctx, 0 );
379
380     /* Set the buffer size for the encoded picture */
381     p_sys->i_buffer_in = p_enc->fmt_in.video.i_width *
382         p_enc->fmt_in.video.i_height * 3 / 2;
383     p_sys->p_buffer_in = malloc( p_sys->i_buffer_in );
384
385     return VLC_SUCCESS;
386 }
387
388 /****************************************************************************
389  * Encode: the whole thing
390  ****************************************************************************
391  * This function spits out ogg packets.
392  ****************************************************************************/
393 static block_t *Encode( encoder_t *p_enc, picture_t *p_pic )
394 {
395     encoder_sys_t *p_sys = p_enc->p_sys;
396     block_t *p_block, *p_chain = NULL;
397     int i_plane, i_line, i_width, i_src_stride;
398     uint8_t *p_dst;
399
400     /* Copy input picture in encoder input buffer (stride by stride) */
401     p_dst = p_sys->p_buffer_in;
402     for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
403     {
404         uint8_t *p_src = p_pic->p[i_plane].p_pixels;
405         i_width = p_pic->p[i_plane].i_visible_pitch;
406         i_src_stride = p_pic->p[i_plane].i_pitch;
407
408         for( i_line = 0; i_line < p_pic->p[i_plane].i_visible_lines; i_line++ )
409         {
410             p_enc->p_libvlc->pf_memcpy( p_dst, p_src, i_width );
411             p_dst += i_width;
412             p_src += i_src_stride;
413         }
414     }
415
416     /* Load one frame of data into encoder */
417     if( dirac_encoder_load( p_sys->p_dirac, p_sys->p_buffer_in,
418                             p_sys->i_buffer_in ) >= 0 )
419     {
420         dirac_encoder_state_t state;
421
422         msg_Dbg( p_enc, "dirac_encoder_load" );
423
424         /* Retrieve encoded frames from encoder */
425         do
426         {
427             p_sys->p_dirac->enc_buf.buffer = p_sys->p_buffer_out;
428             p_sys->p_dirac->enc_buf.size = ENC_BUFSIZE;
429             state = dirac_encoder_output( p_sys->p_dirac );
430             msg_Dbg( p_enc, "dirac_encoder_output: %i", state );
431             switch( state )
432             {
433             case ENC_STATE_AVAIL:
434                  // Encoded frame available in encoder->enc_buf
435                  // Encoded frame params available in enccoder->enc_fparams
436                  // Encoded frame stats available in enccoder->enc_fstats
437                  p_block = block_New( p_enc, p_sys->p_dirac->enc_buf.size );
438                  memcpy( p_block->p_buffer, p_sys->p_dirac->enc_buf.buffer,
439                          p_sys->p_dirac->enc_buf.size );
440                  p_block->i_dts = p_block->i_pts = p_pic->date;
441                  block_ChainAppend( &p_chain, p_block );
442
443                  break;
444             case ENC_STATE_BUFFER:
445                 break;
446             case ENC_STATE_INVALID:
447             default:
448                 break;
449             }
450             if( p_sys->p_dirac->decoded_frame_avail )
451             {
452                 //locally decoded frame is available in 
453                 //encoder->dec_buf
454                 //locally decoded frame parameters available
455                 //in encoder->dec_fparams
456             }
457             if( p_sys->p_dirac->instr_data_avail )
458             {
459                 //Instrumentation data (motion vectors etc.)
460                 //available in encoder->instr
461             }
462
463         } while( state == ENC_STATE_AVAIL );
464     }
465     else
466     {
467         msg_Dbg( p_enc, "dirac_encoder_load() error" );
468     }
469
470     return p_chain;
471 }
472
473 /*****************************************************************************
474  * CloseEncoder: dirac encoder destruction
475  *****************************************************************************/
476 static void CloseEncoder( vlc_object_t *p_this )
477 {
478     encoder_t *p_enc = (encoder_t *)p_this;
479     encoder_sys_t *p_sys = p_enc->p_sys;
480
481     msg_Dbg( p_enc, "resulting bit-rate: %i bits/sec",
482              p_sys->p_dirac->enc_seqstats.bit_rate );
483
484     /* Free the encoder resources */
485     dirac_encoder_close( p_sys->p_dirac );
486  
487     free( p_sys->p_buffer_in );
488     free( p_sys );
489 }