1 /*
2 * include/linux/amlogic/media/codec_mm/configs.h
3 *
4 * Copyright (C) 2017 Amlogic, Inc. All rights reserved.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
15 *
16 */
17
18 #include <linux/kernel.h>
19 #include <linux/types.h>
20 #include <linux/vmalloc.h>
21 #include <linux/mm.h>
22 #include <linux/string.h>
23
24 #include "common.h"
25 #include "pixfmt.h"
26
27 const u8 ff_zigzag_direct[64] = {
28 0, 1, 8, 16, 9, 2, 3, 10,
29 17, 24, 32, 25, 18, 11, 4, 5,
30 12, 19, 26, 33, 40, 48, 41, 34,
31 27, 20, 13, 6, 7, 14, 21, 28,
32 35, 42, 49, 56, 57, 50, 43, 36,
33 29, 22, 15, 23, 30, 37, 44, 51,
34 58, 59, 52, 45, 38, 31, 39, 46,
35 53, 60, 61, 54, 47, 55, 62, 63
36 };
37
38 const u8 ff_zigzag_scan[16 + 1] = {
39 0 + 0 * 4, 1 + 0 * 4, 0 + 1 * 4, 0 + 2 * 4,
40 1 + 1 * 4, 2 + 0 * 4, 3 + 0 * 4, 2 + 1 * 4,
41 1 + 2 * 4, 0 + 3 * 4, 1 + 3 * 4, 2 + 2 * 4,
42 3 + 1 * 4, 3 + 2 * 4, 2 + 3 * 4, 3 + 3 * 4,
43 };
44
45 const char * const color_space_names[] = {
46 [AVCOL_SPC_RGB] = "gbr",
47 [AVCOL_SPC_BT709] = "bt709",
48 [AVCOL_SPC_UNSPECIFIED] = "unknown",
49 [AVCOL_SPC_RESERVED] = "reserved",
50 [AVCOL_SPC_FCC] = "fcc",
51 [AVCOL_SPC_BT470BG] = "bt470bg",
52 [AVCOL_SPC_SMPTE170M] = "smpte170m",
53 [AVCOL_SPC_SMPTE240M] = "smpte240m",
54 [AVCOL_SPC_YCGCO] = "ycgco",
55 [AVCOL_SPC_BT2020_NCL] = "bt2020nc",
56 [AVCOL_SPC_BT2020_CL] = "bt2020c",
57 [AVCOL_SPC_SMPTE2085] = "smpte2085",
58 [AVCOL_SPC_CHROMA_DERIVED_NCL] = "chroma-derived-nc",
59 [AVCOL_SPC_CHROMA_DERIVED_CL] = "chroma-derived-c",
60 [AVCOL_SPC_ICTCP] = "ictcp",
61 };
62
av_color_space_name(enum AVColorSpace space)63 const char *av_color_space_name(enum AVColorSpace space)
64 {
65 return (unsigned) space < AVCOL_SPC_NB ?
66 color_space_names[space] : NULL;
67 }
68
69 const char * const color_primaries_names[AVCOL_PRI_NB] = {
70 [AVCOL_PRI_RESERVED0] = "reserved",
71 [AVCOL_PRI_BT709] = "bt709",
72 [AVCOL_PRI_UNSPECIFIED] = "unknown",
73 [AVCOL_PRI_RESERVED] = "reserved",
74 [AVCOL_PRI_BT470M] = "bt470m",
75 [AVCOL_PRI_BT470BG] = "bt470bg",
76 [AVCOL_PRI_SMPTE170M] = "smpte170m",
77 [AVCOL_PRI_SMPTE240M] = "smpte240m",
78 [AVCOL_PRI_FILM] = "film",
79 [AVCOL_PRI_BT2020] = "bt2020",
80 [AVCOL_PRI_SMPTE428] = "smpte428",
81 [AVCOL_PRI_SMPTE431] = "smpte431",
82 [AVCOL_PRI_SMPTE432] = "smpte432",
83 [AVCOL_PRI_JEDEC_P22] = "jedec-p22",
84 };
85
av_color_primaries_name(enum AVColorPrimaries primaries)86 const char *av_color_primaries_name(enum AVColorPrimaries primaries)
87 {
88 return (unsigned) primaries < AVCOL_PRI_NB ?
89 color_primaries_names[primaries] : NULL;
90 }
91
92 const char * const color_transfer_names[] = {
93 [AVCOL_TRC_RESERVED0] = "reserved",
94 [AVCOL_TRC_BT709] = "bt709",
95 [AVCOL_TRC_UNSPECIFIED] = "unknown",
96 [AVCOL_TRC_RESERVED] = "reserved",
97 [AVCOL_TRC_GAMMA22] = "bt470m",
98 [AVCOL_TRC_GAMMA28] = "bt470bg",
99 [AVCOL_TRC_SMPTE170M] = "smpte170m",
100 [AVCOL_TRC_SMPTE240M] = "smpte240m",
101 [AVCOL_TRC_LINEAR] = "linear",
102 [AVCOL_TRC_LOG] = "log100",
103 [AVCOL_TRC_LOG_SQRT] = "log316",
104 [AVCOL_TRC_IEC61966_2_4] = "iec61966-2-4",
105 [AVCOL_TRC_BT1361_ECG] = "bt1361e",
106 [AVCOL_TRC_IEC61966_2_1] = "iec61966-2-1",
107 [AVCOL_TRC_BT2020_10] = "bt2020-10",
108 [AVCOL_TRC_BT2020_12] = "bt2020-12",
109 [AVCOL_TRC_SMPTE2084] = "smpte2084",
110 [AVCOL_TRC_SMPTE428] = "smpte428",
111 [AVCOL_TRC_ARIB_STD_B67] = "arib-std-b67",
112 };
113
av_color_transfer_name(enum AVColorTransferCharacteristic transfer)114 const char *av_color_transfer_name(enum AVColorTransferCharacteristic transfer)
115 {
116 return (unsigned) transfer < AVCOL_TRC_NB ?
117 color_transfer_names[transfer] : NULL;
118 }
119
120 //math
121 const u8 ff_log2_tab[256]={
122 0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
123 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
124 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
125 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
126 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
127 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
128 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
129 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7
130 };
131
av_log2(u32 v)132 int av_log2(u32 v)
133 {
134 int n = 0;
135
136 if (v & 0xffff0000) {
137 v >>= 16;
138 n += 16;
139 }
140 if (v & 0xff00) {
141 v >>= 8;
142 n += 8;
143 }
144 n += ff_log2_tab[v];
145
146 return n;
147 }
148
149 //bitstream
find_start_code(u8 * data,int data_sz)150 int find_start_code(u8 *data, int data_sz)
151 {
152 if (data_sz > 3 && data[0] == 0 && data[1] == 0 && data[2] == 1)
153 return 3;
154
155 if (data_sz > 4 && data[0] == 0 && data[1] == 0 && data[2] == 0 && data[3] == 1)
156 return 4;
157
158 return -1;
159 }
160
calc_nal_len(u8 * data,int len)161 int calc_nal_len(u8 *data, int len)
162 {
163 int i;
164
165 for (i = 0; i < len - 4; i++) {
166 if (data[i])
167 continue;
168
169 if ((data[i] == 0 && data[i + 1] == 0 && data[i + 2] == 1) ||
170 (data[i] == 0 && data[i + 1] == 0 &&
171 data[i + 2]==0 && data[i + 3] == 1))
172 return i;
173 }
174 return len; //Not find the end of nalu
175 }
176
nal_unit_extract_rbsp(const u8 * src,u32 src_len,u32 * dst_len)177 u8 *nal_unit_extract_rbsp(const u8 *src, u32 src_len, u32 *dst_len)
178 {
179 u8 *dst;
180 u32 i, len;
181
182 dst = vmalloc(src_len + AV_INPUT_BUFFER_PADDING_SIZE);
183 if (!dst)
184 return NULL;
185
186 /* NAL unit header (2 bytes) */
187 i = len = 0;
188 while (i < 2 && i < src_len)
189 dst[len++] = src[i++];
190
191 while (i + 2 < src_len)
192 if (!src[i] && !src[i + 1] && src[i + 2] == 3) {
193 dst[len++] = src[i++];
194 dst[len++] = src[i++];
195 i++; // remove emulation_prevention_three_byte
196 } else
197 dst[len++] = src[i++];
198
199 while (i < src_len)
200 dst[len++] = src[i++];
201
202 memset(dst + len, 0, AV_INPUT_BUFFER_PADDING_SIZE);
203
204 *dst_len = len;
205
206 return dst;
207 }
208
209 //debug
_pr_hex(const char * fmt,...)210 static void _pr_hex(const char *fmt, ...)
211 {
212 u8 buf[512];
213 int len = 0;
214
215 va_list args;
216 va_start(args, fmt);
217 vsnprintf(buf + len, 512 - len, fmt, args);
218 printk("%s", buf);
219 va_end(args);
220 }
221
print_hex_debug(u8 * data,u32 len,int max)222 void print_hex_debug(u8 *data, u32 len, int max)
223 {
224 int i, l;
225
226 l = len > max ? max : len;
227
228 for (i = 0; i < l; i++) {
229 if ((i & 0xf) == 0)
230 _pr_hex("%06x:", i);
231 _pr_hex("%02x ", data[i]);
232 if ((((i + 1) & 0xf) == 0) || ((i + 1) == l))
233 _pr_hex("\n");
234 }
235
236 _pr_hex("print hex ending. len %d\n\n", l);
237 }
238
239