png.c 13.3 KB
Newer Older
1 2 3
/*****************************************************************************
 * png.c: png decoder module making use of libpng.
 *****************************************************************************
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
4
 * Copyright (C) 1999-2001 VLC authors and VideoLAN
5 6 7 8
 * $Id$
 *
 * Authors: Gildas Bazin <gbazin@videolan.org>
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
9 10 11
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
12 13 14 15
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
16 17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
18
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
19 20 21
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 23 24 25 26
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
27 28 29 30
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

31
#include <vlc_common.h>
32
#include <vlc_plugin.h>
zorglub's avatar
zorglub committed
33
#include <vlc_codec.h>
34 35
#include <png.h>

Tristan Matthews's avatar
Tristan Matthews committed
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
/* PNG_SYS_COMMON_MEMBERS:
 * members common to encoder and decoder descriptors
 */
#define PNG_SYS_COMMON_MEMBERS                              \
/**@{*/                                                     \
    bool b_error;                                           \
    vlc_object_t *p_obj;                                    \
/**@}*/                                                     \

/*
 * png common descriptor
 */
struct png_sys_t
{
    PNG_SYS_COMMON_MEMBERS
};

typedef struct png_sys_t png_sys_t;

55 56 57 58 59
/*****************************************************************************
 * decoder_sys_t : png decoder descriptor
 *****************************************************************************/
struct decoder_sys_t
{
Tristan Matthews's avatar
Tristan Matthews committed
60
    PNG_SYS_COMMON_MEMBERS
61 62 63 64 65 66 67 68 69 70
};

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
static int  OpenDecoder   ( vlc_object_t * );
static void CloseDecoder  ( vlc_object_t * );

static picture_t *DecodeBlock  ( decoder_t *, block_t ** );

Tristan Matthews's avatar
Tristan Matthews committed
71 72 73 74 75 76 77 78 79 80 81 82 83 84
/*
 * png encoder descriptor
 */
struct encoder_sys_t
{
    PNG_SYS_COMMON_MEMBERS
    int i_blocksize;
};

static int  OpenEncoder(vlc_object_t *);
static void CloseEncoder(vlc_object_t *);

static block_t *EncodeBlock(encoder_t *, picture_t *);

85 86 87
/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
88 89 90 91 92 93 94
vlc_module_begin ()
    set_category( CAT_INPUT )
    set_subcategory( SUBCAT_INPUT_VCODEC )
    set_description( N_("PNG video decoder") )
    set_capability( "decoder", 1000 )
    set_callbacks( OpenDecoder, CloseDecoder )
    add_shortcut( "png" )
Tristan Matthews's avatar
Tristan Matthews committed
95 96 97 98 99 100 101 102

    /* encoder submodule */
    add_submodule()
    add_shortcut("png")
    set_section(N_("Encoding"), NULL)
    set_description(N_("PNG video encoder"))
    set_capability("encoder", 1000)
    set_callbacks(OpenEncoder, CloseEncoder)
103
vlc_module_end ()
104 105 106 107 108 109 110 111

/*****************************************************************************
 * OpenDecoder: probe the decoder and return score
 *****************************************************************************/
static int OpenDecoder( vlc_object_t *p_this )
{
    decoder_t *p_dec = (decoder_t*)p_this;

112
    if( p_dec->fmt_in.i_codec != VLC_CODEC_PNG &&
hartman's avatar
hartman committed
113
        p_dec->fmt_in.i_codec != VLC_FOURCC('M','P','N','G') )
114 115 116 117 118
    {
        return VLC_EGENERIC;
    }

    /* Allocate the memory needed to store the decoder's structure */
119 120
    p_dec->p_sys = malloc( sizeof(decoder_sys_t) );
    if( p_dec->p_sys == NULL )
121
        return VLC_ENOMEM;
122

Tristan Matthews's avatar
Tristan Matthews committed
123 124
    p_dec->p_sys->p_obj = p_this;

125 126
    /* Set output properties */
    p_dec->fmt_out.i_cat = VIDEO_ES;
127
    p_dec->fmt_out.i_codec = VLC_CODEC_RGBA;
128 129 130 131 132 133 134 135 136 137

    /* Set callbacks */
    p_dec->pf_decode_video = DecodeBlock;

    return VLC_SUCCESS;
}

