]> git.sesse.net Git - vlc/blob - modules/codec/rawvideo.c
Add Y41B and Y42B FourCC. Solves rare raw video detection.
[vlc] / modules / codec / rawvideo.c
1 /*****************************************************************************
2  * rawvideo.c: Pseudo video decoder/packetizer for raw video data
3  *****************************************************************************
4  * Copyright (C) 2001, 2002 the VideoLAN team
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
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 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 General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <vlc/vlc.h>
28 #include <vlc_codec.h>
29 #include <vlc_vout.h>
30
31 /*****************************************************************************
32  * decoder_sys_t : raw video decoder descriptor
33  *****************************************************************************/
34 struct decoder_sys_t
35 {
36     /* Module mode */
37     vlc_bool_t b_packetizer;
38
39     /*
40      * Input properties
41      */
42     int i_raw_size;
43     vlc_bool_t b_invert;
44
45     /*
46      * Common properties
47      */
48     mtime_t i_pts;
49
50 };
51
52 /****************************************************************************
53  * Local prototypes
54  ****************************************************************************/
55 static int  OpenDecoder   ( vlc_object_t * );
56 static int  OpenPacketizer( vlc_object_t * );
57 static void CloseDecoder  ( vlc_object_t * );
58
59 static void *DecodeBlock  ( decoder_t *, block_t ** );
60
61 static picture_t *DecodeFrame( decoder_t *, block_t * );
62 static block_t   *SendFrame  ( decoder_t *, block_t * );
63
64 /*****************************************************************************
65  * Module descriptor
66  *****************************************************************************/
67 vlc_module_begin();
68     set_description( _("Pseudo raw video decoder") );
69     set_capability( "decoder", 50 );
70     set_category( CAT_INPUT );
71     set_subcategory( SUBCAT_INPUT_VCODEC );
72     set_callbacks( OpenDecoder, CloseDecoder );
73
74     add_submodule();
75     set_description( _("Pseudo raw video packetizer") );
76     set_capability( "packetizer", 100 );
77     set_callbacks( OpenPacketizer, CloseDecoder );
78 vlc_module_end();
79
80 /*****************************************************************************
81  * OpenDecoder: probe the decoder and return score
82  *****************************************************************************/
83 static int OpenDecoder( vlc_object_t *p_this )
84 {
85     decoder_t *p_dec = (decoder_t*)p_this;
86     decoder_sys_t *p_sys;
87
88     switch( p_dec->fmt_in.i_codec )
89     {
90         /* Planar YUV */
91         case VLC_FOURCC('I','4','4','4'):
92         case VLC_FOURCC('I','4','2','2'):
93         case VLC_FOURCC('I','4','2','0'):
94         case VLC_FOURCC('Y','V','1','2'):
95         case VLC_FOURCC('I','Y','U','V'):
96         case VLC_FOURCC('I','4','1','1'):
97         case VLC_FOURCC('I','4','1','0'):
98         case VLC_FOURCC('Y','V','U','9'):
99         case VLC_FOURCC('Y','4','2','B'):
100         case VLC_FOURCC('Y','4','1','B'):
101
102         /* Packed YUV */
103         case VLC_FOURCC('Y','U','Y','2'):
104         case VLC_FOURCC('U','Y','V','Y'):
105
106         /* RGB */
107         case VLC_FOURCC('R','V','3','2'):
108         case VLC_FOURCC('R','V','2','4'):
109         case VLC_FOURCC('R','V','1','6'):
110         case VLC_FOURCC('R','V','1','5'):
111             break;
112         case VLC_FOURCC('2','V','u','y'):
113         case VLC_FOURCC('2','v','u','y'):
114         case VLC_FOURCC('A','V','U','I'):
115             p_dec->fmt_in.i_codec = VLC_FOURCC('U','Y','V','Y');
116             break;
117         case VLC_FOURCC('y','v','1','2'):
118             p_dec->fmt_in.i_codec = VLC_FOURCC('Y','V','1','2');
119             break;
120
121         default:
122             return VLC_EGENERIC;
123     }
124
125     /* Allocate the memory needed to store the decoder's structure */
126     if( ( p_dec->p_sys = p_sys =
127           (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
128     {
129         msg_Err( p_dec, "out of memory" );
130         return VLC_EGENERIC;
131     }
132     /* Misc init */
133     p_dec->p_sys->b_packetizer = VLC_FALSE;
134     p_sys->i_pts = 0;
135     p_sys->b_invert = 0;
136
137     if( (int)p_dec->fmt_in.video.i_height < 0 )
138     {
139         /* Frames are coded from bottom to top */
140         p_dec->fmt_in.video.i_height =
141             (unsigned int)(-(int)p_dec->fmt_in.video.i_height);
142         p_sys->b_invert = VLC_TRUE;
143     }
144
145     if( p_dec->fmt_in.video.i_width <= 0 || p_dec->fmt_in.video.i_height <= 0 )
146     {
147         msg_Err( p_dec, "invalid display size %dx%d",
148                  p_dec->fmt_in.video.i_width, p_dec->fmt_in.video.i_height );
149         return VLC_EGENERIC;
150     }
151
152     /* Find out p_vdec->i_raw_size */
153     vout_InitFormat( &p_dec->fmt_out.video, p_dec->fmt_in.i_codec,
154                      p_dec->fmt_in.video.i_width,
155                      p_dec->fmt_in.video.i_height,
156                      p_dec->fmt_in.video.i_aspect );
157     p_sys->i_raw_size = p_dec->fmt_out.video.i_bits_per_pixel *
158         p_dec->fmt_out.video.i_width * p_dec->fmt_out.video.i_height / 8;
159
160     /* Set output properties */
161     p_dec->fmt_out.i_cat = VIDEO_ES;
162     p_dec->fmt_out.i_codec = p_dec->fmt_in.i_codec;
163     if( !p_dec->fmt_in.video.i_aspect )
164     {
165         p_dec->fmt_out.video.i_aspect = VOUT_ASPECT_FACTOR *
166             p_dec->fmt_out.video.i_width / p_dec->fmt_out.video.i_height;
167     }
168     else p_dec->fmt_out.video.i_aspect = p_dec->fmt_in.video.i_aspect;
169
170     if( p_dec->fmt_in.video.i_rmask )
171         p_dec->fmt_out.video.i_rmask = p_dec->fmt_in.video.i_rmask;
172     if( p_dec->fmt_in.video.i_gmask )
173         p_dec->fmt_out.video.i_gmask = p_dec->fmt_in.video.i_gmask;
174     if( p_dec->fmt_in.video.i_bmask )
175         p_dec->fmt_out.video.i_bmask = p_dec->fmt_in.video.i_bmask;
176
177     /* Set callbacks */
178     p_dec->pf_decode_video = (picture_t *(*)(decoder_t *, block_t **))
179         DecodeBlock;
180     p_dec->pf_packetize    = (block_t *(*)(decoder_t *, block_t **))
181         DecodeBlock;
182
183     return VLC_SUCCESS;
184 }
185
186 static int OpenPacketizer( vlc_object_t *p_this )
187 {
188     decoder_t *p_dec = (decoder_t*)p_this;
189
190     int i_ret = OpenDecoder( p_this );
191
192     if( i_ret == VLC_SUCCESS ) p_dec->p_sys->b_packetizer = VLC_TRUE;
193
194     return i_ret;
195 }
196
197 /****************************************************************************
198  * DecodeBlock: the whole thing
199  ****************************************************************************
200  * This function must be fed with complete frames.
201  ****************************************************************************/
202 static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
203 {
204     decoder_sys_t *p_sys = p_dec->p_sys;
205     block_t *p_block;
206     void *p_buf;
207
208     if( !pp_block || !*pp_block ) return NULL;
209
210     p_block = *pp_block;
211
212     if( !p_sys->i_pts && !p_block->i_pts && !p_block->i_dts )
213     {
214         /* We've just started the stream, wait for the first PTS. */
215         block_Release( p_block );
216         return NULL;
217     }
218
219     /* Date management */
220     if( p_block->i_pts > 0 || p_block->i_dts > 0 )
221     {
222         if( p_block->i_pts > 0 ) p_sys->i_pts = p_block->i_pts;
223         else if( p_block->i_dts > 0 ) p_sys->i_pts = p_block->i_dts;
224     }
225
226     if( p_block->i_buffer < p_sys->i_raw_size )
227     {
228         msg_Warn( p_dec, "invalid frame size (%d < %d)",
229                   p_block->i_buffer, p_sys->i_raw_size );
230
231         block_Release( p_block );
232         return NULL;
233     }
234
235     if( p_sys->b_packetizer )
236     {
237         p_buf = SendFrame( p_dec, p_block );
238     }
239     else
240     {
241         p_buf = DecodeFrame( p_dec, p_block );
242     }
243
244     /* Date management: 1 frame per packet */
245     p_sys->i_pts += ( I64C(1000000) * 1.0 / 25 /*FIXME*/ );
246     *pp_block = NULL;
247
248     return p_buf;
249 }
250
251 /*****************************************************************************
252  * FillPicture:
253  *****************************************************************************/
254 static void FillPicture( decoder_t *p_dec, block_t *p_block, picture_t *p_pic )
255 {
256     uint8_t *p_src, *p_dst;
257     int i_src, i_plane, i_line, i_width;
258     decoder_sys_t *p_sys = p_dec->p_sys;
259
260     p_src = p_block->p_buffer;
261     i_src = p_block->i_buffer;
262
263     for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
264     {
265         p_dst = p_pic->p[i_plane].p_pixels;
266         i_width = p_pic->p[i_plane].i_pitch;
267
268         if( p_sys->b_invert )
269             p_src += (i_width * (p_pic->p[i_plane].i_visible_lines - 1));
270
271         for( i_line = 0; i_line < p_pic->p[i_plane].i_visible_lines; i_line++ )
272         {
273             p_dec->p_libvlc->pf_memcpy( p_dst, p_src, i_width );
274             p_src += p_sys->b_invert ? -i_width : i_width;
275             p_dst += i_width;
276         }
277
278         if( p_sys->b_invert )
279             p_src += (i_width * (p_pic->p[i_plane].i_visible_lines + 1));
280     }
281 }
282
283 /*****************************************************************************
284  * DecodeFrame: decodes a video frame.
285  *****************************************************************************/
286 static picture_t *DecodeFrame( decoder_t *p_dec, block_t *p_block )
287 {
288     decoder_sys_t *p_sys = p_dec->p_sys;
289     picture_t *p_pic;
290
291     /* Get a new picture */
292     p_pic = p_dec->pf_vout_buffer_new( p_dec );
293     if( !p_pic )
294     {
295         block_Release( p_block );
296         return NULL;
297     }
298
299     FillPicture( p_dec, p_block, p_pic );
300
301     p_pic->date = p_sys->i_pts;
302
303     block_Release( p_block );
304     return p_pic;
305 }
306
307 /*****************************************************************************
308  * SendFrame: send a video frame to the stream output.
309  *****************************************************************************/
310 static block_t *SendFrame( decoder_t *p_dec, block_t *p_block )
311 {
312     decoder_sys_t *p_sys = p_dec->p_sys;
313
314     p_block->i_dts = p_block->i_pts = p_sys->i_pts;
315
316     if( p_sys->b_invert )
317     {
318         picture_t pic;
319         uint8_t *p_tmp, *p_pixels;
320         int i, j;
321
322         /* Fill in picture_t fields */
323         vout_InitPicture( VLC_OBJECT(p_dec), &pic, p_dec->fmt_out.i_codec,
324                           p_dec->fmt_out.video.i_width,
325                           p_dec->fmt_out.video.i_height, VOUT_ASPECT_FACTOR );
326
327         if( !pic.i_planes )
328         {
329             msg_Err( p_dec, "unsupported chroma" );
330             return p_block;
331         }
332
333         p_tmp = malloc( pic.p[0].i_pitch );
334         p_pixels = p_block->p_buffer;
335         for( i = 0; i < pic.i_planes; i++ )
336         {
337             int i_pitch = pic.p[i].i_pitch;
338             uint8_t *p_top = p_pixels;
339             uint8_t *p_bottom = p_pixels + i_pitch *
340                 (pic.p[i].i_visible_lines - 1);
341
342             for( j = 0; j < pic.p[i].i_visible_lines / 2; j++ )
343             {
344                 p_dec->p_libvlc->pf_memcpy( p_tmp, p_bottom,
345                                          pic.p[i].i_visible_pitch  );
346                 p_dec->p_libvlc->pf_memcpy( p_bottom, p_top,
347                                          pic.p[i].i_visible_pitch  );
348                 p_dec->p_libvlc->pf_memcpy( p_top, p_tmp,
349                                          pic.p[i].i_visible_pitch  );
350                 p_top += i_pitch;
351                 p_bottom -= i_pitch;
352             }
353
354             p_pixels += i_pitch * pic.p[i].i_lines;
355         }
356         free( p_tmp );
357     }
358
359     return p_block;
360 }
361
362 /*****************************************************************************
363  * CloseDecoder: decoder destruction
364  *****************************************************************************/
365 static void CloseDecoder( vlc_object_t *p_this )
366 {
367     decoder_t *p_dec = (decoder_t*)p_this;
368     free( p_dec->p_sys );
369 }