]> git.sesse.net Git - ffmpeg/blob - libavcodec/lscrdec.c
lavc: split LSCR decoder out of PNG decoder
[ffmpeg] / libavcodec / lscrdec.c
1 /*
2  * This file is part of FFmpeg.
3  *
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.
8  *
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.
13  *
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
17  */
18
19 #include <stdint.h>
20 #include <zlib.h>
21
22 #include "libavutil/frame.h"
23 #include "libavutil/error.h"
24 #include "libavutil/log.h"
25
26 #include "avcodec.h"
27 #include "bytestream.h"
28 #include "codec.h"
29 #include "internal.h"
30 #include "packet.h"
31 #include "png.h"
32 #include "pngdsp.h"
33
34 typedef struct LSCRContext {
35     PNGDSPContext   dsp;
36     AVCodecContext *avctx;
37
38     AVFrame        *last_picture;
39     uint8_t        *buffer;
40     int             buffer_size;
41     uint8_t        *crow_buf;
42     int             crow_size;
43     uint8_t        *last_row;
44     unsigned int    last_row_size;
45
46     GetByteContext  gb;
47     uint8_t        *image_buf;
48     int             image_linesize;
49     int             row_size;
50     int             cur_h;
51     int             y;
52
53     z_stream        zstream;
54 } LSCRContext;
55
56 static void handle_row(LSCRContext *s)
57 {
58     uint8_t *ptr, *last_row;
59
60     ptr = s->image_buf + s->image_linesize * s->y;
61     if (s->y == 0)
62         last_row = s->last_row;
63     else
64         last_row = ptr - s->image_linesize;
65
66     ff_png_filter_row(&s->dsp, ptr, s->crow_buf[0], s->crow_buf + 1,
67                       last_row, s->row_size, 3);
68
69     s->y++;
70 }
71
72 static int decode_idat(LSCRContext *s, int length)
73 {
74     int ret;
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);
78
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;
85         }
86         if (s->zstream.avail_out == 0) {
87             if (s->y < s->cur_h) {
88                 handle_row(s);
89             }
90             s->zstream.avail_out = s->crow_size;
91             s->zstream.next_out  = s->crow_buf;
92         }
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);
96             return 0;
97         }
98     }
99     return 0;
100 }
101
102 static int decode_frame_lscr(AVCodecContext *avctx,
103                              void *data, int *got_frame,
104                              AVPacket *avpkt)
105 {
106     LSCRContext *const s = avctx->priv_data;
107     GetByteContext *gb = &s->gb;
108     AVFrame *frame = data;
109     int ret, nb_blocks, offset = 0;
110
111     if (avpkt->size < 2)
112         return AVERROR_INVALIDDATA;
113     if (avpkt->size == 2)
114         return 0;
115
116     bytestream2_init(gb, avpkt->data, avpkt->size);
117
118     if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
119         return ret;
120
121     nb_blocks = bytestream2_get_le16(gb);
122     if (bytestream2_get_bytes_left(gb) < 2 + nb_blocks * (12 + 8))
123         return AVERROR_INVALIDDATA;
124
125     if (s->last_picture->data[0]) {
126         ret = av_frame_copy(frame, s->last_picture);
127         if (ret < 0)
128             return ret;
129     }
130
131     for (int b = 0; b < nb_blocks; b++) {
132         int x, y, x2, y2, w, h, left;
133         uint32_t csize, size;
134
135         s->zstream.zalloc = ff_png_zalloc;
136         s->zstream.zfree  = ff_png_zfree;
137         s->zstream.opaque = NULL;
138
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;
142             goto end;
143         }
144
145         bytestream2_seek(gb, 2 + b * 12, SEEK_SET);
146
147         x = bytestream2_get_le16(gb);
148         y = bytestream2_get_le16(gb);
149         x2 = bytestream2_get_le16(gb);
150         y2 = bytestream2_get_le16(gb);
151         w = x2-x;
152         s->cur_h = h = y2-y;
153
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;
157             goto end;
158         }
159
160         size = bytestream2_get_le32(gb);
161
162         frame->key_frame = (nb_blocks == 1) &&
163                            (w == avctx->width) &&
164                            (h == avctx->height) &&
165                            (x == 0) && (y == 0);
166
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;
171             goto end;
172         }
173
174         offset += size;
175         left = size;
176
177         s->y                 = 0;
178         s->row_size          = w * 3;
179
180         av_fast_padded_malloc(&s->buffer, &s->buffer_size, s->row_size + 16);
181         if (!s->buffer) {
182             ret = AVERROR(ENOMEM);
183             goto end;
184         }
185
186         av_fast_padded_malloc(&s->last_row, &s->last_row_size, s->row_size);
187         if (!s->last_row) {
188             ret = AVERROR(ENOMEM);
189             goto end;
190         }
191
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];
198
199         while (left > 16) {
200             ret = decode_idat(s, csize);
201             if (ret < 0)
202                 goto end;
203             left -= csize + 16;
204             if (left > 16) {
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;
209                     goto end;
210                 }
211             }
212         }
213
214         inflateEnd(&s->zstream);
215     }
216
217     frame->pict_type = frame->key_frame ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
218
219     av_frame_unref(s->last_picture);
220     if ((ret = av_frame_ref(s->last_picture, frame)) < 0)
221         return ret;
222
223     *got_frame = 1;
224 end:
225     inflateEnd(&s->zstream);
226
227     if (ret < 0)
228         return ret;
229     return avpkt->size;
230 }
231
232 static int lscr_decode_close(AVCodecContext *avctx)
233 {
234     LSCRContext *s = avctx->priv_data;
235
236     av_frame_free(&s->last_picture);
237     av_freep(&s->buffer);
238     av_freep(&s->last_row);
239
240     return 0;
241 }
242
243 static int lscr_decode_init(AVCodecContext *avctx)
244 {
245     LSCRContext *s = avctx->priv_data;
246
247     avctx->color_range = AVCOL_RANGE_JPEG;
248     avctx->pix_fmt     = AV_PIX_FMT_BGR24;
249
250     s->avctx = avctx;
251     s->last_picture = av_frame_alloc();
252     if (!s->last_picture)
253         return AVERROR(ENOMEM);
254
255     ff_pngdsp_init(&s->dsp);
256
257     return 0;
258 }
259
260 static void lscr_decode_flush(AVCodecContext *avctx)
261 {
262     LSCRContext *s = avctx->priv_data;
263     av_frame_unref(s->last_picture);
264 }
265
266 AVCodec ff_lscr_decoder = {
267     .name           = "lscr",
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,
279 };