1/*
2 *  Copyright (c) 2014 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#include "third_party/googletest/src/include/gtest/gtest.h"
11
12#include "test/ivf_video_source.h"
13#include "./vpx_config.h"
14#include "vpx/vp8dx.h"
15#include "vpx/vpx_decoder.h"
16
17namespace {
18
19#define NELEMENTS(x) static_cast<int>(sizeof(x) / sizeof(x[0]))
20
21TEST(DecodeAPI, InvalidParams) {
22  static const vpx_codec_iface_t *kCodecs[] = {
23#if CONFIG_VP8_DECODER
24    &vpx_codec_vp8_dx_algo,
25#endif
26#if CONFIG_VP9_DECODER
27    &vpx_codec_vp9_dx_algo,
28#endif
29  };
30  uint8_t buf[1] = {0};
31  vpx_codec_ctx_t dec;
32
33  EXPECT_EQ(VPX_CODEC_INVALID_PARAM, vpx_codec_dec_init(NULL, NULL, NULL, 0));
34  EXPECT_EQ(VPX_CODEC_INVALID_PARAM, vpx_codec_dec_init(&dec, NULL, NULL, 0));
35  EXPECT_EQ(VPX_CODEC_INVALID_PARAM, vpx_codec_decode(NULL, NULL, 0, NULL, 0));
36  EXPECT_EQ(VPX_CODEC_INVALID_PARAM, vpx_codec_decode(NULL, buf, 0, NULL, 0));
37  EXPECT_EQ(VPX_CODEC_INVALID_PARAM,
38            vpx_codec_decode(NULL, buf, NELEMENTS(buf), NULL, 0));
39  EXPECT_EQ(VPX_CODEC_INVALID_PARAM,
40            vpx_codec_decode(NULL, NULL, NELEMENTS(buf), NULL, 0));
41  EXPECT_EQ(VPX_CODEC_INVALID_PARAM, vpx_codec_destroy(NULL));
42  EXPECT_TRUE(vpx_codec_error(NULL) != NULL);
43
44  for (int i = 0; i < NELEMENTS(kCodecs); ++i) {
45    EXPECT_EQ(VPX_CODEC_INVALID_PARAM,
46              vpx_codec_dec_init(NULL, kCodecs[i], NULL, 0));
47
48    EXPECT_EQ(VPX_CODEC_OK, vpx_codec_dec_init(&dec, kCodecs[i], NULL, 0));
49    EXPECT_EQ(VPX_CODEC_UNSUP_BITSTREAM,
50              vpx_codec_decode(&dec, buf, NELEMENTS(buf), NULL, 0));
51    EXPECT_EQ(VPX_CODEC_INVALID_PARAM,
52              vpx_codec_decode(&dec, NULL, NELEMENTS(buf), NULL, 0));
53    EXPECT_EQ(VPX_CODEC_INVALID_PARAM,
54              vpx_codec_decode(&dec, buf, 0, NULL, 0));
55
56    EXPECT_EQ(VPX_CODEC_OK, vpx_codec_destroy(&dec));
57  }
58}
59
60#if CONFIG_VP9_DECODER
61// Test VP9 codec controls after a decode error to ensure the code doesn't
62// misbehave.
63void TestVp9Controls(vpx_codec_ctx_t *dec) {
64  static const int kControls[] = {
65    VP8D_GET_LAST_REF_UPDATES,
66    VP8D_GET_FRAME_CORRUPTED,
67    VP9D_GET_DISPLAY_SIZE,
68  };
69  int val[2];
70
71  for (int i = 0; i < NELEMENTS(kControls); ++i) {
72    const vpx_codec_err_t res = vpx_codec_control_(dec, kControls[i], val);
73    switch (kControls[i]) {
74      case VP8D_GET_FRAME_CORRUPTED:
75        EXPECT_EQ(VPX_CODEC_ERROR, res) << kControls[i];
76        break;
77      default:
78        EXPECT_EQ(VPX_CODEC_OK, res) << kControls[i];
79        break;
80    }
81    EXPECT_EQ(VPX_CODEC_INVALID_PARAM,
82              vpx_codec_control_(dec, kControls[i], NULL));
83  }
84
85  vp9_ref_frame_t ref;
86  ref.idx = 0;
87  EXPECT_EQ(VPX_CODEC_ERROR, vpx_codec_control(dec, VP9_GET_REFERENCE, &ref));
88  EXPECT_EQ(VPX_CODEC_INVALID_PARAM,
89            vpx_codec_control(dec, VP9_GET_REFERENCE, NULL));
90
91  vpx_ref_frame_t ref_copy;
92  const int width = 352;
93  const int height = 288;
94  ASSERT_TRUE(
95      vpx_img_alloc(&ref_copy.img, VPX_IMG_FMT_I420, width, height, 1) != NULL);
96  ref_copy.frame_type = VP8_LAST_FRAME;
97  EXPECT_EQ(VPX_CODEC_ERROR,
98            vpx_codec_control(dec, VP8_COPY_REFERENCE, &ref_copy));
99  EXPECT_EQ(VPX_CODEC_INVALID_PARAM,
100            vpx_codec_control(dec, VP8_COPY_REFERENCE, NULL));
101  vpx_img_free(&ref_copy.img);
102}
103
104TEST(DecodeAPI, Vp9InvalidDecode) {
105  const vpx_codec_iface_t *const codec = &vpx_codec_vp9_dx_algo;
106  const char filename[] =
107      "invalid-vp90-2-00-quantizer-00.webm.ivf.s5861_r01-05_b6-.v2.ivf";
108  libvpx_test::IVFVideoSource video(filename);
109  video.Init();
110  video.Begin();
111  ASSERT_TRUE(!HasFailure());
112
113  vpx_codec_ctx_t dec;
114  EXPECT_EQ(VPX_CODEC_OK, vpx_codec_dec_init(&dec, codec, NULL, 0));
115  const uint32_t frame_size = static_cast<uint32_t>(video.frame_size());
116  EXPECT_EQ(VPX_CODEC_MEM_ERROR,
117            vpx_codec_decode(&dec, video.cxdata(), frame_size, NULL, 0));
118  vpx_codec_iter_t iter = NULL;
119  EXPECT_EQ(NULL, vpx_codec_get_frame(&dec, &iter));
120
121  TestVp9Controls(&dec);
122  EXPECT_EQ(VPX_CODEC_OK, vpx_codec_destroy(&dec));
123}
124#endif  // CONFIG_VP9_DECODER
125
126}  // namespace
127