History

Merge changes I32ab6829,If47867d4,I4442de01

* changes: Add coding_data_bit_size to EncodeFrameResult Pass in infile_path to SimpleEncode() Add SimpleEncode::EncodeFrameWithQuantizeIndex()

Angie Chiang committed 6 years ago

Commit 9fbcfd12 parents 831536c 6a950fe5 files · +115 −37Browse files

Unified · Split

5 files
  1. test/simple_encode_test.cc +39 −27
  2. vp9/encoder/vp9_encoder.c +19 −0
  3. vp9/encoder/vp9_encoder.h +28 −0
  4. vp9/simple_encode.cc +21 −8
  5. vp9/simple_encode.h +8 −2
test/simple_encode_test.cc+39−27
@@ -1,21 +1,28 @@
1+#include <math.h>
12 #include <memory>
23 #include <vector>
34 #include "third_party/googletest/src/include/gtest/gtest.h"
45 #include "vp9/simple_encode.h"
56
67 namespace {
8+const int w = 352;
9+const int h = 288;
10+const int frame_rate_num = 30;
11+const int frame_rate_den = 1;
12+const int target_bitrate = 1000;
13+const int num_frames = 17;
14+// TODO(angiebird): Figure out how to upload test video to our codebase
15+const char infile_path[] = "bus_352x288_420_f20_b8.yuv";
16+
17+static double get_bit_rate_in_kpbs(size_t bit_size, int num_frames,
18+ int frame_rate_num, int frame_rate_den) {
19+ return static_cast<double>(bit_size) / num_frames * frame_rate_num /
20+ frame_rate_den / 1000.;
21+}
722
823 TEST(SimpleEncode, ComputeFirstPassStats) {
9− int w = 352;
10− int h = 288;
11− int frame_rate_num = 30;
12− int frame_rate_den = 1;
13− int target_bitrate = 200;
14− int num_frames = 17;
15− // TODO(angiebird): Figure out how to upload test video to our codebase
16− FILE *file = fopen("bus_352x288_420_f20_b8.yuv", "r");
1724 SimpleEncode simple_encode(w, h, frame_rate_num, frame_rate_den,
18− target_bitrate, num_frames, file);
25+ target_bitrate, num_frames, infile_path);
1926 simple_encode.ComputeFirstPassStats();
2027 std::vector<std::vector<double>> frame_stats =
2128 simple_encode.ObserveFirstPassStats();
@@ -34,32 +41,16 @@ TEST(SimpleEncode, ComputeFirstPassStats) { · 12 unmodified lines
3441 }
3542
3643 TEST(SimpleEncode, GetCodingFrameNum) {
37− int w = 352;
38− int h = 288;
39− int frame_rate_num = 30;
40− int frame_rate_den = 1;
41− int target_bitrate = 200;
42− int num_frames = 17;
43− // TODO(angiebird): Figure out how to upload test video to our codebase
44− FILE *file = fopen("bus_352x288_420_f20_b8.yuv", "r");
4544 SimpleEncode simple_encode(w, h, frame_rate_num, frame_rate_den,
46− target_bitrate, num_frames, file);
45+ target_bitrate, num_frames, infile_path);
4746 simple_encode.ComputeFirstPassStats();
4847 int num_coding_frames = simple_encode.GetCodingFrameNum();
4948 EXPECT_EQ(num_coding_frames, 19);
5049 }
5150
5251 TEST(SimpleEncode, EncodeFrame) {
53− int w = 352;
54− int h = 288;
55− int frame_rate_num = 30;
56− int frame_rate_den = 1;
57− int target_bitrate = 1000;
58− int num_frames = 17;
59− // TODO(angiebird): Figure out how to upload test video to our codebase
60− FILE *file = fopen("bus_352x288_420_f20_b8.yuv", "r");
6152 SimpleEncode simple_encode(w, h, frame_rate_num, frame_rate_den,
62− target_bitrate, num_frames, file);
53+ target_bitrate, num_frames, infile_path);
6354 simple_encode.ComputeFirstPassStats();
6455 int num_coding_frames = simple_encode.GetCodingFrameNum();
6556 EXPECT_GE(num_coding_frames, num_frames);
@@ -68,6 +59,7 @@ TEST(SimpleEncode, EncodeFrame) { · 2 unmodified lines
6859 int ref_num_alternate_refereces = num_coding_frames - num_frames;
6960 int num_alternate_refereces = 0;
7061 simple_encode.StartEncode();
62+ size_t total_data_bit_size = 0;
7163 for (int i = 0; i < num_coding_frames; ++i) {
7264 EncodeFrameResult encode_frame_result;
7365 if (i == 0) {
@@ -88,8 +80,28 @@ TEST(SimpleEncode, EncodeFrame) { · 14 unmodified lines
8880 EXPECT_GE(encode_frame_result.psnr, 34)
8981 << "The psnr is supposed to be greater than 34 given the "
9082 "target_bitrate 1000 kbps";
83+ total_data_bit_size += encode_frame_result.coding_data_bit_size;
9184 }
9285 EXPECT_EQ(num_alternate_refereces, ref_num_alternate_refereces);
86+ double bitrate = get_bit_rate_in_kpbs(total_data_bit_size, num_frames,
87+ frame_rate_num, frame_rate_den);
88+ EXPECT_LE(fabs(target_bitrate - bitrate), 150);
89+ simple_encode.EndEncode();
90+}
91+
92+TEST(SimpleEncode, EncodeFrameWithQuantizeIndex) {
93+ SimpleEncode simple_encode(w, h, frame_rate_num, frame_rate_den,
94+ target_bitrate, num_frames, infile_path);
95+ simple_encode.ComputeFirstPassStats();
96+ int num_coding_frames = simple_encode.GetCodingFrameNum();
97+ simple_encode.StartEncode();
98+ for (int i = 0; i < num_coding_frames; ++i) {
99+ int assigned_quantize_index = 100 + i;
100+ EncodeFrameResult encode_frame_result;
101+ simple_encode.EncodeFrameWithQuantizeIndex(&encode_frame_result,
102+ assigned_quantize_index);
103+ EXPECT_EQ(encode_frame_result.quantize_index, assigned_quantize_index);
104+ }
93105 simple_encode.EndEncode();
94106 }
95107
vp9/encoder/vp9_encoder.c+19−0
@@ -2647,6 +2647,10 @@ VP9_COMP *vp9_create_compressor(const VP9EncoderConfig *oxcf, · 2646 unmodified lines
26472647
26482648 cm->error.setjmp = 0;
26492649
2650+#if CONFIG_RATE_CTRL
2651+ encode_command_init(&cpi->encode_command);
2652+#endif
2653+
26502654 return cpi;
26512655 }
26522656
@@ -4282,6 +4286,14 @@ static void encode_with_recode_loop(VP9_COMP *cpi, size_t *size, · 1629 unmodified lines
42824286 vp9_scale_references(cpi);
42834287 }
42844288
4289+#if CONFIG_RATE_CTRL
4290+ // TODO(angiebird): This is a hack for making sure the encoder use the
4291+ // external_quantize_index exactly. Avoid this kind of hack later.
4292+ if (cpi->encode_command.use_external_quantize_index) {
4293+ q = cpi->encode_command.external_quantize_index;
4294+ }
4295+#endif
4296+
42854297 vp9_set_quantizer(cm, q);
42864298
42874299 if (loop_count == 0) setup_frame(cpi);
@@ -4307,6 +4319,13 @@ static void encode_with_recode_loop(VP9_COMP *cpi, size_t *size, · 19 unmodified lines
43074319 // update_base_skip_probs(cpi);
43084320
43094321 vpx_clear_system_state();
4322+#if CONFIG_RATE_CTRL
4323+ // TODO(angiebird): This is a hack for making sure the encoder use the
4324+ // external_quantize_index exactly. Avoid this kind of hack later.
4325+ if (cpi->encode_command.use_external_quantize_index) {
4326+ break;
4327+ }
4328+#endif
43104329
43114330 // Dummy pack of the bitstream using up to date stats to get an
43124331 // accurate estimate of output frame size to determine if we need
vp9/encoder/vp9_encoder.h+28−0
@@ -516,6 +516,31 @@ typedef struct KMEANS_DATA { · 515 unmodified lines
516516 int group_idx;
517517 } KMEANS_DATA;
518518
519+#if CONFIG_RATE_CTRL
520+typedef struct ENCODE_COMMAND {
521+ int use_external_quantize_index;
522+ int external_quantize_index;
523+} ENCODE_COMMAND;
524+
525+static INLINE void encode_command_init(ENCODE_COMMAND *encode_command) {
526+ vp9_zero(*encode_command);
527+ encode_command->use_external_quantize_index = 0;
528+ encode_command->external_quantize_index = -1;
529+}
530+
531+static INLINE void encode_command_set_external_quantize_index(
532+ ENCODE_COMMAND *encode_command, int quantize_index) {
533+ encode_command->use_external_quantize_index = 1;
534+ encode_command->external_quantize_index = quantize_index;
535+}
536+
537+static INLINE void encode_command_reset_external_quantize_index(
538+ ENCODE_COMMAND *encode_command) {
539+ encode_command->use_external_quantize_index = 0;
540+ encode_command->external_quantize_index = -1;
541+}
542+#endif
543+
519544 typedef struct VP9_COMP {
520545 FRAME_INFO frame_info;
521546 QUANTS quants;
@@ -820,6 +845,9 @@ typedef struct VP9_COMP { · 298 unmodified lines
820845
821846 int multi_layer_arf;
822847 vpx_roi_map_t roi;
848+#if CONFIG_RATE_CTRL
849+ ENCODE_COMMAND encode_command;
850+#endif
823851 } VP9_COMP;
824852
825853 typedef struct ENCODE_FRAME_RESULT {
vp9/simple_encode.cc+21−8
@@ -87,6 +87,8 @@ get_frame_type_from_update_type(FRAME_UPDATE_TYPE update_type) { · 86 unmodified lines
8787 static void update_encode_frame_result(
8888 EncodeFrameResult *encode_frame_result,
8989 const ENCODE_FRAME_RESULT *encode_frame_info) {
90+ encode_frame_result->coding_data_bit_size =
91+ encode_frame_result->coding_data_byte_size * 8;
9092 encode_frame_result->show_idx = encode_frame_info->show_idx;
9193 encode_frame_result->frame_type =
9294 get_frame_type_from_update_type(encode_frame_info->update_type);
@@ -97,7 +99,8 @@ static void update_encode_frame_result( · 4 unmodified lines
9799
98100 SimpleEncode::SimpleEncode(int frame_width, int frame_height,
99101 int frame_rate_num, int frame_rate_den,
100− int target_bitrate, int num_frames, FILE *file)
102+ int target_bitrate, int num_frames,
103+ const char *infile_path)
101104 : pimpl{ std::unique_ptr<impl>(new impl()) } {
102105 this->frame_width = frame_width;
103106 this->frame_height = frame_height;
@@ -105,7 +108,8 @@ SimpleEncode::SimpleEncode(int frame_width, int frame_height, · 1 unmodified line
105108 this->frame_rate_den = frame_rate_den;
106109 this->target_bitrate = target_bitrate;
107110 this->num_frames = num_frames;
108− this->file = file;
111+ // TODO(angirbid): Should we keep a file pointer here or keep the file_path?
112+ this->file = fopen(infile_path, "r");
109113 pimpl->cpi = NULL;
110114 pimpl->img_fmt = VPX_IMG_FMT_I420;
111115 }
@@ -234,26 +238,35 @@ void SimpleEncode::EncodeFrame(EncodeFrameResult *encode_frame_result) { · 122 unmodified lines
234238 }
235239 }
236240 assert(encode_frame_result->coding_data.get() == nullptr);
237− const size_t max_coding_data_size = frame_width * frame_height * 3;
238− encode_frame_result->coding_data =
239− std::move(std::unique_ptr<uint8_t[]>(new uint8_t[max_coding_data_size]));
241+ const size_t max_coding_data_byte_size = frame_width * frame_height * 3;
242+ encode_frame_result->coding_data = std::move(
243+ std::unique_ptr<uint8_t[]>(new uint8_t[max_coding_data_byte_size]));
240244 int64_t time_stamp;
241245 int64_t time_end;
242246 int flush = 1; // Make vp9_get_compressed_data encode a frame
243247 unsigned int frame_flags = 0;
244248 ENCODE_FRAME_RESULT encode_frame_info;
245249 vp9_get_compressed_data(cpi, &frame_flags,
246− &encode_frame_result->coding_data_size,
250+ &encode_frame_result->coding_data_byte_size,
247251 encode_frame_result->coding_data.get(), &time_stamp,
248252 &time_end, flush, &encode_frame_info);
249253 // vp9_get_compressed_data is expected to encode a frame every time, so the
250254 // data size should be greater than zero.
251− assert(encode_frame_result->coding_data_size > 0);
252− assert(encode_frame_result->coding_data_size < max_coding_data_size);
255+ assert(encode_frame_result->coding_data_byte_size > 0);
256+ assert(encode_frame_result->coding_data_byte_size <
257+ max_coding_data_byte_size);
253258
254259 update_encode_frame_result(encode_frame_result, &encode_frame_info);
255260 }
256261
262+void SimpleEncode::EncodeFrameWithQuantizeIndex(
263+ EncodeFrameResult *encode_frame_result, int quantize_index) {
264+ encode_command_set_external_quantize_index(&pimpl->cpi->encode_command,
265+ quantize_index);
266+ EncodeFrame(encode_frame_result);
267+ encode_command_reset_external_quantize_index(&pimpl->cpi->encode_command);
268+}
269+
257270 int SimpleEncode::GetCodingFrameNum() {
258271 assert(pimpl->first_pass_stats.size() - 1 > 0);
259272 // These are the default settings for now.
vp9/simple_encode.h+8−2
@@ -10,7 +10,8 @@ enum FrameType { · 9 unmodified lines
1010 struct EncodeFrameResult {
1111 int show_idx;
1212 FrameType frame_type;
13− size_t coding_data_size;
13+ size_t coding_data_bit_size;
14+ size_t coding_data_byte_size;
1415 // The EncodeFrame will allocate a buffer, write the coding data into the
1516 // buffer and give the ownership of the buffer to coding_data
1617 std::unique_ptr<unsigned char[]> coding_data;
@@ -23,7 +24,7 @@ class SimpleEncode { · 6 unmodified lines
2324 public:
2425 SimpleEncode(int frame_width, int frame_height, int frame_rate_num,
2526 int frame_rate_den, int target_bitrate, int num_frames,
26− FILE *file);
27+ const char *infile_path);
2728 ~SimpleEncode();
2829 SimpleEncode(SimpleEncode &&) = delete;
2930 SimpleEncode &operator=(SimpleEncode &&) = delete;
@@ -47,6 +48,11 @@ class SimpleEncode { · 17 unmodified lines
4748 // This funtion should be called after StartEncode() before EndEncode()
4849 void EncodeFrame(EncodeFrameResult *encode_frame_result);
4950
51+ // Encode a frame with a specific quantize index
52+ // This funtion should be called after StartEncode() before EndEncode()
53+ void EncodeFrameWithQuantizeIndex(EncodeFrameResult *encode_frame_result,
54+ int quantize_index);
55+
5056 // Get the number of coding frames for the video. The coding frames include
5157 // show frame and no show frame.
5258 // This funtion should be called after ComputeFirstPassStats()

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t