Coverage Report

Created: 2026-09-14 08:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libvpx/vp8/vp8_dx_iface.c
Line
Count
Source
1
/*
2
 *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3
 *
4
 *  Use of this source code is governed by a BSD-style license
5
 *  that can be found in the LICENSE file in the root of the source
6
 *  tree. An additional intellectual property rights grant can be found
7
 *  in the file PATENTS.  All contributing project authors may
8
 *  be found in the AUTHORS file in the root of the source tree.
9
 */
10
11
#include <assert.h>
12
#include <stdlib.h>
13
#include <string.h>
14
#include "./vp8_rtcd.h"
15
#include "./vpx_dsp_rtcd.h"
16
#include "./vpx_scale_rtcd.h"
17
#include "vpx/vpx_decoder.h"
18
#include "vpx/vp8dx.h"
19
#include "vpx/internal/vpx_codec_internal.h"
20
#include "vpx_version.h"
21
#include "common/alloccommon.h"
22
#include "common/common.h"
23
#include "common/onyxc_int.h"
24
#include "common/onyxd.h"
25
#include "decoder/onyxd_int.h"
26
#include "vpx_dsp/vpx_dsp_common.h"
27
#include "vpx_mem/vpx_mem.h"
28
#include "vpx_ports/system_state.h"
29
#if CONFIG_ERROR_CONCEALMENT
30
#include "decoder/error_concealment.h"
31
#endif
32
#include "decoder/decoderthreading.h"
33
34
#define VP8_CAP_POSTPROC (CONFIG_POSTPROC ? VPX_CODEC_CAP_POSTPROC : 0)
35
#define VP8_CAP_ERROR_CONCEALMENT \
36
  (CONFIG_ERROR_CONCEALMENT ? VPX_CODEC_CAP_ERROR_CONCEALMENT : 0)
