1/* 2 * Copyright (c) 2019 Paul B Mahol 3 * 4 * This file is part of FFmpeg. 5 * 6 * FFmpeg is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU Lesser General Public 8 * License as published by the Free Software Foundation; either 9 * version 2.1 of the License, or (at your option) any later version. 10 * 11 * FFmpeg is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 * Lesser General Public License for more details. 15 * 16 * You should have received a copy of the GNU Lesser General Public 17 * License along with FFmpeg; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 19 */ 20 21#include <stdint.h> 22#include <zlib.h> 23 24#include "libavutil/frame.h" 25#include "libavutil/error.h" 26#include "libavutil/log.h" 27 28#include "avcodec.h" 29#include "bytestream.h" 30#include "codec.h" 31#include "codec_internal.h" 32#include "internal.h" 33#include "packet.h" 34#include "png.h" 35#include "pngdsp.h" 36#include "zlib_wrapper.h" 37 38typedef struct LSCRContext { 39 PNGDSPContext dsp; 40 AVCodecContext *avctx; 41 42 AVFrame *last_picture; 43 uint8_t *buffer; 44 int buffer_size; 45 uint8_t *crow_buf; 46 int crow_size; 47 uint8_t *last_row; 48 unsigned int last_row_size; 49 50 GetByteContext gb; 51 uint8_t *image_buf; 52 int image_linesize; 53 int row_size; 54 int cur_h; 55 int y; 56 57 FFZStream zstream; 58} LSCRContext; 59 60static void handle_row(LSCRContext *s) 61{ 62 uint8_t *ptr, *last_row; 63 64 ptr = s->image_buf + s->image_linesize * s->y; 65 if (s->y == 0) 66 last_row = s->last_row; 67 else 68 last_row = ptr - s->image_linesize; 69 70 ff_png_filter_row(&s->dsp, ptr, s->crow_buf[0], s->crow_buf + 1, 71 last_row, s->row_size, 3); 72 73 s->y++; 74} 75 76static int decode_idat(LSCRContext *s, z_stream *zstream, int length) 77{ 78 int ret; 79 zstream->avail_in = FFMIN(length, bytestream2_get_bytes_left(&s->gb)); 80 zstream->next_in = s->gb.buffer; 81 82 if (length <= 0) 83 return AVERROR_INVALIDDATA; 84 85 bytestream2_skip(&s->gb, length); 86 87 /* decode one line if possible */ 88 while (zstream->avail_in > 0) { 89 ret = inflate(zstream, Z_PARTIAL_FLUSH); 90 if (ret != Z_OK && ret != Z_STREAM_END) { 91 av_log(s->avctx, AV_LOG_ERROR, "inflate returned error %d\n", ret); 92 return AVERROR_EXTERNAL; 93 } 94 if (zstream->avail_out == 0) { 95 if (s->y < s->cur_h) { 96 handle_row(s); 97 } 98 zstream->avail_out = s->crow_size; 99 zstream->next_out = s->crow_buf; 100 } 101 if (ret == Z_STREAM_END && zstream->avail_in > 0) { 102 av_log(s->avctx, AV_LOG_WARNING, 103 "%d undecompressed bytes left in buffer\n", zstream->avail_in); 104 return 0; 105 } 106 } 107 return 0; 108} 109 110static int decode_frame_lscr(AVCodecContext *avctx, AVFrame *rframe, 111 int *got_frame, AVPacket *avpkt) 112{ 113 LSCRContext *const s = avctx->priv_data; 114 GetByteContext *gb = &s->gb; 115 AVFrame *frame = s->last_picture; 116 int ret, nb_blocks, offset = 0; 117 118 if (avpkt->size < 2) 119 return AVERROR_INVALIDDATA; 120 if (avpkt->size == 2) 121 return 0; 122 123 bytestream2_init(gb, avpkt->data, avpkt->size); 124 125 nb_blocks = bytestream2_get_le16(gb); 126 if (bytestream2_get_bytes_left(gb) < 2 + nb_blocks * (12 + 8)) 127 return AVERROR_INVALIDDATA; 128 129 ret = ff_reget_buffer(avctx, frame, 130 nb_blocks ? 0 : FF_REGET_BUFFER_FLAG_READONLY); 131 if (ret < 0) 132 return ret; 133 134 for (int b = 0; b < nb_blocks; b++) { 135 z_stream *const zstream = &s->zstream.zstream; 136 int x, y, x2, y2, w, h, left; 137 uint32_t csize, size; 138 139 if (inflateReset(zstream) != Z_OK) 140 return AVERROR_EXTERNAL; 141 142 bytestream2_seek(gb, 2 + b * 12, SEEK_SET); 143 144 x = bytestream2_get_le16(gb); 145 y = bytestream2_get_le16(gb); 146 x2 = bytestream2_get_le16(gb); 147 y2 = bytestream2_get_le16(gb); 148 w = x2-x; 149 s->cur_h = h = y2-y; 150 151 if (w <= 0 || x < 0 || x >= avctx->width || w + x > avctx->width || 152 h <= 0 || y < 0 || y >= avctx->height || h + y > avctx->height) 153 return AVERROR_INVALIDDATA; 154 155 size = bytestream2_get_le32(gb); 156 157 frame->key_frame = (nb_blocks == 1) && 158 (w == avctx->width) && 159 (h == avctx->height) && 160 (x == 0) && (y == 0); 161 162 bytestream2_seek(gb, 2 + nb_blocks * 12 + offset, SEEK_SET); 163 csize = bytestream2_get_be32(gb); 164 if (bytestream2_get_le32(gb) != MKTAG('I', 'D', 'A', 'T')) 165 return AVERROR_INVALIDDATA; 166 167 offset += size; 168 left = size; 169 170 s->y = 0; 171 s->row_size = w * 3; 172 173 av_fast_padded_malloc(&s->buffer, &s->buffer_size, s->row_size + 16); 174 if (!s->buffer) 175 return AVERROR(ENOMEM); 176 177 av_fast_padded_malloc(&s->last_row, &s->last_row_size, s->row_size); 178 if (!s->last_row) 179 return AVERROR(ENOMEM); 180 181 s->crow_size = w * 3 + 1; 182 s->crow_buf = s->buffer + 15; 183 zstream->avail_out = s->crow_size; 184 zstream->next_out = s->crow_buf; 185 s->image_buf = frame->data[0] + (avctx->height - y - 1) * frame->linesize[0] + x * 3; 186 s->image_linesize =-frame->linesize[0]; 187 188 while (left > 16) { 189 ret = decode_idat(s, zstream, csize); 190 if (ret < 0) 191 return ret; 192 left -= csize + 16; 193 if (left > 16) { 194 bytestream2_skip(gb, 4); 195 csize = bytestream2_get_be32(gb); 196 if (bytestream2_get_le32(gb) != MKTAG('I', 'D', 'A', 'T')) 197 return AVERROR_INVALIDDATA; 198 } 199 } 200 } 201 202 frame->pict_type = frame->key_frame ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P; 203 204 if ((ret = av_frame_ref(rframe, frame)) < 0) 205 return ret; 206 207 *got_frame = 1; 208 209 return avpkt->size; 210} 211 212static int lscr_decode_close(AVCodecContext *avctx) 213{ 214 LSCRContext *s = avctx->priv_data; 215 216 av_frame_free(&s->last_picture); 217 av_freep(&s->buffer); 218 av_freep(&s->last_row); 219 ff_inflate_end(&s->zstream); 220 221 return 0; 222} 223 224static int lscr_decode_init(AVCodecContext *avctx) 225{ 226 LSCRContext *s = avctx->priv_data; 227 228 avctx->color_range = AVCOL_RANGE_JPEG; 229 avctx->pix_fmt = AV_PIX_FMT_BGR24; 230 231 s->avctx = avctx; 232 s->last_picture = av_frame_alloc(); 233 if (!s->last_picture) 234 return AVERROR(ENOMEM); 235 236 ff_pngdsp_init(&s->dsp); 237 238 return ff_inflate_init(&s->zstream, avctx); 239} 240 241static void lscr_decode_flush(AVCodecContext *avctx) 242{ 243 LSCRContext *s = avctx->priv_data; 244 av_frame_unref(s->last_picture); 245} 246 247const FFCodec ff_lscr_decoder = { 248 .p.name = "lscr", 249 .p.long_name = NULL_IF_CONFIG_SMALL("LEAD Screen Capture"), 250 .p.type = AVMEDIA_TYPE_VIDEO, 251 .p.id = AV_CODEC_ID_LSCR, 252 .p.capabilities = AV_CODEC_CAP_DR1, 253 .priv_data_size = sizeof(LSCRContext), 254 .init = lscr_decode_init, 255 .close = lscr_decode_close, 256 FF_CODEC_DECODE_CB(decode_frame_lscr), 257 .flush = lscr_decode_flush, 258 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP, 259}; 260