FFmpeg  4.3
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 #include <stdlib.h>
25 #include <string.h>
26 
27 #include "avcodec.h"
28 #include "bytestream.h"
29 #include "internal.h"
30 
31 #include <zlib.h>
32 
33 typedef struct MWSCContext {
34  unsigned int decomp_size;
36  z_stream zstream;
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  for (int j = 0; j < run; j++, w++) {
54  if (w == width) {
55  w = 0;
56  bytestream2_seek_p(pb, -(pb_linesize + stride), SEEK_CUR);
57  }
58  bytestream2_put_le24(pb, fill);
59  }
60  } else if (run == 255) {
61  int pos = bytestream2_tell_p(pb);
62 
63  bytestream2_seek(gbp, pos, SEEK_SET);
64  for (int j = 0; j < fill; j++, w++) {
65  if (w == width) {
66  w = 0;
67  bytestream2_seek_p(pb, -(pb_linesize + stride), SEEK_CUR);
68  bytestream2_seek(gbp, -(gbp_linesize + stride), SEEK_CUR);
69  }
70  bytestream2_put_le24(pb, bytestream2_get_le24(gbp));
71  }
72 
73  intra = 0;
74  } else {
75  for (int j = 0; j < run; j++, w++) {
76  if (w == width) {
77  w = 0;
78  bytestream2_seek_p(pb, -(pb_linesize + stride), SEEK_CUR);
79  }
80  bytestream2_put_le24(pb, fill);
81  }
82  }
83  }
84 
85  return intra;
86 }
87 
88 static int decode_frame(AVCodecContext *avctx,
89  void *data, int *got_frame,
90  AVPacket *avpkt)
91 {
92  MWSCContext *s = avctx->priv_data;
93  AVFrame *frame = data;
94  uint8_t *buf = avpkt->data;
95  int buf_size = avpkt->size;
96  GetByteContext gb;
97  GetByteContext gbp;
98  PutByteContext pb;
99  int ret;
100 
101  ret = inflateReset(&s->zstream);
102  if (ret != Z_OK) {
103  av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", ret);
104  return AVERROR_EXTERNAL;
105  }
106  s->zstream.next_in = buf;
107  s->zstream.avail_in = buf_size;
108  s->zstream.next_out = s->decomp_buf;
109  s->zstream.avail_out = s->decomp_size;
110  ret = inflate(&s->zstream, Z_FINISH);
111  if (ret != Z_STREAM_END) {
112  av_log(avctx, AV_LOG_ERROR, "Inflate error: %d\n", ret);
113  return AVERROR_EXTERNAL;
114  }
115 
116  if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
117  return ret;
118 
119  bytestream2_init(&gb, s->decomp_buf, s->zstream.total_out);
120  bytestream2_init(&gbp, s->prev_frame->data[0], avctx->height * s->prev_frame->linesize[0]);
121  bytestream2_init_writer(&pb, frame->data[0], avctx->height * frame->linesize[0]);
122 
123  frame->key_frame = rle_uncompress(&gb, &pb, &gbp, avctx->width, avctx->height, avctx->width * 3,
124  frame->linesize[0], s->prev_frame->linesize[0]);
125 
126  frame->pict_type = frame->key_frame ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
127 
128  av_frame_unref(s->prev_frame);
129  if ((ret = av_frame_ref(s->prev_frame, frame)) < 0)
130  return ret;
131 
132  *got_frame = 1;
133 
134  return avpkt->size;
135 }
136 
138 {
139  MWSCContext *s = avctx->priv_data;
140  int64_t size;
141  int zret;
142 
143  avctx->pix_fmt = AV_PIX_FMT_BGR24;
144 
145  size = 32LL * avctx->height * avctx->width;
146  if (size >= INT32_MAX)
147  return AVERROR_INVALIDDATA;
148  s->decomp_size = size;
149  if (!(s->decomp_buf = av_malloc(s->decomp_size)))
150  return AVERROR(ENOMEM);
151 
152  s->zstream.zalloc = Z_NULL;
153  s->zstream.zfree = Z_NULL;
154  s->zstream.opaque = Z_NULL;
155  zret = inflateInit(&s->zstream);
156  if (zret != Z_OK) {
157  av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
158  return AVERROR_EXTERNAL;
159  }
160 
161  s->prev_frame = av_frame_alloc();
162  if (!s->prev_frame)
163  return AVERROR(ENOMEM);
164 
165  return 0;
166 }
167 
169 {
170  MWSCContext *s = avctx->priv_data;
171 
172  av_frame_free(&s->prev_frame);
173  av_freep(&s->decomp_buf);
174  s->decomp_size = 0;
175  inflateEnd(&s->zstream);
176 
177  return 0;
178 }
179 
181  .name = "mwsc",
182  .long_name = NULL_IF_CONFIG_SMALL("MatchWare Screen Capture Codec"),
183  .type = AVMEDIA_TYPE_VIDEO,
184  .id = AV_CODEC_ID_MWSC,
185  .priv_data_size = sizeof(MWSCContext),
186  .init = decode_init,
187  .close = decode_close,
188  .decode = decode_frame,
189  .capabilities = AV_CODEC_CAP_DR1,
190  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
192 };
AVCodec
AVCodec.
Definition: codec.h:190
stride
int stride
Definition: mace.c:144
FF_CODEC_CAP_INIT_THREADSAFE
#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:40
init
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
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
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:203
bytestream2_seek
static av_always_inline int bytestream2_seek(GetByteContext *g, int offset, int whence)
Definition: bytestream.h:208
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:300
AV_CODEC_ID_MWSC
@ AV_CODEC_ID_MWSC
Definition: codec_id.h:283
internal.h
AVPacket::data
uint8_t * data
Definition: packet.h:355
bytestream2_tell_p
static av_always_inline int bytestream2_tell_p(PutByteContext *p)
Definition: bytestream.h:193
data
const char data[16]
Definition: mxf.c:91
AV_PIX_FMT_BGR24
@ AV_PIX_FMT_BGR24
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:69
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:31
MWSCContext
Definition: mwsc.c:33
decode_frame
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: mwsc.c:88
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:198
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:190
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
av_cold
#define av_cold
Definition: attributes.h:90
bytestream2_init_writer
static av_always_inline void bytestream2_init_writer(PutByteContext *p, uint8_t *buf, int buf_size)
Definition: bytestream.h:143
decode
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:71
MWSCContext::decomp_size
unsigned int decomp_size
Definition: mwsc.c:34
width
#define width
s
#define s(width, name)
Definition: cbs_vp9.c:257
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:509
run
uint8_t run
Definition: svq3.c:208
AV_PICTURE_TYPE_I
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:274
bytestream2_get_bytes_left
static av_always_inline int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:154
PutByteContext
Definition: bytestream.h:37
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1854
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:50
AVPacket::size
int size
Definition: packet.h:356
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:186
av_frame_ref
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:444
size
int size
Definition: twinvq_data.h:11134
MWSCContext::prev_frame
AVFrame * prev_frame
Definition: mwsc.c:37
height
#define height
decode_init
static av_cold int decode_init(AVCodecContext *avctx)
Definition: mwsc.c:137
AVERROR_EXTERNAL
#define AVERROR_EXTERNAL
Generic error in an external library.
Definition: error.h:57
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
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: internal.h:48
decode_close
static av_cold int decode_close(AVCodecContext *avctx)
Definition: mwsc.c:168
uint8_t
uint8_t
Definition: audio_convert.c:194
av_frame_unref
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:554
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:197
AVCodecContext::height
int height
Definition: avcodec.h:699
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:736
avcodec.h
ret
ret
Definition: filter_design.txt:187
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
w
FFmpeg Automated Testing Environment ************************************Introduction Using FATE from your FFmpeg source directory Submitting the results to the FFmpeg result aggregation server Uploading new samples to the fate suite FATE makefile targets and variables Makefile targets Makefile variables Examples Introduction **************FATE is an extended regression suite on the client side and a means for results aggregation and presentation on the server side The first part of this document explains how you can use FATE from your FFmpeg source directory to test your ffmpeg binary The second part describes how you can run FATE to submit the results to FFmpeg’s FATE server In any way you can have a look at the publicly viewable FATE results by visiting this as it can be seen if some test on some platform broke with their recent contribution This usually happens on the platforms the developers could not test on The second part of this document describes how you can run FATE to submit your results to FFmpeg’s FATE server If you want to submit your results be sure to check that your combination of OS and compiler is not already listed on the above mentioned website In the third part you can find a comprehensive listing of FATE makefile targets and variables Using FATE from your FFmpeg source directory **********************************************If you want to run FATE on your machine you need to have the samples in place You can get the samples via the build target fate rsync Use this command from the top level source this will cause FATE to fail NOTE To use a custom wrapper to run the pass ‘ target exec’ to ‘configure’ or set the TARGET_EXEC Make variable Submitting the results to the FFmpeg result aggregation server ****************************************************************To submit your results to the server you should run fate through the shell script ‘tests fate sh’ from the FFmpeg sources This script needs to be invoked with a configuration file as its first argument tests fate sh path to fate_config A configuration file template with comments describing the individual configuration variables can be found at ‘doc fate_config sh template’ Create a configuration that suits your based on the configuration template The ‘slot’ configuration variable can be any string that is not yet but it is suggested that you name it adhering to the following pattern ‘ARCH OS COMPILER COMPILER VERSION’ The configuration file itself will be sourced in a shell therefore all shell features may be used This enables you to setup the environment as you need it for your build For your first test runs the ‘fate_recv’ variable should be empty or commented out This will run everything as normal except that it will omit the submission of the results to the server The following files should be present in $workdir as specified in the configuration it may help to try out the ‘ssh’ command with one or more ‘ v’ options You should get detailed output concerning your SSH configuration and the authentication process The only thing left is to automate the execution of the fate sh script and the synchronisation of the samples directory Uploading new samples to the fate suite *****************************************If you need a sample uploaded send a mail to samples request This is for developers who have an account on the fate suite server If you upload new please make sure they are as small as space on each network bandwidth and so on benefit from smaller test cases Also keep in mind older checkouts use existing sample that means in practice generally do not remove or overwrite files as it likely would break older checkouts or releases Also all needed samples for a commit should be ideally before the push If you need an account for frequently uploading samples or you wish to help others by doing that send a mail to ffmpeg devel rsync vauL Duo ug o o w
Definition: fate.txt:150
ff_mwsc_decoder
AVCodec ff_mwsc_decoder
Definition: mwsc.c:180
pos
unsigned int pos
Definition: spdifenc.c:410
AVCodecContext
main external API structure.
Definition: avcodec.h:526
MWSCContext::zstream
z_stream zstream
Definition: mwsc.c:36
bytestream2_seek_p
static av_always_inline int bytestream2_seek_p(PutByteContext *p, int offset, int whence)
Definition: bytestream.h:232
AV_PICTURE_TYPE_P
@ AV_PICTURE_TYPE_P
Predicted.
Definition: avutil.h:275
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:553
AVPacket
This structure stores compressed data.
Definition: packet.h:332
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:699
bytestream.h
bytestream2_init
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:133
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59