]> git.sesse.net Git - vlc/blob - modules/codec/schroedinger.c
Merge branch 1.0-bugfix
[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
39 #include <schroedinger/schro.h>
40
41 /*****************************************************************************
42  * Module descriptor
43  *****************************************************************************/
44 static int        OpenDecoder  ( vlc_object_t * );
45 static void       CloseDecoder ( vlc_object_t * );
46
47 vlc_module_begin ()
48     set_category( CAT_INPUT )
49     set_subcategory( SUBCAT_INPUT_VCODEC )
50     set_description( N_("Schroedinger video decoder") )
51     set_capability( "decoder", 200 )
52     set_callbacks( OpenDecoder, CloseDecoder )
53     add_shortcut( "schroedinger" )
54 vlc_module_end ()
55
56 /*****************************************************************************
57  * Local prototypes
58  *****************************************************************************/
59 static picture_t *DecodeBlock  ( decoder_t *p_dec, block_t **pp_block );
60
61 struct picture_free_t
62 {
63    picture_t *p_pic;
64    decoder_t *p_dec;
65 };
66
67 /*****************************************************************************
68  * decoder_sys_t : Schroedinger decoder descriptor
69  *****************************************************************************/
70 struct decoder_sys_t
71 {
72     /*
73      * Dirac properties
74      */
75     mtime_t i_lastpts;
76     mtime_t i_frame_pts_delta;
77     SchroDecoder *p_schro;
78     SchroVideoFormat *p_format;
79 };
80
81 /*****************************************************************************
82  * OpenDecoder: probe the decoder and return score
83  *****************************************************************************/
84 static int OpenDecoder( vlc_object_t *p_this )
85 {
86     decoder_t *p_dec = (decoder_t*)p_this;
87     decoder_sys_t *p_sys;
88     SchroDecoder *p_schro;
89
90     if( p_dec->fmt_in.i_codec != VLC_CODEC_DIRAC )
91     {
92         return VLC_EGENERIC;
93     }
94
95     /* Allocate the memory needed to store the decoder's structure */
96     p_sys = malloc(sizeof(decoder_sys_t));
97     if( p_sys == NULL )
98         return VLC_ENOMEM;
99
100     /* Initialise the schroedinger (and hence liboil libraries */
101     /* This does no allocation and is safe to call */
102     schro_init();
103
104     /* Initialise the schroedinger decoder */
105     if( !(p_schro = schro_decoder_new()) )
106     {
107         free( p_sys );
108         return VLC_EGENERIC;
109     }
110
111     p_dec->p_sys = p_sys;
112     p_sys->p_schro = p_schro;
113     p_sys->p_format = NULL;
114     p_sys->i_lastpts = -1;
115     p_sys->i_frame_pts_delta = 0;
116
117     /* Set output properties */
118     p_dec->fmt_out.i_cat = VIDEO_ES;
119     p_dec->fmt_out.i_codec = VLC_CODEC_I420;
120
121     /* Set callbacks */
122     p_dec->pf_decode_video = DecodeBlock;
123
124     return VLC_SUCCESS;
125 }
126
127 /*****************************************************************************
128  * SetPictureFormat: Set the decoded picture params to the ones from the stream
129  *****************************************************************************/
130 static void SetVideoFormat( decoder_t *p_dec )
131 {
132     decoder_sys_t *p_sys = p_dec->p_sys;
133     double f_aspect;
134
135     p_sys->p_format = schro_decoder_get_video_format(p_sys->p_schro);
136     if( p_sys->p_format == NULL ) return;
137
138     p_sys->i_frame_pts_delta = INT64_C(1000000)
139                             * p_sys->p_format->frame_rate_denominator
140                             / p_sys->p_format->frame_rate_numerator;
141
142     switch( p_sys->p_format->chroma_format )
143     {
144     case SCHRO_CHROMA_420: p_dec->fmt_out.i_codec = VLC_CODEC_I420; break;
145     case SCHRO_CHROMA_422: p_dec->fmt_out.i_codec = VLC_CODEC_I422; break;
146     case SCHRO_CHROMA_444: p_dec->fmt_out.i_codec = VLC_CODEC_I444; break;
147     default:
148         p_dec->fmt_out.i_codec = 0;
149         break;
150     }
151
152     p_dec->fmt_out.video.i_visible_width = p_sys->p_format->clean_width;
153     p_dec->fmt_out.video.i_x_offset = p_sys->p_format->left_offset;
154     p_dec->fmt_out.video.i_width = p_sys->p_format->width;
155
156     p_dec->fmt_out.video.i_visible_height = p_sys->p_format->clean_height;
157     p_dec->fmt_out.video.i_y_offset = p_sys->p_format->top_offset;
158     p_dec->fmt_out.video.i_height = p_sys->p_format->height;
159
160     /* aspect_ratio_[numerator|denominator] describes the pixel aspect ratio */
161     f_aspect = (double)
162         ( p_sys->p_format->aspect_ratio_numerator * p_sys->p_format->width ) /
163         ( p_sys->p_format->aspect_ratio_denominator * p_sys->p_format->height);
164
165     p_dec->fmt_out.video.i_aspect = VOUT_ASPECT_FACTOR * f_aspect;
166
167     p_dec->fmt_out.video.i_frame_rate =
168         p_sys->p_format->frame_rate_numerator;
169     p_dec->fmt_out.video.i_frame_rate_base =
170         p_sys->p_format->frame_rate_denominator;
171 }
172
173 /*****************************************************************************
174  * SchroFrameFree: schro_frame callback to release the associated picture_t
175  * When schro_decoder_reset() is called there will be pictures in the
176  * decoding pipeline that need to be released rather than displayed.
177  *****************************************************************************/
178 static void SchroFrameFree( SchroFrame *frame, void *priv)
179 {
180     struct picture_free_t *p_free = priv;
181
182     if( !p_free )
183         return;
184
185     decoder_DeletePicture( p_free->p_dec, p_free->p_pic );
186     free(p_free);
187     (void)frame;
188 }
189
190 /*****************************************************************************
191  * CreateSchroFrameFromPic: wrap a picture_t in a SchroFrame
192  *****************************************************************************/
193 static SchroFrame *CreateSchroFrameFromPic( decoder_t *p_dec )
194 {
195     decoder_sys_t *p_sys = p_dec->p_sys;
196     SchroFrame *p_schroframe = schro_frame_new();
197     picture_t *p_pic = NULL;
198     struct picture_free_t *p_free;
199
200     if( !p_schroframe )
201         return NULL;
202
203     p_pic = decoder_NewPicture( p_dec );
204
205     if( !p_pic )
206         return NULL;
207
208     p_schroframe->format = SCHRO_FRAME_FORMAT_U8_420;
209     if( p_sys->p_format->chroma_format == SCHRO_CHROMA_422 )
210     {
211         p_schroframe->format = SCHRO_FRAME_FORMAT_U8_422;
212     }
213     else if( p_sys->p_format->chroma_format == SCHRO_CHROMA_444 )
214     {
215         p_schroframe->format = SCHRO_FRAME_FORMAT_U8_444;
216     }
217
218     p_schroframe->width = p_sys->p_format->width;
219     p_schroframe->height = p_sys->p_format->height;
220
221     p_free = malloc( sizeof( *p_free ) );
222     p_free->p_pic = p_pic;
223     p_free->p_dec = p_dec;
224     schro_frame_set_free_callback( p_schroframe, SchroFrameFree, p_free );
225
226     for( int i=0; i<3; i++ )
227     {
228         p_schroframe->components[i].width = p_pic->p[i].i_visible_pitch;
229         p_schroframe->components[i].stride = p_pic->p[i].i_pitch;
230         p_schroframe->components[i].height = p_pic->p[i].i_visible_lines;
231         p_schroframe->components[i].length =
232             p_pic->p[i].i_pitch * p_pic->p[i].i_lines;
233         p_schroframe->components[i].data = p_pic->p[i].p_pixels;
234
235         if(i!=0)
236         {
237             p_schroframe->components[i].v_shift =
238                 SCHRO_FRAME_FORMAT_V_SHIFT( p_schroframe->format );
239             p_schroframe->components[i].h_shift =
240                 SCHRO_FRAME_FORMAT_H_SHIFT( p_schroframe->format );
241         }
242     }
243
244     p_pic->b_progressive = !p_sys->p_format->interlaced;
245     p_pic->b_top_field_first = p_sys->p_format->top_field_first;
246     p_pic->i_nb_fields = 2;
247
248     return p_schroframe;
249 }
250
251 /*****************************************************************************
252  * SchroBufferFree: schro_buffer callback to release the associated block_t
253  *****************************************************************************/
254 static void SchroBufferFree( SchroBuffer *buf, void *priv )
255 {
256     block_t *p_block = priv;
257
258     if( !p_block )
259         return;
260
261     block_Release( p_block );
262     (void)buf;
263 }
264
265 /*****************************************************************************
266  * CloseDecoder: decoder destruction
267  *****************************************************************************/
268 static void CloseDecoder( vlc_object_t *p_this )
269 {
270     decoder_t *p_dec = (decoder_t *)p_this;
271     decoder_sys_t *p_sys = p_dec->p_sys;
272
273     schro_decoder_free( p_sys->p_schro );
274     free( p_sys );
275 }
276
277 /****************************************************************************
278  * DecodeBlock: the whole thing
279  ****************************************************************************
280  * Blocks need not be Dirac dataunit aligned.
281  * If a block has a PTS signaled, it applies to the first picture at or after p_block
282  *
283  * If this function returns a picture (!NULL), it is called again and the
284  * same block is resubmitted.  To avoid this, set *pp_block to NULL;
285  * If this function returns NULL, the *pp_block is lost (and leaked).
286  * This function must free all blocks when finished with them.
287  ****************************************************************************/
288 static picture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
289 {
290     decoder_sys_t *p_sys = p_dec->p_sys;
291
292     if( !pp_block ) return NULL;
293
294     if ( *pp_block ) {
295         block_t *p_block = *pp_block;
296
297         /* reset the decoder when seeking as the decode in progress is invalid */
298         /* discard the block as it is just a null magic block */
299         if( p_block->i_flags & BLOCK_FLAG_DISCONTINUITY ) {
300             schro_decoder_reset( p_sys->p_schro );
301
302             p_sys->i_lastpts = -1;
303             block_Release( p_block );
304             *pp_block = NULL;
305             return NULL;
306         }
307
308         SchroBuffer *p_schrobuffer;
309         p_schrobuffer = schro_buffer_new_with_data( p_block->p_buffer, p_block->i_buffer );
310         p_schrobuffer->free = SchroBufferFree;
311         p_schrobuffer->priv = p_block;
312         if( p_block->i_pts != VLC_TS_INVALID ) {
313             mtime_t *p_pts = malloc( sizeof(*p_pts) );
314             if( p_pts ) {
315                 *p_pts = p_block->i_pts;
316                 /* if this call fails, p_pts is freed automatically */
317                 p_schrobuffer->tag = schro_tag_new( p_pts, free );
318             }
319         }
320
321         /* this stops the same block being fed back into this function if
322          * we were on the next iteration of this loop to output a picture */
323         *pp_block = NULL;
324         schro_decoder_autoparse_push( p_sys->p_schro, p_schrobuffer );
325         /* DO NOT refer to p_block after this point, it may have been freed */
326     }
327
328     while( 1 )
329     {
330         SchroFrame *p_schroframe;
331         picture_t *p_pic;
332         int state = schro_decoder_autoparse_wait( p_sys->p_schro );
333
334         switch( state )
335         {
336         case SCHRO_DECODER_FIRST_ACCESS_UNIT:
337             SetVideoFormat( p_dec );
338             break;
339
340         case SCHRO_DECODER_NEED_BITS:
341             return NULL;
342
343         case SCHRO_DECODER_NEED_FRAME:
344             p_schroframe = CreateSchroFrameFromPic( p_dec );
345
346             if( !p_schroframe )
347             {
348                 msg_Err( p_dec, "Could not allocate picture for decoder");
349                 return NULL;
350             }
351
352             schro_decoder_add_output_picture( p_sys->p_schro, p_schroframe);
353             break;
354
355         case SCHRO_DECODER_OK: {
356             SchroTag *p_tag = schro_decoder_get_picture_tag( p_sys->p_schro );
357             p_schroframe = schro_decoder_pull( p_sys->p_schro );
358             if( !p_schroframe || !p_schroframe->priv )
359             {
360                 /* frame can't be one that was allocated by us
361                  *   -- no private data: discard */
362                 if( p_tag ) schro_tag_free( p_tag );
363                 if( p_schroframe ) schro_frame_unref( p_schroframe );
364                 break;
365             }
366             p_pic = ((struct picture_free_t*) p_schroframe->priv)->p_pic;
367             p_schroframe->priv = NULL;
368
369             if( p_tag )
370             {
371                 /* free is handled by schro_frame_unref */
372                 p_pic->date = *(mtime_t*) p_tag->value;
373                 schro_tag_free( p_tag );
374             }
375             else if( p_sys->i_lastpts >= 0 )
376             {
377                 /* NB, this shouldn't happen since the packetizer does a
378                  * very thorough job of inventing timestamps.  The
379                  * following is just a very rough fall back incase packetizer
380                  * is missing. */
381                 /* maybe it would be better to set p_pic->b_force ? */
382                 p_pic->date = p_sys->i_lastpts + p_sys->i_frame_pts_delta;
383             }
384             p_sys->i_lastpts = p_pic->date;
385
386             schro_frame_unref( p_schroframe );
387             return p_pic;
388         }
389         case SCHRO_DECODER_EOS:
390             /* NB, the new api will not emit _EOS, it handles the reset internally */
391             break;
392
393         case SCHRO_DECODER_ERROR:
394             msg_Err( p_dec, "SCHRO_DECODER_ERROR");
395             return NULL;
396         }
397     }
398 }
399