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
9 * Authors: Jonathan Rosser <jonathan.rosser@gmail.com>
10 * David Flynn <davidf at rd dot bbc.co.uk>
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.
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.
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 *****************************************************************************/
27 /*****************************************************************************
29 *****************************************************************************/
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
36 #include <vlc_codec.h>
40 #include <schroedinger/schro.h>
42 /*****************************************************************************
44 *****************************************************************************/
45 static int OpenDecoder ( vlc_object_t * );
46 static void CloseDecoder ( vlc_object_t * );
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" )
57 /*****************************************************************************
59 *****************************************************************************/
60 static picture_t *DecodeBlock ( decoder_t *p_dec, block_t **pp_block );
68 /*****************************************************************************
69 * decoder_sys_t : Schroedinger decoder descriptor
70 *****************************************************************************/
77 mtime_t i_frame_pts_delta;
78 SchroDecoder *p_schro;
79 SchroVideoFormat *p_format;
82 /*****************************************************************************
83 * OpenDecoder: probe the decoder and return score
84 *****************************************************************************/
85 static int OpenDecoder( vlc_object_t *p_this )
87 decoder_t *p_dec = (decoder_t*)p_this;
89 SchroDecoder *p_schro;
91 if( p_dec->fmt_in.i_codec != VLC_FOURCC('d','r','a','c') )
96 /* Allocate the memory needed to store the decoder's structure */
97 p_sys = malloc(sizeof(decoder_sys_t));
101 /* Initialise the schroedinger (and hence liboil libraries */
102 /* This does no allocation and is safe to call */
105 /* Initialise the schroedinger decoder */
106 if( !(p_schro = schro_decoder_new()) )
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;
118 /* request packetizer */
119 p_dec->b_need_packetized = true;
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');
126 p_dec->pf_decode_video = DecodeBlock;
131 /*****************************************************************************
132 * SetPictureFormat: Set the decoded picture params to the ones from the stream
133 *****************************************************************************/
134 static void SetVideoFormat( decoder_t *p_dec )
136 decoder_sys_t *p_sys = p_dec->p_sys;
139 p_sys->p_format = schro_decoder_get_video_format(p_sys->p_schro);
140 if( p_sys->p_format == NULL ) return;
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;
146 switch( p_sys->p_format->chroma_format )
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;
152 p_dec->fmt_out.i_codec = 0;
156 p_dec->fmt_out.video.i_visible_width = p_sys->p_format->clean_width;
157 p_dec->fmt_out.video.i_x_offset = p_sys->p_format->left_offset;
158 p_dec->fmt_out.video.i_width = p_sys->p_format->width;
160 p_dec->fmt_out.video.i_visible_height = p_sys->p_format->clean_height;
161 p_dec->fmt_out.video.i_y_offset = p_sys->p_format->top_offset;
162 p_dec->fmt_out.video.i_height = p_sys->p_format->height;
164 /* aspect_ratio_[numerator|denominator] describes the pixel aspect ratio */
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);
169 p_dec->fmt_out.video.i_aspect = VOUT_ASPECT_FACTOR * f_aspect;
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;
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)
184 struct picture_free_t *p_free = priv;
189 decoder_DeletePicture( p_free->p_dec, p_free->p_pic );
194 /*****************************************************************************
195 * CreateSchroFrameFromPic: wrap a picture_t in a SchroFrame
196 *****************************************************************************/
197 static SchroFrame *CreateSchroFrameFromPic( decoder_t *p_dec )
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;
207 p_pic = decoder_NewPicture( p_dec );
212 p_schroframe->format = SCHRO_FRAME_FORMAT_U8_420;
213 if( p_sys->p_format->chroma_format == SCHRO_CHROMA_422 )
215 p_schroframe->format = SCHRO_FRAME_FORMAT_U8_422;
217 else if( p_sys->p_format->chroma_format == SCHRO_CHROMA_444 )
219 p_schroframe->format = SCHRO_FRAME_FORMAT_U8_444;
222 p_schroframe->width = p_sys->p_format->width;
223 p_schroframe->height = p_sys->p_format->height;
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 );
230 for( int i=0; i<3; i++ )
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;
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 );
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;
255 /*****************************************************************************
256 * SchroBufferFree: schro_buffer callback to release the associated block_t
257 *****************************************************************************/
258 static void SchroBufferFree( SchroBuffer *buf, void *priv )
260 block_t *p_block = priv;
265 block_Release( p_block );
269 /*****************************************************************************
270 * CloseDecoder: decoder destruction
271 *****************************************************************************/
272 static void CloseDecoder( vlc_object_t *p_this )
274 decoder_t *p_dec = (decoder_t *)p_this;
275 decoder_sys_t *p_sys = p_dec->p_sys;
277 schro_decoder_free( p_sys->p_schro );
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
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 )
294 decoder_sys_t *p_sys = p_dec->p_sys;
296 if( !pp_block ) return NULL;
299 block_t *p_block = *pp_block;
301 /* reset the decoder when seeking as the decode in progress is invalid */
302 /* discard the block as it is just a null magic block */
303 if( p_block->i_flags & BLOCK_FLAG_DISCONTINUITY ) {
304 schro_decoder_reset( p_sys->p_schro );
306 p_sys->i_lastpts = -1;
307 block_Release( p_block );
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 if( p_block->i_pts != VLC_TS_INVALID ) {
317 mtime_t *p_pts = malloc( sizeof(*p_pts) );
319 *p_pts = p_block->i_pts;
320 /* if this call fails, p_pts is freed automatically */
321 p_schrobuffer->tag = schro_tag_new( p_pts, free );
325 /* this stops the same block being fed back into this function if
326 * we were on the next iteration of this loop to output a picture */
328 schro_decoder_autoparse_push( p_sys->p_schro, p_schrobuffer );
329 /* DO NOT refer to p_block after this point, it may have been freed */
334 SchroFrame *p_schroframe;
336 int state = schro_decoder_autoparse_wait( p_sys->p_schro );
340 case SCHRO_DECODER_FIRST_ACCESS_UNIT:
341 SetVideoFormat( p_dec );
344 case SCHRO_DECODER_NEED_BITS:
347 case SCHRO_DECODER_NEED_FRAME:
348 p_schroframe = CreateSchroFrameFromPic( p_dec );
352 msg_Err( p_dec, "Could not allocate picture for decoder");
356 schro_decoder_add_output_picture( p_sys->p_schro, p_schroframe);
359 case SCHRO_DECODER_OK: {
360 SchroTag *p_tag = schro_decoder_get_picture_tag( p_sys->p_schro );
361 p_schroframe = schro_decoder_pull( p_sys->p_schro );
362 if( !p_schroframe || !p_schroframe->priv )
364 /* frame can't be one that was allocated by us
365 * -- no private data: discard */
366 if( p_tag ) schro_tag_free( p_tag );
367 if( p_schroframe ) schro_frame_unref( p_schroframe );
370 p_pic = ((struct picture_free_t*) p_schroframe->priv)->p_pic;
371 p_schroframe->priv = NULL;
375 /* free is handled by schro_frame_unref */
376 p_pic->date = *(mtime_t*) p_tag->value;
377 schro_tag_free( p_tag );
379 else if( p_sys->i_lastpts >= 0 )
381 /* NB, this shouldn't happen since the packetizer does a
382 * very thorough job of inventing timestamps. The
383 * following is just a very rough fall back incase packetizer
385 /* maybe it would be better to set p_pic->b_force ? */
386 p_pic->date = p_sys->i_lastpts + p_sys->i_frame_pts_delta;
388 p_sys->i_lastpts = p_pic->date;
390 schro_frame_unref( p_schroframe );
393 case SCHRO_DECODER_EOS:
394 /* NB, the new api will not emit _EOS, it handles the reset internally */
397 case SCHRO_DECODER_ERROR:
398 msg_Err( p_dec, "SCHRO_DECODER_ERROR");