File: Ap4SampleDescription.cpp

package info (click to toggle)
kodi-inputstream-adaptive 2.6.14%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 4,036 kB
  • sloc: cpp: 53,019; ansic: 492; makefile: 10
file content (927 lines) | stat: -rw-r--r-- 41,081 bytes parent folder | download
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
/*****************************************************************
|
|    AP4 - Sample Description Objects
|
|    Copyright 2002-2008 Axiomatic Systems, LLC
|
|
|    This file is part of Bento4/AP4 (MP4 Atom Processing Library).
|
|    Unless you have obtained Bento4 under a difference license,
|    this version of Bento4 is Bento4|GPL.
|    Bento4|GPL is free software; you can redistribute it and/or modify
|    it under the terms of the GNU General Public License as published by
|    the Free Software Foundation; either version 2, or (at your option)
|    any later version.
|
|    Bento4|GPL is distributed in the hope that it will be useful,
|    but WITHOUT ANY WARRANTY; without even the implied warranty of
|    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
|    GNU General Public License for more details.
|
|    You should have received a copy of the GNU General Public License
|    along with Bento4|GPL; see the file COPYING.  If not, write to the
|    Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
|    02111-1307, USA.
|
 ****************************************************************/

/*----------------------------------------------------------------------
|   includes
+---------------------------------------------------------------------*/
#include "Ap4SampleDescription.h"
#include "Ap4EsDescriptor.h"
#include "Ap4SLConfigDescriptor.h"
#include "Ap4SampleEntry.h"
#include "Ap4AvccAtom.h"
#include "Ap4HvccAtom.h"
#include "Ap4Utils.h"
#include "Ap4Mp4AudioInfo.h"

/*----------------------------------------------------------------------
|   dynamic cast support
+---------------------------------------------------------------------*/
AP4_DEFINE_DYNAMIC_CAST_ANCHOR(AP4_SampleDescription)
AP4_DEFINE_DYNAMIC_CAST_ANCHOR(AP4_UnknownSampleDescription)
AP4_DEFINE_DYNAMIC_CAST_ANCHOR(AP4_AudioSampleDescription)
AP4_DEFINE_DYNAMIC_CAST_ANCHOR(AP4_VideoSampleDescription)
AP4_DEFINE_DYNAMIC_CAST_ANCHOR(AP4_GenericAudioSampleDescription)
AP4_DEFINE_DYNAMIC_CAST_ANCHOR(AP4_GenericVideoSampleDescription)
AP4_DEFINE_DYNAMIC_CAST_ANCHOR(AP4_MpegSampleDescription)
AP4_DEFINE_DYNAMIC_CAST_ANCHOR(AP4_MpegAudioSampleDescription)
AP4_DEFINE_DYNAMIC_CAST_ANCHOR(AP4_MpegVideoSampleDescription)
AP4_DEFINE_DYNAMIC_CAST_ANCHOR(AP4_MpegSystemSampleDescription)
AP4_DEFINE_DYNAMIC_CAST_ANCHOR(AP4_AvcSampleDescription)
AP4_DEFINE_DYNAMIC_CAST_ANCHOR(AP4_HevcSampleDescription)
AP4_DEFINE_DYNAMIC_CAST_ANCHOR(AP4_SubtitleSampleDescription)

/*----------------------------------------------------------------------
|  AP4_GetFormatName
+---------------------------------------------------------------------*/
const char*
AP4_GetFormatName(AP4_UI32 format)
{
    switch (format) {
        case AP4_SAMPLE_FORMAT_MP4A: return "MPEG-4 Audio";
        case AP4_SAMPLE_FORMAT_MP4V: return "MPEG-4 Video";
        case AP4_SAMPLE_FORMAT_MP4S: return "MPEG-4 Systems";
        case AP4_SAMPLE_FORMAT_ALAC: return "Apple Lossless Audio";
        case AP4_SAMPLE_FORMAT_AVC1: return "H.264";
        case AP4_SAMPLE_FORMAT_AVC2: return "H.264";
        case AP4_SAMPLE_FORMAT_AVC3: return "H.264";
        case AP4_SAMPLE_FORMAT_AVC4: return "H.264";
        case AP4_SAMPLE_FORMAT_HEV1: return "H.265";
        case AP4_SAMPLE_FORMAT_HVC1: return "H.265";
        case AP4_SAMPLE_FORMAT_OVC1: return "VC-1";
        case AP4_SAMPLE_FORMAT_OWMA: return "WMA";
        case AP4_SAMPLE_FORMAT_AC_3: return "Dolby Digital (AC-3)";
        case AP4_SAMPLE_FORMAT_EC_3: return "Dolby Digital Plus (Enhanced AC-3)";
        case AP4_SAMPLE_FORMAT_DTSC: return "DTS";
        case AP4_SAMPLE_FORMAT_DTSH: return "DTS-HD";
        case AP4_SAMPLE_FORMAT_DTSL: return "DTS-HD Lossless";
        case AP4_SAMPLE_FORMAT_DTSE: return "DTS Low Bitrate";
        case AP4_SAMPLE_FORMAT_AVCP: return "Advanced Video Coding Parameters";
        case AP4_SAMPLE_FORMAT_DRAC: return "Dirac";
        case AP4_SAMPLE_FORMAT_DRA1: return "DRA Audio";
        case AP4_SAMPLE_FORMAT_G726: return "G726";
        case AP4_SAMPLE_FORMAT_MJP2: return "Motion JPEG 2000";
        case AP4_SAMPLE_FORMAT_OKSD: return "OMA Keys";
        case AP4_SAMPLE_FORMAT_RAW_: return "Uncompressed Audio";
        case AP4_SAMPLE_FORMAT_RTP_: return "RTP Hints";
        case AP4_SAMPLE_FORMAT_S263: return "H.263";
        case AP4_SAMPLE_FORMAT_SAMR: return "Narrowband AMR";
        case AP4_SAMPLE_FORMAT_SAWB: return "Wideband AMR";
        case AP4_SAMPLE_FORMAT_SAWP: return "Extended AMR";
        case AP4_SAMPLE_FORMAT_SEVC: return "EVRC Voice";
        case AP4_SAMPLE_FORMAT_SQCP: return "13K Voice";
        case AP4_SAMPLE_FORMAT_SRTP: return "SRTP Hints";
        case AP4_SAMPLE_FORMAT_SSMV: return "SMV Voice";
        case AP4_SAMPLE_FORMAT_TEXT: return "Textual Metadata";
        case AP4_SAMPLE_FORMAT_TWOS: return "Uncompressed 16-bit Audio";
        case AP4_SAMPLE_FORMAT_TX3G: return "Timed Text";
        case AP4_SAMPLE_FORMAT_VC_1: return "SMPTE VC-1";
        case AP4_SAMPLE_FORMAT_XML_: return "XML Metadata";
        default: return NULL;
    }
}

