5 * Copyright (C) 2005 Mike Isely <isely@pobox.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "pvrusb2-std.h"
23 #include "pvrusb2-debug.h"
61 #define TSTD_B (V4L2_STD_PAL_B|V4L2_STD_SECAM_B)
62 #define TSTD_B1 (V4L2_STD_PAL_B1)
63 #define TSTD_D (V4L2_STD_PAL_D|V4L2_STD_SECAM_D)
64 #define TSTD_D1 (V4L2_STD_PAL_D1)
65 #define TSTD_G (V4L2_STD_PAL_G|V4L2_STD_SECAM_G)
66 #define TSTD_H (V4L2_STD_PAL_H|V4L2_STD_SECAM_H)
67 #define TSTD_I (V4L2_STD_PAL_I)
68 #define TSTD_K (V4L2_STD_PAL_K|V4L2_STD_SECAM_K)
69 #define TSTD_K1 (V4L2_STD_SECAM_K1)
70 #define TSTD_L (V4L2_STD_SECAM_L)
71 #define TSTD_M (V4L2_STD_PAL_M|V4L2_STD_NTSC_M)
72 #define TSTD_N (V4L2_STD_PAL_N)
73 #define TSTD_Nc (V4L2_STD_PAL_Nc)
74 #define TSTD_60 (V4L2_STD_PAL_60)
76 #define CSTD_ALL (CSTD_PAL|CSTD_NTSC|CSTD_SECAM)
78 /* Mapping of standard bits to color system */
79 const static struct std_name std_groups[] = {
85 /* Mapping of standard bits to modulation system */
86 const static struct std_name std_items[] = {
97 {"LC",V4L2_STD_SECAM_LC},
99 {"Mj",V4L2_STD_NTSC_M_JP},
100 {"443",V4L2_STD_NTSC_443},
101 {"Mk",V4L2_STD_NTSC_M_KR},
108 // Search an array of std_name structures and return a pointer to the
109 // element with the matching name.
110 static const struct std_name *find_std_name(const struct std_name *arrPtr,
111 unsigned int arrSize,
113 unsigned int bufSize)
116 const struct std_name *p;
117 for (idx = 0; idx < arrSize; idx++) {
119 if (strlen(p->name) != bufSize) continue;
120 if (!memcmp(bufPtr,p->name,bufSize)) return p;
126 int pvr2_std_str_to_id(v4l2_std_id *idPtr,const char *bufPtr,
127 unsigned int bufSize)
130 v4l2_std_id cmsk = 0;
135 const struct std_name *sp;
140 while ((cnt < bufSize) && (bufPtr[cnt] != '-')) cnt++;
141 if (cnt >= bufSize) return 0; // No more characters
144 sizeof(std_groups)/sizeof(std_groups[0]),
146 if (!sp) return 0; // Illegal color system name
155 while (cnt < bufSize) {
161 if (ch == '/') break;
164 sp = find_std_name(std_items,
165 sizeof(std_items)/sizeof(std_items[0]),
167 if (!sp) return 0; // Illegal modulation system ID
169 if (!t) return 0; // Specific color + modulation system illegal
171 if (cnt < bufSize) cnt++;
176 if (idPtr) *idPtr = id;
181 unsigned int pvr2_std_id_to_str(char *bufPtr, unsigned int bufSize,
184 unsigned int idx1,idx2;
185 const struct std_name *ip,*gp;
191 idx1 < sizeof(std_groups)/sizeof(std_groups[0]);
193 gp = std_groups + idx1;
196 idx2 < sizeof(std_items)/sizeof(std_items[0]);
198 ip = std_items + idx2;
199 if (!(gp->id & ip->id & id)) continue;
202 c2 = scnprintf(bufPtr,bufSize,";");
208 c2 = scnprintf(bufPtr,bufSize,
212 c2 = scnprintf(bufPtr,bufSize,"/");
217 c2 = scnprintf(bufPtr,bufSize,
228 // Template data for possible enumerated video standards. Here we group
229 // standards which share common frame rates and resolution.
230 static struct v4l2_standard generic_standards[] = {
232 .id = (TSTD_B|TSTD_B1|
247 .reserved = {0,0,0,0}
258 .reserved = {0,0,0,0}
259 }, { // This is a total wild guess
267 .reserved = {0,0,0,0}
268 }, { // This is total wild guess
269 .id = V4L2_STD_NTSC_443,
276 .reserved = {0,0,0,0}
280 #define generic_standards_cnt (sizeof(generic_standards)/sizeof(generic_standards[0]))
282 static struct v4l2_standard *match_std(v4l2_std_id id)
285 for (idx = 0; idx < generic_standards_cnt; idx++) {
286 if (generic_standards[idx].id & id) {
287 return generic_standards + idx;
293 static int pvr2_std_fill(struct v4l2_standard *std,v4l2_std_id id)
295 struct v4l2_standard *template;
298 template = match_std(id);
299 if (!template) return 0;
301 memcpy(std,template,sizeof(*template));
304 bcnt = pvr2_std_id_to_str(std->name,sizeof(std->name)-1,id);
306 pvr2_trace(PVR2_TRACE_INIT,"Set up standard idx=%u name=%s",
307 std->index,std->name);
311 /* These are special cases of combined standards that we should enumerate
312 separately if the component pieces are present. */
313 static v4l2_std_id std_mixes[] = {
314 V4L2_STD_PAL_B | V4L2_STD_PAL_G,
315 V4L2_STD_PAL_D | V4L2_STD_PAL_K,
316 V4L2_STD_SECAM_B | V4L2_STD_SECAM_G,
317 V4L2_STD_SECAM_D | V4L2_STD_SECAM_K,
320 struct v4l2_standard *pvr2_std_create_enum(unsigned int *countptr,
323 unsigned int std_cnt = 0;
324 unsigned int idx,bcnt,idx2;
325 v4l2_std_id idmsk,cmsk,fmsk;
326 struct v4l2_standard *stddefs;
328 if (pvrusb2_debug & PVR2_TRACE_INIT) {
330 bcnt = pvr2_std_id_to_str(buf,sizeof(buf),id);
332 PVR2_TRACE_INIT,"Mapping standards mask=0x%x (%.*s)",
339 for (idmsk = 1, cmsk = id; cmsk; idmsk <<= 1) {
340 if (!(idmsk & cmsk)) continue;
342 if (match_std(idmsk)) {
349 for (idx2 = 0; idx2 < sizeof(std_mixes)/sizeof(std_mixes[0]); idx2++) {
350 if ((id & std_mixes[idx2]) == std_mixes[idx2]) std_cnt++;
355 bcnt = pvr2_std_id_to_str(buf,sizeof(buf),fmsk);
357 PVR2_TRACE_ERROR_LEGS,
359 " Failed to classify the following standard(s): %.*s",
363 pvr2_trace(PVR2_TRACE_INIT,"Setting up %u unique standard(s)",
365 if (!std_cnt) return 0; // paranoia
367 stddefs = kmalloc(sizeof(struct v4l2_standard) * std_cnt,
369 memset(stddefs,0,sizeof(struct v4l2_standard) * std_cnt);
370 for (idx = 0; idx < std_cnt; idx++) stddefs[idx].index = idx;
374 /* Enumerate potential special cases */
375 for (idx2 = 0; ((idx2 < sizeof(std_mixes)/sizeof(std_mixes[0])) &&
376 (idx < std_cnt)); idx2++) {
377 if (!(id & std_mixes[idx2])) continue;
378 if (pvr2_std_fill(stddefs+idx,std_mixes[idx2])) idx++;
380 /* Now enumerate individual pieces */
381 for (idmsk = 1, cmsk = id; cmsk && (idx < std_cnt); idmsk <<= 1) {
382 if (!(idmsk & cmsk)) continue;
384 if (!pvr2_std_fill(stddefs+idx,idmsk)) continue;
392 v4l2_std_id pvr2_std_get_usable(void)
399 Stuff for Emacs to see, in order to encourage consistent editing style:
400 *** Local Variables: ***
402 *** fill-column: 75 ***
404 *** c-basic-offset: 8 ***