Browse code

avcodec: add avpriv_dca_parse_core_frame_header()

There are 3 different places where DCA core frame header is parsed:
decoder, parser and demuxer. Each one uses ad-hoc code. Add common core
frame header parsing function that will be used in all places.

Signed-off-by: James Almer <jamrial@gmail.com>

foo86 authored on 2017/07/10 23:11:33
Showing 3 changed files
... ...
@@ -28,7 +28,9 @@
28 28
 #include "libavutil/error.h"
29 29
 
30 30
 #include "dca.h"
31
+#include "dca_core.h"
31 32
 #include "dca_syncwords.h"
33
+#include "get_bits.h"
32 34
 #include "put_bits.h"
33 35
 
34 36
 const uint32_t avpriv_dca_sample_rates[16] = {
... ...
@@ -85,3 +87,61 @@ int avpriv_dca_convert_bitstream(const uint8_t *src, int src_size, uint8_t *dst,
85 85
         return AVERROR_INVALIDDATA;
86 86
     }
87 87
 }
88
+
89
+int avpriv_dca_parse_core_frame_header(GetBitContext *gb, DCACoreFrameHeader *h)
90
+{
91
+    if (get_bits_long(gb, 32) != DCA_SYNCWORD_CORE_BE)
92
+        return DCA_PARSE_ERROR_SYNC_WORD;
93
+
94
+    h->normal_frame = get_bits1(gb);
95
+    h->deficit_samples = get_bits(gb, 5) + 1;
96
+    if (h->deficit_samples != DCA_PCMBLOCK_SAMPLES)
97
+        return DCA_PARSE_ERROR_DEFICIT_SAMPLES;
98
+
99
+    h->crc_present = get_bits1(gb);
100
+    h->npcmblocks = get_bits(gb, 7) + 1;
101
+    if (h->npcmblocks & (DCA_SUBBAND_SAMPLES - 1))
102
+        return DCA_PARSE_ERROR_PCM_BLOCKS;
103
+
104
+    h->frame_size = get_bits(gb, 14) + 1;
105
+    if (h->frame_size < 96)
106
+        return DCA_PARSE_ERROR_FRAME_SIZE;
107
+
108
+    h->audio_mode = get_bits(gb, 6);
109
+    if (h->audio_mode >= DCA_AMODE_COUNT)
110
+        return DCA_PARSE_ERROR_AMODE;
111
+
112
+    h->sr_code = get_bits(gb, 4);
113
+    if (!avpriv_dca_sample_rates[h->sr_code])
114
+        return DCA_PARSE_ERROR_SAMPLE_RATE;
115
+
116
+    h->br_code = get_bits(gb, 5);
117
+    if (get_bits1(gb))
118
+        return DCA_PARSE_ERROR_RESERVED_BIT;
119
+
120
+    h->drc_present = get_bits1(gb);
121
+    h->ts_present = get_bits1(gb);
122
+    h->aux_present = get_bits1(gb);
123
+    h->hdcd_master = get_bits1(gb);
124
+    h->ext_audio_type = get_bits(gb, 3);
125
+    h->ext_audio_present = get_bits1(gb);
126
+    h->sync_ssf = get_bits1(gb);
127
+    h->lfe_present = get_bits(gb, 2);
128
+    if (h->lfe_present == DCA_LFE_FLAG_INVALID)
129
+        return DCA_PARSE_ERROR_LFE_FLAG;
130
+
131
+    h->predictor_history = get_bits1(gb);
132
+    if (h->crc_present)
133
+        skip_bits(gb, 16);
134
+    h->filter_perfect = get_bits1(gb);
135
+    h->encoder_rev = get_bits(gb, 4);
136
+    h->copy_hist = get_bits(gb, 2);
137
+    h->pcmr_code = get_bits(gb, 3);
138
+    if (!ff_dca_bits_per_sample[h->pcmr_code])
139
+        return DCA_PARSE_ERROR_PCM_RES;
140
+
141
+    h->sumdiff_front = get_bits1(gb);
142
+    h->sumdiff_surround = get_bits1(gb);
143
+    h->dn_code = get_bits(gb, 4);
144
+    return 0;
145
+}
... ...
@@ -32,6 +32,49 @@
32 32
 #include "libavutil/internal.h"
33 33
 #include "libavutil/intreadwrite.h"
34 34
 
