summaryrefslogtreecommitdiffstats
path: root/libvideoeditor/vss/src/M4VSS3GPP_ClipAnalysis.c
blob: e2c6d7a062ec73b495a9a72a9d190ced516e7952 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/**
 ******************************************************************************
 * @file    M4VSS3GPP_ClipAnalysis.c
 * @brief    Implementation of functions related to analysis of input clips
 * @note    All functions in this file are static, i.e. non public
 ******************************************************************************
 */

/****************/
/*** Includes ***/
/****************/

#include "NXPSW_CompilerSwitches.h"
/**
 *    Our headers */
#include "M4VSS3GPP_API.h"
#include "M4VSS3GPP_ErrorCodes.h"
#include "M4VSS3GPP_InternalTypes.h"
#include "M4VSS3GPP_InternalFunctions.h"
#include "M4VSS3GPP_InternalConfig.h"
#include "M4VD_EXTERNAL_Interface.h"


/**
 *    OSAL headers */
#include "M4OSA_Memory.h" /* OSAL memory management */
#include "M4OSA_Debug.h"  /* OSAL debug management */

/**
 ******************************************************************************
 * M4OSA_ERR M4VSS3GPP_editAnalyseClip()
 * @brief    This function allows checking if a clip is compatible with VSS 3GPP editing
 * @note    It also fills a ClipAnalysis structure, which can be used to check if two
 *        clips are compatible
 * @param    pClip                (IN) File descriptor of the input 3GPP/MP3 clip file.
 * @param    pClipProperties        (IN) Pointer to a valid ClipProperties structure.
 * @param    FileType            (IN) Type of the input file (.3gp, .amr, .mp3)
 * @return    M4NO_ERROR:            No error
 * @return    M4ERR_PARAMETER:    At least one parameter is M4OSA_NULL (debug only)
 * @return   M4VSS3GPP_ERR_H263_PROFILE_NOT_SUPPORTED
 * @return   M4VSS3GPP_ERR_INVALID_CLIP_ANALYSIS_VERSION
 * @return   M4VSS3GPP_ERR_AMR_EDITING_UNSUPPORTED
 * @return   M4VSS3GPP_ERR_EDITING_UNSUPPORTED_H263_PROFILE
 * @return   M4VSS3GPP_ERR_EDITING_UNSUPPORTED_MPEG4_PROFILE
 * @return   M4VSS3GPP_ERR_EDITING_UNSUPPORTED_MPEG4_RVLC
 * @return   M4VSS3GPP_ERR_UNSUPPORTED_INPUT_VIDEO_FORMAT
 * @return   M4VSS3GPP_ERR_EDITING_NO_SUPPORTED_VIDEO_STREAM_IN_FILE
 * @return   M4VSS3GPP_ERR_EDITING_UNSUPPORTED_AUDIO_FORMAT
 * @return   M4VSS3GPP_ERR_EDITING_NO_SUPPORTED_STREAM_IN_FILE
 ******************************************************************************
 */
M4OSA_ERR M4VSS3GPP_editAnalyseClip( M4OSA_Void *pClip,
                                    M4VIDEOEDITING_FileType FileType,
                                    M4VIDEOEDITING_ClipProperties *pClipProperties,
                                    M4OSA_FileReadPointer *pFileReadPtrFct )
{
    M4OSA_ERR err;
    M4VSS3GPP_ClipContext *pClipContext;
    M4VSS3GPP_ClipSettings ClipSettings;

    M4OSA_TRACE3_2(
        "M4VSS3GPP_editAnalyseClip called with pClip=0x%x, pClipProperties=0x%x",
        pClip, pClipProperties);

    /**
    *    Check input parameter */
    M4OSA_DEBUG_IF2((M4OSA_NULL == pClip), M4ERR_PARAMETER,
        "M4VSS3GPP_editAnalyseClip: pClip is M4OSA_NULL");
    M4OSA_DEBUG_IF2((M4OSA_NULL == pClipProperties), M4ERR_PARAMETER,
        "M4VSS3GPP_editAnalyseClip: pClipProperties is M4OSA_NULL");

    /**
    * Build dummy clip settings, in order to use the editClipOpen function */
    ClipSettings.pFile = pClip;
    ClipSettings.FileType = FileType;
    ClipSettings.uiBeginCutTime = 0;
    ClipSettings.uiEndCutTime = 0;

    /* Clip properties not build yet, set at least this flag */
    ClipSettings.ClipProperties.bAnalysed = M4OSA_FALSE;

    /**
    * Open the clip in fast open mode */
    err = M4VSS3GPP_intClipInit(&pClipContext, pFileReadPtrFct);

    if( M4NO_ERROR != err )
    {
        M4OSA_TRACE1_1(
            "M4VSS3GPP_editAnalyseClip: M4VSS3GPP_intClipInit() returns 0x%x!",
            err);

        /**
        * Free the clip */
        if( M4OSA_NULL != pClipContext )
        {
            M4VSS3GPP_intClipCleanUp(pClipContext);
        }
        return err;
    }

    err = M4VSS3GPP_intClipOpen(pClipContext, &ClipSettings, M4OSA_FALSE,
        M4OSA_TRUE, M4OSA_TRUE);

    if( M4NO_ERROR != err )
    {
        M4OSA_TRACE1_1(
            "M4VSS3GPP_editAnalyseClip: M4VSS3GPP_intClipOpen() returns 0x%x!",
            err);

        M4VSS3GPP_intClipCleanUp(pClipContext);

        /**
        * Here it is better to return the Editing specific error code */
        if( ( ((M4OSA_UInt32)M4ERR_DECODER_H263_PROFILE_NOT_SUPPORTED) == err)
            || (((M4OSA_UInt32)M4ERR_DECODER_H263_NOT_BASELINE) == err) )
        {
            M4OSA_TRACE1_0(
                "M4VSS3GPP_editAnalyseClip:\
                M4VSS3GPP_intClipOpen() returns M4VSS3GPP_ERR_H263_PROFILE_NOT_SUPPORTED");
            return M4VSS3GPP_ERR_H263_PROFILE_NOT_SUPPORTED;
        }
        return err;
    }

    /**
    * Analyse the clip */
    if(M4VIDEOEDITING_kFileType_ARGB8888 != pClipContext->pSettings->FileType) {
        err = M4VSS3GPP_intBuildAnalysis(pClipContext, pClipProperties);

        if( M4NO_ERROR != err )
        {
            M4OSA_TRACE1_1(
                "M4VSS3GPP_editAnalyseClip: M4VSS3GPP_intBuildAnalysis() returns 0x%x!",
                err);

            /**
            * Free the clip */
            M4VSS3GPP_intClipCleanUp(pClipContext);
            return err;
        }
    }
    /**
    * Free the clip */
    err = M4VSS3GPP_intClipClose(pClipContext);

    if( M4NO_ERROR != err )
    {
        M4OSA_TRACE1_1(
            "M4VSS3GPP_editAnalyseClip: M4VSS_intClipClose() returns 0x%x!",
            err);
        M4VSS3GPP_intClipCleanUp(pClipContext);
        return err;
    }

    M4VSS3GPP_intClipCleanUp(pClipContext);

    /**
    * Check the clip is compatible with VSS editing */
    if(M4VIDEOEDITING_kFileType_ARGB8888 != ClipSettings.FileType) {
        err = M4VSS3GPP_intCheckClipCompatibleWithVssEditing(pClipProperties);

        if( M4NO_ERROR != err )
        {
            M4OSA_TRACE1_1(
                "M4VSS3GPP_editAnalyseClip:\
                M4VSS3GPP_intCheckClipCompatibleWithVssEditing() returns 0x%x!",
                err);
            return err;
        }
    }
    /**
    * Return with no error */
    M4OSA_TRACE3_0("M4VSS3GPP_editAnalyseClip(): returning M4NO_ERROR");
    return M4NO_ERROR;
}

