Coverage Report

Created: 2024-09-06 07:53

/src/theora/lib/state.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 "state.h"
21
#if defined(OC_DUMP_IMAGES)
22
# include <stdio.h>
23
# include "png.h"
24
# include "zlib.h"
25
#endif
26
27
/*The function used to fill in the chroma plane motion vectors for a macro
28
   block when 4 different motion vectors are specified in the luma plane.
29
  This version is for use with chroma decimated in the X and Y directions
30
   (4:2:0).
31
  _cbmvs: The chroma block-level motion vectors to fill in.
32
  _lbmvs: The luma block-level motion vectors.*/
33
422k
static void oc_set_chroma_mvs00(oc_mv _cbmvs[4],const oc_mv _lbmvs[4]){
34
422k
  int dx;
35
422k
  int dy;
36
422k
  dx=OC_MV_X(_lbmvs[0])+OC_MV_X(_lbmvs[1])
37
422k
   +OC_MV_X(_lbmvs[2])+OC_MV_X(_lbmvs[3]);
38
422k
  dy=OC_MV_Y(_lbmvs[0])+OC_MV_Y(_lbmvs[1])
39
422k
   +OC_MV_Y(_lbmvs[2])+OC_MV_Y(_lbmvs[3]);
40
422k
  _cbmvs[0]=OC_MV(OC_DIV_ROUND_POW2(dx,2,2),OC_DIV_ROUND_POW2(dy,2,2));
41
422k
}
42
43
/*The function used to fill in the chroma plane motion vectors for a macro
44
   block when 4 different motion vectors are specified in the luma plane.
45
  This version is for use with chroma decimated in the Y direction.
46
  _cbmvs: The chroma block-level motion vectors to fill in.
47
  _lbmvs: The luma block-level motion vectors.*/
48
0
static void oc_set_chroma_mvs01(oc_mv _cbmvs[4],const oc_mv _lbmvs[4]){
49
0
  int dx;
50
0
  int dy;
51
0
  dx=OC_MV_X(_lbmvs[0])+OC_MV_X(_lbmvs[2]);
52
0
  dy=OC_MV_Y(_lbmvs[0])+OC_MV_Y(_lbmvs[2]);
53
0
  _cbmvs[0]=OC_MV(OC_DIV_ROUND_POW2(dx,1,1),OC_DIV_ROUND_POW2(dy,1,1));
54
0
  dx=OC_MV_X(_lbmvs[1])+OC_MV_X(_lbmvs[3]);
55
0
  dy=OC_MV_Y(_lbmvs[1])+OC_MV_Y(_lbmvs[3]);
56
0
  _cbmvs[1]=OC_MV(OC_DIV_ROUND_POW2(dx,1,1),OC_DIV_ROUND_POW2(dy,1,1));
57
0
}
58
59
/*The function used to fill in the chroma plane motion vectors for a macro
60
   block when 4 different motion vectors are specified in the luma plane.
61
  This version is for use with chroma decimated in the X direction (4:2:2).
62
  _cbmvs: The chroma block-level motion vectors to fill in.
63
  _lbmvs: The luma block-level motion vectors.*/
64
20.1k
static void oc_set_chroma_mvs10(oc_mv _cbmvs[4],const oc_mv _lbmvs[4]){
65
20.1k
  int dx;
66
20.1k
  int dy;
67
20.1k
  dx=OC_MV_X(_lbmvs[0])+OC_MV_X(_lbmvs[1]);
68
20.1k
  dy=OC_MV_Y(_lbmvs[0])+OC_MV_Y(_lbmvs[1]);
69
20.1k
  _cbmvs[0]=OC_MV(OC_DIV_ROUND_POW2(dx,1,1),OC_DIV_ROUND_POW2(dy,1,1));
70
20.1k
  dx=OC_MV_X(_lbmvs[2])+OC_MV_X(_lbmvs[3]);
71
20.1k
  dy=OC_MV_Y(_lbmvs[2])+OC_MV_Y(_lbmvs[3]);
72
20.1k
  _cbmvs[2]=OC_MV(OC_DIV_ROUND_POW2(dx,1,1),OC_DIV_ROUND_POW2(dy,1,1));
73
20.1k
}
74
75
/*The function used to fill in the chroma plane motion vectors for a macro
76
   block when 4 different motion vectors are specified in the luma plane.
77
  This version is for use with no chroma decimation (4:4:4).
78
  _cbmvs: The chroma block-level motion vectors to fill in.
79
  _lmbmv: The luma macro-block level motion vector to fill in for use in
80
           prediction.
81
  _lbmvs: The luma block-level motion vectors.*/
82
29.9k
static void oc_set_chroma_mvs11(oc_mv _cbmvs[4],const oc_mv _lbmvs[4]){
83
29.9k
  _cbmvs[0]=_lbmvs[0];
84
29.9k
  _cbmvs[1]=_lbmvs[1];
85
29.9k
  _cbmvs[2]=_lbmvs[2];
86
29.9k
  _cbmvs[3]=_lbmvs[3];
87
29.9k
}
88
89
/*A table of functions used to fill in the chroma plane motion vectors for a
90
   macro block when 4 different motion vectors are specified in the luma
91
   plane.*/
92
const oc_set_chroma_mvs_func OC_SET_CHROMA_MVS_TABLE[TH_PF_NFORMATS]={
93
  (oc_set_chroma_mvs_func)oc_set_chroma_mvs00,
94
  (oc_set_chroma_mvs_func)oc_set_chroma_mvs01,
95
  (oc_set_chroma_mvs_func)oc_set_chroma_mvs10,
96
  (oc_set_chroma_mvs_func)oc_set_chroma_mvs11
97
};
98
99
100
101
/*Returns the fragment index of the top-left block in a macro block.
102
  This can be used to test whether or not the whole macro block is valid.
103
  _sb_map: The super block map.
104
  _quadi:  The quadrant number.
105
  Return: The index of the fragment of the upper left block in the macro
106
   block, or -1 if the block lies outside the coded frame.*/
107
5.91M
static ptrdiff_t oc_sb_quad_top_left_frag(oc_sb_map_quad _sb_map[4],int _quadi){
108
  /*It so happens that under the Hilbert curve ordering described below, the
109
     upper-left block in each macro block is at index 0, except in macro block
110
     3, where it is at index 2.*/
111
5.91M
  return _sb_map[_quadi][_quadi&_quadi<<1];
112
5.91M
}
113
114
/*Fills in the mapping from block positions to fragment numbers for a single
115
   color plane.
116
  This function also fills in the "valid" flag of each quadrant in the super
117
   block flags.
118
  _sb_maps:  The array of super block maps for the color plane.
119
  _sb_flags: The array of super block flags for the color plane.
120
  _frag0:    The index of the first fragment in the plane.
121
  _hfrags:   The number of horizontal fragments in a coded frame.
122
  _vfrags:   The number of vertical fragments in a coded frame.*/
123
static void oc_sb_create_plane_mapping(oc_sb_map _sb_maps[],
124
10.3k
 oc_sb_flags _sb_flags[],ptrdiff_t _frag0,int _hfrags,int _vfrags){
125
  /*Contains the (macro_block,block) indices for a 4x4 grid of
126
     fragments.
127
    The pattern is a 4x4 Hilbert space-filling curve.
128
    A Hilbert curve has the nice property that as the curve grows larger, its
129
     fractal dimension approaches 2.
130
    The intuition is that nearby blocks in the curve are also close spatially,
131
     with the previous element always an immediate neighbor, so that runs of
132
     blocks should be well correlated.*/
133
10.3k
  static const int SB_MAP[4][4][2]={
134
10.3k
    {{0,0},{0,1},{3,2},{3,3}},
135
10.3k
    {{0,3},{0,2},{3,1},{3,0}},
136
10.3k
    {{1,0},{1,3},{2,0},{2,3}},
137
10.3k
    {{1,1},{1,2},{2,1},{2,2}}
138
10.3k
  };
139
10.3k
  ptrdiff_t  yfrag;
140
10.3k
  unsigned   sbi;
141
10.3k
  int        y;
142
10.3k
  sbi=0;
143
10.3k
  yfrag=_frag0;
144
273k
  for(y=0;;y+=4){
145
273k
    int imax;
146
273k
    int x;
147
    /*Figure out how many columns of blocks in this super block lie within the
148
       image.*/
149
273k
    imax=_vfrags-y;
150
273k
    if(imax>4)imax=4;
151
20.6k
    else if(imax<=0)break;
152
1.74M
    for(x=0;;x+=4,sbi++){
153
1.74M
      ptrdiff_t xfrag;
154
1.74M
      int       jmax;
155
1.74M
      int       quadi;
156
1.74M
      int       i;
157
      /*Figure out how many rows of blocks in this super block lie within the
158
         image.*/
159
1.74M
      jmax=_hfrags-x;
160
1.74M
      if(jmax>4)jmax=4;
161
526k
      else if(jmax<=0)break;
162
      /*By default, set all fragment indices to -1.*/
163
1.47M
      memset(_sb_maps[sbi],0xFF,sizeof(_sb_maps[sbi]));
164
      /*Fill in the fragment map for this super block.*/
165
1.47M
      xfrag=yfrag+x;
166
4.79M
      for(i=0;i<imax;i++){
167
3.31M
        int j;
168
14.2M
        for(j=0;j<jmax;j++){
169
10.9M
          _sb_maps[sbi][SB_MAP[i][j][0]][SB_MAP[i][j][1]]=xfrag+j;
170
10.9M
        }
171
3.31M
        xfrag+=_hfrags;
172
3.31M
      }
173
      /*Mark which quadrants of this super block lie within the image.*/
174
7.39M
      for(quadi=0;quadi<4;quadi++){
175
5.91M
        _sb_flags[sbi].quad_valid|=
176
5.91M
         (oc_sb_quad_top_left_frag(_sb_maps[sbi],quadi)>=0)<<quadi;
177
5.91M
      }
178
1.47M
    }
179
263k
    yfrag+=_hfrags<<2;
180
263k
  }
181
10.3k
}
182
183
/*Fills in the Y plane fragment map for a macro block given the fragment
184
   coordinates of its upper-left hand corner.
185
  _mb_map:    The macro block map to fill.
186
  _fplane: The description of the Y plane.
187
  _xfrag0: The X location of the upper-left hand fragment in the luma plane.
188
  _yfrag0: The Y location of the upper-left hand fragment in the luma plane.*/
