Index: webrtc/test/testsupport/jpeg_frame_writer.cc |
diff --git a/webrtc/test/testsupport/jpeg_frame_writer.cc b/webrtc/test/testsupport/jpeg_frame_writer.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..106fa2dfb553da3fba96b9a9a4a88cfbdfd7ef20 |
--- /dev/null |
+++ b/webrtc/test/testsupport/jpeg_frame_writer.cc |
@@ -0,0 +1,87 @@ |
+/* |
+ * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved. |
+ * |
+ * Use of this source code is governed by a BSD-style license |
+ * that can be found in the LICENSE file in the root of the source |
+ * tree. An additional intellectual property rights grant can be found |
+ * in the file PATENTS. All contributing project authors may |
+ * be found in the AUTHORS file in the root of the source tree. |
+ */ |
+ |
+#include <stdio.h> |
+ |
+ |
+#include "webrtc/common_types.h" |
+#include "webrtc/common_video/libyuv/include/webrtc_libyuv.h" |
+#include "webrtc/rtc_base/checks.h" |
+#include "webrtc/test/testsupport/frame_writer.h" |
+ |
+extern "C" { |
+#if defined(USE_SYSTEM_LIBJPEG) |
+#include <jpeglib.h> |
+#elif defined(USE_LIBJPEG_TURBO) |
+#include "third_party/libjpeg_turbo/jpeglib.h" |
+#else |
+#include "third_party/libjpeg/jpeglib.h" |
+#endif |
+} |
+ |
+namespace webrtc { |
+namespace test { |
+ |
+JpegFrameWriter::JpegFrameWriter(std::string output_filename) |
+ : frame_written_(false), |
+ output_filename_(output_filename), |
+ output_file_(nullptr) {} |
+ |
+bool JpegFrameWriter::WriteFrame(const VideoFrame& input_frame, int quality) { |
+ RTC_CHECK(!frame_written_) << "Only s single frame can be saved to Jpeg."; |
sprang_webrtc
2017/08/17 15:27:50
s? You mean "a"?
ilnik
2017/08/18 08:42:05
Yep, a typo. Thanks.
|
+ const int kColorPlanes = 3; // R, G and B. |
+ size_t rgb_len = input_frame.height() * input_frame.width() * kColorPlanes; |
+ std::unique_ptr<uint8_t> rgb_buf(new uint8_t[rgb_len]); |
sprang_webrtc
2017/08/17 15:27:50
Hm, shouldn't this be a std::unique_ptr<uint8_t[]>
ilnik
2017/08/18 08:42:05
Done.
|
+ |
+ // kRGB24 actually corresponds to FourCC 24BG which is 24-bit BGR. |
+ if (ConvertFromI420(input_frame, VideoType::kRGB24, 0, rgb_buf.get()) < 0) { |
+ fprintf(stderr, "Could not convert input frame to RGB.\n"); |
sprang_webrtc
2017/08/17 15:27:50
Use LOG macros
ilnik
2017/08/18 08:42:05
Done.
|
+ return false; |
+ } |
+ output_file_ = fopen(output_filename_.c_str(), "wb"); |
+ if (!output_file_) { |
+ fprintf(stderr, "Couldn't open file to write jpeg frame to: %s\n", |
+ output_filename_.c_str()); |
+ return false; |
+ } |
+ |
+ // Invoking LIBJPEG |
+ struct jpeg_compress_struct cinfo; |
+ struct jpeg_error_mgr jerr; |
+ JSAMPROW row_pointer[1]; |
+ cinfo.err = jpeg_std_error(&jerr); |
+ jpeg_create_compress(&cinfo); |
+ |
+ jpeg_stdio_dest(&cinfo, output_file_); |
sprang_webrtc
2017/08/17 15:27:50
Would it be possible to write to a buffer instead,
ilnik
2017/08/18 08:42:05
It's problematic. Outputting to memory is availabl
sprang_webrtc
2017/08/18 08:48:03
Fair enough, let's leave it as is.
|
+ |
+ cinfo.image_width = input_frame.width(); |
+ cinfo.image_height = input_frame.height(); |
+ cinfo.input_components = kColorPlanes; |
+ cinfo.in_color_space = JCS_EXT_BGR; |
+ jpeg_set_defaults(&cinfo); |
+ jpeg_set_quality(&cinfo, quality, TRUE); |
+ |
+ jpeg_start_compress(&cinfo, TRUE); |
+ int row_stride = input_frame.width() * kColorPlanes; |
+ while (cinfo.next_scanline < cinfo.image_height) { |
+ row_pointer[0] = &rgb_buf.get()[cinfo.next_scanline * row_stride]; |
+ (void)jpeg_write_scanlines(&cinfo, row_pointer, 1); |
sprang_webrtc
2017/08/17 15:27:50
why the (void) ?
ilnik
2017/08/18 08:42:04
Came from the example code. Probably has to do som
|
+ } |
+ |
+ jpeg_finish_compress(&cinfo); |
+ jpeg_destroy_compress(&cinfo); |
+ fclose(output_file_); |
+ |
+ frame_written_ = true; |
+ return true; |
+} |
+ |
+} // namespace test |
+} // namespace webrtc |