Coverage Report

Created: 2026-08-13 07:43

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