/*----------------------------------------------------------------------
|   AP4_SampleDescription::AP4_SampleDescription
+---------------------------------------------------------------------*/
AP4_SampleDescription::AP4_SampleDescription(Type            type,
                                             AP4_UI32        format,
                                             AP4_AtomParent* details) :
    m_Type(type), m_Format(format)
{
    if (details) {
        for (AP4_List<AP4_Atom>::Item* item = details->GetChildren().FirstItem();
             item;
             item = item->GetNext()) {
            AP4_Atom* atom = item->GetData();
            if (atom) {
                AP4_Atom* clone = atom->Clone();
                if (clone) m_Details.AddChild(clone);
            }
        }
    }
}

/*----------------------------------------------------------------------
|   AP4_SampleDescription::Clone
+---------------------------------------------------------------------*/
AP4_SampleDescription*
AP4_SampleDescription::Clone(AP4_Result* result)
{
    if (result) *result = AP4_SUCCESS;
    AP4_Atom* atom = ToAtom();
    if (atom == NULL) {
        if (result) *result = AP4_FAILURE;
        return NULL;
    }
    
    // serialize the atom to a buffer
    AP4_MemoryByteStream* mbs = new AP4_MemoryByteStream((AP4_UI32)atom->GetSize());
    atom->Write(*mbs);
    delete atom;
    atom = NULL;
    
    // parse the buffer back to an atom
    mbs->Seek(0);
    AP4_AtomFactory* factory = new AP4_AtomFactory();
    factory->PushContext(AP4_ATOM_TYPE_STSD);
    AP4_Atom* atom_clone = NULL;
    AP4_Result lresult = factory->CreateAtomFromStream(*mbs, atom_clone);
    factory->PopContext();
    delete factory;
    if (result) *result = lresult;
    mbs->Release();
    if (AP4_FAILED(lresult)) return NULL;
    
    // convert the atom clone to a sample description
    AP4_SampleEntry* sample_entry = AP4_DYNAMIC_CAST(AP4_SampleEntry, atom_clone);
    if (sample_entry == NULL) {
        if (result) *result = AP4_ERROR_INTERNAL;
        delete atom_clone;
        return NULL;
    }
    
    AP4_SampleDescription* clone = sample_entry->ToSampleDescription();
    if (clone == NULL) {
        if (result) *result = AP4_ERROR_INTERNAL;
    }
    
    delete atom_clone;
    return clone;
}

/*----------------------------------------------------------------------
|   AP4_SampleDescription::ToAtom
+---------------------------------------------------------------------*/
AP4_Atom*
AP4_SampleDescription::ToAtom() const
{
    return new AP4_SampleEntry(m_Format);
}

/*----------------------------------------------------------------------
|   AP4_SampleDescription::GetCodecString
+---------------------------------------------------------------------*/
AP4_Result
AP4_SampleDescription::GetCodecString(AP4_String& codec)
{
    codec = "";
    return AP4_SUCCESS;
}

/*----------------------------------------------------------------------
|   AP4_UnknownSampleDescription::AP4_UnknownSampleDescription
+---------------------------------------------------------------------*/
AP4_UnknownSampleDescription::AP4_UnknownSampleDescription(AP4_Atom* atom) :
    AP4_SampleDescription(AP4_SampleDescription::TYPE_UNKNOWN, 
                          atom->GetType(), 
                          NULL),
    m_Atom(atom->Clone())
{
}

/*----------------------------------------------------------------------
|   AP4_UnknownSampleDescription::~AP4_UnknownSampleDescription
+---------------------------------------------------------------------*/
AP4_UnknownSampleDescription::~AP4_UnknownSampleDescription()
{
    delete m_Atom;
}

/*----------------------------------------------------------------------
|   AP4_UnknownSampleDescription::Clone
+---------------------------------------------------------------------*/
AP4_SampleDescription*
AP4_UnknownSampleDescription::Clone(AP4_Result* result)
{
    AP4_Atom* atom_clone = NULL;
    if (m_Atom) {
        atom_clone = m_Atom->Clone();
        if (atom_clone == NULL) {
            if (result) *result = AP4_FAILURE;
            return NULL;
        }
    }
    if (result) *result = AP4_SUCCESS;
    return new AP4_UnknownSampleDescription(atom_clone);
}

