blob: c95a0c25c13a0eeedbab3268757ff21af5259f37 [file] [log] [blame]
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08001/*
2 * Copyright (C) 2011 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#include <dlfcn.h>
18#include <stdio.h>
19#include <unistd.h>
20#include <utils/Log.h>
21#include <utils/threads.h>
22#include <VideoEditorClasses.h>
23#include <VideoEditorJava.h>
24#include <VideoEditorOsal.h>
25#include <VideoEditorLogging.h>
26#include <marker.h>
27#include <VideoEditorClasses.h>
28#include <VideoEditorThumbnailMain.h>
29#include <M4OSA_Debug.h>
30#include <M4xVSS_Internal.h>
31#include <surfaceflinger/Surface.h>
32#include <surfaceflinger/ISurface.h>
33#include "VideoEditorPreviewController.h"
34
35#include "VideoEditorMain.h"
36
37extern "C" {
38#include <M4OSA_Clock.h>
39#include <M4OSA_CharStar.h>
40#include <M4OSA_Error.h>
41#include <M4OSA_FileCommon.h>
42#include <M4OSA_FileReader.h>
43#include <M4OSA_FileWriter.h>
Dharmaray Kundargicd196d32011-01-16 15:47:16 -080044#include <M4OSA_Memory.h>
Dharmaray Kundargicd196d32011-01-16 15:47:16 -080045#include <M4OSA_Thread.h>
46#include <M4xVSS_API.h>
47#include <M4VSS3GPP_ErrorCodes.h>
48#include <M4MCS_API.h>
49#include <M4MCS_ErrorCodes.h>
Dharmaray Kundargicd196d32011-01-16 15:47:16 -080050#include <M4READER_Common.h>
51#include <M4WRITER_common.h>
52};
53
54
55using namespace android;
56
57#define THREAD_STACK_SIZE (65536)
58
59#define VIDEOEDITOR_VERSION_MAJOR 0
60#define VIDEOEDITOR_VERSION_MINOR 0
61#define VIDEOEDITOR_VERSION_REVISION 1
62
63
64typedef enum
65{
66 ManualEditState_NOT_INITIALIZED,
67 ManualEditState_INITIALIZED,
68 ManualEditState_ANALYZING,
69 ManualEditState_ANALYZING_ERROR,
70 ManualEditState_OPENED,
71 ManualEditState_SAVING,
72 ManualEditState_SAVING_ERROR,
73 ManualEditState_SAVED,
74 ManualEditState_STOPPING
75} ManualEditState;
76
77typedef struct
78{
79 JavaVM* pVM;
80 jobject engine;
81 jmethodID onCompletionMethodId;
82 jmethodID onErrorMethodId;
83 jmethodID onWarningMethodId;
84 jmethodID onProgressUpdateMethodId;
85 jmethodID onPreviewProgressUpdateMethodId;
Dharmaray Kundargic16ccc12011-01-21 19:20:49 -080086 jmethodID previewFrameEditInfoId;
Dharmaray Kundargicd196d32011-01-16 15:47:16 -080087 M4xVSS_InitParams initParams;
88 void* pTextRendererHandle;
89 M4xVSS_getTextRgbBufferFct pTextRendererFunction;
90 M4OSA_Context engineContext;
91 ManualEditState state;
92 M4VSS3GPP_EditSettings* pEditSettings;
93 M4OSA_Context threadContext;
94 M4OSA_ERR threadResult;
95 M4OSA_UInt8 threadProgress;
96 VideoEditorPreviewController *mPreviewController;
97 M4xVSS_AudioMixingSettings *mAudioSettings;
98 /* Audio Graph changes */
99 M4OSA_Context pAudioGraphMCSCtx;
100 M4OSA_Bool bSkipState;
101 jmethodID onAudioGraphProgressUpdateMethodId;
102 Mutex mLock;
Dharmaray Kundargic16ccc12011-01-21 19:20:49 -0800103 bool mIsUpdateOverlay;
104 char *mOverlayFileName;
105 int mOverlayRenderingMode;
Dharmaray Kundargicd196d32011-01-16 15:47:16 -0800106} ManualEditContext;
107
108extern "C" M4OSA_ERR M4MCS_open_normalMode(
109 M4MCS_Context pContext,
110 M4OSA_Void* pFileIn,
111 M4VIDEOEDITING_FileType InputFileType,
112 M4OSA_Void* pFileOut,
113 M4OSA_Void* pTempFile);
114
115static M4OSA_ERR videoEditor_toUTF8Fct(
116 M4OSA_Void* pBufferIn,
117 M4OSA_UInt8* pBufferOut,
118 M4OSA_UInt32* bufferOutSize);
119
120static M4OSA_ERR videoEditor_fromUTF8Fct(
121 M4OSA_UInt8* pBufferIn,
122 M4OSA_Void* pBufferOut,
123 M4OSA_UInt32* bufferOutSize);
124
125static M4OSA_ERR videoEditor_getTextRgbBufferFct(
126 M4OSA_Void* pRenderingData,
127 M4OSA_Void* pTextBuffer,
128 M4OSA_UInt32 textBufferSize,
129 M4VIFI_ImagePlane** pOutputPlane);
130
131static void videoEditor_callOnProgressUpdate(
132 ManualEditContext* pContext,
133 int task,
134 int progress);
135
136static void videoEditor_freeContext(
137 JNIEnv* pEnv,
138 ManualEditContext** ppContext);
139
140static M4OSA_ERR videoEditor_threadProc(
141 M4OSA_Void* param);
142
143static jobject videoEditor_getVersion(
144 JNIEnv* pEnv,
145 jobject thiz);
146
147static void videoEditor_init(
148 JNIEnv* pEnv,
149 jobject thiz,
150 jstring tempPath,
151 jstring textRendererPath);
152
153static void videoEditor_loadSettings(
154 JNIEnv* pEnv,
155 jobject thiz,
156 jobject settings);
157
158static void videoEditor_unloadSettings(
159 JNIEnv* pEnv,
160 jobject thiz);
161
162
163static void videoEditor_stopEncoding(
164 JNIEnv* pEnv,
165 jobject thiz);
166
167static void videoEditor_release(
168 JNIEnv* pEnv,
169 jobject thiz);
170static int videoEditor_getPixels(
171 JNIEnv* env,
172 jobject thiz,
173 jstring path,
174 jintArray pixelArray,
175 M4OSA_UInt32 width,
176 M4OSA_UInt32 height,
177 M4OSA_UInt32 timeMS);
178static int videoEditor_getPixelsList(
179 JNIEnv* env,
180 jobject thiz,
181 jstring path,
182 jintArray pixelArray,
183 M4OSA_UInt32 width,
184 M4OSA_UInt32 height,
185 M4OSA_UInt32 deltatimeMS,
186 M4OSA_UInt32 noOfThumbnails,
187 M4OSA_UInt32 startTime,
188 M4OSA_UInt32 endTime);
189
190static void
191videoEditor_startPreview(
192 JNIEnv* pEnv,
193 jobject thiz,
194 jobject mSurface,
195 jlong fromMs,
196 jlong toMs,
197 jint callbackInterval,
198 jboolean loop);
199
200static void
201videoEditor_populateSettings(
202 JNIEnv* pEnv,
203 jobject thiz,
204 jobject settings,
205 jobject object,
206 jobject audioSettingObject);
207
Santosh Madhavadf4723e2011-02-16 22:32:02 -0800208static int videoEditor_stopPreview(JNIEnv* pEnv,
Dharmaray Kundargicd196d32011-01-16 15:47:16 -0800209 jobject thiz);
210
211static jobject
212videoEditor_getProperties(
213 JNIEnv* pEnv,
214 jobject thiz,
215 jstring file);
216
217static int videoEditor_renderPreviewFrame(JNIEnv* pEnv,
218 jobject thiz,
219 jobject mSurface,
220 jlong fromMs,
221 jint surfaceWidth,
222 jint surfaceHeight);
223
224static int videoEditor_registerManualEditMethods(
225 JNIEnv* pEnv);
226
227static void jniPreviewProgressCallback(void* cookie, M4OSA_UInt32 msgType,
Dharmaray Kundargic16ccc12011-01-21 19:20:49 -0800228 void *argc);
Dharmaray Kundargicd196d32011-01-16 15:47:16 -0800229
Dheeraj Sharma05d9f812011-01-23 15:14:05 -0800230static int videoEditor_renderMediaItemPreviewFrame(JNIEnv* pEnv,
Dharmaray Kundargicd196d32011-01-16 15:47:16 -0800231 jobject thiz,
232 jobject mSurface,
233 jstring filePath,
234 jint frameWidth,
235 jint frameHeight,
236 jint surfaceWidth,
237 jint surfaceHeight,
238 jlong fromMs);
239
240static int videoEditor_generateAudioWaveFormSync ( JNIEnv* pEnv,
241 jobject thiz,
242 jstring pcmfilePath,
243 jstring outGraphfilePath,
244 jint frameDuration,
245 jint channels,
246 jint samplesCount);
247
248static int videoEditor_generateAudioRawFile(JNIEnv* pEnv,
249 jobject thiz,
250 jstring infilePath,
251 jstring pcmfilePath );
252
253M4OSA_ERR videoEditor_generateAudio(JNIEnv* pEnv,ManualEditContext* pContext,
254 M4OSA_Char* infilePath,
255 M4OSA_Char* pcmfilePath );
256
257static int
258videoEditor_generateClip(
259 JNIEnv* pEnv,
260 jobject thiz,
261 jobject settings);
262
Dheeraj Sharma05d9f812011-01-23 15:14:05 -0800263static void videoEditor_clearSurface(JNIEnv* pEnv,
264 jobject thiz,
Dheeraj Sharma1ac760a2011-01-24 14:46:30 -0800265 jobject surface);
Dharmaray Kundargicd196d32011-01-16 15:47:16 -0800266
267static JNINativeMethod gManualEditMethods[] = {
268 {"getVersion", "()L"VERSION_CLASS_NAME";",
269 (void *)videoEditor_getVersion },
270 {"_init", "(Ljava/lang/String;Ljava/lang/String;)V",
271 (void *)videoEditor_init },
272 {"nativeStartPreview", "(Landroid/view/Surface;JJIZ)V",
273 (void *)videoEditor_startPreview },
274 {"nativePopulateSettings",
275 "(L"EDIT_SETTINGS_CLASS_NAME";L"PREVIEW_PROPERTIES_CLASS_NAME";L"
276 AUDIO_SETTINGS_CLASS_NAME";)V",
277 (void *)videoEditor_populateSettings },
278 {"nativeRenderPreviewFrame", "(Landroid/view/Surface;JII)I",
279 (int *)videoEditor_renderPreviewFrame },
280 {"nativeRenderMediaItemPreviewFrame",
281 "(Landroid/view/Surface;Ljava/lang/String;IIIIJ)I",
282 (int *)videoEditor_renderMediaItemPreviewFrame },
Santosh Madhavadf4723e2011-02-16 22:32:02 -0800283 {"nativeStopPreview", "()I",
284 (int *)videoEditor_stopPreview },
Dharmaray Kundargicd196d32011-01-16 15:47:16 -0800285 {"stopEncoding", "()V",
286 (void *)videoEditor_stopEncoding },
287 {"release", "()V",
288 (void *)videoEditor_release },
289 {"nativeGetPixels", "(Ljava/lang/String;[IIIJ)I",
290 (void*)videoEditor_getPixels },
291 {"nativeGetPixelsList", "(Ljava/lang/String;[IIIIIJJ)I",
292 (void*)videoEditor_getPixelsList },
293 {"getMediaProperties",
294 "(Ljava/lang/String;)Landroid/media/videoeditor/MediaArtistNativeHelper$Properties;",
295 (void *)videoEditor_getProperties },
296 {"nativeGenerateAudioGraph","(Ljava/lang/String;Ljava/lang/String;III)I",
297 (int *)videoEditor_generateAudioWaveFormSync },
298 {"nativeGenerateRawAudio", "(Ljava/lang/String;Ljava/lang/String;)I",
299 (int *)videoEditor_generateAudioRawFile },
300 {"nativeGenerateClip", "(L"EDIT_SETTINGS_CLASS_NAME";)I",
301 (void *)videoEditor_generateClip },
Dheeraj Sharma1ac760a2011-01-24 14:46:30 -0800302 {"nativeClearSurface", "(Landroid/view/Surface;)V",
Dheeraj Sharma05d9f812011-01-23 15:14:05 -0800303 (void *)videoEditor_clearSurface },
Dharmaray Kundargicd196d32011-01-16 15:47:16 -0800304};
305
306// temp file name of VSS out file
Dheeraj Sharma5618e032011-01-29 21:29:49 -0800307#define TEMP_MCS_OUT_FILE_PATH "tmpOut.3gp"
Dharmaray Kundargicd196d32011-01-16 15:47:16 -0800308
309void
310getClipSetting(
311 JNIEnv* pEnv,
312 jobject object,
313 M4VSS3GPP_ClipSettings* pSettings)
314{
315
316 jfieldID fid;
317 int field = 0;
318 bool needToBeLoaded = true;
319 jclass clazz = pEnv->FindClass(PROPERTIES_CLASS_NAME);
320
321 videoEditJava_checkAndThrowIllegalStateException(&needToBeLoaded, pEnv,
322 (M4OSA_NULL == clazz),
323 "not initialized");
324
325 fid = pEnv->GetFieldID(clazz,"duration","I");
326 pSettings->ClipProperties.uiClipDuration = pEnv->GetIntField(object,fid);
327 M4OSA_TRACE1_1("duration = %d",pSettings->ClipProperties.uiClipDuration);
328
329 fid = pEnv->GetFieldID(clazz,"videoFormat","I");
330 pSettings->ClipProperties.VideoStreamType =
331 (M4VIDEOEDITING_VideoFormat)pEnv->GetIntField(object,fid);
332 M4OSA_TRACE1_1("videoFormat = %d",pSettings->ClipProperties.VideoStreamType);
333
334 fid = pEnv->GetFieldID(clazz,"videoDuration","I");
335 pSettings->ClipProperties.uiClipVideoDuration = pEnv->GetIntField(object,fid);
336 M4OSA_TRACE1_1("videoDuration = %d",
337 pSettings->ClipProperties.uiClipVideoDuration);
338
339 fid = pEnv->GetFieldID(clazz,"width","I");
340 pSettings->ClipProperties.uiVideoWidth = pEnv->GetIntField(object,fid);
341 M4OSA_TRACE1_1("width = %d",pSettings->ClipProperties.uiVideoWidth);
342
343 fid = pEnv->GetFieldID(clazz,"height","I");
344 pSettings->ClipProperties.uiVideoHeight = pEnv->GetIntField(object,fid);
345 M4OSA_TRACE1_1("height = %d",pSettings->ClipProperties.uiVideoHeight);
346
347 fid = pEnv->GetFieldID(clazz,"audioFormat","I");
348 pSettings->ClipProperties.AudioStreamType =
349 (M4VIDEOEDITING_AudioFormat)pEnv->GetIntField(object,fid);
350 M4OSA_TRACE1_1("audioFormat = %d",pSettings->ClipProperties.AudioStreamType);
351
352 fid = pEnv->GetFieldID(clazz,"audioDuration","I");
353 pSettings->ClipProperties.uiClipAudioDuration = pEnv->GetIntField(object,fid);
354 M4OSA_TRACE1_1("audioDuration = %d",
355 pSettings->ClipProperties.uiClipAudioDuration);
356
357 fid = pEnv->GetFieldID(clazz,"audioBitrate","I");
358 pSettings->ClipProperties.uiAudioBitrate = pEnv->GetIntField(object,fid);
359 M4OSA_TRACE1_1("audioBitrate = %d",pSettings->ClipProperties.uiAudioBitrate);
360
361 fid = pEnv->GetFieldID(clazz,"audioChannels","I");
362 pSettings->ClipProperties.uiNbChannels = pEnv->GetIntField(object,fid);
363 M4OSA_TRACE1_1("audioChannels = %d",pSettings->ClipProperties.uiNbChannels);
364
365 fid = pEnv->GetFieldID(clazz,"audioSamplingFrequency","I");
366 pSettings->ClipProperties.uiSamplingFrequency = pEnv->GetIntField(object,fid);
367 M4OSA_TRACE1_1("audioSamplingFrequency = %d",
368 pSettings->ClipProperties.uiSamplingFrequency);
369
370 fid = pEnv->GetFieldID(clazz,"audioVolumeValue","I");
371 pSettings->ClipProperties.uiClipAudioVolumePercentage =
372 pEnv->GetIntField(object,fid);
373 M4OSA_TRACE1_1("audioVolumeValue = %d",
374 pSettings->ClipProperties.uiClipAudioVolumePercentage);
375}
376
377static void jniPreviewProgressCallback (void* cookie, M4OSA_UInt32 msgType,
Dharmaray Kundargic16ccc12011-01-21 19:20:49 -0800378 void *argc)
Dharmaray Kundargicd196d32011-01-16 15:47:16 -0800379{
380 ManualEditContext *pContext = (ManualEditContext *)cookie;
381 JNIEnv* pEnv = NULL;
382 bool isFinished = false;
383 int currentMs = 0;
384 int error = M4NO_ERROR;
Dharmaray Kundargic16ccc12011-01-21 19:20:49 -0800385 bool isUpdateOverlay = false;
386 int overlayEffectIndex;
387 char *extPos;
388 bool isSendProgress = true;
389 jstring tmpFileName;
390 VideoEditorCurretEditInfo *pCurrEditInfo;
Dharmaray Kundargicd196d32011-01-16 15:47:16 -0800391
392 // Attach the current thread.
393 pContext->pVM->AttachCurrentThread(&pEnv, NULL);
394 switch(msgType)
395 {
396 case MSG_TYPE_PROGRESS_INDICATION:
Dharmaray Kundargic16ccc12011-01-21 19:20:49 -0800397 currentMs = *(int*)argc;
Dharmaray Kundargicd196d32011-01-16 15:47:16 -0800398 break;
399 case MSG_TYPE_PLAYER_ERROR:
400 currentMs = -1;
Dharmaray Kundargic16ccc12011-01-21 19:20:49 -0800401 error = *(int*)argc;
Dharmaray Kundargicd196d32011-01-16 15:47:16 -0800402 break;
403 case MSG_TYPE_PREVIEW_END:
404 isFinished = true;
405 break;
Dharmaray Kundargic16ccc12011-01-21 19:20:49 -0800406 case MSG_TYPE_OVERLAY_UPDATE:
407 {
408 int overlayFileNameLen = 0;
409 isSendProgress = false;
410 pContext->mIsUpdateOverlay = true;
411 pCurrEditInfo = (VideoEditorCurretEditInfo*)argc;
412 overlayEffectIndex = pCurrEditInfo->overlaySettingsIndex;
413 LOGV("MSG_TYPE_OVERLAY_UPDATE");
414
415 if (pContext->mOverlayFileName != NULL) {
Shyam Pallapothuc0125dd2011-04-21 19:13:56 -0700416 free(pContext->mOverlayFileName);
Dharmaray Kundargic16ccc12011-01-21 19:20:49 -0800417 pContext->mOverlayFileName = NULL;
418 }
419
420 overlayFileNameLen =
421 strlen((const char*)pContext->pEditSettings->Effects[overlayEffectIndex].xVSS.pFramingFilePath);
422
423 pContext->mOverlayFileName =
Shyam Pallapothuc0125dd2011-04-21 19:13:56 -0700424 (char*)M4OSA_32bitAlignedMalloc(overlayFileNameLen+1,
Dharmaray Kundargic16ccc12011-01-21 19:20:49 -0800425 M4VS, (M4OSA_Char*)"videoEdito JNI overlayFile");
426 if (pContext->mOverlayFileName != NULL) {
427 strncpy (pContext->mOverlayFileName,
428 (const char*)pContext->pEditSettings->\
429 Effects[overlayEffectIndex].xVSS.pFramingFilePath, overlayFileNameLen);
430 //Change the name to png file
431 extPos = strstr(pContext->mOverlayFileName, ".rgb");
432 if (extPos != NULL) {
433 *extPos = '\0';
434 } else {
435 LOGE("ERROR the overlay file is incorrect");
436 }
437
438 strcat(pContext->mOverlayFileName, ".png");
439 LOGV("Conv string is %s", pContext->mOverlayFileName);
440 LOGV("Current Clip index = %d", pCurrEditInfo->clipIndex);
441
442 pContext->mOverlayRenderingMode = pContext->pEditSettings->\
443 pClipList[pCurrEditInfo->clipIndex]->xVSS.MediaRendering;
Dheeraj Sharmafb4e92c2011-02-10 17:00:39 -0800444 LOGV("rendering mode %d ", pContext->mOverlayRenderingMode);
Dharmaray Kundargic16ccc12011-01-21 19:20:49 -0800445
446 }
447
448 break;
449 }
450
451 case MSG_TYPE_OVERLAY_CLEAR:
452 isSendProgress = false;
Santosh Madhava71771b32011-02-28 18:30:53 -0800453 if (pContext->mOverlayFileName != NULL) {
Shyam Pallapothuc0125dd2011-04-21 19:13:56 -0700454 free(pContext->mOverlayFileName);
Santosh Madhava71771b32011-02-28 18:30:53 -0800455 pContext->mOverlayFileName = NULL;
456 }
457
Dharmaray Kundargibeb9fc82011-02-02 03:10:29 -0800458 LOGV("MSG_TYPE_OVERLAY_CLEAR");
Dharmaray Kundargic16ccc12011-01-21 19:20:49 -0800459 //argc is not used
460 pContext->mIsUpdateOverlay = true;
461 break;
Dharmaray Kundargicd196d32011-01-16 15:47:16 -0800462 default:
463 break;
464 }
465
Dharmaray Kundargic16ccc12011-01-21 19:20:49 -0800466 if (isSendProgress) {
467 tmpFileName = pEnv->NewStringUTF(pContext->mOverlayFileName);
468 pEnv->CallVoidMethod(pContext->engine,
469 pContext->onPreviewProgressUpdateMethodId,
470 currentMs,isFinished, pContext->mIsUpdateOverlay,
471 tmpFileName, pContext->mOverlayRenderingMode);
472
473 if (pContext->mIsUpdateOverlay) {
474 pContext->mIsUpdateOverlay = false;
475 }
476
477 if (tmpFileName) {
478 pEnv->DeleteLocalRef(tmpFileName);
479 }
480 }
Dharmaray Kundargicd196d32011-01-16 15:47:16 -0800481
482 // Detach the current thread.
483 pContext->pVM->DetachCurrentThread();
484
485}
Santosh Madhavadf4723e2011-02-16 22:32:02 -0800486static int videoEditor_stopPreview(JNIEnv* pEnv,
Dharmaray Kundargicd196d32011-01-16 15:47:16 -0800487 jobject thiz)
488{
489 ManualEditContext* pContext = M4OSA_NULL;
490 bool needToBeLoaded = true;
Santosh Madhavadf4723e2011-02-16 22:32:02 -0800491 M4OSA_UInt32 lastProgressTimeMs = 0;
492
Dharmaray Kundargicd196d32011-01-16 15:47:16 -0800493 // Get the context.
494 pContext =
495 (ManualEditContext*)videoEditClasses_getContext(&needToBeLoaded, pEnv, thiz);
496
497 // Make sure that the context was set.
498 videoEditJava_checkAndThrowIllegalStateException(&needToBeLoaded, pEnv,
499 (M4OSA_NULL == pContext),
500 "not initialized");
Santosh Madhavadf4723e2011-02-16 22:32:02 -0800501 lastProgressTimeMs = pContext->mPreviewController->stopPreview();
Dharmaray Kundargic16ccc12011-01-21 19:20:49 -0800502
503 if (pContext->mOverlayFileName != NULL) {
Shyam Pallapothuc0125dd2011-04-21 19:13:56 -0700504 free(pContext->mOverlayFileName);
Dharmaray Kundargic16ccc12011-01-21 19:20:49 -0800505 pContext->mOverlayFileName = NULL;
506 }
Santosh Madhavadf4723e2011-02-16 22:32:02 -0800507
508 return lastProgressTimeMs;
Dharmaray Kundargicd196d32011-01-16 15:47:16 -0800509}
510
Dheeraj Sharma05d9f812011-01-23 15:14:05 -0800511static void videoEditor_clearSurface(JNIEnv* pEnv,
512 jobject thiz,
Dheeraj Sharma1ac760a2011-01-24 14:46:30 -0800513 jobject surface)
Dheeraj Sharma05d9f812011-01-23 15:14:05 -0800514{
515 bool needToBeLoaded = true;
Dheeraj Sharma05d9f812011-01-23 15:14:05 -0800516 M4OSA_ERR result = M4NO_ERROR;
517 VideoEditor_renderPreviewFrameStr frameStr;
518 const char* pMessage = NULL;
Dheeraj Sharma1ac760a2011-01-24 14:46:30 -0800519 // Let the size be QVGA
520 int width = 320;
521 int height = 240;
Dheeraj Sharma05d9f812011-01-23 15:14:05 -0800522 ManualEditContext* pContext = M4OSA_NULL;
523
524 // Get the context.
525 pContext = (ManualEditContext*)videoEditClasses_getContext(&needToBeLoaded, pEnv, thiz);
526 VIDEOEDIT_LOG_FUNCTION(ANDROID_LOG_INFO,
527 "VIDEO_EDITOR","pContext = 0x%x",pContext);
528
529 // Make sure that the context was set.
530 videoEditJava_checkAndThrowIllegalStateException(&needToBeLoaded, pEnv,
531 (M4OSA_NULL == pContext),
532 "not initialized");
533
534 // Make sure that the context was set.
535 videoEditJava_checkAndThrowIllegalStateException(&needToBeLoaded, pEnv,
536 (M4OSA_NULL == pContext->mPreviewController),
537 "not initialized");
538
539 // Validate the surface parameter.
540 videoEditJava_checkAndThrowIllegalArgumentException(&needToBeLoaded, pEnv,
541 (NULL == surface),
542 "surface is null");
543
544 jclass surfaceClass = pEnv->FindClass("android/view/Surface");
545 videoEditJava_checkAndThrowIllegalStateException(&needToBeLoaded, pEnv,
546 (M4OSA_NULL == surfaceClass),
547 "not initialized");
548
549 jfieldID surface_native =
550 pEnv->GetFieldID(surfaceClass, ANDROID_VIEW_SURFACE_JNI_ID, "I");
551 videoEditJava_checkAndThrowIllegalStateException(&needToBeLoaded, pEnv,
552 (M4OSA_NULL == surface_native),
553 "not initialized");
554
555 Surface* const p = (Surface*)pEnv->GetIntField(surface, surface_native);
556 sp<Surface> previewSurface = sp<Surface>(p);
Rajneesh Chowdury8e7991a2011-03-15 16:20:17 -0700557 // Validate the mSurface's mNativeSurface field
558 videoEditJava_checkAndThrowIllegalStateException(&needToBeLoaded, pEnv,
559 (NULL == previewSurface.get()),
560 "mNativeSurface is null");
Dheeraj Sharma05d9f812011-01-23 15:14:05 -0800561
Dheeraj Sharma1ac760a2011-01-24 14:46:30 -0800562 frameStr.pBuffer = M4OSA_NULL;
Dheeraj Sharma05d9f812011-01-23 15:14:05 -0800563 frameStr.timeMs = 0;
564 frameStr.uiSurfaceWidth = width;
565 frameStr.uiSurfaceHeight = height;
566 frameStr.uiFrameWidth = width;
567 frameStr.uiFrameHeight = height;
568 frameStr.bApplyEffect = M4OSA_FALSE;
569 frameStr.clipBeginCutTime = 0;
570 frameStr.clipEndCutTime = 0;
571
Dheeraj Sharma1ac760a2011-01-24 14:46:30 -0800572 result = pContext->mPreviewController->clearSurface(previewSurface,
Dheeraj Sharma05d9f812011-01-23 15:14:05 -0800573 &frameStr);
574 videoEditJava_checkAndThrowRuntimeException(&needToBeLoaded, pEnv,
575 (M4NO_ERROR != result), result);
576
Dheeraj Sharma05d9f812011-01-23 15:14:05 -0800577 }
578
Dharmaray Kundargicd196d32011-01-16 15:47:16 -0800579static int videoEditor_renderPreviewFrame(JNIEnv* pEnv,
580 jobject thiz,
581 jobject mSurface,
582 jlong fromMs,
583 jint surfaceWidth,
584 jint surfaceHeight )
585{
586 bool needToBeLoaded = true;
587 M4OSA_ERR result = M4NO_ERROR;
588 M4OSA_UInt32 timeMs = (M4OSA_UInt32)fromMs;
589 M4OSA_UInt32 i=0,tnTimeMs = 0, framesizeYuv =0;
590 M4VIFI_UInt8 *pixelArray = M4OSA_NULL;
591 M4OSA_UInt32 iCurrentClipIndex = 0, uiNumberOfClipsInStoryBoard =0,
592 uiClipDuration = 0, uiTotalClipDuration = 0,
593 iIncrementedDuration = 0;
594 VideoEditor_renderPreviewFrameStr frameStr;
595 M4OSA_Context tnContext = M4OSA_NULL;
596 const char* pMessage = NULL;
James Dong581cad22011-01-19 11:35:06 -0800597 M4VIFI_ImagePlane *yuvPlane = NULL;
Dharmaray Kundargic16ccc12011-01-21 19:20:49 -0800598 VideoEditorCurretEditInfo currEditInfo;
Dharmaray Kundargicd196d32011-01-16 15:47:16 -0800599
600 VIDEOEDIT_LOG_FUNCTION(ANDROID_LOG_INFO,
601 "VIDEO_EDITOR", "surfaceWidth = %d",surfaceWidth);
602 VIDEOEDIT_LOG_FUNCTION(ANDROID_LOG_INFO,
603 "VIDEO_EDITOR", "surfaceHeight = %d",surfaceHeight);
604 ManualEditContext* pContext = M4OSA_NULL;
605 // Get the context.
606 pContext =
607 (ManualEditContext*)videoEditClasses_getContext(&needToBeLoaded, pEnv, thiz);
608 VIDEOEDIT_LOG_FUNCTION(ANDROID_LOG_INFO,
609 "VIDEO_EDITOR","pContext = 0x%x",pContext);
610
611 // Make sure that the context was set.
612 videoEditJava_checkAndThrowIllegalStateException(&needToBeLoaded, pEnv,
613 (M4OSA_NULL == pContext),
614 "not initialized");
615
616 // Make sure that the context was set.
617 videoEditJava_checkAndThrowIllegalStateException(&needToBeLoaded, pEnv,
618 (M4OSA_NULL == pContext->mPreviewController),
619 "not initialized");
620
621 // Validate the mSurface parameter.
622 videoEditJava_checkAndThrowIllegalArgumentException(&needToBeLoaded, pEnv,
623 (NULL == mSurface),
624 "mSurface is null");
625 jclass surfaceClass = pEnv->FindClass("android/view/Surface");
626 videoEditJava_checkAndThrowIllegalStateException(&needToBeLoaded, pEnv,
627 (M4OSA_NULL == surfaceClass),
628 "not initialized");
629
630 jfieldID surface_native =
631 pEnv->GetFieldID(surfaceClass, ANDROID_VIEW_SURFACE_JNI_ID, "I");
632 videoEditJava_checkAndThrowIllegalStateException(&needToBeLoaded, pEnv,
633 (M4OSA_NULL == surface_native),
634 "not initialized");
635
636 Surface* const p = (Surface*)pEnv->GetIntField(mSurface, surface_native);
637 sp<Surface> previewSurface = sp<Surface>(p);
Rajneesh Chowdury8e7991a2011-03-15 16:20:17 -0700638 // Validate the mSurface's mNativeSurface field
639 videoEditJava_checkAndThrowIllegalStateException(&needToBeLoaded, pEnv,
640 (NULL == previewSurface.get()),
641 "mNativeSurface is null");
Dharmaray Kundargicd196d32011-01-16 15:47:16 -0800642
643 /* Determine the total number of clips, total duration*/
644 uiNumberOfClipsInStoryBoard = pContext->pEditSettings->uiClipNumber;
645
646 for (i = 0; i < uiNumberOfClipsInStoryBoard; i++) {
647 uiClipDuration = pContext->pEditSettings->pClipList[i]->uiEndCutTime -
648 pContext->pEditSettings->pClipList[i]->uiBeginCutTime;
649 uiTotalClipDuration += uiClipDuration;
650 }
651
652 /* determine the clip whose thumbnail needs to be rendered*/
653 if (timeMs == 0) {
654 iCurrentClipIndex = 0;
655 i=0;
656 } else {
657 VIDEOEDIT_LOG_FUNCTION(ANDROID_LOG_INFO, "VIDEO_EDITOR",
658 "videoEditor_renderPreviewFrame() timeMs=%d", timeMs);
659
660 if (timeMs > uiTotalClipDuration) {
661 VIDEOEDIT_LOG_FUNCTION(ANDROID_LOG_INFO, "VIDEO_EDITOR",
662 "videoEditor_renderPreviewFrame() timeMs > uiTotalClipDuration");
663 pMessage = videoEditJava_getErrorName(M4ERR_PARAMETER);
664 jniThrowException(pEnv, "java/lang/IllegalArgumentException", pMessage);
665 return -1;
666 }
667
668 for (i = 0; i < uiNumberOfClipsInStoryBoard; i++) {
Dheeraj Sharmafb4e92c2011-02-10 17:00:39 -0800669 if (timeMs <= (iIncrementedDuration +
Dharmaray Kundargicd196d32011-01-16 15:47:16 -0800670 (pContext->pEditSettings->pClipList[i]->uiEndCutTime -
671 pContext->pEditSettings->pClipList[i]->uiBeginCutTime)))
672 {
673 iCurrentClipIndex = i;
674 VIDEOEDIT_LOG_FUNCTION(ANDROID_LOG_INFO, "VIDEO_EDITOR",
675 "videoEditor_renderPreviewFrame() iCurrentClipIndex=%d for timeMs=%d",
676 iCurrentClipIndex, timeMs);
677 break;
678 }
679 else {
680 iIncrementedDuration = iIncrementedDuration +
681 (pContext->pEditSettings->pClipList[i]->uiEndCutTime -
682 pContext->pEditSettings->pClipList[i]->uiBeginCutTime);
683 }
684 }
685 }
686 /* If timestamp is beyond story board duration, return*/
687 if (i >= uiNumberOfClipsInStoryBoard) {
688 if (timeMs == iIncrementedDuration) {
689 iCurrentClipIndex = i-1;
690 } else {
691 return -1;
692 }
693 }
694
695 /*+ Handle the image files here */
696 if (pContext->pEditSettings->pClipList[iCurrentClipIndex]->FileType ==
697 /*M4VIDEOEDITING_kFileType_JPG*/ M4VIDEOEDITING_kFileType_ARGB8888 ) {
698 VIDEOEDIT_LOG_FUNCTION(ANDROID_LOG_INFO, "VIDEO_EDITOR", " iCurrentClipIndex %d ", iCurrentClipIndex);
699 VIDEOEDIT_LOG_FUNCTION(ANDROID_LOG_INFO, "VIDEO_EDITOR",
700 " Height = %d",
701 pContext->pEditSettings->pClipList[iCurrentClipIndex]->ClipProperties.uiVideoHeight);
702
703 VIDEOEDIT_LOG_FUNCTION(ANDROID_LOG_INFO, "VIDEO_EDITOR",
704 " Width = %d",
705 pContext->pEditSettings->pClipList[iCurrentClipIndex]->ClipProperties.uiVideoWidth);
706
707 LvGetImageThumbNail((const char *)pContext->pEditSettings->\
708 pClipList[iCurrentClipIndex]->pFile,
709 pContext->pEditSettings->pClipList[iCurrentClipIndex]->ClipProperties.uiVideoHeight,
710 pContext->pEditSettings->pClipList[iCurrentClipIndex]->ClipProperties.uiVideoWidth,
711 (M4OSA_Void **)&frameStr.pBuffer);
Dheeraj Sharmafb4e92c2011-02-10 17:00:39 -0800712 tnTimeMs = (M4OSA_UInt32)timeMs;
Dharmaray Kundargicd196d32011-01-16 15:47:16 -0800713 } else {
714 /* Handle 3gp/mp4 Clips here */
715 /* get thumbnail*/
716 result = ThumbnailOpen(&tnContext,
717 (const M4OSA_Char*)pContext->pEditSettings->\
718 pClipList[iCurrentClipIndex]->pFile, M4OSA_TRUE);
719 if (result != M4NO_ERROR || tnContext == M4OSA_NULL) {
720 return -1;
721 }
722
723 /* timeMs is relative to storyboard; in this api it shud be relative to this clip */
724 if ((i >= uiNumberOfClipsInStoryBoard) &&
725 (timeMs == iIncrementedDuration)) {
726 tnTimeMs = pContext->pEditSettings->\
727 pClipList[iCurrentClipIndex]->uiEndCutTime;
728 } else {
729 tnTimeMs = pContext->pEditSettings->\
730 pClipList[iCurrentClipIndex]->uiBeginCutTime
731 + (timeMs - iIncrementedDuration);
732 }
733
734 VIDEOEDIT_LOG_API(ANDROID_LOG_INFO, "VIDEO_EDITOR",
735 "video width = %d",pContext->pEditSettings->pClipList[iCurrentClipIndex]->\
736 ClipProperties.uiVideoWidth);
737 VIDEOEDIT_LOG_API(ANDROID_LOG_INFO, "VIDEO_EDITOR",
738 "video height = %d",pContext->pEditSettings->pClipList[iCurrentClipIndex]->\
739 ClipProperties.uiVideoHeight);
740 VIDEOEDIT_LOG_API(ANDROID_LOG_INFO, "VIDEO_EDITOR",
741 "current clip index = %d",iCurrentClipIndex);
742
743 M4OSA_UInt32 width = pContext->pEditSettings->pClipList[iCurrentClipIndex]->\
744 ClipProperties.uiVideoWidth;
745 M4OSA_UInt32 height = pContext->pEditSettings->pClipList[iCurrentClipIndex]->\
746 ClipProperties.uiVideoHeight;
747
748 framesizeYuv = width * height * 1.5;
749
Shyam Pallapothuc0125dd2011-04-21 19:13:56 -0700750 pixelArray = (M4VIFI_UInt8 *)M4OSA_32bitAlignedMalloc(framesizeYuv, M4VS,
Dharmaray Kundargicd196d32011-01-16 15:47:16 -0800751 (M4OSA_Char*)"videoEditor pixelArray");
752 if (pixelArray == M4OSA_NULL) {
753 VIDEOEDIT_LOG_FUNCTION(ANDROID_LOG_INFO, "VIDEO_EDITOR",
754 "videoEditor_renderPreviewFrame() malloc error");
755 ThumbnailClose(tnContext);
756 pMessage = videoEditJava_getErrorName(M4ERR_ALLOC);
757 jniThrowException(pEnv, "java/lang/RuntimeException", pMessage);
758 return -1;
759 }
760
761 result = ThumbnailGetPixels16(tnContext, (M4OSA_Int16 *)pixelArray,
762 pContext->pEditSettings->pClipList[iCurrentClipIndex]->\
763 ClipProperties.uiVideoWidth,
764 pContext->pEditSettings->pClipList[iCurrentClipIndex]->\
765 ClipProperties.uiVideoHeight,
766 &tnTimeMs);
767 if (result != M4NO_ERROR) {
Shyam Pallapothuc0125dd2011-04-21 19:13:56 -0700768 free(pixelArray);
Dharmaray Kundargicd196d32011-01-16 15:47:16 -0800769 ThumbnailClose(tnContext);
770 return -1;
771 }
772
773 ThumbnailClose(tnContext);
774 tnContext = M4OSA_NULL;
775
776#ifdef DUMPTOFILE
777 {
778 M4OSA_Context fileContext;
779 M4OSA_Char* fileName = (M4OSA_Char*)"/mnt/sdcard/FirstRGB565.raw";
Dheeraj Sharma7743fa62011-04-12 14:53:58 -0700780 remove((const char *)fileName);
Dharmaray Kundargicd196d32011-01-16 15:47:16 -0800781 M4OSA_fileWriteOpen(&fileContext, (M4OSA_Void*) fileName,\
782 M4OSA_kFileWrite|M4OSA_kFileCreate);
783 M4OSA_fileWriteData(fileContext, (M4OSA_MemAddr8) pixelArray,
784 framesizeYuv);
785 M4OSA_fileWriteClose(fileContext);
786 }
787#endif
788
789 /**
790 * Allocate output YUV planes
791 */
Shyam Pallapothuc0125dd2011-04-21 19:13:56 -0700792 yuvPlane = (M4VIFI_ImagePlane*)M4OSA_32bitAlignedMalloc(3*sizeof(M4VIFI_ImagePlane), M4VS,
Dharmaray Kundargicd196d32011-01-16 15:47:16 -0800793 (M4OSA_Char*)"videoEditor_renderPreviewFrame Output plane YUV");
794 if (yuvPlane == M4OSA_NULL) {
795 VIDEOEDIT_LOG_FUNCTION(ANDROID_LOG_INFO, "VIDEO_EDITOR",
796 "videoEditor_renderPreviewFrame() malloc error for yuv plane");
Shyam Pallapothuc0125dd2011-04-21 19:13:56 -0700797 free(pixelArray);
Dharmaray Kundargicd196d32011-01-16 15:47:16 -0800798 pMessage = videoEditJava_getErrorName(M4ERR_ALLOC);
799 jniThrowException(pEnv, "java/lang/RuntimeException", pMessage);
800 return -1;
801 }
802
803 yuvPlane[0].u_width = width;
804 yuvPlane[0].u_height = height;
805 yuvPlane[0].u_topleft = 0;
806 yuvPlane[0].u_stride = width;
807 yuvPlane[0].pac_data = (M4VIFI_UInt8*)pixelArray;
808
809 yuvPlane[1].u_width = width>>1;
810 yuvPlane[1].u_height = height>>1;
811 yuvPlane[1].u_topleft = 0;
812 yuvPlane[1].u_stride = width>>1;
813 yuvPlane[1].pac_data = yuvPlane[0].pac_data
814 + yuvPlane[0].u_width * yuvPlane[0].u_height;
815 yuvPlane[2].u_width = (width)>>1;
816 yuvPlane[2].u_height = (height)>>1;
817 yuvPlane[2].u_topleft = 0;
818 yuvPlane[2].u_stride = (width)>>1;
819 yuvPlane[2].pac_data = yuvPlane[1].pac_data
820 + yuvPlane[1].u_width * yuvPlane[1].u_height;
821
822#ifdef DUMPTOFILE
823 {
824 M4OSA_Context fileContext;
825 M4OSA_Char* fileName = (M4OSA_Char*)"/mnt/sdcard/ConvertedYuv.yuv";
Dheeraj Sharma7743fa62011-04-12 14:53:58 -0700826 remove((const char *)fileName);
Dharmaray Kundargicd196d32011-01-16 15:47:16 -0800827 M4OSA_fileWriteOpen(&fileContext, (M4OSA_Void*) fileName,\
828 M4OSA_kFileWrite|M4OSA_kFileCreate);
829 M4OSA_fileWriteData(fileContext,
830 (M4OSA_MemAddr8) yuvPlane[0].pac_data, framesizeYuv);
831 M4OSA_fileWriteClose(fileContext);
832 }
833#endif
834
835 /* Fill up the render structure*/
836 frameStr.pBuffer = (M4OSA_Void*)yuvPlane[0].pac_data;
837 }
838
839 frameStr.timeMs = timeMs; /* timestamp on storyboard*/
840 frameStr.uiSurfaceWidth =
841 pContext->pEditSettings->pClipList[iCurrentClipIndex]->\
842 ClipProperties.uiVideoWidth;
843 frameStr.uiSurfaceHeight =
844 pContext->pEditSettings->pClipList[iCurrentClipIndex]->\
845 ClipProperties.uiVideoHeight;
846 frameStr.uiFrameWidth =
847 pContext->pEditSettings->pClipList[iCurrentClipIndex]->\
848 ClipProperties.uiVideoWidth;
849 frameStr.uiFrameHeight =
850 pContext->pEditSettings->pClipList[iCurrentClipIndex]->\
851 ClipProperties.uiVideoHeight;
852 if (pContext->pEditSettings->nbEffects > 0) {
853 frameStr.bApplyEffect = M4OSA_TRUE;
854 } else {
855 frameStr.bApplyEffect = M4OSA_FALSE;
856 }
857 frameStr.clipBeginCutTime = iIncrementedDuration;
858 frameStr.clipEndCutTime =
859 iIncrementedDuration +
860 (pContext->pEditSettings->pClipList[iCurrentClipIndex]->uiEndCutTime -\
861 pContext->pEditSettings->pClipList[iCurrentClipIndex]->uiBeginCutTime);
862
863 pContext->mPreviewController->setPreviewFrameRenderingMode(
864 pContext->pEditSettings->\
865 pClipList[iCurrentClipIndex]->xVSS.MediaRendering,
866 pContext->pEditSettings->xVSS.outputVideoSize);
Dharmaray Kundargicd196d32011-01-16 15:47:16 -0800867 result = pContext->mPreviewController->renderPreviewFrame(previewSurface,
Dharmaray Kundargic16ccc12011-01-21 19:20:49 -0800868 &frameStr, &currEditInfo);
869
870 if (currEditInfo.overlaySettingsIndex != -1) {
871 char tmpOverlayFilename[100];
872 char *extPos = NULL;
873 jstring tmpOverlayString;
874 int tmpRenderingMode = 0;
875
876 strncpy (tmpOverlayFilename,
877 (const char*)pContext->pEditSettings->Effects[currEditInfo.overlaySettingsIndex].xVSS.pFramingFilePath, 99);
878
879 //Change the name to png file
880 extPos = strstr(tmpOverlayFilename, ".rgb");
881 if (extPos != NULL) {
882 *extPos = '\0';
883 } else {
884 LOGE("ERROR the overlay file is incorrect");
885 }
886
887 strcat(tmpOverlayFilename, ".png");
888
889 tmpRenderingMode = pContext->pEditSettings->pClipList[iCurrentClipIndex]->xVSS.MediaRendering;
890 tmpOverlayString = pEnv->NewStringUTF(tmpOverlayFilename);
891 pEnv->CallVoidMethod(pContext->engine,
892 pContext->previewFrameEditInfoId,
893 tmpOverlayString, tmpRenderingMode);
894
895 }
896
Dharmaray Kundargicd196d32011-01-16 15:47:16 -0800897 videoEditJava_checkAndThrowRuntimeException(&needToBeLoaded, pEnv,
898 (M4NO_ERROR != result), result);
899
900 if (pContext->pEditSettings->pClipList[iCurrentClipIndex]->FileType ==\
901 /*M4VIDEOEDITING_kFileType_JPG */ M4VIDEOEDITING_kFileType_ARGB8888) {
Shyam Pallapothuc0125dd2011-04-21 19:13:56 -0700902 free(frameStr.pBuffer);
Dharmaray Kundargicd196d32011-01-16 15:47:16 -0800903 } else {
Shyam Pallapothuc0125dd2011-04-21 19:13:56 -0700904 free(yuvPlane[0].pac_data);
905 free(yuvPlane);
Dharmaray Kundargicd196d32011-01-16 15:47:16 -0800906 }
907 return tnTimeMs;
908}
909
910static int videoEditor_renderMediaItemPreviewFrame(JNIEnv* pEnv,
911 jobject thiz,
912 jobject mSurface,
913 jstring filePath,
914 jint frameWidth,
915 jint frameHeight,
916 jint surfaceWidth,
917 jint surfaceHeight,
918 jlong fromMs)
919{
920 bool needToBeLoaded = true;
921 M4OSA_ERR result = M4NO_ERROR;
922 M4OSA_UInt32 timeMs = (M4OSA_UInt32)fromMs;
923 M4OSA_UInt32 framesizeYuv =0;
924 M4VIFI_UInt8 *pixelArray = M4OSA_NULL;
925 VideoEditor_renderPreviewFrameStr frameStr;
926 M4OSA_Context tnContext = M4OSA_NULL;
927 const char* pMessage = NULL;
928 M4VIFI_ImagePlane yuvPlane[3], rgbPlane;
929
930 ManualEditContext* pContext = M4OSA_NULL;
931 // Get the context.
932 pContext =
933 (ManualEditContext*)videoEditClasses_getContext(&needToBeLoaded,
934 pEnv, thiz);
935
936 // Make sure that the context was set.
937 videoEditJava_checkAndThrowIllegalStateException(&needToBeLoaded, pEnv,
938 (M4OSA_NULL == pContext),
939 "not initialized");
940
941 // Make sure that the context was set.
942 videoEditJava_checkAndThrowIllegalStateException(&needToBeLoaded, pEnv,
943 (M4OSA_NULL == pContext->mPreviewController),
944 "not initialized");
945
946 // Validate the mSurface parameter.
947 videoEditJava_checkAndThrowIllegalArgumentException(&needToBeLoaded, pEnv,
948 (NULL == mSurface),
949 "mSurface is null");
950 jclass surfaceClass = pEnv->FindClass("android/view/Surface");
951 videoEditJava_checkAndThrowIllegalStateException(&needToBeLoaded, pEnv,
952 (M4OSA_NULL == surfaceClass),
953 "not initialized");
954
955 jfieldID surface_native =
956 pEnv->GetFieldID(surfaceClass, ANDROID_VIEW_SURFACE_JNI_ID, "I");
957 videoEditJava_checkAndThrowIllegalStateException(&needToBeLoaded, pEnv,
958 (M4OSA_NULL == surface_native),
959 "not initialized");
960
961 Surface* const p = (Surface*)pEnv->GetIntField(mSurface, surface_native);
962 sp<Surface> previewSurface = sp<Surface>(p);
963
964
965 const char *pString = pEnv->GetStringUTFChars(filePath, NULL);
966 if (pString == M4OSA_NULL) {
967 if (pEnv != NULL) {
968 jniThrowException(pEnv, "java/lang/RuntimeException", "Input string null");
969 }
970 }
971 VIDEOEDIT_LOG_FUNCTION(ANDROID_LOG_INFO, "VIDEO_EDITOR",
972 "videoEditor_renderMediaItemPreviewFrame() timeMs=%d", timeMs);
973 /* get thumbnail*/
974 result = ThumbnailOpen(&tnContext,(const M4OSA_Char*)pString, M4OSA_TRUE);
975 if (result != M4NO_ERROR || tnContext == M4OSA_NULL) {
976 return timeMs;
977 }
978
979 framesizeYuv = ((frameWidth)*(frameHeight)*1.5);
980
Shyam Pallapothuc0125dd2011-04-21 19:13:56 -0700981 pixelArray = (M4VIFI_UInt8 *)M4OSA_32bitAlignedMalloc(framesizeYuv, M4VS,\
Dharmaray Kundargicd196d32011-01-16 15:47:16 -0800982 (M4OSA_Char*)"videoEditor pixelArray");
983 if (pixelArray == M4OSA_NULL) {
984 VIDEOEDIT_LOG_FUNCTION(ANDROID_LOG_INFO, "VIDEO_EDITOR",
985 "videoEditor_renderPreviewFrame() malloc error");
986 ThumbnailClose(tnContext);
987 pMessage = videoEditJava_getErrorName(M4ERR_ALLOC);
988 jniThrowException(pEnv, "java/lang/RuntimeException", pMessage);
989 return timeMs;
990 }
991
992 result = ThumbnailGetPixels16(tnContext, (M4OSA_Int16 *)pixelArray,
993 frameWidth,
994 frameHeight, &timeMs);
995 if (result != M4NO_ERROR) {
Shyam Pallapothuc0125dd2011-04-21 19:13:56 -0700996 free(pixelArray);
Dharmaray Kundargicd196d32011-01-16 15:47:16 -0800997 ThumbnailClose(tnContext);
998 return fromMs;
999 }
1000
1001#ifdef DUMPTOFILESYSTEM
1002 {
1003 M4OSA_Context fileContext;
1004 M4OSA_Char* fileName = (M4OSA_Char*)"/mnt/sdcard/FirstRGB565.rgb";
1005 M4OSA_fileWriteOpen(&fileContext, (M4OSA_Void*) fileName,\
1006 M4OSA_kFileWrite|M4OSA_kFileCreate);
1007 M4OSA_fileWriteData(fileContext, (M4OSA_MemAddr8) pixelArray,
1008 framesizeRgb);
1009 M4OSA_fileWriteClose(fileContext);
1010 }
1011#endif
1012
1013 yuvPlane[0].pac_data = (M4VIFI_UInt8*)pixelArray;
1014 yuvPlane[0].u_height = frameHeight;
1015 yuvPlane[0].u_width = frameWidth;
1016 yuvPlane[0].u_stride = yuvPlane[0].u_width;
1017 yuvPlane[0].u_topleft = 0;
1018
1019 yuvPlane[1].u_height = frameHeight/2;
1020 yuvPlane[1].u_width = frameWidth/2;
1021 yuvPlane[1].u_stride = yuvPlane[1].u_width;
1022 yuvPlane[1].u_topleft = 0;
1023 yuvPlane[1].pac_data = yuvPlane[0].pac_data
1024 + yuvPlane[0].u_width*yuvPlane[0].u_height;
1025
1026 yuvPlane[2].u_height = frameHeight/2;
1027 yuvPlane[2].u_width = frameWidth/2;
1028 yuvPlane[2].u_stride = yuvPlane[2].u_width;
1029 yuvPlane[2].u_topleft = 0;
1030 yuvPlane[2].pac_data = yuvPlane[0].pac_data
1031 + yuvPlane[0].u_width*yuvPlane[0].u_height + \
1032 (yuvPlane[0].u_width/2)*(yuvPlane[0].u_height/2);
1033#ifdef DUMPTOFILESYSTEM
1034 {
1035 M4OSA_Context fileContext;
1036 M4OSA_Char* fileName = (M4OSA_Char*)"/mnt/sdcard/ConvertedYuv.yuv";
1037 M4OSA_fileWriteOpen(&fileContext, (M4OSA_Void*) fileName,\
1038 M4OSA_kFileWrite|M4OSA_kFileCreate);
1039 M4OSA_fileWriteData(fileContext, (M4OSA_MemAddr8) yuvPlane[0].pac_data,
1040 framesizeYuv);
1041 M4OSA_fileWriteClose(fileContext);
1042 }
1043#endif
1044
1045 /* Fill up the render structure*/
1046 frameStr.pBuffer = (M4OSA_Void*)yuvPlane[0].pac_data;
1047 frameStr.timeMs = timeMs; /* timestamp on storyboard*/
1048 frameStr.uiSurfaceWidth = frameWidth;
1049 frameStr.uiSurfaceHeight = frameHeight;
1050 frameStr.uiFrameWidth = frameWidth;
1051 frameStr.uiFrameHeight = frameHeight;
1052 frameStr.bApplyEffect = M4OSA_FALSE;
1053 // clip begin cuttime and end cuttime set to 0
1054 // as its only required when effect needs to be applied while rendering
1055 frameStr.clipBeginCutTime = 0;
1056 frameStr.clipEndCutTime = 0;
1057
1058 /* pContext->mPreviewController->setPreviewFrameRenderingMode(M4xVSS_kBlackBorders,
1059 (M4VIDEOEDITING_VideoFrameSize)(M4VIDEOEDITING_kHD960+1));*/
1060 result
Dharmaray Kundargic16ccc12011-01-21 19:20:49 -08001061 = pContext->mPreviewController->renderPreviewFrame(previewSurface,&frameStr, NULL);
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08001062 videoEditJava_checkAndThrowRuntimeException(&needToBeLoaded, pEnv,
1063 (M4NO_ERROR != result), result);
1064
1065 /* free the pixelArray and yuvPlane[0].pac_data */
Shyam Pallapothuc0125dd2011-04-21 19:13:56 -07001066 free(yuvPlane[0].pac_data);
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08001067
1068 ThumbnailClose(tnContext);
1069
Santosh Madhava91ba3a02011-02-09 19:25:17 -08001070 if (pString != NULL) {
1071 pEnv->ReleaseStringUTFChars(filePath, pString);
1072 }
1073
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08001074 return timeMs;
1075}
1076
1077int videoEditor_generateAudioRawFile( JNIEnv* pEnv,
1078 jobject thiz,
1079 jstring infilePath,
1080 jstring pcmfilePath)
1081{
1082 M4OSA_ERR result = M4NO_ERROR;
1083 bool loaded = true;
1084 ManualEditContext* pContext = M4OSA_NULL;
1085
1086
1087
1088 const char *pInputFile = pEnv->GetStringUTFChars(infilePath, NULL);
1089 if (pInputFile == M4OSA_NULL) {
1090 if (pEnv != NULL) {
1091 jniThrowException(pEnv, "java/lang/RuntimeException", "Input string null");
1092 }
1093 }
1094
1095 const char *pStringOutPCMFilePath = pEnv->GetStringUTFChars(pcmfilePath, NULL);
1096 if (pStringOutPCMFilePath == M4OSA_NULL) {
1097 if (pEnv != NULL) {
1098 jniThrowException(pEnv, "java/lang/RuntimeException", "Input string null");
1099 }
1100 }
1101
1102 VIDEOEDIT_LOG_FUNCTION(ANDROID_LOG_INFO,
1103 "VIDEO_EDITOR", "videoEditor_generateAudioRawFile infilePath %s",
1104 pInputFile);
1105 VIDEOEDIT_LOG_FUNCTION(ANDROID_LOG_INFO,
1106 "VIDEO_EDITOR", "videoEditor_generateAudioRawFile pcmfilePath %s",
1107 pStringOutPCMFilePath);
1108 // Get the context.
1109 pContext = (ManualEditContext*)videoEditClasses_getContext(&loaded, pEnv, thiz);
1110
1111 result = videoEditor_generateAudio( pEnv, pContext, (M4OSA_Char*)pInputFile,
1112 (M4OSA_Char*)pStringOutPCMFilePath);
1113
Santosh Madhava91ba3a02011-02-09 19:25:17 -08001114 if (pInputFile != NULL) {
1115 pEnv->ReleaseStringUTFChars(infilePath, pInputFile);
1116 }
1117 if (pStringOutPCMFilePath != NULL) {
1118 pEnv->ReleaseStringUTFChars(pcmfilePath, pStringOutPCMFilePath);
1119 }
1120
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08001121 return result;
1122}
1123
1124M4OSA_ERR videoEditor_generateAudio(JNIEnv* pEnv,ManualEditContext* pContext,
1125 M4OSA_Char* infilePath,
1126 M4OSA_Char* pcmfilePath )
1127{
1128 bool needToBeLoaded = true;
1129 M4OSA_ERR result = M4NO_ERROR;
Dharmaray Kundargibeb9fc82011-02-02 03:10:29 -08001130 M4MCS_Context mcsContext = M4OSA_NULL;
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08001131 M4OSA_Char* pInputFile = M4OSA_NULL;
1132 M4OSA_Char* pOutputFile = M4OSA_NULL;
1133 M4OSA_Char* pTempPath = M4OSA_NULL;
1134 M4MCS_OutputParams* pOutputParams = M4OSA_NULL;
1135 M4MCS_EncodingParams* pEncodingParams = M4OSA_NULL;
1136 M4OSA_Int32 pInputFileType = 0;
1137 M4OSA_UInt8 threadProgress = 0;
1138 M4OSA_Char* pTemp3gpFilePath = M4OSA_NULL;
1139
1140 VIDEOEDIT_LOG_API(ANDROID_LOG_INFO, "VIDEO_EDITOR", "videoEditor_generateAudio()");
1141
1142 videoEditJava_checkAndThrowIllegalArgumentException(&needToBeLoaded, pEnv,
1143 (NULL == pContext),
1144 "ManualEditContext is null");
1145
1146 VIDEOEDIT_LOG_FUNCTION(ANDROID_LOG_INFO, "VIDEO_EDITOR", "M4MCS_init()");
1147
Shyam Pallapothuc0125dd2011-04-21 19:13:56 -07001148 pOutputParams = (M4MCS_OutputParams *)M4OSA_32bitAlignedMalloc(
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08001149 sizeof(M4MCS_OutputParams),0x00,
1150 (M4OSA_Char *)"M4MCS_OutputParams");
1151 videoEditJava_checkAndThrowIllegalStateException(&needToBeLoaded, pEnv,
1152 (M4OSA_NULL == pOutputParams),
1153 "not initialized");
Dharmaray Kundargibeb9fc82011-02-02 03:10:29 -08001154 if (needToBeLoaded == false) {
1155 return M4ERR_ALLOC;
1156 }
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08001157
Shyam Pallapothuc0125dd2011-04-21 19:13:56 -07001158 pEncodingParams = (M4MCS_EncodingParams *)M4OSA_32bitAlignedMalloc(
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08001159 sizeof(M4MCS_EncodingParams),0x00,
1160 (M4OSA_Char *)"M4MCS_EncodingParams");
1161 videoEditJava_checkAndThrowIllegalStateException(&needToBeLoaded, pEnv,
1162 (M4OSA_NULL == pEncodingParams),
1163 "not initialized");
Dharmaray Kundargibeb9fc82011-02-02 03:10:29 -08001164 if (needToBeLoaded == false) {
Shyam Pallapothuc0125dd2011-04-21 19:13:56 -07001165 free(pEncodingParams);
Dharmaray Kundargibeb9fc82011-02-02 03:10:29 -08001166 pEncodingParams = M4OSA_NULL;
1167 return M4ERR_ALLOC;
1168 }
1169
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08001170 // Initialize the MCS library.
1171 result = M4MCS_init(&mcsContext, pContext->initParams.pFileReadPtr,
1172 pContext->initParams.pFileWritePtr);
1173 videoEditJava_checkAndThrowRuntimeException(&needToBeLoaded, pEnv,\
1174 (M4NO_ERROR != result), result);
1175 videoEditJava_checkAndThrowIllegalStateException(&needToBeLoaded, pEnv,
1176 (M4OSA_NULL == mcsContext),
1177 "not initialized");
Dharmaray Kundargibeb9fc82011-02-02 03:10:29 -08001178 if(needToBeLoaded == false) {
Shyam Pallapothuc0125dd2011-04-21 19:13:56 -07001179 free(pOutputParams);
Dharmaray Kundargibeb9fc82011-02-02 03:10:29 -08001180 pOutputParams = M4OSA_NULL;
Shyam Pallapothuc0125dd2011-04-21 19:13:56 -07001181 free(pEncodingParams);
Dharmaray Kundargibeb9fc82011-02-02 03:10:29 -08001182 pEncodingParams = M4OSA_NULL;
1183 return result;
1184 }
1185
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08001186 // generate the path for temp 3gp output file
Shyam Pallapothuc0125dd2011-04-21 19:13:56 -07001187 pTemp3gpFilePath = (M4OSA_Char*) M4OSA_32bitAlignedMalloc (
Dheeraj Sharma7743fa62011-04-12 14:53:58 -07001188 (strlen((const char*)pContext->initParams.pTempPath)
1189 + strlen((const char*)TEMP_MCS_OUT_FILE_PATH)) + 1 /* for null termination */ , 0x0,
1190 (M4OSA_Char*)"Malloc for temp 3gp file");
1191 if (pTemp3gpFilePath != M4OSA_NULL)
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08001192 {
Shyam Pallapothu3feb41f2011-04-20 20:28:32 -07001193 memset((void *)pTemp3gpFilePath ,0,
Dheeraj Sharma7743fa62011-04-12 14:53:58 -07001194 strlen((const char*)pContext->initParams.pTempPath)
Shyam Pallapothu3feb41f2011-04-20 20:28:32 -07001195 + strlen((const char*)TEMP_MCS_OUT_FILE_PATH) + 1);
Dheeraj Sharma7743fa62011-04-12 14:53:58 -07001196 strncat((char *)pTemp3gpFilePath,
1197 (const char *)pContext->initParams.pTempPath ,
1198 (size_t) ((M4OSA_Char*)pContext->initParams.pTempPath));
1199 strncat((char *)pTemp3gpFilePath , (const char *)TEMP_MCS_OUT_FILE_PATH,
1200 (size_t)strlen ((const char*)TEMP_MCS_OUT_FILE_PATH));
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08001201 }
Dharmaray Kundargibeb9fc82011-02-02 03:10:29 -08001202 else {
1203 M4MCS_abort(mcsContext);
Shyam Pallapothuc0125dd2011-04-21 19:13:56 -07001204 free(pOutputParams);
Dharmaray Kundargibeb9fc82011-02-02 03:10:29 -08001205 pOutputParams = M4OSA_NULL;
Shyam Pallapothuc0125dd2011-04-21 19:13:56 -07001206 free(pEncodingParams);
Dharmaray Kundargibeb9fc82011-02-02 03:10:29 -08001207 pEncodingParams = M4OSA_NULL;
1208 return M4ERR_ALLOC;
1209 }
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08001210
1211 pInputFile = (M4OSA_Char *) infilePath; //pContext->mAudioSettings->pFile;
1212 //Delete this file later
1213 pOutputFile = (M4OSA_Char *) pTemp3gpFilePath;
1214 // Temp folder path for VSS use = ProjectPath
1215 pTempPath = (M4OSA_Char *) pContext->initParams.pTempPath;
1216 pInputFileType = (M4VIDEOEDITING_FileType)pContext->mAudioSettings->fileType;
1217
1218 VIDEOEDIT_LOG_API(ANDROID_LOG_INFO, "VIDEO_EDITOR", "TEMP_MCS_OUT_FILE_PATH len %d",
Dheeraj Sharma7743fa62011-04-12 14:53:58 -07001219 strlen ((const char*)TEMP_MCS_OUT_FILE_PATH));
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08001220 VIDEOEDIT_LOG_API(ANDROID_LOG_INFO, "VIDEO_EDITOR", "pTemp3gpFilePath %s",
1221 pOutputFile);
1222
1223 VIDEOEDIT_LOG_API(ANDROID_LOG_INFO, "VIDEO_EDITOR", "M4MCS_open()");
1224
1225 result = M4MCS_open(mcsContext, pInputFile,
1226 (M4VIDEOEDITING_FileType)pInputFileType,
1227 pOutputFile, pTempPath);
1228 videoEditJava_checkAndThrowRuntimeException(&needToBeLoaded, pEnv,
1229 (M4NO_ERROR != result), result);
Dharmaray Kundargibeb9fc82011-02-02 03:10:29 -08001230 if(needToBeLoaded == false) {
Shyam Pallapothuc0125dd2011-04-21 19:13:56 -07001231 free(pTemp3gpFilePath);
Dharmaray Kundargibeb9fc82011-02-02 03:10:29 -08001232 pTemp3gpFilePath = M4OSA_NULL;
1233 M4MCS_abort(mcsContext);
Shyam Pallapothuc0125dd2011-04-21 19:13:56 -07001234 free(pOutputParams);
Dharmaray Kundargibeb9fc82011-02-02 03:10:29 -08001235 pOutputParams = M4OSA_NULL;
Shyam Pallapothuc0125dd2011-04-21 19:13:56 -07001236 free(pEncodingParams);
Dharmaray Kundargibeb9fc82011-02-02 03:10:29 -08001237 pEncodingParams = M4OSA_NULL;
1238 return result;
1239 }
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08001240
1241 pOutputParams->OutputFileType
1242 = (M4VIDEOEDITING_FileType)M4VIDEOEDITING_kFileType_3GPP;
1243 // Set the video format.
1244 pOutputParams->OutputVideoFormat =
1245 (M4VIDEOEDITING_VideoFormat)M4VIDEOEDITING_kNoneVideo;//M4VIDEOEDITING_kNoneVideo;
1246 // Set the frame size.
1247 pOutputParams->OutputVideoFrameSize
1248 = (M4VIDEOEDITING_VideoFrameSize)M4VIDEOEDITING_kQCIF;
1249 // Set the frame rate.
1250 pOutputParams->OutputVideoFrameRate
1251 = (M4VIDEOEDITING_VideoFramerate)M4VIDEOEDITING_k5_FPS;
1252
1253 // Set the audio format.
1254 pOutputParams->OutputAudioFormat
1255 = (M4VIDEOEDITING_AudioFormat)M4VIDEOEDITING_kAAC;
1256 // Set the audio sampling frequency.
1257 pOutputParams->OutputAudioSamplingFrequency =
1258 (M4VIDEOEDITING_AudioSamplingFrequency)M4VIDEOEDITING_k32000_ASF;
1259 // Set the audio mono.
1260 pOutputParams->bAudioMono = false;
1261 // Set the pcm file; null for now.
1262 pOutputParams->pOutputPCMfile = (M4OSA_Char *)pcmfilePath;
1263 //(M4OSA_Char *)"/sdcard/Output/AudioPcm.pcm";
1264 // Set the audio sampling frequency.
1265 pOutputParams->MediaRendering = (M4MCS_MediaRendering)M4MCS_kCropping;
1266 // new params after integrating MCS 2.0
1267 // Set the number of audio effects; 0 for now.
1268 pOutputParams->nbEffects = 0;
1269 // Set the audio effect; null for now.
1270 pOutputParams->pEffects = NULL;
1271 // Set the audio effect; null for now.
1272 pOutputParams->bDiscardExif = M4OSA_FALSE;
1273 // Set the audio effect; null for now.
1274 pOutputParams->bAdjustOrientation = M4OSA_FALSE;
1275
1276 VIDEOEDIT_LOG_FUNCTION(ANDROID_LOG_INFO, "VIDEO_EDITOR", "M4MCS_setOutputParams()");
1277 result = M4MCS_setOutputParams(mcsContext, pOutputParams);
1278 videoEditJava_checkAndThrowRuntimeException(&needToBeLoaded, pEnv,
1279 (M4NO_ERROR != result), result);
Dharmaray Kundargibeb9fc82011-02-02 03:10:29 -08001280 if (needToBeLoaded == false) {
Shyam Pallapothuc0125dd2011-04-21 19:13:56 -07001281 free(pTemp3gpFilePath);
Dharmaray Kundargibeb9fc82011-02-02 03:10:29 -08001282 pTemp3gpFilePath = M4OSA_NULL;
1283 M4MCS_abort(mcsContext);
Shyam Pallapothuc0125dd2011-04-21 19:13:56 -07001284 free(pOutputParams);
Dharmaray Kundargibeb9fc82011-02-02 03:10:29 -08001285 pOutputParams = M4OSA_NULL;
Shyam Pallapothuc0125dd2011-04-21 19:13:56 -07001286 free(pEncodingParams);
Dharmaray Kundargibeb9fc82011-02-02 03:10:29 -08001287 pEncodingParams = M4OSA_NULL;
1288 return result;
1289 }
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08001290 // Set the video bitrate.
1291 pEncodingParams->OutputVideoBitrate =
1292 (M4VIDEOEDITING_Bitrate)M4VIDEOEDITING_kUndefinedBitrate;
1293 // Set the audio bitrate.
1294 pEncodingParams->OutputAudioBitrate
1295 = (M4VIDEOEDITING_Bitrate)M4VIDEOEDITING_k128_KBPS;
1296 // Set the end cut time in milliseconds.
1297 pEncodingParams->BeginCutTime = 0;
1298 // Set the end cut time in milliseconds.
1299 pEncodingParams->EndCutTime = 0;
1300 // Set the output file size in bytes.
1301 pEncodingParams->OutputFileSize = 0;
1302 // Set video time scale.
1303 pEncodingParams->OutputVideoTimescale = 0;
1304
1305 VIDEOEDIT_LOG_FUNCTION(ANDROID_LOG_INFO, "VIDEO_EDITOR",
1306 "M4MCS_setEncodingParams()");
1307 result = M4MCS_setEncodingParams(mcsContext, pEncodingParams);
1308 videoEditJava_checkAndThrowRuntimeException(&needToBeLoaded, pEnv,
1309 (M4NO_ERROR != result), result);
Dharmaray Kundargibeb9fc82011-02-02 03:10:29 -08001310 if (needToBeLoaded == false) {
Shyam Pallapothuc0125dd2011-04-21 19:13:56 -07001311 free(pTemp3gpFilePath);
Dharmaray Kundargibeb9fc82011-02-02 03:10:29 -08001312 pTemp3gpFilePath = M4OSA_NULL;
1313 M4MCS_abort(mcsContext);
Shyam Pallapothuc0125dd2011-04-21 19:13:56 -07001314 free(pOutputParams);
Dharmaray Kundargibeb9fc82011-02-02 03:10:29 -08001315 pOutputParams = M4OSA_NULL;
Shyam Pallapothuc0125dd2011-04-21 19:13:56 -07001316 free(pEncodingParams);
Dharmaray Kundargibeb9fc82011-02-02 03:10:29 -08001317 pEncodingParams = M4OSA_NULL;
1318 return result;
1319 }
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08001320
1321 VIDEOEDIT_LOG_FUNCTION(ANDROID_LOG_INFO, "VIDEO_EDITOR",
1322 "M4MCS_checkParamsAndStart()");
1323 result = M4MCS_checkParamsAndStart(mcsContext);
1324 videoEditJava_checkAndThrowRuntimeException(&needToBeLoaded, pEnv,
1325 (M4NO_ERROR != result), result);
Dharmaray Kundargibeb9fc82011-02-02 03:10:29 -08001326 if (needToBeLoaded == false) {
Shyam Pallapothuc0125dd2011-04-21 19:13:56 -07001327 free(pTemp3gpFilePath);
Dharmaray Kundargibeb9fc82011-02-02 03:10:29 -08001328 pTemp3gpFilePath = M4OSA_NULL;
1329 M4MCS_abort(mcsContext);
Shyam Pallapothuc0125dd2011-04-21 19:13:56 -07001330 free(pOutputParams);
Dharmaray Kundargibeb9fc82011-02-02 03:10:29 -08001331 pOutputParams = M4OSA_NULL;
Shyam Pallapothuc0125dd2011-04-21 19:13:56 -07001332 free(pEncodingParams);
Dharmaray Kundargibeb9fc82011-02-02 03:10:29 -08001333 pEncodingParams = M4OSA_NULL;
1334 return result;
1335 }
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08001336
1337 VIDEOEDIT_LOG_FUNCTION(ANDROID_LOG_INFO, "VIDEO_EDITOR", "M4MCS_step()");
1338
1339 /*+ PROGRESS CB */
1340 M4OSA_UInt8 curProgress = 0;
1341 int lastProgress = 0;
1342
1343 LOGV("LVME_generateAudio Current progress is =%d", curProgress);
1344 pEnv->CallVoidMethod(pContext->engine,
1345 pContext->onProgressUpdateMethodId, 1/*task status*/,
1346 curProgress/*progress*/);
1347 do {
1348 result = M4MCS_step(mcsContext, &curProgress);
1349
1350 if (result != M4NO_ERROR) {
1351 LOGV("LVME_generateAudio M4MCS_step returned 0x%x",result);
1352
1353 if (result == M4MCS_WAR_TRANSCODING_DONE) {
1354 LOGV("LVME_generateAudio MCS process ended");
1355
1356 // Send a progress notification.
1357 curProgress = 100;
1358 pEnv->CallVoidMethod(pContext->engine,
1359 pContext->onProgressUpdateMethodId, 1/*task status*/,
1360 curProgress);
1361 LOGV("LVME_generateAudio Current progress is =%d", curProgress);
1362 }
1363 } else {
1364 // Send a progress notification if needed
1365 if (curProgress != lastProgress) {
1366 lastProgress = curProgress;
1367 pEnv->CallVoidMethod(pContext->engine,
1368 pContext->onProgressUpdateMethodId, 0/*task status*/,
1369 curProgress/*progress*/);
1370 LOGV("LVME_generateAudio Current progress is =%d",curProgress);
1371 }
1372 }
1373 } while (result == M4NO_ERROR);
1374 /*- PROGRESS CB */
1375
1376 videoEditJava_checkAndThrowRuntimeException(&needToBeLoaded, pEnv,
1377 (M4MCS_WAR_TRANSCODING_DONE != result), result);
Dharmaray Kundargibeb9fc82011-02-02 03:10:29 -08001378 if (needToBeLoaded == false) {
Shyam Pallapothuc0125dd2011-04-21 19:13:56 -07001379 free(pTemp3gpFilePath);
Dharmaray Kundargibeb9fc82011-02-02 03:10:29 -08001380 pTemp3gpFilePath = M4OSA_NULL;
1381 M4MCS_abort(mcsContext);
Shyam Pallapothuc0125dd2011-04-21 19:13:56 -07001382 free(pOutputParams);
Dharmaray Kundargibeb9fc82011-02-02 03:10:29 -08001383 pOutputParams = M4OSA_NULL;
Shyam Pallapothuc0125dd2011-04-21 19:13:56 -07001384 free(pEncodingParams);
Dharmaray Kundargibeb9fc82011-02-02 03:10:29 -08001385 pEncodingParams = M4OSA_NULL;
1386 return result;
1387 }
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08001388
1389 VIDEOEDIT_LOG_FUNCTION(ANDROID_LOG_INFO, "VIDEO_EDITOR", "M4MCS_abort()");
1390 result = M4MCS_abort(mcsContext);
1391 videoEditJava_checkAndThrowRuntimeException(&needToBeLoaded, pEnv,
1392 (M4NO_ERROR != result), result);
1393
1394 //pContext->mAudioSettings->pFile = pOutputParams->pOutputPCMfile;
Dheeraj Sharma7743fa62011-04-12 14:53:58 -07001395 remove((const char *) pTemp3gpFilePath);
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08001396 VIDEOEDIT_LOG_FUNCTION(ANDROID_LOG_INFO, "VIDEO_EDITOR", "videoEditor_generateAudio() EXIT ");
1397
Dharmaray Kundargibeb9fc82011-02-02 03:10:29 -08001398 if (pTemp3gpFilePath != M4OSA_NULL) {
Shyam Pallapothuc0125dd2011-04-21 19:13:56 -07001399 free(pTemp3gpFilePath);
Dharmaray Kundargibeb9fc82011-02-02 03:10:29 -08001400 }
1401 if (pOutputParams != M4OSA_NULL) {
Shyam Pallapothuc0125dd2011-04-21 19:13:56 -07001402 free(pOutputParams);
Dharmaray Kundargibeb9fc82011-02-02 03:10:29 -08001403 }
1404 if(pEncodingParams != M4OSA_NULL) {
Shyam Pallapothuc0125dd2011-04-21 19:13:56 -07001405 free(pEncodingParams);
Dharmaray Kundargibeb9fc82011-02-02 03:10:29 -08001406 }
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08001407 return result;
1408}
1409
1410static int removeAlphafromRGB8888 (
1411 M4OSA_Char* pFramingFilePath,
1412 M4xVSS_FramingStruct *pFramingCtx)
1413{
1414 M4OSA_UInt32 frameSize_argb = (pFramingCtx->width * pFramingCtx->height * 4); // aRGB data
1415 M4OSA_Context lImageFileFp = M4OSA_NULL;
1416 M4OSA_ERR err = M4NO_ERROR;
1417
1418 LOGV("removeAlphafromRGB8888: width %d", pFramingCtx->width);
1419
Shyam Pallapothuc0125dd2011-04-21 19:13:56 -07001420 M4OSA_UInt8 *pTmpData = (M4OSA_UInt8*) M4OSA_32bitAlignedMalloc(frameSize_argb, M4VS, (M4OSA_Char*)"Image argb data");
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08001421 if (pTmpData == M4OSA_NULL) {
1422 LOGE("Failed to allocate memory for Image clip");
1423 return M4ERR_ALLOC;
1424 }
1425
1426 /** Read the argb data from the passed file. */
1427 M4OSA_ERR lerr = M4OSA_fileReadOpen(&lImageFileFp, (M4OSA_Void *) pFramingFilePath, M4OSA_kFileRead);
1428
1429
1430 if ((lerr != M4NO_ERROR) || (lImageFileFp == M4OSA_NULL))
1431 {
1432 LOGE("removeAlphafromRGB8888: Can not open the file ");
Shyam Pallapothuc0125dd2011-04-21 19:13:56 -07001433 free(pTmpData);
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08001434 return M4ERR_FILE_NOT_FOUND;
1435 }
1436
1437
1438 lerr = M4OSA_fileReadData(lImageFileFp, (M4OSA_MemAddr8)pTmpData, &frameSize_argb);
1439 if (lerr != M4NO_ERROR)
1440 {
1441 LOGE("removeAlphafromRGB8888: can not read the data ");
1442 M4OSA_fileReadClose(lImageFileFp);
Shyam Pallapothuc0125dd2011-04-21 19:13:56 -07001443 free(pTmpData);
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08001444 return lerr;
1445 }
1446 M4OSA_fileReadClose(lImageFileFp);
1447
1448 M4OSA_UInt32 frameSize = (pFramingCtx->width * pFramingCtx->height * 3); //Size of RGB 888 data.
1449
Shyam Pallapothuc0125dd2011-04-21 19:13:56 -07001450 pFramingCtx->FramingRgb = (M4VIFI_ImagePlane*)M4OSA_32bitAlignedMalloc(
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08001451 sizeof(M4VIFI_ImagePlane), M4VS, (M4OSA_Char*)"Image clip RGB888 data");
Shyam Pallapothuc0125dd2011-04-21 19:13:56 -07001452 pFramingCtx->FramingRgb->pac_data = (M4VIFI_UInt8*)M4OSA_32bitAlignedMalloc(
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08001453 frameSize, M4VS, (M4OSA_Char*)"Image clip RGB888 data");
1454
1455 if (pFramingCtx->FramingRgb == M4OSA_NULL)
1456 {
1457 LOGE("Failed to allocate memory for Image clip");
Shyam Pallapothuc0125dd2011-04-21 19:13:56 -07001458 free(pTmpData);
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08001459 return M4ERR_ALLOC;
1460 }
1461
1462 /** Remove the alpha channel */
James Dong581cad22011-01-19 11:35:06 -08001463 for (size_t i = 0, j = 0; i < frameSize_argb; i++) {
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08001464 if ((i % 4) == 0) continue;
1465 pFramingCtx->FramingRgb->pac_data[j] = pTmpData[i];
1466 j++;
1467 }
Shyam Pallapothuc0125dd2011-04-21 19:13:56 -07001468 free(pTmpData);
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08001469 return M4NO_ERROR;
1470}
1471
1472static void
1473videoEditor_populateSettings(
1474 JNIEnv* pEnv,
1475 jobject thiz,
1476 jobject settings,
1477 jobject object,
1478 jobject audioSettingObject)
1479{
1480 VIDEOEDIT_LOG_FUNCTION(ANDROID_LOG_INFO, "VIDEO_EDITOR",
1481 "videoEditor_populateSettings()");
1482
1483 bool needToBeLoaded = true;
1484 ManualEditContext* pContext = M4OSA_NULL;
1485 M4OSA_ERR result = M4NO_ERROR;
Santosh Madhava91ba3a02011-02-09 19:25:17 -08001486 jstring strPath = M4OSA_NULL;
1487 jstring strPCMPath = M4OSA_NULL;
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08001488 jobjectArray propertiesClipsArray = M4OSA_NULL;
1489 jobject properties = M4OSA_NULL;
1490 jint* bitmapArray = M4OSA_NULL;
1491 jobjectArray effectSettingsArray = M4OSA_NULL;
1492 jobject effectSettings = M4OSA_NULL;
1493 jintArray pixelArray = M4OSA_NULL;
1494 int width = 0;
1495 int height = 0;
1496 int nbOverlays = 0;
1497 int i,j = 0;
1498 int *pOverlayIndex = M4OSA_NULL;
Santosh Madhava9ee9aad2011-02-10 20:18:18 -08001499 M4OSA_Char* pTempChar = M4OSA_NULL;
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08001500
1501 // Add a code marker (the condition must always be true).
1502 ADD_CODE_MARKER_FUN(NULL != pEnv)
1503
1504 // Validate the settings parameter.
1505 videoEditJava_checkAndThrowIllegalArgumentException(&needToBeLoaded, pEnv,
1506 (NULL == settings),
1507 "settings is null");
1508 // Get the context.
1509 pContext =
1510 (ManualEditContext*)videoEditClasses_getContext(&needToBeLoaded, pEnv, thiz);
1511
1512 // Make sure that the context was set.
1513 videoEditJava_checkAndThrowIllegalStateException(&needToBeLoaded, pEnv,
1514 (M4OSA_NULL == pContext),
1515 "not initialized");
1516 // Make sure that the context was set.
1517 videoEditJava_checkAndThrowIllegalStateException(&needToBeLoaded, pEnv,
1518 (M4OSA_NULL == pContext->mPreviewController),
1519 "not initialized");
1520 jclass mPreviewClipPropClazz = pEnv->FindClass(PREVIEW_PROPERTIES_CLASS_NAME);
1521 videoEditJava_checkAndThrowIllegalStateException(&needToBeLoaded, pEnv,
1522 (M4OSA_NULL == mPreviewClipPropClazz),
1523 "not initialized");
1524
1525 jfieldID fid = pEnv->GetFieldID(mPreviewClipPropClazz,"clipProperties",
1526 "[L"PROPERTIES_CLASS_NAME";" );
1527 videoEditJava_checkAndThrowIllegalStateException(&needToBeLoaded, pEnv,
1528 (M4OSA_NULL == fid),
1529 "not initialized");
1530
1531 propertiesClipsArray = (jobjectArray)pEnv->GetObjectField(object, fid);
1532 videoEditJava_checkAndThrowIllegalStateException(&needToBeLoaded, pEnv,
1533 (M4OSA_NULL == propertiesClipsArray),
1534 "not initialized");
1535
1536 jclass engineClass = pEnv->FindClass(MANUAL_EDIT_ENGINE_CLASS_NAME);
1537 videoEditJava_checkAndThrowIllegalStateException(&needToBeLoaded, pEnv,
1538 (M4OSA_NULL == engineClass),
1539 "not initialized");
1540
1541 pContext->onPreviewProgressUpdateMethodId = pEnv->GetMethodID(engineClass,
Dharmaray Kundargic16ccc12011-01-21 19:20:49 -08001542 "onPreviewProgressUpdate", "(IZZLjava/lang/String;I)V");
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08001543 // Check if the context is valid (required because the context is dereferenced).
1544 if (needToBeLoaded) {
1545 // Make sure that we are in a correct state.
1546 videoEditJava_checkAndThrowIllegalStateException(&needToBeLoaded, pEnv,
1547 (pContext->state != ManualEditState_INITIALIZED),
1548 "settings already loaded");
Dharmaray Kundargibeb9fc82011-02-02 03:10:29 -08001549 if (needToBeLoaded) {
1550 // Retrieve the edit settings.
1551 if (pContext->pEditSettings != M4OSA_NULL) {
1552 videoEditClasses_freeEditSettings(&pContext->pEditSettings);
1553 pContext->pEditSettings = M4OSA_NULL;
1554 }
1555 videoEditClasses_getEditSettings(&needToBeLoaded, pEnv,
1556 settings, &pContext->pEditSettings,false);
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08001557 }
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08001558 }
Dharmaray Kundargibeb9fc82011-02-02 03:10:29 -08001559
1560 if (needToBeLoaded == false) {
1561 j = 0;
1562 while (j < pContext->pEditSettings->nbEffects)
1563 {
1564 if (pContext->pEditSettings->Effects[j].xVSS.pFramingFilePath != M4OSA_NULL) {
1565 if (pContext->pEditSettings->Effects[j].xVSS.pFramingBuffer != M4OSA_NULL) {
Shyam Pallapothuc0125dd2011-04-21 19:13:56 -07001566 free(pContext->pEditSettings->\
Dharmaray Kundargibeb9fc82011-02-02 03:10:29 -08001567 Effects[j].xVSS.pFramingBuffer);
1568 pContext->pEditSettings->Effects[j].xVSS.pFramingBuffer = M4OSA_NULL;
1569 }
1570 }
1571 j++;
1572 }
1573 return;
1574 }
1575
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08001576 M4OSA_TRACE1_0("videoEditorC_getEditSettings done");
1577
Dharmaray Kundargic16ccc12011-01-21 19:20:49 -08001578 pContext->previewFrameEditInfoId = pEnv->GetMethodID(engineClass,
1579 "previewFrameEditInfo", "(Ljava/lang/String;I)V");
1580
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08001581 if ( pContext->pEditSettings != NULL )
1582 {
1583 // Check if the edit settings could be retrieved.
1584 jclass mEditClazz = pEnv->FindClass(EDIT_SETTINGS_CLASS_NAME);
1585 if(mEditClazz == M4OSA_NULL)
1586 {
1587 M4OSA_TRACE1_0("cannot find object field for mEditClazz");
Dharmaray Kundargibeb9fc82011-02-02 03:10:29 -08001588 goto videoEditor_populateSettings_cleanup;
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08001589 }
1590 jclass mEffectsClazz = pEnv->FindClass(EFFECT_SETTINGS_CLASS_NAME);
1591 if(mEffectsClazz == M4OSA_NULL)
1592 {
1593 M4OSA_TRACE1_0("cannot find object field for mEffectsClazz");
Dharmaray Kundargibeb9fc82011-02-02 03:10:29 -08001594 goto videoEditor_populateSettings_cleanup;
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08001595 }
1596 fid = pEnv->GetFieldID(mEditClazz,"effectSettingsArray", "[L"EFFECT_SETTINGS_CLASS_NAME";" );
1597 if(fid == M4OSA_NULL)
1598 {
1599 M4OSA_TRACE1_0("cannot find field for effectSettingsArray Array");
Dharmaray Kundargibeb9fc82011-02-02 03:10:29 -08001600 goto videoEditor_populateSettings_cleanup;
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08001601 }
1602 effectSettingsArray = (jobjectArray)pEnv->GetObjectField(settings, fid);
1603 if(effectSettingsArray == M4OSA_NULL)
1604 {
1605 M4OSA_TRACE1_0("cannot find object field for effectSettingsArray");
Dharmaray Kundargibeb9fc82011-02-02 03:10:29 -08001606 goto videoEditor_populateSettings_cleanup;
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08001607 }
Dharmaray Kundargibeb9fc82011-02-02 03:10:29 -08001608
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08001609 //int overlayIndex[pContext->pEditSettings->nbEffects];
Dharmaray Kundargibeb9fc82011-02-02 03:10:29 -08001610 if (pContext->pEditSettings->nbEffects > 0)
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08001611 {
1612 pOverlayIndex
Shyam Pallapothuc0125dd2011-04-21 19:13:56 -07001613 = (int*) M4OSA_32bitAlignedMalloc(pContext->pEditSettings->nbEffects * sizeof(int), 0,
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08001614 (M4OSA_Char*)"pOverlayIndex");
Dharmaray Kundargibeb9fc82011-02-02 03:10:29 -08001615 if (pOverlayIndex == M4OSA_NULL) {
1616 videoEditJava_checkAndThrowRuntimeException(&needToBeLoaded, pEnv,
1617 M4OSA_TRUE, M4ERR_ALLOC);
1618 goto videoEditor_populateSettings_cleanup;
1619 }
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08001620 }
1621
Dharmaray Kundargibeb9fc82011-02-02 03:10:29 -08001622 i = 0;
1623 j = 0;
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08001624 M4OSA_TRACE1_1("no of effects = %d",pContext->pEditSettings->nbEffects);
1625 while (j < pContext->pEditSettings->nbEffects)
1626 {
1627 if (pContext->pEditSettings->Effects[j].xVSS.pFramingFilePath != M4OSA_NULL)
1628 {
1629 pOverlayIndex[nbOverlays] = j;
Dharmaray Kundargibeb9fc82011-02-02 03:10:29 -08001630
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08001631 M4xVSS_FramingStruct *aFramingCtx = M4OSA_NULL;
1632 aFramingCtx
Shyam Pallapothuc0125dd2011-04-21 19:13:56 -07001633 = (M4xVSS_FramingStruct*)M4OSA_32bitAlignedMalloc(sizeof(M4xVSS_FramingStruct), M4VS,
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08001634 (M4OSA_Char*)"M4xVSS_internalDecodeGIF: Context of the framing effect");
1635 if (aFramingCtx == M4OSA_NULL)
1636 {
1637 M4OSA_TRACE1_0("Allocation error in videoEditor_populateSettings");
Dharmaray Kundargibeb9fc82011-02-02 03:10:29 -08001638 videoEditJava_checkAndThrowRuntimeException(&needToBeLoaded, pEnv,
1639 M4OSA_TRUE, M4ERR_ALLOC);
1640 goto videoEditor_populateSettings_cleanup;
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08001641 }
Dharmaray Kundargibeb9fc82011-02-02 03:10:29 -08001642
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08001643 aFramingCtx->pCurrent = M4OSA_NULL; /* Only used by the first element of the chain */
1644 aFramingCtx->previousClipTime = -1;
1645 aFramingCtx->FramingYuv = M4OSA_NULL;
1646 aFramingCtx->FramingRgb = M4OSA_NULL;
1647 aFramingCtx->topleft_x
1648 = pContext->pEditSettings->Effects[j].xVSS.topleft_x;
1649 aFramingCtx->topleft_y
1650 = pContext->pEditSettings->Effects[j].xVSS.topleft_y;
1651
1652
1653 VIDEOEDIT_LOG_FUNCTION(ANDROID_LOG_INFO, "VIDEO_EDITOR", "OF u_width %d",
1654 pContext->pEditSettings->Effects[j].xVSS.pFramingBuffer->u_width);
1655 VIDEOEDIT_LOG_FUNCTION(ANDROID_LOG_INFO, "VIDEO_EDITOR", "OF u_height() %d",
1656 pContext->pEditSettings->Effects[j].xVSS.pFramingBuffer->u_height);
1657 VIDEOEDIT_LOG_FUNCTION(ANDROID_LOG_INFO, "VIDEO_EDITOR", "OF rgbType() %d",
1658 pContext->pEditSettings->Effects[j].xVSS.rgbType);
1659
1660 aFramingCtx->width = pContext->pEditSettings->Effects[j].xVSS.pFramingBuffer->u_width;
1661 aFramingCtx->height = pContext->pEditSettings->Effects[j].xVSS.pFramingBuffer->u_height;
1662
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08001663 result = M4xVSS_internalConvertARGB888toYUV420_FrammingEffect(pContext->engineContext,
1664 &(pContext->pEditSettings->Effects[j]),aFramingCtx,
1665 pContext->pEditSettings->Effects[j].xVSS.framingScaledSize);
Dharmaray Kundargic08f85a2011-02-01 19:24:00 -08001666 videoEditJava_checkAndThrowRuntimeException(&needToBeLoaded, pEnv,
1667 (M4NO_ERROR != result), result);
1668 if (needToBeLoaded == false) {
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08001669 M4OSA_TRACE1_1("M4xVSS_internalConvertARGB888toYUV420_FrammingEffect returned 0x%x", result);
Dharmaray Kundargic08f85a2011-02-01 19:24:00 -08001670 if (aFramingCtx != M4OSA_NULL) {
Shyam Pallapothuc0125dd2011-04-21 19:13:56 -07001671 free(aFramingCtx);
Dharmaray Kundargic08f85a2011-02-01 19:24:00 -08001672 aFramingCtx = M4OSA_NULL;
1673 }
Dharmaray Kundargibeb9fc82011-02-02 03:10:29 -08001674 goto videoEditor_populateSettings_cleanup;
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08001675 }
1676
1677 //framing buffers are resized to fit the output video resolution.
1678 pContext->pEditSettings->Effects[j].xVSS.pFramingBuffer->u_width =
1679 aFramingCtx->FramingRgb->u_width;
1680 pContext->pEditSettings->Effects[j].xVSS.pFramingBuffer->u_height =
1681 aFramingCtx->FramingRgb->u_height;
1682
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08001683 VIDEOEDIT_LOG_FUNCTION(ANDROID_LOG_INFO, "VIDEO_EDITOR", "A framing Context aFramingCtx->width = %d",
1684 aFramingCtx->FramingRgb->u_width);
1685
1686 VIDEOEDIT_LOG_FUNCTION(ANDROID_LOG_INFO, "VIDEO_EDITOR", "A framing Context aFramingCtx->height = %d",
1687 aFramingCtx->FramingRgb->u_height);
1688
1689
1690 width = pContext->pEditSettings->Effects[j].xVSS.pFramingBuffer->u_width;
1691 height = pContext->pEditSettings->Effects[j].xVSS.pFramingBuffer->u_height;
1692
Dharmaray Kundargic16ccc12011-01-21 19:20:49 -08001693 //RGB 565
Dharmaray Kundargibeb9fc82011-02-02 03:10:29 -08001694 pContext->pEditSettings->Effects[j].xVSS.pFramingBuffer->u_stride = width * 2;
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08001695
Dharmaray Kundargic16ccc12011-01-21 19:20:49 -08001696 //for RGB565
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08001697 pContext->pEditSettings->Effects[j].xVSS.pFramingBuffer->u_topleft = 0;
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08001698 pContext->pEditSettings->Effects[j].xVSS.pFramingBuffer->pac_data =
Shyam Pallapothuc0125dd2011-04-21 19:13:56 -07001699 (M4VIFI_UInt8 *)M4OSA_32bitAlignedMalloc(width*height*2,
Dharmaray Kundargic16ccc12011-01-21 19:20:49 -08001700 0x00,(M4OSA_Char *)"pac_data buffer");
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08001701
Dharmaray Kundargibeb9fc82011-02-02 03:10:29 -08001702 if (pContext->pEditSettings->Effects[j].xVSS.pFramingBuffer->pac_data == M4OSA_NULL) {
1703 M4OSA_TRACE1_0("Failed to allocate memory for framing buffer");
1704 videoEditJava_checkAndThrowRuntimeException(&needToBeLoaded, pEnv,
1705 M4OSA_TRUE, M4ERR_ALLOC);
1706 goto videoEditor_populateSettings_cleanup;
1707 }
1708
Shyam Pallapothu3feb41f2011-04-20 20:28:32 -07001709 memcpy((void *)&pContext->pEditSettings->\
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08001710 Effects[j].xVSS.pFramingBuffer->\
Shyam Pallapothu3feb41f2011-04-20 20:28:32 -07001711 pac_data[0],(void *)&aFramingCtx->FramingRgb->pac_data[0],(width*height*2));
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08001712
Dharmaray Kundargic16ccc12011-01-21 19:20:49 -08001713 //As of now rgb type is 565
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08001714 pContext->pEditSettings->Effects[j].xVSS.rgbType =
Dharmaray Kundargic16ccc12011-01-21 19:20:49 -08001715 (M4VSS3GPP_RGBType) M4VSS3GPP_kRGB565;
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08001716
1717 if (aFramingCtx->FramingYuv != M4OSA_NULL )
1718 {
Santosh Madhava7c8f25f2011-02-27 20:27:20 -08001719 if (aFramingCtx->FramingYuv[0].pac_data != M4OSA_NULL) {
Shyam Pallapothuc0125dd2011-04-21 19:13:56 -07001720 free(aFramingCtx->FramingYuv[0].pac_data);
Santosh Madhava7c8f25f2011-02-27 20:27:20 -08001721 aFramingCtx->FramingYuv[0].pac_data = M4OSA_NULL;
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08001722 }
Santosh Madhava7c8f25f2011-02-27 20:27:20 -08001723 if (aFramingCtx->FramingYuv[1].pac_data != M4OSA_NULL) {
Shyam Pallapothuc0125dd2011-04-21 19:13:56 -07001724 free(aFramingCtx->FramingYuv[1].pac_data);
Santosh Madhava7c8f25f2011-02-27 20:27:20 -08001725 aFramingCtx->FramingYuv[1].pac_data = M4OSA_NULL;
1726 }
1727 if (aFramingCtx->FramingYuv[2].pac_data != M4OSA_NULL) {
Shyam Pallapothuc0125dd2011-04-21 19:13:56 -07001728 free(aFramingCtx->FramingYuv[2].pac_data);
Santosh Madhava7c8f25f2011-02-27 20:27:20 -08001729 aFramingCtx->FramingYuv[2].pac_data = M4OSA_NULL;
1730 }
1731
Shyam Pallapothuc0125dd2011-04-21 19:13:56 -07001732 free(aFramingCtx->FramingYuv);
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08001733 aFramingCtx->FramingYuv = M4OSA_NULL;
1734 }
1735 if (aFramingCtx->FramingRgb->pac_data != M4OSA_NULL) {
Shyam Pallapothuc0125dd2011-04-21 19:13:56 -07001736 free(aFramingCtx->FramingRgb->pac_data);
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08001737 aFramingCtx->FramingRgb->pac_data = M4OSA_NULL;
1738 }
1739 if (aFramingCtx->FramingRgb != M4OSA_NULL) {
Shyam Pallapothuc0125dd2011-04-21 19:13:56 -07001740 free(aFramingCtx->FramingRgb);
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08001741 aFramingCtx->FramingRgb = M4OSA_NULL;
1742 }
1743 if (aFramingCtx != M4OSA_NULL) {
Shyam Pallapothuc0125dd2011-04-21 19:13:56 -07001744 free(aFramingCtx);
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08001745 aFramingCtx = M4OSA_NULL;
1746 }
Dharmaray Kundargibeb9fc82011-02-02 03:10:29 -08001747 nbOverlays++;
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08001748 }
1749 j++;
1750 }
1751
1752 // Check if the edit settings could be retrieved.
1753 M4OSA_TRACE1_1("total clips are = %d",pContext->pEditSettings->uiClipNumber);
1754 for (i = 0; i < pContext->pEditSettings->uiClipNumber; i++) {
1755 M4OSA_TRACE1_1("clip no = %d",i);
1756 properties = pEnv->GetObjectArrayElement(propertiesClipsArray, i);
1757 videoEditJava_checkAndThrowIllegalStateException(&needToBeLoaded, pEnv,
1758 (M4OSA_NULL == properties),
1759 "not initialized");
Dharmaray Kundargibeb9fc82011-02-02 03:10:29 -08001760 if (needToBeLoaded) {
1761 getClipSetting(pEnv,properties, pContext->pEditSettings->pClipList[i]);
1762 } else {
1763 goto videoEditor_populateSettings_cleanup;
1764 }
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08001765 }
1766
1767 if (needToBeLoaded) {
1768 // Log the edit settings.
1769 VIDEOEDIT_LOG_EDIT_SETTINGS(pContext->pEditSettings);
1770 }
1771 }
Santosh Madhava71771b32011-02-28 18:30:53 -08001772 /* free previous allocations , if any */
1773 if (pContext->mAudioSettings != M4OSA_NULL) {
1774 if (pContext->mAudioSettings->pFile != NULL) {
Shyam Pallapothuc0125dd2011-04-21 19:13:56 -07001775 free(pContext->mAudioSettings->pFile);
Santosh Madhava71771b32011-02-28 18:30:53 -08001776 pContext->mAudioSettings->pFile = M4OSA_NULL;
1777 }
1778 if (pContext->mAudioSettings->pPCMFilePath != NULL) {
Shyam Pallapothuc0125dd2011-04-21 19:13:56 -07001779 free(pContext->mAudioSettings->pPCMFilePath);
Santosh Madhava71771b32011-02-28 18:30:53 -08001780 pContext->mAudioSettings->pPCMFilePath = M4OSA_NULL;
1781 }
1782 }
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08001783
1784 if (audioSettingObject != M4OSA_NULL) {
1785 jclass audioSettingClazz = pEnv->FindClass(AUDIO_SETTINGS_CLASS_NAME);
1786 videoEditJava_checkAndThrowIllegalStateException(&needToBeLoaded, pEnv,
1787 (M4OSA_NULL == audioSettingClazz),
1788 "not initialized");
1789
1790 videoEditJava_checkAndThrowIllegalStateException(&needToBeLoaded, pEnv,
1791 (M4OSA_NULL == pContext->mAudioSettings),
1792 "not initialized");
1793
Dharmaray Kundargibeb9fc82011-02-02 03:10:29 -08001794 if (needToBeLoaded == false) {
1795 goto videoEditor_populateSettings_cleanup;
1796 }
1797
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08001798 fid = pEnv->GetFieldID(audioSettingClazz,"bRemoveOriginal","Z");
1799 pContext->mAudioSettings->bRemoveOriginal = pEnv->GetIntField(audioSettingObject,fid);
1800 M4OSA_TRACE1_1("bRemoveOriginal = %d",pContext->mAudioSettings->bRemoveOriginal);
1801
1802 fid = pEnv->GetFieldID(audioSettingClazz,"channels","I");
1803 pContext->mAudioSettings->uiNbChannels = pEnv->GetIntField(audioSettingObject,fid);
1804 M4OSA_TRACE1_1("uiNbChannels = %d",pContext->mAudioSettings->uiNbChannels);
1805
1806 fid = pEnv->GetFieldID(audioSettingClazz,"Fs","I");
1807 pContext->mAudioSettings->uiSamplingFrequency = pEnv->GetIntField(audioSettingObject,fid);
1808 M4OSA_TRACE1_1("uiSamplingFrequency = %d",pContext->mAudioSettings->uiSamplingFrequency);
1809
1810 fid = pEnv->GetFieldID(audioSettingClazz,"ExtendedFs","I");
1811 pContext->mAudioSettings->uiExtendedSamplingFrequency =
1812 pEnv->GetIntField(audioSettingObject,fid);
1813 M4OSA_TRACE1_1("uiExtendedSamplingFrequency = %d",
1814 pContext->mAudioSettings->uiExtendedSamplingFrequency);
1815
1816 fid = pEnv->GetFieldID(audioSettingClazz,"startMs","J");
1817 pContext->mAudioSettings->uiAddCts
1818 = pEnv->GetIntField(audioSettingObject,fid);
1819 M4OSA_TRACE1_1("uiAddCts = %d",pContext->mAudioSettings->uiAddCts);
1820
1821 fid = pEnv->GetFieldID(audioSettingClazz,"volume","I");
1822 pContext->mAudioSettings->uiAddVolume
1823 = pEnv->GetIntField(audioSettingObject,fid);
1824 M4OSA_TRACE1_1("uiAddVolume = %d",pContext->mAudioSettings->uiAddVolume);
1825
1826 fid = pEnv->GetFieldID(audioSettingClazz,"loop","Z");
1827 pContext->mAudioSettings->bLoop
1828 = pEnv->GetIntField(audioSettingObject,fid);
1829 M4OSA_TRACE1_1("bLoop = %d",pContext->mAudioSettings->bLoop);
1830
1831 fid = pEnv->GetFieldID(audioSettingClazz,"beginCutTime","J");
1832 pContext->mAudioSettings->beginCutMs
1833 = pEnv->GetIntField(audioSettingObject,fid);
1834 M4OSA_TRACE1_1("begin cut time = %d",pContext->mAudioSettings->beginCutMs);
1835
1836 fid = pEnv->GetFieldID(audioSettingClazz,"endCutTime","J");
1837 pContext->mAudioSettings->endCutMs
1838 = pEnv->GetIntField(audioSettingObject,fid);
1839 M4OSA_TRACE1_1("end cut time = %d",pContext->mAudioSettings->endCutMs);
1840
1841 fid = pEnv->GetFieldID(audioSettingClazz,"fileType","I");
1842 pContext->mAudioSettings->fileType
1843 = pEnv->GetIntField(audioSettingObject,fid);
1844 M4OSA_TRACE1_1("fileType = %d",pContext->mAudioSettings->fileType);
Santosh Madhava9ee9aad2011-02-10 20:18:18 -08001845
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08001846 fid = pEnv->GetFieldID(audioSettingClazz,"pFile","Ljava/lang/String;");
Santosh Madhava91ba3a02011-02-09 19:25:17 -08001847 strPath = (jstring)pEnv->GetObjectField(audioSettingObject,fid);
Santosh Madhava9ee9aad2011-02-10 20:18:18 -08001848 pTempChar = (M4OSA_Char*)pEnv->GetStringUTFChars(strPath, M4OSA_NULL);
1849 if (pTempChar != NULL) {
Shyam Pallapothuc0125dd2011-04-21 19:13:56 -07001850 pContext->mAudioSettings->pFile = (M4OSA_Char*) M4OSA_32bitAlignedMalloc(
Santosh Madhava9ee9aad2011-02-10 20:18:18 -08001851 (M4OSA_UInt32)(strlen((const char*)pTempChar))+1 /* +1 for NULL termination */, 0,
1852 (M4OSA_Char*)"strPath allocation " );
1853 if (pContext->mAudioSettings->pFile != M4OSA_NULL) {
Shyam Pallapothu3feb41f2011-04-20 20:28:32 -07001854 memcpy((void *)pContext->mAudioSettings->pFile ,
1855 (void *)pTempChar , strlen((const char*)pTempChar));
Santosh Madhava9ee9aad2011-02-10 20:18:18 -08001856 ((M4OSA_Int8 *)(pContext->mAudioSettings->pFile))[strlen((const char*)pTempChar)] = '\0';
1857 pEnv->ReleaseStringUTFChars(strPath,(const char *)pTempChar);
1858 } else {
1859 pEnv->ReleaseStringUTFChars(strPath,(const char *)pTempChar);
1860 VIDEOEDIT_LOG_ERROR(ANDROID_LOG_INFO, "VIDEO_EDITOR",
1861 "regenerateAudio() Malloc failed for pContext->mAudioSettings->pFile ");
1862 videoEditJava_checkAndThrowRuntimeException(&needToBeLoaded, pEnv,
1863 M4OSA_TRUE, M4ERR_ALLOC);
1864 goto videoEditor_populateSettings_cleanup;
1865 }
1866 }
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08001867 M4OSA_TRACE1_1("file name = %s",pContext->mAudioSettings->pFile);
1868 VIDEOEDIT_LOG_API(ANDROID_LOG_INFO, "VIDEOEDITOR", "regenerateAudio() file name = %s",\
1869 pContext->mAudioSettings->pFile);
Dharmaray Kundargibeb9fc82011-02-02 03:10:29 -08001870
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08001871 fid = pEnv->GetFieldID(audioSettingClazz,"pcmFilePath","Ljava/lang/String;");
Santosh Madhava91ba3a02011-02-09 19:25:17 -08001872 strPCMPath = (jstring)pEnv->GetObjectField(audioSettingObject,fid);
Santosh Madhava9ee9aad2011-02-10 20:18:18 -08001873 pTempChar = (M4OSA_Char*)pEnv->GetStringUTFChars(strPCMPath, M4OSA_NULL);
1874 if (pTempChar != NULL) {
Shyam Pallapothuc0125dd2011-04-21 19:13:56 -07001875 pContext->mAudioSettings->pPCMFilePath = (M4OSA_Char*) M4OSA_32bitAlignedMalloc(
Santosh Madhava9ee9aad2011-02-10 20:18:18 -08001876 (M4OSA_UInt32)(strlen((const char*)pTempChar))+1 /* +1 for NULL termination */, 0,
1877 (M4OSA_Char*)"strPCMPath allocation " );
1878 if (pContext->mAudioSettings->pPCMFilePath != M4OSA_NULL) {
Shyam Pallapothu3feb41f2011-04-20 20:28:32 -07001879 memcpy((void *)pContext->mAudioSettings->pPCMFilePath ,
1880 (void *)pTempChar , strlen((const char*)pTempChar));
Santosh Madhava9ee9aad2011-02-10 20:18:18 -08001881 ((M4OSA_Int8 *)(pContext->mAudioSettings->pPCMFilePath))[strlen((const char*)pTempChar)] = '\0';
1882 pEnv->ReleaseStringUTFChars(strPCMPath,(const char *)pTempChar);
1883 } else {
1884 pEnv->ReleaseStringUTFChars(strPCMPath,(const char *)pTempChar);
1885 VIDEOEDIT_LOG_ERROR(ANDROID_LOG_INFO, "VIDEO_EDITOR",
1886 "regenerateAudio() Malloc failed for pContext->mAudioSettings->pPCMFilePath ");
1887 videoEditJava_checkAndThrowRuntimeException(&needToBeLoaded, pEnv,
1888 M4OSA_TRUE, M4ERR_ALLOC);
1889 goto videoEditor_populateSettings_cleanup;
1890 }
1891 }
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08001892 VIDEOEDIT_LOG_API(ANDROID_LOG_INFO, "VIDEOEDITOR", "pPCMFilePath -- %s ",\
1893 pContext->mAudioSettings->pPCMFilePath);
Dharmaray Kundargibeb9fc82011-02-02 03:10:29 -08001894
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08001895 fid = pEnv->GetFieldID(engineClass,"mRegenerateAudio","Z");
1896 bool regenerateAudio = pEnv->GetBooleanField(thiz,fid);
Dharmaray Kundargibeb9fc82011-02-02 03:10:29 -08001897
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08001898 VIDEOEDIT_LOG_API(ANDROID_LOG_INFO, "VIDEOEDITOR", "regenerateAudio -- %d ",\
1899 regenerateAudio);
Dharmaray Kundargibeb9fc82011-02-02 03:10:29 -08001900
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08001901 if (regenerateAudio) {
1902 M4OSA_TRACE1_0("Calling Generate Audio now");
1903 result = videoEditor_generateAudio(pEnv,
1904 pContext,
1905 (M4OSA_Char*)pContext->mAudioSettings->pFile,
1906 (M4OSA_Char*)pContext->mAudioSettings->pPCMFilePath);
Dharmaray Kundargibeb9fc82011-02-02 03:10:29 -08001907
1908 videoEditJava_checkAndThrowRuntimeException(&needToBeLoaded, pEnv,
1909 (M4NO_ERROR != result), result);
1910 if (needToBeLoaded == false) {
1911 goto videoEditor_populateSettings_cleanup;
1912 }
1913
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08001914 regenerateAudio = false;
1915 pEnv->SetBooleanField(thiz,fid,regenerateAudio);
1916 }
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08001917
1918 /* Audio mix and duck */
1919 fid = pEnv->GetFieldID(audioSettingClazz,"ducking_threshold","I");
1920 pContext->mAudioSettings->uiInDucking_threshold
1921 = pEnv->GetIntField(audioSettingObject,fid);
Dharmaray Kundargibeb9fc82011-02-02 03:10:29 -08001922
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08001923 M4OSA_TRACE1_1("ducking threshold = %d",
1924 pContext->mAudioSettings->uiInDucking_threshold);
1925
1926 fid = pEnv->GetFieldID(audioSettingClazz,"ducking_lowVolume","I");
1927 pContext->mAudioSettings->uiInDucking_lowVolume
1928 = pEnv->GetIntField(audioSettingObject,fid);
Dharmaray Kundargibeb9fc82011-02-02 03:10:29 -08001929
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08001930 M4OSA_TRACE1_1("ducking lowVolume = %d",
1931 pContext->mAudioSettings->uiInDucking_lowVolume);
1932
1933 fid = pEnv->GetFieldID(audioSettingClazz,"bInDucking_enable","Z");
1934 pContext->mAudioSettings->bInDucking_enable
1935 = pEnv->GetBooleanField(audioSettingObject,fid);
1936 M4OSA_TRACE1_1("ducking lowVolume = %d",
1937 pContext->mAudioSettings->bInDucking_enable);
Dharmaray Kundargibeb9fc82011-02-02 03:10:29 -08001938
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08001939 } else {
1940 if (pContext->mAudioSettings != M4OSA_NULL) {
1941 pContext->mAudioSettings->pFile = M4OSA_NULL;
Santosh Madhava9ee9aad2011-02-10 20:18:18 -08001942 pContext->mAudioSettings->pPCMFilePath = M4OSA_NULL;
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08001943 pContext->mAudioSettings->bRemoveOriginal = 0;
1944 pContext->mAudioSettings->uiNbChannels = 0;
1945 pContext->mAudioSettings->uiSamplingFrequency = 0;
1946 pContext->mAudioSettings->uiExtendedSamplingFrequency = 0;
1947 pContext->mAudioSettings->uiAddCts = 0;
1948 pContext->mAudioSettings->uiAddVolume = 0;
1949 pContext->mAudioSettings->beginCutMs = 0;
1950 pContext->mAudioSettings->endCutMs = 0;
Santosh Madhava9ee9aad2011-02-10 20:18:18 -08001951 pContext->mAudioSettings->fileType = 0;
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08001952 pContext->mAudioSettings->bLoop = 0;
1953 pContext->mAudioSettings->uiInDucking_lowVolume = 0;
1954 pContext->mAudioSettings->bInDucking_enable = 0;
1955 pContext->mAudioSettings->uiBTChannelCount = 0;
1956 pContext->mAudioSettings->uiInDucking_threshold = 0;
1957
1958 fid = pEnv->GetFieldID(engineClass,"mRegenerateAudio","Z");
1959 bool regenerateAudio = pEnv->GetBooleanField(thiz,fid);
Dharmaray Kundargibeb9fc82011-02-02 03:10:29 -08001960 if (!regenerateAudio) {
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08001961 regenerateAudio = true;
1962 pEnv->SetBooleanField(thiz,fid,regenerateAudio);
1963 }
1964 }
1965 }
Dharmaray Kundargibeb9fc82011-02-02 03:10:29 -08001966
1967 if (pContext->pEditSettings != NULL)
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08001968 {
1969 result = pContext->mPreviewController->loadEditSettings(pContext->pEditSettings,
Dharmaray Kundargibeb9fc82011-02-02 03:10:29 -08001970 pContext->mAudioSettings);
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08001971 videoEditJava_checkAndThrowRuntimeException(&needToBeLoaded, pEnv,
1972 (M4NO_ERROR != result), result);
1973
Dharmaray Kundargibeb9fc82011-02-02 03:10:29 -08001974 if (needToBeLoaded) {
1975 pContext->mPreviewController->setJniCallback((void*)pContext,
1976 (jni_progress_callback_fct)jniPreviewProgressCallback);
1977 }
1978 }
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08001979
Dharmaray Kundargibeb9fc82011-02-02 03:10:29 -08001980videoEditor_populateSettings_cleanup:
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08001981 j = 0;
1982 while (j < nbOverlays)
1983 {
1984 if (pContext->pEditSettings->Effects[pOverlayIndex[j]].xVSS.pFramingBuffer->pac_data != \
1985 M4OSA_NULL) {
Shyam Pallapothuc0125dd2011-04-21 19:13:56 -07001986 free(pContext->pEditSettings->\
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08001987 Effects[pOverlayIndex[j]].xVSS.pFramingBuffer->pac_data);
1988 pContext->pEditSettings->\
1989 Effects[pOverlayIndex[j]].xVSS.pFramingBuffer->pac_data = M4OSA_NULL;
1990 }
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08001991 j++;
1992 }
Dharmaray Kundargibeb9fc82011-02-02 03:10:29 -08001993
1994 j = 0;
1995 while (j < pContext->pEditSettings->nbEffects)
1996 {
1997 if (pContext->pEditSettings->Effects[j].xVSS.pFramingFilePath != M4OSA_NULL) {
1998 if (pContext->pEditSettings->Effects[j].xVSS.pFramingBuffer != M4OSA_NULL) {
Shyam Pallapothuc0125dd2011-04-21 19:13:56 -07001999 free(pContext->pEditSettings->\
Dharmaray Kundargibeb9fc82011-02-02 03:10:29 -08002000 Effects[j].xVSS.pFramingBuffer);
2001 pContext->pEditSettings->Effects[j].xVSS.pFramingBuffer = M4OSA_NULL;
2002 }
2003 }
2004 j++;
2005 }
2006
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08002007 if (pOverlayIndex != M4OSA_NULL)
2008 {
Shyam Pallapothuc0125dd2011-04-21 19:13:56 -07002009 free(pOverlayIndex);
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08002010 pOverlayIndex = M4OSA_NULL;
2011 }
2012 return;
2013}
2014
2015static void
2016videoEditor_startPreview(
2017 JNIEnv* pEnv,
2018 jobject thiz,
2019 jobject mSurface,
2020 jlong fromMs,
2021 jlong toMs,
2022 jint callbackInterval,
2023 jboolean loop)
2024{
2025 bool needToBeLoaded = true;
2026 M4OSA_ERR result = M4NO_ERROR;
2027 VIDEOEDIT_LOG_FUNCTION(ANDROID_LOG_INFO, "VIDEO_EDITOR", "videoEditor_startPreview()");
2028
2029 ManualEditContext* pContext = M4OSA_NULL;
2030 // Get the context.
2031 pContext = (ManualEditContext*)videoEditClasses_getContext(&needToBeLoaded, pEnv, thiz);
2032
2033 // Make sure that the context was set.
2034 videoEditJava_checkAndThrowIllegalStateException(&needToBeLoaded, pEnv,
2035 (M4OSA_NULL == pContext),
2036 "not initialized");
2037
2038 videoEditJava_checkAndThrowIllegalStateException(&needToBeLoaded, pEnv,
2039 (M4OSA_NULL == pContext->mAudioSettings),
2040 "not initialized");
2041 // Make sure that the context was set.
2042 videoEditJava_checkAndThrowIllegalStateException(&needToBeLoaded, pEnv,
2043 (M4OSA_NULL == pContext->mPreviewController),
2044 "not initialized");
2045
2046 // Validate the mSurface parameter.
2047 videoEditJava_checkAndThrowIllegalArgumentException(&needToBeLoaded, pEnv,
2048 (NULL == mSurface),
2049 "mSurface is null");
2050
2051 jclass surfaceClass = pEnv->FindClass("android/view/Surface");
2052 videoEditJava_checkAndThrowIllegalStateException(&needToBeLoaded, pEnv,
2053 (M4OSA_NULL == surfaceClass),
2054 "not initialized");
2055 //jfieldID surface_native = pEnv->GetFieldID(surfaceClass, "mSurface", "I");
2056 jfieldID surface_native
2057 = pEnv->GetFieldID(surfaceClass, ANDROID_VIEW_SURFACE_JNI_ID, "I");
2058
2059 videoEditJava_checkAndThrowIllegalStateException(&needToBeLoaded, pEnv,
2060 (M4OSA_NULL == surface_native),
2061 "not initialized");
2062
2063 Surface* const p = (Surface*)pEnv->GetIntField(mSurface, surface_native);
2064
2065 sp<Surface> previewSurface = sp<Surface>(p);
Rajneesh Chowdury8e7991a2011-03-15 16:20:17 -07002066 // Validate the mSurface's mNativeSurface field
2067 videoEditJava_checkAndThrowIllegalStateException(&needToBeLoaded, pEnv,
2068 (NULL == previewSurface.get()),
2069 "mNativeSurface is null");
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08002070
2071 result = pContext->mPreviewController->setSurface(previewSurface);
2072 videoEditJava_checkAndThrowRuntimeException(&needToBeLoaded, pEnv,
2073 (M4NO_ERROR != result), result);
2074 VIDEOEDIT_LOG_FUNCTION(ANDROID_LOG_INFO, "VIDEO_EDITOR", "fromMs=%ld, toMs=%ld",
2075 (M4OSA_UInt32)fromMs, (M4OSA_Int32)toMs);
2076
2077 result = pContext->mPreviewController->startPreview((M4OSA_UInt32)fromMs,
2078 (M4OSA_Int32)toMs,
2079 (M4OSA_UInt16)callbackInterval,
2080 (M4OSA_Bool)loop);
2081 videoEditJava_checkAndThrowRuntimeException(&needToBeLoaded, pEnv, (M4NO_ERROR != result), result);
2082}
2083
2084
2085static jobject
2086videoEditor_getProperties(
2087 JNIEnv* pEnv,
2088 jobject thiz,
2089 jstring file)
2090{
2091 jobject object = M4OSA_NULL;
2092 object = videoEditProp_getProperties(pEnv,thiz,file);
2093
2094 return object;
2095
2096}
2097static int videoEditor_getPixels(
2098 JNIEnv* env,
2099 jobject thiz,
2100 jstring path,
2101 jintArray pixelArray,
2102 M4OSA_UInt32 width,
2103 M4OSA_UInt32 height,
2104 M4OSA_UInt32 timeMS)
2105{
2106
2107 M4OSA_ERR err = M4NO_ERROR;
2108 M4OSA_Context mContext = M4OSA_NULL;
2109 jint* m_dst32 = M4OSA_NULL;
2110
2111
2112 // Add a text marker (the condition must always be true).
2113 ADD_TEXT_MARKER_FUN(NULL != env)
2114
2115 const char *pString = env->GetStringUTFChars(path, NULL);
2116 if (pString == M4OSA_NULL) {
2117 if (env != NULL) {
2118 jniThrowException(env, "java/lang/RuntimeException", "Input string null");
2119 }
2120 return M4ERR_ALLOC;
2121 }
2122
2123 err = ThumbnailOpen(&mContext,(const M4OSA_Char*)pString, M4OSA_FALSE);
2124 if (err != M4NO_ERROR || mContext == M4OSA_NULL) {
2125 if (pString != NULL) {
2126 env->ReleaseStringUTFChars(path, pString);
2127 }
2128 if (env != NULL) {
2129 jniThrowException(env, "java/lang/RuntimeException", "ThumbnailOpen failed");
2130 }
2131 }
2132
2133 m_dst32 = env->GetIntArrayElements(pixelArray, NULL);
2134
2135 err = ThumbnailGetPixels32(mContext, (M4OSA_Int32 *)m_dst32, width,height,&timeMS);
2136 if (err != M4NO_ERROR ) {
2137 if (env != NULL) {
2138 jniThrowException(env, "java/lang/RuntimeException",\
2139 "ThumbnailGetPixels32 failed");
2140 }
2141 }
2142 env->ReleaseIntArrayElements(pixelArray, m_dst32, 0);
2143
2144 ThumbnailClose(mContext);
2145 if (pString != NULL) {
2146 env->ReleaseStringUTFChars(path, pString);
2147 }
2148
2149 return timeMS;
2150}
2151
2152static int videoEditor_getPixelsList(
2153 JNIEnv* env,
2154 jobject thiz,
2155 jstring path,
2156 jintArray pixelArray,
2157 M4OSA_UInt32 width,
2158 M4OSA_UInt32 height,
2159 M4OSA_UInt32 deltatimeMS,
2160 M4OSA_UInt32 noOfThumbnails,
2161 M4OSA_UInt32 startTime,
2162 M4OSA_UInt32 endTime)
2163{
2164
2165 M4OSA_ERR err;
2166 M4OSA_Context mContext = M4OSA_NULL;
2167 jint* m_dst32;
2168 M4OSA_UInt32 timeMS = startTime;
2169 int arrayOffset = 0;
2170
2171
2172
2173 // Add a text marker (the condition must always be true).
2174 ADD_TEXT_MARKER_FUN(NULL != env)
2175
2176 const char *pString = env->GetStringUTFChars(path, NULL);
2177 if (pString == M4OSA_NULL) {
2178 if (env != NULL) {
2179 jniThrowException(env, "java/lang/RuntimeException", "Input string null");
2180 }
2181 return M4ERR_ALLOC;
2182 }
2183
2184 err = ThumbnailOpen(&mContext,(const M4OSA_Char*)pString, M4OSA_FALSE);
2185 if (err != M4NO_ERROR || mContext == M4OSA_NULL) {
2186 if (env != NULL) {
2187 jniThrowException(env, "java/lang/RuntimeException", "ThumbnailOpen failed");
2188 }
2189 if (pString != NULL) {
2190 env->ReleaseStringUTFChars(path, pString);
2191 }
2192 return err;
2193 }
2194
2195 m_dst32 = env->GetIntArrayElements(pixelArray, NULL);
2196
2197 do {
2198 err = ThumbnailGetPixels32(mContext, ((M4OSA_Int32 *)m_dst32 + arrayOffset),
2199 width,height,&timeMS);
2200 if (err != M4NO_ERROR ) {
2201 if (env != NULL) {
2202 jniThrowException(env, "java/lang/RuntimeException",\
2203 "ThumbnailGetPixels32 failed");
2204 }
2205 return err;
2206 }
2207 timeMS += deltatimeMS;
2208 arrayOffset += (width * height * 4);
2209 noOfThumbnails--;
2210 } while(noOfThumbnails > 0);
2211
2212 env->ReleaseIntArrayElements(pixelArray, m_dst32, 0);
2213
2214 ThumbnailClose(mContext);
2215 if (pString != NULL) {
2216 env->ReleaseStringUTFChars(path, pString);
2217 }
2218
2219 return err;
2220
2221}
2222
2223static M4OSA_ERR
2224videoEditor_toUTF8Fct(
2225 M4OSA_Void* pBufferIn,
2226 M4OSA_UInt8* pBufferOut,
2227 M4OSA_UInt32* bufferOutSize)
2228{
2229 M4OSA_ERR result = M4NO_ERROR;
2230 M4OSA_UInt32 length = 0;
2231
2232 VIDEOEDIT_LOG_FUNCTION(ANDROID_LOG_INFO, "VIDEO_EDITOR", "videoEditor_toUTF8Fct()");
2233
2234 // Determine the length of the input buffer.
2235 if (M4OSA_NULL != pBufferIn)
2236 {
Dheeraj Sharma7743fa62011-04-12 14:53:58 -07002237 length = strlen((const char *)pBufferIn);
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08002238 }
2239
2240 // Check if the output buffer is large enough to hold the input buffer.
2241 if ((*bufferOutSize) > length)
2242 {
2243 // Check if the input buffer is not M4OSA_NULL.
2244 if (M4OSA_NULL != pBufferIn)
2245 {
2246 // Copy the temp path, ignore the result.
2247 M4OSA_chrNCopy((M4OSA_Char *)pBufferOut, (M4OSA_Char *)pBufferIn, length);
2248 }
2249 else
2250 {
2251 // Set the output buffer to an empty string.
2252 (*(M4OSA_Char *)pBufferOut) = 0;
2253 }
2254 }
2255 else
2256 {
2257 // The buffer is too small.
2258 result = M4xVSSWAR_BUFFER_OUT_TOO_SMALL;
2259 }
2260
2261 // Return the buffer output size.
2262 (*bufferOutSize) = length + 1;
2263
2264 // Return the result.
2265 return(result);
2266}
2267
2268static M4OSA_ERR
2269videoEditor_fromUTF8Fct(
2270 M4OSA_UInt8* pBufferIn,
2271 M4OSA_Void* pBufferOut,
2272 M4OSA_UInt32* bufferOutSize)
2273{
2274 M4OSA_ERR result = M4NO_ERROR;
2275 M4OSA_UInt32 length = 0;
2276
2277 VIDEOEDIT_LOG_FUNCTION(ANDROID_LOG_INFO, "VIDEO_EDITOR", "videoEditor_fromUTF8Fct()");
2278
2279 // Determine the length of the input buffer.
2280 if (M4OSA_NULL != pBufferIn)
2281 {
Dheeraj Sharma7743fa62011-04-12 14:53:58 -07002282 length = strlen((const char *)pBufferIn);
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08002283 }
2284
2285 // Check if the output buffer is large enough to hold the input buffer.
2286 if ((*bufferOutSize) > length)
2287 {
2288 // Check if the input buffer is not M4OSA_NULL.
2289 if (M4OSA_NULL != pBufferIn)
2290 {
2291 // Copy the temp path, ignore the result.
2292 M4OSA_chrNCopy((M4OSA_Char *)pBufferOut, (M4OSA_Char *)pBufferIn, length);
2293 }
2294 else
2295 {
2296 // Set the output buffer to an empty string.
2297 (*(M4OSA_Char *)pBufferOut) = 0;
2298 }
2299 }
2300 else
2301 {
2302 // The buffer is too small.
2303 result = M4xVSSWAR_BUFFER_OUT_TOO_SMALL;
2304 }
2305
2306 // Return the buffer output size.
2307 (*bufferOutSize) = length + 1;
2308
2309 // Return the result.
2310 return(result);
2311}
2312
2313static M4OSA_ERR
2314videoEditor_getTextRgbBufferFct(
2315 M4OSA_Void* pRenderingData,
2316 M4OSA_Void* pTextBuffer,
2317 M4OSA_UInt32 textBufferSize,
2318 M4VIFI_ImagePlane** pOutputPlane)
2319{
2320 M4OSA_ERR result = M4NO_ERROR;
2321
2322 VIDEOEDIT_LOG_FUNCTION(ANDROID_LOG_INFO, "VIDEO_EDITOR", "videoEditor_getTextRgbBufferFct()");
2323
2324 // Return the result.
2325 return(result);
2326}
2327
2328static void
2329videoEditor_callOnProgressUpdate(
2330 ManualEditContext* pContext,
2331 int task,
2332 int progress)
2333{
2334 JNIEnv* pEnv = NULL;
2335
2336
2337 // Attach the current thread.
2338 pContext->pVM->AttachCurrentThread(&pEnv, NULL);
2339
2340
2341 // Call the on completion callback.
2342 pEnv->CallVoidMethod(pContext->engine, pContext->onProgressUpdateMethodId,
2343 videoEditJava_getEngineCToJava(task), progress);
2344
2345
2346 // Detach the current thread.
2347 pContext->pVM->DetachCurrentThread();
2348}
2349
2350static void
2351videoEditor_freeContext(
2352 JNIEnv* pEnv,
2353 ManualEditContext** ppContext)
2354{
2355 ManualEditContext* pContext = M4OSA_NULL;
2356
2357 VIDEOEDIT_LOG_FUNCTION(ANDROID_LOG_INFO, "VIDEO_EDITOR", "videoEditor_freeContext");
2358
2359 // Set the context pointer.
2360 pContext = (*ppContext);
2361
2362 // Check if the context was set.
2363 if (M4OSA_NULL != pContext)
2364 {
2365 // Check if a global reference to the engine object was set.
2366 if (NULL != pContext->engine)
2367 {
2368 // Free the global reference.
2369 pEnv->DeleteGlobalRef(pContext->engine);
2370 pContext->engine = NULL;
2371 }
2372
2373 // Check if the temp path was set.
2374 if (M4OSA_NULL != pContext->initParams.pTempPath)
2375 {
2376 // Free the memory allocated for the temp path.
2377 videoEditOsal_free(pContext->initParams.pTempPath);
2378 pContext->initParams.pTempPath = M4OSA_NULL;
2379 }
2380
2381 // Check if the file writer was set.
2382 if (M4OSA_NULL != pContext->initParams.pFileWritePtr)
2383 {
2384 // Free the memory allocated for the file writer.
2385 videoEditOsal_free(pContext->initParams.pFileWritePtr);
2386 pContext->initParams.pFileWritePtr = M4OSA_NULL;
2387 }
2388
2389 // Check if the file reader was set.
2390 if (M4OSA_NULL != pContext->initParams.pFileReadPtr)
2391 {
2392 // Free the memory allocated for the file reader.
2393 videoEditOsal_free(pContext->initParams.pFileReadPtr);
2394 pContext->initParams.pFileReadPtr = M4OSA_NULL;
2395 }
2396
2397 // Free the memory allocated for the context.
2398 videoEditOsal_free(pContext);
2399 pContext = M4OSA_NULL;
2400
2401 // Reset the context pointer.
2402 (*ppContext) = M4OSA_NULL;
2403 }
2404}
2405
2406static jobject
2407videoEditor_getVersion(
2408 JNIEnv* pEnv,
2409 jobject thiz)
2410{
2411 bool isSuccessful = true;
2412 jobject version = NULL;
2413 M4_VersionInfo versionInfo = {0, 0, 0, 0};
2414 M4OSA_ERR result = M4NO_ERROR;
2415
2416 VIDEOEDIT_LOG_API(ANDROID_LOG_INFO, "VIDEO_EDITOR", "videoEditor_getVersion()");
2417
2418 versionInfo.m_structSize = sizeof(versionInfo);
2419 versionInfo.m_major = VIDEOEDITOR_VERSION_MAJOR;
2420 versionInfo.m_minor = VIDEOEDITOR_VERSION_MINOR;
2421 versionInfo.m_revision = VIDEOEDITOR_VERSION_REVISION;
2422
2423 VIDEOEDIT_LOG_API(ANDROID_LOG_INFO, "VIDEO_EDITOR", "videoEditor_getVersion() major %d,\
2424 minor %d, revision %d", versionInfo.m_major, versionInfo.m_minor, versionInfo.m_revision);
2425
2426 // Create a version object.
2427 videoEditClasses_createVersion(&isSuccessful, pEnv, &versionInfo, &version);
2428
2429 // Return the version object.
2430 return(version);
2431}
2432
2433static void
2434videoEditor_init(
2435 JNIEnv* pEnv,
2436 jobject thiz,
2437 jstring tempPath,
2438 jstring libraryPath)
2439{
2440 bool initialized = true;
2441 ManualEditContext* pContext = M4OSA_NULL;
2442 VideoEditJava_EngineMethodIds methodIds = {NULL};
2443 M4OSA_Char* pLibraryPath = M4OSA_NULL;
2444 M4OSA_Char* pTextRendererPath = M4OSA_NULL;
2445 M4OSA_UInt32 textRendererPathLength = 0;
2446 M4OSA_ERR result = M4NO_ERROR;
2447
2448 VIDEOEDIT_LOG_API(ANDROID_LOG_INFO, "VIDEO_EDITOR", "videoEditor_init()");
2449
2450 // Add a text marker (the condition must always be true).
2451 ADD_TEXT_MARKER_FUN(NULL != pEnv)
2452
2453 // Get the context.
2454 pContext = (ManualEditContext*)videoEditClasses_getContext(&initialized, pEnv, thiz);
2455
2456 // Get the engine method ids.
2457 videoEditJava_getEngineMethodIds(&initialized, pEnv, &methodIds);
2458
2459 // Validate the tempPath parameter.
2460 videoEditJava_checkAndThrowIllegalArgumentException(&initialized, pEnv,
2461 (NULL == tempPath),
2462 "tempPath is null");
2463
2464 // Make sure that the context was not set already.
2465 videoEditJava_checkAndThrowIllegalStateException(&initialized, pEnv,
2466 (M4OSA_NULL != pContext),
2467 "already initialized");
2468
2469 // Check if the initialization succeeded (required because of dereferencing of psContext,
2470 // and freeing when initialization fails).
2471 if (initialized)
2472 {
2473 // Allocate a new context.
2474 pContext = new ManualEditContext;
2475
2476 // Check if the initialization succeeded (required because of dereferencing of psContext).
2477 //if (initialized)
2478 if (pContext != NULL)
2479 {
2480 // Set the state to not initialized.
2481 pContext->state = ManualEditState_NOT_INITIALIZED;
2482
2483 // Allocate a file read pointer structure.
2484 pContext->initParams.pFileReadPtr =
2485 (M4OSA_FileReadPointer*)videoEditOsal_alloc(&initialized, pEnv,
2486 sizeof(M4OSA_FileReadPointer), "FileReadPointer");
2487
2488 // Allocate a file write pointer structure.
2489 pContext->initParams.pFileWritePtr =
2490 (M4OSA_FileWriterPointer*)videoEditOsal_alloc(&initialized, pEnv,
2491 sizeof(M4OSA_FileWriterPointer), "FileWriterPointer");
2492
2493 // Get the temp path.
2494 M4OSA_Char* tmpString =
2495 (M4OSA_Char *)videoEditJava_getString(&initialized, pEnv, tempPath,
2496 NULL, M4OSA_NULL);
2497 pContext->initParams.pTempPath = (M4OSA_Char *)
Shyam Pallapothuc0125dd2011-04-21 19:13:56 -07002498 M4OSA_32bitAlignedMalloc(strlen((const char *)tmpString) + 1, 0x0,
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08002499 (M4OSA_Char *)"tempPath");
2500 //initialize the first char. so that strcat works.
2501 M4OSA_Char *ptmpChar = (M4OSA_Char*)pContext->initParams.pTempPath;
2502 ptmpChar[0] = 0x00;
Dheeraj Sharma7743fa62011-04-12 14:53:58 -07002503 strncat((char *)pContext->initParams.pTempPath, (const char *)tmpString,
2504 (size_t)strlen((const char *)tmpString));
2505 strncat((char *)pContext->initParams.pTempPath, (const char *)"/", (size_t)1);
Shyam Pallapothuc0125dd2011-04-21 19:13:56 -07002506 free(tmpString);
Dharmaray Kundargic16ccc12011-01-21 19:20:49 -08002507 pContext->mIsUpdateOverlay = false;
2508 pContext->mOverlayFileName = NULL;
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08002509 }
2510
2511 // Check if the initialization succeeded
2512 // (required because of dereferencing of pContext, pFileReadPtr and pFileWritePtr).
2513 if (initialized)
2514 {
2515
2516 // Initialize the OSAL file system function pointers.
2517 videoEditOsal_getFilePointers(pContext->initParams.pFileReadPtr ,
2518 pContext->initParams.pFileWritePtr);
2519
2520 // Set the UTF8 conversion functions.
2521 pContext->initParams.pConvToUTF8Fct = videoEditor_toUTF8Fct;
2522 pContext->initParams.pConvFromUTF8Fct = videoEditor_fromUTF8Fct;
2523
2524 // Set the callback method ids.
2525 pContext->onProgressUpdateMethodId = methodIds.onProgressUpdate;
2526
2527 // Set the virtual machine.
2528 pEnv->GetJavaVM(&(pContext->pVM));
2529
2530 // Create a global reference to the engine object.
2531 pContext->engine = pEnv->NewGlobalRef(thiz);
2532
2533 // Check if the global reference could be created.
2534 videoEditJava_checkAndThrowRuntimeException(&initialized, pEnv,
2535 (NULL == pContext->engine), M4NO_ERROR);
2536 }
2537
2538 // Check if the initialization succeeded (required because of dereferencing of pContext).
2539 if (initialized)
2540 {
2541 // Log the API call.
2542 VIDEOEDIT_LOG_API(ANDROID_LOG_INFO, "VIDEO_EDITOR", "M4xVSS_Init()");
2543
2544 // Initialize the visual studio library.
2545 result = M4xVSS_Init(&pContext->engineContext, &pContext->initParams);
2546
2547 // Log the result.
2548 VIDEOEDIT_LOG_RESULT(ANDROID_LOG_INFO, "VIDEO_EDITOR",
2549 videoEditOsal_getResultString(result));
2550
2551 // Check if the library could be initialized.
2552 videoEditJava_checkAndThrowRuntimeException(&initialized, pEnv,
2553 (M4NO_ERROR != result), result);
2554 }
2555
2556 if(initialized)
2557 {
2558 pContext->mPreviewController = new VideoEditorPreviewController();
2559 videoEditJava_checkAndThrowIllegalStateException(&initialized, pEnv,
2560 (M4OSA_NULL == pContext->mPreviewController),
2561 "not initialized");
2562 pContext->mAudioSettings =
2563 (M4xVSS_AudioMixingSettings *)
Shyam Pallapothuc0125dd2011-04-21 19:13:56 -07002564 M4OSA_32bitAlignedMalloc(sizeof(M4xVSS_AudioMixingSettings),0x0,
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08002565 (M4OSA_Char *)"mAudioSettings");
2566 videoEditJava_checkAndThrowIllegalStateException(&initialized, pEnv,
2567 (M4OSA_NULL == pContext->mAudioSettings),
2568 "not initialized");
2569 pContext->mAudioSettings->pFile = M4OSA_NULL;
Santosh Madhava9ee9aad2011-02-10 20:18:18 -08002570 pContext->mAudioSettings->pPCMFilePath = M4OSA_NULL;
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08002571 pContext->mAudioSettings->bRemoveOriginal = 0;
2572 pContext->mAudioSettings->uiNbChannels = 0;
2573 pContext->mAudioSettings->uiSamplingFrequency = 0;
2574 pContext->mAudioSettings->uiExtendedSamplingFrequency = 0;
2575 pContext->mAudioSettings->uiAddCts = 0;
2576 pContext->mAudioSettings->uiAddVolume = 0;
2577 pContext->mAudioSettings->beginCutMs = 0;
2578 pContext->mAudioSettings->endCutMs = 0;
2579 pContext->mAudioSettings->fileType = 0;
2580 pContext->mAudioSettings->bLoop = 0;
2581 pContext->mAudioSettings->uiInDucking_lowVolume = 0;
2582 pContext->mAudioSettings->bInDucking_enable = 0;
2583 pContext->mAudioSettings->uiBTChannelCount = 0;
2584 pContext->mAudioSettings->uiInDucking_threshold = 0;
2585 }
2586 // Check if the library could be initialized.
2587 if (initialized)
2588 {
2589 // Set the state to initialized.
2590 pContext->state = ManualEditState_INITIALIZED;
2591 }
2592
2593 // Set the context.
2594 videoEditClasses_setContext(&initialized, pEnv, thiz, (void* )pContext);
2595 pLibraryPath = M4OSA_NULL;
2596
2597 pContext->pEditSettings = M4OSA_NULL;
2598 // Cleanup if anything went wrong during initialization.
2599 if (!initialized)
2600 {
2601 // Free the context.
2602 videoEditor_freeContext(pEnv, &pContext);
2603 }
2604 }
2605}
2606
2607/*+ PROGRESS CB */
2608static
2609M4OSA_ERR videoEditor_processClip(
2610 JNIEnv* pEnv,
2611 jobject thiz,
2612 int unuseditemID) {
2613
2614 bool loaded = true;
2615 ManualEditContext* pContext = NULL;
2616 M4OSA_UInt8 progress = 0;
2617 M4OSA_UInt8 progressBase = 0;
2618 M4OSA_UInt8 lastProgress = 0;
2619 M4OSA_ERR result = M4NO_ERROR;
2620
2621 // Get the context.
2622 pContext = (ManualEditContext*)videoEditClasses_getContext(&loaded, pEnv, thiz);
2623
2624 // Make sure that the context was set.
2625 videoEditJava_checkAndThrowIllegalStateException(&loaded, pEnv,
2626 (M4OSA_NULL == pContext),
2627 "not initialized");
2628
2629 // We start in Analyzing state
Dheeraj Sharma5618e032011-01-29 21:29:49 -08002630 pContext->state = ManualEditState_INITIALIZED;
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08002631 M4OSA_ERR completionResult = M4VSS3GPP_WAR_ANALYZING_DONE;
2632 ManualEditState completionState = ManualEditState_OPENED;
2633 ManualEditState errorState = ManualEditState_ANALYZING_ERROR;
2634
2635 // While analyzing progress goes from 0 to 50
2636 progressBase = 0;
2637
2638 // Set the text rendering function.
2639 if (M4OSA_NULL != pContext->pTextRendererFunction)
2640 {
2641 // Use the text renderer function in the library.
2642 pContext->pEditSettings->xVSS.pTextRenderingFct = pContext->pTextRendererFunction;
2643 }
2644 else
2645 {
2646 // Use the internal text renderer function.
2647 pContext->pEditSettings->xVSS.pTextRenderingFct = videoEditor_getTextRgbBufferFct;
2648 }
2649
2650 // Send the command.
2651 LOGV("videoEditor_processClip ITEM %d Calling M4xVSS_SendCommand()", unuseditemID);
2652 result = M4xVSS_SendCommand(pContext->engineContext, pContext->pEditSettings);
2653 LOGV("videoEditor_processClip ITEM %d M4xVSS_SendCommand() returned 0x%x",
2654 unuseditemID, (unsigned int) result);
2655
2656 // Remove warnings indications (we only care about errors here)
2657 if ((result == M4VSS3GPP_WAR_TRANSCODING_NECESSARY)
2658 || (result == M4VSS3GPP_WAR_OUTPUTFILESIZE_EXCEED)) {
2659 result = M4NO_ERROR;
2660 }
2661
2662 // Send the first progress indication (=0)
2663 LOGV("VERY FIRST PROGRESS videoEditor_processClip ITEM %d Progress indication %d",
2664 unuseditemID, progress);
2665 pEnv->CallVoidMethod(pContext->engine, pContext->onProgressUpdateMethodId,
2666 unuseditemID, progress);
2667
2668 // Check if a task is being performed.
2669 // ??? ADD STOPPING MECHANISM
2670 LOGV("videoEditor_processClip Entering processing loop");
2671 while((result == M4NO_ERROR)
2672 &&(pContext->state!=ManualEditState_SAVED)
2673 &&(pContext->state!=ManualEditState_STOPPING)) {
2674
2675 // Perform the next processing step.
2676 //LOGV("LVME_processClip Entering M4xVSS_Step()");
2677 result = M4xVSS_Step(pContext->engineContext, &progress);
2678 //LOGV("LVME_processClip M4xVSS_Step() returned 0x%x", (unsigned int)result);
2679
2680 // Log the the 1 % .. 100 % progress after processing.
2681 progress = progressBase + progress/2;
2682 if (progress != lastProgress)
2683 {
2684 // Send a progress notification.
2685 LOGV("videoEditor_processClip ITEM %d Progress indication %d",
2686 unuseditemID, progress);
2687 pEnv->CallVoidMethod(pContext->engine,
2688 pContext->onProgressUpdateMethodId,
2689 unuseditemID, progress);
2690 lastProgress = progress;
2691 }
2692
2693 // Check if processing has been completed.
2694 if (result == completionResult)
2695 {
2696 // Set the state to the completions state.
2697 pContext->state = completionState;
2698 LOGV("videoEditor_processClip ITEM %d STATE changed to %d",
2699 unuseditemID, pContext->state);
2700
2701 // Reset progress indication, as we switch to next state
2702 lastProgress = 0;
2703
2704 // Reset error code, as we start a new round of processing
2705 result = M4NO_ERROR;
2706
2707 // Check if we are analyzing input
2708 if (pContext->state == ManualEditState_OPENED) {
2709 // File is opened, we must start saving it
2710 LOGV("videoEditor_processClip Calling M4xVSS_SaveStart()");
2711 result = M4xVSS_SaveStart(pContext->engineContext,
2712 (M4OSA_Char*)pContext->pEditSettings->pOutputFile,
2713 (M4OSA_UInt32)pContext->pEditSettings->uiOutputPathSize);
2714 LOGV("videoEditor_processClip ITEM %d SaveStart() returned 0x%x",
2715 unuseditemID, (unsigned int) result);
2716
2717 // Set the state to saving.
2718 pContext->state = ManualEditState_SAVING;
2719 completionState = ManualEditState_SAVED;
2720 completionResult = M4VSS3GPP_WAR_SAVING_DONE;
2721 errorState = ManualEditState_SAVING_ERROR;
2722
2723 // While saving progress goes from 50 to 100
2724 progressBase = 50;
2725 }
2726 // Check if we encoding is ongoing
2727 else if (pContext->state == ManualEditState_SAVED) {
2728 if (progress != 100) {
2729 // Send a progress notification.
2730 progress = 100;
2731 LOGI("videoEditor_processClip ITEM %d Last progress indication %d",
2732 unuseditemID, progress);
2733 pEnv->CallVoidMethod(pContext->engine,
2734 pContext->onProgressUpdateMethodId,
2735 unuseditemID, progress);
2736 }
2737
2738 // Stop the encoding.
2739 LOGV("videoEditor_processClip Calling M4xVSS_SaveStop()");
2740 result = M4xVSS_SaveStop(pContext->engineContext);
2741 LOGV("videoEditor_processClip M4xVSS_SaveStop() returned 0x%x", result);
2742 }
2743 // Other states are unexpected
2744 else {
2745 result = M4ERR_STATE;
2746 LOGE("videoEditor_processClip ITEM %d State ERROR 0x%x",
2747 unuseditemID, (unsigned int) result);
2748 }
2749 }
2750
2751 // Check if an error occurred.
2752 if (result != M4NO_ERROR)
2753 {
2754 // Set the state to the error state.
2755 pContext->state = errorState;
2756
2757 // Log the result.
2758 LOGE("videoEditor_processClip ITEM %d Processing ERROR 0x%x",
2759 unuseditemID, (unsigned int) result);
2760 }
2761 }
2762
2763 // Return the error result
2764 LOGE("videoEditor_processClip ITEM %d END 0x%x", unuseditemID, (unsigned int) result);
2765 return result;
2766}
2767/*+ PROGRESS CB */
2768
2769static int
2770videoEditor_generateClip(
2771 JNIEnv* pEnv,
2772 jobject thiz,
2773 jobject settings) {
2774 bool loaded = true;
2775 ManualEditContext* pContext = M4OSA_NULL;
2776 M4OSA_ERR result = M4NO_ERROR;
2777
2778 LOGV("videoEditor_generateClip START");
2779
2780 // Get the context.
2781 pContext = (ManualEditContext*)videoEditClasses_getContext(&loaded, pEnv, thiz);
2782
2783 Mutex::Autolock autoLock(pContext->mLock);
2784
2785 // Validate the settings parameter.
2786 videoEditJava_checkAndThrowIllegalArgumentException(&loaded, pEnv,
2787 (NULL == settings),
2788 "settings is null");
2789
2790 // Make sure that the context was set.
2791 videoEditJava_checkAndThrowIllegalStateException(&loaded, pEnv,
2792 (M4OSA_NULL == pContext),
2793 "not initialized");
2794
2795 // Load the clip settings
2796 LOGV("videoEditor_generateClip Calling videoEditor_loadSettings");
2797 videoEditor_loadSettings(pEnv, thiz, settings);
2798 LOGV("videoEditor_generateClip videoEditor_loadSettings returned");
2799
2800 // Generate the clip
2801 LOGV("videoEditor_generateClip Calling LVME_processClip");
2802 result = videoEditor_processClip(pEnv, thiz, 0 /*item id is unused*/);
2803 LOGV("videoEditor_generateClip videoEditor_processClip returned 0x%x", result);
2804
Dheeraj Sharma5618e032011-01-29 21:29:49 -08002805 if (pContext->state != ManualEditState_INITIALIZED) {
2806 // Free up memory (whatever the result)
2807 videoEditor_unloadSettings(pEnv, thiz);
2808 }
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08002809
2810 LOGV("videoEditor_generateClip END 0x%x", (unsigned int) result);
2811 return result;
2812}
2813
2814static void
2815videoEditor_loadSettings(
2816 JNIEnv* pEnv,
2817 jobject thiz,
2818 jobject settings)
2819{
2820 bool needToBeLoaded = true;
2821 ManualEditContext* pContext = M4OSA_NULL;
2822
2823 VIDEOEDIT_LOG_API(ANDROID_LOG_INFO, "VIDEO_EDITOR", "videoEditor_loadSettings()");
2824
2825 // Add a code marker (the condition must always be true).
2826 ADD_CODE_MARKER_FUN(NULL != pEnv)
2827
2828 // Get the context.
2829 pContext = (ManualEditContext*)videoEditClasses_getContext(&needToBeLoaded,
2830 pEnv, thiz);
2831
2832 // Validate the settings parameter.
2833 videoEditJava_checkAndThrowIllegalArgumentException(&needToBeLoaded, pEnv,
2834 (NULL == settings),
2835 "settings is null");
2836
2837 // Make sure that the context was set.
2838 videoEditJava_checkAndThrowIllegalStateException(&needToBeLoaded, pEnv,
2839 (M4OSA_NULL == pContext),
2840 "not initialized");
2841
2842 // Check if the context is valid (required because the context is dereferenced).
2843 if (needToBeLoaded)
2844 {
2845 // Make sure that we are in a correct state.
2846 videoEditJava_checkAndThrowIllegalStateException(&needToBeLoaded, pEnv,
2847 (pContext->state != ManualEditState_INITIALIZED),
2848 "settings already loaded");
2849
2850 // Retrieve the edit settings.
2851 if(pContext->pEditSettings != M4OSA_NULL) {
2852 videoEditClasses_freeEditSettings(&pContext->pEditSettings);
2853 pContext->pEditSettings = M4OSA_NULL;
2854 }
2855 videoEditClasses_getEditSettings(&needToBeLoaded, pEnv, settings,
2856 &pContext->pEditSettings,true);
2857 }
2858
2859 // Check if the edit settings could be retrieved.
2860 if (needToBeLoaded)
2861 {
2862 // Log the edit settings.
2863 VIDEOEDIT_LOG_API(ANDROID_LOG_INFO, "VIDEO_EDITOR", "inside load settings");
2864 VIDEOEDIT_LOG_EDIT_SETTINGS(pContext->pEditSettings);
2865 }
2866 LOGV("videoEditor_loadSettings END");
2867}
2868
2869
2870
2871static void
2872videoEditor_unloadSettings(
2873 JNIEnv* pEnv,
2874 jobject thiz)
2875{
2876 bool needToBeUnLoaded = true;
2877 ManualEditContext* pContext = M4OSA_NULL;
2878 M4OSA_ERR result = M4NO_ERROR;
2879
2880 VIDEOEDIT_LOG_API(ANDROID_LOG_INFO, "VIDEO_EDITOR", "videoEditor_unloadSettings()");
2881
2882 // Get the context.
2883 pContext = (ManualEditContext*)videoEditClasses_getContext(&needToBeUnLoaded, pEnv, thiz);
2884
2885 // Make sure that the context was set.
2886 videoEditJava_checkAndThrowIllegalStateException(&needToBeUnLoaded, pEnv,
2887 (M4OSA_NULL == pContext),
2888 "not initialized");
2889
2890 // Check if the context is valid (required because the context is dereferenced).
2891 if (needToBeUnLoaded)
2892 {
2893 LOGV("videoEditor_unloadSettings state %d", pContext->state);
2894 // Make sure that we are in a correct state.
2895 videoEditJava_checkAndThrowIllegalStateException(&needToBeUnLoaded, pEnv,
2896 ((pContext->state != ManualEditState_ANALYZING ) &&
2897 (pContext->state != ManualEditState_ANALYZING_ERROR) &&
2898 (pContext->state != ManualEditState_OPENED ) &&
2899 (pContext->state != ManualEditState_SAVING_ERROR ) &&
2900 (pContext->state != ManualEditState_SAVED ) &&
2901 (pContext->state != ManualEditState_STOPPING ) ),
2902 "videoEditor_unloadSettings no load settings in progress");
2903 }
2904
2905 // Check if we are in a correct state.
2906 if (needToBeUnLoaded)
2907 {
2908 // Check if the thread could be stopped.
2909 if (needToBeUnLoaded)
2910 {
2911 // Close the command.
2912 LOGV("videoEditor_unloadSettings Calling M4xVSS_CloseCommand()");
2913 result = M4xVSS_CloseCommand(pContext->engineContext);
2914 LOGV("videoEditor_unloadSettings M4xVSS_CloseCommand() returned 0x%x",
2915 (unsigned int)result);
2916
2917 // Check if the command could be closed.
2918 videoEditJava_checkAndThrowRuntimeException(&needToBeUnLoaded, pEnv,
2919 (M4NO_ERROR != result), result);
2920 }
2921
2922 // Check if the command could be closed.
2923 if (needToBeUnLoaded)
2924 {
2925 // Free the edit settings.
2926 //videoEditClasses_freeEditSettings(&pContext->pEditSettings);
2927
2928 // Reset the thread result.
2929 pContext->threadResult = M4NO_ERROR;
2930
2931 // Reset the thread progress.
2932 pContext->threadProgress = 0;
2933
2934 // Set the state to initialized.
2935 pContext->state = ManualEditState_INITIALIZED;
2936 }
2937 }
2938}
2939
2940static void
2941videoEditor_stopEncoding(
2942 JNIEnv* pEnv,
2943 jobject thiz)
2944{
2945 bool stopped = true;
2946 ManualEditContext* pContext = M4OSA_NULL;
2947 M4OSA_ERR result = M4NO_ERROR;
2948
2949 LOGV("videoEditor_stopEncoding START");
2950
2951 // Get the context.
2952 pContext = (ManualEditContext*)videoEditClasses_getContext(&stopped, pEnv, thiz);
2953
2954 // Change state and get Lock
2955 // This will ensure the generateClip function exits
2956 pContext->state = ManualEditState_STOPPING;
2957 Mutex::Autolock autoLock(pContext->mLock);
2958
2959 // Make sure that the context was set.
2960 videoEditJava_checkAndThrowIllegalStateException(&stopped, pEnv,
2961 (M4OSA_NULL == pContext),
2962 "not initialized");
2963
2964 if (stopped) {
2965
2966 // Check if the command should be closed.
2967 if (pContext->state != ManualEditState_INITIALIZED)
2968 {
2969 // Close the command.
2970 LOGV("videoEditor_stopEncoding Calling M4xVSS_CloseCommand()");
2971 result = M4xVSS_CloseCommand(pContext->engineContext);
2972 LOGV("videoEditor_stopEncoding M4xVSS_CloseCommand() returned 0x%x",
2973 (unsigned int)result);
2974 }
2975
2976 // Check if the command could be closed.
2977 videoEditJava_checkAndThrowRuntimeException(&stopped, pEnv,
2978 (M4NO_ERROR != result), result);
2979
2980 // Free the edit settings.
2981 videoEditClasses_freeEditSettings(&pContext->pEditSettings);
2982
2983 // Set the state to initialized.
2984 pContext->state = ManualEditState_INITIALIZED;
2985 }
2986
2987}
2988
2989static void
2990videoEditor_release(
2991 JNIEnv* pEnv,
2992 jobject thiz)
2993{
2994 bool released = true;
2995 ManualEditContext* pContext = M4OSA_NULL;
2996 M4OSA_ERR result = M4NO_ERROR;
2997
2998 VIDEOEDIT_LOG_API(ANDROID_LOG_INFO, "VIDEO_EDITOR", "videoEditor_release()");
2999
3000 // Add a text marker (the condition must always be true).
3001 ADD_TEXT_MARKER_FUN(NULL != pEnv)
3002
3003 // Get the context.
3004 pContext = (ManualEditContext*)videoEditClasses_getContext(&released, pEnv, thiz);
3005
3006 // If context is not set, return (we consider release already happened)
3007 if (pContext == NULL) {
3008 LOGV("videoEditor_release Nothing to do, context is aleady NULL");
3009 return;
3010 }
3011
3012
3013 // Check if the context is valid (required because the context is dereferenced).
3014 if (released)
3015 {
3016 if (pContext->state != ManualEditState_INITIALIZED)
3017 {
3018 // Change state and get Lock
3019 // This will ensure the generateClip function exits if it is running
3020 pContext->state = ManualEditState_STOPPING;
3021 Mutex::Autolock autoLock(pContext->mLock);
3022 }
3023
3024 // Reset the context.
3025 videoEditClasses_setContext(&released, pEnv, thiz, (void *)M4OSA_NULL);
3026
3027 // Check if the command should be closed.
3028 if (pContext->state != ManualEditState_INITIALIZED)
3029 {
3030 // Close the command.
3031 LOGV("videoEditor_release Calling M4xVSS_CloseCommand() state =%d",
3032 pContext->state);
3033 result = M4xVSS_CloseCommand(pContext->engineContext);
3034 LOGV("videoEditor_release M4xVSS_CloseCommand() returned 0x%x",
3035 (unsigned int)result);
3036
3037 // Check if the command could be closed.
3038 videoEditJava_checkAndThrowRuntimeException(&released, pEnv,
3039 (M4NO_ERROR != result), result);
3040 }
3041
3042 // Cleanup the engine.
3043 LOGV("videoEditor_release Calling M4xVSS_CleanUp()");
3044 result = M4xVSS_CleanUp(pContext->engineContext);
3045 LOGV("videoEditor_release M4xVSS_CleanUp() returned 0x%x", (unsigned int)result);
3046
3047 // Check if the cleanup succeeded.
3048 videoEditJava_checkAndThrowRuntimeException(&released, pEnv,
3049 (M4NO_ERROR != result), result);
3050
3051 // Free the edit settings.
3052 videoEditClasses_freeEditSettings(&pContext->pEditSettings);
3053 pContext->pEditSettings = M4OSA_NULL;
3054
3055
3056 if(pContext->mPreviewController != M4OSA_NULL)
3057 {
3058 delete pContext->mPreviewController;
3059 pContext->mPreviewController = M4OSA_NULL;
3060 }
3061
Santosh Madhava71771b32011-02-28 18:30:53 -08003062 // Free the mAudioSettings context.
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08003063 if(pContext->mAudioSettings != M4OSA_NULL)
3064 {
Santosh Madhava71771b32011-02-28 18:30:53 -08003065 if (pContext->mAudioSettings->pFile != NULL) {
Shyam Pallapothuc0125dd2011-04-21 19:13:56 -07003066 free(pContext->mAudioSettings->pFile);
Santosh Madhava71771b32011-02-28 18:30:53 -08003067 pContext->mAudioSettings->pFile = M4OSA_NULL;
3068 }
3069 if (pContext->mAudioSettings->pPCMFilePath != NULL) {
Shyam Pallapothuc0125dd2011-04-21 19:13:56 -07003070 free(pContext->mAudioSettings->pPCMFilePath);
Santosh Madhava71771b32011-02-28 18:30:53 -08003071 pContext->mAudioSettings->pPCMFilePath = M4OSA_NULL;
3072 }
3073
Shyam Pallapothuc0125dd2011-04-21 19:13:56 -07003074 free(pContext->mAudioSettings);
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08003075 pContext->mAudioSettings = M4OSA_NULL;
3076 }
3077 videoEditor_freeContext(pEnv, &pContext);
3078 }
3079}
3080
3081static int
3082videoEditor_registerManualEditMethods(
3083 JNIEnv* pEnv)
3084{
3085 int result = -1;
3086
3087 VIDEOEDIT_LOG_FUNCTION(ANDROID_LOG_INFO, "VIDEO_EDITOR",
3088 "videoEditor_registerManualEditMethods()");
3089
3090 // Look up the engine class
3091 jclass engineClazz = pEnv->FindClass(MANUAL_EDIT_ENGINE_CLASS_NAME);
3092
3093 // Clear any resulting exceptions.
3094 pEnv->ExceptionClear();
3095
3096 // Check if the engine class was found.
3097 if (NULL != engineClazz)
3098 {
3099 // Register all the methods.
3100 if (pEnv->RegisterNatives(engineClazz, gManualEditMethods,
3101 sizeof(gManualEditMethods) / sizeof(gManualEditMethods[0])) == JNI_OK)
3102 {
3103 // Success.
3104 result = 0;
3105 }
3106 }
3107
3108 // Return the result.
3109 return(result);
3110}
3111
3112/*******Audio Graph*******/
3113
3114static M4OSA_UInt32 getDecibelSound(M4OSA_UInt32 value)
3115{
3116 int dbSound = 1;
3117
3118 if (value == 0) return 0;
3119
3120 if (value > 0x4000 && value <= 0x8000) // 32768
3121 dbSound = 90;
3122 else if (value > 0x2000 && value <= 0x4000) // 16384
3123 dbSound = 84;
3124 else if (value > 0x1000 && value <= 0x2000) // 8192
3125 dbSound = 78;
3126 else if (value > 0x0800 && value <= 0x1000) // 4028
3127 dbSound = 72;
3128 else if (value > 0x0400 && value <= 0x0800) // 2048
3129 dbSound = 66;
3130 else if (value > 0x0200 && value <= 0x0400) // 1024
3131 dbSound = 60;
3132 else if (value > 0x0100 && value <= 0x0200) // 512
3133 dbSound = 54;
3134 else if (value > 0x0080 && value <= 0x0100) // 256
3135 dbSound = 48;
3136 else if (value > 0x0040 && value <= 0x0080) // 128
3137 dbSound = 42;
3138 else if (value > 0x0020 && value <= 0x0040) // 64
3139 dbSound = 36;
3140 else if (value > 0x0010 && value <= 0x0020) // 32
3141 dbSound = 30;
3142 else if (value > 0x0008 && value <= 0x0010) //16
3143 dbSound = 24;
3144 else if (value > 0x0007 && value <= 0x0008) //8
3145 dbSound = 24;
3146 else if (value > 0x0003 && value <= 0x0007) // 4
3147 dbSound = 18;
3148 else if (value > 0x0001 && value <= 0x0003) //2
3149 dbSound = 12;
3150 else if (value > 0x000 && value == 0x0001) // 1
3151 dbSound = 6;
3152 else
3153 dbSound = 0;
3154
3155 return dbSound;
3156}
3157
3158typedef struct
3159{
3160 M4OSA_UInt8 *m_dataAddress;
3161 M4OSA_UInt32 m_bufferSize;
3162} M4AM_Buffer;
3163
3164
James Dong581cad22011-01-19 11:35:06 -08003165M4OSA_UInt8 logLookUp[256] = {
Dharmaray Kundargicd196d32011-01-16 15:47:16 -080031660,120,137,146,154,159,163,167,171,173,176,178,181,182,184,186,188,189,190,192,193,
3167194,195,196,198,199,199,200,201,202,203,204,205,205,206,207,207,208,209,209,210,
3168211,211,212,212,213,213,214,215,215,216,216,216,217,217,218,218,219,219,220,220,
3169220,221,221,222,222,222,223,223,223,224,224,224,225,225,225,226,226,226,227,227,
3170227,228,228,228,229,229,229,229,230,230,230,230,231,231,231,232,232,232,232,233,
3171233,233,233,233,234,234,234,234,235,235,235,235,236,236,236,236,236,237,237,237,
3172237,237,238,238,238,238,238,239,239,239,239,239,240,240,240,240,240,240,241,241,
3173241,241,241,241,242,242,242,242,242,242,243,243,243,243,243,243,244,244,244,244,
3174244,244,245,245,245,245,245,245,245,246,246,246,246,246,246,246,247,247,247,247,
3175247,247,247,247,248,248,248,248,248,248,248,249,249,249,249,249,249,249,249,250,
3176250,250,250,250,250,250,250,250,251,251,251,251,251,251,251,251,252,252,252,252,
3177252,252,252,252,252,253,253,253,253,253,253,253,253,253,253,254,254,254,254,254,
3178254,254,254,254,255,255,255,255,255,255,255,255,255,255,255};
3179
3180M4OSA_ERR M4MA_generateAudioGraphFile(JNIEnv* pEnv, M4OSA_Char* pInputFileURL,
3181 M4OSA_Char* pOutFileURL,
3182 M4OSA_UInt32 samplesPerValue,
3183 M4OSA_UInt32 channels,
3184 M4OSA_UInt32 frameDuration,
3185 ManualEditContext* pContext)
3186{
3187 M4OSA_ERR err;
3188 M4OSA_Context outFileHandle = M4OSA_NULL;
3189 M4OSA_Context inputFileHandle = M4OSA_NULL;
3190 M4AM_Buffer bufferIn = {0, 0};
3191 M4OSA_UInt32 peakVolumeDbValue = 0;
3192 M4OSA_UInt32 samplesCountInBytes= 0 , numBytesToRead = 0, index = 0;
3193 M4OSA_UInt32 writeCount = 0, samplesCountBigEndian = 0, volumeValuesCount = 0;
3194 M4OSA_Int32 seekPos = 0;
3195 M4OSA_UInt32 fileSize = 0;
3196 M4OSA_UInt32 totalBytesRead = 0;
3197 M4OSA_UInt32 prevProgress = 0;
3198 bool threadStarted = true;
3199
3200 int dbValue = 0;
3201 M4OSA_Int16 *ptr16 ;
3202
3203 jclass engineClass = pEnv->FindClass(MANUAL_EDIT_ENGINE_CLASS_NAME);
3204 videoEditJava_checkAndThrowIllegalStateException(&threadStarted, pEnv,
3205 (M4OSA_NULL == engineClass),
3206 "not initialized");
3207
3208 /* register the call back function pointer */
3209 pContext->onAudioGraphProgressUpdateMethodId =
3210 pEnv->GetMethodID(engineClass, "onAudioGraphExtractProgressUpdate", "(IZ)V");
3211
3212
3213 /* ENTER */
3214 VIDEOEDIT_LOG_API(ANDROID_LOG_INFO, "VIDEO_EDITOR", "ENTER - M4MA_generateAudioGraphFile");
3215 VIDEOEDIT_LOG_API(ANDROID_LOG_INFO, "VIDEO_EDITOR",
3216 "Audio Graph samplesPerValue %d channels %d", samplesPerValue, channels);
3217
3218 /******************************************************************************
3219 OPEN INPUT AND OUTPUT FILES
3220 *******************************************************************************/
3221 err = M4OSA_fileReadOpen (&inputFileHandle, pInputFileURL, M4OSA_kFileRead);
3222 if (inputFileHandle == M4OSA_NULL) {
3223 VIDEOEDIT_LOG_ERROR(ANDROID_LOG_INFO, "VIDEO_EDITOR",
James Dong581cad22011-01-19 11:35:06 -08003224 "M4MA_generateAudioGraphFile: Cannot open input file 0x%lx", err);
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08003225 return err;
3226 }
3227
3228 /* get the file size for progress */
3229 err = M4OSA_fileReadGetOption(inputFileHandle, M4OSA_kFileReadGetFileSize,
3230 (M4OSA_Void**)&fileSize);
3231 if ( err != M4NO_ERROR) {
3232 //LVMEL_LOG_ERROR("M4MA_generateAudioGraphFile : File write failed \n");
3233 jniThrowException(pEnv, "java/lang/IOException", "file size get option failed");
3234 //return -1;
3235 }
3236
3237 err = M4OSA_fileWriteOpen (&outFileHandle,(M4OSA_Char*) pOutFileURL,
3238 M4OSA_kFileCreate | M4OSA_kFileWrite);
3239 if (outFileHandle == M4OSA_NULL) {
3240 if (inputFileHandle != NULL)
3241 {
3242 M4OSA_fileReadClose(inputFileHandle);
3243 }
3244 return err;
3245 }
3246
3247 /******************************************************************************
3248 PROCESS THE SAMPLES
3249 *******************************************************************************/
3250 samplesCountInBytes = (samplesPerValue * sizeof(M4OSA_UInt16) * channels);
3251
Shyam Pallapothuc0125dd2011-04-21 19:13:56 -07003252 bufferIn.m_dataAddress = (M4OSA_UInt8*)M4OSA_32bitAlignedMalloc(samplesCountInBytes*sizeof(M4OSA_UInt16), 0,
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08003253 (M4OSA_Char*)"AudioGraph" );
3254 if ( bufferIn.m_dataAddress != M4OSA_NULL) {
3255 bufferIn.m_bufferSize = samplesCountInBytes*sizeof(M4OSA_UInt16);
3256 } else {
3257 VIDEOEDIT_LOG_ERROR(ANDROID_LOG_INFO, "VIDEO_EDITOR",
James Dong581cad22011-01-19 11:35:06 -08003258 "M4MA_generateAudioGraphFile: Malloc failed for bufferIn.m_dataAddress 0x%lx",
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08003259 M4ERR_ALLOC);
3260 return M4ERR_ALLOC;
3261 }
3262 /* sample to be converted to BIG endian ; store the frame duration */
3263 samplesCountBigEndian = ((frameDuration>>24)&0xff) | // move byte 3 to byte 0
3264 ((frameDuration<<8)&0xff0000) | // move byte 1 to byte 2
3265 ((frameDuration>>8)&0xff00) | // move byte 2 to byte 1
3266 ((frameDuration<<24)&0xff000000); // byte 0 to byte 3
3267
3268 /* write the samples per value supplied to out file */
3269 err = M4OSA_fileWriteData (outFileHandle, (M4OSA_MemAddr8)&samplesCountBigEndian,
3270 sizeof(M4OSA_UInt32) );
3271 if (err != M4NO_ERROR) {
3272 jniThrowException(pEnv, "java/lang/IOException", "file write failed");
3273 }
3274
3275
3276 /* write UIn32 value 0 for no of values as place holder */
3277 samplesCountBigEndian = 0; /* reusing local var */
3278 err = M4OSA_fileWriteData (outFileHandle, (M4OSA_MemAddr8)&samplesCountBigEndian,
3279 sizeof(M4OSA_UInt32) );
3280 if (err != M4NO_ERROR) {
3281 jniThrowException(pEnv, "java/lang/IOException", "file write failed");
3282 }
3283
3284 /* loop until EOF */
3285 do
3286 {
Shyam Pallapothu3feb41f2011-04-20 20:28:32 -07003287 memset((void *)bufferIn.m_dataAddress,0,bufferIn.m_bufferSize);
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08003288
3289 numBytesToRead = samplesCountInBytes;
3290
3291 err = M4OSA_fileReadData( inputFileHandle,
3292 (M4OSA_MemAddr8)bufferIn.m_dataAddress,
3293 &numBytesToRead );
3294
3295 if (err != M4NO_ERROR) {
3296 // if out value of bytes-read is 0, break
3297 if ( numBytesToRead == 0) {
James Dong581cad22011-01-19 11:35:06 -08003298 VIDEOEDIT_LOG_ERROR(ANDROID_LOG_INFO, "VIDEO_EDITOR", "numBytesToRead 0x%lx",
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08003299 numBytesToRead);
3300 break; /* stop if file is empty or EOF */
3301 }
3302 }
3303
3304 ptr16 = (M4OSA_Int16*)bufferIn.m_dataAddress;
3305
3306 peakVolumeDbValue = 0;
3307 index = 0;
3308
3309 // loop through half the lenght frame bytes read 'cause its 16 bits samples
3310 while (index < (numBytesToRead / 2)) {
3311 /* absolute values of 16 bit sample */
3312 if (ptr16[index] < 0) {
3313 ptr16[index] = -(ptr16[index]);
3314 }
3315 peakVolumeDbValue = (peakVolumeDbValue > (M4OSA_UInt32)ptr16[index] ?\
3316 peakVolumeDbValue : (M4OSA_UInt32)ptr16[index]);
3317 index++;
3318 }
3319
3320 // move 7 bits , ignore sign bit
3321 dbValue = (peakVolumeDbValue >> 7);
3322 dbValue = logLookUp[(M4OSA_UInt8)dbValue];
3323
3324 err = M4OSA_fileWriteData (outFileHandle, (M4OSA_MemAddr8)&dbValue, sizeof(M4OSA_UInt8) );
3325 if (err != M4NO_ERROR) {
3326 VIDEOEDIT_LOG_ERROR(ANDROID_LOG_INFO, "VIDEO_EDITOR",
3327 "M4MA_generateAudioGraphFile : File write failed");
3328 break;
3329 }
3330
3331 volumeValuesCount ++;
3332 totalBytesRead += numBytesToRead;
3333
3334 if ((((totalBytesRead*100)/fileSize)) != prevProgress) {
3335 if ( (pContext->threadProgress != prevProgress) && (prevProgress != 0 )) {
3336 //pContext->threadProgress = prevProgress;
3337 //onWveformProgressUpdateMethodId(prevProgress, 0);
3338 //LVME_callAudioGraphOnProgressUpdate(pContext, 0, prevProgress);
3339 pEnv->CallVoidMethod(pContext->engine,
3340 pContext->onAudioGraphProgressUpdateMethodId,
3341 prevProgress, 0);
3342 VIDEOEDIT_LOG_API(ANDROID_LOG_INFO, "VIDEO_EDITOR", "pContext->threadProgress %d",
3343 prevProgress);
3344 }
3345 }
3346 prevProgress = (((totalBytesRead*100)/fileSize));
3347
3348 } while (numBytesToRead != 0);
3349
James Dong581cad22011-01-19 11:35:06 -08003350 VIDEOEDIT_LOG_ERROR(ANDROID_LOG_INFO, "VIDEO_EDITOR", "loop 0x%lx", volumeValuesCount);
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08003351
3352 /* if some error occured in fwrite */
3353 if (numBytesToRead != 0) {
3354 //err = -1;
3355 jniThrowException(pEnv, "java/lang/IOException", "numBytesToRead != 0 ; file write failed");
3356 }
3357
3358 /* write the count in place holder after seek */
3359 seekPos = sizeof(M4OSA_UInt32);
3360 err = M4OSA_fileWriteSeek(outFileHandle, M4OSA_kFileSeekBeginning,
3361 &seekPos /* after samples per value */);
3362 if ( err != M4NO_ERROR) {
3363 jniThrowException(pEnv, "java/lang/IOException", "file seek failed");
3364 } else {
3365 volumeValuesCount = ((volumeValuesCount>>24)&0xff) | // move byte 3 to byte 0
3366 ((volumeValuesCount<<8)&0xff0000) | // move byte 1 to byte 2
3367 ((volumeValuesCount>>8)&0xff00) | // move byte 2 to byte 1
3368 ((volumeValuesCount<<24)&0xff000000); // byte 0 to byte 3
3369
3370 err = M4OSA_fileWriteData (outFileHandle, (M4OSA_MemAddr8)&volumeValuesCount,
3371 sizeof(M4OSA_UInt32) );
3372 if ( err != M4NO_ERROR) {
3373 jniThrowException(pEnv, "java/lang/IOException", "file write failed");
3374 }
3375 }
3376
3377 /******************************************************************************
3378 CLOSE AND FREE ALLOCATIONS
3379 *******************************************************************************/
Shyam Pallapothuc0125dd2011-04-21 19:13:56 -07003380 free(bufferIn.m_dataAddress);
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08003381 M4OSA_fileReadClose(inputFileHandle);
3382 M4OSA_fileWriteClose(outFileHandle);
3383 /* final finish callback */
3384 pEnv->CallVoidMethod(pContext->engine, pContext->onAudioGraphProgressUpdateMethodId, 100, 0);
3385
3386 /* EXIT */
3387 VIDEOEDIT_LOG_API(ANDROID_LOG_INFO, "VIDEO_EDITOR", "EXIT - M4MA_generateAudioGraphFile");
3388
3389 return err;
3390}
3391
3392static int videoEditor_generateAudioWaveFormSync (JNIEnv* pEnv, jobject thiz,
3393 jstring pcmfilePath,
3394 jstring outGraphfilePath,
3395 jint frameDuration, jint channels,
3396 jint samplesCount)
3397{
3398 M4OSA_ERR result = M4NO_ERROR;
3399 ManualEditContext* pContext = M4OSA_NULL;
3400 bool needToBeLoaded = true;
3401
3402 VIDEOEDIT_LOG_API(ANDROID_LOG_INFO, "VIDEO_EDITOR",
3403 "videoEditor_generateAudioWaveFormSync() ");
3404
3405 /* Get the context. */
3406 pContext = (ManualEditContext*)videoEditClasses_getContext(&needToBeLoaded, pEnv, thiz);
3407 if (pContext == M4OSA_NULL) {
3408 VIDEOEDIT_LOG_API(ANDROID_LOG_INFO, "VIDEO_EDITOR",
3409 "videoEditor_generateAudioWaveFormSync() - pContext is NULL ");
3410 }
3411
3412 VIDEOEDIT_LOG_API(ANDROID_LOG_INFO, "VIDEO_EDITOR",
3413 "videoEditor_generateAudioWaveFormSync Retrieving pStringOutAudioGraphFile");
3414
3415 const char *pPCMFilePath = pEnv->GetStringUTFChars(pcmfilePath, NULL);
3416 if (pPCMFilePath == M4OSA_NULL) {
3417 if (pEnv != NULL) {
3418 jniThrowException(pEnv, "java/lang/RuntimeException",
3419 "Input string PCMFilePath is null");
3420 }
3421 }
3422
3423 const char *pStringOutAudioGraphFile = pEnv->GetStringUTFChars(outGraphfilePath, NULL);
3424 if (pStringOutAudioGraphFile == M4OSA_NULL) {
3425 if (pEnv != NULL) {
3426 jniThrowException(pEnv, "java/lang/RuntimeException",
3427 "Input string outGraphfilePath is null");
3428 }
3429 }
3430
3431 VIDEOEDIT_LOG_API(ANDROID_LOG_INFO, "VIDEO_EDITOR",
3432 "videoEditor_generateAudioWaveFormSync Generate the waveform data %s %d %d %d",
3433 pStringOutAudioGraphFile, frameDuration, channels, samplesCount);
3434
3435 /* Generate the waveform */
3436 result = M4MA_generateAudioGraphFile(pEnv, (M4OSA_Char*) pPCMFilePath,
3437 (M4OSA_Char*) pStringOutAudioGraphFile,
3438 (M4OSA_UInt32) samplesCount,
3439 (M4OSA_UInt32) channels,
3440 (M4OSA_UInt32)frameDuration,
3441 pContext);
3442
3443 if (pStringOutAudioGraphFile != NULL) {
3444 pEnv->ReleaseStringUTFChars(outGraphfilePath, pStringOutAudioGraphFile);
3445 }
3446
Santosh Madhava91ba3a02011-02-09 19:25:17 -08003447 if (pPCMFilePath != NULL) {
3448 pEnv->ReleaseStringUTFChars(pcmfilePath, pPCMFilePath);
3449 }
3450
Dharmaray Kundargicd196d32011-01-16 15:47:16 -08003451 VIDEOEDIT_LOG_FUNCTION(ANDROID_LOG_INFO, "VIDEO_EDITOR",
3452 "videoEditor_generateAudioWaveFormSync pContext->bSkipState ");
3453
3454 return result;
3455}
3456
3457/******** End Audio Graph *******/
3458jint JNI_OnLoad(
3459 JavaVM* pVm,
3460 void* pReserved)
3461{
3462 void* pEnv = NULL;
3463 bool needToBeInitialized = true;
3464 jint result = -1;
3465
3466 VIDEOEDIT_LOG_FUNCTION(ANDROID_LOG_INFO, "VIDEO_EDITOR", "JNI_OnLoad()");
3467
3468 // Add a text marker (the condition must always be true).
3469 ADD_TEXT_MARKER_FUN(NULL != pVm)
3470
3471 // Check the JNI version.
3472 if (pVm->GetEnv(&pEnv, JNI_VERSION_1_4) == JNI_OK)
3473 {
3474 // Add a code marker (the condition must always be true).
3475 ADD_CODE_MARKER_FUN(NULL != pEnv)
3476
3477 // Register the manual edit JNI methods.
3478 if (videoEditor_registerManualEditMethods((JNIEnv*)pEnv) == 0)
3479 {
3480 // Initialize the classes.
3481 videoEditClasses_init(&needToBeInitialized, (JNIEnv*)pEnv);
3482 if (needToBeInitialized)
3483 {
3484 // Success, return valid version number.
3485 result = JNI_VERSION_1_4;
3486 }
3487 }
3488 }
3489
3490 // Return the result.
3491 return(result);
3492}
3493