Coverage Report

Created: 2026-09-03 07:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/vlc/modules/codec/png.c
Line
Count
Source
1
/*****************************************************************************
2
 * png.c: png decoder module making use of libpng.
3
 *****************************************************************************
4
 * Copyright (C) 1999-2001 VLC authors and VideoLAN
5
 *
6
 * Authors: Gildas Bazin <gbazin@videolan.org>
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_rand.h>
34
#include <vlc_ancillary.h>
35
#include <png.h>
36
#include <stdckdint.h>
37
38
/* PNG_SYS_COMMON_MEMBERS:
39
 * members common to encoder and decoder descriptors
40
 */
41
#define PNG_SYS_COMMON_MEMBERS                              \
42
/**@{*/                                                     \
43
    bool b_error;                                           \
44
    vlc_object_t *p_obj;                                    \
45
/**@}*/                                                     \
46
47
/*
48
 * png common descriptor
49
 */
50
struct png_sys_t
51
{
52
    PNG_SYS_COMMON_MEMBERS
53
};
54
55
typedef struct png_sys_t png_sys_t;
56
57
/*****************************************************************************
58
 * decoder_sys_t : png decoder descriptor
59
 *****************************************************************************/
60
typedef struct
61
{
62
    PNG_SYS_COMMON_MEMBERS
63
} decoder_sys_t;
64
65
/*****************************************************************************
66
 * Local prototypes
67
 *****************************************************************************/
68
static int  OpenDecoder   ( vlc_object_t * );
69
70
static int DecodeBlock  ( decoder_t *, block_t * );
71
72
/*
73
 * png encoder descriptor
74
 */
75
typedef struct
76
{
77
    PNG_SYS_COMMON_MEMBERS
78
    size_t i_blocksize;
79
} encoder_sys_t;
80
81
static int  OpenEncoder(vlc_object_t *);
82
83
static block_t *EncodeBlock(encoder_t *, picture_t *);
84
85
/*****************************************************************************
86
 * Module descriptor
87
 *****************************************************************************/
88
174
vlc_module_begin ()
89
87
    set_subcategory( SUBCAT_INPUT_VCODEC )
90
87
    set_description( N_("PNG video decoder") )
91
87
    set_capability( "video decoder", 1000 )
92
87
    set_callback( OpenDecoder )
93
87
    add_shortcut( "png" )
94
95
87
#ifdef ENABLE_SOUT
96
    /* video encoder submodule */
97
87
    add_submodule()
98
87
    add_shortcut("png")
99
87
    set_section(N_("Encoding"), NULL)
100
87
    set_description(N_("PNG video encoder"))
101
87
    set_capability("video encoder", 1000)
102
87
    set_callback(OpenEncoder)
103
87
#endif
104
105
    /* image encoder submodule */
106
87
    add_submodule()
107
87
    add_shortcut("png")
108
87
    set_section(N_("Encoding"), NULL)
109
87
    set_description(N_("PNG image encoder"))
110
87
    set_capability("image encoder", 1000)
111
87
    set_callback(OpenEncoder)
112
87
vlc_module_end ()
113
114
/*****************************************************************************
115
 * OpenDecoder: probe the decoder and return score
116
 *****************************************************************************/