/*----------------------------------------------------------------------
|   AP4_UnknownSampleDescription::ToAtom
+---------------------------------------------------------------------*/
AP4_Atom* 
AP4_UnknownSampleDescription::ToAtom() const
{
    if (m_Atom) {
        return m_Atom->Clone();
    } else {
        return NULL;
    }
}

/*----------------------------------------------------------------------
|   AP4_GenericAudioSampleDescription
+---------------------------------------------------------------------*/
AP4_Atom*
AP4_GenericAudioSampleDescription::ToAtom() const
{
    AP4_AudioSampleEntry* sample_entry = new AP4_AudioSampleEntry(m_Format,
                                                                  m_SampleRate,
                                                                  m_SampleSize,
                                                                  m_ChannelCount);
    AP4_AtomParent& details = const_cast<AP4_AtomParent&>(m_Details);
    for (AP4_List<AP4_Atom>::Item* item = details.GetChildren().FirstItem();
                                   item;
                                   item = item->GetNext()) {
        AP4_Atom* child = item->GetData();
        sample_entry->AddChild(child->Clone());
    }
    return sample_entry;
}

/*----------------------------------------------------------------------
|   AP4_GenericVideoSampleDescription
+---------------------------------------------------------------------*/
AP4_Atom*
AP4_GenericVideoSampleDescription::ToAtom() const
{
    AP4_VisualSampleEntry* sample_entry = new AP4_VisualSampleEntry(m_Format,
                                                                    m_Width,
                                                                    m_Height,
                                                                    m_Depth,
                                                                    m_CompressorName.GetChars());
    AP4_AtomParent& details = const_cast<AP4_AtomParent&>(m_Details);
    for (AP4_List<AP4_Atom>::Item* item = details.GetChildren().FirstItem();
                                   item;
                                   item = item->GetNext()) {
        AP4_Atom* child = item->GetData();
        sample_entry->AddChild(child->Clone());
    }
    return sample_entry;
}

/*----------------------------------------------------------------------
|   AP4_AvcSampleDescription::AP4_AvcSampleDescription
+---------------------------------------------------------------------*/
AP4_AvcSampleDescription::AP4_AvcSampleDescription(AP4_UI32     format,
                                                   AP4_UI16     width,
                                                   AP4_UI16     height,
                                                   AP4_UI16     depth,
                                                   const char*  compressor_name,
                                                   AP4_UI08     profile,
                                                   AP4_UI08     level,
                                                   AP4_UI08     profile_compatibility,
                                                   AP4_UI08     length_size,
                                                   const AP4_Array<AP4_DataBuffer>& sequence_parameters,
                                                   const AP4_Array<AP4_DataBuffer>& picture_parameters) :
    AP4_SampleDescription(TYPE_AVC, format, NULL),
    AP4_VideoSampleDescription(width, height, depth, compressor_name)
{
    m_AvccAtom = new AP4_AvccAtom(profile, 
                                  level, 
                                  profile_compatibility,
                                  length_size,
                                  sequence_parameters,
                                  picture_parameters);
    m_Details.AddChild(m_AvccAtom);
}

/*----------------------------------------------------------------------
|   AP4_AvcSampleDescription::AP4_AvcSampleDescription
+---------------------------------------------------------------------*/
AP4_AvcSampleDescription::AP4_AvcSampleDescription(AP4_UI32            format,
                                                   AP4_UI16            width,
                                                   AP4_UI16            height,
                                                   AP4_UI16            depth,
                                                   const char*         compressor_name,
                                                   const AP4_AvccAtom* avcc) :
    AP4_SampleDescription(TYPE_AVC, format, NULL),
    AP4_VideoSampleDescription(width, height, depth, compressor_name)
{
    if (avcc) {
        m_AvccAtom = new AP4_AvccAtom(*avcc);
    } else {
        // should never happen
        m_AvccAtom = new AP4_AvccAtom();
    }
    m_Details.AddChild(m_AvccAtom);
}

/*----------------------------------------------------------------------
|   AP4_AvcSampleDescription::AP4_AvcSampleDescription
+---------------------------------------------------------------------*/
AP4_AvcSampleDescription::AP4_AvcSampleDescription(AP4_UI32        format,
                                                   AP4_UI16        width,
                                                   AP4_UI16        height,
                                                   AP4_UI16        depth,
                                                   const char*     compressor_name,
                                                   AP4_AtomParent* details) :
    AP4_SampleDescription(TYPE_AVC, format, details),
    AP4_VideoSampleDescription(width, height, depth, compressor_name),
    m_AvccAtom(NULL)
{
    AP4_AvccAtom* avcc = AP4_DYNAMIC_CAST(AP4_AvccAtom, details->GetChild(AP4_ATOM_TYPE_AVCC));
    if (avcc) {
        m_AvccAtom = new AP4_AvccAtom(*avcc);
    } else {
        // shoud never happen
        m_AvccAtom = new AP4_AvccAtom();
    }
    m_Details.AddChild(m_AvccAtom);
}

/*----------------------------------------------------------------------
|   AP4_AvcSampleDescription::GetCodecString
+---------------------------------------------------------------------*/
AP4_Result
AP4_AvcSampleDescription::GetCodecString(AP4_String& codec) {
    char coding[5];
    AP4_FormatFourChars(coding, GetFormat());
    char workspace[64];
    AP4_FormatString(workspace,
                     sizeof(workspace),
                     "%s.%02X%02X%02X",
                     coding,
                     GetProfile(),
                     GetProfileCompatibility(),
                     GetLevel());
    codec = workspace;
    
    return AP4_SUCCESS;
}

