OLD | NEW |
---|---|
(Empty) | |
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 | |
14 #include "webrtc/common_types.h" | |
15 #include "webrtc/common_video/libyuv/include/webrtc_libyuv.h" | |
16 #include "webrtc/rtc_base/checks.h" | |
17 #include "webrtc/test/testsupport/frame_writer.h" | |
18 | |
19 extern "C" { | |
20 #if defined(USE_SYSTEM_LIBJPEG) | |
21 #include <jpeglib.h> | |
22 #elif defined(USE_LIBJPEG_TURBO) | |
23 #include "third_party/libjpeg_turbo/jpeglib.h" | |
24 #else | |
25 #include "third_party/libjpeg/jpeglib.h" | |
26 #endif | |
27 } | |
28 | |
29 namespace webrtc { | |
30 namespace test { | |
31 | |
32 JpegFrameWriter::JpegFrameWriter(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) { | |
38 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.
| |
39 const int kColorPlanes = 3; // R, G and B. | |
40 size_t rgb_len = input_frame.height() * input_frame.width() * kColorPlanes; | |
41 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.
| |
42 | |
43 // kRGB24 actually corresponds to FourCC 24BG which is 24-bit BGR. | |
44 if (ConvertFromI420(input_frame, VideoType::kRGB24, 0, rgb_buf.get()) < 0) { | |
45 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.
| |
46 return false; | |
47 } | |
48 output_file_ = fopen(output_filename_.c_str(), "wb"); | |
49 if (!output_file_) { | |
50 fprintf(stderr, "Couldn't open file to write jpeg frame to: %s\n", | |
51 output_filename_.c_str()); | |
52 return false; | |
53 } | |
54 | |
55 // Invoking LIBJPEG | |
56 struct jpeg_compress_struct cinfo; | |
57 struct jpeg_error_mgr jerr; | |
58 JSAMPROW row_pointer[1]; | |
59 cinfo.err = jpeg_std_error(&jerr); | |
60 jpeg_create_compress(&cinfo); | |
61 | |
62 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.
| |
63 | |
64 cinfo.image_width = input_frame.width(); | |
65 cinfo.image_height = input_frame.height(); | |
66 cinfo.input_components = kColorPlanes; | |
67 cinfo.in_color_space = JCS_EXT_BGR; | |
68 jpeg_set_defaults(&cinfo); | |
69 jpeg_set_quality(&cinfo, quality, TRUE); | |
70 | |
71 jpeg_start_compress(&cinfo, TRUE); | |
72 int row_stride = input_frame.width() * kColorPlanes; | |
73 while (cinfo.next_scanline < cinfo.image_height) { | |
74 row_pointer[0] = &rgb_buf.get()[cinfo.next_scanline * row_stride]; | |
75 (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
| |
76 } | |
77 | |
78 jpeg_finish_compress(&cinfo); | |
79 jpeg_destroy_compress(&cinfo); | |
80 fclose(output_file_); | |
81 | |
82 frame_written_ = true; | |
83 return true; | |
84 } | |
85 | |
86 } // namespace test | |
87 } // namespace webrtc | |
OLD | NEW |