MythTV  master
mpg_common.c
Go to the documentation of this file.
1 /*
2  * mpg_common.c: COMMON MPEG functions for replex
3  *
4  *
5  * Copyright (C) 2003 Marcus Metzler <mocm@metzlerbros.de>
6  * Metzler Brothers Systementwicklung GbR
7  * Changes to use MythTV logging
8  * Copyright (C) 2011 Gavin Hurlbut <ghurlbut@mythtv.org>
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * General Public License for more details.
20  *
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
25  * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
26  *
27  */
28 
29 #include <stdio.h>
30 #include <string.h>
31 #include "element.h"
32 #include "pes.h"
33 #include "ts.h"
34 
35 #include "mythlogging.h"
36 
37 void show_buf(uint8_t *buf, int length)
38 {
39  int i,j,r;
40  uint8_t buffer[100];
41  uint8_t temp[8];
42  buffer[0] = '\0';
43 
44  for (i=0; i<length; i+=16){
45  for (j=0; j < 8 && j+i<length; j++)
46  {
47  sprintf(temp,"0x%02x ", (int)(buf[i+j]));
48  strcat(buffer, temp);
49  }
50  for (r=j; r<8; r++)
51  strcat(buffer, " ");
52 
53  strcat(buffer, " ");
54 
55  for (j=8; j < 16 && j+i<length; j++)
56  {
57  sprintf(temp, "0x%02x ", (int)(buf[i+j]));
58  strcat(buffer, temp);
59  }
60  for (r=j; r<16; r++)
61  strcat(buffer, " ");
62 
63  for (j=0; j < 16 && j+i<length; j++){
64  switch(buf[i+j]){
65  case '0'...'Z':
66  case 'a'...'z':
67  sprintf(temp, "%c", buf[i+j]);
68  break;
69  default:
70  sprintf(temp, ".");
71  }
72  strcat(buffer, temp);
73  }
74  LOG(VB_GENERAL, LOG_INFO, buffer);
75  }
76 }
77 
78 
79 
80 int find_mpg_header(uint8_t head, const uint8_t *buf, int length)
81 {
82 
83  int c = 0;
84  int found=0;
85 
86  if (length <0) return -1;
87 
88  while (found < 4 && c < length){
89  switch(found){
90 
91  case 0:
92  if (buf[c] == 0x00) found = 1;
93  break;
94 
95  case 1:
96  if (buf[c] == 0x00) found = 2;
97  else found = 0;
98  break;
99 
100  case 2:
101  if (buf[c] == 0x01) found = 3;
102  else if (buf[c] != 0x00) found = 0;
103  break;
104 
105  case 3:
106  if (buf[c] == head) return c-3;
107  else found = 0;
108  break;
109  }
110  c++;
111  }
112  return -1;
113 }
114 
115 
116 int find_any_header(uint8_t *head, const uint8_t *buf, int length)
117 {
118 
119  int c = 0;
120  int found=0;
121 
122  if (length <0) return -1;
123 
124  while (found < 4 && c < length){
125  switch(found){
126 
127  case 0:
128  if (buf[c] == 0x00) found = 1;
129  break;
130 
131  case 1:
132  if (buf[c] == 0x00) found = 2;
133  else found = 0;
134  break;
135 
136  case 2:
137  if (buf[c] == 0x01) found = 3;
138  else if (buf[c] != 0x00) found = 0;
139  break;
140 
141  case 3:
142  *head = buf[c];
143  return c-3;
144  break;
145  }
146  c++;
147  }
148  return -1;
149 }
150 
151 
152 uint64_t trans_pts_dts(const uint8_t *pts)
153 {
154  uint64_t wts;
155 
156  wts = ((uint64_t)((pts[0] & 0x0E) << 5) |
157  ((pts[1] & 0xFC) >> 2)) << 24;
158  wts |= (((pts[1] & 0x03) << 6) |
159  ((pts[2] & 0xFC) >> 2)) << 16;
160  wts |= (((pts[2] & 0x02) << 6) |
161  ((pts[3] & 0xFE) >> 1)) << 8;
162  wts |= (((pts[3] & 0x01) << 7) |
163  ((pts[4] & 0xFE) >> 1));
164 
165  wts = wts*300ULL;
166  return wts;
167 }
168 
169 
170 int mring_peek( ringbuffer *rbuf, uint8_t *buf, unsigned int l, uint32_t off)
171 {
172  int c = 0;
173 
174  if (ring_avail(rbuf)+off <= l)
175  return -1;
176 
177  c = ring_peek(rbuf, buf, l, off);
178  return c+off;
179 }
180 
181 
182 
183 int ring_find_mpg_header(ringbuffer *rbuf, uint8_t head, int off, int le)
184 {
185 
186  int c = 0;
187  int found = 0;
188 
189  c = off;
190  while ( c-off < le){
191  uint8_t b;
192  if ( mring_peek(rbuf, &b, 1, c) <0) return -1;
193  switch(found){
194 
195  case 0:
196  if (b == 0x00) found = 1;
197  break;
198 
199  case 1:
200  if (b == 0x00) found = 2;
201  else found = 0;
202  break;
203 
204  case 2:
205  if (b == 0x01) found = 3;
206  else if (b != 0x00) found = 0;
207  break;
208 
209  case 3:
210  if (b == head) return c-3-off;
211  else found = 0;
212  break;
213  }
214  c++;
215 
216  }
217  if (found) return -2;
218  return -1;
219 }
220 
221 
222 int ring_find_any_header(ringbuffer *rbuf, uint8_t *head, int off, int le)
223 {
224 
225  int c = 0;
226  int found =0;
227 
228  c = off;
229  while ( c-off < le){
230  uint8_t b;
231  if ( mring_peek(rbuf, &b, 1, c) <0){
232  return -1;
233  }
234  switch(found){
235 
236  case 0:
237  if (b == 0x00) found = 1;
238  break;
239 
240  case 1:
241  if (b == 0x00) found = 2;
242  else found = 0;
243  break;
244 
245  case 2:
246  if (b == 0x01) found = 3;
247  else if (b != 0x00) found = 0;
248  break;
249 
250  case 3:
251  *head = b;
252  return c-3-off;
253  break;
254  }
255  c++;
256  }
257  if (found) return -2;
258  return -1;
259 }
260 
unsigned char r
Definition: ParseText.cpp:329
unsigned char b
Definition: ParseText.cpp:329
int find_mpg_header(uint8_t head, const uint8_t *buf, int length)
Definition: mpg_common.c:80
int mring_peek(ringbuffer *rbuf, uint8_t *buf, unsigned int l, uint32_t off)
Definition: mpg_common.c:170
static unsigned int ring_avail(ringbuffer *rbuf)
int find_any_header(uint8_t *head, const uint8_t *buf, int length)
Definition: mpg_common.c:116
int ring_find_mpg_header(ringbuffer *rbuf, uint8_t head, int off, int le)
Definition: mpg_common.c:183
uint64_t trans_pts_dts(const uint8_t *pts)
Definition: mpg_common.c:152
int ring_find_any_header(ringbuffer *rbuf, uint8_t *head, int off, int le)
Definition: mpg_common.c:222
int ring_peek(ringbuffer *rbuf, uint8_t *data, unsigned int count, uint32_t off)
Definition: ringbuffer.c:123
void show_buf(uint8_t *buf, int length)
Definition: mpg_common.c:37
#define LOG(_MASK_, _LEVEL_, _STRING_)
Definition: mythlogging.h:41