xref: /aosp_15_r20/external/webrtc/test/testsupport/jpeg_frame_writer.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2017 The WebRTC 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 
11 #include <stdio.h>
12 
13 #include "common_video/libyuv/include/webrtc_libyuv.h"
14 #include "rtc_base/checks.h"
15 #include "rtc_base/logging.h"
16 #include "test/testsupport/frame_writer.h"
17 
18 extern "C" {
19 #if defined(USE_SYSTEM_LIBJPEG)
20 #include <jpeglib.h>
21 #else
22 // Include directory supplied by gn
23 #include "jpeglib.h"  // NOLINT
24 #endif
25 }
26 
27 namespace webrtc {
28 namespace test {
29 
JpegFrameWriter(const std::string & output_filename)30 JpegFrameWriter::JpegFrameWriter(const std::string& output_filename)
31     : frame_written_(false),
32       output_filename_(output_filename),
33       output_file_(nullptr) {}
34 
WriteFrame(const VideoFrame & input_frame,int quality)35 bool JpegFrameWriter::WriteFrame(const VideoFrame& input_frame, int quality) {
36   if (frame_written_) {
37     RTC_LOG(LS_ERROR) << "Only a single frame can be saved to Jpeg.";
38     return false;
39   }
40   const int kColorPlanes = 3;  // R, G and B.
41   size_t rgb_len = input_frame.height() * input_frame.width() * kColorPlanes;
42   std::unique_ptr<uint8_t[]> rgb_buf(new uint8_t[rgb_len]);
43 
44   // kRGB24 actually corresponds to FourCC 24BG which is 24-bit BGR.
45   if (ConvertFromI420(input_frame, VideoType::kRGB24, 0, rgb_buf.get()) < 0) {
46     RTC_LOG(LS_ERROR) << "Could not convert input frame to RGB.";
47     return false;
48   }
49   output_file_ = fopen(output_filename_.c_str(), "wb");
50   if (!output_file_) {
51     RTC_LOG(LS_ERROR) << "Couldn't open file to write jpeg frame to:"
52                       << output_filename_;
53     return false;
54   }
55 
56   // Invoking LIBJPEG
57   struct jpeg_compress_struct cinfo;
58   struct jpeg_error_mgr jerr;
59   JSAMPROW row_pointer[1];
60   cinfo.err = jpeg_std_error(&jerr);
61   jpeg_create_compress(&cinfo);
62 
63   jpeg_stdio_dest(&cinfo, output_file_);
64 
65   cinfo.image_width = input_frame.width();
66   cinfo.image_height = input_frame.height();
67   cinfo.input_components = kColorPlanes;
68   cinfo.in_color_space = JCS_EXT_BGR;
69   jpeg_set_defaults(&cinfo);
70   jpeg_set_quality(&cinfo, quality, TRUE);
71 
72   jpeg_start_compress(&cinfo, TRUE);
73   int row_stride = input_frame.width() * kColorPlanes;
74   while (cinfo.next_scanline < cinfo.image_height) {
75     row_pointer[0] = &rgb_buf.get()[cinfo.next_scanline * row_stride];
76     jpeg_write_scanlines(&cinfo, row_pointer, 1);
77   }
78 
79   jpeg_finish_compress(&cinfo);
80   jpeg_destroy_compress(&cinfo);
81   fclose(output_file_);
82 
83   frame_written_ = true;
84   return true;
85 }
86 
87 }  // namespace test
88 }  // namespace webrtc
89