117
static int OpenDecoder( vlc_object_t *p_this )
118
283k
{
119
283k
    decoder_t *p_dec = (decoder_t*)p_this;
120
121
283k
    if( p_dec->fmt_in->i_codec != VLC_CODEC_PNG &&
122
283k
        p_dec->fmt_in->i_codec != VLC_FOURCC('M','P','N','G') )
123
283k
    {
124
283k
        return VLC_EGENERIC;
125
283k
    }
126
127
    /* Allocate the memory needed to store the decoder's structure */
128
24
    decoder_sys_t *p_sys = vlc_obj_malloc( p_this, sizeof(decoder_sys_t) );
129
24
    if( p_sys == NULL )
130
0
        return VLC_ENOMEM;
131
24
    p_dec->p_sys = p_sys;
132
133
24
    p_sys->p_obj = p_this;
134
135
    /* Set output properties */
136
24
    p_dec->fmt_out.i_codec = VLC_CODEC_RGBA;
137
24
    p_dec->fmt_out.video.transfer  = TRANSFER_FUNC_SRGB;
138
24
    p_dec->fmt_out.video.space     = COLOR_SPACE_SRGB;
139
24
    p_dec->fmt_out.video.primaries = COLOR_PRIMARIES_SRGB;
140
24
    p_dec->fmt_out.video.color_range = COLOR_RANGE_FULL;
141
142
    /* Set callbacks */
143
24
    p_dec->pf_decode = DecodeBlock;
144
145
24
    return VLC_SUCCESS;
146
24
}
147
148
static void user_read( png_structp p_png, png_bytep data, png_size_t i_length )
149
5.45k
{
150
5.45k
    block_t *p_block = (block_t *)png_get_io_ptr( p_png );
151
5.45k
    if( i_length > p_block->i_buffer ) {
152
2
        png_error( p_png, "not enough data" );
153
0
        return;
154
2
    }
155
156
5.45k
    memcpy( data, p_block->p_buffer, i_length );
157
5.45k
    p_block->p_buffer += i_length;
158
5.45k
    p_block->i_buffer -= i_length;
159
5.45k
}
160
161
static void user_flush( png_structp p_png )
162
0
{
163
    /* noop */
164
0
    (void) p_png;
165
0
}
166
167
static void user_write( png_structp p_png, png_bytep data, png_size_t i_length )
168
0
{
169
0
    block_t *p_block = (block_t *)png_get_io_ptr( p_png );
170
0
    if( i_length > p_block->i_buffer ) {
171
0
        char err_str[128];
172
0
        snprintf( err_str, sizeof(err_str),
173
0
                  "block size %zu too small for %zu encoded bytes",
174
0
                  p_block->i_buffer, i_length );
175
0
        png_error( p_png, err_str );
176
0
        return;
177
0
    }
178
179
0
    memcpy( p_block->p_buffer, data, i_length );
180
0
    p_block->p_buffer += i_length;
181
0
    p_block->i_buffer -= i_length;
182
0
}
183
184
static void user_error( png_structp p_png, png_const_charp error_msg )
185
12
{
186
12
    png_sys_t *p_sys = (png_sys_t *)png_get_error_ptr( p_png );
187
12
    p_sys->b_error = true;
188
12
    msg_Err( p_sys->p_obj, "%s", error_msg );
189
12
}
190
191
static void user_warning( png_structp p_png, png_const_charp warning_msg )
192
1.55k
{
193
1.55k
    png_sys_t *p_sys = (png_sys_t *)png_get_error_ptr( p_png );
194
1.55k
    msg_Warn( p_sys->p_obj, "%s", warning_msg );
195
1.55k
}
196
197
#ifdef PNG_TEXT_SUPPORTED
198
static void process_text_chunk( decoder_t *p_dec, const png_textp chunk )
199
6
{
200
6
    if( chunk->compression != PNG_ITXT_COMPRESSION_NONE ||
201
0
        memcmp( chunk->key, "XML:com.adobe.xmp", 17 ) ||
202
0
        chunk->itxt_length < 20 )
203
6
        return;
204
205
0
    const char *exifxmp = (const char *) chunk->text;
206
0
    const char *orient = strnstr( exifxmp, ":Orientation>", chunk->itxt_length );
207
0
    if(orient && orient - exifxmp > 14)
208
0
        p_dec->fmt_out.video.orientation = ORIENT_FROM_EXIF( orient[13] - '0' );
209
0
}
210
211
static int make_xmp_packet( const video_format_t *fmt, png_textp chunk )
212
0
{
213
0
    unsigned char id[9];
214
0
    vlc_rand_bytes(id, 8);
215
0
    for(int i=0; i<8; i++)
216
0
        id[i] = (id[i] % 26) + 'A';
217
0
    id[8] = '\0';
218
0
    int len = asprintf( &chunk->text,
219
0
            "<?xpacket begin='' id='%s'?>"
220
0
             "<x:xmpmeta xmlns:x='adobe:ns:meta/' x:xmptk='VLC " VERSION "'>"
221
0
              "<rdf:RDF xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'>"
222
0
               "<rdf:Description rdf:about='' xmlns:tiff='http://ns.adobe.com/tiff/1.0/'>"
223
0
                "<tiff:Orientation>%" PRIu8 "</tiff:Orientation>"
224
0
               "</rdf:Description>"
225
0
              "</rdf:RDF>"
226
0
             "</x:xmpmeta>"
227
0
            "<?xpacket end='r'?>", id, (uint8_t)ORIENT_TO_EXIF(fmt->orientation) );
228
0
    if(len == 0)
229
0
    {
230
0
        free(chunk->text);
231
0
        chunk->text = NULL;
232
0
    }
233
0
    chunk->itxt_length = (len <= 0) ? 0 : len;
234
0
    chunk->compression = PNG_ITXT_COMPRESSION_NONE;
235
0
    chunk->key = len > 0 ? strdup( "XML:com.adobe.xmp" ) : NULL;
236
0
    chunk->lang_key = NULL;
237
0
    chunk->lang = NULL;
238
0
    chunk->text_length = 0;
239
0
    return len > 0 ? VLC_SUCCESS : VLC_EGENERIC;
240
0
}
241
242
#endif
243
244
/****************************************************************************
245
 * DecodeBlock: the whole thing
246
 ****************************************************************************
247
 * This function must be fed with a complete compressed frame.
248
 ****************************************************************************/
