1 /*****************************************************************************
2 * schroedinger.c: Dirac decoder module making use of libschroedinger.
3 * (http://www.bbc.co.uk/rd/projects/dirac/index.shtml)
4 * (http://diracvideo.org)
5 *****************************************************************************
6 * Copyright (C) 2008 the VideoLAN team
9 * Authors: Jonathan Rosser <jonathan.rosser@gmail.com>
10 * David Flynn <davidf at rd dot bbc.co.uk>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25 *****************************************************************************/
27 /*****************************************************************************
29 *****************************************************************************/
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
36 #include <vlc_codec.h>
40 #include <schroedinger/schro.h>
42 /*****************************************************************************
43 * picture_pts_t : store pts alongside picture number, not carried through
45 *****************************************************************************/
48 int i_empty; //not in use
49 uint32_t u_pnum; //picture number from dirac header
50 mtime_t i_pts; //pts for this picture
53 /*****************************************************************************
54 * decoder_sys_t : Schroedinger decoder descriptor
55 *****************************************************************************/
56 #define PTS_TLB_SIZE 16
63 mtime_t i_frame_pts_delta;
64 SchroDecoder *p_schro;
65 SchroVideoFormat *p_format;
66 struct picture_pts_t pts_tlb[PTS_TLB_SIZE];
70 /*****************************************************************************
72 *****************************************************************************/
73 static int OpenDecoder ( vlc_object_t * );
74 static void CloseDecoder ( vlc_object_t * );
75 static picture_t *DecodeBlock ( decoder_t *p_dec, block_t **pp_block );
77 /*****************************************************************************
79 *****************************************************************************/
82 set_category( CAT_INPUT );
83 set_subcategory( SUBCAT_INPUT_VCODEC );
84 set_description( N_("Schroedinger video decoder") );
85 set_capability( "decoder", 200 );
86 set_callbacks( OpenDecoder, CloseDecoder );
87 add_shortcut( "schroedinger" );
90 /*****************************************************************************
91 * ResetPTStlb: Purge all entries in @p_dec@'s PTS-tlb
92 *****************************************************************************/
93 static void ResetPTStlb( decoder_t *p_dec )
95 decoder_sys_t *p_sys = p_dec->p_sys;
96 for( int i=0; i<PTS_TLB_SIZE; i++) {
97 p_sys->pts_tlb[i].i_empty = 1;
101 /*****************************************************************************
102 * OpenDecoder: probe the decoder and return score
103 *****************************************************************************/
104 static int OpenDecoder( vlc_object_t *p_this )
106 decoder_t *p_dec = (decoder_t*)p_this;
107 decoder_sys_t *p_sys;
108 SchroDecoder *p_schro;
110 if( p_dec->fmt_in.i_codec != VLC_FOURCC('d','r','a','c') )
115 /* Initialise the schroedinger (and hence liboil libraries */
116 /* This does no allocation and is safe to call */
119 /* Initialise the schroedinger decoder */
120 if( !(p_schro = schro_decoder_new()) ) return VLC_EGENERIC;
122 /* Allocate the memory needed to store the decoder's structure */
123 if( ( p_dec->p_sys = p_sys =
124 (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
127 p_sys->p_schro = p_schro;
128 p_sys->p_format = NULL;
129 p_sys->i_lastpts = -1;
130 p_sys->i_frame_pts_delta = 0;
131 p_sys->i_ts_resync_hack = 0;
135 /* Set output properties */
136 p_dec->fmt_out.i_cat = VIDEO_ES;
137 p_dec->fmt_out.i_codec = VLC_FOURCC('I','4','2','0');
140 p_dec->pf_decode_video = DecodeBlock;
145 /*****************************************************************************
146 * SetPictureFormat: Set the decoded picture params to the ones from the stream
147 *****************************************************************************/
148 static void SetVideoFormat( decoder_t *p_dec )
150 decoder_sys_t *p_sys = p_dec->p_sys;
153 p_sys->p_format = schro_decoder_get_video_format(p_sys->p_schro);
154 if( p_sys->p_format == NULL ) return;
156 p_sys->i_frame_pts_delta = INT64_C(1000000)
157 * p_sys->p_format->frame_rate_denominator
158 / p_sys->p_format->frame_rate_numerator;
160 switch( p_sys->p_format->chroma_format )
162 case SCHRO_CHROMA_420: p_dec->fmt_out.i_codec = VLC_FOURCC('I','4','2','0'); break;
163 case SCHRO_CHROMA_422: p_dec->fmt_out.i_codec = VLC_FOURCC('I','4','2','2'); break;
164 case SCHRO_CHROMA_444: p_dec->fmt_out.i_codec = VLC_FOURCC('I','4','4','4'); break;
166 p_dec->fmt_out.i_codec = 0;
170 p_dec->fmt_out.video.i_visible_width =
171 p_dec->fmt_out.video.i_width = p_sys->p_format->width;
173 p_dec->fmt_out.video.i_visible_height =
174 p_dec->fmt_out.video.i_height = p_sys->p_format->height;
176 /* aspect_ratio_[numerator|denominator] describes the pixel aspect ratio */
178 ( p_sys->p_format->aspect_ratio_numerator * p_sys->p_format->width ) /
179 ( p_sys->p_format->aspect_ratio_denominator * p_sys->p_format->height);
181 p_dec->fmt_out.video.i_aspect = VOUT_ASPECT_FACTOR * f_aspect;
183 p_dec->fmt_out.video.i_frame_rate =
184 p_sys->p_format->frame_rate_numerator;
185 p_dec->fmt_out.video.i_frame_rate_base =
186 p_sys->p_format->frame_rate_denominator;
189 /*****************************************************************************
190 * StorePicturePTS: Store the PTS value for a particular picture number
191 *****************************************************************************/
192 static void StorePicturePTS( decoder_t *p_dec, block_t *p_block, int i_pupos )
194 decoder_sys_t *p_sys = p_dec->p_sys;
198 u_pnum = GetDWBE( p_block->p_buffer + i_pupos + 13 );
199 i_pts = p_block->i_pts > 0 ? p_block->i_pts : p_block->i_dts;
201 for( int i=0; i<PTS_TLB_SIZE; i++ ) {
202 if( p_sys->pts_tlb[i].i_empty ) {
204 p_sys->pts_tlb[i].u_pnum = u_pnum;
205 p_sys->pts_tlb[i].i_pts = i_pts;
206 p_sys->pts_tlb[i].i_empty = 0;
212 msg_Err( p_dec, "Could not store PTS %"PRId64" for picture %u",
216 /*****************************************************************************
217 * GetPicturePTS: Retrieve the PTS value for a particular picture number
218 *****************************************************************************/
219 static mtime_t GetPicturePTS( decoder_t *p_dec, uint32_t u_pnum )
221 decoder_sys_t *p_sys = p_dec->p_sys;
223 for( int i=0; i<PTS_TLB_SIZE; i++ ) {
224 if( (!p_sys->pts_tlb[i].i_empty) &&
225 (p_sys->pts_tlb[i].u_pnum == u_pnum)) {
227 p_sys->pts_tlb[i].i_empty = 1;
228 return p_sys->pts_tlb[i].i_pts;
232 msg_Err( p_dec, "Could not retrieve PTS for picture %u", u_pnum );
236 /*****************************************************************************
237 * SchroFrameFree: schro_frame callback to release the associated picture_t
238 * When schro_decoder_reset() is called there will be pictures in the
239 * decoding pipeline that need to be released rather than displayed.
240 *****************************************************************************/
241 static void SchroFrameFree( SchroFrame *frame, void *priv)
243 picture_t *p_pic = priv;
248 if( p_pic->pf_release ) p_pic->pf_release( p_pic );
251 /*****************************************************************************
252 * CreateSchroFrameFromPic: wrap a picture_t in a SchroFrame
253 *****************************************************************************/
254 static SchroFrame *CreateSchroFrameFromPic( decoder_t *p_dec )
256 decoder_sys_t *p_sys = p_dec->p_sys;
257 SchroFrame *p_schroframe = schro_frame_new();
258 picture_t *p_pic = NULL;
263 p_pic = p_dec->pf_vout_buffer_new( p_dec );
268 p_schroframe->format = SCHRO_FRAME_FORMAT_U8_420;
269 if( p_sys->p_format->chroma_format == SCHRO_CHROMA_422 )
271 p_schroframe->format = SCHRO_FRAME_FORMAT_U8_422;
273 else if( p_sys->p_format->chroma_format == SCHRO_CHROMA_444 )
275 p_schroframe->format = SCHRO_FRAME_FORMAT_U8_444;
278 p_schroframe->width = p_sys->p_format->width;
279 p_schroframe->height = p_sys->p_format->height;
280 schro_frame_set_free_callback( p_schroframe, SchroFrameFree, p_pic );
282 for( int i=0; i<3; i++ )
284 p_schroframe->components[i].width = p_pic->p[i].i_visible_pitch;
285 p_schroframe->components[i].stride = p_pic->p[i].i_pitch;
286 p_schroframe->components[i].height = p_pic->p[i].i_visible_lines;
287 p_schroframe->components[i].length =
288 p_pic->p[i].i_pitch * p_pic->p[i].i_lines;
289 p_schroframe->components[i].data = p_pic->p[i].p_pixels;
293 p_schroframe->components[i].v_shift =
294 SCHRO_FRAME_FORMAT_V_SHIFT( p_schroframe->format );
295 p_schroframe->components[i].h_shift =
296 SCHRO_FRAME_FORMAT_H_SHIFT( p_schroframe->format );
300 p_pic->b_progressive = !p_sys->p_format->interlaced;
301 p_pic->b_top_field_first = p_sys->p_format->top_field_first;
302 p_pic->i_nb_fields = 2;
307 /*****************************************************************************
308 * SchroBufferFree: schro_buffer callback to release the associated block_t
309 *****************************************************************************/
310 static void SchroBufferFree( SchroBuffer *buf, void *priv )
312 block_t *p_block = priv;
317 block_Release( p_block );
320 /*****************************************************************************
321 * CloseDecoder: decoder destruction
322 *****************************************************************************/
323 static void CloseDecoder( vlc_object_t *p_this )
325 decoder_t *p_dec = (decoder_t *)p_this;
326 decoder_sys_t *p_sys = p_dec->p_sys;
328 schro_decoder_free( p_sys->p_schro );
332 /****************************************************************************
333 * DecodeBlock: the whole thing
334 ****************************************************************************
335 * Blocks must start with a Dirac parse unit.
336 * Blocks must contain at least one Dirac parse unit.
337 * Blocks must end with a picture parse unit.
338 * Blocks must not contain more than one picture parse unit.
339 * If a block has a PTS signaled, it applies to the first picture in p_block
340 * - Schroedinger has no internal means to tag pictures with a PTS
341 * - In this case, the picture number is extracted and stored in a TLB
342 * When a picture is extracted from schro, it is looked up in the pts_tlb
343 * - If the picture was never tagged with a PTS, a new one is calculated
344 * based upon the frame rate and last output PTS.
346 * If this function returns a picture (!NULL), it is called again and the
347 * same block is resubmitted. To avoid this, set *pp_block to NULL;
348 * If this function returns NULL, the *pp_block is lost (and leaked).
349 * This function must free all blocks when finished with them.
350 ****************************************************************************/
351 static picture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
353 decoder_sys_t *p_sys = p_dec->p_sys;
355 SchroBuffer *p_schrobuffer;
356 SchroFrame *p_schroframe;
362 if( !pp_block ) return NULL;
367 /* prepare block for submission */
369 if (p_sys->i_ts_resync_hack && p_sys->i_ts_resync_hack--)
372 if( !p_block->i_buffer ) {
373 msg_Err( p_dec, "block is of zero size" );
377 /* reset the decoder when seeking as the decode in progress is invalid */
378 /* discard the block as it is just a null magic block */
379 if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) ) {
380 msg_Dbg( p_dec, "SCHRO_DECODER_RESET" );
381 schro_decoder_reset( p_sys->p_schro );
383 ResetPTStlb( p_dec );
385 p_sys->i_lastpts = -1;
387 /* The ts layer manages to corrupt the next packet we are to receive
388 * Since schro has no sync support, we need to drop it */
389 p_sys->i_ts_resync_hack = 1;
391 block_Release( p_block );
396 /* Unsatisfactory, and will later be fixed in schro:
397 * - Schro can only handle a single Dirac parse unit at a time
398 * - Multiple parse units may exist in p_block
399 * - All mapping specs so far guarantee that p_block would
400 * not contain anything after a picture
401 * So, we can not give the whole block to schro, but piecemeal
403 size_t i_bufused = 0;
404 while( schro_decoder_push_ready( p_sys->p_schro )) {
405 if( p_block->i_buffer - i_bufused < 13 ) {
407 block_Release( p_block );
408 msg_Err( p_dec, "not enough data left in block" );
413 size_t i_pulen = GetDWBE( p_block->p_buffer + i_bufused + 5 );
414 uint8_t *p_pu = p_block->p_buffer + i_bufused;
416 /* blocks that do not start with the parse info prefix are invalid */
417 if( p_pu[0] != 'B' || p_pu[1] != 'B' ||
418 p_pu[2] != 'C' || p_pu[3] != 'D')
421 block_Release( p_block );
422 msg_Err( p_dec, "block does not start with dirac parse code" );
426 if( i_bufused + i_pulen > p_block->i_buffer ) {
428 block_Release( p_block );
433 StorePicturePTS( p_dec, p_block, i_bufused );
435 p_schrobuffer = schro_buffer_new_with_data( p_pu, i_pulen );
436 if( i_pulen + i_bufused < p_block->i_buffer ) {
437 /* don't let schro free this block, more data still in it */
438 p_schrobuffer->free = 0;
441 p_schrobuffer->free = SchroBufferFree;
442 p_schrobuffer->priv = p_block;
446 msg_Dbg( p_dec, "Inserting bytes into decoder len=%d of %d pts=%"PRId64,
447 i_pulen, p_block->i_buffer, p_block->i_pts);
448 /* this stops the same block being fed back into this function if
449 * we were on the next iteration of this loop to output a picture */
451 state = schro_decoder_push( p_sys->p_schro, p_schrobuffer );
453 /* DO NOT refer to p_block after this point, it may have been freed */
455 i_bufused += i_pulen;
457 if( state == SCHRO_DECODER_FIRST_ACCESS_UNIT ) {
458 msg_Dbg( p_dec, "SCHRO_DECODER_FIRST_ACCESS_UNIT");
459 SetVideoFormat( p_dec );
460 ResetPTStlb( p_dec );
467 if( (i_bufused > 0) && (i_bufused < p_block->i_buffer) )
468 msg_Err( p_dec, "not submitted everything in p_buffer");
475 state = schro_decoder_wait( p_sys->p_schro );
479 case SCHRO_DECODER_NEED_BITS:
480 msg_Dbg( p_dec, "SCHRO_DECODER_NEED_BITS" );
483 case SCHRO_DECODER_NEED_FRAME:
484 msg_Dbg( p_dec, "SCHRO_DECODER_NEED_FRAME" );
485 p_schroframe = CreateSchroFrameFromPic( p_dec );
489 msg_Err( p_dec, "Could not allocate picture for decoder");
493 schro_decoder_add_output_picture( p_sys->p_schro, p_schroframe);
496 case SCHRO_DECODER_OK:
497 u_pnum = schro_decoder_get_picture_number( p_sys->p_schro );
498 p_schroframe = schro_decoder_pull( p_sys->p_schro );
499 p_pic = p_schroframe->priv;
500 p_schroframe->priv = NULL;
501 schro_frame_unref( p_schroframe );
503 /* solve presentation time stamp for picture. If this picture
504 * was not tagged with a pts when presented to decoder, interpolate
506 * This means no need to set p_pic->b_force, as we have a pts on
508 p_pic->date = GetPicturePTS( p_dec, u_pnum );
509 if (p_sys->i_lastpts >= 0 && p_pic->date == 0)
510 p_pic->date = p_sys->i_lastpts + p_sys->i_frame_pts_delta;
511 p_sys->i_lastpts = p_pic->date;
513 msg_Dbg( p_dec, "SCHRO_DECODER_OK num=%u date=%"PRId64,
514 u_pnum, p_pic->date);
518 case SCHRO_DECODER_EOS:
519 msg_Dbg( p_dec, "SCHRO_DECODER_EOS");
520 /* reset the decoder -- schro doesn't do this itself automatically */
521 /* there are no more pictures in the output buffer at this point */
522 schro_decoder_reset( p_sys->p_schro );
525 case SCHRO_DECODER_ERROR:
526 msg_Dbg( p_dec, "SCHRO_DECODER_ERROR");