Coverage Report

Created: 2025-07-11 07:16

/src/vlc/modules/codec/rawvideo.c
Line
Count
Source (jump to first uncovered line)
1
/*****************************************************************************
2
 * rawvideo.c: Pseudo video decoder/packetizer for raw video data
3
 *****************************************************************************
4
 * Copyright (C) 2001, 2002 VLC authors and VideoLAN
5
 *
6
 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
7
 *
8
 * This program is free software; you can redistribute it and/or modify it
9
 * under the terms of the GNU Lesser General Public License as published by
10
 * the Free Software Foundation; either version 2.1 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
 * GNU Lesser General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public License
19
 * along with this program; if not, write to the Free Software Foundation,
20
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21
 *****************************************************************************/
22
23
/*****************************************************************************
24
 * Preamble
25
 *****************************************************************************/
26
#ifdef HAVE_CONFIG_H
27
# include "config.h"
28
#endif
29
30
#include <vlc_common.h>
31
#include <vlc_plugin.h>
32
#include <vlc_codec.h>
33
#include <vlc_ancillary.h>
34
35
/*****************************************************************************
36
 * decoder_sys_t : raw video decoder descriptor
37
 *****************************************************************************/
38
typedef struct
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
} decoder_sys_t;
52
53
/****************************************************************************
54
 * Local prototypes
55
 ****************************************************************************/
56
static int  OpenDecoder   ( vlc_object_t * );
57
static int  OpenPacketizer( vlc_object_t * );
58
59
/*****************************************************************************
60
 * Module descriptor
61
 *****************************************************************************/
62
104
vlc_module_begin ()
63
52
    set_description( N_("Pseudo raw video decoder") )
64
52
    set_capability( "video decoder", 50 )
65
52
    set_subcategory( SUBCAT_INPUT_VCODEC )
66
52
    set_callback( OpenDecoder )
67
68
52
    add_submodule ()
69
52
    set_description( N_("Pseudo raw video packetizer") )
70
52
    set_capability( "video packetizer", 100 )
71
52
    set_callback( OpenPacketizer )
72
52
vlc_module_end ()
73
74
/**
75
 * Common initialization for decoder and packetizer
76
 */
77
static int OpenCommon( decoder_t *p_dec )
78
69.8k
{
79
69.8k
    const vlc_chroma_description_t *dsc =
80
69.8k
        vlc_fourcc_GetChromaDescription( p_dec->fmt_in->i_codec );
81
69.8k
    if( dsc == NULL || dsc->plane_count == 0 )
82
68.1k
        return VLC_EGENERIC;
83
84
1.66k
    if( p_dec->fmt_in->video.i_width <= 0 || p_dec->fmt_in->video.i_height == 0 )
85
100
    {
86
100
        msg_Err( p_dec, "invalid display size %dx%d",
87
100
                 p_dec->fmt_in->video.i_width, p_dec->fmt_in->video.i_height );
88
100
        return VLC_EGENERIC;
89
100
    }
90
91
    /* Allocate the memory needed to store the decoder's structure */
92
1.56k
    decoder_sys_t *p_sys = vlc_obj_calloc(VLC_OBJECT(p_dec), 1, sizeof(*p_sys));
93
1.56k
    if( unlikely(p_sys == NULL) )
94
0
        return VLC_ENOMEM;
95
96
1.56k
    es_format_Copy( &p_dec->fmt_out, p_dec->fmt_in );
97
98
1.56k
    if( !p_dec->fmt_out.video.i_visible_width )
99
0
        p_dec->fmt_out.video.i_visible_width = p_dec->fmt_out.video.i_width;
100
1.56k
    if( !p_dec->fmt_out.video.i_visible_height )
101
0
        p_dec->fmt_out.video.i_visible_height = p_dec->fmt_out.video.i_height;
102
103
1.56k
    if( p_dec->fmt_in->i_codec == VLC_CODEC_YUV2 )
104
28
    {
105
28
        p_dec->fmt_out.video.i_chroma =
106
28
        p_dec->fmt_out.i_codec = VLC_CODEC_YUYV;
107
28
    }
108
109
1.56k
    if( p_dec->fmt_out.video.i_frame_rate == 0 ||
110
1.56k
        p_dec->fmt_out.video.i_frame_rate_base == 0)
111
692
    {
112
692
        msg_Warn( p_dec, "invalid frame rate %d/%d, using 25 fps instead",
113
692
                  p_dec->fmt_out.video.i_frame_rate,
114
692
                  p_dec->fmt_out.video.i_frame_rate_base);
115
692
        date_Init( &p_sys->pts, 25, 1 );
116
692
    }
117
870
    else
118
870
        date_Init( &p_sys->pts, p_dec->fmt_out.video.i_frame_rate,
119
870
                    p_dec->fmt_out.video.i_frame_rate_base );
120
121
4.26k
    for( unsigned i = 0; i < dsc->plane_count; i++ )
122
2.70k
    {
123
2.70k
        unsigned pitch = ((p_dec->fmt_in->video.i_width + (dsc->p[i].w.den - 1)) / dsc->p[i].w.den)
124
2.70k
                         * dsc->p[i].w.num * dsc->pixel_size;
125
2.70k
        unsigned lines = ((p_dec->fmt_in->video.i_height + (dsc->p[i].h.den - 1)) / dsc->p[i].h.den)
126
2.70k
                         * dsc->p[i].h.num;
127
128
2.70k
        p_sys->pitches[i] = pitch;
129
2.70k
        p_sys->lines[i] = lines;
130
2.70k
        p_sys->size += pitch * lines;
131
2.70k
    }
132
133
1.56k
    p_dec->p_sys           = p_sys;
134
1.56k
    return VLC_SUCCESS;
135
1.56k
}
136
137
/*****************************************************************************
138
 * Flush:
139
 *****************************************************************************/
