1/*
2 *  Copyright (c) 2012 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#include <stdlib.h>
13
14#include <map>
15#include <string>
16#include <vector>
17
18#include "webrtc/tools/converter/converter.h"
19#include "webrtc/tools/simple_command_line_parser.h"
20
21/*
22 * A command-line tool based on libyuv to convert a set of RGBA files to a YUV
23 * video.
24 * Usage:
25 * rgba_to_i420_converter --frames_dir=<directory_to_rgba_frames>
26 * --output_file=<output_yuv_file> --width=<width_of_input_frames>
27 * --height=<height_of_input_frames>
28 */
29int main(int argc, char** argv) {
30  std::string program_name = argv[0];
31  std::string usage = "Converts RGBA raw image files to I420 frames for YUV.\n"
32    "Example usage:\n" + program_name +
33    " --frames_dir=. --output_file=output.yuv --width=320 --height=240\n"
34    "IMPORTANT: If you pass the --delete_frames command line parameter, the "
35    "tool will delete the input frames after conversion.\n"
36    "Command line flags:\n"
37    "  - width(int): Width in pixels of the frames in the input file."
38    " Default: -1\n"
39    "  - height(int): Height in pixels of the frames in the input file."
40    " Default: -1\n"
41    "  - frames_dir(string): The path to the directory where the frames reside."
42    " Default: .\n"
43    "  - output_file(string): The output file to which frames are written."
44    " Default: output.yuv\n"
45    "  - delete_frames(bool): Whether or not to delete the input frames after"
46    " the conversion. Default: false.\n";
47
48  webrtc::test::CommandLineParser parser;
49
50  // Init the parser and set the usage message
51  parser.Init(argc, argv);
52  parser.SetUsageMessage(usage);
53
54  parser.SetFlag("width", "-1");
55  parser.SetFlag("height", "-1");
56  parser.SetFlag("frames_dir", ".");
57  parser.SetFlag("output_file", "output.yuv");
58  parser.SetFlag("delete_frames", "false");
59  parser.SetFlag("help", "false");
60
61  parser.ProcessFlags();
62  if (parser.GetFlag("help") == "true") {
63    parser.PrintUsageMessage();
64  }
65  parser.PrintEnteredFlags();
66
67  int width = strtol((parser.GetFlag("width")).c_str(), NULL, 10);
68  int height = strtol((parser.GetFlag("height")).c_str(), NULL, 10);
69
70  if (width <= 0 || height <= 0) {
71    fprintf(stderr, "Error: width or height cannot be <= 0!\n");
72    return -1;
73  }
74
75  bool del_frames = (parser.GetFlag("delete_frames") == "true") ? true : false;
76
77  webrtc::test::Converter converter(width, height);
78  bool success = converter.ConvertRGBAToI420Video(parser.GetFlag("frames_dir"),
79                                                  parser.GetFlag("output_file"),
80                                                  del_frames);
81
82  if (success) {
83    fprintf(stdout, "Successful conversion of RGBA frames to YUV video!\n");
84    return 0;
85  } else {
86    fprintf(stdout, "Unsuccessful conversion of RGBA frames to YUV video!\n");
87    return -1;
88  }
89}
90