189
static void oc_mb_fill_ymapping(oc_mb_map_plane _mb_map[3],
190
1.36M
 const oc_fragment_plane *_fplane,int _xfrag0,int _yfrag0){
191
1.36M
  int i;
192
1.36M
  int j;
193
8.17M
  for(i=0;i<2;i++)for(j=0;j<2;j++){
194
5.44M
    _mb_map[0][i<<1|j]=(_yfrag0+i)*(ptrdiff_t)_fplane->nhfrags+_xfrag0+j;
195
5.44M
  }
196
1.36M
}
197
198
/*Fills in the chroma plane fragment maps for a macro block.
199
  This version is for use with chroma decimated in the X and Y directions
200
   (4:2:0).
201
  _mb_map:  The macro block map to fill.
202
  _fplanes: The descriptions of the fragment planes.
203
  _xfrag0:  The X location of the upper-left hand fragment in the luma plane.
204
  _yfrag0:  The Y location of the upper-left hand fragment in the luma plane.*/
205
static void oc_mb_fill_cmapping00(oc_mb_map_plane _mb_map[3],
206
776k
 const oc_fragment_plane _fplanes[3],int _xfrag0,int _yfrag0){
207
776k
  ptrdiff_t fragi;
208
776k
  _xfrag0>>=1;
209
776k
  _yfrag0>>=1;
210
776k
  fragi=_yfrag0*(ptrdiff_t)_fplanes[1].nhfrags+_xfrag0;
211
776k
  _mb_map[1][0]=fragi+_fplanes[1].froffset;
212
776k
  _mb_map[2][0]=fragi+_fplanes[2].froffset;
213
776k
}
214
215
/*Fills in the chroma plane fragment maps for a macro block.
216
  This version is for use with chroma decimated in the Y direction.
217
  _mb_map:  The macro block map to fill.
218
  _fplanes: The descriptions of the fragment planes.
219
  _xfrag0:  The X location of the upper-left hand fragment in the luma plane.
220
  _yfrag0:  The Y location of the upper-left hand fragment in the luma plane.*/
221
static void oc_mb_fill_cmapping01(oc_mb_map_plane _mb_map[3],
222
0
 const oc_fragment_plane _fplanes[3],int _xfrag0,int _yfrag0){
223
0
  ptrdiff_t fragi;
224
0
  int       j;
225
0
  _yfrag0>>=1;
226
0
  fragi=_yfrag0*(ptrdiff_t)_fplanes[1].nhfrags+_xfrag0;
227
0
  for(j=0;j<2;j++){
228
0
    _mb_map[1][j]=fragi+_fplanes[1].froffset;
229
0
    _mb_map[2][j]=fragi+_fplanes[2].froffset;
230
0
    fragi++;
231
0
  }
232
0
}
233
234
/*Fills in the chroma plane fragment maps for a macro block.
235
  This version is for use with chroma decimated in the X direction (4:2:2).
236
  _mb_map:  The macro block map to fill.
237
  _fplanes: The descriptions of the fragment planes.
238
  _xfrag0:  The X location of the upper-left hand fragment in the luma plane.
239
  _yfrag0:  The Y location of the upper-left hand fragment in the luma plane.*/
240
static void oc_mb_fill_cmapping10(oc_mb_map_plane _mb_map[3],
241
191k
 const oc_fragment_plane _fplanes[3],int _xfrag0,int _yfrag0){
242
191k
  ptrdiff_t fragi;
243
191k
  int       i;
244
191k
  _xfrag0>>=1;
245
191k
  fragi=_yfrag0*(ptrdiff_t)_fplanes[1].nhfrags+_xfrag0;
246
574k
  for(i=0;i<2;i++){
247
382k
    _mb_map[1][i<<1]=fragi+_fplanes[1].froffset;
248
382k
    _mb_map[2][i<<1]=fragi+_fplanes[2].froffset;
249
382k
    fragi+=_fplanes[1].nhfrags;
250
382k
  }
251
191k
}
252
253
/*Fills in the chroma plane fragment maps for a macro block.
254
  This version is for use with no chroma decimation (4:4:4).
255
  This uses the already filled-in luma plane values.
256
  _mb_map:  The macro block map to fill.
257
  _fplanes: The descriptions of the fragment planes.
258
  _xfrag0:  The X location of the upper-left hand fragment in the luma plane.
259
  _yfrag0:  The Y location of the upper-left hand fragment in the luma plane.*/
260
static void oc_mb_fill_cmapping11(oc_mb_map_plane _mb_map[3],
261
393k
 const oc_fragment_plane _fplanes[3],int _xfrag0,int _yfrag0){
262
393k
  int k;
263
393k
  (void)_xfrag0;
264
393k
  (void)_yfrag0;
265
1.96M
  for(k=0;k<4;k++){
266
1.57M
    _mb_map[1][k]=_mb_map[0][k]+_fplanes[1].froffset;
267
1.57M
    _mb_map[2][k]=_mb_map[0][k]+_fplanes[2].froffset;
268
1.57M
  }
269
393k
}
270
271
/*The function type used to fill in the chroma plane fragment maps for a
272
   macro block.
273
  _mb_map:  The macro block map to fill.
274
  _fplanes: The descriptions of the fragment planes.
275
  _xfrag0:  The X location of the upper-left hand fragment in the luma plane.
276
  _yfrag0:  The Y location of the upper-left hand fragment in the luma plane.*/
277
typedef void (*oc_mb_fill_cmapping_func)(oc_mb_map_plane _mb_map[3],
278
 const oc_fragment_plane _fplanes[3],int _xfrag0,int _yfrag0);
279
280
/*A table of functions used to fill in the chroma plane fragment maps for a
281
   macro block for each type of chrominance decimation.*/
282
static const oc_mb_fill_cmapping_func OC_MB_FILL_CMAPPING_TABLE[4]={
283
  oc_mb_fill_cmapping00,
284
  oc_mb_fill_cmapping01,
285
  oc_mb_fill_cmapping10,
286
  oc_mb_fill_cmapping11
287
};
288
289
/*Fills in the mapping from macro blocks to their corresponding fragment
290
   numbers in each plane.
291
  _mb_maps:   The list of macro block maps.
292
  _mb_modes:  The list of macro block modes; macro blocks completely outside
293
               the coded region are marked invalid.
294
  _fplanes:   The descriptions of the fragment planes.
295
  _pixel_fmt: The chroma decimation type.*/
296
static void oc_mb_create_mapping(oc_mb_map _mb_maps[],
297
3.44k
 signed char _mb_modes[],const oc_fragment_plane _fplanes[3],int _pixel_fmt){
298
3.44k
  oc_mb_fill_cmapping_func  mb_fill_cmapping;
299
3.44k
  unsigned                  sbi;
300
3.44k
  int                       y;
301
3.44k
  mb_fill_cmapping=OC_MB_FILL_CMAPPING_TABLE[_pixel_fmt];
302
  /*Loop through the luma plane super blocks.*/
303
110k
  for(sbi=y=0;y<_fplanes[0].nvfrags;y+=4){
304
107k
    int x;
305
746k
    for(x=0;x<_fplanes[0].nhfrags;x+=4,sbi++){
306
639k
      int ymb;
307
      /*Loop through the macro blocks in each super block in display order.*/
308
1.91M
      for(ymb=0;ymb<2;ymb++){
309
1.27M
        int xmb;
310
3.83M
        for(xmb=0;xmb<2;xmb++){
311
2.55M
          unsigned mbi;
312
2.55M
          int      mbx;
313
2.55M
          int      mby;
314
2.55M
          mbi=sbi<<2|OC_MB_MAP[ymb][xmb];
315
2.55M
          mbx=x|xmb<<1;
316
2.55M
          mby=y|ymb<<1;
317
          /*Initialize fragment indices to -1.*/
318
2.55M
          memset(_mb_maps[mbi],0xFF,sizeof(_mb_maps[mbi]));
319
          /*Make sure this macro block is within the encoded region.*/
320
2.55M
          if(mbx>=_fplanes[0].nhfrags||mby>=_fplanes[0].nvfrags){
321
1.19M
            _mb_modes[mbi]=OC_MODE_INVALID;
322
1.19M
            continue;
323
1.19M
          }
324
          /*Fill in the fragment indices for the luma plane.*/
325
1.36M
          oc_mb_fill_ymapping(_mb_maps[mbi],_fplanes,mbx,mby);
326
          /*Fill in the fragment indices for the chroma planes.*/
327
1.36M
          (*mb_fill_cmapping)(_mb_maps[mbi],_fplanes,mbx,mby);
328
1.36M
        }
329
1.27M
      }
330
639k
    }
331
107k
  }
332
3.44k
}
333
334
/*Marks the fragments which fall all or partially outside the displayable
335
   region of the frame.
336
  _state: The Theora state containing the fragments to be marked.*/
