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
8 * Authors: Gildas Bazin <gbazin@videolan.org>
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.
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.
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 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
33 #include <vlc_plugin.h>
34 #include <vlc_codec.h>
38 #include <libdirac_decoder/dirac_parser.h>
39 #include <libdirac_encoder/dirac_encoder.h>
41 /*****************************************************************************
42 * decoder_sys_t : theora decoder descriptor
43 *****************************************************************************/
49 dirac_decoder_t *p_dirac;
52 /*****************************************************************************
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 );
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 );
63 #define ENC_CFG_PREFIX "sout-dirac-"
65 static const char *ppsz_enc_options[] = {
69 /*****************************************************************************
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)." )
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" );
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 );
93 /*****************************************************************************
94 * OpenDecoder: probe the decoder and return score
95 *****************************************************************************/
96 static int OpenDecoder( vlc_object_t *p_this )
98 decoder_t *p_dec = (decoder_t*)p_this;
100 dirac_decoder_t *p_dirac;
102 if( p_dec->fmt_in.i_codec != VLC_FOURCC('d','r','a','c') )
107 /* Initialise the dirac decoder */
108 if( !(p_dirac = dirac_decoder_init(0)) ) return VLC_EGENERIC;
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 )
114 msg_Err( p_dec, "out of memory" );
118 p_sys->p_dirac = p_dirac;
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');
125 p_dec->pf_decode_video = DecodeBlock;
130 static void FreeFrameBuffer( dirac_decoder_t *p_dirac )
135 for( i = 0; i < 3; i++ )
137 free( p_dirac->fbuf->buf[i] );
138 p_dirac->fbuf->buf[i] = 0;
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 )
148 decoder_sys_t *p_sys = p_dec->p_sys;
152 switch( p_sys->p_dirac->src_params.chroma )
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 ?
158 p_dec->fmt_out.i_codec = 0;
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;
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;
173 /* Get a new picture */
174 p_pic = p_dec->pf_vout_buffer_new( p_dec );
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;
180 p_pic->i_nb_fields = 2;
182 /* Copy picture stride by stride */
183 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
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;
189 i_width = p_pic->p[i_plane].i_visible_pitch;
190 i_dst_stride = p_pic->p[i_plane].i_pitch;
192 for( i_line = 0; i_line < p_pic->p[i_plane].i_visible_lines; i_line++ )
194 vlc_memcpy( p_dst, p_src, i_width );
196 p_dst += i_dst_stride;
203 /*****************************************************************************
204 * CloseDecoder: decoder destruction
205 *****************************************************************************/
206 static void CloseDecoder( vlc_object_t *p_this )
208 decoder_t *p_dec = (decoder_t *)p_this;
209 decoder_sys_t *p_sys = p_dec->p_sys;
211 FreeFrameBuffer( p_sys->p_dirac );
212 dirac_decoder_close( p_sys->p_dirac );
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 )
223 decoder_sys_t *p_sys = p_dec->p_sys;
224 dirac_decoder_state_t state;
228 if( !pp_block || !*pp_block ) return NULL;
234 state = dirac_parse( p_sys->p_dirac );
239 if( !p_block->i_buffer )
241 block_Release( p_block );
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 );
249 p_block->i_buffer = 0;
254 /* Initialize video output */
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 );
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 );
272 dirac_set_buf( p_sys->p_dirac, buf, NULL );
276 case STATE_SEQUENCE_END:
277 msg_Dbg( p_dec, "SEQUENCE_END" );
278 FreeFrameBuffer( p_sys->p_dirac );
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 );
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 );
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
300 msg_Dbg( p_dec, "STATE_INVALID" );
312 /*****************************************************************************
313 * encoder_sys_t : dirac encoder descriptor
314 *****************************************************************************/
315 #define ENC_BUFSIZE 1024*1024
321 dirac_encoder_t *p_dirac;
322 dirac_encoder_context_t ctx;
324 uint8_t *p_buffer_in;
327 uint8_t p_buffer_out[ENC_BUFSIZE];
330 /*****************************************************************************
331 * OpenEncoder: probe the encoder and return score
332 *****************************************************************************/
333 static int OpenEncoder( vlc_object_t *p_this )
335 encoder_t *p_enc = (encoder_t *)p_this;
336 encoder_sys_t *p_sys = p_enc->p_sys;
340 if( p_enc->fmt_out.i_codec != VLC_FOURCC('d','r','a','c') &&
346 /* Allocate the memory needed to store the decoder's structure */
347 if( ( p_sys = (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
349 msg_Err( p_enc, "out of memory" );
352 memset( p_sys, 0, sizeof(encoder_sys_t) );
353 p_enc->p_sys = p_sys;
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');
360 config_ChainParse( p_enc, ENC_CFG_PREFIX, ppsz_enc_options, p_enc->p_cfg );
362 dirac_encoder_context_init( &p_sys->ctx, VIDEO_FORMAT_CUSTOM );
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;
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;
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;
381 /* Initialise the encoder with the encoder context */
382 p_sys->p_dirac = dirac_encoder_init( &p_sys->ctx, 0 );
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 );
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 )
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;
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++ )
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;
412 for( i_line = 0; i_line < p_pic->p[i_plane].i_visible_lines; i_line++ )
414 vlc_memcpy( p_dst, p_src, i_width );
416 p_src += i_src_stride;
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 )
424 dirac_encoder_state_t state;
426 msg_Dbg( p_enc, "dirac_encoder_load" );
428 /* Retrieve encoded frames from encoder */
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 );
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 );
448 case ENC_STATE_BUFFER:
450 case ENC_STATE_INVALID:
454 if( p_sys->p_dirac->decoded_frame_avail )
456 //locally decoded frame is available in
458 //locally decoded frame parameters available
459 //in encoder->dec_fparams
461 if( p_sys->p_dirac->instr_data_avail )
463 //Instrumentation data (motion vectors etc.)
464 //available in encoder->instr
467 } while( state == ENC_STATE_AVAIL );
471 msg_Dbg( p_enc, "dirac_encoder_load() error" );
477 /*****************************************************************************
478 * CloseEncoder: dirac encoder destruction
479 *****************************************************************************/
480 static void CloseEncoder( vlc_object_t *p_this )
482 encoder_t *p_enc = (encoder_t *)p_this;
483 encoder_sys_t *p_sys = p_enc->p_sys;
485 msg_Dbg( p_enc, "resulting bit-rate: %i bits/sec",
486 p_sys->p_dirac->enc_seqstats.bit_rate );
488 /* Free the encoder resources */
489 dirac_encoder_close( p_sys->p_dirac );
491 free( p_sys->p_buffer_in );