diff options
Diffstat (limited to 'libavcodec/cdxl.c')
-rw-r--r-- | libavcodec/cdxl.c | 73 |
1 files changed, 55 insertions, 18 deletions
diff --git a/libavcodec/cdxl.c b/libavcodec/cdxl.c index 99e96eb502..c8d66b5845 100644 --- a/libavcodec/cdxl.c +++ b/libavcodec/cdxl.c @@ -2,32 +2,41 @@ * CDXL video decoder * Copyright (c) 2011-2012 Paul B Mahol * - * This file is part of Libav. + * This file is part of FFmpeg. * - * Libav is free software; you can redistribute it and/or + * FFmpeg 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, + * FFmpeg 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 + * License along with FFmpeg; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ +/** + * @file + * Commodore CDXL video decoder + * @author Paul B Mahol + */ + +#define UNCHECKED_BITSTREAM_READER 1 + #include "libavutil/intreadwrite.h" #include "libavutil/imgutils.h" #include "avcodec.h" +#include "bytestream.h" #include "get_bits.h" #include "internal.h" #define BIT_PLANAR 0x00 -#define BYTE_PLANAR 0x20 -#define CHUNKY 0x40 +#define CHUNKY 0x20 +#define BYTE_PLANAR 0x40 #define BIT_LINE 0x80 #define BYTE_LINE 0xC0 @@ -63,7 +72,7 @@ static void import_palette(CDXLVideoContext *c, uint32_t *new_palette) unsigned r = ((rgb >> 8) & 0xF) * 0x11; unsigned g = ((rgb >> 4) & 0xF) * 0x11; unsigned b = (rgb & 0xF) * 0x11; - AV_WN32(&new_palette[i], (r << 16) | (g << 8) | b); + AV_WN32(&new_palette[i], (0xFFU << 24) | (r << 16) | (g << 8) | b); } } @@ -72,7 +81,8 @@ static void bitplanar2chunky(CDXLVideoContext *c, int linesize, uint8_t *out) GetBitContext gb; int x, y, plane; - init_get_bits(&gb, c->video, c->video_size * 8); + if (init_get_bits8(&gb, c->video, c->video_size) < 0) + return; for (plane = 0; plane < c->bpp; plane++) { for (y = 0; y < c->avctx->height; y++) { for (x = 0; x < c->avctx->width; x++) @@ -87,7 +97,8 @@ static void bitline2chunky(CDXLVideoContext *c, int linesize, uint8_t *out) GetBitContext gb; int x, y, plane; - init_get_bits(&gb, c->video, c->video_size * 8); + if (init_get_bits8(&gb, c->video, c->video_size) < 0) + return; for (y = 0; y < c->avctx->height; y++) { for (plane = 0; plane < c->bpp; plane++) { for (x = 0; x < c->avctx->width; x++) @@ -97,6 +108,17 @@ static void bitline2chunky(CDXLVideoContext *c, int linesize, uint8_t *out) } } +static void chunky2chunky(CDXLVideoContext *c, int linesize, uint8_t *out) +{ + GetByteContext gb; + int y; + + bytestream2_init(&gb, c->video, c->video_size); + for (y = 0; y < c->avctx->height; y++) { + bytestream2_get_buffer(&gb, out + linesize * y, c->avctx->width * 3); + } +} + static void import_format(CDXLVideoContext *c, int linesize, uint8_t *out) { memset(out, 0, linesize * c->avctx->height); @@ -108,6 +130,9 @@ static void import_format(CDXLVideoContext *c, int linesize, uint8_t *out) case BIT_LINE: bitline2chunky(c, linesize, out); break; + case CHUNKY: + chunky2chunky(c, linesize, out); + break; } } @@ -115,10 +140,16 @@ static void cdxl_decode_rgb(CDXLVideoContext *c, AVFrame *frame) { uint32_t *new_palette = (uint32_t *)frame->data[1]; + memset(frame->data[1], 0, AVPALETTE_SIZE); import_palette(c, new_palette); import_format(c, frame->linesize[0], frame->data[0]); } +static void cdxl_decode_raw(CDXLVideoContext *c, AVFrame *frame) +{ + import_format(c, frame->linesize[0], frame->data[0]); +} + static void cdxl_decode_ham6(CDXLVideoContext *c, AVFrame *frame) { AVCodecContext *avctx = c->avctx; @@ -231,7 +262,7 @@ static int cdxl_decode_frame(AVCodecContext *avctx, void *data, return AVERROR_INVALIDDATA; if (c->bpp < 1) return AVERROR_INVALIDDATA; - if (c->format != BIT_PLANAR && c->format != BIT_LINE) { + if (c->format != BIT_PLANAR && c->format != BIT_LINE && c->format != CHUNKY) { avpriv_request_sample(avctx, "Pixel format 0x%0x", c->format); return AVERROR_PATCHWELCOME; } @@ -239,7 +270,10 @@ static int cdxl_decode_frame(AVCodecContext *avctx, void *data, if ((ret = ff_set_dimensions(avctx, w, h)) < 0) return ret; - aligned_width = FFALIGN(c->avctx->width, 16); + if (c->format == CHUNKY) + aligned_width = avctx->width; + else + aligned_width = FFALIGN(c->avctx->width, 16); c->padded_bits = aligned_width - c->avctx->width; if (c->video_size < aligned_width * avctx->height * c->bpp / 8) return AVERROR_INVALIDDATA; @@ -249,16 +283,17 @@ static int cdxl_decode_frame(AVCodecContext *avctx, void *data, if (c->palette_size != (1 << (c->bpp - 1))) return AVERROR_INVALIDDATA; avctx->pix_fmt = AV_PIX_FMT_BGR24; + } else if (!encoding && c->bpp == 24 && c->format == CHUNKY && + !c->palette_size) { + avctx->pix_fmt = AV_PIX_FMT_RGB24; } else { - avpriv_request_sample(avctx, "Encoding %d and bpp %d", - encoding, c->bpp); + avpriv_request_sample(avctx, "Encoding %d, bpp %d and format 0x%x", + encoding, c->bpp, c->format); return AVERROR_PATCHWELCOME; } - if ((ret = ff_get_buffer(avctx, p, 0)) < 0) { - av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n"); + if ((ret = ff_get_buffer(avctx, p, 0)) < 0) return ret; - } p->pict_type = AV_PICTURE_TYPE_I; if (encoding) { @@ -270,8 +305,10 @@ static int cdxl_decode_frame(AVCodecContext *avctx, void *data, cdxl_decode_ham8(c, p); else cdxl_decode_ham6(c, p); - } else { + } else if (avctx->pix_fmt == AV_PIX_FMT_PAL8) { cdxl_decode_rgb(c, p); + } else { + cdxl_decode_raw(c, p); } *got_frame = 1; @@ -282,7 +319,7 @@ static av_cold int cdxl_decode_end(AVCodecContext *avctx) { CDXLVideoContext *c = avctx->priv_data; - av_free(c->new_video); + av_freep(&c->new_video); return 0; } |