35
+#include "get_bits.h"
36
+
37
+#define DCA_CORE_FRAME_HEADER_SIZE      18
38
+
39
+enum DCAParseError {
40
+    DCA_PARSE_ERROR_SYNC_WORD       = -1,
41
+    DCA_PARSE_ERROR_DEFICIT_SAMPLES = -2,
42
+    DCA_PARSE_ERROR_PCM_BLOCKS      = -3,
43
+    DCA_PARSE_ERROR_FRAME_SIZE      = -4,
44
+    DCA_PARSE_ERROR_AMODE           = -5,
45
+    DCA_PARSE_ERROR_SAMPLE_RATE     = -6,
46
+    DCA_PARSE_ERROR_RESERVED_BIT    = -7,
47
+    DCA_PARSE_ERROR_LFE_FLAG        = -8,
48
+    DCA_PARSE_ERROR_PCM_RES         = -9
49
+};
50
+
51
+typedef struct DCACoreFrameHeader {
52
+    uint8_t     normal_frame;       ///< Frame type
53
+    uint8_t     deficit_samples;    ///< Deficit sample count
54
+    uint8_t     crc_present;        ///< CRC present flag
55
+    uint8_t     npcmblocks;         ///< Number of PCM sample blocks
56
+    uint16_t    frame_size;         ///< Primary frame byte size
57
+    uint8_t     audio_mode;         ///< Audio channel arrangement
58
+    uint8_t     sr_code;            ///< Core audio sampling frequency
59
+    uint8_t     br_code;            ///< Transmission bit rate
60
+    uint8_t     drc_present;        ///< Embedded dynamic range flag
61
+    uint8_t     ts_present;         ///< Embedded time stamp flag
62
+    uint8_t     aux_present;        ///< Auxiliary data flag
63
+    uint8_t     hdcd_master;        ///< HDCD mastering flag
64
+    uint8_t     ext_audio_type;     ///< Extension audio descriptor flag
65
+    uint8_t     ext_audio_present;  ///< Extended coding flag
66
+    uint8_t     sync_ssf;           ///< Audio sync word insertion flag
67
+    uint8_t     lfe_present;        ///< Low frequency effects flag
68
+    uint8_t     predictor_history;  ///< Predictor history flag switch
69
+    uint8_t     filter_perfect;     ///< Multirate interpolator switch
70
+    uint8_t     encoder_rev;        ///< Encoder software revision
71
+    uint8_t     copy_hist;          ///< Copy history
72
+    uint8_t     pcmr_code;          ///< Source PCM resolution
73
+    uint8_t     sumdiff_front;      ///< Front sum/difference flag
74
+    uint8_t     sumdiff_surround;   ///< Surround sum/difference flag
75
+    uint8_t     dn_code;            ///< Dialog normalization / unspecified
76
+} DCACoreFrameHeader;
77
+
35 78
 enum DCASpeaker {
36 79
     DCA_SPEAKER_C,    DCA_SPEAKER_L,    DCA_SPEAKER_R,    DCA_SPEAKER_Ls,
37 80
     DCA_SPEAKER_Rs,   DCA_SPEAKER_LFE1, DCA_SPEAKER_Cs,   DCA_SPEAKER_Lsr,
... ...
@@ -165,4 +208,10 @@ extern const uint8_t ff_dca_bits_per_sample[8];
165 165
 int avpriv_dca_convert_bitstream(const uint8_t *src, int src_size, uint8_t *dst,
166 166
                                  int max_size);
167 167
 
168
+/**
169
+ * Parse and validate core frame header
170
+ * @return 0 on success, negative DCA_PARSE_ERROR_ code on failure
171
+ */
172
+int avpriv_dca_parse_core_frame_header(GetBitContext *gb, DCACoreFrameHeader *h);
173
+
168 174
 #endif /* AVCODEC_DCA_H */
... ...
@@ -28,8 +28,8 @@
28 28
 #include "libavutil/version.h"
29 29
 
30 30
 #define LIBAVCODEC_VERSION_MAJOR  57
31
-#define LIBAVCODEC_VERSION_MINOR 100
32
-#define LIBAVCODEC_VERSION_MICRO 104
31
+#define LIBAVCODEC_VERSION_MINOR 101
32
+#define LIBAVCODEC_VERSION_MICRO 100
33 33
 
34 34
 #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
35 35
                                                LIBAVCODEC_VERSION_MINOR, \