37
38
typedef vpx_codec_stream_info_t vp8_stream_info_t;
39
40
/* Structures for handling memory allocations */
41
typedef enum { VP8_SEG_ALG_PRIV = 256, VP8_SEG_MAX } mem_seg_id_t;
42
#define NELEMENTS(x) ((int)(sizeof(x) / sizeof((x)[0])))
43
44
struct vpx_codec_alg_priv {
45
  vpx_codec_priv_t base;
46
  vpx_codec_dec_cfg_t cfg;
47
  vp8_stream_info_t si;
48
  int decoder_init;
49
#if CONFIG_MULTITHREAD
50
  // Restart threads on next frame if set to 1.
51
  // This is set when error happens in multithreaded decoding and all threads
52
  // are shut down.
53
  int restart_threads;
54
#endif
55
  int postproc_cfg_set;
56
  vp8_postproc_cfg_t postproc_cfg;
57
  vpx_decrypt_cb decrypt_cb;
58
  void *decrypt_state;
59
  vpx_image_t img;
60
  int img_setup;
61
  struct frame_buffers yv12_frame_buffers;
62
  void *user_priv;
63
  FRAGMENT_DATA fragments;
64
};
65
66
6.26k
static int vp8_init_ctx(vpx_codec_ctx_t *ctx) {
67
6.26k
  vpx_codec_alg_priv_t *priv =
68
6.26k
      (vpx_codec_alg_priv_t *)vpx_calloc(1, sizeof(*priv));
69
6.26k
  if (!priv) return 1;
70
71
6.26k
  ctx->priv = (vpx_codec_priv_t *)priv;
72
6.26k
  ctx->priv->init_flags = ctx->init_flags;
73
74
6.26k
  priv->si.sz = sizeof(priv->si);
75
6.26k
  priv->decrypt_cb = NULL;
76
6.26k
  priv->decrypt_state = NULL;
77
78
6.26k
  if (ctx->config.dec) {
79
    /* Update the reference to the config structure to an internal copy. */
80
6.26k
    priv->cfg = *ctx->config.dec;
81
6.26k
    ctx->config.dec = &priv->cfg;
82
6.26k
  }
83
84
6.26k
  return 0;
85
6.26k
}
86
87
static vpx_codec_err_t vp8_init(vpx_codec_ctx_t *ctx,
88
6.26k
                                vpx_codec_priv_enc_mr_cfg_t *data) {
89
6.26k
  vpx_codec_err_t res = VPX_CODEC_OK;
90
6.26k
  (void)data;
91
92
6.26k
  vp8_rtcd();
93
6.26k
  vpx_dsp_rtcd();
94
6.26k
  vpx_scale_rtcd();
95
96
  /* This function only allocates space for the vpx_codec_alg_priv_t
97
   * structure. More memory may be required at the time the stream
98
   * information becomes known.
99
   */
100
6.26k
  if (!ctx->priv) {
101
6.26k
    vpx_codec_alg_priv_t *priv;
102
103
6.26k
    if (vp8_init_ctx(ctx)) return VPX_CODEC_MEM_ERROR;
104
105
6.26k
    priv = (vpx_codec_alg_priv_t *)ctx->priv;
106
107
    /* initialize number of fragments to zero */
108
6.26k
    priv->fragments.count = 0;
109
    /* is input fragments enabled? */
110
6.26k
    priv->fragments.enabled =
111
6.26k
        (priv->base.init_flags & VPX_CODEC_USE_INPUT_FRAGMENTS);
112
113
    /*post processing level initialized to do nothing */
114
6.26k
  }
115
116
6.26k
  return res;
117
6.26k
}
118
119
6.26k
static vpx_codec_err_t vp8_destroy(vpx_codec_alg_priv_t *ctx) {
120
6.26k
  vp8_remove_decoder_instances(&ctx->yv12_frame_buffers);
121
122
6.26k
  vpx_free(ctx);
123
124
6.26k
  return VPX_CODEC_OK;
125
6.26k
}
126
127
static vpx_codec_err_t vp8_peek_si_internal(const uint8_t *data,
128
                                            unsigned int data_sz,
129
                                            vpx_codec_stream_info_t *si,
130
                                            vpx_decrypt_cb decrypt_cb,
131
444k
                                            void *decrypt_state) {
132
444k
  vpx_codec_err_t res = VPX_CODEC_OK;
133
134
444k
  if (data == NULL) return VPX_CODEC_INVALID_PARAM;
135
136
444k
  if (data + data_sz <= data) {
137
0
    res = VPX_CODEC_INVALID_PARAM;
138
444k
  } else {
139
    /* Parse uncompresssed part of key frame header.
140
     * 3 bytes:- including version, frame type and an offset
141
     * 3 bytes:- sync code (0x9d, 0x01, 0x2a)
142
     * 4 bytes:- including image width and height in the lowest 14 bits
143
     *           of each 2-byte value.
144
     */
145
444k
    uint8_t clear_buffer[10];
146
444k
    const uint8_t *clear = data;
147
444k
    if (decrypt_cb) {
148
0
      int n = VPXMIN(sizeof(clear_buffer), data_sz);
149
0
      decrypt_cb(decrypt_state, data, clear_buffer, n);
150
0
      clear = clear_buffer;
151
0
    }
152
444k
    si->is_kf = 0;
153
154
444k
    if (data_sz >= 10 && !(clear[0] & 0x01)) { /* I-Frame */
155
47.6k
      si->is_kf = 1;
156
157
      /* vet via sync code */
158
47.6k
      if (clear[3] != 0x9d || clear[4] != 0x01 || clear[5] != 0x2a) {
159
32.1k
        return VPX_CODEC_UNSUP_BITSTREAM;
160
32.1k
      }
161
162
15.5k
      si->w = (clear[6] | (clear[7] << 8)) & 0x3fff;
163
15.5k
      si->h = (clear[8] | (clear[9] << 8)) & 0x3fff;
164
165
      /*printf("w=%d, h=%d\n", si->w, si->h);*/
166
15.5k
      if (!(si->h && si->w)) {
167
1.04k
        si->w = si->h = 0;
168
1.04k
        res = VPX_CODEC_CORRUPT_FRAME;
169
1.04k
      }
170
397k
    } else {
171
397k
      res = VPX_CODEC_UNSUP_BITSTREAM;
172
397k
    }
173
444k
  }
174
175
412k
  return res;
176
444k
}
177
178
static vpx_codec_err_t vp8_peek_si(const uint8_t *data, unsigned int data_sz,
179
0
                                   vpx_codec_stream_info_t *si) {
180
0
  return vp8_peek_si_internal(data, data_sz, si, NULL, NULL);
181
0
}
182
183
static vpx_codec_err_t vp8_get_si(vpx_codec_alg_priv_t *ctx,
184
0
                                  vpx_codec_stream_info_t *si) {
185
0
  unsigned int sz;
186
187
0
  if (si->sz >= sizeof(vp8_stream_info_t)) {
188
0
    sz = sizeof(vp8_stream_info_t);
189
0
  } else {
190
0
    sz = sizeof(vpx_codec_stream_info_t);
191
0
  }
192
193
0
  memcpy(si, &ctx->si, sz);
194
0
  si->sz = sz;
195
196
0
  return VPX_CODEC_OK;
197
0
}
198
199
static vpx_codec_err_t update_error_state(
200
133k
    vpx_codec_alg_priv_t *ctx, const struct vpx_internal_error_info *error) {
201
133k
  vpx_codec_err_t res;
202
203
133k
  if ((res = error->error_code)) {
204
133k
    ctx->base.err_detail = error->has_detail ? error->detail : NULL;
205
133k
  }
206
207
133k
  return res;
208
133k
}
209
210
static void yuvconfig2image(vpx_image_t *img, const YV12_BUFFER_CONFIG *yv12,
211
28.7k
                            void *user_priv) {
212
  /** vpx_img_wrap() doesn't allow specifying independent strides for
213
   * the Y, U, and V planes, nor other alignment adjustments that
214
   * might be representable by a YV12_BUFFER_CONFIG, so we just
215
   * initialize all the fields.*/
216
28.7k
  img->fmt = VPX_IMG_FMT_I420;
217
28.7k
  img->w = yv12->y_stride;
218
28.7k
  img->h = (yv12->y_height + 2 * VP8BORDERINPIXELS + 15) & ~15;
219
28.7k
  img->d_w = img->r_w = yv12->y_width;
220
28.7k
  img->d_h = img->r_h = yv12->y_height;
221
28.7k
  img->x_chroma_shift = 1;
222
28.7k
  img->y_chroma_shift = 1;
223
28.7k
  img->planes[VPX_PLANE_Y] = yv12->y_buffer;
224
28.7k
  img->planes[VPX_PLANE_U] = yv12->u_buffer;
225
28.7k
  img->planes[VPX_PLANE_V] = yv12->v_buffer;
226
28.7k
  img->planes[VPX_PLANE_ALPHA] = NULL;
227
28.7k
  img->stride[VPX_PLANE_Y] = yv12->y_stride;
228
28.7k
  img->stride[VPX_PLANE_U] = yv12->uv_stride;
229
28.7k
  img->stride[VPX_PLANE_V] = yv12->uv_stride;
230
28.7k
  img->stride[VPX_PLANE_ALPHA] = yv12->y_stride;
231
28.7k
  img->bit_depth = 8;
232
28.7k
  img->bps = 12;
233
28.7k
  img->user_priv = user_priv;
234
28.7k
  img->img_data = yv12->buffer_alloc;
235
28.7k
  img->img_data_owner = 0;
236
28.7k
  img->self_allocd = 0;
237
28.7k
}
238
239
static int update_fragments(vpx_codec_alg_priv_t *ctx, const uint8_t *data,
240
                            unsigned int data_sz,
241
444k
                            volatile vpx_codec_err_t *res) {
242
444k
  *res = VPX_CODEC_OK;
243
244
444k
  if (ctx->fragments.count == 0) {
245
    /* New frame, reset fragment pointers and sizes */
246
176k
    memset((void *)ctx->fragments.ptrs, 0, sizeof(ctx->fragments.ptrs));
247
176k
    memset(ctx->fragments.sizes, 0, sizeof(ctx->fragments.sizes));
248
176k
  }
249
250
  /* Flush signal in fragment mode but no fragments were accumulated yet.
251
   * Nothing to decode; treat as a no-op. */
252
444k
  if (ctx->fragments.enabled && data == NULL && data_sz == 0 &&
253
0
      ctx->fragments.count == 0) {
254
0
    return 0;
255
0
  }
256
257
444k
  if (ctx->fragments.enabled && !(data == NULL && data_sz == 0)) {
258
    /* Store a pointer to this fragment and return. We haven't
259
     * received the complete frame yet, so we will wait with decoding.
260
     */
261
0
    if (ctx->fragments.count >= MAX_PARTITIONS) {
262
0
      ctx->fragments.count = 0;
263
0
      *res = VPX_CODEC_INVALID_PARAM;
264
0
      return -1;
265
0
    }
266
0
    ctx->fragments.ptrs[ctx->fragments.count] = data;
267
0
    ctx->fragments.sizes[ctx->fragments.count] = data_sz;
268
0
    ctx->fragments.count++;
269
0
    return 0;
270
0
  }
271
272
444k
  if (!ctx->fragments.enabled && (data == NULL && data_sz == 0)) {
273
0
    return 0;
274
0
  }
275
276
444k
  if (!ctx->fragments.enabled) {
277
444k
    ctx->fragments.ptrs[0] = data;
278
444k
    ctx->fragments.sizes[0] = data_sz;
279
444k
    ctx->fragments.count = 1;
280
444k
  }
281
282
444k
  return 1;
283
444k
}
284
285
static vpx_codec_err_t vp8_decode(vpx_codec_alg_priv_t *ctx,
286
                                  const uint8_t *data, unsigned int data_sz,
287
444k
                                  void *user_priv) {
288
444k
  volatile vpx_codec_err_t res;
289
444k
  volatile unsigned int resolution_change = 0;
290
444k
  volatile unsigned int w, h;
291
292
444k
  if (!ctx->fragments.enabled && (data == NULL && data_sz == 0)) {
293
0
    return 0;
294
0
  }
295
296
  /* Update the input fragment data */
297
444k
  if (update_fragments(ctx, data, data_sz, &res) <= 0) return res;
298
299
  /* Determine the stream parameters. Note that we rely on peek_si to
300
   * validate that we have a buffer that does not wrap around the top
301
   * of the heap.
302
   */
303
444k
  w = ctx->si.w;
304
444k
  h = ctx->si.h;
305
306
444k
  res = vp8_peek_si_internal(ctx->fragments.ptrs[0], ctx->fragments.sizes[0],
307
444k
                             &ctx->si, ctx->decrypt_cb, ctx->decrypt_state);
308
309
444k
  if ((res == VPX_CODEC_UNSUP_BITSTREAM) && !ctx->si.is_kf) {
310
    /* the peek function returns an error for non keyframes, however for
311
     * this case, it is not an error */
312
397k
    res = VPX_CODEC_OK;
313
397k
  }
314
315
444k
#if CONFIG_SIZE_LIMIT
316
  // Reject oversized keyframes before decoder initialization allocates frame
317
  // buffers.
318
444k
  if (!res && ctx->si.is_kf &&
319
14.5k
      (ctx->si.w > DECODE_WIDTH_LIMIT || ctx->si.h > DECODE_HEIGHT_LIMIT)) {
320
592
    ctx->si.w = 0;
321
592
    ctx->si.h = 0;
322
592
    res = VPX_CODEC_CORRUPT_FRAME;
323
592
  }
324
444k
#endif
325
326
444k
  if (!ctx->decoder_init && !ctx->si.is_kf) res = VPX_CODEC_UNSUP_BITSTREAM;
327
444k
  if (!res && ctx->decoder_init && w == 0 && h == 0 && ctx->si.h == 0 &&
328
2.54k
      ctx->si.w == 0) {
329
2.54k
    VP8D_COMP *pbi = ctx->yv12_frame_buffers.pbi[0];
330
2.54k
    assert(pbi != NULL);
331
2.54k
    assert(!pbi->common.error.setjmp);
332
2.54k
    res = VPX_CODEC_CORRUPT_FRAME;
333
2.54k
    vpx_internal_error(&pbi->common.error, res,
334
2.54k
                       "Keyframe / intra-only frame required to reset decoder"
335
2.54k
                       " state");
336
2.54k
  }
337
338
444k
  if ((ctx->si.h != h) || (ctx->si.w != w)) resolution_change = 1;
339
340
444k
#if CONFIG_MULTITHREAD
341
444k
  if (!res && ctx->restart_threads) {
342
0
    VP8D_COMP *pbi = ctx->yv12_frame_buffers.pbi[0];
343
0
    VP8_COMMON *const pc = &pbi->common;
344
0
    if (setjmp(pbi->common.error.jmp)) {
345
0
      pbi->common.error.setjmp = 0;
346
0
      vp8_decoder_remove_threads(pbi);
347
0
      vpx_clear_system_state();
348
0
      return VPX_CODEC_ERROR;
349
0
    }
350
0
    pbi->common.error.setjmp = 1;
351
0
    pbi->max_threads = ctx->cfg.threads;
352
0
    vp8_decoder_create_threads(pbi);
353
0
    if (vpx_atomic_load_acquire(&pbi->b_multithreaded_rd)) {
354
0
      vp8mt_alloc_temp_buffers(pbi, pc->Width, pc->mb_rows);
355
0
    }
356
0
    ctx->restart_threads = 0;
357
0
    pbi->common.error.setjmp = 0;
358
0
  }
359
444k
#endif
360
  /* Initialize the decoder instance on the first frame*/
361
444k
  if (!res && !ctx->decoder_init) {
362
5.84k
    VP8D_CONFIG oxcf;
363
364
5.84k
    oxcf.Width = ctx->si.w;
365
5.84k
    oxcf.Height = ctx->si.h;
366
5.84k
    oxcf.Version = 9;
367
5.84k
    oxcf.postprocess = 0;
368
5.84k
    oxcf.max_threads = ctx->cfg.threads;
369
5.84k
    oxcf.error_concealment =
370
5.84k
        (ctx->base.init_flags & VPX_CODEC_USE_ERROR_CONCEALMENT);
371
372
    /* If postprocessing was enabled by the application and a
373
     * configuration has not been provided, default it.
374
     */
375
5.84k
    if (!ctx->postproc_cfg_set &&
376
5.84k
        (ctx->base.init_flags & VPX_CODEC_USE_POSTPROC)) {
377
0
      ctx->postproc_cfg.post_proc_flag =
378
0
          VP8_DEBLOCK | VP8_DEMACROBLOCK | VP8_MFQE;
379
0
      ctx->postproc_cfg.deblocking_level = 4;
380
0
      ctx->postproc_cfg.noise_level = 0;
381
0
    }
382
383
5.84k
    res = vp8_create_decoder_instances(&ctx->yv12_frame_buffers, &oxcf);
384
5.84k
    if (res == VPX_CODEC_OK) {
385
5.84k
      ctx->decoder_init = 1;
386
5.84k
    } else {
387
      /* on failure clear the cached resolution to ensure a full
388
       * reallocation is attempted on resync. */
389
0
      ctx->si.w = 0;
390
0
      ctx->si.h = 0;
391
0
    }
392
5.84k
  }
393
394
  /* Set these even if already initialized.  The caller may have changed the
395
   * decrypt config between frames.
396
   */
397
444k
  if (ctx->decoder_init) {
398
330k
    ctx->yv12_frame_buffers.pbi[0]->decrypt_cb = ctx->decrypt_cb;
399
330k
    ctx->yv12_frame_buffers.pbi[0]->decrypt_state = ctx->decrypt_state;
400
330k
  }
401
402
444k
  if (!res) {
403
302k
    VP8D_COMP *pbi = ctx->yv12_frame_buffers.pbi[0];
404
302k
    VP8_COMMON *const pc = &pbi->common;
405
302k
    if (resolution_change) {
406
10.8k
      MACROBLOCKD *const xd = &pbi->mb;
407
10.8k
#if CONFIG_MULTITHREAD
408
10.8k
      int i;
409
10.8k
#endif
410
10.8k
      pc->Width = ctx->si.w;
411
10.8k
      pc->Height = ctx->si.h;
412
10.8k
      {
413
10.8k
        if (setjmp(pbi->common.error.jmp)) {
414
0
          pbi->common.error.setjmp = 0;
415
          /* on failure clear the cached resolution to ensure a full
416
           * reallocation is attempted on resync. */
417
0
          ctx->si.w = 0;
418
0
          ctx->si.h = 0;
419
0
          vpx_clear_system_state();
420
          /* same return value as used in vp8dx_receive_compressed_data */
421
0
          return -1;
422
0
        }
423
424
10.8k
        pbi->common.error.setjmp = 1;
425
426
10.8k
        if (pc->Width <= 0) {
427
0
          pc->Width = w;
428
0
          vpx_internal_error(&pc->error, VPX_CODEC_CORRUPT_FRAME,
429
0
                             "Invalid frame width");
430
0
        }
431
432
10.8k
        if (pc->Height <= 0) {
433
0
          pc->Height = h;
434
0
          vpx_internal_error(&pc->error, VPX_CODEC_CORRUPT_FRAME,
435
0
                             "Invalid frame height");
436
0
        }
437
438
10.8k
#if CONFIG_MULTITHREAD
439
10.8k
        if (vpx_atomic_load_acquire(&pbi->b_multithreaded_rd)) {
440
0
          vp8mt_de_alloc_temp_buffers(pbi, pc->mb_rows);
441
0
        }
442
10.8k
#endif
443
444
10.8k
        pbi->decoded_key_frame = 0;
445
10.8k
        if (vp8_alloc_frame_buffers(pc, pc->Width, pc->Height)) {
446
0
          vpx_internal_error(&pc->error, VPX_CODEC_MEM_ERROR,
447
0
                             "Failed to allocate frame buffers");
448
0
        }
449
450
10.8k
        xd->pre = pc->yv12_fb[pc->lst_fb_idx];
451
10.8k
        xd->dst = pc->yv12_fb[pc->new_fb_idx];
452
453
10.8k
#if CONFIG_MULTITHREAD
454
10.8k
        for (i = 0; i < pbi->allocated_decoding_thread_count; ++i) {
455
0
          pbi->mb_row_di[i].mbd.dst = pc->yv12_fb[pc->new_fb_idx];
456
0
          vp8_build_block_doffsets(&pbi->mb_row_di[i].mbd);
457
0
        }
458
10.8k
#endif
459
10.8k
        vp8_build_block_doffsets(&pbi->mb);
460
461
/* allocate memory for last frame MODE_INFO array */
462
#if CONFIG_ERROR_CONCEALMENT
463
464
        if (pbi->ec_enabled) {
465
          /* old prev_mip was released by vp8_de_alloc_frame_buffers()
466
           * called in vp8_alloc_frame_buffers() */
467
          pc->prev_mip = vpx_calloc((pc->mb_cols + 1) * (pc->mb_rows + 1),
468
                                    sizeof(MODE_INFO));
469
470
          if (!pc->prev_mip) {
471
            vp8_de_alloc_frame_buffers(pc);
472
            vpx_internal_error(&pc->error, VPX_CODEC_MEM_ERROR,
473
                               "Failed to allocate"
474
                               "last frame MODE_INFO array");
475
          }
476
477
          pc->prev_mi = pc->prev_mip + pc->mode_info_stride + 1;
478
479
          if (vp8_alloc_overlap_lists(pbi))
480
            vpx_internal_error(&pc->error, VPX_CODEC_MEM_ERROR,
481
                               "Failed to allocate overlap lists "
482
                               "for error concealment");
483
        }
484
485
#endif
486
487
10.8k
#if CONFIG_MULTITHREAD
488
10.8k
        if (vpx_atomic_load_acquire(&pbi->b_multithreaded_rd)) {
489
0
          vp8mt_alloc_temp_buffers(pbi, pc->Width, 0);
490
0
        }
491
10.8k
#endif
492
10.8k
      }
493
494
0
      pbi->common.error.setjmp = 0;
495
496
      /* required to get past the first get_free_fb() call */
497
10.8k
      pbi->common.fb_idx_ref_cnt[0] = 0;
498
10.8k
    }
499
500
302k
    if (setjmp(pbi->common.error.jmp)) {
501
129k
      vpx_clear_system_state();
502
      /* We do not know if the missing frame(s) was supposed to update
503
       * any of the reference buffers, but we act conservative and
504
       * mark only the last buffer as corrupted.
505
       */
506
129k
      pc->yv12_fb[pc->lst_fb_idx].corrupted = 1;
507
508
129k
      if (pc->fb_idx_ref_cnt[pc->new_fb_idx] > 0) {
509
129k
        pc->fb_idx_ref_cnt[pc->new_fb_idx]--;
510
129k
      }
511
129k
      pbi->common.error.setjmp = 0;
512
129k
#if CONFIG_MULTITHREAD
513
129k
      if (pbi->restart_threads) {
514
0
        ctx->si.w = 0;
515
0
        ctx->si.h = 0;
516
0
        ctx->restart_threads = 1;
517
0
      }
518
129k
#endif
519
129k
      res = update_error_state(ctx, &pbi->common.error);
520
129k
      return res;
521
129k
    }
522
523
173k
    pbi->common.error.setjmp = 1;
524
525
    /* update the pbi fragment data */
526
173k
    pbi->fragments = ctx->fragments;
527
173k
#if CONFIG_MULTITHREAD
528
173k
    pbi->restart_threads = 0;
529
173k
#endif
530
173k
    ctx->user_priv = user_priv;
531
173k
    if (vp8dx_receive_compressed_data(pbi)) {
532
3.67k
      res = update_error_state(ctx, &pbi->common.error);
533
3.67k
    }
534
535
    /* get ready for the next series of fragments */
536
173k
    ctx->fragments.count = 0;
537
173k
    pbi->common.error.setjmp = 0;
538
173k
  }
539
540
315k
  return res;
541
444k
}
542
543
static vpx_image_t *vp8_get_frame(vpx_codec_alg_priv_t *ctx,
544
169k
                                  vpx_codec_iter_t *iter) {
545
169k
  vpx_image_t *img = NULL;
546
547
  /* iter acts as a flip flop, so an image is only returned on the first
548
   * call to get_frame.
549
   */
550
169k
  if (!(*iter) && ctx->yv12_frame_buffers.pbi[0]) {
551
169k
    YV12_BUFFER_CONFIG sd;
552
169k
    vp8_ppflags_t flags;
553
169k
    vp8_zero(flags);
554
555
169k
    if (ctx->base.init_flags & VPX_CODEC_USE_POSTPROC) {
556
0
      flags.post_proc_flag = ctx->postproc_cfg.post_proc_flag;
557
0
      flags.deblocking_level = ctx->postproc_cfg.deblocking_level;
558
0
      flags.noise_level = ctx->postproc_cfg.noise_level;
559
0
    }
560
561
169k
    if (0 == vp8dx_get_raw_frame(ctx->yv12_frame_buffers.pbi[0], &sd, &flags)) {
562
28.7k
      yuvconfig2image(&ctx->img, &sd, ctx->user_priv);
563
564
28.7k
      img = &ctx->img;
565
28.7k
      *iter = img;
566
28.7k
    }
567
169k
  }
568
569
169k
  return img;
570
169k
}
571
572
static vpx_codec_err_t image2yuvconfig(const vpx_image_t *img,
573
0
                                       YV12_BUFFER_CONFIG *yv12) {
574
0
  const int y_w = img->d_w;
575
0
  const int y_h = img->d_h;
576
0
  const int uv_w = (img->d_w + 1) / 2;
577
0
  const int uv_h = (img->d_h + 1) / 2;
578
0
  vpx_codec_err_t res = VPX_CODEC_OK;
579
0
  yv12->y_buffer = img->planes[VPX_PLANE_Y];
580
0
  yv12->u_buffer = img->planes[VPX_PLANE_U];
581
0
  yv12->v_buffer = img->planes[VPX_PLANE_V];
582
583
0
  yv12->y_crop_width = y_w;
584
0
  yv12->y_crop_height = y_h;
585
0
  yv12->y_width = y_w;
586
0
  yv12->y_height = y_h;
587
0
  yv12->uv_crop_width = uv_w;
588
0
  yv12->uv_crop_height = uv_h;
589
0
  yv12->uv_width = uv_w;
590
0
  yv12->uv_height = uv_h;
591
592
0
  yv12->y_stride = img->stride[VPX_PLANE_Y];
593
0
  yv12->uv_stride = img->stride[VPX_PLANE_U];
594
595
0
  yv12->border = (img->stride[VPX_PLANE_Y] - img->d_w) / 2;
596
0
  return res;
597
0
}
598
599
static vpx_codec_err_t vp8_set_reference(vpx_codec_alg_priv_t *ctx,
600
0
                                         va_list args) {
601
0
  vpx_ref_frame_t *data = va_arg(args, vpx_ref_frame_t *);
602
603
0
  if (data) {
604
0
    vpx_ref_frame_t *frame = (vpx_ref_frame_t *)data;
605
0
    YV12_BUFFER_CONFIG sd;
606
607
0
    image2yuvconfig(&frame->img, &sd);
608
609
0
    if (ctx->yv12_frame_buffers.pbi[0] == NULL) return VPX_CODEC_CORRUPT_FRAME;
610
611
0
    return vp8dx_set_reference(ctx->yv12_frame_buffers.pbi[0],
612
0
                               frame->frame_type, &sd);
613
0
  } else {
614
0
    return VPX_CODEC_INVALID_PARAM;
615
0
  }
616
0
}
617
618
static vpx_codec_err_t vp8_get_reference(vpx_codec_alg_priv_t *ctx,
619
0
                                         va_list args) {
620
0
  vpx_ref_frame_t *data = va_arg(args, vpx_ref_frame_t *);
621
622
0
  if (data) {
623
0
    vpx_ref_frame_t *frame = (vpx_ref_frame_t *)data;
624
0
    YV12_BUFFER_CONFIG sd;
625
626
0
    image2yuvconfig(&frame->img, &sd);
627
628
0
    if (ctx->yv12_frame_buffers.pbi[0] == NULL) return VPX_CODEC_CORRUPT_FRAME;
629
630
0
    return vp8dx_get_reference(ctx->yv12_frame_buffers.pbi[0],
631
0
                               frame->frame_type, &sd);
632
0
  } else {
633
0
    return VPX_CODEC_INVALID_PARAM;
634
0
  }
635
0
}
636
637
static vpx_codec_err_t vp8_get_quantizer(vpx_codec_alg_priv_t *ctx,
638
0
                                         va_list args) {
639
0
  int *const arg = va_arg(args, int *);
640
0
  VP8D_COMP *pbi = ctx->yv12_frame_buffers.pbi[0];
641
0
  if (arg == NULL) return VPX_CODEC_INVALID_PARAM;
642
0
  if (pbi == NULL) return VPX_CODEC_CORRUPT_FRAME;
643
0
  *arg = vp8dx_get_quantizer(pbi);
644
0
  return VPX_CODEC_OK;
645
0
}
646
647
static vpx_codec_err_t vp8_set_postproc(vpx_codec_alg_priv_t *ctx,
648
0
                                        va_list args) {
649
0
#if CONFIG_POSTPROC
650
0
  vp8_postproc_cfg_t *data = va_arg(args, vp8_postproc_cfg_t *);
651
652
0
  if (data) {
653
0
    ctx->postproc_cfg_set = 1;
654
0
    ctx->postproc_cfg = *((vp8_postproc_cfg_t *)data);
655
0
    return VPX_CODEC_OK;
656
0
  } else {
657
0
    return VPX_CODEC_INVALID_PARAM;
658
0
  }
659
660
#else
661
  (void)ctx;
662
  (void)args;
663
  return VPX_CODEC_INCAPABLE;
664
#endif
665
0
}
666
667
static vpx_codec_err_t vp8_get_last_ref_updates(vpx_codec_alg_priv_t *ctx,
668
0
                                                va_list args) {
669
0
  int *update_info = va_arg(args, int *);
670
671
0
  if (update_info) {
672
0
    VP8D_COMP *pbi = (VP8D_COMP *)ctx->yv12_frame_buffers.pbi[0];
673
0
    if (pbi == NULL) return VPX_CODEC_CORRUPT_FRAME;
674
675
0
    *update_info = pbi->common.refresh_alt_ref_frame * (int)VP8_ALTR_FRAME +
676
0
                   pbi->common.refresh_golden_frame * (int)VP8_GOLD_FRAME +
677
0
                   pbi->common.refresh_last_frame * (int)VP8_LAST_FRAME;
678
679
0
    return VPX_CODEC_OK;
680
0
  } else {
681
0
    return VPX_CODEC_INVALID_PARAM;
682
0
  }
683
0
}
684
685
static vpx_codec_err_t vp8_get_last_ref_frame(vpx_codec_alg_priv_t *ctx,
686
0
                                              va_list args) {
687
0
  int *ref_info = va_arg(args, int *);
688
689
0
  if (ref_info) {
690
0
    VP8D_COMP *pbi = (VP8D_COMP *)ctx->yv12_frame_buffers.pbi[0];
691
0
    if (pbi) {
692
0
      VP8_COMMON *oci = &pbi->common;
693
0
      *ref_info =
694
0
          (vp8dx_references_buffer(oci, ALTREF_FRAME) ? VP8_ALTR_FRAME : 0) |
695
0
          (vp8dx_references_buffer(oci, GOLDEN_FRAME) ? VP8_GOLD_FRAME : 0) |
696
0
          (vp8dx_references_buffer(oci, LAST_FRAME) ? VP8_LAST_FRAME : 0);
697
0
      return VPX_CODEC_OK;
698
0
    } else {
699
0
      return VPX_CODEC_CORRUPT_FRAME;
700
0
    }
701
0
  } else {
702
0
    return VPX_CODEC_INVALID_PARAM;
703
0
  }
704
0
}
705
706
static vpx_codec_err_t vp8_get_frame_corrupted(vpx_codec_alg_priv_t *ctx,
707
0
                                               va_list args) {
708
0
  int *corrupted = va_arg(args, int *);
709
0
  VP8D_COMP *pbi = (VP8D_COMP *)ctx->yv12_frame_buffers.pbi[0];
710
711
0
  if (corrupted && pbi) {
712
0
    const YV12_BUFFER_CONFIG *const frame = pbi->common.frame_to_show;
713
0
    if (frame == NULL) return VPX_CODEC_ERROR;
714
0
    *corrupted = frame->corrupted;
715
0
    return VPX_CODEC_OK;
716
0
  } else {
717
0
    return VPX_CODEC_INVALID_PARAM;
718
0
  }
719
0
}
720
721
static vpx_codec_err_t vp8_set_decryptor(vpx_codec_alg_priv_t *ctx,
722
0
                                         va_list args) {
723
0
  vpx_decrypt_init *init = va_arg(args, vpx_decrypt_init *);
724
725
0
  if (init) {
726
0
    ctx->decrypt_cb = init->decrypt_cb;
727
0
    ctx->decrypt_state = init->decrypt_state;
728
0
  } else {
729
0
    ctx->decrypt_cb = NULL;
730
    ctx->decrypt_state = NULL;
731
0
  }
732
0
  return VPX_CODEC_OK;
733
0
}
734
735
static vpx_codec_ctrl_fn_map_t vp8_ctf_maps[] = {
736
  { VP8_SET_REFERENCE, vp8_set_reference },
737
  { VP8_COPY_REFERENCE, vp8_get_reference },
738
  { VP8_SET_POSTPROC, vp8_set_postproc },
739
  { VP8D_GET_LAST_REF_UPDATES, vp8_get_last_ref_updates },
740
  { VP8D_GET_FRAME_CORRUPTED, vp8_get_frame_corrupted },
741
  { VP8D_GET_LAST_REF_USED, vp8_get_last_ref_frame },
742
  { VPXD_GET_LAST_QUANTIZER, vp8_get_quantizer },
743
  { VPXD_SET_DECRYPTOR, vp8_set_decryptor },
744
  { -1, NULL },
745
};
746
747
#ifndef VERSION_STRING
748
#define VERSION_STRING
749
#endif
750
CODEC_INTERFACE(vpx_codec_vp8_dx) = {
751
  "WebM Project VP8 Decoder" VERSION_STRING,
752
  VPX_CODEC_INTERNAL_ABI_VERSION,
753
  VPX_CODEC_CAP_DECODER | VP8_CAP_POSTPROC | VP8_CAP_ERROR_CONCEALMENT |
754
      VPX_CODEC_CAP_INPUT_FRAGMENTS,
755
  /* vpx_codec_caps_t          caps; */
756
  vp8_init,     /* vpx_codec_init_fn_t       init; */
757
  vp8_destroy,  /* vpx_codec_destroy_fn_t    destroy; */
758
  vp8_ctf_maps, /* vpx_codec_ctrl_fn_map_t  *ctrl_maps; */
759
  {
760
      vp8_peek_si,   /* vpx_codec_peek_si_fn_t    peek_si; */
761
      vp8_get_si,    /* vpx_codec_get_si_fn_t     get_si; */
762
      vp8_decode,    /* vpx_codec_decode_fn_t     decode; */
763
      vp8_get_frame, /* vpx_codec_frame_get_fn_t  frame_get; */
764
      NULL,
765
  },
766
  {
767
      /* encoder functions */
768
      0, NULL, /* vpx_codec_enc_cfg_map_t */
769
      NULL,    /* vpx_codec_encode_fn_t */
770
      NULL,    /* vpx_codec_get_cx_data_fn_t */
771
      NULL,    /* vpx_codec_enc_config_set_fn_t */
772
      NULL,    /* vpx_codec_get_global_headers_fn_t */
773
      NULL,    /* vpx_codec_get_preview_frame_fn_t */
774
      NULL,    /* vpx_codec_enc_mr_get_mem_loc_fn_t */
775
      NULL     /* vpx_codec_enc_mr_free_mem_loc_fn_t */
776
  }
777
};