FFmpeg
mwsc.c
Go to the documentation of this file.
1 /*
2  * MatchWare Screen Capture Codec decoder
3  *
4  * Copyright (c) 2018 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 
25 #include "avcodec.h"
26 #include "bytestream.h"
27 #include "codec_internal.h"
28 #include "decode.h"
29 #include "zlib_wrapper.h"
30 
31 #include <zlib.h>
32 
33 typedef struct MWSCContext {
34  unsigned int decomp_size;
35  uint8_t *decomp_buf;
38 } MWSCContext;
39 
41  int width, int height, int stride, int pb_linesize, int gbp_linesize)
42 {
43  int intra = 1, w = 0;
44 
45  bytestream2_seek_p(pb, (height - 1) * pb_linesize, SEEK_SET);
46 
47  while (bytestream2_get_bytes_left(gb) > 0) {
48  uint32_t fill = bytestream2_get_le24(gb);
49  unsigned run = bytestream2_get_byte(gb);
50 
51  if (run == 0) {
52  run = bytestream2_get_le32(gb);
53 
54  if (bytestream2_tell_p(pb) + width - w < run)
55  return AVERROR_INVALIDDATA;
56 
57  for (int j = 0; j < run; j++, w++) {
58  if (w == width) {
59  w = 0;
60  bytestream2_seek_p(pb, -(pb_linesize + stride), SEEK_CUR);
61  }
62  bytestream2_put_le24(pb, fill);
63  }
64  } else if (run == 255) {
65  int pos = bytestream2_tell_p(pb);
66 
67  bytestream2_seek(gbp, pos, SEEK_SET);
68 
69  if (pos + width - w < fill)
70  return AVERROR_INVALIDDATA;
71 
72  for (int j = 0; j < fill; j++, w++) {
73  if (w == width) {
74  w = 0;
75  bytestream2_seek_p(pb, -(pb_linesize + stride), SEEK_CUR);
76  bytestream2_seek(gbp, -(gbp_linesize + stride), SEEK_CUR);
77  }
78  bytestream2_put_le24(pb, bytestream2_get_le24(gbp));
79  }
80 
81  intra = 0;
82  } else {
83  if (bytestream2_tell_p(pb) + width - w < run)
84  return AVERROR_INVALIDDATA;
85 
86  for (int j = 0; j < run; j++, w++) {
87  if (w == width) {
88  w = 0;
89  bytestream2_seek_p(pb, -(pb_linesize + stride), SEEK_CUR);
90  }
91  bytestream2_put_le24(pb, fill);
92  }
93  }
94  }
95 
96  return intra;
97 }
98 
100  int *got_frame, AVPacket *avpkt)
101 {
102  MWSCContext *s = avctx->priv_data;
103  z_stream *const zstream = &s->zstream.zstream;
104  const uint8_t *buf = avpkt->data;
105  int buf_size = avpkt->size;
106  GetByteContext gb;
107  GetByteContext gbp;
108  PutByteContext pb;
109  int ret;
110 
111  ret = inflateReset(zstream);
112  if (ret != Z_OK) {
113  av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", ret);
114  return AVERROR_EXTERNAL;
115  }
116  zstream->next_in = buf;
117  zstream->avail_in = buf_size;
118  zstream->next_out = s->decomp_buf;
119  zstream->avail_out = s->decomp_size;
120  ret = inflate(zstream, Z_FINISH);
121  if (ret != Z_STREAM_END) {
122  av_log(avctx, AV_LOG_ERROR, "Inflate error: %d\n", ret);
123  return AVERROR_EXTERNAL;
124  }
125 
126  if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
127  return ret;
128 
129  bytestream2_init(&gb, s->decomp_buf, zstream->total_out);
130  bytestream2_init(&gbp, s->prev_frame->data[0], avctx->height * s->prev_frame->linesize[0]);
131  bytestream2_init_writer(&pb, frame->data[0], avctx->height * frame->linesize[0]);
132 
133  if (rle_uncompress(&gb, &pb, &gbp, avctx->width, avctx->height, avctx->width * 3,
134  frame->linesize[0], s->prev_frame->linesize[0]))
136  else
138 
140 
141  if ((ret = av_frame_replace(s->prev_frame, frame)) < 0)
142  return ret;
143 
144  *got_frame = 1;
145 
146  return avpkt->size;
147 }
148 
150 {
151  MWSCContext *s = avctx->priv_data;
152  int64_t size;
153 
154  avctx->pix_fmt = AV_PIX_FMT_BGR24;
155 
156  size = 32LL * avctx->height * avctx->width;
157  if (size >= INT32_MAX)
158  return AVERROR_INVALIDDATA;
159  s->decomp_size = size;
160  if (!(s->decomp_buf = av_malloc(s->decomp_size)))
161  return AVERROR(ENOMEM);
162 
163  s->prev_frame = av_frame_alloc();
164  if (!s->prev_frame)
165  return AVERROR(ENOMEM);
166 
167  return ff_inflate_init(&s->zstream, avctx);
168 }
169 
171 {
172  MWSCContext *s = avctx->priv_data;
173 
174  av_frame_free(&s->prev_frame);
175  av_freep(&s->decomp_buf);
176  s->decomp_size = 0;
177  ff_inflate_end(&s->zstream);
178 
179  return 0;
180 }
181 
183  .p.name = "mwsc",
184  CODEC_LONG_NAME("MatchWare Screen Capture Codec"),
185  .p.type = AVMEDIA_TYPE_VIDEO,
186  .p.id = AV_CODEC_ID_MWSC,
187  .priv_data_size = sizeof(MWSCContext),
188  .init = decode_init,
189  .close = decode_close,
191  .p.capabilities = AV_CODEC_CAP_DR1,
192  .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
193 };
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
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
GetByteContext
Definition: bytestream.h:33
int64_t
long long int64_t
Definition: coverity.c:34
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:130
bytestream2_seek
static av_always_inline int bytestream2_seek(GetByteContext *g, int offset, int whence)
Definition: bytestream.h:212
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:344
w
uint8_t w
Definition: llviddspenc.c:38
AV_CODEC_ID_MWSC
@ AV_CODEC_ID_MWSC
Definition: codec_id.h:289
AVPacket::data
uint8_t * data
Definition: packet.h:522
ff_mwsc_decoder
const FFCodec ff_mwsc_decoder
Definition: mwsc.c:182
bytestream2_tell_p
static av_always_inline int bytestream2_tell_p(PutByteContext *p)
Definition: bytestream.h:197
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
AVFrame::flags
int flags
Frame flags, a combination of AV_FRAME_FLAGS.
Definition: frame.h:616
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
MWSCContext
Definition: mwsc.c:33
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
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
MWSCContext::decomp_buf
uint8_t * decomp_buf
Definition: mwsc.c:35
av_frame_alloc
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:118
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
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
decode_frame
static int decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *avpkt)
Definition: mwsc.c:99
bytestream2_init_writer
static av_always_inline void bytestream2_init_writer(PutByteContext *p, uint8_t *buf, int buf_size)
Definition: bytestream.h:147
MWSCContext::decomp_size
unsigned int decomp_size
Definition: mwsc.c:34
width
#define width
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
AV_GET_BUFFER_FLAG_REF
#define AV_GET_BUFFER_FLAG_REF
The decoder will keep a reference to the frame and may reuse it later.
Definition: avcodec.h:425
decode.h
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:272
frame
static AVFrame * frame
Definition: demux_decode.c:54
run
uint8_t run
Definition: svq3.c:203
AV_PICTURE_TYPE_I
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:279
bytestream2_get_bytes_left
static av_always_inline int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:158
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
codec_internal.h
size
int size
Definition: twinvq_data.h:10344
MWSCContext::prev_frame
AVFrame * prev_frame
Definition: mwsc.c:36
height
#define height
decode_init
static av_cold int decode_init(AVCodecContext *avctx)
Definition: mwsc.c:149
AVERROR_EXTERNAL
#define AVERROR_EXTERNAL
Generic error in an external library.
Definition: error.h:59
rle_uncompress
static int rle_uncompress(GetByteContext *gb, PutByteContext *pb, GetByteContext *gbp, int width, int height, int stride, int pb_linesize, int gbp_linesize)
Definition: mwsc.c:40
MWSCContext::zstream
FFZStream zstream
Definition: mwsc.c:37
decode_close
static av_cold int decode_close(AVCodecContext *avctx)
Definition: mwsc.c:170
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
ret
ret
Definition: filter_design.txt:187
pos
unsigned int pos
Definition: spdifenc.c:413
av_frame_replace
int av_frame_replace(AVFrame *dst, const AVFrame *src)
Ensure the destination frame refers to the same data described by the source frame,...
Definition: frame.c:453
AVCodecContext
main external API structure.
Definition: avcodec.h:445
bytestream2_seek_p
static av_always_inline int bytestream2_seek_p(PutByteContext *p, int offset, int whence)
Definition: bytestream.h:236
AV_PICTURE_TYPE_P
@ AV_PICTURE_TYPE_P
Predicted.
Definition: avutil.h:280
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
FFZStream
Definition: zlib_wrapper.h:27
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:472
AVPacket
This structure stores compressed data.
Definition: packet.h:499
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