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