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