Browse Source

Cleanup ts_{decode,encode}_pts().

Georgi Chorbadzhiyski 8 years ago
parent
commit
6378f28014
3 changed files with 35 additions and 114 deletions
  1. 3 6
      pes.c
  2. 25 106
      tsfuncs.c
  3. 7 2
      tsfuncs.h

+ 3 - 6
pes.c View File

@@ -326,20 +326,17 @@ int ts_pes_parse(struct ts_pes *pes) {
326 326
 		return 0;
327 327
 
328 328
 	if (pes->PTS_flag && !pes->DTS_flag) {
329
-		if (!ts_decode_pts_dts(&data[dpos], 2, &pes->PTS))
330
-			return 0;
329
+		ts_decode_pts_dts(&data[dpos], &pes->PTS);
331 330
 		dpos += 5;
332 331
 		pes->have_pts = 1;
333 332
 	}
334 333
 
335 334
 	if (pes->PTS_flag && pes->DTS_flag) {
336
-		if (!ts_decode_pts_dts(&data[dpos], 3, &pes->PTS))
337
-			return 0;
335
+		ts_decode_pts_dts(&data[dpos], &pes->PTS);
338 336
 		pes->have_pts = 1;
339 337
 		dpos += 5;
340 338
 
341
-		if (!ts_decode_pts_dts(&data[dpos], 1, &pes->DTS))
342
-			return 0;
339
+		ts_decode_pts_dts(&data[dpos], &pes->DTS);
343 340
 		pes->have_dts = 1;
344 341
 		dpos += 5;
345 342
 	}

+ 25 - 106
tsfuncs.c View File

@@ -50,110 +50,32 @@ uint8_t ts_packet_get_payload_offset(uint8_t *ts_packet) {
50 50
 	}
51 51
 }
52 52
 
