diff options
Diffstat (limited to 'libavcodec/rscc.c')
-rw-r--r-- | libavcodec/rscc.c | 77 |
1 files changed, 62 insertions, 15 deletions
diff --git a/libavcodec/rscc.c b/libavcodec/rscc.c index dfc2338..7d4e842 100644 --- a/libavcodec/rscc.c +++ b/libavcodec/rscc.c @@ -2,20 +2,20 @@ * innoHeim/Rsupport Screen Capture Codec * Copyright (C) 2015 Vittorio Giovara <vittorio.giovara@gmail.com> * - * 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 */ @@ -31,7 +31,7 @@ * and it can be deflated or not. Similarly, pixel data comes after the header * and a variable size value, and it can be deflated or just raw. * - * Supports: PAL8, BGRA, BGR24, RGB555, RGB8 + * Supports: PAL8, BGRA, BGR24, RGB555 */ #include <stdint.h> @@ -64,6 +64,7 @@ typedef struct RsccContext { /* zlib interaction */ uint8_t *inflated_buf; uLongf inflated_size; + int valid_pixels; } RsccContext; static av_cold int rscc_init(AVCodecContext *avctx) @@ -85,8 +86,18 @@ static av_cold int rscc_init(AVCodecContext *avctx) /* Get pixel format and the size of the pixel */ if (avctx->codec_tag == MKTAG('I', 'S', 'C', 'C')) { - avctx->pix_fmt = AV_PIX_FMT_BGRA; - ctx->component_size = 4; + if (avctx->extradata && avctx->extradata_size == 4) { + if ((avctx->extradata[0] >> 1) & 1) { + avctx->pix_fmt = AV_PIX_FMT_BGRA; + ctx->component_size = 4; + } else { + avctx->pix_fmt = AV_PIX_FMT_BGR24; + ctx->component_size = 3; + } + } else { + avctx->pix_fmt = AV_PIX_FMT_BGRA; + ctx->component_size = 4; + } } else if (avctx->codec_tag == MKTAG('R', 'S', 'C', 'C')) { ctx->component_size = avctx->bits_per_coded_sample / 8; switch (avctx->bits_per_coded_sample) { @@ -100,7 +111,7 @@ static av_cold int rscc_init(AVCodecContext *avctx) avctx->pix_fmt = AV_PIX_FMT_BGR24; break; case 32: - avctx->pix_fmt = AV_PIX_FMT_BGRA; + avctx->pix_fmt = AV_PIX_FMT_BGR0; break; default: av_log(avctx, AV_LOG_ERROR, "Invalid bits per pixel value (%d)\n", @@ -108,8 +119,9 @@ static av_cold int rscc_init(AVCodecContext *avctx) return AVERROR_INVALIDDATA; } } else { - av_log(avctx, AV_LOG_ERROR, "Invalid codec tag\n"); - return AVERROR_INVALIDDATA; + avctx->pix_fmt = AV_PIX_FMT_BGR0; + ctx->component_size = 4; + av_log(avctx, AV_LOG_WARNING, "Invalid codec tag\n"); } /* Store the value to check for keyframes */ @@ -156,6 +168,12 @@ static int rscc_decode_frame(AVCodecContext *avctx, void *data, /* Read number of tiles, and allocate the array */ tiles_nb = bytestream2_get_le16(gbc); + + if (tiles_nb == 0) { + av_log(avctx, AV_LOG_DEBUG, "no tiles\n"); + return avpkt->size; + } + av_fast_malloc(&ctx->tiles, &ctx->tiles_size, tiles_nb * sizeof(*ctx->tiles)); if (!ctx->tiles) { @@ -210,6 +228,12 @@ static int rscc_decode_frame(AVCodecContext *avctx, void *data, ctx->tiles[i].y = bytestream2_get_le16(gbc); ctx->tiles[i].h = bytestream2_get_le16(gbc); + if (pixel_size + ctx->tiles[i].w * (int64_t)ctx->tiles[i].h * ctx->component_size > INT_MAX) { + av_log(avctx, AV_LOG_ERROR, "Invalid tile dimensions\n"); + ret = AVERROR_INVALIDDATA; + goto end; + } + pixel_size += ctx->tiles[i].w * ctx->tiles[i].h * ctx->component_size; ff_dlog(avctx, "tile %d orig(%d,%d) %dx%d.\n", i, @@ -249,11 +273,27 @@ static int rscc_decode_frame(AVCodecContext *avctx, void *data, ff_dlog(avctx, "pixel_size %d packed_size %d.\n", pixel_size, packed_size); + if (packed_size < 0) { + av_log(avctx, AV_LOG_ERROR, "Invalid tile size %d\n", packed_size); + ret = AVERROR_INVALIDDATA; + goto end; + } + /* Get pixels buffer, it may be deflated or just raw */ if (pixel_size == packed_size) { + if (bytestream2_get_bytes_left(gbc) < pixel_size) { + av_log(avctx, AV_LOG_ERROR, "Insufficient input for %d\n", pixel_size); + ret = AVERROR_INVALIDDATA; + goto end; + } pixels = gbc->buffer; } else { uLongf len = ctx->inflated_size; + if (bytestream2_get_bytes_left(gbc) < packed_size) { + av_log(avctx, AV_LOG_ERROR, "Insufficient input for %d\n", packed_size); + ret = AVERROR_INVALIDDATA; + goto end; + } ret = uncompress(ctx->inflated_buf, &len, gbc->buffer, packed_size); if (ret) { av_log(avctx, AV_LOG_ERROR, "Pixel deflate error %d.\n", ret); @@ -296,18 +336,25 @@ static int rscc_decode_frame(AVCodecContext *avctx, void *data, /* Palette handling */ if (avctx->pix_fmt == AV_PIX_FMT_PAL8) { + int size; const uint8_t *palette = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, - NULL); - if (palette) { + &size); + if (palette && size == AVPALETTE_SIZE) { frame->palette_has_changed = 1; memcpy(ctx->palette, palette, AVPALETTE_SIZE); + } else if (palette) { + av_log(avctx, AV_LOG_ERROR, "Palette size %d is wrong\n", size); } - memcpy(frame->data[1], ctx->palette, AVPALETTE_SIZE); + memcpy (frame->data[1], ctx->palette, AVPALETTE_SIZE); } + // We only return a picture when enough of it is undamaged, this avoids copying nearly broken frames around + if (ctx->valid_pixels < ctx->inflated_size) + ctx->valid_pixels += pixel_size; + if (ctx->valid_pixels >= ctx->inflated_size * (100 - avctx->discard_damaged_percentage) / 100) + *got_frame = 1; - *got_frame = 1; - + ret = avpkt->size; end: av_free(inflated_tiles); return ret; |