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