1 #include <linux/kernel.h>
2 #include <linux/module.h>
3 #include <linux/string.h>
4 #include "dvb_filter.h"
6 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}};
11 u32 freq[4] = {441, 480, 320, 0};
13 unsigned int ac3_bitrates[32] =
14 {32,40,48,56,64,80,96,112,128,160,192,224,256,320,384,448,512,576,640,
15 0,0,0,0,0,0,0,0,0,0,0,0,0};
17 u32 ac3_freq[4] = {480, 441, 320, 0};
18 u32 ac3_frames[3][32] =
19 {{64,80,96,112,128,160,192,224,256,320,384,448,512,640,768,896,1024,
20 1152,1280,0,0,0,0,0,0,0,0,0,0,0,0,0},
21 {69,87,104,121,139,174,208,243,278,348,417,487,557,696,835,975,1114,
22 1253,1393,0,0,0,0,0,0,0,0,0,0,0,0,0},
23 {96,120,144,168,192,240,288,336,384,480,576,672,768,960,1152,1344,
24 1536,1728,1920,0,0,0,0,0,0,0,0,0,0,0,0,0}};
29 static void setup_ts2pes(ipack *pa, ipack *pv, u16 *pida, u16 *pidv,
30 void (*pes_write)(u8 *buf, int count, void *data),
33 dvb_filter_ipack_init(pa, IPACKS, pes_write);
34 dvb_filter_ipack_init(pv, IPACKS, pes_write);
43 static void ts_to_pes(ipack *p, u8 *buf) // don't need count (=188)
48 printk("NULL POINTER IDIOT\n");
51 if (buf[1]&PAY_START) {
52 if (p->plength == MMAX_PLENGTH-6 && p->found>6){
53 p->plength = p->found-6;
56 dvb_filter_ipack_reset(p);
59 if (buf[3] & ADAPT_FIELD) { // adaptation field?
61 if (off+4 > 187) return;
63 dvb_filter_instant_repack(buf+4+off, TS_SIZE-4-off, p);
68 /* needs 5 byte input, returns picture coding type*/
69 static int read_picture_header(u8 *headr, struct mpg_picture *pic, int field, int pr)
73 if (pr) printk( "Pic header: ");
74 pic->temporal_reference[field] = (( headr[0] << 2 ) |
75 (headr[1] & 0x03) )& 0x03ff;
76 if (pr) printk( " temp ref: 0x%04x", pic->temporal_reference[field]);
78 pct = ( headr[1] >> 2 ) & 0x07;
79 pic->picture_coding_type[field] = pct;
95 pic->vinfo.vbv_delay = (( headr[1] >> 5 ) | ( headr[2] << 3) |
96 ( (headr[3] & 0x1F) << 11) ) & 0xffff;
98 if (pr) printk( " vbv delay: 0x%04x", pic->vinfo.vbv_delay);
100 pic->picture_header_parameter = ( headr[3] & 0xe0 ) |
101 ((headr[4] & 0x80) >> 3);
103 if ( pct == B_FRAME ){
104 pic->picture_header_parameter |= ( headr[4] >> 3 ) & 0x0f;
106 if (pr) printk( " pic head param: 0x%x",
107 pic->picture_header_parameter);
114 /* needs 4 byte input */
115 static int read_gop_header(u8 *headr, struct mpg_picture *pic, int pr)
117 if (pr) printk("GOP header: ");
119 pic->time_code = (( headr[0] << 17 ) | ( headr[1] << 9) |
120 ( headr[2] << 1 ) | (headr[3] &0x01)) & 0x1ffffff;
122 if (pr) printk(" time: %d:%d.%d ", (headr[0]>>2)& 0x1F,
123 ((headr[0]<<4)& 0x30)| ((headr[1]>>4)& 0x0F),
124 ((headr[1]<<3)& 0x38)| ((headr[2]>>5)& 0x0F));
126 if ( ( headr[3] & 0x40 ) != 0 ){
131 if (pr) printk("closed: %d", pic->closed_gop);
133 if ( ( headr[3] & 0x20 ) != 0 ){
134 pic->broken_link = 1;
136 pic->broken_link = 0;
138 if (pr) printk(" broken: %d\n", pic->broken_link);
145 /* needs 8 byte input */
146 static int read_sequence_header(u8 *headr, struct dvb_video_info *vi, int pr)
151 if (pr) printk("Reading sequence header\n");
153 vi->horizontal_size = ((headr[1] &0xF0) >> 4) | (headr[0] << 4);
154 vi->vertical_size = ((headr[1] &0x0F) << 8) | (headr[2]);
156 sw = (int)((headr[3]&0xF0) >> 4) ;
161 printk("Videostream: ASPECT: 1:1");
162 vi->aspect_ratio = 100;
166 printk("Videostream: ASPECT: 4:3");
167 vi->aspect_ratio = 133;
171 printk("Videostream: ASPECT: 16:9");
172 vi->aspect_ratio = 177;
176 printk("Videostream: ASPECT: 2.21:1");
177 vi->aspect_ratio = 221;
182 printk("Videostream: ASPECT: reserved");
183 vi->aspect_ratio = 0;
187 vi->aspect_ratio = 0;
192 printk(" Size = %dx%d",vi->horizontal_size,vi->vertical_size);
194 sw = (int)(headr[3]&0x0F);
199 printk(" FRate: 23.976 fps");
200 vi->framerate = 23976;
205 printk(" FRate: 24 fps");
206 vi->framerate = 24000;
211 printk(" FRate: 25 fps");
212 vi->framerate = 25000;
213 form = VIDEO_MODE_PAL;
217 printk(" FRate: 29.97 fps");
218 vi->framerate = 29970;
219 form = VIDEO_MODE_NTSC;
223 printk(" FRate: 30 fps");
224 vi->framerate = 30000;
225 form = VIDEO_MODE_NTSC;
229 printk(" FRate: 50 fps");
230 vi->framerate = 50000;
231 form = VIDEO_MODE_PAL;
235 printk(" FRate: 60 fps");
236 vi->framerate = 60000;
237 form = VIDEO_MODE_NTSC;
241 vi->bit_rate = (headr[4] << 10) | (headr[5] << 2) | (headr[6] & 0x03);
244 = (( headr[6] & 0xF8) >> 3 ) | (( headr[7] & 0x1F )<< 5);
247 printk(" BRate: %d Mbit/s",4*(vi->bit_rate)/10000);
248 printk(" vbvbuffer %d",16*1024*(vi->vbv_buffer_size));
252 vi->video_format = form;
260 static int get_vinfo(u8 *mbuf, int count, struct dvb_video_info *vi, int pr)
266 while (found < 4 && c+4 < count){
270 if ( b[0] == 0x00 && b[1] == 0x00 && b[2] == 0x01
271 && b[3] == 0xb3) found = 4;
277 if (! found) return -1;
279 if (c+12 >= count) return -1;
281 if (read_sequence_header(headr, vi, pr) < 0) return -1;
289 static int get_ainfo(u8 *mbuf, int count, struct dvb_audio_info *ai, int pr)
296 while (found < 2 && c < count){
298 memcpy( b, mbuf+c, 2);
300 if ( b[0] == 0xff && (b[1] & 0xf8) == 0xf8)
307 if (!found) return -1;
309 if (c+3 >= count) return -1;
312 ai->layer = (headr[1] & 0x06) >> 1;
315 printk("Audiostream: Layer: %d", 4-ai->layer);
318 ai->bit_rate = bitrates[(3-ai->layer)][(headr[2] >> 4 )]*1000;
321 if (ai->bit_rate == 0)
322 printk(" Bit rate: free");
323 else if (ai->bit_rate == 0xf)
324 printk(" BRate: reserved");
326 printk(" BRate: %d kb/s", ai->bit_rate/1000);
329 fr = (headr[2] & 0x0c ) >> 2;
330 ai->frequency = freq[fr]*100;
332 if (ai->frequency == 3)
333 printk(" Freq: reserved\n");
335 printk(" Freq: %d kHz\n",ai->frequency);
344 int dvb_filter_get_ac3info(u8 *mbuf, int count, struct dvb_audio_info *ai, int pr)
352 while ( !found && c < count){
355 if ( b[0] == 0x0b && b[1] == 0x77 )
362 if (!found) return -1;
364 printk("Audiostream: AC3");
367 if (c+5 >= count) return -1;
369 ai->layer = 0; // 0 for AC3
372 frame = (headr[2]&0x3f);
373 ai->bit_rate = ac3_bitrates[frame >> 1]*1000;
376 printk(" BRate: %d kb/s", (int) ai->bit_rate/1000);
378 ai->frequency = (headr[2] & 0xc0 ) >> 6;
379 fr = (headr[2] & 0xc0 ) >> 6;
380 ai->frequency = freq[fr]*100;
381 if (pr) printk (" Freq: %d Hz\n", (int) ai->frequency);
384 ai->framesize = ac3_frames[fr][frame >> 1];
385 if ((frame & 1) && (fr == 1)) ai->framesize++;
386 ai->framesize = ai->framesize << 1;
387 if (pr) printk (" Framesize %d\n",(int) ai->framesize);
395 static u8 *skip_pes_header(u8 **bufp)
402 static const int mpeg1_skip_table[16] = {
403 1, 0xffff, 5, 10, 0xffff, 0xffff, 0xffff, 0xffff,
404 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff
408 if ((inbuf[6] & 0xc0) == 0x80){ /* mpeg2 */
409 if (buf[7] & PTS_ONLY)
412 buf = inbuf + 9 + inbuf[8];
414 for (buf = inbuf + 6; *buf == 0xff; buf++)
415 if (buf == inbuf + 6 + 16) {
418 if ((*buf & 0xc0) == 0x40)
420 skip = mpeg1_skip_table [*buf >> 4];
421 if (skip == 5 || skip == 10) pts = buf;
424 buf += mpeg1_skip_table [*buf >> 4];
433 static void initialize_quant_matrix( u32 *matrix )
437 matrix[0] = 0x08101013;
438 matrix[1] = 0x10131616;
439 matrix[2] = 0x16161616;
440 matrix[3] = 0x1a181a1b;
441 matrix[4] = 0x1b1b1a1a;
442 matrix[5] = 0x1a1a1b1b;
443 matrix[6] = 0x1b1d1d1d;
444 matrix[7] = 0x2222221d;
445 matrix[8] = 0x1d1d1b1b;
446 matrix[9] = 0x1d1d2020;
447 matrix[10] = 0x22222526;
448 matrix[11] = 0x25232322;
449 matrix[12] = 0x23262628;
450 matrix[13] = 0x28283030;
451 matrix[14] = 0x2e2e3838;
452 matrix[15] = 0x3a454553;
454 for ( i = 16 ; i < 32 ; i++ )
455 matrix[i] = 0x10101010;
460 static void initialize_mpg_picture(struct mpg_picture *pic)
466 pic->profile_and_level = 0x4A ; /* MP@LL */
467 pic->progressive_sequence = 1;
470 pic->sequence_display_extension_flag = 0;
471 for ( i = 0 ; i < 4 ; i++ ){
472 pic->frame_centre_horizontal_offset[i] = 0;
473 pic->frame_centre_vertical_offset[i] = 0;
475 pic->last_frame_centre_horizontal_offset = 0;
476 pic->last_frame_centre_vertical_offset = 0;
478 pic->picture_display_extension_flag[0] = 0;
479 pic->picture_display_extension_flag[1] = 0;
480 pic->sequence_header_flag = 0;
482 pic->sequence_end_flag = 0;
487 static void mpg_set_picture_parameter( int32_t field_type, struct mpg_picture *pic )
489 int16_t last_h_offset;
490 int16_t last_v_offset;
495 if ( pic->mpeg1_flag ){
496 pic->picture_structure[field_type] = VIDEO_FRAME_PICTURE;
497 pic->top_field_first = 0;
498 pic->repeat_first_field = 0;
499 pic->progressive_frame = 1;
500 pic->picture_coding_parameter = 0x000010;
504 pic->picture_display_extension_flag[field_type] = 0;
506 last_h_offset = pic->last_frame_centre_horizontal_offset;
507 last_v_offset = pic->last_frame_centre_vertical_offset;
508 if ( field_type == FIRST_FIELD ){
509 p_h_offset = pic->frame_centre_horizontal_offset;
510 p_v_offset = pic->frame_centre_vertical_offset;
511 *p_h_offset = last_h_offset;
512 *(p_h_offset + 1) = last_h_offset;
513 *(p_h_offset + 2) = last_h_offset;
514 *p_v_offset = last_v_offset;
515 *(p_v_offset + 1) = last_v_offset;
516 *(p_v_offset + 2) = last_v_offset;
518 pic->frame_centre_horizontal_offset[3] = last_h_offset;
519 pic->frame_centre_vertical_offset[3] = last_v_offset;
525 static void init_mpg_picture( struct mpg_picture *pic, int chan, int32_t field_type)
527 pic->picture_header = 0;
528 pic->sequence_header_data
529 = ( INIT_HORIZONTAL_SIZE << 20 )
530 | ( INIT_VERTICAL_SIZE << 8 )
531 | ( INIT_ASPECT_RATIO << 4 )
532 | ( INIT_FRAME_RATE );
534 pic->vinfo.horizontal_size
535 = INIT_DISP_HORIZONTAL_SIZE;
536 pic->vinfo.vertical_size
537 = INIT_DISP_VERTICAL_SIZE;
538 pic->picture_display_extension_flag[field_type]
540 pic->pts_flag[field_type] = 0;
542 pic->sequence_gop_header = 0;
543 pic->picture_header = 0;
544 pic->sequence_header_flag = 0;
546 pic->sequence_end_flag = 0;
547 pic->sequence_display_extension_flag = 0;
548 pic->last_frame_centre_horizontal_offset = 0;
549 pic->last_frame_centre_vertical_offset = 0;
554 void dvb_filter_pes2ts_init(struct dvb_filter_pes2ts *p2ts, unsigned short pid,
555 dvb_filter_pes2ts_cb_t *cb, void *priv)
557 unsigned char *buf=p2ts->buf;
567 int dvb_filter_pes2ts(struct dvb_filter_pes2ts *p2ts, unsigned char *pes,
568 int len, int payload_start)
570 unsigned char *buf=p2ts->buf;
573 //len=6+((pes[4]<<8)|pes[5]);
580 buf[3]=0x10|((p2ts->cc++)&0x0f);
581 memcpy(buf+4, pes, 184);
582 if ((ret=p2ts->cb(p2ts->priv, buf)))
589 buf[3]=0x30|((p2ts->cc++)&0x0f);
594 memset(buf+6, 0xff, rest-1);
597 memcpy(buf+5+rest, pes, len);
598 return p2ts->cb(p2ts->priv, buf);