/*----------------------------------------------------------------------
|   AP4_AvcSampleDescription::ToAtom
+---------------------------------------------------------------------*/
AP4_Atom*
AP4_AvcSampleDescription::ToAtom() const
{
    return new AP4_AvcSampleEntry(m_Format,
                                  m_Width,
                                  m_Height,
                                  m_Depth,
                                  m_CompressorName.GetChars(),
                                  *m_AvccAtom);
}

/*----------------------------------------------------------------------
|   AP4_HevcSampleDescription::AP4_HevcSampleDescription
+---------------------------------------------------------------------*/
AP4_HevcSampleDescription::AP4_HevcSampleDescription(AP4_UI32            format,
                                                     AP4_UI16            width,
                                                     AP4_UI16            height,
                                                     AP4_UI16            depth,
                                                     const char*         compressor_name,
                                                     const AP4_HvccAtom* hvcc) :
    AP4_SampleDescription(TYPE_HEVC, format, NULL),
    AP4_VideoSampleDescription(width, height, depth, compressor_name)
{
    if (hvcc) {
        m_HvccAtom = new AP4_HvccAtom(*hvcc);
    } else {
        // should never happen
        m_HvccAtom = new AP4_HvccAtom();
    }
    m_Details.AddChild(m_HvccAtom);
}

/*----------------------------------------------------------------------
|   AP4_HevcSampleDescription::AP4_HevcSampleDescription
+---------------------------------------------------------------------*/
AP4_HevcSampleDescription::AP4_HevcSampleDescription(AP4_UI32        format,
                                                     AP4_UI16        width,
                                                     AP4_UI16        height,
                                                     AP4_UI16        depth,
                                                     const char*     compressor_name,
                                                     AP4_AtomParent* details) :
    AP4_SampleDescription(TYPE_HEVC, format, details),
    AP4_VideoSampleDescription(width, height, depth, compressor_name),
    m_HvccAtom(NULL)
{
    AP4_HvccAtom* hvcc = AP4_DYNAMIC_CAST(AP4_HvccAtom, details->GetChild(AP4_ATOM_TYPE_HVCC));
    if (hvcc) {
        m_HvccAtom = new AP4_HvccAtom(*hvcc);
    } else {
        // shoud never happen
        m_HvccAtom = new AP4_HvccAtom();
    }
    m_Details.AddChild(m_HvccAtom);
}

/*----------------------------------------------------------------------
|   ReverseBits
+---------------------------------------------------------------------*/
static AP4_UI32
ReverseBits(AP4_UI32 bits)
{
    unsigned int count = sizeof(bits) * 8;
    AP4_UI32 reverse_bits = 0;
     
    while (bits) {
       reverse_bits = (reverse_bits << 1) | (bits & 1);
       bits >>= 1;
       count--;
    }
    return reverse_bits << count;
}

/*----------------------------------------------------------------------
|   AP4_HevcSampleDescription:GetCodecString
+---------------------------------------------------------------------*/
AP4_Result
AP4_HevcSampleDescription::GetCodecString(AP4_String& codec) {
    char coding[5];
    AP4_FormatFourChars(coding, GetFormat());
    char profile_space[2] = {0,0};
    if (GetGeneralProfileSpace() > 0 && GetGeneralProfileSpace() <= 3) {
        profile_space[0] = 'A'+GetGeneralProfileSpace()-1;
    }
    AP4_UI64 constraints = GetGeneralConstraintIndicatorFlags();
    while (constraints && ((constraints & 0xFF) == 0)) {
        constraints >>= 8;
    }
    char workspace[64];
    AP4_FormatString(workspace,
                     sizeof(workspace),
                     "%s.%s%d.%X.%c%d.%llx",
                     coding,
                     profile_space,
                     GetGeneralProfile(),
                     ReverseBits(GetGeneralProfileCompatibilityFlags()),
                     GetGeneralTierFlag()?'H':'L',
                     GetGeneralLevel(),
                     constraints);
    codec = workspace;
    
    return AP4_SUCCESS;
}

/*----------------------------------------------------------------------
|   AP4_HevcSampleDescription::ToAtom
+---------------------------------------------------------------------*/
AP4_Atom*
AP4_HevcSampleDescription::ToAtom() const
{
    return new AP4_HevcSampleEntry(m_Format,
                                   m_Width,
                                   m_Height,
                                   m_Depth,
                                   m_CompressorName.GetChars(),
                                   *m_HvccAtom);
}

/*----------------------------------------------------------------------
|   AP4_MpegSampleDescription::AP4_MpegSampleDescription
+---------------------------------------------------------------------*/
AP4_MpegSampleDescription::AP4_MpegSampleDescription(AP4_UI32      format,
                                                     AP4_EsdsAtom* esds) :
    AP4_SampleDescription(TYPE_MPEG, format, NULL),
    m_StreamType(0),
    m_ObjectTypeId(0),
    m_BufferSize(0),
    m_MaxBitrate(0),
    m_AvgBitrate(0)
{
    if (esds) {
        // get the es descriptor
        const AP4_EsDescriptor* es_desc = esds->GetEsDescriptor();
        if (es_desc == NULL) return;

        // get the decoder config descriptor
        const AP4_DecoderConfigDescriptor* dc_desc =
            es_desc->GetDecoderConfigDescriptor();
        if (dc_desc) {
            m_StreamType   = dc_desc->GetStreamType();
            m_ObjectTypeId = dc_desc->GetObjectTypeIndication();
            m_BufferSize   = dc_desc->GetBufferSize();
            m_MaxBitrate   = dc_desc->GetMaxBitrate();
            m_AvgBitrate   = dc_desc->GetAvgBitrate();
            const AP4_DecoderSpecificInfoDescriptor* dsi_desc =
                dc_desc->GetDecoderSpecificInfoDescriptor();
            if (dsi_desc != NULL) {
                m_DecoderInfo.SetData(dsi_desc->GetDecoderSpecificInfo().GetData(),
                                      dsi_desc->GetDecoderSpecificInfo().GetDataSize());
            }
        }
    }
}

