]> git.sesse.net Git - vlc/blob - modules/codec/rawvideo.c
db610289791b3849b68c8806d67cfb4d839d6396
[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_common.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     date_t pts;
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( N_("Pseudo raw video decoder") )
73     set_capability( "decoder", 50 )
74     set_category( CAT_INPUT )
75     set_subcategory( SUBCAT_INPUT_VCODEC )
76     set_callbacks( OpenDecoder, CloseDecoder )
77
78     add_submodule ()
79     set_description( N_("Pseudo raw video packetizer") )
80     set_capability( "packetizer", 100 )
81     set_callbacks( OpenPacketizer, CloseDecoder )
82 vlc_module_end ()
83
84 /*****************************************************************************
85  * OpenDecoder: probe the decoder and return score
86  *****************************************************************************/
87 static int OpenDecoder( vlc_object_t *p_this )
88 {
89     decoder_t *p_dec = (decoder_t*)p_this;
90     decoder_sys_t *p_sys;
91
92     switch( p_dec->fmt_in.i_codec )
93     {
94         /* Planar YUV */
95         case VLC_CODEC_I444:
96         case VLC_CODEC_I422:
97         case VLC_CODEC_I420:
98         case VLC_CODEC_YV12:
99         case VLC_CODEC_I411:
100         case VLC_CODEC_I410:
101         case VLC_CODEC_GREY:
102         case VLC_CODEC_YUVP:
103
104         /* Packed YUV */
105         case VLC_CODEC_YUYV:
106         case VLC_CODEC_YVYU:
107         case VLC_CODEC_UYVY:
108         case VLC_CODEC_VYUY:
109
110         /* RGB */
111         case VLC_CODEC_RGB32:
112         case VLC_CODEC_RGB24:
113         case VLC_CODEC_RGB16:
114         case VLC_CODEC_RGB15:
115         case VLC_CODEC_RGB8:
116         case VLC_CODEC_RGBP:
117             break;
118
119         default:
120             return VLC_EGENERIC;
121     }
122
123     /* Allocate the memory needed to store the decoder's structure */
124     if( ( p_dec->p_sys = p_sys =
125           (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
126         return VLC_ENOMEM;
127     /* Misc init */
128     p_dec->p_sys->b_packetizer = false;
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 = 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     es_format_Copy( &p_dec->fmt_out, &p_dec->fmt_in );
147
148     date_Init( &p_sys->pts, p_dec->fmt_out.video.i_frame_rate,
149                p_dec->fmt_out.video.i_frame_rate_base );
150     if( p_dec->fmt_out.video.i_frame_rate == 0 ||
151         p_dec->fmt_out.video.i_frame_rate_base == 0)
152     {
153         msg_Warn( p_dec, "invalid frame rate %d/%d, using 25 fps instead",
154                   p_dec->fmt_out.video.i_frame_rate,
155                   p_dec->fmt_out.video.i_frame_rate_base);
156         date_Init( &p_sys->pts, 25, 1 );
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     if( !p_dec->fmt_in.video.i_aspect )
168     {
169         p_dec->fmt_out.video.i_aspect = VOUT_ASPECT_FACTOR *
170             p_dec->fmt_out.video.i_width / p_dec->fmt_out.video.i_height;
171     }
172
173     /* Set callbacks */
174     p_dec->pf_decode_video = (picture_t *(*)(decoder_t *, block_t **))
175         DecodeBlock;
176     p_dec->pf_packetize    = (block_t *(*)(decoder_t *, block_t **))
177         DecodeBlock;
178
179     return VLC_SUCCESS;
180 }
181
182 static int OpenPacketizer( vlc_object_t *p_this )
183 {
184     decoder_t *p_dec = (decoder_t*)p_this;
185
186     int i_ret = OpenDecoder( p_this );
187
188     if( i_ret == VLC_SUCCESS ) p_dec->p_sys->b_packetizer = true;
189
190     return i_ret;
191 }
192
193 /****************************************************************************
194  * DecodeBlock: the whole thing
195  ****************************************************************************
196  * This function must be fed with complete frames.
197  ****************************************************************************/
198 static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
199 {
200     decoder_sys_t *p_sys = p_dec->p_sys;
201     block_t *p_block;
202     void *p_buf;
203
204     if( !pp_block || !*pp_block ) return NULL;
205
206     p_block = *pp_block;
207
208
209     if( !p_block->i_pts && !p_block->i_dts && !date_Get( &p_sys->pts ) )
210     {
211         /* We've just started the stream, wait for the first PTS. */
212         block_Release( p_block );
213         return NULL;
214     }
215
216     /* Date management: If there is a pts avaliable, use that. */
217     if( p_block->i_pts )
218     {
219         date_Set( &p_sys->pts, p_block->i_pts );
220     }
221     else if( p_block->i_dts )
222     {
223         /* NB, davidf doesn't quite agree with this in general, it is ok
224          * for rawvideo since it is in order (ie pts=dts), however, it
225          * may not be ok for an out-of-order codec, so don't copy this
226          * without thinking */
227         date_Set( &p_sys->pts, p_block->i_dts );
228     }
229
230     if( p_block->i_buffer < p_sys->i_raw_size )
231     {
232         msg_Warn( p_dec, "invalid frame size (%zu < %zu)",
233                   p_block->i_buffer, p_sys->i_raw_size );
234
235         block_Release( p_block );
236         return NULL;
237     }
238
239     if( p_sys->b_packetizer )
240     {
241         p_buf = SendFrame( p_dec, p_block );
242     }
243     else
244     {
245         p_buf = DecodeFrame( p_dec, p_block );
246     }
247
248     /* Date management: 1 frame per packet */
249     date_Increment( &p_sys->pts, 1 );
250     *pp_block = NULL;
251
252     return p_buf;
253 }
254
255 /*****************************************************************************
256  * FillPicture:
257  *****************************************************************************/
258 static void FillPicture( decoder_t *p_dec, block_t *p_block, picture_t *p_pic )
259 {
260     int i_plane;
261     decoder_sys_t *p_sys = p_dec->p_sys;
262     uint8_t *p_src = p_block->p_buffer;
263
264     for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
265     {
266         int i_pitch = p_pic->p[i_plane].i_pitch;
267         int i_visible_pitch = p_pic->p[i_plane].i_visible_pitch;
268         int i_visible_lines = p_pic->p[i_plane].i_visible_lines;
269         uint8_t *p_dst = p_pic->p[i_plane].p_pixels;
270         uint8_t *p_dst_end = p_dst+i_pitch*i_visible_lines;
271
272         if( p_sys->b_invert )
273             for( p_dst_end -= i_pitch; p_dst <= p_dst_end;
274                  p_dst_end -= i_pitch, p_src += i_visible_pitch )
275                 vlc_memcpy( p_dst_end, p_src, i_visible_pitch );
276         else
277             for( ; p_dst < p_dst_end;
278                  p_dst += i_pitch, p_src += i_visible_pitch )
279                 vlc_memcpy( p_dst, p_src, i_visible_pitch );
280     }
281 }
282
283 /*****************************************************************************
284  * DecodeFrame: decodes a video frame.
285  *****************************************************************************/
286 static picture_t *DecodeFrame( decoder_t *p_dec, block_t *p_block )
287 {
288     decoder_sys_t *p_sys = p_dec->p_sys;
289     picture_t *p_pic;
290
291     /* Get a new picture */
292     p_pic = decoder_NewPicture( p_dec );
293     if( !p_pic )
294     {
295         block_Release( p_block );
296         return NULL;
297     }
298
299     FillPicture( p_dec, p_block, p_pic );
300
301     p_pic->date = date_Get( &p_sys->pts );
302     p_pic->b_progressive = true;
303
304     block_Release( p_block );
305     return p_pic;
306 }
307
308 /*****************************************************************************
309  * SendFrame: send a video frame to the stream output.
310  *****************************************************************************/
311 static block_t *SendFrame( decoder_t *p_dec, block_t *p_block )
312 {
313     decoder_sys_t *p_sys = p_dec->p_sys;
314
315     p_block->i_dts = p_block->i_pts = date_Get( &p_sys->pts );
316
317     if( p_sys->b_invert )
318     {
319         picture_t pic;
320         uint8_t *p_tmp, *p_pixels;
321         int i, j;
322
323         /* Fill in picture_t fields */
324         vout_InitPicture( VLC_OBJECT(p_dec), &pic, p_dec->fmt_out.i_codec,
325                           p_dec->fmt_out.video.i_width,
326                           p_dec->fmt_out.video.i_height, VOUT_ASPECT_FACTOR );
327
328         if( !pic.i_planes )
329         {
330             msg_Err( p_dec, "unsupported chroma" );
331             return p_block;
332         }
333
334         p_tmp = malloc( pic.p[0].i_pitch );
335         if( !p_tmp )
336             return p_block;
337         p_pixels = p_block->p_buffer;
338         for( i = 0; i < pic.i_planes; i++ )
339         {
340             int i_pitch = pic.p[i].i_pitch;
341             uint8_t *p_top = p_pixels;
342             uint8_t *p_bottom = p_pixels + i_pitch *
343                 (pic.p[i].i_visible_lines - 1);
344
345             for( j = 0; j < pic.p[i].i_visible_lines / 2; j++ )
346             {
347                 vlc_memcpy( p_tmp, p_bottom, pic.p[i].i_visible_pitch  );
348                 vlc_memcpy( p_bottom, p_top, pic.p[i].i_visible_pitch  );
349                 vlc_memcpy( p_top, p_tmp, pic.p[i].i_visible_pitch  );
350                 p_top += i_pitch;
351                 p_bottom -= i_pitch;
352             }
353
354             p_pixels += i_pitch * pic.p[i].i_lines;
355         }
356         free( p_tmp );
357     }
358
359     return p_block;
360 }
361
362 /*****************************************************************************
363  * CloseDecoder: decoder destruction
364  *****************************************************************************/
365 static void CloseDecoder( vlc_object_t *p_this )
366 {
367     decoder_t *p_dec = (decoder_t*)p_this;
368     free( p_dec->p_sys );
369 }