Coverage Report

Created: 2026-06-09 09:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/vlc/modules/codec/oggspots.c
Line
Count
Source
1
/*****************************************************************************
2
 * oggspots.c: OggSpots decoder module.
3
 *****************************************************************************
4
 * Copyright (C) 2016 VLC authors and VideoLAN
5
 *
6
 * Authors: Michael Taenzer <neo@nhng.de>
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_image.h>
34
35
#include <assert.h>
36
#include <limits.h>
37
38
/*****************************************************************************
39
 * decoder_sys_t : oggspots decoder descriptor
40
 *****************************************************************************/
41
typedef struct
42
{
43
    /* Module mode */
44
    bool b_packetizer;
45
46
    /*
47
     * Input properties
48
     */
49
    bool b_has_headers;
50
51
    /*
52
     * Image handler
53
     */
54
    image_handler_t* p_image;
55
56
    /*
57
     * Common properties
58
     */
59
    vlc_tick_t i_pts;
60
} decoder_sys_t;
61
62
/*****************************************************************************
63
 * Local prototypes
64
 *****************************************************************************/
65
static int  OpenDecoder   (vlc_object_t*);
66
static int  OpenPacketizer(vlc_object_t*);
67
static void CloseDecoder  (vlc_object_t*);
68
69
static int        DecodeVideo  (decoder_t*, block_t*);
70
static block_t*   Packetize  (decoder_t*, block_t**);
71
static int        ProcessHeader(decoder_t*);
72
static void*      ProcessPacket(decoder_t*, block_t*);
73
static void       Flush        (decoder_t*);
74
static picture_t* DecodePacket (decoder_t*, block_t*);
75
76
77
/*****************************************************************************
78
 * Module descriptor
79
 *****************************************************************************/
80
81
168
vlc_module_begin ()
82
84
    set_subcategory(SUBCAT_INPUT_VCODEC)
83
84
    set_shortname("OggSpots")
84
84
    set_description(N_("OggSpots video decoder"))
85
84
    set_capability("video decoder", 10)
86
168
    set_callbacks(OpenDecoder, CloseDecoder)
87
84
    add_shortcut("oggspots")
88
89
84
    add_submodule ()
90
84
    set_description(N_("OggSpots video packetizer"))
91
84
    set_capability("video packetizer", 10)
92
168
    set_callbacks(OpenPacketizer, CloseDecoder)
93
84
    add_shortcut("oggspots")
94
84
vlc_module_end ()
95
96
static int OpenCommon(vlc_object_t* p_this, bool b_packetizer)
97
14.6k
{
98
14.6k
    decoder_t* p_dec = (decoder_t*)p_this;
99
14.6k
    decoder_sys_t* p_sys;
100
101
14.6k
    if (p_dec->fmt_in->i_codec != VLC_CODEC_OGGSPOTS) {
102
14.6k
        return VLC_EGENERIC;
103
14.6k
    }
104
105
    /* Allocate the memory needed to store the decoder's structure */
106
0
    p_sys = malloc(sizeof(*p_sys));
107
0
    if (p_sys == NULL) {
108
0
        return VLC_ENOMEM;
109
0
    }
110
0
    p_dec->p_sys = p_sys;
111
0
    p_sys->b_packetizer = b_packetizer;
112
0
    p_sys->b_has_headers = false;
113
0
    p_sys->i_pts = VLC_TICK_INVALID;
114
115
    /* Initialize image handler */
116
0
    p_sys->p_image = image_HandlerCreate(p_dec);
117
0
    if (p_sys->p_image == NULL) {
118
0
        free(p_sys);
119
0
        return VLC_ENOMEM;
120
0
    }
121
122
0
    if( b_packetizer )
123
0
    {
124
0
        p_dec->fmt_out.i_codec = VLC_CODEC_OGGSPOTS;
125
0
        p_dec->pf_packetize = Packetize;
126
0
    }
127
0
    else
128
0
    {
129
0
        p_dec->fmt_out.i_codec = VLC_CODEC_RGBA;
130
0
        p_dec->pf_decode = DecodeVideo;
131
0
    }
132
133
0
    p_dec->pf_flush = Flush;
134
135
0
    return VLC_SUCCESS;
136
0
}
137
138
/*****************************************************************************
139
 * OpenDecoder: probe the decoder and return score
140
 *****************************************************************************/
