summaryrefslogtreecommitdiffstats
path: root/edid/lib/edid_parser.c
blob: e08822c518fc8aa14e5439d9cf85db9d1fa3cc3d (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
/*
 * Copyright (C) Texas Instruments - http://www.ti.com/
 *
 * 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.
 *
 */

#include <stdint.h>
#include <stdbool.h>
#include <string.h>

#include <inc/edid_parser.h>
#include "edid_parser_priv.h"

const struct svd_info_t svd_table[] =
{
    {0, 0, 0, HDMI_SCAN_PROGRESSIVE, "reserved"},
    {640, 480, 60, HDMI_SCAN_PROGRESSIVE, "DMT0659"},
    {720, 480, 60, HDMI_SCAN_PROGRESSIVE, "480p"},
    {720, 480, 60, HDMI_SCAN_PROGRESSIVE, "480pH"},
    {1280, 720, 60, HDMI_SCAN_PROGRESSIVE, "720p"},
    {1920, 1080, 60, HDMI_SCAN_INTERLACED, "1080i"},
    {720, 480, 60, HDMI_SCAN_INTERLACED, "480i"},
    {720, 480, 60, HDMI_SCAN_INTERLACED, "480iH"},
    {720, 240, 60, HDMI_SCAN_PROGRESSIVE, "240p"},
    {1280, 720, 60, HDMI_SCAN_PROGRESSIVE, "240pH"},
    {720, 480, 60, HDMI_SCAN_INTERLACED, "480i4x"},
    {720, 480, 60, HDMI_SCAN_INTERLACED, "480i4xH"},
    {720, 240, 60, HDMI_SCAN_PROGRESSIVE, "240p4x"},
    {720, 240, 60, HDMI_SCAN_PROGRESSIVE, "240p4xH"},
    {720, 480, 60, HDMI_SCAN_PROGRESSIVE, "480p2x"},
    {720, 480, 60, HDMI_SCAN_PROGRESSIVE, "480p2xH"},
    {1920, 1080, 60, HDMI_SCAN_PROGRESSIVE, "1080p"},
    {720, 576, 50, HDMI_SCAN_PROGRESSIVE, "576p"},
    {720, 576, 50, HDMI_SCAN_PROGRESSIVE, "576pH"},
    {1280, 720, 50, HDMI_SCAN_PROGRESSIVE, "720p50"},
    {1920, 1080, 50, HDMI_SCAN_INTERLACED, "1080i25"},
    {720, 576, 50, HDMI_SCAN_INTERLACED, "576i"},
    {720, 576, 50, HDMI_SCAN_INTERLACED, "576iH"},
    {720, 288, 50, HDMI_SCAN_PROGRESSIVE, "288p"},
    {720, 288, 50, HDMI_SCAN_PROGRESSIVE, "288pH"},
    {720, 576, 50, HDMI_SCAN_INTERLACED, "576i4x"},
    {720, 576, 50, HDMI_SCAN_INTERLACED, "576i4xH"},
    {720, 288, 50, HDMI_SCAN_PROGRESSIVE, "288p4x"},
    {720, 288, 50, HDMI_SCAN_PROGRESSIVE, "288p4xH"},
    {720, 576, 50, HDMI_SCAN_PROGRESSIVE, "576p2x"},
    {720, 576, 50, HDMI_SCAN_PROGRESSIVE, "576p2xH"},
    {1920, 1080, 50, HDMI_SCAN_PROGRESSIVE, "1080p50"},
    {1920, 1080, 24, HDMI_SCAN_PROGRESSIVE, "1080p24"},
    {1920, 1080, 25, HDMI_SCAN_PROGRESSIVE, "1080p25"},
    {1920, 1080, 30, HDMI_SCAN_PROGRESSIVE, "1080p30"},
    {720, 480, 60, HDMI_SCAN_PROGRESSIVE, "480p4x"},
    {720, 480, 60, HDMI_SCAN_PROGRESSIVE, "480p4xH"},
    {720, 576, 50, HDMI_SCAN_PROGRESSIVE, "576p4x"},
    {720, 576, 50, HDMI_SCAN_PROGRESSIVE, "576p4xH"},
    {1920, 1080, 50, HDMI_SCAN_INTERLACED, "1080i25"},
    {1920, 1080, 100, HDMI_SCAN_INTERLACED, "1080i50"},
    {1280, 720, 100, HDMI_SCAN_PROGRESSIVE, "720p100"},
    {720, 576, 100, HDMI_SCAN_PROGRESSIVE, "576p100"},
    {720, 576, 100, HDMI_SCAN_PROGRESSIVE, "576p100H"},
    {720, 576, 100, HDMI_SCAN_INTERLACED, "576i50"},
    {720, 576, 100, HDMI_SCAN_INTERLACED, "576i50H"},
    {1920, 1080, 120, HDMI_SCAN_INTERLACED, "1080i60"},
    {1280, 720, 120, HDMI_SCAN_PROGRESSIVE, "720p120"},
    {720, 480, 120, HDMI_SCAN_PROGRESSIVE, "480p120"},
    {720, 480, 120, HDMI_SCAN_PROGRESSIVE, "480p120H"},
    {720, 480, 120, HDMI_SCAN_INTERLACED, "480i60"},
    {720, 480, 120, HDMI_SCAN_INTERLACED, "480i60H"},
    {720, 576, 200, HDMI_SCAN_PROGRESSIVE, "576p200"},
    {720, 576, 200, HDMI_SCAN_PROGRESSIVE, "576p200H"},
    {720, 576, 200, HDMI_SCAN_INTERLACED, "576i100"},
    {720, 576, 200, HDMI_SCAN_INTERLACED, "576i100H"},
    {720, 480, 240, HDMI_SCAN_PROGRESSIVE, "480p240"},
    {720, 480, 240, HDMI_SCAN_PROGRESSIVE, "480p240H"},
    {720, 480, 240, HDMI_SCAN_INTERLACED, "480i120"},
    {720, 480, 240, HDMI_SCAN_INTERLACED, "480i120H"},
    {1280, 720, 24, HDMI_SCAN_PROGRESSIVE, "720p24"},
    {1280, 720, 25, HDMI_SCAN_PROGRESSIVE, "720p25"},
    {1280, 720, 30, HDMI_SCAN_PROGRESSIVE, "720p30"},
    {1920, 1080, 120, HDMI_SCAN_PROGRESSIVE, "1080p120"}
};

const int NUM_SVD_ENTRIES = sizeof(svd_table)/sizeof(svd_table[0]);

static unsigned int count_set_bits(uint16_t value)
{
    unsigned int count;
    for (count = 0; value; count++)
        value &= value - 1;
    return count;
}

static void set_s3d_format_bits(const uint8_t *edid_data, int off, int hdmi_3d_len, uint16_t *s3d_struct_bits)
{
    //These are 3D formats signaled through 2D VIC order and 3D_structure-3D_Detail
    while (hdmi_3d_len > 0) {
        unsigned int val = edid_data[off++] & 0x0F;
        *s3d_struct_bits |= 1 << val;
        //3D Detail_x is included for 3D_Structure_x range 0x8-0xF
        if ( val >= HDMI_SIDE_BY_SIDE_HALF) {
            hdmi_3d_len--;
            off++;
        }
        hdmi_3d_len--;
    }
}

static void update_s3d_format(struct edid_t *edid, enum hdmi_3d_format format,
                                uint8_t vic_pos, enum hdmi_3d_subsampling subsamp)
{
    unsigned int format_ix, vic_pos_ix;
    if (vic_pos > edid->num_svds) {
        return;
    }

    for (format_ix = 0; format_ix < edid->num_s3d_formats; format_ix++) {
        if (edid->s3d_format_list[format_ix].format == format) {
            break;
        }
    }

    if (format_ix >= edid->num_s3d_formats) {
        return;
    }

    //In case this has already been signaled, we'll update the subsampling mode
    for (vic_pos_ix = 0; vic_pos_ix < edid->s3d_format_list[format_ix].num_valid_vic; vic_pos_ix++) {
        if (edid->s3d_format_list[format_ix].vic_info[vic_pos_ix].vic_pos == vic_pos) {
            break;
        }
    }

    if (vic_pos_ix >= edid->s3d_format_list[format_ix].num_valid_vic) {
        vic_pos_ix = edid->s3d_format_list[format_ix].num_valid_vic;
        edid->s3d_format_list[format_ix].num_valid_vic += 1;
    }

    edid->s3d_format_list[format_ix].vic_info[vic_pos_ix].vic_pos = vic_pos;
    edid->s3d_format_list[format_ix].vic_info[vic_pos_ix].subsampling = subsamp;
}

/* This function was originally written by Mythri pk */
static int edid_get_datablock_offset(const uint8_t *edid_data, enum datablock_id type, unsigned int *off)
{
    uint8_t val;
    uint8_t ext_length;
    uint8_t offset;

    //CEA extension signaled? If not, then no datablocks are contained
    if (edid_data[0x7e] == 0x00) {
        return 1;
    }

    //18-byte descriptors only? Otherwise, there are datablocks present
    ext_length = edid_data[0x82];
    if (ext_length == 0x4) {
        return 1;
    }

    //Start of first extended data block
    offset = 0x84;
    while (offset < (0x80 + ext_length)) {
        val = edid_data[offset];
        //Upper 3 bits indicate block type
        if ((val >> 5) == type) {
            *off = offset;
            return 0;
        } else {
            //lower 5 bits indicate block length
            offset += (val & 0x1F) + 1;
        }
    }
    return 1;
}

static void edid_parse_s3d_support(struct edid_t *edid, const uint8_t *edid_data)
{
    unsigned int off;
    unsigned int i, count;
    uint8_t val;
    uint8_t s3d_multi_present;
    uint8_t hdmi_3d_len;
    uint16_t s3d_struct_all = 0;
    uint16_t s3d_struct_bits = 0;
    uint16_t hdmi_vic_pos_bits = 0;

    //memset(edid->s3d_formats, 0, sizeof(edid->s3d_formats));

    //S3D HDMI information is signaled in the Vendor Specific datablock
    if(edid_get_datablock_offset(edid_data, DATABLOCK_VENDOR, &off))
        return;

    //Skip header and other non-S3D related fields
    off += 8;
    val = edid_data[off++];

    //HDMI_Video_present?
    if (!(val & 0x20)) {
        return;
    }

    //Latency_Fields_Present? Skip
    if (val & 0x80) {
        off += 2;
    }

    //I_Latency_Fields_Present? Skip
    if (val & 0x40) {
        off += 2;
    }

    val = edid_data[off++];
    //3D_Present?
    if (!(val & 0x80)) {
        return;
    }

    edid->s3d_capable = true;

    s3d_multi_present = (val & 0x60) >> 5;

    //Skip HDMI_XX_LEN
    val = edid_data[off++];
    off += (val & 0xE0) >> 5;
    hdmi_3d_len = (val & 0x1F);

    //3D capabilities signaled through bitmasks
    //s3d_struct_all has all the 3D formats supported (per bit)
    //hdmi_vic_mask has which of the corresponding VIC codes the 3D formats apply to
    //if s3d_multi_present = 1, then the 3D formats apply to all the first 16 VIC codes
    if (s3d_multi_present == 1 || s3d_multi_present == 2) {
        s3d_struct_all = (edid_data[off] << 8) | edid_data[off+1];
        hdmi_vic_pos_bits = 0xFFFF;
        hdmi_3d_len -= 2;
        off += 2;
    }

    if (s3d_multi_present == 2) {
        hdmi_vic_pos_bits = (edid_data[off] << 8) | edid_data[off+1];
        hdmi_3d_len -= 2;
        off += 2;
    }

    //Bit 15 signals same format as Bit 8 - HDMI_SIDE_BY_SIDE_HALF, they only differ in subsampling options
    s3d_struct_bits = s3d_struct_all & 0x7FFF;
    set_s3d_format_bits(edid_data, off, hdmi_3d_len, &s3d_struct_bits);

    edid->num_s3d_formats = count_set_bits(s3d_struct_bits);
    edid->s3d_format_list = (struct hdmi_s3d_format_info_t *) malloc(edid->num_s3d_formats * sizeof(struct hdmi_s3d_format_info_t));

    count = 0;
    for (i = 0; i <= HDMI_SIDE_BY_SIDE_HALF; i++) {
        if (s3d_struct_bits & (1 << i)) {
            edid->s3d_format_list[count++].format = (enum hdmi_3d_format)i;
        }
    }

    for (i = 0; i < edid->num_s3d_formats; i++) {
        unsigned int j;
        enum hdmi_3d_subsampling subsampling;
        if (edid->s3d_format_list[i].format == HDMI_SIDE_BY_SIDE_HALF) {
            uint16_t bitmask = HDMI_SIDE_BY_SIDE_HALF_QUINCUNX_BIT | HDMI_SIDE_BY_SIDE_HALF_BIT;
            if ( (s3d_struct_all & bitmask) == bitmask) {
                subsampling = HDMI_SS_HORZANDQUINCUNX;
            } else if ((s3d_struct_all & bitmask) == HDMI_SIDE_BY_SIDE_HALF_QUINCUNX_BIT) {
                subsampling = HDMI_SS_QUINCUNX_ALL;
            } else if ((s3d_struct_all & bitmask) == HDMI_SIDE_BY_SIDE_HALF_BIT) {
                subsampling = HDMI_SS_HORIZONTAL;
            }
        } else if (edid->s3d_format_list[i].format == HDMI_TOPBOTTOM) {
            subsampling = HDMI_SS_VERTICAL;
        } else {
            subsampling = HDMI_SS_NONE;
        }
        count = 0;
        for (j = 0; j < 16; j++) {
             if ((s3d_struct_all & (1 << edid->s3d_format_list[i].format)) &&
                (hdmi_vic_pos_bits & (1 << j))) {
                edid->s3d_format_list[i].vic_info[count].subsampling = subsampling;
                edid->s3d_format_list[i].vic_info[count++].vic_pos = j;
             }
        }
        edid->s3d_format_list[i].num_valid_vic = count;
    }

    //In this case, the 3D formats signaled only apply to the VIC codes signaled per bit
    //i.e. bit0 = VIC code 0 from the Short video descriptors list
    while (hdmi_3d_len > 0) {
        //Upper 4 bits indicate vic position, lower 4 bits are the 3D structure value
        enum hdmi_3d_subsampling subsampling = HDMI_SS_NONE;
        uint8_t vic_pos = (edid_data[off] & 0xF0) >> 4;
        enum hdmi_3d_format format = (enum hdmi_3d_format) (edid_data[off++] & 0x0F);
        if (format >= HDMI_SIDE_BY_SIDE_HALF) {
            subsampling = (enum hdmi_3d_subsampling)((edid_data[off++] & 0xF0) >> 4);
            hdmi_3d_len--;
        }
        if (format == HDMI_TOPBOTTOM) {
            subsampling = HDMI_SS_VERTICAL;
        }
        update_s3d_format(edid, format, vic_pos, subsampling);
        hdmi_3d_len--;
    }
}

static void edid_fill_svd_info(uint8_t code, struct svd_info_t *info)
{
    if(code > NUM_SVD_ENTRIES)
        code = 0;
    memcpy(info, &svd_table[code], sizeof(struct svd_info_t));
}

static void edid_parse_svds(struct edid_t *edid, const uint8_t *raw_edid_data)
{
    unsigned int offset;
    unsigned int i;
    if (edid_get_datablock_offset(raw_edid_data, DATABLOCK_VIDEO, &offset)) {
        edid->num_svds = 0;
        edid->svd_list = NULL;
        return ;
    }

    edid->num_svds = raw_edid_data[offset] & 0x1F;
    edid->svd_list = (struct svd_t *) malloc(edid->num_svds * sizeof(struct svd_t));
    for (i = 0; i < edid->num_svds; i++) {
        struct svd_t *svd = &edid->svd_list[i];
        svd->code = raw_edid_data[offset + i] & 0x7F;
        svd->native = (raw_edid_data[offset + i] & 0x80) == 0x80;
        edid_fill_svd_info(svd->code, &svd->info);
    }
}

/*=======================================================*/
int edid_parser_init(struct edid_t **edid_handle, const uint8_t *raw_edid_data)
{
    if(edid_handle == NULL) {
        return -1;
    }

    struct edid_t *edid = (struct edid_t *) malloc(sizeof(struct edid_t));
    if (edid == NULL) {
        return -1;
    }

    memset(edid, 0, sizeof(struct edid_t));
    edid_parse_svds(edid, raw_edid_data);
    edid_parse_s3d_support(edid, raw_edid_data);

    *edid_handle = edid;
    return 0;
}

void edid_parser_deinit(struct edid_t *edid)
{
    free(edid->s3d_format_list);
    free(edid->svd_list);
    free(edid);
}

bool edid_s3d_capable(struct edid_t *edid)
{
    return edid->s3d_capable;
}

bool edid_supports_s3d_format(struct edid_t *edid, enum hdmi_3d_format format)
{
    unsigned int i;
    for (i = 0; i < edid->num_s3d_formats; i++) {
        if (edid->s3d_format_list[i].format == format) {
            return true;
        }
    }
    return false;
}

const struct hdmi_s3d_format_info_t * edid_get_s3d_format_info(struct edid_t *edid, enum hdmi_3d_format format)
{
    unsigned int i;
    for (i = 0; i < edid->num_s3d_formats; i++) {
        if (edid->s3d_format_list[i].format == format) {
            return &edid->s3d_format_list[i];
        }
    }
    return NULL;
}

void edid_get_svd_list(struct edid_t *edid, struct svd_t **list, unsigned int *num_elements)
{
    if(list == NULL || num_elements == NULL)
        return;

    *list = edid->svd_list;
    *num_elements = edid->num_svds;
}

const struct svd_t *edid_get_svd_descriptor(struct edid_t *edid, uint8_t vic_pos)
{
    if(vic_pos > edid->num_svds)
        return NULL;
    return &edid->svd_list[vic_pos];
}