FFmpeg
Loading...
Searching...
No Matches
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 "libavutil/mem.h"
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
40
42 int width, int height, int stride, int pb_linesize, int gbp_linesize)
43{
44 int intra = 1, w = 0;
45
46 bytestream2_seek_p(pb, (height - 1) * pb_linesize, SEEK_SET);
47
48 while (bytestream2_get_bytes_left(gb) > 0) {
49 uint32_t fill = bytestream2_get_le24(gb);
50 unsigned run = bytestream2_get_byte(gb);
51
52 if (run == 0) {
53 run = bytestream2_get_le32(gb);
54
55 if (bytestream2_tell_p(pb) + width - w < run)
57
58 for (int j = 0; j < run; j++, w++) {
59 if (w == width) {
60 w = 0;
61 bytestream2_seek_p(pb, -(pb_linesize + stride), SEEK_CUR);
62 }
63 bytestream2_put_le24(pb, fill);
64 }
65 } else if (run == 255) {
66 int pos = bytestream2_tell_p(pb);
67
68 if (!gbp)
70
71 bytestream2_seek(gbp, pos, SEEK_SET);
72
73 if (pos + width - w < fill)
75
76 for (int j = 0; j < fill; j++, w++) {
77 if (w == width) {
78 w = 0;
79 bytestream2_seek_p(pb, -(pb_linesize + stride), SEEK_CUR);
80 bytestream2_seek(gbp, -(gbp_linesize + stride), SEEK_CUR);
81 }
82 bytestream2_put_le24(pb, bytestream2_get_le24(gbp));
83 }
84
85 intra = 0;
86 } else {
87 if (bytestream2_tell_p(pb) + width - w < run)
89
90 for (int j = 0; j < run; j++, w++) {
91 if (w == width) {
92 w = 0;
93 bytestream2_seek_p(pb, -(pb_linesize + stride), SEEK_CUR);
94 }
95 bytestream2_put_le24(pb, fill);
96 }
97 }
98 }
99
100 return intra;
101}
102
104 int *got_frame, AVPacket *avpkt)
105{
106 MWSCContext *s = avctx->priv_data;
107 z_stream *const zstream = &s->zstream.zstream;
108 const uint8_t *buf = avpkt->data;
109 int buf_size = avpkt->size;
111 GetByteContext gbp;
113 int ret;
114
115 ret = inflateReset(zstream);
116 if (ret != Z_OK) {
117 av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", ret);
118 return AVERROR_EXTERNAL;
119 }
120 zstream->next_in = buf;
121 zstream->avail_in = buf_size;
122 zstream->next_out = s->decomp_buf;
123 zstream->avail_out = s->decomp_size;
124 ret = inflate(zstream, Z_FINISH);
125 if (ret != Z_STREAM_END) {
126 av_log(avctx, AV_LOG_ERROR, "Inflate error: %d\n", ret);
127 return AVERROR_EXTERNAL;
128 }
129
130 if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
131 return ret;
132
133 bytestream2_init(&gb, s->decomp_buf, zstream->total_out);
134 if (s->prev_frame->data[0])
135 bytestream2_init(&gbp, s->prev_frame->data[0], avctx->height * s->prev_frame->linesize[0]);
136 bytestream2_init_writer(&pb, frame->data[0], avctx->height * frame->linesize[0]);
137
138 ret = rle_uncompress(&gb, &pb, s->prev_frame->data[0] ? &gbp : NULL,
139 avctx->width, avctx->height, avctx->width * 3,
140 frame->linesize[0], s->prev_frame->linesize[0]);
141 if (ret < 0)
142 return ret;
143 if (ret)
144 frame->flags |= AV_FRAME_FLAG_KEY;
145 else
146 frame->flags &= ~AV_FRAME_FLAG_KEY;
147
149
150 if ((ret = av_frame_replace(s->prev_frame, frame)) < 0)
151 return ret;
152
153 *got_frame = 1;
154
155 return avpkt->size;
156}
157
159{
160 MWSCContext *s = avctx->priv_data;
162
163 avctx->pix_fmt = AV_PIX_FMT_BGR24;
164
165 size = 32LL * avctx->height * avctx->width;
166 if (size >= INT32_MAX)
167 return AVERROR_INVALIDDATA;
168 s->decomp_size = size;
169 if (!(s->decomp_buf = av_malloc(s->decomp_size)))
170 return AVERROR(ENOMEM);
171
172 s->prev_frame = av_frame_alloc();
173 if (!s->prev_frame)
174 return AVERROR(ENOMEM);
175
176 return ff_inflate_init(&s->zstream, avctx);
177}
178
180{
181 MWSCContext *s = avctx->priv_data;
182
183 av_frame_free(&s->prev_frame);
184 av_freep(&s->decomp_buf);
185 s->decomp_size = 0;
186 ff_inflate_end(&s->zstream);
187
188 return 0;
189}
190
192 .p.name = "mwsc",
193 CODEC_LONG_NAME("MatchWare Screen Capture Codec"),
194 .p.type = AVMEDIA_TYPE_VIDEO,
195 .p.id = AV_CODEC_ID_MWSC,
196 .priv_data_size = sizeof(MWSCContext),
197 .init = decode_init,
200 .p.capabilities = AV_CODEC_CAP_DR1,
201 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
202};
const FFCodec ff_mwsc_decoder
Definition mwsc.c:191
static av_cold void close(AVCodecParserContext *s)
Definition apv_parser.c:197
Libavcodec external API header.
static av_always_inline int bytestream2_tell_p(const PutByteContext *p)
Definition bytestream.h:197
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 void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition bytestream.h:137
static av_always_inline int bytestream2_seek(GetByteContext *g, int offset, int whence)
Definition bytestream.h:212
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...
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
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
#define AV_GET_BUFFER_FLAG_REF
The decoder will keep a reference to the frame and may reuse it later.
Definition avcodec.h:415
@ AV_CODEC_ID_MWSC
Definition codec_id.h:284
#define AVERROR_EXTERNAL
Generic error in an external library.
Definition error.h:59
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR(e)
Definition error.h:45
#define AV_FRAME_FLAG_KEY
A flag to mark frames that are keyframes.
Definition frame.h:687
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:376
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:64
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition frame.c:52
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
@ AV_PICTURE_TYPE_I
Intra.
Definition avutil.h:278
@ AV_PICTURE_TYPE_P
Predicted.
Definition avutil.h:279
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
uint8_t w
Definition llvidencdsp.c:39
Memory handling functions.
static int decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *avpkt)
Definition mwsc.c:103
static av_cold int decode_close(AVCodecContext *avctx)
Definition mwsc.c:179
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:41
static av_cold int decode_init(AVCodecContext *avctx)
Definition mwsc.c:158
#define av_malloc(s)
Definition ops_static.c:52
@ AV_PIX_FMT_BGR24
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition pixfmt.h:76
unsigned int pos
Definition spdifenc.c:431
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
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
FFZStream zstream
Definition mwsc.c:38
uint8_t * decomp_buf
Definition mwsc.c:36
AVFrame * prev_frame
Definition mwsc.c:37
unsigned int decomp_size
Definition mwsc.c:35
uint8_t run
Definition svq3.c:207
#define stride
#define av_freep(p)
#define av_log(a,...)
#define height
Definition dsp.h:89
#define width
Definition dsp.h:89
int size
static void inflate(uint8_t *dst, const uint8_t *p1, int width, int threshold, const uint8_t *coordinates[], int coord, int maxc)
void ff_inflate_end(FFZStream *zstream)
Wrapper around inflateEnd().
int ff_inflate_init(FFZStream *zstream, void *logctx)
Wrapper around inflateInit().