ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / media / dvb / dvb-core / dvb_filter.c
1 #include <linux/kernel.h>
2 #include <linux/module.h>
3 #include <linux/string.h>
4 #include "dvb_filter.h"
5
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}};
10
11 u32 freq[4] = {441, 480, 320, 0};
12
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};
16
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}}; 
25
26
27
28 #if 0
29 static void setup_ts2pes(ipack *pa, ipack *pv, u16 *pida, u16 *pidv, 
30                   void (*pes_write)(u8 *buf, int count, void *data),
31                   void *priv)
32 {
33         dvb_filter_ipack_init(pa, IPACKS, pes_write);
34         dvb_filter_ipack_init(pv, IPACKS, pes_write);
35         pa->pid = pida;
36         pv->pid = pidv;
37         pa->data = priv;
38         pv->data = priv;
39 }
40 #endif
41
42 #if 0
43 static void ts_to_pes(ipack *p, u8 *buf) // don't need count (=188)
44 {
45         u8 off = 0;
46
47         if (!buf || !p ){
48                 printk("NULL POINTER IDIOT\n");
49                 return;
50         }
51         if (buf[1]&PAY_START) {
52                 if (p->plength == MMAX_PLENGTH-6 && p->found>6){
53                         p->plength = p->found-6;
54                         p->found = 0;
55                         send_ipack(p);
56                         dvb_filter_ipack_reset(p);
57                 }
58         }
59         if (buf[3] & ADAPT_FIELD) {  // adaptation field?
60                 off = buf[4] + 1;
61                 if (off+4 > 187) return;
62         }
63         dvb_filter_instant_repack(buf+4+off, TS_SIZE-4-off, p);
64 }
65 #endif
66
67 #if 0
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)
70 {
71         u8 pct;
72
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]);
77
78         pct = ( headr[1] >> 2 ) & 0x07;
79         pic->picture_coding_type[field] = pct;
80         if (pr) {
81                 switch(pct){
82                         case I_FRAME:
83                                 printk( "  I-FRAME");
84                                 break;
85                         case B_FRAME:
86                                 printk( "  B-FRAME");
87                                 break;
88                         case P_FRAME:
89                                 printk( "  P-FRAME");
90                                 break;
91                 }
92         }
93
94
95         pic->vinfo.vbv_delay  = (( headr[1] >> 5 ) | ( headr[2] << 3) | 
96                                  ( (headr[3] & 0x1F) << 11) ) & 0xffff;
97
98         if (pr) printk( " vbv delay: 0x%04x", pic->vinfo.vbv_delay);
99
100         pic->picture_header_parameter = ( headr[3] & 0xe0 ) | 
101                 ((headr[4] & 0x80) >> 3);
102
103         if ( pct == B_FRAME ){
104                 pic->picture_header_parameter |= ( headr[4] >> 3 ) & 0x0f;
105         }
106         if (pr) printk( " pic head param: 0x%x", 
107                         pic->picture_header_parameter);
108
109         return pct;
110
111 #endif
112
113 #if 0
114 /* needs 4 byte input */
115 static int read_gop_header(u8 *headr, struct mpg_picture *pic, int pr)
116 {
117         if (pr) printk("GOP header: "); 
118
119         pic->time_code  = (( headr[0] << 17 ) | ( headr[1] << 9) | 
120                            ( headr[2] << 1 ) | (headr[3] &0x01)) & 0x1ffffff;
121
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));
125
126         if ( ( headr[3] & 0x40 ) != 0 ){
127                 pic->closed_gop = 1;
128         } else {
129                 pic->closed_gop = 0;
130         }
131         if (pr) printk("closed: %d", pic->closed_gop); 
132
133         if ( ( headr[3] & 0x20 ) != 0 ){
134                 pic->broken_link = 1;
135         } else {
136                 pic->broken_link = 0;
137         }
138         if (pr) printk(" broken: %d\n", pic->broken_link); 
139
140         return 0;
141 }
142 #endif
143
144 #if 0
145 /* needs 8 byte input */
146 static int read_sequence_header(u8 *headr, struct dvb_video_info *vi, int pr)
147 {
148         int sw;
149         int form = -1;
150
151         if (pr) printk("Reading sequence header\n");
152
153         vi->horizontal_size     = ((headr[1] &0xF0) >> 4) | (headr[0] << 4);
154         vi->vertical_size       = ((headr[1] &0x0F) << 8) | (headr[2]);
155     
156         sw = (int)((headr[3]&0xF0) >> 4) ;
157
158         switch( sw ){
159         case 1:
160                 if (pr)
161                         printk("Videostream: ASPECT: 1:1");
162                 vi->aspect_ratio = 100;        
163                 break;
164         case 2:
165                 if (pr)
166                         printk("Videostream: ASPECT: 4:3");
167                 vi->aspect_ratio = 133;        
168                 break;
169         case 3:
170                 if (pr)
171                         printk("Videostream: ASPECT: 16:9");
172                 vi->aspect_ratio = 177;        
173                 break;
174         case 4:
175                 if (pr)
176                         printk("Videostream: ASPECT: 2.21:1");
177                 vi->aspect_ratio = 221;        
178                 break;
179
180         case 5 ... 15:
181                 if (pr)
182                         printk("Videostream: ASPECT: reserved");
183                 vi->aspect_ratio = 0;        
184                 break;
185
186         default:
187                 vi->aspect_ratio = 0;        
188                 return -1;
189         }
190
191         if (pr)
192                 printk("  Size = %dx%d",vi->horizontal_size,vi->vertical_size);
193
194         sw = (int)(headr[3]&0x0F);
195
196         switch ( sw ) {
197         case 1:
198                 if (pr)
199                         printk("  FRate: 23.976 fps");
200                 vi->framerate = 23976;
201                 form = -1;
202                 break;
203         case 2:
204                 if (pr)
205                         printk("  FRate: 24 fps");
206                 vi->framerate = 24000;
207                 form = -1;
208                 break;
209         case 3:
210                 if (pr)
211                         printk("  FRate: 25 fps");
212                 vi->framerate = 25000;
213                 form = VIDEO_MODE_PAL;
214                 break;
215         case 4:
216                 if (pr)
217                         printk("  FRate: 29.97 fps");
218                 vi->framerate = 29970;
219                 form = VIDEO_MODE_NTSC;
220                 break;
221         case 5:
222                 if (pr)
223                         printk("  FRate: 30 fps");
224                 vi->framerate = 30000;
225                 form = VIDEO_MODE_NTSC;
226                 break;
227         case 6:
228                 if (pr)
229                         printk("  FRate: 50 fps");
230                 vi->framerate = 50000;
231                 form = VIDEO_MODE_PAL;
232                 break;
233         case 7:
234                 if (pr)
235                         printk("  FRate: 60 fps");
236                 vi->framerate = 60000;
237                 form = VIDEO_MODE_NTSC;
238                 break;
239         }
240
241         vi->bit_rate = (headr[4] << 10) | (headr[5] << 2) | (headr[6] & 0x03);
242         
243         vi->vbv_buffer_size
244                 = (( headr[6] & 0xF8) >> 3 ) | (( headr[7] & 0x1F )<< 5);
245
246         if (pr){
247                 printk("  BRate: %d Mbit/s",4*(vi->bit_rate)/10000);
248                 printk("  vbvbuffer %d",16*1024*(vi->vbv_buffer_size));
249                 printk("\n");
250         }
251
252         vi->video_format = form;
253
254         return 0;
255 }
256 #endif
257
258
259 #if 0
260 static int get_vinfo(u8 *mbuf, int count, struct dvb_video_info *vi, int pr)
261 {
262         u8 *headr;
263         int found = 0;
264         int c = 0;
265
266         while (found < 4 && c+4 < count){
267                 u8 *b;
268
269                 b = mbuf+c;
270                 if ( b[0] == 0x00 && b[1] == 0x00 && b[2] == 0x01
271                      && b[3] == 0xb3) found = 4;
272                 else {
273                         c++;
274                 }
275         }
276
277         if (! found) return -1;
278         c += 4;
279         if (c+12 >= count) return -1;
280         headr = mbuf+c;
281         if (read_sequence_header(headr, vi, pr) < 0) return -1;
282         vi->off = c-4;
283         return 0;
284 }
285 #endif
286
287
288 #if 0
289 static int get_ainfo(u8 *mbuf, int count, struct dvb_audio_info *ai, int pr)
290 {
291         u8 *headr;
292         int found = 0;
293         int c = 0;
294         int fr = 0;
295
296         while (found < 2 && c < count){
297                 u8 b[2];
298                 memcpy( b, mbuf+c, 2);
299
300                 if ( b[0] == 0xff && (b[1] & 0xf8) == 0xf8)
301                         found = 2;
302                 else {
303                         c++;
304                 }
305         }       
306
307         if (!found) return -1;
308
309         if (c+3 >= count) return -1;
310         headr = mbuf+c;
311
312         ai->layer = (headr[1] & 0x06) >> 1;
313
314         if (pr)
315                 printk("Audiostream: Layer: %d", 4-ai->layer);
316
317
318         ai->bit_rate = bitrates[(3-ai->layer)][(headr[2] >> 4 )]*1000;
319
320         if (pr){
321                 if (ai->bit_rate == 0)
322                         printk("  Bit rate: free");
323                 else if (ai->bit_rate == 0xf)
324                         printk("  BRate: reserved");
325                 else
326                         printk("  BRate: %d kb/s", ai->bit_rate/1000);
327         }
328
329         fr = (headr[2] & 0x0c ) >> 2;
330         ai->frequency = freq[fr]*100;
331         if (pr){
332                 if (ai->frequency == 3)
333                         printk("  Freq: reserved\n");
334                 else
335                         printk("  Freq: %d kHz\n",ai->frequency); 
336                                
337         }
338         ai->off = c;
339         return 0;
340 }
341 #endif
342
343
344 int dvb_filter_get_ac3info(u8 *mbuf, int count, struct dvb_audio_info *ai, int pr)
345 {
346         u8 *headr;
347         int found = 0;
348         int c = 0;
349         u8 frame = 0;
350         int fr = 0;
351         
352         while ( !found  && c < count){
353                 u8 *b = mbuf+c;
354
355                 if ( b[0] == 0x0b &&  b[1] == 0x77 )
356                         found = 1;
357                 else {
358                         c++;
359                 }
360         }       
361
362         if (!found) return -1;
363         if (pr)
364                 printk("Audiostream: AC3");
365
366         ai->off = c;
367         if (c+5 >= count) return -1;
368
369         ai->layer = 0;  // 0 for AC3
370         headr = mbuf+c+2;
371
372         frame = (headr[2]&0x3f);
373         ai->bit_rate = ac3_bitrates[frame >> 1]*1000;
374
375         if (pr)
376                 printk("  BRate: %d kb/s", (int) ai->bit_rate/1000);
377
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);
382
383
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);
388
389
390         return 0;
391 }
392
393
394 #if 0
395 static u8 *skip_pes_header(u8 **bufp)
396 {
397         u8 *inbuf = *bufp;
398         u8 *buf = inbuf;
399         u8 *pts = NULL;
400         int skip = 0;
401
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
405         };
406
407
408         if ((inbuf[6] & 0xc0) == 0x80){ /* mpeg2 */
409                 if (buf[7] & PTS_ONLY)
410                         pts = buf+9;
411                 else pts = NULL;
412                 buf = inbuf + 9 + inbuf[8];
413         } else {        /* mpeg1 */
414                 for (buf = inbuf + 6; *buf == 0xff; buf++)
415                         if (buf == inbuf + 6 + 16) {
416                                 break;
417                         }
418                 if ((*buf & 0xc0) == 0x40)
419                         buf += 2;
420                 skip = mpeg1_skip_table [*buf >> 4];
421                 if (skip == 5 || skip == 10) pts = buf;
422                 else pts = NULL;
423
424                 buf += mpeg1_skip_table [*buf >> 4];
425         }
426
427         *bufp = buf;
428         return pts;
429 }
430 #endif
431
432 #if 0
433 static void initialize_quant_matrix( u32 *matrix )
434 {
435         int i;
436
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;
453
454         for ( i = 16 ; i < 32 ; i++ )
455                 matrix[i] = 0x10101010;
456 }
457 #endif
458
459 #if 0
460 static void initialize_mpg_picture(struct mpg_picture *pic)
461 {
462         int i;
463
464         /* set MPEG1 */
465         pic->mpeg1_flag = 1;
466         pic->profile_and_level = 0x4A ;        /* MP@LL */
467         pic->progressive_sequence = 1;
468         pic->low_delay = 0;
469
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;
474         }
475         pic->last_frame_centre_horizontal_offset = 0;
476         pic->last_frame_centre_vertical_offset = 0;
477
478         pic->picture_display_extension_flag[0] = 0;
479         pic->picture_display_extension_flag[1] = 0;
480         pic->sequence_header_flag = 0;      
481         pic->gop_flag = 0;              
482         pic->sequence_end_flag = 0;     
483 }
484 #endif
485
486 #if 0
487 static void mpg_set_picture_parameter( int32_t field_type, struct mpg_picture *pic )
488 {
489         int16_t last_h_offset;
490         int16_t last_v_offset;
491
492         int16_t *p_h_offset;
493         int16_t *p_v_offset;
494
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;
501         }
502
503         /* Reset flag */
504         pic->picture_display_extension_flag[field_type] = 0;
505
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;
517         } else {
518                 pic->frame_centre_horizontal_offset[3] = last_h_offset;
519                 pic->frame_centre_vertical_offset[3] = last_v_offset;
520         }
521 }
522 #endif
523
524 #if 0
525 static void init_mpg_picture( struct mpg_picture *pic, int chan, int32_t field_type)
526 {
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 );
533         pic->mpeg1_flag = 0;
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]
539                 = 0;
540         pic->pts_flag[field_type] = 0;
541
542         pic->sequence_gop_header = 0;
543         pic->picture_header = 0;
544         pic->sequence_header_flag = 0;
545         pic->gop_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;
550         pic->channel = chan;
551 }
552 #endif
553
554 void dvb_filter_pes2ts_init(struct dvb_filter_pes2ts *p2ts, unsigned short pid, 
555                             dvb_filter_pes2ts_cb_t *cb, void *priv)
556 {
557         unsigned char *buf=p2ts->buf;
558
559         buf[0]=0x47;
560         buf[1]=(pid>>8);
561         buf[2]=pid&0xff;
562         p2ts->cc=0;
563         p2ts->cb=cb;
564         p2ts->priv=priv;
565 }
566
567 int dvb_filter_pes2ts(struct dvb_filter_pes2ts *p2ts, unsigned char *pes,
568                       int len, int payload_start)
569 {
570         unsigned char *buf=p2ts->buf;
571         int ret=0, rest;
572         
573         //len=6+((pes[4]<<8)|pes[5]);
574
575         if (payload_start)
576         buf[1]|=0x40;
577         else
578                 buf[1]&=~0x40;
579         while (len>=184) {
580                 buf[3]=0x10|((p2ts->cc++)&0x0f);
581                 memcpy(buf+4, pes, 184);
582                 if ((ret=p2ts->cb(p2ts->priv, buf)))
583                         return ret;
584                 len-=184; pes+=184;
585                 buf[1]&=~0x40;
586         }
587         if (!len)
588                 return 0;
589         buf[3]=0x30|((p2ts->cc++)&0x0f);
590         rest=183-len;
591         if (rest) {
592                 buf[5]=0x00;
593                 if (rest-1)
594                         memset(buf+6, 0xff, rest-1);
595         }
596         buf[4]=rest;
597         memcpy(buf+5+rest, pes, len);
598         return p2ts->cb(p2ts->priv, buf);
599 }
600