140
static void Flush( decoder_t *p_dec )
141
0
{
142
0
    decoder_sys_t *p_sys = p_dec->p_sys;
143
144
0
    date_Set( &p_sys->pts, VLC_TICK_INVALID );
145
0
}
146
147
/****************************************************************************
148
 * DecodeBlock: the whole thing
149
 ****************************************************************************
150
 * This function must be fed with complete frames.
151
 ****************************************************************************/
152
static block_t *DecodeBlock( decoder_t *p_dec, block_t *p_block )
153
6.17k
{
154
6.17k
    decoder_sys_t *p_sys = p_dec->p_sys;
155
156
6.17k
    if( p_block->i_flags & (BLOCK_FLAG_CORRUPTED|BLOCK_FLAG_DISCONTINUITY) )
157
0
    {
158
0
        date_Set( &p_sys->pts, p_block->i_dts );
159
0
        if( p_block->i_flags & BLOCK_FLAG_CORRUPTED )
160
0
        {
161
0
            block_Release( p_block );
162
0
            return NULL;
163
0
        }
164
0
    }
165
166
6.17k
    if( p_block->i_pts == VLC_TICK_INVALID && p_block->i_dts == VLC_TICK_INVALID &&
167
6.17k
        date_Get( &p_sys->pts ) == VLC_TICK_INVALID )
168
0
    {
169
        /* We've just started the stream, wait for the first PTS. */
170
0
        block_Release( p_block );
171
0
        return NULL;
172
0
    }
173
174
    /* Date management: If there is a pts available, use that. */
175
6.17k
    if( p_block->i_pts != VLC_TICK_INVALID )
176
2.89k
    {
177
2.89k
        date_Set( &p_sys->pts, p_block->i_pts );
178
2.89k
    }
179
3.27k
    else
180
3.27k
    {
181
3.27k
        if( p_block->i_dts != VLC_TICK_INVALID )
182
            /* NB, davidf doesn't quite agree with this in general, it is ok
183
             * for rawvideo since it is in order (ie pts=dts), however, it
184
             * may not be ok for an out-of-order codec, so don't copy this
185
             * without thinking */
186
3.27k
            date_Set( &p_sys->pts, p_block->i_dts );
187
188
3.27k
        p_block->i_pts = date_Get( &p_sys->pts );
189
3.27k
    }
190
191
6.17k
    if( p_block->i_buffer < p_sys->size )
192
1.29k
    {
193
1.29k
        msg_Warn( p_dec, "invalid frame size (%zu < %zu)",
194
1.29k
                  p_block->i_buffer, p_sys->size );
195
196
1.29k
        block_Release( p_block );
197
1.29k
        return NULL;
198
1.29k
    }
199
200
    /* Date management: 1 frame per packet */
201
4.87k
    date_Increment( &p_sys->pts, 1 );
202
203
4.87k
    return p_block;
204
6.17k
}
205
206
/*****************************************************************************
207
 * FillPicture:
208
 *****************************************************************************/
209
static void FillPicture( decoder_t *p_dec, block_t *p_block, picture_t *p_pic )
210
573
{
211
573
    decoder_sys_t *p_sys = p_dec->p_sys;
212
573
    const uint8_t *p_src = p_block->p_buffer;
213
214
1.71k
    for( int i = 0; i < p_pic->i_planes; i++ )
215
1.14k
    {
216
1.14k
        uint8_t *p_dst = p_pic->p[i].p_pixels;
217
218
7.32M
        for( int x = 0; x < p_pic->p[i].i_visible_lines; x++ )
219
7.32M
        {
220
7.32M
            memcpy( p_dst, p_src, p_pic->p[i].i_visible_pitch );
221
            /*Fix chroma sign.*/
222
7.32M
            if( p_dec->fmt_in->i_codec == VLC_CODEC_YUV2 ) {
223
0
                for( int y = 0; y < p_pic->p[i].i_visible_pitch; y++ ) {
224
0
                    p_dst[2*y + 1] ^= 0x80;
225
0
                }
226
0
            }
227
7.32M
            p_src += p_sys->pitches[i];
228
7.32M
            p_dst += p_pic->p[i].i_pitch;
229
7.32M
        }
230
231
1.14k
        p_src += p_sys->pitches[i]
232
1.14k
               * (p_sys->lines[i] - p_pic->p[i].i_visible_lines);
233
1.14k
    }
234
573
}
235
236
/*****************************************************************************
237
 * DecodeFrame: decodes a video frame.
238
 *****************************************************************************/
