~paparazzi-uav/paparazzi/v5.0-manual

« back to all changes in this revision

Viewing changes to sw/ext/opencv_bebop/opencv/3rdparty/libwebp/dec/frame.c

  • Committer: Paparazzi buildbot
  • Date: 2016-05-18 15:00:29 UTC
  • Revision ID: felix.ruess+docbot@gmail.com-20160518150029-e8lgzi5kvb4p7un9
Manual import commit 4b8bbb730080dac23cf816b98908dacfabe2a8ec from v5.0 branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2010 Google Inc. All Rights Reserved.
 
2
//
 
3
// Use of this source code is governed by a BSD-style license
 
4
// that can be found in the COPYING file in the root of the source
 
5
// tree. An additional intellectual property rights grant can be found
 
6
// in the file PATENTS. All contributing project authors may
 
7
// be found in the AUTHORS file in the root of the source tree.
 
8
// -----------------------------------------------------------------------------
 
9
//
 
10
// Frame-reconstruction function. Memory allocation.
 
11
//
 
12
// Author: Skal (pascal.massimino@gmail.com)
 
13
 
 
14
#include <stdlib.h>
 
15
#include "./vp8i.h"
 
16
#include "../utils/utils.h"
 
17
 
 
18
#if defined(__cplusplus) || defined(c_plusplus)
 
