On 02/16/2012 12:14 AM, Aneesh Dogra wrote:

> ---
>  Changelog               |    1 +
>  doc/general.texi        |    2 +-
>  libavcodec/Makefile     |    1 +
>  libavcodec/allcodecs.c  |    2 +-
>  libavcodec/sunrastenc.c |  224 
> +++++++++++++++++++++++++++++++++++++++++++++++
>  libavformat/img2.c      |    3 +-
>  6 files changed, 230 insertions(+), 3 deletions(-)
>  create mode 100644 libavcodec/sunrastenc.c
> 
> diff --git a/Changelog b/Changelog
> index cc7420c..412520e 100644
> --- a/Changelog
> +++ b/Changelog
> @@ -6,6 +6,7 @@ version <next>:
>  - XWD encoder and decoder
>  - Support for fragmentation in the mov/mp4 muxer
>  - ISMV (Smooth Streaming) muxer
> +- Sun Rasterfile Encoder
>  
>  
>  version 0.8:
> diff --git a/doc/general.texi b/doc/general.texi
> index 50ae764..e19a307 100644
> --- a/doc/general.texi
> +++ b/doc/general.texi
> @@ -373,7 +373,7 @@ following image formats are supported:
>      @tab V.Flash PTX format
>  @item SGI          @tab X @tab X
>      @tab SGI RGB image format
> -@item Sun Rasterfile  @tab   @tab X
> +@item Sun Rasterfile  @tab X @tab X
>      @tab Sun RAS image format
>  @item TIFF         @tab X @tab X
>      @tab YUV, JPEG and some extension is not supported yet.
> diff --git a/libavcodec/Makefile b/libavcodec/Makefile
> index a891651..88faefb 100644
> --- a/libavcodec/Makefile
> +++ b/libavcodec/Makefile
> @@ -354,6 +354,7 @@ OBJS-$(CONFIG_SOL_DPCM_DECODER)        += dpcm.o
>  OBJS-$(CONFIG_SP5X_DECODER)            += sp5xdec.o mjpegdec.o mjpeg.o
>  OBJS-$(CONFIG_SRT_DECODER)             += srtdec.o ass.o
>  OBJS-$(CONFIG_SUNRAST_DECODER)         += sunrast.o
> +OBJS-$(CONFIG_SUNRAST_ENCODER)         += sunrastenc.o
>  OBJS-$(CONFIG_SVQ1_DECODER)            += svq1dec.o svq1.o h263.o \
>                                            mpegvideo.o error_resilience.o
>  OBJS-$(CONFIG_SVQ1_ENCODER)            += svq1enc.o svq1.o    \
> diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
> index cda71e0..69a37b2 100644
> --- a/libavcodec/allcodecs.c
> +++ b/libavcodec/allcodecs.c
> @@ -188,7 +188,7 @@ void avcodec_register_all(void)
>      REGISTER_DECODER (SMC, smc);
>      REGISTER_ENCDEC  (SNOW, snow);
>      REGISTER_DECODER (SP5X, sp5x);
> -    REGISTER_DECODER (SUNRAST, sunrast);
> +    REGISTER_ENCDEC  (SUNRAST, sunrast);
>      REGISTER_ENCDEC  (SVQ1, svq1);
>      REGISTER_DECODER (SVQ3, svq3);
>      REGISTER_ENCDEC  (TARGA, targa);
> diff --git a/libavcodec/sunrastenc.c b/libavcodec/sunrastenc.c
> new file mode 100644
> index 0000000..a5318a9
> --- /dev/null
> +++ b/libavcodec/sunrastenc.c
> @@ -0,0 +1,224 @@
> +/*
> + * Sun Rasterfile (.sun/.ras/im{1,8,24}/.sunras) image encoder
> + * Copyright (c) 2012 Aneesh Dogra (lionaneesh) <[email protected]>
> + *
> + * This file is part of Libav.
> + *
> + * Libav 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 (at your option) any later version.
> + *
> + * Libav is distributed in the hope that it will be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
> + * Lesser General Public License for more details.
> + *
> + * You should have received a copy of the GNU Lesser General Public
> + * License along with Libav; if not, write to the Free Software
> + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 
> USA
> + */
> +
> +#include "avcodec.h"
> +#include "bytestream.h"
> +#include "internal.h"
> +#include "libavutil/pixdesc.h"
> +#include "sunrast.h"
> +
> +#define RMT_NONE      0
> +#define RMT_EQUAL_RGB 1
> +#define RMT_RAW       2


what is RMT_RAW? it isn't used anywhere in the encoder, and the sunrast
decoder gives an error if the map type is > 1. is there any
documentation of the data layout anywhere?

