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