337
3.44k
static void oc_state_border_init(oc_theora_state *_state){
338
3.44k
  oc_fragment       *frag;
339
3.44k
  oc_fragment       *yfrag_end;
340
3.44k
  oc_fragment       *xfrag_end;
341
3.44k
  oc_fragment_plane *fplane;
342
3.44k
  int                crop_x0;
343
3.44k
  int                crop_y0;
344
3.44k
  int                crop_xf;
345
3.44k
  int                crop_yf;
346
3.44k
  int                pli;
347
3.44k
  int                y;
348
3.44k
  int                x;
349
  /*The method we use here is slow, but the code is dead simple and handles
350
     all the special cases easily.
351
    We only ever need to do it once.*/
352
  /*Loop through the fragments, marking those completely outside the
353
     displayable region and constructing a border mask for those that straddle
354
     the border.*/
355
3.44k
  _state->nborders=0;
356
3.44k
  yfrag_end=frag=_state->frags;
357
13.7k
  for(pli=0;pli<3;pli++){
358
10.3k
    fplane=_state->fplanes+pli;
359
    /*Set up the cropping rectangle for this plane.*/
360
10.3k
    crop_x0=_state->info.pic_x;
361
10.3k
    crop_xf=_state->info.pic_x+_state->info.pic_width;
362
10.3k
    crop_y0=_state->info.pic_y;
363
10.3k
    crop_yf=_state->info.pic_y+_state->info.pic_height;
364
10.3k
    if(pli>0){
365
6.89k
      if(!(_state->info.pixel_fmt&1)){
366
5.91k
        crop_x0=crop_x0>>1;
367
5.91k
        crop_xf=crop_xf+1>>1;
368
5.91k
      }
369
6.89k
      if(!(_state->info.pixel_fmt&2)){
370
5.34k
        crop_y0=crop_y0>>1;
371
5.34k
        crop_yf=crop_yf+1>>1;
372
5.34k
      }
373
6.89k
    }
374
10.3k
    y=0;
375
1.05M
    for(yfrag_end+=fplane->nfrags;frag<yfrag_end;y+=8){
376
1.04M
      x=0;
377
11.9M
      for(xfrag_end=frag+fplane->nhfrags;frag<xfrag_end;frag++,x+=8){
378
        /*First check to see if this fragment is completely outside the
379
           displayable region.*/
380
        /*Note the special checks for an empty cropping rectangle.
381
          This guarantees that if we count a fragment as straddling the
382
           border below, at least one pixel in the fragment will be inside
383
           the displayable region.*/
384
10.9M
        if(x+8<=crop_x0||crop_xf<=x||y+8<=crop_y0||crop_yf<=y||
385
10.9M
         crop_x0>=crop_xf||crop_y0>=crop_yf){
386
4.16M
          frag->invalid=1;
387
4.16M
        }
388
        /*Otherwise, check to see if it straddles the border.*/
389
6.74M
        else if(x<crop_x0&&crop_x0<x+8||x<crop_xf&&crop_xf<x+8||
390
6.74M
         y<crop_y0&&crop_y0<y+8||y<crop_yf&&crop_yf<y+8){
391
5.62M
          ogg_int64_t mask;
392
5.62M
          int         npixels;
393
5.62M
          int         i;
394
5.62M
          mask=npixels=0;
395
50.6M
          for(i=0;i<8;i++){
396
45.0M
            int j;
397
405M
            for(j=0;j<8;j++){
398
360M
              if(x+j>=crop_x0&&x+j<crop_xf&&y+i>=crop_y0&&y+i<crop_yf){
399
106M
                mask|=(ogg_int64_t)1<<(i<<3|j);
400
106M
                npixels++;
401
106M
              }
402
360M
            }
403
45.0M
          }
404
          /*Search the fragment array for border info with the same pattern.
405
            In general, there will be at most 8 different patterns (per
406
             plane).*/
407
7.64M
          for(i=0;;i++){
408
7.64M
            if(i>=_state->nborders){
409
7.23k
              _state->nborders++;
410
7.23k
              _state->borders[i].mask=mask;
411
7.23k
              _state->borders[i].npixels=npixels;
412
7.23k
            }
413
7.63M
            else if(_state->borders[i].mask!=mask)continue;
414
5.62M
            frag->borderi=i;
415
5.62M
            break;
416
7.64M
          }
417
5.62M
        }
418
1.11M
        else frag->borderi=-1;
419
10.9M
      }
420
1.04M
    }
421
10.3k
  }
422
3.44k
}
423
424
3.44k
static int oc_state_frarray_init(oc_theora_state *_state){
425
3.44k
  int       yhfrags;
426
3.44k
  int       yvfrags;
427
3.44k
  int       chfrags;
428
3.44k
  int       cvfrags;
429
3.44k
  ptrdiff_t yfrags;
430
3.44k
  ptrdiff_t cfrags;
431
3.44k
  ptrdiff_t nfrags;
432
3.44k
  unsigned  yhsbs;
433
3.44k
  unsigned  yvsbs;
434
3.44k
  unsigned  chsbs;
435
3.44k
  unsigned  cvsbs;
436
3.44k
  unsigned  ysbs;
437
3.44k
  unsigned  csbs;
438
3.44k
  unsigned  nsbs;
439
3.44k
  size_t    nmbs;
440
3.44k
  int       hdec;
441
3.44k
  int       vdec;
442
3.44k
  int       pli;
443
  /*Figure out the number of fragments in each plane.*/
444
  /*These parameters have already been validated to be multiples of 16.*/
445
3.44k
  yhfrags=_state->info.frame_width>>3;
446
3.44k
  yvfrags=_state->info.frame_height>>3;
447
3.44k
  hdec=!(_state->info.pixel_fmt&1);
448
3.44k
  vdec=!(_state->info.pixel_fmt&2);
449
3.44k
  chfrags=yhfrags+hdec>>hdec;
450
3.44k
  cvfrags=yvfrags+vdec>>vdec;
451
3.44k
  yfrags=yhfrags*(ptrdiff_t)yvfrags;
452
3.44k
  cfrags=chfrags*(ptrdiff_t)cvfrags;
453
3.44k
  nfrags=yfrags+2*cfrags;
454
  /*Figure out the number of super blocks in each plane.*/
455
3.44k
  yhsbs=yhfrags+3>>2;
456
3.44k
  yvsbs=yvfrags+3>>2;
457
3.44k
  chsbs=chfrags+3>>2;
458
3.44k
  cvsbs=cvfrags+3>>2;
459
3.44k
  ysbs=yhsbs*yvsbs;
460
3.44k
  csbs=chsbs*cvsbs;
461
3.44k
  nsbs=ysbs+2*csbs;
462
3.44k
  nmbs=(size_t)ysbs<<2;
463
  /*Check for overflow.
464
    We support the ridiculous upper limits of the specification (1048560 by
465
     1048560, or 3 TB frames) if the target architecture has 64-bit pointers,
466
     but for those with 32-bit pointers (or smaller!) we have to check.
467
    If the caller wants to prevent denial-of-service by imposing a more
468
     reasonable upper limit on the size of attempted allocations, they must do
469
     so themselves; we have no platform independent way to determine how much
470
     system memory there is nor an application-independent way to decide what a
471
     "reasonable" allocation is.*/
472
3.44k
  if(yfrags/yhfrags!=yvfrags||2*cfrags<cfrags||nfrags<yfrags||
473
3.44k
   ysbs/yhsbs!=yvsbs||2*csbs<csbs||nsbs<ysbs||nmbs>>2!=ysbs){
474
0
    return TH_EIMPL;
475
0
  }
476
  /*Initialize the fragment array.*/
477
3.44k
  _state->fplanes[0].nhfrags=yhfrags;
478
3.44k
  _state->fplanes[0].nvfrags=yvfrags;
479
3.44k
  _state->fplanes[0].froffset=0;
480
3.44k
  _state->fplanes[0].nfrags=yfrags;
481
3.44k
  _state->fplanes[0].nhsbs=yhsbs;
482
3.44k
  _state->fplanes[0].nvsbs=yvsbs;
483
3.44k
  _state->fplanes[0].sboffset=0;
484
3.44k
  _state->fplanes[0].nsbs=ysbs;
485
3.44k
  _state->fplanes[1].nhfrags=_state->fplanes[2].nhfrags=chfrags;
486
3.44k
  _state->fplanes[1].nvfrags=_state->fplanes[2].nvfrags=cvfrags;
487
3.44k
  _state->fplanes[1].froffset=yfrags;
488
3.44k
  _state->fplanes[2].froffset=yfrags+cfrags;
489
3.44k
  _state->fplanes[1].nfrags=_state->fplanes[2].nfrags=cfrags;
490
3.44k
  _state->fplanes[1].nhsbs=_state->fplanes[2].nhsbs=chsbs;
491
3.44k
  _state->fplanes[1].nvsbs=_state->fplanes[2].nvsbs=cvsbs;
492
3.44k
  _state->fplanes[1].sboffset=ysbs;
493
3.44k
  _state->fplanes[2].sboffset=ysbs+csbs;
494
3.44k
  _state->fplanes[1].nsbs=_state->fplanes[2].nsbs=csbs;
495
3.44k
  _state->nfrags=nfrags;
496
3.44k
  _state->frags=_ogg_calloc(nfrags,sizeof(*_state->frags));
497
3.44k
  _state->frag_mvs=_ogg_malloc(nfrags*sizeof(*_state->frag_mvs));
498
3.44k
  _state->nsbs=nsbs;
499
3.44k
  _state->sb_maps=_ogg_malloc(nsbs*sizeof(*_state->sb_maps));
500
3.44k
  _state->sb_flags=_ogg_calloc(nsbs,sizeof(*_state->sb_flags));
501
3.44k
  _state->nhmbs=yhsbs<<1;
502
3.44k
  _state->nvmbs=yvsbs<<1;
503
3.44k
  _state->nmbs=nmbs;
504
3.44k
  _state->mb_maps=_ogg_calloc(nmbs,sizeof(*_state->mb_maps));
505
3.44k
  _state->mb_modes=_ogg_calloc(nmbs,sizeof(*_state->mb_modes));
506
3.44k
  _state->coded_fragis=_ogg_malloc(nfrags*sizeof(*_state->coded_fragis));
507
3.44k
  if(_state->frags==NULL||_state->frag_mvs==NULL||_state->sb_maps==NULL||
508
3.44k
   _state->sb_flags==NULL||_state->mb_maps==NULL||_state->mb_modes==NULL||
509
3.44k
   _state->coded_fragis==NULL){
510
0
    return TH_EFAULT;
511
0
  }
512
  /*Create the mapping from super blocks to fragments.*/
513
13.7k
  for(pli=0;pli<3;pli++){
514
10.3k
    oc_fragment_plane *fplane;
515
10.3k
    fplane=_state->fplanes+pli;
516
10.3k
    oc_sb_create_plane_mapping(_state->sb_maps+fplane->sboffset,
517
10.3k
     _state->sb_flags+fplane->sboffset,fplane->froffset,
518
10.3k
     fplane->nhfrags,fplane->nvfrags);
519
10.3k
  }
520
  /*Create the mapping from macro blocks to fragments.*/
521
3.44k
  oc_mb_create_mapping(_state->mb_maps,_state->mb_modes,
522
3.44k
   _state->fplanes,_state->info.pixel_fmt);
523
  /*Initialize the invalid and borderi fields of each fragment.*/
524
3.44k
  oc_state_border_init(_state);
525
3.44k
  return 0;
526
3.44k
}
527
528
3.44k
static void oc_state_frarray_clear(oc_theora_state *_state){
529
3.44k
  _ogg_free(_state->coded_fragis);
530
3.44k
  _ogg_free(_state->mb_modes);
531
3.44k
  _ogg_free(_state->mb_maps);
532
3.44k
  _ogg_free(_state->sb_flags);
533
3.44k
  _ogg_free(_state->sb_maps);
534
3.44k
  _ogg_free(_state->frag_mvs);
535
3.44k
  _ogg_free(_state->frags);
536
3.44k
}
537
538
539
/*Initializes the buffers used for reconstructed frames.
540
  These buffers are padded with 16 extra pixels on each side, to allow
541
   unrestricted motion vectors without special casing the boundary.
542
  If chroma is decimated in either direction, the padding is reduced by a
543
   factor of 2 on the appropriate sides.
544
  _nrefs: The number of reference buffers to init; must be in the range 3...6.*/
