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