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 /*****************************************************************************
44 *****************************************************************************/
45 static int OpenDecoder ( vlc_object_t * );
46 static void CloseDecoder ( vlc_object_t * );
49 set_category( CAT_INPUT )
50 set_subcategory( SUBCAT_INPUT_VCODEC )
51 set_description( N_("Schroedinger video decoder") )
52 set_capability( "decoder", 200 )
53 set_callbacks( OpenDecoder, CloseDecoder )
54 add_shortcut( "schroedinger" )
57 /*****************************************************************************
59 *****************************************************************************/
60 static picture_t *DecodeBlock ( decoder_t *p_dec, block_t **pp_block );
62 /*****************************************************************************
63 * picture_pts_t : store pts alongside picture number, not carried through
65 *****************************************************************************/
68 int i_empty; //not in use
69 uint32_t u_pnum; //picture number from dirac header
70 mtime_t i_pts; //pts for this picture
79 /*****************************************************************************
80 * decoder_sys_t : Schroedinger decoder descriptor
81 *****************************************************************************/
82 #define PTS_TLB_SIZE 16
89 mtime_t i_frame_pts_delta;
90 SchroDecoder *p_schro;
91 SchroVideoFormat *p_format;
92 struct picture_pts_t pts_tlb[PTS_TLB_SIZE];
97 /*****************************************************************************
98 * ResetPTStlb: Purge all entries in @p_dec@'s PTS-tlb
99 *****************************************************************************/
100 static void ResetPTStlb( decoder_t *p_dec )
102 decoder_sys_t *p_sys = p_dec->p_sys;
103 for( int i=0; i<PTS_TLB_SIZE; i++) {
104 p_sys->pts_tlb[i].i_empty = 1;
108 /*****************************************************************************
109 * OpenDecoder: probe the decoder and return score
110 *****************************************************************************/
111 static int OpenDecoder( vlc_object_t *p_this )
113 decoder_t *p_dec = (decoder_t*)p_this;
114 decoder_sys_t *p_sys;
115 SchroDecoder *p_schro;
117 if( p_dec->fmt_in.i_codec != VLC_FOURCC('d','r','a','c') )
122 /* Allocate the memory needed to store the decoder's structure */
123 p_sys = malloc(sizeof(decoder_sys_t));
127 /* Initialise the schroedinger (and hence liboil libraries */
128 /* This does no allocation and is safe to call */
131 /* Initialise the schroedinger decoder */
132 if( !(p_schro = schro_decoder_new()) )
138 p_dec->p_sys = p_sys;
139 p_sys->p_schro = p_schro;
140 p_sys->p_format = NULL;
141 p_sys->i_lastpts = -1;
142 p_sys->i_frame_pts_delta = 0;
146 /* request packetizer */
147 p_dec->b_need_packetized = true;
149 /* Set output properties */
150 p_dec->fmt_out.i_cat = VIDEO_ES;
151 p_dec->fmt_out.i_codec = VLC_FOURCC('I','4','2','0');
154 p_dec->pf_decode_video = DecodeBlock;
159 /*****************************************************************************
160 * SetPictureFormat: Set the decoded picture params to the ones from the stream
161 *****************************************************************************/
162 static void SetVideoFormat( decoder_t *p_dec )
164 decoder_sys_t *p_sys = p_dec->p_sys;
167 p_sys->p_format = schro_decoder_get_video_format(p_sys->p_schro);
168 if( p_sys->p_format == NULL ) return;
170 p_sys->i_frame_pts_delta = INT64_C(1000000)
171 * p_sys->p_format->frame_rate_denominator
172 / p_sys->p_format->frame_rate_numerator;
174 switch( p_sys->p_format->chroma_format )
176 case SCHRO_CHROMA_420: p_dec->fmt_out.i_codec = VLC_FOURCC('I','4','2','0'); break;
177 case SCHRO_CHROMA_422: p_dec->fmt_out.i_codec = VLC_FOURCC('I','4','2','2'); break;
178 case SCHRO_CHROMA_444: p_dec->fmt_out.i_codec = VLC_FOURCC('I','4','4','4'); break;
180 p_dec->fmt_out.i_codec = 0;
184 p_dec->fmt_out.video.i_visible_width =
185 p_dec->fmt_out.video.i_width = p_sys->p_format->width;
187 p_dec->fmt_out.video.i_visible_height =
188 p_dec->fmt_out.video.i_height = p_sys->p_format->height;
190 /* aspect_ratio_[numerator|denominator] describes the pixel aspect ratio */
192 ( p_sys->p_format->aspect_ratio_numerator * p_sys->p_format->width ) /
193 ( p_sys->p_format->aspect_ratio_denominator * p_sys->p_format->height);
195 p_dec->fmt_out.video.i_aspect = VOUT_ASPECT_FACTOR * f_aspect;
197 p_dec->fmt_out.video.i_frame_rate =
198 p_sys->p_format->frame_rate_numerator;
199 p_dec->fmt_out.video.i_frame_rate_base =
200 p_sys->p_format->frame_rate_denominator;
203 /*****************************************************************************
204 * StorePicturePTS: Store the PTS value for a particular picture number
205 *****************************************************************************/
206 static void StorePicturePTS( decoder_t *p_dec, block_t *p_block, int i_pupos )
208 decoder_sys_t *p_sys = p_dec->p_sys;
211 u_pnum = GetDWBE( p_block->p_buffer + i_pupos + 13 );
213 for( int i=0; i<PTS_TLB_SIZE; i++ ) {
214 if( p_sys->pts_tlb[i].i_empty ) {
216 p_sys->pts_tlb[i].u_pnum = u_pnum;
217 p_sys->pts_tlb[i].i_pts = p_block->i_pts;
218 p_sys->pts_tlb[i].i_empty = 0;
224 msg_Err( p_dec, "Could not store PTS %"PRId64" for picture %u",
225 p_block->i_pts, u_pnum );
228 /*****************************************************************************
229 * GetPicturePTS: Retrieve the PTS value for a particular picture number
230 *****************************************************************************/
231 static mtime_t GetPicturePTS( decoder_t *p_dec, uint32_t u_pnum )
233 decoder_sys_t *p_sys = p_dec->p_sys;
235 for( int i=0; i<PTS_TLB_SIZE; i++ ) {
236 if( (!p_sys->pts_tlb[i].i_empty) &&
237 (p_sys->pts_tlb[i].u_pnum == u_pnum)) {
239 p_sys->pts_tlb[i].i_empty = 1;
240 return p_sys->pts_tlb[i].i_pts;
244 msg_Err( p_dec, "Could not retrieve PTS for picture %u", u_pnum );
248 /*****************************************************************************
249 * SchroFrameFree: schro_frame callback to release the associated picture_t
250 * When schro_decoder_reset() is called there will be pictures in the
251 * decoding pipeline that need to be released rather than displayed.
252 *****************************************************************************/
253 static void SchroFrameFree( SchroFrame *frame, void *priv)
255 struct picture_free_t *p_free = priv;
260 decoder_DeletePicture( p_free->p_dec, p_free->p_pic );
265 /*****************************************************************************
266 * CreateSchroFrameFromPic: wrap a picture_t in a SchroFrame
267 *****************************************************************************/
268 static SchroFrame *CreateSchroFrameFromPic( decoder_t *p_dec )
270 decoder_sys_t *p_sys = p_dec->p_sys;
271 SchroFrame *p_schroframe = schro_frame_new();
272 picture_t *p_pic = NULL;
273 struct picture_free_t *p_free;
278 p_pic = decoder_NewPicture( p_dec );
283 p_schroframe->format = SCHRO_FRAME_FORMAT_U8_420;
284 if( p_sys->p_format->chroma_format == SCHRO_CHROMA_422 )
286 p_schroframe->format = SCHRO_FRAME_FORMAT_U8_422;
288 else if( p_sys->p_format->chroma_format == SCHRO_CHROMA_444 )
290 p_schroframe->format = SCHRO_FRAME_FORMAT_U8_444;
293 p_schroframe->width = p_sys->p_format->width;
294 p_schroframe->height = p_sys->p_format->height;
296 p_free = malloc( sizeof( *p_free ) );
297 p_free->p_pic = p_pic;
298 p_free->p_dec = p_dec;
299 schro_frame_set_free_callback( p_schroframe, SchroFrameFree, p_free );
301 for( int i=0; i<3; i++ )
303 p_schroframe->components[i].width = p_pic->p[i].i_visible_pitch;
304 p_schroframe->components[i].stride = p_pic->p[i].i_pitch;
305 p_schroframe->components[i].height = p_pic->p[i].i_visible_lines;
306 p_schroframe->components[i].length =
307 p_pic->p[i].i_pitch * p_pic->p[i].i_lines;
308 p_schroframe->components[i].data = p_pic->p[i].p_pixels;
312 p_schroframe->components[i].v_shift =
313 SCHRO_FRAME_FORMAT_V_SHIFT( p_schroframe->format );
314 p_schroframe->components[i].h_shift =
315 SCHRO_FRAME_FORMAT_H_SHIFT( p_schroframe->format );
319 p_pic->b_progressive = !p_sys->p_format->interlaced;
320 p_pic->b_top_field_first = p_sys->p_format->top_field_first;
321 p_pic->i_nb_fields = 2;
326 /*****************************************************************************
327 * SchroBufferFree: schro_buffer callback to release the associated block_t
328 *****************************************************************************/
329 static void SchroBufferFree( SchroBuffer *buf, void *priv )
331 block_t *p_block = priv;
336 block_Release( p_block );
340 /*****************************************************************************
341 * CloseDecoder: decoder destruction
342 *****************************************************************************/
343 static void CloseDecoder( vlc_object_t *p_this )
345 decoder_t *p_dec = (decoder_t *)p_this;
346 decoder_sys_t *p_sys = p_dec->p_sys;
348 schro_decoder_free( p_sys->p_schro );
352 /****************************************************************************
353 * DecodeBlock: the whole thing
354 ****************************************************************************
355 * Blocks must start with a Dirac parse unit.
356 * Blocks must contain at least one Dirac parse unit.
357 * Blocks must end with a picture parse unit.
358 * Blocks must not contain more than one picture parse unit.
359 * If a block has a PTS signaled, it applies to the first picture in p_block
360 * - Schroedinger has no internal means to tag pictures with a PTS
361 * - In this case, the picture number is extracted and stored in a TLB
362 * When a picture is extracted from schro, it is looked up in the pts_tlb
363 * - If the picture was never tagged with a PTS, a new one is calculated
364 * based upon the frame rate and last output PTS.
366 * If this function returns a picture (!NULL), it is called again and the
367 * same block is resubmitted. To avoid this, set *pp_block to NULL;
368 * If this function returns NULL, the *pp_block is lost (and leaked).
369 * This function must free all blocks when finished with them.
370 ****************************************************************************/
371 static picture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
373 decoder_sys_t *p_sys = p_dec->p_sys;
375 SchroBuffer *p_schrobuffer;
376 SchroFrame *p_schroframe;
381 if( !pp_block ) return NULL;
386 /* prepare block for submission */
388 if( !p_block->i_buffer ) {
389 msg_Err( p_dec, "block is of zero size" );
393 /* reset the decoder when seeking as the decode in progress is invalid */
394 /* discard the block as it is just a null magic block */
395 if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) ) {
397 msg_Dbg( p_dec, "SCHRO_DECODER_RESET" );
399 schro_decoder_reset( p_sys->p_schro );
401 ResetPTStlb( p_dec );
403 p_sys->i_lastpts = -1;
405 block_Release( p_block );
410 /* Unsatisfactory, and will later be fixed in schro:
411 * - Schro can only handle a single Dirac parse unit at a time
412 * - Multiple parse units may exist in p_block
413 * - All mapping specs so far guarantee that p_block would
414 * not contain anything after a picture
415 * So, we can not give the whole block to schro, but piecemeal
417 size_t i_bufused = 0;
418 while( schro_decoder_push_ready( p_sys->p_schro )) {
419 if( p_block->i_buffer - i_bufused < 13 ) {
421 block_Release( p_block );
422 msg_Err( p_dec, "not enough data left in block" );
427 size_t i_pulen = GetDWBE( p_block->p_buffer + i_bufused + 5 );
428 uint8_t *p_pu = p_block->p_buffer + i_bufused;
434 /* blocks that do not start with the parse info prefix are invalid */
435 if( p_pu[0] != 'B' || p_pu[1] != 'B' ||
436 p_pu[2] != 'C' || p_pu[3] != 'D')
439 block_Release( p_block );
440 msg_Err( p_dec, "block does not start with dirac parse code" );
444 if( i_bufused + i_pulen > p_block->i_buffer ) {
446 block_Release( p_block );
451 StorePicturePTS( p_dec, p_block, i_bufused );
453 p_schrobuffer = schro_buffer_new_with_data( p_pu, i_pulen );
454 if( i_pulen + i_bufused < p_block->i_buffer ) {
455 /* don't let schro free this block, more data still in it */
456 p_schrobuffer->free = 0;
459 p_schrobuffer->free = SchroBufferFree;
460 p_schrobuffer->priv = p_block;
465 msg_Dbg( p_dec, "Inserting bytes into decoder len=%zu of %zu pts=%"PRId64,
466 i_pulen, p_block->i_buffer, p_block->i_pts);
468 /* this stops the same block being fed back into this function if
469 * we were on the next iteration of this loop to output a picture */
471 state = schro_decoder_push( p_sys->p_schro, p_schrobuffer );
473 /* DO NOT refer to p_block after this point, it may have been freed */
475 i_bufused += i_pulen;
477 if( state == SCHRO_DECODER_FIRST_ACCESS_UNIT ) {
479 msg_Dbg( p_dec, "SCHRO_DECODER_FIRST_ACCESS_UNIT");
481 SetVideoFormat( p_dec );
482 ResetPTStlb( p_dec );
484 p_schroframe = CreateSchroFrameFromPic( p_dec );
486 schro_decoder_add_output_picture( p_sys->p_schro, p_schroframe);
497 state = schro_decoder_wait( p_sys->p_schro );
501 case SCHRO_DECODER_NEED_BITS:
503 msg_Dbg( p_dec, "SCHRO_DECODER_NEED_BITS" );
507 case SCHRO_DECODER_NEED_FRAME:
509 msg_Dbg( p_dec, "SCHRO_DECODER_NEED_FRAME" );
511 p_schroframe = CreateSchroFrameFromPic( p_dec );
515 msg_Err( p_dec, "Could not allocate picture for decoder");
519 schro_decoder_add_output_picture( p_sys->p_schro, p_schroframe);
522 case SCHRO_DECODER_OK:
523 u_pnum = schro_decoder_get_picture_number( p_sys->p_schro );
524 p_schroframe = schro_decoder_pull( p_sys->p_schro );
525 if( !p_schroframe->priv )
527 schro_frame_unref( p_schroframe );
530 p_pic = ((struct picture_free_t*) p_schroframe->priv)->p_pic;
531 p_schroframe->priv = NULL;
532 schro_frame_unref( p_schroframe );
534 /* solve presentation time stamp for picture. If this picture
535 * was not tagged with a pts when presented to decoder, interpolate
537 * This means no need to set p_pic->b_force, as we have a pts on
539 p_pic->date = GetPicturePTS( p_dec, u_pnum );
540 if (p_sys->i_lastpts >= 0 && p_pic->date == 0)
541 p_pic->date = p_sys->i_lastpts + p_sys->i_frame_pts_delta;
542 p_sys->i_lastpts = p_pic->date;
545 msg_Dbg( p_dec, "SCHRO_DECODER_OK num=%u date=%"PRId64,
546 u_pnum, p_pic->date);
550 case SCHRO_DECODER_EOS:
552 msg_Dbg( p_dec, "SCHRO_DECODER_EOS");
554 /* reset the decoder -- schro doesn't do this itself automatically */
555 /* there are no more pictures in the output buffer at this point */
556 schro_decoder_reset( p_sys->p_schro );
559 case SCHRO_DECODER_ERROR:
561 msg_Dbg( p_dec, "SCHRO_DECODER_ERROR");