LCOV - code coverage report
Current view: top level - libavcodec - svq3.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 701 863 81.2 %
Date: 2017-12-10 21:22:29 Functions: 18 18 100.0 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2003 The FFmpeg Project
       3             :  *
       4             :  * This file is part of FFmpeg.
       5             :  *
       6             :  * FFmpeg is free software; you can redistribute it and/or
       7             :  * modify it under the terms of the GNU Lesser General Public
       8             :  * License as published by the Free Software Foundation; either
       9             :  * version 2.1 of the License, or (at your option) any later version.
      10             :  *
      11             :  * FFmpeg is distributed in the hope that it will be useful,
      12             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      13             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      14             :  * Lesser General Public License for more details.
      15             :  *
      16             :  * You should have received a copy of the GNU Lesser General Public
      17             :  * License along with FFmpeg; if not, write to the Free Software
      18             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      19             :  */
      20             : 
      21             : /*
      22             :  * How to use this decoder:
      23             :  * SVQ3 data is transported within Apple Quicktime files. Quicktime files
      24             :  * have stsd atoms to describe media trak properties. A stsd atom for a
      25             :  * video trak contains 1 or more ImageDescription atoms. These atoms begin
      26             :  * with the 4-byte length of the atom followed by the codec fourcc. Some
      27             :  * decoders need information in this atom to operate correctly. Such
      28             :  * is the case with SVQ3. In order to get the best use out of this decoder,
      29             :  * the calling app must make the SVQ3 ImageDescription atom available
      30             :  * via the AVCodecContext's extradata[_size] field:
      31             :  *
      32             :  * AVCodecContext.extradata = pointer to ImageDescription, first characters
      33             :  * are expected to be 'S', 'V', 'Q', and '3', NOT the 4-byte atom length
      34             :  * AVCodecContext.extradata_size = size of ImageDescription atom memory
      35             :  * buffer (which will be the same as the ImageDescription atom size field
      36             :  * from the QT file, minus 4 bytes since the length is missing)
      37             :  *
      38             :  * You will know you have these parameters passed correctly when the decoder
      39             :  * correctly decodes this file:
      40             :  *  http://samples.mplayerhq.hu/V-codecs/SVQ3/Vertical400kbit.sorenson3.mov
      41             :  */
      42             : 
      43             : #include <inttypes.h>
      44             : 
      45             : #include "libavutil/attributes.h"
      46             : #include "internal.h"
      47             : #include "avcodec.h"
      48             : #include "mpegutils.h"
      49             : #include "h264dec.h"
      50             : #include "h264data.h"
      51             : #include "golomb.h"
      52             : #include "hpeldsp.h"
      53             : #include "mathops.h"
      54             : #include "rectangle.h"
      55             : #include "tpeldsp.h"
      56             : 
      57             : #if CONFIG_ZLIB
      58             : #include <zlib.h>
      59             : #endif
      60             : 
      61             : #include "svq1.h"
      62             : 
      63             : /**
      64             :  * @file
      65             :  * svq3 decoder.
      66             :  */
      67             : 
      68             : typedef struct SVQ3Frame {
      69             :     AVFrame *f;
      70             : 
      71             :     AVBufferRef *motion_val_buf[2];
      72             :     int16_t (*motion_val[2])[2];
      73             : 
      74             :     AVBufferRef *mb_type_buf;
      75             :     uint32_t *mb_type;
      76             : 
      77             : 
      78             :     AVBufferRef *ref_index_buf[2];
      79             :     int8_t *ref_index[2];
      80             : } SVQ3Frame;
      81             : 
      82             : typedef struct SVQ3Context {
      83             :     AVCodecContext *avctx;
      84             : 
      85             :     H264DSPContext  h264dsp;
      86             :     H264PredContext hpc;
      87             :     HpelDSPContext hdsp;
      88             :     TpelDSPContext tdsp;
      89             :     VideoDSPContext vdsp;
      90             : 
      91             :     SVQ3Frame *cur_pic;
      92             :     SVQ3Frame *next_pic;
      93             :     SVQ3Frame *last_pic;
      94             :     GetBitContext gb;
      95             :     GetBitContext gb_slice;
      96             :     uint8_t *slice_buf;
      97             :     int slice_size;
      98             :     int halfpel_flag;
      99             :     int thirdpel_flag;
     100             :     int has_watermark;
     101             :     uint32_t watermark_key;
     102             :     uint8_t *buf;
     103             :     int buf_size;
     104             :     int adaptive_quant;
     105             :     int next_p_frame_damaged;
     106             :     int h_edge_pos;
     107             :     int v_edge_pos;
     108             :     int last_frame_output;
     109             :     int slice_num;
     110             :     int qscale;
     111             :     int cbp;
     112             :     int frame_num;
     113             :     int frame_num_offset;
     114             :     int prev_frame_num_offset;
     115             :     int prev_frame_num;
     116             : 
     117             :     enum AVPictureType pict_type;
     118             :     enum AVPictureType slice_type;
     119             :     int low_delay;
     120             : 
     121             :     int mb_x, mb_y;
     122             :     int mb_xy;
     123             :     int mb_width, mb_height;
     124             :     int mb_stride, mb_num;
     125             :     int b_stride;
     126             : 
     127             :     uint32_t *mb2br_xy;
     128             : 
     129             :     int chroma_pred_mode;
     130             :     int intra16x16_pred_mode;
     131             : 
     132             :     int8_t   intra4x4_pred_mode_cache[5 * 8];
     133             :     int8_t (*intra4x4_pred_mode);
     134             : 
     135             :     unsigned int top_samples_available;
     136             :     unsigned int topright_samples_available;
     137             :     unsigned int left_samples_available;
     138             : 
     139             :     uint8_t *edge_emu_buffer;
     140             : 
     141             :     DECLARE_ALIGNED(16, int16_t, mv_cache)[2][5 * 8][2];
     142             :     DECLARE_ALIGNED(8,  int8_t, ref_cache)[2][5 * 8];
     143             :     DECLARE_ALIGNED(16, int16_t, mb)[16 * 48 * 2];
     144             :     DECLARE_ALIGNED(16, int16_t, mb_luma_dc)[3][16 * 2];
     145             :     DECLARE_ALIGNED(8, uint8_t, non_zero_count_cache)[15 * 8];
     146             :     uint32_t dequant4_coeff[QP_MAX_NUM + 1][16];
     147             :     int block_offset[2 * (16 * 3)];
     148             : } SVQ3Context;
     149             : 
     150             : #define FULLPEL_MODE  1
     151             : #define HALFPEL_MODE  2
     152             : #define THIRDPEL_MODE 3
     153             : #define PREDICT_MODE  4
     154             : 
     155             : /* dual scan (from some older H.264 draft)
     156             :  * o-->o-->o   o
     157             :  *         |  /|
     158             :  * o   o   o / o
     159             :  * | / |   |/  |
     160             :  * o   o   o   o
     161             :  *   /
     162             :  * o-->o-->o-->o
     163             :  */
     164             : static const uint8_t svq3_scan[16] = {
     165             :     0 + 0 * 4, 1 + 0 * 4, 2 + 0 * 4, 2 + 1 * 4,
     166             :     2 + 2 * 4, 3 + 0 * 4, 3 + 1 * 4, 3 + 2 * 4,
     167             :     0 + 1 * 4, 0 + 2 * 4, 1 + 1 * 4, 1 + 2 * 4,
     168             :     0 + 3 * 4, 1 + 3 * 4, 2 + 3 * 4, 3 + 3 * 4,
     169             : };
     170             : 
     171             : static const uint8_t luma_dc_zigzag_scan[16] = {
     172             :     0 * 16 + 0 * 64, 1 * 16 + 0 * 64, 2 * 16 + 0 * 64, 0 * 16 + 2 * 64,
     173             :     3 * 16 + 0 * 64, 0 * 16 + 1 * 64, 1 * 16 + 1 * 64, 2 * 16 + 1 * 64,
     174             :     1 * 16 + 2 * 64, 2 * 16 + 2 * 64, 3 * 16 + 2 * 64, 0 * 16 + 3 * 64,
     175             :     3 * 16 + 1 * 64, 1 * 16 + 3 * 64, 2 * 16 + 3 * 64, 3 * 16 + 3 * 64,
     176             : };
     177             : 
     178             : static const uint8_t svq3_pred_0[25][2] = {
     179             :     { 0, 0 },
     180             :     { 1, 0 }, { 0, 1 },
     181             :     { 0, 2 }, { 1, 1 }, { 2, 0 },
     182             :     { 3, 0 }, { 2, 1 }, { 1, 2 }, { 0, 3 },
     183             :     { 0, 4 }, { 1, 3 }, { 2, 2 }, { 3, 1 }, { 4, 0 },
     184             :     { 4, 1 }, { 3, 2 }, { 2, 3 }, { 1, 4 },
     185             :     { 2, 4 }, { 3, 3 }, { 4, 2 },
     186             :     { 4, 3 }, { 3, 4 },
     187             :     { 4, 4 }
     188             : };
     189             : 
     190             : static const int8_t svq3_pred_1[6][6][5] = {
     191             :     { { 2, -1, -1, -1, -1 }, { 2, 1, -1, -1, -1 }, { 1, 2, -1, -1, -1 },
     192             :       { 2,  1, -1, -1, -1 }, { 1, 2, -1, -1, -1 }, { 1, 2, -1, -1, -1 } },
     193             :     { { 0,  2, -1, -1, -1 }, { 0, 2,  1,  4,  3 }, { 0, 1,  2,  4,  3 },
     194             :       { 0,  2,  1,  4,  3 }, { 2, 0,  1,  3,  4 }, { 0, 4,  2,  1,  3 } },
     195             :     { { 2,  0, -1, -1, -1 }, { 2, 1,  0,  4,  3 }, { 1, 2,  4,  0,  3 },
     196             :       { 2,  1,  0,  4,  3 }, { 2, 1,  4,  3,  0 }, { 1, 2,  4,  0,  3 } },
     197             :     { { 2,  0, -1, -1, -1 }, { 2, 0,  1,  4,  3 }, { 1, 2,  0,  4,  3 },
     198             :       { 2,  1,  0,  4,  3 }, { 2, 1,  3,  4,  0 }, { 2, 4,  1,  0,  3 } },
     199             :     { { 0,  2, -1, -1, -1 }, { 0, 2,  1,  3,  4 }, { 1, 2,  3,  0,  4 },
     200             :       { 2,  0,  1,  3,  4 }, { 2, 1,  3,  0,  4 }, { 2, 0,  4,  3,  1 } },
     201             :     { { 0,  2, -1, -1, -1 }, { 0, 2,  4,  1,  3 }, { 1, 4,  2,  0,  3 },
     202             :       { 4,  2,  0,  1,  3 }, { 2, 0,  1,  4,  3 }, { 4, 2,  1,  0,  3 } },
     203             : };
     204             : 
     205             : static const struct {
     206             :     uint8_t run;
     207             :     uint8_t level;
     208             : } svq3_dct_tables[2][16] = {
     209             :     { { 0, 0 }, { 0, 1 }, { 1, 1 }, { 2, 1 }, { 0, 2 }, { 3, 1 }, { 4, 1 }, { 5, 1 },
     210             :       { 0, 3 }, { 1, 2 }, { 2, 2 }, { 6, 1 }, { 7, 1 }, { 8, 1 }, { 9, 1 }, { 0, 4 } },
     211             :     { { 0, 0 }, { 0, 1 }, { 1, 1 }, { 0, 2 }, { 2, 1 }, { 0, 3 }, { 0, 4 }, { 0, 5 },
     212             :       { 3, 1 }, { 4, 1 }, { 1, 2 }, { 1, 3 }, { 0, 6 }, { 0, 7 }, { 0, 8 }, { 0, 9 } }
     213             : };
     214             : 
     215             : static const uint32_t svq3_dequant_coeff[32] = {
     216             :      3881,  4351,  4890,  5481,   6154,   6914,   7761,   8718,
     217             :      9781, 10987, 12339, 13828,  15523,  17435,  19561,  21873,
     218             :     24552, 27656, 30847, 34870,  38807,  43747,  49103,  54683,
     219             :     61694, 68745, 77615, 89113, 100253, 109366, 126635, 141533
     220             : };
     221             : 
     222             : static int svq3_decode_end(AVCodecContext *avctx);
     223             : 
     224        1397 : static void svq3_luma_dc_dequant_idct_c(int16_t *output, int16_t *input, int qp)
     225             : {
     226        1397 :     const unsigned qmul = svq3_dequant_coeff[qp];
     227             : #define stride 16
     228             :     int i;
     229             :     int temp[16];
     230             :     static const uint8_t x_offset[4] = { 0, 1 * stride, 4 * stride, 5 * stride };
     231             : 
     232        6985 :     for (i = 0; i < 4; i++) {
     233        5588 :         const int z0 = 13 * (input[4 * i + 0] +      input[4 * i + 2]);
     234        5588 :         const int z1 = 13 * (input[4 * i + 0] -      input[4 * i + 2]);
     235        5588 :         const int z2 =  7 *  input[4 * i + 1] - 17 * input[4 * i + 3];
     236        5588 :         const int z3 = 17 *  input[4 * i + 1] +  7 * input[4 * i + 3];
     237             : 
     238        5588 :         temp[4 * i + 0] = z0 + z3;
     239        5588 :         temp[4 * i + 1] = z1 + z2;
     240        5588 :         temp[4 * i + 2] = z1 - z2;
     241        5588 :         temp[4 * i + 3] = z0 - z3;
     242             :     }
     243             : 
     244        6985 :     for (i = 0; i < 4; i++) {
     245        5588 :         const int offset = x_offset[i];
     246        5588 :         const int z0     = 13 * (temp[4 * 0 + i] +      temp[4 * 2 + i]);
     247        5588 :         const int z1     = 13 * (temp[4 * 0 + i] -      temp[4 * 2 + i]);
     248        5588 :         const int z2     =  7 *  temp[4 * 1 + i] - 17 * temp[4 * 3 + i];
     249        5588 :         const int z3     = 17 *  temp[4 * 1 + i] +  7 * temp[4 * 3 + i];
     250             : 
     251        5588 :         output[stride *  0 + offset] = (int)((z0 + z3) * qmul + 0x80000) >> 20;
     252        5588 :         output[stride *  2 + offset] = (int)((z1 + z2) * qmul + 0x80000) >> 20;
     253        5588 :         output[stride *  8 + offset] = (int)((z1 - z2) * qmul + 0x80000) >> 20;
     254        5588 :         output[stride * 10 + offset] = (int)((z0 - z3) * qmul + 0x80000) >> 20;
     255             :     }
     256        1397 : }
     257             : #undef stride
     258             : 
     259     2193293 : static void svq3_add_idct_c(uint8_t *dst, int16_t *block,
     260             :                             int stride, int qp, int dc)
     261             : {
     262     2193293 :     const int qmul = svq3_dequant_coeff[qp];
     263             :     int i;
     264             : 
     265     2193293 :     if (dc) {
     266     1049966 :         dc       = 13 * 13 * (dc == 1 ? 1538U* block[0]
     267      517849 :                                       : qmul * (block[0] >> 3) / 2);
     268      532117 :         block[0] = 0;
     269             :     }
     270             : 
     271    10966465 :     for (i = 0; i < 4; i++) {
     272     8773172 :         const int z0 = 13 * (block[0 + 4 * i] +      block[2 + 4 * i]);
     273     8773172 :         const int z1 = 13 * (block[0 + 4 * i] -      block[2 + 4 * i]);
     274     8773172 :         const int z2 =  7 *  block[1 + 4 * i] - 17 * block[3 + 4 * i];
     275     8773172 :         const int z3 = 17 *  block[1 + 4 * i] +  7 * block[3 + 4 * i];
     276             : 
     277     8773172 :         block[0 + 4 * i] = z0 + z3;
     278     8773172 :         block[1 + 4 * i] = z1 + z2;
     279     8773172 :         block[2 + 4 * i] = z1 - z2;
     280     8773172 :         block[3 + 4 * i] = z0 - z3;
     281             :     }
     282             : 
     283    10966465 :     for (i = 0; i < 4; i++) {
     284     8773172 :         const unsigned z0 = 13 * (block[i + 4 * 0] +      block[i + 4 * 2]);
     285     8773172 :         const unsigned z1 = 13 * (block[i + 4 * 0] -      block[i + 4 * 2]);
     286     8773172 :         const unsigned z2 =  7 *  block[i + 4 * 1] - 17 * block[i + 4 * 3];
     287     8773172 :         const unsigned z3 = 17 *  block[i + 4 * 1] +  7 * block[i + 4 * 3];
     288     8773172 :         const int rr = (dc + 0x80000u);
     289             : 
     290     8773172 :         dst[i + stride * 0] = av_clip_uint8(dst[i + stride * 0] + ((int)((z0 + z3) * qmul + rr) >> 20));
     291     8773172 :         dst[i + stride * 1] = av_clip_uint8(dst[i + stride * 1] + ((int)((z1 + z2) * qmul + rr) >> 20));
     292     8773172 :         dst[i + stride * 2] = av_clip_uint8(dst[i + stride * 2] + ((int)((z1 - z2) * qmul + rr) >> 20));
     293     8773172 :         dst[i + stride * 3] = av_clip_uint8(dst[i + stride * 3] + ((int)((z0 - z3) * qmul + rr) >> 20));
     294             :     }
     295             : 
     296     2193293 :     memset(block, 0, 16 * sizeof(int16_t));
     297     2193293 : }
     298             : 
     299     2121237 : static inline int svq3_decode_block(GetBitContext *gb, int16_t *block,
     300             :                                     int index, const int type)
     301             : {
     302             :     static const uint8_t *const scan_patterns[4] = {
     303             :         luma_dc_zigzag_scan, ff_zigzag_scan, svq3_scan, ff_h264_chroma_dc_scan
     304             :     };
     305             : 
     306             :     int run, level, sign, limit;
     307             :     unsigned vlc;
     308     2121237 :     const int intra           = 3 * type >> 2;
     309     2121237 :     const uint8_t *const scan = scan_patterns[type];
     310             : 
     311     2772453 :     for (limit = (16 >> intra); index < 16; index = limit, limit += 8) {
     312     5436753 :         for (; (vlc = get_interleaved_ue_golomb(gb)) != 0; index++) {
     313     2989908 :             if ((int32_t)vlc < 0)
     314           0 :                 return -1;
     315             : 
     316     2989908 :             sign     = (vlc & 1) ? 0 : -1;
     317     2989908 :             vlc      = vlc + 1 >> 1;
     318             : 
     319     2989908 :             if (type == 3) {
     320      134319 :                 if (vlc < 3) {
     321       90948 :                     run   = 0;
     322       90948 :                     level = vlc;
     323       43371 :                 } else if (vlc < 4) {
     324       18342 :                     run   = 1;
     325       18342 :                     level = 1;
     326             :                 } else {
     327       25029 :                     run   = vlc & 0x3;
     328       25029 :                     level = (vlc + 9 >> 2) - run;
     329             :                 }
     330             :             } else {
     331     2855589 :                 if (vlc < 16U) {
     332     2803024 :                     run   = svq3_dct_tables[intra][vlc].run;
     333     2803024 :                     level = svq3_dct_tables[intra][vlc].level;
     334       52565 :                 } else if (intra) {
     335        4823 :                     run   = vlc & 0x7;
     336        4823 :                     level = (vlc >> 3) + ((run == 0) ? 8 : ((run < 2) ? 2 : ((run < 5) ? 0 : -1)));
     337             :                 } else {
     338       47742 :                     run   = vlc & 0xF;
     339       47742 :                     level = (vlc >> 4) + ((run == 0) ? 4 : ((run < 3) ? 2 : ((run < 10) ? 1 : 0)));
     340             :                 }
     341             :             }
     342             : 
     343             : 
     344     2989908 :             if ((index += run) >= limit)
     345           0 :                 return -1;
     346             : 
     347     2989908 :             block[scan[index]] = (level ^ sign) - sign;
     348             :         }
     349             : 
     350     2446845 :         if (type != 2) {
     351     1795629 :             break;
     352             :         }
     353             :     }
     354             : 
     355     2121237 :     return 0;
     356             : }
     357             : 
     358             : static av_always_inline int
     359      298548 : svq3_fetch_diagonal_mv(const SVQ3Context *s, const int16_t **C,
     360             :                        int i, int list, int part_width)
     361             : {
     362      298548 :     const int topright_ref = s->ref_cache[list][i - 8 + part_width];
     363             : 
     364      298548 :     if (topright_ref != PART_NOT_AVAILABLE) {
     365      254358 :         *C = s->mv_cache[list][i - 8 + part_width];
     366      254358 :         return topright_ref;
     367             :     } else {
     368       44190 :         *C = s->mv_cache[list][i - 8 - 1];
     369       44190 :         return s->ref_cache[list][i - 8 - 1];
     370             :     }
     371             : }
     372             : 
     373             : /**
     374             :  * Get the predicted MV.
     375             :  * @param n the block index
     376             :  * @param part_width the width of the partition (4, 8,16) -> (1, 2, 4)
     377             :  * @param mx the x component of the predicted motion vector
     378             :  * @param my the y component of the predicted motion vector
     379             :  */
     380      298548 : static av_always_inline void svq3_pred_motion(const SVQ3Context *s, int n,
     381             :                                               int part_width, int list,
     382             :                                               int ref, int *const mx, int *const my)
     383             : {
     384      298548 :     const int index8       = scan8[n];
     385      298548 :     const int top_ref      = s->ref_cache[list][index8 - 8];
     386      298548 :     const int left_ref     = s->ref_cache[list][index8 - 1];
     387      298548 :     const int16_t *const A = s->mv_cache[list][index8 - 1];
     388      298548 :     const int16_t *const B = s->mv_cache[list][index8 - 8];
     389             :     const int16_t *C;
     390             :     int diagonal_ref, match_count;
     391             : 
     392             : /* mv_cache
     393             :  * B . . A T T T T
     394             :  * U . . L . . , .
     395             :  * U . . L . . . .
     396             :  * U . . L . . , .
     397             :  * . . . L . . . .
     398             :  */
     399             : 
     400      298548 :     diagonal_ref = svq3_fetch_diagonal_mv(s, &C, index8, list, part_width);
     401      298548 :     match_count  = (diagonal_ref == ref) + (top_ref == ref) + (left_ref == ref);
     402      298548 :     if (match_count > 1) { //most common
     403      285967 :         *mx = mid_pred(A[0], B[0], C[0]);
     404      285967 :         *my = mid_pred(A[1], B[1], C[1]);
     405       12581 :     } else if (match_count == 1) {
     406       12581 :         if (left_ref == ref) {
     407       12581 :             *mx = A[0];
     408       12581 :             *my = A[1];
     409           0 :         } else if (top_ref == ref) {
     410           0 :             *mx = B[0];
     411           0 :             *my = B[1];
     412             :         } else {
     413           0 :             *mx = C[0];
     414           0 :             *my = C[1];
     415             :         }
     416             :     } else {
     417           0 :         if (top_ref      == PART_NOT_AVAILABLE &&
     418           0 :             diagonal_ref == PART_NOT_AVAILABLE &&
     419             :             left_ref     != PART_NOT_AVAILABLE) {
     420           0 :             *mx = A[0];
     421           0 :             *my = A[1];
     422             :         } else {
     423           0 :             *mx = mid_pred(A[0], B[0], C[0]);
     424           0 :             *my = mid_pred(A[1], B[1], C[1]);
     425             :         }
     426             :     }
     427      298548 : }
     428             : 
     429      487569 : static inline void svq3_mc_dir_part(SVQ3Context *s,
     430             :                                     int x, int y, int width, int height,
     431             :                                     int mx, int my, int dxy,
     432             :                                     int thirdpel, int dir, int avg)
     433             : {
     434      487569 :     const SVQ3Frame *pic = (dir == 0) ? s->last_pic : s->next_pic;
     435             :     uint8_t *src, *dest;
     436      487569 :     int i, emu = 0;
     437      487569 :     int blocksize = 2 - (width >> 3); // 16->0, 8->1, 4->2
     438      487569 :     int linesize   = s->cur_pic->f->linesize[0];
     439      487569 :     int uvlinesize = s->cur_pic->f->linesize[1];
     440             : 
     441      487569 :     mx += x;
     442      487569 :     my += y;
     443             : 
     444      487569 :     if (mx < 0 || mx >= s->h_edge_pos - width  - 1 ||
     445      465782 :         my < 0 || my >= s->v_edge_pos - height - 1) {
     446       47114 :         emu = 1;
     447       47114 :         mx = av_clip(mx, -16, s->h_edge_pos - width  + 15);
     448       47114 :         my = av_clip(my, -16, s->v_edge_pos - height + 15);
     449             :     }
     450             : 
     451             :     /* form component predictions */
     452      487569 :     dest = s->cur_pic->f->data[0] + x + y * linesize;
     453      487569 :     src  = pic->f->data[0] + mx + my * linesize;
     454             : 
     455      487569 :     if (emu) {
     456       47114 :         s->vdsp.emulated_edge_mc(s->edge_emu_buffer, src,
     457             :                                  linesize, linesize,
     458             :                                  width + 1, height + 1,
     459             :                                  mx, my, s->h_edge_pos, s->v_edge_pos);
     460       47114 :         src = s->edge_emu_buffer;
     461             :     }
     462      487569 :     if (thirdpel)
     463             :         (avg ? s->tdsp.avg_tpel_pixels_tab
     464         182 :              : s->tdsp.put_tpel_pixels_tab)[dxy](dest, src, linesize,
     465             :                                                  width, height);
     466             :     else
     467             :         (avg ? s->hdsp.avg_pixels_tab
     468      487387 :              : s->hdsp.put_pixels_tab)[blocksize][dxy](dest, src, linesize,
     469             :                                                        height);
     470             : 
     471      487569 :     if (!(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
     472      487569 :         mx     = mx + (mx < (int) x) >> 1;
     473      487569 :         my     = my + (my < (int) y) >> 1;
     474      487569 :         width  = width  >> 1;
     475      487569 :         height = height >> 1;
     476      487569 :         blocksize++;
     477             : 
     478     1462707 :         for (i = 1; i < 3; i++) {
     479      975138 :             dest = s->cur_pic->f->data[i] + (x >> 1) + (y >> 1) * uvlinesize;
     480      975138 :             src  = pic->f->data[i] + mx + my * uvlinesize;
     481             : 
     482      975138 :             if (emu) {
     483      282684 :                 s->vdsp.emulated_edge_mc(s->edge_emu_buffer, src,
     484             :                                          uvlinesize, uvlinesize,
     485             :                                          width + 1, height + 1,
     486       94228 :                                          mx, my, (s->h_edge_pos >> 1),
     487       94228 :                                          s->v_edge_pos >> 1);
     488       94228 :                 src = s->edge_emu_buffer;
     489             :             }
     490      975138 :             if (thirdpel)
     491             :                 (avg ? s->tdsp.avg_tpel_pixels_tab
     492         364 :                      : s->tdsp.put_tpel_pixels_tab)[dxy](dest, src,
     493             :                                                          uvlinesize,
     494             :                                                          width, height);
     495             :             else
     496             :                 (avg ? s->hdsp.avg_pixels_tab
     497      974774 :                      : s->hdsp.put_pixels_tab)[blocksize][dxy](dest, src,
     498             :                                                                uvlinesize,
     499             :                                                                height);
     500             :         }
     501             :     }
     502      487569 : }
     503             : 
     504      233455 : static inline int svq3_mc_dir(SVQ3Context *s, int size, int mode,
     505             :                               int dir, int avg)
     506             : {
     507             :     int i, j, k, mx, my, dx, dy, x, y;
     508      233455 :     const int part_width    = ((size & 5) == 4) ? 4 : 16 >> (size & 1);
     509      233455 :     const int part_height   = 16 >> ((unsigned)(size + 1) / 3);
     510      233455 :     const int extra_width   = (mode == PREDICT_MODE) ? -16 * 6 : 0;
     511      233455 :     const int h_edge_pos    = 6 * (s->h_edge_pos - part_width)  - extra_width;
     512      233455 :     const int v_edge_pos    = 6 * (s->v_edge_pos - part_height) - extra_width;
     513             : 
     514      488909 :     for (i = 0; i < 16; i += part_height)
     515      555040 :         for (j = 0; j < 16; j += part_width) {
     516      599172 :             const int b_xy = (4 * s->mb_x + (j >> 2)) +
     517      299586 :                              (4 * s->mb_y + (i >> 2)) * s->b_stride;
     518             :             int dxy;
     519      299586 :             x = 16 * s->mb_x + j;
     520      299586 :             y = 16 * s->mb_y + i;
     521      898758 :             k = (j >> 2 & 1) + (i >> 1 & 2) +
     522      599172 :                 (j >> 1 & 4) + (i      & 8);
     523             : 
     524      299586 :             if (mode != PREDICT_MODE) {
     525      298548 :                 svq3_pred_motion(s, k, part_width >> 2, dir, 1, &mx, &my);
     526             :             } else {
     527        1038 :                 mx = s->next_pic->motion_val[0][b_xy][0] * 2;
     528        1038 :                 my = s->next_pic->motion_val[0][b_xy][1] * 2;
     529             : 
     530        1038 :                 if (dir == 0) {
     531        1557 :                     mx = mx * s->frame_num_offset /
     532        1038 :                          s->prev_frame_num_offset + 1 >> 1;
     533        1557 :                     my = my * s->frame_num_offset /
     534        1038 :                          s->prev_frame_num_offset + 1 >> 1;
     535             :                 } else {
     536        1557 :                     mx = mx * (s->frame_num_offset - s->prev_frame_num_offset) /
     537        1038 :                          s->prev_frame_num_offset + 1 >> 1;
     538        1557 :                     my = my * (s->frame_num_offset - s->prev_frame_num_offset) /
     539        1038 :                          s->prev_frame_num_offset + 1 >> 1;
     540             :                 }
     541             :             }
     542             : 
     543             :             /* clip motion vector prediction to frame border */
     544      299586 :             mx = av_clip(mx, extra_width - 6 * x, h_edge_pos - 6 * x);
     545      299586 :             my = av_clip(my, extra_width - 6 * y, v_edge_pos - 6 * y);
     546             : 
     547             :             /* get (optional) motion vector differential */
     548      299586 :             if (mode == PREDICT_MODE) {
     549        1038 :                 dx = dy = 0;
     550             :             } else {
     551      298548 :                 dy = get_interleaved_se_golomb(&s->gb_slice);
     552      298548 :                 dx = get_interleaved_se_golomb(&s->gb_slice);
     553             : 
     554      298548 :                 if (dx != (int16_t)dx || dy != (int16_t)dy) {
     555           0 :                     av_log(s->avctx, AV_LOG_ERROR, "invalid MV vlc\n");
     556           0 :                     return -1;
     557             :                 }
     558             :             }
     559             : 
     560             :             /* compute motion vector */
     561      299586 :             if (mode == THIRDPEL_MODE) {
     562             :                 int fx, fy;
     563         182 :                 mx  = (mx + 1 >> 1) + dx;
     564         182 :                 my  = (my + 1 >> 1) + dy;
     565         182 :                 fx  = (unsigned)(mx + 0x30000) / 3 - 0x10000;
     566         182 :                 fy  = (unsigned)(my + 0x30000) / 3 - 0x10000;
     567         182 :                 dxy = (mx - 3 * fx) + 4 * (my - 3 * fy);
     568             : 
     569         182 :                 svq3_mc_dir_part(s, x, y, part_width, part_height,
     570             :                                  fx, fy, dxy, 1, dir, avg);
     571         182 :                 mx += mx;
     572         182 :                 my += my;
     573      299404 :             } else if (mode == HALFPEL_MODE || mode == PREDICT_MODE) {
     574      149538 :                 mx  = (unsigned)(mx + 1 + 0x30000) / 3 + dx - 0x10000;
     575      149538 :                 my  = (unsigned)(my + 1 + 0x30000) / 3 + dy - 0x10000;
     576      149538 :                 dxy = (mx & 1) + 2 * (my & 1);
     577             : 
     578      149538 :                 svq3_mc_dir_part(s, x, y, part_width, part_height,
     579             :                                  mx >> 1, my >> 1, dxy, 0, dir, avg);
     580      149538 :                 mx *= 3;
     581      149538 :                 my *= 3;
     582             :             } else {
     583      149866 :                 mx = (unsigned)(mx + 3 + 0x60000) / 6 + dx - 0x10000;
     584      149866 :                 my = (unsigned)(my + 3 + 0x60000) / 6 + dy - 0x10000;
     585             : 
     586      149866 :                 svq3_mc_dir_part(s, x, y, part_width, part_height,
     587             :                                  mx, my, 0, 0, dir, avg);
     588      149866 :                 mx *= 6;
     589      149866 :                 my *= 6;
     590             :             }
     591             : 
     592             :             /* update mv_cache */
     593      299586 :             if (mode != PREDICT_MODE) {
     594      298548 :                 int32_t mv = pack16to32(mx, my);
     595             : 
     596      298548 :                 if (part_height == 8 && i < 8) {
     597       43807 :                     AV_WN32A(s->mv_cache[dir][scan8[k] + 1 * 8], mv);
     598             : 
     599       43807 :                     if (part_width == 8 && j < 8)
     600       21864 :                         AV_WN32A(s->mv_cache[dir][scan8[k] + 1 + 1 * 8], mv);
     601             :                 }
     602      298548 :                 if (part_width == 8 && j < 8)
     603       43840 :                     AV_WN32A(s->mv_cache[dir][scan8[k] + 1], mv);
     604      298548 :                 if (part_width == 4 || part_height == 4)
     605         184 :                     AV_WN32A(s->mv_cache[dir][scan8[k]], mv);
     606             :             }
     607             : 
     608             :             /* write back motion vectors */
     609      299586 :             fill_rectangle(s->cur_pic->motion_val[dir][b_xy],
     610             :                            part_width >> 2, part_height >> 2, s->b_stride,
     611             :                            pack16to32(mx, my), 4);
     612             :         }
     613             : 
     614      233455 :     return 0;
     615             : }
     616             : 
     617      261865 : static av_always_inline void hl_decode_mb_idct_luma(SVQ3Context *s,
     618             :                                                     int mb_type, const int *block_offset,
     619             :                                                     int linesize, uint8_t *dest_y)
     620             : {
     621             :     int i;
     622      261865 :     if (!IS_INTRA4x4(mb_type)) {
     623     3978765 :         for (i = 0; i < 16; i++)
     624     3744720 :             if (s->non_zero_count_cache[scan8[i]] || s->mb[i * 16]) {
     625     1349680 :                 uint8_t *const ptr = dest_y + block_offset[i];
     626     1349680 :                 svq3_add_idct_c(ptr, s->mb + i * 16, linesize,
     627     1349680 :                                 s->qscale, IS_INTRA(mb_type) ? 1 : 0);
     628             :             }
     629             :     }
     630      261865 : }
     631             : 
     632       29217 : static av_always_inline void hl_decode_mb_predict_luma(SVQ3Context *s,
     633             :                                                        int mb_type,
     634             :                                                        const int *block_offset,
     635             :                                                        int linesize,
     636             :                                                        uint8_t *dest_y)
     637             : {
     638             :     int i;
     639       29217 :     int qscale = s->qscale;
     640             : 
     641       29217 :     if (IS_INTRA4x4(mb_type)) {
     642      472940 :         for (i = 0; i < 16; i++) {
     643      445120 :             uint8_t *const ptr = dest_y + block_offset[i];
     644      445120 :             const int dir      = s->intra4x4_pred_mode_cache[scan8[i]];
     645             : 
     646             :             uint8_t *topright;
     647             :             int nnz, tr;
     648      476509 :             if (dir == DIAG_DOWN_LEFT_PRED || dir == VERT_LEFT_PRED) {
     649       31389 :                 const int topright_avail = (s->topright_samples_available << i) & 0x8000;
     650             :                 av_assert2(s->mb_y || linesize <= block_offset[i]);
     651       31389 :                 if (!topright_avail) {
     652           0 :                     tr       = ptr[3 - linesize] * 0x01010101u;
     653           0 :                     topright = (uint8_t *)&tr;
     654             :                 } else
     655       31389 :                     topright = ptr + 4 - linesize;
     656             :             } else
     657      413731 :                 topright = NULL;
     658             : 
     659      445120 :             s->hpc.pred4x4[dir](ptr, topright, linesize);
     660      445120 :             nnz = s->non_zero_count_cache[scan8[i]];
     661      445120 :             if (nnz) {
     662      325764 :                 svq3_add_idct_c(ptr, s->mb + i * 16, linesize, qscale, 0);
     663             :             }
     664             :         }
     665             :     } else {
     666        1397 :         s->hpc.pred16x16[s->intra16x16_pred_mode](dest_y, linesize);
     667        1397 :         svq3_luma_dc_dequant_idct_c(s->mb, s->mb_luma_dc[0], qscale);
     668             :     }
     669       29217 : }
     670             : 
     671      261865 : static void hl_decode_mb(SVQ3Context *s)
     672             : {
     673      261865 :     const int mb_x    = s->mb_x;
     674      261865 :     const int mb_y    = s->mb_y;
     675      261865 :     const int mb_xy   = s->mb_xy;
     676      261865 :     const int mb_type = s->cur_pic->mb_type[mb_xy];
     677             :     uint8_t *dest_y, *dest_cb, *dest_cr;
     678             :     int linesize, uvlinesize;
     679             :     int i, j;
     680      261865 :     const int *block_offset = &s->block_offset[0];
     681      261865 :     const int block_h   = 16 >> 1;
     682             : 
     683      261865 :     linesize   = s->cur_pic->f->linesize[0];
     684      261865 :     uvlinesize = s->cur_pic->f->linesize[1];
     685             : 
     686      261865 :     dest_y  = s->cur_pic->f->data[0] + (mb_x     + mb_y * linesize)  * 16;
     687      261865 :     dest_cb = s->cur_pic->f->data[1] +  mb_x * 8 + mb_y * uvlinesize * block_h;
     688      261865 :     dest_cr = s->cur_pic->f->data[2] +  mb_x * 8 + mb_y * uvlinesize * block_h;
     689             : 
     690      261865 :     s->vdsp.prefetch(dest_y  + (s->mb_x & 3) * 4 * linesize   + 64, linesize,      4);
     691      261865 :     s->vdsp.prefetch(dest_cb + (s->mb_x & 7)     * uvlinesize + 64, dest_cr - dest_cb, 2);
     692             : 
     693      261865 :     if (IS_INTRA(mb_type)) {
     694       29217 :         s->hpc.pred8x8[s->chroma_pred_mode](dest_cb, uvlinesize);
     695       29217 :         s->hpc.pred8x8[s->chroma_pred_mode](dest_cr, uvlinesize);
     696             : 
     697       29217 :         hl_decode_mb_predict_luma(s, mb_type, block_offset, linesize, dest_y);
     698             :     }
     699             : 
     700      261865 :     hl_decode_mb_idct_luma(s, mb_type, block_offset, linesize, dest_y);
     701             : 
     702      261865 :     if (s->cbp & 0x30) {
     703       84952 :         uint8_t *dest[2] = { dest_cb, dest_cr };
     704      169904 :         s->h264dsp.h264_chroma_dc_dequant_idct(s->mb + 16 * 16 * 1,
     705       84952 :                                                s->dequant4_coeff[4][0]);
     706      169904 :         s->h264dsp.h264_chroma_dc_dequant_idct(s->mb + 16 * 16 * 2,
     707       84952 :                                                s->dequant4_coeff[4][0]);
     708      254856 :         for (j = 1; j < 3; j++) {
     709      849520 :             for (i = j * 16; i < j * 16 + 4; i++)
     710      679616 :                 if (s->non_zero_count_cache[scan8[i]] || s->mb[i * 16]) {
     711      517849 :                     uint8_t *const ptr = dest[j - 1] + block_offset[i];
     712      517849 :                     svq3_add_idct_c(ptr, s->mb + i * 16,
     713      517849 :                                     uvlinesize, ff_h264_chroma_qp[0][s->qscale + 12] - 12, 2);
     714             :                 }
     715             :         }
     716             :     }
     717      261865 : }
     718             : 
     719      449700 : static int svq3_decode_mb(SVQ3Context *s, unsigned int mb_type)
     720             : {
     721             :     int i, j, k, m, dir, mode;
     722      449700 :     int cbp = 0;
     723             :     uint32_t vlc;
     724             :     int8_t *top, *left;
     725      449700 :     const int mb_xy = s->mb_xy;
     726      449700 :     const int b_xy  = 4 * s->mb_x + 4 * s->mb_y * s->b_stride;
     727             : 
     728      449700 :     s->top_samples_available      = (s->mb_y == 0) ? 0x33FF : 0xFFFF;
     729      449700 :     s->left_samples_available     = (s->mb_x == 0) ? 0x5F5F : 0xFFFF;
     730      449700 :     s->topright_samples_available = 0xFFFF;
     731             : 
     732      449700 :     if (mb_type == 0) {           /* SKIP */
     733      188769 :         if (s->pict_type == AV_PICTURE_TYPE_P ||
     734         874 :             s->next_pic->mb_type[mb_xy] == -1) {
     735      187502 :             svq3_mc_dir_part(s, 16 * s->mb_x, 16 * s->mb_y, 16, 16,
     736             :                              0, 0, 0, 0, 0, 0);
     737             : 
     738      187502 :             if (s->pict_type == AV_PICTURE_TYPE_B)
     739         481 :                 svq3_mc_dir_part(s, 16 * s->mb_x, 16 * s->mb_y, 16, 16,
     740             :                                  0, 0, 0, 0, 1, 1);
     741             : 
     742      187502 :             mb_type = MB_TYPE_SKIP;
     743             :         } else {
     744         393 :             mb_type = FFMIN(s->next_pic->mb_type[mb_xy], 6);
     745         393 :             if (svq3_mc_dir(s, mb_type, PREDICT_MODE, 0, 0) < 0)
     746           0 :                 return -1;
     747         393 :             if (svq3_mc_dir(s, mb_type, PREDICT_MODE, 1, 1) < 0)
     748           0 :                 return -1;
     749             : 
     750         393 :             mb_type = MB_TYPE_16x16;
     751             :         }
     752      261805 :     } else if (mb_type < 8) {     /* INTER */
     753      232588 :         if (s->thirdpel_flag && s->halfpel_flag == !get_bits1(&s->gb_slice))
     754         129 :             mode = THIRDPEL_MODE;
     755      464918 :         else if (s->halfpel_flag &&
     756      232459 :                  s->thirdpel_flag == !get_bits1(&s->gb_slice))
     757      105521 :             mode = HALFPEL_MODE;
     758             :         else
     759      126938 :             mode = FULLPEL_MODE;
     760             : 
     761             :         /* fill caches */
     762             :         /* note ref_cache should contain here:
     763             :          *  ????????
     764             :          *  ???11111
     765             :          *  N??11111
     766             :          *  N??11111
     767             :          *  N??11111
     768             :          */
     769             : 
     770      232956 :         for (m = 0; m < 2; m++) {
     771      456769 :             if (s->mb_x > 0 && s->intra4x4_pred_mode[s->mb2br_xy[mb_xy - 1] + 6] != -1) {
     772     1119985 :                 for (i = 0; i < 4; i++)
     773      895988 :                     AV_COPY32(s->mv_cache[m][scan8[0] - 1 + i * 8],
     774             :                               s->cur_pic->motion_val[m][b_xy - 1 + i * s->b_stride]);
     775             :             } else {
     776       43875 :                 for (i = 0; i < 4; i++)
     777       35100 :                     AV_ZERO32(s->mv_cache[m][scan8[0] - 1 + i * 8]);
     778             :             }
     779      232772 :             if (s->mb_y > 0) {
     780      220692 :                 memcpy(s->mv_cache[m][scan8[0] - 1 * 8],
     781      220692 :                        s->cur_pic->motion_val[m][b_xy - s->b_stride],
     782             :                        4 * 2 * sizeof(int16_t));
     783      220692 :                 memset(&s->ref_cache[m][scan8[0] - 1 * 8],
     784      220692 :                        (s->intra4x4_pred_mode[s->mb2br_xy[mb_xy - s->mb_stride]] == -1) ? PART_NOT_AVAILABLE : 1, 4);
     785             : 
     786      220692 :                 if (s->mb_x < s->mb_width - 1) {
     787      211020 :                     AV_COPY32(s->mv_cache[m][scan8[0] + 4 - 1 * 8],
     788             :                               s->cur_pic->motion_val[m][b_xy - s->b_stride + 4]);
     789      211020 :                     s->ref_cache[m][scan8[0] + 4 - 1 * 8] =
     790      422040 :                         (s->intra4x4_pred_mode[s->mb2br_xy[mb_xy - s->mb_stride + 1] + 6] == -1 ||
     791      211020 :                          s->intra4x4_pred_mode[s->mb2br_xy[mb_xy - s->mb_stride]] == -1) ? PART_NOT_AVAILABLE : 1;
     792             :                 } else
     793        9672 :                     s->ref_cache[m][scan8[0] + 4 - 1 * 8] = PART_NOT_AVAILABLE;
     794      220692 :                 if (s->mb_x > 0) {
     795      212399 :                     AV_COPY32(s->mv_cache[m][scan8[0] - 1 - 1 * 8],
     796             :                               s->cur_pic->motion_val[m][b_xy - s->b_stride - 1]);
     797      212399 :                     s->ref_cache[m][scan8[0] - 1 - 1 * 8] =
     798      212399 :                         (s->intra4x4_pred_mode[s->mb2br_xy[mb_xy - s->mb_stride - 1] + 3] == -1) ? PART_NOT_AVAILABLE : 1;
     799             :                 } else
     800        8293 :                     s->ref_cache[m][scan8[0] - 1 - 1 * 8] = PART_NOT_AVAILABLE;
     801             :             } else
     802       12080 :                 memset(&s->ref_cache[m][scan8[0] - 1 * 8 - 1],
     803             :                        PART_NOT_AVAILABLE, 8);
     804             : 
     805      232772 :             if (s->pict_type != AV_PICTURE_TYPE_B)
     806      232404 :                 break;
     807             :         }
     808             : 
     809             :         /* decode motion vector(s) and form prediction(s) */
     810      232588 :         if (s->pict_type == AV_PICTURE_TYPE_P) {
     811      232404 :             if (svq3_mc_dir(s, mb_type - 1, mode, 0, 0) < 0)
     812           0 :                 return -1;
     813             :         } else {        /* AV_PICTURE_TYPE_B */
     814         184 :             if (mb_type != 2) {
     815         134 :                 if (svq3_mc_dir(s, 0, mode, 0, 0) < 0)
     816           0 :                     return -1;
     817             :             } else {
     818         250 :                 for (i = 0; i < 4; i++)
     819         200 :                     memset(s->cur_pic->motion_val[0][b_xy + i * s->b_stride],
     820             :                            0, 4 * 2 * sizeof(int16_t));
     821             :             }
     822         184 :             if (mb_type != 1) {
     823         131 :                 if (svq3_mc_dir(s, 0, mode, 1, mb_type == 3) < 0)
     824           0 :                     return -1;
     825             :             } else {
     826         265 :                 for (i = 0; i < 4; i++)
     827         212 :                     memset(s->cur_pic->motion_val[1][b_xy + i * s->b_stride],
     828             :                            0, 4 * 2 * sizeof(int16_t));
     829             :             }
     830             :         }
     831             : 
     832      232588 :         mb_type = MB_TYPE_16x16;
     833       57037 :     } else if (mb_type == 8 || mb_type == 33) {   /* INTRA4x4 */
     834       27820 :         int8_t *i4x4       = s->intra4x4_pred_mode + s->mb2br_xy[s->mb_xy];
     835       27820 :         int8_t *i4x4_cache = s->intra4x4_pred_mode_cache;
     836             : 
     837       27820 :         memset(s->intra4x4_pred_mode_cache, -1, 8 * 5 * sizeof(int8_t));
     838             : 
     839       27820 :         if (mb_type == 8) {
     840       27820 :             if (s->mb_x > 0) {
     841      127635 :                 for (i = 0; i < 4; i++)
     842      102108 :                     s->intra4x4_pred_mode_cache[scan8[0] - 1 + i * 8] = s->intra4x4_pred_mode[s->mb2br_xy[mb_xy - 1] + 6 - i];
     843       25527 :                 if (s->intra4x4_pred_mode_cache[scan8[0] - 1] == -1)
     844           0 :                     s->left_samples_available = 0x5F5F;
     845             :             }
     846       27820 :             if (s->mb_y > 0) {
     847       26160 :                 s->intra4x4_pred_mode_cache[4 + 8 * 0] = s->intra4x4_pred_mode[s->mb2br_xy[mb_xy - s->mb_stride] + 0];
     848       26160 :                 s->intra4x4_pred_mode_cache[5 + 8 * 0] = s->intra4x4_pred_mode[s->mb2br_xy[mb_xy - s->mb_stride] + 1];
     849       26160 :                 s->intra4x4_pred_mode_cache[6 + 8 * 0] = s->intra4x4_pred_mode[s->mb2br_xy[mb_xy - s->mb_stride] + 2];
     850       26160 :                 s->intra4x4_pred_mode_cache[7 + 8 * 0] = s->intra4x4_pred_mode[s->mb2br_xy[mb_xy - s->mb_stride] + 3];
     851             : 
     852       26160 :                 if (s->intra4x4_pred_mode_cache[4 + 8 * 0] == -1)
     853           0 :                     s->top_samples_available = 0x33FF;
     854             :             }
     855             : 
     856             :             /* decode prediction codes for luma blocks */
     857      250380 :             for (i = 0; i < 16; i += 2) {
     858      222560 :                 vlc = get_interleaved_ue_golomb(&s->gb_slice);
     859             : 
     860      222560 :                 if (vlc >= 25U) {
     861           0 :                     av_log(s->avctx, AV_LOG_ERROR,
     862             :                            "luma prediction:%"PRIu32"\n", vlc);
     863           0 :                     return -1;
     864             :                 }
     865             : 
     866      222560 :                 left = &s->intra4x4_pred_mode_cache[scan8[i] - 1];
     867      222560 :                 top  = &s->intra4x4_pred_mode_cache[scan8[i] - 8];
     868             : 
     869      222560 :                 left[1] = svq3_pred_1[top[0] + 1][left[0] + 1][svq3_pred_0[vlc][0]];
     870      222560 :                 left[2] = svq3_pred_1[top[1] + 1][left[1] + 1][svq3_pred_0[vlc][1]];
     871             : 
     872      222560 :                 if (left[1] == -1 || left[2] == -1) {
     873           0 :                     av_log(s->avctx, AV_LOG_ERROR, "weird prediction\n");
     874           0 :                     return -1;
     875             :                 }
     876             :             }
     877             :         } else {    /* mb_type == 33, DC_128_PRED block type */
     878           0 :             for (i = 0; i < 4; i++)
     879           0 :                 memset(&s->intra4x4_pred_mode_cache[scan8[0] + 8 * i], DC_PRED, 4);
     880             :         }
     881             : 
     882       27820 :         AV_COPY32(i4x4, i4x4_cache + 4 + 8 * 4);
     883       27820 :         i4x4[4] = i4x4_cache[7 + 8 * 3];
     884       27820 :         i4x4[5] = i4x4_cache[7 + 8 * 2];
     885       27820 :         i4x4[6] = i4x4_cache[7 + 8 * 1];
     886             : 
     887       27820 :         if (mb_type == 8) {
     888       83460 :             ff_h264_check_intra4x4_pred_mode(s->intra4x4_pred_mode_cache,
     889       55640 :                                              s->avctx, s->top_samples_available,
     890       27820 :                                              s->left_samples_available);
     891             : 
     892       27820 :             s->top_samples_available  = (s->mb_y == 0) ? 0x33FF : 0xFFFF;
     893       27820 :             s->left_samples_available = (s->mb_x == 0) ? 0x5F5F : 0xFFFF;
     894             :         } else {
     895           0 :             for (i = 0; i < 4; i++)
     896           0 :                 memset(&s->intra4x4_pred_mode_cache[scan8[0] + 8 * i], DC_128_PRED, 4);
     897             : 
     898           0 :             s->top_samples_available  = 0x33FF;
     899           0 :             s->left_samples_available = 0x5F5F;
     900             :         }
     901             : 
     902       27820 :         mb_type = MB_TYPE_INTRA4x4;
     903             :     } else {                      /* INTRA16x16 */
     904        1397 :         dir = ff_h264_i_mb_type_info[mb_type - 8].pred_mode;
     905        1397 :         dir = (dir >> 1) ^ 3 * (dir & 1) ^ 1;
     906             : 
     907        1397 :         if ((s->intra16x16_pred_mode = ff_h264_check_intra_pred_mode(s->avctx, s->top_samples_available,
     908        1397 :                                                                      s->left_samples_available, dir, 0)) < 0) {
     909           0 :             av_log(s->avctx, AV_LOG_ERROR, "ff_h264_check_intra_pred_mode < 0\n");
     910           0 :             return s->intra16x16_pred_mode;
     911             :         }
     912             : 
     913        1397 :         cbp     = ff_h264_i_mb_type_info[mb_type - 8].cbp;
     914        1397 :         mb_type = MB_TYPE_INTRA16x16;
     915             :     }
     916             : 
     917      449700 :     if (!IS_INTER(mb_type) && s->pict_type != AV_PICTURE_TYPE_I) {
     918     1029745 :         for (i = 0; i < 4; i++)
     919      823796 :             memset(s->cur_pic->motion_val[0][b_xy + i * s->b_stride],
     920             :                    0, 4 * 2 * sizeof(int16_t));
     921      205949 :         if (s->pict_type == AV_PICTURE_TYPE_B) {
     922        2515 :             for (i = 0; i < 4; i++)
     923        2012 :                 memset(s->cur_pic->motion_val[1][b_xy + i * s->b_stride],
     924             :                        0, 4 * 2 * sizeof(int16_t));
     925             :         }
     926             :     }
     927      449700 :     if (!IS_INTRA4x4(mb_type)) {
     928      421880 :         memset(s->intra4x4_pred_mode + s->mb2br_xy[mb_xy], DC_PRED, 8);
     929             :     }
     930      449700 :     if (!IS_SKIP(mb_type) || s->pict_type == AV_PICTURE_TYPE_B) {
     931      262679 :         memset(s->non_zero_count_cache + 8, 0, 14 * 8 * sizeof(uint8_t));
     932             :     }
     933             : 
     934      898003 :     if (!IS_INTRA16x16(mb_type) &&
     935      635805 :         (!IS_SKIP(mb_type) || s->pict_type == AV_PICTURE_TYPE_B)) {
     936      261282 :         if ((vlc = get_interleaved_ue_golomb(&s->gb_slice)) >= 48U){
     937           0 :             av_log(s->avctx, AV_LOG_ERROR, "cbp_vlc=%"PRIu32"\n", vlc);
     938           0 :             return -1;
     939             :         }
     940             : 
     941      550384 :         cbp = IS_INTRA(mb_type) ? ff_h264_golomb_to_intra4x4_cbp[vlc]
     942      289102 :                                 : ff_h264_golomb_to_inter_cbp[vlc];
     943             :     }
     944      898003 :     if (IS_INTRA16x16(mb_type) ||
     945      887044 :         (s->pict_type != AV_PICTURE_TYPE_I && s->adaptive_quant && cbp)) {
     946        2093 :         s->qscale += get_interleaved_se_golomb(&s->gb_slice);
     947             : 
     948        2093 :         if (s->qscale > 31u) {
     949           0 :             av_log(s->avctx, AV_LOG_ERROR, "qscale:%d\n", s->qscale);
     950           0 :             return -1;
     951             :         }
     952             :     }
     953      449700 :     if (IS_INTRA16x16(mb_type)) {
     954        1397 :         AV_ZERO128(s->mb_luma_dc[0] + 0);
     955        1397 :         AV_ZERO128(s->mb_luma_dc[0] + 8);
     956        1397 :         if (svq3_decode_block(&s->gb_slice, s->mb_luma_dc[0], 0, 1)) {
     957           0 :             av_log(s->avctx, AV_LOG_ERROR,
     958             :                    "error while decoding intra luma dc\n");
     959           0 :             return -1;
     960             :         }
     961             :     }
     962             : 
     963      449700 :     if (cbp) {
     964      180177 :         const int index = IS_INTRA16x16(mb_type) ? 1 : 0;
     965      180177 :         const int type  = ((s->qscale < 24 && IS_INTRA4x4(mb_type)) ? 2 : 1);
     966             : 
     967      900885 :         for (i = 0; i < 4; i++)
     968      720708 :             if ((cbp & (1 << i))) {
     969     2079190 :                 for (j = 0; j < 4; j++) {
     970     1667704 :                     k = index ? (1 * (j & 1) + 2 * (i & 1) +
     971        4352 :                                  2 * (j & 2) + 4 * (i & 2))
     972     1665528 :                               : (4 * i + j);
     973     1663352 :                     s->non_zero_count_cache[scan8[k]] = 1;
     974             : 
     975     1663352 :                     if (svq3_decode_block(&s->gb_slice, &s->mb[16 * k], index, type)) {
     976           0 :                         av_log(s->avctx, AV_LOG_ERROR,
     977             :                                "error while decoding block\n");
     978           0 :                         return -1;
     979             :                     }
     980             :                 }
     981             :             }
     982             : 
     983      180177 :         if ((cbp & 0x30)) {
     984      254856 :             for (i = 1; i < 3; ++i)
     985      169904 :                 if (svq3_decode_block(&s->gb_slice, &s->mb[16 * 16 * i], 0, 3)) {
     986           0 :                     av_log(s->avctx, AV_LOG_ERROR,
     987             :                            "error while decoding chroma dc block\n");
     988           0 :                     return -1;
     989             :                 }
     990             : 
     991       84952 :             if ((cbp & 0x20)) {
     992      107469 :                 for (i = 1; i < 3; i++) {
     993      358230 :                     for (j = 0; j < 4; j++) {
     994      286584 :                         k                                 = 16 * i + j;
     995      286584 :                         s->non_zero_count_cache[scan8[k]] = 1;
     996             : 
     997      286584 :                         if (svq3_decode_block(&s->gb_slice, &s->mb[16 * k], 1, 1)) {
     998           0 :                             av_log(s->avctx, AV_LOG_ERROR,
     999             :                                    "error while decoding chroma ac block\n");
    1000           0 :                             return -1;
    1001             :                         }
    1002             :                     }
    1003             :                 }
    1004             :             }
    1005             :         }
    1006             :     }
    1007             : 
    1008      449700 :     s->cbp                     = cbp;
    1009      449700 :     s->cur_pic->mb_type[mb_xy] = mb_type;
    1010             : 
    1011      449700 :     if (IS_INTRA(mb_type))
    1012       29217 :         s->chroma_pred_mode = ff_h264_check_intra_pred_mode(s->avctx, s->top_samples_available,
    1013       29217 :                                                             s->left_samples_available, DC_PRED8x8, 1);
    1014             : 
    1015      449700 :     return 0;
    1016             : }
    1017             : 
    1018        1500 : static int svq3_decode_slice_header(AVCodecContext *avctx)
    1019             : {
    1020        1500 :     SVQ3Context *s = avctx->priv_data;
    1021        1500 :     const int mb_xy   = s->mb_xy;
    1022             :     int i, header;
    1023             :     unsigned slice_id;
    1024             : 
    1025        1500 :     header = get_bits(&s->gb, 8);
    1026             : 
    1027        1500 :     if (((header & 0x9F) != 1 && (header & 0x9F) != 2) || (header & 0x60) == 0) {
    1028             :         /* TODO: what? */
    1029           0 :         av_log(avctx, AV_LOG_ERROR, "unsupported slice header (%02X)\n", header);
    1030           0 :         return -1;
    1031             :     } else {
    1032             :         int slice_bits, slice_bytes, slice_length;
    1033        1500 :         int length = header >> 5 & 3;
    1034             : 
    1035        1500 :         slice_length = show_bits(&s->gb, 8 * length);
    1036        1500 :         slice_bits   = slice_length * 8;
    1037        1500 :         slice_bytes  = slice_length + length - 1;
    1038             : 
    1039        1500 :         skip_bits(&s->gb, 8);
    1040             : 
    1041        1500 :         av_fast_malloc(&s->slice_buf, &s->slice_size, slice_bytes + AV_INPUT_BUFFER_PADDING_SIZE);
    1042        1500 :         if (!s->slice_buf)
    1043           0 :             return AVERROR(ENOMEM);
    1044             : 
    1045        1500 :         if (slice_bytes * 8LL > get_bits_left(&s->gb)) {
    1046           0 :             av_log(avctx, AV_LOG_ERROR, "slice after bitstream end\n");
    1047           0 :             return AVERROR_INVALIDDATA;
    1048             :         }
    1049        1500 :         memcpy(s->slice_buf, s->gb.buffer + s->gb.index / 8, slice_bytes);
    1050             : 
    1051        1500 :         init_get_bits(&s->gb_slice, s->slice_buf, slice_bits);
    1052             : 
    1053        1500 :         if (s->watermark_key) {
    1054          10 :             uint32_t header = AV_RL32(&s->gb_slice.buffer[1]);
    1055          10 :             AV_WL32(&s->gb_slice.buffer[1], header ^ s->watermark_key);
    1056             :         }
    1057        1500 :         if (length > 0) {
    1058        1500 :             memmove(s->slice_buf, &s->slice_buf[slice_length], length - 1);
    1059             :         }
    1060        1500 :         skip_bits_long(&s->gb, slice_bytes * 8);
    1061             :     }
    1062             : 
    1063        1500 :     if ((slice_id = get_interleaved_ue_golomb(&s->gb_slice)) >= 3) {
    1064           0 :         av_log(s->avctx, AV_LOG_ERROR, "illegal slice type %u \n", slice_id);
    1065           0 :         return -1;
    1066             :     }
    1067        1500 :     if (get_bits1(&s->gb_slice)) {
    1068           0 :         avpriv_report_missing_feature(s->avctx, "Media key encryption");
    1069           0 :         return AVERROR_PATCHWELCOME;
    1070             :     }
    1071             : 
    1072        1500 :     s->slice_type = ff_h264_golomb_to_pict_type[slice_id];
    1073             : 
    1074        1500 :     if ((header & 0x9F) == 2) {
    1075           0 :         i = (s->mb_num < 64) ? 5 : av_log2(s->mb_num - 1);
    1076           0 :         get_bits(&s->gb_slice, i);
    1077             :     }
    1078             : 
    1079        1500 :     s->slice_num      = get_bits(&s->gb_slice, 8);
    1080        1500 :     s->qscale         = get_bits(&s->gb_slice, 5);
    1081        1500 :     s->adaptive_quant = get_bits1(&s->gb_slice);
    1082             : 
    1083             :     /* unknown fields */
    1084        1500 :     skip_bits1(&s->gb_slice);
    1085             : 
    1086        1500 :     if (s->has_watermark)
    1087          10 :         skip_bits1(&s->gb_slice);
    1088             : 
    1089        1500 :     skip_bits1(&s->gb_slice);
    1090        1500 :     skip_bits(&s->gb_slice, 2);
    1091             : 
    1092        1500 :     if (skip_1stop_8data_bits(&s->gb_slice) < 0)
    1093           0 :         return AVERROR_INVALIDDATA;
    1094             : 
    1095             :     /* reset intra predictors and invalidate motion vector references */
    1096        1500 :     if (s->mb_x > 0) {
    1097           0 :         memset(s->intra4x4_pred_mode + s->mb2br_xy[mb_xy - 1] + 3,
    1098             :                -1, 4 * sizeof(int8_t));
    1099           0 :         memset(s->intra4x4_pred_mode + s->mb2br_xy[mb_xy - s->mb_x],
    1100           0 :                -1, 8 * sizeof(int8_t) * s->mb_x);
    1101             :     }
    1102        1500 :     if (s->mb_y > 0) {
    1103           0 :         memset(s->intra4x4_pred_mode + s->mb2br_xy[mb_xy - s->mb_stride],
    1104           0 :                -1, 8 * sizeof(int8_t) * (s->mb_width - s->mb_x));
    1105             : 
    1106           0 :         if (s->mb_x > 0)
    1107           0 :             s->intra4x4_pred_mode[s->mb2br_xy[mb_xy - s->mb_stride - 1] + 3] = -1;
    1108             :     }
    1109             : 
    1110        1500 :     return 0;
    1111             : }
    1112             : 
    1113           6 : static void init_dequant4_coeff_table(SVQ3Context *s)
    1114             : {
    1115             :     int q, x;
    1116           6 :     const int max_qp = 51;
    1117             : 
    1118         318 :     for (q = 0; q < max_qp + 1; q++) {
    1119         312 :         int shift = ff_h264_quant_div6[q] + 2;
    1120         312 :         int idx   = ff_h264_quant_rem6[q];
    1121        5304 :         for (x = 0; x < 16; x++)
    1122        9984 :             s->dequant4_coeff[q][(x >> 2) | ((x << 2) & 0xF)] =
    1123        9984 :                 ((uint32_t)ff_h264_dequant4_coeff_init[idx][(x & 1) + ((x >> 2) & 1)] * 16) << shift;
    1124             :     }
    1125           6 : }
    1126             : 
    1127           6 : static av_cold int svq3_decode_init(AVCodecContext *avctx)
    1128             : {
    1129           6 :     SVQ3Context *s = avctx->priv_data;
    1130             :     int m, x, y;
    1131             :     unsigned char *extradata;
    1132             :     unsigned char *extradata_end;
    1133             :     unsigned int size;
    1134           6 :     int marker_found = 0;
    1135             :     int ret;
    1136             : 
    1137           6 :     s->cur_pic  = av_mallocz(sizeof(*s->cur_pic));
    1138           6 :     s->last_pic = av_mallocz(sizeof(*s->last_pic));
    1139           6 :     s->next_pic = av_mallocz(sizeof(*s->next_pic));
    1140           6 :     if (!s->next_pic || !s->last_pic || !s->cur_pic) {
    1141           0 :         ret = AVERROR(ENOMEM);
    1142           0 :         goto fail;
    1143             :     }
    1144             : 
    1145           6 :     s->cur_pic->f  = av_frame_alloc();
    1146           6 :     s->last_pic->f = av_frame_alloc();
    1147           6 :     s->next_pic->f = av_frame_alloc();
    1148           6 :     if (!s->cur_pic->f || !s->last_pic->f || !s->next_pic->f)
    1149           0 :         return AVERROR(ENOMEM);
    1150             : 
    1151           6 :     ff_h264dsp_init(&s->h264dsp, 8, 1);
    1152           6 :     ff_h264_pred_init(&s->hpc, AV_CODEC_ID_SVQ3, 8, 1);
    1153           6 :     ff_videodsp_init(&s->vdsp, 8);
    1154             : 
    1155             : 
    1156           6 :     avctx->bits_per_raw_sample = 8;
    1157             : 
    1158           6 :     ff_hpeldsp_init(&s->hdsp, avctx->flags);
    1159           6 :     ff_tpeldsp_init(&s->tdsp);
    1160             : 
    1161           6 :     avctx->pix_fmt     = AV_PIX_FMT_YUVJ420P;
    1162           6 :     avctx->color_range = AVCOL_RANGE_JPEG;
    1163             : 
    1164           6 :     s->avctx         = avctx;
    1165           6 :     s->halfpel_flag  = 1;
    1166           6 :     s->thirdpel_flag = 1;
    1167           6 :     s->has_watermark = 0;
    1168             : 
    1169             :     /* prowl for the "SEQH" marker in the extradata */
    1170           6 :     extradata     = (unsigned char *)avctx->extradata;
    1171           6 :     extradata_end = avctx->extradata + avctx->extradata_size;
    1172           6 :     if (extradata) {
    1173          54 :         for (m = 0; m + 8 < avctx->extradata_size; m++) {
    1174          54 :             if (!memcmp(extradata, "SEQH", 4)) {
    1175           6 :                 marker_found = 1;
    1176           6 :                 break;
    1177             :             }
    1178          48 :             extradata++;
    1179             :         }
    1180             :     }
    1181             : 
    1182             :     /* if a match was found, parse the extra data */
    1183           6 :     if (marker_found) {
    1184             :         GetBitContext gb;
    1185             :         int frame_size_code;
    1186             :         int unk0, unk1, unk2, unk3, unk4;
    1187             : 
    1188           6 :         size = AV_RB32(&extradata[4]);
    1189           6 :         if (size > extradata_end - extradata - 8) {
    1190           0 :             ret = AVERROR_INVALIDDATA;
    1191           0 :             goto fail;
    1192             :         }
    1193           6 :         init_get_bits(&gb, extradata + 8, size * 8);
    1194             : 
    1195             :         /* 'frame size code' and optional 'width, height' */
    1196           6 :         frame_size_code = get_bits(&gb, 3);
    1197           6 :         switch (frame_size_code) {
    1198           0 :         case 0:
    1199           0 :             avctx->width  = 160;
    1200           0 :             avctx->height = 120;
    1201           0 :             break;
    1202           0 :         case 1:
    1203           0 :             avctx->width  = 128;
    1204           0 :             avctx->height =  96;
    1205           0 :             break;
    1206           0 :         case 2:
    1207           0 :             avctx->width  = 176;
    1208           0 :             avctx->height = 144;
    1209           0 :             break;
    1210           0 :         case 3:
    1211           0 :             avctx->width  = 352;
    1212           0 :             avctx->height = 288;
    1213           0 :             break;
    1214           0 :         case 4:
    1215           0 :             avctx->width  = 704;
    1216           0 :             avctx->height = 576;
    1217           0 :             break;
    1218           0 :         case 5:
    1219           0 :             avctx->width  = 240;
    1220           0 :             avctx->height = 180;
    1221           0 :             break;
    1222           4 :         case 6:
    1223           4 :             avctx->width  = 320;
    1224           4 :             avctx->height = 240;
    1225           4 :             break;
    1226           2 :         case 7:
    1227           2 :             avctx->width  = get_bits(&gb, 12);
    1228           2 :             avctx->height = get_bits(&gb, 12);
    1229           2 :             break;
    1230             :         }
    1231             : 
    1232           6 :         s->halfpel_flag  = get_bits1(&gb);
    1233           6 :         s->thirdpel_flag = get_bits1(&gb);
    1234             : 
    1235             :         /* unknown fields */
    1236           6 :         unk0 = get_bits1(&gb);
    1237           6 :         unk1 = get_bits1(&gb);
    1238           6 :         unk2 = get_bits1(&gb);
    1239           6 :         unk3 = get_bits1(&gb);
    1240             : 
    1241           6 :         s->low_delay = get_bits1(&gb);
    1242             : 
    1243             :         /* unknown field */
    1244           6 :         unk4 = get_bits1(&gb);
    1245             : 
    1246           6 :         av_log(avctx, AV_LOG_DEBUG, "Unknown fields %d %d %d %d %d\n",
    1247             :                unk0, unk1, unk2, unk3, unk4);
    1248             : 
    1249           6 :         if (skip_1stop_8data_bits(&gb) < 0) {
    1250           0 :             ret = AVERROR_INVALIDDATA;
    1251           0 :             goto fail;
    1252             :         }
    1253             : 
    1254           6 :         s->has_watermark  = get_bits1(&gb);
    1255           6 :         avctx->has_b_frames = !s->low_delay;
    1256           6 :         if (s->has_watermark) {
    1257             : #if CONFIG_ZLIB
    1258           2 :             unsigned watermark_width  = get_interleaved_ue_golomb(&gb);
    1259           2 :             unsigned watermark_height = get_interleaved_ue_golomb(&gb);
    1260           2 :             int u1                    = get_interleaved_ue_golomb(&gb);
    1261           2 :             int u2                    = get_bits(&gb, 8);
    1262           2 :             int u3                    = get_bits(&gb, 2);
    1263           2 :             int u4                    = get_interleaved_ue_golomb(&gb);
    1264           4 :             unsigned long buf_len     = watermark_width *
    1265           2 :                                         watermark_height * 4;
    1266           2 :             int offset                = get_bits_count(&gb) + 7 >> 3;
    1267             :             uint8_t *buf;
    1268             : 
    1269           4 :             if (watermark_height <= 0 ||
    1270           2 :                 (uint64_t)watermark_width * 4 > UINT_MAX / watermark_height) {
    1271           0 :                 ret = -1;
    1272           0 :                 goto fail;
    1273             :             }
    1274             : 
    1275           2 :             buf = av_malloc(buf_len);
    1276           2 :             if (!buf) {
    1277           0 :                 ret = AVERROR(ENOMEM);
    1278           0 :                 goto fail;
    1279             :             }
    1280           2 :             av_log(avctx, AV_LOG_DEBUG, "watermark size: %ux%u\n",
    1281             :                    watermark_width, watermark_height);
    1282           2 :             av_log(avctx, AV_LOG_DEBUG,
    1283             :                    "u1: %x u2: %x u3: %x compressed data size: %d offset: %d\n",
    1284             :                    u1, u2, u3, u4, offset);
    1285           2 :             if (uncompress(buf, &buf_len, extradata + 8 + offset,
    1286           2 :                            size - offset) != Z_OK) {
    1287           0 :                 av_log(avctx, AV_LOG_ERROR,
    1288             :                        "could not uncompress watermark logo\n");
    1289           0 :                 av_free(buf);
    1290           0 :                 ret = -1;
    1291           0 :                 goto fail;
    1292             :             }
    1293           2 :             s->watermark_key = ff_svq1_packet_checksum(buf, buf_len, 0);
    1294           2 :             s->watermark_key = s->watermark_key << 16 | s->watermark_key;
    1295           2 :             av_log(avctx, AV_LOG_DEBUG,
    1296             :                    "watermark key %#"PRIx32"\n", s->watermark_key);
    1297           2 :             av_free(buf);
    1298             : #else
    1299             :             av_log(avctx, AV_LOG_ERROR,
    1300             :                    "this svq3 file contains watermark which need zlib support compiled in\n");
    1301             :             ret = -1;
    1302             :             goto fail;
    1303             : #endif
    1304             :         }
    1305             :     }
    1306             : 
    1307           6 :     s->mb_width   = (avctx->width + 15) / 16;
    1308           6 :     s->mb_height  = (avctx->height + 15) / 16;
    1309           6 :     s->mb_stride  = s->mb_width + 1;
    1310           6 :     s->mb_num     = s->mb_width * s->mb_height;
    1311           6 :     s->b_stride   = 4 * s->mb_width;
    1312           6 :     s->h_edge_pos = s->mb_width * 16;
    1313           6 :     s->v_edge_pos = s->mb_height * 16;
    1314             : 
    1315           6 :     s->intra4x4_pred_mode = av_mallocz(s->mb_stride * 2 * 8);
    1316           6 :     if (!s->intra4x4_pred_mode)
    1317           0 :         return AVERROR(ENOMEM);
    1318             : 
    1319           6 :     s->mb2br_xy = av_mallocz(s->mb_stride * (s->mb_height + 1) *
    1320             :                              sizeof(*s->mb2br_xy));
    1321           6 :     if (!s->mb2br_xy)
    1322           0 :         return AVERROR(ENOMEM);
    1323             : 
    1324          96 :     for (y = 0; y < s->mb_height; y++)
    1325        1830 :         for (x = 0; x < s->mb_width; x++) {
    1326        1740 :             const int mb_xy = x + y * s->mb_stride;
    1327             : 
    1328        1740 :             s->mb2br_xy[mb_xy] = 8 * (mb_xy % (2 * s->mb_stride));
    1329             :         }
    1330             : 
    1331           6 :     init_dequant4_coeff_table(s);
    1332             : 
    1333           6 :     return 0;
    1334           0 : fail:
    1335           0 :     svq3_decode_end(avctx);
    1336           0 :     return ret;
    1337             : }
    1338             : 
    1339          18 : static void free_picture(AVCodecContext *avctx, SVQ3Frame *pic)
    1340             : {
    1341             :     int i;
    1342          54 :     for (i = 0; i < 2; i++) {
    1343          36 :         av_buffer_unref(&pic->motion_val_buf[i]);
    1344          36 :         av_buffer_unref(&pic->ref_index_buf[i]);
    1345             :     }
    1346          18 :     av_buffer_unref(&pic->mb_type_buf);
    1347             : 
    1348          18 :     av_frame_unref(pic->f);
    1349          18 : }
    1350             : 
    1351        1500 : static int get_buffer(AVCodecContext *avctx, SVQ3Frame *pic)
    1352             : {
    1353        1500 :     SVQ3Context *s = avctx->priv_data;
    1354        1500 :     const int big_mb_num    = s->mb_stride * (s->mb_height + 1) + 1;
    1355        1500 :     const int mb_array_size = s->mb_stride * s->mb_height;
    1356        1500 :     const int b4_stride     = s->mb_width * 4 + 1;
    1357        1500 :     const int b4_array_size = b4_stride * s->mb_height * 4;
    1358             :     int ret;
    1359             : 
    1360        1500 :     if (!pic->motion_val_buf[0]) {
    1361             :         int i;
    1362             : 
    1363           9 :         pic->mb_type_buf = av_buffer_allocz((big_mb_num + s->mb_stride) * sizeof(uint32_t));
    1364           9 :         if (!pic->mb_type_buf)
    1365           0 :             return AVERROR(ENOMEM);
    1366           9 :         pic->mb_type = (uint32_t*)pic->mb_type_buf->data + 2 * s->mb_stride + 1;
    1367             : 
    1368          27 :         for (i = 0; i < 2; i++) {
    1369          18 :             pic->motion_val_buf[i] = av_buffer_allocz(2 * (b4_array_size + 4) * sizeof(int16_t));
    1370          18 :             pic->ref_index_buf[i]  = av_buffer_allocz(4 * mb_array_size);
    1371          18 :             if (!pic->motion_val_buf[i] || !pic->ref_index_buf[i]) {
    1372           0 :                 ret = AVERROR(ENOMEM);
    1373           0 :                 goto fail;
    1374             :             }
    1375             : 
    1376          18 :             pic->motion_val[i] = (int16_t (*)[2])pic->motion_val_buf[i]->data + 4;
    1377          18 :             pic->ref_index[i]  = pic->ref_index_buf[i]->data;
    1378             :         }
    1379             :     }
    1380             : 
    1381        1500 :     ret = ff_get_buffer(avctx, pic->f,
    1382        1500 :                         (s->pict_type != AV_PICTURE_TYPE_B) ?
    1383             :                          AV_GET_BUFFER_FLAG_REF : 0);
    1384        1500 :     if (ret < 0)
    1385           0 :         goto fail;
    1386             : 
    1387        1500 :     if (!s->edge_emu_buffer) {
    1388           3 :         s->edge_emu_buffer = av_mallocz_array(pic->f->linesize[0], 17);
    1389           3 :         if (!s->edge_emu_buffer)
    1390           0 :             return AVERROR(ENOMEM);
    1391             :     }
    1392             : 
    1393        1500 :     return 0;
    1394           0 : fail:
    1395           0 :     free_picture(avctx, pic);
    1396           0 :     return ret;
    1397             : }
    1398             : 
    1399        1505 : static int svq3_decode_frame(AVCodecContext *avctx, void *data,
    1400             :                              int *got_frame, AVPacket *avpkt)
    1401             : {
    1402        1505 :     SVQ3Context *s     = avctx->priv_data;
    1403        1505 :     int buf_size       = avpkt->size;
    1404             :     int left;
    1405             :     uint8_t *buf;
    1406             :     int ret, m, i;
    1407             : 
    1408             :     /* special case for last picture */
    1409        1505 :     if (buf_size == 0) {
    1410           5 :         if (s->next_pic->f->data[0] && !s->low_delay && !s->last_frame_output) {
    1411           3 :             ret = av_frame_ref(data, s->next_pic->f);
    1412           3 :             if (ret < 0)
    1413           0 :                 return ret;
    1414           3 :             s->last_frame_output = 1;
    1415           3 :             *got_frame          = 1;
    1416             :         }
    1417           5 :         return 0;
    1418             :     }
    1419             : 
    1420        1500 :     s->mb_x = s->mb_y = s->mb_xy = 0;
    1421             : 
    1422        1500 :     if (s->watermark_key) {
    1423          10 :         av_fast_padded_malloc(&s->buf, &s->buf_size, buf_size);
    1424          10 :         if (!s->buf)
    1425           0 :             return AVERROR(ENOMEM);
    1426          10 :         memcpy(s->buf, avpkt->data, buf_size);
    1427          10 :         buf = s->buf;
    1428             :     } else {
    1429        1490 :         buf = avpkt->data;
    1430             :     }
    1431             : 
    1432        1500 :     ret = init_get_bits(&s->gb, buf, 8 * buf_size);
    1433        1500 :     if (ret < 0)
    1434           0 :         return ret;
    1435             : 
    1436        1500 :     if (svq3_decode_slice_header(avctx))
    1437           0 :         return -1;
    1438             : 
    1439        1500 :     s->pict_type = s->slice_type;
    1440             : 
    1441        1500 :     if (s->pict_type != AV_PICTURE_TYPE_B)
    1442        1496 :         FFSWAP(SVQ3Frame*, s->next_pic, s->last_pic);
    1443             : 
    1444        1500 :     av_frame_unref(s->cur_pic->f);
    1445             : 
    1446             :     /* for skipping the frame */
    1447        1500 :     s->cur_pic->f->pict_type = s->pict_type;
    1448        1500 :     s->cur_pic->f->key_frame = (s->pict_type == AV_PICTURE_TYPE_I);
    1449             : 
    1450        1500 :     ret = get_buffer(avctx, s->cur_pic);
    1451        1500 :     if (ret < 0)
    1452           0 :         return ret;
    1453             : 
    1454       25500 :     for (i = 0; i < 16; i++) {
    1455       24000 :         s->block_offset[i]           = (4 * ((scan8[i] - scan8[0]) & 7)) + 4 * s->cur_pic->f->linesize[0] * ((scan8[i] - scan8[0]) >> 3);
    1456       24000 :         s->block_offset[48 + i]      = (4 * ((scan8[i] - scan8[0]) & 7)) + 8 * s->cur_pic->f->linesize[0] * ((scan8[i] - scan8[0]) >> 3);
    1457             :     }
    1458       25500 :     for (i = 0; i < 16; i++) {
    1459       48000 :         s->block_offset[16 + i]      =
    1460       48000 :         s->block_offset[32 + i]      = (4 * ((scan8[i] - scan8[0]) & 7)) + 4 * s->cur_pic->f->linesize[1] * ((scan8[i] - scan8[0]) >> 3);
    1461       48000 :         s->block_offset[48 + 16 + i] =
    1462       48000 :         s->block_offset[48 + 32 + i] = (4 * ((scan8[i] - scan8[0]) & 7)) + 8 * s->cur_pic->f->linesize[1] * ((scan8[i] - scan8[0]) >> 3);
    1463             :     }
    1464             : 
    1465        1500 :     if (s->pict_type != AV_PICTURE_TYPE_I) {
    1466        1464 :         if (!s->last_pic->f->data[0]) {
    1467           0 :             av_log(avctx, AV_LOG_ERROR, "Missing reference frame.\n");
    1468           0 :             av_frame_unref(s->last_pic->f);
    1469           0 :             ret = get_buffer(avctx, s->last_pic);
    1470           0 :             if (ret < 0)
    1471           0 :                 return ret;
    1472           0 :             memset(s->last_pic->f->data[0], 0, avctx->height * s->last_pic->f->linesize[0]);
    1473           0 :             memset(s->last_pic->f->data[1], 0x80, (avctx->height / 2) *
    1474           0 :                    s->last_pic->f->linesize[1]);
    1475           0 :             memset(s->last_pic->f->data[2], 0x80, (avctx->height / 2) *
    1476           0 :                    s->last_pic->f->linesize[2]);
    1477             :         }
    1478             : 
    1479        1464 :         if (s->pict_type == AV_PICTURE_TYPE_B && !s->next_pic->f->data[0]) {
    1480           0 :             av_log(avctx, AV_LOG_ERROR, "Missing reference frame.\n");
    1481           0 :             av_frame_unref(s->next_pic->f);
    1482           0 :             ret = get_buffer(avctx, s->next_pic);
    1483           0 :             if (ret < 0)
    1484           0 :                 return ret;
    1485           0 :             memset(s->next_pic->f->data[0], 0, avctx->height * s->next_pic->f->linesize[0]);
    1486           0 :             memset(s->next_pic->f->data[1], 0x80, (avctx->height / 2) *
    1487           0 :                    s->next_pic->f->linesize[1]);
    1488           0 :             memset(s->next_pic->f->data[2], 0x80, (avctx->height / 2) *
    1489           0 :                    s->next_pic->f->linesize[2]);
    1490             :         }
    1491             :     }
    1492             : 
    1493        1500 :     if (avctx->debug & FF_DEBUG_PICT_INFO)
    1494           0 :         av_log(s->avctx, AV_LOG_DEBUG,
    1495             :                "%c hpel:%d, tpel:%d aqp:%d qp:%d, slice_num:%02X\n",
    1496           0 :                av_get_picture_type_char(s->pict_type),
    1497             :                s->halfpel_flag, s->thirdpel_flag,
    1498             :                s->adaptive_quant, s->qscale, s->slice_num);
    1499             : 
    1500        3000 :     if (avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B ||
    1501        3000 :         avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I ||
    1502        1500 :         avctx->skip_frame >= AVDISCARD_ALL)
    1503           0 :         return 0;
    1504             : 
    1505        1500 :     if (s->next_p_frame_damaged) {
    1506           0 :         if (s->pict_type == AV_PICTURE_TYPE_B)
    1507           0 :             return 0;
    1508             :         else
    1509           0 :             s->next_p_frame_damaged = 0;
    1510             :     }
    1511             : 
    1512        1500 :     if (s->pict_type == AV_PICTURE_TYPE_B) {
    1513           4 :         s->frame_num_offset = s->slice_num - s->prev_frame_num;
    1514             : 
    1515           4 :         if (s->frame_num_offset < 0)
    1516           0 :             s->frame_num_offset += 256;
    1517           8 :         if (s->frame_num_offset == 0 ||
    1518           4 :             s->frame_num_offset >= s->prev_frame_num_offset) {
    1519           0 :             av_log(s->avctx, AV_LOG_ERROR, "error in B-frame picture id\n");
    1520           0 :             return -1;
    1521             :         }
    1522             :     } else {
    1523        1496 :         s->prev_frame_num        = s->frame_num;
    1524        1496 :         s->frame_num             = s->slice_num;
    1525        1496 :         s->prev_frame_num_offset = s->frame_num - s->prev_frame_num;
    1526             : 
    1527        1496 :         if (s->prev_frame_num_offset < 0)
    1528           5 :             s->prev_frame_num_offset += 256;
    1529             :     }
    1530             : 
    1531        4500 :     for (m = 0; m < 2; m++) {
    1532             :         int i;
    1533       15000 :         for (i = 0; i < 4; i++) {
    1534             :             int j;
    1535       72000 :             for (j = -1; j < 4; j++)
    1536       60000 :                 s->ref_cache[m][scan8[0] + 8 * i + j] = 1;
    1537       12000 :             if (i < 3)
    1538        9000 :                 s->ref_cache[m][scan8[0] + 8 * i + j] = PART_NOT_AVAILABLE;
    1539             :         }
    1540             :     }
    1541             : 
    1542       24000 :     for (s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) {
    1543      472200 :         for (s->mb_x = 0; s->mb_x < s->mb_width; s->mb_x++) {
    1544             :             unsigned mb_type;
    1545      449700 :             s->mb_xy = s->mb_x + s->mb_y * s->mb_stride;
    1546             : 
    1547      449700 :             if ((get_bits_left(&s->gb_slice)) <= 7) {
    1548        4070 :                 if (((get_bits_count(&s->gb_slice) & 7) == 0 ||
    1549        2035 :                     show_bits(&s->gb_slice, get_bits_left(&s->gb_slice) & 7) == 0)) {
    1550             : 
    1551           0 :                     if (svq3_decode_slice_header(avctx))
    1552           0 :                         return -1;
    1553             :                 }
    1554        2035 :                 if (s->slice_type != s->pict_type) {
    1555           0 :                     avpriv_request_sample(avctx, "non constant slice type");
    1556             :                 }
    1557             :                 /* TODO: support s->mb_skip_run */
    1558             :             }
    1559             : 
    1560      449700 :             mb_type = get_interleaved_ue_golomb(&s->gb_slice);
    1561             : 
    1562      449700 :             if (s->pict_type == AV_PICTURE_TYPE_I)
    1563       10770 :                 mb_type += 8;
    1564      438930 :             else if (s->pict_type == AV_PICTURE_TYPE_B && mb_type >= 4)
    1565          22 :                 mb_type += 4;
    1566      449700 :             if (mb_type > 33 || svq3_decode_mb(s, mb_type)) {
    1567           0 :                 av_log(s->avctx, AV_LOG_ERROR,
    1568             :                        "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
    1569           0 :                 return -1;
    1570             :             }
    1571             : 
    1572      449700 :             if (mb_type != 0 || s->cbp)
    1573      261865 :                 hl_decode_mb(s);
    1574             : 
    1575      449700 :             if (s->pict_type != AV_PICTURE_TYPE_B && !s->low_delay)
    1576      448620 :                 s->cur_pic->mb_type[s->mb_x + s->mb_y * s->mb_stride] =
    1577      448620 :                     (s->pict_type == AV_PICTURE_TYPE_P && mb_type < 8) ? (mb_type - 1) : -1;
    1578             :         }
    1579             : 
    1580       89955 :         ff_draw_horiz_band(avctx, s->cur_pic->f,
    1581       44955 :                            s->last_pic->f->data[0] ? s->last_pic->f : NULL,
    1582       22500 :                            16 * s->mb_y, 16, PICT_FRAME, 0,
    1583             :                            s->low_delay);
    1584             :     }
    1585             : 
    1586        1500 :     left = buf_size*8 - get_bits_count(&s->gb_slice);
    1587             : 
    1588        1500 :     if (s->mb_y != s->mb_height || s->mb_x != s->mb_width) {
    1589           0 :         av_log(avctx, AV_LOG_INFO, "frame num %d incomplete pic x %d y %d left %d\n", avctx->frame_number, s->mb_y, s->mb_x, left);
    1590             :         //av_hex_dump(stderr, buf+buf_size-8, 8);
    1591             :     }
    1592             : 
    1593        1500 :     if (left < 0) {
    1594           0 :         av_log(avctx, AV_LOG_ERROR, "frame num %d left %d\n", avctx->frame_number, left);
    1595           0 :         return -1;
    1596             :     }
    1597             : 
    1598        1500 :     if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay)
    1599           4 :         ret = av_frame_ref(data, s->cur_pic->f);
    1600        1496 :     else if (s->last_pic->f->data[0])
    1601        1493 :         ret = av_frame_ref(data, s->last_pic->f);
    1602        1500 :     if (ret < 0)
    1603           0 :         return ret;
    1604             : 
    1605             :     /* Do not output the last pic after seeking. */
    1606        1500 :     if (s->last_pic->f->data[0] || s->low_delay)
    1607        1497 :         *got_frame = 1;
    1608             : 
    1609        1500 :     if (s->pict_type != AV_PICTURE_TYPE_B) {
    1610        1496 :         FFSWAP(SVQ3Frame*, s->cur_pic, s->next_pic);
    1611             :     } else {
    1612           4 :         av_frame_unref(s->cur_pic->f);
    1613             :     }
    1614             : 
    1615        1500 :     return buf_size;
    1616             : }
    1617             : 
    1618           6 : static av_cold int svq3_decode_end(AVCodecContext *avctx)
    1619             : {
    1620           6 :     SVQ3Context *s = avctx->priv_data;
    1621             : 
    1622           6 :     free_picture(avctx, s->cur_pic);
    1623           6 :     free_picture(avctx, s->next_pic);
    1624           6 :     free_picture(avctx, s->last_pic);
    1625           6 :     av_frame_free(&s->cur_pic->f);
    1626           6 :     av_frame_free(&s->next_pic->f);
    1627           6 :     av_frame_free(&s->last_pic->f);
    1628           6 :     av_freep(&s->cur_pic);
    1629           6 :     av_freep(&s->next_pic);
    1630           6 :     av_freep(&s->last_pic);
    1631           6 :     av_freep(&s->slice_buf);
    1632           6 :     av_freep(&s->intra4x4_pred_mode);
    1633           6 :     av_freep(&s->edge_emu_buffer);
    1634           6 :     av_freep(&s->mb2br_xy);
    1635             : 
    1636             : 
    1637           6 :     av_freep(&s->buf);
    1638           6 :     s->buf_size = 0;
    1639             : 
    1640           6 :     return 0;
    1641             : }
    1642             : 
    1643             : AVCodec ff_svq3_decoder = {
    1644             :     .name           = "svq3",
    1645             :     .long_name      = NULL_IF_CONFIG_SMALL("Sorenson Vector Quantizer 3 / Sorenson Video 3 / SVQ3"),
    1646             :     .type           = AVMEDIA_TYPE_VIDEO,
    1647             :     .id             = AV_CODEC_ID_SVQ3,
    1648             :     .priv_data_size = sizeof(SVQ3Context),
    1649             :     .init           = svq3_decode_init,
    1650             :     .close          = svq3_decode_end,
    1651             :     .decode         = svq3_decode_frame,
    1652             :     .capabilities   = AV_CODEC_CAP_DRAW_HORIZ_BAND |
    1653             :                       AV_CODEC_CAP_DR1             |
    1654             :                       AV_CODEC_CAP_DELAY,
    1655             :     .pix_fmts       = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUVJ420P,
    1656             :                                                      AV_PIX_FMT_NONE},
    1657             : };

Generated by: LCOV version 1.13