static void user_read( png_structp p_png, png_bytep data, png_size_t i_length )
{
    block_t *p_block = (block_t *)png_get_io_ptr( p_png );
Edward Wang's avatar
Edward Wang committed
138 139 140 141 142
    if( i_length > p_block->i_buffer ) {
        png_error( p_png, "not enough data" );
        return;
    }

143 144 145 146 147
    memcpy( data, p_block->p_buffer, i_length );
    p_block->p_buffer += i_length;
    p_block->i_buffer -= i_length;
}

Tristan Matthews's avatar
Tristan Matthews committed
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
static void user_flush( png_structp p_png )
{
    /* noop */
    (void) p_png;
}

static void user_write( png_structp p_png, png_bytep data, png_size_t i_length )
{
    block_t *p_block = (block_t *)png_get_io_ptr( p_png );
    if( i_length > p_block->i_buffer ) {
        char err_str[64];
        snprintf( err_str, sizeof(err_str),
                  "block size %zu too small for %zu encoded bytes",
                  p_block->i_buffer, i_length );
        png_error( p_png, err_str );
        return;
    }

    memcpy( p_block->p_buffer, data, i_length );
    p_block->p_buffer += i_length;
    p_block->i_buffer -= i_length;
}

171 172
static void user_error( png_structp p_png, png_const_charp error_msg )
{
Tristan Matthews's avatar
Tristan Matthews committed
173 174 175
    png_sys_t *p_sys = (png_sys_t *)png_get_error_ptr( p_png );
    p_sys->b_error = true;
    msg_Err( p_sys->p_obj, "%s", error_msg );
176 177 178 179
}

static void user_warning( png_structp p_png, png_const_charp warning_msg )
{
Tristan Matthews's avatar
Tristan Matthews committed
180 181
    png_sys_t *p_sys = (png_sys_t *)png_get_error_ptr( p_png );
    msg_Warn( p_sys->p_obj, "%s", warning_msg );
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
}

/****************************************************************************
 * DecodeBlock: the whole thing
 ****************************************************************************
 * This function must be fed with a complete compressed frame.
 ****************************************************************************/
