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