LCOV - code coverage report
Current view: top level - libavcodec - mss3.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 480 0.0 %
Date: 2017-12-10 21:22:29 Functions: 0 27 0.0 %

          Line data    Source code
       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 {
      40             :     int      upd_val, till_rescale;
      41             :     unsigned zero_freq,  zero_weight;
      42             :     unsigned total_freq, total_weight;
      43             : } Model2;
      44             : 
      45             : typedef struct Model {
      46             :     int weights[16], freqs[16];
      47             :     int num_syms;
      48             :     int tot_weight;
      49             :     int upd_val, max_upd_val, till_rescale;
      50             : } Model;
      51             : 
      52             : typedef struct Model256 {
      53             :     int weights[256], freqs[256];
      54             :     int tot_weight;
      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 {
      69             :     FILL_BLOCK = 0,
      70             :     IMAGE_BLOCK,
      71             :     DCT_BLOCK,
      72             :     HAAR_BLOCK,
      73             :     SKIP_BLOCK
      74             : };
      75             : 
      76             : typedef struct BlockTypeContext {
      77             :     int      last_type;
      78             :     Model    bt_model[5];
      79             : } BlockTypeContext;
      80             : 
      81             : typedef struct FillBlockCoder {
      82             :     int      fill_val;
      83             :     Model    coef_model;
      84             : } FillBlockCoder;
      85             : 
      86             : typedef struct ImageBlockCoder {
      87             :     Model256 esc_model, vec_entry_model;
      88             :     Model    vec_size_model;
      89             :     Model    vq_model[125];
      90             : } ImageBlockCoder;
      91             : 
      92             : typedef struct DCTBlockCoder {
      93             :     int      *prev_dc;
      94             :     ptrdiff_t prev_dc_stride;
      95             :     int      prev_dc_height;
      96             :     int      quality;
      97             :     uint16_t qmat[64];
      98             :     Model    dc_model;
      99             :     Model2   sign_model;
     100             :     Model256 ac_model;
     101             : } DCTBlockCoder;
     102             : 
     103             : typedef struct HaarBlockCoder {
     104             :     int      quality, scale;
     105             :     Model256 coef_model;
     106             :     Model    coef_hi_model;
     107             : } HaarBlockCoder;
     108             : 
     109             : typedef struct MSS3Context {
     110             :     AVCodecContext   *avctx;
     111             :     AVFrame          *pic;
     112             : 
     113             :     int              got_error;
     114             :     RangeCoder       coder;
     115             :     BlockTypeContext btype[3];
     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           0 : static void model2_reset(Model2 *m)
     127             : {
     128           0 :     m->zero_weight  = 1;
     129           0 :     m->total_weight = 2;
     130           0 :     m->zero_freq    = 0x1000;
     131           0 :     m->total_freq   = 0x2000;
     132           0 :     m->upd_val      = 4;
     133           0 :     m->till_rescale = 4;
     134           0 : }
     135             : 
     136           0 : static void model2_update(Model2 *m, int bit)
     137             : {
     138             :     unsigned scale;
     139             : 
     140           0 :     if (!bit)
     141           0 :         m->zero_weight++;
     142           0 :     m->till_rescale--;
     143           0 :     if (m->till_rescale)
     144           0 :         return;
     145             : 
     146           0 :     m->total_weight += m->upd_val;
     147           0 :     if (m->total_weight > 0x2000) {
     148           0 :         m->total_weight = (m->total_weight + 1) >> 1;
     149           0 :         m->zero_weight  = (m->zero_weight  + 1) >> 1;
     150           0 :         if (m->total_weight == m->zero_weight)
     151           0 :             m->total_weight = m->zero_weight + 1;
     152             :     }
     153           0 :     m->upd_val = m->upd_val * 5 >> 2;
     154           0 :     if (m->upd_val > 64)
     155           0 :         m->upd_val = 64;
     156           0 :     scale = 0x80000000u / m->total_weight;
     157           0 :     m->zero_freq    = m->zero_weight  * scale >> 18;
     158           0 :     m->total_freq   = m->total_weight * scale >> 18;
     159           0 :     m->till_rescale = m->upd_val;
     160             : }
     161             : 
     162           0 : static void model_update(Model *m, int val)
     163             : {
     164           0 :     int i, sum = 0;
     165             :     unsigned scale;
     166             : 
     167           0 :     m->weights[val]++;
     168           0 :     m->till_rescale--;
     169           0 :     if (m->till_rescale)
     170           0 :         return;
     171           0 :     m->tot_weight += m->upd_val;
     172             : 
     173           0 :     if (m->tot_weight > 0x8000) {
     174           0 :         m->tot_weight = 0;
     175           0 :         for (i = 0; i < m->num_syms; i++) {
     176           0 :             m->weights[i]  = (m->weights[i] + 1) >> 1;
     177           0 :             m->tot_weight +=  m->weights[i];
     178             :         }
     179             :     }
     180           0 :     scale = 0x80000000u / m->tot_weight;
     181           0 :     for (i = 0; i < m->num_syms; i++) {
     182           0 :         m->freqs[i] = sum * scale >> 16;
     183           0 :         sum += m->weights[i];
     184             :     }
     185             : 
     186           0 :     m->upd_val = m->upd_val * 5 >> 2;
     187           0 :     if (m->upd_val > m->max_upd_val)
     188           0 :         m->upd_val = m->max_upd_val;
     189           0 :     m->till_rescale = m->upd_val;
     190             : }
     191             : 
     192           0 : static void model_reset(Model *m)
     193             : {
     194             :     int i;
     195             : 
     196           0 :     m->tot_weight   = 0;
     197           0 :     for (i = 0; i < m->num_syms - 1; i++)
     198           0 :         m->weights[i] = 1;
     199           0 :     m->weights[m->num_syms - 1] = 0;
     200             : 
     201           0 :     m->upd_val      = m->num_syms;
     202           0 :     m->till_rescale = 1;
     203           0 :     model_update(m, m->num_syms - 1);
     204           0 :     m->till_rescale =
     205           0 :     m->upd_val      = (m->num_syms + 6) >> 1;
     206           0 : }
     207             : 
     208           0 : static av_cold void model_init(Model *m, int num_syms)
     209             : {
     210           0 :     m->num_syms    = num_syms;
     211           0 :     m->max_upd_val = 8 * num_syms + 48;
     212             : 
     213           0 :     model_reset(m);
     214           0 : }
     215             : 
     216           0 : static void model256_update(Model256 *m, int val)
     217             : {
     218           0 :     int i, sum = 0;
     219             :     unsigned scale;
     220           0 :     int send, sidx = 1;
     221             : 
     222           0 :     m->weights[val]++;
     223           0 :     m->till_rescale--;
     224           0 :     if (m->till_rescale)
     225           0 :         return;
     226           0 :     m->tot_weight += m->upd_val;
     227             : 
     228           0 :     if (m->tot_weight > 0x8000) {
     229           0 :         m->tot_weight = 0;
     230           0 :         for (i = 0; i < 256; i++) {
     231           0 :             m->weights[i]  = (m->weights[i] + 1) >> 1;
     232           0 :             m->tot_weight +=  m->weights[i];
     233             :         }
     234             :     }
     235           0 :     scale = 0x80000000u / m->tot_weight;
     236           0 :     m->secondary[0] = 0;
     237           0 :     for (i = 0; i < 256; i++) {
     238           0 :         m->freqs[i] = sum * scale >> 16;
     239           0 :         sum += m->weights[i];
     240           0 :         send = m->freqs[i] >> MODEL256_SEC_SCALE;
     241           0 :         while (sidx <= send)
     242           0 :             m->secondary[sidx++] = i - 1;
     243             :     }
     244           0 :     while (sidx < m->sec_size)
     245           0 :         m->secondary[sidx++] = 255;
     246             : 
     247           0 :     m->upd_val = m->upd_val * 5 >> 2;
     248           0 :     if (m->upd_val > m->max_upd_val)
     249           0 :         m->upd_val = m->max_upd_val;
     250           0 :     m->till_rescale = m->upd_val;
     251             : }
     252             : 
     253           0 : static void model256_reset(Model256 *m)
     254             : {
     255             :     int i;
     256             : 
     257           0 :     for (i = 0; i < 255; i++)
     258           0 :         m->weights[i] = 1;
     259           0 :     m->weights[255] = 0;
     260             : 
     261           0 :     m->tot_weight   = 0;
     262           0 :     m->upd_val      = 256;
     263           0 :     m->till_rescale = 1;
     264           0 :     model256_update(m, 255);
     265           0 :     m->till_rescale =
     266           0 :     m->upd_val      = (256 + 6) >> 1;
     267           0 : }
     268             : 
     269           0 : static av_cold void model256_init(Model256 *m)
     270             : {
     271           0 :     m->max_upd_val = 8 * 256 + 48;
     272           0 :     m->sec_size    = (1 << 6) + 2;
     273             : 
     274           0 :     model256_reset(m);
     275           0 : }
     276             : 
     277           0 : static void rac_init(RangeCoder *c, const uint8_t *src, int size)
     278             : {
     279             :     int i;
     280             : 
     281           0 :     c->src       = src;
     282           0 :     c->src_end   = src + size;
     283           0 :     c->low       = 0;
     284           0 :     for (i = 0; i < FFMIN(size, 4); i++)
     285           0 :         c->low = (c->low << 8) | *c->src++;
     286           0 :     c->range     = 0xFFFFFFFF;
     287           0 :     c->got_error = 0;
     288           0 : }
     289             : 
     290           0 : static void rac_normalise(RangeCoder *c)
     291             : {
     292             :     for (;;) {
     293           0 :         c->range <<= 8;
     294           0 :         c->low   <<= 8;
     295           0 :         if (c->src < c->src_end) {
     296           0 :             c->low |= *c->src++;
     297           0 :         } else if (!c->low) {
     298           0 :             c->got_error = 1;
     299           0 :             c->low = 1;
     300             :         }
     301           0 :         if (c->range >= RAC_BOTTOM)
     302           0 :             return;
     303             :     }
     304             : }
     305             : 
     306           0 : static int rac_get_bit(RangeCoder *c)
     307             : {
     308             :     int bit;
     309             : 
     310           0 :     c->range >>= 1;
     311             : 
     312           0 :     bit = (c->range <= c->low);
     313           0 :     if (bit)
     314           0 :         c->low -= c->range;
     315             : 
     316           0 :     if (c->range < RAC_BOTTOM)
     317           0 :         rac_normalise(c);
     318             : 
     319           0 :     return bit;
     320             : }
     321             : 
     322           0 : static int rac_get_bits(RangeCoder *c, int nbits)
     323             : {
     324             :     int val;
     325             : 
     326           0 :     c->range >>= nbits;
     327           0 :     val = c->low / c->range;
     328           0 :     c->low -= c->range * val;
     329             : 
     330           0 :     if (c->range < RAC_BOTTOM)
     331           0 :         rac_normalise(c);
     332             : 
     333           0 :     return val;
     334             : }
     335             : 
     336           0 : static int rac_get_model2_sym(RangeCoder *c, Model2 *m)
     337             : {
     338             :     int bit, helper;
     339             : 
     340           0 :     helper = m->zero_freq * (c->range >> MODEL2_SCALE);
     341           0 :     bit    = (c->low >= helper);
     342           0 :     if (bit) {
     343           0 :         c->low   -= helper;
     344           0 :         c->range -= helper;
     345             :     } else {
     346           0 :         c->range  = helper;
     347             :     }
     348             : 
     349           0 :     if (c->range < RAC_BOTTOM)
     350           0 :         rac_normalise(c);
     351             : 
     352           0 :     model2_update(m, bit);
     353             : 
     354           0 :     return bit;
     355             : }
     356             : 
     357           0 : static int rac_get_model_sym(RangeCoder *c, Model *m)
     358             : {
     359             :     int val;
     360             :     int end, end2;
     361             :     unsigned prob, prob2, helper;
     362             : 
     363           0 :     prob       = 0;
     364           0 :     prob2      = c->range;
     365           0 :     c->range >>= MODEL_SCALE;
     366           0 :     val        = 0;
     367           0 :     end        = m->num_syms >> 1;
     368           0 :     end2       = m->num_syms;
     369             :     do {
     370           0 :         helper = m->freqs[end] * c->range;
     371           0 :         if (helper <= c->low) {
     372           0 :             val   = end;
     373           0 :             prob  = helper;
     374             :         } else {
     375           0 :             end2  = end;
     376           0 :             prob2 = helper;
     377             :         }
     378           0 :         end = (end2 + val) >> 1;
     379           0 :     } while (end != val);
     380           0 :     c->low  -= prob;
     381           0 :     c->range = prob2 - prob;
     382           0 :     if (c->range < RAC_BOTTOM)
     383           0 :         rac_normalise(c);
     384             : 
     385           0 :     model_update(m, val);
     386             : 
     387           0 :     return val;
     388             : }
     389             : 
     390           0 : static int rac_get_model256_sym(RangeCoder *c, Model256 *m)
     391             : {
     392             :     int val;
     393             :     int start, end;
     394             :     int ssym;
     395             :     unsigned prob, prob2, helper;
     396             : 
     397           0 :     prob2      = c->range;
     398           0 :     c->range >>= MODEL_SCALE;
     399             : 
     400           0 :     helper     = c->low / c->range;
     401           0 :     ssym       = helper >> MODEL256_SEC_SCALE;
     402           0 :     val        = m->secondary[ssym];
     403             : 
     404           0 :     end = start = m->secondary[ssym + 1] + 1;
     405           0 :     while (end > val + 1) {
     406           0 :         ssym = (end + val) >> 1;
     407           0 :         if (m->freqs[ssym] <= helper) {
     408           0 :             end = start;
     409           0 :             val = ssym;
     410             :         } else {
     411           0 :             end   = (end + val) >> 1;
     412           0 :             start = ssym;
     413             :         }
     414             :     }
     415           0 :     prob = m->freqs[val] * c->range;
     416           0 :     if (val != 255)
     417           0 :         prob2 = m->freqs[val + 1] * c->range;
     418             : 
     419           0 :     c->low  -= prob;
     420           0 :     c->range = prob2 - prob;
     421           0 :     if (c->range < RAC_BOTTOM)
     422           0 :         rac_normalise(c);
     423             : 
     424           0 :     model256_update(m, val);
     425             : 
     426           0 :     return val;
     427             : }
     428             : 
     429           0 : static int decode_block_type(RangeCoder *c, BlockTypeContext *bt)
     430             : {
     431           0 :     bt->last_type = rac_get_model_sym(c, &bt->bt_model[bt->last_type]);
     432             : 
     433           0 :     return bt->last_type;
     434             : }
     435             : 
     436           0 : static int decode_coeff(RangeCoder *c, Model *m)
     437             : {
     438             :     int val, sign;
     439             : 
     440           0 :     val = rac_get_model_sym(c, m);
     441           0 :     if (val) {
     442           0 :         sign = rac_get_bit(c);
     443           0 :         if (val > 1) {
     444           0 :             val--;
     445           0 :             val = (1 << val) + rac_get_bits(c, val);
     446             :         }
     447           0 :         if (!sign)
     448           0 :             val = -val;
     449             :     }
     450             : 
     451           0 :     return val;
     452             : }
     453             : 
     454           0 : static void decode_fill_block(RangeCoder *c, FillBlockCoder *fc,
     455             :                               uint8_t *dst, ptrdiff_t stride, int block_size)
     456             : {
     457             :     int i;
     458             : 
     459           0 :     fc->fill_val += decode_coeff(c, &fc->coef_model);
     460             : 
     461           0 :     for (i = 0; i < block_size; i++, dst += stride)
     462           0 :         memset(dst, fc->fill_val, block_size);
     463           0 : }
     464             : 
     465           0 : static void decode_image_block(RangeCoder *c, ImageBlockCoder *ic,
     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           0 :     vec_size = rac_get_model_sym(c, &ic->vec_size_model) + 2;
     475           0 :     for (i = 0; i < vec_size; i++)
     476           0 :         vec[i] = rac_get_model256_sym(c, &ic->vec_entry_model);
     477           0 :     for (; i < 4; i++)
     478           0 :         vec[i] = 0;
     479           0 :     memset(prev_line, 0, sizeof(prev_line));
     480             : 
     481           0 :     for (j = 0; j < block_size; j++) {
     482           0 :         A = 0;
     483           0 :         B = 0;
     484           0 :         for (i = 0; i < block_size; i++) {
     485           0 :             C = B;
     486           0 :             B = prev_line[i];
     487           0 :             A = rac_get_model_sym(c, &ic->vq_model[A + B * 5 + C * 25]);
     488             : 
     489           0 :             prev_line[i] = A;
     490           0 :             if (A < 4)
     491           0 :                dst[i] = vec[A];
     492             :             else
     493           0 :                dst[i] = rac_get_model256_sym(c, &ic->esc_model);
     494             :         }
     495           0 :         dst += stride;
     496             :     }
     497           0 : }
     498             : 
     499           0 : static int decode_dct(RangeCoder *c, DCTBlockCoder *bc, int *block,
     500             :                       int bx, int by)
     501             : {
     502           0 :     int skip, val, sign, pos = 1, zz_pos, dc;
     503           0 :     int blk_pos = bx + by * bc->prev_dc_stride;
     504             : 
     505           0 :     memset(block, 0, sizeof(*block) * 64);
     506             : 
     507           0 :     dc = decode_coeff(c, &bc->dc_model);
     508           0 :     if (by) {
     509           0 :         if (bx) {
     510             :             int l, tl, t;
     511             : 
     512           0 :             l  = bc->prev_dc[blk_pos - 1];
     513           0 :             tl = bc->prev_dc[blk_pos - 1 - bc->prev_dc_stride];
     514           0 :             t  = bc->prev_dc[blk_pos     - bc->prev_dc_stride];
     515             : 
     516           0 :             if (FFABS(t - tl) <= FFABS(l - tl))
     517           0 :                 dc += l;
     518             :             else
     519           0 :                 dc += t;
     520             :         } else {
     521           0 :             dc += bc->prev_dc[blk_pos - bc->prev_dc_stride];
     522             :         }
     523           0 :     } else if (bx) {
     524           0 :         dc += bc->prev_dc[bx - 1];
     525             :     }
     526           0 :     bc->prev_dc[blk_pos] = dc;
     527           0 :     block[0]             = dc * bc->qmat[0];
     528             : 
     529           0 :     while (pos < 64) {
     530           0 :         val = rac_get_model256_sym(c, &bc->ac_model);
     531           0 :         if (!val)
     532           0 :             return 0;
     533           0 :         if (val == 0xF0) {
     534           0 :             pos += 16;
     535           0 :             continue;
     536             :         }
     537           0 :         skip = val >> 4;
     538           0 :         val  = val & 0xF;
     539           0 :         if (!val)
     540           0 :             return -1;
     541           0 :         pos += skip;
     542           0 :         if (pos >= 64)
     543           0 :             return -1;
     544             : 
     545           0 :         sign = rac_get_model2_sym(c, &bc->sign_model);
     546           0 :         if (val > 1) {
     547           0 :             val--;
     548           0 :             val = (1 << val) + rac_get_bits(c, val);
     549             :         }
     550           0 :         if (!sign)
     551           0 :             val = -val;
     552             : 
     553           0 :         zz_pos = ff_zigzag_direct[pos];
     554           0 :         block[zz_pos] = val * bc->qmat[zz_pos];
     555           0 :         pos++;
     556             :     }
     557             : 
     558           0 :     return pos == 64 ? 0 : -1;
     559             : }
     560             : 
     561           0 : static void decode_dct_block(RangeCoder *c, DCTBlockCoder *bc,
     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           0 :     int nblocks = block_size >> 3;
     568             : 
     569           0 :     bx = mb_x * nblocks;
     570           0 :     by = mb_y * nblocks;
     571             : 
     572           0 :     for (j = 0; j < nblocks; j++) {
     573           0 :         for (i = 0; i < nblocks; i++) {
     574           0 :             if (decode_dct(c, bc, block, bx + i, by + j)) {
     575           0 :                 c->got_error = 1;
     576           0 :                 return;
     577             :             }
     578           0 :             ff_mss34_dct_put(dst + i * 8, stride, block);
     579             :         }
     580           0 :         dst += 8 * stride;
     581             :     }
     582             : }
     583             : 
     584           0 : static void decode_haar_block(RangeCoder *c, HaarBlockCoder *hc,
     585             :                               uint8_t *dst, ptrdiff_t stride,
     586             :                               int block_size, int *block)
     587             : {
     588           0 :     const int hsize = block_size >> 1;
     589             :     int A, B, C, D, t1, t2, t3, t4;
     590             :     int i, j;
     591             : 
     592           0 :     for (j = 0; j < block_size; j++) {
     593           0 :         for (i = 0; i < block_size; i++) {
     594           0 :             if (i < hsize && j < hsize)
     595           0 :                 block[i] = rac_get_model256_sym(c, &hc->coef_model);
     596             :             else
     597           0 :                 block[i] = decode_coeff(c, &hc->coef_hi_model);
     598           0 :             block[i] *= hc->scale;
     599             :         }
     600           0 :         block += block_size;
     601             :     }
     602           0 :     block -= block_size * block_size;
     603             : 
     604           0 :     for (j = 0; j < hsize; j++) {
     605           0 :         for (i = 0; i < hsize; i++) {
     606           0 :             A = block[i];
     607           0 :             B = block[i + hsize];
     608           0 :             C = block[i + hsize * block_size];
     609           0 :             D = block[i + hsize * block_size + hsize];
     610             : 
     611           0 :             t1 = A - B;
     612           0 :             t2 = C - D;
     613           0 :             t3 = A + B;
     614           0 :             t4 = C + D;
     615           0 :             dst[i * 2]              = av_clip_uint8(t1 - t2);
     616           0 :             dst[i * 2 + stride]     = av_clip_uint8(t1 + t2);
     617           0 :             dst[i * 2 + 1]          = av_clip_uint8(t3 - t4);
     618           0 :             dst[i * 2 + 1 + stride] = av_clip_uint8(t3 + t4);
     619             :         }
     620           0 :         block += block_size;
     621           0 :         dst   += stride * 2;
     622             :     }
     623           0 : }
     624             : 
     625           0 : static void reset_coders(MSS3Context *ctx, int quality)
     626             : {
     627             :     int i, j;
     628             : 
     629           0 :     for (i = 0; i < 3; i++) {
     630           0 :         ctx->btype[i].last_type = SKIP_BLOCK;
     631           0 :         for (j = 0; j < 5; j++)
     632           0 :             model_reset(&ctx->btype[i].bt_model[j]);
     633           0 :         ctx->fill_coder[i].fill_val = 0;
     634           0 :         model_reset(&ctx->fill_coder[i].coef_model);
     635           0 :         model256_reset(&ctx->image_coder[i].esc_model);
     636           0 :         model256_reset(&ctx->image_coder[i].vec_entry_model);
     637           0 :         model_reset(&ctx->image_coder[i].vec_size_model);
     638           0 :         for (j = 0; j < 125; j++)
     639           0 :             model_reset(&ctx->image_coder[i].vq_model[j]);
     640           0 :         if (ctx->dct_coder[i].quality != quality) {
     641           0 :             ctx->dct_coder[i].quality = quality;
     642           0 :             ff_mss34_gen_quant_mat(ctx->dct_coder[i].qmat, quality, !i);
     643             :         }
     644           0 :         memset(ctx->dct_coder[i].prev_dc, 0,
     645             :                sizeof(*ctx->dct_coder[i].prev_dc) *
     646           0 :                ctx->dct_coder[i].prev_dc_stride *
     647           0 :                ctx->dct_coder[i].prev_dc_height);
     648           0 :         model_reset(&ctx->dct_coder[i].dc_model);
     649           0 :         model2_reset(&ctx->dct_coder[i].sign_model);
     650           0 :         model256_reset(&ctx->dct_coder[i].ac_model);
     651           0 :         if (ctx->haar_coder[i].quality != quality) {
     652           0 :             ctx->haar_coder[i].quality = quality;
     653           0 :             ctx->haar_coder[i].scale   = 17 - 7 * quality / 50;
     654             :         }
     655           0 :         model_reset(&ctx->haar_coder[i].coef_hi_model);
     656           0 :         model256_reset(&ctx->haar_coder[i].coef_model);
     657             :     }
     658           0 : }
     659             : 
     660           0 : static av_cold void init_coders(MSS3Context *ctx)
     661             : {
     662             :     int i, j;
     663             : 
     664           0 :     for (i = 0; i < 3; i++) {
     665           0 :         for (j = 0; j < 5; j++)
     666           0 :             model_init(&ctx->btype[i].bt_model[j], 5);
     667           0 :         model_init(&ctx->fill_coder[i].coef_model, 12);
     668           0 :         model256_init(&ctx->image_coder[i].esc_model);
     669           0 :         model256_init(&ctx->image_coder[i].vec_entry_model);
     670           0 :         model_init(&ctx->image_coder[i].vec_size_model, 3);
     671           0 :         for (j = 0; j < 125; j++)
     672           0 :             model_init(&ctx->image_coder[i].vq_model[j], 5);
     673           0 :         model_init(&ctx->dct_coder[i].dc_model, 12);
     674           0 :         model256_init(&ctx->dct_coder[i].ac_model);
     675           0 :         model_init(&ctx->haar_coder[i].coef_hi_model, 12);
     676           0 :         model256_init(&ctx->haar_coder[i].coef_model);
     677             :     }
     678           0 : }
     679             : 
     680           0 : static int mss3_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
     681             :                              AVPacket *avpkt)
     682             : {
     683           0 :     const uint8_t *buf = avpkt->data;
     684           0 :     int buf_size = avpkt->size;
     685           0 :     MSS3Context *c = avctx->priv_data;
     686           0 :     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           0 :     if (buf_size < HEADER_SIZE) {
     694           0 :         av_log(avctx, AV_LOG_ERROR,
     695             :                "Frame should have at least %d bytes, got %d instead\n",
     696             :                HEADER_SIZE, buf_size);
     697           0 :         return AVERROR_INVALIDDATA;
     698             :     }
     699             : 
     700           0 :     bytestream2_init(&gb, buf, buf_size);
     701           0 :     keyframe   = bytestream2_get_be32(&gb);
     702           0 :     if (keyframe & ~0x301) {
     703           0 :         av_log(avctx, AV_LOG_ERROR, "Invalid frame type %X\n", keyframe);
     704           0 :         return AVERROR_INVALIDDATA;
     705             :     }
     706           0 :     keyframe   = !(keyframe & 1);
     707           0 :     bytestream2_skip(&gb, 6);
     708           0 :     dec_x      = bytestream2_get_be16(&gb);
     709           0 :     dec_y      = bytestream2_get_be16(&gb);
     710           0 :     dec_width  = bytestream2_get_be16(&gb);
     711           0 :     dec_height = bytestream2_get_be16(&gb);
     712             : 
     713           0 :     if (dec_x + dec_width > avctx->width ||
     714           0 :         dec_y + dec_height > avctx->height ||
     715           0 :         (dec_width | dec_height) & 0xF) {
     716           0 :         av_log(avctx, AV_LOG_ERROR, "Invalid frame dimensions %dx%d +%d,%d\n",
     717             :                dec_width, dec_height, dec_x, dec_y);
     718           0 :         return AVERROR_INVALIDDATA;
     719             :     }
     720           0 :     bytestream2_skip(&gb, 4);
     721           0 :     quality    = bytestream2_get_byte(&gb);
     722           0 :     if (quality < 1 || quality > 100) {
     723           0 :         av_log(avctx, AV_LOG_ERROR, "Invalid quality setting %d\n", quality);
     724           0 :         return AVERROR_INVALIDDATA;
     725             :     }
     726           0 :     bytestream2_skip(&gb, 4);
     727             : 
     728           0 :     if (keyframe && !bytestream2_get_bytes_left(&gb)) {
     729           0 :         av_log(avctx, AV_LOG_ERROR, "Keyframe without data found\n");
     730           0 :         return AVERROR_INVALIDDATA;
     731             :     }
     732           0 :     if (!keyframe && c->got_error)
     733           0 :         return buf_size;
     734           0 :     c->got_error = 0;
     735             : 
     736           0 :     if ((ret = ff_reget_buffer(avctx, c->pic)) < 0)
     737           0 :         return ret;
     738           0 :     c->pic->key_frame = keyframe;
     739           0 :     c->pic->pict_type = keyframe ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
     740           0 :     if (!bytestream2_get_bytes_left(&gb)) {
     741           0 :         if ((ret = av_frame_ref(data, c->pic)) < 0)
     742           0 :             return ret;
     743           0 :         *got_frame      = 1;
     744             : 
     745           0 :         return buf_size;
     746             :     }
     747             : 
     748           0 :     reset_coders(c, quality);
     749             : 
     750           0 :     rac_init(acoder, buf + HEADER_SIZE, buf_size - HEADER_SIZE);
     751             : 
     752           0 :     mb_width  = dec_width  >> 4;
     753           0 :     mb_height = dec_height >> 4;
     754           0 :     dst[0] = c->pic->data[0] + dec_x     +  dec_y      * c->pic->linesize[0];
     755           0 :     dst[1] = c->pic->data[1] + dec_x / 2 + (dec_y / 2) * c->pic->linesize[1];
     756           0 :     dst[2] = c->pic->data[2] + dec_x / 2 + (dec_y / 2) * c->pic->linesize[2];
     757           0 :     for (y = 0; y < mb_height; y++) {
     758           0 :         for (x = 0; x < mb_width; x++) {
     759           0 :             for (i = 0; i < 3; i++) {
     760           0 :                 blk_size = 8 << !i;
     761             : 
     762           0 :                 btype = decode_block_type(acoder, c->btype + i);
     763           0 :                 switch (btype) {
     764           0 :                 case FILL_BLOCK:
     765           0 :                     decode_fill_block(acoder, c->fill_coder + i,
     766           0 :                                       dst[i] + x * blk_size,
     767           0 :                                       c->pic->linesize[i], blk_size);
     768           0 :                     break;
     769           0 :                 case IMAGE_BLOCK:
     770           0 :                     decode_image_block(acoder, c->image_coder + i,
     771           0 :                                        dst[i] + x * blk_size,
     772           0 :                                        c->pic->linesize[i], blk_size);
     773           0 :                     break;
     774           0 :                 case DCT_BLOCK:
     775           0 :                     decode_dct_block(acoder, c->dct_coder + i,
     776           0 :                                      dst[i] + x * blk_size,
     777           0 :                                      c->pic->linesize[i], blk_size,
     778           0 :                                      c->dctblock, x, y);
     779           0 :                     break;
     780           0 :                 case HAAR_BLOCK:
     781           0 :                     decode_haar_block(acoder, c->haar_coder + i,
     782           0 :                                       dst[i] + x * blk_size,
     783           0 :                                       c->pic->linesize[i], blk_size,
     784           0 :                                       c->hblock);
     785           0 :                     break;
     786             :                 }
     787           0 :                 if (c->got_error || acoder->got_error) {
     788           0 :                     av_log(avctx, AV_LOG_ERROR, "Error decoding block %d,%d\n",
     789             :                            x, y);
     790           0 :                     c->got_error = 1;
     791           0 :                     return AVERROR_INVALIDDATA;
     792             :                 }
     793             :             }
     794             :         }
     795           0 :         dst[0] += c->pic->linesize[0] * 16;
     796           0 :         dst[1] += c->pic->linesize[1] * 8;
     797           0 :         dst[2] += c->pic->linesize[2] * 8;
     798             :     }
     799             : 
     800           0 :     if ((ret = av_frame_ref(data, c->pic)) < 0)
     801           0 :         return ret;
     802             : 
     803           0 :     *got_frame      = 1;
     804             : 
     805           0 :     return buf_size;
     806             : }
     807             : 
     808           0 : static av_cold int mss3_decode_end(AVCodecContext *avctx)
     809             : {
     810           0 :     MSS3Context * const c = avctx->priv_data;
     811             :     int i;
     812             : 
     813           0 :     av_frame_free(&c->pic);
     814           0 :     for (i = 0; i < 3; i++)
     815           0 :         av_freep(&c->dct_coder[i].prev_dc);
     816             : 
     817           0 :     return 0;
     818             : }
     819             : 
     820           0 : static av_cold int mss3_decode_init(AVCodecContext *avctx)
     821             : {
     822           0 :     MSS3Context * const c = avctx->priv_data;
     823             :     int i;
     824             : 
     825           0 :     c->avctx = avctx;
     826             : 
     827           0 :     if ((avctx->width & 0xF) || (avctx->height & 0xF)) {
     828           0 :         av_log(avctx, AV_LOG_ERROR,
     829             :                "Image dimensions should be a multiple of 16.\n");
     830           0 :         return AVERROR_INVALIDDATA;
     831             :     }
     832             : 
     833           0 :     c->got_error = 0;
     834           0 :     for (i = 0; i < 3; i++) {
     835           0 :         int b_width  = avctx->width  >> (2 + !!i);
     836           0 :         int b_height = avctx->height >> (2 + !!i);
     837           0 :         c->dct_coder[i].prev_dc_stride = b_width;
     838           0 :         c->dct_coder[i].prev_dc_height = b_height;
     839           0 :         c->dct_coder[i].prev_dc = av_malloc(sizeof(*c->dct_coder[i].prev_dc) *
     840           0 :                                             b_width * b_height);
     841           0 :         if (!c->dct_coder[i].prev_dc) {
     842           0 :             av_log(avctx, AV_LOG_ERROR, "Cannot allocate buffer\n");
     843           0 :             av_frame_free(&c->pic);
     844           0 :             while (i >= 0) {
     845           0 :                 av_freep(&c->dct_coder[i].prev_dc);
     846           0 :                 i--;
     847             :             }
     848           0 :             return AVERROR(ENOMEM);
     849             :         }
     850             :     }
     851             : 
     852           0 :     c->pic = av_frame_alloc();
     853           0 :     if (!c->pic) {
     854           0 :         mss3_decode_end(avctx);
     855           0 :         return AVERROR(ENOMEM);
     856             :     }
     857             : 
     858           0 :     avctx->pix_fmt     = AV_PIX_FMT_YUV420P;
     859             : 
     860           0 :     init_coders(c);
     861             : 
     862           0 :     return 0;
     863             : }
     864             : 
     865             : AVCodec ff_msa1_decoder = {
     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),
     871             :     .init           = mss3_decode_init,
     872             :     .close          = mss3_decode_end,
     873             :     .decode         = mss3_decode_frame,
     874             :     .capabilities   = AV_CODEC_CAP_DR1,
     875             : };

Generated by: LCOV version 1.13