Coverage Report

Created: 2024-09-06 07:53

/src/theora/lib/dequant.c
Line
Count
Source (jump to first uncovered line)
1
/********************************************************************
2
 *                                                                  *
3
 * THIS FILE IS PART OF THE OggTheora SOFTWARE CODEC SOURCE CODE.   *
4
 * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS     *
5
 * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
6
 * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING.       *
7
 *                                                                  *
8
 * THE Theora SOURCE CODE IS COPYRIGHT (C) 2002-2009                *
9
 * by the Xiph.Org Foundation and contributors http://www.xiph.org/ *
10
 *                                                                  *
11
 ********************************************************************
12
13
  function:
14
    last mod: $Id$
15
16
 ********************************************************************/
17
18
#include <stdlib.h>
19
#include <string.h>
20
#include <ogg/ogg.h>
21
#include "dequant.h"
22
#include "decint.h"
23
24
0
int oc_quant_params_unpack(oc_pack_buf *_opb,th_quant_info *_qinfo){
25
0
  th_quant_base *base_mats;
26
0
  long           val;
27
0
  int            nbase_mats;
28
0
  int            sizes[64];
29
0
  int            indices[64];
30
0
  int            nbits;
31
0
  int            bmi;
32
0
  int            ci;
33
0
  int            qti;
34
0
  int            pli;
35
0
  int            qri;
36
0
  int            qi;
37
0
  int            i;
38
0
  val=oc_pack_read(_opb,3);
39
0
  nbits=(int)val;
40
0
  for(qi=0;qi<64;qi++){
41
0
    val=oc_pack_read(_opb,nbits);
42
0
    _qinfo->loop_filter_limits[qi]=(unsigned char)val;
43
0
  }
44
0
  val=oc_pack_read(_opb,4);
45
0
  nbits=(int)val+1;
46
0
  for(qi=0;qi<64;qi++){
47
0
    val=oc_pack_read(_opb,nbits);
48
0
    _qinfo->ac_scale[qi]=(ogg_uint16_t)val;
49
0
  }
50
0
  val=oc_pack_read(_opb,4);
51
0
  nbits=(int)val+1;
52
0
  for(qi=0;qi<64;qi++){
53
0
    val=oc_pack_read(_opb,nbits);
54
0
    _qinfo->dc_scale[qi]=(ogg_uint16_t)val;
55
0
  }
56
0
  val=oc_pack_read(_opb,9);
57
0
  nbase_mats=(int)val+1;
58
0
  base_mats=_ogg_malloc(nbase_mats*sizeof(base_mats[0]));
59
0
  if(base_mats==NULL)return TH_EFAULT;
60
0
  for(bmi=0;bmi<nbase_mats;bmi++){
61
0
    for(ci=0;ci<64;ci++){
62
0
      val=oc_pack_read(_opb,8);
63
0
      base_mats[bmi][ci]=(unsigned char)val;
64
0
    }
65
0
  }
66
0
  nbits=oc_ilog(nbase_mats-1);
67
0
  for(i=0;i<6;i++){
68
0
    th_quant_ranges *qranges;
69
0
    th_quant_base   *qrbms;
70
0
    int             *qrsizes;
71
0
    qti=i/3;
72
0
    pli=i%3;
73
0
    qranges=_qinfo->qi_ranges[qti]+pli;
74
0
    if(i>0){
75
0
      val=oc_pack_read1(_opb);
76
0
      if(!val){
77
0
        int qtj;
78
0
        int plj;
79
0
        if(qti>0){
80
0
          val=oc_pack_read1(_opb);
81
0
          if(val){
82
0
            qtj=qti-1;
83
0
            plj=pli;
84
0
          }
85
0
          else{
86
0
            qtj=(i-1)/3;
87
0
            plj=(i-1)%3;
88
0
          }
89
0
        }
90
0
        else{
91
0
          qtj=(i-1)/3;
92
0
          plj=(i-1)%3;
93
0
        }
94
0
        *qranges=*(_qinfo->qi_ranges[qtj]+plj);
95
0
        continue;
96
0
      }
97
0
    }
98
0
    val=oc_pack_read(_opb,nbits);
99
0
    indices[0]=(int)val;
100
0
    for(qi=qri=0;qi<63;){
101
0
      val=oc_pack_read(_opb,oc_ilog(62-qi));
102
0
      sizes[qri]=(int)val+1;
103
0
      qi+=(int)val+1;
104
0
      val=oc_pack_read(_opb,nbits);
105
0
      indices[++qri]=(int)val;
106
0
    }
107
    /*Note: The caller is responsible for cleaning up any partially
108
       constructed qinfo.*/
109
0
    if(qi>63){
110
0
      _ogg_free(base_mats);
111
0
      return TH_EBADHEADER;
112
0
    }
113
0
    qranges->nranges=qri;
114
0
    qranges->sizes=qrsizes=(int *)_ogg_malloc(qri*sizeof(qrsizes[0]));
115
0
    if(qranges->sizes==NULL){
116
      /*Note: The caller is responsible for cleaning up any partially
117
         constructed qinfo.*/
118
0
      _ogg_free(base_mats);
119
0
      return TH_EFAULT;
120
0
    }
121
0
    memcpy(qrsizes,sizes,qri*sizeof(qrsizes[0]));
122
0
    qrbms=(th_quant_base *)_ogg_malloc((qri+1)*sizeof(qrbms[0]));
123
0
    if(qrbms==NULL){
124
      /*Note: The caller is responsible for cleaning up any partially
125
         constructed qinfo.*/
126
0
      _ogg_free(base_mats);
127
0
      return TH_EFAULT;
128
0
    }
129
0
    qranges->base_matrices=(const th_quant_base *)qrbms;
130
0
    do{
131
0
      bmi=indices[qri];
132
      /*Note: The caller is responsible for cleaning up any partially
133
         constructed qinfo.*/
134
0
      if(bmi>=nbase_mats){
135
0
        _ogg_free(base_mats);
136
0
        return TH_EBADHEADER;
137
0
      }
138
0
      memcpy(qrbms[qri],base_mats[bmi],sizeof(qrbms[qri]));
139
0
    }
140
0
    while(qri-->0);
141
0
  }
142
0
  _ogg_free(base_mats);
143
0
  return 0;
144
0
}
145
146
6.89k
void oc_quant_params_clear(th_quant_info *_qinfo){
147
6.89k
  int i;
148
48.2k
  for(i=6;i-->0;){
149
41.3k
    int qti;
150
41.3k
    int pli;
151
41.3k
    qti=i/3;
152
41.3k
    pli=i%3;
153
    /*Clear any duplicate pointer references.*/
154
41.3k
    if(i>0){
155
34.4k
      int qtj;
156
34.4k
      int plj;
157
34.4k
      qtj=(i-1)/3;
158
34.4k
      plj=(i-1)%3;
159
34.4k
      if(_qinfo->qi_ranges[qti][pli].sizes==
160
34.4k
       _qinfo->qi_ranges[qtj][plj].sizes){
161
34.4k
        _qinfo->qi_ranges[qti][pli].sizes=NULL;
162
34.4k
      }
163
34.4k
      if(_qinfo->qi_ranges[qti][pli].base_matrices==
164
34.4k
       _qinfo->qi_ranges[qtj][plj].base_matrices){
165
27.5k
        _qinfo->qi_ranges[qti][pli].base_matrices=NULL;
166
27.5k
      }
167
34.4k
    }
168
41.3k
    if(qti>0){
169
20.6k
      if(_qinfo->qi_ranges[1][pli].sizes==
170
20.6k
       _qinfo->qi_ranges[0][pli].sizes){
171
10.3k
        _qinfo->qi_ranges[1][pli].sizes=NULL;
172
10.3k
      }
173
20.6k
      if(_qinfo->qi_ranges[1][pli].base_matrices==
174
20.6k
       _qinfo->qi_ranges[0][pli].base_matrices){
175
10.3k
        _qinfo->qi_ranges[1][pli].base_matrices=NULL;
176
10.3k
      }
177
20.6k
    }
178
    /*Now free all the non-duplicate storage.*/
179
41.3k
    _ogg_free((void *)_qinfo->qi_ranges[qti][pli].sizes);
180
41.3k
    _ogg_free((void *)_qinfo->qi_ranges[qti][pli].base_matrices);
181
41.3k
  }
182
6.89k
}