> +
> +typedef struct SUNRASTContext {
> +    AVFrame picture;
> +    PutByteContext p;
> +    int depth;      ///< depth (1, 8, or 24 bits) of pixel


you can leave out the (1, 8, or 24 bits) part

> +    int length;     ///< length (bytes) of image
> +    int type;       ///< type of file
> +    int maptype;    ///< type of colormap
> +    int maplength;  ///< length (bytes) of following map


following what? how about:
"length (bytes) of colormap"

> +    int size;
> +} SUNRASTContext;
> +
> +static void sunrast_image_write_header(AVCodecContext *avctx)
> +{
> +    SUNRASTContext *s = avctx->priv_data;
> +
> +    bytestream2_put_be32u(&s->p, RAS_MAGIC);
> +    bytestream2_put_be32u(&s->p, avctx->width);
> +    bytestream2_put_be32u(&s->p, avctx->height);
> +    bytestream2_put_be32u(&s->p, s->depth);
> +    bytestream2_put_be32u(&s->p, s->length);
> +    bytestream2_put_be32u(&s->p, s->type);
> +    bytestream2_put_be32u(&s->p, s->maptype);
> +    bytestream2_put_be32u(&s->p, s->maplength);
> +}
> +
> +static void sunrast_image_write_image(AVCodecContext *avctx,
> +                                      const uint8_t *pixels,
> +                                      const uint32_t *palette_data,
> +                                      int linesize, AVPacket *avpkt)


Don't pass avpkt. You're not using it for anything.