/**
 ******************************************************************************
 * M4OSA_ERR M4VSS3GPP_editCheckClipCompatibility()
 * @brief    This function allows checking if two clips are compatible with each other for
 *        VSS 3GPP editing assembly feature.
 * @note
 * @param    pClip1Properties        (IN) Clip analysis of the first clip
 * @param    pClip2Properties        (IN) Clip analysis of the second clip
 * @return    M4NO_ERROR:            No error
 * @return    M4ERR_PARAMETER:    At least one parameter is M4OSA_NULL (debug only)
 * @return    M4VSS3GPP_ERR_INVALID_CLIP_ANALYSIS_VERSION
 * @return    M4VSS3GPP_ERR_INCOMPATIBLE_VIDEO_FORMAT
 * @return    M4VSS3GPP_ERR_INCOMPATIBLE_VIDEO_FRAME_SIZE
 * @return    M4VSS3GPP_ERR_INCOMPATIBLE_VIDEO_TIME_SCALE
 * @return    M4VSS3GPP_ERR_INCOMPATIBLE_VIDEO_DATA_PARTITIONING
 * @return  M4VSS3GPP_ERR_UNSUPPORTED_MP3_ASSEMBLY
 * @return  M4VSS3GPP_ERR_UNSUPPORTED_INPUT_VIDEO_FORMAT
 ******************************************************************************
 */