239
static int DecodeFrame( decoder_t *p_dec, block_t *p_block )
240
6.95k
{
241
6.95k
    if( p_block == NULL ) /* No Drain */
242
4.51k
        return VLCDEC_SUCCESS;
243
244
2.43k
    p_block = DecodeBlock( p_dec, p_block );
245
2.43k
    if( p_block == NULL )
246
0
        return VLCDEC_SUCCESS;
247
248
    /* Get a new picture */
249
2.43k
    picture_t *p_pic = NULL;
250
2.43k
    if( !decoder_UpdateVideoFormat( p_dec ) )
251
2.43k
        p_pic = decoder_NewPicture( p_dec );
252
2.43k
    if( p_pic == NULL )
253
1.86k
    {
254
1.86k
        block_Release( p_block );
255
1.86k
        return VLCDEC_SUCCESS;
256
1.86k
    }
257
258
573
    struct vlc_ancillary *ancillary;
259
573
    ancillary = vlc_frame_GetAncillary( p_block, VLC_ANCILLARY_ID_ICC );
260
573
    if( ancillary )
261
0
        picture_AttachAncillary( p_pic, ancillary );
262
263
573
    FillPicture( p_dec, p_block, p_pic );
264
573
    p_pic->date = p_block->i_pts;
265
266
573
    if( p_block->i_flags & BLOCK_FLAG_INTERLACED_MASK )
267
0
    {
268
0
        p_pic->b_progressive = false;
269
0
        p_pic->i_nb_fields = (p_block->i_flags & BLOCK_FLAG_SINGLE_FIELD) ? 1 : 2;
270
0
        if( p_block->i_flags & BLOCK_FLAG_TOP_FIELD_FIRST )
271
0
            p_pic->b_top_field_first = true;
272
0
        else
273
0
            p_pic->b_top_field_first = false;
274
0
    }
275
573
    else
276
573
        p_pic->b_progressive = true;
277
278
573
    block_Release( p_block );
279
573
    decoder_QueueVideo( p_dec, p_pic );
280
573
    return VLCDEC_SUCCESS;
281
2.43k
}
282
283
static int OpenDecoder( vlc_object_t *p_this )
284
29.0k
{
285
29.0k
    decoder_t *p_dec = (decoder_t *)p_this;
286
287
29.0k
    int ret = OpenCommon( p_dec );
288
29.0k
    if( ret == VLC_SUCCESS )
289
781
    {
290
781
        p_dec->pf_decode = DecodeFrame;
291
781
        p_dec->pf_flush  = Flush;
292
781
    }
293
29.0k
    return ret;
294
29.0k
}
295
296
/*****************************************************************************
297
 * SendFrame: send a video frame to the stream output.
298
 *****************************************************************************/
299
static block_t *SendFrame( decoder_t *p_dec, block_t **pp_block )
300
6.95k
{
301
6.95k
    if( pp_block == NULL ) /* No Drain */
302
781
        return NULL;
303
304
6.17k
    block_t *p_block = *pp_block;
305
6.17k
    if( p_block == NULL )
306
2.43k
        return NULL;
307
3.73k
    *pp_block = NULL;
308
309
3.73k
    p_block = DecodeBlock( p_dec, p_block );
310
3.73k
    if( p_block != NULL )
311
2.43k
        p_block->i_dts = p_block->i_pts;
312
3.73k
    return p_block;
313
6.17k
}
314
315
static int OpenPacketizer( vlc_object_t *p_this )
316
40.7k
{
317
40.7k
    decoder_t *p_dec = (decoder_t *)p_this;
318
319
40.7k
    if ( p_dec->fmt_in->i_cat != VIDEO_ES )
320
0
        return VLC_ENOTSUP;
321
322
40.7k
    int ret = OpenCommon( p_dec );
323
40.7k
    if( ret == VLC_SUCCESS )
324
781
    {
325
781
        p_dec->pf_packetize = SendFrame;
326
781
        p_dec->pf_flush = Flush;
327
781
    }
328
40.7k
    return ret;
329
40.7k
}