MythTV  master
mpg_common.cpp
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 <cstdio>
30 #include <cstring>
31 #include "element.h"
32 #include "pes.h"
33 #include "ts.h"
34 
36 
37 void show_buf(uint8_t *buf, int length)
38 {
39  QString buffer;
40 
41  for (int i=0; i<length; i+=16){
42  int j = 0;
43  for (j=0; j < 8 && j+i<length; j++)
44  buffer += QString("0x%1 ").arg(buf[i+j],16,2,QChar('0'));
45  for (int r=j; r<8; r++)
46  buffer += " ";
47 
48  buffer += " ";
49 
50  for (j=8; j < 16 && j+i<length; j++)
51  buffer += QString("0x%1 ").arg(buf[i+j],16,2,QChar('0'));
52  for (int r=j; r<16; r++)
53  buffer += " ";
54 
55  for (j=0; j < 16 && j+i<length; j++){
56  switch(buf[i+j]){
57  case '0'...'Z':
58  case 'a'...'z':
59  buffer += QString(QChar(buf[i+j]));
60  break;
61  default:
62  buffer += ".";
63  }
64  }
65  LOG(VB_GENERAL, LOG_INFO, buffer);
66  }
67 }
68 
69 
70 
71 int find_mpg_header(uint8_t head, const uint8_t *buf, int length)
72 {
73 
74  int c = 0;
75  int found=0;
76 
77  if (length <0) return -1;
78 
79  while (found < 4 && c < length){
80  switch(found){
81 
82  case 0:
83  if (buf[c] == 0x00) found = 1;
84  break;
85 
86  case 1:
87  if (buf[c] == 0x00) found = 2;
88  else found = 0;
89  break;
90 
91  case 2:
92  if (buf[c] == 0x01) found = 3;
93  else if (buf[c] != 0x00) found = 0;
94  break;
95 
96  case 3:
97  if (buf[c] == head) return c-3;
98  else found = 0;
99  break;
100  }
101  c++;
102  }
103  return -1;
104 }
105 
106 
107 int find_any_header(uint8_t *head, const uint8_t *buf, int length)
108 {
109 
110  int c = 0;
111  int found=0;
112 
113  if (length <0) return -1;
114 
115  while (found < 4 && c < length){
116  switch(found){
117 
118  case 0:
119  if (buf[c] == 0x00) found = 1;
120  break;
121 
122  case 1:
123  if (buf[c] == 0x00) found = 2;
124  else found = 0;
125  break;
126 
127  case 2:
128  if (buf[c] == 0x01) found = 3;
129  else if (buf[c] != 0x00) found = 0;
130  break;
131 
132  case 3:
133  *head = buf[c];
134  return c-3;
135  break;
136  }
137  c++;
138  }
139  return -1;
140 }
141 
142 
143 uint64_t trans_pts_dts(const uint8_t *pts)
144 {
145  uint64_t wts = ((uint64_t)((pts[0] & 0x0E) << 5) |
146  ((pts[1] & 0xFC) >> 2)) << 24;
147  wts |= (((pts[1] & 0x03) << 6) |
148  ((pts[2] & 0xFC) >> 2)) << 16;
149  wts |= (((pts[2] & 0x02) << 6) |
150  ((pts[3] & 0xFE) >> 1)) << 8;
151  wts |= (((pts[3] & 0x01) << 7) |
152  ((pts[4] & 0xFE) >> 1));
153 
154  wts = wts*300ULL;
155  return wts;
156 }
157 
158 
159 int mring_peek( ringbuffer *rbuf, uint8_t *buf, unsigned int l, uint32_t off)
160 {
161  int c = 0;
162 
163  if (ring_avail(rbuf)+off <= l)
164  return -1;
165 
166  c = ring_peek(rbuf, buf, l, off);
167  return c+off;
168 }
169 
170 
171 int mring_peek( ringbuffer *rbuf, peek_poke_vec &buf, unsigned int l, uint32_t off)
172 {
173  return mring_peek(rbuf, buf.data(), l, off);
174 }
175 
176 
177 int ring_find_mpg_header(ringbuffer *rbuf, uint8_t head, int off, int le)
178 {
179 
180  int c = 0;
181  int found = 0;
182 
183  c = off;
184  while ( c-off < le){
185  uint8_t b = 0;
186  if ( mring_peek(rbuf, &b, 1, c) <0) return -1;
187  switch(found){
188 
189  case 0:
190  if (b == 0x00) found = 1;
191  break;
192 
193  case 1:
194  if (b == 0x00) found = 2;
195  else found = 0;
196  break;
197 
198  case 2:
199  if (b == 0x01) found = 3;
200  else if (b != 0x00) found = 0;
201  break;
202 
203  case 3:
204  if (b == head) return c-3-off;
205  else found = 0;
206  break;
207  }
208  c++;
209 
210  }
211  if (found) return -2;
212  return -1;
213 }
214 
215 
216 int ring_find_any_header(ringbuffer *rbuf, uint8_t *head, int off, int le)
217 {
218 
219  int c = 0;
220  int found =0;
221 
222  c = off;
223  while ( c-off < le){
224  uint8_t b = 0;
225  if ( mring_peek(rbuf, &b, 1, c) <0){
226  return -1;
227  }
228  switch(found){
229 
230  case 0:
231  if (b == 0x00) found = 1;
232  break;
233 
234  case 1:
235  if (b == 0x00) found = 2;
236  else found = 0;
237  break;
238 
239  case 2:
240  if (b == 0x01) found = 3;
241  else if (b != 0x00) found = 0;
242  break;
243 
244  case 3:
245  *head = b;
246  return c-3-off;
247  break;
248  }
249  c++;
250  }
251  if (found) return -2;
252  return -1;
253 }
254 
find_mpg_header
int find_mpg_header(uint8_t head, const uint8_t *buf, int length)
Definition: mpg_common.cpp:71
pes.h
ring_peek
int ring_peek(ringbuffer *rbuf, uint8_t *data, unsigned int count, uint32_t off)
Definition: ringbuffer.cpp:124
ringbuffer
Definition: ringbuffer.h:39
find_any_header
int find_any_header(uint8_t *head, const uint8_t *buf, int length)
Definition: mpg_common.cpp:107
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:39
mythlogging.h
mring_peek
int mring_peek(ringbuffer *rbuf, uint8_t *buf, unsigned int l, uint32_t off)
Definition: mpg_common.cpp:159
trans_pts_dts
uint64_t trans_pts_dts(const uint8_t *pts)
Definition: mpg_common.cpp:143
element.h
ring_find_mpg_header
int ring_find_mpg_header(ringbuffer *rbuf, uint8_t head, int off, int le)
Definition: mpg_common.cpp:177
ring_find_any_header
int ring_find_any_header(ringbuffer *rbuf, uint8_t *head, int off, int le)
Definition: mpg_common.cpp:216
ring_avail
static unsigned int ring_avail(ringbuffer *rbuf)
Definition: ringbuffer.h:108
mpeg::chrono::pts
std::chrono::duration< CHRONO_TYPE, std::ratio< 1, 90000 > > pts
Definition: mythchrono.h:55
show_buf
void show_buf(uint8_t *buf, int length)
Definition: mpg_common.cpp:37
peek_poke_vec
std::vector< uint8_t > peek_poke_vec
Definition: ringbuffer.h:35
ts.h