545
3.44k
static int oc_state_ref_bufs_init(oc_theora_state *_state,int _nrefs){
546
3.44k
  th_info       *info;
547
3.44k
  unsigned char *ref_frame_data;
548
3.44k
  size_t         ref_frame_data_sz;
549
3.44k
  size_t         ref_frame_sz;
550
3.44k
  size_t         yplane_sz;
551
3.44k
  size_t         cplane_sz;
552
3.44k
  int            yhstride;
553
3.44k
  int            yheight;
554
3.44k
  int            chstride;
555
3.44k
  int            cheight;
556
3.44k
  ptrdiff_t      align;
557
3.44k
  ptrdiff_t      yoffset;
558
3.44k
  ptrdiff_t      coffset;
559
3.44k
  ptrdiff_t     *frag_buf_offs;
560
3.44k
  ptrdiff_t      fragi;
561
3.44k
  int            hdec;
562
3.44k
  int            vdec;
563
3.44k
  int            rfi;
564
3.44k
  int            pli;
565
3.44k
  if(_nrefs<3||_nrefs>6)return TH_EINVAL;
566
3.44k
  info=&_state->info;
567
  /*Compute the image buffer parameters for each plane.*/
568
3.44k
  hdec=!(info->pixel_fmt&1);
569
3.44k
  vdec=!(info->pixel_fmt&2);
570
3.44k
  yhstride=info->frame_width+2*OC_UMV_PADDING;
571
3.44k
  yheight=info->frame_height+2*OC_UMV_PADDING;
572
  /*Require 16-byte aligned rows in the chroma planes.*/
573
3.44k
  chstride=(yhstride>>hdec)+15&~15;
574
3.44k
  cheight=yheight>>vdec;
575
3.44k
  yplane_sz=yhstride*(size_t)yheight;
576
3.44k
  cplane_sz=chstride*(size_t)cheight;
577
3.44k
  yoffset=OC_UMV_PADDING+OC_UMV_PADDING*(ptrdiff_t)yhstride;
578
3.44k
  coffset=(OC_UMV_PADDING>>hdec)+(OC_UMV_PADDING>>vdec)*(ptrdiff_t)chstride;
579
  /*Although we guarantee the rows of the chroma planes are a multiple of 16
580
     bytes, the initial padding on the first row may only be 8 bytes.
581
    Compute the offset needed to the actual image data to a multiple of 16.*/
582
3.44k
  align=-coffset&15;
583
3.44k
  ref_frame_sz=yplane_sz+2*cplane_sz+16;
584
3.44k
  ref_frame_data_sz=_nrefs*ref_frame_sz;
585
  /*Check for overflow.
586
    The same caveats apply as for oc_state_frarray_init().*/
587
3.44k
  if(yplane_sz/yhstride!=(size_t)yheight||2*cplane_sz+16<cplane_sz||
588
3.44k
   ref_frame_sz<yplane_sz||ref_frame_data_sz/_nrefs!=ref_frame_sz){
589
0
    return TH_EIMPL;
590
0
  }
591
3.44k
  ref_frame_data=oc_aligned_malloc(ref_frame_data_sz,16);
592
3.44k
  frag_buf_offs=_state->frag_buf_offs=
593
3.44k
   _ogg_malloc(_state->nfrags*sizeof(*frag_buf_offs));
594
3.44k
  if(ref_frame_data==NULL||frag_buf_offs==NULL){
595
0
    _ogg_free(frag_buf_offs);
596
0
    oc_aligned_free(ref_frame_data);
597
0
    return TH_EFAULT;
598
0
  }
599
  /*Set up the width, height and stride for the image buffers.*/
600
3.44k
  _state->ref_frame_bufs[0][0].width=info->frame_width;
601
3.44k
  _state->ref_frame_bufs[0][0].height=info->frame_height;
602
3.44k
  _state->ref_frame_bufs[0][0].stride=yhstride;
603
3.44k
  _state->ref_frame_bufs[0][1].width=_state->ref_frame_bufs[0][2].width=
604
3.44k
   info->frame_width>>hdec;
605
3.44k
  _state->ref_frame_bufs[0][1].height=_state->ref_frame_bufs[0][2].height=
606
3.44k
   info->frame_height>>vdec;
607
3.44k
  _state->ref_frame_bufs[0][1].stride=_state->ref_frame_bufs[0][2].stride=
608
3.44k
   chstride;
609
20.6k
  for(rfi=1;rfi<_nrefs;rfi++){
610
17.2k
    memcpy(_state->ref_frame_bufs[rfi],_state->ref_frame_bufs[0],
611
17.2k
     sizeof(_state->ref_frame_bufs[0]));
612
17.2k
  }
613
3.44k
  _state->ref_frame_handle=ref_frame_data;
614
  /*Set up the data pointers for the image buffers.*/
615
24.1k
  for(rfi=0;rfi<_nrefs;rfi++){
616
20.6k
    _state->ref_frame_bufs[rfi][0].data=ref_frame_data+yoffset;
617
20.6k
    ref_frame_data+=yplane_sz+align;
618
20.6k
    _state->ref_frame_bufs[rfi][1].data=ref_frame_data+coffset;
619
20.6k
    ref_frame_data+=cplane_sz;
620
20.6k
    _state->ref_frame_bufs[rfi][2].data=ref_frame_data+coffset;
621
20.6k
    ref_frame_data+=cplane_sz+(16-align);
622
    /*Flip the buffer upside down.
623
      This allows us to decode Theora's bottom-up frames in their natural
624
       order, yet return a top-down buffer with a positive stride to the user.*/
625
20.6k
    oc_ycbcr_buffer_flip(_state->ref_frame_bufs[rfi],
626
20.6k
     _state->ref_frame_bufs[rfi]);
627
20.6k
  }
628
3.44k
  _state->ref_ystride[0]=-yhstride;
629
3.44k
  _state->ref_ystride[1]=_state->ref_ystride[2]=-chstride;
630
  /*Initialize the fragment buffer offsets.*/
631
3.44k
  ref_frame_data=_state->ref_frame_bufs[0][0].data;
632
3.44k
  fragi=0;
633
13.7k
  for(pli=0;pli<3;pli++){
634
10.3k
    th_img_plane      *iplane;
635
10.3k
    oc_fragment_plane *fplane;
636
10.3k
    unsigned char     *vpix;
637
10.3k
    ptrdiff_t          stride;
638
10.3k
    ptrdiff_t          vfragi_end;
639
10.3k
    int                nhfrags;
640
10.3k
    iplane=_state->ref_frame_bufs[0]+pli;
641
10.3k
    fplane=_state->fplanes+pli;
642
10.3k
    vpix=iplane->data;
643
10.3k
    vfragi_end=fplane->froffset+fplane->nfrags;
644
10.3k
    nhfrags=fplane->nhfrags;
645
10.3k
    stride=iplane->stride;
646
1.05M
    while(fragi<vfragi_end){
647
1.04M
      ptrdiff_t      hfragi_end;
648
1.04M
      unsigned char *hpix;
649
1.04M
      hpix=vpix;
650
11.9M
      for(hfragi_end=fragi+nhfrags;fragi<hfragi_end;fragi++){
651
10.9M
        frag_buf_offs[fragi]=hpix-ref_frame_data;
652
10.9M
        hpix+=8;
653
10.9M
      }
654
1.04M
      vpix+=stride<<3;
655
1.04M
    }
656
10.3k
  }
657
  /*Initialize the reference frame pointers and indices.*/
658
3.44k
  _state->ref_frame_idx[OC_FRAME_GOLD]=
659
3.44k
   _state->ref_frame_idx[OC_FRAME_PREV]=
660
3.44k
   _state->ref_frame_idx[OC_FRAME_GOLD_ORIG]=
661
3.44k
   _state->ref_frame_idx[OC_FRAME_PREV_ORIG]=
662
3.44k
   _state->ref_frame_idx[OC_FRAME_SELF]=
663
3.44k
   _state->ref_frame_idx[OC_FRAME_IO]=-1;
664
3.44k
  _state->ref_frame_data[OC_FRAME_GOLD]=
665
3.44k
   _state->ref_frame_data[OC_FRAME_PREV]=
666
3.44k
   _state->ref_frame_data[OC_FRAME_GOLD_ORIG]=
667
3.44k
   _state->ref_frame_data[OC_FRAME_PREV_ORIG]=
668
3.44k
   _state->ref_frame_data[OC_FRAME_SELF]=
669
3.44k
   _state->ref_frame_data[OC_FRAME_IO]=NULL;
670
3.44k
  return 0;
671
3.44k
}
672
673
3.44k
static void oc_state_ref_bufs_clear(oc_theora_state *_state){
674
3.44k
  _ogg_free(_state->frag_buf_offs);
675
3.44k
  oc_aligned_free(_state->ref_frame_handle);
676
3.44k
}
677
678
679
3.44k
void oc_state_accel_init_c(oc_theora_state *_state){
680
3.44k
  _state->cpu_flags=0;
681
#if defined(OC_STATE_USE_VTABLE)
682
  _state->opt_vtable.frag_copy=oc_frag_copy_c;
683
  _state->opt_vtable.frag_copy_list=oc_frag_copy_list_c;
684
  _state->opt_vtable.frag_recon_intra=oc_frag_recon_intra_c;
685
  _state->opt_vtable.frag_recon_inter=oc_frag_recon_inter_c;
686
  _state->opt_vtable.frag_recon_inter2=oc_frag_recon_inter2_c;
687
  _state->opt_vtable.idct8x8=oc_idct8x8_c;
688
  _state->opt_vtable.state_frag_recon=oc_state_frag_recon_c;
689
  _state->opt_vtable.loop_filter_init=oc_loop_filter_init_c;
690
  _state->opt_vtable.state_loop_filter_frag_rows=
691
   oc_state_loop_filter_frag_rows_c;
692
  _state->opt_vtable.restore_fpu=oc_restore_fpu_c;
693
#endif
694
3.44k
  _state->opt_data.dct_fzig_zag=OC_FZIG_ZAG;
695
3.44k
}
696
697
698
3.44k
int oc_state_init(oc_theora_state *_state,const th_info *_info,int _nrefs){
699
3.44k
  int ret;
700
  /*First validate the parameters.*/
701
3.44k
  if(_info==NULL)return TH_EFAULT;
702
  /*The width and height of the encoded frame must be multiples of 16.
703
    They must also, when divided by 16, fit into a 16-bit unsigned integer.
704
    The displayable frame offset coordinates must fit into an 8-bit unsigned
705
     integer.
706
    Note that the offset Y in the API is specified on the opposite side from
707
     how it is specified in the bitstream, because the Y axis is flipped in
708
     the bitstream.
709
    The displayable frame must fit inside the encoded frame.
710
    The color space must be one known by the encoder.
711
    The framerate ratio must not contain a zero value.*/
712
3.44k
  if((_info->frame_width&0xF)||(_info->frame_height&0xF)||
713
3.44k
   _info->frame_width<=0||_info->frame_width>=0x100000||
714
3.44k
   _info->frame_height<=0||_info->frame_height>=0x100000||
715
3.44k
   _info->pic_x+_info->pic_width>_info->frame_width||
716
3.44k
   _info->pic_y+_info->pic_height>_info->frame_height||
717
3.44k
   _info->pic_x>255||_info->frame_height-_info->pic_height-_info->pic_y>255||
718
   /*Note: the following <0 comparisons may generate spurious warnings on
719
      platforms where enums are unsigned.
720
     We could cast them to unsigned and just use the following >= comparison,
721
      but there are a number of compilers which will mis-optimize this.
722
     It's better to live with the spurious warnings.*/
723
3.44k
   _info->colorspace<0||_info->colorspace>=TH_CS_NSPACES||
724
3.44k
   _info->pixel_fmt<0||_info->pixel_fmt>=TH_PF_NFORMATS||
725
3.44k
   _info->fps_numerator<1||_info->fps_denominator<1){
726
0
    return TH_EINVAL;
727
0
  }
728
3.44k
  memset(_state,0,sizeof(*_state));
729
3.44k
  memcpy(&_state->info,_info,sizeof(*_info));
730
  /*Invert the sense of pic_y to match Theora's right-handed coordinate
731
     system.*/
732
3.44k
  _state->info.pic_y=_info->frame_height-_info->pic_height-_info->pic_y;
733
3.44k
  _state->frame_type=OC_UNKWN_FRAME;
734
3.44k
  oc_state_accel_init(_state);
735
3.44k
  ret=oc_state_frarray_init(_state);
736
3.44k
  if(ret>=0)ret=oc_state_ref_bufs_init(_state,_nrefs);
737
3.44k
  if(ret<0){
738
0
    oc_state_frarray_clear(_state);
739
0
    return ret;
740
0
  }
741
  /*If the keyframe_granule_shift is out of range, use the maximum allowable
742
     value.*/
743
3.44k
  if(_info->keyframe_granule_shift<0||_info->keyframe_granule_shift>31){
744
0
    _state->info.keyframe_granule_shift=31;
745
0
  }
746
3.44k
  _state->keyframe_num=0;
747
3.44k
  _state->curframe_num=-1;
748
  /*3.2.0 streams mark the frame index instead of the frame count.
749
    This was changed with stream version 3.2.1 to conform to other Ogg
750
     codecs.
751
    We add an extra bias when computing granule positions for new streams.*/
752
3.44k
  _state->granpos_bias=TH_VERSION_CHECK(_info,3,2,1);
753
3.44k
  return 0;
754
3.44k
}
755
756
3.44k
void oc_state_clear(oc_theora_state *_state){
757
3.44k
  oc_state_ref_bufs_clear(_state);
758
3.44k
  oc_state_frarray_clear(_state);
759
3.44k
}
760
761
762
/*Duplicates the pixels on the border of the image plane out into the
763
   surrounding padding for use by unrestricted motion vectors.
764
  This function only adds the left and right borders, and only for the fragment
765
   rows specified.
766
  _refi: The index of the reference buffer to pad.
767
  _pli:  The color plane.
768
  _y0:   The Y coordinate of the first row to pad.
769
  _yend: The Y coordinate of the row to stop padding at.*/
