this repo has no description
1/* Copyright: � Copyright 2005 Apple Computer, Inc. All rights reserved.
2
3 Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Computer, Inc.
4 ("Apple") in consideration of your agreement to the following terms, and your
5 use, installation, modification or redistribution of this Apple software
6 constitutes acceptance of these terms. If you do not agree with these terms,
7 please do not use, install, modify or redistribute this Apple software.
8
9 In consideration of your agreement to abide by the following terms, and subject
10 to these terms, Apple grants you a personal, non-exclusive license, under Apple�s
11 copyrights in this original Apple software (the "Apple Software"), to use,
12 reproduce, modify and redistribute the Apple Software, with or without
13 modifications, in source and/or binary forms; provided that if you redistribute
14 the Apple Software in its entirety and without modifications, you must retain
15 this notice and the following text and disclaimers in all such redistributions of
16 the Apple Software. Neither the name, trademarks, service marks or logos of
17 Apple Computer, Inc. may be used to endorse or promote products derived from the
18 Apple Software without specific prior written permission from Apple. Except as
19 expressly stated in this notice, no other rights or licenses, express or implied,
20 are granted by Apple herein, including but not limited to any patent rights that
21 may be infringed by your derivative works or by other works in which the Apple
22 Software may be incorporated.
23
24 The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO
25 WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
26 WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27 PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
28 COMBINATION WITH YOUR PRODUCTS.
29
30 IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
31 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
32 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
34 OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
35 (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
36 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37*/
38/*=============================================================================
39 afconvert.cpp
40
41=============================================================================*/
42
43/*
44 afconvert
45
46 command-line tool to convert an audio file to another format
47*/
48
49#include "afconvert.h"
50#include <stdio.h>
51#include <vector>
52#if !defined(__COREAUDIO_USE_FLAT_INCLUDES__)
53 #include <CoreServices/CoreServices.h>
54 #include <AudioToolbox/AudioToolbox.h>
55#else
56 #include <CoreServices.h>
57 #include <AudioToolbox.h>
58#endif
59#include "CAChannelLayouts.h"
60
61#include "CAAudioFileConverter.h"
62#include "CAXException.h"
63#include "CAAudioFileFormats.h"
64#include "AFToolsCommon.h"
65#if !TARGET_OS_MAC
66 #include <QTML.h>
67 #include "CAWindows.h"
68#endif
69
70
71void AFConvertTool::usage()
72{
73 fprintf(stderr,
74 "Usage:\n"
75 "%s [option...] input_file [output_file]\n\n"
76 "Options: (may appear before or after arguments)\n"
77 " { -f | --file } file_format:\n",
78#if !TARGET_OS_WIN32
79 getprogname());
80#else
81 "afconvert");
82#endif
83 PrintAudioFileTypesAndFormats(stderr);
84 fprintf(stderr,
85 " { -d | --data } data_format[@sample_rate_hz][/format_flags][#frames_per_packet] :\n"
86 " [-][BE|LE]{F|[U]I}{8|16|24|32|64} (PCM)\n"
87 " e.g. BEI16 F32@44100\n"
88 " or a data format appropriate to file format, as above\n"
89 " format_flags: hex digits, e.g. '80'\n"
90 " bitdepth on non-PCM formats can be specified, e.g.: alac-24\n"
91 " Frames per packet can be specified for some encoders, e.g.: samr#12\n"
92 " { -c | --channels } number_of_channels\n"
93 " add/remove channels without regard to order\n"
94 " { -l | --channellayout } layout_tag\n"
95 " layout_tag: name of a constant from CoreAudioTypes.h\n"
96 " (prefix \"kAudioChannelLayoutTag_\" may be omitted)\n"
97 " if specified once, applies to output file; if twice, the first\n"
98 " applies to the input file, the second to the output file\n"
99 " { -b | --bitrate } bit_rate_bps\n"
100 " e.g. 128000\n"
101 " { -q | --quality } codec_quality\n"
102 " codec_quality: 0-127\n"
103 " { -r | --src-quality } src_quality\n"
104 " src_quality (sample rate converter quality): 0-127\n"
105 " { -v | --verbose }\n"
106 " print progress verbosely\n"
107 " { -s | --strategy } strategy\n"
108 " bitrate strategy for encoded file\n"
109 " 0 for CBR, 1 for ABR, 2 for VBR\n"
110 " { -t | --tag }\n"
111 " If encoding to CAF, store the source file's format and name in a user chunk.\n"
112 " If decoding from CAF, use the destination format and filename found in a user chunk.\n"
113 " --prime-method method\n"
114 " decode priming method (see AudioConverter.h)\n"
115 );
116 ExtraUsageOptions();
117 exit(1);
118}
119
120void Warning(const char *s, OSStatus error)
121{
122 char buf[256];
123 fprintf(stderr, "*** warning: %s (%s)\n", s, CAXException::FormatError(buf, sizeof(buf), error));
124}
125
126OSType AFConvertTool::Parse4CharCode(const char *arg, const char *name)
127{
128 OSType t;
129 StrToOSType(arg, t);
130 if (t == 0) {
131 fprintf(stderr, "invalid 4-char-code argument for %s: '%s'\n\n", name, arg);
132 usage();
133 }
134 return t;
135}
136
137
138int AFConvertTool::main(int argc, const char * argv[])
139{
140 Init();
141
142 CAAudioFileConverter::ConversionParameters ¶ms = *mParams;
143 bool gotOutDataFormat = false;
144
145 CAXException::SetWarningHandler(Warning);
146
147 if (argc < 2)
148 usage();
149
150 params.flags = CAAudioFileConverter::kOpt_OverwriteOutputFile;
151
152 for (int i = 1; i < argc; ++i) {
153 const char *arg = argv[i];
154 if (arg[0] != '-') {
155 if (params.input.filePath == NULL) params.input.filePath = arg;
156 else if (params.output.filePath == NULL) params.output.filePath = arg;
157 else usage();
158 } else {
159 arg += 1;
160 if (arg[0] == 'f' || !strcmp(arg, "-file")) {
161 if (++i == argc) MissingArgument();
162 params.output.fileType = Parse4CharCode(argv[i], "-f | --file");
163 } else if (arg[0] == 'd' || !strcmp(arg, "-data")) {
164 if (++i == argc) MissingArgument();
165 if (!ParseStreamDescription(argv[i], params.output.dataFormat))
166 usage();
167 gotOutDataFormat = true;
168 } else if (arg[0] == 'b' || !strcmp(arg, "-bitrate")) {
169 if (++i == argc) MissingArgument();
170 params.output.bitRate = ParseInt(argv[i], "-b | --bitrate");
171 } else if (arg[0] == 'q' || !strcmp(arg, "-quality")) {
172 if (++i == argc) MissingArgument();
173 params.output.codecQuality = ParseInt(argv[i], "-q | --quality");
174 } else if (arg[0] == 'r' || !strcmp(arg, "-src-quality")) {
175 if (++i == argc) MissingArgument();
176 params.output.srcQuality = ParseInt(argv[i], "-r | --src-quality");
177 } else if (arg[0] == 'l' || !strcmp(arg, "-channellayout")) {
178 if (++i == argc) MissingArgument();
179 UInt32 tag = CAChannelLayouts::StringToConstant(argv[i]);
180 if (tag == CAChannelLayouts::kInvalidTag) {
181 fprintf(stderr, "unknown channel layout tag: %s\n\n", argv[i]);
182 usage();
183 }
184 if (params.output.channelLayoutTag == 0)
185 params.output.channelLayoutTag = tag;
186 else if (params.input.channelLayoutTag == 0) {
187 params.input.channelLayoutTag = params.output.channelLayoutTag;
188 params.output.channelLayoutTag = tag;
189 } else {
190 fprintf(stderr, "too many channel layout tags!\n\n");
191 usage();
192 }
193 } else if (arg[0] == 'c' || !strcmp(arg, "-channels")) {
194 if (++i == argc) MissingArgument();
195 params.output.channels = ParseInt(argv[i], "-c | --channels");
196 } else if (arg[0] == 'v' || !strcmp(arg, "-verbose")) {
197 params.flags |= CAAudioFileConverter::kOpt_Verbose;
198 } else if (arg[0] == 's' || !strcmp(arg, "-strategy")) {
199 if (++i == argc) MissingArgument();
200 params.output.strategy = ParseInt(argv[i], "-s | --strategy");
201 } else if (arg[0] == 't' || !strcmp(arg, "-tag")) {
202 params.flags |= CAAudioFileConverter::kOpt_CAFTag;
203 } else if (!strcmp(arg, "-prime-method")) {
204 if (++i == argc) MissingArgument();
205 params.output.primeMethod = ParseInt(argv[i], "-p | --prime-method");
206 } else if (!ParseOtherOption(argv, i)) {
207 fprintf(stderr, "unknown argument: %s\n\n", arg - 1);
208 usage();
209 }
210 }
211 }
212 if (params.input.filePath == NULL) {
213 fprintf(stderr, "no input file specified\n\n");
214 usage();
215 }
216
217 if (!(params.flags & CAAudioFileConverter::kOpt_CAFTag)) {
218 if (!gotOutDataFormat) {
219 if (params.output.fileType == 0) {
220 fprintf(stderr, "no output file or data format specified\n\n");
221 usage();
222 }
223 if (!CAAudioFileFormats::Instance()->InferDataFormatFromFileFormat(params.output.fileType, params.output.dataFormat)) {
224 fprintf(stderr, "Couldn't infer data format from file format\n\n");
225 usage();
226 }
227 } else if (params.output.fileType == 0) {
228 CAAudioFileFormats *formats = CAAudioFileFormats::Instance();
229 if (!formats->InferFileFormatFromFilename(params.output.filePath, params.output.fileType) && !formats->InferFileFormatFromDataFormat(params.output.dataFormat, params.output.fileType)) {
230 params.output.dataFormat.PrintFormat(stderr, "", "Couldn't infer file format from data format");
231 usage();
232 }
233 }
234 }
235
236 try {
237 mAFConverter->ConvertFile(params);
238 Success();
239 }
240 catch (CAXException &e) {
241 char buf[256];
242 fprintf(stderr, "Error: %s (%s)\n", e.mOperation, e.FormatError(buf, sizeof(buf)));
243 return 1;
244 }
245 catch (...) {
246 fprintf(stderr, "An unknown error occurred\n");
247 return 1;
248 }
249
250 return 0;
251}