FFmpeg
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
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 "codec_internal.h"
29 #include "decode.h"
30 #include "mss12.h"
31 
32 typedef struct MSS1Context {
36 } MSS1Context;
37 
39 {
40  for (;;) {
41  if (c->high >= 0x8000) {
42  if (c->low < 0x8000) {
43  if (c->low >= 0x4000 && c->high < 0xC000) {
44  c->value -= 0x4000;
45  c->low -= 0x4000;
46  c->high -= 0x4000;
47  } else {
48  return;
49  }
50  } else {
51  c->value -= 0x8000;
52  c->low -= 0x8000;
53  c->high -= 0x8000;
54  }
55  }
56  c->value <<= 1;
57  c->low <<= 1;
58  c->high <<= 1;
59  c->high |= 1;
60  if (get_bits_left(c->gbc.gb) < 1)
61  c->overread++;
62  c->value |= get_bits1(c->gbc.gb);
63  }
64 }
65 
66 ARITH_GET_BIT(arith)
67 
68 static int arith_get_bits(ArithCoder *c, int bits)
69 {
70  int range = c->high - c->low + 1;
71  int val = (((c->value - c->low + 1) << bits) - 1) / range;
72  int prob = range * val;
73 
74  c->high = ((prob + range) >> bits) + c->low - 1;
75  c->low += prob >> bits;
76 
78 
79  return val;
80 }
81 
82 static int arith_get_number(ArithCoder *c, int mod_val)
83 {
84  int range = c->high - c->low + 1;
85  int val = ((c->value - c->low + 1) * mod_val - 1) / range;
86  int prob = range * val;
87 
88  c->high = (prob + range) / mod_val + c->low - 1;
89  c->low += prob / mod_val;
90 
92 
93  return val;
94 }
95 
96 static int arith_get_prob(ArithCoder *c, int16_t *probs)
97 {
98  int range = c->high - c->low + 1;
99  int val = ((c->value - c->low + 1) * probs[0] - 1) / range;
100  int sym = 1;
101 
102  while (probs[sym] > val)
103  sym++;
104 
105  c->high = range * probs[sym - 1] / probs[0] + c->low - 1;
106  c->low += range * probs[sym] / probs[0];
107 
108  return sym;
109 }
110 
111 ARITH_GET_MODEL_SYM(arith)
112 
114 {
115  c->low = 0;
116  c->high = 0xFFFF;
117  c->value = get_bits(gb, 16);
118  c->overread = 0;
119  c->gbc.gb = gb;
120  c->get_model_sym = arith_get_model_sym;
121  c->get_number = arith_get_number;
122 }
123 
124 static void decode_pal(MSS12Context *ctx, ArithCoder *acoder)
125 {
126  int i, ncol, r, g, b;
127  uint32_t *pal = ctx->pal + 256 - ctx->free_colours;
128 
129  if (!ctx->free_colours)
130  return;
131 
132  ncol = arith_get_number(acoder, ctx->free_colours + 1);
133  for (i = 0; i < ncol; i++) {
134  r = arith_get_bits(acoder, 8);
135  g = arith_get_bits(acoder, 8);
136  b = arith_get_bits(acoder, 8);
137  *pal++ = (0xFFU << 24) | (r << 16) | (g << 8) | b;
138  }
139 }
140 
141 static int mss1_decode_frame(AVCodecContext *avctx, AVFrame *rframe,
142  int *got_frame, AVPacket *avpkt)
143 {
144  MSS1Context *ctx = avctx->priv_data;
145  MSS12Context *c = &ctx->ctx;
146  GetBitContext gb;
147  ArithCoder acoder;
148  int ret;
149 
150  if ((ret = init_get_bits8(&gb, avpkt->data, avpkt->size)) < 0)
151  return ret;
152 
153  arith_init(&acoder, &gb);
154 
155  if ((ret = ff_reget_buffer(avctx, ctx->pic, 0)) < 0)
156  return ret;
157 
158  c->pal_pic = ctx->pic->data[0] + ctx->pic->linesize[0] * (avctx->height - 1);
159  c->pal_stride = -ctx->pic->linesize[0];
160  c->keyframe = !arith_get_bit(&acoder);
161  if (c->keyframe) {
162  c->corrupted = 0;
164  decode_pal(c, &acoder);
165  ctx->pic->flags |= AV_FRAME_FLAG_KEY;
166  ctx->pic->pict_type = AV_PICTURE_TYPE_I;
167  } else {
168  if (c->corrupted)
169  return AVERROR_INVALIDDATA;
170  ctx->pic->flags &= ~AV_FRAME_FLAG_KEY;
171  ctx->pic->pict_type = AV_PICTURE_TYPE_P;
172  }
173  c->corrupted = ff_mss12_decode_rect(&ctx->sc, &acoder, 0, 0,
174  avctx->width, avctx->height);
175  if (c->corrupted)
176  return AVERROR_INVALIDDATA;
177  memcpy(ctx->pic->data[1], c->pal, AVPALETTE_SIZE);
178 
179  if ((ret = av_frame_ref(rframe, ctx->pic)) < 0)
180  return ret;
181 
182  *got_frame = 1;
183 
184  /* always report that the buffer was completely consumed */
185  return avpkt->size;
186 }
187 
189 {
190  MSS1Context * const c = avctx->priv_data;
191  int ret;
192 
193  c->ctx.avctx = avctx;
194 
195  c->pic = av_frame_alloc();
196  if (!c->pic)
197  return AVERROR(ENOMEM);
198 
199  ret = ff_mss12_decode_init(&c->ctx, 0, &c->sc, NULL);
200  if (ret < 0)
201  return ret;
202 
203  avctx->pix_fmt = AV_PIX_FMT_PAL8;
204 
205  return ret;
206 }
207 
209 {
210  MSS1Context * const ctx = avctx->priv_data;
211 
212  av_frame_free(&ctx->pic);
213  ff_mss12_decode_end(&ctx->ctx);
214 
215  return 0;
216 }
217 
219  .p.name = "mss1",
220  CODEC_LONG_NAME("MS Screen 1"),
221  .p.type = AVMEDIA_TYPE_VIDEO,
222  .p.id = AV_CODEC_ID_MSS1,
223  .priv_data_size = sizeof(MSS1Context),
227  .p.capabilities = AV_CODEC_CAP_DR1,
228  .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
229 };
MSS1Context
Definition: mss1.c:32
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
get_bits_left
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:678
r
const char * r
Definition: vf_curves.c:127
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
arith_get_number
static int arith_get_number(ArithCoder *c, int mod_val)
Definition: mss1.c:82
ARITH_GET_MODEL_SYM
#define ARITH_GET_MODEL_SYM(prefix)
Definition: mss12.h:120
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:63
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:410
AVPacket::data
uint8_t * data
Definition: packet.h:535
arith_get_prob
static int arith_get_prob(ArithCoder *c, int16_t *probs)
Definition: mss1.c:96
b
#define b
Definition: input.c:42
mss12.h
FFCodec
Definition: codec_internal.h:127
mss1_decode_init
static av_cold int mss1_decode_init(AVCodecContext *avctx)
Definition: mss1.c:188
mss1_decode_frame
static int mss1_decode_frame(AVCodecContext *avctx, AVFrame *rframe, int *got_frame, AVPacket *avpkt)
Definition: mss1.c:141
get_bits
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:318
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
GetBitContext
Definition: get_bits.h:108
val
static double val(void *priv, double ch)
Definition: aeval.c:77
av_frame_alloc
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:51
av_cold
#define av_cold
Definition: attributes.h:90
init_get_bits8
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:528
AV_FRAME_FLAG_KEY
#define AV_FRAME_FLAG_KEY
A flag to mark frames that are keyframes.
Definition: frame.h:625
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:341
AVFormatContext::flags
int flags
Flags modifying the (de)muxer behaviour.
Definition: avformat.h:1416
g
const char * g
Definition: vf_curves.c:128
bits
uint8_t bits
Definition: vp3data.h:128
ctx
AVFormatContext * ctx
Definition: movenc.c:49
decode.h
ArithCoder
Definition: dstdec.c:57
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:326
MSS1Context::ctx
MSS12Context ctx
Definition: mss1.c:33
ff_mss1_decoder
const FFCodec ff_mss1_decoder
Definition: mss1.c:218
NULL
#define NULL
Definition: coverity.c:32
AV_CODEC_ID_MSS1
@ AV_CODEC_ID_MSS1
Definition: codec_id.h:216
AVPALETTE_SIZE
#define AVPALETTE_SIZE
Definition: pixfmt.h:32
AV_PICTURE_TYPE_I
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:279
get_bits1
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:371
close
av_cold void CBS_FUNC() close(CodedBitstreamContext **ctx_ptr)
Close a context and free all internal state.
Definition: cbs.c:140
c
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
mss1_decode_end
static av_cold int mss1_decode_end(AVCodecContext *avctx)
Definition: mss1.c:208
SliceContext
Definition: mss12.h:70
ARITH_GET_BIT
#define ARITH_GET_BIT(prefix)
Definition: mss12.h:104
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts.c:368
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:536
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:276
codec_internal.h
range
enum AVColorRange range
Definition: mediacodec_wrapper.c:2594
MSS12Context
Definition: mss12.h:77
decode_pal
static void decode_pal(MSS12Context *ctx, ArithCoder *acoder)
Definition: mss1.c:124
arith_get_bits
static int arith_get_bits(ArithCoder *c, int bits)
Definition: mss1.c:68
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
arith_init
static void arith_init(ArithCoder *c, GetBitContext *gb)
Definition: mss1.c:113
ff_mss12_decode_end
av_cold int ff_mss12_decode_end(MSS12Context *c)
Definition: mss12.c:693
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:179
arith_normalise
static void arith_normalise(ArithCoder *c)
Definition: mss1.c:38
AVCodecContext::height
int height
Definition: avcodec.h:595
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:634
avcodec.h
AV_PIX_FMT_PAL8
@ AV_PIX_FMT_PAL8
8 bits with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:84
ff_reget_buffer
int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Identical in function to ff_get_buffer(), except it reuses the existing buffer if available.
Definition: decode.c:1726
ret
ret
Definition: filter_design.txt:187
prob
#define prob(name, subs,...)
Definition: cbs_vp9.c:325
U
#define U(x)
Definition: vpx_arith.h:37
AVCodecContext
main external API structure.
Definition: avcodec.h:431
MSS1Context::sc
SliceContext sc
Definition: mss1.c:35
ff_mss12_decode_rect
int ff_mss12_decode_rect(SliceContext *sc, ArithCoder *acoder, int x, int y, int width, int height)
Definition: mss12.c:543
AV_PICTURE_TYPE_P
@ AV_PICTURE_TYPE_P
Predicted.
Definition: avutil.h:280
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
MSS1Context::pic
AVFrame * pic
Definition: mss1.c:34
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:458
AVPacket
This structure stores compressed data.
Definition: packet.h:512
ff_mss12_slicecontext_reset
void ff_mss12_slicecontext_reset(SliceContext *sc)
Definition: mss12.c:437
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:595
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
ff_mss12_decode_init
av_cold int ff_mss12_decode_init(MSS12Context *c, int version, SliceContext *sc1, SliceContext *sc2)
Definition: mss12.c:581