]> git.sesse.net Git - vlc/blob - modules/codec/rawvideo.c
* all: only include header that are needed (and no more stdlib.h, string.h
[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: rawvideo.c,v 1.9 2003/11/22 23:39:14 fenrir Exp $
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
30 /*****************************************************************************
31  * decoder_sys_t : raw video decoder descriptor
32  *****************************************************************************/
33 struct decoder_sys_t
34 {
35     /* Module mode */
36     vlc_bool_t b_packetizer;
37
38     /*
39      * Input properties
40      */
41     int i_raw_size;
42
43     /*
44      * Common properties
45      */
46     mtime_t i_pts;
47
48 };
49
50 /****************************************************************************
51  * Local prototypes
52  ****************************************************************************/
53 static int  OpenDecoder   ( vlc_object_t * );
54 static int  OpenPacketizer( vlc_object_t * );
55 static void CloseDecoder  ( vlc_object_t * );
56
57 static void *DecodeBlock  ( decoder_t *, block_t ** );
58
59 static picture_t *DecodeFrame( decoder_t *, block_t * );
60 static block_t   *SendFrame  ( decoder_t *, block_t * );
61
62 /*****************************************************************************
63  * Module descriptor
64  *****************************************************************************/
65 vlc_module_begin();
66     set_description( _("Pseudo Raw Video decoder") );
67     set_capability( "decoder", 50 );
68     set_callbacks( OpenDecoder, CloseDecoder );
69
70     add_submodule();
71     set_description( _("Pseudo Raw Video packetizer") );
72     set_capability( "packetizer", 100 );
73     set_callbacks( OpenPacketizer, CloseDecoder );
74 vlc_module_end();
75
76 /*****************************************************************************
77  * OpenDecoder: probe the decoder and return score
78  *****************************************************************************/
79 static int OpenDecoder( vlc_object_t *p_this )
80 {
81     decoder_t *p_dec = (decoder_t*)p_this;
82     decoder_sys_t *p_sys;
83
84     switch( p_dec->fmt_in.i_codec )
85     {
86         /* Planar YUV */
87         case VLC_FOURCC('I','4','4','4'):
88         case VLC_FOURCC('I','4','2','2'):
89         case VLC_FOURCC('I','4','2','0'):
90         case VLC_FOURCC('Y','V','1','2'):
91         case VLC_FOURCC('I','Y','U','V'):
92         case VLC_FOURCC('I','4','1','1'):
93         case VLC_FOURCC('I','4','1','0'):
94
95         /* Packed YUV */
96         case VLC_FOURCC('Y','U','Y','2'):
97         case VLC_FOURCC('U','Y','V','Y'):
98
99         /* RGB */
100         case VLC_FOURCC('R','V','3','2'):
101         case VLC_FOURCC('R','V','2','4'):
102         case VLC_FOURCC('R','V','1','6'):
103         case VLC_FOURCC('R','V','1','5'):
104             break;
105
106         default:
107             return VLC_EGENERIC;
108     }
109
110     /* Allocate the memory needed to store the decoder's structure */
111     if( ( p_dec->p_sys = p_sys =
112           (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
113     {
114         msg_Err( p_dec, "out of memory" );
115         return VLC_EGENERIC;
116     }
117     /* Misc init */
118     p_dec->p_sys->b_packetizer = VLC_FALSE;
119     p_sys->i_pts = 0;
120
121     if( p_dec->fmt_in.video.i_width <= 0 ||
122         p_dec->fmt_in.video.i_height <= 0 )
123     {
124         msg_Err( p_dec, "invalid display size %dx%d",
125                  p_dec->fmt_in.video.i_width, p_dec->fmt_in.video.i_height );
126         return VLC_EGENERIC;
127     }
128
129     /* Find out p_vdec->i_raw_size */
130     vout_InitFormat( &p_dec->fmt_out.video, p_dec->fmt_in.i_codec,
131                      p_dec->fmt_in.video.i_width,
132                      p_dec->fmt_in.video.i_height,
133                      p_dec->fmt_in.video.i_aspect );
134     p_sys->i_raw_size = p_dec->fmt_out.video.i_bits_per_pixel *
135         p_dec->fmt_out.video.i_width * p_dec->fmt_out.video.i_height / 8;
136
137     /* Set output properties */
138     p_dec->fmt_out.i_cat = VIDEO_ES;
139     p_dec->fmt_out.i_codec = p_dec->fmt_in.i_codec;
140     //if( !p_dec->fmt_in.video.i_aspect )
141     {
142         p_dec->fmt_out.video.i_aspect = VOUT_ASPECT_FACTOR *
143             p_dec->fmt_out.video.i_width / p_dec->fmt_out.video.i_height;
144     }
145
146     /* Set callbacks */
147     p_dec->pf_decode_video = (picture_t *(*)(decoder_t *, block_t **))
148         DecodeBlock;
149     p_dec->pf_packetize    = (block_t *(*)(decoder_t *, block_t **))
150         DecodeBlock;
151
152     return VLC_SUCCESS;
153 }
154
155 static int OpenPacketizer( vlc_object_t *p_this )
156 {
157     decoder_t *p_dec = (decoder_t*)p_this;
158
159     int i_ret = OpenDecoder( p_this );
160
161     if( i_ret == VLC_SUCCESS ) p_dec->p_sys->b_packetizer = VLC_TRUE;
162
163     return i_ret;
164 }
165
166 /****************************************************************************
167  * DecodeBlock: the whole thing
168  ****************************************************************************
169  * This function must be fed with complete frames.
170  ****************************************************************************/
171 static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
172 {
173     decoder_sys_t *p_sys = p_dec->p_sys;
174     block_t *p_block;
175     void *p_buf;
176
177     if( !pp_block || !*pp_block ) return NULL;
178
179     p_block = *pp_block;
180
181     if( !p_sys->i_pts && !p_block->i_pts )
182     {
183         /* We've just started the stream, wait for the first PTS. */
184         block_Release( p_block );
185         return NULL;
186     }
187
188     /* Date management */
189     if( p_block->i_pts > 0 && p_block->i_pts != p_sys->i_pts )
190     {
191         p_sys->i_pts = p_block->i_pts;
192     }
193
194     if( p_block->i_buffer < p_sys->i_raw_size )
195     {
196         msg_Warn( p_dec, "invalid frame size (%d < %d)",
197                   p_block->i_buffer, p_sys->i_raw_size );
198
199         block_Release( p_block );
200         return NULL;
201     }
202
203     if( p_sys->b_packetizer )
204     {
205         p_buf = SendFrame( p_dec, p_block );
206     }
207     else
208     {
209         p_buf = DecodeFrame( p_dec, p_block );
210     }
211
212     /* Date management: 1 frame per packet */
213     p_sys->i_pts += ( I64C(1000000) * 1.0 / 25 /*FIXME*/ );
214     *pp_block = NULL;
215
216     return p_buf;
217 }
218
219 /*****************************************************************************
220  * FillPicture:
221  *****************************************************************************/
222 static void FillPicture( decoder_t *p_dec, block_t *p_block, picture_t *p_pic )
223 {
224     uint8_t *p_src, *p_dst;
225     int i_src, i_plane, i_line, i_width;
226
227     p_src  = p_block->p_buffer;
228     i_src = p_block->i_buffer;
229
230     for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
231     {
232         p_dst = p_pic->p[i_plane].p_pixels;
233         i_width = p_pic->p[i_plane].i_visible_pitch;
234
235         for( i_line = 0; i_line < p_pic->p[i_plane].i_lines; i_line++ )
236         {
237             p_dec->p_vlc->pf_memcpy( p_dst, p_src, i_width );
238             p_src += i_width;
239             p_dst += p_pic->p[i_plane].i_pitch;
240         }
241     }
242 }
243
244 /*****************************************************************************
245  * DecodeFrame: decodes a video frame.
246  *****************************************************************************/
247 static picture_t *DecodeFrame( decoder_t *p_dec, block_t *p_block )
248 {
249     decoder_sys_t *p_sys = p_dec->p_sys;
250     picture_t *p_pic;
251
252     /* Get a new picture */
253     p_pic = p_dec->pf_vout_buffer_new( p_dec );
254     if( !p_pic )
255     {
256         block_Release( p_block );
257         return NULL;
258     }
259
260     FillPicture( p_dec, p_block, p_pic );
261
262     p_pic->date = p_sys->i_pts;
263
264     block_Release( p_block );
265     return p_pic;
266 }
267
268 /*****************************************************************************
269  * SendFrame: send a video frame to the stream output.
270  *****************************************************************************/
271 static block_t *SendFrame( decoder_t *p_dec, block_t *p_block )
272 {
273     decoder_sys_t *p_sys = p_dec->p_sys;
274
275     p_block->i_dts = p_block->i_pts = p_sys->i_pts;
276
277     return p_block;
278 }
279
280 /*****************************************************************************
281  * CloseDecoder: decoder destruction
282  *****************************************************************************/
283 static void CloseDecoder( vlc_object_t *p_this )
284 {
285     decoder_t *p_dec = (decoder_t*)p_this;
286     free( p_dec->p_sys );
287 }