> +{
> +    SUNRASTContext *s = avctx->priv_data;


You can put avctx in SUNRASTContext and pass that instead of casting it
again here.

> +    const uint8_t *ptr;
> +    uint32_t pixel;
> +    int x, y, len, alen, r, g, b;
> +    PutByteContext pb_r, pb_g;


pb_r, and pb_g can be declared inside of "if (s->maplength)", pixel, r,
g, and b can be declared inside the loop they're used in, and y can be
declared down below in the raw encoding section.

> +
> +    if (s->maplength) {     // palettized
> +        unsigned int len = s->maplength / 3;
> +
> +        pb_r = s->p;
> +        bytestream2_skip_p(&s->p, len);
> +        pb_g = s->p;
> +        bytestream2_skip_p(&s->p, len);
> +
> +        for (x = 0; x < len; x++) {
> +            pixel = palette_data[x];
> +            r = (pixel >> 16) & 0xFF;
> +            g = (pixel >> 8)  & 0xFF;
> +            b =  pixel        & 0xFF;
> +
> +            bytestream2_put_byteu(&pb_r, r);
> +            bytestream2_put_byteu(&pb_g, g);
> +            bytestream2_put_byteu(&s->p, b);


you can just merge those like:
bytestream2_put_byteu(&pb_r, (pixel >> 16) & 0xFF);
etc...


> +        }
> +    }
> +
> +    len  = (s->depth * avctx->width + 7) >> 3;
> +    alen = len + (len & 1);
> +    ptr  = pixels;
> +
> +     if (s->type == RT_BYTE_ENCODED) {
> +        uint8_t value, value2;
> +        int run;
> +        const uint8_t *end = pixels + avctx->height * linesize;
> +        int x = 0;
> +
> +        ptr = pixels;
> +#define RLE_TRIGGER 0x80
> +#define GET_VALUE (ptr >= end || x >= len) ? ptr[len-1] : ptr[x]


I'm sorry, I led you wrong on this one with my previous suggestion. we
can't read ptr[len-1] if ptr >= end. So it should be something like:

#define GET_VALUE ptr >= end ? 0 : x >= len ? ptr[len-1] : ptr[x]

> +
> +        while (ptr < end) {
> +            run = 1;
> +            value = GET_VALUE;
> +            x++;
> +            if (x >= alen) {
> +                x = 0;
> +                ptr += linesize;
> +            }
> +
> +            value2 = GET_VALUE;
> +            while (value2 == value && run < 256 && ptr < end) {
> +                x++;
> +                run++;
> +                if (x >= alen) {
> +                    x = 0;
> +                    ptr += linesize;
> +                }
> +                value2 = GET_VALUE;
> +            }


You can simplify this slightly by reading value2 once before the start
of the loop, then changing the first "value = GET_VALUE" to "value =
value2".

> +
> +            if (run > 2 || value == RLE_TRIGGER) {
> +                bytestream2_put_byteu(&s->p, RLE_TRIGGER);
> +                bytestream2_put_byteu(&s->p, run - 1);
> +                    if (run > 1)
> +                        bytestream2_put_byteu(&s->p, value);
> +            } else if (run == 1) {
> +               bytestream2_put_byteu(&s->p, value);
> +            } else
> +                bytestream2_put_be16u(&s->p, (value << 8) | value);
> +        }
> +
> +        // update data length in header
> +        s->length = bytestream2_tell_p(&s->p) - 32 - s->maplength;


nit: update data length for header

> +    } else {
> +        for (y = 0; y < avctx->height; y++) {
> +            bytestream2_put_buffer(&s->p, ptr, len);
> +            if (len < alen)
> +                bytestream2_put_byteu(&s->p, 0);
> +            ptr += linesize;
> +        }
> +    }
> +}
> +
> +static av_cold int sunrast_encode_init(AVCodecContext *avctx)
> +{
> +    SUNRASTContext *s = avctx->priv_data;
> +
> +    avctx->coded_frame   = &s->picture;
> +    s->picture.pict_type = AV_PICTURE_TYPE_I;
> +    s->maplength         = 0;
> +    s->type              = (avctx->coder_type == FF_CODER_TYPE_RLE) ?
> +                                                    RT_BYTE_ENCODED : 
> RT_STANDARD;


Since you've set RLE as the default, you can validate that it's either
RAW or RLE and return an error if it's not.

> +    s->maptype           = RMT_NONE;
> +
> +    switch (avctx->pix_fmt) {
> +    case PIX_FMT_MONOWHITE:
> +        s->depth = 1;
> +        break;
> +    case PIX_FMT_PAL8 :
> +    case PIX_FMT_GRAY8:
> +        s->depth = 8;
> +        if (avctx->pix_fmt == PIX_FMT_PAL8) {
> +            s->maptype   = RMT_EQUAL_RGB;
> +            s->maplength = 3 * 256;
> +        }
> +        break;
> +    case PIX_FMT_BGR24:
> +        s->depth = 24;
> +        break;
> +    default:
> +        return AVERROR_BUG;
> +    }
> +    s->length = avctx->height * (FFALIGN(avctx->width * s->depth, 16) >> 3);
> +    s->size   = 32 + s->maplength + s->length * (s->type == RT_BYTE_ENCODED ?
> +                                                                          2 
> : 1);


nit: vertical alignment looks weird. try moving the whole "s->length *
..." to the next line and it might look a little better.

> +
> +    return 0;
> +}
> +
> +static int sunrast_encode_frame(AVCodecContext *avctx,  AVPacket *avpkt,
> +                                const AVFrame *frame, int *got_packet_ptr)
> +{
> +    SUNRASTContext *s = avctx->priv_data;
> +    int ret;
> +
> +    if ((ret = ff_alloc_packet(avpkt, s->size)) < 0)
> +        return ret;
> +
> +    bytestream2_init_writer(&s->p, avpkt->data, avpkt->size);
> +    avctx->coded_frame->key_frame = 1;
> +    sunrast_image_write_header(avctx);
> +    sunrast_image_write_image(avctx, frame->data[0],
> +                              (const uint32_t *)frame->data[1],
> +                              frame->linesize[0], avpkt);
> +    // update data length in header after RLE
> +    if (s->type == RT_BYTE_ENCODED)
> +        AV_WB32(&avpkt->data[16], s->length);
> +
> +    *got_packet_ptr = 1;
> +    avpkt->size = bytestream2_tell_p(&s->p);
> +    return 0;
> +}
> +
> +static const AVCodecDefault sunrast_defaults[] = {
> +     { "coder", "rle" },
> +     { NULL },
> +};
> +
> +AVCodec ff_sunrast_encoder = {
> +    .name           = "sunrast",
> +    .type           = AVMEDIA_TYPE_VIDEO,
> +    .id             = CODEC_ID_SUNRAST,
> +    .priv_data_size = sizeof(SUNRASTContext),
> +    .init           = sunrast_encode_init,
> +    .encode2        = sunrast_encode_frame,
> +    .defaults       = sunrast_defaults,
> +    .pix_fmts       = (const enum PixelFormat[]){ PIX_FMT_BGR24,
> +                                                  PIX_FMT_PAL8,
> +                                                  PIX_FMT_GRAY8,
> +                                                  PIX_FMT_MONOWHITE,
> +                                                  PIX_FMT_NONE },
> +    .long_name      = NULL_IF_CONFIG_SMALL("Sun Rasterfile image"),
> +};
> diff --git a/libavformat/img2.c b/libavformat/img2.c
> index f7a9fa5..d7a749c 100644
> --- a/libavformat/img2.c
> +++ b/libavformat/img2.c
> @@ -491,7 +491,8 @@ AVOutputFormat ff_image2_muxer = {
>      .name           = "image2",
>      .long_name      = NULL_IF_CONFIG_SMALL("image2 sequence"),
>      .extensions     = "bmp,dpx,jpeg,jpg,ljpg,pam,pbm,pcx,pgm,pgmyuv,png,"
> -                      "ppm,sgi,tga,tif,tiff,jp2,xwd",
> +                      "ppm,sgi,tga,tif,tiff,jp2,xwd,sun,ras,rs,im1,im8,im24,"
> +                      "sunras",
>      .priv_data_size = sizeof(VideoData),
>      .video_codec    = CODEC_ID_MJPEG,
>      .write_header   = write_header,


Thanks,
Justin
_______________________________________________
libav-devel mailing list
[email protected]
https://lists.libav.org/mailman/listinfo/libav-devel

Reply via email to