/*----------------------------------------------------------------------
|   AP4_MpegSampleDescription::AP4_MpegSampleDescription
+---------------------------------------------------------------------*/
AP4_MpegSampleDescription::AP4_MpegSampleDescription(
    AP4_UI32              format,
    StreamType            stream_type,
    OTI                   oti,
    const AP4_DataBuffer* decoder_info,
    AP4_UI32              buffer_size,
    AP4_UI32              max_bitrate,
    AP4_UI32              avg_bitrate) :
    AP4_SampleDescription(TYPE_MPEG, format, NULL),
    m_StreamType(stream_type),
    m_ObjectTypeId(oti),
    m_BufferSize(buffer_size),
    m_MaxBitrate(max_bitrate),
    m_AvgBitrate(avg_bitrate)
{
    if (decoder_info != NULL) {
        m_DecoderInfo.SetData(decoder_info->GetData(), decoder_info->GetDataSize());
    }
}

/*----------------------------------------------------------------------
|   AP4_MpegSampleDescription::CreateEsDescriptor
+---------------------------------------------------------------------*/
AP4_EsDescriptor* 
AP4_MpegSampleDescription::CreateEsDescriptor() const
{
    AP4_EsDescriptor* desc = new AP4_EsDescriptor(0);
    AP4_DecoderSpecificInfoDescriptor* dsi_desc;
    if (m_DecoderInfo.GetDataSize() != 0) {
        dsi_desc = new AP4_DecoderSpecificInfoDescriptor(m_DecoderInfo);
    } else {
        dsi_desc = NULL;
    }
    AP4_DecoderConfigDescriptor* decoder_config = 
        new AP4_DecoderConfigDescriptor(m_StreamType,
        m_ObjectTypeId,
        m_BufferSize,
        m_MaxBitrate,
        m_AvgBitrate,
        dsi_desc);
    desc->AddSubDescriptor(decoder_config);
    
    // add a fixed SL Config
    desc->AddSubDescriptor(new AP4_SLConfigDescriptor());
    
    return desc;
}

/*----------------------------------------------------------------------
|   AP4_MpegSystemSampleDescription::AP4_MpegSystemSampleDescription
+---------------------------------------------------------------------*/
AP4_MpegSystemSampleDescription::AP4_MpegSystemSampleDescription(AP4_EsdsAtom* esds) :
    AP4_MpegSampleDescription(AP4_ATOM_TYPE_MP4S, esds)
{
}

/*----------------------------------------------------------------------
|   AP4_MpegSystemSampleDescription::AP4_MpegSystemSampleDescription
+---------------------------------------------------------------------*/
AP4_MpegSystemSampleDescription::AP4_MpegSystemSampleDescription(
    StreamType            stream_type,
    OTI                   oti,
    const AP4_DataBuffer* decoder_info,
    AP4_UI32              buffer_size,
    AP4_UI32              max_bitrate,
    AP4_UI32              avg_bitrate) :
    AP4_MpegSampleDescription(AP4_ATOM_TYPE_MP4S,
                              stream_type,
                              oti,
                              decoder_info,
                              buffer_size,
                              max_bitrate,
                              avg_bitrate)
{
}

/*----------------------------------------------------------------------
|   AP4_MpegSystemSampleDescription::ToAtom
+---------------------------------------------------------------------*/
AP4_Atom*
AP4_MpegSystemSampleDescription::ToAtom() const
{
    return new AP4_Mp4sSampleEntry(CreateEsDescriptor());
}

/*----------------------------------------------------------------------
|   AP4_MpegAudioSampleDescription::AP4_MpegAudioSampleDescription
+---------------------------------------------------------------------*/
AP4_MpegAudioSampleDescription::AP4_MpegAudioSampleDescription(
    AP4_UI32  sample_rate,
    AP4_UI16  sample_size,
    AP4_UI16  channel_count,
    AP4_EsdsAtom* esds) :
    AP4_MpegSampleDescription(AP4_ATOM_TYPE_MP4A, esds),
    AP4_AudioSampleDescription(sample_rate, sample_size, channel_count)
{
}

/*----------------------------------------------------------------------
|   AP4_MpegAudioSampleDescription::AP4_MpegAudioSampleDescription
+---------------------------------------------------------------------*/
AP4_MpegAudioSampleDescription::AP4_MpegAudioSampleDescription(
    OTI                   oti,
    AP4_UI32              sample_rate,
    AP4_UI16              sample_size,
    AP4_UI16              channel_count,
    const AP4_DataBuffer* decoder_info,
    AP4_UI32              buffer_size,
    AP4_UI32              max_bitrate,
    AP4_UI32              avg_bitrate) :
    AP4_MpegSampleDescription(AP4_ATOM_TYPE_MP4A,
                              AP4_STREAM_TYPE_AUDIO,
                              oti, 
                              decoder_info, buffer_size, 
                              max_bitrate, avg_bitrate),
    AP4_AudioSampleDescription(sample_rate, sample_size, channel_count)
{
}