770
void oc_state_borders_fill_rows(oc_theora_state *_state,int _refi,int _pli,
771
774k
 int _y0,int _yend){
772
774k
  th_img_plane  *iplane;
773
774k
  unsigned char *apix;
774
774k
  unsigned char *bpix;
775
774k
  unsigned char *epix;
776
774k
  int            stride;
777
774k
  int            hpadding;
778
774k
  hpadding=OC_UMV_PADDING>>(_pli!=0&&!(_state->info.pixel_fmt&1));
779
774k
  iplane=_state->ref_frame_bufs[_refi]+_pli;
780
774k
  stride=iplane->stride;
781
774k
  apix=iplane->data+_y0*(ptrdiff_t)stride;
782
774k
  bpix=apix+iplane->width-1;
783
774k
  epix=iplane->data+_yend*(ptrdiff_t)stride;
784
  /*Note the use of != instead of <, which allows the stride to be negative.*/
785
31.9M
  while(apix!=epix){
786
31.1M
    memset(apix-hpadding,apix[0],hpadding);
787
31.1M
    memset(bpix+1,bpix[0],hpadding);
788
31.1M
    apix+=stride;
789
31.1M
    bpix+=stride;
790
31.1M
  }
791
774k
}
792
793
/*Duplicates the pixels on the border of the image plane out into the
794
   surrounding padding for use by unrestricted motion vectors.
795
  This function only adds the top and bottom borders, and must be called after
796
   the left and right borders are added.
797
  _refi:      The index of the reference buffer to pad.
798
  _pli:       The color plane.*/
