ref: f975d02d6dc095c97c6950a2b8c3768f29644fe5
parent: 7dd26ae967734f9b27b0ecbdbf2be89ecc8b6331
author: angiebird <angiebird@google.com>
date: Mon Nov 11 05:20:15 EST 2019
Add EncodeFrameResults It contains coding_data_size and coding_data. The EncodeFrame will allocate a buffer, write the coding data into the buffer and give the ownership of the buffer to encode_frame_result->coding_data Change-Id: I6bd86aede191ade1db4a1f1bba5be601eef97d60
--- a/test/simple_encode_test.cc
+++ b/test/simple_encode_test.cc
@@ -60,14 +60,12 @@
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);
+ EncodeFrameResult encode_frame_result;
+ simple_encode.EncodeFrame(&encode_frame_result);
// TODO(angiebird): For now, this test just check whether EncodeFrame can be
// run proprly. Add extra check later.
}
--- a/vp9/simple_encode.cc
+++ b/vp9/simple_encode.cc
@@ -182,7 +182,7 @@
rewind(file);
}
-void SimpleEncode::EncodeFrame(char *cx_data, size_t *size, size_t max_size) {
+void SimpleEncode::EncodeFrame(EncodeFrameResult *encode_frame_result) {
VP9_COMP *cpi = pimpl->cpi;
struct lookahead_ctx *lookahead = cpi->lookahead;
int use_highbitdepth = 0;
@@ -210,19 +210,21 @@
break;
}
}
+ assert(encode_frame_result->coding_data.get() == nullptr);
+ const size_t max_coding_data_size = frame_width * frame_height * 3;
+ encode_frame_result->coding_data =
+ std::move(std::unique_ptr<uint8_t[]>(new uint8_t[max_coding_data_size]));
int64_t time_stamp;
int64_t time_end;
int flush = 1; // Make vp9_get_compressed_data encode a frame
unsigned int frame_flags = 0;
- vp9_get_compressed_data(cpi, &frame_flags, size,
- reinterpret_cast<uint8_t *>(cx_data), &time_stamp,
- &time_end, flush);
+ vp9_get_compressed_data(
+ cpi, &frame_flags, &encode_frame_result->coding_data_size,
+ encode_frame_result->coding_data.get(), &time_stamp, &time_end, flush);
// vp9_get_compressed_data is expected to encode a frame every time, so the
// data size should be greater than zero.
- assert(*size > 0);
- if (*size >= max_size) {
- assert(0);
- }
+ assert(encode_frame_result->coding_data_size > 0);
+ assert(encode_frame_result->coding_data_size < max_coding_data_size);
}
int SimpleEncode::GetCodingFrameNum() {
--- a/vp9/simple_encode.h
+++ b/vp9/simple_encode.h
@@ -1,5 +1,23 @@
#include <memory>
#include <vector>
+
+enum FrameType {
+ kKeyFrame = 0,
+ kInterFrame,
+ kAlternateReference,
+};
+
+struct EncodeFrameResult {
+ // TODO(angiebird): int show_index;
+ // TODO(angiebird): FrameType frame_type;
+ size_t coding_data_size;
+ // The EncodeFrame will allocate a buffer, write the coding data into the
+ // buffer and give the ownership of the buffer to coding_data
+ std::unique_ptr<unsigned char[]> coding_data;
+ // TODO(angiebird): double psnr;
+ // TODO(angiebird): int quantize_index ;
+};
+
class SimpleEncode {
public:
SimpleEncode(int frame_width, int frame_height, int frame_rate_num,
@@ -26,7 +44,7 @@
// Encode a frame
// This funtion should be called after StartEncode() before EndEncode()
- void EncodeFrame(char *cx_data, size_t *size, size_t max_size);
+ void EncodeFrame(EncodeFrameResult *encode_frame_result);
// Get the number of coding frames for the video. The coding frames include
// show frame and no show frame.