/*----------------------------------------------------------------------
|   AP4_MpegAudioSampleDescription::ToAtom
+---------------------------------------------------------------------*/
AP4_Atom*
AP4_MpegAudioSampleDescription::ToAtom() const
{
    return new AP4_Mp4aSampleEntry(m_SampleRate<<16,
                                   m_SampleSize,
                                   m_ChannelCount,
                                   CreateEsDescriptor());
}

/*----------------------------------------------------------------------
|   AP4_MpegAudioSampleDescription::GetCodecString
+---------------------------------------------------------------------*/
AP4_Result
AP4_MpegAudioSampleDescription::GetCodecString(AP4_String& codec)
{
    char coding[5];
    AP4_FormatFourChars(coding, GetFormat());
    char workspace[64];
    workspace[0] = 0;
    if (GetFormat() == AP4_SAMPLE_FORMAT_MP4A) {
        if (GetObjectTypeId() == AP4_OTI_MPEG4_AUDIO) {
            Mpeg4AudioObjectType object_type = GetMpeg4AudioObjectType();
            if (object_type == AP4_MPEG4_AUDIO_OBJECT_TYPE_AAC_LC) {
                const AP4_DataBuffer& dsi = GetDecoderInfo();
                if (dsi.GetDataSize()) {
                    AP4_Mp4AudioDecoderConfig dec_config;
                    AP4_Result result = dec_config.Parse(dsi.GetData(), dsi.GetDataSize());
                    if (AP4_SUCCEEDED(result)) {
                        if (dec_config.m_Extension.m_PsPresent) {
                            object_type = AP4_MPEG4_AUDIO_OBJECT_TYPE_PS;
                        } else if (dec_config.m_Extension.m_SbrPresent) {
                            object_type = AP4_MPEG4_AUDIO_OBJECT_TYPE_SBR;
                        }
                    }
                }
            }
            AP4_FormatString(workspace, sizeof(workspace), "%s.%02X.%d", coding, (int)GetObjectTypeId(), object_type);
        } else {
            AP4_FormatString(workspace, sizeof(workspace), "%s.%02X", coding, (int)GetObjectTypeId());
        }
    }
    
    codec = workspace;
    return AP4_SUCCESS;
}

/*----------------------------------------------------------------------
|   AP4_MpegAudioSampleDescription::GetMpeg4AudioObjectType
+---------------------------------------------------------------------*/
AP4_MpegAudioSampleDescription::Mpeg4AudioObjectType
AP4_MpegAudioSampleDescription::GetMpeg4AudioObjectType() const
{
    if (m_ObjectTypeId == AP4_OTI_MPEG4_AUDIO &&
        m_DecoderInfo.GetDataSize() >= 1) {
        AP4_UI08 type = m_DecoderInfo.GetData()[0]>>3;
        if (type == 31) {
            if (m_DecoderInfo.GetDataSize() < 2) return 0;
            type = 32+(((m_DecoderInfo.GetData()[0]&0x07)<<3) |
                       ((m_DecoderInfo.GetData()[1]&0xE0)>>5));
        }
        return type;
    } else {
        return 0;
    }
}

/*----------------------------------------------------------------------
|   AP4_MpegVideoSampleDescription::AP4_MpegVideoSampleDescription
+---------------------------------------------------------------------*/
AP4_MpegVideoSampleDescription::AP4_MpegVideoSampleDescription(
    AP4_UI16      width,
    AP4_UI16      height,
    AP4_UI16      depth,
    const char*   compressor_name,
    AP4_EsdsAtom* esds) :
    
    AP4_MpegSampleDescription(AP4_ATOM_TYPE_MP4V, esds),
    AP4_VideoSampleDescription(width, height, depth, compressor_name)
{
}

/*----------------------------------------------------------------------
|   AP4_MpegVideoSampleDescription::AP4_MpegVideoSampleDescription
+---------------------------------------------------------------------*/
AP4_MpegVideoSampleDescription::AP4_MpegVideoSampleDescription(
    OTI                   oti,
    AP4_UI16              width,
    AP4_UI16              height,
    AP4_UI16              depth,
    const char*           compressor_name,
    const AP4_DataBuffer* decoder_info,
    AP4_UI32              buffer_size,
    AP4_UI32              max_bitrate,
    AP4_UI32              avg_bitrate) :
    AP4_MpegSampleDescription(AP4_ATOM_TYPE_MP4V, 
                              AP4_STREAM_TYPE_VISUAL,
                              oti,
                              decoder_info,
                              buffer_size,
                              max_bitrate,
                              avg_bitrate),
    AP4_VideoSampleDescription(width, height, depth, compressor_name)
{
}

/*----------------------------------------------------------------------
|   AP4_MpegVideoSampleDescription::ToAtom
+---------------------------------------------------------------------*/
AP4_Atom*
AP4_MpegVideoSampleDescription::ToAtom() const
{
    return new AP4_Mp4vSampleEntry(m_Width,
                                   m_Height,
                                   m_Depth,
                                   m_CompressorName.GetChars(),
                                   CreateEsDescriptor());
}

