On 13/09/13 22:30, Justin Ruggles wrote:
> From: Aneesh Dogra <[email protected]>
> 
> ---
>  doc/general.texi        |   2 +
>  libavcodec/Makefile     |   1 +
>  libavcodec/allcodecs.c  |   1 +
>  libavcodec/avcodec.h    |   1 +
>  libavcodec/codec_desc.c |   8 ++
>  libavcodec/vp8.c        |  18 ++--
>  libavcodec/vp8.h        |   7 ++
>  libavcodec/webp.c       | 246 
> ++++++++++++++++++++++++++++++++++++++++++++++++
>  libavformat/img2.c      |   1 +
>  9 files changed, 276 insertions(+), 9 deletions(-)
>  create mode 100644 libavcodec/webp.c
> 
> diff --git a/doc/general.texi b/doc/general.texi
> index bd0a7fb..18e425a 100644
> --- a/doc/general.texi
> +++ b/doc/general.texi
> @@ -412,6 +412,8 @@ following image formats are supported:
>      @tab YUV, JPEG and some extension is not supported yet.
>  @item Truevision Targa  @tab X @tab X
>      @tab Targa (.TGA) image format
> +@item WebP         @tab @tab X
> +    @tab WebP image format (lossy only)
>  @item XBM  @tab X @tab
>      @tab X BitMap image format
>  @item XWD  @tab X @tab X
> diff --git a/libavcodec/Makefile b/libavcodec/Makefile
> index bb4f960..b10526b 100644
> --- a/libavcodec/Makefile
> +++ b/libavcodec/Makefile
> @@ -390,6 +390,7 @@ OBJS-$(CONFIG_VP6_DECODER)             += vp6.o vp56.o 
> vp56data.o vp56dsp.o \
>  OBJS-$(CONFIG_VP8_DECODER)             += vp8.o vp8dsp.o vp56rac.o
>  OBJS-$(CONFIG_VQA_DECODER)             += vqavideo.o
>  OBJS-$(CONFIG_WAVPACK_DECODER)         += wavpack.o
> +OBJS-$(CONFIG_WEBP_DECODER)            += webp.o
>  OBJS-$(CONFIG_WMALOSSLESS_DECODER)     += wmalosslessdec.o wma_common.o
>  OBJS-$(CONFIG_WMAPRO_DECODER)          += wmaprodec.o wma.o wma_common.o
>  OBJS-$(CONFIG_WMAV1_DECODER)           += wmadec.o wma.o wma_common.o 
> aactab.o
> diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
> index d4531f2..f827ca3 100644
> --- a/libavcodec/allcodecs.c
> +++ b/libavcodec/allcodecs.c
> @@ -253,6 +253,7 @@ void avcodec_register_all(void)
>      REGISTER_DECODER(VP6F,              vp6f);
>      REGISTER_DECODER(VP8,               vp8);
>      REGISTER_DECODER(VQA,               vqa);
> +    REGISTER_DECODER(WEBP,              webp);
>      REGISTER_ENCDEC (WMV1,              wmv1);
>      REGISTER_ENCDEC (WMV2,              wmv2);
>      REGISTER_DECODER(WMV3,              wmv3);
> diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h
> index 2a39b43..b894ee8 100644
> --- a/libavcodec/avcodec.h
> +++ b/libavcodec/avcodec.h
> @@ -273,6 +273,7 @@ enum AVCodecID {
>      AV_CODEC_ID_AIC,
>      AV_CODEC_ID_ESCAPE130,
>      AV_CODEC_ID_G2M,
> +    AV_CODEC_ID_WEBP,
>  
>      /* various PCM "codecs" */
>      AV_CODEC_ID_FIRST_AUDIO = 0x10000,     ///< A dummy id pointing at the 
> start of audio codecs
> diff --git a/libavcodec/codec_desc.c b/libavcodec/codec_desc.c
> index 90e4891..2be5a2c 100644
> --- a/libavcodec/codec_desc.c
> +++ b/libavcodec/codec_desc.c
> @@ -111,6 +111,14 @@ static const AVCodecDescriptor codec_descriptors[] = {
>                       AV_CODEC_PROP_LOSSLESS,
>      },
>      {
> +        .id        = AV_CODEC_ID_WEBP,
> +        .type      = AVMEDIA_TYPE_VIDEO,
> +        .name      = "webp",
> +        .long_name = NULL_IF_CONFIG_SMALL("WebP"),
> +        .props     = AV_CODEC_PROP_INTRA_ONLY | AV_CODEC_PROP_LOSSY |
> +                     AV_CODEC_PROP_LOSSLESS,
> +    },
> +    {
>          .id        = AV_CODEC_ID_MPEG4,
>          .type      = AVMEDIA_TYPE_VIDEO,
>          .name      = "mpeg4",
> diff --git a/libavcodec/vp8.c b/libavcodec/vp8.c
> index 17b79f5..3c60aa3 100644
> --- a/libavcodec/vp8.c
> +++ b/libavcodec/vp8.c
> @@ -1853,8 +1853,8 @@ static int vp8_decode_mb_row_sliced(AVCodecContext 
> *avctx, void *tdata,
>      return 0;
>  }
>  
> -static int vp8_decode_frame(AVCodecContext *avctx, void *data, int 
> *got_frame,
> -                            AVPacket *avpkt)
> +int ff_vp8_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
> +                        AVPacket *avpkt)
>  {
>      VP8Context *s = avctx->priv_data;
>      int ret, i, referenced, num_jobs;
> @@ -2010,7 +2010,7 @@ err:
>      return ret;
>  }
>  
> -static av_cold int vp8_decode_free(AVCodecContext *avctx)
> +av_cold int ff_vp8_decode_free(AVCodecContext *avctx)
>  {
>      VP8Context *s = avctx->priv_data;
>      int i;
> @@ -2033,7 +2033,7 @@ static av_cold int vp8_init_frames(VP8Context *s)
>      return 0;
>  }
>  
> -static av_cold int vp8_decode_init(AVCodecContext *avctx)
> +av_cold int ff_vp8_decode_init(AVCodecContext *avctx)
>  {
>      VP8Context *s = avctx->priv_data;
>      int ret;
> @@ -2047,7 +2047,7 @@ static av_cold int vp8_decode_init(AVCodecContext 
> *avctx)
>      ff_vp8dsp_init(&s->vp8dsp);
>  
>      if ((ret = vp8_init_frames(s)) < 0) {
> -        vp8_decode_free(avctx);
> +        ff_vp8_decode_free(avctx);
>          return ret;
>      }
>  
> @@ -2062,7 +2062,7 @@ static av_cold int 
> vp8_decode_init_thread_copy(AVCodecContext *avctx)
>      s->avctx = avctx;
>  
>      if ((ret = vp8_init_frames(s)) < 0) {
> -        vp8_decode_free(avctx);
> +        ff_vp8_decode_free(avctx);
>          return ret;
>      }
>  
> @@ -2110,9 +2110,9 @@ AVCodec ff_vp8_decoder = {
>      .type                  = AVMEDIA_TYPE_VIDEO,
>      .id                    = AV_CODEC_ID_VP8,
>      .priv_data_size        = sizeof(VP8Context),
> -    .init                  = vp8_decode_init,
> -    .close                 = vp8_decode_free,
> -    .decode                = vp8_decode_frame,
> +    .init                  = ff_vp8_decode_init,
> +    .close                 = ff_vp8_decode_free,
> +    .decode                = ff_vp8_decode_frame,
>      .capabilities          = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS | 
> CODEC_CAP_SLICE_THREADS,
>      .flush                 = vp8_decode_flush,
>      .long_name             = NULL_IF_CONFIG_SMALL("On2 VP8"),
> diff --git a/libavcodec/vp8.h b/libavcodec/vp8.h
> index 80f555f..6555629 100644
> --- a/libavcodec/vp8.h
> +++ b/libavcodec/vp8.h
> @@ -269,4 +269,11 @@ typedef struct VP8Context {
>      int mb_layout;
>  } VP8Context;
>  
> +int ff_vp8_decode_init(AVCodecContext *avctx);
> +
> +int ff_vp8_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
> +                        AVPacket *avpkt);
> +
> +int ff_vp8_decode_free(AVCodecContext *avctx);
> +
>  #endif /* AVCODEC_VP8_H */
> diff --git a/libavcodec/webp.c b/libavcodec/webp.c
> new file mode 100644
> index 0000000..33b242f
> --- /dev/null
> +++ b/libavcodec/webp.c
> @@ -0,0 +1,246 @@
> +/*
> + * WebP (.webp) image decoder
> + * Copyright (c) 2013 Aneesh Dogra <[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 "vp8.h"
> +
> +#define VP8X_FLAG_ANIMATION     0x2
> +#define VP8X_FLAG_XMP_METADATA  0x4
> +#define VP8X_FLAG_EXIF_METADATA 0x8
> +#define VP8X_FLAG_ALPHA         0x10
> +#define VP8X_FLAG_ICC           0x20

While at it align the numbers padding them with 0

> +
> +typedef struct WebPContext {
> +    VP8Context v;
> +    int initialized;
> +    GetByteContext alpha_gb;
> +    uint8_t has_alpha;
> +    int width;
> +    int height;
> +} WebPContext;

While at it reorder so we do not have holes (why has_alpha is uint8_t?).

> +static int vp8_lossless_decode_frame(AVCodecContext *avctx, AVFrame *p,
> +                                     int *got_frame, uint8_t *data_start,
> +                                     unsigned int data_size)
> +{
> +    WebPContext *s = avctx->priv_data;
> +    int ret;
> +
> +    avctx->pix_fmt = AV_PIX_FMT_ARGB;
> +
> +    /* use dummy dimensions for now */
> +    if (s->width)
> +        avctx->width = s->width;
> +    else
> +        avctx->width = 800;
> +    if (s->height)
> +        avctx->height = s->height;
> +    else
> +        avctx->height  = 600;

I guess the code above is a stray.

> +    if ((ret = ff_get_buffer(avctx, p, 0)) < 0) {
> +        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
> +        return ret;
> +    }
> +    memset(p->data[0], 0, p->linesize[0] * avctx->height);
> +
> +    *got_frame = 1;
> +    p->pict_type = AV_PICTURE_TYPE_I;
> +    p->key_frame = 1;
> +
> +    return data_size;
> +}
> +
> +static int vp8_lossy_decode_frame(AVCodecContext *avctx, AVFrame *p,
> +                                  int *got_frame, uint8_t *data_start,
> +                                  unsigned int data_size)
> +{
> +    WebPContext *s = avctx->priv_data;
> +    AVPacket pkt;
> +    int ret;
> +
> +    if (!s->initialized) {
> +        ff_vp8_decode_init(avctx);
> +        s->initialized = 1;
> +        if (s->has_alpha)
> +            avctx->pix_fmt = AV_PIX_FMT_YUVA420P;
> +    }
> +
> +    if (data_size > INT_MAX) {
> +        av_log(avctx, AV_LOG_ERROR, "unsupported chunk size\n");
> +        return AVERROR_PATCHWELCOME;
> +    }
> +
> +    av_init_packet(&pkt);
> +    pkt.data = data_start;
> +    pkt.size = data_size;
> +
> +    ret = ff_vp8_decode_frame(avctx, p, got_frame, &pkt);
> +    if (s->has_alpha) {
> +        int y;
> +        for (y = 0; y < s->height; y++)
> +            bytestream2_get_buffer(&s->alpha_gb,
> +                                   p->data[3] + p->linesize[3] * y, 
> s->width);
> +    }
> +    return ret;
> +}
> +
> +static int webp_decode_frame(AVCodecContext *avctx, void *data, int 
> *got_frame,
> +                             AVPacket *avpkt)
> +{
> +    AVFrame * const p = data;
> +    WebPContext *s = avctx->priv_data;
> +    GetByteContext gb;
> +    int ret = 0;
> +    unsigned int chunk_type, chunk_size;
> +    uint8_t vp8x_flags = 0;
> +
> +    s->width  = 0;
> +    s->height = 0;
> +    *got_frame = 0;
> +    s->has_alpha = 0;

vertical align.

> +    bytestream2_init(&gb, avpkt->data, avpkt->size);
> +
> +    if (bytestream2_get_bytes_left(&gb) < 12)
> +        return AVERROR_INVALIDDATA;
> +
> +    if (bytestream2_get_le32(&gb) != MKTAG('R', 'I', 'F', 'F')) {
> +        av_log(avctx, AV_LOG_ERROR, "missing RIFF tag\n");
> +        return AVERROR_INVALIDDATA;
> +    }
> +
> +    chunk_size = bytestream2_get_le32(&gb);
> +    if (bytestream2_get_bytes_left(&gb) < chunk_size)
> +        return AVERROR_INVALIDDATA;
> +
> +    if (bytestream2_get_le32(&gb) != MKTAG('W', 'E', 'B', 'P')) {
> +        av_log(avctx, AV_LOG_ERROR, "missing WEBP tag\n");
> +        return AVERROR_INVALIDDATA;
> +    }
> +
> +    while (bytestream2_get_bytes_left(&gb) > 0) {
> +        chunk_type = bytestream2_get_le32(&gb);
> +        chunk_size = bytestream2_get_le32(&gb);
> +        if (chunk_size == UINT32_MAX)
> +            return AVERROR_INVALIDDATA;
> +        chunk_size += chunk_size & 1;
> +
> +        if (bytestream2_get_bytes_left(&gb) < chunk_size)
> +            return AVERROR_INVALIDDATA;
> +
> +        switch (chunk_type) {
> +        case MKTAG('V', 'P', '8', ' '):
> +            if (!*got_frame) {
> +                ret = vp8_lossy_decode_frame(avctx, p, got_frame,
> +                                             avpkt->data + 
> bytestream2_tell(&gb),
> +                                             chunk_size);
> +                if (ret < 0)
> +                    return ret;
> +            }
> +            bytestream2_skip(&gb, chunk_size);
> +            break;
> +        case MKTAG('V', 'P', '8', 'L'):
> +            if (!*got_frame) {
> +                ret = vp8_lossless_decode_frame(avctx, p, got_frame,
> +                                                avpkt->data + 
> bytestream2_tell(&gb),
> +                                                chunk_size);
> +                if (ret < 0)
> +                    return ret;
> +            }
> +            bytestream2_skip(&gb, chunk_size);
> +            break;
> +        case MKTAG('V', 'P', '8', 'X'):
> +            vp8x_flags = bytestream2_get_byte(&gb);
> +            bytestream2_skip(&gb, 3);
> +            s->width  = bytestream2_get_le24(&gb) + 1;
> +            s->height = bytestream2_get_le24(&gb) + 1;
> +            break;
> +        case MKTAG('A', 'L', 'P', 'H'): {
> +            uint8_t alpha_header;
> +            uint8_t av_unused pre_p;
> +            uint8_t filter_m, compression;

do not use uint8_t when not necessary (here and all over it)
> +
> +            if (!(vp8x_flags & VP8X_FLAG_ALPHA)) {
> +                av_log(avctx, AV_LOG_WARNING,
> +                       "ALPHA chunk present, but alpha bit not set in the "
> +                       "VP8X header\n");
> +            }

Drop braces.

> +            s->alpha_gb = gb;
> +            bytestream2_skip(&gb, chunk_size);
> +            alpha_header = bytestream2_get_byte(&s->alpha_gb);
> +
> +            pre_p       = (alpha_header >> 4) & 0x03;
> +            filter_m    = (alpha_header >> 2) & 0x03;
> +            compression =  alpha_header       & 0x03;
> +
> +            if (filter_m || compression) {
> +                av_log(avctx, AV_LOG_VERBOSE,
> +                       "skipping unsupported ALPHA chunk\n");
> +            } else {
> +                s->has_alpha = 1;
> +            }

Drop braces, what if you have has_alpha set but filter or compression on?

> +
> +            break;
> +        }
> +        case MKTAG('I', 'C', 'C', 'P'):
> +        case MKTAG('A', 'N', 'I', 'M'):
> +        case MKTAG('A', 'N', 'M', 'F'):
> +        case MKTAG('E', 'X', 'I', 'F'):
> +        case MKTAG('X', 'M', 'P', ' '):
> +            av_log(avctx, AV_LOG_VERBOSE, "skipping unsupported chunk\n");
> +            bytestream2_skip(&gb, chunk_size);
> +            break;
> +        default:
> +            av_log(avctx, AV_LOG_WARNING, "skipping unknown chunk\n");

Print the hunk name.

> +            bytestream2_skip(&gb, chunk_size);
> +            break;
> +        }
> +    }
> +
> +    if (!*got_frame) {
> +        av_log(avctx, AV_LOG_ERROR, "image data not found\n");
> +        return AVERROR_INVALIDDATA;
> +    }
> +

I guess that's all.

lu

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

Reply via email to