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