19
extern "C" {
 
20
#endif
 
21
 
 
22
#define ALIGN_MASK (32 - 1)
 
23
 
 
24
//------------------------------------------------------------------------------
 
25
// Filtering
 
26
 
 
27
// kFilterExtraRows[] = How many extra lines are needed on the MB boundary
 
28
// for caching, given a filtering level.
 
29
// Simple filter:  up to 2 luma samples are read and 1 is written.
 
30
// Complex filter: up to 4 luma samples are read and 3 are written. Same for
 
31
//                 U/V, so it's 8 samples total (because of the 2x upsampling).
 
32
static const uint8_t kFilterExtraRows[3] = { 0, 2, 8 };
 
33
 
 
34
static WEBP_INLINE int hev_thresh_from_level(int level, int keyframe) {
 
35
  if (keyframe) {
 
36
    return (level >= 40) ? 2 : (level >= 15) ? 1 : 0;
 
37
  } else {
 
38
    return (level >= 40) ? 3 : (level >= 20) ? 2 : (level >= 15) ? 1 : 0;
 
39
  }
 
40
}
 
41
 
 
42
static void DoFilter(const VP8Decoder* const dec, int mb_x, int mb_y) {
 
43
  const VP8ThreadContext* const ctx = &dec->thread_ctx_;
 
44
  const int y_bps = dec->cache_y_stride_;
 
45
  VP8FInfo* const f_info = ctx->f_info_ + mb_x;
 
46
  uint8_t* const y_dst = dec->cache_y_ + ctx->id_ * 16 * y_bps + mb_x * 16;
 
47
  const int level = f_info->f_level_;
 
48
  const int ilevel = f_info->f_ilevel_;
 
49
  const int limit = 2 * level + ilevel;
 
50
  if (level == 0) {
 
51
    return;
 
52
  }
 
53
  if (dec->filter_type_ == 1) {   // simple
 
54
    if (mb_x > 0) {
 
55
      VP8SimpleHFilter16(y_dst, y_bps, limit + 4);
 
56
    }
 
57
    if (f_info->f_inner_) {
 
58
      VP8SimpleHFilter16i(y_dst, y_bps, limit);
 
59
    }
 
60
    if (mb_y > 0) {
 
61
      VP8SimpleVFilter16(y_dst, y_bps, limit + 4);
 
62
    }
 
63
    if (f_info->f_inner_) {
 
64
      VP8SimpleVFilter16i(y_dst, y_bps, limit);
 
65
    }
 
66
  } else {    // complex
 
67
    const int uv_bps = dec->cache_uv_stride_;
 
68
    uint8_t* const u_dst = dec->cache_u_ + ctx->id_ * 8 * uv_bps + mb_x * 8;
 
69
    uint8_t* const v_dst = dec->cache_v_ + ctx->id_ * 8 * uv_bps + mb_x * 8;
 
70
    const int hev_thresh =
 
71
        hev_thresh_from_level(level, dec->frm_hdr_.key_frame_);
 
72
    if (mb_x > 0) {
 
73
      VP8HFilter16(y_dst, y_bps, limit + 4, ilevel, hev_thresh);
 
74
      VP8HFilter8(u_dst, v_dst, uv_bps, limit + 4, ilevel, hev_thresh);
 
75
    }
 
76
    if (f_info->f_inner_) {
 
77
      VP8HFilter16i(y_dst, y_bps, limit, ilevel, hev_thresh);
 
78
      VP8HFilter8i(u_dst, v_dst, uv_bps, limit, ilevel, hev_thresh);
 
79
    }
 
80
    if (mb_y > 0) {
 
81
      VP8VFilter16(y_dst, y_bps, limit + 4, ilevel, hev_thresh);
 
82
      VP8VFilter8(u_dst, v_dst, uv_bps, limit + 4, ilevel, hev_thresh);
 
83
    }
 
84
    if (f_info->f_inner_) {
 
85
      VP8VFilter16i(y_dst, y_bps, limit, ilevel, hev_thresh);
 
86
      VP8VFilter8i(u_dst, v_dst, uv_bps, limit, ilevel, hev_thresh);
 
87
    }
 
88
  }
 
89
}
 
90
 
 
91
// Filter the decoded macroblock row (if needed)
 
92
static void FilterRow(const VP8Decoder* const dec) {
 
93
  int mb_x;
 
94
  const int mb_y = dec->thread_ctx_.mb_y_;
 
95
  assert(dec->thread_ctx_.filter_row_);
 
96
  for (mb_x = dec->tl_mb_x_; mb_x < dec->br_mb_x_; ++mb_x) {
 
97
    DoFilter(dec, mb_x, mb_y);
 
98
  }
 
99
}
 
100
 
 
101
//------------------------------------------------------------------------------
 
102
// Precompute the filtering strength for each segment and each i4x4/i16x16 mode.
 
103
 
 
104
static void PrecomputeFilterStrengths(VP8Decoder* const dec) {
 
105
  if (dec->filter_type_ > 0) {
 
106
    int s;
 
107
    const VP8FilterHeader* const hdr = &dec->filter_hdr_;
 
108
    for (s = 0; s < NUM_MB_SEGMENTS; ++s) {
 
109
      int i4x4;
 
110
      // First, compute the initial level
 
111
      int base_level;
 
112
      if (dec->segment_hdr_.use_segment_) {
 
113
        base_level = dec->segment_hdr_.filter_strength_[s];
 
114
        if (!dec->segment_hdr_.absolute_delta_) {
 
115
          base_level += hdr->level_;
 
116
        }
 
117
      } else {
 
118
        base_level = hdr->level_;
 
119
      }
 
120
      for (i4x4 = 0; i4x4 <= 1; ++i4x4) {
 
121
        VP8FInfo* const info = &dec->fstrengths_[s][i4x4];
 
122
        int level = base_level;
 
123
        if (hdr->use_lf_delta_) {
 
124
          // TODO(skal): only CURRENT is handled for now.
 
125
          level += hdr->ref_lf_delta_[0];
 
126
          if (i4x4) {
 
127
            level += hdr->mode_lf_delta_[0];
 
128
          }
 
129
        }
 
130
        level = (level < 0) ? 0 : (level > 63) ? 63 : level;
 
131
        info->f_level_ = level;
 
132
 
 
133
        if (hdr->sharpness_ > 0) {
 
134
          if (hdr->sharpness_ > 4) {
 
135
            level >>= 2;
 
136
          } else {
 
137
            level >>= 1;
 
138
          }
 
139
          if (level > 9 - hdr->sharpness_) {
 
140
            level = 9 - hdr->sharpness_;
 
141
          }
 
142
        }
 
143
        info->f_ilevel_ = (level < 1) ? 1 : level;
 
144
        info->f_inner_ = 0;
 
145
      }
 
146
    }
 
147
  }
 
148
}
 
149
 
 
150
//------------------------------------------------------------------------------
 
151
// This function is called after a row of macroblocks is finished decoding.
 
152
// It also takes into account the following restrictions:
 
153
//  * In case of in-loop filtering, we must hold off sending some of the bottom
 
154
//    pixels as they are yet unfiltered. They will be when the next macroblock
 
155
//    row is decoded. Meanwhile, we must preserve them by rotating them in the
 
156
//    cache area. This doesn't hold for the very bottom row of the uncropped
 
157
//    picture of course.
 
158
//  * we must clip the remaining pixels against the cropping area. The VP8Io
 
159
//    struct must have the following fields set correctly before calling put():
 
160
 
 
161
#define MACROBLOCK_VPOS(mb_y)  ((mb_y) * 16)    // vertical position of a MB
 
162
 
 
163
// Finalize and transmit a complete row. Return false in case of user-abort.
 
164
static int FinishRow(VP8Decoder* const dec, VP8Io* const io) {
 
165
  int ok = 1;
 
166
  const VP8ThreadContext* const ctx = &dec->thread_ctx_;
 
167
  const int extra_y_rows = kFilterExtraRows[dec->filter_type_];
 
168
  const int ysize = extra_y_rows * dec->cache_y_stride_;
 
169
  const int uvsize = (extra_y_rows / 2) * dec->cache_uv_stride_;
 
170
  const int y_offset = ctx->id_ * 16 * dec->cache_y_stride_;
 
171
  const int uv_offset = ctx->id_ * 8 * dec->cache_uv_stride_;
 
172
  uint8_t* const ydst = dec->cache_y_ - ysize + y_offset;
 
173
  uint8_t* const udst = dec->cache_u_ - uvsize + uv_offset;
 
174
  uint8_t* const vdst = dec->cache_v_ - uvsize + uv_offset;
 
175
  const int first_row = (ctx->mb_y_ == 0);
 
176
  const int last_row = (ctx->mb_y_ >= dec->br_mb_y_ - 1);
 
177
  int y_start = MACROBLOCK_VPOS(ctx->mb_y_);
 
178
  int y_end = MACROBLOCK_VPOS(ctx->mb_y_ + 1);
 
179
 
 
180
  if (ctx->filter_row_) {
 
181
    FilterRow(dec);
 
182
  }
 
183
 
 
184
  if (io->put) {
 
185
    if (!first_row) {
 
186
      y_start -= extra_y_rows;
 
187
      io->y = ydst;
 
188
      io->u = udst;
 
189
      io->v = vdst;
 
190
    } else {
 
191
      io->y = dec->cache_y_ + y_offset;
 
192
      io->u = dec->cache_u_ + uv_offset;
 
193
      io->v = dec->cache_v_ + uv_offset;
 
194
    }
 
195
 
 
196
    if (!last_row) {
 
197
      y_end -= extra_y_rows;
 
198
    }
 
199
    if (y_end > io->crop_bottom) {
 
200
      y_end = io->crop_bottom;    // make sure we don't overflow on last row.
 
201
    }
 
202
    io->a = NULL;
 
203
    if (dec->alpha_data_ != NULL && y_start < y_end) {
 
204
      // TODO(skal): several things to correct here:
 
205
      // * testing presence of alpha with dec->alpha_data_ is not a good idea
 
206
      // * we're actually decompressing the full plane only once. It should be
 
207
      //   more obvious from signature.
 
208
      // * we could free alpha_data_ right after this call, but we don't own.
 
209
      io->a = VP8DecompressAlphaRows(dec, y_start, y_end - y_start);
 
210
      if (io->a == NULL) {
 
211
        return VP8SetError(dec, VP8_STATUS_BITSTREAM_ERROR,
 
212
                           "Could not decode alpha data.");
 
213
      }
 
214
    }
 
215
    if (y_start < io->crop_top) {
 
216
      const int delta_y = io->crop_top - y_start;
 
217
      y_start = io->crop_top;
 
218
      assert(!(delta_y & 1));
 
219
      io->y += dec->cache_y_stride_ * delta_y;
 
220
      io->u += dec->cache_uv_stride_ * (delta_y >> 1);
 
221
      io->v += dec->cache_uv_stride_ * (delta_y >> 1);
 
222
      if (io->a != NULL) {
 
223
        io->a += io->width * delta_y;
 
224
      }
 
225
    }
 
226
    if (y_start < y_end) {
 
227
      io->y += io->crop_left;
 
228
      io->u += io->crop_left >> 1;
 
229
      io->v += io->crop_left >> 1;
 
230
      if (io->a != NULL) {
 
231
        io->a += io->crop_left;
 
232
      }
 
233
      io->mb_y = y_start - io->crop_top;
 
234
      io->mb_w = io->crop_right - io->crop_left;
 
235
      io->mb_h = y_end - y_start;
 
236
      ok = io->put(io);
 
237
    }
 
238
  }
 
239
  // rotate top samples if needed
 
240
  if (ctx->id_ + 1 == dec->num_caches_) {
 
241
    if (!last_row) {
 
242
      memcpy(dec->cache_y_ - ysize, ydst + 16 * dec->cache_y_stride_, ysize);
 
243
      memcpy(dec->cache_u_ - uvsize, udst + 8 * dec->cache_uv_stride_, uvsize);
 
244
      memcpy(dec->cache_v_ - uvsize, vdst + 8 * dec->cache_uv_stride_, uvsize);
 
245
    }
 
246
  }
 
247
 
 
248
  return ok;
 
249
}
 
250
 
 
251
#undef MACROBLOCK_VPOS
 
252
 
 
253
//------------------------------------------------------------------------------
 
254
 
 
255
int VP8ProcessRow(VP8Decoder* const dec, VP8Io* const io) {
 
256
  int ok = 1;
 
257
  VP8ThreadContext* const ctx = &dec->thread_ctx_;
 
258
  if (!dec->use_threads_) {
 
259
    // ctx->id_ and ctx->f_info_ are already set
 
260
    ctx->mb_y_ = dec->mb_y_;
 
261
    ctx->filter_row_ = dec->filter_row_;
 
262
    ok = FinishRow(dec, io);
 
263
  } else {
 
264
    WebPWorker* const worker = &dec->worker_;
 
265
    // Finish previous job *before* updating context
 
266
    ok &= WebPWorkerSync(worker);
 
267
    assert(worker->status_ == OK);
 
268
    if (ok) {   // spawn a new deblocking/output job
 
269
      ctx->io_ = *io;
 
270
      ctx->id_ = dec->cache_id_;
 
271
      ctx->mb_y_ = dec->mb_y_;
 
272
      ctx->filter_row_ = dec->filter_row_;
 
273
      if (ctx->filter_row_) {    // just swap filter info
 
274
        VP8FInfo* const tmp = ctx->f_info_;
 
275
        ctx->f_info_ = dec->f_info_;
 
276
        dec->f_info_ = tmp;
 
277
      }
 
278
      WebPWorkerLaunch(worker);
 
279
      if (++dec->cache_id_ == dec->num_caches_) {
 
280
        dec->cache_id_ = 0;
 
281
      }
 
282
    }
 
283
  }
 
284
  return ok;
 
285
}
 
286
 
 
287
//------------------------------------------------------------------------------
 
288
// Finish setting up the decoding parameter once user's setup() is called.
 
289
 
 
290
VP8StatusCode VP8EnterCritical(VP8Decoder* const dec, VP8Io* const io) {
 
291
  // Call setup() first. This may trigger additional decoding features on 'io'.
 
292
  // Note: Afterward, we must call teardown() not matter what.
 
293
  if (io->setup && !io->setup(io)) {
 
294
    VP8SetError(dec, VP8_STATUS_USER_ABORT, "Frame setup failed");
 
295
    return dec->status_;
 
296
  }
 
297
 
 
298
  // Disable filtering per user request
 
299
  if (io->bypass_filtering) {
 
300
    dec->filter_type_ = 0;
 
301
  }
 
302
  // TODO(skal): filter type / strength / sharpness forcing
 
303
 
 
304
  // Define the area where we can skip in-loop filtering, in case of cropping.
 
305
  //
 
306
  // 'Simple' filter reads two luma samples outside of the macroblock and
 
307
  // and filters one. It doesn't filter the chroma samples. Hence, we can
 
308
  // avoid doing the in-loop filtering before crop_top/crop_left position.
 
309
  // For the 'Complex' filter, 3 samples are read and up to 3 are filtered.
 
310
  // Means: there's a dependency chain that goes all the way up to the
 
311
  // top-left corner of the picture (MB #0). We must filter all the previous
 
312
  // macroblocks.
 
313
  // TODO(skal): add an 'approximate_decoding' option, that won't produce
 
314
  // a 1:1 bit-exactness for complex filtering?
 
315
  {
 
316
    const int extra_pixels = kFilterExtraRows[dec->filter_type_];
 
317
    if (dec->filter_type_ == 2) {
 
318
      // For complex filter, we need to preserve the dependency chain.
 
319
      dec->tl_mb_x_ = 0;
 
320
      dec->tl_mb_y_ = 0;
 
321
    } else {
 
322
      // For simple filter, we can filter only the cropped region.
 
323
      // We include 'extra_pixels' on the other side of the boundary, since
 
324
      // vertical or horizontal filtering of the previous macroblock can
 
325
      // modify some abutting pixels.
 
326
      dec->tl_mb_x_ = (io->crop_left - extra_pixels) >> 4;
 
327
      dec->tl_mb_y_ = (io->crop_top - extra_pixels) >> 4;
 
328
      if (dec->tl_mb_x_ < 0) dec->tl_mb_x_ = 0;
 
329
      if (dec->tl_mb_y_ < 0) dec->tl_mb_y_ = 0;
 
330
    }
 
331
    // We need some 'extra' pixels on the right/bottom.
 
332
    dec->br_mb_y_ = (io->crop_bottom + 15 + extra_pixels) >> 4;
 
333
    dec->br_mb_x_ = (io->crop_right + 15 + extra_pixels) >> 4;
 
334
    if (dec->br_mb_x_ > dec->mb_w_) {
 
335
      dec->br_mb_x_ = dec->mb_w_;
 
336
    }
 
337
    if (dec->br_mb_y_ > dec->mb_h_) {
 
338
      dec->br_mb_y_ = dec->mb_h_;
 
339
    }
 
340
  }
 
341
  PrecomputeFilterStrengths(dec);
 
342
  return VP8_STATUS_OK;
 
343
}
 
344
 
 
345
int VP8ExitCritical(VP8Decoder* const dec, VP8Io* const io) {
 
346
  int ok = 1;
 
347
  if (dec->use_threads_) {
 
348
    ok = WebPWorkerSync(&dec->worker_);
 
349
  }
 
350
 
 
351
  if (io->teardown) {
 
352
    io->teardown(io);
 
353
  }
 
354
  return ok;
 
355
}
 
356
 
 
357
//------------------------------------------------------------------------------
 
358
// For multi-threaded decoding we need to use 3 rows of 16 pixels as delay line.
 
359
//
 
360
// Reason is: the deblocking filter cannot deblock the bottom horizontal edges
 
361
// immediately, and needs to wait for first few rows of the next macroblock to
 
362
// be decoded. Hence, deblocking is lagging behind by 4 or 8 pixels (depending
 
363
// on strength).
 
364
// With two threads, the vertical positions of the rows being decoded are:
 
365
// Decode:  [ 0..15][16..31][32..47][48..63][64..79][...
 
366
// Deblock:         [ 0..11][12..27][28..43][44..59][...
 
367
// If we use two threads and two caches of 16 pixels, the sequence would be:
 
368
// Decode:  [ 0..15][16..31][ 0..15!!][16..31][ 0..15][...
 
369
// Deblock:         [ 0..11][12..27!!][-4..11][12..27][...
 
370
// The problem occurs during row [12..15!!] that both the decoding and
 
371
// deblocking threads are writing simultaneously.
 
372
// With 3 cache lines, one get a safe write pattern:
 
373
// Decode:  [ 0..15][16..31][32..47][ 0..15][16..31][32..47][0..
 
374
// Deblock:         [ 0..11][12..27][28..43][-4..11][12..27][28...
 
375
// Note that multi-threaded output _without_ deblocking can make use of two
 
376
// cache lines of 16 pixels only, since there's no lagging behind. The decoding
 
377
// and output process have non-concurrent writing:
 
378
// Decode:  [ 0..15][16..31][ 0..15][16..31][...
 
379
// io->put:         [ 0..15][16..31][ 0..15][...
 
380
 
 
381
#define MT_CACHE_LINES 3
 
382
#define ST_CACHE_LINES 1   // 1 cache row only for single-threaded case
 
383
 
 
384
// Initialize multi/single-thread worker
 
385
static int InitThreadContext(VP8Decoder* const dec) {
 
386
  dec->cache_id_ = 0;
 
387
  if (dec->use_threads_) {
 
388
    WebPWorker* const worker = &dec->worker_;
 
389
    if (!WebPWorkerReset(worker)) {
 
390
      return VP8SetError(dec, VP8_STATUS_OUT_OF_MEMORY,
 
391
                         "thread initialization failed.");
 
392
    }
 
393
    worker->data1 = dec;
 
394
    worker->data2 = (void*)&dec->thread_ctx_.io_;
 
395
    worker->hook = (WebPWorkerHook)FinishRow;
 
396
    dec->num_caches_ =
 
397
      (dec->filter_type_ > 0) ? MT_CACHE_LINES : MT_CACHE_LINES - 1;
 
398
  } else {
 
399
    dec->num_caches_ = ST_CACHE_LINES;
 
400
  }
 
401
  return 1;
 
402
}
 
403
 
 
404
#undef MT_CACHE_LINES
 
405
#undef ST_CACHE_LINES
 
406
 
 
407
//------------------------------------------------------------------------------
 
408
// Memory setup
 
409
 
 
410
static int AllocateMemory(VP8Decoder* const dec) {
 
411
  const int num_caches = dec->num_caches_;
 
412
  const int mb_w = dec->mb_w_;
 
413
  // Note: we use 'size_t' when there's no overflow risk, uint64_t otherwise.
 
414
  const size_t intra_pred_mode_size = 4 * mb_w * sizeof(uint8_t);
 
415
  const size_t top_size = (16 + 8 + 8) * mb_w;
 
416
  const size_t mb_info_size = (mb_w + 1) * sizeof(VP8MB);
 
417
  const size_t f_info_size =
 
418
      (dec->filter_type_ > 0) ?
 
419
          mb_w * (dec->use_threads_ ? 2 : 1) * sizeof(VP8FInfo)
 
420
        : 0;
 
421
  const size_t yuv_size = YUV_SIZE * sizeof(*dec->yuv_b_);
 
422
  const size_t coeffs_size = 384 * sizeof(*dec->coeffs_);
 
423
  const size_t cache_height = (16 * num_caches
 
424
                            + kFilterExtraRows[dec->filter_type_]) * 3 / 2;
 
425
  const size_t cache_size = top_size * cache_height;
 
426
  // alpha_size is the only one that scales as width x height.
 
427
  const uint64_t alpha_size = (dec->alpha_data_ != NULL) ?
 
428
      (uint64_t)dec->pic_hdr_.width_ * dec->pic_hdr_.height_ : 0ULL;
 
429
  const uint64_t needed = (uint64_t)intra_pred_mode_size
 
430
                        + top_size + mb_info_size + f_info_size
 
431
                        + yuv_size + coeffs_size
 
432
                        + cache_size + alpha_size + ALIGN_MASK;
 
433
  uint8_t* mem;
 
434
 
 
435
  if (needed != (size_t)needed) return 0;  // check for overflow
 
436
  if (needed > dec->mem_size_) {
 
437
    free(dec->mem_);
 
438
    dec->mem_size_ = 0;
 
439
    dec->mem_ = WebPSafeMalloc(needed, sizeof(uint8_t));
 
440
    if (dec->mem_ == NULL) {
 
441
      return VP8SetError(dec, VP8_STATUS_OUT_OF_MEMORY,
 
442
                         "no memory during frame initialization.");
 
443
    }
 
444
    // down-cast is ok, thanks to WebPSafeAlloc() above.
 
445
    dec->mem_size_ = (size_t)needed;
 
446
  }
 
447
 
 
448
  mem = (uint8_t*)dec->mem_;
 
449
  dec->intra_t_ = (uint8_t*)mem;
 
450
  mem += intra_pred_mode_size;
 
451
 
 
452
  dec->y_t_ = (uint8_t*)mem;
 
453
  mem += 16 * mb_w;
 
454
  dec->u_t_ = (uint8_t*)mem;
 
455
  mem += 8 * mb_w;
 
456
  dec->v_t_ = (uint8_t*)mem;
 
457
  mem += 8 * mb_w;
 
458
 
 
459
  dec->mb_info_ = ((VP8MB*)mem) + 1;
 
460
  mem += mb_info_size;
 
461
 
 
462
  dec->f_info_ = f_info_size ? (VP8FInfo*)mem : NULL;
 
463
  mem += f_info_size;
 
464
  dec->thread_ctx_.id_ = 0;
 
465
  dec->thread_ctx_.f_info_ = dec->f_info_;
 
466
  if (dec->use_threads_) {
 
467
    // secondary cache line. The deblocking process need to make use of the
 
468
    // filtering strength from previous macroblock row, while the new ones
 
469
    // are being decoded in parallel. We'll just swap the pointers.
 
470
    dec->thread_ctx_.f_info_ += mb_w;
 
471
  }
 
472
 
 
473
  mem = (uint8_t*)((uintptr_t)(mem + ALIGN_MASK) & ~ALIGN_MASK);
 
474
  assert((yuv_size & ALIGN_MASK) == 0);
 
475
  dec->yuv_b_ = (uint8_t*)mem;
 
476
  mem += yuv_size;
 
477
 
 
478
  dec->coeffs_ = (int16_t*)mem;
 
479
  mem += coeffs_size;
 
480
 
 
481
  dec->cache_y_stride_ = 16 * mb_w;
 
482
  dec->cache_uv_stride_ = 8 * mb_w;
 
483
  {
 
484
    const int extra_rows = kFilterExtraRows[dec->filter_type_];
 
485
    const int extra_y = extra_rows * dec->cache_y_stride_;
 
486
    const int extra_uv = (extra_rows / 2) * dec->cache_uv_stride_;
 
487
    dec->cache_y_ = ((uint8_t*)mem) + extra_y;
 
488
    dec->cache_u_ = dec->cache_y_
 
489
                  + 16 * num_caches * dec->cache_y_stride_ + extra_uv;
 
490
    dec->cache_v_ = dec->cache_u_
 
491
                  + 8 * num_caches * dec->cache_uv_stride_ + extra_uv;
 
492
    dec->cache_id_ = 0;
 
493
  }
 
494
  mem += cache_size;
 
495
 
 
496
  // alpha plane
 
497
  dec->alpha_plane_ = alpha_size ? (uint8_t*)mem : NULL;
 
498
  mem += alpha_size;
 
499
  assert(mem <= (uint8_t*)dec->mem_ + dec->mem_size_);
 
500
 
 
501
  // note: left-info is initialized once for all.
 
502
  memset(dec->mb_info_ - 1, 0, mb_info_size);
 
503
 
 
504
  // initialize top
 
505
  memset(dec->intra_t_, B_DC_PRED, intra_pred_mode_size);
 
506
 
 
507
  return 1;
 
508
}
 
509
 
 
510
static void InitIo(VP8Decoder* const dec, VP8Io* io) {
 
511
  // prepare 'io'
 
512
  io->mb_y = 0;
 
513
  io->y = dec->cache_y_;
 
514
  io->u = dec->cache_u_;
 
515
  io->v = dec->cache_v_;
 
516
  io->y_stride = dec->cache_y_stride_;
 
517
  io->uv_stride = dec->cache_uv_stride_;
 
518
  io->a = NULL;
 
519
}
 
520
 
 
521
int VP8InitFrame(VP8Decoder* const dec, VP8Io* io) {
 
522
  if (!InitThreadContext(dec)) return 0;  // call first. Sets dec->num_caches_.
 
523
  if (!AllocateMemory(dec)) return 0;
 
524
  InitIo(dec, io);
 
525
  VP8DspInit();  // Init critical function pointers and look-up tables.
 
526
  return 1;
 
527
}
 
528
 
 
529
//------------------------------------------------------------------------------
 
530
// Main reconstruction function.
 
531
 
 
532
static const int kScan[16] = {
 
533
  0 +  0 * BPS,  4 +  0 * BPS, 8 +  0 * BPS, 12 +  0 * BPS,
 
534
  0 +  4 * BPS,  4 +  4 * BPS, 8 +  4 * BPS, 12 +  4 * BPS,
 
535
  0 +  8 * BPS,  4 +  8 * BPS, 8 +  8 * BPS, 12 +  8 * BPS,
 
536
  0 + 12 * BPS,  4 + 12 * BPS, 8 + 12 * BPS, 12 + 12 * BPS
 
537
};
 
538
 
 
539
static WEBP_INLINE int CheckMode(VP8Decoder* const dec, int mode) {
 
540
  if (mode == B_DC_PRED) {
 
541
    if (dec->mb_x_ == 0) {
 
542
      return (dec->mb_y_ == 0) ? B_DC_PRED_NOTOPLEFT : B_DC_PRED_NOLEFT;
 
543
    } else {
 
544
      return (dec->mb_y_ == 0) ? B_DC_PRED_NOTOP : B_DC_PRED;
 
545
    }
 
546
  }
 
547
  return mode;
 
548
}
 
549
 
 
550
static WEBP_INLINE void Copy32b(uint8_t* dst, uint8_t* src) {
 
551
  *(uint32_t*)dst = *(uint32_t*)src;
 
552
}
 
553
 
 
554
void VP8ReconstructBlock(VP8Decoder* const dec) {
 
555
  int j;
 
556
  uint8_t* const y_dst = dec->yuv_b_ + Y_OFF;
 
557
  uint8_t* const u_dst = dec->yuv_b_ + U_OFF;
 
558
  uint8_t* const v_dst = dec->yuv_b_ + V_OFF;
 
559
 
 
560
  // Rotate in the left samples from previously decoded block. We move four
 
561
  // pixels at a time for alignment reason, and because of in-loop filter.
 
562
  if (dec->mb_x_ > 0) {
 
563
    for (j = -1; j < 16; ++j) {
 
564
      Copy32b(&y_dst[j * BPS - 4], &y_dst[j * BPS + 12]);
 
565
    }
 
566
    for (j = -1; j < 8; ++j) {
 
567
      Copy32b(&u_dst[j * BPS - 4], &u_dst[j * BPS + 4]);
 
568
      Copy32b(&v_dst[j * BPS - 4], &v_dst[j * BPS + 4]);
 
569
    }
 
570
  } else {
 
571
    for (j = 0; j < 16; ++j) {
 
572
      y_dst[j * BPS - 1] = 129;
 
573
    }
 
574
    for (j = 0; j < 8; ++j) {
 
575
      u_dst[j * BPS - 1] = 129;
 
576
      v_dst[j * BPS - 1] = 129;
 
577
    }
 
578
    // Init top-left sample on left column too
 
579
    if (dec->mb_y_ > 0) {
 
580
      y_dst[-1 - BPS] = u_dst[-1 - BPS] = v_dst[-1 - BPS] = 129;
 
581
    }
 
582
  }
 
583
  {
 
584
    // bring top samples into the cache
 
585
    uint8_t* const top_y = dec->y_t_ + dec->mb_x_ * 16;
 
586
    uint8_t* const top_u = dec->u_t_ + dec->mb_x_ * 8;
 
587
    uint8_t* const top_v = dec->v_t_ + dec->mb_x_ * 8;
 
588
    const int16_t* coeffs = dec->coeffs_;
 
589
    int n;
 
590
 
 
591
    if (dec->mb_y_ > 0) {
 
592
      memcpy(y_dst - BPS, top_y, 16);
 
593
      memcpy(u_dst - BPS, top_u, 8);
 
594
      memcpy(v_dst - BPS, top_v, 8);
 
595
    } else if (dec->mb_x_ == 0) {
 
596
      // we only need to do this init once at block (0,0).
 
597
      // Afterward, it remains valid for the whole topmost row.
 
598
      memset(y_dst - BPS - 1, 127, 16 + 4 + 1);
 
599
      memset(u_dst - BPS - 1, 127, 8 + 1);
 
600
      memset(v_dst - BPS - 1, 127, 8 + 1);
 
601
    }
 
602
 
 
603
    // predict and add residuals
 
604
 
 
605
    if (dec->is_i4x4_) {   // 4x4
 
606
      uint32_t* const top_right = (uint32_t*)(y_dst - BPS + 16);
 
607
 
 
608
      if (dec->mb_y_ > 0) {
 
609
        if (dec->mb_x_ >= dec->mb_w_ - 1) {    // on rightmost border
 
610
          top_right[0] = top_y[15] * 0x01010101u;
 
611
        } else {
 
612
          memcpy(top_right, top_y + 16, sizeof(*top_right));
 
613
        }
 
614
      }
 
615
      // replicate the top-right pixels below
 
616
      top_right[BPS] = top_right[2 * BPS] = top_right[3 * BPS] = top_right[0];
 
617
 
 
618
      // predict and add residues for all 4x4 blocks in turn.
 
619
      for (n = 0; n < 16; n++) {
 
620
        uint8_t* const dst = y_dst + kScan[n];
 
621
        VP8PredLuma4[dec->imodes_[n]](dst);
 
622
        if (dec->non_zero_ac_ & (1 << n)) {
 
623
          VP8Transform(coeffs + n * 16, dst, 0);
 
624
        } else if (dec->non_zero_ & (1 << n)) {  // only DC is present
 
625
          VP8TransformDC(coeffs + n * 16, dst);
 
626
        }
 
627
      }
 
628
    } else {    // 16x16
 
629
      const int pred_func = CheckMode(dec, dec->imodes_[0]);
 
630
      VP8PredLuma16[pred_func](y_dst);
 
631
      if (dec->non_zero_) {
 
632
        for (n = 0; n < 16; n++) {
 
633
          uint8_t* const dst = y_dst + kScan[n];
 
634
          if (dec->non_zero_ac_ & (1 << n)) {
 
635
            VP8Transform(coeffs + n * 16, dst, 0);
 
636
          } else if (dec->non_zero_ & (1 << n)) {  // only DC is present
 
637
            VP8TransformDC(coeffs + n * 16, dst);
 
638
          }
 
639
        }
 
640
      }
 
641
    }
 
642
    {
 
643
      // Chroma
 
644
      const int pred_func = CheckMode(dec, dec->uvmode_);
 
645
      VP8PredChroma8[pred_func](u_dst);
 
646
      VP8PredChroma8[pred_func](v_dst);
 
647
 
 
648
      if (dec->non_zero_ & 0x0f0000) {   // chroma-U
 
649
        const int16_t* const u_coeffs = dec->coeffs_ + 16 * 16;
 
650
        if (dec->non_zero_ac_ & 0x0f0000) {
 
651
          VP8TransformUV(u_coeffs, u_dst);
 
652
        } else {
 
653
          VP8TransformDCUV(u_coeffs, u_dst);
 
654
        }
 
655
      }
 
656
      if (dec->non_zero_ & 0xf00000) {   // chroma-V
 
657
        const int16_t* const v_coeffs = dec->coeffs_ + 20 * 16;
 
658
        if (dec->non_zero_ac_ & 0xf00000) {
 
659
          VP8TransformUV(v_coeffs, v_dst);
 
660
        } else {
 
661
          VP8TransformDCUV(v_coeffs, v_dst);
 
662
        }
 
663
      }
 
664
 
 
665
      // stash away top samples for next block
 
666
      if (dec->mb_y_ < dec->mb_h_ - 1) {
 
667
        memcpy(top_y, y_dst + 15 * BPS, 16);
 
668
        memcpy(top_u, u_dst +  7 * BPS,  8);
 
669
        memcpy(top_v, v_dst +  7 * BPS,  8);
 
670
      }
 
671
    }
 
672
  }
 
673
  // Transfer reconstructed samples from yuv_b_ cache to final destination.
 
674
  {
 
675
    const int y_offset = dec->cache_id_ * 16 * dec->cache_y_stride_;
 
676
    const int uv_offset = dec->cache_id_ * 8 * dec->cache_uv_stride_;
 
677
    uint8_t* const y_out = dec->cache_y_ + dec->mb_x_ * 16 + y_offset;
 
678
    uint8_t* const u_out = dec->cache_u_ + dec->mb_x_ * 8 + uv_offset;
 
679
    uint8_t* const v_out = dec->cache_v_ + dec->mb_x_ * 8 + uv_offset;
 
680
    for (j = 0; j < 16; ++j) {
 
681
      memcpy(y_out + j * dec->cache_y_stride_, y_dst + j * BPS, 16);
 
682
    }
 
683
    for (j = 0; j < 8; ++j) {
 
684
      memcpy(u_out + j * dec->cache_uv_stride_, u_dst + j * BPS, 8);
 
685
      memcpy(v_out + j * dec->cache_uv_stride_, v_dst + j * BPS, 8);
 
686
    }
 
687
  }
 
688
}
 
689
 
 
690
//------------------------------------------------------------------------------
 
691
 
 
692
#if defined(__cplusplus) || defined(c_plusplus)
 
693
}    // extern "C"
 
694
#endif