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