249
static int DecodeBlock( decoder_t *p_dec, block_t *p_block )
250
36
{
251
36
    decoder_sys_t *p_sys = p_dec->p_sys;
252
36
    picture_t *p_pic = 0;
253
254
36
    png_uint_32 i_width, i_height;
255
36
    int i_color_type, i_interlace_type, i_compression_type, i_filter_type;
256
36
    int i_bit_depth, i;
257
258
36
    png_structp p_png;
259
36
    png_infop p_info, p_end_info;
260
36
    png_bytep *volatile p_row_pointers = NULL;
261
262
36
    if( !p_block ) /* No Drain */
263
24
        return VLCDEC_SUCCESS;
264
265
12
    p_sys->b_error = false;
266
267
12
    if( p_block->i_flags & BLOCK_FLAG_CORRUPTED )
268
0
    {
269
0
        block_Release( p_block );
270
0
        return VLCDEC_SUCCESS;
271
0
    }
272
273
12
    p_png = png_create_read_struct( PNG_LIBPNG_VER_STRING, 0, 0, 0 );
274
12
    if( p_png == NULL )
275
0
    {
276
0
        block_Release( p_block );
277
0
        return VLCDEC_SUCCESS;
278
0
    }
279
280
12
    p_info = png_create_info_struct( p_png );
281
12
    if( p_info == NULL )
282
0
    {
283
0
        png_destroy_read_struct( &p_png, NULL, NULL );
284
0
        block_Release( p_block );
285
0
        return VLCDEC_SUCCESS;
286
0
    }
287
288
12
    p_end_info = png_create_info_struct( p_png );
289
12
    if( p_end_info == NULL )
290
0
    {
291
0
        png_destroy_read_struct( &p_png, &p_info, NULL );
292
0
        block_Release( p_block );
293
0
        return VLCDEC_SUCCESS;
294
0
    }
295
296
    /* libpng longjmp's there in case of error */
297
12
    if( setjmp( png_jmpbuf( p_png ) ) )
298
12
        goto error;
299
300
0
    png_set_read_fn( p_png, p_block, user_read );
301
0
    png_set_error_fn( p_png, p_sys, user_error, user_warning );
302
303
0
    png_read_info( p_png, p_info );
304
0
    if( p_sys->b_error ) goto error;
305
306
0
    png_get_IHDR( p_png, p_info, &i_width, &i_height,
307
0
                  &i_bit_depth, &i_color_type, &i_interlace_type,
308
0
                  &i_compression_type, &i_filter_type);
309
0
    if( p_sys->b_error ) goto error;
310
311
    /* Set output properties */
312
0
    p_dec->fmt_out.i_codec = VLC_CODEC_RGBA;
313
0
    p_dec->fmt_out.video.i_visible_width = p_dec->fmt_out.video.i_width = i_width;
314
0
    p_dec->fmt_out.video.i_visible_height = p_dec->fmt_out.video.i_height = i_height;
315
0
    p_dec->fmt_out.video.i_sar_num = 1;
316
0
    p_dec->fmt_out.video.i_sar_den = 1;
317
318
0
#ifdef PNG_TEXT_SUPPORTED
319
0
    png_textp textp;
320
0
    int numtextp;
321
0
    if( png_get_text( p_png, p_info, &textp, &numtextp ) > 0 )
322
8
        for( int ii=0; ii<numtextp; ii++ )
323
6
            process_text_chunk( p_dec, &textp[ii] );
324
0
#endif
325
326
0
    if( i_color_type == PNG_COLOR_TYPE_PALETTE )
327
0
        png_set_palette_to_rgb( p_png );
328
329
0
    if( i_color_type == PNG_COLOR_TYPE_GRAY ||
330
4
        i_color_type == PNG_COLOR_TYPE_GRAY_ALPHA )
331
0
          png_set_gray_to_rgb( p_png );
332
0
    if( i_color_type & PNG_COLOR_MASK_ALPHA )
333
0
        png_set_alpha_mode( p_png, PNG_ALPHA_OPTIMIZED, PNG_DEFAULT_sRGB );
334
335
    /* Strip to 8 bits per channel */
336
0
    if( i_bit_depth == 16 )
337
1
    {
338
1
#if PNG_LIBPNG_VER >= 10504
339
1
        png_set_scale_16( p_png );
340
#else
341
        png_set_strip_16( p_png );
342
#endif
343
1
    }
344
345
0
    if( png_get_valid( p_png, p_info, PNG_INFO_tRNS ) )
346
0
    {
347
0
        png_set_tRNS_to_alpha( p_png );
348
0
    }
349
0
    else if( !(i_color_type & PNG_COLOR_MASK_ALPHA) )
350
4
    {
351
4
        p_dec->fmt_out.i_codec = VLC_CODEC_RGB24;
352
4
    }
353
354
    /* Get a new picture */
355
0
    if( decoder_UpdateVideoFormat( p_dec ) )
356
0
        goto error;
357
0
    p_pic = decoder_NewPicture( p_dec );
358
0
    if( !p_pic ) goto error;
359
360
0
    p_pic->b_progressive = true;
361
    /* Decode ICC profile */
362
0
#ifdef PNG_iCCP_SUPPORTED
363
0
    if (png_get_valid( p_png, p_info, PNG_INFO_iCCP ))
364
2
    {
365
2
        vlc_icc_profile_t *icc;
366
2
        png_charp name;
367
2
        int compression;
368
# if PNG_LIBPNG_VER < 10500
369
        png_charp iccdata;
370
# else
371
2
        png_bytep iccdata;
372
2
# endif
373
2
        png_uint_32 icclen;
374
2
        png_get_iCCP( p_png, p_info, &name, &compression, &iccdata, &icclen);
375
2
        if( compression != PNG_COMPRESSION_TYPE_BASE )
376
0
            goto error; /* impossible with current libpng */
377
2
        icc = picture_AttachNewAncillary( p_pic, VLC_ANCILLARY_ID_ICC, sizeof(*icc) + icclen );
378
2
        if ( !icc )
379
0
            goto error;
380
2
        memcpy( icc->data, iccdata, icclen );
381
2
        icc->size = icclen;
382
2
    }
383
0
#endif
384
385
386
    /* Decode picture */
387
0
    p_row_pointers = vlc_alloc( i_height, sizeof(png_bytep) );
388
0
    if( !p_row_pointers )
389
0
        goto error;
390
1.63k
    for( i = 0; i < (int)i_height; i++ )
391
1.63k
        p_row_pointers[i] = p_pic->p->p_pixels + p_pic->p->i_pitch * i;
392
393
0
    png_read_image( p_png, p_row_pointers );
394
0
    if( p_sys->b_error ) goto error;
395
0
    png_read_end( p_png, p_end_info );
396
0
    if( p_sys->b_error ) goto error;
397
398
0
    png_destroy_read_struct( &p_png, &p_info, &p_end_info );
399
0
    free( p_row_pointers );
400
401
0
    p_pic->date = p_block->i_pts != VLC_TICK_INVALID ? p_block->i_pts : p_block->i_dts;
402
403
0
    block_Release( p_block );
404
0
    decoder_QueueVideo( p_dec, p_pic );
405
0
    return VLCDEC_SUCCESS;
406
407
12
 error:
408
409
12
    if( p_pic )
410
0
        picture_Release( p_pic );
411
12
    free( p_row_pointers );
412
12
    png_destroy_read_struct( &p_png, &p_info, &p_end_info );
413
12
    block_Release( p_block );
414
12
    return VLCDEC_SUCCESS;
415
0
}
416
417
static int OpenEncoder(vlc_object_t *p_this)
418
0
{
419
0
    encoder_t *p_enc = (encoder_t *) p_this;
420
421
0
    if( p_enc->fmt_out.i_codec != VLC_CODEC_PNG )
422
0
        return VLC_EGENERIC;
423
424
    /* Allocate the memory needed to store the encoder's structure */
425
0
    encoder_sys_t *p_sys = vlc_obj_malloc(p_this, sizeof(encoder_sys_t));
426
0
    if( p_sys == NULL )
427
0
        return VLC_ENOMEM;
428
0
    p_enc->p_sys = p_sys;
429
430
0
    p_sys->p_obj = p_this;
431
432
0
    if( ckd_mul( &p_sys->i_blocksize, 3, p_enc->fmt_in.video.i_visible_width ) ||
433
0
        ckd_mul( &p_sys->i_blocksize, p_sys->i_blocksize, p_enc->fmt_in.video.i_visible_height ) )
434
0
        return VLC_EGENERIC;
435
436
0
    p_enc->fmt_in.i_codec =
437
0
    p_enc->fmt_in.video.i_chroma = VLC_CODEC_RGB24;
438
439
0
    static const struct vlc_encoder_operations ops =
440
0
        { .encode_video = EncodeBlock };
441
442
0
    p_enc->ops = &ops;
443
444
0
    return VLC_SUCCESS;
445
0
}
446
447
/*
448
 * EncodeBlock
449
 */
