]> git.sesse.net Git - vlc/blob - modules/codec/schroedinger.c
46245f87247eb123f6db9e03f0ce176c6b5bf76d
[vlc] / modules / codec / schroedinger.c
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
7  * $Id$
8  *
9  * Authors: Jonathan Rosser <jonathan.rosser@gmail.com>
10  *          David Flynn <davidf at rd dot bbc.co.uk>
11  *
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.
16  *
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.
21  *
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  *****************************************************************************/
26
27 /*****************************************************************************
28  * Preamble
29  *****************************************************************************/
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
33
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
36 #include <vlc_codec.h>
37 #include <vlc_sout.h>
38 #include <vlc_vout.h>
39
40 #include <schroedinger/schro.h>
41
42 /*****************************************************************************
43  * picture_pts_t : store pts alongside picture number, not carried through
44  * decoder
45  *****************************************************************************/
46 struct picture_pts_t
47 {
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
51 };
52
53 /*****************************************************************************
54  * decoder_sys_t : Schroedinger decoder descriptor
55  *****************************************************************************/
56 #define PTS_TLB_SIZE 16
57 struct decoder_sys_t
58 {
59     /*
60      * Dirac properties
61      */
62     mtime_t i_lastpts;
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];
67     int i_ts_resync_hack;
68 };
69
70 /*****************************************************************************
71  * Local prototypes
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 );
76
77 /*****************************************************************************
78  * Module descriptor
79  *****************************************************************************/
80
81 vlc_module_begin();
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" );
88 vlc_module_end();
89
90 /*****************************************************************************
91  * ResetPTStlb: Purge all entries in @p_dec@'s PTS-tlb
92  *****************************************************************************/
93 static void ResetPTStlb( decoder_t *p_dec )
94 {
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;
98     }
99 }
100
101 /*****************************************************************************
102  * OpenDecoder: probe the decoder and return score
103  *****************************************************************************/
104 static int OpenDecoder( vlc_object_t *p_this )
105 {
106     decoder_t *p_dec = (decoder_t*)p_this;
107     decoder_sys_t *p_sys;
108     SchroDecoder *p_schro;
109
110     if( p_dec->fmt_in.i_codec != VLC_FOURCC('d','r','a','c') )
111     {
112         return VLC_EGENERIC;
113     }
114
115     /* Allocate the memory needed to store the decoder's structure */
116     p_sys = malloc(sizeof(decoder_sys_t));
117     if( p_sys == NULL )
118         return VLC_ENOMEM;
119
120     /* Initialise the schroedinger (and hence liboil libraries */
121     /* This does no allocation and is safe to call */
122     schro_init();
123
124     /* Initialise the schroedinger decoder */
125     if( !(p_schro = schro_decoder_new()) )
126     {
127         free( p_sys );
128         return VLC_EGENERIC;
129     }
130
131     p_dec->p_sys = p_sys;
132     p_sys->p_schro = p_schro;
133     p_sys->p_format = NULL;
134     p_sys->i_lastpts = -1;
135     p_sys->i_frame_pts_delta = 0;
136     p_sys->i_ts_resync_hack = 0;
137
138     ResetPTStlb(p_dec);
139
140     /* Set output properties */
141     p_dec->fmt_out.i_cat = VIDEO_ES;
142     p_dec->fmt_out.i_codec = VLC_FOURCC('I','4','2','0');
143
144     /* Set callbacks */
145     p_dec->pf_decode_video = DecodeBlock;
146
147     return VLC_SUCCESS;
148 }
149
150 /*****************************************************************************
151  * SetPictureFormat: Set the decoded picture params to the ones from the stream
152  *****************************************************************************/
153 static void SetVideoFormat( decoder_t *p_dec )
154 {
155     decoder_sys_t *p_sys = p_dec->p_sys;
156     double f_aspect;
157
158     p_sys->p_format = schro_decoder_get_video_format(p_sys->p_schro);
159     if( p_sys->p_format == NULL ) return;
160
161     p_sys->i_frame_pts_delta = INT64_C(1000000)
162                             * p_sys->p_format->frame_rate_denominator
163                             / p_sys->p_format->frame_rate_numerator;
164
165     switch( p_sys->p_format->chroma_format )
166     {
167     case SCHRO_CHROMA_420: p_dec->fmt_out.i_codec = VLC_FOURCC('I','4','2','0'); break;
168     case SCHRO_CHROMA_422: p_dec->fmt_out.i_codec = VLC_FOURCC('I','4','2','2'); break;
169     case SCHRO_CHROMA_444: p_dec->fmt_out.i_codec = VLC_FOURCC('I','4','4','4'); break;
170     default:
171         p_dec->fmt_out.i_codec = 0;
172         break;
173     }
174
175     p_dec->fmt_out.video.i_visible_width =
176     p_dec->fmt_out.video.i_width = p_sys->p_format->width;
177
178     p_dec->fmt_out.video.i_visible_height =
179     p_dec->fmt_out.video.i_height = p_sys->p_format->height;
180
181     /* aspect_ratio_[numerator|denominator] describes the pixel aspect ratio */
182     f_aspect = (double)
183         ( p_sys->p_format->aspect_ratio_numerator * p_sys->p_format->width ) /
184         ( p_sys->p_format->aspect_ratio_denominator * p_sys->p_format->height);
185
186     p_dec->fmt_out.video.i_aspect = VOUT_ASPECT_FACTOR * f_aspect;
187
188     p_dec->fmt_out.video.i_frame_rate =
189         p_sys->p_format->frame_rate_numerator;
190     p_dec->fmt_out.video.i_frame_rate_base =
191         p_sys->p_format->frame_rate_denominator;
192 }
193
194 /*****************************************************************************
195  * StorePicturePTS: Store the PTS value for a particular picture number
196  *****************************************************************************/
197 static void StorePicturePTS( decoder_t *p_dec, block_t *p_block, int i_pupos )
198 {
199     decoder_sys_t *p_sys = p_dec->p_sys;
200     uint32_t u_pnum;
201     mtime_t i_pts;
202
203     u_pnum = GetDWBE( p_block->p_buffer + i_pupos + 13 );
204     i_pts = p_block->i_pts > 0 ? p_block->i_pts : p_block->i_dts;
205
206     for( int i=0; i<PTS_TLB_SIZE; i++ ) {
207         if( p_sys->pts_tlb[i].i_empty ) {
208
209             p_sys->pts_tlb[i].u_pnum = u_pnum;
210             p_sys->pts_tlb[i].i_pts = i_pts;
211             p_sys->pts_tlb[i].i_empty = 0;
212
213             return;
214         }
215     }
216
217     msg_Err( p_dec, "Could not store PTS %"PRId64" for picture %u",
218              i_pts, u_pnum );
219 }
220
221 /*****************************************************************************
222  * GetPicturePTS: Retrieve the PTS value for a particular picture number
223  *****************************************************************************/
224 static mtime_t GetPicturePTS( decoder_t *p_dec, uint32_t u_pnum )
225 {
226     decoder_sys_t *p_sys = p_dec->p_sys;
227
228     for( int i=0; i<PTS_TLB_SIZE; i++ ) {
229         if( (!p_sys->pts_tlb[i].i_empty) &&
230             (p_sys->pts_tlb[i].u_pnum == u_pnum)) {
231
232              p_sys->pts_tlb[i].i_empty = 1;
233              return p_sys->pts_tlb[i].i_pts;
234         }
235     }
236
237     msg_Err( p_dec, "Could not retrieve PTS for picture %u", u_pnum );
238     return 0;
239 }
240
241 /*****************************************************************************
242  * SchroFrameFree: schro_frame callback to release the associated picture_t
243  * When schro_decoder_reset() is called there will be pictures in the
244  * decoding pipeline that need to be released rather than displayed.
245  *****************************************************************************/
246 static void SchroFrameFree( SchroFrame *frame, void *priv)
247 {
248     picture_t *p_pic = priv;
249
250     if( !p_pic )
251         return;
252
253     if( p_pic->pf_release ) p_pic->pf_release( p_pic );
254 }
255
256 /*****************************************************************************
257  * CreateSchroFrameFromPic: wrap a picture_t in a SchroFrame
258  *****************************************************************************/
259 static SchroFrame *CreateSchroFrameFromPic( decoder_t *p_dec )
260 {
261     decoder_sys_t *p_sys = p_dec->p_sys;
262     SchroFrame *p_schroframe = schro_frame_new();
263     picture_t *p_pic = NULL;
264
265     if( !p_schroframe )
266         return NULL;
267
268     p_pic = p_dec->pf_vout_buffer_new( p_dec );
269
270     if( !p_pic )
271         return NULL;
272
273     p_schroframe->format = SCHRO_FRAME_FORMAT_U8_420;
274     if( p_sys->p_format->chroma_format == SCHRO_CHROMA_422 )
275     {
276         p_schroframe->format = SCHRO_FRAME_FORMAT_U8_422;
277     }
278     else if( p_sys->p_format->chroma_format == SCHRO_CHROMA_444 )
279     {
280         p_schroframe->format = SCHRO_FRAME_FORMAT_U8_444;
281     }
282
283     p_schroframe->width = p_sys->p_format->width;
284     p_schroframe->height = p_sys->p_format->height;
285     schro_frame_set_free_callback( p_schroframe, SchroFrameFree, p_pic );
286
287     for( int i=0; i<3; i++ )
288     {
289         p_schroframe->components[i].width = p_pic->p[i].i_visible_pitch;
290         p_schroframe->components[i].stride = p_pic->p[i].i_pitch;
291         p_schroframe->components[i].height = p_pic->p[i].i_visible_lines;
292         p_schroframe->components[i].length =
293             p_pic->p[i].i_pitch * p_pic->p[i].i_lines;
294         p_schroframe->components[i].data = p_pic->p[i].p_pixels;
295
296         if(i!=0)
297         {
298             p_schroframe->components[i].v_shift =
299                 SCHRO_FRAME_FORMAT_V_SHIFT( p_schroframe->format );
300             p_schroframe->components[i].h_shift =
301                 SCHRO_FRAME_FORMAT_H_SHIFT( p_schroframe->format );
302         }
303     }
304
305     p_pic->b_progressive = !p_sys->p_format->interlaced;
306     p_pic->b_top_field_first = p_sys->p_format->top_field_first;
307     p_pic->i_nb_fields = 2;
308
309     return p_schroframe;
310 }
311
312 /*****************************************************************************
313  * SchroBufferFree: schro_buffer callback to release the associated block_t
314  *****************************************************************************/
315 static void SchroBufferFree( SchroBuffer *buf, void *priv )
316 {
317     block_t *p_block = priv;
318
319     if( !p_block )
320         return;
321
322     block_Release( p_block );
323 }
324
325 /*****************************************************************************
326  * CloseDecoder: decoder destruction
327  *****************************************************************************/
328 static void CloseDecoder( vlc_object_t *p_this )
329 {
330     decoder_t *p_dec = (decoder_t *)p_this;
331     decoder_sys_t *p_sys = p_dec->p_sys;
332
333     schro_decoder_free( p_sys->p_schro );
334     free( p_sys );
335 }
336
337 /****************************************************************************
338  * DecodeBlock: the whole thing
339  ****************************************************************************
340  * Blocks must start with a Dirac parse unit.
341  * Blocks must contain at least one Dirac parse unit.
342  * Blocks must end with a picture parse unit.
343  * Blocks must not contain more than one picture parse unit.
344  * If a block has a PTS signaled, it applies to the first picture in p_block
345  *   - Schroedinger has no internal means to tag pictures with a PTS
346  *   - In this case, the picture number is extracted and stored in a TLB
347  * When a picture is extracted from schro, it is looked up in the pts_tlb
348  *   - If the picture was never tagged with a PTS, a new one is calculated
349  *     based upon the frame rate and last output PTS.
350  *
351  * If this function returns a picture (!NULL), it is called again and the
352  * same block is resubmitted.  To avoid this, set *pp_block to NULL;
353  * If this function returns NULL, the *pp_block is lost (and leaked).
354  * This function must free all blocks when finished with them.
355  ****************************************************************************/
356 static picture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
357 {
358     decoder_sys_t *p_sys = p_dec->p_sys;
359     int state;
360     SchroBuffer *p_schrobuffer;
361     SchroFrame *p_schroframe;
362     picture_t *p_pic;
363     block_t *p_block;
364     uint32_t u_pnum;
365     static int drop = 0;
366
367     if( !pp_block ) return NULL;
368
369     p_block = *pp_block;
370
371     if ( p_block ) do {
372         /* prepare block for submission */
373
374         if (p_sys->i_ts_resync_hack && p_sys->i_ts_resync_hack--)
375             return NULL;
376
377         if( !p_block->i_buffer ) {
378             msg_Err( p_dec, "block is of zero size" );
379             break;
380         }
381
382         /* reset the decoder when seeking as the decode in progress is invalid */
383         /* discard the block as it is just a null magic block */
384         if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) ) {
385             msg_Dbg( p_dec, "SCHRO_DECODER_RESET" );
386             schro_decoder_reset( p_sys->p_schro );
387
388             ResetPTStlb( p_dec );
389
390             p_sys->i_lastpts = -1;
391
392             /* The ts layer manages to corrupt the next packet we are to receive
393              * Since schro has no sync support, we need to drop it */
394             p_sys->i_ts_resync_hack = 1;
395
396             block_Release( p_block );
397             *pp_block = NULL;
398             return NULL;
399         }
400
401         /* Unsatisfactory, and will later be fixed in schro:
402          *  - Schro can only handle a single Dirac parse unit at a time
403          *  - Multiple parse units may exist in p_block
404          *  - All mapping specs so far guarantee that p_block would
405          *    not contain anything after a picture
406          * So, we can not give the whole block to schro, but piecemeal
407          */
408         size_t i_bufused = 0;
409         while( schro_decoder_push_ready( p_sys->p_schro )) {
410             if( p_block->i_buffer - i_bufused < 13 ) {
411                 *pp_block = NULL;
412                 block_Release( p_block );
413                 msg_Err( p_dec, "not enough data left in block" );
414                 break;
415             }
416
417             int b_bail = 0;
418             size_t i_pulen = GetDWBE( p_block->p_buffer + i_bufused + 5 );
419             uint8_t *p_pu = p_block->p_buffer + i_bufused;
420
421             /* blocks that do not start with the parse info prefix are invalid */
422             if( p_pu[0] != 'B' || p_pu[1] != 'B' ||
423                 p_pu[2] != 'C' || p_pu[3] != 'D')
424             {
425                 *pp_block = NULL;
426                 block_Release( p_block );
427                 msg_Err( p_dec, "block does not start with dirac parse code" );
428                 break;
429             }
430
431             if( i_bufused + i_pulen > p_block->i_buffer ) {
432                 *pp_block = NULL;
433                 block_Release( p_block );
434                 break;
435             }
436
437             if( p_pu[4] & 0x08 )
438                 StorePicturePTS( p_dec, p_block, i_bufused );
439
440             p_schrobuffer = schro_buffer_new_with_data( p_pu, i_pulen );
441             if( i_pulen + i_bufused < p_block->i_buffer ) {
442                 /* don't let schro free this block, more data still in it */
443                 p_schrobuffer->free = 0;
444             }
445             else {
446                 p_schrobuffer->free = SchroBufferFree;
447                 p_schrobuffer->priv = p_block;
448                 b_bail = 1;
449             }
450
451             msg_Dbg( p_dec, "Inserting bytes into decoder len=%d of %d pts=%"PRId64,
452                      i_pulen, p_block->i_buffer, p_block->i_pts);
453             /* this stops the same block being fed back into this function if
454              * we were on the next iteration of this loop to output a picture */
455             *pp_block = NULL;
456             state = schro_decoder_push( p_sys->p_schro, p_schrobuffer );
457
458             /* DO NOT refer to p_block after this point, it may have been freed */
459
460             i_bufused += i_pulen;
461
462             if( state == SCHRO_DECODER_FIRST_ACCESS_UNIT ) {
463                 msg_Dbg( p_dec, "SCHRO_DECODER_FIRST_ACCESS_UNIT");
464                 SetVideoFormat( p_dec );
465                 ResetPTStlb( p_dec );
466             }
467
468             if( b_bail )
469                 break;
470         }
471
472         if( (i_bufused > 0) && (i_bufused < p_block->i_buffer) )
473             msg_Err( p_dec, "not submitted everything in p_buffer");
474
475
476     } while( 0 );
477
478     while( 1 )
479     {
480         state = schro_decoder_wait( p_sys->p_schro );
481
482         switch( state )
483         {
484         case SCHRO_DECODER_NEED_BITS:
485             msg_Dbg( p_dec, "SCHRO_DECODER_NEED_BITS" );
486             return NULL;
487
488         case SCHRO_DECODER_NEED_FRAME:
489             msg_Dbg( p_dec, "SCHRO_DECODER_NEED_FRAME" );
490             p_schroframe = CreateSchroFrameFromPic( p_dec );
491
492             if( !p_schroframe )
493             {
494                 msg_Err( p_dec, "Could not allocate picture for decoder");
495                 return NULL;
496             }
497
498             schro_decoder_add_output_picture( p_sys->p_schro, p_schroframe);
499             break;
500
501         case SCHRO_DECODER_OK:
502             u_pnum = schro_decoder_get_picture_number( p_sys->p_schro );
503             p_schroframe = schro_decoder_pull( p_sys->p_schro );
504             p_pic = p_schroframe->priv;
505             p_schroframe->priv = NULL;
506             schro_frame_unref( p_schroframe );
507
508             /* solve presentation time stamp for picture.  If this picture
509              * was not tagged with a pts when presented to decoder, interpolate
510              * one
511              * This means no need to set p_pic->b_force, as we have a pts on
512              * each picture */
513             p_pic->date = GetPicturePTS( p_dec, u_pnum );
514             if (p_sys->i_lastpts >= 0 && p_pic->date == 0)
515                 p_pic->date = p_sys->i_lastpts + p_sys->i_frame_pts_delta;
516             p_sys->i_lastpts = p_pic->date;
517
518             msg_Dbg( p_dec, "SCHRO_DECODER_OK num=%u date=%"PRId64,
519                      u_pnum, p_pic->date);
520
521             return p_pic;
522
523         case SCHRO_DECODER_EOS:
524             msg_Dbg( p_dec, "SCHRO_DECODER_EOS");
525             /* reset the decoder -- schro doesn't do this itself automatically */
526             /* there are no more pictures in the output buffer at this point */
527             schro_decoder_reset( p_sys->p_schro );
528             break;
529
530         case SCHRO_DECODER_ERROR:
531             msg_Dbg( p_dec, "SCHRO_DECODER_ERROR");
532             return NULL;
533         }
534     }
535
536     /* Never reached */
537     return NULL;
538 }