static picture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
{
    decoder_sys_t *p_sys = p_dec->p_sys;
    block_t *p_block;
    picture_t *p_pic = 0;

    png_uint_32 i_width, i_height;
    int i_color_type, i_interlace_type, i_compression_type, i_filter_type;
    int i_bit_depth, i;

    png_structp p_png;
    png_infop p_info, p_end_info;
    png_bytep *p_row_pointers = NULL;

    if( !pp_block || !*pp_block ) return NULL;

    p_block = *pp_block;
206
    p_sys->b_error = false;
207

208
    if( p_block->i_flags & BLOCK_FLAG_DISCONTINUITY )
209 210 211 212 213
    {
        block_Release( p_block ); *pp_block = NULL;
        return NULL;
    }

214
    p_png = png_create_read_struct( PNG_LIBPNG_VER_STRING, 0, 0, 0 );
215 216 217 218 219
    if( p_png == NULL )
    {
        block_Release( p_block ); *pp_block = NULL;
        return NULL;
    }
ivoire's avatar
ivoire committed
220

221
    p_info = png_create_info_struct( p_png );
222 223
    if( p_info == NULL )
    {
224
        png_destroy_read_struct( &p_png, NULL, NULL );
225 226 227 228
        block_Release( p_block ); *pp_block = NULL;
        return NULL;
    }

229
    p_end_info = png_create_info_struct( p_png );
230 231
    if( p_end_info == NULL )
    {
232
        png_destroy_read_struct( &p_png, &p_info, NULL );
233 234 235
        block_Release( p_block ); *pp_block = NULL;
        return NULL;
    }
ivoire's avatar
ivoire committed
236

237 238 239 240
    /* libpng longjmp's there in case of error */
    if( setjmp( png_jmpbuf( p_png ) ) )
        goto error;

241 242 243 244 245 246 247 248 249 250 251 252
    png_set_read_fn( p_png, (void *)p_block, user_read );
    png_set_error_fn( p_png, (void *)p_dec, user_error, user_warning );

    png_read_info( p_png, p_info );
    if( p_sys->b_error ) goto error;

    png_get_IHDR( p_png, p_info, &i_width, &i_height,
                  &i_bit_depth, &i_color_type, &i_interlace_type,
                  &i_compression_type, &i_filter_type);
    if( p_sys->b_error ) goto error;

    /* Set output properties */
253
    p_dec->fmt_out.i_codec = VLC_CODEC_RGBA;
254 255
    p_dec->fmt_out.video.i_visible_width = p_dec->fmt_out.video.i_width = i_width;
    p_dec->fmt_out.video.i_visible_height = p_dec->fmt_out.video.i_height = i_height;
Laurent Aimar's avatar
Laurent Aimar committed
256 257
    p_dec->fmt_out.video.i_sar_num = 1;
    p_dec->fmt_out.video.i_sar_den = 1;
258 259 260
    p_dec->fmt_out.video.i_rmask = 0x000000ff;
    p_dec->fmt_out.video.i_gmask = 0x0000ff00;
    p_dec->fmt_out.video.i_bmask = 0x00ff0000;
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277

    if( i_color_type == PNG_COLOR_TYPE_PALETTE )
        png_set_palette_to_rgb( p_png );

    if( i_color_type == PNG_COLOR_TYPE_GRAY ||
        i_color_type == PNG_COLOR_TYPE_GRAY_ALPHA )
          png_set_gray_to_rgb( p_png );

    /* Strip to 8 bits per channel */
    if( i_bit_depth == 16 ) png_set_strip_16( p_png );

    if( png_get_valid( p_png, p_info, PNG_INFO_tRNS ) )
    {
        png_set_tRNS_to_alpha( p_png );
    }
    else if( !(i_color_type & PNG_COLOR_MASK_ALPHA) )
    {
278
        p_dec->fmt_out.i_codec = VLC_CODEC_RGB24;
279 280 281
    }

    /* Get a new picture */
282
    p_pic = decoder_NewPicture( p_dec );
283 284 285 286
    if( !p_pic ) goto error;

    /* Decode picture */
    p_row_pointers = malloc( sizeof(png_bytep) * i_height );
287 288
    if( !p_row_pointers )
        goto error;
289 290 291 292 293 294 295 296 297 298 299
    for( i = 0; i < (int)i_height; i++ )
        p_row_pointers[i] = p_pic->p->p_pixels + p_pic->p->i_pitch * i;

    png_read_image( p_png, p_row_pointers );
    if( p_sys->b_error ) goto error;
    png_read_end( p_png, p_end_info );
    if( p_sys->b_error ) goto error;

    png_destroy_read_struct( &p_png, &p_info, &p_end_info );
    free( p_row_pointers );

300
    p_pic->date = p_block->i_pts > VLC_TS_INVALID ? p_block->i_pts : p_block->i_dts;
hartman's avatar
hartman committed
301

302 303 304 305 306
    block_Release( p_block ); *pp_block = NULL;
    return p_pic;

 error:

307
    free( p_row_pointers );
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
    png_destroy_read_struct( &p_png, &p_info, &p_end_info );
    block_Release( p_block ); *pp_block = NULL;
    return NULL;
}

/*****************************************************************************
 * CloseDecoder: png decoder destruction
 *****************************************************************************/
static void CloseDecoder( vlc_object_t *p_this )
{
    decoder_t *p_dec = (decoder_t *)p_this;
    decoder_sys_t *p_sys = p_dec->p_sys;

    free( p_sys );
}
Tristan Matthews's avatar
Tristan Matthews committed
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374

