VideoEditorMain.cpp revision 5618e037af1a9d4323990a580d4181b9bc8e3091
1/*
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>
44#include <M4OSA_FileExtra.h>
45#include <M4OSA_Memory.h>
46#include <M4OSA_String.h>
47#include <M4OSA_Thread.h>
48#include <M4xVSS_API.h>
49#include <M4VSS3GPP_ErrorCodes.h>
50#include <M4MCS_API.h>
51#include <M4MCS_ErrorCodes.h>
52#include <M4MDP_API.h>
53#include <M4READER_Common.h>
54#include <M4WRITER_common.h>
55};
56
57
58using namespace android;
59
60#define THREAD_STACK_SIZE       (65536)
61
62#define VIDEOEDITOR_VERSION_MAJOR     0
63#define VIDEOEDITOR_VERSION_MINOR     0
64#define VIDEOEDITOR_VERSION_REVISION  1
65
66
67typedef enum
68{
69    ManualEditState_NOT_INITIALIZED,
70    ManualEditState_INITIALIZED,
71    ManualEditState_ANALYZING,
72    ManualEditState_ANALYZING_ERROR,
73    ManualEditState_OPENED,
74    ManualEditState_SAVING,
75    ManualEditState_SAVING_ERROR,
76    ManualEditState_SAVED,
77    ManualEditState_STOPPING
78} ManualEditState;
79
80typedef struct
81{
82    JavaVM*                        pVM;
83    jobject                        engine;
84    jmethodID                      onCompletionMethodId;
85    jmethodID                      onErrorMethodId;
86    jmethodID                      onWarningMethodId;
87    jmethodID                      onProgressUpdateMethodId;
88    jmethodID                      onPreviewProgressUpdateMethodId;
89    jmethodID                      previewFrameEditInfoId;
90    M4xVSS_InitParams              initParams;
91    void*                          pTextRendererHandle;
92    M4xVSS_getTextRgbBufferFct     pTextRendererFunction;
93    M4OSA_Context                  engineContext;
94    ManualEditState                state;
95    M4VSS3GPP_EditSettings*        pEditSettings;
96    M4OSA_Context                  threadContext;
97    M4OSA_ERR                      threadResult;
98    M4OSA_UInt8                    threadProgress;
99    VideoEditorPreviewController   *mPreviewController;
100    M4xVSS_AudioMixingSettings     *mAudioSettings;
101    /* Audio Graph changes */
102    M4OSA_Context                   pAudioGraphMCSCtx;
103    M4OSA_Bool                      bSkipState;
104    jmethodID                       onAudioGraphProgressUpdateMethodId;
105    Mutex                           mLock;
106    bool                            mIsUpdateOverlay;
107    char                            *mOverlayFileName;
108    int                             mOverlayRenderingMode;
109} ManualEditContext;
110
111extern "C" M4OSA_ERR M4MCS_open_normalMode(
112                M4MCS_Context                       pContext,
113                M4OSA_Void*                         pFileIn,
114                M4VIDEOEDITING_FileType             InputFileType,
115                M4OSA_Void*                         pFileOut,
116                M4OSA_Void*                         pTempFile);
117
118static M4OSA_ERR videoEditor_toUTF8Fct(
119                M4OSA_Void*                         pBufferIn,
120                M4OSA_UInt8*                        pBufferOut,
121                M4OSA_UInt32*                       bufferOutSize);
122
123static M4OSA_ERR videoEditor_fromUTF8Fct(
124                M4OSA_UInt8*                        pBufferIn,
125                M4OSA_Void*                         pBufferOut,
126                M4OSA_UInt32*                       bufferOutSize);
127
128static M4OSA_ERR videoEditor_getTextRgbBufferFct(
129                M4OSA_Void*                         pRenderingData,
130                M4OSA_Void*                         pTextBuffer,
131                M4OSA_UInt32                        textBufferSize,
132                M4VIFI_ImagePlane**                 pOutputPlane);
133
134static void videoEditor_callOnProgressUpdate(
135                ManualEditContext*                  pContext,
136                int                                 task,
137                int                                 progress);
138
139static void videoEditor_freeContext(
140                JNIEnv*                             pEnv,
141                ManualEditContext**                 ppContext);
142
143static M4OSA_ERR videoEditor_threadProc(
144                M4OSA_Void*                         param);
145
146static jobject videoEditor_getVersion(
147                JNIEnv*                             pEnv,
148                jobject                             thiz);
149
150static void videoEditor_init(
151                JNIEnv*                             pEnv,
152                jobject                             thiz,
153                jstring                             tempPath,
154                jstring                             textRendererPath);
155
156static void videoEditor_loadSettings(
157                JNIEnv*                             pEnv,
158                jobject                             thiz,
159                jobject                             settings);
160
161static void videoEditor_unloadSettings(
162                JNIEnv*                             pEnv,
163                jobject                             thiz);
164
165
166static void videoEditor_stopEncoding(
167                JNIEnv*                             pEnv,
168                jobject                             thiz);
169
170static void videoEditor_release(
171                JNIEnv*                             pEnv,
172                jobject                             thiz);
173static int videoEditor_getPixels(
174                                 JNIEnv*                  env,
175                                 jobject                  thiz,
176                                 jstring                  path,
177                                 jintArray                pixelArray,
178                                 M4OSA_UInt32             width,
179                                 M4OSA_UInt32             height,
180                                 M4OSA_UInt32             timeMS);
181static int videoEditor_getPixelsList(
182                                     JNIEnv*                  env,
183                                     jobject                  thiz,
184                                     jstring                  path,
185                                     jintArray                pixelArray,
186                                     M4OSA_UInt32             width,
187                                     M4OSA_UInt32             height,
188                                     M4OSA_UInt32             deltatimeMS,
189                                     M4OSA_UInt32             noOfThumbnails,
190                                     M4OSA_UInt32             startTime,
191                                     M4OSA_UInt32             endTime);
192
193static void
194videoEditor_startPreview(
195                JNIEnv*                 pEnv,
196                jobject                 thiz,
197                jobject                 mSurface,
198                jlong                   fromMs,
199                jlong                   toMs,
200                jint                    callbackInterval,
201                jboolean                loop);
202
203static void
204videoEditor_populateSettings(
205                JNIEnv*                 pEnv,
206                jobject                 thiz,
207                jobject                 settings,
208                jobject                 object,
209                jobject                 audioSettingObject);
210
211static void videoEditor_stopPreview(JNIEnv*  pEnv,
212                              jobject  thiz);
213
214static jobject
215videoEditor_getProperties(
216                JNIEnv*                             pEnv,
217                jobject                             thiz,
218                jstring                             file);
219
220static int videoEditor_renderPreviewFrame(JNIEnv* pEnv,
221                                    jobject thiz,
222                                    jobject    mSurface,
223                                    jlong fromMs,
224                                    jint  surfaceWidth,
225                                    jint  surfaceHeight);
226
227static int videoEditor_registerManualEditMethods(
228                JNIEnv*                             pEnv);
229
230static void jniPreviewProgressCallback(void* cookie, M4OSA_UInt32 msgType,
231                                        void *argc);
232
233static int videoEditor_renderMediaItemPreviewFrame(JNIEnv* pEnv,
234                                                    jobject thiz,
235                                                    jobject mSurface,
236                                                    jstring filePath,
237                                                    jint frameWidth,
238                                                    jint frameHeight,
239                                                    jint surfaceWidth,
240                                                    jint surfaceHeight,
241                                                    jlong fromMs);
242
243static int videoEditor_generateAudioWaveFormSync ( JNIEnv*     pEnv,
244                                                  jobject     thiz,
245                                                  jstring     pcmfilePath,
246                                                  jstring     outGraphfilePath,
247                                                  jint        frameDuration,
248                                                  jint        channels,
249                                                  jint        samplesCount);
250
251static int videoEditor_generateAudioRawFile(JNIEnv* pEnv,
252                                    jobject thiz,
253                                    jstring infilePath,
254                                    jstring pcmfilePath );
255
256M4OSA_ERR videoEditor_generateAudio(JNIEnv* pEnv,ManualEditContext* pContext,
257                                    M4OSA_Char* infilePath,
258                                    M4OSA_Char* pcmfilePath );
259
260static int
261videoEditor_generateClip(
262                JNIEnv*                             pEnv,
263                jobject                             thiz,
264                jobject                             settings);
265
266static void videoEditor_clearSurface(JNIEnv* pEnv,
267                                    jobject thiz,
268                                    jobject surface);
269
270static JNINativeMethod gManualEditMethods[] = {
271    {"getVersion",               "()L"VERSION_CLASS_NAME";",
272                                (void *)videoEditor_getVersion      },
273    {"_init",                    "(Ljava/lang/String;Ljava/lang/String;)V",
274                                (void *)videoEditor_init    },
275    {"nativeStartPreview",       "(Landroid/view/Surface;JJIZ)V",
276                                (void *)videoEditor_startPreview    },
277    {"nativePopulateSettings",
278            "(L"EDIT_SETTINGS_CLASS_NAME";L"PREVIEW_PROPERTIES_CLASS_NAME";L"
279            AUDIO_SETTINGS_CLASS_NAME";)V",
280                                (void *)videoEditor_populateSettings    },
281    {"nativeRenderPreviewFrame", "(Landroid/view/Surface;JII)I",
282                                (int *)videoEditor_renderPreviewFrame     },
283    {"nativeRenderMediaItemPreviewFrame",
284    "(Landroid/view/Surface;Ljava/lang/String;IIIIJ)I",
285                        (int *)videoEditor_renderMediaItemPreviewFrame     },
286    {"nativeStopPreview",       "()V",
287                                (void *)videoEditor_stopPreview            },
288    {"stopEncoding",            "()V",
289                                (void *)videoEditor_stopEncoding         },
290    {"release",                 "()V",
291                                (void *)videoEditor_release            },
292    {"nativeGetPixels",         "(Ljava/lang/String;[IIIJ)I",
293                                (void*)videoEditor_getPixels               },
294    {"nativeGetPixelsList",     "(Ljava/lang/String;[IIIIIJJ)I",
295                                (void*)videoEditor_getPixelsList           },
296    {"getMediaProperties",
297    "(Ljava/lang/String;)Landroid/media/videoeditor/MediaArtistNativeHelper$Properties;",
298                                (void *)videoEditor_getProperties          },
299    {"nativeGenerateAudioGraph","(Ljava/lang/String;Ljava/lang/String;III)I",
300                                (int *)videoEditor_generateAudioWaveFormSync },
301    {"nativeGenerateRawAudio",  "(Ljava/lang/String;Ljava/lang/String;)I",
302                                (int *)videoEditor_generateAudioRawFile      },
303    {"nativeGenerateClip",      "(L"EDIT_SETTINGS_CLASS_NAME";)I",
304                                (void *)videoEditor_generateClip  },
305    {"nativeClearSurface",       "(Landroid/view/Surface;)V",
306                                (void *)videoEditor_clearSurface  },
307};
308
309// temp file name of VSS out file
310#define TEMP_MCS_OUT_FILE_PATH "tmpOut.3gp"
311
312void
313getClipSetting(
314                JNIEnv*                                       pEnv,
315                jobject                                       object,
316                M4VSS3GPP_ClipSettings*                       pSettings)
317{
318
319    jfieldID fid;
320    int field = 0;
321    bool needToBeLoaded = true;
322    jclass clazz = pEnv->FindClass(PROPERTIES_CLASS_NAME);
323
324    videoEditJava_checkAndThrowIllegalStateException(&needToBeLoaded, pEnv,
325                                             (M4OSA_NULL == clazz),
326                                             "not initialized");
327
328    fid = pEnv->GetFieldID(clazz,"duration","I");
329    pSettings->ClipProperties.uiClipDuration = pEnv->GetIntField(object,fid);
330    M4OSA_TRACE1_1("duration = %d",pSettings->ClipProperties.uiClipDuration);
331
332    fid = pEnv->GetFieldID(clazz,"videoFormat","I");
333    pSettings->ClipProperties.VideoStreamType =
334        (M4VIDEOEDITING_VideoFormat)pEnv->GetIntField(object,fid);
335    M4OSA_TRACE1_1("videoFormat = %d",pSettings->ClipProperties.VideoStreamType);
336
337    fid = pEnv->GetFieldID(clazz,"videoDuration","I");
338    pSettings->ClipProperties.uiClipVideoDuration = pEnv->GetIntField(object,fid);
339    M4OSA_TRACE1_1("videoDuration = %d",
340                    pSettings->ClipProperties.uiClipVideoDuration);
341
342    fid = pEnv->GetFieldID(clazz,"width","I");
343    pSettings->ClipProperties.uiVideoWidth = pEnv->GetIntField(object,fid);
344    M4OSA_TRACE1_1("width = %d",pSettings->ClipProperties.uiVideoWidth);
345
346    fid = pEnv->GetFieldID(clazz,"height","I");
347    pSettings->ClipProperties.uiVideoHeight = pEnv->GetIntField(object,fid);
348    M4OSA_TRACE1_1("height = %d",pSettings->ClipProperties.uiVideoHeight);
349
350    fid = pEnv->GetFieldID(clazz,"audioFormat","I");
351    pSettings->ClipProperties.AudioStreamType =
352        (M4VIDEOEDITING_AudioFormat)pEnv->GetIntField(object,fid);
353    M4OSA_TRACE1_1("audioFormat = %d",pSettings->ClipProperties.AudioStreamType);
354
355    fid = pEnv->GetFieldID(clazz,"audioDuration","I");
356    pSettings->ClipProperties.uiClipAudioDuration = pEnv->GetIntField(object,fid);
357    M4OSA_TRACE1_1("audioDuration = %d",
358                    pSettings->ClipProperties.uiClipAudioDuration);
359
360    fid = pEnv->GetFieldID(clazz,"audioBitrate","I");
361    pSettings->ClipProperties.uiAudioBitrate = pEnv->GetIntField(object,fid);
362    M4OSA_TRACE1_1("audioBitrate = %d",pSettings->ClipProperties.uiAudioBitrate);
363
364    fid = pEnv->GetFieldID(clazz,"audioChannels","I");
365    pSettings->ClipProperties.uiNbChannels = pEnv->GetIntField(object,fid);
366    M4OSA_TRACE1_1("audioChannels = %d",pSettings->ClipProperties.uiNbChannels);
367
368    fid = pEnv->GetFieldID(clazz,"audioSamplingFrequency","I");
369    pSettings->ClipProperties.uiSamplingFrequency = pEnv->GetIntField(object,fid);
370    M4OSA_TRACE1_1("audioSamplingFrequency = %d",
371                    pSettings->ClipProperties.uiSamplingFrequency);
372
373   fid = pEnv->GetFieldID(clazz,"audioVolumeValue","I");
374   pSettings->ClipProperties.uiClipAudioVolumePercentage =
375                    pEnv->GetIntField(object,fid);
376   M4OSA_TRACE1_1("audioVolumeValue = %d",
377                    pSettings->ClipProperties.uiClipAudioVolumePercentage);
378}
379
380static void jniPreviewProgressCallback (void* cookie, M4OSA_UInt32 msgType,
381                                        void *argc)
382{
383    ManualEditContext *pContext = (ManualEditContext *)cookie;
384    JNIEnv*     pEnv = NULL;
385    bool        isFinished = false;
386    int         currentMs = 0;
387    int         error = M4NO_ERROR;
388    bool        isUpdateOverlay = false;
389    int         overlayEffectIndex;
390    char        *extPos;
391    bool        isSendProgress = true;
392    jstring     tmpFileName;
393    VideoEditorCurretEditInfo *pCurrEditInfo;
394
395    // Attach the current thread.
396    pContext->pVM->AttachCurrentThread(&pEnv, NULL);
397    switch(msgType)
398    {
399        case MSG_TYPE_PROGRESS_INDICATION:
400            currentMs = *(int*)argc;
401            break;
402        case MSG_TYPE_PLAYER_ERROR:
403            currentMs = -1;
404            error = *(int*)argc;
405            break;
406        case MSG_TYPE_PREVIEW_END:
407            isFinished = true;
408            break;
409        case MSG_TYPE_OVERLAY_UPDATE:
410        {
411            int overlayFileNameLen = 0;
412            isSendProgress = false;
413            pContext->mIsUpdateOverlay = true;
414            pCurrEditInfo = (VideoEditorCurretEditInfo*)argc;
415            overlayEffectIndex = pCurrEditInfo->overlaySettingsIndex;
416            LOGV("MSG_TYPE_OVERLAY_UPDATE");
417
418            if (pContext->mOverlayFileName != NULL) {
419                M4OSA_free((M4OSA_MemAddr32)pContext->mOverlayFileName);
420                pContext->mOverlayFileName = NULL;
421            }
422
423            overlayFileNameLen =
424                strlen((const char*)pContext->pEditSettings->Effects[overlayEffectIndex].xVSS.pFramingFilePath);
425
426            pContext->mOverlayFileName =
427                (char*)M4OSA_malloc(overlayFileNameLen+1,
428                                    M4VS, (M4OSA_Char*)"videoEdito JNI overlayFile");
429            if (pContext->mOverlayFileName != NULL) {
430                strncpy (pContext->mOverlayFileName,
431                    (const char*)pContext->pEditSettings->\
432                    Effects[overlayEffectIndex].xVSS.pFramingFilePath, overlayFileNameLen);
433                //Change the name to png file
434                extPos = strstr(pContext->mOverlayFileName, ".rgb");
435                if (extPos != NULL) {
436                    *extPos = '\0';
437                } else {
438                    LOGE("ERROR the overlay file is incorrect");
439                }
440
441                strcat(pContext->mOverlayFileName, ".png");
442                LOGV("Conv string is %s", pContext->mOverlayFileName);
443                LOGV("Current Clip index = %d", pCurrEditInfo->clipIndex);
444
445                pContext->mOverlayRenderingMode = pContext->pEditSettings->\
446                         pClipList[pCurrEditInfo->clipIndex]->xVSS.MediaRendering;
447                LOGI("rendering mode %d ", pContext->mOverlayRenderingMode);
448
449            }
450
451            break;
452        }
453
454        case MSG_TYPE_OVERLAY_CLEAR:
455            isSendProgress = false;
456            pContext->mOverlayFileName = NULL;
457            LOGI("MSG_TYPE_OVERLAY_CLEAR");
458            //argc is not used
459            pContext->mIsUpdateOverlay = true;
460            break;
461        default:
462            break;
463    }
464
465    if (isSendProgress) {
466        tmpFileName  = pEnv->NewStringUTF(pContext->mOverlayFileName);
467        pEnv->CallVoidMethod(pContext->engine,
468                pContext->onPreviewProgressUpdateMethodId,
469                currentMs,isFinished, pContext->mIsUpdateOverlay,
470                tmpFileName, pContext->mOverlayRenderingMode);
471
472        if (pContext->mIsUpdateOverlay) {
473            pContext->mIsUpdateOverlay = false;
474        }
475
476        if (tmpFileName) {
477            pEnv->DeleteLocalRef(tmpFileName);
478        }
479    }
480
481    // Detach the current thread.
482    pContext->pVM->DetachCurrentThread();
483
484}
485static void videoEditor_stopPreview(JNIEnv*  pEnv,
486                              jobject  thiz)
487{
488    ManualEditContext* pContext = M4OSA_NULL;
489    bool needToBeLoaded = true;
490    // Get the context.
491    pContext =
492            (ManualEditContext*)videoEditClasses_getContext(&needToBeLoaded, pEnv, thiz);
493
494    // Make sure that the context was set.
495    videoEditJava_checkAndThrowIllegalStateException(&needToBeLoaded, pEnv,
496                                             (M4OSA_NULL == pContext),
497                                             "not initialized");
498    pContext->mPreviewController->stopPreview();
499
500    if (pContext->mOverlayFileName != NULL) {
501        M4OSA_free((M4OSA_MemAddr32)pContext->mOverlayFileName);
502        pContext->mOverlayFileName = NULL;
503    }
504}
505
506static void videoEditor_clearSurface(JNIEnv* pEnv,
507                                    jobject thiz,
508                                    jobject surface)
509{
510    bool needToBeLoaded = true;
511    M4OSA_ERR result = M4NO_ERROR;
512    VideoEditor_renderPreviewFrameStr frameStr;
513    const char* pMessage = NULL;
514    // Let the size be QVGA
515    int width = 320;
516    int height = 240;
517    ManualEditContext* pContext = M4OSA_NULL;
518
519    // Get the context.
520    pContext = (ManualEditContext*)videoEditClasses_getContext(&needToBeLoaded, pEnv, thiz);
521    VIDEOEDIT_LOG_FUNCTION(ANDROID_LOG_INFO,
522                                "VIDEO_EDITOR","pContext = 0x%x",pContext);
523
524    // Make sure that the context was set.
525    videoEditJava_checkAndThrowIllegalStateException(&needToBeLoaded, pEnv,
526                                             (M4OSA_NULL == pContext),
527                                             "not initialized");
528
529    // Make sure that the context was set.
530    videoEditJava_checkAndThrowIllegalStateException(&needToBeLoaded, pEnv,
531                                 (M4OSA_NULL == pContext->mPreviewController),
532                                 "not initialized");
533
534    // Validate the surface parameter.
535    videoEditJava_checkAndThrowIllegalArgumentException(&needToBeLoaded, pEnv,
536                                                (NULL == surface),
537                                                "surface is null");
538
539    jclass surfaceClass = pEnv->FindClass("android/view/Surface");
540    videoEditJava_checkAndThrowIllegalStateException(&needToBeLoaded, pEnv,
541                                             (M4OSA_NULL == surfaceClass),
542                                             "not initialized");
543
544    jfieldID surface_native =
545            pEnv->GetFieldID(surfaceClass, ANDROID_VIEW_SURFACE_JNI_ID, "I");
546    videoEditJava_checkAndThrowIllegalStateException(&needToBeLoaded, pEnv,
547                                             (M4OSA_NULL == surface_native),
548                                             "not initialized");
549
550    Surface* const p = (Surface*)pEnv->GetIntField(surface, surface_native);
551    sp<Surface> previewSurface = sp<Surface>(p);
552
553    frameStr.pBuffer = M4OSA_NULL;
554    frameStr.timeMs = 0;
555    frameStr.uiSurfaceWidth = width;
556    frameStr.uiSurfaceHeight = height;
557    frameStr.uiFrameWidth = width;
558    frameStr.uiFrameHeight = height;
559    frameStr.bApplyEffect = M4OSA_FALSE;
560    frameStr.clipBeginCutTime = 0;
561    frameStr.clipEndCutTime = 0;
562
563    result = pContext->mPreviewController->clearSurface(previewSurface,
564                                                              &frameStr);
565    videoEditJava_checkAndThrowRuntimeException(&needToBeLoaded, pEnv,
566            (M4NO_ERROR != result), result);
567
568  }
569
570static int videoEditor_renderPreviewFrame(JNIEnv* pEnv,
571                                    jobject thiz,
572                                    jobject    mSurface,
573                                    jlong fromMs,
574                                    jint surfaceWidth,
575                                    jint surfaceHeight )
576{
577    bool needToBeLoaded = true;
578    M4OSA_ERR result = M4NO_ERROR;
579    M4OSA_UInt32 timeMs = (M4OSA_UInt32)fromMs;
580    M4OSA_UInt32 i=0,tnTimeMs = 0, framesizeYuv =0;
581    M4VIFI_UInt8 *pixelArray = M4OSA_NULL;
582    M4OSA_UInt32    iCurrentClipIndex = 0, uiNumberOfClipsInStoryBoard =0,
583                    uiClipDuration = 0, uiTotalClipDuration = 0,
584                    iIncrementedDuration = 0;
585    VideoEditor_renderPreviewFrameStr frameStr;
586    M4OSA_Context tnContext = M4OSA_NULL;
587    const char* pMessage = NULL;
588    M4VIFI_ImagePlane *yuvPlane = NULL;
589    VideoEditorCurretEditInfo  currEditInfo;
590
591    VIDEOEDIT_LOG_FUNCTION(ANDROID_LOG_INFO,
592        "VIDEO_EDITOR", "surfaceWidth = %d",surfaceWidth);
593    VIDEOEDIT_LOG_FUNCTION(ANDROID_LOG_INFO,
594        "VIDEO_EDITOR", "surfaceHeight = %d",surfaceHeight);
595    ManualEditContext* pContext = M4OSA_NULL;
596    // Get the context.
597    pContext =
598            (ManualEditContext*)videoEditClasses_getContext(&needToBeLoaded, pEnv, thiz);
599    VIDEOEDIT_LOG_FUNCTION(ANDROID_LOG_INFO,
600                                "VIDEO_EDITOR","pContext = 0x%x",pContext);
601
602    // Make sure that the context was set.
603    videoEditJava_checkAndThrowIllegalStateException(&needToBeLoaded, pEnv,
604                                             (M4OSA_NULL == pContext),
605                                             "not initialized");
606
607    // Make sure that the context was set.
608    videoEditJava_checkAndThrowIllegalStateException(&needToBeLoaded, pEnv,
609                                 (M4OSA_NULL == pContext->mPreviewController),
610                                 "not initialized");
611
612    // Validate the mSurface parameter.
613    videoEditJava_checkAndThrowIllegalArgumentException(&needToBeLoaded, pEnv,
614                                                (NULL == mSurface),
615                                                "mSurface is null");
616    jclass surfaceClass = pEnv->FindClass("android/view/Surface");
617    videoEditJava_checkAndThrowIllegalStateException(&needToBeLoaded, pEnv,
618                                             (M4OSA_NULL == surfaceClass),
619                                             "not initialized");
620
621    jfieldID surface_native =
622            pEnv->GetFieldID(surfaceClass, ANDROID_VIEW_SURFACE_JNI_ID, "I");
623    videoEditJava_checkAndThrowIllegalStateException(&needToBeLoaded, pEnv,
624                                             (M4OSA_NULL == surface_native),
625                                             "not initialized");
626
627    Surface* const p = (Surface*)pEnv->GetIntField(mSurface, surface_native);
628    sp<Surface> previewSurface = sp<Surface>(p);
629
630    /* Determine the total number of clips, total duration*/
631    uiNumberOfClipsInStoryBoard = pContext->pEditSettings->uiClipNumber;
632
633    for (i = 0; i < uiNumberOfClipsInStoryBoard; i++) {
634        uiClipDuration = pContext->pEditSettings->pClipList[i]->uiEndCutTime -
635            pContext->pEditSettings->pClipList[i]->uiBeginCutTime;
636        uiTotalClipDuration += uiClipDuration;
637    }
638
639    /* determine the clip whose thumbnail needs to be rendered*/
640    if (timeMs == 0) {
641        iCurrentClipIndex = 0;
642        i=0;
643    } else {
644        VIDEOEDIT_LOG_FUNCTION(ANDROID_LOG_INFO, "VIDEO_EDITOR",
645            "videoEditor_renderPreviewFrame() timeMs=%d", timeMs);
646
647        if (timeMs > uiTotalClipDuration) {
648            VIDEOEDIT_LOG_FUNCTION(ANDROID_LOG_INFO, "VIDEO_EDITOR",
649                "videoEditor_renderPreviewFrame() timeMs > uiTotalClipDuration");
650            pMessage = videoEditJava_getErrorName(M4ERR_PARAMETER);
651            jniThrowException(pEnv, "java/lang/IllegalArgumentException", pMessage);
652            return -1;
653        }
654
655        for (i = 0; i < uiNumberOfClipsInStoryBoard; i++) {
656            if (timeMs < (iIncrementedDuration +
657                          (pContext->pEditSettings->pClipList[i]->uiEndCutTime -
658                           pContext->pEditSettings->pClipList[i]->uiBeginCutTime)))
659            {
660                iCurrentClipIndex = i;
661                VIDEOEDIT_LOG_FUNCTION(ANDROID_LOG_INFO, "VIDEO_EDITOR",
662                    "videoEditor_renderPreviewFrame() iCurrentClipIndex=%d for timeMs=%d",
663                    iCurrentClipIndex, timeMs);
664                break;
665            }
666            else {
667                iIncrementedDuration = iIncrementedDuration +
668                    (pContext->pEditSettings->pClipList[i]->uiEndCutTime -
669                    pContext->pEditSettings->pClipList[i]->uiBeginCutTime);
670            }
671        }
672    }
673    /* If timestamp is beyond story board duration, return*/
674    if (i >= uiNumberOfClipsInStoryBoard) {
675        if (timeMs == iIncrementedDuration) {
676            iCurrentClipIndex = i-1;
677        } else {
678           return -1;
679        }
680    }
681
682    /*+ Handle the image files here */
683      if (pContext->pEditSettings->pClipList[iCurrentClipIndex]->FileType ==
684          /*M4VIDEOEDITING_kFileType_JPG*/ M4VIDEOEDITING_kFileType_ARGB8888 ) {
685          VIDEOEDIT_LOG_FUNCTION(ANDROID_LOG_INFO, "VIDEO_EDITOR", " iCurrentClipIndex %d ", iCurrentClipIndex);
686          VIDEOEDIT_LOG_FUNCTION(ANDROID_LOG_INFO, "VIDEO_EDITOR",
687                "  Height = %d",
688                pContext->pEditSettings->pClipList[iCurrentClipIndex]->ClipProperties.uiVideoHeight);
689
690          VIDEOEDIT_LOG_FUNCTION(ANDROID_LOG_INFO, "VIDEO_EDITOR",
691                "  Width = %d",
692                pContext->pEditSettings->pClipList[iCurrentClipIndex]->ClipProperties.uiVideoWidth);
693
694          LvGetImageThumbNail((const char *)pContext->pEditSettings->\
695          pClipList[iCurrentClipIndex]->pFile,
696            pContext->pEditSettings->pClipList[iCurrentClipIndex]->ClipProperties.uiVideoHeight,
697            pContext->pEditSettings->pClipList[iCurrentClipIndex]->ClipProperties.uiVideoWidth,
698            (M4OSA_Void **)&frameStr.pBuffer);
699    } else {
700        /* Handle 3gp/mp4 Clips here */
701        /* get thumbnail*/
702        result = ThumbnailOpen(&tnContext,
703            (const M4OSA_Char*)pContext->pEditSettings->\
704            pClipList[iCurrentClipIndex]->pFile, M4OSA_TRUE);
705        if (result != M4NO_ERROR || tnContext  == M4OSA_NULL) {
706            return -1;
707        }
708
709        /* timeMs is relative to storyboard; in this api it shud be relative to this clip */
710        if ((i >= uiNumberOfClipsInStoryBoard) &&
711            (timeMs == iIncrementedDuration)) {
712            tnTimeMs = pContext->pEditSettings->\
713            pClipList[iCurrentClipIndex]->uiEndCutTime;
714        } else {
715            tnTimeMs = pContext->pEditSettings->\
716            pClipList[iCurrentClipIndex]->uiBeginCutTime
717            + (timeMs - iIncrementedDuration);
718        }
719
720        VIDEOEDIT_LOG_API(ANDROID_LOG_INFO, "VIDEO_EDITOR",
721            "video width = %d",pContext->pEditSettings->pClipList[iCurrentClipIndex]->\
722            ClipProperties.uiVideoWidth);
723        VIDEOEDIT_LOG_API(ANDROID_LOG_INFO, "VIDEO_EDITOR",
724            "video height = %d",pContext->pEditSettings->pClipList[iCurrentClipIndex]->\
725            ClipProperties.uiVideoHeight);
726        VIDEOEDIT_LOG_API(ANDROID_LOG_INFO, "VIDEO_EDITOR",
727            "current clip index = %d",iCurrentClipIndex);
728
729        M4OSA_UInt32 width = pContext->pEditSettings->pClipList[iCurrentClipIndex]->\
730            ClipProperties.uiVideoWidth;
731        M4OSA_UInt32 height = pContext->pEditSettings->pClipList[iCurrentClipIndex]->\
732            ClipProperties.uiVideoHeight;
733
734        framesizeYuv = width * height * 1.5;
735
736        pixelArray = (M4VIFI_UInt8 *)M4OSA_malloc(framesizeYuv, M4VS,
737            (M4OSA_Char*)"videoEditor pixelArray");
738        if (pixelArray == M4OSA_NULL) {
739            VIDEOEDIT_LOG_FUNCTION(ANDROID_LOG_INFO, "VIDEO_EDITOR",
740                "videoEditor_renderPreviewFrame() malloc error");
741            ThumbnailClose(tnContext);
742            pMessage = videoEditJava_getErrorName(M4ERR_ALLOC);
743            jniThrowException(pEnv, "java/lang/RuntimeException", pMessage);
744            return -1;
745        }
746
747        result = ThumbnailGetPixels16(tnContext, (M4OSA_Int16 *)pixelArray,
748            pContext->pEditSettings->pClipList[iCurrentClipIndex]->\
749            ClipProperties.uiVideoWidth,
750            pContext->pEditSettings->pClipList[iCurrentClipIndex]->\
751            ClipProperties.uiVideoHeight,
752            &tnTimeMs);
753        if (result != M4NO_ERROR) {
754            M4OSA_free((M4OSA_MemAddr32)pixelArray);
755            ThumbnailClose(tnContext);
756            return -1;
757        }
758
759        ThumbnailClose(tnContext);
760        tnContext = M4OSA_NULL;
761
762#ifdef DUMPTOFILE
763        {
764            M4OSA_Context fileContext;
765            M4OSA_Char* fileName = (M4OSA_Char*)"/mnt/sdcard/FirstRGB565.raw";
766            M4OSA_fileExtraDelete((const M4OSA_Char *)fileName);
767            M4OSA_fileWriteOpen(&fileContext, (M4OSA_Void*) fileName,\
768                M4OSA_kFileWrite|M4OSA_kFileCreate);
769            M4OSA_fileWriteData(fileContext, (M4OSA_MemAddr8) pixelArray,
770                framesizeYuv);
771            M4OSA_fileWriteClose(fileContext);
772        }
773#endif
774
775        /**
776        * Allocate output YUV planes
777        */
778        yuvPlane = (M4VIFI_ImagePlane*)M4OSA_malloc(3*sizeof(M4VIFI_ImagePlane), M4VS,
779            (M4OSA_Char*)"videoEditor_renderPreviewFrame Output plane YUV");
780        if (yuvPlane == M4OSA_NULL) {
781            VIDEOEDIT_LOG_FUNCTION(ANDROID_LOG_INFO, "VIDEO_EDITOR",
782                "videoEditor_renderPreviewFrame() malloc error for yuv plane");
783            M4OSA_free((M4OSA_MemAddr32)pixelArray);
784            pMessage = videoEditJava_getErrorName(M4ERR_ALLOC);
785            jniThrowException(pEnv, "java/lang/RuntimeException", pMessage);
786            return -1;
787        }
788
789        yuvPlane[0].u_width = width;
790        yuvPlane[0].u_height = height;
791        yuvPlane[0].u_topleft = 0;
792        yuvPlane[0].u_stride = width;
793        yuvPlane[0].pac_data = (M4VIFI_UInt8*)pixelArray;
794
795        yuvPlane[1].u_width = width>>1;
796        yuvPlane[1].u_height = height>>1;
797        yuvPlane[1].u_topleft = 0;
798        yuvPlane[1].u_stride = width>>1;
799        yuvPlane[1].pac_data = yuvPlane[0].pac_data
800                    + yuvPlane[0].u_width * yuvPlane[0].u_height;
801        yuvPlane[2].u_width = (width)>>1;
802        yuvPlane[2].u_height = (height)>>1;
803        yuvPlane[2].u_topleft = 0;
804        yuvPlane[2].u_stride = (width)>>1;
805        yuvPlane[2].pac_data = yuvPlane[1].pac_data
806                    + yuvPlane[1].u_width * yuvPlane[1].u_height;
807
808#ifdef DUMPTOFILE
809        {
810            M4OSA_Context fileContext;
811            M4OSA_Char* fileName = (M4OSA_Char*)"/mnt/sdcard/ConvertedYuv.yuv";
812            M4OSA_fileExtraDelete((const M4OSA_Char *)fileName);
813            M4OSA_fileWriteOpen(&fileContext, (M4OSA_Void*) fileName,\
814                M4OSA_kFileWrite|M4OSA_kFileCreate);
815            M4OSA_fileWriteData(fileContext,
816                (M4OSA_MemAddr8) yuvPlane[0].pac_data, framesizeYuv);
817            M4OSA_fileWriteClose(fileContext);
818        }
819#endif
820
821        /* Fill up the render structure*/
822        frameStr.pBuffer = (M4OSA_Void*)yuvPlane[0].pac_data;
823    }
824
825    frameStr.timeMs = timeMs;    /* timestamp on storyboard*/
826    frameStr.uiSurfaceWidth =
827        pContext->pEditSettings->pClipList[iCurrentClipIndex]->\
828        ClipProperties.uiVideoWidth;
829    frameStr.uiSurfaceHeight =
830        pContext->pEditSettings->pClipList[iCurrentClipIndex]->\
831        ClipProperties.uiVideoHeight;
832    frameStr.uiFrameWidth =
833        pContext->pEditSettings->pClipList[iCurrentClipIndex]->\
834        ClipProperties.uiVideoWidth;
835    frameStr.uiFrameHeight =
836        pContext->pEditSettings->pClipList[iCurrentClipIndex]->\
837        ClipProperties.uiVideoHeight;
838    if (pContext->pEditSettings->nbEffects > 0) {
839        frameStr.bApplyEffect = M4OSA_TRUE;
840    } else {
841        frameStr.bApplyEffect = M4OSA_FALSE;
842    }
843    frameStr.clipBeginCutTime = iIncrementedDuration;
844    frameStr.clipEndCutTime =
845        iIncrementedDuration +
846        (pContext->pEditSettings->pClipList[iCurrentClipIndex]->uiEndCutTime -\
847        pContext->pEditSettings->pClipList[iCurrentClipIndex]->uiBeginCutTime);
848
849    pContext->mPreviewController->setPreviewFrameRenderingMode(
850        pContext->pEditSettings->\
851        pClipList[iCurrentClipIndex]->xVSS.MediaRendering,
852        pContext->pEditSettings->xVSS.outputVideoSize);
853    result = pContext->mPreviewController->renderPreviewFrame(previewSurface,
854                                                              &frameStr, &currEditInfo);
855
856    if (currEditInfo.overlaySettingsIndex != -1) {
857        char tmpOverlayFilename[100];
858        char *extPos = NULL;
859        jstring tmpOverlayString;
860        int tmpRenderingMode = 0;
861
862        strncpy (tmpOverlayFilename,
863                (const char*)pContext->pEditSettings->Effects[currEditInfo.overlaySettingsIndex].xVSS.pFramingFilePath, 99);
864
865        //Change the name to png file
866        extPos = strstr(tmpOverlayFilename, ".rgb");
867        if (extPos != NULL) {
868            *extPos = '\0';
869        } else {
870            LOGE("ERROR the overlay file is incorrect");
871        }
872
873        strcat(tmpOverlayFilename, ".png");
874
875        tmpRenderingMode = pContext->pEditSettings->pClipList[iCurrentClipIndex]->xVSS.MediaRendering;
876        tmpOverlayString = pEnv->NewStringUTF(tmpOverlayFilename);
877        pEnv->CallVoidMethod(pContext->engine,
878            pContext->previewFrameEditInfoId,
879            tmpOverlayString, tmpRenderingMode);
880
881    }
882
883    videoEditJava_checkAndThrowRuntimeException(&needToBeLoaded, pEnv,
884            (M4NO_ERROR != result), result);
885
886    if (pContext->pEditSettings->pClipList[iCurrentClipIndex]->FileType ==\
887         /*M4VIDEOEDITING_kFileType_JPG */ M4VIDEOEDITING_kFileType_ARGB8888) {
888            M4OSA_free((M4OSA_MemAddr32)frameStr.pBuffer);
889    } else {
890        M4OSA_free((M4OSA_MemAddr32)yuvPlane[0].pac_data);
891        M4OSA_free((M4OSA_MemAddr32)yuvPlane);
892    }
893    return tnTimeMs;
894}
895
896static int videoEditor_renderMediaItemPreviewFrame(JNIEnv* pEnv,
897                                                    jobject thiz,
898                                                    jobject mSurface,
899                                                    jstring filePath,
900                                                    jint    frameWidth,
901                                                    jint    frameHeight,
902                                                    jint    surfaceWidth,
903                                                    jint    surfaceHeight,
904                                                    jlong   fromMs)
905{
906    bool needToBeLoaded = true;
907    M4OSA_ERR result = M4NO_ERROR;
908    M4OSA_UInt32 timeMs = (M4OSA_UInt32)fromMs;
909    M4OSA_UInt32 framesizeYuv =0;
910    M4VIFI_UInt8 *pixelArray = M4OSA_NULL;
911    VideoEditor_renderPreviewFrameStr frameStr;
912    M4OSA_Context tnContext = M4OSA_NULL;
913    const char* pMessage = NULL;
914    M4VIFI_ImagePlane yuvPlane[3], rgbPlane;
915
916    ManualEditContext* pContext = M4OSA_NULL;
917    // Get the context.
918    pContext =
919            (ManualEditContext*)videoEditClasses_getContext(&needToBeLoaded,
920                                                      pEnv, thiz);
921
922    // Make sure that the context was set.
923    videoEditJava_checkAndThrowIllegalStateException(&needToBeLoaded, pEnv,
924                                             (M4OSA_NULL == pContext),
925                                             "not initialized");
926
927    // Make sure that the context was set.
928    videoEditJava_checkAndThrowIllegalStateException(&needToBeLoaded, pEnv,
929                                 (M4OSA_NULL == pContext->mPreviewController),
930                                 "not initialized");
931
932    // Validate the mSurface parameter.
933    videoEditJava_checkAndThrowIllegalArgumentException(&needToBeLoaded, pEnv,
934                                                (NULL == mSurface),
935                                                "mSurface is null");
936    jclass surfaceClass = pEnv->FindClass("android/view/Surface");
937    videoEditJava_checkAndThrowIllegalStateException(&needToBeLoaded, pEnv,
938                                             (M4OSA_NULL == surfaceClass),
939                                             "not initialized");
940
941    jfieldID surface_native =
942            pEnv->GetFieldID(surfaceClass, ANDROID_VIEW_SURFACE_JNI_ID, "I");
943    videoEditJava_checkAndThrowIllegalStateException(&needToBeLoaded, pEnv,
944                                             (M4OSA_NULL == surface_native),
945                                             "not initialized");
946
947    Surface* const p = (Surface*)pEnv->GetIntField(mSurface, surface_native);
948    sp<Surface> previewSurface = sp<Surface>(p);
949
950
951    const char *pString = pEnv->GetStringUTFChars(filePath, NULL);
952    if (pString == M4OSA_NULL) {
953        if (pEnv != NULL) {
954            jniThrowException(pEnv, "java/lang/RuntimeException", "Input string null");
955        }
956    }
957    VIDEOEDIT_LOG_FUNCTION(ANDROID_LOG_INFO, "VIDEO_EDITOR",
958        "videoEditor_renderMediaItemPreviewFrame() timeMs=%d", timeMs);
959    /* get thumbnail*/
960    result = ThumbnailOpen(&tnContext,(const M4OSA_Char*)pString, M4OSA_TRUE);
961    if (result != M4NO_ERROR || tnContext  == M4OSA_NULL) {
962        return timeMs;
963    }
964
965    framesizeYuv = ((frameWidth)*(frameHeight)*1.5);
966
967    pixelArray = (M4VIFI_UInt8 *)M4OSA_malloc(framesizeYuv, M4VS,\
968        (M4OSA_Char*)"videoEditor pixelArray");
969    if (pixelArray == M4OSA_NULL) {
970        VIDEOEDIT_LOG_FUNCTION(ANDROID_LOG_INFO, "VIDEO_EDITOR",
971            "videoEditor_renderPreviewFrame() malloc error");
972        ThumbnailClose(tnContext);
973        pMessage = videoEditJava_getErrorName(M4ERR_ALLOC);
974        jniThrowException(pEnv, "java/lang/RuntimeException", pMessage);
975        return timeMs;
976    }
977
978    result = ThumbnailGetPixels16(tnContext, (M4OSA_Int16 *)pixelArray,
979                                                frameWidth,
980                                                frameHeight, &timeMs);
981    if (result != M4NO_ERROR) {
982        M4OSA_free((M4OSA_MemAddr32)pixelArray);
983        ThumbnailClose(tnContext);
984        return fromMs;
985    }
986
987#ifdef DUMPTOFILESYSTEM
988    {
989        M4OSA_Context fileContext;
990        M4OSA_Char* fileName = (M4OSA_Char*)"/mnt/sdcard/FirstRGB565.rgb";
991        M4OSA_fileWriteOpen(&fileContext, (M4OSA_Void*) fileName,\
992            M4OSA_kFileWrite|M4OSA_kFileCreate);
993        M4OSA_fileWriteData(fileContext, (M4OSA_MemAddr8) pixelArray,
994                            framesizeRgb);
995        M4OSA_fileWriteClose(fileContext);
996    }
997#endif
998
999    yuvPlane[0].pac_data = (M4VIFI_UInt8*)pixelArray;
1000    yuvPlane[0].u_height = frameHeight;
1001    yuvPlane[0].u_width = frameWidth;
1002    yuvPlane[0].u_stride = yuvPlane[0].u_width;
1003    yuvPlane[0].u_topleft = 0;
1004
1005    yuvPlane[1].u_height = frameHeight/2;
1006    yuvPlane[1].u_width = frameWidth/2;
1007    yuvPlane[1].u_stride = yuvPlane[1].u_width;
1008    yuvPlane[1].u_topleft = 0;
1009    yuvPlane[1].pac_data = yuvPlane[0].pac_data
1010                + yuvPlane[0].u_width*yuvPlane[0].u_height;
1011
1012    yuvPlane[2].u_height = frameHeight/2;
1013    yuvPlane[2].u_width = frameWidth/2;
1014    yuvPlane[2].u_stride = yuvPlane[2].u_width;
1015    yuvPlane[2].u_topleft = 0;
1016    yuvPlane[2].pac_data = yuvPlane[0].pac_data
1017        + yuvPlane[0].u_width*yuvPlane[0].u_height + \
1018        (yuvPlane[0].u_width/2)*(yuvPlane[0].u_height/2);
1019#ifdef DUMPTOFILESYSTEM
1020    {
1021        M4OSA_Context fileContext;
1022        M4OSA_Char* fileName = (M4OSA_Char*)"/mnt/sdcard/ConvertedYuv.yuv";
1023        M4OSA_fileWriteOpen(&fileContext, (M4OSA_Void*) fileName,\
1024            M4OSA_kFileWrite|M4OSA_kFileCreate);
1025        M4OSA_fileWriteData(fileContext, (M4OSA_MemAddr8) yuvPlane[0].pac_data,
1026                            framesizeYuv);
1027        M4OSA_fileWriteClose(fileContext);
1028    }
1029#endif
1030
1031    /* Fill up the render structure*/
1032    frameStr.pBuffer = (M4OSA_Void*)yuvPlane[0].pac_data;
1033    frameStr.timeMs = timeMs;    /* timestamp on storyboard*/
1034    frameStr.uiSurfaceWidth = frameWidth;
1035    frameStr.uiSurfaceHeight = frameHeight;
1036    frameStr.uiFrameWidth = frameWidth;
1037    frameStr.uiFrameHeight = frameHeight;
1038    frameStr.bApplyEffect = M4OSA_FALSE;
1039    // clip begin cuttime and end cuttime set to 0
1040    // as its only required when effect needs to be applied while rendering
1041    frameStr.clipBeginCutTime = 0;
1042    frameStr.clipEndCutTime = 0;
1043
1044    /*  pContext->mPreviewController->setPreviewFrameRenderingMode(M4xVSS_kBlackBorders,
1045    (M4VIDEOEDITING_VideoFrameSize)(M4VIDEOEDITING_kHD960+1));*/
1046    result
1047    = pContext->mPreviewController->renderPreviewFrame(previewSurface,&frameStr, NULL);
1048    videoEditJava_checkAndThrowRuntimeException(&needToBeLoaded, pEnv,
1049                                                (M4NO_ERROR != result), result);
1050
1051    /* free the pixelArray and yuvPlane[0].pac_data */
1052    M4OSA_free((M4OSA_MemAddr32)yuvPlane[0].pac_data);
1053
1054    ThumbnailClose(tnContext);
1055
1056    return timeMs;
1057}
1058
1059int videoEditor_generateAudioRawFile(   JNIEnv*     pEnv,
1060                                        jobject     thiz,
1061                                        jstring     infilePath,
1062                                        jstring     pcmfilePath)
1063{
1064    M4OSA_ERR result = M4NO_ERROR;
1065    bool               loaded   = true;
1066    ManualEditContext* pContext = M4OSA_NULL;
1067
1068
1069
1070    const char *pInputFile = pEnv->GetStringUTFChars(infilePath, NULL);
1071    if (pInputFile == M4OSA_NULL) {
1072        if (pEnv != NULL) {
1073            jniThrowException(pEnv, "java/lang/RuntimeException", "Input string null");
1074        }
1075    }
1076
1077    const char *pStringOutPCMFilePath = pEnv->GetStringUTFChars(pcmfilePath, NULL);
1078    if (pStringOutPCMFilePath == M4OSA_NULL) {
1079        if (pEnv != NULL) {
1080            jniThrowException(pEnv, "java/lang/RuntimeException", "Input string null");
1081        }
1082    }
1083
1084    VIDEOEDIT_LOG_FUNCTION(ANDROID_LOG_INFO,
1085        "VIDEO_EDITOR", "videoEditor_generateAudioRawFile infilePath %s",
1086        pInputFile);
1087    VIDEOEDIT_LOG_FUNCTION(ANDROID_LOG_INFO,
1088        "VIDEO_EDITOR", "videoEditor_generateAudioRawFile pcmfilePath %s",
1089        pStringOutPCMFilePath);
1090    // Get the context.
1091    pContext = (ManualEditContext*)videoEditClasses_getContext(&loaded, pEnv, thiz);
1092
1093    result = videoEditor_generateAudio( pEnv, pContext, (M4OSA_Char*)pInputFile,
1094        (M4OSA_Char*)pStringOutPCMFilePath);
1095
1096    return result;
1097}
1098
1099M4OSA_ERR videoEditor_generateAudio(JNIEnv* pEnv,ManualEditContext* pContext,
1100                                    M4OSA_Char* infilePath,
1101                                    M4OSA_Char* pcmfilePath )
1102{
1103    bool                            needToBeLoaded = true;
1104    M4OSA_ERR                       result = M4NO_ERROR;
1105    M4MCS_Context                   mcsContext;
1106    M4OSA_Char*                     pInputFile = M4OSA_NULL;
1107    M4OSA_Char*                     pOutputFile = M4OSA_NULL;
1108    M4OSA_Char*                     pTempPath = M4OSA_NULL;
1109    M4MCS_OutputParams*             pOutputParams = M4OSA_NULL;
1110    M4MCS_EncodingParams*           pEncodingParams = M4OSA_NULL;
1111    M4OSA_Int32                     pInputFileType = 0;
1112    M4OSA_UInt8                     threadProgress = 0;
1113    M4OSA_Char*                     pTemp3gpFilePath = M4OSA_NULL;
1114
1115    VIDEOEDIT_LOG_API(ANDROID_LOG_INFO, "VIDEO_EDITOR", "videoEditor_generateAudio()");
1116
1117    videoEditJava_checkAndThrowIllegalArgumentException(&needToBeLoaded, pEnv,
1118        (NULL == pContext),
1119        "ManualEditContext is null");
1120
1121    VIDEOEDIT_LOG_FUNCTION(ANDROID_LOG_INFO, "VIDEO_EDITOR", "M4MCS_init()");
1122
1123    pOutputParams = (M4MCS_OutputParams *)M4OSA_malloc(
1124        sizeof(M4MCS_OutputParams),0x00,
1125        (M4OSA_Char *)"M4MCS_OutputParams");
1126    videoEditJava_checkAndThrowIllegalStateException(&needToBeLoaded, pEnv,
1127        (M4OSA_NULL == pOutputParams),
1128        "not initialized");
1129
1130    pEncodingParams = (M4MCS_EncodingParams *)M4OSA_malloc(
1131        sizeof(M4MCS_EncodingParams),0x00,
1132        (M4OSA_Char *)"M4MCS_EncodingParams");
1133    videoEditJava_checkAndThrowIllegalStateException(&needToBeLoaded, pEnv,
1134        (M4OSA_NULL == pEncodingParams),
1135        "not initialized");
1136    // Initialize the MCS library.
1137    result = M4MCS_init(&mcsContext, pContext->initParams.pFileReadPtr,
1138        pContext->initParams.pFileWritePtr);
1139    videoEditJava_checkAndThrowRuntimeException(&needToBeLoaded, pEnv,\
1140        (M4NO_ERROR != result), result);
1141    videoEditJava_checkAndThrowIllegalStateException(&needToBeLoaded, pEnv,
1142        (M4OSA_NULL == mcsContext),
1143        "not initialized");
1144    // generate the path for temp 3gp output file
1145    pTemp3gpFilePath = (M4OSA_Char*) M4OSA_malloc (
1146        (M4OSA_chrLength((M4OSA_Char*)pContext->initParams.pTempPath)
1147        + M4OSA_chrLength ((M4OSA_Char*)TEMP_MCS_OUT_FILE_PATH)) + 1 /* for null termination */ , 0x0,
1148        (M4OSA_Char*) "Malloc for temp 3gp file");
1149    if ( pTemp3gpFilePath != M4OSA_NULL )
1150    {
1151        M4OSA_memset(pTemp3gpFilePath  ,
1152            M4OSA_chrLength((M4OSA_Char*)pContext->initParams.pTempPath)
1153            + M4OSA_chrLength((M4OSA_Char*)TEMP_MCS_OUT_FILE_PATH) + 1, 0);
1154        M4OSA_chrNCat ( (M4OSA_Char*)pTemp3gpFilePath,
1155            (M4OSA_Char*)pContext->initParams.pTempPath  ,
1156            M4OSA_chrLength ((M4OSA_Char*)pContext->initParams.pTempPath));
1157        M4OSA_chrNCat ( pTemp3gpFilePath , (M4OSA_Char*)TEMP_MCS_OUT_FILE_PATH,
1158            M4OSA_chrLength ((M4OSA_Char*)TEMP_MCS_OUT_FILE_PATH));
1159    }
1160
1161    pInputFile = (M4OSA_Char *) infilePath; //pContext->mAudioSettings->pFile;
1162    //Delete this file later
1163    pOutputFile = (M4OSA_Char *) pTemp3gpFilePath;
1164    // Temp folder path for VSS use = ProjectPath
1165    pTempPath = (M4OSA_Char *) pContext->initParams.pTempPath;
1166    pInputFileType = (M4VIDEOEDITING_FileType)pContext->mAudioSettings->fileType;
1167
1168    VIDEOEDIT_LOG_API(ANDROID_LOG_INFO, "VIDEO_EDITOR", "TEMP_MCS_OUT_FILE_PATH len %d",
1169        M4OSA_chrLength ((M4OSA_Char*)TEMP_MCS_OUT_FILE_PATH));
1170    VIDEOEDIT_LOG_API(ANDROID_LOG_INFO, "VIDEO_EDITOR", "pTemp3gpFilePath %s",
1171        pOutputFile);
1172
1173    VIDEOEDIT_LOG_API(ANDROID_LOG_INFO, "VIDEO_EDITOR", "M4MCS_open()");
1174
1175    result = M4MCS_open(mcsContext, pInputFile,
1176        (M4VIDEOEDITING_FileType)pInputFileType,
1177        pOutputFile, pTempPath);
1178    videoEditJava_checkAndThrowRuntimeException(&needToBeLoaded, pEnv,
1179        (M4NO_ERROR != result), result);
1180
1181    pOutputParams->OutputFileType
1182        = (M4VIDEOEDITING_FileType)M4VIDEOEDITING_kFileType_3GPP;
1183    // Set the video format.
1184    pOutputParams->OutputVideoFormat =
1185        (M4VIDEOEDITING_VideoFormat)M4VIDEOEDITING_kNoneVideo;//M4VIDEOEDITING_kNoneVideo;
1186    // Set the frame size.
1187    pOutputParams->OutputVideoFrameSize
1188        = (M4VIDEOEDITING_VideoFrameSize)M4VIDEOEDITING_kQCIF;
1189    // Set the frame rate.
1190    pOutputParams->OutputVideoFrameRate
1191        = (M4VIDEOEDITING_VideoFramerate)M4VIDEOEDITING_k5_FPS;
1192
1193    // Set the audio format.
1194    pOutputParams->OutputAudioFormat
1195        = (M4VIDEOEDITING_AudioFormat)M4VIDEOEDITING_kAAC;
1196    // Set the audio sampling frequency.
1197    pOutputParams->OutputAudioSamplingFrequency =
1198        (M4VIDEOEDITING_AudioSamplingFrequency)M4VIDEOEDITING_k32000_ASF;
1199    // Set the audio mono.
1200    pOutputParams->bAudioMono = false;
1201    // Set the pcm file; null for now.
1202    pOutputParams->pOutputPCMfile = (M4OSA_Char *)pcmfilePath;
1203    //(M4OSA_Char *)"/sdcard/Output/AudioPcm.pcm";
1204    // Set the audio sampling frequency.
1205    pOutputParams->MediaRendering = (M4MCS_MediaRendering)M4MCS_kCropping;
1206    // new params after integrating MCS 2.0
1207    // Set the number of audio effects; 0 for now.
1208    pOutputParams->nbEffects = 0;
1209    // Set the audio effect; null for now.
1210    pOutputParams->pEffects = NULL;
1211    // Set the audio effect; null for now.
1212    pOutputParams->bDiscardExif = M4OSA_FALSE;
1213    // Set the audio effect; null for now.
1214    pOutputParams->bAdjustOrientation = M4OSA_FALSE;
1215
1216    VIDEOEDIT_LOG_FUNCTION(ANDROID_LOG_INFO, "VIDEO_EDITOR", "M4MCS_setOutputParams()");
1217    result = M4MCS_setOutputParams(mcsContext, pOutputParams);
1218    videoEditJava_checkAndThrowRuntimeException(&needToBeLoaded, pEnv,
1219                                        (M4NO_ERROR != result), result);
1220
1221    // Set the video bitrate.
1222    pEncodingParams->OutputVideoBitrate =
1223    (M4VIDEOEDITING_Bitrate)M4VIDEOEDITING_kUndefinedBitrate;
1224    // Set the audio bitrate.
1225    pEncodingParams->OutputAudioBitrate
1226        = (M4VIDEOEDITING_Bitrate)M4VIDEOEDITING_k128_KBPS;
1227    // Set the end cut time in milliseconds.
1228    pEncodingParams->BeginCutTime = 0;
1229    // Set the end cut time in milliseconds.
1230    pEncodingParams->EndCutTime = 0;
1231    // Set the output file size in bytes.
1232    pEncodingParams->OutputFileSize = 0;
1233    // Set video time scale.
1234    pEncodingParams->OutputVideoTimescale = 0;
1235
1236    VIDEOEDIT_LOG_FUNCTION(ANDROID_LOG_INFO, "VIDEO_EDITOR",
1237                            "M4MCS_setEncodingParams()");
1238    result = M4MCS_setEncodingParams(mcsContext, pEncodingParams);
1239    videoEditJava_checkAndThrowRuntimeException(&needToBeLoaded, pEnv,
1240        (M4NO_ERROR != result), result);
1241
1242    VIDEOEDIT_LOG_FUNCTION(ANDROID_LOG_INFO, "VIDEO_EDITOR",
1243                            "M4MCS_checkParamsAndStart()");
1244    result = M4MCS_checkParamsAndStart(mcsContext);
1245    videoEditJava_checkAndThrowRuntimeException(&needToBeLoaded, pEnv,
1246        (M4NO_ERROR != result), result);
1247
1248    VIDEOEDIT_LOG_FUNCTION(ANDROID_LOG_INFO, "VIDEO_EDITOR", "M4MCS_step()");
1249
1250    /*+ PROGRESS CB */
1251    M4OSA_UInt8 curProgress = 0;
1252    int         lastProgress = 0;
1253
1254    LOGV("LVME_generateAudio Current progress is =%d", curProgress);
1255    pEnv->CallVoidMethod(pContext->engine,
1256            pContext->onProgressUpdateMethodId, 1/*task status*/,
1257            curProgress/*progress*/);
1258    do {
1259        result = M4MCS_step(mcsContext, &curProgress);
1260
1261        if (result != M4NO_ERROR) {
1262            LOGV("LVME_generateAudio M4MCS_step returned 0x%x",result);
1263
1264            if (result == M4MCS_WAR_TRANSCODING_DONE) {
1265                LOGV("LVME_generateAudio MCS process ended");
1266
1267                // Send a progress notification.
1268                curProgress = 100;
1269                pEnv->CallVoidMethod(pContext->engine,
1270                    pContext->onProgressUpdateMethodId, 1/*task status*/,
1271                    curProgress);
1272                LOGV("LVME_generateAudio Current progress is =%d", curProgress);
1273            }
1274        } else {
1275            // Send a progress notification if needed
1276            if (curProgress != lastProgress) {
1277                lastProgress = curProgress;
1278                pEnv->CallVoidMethod(pContext->engine,
1279                    pContext->onProgressUpdateMethodId, 0/*task status*/,
1280                    curProgress/*progress*/);
1281                LOGV("LVME_generateAudio Current progress is =%d",curProgress);
1282            }
1283        }
1284    } while (result == M4NO_ERROR);
1285    /*- PROGRESS CB */
1286
1287    videoEditJava_checkAndThrowRuntimeException(&needToBeLoaded, pEnv,
1288        (M4MCS_WAR_TRANSCODING_DONE != result), result);
1289
1290    VIDEOEDIT_LOG_FUNCTION(ANDROID_LOG_INFO, "VIDEO_EDITOR", "M4MCS_abort()");
1291    result = M4MCS_abort(mcsContext);
1292    videoEditJava_checkAndThrowRuntimeException(&needToBeLoaded, pEnv,
1293        (M4NO_ERROR != result), result);
1294
1295    //pContext->mAudioSettings->pFile = pOutputParams->pOutputPCMfile;
1296    M4OSA_fileExtraDelete((const M4OSA_Char *) pTemp3gpFilePath);
1297    VIDEOEDIT_LOG_FUNCTION(ANDROID_LOG_INFO, "VIDEO_EDITOR", "videoEditor_generateAudio() EXIT ");
1298
1299    M4OSA_free((M4OSA_MemAddr32)pTemp3gpFilePath);
1300    M4OSA_free((M4OSA_MemAddr32)pOutputParams);
1301    M4OSA_free((M4OSA_MemAddr32)pEncodingParams);
1302    return result;
1303}
1304
1305static int removeAlphafromRGB8888 (
1306                        M4OSA_Char* pFramingFilePath,
1307                        M4xVSS_FramingStruct *pFramingCtx)
1308{
1309    M4OSA_UInt32 frameSize_argb = (pFramingCtx->width * pFramingCtx->height * 4); // aRGB data
1310    M4OSA_Context lImageFileFp  = M4OSA_NULL;
1311    M4OSA_ERR err = M4NO_ERROR;
1312
1313    LOGV("removeAlphafromRGB8888: width %d", pFramingCtx->width);
1314
1315    M4OSA_UInt8 *pTmpData = (M4OSA_UInt8*) M4OSA_malloc(frameSize_argb, M4VS, (M4OSA_Char*)"Image argb data");
1316    if (pTmpData == M4OSA_NULL) {
1317        LOGE("Failed to allocate memory for Image clip");
1318        return M4ERR_ALLOC;
1319    }
1320
1321       /** Read the argb data from the passed file. */
1322    M4OSA_ERR lerr = M4OSA_fileReadOpen(&lImageFileFp, (M4OSA_Void *) pFramingFilePath, M4OSA_kFileRead);
1323
1324
1325    if ((lerr != M4NO_ERROR) || (lImageFileFp == M4OSA_NULL))
1326    {
1327        LOGE("removeAlphafromRGB8888: Can not open the file ");
1328        M4OSA_free((M4OSA_MemAddr32)pTmpData);
1329        return M4ERR_FILE_NOT_FOUND;
1330    }
1331
1332
1333    lerr = M4OSA_fileReadData(lImageFileFp, (M4OSA_MemAddr8)pTmpData, &frameSize_argb);
1334    if (lerr != M4NO_ERROR)
1335    {
1336        LOGE("removeAlphafromRGB8888: can not read the data ");
1337        M4OSA_fileReadClose(lImageFileFp);
1338        M4OSA_free((M4OSA_MemAddr32)pTmpData);
1339        return lerr;
1340    }
1341    M4OSA_fileReadClose(lImageFileFp);
1342
1343    M4OSA_UInt32 frameSize = (pFramingCtx->width * pFramingCtx->height * 3); //Size of RGB 888 data.
1344
1345    pFramingCtx->FramingRgb = (M4VIFI_ImagePlane*)M4OSA_malloc(
1346             sizeof(M4VIFI_ImagePlane), M4VS, (M4OSA_Char*)"Image clip RGB888 data");
1347    pFramingCtx->FramingRgb->pac_data = (M4VIFI_UInt8*)M4OSA_malloc(
1348             frameSize, M4VS, (M4OSA_Char*)"Image clip RGB888 data");
1349
1350    if (pFramingCtx->FramingRgb == M4OSA_NULL)
1351    {
1352        LOGE("Failed to allocate memory for Image clip");
1353        M4OSA_free((M4OSA_MemAddr32)pTmpData);
1354        return M4ERR_ALLOC;
1355    }
1356
1357    /** Remove the alpha channel */
1358    for (size_t i = 0, j = 0; i < frameSize_argb; i++) {
1359        if ((i % 4) == 0) continue;
1360        pFramingCtx->FramingRgb->pac_data[j] = pTmpData[i];
1361        j++;
1362    }
1363    M4OSA_free((M4OSA_MemAddr32)pTmpData);
1364    return M4NO_ERROR;
1365}
1366
1367static void
1368videoEditor_populateSettings(
1369                JNIEnv*                 pEnv,
1370                jobject                 thiz,
1371                jobject                 settings,
1372                jobject                 object,
1373                jobject                 audioSettingObject)
1374{
1375    VIDEOEDIT_LOG_FUNCTION(ANDROID_LOG_INFO, "VIDEO_EDITOR",
1376            "videoEditor_populateSettings()");
1377
1378    bool                needToBeLoaded  = true;
1379    ManualEditContext*  pContext        = M4OSA_NULL;
1380    M4OSA_ERR           result          = M4NO_ERROR;
1381    jstring             str             = M4OSA_NULL;
1382    jobjectArray        propertiesClipsArray           = M4OSA_NULL;
1383    jobject             properties      = M4OSA_NULL;
1384    jint*               bitmapArray     =  M4OSA_NULL;
1385    jobjectArray        effectSettingsArray = M4OSA_NULL;
1386    jobject             effectSettings  = M4OSA_NULL;
1387    jintArray           pixelArray      = M4OSA_NULL;
1388    int width = 0;
1389    int height = 0;
1390    int nbOverlays = 0;
1391    int i,j = 0;
1392    int *pOverlayIndex = M4OSA_NULL;
1393
1394    // Add a code marker (the condition must always be true).
1395    ADD_CODE_MARKER_FUN(NULL != pEnv)
1396
1397    // Validate the settings parameter.
1398    videoEditJava_checkAndThrowIllegalArgumentException(&needToBeLoaded, pEnv,
1399                                                (NULL == settings),
1400                                                "settings is null");
1401    // Get the context.
1402    pContext =
1403            (ManualEditContext*)videoEditClasses_getContext(&needToBeLoaded, pEnv, thiz);
1404
1405    // Make sure that the context was set.
1406    videoEditJava_checkAndThrowIllegalStateException(&needToBeLoaded, pEnv,
1407                                             (M4OSA_NULL == pContext),
1408                                             "not initialized");
1409    // Make sure that the context was set.
1410    videoEditJava_checkAndThrowIllegalStateException(&needToBeLoaded, pEnv,
1411                                 (M4OSA_NULL == pContext->mPreviewController),
1412                                 "not initialized");
1413    jclass mPreviewClipPropClazz = pEnv->FindClass(PREVIEW_PROPERTIES_CLASS_NAME);
1414    videoEditJava_checkAndThrowIllegalStateException(&needToBeLoaded, pEnv,
1415                                     (M4OSA_NULL == mPreviewClipPropClazz),
1416                                     "not initialized");
1417
1418    jfieldID fid = pEnv->GetFieldID(mPreviewClipPropClazz,"clipProperties",
1419            "[L"PROPERTIES_CLASS_NAME";"  );
1420    videoEditJava_checkAndThrowIllegalStateException(&needToBeLoaded, pEnv,
1421                                     (M4OSA_NULL == fid),
1422                                     "not initialized");
1423
1424    propertiesClipsArray = (jobjectArray)pEnv->GetObjectField(object, fid);
1425    videoEditJava_checkAndThrowIllegalStateException(&needToBeLoaded, pEnv,
1426                                     (M4OSA_NULL == propertiesClipsArray),
1427                                     "not initialized");
1428
1429    jclass engineClass = pEnv->FindClass(MANUAL_EDIT_ENGINE_CLASS_NAME);
1430    videoEditJava_checkAndThrowIllegalStateException(&needToBeLoaded, pEnv,
1431                                     (M4OSA_NULL == engineClass),
1432                                     "not initialized");
1433
1434    pContext->onPreviewProgressUpdateMethodId = pEnv->GetMethodID(engineClass,
1435            "onPreviewProgressUpdate",     "(IZZLjava/lang/String;I)V");
1436    // Check if the context is valid (required because the context is dereferenced).
1437    if (needToBeLoaded) {
1438        // Make sure that we are in a correct state.
1439        videoEditJava_checkAndThrowIllegalStateException(&needToBeLoaded, pEnv,
1440                             (pContext->state != ManualEditState_INITIALIZED),
1441                             "settings already loaded");
1442        // Retrieve the edit settings.
1443        if (pContext->pEditSettings != M4OSA_NULL) {
1444            videoEditClasses_freeEditSettings(&pContext->pEditSettings);
1445            pContext->pEditSettings = M4OSA_NULL;
1446        }
1447        videoEditClasses_getEditSettings(&needToBeLoaded, pEnv,
1448            settings, &pContext->pEditSettings,false);
1449    }
1450    M4OSA_TRACE1_0("videoEditorC_getEditSettings done");
1451
1452    pContext->previewFrameEditInfoId = pEnv->GetMethodID(engineClass,
1453        "previewFrameEditInfo", "(Ljava/lang/String;I)V");
1454
1455    if ( pContext->pEditSettings != NULL )
1456    {
1457        // Check if the edit settings could be retrieved.
1458        jclass mEditClazz = pEnv->FindClass(EDIT_SETTINGS_CLASS_NAME);
1459        if(mEditClazz == M4OSA_NULL)
1460        {
1461            M4OSA_TRACE1_0("cannot find object field for mEditClazz");
1462            return;
1463        }
1464        jclass mEffectsClazz = pEnv->FindClass(EFFECT_SETTINGS_CLASS_NAME);
1465        if(mEffectsClazz == M4OSA_NULL)
1466        {
1467            M4OSA_TRACE1_0("cannot find object field for mEffectsClazz");
1468            return;
1469        }
1470        fid = pEnv->GetFieldID(mEditClazz,"effectSettingsArray", "[L"EFFECT_SETTINGS_CLASS_NAME";"  );
1471        if(fid == M4OSA_NULL)
1472        {
1473            M4OSA_TRACE1_0("cannot find field for effectSettingsArray Array");
1474            return;
1475        }
1476        effectSettingsArray = (jobjectArray)pEnv->GetObjectField(settings, fid);
1477        if(effectSettingsArray == M4OSA_NULL)
1478        {
1479            M4OSA_TRACE1_0("cannot find object field for effectSettingsArray");
1480            return;
1481        }
1482        i = 0;
1483        j = 0;
1484        //int overlayIndex[pContext->pEditSettings->nbEffects];
1485        if ( pContext->pEditSettings->nbEffects )
1486        {
1487            pOverlayIndex
1488            = (int*) M4OSA_malloc(pContext->pEditSettings->nbEffects, 0,
1489                (M4OSA_Char*)"pOverlayIndex");
1490        }
1491
1492        M4OSA_TRACE1_1("no of effects = %d",pContext->pEditSettings->nbEffects);
1493        while (j < pContext->pEditSettings->nbEffects)
1494        {
1495            if (pContext->pEditSettings->Effects[j].xVSS.pFramingFilePath != M4OSA_NULL)
1496            {
1497                pOverlayIndex[nbOverlays] = j;
1498                nbOverlays++;
1499                M4xVSS_FramingStruct *aFramingCtx = M4OSA_NULL;
1500                aFramingCtx
1501                = (M4xVSS_FramingStruct*)M4OSA_malloc(sizeof(M4xVSS_FramingStruct), M4VS,
1502                  (M4OSA_Char*)"M4xVSS_internalDecodeGIF: Context of the framing effect");
1503                if (aFramingCtx == M4OSA_NULL)
1504                {
1505                    M4OSA_TRACE1_0("Allocation error in videoEditor_populateSettings");
1506                }
1507                aFramingCtx->pCurrent = M4OSA_NULL; /* Only used by the first element of the chain */
1508                aFramingCtx->previousClipTime = -1;
1509                aFramingCtx->FramingYuv = M4OSA_NULL;
1510                aFramingCtx->FramingRgb = M4OSA_NULL;
1511                aFramingCtx->topleft_x
1512                    = pContext->pEditSettings->Effects[j].xVSS.topleft_x;
1513                aFramingCtx->topleft_y
1514                    = pContext->pEditSettings->Effects[j].xVSS.topleft_y;
1515
1516
1517                 VIDEOEDIT_LOG_FUNCTION(ANDROID_LOG_INFO, "VIDEO_EDITOR", "OF u_width %d",
1518                                        pContext->pEditSettings->Effects[j].xVSS.pFramingBuffer->u_width);
1519                 VIDEOEDIT_LOG_FUNCTION(ANDROID_LOG_INFO, "VIDEO_EDITOR", "OF u_height() %d",
1520                                        pContext->pEditSettings->Effects[j].xVSS.pFramingBuffer->u_height);
1521                 VIDEOEDIT_LOG_FUNCTION(ANDROID_LOG_INFO, "VIDEO_EDITOR", "OF rgbType() %d",
1522                                        pContext->pEditSettings->Effects[j].xVSS.rgbType);
1523
1524                 aFramingCtx->width = pContext->pEditSettings->Effects[j].xVSS.pFramingBuffer->u_width;
1525                 aFramingCtx->height = pContext->pEditSettings->Effects[j].xVSS.pFramingBuffer->u_height;
1526
1527
1528                result = M4xVSS_internalConvertARGB888toYUV420_FrammingEffect(pContext->engineContext,
1529                    &(pContext->pEditSettings->Effects[j]),aFramingCtx,
1530                pContext->pEditSettings->Effects[j].xVSS.framingScaledSize);
1531                if (result != M4NO_ERROR)
1532                {
1533                    M4OSA_TRACE1_1("M4xVSS_internalConvertARGB888toYUV420_FrammingEffect returned 0x%x", result);
1534                }
1535
1536                //framing buffers are resized to fit the output video resolution.
1537                pContext->pEditSettings->Effects[j].xVSS.pFramingBuffer->u_width =
1538                    aFramingCtx->FramingRgb->u_width;
1539                pContext->pEditSettings->Effects[j].xVSS.pFramingBuffer->u_height =
1540                    aFramingCtx->FramingRgb->u_height;
1541
1542
1543                VIDEOEDIT_LOG_FUNCTION(ANDROID_LOG_INFO, "VIDEO_EDITOR", "A framing Context aFramingCtx->width = %d",
1544                    aFramingCtx->FramingRgb->u_width);
1545
1546                VIDEOEDIT_LOG_FUNCTION(ANDROID_LOG_INFO, "VIDEO_EDITOR", "A framing Context aFramingCtx->height = %d",
1547                    aFramingCtx->FramingRgb->u_height);
1548
1549
1550                width = pContext->pEditSettings->Effects[j].xVSS.pFramingBuffer->u_width;
1551                height = pContext->pEditSettings->Effects[j].xVSS.pFramingBuffer->u_height;
1552
1553                //RGB 565
1554                pContext->pEditSettings->Effects[j].xVSS.pFramingBuffer->u_stride = width*2;
1555
1556                //for RGB565
1557                pContext->pEditSettings->Effects[j].xVSS.pFramingBuffer->u_topleft = 0;
1558                pContext->pEditSettings->Effects[j].xVSS.pFramingBuffer->pac_data =
1559                            (M4VIFI_UInt8 *)M4OSA_malloc(width*height*2,
1560                            0x00,(M4OSA_Char *)"pac_data buffer");
1561
1562                M4OSA_memcpy((M4OSA_Int8 *)&pContext->pEditSettings->\
1563                    Effects[j].xVSS.pFramingBuffer->\
1564                    pac_data[0],(M4OSA_Int8 *)&aFramingCtx->FramingRgb->pac_data[0],(width*height*2));
1565
1566                //As of now rgb type is 565
1567                pContext->pEditSettings->Effects[j].xVSS.rgbType =
1568                    (M4VSS3GPP_RGBType) M4VSS3GPP_kRGB565;
1569
1570                if (aFramingCtx->FramingYuv != M4OSA_NULL )
1571                {
1572                    if (aFramingCtx->FramingYuv->pac_data != M4OSA_NULL) {
1573                        M4OSA_free((M4OSA_MemAddr32)aFramingCtx->FramingYuv->pac_data);
1574                        aFramingCtx->FramingYuv->pac_data = M4OSA_NULL;
1575                    }
1576                }
1577                if (aFramingCtx->FramingYuv != M4OSA_NULL) {
1578                    M4OSA_free((M4OSA_MemAddr32)aFramingCtx->FramingYuv);
1579                    aFramingCtx->FramingYuv = M4OSA_NULL;
1580                }
1581                if (aFramingCtx->FramingRgb->pac_data != M4OSA_NULL) {
1582                    M4OSA_free((M4OSA_MemAddr32)aFramingCtx->FramingRgb->pac_data);
1583                    aFramingCtx->FramingRgb->pac_data = M4OSA_NULL;
1584                }
1585                if (aFramingCtx->FramingRgb != M4OSA_NULL) {
1586                    M4OSA_free((M4OSA_MemAddr32)aFramingCtx->FramingRgb);
1587                    aFramingCtx->FramingRgb = M4OSA_NULL;
1588                }
1589                if (aFramingCtx != M4OSA_NULL) {
1590                    M4OSA_free((M4OSA_MemAddr32)aFramingCtx);
1591                    aFramingCtx = M4OSA_NULL;
1592                }
1593            }
1594            j++;
1595        }
1596
1597        // Check if the edit settings could be retrieved.
1598        M4OSA_TRACE1_1("total clips are = %d",pContext->pEditSettings->uiClipNumber);
1599        for (i = 0; i < pContext->pEditSettings->uiClipNumber; i++) {
1600            M4OSA_TRACE1_1("clip no = %d",i);
1601            properties = pEnv->GetObjectArrayElement(propertiesClipsArray, i);
1602            videoEditJava_checkAndThrowIllegalStateException(&needToBeLoaded, pEnv,
1603                (M4OSA_NULL == properties),
1604                "not initialized");
1605            getClipSetting(pEnv,properties, pContext->pEditSettings->pClipList[i]);
1606        }
1607
1608        if (needToBeLoaded) {
1609            // Log the edit settings.
1610            VIDEOEDIT_LOG_EDIT_SETTINGS(pContext->pEditSettings);
1611        }
1612    }
1613
1614    if (audioSettingObject != M4OSA_NULL) {
1615        jclass audioSettingClazz = pEnv->FindClass(AUDIO_SETTINGS_CLASS_NAME);
1616        videoEditJava_checkAndThrowIllegalStateException(&needToBeLoaded, pEnv,
1617                                         (M4OSA_NULL == audioSettingClazz),
1618                                         "not initialized");
1619
1620        videoEditJava_checkAndThrowIllegalStateException(&needToBeLoaded, pEnv,
1621                                     (M4OSA_NULL == pContext->mAudioSettings),
1622                                     "not initialized");
1623
1624        fid = pEnv->GetFieldID(audioSettingClazz,"bRemoveOriginal","Z");
1625        pContext->mAudioSettings->bRemoveOriginal = pEnv->GetIntField(audioSettingObject,fid);
1626        M4OSA_TRACE1_1("bRemoveOriginal = %d",pContext->mAudioSettings->bRemoveOriginal);
1627
1628        fid = pEnv->GetFieldID(audioSettingClazz,"channels","I");
1629        pContext->mAudioSettings->uiNbChannels = pEnv->GetIntField(audioSettingObject,fid);
1630        M4OSA_TRACE1_1("uiNbChannels = %d",pContext->mAudioSettings->uiNbChannels);
1631
1632        fid = pEnv->GetFieldID(audioSettingClazz,"Fs","I");
1633        pContext->mAudioSettings->uiSamplingFrequency = pEnv->GetIntField(audioSettingObject,fid);
1634        M4OSA_TRACE1_1("uiSamplingFrequency = %d",pContext->mAudioSettings->uiSamplingFrequency);
1635
1636        fid = pEnv->GetFieldID(audioSettingClazz,"ExtendedFs","I");
1637        pContext->mAudioSettings->uiExtendedSamplingFrequency =
1638         pEnv->GetIntField(audioSettingObject,fid);
1639        M4OSA_TRACE1_1("uiExtendedSamplingFrequency = %d",
1640        pContext->mAudioSettings->uiExtendedSamplingFrequency);
1641
1642        fid = pEnv->GetFieldID(audioSettingClazz,"startMs","J");
1643        pContext->mAudioSettings->uiAddCts
1644            = pEnv->GetIntField(audioSettingObject,fid);
1645        M4OSA_TRACE1_1("uiAddCts = %d",pContext->mAudioSettings->uiAddCts);
1646
1647        fid = pEnv->GetFieldID(audioSettingClazz,"volume","I");
1648        pContext->mAudioSettings->uiAddVolume
1649            = pEnv->GetIntField(audioSettingObject,fid);
1650        M4OSA_TRACE1_1("uiAddVolume = %d",pContext->mAudioSettings->uiAddVolume);
1651
1652        fid = pEnv->GetFieldID(audioSettingClazz,"loop","Z");
1653        pContext->mAudioSettings->bLoop
1654            = pEnv->GetIntField(audioSettingObject,fid);
1655        M4OSA_TRACE1_1("bLoop = %d",pContext->mAudioSettings->bLoop);
1656
1657        fid = pEnv->GetFieldID(audioSettingClazz,"beginCutTime","J");
1658        pContext->mAudioSettings->beginCutMs
1659            = pEnv->GetIntField(audioSettingObject,fid);
1660        M4OSA_TRACE1_1("begin cut time = %d",pContext->mAudioSettings->beginCutMs);
1661
1662        fid = pEnv->GetFieldID(audioSettingClazz,"endCutTime","J");
1663        pContext->mAudioSettings->endCutMs
1664            = pEnv->GetIntField(audioSettingObject,fid);
1665        M4OSA_TRACE1_1("end cut time = %d",pContext->mAudioSettings->endCutMs);
1666
1667        fid = pEnv->GetFieldID(audioSettingClazz,"fileType","I");
1668        pContext->mAudioSettings->fileType
1669            = pEnv->GetIntField(audioSettingObject,fid);
1670        M4OSA_TRACE1_1("fileType = %d",pContext->mAudioSettings->fileType);
1671        fid = pEnv->GetFieldID(audioSettingClazz,"pFile","Ljava/lang/String;");
1672        str = (jstring)pEnv->GetObjectField(audioSettingObject,fid);
1673        pContext->mAudioSettings->pFile
1674                = (M4OSA_Char*)pEnv->GetStringUTFChars(str, M4OSA_NULL);
1675        M4OSA_TRACE1_1("file name = %s",pContext->mAudioSettings->pFile);
1676        VIDEOEDIT_LOG_API(ANDROID_LOG_INFO, "VIDEOEDITOR", "regenerateAudio() file name = %s",\
1677        pContext->mAudioSettings->pFile);
1678        fid = pEnv->GetFieldID(audioSettingClazz,"pcmFilePath","Ljava/lang/String;");
1679        str = (jstring)pEnv->GetObjectField(audioSettingObject,fid);
1680        pContext->mAudioSettings->pPCMFilePath =
1681        (M4OSA_Char*)pEnv->GetStringUTFChars(str, M4OSA_NULL);
1682        VIDEOEDIT_LOG_API(ANDROID_LOG_INFO, "VIDEOEDITOR", "pPCMFilePath -- %s ",\
1683        pContext->mAudioSettings->pPCMFilePath);
1684        fid = pEnv->GetFieldID(engineClass,"mRegenerateAudio","Z");
1685        bool regenerateAudio = pEnv->GetBooleanField(thiz,fid);
1686        VIDEOEDIT_LOG_API(ANDROID_LOG_INFO, "VIDEOEDITOR", "regenerateAudio -- %d ",\
1687        regenerateAudio);
1688        if (regenerateAudio) {
1689            M4OSA_TRACE1_0("Calling Generate Audio now");
1690            result = videoEditor_generateAudio(pEnv,
1691                        pContext,
1692                        (M4OSA_Char*)pContext->mAudioSettings->pFile,
1693                        (M4OSA_Char*)pContext->mAudioSettings->pPCMFilePath);
1694            regenerateAudio = false;
1695            pEnv->SetBooleanField(thiz,fid,regenerateAudio);
1696        }
1697        VIDEOEDIT_LOG_API(ANDROID_LOG_INFO, "VIDEOEDITOR", "regenerateAudio()");
1698
1699        /* Audio mix and duck */
1700        fid = pEnv->GetFieldID(audioSettingClazz,"ducking_threshold","I");
1701        pContext->mAudioSettings->uiInDucking_threshold
1702            = pEnv->GetIntField(audioSettingObject,fid);
1703        M4OSA_TRACE1_1("ducking threshold = %d",
1704            pContext->mAudioSettings->uiInDucking_threshold);
1705
1706        fid = pEnv->GetFieldID(audioSettingClazz,"ducking_lowVolume","I");
1707        pContext->mAudioSettings->uiInDucking_lowVolume
1708            = pEnv->GetIntField(audioSettingObject,fid);
1709        M4OSA_TRACE1_1("ducking lowVolume = %d",
1710            pContext->mAudioSettings->uiInDucking_lowVolume);
1711
1712        fid = pEnv->GetFieldID(audioSettingClazz,"bInDucking_enable","Z");
1713        pContext->mAudioSettings->bInDucking_enable
1714            = pEnv->GetBooleanField(audioSettingObject,fid);
1715        M4OSA_TRACE1_1("ducking lowVolume = %d",
1716            pContext->mAudioSettings->bInDucking_enable);
1717    } else {
1718        if (pContext->mAudioSettings != M4OSA_NULL) {
1719            pContext->mAudioSettings->pFile = M4OSA_NULL;
1720            pContext->mAudioSettings->bRemoveOriginal = 0;
1721            pContext->mAudioSettings->uiNbChannels = 0;
1722            pContext->mAudioSettings->uiSamplingFrequency = 0;
1723            pContext->mAudioSettings->uiExtendedSamplingFrequency = 0;
1724            pContext->mAudioSettings->uiAddCts = 0;
1725            pContext->mAudioSettings->uiAddVolume = 0;
1726            pContext->mAudioSettings->beginCutMs = 0;
1727            pContext->mAudioSettings->endCutMs = 0;
1728               pContext->mAudioSettings->fileType = 0;
1729            pContext->mAudioSettings->bLoop = 0;
1730            pContext->mAudioSettings->uiInDucking_lowVolume  = 0;
1731            pContext->mAudioSettings->bInDucking_enable  = 0;
1732            pContext->mAudioSettings->uiBTChannelCount  = 0;
1733            pContext->mAudioSettings->uiInDucking_threshold = 0;
1734
1735            fid = pEnv->GetFieldID(engineClass,"mRegenerateAudio","Z");
1736            bool regenerateAudio = pEnv->GetBooleanField(thiz,fid);
1737            if(!regenerateAudio) {
1738                regenerateAudio = true;
1739                pEnv->SetBooleanField(thiz,fid,regenerateAudio);
1740            }
1741        }
1742    }
1743    if (pContext->pEditSettings != NULL )
1744    {
1745        result = pContext->mPreviewController->loadEditSettings(pContext->pEditSettings,
1746        pContext->mAudioSettings);
1747        videoEditJava_checkAndThrowRuntimeException(&needToBeLoaded, pEnv,
1748                                            (M4NO_ERROR != result), result);
1749
1750        pContext->mPreviewController->setJniCallback((void*)pContext,
1751         (jni_progress_callback_fct)jniPreviewProgressCallback);
1752
1753        j = 0;
1754        while (j < nbOverlays)
1755        {
1756            if (pContext->pEditSettings->Effects[pOverlayIndex[j]].xVSS.pFramingBuffer->pac_data != \
1757                M4OSA_NULL) {
1758                M4OSA_free((M4OSA_MemAddr32)pContext->pEditSettings->\
1759                Effects[pOverlayIndex[j]].xVSS.pFramingBuffer->pac_data);
1760                pContext->pEditSettings->\
1761                Effects[pOverlayIndex[j]].xVSS.pFramingBuffer->pac_data = M4OSA_NULL;
1762            }
1763            if (pContext->pEditSettings->Effects[pOverlayIndex[j]].xVSS.pFramingBuffer != M4OSA_NULL) {
1764                M4OSA_free((M4OSA_MemAddr32)pContext->pEditSettings->\
1765                Effects[pOverlayIndex[j]].xVSS.pFramingBuffer);
1766                pContext->pEditSettings->Effects[pOverlayIndex[j]].xVSS.pFramingBuffer = M4OSA_NULL;
1767            }
1768            j++;
1769        }
1770    }
1771    if (pOverlayIndex != M4OSA_NULL)
1772    {
1773        M4OSA_free((M4OSA_MemAddr32)pOverlayIndex);
1774        pOverlayIndex = M4OSA_NULL;
1775    }
1776    return;
1777}
1778
1779static void
1780videoEditor_startPreview(
1781                JNIEnv*                 pEnv,
1782                jobject                 thiz,
1783                jobject                 mSurface,
1784                jlong                   fromMs,
1785                jlong                   toMs,
1786                jint                    callbackInterval,
1787                jboolean                loop)
1788{
1789    bool needToBeLoaded = true;
1790    M4OSA_ERR result = M4NO_ERROR;
1791    VIDEOEDIT_LOG_FUNCTION(ANDROID_LOG_INFO, "VIDEO_EDITOR", "videoEditor_startPreview()");
1792
1793    ManualEditContext* pContext = M4OSA_NULL;
1794    // Get the context.
1795    pContext = (ManualEditContext*)videoEditClasses_getContext(&needToBeLoaded, pEnv, thiz);
1796
1797    // Make sure that the context was set.
1798    videoEditJava_checkAndThrowIllegalStateException(&needToBeLoaded, pEnv,
1799                                             (M4OSA_NULL == pContext),
1800                                             "not initialized");
1801
1802    videoEditJava_checkAndThrowIllegalStateException(&needToBeLoaded, pEnv,
1803                                     (M4OSA_NULL == pContext->mAudioSettings),
1804                                     "not initialized");
1805    // Make sure that the context was set.
1806    videoEditJava_checkAndThrowIllegalStateException(&needToBeLoaded, pEnv,
1807                                 (M4OSA_NULL == pContext->mPreviewController),
1808                                 "not initialized");
1809
1810    // Validate the mSurface parameter.
1811    videoEditJava_checkAndThrowIllegalArgumentException(&needToBeLoaded, pEnv,
1812                                                (NULL == mSurface),
1813                                                "mSurface is null");
1814
1815    jclass surfaceClass = pEnv->FindClass("android/view/Surface");
1816    videoEditJava_checkAndThrowIllegalStateException(&needToBeLoaded, pEnv,
1817                                             (M4OSA_NULL == surfaceClass),
1818                                             "not initialized");
1819    //jfieldID surface_native = pEnv->GetFieldID(surfaceClass, "mSurface", "I");
1820    jfieldID surface_native
1821        = pEnv->GetFieldID(surfaceClass, ANDROID_VIEW_SURFACE_JNI_ID, "I");
1822
1823    videoEditJava_checkAndThrowIllegalStateException(&needToBeLoaded, pEnv,
1824                                             (M4OSA_NULL == surface_native),
1825                                             "not initialized");
1826
1827    Surface* const p = (Surface*)pEnv->GetIntField(mSurface, surface_native);
1828
1829    sp<Surface> previewSurface = sp<Surface>(p);
1830
1831    result =  pContext->mPreviewController->setSurface(previewSurface);
1832    videoEditJava_checkAndThrowRuntimeException(&needToBeLoaded, pEnv,
1833        (M4NO_ERROR != result), result);
1834    VIDEOEDIT_LOG_FUNCTION(ANDROID_LOG_INFO, "VIDEO_EDITOR", "fromMs=%ld, toMs=%ld",
1835        (M4OSA_UInt32)fromMs, (M4OSA_Int32)toMs);
1836
1837    result = pContext->mPreviewController->startPreview((M4OSA_UInt32)fromMs,
1838                                                (M4OSA_Int32)toMs,
1839                                                (M4OSA_UInt16)callbackInterval,
1840                                                (M4OSA_Bool)loop);
1841    videoEditJava_checkAndThrowRuntimeException(&needToBeLoaded, pEnv, (M4NO_ERROR != result), result);
1842}
1843
1844
1845static jobject
1846videoEditor_getProperties(
1847                JNIEnv*                             pEnv,
1848                jobject                             thiz,
1849                jstring                             file)
1850{
1851    jobject object = M4OSA_NULL;
1852    object = videoEditProp_getProperties(pEnv,thiz,file);
1853
1854    return object;
1855
1856}
1857static int videoEditor_getPixels(
1858                    JNIEnv*                     env,
1859                    jobject                     thiz,
1860                    jstring                     path,
1861                    jintArray                   pixelArray,
1862                    M4OSA_UInt32                width,
1863                    M4OSA_UInt32                height,
1864                    M4OSA_UInt32                timeMS)
1865{
1866
1867    M4OSA_ERR       err = M4NO_ERROR;
1868    M4OSA_Context   mContext = M4OSA_NULL;
1869    jint*           m_dst32 = M4OSA_NULL;
1870
1871
1872    // Add a text marker (the condition must always be true).
1873    ADD_TEXT_MARKER_FUN(NULL != env)
1874
1875    const char *pString = env->GetStringUTFChars(path, NULL);
1876    if (pString == M4OSA_NULL) {
1877        if (env != NULL) {
1878            jniThrowException(env, "java/lang/RuntimeException", "Input string null");
1879        }
1880        return M4ERR_ALLOC;
1881    }
1882
1883    err = ThumbnailOpen(&mContext,(const M4OSA_Char*)pString, M4OSA_FALSE);
1884    if (err != M4NO_ERROR || mContext == M4OSA_NULL) {
1885        if (pString != NULL) {
1886            env->ReleaseStringUTFChars(path, pString);
1887        }
1888        if (env != NULL) {
1889            jniThrowException(env, "java/lang/RuntimeException", "ThumbnailOpen failed");
1890        }
1891    }
1892
1893    m_dst32 = env->GetIntArrayElements(pixelArray, NULL);
1894
1895    err = ThumbnailGetPixels32(mContext, (M4OSA_Int32 *)m_dst32, width,height,&timeMS);
1896    if (err != M4NO_ERROR ) {
1897        if (env != NULL) {
1898            jniThrowException(env, "java/lang/RuntimeException",\
1899                "ThumbnailGetPixels32 failed");
1900        }
1901    }
1902    env->ReleaseIntArrayElements(pixelArray, m_dst32, 0);
1903
1904    ThumbnailClose(mContext);
1905    if (pString != NULL) {
1906        env->ReleaseStringUTFChars(path, pString);
1907    }
1908
1909    return timeMS;
1910}
1911
1912static int videoEditor_getPixelsList(
1913                JNIEnv*                     env,
1914                jobject                     thiz,
1915                jstring                     path,
1916                jintArray                 pixelArray,
1917                M4OSA_UInt32             width,
1918                M4OSA_UInt32             height,
1919                M4OSA_UInt32             deltatimeMS,
1920                M4OSA_UInt32            noOfThumbnails,
1921                M4OSA_UInt32                startTime,
1922                M4OSA_UInt32                endTime)
1923{
1924
1925    M4OSA_ERR           err;
1926    M4OSA_Context       mContext = M4OSA_NULL;
1927    jint*               m_dst32;
1928    M4OSA_UInt32        timeMS = startTime;
1929    int                 arrayOffset = 0;
1930
1931
1932
1933    // Add a text marker (the condition must always be true).
1934    ADD_TEXT_MARKER_FUN(NULL != env)
1935
1936    const char *pString = env->GetStringUTFChars(path, NULL);
1937    if (pString == M4OSA_NULL) {
1938        if (env != NULL) {
1939            jniThrowException(env, "java/lang/RuntimeException", "Input string null");
1940        }
1941        return M4ERR_ALLOC;
1942    }
1943
1944    err = ThumbnailOpen(&mContext,(const M4OSA_Char*)pString, M4OSA_FALSE);
1945    if (err != M4NO_ERROR || mContext == M4OSA_NULL) {
1946        if (env != NULL) {
1947            jniThrowException(env, "java/lang/RuntimeException", "ThumbnailOpen failed");
1948        }
1949        if (pString != NULL) {
1950            env->ReleaseStringUTFChars(path, pString);
1951        }
1952        return err;
1953    }
1954
1955    m_dst32 = env->GetIntArrayElements(pixelArray, NULL);
1956
1957    do {
1958        err = ThumbnailGetPixels32(mContext, ((M4OSA_Int32 *)m_dst32 + arrayOffset),
1959            width,height,&timeMS);
1960        if (err != M4NO_ERROR ) {
1961            if (env != NULL) {
1962                jniThrowException(env, "java/lang/RuntimeException",\
1963                    "ThumbnailGetPixels32 failed");
1964            }
1965            return err;
1966        }
1967        timeMS += deltatimeMS;
1968        arrayOffset += (width * height * 4);
1969        noOfThumbnails--;
1970    } while(noOfThumbnails > 0);
1971
1972    env->ReleaseIntArrayElements(pixelArray, m_dst32, 0);
1973
1974    ThumbnailClose(mContext);
1975    if (pString != NULL) {
1976        env->ReleaseStringUTFChars(path, pString);
1977    }
1978
1979    return err;
1980
1981}
1982
1983static M4OSA_ERR
1984videoEditor_toUTF8Fct(
1985                M4OSA_Void*                         pBufferIn,
1986                M4OSA_UInt8*                        pBufferOut,
1987                M4OSA_UInt32*                       bufferOutSize)
1988{
1989    M4OSA_ERR    result = M4NO_ERROR;
1990    M4OSA_UInt32 length = 0;
1991
1992    VIDEOEDIT_LOG_FUNCTION(ANDROID_LOG_INFO, "VIDEO_EDITOR", "videoEditor_toUTF8Fct()");
1993
1994    // Determine the length of the input buffer.
1995    if (M4OSA_NULL != pBufferIn)
1996    {
1997        length = M4OSA_chrLength((M4OSA_Char *)pBufferIn);
1998    }
1999
2000    // Check if the output buffer is large enough to hold the input buffer.
2001    if ((*bufferOutSize) > length)
2002    {
2003        // Check if the input buffer is not M4OSA_NULL.
2004        if (M4OSA_NULL != pBufferIn)
2005        {
2006            // Copy the temp path, ignore the result.
2007            M4OSA_chrNCopy((M4OSA_Char *)pBufferOut, (M4OSA_Char *)pBufferIn, length);
2008        }
2009        else
2010        {
2011            // Set the output buffer to an empty string.
2012            (*(M4OSA_Char *)pBufferOut) = 0;
2013        }
2014    }
2015    else
2016    {
2017        // The buffer is too small.
2018        result = M4xVSSWAR_BUFFER_OUT_TOO_SMALL;
2019    }
2020
2021    // Return the buffer output size.
2022    (*bufferOutSize) = length + 1;
2023
2024    // Return the result.
2025    return(result);
2026}
2027
2028static M4OSA_ERR
2029videoEditor_fromUTF8Fct(
2030                M4OSA_UInt8*                        pBufferIn,
2031                M4OSA_Void*                         pBufferOut,
2032                M4OSA_UInt32*                       bufferOutSize)
2033{
2034    M4OSA_ERR    result = M4NO_ERROR;
2035    M4OSA_UInt32 length = 0;
2036
2037    VIDEOEDIT_LOG_FUNCTION(ANDROID_LOG_INFO, "VIDEO_EDITOR", "videoEditor_fromUTF8Fct()");
2038
2039    // Determine the length of the input buffer.
2040    if (M4OSA_NULL != pBufferIn)
2041    {
2042        length = M4OSA_chrLength((M4OSA_Char *)pBufferIn);
2043    }
2044
2045    // Check if the output buffer is large enough to hold the input buffer.
2046    if ((*bufferOutSize) > length)
2047    {
2048        // Check if the input buffer is not M4OSA_NULL.
2049        if (M4OSA_NULL != pBufferIn)
2050        {
2051            // Copy the temp path, ignore the result.
2052            M4OSA_chrNCopy((M4OSA_Char *)pBufferOut, (M4OSA_Char *)pBufferIn, length);
2053        }
2054        else
2055        {
2056            // Set the output buffer to an empty string.
2057            (*(M4OSA_Char *)pBufferOut) = 0;
2058        }
2059    }
2060    else
2061    {
2062        // The buffer is too small.
2063        result = M4xVSSWAR_BUFFER_OUT_TOO_SMALL;
2064    }
2065
2066    // Return the buffer output size.
2067    (*bufferOutSize) = length + 1;
2068
2069    // Return the result.
2070    return(result);
2071}
2072
2073static M4OSA_ERR
2074videoEditor_getTextRgbBufferFct(
2075                M4OSA_Void*                         pRenderingData,
2076                M4OSA_Void*                         pTextBuffer,
2077                M4OSA_UInt32                        textBufferSize,
2078                M4VIFI_ImagePlane**                 pOutputPlane)
2079{
2080    M4OSA_ERR result = M4NO_ERROR;
2081
2082    VIDEOEDIT_LOG_FUNCTION(ANDROID_LOG_INFO, "VIDEO_EDITOR", "videoEditor_getTextRgbBufferFct()");
2083
2084    // Return the result.
2085    return(result);
2086}
2087
2088static void
2089videoEditor_callOnProgressUpdate(
2090                ManualEditContext*                  pContext,
2091                int                                 task,
2092                int                                 progress)
2093{
2094    JNIEnv* pEnv = NULL;
2095
2096
2097    // Attach the current thread.
2098    pContext->pVM->AttachCurrentThread(&pEnv, NULL);
2099
2100
2101    // Call the on completion callback.
2102    pEnv->CallVoidMethod(pContext->engine, pContext->onProgressUpdateMethodId,
2103     videoEditJava_getEngineCToJava(task), progress);
2104
2105
2106    // Detach the current thread.
2107    pContext->pVM->DetachCurrentThread();
2108}
2109
2110static void
2111videoEditor_freeContext(
2112                JNIEnv*                             pEnv,
2113                ManualEditContext**                 ppContext)
2114{
2115    ManualEditContext* pContext = M4OSA_NULL;
2116
2117    VIDEOEDIT_LOG_FUNCTION(ANDROID_LOG_INFO, "VIDEO_EDITOR", "videoEditor_freeContext");
2118
2119    // Set the context pointer.
2120    pContext = (*ppContext);
2121
2122    // Check if the context was set.
2123    if (M4OSA_NULL != pContext)
2124    {
2125        // Check if a global reference to the engine object was set.
2126        if (NULL != pContext->engine)
2127        {
2128            // Free the global reference.
2129            pEnv->DeleteGlobalRef(pContext->engine);
2130            pContext->engine = NULL;
2131        }
2132
2133        // Check if the temp path was set.
2134        if (M4OSA_NULL != pContext->initParams.pTempPath)
2135        {
2136            // Free the memory allocated for the temp path.
2137            videoEditOsal_free(pContext->initParams.pTempPath);
2138            pContext->initParams.pTempPath = M4OSA_NULL;
2139        }
2140
2141        // Check if the file writer was set.
2142        if (M4OSA_NULL != pContext->initParams.pFileWritePtr)
2143        {
2144            // Free the memory allocated for the file writer.
2145            videoEditOsal_free(pContext->initParams.pFileWritePtr);
2146            pContext->initParams.pFileWritePtr = M4OSA_NULL;
2147        }
2148
2149        // Check if the file reader was set.
2150        if (M4OSA_NULL != pContext->initParams.pFileReadPtr)
2151        {
2152            // Free the memory allocated for the file reader.
2153            videoEditOsal_free(pContext->initParams.pFileReadPtr);
2154            pContext->initParams.pFileReadPtr = M4OSA_NULL;
2155        }
2156
2157        // Free the memory allocated for the context.
2158        videoEditOsal_free(pContext);
2159        pContext = M4OSA_NULL;
2160
2161        // Reset the context pointer.
2162        (*ppContext) = M4OSA_NULL;
2163    }
2164}
2165
2166static jobject
2167videoEditor_getVersion(
2168                JNIEnv*                             pEnv,
2169                jobject                             thiz)
2170{
2171    bool           isSuccessful          = true;
2172    jobject        version         = NULL;
2173    M4_VersionInfo versionInfo     = {0, 0, 0, 0};
2174    M4OSA_ERR      result          = M4NO_ERROR;
2175
2176    VIDEOEDIT_LOG_API(ANDROID_LOG_INFO, "VIDEO_EDITOR", "videoEditor_getVersion()");
2177
2178    versionInfo.m_structSize = sizeof(versionInfo);
2179    versionInfo.m_major = VIDEOEDITOR_VERSION_MAJOR;
2180    versionInfo.m_minor = VIDEOEDITOR_VERSION_MINOR;
2181    versionInfo.m_revision = VIDEOEDITOR_VERSION_REVISION;
2182
2183    VIDEOEDIT_LOG_API(ANDROID_LOG_INFO, "VIDEO_EDITOR", "videoEditor_getVersion() major %d,\
2184     minor %d, revision %d", versionInfo.m_major, versionInfo.m_minor, versionInfo.m_revision);
2185
2186    // Create a version object.
2187    videoEditClasses_createVersion(&isSuccessful, pEnv, &versionInfo, &version);
2188
2189    // Return the version object.
2190    return(version);
2191}
2192
2193static void
2194videoEditor_init(
2195                JNIEnv*                             pEnv,
2196                jobject                             thiz,
2197                jstring                             tempPath,
2198                jstring                             libraryPath)
2199{
2200    bool                  initialized            = true;
2201    ManualEditContext*    pContext               = M4OSA_NULL;
2202    VideoEditJava_EngineMethodIds methodIds              = {NULL};
2203    M4OSA_Char*           pLibraryPath           = M4OSA_NULL;
2204    M4OSA_Char*           pTextRendererPath      = M4OSA_NULL;
2205    M4OSA_UInt32          textRendererPathLength = 0;
2206    M4OSA_ERR             result                 = M4NO_ERROR;
2207
2208    VIDEOEDIT_LOG_API(ANDROID_LOG_INFO, "VIDEO_EDITOR", "videoEditor_init()");
2209
2210    // Add a text marker (the condition must always be true).
2211    ADD_TEXT_MARKER_FUN(NULL != pEnv)
2212
2213    // Get the context.
2214    pContext = (ManualEditContext*)videoEditClasses_getContext(&initialized, pEnv, thiz);
2215
2216    // Get the engine method ids.
2217    videoEditJava_getEngineMethodIds(&initialized, pEnv, &methodIds);
2218
2219    // Validate the tempPath parameter.
2220    videoEditJava_checkAndThrowIllegalArgumentException(&initialized, pEnv,
2221                                                (NULL == tempPath),
2222                                                "tempPath is null");
2223
2224    // Make sure that the context was not set already.
2225    videoEditJava_checkAndThrowIllegalStateException(&initialized, pEnv,
2226                                             (M4OSA_NULL != pContext),
2227                                             "already initialized");
2228
2229    // Check if the initialization succeeded (required because of dereferencing of psContext,
2230    // and freeing when initialization fails).
2231    if (initialized)
2232    {
2233        // Allocate a new context.
2234        pContext = new ManualEditContext;
2235
2236        // Check if the initialization succeeded (required because of dereferencing of psContext).
2237        //if (initialized)
2238        if (pContext != NULL)
2239        {
2240            // Set the state to not initialized.
2241            pContext->state = ManualEditState_NOT_INITIALIZED;
2242
2243            // Allocate a file read pointer structure.
2244            pContext->initParams.pFileReadPtr =
2245             (M4OSA_FileReadPointer*)videoEditOsal_alloc(&initialized, pEnv,
2246              sizeof(M4OSA_FileReadPointer), "FileReadPointer");
2247
2248            // Allocate a file write pointer structure.
2249            pContext->initParams.pFileWritePtr =
2250             (M4OSA_FileWriterPointer*)videoEditOsal_alloc(&initialized, pEnv,
2251              sizeof(M4OSA_FileWriterPointer), "FileWriterPointer");
2252
2253            // Get the temp path.
2254            M4OSA_Char* tmpString =
2255                (M4OSA_Char *)videoEditJava_getString(&initialized, pEnv, tempPath,
2256                NULL, M4OSA_NULL);
2257            pContext->initParams.pTempPath = (M4OSA_Char *)
2258                 M4OSA_malloc(M4OSA_chrLength(tmpString) + 1, 0x0,
2259                                                 (M4OSA_Char *)"tempPath");
2260            //initialize the first char. so that strcat works.
2261            M4OSA_Char *ptmpChar = (M4OSA_Char*)pContext->initParams.pTempPath;
2262            ptmpChar[0] = 0x00;
2263            M4OSA_chrNCat((M4OSA_Char*)pContext->initParams.pTempPath, tmpString, M4OSA_chrLength(tmpString));
2264            M4OSA_chrNCat((M4OSA_Char*)pContext->initParams.pTempPath, (M4OSA_Char*)"/", 1);
2265            M4OSA_free((M4OSA_MemAddr32)tmpString);
2266            pContext->mIsUpdateOverlay = false;
2267            pContext->mOverlayFileName = NULL;
2268        }
2269
2270        // Check if the initialization succeeded
2271        // (required because of dereferencing of pContext, pFileReadPtr and pFileWritePtr).
2272        if (initialized)
2273        {
2274
2275            // Initialize the OSAL file system function pointers.
2276            videoEditOsal_getFilePointers(pContext->initParams.pFileReadPtr ,
2277                                          pContext->initParams.pFileWritePtr);
2278
2279            // Set the UTF8 conversion functions.
2280            pContext->initParams.pConvToUTF8Fct   = videoEditor_toUTF8Fct;
2281            pContext->initParams.pConvFromUTF8Fct = videoEditor_fromUTF8Fct;
2282
2283            // Set the callback method ids.
2284            pContext->onProgressUpdateMethodId = methodIds.onProgressUpdate;
2285
2286            // Set the virtual machine.
2287            pEnv->GetJavaVM(&(pContext->pVM));
2288
2289            // Create a global reference to the engine object.
2290            pContext->engine = pEnv->NewGlobalRef(thiz);
2291
2292            // Check if the global reference could be created.
2293            videoEditJava_checkAndThrowRuntimeException(&initialized, pEnv,
2294             (NULL == pContext->engine), M4NO_ERROR);
2295        }
2296
2297        // Check if the initialization succeeded (required because of dereferencing of pContext).
2298        if (initialized)
2299        {
2300            // Log the API call.
2301            VIDEOEDIT_LOG_API(ANDROID_LOG_INFO, "VIDEO_EDITOR", "M4xVSS_Init()");
2302
2303            // Initialize the visual studio library.
2304            result = M4xVSS_Init(&pContext->engineContext, &pContext->initParams);
2305
2306            // Log the result.
2307            VIDEOEDIT_LOG_RESULT(ANDROID_LOG_INFO, "VIDEO_EDITOR",
2308             videoEditOsal_getResultString(result));
2309
2310            // Check if the library could be initialized.
2311            videoEditJava_checkAndThrowRuntimeException(&initialized, pEnv,
2312             (M4NO_ERROR != result), result);
2313        }
2314
2315        if(initialized)
2316        {
2317            pContext->mPreviewController = new VideoEditorPreviewController();
2318            videoEditJava_checkAndThrowIllegalStateException(&initialized, pEnv,
2319                                 (M4OSA_NULL == pContext->mPreviewController),
2320                                 "not initialized");
2321            pContext->mAudioSettings =
2322             (M4xVSS_AudioMixingSettings *)
2323             M4OSA_malloc(sizeof(M4xVSS_AudioMixingSettings),0x0,
2324             (M4OSA_Char *)"mAudioSettings");
2325            videoEditJava_checkAndThrowIllegalStateException(&initialized, pEnv,
2326                                     (M4OSA_NULL == pContext->mAudioSettings),
2327                                     "not initialized");
2328            pContext->mAudioSettings->pFile = M4OSA_NULL;
2329            pContext->mAudioSettings->bRemoveOriginal = 0;
2330            pContext->mAudioSettings->uiNbChannels = 0;
2331            pContext->mAudioSettings->uiSamplingFrequency = 0;
2332            pContext->mAudioSettings->uiExtendedSamplingFrequency = 0;
2333            pContext->mAudioSettings->uiAddCts = 0;
2334            pContext->mAudioSettings->uiAddVolume = 0;
2335            pContext->mAudioSettings->beginCutMs = 0;
2336            pContext->mAudioSettings->endCutMs = 0;
2337            pContext->mAudioSettings->fileType = 0;
2338            pContext->mAudioSettings->bLoop = 0;
2339            pContext->mAudioSettings->uiInDucking_lowVolume  = 0;
2340            pContext->mAudioSettings->bInDucking_enable  = 0;
2341            pContext->mAudioSettings->uiBTChannelCount  = 0;
2342            pContext->mAudioSettings->uiInDucking_threshold = 0;
2343        }
2344        // Check if the library could be initialized.
2345        if (initialized)
2346        {
2347            // Set the state to initialized.
2348            pContext->state = ManualEditState_INITIALIZED;
2349        }
2350
2351        // Set the context.
2352        videoEditClasses_setContext(&initialized, pEnv, thiz, (void* )pContext);
2353        pLibraryPath = M4OSA_NULL;
2354
2355        pContext->pEditSettings = M4OSA_NULL;
2356        // Cleanup if anything went wrong during initialization.
2357        if (!initialized)
2358        {
2359            // Free the context.
2360            videoEditor_freeContext(pEnv, &pContext);
2361        }
2362    }
2363}
2364
2365/*+ PROGRESS CB */
2366static
2367M4OSA_ERR videoEditor_processClip(
2368                            JNIEnv*  pEnv,
2369                            jobject  thiz,
2370                            int      unuseditemID) {
2371
2372    bool               loaded           = true;
2373    ManualEditContext* pContext         = NULL;
2374    M4OSA_UInt8        progress         = 0;
2375    M4OSA_UInt8        progressBase     = 0;
2376    M4OSA_UInt8        lastProgress     = 0;
2377    M4OSA_ERR          result           = M4NO_ERROR;
2378
2379    // Get the context.
2380    pContext = (ManualEditContext*)videoEditClasses_getContext(&loaded, pEnv, thiz);
2381
2382    // Make sure that the context was set.
2383    videoEditJava_checkAndThrowIllegalStateException(&loaded, pEnv,
2384                                             (M4OSA_NULL == pContext),
2385                                             "not initialized");
2386
2387    // We start in Analyzing state
2388    pContext->state = ManualEditState_INITIALIZED;
2389    M4OSA_ERR          completionResult = M4VSS3GPP_WAR_ANALYZING_DONE;
2390    ManualEditState    completionState  = ManualEditState_OPENED;
2391    ManualEditState    errorState       = ManualEditState_ANALYZING_ERROR;
2392
2393    // While analyzing progress goes from 0 to 50
2394    progressBase     = 0;
2395
2396    // Set the text rendering function.
2397    if (M4OSA_NULL != pContext->pTextRendererFunction)
2398    {
2399        // Use the text renderer function in the library.
2400        pContext->pEditSettings->xVSS.pTextRenderingFct = pContext->pTextRendererFunction;
2401    }
2402    else
2403    {
2404        // Use the internal text renderer function.
2405        pContext->pEditSettings->xVSS.pTextRenderingFct = videoEditor_getTextRgbBufferFct;
2406    }
2407
2408    // Send the command.
2409    LOGV("videoEditor_processClip ITEM %d Calling M4xVSS_SendCommand()", unuseditemID);
2410    result = M4xVSS_SendCommand(pContext->engineContext, pContext->pEditSettings);
2411    LOGV("videoEditor_processClip ITEM %d M4xVSS_SendCommand() returned 0x%x",
2412        unuseditemID, (unsigned int) result);
2413
2414    // Remove warnings indications (we only care about errors here)
2415    if ((result == M4VSS3GPP_WAR_TRANSCODING_NECESSARY)
2416        || (result == M4VSS3GPP_WAR_OUTPUTFILESIZE_EXCEED)) {
2417        result = M4NO_ERROR;
2418    }
2419
2420    // Send the first progress indication (=0)
2421    LOGV("VERY FIRST PROGRESS videoEditor_processClip ITEM %d Progress indication %d",
2422        unuseditemID, progress);
2423    pEnv->CallVoidMethod(pContext->engine, pContext->onProgressUpdateMethodId,
2424        unuseditemID, progress);
2425
2426    // Check if a task is being performed.
2427    // ??? ADD STOPPING MECHANISM
2428    LOGV("videoEditor_processClip Entering processing loop");
2429    while((result == M4NO_ERROR)
2430        &&(pContext->state!=ManualEditState_SAVED)
2431        &&(pContext->state!=ManualEditState_STOPPING)) {
2432
2433            // Perform the next processing step.
2434            //LOGV("LVME_processClip Entering M4xVSS_Step()");
2435            result = M4xVSS_Step(pContext->engineContext, &progress);
2436            //LOGV("LVME_processClip M4xVSS_Step() returned 0x%x", (unsigned int)result);
2437
2438            // Log the the 1 % .. 100 % progress after processing.
2439            progress = progressBase + progress/2;
2440            if (progress != lastProgress)
2441            {
2442                // Send a progress notification.
2443                LOGV("videoEditor_processClip ITEM %d Progress indication %d",
2444                    unuseditemID, progress);
2445                pEnv->CallVoidMethod(pContext->engine,
2446                    pContext->onProgressUpdateMethodId,
2447                    unuseditemID, progress);
2448                lastProgress = progress;
2449            }
2450
2451            // Check if processing has been completed.
2452            if (result == completionResult)
2453            {
2454                // Set the state to the completions state.
2455                pContext->state = completionState;
2456                LOGV("videoEditor_processClip ITEM %d STATE changed to %d",
2457                    unuseditemID, pContext->state);
2458
2459                // Reset progress indication, as we switch to next state
2460                lastProgress = 0;
2461
2462                // Reset error code, as we start a new round of processing
2463                result = M4NO_ERROR;
2464
2465                // Check if we are analyzing input
2466                if (pContext->state == ManualEditState_OPENED) {
2467                    // File is opened, we must start saving it
2468                    LOGV("videoEditor_processClip Calling M4xVSS_SaveStart()");
2469                    result = M4xVSS_SaveStart(pContext->engineContext,
2470                        (M4OSA_Char*)pContext->pEditSettings->pOutputFile,
2471                        (M4OSA_UInt32)pContext->pEditSettings->uiOutputPathSize);
2472                    LOGV("videoEditor_processClip ITEM %d SaveStart() returned 0x%x",
2473                        unuseditemID, (unsigned int) result);
2474
2475                    // Set the state to saving.
2476                    pContext->state  = ManualEditState_SAVING;
2477                    completionState  = ManualEditState_SAVED;
2478                    completionResult = M4VSS3GPP_WAR_SAVING_DONE;
2479                    errorState       = ManualEditState_SAVING_ERROR;
2480
2481                    // While saving progress goes from 50 to 100
2482                    progressBase     = 50;
2483                }
2484                // Check if we encoding is ongoing
2485                else if (pContext->state == ManualEditState_SAVED) {
2486                    if (progress != 100) {
2487                        // Send a progress notification.
2488                        progress = 100;
2489                        LOGI("videoEditor_processClip ITEM %d Last progress indication %d",
2490                            unuseditemID, progress);
2491                        pEnv->CallVoidMethod(pContext->engine,
2492                            pContext->onProgressUpdateMethodId,
2493                            unuseditemID, progress);
2494                    }
2495
2496                    // Stop the encoding.
2497                    LOGV("videoEditor_processClip Calling M4xVSS_SaveStop()");
2498                    result = M4xVSS_SaveStop(pContext->engineContext);
2499                    LOGV("videoEditor_processClip M4xVSS_SaveStop() returned 0x%x", result);
2500                }
2501                // Other states are unexpected
2502                else {
2503                    result = M4ERR_STATE;
2504                    LOGE("videoEditor_processClip ITEM %d State ERROR 0x%x",
2505                        unuseditemID, (unsigned int) result);
2506                }
2507            }
2508
2509            // Check if an error occurred.
2510            if (result != M4NO_ERROR)
2511            {
2512                // Set the state to the error state.
2513                pContext->state = errorState;
2514
2515                // Log the result.
2516                LOGE("videoEditor_processClip ITEM %d Processing ERROR 0x%x",
2517                    unuseditemID, (unsigned int) result);
2518            }
2519    }
2520
2521    // Return the error result
2522    LOGE("videoEditor_processClip ITEM %d END 0x%x", unuseditemID, (unsigned int) result);
2523    return result;
2524}
2525/*+ PROGRESS CB */
2526
2527static int
2528videoEditor_generateClip(
2529                JNIEnv*                             pEnv,
2530                jobject                             thiz,
2531                jobject                             settings) {
2532    bool               loaded   = true;
2533    ManualEditContext* pContext = M4OSA_NULL;
2534    M4OSA_ERR          result   = M4NO_ERROR;
2535
2536    LOGV("videoEditor_generateClip START");
2537
2538    // Get the context.
2539    pContext = (ManualEditContext*)videoEditClasses_getContext(&loaded, pEnv, thiz);
2540
2541    Mutex::Autolock autoLock(pContext->mLock);
2542
2543    // Validate the settings parameter.
2544    videoEditJava_checkAndThrowIllegalArgumentException(&loaded, pEnv,
2545                                                (NULL == settings),
2546                                                "settings is null");
2547
2548    // Make sure that the context was set.
2549    videoEditJava_checkAndThrowIllegalStateException(&loaded, pEnv,
2550                                             (M4OSA_NULL == pContext),
2551                                             "not initialized");
2552
2553    // Load the clip settings
2554    LOGV("videoEditor_generateClip Calling videoEditor_loadSettings");
2555    videoEditor_loadSettings(pEnv, thiz, settings);
2556    LOGV("videoEditor_generateClip videoEditor_loadSettings returned");
2557
2558    // Generate the clip
2559    LOGV("videoEditor_generateClip Calling LVME_processClip");
2560    result = videoEditor_processClip(pEnv, thiz, 0 /*item id is unused*/);
2561    LOGV("videoEditor_generateClip videoEditor_processClip returned 0x%x", result);
2562
2563    if (pContext->state != ManualEditState_INITIALIZED) {
2564        // Free up memory (whatever the result)
2565        videoEditor_unloadSettings(pEnv, thiz);
2566    }
2567
2568    LOGV("videoEditor_generateClip END 0x%x", (unsigned int) result);
2569    return result;
2570}
2571
2572static void
2573videoEditor_loadSettings(
2574                JNIEnv*                             pEnv,
2575                jobject                             thiz,
2576                jobject                             settings)
2577{
2578    bool               needToBeLoaded   = true;
2579    ManualEditContext* pContext = M4OSA_NULL;
2580
2581    VIDEOEDIT_LOG_API(ANDROID_LOG_INFO, "VIDEO_EDITOR", "videoEditor_loadSettings()");
2582
2583    // Add a code marker (the condition must always be true).
2584    ADD_CODE_MARKER_FUN(NULL != pEnv)
2585
2586    // Get the context.
2587    pContext = (ManualEditContext*)videoEditClasses_getContext(&needToBeLoaded,
2588                                                                pEnv, thiz);
2589
2590    // Validate the settings parameter.
2591    videoEditJava_checkAndThrowIllegalArgumentException(&needToBeLoaded, pEnv,
2592                                                (NULL == settings),
2593                                                "settings is null");
2594
2595    // Make sure that the context was set.
2596    videoEditJava_checkAndThrowIllegalStateException(&needToBeLoaded, pEnv,
2597                                             (M4OSA_NULL == pContext),
2598                                             "not initialized");
2599
2600    // Check if the context is valid (required because the context is dereferenced).
2601    if (needToBeLoaded)
2602    {
2603        // Make sure that we are in a correct state.
2604        videoEditJava_checkAndThrowIllegalStateException(&needToBeLoaded, pEnv,
2605                             (pContext->state != ManualEditState_INITIALIZED),
2606                             "settings already loaded");
2607
2608        // Retrieve the edit settings.
2609        if(pContext->pEditSettings != M4OSA_NULL) {
2610            videoEditClasses_freeEditSettings(&pContext->pEditSettings);
2611            pContext->pEditSettings = M4OSA_NULL;
2612        }
2613        videoEditClasses_getEditSettings(&needToBeLoaded, pEnv, settings,
2614            &pContext->pEditSettings,true);
2615    }
2616
2617    // Check if the edit settings could be retrieved.
2618    if (needToBeLoaded)
2619    {
2620        // Log the edit settings.
2621        VIDEOEDIT_LOG_API(ANDROID_LOG_INFO, "VIDEO_EDITOR", "inside load settings");
2622        VIDEOEDIT_LOG_EDIT_SETTINGS(pContext->pEditSettings);
2623    }
2624    LOGV("videoEditor_loadSettings END");
2625}
2626
2627
2628
2629static void
2630videoEditor_unloadSettings(
2631                JNIEnv*                             pEnv,
2632                jobject                             thiz)
2633{
2634    bool               needToBeUnLoaded = true;
2635    ManualEditContext* pContext = M4OSA_NULL;
2636    M4OSA_ERR          result   = M4NO_ERROR;
2637
2638    VIDEOEDIT_LOG_API(ANDROID_LOG_INFO, "VIDEO_EDITOR", "videoEditor_unloadSettings()");
2639
2640    // Get the context.
2641    pContext = (ManualEditContext*)videoEditClasses_getContext(&needToBeUnLoaded, pEnv, thiz);
2642
2643    // Make sure that the context was set.
2644    videoEditJava_checkAndThrowIllegalStateException(&needToBeUnLoaded, pEnv,
2645                                             (M4OSA_NULL == pContext),
2646                                             "not initialized");
2647
2648    // Check if the context is valid (required because the context is dereferenced).
2649    if (needToBeUnLoaded)
2650    {
2651        LOGV("videoEditor_unloadSettings state %d", pContext->state);
2652        // Make sure that we are in a correct state.
2653        videoEditJava_checkAndThrowIllegalStateException(&needToBeUnLoaded, pEnv,
2654                     ((pContext->state != ManualEditState_ANALYZING      ) &&
2655                      (pContext->state != ManualEditState_ANALYZING_ERROR) &&
2656                      (pContext->state != ManualEditState_OPENED         ) &&
2657                      (pContext->state != ManualEditState_SAVING_ERROR   ) &&
2658                      (pContext->state != ManualEditState_SAVED          ) &&
2659                      (pContext->state != ManualEditState_STOPPING       ) ),
2660                     "videoEditor_unloadSettings no load settings in progress");
2661    }
2662
2663    // Check if we are in a correct state.
2664    if (needToBeUnLoaded)
2665    {
2666        // Check if the thread could be stopped.
2667        if (needToBeUnLoaded)
2668        {
2669            // Close the command.
2670            LOGV("videoEditor_unloadSettings Calling M4xVSS_CloseCommand()");
2671            result = M4xVSS_CloseCommand(pContext->engineContext);
2672            LOGV("videoEditor_unloadSettings M4xVSS_CloseCommand() returned 0x%x",
2673                (unsigned int)result);
2674
2675            // Check if the command could be closed.
2676            videoEditJava_checkAndThrowRuntimeException(&needToBeUnLoaded, pEnv,
2677             (M4NO_ERROR != result), result);
2678        }
2679
2680        // Check if the command could be closed.
2681        if (needToBeUnLoaded)
2682        {
2683            // Free the edit settings.
2684            //videoEditClasses_freeEditSettings(&pContext->pEditSettings);
2685
2686            // Reset the thread result.
2687            pContext->threadResult = M4NO_ERROR;
2688
2689            // Reset the thread progress.
2690            pContext->threadProgress = 0;
2691
2692            // Set the state to initialized.
2693            pContext->state = ManualEditState_INITIALIZED;
2694        }
2695    }
2696}
2697
2698static void
2699videoEditor_stopEncoding(
2700                JNIEnv*                             pEnv,
2701                jobject                             thiz)
2702{
2703    bool               stopped  = true;
2704    ManualEditContext* pContext = M4OSA_NULL;
2705    M4OSA_ERR          result   = M4NO_ERROR;
2706
2707    LOGV("videoEditor_stopEncoding START");
2708
2709    // Get the context.
2710    pContext = (ManualEditContext*)videoEditClasses_getContext(&stopped, pEnv, thiz);
2711
2712    // Change state and get Lock
2713    // This will ensure the generateClip function exits
2714    pContext->state = ManualEditState_STOPPING;
2715    Mutex::Autolock autoLock(pContext->mLock);
2716
2717    // Make sure that the context was set.
2718    videoEditJava_checkAndThrowIllegalStateException(&stopped, pEnv,
2719                                             (M4OSA_NULL == pContext),
2720                                             "not initialized");
2721
2722    if (stopped) {
2723
2724        // Check if the command should be closed.
2725        if (pContext->state != ManualEditState_INITIALIZED)
2726        {
2727            // Close the command.
2728            LOGV("videoEditor_stopEncoding Calling M4xVSS_CloseCommand()");
2729            result = M4xVSS_CloseCommand(pContext->engineContext);
2730            LOGV("videoEditor_stopEncoding M4xVSS_CloseCommand() returned 0x%x",
2731                (unsigned int)result);
2732        }
2733
2734        // Check if the command could be closed.
2735        videoEditJava_checkAndThrowRuntimeException(&stopped, pEnv,
2736            (M4NO_ERROR != result), result);
2737
2738        // Free the edit settings.
2739        videoEditClasses_freeEditSettings(&pContext->pEditSettings);
2740
2741        // Set the state to initialized.
2742        pContext->state = ManualEditState_INITIALIZED;
2743    }
2744
2745}
2746
2747static void
2748videoEditor_release(
2749                JNIEnv*                             pEnv,
2750                jobject                             thiz)
2751{
2752    bool               released = true;
2753    ManualEditContext* pContext = M4OSA_NULL;
2754    M4OSA_ERR          result   = M4NO_ERROR;
2755
2756    VIDEOEDIT_LOG_API(ANDROID_LOG_INFO, "VIDEO_EDITOR", "videoEditor_release()");
2757
2758    // Add a text marker (the condition must always be true).
2759    ADD_TEXT_MARKER_FUN(NULL != pEnv)
2760
2761    // Get the context.
2762    pContext = (ManualEditContext*)videoEditClasses_getContext(&released, pEnv, thiz);
2763
2764    // If context is not set, return (we consider release already happened)
2765    if (pContext == NULL) {
2766        LOGV("videoEditor_release Nothing to do, context is aleady NULL");
2767        return;
2768    }
2769
2770
2771    // Check if the context is valid (required because the context is dereferenced).
2772    if (released)
2773    {
2774        if (pContext->state != ManualEditState_INITIALIZED)
2775        {
2776            // Change state and get Lock
2777            // This will ensure the generateClip function exits if it is running
2778            pContext->state = ManualEditState_STOPPING;
2779            Mutex::Autolock autoLock(pContext->mLock);
2780        }
2781
2782        // Reset the context.
2783        videoEditClasses_setContext(&released, pEnv, thiz, (void *)M4OSA_NULL);
2784
2785        // Check if the command should be closed.
2786        if (pContext->state != ManualEditState_INITIALIZED)
2787        {
2788            // Close the command.
2789            LOGV("videoEditor_release Calling M4xVSS_CloseCommand() state =%d",
2790                pContext->state);
2791            result = M4xVSS_CloseCommand(pContext->engineContext);
2792            LOGV("videoEditor_release M4xVSS_CloseCommand() returned 0x%x",
2793                (unsigned int)result);
2794
2795            // Check if the command could be closed.
2796            videoEditJava_checkAndThrowRuntimeException(&released, pEnv,
2797                (M4NO_ERROR != result), result);
2798        }
2799
2800        // Cleanup the engine.
2801        LOGV("videoEditor_release Calling M4xVSS_CleanUp()");
2802        result = M4xVSS_CleanUp(pContext->engineContext);
2803        LOGV("videoEditor_release M4xVSS_CleanUp() returned 0x%x", (unsigned int)result);
2804
2805        // Check if the cleanup succeeded.
2806        videoEditJava_checkAndThrowRuntimeException(&released, pEnv,
2807            (M4NO_ERROR != result), result);
2808
2809        // Free the edit settings.
2810        videoEditClasses_freeEditSettings(&pContext->pEditSettings);
2811        pContext->pEditSettings = M4OSA_NULL;
2812
2813
2814        if(pContext->mPreviewController != M4OSA_NULL)
2815        {
2816            delete pContext->mPreviewController;
2817            pContext->mPreviewController = M4OSA_NULL;
2818        }
2819
2820        // Free the context.
2821        if(pContext->mAudioSettings != M4OSA_NULL)
2822        {
2823            M4OSA_free((M4OSA_MemAddr32)pContext->mAudioSettings);
2824            pContext->mAudioSettings = M4OSA_NULL;
2825        }
2826        videoEditor_freeContext(pEnv, &pContext);
2827    }
2828}
2829
2830static int
2831videoEditor_registerManualEditMethods(
2832                JNIEnv*                             pEnv)
2833{
2834    int result = -1;
2835
2836    VIDEOEDIT_LOG_FUNCTION(ANDROID_LOG_INFO, "VIDEO_EDITOR",
2837     "videoEditor_registerManualEditMethods()");
2838
2839    // Look up the engine class
2840    jclass engineClazz = pEnv->FindClass(MANUAL_EDIT_ENGINE_CLASS_NAME);
2841
2842    // Clear any resulting exceptions.
2843    pEnv->ExceptionClear();
2844
2845    // Check if the engine class was found.
2846    if (NULL != engineClazz)
2847    {
2848        // Register all the methods.
2849        if (pEnv->RegisterNatives(engineClazz, gManualEditMethods,
2850                sizeof(gManualEditMethods) / sizeof(gManualEditMethods[0])) == JNI_OK)
2851        {
2852            // Success.
2853            result = 0;
2854        }
2855    }
2856
2857    // Return the result.
2858    return(result);
2859}
2860
2861/*******Audio Graph*******/
2862
2863static M4OSA_UInt32 getDecibelSound(M4OSA_UInt32 value)
2864{
2865    int dbSound = 1;
2866
2867    if (value == 0) return 0;
2868
2869    if (value > 0x4000 && value <= 0x8000) // 32768
2870        dbSound = 90;
2871    else if (value > 0x2000 && value <= 0x4000) // 16384
2872        dbSound = 84;
2873    else if (value > 0x1000 && value <= 0x2000) // 8192
2874        dbSound = 78;
2875    else if (value > 0x0800 && value <= 0x1000) // 4028
2876        dbSound = 72;
2877    else if (value > 0x0400 && value <= 0x0800) // 2048
2878        dbSound = 66;
2879    else if (value > 0x0200 && value <= 0x0400) // 1024
2880        dbSound = 60;
2881    else if (value > 0x0100 && value <= 0x0200) // 512
2882        dbSound = 54;
2883    else if (value > 0x0080 && value <= 0x0100) // 256
2884        dbSound = 48;
2885    else if (value > 0x0040 && value <= 0x0080) // 128
2886        dbSound = 42;
2887    else if (value > 0x0020 && value <= 0x0040) // 64
2888        dbSound = 36;
2889    else if (value > 0x0010 && value <= 0x0020) // 32
2890        dbSound = 30;
2891    else if (value > 0x0008 && value <= 0x0010) //16
2892        dbSound = 24;
2893    else if (value > 0x0007 && value <= 0x0008) //8
2894        dbSound = 24;
2895    else if (value > 0x0003 && value <= 0x0007) // 4
2896        dbSound = 18;
2897    else if (value > 0x0001 && value <= 0x0003) //2
2898        dbSound = 12;
2899    else if (value > 0x000 && value == 0x0001) // 1
2900        dbSound = 6;
2901    else
2902        dbSound = 0;
2903
2904    return dbSound;
2905}
2906
2907typedef struct
2908{
2909    M4OSA_UInt8      *m_dataAddress;
2910    M4OSA_UInt32    m_bufferSize;
2911} M4AM_Buffer;
2912
2913
2914M4OSA_UInt8 logLookUp[256] = {
29150,120,137,146,154,159,163,167,171,173,176,178,181,182,184,186,188,189,190,192,193,
2916194,195,196,198,199,199,200,201,202,203,204,205,205,206,207,207,208,209,209,210,
2917211,211,212,212,213,213,214,215,215,216,216,216,217,217,218,218,219,219,220,220,
2918220,221,221,222,222,222,223,223,223,224,224,224,225,225,225,226,226,226,227,227,
2919227,228,228,228,229,229,229,229,230,230,230,230,231,231,231,232,232,232,232,233,
2920233,233,233,233,234,234,234,234,235,235,235,235,236,236,236,236,236,237,237,237,
2921237,237,238,238,238,238,238,239,239,239,239,239,240,240,240,240,240,240,241,241,
2922241,241,241,241,242,242,242,242,242,242,243,243,243,243,243,243,244,244,244,244,
2923244,244,245,245,245,245,245,245,245,246,246,246,246,246,246,246,247,247,247,247,
2924247,247,247,247,248,248,248,248,248,248,248,249,249,249,249,249,249,249,249,250,
2925250,250,250,250,250,250,250,250,251,251,251,251,251,251,251,251,252,252,252,252,
2926252,252,252,252,252,253,253,253,253,253,253,253,253,253,253,254,254,254,254,254,
2927254,254,254,254,255,255,255,255,255,255,255,255,255,255,255};
2928
2929M4OSA_ERR M4MA_generateAudioGraphFile(JNIEnv* pEnv, M4OSA_Char* pInputFileURL,
2930                     M4OSA_Char* pOutFileURL,
2931                     M4OSA_UInt32 samplesPerValue,
2932                     M4OSA_UInt32 channels,
2933                     M4OSA_UInt32 frameDuration,
2934                     ManualEditContext* pContext)
2935{
2936    M4OSA_ERR           err;
2937    M4OSA_Context       outFileHandle = M4OSA_NULL;
2938    M4OSA_Context       inputFileHandle = M4OSA_NULL;
2939    M4AM_Buffer         bufferIn = {0, 0};
2940    M4OSA_UInt32        peakVolumeDbValue = 0;
2941    M4OSA_UInt32        samplesCountInBytes= 0 , numBytesToRead = 0, index = 0;
2942    M4OSA_UInt32        writeCount = 0, samplesCountBigEndian = 0, volumeValuesCount = 0;
2943    M4OSA_Int32         seekPos = 0;
2944    M4OSA_UInt32        fileSize = 0;
2945    M4OSA_UInt32        totalBytesRead = 0;
2946    M4OSA_UInt32        prevProgress = 0;
2947    bool                threadStarted = true;
2948
2949    int dbValue = 0;
2950    M4OSA_Int16 *ptr16 ;
2951
2952    jclass engineClass = pEnv->FindClass(MANUAL_EDIT_ENGINE_CLASS_NAME);
2953    videoEditJava_checkAndThrowIllegalStateException(&threadStarted, pEnv,
2954                                             (M4OSA_NULL == engineClass),
2955                                             "not initialized");
2956
2957    /* register the call back function pointer */
2958    pContext->onAudioGraphProgressUpdateMethodId =
2959            pEnv->GetMethodID(engineClass, "onAudioGraphExtractProgressUpdate", "(IZ)V");
2960
2961
2962    /* ENTER */
2963    VIDEOEDIT_LOG_API(ANDROID_LOG_INFO, "VIDEO_EDITOR", "ENTER - M4MA_generateAudioGraphFile");
2964    VIDEOEDIT_LOG_API(ANDROID_LOG_INFO, "VIDEO_EDITOR",
2965            "Audio Graph samplesPerValue %d channels %d", samplesPerValue, channels);
2966
2967    /******************************************************************************
2968        OPEN INPUT AND OUTPUT FILES
2969    *******************************************************************************/
2970    err = M4OSA_fileReadOpen (&inputFileHandle, pInputFileURL, M4OSA_kFileRead);
2971    if (inputFileHandle == M4OSA_NULL) {
2972        VIDEOEDIT_LOG_ERROR(ANDROID_LOG_INFO, "VIDEO_EDITOR",
2973            "M4MA_generateAudioGraphFile: Cannot open input file 0x%lx", err);
2974        return err;
2975    }
2976
2977    /* get the file size for progress */
2978    err = M4OSA_fileReadGetOption(inputFileHandle, M4OSA_kFileReadGetFileSize,
2979                                (M4OSA_Void**)&fileSize);
2980    if ( err != M4NO_ERROR) {
2981        //LVMEL_LOG_ERROR("M4MA_generateAudioGraphFile : File write failed \n");
2982        jniThrowException(pEnv, "java/lang/IOException", "file size get option failed");
2983        //return -1;
2984    }
2985
2986    err = M4OSA_fileWriteOpen (&outFileHandle,(M4OSA_Char*) pOutFileURL,
2987        M4OSA_kFileCreate | M4OSA_kFileWrite);
2988    if (outFileHandle == M4OSA_NULL) {
2989        if (inputFileHandle != NULL)
2990        {
2991            M4OSA_fileReadClose(inputFileHandle);
2992        }
2993        return err;
2994    }
2995
2996    /******************************************************************************
2997        PROCESS THE SAMPLES
2998    *******************************************************************************/
2999    samplesCountInBytes = (samplesPerValue * sizeof(M4OSA_UInt16) * channels);
3000
3001    bufferIn.m_dataAddress = (M4OSA_UInt8*)M4OSA_malloc(samplesCountInBytes*sizeof(M4OSA_UInt16), 0,
3002    (M4OSA_Char*)"AudioGraph" );
3003    if ( bufferIn.m_dataAddress != M4OSA_NULL) {
3004        bufferIn.m_bufferSize = samplesCountInBytes*sizeof(M4OSA_UInt16);
3005    } else {
3006        VIDEOEDIT_LOG_ERROR(ANDROID_LOG_INFO, "VIDEO_EDITOR",
3007            "M4MA_generateAudioGraphFile: Malloc failed for bufferIn.m_dataAddress 0x%lx",
3008            M4ERR_ALLOC);
3009        return M4ERR_ALLOC;
3010    }
3011    /* sample to be converted to BIG endian ; store the frame duration */
3012    samplesCountBigEndian = ((frameDuration>>24)&0xff) | // move byte 3 to byte 0
3013                    ((frameDuration<<8)&0xff0000) | // move byte 1 to byte 2
3014                    ((frameDuration>>8)&0xff00) | // move byte 2 to byte 1
3015                    ((frameDuration<<24)&0xff000000); // byte 0 to byte 3
3016
3017    /* write the samples per value supplied to out file */
3018    err = M4OSA_fileWriteData (outFileHandle, (M4OSA_MemAddr8)&samplesCountBigEndian,
3019        sizeof(M4OSA_UInt32) );
3020    if (err != M4NO_ERROR) {
3021        jniThrowException(pEnv, "java/lang/IOException", "file write failed");
3022    }
3023
3024
3025    /* write UIn32 value 0 for no of values as place holder */
3026    samplesCountBigEndian = 0; /* reusing local var */
3027    err = M4OSA_fileWriteData (outFileHandle, (M4OSA_MemAddr8)&samplesCountBigEndian,
3028        sizeof(M4OSA_UInt32) );
3029    if (err != M4NO_ERROR) {
3030        jniThrowException(pEnv, "java/lang/IOException", "file write failed");
3031    }
3032
3033    /* loop until EOF */
3034    do
3035    {
3036        M4OSA_memset((M4OSA_MemAddr8)bufferIn.m_dataAddress,bufferIn.m_bufferSize, 0);
3037
3038        numBytesToRead = samplesCountInBytes;
3039
3040        err =  M4OSA_fileReadData(  inputFileHandle,
3041                                    (M4OSA_MemAddr8)bufferIn.m_dataAddress,
3042                                    &numBytesToRead );
3043
3044        if (err != M4NO_ERROR) {
3045            // if out value of bytes-read is 0, break
3046            if ( numBytesToRead == 0) {
3047                VIDEOEDIT_LOG_ERROR(ANDROID_LOG_INFO, "VIDEO_EDITOR", "numBytesToRead 0x%lx",
3048                numBytesToRead);
3049                break; /* stop if file is empty or EOF */
3050            }
3051        }
3052
3053        ptr16 = (M4OSA_Int16*)bufferIn.m_dataAddress;
3054
3055        peakVolumeDbValue = 0;
3056        index = 0;
3057
3058        // loop through half the lenght frame bytes read 'cause its 16 bits samples
3059        while (index < (numBytesToRead / 2)) {
3060            /* absolute values of 16 bit sample */
3061            if (ptr16[index] < 0) {
3062                ptr16[index] = -(ptr16[index]);
3063            }
3064            peakVolumeDbValue = (peakVolumeDbValue > (M4OSA_UInt32)ptr16[index] ?\
3065             peakVolumeDbValue : (M4OSA_UInt32)ptr16[index]);
3066            index++;
3067        }
3068
3069        // move 7 bits , ignore sign bit
3070        dbValue = (peakVolumeDbValue >> 7);
3071        dbValue = logLookUp[(M4OSA_UInt8)dbValue];
3072
3073        err = M4OSA_fileWriteData (outFileHandle, (M4OSA_MemAddr8)&dbValue, sizeof(M4OSA_UInt8) );
3074        if (err != M4NO_ERROR) {
3075            VIDEOEDIT_LOG_ERROR(ANDROID_LOG_INFO, "VIDEO_EDITOR",
3076             "M4MA_generateAudioGraphFile : File write failed");
3077            break;
3078        }
3079
3080        volumeValuesCount ++;
3081        totalBytesRead += numBytesToRead;
3082
3083        if ((((totalBytesRead*100)/fileSize)) != prevProgress) {
3084            if ( (pContext->threadProgress != prevProgress) && (prevProgress != 0 )) {
3085                //pContext->threadProgress = prevProgress;
3086                //onWveformProgressUpdateMethodId(prevProgress, 0);
3087                //LVME_callAudioGraphOnProgressUpdate(pContext, 0, prevProgress);
3088            pEnv->CallVoidMethod(pContext->engine,
3089                                 pContext->onAudioGraphProgressUpdateMethodId,
3090                                 prevProgress, 0);
3091            VIDEOEDIT_LOG_API(ANDROID_LOG_INFO, "VIDEO_EDITOR", "pContext->threadProgress %d",
3092                             prevProgress);
3093            }
3094        }
3095        prevProgress = (((totalBytesRead*100)/fileSize));
3096
3097    } while (numBytesToRead != 0);
3098
3099    VIDEOEDIT_LOG_ERROR(ANDROID_LOG_INFO, "VIDEO_EDITOR", "loop 0x%lx", volumeValuesCount);
3100
3101    /* if some error occured in fwrite */
3102    if (numBytesToRead != 0) {
3103        //err = -1;
3104        jniThrowException(pEnv, "java/lang/IOException", "numBytesToRead != 0 ; file write failed");
3105    }
3106
3107    /* write the count in place holder after seek */
3108    seekPos = sizeof(M4OSA_UInt32);
3109    err = M4OSA_fileWriteSeek(outFileHandle, M4OSA_kFileSeekBeginning,
3110            &seekPos /* after samples per value */);
3111    if ( err != M4NO_ERROR) {
3112        jniThrowException(pEnv, "java/lang/IOException", "file seek failed");
3113    } else {
3114        volumeValuesCount = ((volumeValuesCount>>24)&0xff) | // move byte 3 to byte 0
3115                    ((volumeValuesCount<<8)&0xff0000) | // move byte 1 to byte 2
3116                    ((volumeValuesCount>>8)&0xff00) |  // move byte 2 to byte 1
3117                    ((volumeValuesCount<<24)&0xff000000); // byte 0 to byte 3
3118
3119        err = M4OSA_fileWriteData (outFileHandle, (M4OSA_MemAddr8)&volumeValuesCount,
3120                                    sizeof(M4OSA_UInt32) );
3121        if ( err != M4NO_ERROR) {
3122            jniThrowException(pEnv, "java/lang/IOException", "file write failed");
3123        }
3124    }
3125
3126    /******************************************************************************
3127    CLOSE AND FREE ALLOCATIONS
3128    *******************************************************************************/
3129    M4OSA_free((M4OSA_MemAddr32)bufferIn.m_dataAddress);
3130    M4OSA_fileReadClose(inputFileHandle);
3131    M4OSA_fileWriteClose(outFileHandle);
3132    /* final finish callback */
3133    pEnv->CallVoidMethod(pContext->engine, pContext->onAudioGraphProgressUpdateMethodId, 100, 0);
3134
3135    /* EXIT */
3136    VIDEOEDIT_LOG_API(ANDROID_LOG_INFO, "VIDEO_EDITOR", "EXIT - M4MA_generateAudioGraphFile");
3137
3138    return err;
3139}
3140
3141static int videoEditor_generateAudioWaveFormSync (JNIEnv*  pEnv, jobject thiz,
3142                                                  jstring pcmfilePath,
3143                                                  jstring outGraphfilePath,
3144                                                  jint frameDuration, jint channels,
3145                                                  jint samplesCount)
3146{
3147    M4OSA_ERR result = M4NO_ERROR;
3148    ManualEditContext* pContext = M4OSA_NULL;
3149    bool needToBeLoaded = true;
3150
3151    VIDEOEDIT_LOG_API(ANDROID_LOG_INFO, "VIDEO_EDITOR",
3152        "videoEditor_generateAudioWaveFormSync() ");
3153
3154    /* Get the context. */
3155    pContext = (ManualEditContext*)videoEditClasses_getContext(&needToBeLoaded, pEnv, thiz);
3156    if (pContext == M4OSA_NULL) {
3157        VIDEOEDIT_LOG_API(ANDROID_LOG_INFO, "VIDEO_EDITOR",
3158            "videoEditor_generateAudioWaveFormSync() - pContext is NULL ");
3159    }
3160
3161    VIDEOEDIT_LOG_API(ANDROID_LOG_INFO, "VIDEO_EDITOR",
3162        "videoEditor_generateAudioWaveFormSync Retrieving pStringOutAudioGraphFile");
3163
3164    const char *pPCMFilePath = pEnv->GetStringUTFChars(pcmfilePath, NULL);
3165    if (pPCMFilePath == M4OSA_NULL) {
3166        if (pEnv != NULL) {
3167            jniThrowException(pEnv, "java/lang/RuntimeException",
3168                "Input string PCMFilePath is null");
3169        }
3170    }
3171
3172    const char *pStringOutAudioGraphFile = pEnv->GetStringUTFChars(outGraphfilePath, NULL);
3173    if (pStringOutAudioGraphFile == M4OSA_NULL) {
3174        if (pEnv != NULL) {
3175            jniThrowException(pEnv, "java/lang/RuntimeException",
3176                "Input string outGraphfilePath is null");
3177        }
3178    }
3179
3180    VIDEOEDIT_LOG_API(ANDROID_LOG_INFO, "VIDEO_EDITOR",
3181        "videoEditor_generateAudioWaveFormSync Generate the waveform data %s %d %d %d",
3182        pStringOutAudioGraphFile, frameDuration, channels, samplesCount);
3183
3184    /* Generate the waveform */
3185    result = M4MA_generateAudioGraphFile(pEnv, (M4OSA_Char*) pPCMFilePath,
3186        (M4OSA_Char*) pStringOutAudioGraphFile,
3187        (M4OSA_UInt32) samplesCount,
3188        (M4OSA_UInt32) channels,
3189        (M4OSA_UInt32)frameDuration,
3190        pContext);
3191
3192    if (pStringOutAudioGraphFile != NULL) {
3193        pEnv->ReleaseStringUTFChars(outGraphfilePath, pStringOutAudioGraphFile);
3194    }
3195
3196    VIDEOEDIT_LOG_FUNCTION(ANDROID_LOG_INFO, "VIDEO_EDITOR",
3197        "videoEditor_generateAudioWaveFormSync pContext->bSkipState ");
3198
3199    return result;
3200}
3201
3202/******** End Audio Graph *******/
3203jint JNI_OnLoad(
3204                JavaVM*                             pVm,
3205                void*                               pReserved)
3206{
3207    void* pEnv         = NULL;
3208    bool  needToBeInitialized = true;
3209    jint  result      = -1;
3210
3211    VIDEOEDIT_LOG_FUNCTION(ANDROID_LOG_INFO, "VIDEO_EDITOR", "JNI_OnLoad()");
3212
3213    // Add a text marker (the condition must always be true).
3214    ADD_TEXT_MARKER_FUN(NULL != pVm)
3215
3216    // Check the JNI version.
3217    if (pVm->GetEnv(&pEnv, JNI_VERSION_1_4) == JNI_OK)
3218    {
3219        // Add a code marker (the condition must always be true).
3220        ADD_CODE_MARKER_FUN(NULL != pEnv)
3221
3222        // Register the manual edit JNI methods.
3223        if (videoEditor_registerManualEditMethods((JNIEnv*)pEnv) == 0)
3224        {
3225            // Initialize the classes.
3226            videoEditClasses_init(&needToBeInitialized, (JNIEnv*)pEnv);
3227            if (needToBeInitialized)
3228            {
3229                // Success, return valid version number.
3230                result = JNI_VERSION_1_4;
3231            }
3232        }
3233    }
3234
3235    // Return the result.
3236    return(result);
3237}
3238
3239