2 * This file is part of FFmpeg.
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "libavutil/frame.h"
23 #include "libavutil/error.h"
24 #include "libavutil/log.h"
27 #include "bytestream.h"
34 typedef struct LSCRContext {
36 AVCodecContext *avctx;
38 AVFrame *last_picture;
44 unsigned int last_row_size;
56 static void handle_row(LSCRContext *s)
58 uint8_t *ptr, *last_row;
60 ptr = s->image_buf + s->image_linesize * s->y;
62 last_row = s->last_row;
64 last_row = ptr - s->image_linesize;
66 ff_png_filter_row(&s->dsp, ptr, s->crow_buf[0], s->crow_buf + 1,
67 last_row, s->row_size, 3);
72 static int decode_idat(LSCRContext *s, int length)
75 s->zstream.avail_in = FFMIN(length, bytestream2_get_bytes_left(&s->gb));
76 s->zstream.next_in = s->gb.buffer;
77 bytestream2_skip(&s->gb, length);
79 /* decode one line if possible */
80 while (s->zstream.avail_in > 0) {
81 ret = inflate(&s->zstream, Z_PARTIAL_FLUSH);
82 if (ret != Z_OK && ret != Z_STREAM_END) {
83 av_log(s->avctx, AV_LOG_ERROR, "inflate returned error %d\n", ret);
84 return AVERROR_EXTERNAL;
86 if (s->zstream.avail_out == 0) {
87 if (s->y < s->cur_h) {
90 s->zstream.avail_out = s->crow_size;
91 s->zstream.next_out = s->crow_buf;
93 if (ret == Z_STREAM_END && s->zstream.avail_in > 0) {
94 av_log(s->avctx, AV_LOG_WARNING,
95 "%d undecompressed bytes left in buffer\n", s->zstream.avail_in);
102 static int decode_frame_lscr(AVCodecContext *avctx,
103 void *data, int *got_frame,
106 LSCRContext *const s = avctx->priv_data;
107 GetByteContext *gb = &s->gb;
108 AVFrame *frame = data;
109 int ret, nb_blocks, offset = 0;
112 return AVERROR_INVALIDDATA;
113 if (avpkt->size == 2)
116 bytestream2_init(gb, avpkt->data, avpkt->size);
118 if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
121 nb_blocks = bytestream2_get_le16(gb);
122 if (bytestream2_get_bytes_left(gb) < 2 + nb_blocks * (12 + 8))
123 return AVERROR_INVALIDDATA;
125 if (s->last_picture->data[0]) {
126 ret = av_frame_copy(frame, s->last_picture);
131 for (int b = 0; b < nb_blocks; b++) {
132 int x, y, x2, y2, w, h, left;
133 uint32_t csize, size;
135 s->zstream.zalloc = ff_png_zalloc;
136 s->zstream.zfree = ff_png_zfree;
137 s->zstream.opaque = NULL;
139 if ((ret = inflateInit(&s->zstream)) != Z_OK) {
140 av_log(avctx, AV_LOG_ERROR, "inflateInit returned error %d\n", ret);
141 ret = AVERROR_EXTERNAL;
145 bytestream2_seek(gb, 2 + b * 12, SEEK_SET);
147 x = bytestream2_get_le16(gb);
148 y = bytestream2_get_le16(gb);
149 x2 = bytestream2_get_le16(gb);
150 y2 = bytestream2_get_le16(gb);
154 if (w <= 0 || x < 0 || x >= avctx->width || w + x > avctx->width ||
155 h <= 0 || y < 0 || y >= avctx->height || h + y > avctx->height) {
156 ret = AVERROR_INVALIDDATA;
160 size = bytestream2_get_le32(gb);
162 frame->key_frame = (nb_blocks == 1) &&
163 (w == avctx->width) &&
164 (h == avctx->height) &&
165 (x == 0) && (y == 0);
167 bytestream2_seek(gb, 2 + nb_blocks * 12 + offset, SEEK_SET);
168 csize = bytestream2_get_be32(gb);
169 if (bytestream2_get_le32(gb) != MKTAG('I', 'D', 'A', 'T')) {
170 ret = AVERROR_INVALIDDATA;
180 av_fast_padded_malloc(&s->buffer, &s->buffer_size, s->row_size + 16);
182 ret = AVERROR(ENOMEM);
186 av_fast_padded_malloc(&s->last_row, &s->last_row_size, s->row_size);
188 ret = AVERROR(ENOMEM);
192 s->crow_size = w * 3 + 1;
193 s->crow_buf = s->buffer + 15;
194 s->zstream.avail_out = s->crow_size;
195 s->zstream.next_out = s->crow_buf;
196 s->image_buf = frame->data[0] + (avctx->height - y - 1) * frame->linesize[0] + x * 3;
197 s->image_linesize =-frame->linesize[0];
200 ret = decode_idat(s, csize);
205 bytestream2_skip(gb, 4);
206 csize = bytestream2_get_be32(gb);
207 if (bytestream2_get_le32(gb) != MKTAG('I', 'D', 'A', 'T')) {
208 ret = AVERROR_INVALIDDATA;
214 inflateEnd(&s->zstream);
217 frame->pict_type = frame->key_frame ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
219 av_frame_unref(s->last_picture);
220 if ((ret = av_frame_ref(s->last_picture, frame)) < 0)
225 inflateEnd(&s->zstream);
232 static int lscr_decode_close(AVCodecContext *avctx)
234 LSCRContext *s = avctx->priv_data;
236 av_frame_free(&s->last_picture);
237 av_freep(&s->buffer);
238 av_freep(&s->last_row);
243 static int lscr_decode_init(AVCodecContext *avctx)
245 LSCRContext *s = avctx->priv_data;
247 avctx->color_range = AVCOL_RANGE_JPEG;
248 avctx->pix_fmt = AV_PIX_FMT_BGR24;
251 s->last_picture = av_frame_alloc();
252 if (!s->last_picture)
253 return AVERROR(ENOMEM);
255 ff_pngdsp_init(&s->dsp);
260 static void lscr_decode_flush(AVCodecContext *avctx)
262 LSCRContext *s = avctx->priv_data;
263 av_frame_unref(s->last_picture);
266 AVCodec ff_lscr_decoder = {
268 .long_name = NULL_IF_CONFIG_SMALL("LEAD Screen Capture"),
269 .type = AVMEDIA_TYPE_VIDEO,
270 .id = AV_CODEC_ID_LSCR,
271 .priv_data_size = sizeof(LSCRContext),
272 .init = lscr_decode_init,
273 .close = lscr_decode_close,
274 .decode = decode_frame_lscr,
275 .flush = lscr_decode_flush,
276 .capabilities = AV_CODEC_CAP_DR1,
277 .caps_internal = FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM | FF_CODEC_CAP_INIT_THREADSAFE |
278 FF_CODEC_CAP_ALLOCATE_PROGRESS,