ref: 422445a81d4c0da111cbd2d069f37243dfaea893
parent: 9330bd71a10a216aa0098b2a7a6e17c40c247c27
author: angiebird <[email protected]>
date: Sun Nov 10 15:36:04 EST 2019
Add SimpleEncode::GetCodingFrameNum() Also add unit tests for GetCodingFrameNum() and EncodeFrame() Change-Id: I3e7b65f47226be4660409481435f8f784db72a68
--- a/test/simple_encode_test.cc
+++ b/test/simple_encode_test.cc
@@ -33,4 +33,45 @@
}
}
+TEST(SimpleEncode, GetCodingFrameNum) {
+ int w = 352;
+ int h = 288;
+ int frame_rate_num = 30;
+ int frame_rate_den = 1;
+ int target_bitrate = 200;
+ int num_frames = 17;
+ // TODO(angiebird): Figure out how to upload test video to our codebase
+ FILE *file = fopen("bus_352x288_420_f20_b8.yuv", "r");
+ SimpleEncode simple_encode(w, h, frame_rate_num, frame_rate_den,
+ target_bitrate, num_frames, file);
+ simple_encode.ComputeFirstPassStats();
+ int num_coding_frames = simple_encode.GetCodingFrameNum();
+ EXPECT_EQ(num_coding_frames, 19);
+}
+
+TEST(SimpleEncode, EncodeFrame) {
+ int w = 352;
+ int h = 288;
+ int frame_rate_num = 30;
+ int frame_rate_den = 1;
+ int target_bitrate = 200;
+ int num_frames = 17;
+ // TODO(angiebird): Figure out how to upload test video to our codebase
+ FILE *file = fopen("bus_352x288_420_f20_b8.yuv", "r");
+ SimpleEncode simple_encode(w, h, frame_rate_num, frame_rate_den,
+ target_bitrate, num_frames, file);
+ char cx_data[352 * 288 * 3];
+ size_t max_size = 352 * 288 * 3;
+ size_t frame_size;
+ simple_encode.ComputeFirstPassStats();
+ int num_coding_frames = simple_encode.GetCodingFrameNum();
+ simple_encode.StartEncode();
+ for (int i = 0; i < num_coding_frames; ++i) {
+ simple_encode.EncodeFrame(cx_data, &frame_size, max_size);
+ // TODO(angiebird): For now, this test just check whether EncodeFrame can be
+ // run proprly. Add extra check later.
+ }
+ simple_encode.EndEncode();
+}
+
} // namespace
--- a/vp9/simple_encode.cc
+++ b/vp9/simple_encode.cc
@@ -223,4 +223,20 @@
}
}
+int SimpleEncode::GetCodingFrameNum() {
+ assert(pimpl->frame_stats.size() - 1 > 0);
+ // These are the default settings for now.
+ const int multi_layer_arf = 0;
+ const int allow_alt_ref = 1;
+ vpx_rational_t frame_rate = make_vpx_rational(frame_rate_num, frame_rate_den);
+ const VP9EncoderConfig oxcf = vp9_get_encoder_config(
+ frame_width, frame_height, frame_rate, target_bitrate, VPX_RC_LAST_PASS);
+ FRAME_INFO frame_info = vp9_get_frame_info(&oxcf);
+ FIRST_PASS_INFO first_pass_info;
+ fps_init_first_pass_info(&first_pass_info, pimpl->frame_stats.data(),
+ num_frames);
+ return vp9_get_coding_frame_num(&oxcf, &frame_info, &first_pass_info,
+ multi_layer_arf, allow_alt_ref);
+}
+
SimpleEncode::~SimpleEncode() {}
--- a/vp9/simple_encode.h
+++ b/vp9/simple_encode.h
@@ -17,13 +17,21 @@
std::vector<std::vector<double>> ObserveFirstPassStats();
// Initialize the encoder for actual encoding
+ // This funtion should be called after ComputeFirstPassStats()
void StartEncode();
// Free the encoder
+ // This funtion should be called after StartEncode() or EncodeFrame()
void EndEncode();
// Encode a frame
+ // This funtion should be called after StartEncode() before EndEncode()
void EncodeFrame(char *cx_data, size_t *size, size_t max_size);
+
+ // Get the number of coding frames for the video. The coding frames include
+ // show frame and no show frame.
+ // This funtion should be called after ComputeFirstPassStats()
+ int GetCodingFrameNum();
private:
class impl;