]> git.sesse.net Git - vlc/blob - modules/codec/dirac.c
74e2c1a14594262bab27496994f377c7ed4266a6
[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_float( ENC_CFG_PREFIX "quality", 7.0, NULL, ENC_QUALITY_TEXT,
84                ENC_QUALITY_LONGTEXT, VLC_FALSE );
85
86 vlc_module_end();
87
88 /*****************************************************************************
89  * OpenDecoder: probe the decoder and return score
90  *****************************************************************************/
91 static int OpenDecoder( vlc_object_t *p_this )
92 {
93     decoder_t *p_dec = (decoder_t*)p_this;
94     decoder_sys_t *p_sys;
95     dirac_decoder_t *p_dirac;
96
97     if( p_dec->fmt_in.i_codec != VLC_FOURCC('d','r','a','c') )
98     {
99         return VLC_EGENERIC;
100     }
101
102     /* Initialise the dirac decoder */
103     if( !(p_dirac = dirac_decoder_init(0)) ) return VLC_EGENERIC;
104
105     /* Allocate the memory needed to store the decoder's structure */
106     if( ( p_dec->p_sys = p_sys =
107           (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
108     {
109         msg_Err( p_dec, "out of memory" );
110         return VLC_EGENERIC;
111     }
112
113     p_sys->p_dirac = p_dirac;
114
115     /* Set output properties */
116     p_dec->fmt_out.i_cat = VIDEO_ES;
117     p_dec->fmt_out.i_codec = VLC_FOURCC('I','4','2','0');
118
119     /* Set callbacks */
120     p_dec->pf_decode_video = DecodeBlock;
121
122     return VLC_SUCCESS;
123 }
124
125 static void FreeFrameBuffer( dirac_decoder_t *p_dirac )
126 {
127     if( p_dirac->fbuf )
128     {
129         int i;
130         for( i = 0; i < 3; i++ )
131         {
132             if( p_dirac->fbuf->buf[i] ) free( p_dirac->fbuf->buf[i] );
133             p_dirac->fbuf->buf[i] = 0;
134         }
135     }
136 }
137
138 /*****************************************************************************
139  * GetNewPicture: Get a new picture from the vout and copy the decoder output
140  *****************************************************************************/
141 static picture_t *GetNewPicture( decoder_t *p_dec )
142 {
143     decoder_sys_t *p_sys = p_dec->p_sys;
144     picture_t *p_pic;
145     int i_plane;
146
147     switch( p_sys->p_dirac->seq_params.chroma )
148     {
149     case format420: p_dec->fmt_out.i_codec = VLC_FOURCC('I','4','2','0'); break;
150     case format422: p_dec->fmt_out.i_codec = VLC_FOURCC('I','4','2','2'); break;
151     case format444: p_dec->fmt_out.i_codec = VLC_FOURCC('I','4','4','4'); break;    // XXX 0.6 ?
152     default:        
153         p_dec->fmt_out.i_codec = 0;
154         break;
155     }
156
157     p_dec->fmt_out.video.i_visible_width =
158     p_dec->fmt_out.video.i_width = p_sys->p_dirac->seq_params.width;
159     p_dec->fmt_out.video.i_visible_height =
160     p_dec->fmt_out.video.i_height = p_sys->p_dirac->seq_params.height;
161     p_dec->fmt_out.video.i_aspect = VOUT_ASPECT_FACTOR * 4 / 3;
162
163     p_dec->fmt_out.video.i_frame_rate =
164         p_sys->p_dirac->src_params.frame_rate.numerator;
165     p_dec->fmt_out.video.i_frame_rate_base =
166         p_sys->p_dirac->src_params.frame_rate.denominator;
167
168     /* Get a new picture */
169     p_pic = p_dec->pf_vout_buffer_new( p_dec );
170
171     if( p_pic == NULL ) return NULL;
172     p_pic->b_progressive = !p_sys->p_dirac->src_params.interlace;
173     p_pic->b_top_field_first = p_sys->p_dirac->src_params.topfieldfirst;
174
175     p_pic->i_nb_fields = 2;
176
177     /* Copy picture stride by stride */
178     for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
179     {
180         int i_line, i_width, i_dst_stride;
181         uint8_t *p_src = p_sys->p_dirac->fbuf->buf[i_plane];
182         uint8_t *p_dst = p_pic->p[i_plane].p_pixels;
183
184         i_width = p_pic->p[i_plane].i_visible_pitch;
185         i_dst_stride = p_pic->p[i_plane].i_pitch;
186
187         for( i_line = 0; i_line < p_pic->p[i_plane].i_visible_lines; i_line++ )
188         {
189             p_dec->p_libvlc->pf_memcpy( p_dst, p_src, i_width );
190             p_src += i_width;
191             p_dst += i_dst_stride;
192         }
193     }
194
195     return p_pic;
196 }
197
198 /*****************************************************************************
199  * CloseDecoder: decoder destruction
200  *****************************************************************************/
201 static void CloseDecoder( vlc_object_t *p_this )
202 {
203     decoder_t *p_dec = (decoder_t *)p_this;
204     decoder_sys_t *p_sys = p_dec->p_sys;
205
206     FreeFrameBuffer( p_sys->p_dirac );
207     dirac_decoder_close( p_sys->p_dirac );
208     free( p_sys );
209 }
210
211 /****************************************************************************
212  * DecodeBlock: the whole thing
213  ****************************************************************************
214  * This function must be fed with complete frames.
215  ****************************************************************************/
216 static picture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
217 {
218     decoder_sys_t *p_sys = p_dec->p_sys;
219     dirac_decoder_state_t state;
220     picture_t *p_pic;
221     block_t *p_block;
222
223     if( !pp_block || !*pp_block ) return NULL;
224
225     p_block = *pp_block;
226
227     while( 1 )
228     {
229         state = dirac_parse( p_sys->p_dirac );
230
231         switch( state )
232         {
233         case STATE_BUFFER:
234             if( !p_block->i_buffer )
235             {
236                 block_Release( p_block );
237                 return NULL;
238             }
239
240             msg_Dbg( p_dec, "STATE_BUFFER" );
241             dirac_buffer( p_sys->p_dirac, p_block->p_buffer,
242                           p_block->p_buffer + p_block->i_buffer );
243
244             p_block->i_buffer = 0;
245             break;
246
247         case STATE_SEQUENCE:
248         {
249             /* Initialize video output */
250             uint8_t *buf[3];
251
252             msg_Dbg( p_dec, "%dx%d, chroma %i, %f fps",
253                      p_sys->p_dirac->seq_params.width,
254                      p_sys->p_dirac->seq_params.height,
255                      p_sys->p_dirac->seq_params.chroma,
256                      (float)p_sys->p_dirac->src_params.frame_rate.numerator/
257                      p_sys->p_dirac->src_params.frame_rate.denominator );
258
259             FreeFrameBuffer( p_sys->p_dirac );
260             buf[0] = malloc( p_sys->p_dirac->seq_params.width *
261                              p_sys->p_dirac->seq_params.height );
262             buf[1] = malloc( p_sys->p_dirac->seq_params.chroma_width *
263                              p_sys->p_dirac->seq_params.chroma_height );
264             buf[2] = malloc( p_sys->p_dirac->seq_params.chroma_width *
265                              p_sys->p_dirac->seq_params.chroma_height );
266
267             dirac_set_buf( p_sys->p_dirac, buf, NULL );
268             break;
269         }
270
271         case STATE_SEQUENCE_END:
272             msg_Dbg( p_dec, "SEQUENCE_END" );
273             FreeFrameBuffer( p_sys->p_dirac );
274             break;
275
276         case STATE_PICTURE_START:
277             msg_Dbg( p_dec, "PICTURE_START: frame_type=%i frame_num=%d",
278                      p_sys->p_dirac->frame_params.ftype,
279                      p_sys->p_dirac->frame_params.fnum );
280             break;
281
282         case STATE_PICTURE_AVAIL:
283             msg_Dbg( p_dec, "PICTURE_AVAI : frame_type=%i frame_num=%d",
284                      p_sys->p_dirac->frame_params.ftype,
285                      p_sys->p_dirac->frame_params.fnum );
286
287             /* Picture available for display */
288             p_pic = GetNewPicture( p_dec );
289             p_pic->date = p_block->i_pts > 0 ? p_block->i_pts : p_block->i_dts;
290             p_pic->b_force = 1; // HACK
291             return p_pic;
292             break;
293
294         case STATE_INVALID:
295             msg_Dbg( p_dec, "STATE_INVALID" );
296             break;
297
298         default:
299             break;
300         }
301     }
302
303     /* Never reached */
304     return NULL;
305 }
306
307 /*****************************************************************************
308  * encoder_sys_t : dirac encoder descriptor
309  *****************************************************************************/
310 #define ENC_BUFSIZE 1024*1024
311 struct encoder_sys_t
312 {
313     /*
314      * Dirac properties
315      */
316     dirac_encoder_t *p_dirac;
317     dirac_encoder_context_t ctx;
318
319     uint8_t *p_buffer_in;
320     int i_buffer_in;
321
322     uint8_t p_buffer_out[ENC_BUFSIZE];
323 };
324
325 /*****************************************************************************
326  * OpenEncoder: probe the encoder and return score
327  *****************************************************************************/
328 static int OpenEncoder( vlc_object_t *p_this )
329 {
330     encoder_t *p_enc = (encoder_t *)p_this;
331     encoder_sys_t *p_sys = p_enc->p_sys;
332     vlc_value_t val;
333     float f_quality;
334
335     if( p_enc->fmt_out.i_codec != VLC_FOURCC('d','r','a','c') &&
336         !p_enc->b_force )
337     {
338         return VLC_EGENERIC;
339     }
340
341     /* Allocate the memory needed to store the decoder's structure */
342     if( ( p_sys = (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
343     {
344         msg_Err( p_enc, "out of memory" );
345         return VLC_EGENERIC;
346     }
347     memset( p_sys, 0, sizeof(encoder_sys_t) );
348     p_enc->p_sys = p_sys;
349
350     p_enc->pf_encode_video = Encode;
351     p_enc->fmt_in.i_codec = VLC_FOURCC('I','4','2','0');
352     p_enc->fmt_in.video.i_bits_per_pixel = 12;
353     p_enc->fmt_out.i_codec = VLC_FOURCC('d','r','a','c');
354
355     config_ChainParse( p_enc, ENC_CFG_PREFIX, ppsz_enc_options, p_enc->p_cfg );
356
357     dirac_encoder_context_init( &p_sys->ctx, VIDEO_FORMAT_CUSTOM );
358     /* */
359     p_sys->ctx.seq_params.width = p_enc->fmt_in.video.i_width;
360     p_sys->ctx.seq_params.height = p_enc->fmt_in.video.i_height;
361     p_sys->ctx.seq_params.chroma = format420;
362     /* */
363     p_sys->ctx.src_params.frame_rate.numerator =
364         p_enc->fmt_in.video.i_frame_rate;
365     p_sys->ctx.src_params.frame_rate.denominator =
366         p_enc->fmt_in.video.i_frame_rate_base;
367     p_sys->ctx.src_params.interlace = 0;
368     p_sys->ctx.src_params.topfieldfirst = 0;
369
370     var_Get( p_enc, ENC_CFG_PREFIX "quality", &val );
371     f_quality = val.f_float;
372     if( f_quality > 10 ) f_quality = 10;
373     if( f_quality < 1 ) f_quality = 1;
374     p_sys->ctx.enc_params.qf = f_quality;
375
376     /* Initialise the encoder with the encoder context */
377     p_sys->p_dirac = dirac_encoder_init( &p_sys->ctx, 0 );
378
379     /* Set the buffer size for the encoded picture */
380     p_sys->i_buffer_in = p_enc->fmt_in.video.i_width *
381         p_enc->fmt_in.video.i_height * 3 / 2;
382     p_sys->p_buffer_in = malloc( p_sys->i_buffer_in );
383
384     return VLC_SUCCESS;
385 }
386
387 /****************************************************************************
388  * Encode: the whole thing
389  ****************************************************************************
390  * This function spits out ogg packets.
391  ****************************************************************************/
392 static block_t *Encode( encoder_t *p_enc, picture_t *p_pic )
393 {
394     encoder_sys_t *p_sys = p_enc->p_sys;
395     block_t *p_block, *p_chain = NULL;
396     int i_plane, i_line, i_width, i_src_stride;
397     uint8_t *p_dst;
398
399     /* Copy input picture in encoder input buffer (stride by stride) */
400     p_dst = p_sys->p_buffer_in;
401     for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
402     {
403         uint8_t *p_src = p_pic->p[i_plane].p_pixels;
404         i_width = p_pic->p[i_plane].i_visible_pitch;
405         i_src_stride = p_pic->p[i_plane].i_pitch;
406
407         for( i_line = 0; i_line < p_pic->p[i_plane].i_visible_lines; i_line++ )
408         {
409             p_enc->p_libvlc->pf_memcpy( p_dst, p_src, i_width );
410             p_dst += i_width;
411             p_src += i_src_stride;
412         }
413     }
414
415     /* Load one frame of data into encoder */
416     if( dirac_encoder_load( p_sys->p_dirac, p_sys->p_buffer_in,
417                             p_sys->i_buffer_in ) >= 0 )
418     {
419         dirac_encoder_state_t state;
420
421         msg_Dbg( p_enc, "dirac_encoder_load" );
422
423         /* Retrieve encoded frames from encoder */
424         do
425         {
426             p_sys->p_dirac->enc_buf.buffer = p_sys->p_buffer_out;
427             p_sys->p_dirac->enc_buf.size = ENC_BUFSIZE;
428             state = dirac_encoder_output( p_sys->p_dirac );
429             msg_Dbg( p_enc, "dirac_encoder_output: %i", state );
430             switch( state )
431             {
432             case ENC_STATE_AVAIL:
433                  // Encoded frame available in encoder->enc_buf
434                  // Encoded frame params available in enccoder->enc_fparams
435                  // Encoded frame stats available in enccoder->enc_fstats
436                  p_block = block_New( p_enc, p_sys->p_dirac->enc_buf.size );
437                  memcpy( p_block->p_buffer, p_sys->p_dirac->enc_buf.buffer,
438                          p_sys->p_dirac->enc_buf.size );
439                  p_block->i_dts = p_block->i_pts = p_pic->date;
440                  block_ChainAppend( &p_chain, p_block );
441
442                  break;
443             case ENC_STATE_BUFFER:
444                 break;
445             case ENC_STATE_INVALID:
446             default:
447                 break;
448             }
449             if( p_sys->p_dirac->decoded_frame_avail )
450             {
451                 //locally decoded frame is available in 
452                 //encoder->dec_buf
453                 //locally decoded frame parameters available
454                 //in encoder->dec_fparams
455             }
456             if( p_sys->p_dirac->instr_data_avail )
457             {
458                 //Instrumentation data (motion vectors etc.)
459                 //available in encoder->instr
460             }
461
462         } while( state == ENC_STATE_AVAIL );
463     }
464     else
465     {
466         msg_Dbg( p_enc, "dirac_encoder_load() error" );
467     }
468
469     return p_chain;
470 }
471
472 /*****************************************************************************
473  * CloseEncoder: dirac encoder destruction
474  *****************************************************************************/
475 static void CloseEncoder( vlc_object_t *p_this )
476 {
477     encoder_t *p_enc = (encoder_t *)p_this;
478     encoder_sys_t *p_sys = p_enc->p_sys;
479
480     msg_Dbg( p_enc, "resulting bit-rate: %i bits/sec",
481              p_sys->p_dirac->enc_seqstats.bit_rate );
482
483     /* Free the encoder resources */
484     dirac_encoder_close( p_sys->p_dirac );
485  
486     free( p_sys->p_buffer_in );
487     free( p_sys );
488 }