Merge changes I38e93fe2,I6d6a0fb6,I51155833,If4c3e5d4,Ia2f40ef2
[profile/ivi/libvpx.git] / test / encode_test_driver.cc
1 /*
2  *  Copyright (c) 2012 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 "test/encode_test_driver.h"
11 #include "test/video_source.h"
12 #include "third_party/googletest/src/include/gtest/gtest.h"
13
14 namespace libvpx_test {
15
16 void Encoder::EncodeFrame(VideoSource *video, unsigned long flags) {
17   if (video->img())
18     EncodeFrameInternal(*video, flags);
19   else
20     Flush();
21
22   // Handle twopass stats
23   CxDataIterator iter = GetCxData();
24
25   while (const vpx_codec_cx_pkt_t *pkt = iter.Next()) {
26     if (pkt->kind != VPX_CODEC_STATS_PKT)
27       continue;
28
29     stats_->Append(*pkt);
30   }
31 }
32
33 void Encoder::EncodeFrameInternal(const VideoSource &video,
34                                   unsigned long flags) {
35   vpx_codec_err_t res;
36   const vpx_image_t *img = video.img();
37
38   // Handle first frame initialization
39   if (!encoder_.priv) {
40     cfg_.g_w = img->d_w;
41     cfg_.g_h = img->d_h;
42     cfg_.g_timebase = video.timebase();
43     cfg_.rc_twopass_stats_in = stats_->buf();
44     res = vpx_codec_enc_init(&encoder_, &vpx_codec_vp8_cx_algo, &cfg_, 0);
45     ASSERT_EQ(VPX_CODEC_OK, res) << EncoderError();
46   }
47
48   // Handle frame resizing
49   if (cfg_.g_w != img->d_w || cfg_.g_h != img->d_h) {
50     cfg_.g_w = img->d_w;
51     cfg_.g_h = img->d_h;
52     res = vpx_codec_enc_config_set(&encoder_, &cfg_);
53     ASSERT_EQ(VPX_CODEC_OK, res) << EncoderError();
54   }
55
56   // Encode the frame
57   res = vpx_codec_encode(&encoder_,
58                          video.img(), video.pts(), video.duration(),
59                          flags, deadline_);
60   ASSERT_EQ(VPX_CODEC_OK, res) << EncoderError();
61 }
62
63 void Encoder::Flush() {
64   const vpx_codec_err_t res = vpx_codec_encode(&encoder_, NULL, 0, 0, 0,
65                                                deadline_);
66   ASSERT_EQ(VPX_CODEC_OK, res) << EncoderError();
67 }
68
69 void EncoderTest::SetMode(TestMode mode) {
70   switch (mode) {
71     case kRealTime:
72       deadline_ = VPX_DL_REALTIME;
73       break;
74
75     case kOnePassGood:
76     case kTwoPassGood:
77       deadline_ = VPX_DL_GOOD_QUALITY;
78       break;
79
80     case kOnePassBest:
81     case kTwoPassBest:
82       deadline_ = VPX_DL_BEST_QUALITY;
83       break;
84
85     default:
86       ASSERT_TRUE(false) << "Unexpected mode " << mode;
87   }
88
89   if (mode == kTwoPassGood || mode == kTwoPassBest)
90     passes_ = 2;
91   else
92     passes_ = 1;
93 }
94
95 void EncoderTest::RunLoop(VideoSource *video) {
96   for (unsigned int pass = 0; pass < passes_; pass++) {
97     if (passes_ == 1)
98       cfg_.g_pass = VPX_RC_ONE_PASS;
99     else if (pass == 0)
100       cfg_.g_pass = VPX_RC_FIRST_PASS;
101     else
102       cfg_.g_pass = VPX_RC_LAST_PASS;
103
104     BeginPassHook(pass);
105     Encoder encoder(cfg_, deadline_, &stats_);
106
107     bool again;
108
109     for (video->Begin(), again = true; again; video->Next()) {
110       again = video->img() != NULL;
111
112       PreEncodeFrameHook(video);
113       encoder.EncodeFrame(video, flags_);
114
115       CxDataIterator iter = encoder.GetCxData();
116
117       while (const vpx_codec_cx_pkt_t *pkt = iter.Next()) {
118         again = true;
119
120         if (pkt->kind != VPX_CODEC_CX_FRAME_PKT)
121           continue;
122
123         FramePktHook(pkt);
124       }
125
126       if (!Continue())
127         break;
128     }
129
130     EndPassHook();
131
132     if (!Continue())
133       break;
134   }
135 }
136
137 }  // namespace libvpx_test