blob: 1b127c7751cb7388660ebcfc77a1b6a3630b8ef7 [file] [log] [blame]
ztenghui6df48bf2013-02-07 15:12:10 -08001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//#define LOG_NDEBUG 0
18#define LOG_TAG "muxer"
19#include <utils/Log.h>
20
21#include <binder/ProcessState.h>
22#include <media/stagefright/foundation/ABuffer.h>
23#include <media/stagefright/foundation/ADebug.h>
24#include <media/stagefright/foundation/ALooper.h>
25#include <media/stagefright/foundation/AMessage.h>
26#include <media/stagefright/foundation/AString.h>
27#include <media/stagefright/DataSource.h>
28#include <media/stagefright/MediaCodec.h>
29#include <media/stagefright/MediaDefs.h>
30#include <media/stagefright/MediaMuxer.h>
31#include <media/stagefright/MetaData.h>
32#include <media/stagefright/NuMediaExtractor.h>
33
34static void usage(const char *me) {
35 fprintf(stderr, "usage: %s [-a] [-v] [-s <trim start time>]"
36 " [-e <trim end time>] [-o <output file>]"
37 " <input video file>\n", me);
38 fprintf(stderr, " -h help\n");
39 fprintf(stderr, " -a use audio\n");
40 fprintf(stderr, " -v use video\n");
41 fprintf(stderr, " -s Time in milli-seconds when the trim should start\n");
42 fprintf(stderr, " -e Time in milli-seconds when the trim should end\n");
43 fprintf(stderr, " -o output file name. Default is /sdcard/muxeroutput.mp4\n");
44
45 exit(1);
46}
47
48using namespace android;
49
50static int muxing(
51 const android::sp<android::ALooper> &looper,
52 const char *path,
53 bool useAudio,
54 bool useVideo,
55 const char *outputFileName,
56 bool enableTrim,
57 int trimStartTimeMs,
58 int trimEndTimeMs) {
59 sp<NuMediaExtractor> extractor = new NuMediaExtractor;
60 if (extractor->setDataSource(path) != OK) {
61 fprintf(stderr, "unable to instantiate extractor. %s\n", path);
62 return 1;
63 }
64
65 if (outputFileName == NULL) {
66 outputFileName = "/sdcard/muxeroutput.mp4";
67 }
68
69 ALOGV("input file %s, output file %s", path, outputFileName);
70 ALOGV("useAudio %d, useVideo %d", useAudio, useVideo);
71
72 sp<MediaMuxer> muxer = new MediaMuxer(outputFileName);
73
74 size_t trackCount = extractor->countTracks();
75 // Map the extractor's track index to the muxer's track index.
76 KeyedVector<size_t, ssize_t> trackIndexMap;
77 size_t bufferSize = 1 * 1024 * 1024; // default buffer size is 1MB.
78
79 bool haveAudio = false;
80 bool haveVideo = false;
81
82 int64_t trimStartTimeUs = trimStartTimeMs * 1000;
83 int64_t trimEndTimeUs = trimEndTimeMs * 1000;
84 bool trimStarted = false;
85 int64_t trimOffsetTimeUs = 0;
86
87 for (size_t i = 0; i < trackCount; ++i) {
88 sp<AMessage> format;
89 status_t err = extractor->getTrackFormat(i, &format);
90 CHECK_EQ(err, (status_t)OK);
91 ALOGV("extractor getTrackFormat: %s", format->debugString().c_str());
92
93 AString mime;
94 CHECK(format->findString("mime", &mime));
95
96 bool isAudio = !strncasecmp(mime.c_str(), "audio/", 6);
97 bool isVideo = !strncasecmp(mime.c_str(), "video/", 6);
98
99 if (useAudio && !haveAudio && isAudio) {
100 haveAudio = true;
101 } else if (useVideo && !haveVideo && isVideo) {
102 haveVideo = true;
103 } else {
104 continue;
105 }
106
107 if (isVideo) {
108 int width , height;
109 CHECK(format->findInt32("width", &width));
110 CHECK(format->findInt32("height", &height));
111 bufferSize = width * height * 4; // Assuming it is maximally 4BPP
112 }
113
114 int64_t duration;
115 CHECK(format->findInt64("durationUs", &duration));
116
117 // Since we got the duration now, correct the start time.
118 if (enableTrim) {
119 if (trimStartTimeUs > duration) {
120 fprintf(stderr, "Warning: trimStartTimeUs > duration,"
121 " reset to 0\n");
122 trimStartTimeUs = 0;
123 }
124 }
125
126 ALOGV("selecting track %d", i);
127
128 err = extractor->selectTrack(i);
129 CHECK_EQ(err, (status_t)OK);
130
131 ssize_t newTrackIndex = muxer->addTrack(format);
132 CHECK_GE(newTrackIndex, 0);
133 trackIndexMap.add(i, newTrackIndex);
134 }
135
136 int64_t muxerStartTimeUs = ALooper::GetNowUs();
137
138 bool sawInputEOS = false;
139
140 size_t trackIndex = -1;
141 sp<ABuffer> newBuffer = new ABuffer(bufferSize);
142
143 muxer->start();
144
145 while (!sawInputEOS) {
146 status_t err = extractor->getSampleTrackIndex(&trackIndex);
147 if (err != OK) {
148 ALOGV("saw input eos, err %d", err);
149 sawInputEOS = true;
150 break;
151 } else {
152 err = extractor->readSampleData(newBuffer);
153 CHECK_EQ(err, (status_t)OK);
154
155 int64_t timeUs;
156 err = extractor->getSampleTime(&timeUs);
157 CHECK_EQ(err, (status_t)OK);
158
159 sp<MetaData> meta;
160 err = extractor->getSampleMeta(&meta);
161 CHECK_EQ(err, (status_t)OK);
162
163 uint32_t sampleFlags = 0;
164 int32_t val;
165 if (meta->findInt32(kKeyIsSyncFrame, &val) && val != 0) {
166 // We only support BUFFER_FLAG_SYNCFRAME in the flag for now.
167 sampleFlags |= MediaCodec::BUFFER_FLAG_SYNCFRAME;
168
169 // We turn on trimming at the sync frame.
170 if (enableTrim && timeUs > trimStartTimeUs &&
171 timeUs <= trimEndTimeUs) {
172 if (trimStarted == false) {
173 trimOffsetTimeUs = timeUs;
174 }
175 trimStarted = true;
176 }
177 }
178 // Trim can end at any non-sync frame.
179 if (enableTrim && timeUs > trimEndTimeUs) {
180 trimStarted = false;
181 }
182
183 if (!enableTrim || (enableTrim && trimStarted)) {
184 err = muxer->writeSampleData(newBuffer,
185 trackIndexMap.valueFor(trackIndex),
186 timeUs - trimOffsetTimeUs, sampleFlags);
187 }
188
189 extractor->advance();
190 }
191 }
192
193 muxer->stop();
194 newBuffer.clear();
195 trackIndexMap.clear();
196
197 int64_t elapsedTimeUs = ALooper::GetNowUs() - muxerStartTimeUs;
198 fprintf(stderr, "SUCCESS: muxer generate the video in %lld ms\n",
199 elapsedTimeUs / 1000);
200
201 return 0;
202}
203
204int main(int argc, char **argv) {
205 const char *me = argv[0];
206
207 bool useAudio = false;
208 bool useVideo = false;
209 char *outputFileName = NULL;
210 int trimStartTimeMs = -1;
211 int trimEndTimeMs = -1;
212 // When trimStartTimeMs and trimEndTimeMs seems valid, we turn this switch
213 // to true.
214 bool enableTrim = false;
215
216 int res;
217 while ((res = getopt(argc, argv, "h?avo:s:e:")) >= 0) {
218 switch (res) {
219 case 'a':
220 {
221 useAudio = true;
222 break;
223 }
224
225 case 'v':
226 {
227 useVideo = true;
228 break;
229 }
230
231 case 'o':
232 {
233 outputFileName = optarg;
234 break;
235 }
236
237 case 's':
238 {
239 trimStartTimeMs = atoi(optarg);
240 break;
241 }
242
243 case 'e':
244 {
245 trimEndTimeMs = atoi(optarg);
246 break;
247 }
248
249 case '?':
250 case 'h':
251 default:
252 {
253 usage(me);
254 }
255 }
256 }
257
258 argc -= optind;
259 argv += optind;
260
261 if (argc != 1) {
262 usage(me);
263 }
264
265 if (trimStartTimeMs < 0 || trimEndTimeMs < 0) {
266 // If no input on either 's' or 'e', or they are obviously wrong input,
267 // then turn off trimming.
268 ALOGV("Trimming is disabled, copying the whole length video.");
269 enableTrim = false;
270 } else if (trimStartTimeMs > trimEndTimeMs) {
271 fprintf(stderr, "ERROR: start time is bigger\n");
272 return 1;
273 } else {
274 enableTrim = true;
275 }
276
277 if (!useAudio && !useVideo) {
278 fprintf(stderr, "ERROR: Missing both -a and -v, no track to mux then.\n");
279 return 1;
280 }
281 ProcessState::self()->startThreadPool();
282
283 // Make sure setDataSource() works.
284 DataSource::RegisterDefaultSniffers();
285
286 sp<ALooper> looper = new ALooper;
287 looper->start();
288
289 int result = muxing(looper, argv[0], useAudio, useVideo, outputFileName,
290 enableTrim, trimStartTimeMs, trimEndTimeMs);
291
292 looper->stop();
293
294 return result;
295}