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