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_codec.h>
37 #include <libdirac_decoder/dirac_parser.h>
38 #include <libdirac_encoder/dirac_encoder.h>
40 /*****************************************************************************
41 * decoder_sys_t : theora decoder descriptor
42 *****************************************************************************/
48 dirac_decoder_t *p_dirac;
51 /*****************************************************************************
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 );
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 );
62 #define ENC_CFG_PREFIX "sout-dirac-"
64 static const char *ppsz_enc_options[] = {
68 /*****************************************************************************
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)." )
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" );
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 );
92 /*****************************************************************************
93 * OpenDecoder: probe the decoder and return score
94 *****************************************************************************/
95 static int OpenDecoder( vlc_object_t *p_this )
97 decoder_t *p_dec = (decoder_t*)p_this;
99 dirac_decoder_t *p_dirac;
101 if( p_dec->fmt_in.i_codec != VLC_FOURCC('d','r','a','c') )
106 /* Initialise the dirac decoder */
107 if( !(p_dirac = dirac_decoder_init(0)) ) return VLC_EGENERIC;
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 )
113 msg_Err( p_dec, "out of memory" );
117 p_sys->p_dirac = p_dirac;
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');
124 p_dec->pf_decode_video = DecodeBlock;
129 static void FreeFrameBuffer( dirac_decoder_t *p_dirac )
134 for( i = 0; i < 3; i++ )
136 free( p_dirac->fbuf->buf[i] );
137 p_dirac->fbuf->buf[i] = 0;
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 )
147 decoder_sys_t *p_sys = p_dec->p_sys;
151 switch( p_sys->p_dirac->src_params.chroma )
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 ?
157 p_dec->fmt_out.i_codec = 0;
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;
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;
172 /* Get a new picture */
173 p_pic = p_dec->pf_vout_buffer_new( p_dec );
175 if( p_pic == NULL ) return NULL;
176 p_pic->b_progressive = !p_sys->p_dirac->src_params.source_sampling;
177 p_pic->b_top_field_first = p_sys->p_dirac->src_params.topfieldfirst;
179 p_pic->i_nb_fields = 2;
181 /* Copy picture stride by stride */
182 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
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;
188 i_width = p_pic->p[i_plane].i_visible_pitch;
189 i_dst_stride = p_pic->p[i_plane].i_pitch;
191 for( i_line = 0; i_line < p_pic->p[i_plane].i_visible_lines; i_line++ )
193 p_dec->p_libvlc->pf_memcpy( p_dst, p_src, i_width );
195 p_dst += i_dst_stride;
202 /*****************************************************************************
203 * CloseDecoder: decoder destruction
204 *****************************************************************************/
205 static void CloseDecoder( vlc_object_t *p_this )
207 decoder_t *p_dec = (decoder_t *)p_this;
208 decoder_sys_t *p_sys = p_dec->p_sys;
210 FreeFrameBuffer( p_sys->p_dirac );
211 dirac_decoder_close( p_sys->p_dirac );
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 )
222 decoder_sys_t *p_sys = p_dec->p_sys;
223 dirac_decoder_state_t state;
227 if( !pp_block || !*pp_block ) return NULL;
233 state = dirac_parse( p_sys->p_dirac );
238 if( !p_block->i_buffer )
240 block_Release( p_block );
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 );
248 p_block->i_buffer = 0;
253 /* Initialize video output */
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 );
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 );
271 dirac_set_buf( p_sys->p_dirac, buf, NULL );
275 case STATE_SEQUENCE_END:
276 msg_Dbg( p_dec, "SEQUENCE_END" );
277 FreeFrameBuffer( p_sys->p_dirac );
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 );
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 );
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
299 msg_Dbg( p_dec, "STATE_INVALID" );
311 /*****************************************************************************
312 * encoder_sys_t : dirac encoder descriptor
313 *****************************************************************************/
314 #define ENC_BUFSIZE 1024*1024
320 dirac_encoder_t *p_dirac;
321 dirac_encoder_context_t ctx;
323 uint8_t *p_buffer_in;
326 uint8_t p_buffer_out[ENC_BUFSIZE];
329 /*****************************************************************************
330 * OpenEncoder: probe the encoder and return score
331 *****************************************************************************/
332 static int OpenEncoder( vlc_object_t *p_this )
334 encoder_t *p_enc = (encoder_t *)p_this;
335 encoder_sys_t *p_sys = p_enc->p_sys;
339 if( p_enc->fmt_out.i_codec != VLC_FOURCC('d','r','a','c') &&
345 /* Allocate the memory needed to store the decoder's structure */
346 if( ( p_sys = (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
348 msg_Err( p_enc, "out of memory" );
351 memset( p_sys, 0, sizeof(encoder_sys_t) );
352 p_enc->p_sys = p_sys;
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');
359 config_ChainParse( p_enc, ENC_CFG_PREFIX, ppsz_enc_options, p_enc->p_cfg );
361 dirac_encoder_context_init( &p_sys->ctx, VIDEO_FORMAT_CUSTOM );
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;
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.source_sampling = 0;
372 p_sys->ctx.src_params.topfieldfirst = 0;
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;
380 /* Initialise the encoder with the encoder context */
381 p_sys->p_dirac = dirac_encoder_init( &p_sys->ctx, 0 );
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 );
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 )
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;
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++ )
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;
411 for( i_line = 0; i_line < p_pic->p[i_plane].i_visible_lines; i_line++ )
413 p_enc->p_libvlc->pf_memcpy( p_dst, p_src, i_width );
415 p_src += i_src_stride;
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 )
423 dirac_encoder_state_t state;
425 msg_Dbg( p_enc, "dirac_encoder_load" );
427 /* Retrieve encoded frames from encoder */
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 );
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 );
447 case ENC_STATE_BUFFER:
449 case ENC_STATE_INVALID:
453 if( p_sys->p_dirac->decoded_frame_avail )
455 //locally decoded frame is available in
457 //locally decoded frame parameters available
458 //in encoder->dec_fparams
460 if( p_sys->p_dirac->instr_data_avail )
462 //Instrumentation data (motion vectors etc.)
463 //available in encoder->instr
466 } while( state == ENC_STATE_AVAIL );
470 msg_Dbg( p_enc, "dirac_encoder_load() error" );
476 /*****************************************************************************
477 * CloseEncoder: dirac encoder destruction
478 *****************************************************************************/
479 static void CloseEncoder( vlc_object_t *p_this )
481 encoder_t *p_enc = (encoder_t *)p_this;
482 encoder_sys_t *p_sys = p_enc->p_sys;
484 msg_Dbg( p_enc, "resulting bit-rate: %i bits/sec",
485 p_sys->p_dirac->enc_seqstats.bit_rate );
487 /* Free the encoder resources */
488 dirac_encoder_close( p_sys->p_dirac );
490 free( p_sys->p_buffer_in );