Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / webrtc / modules / audio_processing / test / unpack.cc
1 /*
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>
17
18 #include "gflags/gflags.h"
19 #include "webrtc/audio_processing/debug.pb.h"
20 #include "webrtc/modules/audio_processing/test/test_utils.h"
21 #include "webrtc/system_wrappers/interface/scoped_ptr.h"
22 #include "webrtc/typedefs.h"
23
24 // TODO(andrew): unpack more of the data.
25 DEFINE_string(input_file, "input.pcm", "The name of the input stream file.");
26 DEFINE_string(float_input_file, "input.float",
27               "The name of the float input stream file.");
28 DEFINE_string(output_file, "ref_out.pcm",
29               "The name of the reference output stream file.");
30 DEFINE_string(float_output_file, "ref_out.float",
31               "The name of the float reference output stream file.");
32 DEFINE_string(reverse_file, "reverse.pcm",
33               "The name of the reverse input stream file.");
34 DEFINE_string(float_reverse_file, "reverse.float",
35               "The name of the float reverse input stream file.");
36 DEFINE_string(delay_file, "delay.int32", "The name of the delay file.");
37 DEFINE_string(drift_file, "drift.int32", "The name of the drift file.");
38 DEFINE_string(level_file, "level.int32", "The name of the level file.");
39 DEFINE_string(keypress_file, "keypress.bool", "The name of the keypress file.");
40 DEFINE_string(settings_file, "settings.txt", "The name of the settings file.");
41 DEFINE_bool(full, false,
42             "Unpack the full set of files (normally not needed).");
43
44 namespace webrtc {
45
46 using audioproc::Event;
47 using audioproc::ReverseStream;
48 using audioproc::Stream;
49 using audioproc::Init;
50
51 void WriteData(const void* data, size_t size, FILE* file,
52                const std::string& filename) {
53   if (fwrite(data, size, 1, file) != 1) {
54     printf("Error when writing to %s\n", filename.c_str());
55     exit(1);
56   }
57 }
58
59 int do_main(int argc, char* argv[]) {
60   std::string program_name = argv[0];
61   std::string usage = "Commandline tool to unpack audioproc debug files.\n"
62     "Example usage:\n" + program_name + " debug_dump.pb\n";
63   google::SetUsageMessage(usage);
64   google::ParseCommandLineFlags(&argc, &argv, true);
65
66   if (argc < 2) {
67     printf("%s", google::ProgramUsage());
68     return 1;
69   }
70
71   FILE* debug_file = OpenFile(argv[1], "rb");
72
73   Event event_msg;
74   int frame_count = 0;
75 while (ReadMessageFromFile(debug_file, &event_msg)) {
76     if (event_msg.type() == Event::REVERSE_STREAM) {
77       if (!event_msg.has_reverse_stream()) {
78         printf("Corrupt input file: ReverseStream missing.\n");
79         return 1;
80       }
81
82       const ReverseStream msg = event_msg.reverse_stream();
83       if (msg.has_data()) {
84         static FILE* reverse_file = OpenFile(FLAGS_reverse_file, "wb");
85         WriteData(msg.data().data(), msg.data().size(), reverse_file,
86                   FLAGS_reverse_file);
87
88       } else if (msg.channel_size() > 0) {
89         static FILE* float_reverse_file = OpenFile(FLAGS_float_reverse_file,
90                                                    "wb");
91         // TODO(ajm): Interleave multiple channels.
92         assert(msg.channel_size() == 1);
93         WriteData(msg.channel(0).data(), msg.channel(0).size(),
94                   float_reverse_file, FLAGS_reverse_file);
95       }
96     } else if (event_msg.type() == Event::STREAM) {
97       frame_count++;
98       if (!event_msg.has_stream()) {
99         printf("Corrupt input file: Stream missing.\n");
100         return 1;
101       }
102
103       const Stream msg = event_msg.stream();
104       if (msg.has_input_data()) {
105         static FILE* input_file = OpenFile(FLAGS_input_file, "wb");
106         WriteData(msg.input_data().data(), msg.input_data().size(),
107                   input_file, FLAGS_input_file);
108
109       } else if (msg.input_channel_size() > 0) {
110         static FILE* float_input_file = OpenFile(FLAGS_float_input_file, "wb");
111         // TODO(ajm): Interleave multiple channels.
112         assert(msg.input_channel_size() == 1);
113         WriteData(msg.input_channel(0).data(), msg.input_channel(0).size(),
114                   float_input_file, FLAGS_float_input_file);
115       }
116
117       if (msg.has_output_data()) {
118         static FILE* output_file = OpenFile(FLAGS_output_file, "wb");
119         WriteData(msg.output_data().data(), msg.output_data().size(),
120                   output_file, FLAGS_output_file);
121
122       } else if (msg.output_channel_size() > 0) {
123         static FILE* float_output_file = OpenFile(FLAGS_float_output_file,
124                                                   "wb");
125         // TODO(ajm): Interleave multiple channels.
126         assert(msg.output_channel_size() == 1);
127         WriteData(msg.output_channel(0).data(), msg.output_channel(0).size(),
128                   float_output_file, FLAGS_float_output_file);
129       }
130
131       if (FLAGS_full) {
132         if (msg.has_delay()) {
133           static FILE* delay_file = OpenFile(FLAGS_delay_file, "wb");
134           int32_t delay = msg.delay();
135           WriteData(&delay, sizeof(delay), delay_file, FLAGS_delay_file);
136         }
137
138         if (msg.has_drift()) {
139           static FILE* drift_file = OpenFile(FLAGS_drift_file, "wb");
140           int32_t drift = msg.drift();
141           WriteData(&drift, sizeof(drift), drift_file, FLAGS_drift_file);
142         }
143
144         if (msg.has_level()) {
145           static FILE* level_file = OpenFile(FLAGS_level_file, "wb");
146           int32_t level = msg.level();
147           WriteData(&level, sizeof(level), level_file, FLAGS_level_file);
148         }
149
150         if (msg.has_keypress()) {
151           static FILE* keypress_file = OpenFile(FLAGS_keypress_file, "wb");
152           bool keypress = msg.keypress();
153           WriteData(&keypress, sizeof(keypress), keypress_file,
154                     FLAGS_keypress_file);
155         }
156       }
157     } else if (event_msg.type() == Event::INIT) {
158       if (!event_msg.has_init()) {
159         printf("Corrupt input file: Init missing.\n");
160         return 1;
161       }
162
163       static FILE* settings_file = OpenFile(FLAGS_settings_file, "wb");
164       const Init msg = event_msg.init();
165       // These should print out zeros if they're missing.
166       fprintf(settings_file, "Init at frame: %d\n", frame_count);
167       fprintf(settings_file, "  Sample rate: %d\n", msg.sample_rate());
168       fprintf(settings_file, "  Input channels: %d\n",
169               msg.num_input_channels());
170       fprintf(settings_file, "  Output channels: %d\n",
171               msg.num_output_channels());
172       fprintf(settings_file, "  Reverse channels: %d\n",
173               msg.num_reverse_channels());
174
175       fprintf(settings_file, "\n");
176     }
177   }
178
179   return 0;
180 }
181
182 }  // namespace webrtc
183
184 int main(int argc, char* argv[]) {
185   return webrtc::do_main(argc, argv);
186 }