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