Debian Codegraph @

utils/edid-decode/parse-if.cpp

raw
1 // SPDX-License-Identifier: MIT
2 /*
3 * Copyright 2024 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
4 *
5 * Author: Hans Verkuil <[email protected]>
6 */
7
8 #include <stdio.h>
9
10 #include "edid-decode.h"
11
12 int edid_state::parse_if_hdr(const unsigned char *x, unsigned size, unsigned char mask)
13 {
14 unsigned char length = x[2];
15
16 printf("%s\n", data_block.c_str());
17 printf(" Version: %u\n", x[1] & mask);
18 printf(" Length: %u\n", length);
19
20 if (length + 3U > size) {
21 fprintf(stderr, "\nExpected InfoFrame total length of %d, but have only %d bytes.\n",
22 length + 3, size);
23 fail("Expected InfoFrame total length of %d, but have only %d bytes.\n",
24 length + 3, size);
25 return -1;
26 }
27 if (length + 3U < size) {
28 warn("There are %d dummy bytes after the payload.\n", size - length - 3);
29 if (!memchk(x + length + 3, length + 3 - size))
30 warn("There are non-zero dummy bytes after the payload.\n");
31 }
32 return 0;
33 }
34
35 static const char *Structure_map[] = {
36 "Frame packing",
37 "Field alternative",
38 "Line alternative",
39 "Side-by-Side (Full)",
40 "L + depth",
41 "L + depth + graphics + graphics-depth",
42 "Top-and-Bottom",
43 "Reserved (7)",
44 "Side-by-Side (Half)",
45 "Reserved (9)",
46 "Reserved (10)",
47 "Reserved (11)",
48 "Reserved (12)",
49 "Reserved (13)",
50 "Reserved (14)",
51 "Not in use"
52 };
53
54 static const char *ExtData_map[] = {
55 "Horizontal sub-sampling (0)",
56 "Horizontal sub-sampling (1)",
57 "Horizontal sub-sampling (2)",
58 "Horizontal sub-sampling (3)",
59 "Quincunx matrix: Odd/Left picture, Odd/Right picture",
60 "Quincunx matrix: Odd/Left picture, Even/Right picture",
61 "Quincunx matrix: Even/Left picture, Odd/Right picture",
62 "Quincunx matrix: Even/Left picture, Even/Right picture"
63 };
64
65 void edid_state::parse_if_hdmi(const unsigned char *x, unsigned len)
66 {
67 if (len < 4) {
68 fail("Expected InfoFrame length of at least 4, got %d.\n", len);
69 return;
70 }
71 if (x[4] & 0x1f)
72 fail("Bits 4-0 of PB4 are not 0.\n");
73
74 printf(" HDMI Video Format: ");
75
76 char buf[32];
77
78 switch (x[4] >> 5) {
79 case 0:
80 printf("No additional data\n");
81 if (!memchk(x + 5, len - 4))
82 fail("Trailing non-0 bytes.\n");
83 return;
84 case 1: {
85 printf("HDMI_VIC is present\n");
86
87 if (len < 5) {
88 fail("Expected InfoFrame length of at least 5, got %d.\n", len);
89 return;
90 }
91 sprintf(buf, "HDMI VIC %u", x[5]);
92 const struct timings *t = find_hdmi_vic_id(x[5]);
93 if (!t) {
94 printf(" HDMI VIC: %d (0x%02x)\n", x[5], x[5]);
95 fail("Unknown HDMI VIC code.\n");
96 } else {
97 print_timings(" ", t, buf);
98 }
99 if (!memchk(x + 6, len - 5))
100 fail("Trailing non-0 bytes.\n");
101 return;
102 }
103 case 2:
104 printf("3D format indication present\n");
105
106 if (len < 5) {
107 fail("Expected InfoFrame length of at least 5, got %d.\n", len);
108 return;
109 }
110 break;
111 default:
112 printf("Reserved (%d)\n", x[4] >> 5);
113 fail("Invalid HDMI Video Format (%d).\n", x[4] >> 5);
114 return;
115 }
116
117 // Parsing of 3D extension continues here
118 unsigned char v = x[5] >> 4;
119
120 printf(" 3D Structure: %s\n", Structure_map[v]);
121 if (x[5] & 7)
122 fail("Bits 2-0 of PB5 are not 0.\n");
123 printf("3D Metadata Present: %s\n", (x[5] & 8) ? "Yes" : "No");
124
125 if (v < 8) {
126 if (!memchk(x + 6, len - 5))
127 fail("Trailing non-0 bytes.\n");
128 return;
129 }
130
131 if (len < 6) {
132 fail("Expected InfoFrame length of at least 6, got %d.\n", len);
133 return;
134 }
135
136 if (x[6] & 0xf)
137 fail("Bits 3-0 of PB6 are not 0.\n");
138
139 if ((x[6] >> 4) >= 8)
140 printf(" 3D Extended Data: Reserved (%d)\n", (x[6] >> 4));
141 else
142 printf(" 3D Extended Data: %s\n", ExtData_map[x[6] >> 4]);
143
144 if (!(x[5] & 8)) {
145 if (!memchk(x + 7, len - 6))
146 fail("Trailing non-0 bytes.\n");
147 return;
148 }
149
150 if (len < 7) {
151 fail("Expected InfoFrame length of at least 7, got %d.\n", len);
152 return;
153 }
154 unsigned mlen = x[7] & 0x1f;
155 if (len < mlen + 7) {
156 fail("Expected InfoFrame length of at least %d, got %d.\n", mlen + 7, len);
157 return;
158 }
159 if (!memchk(x + mlen + 7, len - mlen - 6))
160 fail("Trailing non-0 bytes.\n");
161 if (x[7] >> 5) {
162 printf(" 3D Metadata Type: Reserved (%d)\n", x[7] >> 5);
163 fail("Invalid 3D Metadata Type.\n");
164 return;
165 } else {
166 printf(" 3D Metadata Type: Parallax Information\n");
167 }
168 printf(" Metadata: Parallax Zero: %u\n", (x[8] << 8) | x[9]);
169 printf(" Metadata: Parallax Scale: %u\n", (x[10] << 8) | x[11]);
170 printf(" Metadata: DRef: %u\n", (x[12] << 8) | x[13]);
171 printf(" Metadata: WRef: %u\n", (x[14] << 8) | x[15]);
172 }
173
174 void edid_state::parse_if_hdmi_forum(const unsigned char *x, unsigned len)
175 {
176 if (len < 5) {
177 fail("Expected InfoFrame length of at least 5, got %d.\n", len);
178 return;
179 }
180 if (x[5] & 0x0c)
181 fail("Bits 3-2 of PB5 are not 0.\n");
182 unsigned char v = x[5] >> 4;
183 printf(" Color Content Bits Per Component: ");
184 if (!v)
185 printf("No Indication\n");
186 else if (v > 9)
187 printf("Reserved (%d)\n", v);
188 else
189 printf("%d bits\n", v + 7);
190 if (x[5] & 1)
191 printf(" 3D Valid\n");
192 printf(" Auto Low-Latency Mode: %s\n", (x[5] & 2) ? "Yes" : "No");
193 if (!(x[5] & 1)) {
194 if (!memchk(x + 6, len - 5))
195 fail("Trailing non-0 bytes.\n");
196 return;
197 }
198
199 // Parsing of 3D extension continues here
200 unsigned offset = 6;
201 v = x[offset] >> 4;
202
203 if (len < offset)
204 goto err_len;
205
206 printf(" 3D Structure: %s\n", Structure_map[v]);
207 if (x[offset] & 1)
208 fail("Bit 0 of PB6 is not 0.\n");
209 printf("3D Additional Info Present: %s\n", (x[offset] & 8) ? "Yes" : "No");
210 printf("3D Disparity Data Present: %s\n", (x[offset] & 4) ? "Yes" : "No");
211 printf("3D Metadata Present: %s\n", (x[offset] & 2) ? "Yes" : "No");
212 offset++;
213
214 if (v >= 8) {
215 if (len < offset)
216 goto err_len;
217
218 if (x[offset] & 0xf)
219 fail("Bits 3-0 of PB7 are not 0.\n");
220
221 if ((x[offset] >> 4) >= 8)
222 printf(" 3D Extended Data: Reserved (%d)\n", (x[offset] >> 4));
223 else
224 printf(" 3D Extended Data: %s\n", ExtData_map[x[offset] >> 4]);
225 offset++;
226 }
227
228 if (x[6] & 8) {
229 if (len < offset)
230 goto err_len;
231
232 if (x[offset] & 0xe0)
233 fail("Bits 7-5 if PB%d are not 0.\n", offset);
234 printf(" 3D Dual View: %s\n", (x[offset] & 0x10) ? "Yes" : "No");
235
236 static const char *ViewDep_map[] = {
237 "No Indication",
238 "Independent Right View",
239 "Independent Left View",
240 "Independent Right and Left Views"
241 };
242 printf(" 3D View Dependency: %s\n", ViewDep_map[(x[offset] & 0x0c) >> 2]);
243
244 static const char *Pref2DView_map[] = {
245 "No Indication",
246 "Right View",
247 "Left View",
248 "Either View"
249 };
250 printf(" 3D Preferred 2D View: %s\n", Pref2DView_map[x[offset] & 3]);
251 offset++;
252 }
253
254 if (x[6] & 4) {
255 if (len < offset)
256 goto err_len;
257
258 printf(" Disparity Data Version: %d\n", x[offset] >> 5);
259 unsigned dlen = x[offset] & 0x1f;
260 printf(" Disparity Data Length: %d\n", dlen);
261 if (len < offset + dlen)
262 goto err_len;
263 offset++;
264
265 hex_block(" Disparity Data Payload: ", x + offset, dlen, false, dlen);
266
267 offset += dlen;
268 }
269
270 if (x[6] & 2) {
271 if (len < offset)
272 goto err_len;
273
274 if (x[offset] >> 5) {
275 printf(" 3D Metadata Type: Reserved (%d)\n", x[offset] >> 5);
276 fail("Invalid 3D Metadata Type.\n");
277 return;
278 }
279 printf(" 3D Metadata Type: Parallax Information\n");
280 printf(" 3D Metadata Length: %d\n", x[offset] & 0x1f);
281 if (len < offset + (x[offset] & 0x1f))
282 goto err_len;
283 offset++;
284
285 printf(" Metadata: Parallax Zero: %u\n", (x[offset] << 8) | x[offset+1]);
286 printf(" Metadata: Parallax Scale: %u\n", (x[offset+2] << 8) | x[offset+3]);
287 printf(" Metadata: DRef: %u\n", (x[offset+4] << 8) | x[offset+5]);
288 printf(" Metadata: WRef: %u\n", (x[offset+6] << 8) | x[offset+7]);
289
290 offset += x[offset - 1] & 0x1f;
291 }
292
293 if (!memchk(x + offset + 1, len - offset))
294 fail("Trailing non-0 bytes.\n");
295 return;
296
297 err_len:
298 fail("Expected InfoFrame length of at least %d, got %d.\n", offset, len);
299 return;
300 }
301
302 void edid_state::parse_if_vendor(const unsigned char *x, unsigned size)
303 {
304 data_block = "Vendor-Specific InfoFrame";
305
306 unsigned oui;
307 unsigned char len = x[2];
308
309 data_block_oui(data_block, x + 3, 3, &oui, false, false, false, true);
310
311 if (parse_if_hdr(x, size, (x[1] & 0x7f) == 2 ? 0x7f : 0xff))
312 return;
313
314 if (x[1] != 1 && (x[1] & 0x7f) != 2) {
315 fail("Invalid version %d\n", x[1] & 0x7f);
316 return;
317 }
318 if (len < 3) {
319 fail("Expected InfoFrame length of at least 3, got %d.\n", len);
320 return;
321 }
322
323 if (x[1] != 1)
324 printf(" VSIF Change: %s\n", (x[1] & 0x80) ? "Yes" : "No");
325
326 // After this x[1] will refer to Data Byte 1
327 x += 2;
328
329 switch (oui) {
330 case kOUI_HDMI:
331 parse_if_hdmi(x, len);
332 break;
333 case kOUI_HDMIForum:
334 parse_if_hdmi_forum(x, len);
335 break;
336 default:
337 hex_block(" Payload: ", x + 4, len - 3, false, len - 3);
338 break;
339 }
340 }
341
342 void edid_state::parse_if_avi(const unsigned char *x, unsigned size)
343 {
344 unsigned char version = x[1];
345 unsigned char length = x[2];
346
347 data_block = "AVI InfoFrame";
348
349 if (parse_if_hdr(x, size))
350 return;
351
352 if (version == 0 || version > 4) {
353 fail("Invalid version %u\n", version);
354 return;
355 }
356 if (version == 1)
357 fail("Sources shall not use version 1.\n");
358 if (length < 13) {
359 fail("Expected InfoFrame length of 13, got %u.\n", length);
360 return;
361 }
362
363 // After this x[1] will refer to Data Byte 1
364 x += 2;
365
366 if (version == 1) {
367 if (x[3] & 0xfc)
368 fail("Bits F37-F32 are not 0.\n");
369 if (x[4])
370 fail("Bits F47-F40 are not 0.\n");
371 if (x[5])
372 fail("Bits F57-F50 are not 0.\n");
373 }
374 if (version == 2) {
375 if (x[1] & 0x80)
376 fail("Bit Y2 is not 0.\n");
377 if (x[4] & 0x80)
378 fail("Bit VIC7 is not 0.\n");
379 }
380 if (version == 4 && length == 15) {
381 if (x[15] & 0x80)
382 fail("Bit F157 is not 0.\n");
383 }
384 if (version == 4 && length < 14) {
385 fail("Version 4 expects a length of >= 14, got %u\n", length);
386 return;
387 }
388
389 if (edid_size) {
390 if (version > cta.avi_version)
391 warn("AVI version is %u, but EDID indicates support for %u only.\n",
392 version, cta.avi_version);
393 if (version == 4 && length > cta.avi_v4_length)
394 warn("AVI version 4 length is %u, but EDID indicates support for %u only.\n",
395 length, cta.avi_v4_length);
396 }
397
398 char buf[32];
399 unsigned vic_fps = 0;
400 unsigned char vic = x[4];
401 unsigned char rid = length >= 14 ? x[15] & 0x3f : 0;
402
403 sprintf(buf, "VIC %3u", vic);
404 const struct timings *t = find_vic_id(vic);
405 if (t) {
406 print_timings(" ", t, buf);
407 vic_fps = calc_fps(t);
408 } else if (vic) {
409 printf(" VIC: %d (0x%02x)\n", vic, vic);
410 fail("Unknown VIC code.\n");
411 }
412
413 static const char *Y_map[] = {
414 "RGB",
415 "YCbCr 4:2:2",
416 "YCbCr 4:4:4",
417 "YCbCr 4:2:0",
418 "Reserved (4)",
419 "Reserved (5)",
420 "Reserved (6)",
421 "IDO-Defined",
422 };
423 unsigned char y = x[1] >> 5;
424 unsigned char v;
425
426 printf(" Y: Color Component Sample Format: %s\n", Y_map[y]);
427 if (y == 7 && version == 2)
428 warn("Y == 7 but AVI Version == 2.\n");
429 if (edid_size) {
430 if ((y == 1 && !cta.has_ycbcr422) ||
431 (y == 2 && !cta.has_ycbcr444) ||
432 (y == 3 && !cta.has_ycbcr420))
433 fail("Y == %s, but this capability is not enabled in the EDID.\n", Y_map[y]);
434 }
435
436 printf(" A: Active Format Information Present: %s\n",
437 ((x[1] >> 4) & 1) ? "Yes" : "No");
438
439 static const char *B_map[] = {
440 "Bar Data not present",
441 "Vertical Bar Info present",
442 "Horizontal Bar Info present",
443 "Vertical and Horizontal Bar Info present"
444 };
445 printf(" B: Bar Data Present: %s\n", B_map[(x[1] >> 2) & 3]);
446
447 static const char *S_map[] = {
448 "No Data",
449 "Composed for an overscanned display",
450 "Composed for an underscanned display",
451 "Reserved"
452 };
453 printf(" S: Scan Information: %s\n", S_map[x[1] & 3]);
454
455 static const char *C_map[] = {
456 "No Data",
457 "SMPTE ST 170",
458 "Rec. ITU-R BT.709",
459 "Extended Colorimetry Information Valid"
460 };
461 printf(" C: Colorimetry: %s\n", C_map[x[2] >> 6]);
462
463 static const char *M_map[] = {
464 "No Data",
465 "4:3",
466 "16:9",
467 "Reserved"
468 };
469 v = (x[2] >> 4) & 3;
470 printf(" M: Picture Aspect Ratio: %s\n", M_map[v]);
471 if ((vic || rid) && v)
472 warn("If a VID or RID is specified, then set M to 0.\n");
473 printf(" R: Active Portion Aspect Ratio: %d\n", x[2] & 0xf);
474
475 static const char *ITC_map[] = {
476 "No Data",
477 "IT Content (CN is valid)"
478 };
479 printf(" ITC: IT Content: %s\n", ITC_map[x[3] >> 7]);
480
481 static const char *EC_map[] = {
482 "xvYCC601",
483 "xvYCC709",
484 "sYCC601",
485 "opYCC601",
486 "opRGB",
487 "Rec. ITU-R BR.2020 YcCbcCrc",
488 "Rec. ITU-R BR.2020 RGB or YCbCr",
489 "Additional Colorimetry Extension Information Valid"
490 };
491 printf(" EC: Extended Colorimetry: %s\n", EC_map[(x[3] >> 4) & 7]);
492
493 static const char *Q_map[] = {
494 "Default",
495 "Limited Range",
496 "Full Range",
497 "Reserved"
498 };
499 printf(" Q: RGB Quantization Range: %s\n", Q_map[(x[3] >> 2) & 3]);
500
501 static const char *SC_map[] = {
502 "No Known non-uniform scaling",
503 "Picture has been scaled horizontally",
504 "Picture has been scaled vertically",
505 "Picture has been scaled horizontally and vertically"
506 };
507 printf(" SC: Non-Uniform Picture Scaling: %s\n", SC_map[x[3] & 3]);
508
509 static const char *YQ_map[] = {
510 "Limited Range",
511 "Full Range",
512 "Reserved",
513 "Reserved"
514 };
515 printf(" YQ: YCC Quantization Range: %s\n", YQ_map[x[5] >> 6]);
516
517 static const char *CN_map[] = {
518 "Graphics",
519 "Photo",
520 "Cinema",
521 "Game"
522 };
523 printf(" CN: IT Content Type: %s\n", CN_map[(x[5] >> 4) & 3]);
524 unsigned char pr = x[5] & 0xf;
525 printf(" PR: Pixel Data Repetition Count: %d\n", pr);
526
527 const unsigned short pr_2 = 2;
528 const unsigned short pr_1_10 = 0x3ff;
529 const unsigned short pr_1_2 = 3;
530 const unsigned short pr_1_2_4 = 0xb;
531 static const unsigned short vic_valid_pr[] = {
532 // VIC 0-7
533 0, 0, 0, 0, 0, 0, pr_2, pr_2,
534 // VIC 8-15
535 pr_2, pr_2, pr_1_10, pr_1_10, pr_1_10, pr_1_10, pr_1_2, pr_1_2,
536 // VIC 16-23
537 0, 0, 0, 0, 0, pr_2, pr_2, pr_2,
538 // VIC 24-31
539 pr_2, pr_1_10, pr_1_10, pr_1_10, pr_1_10, pr_1_2, pr_1_2, 0,
540 // VIC 32-39
541 0, 0, 0, pr_1_2_4, pr_1_2_4, pr_1_2_4, pr_1_2_4, 0,
542 // VIC 40-47
543 0, 0, 0, 0, pr_2, pr_2, 0, 0,
544 // VIC 48-55
545 0, 0, pr_2, pr_2, 0, 0, pr_2, pr_2,
546 // VIC 56-59
547 0, 0, pr_2, pr_2
548 };
549
550 if (pr >= 10)
551 fail("PR >= 10 is a reserved value.\n");
552 else if (pr && (vic >= ARRAY_SIZE(vic_valid_pr) ||
553 !(vic_valid_pr[vic] & (1 << pr))))
554 fail("PR %u is not supported by VIC %u.\n", pr, vic);
555
556 printf(" Line Number of End of Top Bar: %d\n", (x[7] << 8) | x[6]);
557 printf(" Line Number of Start of Bottom Bar: %d\n", (x[9] << 8) | x[8]);
558 printf(" Pixel Number of End of Left Bar: %d\n", (x[11] << 8) | x[10]);
559 printf(" Pixel Number of Start of Right Bar: %d\n", (x[13] << 8) | x[12]);
560 if (length <= 13)
561 return;
562
563 static const char *ACE_map[] = {
564 "SMPTE ST 2113 P3D65 RGB",
565 "SMPTE ST 2113 P3DCI RGB",
566 "Rec. ITU-R BT.2100 ICtCp",
567 "sRGB",
568 "defaultRGB",
569 "Reserved (5)",
570 "Reserved (6)",
571 "Reserved (7)",
572 "Reserved (8)",
573 "Reserved (9)",
574 "Reserved (10)",
575 "Reserved (11)",
576 "Reserved (12)",
577 "Reserved (13)",
578 "Reserved (14)",
579 "Reserved (15)"
580 };
581 printf(" ACE: Additional Colorimetry Extension: %s\n", ACE_map[x[14] >> 4]);
582 if (length <= 14) {
583 if (x[14] & 0xf) {
584 printf(" FR: %d\n", x[14] & 0xf);
585 fail("InfoFrame length is 14, but FR != 0.\n");
586 }
587 return;
588 }
589
590 unsigned fr = ((x[15] & 0x40) >> 2) | (x[14] >> 4);
591
592 printf(" RID/FR: %u/%u\n", rid, fr);
593 if (vic && rid)
594 fail("Both a RID and a VIC were specified.\n");
595
596 if (!rid && !fr)
597 return;
598
599 if (rid && !fr) {
600 fail("RID is set, but FR is 0.\n");
601 return;
602 }
603
604 static const unsigned fr_rate_values[] = {
605 /* FR 0-7 */
606 0, 24, 24, 25, 30, 30, 48, 48,
607 /* FR 8-15 */
608 50, 60, 60, 100, 120, 120, 144, 144,
609 /* FR 16-23 */
610 200, 240, 240, 300, 360, 360, 400, 480,
611 /* FR 24 */
612 480
613 };
614 static const bool fr_ntsc[] = {
615 /* FR 0-7 */
616 0, 1, 0, 0, 1, 0, 1, 0,
617 /* FR 8-15 */
618 0, 1, 0, 0, 1, 0, 1, 0,
619 /* FR 16-23 */
620 0, 1, 0, 0, 1, 0, 0, 1,
621 /* FR 24 */
622 0
623 };
624
625 if (fr >= ARRAY_SIZE(fr_rate_values)) {
626 fail("Unknown FR %u.\n", fr);
627 return;
628 }
629 unsigned fps = fr_rate_values[fr];
630 bool ntsc = fr_ntsc[fr];
631
632 printf(" Frame Rate: %u%s\n", fps, ntsc ? "/1.001" : "");
633
634 if (vic) {
635 if (vic_fps != fps)
636 warn("VIC %u is %u fps, while FR indicates %u fps.\n",
637 x[4], vic_fps, fps);
638 return;
639 }
640
641 if (!rid)
642 return;
643
644 const struct cta_rid *crid = find_rid(rid);
645
646 if (!crid) {
647 fail("Unknown RID %u.\n", rid);
648 return;
649 }
650
651 unsigned char rid_vic = rid_fps_to_vic(rid, fps);
652 if (rid_vic) {
653 sprintf(buf, "VIC %3u", rid_vic);
654 const struct timings *t = find_vic_id(rid_vic);
655 print_timings(" ", t, buf);
656 warn("RID/FR %u/%u maps to VIC %d.\n", rid, fr, rid_vic);
657 } else {
658 sprintf(buf, "RID/FR %u/%u", rid, fr);
659 timings t = calc_ovt_mode(crid->hact, crid->vact,
660 crid->hratio, crid->vratio, fps);
661 print_timings("", &t, buf, "", false, false, ntsc);
662 }
663 }
664
665 void edid_state::parse_if_spd(const unsigned char *x, unsigned size)
666 {
667 data_block = "Source Product Description InfoFrame";
668
669 if (parse_if_hdr(x, size))
670 return;
671
672 if (x[1] != 1) {
673 fail("Invalid version %d\n", x[1]);
674 return;
675 }
676 if (x[2] < 25) {
677 fail("Expected InfoFrame length of 25, got %d.\n", x[2]);
678 return;
679 }
680
681 // After this x[1] will refer to Data Byte 1
682 x += 2;
683
684 for (unsigned i = 1; i <= 24; i++) {
685 if (x[i] & 0x80) {
686 fail("SPD contains ASCII character with bit 7 set.\n");
687 }
688 }
689 char vendor[9] = {};
690 memcpy(vendor, x + 1, 8);
691 printf(" Vendor Name: '%s'\n", vendor);
692 unsigned len = strlen(vendor);
693 if (!memchk(x + 1 + len, 8 - len))
694 fail("Vendor name has trailing non-zero characters.\n");
695
696 char product[17] = {};
697 memcpy(product, x + 9, 16);
698 printf(" Product Description: '%s'\n", product);
699 len = strlen(product);
700 if (!memchk(x + 9 + len, 16 - len))
701 fail("Product name has trailing non-zero characters.\n");
702
703 if (x[25] >= 0x0e) {
704 printf(" Source Information: %d (Reserved)\n", x[25]);
705 fail("Source Information value %d is reserved.\n", x[25]);
706 } else {
707 static const char *SI_map[] = {
708 "Unknown",
709 "Digital STB",
710 "DVD player",
711 "D-VHS",
712 "HDD Videorecorder",
713 "DVC",
714 "DSC",
715 "Video CD",
716 "Game",
717 "PC general",
718 "Blu-Ray Disck (DB)",
719 "Super Audio CD",
720 "HD DVD",
721 "PMP"
722 };
723 printf(" Source Information: %s\n", SI_map[x[25]]);
724 }
725 }
726
727 void edid_state::parse_if_audio(const unsigned char *x, unsigned size)
728 {
729 data_block = "Audio InfoFrame";
730
731 if (parse_if_hdr(x, size))
732 return;
733
734 if (x[1] != 1) {
735 fail("Invalid version %d\n", x[1]);
736 return;
737 }
738 if (x[2] < 10) {
739 fail("Expected InfoFrame length of 10, got %d.\n", x[2]);
740 return;
741 }
742
743 // After this x[1] will refer to Data Byte 1
744 x += 2;
745
746 if (x[1] & 0x08)
747 fail("Bit F13 is not 0.\n");
748 if (x[2] & 0xe0)
749 fail("Bits F27-F25 are not 0.\n");
750 if (x[3] & 0xe0)
751 fail("Bits F37-F35 are not 0.\n");
752 if (x[5] & 0x04)
753 fail("Bit F52 is not 0.\n");
754 if (x[4] <= 0x31 && !memchk(x + 6, 5))
755 fail("Bits F107-F60 are not 0.\n");
756 else if (x[4] == 0xfe && (x[10] || x[9] || (x[8] & 0xf8)))
757 fail("Bits F107-F90 and/or F87-F83 are not 0.\n");
758 else if (x[4] == 0xff && x[10])
759 fail("Bits F107-F100 are not 0.\n");
760
761 static const char *CT_map[] = {
762 "Refer to Stream Header",
763 "L-PCM",
764 "AC-3",
765 "MPEG-1",
766 "MP3",
767 "MPEG2",
768 "AAC LC",
769 "DTS",
770 "ATRAC",
771 "DSD",
772 "Enhanced AC-3",
773 "DTS-(U)HD",
774 "MAT",
775 "DST",
776 "WMA Pro",
777 "Refer to Audio Coding Extension Type (CXT) Field"
778 };
779 printf(" CT: Audio Coding Type: %s\n", CT_map[x[1] >> 4]);
780 if (x[1] & 7)
781 printf(" CC: Audio Channel Count: %d\n", (x[1] & 7) + 1);
782 else
783 printf(" CC: Audio Channel Count: Refer to Stream Header\n");
784
785 static const char *SF_map[] = {
786 "Refer to Stream Header",
787 "32 kHz",
788 "44.1 kHz (CD)",
789 "48 kHz",
790 "88.2 kHz",
791 "96 kHz",
792 "176.4 kHz",
793 "192 kHz"
794 };
795 printf(" SF: Sampling Frequency: %s\n", SF_map[(x[2] >> 2) & 7]);
796
797 static const char *SS_map[] = {
798 "Refer to Stream Header",
799 "16 bit",
800 "20 bit",
801 "24 bit"
802 };
803 printf(" SS: Bits/Sample: %s\n", SS_map[x[2] & 3]);
804
805 static const char *CXT_map[] = {
806 "Refer to Audio Coding Type (CT) Field",
807 "Not in Use (1)",
808 "Not in Use (2)",
809 "Not in Use (3)",
810 "MPEG-4 HE AAC",
811 "MPEG-4 HE AAC v2",
812 "MPEG-4 AAC LC",
813 "DRA",
814 "MPEG-4 HE AAC + MPEG Surround",
815 "Reserved (9)",
816 "MPEG-4 AAC LC + MPEG Surround",
817 "MPEG-H 3D Audio",
818 "AC-4",
819 "L-PCM 3D Audio",
820 "Auro-Cx",
821 "MPEG-D USAC"
822 };
823 if ((x[3] & 0x1f) < ARRAY_SIZE(CXT_map))
824 printf(" CXT: Audio Coding Extension Type: %s\n", CXT_map[x[3] & 0x1f]);
825 else
826 printf(" CXT: Audio Coding Extension Type: Reserved (%d)\n", x[3] & 0x1f);
827 if ((x[3] & 0x1f) == 9 || (x[3] & 0x1f) >= ARRAY_SIZE(CXT_map))
828 fail("CXT: Reserved value.\n");
829
830 static const char *CA_map[] = {
831 "FR/FL",
832 "LFE1, FR/FL",
833 "FC, FR/FL",
834 "FC, LFE1, FR/FL",
835 "BC, FR/FL",
836 "BC, LFE1, FR/FL",
837 "BC, FC, FR/FL",
838 "BC, FC, LFE1, FR/FL",
839 "RS/LS, FR/FL",
840 "RS/LS, LFE1, FR/FL",
841 "RS/LS, FC, FR/FL",
842 "RS/LS, FC, LFE1, FR/FL",
843 "BC, RS/LS, FR/FL",
844 "BC, RS/LS, LFE1, FR/FL",
845 "BC, RS/LS, FC, FR/FL",
846 "BC, RS/LS, FC, LFE1, FR/FL",
847 "BR/BL, RS/LS, FR/FL",
848 "BR/BL, RS/LS, LFE1, FR/FL",
849 "BR/BL, RS/LS, FC, FR/FL",
850 "BR/BL, RS/LS, FC, LFE1, FR/FL",
851 "FRc/FLc, FR/FL",
852 "FRc/FLc, LFE1, FR/FL",
853 "FRc/FLc, FC, FR/FL",
854 "FRc/FLc, FC, LFE1, FR/FL",
855 "FRc/FLc, BC, FR/FL",
856 "FRc/FLc, BC, LFE1, FR/FL",
857 "FRc/FLc, BC, FC, FR/FL",
858 "FRc/FLc, BC, FC, LFE1, FR/FL",
859 "FRc/FLc, RS/LS, FR/FL",
860 "FRc/FLc, RS/LS, LFE1, FR/FL",
861 "FRc/FLc, RS/LS, FC, FR/FL",
862 "FRc/FLc, RS/LS, FC, LFE1, FR/FL",
863 "TpFC, RS/LS, FC, FR/FL",
864 "TpFC, RS/LS, FC, LFE1, FR/FL",
865 "TpC, RS/LS, FC, FR/FL",
866 "TpC, RS/LS, FC, LFE1, FR/FL",
867 "TpFR/TpFL, RS/LS, FR/FL",
868 "TpFR/TpFL, RS/LS, LFE1, FR/FL",
869 "FRw/FLw, RS/LS, FR/FL",
870 "FRw/FLw, RS/LS, LFE1, FR/FL",
871 "TpC, BC, RS/LS, FC, FR/FL",
872 "TpC, BC, RS/LS, FC, LFE1, FR/FL",
873 "TpFC, BC, RS/LS, FC, FR/FL",
874 "TpFC, BC, RS/LS, FC, FR/FL",
875 "TpC, TpFC, RS/LS, FC, FR/FL",
876 "TpC, TpFC, RS/LS, FC, FR/FL",
877 "TpFR/TpFL, RS/LS, FC, FR/FL",
878 "TpFR/TpFL, RS/LS, FC, FR/FL",
879 "FRw/FLw, RS/LS, FC, FR/FL",
880 "FRw/FLw, RS/LS, FC, FR/FL"
881 };
882 if (x[4] < ARRAY_SIZE(CA_map))
883 printf(" CA: Channel Allocation: %s\n", CA_map[x[4]]);
884 else if (x[4] < 0xfe) {
885 printf(" CA: Channel Allocation: Reserved (%d, 0x%02x)\n", x[4], x[4]);
886 fail("CA: Reserved value.\n");
887 }
888 else if (x[4] == 0xfe)
889 printf(" CA: Channel Allocation: According to the Speaker Mask\n");
890 else
891 printf(" CA: Channel Allocation: According to Channel Index\n");
892 printf(" LSV: Level Shift Value: %d dB\n", (x[5] >> 3) & 0xf);
893 printf(" DM_INH: Allow the Down Mixed Stereo Output: %s\n",
894 (x[5] & 0x80) ? "Prohibited" : "Yes");
895
896 static const char *LFEPBL_map[] = {
897 "Unknown or refer to other information",
898 "0 dB",
899 "+10 dB",
900 "Reserved"
901 };
902 printf(" LFEPBL: LFE Playback Level compared to other channels: %s\n", LFEPBL_map[x[5] & 3]);
903 if ((x[5] & 3) == 3)
904 fail("LFEPBL: Reserved value.\n");
905
906 if (x[4] < 0xfe)
907 return;
908
909 if (x[4] == 0xfe) {
910 if (x[6] & 0x40)
911 warn("F66 is not 0, the use of this bit is deprecated.\n");
912 if (x[8] & 0x08)
913 warn("F83 is not 0, the use of this bit is deprecated.\n");
914 printf(" SPM: Speaker Mask:\n");
915
916 unsigned spm = (x[8] << 16) | (x[7] << 8) | x[6];
917
918 for (unsigned i = 0; cta_speaker_map[i]; i++) {
919 if ((spm >> i) & 1)
920 printf(" %s\n", cta_speaker_map[i]);
921 }
922 return;
923 }
924
925 // CA == 0xff
926 printf(" CID: Channel Index: ");
927
928 unsigned cid = (x[9] << 24) | (x[8] << 16) | (x[7] << 8) | x[6];
929 bool first = true;
930
931 for (unsigned i = 0; i < 32; i++) {
932 if ((cid >> i) & 1) {
933 if (!first)
934 printf(" ,");
935 first = false;
936 printf("%u", i);
937 }
938 }
939 printf("\n");
940 }
941
942 void edid_state::parse_if_mpeg_source(const unsigned char *x, unsigned size)
943 {
944 data_block = "MPEG Source InfoFrame";
945
946 warn("The use of the %s is not recommended.\n", data_block.c_str());
947
948 if (parse_if_hdr(x, size))
949 return;
950
951 if (x[1] != 1) {
952 fail("Invalid version %d\n", x[1]);
953 return;
954 }
955 if (x[2] < 10) {
956 fail("Expected InfoFrame length of 10, got %d.\n", x[2]);
957 return;
958 }
959
960 // After this x[1] will refer to Data Byte 1
961 x += 2;
962
963 unsigned mb = (x[4] << 24) | (x[3] << 16) | (x[2] << 8) | x[1];
964
965 if (mb)
966 printf(" MB: MPEG Bit Rate: %u Hz\n", mb);
967 else
968 printf(" MB: MPEG Bit Rate: Unknown/Does Not Apply\n");
969
970 static const char *MF_map[] = {
971 "Unknown (No Data)",
972 "I Picture",
973 "B Picture",
974 "P Picture"
975 };
976 printf(" MF: MPEG Frame: %s\n", MF_map[x[5] & 3]);
977 printf(" FR: Field Repeat: %s\n", (x[5] & 0x10) ? "Repeated Field" : "New Field (Picture)");
978 if (x[5] & 0xec)
979 fail("Bits F57-F55 and/or F53-F52 are not 0.\n");
980 if (x[6] || x[7] || x[8] || x[9] || x[10])
981 fail("Bits F100-F60 are not 0.\n");
982 }
983
984 void edid_state::parse_if_ntsc_vbi(const unsigned char *x, unsigned size)
985 {
986 data_block = "NTSC VBI InfoFrame";
987
988 if (parse_if_hdr(x, size))
989 return;
990
991 int len = x[2];
992
993 if (x[1] != 1) {
994 fail("Invalid version %d\n", x[1]);
995 return;
996 }
997
998 // After this x[1] will refer to Data Byte 1
999 x += 2;
1000
1001 // See SCTE 127, Table 2 for more details
1002 hex_block(" PES_data_field: ", x + 1, len, false, len);
1003 }
1004
1005 void edid_state::parse_if_drm(const unsigned char *x, unsigned size)
1006 {
1007 unsigned length = x[2];
1008
1009 data_block = "Dynamic Range and Mastering InfoFrame";
1010
1011 if (parse_if_hdr(x, size))
1012 return;
1013
1014 if (x[1] != 1) {
1015 fail("Invalid version %d\n", x[1]);
1016 return;
1017 }
1018
1019 // After this x[1] will refer to Data Byte 1
1020 x += 2;
1021
1022 if (x[1] & 0xf8)
1023 fail("Bits F17-F13 are not 0.\n");
1024
1025 static const char *TF_map[] = {
1026 "Traditional Gamma - SDR Luminance Range",
1027 "Traditional Gamma - HDR Luminance Range",
1028 "Perceptual Quantization (PQ) based on SMPTE ST 2084",
1029 "Hybrid Log-Gamma (HLG) based on Rec. ITU-R BT.2100",
1030 "Reserved (4)",
1031 "Reserved (5)",
1032 "Reserved (6)",
1033 "Reserved (7)"
1034 };
1035 printf("Transfer Function: %s\n",
1036 TF_map[x[1] & 7]);
1037 if (length < 2)
1038 return;
1039
1040 if (x[2] & 0xf8)
1041 fail("Bits F27-F23 are not 0.\n");
1042 if (x[2] & 7) {
1043 printf("Static Metadata Descriptor ID: Reserved (%d)\n", x[2] & 7);
1044 if (!memchk(x + 3, length - 2))
1045 fail("Trailing non-zero bytes.\n");
1046 return;
1047 }
1048 printf("Static Metadata Descriptor ID: Type 1\n");
1049 if (length < 26) {
1050 fail("Expected a length of 26, got %d.\n", length);
1051 return;
1052 }
1053 if (!memchk(x + 3, 12)) {
1054 printf(" Display Primary 0: (%.5f, %.5f)\n", chrom2d(x + 3), chrom2d(x + 5));
1055 printf(" Display Primary 1: (%.5f, %.5f)\n", chrom2d(x + 7), chrom2d(x + 7));
1056 printf(" Display Primary 2: (%.5f, %.5f)\n", chrom2d(x + 11), chrom2d(x + 13));
1057 }
1058 if (!memchk(x + 15, 4)) {
1059 printf(" White Point: (%.5f, %.5f)\n", chrom2d(x + 15), chrom2d(x + 17));
1060 }
1061 if (!memchk(x + 19, 2)) {
1062 printf(" Max Display Mastering Luminance: %u cd/m^2\n",
1063 x[19] + (x[20] << 8));
1064 }
1065 if (!memchk(x + 21, 2)) {
1066 printf(" Min Display Mastering Luminance: %f cd/m^2\n",
1067 (x[21] + (x[22] << 8)) * 0.0001);
1068 }
1069 if (!memchk(x + 23, 2)) {
1070 printf(" Maximum Content Light Level (MaxCLL): %u cd/m^2\n",
1071 x[23] + (x[24] << 8));
1072 }
1073 if (!memchk(x + 25, 2)) {
1074 printf(" Maximum Frame-Average Light Level (MaxFALL): %u cd/m^2\n",
1075 x[25] + (x[26] << 8));
1076 }
1077 }