799
312k
void oc_state_borders_fill_caps(oc_theora_state *_state,int _refi,int _pli){
800
312k
  th_img_plane  *iplane;
801
312k
  unsigned char *apix;
802
312k
  unsigned char *bpix;
803
312k
  unsigned char *epix;
804
312k
  int            stride;
805
312k
  int            hpadding;
806
312k
  int            vpadding;
807
312k
  int            fullw;
808
312k
  hpadding=OC_UMV_PADDING>>(_pli!=0&&!(_state->info.pixel_fmt&1));
809
312k
  vpadding=OC_UMV_PADDING>>(_pli!=0&&!(_state->info.pixel_fmt&2));
810
312k
  iplane=_state->ref_frame_bufs[_refi]+_pli;
811
312k
  stride=iplane->stride;
812
312k
  fullw=iplane->width+(hpadding<<1);
813
312k
  apix=iplane->data-hpadding;
814
312k
  bpix=iplane->data+(iplane->height-1)*(ptrdiff_t)stride-hpadding;
815
312k
  epix=apix-stride*(ptrdiff_t)vpadding;
816
3.75M
  while(apix!=epix){
817
3.44M
    memcpy(apix-stride,apix,fullw);
818
3.44M
    memcpy(bpix+stride,bpix,fullw);
819
3.44M
    apix-=stride;
820
3.44M
    bpix+=stride;
821
3.44M
  }
822
312k
}
823
824
/*Duplicates the pixels on the border of the given reference image out into
825
   the surrounding padding for use by unrestricted motion vectors.
826
  _state: The context containing the reference buffers.
827
  _refi:  The index of the reference buffer to pad.*/
828
0
void oc_state_borders_fill(oc_theora_state *_state,int _refi){
829
0
  int pli;
830
0
  for(pli=0;pli<3;pli++){
831
0
    oc_state_borders_fill_rows(_state,_refi,pli,0,
832
0
     _state->ref_frame_bufs[_refi][pli].height);
833
0
    oc_state_borders_fill_caps(_state,_refi,pli);
834
0
  }
835
0
}
836
837
/*Determines the offsets in an image buffer to use for motion compensation.
838
  _state:   The Theora state the offsets are to be computed with.
839
  _offsets: Returns the offset for the buffer(s).
840
            _offsets[0] is always set.
841
            _offsets[1] is set if the motion vector has non-zero fractional
842
             components.
843
  _pli:     The color plane index.
844
  _mv:      The motion vector.
845
  Return: The number of offsets returned: 1 or 2.*/
