av7110_ipack.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. #include "dvb_filter.h"
  2. #include "av7110_ipack.h"
  3. #include <linux/string.h> /* for memcpy() */
  4. #include <linux/vmalloc.h>
  5. void av7110_ipack_reset(struct ipack *p)
  6. {
  7. p->found = 0;
  8. p->cid = 0;
  9. p->plength = 0;
  10. p->flag1 = 0;
  11. p->flag2 = 0;
  12. p->hlength = 0;
  13. p->mpeg = 0;
  14. p->check = 0;
  15. p->which = 0;
  16. p->done = 0;
  17. p->count = 0;
  18. }
  19. int av7110_ipack_init(struct ipack *p, int size,
  20. void (*func)(u8 *buf, int size, void *priv))
  21. {
  22. if (!(p->buf = vmalloc(size*sizeof(u8)))) {
  23. printk(KERN_WARNING "Couldn't allocate memory for ipack\n");
  24. return -ENOMEM;
  25. }
  26. p->size = size;
  27. p->func = func;
  28. p->repack_subids = 0;
  29. av7110_ipack_reset(p);
  30. return 0;
  31. }
  32. void av7110_ipack_free(struct ipack *p)
  33. {
  34. vfree(p->buf);
  35. }
  36. static void send_ipack(struct ipack *p)
  37. {
  38. int off;
  39. struct dvb_audio_info ai;
  40. int ac3_off = 0;
  41. int streamid = 0;
  42. int nframes = 0;
  43. int f = 0;
  44. switch (p->mpeg) {
  45. case 2:
  46. if (p->count < 10)
  47. return;
  48. p->buf[3] = p->cid;
  49. p->buf[4] = (u8)(((p->count - 6) & 0xff00) >> 8);
  50. p->buf[5] = (u8)((p->count - 6) & 0x00ff);
  51. if (p->repack_subids && p->cid == PRIVATE_STREAM1) {
  52. off = 9 + p->buf[8];
  53. streamid = p->buf[off];
  54. if ((streamid & 0xf8) == 0x80) {
  55. ai.off = 0;
  56. ac3_off = ((p->buf[off + 2] << 8)|
  57. p->buf[off + 3]);
  58. if (ac3_off < p->count)
  59. f = dvb_filter_get_ac3info(p->buf + off + 3 + ac3_off,
  60. p->count - ac3_off, &ai, 0);
  61. if (!f) {
  62. nframes = (p->count - off - 3 - ac3_off) /
  63. ai.framesize + 1;
  64. p->buf[off + 2] = (ac3_off >> 8) & 0xff;
  65. p->buf[off + 3] = (ac3_off) & 0xff;
  66. p->buf[off + 1] = nframes;
  67. ac3_off += nframes * ai.framesize - p->count;
  68. }
  69. }
  70. }
  71. p->func(p->buf, p->count, p->data);
  72. p->buf[6] = 0x80;
  73. p->buf[7] = 0x00;
  74. p->buf[8] = 0x00;
  75. p->count = 9;
  76. if (p->repack_subids && p->cid == PRIVATE_STREAM1
  77. && (streamid & 0xf8) == 0x80) {
  78. p->count += 4;
  79. p->buf[9] = streamid;
  80. p->buf[10] = (ac3_off >> 8) & 0xff;
  81. p->buf[11] = (ac3_off) & 0xff;
  82. p->buf[12] = 0;
  83. }
  84. break;
  85. case 1:
  86. if (p->count < 8)
  87. return;
  88. p->buf[3] = p->cid;
  89. p->buf[4] = (u8)(((p->count - 6) & 0xff00) >> 8);
  90. p->buf[5] = (u8)((p->count - 6) & 0x00ff);
  91. p->func(p->buf, p->count, p->data);
  92. p->buf[6] = 0x0f;
  93. p->count = 7;
  94. break;
  95. }
  96. }
  97. void av7110_ipack_flush(struct ipack *p)
  98. {
  99. if (p->plength != MMAX_PLENGTH - 6 || p->found <= 6)
  100. return;
  101. p->plength = p->found - 6;
  102. p->found = 0;
  103. send_ipack(p);
  104. av7110_ipack_reset(p);
  105. }
  106. static void write_ipack(struct ipack *p, const u8 *data, int count)
  107. {
  108. u8 headr[3] = { 0x00, 0x00, 0x01 };
  109. if (p->count < 6) {
  110. memcpy(p->buf, headr, 3);
  111. p->count = 6;
  112. }
  113. if (p->count + count < p->size){
  114. memcpy(p->buf+p->count, data, count);
  115. p->count += count;
  116. } else {
  117. int rest = p->size - p->count;
  118. memcpy(p->buf+p->count, data, rest);
  119. p->count += rest;
  120. send_ipack(p);
  121. if (count - rest > 0)
  122. write_ipack(p, data + rest, count - rest);
  123. }
  124. }
  125. int av7110_ipack_instant_repack (const u8 *buf, int count, struct ipack *p)
  126. {
  127. int l;
  128. int c = 0;
  129. while (c < count && (p->mpeg == 0 ||
  130. (p->mpeg == 1 && p->found < 7) ||
  131. (p->mpeg == 2 && p->found < 9))
  132. && (p->found < 5 || !p->done)) {
  133. switch (p->found) {
  134. case 0:
  135. case 1:
  136. if (buf[c] == 0x00)
  137. p->found++;
  138. else
  139. p->found = 0;
  140. c++;
  141. break;
  142. case 2:
  143. if (buf[c] == 0x01)
  144. p->found++;
  145. else if (buf[c] == 0)
  146. p->found = 2;
  147. else
  148. p->found = 0;
  149. c++;
  150. break;
  151. case 3:
  152. p->cid = 0;
  153. switch (buf[c]) {
  154. case PROG_STREAM_MAP:
  155. case PRIVATE_STREAM2:
  156. case PROG_STREAM_DIR:
  157. case ECM_STREAM :
  158. case EMM_STREAM :
  159. case PADDING_STREAM :
  160. case DSM_CC_STREAM :
  161. case ISO13522_STREAM:
  162. p->done = 1;
  163. /* fall through */
  164. case PRIVATE_STREAM1:
  165. case VIDEO_STREAM_S ... VIDEO_STREAM_E:
  166. case AUDIO_STREAM_S ... AUDIO_STREAM_E:
  167. p->found++;
  168. p->cid = buf[c];
  169. c++;
  170. break;
  171. default:
  172. p->found = 0;
  173. break;
  174. }
  175. break;
  176. case 4:
  177. if (count-c > 1) {
  178. p->plen[0] = buf[c];
  179. c++;
  180. p->plen[1] = buf[c];
  181. c++;
  182. p->found += 2;
  183. p->plength = (p->plen[0] << 8) | p->plen[1];
  184. } else {
  185. p->plen[0] = buf[c];
  186. p->found++;
  187. return count;
  188. }
  189. break;
  190. case 5:
  191. p->plen[1] = buf[c];
  192. c++;
  193. p->found++;
  194. p->plength = (p->plen[0] << 8) | p->plen[1];
  195. break;
  196. case 6:
  197. if (!p->done) {
  198. p->flag1 = buf[c];
  199. c++;
  200. p->found++;
  201. if ((p->flag1 & 0xc0) == 0x80)
  202. p->mpeg = 2;
  203. else {
  204. p->hlength = 0;
  205. p->which = 0;
  206. p->mpeg = 1;
  207. p->flag2 = 0;
  208. }
  209. }
  210. break;
  211. case 7:
  212. if (!p->done && p->mpeg == 2) {
  213. p->flag2 = buf[c];
  214. c++;
  215. p->found++;
  216. }
  217. break;
  218. case 8:
  219. if (!p->done && p->mpeg == 2) {
  220. p->hlength = buf[c];
  221. c++;
  222. p->found++;
  223. }
  224. break;
  225. }
  226. }
  227. if (c == count)
  228. return count;
  229. if (!p->plength)
  230. p->plength = MMAX_PLENGTH - 6;
  231. if (p->done || ((p->mpeg == 2 && p->found >= 9) ||
  232. (p->mpeg == 1 && p->found >= 7))) {
  233. switch (p->cid) {
  234. case AUDIO_STREAM_S ... AUDIO_STREAM_E:
  235. case VIDEO_STREAM_S ... VIDEO_STREAM_E:
  236. case PRIVATE_STREAM1:
  237. if (p->mpeg == 2 && p->found == 9) {
  238. write_ipack(p, &p->flag1, 1);
  239. write_ipack(p, &p->flag2, 1);
  240. write_ipack(p, &p->hlength, 1);
  241. }
  242. if (p->mpeg == 1 && p->found == 7)
  243. write_ipack(p, &p->flag1, 1);
  244. if (p->mpeg == 2 && (p->flag2 & PTS_ONLY) &&
  245. p->found < 14) {
  246. while (c < count && p->found < 14) {
  247. p->pts[p->found - 9] = buf[c];
  248. write_ipack(p, buf + c, 1);
  249. c++;
  250. p->found++;
  251. }
  252. if (c == count)
  253. return count;
  254. }
  255. if (p->mpeg == 1 && p->which < 2000) {
  256. if (p->found == 7) {
  257. p->check = p->flag1;
  258. p->hlength = 1;
  259. }
  260. while (!p->which && c < count &&
  261. p->check == 0xff){
  262. p->check = buf[c];
  263. write_ipack(p, buf + c, 1);
  264. c++;
  265. p->found++;
  266. p->hlength++;
  267. }
  268. if (c == count)
  269. return count;
  270. if ((p->check & 0xc0) == 0x40 && !p->which) {
  271. p->check = buf[c];
  272. write_ipack(p, buf + c, 1);
  273. c++;
  274. p->found++;
  275. p->hlength++;
  276. p->which = 1;
  277. if (c == count)
  278. return count;
  279. p->check = buf[c];
  280. write_ipack(p, buf + c, 1);
  281. c++;
  282. p->found++;
  283. p->hlength++;
  284. p->which = 2;
  285. if (c == count)
  286. return count;
  287. }
  288. if (p->which == 1) {
  289. p->check = buf[c];
  290. write_ipack(p, buf + c, 1);
  291. c++;
  292. p->found++;
  293. p->hlength++;
  294. p->which = 2;
  295. if (c == count)
  296. return count;
  297. }
  298. if ((p->check & 0x30) && p->check != 0xff) {
  299. p->flag2 = (p->check & 0xf0) << 2;
  300. p->pts[0] = p->check;
  301. p->which = 3;
  302. }
  303. if (c == count)
  304. return count;
  305. if (p->which > 2){
  306. if ((p->flag2 & PTS_DTS_FLAGS) == PTS_ONLY) {
  307. while (c < count && p->which < 7) {
  308. p->pts[p->which - 2] = buf[c];
  309. write_ipack(p, buf + c, 1);
  310. c++;
  311. p->found++;
  312. p->which++;
  313. p->hlength++;
  314. }
  315. if (c == count)
  316. return count;
  317. } else if ((p->flag2 & PTS_DTS_FLAGS) == PTS_DTS) {
  318. while (c < count && p->which < 12) {
  319. if (p->which < 7)
  320. p->pts[p->which - 2] = buf[c];
  321. write_ipack(p, buf + c, 1);
  322. c++;
  323. p->found++;
  324. p->which++;
  325. p->hlength++;
  326. }
  327. if (c == count)
  328. return count;
  329. }
  330. p->which = 2000;
  331. }
  332. }
  333. while (c < count && p->found < p->plength + 6) {
  334. l = count - c;
  335. if (l + p->found > p->plength + 6)
  336. l = p->plength + 6 - p->found;
  337. write_ipack(p, buf + c, l);
  338. p->found += l;
  339. c += l;
  340. }
  341. break;
  342. }
  343. if (p->done) {
  344. if (p->found + count - c < p->plength + 6) {
  345. p->found += count - c;
  346. c = count;
  347. } else {
  348. c += p->plength + 6 - p->found;
  349. p->found = p->plength + 6;
  350. }
  351. }
  352. if (p->plength && p->found == p->plength + 6) {
  353. send_ipack(p);
  354. av7110_ipack_reset(p);
  355. if (c < count)
  356. av7110_ipack_instant_repack(buf + c, count - c, p);
  357. }
  358. }
  359. return count;
  360. }