]> git.sesse.net Git - ffmpeg/blob - libavcodec/lscrdec.c
lavc/lscrdec: use ff_reget_buffer()
[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 = s->last_picture;
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     nb_blocks = bytestream2_get_le16(gb);
119     if (bytestream2_get_bytes_left(gb) < 2 + nb_blocks * (12 + 8))
120         return AVERROR_INVALIDDATA;
121
122     ret = ff_reget_buffer(avctx, frame,
123                           nb_blocks ? 0 : FF_REGET_BUFFER_FLAG_READONLY);
124     if (ret < 0)
125         return ret;
126
127     for (int b = 0; b < nb_blocks; b++) {
128         int x, y, x2, y2, w, h, left;
129         uint32_t csize, size;
130
131         s->zstream.zalloc = ff_png_zalloc;
132         s->zstream.zfree  = ff_png_zfree;
133         s->zstream.opaque = NULL;
134
135         if ((ret = inflateInit(&s->zstream)) != Z_OK) {
136             av_log(avctx, AV_LOG_ERROR, "inflateInit returned error %d\n", ret);
137             ret = AVERROR_EXTERNAL;
138             goto end;
139         }
140
141         bytestream2_seek(gb, 2 + b * 12, SEEK_SET);
142
143         x = bytestream2_get_le16(gb);
144         y = bytestream2_get_le16(gb);
145         x2 = bytestream2_get_le16(gb);
146         y2 = bytestream2_get_le16(gb);
147         w = x2-x;
148         s->cur_h = h = y2-y;
149
150         if (w <= 0 || x < 0 || x >= avctx->width || w + x > avctx->width ||
151             h <= 0 || y < 0 || y >= avctx->height || h + y > avctx->height) {
152             ret = AVERROR_INVALIDDATA;
153             goto end;
154         }
155
156         size = bytestream2_get_le32(gb);
157
158         frame->key_frame = (nb_blocks == 1) &&
159                            (w == avctx->width) &&
160                            (h == avctx->height) &&
161                            (x == 0) && (y == 0);
162
163         bytestream2_seek(gb, 2 + nb_blocks * 12 + offset, SEEK_SET);
164         csize = bytestream2_get_be32(gb);
165         if (bytestream2_get_le32(gb) != MKTAG('I', 'D', 'A', 'T')) {
166             ret = AVERROR_INVALIDDATA;
167             goto end;
168         }
169
170         offset += size;
171         left = size;
172
173         s->y                 = 0;
174         s->row_size          = w * 3;
175
176         av_fast_padded_malloc(&s->buffer, &s->buffer_size, s->row_size + 16);
177         if (!s->buffer) {
178             ret = AVERROR(ENOMEM);
179             goto end;
180         }
181
182         av_fast_padded_malloc(&s->last_row, &s->last_row_size, s->row_size);
183         if (!s->last_row) {
184             ret = AVERROR(ENOMEM);
185             goto end;
186         }
187
188         s->crow_size         = w * 3 + 1;
189         s->crow_buf          = s->buffer + 15;
190         s->zstream.avail_out = s->crow_size;
191         s->zstream.next_out  = s->crow_buf;
192         s->image_buf         = frame->data[0] + (avctx->height - y - 1) * frame->linesize[0] + x * 3;
193         s->image_linesize    =-frame->linesize[0];
194
195         while (left > 16) {
196             ret = decode_idat(s, csize);
197             if (ret < 0)
198                 goto end;
199             left -= csize + 16;
200             if (left > 16) {
201                 bytestream2_skip(gb, 4);
202                 csize = bytestream2_get_be32(gb);
203                 if (bytestream2_get_le32(gb) != MKTAG('I', 'D', 'A', 'T')) {
204                     ret = AVERROR_INVALIDDATA;
205                     goto end;
206                 }
207             }
208         }
209
210         inflateEnd(&s->zstream);
211     }
212
213     frame->pict_type = frame->key_frame ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
214
215     if ((ret = av_frame_ref(data, frame)) < 0)
216         return ret;
217
218     *got_frame = 1;
219 end:
220     inflateEnd(&s->zstream);
221
222     if (ret < 0)
223         return ret;
224     return avpkt->size;
225 }
226
227 static int lscr_decode_close(AVCodecContext *avctx)
228 {
229     LSCRContext *s = avctx->priv_data;
230
231     av_frame_free(&s->last_picture);
232     av_freep(&s->buffer);
233     av_freep(&s->last_row);
234
235     return 0;
236 }
237
238 static int lscr_decode_init(AVCodecContext *avctx)
239 {
240     LSCRContext *s = avctx->priv_data;
241
242     avctx->color_range = AVCOL_RANGE_JPEG;
243     avctx->pix_fmt     = AV_PIX_FMT_BGR24;
244
245     s->avctx = avctx;
246     s->last_picture = av_frame_alloc();
247     if (!s->last_picture)
248         return AVERROR(ENOMEM);
249
250     ff_pngdsp_init(&s->dsp);
251
252     return 0;
253 }
254
255 static void lscr_decode_flush(AVCodecContext *avctx)
256 {
257     LSCRContext *s = avctx->priv_data;
258     av_frame_unref(s->last_picture);
259 }
260
261 AVCodec ff_lscr_decoder = {
262     .name           = "lscr",
263     .long_name      = NULL_IF_CONFIG_SMALL("LEAD Screen Capture"),
264     .type           = AVMEDIA_TYPE_VIDEO,
265     .id             = AV_CODEC_ID_LSCR,
266     .priv_data_size = sizeof(LSCRContext),
267     .init           = lscr_decode_init,
268     .close          = lscr_decode_close,
269     .decode         = decode_frame_lscr,
270     .flush          = lscr_decode_flush,
271     .capabilities   = AV_CODEC_CAP_DR1,
272     .caps_internal  = FF_CODEC_CAP_INIT_THREADSAFE,
273 };