]> git.sesse.net Git - vlc/blob - modules/codec/dirac.c
Remove most stray semi-colons in module descriptions
[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_common.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 = decoder_NewPicture( 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_AVAIL:
279             msg_Dbg( p_dec, "PICTURE_AVAIL : frame_num=%d",
280                      p_sys->p_dirac->frame_num );
281
282             /* Picture available for display */
283             p_pic = GetNewPicture( p_dec );
284             p_pic->date = p_block->i_pts > 0 ? p_block->i_pts : p_block->i_dts;
285             p_pic->b_force = 1; // HACK
286             return p_pic;
287             break;
288
289         case STATE_INVALID:
290             msg_Dbg( p_dec, "STATE_INVALID" );
291             break;
292
293         default:
294             break;
295         }
296     }
297
298     /* Never reached */
299     return NULL;
300 }
301
302 /*****************************************************************************
303  * encoder_sys_t : dirac encoder descriptor
304  *****************************************************************************/
305 #define ENC_BUFSIZE 1024*1024
306 struct encoder_sys_t
307 {
308     /*
309      * Dirac properties
310      */
311     dirac_encoder_t *p_dirac;
312     dirac_encoder_context_t ctx;
313
314     uint8_t *p_buffer_in;
315     int i_buffer_in;
316
317     uint8_t p_buffer_out[ENC_BUFSIZE];
318 };
319
320 /*****************************************************************************
321  * OpenEncoder: probe the encoder and return score
322  *****************************************************************************/
323 static int OpenEncoder( vlc_object_t *p_this )
324 {
325     encoder_t *p_enc = (encoder_t *)p_this;
326     encoder_sys_t *p_sys = p_enc->p_sys;
327     vlc_value_t val;
328     float f_quality;
329
330     if( p_enc->fmt_out.i_codec != VLC_FOURCC('d','r','a','c') &&
331         !p_enc->b_force )
332     {
333         return VLC_EGENERIC;
334     }
335
336     /* Allocate the memory needed to store the decoder's structure */
337     if( ( p_sys = (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
338         return VLC_ENOMEM;
339     memset( p_sys, 0, sizeof(encoder_sys_t) );
340     p_enc->p_sys = p_sys;
341
342     p_enc->pf_encode_video = Encode;
343     p_enc->fmt_in.i_codec = VLC_FOURCC('I','4','2','0');
344     p_enc->fmt_in.video.i_bits_per_pixel = 12;
345     p_enc->fmt_out.i_codec = VLC_FOURCC('d','r','a','c');
346
347     config_ChainParse( p_enc, ENC_CFG_PREFIX, ppsz_enc_options, p_enc->p_cfg );
348
349     dirac_encoder_context_init( &p_sys->ctx, VIDEO_FORMAT_CUSTOM );
350     /* */
351     p_sys->ctx.src_params.width = p_enc->fmt_in.video.i_width;
352     p_sys->ctx.src_params.height = p_enc->fmt_in.video.i_height;
353     p_sys->ctx.src_params.chroma = format420;
354     /* */
355     p_sys->ctx.src_params.frame_rate.numerator =
356         p_enc->fmt_in.video.i_frame_rate;
357     p_sys->ctx.src_params.frame_rate.denominator =
358         p_enc->fmt_in.video.i_frame_rate_base;
359     p_sys->ctx.src_params.source_sampling = 0;
360     p_sys->ctx.src_params.topfieldfirst = 0;
361
362     var_Get( p_enc, ENC_CFG_PREFIX "quality", &val );
363     f_quality = val.f_float;
364     if( f_quality > 10 ) f_quality = 10;
365     if( f_quality < 1 ) f_quality = 1;
366     p_sys->ctx.enc_params.qf = f_quality;
367
368     /* Initialise the encoder with the encoder context */
369     p_sys->p_dirac = dirac_encoder_init( &p_sys->ctx, 0 );
370
371     /* Set the buffer size for the encoded picture */
372     p_sys->i_buffer_in = p_enc->fmt_in.video.i_width *
373         p_enc->fmt_in.video.i_height * 3 / 2;
374     p_sys->p_buffer_in = malloc( p_sys->i_buffer_in );
375
376     return VLC_SUCCESS;
377 }
378
379 /****************************************************************************
380  * Encode: the whole thing
381  ****************************************************************************
382  * This function spits out ogg packets.
383  ****************************************************************************/
384 static block_t *Encode( encoder_t *p_enc, picture_t *p_pic )
385 {
386     encoder_sys_t *p_sys = p_enc->p_sys;
387     block_t *p_block, *p_chain = NULL;
388     int i_plane, i_line, i_width, i_src_stride;
389     uint8_t *p_dst;
390
391     /* Copy input picture in encoder input buffer (stride by stride) */
392     p_dst = p_sys->p_buffer_in;
393     for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
394     {
395         uint8_t *p_src = p_pic->p[i_plane].p_pixels;
396         i_width = p_pic->p[i_plane].i_visible_pitch;
397         i_src_stride = p_pic->p[i_plane].i_pitch;
398
399         for( i_line = 0; i_line < p_pic->p[i_plane].i_visible_lines; i_line++ )
400         {
401             vlc_memcpy( p_dst, p_src, i_width );
402             p_dst += i_width;
403             p_src += i_src_stride;
404         }
405     }
406
407     /* Load one frame of data into encoder */
408     if( dirac_encoder_load( p_sys->p_dirac, p_sys->p_buffer_in,
409                             p_sys->i_buffer_in ) >= 0 )
410     {
411         dirac_encoder_state_t state;
412
413         msg_Dbg( p_enc, "dirac_encoder_load" );
414
415         /* Retrieve encoded frames from encoder */
416         do
417         {
418             p_sys->p_dirac->enc_buf.buffer = p_sys->p_buffer_out;
419             p_sys->p_dirac->enc_buf.size = ENC_BUFSIZE;
420             state = dirac_encoder_output( p_sys->p_dirac );
421             msg_Dbg( p_enc, "dirac_encoder_output: %i", state );
422             switch( state )
423             {
424             case ENC_STATE_AVAIL:
425                  // Encoded frame available in encoder->enc_buf
426                  // Encoded frame params available in enccoder->enc_fparams
427                  // Encoded frame stats available in enccoder->enc_fstats
428                  p_block = block_New( p_enc, p_sys->p_dirac->enc_buf.size );
429                  memcpy( p_block->p_buffer, p_sys->p_dirac->enc_buf.buffer,
430                          p_sys->p_dirac->enc_buf.size );
431                  p_block->i_dts = p_block->i_pts = p_pic->date;
432                  block_ChainAppend( &p_chain, p_block );
433
434                  break;
435             case ENC_STATE_BUFFER:
436                 break;
437             case ENC_STATE_INVALID:
438             default:
439                 break;
440             }
441             if( p_sys->p_dirac->decoded_frame_avail )
442             {
443                 //locally decoded frame is available in
444                 //encoder->dec_buf
445                 //locally decoded frame parameters available
446                 //in encoder->dec_fparams
447             }
448             if( p_sys->p_dirac->instr_data_avail )
449             {
450                 //Instrumentation data (motion vectors etc.)
451                 //available in encoder->instr
452             }
453
454         } while( state == ENC_STATE_AVAIL );
455     }
456     else
457     {
458         msg_Dbg( p_enc, "dirac_encoder_load() error" );
459     }
460
461     return p_chain;
462 }
463
464 /*****************************************************************************
465  * CloseEncoder: dirac encoder destruction
466  *****************************************************************************/
467 static void CloseEncoder( vlc_object_t *p_this )
468 {
469     encoder_t *p_enc = (encoder_t *)p_this;
470     encoder_sys_t *p_sys = p_enc->p_sys;
471
472     msg_Dbg( p_enc, "resulting bit-rate: %lld bits/sec",
473              p_sys->p_dirac->enc_seqstats.bit_rate );
474
475     /* Free the encoder resources */
476     dirac_encoder_close( p_sys->p_dirac );
477  
478     free( p_sys->p_buffer_in );
479     free( p_sys );
480 }