00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include <stdio.h>
00028 #include "element.h"
00029 #include "pes.h"
00030 #include "ts.h"
00031
00032 void show_buf(uint8_t *buf, int length)
00033 {
00034 int i,j,r;
00035
00036 fprintf(stderr,"\n");
00037 for (i=0; i<length; i+=16){
00038 for (j=0; j < 8 && j+i<length; j++)
00039 fprintf(stderr,"0x%02x ", (int)(buf[i+j]));
00040 for (r=j; r<8; r++)
00041 fprintf(stderr," ");
00042
00043 fprintf(stderr," ");
00044
00045 for (j=8; j < 16 && j+i<length; j++)
00046 fprintf(stderr,"0x%02x ", (int)(buf[i+j]));
00047 for (r=j; r<16; r++)
00048 fprintf(stderr," ");
00049
00050 for (j=0; j < 16 && j+i<length; j++){
00051 switch(buf[i+j]){
00052 case '0'...'Z':
00053 case 'a'...'z':
00054 fprintf(stderr,"%c", buf[i+j]);
00055 break;
00056 default:
00057 fprintf(stderr,".");
00058 }
00059 }
00060 fprintf(stderr,"\n");
00061 }
00062 }
00063
00064
00065
00066 int find_mpg_header(uint8_t head, uint8_t *buf, int length)
00067 {
00068
00069 int c = 0;
00070 int found=0;
00071
00072 if (length <0) return -1;
00073
00074 while (found < 4 && c < length){
00075 switch(found){
00076
00077 case 0:
00078 if (buf[c] == 0x00) found = 1;
00079 break;
00080
00081 case 1:
00082 if (buf[c] == 0x00) found = 2;
00083 else found = 0;
00084 break;
00085
00086 case 2:
00087 if (buf[c] == 0x01) found = 3;
00088 else if (buf[c] != 0x00) found = 0;
00089 break;
00090
00091 case 3:
00092 if (buf[c] == head) return c-3;
00093 else found = 0;
00094 break;
00095 }
00096 c++;
00097 }
00098 return -1;
00099 }
00100
00101
00102 int find_any_header(uint8_t *head, uint8_t *buf, int length)
00103 {
00104
00105 int c = 0;
00106 int found=0;
00107
00108 if (length <0) return -1;
00109
00110 while (found < 4 && c < length){
00111 switch(found){
00112
00113 case 0:
00114 if (buf[c] == 0x00) found = 1;
00115 break;
00116
00117 case 1:
00118 if (buf[c] == 0x00) found = 2;
00119 else found = 0;
00120 break;
00121
00122 case 2:
00123 if (buf[c] == 0x01) found = 3;
00124 else if (buf[c] != 0x00) found = 0;
00125 break;
00126
00127 case 3:
00128 *head = buf[c];
00129 return c-3;
00130 break;
00131 }
00132 c++;
00133 }
00134 return -1;
00135 }
00136
00137
00138 uint64_t trans_pts_dts(uint8_t *pts)
00139 {
00140 uint64_t wts;
00141
00142 wts = ((uint64_t)((pts[0] & 0x0E) << 5) |
00143 ((pts[1] & 0xFC) >> 2)) << 24;
00144 wts |= (((pts[1] & 0x03) << 6) |
00145 ((pts[2] & 0xFC) >> 2)) << 16;
00146 wts |= (((pts[2] & 0x02) << 6) |
00147 ((pts[3] & 0xFE) >> 1)) << 8;
00148 wts |= (((pts[3] & 0x01) << 7) |
00149 ((pts[4] & 0xFE) >> 1));
00150
00151 wts = wts*300ULL;
00152 return wts;
00153 }
00154
00155
00156 int mring_peek( ringbuffer *rbuf, uint8_t *buf, unsigned int l, uint32_t off)
00157 {
00158 int c = 0;
00159
00160 if (ring_avail(rbuf)+off <= l)
00161 return -1;
00162
00163 c = ring_peek(rbuf, buf, l, off);
00164 return c+off;
00165 }
00166
00167
00168
00169 int ring_find_mpg_header(ringbuffer *rbuf, uint8_t head, int off, int le)
00170 {
00171
00172 int c = 0;
00173 int found = 0;
00174
00175 c = off;
00176 while ( c-off < le){
00177 uint8_t b;
00178 if ( mring_peek(rbuf, &b, 1, c) <0) return -1;
00179 switch(found){
00180
00181 case 0:
00182 if (b == 0x00) found = 1;
00183 break;
00184
00185 case 1:
00186 if (b == 0x00) found = 2;
00187 else found = 0;
00188 break;
00189
00190 case 2:
00191 if (b == 0x01) found = 3;
00192 else if (b != 0x00) found = 0;
00193 break;
00194
00195 case 3:
00196 if (b == head) return c-3-off;
00197 else found = 0;
00198 break;
00199 }
00200 c++;
00201
00202 }
00203 if (found) return -2;
00204 else return -1;
00205 }
00206
00207
00208 int ring_find_any_header(ringbuffer *rbuf, uint8_t *head, int off, int le)
00209 {
00210
00211 int c = 0;
00212 int found =0;
00213
00214 c = off;
00215 while ( c-off < le){
00216 uint8_t b;
00217 if ( mring_peek(rbuf, &b, 1, c) <0){
00218 return -1;
00219 }
00220 switch(found){
00221
00222 case 0:
00223 if (b == 0x00) found = 1;
00224 break;
00225
00226 case 1:
00227 if (b == 0x00) found = 2;
00228 else found = 0;
00229 break;
00230
00231 case 2:
00232 if (b == 0x01) found = 3;
00233 else if (b != 0x00) found = 0;
00234 break;
00235
00236 case 3:
00237 *head = b;
00238 return c-3-off;
00239 break;
00240 }
00241 c++;
00242 }
00243 if (found) return -2;
00244 else return -1;
00245 }
00246