LCOV - code coverage report
Current view: top level - libavcodec - sheervideo.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 1364 0.0 %
Date: 2018-05-20 11:54:08 Functions: 0 29 0.0 %

          Line data    Source code
       1             : /*
       2             :  * BitJazz SheerVideo decoder
       3             :  * Copyright (c) 2016 Paul B Mahol
       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             : #include <stdio.h>
      23             : #include <stdlib.h>
      24             : #include <string.h>
      25             : 
      26             : #include "libavutil/intreadwrite.h"
      27             : #include "avcodec.h"
      28             : #include "get_bits.h"
      29             : #include "internal.h"
      30             : #include "thread.h"
      31             : #include "sheervideodata.h"
      32             : 
      33             : typedef struct SheerVideoContext {
      34             :     unsigned format;
      35             :     int alt;
      36             :     VLC vlc[2];
      37             :     void (*decode_frame)(AVCodecContext *avctx, AVFrame *p, GetBitContext *gb);
      38             : } SheerVideoContext;
      39             : 
      40           0 : static void decode_ca4i(AVCodecContext *avctx, AVFrame *p, GetBitContext *gb)
      41             : {
      42           0 :     SheerVideoContext *s = avctx->priv_data;
      43             :     uint16_t *dst_y, *dst_u, *dst_v, *dst_a;
      44             :     int x, y;
      45             : 
      46           0 :     dst_a = (uint16_t *)p->data[3];
      47           0 :     dst_y = (uint16_t *)p->data[0];
      48           0 :     dst_u = (uint16_t *)p->data[1];
      49           0 :     dst_v = (uint16_t *)p->data[2];
      50             : 
      51           0 :     for (y = 0; y < avctx->height; y++) {
      52           0 :         if (get_bits1(gb)) {
      53           0 :             for (x = 0; x < avctx->width; x++) {
      54           0 :                 dst_a[x] = get_bits(gb, 10);
      55           0 :                 dst_y[x] = get_bits(gb, 10);
      56           0 :                 dst_u[x] = get_bits(gb, 10);
      57           0 :                 dst_v[x] = get_bits(gb, 10);
      58             :             }
      59             :         } else {
      60           0 :             int pred[4] = { 502, 512, 512, 502 };
      61             : 
      62           0 :             for (x = 0; x < avctx->width; x++) {
      63             :                 int y, u, v, a;
      64             : 
      65           0 :                 a = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
      66           0 :                 y = get_vlc2(gb, s->vlc[0].table, s->vlc[0].bits, 2);
      67           0 :                 u = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
      68           0 :                 v = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
      69             : 
      70           0 :                 dst_a[x] = pred[3] = (a + pred[3]) & 0x3ff;
      71           0 :                 dst_y[x] = pred[0] = (y + pred[0]) & 0x3ff;
      72           0 :                 dst_u[x] = pred[1] = (u + pred[1]) & 0x3ff;
      73           0 :                 dst_v[x] = pred[2] = (v + pred[2]) & 0x3ff;
      74             :             }
      75             :         }
      76             : 
      77           0 :         dst_y += p->linesize[0] / 2;
      78           0 :         dst_u += p->linesize[1] / 2;
      79           0 :         dst_v += p->linesize[2] / 2;
      80           0 :         dst_a += p->linesize[3] / 2;
      81             :     }
      82           0 : }
      83             : 
      84           0 : static void decode_ca4p(AVCodecContext *avctx, AVFrame *p, GetBitContext *gb)
      85             : {
      86           0 :     SheerVideoContext *s = avctx->priv_data;
      87             :     uint16_t *dst_y, *dst_u, *dst_v, *dst_a;
      88             :     int x, y;
      89             : 
      90           0 :     dst_a = (uint16_t *)p->data[3];
      91           0 :     dst_y = (uint16_t *)p->data[0];
      92           0 :     dst_u = (uint16_t *)p->data[1];
      93           0 :     dst_v = (uint16_t *)p->data[2];
      94             : 
      95           0 :     if (get_bits1(gb)) {
      96           0 :         for (x = 0; x < avctx->width; x++) {
      97           0 :             dst_a[x] = get_bits(gb, 10);
      98           0 :             dst_y[x] = get_bits(gb, 10);
      99           0 :             dst_u[x] = get_bits(gb, 10);
     100           0 :             dst_v[x] = get_bits(gb, 10);
     101             :         }
     102             :     } else {
     103           0 :         int pred[4] = { 502, 512, 512, 502 };
     104             : 
     105           0 :         for (x = 0; x < avctx->width; x++) {
     106             :             int y, u, v, a;
     107             : 
     108           0 :             a = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
     109           0 :             y = get_vlc2(gb, s->vlc[0].table, s->vlc[0].bits, 2);
     110           0 :             u = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
     111           0 :             v = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
     112             : 
     113           0 :             dst_a[x] = pred[3] = (a + pred[3]) & 0x3ff;
     114           0 :             dst_y[x] = pred[0] = (y + pred[0]) & 0x3ff;
     115           0 :             dst_u[x] = pred[1] = (u + pred[1]) & 0x3ff;
     116           0 :             dst_v[x] = pred[2] = (v + pred[2]) & 0x3ff;
     117             :         }
     118             :     }
     119             : 
     120           0 :     dst_y += p->linesize[0] / 2;
     121           0 :     dst_u += p->linesize[1] / 2;
     122           0 :     dst_v += p->linesize[2] / 2;
     123           0 :     dst_a += p->linesize[3] / 2;
     124             : 
     125           0 :     for (y = 1; y < avctx->height; y++) {
     126           0 :         if (get_bits1(gb)) {
     127           0 :             for (x = 0; x < avctx->width; x++) {
     128           0 :                 dst_a[x] = get_bits(gb, 10);
     129           0 :                 dst_y[x] = get_bits(gb, 10);
     130           0 :                 dst_u[x] = get_bits(gb, 10);
     131           0 :                 dst_v[x] = get_bits(gb, 10);
     132             :             }
     133             :         } else {
     134             :             int pred_TL[4], pred_L[4], pred_T[4];
     135             :             int y, u, v, a;
     136             : 
     137           0 :             pred_TL[0] = pred_L[0] = dst_y[-p->linesize[0] / 2];
     138           0 :             pred_TL[1] = pred_L[1] = dst_u[-p->linesize[1] / 2];
     139           0 :             pred_TL[2] = pred_L[2] = dst_v[-p->linesize[2] / 2];
     140           0 :             pred_TL[3] = pred_L[3] = dst_a[-p->linesize[3] / 2];
     141             : 
     142           0 :             for (x = 0; x < avctx->width; x++) {
     143           0 :                 pred_T[0] = dst_y[-p->linesize[0] / 2 + x];
     144           0 :                 pred_T[1] = dst_u[-p->linesize[1] / 2 + x];
     145           0 :                 pred_T[2] = dst_v[-p->linesize[2] / 2 + x];
     146           0 :                 pred_T[3] = dst_a[-p->linesize[3] / 2 + x];
     147             : 
     148           0 :                 a = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
     149           0 :                 y = get_vlc2(gb, s->vlc[0].table, s->vlc[0].bits, 2);
     150           0 :                 u = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
     151           0 :                 v = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
     152             : 
     153           0 :                 dst_a[x] = pred_L[3] = (a + ((3 * (pred_T[3] + pred_L[3]) - 2 * pred_TL[3]) >> 2)) & 0x3ff;
     154           0 :                 dst_y[x] = pred_L[0] = (y + ((3 * (pred_T[0] + pred_L[0]) - 2 * pred_TL[0]) >> 2)) & 0x3ff;
     155           0 :                 dst_u[x] = pred_L[1] = (u + ((3 * (pred_T[1] + pred_L[1]) - 2 * pred_TL[1]) >> 2)) & 0x3ff;
     156           0 :                 dst_v[x] = pred_L[2] = (v + ((3 * (pred_T[2] + pred_L[2]) - 2 * pred_TL[2]) >> 2)) & 0x3ff;
     157             : 
     158           0 :                 pred_TL[0] = pred_T[0];
     159           0 :                 pred_TL[1] = pred_T[1];
     160           0 :                 pred_TL[2] = pred_T[2];
     161           0 :                 pred_TL[3] = pred_T[3];
     162             :             }
     163             :         }
     164             : 
     165           0 :         dst_y += p->linesize[0] / 2;
     166           0 :         dst_u += p->linesize[1] / 2;
     167           0 :         dst_v += p->linesize[2] / 2;
     168           0 :         dst_a += p->linesize[3] / 2;
     169             :     }
     170           0 : }
     171             : 
     172           0 : static void decode_ybr10i(AVCodecContext *avctx, AVFrame *p, GetBitContext *gb)
     173             : {
     174           0 :     SheerVideoContext *s = avctx->priv_data;
     175             :     uint16_t *dst_y, *dst_u, *dst_v;
     176             :     int x, y;
     177             : 
     178           0 :     dst_y = (uint16_t *)p->data[0];
     179           0 :     dst_u = (uint16_t *)p->data[1];
     180           0 :     dst_v = (uint16_t *)p->data[2];
     181             : 
     182           0 :     for (y = 0; y < avctx->height; y++) {
     183           0 :         if (get_bits1(gb)) {
     184           0 :             for (x = 0; x < avctx->width; x++) {
     185           0 :                 dst_y[x] = get_bits(gb, 10);
     186           0 :                 dst_u[x] = get_bits(gb, 10);
     187           0 :                 dst_v[x] = get_bits(gb, 10);
     188             :             }
     189             :         } else {
     190           0 :             int pred[4] = { 502, 512, 512, 512 };
     191             : 
     192           0 :             for (x = 0; x < avctx->width; x++) {
     193             :                 int y, u, v;
     194             : 
     195           0 :                 y = get_vlc2(gb, s->vlc[0].table, s->vlc[0].bits, 2);
     196           0 :                 u = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
     197           0 :                 v = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
     198             : 
     199           0 :                 dst_y[x] = pred[0] = (y + pred[0]) & 0x3ff;
     200           0 :                 dst_u[x] = pred[1] = (u + pred[1]) & 0x3ff;
     201           0 :                 dst_v[x] = pred[2] = (v + pred[2]) & 0x3ff;
     202             :             }
     203             :         }
     204             : 
     205           0 :         dst_y += p->linesize[0] / 2;
     206           0 :         dst_u += p->linesize[1] / 2;
     207           0 :         dst_v += p->linesize[2] / 2;
     208             :     }
     209           0 : }
     210             : 
     211           0 : static void decode_ybr10(AVCodecContext *avctx, AVFrame *p, GetBitContext *gb)
     212             : {
     213           0 :     SheerVideoContext *s = avctx->priv_data;
     214             :     uint16_t *dst_y, *dst_u, *dst_v;
     215             :     int x, y;
     216             : 
     217           0 :     dst_y = (uint16_t *)p->data[0];
     218           0 :     dst_u = (uint16_t *)p->data[1];
     219           0 :     dst_v = (uint16_t *)p->data[2];
     220             : 
     221           0 :     if (get_bits1(gb)) {
     222           0 :         for (x = 0; x < avctx->width; x++) {
     223           0 :             dst_y[x] = get_bits(gb, 10);
     224           0 :             dst_u[x] = get_bits(gb, 10);
     225           0 :             dst_v[x] = get_bits(gb, 10);
     226             :         }
     227             :     } else {
     228           0 :         int pred[4] = { 502, 512, 512, 512 };
     229             : 
     230           0 :         for (x = 0; x < avctx->width; x++) {
     231             :             int y, u, v;
     232             : 
     233           0 :             y = get_vlc2(gb, s->vlc[0].table, s->vlc[0].bits, 2);
     234           0 :             u = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
     235           0 :             v = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
     236             : 
     237           0 :             dst_y[x] = pred[0] = (y + pred[0]) & 0x3ff;
     238           0 :             dst_u[x] = pred[1] = (u + pred[1]) & 0x3ff;
     239           0 :             dst_v[x] = pred[2] = (v + pred[2]) & 0x3ff;
     240             :         }
     241             :     }
     242             : 
     243           0 :     dst_y += p->linesize[0] / 2;
     244           0 :     dst_u += p->linesize[1] / 2;
     245           0 :     dst_v += p->linesize[2] / 2;
     246             : 
     247           0 :     for (y = 1; y < avctx->height; y++) {
     248           0 :         if (get_bits1(gb)) {
     249           0 :             for (x = 0; x < avctx->width; x++) {
     250           0 :                 dst_y[x] = get_bits(gb, 10);
     251           0 :                 dst_u[x] = get_bits(gb, 10);
     252           0 :                 dst_v[x] = get_bits(gb, 10);
     253             :             }
     254             :         } else {
     255             :             int pred_TL[4], pred_L[4], pred_T[4];
     256             :             int y, u, v;
     257             : 
     258           0 :             pred_TL[0] = pred_L[0] = dst_y[-p->linesize[0] / 2];
     259           0 :             pred_TL[1] = pred_L[1] = dst_u[-p->linesize[1] / 2];
     260           0 :             pred_TL[2] = pred_L[2] = dst_v[-p->linesize[2] / 2];
     261             : 
     262           0 :             for (x = 0; x < avctx->width; x++) {
     263           0 :                 pred_T[0] = dst_y[-p->linesize[0] / 2 + x];
     264           0 :                 pred_T[1] = dst_u[-p->linesize[1] / 2 + x];
     265           0 :                 pred_T[2] = dst_v[-p->linesize[2] / 2 + x];
     266             : 
     267           0 :                 y = get_vlc2(gb, s->vlc[0].table, s->vlc[0].bits, 2);
     268           0 :                 u = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
     269           0 :                 v = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
     270             : 
     271           0 :                 dst_y[x] = pred_L[0] = (y + ((3 * (pred_T[0] + pred_L[0]) - 2 * pred_TL[0]) >> 2)) & 0x3ff;
     272           0 :                 dst_u[x] = pred_L[1] = (u + ((3 * (pred_T[1] + pred_L[1]) - 2 * pred_TL[1]) >> 2)) & 0x3ff;
     273           0 :                 dst_v[x] = pred_L[2] = (v + ((3 * (pred_T[2] + pred_L[2]) - 2 * pred_TL[2]) >> 2)) & 0x3ff;
     274             : 
     275           0 :                 pred_TL[0] = pred_T[0];
     276           0 :                 pred_TL[1] = pred_T[1];
     277           0 :                 pred_TL[2] = pred_T[2];
     278             :             }
     279             :         }
     280             : 
     281           0 :         dst_y += p->linesize[0] / 2;
     282           0 :         dst_u += p->linesize[1] / 2;
     283           0 :         dst_v += p->linesize[2] / 2;
     284             :     }
     285           0 : }
     286             : 
     287           0 : static void decode_yry10i(AVCodecContext *avctx, AVFrame *p, GetBitContext *gb)
     288             : {
     289           0 :     SheerVideoContext *s = avctx->priv_data;
     290             :     uint16_t *dst_y, *dst_u, *dst_v;
     291             :     int x, y;
     292             : 
     293           0 :     dst_y = (uint16_t *)p->data[0];
     294           0 :     dst_u = (uint16_t *)p->data[1];
     295           0 :     dst_v = (uint16_t *)p->data[2];
     296             : 
     297           0 :     for (y = 0; y < avctx->height; y++) {
     298           0 :         if (get_bits1(gb)) {
     299           0 :             for (x = 0; x < avctx->width; x += 2) {
     300           0 :                 dst_y[x    ] = get_bits(gb, 10);
     301           0 :                 dst_u[x / 2] = get_bits(gb, 10);
     302           0 :                 dst_y[x + 1] = get_bits(gb, 10);
     303           0 :                 dst_v[x / 2] = get_bits(gb, 10);
     304             :             }
     305             :         } else {
     306           0 :             int pred[4] = { 502, 512, 512, 0 };
     307             : 
     308           0 :             for (x = 0; x < avctx->width; x += 2) {
     309             :                 int y1, y2, u, v;
     310             : 
     311           0 :                 y1 = get_vlc2(gb, s->vlc[0].table, s->vlc[0].bits, 2);
     312           0 :                 u  = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
     313           0 :                 y2 = get_vlc2(gb, s->vlc[0].table, s->vlc[0].bits, 2);
     314           0 :                 v  = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
     315             : 
     316           0 :                 dst_y[x    ] = pred[0] = (y1 + pred[0]) & 0x3ff;
     317           0 :                 dst_u[x / 2] = pred[1] = (u  + pred[1]) & 0x3ff;
     318           0 :                 dst_y[x + 1] = pred[0] = (y2 + pred[0]) & 0x3ff;
     319           0 :                 dst_v[x / 2] = pred[2] = (v  + pred[2]) & 0x3ff;
     320             :             }
     321             :         }
     322             : 
     323           0 :         dst_y += p->linesize[0] / 2;
     324           0 :         dst_u += p->linesize[1] / 2;
     325           0 :         dst_v += p->linesize[2] / 2;
     326             :     }
     327           0 : }
     328             : 
     329           0 : static void decode_yry10(AVCodecContext *avctx, AVFrame *p, GetBitContext *gb)
     330             : {
     331           0 :     SheerVideoContext *s = avctx->priv_data;
     332             :     uint16_t *dst_y, *dst_u, *dst_v;
     333             :     int x, y;
     334             : 
     335           0 :     dst_y = (uint16_t *)p->data[0];
     336           0 :     dst_u = (uint16_t *)p->data[1];
     337           0 :     dst_v = (uint16_t *)p->data[2];
     338             : 
     339           0 :     if (get_bits1(gb)) {
     340           0 :         for (x = 0; x < avctx->width; x += 2) {
     341           0 :             dst_y[x    ] = get_bits(gb, 10);
     342           0 :             dst_u[x / 2] = get_bits(gb, 10);
     343           0 :             dst_y[x + 1] = get_bits(gb, 10);
     344           0 :             dst_v[x / 2] = get_bits(gb, 10);
     345             :         }
     346             :     } else {
     347           0 :         int pred[4] = { 502, 512, 512, 0 };
     348             : 
     349           0 :         for (x = 0; x < avctx->width; x += 2) {
     350             :             int y1, y2, u, v;
     351             : 
     352           0 :             y1 = get_vlc2(gb, s->vlc[0].table, s->vlc[0].bits, 2);
     353           0 :             u  = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
     354           0 :             y2 = get_vlc2(gb, s->vlc[0].table, s->vlc[0].bits, 2);
     355           0 :             v  = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
     356             : 
     357           0 :             dst_y[x    ] = pred[0] = (y1 + pred[0]) & 0x3ff;
     358           0 :             dst_u[x / 2] = pred[1] = (u  + pred[1]) & 0x3ff;
     359           0 :             dst_y[x + 1] = pred[0] = (y2 + pred[0]) & 0x3ff;
     360           0 :             dst_v[x / 2] = pred[2] = (v  + pred[2]) & 0x3ff;
     361             :         }
     362             :     }
     363             : 
     364           0 :     dst_y += p->linesize[0] / 2;
     365           0 :     dst_u += p->linesize[1] / 2;
     366           0 :     dst_v += p->linesize[2] / 2;
     367             : 
     368           0 :     for (y = 1; y < avctx->height; y++) {
     369           0 :         if (get_bits1(gb)) {
     370           0 :             for (x = 0; x < avctx->width; x += 2) {
     371           0 :                 dst_y[x    ] = get_bits(gb, 10);
     372           0 :                 dst_u[x / 2] = get_bits(gb, 10);
     373           0 :                 dst_y[x + 1] = get_bits(gb, 10);
     374           0 :                 dst_v[x / 2] = get_bits(gb, 10);
     375             :             }
     376             :         } else {
     377             :             int pred_TL[6], pred_L[6], pred_T[6];
     378             :             int y1, y2, u, v;
     379             : 
     380           0 :             pred_TL[0] = pred_L[0] = dst_y[-p->linesize[0] / 2];
     381           0 :             pred_TL[1] = pred_L[1] = dst_u[-p->linesize[1] / 2];
     382           0 :             pred_TL[2] = pred_L[2] = dst_v[-p->linesize[2] / 2];
     383             : 
     384           0 :             for (x = 0; x < avctx->width; x += 2) {
     385           0 :                 pred_T[0] = dst_y[-p->linesize[0] / 2 + x];
     386           0 :                 pred_T[3] = dst_y[-p->linesize[0] / 2 + x + 1];
     387           0 :                 pred_T[1] = dst_u[-p->linesize[1] / 2 + x / 2];
     388           0 :                 pred_T[2] = dst_v[-p->linesize[2] / 2 + x / 2];
     389             : 
     390           0 :                 y1 = get_vlc2(gb, s->vlc[0].table, s->vlc[0].bits, 2);
     391           0 :                 u  = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
     392           0 :                 y2 = get_vlc2(gb, s->vlc[0].table, s->vlc[0].bits, 2);
     393           0 :                 v  = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
     394             : 
     395           0 :                 dst_y[x    ] = pred_L[0] = (y1 + ((3 * (pred_T[0] + pred_L[0]) - 2 * pred_TL[0]) >> 2)) & 0x3ff;
     396           0 :                 dst_u[x / 2] = pred_L[1] = (u + (((pred_L[1] - pred_TL[1]) >> 1) + pred_T[1])) & 0x3ff;
     397           0 :                 dst_y[x + 1] = pred_L[0] = (y2 + ((3 * (pred_T[3] + pred_L[0]) - 2 * pred_T[0]) >> 2)) & 0x3ff;
     398           0 :                 dst_v[x / 2] = pred_L[2] = (v + (((pred_L[2] - pred_TL[2]) >> 1) + pred_T[2])) & 0x3ff;
     399             : 
     400           0 :                 pred_TL[0] = pred_T[3];
     401           0 :                 pred_TL[1] = pred_T[1];
     402           0 :                 pred_TL[2] = pred_T[2];
     403             :             }
     404             :         }
     405             : 
     406           0 :         dst_y += p->linesize[0] / 2;
     407           0 :         dst_u += p->linesize[1] / 2;
     408           0 :         dst_v += p->linesize[2] / 2;
     409             :     }
     410           0 : }
     411             : 
     412           0 : static void decode_ca2i(AVCodecContext *avctx, AVFrame *p, GetBitContext *gb)
     413             : {
     414           0 :     SheerVideoContext *s = avctx->priv_data;
     415             :     uint16_t *dst_y, *dst_u, *dst_v, *dst_a;
     416             :     int x, y;
     417             : 
     418           0 :     dst_y = (uint16_t *)p->data[0];
     419           0 :     dst_u = (uint16_t *)p->data[1];
     420           0 :     dst_v = (uint16_t *)p->data[2];
     421           0 :     dst_a = (uint16_t *)p->data[3];
     422             : 
     423           0 :     for (y = 0; y < avctx->height; y++) {
     424           0 :         if (get_bits1(gb)) {
     425           0 :             for (x = 0; x < avctx->width; x += 2) {
     426           0 :                 dst_a[x    ] = get_bits(gb, 10);
     427           0 :                 dst_y[x    ] = get_bits(gb, 10);
     428           0 :                 dst_u[x / 2] = get_bits(gb, 10);
     429           0 :                 dst_a[x + 1] = get_bits(gb, 10);
     430           0 :                 dst_y[x + 1] = get_bits(gb, 10);
     431           0 :                 dst_v[x / 2] = get_bits(gb, 10);
     432             :             }
     433             :         } else {
     434           0 :             int pred[4] = { 502, 512, 512, 502 };
     435             : 
     436           0 :             for (x = 0; x < avctx->width; x += 2) {
     437             :                 int y1, y2, u, v, a1, a2;
     438             : 
     439           0 :                 a1 = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
     440           0 :                 y1 = get_vlc2(gb, s->vlc[0].table, s->vlc[0].bits, 2);
     441           0 :                 u  = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
     442           0 :                 a2 = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
     443           0 :                 y2 = get_vlc2(gb, s->vlc[0].table, s->vlc[0].bits, 2);
     444           0 :                 v  = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
     445             : 
     446           0 :                 dst_y[x    ] = pred[0] = (y1 + pred[0]) & 0x3ff;
     447           0 :                 dst_u[x / 2] = pred[1] = (u  + pred[1]) & 0x3ff;
     448           0 :                 dst_y[x + 1] = pred[0] = (y2 + pred[0]) & 0x3ff;
     449           0 :                 dst_a[x    ] = pred[3] = (a1 + pred[3]) & 0x3ff;
     450           0 :                 dst_v[x / 2] = pred[2] = (v  + pred[2]) & 0x3ff;
     451           0 :                 dst_a[x + 1] = pred[3] = (a2 + pred[3]) & 0x3ff;
     452             :             }
     453             :         }
     454             : 
     455           0 :         dst_y += p->linesize[0] / 2;
     456           0 :         dst_u += p->linesize[1] / 2;
     457           0 :         dst_v += p->linesize[2] / 2;
     458           0 :         dst_a += p->linesize[3] / 2;
     459             :     }
     460           0 : }
     461             : 
     462           0 : static void decode_ca2p(AVCodecContext *avctx, AVFrame *p, GetBitContext *gb)
     463             : {
     464           0 :     SheerVideoContext *s = avctx->priv_data;
     465             :     uint16_t *dst_y, *dst_u, *dst_v, *dst_a;
     466             :     int x, y;
     467             : 
     468           0 :     dst_y = (uint16_t *)p->data[0];
     469           0 :     dst_u = (uint16_t *)p->data[1];
     470           0 :     dst_v = (uint16_t *)p->data[2];
     471           0 :     dst_a = (uint16_t *)p->data[3];
     472             : 
     473           0 :     if (get_bits1(gb)) {
     474           0 :         for (x = 0; x < avctx->width; x += 2) {
     475           0 :             dst_a[x    ] = get_bits(gb, 10);
     476           0 :             dst_y[x    ] = get_bits(gb, 10);
     477           0 :             dst_u[x / 2] = get_bits(gb, 10);
     478           0 :             dst_a[x + 1] = get_bits(gb, 10);
     479           0 :             dst_y[x + 1] = get_bits(gb, 10);
     480           0 :             dst_v[x / 2] = get_bits(gb, 10);
     481             :         }
     482             :     } else {
     483           0 :         int pred[4] = { 502, 512, 512, 502 };
     484             : 
     485           0 :         for (x = 0; x < avctx->width; x += 2) {
     486             :             int y1, y2, u, v, a1, a2;
     487             : 
     488           0 :             a1 = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
     489           0 :             y1 = get_vlc2(gb, s->vlc[0].table, s->vlc[0].bits, 2);
     490           0 :             u  = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
     491           0 :             a2 = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
     492           0 :             y2 = get_vlc2(gb, s->vlc[0].table, s->vlc[0].bits, 2);
     493           0 :             v  = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
     494             : 
     495           0 :             dst_y[x    ] = pred[0] = (y1 + pred[0]) & 0x3ff;
     496           0 :             dst_u[x / 2] = pred[1] = (u  + pred[1]) & 0x3ff;
     497           0 :             dst_y[x + 1] = pred[0] = (y2 + pred[0]) & 0x3ff;
     498           0 :             dst_a[x    ] = pred[3] = (a1 + pred[3]) & 0x3ff;
     499           0 :             dst_v[x / 2] = pred[2] = (v  + pred[2]) & 0x3ff;
     500           0 :             dst_a[x + 1] = pred[3] = (a2 + pred[3]) & 0x3ff;
     501             :         }
     502             :     }
     503             : 
     504           0 :     dst_y += p->linesize[0] / 2;
     505           0 :     dst_u += p->linesize[1] / 2;
     506           0 :     dst_v += p->linesize[2] / 2;
     507           0 :     dst_a += p->linesize[3] / 2;
     508             : 
     509           0 :     for (y = 1; y < avctx->height; y++) {
     510           0 :         if (get_bits1(gb)) {
     511           0 :             for (x = 0; x < avctx->width; x += 2) {
     512           0 :                 dst_a[x    ] = get_bits(gb, 10);
     513           0 :                 dst_y[x    ] = get_bits(gb, 10);
     514           0 :                 dst_u[x / 2] = get_bits(gb, 10);
     515           0 :                 dst_a[x + 1] = get_bits(gb, 10);
     516           0 :                 dst_y[x + 1] = get_bits(gb, 10);
     517           0 :                 dst_v[x / 2] = get_bits(gb, 10);
     518             :             }
     519             :         } else {
     520             :             int pred_TL[6], pred_L[6], pred_T[6];
     521             :             int y1, y2, u, v, a1, a2;
     522             : 
     523           0 :             pred_TL[0] = pred_L[0] = dst_y[-p->linesize[0] / 2];
     524           0 :             pred_TL[1] = pred_L[1] = dst_u[-p->linesize[1] / 2];
     525           0 :             pred_TL[2] = pred_L[2] = dst_v[-p->linesize[2] / 2];
     526           0 :             pred_TL[4] = pred_L[4] = dst_a[-p->linesize[3] / 2];
     527             : 
     528           0 :             for (x = 0; x < avctx->width; x += 2) {
     529           0 :                 pred_T[0] = dst_y[-p->linesize[0] / 2 + x];
     530           0 :                 pred_T[3] = dst_y[-p->linesize[0] / 2 + x + 1];
     531           0 :                 pred_T[1] = dst_u[-p->linesize[1] / 2 + x / 2];
     532           0 :                 pred_T[2] = dst_v[-p->linesize[2] / 2 + x / 2];
     533           0 :                 pred_T[4] = dst_a[-p->linesize[3] / 2 + x];
     534           0 :                 pred_T[5] = dst_a[-p->linesize[3] / 2 + x + 1];
     535             : 
     536           0 :                 a1 = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
     537           0 :                 y1 = get_vlc2(gb, s->vlc[0].table, s->vlc[0].bits, 2);
     538           0 :                 u  = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
     539           0 :                 a2 = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
     540           0 :                 y2 = get_vlc2(gb, s->vlc[0].table, s->vlc[0].bits, 2);
     541           0 :                 v  = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
     542             : 
     543           0 :                 dst_y[x    ] = pred_L[0] = (y1 + ((3 * (pred_T[0] + pred_L[0]) - 2 * pred_TL[0]) >> 2)) & 0x3ff;
     544           0 :                 dst_u[x / 2] = pred_L[1] = (u + (((pred_L[1] - pred_TL[1]) >> 1) + pred_T[1])) & 0x3ff;
     545           0 :                 dst_y[x + 1] = pred_L[0] = (y2 + ((3 * (pred_T[3] + pred_L[0]) - 2 * pred_T[0]) >> 2)) & 0x3ff;
     546           0 :                 dst_v[x / 2] = pred_L[2] = (v + (((pred_L[2] - pred_TL[2]) >> 1) + pred_T[2])) & 0x3ff;
     547           0 :                 dst_a[x    ] = pred_L[4] = (a1 + ((3 * (pred_T[4] + pred_L[4]) - 2 * pred_TL[4]) >> 2)) & 0x3ff;
     548           0 :                 dst_a[x + 1] = pred_L[4] = (a2 + ((3 * (pred_T[5] + pred_L[4]) - 2 * pred_T[4]) >> 2)) & 0x3ff;
     549             : 
     550           0 :                 pred_TL[0] = pred_T[3];
     551           0 :                 pred_TL[1] = pred_T[1];
     552           0 :                 pred_TL[2] = pred_T[2];
     553           0 :                 pred_TL[4] = pred_T[5];
     554             :             }
     555             :         }
     556             : 
     557           0 :         dst_y += p->linesize[0] / 2;
     558           0 :         dst_u += p->linesize[1] / 2;
     559           0 :         dst_v += p->linesize[2] / 2;
     560           0 :         dst_a += p->linesize[3] / 2;
     561             :     }
     562           0 : }
     563             : 
     564           0 : static void decode_c82i(AVCodecContext *avctx, AVFrame *p, GetBitContext *gb)
     565             : {
     566           0 :     SheerVideoContext *s = avctx->priv_data;
     567             :     uint8_t *dst_y, *dst_u, *dst_v, *dst_a;
     568             :     int x, y;
     569             : 
     570           0 :     dst_y = p->data[0];
     571           0 :     dst_u = p->data[1];
     572           0 :     dst_v = p->data[2];
     573           0 :     dst_a = p->data[3];
     574             : 
     575           0 :     for (y = 0; y < avctx->height; y += 1) {
     576           0 :         if (get_bits1(gb)) {
     577           0 :             for (x = 0; x < avctx->width; x += 2) {
     578           0 :                 dst_a[x    ] = get_bits(gb, 8);
     579           0 :                 dst_y[x    ] = get_bits(gb, 8);
     580           0 :                 dst_u[x / 2] = get_bits(gb, 8);
     581           0 :                 dst_a[x + 1] = get_bits(gb, 8);
     582           0 :                 dst_y[x + 1] = get_bits(gb, 8);
     583           0 :                 dst_v[x / 2] = get_bits(gb, 8);
     584             :             }
     585             :         } else {
     586           0 :             int pred[4] = { 125, -128, -128, 125 };
     587             : 
     588           0 :             for (x = 0; x < avctx->width; x += 2) {
     589             :                 int y1, y2, u, v, a1, a2;
     590             : 
     591           0 :                 a1 = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
     592           0 :                 y1 = get_vlc2(gb, s->vlc[0].table, s->vlc[0].bits, 2);
     593           0 :                 u  = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
     594           0 :                 a2 = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
     595           0 :                 y2 = get_vlc2(gb, s->vlc[0].table, s->vlc[0].bits, 2);
     596           0 :                 v  = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
     597             : 
     598           0 :                 dst_y[x    ] = pred[0] = (y1 + pred[0]) & 0xff;
     599           0 :                 dst_y[x + 1] = pred[0] = (y2 + pred[0]) & 0xff;
     600           0 :                 dst_u[x / 2] = pred[1] = (u  + pred[1]) & 0xff;
     601           0 :                 dst_v[x / 2] = pred[2] = (v  + pred[2]) & 0xff;
     602           0 :                 dst_a[x    ] = pred[3] = (a1 + pred[3]) & 0xff;
     603           0 :                 dst_a[x + 1] = pred[3] = (a2 + pred[3]) & 0xff;
     604             :             }
     605             :         }
     606             : 
     607           0 :         dst_y += p->linesize[0];
     608           0 :         dst_u += p->linesize[1];
     609           0 :         dst_v += p->linesize[2];
     610           0 :         dst_a += p->linesize[3];
     611             :     }
     612           0 : }
     613             : 
     614           0 : static void decode_c82p(AVCodecContext *avctx, AVFrame *p, GetBitContext *gb)
     615             : {
     616           0 :     SheerVideoContext *s = avctx->priv_data;
     617             :     uint8_t *dst_y, *dst_u, *dst_v, *dst_a;
     618             :     int x, y;
     619             : 
     620           0 :     dst_y = p->data[0];
     621           0 :     dst_u = p->data[1];
     622           0 :     dst_v = p->data[2];
     623           0 :     dst_a = p->data[3];
     624             : 
     625           0 :     if (get_bits1(gb)) {
     626           0 :         for (x = 0; x < avctx->width; x += 2) {
     627           0 :             dst_a[x    ] = get_bits(gb, 8);
     628           0 :             dst_y[x    ] = get_bits(gb, 8);
     629           0 :             dst_u[x / 2] = get_bits(gb, 8);
     630           0 :             dst_a[x + 1] = get_bits(gb, 8);
     631           0 :             dst_y[x + 1] = get_bits(gb, 8);
     632           0 :             dst_v[x / 2] = get_bits(gb, 8);
     633             :         }
     634             :     } else {
     635           0 :         int pred[4] = { 125, -128, -128, 125 };
     636             : 
     637           0 :         for (x = 0; x < avctx->width; x += 2) {
     638             :             int y1, y2, u, v, a1, a2;
     639             : 
     640           0 :             a1 = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
     641           0 :             y1 = get_vlc2(gb, s->vlc[0].table, s->vlc[0].bits, 2);
     642           0 :             u  = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
     643           0 :             a2 = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
     644           0 :             y2 = get_vlc2(gb, s->vlc[0].table, s->vlc[0].bits, 2);
     645           0 :             v  = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
     646             : 
     647           0 :             dst_y[x    ] = pred[0] = (y1 + pred[0]) & 0xff;
     648           0 :             dst_u[x / 2] = pred[1] = (u  + pred[1]) & 0xff;
     649           0 :             dst_y[x + 1] = pred[0] = (y2 + pred[0]) & 0xff;
     650           0 :             dst_a[x    ] = pred[3] = (a1 + pred[3]) & 0xff;
     651           0 :             dst_v[x / 2] = pred[2] = (v  + pred[2]) & 0xff;
     652           0 :             dst_a[x + 1] = pred[3] = (a2 + pred[3]) & 0xff;
     653             :         }
     654             :     }
     655             : 
     656           0 :     dst_y += p->linesize[0];
     657           0 :     dst_u += p->linesize[1];
     658           0 :     dst_v += p->linesize[2];
     659           0 :     dst_a += p->linesize[3];
     660             : 
     661           0 :     for (y = 1; y < avctx->height; y++) {
     662           0 :         if (get_bits1(gb)) {
     663           0 :             for (x = 0; x < avctx->width; x += 2) {
     664           0 :                 dst_a[x    ] = get_bits(gb, 8);
     665           0 :                 dst_y[x    ] = get_bits(gb, 8);
     666           0 :                 dst_u[x / 2] = get_bits(gb, 8);
     667           0 :                 dst_a[x + 1] = get_bits(gb, 8);
     668           0 :                 dst_y[x + 1] = get_bits(gb, 8);
     669           0 :                 dst_v[x / 2] = get_bits(gb, 8);
     670             :             }
     671             :         } else {
     672             :             int pred_TL[6], pred_L[6], pred_T[6];
     673             :             int y1, y2, u, v, a1, a2;
     674             : 
     675           0 :             pred_TL[0] = pred_L[0] = dst_y[-p->linesize[0]];
     676           0 :             pred_TL[1] = pred_L[1] = dst_u[-p->linesize[1]];
     677           0 :             pred_TL[2] = pred_L[2] = dst_v[-p->linesize[2]];
     678           0 :             pred_TL[4] = pred_L[4] = dst_a[-p->linesize[3]];
     679             : 
     680           0 :             for (x = 0; x < avctx->width; x += 2) {
     681           0 :                 pred_T[0] = dst_y[-p->linesize[0] + x];
     682           0 :                 pred_T[3] = dst_y[-p->linesize[0] + x + 1];
     683           0 :                 pred_T[1] = dst_u[-p->linesize[1] + x / 2];
     684           0 :                 pred_T[2] = dst_v[-p->linesize[2] + x / 2];
     685           0 :                 pred_T[4] = dst_a[-p->linesize[3] + x];
     686           0 :                 pred_T[5] = dst_a[-p->linesize[3] + x + 1];
     687             : 
     688           0 :                 a1 = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
     689           0 :                 y1 = get_vlc2(gb, s->vlc[0].table, s->vlc[0].bits, 2);
     690           0 :                 u  = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
     691           0 :                 a2 = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
     692           0 :                 y2 = get_vlc2(gb, s->vlc[0].table, s->vlc[0].bits, 2);
     693           0 :                 v  = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
     694             : 
     695           0 :                 dst_y[x    ] = pred_L[0] = (y1 + ((3 * (pred_T[0] + pred_L[0]) - 2 * pred_TL[0]) >> 2)) & 0xff;
     696           0 :                 dst_u[x / 2] = pred_L[1] = (u + (((pred_L[1] - pred_TL[1]) >> 1) + pred_T[1])) & 0xff;
     697           0 :                 dst_y[x + 1] = pred_L[0] = (y2 + ((3 * (pred_T[3] + pred_L[0]) - 2 * pred_T[0]) >> 2)) & 0xff;
     698           0 :                 dst_v[x / 2] = pred_L[2] = (v + (((pred_L[2] - pred_TL[2]) >> 1) + pred_T[2])) & 0xff;
     699           0 :                 dst_a[x    ] = pred_L[4] = (a1 + ((3 * (pred_T[4] + pred_L[4]) - 2 * pred_TL[4]) >> 2)) & 0xff;
     700           0 :                 dst_a[x + 1] = pred_L[4] = (a2 + ((3 * (pred_T[5] + pred_L[4]) - 2 * pred_T[4]) >> 2)) & 0xff;
     701             : 
     702           0 :                 pred_TL[0] = pred_T[3];
     703           0 :                 pred_TL[1] = pred_T[1];
     704           0 :                 pred_TL[2] = pred_T[2];
     705           0 :                 pred_TL[4] = pred_T[5];
     706             :             }
     707             :         }
     708             : 
     709           0 :         dst_y += p->linesize[0];
     710           0 :         dst_u += p->linesize[1];
     711           0 :         dst_v += p->linesize[2];
     712           0 :         dst_a += p->linesize[3];
     713             :     }
     714           0 : }
     715             : 
     716           0 : static void decode_ybyr(AVCodecContext *avctx, AVFrame *p, GetBitContext *gb)
     717             : {
     718           0 :     SheerVideoContext *s = avctx->priv_data;
     719             :     uint8_t *dst_y, *dst_u, *dst_v;
     720             :     int x, y;
     721             : 
     722           0 :     dst_y = p->data[0];
     723           0 :     dst_u = p->data[1];
     724           0 :     dst_v = p->data[2];
     725             : 
     726           0 :     if (get_bits1(gb)) {
     727           0 :         for (x = 0; x < avctx->width; x += 2) {
     728           0 :             dst_y[x    ] = get_bits(gb, 8);
     729           0 :             dst_u[x / 2] = get_bits(gb, 8) + 128;
     730           0 :             dst_y[x + 1] = get_bits(gb, 8);
     731           0 :             dst_v[x / 2] = get_bits(gb, 8) + 128;
     732             :         }
     733             :     } else {
     734           0 :         int pred[4] = { -128, 128, 128, 0 };
     735             : 
     736           0 :         for (x = 0; x < avctx->width; x += 2) {
     737             :             int y1, y2, u, v;
     738             : 
     739           0 :             y1 = get_vlc2(gb, s->vlc[0].table, s->vlc[0].bits, 2);
     740           0 :             u  = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
     741           0 :             y2 = get_vlc2(gb, s->vlc[0].table, s->vlc[0].bits, 2);
     742           0 :             v  = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
     743             : 
     744           0 :             dst_y[x    ] = pred[0] = (y1 + pred[0]) & 0xff;
     745           0 :             dst_u[x / 2] = pred[1] = (u  + pred[1]) & 0xff;
     746           0 :             dst_y[x + 1] = pred[0] = (y2 + pred[0]) & 0xff;
     747           0 :             dst_v[x / 2] = pred[2] = (v  + pred[2]) & 0xff;
     748             :         }
     749             :     }
     750             : 
     751           0 :     dst_y += p->linesize[0];
     752           0 :     dst_u += p->linesize[1];
     753           0 :     dst_v += p->linesize[2];
     754             : 
     755           0 :     for (y = 1; y < avctx->height; y++) {
     756           0 :         if (get_bits1(gb)) {
     757           0 :             for (x = 0; x < avctx->width; x += 2) {
     758           0 :                 dst_y[x    ] = get_bits(gb, 8);
     759           0 :                 dst_u[x / 2] = get_bits(gb, 8) + 128;
     760           0 :                 dst_y[x + 1] = get_bits(gb, 8);
     761           0 :                 dst_v[x / 2] = get_bits(gb, 8) + 128;
     762             :             }
     763             :         } else {
     764             :             int pred_TL[4], pred_L[4], pred_T[4];
     765             :             int y1, y2, u, v;
     766             : 
     767           0 :             pred_TL[0] = pred_L[0] = dst_y[-p->linesize[0]];
     768           0 :             pred_TL[1] = pred_L[1] = dst_u[-p->linesize[1]];
     769           0 :             pred_TL[2] = pred_L[2] = dst_v[-p->linesize[2]];
     770             : 
     771           0 :             for (x = 0; x < avctx->width; x += 2) {
     772           0 :                 pred_T[0] = dst_y[-p->linesize[0] + x];
     773           0 :                 pred_T[3] = dst_y[-p->linesize[0] + x + 1];
     774           0 :                 pred_T[1] = dst_u[-p->linesize[1] + x / 2];
     775           0 :                 pred_T[2] = dst_v[-p->linesize[2] + x / 2];
     776             : 
     777           0 :                 y1 = get_vlc2(gb, s->vlc[0].table, s->vlc[0].bits, 2);
     778           0 :                 u  = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
     779           0 :                 y2 = get_vlc2(gb, s->vlc[0].table, s->vlc[0].bits, 2);
     780           0 :                 v  = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
     781             : 
     782           0 :                 dst_y[x    ] = pred_L[0] = (y1 + ((3 * (pred_T[0] + pred_L[0]) - 2 * pred_TL[0]) >> 2)) & 0xff;
     783           0 :                 dst_u[x / 2] = pred_L[1] = (u + (((pred_L[1] - pred_TL[1]) >> 1) + pred_T[1])) & 0xff;
     784           0 :                 dst_y[x + 1] = pred_L[0] = (y2 + ((3 * (pred_T[3] + pred_L[0]) - 2 * pred_T[0]) >> 2)) & 0xff;
     785           0 :                 dst_v[x / 2] = pred_L[2] = (v + (((pred_L[2] - pred_TL[2]) >> 1) + pred_T[2])) & 0xff;
     786             : 
     787           0 :                 pred_TL[0] = pred_T[3];
     788           0 :                 pred_TL[1] = pred_T[1];
     789           0 :                 pred_TL[2] = pred_T[2];
     790             :             }
     791             :         }
     792             : 
     793           0 :         dst_y += p->linesize[0];
     794           0 :         dst_u += p->linesize[1];
     795           0 :         dst_v += p->linesize[2];
     796             :     }
     797           0 : }
     798             : 
     799           0 : static void decode_byryi(AVCodecContext *avctx, AVFrame *p, GetBitContext *gb)
     800             : {
     801           0 :     SheerVideoContext *s = avctx->priv_data;
     802             :     uint8_t *dst_y, *dst_u, *dst_v;
     803             :     int x, y;
     804             : 
     805           0 :     dst_y = p->data[0];
     806           0 :     dst_u = p->data[1];
     807           0 :     dst_v = p->data[2];
     808             : 
     809           0 :     if (get_bits1(gb)) {
     810           0 :         for (x = 0; x < avctx->width; x += 2) {
     811           0 :             dst_y[x    ] = get_bits(gb, 8);
     812           0 :             dst_u[x / 2] = get_bits(gb, 8);
     813           0 :             dst_y[x + 1] = get_bits(gb, 8);
     814           0 :             dst_v[x / 2] = get_bits(gb, 8);
     815             :         }
     816             :     } else {
     817           0 :         int pred[4] = { 125, -128, -128, 0 };
     818             : 
     819           0 :         for (x = 0; x < avctx->width; x += 2) {
     820             :             int y1, y2, u, v;
     821             : 
     822           0 :             y1 = get_vlc2(gb, s->vlc[0].table, s->vlc[0].bits, 2);
     823           0 :             u  = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
     824           0 :             y2 = get_vlc2(gb, s->vlc[0].table, s->vlc[0].bits, 2);
     825           0 :             v  = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
     826             : 
     827           0 :             dst_y[x    ] = pred[0] = (y1 + pred[0]) & 0xff;
     828           0 :             dst_u[x / 2] = pred[1] = (u  + pred[1]) & 0xff;
     829           0 :             dst_y[x + 1] = pred[0] = (y2 + pred[0]) & 0xff;
     830           0 :             dst_v[x / 2] = pred[2] = (v  + pred[2]) & 0xff;
     831             :         }
     832             :     }
     833             : 
     834           0 :     dst_y += p->linesize[0];
     835           0 :     dst_u += p->linesize[1];
     836           0 :     dst_v += p->linesize[2];
     837             : 
     838           0 :     for (y = 1; y < avctx->height; y++) {
     839           0 :         if (get_bits1(gb)) {
     840           0 :             for (x = 0; x < avctx->width; x += 2) {
     841           0 :                 dst_y[x    ] = get_bits(gb, 8);
     842           0 :                 dst_u[x / 2] = get_bits(gb, 8);
     843           0 :                 dst_y[x + 1] = get_bits(gb, 8);
     844           0 :                 dst_v[x / 2] = get_bits(gb, 8);
     845             :             }
     846             :         } else {
     847             :             int pred_L[4];
     848             :             int y1, y2, u, v;
     849             : 
     850           0 :             pred_L[0] = dst_y[-p->linesize[0]];
     851           0 :             pred_L[1] = dst_u[-p->linesize[1]];
     852           0 :             pred_L[2] = dst_v[-p->linesize[2]];
     853             : 
     854           0 :             for (x = 0; x < avctx->width; x += 2) {
     855           0 :                 y1 = get_vlc2(gb, s->vlc[0].table, s->vlc[0].bits, 2);
     856           0 :                 u  = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
     857           0 :                 y2 = get_vlc2(gb, s->vlc[0].table, s->vlc[0].bits, 2);
     858           0 :                 v  = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
     859             : 
     860           0 :                 dst_y[x    ] = pred_L[0] = (y1 + pred_L[0]) & 0xff;
     861           0 :                 dst_u[x / 2] = pred_L[1] = (u  + pred_L[1]) & 0xff;
     862           0 :                 dst_y[x + 1] = pred_L[0] = (y2 + pred_L[0]) & 0xff;
     863           0 :                 dst_v[x / 2] = pred_L[2] = (v +  pred_L[2]) & 0xff;
     864             :             }
     865             :         }
     866             : 
     867           0 :         dst_y += p->linesize[0];
     868           0 :         dst_u += p->linesize[1];
     869           0 :         dst_v += p->linesize[2];
     870             :     }
     871           0 : }
     872             : 
     873           0 : static void decode_byry(AVCodecContext *avctx, AVFrame *p, GetBitContext *gb)
     874             : {
     875           0 :     SheerVideoContext *s = avctx->priv_data;
     876             :     uint8_t *dst_y, *dst_u, *dst_v;
     877             :     int x, y;
     878             : 
     879           0 :     dst_y = p->data[0];
     880           0 :     dst_u = p->data[1];
     881           0 :     dst_v = p->data[2];
     882             : 
     883           0 :     if (get_bits1(gb)) {
     884           0 :         for (x = 0; x < avctx->width; x += 2) {
     885           0 :             dst_y[x    ] = get_bits(gb, 8);
     886           0 :             dst_u[x / 2] = get_bits(gb, 8);
     887           0 :             dst_y[x + 1] = get_bits(gb, 8);
     888           0 :             dst_v[x / 2] = get_bits(gb, 8);
     889             :         }
     890             :     } else {
     891           0 :         int pred[4] = { 125, -128, -128, 0 };
     892             : 
     893           0 :         for (x = 0; x < avctx->width; x += 2) {
     894             :             int y1, y2, u, v;
     895             : 
     896           0 :             y1 = get_vlc2(gb, s->vlc[0].table, s->vlc[0].bits, 2);
     897           0 :             u  = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
     898           0 :             y2 = get_vlc2(gb, s->vlc[0].table, s->vlc[0].bits, 2);
     899           0 :             v  = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
     900             : 
     901           0 :             dst_y[x    ] = pred[0] = (y1 + pred[0]) & 0xff;
     902           0 :             dst_u[x / 2] = pred[1] = (u  + pred[1]) & 0xff;
     903           0 :             dst_y[x + 1] = pred[0] = (y2 + pred[0]) & 0xff;
     904           0 :             dst_v[x / 2] = pred[2] = (v  + pred[2]) & 0xff;
     905             :         }
     906             :     }
     907             : 
     908           0 :     dst_y += p->linesize[0];
     909           0 :     dst_u += p->linesize[1];
     910           0 :     dst_v += p->linesize[2];
     911             : 
     912           0 :     for (y = 1; y < avctx->height; y++) {
     913           0 :         if (get_bits1(gb)) {
     914           0 :             for (x = 0; x < avctx->width; x += 2) {
     915           0 :                 dst_y[x    ] = get_bits(gb, 8);
     916           0 :                 dst_u[x / 2] = get_bits(gb, 8);
     917           0 :                 dst_y[x + 1] = get_bits(gb, 8);
     918           0 :                 dst_v[x / 2] = get_bits(gb, 8);
     919             :             }
     920             :         } else {
     921             :             int pred_TL[4], pred_L[4], pred_T[4];
     922             :             int y1, y2, u, v;
     923             : 
     924           0 :             pred_TL[0] = pred_L[0] = dst_y[-p->linesize[0]];
     925           0 :             pred_TL[1] = pred_L[1] = dst_u[-p->linesize[1]];
     926           0 :             pred_TL[2] = pred_L[2] = dst_v[-p->linesize[2]];
     927             : 
     928           0 :             for (x = 0; x < avctx->width; x += 2) {
     929           0 :                 pred_T[0] = dst_y[-p->linesize[0] + x];
     930           0 :                 pred_T[3] = dst_y[-p->linesize[0] + x + 1];
     931           0 :                 pred_T[1] = dst_u[-p->linesize[1] + x / 2];
     932           0 :                 pred_T[2] = dst_v[-p->linesize[2] + x / 2];
     933             : 
     934           0 :                 y1 = get_vlc2(gb, s->vlc[0].table, s->vlc[0].bits, 2);
     935           0 :                 u  = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
     936           0 :                 y2 = get_vlc2(gb, s->vlc[0].table, s->vlc[0].bits, 2);
     937           0 :                 v  = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
     938             : 
     939           0 :                 dst_y[x    ] = pred_L[0] = (y1 + ((3 * (pred_T[0] + pred_L[0]) - 2 * pred_TL[0]) >> 2)) & 0xff;
     940           0 :                 dst_u[x / 2] = pred_L[1] = (u + (((pred_L[1] - pred_TL[1]) >> 1) + pred_T[1])) & 0xff;
     941           0 :                 dst_y[x + 1] = pred_L[0] = (y2 + ((3 * (pred_T[3] + pred_L[0]) - 2 * pred_T[0]) >> 2)) & 0xff;
     942           0 :                 dst_v[x / 2] = pred_L[2] = (v + (((pred_L[2] - pred_TL[2]) >> 1) + pred_T[2])) & 0xff;
     943             : 
     944           0 :                 pred_TL[0] = pred_T[3];
     945           0 :                 pred_TL[1] = pred_T[1];
     946           0 :                 pred_TL[2] = pred_T[2];
     947             :             }
     948             :         }
     949             : 
     950           0 :         dst_y += p->linesize[0];
     951           0 :         dst_u += p->linesize[1];
     952           0 :         dst_v += p->linesize[2];
     953             :     }
     954           0 : }
     955             : 
     956           0 : static void decode_ybri(AVCodecContext *avctx, AVFrame *p, GetBitContext *gb)
     957             : {
     958           0 :     SheerVideoContext *s = avctx->priv_data;
     959             :     uint8_t *dst_y, *dst_u, *dst_v;
     960             :     int x, y;
     961             : 
     962           0 :     dst_y = p->data[0];
     963           0 :     dst_u = p->data[1];
     964           0 :     dst_v = p->data[2];
     965             : 
     966           0 :     if (get_bits1(gb)) {
     967           0 :         for (x = 0; x < avctx->width; x++) {
     968           0 :             dst_y[x] = get_bits(gb, 8);
     969           0 :             dst_u[x] = get_bits(gb, 8);
     970           0 :             dst_v[x] = get_bits(gb, 8);
     971             :         }
     972             :     } else {
     973           0 :         int pred[4] = { s->alt ? 125 : -146, -128, -128, -128 };
     974             : 
     975           0 :         for (x = 0; x < avctx->width; x++) {
     976             :             int y, u, v;
     977             : 
     978           0 :             y = get_vlc2(gb, s->vlc[0].table, s->vlc[0].bits, 2);
     979           0 :             u = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
     980           0 :             v = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
     981             : 
     982           0 :             dst_y[x] = pred[0] = (y + pred[0]) & 0xff;
     983           0 :             dst_u[x] = pred[1] = (u + pred[1]) & 0xff;
     984           0 :             dst_v[x] = pred[2] = (v + pred[2]) & 0xff;
     985             :         }
     986             :     }
     987             : 
     988           0 :     dst_y += p->linesize[0];
     989           0 :     dst_u += p->linesize[1];
     990           0 :     dst_v += p->linesize[2];
     991             : 
     992           0 :     for (y = 1; y < avctx->height; y++) {
     993           0 :         if (get_bits1(gb)) {
     994           0 :             for (x = 0; x < avctx->width; x++) {
     995           0 :                 dst_y[x] = get_bits(gb, 8);
     996           0 :                 dst_u[x] = get_bits(gb, 8);
     997           0 :                 dst_v[x] = get_bits(gb, 8);
     998             :             }
     999             :         } else {
    1000             :             int pred_L[4];
    1001             :             int y, u, v;
    1002             : 
    1003           0 :             pred_L[0] = dst_y[-p->linesize[0]];
    1004           0 :             pred_L[1] = dst_u[-p->linesize[1]];
    1005           0 :             pred_L[2] = dst_v[-p->linesize[2]];
    1006             : 
    1007           0 :             for (x = 0; x < avctx->width; x++) {
    1008           0 :                 y = get_vlc2(gb, s->vlc[0].table, s->vlc[0].bits, 2);
    1009           0 :                 u = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
    1010           0 :                 v = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
    1011             : 
    1012           0 :                 dst_y[x] = pred_L[0] = (y + pred_L[0]) & 0xff;
    1013           0 :                 dst_u[x] = pred_L[1] = (u + pred_L[1]) & 0xff;
    1014           0 :                 dst_v[x] = pred_L[2] = (v + pred_L[2]) & 0xff;
    1015             :             }
    1016             :         }
    1017             : 
    1018           0 :         dst_y += p->linesize[0];
    1019           0 :         dst_u += p->linesize[1];
    1020           0 :         dst_v += p->linesize[2];
    1021             :     }
    1022           0 : }
    1023             : 
    1024           0 : static void decode_ybr(AVCodecContext *avctx, AVFrame *p, GetBitContext *gb)
    1025             : {
    1026           0 :     SheerVideoContext *s = avctx->priv_data;
    1027             :     uint8_t *dst_y, *dst_u, *dst_v;
    1028             :     int x, y;
    1029             : 
    1030           0 :     dst_y = p->data[0];
    1031           0 :     dst_u = p->data[1];
    1032           0 :     dst_v = p->data[2];
    1033             : 
    1034           0 :     if (get_bits1(gb)) {
    1035           0 :         for (x = 0; x < avctx->width; x++) {
    1036           0 :             dst_y[x] = get_bits(gb, 8);
    1037           0 :             dst_u[x] = get_bits(gb, 8);
    1038           0 :             dst_v[x] = get_bits(gb, 8);
    1039             :         }
    1040             :     } else {
    1041           0 :         int pred[4] = { s->alt ? 125 : -146, -128, -128, -128 };
    1042             : 
    1043           0 :         for (x = 0; x < avctx->width; x++) {
    1044             :             int y, u, v;
    1045             : 
    1046           0 :             y = get_vlc2(gb, s->vlc[0].table, s->vlc[0].bits, 2);
    1047           0 :             u = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
    1048           0 :             v = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
    1049             : 
    1050           0 :             dst_y[x] = pred[0] = (y + pred[0]) & 0xff;
    1051           0 :             dst_u[x] = pred[1] = (u + pred[1]) & 0xff;
    1052           0 :             dst_v[x] = pred[2] = (v + pred[2]) & 0xff;
    1053             :         }
    1054             :     }
    1055             : 
    1056           0 :     dst_y += p->linesize[0];
    1057           0 :     dst_u += p->linesize[1];
    1058           0 :     dst_v += p->linesize[2];
    1059             : 
    1060           0 :     for (y = 1; y < avctx->height; y++) {
    1061           0 :         if (get_bits1(gb)) {
    1062           0 :             for (x = 0; x < avctx->width; x++) {
    1063           0 :                 dst_y[x] = get_bits(gb, 8);
    1064           0 :                 dst_u[x] = get_bits(gb, 8);
    1065           0 :                 dst_v[x] = get_bits(gb, 8);
    1066             :             }
    1067             :         } else {
    1068             :             int pred_TL[4], pred_L[4], pred_T[4];
    1069             :             int y, u, v;
    1070             : 
    1071           0 :             pred_TL[0] = pred_L[0] = dst_y[-p->linesize[0]];
    1072           0 :             pred_TL[1] = pred_L[1] = dst_u[-p->linesize[1]];
    1073           0 :             pred_TL[2] = pred_L[2] = dst_v[-p->linesize[2]];
    1074             : 
    1075           0 :             for (x = 0; x < avctx->width; x++) {
    1076           0 :                 pred_T[0] = dst_y[-p->linesize[0] + x];
    1077           0 :                 pred_T[1] = dst_u[-p->linesize[1] + x];
    1078           0 :                 pred_T[2] = dst_v[-p->linesize[2] + x];
    1079             : 
    1080           0 :                 y = get_vlc2(gb, s->vlc[0].table, s->vlc[0].bits, 2);
    1081           0 :                 u = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
    1082           0 :                 v = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
    1083             : 
    1084           0 :                 dst_y[x] = pred_L[0] = (y + ((3 * (pred_T[0] + pred_L[0]) - 2 * pred_TL[0]) >> 2)) & 0xff;
    1085           0 :                 dst_u[x] = pred_L[1] = (u + ((3 * (pred_T[1] + pred_L[1]) - 2 * pred_TL[1]) >> 2)) & 0xff;
    1086           0 :                 dst_v[x] = pred_L[2] = (v + ((3 * (pred_T[2] + pred_L[2]) - 2 * pred_TL[2]) >> 2)) & 0xff;
    1087             : 
    1088           0 :                 pred_TL[0] = pred_T[0];
    1089           0 :                 pred_TL[1] = pred_T[1];
    1090           0 :                 pred_TL[2] = pred_T[2];
    1091             :             }
    1092             :         }
    1093             : 
    1094           0 :         dst_y += p->linesize[0];
    1095           0 :         dst_u += p->linesize[1];
    1096           0 :         dst_v += p->linesize[2];
    1097             :     }
    1098           0 : }
    1099             : 
    1100           0 : static void decode_aybri(AVCodecContext *avctx, AVFrame *p, GetBitContext *gb)
    1101             : {
    1102           0 :     SheerVideoContext *s = avctx->priv_data;
    1103             :     uint8_t *dst_a, *dst_y, *dst_u, *dst_v;
    1104             :     int x, y;
    1105             : 
    1106           0 :     dst_a = p->data[3];
    1107           0 :     dst_y = p->data[0];
    1108           0 :     dst_u = p->data[1];
    1109           0 :     dst_v = p->data[2];
    1110             : 
    1111           0 :     if (get_bits1(gb)) {
    1112           0 :         for (x = 0; x < avctx->width; x++) {
    1113           0 :             dst_a[x] = get_bits(gb, 8);
    1114           0 :             dst_y[x] = get_bits(gb, 8);
    1115           0 :             dst_u[x] = get_bits(gb, 8);
    1116           0 :             dst_v[x] = get_bits(gb, 8);
    1117             :         }
    1118             :     } else {
    1119           0 :         int pred[4] = { 125, s->alt ? 125 : -146, -128, -128 };
    1120             : 
    1121           0 :         for (x = 0; x < avctx->width; x++) {
    1122             :             int a, y, u, v;
    1123             : 
    1124           0 :             a = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
    1125           0 :             y = get_vlc2(gb, s->vlc[0].table, s->vlc[0].bits, 2);
    1126           0 :             u = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
    1127           0 :             v = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
    1128             : 
    1129           0 :             dst_a[x] = pred[0] = (a + pred[0]) & 0xff;
    1130           0 :             dst_y[x] = pred[1] = (y + pred[1]) & 0xff;
    1131           0 :             dst_u[x] = pred[2] = (u + pred[2]) & 0xff;
    1132           0 :             dst_v[x] = pred[3] = (v + pred[3]) & 0xff;
    1133             :         }
    1134             :     }
    1135             : 
    1136           0 :     dst_a += p->linesize[3];
    1137           0 :     dst_y += p->linesize[0];
    1138           0 :     dst_u += p->linesize[1];
    1139           0 :     dst_v += p->linesize[2];
    1140             : 
    1141           0 :     for (y = 1; y < avctx->height; y++) {
    1142           0 :         if (get_bits1(gb)) {
    1143           0 :             for (x = 0; x < avctx->width; x++) {
    1144           0 :                 dst_a[x] = get_bits(gb, 8);
    1145           0 :                 dst_y[x] = get_bits(gb, 8);
    1146           0 :                 dst_u[x] = get_bits(gb, 8);
    1147           0 :                 dst_v[x] = get_bits(gb, 8);
    1148             :             }
    1149             :         } else {
    1150             :             int pred_L[4];
    1151             :             int a, y, u, v;
    1152             : 
    1153           0 :             pred_L[0] = dst_a[-p->linesize[3]];
    1154           0 :             pred_L[1] = dst_y[-p->linesize[0]];
    1155           0 :             pred_L[2] = dst_u[-p->linesize[1]];
    1156           0 :             pred_L[3] = dst_v[-p->linesize[2]];
    1157             : 
    1158           0 :             for (x = 0; x < avctx->width; x++) {
    1159           0 :                 a = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
    1160           0 :                 y = get_vlc2(gb, s->vlc[0].table, s->vlc[0].bits, 2);
    1161           0 :                 u = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
    1162           0 :                 v = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
    1163             : 
    1164           0 :                 dst_a[x] = pred_L[0] = (a + pred_L[0]) & 0xff;
    1165           0 :                 dst_y[x] = pred_L[1] = (y + pred_L[1]) & 0xff;
    1166           0 :                 dst_u[x] = pred_L[2] = (u + pred_L[2]) & 0xff;
    1167           0 :                 dst_v[x] = pred_L[3] = (v + pred_L[3]) & 0xff;
    1168             :             }
    1169             :         }
    1170             : 
    1171           0 :         dst_a += p->linesize[3];
    1172           0 :         dst_y += p->linesize[0];
    1173           0 :         dst_u += p->linesize[1];
    1174           0 :         dst_v += p->linesize[2];
    1175             :     }
    1176           0 : }
    1177             : 
    1178           0 : static void decode_aybr(AVCodecContext *avctx, AVFrame *p, GetBitContext *gb)
    1179             : {
    1180           0 :     SheerVideoContext *s = avctx->priv_data;
    1181             :     uint8_t *dst_a, *dst_y, *dst_u, *dst_v;
    1182             :     int x, y;
    1183             : 
    1184           0 :     dst_a = p->data[3];
    1185           0 :     dst_y = p->data[0];
    1186           0 :     dst_u = p->data[1];
    1187           0 :     dst_v = p->data[2];
    1188             : 
    1189           0 :     if (get_bits1(gb)) {
    1190           0 :         for (x = 0; x < avctx->width; x++) {
    1191           0 :             dst_a[x] = get_bits(gb, 8);
    1192           0 :             dst_y[x] = get_bits(gb, 8);
    1193           0 :             dst_u[x] = get_bits(gb, 8);
    1194           0 :             dst_v[x] = get_bits(gb, 8);
    1195             :         }
    1196             :     } else {
    1197           0 :         int pred[4] = { 125, s->alt ? 125 : -146, -128, -128 };
    1198             : 
    1199           0 :         for (x = 0; x < avctx->width; x++) {
    1200             :             int a, y, u, v;
    1201             : 
    1202           0 :             a = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
    1203           0 :             y = get_vlc2(gb, s->vlc[0].table, s->vlc[0].bits, 2);
    1204           0 :             u = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
    1205           0 :             v = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
    1206             : 
    1207           0 :             dst_a[x] = pred[0] = (a + pred[0]) & 0xff;
    1208           0 :             dst_y[x] = pred[1] = (y + pred[1]) & 0xff;
    1209           0 :             dst_u[x] = pred[2] = (u + pred[2]) & 0xff;
    1210           0 :             dst_v[x] = pred[3] = (v + pred[3]) & 0xff;
    1211             :         }
    1212             :     }
    1213             : 
    1214           0 :     dst_a += p->linesize[3];
    1215           0 :     dst_y += p->linesize[0];
    1216           0 :     dst_u += p->linesize[1];
    1217           0 :     dst_v += p->linesize[2];
    1218             : 
    1219           0 :     for (y = 1; y < avctx->height; y++) {
    1220           0 :         if (get_bits1(gb)) {
    1221           0 :             for (x = 0; x < avctx->width; x++) {
    1222           0 :                 dst_a[x] = get_bits(gb, 8);
    1223           0 :                 dst_y[x] = get_bits(gb, 8);
    1224           0 :                 dst_u[x] = get_bits(gb, 8);
    1225           0 :                 dst_v[x] = get_bits(gb, 8);
    1226             :             }
    1227             :         } else {
    1228             :             int pred_TL[4], pred_L[4], pred_T[4];
    1229             :             int a, y, u, v;
    1230             : 
    1231           0 :             pred_TL[0] = pred_L[0] = dst_a[-p->linesize[3]];
    1232           0 :             pred_TL[1] = pred_L[1] = dst_y[-p->linesize[0]];
    1233           0 :             pred_TL[2] = pred_L[2] = dst_u[-p->linesize[1]];
    1234           0 :             pred_TL[3] = pred_L[3] = dst_v[-p->linesize[2]];
    1235             : 
    1236           0 :             for (x = 0; x < avctx->width; x++) {
    1237           0 :                 pred_T[0] = dst_a[-p->linesize[3] + x];
    1238           0 :                 pred_T[1] = dst_y[-p->linesize[0] + x];
    1239           0 :                 pred_T[2] = dst_u[-p->linesize[1] + x];
    1240           0 :                 pred_T[3] = dst_v[-p->linesize[2] + x];
    1241             : 
    1242           0 :                 a = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
    1243           0 :                 y = get_vlc2(gb, s->vlc[0].table, s->vlc[0].bits, 2);
    1244           0 :                 u = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
    1245           0 :                 v = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
    1246             : 
    1247           0 :                 dst_a[x] = pred_L[0] = (a + ((3 * (pred_T[0] + pred_L[0]) - 2 * pred_TL[0]) >> 2)) & 0xff;
    1248           0 :                 dst_y[x] = pred_L[1] = (y + ((3 * (pred_T[1] + pred_L[1]) - 2 * pred_TL[1]) >> 2)) & 0xff;
    1249           0 :                 dst_u[x] = pred_L[2] = (u + ((3 * (pred_T[2] + pred_L[2]) - 2 * pred_TL[2]) >> 2)) & 0xff;
    1250           0 :                 dst_v[x] = pred_L[3] = (v + ((3 * (pred_T[3] + pred_L[3]) - 2 * pred_TL[3]) >> 2)) & 0xff;
    1251             : 
    1252           0 :                 pred_TL[0] = pred_T[0];
    1253           0 :                 pred_TL[1] = pred_T[1];
    1254           0 :                 pred_TL[2] = pred_T[2];
    1255           0 :                 pred_TL[3] = pred_T[3];
    1256             :             }
    1257             :         }
    1258             : 
    1259           0 :         dst_a += p->linesize[3];
    1260           0 :         dst_y += p->linesize[0];
    1261           0 :         dst_u += p->linesize[1];
    1262           0 :         dst_v += p->linesize[2];
    1263             :     }
    1264           0 : }
    1265             : 
    1266           0 : static void decode_argxi(AVCodecContext *avctx, AVFrame *p, GetBitContext *gb)
    1267             : {
    1268           0 :     SheerVideoContext *s = avctx->priv_data;
    1269             :     uint16_t *dst_r, *dst_g, *dst_b, *dst_a;
    1270             :     int x, y;
    1271             : 
    1272           0 :     dst_r = (uint16_t *)p->data[2];
    1273           0 :     dst_g = (uint16_t *)p->data[0];
    1274           0 :     dst_b = (uint16_t *)p->data[1];
    1275           0 :     dst_a = (uint16_t *)p->data[3];
    1276             : 
    1277           0 :     for (y = 0; y < avctx->height; y++) {
    1278           0 :         if (get_bits1(gb)) {
    1279           0 :             for (x = 0; x < avctx->width; x++) {
    1280           0 :                 dst_a[x] = get_bits(gb, 10);
    1281           0 :                 dst_r[x] = get_bits(gb, 10);
    1282           0 :                 dst_g[x] = get_bits(gb, 10);
    1283           0 :                 dst_b[x] = get_bits(gb, 10);
    1284             :             }
    1285             :         } else {
    1286           0 :             int pred[4] = { 512, 512, 512, 512 };
    1287             : 
    1288           0 :             for (x = 0; x < avctx->width; x++) {
    1289             :                 int r, g, b, a;
    1290             : 
    1291           0 :                 a = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
    1292           0 :                 r = get_vlc2(gb, s->vlc[0].table, s->vlc[0].bits, 2);
    1293           0 :                 g = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
    1294           0 :                 b = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
    1295             : 
    1296           0 :                 dst_a[x] = pred[3] = (a + pred[3]) & 0x3ff;
    1297           0 :                 dst_r[x] = pred[0] = (r + pred[0]) & 0x3ff;
    1298           0 :                 dst_g[x] = pred[1] = (r + g + pred[1]) & 0x3ff;
    1299           0 :                 dst_b[x] = pred[2] = (r + g + b + pred[2]) & 0x3ff;
    1300             :             }
    1301             :         }
    1302             : 
    1303           0 :         dst_r += p->linesize[2] / 2;
    1304           0 :         dst_g += p->linesize[0] / 2;
    1305           0 :         dst_b += p->linesize[1] / 2;
    1306           0 :         dst_a += p->linesize[3] / 2;
    1307             :     }
    1308           0 : }
    1309             : 
    1310           0 : static void decode_argx(AVCodecContext *avctx, AVFrame *p, GetBitContext *gb)
    1311             : {
    1312           0 :     SheerVideoContext *s = avctx->priv_data;
    1313             :     uint16_t *dst_r, *dst_g, *dst_b, *dst_a;
    1314             :     int x, y;
    1315             : 
    1316           0 :     dst_r = (uint16_t *)p->data[2];
    1317           0 :     dst_g = (uint16_t *)p->data[0];
    1318           0 :     dst_b = (uint16_t *)p->data[1];
    1319           0 :     dst_a = (uint16_t *)p->data[3];
    1320             : 
    1321           0 :     if (get_bits1(gb)) {
    1322           0 :         for (x = 0; x < avctx->width; x++) {
    1323           0 :             dst_a[x] = get_bits(gb, 10);
    1324           0 :             dst_r[x] = get_bits(gb, 10);
    1325           0 :             dst_g[x] = get_bits(gb, 10);
    1326           0 :             dst_b[x] = get_bits(gb, 10);
    1327             :         }
    1328             :     } else {
    1329           0 :         int pred[4] = { 512, 512, 512, 512 };
    1330             : 
    1331           0 :         for (x = 0; x < avctx->width; x++) {
    1332             :             int r, g, b, a;
    1333             : 
    1334           0 :             a = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
    1335           0 :             r = get_vlc2(gb, s->vlc[0].table, s->vlc[0].bits, 2);
    1336           0 :             g = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
    1337           0 :             b = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
    1338             : 
    1339           0 :             dst_a[x] = pred[3] = (a + pred[3]) & 0x3ff;
    1340           0 :             dst_r[x] = pred[0] = (r + pred[0]) & 0x3ff;
    1341           0 :             dst_g[x] = pred[1] = (r + g + pred[1]) & 0x3ff;
    1342           0 :             dst_b[x] = pred[2] = (r + g + b + pred[2]) & 0x3ff;
    1343             :         }
    1344             :     }
    1345             : 
    1346           0 :     dst_r += p->linesize[2] / 2;
    1347           0 :     dst_g += p->linesize[0] / 2;
    1348           0 :     dst_b += p->linesize[1] / 2;
    1349           0 :     dst_a += p->linesize[3] / 2;
    1350             : 
    1351           0 :     for (y = 1; y < avctx->height; y++) {
    1352           0 :         if (get_bits1(gb)) {
    1353           0 :             for (x = 0; x < avctx->width; x++) {
    1354           0 :                 dst_a[x] = get_bits(gb, 10);
    1355           0 :                 dst_r[x] = get_bits(gb, 10);
    1356           0 :                 dst_g[x] = get_bits(gb, 10);
    1357           0 :                 dst_b[x] = get_bits(gb, 10);
    1358             :             }
    1359             :         } else {
    1360             :             int pred_TL[4], pred_L[4], pred_T[4];
    1361             :             int r, g, b, a;
    1362             : 
    1363           0 :             pred_TL[0] = pred_L[0] = dst_r[-p->linesize[2] / 2];
    1364           0 :             pred_TL[1] = pred_L[1] = dst_g[-p->linesize[0] / 2];
    1365           0 :             pred_TL[2] = pred_L[2] = dst_b[-p->linesize[1] / 2];
    1366           0 :             pred_TL[3] = pred_L[3] = dst_a[-p->linesize[3] / 2];
    1367             : 
    1368           0 :             for (x = 0; x < avctx->width; x++) {
    1369           0 :                 pred_T[0] = dst_r[-p->linesize[2] / 2 + x];
    1370           0 :                 pred_T[1] = dst_g[-p->linesize[0] / 2 + x];
    1371           0 :                 pred_T[2] = dst_b[-p->linesize[1] / 2 + x];
    1372           0 :                 pred_T[3] = dst_a[-p->linesize[3] / 2 + x];
    1373             : 
    1374           0 :                 a = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
    1375           0 :                 r = get_vlc2(gb, s->vlc[0].table, s->vlc[0].bits, 2);
    1376           0 :                 g = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
    1377           0 :                 b = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
    1378             : 
    1379           0 :                 dst_a[x] = pred_L[3] = (a + ((3 * (pred_T[3] + pred_L[3]) - 2 * pred_TL[3]) >> 2)) & 0x3ff;
    1380           0 :                 dst_r[x] = pred_L[0] = (r + ((3 * (pred_T[0] + pred_L[0]) - 2 * pred_TL[0]) >> 2)) & 0x3ff;
    1381           0 :                 dst_g[x] = pred_L[1] = (r + g + ((3 * (pred_T[1] + pred_L[1]) - 2 * pred_TL[1]) >> 2)) & 0x3ff;
    1382           0 :                 dst_b[x] = pred_L[2] = (r + g + b + ((3 * (pred_T[2] + pred_L[2]) - 2 * pred_TL[2]) >> 2)) & 0x3ff;
    1383             : 
    1384           0 :                 pred_TL[0] = pred_T[0];
    1385           0 :                 pred_TL[1] = pred_T[1];
    1386           0 :                 pred_TL[2] = pred_T[2];
    1387           0 :                 pred_TL[3] = pred_T[3];
    1388             :             }
    1389             :         }
    1390             : 
    1391           0 :         dst_r += p->linesize[2] / 2;
    1392           0 :         dst_g += p->linesize[0] / 2;
    1393           0 :         dst_b += p->linesize[1] / 2;
    1394           0 :         dst_a += p->linesize[3] / 2;
    1395             :     }
    1396           0 : }
    1397             : 
    1398           0 : static void decode_rgbxi(AVCodecContext *avctx, AVFrame *p, GetBitContext *gb)
    1399             : {
    1400           0 :     SheerVideoContext *s = avctx->priv_data;
    1401             :     uint16_t *dst_r, *dst_g, *dst_b;
    1402             :     int x, y;
    1403             : 
    1404           0 :     dst_r = (uint16_t *)p->data[2];
    1405           0 :     dst_g = (uint16_t *)p->data[0];
    1406           0 :     dst_b = (uint16_t *)p->data[1];
    1407             : 
    1408           0 :     for (y = 0; y < avctx->height; y++) {
    1409           0 :         if (get_bits1(gb)) {
    1410           0 :             for (x = 0; x < avctx->width; x++) {
    1411           0 :                 dst_r[x] = get_bits(gb, 10);
    1412           0 :                 dst_g[x] = get_bits(gb, 10);
    1413           0 :                 dst_b[x] = get_bits(gb, 10);
    1414             :             }
    1415             :         } else {
    1416           0 :             int pred[4] = { 512, 512, 512, 0 };
    1417             : 
    1418           0 :             for (x = 0; x < avctx->width; x++) {
    1419             :                 int r, g, b;
    1420             : 
    1421           0 :                 r = get_vlc2(gb, s->vlc[0].table, s->vlc[0].bits, 2);
    1422           0 :                 g = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
    1423           0 :                 b = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
    1424             : 
    1425           0 :                 dst_r[x] = pred[0] = (r + pred[0]) & 0x3ff;
    1426           0 :                 dst_g[x] = pred[1] = (r + g + pred[1]) & 0x3ff;
    1427           0 :                 dst_b[x] = pred[2] = (r + g + b + pred[2]) & 0x3ff;
    1428             :             }
    1429             :         }
    1430             : 
    1431           0 :         dst_r += p->linesize[2] / 2;
    1432           0 :         dst_g += p->linesize[0] / 2;
    1433           0 :         dst_b += p->linesize[1] / 2;
    1434             :     }
    1435           0 : }
    1436             : 
    1437           0 : static void decode_rgbx(AVCodecContext *avctx, AVFrame *p, GetBitContext *gb)
    1438             : {
    1439           0 :     SheerVideoContext *s = avctx->priv_data;
    1440             :     uint16_t *dst_r, *dst_g, *dst_b;
    1441             :     int x, y;
    1442             : 
    1443           0 :     dst_r = (uint16_t *)p->data[2];
    1444           0 :     dst_g = (uint16_t *)p->data[0];
    1445           0 :     dst_b = (uint16_t *)p->data[1];
    1446             : 
    1447           0 :     if (get_bits1(gb)) {
    1448           0 :         for (x = 0; x < avctx->width; x++) {
    1449           0 :             dst_r[x] = get_bits(gb, 10);
    1450           0 :             dst_g[x] = get_bits(gb, 10);
    1451           0 :             dst_b[x] = get_bits(gb, 10);
    1452             :         }
    1453             :     } else {
    1454           0 :         int pred[4] = { 512, 512, 512, 0 };
    1455             : 
    1456           0 :         for (x = 0; x < avctx->width; x++) {
    1457             :             int r, g, b;
    1458             : 
    1459           0 :             r = get_vlc2(gb, s->vlc[0].table, s->vlc[0].bits, 2);
    1460           0 :             g = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
    1461           0 :             b = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
    1462             : 
    1463           0 :             dst_r[x] = pred[0] = (r + pred[0]) & 0x3ff;
    1464           0 :             dst_g[x] = pred[1] = (r + g + pred[1]) & 0x3ff;
    1465           0 :             dst_b[x] = pred[2] = (r + g + b + pred[2]) & 0x3ff;
    1466             :         }
    1467             :     }
    1468             : 
    1469           0 :     dst_r += p->linesize[2] / 2;
    1470           0 :     dst_g += p->linesize[0] / 2;
    1471           0 :     dst_b += p->linesize[1] / 2;
    1472             : 
    1473           0 :     for (y = 1; y < avctx->height; y++) {
    1474           0 :         if (get_bits1(gb)) {
    1475           0 :             for (x = 0; x < avctx->width; x++) {
    1476           0 :                 dst_r[x] = get_bits(gb, 10);
    1477           0 :                 dst_g[x] = get_bits(gb, 10);
    1478           0 :                 dst_b[x] = get_bits(gb, 10);
    1479             :             }
    1480             :         } else {
    1481             :             int pred_TL[4], pred_L[4], pred_T[4];
    1482             :             int r, g, b;
    1483             : 
    1484           0 :             pred_TL[0] = pred_L[0] = dst_r[-p->linesize[2] / 2];
    1485           0 :             pred_TL[1] = pred_L[1] = dst_g[-p->linesize[0] / 2];
    1486           0 :             pred_TL[2] = pred_L[2] = dst_b[-p->linesize[1] / 2];
    1487             : 
    1488           0 :             for (x = 0; x < avctx->width; x++) {
    1489           0 :                 pred_T[0] = dst_r[-p->linesize[2] / 2 + x];
    1490           0 :                 pred_T[1] = dst_g[-p->linesize[0] / 2 + x];
    1491           0 :                 pred_T[2] = dst_b[-p->linesize[1] / 2 + x];
    1492             : 
    1493           0 :                 r = get_vlc2(gb, s->vlc[0].table, s->vlc[0].bits, 2);
    1494           0 :                 g = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
    1495           0 :                 b = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
    1496             : 
    1497           0 :                 dst_r[x] = pred_L[0] = (r + ((3 * (pred_T[0] + pred_L[0]) - 2 * pred_TL[0]) >> 2)) & 0x3ff;
    1498           0 :                 dst_g[x] = pred_L[1] = (r + g + ((3 * (pred_T[1] + pred_L[1]) - 2 * pred_TL[1]) >> 2)) & 0x3ff;
    1499           0 :                 dst_b[x] = pred_L[2] = (r + g + b + ((3 * (pred_T[2] + pred_L[2]) - 2 * pred_TL[2]) >> 2)) & 0x3ff;
    1500             : 
    1501           0 :                 pred_TL[0] = pred_T[0];
    1502           0 :                 pred_TL[1] = pred_T[1];
    1503           0 :                 pred_TL[2] = pred_T[2];
    1504             :             }
    1505             :         }
    1506             : 
    1507           0 :         dst_r += p->linesize[2] / 2;
    1508           0 :         dst_g += p->linesize[0] / 2;
    1509           0 :         dst_b += p->linesize[1] / 2;
    1510             :     }
    1511           0 : }
    1512             : 
    1513           0 : static void decode_argbi(AVCodecContext *avctx, AVFrame *p, GetBitContext *gb)
    1514             : {
    1515           0 :     SheerVideoContext *s = avctx->priv_data;
    1516             :     uint8_t *dst;
    1517             :     int x, y;
    1518             : 
    1519           0 :     dst = p->data[0];
    1520           0 :     if (get_bits1(gb)) {
    1521           0 :         for (x = 0; x < avctx->width; x++) {
    1522           0 :             dst[x * 4 + 0] = get_bits(gb, 8);
    1523           0 :             dst[x * 4 + 1] = get_bits(gb, 8);
    1524           0 :             dst[x * 4 + 2] = get_bits(gb, 8);
    1525           0 :             dst[x * 4 + 3] = get_bits(gb, 8);
    1526             :         }
    1527             :     } else {
    1528           0 :         int pred[4] = { -128, -128, -128, -128 };
    1529             : 
    1530           0 :         for (x = 0; x < avctx->width; x++) {
    1531             :             int a, r, g, b;
    1532             : 
    1533           0 :             a = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
    1534           0 :             r = get_vlc2(gb, s->vlc[0].table, s->vlc[0].bits, 2);
    1535           0 :             g = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
    1536           0 :             b = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
    1537             : 
    1538           0 :             dst[4 * x + 0] = pred[0] = (a + pred[0]) & 0xff;
    1539           0 :             dst[4 * x + 1] = pred[1] = (r + pred[1]) & 0xff;
    1540           0 :             dst[4 * x + 2] = pred[2] = (r + g + pred[2]) & 0xff;
    1541           0 :             dst[4 * x + 3] = pred[3] = (r + g + b + pred[3]) & 0xff;
    1542             :         }
    1543             :     }
    1544             : 
    1545           0 :     dst += p->linesize[0];
    1546           0 :     for (y = 1; y < avctx->height; y++) {
    1547           0 :         if (get_bits1(gb)) {
    1548           0 :             for (x = 0; x < avctx->width; x++) {
    1549           0 :                 dst[x * 4 + 0] = get_bits(gb, 8);
    1550           0 :                 dst[x * 4 + 1] = get_bits(gb, 8);
    1551           0 :                 dst[x * 4 + 2] = get_bits(gb, 8);
    1552           0 :                 dst[x * 4 + 3] = get_bits(gb, 8);
    1553             :             }
    1554             :         } else {
    1555             :             int pred_L[4];
    1556             :             int a, r, g, b;
    1557             : 
    1558           0 :             pred_L[0] = dst[-p->linesize[0] + 0];
    1559           0 :             pred_L[1] = dst[-p->linesize[0] + 1];
    1560           0 :             pred_L[2] = dst[-p->linesize[0] + 2];
    1561           0 :             pred_L[3] = dst[-p->linesize[0] + 3];
    1562             : 
    1563           0 :             for (x = 0; x < avctx->width; x++) {
    1564           0 :                 a = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
    1565           0 :                 r = get_vlc2(gb, s->vlc[0].table, s->vlc[0].bits, 2);
    1566           0 :                 g = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
    1567           0 :                 b = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
    1568             : 
    1569           0 :                 dst[4 * x + 0] = pred_L[0] = (a + pred_L[0]) & 0xff;
    1570           0 :                 dst[4 * x + 1] = pred_L[1] = (r + pred_L[1]) & 0xff;
    1571           0 :                 dst[4 * x + 2] = pred_L[2] = (r + g + pred_L[2]) & 0xff;
    1572           0 :                 dst[4 * x + 3] = pred_L[3] = (r + g + b + pred_L[3]) & 0xff;
    1573             :             }
    1574             :         }
    1575           0 :         dst += p->linesize[0];
    1576             :     }
    1577           0 : }
    1578             : 
    1579           0 : static void decode_argb(AVCodecContext *avctx, AVFrame *p, GetBitContext *gb)
    1580             : {
    1581           0 :     SheerVideoContext *s = avctx->priv_data;
    1582             :     uint8_t *dst;
    1583             :     int x, y;
    1584             : 
    1585           0 :     dst = p->data[0];
    1586           0 :     if (get_bits1(gb)) {
    1587           0 :         for (x = 0; x < avctx->width; x++) {
    1588           0 :             dst[x * 4 + 0] = get_bits(gb, 8);
    1589           0 :             dst[x * 4 + 1] = get_bits(gb, 8);
    1590           0 :             dst[x * 4 + 2] = get_bits(gb, 8);
    1591           0 :             dst[x * 4 + 3] = get_bits(gb, 8);
    1592             :         }
    1593             :     } else {
    1594           0 :         int pred[4] = { -128, -128, -128, -128 };
    1595             : 
    1596           0 :         for (x = 0; x < avctx->width; x++) {
    1597             :             int a, r, g, b;
    1598             : 
    1599           0 :             a = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
    1600           0 :             r = get_vlc2(gb, s->vlc[0].table, s->vlc[0].bits, 2);
    1601           0 :             g = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
    1602           0 :             b = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
    1603             : 
    1604           0 :             dst[4 * x + 0] = pred[0] = (a + pred[0]) & 0xff;
    1605           0 :             dst[4 * x + 1] = pred[1] = (r + pred[1]) & 0xff;
    1606           0 :             dst[4 * x + 2] = pred[2] = (r + g + pred[2]) & 0xff;
    1607           0 :             dst[4 * x + 3] = pred[3] = (r + g + b + pred[3]) & 0xff;
    1608             :         }
    1609             :     }
    1610             : 
    1611           0 :     dst += p->linesize[0];
    1612           0 :     for (y = 1; y < avctx->height; y++) {
    1613           0 :         if (get_bits1(gb)) {
    1614           0 :             for (x = 0; x < avctx->width; x++) {
    1615           0 :                 dst[x * 4 + 0] = get_bits(gb, 8);
    1616           0 :                 dst[x * 4 + 1] = get_bits(gb, 8);
    1617           0 :                 dst[x * 4 + 2] = get_bits(gb, 8);
    1618           0 :                 dst[x * 4 + 3] = get_bits(gb, 8);
    1619             :             }
    1620             :         } else {
    1621             :             int pred_TL[4], pred_L[4], pred_T[4];
    1622             :             int a, r, g, b;
    1623             : 
    1624           0 :             pred_TL[0] = pred_L[0] = dst[-p->linesize[0] + 0];
    1625           0 :             pred_TL[1] = pred_L[1] = dst[-p->linesize[0] + 1];
    1626           0 :             pred_TL[2] = pred_L[2] = dst[-p->linesize[0] + 2];
    1627           0 :             pred_TL[3] = pred_L[3] = dst[-p->linesize[0] + 3];
    1628             : 
    1629           0 :             for (x = 0; x < avctx->width; x++) {
    1630           0 :                 pred_T[0] = dst[-p->linesize[0] + 4 * x + 0];
    1631           0 :                 pred_T[1] = dst[-p->linesize[0] + 4 * x + 1];
    1632           0 :                 pred_T[2] = dst[-p->linesize[0] + 4 * x + 2];
    1633           0 :                 pred_T[3] = dst[-p->linesize[0] + 4 * x + 3];
    1634             : 
    1635           0 :                 a = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
    1636           0 :                 r = get_vlc2(gb, s->vlc[0].table, s->vlc[0].bits, 2);
    1637           0 :                 g = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
    1638           0 :                 b = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
    1639             : 
    1640           0 :                 dst[4 * x + 0] = pred_L[0] = (a + ((3 * (pred_T[0] + pred_L[0]) - 2 * pred_TL[0]) >> 2)) & 0xff;
    1641           0 :                 dst[4 * x + 1] = pred_L[1] = (r + ((3 * (pred_T[1] + pred_L[1]) - 2 * pred_TL[1]) >> 2)) & 0xff;
    1642           0 :                 dst[4 * x + 2] = pred_L[2] = (r + g + ((3 * (pred_T[2] + pred_L[2]) - 2 * pred_TL[2]) >> 2)) & 0xff;
    1643           0 :                 dst[4 * x + 3] = pred_L[3] = (r + g + b + ((3 * (pred_T[3] + pred_L[3]) - 2 * pred_TL[3]) >> 2)) & 0xff;
    1644             : 
    1645           0 :                 pred_TL[0] = pred_T[0];
    1646           0 :                 pred_TL[1] = pred_T[1];
    1647           0 :                 pred_TL[2] = pred_T[2];
    1648           0 :                 pred_TL[3] = pred_T[3];
    1649             :             }
    1650             :         }
    1651           0 :         dst += p->linesize[0];
    1652             :     }
    1653           0 : }
    1654             : 
    1655           0 : static void decode_rgbi(AVCodecContext *avctx, AVFrame *p, GetBitContext *gb)
    1656             : {
    1657           0 :     SheerVideoContext *s = avctx->priv_data;
    1658             :     uint8_t *dst;
    1659             :     int x, y;
    1660             : 
    1661           0 :     dst = p->data[0];
    1662           0 :     if (get_bits1(gb)) {
    1663           0 :         for (x = 0; x < avctx->width; x++) {
    1664           0 :             dst[x * 4 + 0] = get_bits(gb, 8);
    1665           0 :             dst[x * 4 + 1] = get_bits(gb, 8);
    1666           0 :             dst[x * 4 + 2] = get_bits(gb, 8);
    1667             :         }
    1668             :     } else {
    1669           0 :         int pred[4] = { -128, -128, -128, -128 };
    1670             : 
    1671           0 :         for (x = 0; x < avctx->width; x++) {
    1672             :             int r, g, b;
    1673             : 
    1674           0 :             r = get_vlc2(gb, s->vlc[0].table, s->vlc[0].bits, 2);
    1675           0 :             g = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
    1676           0 :             b = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
    1677             : 
    1678           0 :             dst[4 * x + 0] = pred[0] = (r + pred[0]) & 0xff;
    1679           0 :             dst[4 * x + 1] = pred[1] = (r + g + pred[1]) & 0xff;
    1680           0 :             dst[4 * x + 2] = pred[2] = (r + g + b + pred[2]) & 0xff;
    1681             :         }
    1682             :     }
    1683             : 
    1684           0 :     dst += p->linesize[0];
    1685           0 :     for (y = 1; y < avctx->height; y++) {
    1686           0 :         if (get_bits1(gb)) {
    1687           0 :             for (x = 0; x < avctx->width; x++) {
    1688           0 :                 dst[x * 4 + 0] = get_bits(gb, 8);
    1689           0 :                 dst[x * 4 + 1] = get_bits(gb, 8);
    1690           0 :                 dst[x * 4 + 2] = get_bits(gb, 8);
    1691             :             }
    1692             :         } else {
    1693             :             int pred_L[4];
    1694             :             int r, g, b;
    1695             : 
    1696           0 :             pred_L[0] = dst[-p->linesize[0] + 0];
    1697           0 :             pred_L[1] = dst[-p->linesize[0] + 1];
    1698           0 :             pred_L[2] = dst[-p->linesize[0] + 2];
    1699             : 
    1700           0 :             for (x = 0; x < avctx->width; x++) {
    1701           0 :                 r = get_vlc2(gb, s->vlc[0].table, s->vlc[0].bits, 2);
    1702           0 :                 g = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
    1703           0 :                 b = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
    1704             : 
    1705           0 :                 dst[4 * x + 0] = pred_L[0] = (r + pred_L[0]) & 0xff;
    1706           0 :                 dst[4 * x + 1] = pred_L[1] = (r + g + pred_L[1]) & 0xff;
    1707           0 :                 dst[4 * x + 2] = pred_L[2] = (r + g + b + pred_L[2]) & 0xff;
    1708             :             }
    1709             :         }
    1710           0 :         dst += p->linesize[0];
    1711             :     }
    1712           0 : }
    1713             : 
    1714           0 : static void decode_rgb(AVCodecContext *avctx, AVFrame *p, GetBitContext *gb)
    1715             : {
    1716           0 :     SheerVideoContext *s = avctx->priv_data;
    1717             :     uint8_t *dst;
    1718             :     int x, y;
    1719             : 
    1720           0 :     dst = p->data[0];
    1721           0 :     if (get_bits1(gb)) {
    1722           0 :         for (x = 0; x < avctx->width; x++) {
    1723           0 :             dst[x * 4 + 0] = get_bits(gb, 8);
    1724           0 :             dst[x * 4 + 1] = get_bits(gb, 8);
    1725           0 :             dst[x * 4 + 2] = get_bits(gb, 8);
    1726             :         }
    1727             :     } else {
    1728           0 :         int pred[4] = { -128, -128, -128, -128 };
    1729             : 
    1730           0 :         for (x = 0; x < avctx->width; x++) {
    1731             :             int r, g, b;
    1732             : 
    1733           0 :             r = get_vlc2(gb, s->vlc[0].table, s->vlc[0].bits, 2);
    1734           0 :             g = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
    1735           0 :             b = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
    1736             : 
    1737           0 :             dst[4 * x + 0] = pred[0] = (r + pred[0]) & 0xff;
    1738           0 :             dst[4 * x + 1] = pred[1] = (r + g + pred[1]) & 0xff;
    1739           0 :             dst[4 * x + 2] = pred[2] = (r + g + b + pred[2]) & 0xff;
    1740             :         }
    1741             :     }
    1742             : 
    1743           0 :     dst += p->linesize[0];
    1744           0 :     for (y = 1; y < avctx->height; y++) {
    1745           0 :         if (get_bits1(gb)) {
    1746           0 :             for (x = 0; x < avctx->width; x++) {
    1747           0 :                 dst[x * 4 + 0] = get_bits(gb, 8);
    1748           0 :                 dst[x * 4 + 1] = get_bits(gb, 8);
    1749           0 :                 dst[x * 4 + 2] = get_bits(gb, 8);
    1750             :             }
    1751             :         } else {
    1752             :             int pred_TL[4], pred_L[4], pred_T[4];
    1753             :             int r, g, b;
    1754             : 
    1755           0 :             pred_TL[0] = pred_L[0] = dst[-p->linesize[0] + 0];
    1756           0 :             pred_TL[1] = pred_L[1] = dst[-p->linesize[0] + 1];
    1757           0 :             pred_TL[2] = pred_L[2] = dst[-p->linesize[0] + 2];
    1758             : 
    1759           0 :             for (x = 0; x < avctx->width; x++) {
    1760           0 :                 pred_T[0] = dst[-p->linesize[0] + 4 * x + 0];
    1761           0 :                 pred_T[1] = dst[-p->linesize[0] + 4 * x + 1];
    1762           0 :                 pred_T[2] = dst[-p->linesize[0] + 4 * x + 2];
    1763             : 
    1764           0 :                 r = get_vlc2(gb, s->vlc[0].table, s->vlc[0].bits, 2);
    1765           0 :                 g = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
    1766           0 :                 b = get_vlc2(gb, s->vlc[1].table, s->vlc[1].bits, 2);
    1767             : 
    1768           0 :                 dst[4 * x + 0] = pred_L[0] = (r + ((3 * (pred_T[0] + pred_L[0]) - 2 * pred_TL[0]) >> 2)) & 0xff;
    1769           0 :                 dst[4 * x + 1] = pred_L[1] = (r + g + ((3 * (pred_T[1] + pred_L[1]) - 2 * pred_TL[1]) >> 2)) & 0xff;
    1770           0 :                 dst[4 * x + 2] = pred_L[2] = (r + g + b + ((3 * (pred_T[2] + pred_L[2]) - 2 * pred_TL[2]) >> 2)) & 0xff;
    1771             : 
    1772           0 :                 pred_TL[0] = pred_T[0];
    1773           0 :                 pred_TL[1] = pred_T[1];
    1774           0 :                 pred_TL[2] = pred_T[2];
    1775             :             }
    1776             :         }
    1777           0 :         dst += p->linesize[0];
    1778             :     }
    1779           0 : }
    1780             : 
    1781           0 : static int build_vlc(VLC *vlc, const uint8_t *len, int count)
    1782             : {
    1783             :     uint32_t codes[1024];
    1784             :     uint8_t bits[1024];
    1785             :     uint16_t syms[1024];
    1786             :     uint64_t index;
    1787             :     int i;
    1788             : 
    1789           0 :     index = 0;
    1790           0 :     for (i = 0; i < count; i++) {
    1791           0 :         codes[i]  = index >> (32 - len[i]);
    1792           0 :         bits[i] = len[i];
    1793           0 :         syms[i]  = i;
    1794           0 :         index += 1ULL << (32 - len[i]);
    1795             :     }
    1796             : 
    1797           0 :     ff_free_vlc(vlc);
    1798           0 :     return ff_init_vlc_sparse(vlc, 16, count,
    1799             :                               bits,  sizeof(*bits),  sizeof(*bits),
    1800             :                               codes, sizeof(*codes), sizeof(*codes),
    1801             :                               syms,  sizeof(*syms),  sizeof(*syms), 0);
    1802             : }
    1803             : 
    1804           0 : static int decode_frame(AVCodecContext *avctx,
    1805             :                         void *data, int *got_frame,
    1806             :                         AVPacket *avpkt)
    1807             : {
    1808           0 :     SheerVideoContext *s = avctx->priv_data;
    1809           0 :     ThreadFrame frame = { .f = data };
    1810           0 :     AVFrame *p = data;
    1811             :     GetBitContext gb;
    1812             :     unsigned format;
    1813             :     int ret;
    1814             : 
    1815           0 :     if (avpkt->size <= 20)
    1816           0 :         return AVERROR_INVALIDDATA;
    1817             : 
    1818           0 :     if (AV_RL32(avpkt->data) != MKTAG('S','h','i','r') &&
    1819           0 :         AV_RL32(avpkt->data) != MKTAG('Z','w','a','k'))
    1820           0 :         return AVERROR_INVALIDDATA;
    1821             : 
    1822           0 :     s->alt = 0;
    1823           0 :     format = AV_RL32(avpkt->data + 16);
    1824           0 :     av_log(avctx, AV_LOG_DEBUG, "format: %s\n", av_fourcc2str(format));
    1825           0 :     switch (format) {
    1826           0 :     case MKTAG(' ', 'R', 'G', 'B'):
    1827           0 :         avctx->pix_fmt = AV_PIX_FMT_RGB0;
    1828           0 :         s->decode_frame = decode_rgb;
    1829           0 :         if (s->format != format) {
    1830           0 :             ret  = build_vlc(&s->vlc[0], l_r_rgb, 256);
    1831           0 :             ret |= build_vlc(&s->vlc[1], l_g_rgb, 256);
    1832             :         }
    1833           0 :         break;
    1834           0 :     case MKTAG(' ', 'r', 'G', 'B'):
    1835           0 :         avctx->pix_fmt = AV_PIX_FMT_RGB0;
    1836           0 :         s->decode_frame = decode_rgbi;
    1837           0 :         if (s->format != format) {
    1838           0 :             ret  = build_vlc(&s->vlc[0], l_r_rgbi, 256);
    1839           0 :             ret |= build_vlc(&s->vlc[1], l_g_rgbi, 256);
    1840             :         }
    1841           0 :         break;
    1842           0 :     case MKTAG('A', 'R', 'G', 'X'):
    1843           0 :         avctx->pix_fmt = AV_PIX_FMT_GBRAP10;
    1844           0 :         s->decode_frame = decode_argx;
    1845           0 :         if (s->format != format) {
    1846           0 :             ret  = build_vlc(&s->vlc[0], l_r_rgbx, 1024);
    1847           0 :             ret |= build_vlc(&s->vlc[1], l_g_rgbx, 1024);
    1848             :         }
    1849           0 :         break;
    1850           0 :     case MKTAG('A', 'r', 'G', 'X'):
    1851           0 :         avctx->pix_fmt = AV_PIX_FMT_GBRAP10;
    1852           0 :         s->decode_frame = decode_argxi;
    1853           0 :         if (s->format != format) {
    1854           0 :             ret  = build_vlc(&s->vlc[0], l_r_rgbxi, 1024);
    1855           0 :             ret |= build_vlc(&s->vlc[1], l_g_rgbxi, 1024);
    1856             :         }
    1857           0 :         break;
    1858           0 :     case MKTAG('R', 'G', 'B', 'X'):
    1859           0 :         avctx->pix_fmt = AV_PIX_FMT_GBRP10;
    1860           0 :         s->decode_frame = decode_rgbx;
    1861           0 :         if (s->format != format) {
    1862           0 :             ret  = build_vlc(&s->vlc[0], l_r_rgbx, 1024);
    1863           0 :             ret |= build_vlc(&s->vlc[1], l_g_rgbx, 1024);
    1864             :         }
    1865           0 :         break;
    1866           0 :     case MKTAG('r', 'G', 'B', 'X'):
    1867           0 :         avctx->pix_fmt = AV_PIX_FMT_GBRP10;
    1868           0 :         s->decode_frame = decode_rgbxi;
    1869           0 :         if (s->format != format) {
    1870           0 :             ret  = build_vlc(&s->vlc[0], l_r_rgbxi, 1024);
    1871           0 :             ret |= build_vlc(&s->vlc[1], l_g_rgbxi, 1024);
    1872             :         }
    1873           0 :         break;
    1874           0 :     case MKTAG('A', 'R', 'G', 'B'):
    1875           0 :         avctx->pix_fmt = AV_PIX_FMT_ARGB;
    1876           0 :         s->decode_frame = decode_argb;
    1877           0 :         if (s->format != format) {
    1878           0 :             ret  = build_vlc(&s->vlc[0], l_r_rgb, 256);
    1879           0 :             ret |= build_vlc(&s->vlc[1], l_g_rgb, 256);
    1880             :         }
    1881           0 :         break;
    1882           0 :     case MKTAG('A', 'r', 'G', 'B'):
    1883           0 :         avctx->pix_fmt = AV_PIX_FMT_ARGB;
    1884           0 :         s->decode_frame = decode_argbi;
    1885           0 :         if (s->format != format) {
    1886           0 :             ret  = build_vlc(&s->vlc[0], l_r_rgbi, 256);
    1887           0 :             ret |= build_vlc(&s->vlc[1], l_g_rgbi, 256);
    1888             :         }
    1889           0 :         break;
    1890           0 :     case MKTAG('A', 'Y', 'B', 'R'):
    1891           0 :         s->alt = 1;
    1892           0 :     case MKTAG('A', 'Y', 'b', 'R'):
    1893           0 :         avctx->pix_fmt = AV_PIX_FMT_YUVA444P;
    1894           0 :         s->decode_frame = decode_aybr;
    1895           0 :         if (s->format != format) {
    1896           0 :             ret  = build_vlc(&s->vlc[0], l_y_ybr, 256);
    1897           0 :             ret |= build_vlc(&s->vlc[1], l_u_ybr, 256);
    1898             :         }
    1899           0 :         break;
    1900           0 :     case MKTAG('A', 'y', 'B', 'R'):
    1901           0 :         s->alt = 1;
    1902           0 :     case MKTAG('A', 'y', 'b', 'R'):
    1903           0 :         avctx->pix_fmt = AV_PIX_FMT_YUVA444P;
    1904           0 :         s->decode_frame = decode_aybri;
    1905           0 :         if (s->format != format) {
    1906           0 :             ret  = build_vlc(&s->vlc[0], l_y_ybri, 256);
    1907           0 :             ret |= build_vlc(&s->vlc[1], l_u_ybri, 256);
    1908             :         }
    1909           0 :         break;
    1910           0 :     case MKTAG(' ', 'Y', 'B', 'R'):
    1911           0 :         s->alt = 1;
    1912           0 :     case MKTAG(' ', 'Y', 'b', 'R'):
    1913           0 :         avctx->pix_fmt = AV_PIX_FMT_YUV444P;
    1914           0 :         s->decode_frame = decode_ybr;
    1915           0 :         if (s->format != format) {
    1916           0 :             ret  = build_vlc(&s->vlc[0], l_y_ybr, 256);
    1917           0 :             ret |= build_vlc(&s->vlc[1], l_u_ybr, 256);
    1918             :         }
    1919           0 :         break;
    1920           0 :     case MKTAG(' ', 'y', 'B', 'R'):
    1921           0 :         s->alt = 1;
    1922           0 :     case MKTAG(' ', 'y', 'b', 'R'):
    1923           0 :         avctx->pix_fmt = AV_PIX_FMT_YUV444P;
    1924           0 :         s->decode_frame = decode_ybri;
    1925           0 :         if (s->format != format) {
    1926           0 :             ret  = build_vlc(&s->vlc[0], l_y_ybri, 256);
    1927           0 :             ret |= build_vlc(&s->vlc[1], l_u_ybri, 256);
    1928             :         }
    1929           0 :         break;
    1930           0 :     case MKTAG('Y', 'B', 'R', 0x0a):
    1931           0 :         avctx->pix_fmt = AV_PIX_FMT_YUV444P10;
    1932           0 :         s->decode_frame = decode_ybr10;
    1933           0 :         if (s->format != format) {
    1934           0 :             ret  = build_vlc(&s->vlc[0], l_y_ybr10, 1024);
    1935           0 :             ret |= build_vlc(&s->vlc[1], l_u_ybr10, 1024);
    1936             :         }
    1937           0 :         break;
    1938           0 :     case MKTAG('y', 'B', 'R', 0x0a):
    1939           0 :         avctx->pix_fmt = AV_PIX_FMT_YUV444P10;
    1940           0 :         s->decode_frame = decode_ybr10i;
    1941           0 :         if (s->format != format) {
    1942           0 :             ret  = build_vlc(&s->vlc[0], l_y_ybr10i, 1024);
    1943           0 :             ret |= build_vlc(&s->vlc[1], l_u_ybr10i, 1024);
    1944             :         }
    1945           0 :         break;
    1946           0 :     case MKTAG('C', 'A', '4', 'p'):
    1947           0 :         avctx->pix_fmt = AV_PIX_FMT_YUVA444P10;
    1948           0 :         s->decode_frame = decode_ca4p;
    1949           0 :         if (s->format != format) {
    1950           0 :             ret  = build_vlc(&s->vlc[0], l_y_ybr10, 1024);
    1951           0 :             ret |= build_vlc(&s->vlc[1], l_u_ybr10, 1024);
    1952             :         }
    1953           0 :         break;
    1954           0 :     case MKTAG('C', 'A', '4', 'i'):
    1955           0 :         avctx->pix_fmt = AV_PIX_FMT_YUVA444P10;
    1956           0 :         s->decode_frame = decode_ca4i;
    1957           0 :         if (s->format != format) {
    1958           0 :             ret  = build_vlc(&s->vlc[0], l_y_ybr10i, 1024);
    1959           0 :             ret |= build_vlc(&s->vlc[1], l_u_ybr10i, 1024);
    1960             :         }
    1961           0 :         break;
    1962           0 :     case MKTAG('B', 'Y', 'R', 'Y'):
    1963           0 :         avctx->pix_fmt = AV_PIX_FMT_YUV422P;
    1964           0 :         s->decode_frame = decode_byry;
    1965           0 :         if (s->format != format) {
    1966           0 :             ret  = build_vlc(&s->vlc[0], l_y_byry, 256);
    1967           0 :             ret |= build_vlc(&s->vlc[1], l_u_byry, 256);
    1968             :         }
    1969           0 :         break;
    1970           0 :     case MKTAG('B', 'Y', 'R', 'y'):
    1971           0 :         avctx->pix_fmt = AV_PIX_FMT_YUV422P;
    1972           0 :         s->decode_frame = decode_byryi;
    1973           0 :         if (s->format != format) {
    1974           0 :             ret  = build_vlc(&s->vlc[0], l_y_byryi, 256);
    1975           0 :             ret |= build_vlc(&s->vlc[1], l_u_byryi, 256);
    1976             :         }
    1977           0 :         break;
    1978           0 :     case MKTAG('Y', 'b', 'Y', 'r'):
    1979           0 :         avctx->pix_fmt = AV_PIX_FMT_YUV422P;
    1980           0 :         s->decode_frame = decode_ybyr;
    1981           0 :         if (s->format != format) {
    1982           0 :             ret  = build_vlc(&s->vlc[0], l_y_ybyr, 256);
    1983           0 :             ret |= build_vlc(&s->vlc[1], l_u_ybyr, 256);
    1984             :         }
    1985           0 :         break;
    1986           0 :     case MKTAG('C', '8', '2', 'p'):
    1987           0 :         avctx->pix_fmt = AV_PIX_FMT_YUVA422P;
    1988           0 :         s->decode_frame = decode_c82p;
    1989           0 :         if (s->format != format) {
    1990           0 :             ret  = build_vlc(&s->vlc[0], l_y_byry, 256);
    1991           0 :             ret |= build_vlc(&s->vlc[1], l_u_byry, 256);
    1992             :         }
    1993           0 :         break;
    1994           0 :     case MKTAG('C', '8', '2', 'i'):
    1995           0 :         avctx->pix_fmt = AV_PIX_FMT_YUVA422P;
    1996           0 :         s->decode_frame = decode_c82i;
    1997           0 :         if (s->format != format) {
    1998           0 :             ret  = build_vlc(&s->vlc[0], l_y_byryi, 256);
    1999           0 :             ret |= build_vlc(&s->vlc[1], l_u_byryi, 256);
    2000             :         }
    2001           0 :         break;
    2002           0 :     case MKTAG(0xa2, 'Y', 'R', 'Y'):
    2003           0 :         avctx->pix_fmt = AV_PIX_FMT_YUV422P10;
    2004           0 :         s->decode_frame = decode_yry10;
    2005           0 :         if (s->format != format) {
    2006           0 :             ret  = build_vlc(&s->vlc[0], l_y_yry10, 1024);
    2007           0 :             ret |= build_vlc(&s->vlc[1], l_u_yry10, 1024);
    2008             :         }
    2009           0 :         break;
    2010           0 :     case MKTAG(0xa2, 'Y', 'R', 'y'):
    2011           0 :         avctx->pix_fmt = AV_PIX_FMT_YUV422P10;
    2012           0 :         s->decode_frame = decode_yry10i;
    2013           0 :         if (s->format != format) {
    2014           0 :             ret  = build_vlc(&s->vlc[0], l_y_yry10i, 1024);
    2015           0 :             ret |= build_vlc(&s->vlc[1], l_u_yry10i, 1024);
    2016             :         }
    2017           0 :         break;
    2018           0 :     case MKTAG('C', 'A', '2', 'p'):
    2019           0 :         avctx->pix_fmt = AV_PIX_FMT_YUVA422P10;
    2020           0 :         s->decode_frame = decode_ca2p;
    2021           0 :         if (s->format != format) {
    2022           0 :             ret  = build_vlc(&s->vlc[0], l_y_yry10, 1024);
    2023           0 :             ret |= build_vlc(&s->vlc[1], l_u_yry10, 1024);
    2024             :         }
    2025           0 :         break;
    2026           0 :     case MKTAG('C', 'A', '2', 'i'):
    2027           0 :         avctx->pix_fmt = AV_PIX_FMT_YUVA422P10;
    2028           0 :         s->decode_frame = decode_ca2i;
    2029           0 :         if (s->format != format) {
    2030           0 :             ret  = build_vlc(&s->vlc[0], l_y_yry10i, 1024);
    2031           0 :             ret |= build_vlc(&s->vlc[1], l_u_yry10i, 1024);
    2032             :         }
    2033           0 :         break;
    2034           0 :     default:
    2035           0 :         avpriv_request_sample(avctx, "unsupported format: 0x%X", format);
    2036           0 :         return AVERROR_PATCHWELCOME;
    2037             :     }
    2038             : 
    2039           0 :     if (avpkt->size < 20 + avctx->width * avctx->height / 16) {
    2040           0 :         av_log(avctx, AV_LOG_ERROR, "Input packet too small\n");
    2041           0 :         return AVERROR_INVALIDDATA;
    2042             :     }
    2043             : 
    2044           0 :     if (s->format != format) {
    2045           0 :         if (ret < 0)
    2046           0 :             return ret;
    2047           0 :         s->format = format;
    2048             :     }
    2049             : 
    2050           0 :     p->pict_type = AV_PICTURE_TYPE_I;
    2051           0 :     p->key_frame = 1;
    2052             : 
    2053           0 :     if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
    2054           0 :         return ret;
    2055             : 
    2056           0 :     if ((ret = init_get_bits8(&gb, avpkt->data + 20, avpkt->size - 20)) < 0)
    2057           0 :         return ret;
    2058             : 
    2059           0 :     s->decode_frame(avctx, p, &gb);
    2060             : 
    2061           0 :     *got_frame = 1;
    2062             : 
    2063           0 :     return avpkt->size;
    2064             : }
    2065             : 
    2066             : #if HAVE_THREADS
    2067           0 : static int decode_init_thread_copy(AVCodecContext *avctx)
    2068             : {
    2069           0 :     SheerVideoContext *s = avctx->priv_data;
    2070             : 
    2071           0 :     s->format = 0;
    2072           0 :     memset(&s->vlc[0], 0, sizeof(s->vlc[0]));
    2073           0 :     memset(&s->vlc[1], 0, sizeof(s->vlc[1]));
    2074             : 
    2075           0 :     return 0;
    2076             : }
    2077             : #endif
    2078             : 
    2079           0 : static av_cold int decode_end(AVCodecContext *avctx)
    2080             : {
    2081           0 :     SheerVideoContext *s = avctx->priv_data;
    2082             : 
    2083           0 :     ff_free_vlc(&s->vlc[0]);
    2084           0 :     ff_free_vlc(&s->vlc[1]);
    2085             : 
    2086           0 :     return 0;
    2087             : }
    2088             : 
    2089             : AVCodec ff_sheervideo_decoder = {
    2090             :     .name             = "sheervideo",
    2091             :     .long_name        = NULL_IF_CONFIG_SMALL("BitJazz SheerVideo"),
    2092             :     .type             = AVMEDIA_TYPE_VIDEO,
    2093             :     .id               = AV_CODEC_ID_SHEERVIDEO,
    2094             :     .priv_data_size   = sizeof(SheerVideoContext),
    2095             :     .init_thread_copy = ONLY_IF_THREADS_ENABLED(decode_init_thread_copy),
    2096             :     .close            = decode_end,
    2097             :     .decode           = decode_frame,
    2098             :     .capabilities     = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
    2099             : };

Generated by: LCOV version 1.13