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  uint8_t buffer[100];
40  uint8_t temp[8];
41  buffer[0] = '\0';
42 
43  for (int i=0; i<length; i+=16){
44  int j = 0;
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 (int 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 (int 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 = ((uint64_t)((pts[0] & 0x0E) << 5) |
155  ((pts[1] & 0xFC) >> 2)) << 24;
156  wts |= (((pts[1] & 0x03) << 6) |
157  ((pts[2] & 0xFC) >> 2)) << 16;
158  wts |= (((pts[2] & 0x02) << 6) |
159  ((pts[3] & 0xFE) >> 1)) << 8;
160  wts |= (((pts[3] & 0x01) << 7) |
161  ((pts[4] & 0xFE) >> 1));
162 
163  wts = wts*300ULL;
164  return wts;
165 }
166 
167 
168 int mring_peek( ringbuffer *rbuf, uint8_t *buf, unsigned int l, uint32_t off)
169 {
170  int c = 0;
171 
172  if (ring_avail(rbuf)+off <= l)
173  return -1;
174 
175  c = ring_peek(rbuf, buf, l, off);
176  return c+off;
177 }
178 
179 
180 
181 int ring_find_mpg_header(ringbuffer *rbuf, uint8_t head, int off, int le)
182 {
183 
184  int c = 0;
185  int found = 0;
186 
187  c = off;
188  while ( c-off < le){
189  uint8_t b = 0;
190  if ( mring_peek(rbuf, &b, 1, c) <0) return -1;
191  switch(found){
192 
193  case 0:
194  if (b == 0x00) found = 1;
195  break;
196 
197  case 1:
198  if (b == 0x00) found = 2;
199  else found = 0;
200  break;
201 
202  case 2:
203  if (b == 0x01) found = 3;
204  else if (b != 0x00) found = 0;
205  break;
206 
207  case 3:
208  if (b == head) return c-3-off;
209  else found = 0;
210  break;
211  }
212  c++;
213 
214  }
215  if (found) return -2;
216  return -1;
217 }
218 
219 
220 int ring_find_any_header(ringbuffer *rbuf, uint8_t *head, int off, int le)
221 {
222 
223  int c = 0;
224  int found =0;
225 
226  c = off;
227  while ( c-off < le){
228  uint8_t b = 0;
229  if ( mring_peek(rbuf, &b, 1, c) <0){
230  return -1;
231  }
232  switch(found){
233 
234  case 0:
235  if (b == 0x00) found = 1;
236  break;
237 
238  case 1:
239  if (b == 0x00) found = 2;
240  else found = 0;
241  break;
242 
243  case 2:
244  if (b == 0x01) found = 3;
245  else if (b != 0x00) found = 0;
246  break;
247 
248  case 3:
249  *head = b;
250  return c-3-off;
251  break;
252  }
253  c++;
254  }
255  if (found) return -2;
256  return -1;
257 }
258 
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:168
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:181
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:220
int ring_peek(ringbuffer *rbuf, uint8_t *data, unsigned int count, uint32_t off)
Definition: ringbuffer.c:121
void show_buf(uint8_t *buf, int length)
Definition: mpg_common.c:37
#define LOG(_MASK_, _LEVEL_, _STRING_)
Definition: mythlogging.h:41