M4OSA_ERR M4VSS3GPP_editCheckClipCompatibility( M4VIDEOEDITING_ClipProperties *pClip1Properties,
                                                M4VIDEOEDITING_ClipProperties *pClip2Properties )
{
    M4OSA_ERR err = M4NO_ERROR;
    M4OSA_ERR video_err = M4NO_ERROR;
    M4OSA_ERR audio_err = M4NO_ERROR;

    M4OSA_Bool bClip1IsAAC = M4OSA_FALSE;
    M4OSA_Bool bClip2IsAAC = M4OSA_FALSE;

    M4OSA_TRACE3_2("M4VSS3GPP_editCheckClipCompatibility called with pClip1Analysis=0x%x,\
                   pClip2Analysis=0x%x", pClip1Properties, pClip2Properties);

    /**
    *    Check input parameter */
    M4OSA_DEBUG_IF2((M4OSA_NULL == pClip1Properties), M4ERR_PARAMETER,
        "M4VSS3GPP_editCheckClipCompatibility: pClip1Properties is M4OSA_NULL");
    M4OSA_DEBUG_IF2((M4OSA_NULL == pClip2Properties), M4ERR_PARAMETER,
        "M4VSS3GPP_editCheckClipCompatibility: pClip2Properties is M4OSA_NULL");

    if( ( M4VIDEOEDITING_kFileType_MP3 == pClip1Properties->FileType)
        || (M4VIDEOEDITING_kFileType_AMR == pClip1Properties->FileType) )
    {
        if( pClip1Properties != pClip2Properties )
        {
            M4OSA_TRACE1_0(
                "M4VSS3GPP_editCheckClipCompatibility: MP3 CAN ONLY BE CUT,\
                returning M4VSS3GPP_ERR_UNSUPPORTED_MP3_ASSEMBLY");
            return M4VSS3GPP_ERR_UNSUPPORTED_MP3_ASSEMBLY;
        }
        else
        {
            /* We are in VSS Splitter mode */
            goto audio_analysis;
        }
    }

    /********** Audio ************/

audio_analysis:
    if( M4VIDEOEDITING_kNoneAudio != pClip1Properties->
        AudioStreamType ) /**< if there is an audio stream */
    {
        /**
        * Check audio format is AAC */
        switch( pClip1Properties->AudioStreamType )
        {
            case M4VIDEOEDITING_kAAC:
            case M4VIDEOEDITING_kAACplus:
            case M4VIDEOEDITING_keAACplus:
                bClip1IsAAC = M4OSA_TRUE;
                break;
            default:
                break;
        }
    }

    if( M4VIDEOEDITING_kNoneAudio != pClip2Properties->
        AudioStreamType ) /**< if there is an audio stream */
    {
        /**
        * Check audio format is AAC */
        switch( pClip2Properties->AudioStreamType )
        {
            case M4VIDEOEDITING_kAAC:
            case M4VIDEOEDITING_kAACplus:
            case M4VIDEOEDITING_keAACplus:
                bClip2IsAAC = M4OSA_TRUE;
                break;
            default:
                break;
        }
    }

    /**
    * If there is no audio, the clips are compatibles ... */
    if( ( pClip1Properties->AudioStreamType != M4VIDEOEDITING_kNoneAudio)
        && (pClip2Properties->AudioStreamType != M4VIDEOEDITING_kNoneAudio) )
    {
        /**
        * Check both clips have same audio stream type
        * And let_s say AAC, AAC+ and eAAC+ are mixable */
        if( ( pClip1Properties->AudioStreamType
            != pClip2Properties->AudioStreamType)
            && (( M4OSA_FALSE == bClip1IsAAC) || (M4OSA_FALSE == bClip2IsAAC)) )
        {
            M4OSA_TRACE1_0(
                "M4VSS3GPP_editCheckClipCompatibility:\
                Clips don't have the same Audio Stream Type");

            audio_err = M4VSS3GPP_WAR_INCOMPATIBLE_AUDIO_STREAM_TYPE;
            goto analysis_done;
        }

        /**
        * Check both clips have same number of channels */
        if( pClip1Properties->uiNbChannels != pClip2Properties->uiNbChannels )
        {
            M4OSA_TRACE1_0(
                "M4VSS3GPP_editCheckClipCompatibility: Clips don't have the same Nb of Channels");
            audio_err = M4VSS3GPP_WAR_INCOMPATIBLE_AUDIO_NB_OF_CHANNELS;
            goto analysis_done;
        }

        /**
        * Check both clips have same sampling frequency */
        if( pClip1Properties->uiSamplingFrequency
            != pClip2Properties->uiSamplingFrequency )
        {
            M4OSA_TRACE1_0(
                "M4VSS3GPP_editCheckClipCompatibility:\
                Clips don't have the same Sampling Frequency");
            audio_err = M4VSS3GPP_WAR_INCOMPATIBLE_AUDIO_SAMPLING_FREQUENCY;
            goto analysis_done;
        }
    }

    pClip2Properties->bAudioIsCompatibleWithMasterClip = M4OSA_TRUE;

    /**
    * Return with no error */

analysis_done:
    if( video_err != M4NO_ERROR )
        return video_err;

    if( audio_err != M4NO_ERROR )
        return audio_err;

    M4OSA_TRACE3_0(
        "M4VSS3GPP_editCheckClipCompatibility(): returning M4NO_ERROR");
    return M4NO_ERROR;
}

/**
 ******************************************************************************
 * M4OSA_ERR M4VSS3GPP_intBuildAnalysis()
 * @brief    Get video and audio properties from the clip streams
 * @note    This function must return fatal errors only (errors that should not happen
 *        in the final integrated product).
 * @param   pClipCtxt            (IN) internal clip context
 * @param    pClipProperties        (OUT) Pointer to a valid ClipProperties structure.
 * @return    M4NO_ERROR:            No error
 ******************************************************************************
 */
