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