9610
|
1 /*
|
|
2 * Demultiplexer for MPEG2 Transport Streams.
|
|
3 *
|
|
4 * For the purposes of playing video, we make some assumptions about the
|
|
5 * kinds of TS we have to process. The most important simplification is to
|
|
6 * assume that the TS contains a single program (SPTS) because this then
|
|
7 * allows significant simplifications to be made in processing PATs.
|
10014
|
8 *
|
9610
|
9 * WARNING: Quite a hack was required in order to get files by MultiDec played back correctly.
|
|
10 * If it breaks anything else, just comment out the "#define DEMUX_PVA_MULTIDEC_HACK" below
|
|
11 * and it will not be compiled in.
|
|
12 *
|
|
13 * Feedback is appreciated.
|
|
14 *
|
|
15 * written by Matteo Giani
|
10014
|
16 * based on FFmpeg (libavformat) sources
|
9610
|
17 *
|
10014
|
18 *
|
|
19 * This file is free software; you can redistribute it and/or
|
9610
|
20 * modify it under the terms of the GNU Lesser General Public
|
|
21 * License as published by the Free Software Foundation; either
|
|
22 * version 2 of the License, or (at your option) any later version.
|
|
23 *
|
10014
|
24 * This file is distributed in the hope that it will be useful,
|
9610
|
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
27 * Lesser General Public License for more details.
|
|
28 *
|
|
29 * You should have received a copy of the GNU Lesser General Public
|
|
30 * License along with this library; if not, write to the Free Software
|
|
31 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
32 */
|
|
33
|
|
34
|
|
35 #include <stdio.h>
|
|
36 #include <stdlib.h>
|
|
37 #include <string.h>
|
|
38
|
|
39 #include "config.h"
|
|
40 #include "mp_msg.h"
|
|
41 #include "help_mp.h"
|
|
42
|
|
43 #include "stream.h"
|
|
44 #include "demuxer.h"
|
|
45 #include "stheader.h"
|
|
46
|
|
47 #include "bswap.h"
|
|
48
|
|
49
|
|
50
|
|
51 #define TS_FEC_PACKET_SIZE 204
|
|
52 #define TS_PACKET_SIZE 188
|
|
53 #define NB_PID_MAX 8192
|
|
54
|
|
55 #define MAX_HEADER_SIZE 6 /* enough for PES header + length */
|
|
56 #define MAX_PROBE_SIZE 1000000
|
10014
|
57 #define NUM_CONSECUTIVE_TS_PACKETS 32
|
9610
|
58
|
|
59
|
|
60 int ts_fastparse = 0;
|
|
61
|
|
62 typedef enum
|
|
63 {
|
|
64 UNKNOWN = -1,
|
|
65 VIDEO_MPEG2 = 0x10000002,
|
|
66 AUDIO_MP2 = 0x50,
|
|
67 AUDIO_A52 = 0x2000,
|
10014
|
68 AUDIO_LPCM_BE = 0x10001
|
|
69 /*,
|
9610
|
70 SPU_DVD = 0x3000000,
|
|
71 SPU_DVB = 0x3000001,
|
10014
|
72 */
|
9610
|
73 } es_stream_type_t;
|
|
74
|
|
75
|
|
76 typedef struct {
|
|
77 int size;
|
|
78 unsigned char *start;
|
10014
|
79 uint16_t payload_size;
|
9610
|
80 es_stream_type_t type;
|
10014
|
81 float pts, last_pts;
|
9610
|
82 int pid;
|
10014
|
83 int last_cc; // last cc code (-1 if first packet)
|
|
84 } ES_stream_t;
|
|
85
|
9610
|
86
|
10014
|
87 typedef struct MpegTSContext {
|
|
88 int packet_size; // raw packet size, including FEC if present e.g. 188 bytes
|
|
89 ES_stream_t *pids[NB_PID_MAX];
|
|
90 } MpegTSContext;
|
9610
|
91
|
10014
|
92
|
|
93 typedef struct {
|
|
94 MpegTSContext ts;
|
|
95 } ts_priv_t;
|
|
96
|
9610
|
97
|
|
98
|
|
99 static uint8_t get_packet_size(const unsigned char *buf, int size)
|
|
100 {
|
|
101 int i;
|
|
102
|
|
103 if (size < (TS_FEC_PACKET_SIZE * NUM_CONSECUTIVE_TS_PACKETS))
|
|
104 return 0;
|
|
105
|
|
106 for(i=0; i<NUM_CONSECUTIVE_TS_PACKETS; i++)
|
|
107 {
|
|
108 if (buf[i * TS_PACKET_SIZE] != 0x47)
|
|
109 {
|
|
110 mp_msg(MSGT_DEMUX, MSGL_DBG2, "GET_PACKET_SIZE, pos %d, char: %2x\n", i, buf[i * TS_PACKET_SIZE]);
|
|
111 goto try_fec;
|
|
112 }
|
|
113 }
|
|
114 return TS_PACKET_SIZE;
|
|
115
|
|
116 try_fec:
|
|
117 for(i=0; i<NUM_CONSECUTIVE_TS_PACKETS; i++)
|
|
118 {
|
|
119 if (buf[i * TS_FEC_PACKET_SIZE] != 0x47)
|
|
120 return 0;
|
|
121 }
|
|
122 return TS_FEC_PACKET_SIZE;
|
|
123 }
|
|
124
|
|
125
|
|
126
|
|
127 int ts_check_file(demuxer_t * demuxer)
|
|
128 {
|
|
129 const int buf_size = (TS_FEC_PACKET_SIZE * NUM_CONSECUTIVE_TS_PACKETS);
|
10014
|
130 unsigned char buf[buf_size], c, done = 0, *ptr;
|
|
131 uint32_t _read, i=1, count = 0;
|
|
132 int cc[NB_PID_MAX], last_cc[NB_PID_MAX], pid, cc_ok;
|
9610
|
133 uint8_t size = 0;
|
|
134 off_t pos = 0;
|
10014
|
135
|
9610
|
136 mp_msg(MSGT_DEMUX, MSGL_V, "************Checking for TS************\n");
|
|
137
|
|
138 while(! done)
|
|
139 {
|
|
140 while(((c=stream_read_char(demuxer->stream)) != 0x47)
|
10014
|
141 && (i < MAX_PROBE_SIZE)
|
|
142 && ! demuxer->stream->eof
|
9610
|
143 ) i++;
|
|
144
|
|
145 if(c != 0x47)
|
|
146 {
|
10014
|
147 mp_msg(MSGT_DEMUX, MSGL_V, "NOT A TS FILE1\n");
|
|
148 done = 1;
|
|
149 continue;
|
9610
|
150 }
|
|
151
|
|
152 pos = stream_tell(demuxer->stream) - 1;
|
|
153 buf[0] = c;
|
|
154 _read = stream_read(demuxer->stream, &buf[1], buf_size-1);
|
|
155
|
|
156 if(_read < buf_size-1)
|
|
157 {
|
10014
|
158 mp_msg(MSGT_DEMUX, MSGL_V, "COULDN'T READ ENOUGH DATA, EXITING TS_CHECK\n");
|
|
159 stream_reset(demuxer->stream);
|
|
160 return 0;
|
9610
|
161 }
|
|
162
|
|
163 size = get_packet_size(buf, buf_size);
|
|
164 if(size)
|
10014
|
165 done = 1;
|
|
166 }
|
|
167
|
|
168 mp_msg(MSGT_DEMUX, MSGL_V, "TRIED UP TO POSITION %u, FOUND %x, packet_size= %d\n", i, c, size);
|
|
169 stream_seek(demuxer->stream, pos);
|
|
170
|
|
171 //return size;
|
|
172
|
|
173 for(count = 0; count < NB_PID_MAX; count++)
|
|
174 {
|
|
175 cc[count] = last_cc[count] = -1;
|
9610
|
176 }
|
|
177
|
10014
|
178 done = 0;
|
|
179 for(count = 0; count < NUM_CONSECUTIVE_TS_PACKETS; count++)
|
|
180 {
|
|
181 ptr = &(buf[size * count]);
|
|
182 pid = ((ptr[1] & 0x1f) << 8) | ptr[2];
|
|
183 mp_msg(MSGT_DEMUX, MSGL_V, "BUF: %02x %02x %02x %02x, PID %d, SIZE: %d \n",
|
|
184 ptr[0], ptr[1], ptr[2], ptr[3], pid, size);
|
9610
|
185
|
10014
|
186 if(pid == 8191)
|
|
187 continue;
|
9610
|
188
|
10014
|
189 cc[pid] = (ptr[3] & 0xf);
|
|
190 cc_ok = (last_cc[pid] < 0) || ((((last_cc[pid] + 1) & 0x0f) == cc[pid]));
|
|
191 mp_msg(MSGT_DEMUX, MSGL_V, "PID %d, COMPARE CC %d AND LAST_CC %d\n", pid, cc[pid], last_cc[pid]);
|
|
192 if(! cc_ok)
|
|
193 return 0;
|
9610
|
194
|
10014
|
195 last_cc[pid] = cc[pid];
|
|
196 done++;
|
|
197 }
|
9610
|
198
|
10014
|
199 return size;
|
9610
|
200 }
|
|
201
|
|
202
|
|
203 void ts_detect_streams(demuxer_t *demuxer)
|
|
204 {
|
|
205 int video_found = 0, audio_found = 0;
|
|
206 off_t pos=0;
|
10014
|
207 ES_stream_t es;
|
|
208 int *apid, *vpid, *spid, fapid, fvpid, fspid;
|
9610
|
209 unsigned char tmp[TS_FEC_PACKET_SIZE];
|
|
210 sh_video_t *sh_video = demuxer->video->sh;
|
|
211 sh_audio_t *sh_audio = demuxer->audio->sh;
|
|
212
|
|
213 apid = &(demuxer->audio->id);
|
|
214 vpid = &(demuxer->video->id);
|
|
215 spid = &(demuxer->sub->id);
|
|
216
|
|
217
|
|
218 mp_msg(MSGT_DEMUXER, MSGL_INFO, "PROBING UP TO %u\n", MAX_PROBE_SIZE);
|
|
219 while(pos <= MAX_PROBE_SIZE)
|
|
220 {
|
|
221 if(ts_parse(demuxer, &es, tmp))
|
|
222 {
|
|
223 mp_msg(MSGT_DEMUXER, MSGL_V, "TYPE: %x, PID: %d\n", es.type, es.pid);
|
|
224 if(es.type == VIDEO_MPEG2)
|
|
225 {
|
|
226 sh_video->format = VIDEO_MPEG2; //MPEG2 video
|
|
227 if(*vpid == -1)
|
10014
|
228 *vpid = fvpid = es.pid;
|
9610
|
229 video_found = 1;
|
|
230 }
|
|
231
|
|
232 if(es.type == AUDIO_MP2)
|
|
233 {
|
|
234 sh_audio->format = AUDIO_MP2; //MPEG1L2 audio
|
|
235 if(*apid == -1)
|
10014
|
236 *apid = fapid = es.pid;
|
9610
|
237 audio_found = 1;
|
|
238 }
|
|
239
|
|
240 if(es.type == AUDIO_A52)
|
|
241 {
|
10014
|
242 sh_audio->format = AUDIO_A52; //A52 audio
|
9610
|
243 if(*apid == -1)
|
10014
|
244 *apid = fapid = es.pid;
|
|
245 audio_found = 1;
|
|
246 }
|
|
247
|
|
248 if(es.type == AUDIO_LPCM_BE) //LPCM AUDIO
|
|
249 {
|
|
250 sh_audio->format = AUDIO_LPCM_BE;
|
|
251 if(*apid == -1)
|
|
252 *apid = fapid = es.pid;
|
9610
|
253 audio_found = 1;
|
|
254 }
|
|
255
|
|
256 pos = stream_tell(demuxer->stream);
|
|
257 if(video_found && audio_found)
|
|
258 break;
|
|
259 }
|
|
260 }
|
|
261
|
|
262 if(video_found)
|
10014
|
263 mp_msg(MSGT_DEMUXER, MSGL_INFO, "VIDEO MPEG2(pid=%d)...", fvpid);
|
9610
|
264 else
|
|
265 {
|
|
266 *vpid = -2; //WE DIDN'T MATCH ANY VIDEO STREAM, SO WE FORCE THE DEMUXER TO IGNORE VIDEO
|
|
267 mp_msg(MSGT_DEMUXER, MSGL_INFO, "NO VIDEO!\n");
|
|
268 }
|
|
269
|
|
270 if(sh_audio->format == AUDIO_MP2)
|
10014
|
271 mp_msg(MSGT_DEMUXER, MSGL_INFO, "AUDIO MP2(pid=%d)\n", fapid);
|
9610
|
272 else if(sh_audio->format == AUDIO_A52)
|
10014
|
273 mp_msg(MSGT_DEMUXER, MSGL_INFO, "AUDIO A52(pid=%d)\n", fapid);
|
|
274 else if(sh_audio->format == AUDIO_LPCM_BE)
|
|
275 mp_msg(MSGT_DEMUXER, MSGL_INFO, "AUDIO LPCM(pid=%d)\n", fapid);
|
9610
|
276 else
|
|
277 {
|
|
278 *apid = -2; //WE DIDN'T MATCH ANY AUDIO STREAM, SO WE FORCE THE DEMUXER TO IGNORE AUDIO
|
|
279 mp_msg(MSGT_DEMUXER, MSGL_INFO, "NO AUDIO!\n");
|
|
280 }
|
|
281 }
|
|
282
|
|
283
|
10014
|
284
|
|
285 demuxer_t *demux_open_ts(demuxer_t * demuxer)
|
|
286 {
|
|
287 int i;
|
|
288 uint8_t packet_size;
|
|
289 sh_video_t *sh_video;
|
|
290 sh_audio_t *sh_audio;
|
|
291 ts_priv_t * priv = (ts_priv_t*) demuxer->priv;
|
|
292
|
|
293
|
|
294 mp_msg(MSGT_DEMUX, MSGL_V, "DEMUX OPEN, AUDIO_ID: %d, VIDEO_ID: %d, SUBTITLE_ID: %d,\n",
|
|
295 demuxer->audio->id, demuxer->video->id, demuxer->sub->id);
|
|
296
|
|
297
|
|
298 demuxer->type= DEMUXER_TYPE_MPEG_TS;
|
|
299
|
|
300 stream_reset(demuxer->stream);
|
|
301 stream_seek(demuxer->stream, 0);
|
|
302
|
|
303 packet_size = ts_check_file(demuxer);
|
|
304 if(!packet_size)
|
|
305 return NULL;
|
|
306
|
|
307 priv = malloc(sizeof(ts_priv_t));
|
|
308
|
|
309 for(i=0; i < 8192; i++)
|
|
310 priv->ts.pids[i] = NULL;
|
|
311 priv->ts.packet_size = packet_size;
|
|
312
|
|
313
|
|
314 demuxer->priv = priv;
|
|
315
|
|
316 if(demuxer->stream->type != STREAMTYPE_FILE) demuxer->seekable=0;
|
|
317 else demuxer->seekable = 1;
|
|
318
|
|
319
|
|
320 sh_video = new_sh_video(demuxer, 0);
|
|
321 sh_video->ds = demuxer->video;
|
|
322 demuxer->video->sh = sh_video;
|
|
323
|
|
324 sh_audio = new_sh_audio(demuxer, 0);
|
|
325 sh_audio->ds = demuxer->audio;
|
|
326 demuxer->audio->sh = sh_audio;
|
|
327
|
|
328
|
|
329 mp_msg(MSGT_DEMUXER,MSGL_INFO, "Opened TS demuxer...");
|
|
330
|
|
331 if(! ts_fastparse)
|
|
332 ts_detect_streams(demuxer);
|
|
333
|
|
334
|
|
335 /*
|
|
336 demuxer->movi_start = 0;
|
|
337 demuxer->movi_end = demuxer->stream->end_pos;
|
|
338 */
|
|
339
|
|
340
|
|
341 stream_seek(demuxer->stream, 0); //IF IT'S FROM A PIPE IT WILL FAIL, BUT WHO CARES?
|
|
342 return demuxer;
|
|
343 }
|
|
344
|
|
345
|
|
346
|
|
347
|
|
348
|
|
349
|
9610
|
350 void demux_close_ts(demuxer_t * demuxer)
|
|
351 {
|
|
352 if(demuxer->priv)
|
|
353 {
|
|
354 free(demuxer->priv);
|
|
355 demuxer->priv=NULL;
|
|
356 }
|
|
357 }
|
|
358
|
|
359
|
|
360
|
|
361
|
10014
|
362 static int pes_parse2(unsigned char *buf, uint16_t packet_len, int is_start, ES_stream_t *es)
|
9610
|
363 {
|
|
364 unsigned char *p;
|
|
365 uint32_t header_len;
|
|
366 int64_t pts;
|
|
367 uint32_t stream_id;
|
|
368 uint32_t pkt_len;
|
|
369
|
10014
|
370 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2(%X, %d, %d, ): \n", buf, packet_len, is_start);
|
9610
|
371
|
|
372 if(packet_len == 0)
|
|
373 {
|
|
374 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2(,PACKET_LEN = 0, EXIT\n");
|
|
375 return 0;
|
|
376 }
|
|
377
|
|
378 if(packet_len > 184)
|
|
379 {
|
|
380 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2, BUFFER LEN IS TOO BIG: %d, EXIT\n", packet_len);
|
|
381 return 0;
|
|
382 }
|
|
383
|
|
384
|
|
385 p = buf;
|
|
386 pkt_len = packet_len;
|
|
387
|
|
388 if(! is_start)
|
|
389 {
|
10014
|
390 es->pts = 0.0f;
|
9610
|
391 es->start = p;
|
|
392 es->size = packet_len;
|
|
393 return es->size;
|
|
394 }
|
|
395
|
|
396 /* we should have a PES packet here */
|
|
397
|
10014
|
398 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2: HEADER %02x %02x %02x %02x\n", p[0], p[1], p[2], p[3]);
|
9610
|
399 if (p[0] || p[1] || (p[2] != 1))
|
|
400 {
|
|
401 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2: error HEADER %02x %02x %02x (should be 0x000001) \n", p[0], p[1], p[2]);
|
|
402 return 0 ;
|
|
403 }
|
|
404
|
|
405 packet_len -= 6;
|
|
406
|
10014
|
407 es->payload_size = p[4] << 8 | p[5];
|
|
408 if (es->payload_size == 0)
|
|
409 es->payload_size = 65535;
|
9610
|
410
|
|
411 stream_id = p[3];
|
|
412
|
|
413 if(packet_len==0)
|
|
414 return 0;
|
|
415
|
|
416
|
|
417 if (p[7] & 0x80)
|
10014
|
418 { /* pts available */
|
9610
|
419 pts = (int64_t)(p[9] & 0x0E) << 29 ;
|
|
420 pts |= p[10] << 22 ;
|
|
421 pts |= (p[11] & 0xFE) << 14 ;
|
|
422 pts |= p[12] << 7 ;
|
|
423 pts |= (p[13] & 0xFE) >> 1 ;
|
|
424
|
10014
|
425 es->pts = pts / 90000.0f;
|
9610
|
426 }
|
|
427 else
|
10014
|
428 es->pts = 0.0f;
|
9610
|
429
|
|
430 header_len = p[8];
|
|
431
|
|
432 /* sometimes corruption on header_len causes segfault in memcpy below */
|
|
433 if (header_len + 9 > pkt_len)
|
|
434 {
|
|
435 mp_msg(MSGT_DEMUX, MSGL_DBG2, "demux_ts: illegal value for PES_header_data_length (0x%02x)\n", header_len);
|
|
436 return 0;
|
|
437 }
|
|
438
|
|
439 p += header_len + 9;
|
|
440 packet_len -= header_len + 3;
|
|
441
|
|
442 if (stream_id == 0xbd)
|
|
443 {
|
10014
|
444 /* hack : ac3 track */
|
9610
|
445 int track, spu_id;
|
|
446
|
10014
|
447 mp_msg(MSGT_DEMUX, MSGL_V, "pes_parse2: audio buf = %02X %02X %02X %02X %02X %02X %02X %02X, 80: %d\n",
|
|
448 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], p[0] & 0x80);
|
9610
|
449
|
10014
|
450 track = p[0] & 0x0F;
|
|
451 mp_msg(MSGT_DEMUX, MSGL_DBG2, "AC3 TRACK: %d\n", track);
|
|
452
|
|
453
|
9610
|
454 /*
|
|
455 * we check the descriptor tag first because some stations
|
|
456 * do not include any of the ac3 header info in their audio tracks
|
|
457 * these "raw" streams may begin with a byte that looks like a stream type.
|
|
458 */
|
10014
|
459 if( /* ac3 - raw or syncword */
|
|
460 (p[0] == 0x0B && p[1] == 0x77))
|
9610
|
461 {
|
10014
|
462 mp_msg(MSGT_DEMUX, MSGL_DBG2, "AC3 SYNCWORD\n");
|
9610
|
463 es->start = p;
|
|
464 es->size = packet_len;
|
10014
|
465 es->type = AUDIO_A52;
|
9610
|
466 return es->size;
|
|
467 }
|
10014
|
468 /*
|
9610
|
469 else if (//m->descriptor_tag == 0x06 &&
|
|
470 p[0] == 0x20 && p[1] == 0x00)
|
|
471 {
|
10014
|
472 // DVBSUB
|
9610
|
473 long payload_len = ((buf[4] << 8) | buf[5]) - header_len - 3;
|
|
474 es->start = p;
|
|
475 es->size = packet_len;
|
10014
|
476 es->type = SPU_DVB + payload_len;
|
9610
|
477 return es->size;
|
|
478 }
|
10014
|
479
|
9610
|
480 else if ((p[0] & 0xE0) == 0x20)
|
|
481 {
|
|
482 spu_id = (p[0] & 0x1f);
|
|
483 es->start = p+1;
|
|
484 es->size = packet_len-1;
|
10014
|
485 es->type = SPU_DVD + spu_id;
|
9610
|
486 return es->size;
|
|
487 }
|
10014
|
488 */
|
9610
|
489 else if ((p[0] & 0xF0) == 0x80)
|
|
490 {
|
10014
|
491 mp_msg(MSGT_DEMUX, MSGL_DBG2, "AC3 WITH HEADER\n");
|
9610
|
492 es->start = p+4;
|
|
493 es->size = packet_len - 4;
|
10014
|
494 es->type = AUDIO_A52;
|
9610
|
495 return es->size;
|
|
496 }
|
|
497 else if ((p[0]&0xf0) == 0xa0)
|
|
498 {
|
|
499 int pcm_offset;
|
|
500
|
|
501 for (pcm_offset=0; ++pcm_offset < packet_len-1 ; )
|
|
502 {
|
|
503 if (p[pcm_offset] == 0x01 && p[pcm_offset+1] == 0x80)
|
|
504 { /* START */
|
|
505 pcm_offset += 2;
|
|
506 break;
|
|
507 }
|
|
508 }
|
|
509
|
|
510 es->start = p + pcm_offset;
|
|
511 es->size = packet_len - pcm_offset;
|
10014
|
512 es->type = AUDIO_LPCM_BE;
|
9610
|
513 return es->size;
|
|
514 }
|
|
515 }
|
|
516 else if ((stream_id >= 0xbc) && ((stream_id & 0xf0) == 0xe0))
|
|
517 {
|
|
518 es->start = p;
|
|
519 es->size = packet_len;
|
10014
|
520 es->type = VIDEO_MPEG2;
|
9610
|
521 return es->size;
|
|
522 }
|
|
523 else if ((stream_id & 0xe0) == 0xc0)
|
|
524 {
|
|
525 int track;
|
|
526 track = stream_id & 0x1f;
|
|
527 es->start = p;
|
|
528 es->size = packet_len;
|
10014
|
529 es->type = AUDIO_MP2;
|
9610
|
530 return es->size;
|
|
531 }
|
|
532 else
|
|
533 {
|
|
534 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2: unknown packet, id: %x\n", stream_id);
|
|
535 }
|
|
536
|
|
537 return 0;
|
|
538 }
|
|
539
|
|
540
|
|
541
|
|
542
|
|
543 int ts_sync(demuxer_t *demuxer)
|
|
544 {
|
|
545 uint8_t c=0;
|
|
546
|
|
547 mp_msg(MSGT_DEMUX, MSGL_DBG2, "TS_SYNC \n");
|
|
548
|
|
549 while(((c=stream_read_char(demuxer->stream)) != 0x47) && ! demuxer->stream->eof);
|
|
550
|
|
551 if(c == 0x47)
|
|
552 return c;
|
|
553 else
|
|
554 return 0;
|
|
555 }
|
|
556
|
|
557
|
|
558
|
|
559
|
|
560
|
|
561
|
|
562 // 0 = EOF or no stream found
|
|
563 // 1 = successfully read a packet
|
10014
|
564 int ts_parse(demuxer_t * demuxer , ES_stream_t *es, unsigned char *packet)
|
9610
|
565 {
|
10014
|
566 ES_stream_t *tss;
|
9610
|
567 uint8_t done = 0;
|
|
568 uint16_t buf_size, is_start;
|
|
569 int len, pid, cc, cc_ok, afc;
|
|
570 unsigned char *p;
|
10014
|
571 ts_priv_t * priv = (ts_priv_t*) demuxer->priv;
|
|
572
|
9610
|
573
|
|
574 while(! done)
|
|
575 {
|
|
576 if(! ts_sync(demuxer))
|
|
577 {
|
|
578 mp_msg(MSGT_DEMUX, MSGL_V, "TS_FILL_BUFFER: COULDN'T SYNC\n");
|
|
579 return 0;
|
|
580 }
|
|
581
|
10014
|
582 len = stream_read(demuxer->stream, &packet[1], priv->ts.packet_size-1);
|
|
583 if (len != priv->ts.packet_size-1)
|
9610
|
584 return 0;
|
|
585
|
|
586 pid = ((packet[1] & 0x1f) << 8) | packet[2];
|
10014
|
587 tss = priv->ts.pids[pid]; //an ES stream
|
9610
|
588 if(tss == NULL)
|
|
589 {
|
10014
|
590 tss = malloc(sizeof(ES_stream_t));
|
|
591 if(! tss)
|
9610
|
592 continue;
|
10014
|
593 memset(tss, 0, sizeof(ES_stream_t));
|
9610
|
594 tss->pid = pid;
|
|
595 tss->last_cc = -1;
|
|
596 tss->type = UNKNOWN;
|
10014
|
597 priv->ts.pids[pid] = tss;
|
9610
|
598 mp_msg(MSGT_DEMUX, MSGL_DBG2, "new TS pid=%u\n", pid);
|
|
599 }
|
|
600
|
|
601 cc = (packet[3] & 0xf);
|
|
602 cc_ok = (tss->last_cc < 0) || ((((tss->last_cc + 1) & 0x0f) == cc));
|
|
603 if(! cc_ok)
|
|
604 {
|
10014
|
605 mp_msg(MSGT_DEMUX, MSGL_DBG2, "ts_parse: CCCheck NOT OK: %d -> %d\n", tss->last_cc, cc);
|
9610
|
606 }
|
|
607 tss->last_cc = cc;
|
|
608
|
|
609
|
|
610
|
|
611 /* skip adaptation field */
|
|
612 afc = (packet[3] >> 4) & 3;
|
|
613 p = packet + 4;
|
|
614 if (afc == 0) /* reserved value */
|
|
615 continue;
|
|
616 if (afc == 2) /* adaptation field only */
|
|
617 continue;
|
|
618 if (afc == 3)
|
|
619 {
|
|
620 /* skip adapation field */
|
|
621 p += p[0] + 1;
|
|
622 }
|
|
623 /* if past the end of packet, ignore */
|
|
624 if (p >= packet + TS_PACKET_SIZE)
|
|
625 continue;
|
|
626
|
|
627 // PES CONTENT STARTS HERE
|
|
628
|
|
629 buf_size = TS_PACKET_SIZE - (p - packet);
|
|
630
|
|
631 is_start = packet[1] & 0x40;
|
10014
|
632 len = pes_parse2(p, buf_size, is_start, es);
|
|
633
|
|
634 if(len)
|
9610
|
635 {
|
|
636 es->pid = tss->pid;
|
10014
|
637
|
|
638 if(! is_start)
|
9610
|
639 es->type = tss->type;
|
10014
|
640 else
|
|
641 {
|
|
642 tss->type = es->type;
|
|
643 tss->payload_size = es->payload_size;
|
|
644 }
|
|
645
|
|
646 if(es->pts == 0.0f)
|
|
647 es->pts = tss->pts = tss->last_pts;
|
|
648 else
|
|
649 tss->pts = tss->last_pts = es->pts;
|
9610
|
650
|
10014
|
651 mp_msg(MSGT_DEMUX, MSGL_V, "ts_parse, pid=%d, PSIZE: %u, type=%X, start=%X, len=%d\n", es->pid, es->payload_size, es->type, es->start, es->size);
|
9610
|
652
|
|
653 return len;
|
|
654 }
|
|
655 }
|
|
656
|
|
657 return 0;
|
|
658 }
|
|
659
|
|
660
|
10014
|
661
|
9610
|
662 int demux_ts_fill_buffer(demuxer_t * demuxer)
|
|
663 {
|
10014
|
664 ES_stream_t es;
|
9610
|
665 demux_packet_t *dp;
|
|
666 int len;
|
|
667 unsigned char packet[TS_FEC_PACKET_SIZE];
|
|
668 int *apid, *vpid, *spid;
|
|
669
|
|
670 apid = &(demuxer->audio->id);
|
|
671 vpid = &(demuxer->video->id);
|
|
672 spid = &(demuxer->sub->id);
|
|
673
|
10014
|
674 while((len = ts_parse(demuxer, &es, packet)) > 0)
|
9610
|
675 {
|
|
676 mp_msg(MSGT_DEMUX, MSGL_V, "NEW_FILL_BUFFER, NEW_ADD_PACKET(%x, %d) type: %x, PTS: %f\n", es.start, es.size, es.type, es.pts);
|
|
677
|
|
678 if(es.type == VIDEO_MPEG2)
|
|
679 {
|
|
680 if(ts_fastparse)
|
|
681 {
|
|
682 if(*vpid == -2)
|
|
683 continue;
|
|
684
|
|
685 if(*vpid == -1)
|
|
686 *vpid = es.pid;
|
|
687 }
|
|
688
|
|
689 if(*vpid != es.pid)
|
|
690 continue;
|
|
691
|
|
692 dp = new_demux_packet(es.size);
|
|
693 if(! dp || ! dp->buffer)
|
|
694 {
|
|
695 fprintf(stderr, "fill_buffer, NEW_ADD_PACKET(%d) FAILED\n", es.size);
|
|
696 continue;
|
|
697 }
|
|
698 memcpy(dp->buffer, es.start, es.size);
|
|
699 dp->pts = es.pts;
|
|
700 dp->flags = 0;
|
|
701 dp->pos = stream_tell(demuxer->stream);
|
|
702 ds_add_packet(demuxer->video, dp);
|
|
703 mp_msg(MSGT_DEMUX, MSGL_V, "VIDEO pts=%f\n", es.pts);
|
|
704 return len;
|
|
705 }
|
|
706
|
10014
|
707 if((es.type == AUDIO_MP2) || (es.type == AUDIO_A52) || (es.type == AUDIO_LPCM_BE))
|
9610
|
708 {
|
10014
|
709 mp_msg(MSGT_DEMUX, MSGL_V, "FILL_AUDIO %x \n", es.type);
|
|
710
|
9610
|
711 if(ts_fastparse)
|
|
712 {
|
|
713 if(*apid == -2)
|
|
714 continue;
|
|
715
|
|
716 if(*apid == -1)
|
|
717 *apid = es.pid;
|
|
718 }
|
|
719
|
|
720 if(*apid != es.pid)
|
|
721 continue;
|
|
722
|
|
723 dp = new_demux_packet(es.size);
|
|
724 if(! dp || ! dp->buffer)
|
|
725 {
|
|
726 fprintf(stderr, "fill_buffer, NEW_ADD_PACKET(%d) FAILED\n", es.size);
|
|
727 continue;
|
|
728 }
|
|
729 memcpy(dp->buffer, es.start, es.size);
|
|
730 dp->flags = 0;
|
|
731 dp->pts = es.pts;
|
|
732 dp->pos = stream_tell(demuxer->stream);
|
|
733 ds_add_packet(demuxer->audio, dp);
|
|
734 mp_msg(MSGT_DEMUX, MSGL_V, "AUDIO pts=%f\r\n", es.pts);
|
|
735 return len;
|
|
736 }
|
|
737
|
|
738 mp_msg(MSGT_DEMUX, MSGL_V, "SKIP--------\n");
|
|
739 }
|
10014
|
740 return 0;
|
9610
|
741 }
|
|
742
|
|
743
|
|
744
|
|
745
|
|
746 int stringent_ts_sync(demuxer_t *demuxer)
|
|
747 {
|
|
748 ts_priv_t *priv = demuxer->priv;
|
|
749 uint8_t c = 0, done = 0, i, buf[TS_FEC_PACKET_SIZE * NUM_CONSECUTIVE_TS_PACKETS];
|
|
750 off_t pos;
|
|
751
|
10014
|
752 mp_msg(MSGT_DEMUX, MSGL_DBG2, "STRINGENT_TS_SYNC packet_size: %d\n", priv->ts.packet_size);
|
9610
|
753
|
|
754
|
|
755 if(! demuxer->seekable)
|
|
756 return 0;
|
|
757
|
|
758
|
|
759 while(! done)
|
|
760 {
|
|
761 while(((c=stream_read_char(demuxer->stream)) != 0x47) && !demuxer->stream->eof);
|
|
762
|
|
763 if(c != 0x47)
|
|
764 {
|
|
765 stream_reset(demuxer->stream);
|
|
766 return 0;
|
|
767 }
|
|
768
|
|
769 pos = stream_tell(demuxer->stream);
|
|
770 if(pos < 1)
|
|
771 pos = 1;
|
|
772 mp_msg(MSGT_DEMUX, MSGL_DBG2, "dopo il while, pos=%u\n", pos);
|
|
773
|
|
774 done = 1;
|
|
775 buf[0] = c;
|
10014
|
776 stream_read(demuxer->stream, &buf[1], (priv->ts.packet_size * NUM_CONSECUTIVE_TS_PACKETS) - 1);
|
9610
|
777 for(i = 0; i < 5; i++)
|
|
778 {
|
10014
|
779 if (buf[i * priv->ts.packet_size] != 0x47)
|
9610
|
780 done = 0;
|
10014
|
781 mp_msg(MSGT_DEMUX, MSGL_DBG2, "i: %d, char: %x\n", i, buf[i * priv->ts.packet_size]);
|
9610
|
782 }
|
|
783
|
|
784 if(done)
|
|
785 stream_seek(demuxer->stream, pos);
|
|
786 else
|
|
787 stream_seek(demuxer->stream, pos);
|
|
788 }
|
|
789 //stream_seek(demuxer->stream, pos+1);
|
|
790 mp_msg(MSGT_DEMUX, MSGL_DBG2, "STRINGENT_TS_SYNC, STREAM_POS: %lu\n", stream_tell(demuxer->stream));
|
|
791 return 0x47;
|
|
792 }
|
|
793
|
|
794
|
|
795 extern void resync_audio_stream(sh_audio_t *);
|
|
796
|
|
797
|
|
798 int demux_seek_ts(demuxer_t * demuxer, float rel_seek_secs, int flags)
|
|
799 {
|
|
800 int total_bitrate=0;
|
|
801 off_t dest_offset;
|
|
802 ts_priv_t * priv = demuxer->priv;
|
|
803 int a_bps, v_bps;
|
|
804 demux_stream_t *d_audio=demuxer->audio;
|
|
805 demux_stream_t *d_video=demuxer->video;
|
|
806 sh_audio_t *sh_audio=d_audio->sh;
|
|
807 sh_video_t *sh_video=d_video->sh;
|
|
808
|
|
809
|
|
810 /*
|
|
811 * Compute absolute offset inside the stream. Approximate total bitrate with sum of bitrates
|
|
812 * reported by the audio and video codecs. The seek is not accurate because, just like
|
|
813 * with MPEG streams, the bitrate is not constant. Moreover, we do not take into account
|
|
814 * the overhead caused by PVA and PES headers.
|
|
815 * If the calculated absolute offset is negative, seek to the beginning of the file.
|
|
816 */
|
|
817
|
|
818
|
|
819 if(demuxer->audio->id != -2)
|
|
820 {
|
|
821 a_bps = ((sh_audio_t *)demuxer->audio->sh)->i_bps;
|
|
822 total_bitrate += a_bps;
|
|
823 }
|
|
824
|
|
825 if(demuxer->video->id != -2)
|
|
826 {
|
|
827 v_bps = ((sh_video_t *)demuxer->video->sh)->i_bps;
|
|
828 total_bitrate += v_bps;
|
|
829 }
|
|
830
|
|
831 if(! total_bitrate)
|
|
832 {
|
|
833 mp_msg(MSGT_DEMUX, MSGL_V, "SEEK_TS, couldn't determine bitrate, no seek\n");
|
|
834 return 0;
|
|
835 }
|
|
836
|
|
837 dest_offset = stream_tell(demuxer->stream) + rel_seek_secs*total_bitrate;
|
|
838 if(dest_offset < 0) dest_offset = 0;
|
|
839
|
|
840 mp_msg(MSGT_DEMUX, MSGL_V, "SEEK TO: %f, BITRATE: %lu, FINAL_POS: %u \n", rel_seek_secs, total_bitrate, dest_offset);
|
|
841
|
|
842 stream_seek(demuxer->stream, dest_offset);
|
|
843
|
|
844 /*if(!ts_sync(demuxer))
|
|
845 {
|
|
846 mp_msg(MSGT_DEMUX, MSGL_V, "demux_ts: Couldn't seek!\n");
|
|
847 return 0;
|
|
848 }
|
|
849 */
|
|
850
|
|
851 ds_fill_buffer(d_video);
|
|
852 if(sh_audio)
|
|
853 {
|
|
854 ds_fill_buffer(d_audio);
|
|
855 resync_audio_stream(sh_audio);
|
|
856 }
|
|
857
|
|
858 return 1;
|
|
859 }
|
|
860
|
|
861
|
|
862
|
|
863
|
|
864
|
|
865 static int mpegts_read_close(MpegTSContext *ts)
|
|
866 {
|
|
867 int i;
|
|
868 for(i=0;i<NB_PID_MAX;i++)
|
|
869 free(ts->pids[i]);
|
|
870 return 0;
|
|
871 }
|
|
872
|
|
873
|
|
874
|