pvrusb2-std.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. /*
  2. *
  3. * $Id$
  4. *
  5. * Copyright (C) 2005 Mike Isely <isely@pobox.com>
  6. *
  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
  10. *
  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.
  15. *
  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
  19. *
  20. */
  21. #include "pvrusb2-std.h"
  22. #include "pvrusb2-debug.h"
  23. struct std_name {
  24. const char *name;
  25. v4l2_std_id id;
  26. };
  27. #define CSTD_PAL \
  28. (V4L2_STD_PAL_B| \
  29. V4L2_STD_PAL_B1| \
  30. V4L2_STD_PAL_G| \
  31. V4L2_STD_PAL_H| \
  32. V4L2_STD_PAL_I| \
  33. V4L2_STD_PAL_D| \
  34. V4L2_STD_PAL_D1| \
  35. V4L2_STD_PAL_K| \
  36. V4L2_STD_PAL_M| \
  37. V4L2_STD_PAL_N| \
  38. V4L2_STD_PAL_Nc| \
  39. V4L2_STD_PAL_60)
  40. #define CSTD_NTSC \
  41. (V4L2_STD_NTSC_M| \
  42. V4L2_STD_NTSC_M_JP| \
  43. V4L2_STD_NTSC_M_KR| \
  44. V4L2_STD_NTSC_443)
  45. #define CSTD_SECAM \
  46. (V4L2_STD_SECAM_B| \
  47. V4L2_STD_SECAM_D| \
  48. V4L2_STD_SECAM_G| \
  49. V4L2_STD_SECAM_H| \
  50. V4L2_STD_SECAM_K| \
  51. V4L2_STD_SECAM_K1| \
  52. V4L2_STD_SECAM_L| \
  53. V4L2_STD_SECAM_LC)
  54. #define TSTD_B (V4L2_STD_PAL_B|V4L2_STD_SECAM_B)
  55. #define TSTD_B1 (V4L2_STD_PAL_B1)
  56. #define TSTD_D (V4L2_STD_PAL_D|V4L2_STD_SECAM_D)
  57. #define TSTD_D1 (V4L2_STD_PAL_D1)
  58. #define TSTD_G (V4L2_STD_PAL_G|V4L2_STD_SECAM_G)
  59. #define TSTD_H (V4L2_STD_PAL_H|V4L2_STD_SECAM_H)
  60. #define TSTD_I (V4L2_STD_PAL_I)
  61. #define TSTD_K (V4L2_STD_PAL_K|V4L2_STD_SECAM_K)
  62. #define TSTD_K1 (V4L2_STD_SECAM_K1)
  63. #define TSTD_L (V4L2_STD_SECAM_L)
  64. #define TSTD_M (V4L2_STD_PAL_M|V4L2_STD_NTSC_M)
  65. #define TSTD_N (V4L2_STD_PAL_N)
  66. #define TSTD_Nc (V4L2_STD_PAL_Nc)
  67. #define TSTD_60 (V4L2_STD_PAL_60)
  68. #define CSTD_ALL (CSTD_PAL|CSTD_NTSC|CSTD_SECAM)
  69. /* Mapping of standard bits to color system */
  70. const static struct std_name std_groups[] = {
  71. {"PAL",CSTD_PAL},
  72. {"NTSC",CSTD_NTSC},
  73. {"SECAM",CSTD_SECAM},
  74. };
  75. /* Mapping of standard bits to modulation system */
  76. const static struct std_name std_items[] = {
  77. {"B",TSTD_B},
  78. {"B1",TSTD_B1},
  79. {"D",TSTD_D},
  80. {"D1",TSTD_D1},
  81. {"G",TSTD_G},
  82. {"H",TSTD_H},
  83. {"I",TSTD_I},
  84. {"K",TSTD_K},
  85. {"K1",TSTD_K1},
  86. {"L",TSTD_L},
  87. {"LC",V4L2_STD_SECAM_LC},
  88. {"M",TSTD_M},
  89. {"Mj",V4L2_STD_NTSC_M_JP},
  90. {"443",V4L2_STD_NTSC_443},
  91. {"Mk",V4L2_STD_NTSC_M_KR},
  92. {"N",TSTD_N},
  93. {"Nc",TSTD_Nc},
  94. {"60",TSTD_60},
  95. };
  96. // Search an array of std_name structures and return a pointer to the
  97. // element with the matching name.
  98. static const struct std_name *find_std_name(const struct std_name *arrPtr,
  99. unsigned int arrSize,
  100. const char *bufPtr,
  101. unsigned int bufSize)
  102. {
  103. unsigned int idx;
  104. const struct std_name *p;
  105. for (idx = 0; idx < arrSize; idx++) {
  106. p = arrPtr + idx;
  107. if (strlen(p->name) != bufSize) continue;
  108. if (!memcmp(bufPtr,p->name,bufSize)) return p;
  109. }
  110. return 0;
  111. }
  112. int pvr2_std_str_to_id(v4l2_std_id *idPtr,const char *bufPtr,
  113. unsigned int bufSize)
  114. {
  115. v4l2_std_id id = 0;
  116. v4l2_std_id cmsk = 0;
  117. v4l2_std_id t;
  118. int mMode = 0;
  119. unsigned int cnt;
  120. char ch;
  121. const struct std_name *sp;
  122. while (bufSize) {
  123. if (!mMode) {
  124. cnt = 0;
  125. while ((cnt < bufSize) && (bufPtr[cnt] != '-')) cnt++;
  126. if (cnt >= bufSize) return 0; // No more characters
  127. sp = find_std_name(
  128. std_groups,
  129. sizeof(std_groups)/sizeof(std_groups[0]),
  130. bufPtr,cnt);
  131. if (!sp) return 0; // Illegal color system name
  132. cnt++;
  133. bufPtr += cnt;
  134. bufSize -= cnt;
  135. mMode = !0;
  136. cmsk = sp->id;
  137. continue;
  138. }
  139. cnt = 0;
  140. while (cnt < bufSize) {
  141. ch = bufPtr[cnt];
  142. if (ch == ';') {
  143. mMode = 0;
  144. break;
  145. }
  146. if (ch == '/') break;
  147. cnt++;
  148. }
  149. sp = find_std_name(std_items,
  150. sizeof(std_items)/sizeof(std_items[0]),
  151. bufPtr,cnt);
  152. if (!sp) return 0; // Illegal modulation system ID
  153. t = sp->id & cmsk;
  154. if (!t) return 0; // Specific color + modulation system illegal
  155. id |= t;
  156. if (cnt < bufSize) cnt++;
  157. bufPtr += cnt;
  158. bufSize -= cnt;
  159. }
  160. if (idPtr) *idPtr = id;
  161. return !0;
  162. }
  163. unsigned int pvr2_std_id_to_str(char *bufPtr, unsigned int bufSize,
  164. v4l2_std_id id)
  165. {
  166. unsigned int idx1,idx2;
  167. const struct std_name *ip,*gp;
  168. int gfl,cfl;
  169. unsigned int c1,c2;
  170. cfl = 0;
  171. c1 = 0;
  172. for (idx1 = 0;
  173. idx1 < sizeof(std_groups)/sizeof(std_groups[0]);
  174. idx1++) {
  175. gp = std_groups + idx1;
  176. gfl = 0;
  177. for (idx2 = 0;
  178. idx2 < sizeof(std_items)/sizeof(std_items[0]);
  179. idx2++) {
  180. ip = std_items + idx2;
  181. if (!(gp->id & ip->id & id)) continue;
  182. if (!gfl) {
  183. if (cfl) {
  184. c2 = scnprintf(bufPtr,bufSize,";");
  185. c1 += c2;
  186. bufSize -= c2;
  187. bufPtr += c2;
  188. }
  189. cfl = !0;
  190. c2 = scnprintf(bufPtr,bufSize,
  191. "%s-",gp->name);
  192. gfl = !0;
  193. } else {
  194. c2 = scnprintf(bufPtr,bufSize,"/");
  195. }
  196. c1 += c2;
  197. bufSize -= c2;
  198. bufPtr += c2;
  199. c2 = scnprintf(bufPtr,bufSize,
  200. ip->name);
  201. c1 += c2;
  202. bufSize -= c2;
  203. bufPtr += c2;
  204. }
  205. }
  206. return c1;
  207. }
  208. // Template data for possible enumerated video standards. Here we group
  209. // standards which share common frame rates and resolution.
  210. static struct v4l2_standard generic_standards[] = {
  211. {
  212. .id = (TSTD_B|TSTD_B1|
  213. TSTD_D|TSTD_D1|
  214. TSTD_G|
  215. TSTD_H|
  216. TSTD_I|
  217. TSTD_K|TSTD_K1|
  218. TSTD_L|
  219. V4L2_STD_SECAM_LC |
  220. TSTD_N|TSTD_Nc),
  221. .frameperiod =
  222. {
  223. .numerator = 1,
  224. .denominator= 25
  225. },
  226. .framelines = 625,
  227. .reserved = {0,0,0,0}
  228. }, {
  229. .id = (TSTD_M|
  230. V4L2_STD_NTSC_M_JP|
  231. V4L2_STD_NTSC_M_KR),
  232. .frameperiod =
  233. {
  234. .numerator = 1001,
  235. .denominator= 30000
  236. },
  237. .framelines = 525,
  238. .reserved = {0,0,0,0}
  239. }, { // This is a total wild guess
  240. .id = (TSTD_60),
  241. .frameperiod =
  242. {
  243. .numerator = 1001,
  244. .denominator= 30000
  245. },
  246. .framelines = 525,
  247. .reserved = {0,0,0,0}
  248. }, { // This is total wild guess
  249. .id = V4L2_STD_NTSC_443,
  250. .frameperiod =
  251. {
  252. .numerator = 1001,
  253. .denominator= 30000
  254. },
  255. .framelines = 525,
  256. .reserved = {0,0,0,0}
  257. }
  258. };
  259. #define generic_standards_cnt (sizeof(generic_standards)/sizeof(generic_standards[0]))
  260. static struct v4l2_standard *match_std(v4l2_std_id id)
  261. {
  262. unsigned int idx;
  263. for (idx = 0; idx < generic_standards_cnt; idx++) {
  264. if (generic_standards[idx].id & id) {
  265. return generic_standards + idx;
  266. }
  267. }
  268. return 0;
  269. }
  270. static int pvr2_std_fill(struct v4l2_standard *std,v4l2_std_id id)
  271. {
  272. struct v4l2_standard *template;
  273. int idx;
  274. unsigned int bcnt;
  275. template = match_std(id);
  276. if (!template) return 0;
  277. idx = std->index;
  278. memcpy(std,template,sizeof(*template));
  279. std->index = idx;
  280. std->id = id;
  281. bcnt = pvr2_std_id_to_str(std->name,sizeof(std->name)-1,id);
  282. std->name[bcnt] = 0;
  283. pvr2_trace(PVR2_TRACE_INIT,"Set up standard idx=%u name=%s",
  284. std->index,std->name);
  285. return !0;
  286. }
  287. /* These are special cases of combined standards that we should enumerate
  288. separately if the component pieces are present. */
  289. static v4l2_std_id std_mixes[] = {
  290. V4L2_STD_PAL_B | V4L2_STD_PAL_G,
  291. V4L2_STD_PAL_D | V4L2_STD_PAL_K,
  292. V4L2_STD_SECAM_B | V4L2_STD_SECAM_G,
  293. V4L2_STD_SECAM_D | V4L2_STD_SECAM_K,
  294. };
  295. struct v4l2_standard *pvr2_std_create_enum(unsigned int *countptr,
  296. v4l2_std_id id)
  297. {
  298. unsigned int std_cnt = 0;
  299. unsigned int idx,bcnt,idx2;
  300. v4l2_std_id idmsk,cmsk,fmsk;
  301. struct v4l2_standard *stddefs;
  302. if (pvrusb2_debug & PVR2_TRACE_INIT) {
  303. char buf[50];
  304. bcnt = pvr2_std_id_to_str(buf,sizeof(buf),id);
  305. pvr2_trace(
  306. PVR2_TRACE_INIT,"Mapping standards mask=0x%x (%.*s)",
  307. (int)id,bcnt,buf);
  308. }
  309. *countptr = 0;
  310. std_cnt = 0;
  311. fmsk = 0;
  312. for (idmsk = 1, cmsk = id; cmsk; idmsk <<= 1) {
  313. if (!(idmsk & cmsk)) continue;
  314. cmsk &= ~idmsk;
  315. if (match_std(idmsk)) {
  316. std_cnt++;
  317. continue;
  318. }
  319. fmsk |= idmsk;
  320. }
  321. for (idx2 = 0; idx2 < sizeof(std_mixes)/sizeof(std_mixes[0]); idx2++) {
  322. if ((id & std_mixes[idx2]) == std_mixes[idx2]) std_cnt++;
  323. }
  324. if (fmsk) {
  325. char buf[50];
  326. bcnt = pvr2_std_id_to_str(buf,sizeof(buf),fmsk);
  327. pvr2_trace(
  328. PVR2_TRACE_ERROR_LEGS,
  329. "WARNING:"
  330. " Failed to classify the following standard(s): %.*s",
  331. bcnt,buf);
  332. }
  333. pvr2_trace(PVR2_TRACE_INIT,"Setting up %u unique standard(s)",
  334. std_cnt);
  335. if (!std_cnt) return 0; // paranoia
  336. stddefs = kmalloc(sizeof(struct v4l2_standard) * std_cnt,
  337. GFP_KERNEL);
  338. memset(stddefs,0,sizeof(struct v4l2_standard) * std_cnt);
  339. for (idx = 0; idx < std_cnt; idx++) stddefs[idx].index = idx;
  340. idx = 0;
  341. /* Enumerate potential special cases */
  342. for (idx2 = 0; ((idx2 < sizeof(std_mixes)/sizeof(std_mixes[0])) &&
  343. (idx < std_cnt)); idx2++) {
  344. if (!(id & std_mixes[idx2])) continue;
  345. if (pvr2_std_fill(stddefs+idx,std_mixes[idx2])) idx++;
  346. }
  347. /* Now enumerate individual pieces */
  348. for (idmsk = 1, cmsk = id; cmsk && (idx < std_cnt); idmsk <<= 1) {
  349. if (!(idmsk & cmsk)) continue;
  350. cmsk &= ~idmsk;
  351. if (!pvr2_std_fill(stddefs+idx,idmsk)) continue;
  352. idx++;
  353. }
  354. *countptr = std_cnt;
  355. return stddefs;
  356. }
  357. v4l2_std_id pvr2_std_get_usable(void)
  358. {
  359. return CSTD_ALL;
  360. }
  361. /*
  362. Stuff for Emacs to see, in order to encourage consistent editing style:
  363. *** Local Variables: ***
  364. *** mode: c ***
  365. *** fill-column: 75 ***
  366. *** tab-width: 8 ***
  367. *** c-basic-offset: 8 ***
  368. *** End: ***
  369. */