dvb_filter.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  1. #include <linux/kernel.h>
  2. #include <linux/module.h>
  3. #include <linux/string.h>
  4. #include "dvb_filter.h"
  5. #if 0
  6. static unsigned int bitrates[3][16] =
  7. {{0,32,64,96,128,160,192,224,256,288,320,352,384,416,448,0},
  8. {0,32,48,56,64,80,96,112,128,160,192,224,256,320,384,0},
  9. {0,32,40,48,56,64,80,96,112,128,160,192,224,256,320,0}};
  10. #endif
  11. static u32 freq[4] = {480, 441, 320, 0};
  12. static unsigned int ac3_bitrates[32] =
  13. {32,40,48,56,64,80,96,112,128,160,192,224,256,320,384,448,512,576,640,
  14. 0,0,0,0,0,0,0,0,0,0,0,0,0};
  15. static u32 ac3_frames[3][32] =
  16. {{64,80,96,112,128,160,192,224,256,320,384,448,512,640,768,896,1024,
  17. 1152,1280,0,0,0,0,0,0,0,0,0,0,0,0,0},
  18. {69,87,104,121,139,174,208,243,278,348,417,487,557,696,835,975,1114,
  19. 1253,1393,0,0,0,0,0,0,0,0,0,0,0,0,0},
  20. {96,120,144,168,192,240,288,336,384,480,576,672,768,960,1152,1344,
  21. 1536,1728,1920,0,0,0,0,0,0,0,0,0,0,0,0,0}};
  22. #if 0
  23. static void setup_ts2pes(ipack *pa, ipack *pv, u16 *pida, u16 *pidv,
  24. void (*pes_write)(u8 *buf, int count, void *data),
  25. void *priv)
  26. {
  27. dvb_filter_ipack_init(pa, IPACKS, pes_write);
  28. dvb_filter_ipack_init(pv, IPACKS, pes_write);
  29. pa->pid = pida;
  30. pv->pid = pidv;
  31. pa->data = priv;
  32. pv->data = priv;
  33. }
  34. #endif
  35. #if 0
  36. static void ts_to_pes(ipack *p, u8 *buf) // don't need count (=188)
  37. {
  38. u8 off = 0;
  39. if (!buf || !p ){
  40. printk("NULL POINTER IDIOT\n");
  41. return;
  42. }
  43. if (buf[1]&PAY_START) {
  44. if (p->plength == MMAX_PLENGTH-6 && p->found>6){
  45. p->plength = p->found-6;
  46. p->found = 0;
  47. send_ipack(p);
  48. dvb_filter_ipack_reset(p);
  49. }
  50. }
  51. if (buf[3] & ADAPT_FIELD) { // adaptation field?
  52. off = buf[4] + 1;
  53. if (off+4 > 187) return;
  54. }
  55. dvb_filter_instant_repack(buf+4+off, TS_SIZE-4-off, p);
  56. }
  57. #endif
  58. #if 0
  59. /* needs 5 byte input, returns picture coding type*/
  60. static int read_picture_header(u8 *headr, struct mpg_picture *pic, int field, int pr)
  61. {
  62. u8 pct;
  63. if (pr) printk( "Pic header: ");
  64. pic->temporal_reference[field] = (( headr[0] << 2 ) |
  65. (headr[1] & 0x03) )& 0x03ff;
  66. if (pr) printk( " temp ref: 0x%04x", pic->temporal_reference[field]);
  67. pct = ( headr[1] >> 2 ) & 0x07;
  68. pic->picture_coding_type[field] = pct;
  69. if (pr) {
  70. switch(pct){
  71. case I_FRAME:
  72. printk( " I-FRAME");
  73. break;
  74. case B_FRAME:
  75. printk( " B-FRAME");
  76. break;
  77. case P_FRAME:
  78. printk( " P-FRAME");
  79. break;
  80. }
  81. }
  82. pic->vinfo.vbv_delay = (( headr[1] >> 5 ) | ( headr[2] << 3) |
  83. ( (headr[3] & 0x1F) << 11) ) & 0xffff;
  84. if (pr) printk( " vbv delay: 0x%04x", pic->vinfo.vbv_delay);
  85. pic->picture_header_parameter = ( headr[3] & 0xe0 ) |
  86. ((headr[4] & 0x80) >> 3);
  87. if ( pct == B_FRAME ){
  88. pic->picture_header_parameter |= ( headr[4] >> 3 ) & 0x0f;
  89. }
  90. if (pr) printk( " pic head param: 0x%x",
  91. pic->picture_header_parameter);
  92. return pct;
  93. }
  94. #endif
  95. #if 0
  96. /* needs 4 byte input */
  97. static int read_gop_header(u8 *headr, struct mpg_picture *pic, int pr)
  98. {
  99. if (pr) printk("GOP header: ");
  100. pic->time_code = (( headr[0] << 17 ) | ( headr[1] << 9) |
  101. ( headr[2] << 1 ) | (headr[3] &0x01)) & 0x1ffffff;
  102. if (pr) printk(" time: %d:%d.%d ", (headr[0]>>2)& 0x1F,
  103. ((headr[0]<<4)& 0x30)| ((headr[1]>>4)& 0x0F),
  104. ((headr[1]<<3)& 0x38)| ((headr[2]>>5)& 0x0F));
  105. if ( ( headr[3] & 0x40 ) != 0 ){
  106. pic->closed_gop = 1;
  107. } else {
  108. pic->closed_gop = 0;
  109. }
  110. if (pr) printk("closed: %d", pic->closed_gop);
  111. if ( ( headr[3] & 0x20 ) != 0 ){
  112. pic->broken_link = 1;
  113. } else {
  114. pic->broken_link = 0;
  115. }
  116. if (pr) printk(" broken: %d\n", pic->broken_link);
  117. return 0;
  118. }
  119. #endif
  120. #if 0
  121. /* needs 8 byte input */
  122. static int read_sequence_header(u8 *headr, struct dvb_video_info *vi, int pr)
  123. {
  124. int sw;
  125. int form = -1;
  126. if (pr) printk("Reading sequence header\n");
  127. vi->horizontal_size = ((headr[1] &0xF0) >> 4) | (headr[0] << 4);
  128. vi->vertical_size = ((headr[1] &0x0F) << 8) | (headr[2]);
  129. sw = (int)((headr[3]&0xF0) >> 4) ;
  130. switch( sw ){
  131. case 1:
  132. if (pr)
  133. printk("Videostream: ASPECT: 1:1");
  134. vi->aspect_ratio = 100;
  135. break;
  136. case 2:
  137. if (pr)
  138. printk("Videostream: ASPECT: 4:3");
  139. vi->aspect_ratio = 133;
  140. break;
  141. case 3:
  142. if (pr)
  143. printk("Videostream: ASPECT: 16:9");
  144. vi->aspect_ratio = 177;
  145. break;
  146. case 4:
  147. if (pr)
  148. printk("Videostream: ASPECT: 2.21:1");
  149. vi->aspect_ratio = 221;
  150. break;
  151. case 5 ... 15:
  152. if (pr)
  153. printk("Videostream: ASPECT: reserved");
  154. vi->aspect_ratio = 0;
  155. break;
  156. default:
  157. vi->aspect_ratio = 0;
  158. return -1;
  159. }
  160. if (pr)
  161. printk(" Size = %dx%d",vi->horizontal_size,vi->vertical_size);
  162. sw = (int)(headr[3]&0x0F);
  163. switch ( sw ) {
  164. case 1:
  165. if (pr)
  166. printk(" FRate: 23.976 fps");
  167. vi->framerate = 23976;
  168. form = -1;
  169. break;
  170. case 2:
  171. if (pr)
  172. printk(" FRate: 24 fps");
  173. vi->framerate = 24000;
  174. form = -1;
  175. break;
  176. case 3:
  177. if (pr)
  178. printk(" FRate: 25 fps");
  179. vi->framerate = 25000;
  180. form = VIDEO_MODE_PAL;
  181. break;
  182. case 4:
  183. if (pr)
  184. printk(" FRate: 29.97 fps");
  185. vi->framerate = 29970;
  186. form = VIDEO_MODE_NTSC;
  187. break;
  188. case 5:
  189. if (pr)
  190. printk(" FRate: 30 fps");
  191. vi->framerate = 30000;
  192. form = VIDEO_MODE_NTSC;
  193. break;
  194. case 6:
  195. if (pr)
  196. printk(" FRate: 50 fps");
  197. vi->framerate = 50000;
  198. form = VIDEO_MODE_PAL;
  199. break;
  200. case 7:
  201. if (pr)
  202. printk(" FRate: 60 fps");
  203. vi->framerate = 60000;
  204. form = VIDEO_MODE_NTSC;
  205. break;
  206. }
  207. vi->bit_rate = (headr[4] << 10) | (headr[5] << 2) | (headr[6] & 0x03);
  208. vi->vbv_buffer_size
  209. = (( headr[6] & 0xF8) >> 3 ) | (( headr[7] & 0x1F )<< 5);
  210. if (pr){
  211. printk(" BRate: %d Mbit/s",4*(vi->bit_rate)/10000);
  212. printk(" vbvbuffer %d",16*1024*(vi->vbv_buffer_size));
  213. printk("\n");
  214. }
  215. vi->video_format = form;
  216. return 0;
  217. }
  218. #endif
  219. #if 0
  220. static int get_vinfo(u8 *mbuf, int count, struct dvb_video_info *vi, int pr)
  221. {
  222. u8 *headr;
  223. int found = 0;
  224. int c = 0;
  225. while (found < 4 && c+4 < count){
  226. u8 *b;
  227. b = mbuf+c;
  228. if ( b[0] == 0x00 && b[1] == 0x00 && b[2] == 0x01
  229. && b[3] == 0xb3) found = 4;
  230. else {
  231. c++;
  232. }
  233. }
  234. if (! found) return -1;
  235. c += 4;
  236. if (c+12 >= count) return -1;
  237. headr = mbuf+c;
  238. if (read_sequence_header(headr, vi, pr) < 0) return -1;
  239. vi->off = c-4;
  240. return 0;
  241. }
  242. #endif
  243. #if 0
  244. static int get_ainfo(u8 *mbuf, int count, struct dvb_audio_info *ai, int pr)
  245. {
  246. u8 *headr;
  247. int found = 0;
  248. int c = 0;
  249. int fr = 0;
  250. while (found < 2 && c < count){
  251. u8 b[2];
  252. memcpy( b, mbuf+c, 2);
  253. if ( b[0] == 0xff && (b[1] & 0xf8) == 0xf8)
  254. found = 2;
  255. else {
  256. c++;
  257. }
  258. }
  259. if (!found) return -1;
  260. if (c+3 >= count) return -1;
  261. headr = mbuf+c;
  262. ai->layer = (headr[1] & 0x06) >> 1;
  263. if (pr)
  264. printk("Audiostream: Layer: %d", 4-ai->layer);
  265. ai->bit_rate = bitrates[(3-ai->layer)][(headr[2] >> 4 )]*1000;
  266. if (pr){
  267. if (ai->bit_rate == 0)
  268. printk(" Bit rate: free");
  269. else if (ai->bit_rate == 0xf)
  270. printk(" BRate: reserved");
  271. else
  272. printk(" BRate: %d kb/s", ai->bit_rate/1000);
  273. }
  274. fr = (headr[2] & 0x0c ) >> 2;
  275. ai->frequency = freq[fr]*100;
  276. if (pr){
  277. if (ai->frequency == 3)
  278. printk(" Freq: reserved\n");
  279. else
  280. printk(" Freq: %d kHz\n",ai->frequency);
  281. }
  282. ai->off = c;
  283. return 0;
  284. }
  285. #endif
  286. int dvb_filter_get_ac3info(u8 *mbuf, int count, struct dvb_audio_info *ai, int pr)
  287. {
  288. u8 *headr;
  289. int found = 0;
  290. int c = 0;
  291. u8 frame = 0;
  292. int fr = 0;
  293. while ( !found && c < count){
  294. u8 *b = mbuf+c;
  295. if ( b[0] == 0x0b && b[1] == 0x77 )
  296. found = 1;
  297. else {
  298. c++;
  299. }
  300. }
  301. if (!found) return -1;
  302. if (pr)
  303. printk("Audiostream: AC3");
  304. ai->off = c;
  305. if (c+5 >= count) return -1;
  306. ai->layer = 0; // 0 for AC3
  307. headr = mbuf+c+2;
  308. frame = (headr[2]&0x3f);
  309. ai->bit_rate = ac3_bitrates[frame >> 1]*1000;
  310. if (pr)
  311. printk(" BRate: %d kb/s", (int) ai->bit_rate/1000);
  312. ai->frequency = (headr[2] & 0xc0 ) >> 6;
  313. fr = (headr[2] & 0xc0 ) >> 6;
  314. ai->frequency = freq[fr]*100;
  315. if (pr) printk (" Freq: %d Hz\n", (int) ai->frequency);
  316. ai->framesize = ac3_frames[fr][frame >> 1];
  317. if ((frame & 1) && (fr == 1)) ai->framesize++;
  318. ai->framesize = ai->framesize << 1;
  319. if (pr) printk (" Framesize %d\n",(int) ai->framesize);
  320. return 0;
  321. }
  322. EXPORT_SYMBOL(dvb_filter_get_ac3info);
  323. #if 0
  324. static u8 *skip_pes_header(u8 **bufp)
  325. {
  326. u8 *inbuf = *bufp;
  327. u8 *buf = inbuf;
  328. u8 *pts = NULL;
  329. int skip = 0;
  330. static const int mpeg1_skip_table[16] = {
  331. 1, 0xffff, 5, 10, 0xffff, 0xffff, 0xffff, 0xffff,
  332. 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff
  333. };
  334. if ((inbuf[6] & 0xc0) == 0x80){ /* mpeg2 */
  335. if (buf[7] & PTS_ONLY)
  336. pts = buf+9;
  337. else pts = NULL;
  338. buf = inbuf + 9 + inbuf[8];
  339. } else { /* mpeg1 */
  340. for (buf = inbuf + 6; *buf == 0xff; buf++)
  341. if (buf == inbuf + 6 + 16) {
  342. break;
  343. }
  344. if ((*buf & 0xc0) == 0x40)
  345. buf += 2;
  346. skip = mpeg1_skip_table [*buf >> 4];
  347. if (skip == 5 || skip == 10) pts = buf;
  348. else pts = NULL;
  349. buf += mpeg1_skip_table [*buf >> 4];
  350. }
  351. *bufp = buf;
  352. return pts;
  353. }
  354. #endif
  355. #if 0
  356. static void initialize_quant_matrix( u32 *matrix )
  357. {
  358. int i;
  359. matrix[0] = 0x08101013;
  360. matrix[1] = 0x10131616;
  361. matrix[2] = 0x16161616;
  362. matrix[3] = 0x1a181a1b;
  363. matrix[4] = 0x1b1b1a1a;
  364. matrix[5] = 0x1a1a1b1b;
  365. matrix[6] = 0x1b1d1d1d;
  366. matrix[7] = 0x2222221d;
  367. matrix[8] = 0x1d1d1b1b;
  368. matrix[9] = 0x1d1d2020;
  369. matrix[10] = 0x22222526;
  370. matrix[11] = 0x25232322;
  371. matrix[12] = 0x23262628;
  372. matrix[13] = 0x28283030;
  373. matrix[14] = 0x2e2e3838;
  374. matrix[15] = 0x3a454553;
  375. for ( i = 16 ; i < 32 ; i++ )
  376. matrix[i] = 0x10101010;
  377. }
  378. #endif
  379. #if 0
  380. static void initialize_mpg_picture(struct mpg_picture *pic)
  381. {
  382. int i;
  383. /* set MPEG1 */
  384. pic->mpeg1_flag = 1;
  385. pic->profile_and_level = 0x4A ; /* MP@LL */
  386. pic->progressive_sequence = 1;
  387. pic->low_delay = 0;
  388. pic->sequence_display_extension_flag = 0;
  389. for ( i = 0 ; i < 4 ; i++ ){
  390. pic->frame_centre_horizontal_offset[i] = 0;
  391. pic->frame_centre_vertical_offset[i] = 0;
  392. }
  393. pic->last_frame_centre_horizontal_offset = 0;
  394. pic->last_frame_centre_vertical_offset = 0;
  395. pic->picture_display_extension_flag[0] = 0;
  396. pic->picture_display_extension_flag[1] = 0;
  397. pic->sequence_header_flag = 0;
  398. pic->gop_flag = 0;
  399. pic->sequence_end_flag = 0;
  400. }
  401. #endif
  402. #if 0
  403. static void mpg_set_picture_parameter( int32_t field_type, struct mpg_picture *pic )
  404. {
  405. int16_t last_h_offset;
  406. int16_t last_v_offset;
  407. int16_t *p_h_offset;
  408. int16_t *p_v_offset;
  409. if ( pic->mpeg1_flag ){
  410. pic->picture_structure[field_type] = VIDEO_FRAME_PICTURE;
  411. pic->top_field_first = 0;
  412. pic->repeat_first_field = 0;
  413. pic->progressive_frame = 1;
  414. pic->picture_coding_parameter = 0x000010;
  415. }
  416. /* Reset flag */
  417. pic->picture_display_extension_flag[field_type] = 0;
  418. last_h_offset = pic->last_frame_centre_horizontal_offset;
  419. last_v_offset = pic->last_frame_centre_vertical_offset;
  420. if ( field_type == FIRST_FIELD ){
  421. p_h_offset = pic->frame_centre_horizontal_offset;
  422. p_v_offset = pic->frame_centre_vertical_offset;
  423. *p_h_offset = last_h_offset;
  424. *(p_h_offset + 1) = last_h_offset;
  425. *(p_h_offset + 2) = last_h_offset;
  426. *p_v_offset = last_v_offset;
  427. *(p_v_offset + 1) = last_v_offset;
  428. *(p_v_offset + 2) = last_v_offset;
  429. } else {
  430. pic->frame_centre_horizontal_offset[3] = last_h_offset;
  431. pic->frame_centre_vertical_offset[3] = last_v_offset;
  432. }
  433. }
  434. #endif
  435. #if 0
  436. static void init_mpg_picture( struct mpg_picture *pic, int chan, int32_t field_type)
  437. {
  438. pic->picture_header = 0;
  439. pic->sequence_header_data
  440. = ( INIT_HORIZONTAL_SIZE << 20 )
  441. | ( INIT_VERTICAL_SIZE << 8 )
  442. | ( INIT_ASPECT_RATIO << 4 )
  443. | ( INIT_FRAME_RATE );
  444. pic->mpeg1_flag = 0;
  445. pic->vinfo.horizontal_size
  446. = INIT_DISP_HORIZONTAL_SIZE;
  447. pic->vinfo.vertical_size
  448. = INIT_DISP_VERTICAL_SIZE;
  449. pic->picture_display_extension_flag[field_type]
  450. = 0;
  451. pic->pts_flag[field_type] = 0;
  452. pic->sequence_gop_header = 0;
  453. pic->picture_header = 0;
  454. pic->sequence_header_flag = 0;
  455. pic->gop_flag = 0;
  456. pic->sequence_end_flag = 0;
  457. pic->sequence_display_extension_flag = 0;
  458. pic->last_frame_centre_horizontal_offset = 0;
  459. pic->last_frame_centre_vertical_offset = 0;
  460. pic->channel = chan;
  461. }
  462. #endif
  463. void dvb_filter_pes2ts_init(struct dvb_filter_pes2ts *p2ts, unsigned short pid,
  464. dvb_filter_pes2ts_cb_t *cb, void *priv)
  465. {
  466. unsigned char *buf=p2ts->buf;
  467. buf[0]=0x47;
  468. buf[1]=(pid>>8);
  469. buf[2]=pid&0xff;
  470. p2ts->cc=0;
  471. p2ts->cb=cb;
  472. p2ts->priv=priv;
  473. }
  474. EXPORT_SYMBOL(dvb_filter_pes2ts_init);
  475. int dvb_filter_pes2ts(struct dvb_filter_pes2ts *p2ts, unsigned char *pes,
  476. int len, int payload_start)
  477. {
  478. unsigned char *buf=p2ts->buf;
  479. int ret=0, rest;
  480. //len=6+((pes[4]<<8)|pes[5]);
  481. if (payload_start)
  482. buf[1]|=0x40;
  483. else
  484. buf[1]&=~0x40;
  485. while (len>=184) {
  486. buf[3]=0x10|((p2ts->cc++)&0x0f);
  487. memcpy(buf+4, pes, 184);
  488. if ((ret=p2ts->cb(p2ts->priv, buf)))
  489. return ret;
  490. len-=184; pes+=184;
  491. buf[1]&=~0x40;
  492. }
  493. if (!len)
  494. return 0;
  495. buf[3]=0x30|((p2ts->cc++)&0x0f);
  496. rest=183-len;
  497. if (rest) {
  498. buf[5]=0x00;
  499. if (rest-1)
  500. memset(buf+6, 0xff, rest-1);
  501. }
  502. buf[4]=rest;
  503. memcpy(buf+5+rest, pes, len);
  504. return p2ts->cb(p2ts->priv, buf);
  505. }
  506. EXPORT_SYMBOL(dvb_filter_pes2ts);