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