FFmpeg
mss3.c
Go to the documentation of this file.
1 /*
2  * Microsoft Screen 3 (aka Microsoft ATC 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 3 (aka Microsoft ATC Screen) decoder
25  */
26 
27 #include "avcodec.h"
28 #include "bytestream.h"
29 #include "internal.h"
30 #include "mathops.h"
31 #include "mss34dsp.h"
32 
33 #define HEADER_SIZE 27
34 
35 #define MODEL2_SCALE 13
36 #define MODEL_SCALE 15
37 #define MODEL256_SEC_SCALE 9
38 
39 typedef struct Model2 {
43 } Model2;
44 
45 typedef struct Model {
46  int weights[16], freqs[16];
47  int num_syms;
49  int upd_val, max_upd_val, till_rescale;
50 } Model;
51 
52 typedef struct Model256 {
53  int weights[256], freqs[256];
55  int secondary[68];
56  int sec_size;
57  int upd_val, max_upd_val, till_rescale;
58 } Model256;
59 
60 #define RAC_BOTTOM 0x01000000
61 typedef struct RangeCoder {
62  const uint8_t *src, *src_end;
63 
64  uint32_t range, low;
65  int got_error;
66 } RangeCoder;
67 
68 enum BlockType {
74 };
75 
76 typedef struct BlockTypeContext {
77  int last_type;
78  Model bt_model[5];
80 
81 typedef struct FillBlockCoder {
82  int fill_val;
85 
86 typedef struct ImageBlockCoder {
89  Model vq_model[125];
91 
92 typedef struct DCTBlockCoder {
93  int *prev_dc;
94  ptrdiff_t prev_dc_stride;
96  int quality;
97  uint16_t qmat[64];
101 } DCTBlockCoder;
102 
103 typedef struct HaarBlockCoder {
108 
109 typedef struct MSS3Context {
112 
116  FillBlockCoder fill_coder[3];
117  ImageBlockCoder image_coder[3];
118  DCTBlockCoder dct_coder[3];
119  HaarBlockCoder haar_coder[3];
120 
121  int dctblock[64];
122  int hblock[16 * 16];
123 } MSS3Context;
124 
125 
126 static void model2_reset(Model2 *m)
127 {
128  m->zero_weight = 1;
129  m->total_weight = 2;
130  m->zero_freq = 0x1000;
131  m->total_freq = 0x2000;
132  m->upd_val = 4;
133  m->till_rescale = 4;
134 }
135 
136 static void model2_update(Model2 *m, int bit)
137 {
138  unsigned scale;
139 
140  if (!bit)
141  m->zero_weight++;
142  m->till_rescale--;
143  if (m->till_rescale)
144  return;
145 
146  m->total_weight += m->upd_val;
147  if (m->total_weight > 0x2000) {
148  m->total_weight = (m->total_weight + 1) >> 1;
149  m->zero_weight = (m->zero_weight + 1) >> 1;
150  if (m->total_weight == m->zero_weight)
151  m->total_weight = m->zero_weight + 1;
152  }
153  m->upd_val = m->upd_val * 5 >> 2;
154  if (m->upd_val > 64)
155  m->upd_val = 64;
156  scale = 0x80000000u / m->total_weight;
157  m->zero_freq = m->zero_weight * scale >> 18;
158  m->total_freq = m->total_weight * scale >> 18;
159  m->till_rescale = m->upd_val;
160 }
161 
162 static void model_update(Model *m, int val)
163 {
164  int i, sum = 0;
165  unsigned scale;
166 
167  m->weights[val]++;
168  m->till_rescale--;
169  if (m->till_rescale)
170  return;
171  m->tot_weight += m->upd_val;
172 
173  if (m->tot_weight > 0x8000) {
174  m->tot_weight = 0;
175  for (i = 0; i < m->num_syms; i++) {
176  m->weights[i] = (m->weights[i] + 1) >> 1;
177  m->tot_weight += m->weights[i];
178  }
179  }
180  scale = 0x80000000u / m->tot_weight;
181  for (i = 0; i < m->num_syms; i++) {
182  m->freqs[i] = sum * scale >> 16;
183  sum += m->weights[i];
184  }
185 
186  m->upd_val = m->upd_val * 5 >> 2;
187  if (m->upd_val > m->max_upd_val)
188  m->upd_val = m->max_upd_val;
189  m->till_rescale = m->upd_val;
190 }
191 
192 static void model_reset(Model *m)
193 {
194  int i;
195 
196  m->tot_weight = 0;
197  for (i = 0; i < m->num_syms - 1; i++)
198  m->weights[i] = 1;
199  m->weights[m->num_syms - 1] = 0;
200 
201  m->upd_val = m->num_syms;
202  m->till_rescale = 1;
203  model_update(m, m->num_syms - 1);
204  m->till_rescale =
205  m->upd_val = (m->num_syms + 6) >> 1;
206 }
207 
208 static av_cold void model_init(Model *m, int num_syms)
209 {
210  m->num_syms = num_syms;
211  m->max_upd_val = 8 * num_syms + 48;
212 
213  model_reset(m);
214 }
215 
216 static void model256_update(Model256 *m, int val)
217 {
218  int i, sum = 0;
219  unsigned scale;
220  int send, sidx = 1;
221 
222  m->weights[val]++;
223  m->till_rescale--;
224  if (m->till_rescale)
225  return;
226  m->tot_weight += m->upd_val;
227 
228  if (m->tot_weight > 0x8000) {
229  m->tot_weight = 0;
230  for (i = 0; i < 256; i++) {
231  m->weights[i] = (m->weights[i] + 1) >> 1;
232  m->tot_weight += m->weights[i];
233  }
234  }
235  scale = 0x80000000u / m->tot_weight;
236  m->secondary[0] = 0;
237  for (i = 0; i < 256; i++) {
238  m->freqs[i] = sum * scale >> 16;
239  sum += m->weights[i];
240  send = m->freqs[i] >> MODEL256_SEC_SCALE;
241  while (sidx <= send)
242  m->secondary[sidx++] = i - 1;
243  }
244  while (sidx < m->sec_size)
245  m->secondary[sidx++] = 255;
246 
247  m->upd_val = m->upd_val * 5 >> 2;
248  if (m->upd_val > m->max_upd_val)
249  m->upd_val = m->max_upd_val;
250  m->till_rescale = m->upd_val;
251 }
252 
253 static void model256_reset(Model256 *m)
254 {
255  int i;
256 
257  for (i = 0; i < 255; i++)
258  m->weights[i] = 1;
259  m->weights[255] = 0;
260 
261  m->tot_weight = 0;
262  m->upd_val = 256;
263  m->till_rescale = 1;
264  model256_update(m, 255);
265  m->till_rescale =
266  m->upd_val = (256 + 6) >> 1;
267 }
268 
270 {
271  m->max_upd_val = 8 * 256 + 48;
272  m->sec_size = (1 << 6) + 2;
273 
274  model256_reset(m);
275 }
276 
277 static void rac_init(RangeCoder *c, const uint8_t *src, int size)
278 {
279  int i;
280 
281  c->src = src;
282  c->src_end = src + size;
283  c->low = 0;
284  for (i = 0; i < FFMIN(size, 4); i++)
285  c->low = (c->low << 8) | *c->src++;
286  c->range = 0xFFFFFFFF;
287  c->got_error = 0;
288 }
289 
291 {
292  for (;;) {
293  c->range <<= 8;
294  c->low <<= 8;
295  if (c->src < c->src_end) {
296  c->low |= *c->src++;
297  } else if (!c->low) {
298  c->got_error = 1;
299  c->low = 1;
300  }
301  if (c->range >= RAC_BOTTOM)
302  return;
303  }
304 }
305 
307 {
308  int bit;
309 
310  c->range >>= 1;
311 
312  bit = (c->range <= c->low);
313  if (bit)
314  c->low -= c->range;
315 
316  if (c->range < RAC_BOTTOM)
317  rac_normalise(c);
318 
319  return bit;
320 }
321 
322 static int rac_get_bits(RangeCoder *c, int nbits)
323 {
324  int val;
325 
326  c->range >>= nbits;
327  val = c->low / c->range;
328  c->low -= c->range * val;
329 
330  if (c->range < RAC_BOTTOM)
331  rac_normalise(c);
332 
333  return val;
334 }
335 
337 {
338  int bit, helper;
339 
340  helper = m->zero_freq * (c->range >> MODEL2_SCALE);
341  bit = (c->low >= helper);
342  if (bit) {
343  c->low -= helper;
344  c->range -= helper;
345  } else {
346  c->range = helper;
347  }
348 
349  if (c->range < RAC_BOTTOM)
350  rac_normalise(c);
351 
352  model2_update(m, bit);
353 
354  return bit;
355 }
356 
358 {
359  int val;
360  int end, end2;
361  unsigned prob, prob2, helper;
362 
363  prob = 0;
364  prob2 = c->range;
365  c->range >>= MODEL_SCALE;
366  val = 0;
367  end = m->num_syms >> 1;
368  end2 = m->num_syms;
369  do {
370  helper = m->freqs[end] * c->range;
371  if (helper <= c->low) {
372  val = end;
373  prob = helper;
374  } else {
375  end2 = end;
376  prob2 = helper;
377  }
378  end = (end2 + val) >> 1;
379  } while (end != val);
380  c->low -= prob;
381  c->range = prob2 - prob;
382  if (c->range < RAC_BOTTOM)
383  rac_normalise(c);
384 
385  model_update(m, val);
386 
387  return val;
388 }
389 
391 {
392  int val;
393  int start, end;
394  int ssym;
395  unsigned prob, prob2, helper;
396 
397  prob2 = c->range;
398  c->range >>= MODEL_SCALE;
399 
400  helper = c->low / c->range;
401  ssym = helper >> MODEL256_SEC_SCALE;
402  val = m->secondary[ssym];
403 
404  end = start = m->secondary[ssym + 1] + 1;
405  while (end > val + 1) {
406  ssym = (end + val) >> 1;
407  if (m->freqs[ssym] <= helper) {
408  end = start;
409  val = ssym;
410  } else {
411  end = (end + val) >> 1;
412  start = ssym;
413  }
414  }
415  prob = m->freqs[val] * c->range;
416  if (val != 255)
417  prob2 = m->freqs[val + 1] * c->range;
418 
419  c->low -= prob;
420  c->range = prob2 - prob;
421  if (c->range < RAC_BOTTOM)
422  rac_normalise(c);
423 
424  model256_update(m, val);
425 
426  return val;
427 }
428 
430 {
431  bt->last_type = rac_get_model_sym(c, &bt->bt_model[bt->last_type]);
432 
433  return bt->last_type;
434 }
435 
436 static int decode_coeff(RangeCoder *c, Model *m)
437 {
438  int val, sign;
439 
440  val = rac_get_model_sym(c, m);
441  if (val) {
442  sign = rac_get_bit(c);
443  if (val > 1) {
444  val--;
445  val = (1 << val) + rac_get_bits(c, val);
446  }
447  if (!sign)
448  val = -val;
449  }
450 
451  return val;
452 }
453 
455  uint8_t *dst, ptrdiff_t stride, int block_size)
456 {
457  int i;
458 
459  fc->fill_val += decode_coeff(c, &fc->coef_model);
460 
461  for (i = 0; i < block_size; i++, dst += stride)
462  memset(dst, fc->fill_val, block_size);
463 }
464 
466  uint8_t *dst, ptrdiff_t stride, int block_size)
467 {
468  int i, j;
469  int vec_size;
470  int vec[4];
471  int prev_line[16];
472  int A, B, C;
473 
474  vec_size = rac_get_model_sym(c, &ic->vec_size_model) + 2;
475  for (i = 0; i < vec_size; i++)
476  vec[i] = rac_get_model256_sym(c, &ic->vec_entry_model);
477  for (; i < 4; i++)
478  vec[i] = 0;
479  memset(prev_line, 0, sizeof(prev_line));
480 
481  for (j = 0; j < block_size; j++) {
482  A = 0;
483  B = 0;
484  for (i = 0; i < block_size; i++) {
485  C = B;
486  B = prev_line[i];
487  A = rac_get_model_sym(c, &ic->vq_model[A + B * 5 + C * 25]);
488 
489  prev_line[i] = A;
490  if (A < 4)
491  dst[i] = vec[A];
492  else
493  dst[i] = rac_get_model256_sym(c, &ic->esc_model);
494  }
495  dst += stride;
496  }
497 }
498 
499 static int decode_dct(RangeCoder *c, DCTBlockCoder *bc, int *block,
500  int bx, int by)
501 {
502  int skip, val, sign, pos = 1, zz_pos, dc;
503  int blk_pos = bx + by * bc->prev_dc_stride;
504 
505  memset(block, 0, sizeof(*block) * 64);
506 
507  dc = decode_coeff(c, &bc->dc_model);
508  if (by) {
509  if (bx) {
510  int l, tl, t;
511 
512  l = bc->prev_dc[blk_pos - 1];
513  tl = bc->prev_dc[blk_pos - 1 - bc->prev_dc_stride];
514  t = bc->prev_dc[blk_pos - bc->prev_dc_stride];
515 
516  if (FFABS(t - tl) <= FFABS(l - tl))
517  dc += l;
518  else
519  dc += t;
520  } else {
521  dc += bc->prev_dc[blk_pos - bc->prev_dc_stride];
522  }
523  } else if (bx) {
524  dc += bc->prev_dc[bx - 1];
525  }
526  bc->prev_dc[blk_pos] = dc;
527  block[0] = dc * bc->qmat[0];
528 
529  while (pos < 64) {
530  val = rac_get_model256_sym(c, &bc->ac_model);
531  if (!val)
532  return 0;
533  if (val == 0xF0) {
534  pos += 16;
535  continue;
536  }
537  skip = val >> 4;
538  val = val & 0xF;
539  if (!val)
540  return -1;
541  pos += skip;
542  if (pos >= 64)
543  return -1;
544 
545  sign = rac_get_model2_sym(c, &bc->sign_model);
546  if (val > 1) {
547  val--;
548  val = (1 << val) + rac_get_bits(c, val);
549  }
550  if (!sign)
551  val = -val;
552 
553  zz_pos = ff_zigzag_direct[pos];
554  block[zz_pos] = val * bc->qmat[zz_pos];
555  pos++;
556  }
557 
558  return pos == 64 ? 0 : -1;
559 }
560 
562  uint8_t *dst, ptrdiff_t stride, int block_size,
563  int *block, int mb_x, int mb_y)
564 {
565  int i, j;
566  int bx, by;
567  int nblocks = block_size >> 3;
568 
569  bx = mb_x * nblocks;
570  by = mb_y * nblocks;
571 
572  for (j = 0; j < nblocks; j++) {
573  for (i = 0; i < nblocks; i++) {
574  if (decode_dct(c, bc, block, bx + i, by + j)) {
575  c->got_error = 1;
576  return;
577  }
578  ff_mss34_dct_put(dst + i * 8, stride, block);
579  }
580  dst += 8 * stride;
581  }
582 }
583 
585  uint8_t *dst, ptrdiff_t stride,
586  int block_size, int *block)
587 {
588  const int hsize = block_size >> 1;
589  int A, B, C, D, t1, t2, t3, t4;
590  int i, j;
591 
592  for (j = 0; j < block_size; j++) {
593  for (i = 0; i < block_size; i++) {
594  if (i < hsize && j < hsize)
595  block[i] = rac_get_model256_sym(c, &hc->coef_model);
596  else
597  block[i] = decode_coeff(c, &hc->coef_hi_model);
598  block[i] *= hc->scale;
599  }
600  block += block_size;
601  }
602  block -= block_size * block_size;
603 
604  for (j = 0; j < hsize; j++) {
605  for (i = 0; i < hsize; i++) {
606  A = block[i];
607  B = block[i + hsize];
608  C = block[i + hsize * block_size];
609  D = block[i + hsize * block_size + hsize];
610 
611  t1 = A - B;
612  t2 = C - D;
613  t3 = A + B;
614  t4 = C + D;
615  dst[i * 2] = av_clip_uint8(t1 - t2);
616  dst[i * 2 + stride] = av_clip_uint8(t1 + t2);
617  dst[i * 2 + 1] = av_clip_uint8(t3 - t4);
618  dst[i * 2 + 1 + stride] = av_clip_uint8(t3 + t4);
619  }
620  block += block_size;
621  dst += stride * 2;
622  }
623 }
624 
626 {
627  int i, j;
628 
629  for (i = 0; i < 3; i++) {
630  ctx->btype[i].last_type = SKIP_BLOCK;
631  for (j = 0; j < 5; j++)
632  model_reset(&ctx->btype[i].bt_model[j]);
633  ctx->fill_coder[i].fill_val = 0;
638  for (j = 0; j < 125; j++)
639  model_reset(&ctx->image_coder[i].vq_model[j]);
640  if (ctx->dct_coder[i].quality != quality) {
641  ctx->dct_coder[i].quality = quality;
642  ff_mss34_gen_quant_mat(ctx->dct_coder[i].qmat, quality, !i);
643  }
644  memset(ctx->dct_coder[i].prev_dc, 0,
645  sizeof(*ctx->dct_coder[i].prev_dc) *
646  ctx->dct_coder[i].prev_dc_stride *
647  ctx->dct_coder[i].prev_dc_height);
648  model_reset(&ctx->dct_coder[i].dc_model);
651  if (ctx->haar_coder[i].quality != quality) {
652  ctx->haar_coder[i].quality = quality;
653  ctx->haar_coder[i].scale = 17 - 7 * quality / 50;
654  }
657  }
658 }
659 
661 {
662  int i, j;
663 
664  for (i = 0; i < 3; i++) {
665  for (j = 0; j < 5; j++)
666  model_init(&ctx->btype[i].bt_model[j], 5);
667  model_init(&ctx->fill_coder[i].coef_model, 12);
671  for (j = 0; j < 125; j++)
672  model_init(&ctx->image_coder[i].vq_model[j], 5);
673  model_init(&ctx->dct_coder[i].dc_model, 12);
674  model256_init(&ctx->dct_coder[i].ac_model);
675  model_init(&ctx->haar_coder[i].coef_hi_model, 12);
677  }
678 }
679 
680 static int mss3_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
681  AVPacket *avpkt)
682 {
683  const uint8_t *buf = avpkt->data;
684  int buf_size = avpkt->size;
685  MSS3Context *c = avctx->priv_data;
686  RangeCoder *acoder = &c->coder;
687  GetByteContext gb;
688  uint8_t *dst[3];
689  int dec_width, dec_height, dec_x, dec_y, quality, keyframe;
690  int x, y, i, mb_width, mb_height, blk_size, btype;
691  int ret;
692 
693  if (buf_size < HEADER_SIZE) {
694  av_log(avctx, AV_LOG_ERROR,
695  "Frame should have at least %d bytes, got %d instead\n",
696  HEADER_SIZE, buf_size);
697  return AVERROR_INVALIDDATA;
698  }
699 
700  bytestream2_init(&gb, buf, buf_size);
701  keyframe = bytestream2_get_be32(&gb);
702  if (keyframe & ~0x301) {
703  av_log(avctx, AV_LOG_ERROR, "Invalid frame type %X\n", keyframe);
704  return AVERROR_INVALIDDATA;
705  }
706  keyframe = !(keyframe & 1);
707  bytestream2_skip(&gb, 6);
708  dec_x = bytestream2_get_be16(&gb);
709  dec_y = bytestream2_get_be16(&gb);
710  dec_width = bytestream2_get_be16(&gb);
711  dec_height = bytestream2_get_be16(&gb);
712 
713  if (dec_x + dec_width > avctx->width ||
714  dec_y + dec_height > avctx->height ||
715  (dec_width | dec_height) & 0xF) {
716  av_log(avctx, AV_LOG_ERROR, "Invalid frame dimensions %dx%d +%d,%d\n",
717  dec_width, dec_height, dec_x, dec_y);
718  return AVERROR_INVALIDDATA;
719  }
720  bytestream2_skip(&gb, 4);
721  quality = bytestream2_get_byte(&gb);
722  if (quality < 1 || quality > 100) {
723  av_log(avctx, AV_LOG_ERROR, "Invalid quality setting %d\n", quality);
724  return AVERROR_INVALIDDATA;
725  }
726  bytestream2_skip(&gb, 4);
727 
728  if (keyframe && !bytestream2_get_bytes_left(&gb)) {
729  av_log(avctx, AV_LOG_ERROR, "Keyframe without data found\n");
730  return AVERROR_INVALIDDATA;
731  }
732  if (!keyframe && c->got_error)
733  return buf_size;
734  c->got_error = 0;
735 
736  if ((ret = ff_reget_buffer(avctx, c->pic)) < 0)
737  return ret;
738  c->pic->key_frame = keyframe;
740  if (!bytestream2_get_bytes_left(&gb)) {
741  if ((ret = av_frame_ref(data, c->pic)) < 0)
742  return ret;
743  *got_frame = 1;
744 
745  return buf_size;
746  }
747 
748  reset_coders(c, quality);
749 
750  rac_init(acoder, buf + HEADER_SIZE, buf_size - HEADER_SIZE);
751 
752  mb_width = dec_width >> 4;
753  mb_height = dec_height >> 4;
754  dst[0] = c->pic->data[0] + dec_x + dec_y * c->pic->linesize[0];
755  dst[1] = c->pic->data[1] + dec_x / 2 + (dec_y / 2) * c->pic->linesize[1];
756  dst[2] = c->pic->data[2] + dec_x / 2 + (dec_y / 2) * c->pic->linesize[2];
757  for (y = 0; y < mb_height; y++) {
758  for (x = 0; x < mb_width; x++) {
759  for (i = 0; i < 3; i++) {
760  blk_size = 8 << !i;
761 
762  btype = decode_block_type(acoder, c->btype + i);
763  switch (btype) {
764  case FILL_BLOCK:
765  decode_fill_block(acoder, c->fill_coder + i,
766  dst[i] + x * blk_size,
767  c->pic->linesize[i], blk_size);
768  break;
769  case IMAGE_BLOCK:
770  decode_image_block(acoder, c->image_coder + i,
771  dst[i] + x * blk_size,
772  c->pic->linesize[i], blk_size);
773  break;
774  case DCT_BLOCK:
775  decode_dct_block(acoder, c->dct_coder + i,
776  dst[i] + x * blk_size,
777  c->pic->linesize[i], blk_size,
778  c->dctblock, x, y);
779  break;
780  case HAAR_BLOCK:
781  decode_haar_block(acoder, c->haar_coder + i,
782  dst[i] + x * blk_size,
783  c->pic->linesize[i], blk_size,
784  c->hblock);
785  break;
786  }
787  if (c->got_error || acoder->got_error) {
788  av_log(avctx, AV_LOG_ERROR, "Error decoding block %d,%d\n",
789  x, y);
790  c->got_error = 1;
791  return AVERROR_INVALIDDATA;
792  }
793  }
794  }
795  dst[0] += c->pic->linesize[0] * 16;
796  dst[1] += c->pic->linesize[1] * 8;
797  dst[2] += c->pic->linesize[2] * 8;
798  }
799 
800  if ((ret = av_frame_ref(data, c->pic)) < 0)
801  return ret;
802 
803  *got_frame = 1;
804 
805  return buf_size;
806 }
807 
809 {
810  MSS3Context * const c = avctx->priv_data;
811  int i;
812 
813  av_frame_free(&c->pic);
814  for (i = 0; i < 3; i++)
815  av_freep(&c->dct_coder[i].prev_dc);
816 
817  return 0;
818 }
819 
821 {
822  MSS3Context * const c = avctx->priv_data;
823  int i;
824 
825  c->avctx = avctx;
826 
827  if ((avctx->width & 0xF) || (avctx->height & 0xF)) {
828  av_log(avctx, AV_LOG_ERROR,
829  "Image dimensions should be a multiple of 16.\n");
830  return AVERROR_INVALIDDATA;
831  }
832 
833  c->got_error = 0;
834  for (i = 0; i < 3; i++) {
835  int b_width = avctx->width >> (2 + !!i);
836  int b_height = avctx->height >> (2 + !!i);
837  c->dct_coder[i].prev_dc_stride = b_width;
838  c->dct_coder[i].prev_dc_height = b_height;
839  c->dct_coder[i].prev_dc = av_malloc(sizeof(*c->dct_coder[i].prev_dc) *
840  b_width * b_height);
841  if (!c->dct_coder[i].prev_dc) {
842  av_log(avctx, AV_LOG_ERROR, "Cannot allocate buffer\n");
843  av_frame_free(&c->pic);
844  while (i >= 0) {
845  av_freep(&c->dct_coder[i].prev_dc);
846  i--;
847  }
848  return AVERROR(ENOMEM);
849  }
850  }
851 
852  c->pic = av_frame_alloc();
853  if (!c->pic) {
854  mss3_decode_end(avctx);
855  return AVERROR(ENOMEM);
856  }
857 
858  avctx->pix_fmt = AV_PIX_FMT_YUV420P;
859 
860  init_coders(c);
861 
862  return 0;
863 }
864 
866  .name = "msa1",
867  .long_name = NULL_IF_CONFIG_SMALL("MS ATC Screen"),
868  .type = AVMEDIA_TYPE_VIDEO,
869  .id = AV_CODEC_ID_MSA1,
870  .priv_data_size = sizeof(MSS3Context),
872  .close = mss3_decode_end,
874  .capabilities = AV_CODEC_CAP_DR1,
875 };
const char const char void * val
Definition: avisynth_c.h:863
void ff_mss34_gen_quant_mat(uint16_t *qmat, int quality, int luma)
Generate quantisation matrix for given quality.
Definition: mss34dsp.c:48
int * prev_dc
Definition: mss3.c:93
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
static void decode_image_block(RangeCoder *c, ImageBlockCoder *ic, uint8_t *dst, ptrdiff_t stride, int block_size)
Definition: mss3.c:465
int scale
Definition: mss3.c:104
int sec_size
Definition: mss3.c:56
unsigned total_freq
Definition: mss3.c:42
static int decode_block_type(RangeCoder *c, BlockTypeContext *bt)
Definition: mss3.c:429
uint32_t low
Definition: mss3.c:64
This structure describes decoded (raw) audio or video data.
Definition: frame.h:268
#define MODEL256_SEC_SCALE
Definition: mss3.c:37
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:100
static void model2_reset(Model2 *m)
Definition: mss3.c:126
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
Model256 ac_model
Definition: mss3.c:100
Definition: mss12.h:40
ImageBlockCoder image_coder[3]
Definition: mss3.c:117
int size
Definition: avcodec.h:1478
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1775
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:133
int16_t weights[MODEL_MAX_SYMS+1]
Definition: mss12.h:42
int till_rescale
Definition: mss3.c:57
uint32_t range
Definition: mss3.c:64
static void decode_haar_block(RangeCoder *c, HaarBlockCoder *hc, uint8_t *dst, ptrdiff_t stride, int block_size, int *block)
Definition: mss3.c:584
static int mss3_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: mss3.c:680
ptrdiff_t prev_dc_stride
Definition: mss3.c:94
#define src
Definition: vp8dsp.c:254
AVCodec.
Definition: avcodec.h:3477
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:42
#define MODEL2_SCALE
Definition: mss3.c:35
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:2011
Definition: mss3.c:71
AVFrame * pic
Definition: mss3.c:111
The exact code depends on how similar the blocks are and how related they are to the block
AVCodecContext * avctx
Definition: mss3.c:110
uint8_t
#define av_cold
Definition: attributes.h:82
#define av_malloc(s)
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:189
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
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
static void rac_normalise(RangeCoder *c)
Definition: mss3.c:290
#define HEADER_SIZE
Definition: mss3.c:33
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 decode_dct(RangeCoder *c, DCTBlockCoder *bc, int *block, int bx, int by)
Definition: mss3.c:499
Model256 esc_model
Definition: mss3.c:87
int quality
Definition: mss3.c:104
#define u(width, name, range_min, range_max)
Definition: cbs_h2645.c:253
static void reset_coders(MSS3Context *ctx, int quality)
Definition: mss3.c:625
int weights[256]
Definition: mss3.c:53
static void rac_init(RangeCoder *c, const uint8_t *src, int size)
Definition: mss3.c:277
uint8_t * data
Definition: avcodec.h:1477
static void model256_update(Model256 *m, int val)
Definition: mss3.c:216
uint16_t qmat[64]
Definition: mss3.c:97
int got_error
Definition: mss3.c:65
ptrdiff_t size
Definition: opengl_enc.c:100
#define A(x)
Definition: vp56_arith.h:28
#define av_log(a,...)
Model coef_model
Definition: mss3.c:83
#define prob(name, subs,...)
Definition: cbs_vp9.c:374
#define fc(width, name, range_min, range_max)
Definition: cbs_av1.c:551
static av_cold void model_init(Model *m, int num_syms)
Definition: mss3.c:208
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:260
Definition: mss3.c:39
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:164
int freqs[256]
Definition: mss3.c:53
int till_rescale
Definition: mss3.c:40
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:202
Model vq_model[125]
Definition: mss3.c:89
#define B
Definition: huffyuvdsp.h:32
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
int tot_weight
Definition: mss3.c:54
int quality
Definition: mss3.c:96
DCTBlockCoder dct_coder[3]
Definition: mss3.c:118
#define t1
Definition: regdef.h:29
static av_always_inline unsigned int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:154
const char * name
Name of the codec implementation.
Definition: avcodec.h:3484
#define t3
Definition: regdef.h:31
static av_cold void model256_init(Model256 *m)
Definition: mss3.c:269
HaarBlockCoder haar_coder[3]
Definition: mss3.c:119
int upd_val
Definition: mss3.c:40
void ff_mss34_dct_put(uint8_t *dst, ptrdiff_t stride, int *block)
Transform and output DCT block.
Definition: mss34dsp.c:90
int secondary[68]
Definition: mss3.c:55
unsigned zero_weight
Definition: mss3.c:41
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:351
unsigned total_weight
Definition: mss3.c:42
#define FFMIN(a, b)
Definition: common.h:96
static av_cold int mss3_decode_init(AVCodecContext *avctx)
Definition: mss3.c:820
const uint8_t * src_end
Definition: mss3.c:62
int width
picture width / height.
Definition: avcodec.h:1738
static void decode_dct_block(RangeCoder *c, DCTBlockCoder *bc, uint8_t *dst, ptrdiff_t stride, int block_size, int *block, int mb_x, int mb_y)
Definition: mss3.c:561
int upd_val
Definition: mss3.c:57
AVFormatContext * ctx
Definition: movenc.c:48
trying all byte sequences megabyte in length and selecting the best looking sequence will yield cases to try But a word about quality
static int rac_get_bits(RangeCoder *c, int nbits)
Definition: mss3.c:322
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:72
int prev_dc_height
Definition: mss3.c:95
unsigned zero_freq
Definition: mss3.c:41
int hblock[16 *16]
Definition: mss3.c:122
int upd_val
Definition: mss3.c:49
s EdgeDetect Foobar g libavfilter vf_edgedetect c libavfilter vf_foobar c edit libavfilter and add an entry for foobar following the pattern of the other filters edit libavfilter allfilters and add an entry for foobar following the pattern of the other filters configure make j< whatever > ffmpeg ffmpeg i you should get a foobar png with Lena edge detected That s your new playground is ready Some little details about what s going which in turn will define variables for the build system and the C
const uint8_t * src
Definition: mss3.c:62
static void model_reset(Model *m)
Definition: mss3.c:192
static int rac_get_model256_sym(RangeCoder *c, Model256 *m)
Definition: mss3.c:390
Definition: mss3.c:52
Libavcodec external API header.
static int rac_get_model_sym(RangeCoder *c, Model *m)
Definition: mss3.c:357
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:299
main external API structure.
Definition: avcodec.h:1565
int num_syms
Definition: mss12.h:44
Tag MUST be and< 10hcoeff half pel interpolation filter coefficients, hcoeff[0] are the 2 middle coefficients[1] are the next outer ones and so on, resulting in a filter like:...eff[2], hcoeff[1], hcoeff[0], hcoeff[0], hcoeff[1], hcoeff[2]...the sign of the coefficients is not explicitly stored but alternates after each coeff and coeff[0] is positive, so...,+,-,+,-,+,+,-,+,-,+,...hcoeff[0] is not explicitly stored but found by subtracting the sum of all stored coefficients with signs from 32 hcoeff[0]=32-hcoeff[1]-hcoeff[2]-...a good choice for hcoeff and htaps is htaps=6 hcoeff={40,-10, 2}an alternative which requires more computations at both encoder and decoder side and may or may not be better is htaps=8 hcoeff={42,-14, 6,-2}ref_frames minimum of the number of available reference frames and max_ref_frames for example the first frame after a key frame always has ref_frames=1spatial_decomposition_type wavelet type 0 is a 9/7 symmetric compact integer wavelet 1 is a 5/3 symmetric compact integer wavelet others are reserved stored as delta from last, last is reset to 0 if always_reset||keyframeqlog quality(logarithmic quantizer scale) stored as delta from last, last is reset to 0 if always_reset||keyframemv_scale stored as delta from last, last is reset to 0 if always_reset||keyframe FIXME check that everything works fine if this changes between framesqbias dequantization bias stored as delta from last, last is reset to 0 if always_reset||keyframeblock_max_depth maximum depth of the block tree stored as delta from last, last is reset to 0 if always_reset||keyframequant_table quantization tableHighlevel bitstream structure:==============================--------------------------------------------|Header|--------------------------------------------|------------------------------------|||Block0||||split?||||yes no||||.........intra?||||:Block01:yes no||||:Block02:.................||||:Block03::y DC::ref index:||||:Block04::cb DC::motion x:||||.........:cr DC::motion y:||||.................|||------------------------------------||------------------------------------|||Block1|||...|--------------------------------------------|------------------------------------|||Y subbands||Cb subbands||Cr subbands||||------||------||------|||||LL0||HL0||||LL0||HL0||||LL0||HL0|||||------||------||------||||------||------||------|||||LH0||HH0||||LH0||HH0||||LH0||HH0|||||------||------||------||||------||------||------|||||HL1||LH1||||HL1||LH1||||HL1||LH1|||||------||------||------||||------||------||------|||||HH1||HL2||||HH1||HL2||||HH1||HL2|||||...||...||...|||------------------------------------|--------------------------------------------Decoding process:=================------------|||Subbands|------------||||------------|Intra DC||||LL0 subband prediction------------|\Dequantization-------------------\||Reference frames|\IDWT|--------------|Motion\|||Frame 0||Frame 1||Compensation.OBMC v-------|--------------|--------------.\------> Frame n output Frame Frame<----------------------------------/|...|-------------------Range Coder:============Binary Range Coder:-------------------The implemented range coder is an adapted version based upon"Range encoding: an algorithm for removing redundancy from a digitised message."by G.N.N.Martin.The symbols encoded by the Snow range coder are bits(0|1).The associated probabilities are not fix but change depending on the symbol mix seen so far.bit seen|new state---------+-----------------------------------------------0|256-state_transition_table[256-old_state];1|state_transition_table[old_state];state_transition_table={0, 0, 0, 0, 0, 0, 0, 0, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 190, 191, 192, 194, 194, 195, 196, 197, 198, 199, 200, 201, 202, 202, 204, 205, 206, 207, 208, 209, 209, 210, 211, 212, 213, 215, 215, 216, 217, 218, 219, 220, 220, 222, 223, 224, 225, 226, 227, 227, 229, 229, 230, 231, 232, 234, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 248, 0, 0, 0, 0, 0, 0, 0};FIXME Range Coding of integers:-------------------------FIXME Neighboring Blocks:===================left and top are set to the respective blocks unless they are outside of the image in which case they are set to the Null block top-left is set to the top left block unless it is outside of the image in which case it is set to the left block if this block has no larger parent block or it is at the left side of its parent block and the top right block is not outside of the image then the top right block is used for top-right else the top-left block is used Null block y, cb, cr are 128 level, ref, mx and my are 0 Motion Vector Prediction:=========================1.the motion vectors of all the neighboring blocks are scaled to compensate for the difference of reference frames scaled_mv=(mv *(256 *(current_reference+1)/(mv.reference+1))+128)> the median of the scaled top and top right vectors is used as motion vector prediction the used motion vector is the sum of the predictor and(mvx_diff, mvy_diff)*mv_scale Intra DC Prediction block[y][x] dc[1]
Definition: snow.txt:400
static void model_update(Model *m, int val)
Definition: mss3.c:162
static av_cold void init_coders(MSS3Context *ctx)
Definition: mss3.c:660
static void decode_fill_block(RangeCoder *c, FillBlockCoder *fc, uint8_t *dst, ptrdiff_t stride, int block_size)
Definition: mss3.c:454
void * buf
Definition: avisynth_c.h:766
#define MODEL_SCALE
Definition: mss3.c:36
int tot_weight
Definition: mss3.c:48
static int rac_get_bit(RangeCoder *c)
Definition: mss3.c:306
int dctblock[64]
Definition: mss3.c:121
static void model256_reset(Model256 *m)
Definition: mss3.c:253
Model2 sign_model
Definition: mss3.c:99
int got_error
Definition: mss3.c:113
int freqs[16]
Definition: mss3.c:46
const uint8_t ff_zigzag_direct[64]
Definition: mathtables.c:98
Model256 coef_model
Definition: mss3.c:105
FillBlockCoder fill_coder[3]
Definition: mss3.c:116
BlockType
Definition: mss3.c:68
static av_cold int mss3_decode_end(AVCodecContext *avctx)
Definition: mss3.c:808
#define RAC_BOTTOM
Definition: mss3.c:60
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:282
Model256 vec_entry_model
Definition: mss3.c:87
RangeCoder coder
Definition: mss3.c:114
int max_upd_val
Definition: mss3.c:57
static void model2_update(Model2 *m, int bit)
Definition: mss3.c:136
GLint GLenum GLboolean GLsizei stride
Definition: opengl_enc.c:104
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:66
int till_rescale
Definition: mss3.c:49
common internal api header.
D(D(float, sse)
Definition: rematrix_init.c:28
static int decode_coeff(RangeCoder *c, Model *m)
Definition: mss3.c:436
int max_upd_val
Definition: mss3.c:49
AVCodec ff_msa1_decoder
Definition: mss3.c:865
void * priv_data
Definition: avcodec.h:1592
#define t4
Definition: regdef.h:32
int fill_val
Definition: mss3.c:82
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:346
Model bt_model[5]
Definition: mss3.c:78
#define av_freep(p)
void INT64 start
Definition: avisynth_c.h:766
BlockTypeContext btype[3]
Definition: mss3.c:115
int last_type
Definition: mss3.c:77
#define stride
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
static int rac_get_model2_sym(RangeCoder *c, Model2 *m)
Definition: mss3.c:336
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
#define t2
Definition: regdef.h:30
Model dc_model
Definition: mss3.c:98
Predicted.
Definition: avutil.h:275
Model vec_size_model
Definition: mss3.c:88
Model coef_hi_model
Definition: mss3.c:106