]> git.sesse.net Git - vlc/blob - modules/codec/rawvideo.c
Don't include config.h from the headers - refs #297.
[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 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <vlc/vlc.h>
32 #include <vlc_codec.h>
33 #include <vlc_vout.h>
34
35 /*****************************************************************************
36  * decoder_sys_t : raw video decoder descriptor
37  *****************************************************************************/
38 struct decoder_sys_t
39 {
40     /* Module mode */
41     vlc_bool_t b_packetizer;
42
43     /*
44      * Input properties
45      */
46     int i_raw_size;
47     vlc_bool_t b_invert;
48
49     /*
50      * Common properties
51      */
52     mtime_t i_pts;
53
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( _("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( _("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_FOURCC('I','4','4','4'):
96         case VLC_FOURCC('I','4','2','2'):
97         case VLC_FOURCC('I','4','2','0'):
98         case VLC_FOURCC('Y','V','1','2'):
99         case VLC_FOURCC('I','Y','U','V'):
100         case VLC_FOURCC('I','4','1','1'):
101         case VLC_FOURCC('I','4','1','0'):
102         case VLC_FOURCC('Y','V','U','9'):
103         case VLC_FOURCC('Y','4','2','B'):
104         case VLC_FOURCC('Y','4','1','B'):
105
106         /* Packed YUV */
107         case VLC_FOURCC('Y','U','Y','2'):
108         case VLC_FOURCC('Y','8','0','0'):
109         case VLC_FOURCC('U','Y','V','Y'):
110         case VLC_FOURCC('H','D','Y','C'):
111
112         /* RGB */
113         case VLC_FOURCC('R','V','3','2'):
114         case VLC_FOURCC('R','V','2','4'):
115         case VLC_FOURCC('R','V','1','6'):
116         case VLC_FOURCC('R','V','1','5'):
117             break;
118         case VLC_FOURCC('2','V','u','y'):
119         case VLC_FOURCC('2','v','u','y'):
120         case VLC_FOURCC('A','V','U','I'):
121             p_dec->fmt_in.i_codec = VLC_FOURCC('U','Y','V','Y');
122             break;
123         case VLC_FOURCC('y','v','1','2'):
124             p_dec->fmt_in.i_codec = VLC_FOURCC('Y','V','1','2');
125             break;
126
127         default:
128             return VLC_EGENERIC;
129     }
130
131     /* Allocate the memory needed to store the decoder's structure */
132     if( ( p_dec->p_sys = p_sys =
133           (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
134     {
135         msg_Err( p_dec, "out of memory" );
136         return VLC_EGENERIC;
137     }
138     /* Misc init */
139     p_dec->p_sys->b_packetizer = VLC_FALSE;
140     p_sys->i_pts = 0;
141     p_sys->b_invert = 0;
142
143     if( (int)p_dec->fmt_in.video.i_height < 0 )
144     {
145         /* Frames are coded from bottom to top */
146         p_dec->fmt_in.video.i_height =
147             (unsigned int)(-(int)p_dec->fmt_in.video.i_height);
148         p_sys->b_invert = VLC_TRUE;
149     }
150
151     if( p_dec->fmt_in.video.i_width <= 0 || p_dec->fmt_in.video.i_height <= 0 )
152     {
153         msg_Err( p_dec, "invalid display size %dx%d",
154                  p_dec->fmt_in.video.i_width, p_dec->fmt_in.video.i_height );
155         return VLC_EGENERIC;
156     }
157
158     /* Find out p_vdec->i_raw_size */
159     vout_InitFormat( &p_dec->fmt_out.video, p_dec->fmt_in.i_codec,
160                      p_dec->fmt_in.video.i_width,
161                      p_dec->fmt_in.video.i_height,
162                      p_dec->fmt_in.video.i_aspect );
163     p_sys->i_raw_size = p_dec->fmt_out.video.i_bits_per_pixel *
164         p_dec->fmt_out.video.i_width * p_dec->fmt_out.video.i_height / 8;
165
166     /* Set output properties */
167     p_dec->fmt_out.i_cat = VIDEO_ES;
168     p_dec->fmt_out.i_codec = p_dec->fmt_in.i_codec;
169     if( !p_dec->fmt_in.video.i_aspect )
170     {
171         p_dec->fmt_out.video.i_aspect = VOUT_ASPECT_FACTOR *
172             p_dec->fmt_out.video.i_width / p_dec->fmt_out.video.i_height;
173     }
174     else p_dec->fmt_out.video.i_aspect = p_dec->fmt_in.video.i_aspect;
175
176     if( p_dec->fmt_in.video.i_rmask )
177         p_dec->fmt_out.video.i_rmask = p_dec->fmt_in.video.i_rmask;
178     if( p_dec->fmt_in.video.i_gmask )
179         p_dec->fmt_out.video.i_gmask = p_dec->fmt_in.video.i_gmask;
180     if( p_dec->fmt_in.video.i_bmask )
181         p_dec->fmt_out.video.i_bmask = p_dec->fmt_in.video.i_bmask;
182
183     /* Set callbacks */
184     p_dec->pf_decode_video = (picture_t *(*)(decoder_t *, block_t **))
185         DecodeBlock;
186     p_dec->pf_packetize    = (block_t *(*)(decoder_t *, block_t **))
187         DecodeBlock;
188
189     return VLC_SUCCESS;
190 }
191
192 static int OpenPacketizer( vlc_object_t *p_this )
193 {
194     decoder_t *p_dec = (decoder_t*)p_this;
195
196     int i_ret = OpenDecoder( p_this );
197
198     if( i_ret == VLC_SUCCESS ) p_dec->p_sys->b_packetizer = VLC_TRUE;
199
200     return i_ret;
201 }
202
203 /****************************************************************************
204  * DecodeBlock: the whole thing
205  ****************************************************************************
206  * This function must be fed with complete frames.
207  ****************************************************************************/
208 static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
209 {
210     decoder_sys_t *p_sys = p_dec->p_sys;
211     block_t *p_block;
212     void *p_buf;
213
214     if( !pp_block || !*pp_block ) return NULL;
215
216     p_block = *pp_block;
217
218     if( !p_sys->i_pts && !p_block->i_pts && !p_block->i_dts )
219     {
220         /* We've just started the stream, wait for the first PTS. */
221         block_Release( p_block );
222         return NULL;
223     }
224
225     /* Date management */
226     if( p_block->i_pts > 0 || p_block->i_dts > 0 )
227     {
228         if( p_block->i_pts > 0 ) p_sys->i_pts = p_block->i_pts;
229         else if( p_block->i_dts > 0 ) p_sys->i_pts = p_block->i_dts;
230     }
231
232     if( p_block->i_buffer < p_sys->i_raw_size )
233     {
234         msg_Warn( p_dec, "invalid frame size (%d < %d)",
235                   p_block->i_buffer, p_sys->i_raw_size );
236
237         block_Release( p_block );
238         return NULL;
239     }
240
241     if( p_sys->b_packetizer )
242     {
243         p_buf = SendFrame( p_dec, p_block );
244     }
245     else
246     {
247         p_buf = DecodeFrame( p_dec, p_block );
248     }
249
250     /* Date management: 1 frame per packet */
251     p_sys->i_pts += ( I64C(1000000) * 1.0 / 25 /*FIXME*/ );
252     *pp_block = NULL;
253
254     return p_buf;
255 }
256
257 /*****************************************************************************
258  * FillPicture:
259  *****************************************************************************/
260 static void FillPicture( decoder_t *p_dec, block_t *p_block, picture_t *p_pic )
261 {
262     uint8_t *p_src, *p_dst;
263     int i_src, i_plane, i_line, i_width;
264     decoder_sys_t *p_sys = p_dec->p_sys;
265
266     p_src = p_block->p_buffer;
267     i_src = p_block->i_buffer;
268
269     for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
270     {
271         p_dst = p_pic->p[i_plane].p_pixels;
272         i_width = p_pic->p[i_plane].i_pitch;
273
274         if( p_sys->b_invert )
275             p_src += (i_width * (p_pic->p[i_plane].i_visible_lines - 1));
276
277         for( i_line = 0; i_line < p_pic->p[i_plane].i_visible_lines; i_line++ )
278         {
279             p_dec->p_libvlc->pf_memcpy( p_dst, p_src, i_width );
280             p_src += p_sys->b_invert ? -i_width : i_width;
281             p_dst += i_width;
282         }
283
284         if( p_sys->b_invert )
285             p_src += (i_width * (p_pic->p[i_plane].i_visible_lines + 1));
286     }
287 }
288
289 /*****************************************************************************
290  * DecodeFrame: decodes a video frame.
291  *****************************************************************************/
292 static picture_t *DecodeFrame( decoder_t *p_dec, block_t *p_block )
293 {
294     decoder_sys_t *p_sys = p_dec->p_sys;
295     picture_t *p_pic;
296
297     /* Get a new picture */
298     p_pic = p_dec->pf_vout_buffer_new( p_dec );
299     if( !p_pic )
300     {
301         block_Release( p_block );
302         return NULL;
303     }
304
305     FillPicture( p_dec, p_block, p_pic );
306
307     p_pic->date = p_sys->i_pts;
308
309     block_Release( p_block );
310     return p_pic;
311 }
312
313 /*****************************************************************************
314  * SendFrame: send a video frame to the stream output.
315  *****************************************************************************/
316 static block_t *SendFrame( decoder_t *p_dec, block_t *p_block )
317 {
318     decoder_sys_t *p_sys = p_dec->p_sys;
319
320     p_block->i_dts = p_block->i_pts = p_sys->i_pts;
321
322     if( p_sys->b_invert )
323     {
324         picture_t pic;
325         uint8_t *p_tmp, *p_pixels;
326         int i, j;
327
328         /* Fill in picture_t fields */
329         vout_InitPicture( VLC_OBJECT(p_dec), &pic, p_dec->fmt_out.i_codec,
330                           p_dec->fmt_out.video.i_width,
331                           p_dec->fmt_out.video.i_height, VOUT_ASPECT_FACTOR );
332
333         if( !pic.i_planes )
334         {
335             msg_Err( p_dec, "unsupported chroma" );
336             return p_block;
337         }
338
339         p_tmp = malloc( pic.p[0].i_pitch );
340         p_pixels = p_block->p_buffer;
341         for( i = 0; i < pic.i_planes; i++ )
342         {
343             int i_pitch = pic.p[i].i_pitch;
344             uint8_t *p_top = p_pixels;
345             uint8_t *p_bottom = p_pixels + i_pitch *
346                 (pic.p[i].i_visible_lines - 1);
347
348             for( j = 0; j < pic.p[i].i_visible_lines / 2; j++ )
349             {
350                 p_dec->p_libvlc->pf_memcpy( p_tmp, p_bottom,
351                                          pic.p[i].i_visible_pitch  );
352                 p_dec->p_libvlc->pf_memcpy( p_bottom, p_top,
353                                          pic.p[i].i_visible_pitch  );
354                 p_dec->p_libvlc->pf_memcpy( p_top, p_tmp,
355                                          pic.p[i].i_visible_pitch  );
356                 p_top += i_pitch;
357                 p_bottom -= i_pitch;
358             }
359
360             p_pixels += i_pitch * pic.p[i].i_lines;
361         }
362         free( p_tmp );
363     }
364
365     return p_block;
366 }
367
368 /*****************************************************************************
369  * CloseDecoder: decoder destruction
370  *****************************************************************************/
371 static void CloseDecoder( vlc_object_t *p_this )
372 {
373     decoder_t *p_dec = (decoder_t*)p_this;
374     free( p_dec->p_sys );
375 }