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 Libav.
6  *
7  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
27 #include "avcodec.h"
28 #include "mss12.h"
29 
30 typedef struct MSS1Context {
34 } MSS1Context;
35 
36 static void arith_normalise(ArithCoder *c)
37 {
38  for (;;) {
39  if (c->high >= 0x8000) {
40  if (c->low < 0x8000) {
41  if (c->low >= 0x4000 && c->high < 0xC000) {
42  c->value -= 0x4000;
43  c->low -= 0x4000;
44  c->high -= 0x4000;
45  } else {
46  return;
47  }
48  } else {
49  c->value -= 0x8000;
50  c->low -= 0x8000;
51  c->high -= 0x8000;
52  }
53  }
54  c->value <<= 1;
55  c->low <<= 1;
56  c->high <<= 1;
57  c->high |= 1;
58  c->value |= get_bits1(c->gbc.gb);
59  }
60 }
61 
63 
64 static int arith_get_bits(ArithCoder *c, int bits)
65 {
66  int range = c->high - c->low + 1;
67  int val = (((c->value - c->low + 1) << bits) - 1) / range;
68  int prob = range * val;
69 
70  c->high = ((prob + range) >> bits) + c->low - 1;
71  c->low += prob >> bits;
72 
73  arith_normalise(c);
74 
75  return val;
76 }
77 
78 static int arith_get_number(ArithCoder *c, int mod_val)
79 {
80  int range = c->high - c->low + 1;
81  int val = ((c->value - c->low + 1) * mod_val - 1) / range;
82  int prob = range * val;
83 
84  c->high = (prob + range) / mod_val + c->low - 1;
85  c->low += prob / mod_val;
86 
87  arith_normalise(c);
88 
89  return val;
90 }
91 
92 static int arith_get_prob(ArithCoder *c, int16_t *probs)
93 {
94  int range = c->high - c->low + 1;
95  int val = ((c->value - c->low + 1) * probs[0] - 1) / range;
96  int sym = 1;
97 
98  while (probs[sym] > val)
99  sym++;
100 
101  c->high = range * probs[sym - 1] / probs[0] + c->low - 1;
102  c->low += range * probs[sym] / probs[0];
103 
104  return sym;
105 }
106 
108 
109 static void arith_init(ArithCoder *c, GetBitContext *gb)
110 {
111  c->low = 0;
112  c->high = 0xFFFF;
113  c->value = get_bits(gb, 16);
114  c->gbc.gb = gb;
115  c->get_model_sym = arith_get_model_sym;
116  c->get_number = arith_get_number;
117 }
118 
119 static int decode_pal(MSS12Context *ctx, ArithCoder *acoder)
120 {
121  int i, ncol, r, g, b;
122  uint32_t *pal = ctx->pal + 256 - ctx->free_colours;
123 
124  if (!ctx->free_colours)
125  return 0;
126 
127  ncol = arith_get_number(acoder, ctx->free_colours + 1);
128  for (i = 0; i < ncol; i++) {
129  r = arith_get_bits(acoder, 8);
130  g = arith_get_bits(acoder, 8);
131  b = arith_get_bits(acoder, 8);
132  *pal++ = (r << 16) | (g << 8) | b;
133  }
134 
135  return !!ncol;
136 }
137 
138 static int mss1_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
139  AVPacket *avpkt)
140 {
141  const uint8_t *buf = avpkt->data;
142  int buf_size = avpkt->size;
143  MSS1Context *ctx = avctx->priv_data;
144  MSS12Context *c = &ctx->ctx;
145  GetBitContext gb;
146  ArithCoder acoder;
147  int pal_changed = 0;
148  int ret;
149 
150  init_get_bits(&gb, buf, buf_size * 8);
151  arith_init(&acoder, &gb);
152 
153  ctx->pic.reference = 3;
156  if ((ret = avctx->reget_buffer(avctx, &ctx->pic)) < 0) {
157  av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
158  return ret;
159  }
160 
161  c->pal_pic = ctx->pic.data[0] + ctx->pic.linesize[0] * (avctx->height - 1);
162  c->pal_stride = -ctx->pic.linesize[0];
163  c->keyframe = !arith_get_bit(&acoder);
164  if (c->keyframe) {
165  c->corrupted = 0;
167  pal_changed = decode_pal(c, &acoder);
168  ctx->pic.key_frame = 1;
170  } else {
171  if (c->corrupted)
172  return AVERROR_INVALIDDATA;
173  ctx->pic.key_frame = 0;
175  }
176  c->corrupted = ff_mss12_decode_rect(&ctx->sc, &acoder, 0, 0,
177  avctx->width, avctx->height);
178  if (c->corrupted)
179  return AVERROR_INVALIDDATA;
180  memcpy(ctx->pic.data[1], c->pal, AVPALETTE_SIZE);
181  ctx->pic.palette_has_changed = pal_changed;
182 
183  *got_frame = 1;
184  *(AVFrame*)data = ctx->pic;
185 
186  /* always report that the buffer was completely consumed */
187  return buf_size;
188 }
189 
191 {
192  MSS1Context * const c = avctx->priv_data;
193  int ret;
194 
195  c->ctx.avctx = avctx;
196  avctx->coded_frame = &c->pic;
197 
198  ret = ff_mss12_decode_init(&c->ctx, 0, &c->sc, NULL);
199 
200  avctx->pix_fmt = AV_PIX_FMT_PAL8;
201 
202  return ret;
203 }
204 
206 {
207  MSS1Context * const ctx = avctx->priv_data;
208 
209  if (ctx->pic.data[0])
210  avctx->release_buffer(avctx, &ctx->pic);
211  ff_mss12_decode_end(&ctx->ctx);
212 
213  return 0;
214 }
215 
217  .name = "mss1",
218  .type = AVMEDIA_TYPE_VIDEO,
219  .id = AV_CODEC_ID_MSS1,
220  .priv_data_size = sizeof(MSS1Context),
224  .capabilities = CODEC_CAP_DR1,
225  .long_name = NULL_IF_CONFIG_SMALL("MS Screen 1"),
226 };
#define ARITH_GET_BIT(VERSION)
Definition: mss12.h:102
static int decode_pal(MSS12Context *ctx, ArithCoder *acoder)
Definition: mss1.c:119
This structure describes decoded (raw) audio or video data.
Definition: avcodec.h:989
int buffer_hints
codec suggestion on buffer type if != 0
Definition: avcodec.h:1253
int high
Definition: mss12.h:49
void(* release_buffer)(struct AVCodecContext *c, AVFrame *pic)
Called to release buffers which were allocated with get_buffer.
Definition: avcodec.h:2259
av_cold int ff_mss12_decode_init(MSS12Context *c, int version, SliceContext *sc1, SliceContext *sc2)
Definition: mss12.c:562
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:237
AVFrame * coded_frame
the picture in the bitstream
Definition: avcodec.h:2725
int value
Definition: mss12.h:49
int corrupted
Definition: mss12.h:89
int size
Definition: avcodec.h:916
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1533
static av_cold int mss1_decode_end(AVCodecContext *avctx)
Definition: mss1.c:205
AVCodec.
Definition: avcodec.h:2960
int ff_mss12_decode_rect(SliceContext *sc, ArithCoder *acoder, int x, int y, int width, int height)
Definition: mss12.c:526
int(* reget_buffer)(struct AVCodecContext *c, AVFrame *pic)
Called at the beginning of a frame to get cr buffer for it.
Definition: avcodec.h:2273
static int decode(MimicContext *ctx, int quality, int num_coeffs, int is_iframe)
Definition: mimic.c:228
uint8_t bits
Definition: crc.c:31
uint32_t pal[256]
Definition: mss12.h:77
uint8_t
8 bit with PIX_FMT_RGB32 palette
Definition: pixfmt.h:76
int keyframe
Definition: mss12.h:87
#define b
Definition: input.c:52
static int arith_get_bits(ArithCoder *c, int bits)
Definition: mss1.c:64
const char data[16]
Definition: mxf.c:66
uint8_t * data
Definition: avcodec.h:915
static void arith_normalise(ArithCoder *c)
Definition: mss1.c:36
static int init(AVCodecParserContext *s)
Definition: h264_parser.c:335
#define r
Definition: input.c:51
static int arith_get_prob(ArithCoder *c, int16_t *probs)
Definition: mss1.c:92
int reference
is this picture used as reference The values for this are the same as the MpegEncContext.picture_structure variable, that is 1->top field, 2->bottom field, 3->frame/both fields.
Definition: avcodec.h:1132
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:88
g
Definition: yuv2rgb.c:540
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:146
const char * name
Name of the codec implementation.
Definition: avcodec.h:2967
int pal_stride
Definition: mss12.h:80
AVCodecContext * avctx
Definition: mss12.h:76
enum AVPictureType pict_type
Picture type of the frame, see ?_TYPE below.
Definition: avcodec.h:1065
int width
picture width / height.
Definition: avcodec.h:1508
NULL
Definition: eval.c:52
av_cold int ff_mss12_decode_end(MSS12Context *c)
Definition: mss12.c:669
struct MSS1Context MSS1Context
external API header
int linesize[AV_NUM_DATA_POINTERS]
Size, in bytes, of the data for each picture/channel plane.
Definition: avcodec.h:1008
main external API structure.
Definition: avcodec.h:1339
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:326
union ArithCoder::@48 gbc
static int mss1_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: mss1.c:138
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:268
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:372
int palette_has_changed
Tell user application that palette has changed from previous frame.
Definition: avcodec.h:1246
AVFrame pic
Definition: mss1.c:32
SliceContext sc
Definition: mss1.c:33
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: avcodec.h:997
void ff_mss12_slicecontext_reset(SliceContext *sc)
Definition: mss12.c:426
static int arith_get_number(ArithCoder *c, int mod_val)
Definition: mss1.c:78
MSS12Context ctx
Definition: mss1.c:31
int free_colours
Definition: mss12.h:86
void * priv_data
Definition: avcodec.h:1382
static av_cold int mss1_decode_init(AVCodecContext *avctx)
Definition: mss1.c:190
int low
Definition: mss12.h:49
int key_frame
1 -> keyframe, 0-> not
Definition: avcodec.h:1058
#define ARITH_GET_MODEL_SYM(VERSION)
Definition: mss12.h:118
GetBitContext * gb
Definition: mss12.h:51
AVCodec ff_mss1_decoder
Definition: mss1.c:216
This structure stores compressed data.
Definition: avcodec.h:898
uint8_t * pal_pic
Definition: mss12.h:78
Predicted.
Definition: avutil.h:246
Common header for Microsoft Screen 1 and 2.
static void arith_init(ArithCoder *c, GetBitContext *gb)
Definition: mss1.c:109