846
int oc_state_get_mv_offsets(const oc_theora_state *_state,int _offsets[2],
847
9.52M
 int _pli,oc_mv _mv){
848
  /*Here is a brief description of how Theora handles motion vectors:
849
    Motion vector components are specified to half-pixel accuracy in
850
     undecimated directions of each plane, and quarter-pixel accuracy in
851
     decimated directions.
852
    Integer parts are extracted by dividing (not shifting) by the
853
     appropriate amount, with truncation towards zero.
854
    These integer values are used to calculate the first offset.
855
856
    If either of the fractional parts are non-zero, then a second offset is
857
     computed.
858
    No third or fourth offsets are computed, even if both components have
859
     non-zero fractional parts.
860
    The second offset is computed by dividing (not shifting) by the
861
     appropriate amount, always truncating _away_ from zero.*/
862
#if 0
863
  /*This version of the code doesn't use any tables, but is slower.*/
864
  int ystride;
865
  int xprec;
866
  int yprec;
867
  int xfrac;
868
  int yfrac;
869
  int offs;
870
  int dx;
871
  int dy;
872
  ystride=_state->ref_ystride[_pli];
873
  /*These two variables decide whether we are in half- or quarter-pixel
874
     precision in each component.*/
875
  xprec=1+(_pli!=0&&!(_state->info.pixel_fmt&1));
876
  yprec=1+(_pli!=0&&!(_state->info.pixel_fmt&2));
877
  dx=OC_MV_X(_mv);
878
  dy=OC_MV_Y(_mv);
879
  /*These two variables are either 0 if all the fractional bits are zero or -1
880
     if any of them are non-zero.*/
881
  xfrac=OC_SIGNMASK(-(dx&(xprec|1)));
882
  yfrac=OC_SIGNMASK(-(dy&(yprec|1)));
883
  offs=(dx>>xprec)+(dy>>yprec)*ystride;
884
  if(xfrac||yfrac){
885
    int xmask;
886
    int ymask;
887
    xmask=OC_SIGNMASK(dx);
888
    ymask=OC_SIGNMASK(dy);
889
    yfrac&=ystride;
890
    _offsets[0]=offs-(xfrac&xmask)+(yfrac&ymask);
891
    _offsets[1]=offs-(xfrac&~xmask)+(yfrac&~ymask);
892
    return 2;
893
  }
894
  else{
895
    _offsets[0]=offs;
896
    return 1;
897
  }
898
#else
899
  /*Using tables simplifies the code, and there's enough arithmetic to hide the
900
     latencies of the memory references.*/
901
9.52M
  static const signed char OC_MVMAP[2][64]={
902
9.52M
    {
903
9.52M
          -15,-15,-14,-14,-13,-13,-12,-12,-11,-11,-10,-10, -9, -9, -8,
904
9.52M
       -8, -7, -7, -6, -6, -5, -5, -4, -4, -3, -3, -2, -2, -1, -1,  0,
905
9.52M
        0,  0,  1,  1,  2,  2,  3,  3,  4,  4,  5,  5,  6,  6,  7,  7,
906
9.52M
        8,  8,  9,  9, 10, 10, 11, 11, 12, 12, 13, 13, 14, 14, 15, 15
907
9.52M
    },
908
9.52M
    {
909
9.52M
           -7, -7, -7, -7, -6, -6, -6, -6, -5, -5, -5, -5, -4, -4, -4,
910
9.52M
       -4, -3, -3, -3, -3, -2, -2, -2, -2, -1, -1, -1, -1,  0,  0,  0,
911
9.52M
        0,  0,  0,  0,  1,  1,  1,  1,  2,  2,  2,  2,  3,  3,  3,  3,
912
9.52M
        4,  4,  4,  4,  5,  5,  5,  5,  6,  6,  6,  6,  7,  7,  7,  7
913
9.52M
    }
914
9.52M
  };
915
9.52M
  static const signed char OC_MVMAP2[2][64]={
916
9.52M
    {
917
9.52M
        -1, 0,-1,  0,-1, 0,-1,  0,-1, 0,-1,  0,-1, 0,-1,
918
9.52M
      0,-1, 0,-1,  0,-1, 0,-1,  0,-1, 0,-1,  0,-1, 0,-1,
919
9.52M
      0, 1, 0, 1,  0, 1, 0, 1,  0, 1, 0, 1,  0, 1, 0, 1,
920
9.52M
      0, 1, 0, 1,  0, 1, 0, 1,  0, 1, 0, 1,  0, 1, 0, 1
921
9.52M
    },
922
9.52M
    {
923
9.52M
        -1,-1,-1,  0,-1,-1,-1,  0,-1,-1,-1,  0,-1,-1,-1,
924
9.52M
      0,-1,-1,-1,  0,-1,-1,-1,  0,-1,-1,-1,  0,-1,-1,-1,
925
9.52M
      0, 1, 1, 1,  0, 1, 1, 1,  0, 1, 1, 1,  0, 1, 1, 1,
926
9.52M
      0, 1, 1, 1,  0, 1, 1, 1,  0, 1, 1, 1,  0, 1, 1, 1
927
9.52M
    }
928
9.52M
  };
929
9.52M
  int ystride;
930
9.52M
  int qpx;
931
9.52M
  int qpy;
932
9.52M
  int mx;
933
9.52M
  int my;
934
9.52M
  int mx2;
935
9.52M
  int my2;
936
9.52M
  int offs;
937
9.52M
  int dx;
938
9.52M
  int dy;
939
9.52M
  ystride=_state->ref_ystride[_pli];
940
9.52M
  qpy=_pli!=0&&!(_state->info.pixel_fmt&2);
941
9.52M
  dx=OC_MV_X(_mv);
942
9.52M
  dy=OC_MV_Y(_mv);
943
9.52M
  my=OC_MVMAP[qpy][dy+31];
944
9.52M
  my2=OC_MVMAP2[qpy][dy+31];
945
9.52M
  qpx=_pli!=0&&!(_state->info.pixel_fmt&1);
946
9.52M
  mx=OC_MVMAP[qpx][dx+31];
947
9.52M
  mx2=OC_MVMAP2[qpx][dx+31];
948
9.52M
  offs=my*ystride+mx;
949
9.52M
  if(mx2||my2){
950
2.92M
    _offsets[1]=offs+my2*ystride+mx2;
951
2.92M
    _offsets[0]=offs;
952
2.92M
    return 2;
953
2.92M
  }
954
6.60M
  _offsets[0]=offs;
955
6.60M
  return 1;
956
9.52M
#endif
957
9.52M
}
958
959
void oc_state_frag_recon_c(const oc_theora_state *_state,ptrdiff_t _fragi,
960
0
 int _pli,ogg_int16_t _dct_coeffs[128],int _last_zzi,ogg_uint16_t _dc_quant){
961
0
  unsigned char *dst;
962
0
  ptrdiff_t      frag_buf_off;
963
0
  int            ystride;
964
0
  int            refi;
965
  /*Apply the inverse transform.*/
966
  /*Special case only having a DC component.*/
967
0
  if(_last_zzi<2){
968
0
    ogg_int16_t p;
969
0
    int         ci;
970
    /*We round this dequant product (and not any of the others) because there's
971
       no iDCT rounding.*/
972
0
    p=(ogg_int16_t)(_dct_coeffs[0]*(ogg_int32_t)_dc_quant+15>>5);
973
    /*LOOP VECTORIZES.*/
974
0
    for(ci=0;ci<64;ci++)_dct_coeffs[64+ci]=p;
975
0
  }
976
0
  else{
977
    /*First, dequantize the DC coefficient.*/
978
0
    _dct_coeffs[0]=(ogg_int16_t)(_dct_coeffs[0]*(int)_dc_quant);
979
0
    oc_idct8x8(_state,_dct_coeffs+64,_dct_coeffs,_last_zzi);
980
0
  }
981
  /*Fill in the target buffer.*/
982
0
  frag_buf_off=_state->frag_buf_offs[_fragi];
983
0
  refi=_state->frags[_fragi].refi;
984
0
  ystride=_state->ref_ystride[_pli];
985
0
  dst=_state->ref_frame_data[OC_FRAME_SELF]+frag_buf_off;
986
0
  if(refi==OC_FRAME_SELF)oc_frag_recon_intra(_state,dst,ystride,_dct_coeffs+64);
987
0
  else{
988
0
    const unsigned char *ref;
989
0
    int                  mvoffsets[2];
990
0
    ref=_state->ref_frame_data[refi]+frag_buf_off;
991
0
    if(oc_state_get_mv_offsets(_state,mvoffsets,_pli,
992
0
     _state->frag_mvs[_fragi])>1){
993
0
      oc_frag_recon_inter2(_state,
994
0
       dst,ref+mvoffsets[0],ref+mvoffsets[1],ystride,_dct_coeffs+64);
995
0
    }
996
0
    else{
997
0
      oc_frag_recon_inter(_state,dst,ref+mvoffsets[0],ystride,_dct_coeffs+64);
998
0
    }
999
0
  }
1000
0
}
1001
1002
0
static void loop_filter_h(unsigned char *_pix,int _ystride,signed char *_bv){
1003
0
  int y;
1004
0
  _pix-=2;
1005
0
  for(y=0;y<8;y++){
1006
0
    int f;
1007
0
    f=_pix[0]-_pix[3]+3*(_pix[2]-_pix[1]);
1008
    /*The _bv array is used to compute the function
1009
      f=OC_CLAMPI(OC_MINI(-_2flimit-f,0),f,OC_MAXI(_2flimit-f,0));
1010
      where _2flimit=_state->loop_filter_limits[_state->qis[0]]<<1;*/
1011
0
    f=*(_bv+(f+4>>3));
1012
0
    _pix[1]=OC_CLAMP255(_pix[1]+f);
1013
0
    _pix[2]=OC_CLAMP255(_pix[2]-f);
1014
0
    _pix+=_ystride;
1015
0
  }
1016
0
}
1017
1018
0
static void loop_filter_v(unsigned char *_pix,int _ystride,signed char *_bv){
1019
0
  int x;
1020
0
  _pix-=_ystride*2;
1021
0
  for(x=0;x<8;x++){
1022
0
    int f;
1023
0
    f=_pix[x]-_pix[_ystride*3+x]+3*(_pix[_ystride*2+x]-_pix[_ystride+x]);
1024
    /*The _bv array is used to compute the function
1025
      f=OC_CLAMPI(OC_MINI(-_2flimit-f,0),f,OC_MAXI(_2flimit-f,0));
1026
      where _2flimit=_state->loop_filter_limits[_state->qis[0]]<<1;*/
1027
0
    f=*(_bv+(f+4>>3));
1028
0
    _pix[_ystride+x]=OC_CLAMP255(_pix[_ystride+x]+f);
1029
0
    _pix[_ystride*2+x]=OC_CLAMP255(_pix[_ystride*2+x]-f);
1030
0
  }
1031
0
}
1032
1033
/*Initialize the bounding values array used by the loop filter.
1034
  _bv: Storage for the array.
1035
  _flimit: The filter limit as defined in Section 7.10 of the spec.*/
1036
0
void oc_loop_filter_init_c(signed char _bv[256],int _flimit){
1037
0
  int i;
1038
0
  memset(_bv,0,sizeof(_bv[0])*256);
1039
0
  for(i=0;i<_flimit;i++){
1040
0
    if(127-i-_flimit>=0)_bv[127-i-_flimit]=(signed char)(i-_flimit);
1041
0
    _bv[127-i]=(signed char)(-i);
1042
0
    _bv[127+i]=(signed char)(i);
1043
0
    if(127+i+_flimit<256)_bv[127+i+_flimit]=(signed char)(_flimit-i);
1044
0
  }
1045
0
}
1046
1047
/*Apply the loop filter to a given set of fragment rows in the given plane.
1048
  The filter may be run on the bottom edge, affecting pixels in the next row of
1049
   fragments, so this row also needs to be available.
1050
  _bv:        The bounding values array.
1051
  _refi:      The index of the frame buffer to filter.
1052
  _pli:       The color plane to filter.
1053
  _fragy0:    The Y coordinate of the first fragment row to filter.
1054
  _fragy_end: The Y coordinate of the fragment row to stop filtering at.*/
