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