[vlc-devel] [PATCH v3] codec: jpeg encoder implemented

Rafaël Carré funman at videolan.org
Wed Jan 29 16:30:36 CET 2014


Hello,

On 01/29/14 14:43, Maxim Bublis wrote:
> This patch implements jpeg encoder using libjpeg.

> +/*
> + * Probe the encoder and return score
> + */
> +static int OpenEncoder(vlc_object_t *p_this)
> +{
> +    encoder_t *p_enc = (encoder_t *)p_this;
> +
> +    config_ChainParse(p_enc, ENC_CFG_PREFIX, ppsz_enc_options, p_enc->p_cfg);
> +
> +    if (p_enc->fmt_out.i_codec != VLC_CODEC_JPEG)
> +    {
> +        return VLC_EGENERIC;
> +    }
> +
> +    /* Allocate the memory needed to store 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->pf_encode_video = EncodeBlock;
> +
> +    p_enc->p_sys->i_quality = var_GetInteger(p_enc, ENC_CFG_PREFIX "quality");
> +
> +    return VLC_SUCCESS;
> +}
> +
> +static block_t *EncodeBlock(encoder_t *p_enc, picture_t *p_pic)
> +{
> +    encoder_sys_t *p_sys = p_enc->p_sys;
> +
> +    const int bytesPerPixel = p_enc->fmt_out.video.i_bits_per_pixel ?
> +                                p_enc->fmt_out.video.i_bits_per_pixel / 8 : 3;

This should be done in Open although if we can't mux a mjpeg stream it's
useless.

However you should check video input format before assuming it's 3 bytes
per pixel.

Is the input J420 ?

You should force p_enc->fmt_in.i_codec = VLC_CODEC_J420 and then no need
to calculate
the bytes per pixel.

> +    const int blocksize = bytesPerPixel * p_enc->fmt_in.video.i_visible_width * p_enc->fmt_in.video.i_visible_height;


> +    p_jpeg.in_color_space = JCS_YCbCr;

I believe this is J420.


> +    for (int i = 0; i < p_pic->i_planes; i++)
> +    {
> +        p_row_pointers[i] = malloc(sizeof(JSAMPROW) * p_jpeg.comp_info[i].v_samp_factor * DCTSIZE);

Really, no way to operate on the picture buffers directly?

> +    }
> +
> +    while (p_jpeg.next_scanline < p_jpeg.image_height)
> +    {
> +        for (int i = 0; i < p_pic->i_planes; i++)
> +        {
> +            for (int j = 0; j < p_jpeg.comp_info[i].v_samp_factor * DCTSIZE; j++)
> +            {
> +                p_row_pointers[i][j] = p_pic->p[i].p_pixels + p_pic->p[i].i_pitch * p_jpeg.next_scanline * p_jpeg.comp_info[i].v_samp_factor / p_jpeg.max_v_samp_factor + j;
> +            }
> +        }
> +        jpeg_write_raw_data(&p_jpeg, p_row_pointers, p_jpeg.max_v_samp_factor * DCTSIZE);
> +    }
> +
> +    jpeg_finish_compress(&p_jpeg);
> +    jpeg_destroy_compress(&p_jpeg);
> +
> +    for (int i = 0; i < p_pic->i_planes; i++)
> +    {
> +        free(p_row_pointers[i]);
> +    }
> +    free(p_row_pointers);
> +
> +    return p_block;
> +
> +error:
> +    jpeg_destroy_compress(&p_jpeg);
> +
> +    if (p_row_pointers != NULL)
> +    {
> +        for (int i = 0; i < p_pic->i_planes; i++)
> +        {
> +            free(p_row_pointers[i]);
> +        }
> +    }
> +    free(p_row_pointers);
> +
> +    block_Release(p_block);
> +
> +    return NULL;
> +}



More information about the vlc-devel mailing list