blob: a225d58a2bc8fe0e4fe79b1ad637e93f36397519 [file] [log] [blame]
andrew@webrtc.orgcb181212011-10-26 00:27:17 +00001/*
2 * Copyright (c) 2011 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// Commandline tool to unpack audioproc debug files.
12//
13// The debug files are dumped as protobuf blobs. For analysis, it's necessary
14// to unpack the file into its component parts: audio and other data.
15
16#include <stdio.h>
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +000017#include <limits>
andrew@webrtc.orgcb181212011-10-26 00:27:17 +000018
pbos@webrtc.org7fad4b82013-05-28 08:11:59 +000019#include "gflags/gflags.h"
andrew@webrtc.orgcb181212011-10-26 00:27:17 +000020#include "webrtc/audio_processing/debug.pb.h"
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +000021#include "webrtc/common_audio/include/audio_util.h"
22#include "webrtc/common_audio/wav_writer.h"
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000023#include "webrtc/modules/audio_processing/test/test_utils.h"
pbos@webrtc.org7fad4b82013-05-28 08:11:59 +000024#include "webrtc/system_wrappers/interface/scoped_ptr.h"
25#include "webrtc/typedefs.h"
andrew@webrtc.orgcb181212011-10-26 00:27:17 +000026
andrew@webrtc.orgcb181212011-10-26 00:27:17 +000027// TODO(andrew): unpack more of the data.
28DEFINE_string(input_file, "input.pcm", "The name of the input stream file.");
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +000029DEFINE_string(input_wav_file, "input.wav",
30 "The name of the WAV input stream file.");
andrew@webrtc.orgcd824382011-11-11 19:13:36 +000031DEFINE_string(output_file, "ref_out.pcm",
32 "The name of the reference output stream file.");
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +000033DEFINE_string(output_wav_file, "ref_out.wav",
34 "The name of the WAV reference output stream file.");
andrew@webrtc.orgcd824382011-11-11 19:13:36 +000035DEFINE_string(reverse_file, "reverse.pcm",
36 "The name of the reverse input stream file.");
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +000037DEFINE_string(reverse_wav_file, "reverse.wav",
38 "The name of the WAV reverse input stream file.");
andrew@webrtc.orgcd824382011-11-11 19:13:36 +000039DEFINE_string(delay_file, "delay.int32", "The name of the delay file.");
40DEFINE_string(drift_file, "drift.int32", "The name of the drift file.");
41DEFINE_string(level_file, "level.int32", "The name of the level file.");
andrew@webrtc.orgce8e0772014-02-12 15:28:30 +000042DEFINE_string(keypress_file, "keypress.bool", "The name of the keypress file.");
andrew@webrtc.orgcd824382011-11-11 19:13:36 +000043DEFINE_string(settings_file, "settings.txt", "The name of the settings file.");
44DEFINE_bool(full, false,
45 "Unpack the full set of files (normally not needed).");
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +000046DEFINE_bool(pcm, false, "Write to PCM instead of WAV file.");
andrew@webrtc.orgcb181212011-10-26 00:27:17 +000047
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000048namespace webrtc {
andrew@webrtc.orgcb181212011-10-26 00:27:17 +000049
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000050using audioproc::Event;
51using audioproc::ReverseStream;
52using audioproc::Stream;
53using audioproc::Init;
andrew@webrtc.orgcb181212011-10-26 00:27:17 +000054
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +000055class PcmFile {
56 public:
57 PcmFile(const std::string& filename)
58 : file_handle_(fopen(filename.c_str(), "wb")) {}
59
60 ~PcmFile() {
61 fclose(file_handle_);
62 }
63
64 void WriteSamples(const int16_t* samples, size_t num_samples) {
65#ifndef WEBRTC_ARCH_LITTLE_ENDIAN
66#error "Need to convert samples to little-endian when writing to PCM file"
67#endif
68 fwrite(samples, sizeof(*samples), num_samples, file_handle_);
69 }
70
71 void WriteSamples(const float* samples, size_t num_samples) {
72 static const size_t kChunksize = 4096 / sizeof(uint16_t);
73 for (size_t i = 0; i < num_samples; i += kChunksize) {
74 int16_t isamples[kChunksize];
75 const size_t chunk = std::min(kChunksize, num_samples - i);
76 RoundToInt16(samples + i, chunk, isamples);
77 WriteSamples(isamples, chunk);
78 }
79 }
80
81 private:
82 FILE* file_handle_;
83};
84
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000085void WriteData(const void* data, size_t size, FILE* file,
86 const std::string& filename) {
87 if (fwrite(data, size, 1, file) != 1) {
88 printf("Error when writing to %s\n", filename.c_str());
89 exit(1);
90 }
andrew@webrtc.orgcb181212011-10-26 00:27:17 +000091}
92
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +000093void WriteIntData(const int16_t* data,
94 size_t length,
95 WavFile* wav_file,
96 PcmFile* pcm_file) {
97 if (wav_file) {
98 wav_file->WriteSamples(data, length);
99 }
100 if (pcm_file) {
101 pcm_file->WriteSamples(data, length);
102 }
103}
104
105void WriteFloatData(const float* const* data,
106 size_t samples_per_channel,
107 int num_channels,
108 WavFile* wav_file,
109 PcmFile* pcm_file) {
110 size_t length = num_channels * samples_per_channel;
111 scoped_ptr<float[]> buffer(new float[length]);
112 Interleave(data, samples_per_channel, num_channels, buffer.get());
113 // TODO(aluebs): Use ScaleToInt16Range() from audio_util
114 for (size_t i = 0; i < length; ++i) {
115 buffer[i] = buffer[i] > 0 ?
116 buffer[i] * std::numeric_limits<int16_t>::max() :
117 -buffer[i] * std::numeric_limits<int16_t>::min();
118 }
119 if (wav_file) {
120 wav_file->WriteSamples(buffer.get(), length);
121 }
122 if (pcm_file) {
123 pcm_file->WriteSamples(buffer.get(), length);
124 }
125}
126
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000127int do_main(int argc, char* argv[]) {
andrew@webrtc.orgcb181212011-10-26 00:27:17 +0000128 std::string program_name = argv[0];
129 std::string usage = "Commandline tool to unpack audioproc debug files.\n"
130 "Example usage:\n" + program_name + " debug_dump.pb\n";
131 google::SetUsageMessage(usage);
132 google::ParseCommandLineFlags(&argc, &argv, true);
133
134 if (argc < 2) {
135 printf("%s", google::ProgramUsage());
136 return 1;
137 }
138
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000139 FILE* debug_file = OpenFile(argv[1], "rb");
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000140
andrew@webrtc.orgcb181212011-10-26 00:27:17 +0000141 Event event_msg;
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000142 int frame_count = 0;
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000143 int num_input_channels = 0;
144 int num_output_channels = 0;
145 int num_reverse_channels = 0;
146 scoped_ptr<WavFile> reverse_wav_file;
147 scoped_ptr<WavFile> input_wav_file;
148 scoped_ptr<WavFile> output_wav_file;
149 scoped_ptr<PcmFile> reverse_pcm_file;
150 scoped_ptr<PcmFile> input_pcm_file;
151 scoped_ptr<PcmFile> output_pcm_file;
152 while (ReadMessageFromFile(debug_file, &event_msg)) {
andrew@webrtc.orgcb181212011-10-26 00:27:17 +0000153 if (event_msg.type() == Event::REVERSE_STREAM) {
154 if (!event_msg.has_reverse_stream()) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000155 printf("Corrupt input file: ReverseStream missing.\n");
andrew@webrtc.orgcb181212011-10-26 00:27:17 +0000156 return 1;
157 }
158
159 const ReverseStream msg = event_msg.reverse_stream();
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000160 if (msg.has_data()) {
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000161 WriteIntData(reinterpret_cast<const int16_t*>(msg.data().data()),
162 msg.data().size() / sizeof(int16_t),
163 reverse_wav_file.get(),
164 reverse_pcm_file.get());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000165 } else if (msg.channel_size() > 0) {
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000166 scoped_ptr<const float*[]> data(new const float*[num_reverse_channels]);
167 for (int i = 0; i < num_reverse_channels; ++i) {
168 data[i] = reinterpret_cast<const float*>(msg.channel(i).data());
169 }
170 WriteFloatData(data.get(),
171 msg.channel(0).size() / sizeof(float),
172 num_reverse_channels,
173 reverse_wav_file.get(),
174 reverse_pcm_file.get());
andrew@webrtc.orgcb181212011-10-26 00:27:17 +0000175 }
176 } else if (event_msg.type() == Event::STREAM) {
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000177 frame_count++;
andrew@webrtc.orgcb181212011-10-26 00:27:17 +0000178 if (!event_msg.has_stream()) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000179 printf("Corrupt input file: Stream missing.\n");
andrew@webrtc.orgcb181212011-10-26 00:27:17 +0000180 return 1;
181 }
182
183 const Stream msg = event_msg.stream();
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000184 if (msg.has_input_data()) {
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000185 WriteIntData(reinterpret_cast<const int16_t*>(msg.input_data().data()),
186 msg.input_data().size() / sizeof(int16_t),
187 input_wav_file.get(),
188 input_pcm_file.get());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000189 } else if (msg.input_channel_size() > 0) {
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000190 scoped_ptr<const float*[]> data(new const float*[num_input_channels]);
191 for (int i = 0; i < num_input_channels; ++i) {
192 data[i] = reinterpret_cast<const float*>(msg.input_channel(i).data());
193 }
194 WriteFloatData(data.get(),
195 msg.input_channel(0).size() / sizeof(float),
196 num_input_channels,
197 input_wav_file.get(),
198 input_pcm_file.get());
andrew@webrtc.orgcb181212011-10-26 00:27:17 +0000199 }
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000200
201 if (msg.has_output_data()) {
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000202 WriteIntData(reinterpret_cast<const int16_t*>(msg.output_data().data()),
203 msg.output_data().size() / sizeof(int16_t),
204 output_wav_file.get(),
205 output_pcm_file.get());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000206 } else if (msg.output_channel_size() > 0) {
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000207 scoped_ptr<const float*[]> data(new const float*[num_output_channels]);
208 for (int i = 0; i < num_output_channels; ++i) {
209 data[i] =
210 reinterpret_cast<const float*>(msg.output_channel(i).data());
211 }
212 WriteFloatData(data.get(),
213 msg.output_channel(0).size() / sizeof(float),
214 num_output_channels,
215 output_wav_file.get(),
216 output_pcm_file.get());
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000217 }
218
219 if (FLAGS_full) {
220 if (msg.has_delay()) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000221 static FILE* delay_file = OpenFile(FLAGS_delay_file, "wb");
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000222 int32_t delay = msg.delay();
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000223 WriteData(&delay, sizeof(delay), delay_file, FLAGS_delay_file);
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000224 }
225
226 if (msg.has_drift()) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000227 static FILE* drift_file = OpenFile(FLAGS_drift_file, "wb");
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000228 int32_t drift = msg.drift();
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000229 WriteData(&drift, sizeof(drift), drift_file, FLAGS_drift_file);
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000230 }
231
232 if (msg.has_level()) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000233 static FILE* level_file = OpenFile(FLAGS_level_file, "wb");
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000234 int32_t level = msg.level();
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000235 WriteData(&level, sizeof(level), level_file, FLAGS_level_file);
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000236 }
andrew@webrtc.orgce8e0772014-02-12 15:28:30 +0000237
238 if (msg.has_keypress()) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000239 static FILE* keypress_file = OpenFile(FLAGS_keypress_file, "wb");
andrew@webrtc.orgce8e0772014-02-12 15:28:30 +0000240 bool keypress = msg.keypress();
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000241 WriteData(&keypress, sizeof(keypress), keypress_file,
242 FLAGS_keypress_file);
andrew@webrtc.orgce8e0772014-02-12 15:28:30 +0000243 }
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000244 }
245 } else if (event_msg.type() == Event::INIT) {
246 if (!event_msg.has_init()) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000247 printf("Corrupt input file: Init missing.\n");
andrew@webrtc.orgcb181212011-10-26 00:27:17 +0000248 return 1;
249 }
250
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000251 static FILE* settings_file = OpenFile(FLAGS_settings_file, "wb");
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000252 const Init msg = event_msg.init();
253 // These should print out zeros if they're missing.
254 fprintf(settings_file, "Init at frame: %d\n", frame_count);
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000255 int input_sample_rate = msg.sample_rate();
256 fprintf(settings_file, " Input sample rate: %d\n", input_sample_rate);
257 int output_sample_rate = msg.output_sample_rate();
258 fprintf(settings_file, " Output sample rate: %d\n", output_sample_rate);
259 int reverse_sample_rate = msg.reverse_sample_rate();
260 fprintf(settings_file,
261 " Reverse sample rate: %d\n",
262 reverse_sample_rate);
263 num_input_channels = msg.num_input_channels();
264 fprintf(settings_file, " Input channels: %d\n", num_input_channels);
265 num_output_channels = msg.num_output_channels();
266 fprintf(settings_file, " Output channels: %d\n", num_output_channels);
267 num_reverse_channels = msg.num_reverse_channels();
268 fprintf(settings_file, " Reverse channels: %d\n", num_reverse_channels);
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000269
270 fprintf(settings_file, "\n");
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000271
272 if (reverse_sample_rate == 0) {
273 reverse_sample_rate = input_sample_rate;
274 }
275 if (output_sample_rate == 0) {
276 output_sample_rate = input_sample_rate;
277 }
278
279 if (FLAGS_pcm) {
280 if (!reverse_pcm_file.get()) {
281 reverse_pcm_file.reset(new PcmFile(FLAGS_reverse_file));
282 }
283 if (!input_pcm_file.get()) {
284 input_pcm_file.reset(new PcmFile(FLAGS_input_file));
285 }
286 if (!output_pcm_file.get()) {
287 output_pcm_file.reset(new PcmFile(FLAGS_output_file));
288 }
289 } else {
290 reverse_wav_file.reset(new WavFile(FLAGS_reverse_wav_file,
291 reverse_sample_rate,
292 num_reverse_channels));
293 input_wav_file.reset(new WavFile(FLAGS_input_wav_file,
294 input_sample_rate,
295 num_input_channels));
296 output_wav_file.reset(new WavFile(FLAGS_output_wav_file,
297 output_sample_rate,
298 num_output_channels));
299 }
andrew@webrtc.orgcb181212011-10-26 00:27:17 +0000300 }
301 }
302
303 return 0;
304}
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000305
306} // namespace webrtc
307
308int main(int argc, char* argv[]) {
309 return webrtc::do_main(argc, argv);
310}