/*----------------------------------------------------------------------
|   AP4_MpegSampleDescription::GetStreamTypeString
+---------------------------------------------------------------------*/
const char* 
AP4_MpegSampleDescription::GetStreamTypeString(StreamType type)
{
    switch (type) {
        case AP4_STREAM_TYPE_FORBIDDEN: return "INVALID"; 
        case AP4_STREAM_TYPE_OD:        return "Object Descriptor";
        case AP4_STREAM_TYPE_CR:        return "CR";	
        case AP4_STREAM_TYPE_BIFS:      return "BIFS";
        case AP4_STREAM_TYPE_VISUAL:    return "Visual";
        case AP4_STREAM_TYPE_AUDIO:     return "Audio";
        case AP4_STREAM_TYPE_MPEG7:     return "MPEG-7";
        case AP4_STREAM_TYPE_IPMP:      return "IPMP";
        case AP4_STREAM_TYPE_OCI:       return "OCI";
        case AP4_STREAM_TYPE_MPEGJ:     return "MPEG-J";
        default:                        return "UNKNOWN";
    }
}

/*----------------------------------------------------------------------
|   AP4_MpegSampleDescription::GetObjectTypeString
+---------------------------------------------------------------------*/
const char* 
AP4_MpegSampleDescription::GetObjectTypeString(OTI oti)
{
    switch (oti) {
        case AP4_OTI_MPEG4_SYSTEM:         return "MPEG-4 System";
        case AP4_OTI_MPEG4_SYSTEM_COR:     return "MPEG-4 System COR";
        case AP4_OTI_MPEG4_VISUAL:         return "MPEG-4 Video";
        case AP4_OTI_MPEG4_AUDIO:          return "MPEG-4 Audio";
        case AP4_OTI_MPEG2_VISUAL_SIMPLE:  return "MPEG-2 Video Simple Profile";
        case AP4_OTI_MPEG2_VISUAL_MAIN:    return "MPEG-2 Video Main Profile";
        case AP4_OTI_MPEG2_VISUAL_SNR:     return "MPEG-2 Video SNR";
        case AP4_OTI_MPEG2_VISUAL_SPATIAL: return "MPEG-2 Video Spatial";
        case AP4_OTI_MPEG2_VISUAL_HIGH:    return "MPEG-2 Video High";
        case AP4_OTI_MPEG2_VISUAL_422:     return "MPEG-2 Video 4:2:2";
        case AP4_OTI_MPEG2_AAC_AUDIO_MAIN: return "MPEG-2 Audio AAC Main Profile";
        case AP4_OTI_MPEG2_AAC_AUDIO_LC:   return "MPEG-2 Audio AAC Low Complexity";
        case AP4_OTI_MPEG2_AAC_AUDIO_SSRP: return "MPEG-2 Audio AAC SSRP";
        case AP4_OTI_MPEG2_PART3_AUDIO:    return "MPEG-2 Audio Part-3";
        case AP4_OTI_MPEG1_VISUAL:         return "MPEG-1 Video";
        case AP4_OTI_MPEG1_AUDIO:          return "MPEG-1 Audio";
        case AP4_OTI_JPEG:                 return "JPEG";
        case AP4_OTI_JPEG2000:             return "JPEG-2000";
        case AP4_OTI_EVRC_VOICE:           return "EVRC Voice";
        case AP4_OTI_SMV_VOICE:            return "SMV Voice";
        case AP4_OTI_3GPP2_CMF:            return "3GPP2 CMF";
        case AP4_OTI_SMPTE_VC1:            return "SMPTE VC1 Video";
        case AP4_OTI_DIRAC_VIDEO:          return "Dirac Video";
        case AP4_OTI_AC3_AUDIO:            return "AC3 Audio";
        case AP4_OTI_EAC3_AUDIO:           return "E-AC3 Audio";
        case AP4_OTI_DRA_AUDIO:            return "DRA Audio";
        case AP4_OTI_G719_AUDIO:           return "G.719 Audio";
        case AP4_OTI_DTS_AUDIO:            return "DTS Audio";
        case AP4_OTI_DTS_HIRES_AUDIO:      return "DTS High Resolution Audio";
        case AP4_OTI_DTS_MASTER_AUDIO:     return "DTS Master Audio";
        case AP4_OTI_DTS_EXPRESS_AUDIO:    return "DTS Express/LBR Audio";
        case AP4_OTI_13K_VOICE:            return "13K Voice";
        default:                           return "UNKNOWN";
    }
}

