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 *****************************************************************************/
32 #include <vlc_common.h>
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 *const 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( N_("Dirac video decoder") );
80 set_capability( "decoder", 100 );
81 set_callbacks( OpenDecoder, CloseDecoder );
82 add_shortcut( "dirac" );
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 );
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 )
115 p_sys->p_dirac = p_dirac;
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');
122 p_dec->pf_decode_video = DecodeBlock;
127 static void FreeFrameBuffer( dirac_decoder_t *p_dirac )
132 for( i = 0; i < 3; i++ )
134 free( p_dirac->fbuf->buf[i] );
135 p_dirac->fbuf->buf[i] = 0;
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 )
145 decoder_sys_t *p_sys = p_dec->p_sys;
149 switch( p_sys->p_dirac->src_params.chroma )
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 ?
155 p_dec->fmt_out.i_codec = 0;
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;
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;
170 /* Get a new picture */
171 p_pic = p_dec->pf_vout_buffer_new( p_dec );
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;
177 p_pic->i_nb_fields = 2;
179 /* Copy picture stride by stride */
180 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
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;
186 i_width = p_pic->p[i_plane].i_visible_pitch;
187 i_dst_stride = p_pic->p[i_plane].i_pitch;
189 for( i_line = 0; i_line < p_pic->p[i_plane].i_visible_lines; i_line++ )
191 vlc_memcpy( p_dst, p_src, i_width );
193 p_dst += i_dst_stride;
200 /*****************************************************************************
201 * CloseDecoder: decoder destruction
202 *****************************************************************************/
203 static void CloseDecoder( vlc_object_t *p_this )
205 decoder_t *p_dec = (decoder_t *)p_this;
206 decoder_sys_t *p_sys = p_dec->p_sys;
208 FreeFrameBuffer( p_sys->p_dirac );
209 dirac_decoder_close( p_sys->p_dirac );
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 )
220 decoder_sys_t *p_sys = p_dec->p_sys;
221 dirac_decoder_state_t state;
225 if( !pp_block || !*pp_block ) return NULL;
231 state = dirac_parse( p_sys->p_dirac );
236 if( !p_block->i_buffer )
238 block_Release( p_block );
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 );
246 p_block->i_buffer = 0;
251 /* Initialize video output */
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 );
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 );
269 dirac_set_buf( p_sys->p_dirac, buf, NULL );
273 case STATE_SEQUENCE_END:
274 msg_Dbg( p_dec, "SEQUENCE_END" );
275 FreeFrameBuffer( p_sys->p_dirac );
278 case STATE_PICTURE_AVAIL:
279 msg_Dbg( p_dec, "PICTURE_AVAIL : frame_num=%d",
280 p_sys->p_dirac->frame_num );
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
290 msg_Dbg( p_dec, "STATE_INVALID" );
302 /*****************************************************************************
303 * encoder_sys_t : dirac encoder descriptor
304 *****************************************************************************/
305 #define ENC_BUFSIZE 1024*1024
311 dirac_encoder_t *p_dirac;
312 dirac_encoder_context_t ctx;
314 uint8_t *p_buffer_in;
317 uint8_t p_buffer_out[ENC_BUFSIZE];
320 /*****************************************************************************
321 * OpenEncoder: probe the encoder and return score
322 *****************************************************************************/
323 static int OpenEncoder( vlc_object_t *p_this )
325 encoder_t *p_enc = (encoder_t *)p_this;
326 encoder_sys_t *p_sys = p_enc->p_sys;
330 if( p_enc->fmt_out.i_codec != VLC_FOURCC('d','r','a','c') &&
336 /* Allocate the memory needed to store the decoder's structure */
337 if( ( p_sys = (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
339 memset( p_sys, 0, sizeof(encoder_sys_t) );
340 p_enc->p_sys = p_sys;
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');
347 config_ChainParse( p_enc, ENC_CFG_PREFIX, ppsz_enc_options, p_enc->p_cfg );
349 dirac_encoder_context_init( &p_sys->ctx, VIDEO_FORMAT_CUSTOM );
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;
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;
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;
368 /* Initialise the encoder with the encoder context */
369 p_sys->p_dirac = dirac_encoder_init( &p_sys->ctx, 0 );
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 );
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 )
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;
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++ )
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;
399 for( i_line = 0; i_line < p_pic->p[i_plane].i_visible_lines; i_line++ )
401 vlc_memcpy( p_dst, p_src, i_width );
403 p_src += i_src_stride;
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 )
411 dirac_encoder_state_t state;
413 msg_Dbg( p_enc, "dirac_encoder_load" );
415 /* Retrieve encoded frames from encoder */
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 );
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 );
435 case ENC_STATE_BUFFER:
437 case ENC_STATE_INVALID:
441 if( p_sys->p_dirac->decoded_frame_avail )
443 //locally decoded frame is available in
445 //locally decoded frame parameters available
446 //in encoder->dec_fparams
448 if( p_sys->p_dirac->instr_data_avail )
450 //Instrumentation data (motion vectors etc.)
451 //available in encoder->instr
454 } while( state == ENC_STATE_AVAIL );
458 msg_Dbg( p_enc, "dirac_encoder_load() error" );
464 /*****************************************************************************
465 * CloseEncoder: dirac encoder destruction
466 *****************************************************************************/
467 static void CloseEncoder( vlc_object_t *p_this )
469 encoder_t *p_enc = (encoder_t *)p_this;
470 encoder_sys_t *p_sys = p_enc->p_sys;
472 msg_Dbg( p_enc, "resulting bit-rate: %lld bits/sec",
473 p_sys->p_dirac->enc_seqstats.bit_rate );
475 /* Free the encoder resources */
476 dirac_encoder_close( p_sys->p_dirac );
478 free( p_sys->p_buffer_in );