]> git.sesse.net Git - vlc/blob - modules/codec/rawvideo.c
mediacodec: fix warning
[vlc] / modules / codec / rawvideo.c
1 /*****************************************************************************
2  * rawvideo.c: Pseudo video decoder/packetizer for raw video data
3  *****************************************************************************
4  * Copyright (C) 2001, 2002 VLC authors and VideoLAN
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 it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * 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     /*
41      * Input properties
42      */
43     size_t size;
44     unsigned pitches[PICTURE_PLANE_MAX];
45     unsigned lines[PICTURE_PLANE_MAX];
46
47     /*
48      * Common properties
49      */
50     date_t pts;
51 };
52
53 /****************************************************************************
54  * Local prototypes
55  ****************************************************************************/
56 static int  OpenDecoder   ( vlc_object_t * );
57 static int  OpenPacketizer( vlc_object_t * );
58 static void CloseCommon   ( vlc_object_t * );
59
60 /*****************************************************************************
61  * Module descriptor
62  *****************************************************************************/
63 vlc_module_begin ()
64     set_description( N_("Pseudo raw video decoder") )
65     set_capability( "decoder", 50 )
66     set_category( CAT_INPUT )
67     set_subcategory( SUBCAT_INPUT_VCODEC )
68     set_callbacks( OpenDecoder, CloseCommon )
69
70     add_submodule ()
71     set_description( N_("Pseudo raw video packetizer") )
72     set_capability( "packetizer", 100 )
73     set_callbacks( OpenPacketizer, CloseCommon )
74 vlc_module_end ()
75
76 /**
77  * Common initialization for decoder and packetizer
78  */
79 static int OpenCommon( decoder_t *p_dec )
80 {
81     const vlc_chroma_description_t *dsc =
82         vlc_fourcc_GetChromaDescription( p_dec->fmt_in.i_codec );
83     if( dsc == NULL || dsc->plane_count == 0 )
84         return VLC_EGENERIC;
85
86     if( p_dec->fmt_in.video.i_width <= 0 || p_dec->fmt_in.video.i_height == 0 )
87     {
88         msg_Err( p_dec, "invalid display size %dx%d",
89                  p_dec->fmt_in.video.i_width, p_dec->fmt_in.video.i_height );
90         return VLC_EGENERIC;
91     }
92
93     /* Allocate the memory needed to store the decoder's structure */
94     decoder_sys_t *p_sys = calloc(1, sizeof(*p_sys));
95     if( unlikely(p_sys == NULL) )
96         return VLC_ENOMEM;
97
98     if( !p_dec->fmt_in.video.i_visible_width )
99         p_dec->fmt_in.video.i_visible_width = p_dec->fmt_in.video.i_width;
100     if( !p_dec->fmt_in.video.i_visible_height )
101         p_dec->fmt_in.video.i_visible_height = p_dec->fmt_in.video.i_height;
102
103     es_format_Copy( &p_dec->fmt_out, &p_dec->fmt_in );
104
105     date_Init( &p_sys->pts, p_dec->fmt_out.video.i_frame_rate,
106                p_dec->fmt_out.video.i_frame_rate_base );
107     if( p_dec->fmt_out.video.i_frame_rate == 0 ||
108         p_dec->fmt_out.video.i_frame_rate_base == 0)
109     {
110         msg_Warn( p_dec, "invalid frame rate %d/%d, using 25 fps instead",
111                   p_dec->fmt_out.video.i_frame_rate,
112                   p_dec->fmt_out.video.i_frame_rate_base);
113         date_Init( &p_sys->pts, 25, 1 );
114     }
115
116     for( unsigned i = 0; i < dsc->plane_count; i++ )
117     {
118         unsigned pitch = p_dec->fmt_in.video.i_width * dsc->pixel_size
119                          * dsc->p[i].w.num / dsc->p[i].w.den;
120         unsigned lines = p_dec->fmt_in.video.i_height
121                          * dsc->p[i].h.num / dsc->p[i].h.den;
122
123         p_sys->pitches[i] = pitch;
124         p_sys->lines[i] = lines;
125         p_sys->size += pitch * lines;
126     }
127
128     p_dec->p_sys           = p_sys;
129     return VLC_SUCCESS;
130 }
131
132 /****************************************************************************
133  * DecodeBlock: the whole thing
134  ****************************************************************************
135  * This function must be fed with complete frames.
136  ****************************************************************************/
137 static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
138 {
139     decoder_sys_t *p_sys = p_dec->p_sys;
140
141     if( pp_block == NULL || *pp_block == NULL )
142         return NULL;
143
144     block_t *p_block = *pp_block;
145
146     if( p_block->i_pts <= VLC_TS_INVALID && p_block->i_dts <= VLC_TS_INVALID &&
147         !date_Get( &p_sys->pts ) )
148     {
149         /* We've just started the stream, wait for the first PTS. */
150         block_Release( p_block );
151         return NULL;
152     }
153
154     /* Date management: If there is a pts avaliable, use that. */
155     if( p_block->i_pts > VLC_TS_INVALID )
156     {
157         date_Set( &p_sys->pts, p_block->i_pts );
158     }
159     else if( p_block->i_dts > VLC_TS_INVALID )
160     {
161         /* NB, davidf doesn't quite agree with this in general, it is ok
162          * for rawvideo since it is in order (ie pts=dts), however, it
163          * may not be ok for an out-of-order codec, so don't copy this
164          * without thinking */
165         date_Set( &p_sys->pts, p_block->i_dts );
166     }
167
168     if( p_block->i_buffer < p_sys->size )
169     {
170         msg_Warn( p_dec, "invalid frame size (%zu < %zu)",
171                   p_block->i_buffer, p_sys->size );
172
173         block_Release( p_block );
174         return NULL;
175     }
176
177     *pp_block = NULL;
178     return p_block;
179 }
180
181 /*****************************************************************************
182  * FillPicture:
183  *****************************************************************************/
184 static void FillPicture( decoder_t *p_dec, block_t *p_block, picture_t *p_pic )
185 {
186     decoder_sys_t *p_sys = p_dec->p_sys;
187     const uint8_t *p_src = p_block->p_buffer;
188
189     for( int i = 0; i < p_pic->i_planes; i++ )
190     {
191         uint8_t *p_dst = p_pic->p[i].p_pixels;
192
193         for( int x = 0; x < p_pic->p[i].i_visible_lines; x++ )
194         {
195             memcpy( p_dst, p_src, p_pic->p[i].i_visible_pitch );
196             p_src += p_sys->pitches[i];
197             p_dst += p_pic->p[i].i_pitch;
198         }
199
200         p_src += p_sys->pitches[i]
201                * (p_sys->lines[i] - p_pic->p[i].i_visible_lines);
202     }
203 }
204
205 /*****************************************************************************
206  * DecodeFrame: decodes a video frame.
207  *****************************************************************************/
208 static picture_t *DecodeFrame( decoder_t *p_dec, block_t **pp_block )
209 {
210     block_t *p_block = DecodeBlock( p_dec, pp_block );
211     if( p_block == NULL )
212         return NULL;
213
214     decoder_sys_t *p_sys = p_dec->p_sys;
215
216     /* Get a new picture */
217     picture_t *p_pic = decoder_NewPicture( p_dec );
218     if( p_pic == NULL )
219     {
220         block_Release( p_block );
221         return NULL;
222     }
223
224     FillPicture( p_dec, p_block, p_pic );
225
226     /* Date management: 1 frame per packet */
227     p_pic->date = date_Get( &p_dec->p_sys->pts );
228     date_Increment( &p_sys->pts, 1 );
229
230     if( p_block->i_flags & BLOCK_FLAG_INTERLACED_MASK )
231     {
232         p_pic->b_progressive = false;
233         p_pic->i_nb_fields = 2;
234         if( p_block->i_flags & BLOCK_FLAG_TOP_FIELD_FIRST )
235             p_pic->b_top_field_first = true;
236         else
237             p_pic->b_top_field_first = false;
238     }
239     else
240         p_pic->b_progressive = true;
241
242     block_Release( p_block );
243     return p_pic;
244 }
245
246 static int OpenDecoder( vlc_object_t *p_this )
247 {
248     decoder_t *p_dec = (decoder_t *)p_this;
249
250     int ret = OpenCommon( p_dec );
251     if( ret == VLC_SUCCESS )
252         p_dec->pf_decode_video = DecodeFrame;
253     return ret;
254 }
255
256 /*****************************************************************************
257  * SendFrame: send a video frame to the stream output.
258  *****************************************************************************/
259 static block_t *SendFrame( decoder_t *p_dec, block_t **pp_block )
260 {
261     block_t *p_block = DecodeBlock( p_dec, pp_block );
262     if( p_block == NULL )
263         return NULL;
264
265     decoder_sys_t *p_sys = p_dec->p_sys;
266
267     /* Date management: 1 frame per packet */
268     p_block->i_dts = p_block->i_pts = date_Get( &p_sys->pts );
269     date_Increment( &p_sys->pts, 1 );
270     return p_block;
271 }
272
273 static int OpenPacketizer( vlc_object_t *p_this )
274 {
275     decoder_t *p_dec = (decoder_t *)p_this;
276
277     int ret = OpenCommon( p_dec );
278     if( ret == VLC_SUCCESS )
279         p_dec->pf_packetize = SendFrame;
280     return ret;
281 }
282
283 /**
284  * Common deinitialization
285  */
286 static void CloseCommon( vlc_object_t *p_this )
287 {
288     decoder_t *p_dec = (decoder_t*)p_this;
289     free( p_dec->p_sys );
290 }