FFmpeg
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 "avcodec.h"
27 #include "bytestream.h"
28 #include "codec_internal.h"
29 #include "decode.h"
30 #include "zlib_wrapper.h"
31 
32 #include <zlib.h>
33 
34 typedef struct MSCCContext {
35  unsigned bpp;
36  unsigned int decomp_size;
37  uint8_t *decomp_buf;
38  unsigned int uncomp_size;
39  uint8_t *uncomp_buf;
41 
42  uint32_t pal[256];
43 } MSCCContext;
44 
46 {
47  MSCCContext *s = avctx->priv_data;
48  unsigned x = 0, y = 0;
49 
50  while (bytestream2_get_bytes_left(gb) > 0) {
51  uint32_t fill;
52  int j;
53  unsigned run = bytestream2_get_byte(gb);
54 
55  if (run) {
56  if (bytestream2_get_bytes_left_p(pb) < run * s->bpp)
57  return AVERROR_INVALIDDATA;
58 
59  switch (avctx->bits_per_coded_sample) {
60  case 8:
61  fill = bytestream2_get_byte(gb);
62  break;
63  case 16:
64  fill = bytestream2_get_le16(gb);
65  break;
66  case 24:
67  fill = bytestream2_get_le24(gb);
68  break;
69  case 32:
70  fill = bytestream2_get_le32(gb);
71  break;
72  }
73 
74  for (j = 0; j < run; j++) {
75  switch (avctx->bits_per_coded_sample) {
76  case 8:
77  bytestream2_put_byte(pb, fill);
78  break;
79  case 16:
80  bytestream2_put_le16(pb, fill);
81  break;
82  case 24:
83  bytestream2_put_le24(pb, fill);
84  break;
85  case 32:
86  bytestream2_put_le32(pb, fill);
87  break;
88  }
89  }
90  x += run;
91  } else {
92  unsigned copy = bytestream2_get_byte(gb);
93 
94  if (copy == 0) {
95  x = 0;
96  y++;
97  bytestream2_seek_p(pb, y * avctx->width * s->bpp, SEEK_SET);
98  } else if (copy == 1) {
99  return 0;
100  } else if (copy == 2) {
101 
102  x += bytestream2_get_byte(gb);
103  y += bytestream2_get_byte(gb);
104 
105  bytestream2_seek_p(pb, y * avctx->width * s->bpp + x * s->bpp, SEEK_SET);
106  } else {
107  if (bytestream2_get_bytes_left_p(pb) < copy * s->bpp)
108  return AVERROR_INVALIDDATA;
109 
110  for (j = 0; j < copy; j++) {
111  switch (avctx->bits_per_coded_sample) {
112  case 8:
113  bytestream2_put_byte(pb, bytestream2_get_byte(gb));
114  break;
115  case 16:
116  bytestream2_put_le16(pb, bytestream2_get_le16(gb));
117  break;
118  case 24:
119  bytestream2_put_le24(pb, bytestream2_get_le24(gb));
120  break;
121  case 32:
122  bytestream2_put_le32(pb, bytestream2_get_le32(gb));
123  break;
124  }
125  }
126 
127  if (s->bpp == 1 && (copy & 1))
128  bytestream2_skip(gb, 1);
129  x += copy;
130  }
131  }
132  }
133 
134  return AVERROR_INVALIDDATA;
135 }
136 
138  int *got_frame, AVPacket *avpkt)
139 {
140  MSCCContext *s = avctx->priv_data;
141  z_stream *const zstream = &s->zstream.zstream;
142  const uint8_t *buf = avpkt->data;
143  int buf_size = avpkt->size;
144  GetByteContext gb;
145  PutByteContext pb;
146  int ret, j;
147 
148  if (avpkt->size < 3)
149  return buf_size;
150 
151  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
152  return ret;
153 
154  if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
155  size_t size;
156  const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, &size);
157 
158  if (pal && size == AVPALETTE_SIZE) {
159 #if FF_API_PALETTE_HAS_CHANGED
163 #endif
164  for (j = 0; j < 256; j++)
165  s->pal[j] = 0xFF000000 | AV_RL32(pal + j * 4);
166  } else if (pal) {
167  av_log(avctx, AV_LOG_ERROR,
168  "Palette size %"SIZE_SPECIFIER" is wrong\n", size);
169  }
170  memcpy(frame->data[1], s->pal, AVPALETTE_SIZE);
171  }
172 
173  ret = inflateReset(zstream);
174  if (ret != Z_OK) {
175  av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", ret);
176  return AVERROR_UNKNOWN;
177  }
178  zstream->next_out = s->decomp_buf;
179  zstream->avail_out = s->decomp_size;
180  if (avctx->codec_id == AV_CODEC_ID_MSCC) {
181  const uint8_t start = avpkt->data[2] ^ avpkt->data[0];
182 
183  zstream->next_in = &start;
184  zstream->avail_in = 1;
185  ret = inflate(zstream, Z_NO_FLUSH);
186  if (ret != Z_OK || zstream->avail_in != 0)
187  goto inflate_error;
188 
189  buf += 3;
190  buf_size -= 3;
191  }
192  zstream->next_in = buf;
193  zstream->avail_in = buf_size;
194  ret = inflate(zstream, Z_FINISH);
195  if (ret != Z_STREAM_END) {
196 inflate_error:
197  av_log(avctx, AV_LOG_ERROR, "Inflate error: %d\n", ret);
198  return AVERROR_UNKNOWN;
199  }
200 
201  bytestream2_init(&gb, s->decomp_buf, zstream->total_out);
202  bytestream2_init_writer(&pb, s->uncomp_buf, s->uncomp_size);
203 
204  ret = rle_uncompress(avctx, &gb, &pb);
205  if (ret)
206  return ret;
207 
208  for (j = 0; j < avctx->height; j++) {
209  memcpy(frame->data[0] + (avctx->height - j - 1) * frame->linesize[0],
210  s->uncomp_buf + s->bpp * j * avctx->width, s->bpp * avctx->width);
211  }
212 
215 
216  *got_frame = 1;
217 
218  return avpkt->size;
219 }
220 
222 {
223  MSCCContext *s = avctx->priv_data;
224  int stride;
225 
226  switch (avctx->bits_per_coded_sample) {
227  case 8: avctx->pix_fmt = AV_PIX_FMT_PAL8; break;
228  case 16: avctx->pix_fmt = AV_PIX_FMT_RGB555; break;
229  case 24: avctx->pix_fmt = AV_PIX_FMT_BGR24; break;
230  case 32: avctx->pix_fmt = AV_PIX_FMT_BGRA; break;
231  default:
232  av_log(avctx, AV_LOG_ERROR, "Unsupported bitdepth %i\n", avctx->bits_per_coded_sample);
233  return AVERROR_INVALIDDATA;
234  }
235 
236  s->bpp = avctx->bits_per_coded_sample >> 3;
237  stride = 4 * ((avctx->width * avctx->bits_per_coded_sample + 31) / 32);
238 
239  s->decomp_size = 2 * avctx->height * stride;
240  if (!(s->decomp_buf = av_malloc(s->decomp_size)))
241  return AVERROR(ENOMEM);
242 
243  s->uncomp_size = avctx->height * stride;
244  if (!(s->uncomp_buf = av_malloc(s->uncomp_size)))
245  return AVERROR(ENOMEM);
246 
247  return ff_inflate_init(&s->zstream, avctx);
248 }
249 
251 {
252  MSCCContext *s = avctx->priv_data;
253 
254  av_freep(&s->decomp_buf);
255  s->decomp_size = 0;
256  av_freep(&s->uncomp_buf);
257  s->uncomp_size = 0;
258  ff_inflate_end(&s->zstream);
259 
260  return 0;
261 }
262 
264  .p.name = "mscc",
265  CODEC_LONG_NAME("Mandsoft Screen Capture Codec"),
266  .p.type = AVMEDIA_TYPE_VIDEO,
267  .p.id = AV_CODEC_ID_MSCC,
268  .priv_data_size = sizeof(MSCCContext),
269  .init = decode_init,
270  .close = decode_close,
272  .p.capabilities = AV_CODEC_CAP_DR1,
273  .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
274 };
275 
277  .p.name = "srgc",
278  CODEC_LONG_NAME("Screen Recorder Gold Codec"),
279  .p.type = AVMEDIA_TYPE_VIDEO,
280  .p.id = AV_CODEC_ID_SRGC,
281  .priv_data_size = sizeof(MSCCContext),
282  .init = decode_init,
283  .close = decode_close,
285  .p.capabilities = AV_CODEC_CAP_DR1,
286  .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
287 };
FF_ENABLE_DEPRECATION_WARNINGS
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:73
AV_CODEC_ID_MSCC
@ AV_CODEC_ID_MSCC
Definition: codec_id.h:282
FF_CODEC_CAP_INIT_CLEANUP
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Definition: codec_internal.h:42
AVFrame::palette_has_changed
attribute_deprecated int palette_has_changed
Tell user application that palette has changed from previous frame.
Definition: frame.h:537
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
MSCCContext::uncomp_buf
uint8_t * uncomp_buf
Definition: mscc.c:39
GetByteContext
Definition: bytestream.h:33
AV_CODEC_ID_SRGC
@ AV_CODEC_ID_SRGC
Definition: codec_id.h:283
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:344
AVPacket::data
uint8_t * data
Definition: packet.h:522
FFCodec
Definition: codec_internal.h:127
AV_PIX_FMT_BGR24
@ AV_PIX_FMT_BGR24
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:76
AV_PIX_FMT_BGRA
@ AV_PIX_FMT_BGRA
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
Definition: pixfmt.h:102
AVFrame::flags
int flags
Frame flags, a combination of AV_FRAME_FLAGS.
Definition: frame.h:616
AVERROR_UNKNOWN
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
Definition: error.h:73
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:365
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:30
bytestream2_skip
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:168
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
AV_PKT_DATA_PALETTE
@ AV_PKT_DATA_PALETTE
An AV_PKT_DATA_PALETTE side data packet contains exactly AVPALETTE_SIZE bytes worth of palette.
Definition: packet.h:47
decode_frame
static int decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *avpkt)
Definition: mscc.c:137
inflate
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:194
ff_srgc_decoder
const FFCodec ff_srgc_decoder
Definition: mscc.c:276
ff_mscc_decoder
const FFCodec ff_mscc_decoder
Definition: mscc.c:263
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
bytestream2_get_bytes_left_p
static av_always_inline int bytestream2_get_bytes_left_p(PutByteContext *p)
Definition: bytestream.h:163
zlib_wrapper.h
av_cold
#define av_cold
Definition: attributes.h:90
AV_FRAME_FLAG_KEY
#define AV_FRAME_FLAG_KEY
A flag to mark frames that are keyframes.
Definition: frame.h:595
bytestream2_init_writer
static av_always_inline void bytestream2_init_writer(PutByteContext *p, uint8_t *buf, int buf_size)
Definition: bytestream.h:147
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:287
s
#define s(width, name)
Definition: cbs_vp9.c:198
MSCCContext
Definition: mscc.c:34
decode.h
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:272
frame
static AVFrame * frame
Definition: demux_decode.c:54
AVCodecContext::codec_id
enum AVCodecID codec_id
Definition: avcodec.h:455
run
uint8_t run
Definition: svq3.c:203
MSCCContext::bpp
unsigned bpp
Definition: mscc.c:35
AVPALETTE_SIZE
#define AVPALETTE_SIZE
Definition: pixfmt.h:32
AV_PICTURE_TYPE_I
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:279
MSCCContext::decomp_buf
uint8_t * decomp_buf
Definition: mscc.c:37
bytestream2_get_bytes_left
static av_always_inline int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:158
MSCCContext::pal
uint32_t pal[256]
Definition: mscc.c:42
PutByteContext
Definition: bytestream.h:37
AVFrame::pict_type
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:446
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1569
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts.c:365
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
AVPacket::size
int size
Definition: packet.h:523
copy
static void copy(const float *p1, float *p2, const int length)
Definition: vf_vaguedenoiser.c:185
rle_uncompress
static int rle_uncompress(AVCodecContext *avctx, GetByteContext *gb, PutByteContext *pb)
Definition: mscc.c:45
codec_internal.h
size
int size
Definition: twinvq_data.h:10344
MSCCContext::uncomp_size
unsigned int uncomp_size
Definition: mscc.c:38
MSCCContext::zstream
FFZStream zstream
Definition: mscc.c:40
AVCodecContext::bits_per_coded_sample
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:1567
MSCCContext::decomp_size
unsigned int decomp_size
Definition: mscc.c:36
decode_close
static av_cold int decode_close(AVCodecContext *avctx)
Definition: mscc.c:250
av_packet_get_side_data
uint8_t * av_packet_get_side_data(const AVPacket *pkt, enum AVPacketSideDataType type, size_t *size)
Get side information from packet.
Definition: avpacket.c:252
AV_PIX_FMT_RGB555
#define AV_PIX_FMT_RGB555
Definition: pixfmt.h:466
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:194
ff_inflate_end
void ff_inflate_end(FFZStream *zstream)
Wrapper around inflateEnd().
AVCodecContext::height
int height
Definition: avcodec.h:618
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:657
avcodec.h
stride
#define stride
Definition: h264pred_template.c:537
AV_PIX_FMT_PAL8
@ AV_PIX_FMT_PAL8
8 bits with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:84
ret
ret
Definition: filter_design.txt:187
AV_RL32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:92
SIZE_SPECIFIER
#define SIZE_SPECIFIER
Definition: internal.h:141
AVCodecContext
main external API structure.
Definition: avcodec.h:445
decode_init
static av_cold int decode_init(AVCodecContext *avctx)
Definition: mscc.c:221
bytestream2_seek_p
static av_always_inline int bytestream2_seek_p(PutByteContext *p, int offset, int whence)
Definition: bytestream.h:236
FF_DISABLE_DEPRECATION_WARNINGS
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:72
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
FFZStream
Definition: zlib_wrapper.h:27
AVPacket
This structure stores compressed data.
Definition: packet.h:499
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:472
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
ff_inflate_init
int ff_inflate_init(FFZStream *zstream, void *logctx)
Wrapper around inflateInit().
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:618
bytestream.h
bytestream2_init
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:137
AVFrame::linesize
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
Definition: frame.h:389
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61