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