svc_test.cc 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789
  1. /*
  2. * Copyright (c) 2013 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 <string>
  11. #include "third_party/googletest/src/include/gtest/gtest.h"
  12. #include "test/codec_factory.h"
  13. #include "test/decode_test_driver.h"
  14. #include "test/i420_video_source.h"
  15. #include "vp9/decoder/vp9_decoder.h"
  16. #include "vpx/svc_context.h"
  17. #include "vpx/vp8cx.h"
  18. #include "vpx/vpx_encoder.h"
  19. namespace {
  20. using libvpx_test::CodecFactory;
  21. using libvpx_test::Decoder;
  22. using libvpx_test::DxDataIterator;
  23. using libvpx_test::VP9CodecFactory;
  24. class SvcTest : public ::testing::Test {
  25. protected:
  26. static const uint32_t kWidth = 352;
  27. static const uint32_t kHeight = 288;
  28. SvcTest()
  29. : codec_iface_(0), test_file_name_("hantro_collage_w352h288.yuv"),
  30. codec_initialized_(false), decoder_(0) {
  31. memset(&svc_, 0, sizeof(svc_));
  32. memset(&codec_, 0, sizeof(codec_));
  33. memset(&codec_enc_, 0, sizeof(codec_enc_));
  34. }
  35. virtual ~SvcTest() {}
  36. virtual void SetUp() {
  37. svc_.log_level = SVC_LOG_DEBUG;
  38. svc_.log_print = 0;
  39. codec_iface_ = vpx_codec_vp9_cx();
  40. const vpx_codec_err_t res =
  41. vpx_codec_enc_config_default(codec_iface_, &codec_enc_, 0);
  42. EXPECT_EQ(VPX_CODEC_OK, res);
  43. codec_enc_.g_w = kWidth;
  44. codec_enc_.g_h = kHeight;
  45. codec_enc_.g_timebase.num = 1;
  46. codec_enc_.g_timebase.den = 60;
  47. codec_enc_.kf_min_dist = 100;
  48. codec_enc_.kf_max_dist = 100;
  49. vpx_codec_dec_cfg_t dec_cfg = vpx_codec_dec_cfg_t();
  50. VP9CodecFactory codec_factory;
  51. decoder_ = codec_factory.CreateDecoder(dec_cfg, 0);
  52. tile_columns_ = 0;
  53. tile_rows_ = 0;
  54. }
  55. virtual void TearDown() {
  56. ReleaseEncoder();
  57. delete (decoder_);
  58. }
  59. void InitializeEncoder() {
  60. const vpx_codec_err_t res =
  61. vpx_svc_init(&svc_, &codec_, vpx_codec_vp9_cx(), &codec_enc_);
  62. EXPECT_EQ(VPX_CODEC_OK, res);
  63. vpx_codec_control(&codec_, VP8E_SET_CPUUSED, 4); // Make the test faster
  64. vpx_codec_control(&codec_, VP9E_SET_TILE_COLUMNS, tile_columns_);
  65. vpx_codec_control(&codec_, VP9E_SET_TILE_ROWS, tile_rows_);
  66. codec_initialized_ = true;
  67. }
  68. void ReleaseEncoder() {
  69. vpx_svc_release(&svc_);
  70. if (codec_initialized_) vpx_codec_destroy(&codec_);
  71. codec_initialized_ = false;
  72. }
  73. void GetStatsData(std::string *const stats_buf) {
  74. vpx_codec_iter_t iter = NULL;
  75. const vpx_codec_cx_pkt_t *cx_pkt;
  76. while ((cx_pkt = vpx_codec_get_cx_data(&codec_, &iter)) != NULL) {
  77. if (cx_pkt->kind == VPX_CODEC_STATS_PKT) {
  78. EXPECT_GT(cx_pkt->data.twopass_stats.sz, 0U);
  79. ASSERT_TRUE(cx_pkt->data.twopass_stats.buf != NULL);
  80. stats_buf->append(static_cast<char *>(cx_pkt->data.twopass_stats.buf),
  81. cx_pkt->data.twopass_stats.sz);
  82. }
  83. }
  84. }
  85. void Pass1EncodeNFrames(const int n, const int layers,
  86. std::string *const stats_buf) {
  87. vpx_codec_err_t res;
  88. ASSERT_GT(n, 0);
  89. ASSERT_GT(layers, 0);
  90. svc_.spatial_layers = layers;
  91. codec_enc_.g_pass = VPX_RC_FIRST_PASS;
  92. InitializeEncoder();
  93. libvpx_test::I420VideoSource video(
  94. test_file_name_, codec_enc_.g_w, codec_enc_.g_h,
  95. codec_enc_.g_timebase.den, codec_enc_.g_timebase.num, 0, 30);
  96. video.Begin();
  97. for (int i = 0; i < n; ++i) {
  98. res = vpx_svc_encode(&svc_, &codec_, video.img(), video.pts(),
  99. video.duration(), VPX_DL_GOOD_QUALITY);
  100. ASSERT_EQ(VPX_CODEC_OK, res);
  101. GetStatsData(stats_buf);
  102. video.Next();
  103. }
  104. // Flush encoder and test EOS packet.
  105. res = vpx_svc_encode(&svc_, &codec_, NULL, video.pts(), video.duration(),
  106. VPX_DL_GOOD_QUALITY);
  107. ASSERT_EQ(VPX_CODEC_OK, res);
  108. GetStatsData(stats_buf);
  109. ReleaseEncoder();
  110. }
  111. void StoreFrames(const size_t max_frame_received,
  112. struct vpx_fixed_buf *const outputs,
  113. size_t *const frame_received) {
  114. vpx_codec_iter_t iter = NULL;
  115. const vpx_codec_cx_pkt_t *cx_pkt;
  116. while ((cx_pkt = vpx_codec_get_cx_data(&codec_, &iter)) != NULL) {
  117. if (cx_pkt->kind == VPX_CODEC_CX_FRAME_PKT) {
  118. const size_t frame_size = cx_pkt->data.frame.sz;
  119. EXPECT_GT(frame_size, 0U);
  120. ASSERT_TRUE(cx_pkt->data.frame.buf != NULL);
  121. ASSERT_LT(*frame_received, max_frame_received);
  122. if (*frame_received == 0)
  123. EXPECT_EQ(1, !!(cx_pkt->data.frame.flags & VPX_FRAME_IS_KEY));
  124. outputs[*frame_received].buf = malloc(frame_size + 16);
  125. ASSERT_TRUE(outputs[*frame_received].buf != NULL);
  126. memcpy(outputs[*frame_received].buf, cx_pkt->data.frame.buf,
  127. frame_size);
  128. outputs[*frame_received].sz = frame_size;
  129. ++(*frame_received);
  130. }
  131. }
  132. }
  133. void Pass2EncodeNFrames(std::string *const stats_buf, const int n,
  134. const int layers,
  135. struct vpx_fixed_buf *const outputs) {
  136. vpx_codec_err_t res;
  137. size_t frame_received = 0;
  138. ASSERT_TRUE(outputs != NULL);
  139. ASSERT_GT(n, 0);
  140. ASSERT_GT(layers, 0);
  141. svc_.spatial_layers = layers;
  142. codec_enc_.rc_target_bitrate = 500;
  143. if (codec_enc_.g_pass == VPX_RC_LAST_PASS) {
  144. ASSERT_TRUE(stats_buf != NULL);
  145. ASSERT_GT(stats_buf->size(), 0U);
  146. codec_enc_.rc_twopass_stats_in.buf = &(*stats_buf)[0];
  147. codec_enc_.rc_twopass_stats_in.sz = stats_buf->size();
  148. }
  149. InitializeEncoder();
  150. libvpx_test::I420VideoSource video(
  151. test_file_name_, codec_enc_.g_w, codec_enc_.g_h,
  152. codec_enc_.g_timebase.den, codec_enc_.g_timebase.num, 0, 30);
  153. video.Begin();
  154. for (int i = 0; i < n; ++i) {
  155. res = vpx_svc_encode(&svc_, &codec_, video.img(), video.pts(),
  156. video.duration(), VPX_DL_GOOD_QUALITY);
  157. ASSERT_EQ(VPX_CODEC_OK, res);
  158. StoreFrames(n, outputs, &frame_received);
  159. video.Next();
  160. }
  161. // Flush encoder.
  162. res = vpx_svc_encode(&svc_, &codec_, NULL, 0, video.duration(),
  163. VPX_DL_GOOD_QUALITY);
  164. EXPECT_EQ(VPX_CODEC_OK, res);
  165. StoreFrames(n, outputs, &frame_received);
  166. EXPECT_EQ(frame_received, static_cast<size_t>(n));
  167. ReleaseEncoder();
  168. }
  169. void DecodeNFrames(const struct vpx_fixed_buf *const inputs, const int n) {
  170. int decoded_frames = 0;
  171. int received_frames = 0;
  172. ASSERT_TRUE(inputs != NULL);
  173. ASSERT_GT(n, 0);
  174. for (int i = 0; i < n; ++i) {
  175. ASSERT_TRUE(inputs[i].buf != NULL);
  176. ASSERT_GT(inputs[i].sz, 0U);
  177. const vpx_codec_err_t res_dec = decoder_->DecodeFrame(
  178. static_cast<const uint8_t *>(inputs[i].buf), inputs[i].sz);
  179. ASSERT_EQ(VPX_CODEC_OK, res_dec) << decoder_->DecodeError();
  180. ++decoded_frames;
  181. DxDataIterator dec_iter = decoder_->GetDxData();
  182. while (dec_iter.Next() != NULL) {
  183. ++received_frames;
  184. }
  185. }
  186. EXPECT_EQ(decoded_frames, n);
  187. EXPECT_EQ(received_frames, n);
  188. }
  189. void DropEnhancementLayers(struct vpx_fixed_buf *const inputs,
  190. const int num_super_frames,
  191. const int remained_spatial_layers) {
  192. ASSERT_TRUE(inputs != NULL);
  193. ASSERT_GT(num_super_frames, 0);
  194. ASSERT_GT(remained_spatial_layers, 0);
  195. for (int i = 0; i < num_super_frames; ++i) {
  196. uint32_t frame_sizes[8] = { 0 };
  197. int frame_count = 0;
  198. int frames_found = 0;
  199. int frame;
  200. ASSERT_TRUE(inputs[i].buf != NULL);
  201. ASSERT_GT(inputs[i].sz, 0U);
  202. vpx_codec_err_t res = vp9_parse_superframe_index(
  203. static_cast<const uint8_t *>(inputs[i].buf), inputs[i].sz,
  204. frame_sizes, &frame_count, NULL, NULL);
  205. ASSERT_EQ(VPX_CODEC_OK, res);
  206. if (frame_count == 0) {
  207. // There's no super frame but only a single frame.
  208. ASSERT_EQ(1, remained_spatial_layers);
  209. } else {
  210. // Found a super frame.
  211. uint8_t *frame_data = static_cast<uint8_t *>(inputs[i].buf);
  212. uint8_t *frame_start = frame_data;
  213. for (frame = 0; frame < frame_count; ++frame) {
  214. // Looking for a visible frame.
  215. if (frame_data[0] & 0x02) {
  216. ++frames_found;
  217. if (frames_found == remained_spatial_layers) break;
  218. }
  219. frame_data += frame_sizes[frame];
  220. }
  221. ASSERT_LT(frame, frame_count)
  222. << "Couldn't find a visible frame. "
  223. << "remained_spatial_layers: " << remained_spatial_layers
  224. << " super_frame: " << i;
  225. if (frame == frame_count - 1) continue;
  226. frame_data += frame_sizes[frame];
  227. // We need to add one more frame for multiple frame contexts.
  228. uint8_t marker =
  229. static_cast<const uint8_t *>(inputs[i].buf)[inputs[i].sz - 1];
  230. const uint32_t mag = ((marker >> 3) & 0x3) + 1;
  231. const size_t index_sz = 2 + mag * frame_count;
  232. const size_t new_index_sz = 2 + mag * (frame + 1);
  233. marker &= 0x0f8;
  234. marker |= frame;
  235. // Copy existing frame sizes.
  236. memmove(frame_data + 1, frame_start + inputs[i].sz - index_sz + 1,
  237. new_index_sz - 2);
  238. // New marker.
  239. frame_data[0] = marker;
  240. frame_data += (mag * (frame + 1) + 1);
  241. *frame_data++ = marker;
  242. inputs[i].sz = frame_data - frame_start;
  243. }
  244. }
  245. }
  246. void FreeBitstreamBuffers(struct vpx_fixed_buf *const inputs, const int n) {
  247. ASSERT_TRUE(inputs != NULL);
  248. ASSERT_GT(n, 0);
  249. for (int i = 0; i < n; ++i) {
  250. free(inputs[i].buf);
  251. inputs[i].buf = NULL;
  252. inputs[i].sz = 0;
  253. }
  254. }
  255. SvcContext svc_;
  256. vpx_codec_ctx_t codec_;
  257. struct vpx_codec_enc_cfg codec_enc_;
  258. vpx_codec_iface_t *codec_iface_;
  259. std::string test_file_name_;
  260. bool codec_initialized_;
  261. Decoder *decoder_;
  262. int tile_columns_;
  263. int tile_rows_;
  264. };
  265. TEST_F(SvcTest, SvcInit) {
  266. // test missing parameters
  267. vpx_codec_err_t res = vpx_svc_init(NULL, &codec_, codec_iface_, &codec_enc_);
  268. EXPECT_EQ(VPX_CODEC_INVALID_PARAM, res);
  269. res = vpx_svc_init(&svc_, NULL, codec_iface_, &codec_enc_);
  270. EXPECT_EQ(VPX_CODEC_INVALID_PARAM, res);
  271. res = vpx_svc_init(&svc_, &codec_, NULL, &codec_enc_);
  272. EXPECT_EQ(VPX_CODEC_INVALID_PARAM, res);
  273. res = vpx_svc_init(&svc_, &codec_, codec_iface_, NULL);
  274. EXPECT_EQ(VPX_CODEC_INVALID_PARAM, res);
  275. svc_.spatial_layers = 6; // too many layers
  276. res = vpx_svc_init(&svc_, &codec_, codec_iface_, &codec_enc_);
  277. EXPECT_EQ(VPX_CODEC_INVALID_PARAM, res);
  278. svc_.spatial_layers = 0; // use default layers
  279. InitializeEncoder();
  280. EXPECT_EQ(VPX_SS_DEFAULT_LAYERS, svc_.spatial_layers);
  281. }
  282. TEST_F(SvcTest, InitTwoLayers) {
  283. svc_.spatial_layers = 2;
  284. InitializeEncoder();
  285. }
  286. TEST_F(SvcTest, InvalidOptions) {
  287. vpx_codec_err_t res = vpx_svc_set_options(&svc_, NULL);
  288. EXPECT_EQ(VPX_CODEC_INVALID_PARAM, res);
  289. res = vpx_svc_set_options(&svc_, "not-an-option=1");
  290. EXPECT_EQ(VPX_CODEC_OK, res);
  291. res = vpx_svc_init(&svc_, &codec_, vpx_codec_vp9_cx(), &codec_enc_);
  292. EXPECT_EQ(VPX_CODEC_INVALID_PARAM, res);
  293. }
  294. TEST_F(SvcTest, SetLayersOption) {
  295. vpx_codec_err_t res = vpx_svc_set_options(&svc_, "spatial-layers=3");
  296. EXPECT_EQ(VPX_CODEC_OK, res);
  297. InitializeEncoder();
  298. EXPECT_EQ(3, svc_.spatial_layers);
  299. }
  300. TEST_F(SvcTest, SetMultipleOptions) {
  301. vpx_codec_err_t res =
  302. vpx_svc_set_options(&svc_, "spatial-layers=2 scale-factors=1/3,2/3");
  303. EXPECT_EQ(VPX_CODEC_OK, res);
  304. InitializeEncoder();
  305. EXPECT_EQ(2, svc_.spatial_layers);
  306. }
  307. TEST_F(SvcTest, SetScaleFactorsOption) {
  308. svc_.spatial_layers = 2;
  309. vpx_codec_err_t res =
  310. vpx_svc_set_options(&svc_, "scale-factors=not-scale-factors");
  311. EXPECT_EQ(VPX_CODEC_OK, res);
  312. res = vpx_svc_init(&svc_, &codec_, vpx_codec_vp9_cx(), &codec_enc_);
  313. EXPECT_EQ(VPX_CODEC_INVALID_PARAM, res);
  314. res = vpx_svc_set_options(&svc_, "scale-factors=1/3, 3*3");
  315. EXPECT_EQ(VPX_CODEC_OK, res);
  316. res = vpx_svc_init(&svc_, &codec_, vpx_codec_vp9_cx(), &codec_enc_);
  317. EXPECT_EQ(VPX_CODEC_INVALID_PARAM, res);
  318. res = vpx_svc_set_options(&svc_, "scale-factors=1/3");
  319. EXPECT_EQ(VPX_CODEC_OK, res);
  320. res = vpx_svc_init(&svc_, &codec_, vpx_codec_vp9_cx(), &codec_enc_);
  321. EXPECT_EQ(VPX_CODEC_INVALID_PARAM, res);
  322. res = vpx_svc_set_options(&svc_, "scale-factors=1/3,2/3");
  323. EXPECT_EQ(VPX_CODEC_OK, res);
  324. InitializeEncoder();
  325. }
  326. TEST_F(SvcTest, SetQuantizersOption) {
  327. svc_.spatial_layers = 2;
  328. vpx_codec_err_t res = vpx_svc_set_options(&svc_, "max-quantizers=nothing");
  329. EXPECT_EQ(VPX_CODEC_OK, res);
  330. res = vpx_svc_init(&svc_, &codec_, vpx_codec_vp9_cx(), &codec_enc_);
  331. EXPECT_EQ(VPX_CODEC_INVALID_PARAM, res);
  332. res = vpx_svc_set_options(&svc_, "min-quantizers=nothing");
  333. EXPECT_EQ(VPX_CODEC_OK, res);
  334. res = vpx_svc_init(&svc_, &codec_, vpx_codec_vp9_cx(), &codec_enc_);
  335. EXPECT_EQ(VPX_CODEC_INVALID_PARAM, res);
  336. res = vpx_svc_set_options(&svc_, "max-quantizers=40");
  337. EXPECT_EQ(VPX_CODEC_OK, res);
  338. res = vpx_svc_init(&svc_, &codec_, vpx_codec_vp9_cx(), &codec_enc_);
  339. EXPECT_EQ(VPX_CODEC_INVALID_PARAM, res);
  340. res = vpx_svc_set_options(&svc_, "min-quantizers=40");
  341. EXPECT_EQ(VPX_CODEC_OK, res);
  342. res = vpx_svc_init(&svc_, &codec_, vpx_codec_vp9_cx(), &codec_enc_);
  343. EXPECT_EQ(VPX_CODEC_INVALID_PARAM, res);
  344. res = vpx_svc_set_options(&svc_, "max-quantizers=30,30 min-quantizers=40,40");
  345. EXPECT_EQ(VPX_CODEC_OK, res);
  346. res = vpx_svc_init(&svc_, &codec_, vpx_codec_vp9_cx(), &codec_enc_);
  347. EXPECT_EQ(VPX_CODEC_INVALID_PARAM, res);
  348. res = vpx_svc_set_options(&svc_, "max-quantizers=40,40 min-quantizers=30,30");
  349. InitializeEncoder();
  350. }
  351. TEST_F(SvcTest, SetAutoAltRefOption) {
  352. svc_.spatial_layers = 5;
  353. vpx_codec_err_t res = vpx_svc_set_options(&svc_, "auto-alt-refs=none");
  354. EXPECT_EQ(VPX_CODEC_OK, res);
  355. res = vpx_svc_init(&svc_, &codec_, vpx_codec_vp9_cx(), &codec_enc_);
  356. EXPECT_EQ(VPX_CODEC_INVALID_PARAM, res);
  357. res = vpx_svc_set_options(&svc_, "auto-alt-refs=1,1,1,1,0");
  358. EXPECT_EQ(VPX_CODEC_OK, res);
  359. res = vpx_svc_init(&svc_, &codec_, vpx_codec_vp9_cx(), &codec_enc_);
  360. EXPECT_EQ(VPX_CODEC_INVALID_PARAM, res);
  361. vpx_svc_set_options(&svc_, "auto-alt-refs=0,1,1,1,0");
  362. InitializeEncoder();
  363. }
  364. // Test that decoder can handle an SVC frame as the first frame in a sequence.
  365. TEST_F(SvcTest, OnePassEncodeOneFrame) {
  366. codec_enc_.g_pass = VPX_RC_ONE_PASS;
  367. vpx_fixed_buf output = vpx_fixed_buf();
  368. Pass2EncodeNFrames(NULL, 1, 2, &output);
  369. DecodeNFrames(&output, 1);
  370. FreeBitstreamBuffers(&output, 1);
  371. }
  372. TEST_F(SvcTest, OnePassEncodeThreeFrames) {
  373. codec_enc_.g_pass = VPX_RC_ONE_PASS;
  374. codec_enc_.g_lag_in_frames = 0;
  375. vpx_fixed_buf outputs[3];
  376. memset(&outputs[0], 0, sizeof(outputs));
  377. Pass2EncodeNFrames(NULL, 3, 2, &outputs[0]);
  378. DecodeNFrames(&outputs[0], 3);
  379. FreeBitstreamBuffers(&outputs[0], 3);
  380. }
  381. TEST_F(SvcTest, TwoPassEncode10Frames) {
  382. // First pass encode
  383. std::string stats_buf;
  384. Pass1EncodeNFrames(10, 2, &stats_buf);
  385. // Second pass encode
  386. codec_enc_.g_pass = VPX_RC_LAST_PASS;
  387. vpx_fixed_buf outputs[10];
  388. memset(&outputs[0], 0, sizeof(outputs));
  389. Pass2EncodeNFrames(&stats_buf, 10, 2, &outputs[0]);
  390. DecodeNFrames(&outputs[0], 10);
  391. FreeBitstreamBuffers(&outputs[0], 10);
  392. }
  393. TEST_F(SvcTest, TwoPassEncode20FramesWithAltRef) {
  394. // First pass encode
  395. std::string stats_buf;
  396. Pass1EncodeNFrames(20, 2, &stats_buf);
  397. // Second pass encode
  398. codec_enc_.g_pass = VPX_RC_LAST_PASS;
  399. vpx_svc_set_options(&svc_, "auto-alt-refs=1,1");
  400. vpx_fixed_buf outputs[20];
  401. memset(&outputs[0], 0, sizeof(outputs));
  402. Pass2EncodeNFrames(&stats_buf, 20, 2, &outputs[0]);
  403. DecodeNFrames(&outputs[0], 20);
  404. FreeBitstreamBuffers(&outputs[0], 20);
  405. }
  406. TEST_F(SvcTest, TwoPassEncode2SpatialLayersDecodeBaseLayerOnly) {
  407. // First pass encode
  408. std::string stats_buf;
  409. Pass1EncodeNFrames(10, 2, &stats_buf);
  410. // Second pass encode
  411. codec_enc_.g_pass = VPX_RC_LAST_PASS;
  412. vpx_svc_set_options(&svc_, "auto-alt-refs=1,1");
  413. vpx_fixed_buf outputs[10];
  414. memset(&outputs[0], 0, sizeof(outputs));
  415. Pass2EncodeNFrames(&stats_buf, 10, 2, &outputs[0]);
  416. DropEnhancementLayers(&outputs[0], 10, 1);
  417. DecodeNFrames(&outputs[0], 10);
  418. FreeBitstreamBuffers(&outputs[0], 10);
  419. }
  420. TEST_F(SvcTest, TwoPassEncode5SpatialLayersDecode54321Layers) {
  421. // First pass encode
  422. std::string stats_buf;
  423. Pass1EncodeNFrames(10, 5, &stats_buf);
  424. // Second pass encode
  425. codec_enc_.g_pass = VPX_RC_LAST_PASS;
  426. vpx_svc_set_options(&svc_, "auto-alt-refs=0,1,1,1,0");
  427. vpx_fixed_buf outputs[10];
  428. memset(&outputs[0], 0, sizeof(outputs));
  429. Pass2EncodeNFrames(&stats_buf, 10, 5, &outputs[0]);
  430. DecodeNFrames(&outputs[0], 10);
  431. DropEnhancementLayers(&outputs[0], 10, 4);
  432. DecodeNFrames(&outputs[0], 10);
  433. DropEnhancementLayers(&outputs[0], 10, 3);
  434. DecodeNFrames(&outputs[0], 10);
  435. DropEnhancementLayers(&outputs[0], 10, 2);
  436. DecodeNFrames(&outputs[0], 10);
  437. DropEnhancementLayers(&outputs[0], 10, 1);
  438. DecodeNFrames(&outputs[0], 10);
  439. FreeBitstreamBuffers(&outputs[0], 10);
  440. }
  441. TEST_F(SvcTest, TwoPassEncode2SNRLayers) {
  442. // First pass encode
  443. std::string stats_buf;
  444. vpx_svc_set_options(&svc_, "scale-factors=1/1,1/1");
  445. Pass1EncodeNFrames(20, 2, &stats_buf);
  446. // Second pass encode
  447. codec_enc_.g_pass = VPX_RC_LAST_PASS;
  448. vpx_svc_set_options(&svc_, "auto-alt-refs=1,1 scale-factors=1/1,1/1");
  449. vpx_fixed_buf outputs[20];
  450. memset(&outputs[0], 0, sizeof(outputs));
  451. Pass2EncodeNFrames(&stats_buf, 20, 2, &outputs[0]);
  452. DecodeNFrames(&outputs[0], 20);
  453. FreeBitstreamBuffers(&outputs[0], 20);
  454. }
  455. TEST_F(SvcTest, TwoPassEncode3SNRLayersDecode321Layers) {
  456. // First pass encode
  457. std::string stats_buf;
  458. vpx_svc_set_options(&svc_, "scale-factors=1/1,1/1,1/1");
  459. Pass1EncodeNFrames(20, 3, &stats_buf);
  460. // Second pass encode
  461. codec_enc_.g_pass = VPX_RC_LAST_PASS;
  462. vpx_svc_set_options(&svc_, "auto-alt-refs=1,1,1 scale-factors=1/1,1/1,1/1");
  463. vpx_fixed_buf outputs[20];
  464. memset(&outputs[0], 0, sizeof(outputs));
  465. Pass2EncodeNFrames(&stats_buf, 20, 3, &outputs[0]);
  466. DecodeNFrames(&outputs[0], 20);
  467. DropEnhancementLayers(&outputs[0], 20, 2);
  468. DecodeNFrames(&outputs[0], 20);
  469. DropEnhancementLayers(&outputs[0], 20, 1);
  470. DecodeNFrames(&outputs[0], 20);
  471. FreeBitstreamBuffers(&outputs[0], 20);
  472. }
  473. TEST_F(SvcTest, SetMultipleFrameContextsOption) {
  474. svc_.spatial_layers = 5;
  475. vpx_codec_err_t res = vpx_svc_set_options(&svc_, "multi-frame-contexts=1");
  476. EXPECT_EQ(VPX_CODEC_OK, res);
  477. res = vpx_svc_init(&svc_, &codec_, vpx_codec_vp9_cx(), &codec_enc_);
  478. EXPECT_EQ(VPX_CODEC_INVALID_PARAM, res);
  479. svc_.spatial_layers = 2;
  480. res = vpx_svc_set_options(&svc_, "multi-frame-contexts=1");
  481. InitializeEncoder();
  482. }
  483. TEST_F(SvcTest, TwoPassEncode2SpatialLayersWithMultipleFrameContexts) {
  484. // First pass encode
  485. std::string stats_buf;
  486. Pass1EncodeNFrames(10, 2, &stats_buf);
  487. // Second pass encode
  488. codec_enc_.g_pass = VPX_RC_LAST_PASS;
  489. codec_enc_.g_error_resilient = 0;
  490. vpx_svc_set_options(&svc_, "auto-alt-refs=1,1 multi-frame-contexts=1");
  491. vpx_fixed_buf outputs[10];
  492. memset(&outputs[0], 0, sizeof(outputs));
  493. Pass2EncodeNFrames(&stats_buf, 10, 2, &outputs[0]);
  494. DecodeNFrames(&outputs[0], 10);
  495. FreeBitstreamBuffers(&outputs[0], 10);
  496. }
  497. TEST_F(SvcTest,
  498. TwoPassEncode2SpatialLayersWithMultipleFrameContextsDecodeBaselayer) {
  499. // First pass encode
  500. std::string stats_buf;
  501. Pass1EncodeNFrames(10, 2, &stats_buf);
  502. // Second pass encode
  503. codec_enc_.g_pass = VPX_RC_LAST_PASS;
  504. codec_enc_.g_error_resilient = 0;
  505. vpx_svc_set_options(&svc_, "auto-alt-refs=1,1 multi-frame-contexts=1");
  506. vpx_fixed_buf outputs[10];
  507. memset(&outputs[0], 0, sizeof(outputs));
  508. Pass2EncodeNFrames(&stats_buf, 10, 2, &outputs[0]);
  509. DropEnhancementLayers(&outputs[0], 10, 1);
  510. DecodeNFrames(&outputs[0], 10);
  511. FreeBitstreamBuffers(&outputs[0], 10);
  512. }
  513. TEST_F(SvcTest, TwoPassEncode2SNRLayersWithMultipleFrameContexts) {
  514. // First pass encode
  515. std::string stats_buf;
  516. vpx_svc_set_options(&svc_, "scale-factors=1/1,1/1");
  517. Pass1EncodeNFrames(10, 2, &stats_buf);
  518. // Second pass encode
  519. codec_enc_.g_pass = VPX_RC_LAST_PASS;
  520. codec_enc_.g_error_resilient = 0;
  521. vpx_svc_set_options(&svc_,
  522. "auto-alt-refs=1,1 scale-factors=1/1,1/1 "
  523. "multi-frame-contexts=1");
  524. vpx_fixed_buf outputs[10];
  525. memset(&outputs[0], 0, sizeof(outputs));
  526. Pass2EncodeNFrames(&stats_buf, 10, 2, &outputs[0]);
  527. DecodeNFrames(&outputs[0], 10);
  528. FreeBitstreamBuffers(&outputs[0], 10);
  529. }
  530. TEST_F(SvcTest,
  531. TwoPassEncode3SNRLayersWithMultipleFrameContextsDecode321Layer) {
  532. // First pass encode
  533. std::string stats_buf;
  534. vpx_svc_set_options(&svc_, "scale-factors=1/1,1/1,1/1");
  535. Pass1EncodeNFrames(10, 3, &stats_buf);
  536. // Second pass encode
  537. codec_enc_.g_pass = VPX_RC_LAST_PASS;
  538. codec_enc_.g_error_resilient = 0;
  539. vpx_svc_set_options(&svc_,
  540. "auto-alt-refs=1,1,1 scale-factors=1/1,1/1,1/1 "
  541. "multi-frame-contexts=1");
  542. vpx_fixed_buf outputs[10];
  543. memset(&outputs[0], 0, sizeof(outputs));
  544. Pass2EncodeNFrames(&stats_buf, 10, 3, &outputs[0]);
  545. DecodeNFrames(&outputs[0], 10);
  546. DropEnhancementLayers(&outputs[0], 10, 2);
  547. DecodeNFrames(&outputs[0], 10);
  548. DropEnhancementLayers(&outputs[0], 10, 1);
  549. DecodeNFrames(&outputs[0], 10);
  550. FreeBitstreamBuffers(&outputs[0], 10);
  551. }
  552. TEST_F(SvcTest, TwoPassEncode2TemporalLayers) {
  553. // First pass encode
  554. std::string stats_buf;
  555. vpx_svc_set_options(&svc_, "scale-factors=1/1");
  556. svc_.temporal_layers = 2;
  557. Pass1EncodeNFrames(10, 1, &stats_buf);
  558. // Second pass encode
  559. codec_enc_.g_pass = VPX_RC_LAST_PASS;
  560. svc_.temporal_layers = 2;
  561. vpx_svc_set_options(&svc_, "auto-alt-refs=1 scale-factors=1/1");
  562. vpx_fixed_buf outputs[10];
  563. memset(&outputs[0], 0, sizeof(outputs));
  564. Pass2EncodeNFrames(&stats_buf, 10, 1, &outputs[0]);
  565. DecodeNFrames(&outputs[0], 10);
  566. FreeBitstreamBuffers(&outputs[0], 10);
  567. }
  568. TEST_F(SvcTest, TwoPassEncode2TemporalLayersWithMultipleFrameContexts) {
  569. // First pass encode
  570. std::string stats_buf;
  571. vpx_svc_set_options(&svc_, "scale-factors=1/1");
  572. svc_.temporal_layers = 2;
  573. Pass1EncodeNFrames(10, 1, &stats_buf);
  574. // Second pass encode
  575. codec_enc_.g_pass = VPX_RC_LAST_PASS;
  576. svc_.temporal_layers = 2;
  577. codec_enc_.g_error_resilient = 0;
  578. vpx_svc_set_options(&svc_,
  579. "auto-alt-refs=1 scale-factors=1/1 "
  580. "multi-frame-contexts=1");
  581. vpx_fixed_buf outputs[10];
  582. memset(&outputs[0], 0, sizeof(outputs));
  583. Pass2EncodeNFrames(&stats_buf, 10, 1, &outputs[0]);
  584. DecodeNFrames(&outputs[0], 10);
  585. FreeBitstreamBuffers(&outputs[0], 10);
  586. }
  587. TEST_F(SvcTest, TwoPassEncode2TemporalLayersDecodeBaseLayer) {
  588. // First pass encode
  589. std::string stats_buf;
  590. vpx_svc_set_options(&svc_, "scale-factors=1/1");
  591. svc_.temporal_layers = 2;
  592. Pass1EncodeNFrames(10, 1, &stats_buf);
  593. // Second pass encode
  594. codec_enc_.g_pass = VPX_RC_LAST_PASS;
  595. svc_.temporal_layers = 2;
  596. vpx_svc_set_options(&svc_, "auto-alt-refs=1 scale-factors=1/1");
  597. vpx_fixed_buf outputs[10];
  598. memset(&outputs[0], 0, sizeof(outputs));
  599. Pass2EncodeNFrames(&stats_buf, 10, 1, &outputs[0]);
  600. vpx_fixed_buf base_layer[5];
  601. for (int i = 0; i < 5; ++i) base_layer[i] = outputs[i * 2];
  602. DecodeNFrames(&base_layer[0], 5);
  603. FreeBitstreamBuffers(&outputs[0], 10);
  604. }
  605. TEST_F(SvcTest,
  606. TwoPassEncode2TemporalLayersWithMultipleFrameContextsDecodeBaseLayer) {
  607. // First pass encode
  608. std::string stats_buf;
  609. vpx_svc_set_options(&svc_, "scale-factors=1/1");
  610. svc_.temporal_layers = 2;
  611. Pass1EncodeNFrames(10, 1, &stats_buf);
  612. // Second pass encode
  613. codec_enc_.g_pass = VPX_RC_LAST_PASS;
  614. svc_.temporal_layers = 2;
  615. codec_enc_.g_error_resilient = 0;
  616. vpx_svc_set_options(&svc_,
  617. "auto-alt-refs=1 scale-factors=1/1 "
  618. "multi-frame-contexts=1");
  619. vpx_fixed_buf outputs[10];
  620. memset(&outputs[0], 0, sizeof(outputs));
  621. Pass2EncodeNFrames(&stats_buf, 10, 1, &outputs[0]);
  622. vpx_fixed_buf base_layer[5];
  623. for (int i = 0; i < 5; ++i) base_layer[i] = outputs[i * 2];
  624. DecodeNFrames(&base_layer[0], 5);
  625. FreeBitstreamBuffers(&outputs[0], 10);
  626. }
  627. TEST_F(SvcTest, TwoPassEncode2TemporalLayersWithTiles) {
  628. // First pass encode
  629. std::string stats_buf;
  630. vpx_svc_set_options(&svc_, "scale-factors=1/1");
  631. svc_.temporal_layers = 2;
  632. Pass1EncodeNFrames(10, 1, &stats_buf);
  633. // Second pass encode
  634. codec_enc_.g_pass = VPX_RC_LAST_PASS;
  635. svc_.temporal_layers = 2;
  636. vpx_svc_set_options(&svc_, "auto-alt-refs=1 scale-factors=1/1");
  637. codec_enc_.g_w = 704;
  638. codec_enc_.g_h = 144;
  639. tile_columns_ = 1;
  640. tile_rows_ = 1;
  641. vpx_fixed_buf outputs[10];
  642. memset(&outputs[0], 0, sizeof(outputs));
  643. Pass2EncodeNFrames(&stats_buf, 10, 1, &outputs[0]);
  644. DecodeNFrames(&outputs[0], 10);
  645. FreeBitstreamBuffers(&outputs[0], 10);
  646. }
  647. TEST_F(SvcTest, TwoPassEncode2TemporalLayersWithMultipleFrameContextsAndTiles) {
  648. // First pass encode
  649. std::string stats_buf;
  650. vpx_svc_set_options(&svc_, "scale-factors=1/1");
  651. svc_.temporal_layers = 2;
  652. Pass1EncodeNFrames(10, 1, &stats_buf);
  653. // Second pass encode
  654. codec_enc_.g_pass = VPX_RC_LAST_PASS;
  655. svc_.temporal_layers = 2;
  656. codec_enc_.g_error_resilient = 0;
  657. codec_enc_.g_w = 704;
  658. codec_enc_.g_h = 144;
  659. tile_columns_ = 1;
  660. tile_rows_ = 1;
  661. vpx_svc_set_options(&svc_,
  662. "auto-alt-refs=1 scale-factors=1/1 "
  663. "multi-frame-contexts=1");
  664. vpx_fixed_buf outputs[10];
  665. memset(&outputs[0], 0, sizeof(outputs));
  666. Pass2EncodeNFrames(&stats_buf, 10, 1, &outputs[0]);
  667. DecodeNFrames(&outputs[0], 10);
  668. FreeBitstreamBuffers(&outputs[0], 10);
  669. }
  670. } // namespace