/*----------------------------------------------------------------------
|   AP4_MpegAudioSampleDescription::GetMpeg4AudioObjectTypeString
+---------------------------------------------------------------------*/
const char* 
AP4_MpegAudioSampleDescription::GetMpeg4AudioObjectTypeString(Mpeg4AudioObjectType type)
{
    switch (type) {
        case AP4_MPEG4_AUDIO_OBJECT_TYPE_AAC_MAIN:                return "AAC Main Profile";
        case AP4_MPEG4_AUDIO_OBJECT_TYPE_AAC_LC:                  return "AAC Low Complexity";
        case AP4_MPEG4_AUDIO_OBJECT_TYPE_AAC_SSR:                 return "AAC Scalable Sample Rate";
        case AP4_MPEG4_AUDIO_OBJECT_TYPE_AAC_LTP:                 return "AAC Long Term Predictor";
        case AP4_MPEG4_AUDIO_OBJECT_TYPE_SBR:                     return "Spectral Band Replication";
        case AP4_MPEG4_AUDIO_OBJECT_TYPE_AAC_SCALABLE:            return "AAC Scalable";
        case AP4_MPEG4_AUDIO_OBJECT_TYPE_TWINVQ:                  return "Twin VQ";
        case AP4_MPEG4_AUDIO_OBJECT_TYPE_CELP:                    return "CELP";
        case AP4_MPEG4_AUDIO_OBJECT_TYPE_HVXC:                    return "HVXC";
        case AP4_MPEG4_AUDIO_OBJECT_TYPE_TTSI:                    return "TTSI";
        case AP4_MPEG4_AUDIO_OBJECT_TYPE_MAIN_SYNTHETIC:          return "Main Synthetic";
        case AP4_MPEG4_AUDIO_OBJECT_TYPE_WAVETABLE_SYNTHESIS:     return "Wavetable Synthesis";
        case AP4_MPEG4_AUDIO_OBJECT_TYPE_GENERAL_MIDI:            return "General MIDI";
        case AP4_MPEG4_AUDIO_OBJECT_TYPE_ALGORITHMIC_SYNTHESIS:   return "Algorithmic Synthesis";
        case AP4_MPEG4_AUDIO_OBJECT_TYPE_ER_AAC_LC:               return "Error Resilient AAC Low Complexity";
        case AP4_MPEG4_AUDIO_OBJECT_TYPE_ER_AAC_LTP:              return "Error Resilient AAC Long Term Prediction";
        case AP4_MPEG4_AUDIO_OBJECT_TYPE_ER_AAC_SCALABLE:         return "Error Resilient AAC Scalable";
        case AP4_MPEG4_AUDIO_OBJECT_TYPE_ER_TWINVQ:               return "Error Resilient Twin VQ";
        case AP4_MPEG4_AUDIO_OBJECT_TYPE_ER_BSAC:                 return "Error Resilient Bit Sliced Arithmetic Coding";
        case AP4_MPEG4_AUDIO_OBJECT_TYPE_ER_AAC_LD:               return "Error Resilient AAC Low Delay";
        case AP4_MPEG4_AUDIO_OBJECT_TYPE_ER_CELP:                 return "Error Resilient CELP";
        case AP4_MPEG4_AUDIO_OBJECT_TYPE_ER_HVXC:                 return "Error Resilient HVXC";
        case AP4_MPEG4_AUDIO_OBJECT_TYPE_ER_HILN:                 return "Error Resilient HILN";
        case AP4_MPEG4_AUDIO_OBJECT_TYPE_ER_PARAMETRIC:           return "Error Resilient Parametric";
        case AP4_MPEG4_AUDIO_OBJECT_TYPE_SSC:                     return "SSC";
        case AP4_MPEG4_AUDIO_OBJECT_TYPE_PS:                      return "Parametric Stereo";
        case AP4_MPEG4_AUDIO_OBJECT_TYPE_MPEG_SURROUND:           return "MPEG Surround";
        case AP4_MPEG4_AUDIO_OBJECT_TYPE_LAYER_1:                 return "MPEG Layer 1";
        case AP4_MPEG4_AUDIO_OBJECT_TYPE_LAYER_2:                 return "MPEG Layer 2";
        case AP4_MPEG4_AUDIO_OBJECT_TYPE_LAYER_3:                 return "MPEG Layer 3";
        case AP4_MPEG4_AUDIO_OBJECT_TYPE_DST:                     return "Direct Stream Transfer";
        case AP4_MPEG4_AUDIO_OBJECT_TYPE_ALS:                     return "ALS Lossless Coding";
        case AP4_MPEG4_AUDIO_OBJECT_TYPE_SLS:                     return "SLS Scalable Lossless Coding";
        case AP4_MPEG4_AUDIO_OBJECT_TYPE_SLS_NON_CORE:            return "SLS Scalable Lossless Coding (Non Core)";
        case AP4_MPEG4_AUDIO_OBJECT_TYPE_ER_AAC_ELD:              return "Error Resilient AAC ELD";
        case AP4_MPEG4_AUDIO_OBJECT_TYPE_SMR_SIMPLE:              return "SMR Simple";
        case AP4_MPEG4_AUDIO_OBJECT_TYPE_SMR_MAIN:                return "SMR Main";
        default:                                                  return "UNKNOWN";
    }
}

/*----------------------------------------------------------------------
|   AP4_SubtitleSampleDescription::AP4_SubtitleSampleDescription
+---------------------------------------------------------------------*/
AP4_SubtitleSampleDescription::AP4_SubtitleSampleDescription(AP4_UI32    format,
                                                             const char* namespce,
                                                             const char* schema_location,
                                                             const char* image_mime_type) :
    AP4_SampleDescription(AP4_SampleDescription::TYPE_SUBTITLES, format, NULL),
    m_Namespace(namespce),
    m_SchemaLocation(schema_location),
    m_ImageMimeType(image_mime_type)
{
}

/*----------------------------------------------------------------------
|   AP4_SubtitleSampleDescription::Clone
+---------------------------------------------------------------------*/
AP4_SampleDescription*
AP4_SubtitleSampleDescription::Clone(AP4_Result* result)
{
    if (result) *result = AP4_SUCCESS;
    return new AP4_SubtitleSampleDescription(m_Format,
                                             m_Namespace.GetChars(),
                                             m_SchemaLocation.GetChars(),
                                             m_ImageMimeType.GetChars());
}

/*----------------------------------------------------------------------
|   AP4_SubtitleSampleDescription::ToAtom
+---------------------------------------------------------------------*/
AP4_Atom*
AP4_SubtitleSampleDescription::ToAtom() const
{
    return new AP4_SubtitleSampleEntry(m_Format,
                                       m_Namespace.GetChars(),
                                       m_SchemaLocation.GetChars(),
                                       m_ImageMimeType.GetChars());
}