FFmpeg
mss1.c
Go to the documentation of this file.
1 /*
2  * Microsoft Screen 1 (aka Windows Media Video V7 Screen) decoder
3  * Copyright (c) 2012 Konstantin Shishkov
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 /**
23  * @file
24  * Microsoft Screen 1 (aka Windows Media Video V7 Screen) decoder
25  */
26 
27 #include "avcodec.h"
28 #include "internal.h"
29 #include "mss12.h"
30 
31 typedef struct MSS1Context {
35 } MSS1Context;
36 
38 {
39  for (;;) {
40  if (c->high >= 0x8000) {
41  if (c->low < 0x8000) {
42  if (c->low >= 0x4000 && c->high < 0xC000) {
43  c->value -= 0x4000;
44  c->low -= 0x4000;
45  c->high -= 0x4000;
46  } else {
47  return;
48  }
49  } else {
50  c->value -= 0x8000;
51  c->low -= 0x8000;
52  c->high -= 0x8000;
53  }
54  }
55  c->value <<= 1;
56  c->low <<= 1;
57  c->high <<= 1;
58  c->high |= 1;
59  if (get_bits_left(c->gbc.gb) < 1)
60  c->overread++;
61  c->value |= get_bits1(c->gbc.gb);
62  }
63 }
64 
65 ARITH_GET_BIT(arith)
66 
67 static int arith_get_bits(ArithCoder *c, int bits)
68 {
69  int range = c->high - c->low + 1;
70  int val = (((c->value - c->low + 1) << bits) - 1) / range;
71  int prob = range * val;
72 
73  c->high = ((prob + range) >> bits) + c->low - 1;
74  c->low += prob >> bits;
75 
77 
78  return val;
79 }
80 
81 static int arith_get_number(ArithCoder *c, int mod_val)
82 {
83  int range = c->high - c->low + 1;
84  int val = ((c->value - c->low + 1) * mod_val - 1) / range;
85  int prob = range * val;
86 
87  c->high = (prob + range) / mod_val + c->low - 1;
88  c->low += prob / mod_val;
89 
90  arith_normalise(c);
91 
92  return val;
93 }
94 
95 static int arith_get_prob(ArithCoder *c, int16_t *probs)
96 {
97  int range = c->high - c->low + 1;
98  int val = ((c->value - c->low + 1) * probs[0] - 1) / range;
99  int sym = 1;
100 
101  while (probs[sym] > val)
102  sym++;
103 
104  c->high = range * probs[sym - 1] / probs[0] + c->low - 1;
105  c->low += range * probs[sym] / probs[0];
106 
107  return sym;
108 }
109 
110 ARITH_GET_MODEL_SYM(arith)
111 
113 {
114  c->low = 0;
115  c->high = 0xFFFF;
116  c->value = get_bits(gb, 16);
117  c->overread = 0;
118  c->gbc.gb = gb;
119  c->get_model_sym = arith_get_model_sym;
120  c->get_number = arith_get_number;
121 }
122 
123 static int decode_pal(MSS12Context *ctx, ArithCoder *acoder)
124 {
125  int i, ncol, r, g, b;
126  uint32_t *pal = ctx->pal + 256 - ctx->free_colours;
127 
128  if (!ctx->free_colours)
129  return 0;
130 
131  ncol = arith_get_number(acoder, ctx->free_colours + 1);
132  for (i = 0; i < ncol; i++) {
133  r = arith_get_bits(acoder, 8);
134  g = arith_get_bits(acoder, 8);
135  b = arith_get_bits(acoder, 8);
136  *pal++ = (0xFFU << 24) | (r << 16) | (g << 8) | b;
137  }
138 
139  return !!ncol;
140 }
141 
142 static int mss1_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
143  AVPacket *avpkt)
144 {
145  MSS1Context *ctx = avctx->priv_data;
146  MSS12Context *c = &ctx->ctx;
147  GetBitContext gb;
148  ArithCoder acoder;
149  int pal_changed = 0;
150  int ret;
151 
152  if ((ret = init_get_bits8(&gb, avpkt->data, avpkt->size)) < 0)
153  return ret;
154 
155  arith_init(&acoder, &gb);
156 
157  if ((ret = ff_reget_buffer(avctx, ctx->pic)) < 0)
158  return ret;
159 
160  c->pal_pic = ctx->pic->data[0] + ctx->pic->linesize[0] * (avctx->height - 1);
161  c->pal_stride = -ctx->pic->linesize[0];
162  c->keyframe = !arith_get_bit(&acoder);
163  if (c->keyframe) {
164  c->corrupted = 0;
166  pal_changed = decode_pal(c, &acoder);
167  ctx->pic->key_frame = 1;
169  } else {
170  if (c->corrupted)
171  return AVERROR_INVALIDDATA;
172  ctx->pic->key_frame = 0;
174  }
175  c->corrupted = ff_mss12_decode_rect(&ctx->sc, &acoder, 0, 0,
176  avctx->width, avctx->height);
177  if (c->corrupted)
178  return AVERROR_INVALIDDATA;
179  memcpy(ctx->pic->data[1], c->pal, AVPALETTE_SIZE);
180  ctx->pic->palette_has_changed = pal_changed;
181 
182  if ((ret = av_frame_ref(data, ctx->pic)) < 0)
183  return ret;
184 
185  *got_frame = 1;
186 
187  /* always report that the buffer was completely consumed */
188  return avpkt->size;
189 }
190 
192 {
193  MSS1Context * const c = avctx->priv_data;
194  int ret;
195 
196  c->ctx.avctx = avctx;
197 
198  c->pic = av_frame_alloc();
199  if (!c->pic)
200  return AVERROR(ENOMEM);
201 
202  ret = ff_mss12_decode_init(&c->ctx, 0, &c->sc, NULL);
203  if (ret < 0)
204  av_frame_free(&c->pic);
205 
206  avctx->pix_fmt = AV_PIX_FMT_PAL8;
207 
208  return ret;
209 }
210 
212 {
213  MSS1Context * const ctx = avctx->priv_data;
214 
215  av_frame_free(&ctx->pic);
216  ff_mss12_decode_end(&ctx->ctx);
217 
218  return 0;
219 }
220 
222  .name = "mss1",
223  .long_name = NULL_IF_CONFIG_SMALL("MS Screen 1"),
224  .type = AVMEDIA_TYPE_VIDEO,
225  .id = AV_CODEC_ID_MSS1,
226  .priv_data_size = sizeof(MSS1Context),
228  .close = mss1_decode_end,
230  .capabilities = AV_CODEC_CAP_DR1,
231 };
static int decode_pal(MSS12Context *ctx, ArithCoder *acoder)
Definition: mss1.c:123
#define NULL
Definition: coverity.c:32
const char const char void * val
Definition: avisynth_c.h:863
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
This structure describes decoded (raw) audio or video data.
Definition: frame.h:295
int overread
Definition: mss12.h:50
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:100
int high
Definition: mss12.h:49
av_cold int ff_mss12_decode_init(MSS12Context *c, int version, SliceContext *sc1, SliceContext *sc2)
Definition: mss12.c:577
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:379
const char * g
Definition: vf_curves.c:115
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
int value
Definition: mss12.h:49
int corrupted
Definition: mss12.h:91
int size
Definition: avcodec.h:1478
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1775
static av_cold int mss1_decode_end(AVCodecContext *avctx)
Definition: mss1.c:211
AVCodec.
Definition: avcodec.h:3481
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:42
int ff_mss12_decode_rect(SliceContext *sc, ArithCoder *acoder, int x, int y, int width, int height)
Definition: mss12.c:539
int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame)
Identical in function to av_frame_make_writable(), except it uses ff_get_buffer() to allocate the buf...
Definition: decode.c:2010
uint32_t pal[256]
Definition: mss12.h:79
#define av_cold
Definition: attributes.h:82
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:189
8 bits with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:77
int keyframe
Definition: mss12.h:89
#define AVPALETTE_SIZE
Definition: pixfmt.h:32
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
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:443
static int arith_get_bits(ArithCoder *c, int bits)
Definition: mss1.c:67
union ArithCoder::@119 gbc
uint8_t * data
Definition: avcodec.h:1477
static void arith_normalise(ArithCoder *c)
Definition: mss1.c:37
#define prob(name, subs,...)
Definition: cbs_vp9.c:374
#define U(x)
Definition: vp56_arith.h:37
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:849
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:259
static int arith_get_prob(ArithCoder *c, int16_t *probs)
Definition: mss1.c:95
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:202
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
const char * r
Definition: vf_curves.c:114
#define ARITH_GET_MODEL_SYM(prefix)
Definition: mss12.h:120
const char * name
Name of the codec implementation.
Definition: avcodec.h:3488
uint8_t bits
Definition: vp3data.h:202
AVCodecContext * avctx
Definition: mss12.h:78
#define b
Definition: input.c:41
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:378
int width
picture width / height.
Definition: avcodec.h:1738
ptrdiff_t pal_stride
Definition: mss12.h:82
av_cold int ff_mss12_decode_end(MSS12Context *c)
Definition: mss12.c:689
Libavcodec external API header.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:326
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:677
main external API structure.
Definition: avcodec.h:1565
static int mss1_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: mss1.c:142
AVFrame * pic
Definition: mss1.c:33
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:498
int palette_has_changed
Tell user application that palette has changed from previous frame.
Definition: frame.h:452
SliceContext sc
Definition: mss1.c:34
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:309
common internal api header.
void ff_mss12_slicecontext_reset(SliceContext *sc)
Definition: mss12.c:435
static int arith_get_number(ArithCoder *c, int mod_val)
Definition: mss1.c:81
MSS12Context ctx
Definition: mss1.c:32
int free_colours
Definition: mss12.h:88
void * priv_data
Definition: avcodec.h:1592
static av_cold int mss1_decode_init(AVCodecContext *avctx)
Definition: mss1.c:191
int low
Definition: mss12.h:49
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:373
#define ARITH_GET_BIT(prefix)
Definition: mss12.h:104
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
GetBitContext * gb
Definition: mss12.h:53
AVCodec ff_mss1_decoder
Definition: mss1.c:221
This structure stores compressed data.
Definition: avcodec.h:1454
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:981
uint8_t * pal_pic
Definition: mss12.h:80
Predicted.
Definition: avutil.h:275
Common header for Microsoft Screen 1 and 2.
static void arith_init(ArithCoder *c, GetBitContext *gb)
Definition: mss1.c:112