M4OSA_ERR M4VSS3GPP_intBuildAnalysis( M4VSS3GPP_ClipContext *pClipCtxt,
                                     M4VIDEOEDITING_ClipProperties *pClipProperties )
{
    M4OSA_ERR err;
    M4DECODER_MPEG4_DecoderConfigInfo DecConfigInfo;
    M4DECODER_VideoSize dummySize;
    M4DECODER_AVCProfileLevel AVCProfle;

    pClipProperties->bAnalysed = M4OSA_FALSE;

    /**
    * Reset video characteristics */
    pClipProperties->VideoStreamType = M4VIDEOEDITING_kNoneVideo;
    pClipProperties->uiClipVideoDuration = 0;
    pClipProperties->uiVideoBitrate = 0;
    pClipProperties->uiVideoMaxAuSize = 0;
    pClipProperties->uiVideoWidth = 0;
    pClipProperties->uiVideoHeight = 0;
    pClipProperties->uiVideoTimeScale = 0;
    pClipProperties->fAverageFrameRate = 0.0;
    pClipProperties->uiVideoProfile =
        M4VIDEOEDITING_VIDEO_UNKNOWN_PROFILE;
    pClipProperties->uiVideoLevel =
        M4VIDEOEDITING_VIDEO_UNKNOWN_LEVEL;
    pClipProperties->bMPEG4dataPartition = M4OSA_FALSE;
    pClipProperties->bMPEG4rvlc = M4OSA_FALSE;
    pClipProperties->bMPEG4resynchMarker = M4OSA_FALSE;

    memset((void *) &pClipProperties->ftyp,0,
        sizeof(pClipProperties->ftyp));

    /**
    * Video Analysis */
    if( M4OSA_NULL != pClipCtxt->pVideoStream )
    {
        pClipProperties->uiVideoWidth = pClipCtxt->pVideoStream->m_videoWidth;
        pClipProperties->uiVideoHeight = pClipCtxt->pVideoStream->m_videoHeight;
        pClipProperties->fAverageFrameRate =
            pClipCtxt->pVideoStream->m_averageFrameRate;

        switch( pClipCtxt->pVideoStream->m_basicProperties.m_streamType )
        {
            case M4DA_StreamTypeVideoMpeg4:

                pClipProperties->VideoStreamType = M4VIDEOEDITING_kMPEG4;

   /* This issue is so incredibly stupid that it's depressing. Basically, a file can be analysed
   outside of any context (besides that of the clip itself), so that for instance two clips can
   be checked for compatibility before allocating an edit context for editing them. But this
   means there is no way in heck to pass an external video decoder (to begin with) to this
   function, as they work by being registered in an existing context; furthermore, it is actually
   pretty overkill to use a full decoder for that, moreso a HARDWARE decoder just to get the
   clip config info. In fact, the hardware itself doesn't provide this service, in the case of a
   HW decoder, the shell builds the config info itself, so we don't need the actual decoder, only
   a detached functionality of it. So in case HW/external decoders may be present, we instead use
   directly the DSI parsing function of the shell HW decoder (which we know to be present, since
   HW decoders are possible) to get the config info. Notice this function is used even if the
   software decoder is actually present and even if it will end up being actually used: figuring
   out the config does not involve actual decoding nor the particularities of a specific decoder,
   it's the fact that it's MPEG4 that matters, so it should not be functionally any different
   from the way it was done before (and it's light enough for performance not to be any problem
         whatsoever). */

                err = M4DECODER_EXTERNAL_ParseVideoDSI(pClipCtxt->pVideoStream->
                            m_basicProperties.m_pDecoderSpecificInfo,
                            pClipCtxt->pVideoStream->m_basicProperties.m_decoderSpecificInfoSize,
                            &DecConfigInfo, &dummySize);

                if( M4NO_ERROR != err )
                {
                    M4OSA_TRACE1_1(
                        "M4VSS3GPP_intBuildAnalysis():\
                        M4DECODER_EXTERNAL_ParseVideoDSI returns 0x%08X", err);
                    return err;
                }

                pClipProperties->uiVideoTimeScale =
                    DecConfigInfo.uiTimeScale;
                pClipProperties->bMPEG4dataPartition =
                    DecConfigInfo.bDataPartition;
                pClipProperties->bMPEG4rvlc =
                    DecConfigInfo.bUseOfRVLC;
                pClipProperties->bMPEG4resynchMarker =
                    DecConfigInfo.uiUseOfResynchMarker;
                err = getMPEG4ProfileAndLevel(DecConfigInfo.uiProfile,
                            &(pClipProperties->uiVideoProfile),
                            &(pClipProperties->uiVideoLevel));
               if (M4NO_ERROR != err) {
                    M4OSA_TRACE1_1("M4VSS3GPP_intBuildAnalysis(): \
                         getMPEG4ProfileAndLevel returns 0x%08X", err);
                    return err;
                }
                break;

            case M4DA_StreamTypeVideoH263:

                pClipProperties->VideoStreamType = M4VIDEOEDITING_kH263;
                /* H263 time scale is always 30000 */
                pClipProperties->uiVideoTimeScale = 30000;

                err = getH263ProfileAndLevel(pClipCtxt->pVideoStream->
                            m_basicProperties.m_pDecoderSpecificInfo,
                            pClipCtxt->pVideoStream->m_basicProperties.m_decoderSpecificInfoSize,
                            &pClipProperties->uiVideoProfile,
                            &pClipProperties->uiVideoLevel);
                if (M4NO_ERROR != err) {
                    M4OSA_TRACE1_1("M4VSS3GPP_intBuildAnalysis(): \
                         getH263ProfileAndLevel returns 0x%08X", err);
                    return err;
                }
                break;

            case M4DA_StreamTypeVideoMpeg4Avc:

                pClipProperties->VideoStreamType = M4VIDEOEDITING_kH264;
                err = getAVCProfileAndLevel(pClipCtxt->pVideoStream->
                            m_basicProperties.m_pDecoderSpecificInfo,
                            pClipCtxt->pVideoStream->m_basicProperties.m_decoderSpecificInfoSize,
                            &pClipProperties->uiVideoProfile,
                            &pClipProperties->uiVideoLevel);
                if (M4NO_ERROR != err) {
                    M4OSA_TRACE1_1("M4VSS3GPP_intBuildAnalysis(): \
                         getAVCProfileAndLevel returns 0x%08X", err);
                    return err;
                }
                break;

            default:
                M4OSA_TRACE1_1(
                    "M4VSS3GPP_intBuildAnalysis: unknown input video format (0x%x),\
                     returning M4NO_ERROR",
                    pClipCtxt->pVideoStream->m_basicProperties.m_streamType);

                 /** We do not return error here.
                   *  The video format compatibility check will be done latter */
                return M4NO_ERROR;
        }

        pClipProperties->uiClipVideoDuration =
            (M4OSA_UInt32)pClipCtxt->pVideoStream->m_basicProperties.m_duration;
        pClipProperties->uiVideoMaxAuSize =
            pClipCtxt->pVideoStream->m_basicProperties.m_maxAUSize;

        /* if video bitrate not available retrieve an estimation of the overall bitrate */
        pClipProperties->uiVideoBitrate =
            (M4OSA_UInt32)pClipCtxt->pVideoStream->
            m_basicProperties.m_averageBitRate;

        if( 0 == pClipProperties->uiVideoBitrate )
        {
            pClipCtxt->ShellAPI.m_pReader->m_pFctGetOption(
                pClipCtxt->pReaderContext, M4READER_kOptionID_Bitrate,
                &pClipProperties->uiVideoBitrate);

            if( M4OSA_NULL != pClipCtxt->pAudioStream )
            {
                /* we get the overall bitrate, substract the audio bitrate if any */
                pClipProperties->uiVideoBitrate -=
                    pClipCtxt->pAudioStream->m_basicProperties.m_averageBitRate;
            }
        }
    }

    /**
    * Reset audio characteristics */
    pClipProperties->AudioStreamType = M4VIDEOEDITING_kNoneAudio;
    pClipProperties->uiClipAudioDuration = 0;
    pClipProperties->uiAudioBitrate = 0;
    pClipProperties->uiAudioMaxAuSize = 0;
    pClipProperties->uiNbChannels = 0;
    pClipProperties->uiSamplingFrequency = 0;
    pClipProperties->uiExtendedSamplingFrequency = 0;
    pClipProperties->uiDecodedPcmSize = 0;

    /**
    * Audio Analysis */
    if( M4OSA_NULL != pClipCtxt->pAudioStream )
    {
        switch( pClipCtxt->pAudioStream->m_basicProperties.m_streamType )
        {
            case M4DA_StreamTypeAudioAmrNarrowBand:

                pClipProperties->AudioStreamType = M4VIDEOEDITING_kAMR_NB;
                break;

            case M4DA_StreamTypeAudioAac:

                pClipProperties->AudioStreamType = M4VIDEOEDITING_kAAC;
                break;

            case M4DA_StreamTypeAudioMp3:

                pClipProperties->AudioStreamType = M4VIDEOEDITING_kMP3;
                break;

            case M4DA_StreamTypeAudioEvrc:

                pClipProperties->AudioStreamType = M4VIDEOEDITING_kEVRC;
                break;

            case M4DA_StreamTypeAudioPcm:

                pClipProperties->AudioStreamType = M4VIDEOEDITING_kPCM;
                break;

            default:

                M4OSA_TRACE1_1(
                    "M4VSS3GPP_intBuildAnalysis: unknown input audio format (0x%x),\
                    returning M4NO_ERROR!",
                    pClipCtxt->pAudioStream->m_basicProperties.m_streamType);
                return
                    M4NO_ERROR; /**< We do not return error here.
                                The audio format compatibility check will be done latter */
        }

        pClipProperties->uiAudioMaxAuSize =
            pClipCtxt->pAudioStream->m_basicProperties.m_maxAUSize;
        pClipProperties->uiClipAudioDuration =
            (M4OSA_UInt32)pClipCtxt->pAudioStream->m_basicProperties.m_duration;

        pClipProperties->uiNbChannels = pClipCtxt->pAudioStream->m_nbChannels;
        pClipProperties->uiSamplingFrequency =
            pClipCtxt->pAudioStream->m_samplingFrequency;
        pClipProperties->uiDecodedPcmSize =
            pClipCtxt->pAudioStream->m_byteFrameLength
            * pClipCtxt->pAudioStream->m_byteSampleSize
            * pClipCtxt->pAudioStream->m_nbChannels;

        /**
        * Bugfix P4ME00001128: With some IMTC files, the AMR bit rate is 0 kbps
        according the GetProperties function */
        pClipProperties->uiAudioBitrate =
            (M4OSA_UInt32)pClipCtxt->pAudioStream->
            m_basicProperties.m_averageBitRate;

        if( 0 == pClipProperties->uiAudioBitrate )
        {
            if( M4VIDEOEDITING_kAMR_NB == pClipProperties->AudioStreamType )
            {
                /**
                *Better returning a guessed 12.2 kbps value than a sure-to-be-false 0 kbps value!*/
                pClipProperties->uiAudioBitrate = M4VSS3GPP_AMR_DEFAULT_BITRATE;
            }
            else if( M4VIDEOEDITING_kEVRC == pClipProperties->AudioStreamType )
            {
                /**
                *Better returning a guessed 9.2 kbps value than a sure-to-be-false 0 kbps value!*/
                pClipProperties->uiAudioBitrate =
                    M4VSS3GPP_EVRC_DEFAULT_BITRATE;
            }
            else
            {
                pClipCtxt->ShellAPI.m_pReader->m_pFctGetOption(
                    pClipCtxt->pReaderContext, M4READER_kOptionID_Bitrate,
                    &pClipProperties->uiAudioBitrate);

                if( M4OSA_NULL != pClipCtxt->pVideoStream )
                {
                    /* we get the overall bitrate, substract the video bitrate if any */
                    pClipProperties->uiAudioBitrate -= pClipCtxt->pVideoStream->
                        m_basicProperties.m_averageBitRate;
                }
            }
        }

        /* New aac properties */
        if( M4DA_StreamTypeAudioAac
            == pClipCtxt->pAudioStream->m_basicProperties.m_streamType )
        {
            pClipProperties->uiNbChannels = pClipCtxt->AacProperties.aNumChan;
            pClipProperties->uiSamplingFrequency =
                pClipCtxt->AacProperties.aSampFreq;

            if( pClipCtxt->AacProperties.aSBRPresent )
            {
                pClipProperties->AudioStreamType = M4VIDEOEDITING_kAACplus;
                pClipProperties->uiExtendedSamplingFrequency =
                    pClipCtxt->AacProperties.aExtensionSampFreq;
            }

            if( pClipCtxt->AacProperties.aPSPresent )
            {
                pClipProperties->AudioStreamType = M4VIDEOEDITING_keAACplus;
            }
        }
    }

    /* Get 'ftyp' atom */
    err = pClipCtxt->ShellAPI.m_pReader->m_pFctGetOption(
        pClipCtxt->pReaderContext,
        M4READER_kOptionID_3gpFtypBox, &pClipProperties->ftyp);

    /**
    * We write the VSS 3GPP version in the clip analysis to be sure the integrator doesn't
    * mix older analysis results with newer libraries */
    pClipProperties->Version[0] = M4VIDEOEDITING_VERSION_MAJOR;
    pClipProperties->Version[1] = M4VIDEOEDITING_VERSION_MINOR;
    pClipProperties->Version[2] = M4VIDEOEDITING_VERSION_REVISION;

    pClipProperties->FileType = pClipCtxt->pSettings->FileType;

    if( pClipProperties->uiClipVideoDuration
        > pClipProperties->uiClipAudioDuration )
        pClipProperties->uiClipDuration = pClipProperties->uiClipVideoDuration;
    else
        pClipProperties->uiClipDuration = pClipProperties->uiClipAudioDuration;

    /* Reset compatibility chart */
    pClipProperties->bVideoIsEditable = M4OSA_FALSE;
    pClipProperties->bAudioIsEditable = M4OSA_FALSE;
    pClipProperties->bVideoIsCompatibleWithMasterClip = M4OSA_FALSE;
    pClipProperties->bAudioIsCompatibleWithMasterClip = M4OSA_FALSE;

    /* Analysis successfully completed */
    pClipProperties->bAnalysed = M4OSA_TRUE;

    /**
    * Return with no error */
    M4OSA_TRACE3_0("M4VSS3GPP_intBuildAnalysis(): returning M4NO_ERROR");
    return M4NO_ERROR;
}