53
-/*
54
- * Decode a PTS or DTS value.
55
- *
56
- * - `data` is the 5 bytes containing the encoded PTS or DTS value
57
- * - `required_guard` should be 2 for a PTS alone, 3 for a PTS before
58
- *   a DTS, or 1 for a DTS after a PTS
59
- * - `value` is the PTS or DTS value as decoded
60
- *
61
- * Returns 0 if the PTS/DTS value is decoded successfully, 1 if an error occurs
62
- */
63
-int ts_decode_pts_dts(uint8_t *data, int required_guard, uint64_t *value) {
64
-  uint64_t      pts1,pts2,pts3;
65
-  int           marker;
66
-  char         *what;
67
-  int           guard = (data[0] & 0xF0) >> 4;
68
-
69
-  // Rather than try to use casts to make the arithmetic come out right on both
70
-  // Linux-with-gcc (old-style C rules) and Windows-with-VisualC++ (C99 rules),
71
-  // it's simpler just to use intermediates that won't get cast to "int".
72
-  unsigned int  data0 = data[0];
73
-  unsigned int  data1 = data[1];
74
-  unsigned int  data2 = data[2];
75
-  unsigned int  data3 = data[3];
76
-  unsigned int  data4 = data[4];
77
-
78
-  switch (required_guard) {
79
-    case 2:  what = "PTS"; break;  // standalone
80
-    case 3:  what = "PTS"; break;  // before a DTS
81
-    case 1:  what = "DTS"; break;  // always after a PTS
82
-    default: what = "???"; break;
83
-  }
84
-
85
-  if (guard != required_guard)
86
-  {
87
-    ts_LOGf("!!! decode_pts_dts(), Guard bits at start of %s data are %x, not %x\n", what, guard, required_guard);
88
-  }
89
-
90
-  pts1 = (data0 & 0x0E) >> 1;
91
-  marker = data0 & 0x01;
92
-  if (marker != 1)
93
-  {
94
-    ts_LOGf("!!! decode_pts_dts(), First %s marker is not 1\n",what);
95
-    return 0;
96
-  }
97
-
98
-  pts2 = (data1 << 7) | ((data2 & 0xFE) >> 1);
99
-  marker = data2 & 0x01;
100
-  if (marker != 1)
101
-  {
102
-    ts_LOGf("!!! decode_pts_dts(), Second %s marker is not 1\n",what);
103
-    return 0;
104
-  }
105
-
106
-  pts3 = (data3 << 7) | ((data4 & 0xFE) >> 1);
107
-  marker = data4 & 0x01;
108
-  if (marker != 1)
109
-  {
110
-    ts_LOGf("!!! decode_pts_dts(), Third %s marker is not 1\n",what);
111
-    return 0;
112
-  }
113
-
114
-  *value = (pts1 << 30) | (pts2 << 15) | pts3;
115
-  return 1;
53
+void ts_decode_pts_dts(uint8_t *data, uint64_t *value) {
54
+	uint64_t pts1 = ((unsigned int)data[0] & 0x0E) >> 1;
55
+	uint64_t pts2 = ((unsigned int)data[1] << 7) | (((unsigned int)data[2] & 0xFE) >> 1);
56
+	uint64_t pts3 = ((unsigned int)data[3] << 7) | (((unsigned int)data[4] & 0xFE) >> 1);
57
+	*value = (pts1 << 30) | (pts2 << 15) | pts3;
116 58
 }
117 59
 
118 60
 /*
119
- * Encode a PTS or DTS.
120
- *
121
- * - `data` is the array of 5 bytes into which to encode the PTS/DTS
122
- * - `guard_bits` are the required guard bits: 2 for a PTS alone, 3 for
123
- *   a PTS before a DTS, or 1 for a DTS after a PTS
124
- * - `value` is the PTS or DTS value to be encoded
61
+ * guard 2 == pts
62
+ * guard 3 == pts before dts
63
+ * guard 1 == dts
125 64
  */
126
-void ts_encode_pts_dts(uint8_t *data, int guard_bits, uint64_t value) {
127
-  int   pts1,pts2,pts3;
128
-
129
-#define MAX_PTS_VALUE 0x1FFFFFFFFLL
130
-
131
-  if (value > MAX_PTS_VALUE)
132
-  {
133
-    char        *what;
134
-    uint64_t     temp = value;
135
-    while (temp > MAX_PTS_VALUE)
136
-      temp -= MAX_PTS_VALUE;
137
-    switch (guard_bits)
138
-    {
139
-    case 2:  what = "PTS alone"; break;
140
-    case 3:  what = "PTS before DTS"; break;
141
-    case 1:  what = "DTS after PTS"; break;
142
-    default: what = "PTS/DTS/???"; break;
143
-    }
144
-    ts_LOGf("!!! value %llu for %s is more than %llu - reduced to %llu\n",value,what,MAX_PTS_VALUE,temp);
145
-    value = temp;
146
-  }
147
-
148
-  pts1 = (int)((value >> 30) & 0x07);
149
-  pts2 = (int)((value >> 15) & 0x7FFF);
150
-  pts3 = (int)( value        & 0x7FFF);
151
-
152
-  data[0] =  (guard_bits << 4) | (pts1 << 1) | 0x01;
153
-  data[1] =  (pts2 & 0x7F80) >> 7;
154
-  data[2] = ((pts2 & 0x007F) << 1) | 0x01;
155
-  data[3] =  (pts3 & 0x7F80) >> 7;
156
-  data[4] = ((pts3 & 0x007F) << 1) | 0x01;
65
+void ts_encode_pts_dts(uint8_t *data, int guard, uint64_t value) {
66
+	#define MAX_PTS_VALUE 0x1FFFFFFFFLL
67
+	while (value > MAX_PTS_VALUE)
68
+		value -= MAX_PTS_VALUE;
69
+
70
+	unsigned int pts1 = (unsigned int)((value >> 30) & 0x07);
71
+	unsigned int pts2 = (unsigned int)((value >> 15) & 0x7FFF);
72
+	unsigned int pts3 = (unsigned int)( value        & 0x7FFF);
73
+
74
+	data[0] =  (guard << 4) | (pts1 << 1) | 0x01;
75
+	data[1] =  (pts2 & 0x7F80) >> 7;
76
+	data[2] = ((pts2 & 0x007F) << 1) | 0x01;
77
+	data[3] =  (pts3 & 0x7F80) >> 7;
78
+	data[4] = ((pts3 & 0x007F) << 1) | 0x01;
157 79
 }
158 80
 
159 81
 // Return 0 on failure
@@ -209,14 +131,11 @@ int ts_packet_has_pts_dts(uint8_t *ts_packet, uint64_t *pts, uint64_t *dts) {
209 131
 
210 132
 	if (pts_flag && !dts_flag) {
211 133
 		if (data + 14 >= data_end) goto ERR;
212
-		if (!ts_decode_pts_dts(&data[9], 2, pts))
213
-			goto ERR;
134
+		ts_decode_pts_dts(&data[9], pts);
214 135
 	} else if (pts_flag && dts_flag) {
215 136
 		if (data + 19 >= data_end) goto ERR;
216
-		if (!ts_decode_pts_dts(&data[9], 3, pts))
217
-			goto ERR;
218
-		if (!ts_decode_pts_dts(&data[14], 1, dts))
219
-			goto ERR;
137
+		ts_decode_pts_dts(&data[9], pts);
138
+		ts_decode_pts_dts(&data[14], dts);
220 139
 	}
221 140
 	return 1;
222 141
 

+ 7 - 2
tsfuncs.h View File

@@ -85,8 +85,13 @@ uint64_t		ts_packet_get_pcr		(uint8_t *ts_packet);
85 85
 void			ts_packet_set_pcr_ex	(uint8_t *ts_packet, uint64_t pcr_base, uint16_t pcr_ext);
86 86
 void			ts_packet_set_pcr		(uint8_t *ts_packet, uint64_t pcr);
87 87
 
88
-void			ts_encode_pts_dts		(uint8_t *data, int guard_bits, uint64_t value);
89
-int				ts_decode_pts_dts		(uint8_t *data, int required_guard, uint64_t *value);
88
+/*
89
+ * guard 2 == pts
90
+ * guard 3 == pts before dts
91
+ * guard 1 == dts
92
+ */
93
+void			ts_encode_pts_dts		(uint8_t *data, int guard, uint64_t value);
94
+void			ts_decode_pts_dts		(uint8_t *data, uint64_t *value);
90 95
 
91 96
 int				ts_packet_has_pes		(uint8_t *ts_packet, uint16_t *pes_packet_len);
92 97
 int				ts_packet_has_pts_dts	(uint8_t *ts_packet, uint64_t *pts, uint64_t *dts);