1 /*
2  * Copyright © 2019 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  *
23  * Authors: Simon Ser <simon.ser@intel.com>
24  */
25 
26 #include "config.h"
27 
28 #include <string.h>
29 
30 #include "igt_core.h"
31 #include "igt_infoframe.h"
32 
33 /**
34  * SECTION:igt_infoframe
35  * @short_description: InfoFrame parsing library
36  * @title: InfoFrame
37  * @include: igt_infoframe.h
38  *
39  * This library provides helpers to parse InfoFrames as defined in CEA-861-D
40  * section 6.
41  */
42 
43 static const int sampling_freqs[] = {
44 	-1, /* refer to stream header */
45 	33000,
46 	44100,
47 	48000,
48 	88200,
49 	96000,
50 	176400,
51 	192000,
52 };
53 
54 static const size_t sampling_freqs_len = sizeof(sampling_freqs) / sizeof(sampling_freqs[0]);
55 
56 static const int sample_sizes[] = {
57 	-1, /* refer to stream header */
58 	16,
59 	20,
60 	24,
61 };
62 
63 static const size_t sample_sizes_len = sizeof(sample_sizes) / sizeof(sample_sizes[0]);
64 
infoframe_avi_parse(struct infoframe_avi * infoframe,int version,const uint8_t * buf,size_t buf_size)65 bool infoframe_avi_parse(struct infoframe_avi *infoframe, int version,
66 			 const uint8_t *buf, size_t buf_size)
67 {
68 	memset(infoframe, 0, sizeof(*infoframe));
69 
70 	switch (version) {
71 	case 2:
72 	case 3:
73 	case 4:
74 		break; /* supported */
75 	default:
76 		igt_debug("Unsuppported AVI InfoFrame version: %d\n", version);
77 		return false;
78 	}
79 
80 	if (buf_size < 13)
81 		return false;
82 
83 	infoframe->rgb_ycbcr = buf[0] >> 5;
84 	infoframe->scan = buf[0] & 0x3;
85 
86 	infoframe->colorimetry = buf[1] >> 6;
87 	infoframe->picture_aspect_ratio = (buf[1] >> 4) & 0x3;
88 	infoframe->active_aspect_ratio = buf[1] & 0xF;
89 	infoframe->vic = buf[3];
90 
91 	return true;
92 }
93 
infoframe_audio_parse(struct infoframe_audio * infoframe,int version,const uint8_t * buf,size_t buf_size)94 bool infoframe_audio_parse(struct infoframe_audio *infoframe, int version,
95 			   const uint8_t *buf, size_t buf_size)
96 {
97 	int channel_count;
98 	size_t sampling_freq_idx, sample_size_idx;
99 
100 	memset(infoframe, 0, sizeof(*infoframe));
101 
102 	if (version != 1 || buf_size < 5)
103 		return false;
104 
105 	infoframe->coding_type = buf[0] >> 4;
106 
107 	channel_count = buf[0] & 0x7;
108 	if (channel_count == 0)
109 		infoframe->channel_count = -1;
110 	else
111 		infoframe->channel_count = channel_count + 1;
112 
113 	sampling_freq_idx = (buf[1] >> 2) & 0x7;
114 	if (sampling_freq_idx >= sampling_freqs_len)
115 		return false;
116 	infoframe->sampling_freq = sampling_freqs[sampling_freq_idx];
117 
118 	sample_size_idx = buf[1] & 0x3;
119 	if (sample_size_idx >= sample_sizes_len)
120 		return false;
121 	infoframe->sample_size = sample_sizes[sample_size_idx];
122 
123 	return true;
124 }
125