2 * Copyright (c) 2019 Paul B Mahol
4 * This file is part of FFmpeg.
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.
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.
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
24 #include "libavutil/frame.h"
25 #include "libavutil/error.h"
26 #include "libavutil/log.h"
29 #include "bytestream.h"
36 typedef struct LSCRContext {
38 AVCodecContext *avctx;
40 AVFrame *last_picture;
46 unsigned int last_row_size;
58 static void handle_row(LSCRContext *s)
60 uint8_t *ptr, *last_row;
62 ptr = s->image_buf + s->image_linesize * s->y;
64 last_row = s->last_row;
66 last_row = ptr - s->image_linesize;
68 ff_png_filter_row(&s->dsp, ptr, s->crow_buf[0], s->crow_buf + 1,
69 last_row, s->row_size, 3);
74 static int decode_idat(LSCRContext *s, int length)
77 s->zstream.avail_in = FFMIN(length, bytestream2_get_bytes_left(&s->gb));
78 s->zstream.next_in = s->gb.buffer;
81 return AVERROR_INVALIDDATA;
83 bytestream2_skip(&s->gb, length);
85 /* decode one line if possible */
86 while (s->zstream.avail_in > 0) {
87 ret = inflate(&s->zstream, Z_PARTIAL_FLUSH);
88 if (ret != Z_OK && ret != Z_STREAM_END) {
89 av_log(s->avctx, AV_LOG_ERROR, "inflate returned error %d\n", ret);
90 return AVERROR_EXTERNAL;
92 if (s->zstream.avail_out == 0) {
93 if (s->y < s->cur_h) {
96 s->zstream.avail_out = s->crow_size;
97 s->zstream.next_out = s->crow_buf;
99 if (ret == Z_STREAM_END && s->zstream.avail_in > 0) {
100 av_log(s->avctx, AV_LOG_WARNING,
101 "%d undecompressed bytes left in buffer\n", s->zstream.avail_in);
108 static int decode_frame_lscr(AVCodecContext *avctx,
109 void *data, int *got_frame,
112 LSCRContext *const s = avctx->priv_data;
113 GetByteContext *gb = &s->gb;
114 AVFrame *frame = s->last_picture;
115 int ret, nb_blocks, offset = 0;
118 return AVERROR_INVALIDDATA;
119 if (avpkt->size == 2)
122 bytestream2_init(gb, avpkt->data, avpkt->size);
124 nb_blocks = bytestream2_get_le16(gb);
125 if (bytestream2_get_bytes_left(gb) < 2 + nb_blocks * (12 + 8))
126 return AVERROR_INVALIDDATA;
128 ret = ff_reget_buffer(avctx, frame,
129 nb_blocks ? 0 : FF_REGET_BUFFER_FLAG_READONLY);
133 for (int b = 0; b < nb_blocks; b++) {
134 int x, y, x2, y2, w, h, left;
135 uint32_t csize, size;
137 s->zstream.zalloc = ff_png_zalloc;
138 s->zstream.zfree = ff_png_zfree;
139 s->zstream.opaque = NULL;
141 if ((ret = inflateInit(&s->zstream)) != Z_OK) {
142 av_log(avctx, AV_LOG_ERROR, "inflateInit returned error %d\n", ret);
143 ret = AVERROR_EXTERNAL;
147 bytestream2_seek(gb, 2 + b * 12, SEEK_SET);
149 x = bytestream2_get_le16(gb);
150 y = bytestream2_get_le16(gb);
151 x2 = bytestream2_get_le16(gb);
152 y2 = bytestream2_get_le16(gb);
156 if (w <= 0 || x < 0 || x >= avctx->width || w + x > avctx->width ||
157 h <= 0 || y < 0 || y >= avctx->height || h + y > avctx->height) {
158 ret = AVERROR_INVALIDDATA;
162 size = bytestream2_get_le32(gb);
164 frame->key_frame = (nb_blocks == 1) &&
165 (w == avctx->width) &&
166 (h == avctx->height) &&
167 (x == 0) && (y == 0);
169 bytestream2_seek(gb, 2 + nb_blocks * 12 + offset, SEEK_SET);
170 csize = bytestream2_get_be32(gb);
171 if (bytestream2_get_le32(gb) != MKTAG('I', 'D', 'A', 'T')) {
172 ret = AVERROR_INVALIDDATA;
182 av_fast_padded_malloc(&s->buffer, &s->buffer_size, s->row_size + 16);
184 ret = AVERROR(ENOMEM);
188 av_fast_padded_malloc(&s->last_row, &s->last_row_size, s->row_size);
190 ret = AVERROR(ENOMEM);
194 s->crow_size = w * 3 + 1;
195 s->crow_buf = s->buffer + 15;
196 s->zstream.avail_out = s->crow_size;
197 s->zstream.next_out = s->crow_buf;
198 s->image_buf = frame->data[0] + (avctx->height - y - 1) * frame->linesize[0] + x * 3;
199 s->image_linesize =-frame->linesize[0];
202 ret = decode_idat(s, csize);
207 bytestream2_skip(gb, 4);
208 csize = bytestream2_get_be32(gb);
209 if (bytestream2_get_le32(gb) != MKTAG('I', 'D', 'A', 'T')) {
210 ret = AVERROR_INVALIDDATA;
216 inflateEnd(&s->zstream);
219 frame->pict_type = frame->key_frame ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
221 if ((ret = av_frame_ref(data, frame)) < 0)
226 inflateEnd(&s->zstream);
233 static int lscr_decode_close(AVCodecContext *avctx)
235 LSCRContext *s = avctx->priv_data;
237 av_frame_free(&s->last_picture);
238 av_freep(&s->buffer);
239 av_freep(&s->last_row);
244 static int lscr_decode_init(AVCodecContext *avctx)
246 LSCRContext *s = avctx->priv_data;
248 avctx->color_range = AVCOL_RANGE_JPEG;
249 avctx->pix_fmt = AV_PIX_FMT_BGR24;
252 s->last_picture = av_frame_alloc();
253 if (!s->last_picture)
254 return AVERROR(ENOMEM);
256 ff_pngdsp_init(&s->dsp);
261 static void lscr_decode_flush(AVCodecContext *avctx)
263 LSCRContext *s = avctx->priv_data;
264 av_frame_unref(s->last_picture);
267 const AVCodec ff_lscr_decoder = {
269 .long_name = NULL_IF_CONFIG_SMALL("LEAD Screen Capture"),
270 .type = AVMEDIA_TYPE_VIDEO,
271 .id = AV_CODEC_ID_LSCR,
272 .priv_data_size = sizeof(LSCRContext),
273 .init = lscr_decode_init,
274 .close = lscr_decode_close,
275 .decode = decode_frame_lscr,
276 .flush = lscr_decode_flush,
277 .capabilities = AV_CODEC_CAP_DR1,
278 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,