]> git.sesse.net Git - vlc/blob - modules/codec/rawvideo.c
Simplify FLAC extradata (streaminfo) parsing
[vlc] / modules / codec / rawvideo.c
1 /*****************************************************************************
2  * rawvideo.c: Pseudo video decoder/packetizer for raw video data
3  *****************************************************************************
4  * Copyright (C) 2001, 2002 VLC authors and VideoLAN
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_codec.h>
34
35 /*****************************************************************************
36  * decoder_sys_t : raw video decoder descriptor
37  *****************************************************************************/
38 struct decoder_sys_t
39 {
40     /* Module mode */
41     bool b_packetizer;
42
43     /*
44      * Input properties
45      */
46     size_t i_raw_size;
47     bool b_invert;
48     plane_t planes[PICTURE_PLANE_MAX];
49
50     /*
51      * Common properties
52      */
53     date_t pts;
54 };
55
56 /****************************************************************************
57  * Local prototypes
58  ****************************************************************************/
59 static int  OpenDecoder   ( vlc_object_t * );
60 static int  OpenPacketizer( vlc_object_t * );
61 static void CloseDecoder  ( vlc_object_t * );
62
63 static void *DecodeBlock  ( decoder_t *, block_t ** );
64
65 static picture_t *DecodeFrame( decoder_t *, block_t * );
66 static block_t   *SendFrame  ( decoder_t *, block_t * );
67
68 /*****************************************************************************
69  * Module descriptor
70  *****************************************************************************/
71 vlc_module_begin ()
72     set_description( N_("Pseudo raw video decoder") )
73     set_capability( "decoder", 50 )
74     set_category( CAT_INPUT )
75     set_subcategory( SUBCAT_INPUT_VCODEC )
76     set_callbacks( OpenDecoder, CloseDecoder )
77
78     add_submodule ()
79     set_description( N_("Pseudo raw video packetizer") )
80     set_capability( "packetizer", 100 )
81     set_callbacks( OpenPacketizer, CloseDecoder )
82 vlc_module_end ()
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
92     switch( p_dec->fmt_in.i_codec )
93     {
94         /* Planar YUV */
95         case VLC_CODEC_I444:
96         case VLC_CODEC_J444:
97         case VLC_CODEC_I440:
98         case VLC_CODEC_J440:
99         case VLC_CODEC_I422:
100         case VLC_CODEC_J422:
101         case VLC_CODEC_I420:
102         case VLC_CODEC_J420:
103         case VLC_CODEC_YV12:
104         case VLC_CODEC_YV9:
105         case VLC_CODEC_I411:
106         case VLC_CODEC_I410:
107         case VLC_CODEC_GREY:
108         case VLC_CODEC_YUVP:
109         case VLC_CODEC_NV12:
110         case VLC_CODEC_NV21:
111         case VLC_CODEC_I422_10L:
112         case VLC_CODEC_I422_10B:
113
114         /* Packed YUV */
115         case VLC_CODEC_YUYV:
116         case VLC_CODEC_YVYU:
117         case VLC_CODEC_UYVY:
118         case VLC_CODEC_VYUY:
119
120         /* RGB */
121         case VLC_CODEC_RGB32:
122         case VLC_CODEC_RGB24:
123         case VLC_CODEC_RGB16:
124         case VLC_CODEC_RGB15:
125         case VLC_CODEC_RGB8:
126         case VLC_CODEC_RGBP:
127         case VLC_CODEC_RGBA:
128             break;
129
130         default:
131             return VLC_EGENERIC;
132     }
133
134     /* Allocate the memory needed to store the decoder's structure */
135     if( ( p_dec->p_sys = p_sys =
136           (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
137         return VLC_ENOMEM;
138     /* Misc init */
139     p_dec->p_sys->b_packetizer = false;
140     p_sys->b_invert = false;
141
142     if( (int)p_dec->fmt_in.video.i_height < 0 )
143     {
144         /* Frames are coded from bottom to top */
145         p_dec->fmt_in.video.i_height =
146             (unsigned int)(-(int)p_dec->fmt_in.video.i_height);
147         p_sys->b_invert = true;
148     }
149     if( !p_dec->fmt_in.video.i_visible_width )
150         p_dec->fmt_in.video.i_visible_width = p_dec->fmt_in.video.i_width;
151     if( !p_dec->fmt_in.video.i_visible_height )
152         p_dec->fmt_in.video.i_visible_height = p_dec->fmt_in.video.i_height;
153
154     if( p_dec->fmt_in.video.i_visible_width <= 0
155      || p_dec->fmt_in.video.i_visible_height <= 0 )
156     {
157         msg_Err( p_dec, "invalid display size %dx%d",
158                  p_dec->fmt_in.video.i_width, p_dec->fmt_in.video.i_height );
159         return VLC_EGENERIC;
160     }
161
162     es_format_Copy( &p_dec->fmt_out, &p_dec->fmt_in );
163
164     date_Init( &p_sys->pts, p_dec->fmt_out.video.i_frame_rate,
165                p_dec->fmt_out.video.i_frame_rate_base );
166     if( p_dec->fmt_out.video.i_frame_rate == 0 ||
167         p_dec->fmt_out.video.i_frame_rate_base == 0)
168     {
169         msg_Warn( p_dec, "invalid frame rate %d/%d, using 25 fps instead",
170                   p_dec->fmt_out.video.i_frame_rate,
171                   p_dec->fmt_out.video.i_frame_rate_base);
172         date_Init( &p_sys->pts, 25, 1 );
173     }
174
175     /* Find out p_vdec->i_raw_size */
176     video_format_Setup( &p_dec->fmt_out.video, p_dec->fmt_in.i_codec,
177                         p_dec->fmt_in.video.i_visible_width,
178                         p_dec->fmt_in.video.i_visible_height,
179                         p_dec->fmt_in.video.i_sar_num,
180                         p_dec->fmt_in.video.i_sar_den );
181     picture_t picture;
182     picture_Setup( &picture, p_dec->fmt_out.i_codec,
183                    p_dec->fmt_in.video.i_width,
184                    p_dec->fmt_in.video.i_height, 0, 1 );
185     p_sys->i_raw_size = 0;
186     for( int i = 0; i < picture.i_planes; i++ )
187     {
188         p_sys->i_raw_size += picture.p[i].i_visible_pitch *
189                              picture.p[i].i_visible_lines;
190         p_sys->planes[i] = picture.p[i];
191     }
192
193     if( !p_dec->fmt_in.video.i_sar_num || !p_dec->fmt_in.video.i_sar_den )
194     {
195         p_dec->fmt_out.video.i_sar_num = 1;
196         p_dec->fmt_out.video.i_sar_den = 1;
197     }
198
199     /* Set callbacks */
200     p_dec->pf_decode_video = (picture_t *(*)(decoder_t *, block_t **))
201         DecodeBlock;
202     p_dec->pf_packetize    = (block_t *(*)(decoder_t *, block_t **))
203         DecodeBlock;
204
205     return VLC_SUCCESS;
206 }
207
208 static int OpenPacketizer( vlc_object_t *p_this )
209 {
210     decoder_t *p_dec = (decoder_t*)p_this;
211
212     int i_ret = OpenDecoder( p_this );
213
214     if( i_ret == VLC_SUCCESS ) p_dec->p_sys->b_packetizer = true;
215
216     return i_ret;
217 }
218
219 /****************************************************************************
220  * DecodeBlock: the whole thing
221  ****************************************************************************
222  * This function must be fed with complete frames.
223  ****************************************************************************/
224 static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
225 {
226     decoder_sys_t *p_sys = p_dec->p_sys;
227     block_t *p_block;
228     void *p_buf;
229
230     if( !pp_block || !*pp_block ) return NULL;
231
232     p_block = *pp_block;
233
234
235     if( p_block->i_pts <= VLC_TS_INVALID && p_block->i_dts <= VLC_TS_INVALID &&
236         !date_Get( &p_sys->pts ) )
237     {
238         /* We've just started the stream, wait for the first PTS. */
239         block_Release( p_block );
240         return NULL;
241     }
242
243     /* Date management: If there is a pts avaliable, use that. */
244     if( p_block->i_pts > VLC_TS_INVALID )
245     {
246         date_Set( &p_sys->pts, p_block->i_pts );
247     }
248     else if( p_block->i_dts > VLC_TS_INVALID )
249     {
250         /* NB, davidf doesn't quite agree with this in general, it is ok
251          * for rawvideo since it is in order (ie pts=dts), however, it
252          * may not be ok for an out-of-order codec, so don't copy this
253          * without thinking */
254         date_Set( &p_sys->pts, p_block->i_dts );
255     }
256
257     if( p_block->i_buffer < p_sys->i_raw_size )
258     {
259         msg_Warn( p_dec, "invalid frame size (%zu < %zu)",
260                   p_block->i_buffer, p_sys->i_raw_size );
261
262         block_Release( p_block );
263         return NULL;
264     }
265
266     if( p_sys->b_packetizer )
267     {
268         p_buf = SendFrame( p_dec, p_block );
269     }
270     else
271     {
272         p_buf = DecodeFrame( p_dec, p_block );
273     }
274
275     /* Date management: 1 frame per packet */
276     date_Increment( &p_sys->pts, 1 );
277     *pp_block = NULL;
278
279     return p_buf;
280 }
281
282 /*****************************************************************************
283  * FillPicture:
284  *****************************************************************************/
285 static void FillPicture( decoder_t *p_dec, block_t *p_block, picture_t *p_pic )
286 {
287     int i_plane;
288     decoder_sys_t *p_sys = p_dec->p_sys;
289     uint8_t *p_src = p_block->p_buffer;
290
291     for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
292     {
293         int i_pitch = p_pic->p[i_plane].i_pitch;
294         int i_visible_pitch = p_sys->planes[i_plane].i_visible_pitch;
295         int i_visible_lines = p_sys->planes[i_plane].i_visible_lines;
296         uint8_t *p_dst = p_pic->p[i_plane].p_pixels;
297         uint8_t *p_dst_end = p_dst+i_pitch*i_visible_lines;
298
299         if( p_sys->b_invert )
300             for( p_dst_end -= i_pitch; p_dst <= p_dst_end;
301                  p_dst_end -= i_pitch, p_src += i_visible_pitch )
302                 memcpy( p_dst_end, p_src, i_visible_pitch );
303         else
304             for( ; p_dst < p_dst_end;
305                  p_dst += i_pitch, p_src += i_visible_pitch )
306                 memcpy( p_dst, p_src, i_visible_pitch );
307     }
308 }
309
310 /*****************************************************************************
311  * DecodeFrame: decodes a video frame.
312  *****************************************************************************/
313 static picture_t *DecodeFrame( decoder_t *p_dec, block_t *p_block )
314 {
315     decoder_sys_t *p_sys = p_dec->p_sys;
316     picture_t *p_pic;
317
318     /* Get a new picture */
319     p_pic = decoder_NewPicture( p_dec );
320     if( !p_pic )
321     {
322         block_Release( p_block );
323         return NULL;
324     }
325
326     FillPicture( p_dec, p_block, p_pic );
327
328     p_pic->date = date_Get( &p_sys->pts );
329     if( p_block->i_flags & BLOCK_FLAG_INTERLACED_MASK )
330     {
331         p_pic->b_progressive = false;
332         p_pic->i_nb_fields = 2;
333         if( p_block->i_flags & BLOCK_FLAG_TOP_FIELD_FIRST )
334             p_pic->b_top_field_first = true;
335         else
336             p_pic->b_top_field_first = false;
337     }
338     else
339         p_pic->b_progressive = true;
340
341     block_Release( p_block );
342     return p_pic;
343 }
344
345 /*****************************************************************************
346  * SendFrame: send a video frame to the stream output.
347  *****************************************************************************/
348 static block_t *SendFrame( decoder_t *p_dec, block_t *p_block )
349 {
350     decoder_sys_t *p_sys = p_dec->p_sys;
351
352     p_block->i_dts = p_block->i_pts = date_Get( &p_sys->pts );
353
354     if( p_sys->b_invert )
355     {
356         picture_t pic;
357         uint8_t *p_tmp, *p_pixels;
358         int i, j;
359
360         /* Fill in picture_t fields */
361         picture_Setup( &pic, p_dec->fmt_out.i_codec,
362                        p_dec->fmt_out.video.i_width,
363                        p_dec->fmt_out.video.i_height, 0, 1 );
364
365         if( !pic.i_planes )
366         {
367             msg_Err( p_dec, "unsupported chroma" );
368             return p_block;
369         }
370
371         p_tmp = malloc( pic.p[0].i_pitch );
372         if( !p_tmp )
373             return p_block;
374         p_pixels = p_block->p_buffer;
375         for( i = 0; i < pic.i_planes; i++ )
376         {
377             int i_pitch = pic.p[i].i_pitch;
378             uint8_t *p_top = p_pixels;
379             uint8_t *p_bottom = p_pixels + i_pitch *
380                 (pic.p[i].i_visible_lines - 1);
381
382             for( j = 0; j < pic.p[i].i_visible_lines / 2; j++ )
383             {
384                 memcpy( p_tmp, p_bottom, pic.p[i].i_visible_pitch  );
385                 memcpy( p_bottom, p_top, pic.p[i].i_visible_pitch  );
386                 memcpy( p_top, p_tmp, pic.p[i].i_visible_pitch  );
387                 p_top += i_pitch;
388                 p_bottom -= i_pitch;
389             }
390
391             p_pixels += i_pitch * pic.p[i].i_lines;
392         }
393         free( p_tmp );
394     }
395
396     return p_block;
397 }
398
399 /*****************************************************************************
400  * CloseDecoder: decoder destruction
401  *****************************************************************************/
402 static void CloseDecoder( vlc_object_t *p_this )
403 {
404     decoder_t *p_dec = (decoder_t*)p_this;
405     free( p_dec->p_sys );
406 }