/**
 ******************************************************************************
 * M4OSA_ERR M4VSS3GPP_intCheckClipCompatibleWithVssEditing()
 * @brief    Check if the clip is compatible with VSS editing
 * @note
 * @param   pClipCtxt            (IN) internal clip context
 * @param    pClipProperties     (OUT) Pointer to a valid ClipProperties structure.
 * @return    M4NO_ERROR:            No error
 ******************************************************************************
 */
M4OSA_ERR M4VSS3GPP_intCheckClipCompatibleWithVssEditing(
    M4VIDEOEDITING_ClipProperties *pClipProperties )
{
    M4OSA_UInt32 uiNbOfValidStreams = 0;
    M4OSA_ERR video_err = M4NO_ERROR;
    M4OSA_ERR audio_err = M4NO_ERROR;
    /********* file type *********/

    if( M4VIDEOEDITING_kFileType_AMR == pClipProperties->FileType )
    {
        M4OSA_TRACE1_0(
            "M4VSS3GPP_intCheckClipCompatibleWithVssEditing:\
            returning M4VSS3GPP_ERR_AMR_EDITING_UNSUPPORTED");
        return M4VSS3GPP_ERR_AMR_EDITING_UNSUPPORTED;
    }

    if( M4VIDEOEDITING_kFileType_MP3 == pClipProperties->FileType )
    {
        M4OSA_TRACE3_0(
            "M4VSS3GPP_intCheckClipCompatibleWithVssEditing(): returning M4NO_ERROR");
        return M4NO_ERROR;
    }

    /********* Video *********/

    if( M4VIDEOEDITING_kNoneVideo
        != pClipProperties->VideoStreamType ) /**< if there is a video stream */
    {
        /* Check video format is MPEG-4, H263 or H264 */
        switch( pClipProperties->VideoStreamType )
        {
            case M4VIDEOEDITING_kH263:
            case M4VIDEOEDITING_kMPEG4:
            case M4VIDEOEDITING_kH264:
                uiNbOfValidStreams++;
                pClipProperties->bVideoIsEditable = M4OSA_TRUE;
                break;

            default: /*< KO, we return error */
                M4OSA_TRACE1_0(
                    "M4VSS3GPP_intCheckClipCompatibleWithVssEditing(): unsupported video format");
                video_err = M4VSS3GPP_ERR_UNSUPPORTED_INPUT_VIDEO_FORMAT;
                break;
        }
    }
    else
    {
        /**
        * Audio only stream are currently not supported by the VSS editing feature
        (unless in the MP3 case) */
        M4OSA_TRACE1_0(
            "M4VSS3GPP_intCheckClipCompatibleWithVssEditing(): No video stream in clip");
        video_err = M4VSS3GPP_ERR_EDITING_NO_SUPPORTED_VIDEO_STREAM_IN_FILE;
    }

    /********* Audio *********/
    if( M4VIDEOEDITING_kNoneAudio != pClipProperties->
        AudioStreamType ) /**< if there is an audio stream */
    {
        /**
        * Check audio format is AMR-NB, EVRC or AAC */
        switch( pClipProperties->AudioStreamType )
        {
            case M4VIDEOEDITING_kAMR_NB:
                pClipProperties->bAudioIsEditable = M4OSA_TRUE;
                uiNbOfValidStreams++;
                break;

            case M4VIDEOEDITING_kAAC:
            case M4VIDEOEDITING_kAACplus:
            case M4VIDEOEDITING_keAACplus:
                switch( pClipProperties->uiSamplingFrequency )
                {
                case 8000:
                case 16000:
                case 22050:
                case 24000:
                case 32000:
                case 44100:
                case 48000:
                    pClipProperties->bAudioIsEditable = M4OSA_TRUE;
                    break;

                default:
                    break;
                }
                uiNbOfValidStreams++;
                break;

            case M4VIDEOEDITING_kEVRC:
                /*< OK, we proceed, no return */
                uiNbOfValidStreams++;
                break;

            default: /*< KO, we return error */
                M4OSA_TRACE1_0(
                    "M4VSS3GPP_intCheckClipCompatibleWithVssEditing(): unsupported audio format");
                audio_err = M4VSS3GPP_ERR_EDITING_UNSUPPORTED_AUDIO_FORMAT;
                break;
        }
    }
    else
    {
        /* Silence is always editable */
        pClipProperties->bAudioIsEditable = M4OSA_TRUE;
    }

    /**
    * Check there is at least one valid stream in the file... */
    if( video_err != M4NO_ERROR )
        return video_err;

    if( audio_err != M4NO_ERROR )
        return audio_err;

    if( 0 == uiNbOfValidStreams )
    {
        M4OSA_TRACE1_0(
            "M4VSS3GPP_intCheckClipCompatibleWithVssEditing(): File contains no supported stream,\
            returning M4VSS3GPP_ERR_EDITING_NO_SUPPORTED_STREAM_IN_FILE");
        return M4VSS3GPP_ERR_EDITING_NO_SUPPORTED_STREAM_IN_FILE;
    }

    /**
    * Return with no error */
    M4OSA_TRACE3_0(
        "M4VSS3GPP_intCheckClipCompatibleWithVssEditing(): returning M4NO_ERROR");
    return M4NO_ERROR;
}

