FFmpeg
Loading...
Searching...
No Matches
mscc.c
Go to the documentation of this file.
1/*
2 * Mandsoft Screen Capture Codec decoder
3 *
4 * Copyright (c) 2017 Paul B Mahol
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23#include <stdio.h>
24#include <string.h>
25
26#include "libavutil/mem.h"
27#include "avcodec.h"
28#include "bytestream.h"
29#include "codec_internal.h"
30#include "decode.h"
31#include "zlib_wrapper.h"
32
33#include <zlib.h>
34
35typedef struct MSCCContext {
36 unsigned bpp;
37 unsigned int decomp_size;
38 uint8_t *decomp_buf;
39 unsigned int uncomp_size;
40 uint8_t *uncomp_buf;
42
43 uint32_t pal[256];
45
47{
48 MSCCContext *s = avctx->priv_data;
49 unsigned x = 0, y = 0;
50
51 while (bytestream2_get_bytes_left(gb) > 0) {
52 uint32_t fill;
53 int j;
54 unsigned run = bytestream2_get_byte(gb);
55
56 if (run) {
57 if (bytestream2_get_bytes_left_p(pb) < run * s->bpp)
59
60 switch (avctx->bits_per_coded_sample) {
61 case 8:
62 fill = bytestream2_get_byte(gb);
63 break;
64 case 16:
65 fill = bytestream2_get_le16(gb);
66 break;
67 case 24:
68 fill = bytestream2_get_le24(gb);
69 break;
70 case 32:
71 fill = bytestream2_get_le32(gb);
72 break;
73 }
74
75 for (j = 0; j < run; j++) {
76 switch (avctx->bits_per_coded_sample) {
77 case 8:
78 bytestream2_put_byte(pb, fill);
79 break;
80 case 16:
81 bytestream2_put_le16(pb, fill);
82 break;
83 case 24:
84 bytestream2_put_le24(pb, fill);
85 break;
86 case 32:
87 bytestream2_put_le32(pb, fill);
88 break;
89 }
90 }
91 x += run;
92 } else {
93 unsigned copy = bytestream2_get_byte(gb);
94
95 if (copy == 0) {
96 x = 0;
97 y++;
98 bytestream2_seek_p(pb, y * avctx->width * s->bpp, SEEK_SET);
99 } else if (copy == 1) {
100 return 0;
101 } else if (copy == 2) {
102
103 x += bytestream2_get_byte(gb);
104 y += bytestream2_get_byte(gb);
105
106 bytestream2_seek_p(pb, y * avctx->width * s->bpp + x * s->bpp, SEEK_SET);
107 } else {
108 if (bytestream2_get_bytes_left_p(pb) < copy * s->bpp)
109 return AVERROR_INVALIDDATA;
110
111 for (j = 0; j < copy; j++) {
112 switch (avctx->bits_per_coded_sample) {
113 case 8:
114 bytestream2_put_byte(pb, bytestream2_get_byte(gb));
115 break;
116 case 16:
117 bytestream2_put_le16(pb, bytestream2_get_le16(gb));
118 break;
119 case 24:
120 bytestream2_put_le24(pb, bytestream2_get_le24(gb));
121 break;
122 case 32:
123 bytestream2_put_le32(pb, bytestream2_get_le32(gb));
124 break;
125 }
126 }
127
128 if (s->bpp == 1 && (copy & 1))
129 bytestream2_skip(gb, 1);
130 x += copy;
131 }
132 }
133 }
134
135 return AVERROR_INVALIDDATA;
136}
137
139 int *got_frame, AVPacket *avpkt)
140{
141 MSCCContext *s = avctx->priv_data;
142 z_stream *const zstream = &s->zstream.zstream;
143 const uint8_t *buf = avpkt->data;
144 int buf_size = avpkt->size;
147 int ret, j;
148
149 if (avpkt->size < 3)
150 return buf_size;
151
152 ret = inflateReset(zstream);
153 if (ret != Z_OK) {
154 av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", ret);
155 return AVERROR_UNKNOWN;
156 }
157 zstream->next_out = s->decomp_buf;
158 zstream->avail_out = s->decomp_size;
159 if (avctx->codec_id == AV_CODEC_ID_MSCC) {
160 const uint8_t start = avpkt->data[2] ^ avpkt->data[0];
161
162 zstream->next_in = &start;
163 zstream->avail_in = 1;
164 ret = inflate(zstream, Z_NO_FLUSH);
165 if (ret != Z_OK || zstream->avail_in != 0)
166 goto inflate_error;
167
168 buf += 3;
169 buf_size -= 3;
170 }
171 zstream->next_in = buf;
172 zstream->avail_in = buf_size;
173 ret = inflate(zstream, Z_FINISH);
174 if (ret != Z_STREAM_END) {
175inflate_error:
176 av_log(avctx, AV_LOG_ERROR, "Inflate error: %d\n", ret);
177 return AVERROR_UNKNOWN;
178 }
179 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
180 return ret;
181
182 if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
183 size_t size;
184 const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, &size);
185
186 if (pal && size == AVPALETTE_SIZE) {
187 for (j = 0; j < 256; j++)
188 s->pal[j] = 0xFF000000 | AV_RL32(pal + j * 4);
189 } else if (pal) {
190 av_log(avctx, AV_LOG_ERROR,
191 "Palette size %zu is wrong\n", size);
192 }
193 memcpy(frame->data[1], s->pal, AVPALETTE_SIZE);
194 }
195
196 bytestream2_init(&gb, s->decomp_buf, zstream->total_out);
197 bytestream2_init_writer(&pb, s->uncomp_buf, s->uncomp_size);
198
199 ret = rle_uncompress(avctx, &gb, &pb);
200 if (ret)
201 return ret;
202
203 for (j = 0; j < avctx->height; j++) {
204 memcpy(frame->data[0] + (avctx->height - j - 1) * frame->linesize[0],
205 s->uncomp_buf + s->bpp * j * avctx->width, s->bpp * avctx->width);
206 }
207
208 *got_frame = 1;
209
210 return avpkt->size;
211}
212
214{
215 MSCCContext *s = avctx->priv_data;
216 int stride;
217
218 switch (avctx->bits_per_coded_sample) {
219 case 8: avctx->pix_fmt = AV_PIX_FMT_PAL8; break;
220 case 16: avctx->pix_fmt = AV_PIX_FMT_RGB555; break;
221 case 24: avctx->pix_fmt = AV_PIX_FMT_BGR24; break;
222 case 32: avctx->pix_fmt = AV_PIX_FMT_BGRA; break;
223 default:
224 av_log(avctx, AV_LOG_ERROR, "Unsupported bitdepth %i\n", avctx->bits_per_coded_sample);
225 return AVERROR_INVALIDDATA;
226 }
227
228 s->bpp = avctx->bits_per_coded_sample >> 3;
229 stride = 4 * ((avctx->width * avctx->bits_per_coded_sample + 31) / 32);
230
231 s->decomp_size = 2 * avctx->height * stride;
232 if (!(s->decomp_buf = av_malloc(s->decomp_size)))
233 return AVERROR(ENOMEM);
234
235 s->uncomp_size = avctx->height * stride;
236 if (!(s->uncomp_buf = av_malloc(s->uncomp_size)))
237 return AVERROR(ENOMEM);
238
239 return ff_inflate_init(&s->zstream, avctx);
240}
241
243{
244 MSCCContext *s = avctx->priv_data;
245
246 av_freep(&s->decomp_buf);
247 s->decomp_size = 0;
248 av_freep(&s->uncomp_buf);
249 s->uncomp_size = 0;
250 ff_inflate_end(&s->zstream);
251
252 return 0;
253}
254
256 .p.name = "mscc",
257 CODEC_LONG_NAME("Mandsoft Screen Capture Codec"),
258 .p.type = AVMEDIA_TYPE_VIDEO,
259 .p.id = AV_CODEC_ID_MSCC,
260 .priv_data_size = sizeof(MSCCContext),
261 .init = decode_init,
264 .p.capabilities = AV_CODEC_CAP_DR1,
265 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
266};
267
269 .p.name = "srgc",
270 CODEC_LONG_NAME("Screen Recorder Gold Codec"),
271 .p.type = AVMEDIA_TYPE_VIDEO,
272 .p.id = AV_CODEC_ID_SRGC,
273 .priv_data_size = sizeof(MSCCContext),
274 .init = decode_init,
277 .p.capabilities = AV_CODEC_CAP_DR1,
278 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
279};
const FFCodec ff_mscc_decoder
Definition mscc.c:255
const FFCodec ff_srgc_decoder
Definition mscc.c:268
static av_cold void close(AVCodecParserContext *s)
Definition apv_parser.c:197
Libavcodec external API header.
static av_always_inline void bytestream2_init_writer(PutByteContext *p, uint8_t *buf, int buf_size)
Definition bytestream.h:147
static av_always_inline int bytestream2_get_bytes_left(const GetByteContext *g)
Definition bytestream.h:158
static av_always_inline int bytestream2_get_bytes_left_p(const PutByteContext *p)
Definition bytestream.h:163
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_p(PutByteContext *p, int offset, int whence)
Definition bytestream.h:236
#define s(width, name)
Definition cbs_vp9.c:198
#define FF_CODEC_DECODE_CB(func)
#define CODEC_LONG_NAME(str)
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition decode.c:1777
static AVFrame * frame
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition codec.h:49
@ AV_CODEC_ID_SRGC
Definition codec_id.h:278
@ AV_CODEC_ID_MSCC
Definition codec_id.h:277
@ AV_PKT_DATA_PALETTE
An AV_PKT_DATA_PALETTE side data packet contains exactly AVPALETTE_SIZE bytes worth of palette.
Definition packet.h:47
uint8_t * av_packet_get_side_data(const AVPacket *pkt, enum AVPacketSideDataType type, size_t *size)
Get side information from packet.
Definition packet.c:252
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
Definition error.h:73
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
#define AV_RL32(p)
static av_cold int decode_init(AVCodecContext *avctx)
Definition 4xm.c:998
static int decode_frame(AVCodecContext *avctx, AVFrame *picture, int *got_frame, AVPacket *avpkt)
Definition 4xm.c:837
static av_cold int decode_close(AVCodecContext *avctx)
Definition aacdec.c:1220
#define av_cold
Definition attributes.h:117
Memory handling functions.
static int rle_uncompress(AVCodecContext *avctx, GetByteContext *gb, PutByteContext *pb)
Definition mscc.c:46
static int decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *avpkt)
Definition mscc.c:138
static av_cold int decode_close(AVCodecContext *avctx)
Definition mscc.c:242
static av_cold int decode_init(AVCodecContext *avctx)
Definition mscc.c:213
#define av_malloc(s)
Definition ops_static.c:52
@ AV_PIX_FMT_BGRA
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
Definition pixfmt.h:102
@ AV_PIX_FMT_PAL8
8 bits with AV_PIX_FMT_RGB32 palette
Definition pixfmt.h:84
@ AV_PIX_FMT_BGR24
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition pixfmt.h:76
#define AVPALETTE_SIZE
Definition pixfmt.h:32
#define AV_PIX_FMT_RGB555
Definition pixfmt.h:533
main external API structure.
Definition avcodec.h:443
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition avcodec.h:643
int width
picture width / height.
Definition avcodec.h:604
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition avcodec.h:1564
enum AVCodecID codec_id
Definition avcodec.h:453
void * priv_data
Definition avcodec.h:470
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
This structure stores compressed data.
Definition packet.h:580
int size
Definition packet.h:604
uint8_t * data
Definition packet.h:603
uint32_t pal[256]
Definition mscc.c:43
unsigned int decomp_size
Definition mscc.c:37
unsigned int uncomp_size
Definition mscc.c:39
uint8_t * uncomp_buf
Definition mscc.c:40
FFZStream zstream
Definition mscc.c:41
unsigned bpp
Definition mscc.c:36
uint8_t * decomp_buf
Definition mscc.c:38
uint8_t run
Definition svq3.c:207
#define stride
#define av_freep(p)
#define av_log(a,...)
int size
static void inflate(uint8_t *dst, const uint8_t *p1, int width, int threshold, const uint8_t *coordinates[], int coord, int maxc)
static void copy(const float *p1, float *p2, const int length)
void ff_inflate_end(FFZStream *zstream)
Wrapper around inflateEnd().
int ff_inflate_init(FFZStream *zstream, void *logctx)
Wrapper around inflateInit().