pngdec: decode and expose iCCP chunks as side data
[ffmpeg.git] / libavcodec / dca.h
1 /*
2  * DCA compatible decoder
3  * Copyright (C) 2004 Gildas Bazin
4  * Copyright (C) 2004 Benjamin Zores
5  * Copyright (C) 2006 Benjamin Larsson
6  * Copyright (C) 2007 Konstantin Shishkov
7  * Copyright (C) 2016 foo86
8  *
9  * This file is part of FFmpeg.
10  *
11  * FFmpeg is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * FFmpeg is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with FFmpeg; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24  */
25
26 #ifndef AVCODEC_DCA_H
27 #define AVCODEC_DCA_H
28
29 #include <stdint.h>
30
31 #include "libavutil/common.h"
32 #include "libavutil/internal.h"
33 #include "libavutil/intreadwrite.h"
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     DCA_PARSE_ERROR_INVALIDDATA     = -10,
50 };
51
52 typedef struct DCACoreFrameHeader {
53     uint8_t     normal_frame;       ///< Frame type
54     uint8_t     deficit_samples;    ///< Deficit sample count
55     uint8_t     crc_present;        ///< CRC present flag
56     uint8_t     npcmblocks;         ///< Number of PCM sample blocks
57     uint16_t    frame_size;         ///< Primary frame byte size
58     uint8_t     audio_mode;         ///< Audio channel arrangement
59     uint8_t     sr_code;            ///< Core audio sampling frequency
60     uint8_t     br_code;            ///< Transmission bit rate
61     uint8_t     drc_present;        ///< Embedded dynamic range flag
62     uint8_t     ts_present;         ///< Embedded time stamp flag
63     uint8_t     aux_present;        ///< Auxiliary data flag
64     uint8_t     hdcd_master;        ///< HDCD mastering flag
65     uint8_t     ext_audio_type;     ///< Extension audio descriptor flag
66     uint8_t     ext_audio_present;  ///< Extended coding flag
67     uint8_t     sync_ssf;           ///< Audio sync word insertion flag
68     uint8_t     lfe_present;        ///< Low frequency effects flag
69     uint8_t     predictor_history;  ///< Predictor history flag switch
70     uint8_t     filter_perfect;     ///< Multirate interpolator switch
71     uint8_t     encoder_rev;        ///< Encoder software revision
72     uint8_t     copy_hist;          ///< Copy history
73     uint8_t     pcmr_code;          ///< Source PCM resolution
74     uint8_t     sumdiff_front;      ///< Front sum/difference flag
75     uint8_t     sumdiff_surround;   ///< Surround sum/difference flag
76     uint8_t     dn_code;            ///< Dialog normalization / unspecified
77 } DCACoreFrameHeader;
78
79 enum DCASpeaker {
80     DCA_SPEAKER_C,    DCA_SPEAKER_L,    DCA_SPEAKER_R,    DCA_SPEAKER_Ls,
81     DCA_SPEAKER_Rs,   DCA_SPEAKER_LFE1, DCA_SPEAKER_Cs,   DCA_SPEAKER_Lsr,
82     DCA_SPEAKER_Rsr,  DCA_SPEAKER_Lss,  DCA_SPEAKER_Rss,  DCA_SPEAKER_Lc,
83     DCA_SPEAKER_Rc,   DCA_SPEAKER_Lh,   DCA_SPEAKER_Ch,   DCA_SPEAKER_Rh,
84     DCA_SPEAKER_LFE2, DCA_SPEAKER_Lw,   DCA_SPEAKER_Rw,   DCA_SPEAKER_Oh,
85     DCA_SPEAKER_Lhs,  DCA_SPEAKER_Rhs,  DCA_SPEAKER_Chr,  DCA_SPEAKER_Lhr,
86     DCA_SPEAKER_Rhr,  DCA_SPEAKER_Cl,   DCA_SPEAKER_Ll,   DCA_SPEAKER_Rl,
87     DCA_SPEAKER_RSV1, DCA_SPEAKER_RSV2, DCA_SPEAKER_RSV3, DCA_SPEAKER_RSV4,
88
89     DCA_SPEAKER_COUNT
90 };
91
92 enum DCASpeakerMask {
93     DCA_SPEAKER_MASK_C     = 0x00000001,
94     DCA_SPEAKER_MASK_L     = 0x00000002,
95     DCA_SPEAKER_MASK_R     = 0x00000004,
96     DCA_SPEAKER_MASK_Ls    = 0x00000008,
97     DCA_SPEAKER_MASK_Rs    = 0x00000010,
98     DCA_SPEAKER_MASK_LFE1  = 0x00000020,
99     DCA_SPEAKER_MASK_Cs    = 0x00000040,
100     DCA_SPEAKER_MASK_Lsr   = 0x00000080,
101     DCA_SPEAKER_MASK_Rsr   = 0x00000100,
102     DCA_SPEAKER_MASK_Lss   = 0x00000200,
103     DCA_SPEAKER_MASK_Rss   = 0x00000400,
104     DCA_SPEAKER_MASK_Lc    = 0x00000800,
105     DCA_SPEAKER_MASK_Rc    = 0x00001000,
106     DCA_SPEAKER_MASK_Lh    = 0x00002000,
107     DCA_SPEAKER_MASK_Ch    = 0x00004000,
108     DCA_SPEAKER_MASK_Rh    = 0x00008000,
109     DCA_SPEAKER_MASK_LFE2  = 0x00010000,
110     DCA_SPEAKER_MASK_Lw    = 0x00020000,
111     DCA_SPEAKER_MASK_Rw    = 0x00040000,
112     DCA_SPEAKER_MASK_Oh    = 0x00080000,
113     DCA_SPEAKER_MASK_Lhs   = 0x00100000,
114     DCA_SPEAKER_MASK_Rhs   = 0x00200000,
115     DCA_SPEAKER_MASK_Chr   = 0x00400000,
116     DCA_SPEAKER_MASK_Lhr   = 0x00800000,
117     DCA_SPEAKER_MASK_Rhr   = 0x01000000,
118     DCA_SPEAKER_MASK_Cl    = 0x02000000,
119     DCA_SPEAKER_MASK_Ll    = 0x04000000,
120     DCA_SPEAKER_MASK_Rl    = 0x08000000,
121 };
122
123 #define DCA_SPEAKER_LAYOUT_MONO         (DCA_SPEAKER_MASK_C)
124 #define DCA_SPEAKER_LAYOUT_STEREO       (DCA_SPEAKER_MASK_L | DCA_SPEAKER_MASK_R)
125 #define DCA_SPEAKER_LAYOUT_2POINT1      (DCA_SPEAKER_LAYOUT_STEREO | DCA_SPEAKER_MASK_LFE1)
126 #define DCA_SPEAKER_LAYOUT_3_0          (DCA_SPEAKER_LAYOUT_STEREO | DCA_SPEAKER_MASK_C)
127 #define DCA_SPEAKER_LAYOUT_2_1          (DCA_SPEAKER_LAYOUT_STEREO | DCA_SPEAKER_MASK_Cs)
128 #define DCA_SPEAKER_LAYOUT_3_1          (DCA_SPEAKER_LAYOUT_3_0 | DCA_SPEAKER_MASK_Cs)
129 #define DCA_SPEAKER_LAYOUT_2_2          (DCA_SPEAKER_LAYOUT_STEREO | DCA_SPEAKER_MASK_Ls | DCA_SPEAKER_MASK_Rs)
130 #define DCA_SPEAKER_LAYOUT_5POINT0      (DCA_SPEAKER_LAYOUT_3_0 | DCA_SPEAKER_MASK_Ls | DCA_SPEAKER_MASK_Rs)
131 #define DCA_SPEAKER_LAYOUT_5POINT1      (DCA_SPEAKER_LAYOUT_5POINT0 | DCA_SPEAKER_MASK_LFE1)
132 #define DCA_SPEAKER_LAYOUT_7POINT0_WIDE (DCA_SPEAKER_LAYOUT_5POINT0 | DCA_SPEAKER_MASK_Lw | DCA_SPEAKER_MASK_Rw)
133 #define DCA_SPEAKER_LAYOUT_7POINT1_WIDE (DCA_SPEAKER_LAYOUT_7POINT0_WIDE | DCA_SPEAKER_MASK_LFE1)
134
135 #define DCA_HAS_STEREO(mask) \
136     ((mask & DCA_SPEAKER_LAYOUT_STEREO) == DCA_SPEAKER_LAYOUT_STEREO)
137
138 enum DCASpeakerPair {
139     DCA_SPEAKER_PAIR_C      = 0x0001,
140     DCA_SPEAKER_PAIR_LR     = 0x0002,
141     DCA_SPEAKER_PAIR_LsRs   = 0x0004,
142     DCA_SPEAKER_PAIR_LFE1   = 0x0008,
143     DCA_SPEAKER_PAIR_Cs     = 0x0010,
144     DCA_SPEAKER_PAIR_LhRh   = 0x0020,
145     DCA_SPEAKER_PAIR_LsrRsr = 0x0040,
146     DCA_SPEAKER_PAIR_Ch     = 0x0080,
147     DCA_SPEAKER_PAIR_Oh     = 0x0100,
148     DCA_SPEAKER_PAIR_LcRc   = 0x0200,
149     DCA_SPEAKER_PAIR_LwRw   = 0x0400,
150     DCA_SPEAKER_PAIR_LssRss = 0x0800,
151     DCA_SPEAKER_PAIR_LFE2   = 0x1000,
152     DCA_SPEAKER_PAIR_LhsRhs = 0x2000,
153     DCA_SPEAKER_PAIR_Chr    = 0x4000,
154     DCA_SPEAKER_PAIR_LhrRhr = 0x8000
155 };
156
157 /**
158  * Return number of individual channels in DCASpeakerPair mask
159  */
160 static inline int ff_dca_count_chs_for_mask(unsigned int mask)
161 {
162     return av_popcount((mask & 0xffff) | ((mask & 0xae66) << 16));
163 }
164
165 enum DCARepresentationType {
166     DCA_REPR_TYPE_LtRt = 2,
167     DCA_REPR_TYPE_LhRh = 3
168 };
169
170 enum DCAExtensionMask {
171     DCA_CSS_CORE   = 0x001,
172     DCA_CSS_XXCH   = 0x002,
173     DCA_CSS_X96    = 0x004,
174     DCA_CSS_XCH    = 0x008,
175     DCA_CSS_MASK   = 0x00f,
176     DCA_EXSS_CORE  = 0x010,
177     DCA_EXSS_XBR   = 0x020,
178     DCA_EXSS_XXCH  = 0x040,
179     DCA_EXSS_X96   = 0x080,
180     DCA_EXSS_LBR   = 0x100,
181     DCA_EXSS_XLL   = 0x200,
182     DCA_EXSS_RSV1  = 0x400,
183     DCA_EXSS_RSV2  = 0x800,
184     DCA_EXSS_MASK  = 0xff0,
185 };
186
187 enum DCADownMixType {
188     DCA_DMIX_TYPE_1_0,
189     DCA_DMIX_TYPE_LoRo,
190     DCA_DMIX_TYPE_LtRt,
191     DCA_DMIX_TYPE_3_0,
192     DCA_DMIX_TYPE_2_1,
193     DCA_DMIX_TYPE_2_2,
194     DCA_DMIX_TYPE_3_1,
195
196     DCA_DMIX_TYPE_COUNT
197 };
198
199 extern av_export const uint32_t avpriv_dca_sample_rates[16];
200
201 extern const uint32_t ff_dca_sampling_freqs[16];
202 extern const uint8_t ff_dca_freq_ranges[16];
203 extern const uint8_t ff_dca_bits_per_sample[8];
204
205
206 /**
207  * Convert bitstream to one representation based on sync marker
208  */
209 int avpriv_dca_convert_bitstream(const uint8_t *src, int src_size, uint8_t *dst,
210                                  int max_size);
211
212 /**
213  * Parse and validate core frame header
214  * @return 0 on success, negative DCA_PARSE_ERROR_ code on failure
215  */
216 int avpriv_dca_parse_core_frame_header(DCACoreFrameHeader *h, uint8_t *buf, int size);
217
218 int ff_dca_parse_core_frame_header(DCACoreFrameHeader *h, GetBitContext *gb);
219
220 #endif /* AVCODEC_DCA_H */