/**
 ******************************************************************************
 * M4OSA_ERR M4VSS3GPP_intAudioMixingCompatibility()
 * @brief    This function allows checking if two clips are compatible with each other for
 *        VSS 3GPP audio mixing feature.
 * @note
 * @param    pC                            (IN) Context of the audio mixer
 * @param    pInputClipProperties        (IN) Clip analysis of the first clip
 * @param    pAddedClipProperties        (IN) Clip analysis of the second clip
 * @return    M4NO_ERROR:            No error
 * @return    M4ERR_PARAMETER:    At least one parameter is M4OSA_NULL (debug only)
 * @return    M4VSS3GPP_ERR_INVALID_CLIP_ANALYSIS_VERSION
 * @return  M4VSS3GPP_ERR_INPUT_CLIP_IS_NOT_A_3GPP
 * @return  M4NO_ERROR
 ******************************************************************************
 */
M4OSA_ERR
M4VSS3GPP_intAudioMixingCompatibility( M4VSS3GPP_InternalAudioMixingContext
                                      *pC, M4VIDEOEDITING_ClipProperties *pInputClipProperties,
                                      M4VIDEOEDITING_ClipProperties *pAddedClipProperties )
{
    M4OSA_Bool bClip1IsAAC = M4OSA_FALSE;
    M4OSA_Bool bClip2IsAAC = M4OSA_FALSE;

    /**
    * Reset settings */
    pInputClipProperties->bAudioIsEditable = M4OSA_FALSE;
    pAddedClipProperties->bAudioIsEditable = M4OSA_FALSE;
    pInputClipProperties->bAudioIsCompatibleWithMasterClip = M4OSA_FALSE;
    pAddedClipProperties->bAudioIsCompatibleWithMasterClip = M4OSA_FALSE;

    /**
    * Check that analysis has been generated by this version of the VSS3GPP library */
    if( ( pInputClipProperties->Version[0] != M4VIDEOEDITING_VERSION_MAJOR)
        || (pInputClipProperties->Version[1] != M4VIDEOEDITING_VERSION_MINOR)
        || (pInputClipProperties->Version[2]
    != M4VIDEOEDITING_VERSION_REVISION) )
    {
        M4OSA_TRACE1_0(
            "M4VSS3GPP_intAudioMixingCompatibility: The clip analysis has been generated\
            by another version, returning M4VSS3GPP_ERR_INVALID_CLIP_ANALYSIS_VERSION");
        return M4VSS3GPP_ERR_INVALID_CLIP_ANALYSIS_VERSION;
    }

    if( ( pAddedClipProperties->Version[0] != M4VIDEOEDITING_VERSION_MAJOR)
        || (pAddedClipProperties->Version[1] != M4VIDEOEDITING_VERSION_MINOR)
        || (pAddedClipProperties->Version[2]
    != M4VIDEOEDITING_VERSION_REVISION) )
    {
        M4OSA_TRACE1_0(
            "M4VSS3GPP_intAudioMixingCompatibility: The clip analysis has been generated\
            by another version, returning M4VSS3GPP_ERR_INVALID_CLIP_ANALYSIS_VERSION");
        return M4VSS3GPP_ERR_INVALID_CLIP_ANALYSIS_VERSION;
    }

    /********* input file type *********/

    if( M4VIDEOEDITING_kFileType_3GPP != pInputClipProperties->FileType )
    {
        M4OSA_TRACE1_0(
            "M4VSS3GPP_intAudioMixingCompatibility:\
            returning M4VSS3GPP_ERR_INPUT_CLIP_IS_NOT_A_3GPP");
        return M4VSS3GPP_ERR_INPUT_CLIP_IS_NOT_A_3GPP;
    }

    /********* input audio *********/

    if( M4VIDEOEDITING_kNoneAudio != pInputClipProperties->
        AudioStreamType ) /**< if there is an audio stream */
    {
        /**
        * Check audio format is AMR-NB or AAC */
        switch( pInputClipProperties->AudioStreamType )
        {
            case M4VIDEOEDITING_kAMR_NB:
                pInputClipProperties->bAudioIsEditable = M4OSA_TRUE;
                break;

            case M4VIDEOEDITING_kAAC:
            case M4VIDEOEDITING_kAACplus:
            case M4VIDEOEDITING_keAACplus:
                switch( pInputClipProperties->uiSamplingFrequency )
                {
                case 8000:
                case 16000:
                case 22050:
                case 24000:
                case 32000:
                case 44100:
                case 48000:
                    pInputClipProperties->bAudioIsEditable = M4OSA_TRUE;
                    break;

                default:
                    break;
            }
            bClip1IsAAC = M4OSA_TRUE;
            break;
          default:
            break;
        }
    }
    else
    {
        /* Silence is always editable */
        pInputClipProperties->bAudioIsEditable = M4OSA_TRUE;
    }

    /********* added audio *********/

    if( M4VIDEOEDITING_kNoneAudio != pAddedClipProperties->
        AudioStreamType ) /**< if there is an audio stream */
    {
        /**
        * Check audio format is AMR-NB or AAC */
        switch( pAddedClipProperties->AudioStreamType )
        {
            case M4VIDEOEDITING_kAMR_NB:
                pAddedClipProperties->bAudioIsEditable = M4OSA_TRUE;
                pAddedClipProperties->bAudioIsCompatibleWithMasterClip =
                    M4OSA_TRUE; /* I use this field to know if silence supported */
                break;

            case M4VIDEOEDITING_kAAC:
            case M4VIDEOEDITING_kAACplus:
            case M4VIDEOEDITING_keAACplus:
                switch( pAddedClipProperties->uiSamplingFrequency )
                {
                case 8000:
                case 16000:
                case 22050:
                case 24000:
                case 32000:
                case 44100:
                case 48000:
                    pAddedClipProperties->bAudioIsEditable = M4OSA_TRUE;
                    break;

                default:
                    break;
                }
                pAddedClipProperties->bAudioIsCompatibleWithMasterClip =
                    M4OSA_TRUE; /* I use this field to know if silence supported */
                bClip2IsAAC = M4OSA_TRUE;
                break;

            case M4VIDEOEDITING_kEVRC:
                break;

            case M4VIDEOEDITING_kPCM:
                pAddedClipProperties->bAudioIsEditable = M4OSA_TRUE;
                pAddedClipProperties->bAudioIsCompatibleWithMasterClip =
                    M4OSA_TRUE; /* I use this field to know if silence supported */

                if( pAddedClipProperties->uiSamplingFrequency == 16000 )
                {
                    bClip2IsAAC = M4OSA_TRUE;
                }
                break;

            case M4VIDEOEDITING_kMP3: /*RC*/
                pAddedClipProperties->bAudioIsEditable = M4OSA_TRUE;
                pAddedClipProperties->bAudioIsCompatibleWithMasterClip =
                    M4OSA_TRUE; /* I use this field to know if silence supported */
                break;

            default:
                /* The writer cannot write this  into a 3gpp */
                M4OSA_TRACE1_0(
                    "M4VSS3GPP_intAudioMixingCompatibility:\
                    returning M4VSS3GPP_ERR_UNSUPPORTED_ADDED_AUDIO_STREAM");
                return M4VSS3GPP_ERR_UNSUPPORTED_ADDED_AUDIO_STREAM;
        }
    }
    else
    {
        /* Silence is always editable */
        pAddedClipProperties->bAudioIsEditable = M4OSA_TRUE;
        pAddedClipProperties->bAudioIsCompatibleWithMasterClip =
            M4OSA_TRUE; /* I use this field to know if silence supported */
    }

    if( pC->bRemoveOriginal == M4OSA_FALSE )
    {
        if( pInputClipProperties->uiSamplingFrequency
            != pAddedClipProperties->uiSamplingFrequency )
        {
            /* We need to call SSRC in order to align ASF and/or nb of channels */
            /* Moreover, audio encoder may be needed in case of audio replacing... */
            pC->b_SSRCneeded = M4OSA_TRUE;
        }

        if( pInputClipProperties->uiNbChannels
            < pAddedClipProperties->uiNbChannels )
        {
            /* Stereo to Mono */
            pC->ChannelConversion = 1;
        }
        else if( pInputClipProperties->uiNbChannels
            > pAddedClipProperties->uiNbChannels )
        {
            /* Mono to Stereo */
            pC->ChannelConversion = 2;
        }
    }

    pInputClipProperties->bAudioIsCompatibleWithMasterClip = M4OSA_TRUE;

    /**
    * Return with no error */
    M4OSA_TRACE3_0(
        "M4VSS3GPP_intAudioMixingCompatibility(): returning M4NO_ERROR");
    return M4NO_ERROR;
}