static int OpenEncoder(vlc_object_t *p_this)
{
    encoder_t *p_enc = (encoder_t *) p_this;

    if( p_enc->fmt_out.i_codec != VLC_CODEC_PNG )
        return VLC_EGENERIC;

    /* Allocate the memory needed to store the encoder's structure */
    p_enc->p_sys = malloc( sizeof(encoder_sys_t) );
    if( p_enc->p_sys  == NULL )
        return VLC_ENOMEM;

    p_enc->p_sys->p_obj = p_this;

    p_enc->p_sys->i_blocksize = 3 * p_enc->fmt_in.video.i_visible_width *
        p_enc->fmt_in.video.i_visible_height;

    p_enc->fmt_in.i_codec = VLC_CODEC_RGB24;
    p_enc->fmt_in.video.i_rmask = 0x000000ff;
    p_enc->fmt_in.video.i_gmask = 0x0000ff00;
    p_enc->fmt_in.video.i_bmask = 0x00ff0000;
    p_enc->pf_encode_video = EncodeBlock;

    return VLC_SUCCESS;
}

/*
 * EncodeBlock
 */
static block_t *EncodeBlock(encoder_t *p_enc, picture_t *p_pic)
{
    encoder_sys_t *p_sys = p_enc->p_sys;

    if( unlikely( !p_pic ) )
    {
        return NULL;
    }

    block_t *p_block = block_Alloc( p_sys->i_blocksize );
    if( p_block == NULL )
    {
        return NULL;
    }

    png_structp p_png = png_create_write_struct( PNG_LIBPNG_VER_STRING, 0, 0, 0 );
    if( p_png == NULL )
    {
        block_Release( p_block );
        return NULL;
    }

Tristan Matthews's avatar
Tristan Matthews committed
375 376 377 378 379
    /* Disable filtering to speed-up encoding */
    png_set_filter( p_png, 0, PNG_NO_FILTERS );
    /* 1 == best speed */
    png_set_compression_level( p_png, 1 );

Tristan Matthews's avatar
Tristan Matthews committed
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
    /* save buffer start */
    uint8_t *p_start = p_block->p_buffer;
    size_t i_start = p_block->i_buffer;

    p_sys->b_error = false;
    png_infop p_info = NULL;

    /* libpng longjmp's there in case of error */
    if( setjmp( png_jmpbuf( p_png ) ) )
        goto error;

    png_set_write_fn( p_png, p_block, user_write, user_flush );
    png_set_error_fn( p_png, p_enc, user_error, user_warning );

    p_info = png_create_info_struct( p_png );
    if( p_info == NULL )
        goto error;

    png_infop p_end_info = png_create_info_struct( p_png );
    if( p_end_info == NULL ) goto error;

    png_set_IHDR( p_png, p_info,
402 403
            p_enc->fmt_in.video.i_visible_width,
            p_enc->fmt_in.video.i_visible_height,
Tristan Matthews's avatar
Tristan Matthews committed
404 405 406 407 408 409 410 411 412
            8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE,
            PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT );
    if( p_sys->b_error ) goto error;

    png_write_info( p_png, p_info );
    if( p_sys->b_error ) goto error;

    /* Encode picture */

413
    for( int i = 0; i < p_pic->p->i_visible_lines; i++ )
Tristan Matthews's avatar
Tristan Matthews committed
414
    {
415
        png_write_row( p_png, p_pic->p->p_pixels + (i * p_pic->p->i_pitch) );
Tristan Matthews's avatar
Tristan Matthews committed
416 417 418 419 420 421 422 423 424 425
        if( p_sys->b_error ) goto error;
    }

    png_write_end( p_png, p_end_info );
    if( p_sys->b_error ) goto error;

    png_destroy_write_struct( &p_png, &p_info );

    /* restore original buffer position */
    p_block->p_buffer = p_start;
Rafaël Carré's avatar
Rafaël Carré committed
426
    p_block->i_buffer = i_start - p_block->i_buffer;
427 428 429

    p_block->i_dts = p_block->i_pts = p_pic->date;

Tristan Matthews's avatar
Tristan Matthews committed
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
    return p_block;

 error:

    png_destroy_write_struct( &p_png, p_info ? &p_info : NULL );

    block_Release(p_block);
    return NULL;
}

/*****************************************************************************
 * CloseEncoder: png encoder destruction
 *****************************************************************************/
static void CloseEncoder( vlc_object_t *p_this )
{
    encoder_t *p_enc = (encoder_t *)p_this;
    encoder_sys_t *p_sys = p_enc->p_sys;

    free( p_sys );
}