fc8080_demux.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /*****************************************************************************
  2. Copyright(c) 2013 FCI Inc. All Rights Reserved
  3. File name : fc8080_demux.c
  4. Description : fc8080 TSIF demux source file
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. History :
  17. ----------------------------------------------------------------------
  18. *******************************************************************************/
  19. #include <linux/kernel.h>
  20. #include <linux/string.h>
  21. #include "fci_types.h"
  22. #include "fc8080_regs.h"
  23. /* sync byte */
  24. #define SYNC_MASK_FIC 0x80
  25. #define SYNC_MASK_NVIDEO 0xc0
  26. #define SYNC_MASK_VIDEO 0x47
  27. #define SYNC_MASK_VIDEO1 0xb8
  28. /* packet indicator */
  29. #define PKT_IND_NONE 0x00
  30. #define PKT_IND_END 0x20
  31. #define PKT_IND_CONTINUE 0x40
  32. #define PKT_IND_START 0x80
  33. #define PKT_IND_MASK 0xe0
  34. /* data size */
  35. #define FIC_DATA_SIZE (FIC_BUF_LENGTH / 2)
  36. #define VIDEO_DATA_SIZE (CH0_BUF_LENGTH / 2) /* the maximum size */
  37. #define NON_VIDEO_DATA_SIZE (CH0_BUF_LENGTH / 2) /* the maximum size */
  38. #define TS_FRAME_SIZE 188
  39. /* TS service information */
  40. struct TS_FRAME_INFO {
  41. u8 ind;
  42. u16 length; /* current receiving length */
  43. u8 subch_id; /* subch id */
  44. u8 *buffer;
  45. };
  46. /* TS frame header information */
  47. struct TS_FRAME_HDR {
  48. u8 sync;
  49. u8 ind;
  50. u16 length;
  51. u8 *data;
  52. };
  53. static u32 fic_user_data;
  54. static u32 msc_user_data;
  55. static int (*fic_callback)(u32 userdata, u8 *data, int length);
  56. static int (*msc_callback)(u32 userdata, u8 subch_id, u8 *data, int length);
  57. static u8 video_data[2][VIDEO_DATA_SIZE];
  58. static u8 fic_data[FIC_DATA_SIZE];
  59. static u8 non_video_data[4][NON_VIDEO_DATA_SIZE];
  60. struct TS_FRAME_INFO video_frame_info[2] = {
  61. /* Video 0 */
  62. {
  63. 0, 0, 0xff, (u8 *) &video_data[0]
  64. },
  65. /* Video 1 */
  66. {
  67. 0, 0, 0xff, (u8 *) &video_data[1]
  68. }
  69. };
  70. struct TS_FRAME_INFO fic_frame_info = {
  71. 0, 0, 0xff, (u8 *) &fic_data[0]
  72. };
  73. struct TS_FRAME_INFO non_video_frame_info[4] = {
  74. {0, 0, 0xff, (u8 *) non_video_data[0]},
  75. {0, 0, 0xff, (u8 *) non_video_data[1]},
  76. {0, 0, 0xff, (u8 *) non_video_data[2]},
  77. {0, 0, 0xff, (u8 *) non_video_data[3]}
  78. };
  79. /* mapping buf id to subch id */
  80. static u8 video_subch[2] = {
  81. 0xff, 0xff
  82. };
  83. /* mapping subch id to buf id */
  84. static u8 non_video_subch[64] = {
  85. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  86. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  87. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  88. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  89. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  90. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  91. 0xff, 0xff, 0xff, 0xff
  92. };
  93. static int gether_fic(u8 *data)
  94. {
  95. struct TS_FRAME_HDR header;
  96. u16 len;
  97. /*header.sync = data[0];*/
  98. header.ind = data[1];
  99. header.length = (data[2] << 8) | data[3];
  100. header.data = &data[4];
  101. /* current real length */
  102. len = (header.length > 184) ? 184 : header.length;
  103. switch (header.ind) {
  104. case PKT_IND_START:
  105. fic_frame_info.ind = header.ind;
  106. fic_frame_info.length = 0;
  107. memcpy((void *) &fic_frame_info.buffer[0], header.data, len);
  108. fic_frame_info.length = len;
  109. return BBM_OK;
  110. case PKT_IND_CONTINUE:
  111. if (fic_frame_info.ind != PKT_IND_START) {
  112. fic_frame_info.ind = PKT_IND_NONE;
  113. return BBM_E_MUX_INDICATOR;
  114. }
  115. fic_frame_info.ind = header.ind;
  116. memcpy((void *) &fic_frame_info.buffer[fic_frame_info.length],
  117. header.data, len);
  118. fic_frame_info.length += len;
  119. return BBM_OK;
  120. case PKT_IND_END:
  121. if (fic_frame_info.ind != PKT_IND_CONTINUE) {
  122. fic_frame_info.ind = PKT_IND_NONE;
  123. return BBM_E_MUX_INDICATOR;
  124. }
  125. fic_frame_info.ind = header.ind;
  126. memcpy((void *) &fic_frame_info.buffer[fic_frame_info.length],
  127. header.data, len);
  128. fic_frame_info.length += len;
  129. break;
  130. default:
  131. return BBM_E_MUX_INDICATOR;
  132. }
  133. if ((fic_frame_info.length >= FIC_DATA_SIZE) && (fic_frame_info.ind ==
  134. PKT_IND_END)) {
  135. if (fic_callback)
  136. (*fic_callback)(fic_user_data, fic_frame_info.buffer,
  137. fic_frame_info.length);
  138. fic_frame_info.length = 0;
  139. fic_frame_info.ind = PKT_IND_NONE;
  140. }
  141. return BBM_OK;
  142. }
  143. static int gether_non_video(u8 *data)
  144. {
  145. struct TS_FRAME_HDR header;
  146. u16 len;
  147. u8 subch;
  148. u8 buf_id;
  149. u16 data_sz;
  150. header.sync = data[0];
  151. header.ind = data[1];
  152. header.length = (data[2] << 8) | data[3];
  153. header.data = &data[4];
  154. len = (header.length >= 184) ? 184 : header.length;
  155. subch = header.sync & 0x3f;
  156. buf_id = non_video_subch[subch];
  157. if (buf_id == 0xff)
  158. return BBM_E_MUX_SUBCHANNEL;
  159. if (buf_id > 3)
  160. return BBM_E_MUX_SUBCHANNEL;
  161. switch (header.ind) {
  162. case PKT_IND_START:
  163. non_video_frame_info[buf_id].length = 0;
  164. memcpy((void *) &non_video_frame_info[buf_id].buffer[0],
  165. header.data, len);
  166. non_video_frame_info[buf_id].length += len;
  167. if (non_video_frame_info[buf_id].length < header.length)
  168. non_video_frame_info[buf_id].ind = header.ind;
  169. else
  170. non_video_frame_info[buf_id].ind = PKT_IND_END;
  171. break;
  172. case PKT_IND_CONTINUE:
  173. data_sz = non_video_frame_info[buf_id].length;
  174. if (data_sz + len > NON_VIDEO_DATA_SIZE)
  175. return BBM_E_MUX_DATA_SIZE;
  176. memcpy((void *) &non_video_frame_info[buf_id].buffer[data_sz],
  177. header.data, len);
  178. non_video_frame_info[buf_id].length += len;
  179. non_video_frame_info[buf_id].ind = header.ind;
  180. return BBM_OK;
  181. case PKT_IND_END:
  182. data_sz = non_video_frame_info[buf_id].length;
  183. if (data_sz + len > NON_VIDEO_DATA_SIZE)
  184. return BBM_E_MUX_DATA_SIZE;
  185. memcpy((void *) &non_video_frame_info[buf_id].buffer[data_sz],
  186. header.data, len);
  187. non_video_frame_info[buf_id].length += len;
  188. non_video_frame_info[buf_id].ind = header.ind;
  189. break;
  190. default:
  191. return BBM_E_MUX_INDICATOR;
  192. }
  193. if ((header.ind == PKT_IND_END) || (header.ind == PKT_IND_START &&
  194. 184 >= header.length)) {
  195. if (msc_callback)
  196. (*msc_callback)(msc_user_data, subch,
  197. &non_video_frame_info[buf_id].buffer[0],
  198. non_video_frame_info[buf_id].length);
  199. non_video_frame_info[buf_id].length = 0;
  200. non_video_frame_info[buf_id].ind = PKT_IND_NONE;
  201. }
  202. return BBM_OK;
  203. }
  204. static int gether_video(u8 *data)
  205. {
  206. u8 subch;
  207. u8 buf_id;
  208. u16 data_sz;
  209. switch (data[0]) {
  210. case SYNC_MASK_VIDEO:
  211. buf_id = 0;
  212. subch = video_subch[0];
  213. break;
  214. case SYNC_MASK_VIDEO1:
  215. buf_id = 1;
  216. data[0] = 0x47;
  217. subch = video_subch[1];
  218. break;
  219. default:
  220. return BBM_E_MUX_DATA_MASK;
  221. }
  222. if (subch == 0xff)
  223. return BBM_E_MUX_SUBCHANNEL;
  224. if (subch > 64)
  225. return BBM_E_MUX_SUBCHANNEL;
  226. data_sz = video_frame_info[buf_id].length;
  227. if (data_sz + TS_FRAME_SIZE > VIDEO_DATA_SIZE)
  228. return BBM_E_MUX_DATA_SIZE;
  229. memcpy((void *) (video_frame_info[buf_id].buffer + data_sz), data, 188);
  230. video_frame_info[buf_id].length += TS_FRAME_SIZE;
  231. if (video_frame_info[buf_id].length >= VIDEO_DATA_SIZE) {
  232. if (msc_callback)
  233. (*msc_callback)(msc_user_data, subch,
  234. video_frame_info[buf_id].buffer,
  235. video_frame_info[buf_id].length);
  236. video_frame_info[buf_id].length = 0;
  237. }
  238. return BBM_OK;
  239. }
  240. int fc8080_demux(u8 *data, u32 length)
  241. {
  242. u32 i;
  243. for (i = 0; i < length; i += TS_FRAME_SIZE) {
  244. if (data[i] == SYNC_MASK_FIC)
  245. gether_fic(&data[i]);
  246. else if ((data[i] == SYNC_MASK_VIDEO) || (data[i] ==
  247. SYNC_MASK_VIDEO1))
  248. gether_video(&data[i]);
  249. else if (((data[i] & 0xc0) == 0xc0) || ((data[i] & 0xc0) ==
  250. 0x00))
  251. gether_non_video(&data[i]);
  252. }
  253. return BBM_OK;
  254. }
  255. int fc8080_demux_fic_callback_register(u32 userdata,
  256. int (*callback)(u32 userdata, u8 *data, int length))
  257. {
  258. fic_user_data = userdata;
  259. fic_callback = callback;
  260. return BBM_OK;
  261. }
  262. int fc8080_demux_msc_callback_register(u32 userdata,
  263. int (*callback)(u32 userdata, u8 subch_id, u8 *data, int length))
  264. {
  265. msc_user_data = userdata;
  266. msc_callback = callback;
  267. return BBM_OK;
  268. }
  269. int fc8080_demux_select_video(u8 subch_id, u8 buf_id)
  270. {
  271. video_subch[buf_id] = subch_id & 0x3f;
  272. return BBM_OK;
  273. }
  274. int fc8080_demux_select_channel(u8 subch_id, u8 buf_id)
  275. {
  276. non_video_subch[subch_id & 0x3f] = buf_id;
  277. return BBM_OK;
  278. }
  279. int fc8080_demux_deselect_video(u8 subch_id, u8 buf_id)
  280. {
  281. video_subch[buf_id] = 0xff;
  282. return BBM_OK;
  283. }
  284. int fc8080_demux_deselect_channel(u8 subch_id, u8 buf_id)
  285. {
  286. non_video_subch[subch_id & 0x3f] = 0xff;
  287. return BBM_OK;
  288. }