1055
void oc_state_loop_filter_frag_rows_c(const oc_theora_state *_state,
1056
0
 signed char *_bv,int _refi,int _pli,int _fragy0,int _fragy_end){
1057
0
  const oc_fragment_plane *fplane;
1058
0
  const oc_fragment       *frags;
1059
0
  const ptrdiff_t         *frag_buf_offs;
1060
0
  unsigned char           *ref_frame_data;
1061
0
  ptrdiff_t                fragi_top;
1062
0
  ptrdiff_t                fragi_bot;
1063
0
  ptrdiff_t                fragi0;
1064
0
  ptrdiff_t                fragi0_end;
1065
0
  int                      ystride;
1066
0
  int                      nhfrags;
1067
0
  _bv+=127;
1068
0
  fplane=_state->fplanes+_pli;
1069
0
  nhfrags=fplane->nhfrags;
1070
0
  fragi_top=fplane->froffset;
1071
0
  fragi_bot=fragi_top+fplane->nfrags;
1072
0
  fragi0=fragi_top+_fragy0*(ptrdiff_t)nhfrags;
1073
0
  fragi0_end=fragi_top+_fragy_end*(ptrdiff_t)nhfrags;
1074
0
  ystride=_state->ref_ystride[_pli];
1075
0
  frags=_state->frags;
1076
0
  frag_buf_offs=_state->frag_buf_offs;
1077
0
  ref_frame_data=_state->ref_frame_data[_refi];
1078
  /*The following loops are constructed somewhat non-intuitively on purpose.
1079
    The main idea is: if a block boundary has at least one coded fragment on
1080
     it, the filter is applied to it.
1081
    However, the order that the filters are applied in matters, and VP3 chose
1082
     the somewhat strange ordering used below.*/
1083
0
  while(fragi0<fragi0_end){
1084
0
    ptrdiff_t fragi;
1085
0
    ptrdiff_t fragi_end;
1086
0
    fragi=fragi0;
1087
0
    fragi_end=fragi+nhfrags;
1088
0
    while(fragi<fragi_end){
1089
0
      if(frags[fragi].coded){
1090
0
        unsigned char *ref;
1091
0
        ref=ref_frame_data+frag_buf_offs[fragi];
1092
0
        if(fragi>fragi0)loop_filter_h(ref,ystride,_bv);
1093
0
        if(fragi0>fragi_top)loop_filter_v(ref,ystride,_bv);
1094
0
        if(fragi+1<fragi_end&&!frags[fragi+1].coded){
1095
0
          loop_filter_h(ref+8,ystride,_bv);
1096
0
        }
1097
0
        if(fragi+nhfrags<fragi_bot&&!frags[fragi+nhfrags].coded){
1098
0
          loop_filter_v(ref+(ystride<<3),ystride,_bv);
1099
0
        }
1100
0
      }
1101
0
      fragi++;
1102
0
    }
1103
0
    fragi0+=nhfrags;
1104
0
  }
1105
0
}
1106
1107
#if defined(OC_DUMP_IMAGES)
1108
int oc_state_dump_frame(const oc_theora_state *_state,int _frame,
1109
 const char *_suf){
1110
  /*Dump a PNG of the reconstructed image.*/
1111
  png_structp    png;
1112
  png_infop      info;
1113
  png_bytep     *image;
1114
  FILE          *fp;
1115
  char           fname[16];
1116
  unsigned char *y_row;
1117
  unsigned char *u_row;
1118
  unsigned char *v_row;
1119
  unsigned char *y;
1120
  unsigned char *u;
1121
  unsigned char *v;
1122
  ogg_int64_t    iframe;
1123
  ogg_int64_t    pframe;
1124
  int            y_stride;
1125
  int            u_stride;
1126
  int            v_stride;
1127
  int            framei;
1128
  int            width;
1129
  int            height;
1130
  int            imgi;
1131
  int            imgj;
1132
  width=_state->info.frame_width;
1133
  height=_state->info.frame_height;
1134
  iframe=_state->granpos>>_state->info.keyframe_granule_shift;
1135
  pframe=_state->granpos-(iframe<<_state->info.keyframe_granule_shift);
1136
  sprintf(fname,"%08i%s.png",(int)(iframe+pframe),_suf);
1137
  fp=fopen(fname,"wb");
1138
  if(fp==NULL)return TH_EFAULT;
1139
  image=(png_bytep *)oc_malloc_2d(height,6*width,sizeof(**image));
1140
  if(image==NULL){
1141
    fclose(fp);
1142
    return TH_EFAULT;
1143
  }
1144
  png=png_create_write_struct(PNG_LIBPNG_VER_STRING,NULL,NULL,NULL);
1145
  if(png==NULL){
1146
    oc_free_2d(image);
1147
    fclose(fp);
1148
    return TH_EFAULT;
1149
  }
1150
  info=png_create_info_struct(png);
1151
  if(info==NULL){
1152
    png_destroy_write_struct(&png,NULL);
1153
    oc_free_2d(image);
1154
    fclose(fp);
1155
    return TH_EFAULT;
1156
  }
1157
  if(setjmp(png_jmpbuf(png))){
1158
    png_destroy_write_struct(&png,&info);
1159
    oc_free_2d(image);
1160
    fclose(fp);
1161
    return TH_EFAULT;
1162
  }
1163
  framei=_state->ref_frame_idx[_frame];
1164
  y_row=_state->ref_frame_bufs[framei][0].data;
1165
  u_row=_state->ref_frame_bufs[framei][1].data;
1166
  v_row=_state->ref_frame_bufs[framei][2].data;
1167
  y_stride=_state->ref_frame_bufs[framei][0].stride;
1168
  u_stride=_state->ref_frame_bufs[framei][1].stride;
1169
  v_stride=_state->ref_frame_bufs[framei][2].stride;
1170
  /*Chroma up-sampling is just done with a box filter.
1171
    This is very likely what will actually be used in practice on a real
1172
     display, and also removes one more layer to search in for the source of
1173
     artifacts.
1174
    As an added bonus, it's dead simple.*/
1175
  for(imgi=height;imgi-->0;){
1176
    int dc;
1177
    y=y_row;
1178
    u=u_row;
1179
    v=v_row;
1180
    for(imgj=0;imgj<6*width;){
1181
      float    yval;
1182
      float    uval;
1183
      float    vval;
1184
      unsigned rval;
1185
      unsigned gval;
1186
      unsigned bval;
1187
      /*This is intentionally slow and very accurate.*/
1188
      yval=(*y-16)*(1.0F/219);
1189
      uval=(*u-128)*(2*(1-0.114F)/224);
1190
      vval=(*v-128)*(2*(1-0.299F)/224);
1191
      rval=OC_CLAMPI(0,(int)(65535*(yval+vval)+0.5F),65535);
1192
      gval=OC_CLAMPI(0,(int)(65535*(
1193
       yval-uval*(0.114F/0.587F)-vval*(0.299F/0.587F))+0.5F),65535);
1194
      bval=OC_CLAMPI(0,(int)(65535*(yval+uval)+0.5F),65535);
1195
      image[imgi][imgj++]=(unsigned char)(rval>>8);
1196
      image[imgi][imgj++]=(unsigned char)(rval&0xFF);
1197
      image[imgi][imgj++]=(unsigned char)(gval>>8);
1198
      image[imgi][imgj++]=(unsigned char)(gval&0xFF);
1199
      image[imgi][imgj++]=(unsigned char)(bval>>8);
1200
      image[imgi][imgj++]=(unsigned char)(bval&0xFF);
1201
      dc=(y-y_row&1)|(_state->info.pixel_fmt&1);
1202
      y++;
1203
      u+=dc;
1204
      v+=dc;
1205
    }
1206
    dc=-((height-1-imgi&1)|_state->info.pixel_fmt>>1);
1207
    y_row+=y_stride;
1208
    u_row+=dc&u_stride;
1209
    v_row+=dc&v_stride;
1210
  }
1211
  png_init_io(png,fp);
1212
  png_set_compression_level(png,Z_BEST_COMPRESSION);
1213
  png_set_IHDR(png,info,width,height,16,PNG_COLOR_TYPE_RGB,
1214
   PNG_INTERLACE_NONE,PNG_COMPRESSION_TYPE_DEFAULT,PNG_FILTER_TYPE_DEFAULT);
1215
  switch(_state->info.colorspace){
1216
    case TH_CS_ITU_REC_470M:{
1217
      png_set_gAMA(png,info,2.2);
1218
      png_set_cHRM_fixed(png,info,31006,31616,
1219
       67000,32000,21000,71000,14000,8000);
1220
    }break;
1221
    case TH_CS_ITU_REC_470BG:{
1222
      png_set_gAMA(png,info,2.67);
1223
      png_set_cHRM_fixed(png,info,31271,32902,
1224
       64000,33000,29000,60000,15000,6000);
1225
    }break;
1226
    default:break;
1227
  }
1228
  png_set_pHYs(png,info,_state->info.aspect_numerator,
1229
   _state->info.aspect_denominator,0);
1230
  png_set_rows(png,info,image);
1231
  png_write_png(png,info,PNG_TRANSFORM_IDENTITY,NULL);
1232
  png_write_end(png,info);
1233
  png_destroy_write_struct(&png,&info);
1234
  oc_free_2d(image);
1235
  fclose(fp);
1236
  return 0;
1237
}
1238
#endif
1239
1240
1241
1242
42.1k
ogg_int64_t th_granule_frame(void *_encdec,ogg_int64_t _granpos){
1243
42.1k
  oc_theora_state *state;
1244
42.1k
  state=(oc_theora_state *)_encdec;
1245
42.1k
  if(_granpos>=0){
1246
42.1k
    ogg_int64_t iframe;
1247
42.1k
    ogg_int64_t pframe;
1248
42.1k
    iframe=_granpos>>state->info.keyframe_granule_shift;
1249
42.1k
    pframe=_granpos-(iframe<<state->info.keyframe_granule_shift);
1250
    /*3.2.0 streams store the frame index in the granule position.
1251
      3.2.1 and later store the frame count.
1252
      We return the index, so adjust the value if we have a 3.2.1 or later
1253
       stream.*/
1254
42.1k
    return iframe+pframe-TH_VERSION_CHECK(&state->info,3,2,1);
1255
42.1k
  }
1256
0
  return -1;
1257
42.1k
}
1258
1259
0
double th_granule_time(void *_encdec,ogg_int64_t _granpos){
1260
0
  oc_theora_state *state;
1261
0
  state=(oc_theora_state *)_encdec;
1262
0
  if(_granpos>=0){
1263
0
    return (th_granule_frame(_encdec, _granpos)+1)*(
1264
0
     (double)state->info.fps_denominator/state->info.fps_numerator);
1265
0
  }
1266
0
  return -1;
1267
0
}