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