141
static int OpenDecoder(vlc_object_t* p_this)
142
2.01k
{
143
2.01k
    return OpenCommon(p_this, false);
144
2.01k
}
145
146
static int OpenPacketizer(vlc_object_t* p_this)
147
12.6k
{
148
12.6k
    return OpenCommon(p_this, true);
149
12.6k
}
150
151
/****************************************************************************
152
 * DecodeBlock: the whole thing
153
 ****************************************************************************
154
 * This function must be fed with ogg packets.
155
 ****************************************************************************/
156
static void* DecodeBlock(decoder_t* p_dec, block_t* p_block)
157
0
{
158
0
    decoder_sys_t* p_sys = p_dec->p_sys;
159
160
    /* Check for headers */
161
0
    if (!p_sys->b_has_headers) {
162
0
        if (ProcessHeader(p_dec)) {
163
0
            block_Release(p_block);
164
0
            return NULL;
165
0
        }
166
0
        p_sys->b_has_headers = true;
167
0
    }
168
169
0
    return ProcessPacket(p_dec, p_block);
170
0
}
171
172
static int DecodeVideo( decoder_t *p_dec, block_t *p_block )
173
0
{
174
0
    if( p_block == NULL ) /* No Drain */
175
0
        return VLCDEC_SUCCESS;
176
177
0
    picture_t *p_pic = DecodeBlock( p_dec, p_block );
178
0
    if( p_pic != NULL )
179
0
        decoder_QueueVideo( p_dec, p_pic );
180
0
    return VLCDEC_SUCCESS;
181
0
}
182
183
static block_t *Packetize( decoder_t *p_dec, block_t **pp_block )
184
0
{
185
0
    if( pp_block == NULL ) /* No Drain */
186
0
        return NULL;
187
0
    block_t *p_block = *pp_block; *pp_block = NULL;
188
0
    if( p_block == NULL )
189
0
        return NULL;
190
0
    return DecodeBlock( p_dec, p_block );
191
0
}
192
193
/*****************************************************************************
194
 * ProcessHeader: process OggSpots header.
195
 *****************************************************************************/