450
static block_t *EncodeBlock(encoder_t *p_enc, picture_t *p_pic)
451
0
{
452
0
    encoder_sys_t *p_sys = p_enc->p_sys;
453
454
0
    if( unlikely( !p_pic ) )
455
0
    {
456
0
        return NULL;
457
0
    }
458
459
0
    block_t *p_block = block_Alloc( p_sys->i_blocksize );
460
0
    if( p_block == NULL )
461
0
    {
462
0
        return NULL;
463
0
    }
464
465
0
    png_structp p_png = png_create_write_struct( PNG_LIBPNG_VER_STRING, 0, 0, 0 );
466
0
    if( p_png == NULL )
467
0
    {
468
0
        block_Release( p_block );
469
0
        return NULL;
470
0
    }
471
472
    /* Disable filtering to speed-up encoding */
473
0
    png_set_filter( p_png, 0, PNG_NO_FILTERS );
474
    /* 1 == best speed */
475
0
    png_set_compression_level( p_png, 1 );
476
477
    /* save buffer start */
478
0
    uint8_t *p_start = p_block->p_buffer;
479
0
    size_t i_start = p_block->i_buffer;
480
481
0
    p_sys->b_error = false;
482
0
    png_infop p_info = NULL;
483
484
    /* libpng longjmp's there in case of error */
485
0
    if( setjmp( png_jmpbuf( p_png ) ) )
486
0
        goto error;
487
488
0
    png_set_write_fn( p_png, p_block, user_write, user_flush );
489
0
    png_set_error_fn( p_png, p_sys, user_error, user_warning );
490
491
0
    p_info = png_create_info_struct( p_png );
492
0
    if( p_info == NULL )
493
0
        goto error;
494
495
0
    png_set_IHDR( p_png, p_info,
496
0
            p_enc->fmt_in.video.i_visible_width,
497
0
            p_enc->fmt_in.video.i_visible_height,
498
0
            8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE,
499
0
            PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT );
500
0
    if( p_sys->b_error ) goto error;
501
502
0
#ifdef PNG_TEXT_SUPPORTED
503
0
    png_text text;
504
0
    if( make_xmp_packet( &p_pic->format, &text ) == VLC_SUCCESS )
505
0
    {
506
0
        png_set_text( p_png, p_info, &text, 1 );
507
0
        png_write_info( p_png, p_info );
508
0
        free( text.key );
509
0
        free( text.text );
510
0
    }
511
0
    else
512
0
#endif
513
0
        png_write_info( p_png, p_info );
514
515
0
    if( p_sys->b_error ) goto error;
516
517
    /* Encode picture */
518
519
0
    for( int i = 0; i < p_pic->p->i_visible_lines; i++ )
520
0
    {
521
0
        png_write_row( p_png, p_pic->p->p_pixels + (i * p_pic->p->i_pitch) );
522
0
        if( p_sys->b_error ) goto error;
523
0
    }
524
525
0
    png_write_end( p_png, p_info );
526
0
    if( p_sys->b_error ) goto error;
527
528
0
    png_destroy_write_struct( &p_png, &p_info );
529
530
    /* restore original buffer position */
531
0
    p_block->p_buffer = p_start;
532
0
    p_block->i_buffer = i_start - p_block->i_buffer;
533
534
0
    p_block->i_dts = p_block->i_pts = p_pic->date;
535
536
0
    return p_block;
537
538
0
 error:
539
540
0
    png_destroy_write_struct( &p_png, p_info ? &p_info : NULL );
541
542
0
    block_Release(p_block);
543
    return NULL;
544
0
}