FFmpeg  4.4.6
lscrdec.c
Go to the documentation of this file.
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 "internal.h"
32 #include "packet.h"
33 #include "png.h"
34 #include "pngdsp.h"
35 
36 typedef struct LSCRContext {
39 
44  int crow_size;
46  unsigned int last_row_size;
47 
51  int row_size;
52  int cur_h;
53  int y;
54 
55  z_stream zstream;
56 } LSCRContext;
57 
58 static void handle_row(LSCRContext *s)
59 {
60  uint8_t *ptr, *last_row;
61 
62  ptr = s->image_buf + s->image_linesize * s->y;
63  if (s->y == 0)
64  last_row = s->last_row;
65  else
66  last_row = ptr - s->image_linesize;
67 
68  ff_png_filter_row(&s->dsp, ptr, s->crow_buf[0], s->crow_buf + 1,
69  last_row, s->row_size, 3);
70 
71  s->y++;
72 }
73 
74 static int decode_idat(LSCRContext *s, int length)
75 {
76  int ret;
77  s->zstream.avail_in = FFMIN(length, bytestream2_get_bytes_left(&s->gb));
78  s->zstream.next_in = s->gb.buffer;
79 
80  if (length <= 0)
81  return AVERROR_INVALIDDATA;
82 
83  bytestream2_skip(&s->gb, length);
84 
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;
91  }
92  if (s->zstream.avail_out == 0) {
93  if (s->y < s->cur_h) {
94  handle_row(s);
95  }
96  s->zstream.avail_out = s->crow_size;
97  s->zstream.next_out = s->crow_buf;
98  }
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);
102  return 0;
103  }
104  }
105  return 0;
106 }
107 
109  void *data, int *got_frame,
110  AVPacket *avpkt)
111 {
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;
116 
117  if (avpkt->size < 2)
118  return AVERROR_INVALIDDATA;
119  if (avpkt->size == 2)
120  return 0;
121 
122  bytestream2_init(gb, avpkt->data, avpkt->size);
123 
124  nb_blocks = bytestream2_get_le16(gb);
125  if (bytestream2_get_bytes_left(gb) < 2 + nb_blocks * (12 + 8))
126  return AVERROR_INVALIDDATA;
127 
128  ret = ff_reget_buffer(avctx, frame,
129  nb_blocks ? 0 : FF_REGET_BUFFER_FLAG_READONLY);
130  if (ret < 0)
131  return ret;
132 
133  for (int b = 0; b < nb_blocks; b++) {
134  int x, y, x2, y2, w, h, left;
135  uint32_t csize, size;
136 
137  s->zstream.zalloc = ff_png_zalloc;
138  s->zstream.zfree = ff_png_zfree;
139  s->zstream.opaque = NULL;
140 
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;
144  goto end;
145  }
146 
147  bytestream2_seek(gb, 2 + b * 12, SEEK_SET);
148 
149  x = bytestream2_get_le16(gb);
150  y = bytestream2_get_le16(gb);
151  x2 = bytestream2_get_le16(gb);
152  y2 = bytestream2_get_le16(gb);
153  w = x2-x;
154  s->cur_h = h = y2-y;
155 
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;
159  goto end;
160  }
161 
162  size = bytestream2_get_le32(gb);
163 
164  frame->key_frame = (nb_blocks == 1) &&
165  (w == avctx->width) &&
166  (h == avctx->height) &&
167  (x == 0) && (y == 0);
168 
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;
173  goto end;
174  }
175 
176  offset += size;
177  left = size;
178 
179  s->y = 0;
180  s->row_size = w * 3;
181 
182  av_fast_padded_malloc(&s->buffer, &s->buffer_size, s->row_size + 16);
183  if (!s->buffer) {
184  ret = AVERROR(ENOMEM);
185  goto end;
186  }
187 
188  av_fast_padded_malloc(&s->last_row, &s->last_row_size, s->row_size);
189  if (!s->last_row) {
190  ret = AVERROR(ENOMEM);
191  goto end;
192  }
193 
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];
200 
201  while (left > 16) {
202  ret = decode_idat(s, csize);
203  if (ret < 0)
204  goto end;
205  left -= csize + 16;
206  if (left > 16) {
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;
211  goto end;
212  }
213  }
214  }
215 
216  inflateEnd(&s->zstream);
217  }
218 
220 
221  if ((ret = av_frame_ref(data, frame)) < 0)
222  return ret;
223 
224  *got_frame = 1;
225 end:
226  inflateEnd(&s->zstream);
227 
228  if (ret < 0)
229  return ret;
230  return avpkt->size;
231 }
232 
234 {
235  LSCRContext *s = avctx->priv_data;
236 
237  av_frame_free(&s->last_picture);
238  av_freep(&s->buffer);
239  av_freep(&s->last_row);
240 
241  return 0;
242 }
243 
245 {
246  LSCRContext *s = avctx->priv_data;
247 
248  avctx->color_range = AVCOL_RANGE_JPEG;
249  avctx->pix_fmt = AV_PIX_FMT_BGR24;
250 
251  s->avctx = avctx;
252  s->last_picture = av_frame_alloc();
253  if (!s->last_picture)
254  return AVERROR(ENOMEM);
255 
256  ff_pngdsp_init(&s->dsp);
257 
258  return 0;
259 }
260 
262 {
263  LSCRContext *s = avctx->priv_data;
264  av_frame_unref(s->last_picture);
265 }
266 
268  .name = "lscr",
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),
274  .close = lscr_decode_close,
277  .capabilities = AV_CODEC_CAP_DR1,
278  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
279 };
static void flush(AVCodecContext *avctx)
uint8_t
Libavcodec external API header.
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:31
static av_always_inline int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:158
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:137
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:168
static av_always_inline int bytestream2_seek(GetByteContext *g, int offset, int whence)
Definition: bytestream.h:212
#define s(width, name)
Definition: cbs_vp9.c:257
#define FFMIN(a, b)
Definition: common.h:105
#define MKTAG(a, b, c, d)
Definition: common.h:478
#define NULL
Definition: coverity.c:32
int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Identical in function to ff_get_buffer(), except it reuses the existing buffer if available.
Definition: decode.c:2007
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:71
static AVFrame * frame
error code definitions
reference-counted frame API
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
@ AV_CODEC_ID_LSCR
Definition: codec_id.h:294
void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
Same behaviour av_fast_malloc but the buffer has additional AV_INPUT_BUFFER_PADDING_SIZE at the end w...
Definition: utils.c:50
#define AVERROR_EXTERNAL
Generic error in an external library.
Definition: error.h:57
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
#define AVERROR(e)
Definition: error.h:43
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:553
int av_frame_ref(AVFrame *dst, const AVFrame *src)
Set up a new reference to the data described by the source frame.
Definition: frame.c:443
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:203
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:190
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:200
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:274
@ AV_PICTURE_TYPE_P
Predicted.
Definition: avutil.h:275
#define FF_REGET_BUFFER_FLAG_READONLY
the returned buffer does not need to be writable
Definition: internal.h:312
#define FF_CODEC_CAP_INIT_THREADSAFE
The codec does not modify any global variables in the init function, allowing to call the init functi...
Definition: internal.h:41
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
uint8_t w
Definition: llviddspenc.c:39
static int lscr_decode_close(AVCodecContext *avctx)
Definition: lscrdec.c:233
static void lscr_decode_flush(AVCodecContext *avctx)
Definition: lscrdec.c:261
AVCodec ff_lscr_decoder
Definition: lscrdec.c:267
static int decode_frame_lscr(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: lscrdec.c:108
static int decode_idat(LSCRContext *s, int length)
Definition: lscrdec.c:74
static void handle_row(LSCRContext *s)
Definition: lscrdec.c:58
static int lscr_decode_init(AVCodecContext *avctx)
Definition: lscrdec.c:244
const char data[16]
Definition: mxf.c:142
@ AVCOL_RANGE_JPEG
Full range content.
Definition: pixfmt.h:586
@ AV_PIX_FMT_BGR24
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:69
void * ff_png_zalloc(void *opaque, unsigned int items, unsigned int size)
Definition: png.c:39
void ff_png_zfree(void *opaque, void *ptr)
Definition: png.c:44
void ff_png_filter_row(PNGDSPContext *dsp, uint8_t *dst, int filter_type, uint8_t *src, uint8_t *last, int size, int bpp)
Definition: pngdec.c:264
av_cold void ff_pngdsp_init(PNGDSPContext *dsp)
Definition: pngdsp.c:43
main external API structure.
Definition: avcodec.h:536
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:746
int width
picture width / height.
Definition: avcodec.h:709
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: avcodec.h:1171
void * priv_data
Definition: avcodec.h:563
AVCodec.
Definition: codec.h:197
const char * name
Name of the codec implementation.
Definition: codec.h:204
This structure describes decoded (raw) audio or video data.
Definition: frame.h:318
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:332
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:396
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:349
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:401
This structure stores compressed data.
Definition: packet.h:346
int size
Definition: packet.h:370
uint8_t * data
Definition: packet.h:369
AVCodecContext * avctx
Definition: lscrdec.c:38
GetByteContext gb
Definition: lscrdec.c:48
int row_size
Definition: lscrdec.c:51
int buffer_size
Definition: lscrdec.c:42
PNGDSPContext dsp
Definition: lscrdec.c:37
int crow_size
Definition: lscrdec.c:44
z_stream zstream
Definition: lscrdec.c:55
unsigned int last_row_size
Definition: lscrdec.c:46
int image_linesize
Definition: lscrdec.c:50
AVFrame * last_picture
Definition: lscrdec.c:40
uint8_t * image_buf
Definition: lscrdec.c:49
uint8_t * crow_buf
Definition: lscrdec.c:43
int cur_h
Definition: lscrdec.c:52
uint8_t * buffer
Definition: lscrdec.c:41
uint8_t * last_row
Definition: lscrdec.c:45
#define av_freep(p)
#define av_log(a,...)
int size
const char * b
Definition: vf_curves.c:118
static void inflate(uint8_t *dst, const uint8_t *p1, int width, int threshold, const uint8_t *coordinates[], int coord, int maxc)
Definition: vf_neighbor.c:198
static const uint8_t offset[127][2]
Definition: vf_spp.c:107