196
static int ProcessHeader(decoder_t* p_dec)
197
0
{
198
0
    decoder_sys_t* p_sys = p_dec->p_sys;
199
0
    const uint8_t* p_extra;
200
0
    int i_major;
201
0
    int i_minor;
202
0
    uint64_t i_granulerate_numerator;
203
0
    uint64_t i_granulerate_denominator;
204
205
    /* The OggSpots header is always 52 bytes */
206
0
    if (p_dec->fmt_in->i_extra != 52) {
207
0
        return VLC_EGENERIC;
208
0
    }
209
0
    p_extra = p_dec->fmt_in->p_extra;
210
211
    /* Identification string */
212
0
    if ( memcmp(p_extra, "SPOTS\0\0", 8) ) {
213
0
        return VLC_EGENERIC;
214
0
    }
215
216
    /* Version number */
217
0
    i_major = GetWLE(&p_extra[ 8]); /* major version num */
218
0
    i_minor = GetWLE(&p_extra[10]); /* minor version num */
219
0
    if (i_major != 0 || i_minor != 1) {
220
0
        return VLC_EGENERIC;
221
0
    }
222
223
    /* Granule rate */
224
0
    i_granulerate_numerator   = GetQWLE(&p_extra[12]);
225
0
    i_granulerate_denominator = GetQWLE(&p_extra[20]);
226
0
    if (i_granulerate_numerator == 0 || i_granulerate_denominator == 0) {
227
0
        return VLC_EGENERIC;
228
0
    }
229
230
    /* The OggSpots spec contained an error and there are implementations out
231
     * there that used the wrong value. So we detect that case and switch
232
     * numerator and denominator in that case */
233
0
    if (i_granulerate_numerator == 1 && i_granulerate_denominator == 30) {
234
0
        i_granulerate_numerator   = 30;
235
0
        i_granulerate_denominator = 1;
236
0
    }
237
238
    /* Normalize granulerate */
239
0
    vlc_ureduce(&p_dec->fmt_out.video.i_frame_rate,
240
0
                &p_dec->fmt_out.video.i_frame_rate_base,
241
0
                i_granulerate_numerator, i_granulerate_denominator, 0);
242
243
    /* Image format */
244
0
    if (!p_sys->b_packetizer) {
245
0
        if ( memcmp(&p_extra[32], "PNG", 3) && memcmp(&p_extra[32], "JPEG", 4) ) {
246
0
            char psz_image_type[8+1];
247
0
            strncpy(psz_image_type, (char*)&p_extra[32], 8);
248
0
            psz_image_type[sizeof(psz_image_type)-1] = '\0';
249
250
0
            msg_Warn(p_dec, "Unsupported image format: %s", psz_image_type);
251
0
        }
252
0
    }
253
254
    /* Dimensions */
255
0
    p_dec->fmt_out.video.i_width  = p_dec->fmt_out.video.i_visible_width  =
256
0
            GetWLE(&p_extra[40]);
257
0
    p_dec->fmt_out.video.i_height = p_dec->fmt_out.video.i_visible_height =
258
0
            GetWLE(&p_extra[42]);
259
260
    /* We assume square pixels */
261
0
    p_dec->fmt_out.video.i_sar_num = 1;
262
0
    p_dec->fmt_out.video.i_sar_den = 1;
263
264
    /* We don't implement background color, alignment and options at the
265
     * moment because the former doesn't seem necessary right now and the
266
     * latter are underspecified. */
267
268
0
    if (p_sys->b_packetizer) {
269
0
        void* p_new_extra = realloc(p_dec->fmt_out.p_extra,
270
0
                                p_dec->fmt_in->i_extra);
271
0
        if (unlikely(p_new_extra == NULL)) {
272
0
            return VLC_ENOMEM;
273
0
        }
274
0
        p_dec->fmt_out.p_extra = p_new_extra;
275
0
        p_dec->fmt_out.i_extra = p_dec->fmt_in->i_extra;
276
0
        memcpy(p_dec->fmt_out.p_extra,
277
0
               p_dec->fmt_in->p_extra, p_dec->fmt_out.i_extra);
278
0
    }
279
280
0
    return VLC_SUCCESS;
281
0
}
282
283
/*****************************************************************************
284
 * Flush:
285
 *****************************************************************************/
286
static void Flush(decoder_t* p_dec)
287
0
{
288
0
    decoder_sys_t* p_sys = p_dec->p_sys;
289
290
0
    p_sys->i_pts = VLC_TICK_INVALID;
291
0
}
292
293
/*****************************************************************************
294
 * ProcessPacket: processes an OggSpots packet.
295
 *****************************************************************************/
296
static void* ProcessPacket(decoder_t* p_dec, block_t* p_block)
297
0
{
298
0
    decoder_sys_t* p_sys = p_dec->p_sys;
299
0
    void* p_buf;
300
301
0
    if ( (p_block->i_flags & BLOCK_FLAG_DISCONTINUITY) != 0 ) {
302
0
        p_sys->i_pts = p_block->i_pts;
303
0
    }
304
305
0
    if ( (p_block->i_flags & BLOCK_FLAG_CORRUPTED) != 0 ) {
306
0
        block_Release(p_block);
307
0
        return NULL;
308
0
    }
309
310
    /* Date management */
311
0
    if (p_block->i_pts != VLC_TICK_INVALID && p_block->i_pts != p_sys->i_pts) {
312
0
        p_sys->i_pts = p_block->i_pts;
313
0
    }
314
315
0
    if (p_sys->b_packetizer) {
316
        /* Date management */
317
        /* FIXME: This is copied from theora but it looks wrong.
318
         * p_block->i_length will always be zero. */
319
0
        p_block->i_dts = p_block->i_pts = p_sys->i_pts;
320
321
0
        p_block->i_length = p_sys->i_pts - p_block->i_pts;
322
323
0
        p_buf = p_block;
324
0
    }
325
0
    else {
326
0
        p_buf = DecodePacket(p_dec, p_block);
327
0
    }
328
329
0
    return p_buf;
330
0
}
331
332
/*****************************************************************************
333
 * DecodePacket: decodes an OggSpots packet.
334
 *****************************************************************************/
335
static picture_t* DecodePacket(decoder_t* p_dec, block_t* p_block)
336
0
{
337
0
    decoder_sys_t* p_sys = p_dec->p_sys;
338
0
    uint32_t i_img_offset;
339
0
    picture_t* p_pic;
340
341
0
    if (p_block->i_buffer < 20) {
342
0
        msg_Dbg(p_dec, "Packet too short");
343
0
        goto error;
344
0
    }
345
346
    /* Byte offset */
347
0
    i_img_offset = GetDWLE(p_block->p_buffer);
348
0
    if (i_img_offset < 20) {
349
0
        msg_Dbg(p_dec, "Invalid byte offset");
350
0
        goto error;
351
0
    }
352
353
0
    if (i_img_offset > p_block->i_buffer) {
354
0
        msg_Dbg(p_dec, "Invalid byte offset: %u exceeds packet size %zu",
355
0
                i_img_offset, p_block->i_buffer);
356
0
        goto error;
357
0
    }
358
359
    /* Image format */
360
0
    es_format_t fmt_in = *p_dec->fmt_in;
361
0
    if ( !memcmp(&p_block->p_buffer[4], "PNG", 3) ) {
362
0
        fmt_in.video.i_chroma = VLC_CODEC_PNG;
363
0
    }
364
0
    else if ( !memcmp(&p_block->p_buffer[4], "JPEG", 4) ) {
365
0
        fmt_in.video.i_chroma = VLC_CODEC_JPEG;
366
0
    }
367
0
    else {
368
0
        char psz_image_type[8+1];
369
0
        strncpy(psz_image_type, (char*)&p_block->p_buffer[4], 8);
370
0
        psz_image_type[sizeof(psz_image_type)-1] = '\0';
371
372
0
        msg_Dbg(p_dec, "Unsupported image format: %s", psz_image_type);
373
0
        goto error;
374
0
    }
375
376
    /* We currently ignore the rest of the header and let the image format
377
     * handle the details */
378
379
0
    p_block->i_buffer -= i_img_offset;
380
0
    p_block->p_buffer += i_img_offset;
381
382
0
    p_pic = image_Read(p_sys->p_image, p_block,
383
0
                       &fmt_in,
384
0
                       &p_dec->fmt_out.video);
385
0
    if (p_pic == NULL) {
386
0
        return NULL;
387
0
    }
388
389
0
    p_pic->b_force = true;
390
0
    p_dec->fmt_out.i_codec = p_dec->fmt_out.video.i_chroma;
391
0
    decoder_UpdateVideoFormat(p_dec);
392
393
0
    return p_pic;
394
395
0
error:
396
0
    block_Release(p_block);
397
0
    return NULL;
398
0
}
399
400
/*****************************************************************************
401
 * CloseDecoder: OggSpots decoder destruction
402
 *****************************************************************************/
403
static void CloseDecoder(vlc_object_t* p_this)
404
0
{
405
0
    decoder_t* p_dec = (decoder_t*)p_this;
406
0
    decoder_sys_t* p_sys = p_dec->p_sys;
407
408
0
    image_HandlerDelete(p_sys->p_image);
409
0
    free(p_sys);
410
0
}