Mercurial > mplayer.hg
annotate libmpdemux/demux_mpg.c @ 14968:b5fb8b0b07c5
initial support for SL packetized data, with certain limitations; partly reworked the tables management for a better code reuse
author | nicodvb |
---|---|
date | Sun, 20 Mar 2005 14:20:59 +0000 |
parents | 658fc109eefc |
children | b7aa70b05d76 |
rev | line source |
---|---|
1 | 1 // MPG/VOB file parser for DEMUXER v2.5 by A'rpi/ESP-team |
2 | |
587
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
554
diff
changeset
|
3 #include <stdio.h> |
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
554
diff
changeset
|
4 #include <stdlib.h> |
1430 | 5 #include <unistd.h> |
587
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
554
diff
changeset
|
6 |
1567 | 7 #include "config.h" |
8 #include "mp_msg.h" | |
1973
5216f108cb4f
all error/warn/info messages moved to help_mp-en.h for translation
arpi
parents:
1735
diff
changeset
|
9 #include "help_mp.h" |
587
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
554
diff
changeset
|
10 |
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
554
diff
changeset
|
11 #include "stream.h" |
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
554
diff
changeset
|
12 #include "demuxer.h" |
1466 | 13 #include "parse_es.h" |
2338 | 14 #include "stheader.h" |
4711 | 15 #include "mp3_hdr.h" |
587
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
554
diff
changeset
|
16 |
524
9105fc95636c
A fast'n'ugly hack to correct DVD VOB playback problems
lgb
parents:
501
diff
changeset
|
17 //#define MAX_PS_PACKETSIZE 2048 |
501 | 18 #define MAX_PS_PACKETSIZE (224*1024) |
19 | |
14923
658fc109eefc
added support for other codecs (mpeg4/h264/aac) in mpeg-ps parsing the PSM
nicodvb
parents:
14502
diff
changeset
|
20 #define UNKNOWN 0 |
658fc109eefc
added support for other codecs (mpeg4/h264/aac) in mpeg-ps parsing the PSM
nicodvb
parents:
14502
diff
changeset
|
21 #define VIDEO_MPEG1 0x10000001 |
658fc109eefc
added support for other codecs (mpeg4/h264/aac) in mpeg-ps parsing the PSM
nicodvb
parents:
14502
diff
changeset
|
22 #define VIDEO_MPEG2 0x10000002 |
658fc109eefc
added support for other codecs (mpeg4/h264/aac) in mpeg-ps parsing the PSM
nicodvb
parents:
14502
diff
changeset
|
23 #define VIDEO_MPEG4 0x10000004 |
658fc109eefc
added support for other codecs (mpeg4/h264/aac) in mpeg-ps parsing the PSM
nicodvb
parents:
14502
diff
changeset
|
24 #define VIDEO_H264 0x10000005 |
658fc109eefc
added support for other codecs (mpeg4/h264/aac) in mpeg-ps parsing the PSM
nicodvb
parents:
14502
diff
changeset
|
25 #define AUDIO_MP2 0x50 |
658fc109eefc
added support for other codecs (mpeg4/h264/aac) in mpeg-ps parsing the PSM
nicodvb
parents:
14502
diff
changeset
|
26 #define AUDIO_A52 0x2000 |
658fc109eefc
added support for other codecs (mpeg4/h264/aac) in mpeg-ps parsing the PSM
nicodvb
parents:
14502
diff
changeset
|
27 #define AUDIO_LPCM_BE 0x10001 |
658fc109eefc
added support for other codecs (mpeg4/h264/aac) in mpeg-ps parsing the PSM
nicodvb
parents:
14502
diff
changeset
|
28 #define AUDIO_AAC mmioFOURCC('M', 'P', '4', 'A') |
658fc109eefc
added support for other codecs (mpeg4/h264/aac) in mpeg-ps parsing the PSM
nicodvb
parents:
14502
diff
changeset
|
29 |
13738
39004f891def
seeking based on the largest timestamp in an mpeg stream
aurel
parents:
13649
diff
changeset
|
30 typedef struct mpg_demuxer { |
39004f891def
seeking based on the largest timestamp in an mpeg stream
aurel
parents:
13649
diff
changeset
|
31 float last_pts; |
39004f891def
seeking based on the largest timestamp in an mpeg stream
aurel
parents:
13649
diff
changeset
|
32 float final_pts; |
39004f891def
seeking based on the largest timestamp in an mpeg stream
aurel
parents:
13649
diff
changeset
|
33 int has_valid_timestamps; |
14923
658fc109eefc
added support for other codecs (mpeg4/h264/aac) in mpeg-ps parsing the PSM
nicodvb
parents:
14502
diff
changeset
|
34 unsigned int es_map[0x40]; //es map of stream types (associated to the pes id) from 0xb0 to 0xef |
13738
39004f891def
seeking based on the largest timestamp in an mpeg stream
aurel
parents:
13649
diff
changeset
|
35 } mpg_demuxer_t; |
39004f891def
seeking based on the largest timestamp in an mpeg stream
aurel
parents:
13649
diff
changeset
|
36 |
547 | 37 static int mpeg_pts_error=0; |
38 | |
14923
658fc109eefc
added support for other codecs (mpeg4/h264/aac) in mpeg-ps parsing the PSM
nicodvb
parents:
14502
diff
changeset
|
39 static int parse_psm(demuxer_t *demux, int len) { |
658fc109eefc
added support for other codecs (mpeg4/h264/aac) in mpeg-ps parsing the PSM
nicodvb
parents:
14502
diff
changeset
|
40 unsigned char c, id, type; |
658fc109eefc
added support for other codecs (mpeg4/h264/aac) in mpeg-ps parsing the PSM
nicodvb
parents:
14502
diff
changeset
|
41 unsigned int plen, prog_len, es_map_len; |
658fc109eefc
added support for other codecs (mpeg4/h264/aac) in mpeg-ps parsing the PSM
nicodvb
parents:
14502
diff
changeset
|
42 mpg_demuxer_t *priv = (mpg_demuxer_t *) demux->priv; |
658fc109eefc
added support for other codecs (mpeg4/h264/aac) in mpeg-ps parsing the PSM
nicodvb
parents:
14502
diff
changeset
|
43 |
658fc109eefc
added support for other codecs (mpeg4/h264/aac) in mpeg-ps parsing the PSM
nicodvb
parents:
14502
diff
changeset
|
44 mp_dbg(MSGT_DEMUX,MSGL_V, "PARSE_PSM, len=%d\n", len); |
658fc109eefc
added support for other codecs (mpeg4/h264/aac) in mpeg-ps parsing the PSM
nicodvb
parents:
14502
diff
changeset
|
45 if(! len) |
658fc109eefc
added support for other codecs (mpeg4/h264/aac) in mpeg-ps parsing the PSM
nicodvb
parents:
14502
diff
changeset
|
46 return 0; |
658fc109eefc
added support for other codecs (mpeg4/h264/aac) in mpeg-ps parsing the PSM
nicodvb
parents:
14502
diff
changeset
|
47 |
658fc109eefc
added support for other codecs (mpeg4/h264/aac) in mpeg-ps parsing the PSM
nicodvb
parents:
14502
diff
changeset
|
48 c = stream_read_char(demux->stream); |
658fc109eefc
added support for other codecs (mpeg4/h264/aac) in mpeg-ps parsing the PSM
nicodvb
parents:
14502
diff
changeset
|
49 if(! (c & 0x80)) { |
658fc109eefc
added support for other codecs (mpeg4/h264/aac) in mpeg-ps parsing the PSM
nicodvb
parents:
14502
diff
changeset
|
50 stream_skip(demux->stream, len - 1); //not yet valid, discard |
658fc109eefc
added support for other codecs (mpeg4/h264/aac) in mpeg-ps parsing the PSM
nicodvb
parents:
14502
diff
changeset
|
51 return 0; |
658fc109eefc
added support for other codecs (mpeg4/h264/aac) in mpeg-ps parsing the PSM
nicodvb
parents:
14502
diff
changeset
|
52 } |
658fc109eefc
added support for other codecs (mpeg4/h264/aac) in mpeg-ps parsing the PSM
nicodvb
parents:
14502
diff
changeset
|
53 stream_skip(demux->stream, 1); |
658fc109eefc
added support for other codecs (mpeg4/h264/aac) in mpeg-ps parsing the PSM
nicodvb
parents:
14502
diff
changeset
|
54 prog_len = stream_read_word(demux->stream); //length of program descriptors |
658fc109eefc
added support for other codecs (mpeg4/h264/aac) in mpeg-ps parsing the PSM
nicodvb
parents:
14502
diff
changeset
|
55 stream_skip(demux->stream, prog_len); //.. that we ignore |
658fc109eefc
added support for other codecs (mpeg4/h264/aac) in mpeg-ps parsing the PSM
nicodvb
parents:
14502
diff
changeset
|
56 es_map_len = stream_read_word(demux->stream); //length of elementary streams map |
658fc109eefc
added support for other codecs (mpeg4/h264/aac) in mpeg-ps parsing the PSM
nicodvb
parents:
14502
diff
changeset
|
57 es_map_len = min(es_map_len, len - prog_len - 8); //sanity check |
658fc109eefc
added support for other codecs (mpeg4/h264/aac) in mpeg-ps parsing the PSM
nicodvb
parents:
14502
diff
changeset
|
58 while(es_map_len > 0) { |
658fc109eefc
added support for other codecs (mpeg4/h264/aac) in mpeg-ps parsing the PSM
nicodvb
parents:
14502
diff
changeset
|
59 type = stream_read_char(demux->stream); |
658fc109eefc
added support for other codecs (mpeg4/h264/aac) in mpeg-ps parsing the PSM
nicodvb
parents:
14502
diff
changeset
|
60 id = stream_read_char(demux->stream); |
658fc109eefc
added support for other codecs (mpeg4/h264/aac) in mpeg-ps parsing the PSM
nicodvb
parents:
14502
diff
changeset
|
61 if(id >= 0xB0 && id <= 0xEF && priv) { |
658fc109eefc
added support for other codecs (mpeg4/h264/aac) in mpeg-ps parsing the PSM
nicodvb
parents:
14502
diff
changeset
|
62 int idoffset = id - 0xB0; |
658fc109eefc
added support for other codecs (mpeg4/h264/aac) in mpeg-ps parsing the PSM
nicodvb
parents:
14502
diff
changeset
|
63 switch(type) { |
658fc109eefc
added support for other codecs (mpeg4/h264/aac) in mpeg-ps parsing the PSM
nicodvb
parents:
14502
diff
changeset
|
64 case 0x1: |
658fc109eefc
added support for other codecs (mpeg4/h264/aac) in mpeg-ps parsing the PSM
nicodvb
parents:
14502
diff
changeset
|
65 priv->es_map[idoffset] = VIDEO_MPEG1; |
658fc109eefc
added support for other codecs (mpeg4/h264/aac) in mpeg-ps parsing the PSM
nicodvb
parents:
14502
diff
changeset
|
66 break; |
658fc109eefc
added support for other codecs (mpeg4/h264/aac) in mpeg-ps parsing the PSM
nicodvb
parents:
14502
diff
changeset
|
67 case 0x2: |
658fc109eefc
added support for other codecs (mpeg4/h264/aac) in mpeg-ps parsing the PSM
nicodvb
parents:
14502
diff
changeset
|
68 priv->es_map[idoffset] = VIDEO_MPEG2; |
658fc109eefc
added support for other codecs (mpeg4/h264/aac) in mpeg-ps parsing the PSM
nicodvb
parents:
14502
diff
changeset
|
69 break; |
658fc109eefc
added support for other codecs (mpeg4/h264/aac) in mpeg-ps parsing the PSM
nicodvb
parents:
14502
diff
changeset
|
70 case 0x3: |
658fc109eefc
added support for other codecs (mpeg4/h264/aac) in mpeg-ps parsing the PSM
nicodvb
parents:
14502
diff
changeset
|
71 case 0x4: |
658fc109eefc
added support for other codecs (mpeg4/h264/aac) in mpeg-ps parsing the PSM
nicodvb
parents:
14502
diff
changeset
|
72 priv->es_map[idoffset] = AUDIO_MP2; |
658fc109eefc
added support for other codecs (mpeg4/h264/aac) in mpeg-ps parsing the PSM
nicodvb
parents:
14502
diff
changeset
|
73 break; |
658fc109eefc
added support for other codecs (mpeg4/h264/aac) in mpeg-ps parsing the PSM
nicodvb
parents:
14502
diff
changeset
|
74 case 0x0f: |
658fc109eefc
added support for other codecs (mpeg4/h264/aac) in mpeg-ps parsing the PSM
nicodvb
parents:
14502
diff
changeset
|
75 case 0x11: |
658fc109eefc
added support for other codecs (mpeg4/h264/aac) in mpeg-ps parsing the PSM
nicodvb
parents:
14502
diff
changeset
|
76 priv->es_map[idoffset] = AUDIO_AAC; |
658fc109eefc
added support for other codecs (mpeg4/h264/aac) in mpeg-ps parsing the PSM
nicodvb
parents:
14502
diff
changeset
|
77 break; |
658fc109eefc
added support for other codecs (mpeg4/h264/aac) in mpeg-ps parsing the PSM
nicodvb
parents:
14502
diff
changeset
|
78 case 0x10: |
658fc109eefc
added support for other codecs (mpeg4/h264/aac) in mpeg-ps parsing the PSM
nicodvb
parents:
14502
diff
changeset
|
79 priv->es_map[idoffset] = VIDEO_MPEG4; |
658fc109eefc
added support for other codecs (mpeg4/h264/aac) in mpeg-ps parsing the PSM
nicodvb
parents:
14502
diff
changeset
|
80 break; |
658fc109eefc
added support for other codecs (mpeg4/h264/aac) in mpeg-ps parsing the PSM
nicodvb
parents:
14502
diff
changeset
|
81 case 0x1b: |
658fc109eefc
added support for other codecs (mpeg4/h264/aac) in mpeg-ps parsing the PSM
nicodvb
parents:
14502
diff
changeset
|
82 priv->es_map[idoffset] = VIDEO_H264; |
658fc109eefc
added support for other codecs (mpeg4/h264/aac) in mpeg-ps parsing the PSM
nicodvb
parents:
14502
diff
changeset
|
83 break; |
658fc109eefc
added support for other codecs (mpeg4/h264/aac) in mpeg-ps parsing the PSM
nicodvb
parents:
14502
diff
changeset
|
84 case 0x81: |
658fc109eefc
added support for other codecs (mpeg4/h264/aac) in mpeg-ps parsing the PSM
nicodvb
parents:
14502
diff
changeset
|
85 priv->es_map[idoffset] = AUDIO_A52; |
658fc109eefc
added support for other codecs (mpeg4/h264/aac) in mpeg-ps parsing the PSM
nicodvb
parents:
14502
diff
changeset
|
86 break; |
658fc109eefc
added support for other codecs (mpeg4/h264/aac) in mpeg-ps parsing the PSM
nicodvb
parents:
14502
diff
changeset
|
87 } |
658fc109eefc
added support for other codecs (mpeg4/h264/aac) in mpeg-ps parsing the PSM
nicodvb
parents:
14502
diff
changeset
|
88 mp_dbg(MSGT_DEMUX,MSGL_V, "PSM ES, id=0x%x, type=%x, stype: %x\n", id, type, priv->es_map[idoffset]); |
658fc109eefc
added support for other codecs (mpeg4/h264/aac) in mpeg-ps parsing the PSM
nicodvb
parents:
14502
diff
changeset
|
89 } |
658fc109eefc
added support for other codecs (mpeg4/h264/aac) in mpeg-ps parsing the PSM
nicodvb
parents:
14502
diff
changeset
|
90 plen = stream_read_word(demux->stream); //length of elementary stream descriptors |
658fc109eefc
added support for other codecs (mpeg4/h264/aac) in mpeg-ps parsing the PSM
nicodvb
parents:
14502
diff
changeset
|
91 plen = min(plen, es_map_len); //sanity check |
658fc109eefc
added support for other codecs (mpeg4/h264/aac) in mpeg-ps parsing the PSM
nicodvb
parents:
14502
diff
changeset
|
92 stream_skip(demux->stream, plen); //skip descriptors for now |
658fc109eefc
added support for other codecs (mpeg4/h264/aac) in mpeg-ps parsing the PSM
nicodvb
parents:
14502
diff
changeset
|
93 es_map_len -= 4 + plen; |
658fc109eefc
added support for other codecs (mpeg4/h264/aac) in mpeg-ps parsing the PSM
nicodvb
parents:
14502
diff
changeset
|
94 } |
658fc109eefc
added support for other codecs (mpeg4/h264/aac) in mpeg-ps parsing the PSM
nicodvb
parents:
14502
diff
changeset
|
95 stream_skip(demux->stream, 4); //skip crc32 |
658fc109eefc
added support for other codecs (mpeg4/h264/aac) in mpeg-ps parsing the PSM
nicodvb
parents:
14502
diff
changeset
|
96 return 1; |
658fc109eefc
added support for other codecs (mpeg4/h264/aac) in mpeg-ps parsing the PSM
nicodvb
parents:
14502
diff
changeset
|
97 } |
658fc109eefc
added support for other codecs (mpeg4/h264/aac) in mpeg-ps parsing the PSM
nicodvb
parents:
14502
diff
changeset
|
98 |
13738
39004f891def
seeking based on the largest timestamp in an mpeg stream
aurel
parents:
13649
diff
changeset
|
99 /// Open an mpg physical stream |
39004f891def
seeking based on the largest timestamp in an mpeg stream
aurel
parents:
13649
diff
changeset
|
100 int demux_mpg_open(demuxer_t* demuxer) { |
39004f891def
seeking based on the largest timestamp in an mpeg stream
aurel
parents:
13649
diff
changeset
|
101 stream_t *s = demuxer->stream; |
39004f891def
seeking based on the largest timestamp in an mpeg stream
aurel
parents:
13649
diff
changeset
|
102 off_t pos = stream_tell(s); |
39004f891def
seeking based on the largest timestamp in an mpeg stream
aurel
parents:
13649
diff
changeset
|
103 off_t end_seq_start = demuxer->movi_end-500000; // 500000 is a wild guess |
39004f891def
seeking based on the largest timestamp in an mpeg stream
aurel
parents:
13649
diff
changeset
|
104 float half_pts = 0.0; |
39004f891def
seeking based on the largest timestamp in an mpeg stream
aurel
parents:
13649
diff
changeset
|
105 mpg_demuxer_t* mpg_d; |
39004f891def
seeking based on the largest timestamp in an mpeg stream
aurel
parents:
13649
diff
changeset
|
106 |
39004f891def
seeking based on the largest timestamp in an mpeg stream
aurel
parents:
13649
diff
changeset
|
107 if (!ds_fill_buffer(demuxer->video)) return 0; |
39004f891def
seeking based on the largest timestamp in an mpeg stream
aurel
parents:
13649
diff
changeset
|
108 mpg_d = (mpg_demuxer_t*)calloc(1,sizeof(mpg_demuxer_t)); |
39004f891def
seeking based on the largest timestamp in an mpeg stream
aurel
parents:
13649
diff
changeset
|
109 demuxer->priv = mpg_d; |
39004f891def
seeking based on the largest timestamp in an mpeg stream
aurel
parents:
13649
diff
changeset
|
110 mpg_d->final_pts = 0.0; |
39004f891def
seeking based on the largest timestamp in an mpeg stream
aurel
parents:
13649
diff
changeset
|
111 mpg_d->has_valid_timestamps = 1; |
39004f891def
seeking based on the largest timestamp in an mpeg stream
aurel
parents:
13649
diff
changeset
|
112 if (demuxer->seekable && stream_tell(demuxer->stream) < end_seq_start) { |
39004f891def
seeking based on the largest timestamp in an mpeg stream
aurel
parents:
13649
diff
changeset
|
113 stream_seek(s,(pos + end_seq_start / 2)); |
39004f891def
seeking based on the largest timestamp in an mpeg stream
aurel
parents:
13649
diff
changeset
|
114 while ((!s->eof) && ds_fill_buffer(demuxer->video) && half_pts == 0.0) { |
39004f891def
seeking based on the largest timestamp in an mpeg stream
aurel
parents:
13649
diff
changeset
|
115 half_pts = mpg_d->last_pts; |
39004f891def
seeking based on the largest timestamp in an mpeg stream
aurel
parents:
13649
diff
changeset
|
116 } |
39004f891def
seeking based on the largest timestamp in an mpeg stream
aurel
parents:
13649
diff
changeset
|
117 stream_seek(s,end_seq_start); |
39004f891def
seeking based on the largest timestamp in an mpeg stream
aurel
parents:
13649
diff
changeset
|
118 while ((!s->eof) && ds_fill_buffer(demuxer->video)) { |
39004f891def
seeking based on the largest timestamp in an mpeg stream
aurel
parents:
13649
diff
changeset
|
119 if (mpg_d->final_pts < mpg_d->last_pts) mpg_d->final_pts = mpg_d->last_pts; |
39004f891def
seeking based on the largest timestamp in an mpeg stream
aurel
parents:
13649
diff
changeset
|
120 } |
39004f891def
seeking based on the largest timestamp in an mpeg stream
aurel
parents:
13649
diff
changeset
|
121 // educated guess about validity of timestamps |
39004f891def
seeking based on the largest timestamp in an mpeg stream
aurel
parents:
13649
diff
changeset
|
122 if (mpg_d->final_pts > 3 * half_pts || mpg_d->final_pts < 1.5 * half_pts) { |
39004f891def
seeking based on the largest timestamp in an mpeg stream
aurel
parents:
13649
diff
changeset
|
123 mpg_d->has_valid_timestamps = 0; |
39004f891def
seeking based on the largest timestamp in an mpeg stream
aurel
parents:
13649
diff
changeset
|
124 } |
39004f891def
seeking based on the largest timestamp in an mpeg stream
aurel
parents:
13649
diff
changeset
|
125 ds_free_packs(demuxer->audio); |
39004f891def
seeking based on the largest timestamp in an mpeg stream
aurel
parents:
13649
diff
changeset
|
126 ds_free_packs(demuxer->video); |
39004f891def
seeking based on the largest timestamp in an mpeg stream
aurel
parents:
13649
diff
changeset
|
127 demuxer->stream->eof=0; // clear eof flag |
39004f891def
seeking based on the largest timestamp in an mpeg stream
aurel
parents:
13649
diff
changeset
|
128 demuxer->video->eof=0; |
39004f891def
seeking based on the largest timestamp in an mpeg stream
aurel
parents:
13649
diff
changeset
|
129 demuxer->audio->eof=0; |
39004f891def
seeking based on the largest timestamp in an mpeg stream
aurel
parents:
13649
diff
changeset
|
130 |
39004f891def
seeking based on the largest timestamp in an mpeg stream
aurel
parents:
13649
diff
changeset
|
131 stream_seek(s,pos); |
39004f891def
seeking based on the largest timestamp in an mpeg stream
aurel
parents:
13649
diff
changeset
|
132 ds_fill_buffer(demuxer->video); |
39004f891def
seeking based on the largest timestamp in an mpeg stream
aurel
parents:
13649
diff
changeset
|
133 } |
39004f891def
seeking based on the largest timestamp in an mpeg stream
aurel
parents:
13649
diff
changeset
|
134 return 1; |
39004f891def
seeking based on the largest timestamp in an mpeg stream
aurel
parents:
13649
diff
changeset
|
135 } |
39004f891def
seeking based on the largest timestamp in an mpeg stream
aurel
parents:
13649
diff
changeset
|
136 |
39004f891def
seeking based on the largest timestamp in an mpeg stream
aurel
parents:
13649
diff
changeset
|
137 void demux_close_mpg(demuxer_t* demuxer) { |
39004f891def
seeking based on the largest timestamp in an mpeg stream
aurel
parents:
13649
diff
changeset
|
138 mpg_demuxer_t* mpg_d = demuxer->priv; |
39004f891def
seeking based on the largest timestamp in an mpeg stream
aurel
parents:
13649
diff
changeset
|
139 if (mpg_d) free(mpg_d); |
39004f891def
seeking based on the largest timestamp in an mpeg stream
aurel
parents:
13649
diff
changeset
|
140 } |
39004f891def
seeking based on the largest timestamp in an mpeg stream
aurel
parents:
13649
diff
changeset
|
141 |
39004f891def
seeking based on the largest timestamp in an mpeg stream
aurel
parents:
13649
diff
changeset
|
142 |
1 | 143 static unsigned int read_mpeg_timestamp(stream_t *s,int c){ |
144 int d,e; | |
145 unsigned int pts; | |
146 d=stream_read_word(s); | |
147 e=stream_read_word(s); | |
547 | 148 if( ((c&1)!=1) || ((d&1)!=1) || ((e&1)!=1) ){ |
149 ++mpeg_pts_error; | |
150 return 0; // invalid pts | |
151 } | |
1 | 152 pts=(((c>>1)&7)<<30)|((d>>1)<<15)|(e>>1); |
1567 | 153 mp_dbg(MSGT_DEMUX,MSGL_DBG3,"{%d}",pts); |
1 | 154 return pts; |
155 } | |
156 | |
157 static int demux_mpg_read_packet(demuxer_t *demux,int id){ | |
158 int d; | |
159 int len; | |
160 unsigned char c=0; | |
161 unsigned int pts=0; | |
162 unsigned int dts=0; | |
163 demux_stream_t *ds=NULL; | |
14923
658fc109eefc
added support for other codecs (mpeg4/h264/aac) in mpeg-ps parsing the PSM
nicodvb
parents:
14502
diff
changeset
|
164 mpg_demuxer_t *priv = (mpg_demuxer_t *) demux->priv; |
1 | 165 |
1567 | 166 mp_dbg(MSGT_DEMUX,MSGL_DBG3,"demux_read_packet: %X\n",id); |
1 | 167 |
536 | 168 // if(id==0x1F0){ |
169 // demux->synced=0; // force resync after 0x1F0 | |
170 // return -1; | |
171 //} | |
501 | 172 |
1 | 173 // if(id==0x1BA) packet_start_pos=stream_tell(demux->stream); |
501 | 174 if(id<0x1BC || id>=0x1F0) return -1; |
1 | 175 if(id==0x1BE) return -1; // padding stream |
176 if(id==0x1BF) return -1; // private2 | |
177 | |
178 len=stream_read_word(demux->stream); | |
1567 | 179 mp_dbg(MSGT_DEMUX,MSGL_DBG3,"PACKET len=%d",len); |
536 | 180 // if(len==62480){ demux->synced=0;return -1;} /* :) */ |
501 | 181 if(len==0 || len>MAX_PS_PACKETSIZE){ |
1567 | 182 mp_dbg(MSGT_DEMUX,MSGL_DBG2,"Invalid PS packet len: %d\n",len); |
501 | 183 return -2; // invalid packet !!!!!! |
184 } | |
1 | 185 |
547 | 186 mpeg_pts_error=0; |
187 | |
14923
658fc109eefc
added support for other codecs (mpeg4/h264/aac) in mpeg-ps parsing the PSM
nicodvb
parents:
14502
diff
changeset
|
188 if(id==0x1BC) { |
658fc109eefc
added support for other codecs (mpeg4/h264/aac) in mpeg-ps parsing the PSM
nicodvb
parents:
14502
diff
changeset
|
189 parse_psm(demux, len); |
658fc109eefc
added support for other codecs (mpeg4/h264/aac) in mpeg-ps parsing the PSM
nicodvb
parents:
14502
diff
changeset
|
190 return 0; |
658fc109eefc
added support for other codecs (mpeg4/h264/aac) in mpeg-ps parsing the PSM
nicodvb
parents:
14502
diff
changeset
|
191 } |
658fc109eefc
added support for other codecs (mpeg4/h264/aac) in mpeg-ps parsing the PSM
nicodvb
parents:
14502
diff
changeset
|
192 |
1 | 193 while(len>0){ // Skip stuFFing bytes |
194 c=stream_read_char(demux->stream);--len; | |
195 if(c!=0xFF)break; | |
196 } | |
197 if((c>>6)==1){ // Read (skip) STD scale & size value | |
198 // printf(" STD_scale=%d",(c>>5)&1); | |
199 d=((c&0x1F)<<8)|stream_read_char(demux->stream); | |
200 len-=2; | |
201 // printf(" STD_size=%d",d); | |
202 c=stream_read_char(demux->stream); | |
203 } | |
204 // Read System-1 stream timestamps: | |
205 if((c>>4)==2){ | |
206 pts=read_mpeg_timestamp(demux->stream,c); | |
207 len-=4; | |
208 } else | |
209 if((c>>4)==3){ | |
210 pts=read_mpeg_timestamp(demux->stream,c); | |
211 c=stream_read_char(demux->stream); | |
212 if((c>>4)!=1) pts=0; //printf("{ERROR4}"); | |
213 dts=read_mpeg_timestamp(demux->stream,c); | |
214 len-=4+1+4; | |
215 } else | |
216 if((c>>6)==2){ | |
217 int pts_flags; | |
218 int hdrlen; | |
219 // System-2 (.VOB) stream: | |
492 | 220 if((c>>4)&3) { |
1973
5216f108cb4f
all error/warn/info messages moved to help_mp-en.h for translation
arpi
parents:
1735
diff
changeset
|
221 mp_msg(MSGT_DEMUX,MSGL_WARN,MSGTR_EncryptedVOB); |
492 | 222 } |
1 | 223 c=stream_read_char(demux->stream); pts_flags=c>>6; |
224 c=stream_read_char(demux->stream); hdrlen=c; | |
225 len-=2; | |
1567 | 226 mp_dbg(MSGT_DEMUX,MSGL_DBG3," hdrlen=%d (len=%d)",hdrlen,len); |
227 if(hdrlen>len){ mp_msg(MSGT_DEMUX,MSGL_V,"demux_mpg: invalid header length \n"); return -1;} | |
7671 | 228 if(pts_flags==2 && hdrlen>=5){ |
1 | 229 c=stream_read_char(demux->stream); |
230 pts=read_mpeg_timestamp(demux->stream,c); | |
231 len-=5;hdrlen-=5; | |
232 } else | |
7671 | 233 if(pts_flags==3 && hdrlen>=10){ |
1 | 234 c=stream_read_char(demux->stream); |
235 pts=read_mpeg_timestamp(demux->stream,c); | |
236 c=stream_read_char(demux->stream); | |
237 dts=read_mpeg_timestamp(demux->stream,c); | |
238 len-=10;hdrlen-=10; | |
239 } | |
240 len-=hdrlen; | |
241 if(hdrlen>0) stream_skip(demux->stream,hdrlen); // skip header bytes | |
242 | |
243 //============== DVD Audio sub-stream ====================== | |
244 if(id==0x1BD){ | |
552 | 245 int aid=stream_read_char(demux->stream);--len; |
1 | 246 if(len<3) return -1; // invalid audio packet |
552 | 247 |
248 // AID: | |
249 // 0x20..0x3F subtitle | |
250 // 0x80..0x9F AC3 audio | |
251 // 0xA0..0xBF PCM audio | |
252 | |
253 if((aid & 0xE0) == 0x20){ | |
254 // subtitle: | |
255 aid&=0x1F; | |
426 | 256 |
587
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
554
diff
changeset
|
257 if(!demux->s_streams[aid]){ |
1567 | 258 mp_msg(MSGT_DEMUX,MSGL_V,"==> Found subtitle: %d\n",aid); |
587
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
554
diff
changeset
|
259 demux->s_streams[aid]=1; |
552 | 260 } |
261 | |
262 if(demux->sub->id==aid){ | |
263 ds=demux->sub; | |
264 } | |
265 | |
3955 | 266 } else if((aid & 0xC0) == 0x80 || (aid & 0xE0) == 0x00) { |
552 | 267 |
268 // aid=128+(aid&0x7F); | |
269 // aid=0x80..0xBF | |
270 | |
1289 | 271 if(!demux->a_streams[aid]) new_sh_audio(demux,aid); |
552 | 272 if(demux->audio->id==-1) demux->audio->id=aid; |
426 | 273 |
274 if(demux->audio->id==aid){ | |
7671 | 275 int type; |
426 | 276 ds=demux->audio; |
587
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
554
diff
changeset
|
277 if(!ds->sh) ds->sh=demux->a_streams[aid]; |
1 | 278 // READ Packet: Skip additional audio header data: |
7671 | 279 c=stream_read_char(demux->stream);//num of frames |
280 type=stream_read_char(demux->stream);//startpos hi | |
281 type=(type<<8)|stream_read_char(demux->stream);//startpos lo | |
282 // printf("\r[%02X][%04X]",c,type); | |
1 | 283 len-=3; |
7671 | 284 if((aid&0xE0)==0xA0 && len>=3){ |
285 unsigned char* hdr; | |
286 // save audio header as codecdata! | |
287 if(!((sh_audio_t*)(ds->sh))->codecdata_len){ | |
288 ((sh_audio_t*)(ds->sh))->codecdata=malloc(3); | |
289 ((sh_audio_t*)(ds->sh))->codecdata_len=3; | |
290 } | |
291 hdr=((sh_audio_t*)(ds->sh))->codecdata; | |
292 // read LPCM header: | |
293 // emphasis[1], mute[1], rvd[1], frame number[5]: | |
294 hdr[0]=stream_read_char(demux->stream); | |
295 // printf(" [%01X:%02d]",c>>5,c&31); | |
296 // quantization[2],freq[2],rvd[1],channels[3] | |
297 hdr[1]=stream_read_char(demux->stream); | |
298 // printf("[%01X:%01X] ",c>>4,c&15); | |
299 // dynamic range control (0x80=off): | |
300 hdr[2]=stream_read_char(demux->stream); | |
301 // printf("[%02X] ",c); | |
302 len-=3; | |
303 if(len<=0) mp_msg(MSGT_DEMUX,MSGL_V,"End of packet while searching for PCM header\n"); | |
1 | 304 } |
7671 | 305 // printf(" \n"); |
552 | 306 } // if(demux->audio->id==aid) |
307 | |
1567 | 308 } else mp_msg(MSGT_DEMUX,MSGL_V,"Unknown 0x1BD substream: 0x%02X \n",aid); |
552 | 309 |
310 } //if(id==0x1BD) | |
1 | 311 |
312 } else { | |
539 | 313 if(c!=0x0f){ |
1567 | 314 mp_msg(MSGT_DEMUX,MSGL_V," {ERROR5,c=%d} \n",c); |
539 | 315 return -1; // invalid packet !!!!!! |
316 } | |
1 | 317 } |
1567 | 318 if(mpeg_pts_error) mp_msg(MSGT_DEMUX,MSGL_V," {PTS_err:%d} \n",mpeg_pts_error); |
319 mp_dbg(MSGT_DEMUX,MSGL_DBG3," => len=%d\n",len); | |
1 | 320 |
501 | 321 // if(len<=0 || len>MAX_PS_PACKETSIZE) return -1; // Invalid packet size |
322 if(len<=0 || len>MAX_PS_PACKETSIZE){ | |
1567 | 323 mp_dbg(MSGT_DEMUX,MSGL_DBG2,"Invalid PS data len: %d\n",len); |
501 | 324 return -1; // invalid packet !!!!!! |
325 } | |
1 | 326 |
327 if(id>=0x1C0 && id<=0x1DF){ | |
328 // mpeg audio | |
329 int aid=id-0x1C0; | |
1289 | 330 if(!demux->a_streams[aid]) new_sh_audio(demux,aid); |
1 | 331 if(demux->audio->id==-1) demux->audio->id=aid; |
332 if(demux->audio->id==aid){ | |
333 ds=demux->audio; | |
587
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
554
diff
changeset
|
334 if(!ds->sh) ds->sh=demux->a_streams[aid]; |
14923
658fc109eefc
added support for other codecs (mpeg4/h264/aac) in mpeg-ps parsing the PSM
nicodvb
parents:
14502
diff
changeset
|
335 if(priv && ds->sh) { |
658fc109eefc
added support for other codecs (mpeg4/h264/aac) in mpeg-ps parsing the PSM
nicodvb
parents:
14502
diff
changeset
|
336 sh_audio_t *sh = (sh_audio_t *)ds->sh; |
658fc109eefc
added support for other codecs (mpeg4/h264/aac) in mpeg-ps parsing the PSM
nicodvb
parents:
14502
diff
changeset
|
337 if(priv->es_map[id - 0x1B0]) |
658fc109eefc
added support for other codecs (mpeg4/h264/aac) in mpeg-ps parsing the PSM
nicodvb
parents:
14502
diff
changeset
|
338 sh->format = priv->es_map[id - 0x1B0]; |
658fc109eefc
added support for other codecs (mpeg4/h264/aac) in mpeg-ps parsing the PSM
nicodvb
parents:
14502
diff
changeset
|
339 mp_dbg(MSGT_DEMUX,MSGL_DBG2,"ASSIGNED TO STREAM %d CODEC %x\n", id, priv->es_map[id - 0x1B0]); |
658fc109eefc
added support for other codecs (mpeg4/h264/aac) in mpeg-ps parsing the PSM
nicodvb
parents:
14502
diff
changeset
|
340 } |
1 | 341 } |
342 } else | |
343 if(id>=0x1E0 && id<=0x1EF){ | |
344 // mpeg video | |
345 int aid=id-0x1E0; | |
1289 | 346 if(!demux->v_streams[aid]) new_sh_video(demux,aid); |
1 | 347 if(demux->video->id==-1) demux->video->id=aid; |
426 | 348 if(demux->video->id==aid){ |
349 ds=demux->video; | |
587
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
554
diff
changeset
|
350 if(!ds->sh) ds->sh=demux->v_streams[aid]; |
14923
658fc109eefc
added support for other codecs (mpeg4/h264/aac) in mpeg-ps parsing the PSM
nicodvb
parents:
14502
diff
changeset
|
351 if(priv && ds->sh) { |
658fc109eefc
added support for other codecs (mpeg4/h264/aac) in mpeg-ps parsing the PSM
nicodvb
parents:
14502
diff
changeset
|
352 sh_video_t *sh = (sh_video_t *)ds->sh; |
658fc109eefc
added support for other codecs (mpeg4/h264/aac) in mpeg-ps parsing the PSM
nicodvb
parents:
14502
diff
changeset
|
353 if(priv->es_map[id - 0x1B0]) { |
658fc109eefc
added support for other codecs (mpeg4/h264/aac) in mpeg-ps parsing the PSM
nicodvb
parents:
14502
diff
changeset
|
354 sh->format = priv->es_map[id - 0x1B0]; |
658fc109eefc
added support for other codecs (mpeg4/h264/aac) in mpeg-ps parsing the PSM
nicodvb
parents:
14502
diff
changeset
|
355 mp_dbg(MSGT_DEMUX,MSGL_DBG2,"ASSIGNED TO STREAM %d CODEC %x\n", id, priv->es_map[id - 0x1B0]); |
658fc109eefc
added support for other codecs (mpeg4/h264/aac) in mpeg-ps parsing the PSM
nicodvb
parents:
14502
diff
changeset
|
356 } |
658fc109eefc
added support for other codecs (mpeg4/h264/aac) in mpeg-ps parsing the PSM
nicodvb
parents:
14502
diff
changeset
|
357 } |
426 | 358 } |
1 | 359 } |
360 | |
361 if(ds){ | |
1567 | 362 mp_dbg(MSGT_DEMUX,MSGL_DBG2,"DEMUX_MPG: Read %d data bytes from packet %04X\n",len,id); |
1 | 363 // printf("packet start = 0x%X \n",stream_tell(demux->stream)-packet_start_pos); |
1735 | 364 ds_read_packet(ds,demux->stream,len,pts/90000.0f,demux->filepos,0); |
13738
39004f891def
seeking based on the largest timestamp in an mpeg stream
aurel
parents:
13649
diff
changeset
|
365 if (demux->priv) ((mpg_demuxer_t*)demux->priv)->last_pts = pts/90000.0f; |
554 | 366 // if(ds==demux->sub) parse_dvdsub(ds->last->buffer,ds->last->len); |
1 | 367 return 1; |
368 } | |
1567 | 369 mp_dbg(MSGT_DEMUX,MSGL_DBG2,"DEMUX_MPG: Skipping %d data bytes from packet %04X\n",len,id); |
536 | 370 if(len<=2356) stream_skip(demux->stream,len); |
1 | 371 return 0; |
372 } | |
373 | |
587
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
554
diff
changeset
|
374 int num_elementary_packets100=0; |
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
554
diff
changeset
|
375 int num_elementary_packets101=0; |
9069
0d2b25a821c9
raw mpeg4-es support (you need to set -fps manually!)
arpi
parents:
8254
diff
changeset
|
376 int num_elementary_packets12x=0; |
1162 | 377 int num_elementary_packets1B6=0; |
1338
345585097137
mpeg PES steram support (only 1E0 & 1C0 packets, no 1BA/1BB headers)
arpi
parents:
1331
diff
changeset
|
378 int num_elementary_packetsPES=0; |
9824 | 379 int num_h264_slice=0; //combined slice |
380 int num_h264_dpa=0; //DPA Slice | |
381 int num_h264_dpb=0; //DPB Slice | |
382 int num_h264_dpc=0; //DPC Slice | |
383 int num_h264_idr=0; //IDR Slice | |
384 int num_h264_sps=0; | |
385 int num_h264_pps=0; | |
386 | |
4711 | 387 int num_mp3audio_packets=0; |
1 | 388 |
389 int demux_mpg_es_fill_buffer(demuxer_t *demux){ | |
390 // Elementary video stream | |
391 if(demux->stream->eof) return 0; | |
392 demux->filepos=stream_tell(demux->stream); | |
1735 | 393 ds_read_packet(demux->video,demux->stream,STREAM_BUFFER_SIZE,0,demux->filepos,0); |
1 | 394 return 1; |
395 } | |
396 | |
397 int demux_mpg_fill_buffer(demuxer_t *demux){ | |
398 unsigned int head=0; | |
399 int skipped=0; | |
5570
3a8d8c51355a
max_packs increased for some dvd with too many audio/sub
arpi
parents:
4711
diff
changeset
|
400 int max_packs=256; // 512kbyte |
1 | 401 int ret=0; |
402 | |
403 // System stream | |
404 do{ | |
405 demux->filepos=stream_tell(demux->stream); | |
406 head=stream_read_dword(demux->stream); | |
547 | 407 if((head&0xFFFFFF00)!=0x100){ |
408 // sync... | |
409 demux->filepos-=skipped; | |
410 while(1){ | |
536 | 411 int c=stream_read_char(demux->stream); |
412 if(c<0) break; //EOF | |
413 head<<=8; | |
414 if(head!=0x100){ | |
415 head|=c; | |
4711 | 416 if(mp_check_mp3_header(head)) ++num_mp3audio_packets; |
536 | 417 ++skipped; //++demux->filepos; |
418 continue; | |
419 } | |
420 head|=c; | |
421 break; | |
547 | 422 } |
423 demux->filepos+=skipped; | |
1 | 424 } |
425 if(stream_eof(demux->stream)) break; | |
426 // sure: head=0x000001XX | |
1567 | 427 mp_dbg(MSGT_DEMUX,MSGL_DBG4,"*** head=0x%X\n",head); |
1 | 428 if(demux->synced==0){ |
3770 | 429 if(head==0x1BA) demux->synced=1; //else |
430 // if(head==0x1BD || (head>=0x1C0 && head<=0x1EF)) demux->synced=3; // PES? | |
1 | 431 } else |
432 if(demux->synced==1){ | |
929 | 433 if(head==0x1BB || head==0x1BD || (head>=0x1C0 && head<=0x1EF)){ |
1 | 434 demux->synced=2; |
1567 | 435 mp_msg(MSGT_DEMUX,MSGL_V,"system stream synced at 0x%X (%d)!\n",demux->filepos,demux->filepos); |
536 | 436 num_elementary_packets100=0; // requires for re-sync! |
437 num_elementary_packets101=0; // requires for re-sync! | |
1 | 438 } else demux->synced=0; |
439 } // else | |
3255
ee28577dad02
combined PS/PES sync to allow .VDR playback from stdin
arpi
parents:
2555
diff
changeset
|
440 if(demux->synced>=2){ |
1 | 441 ret=demux_mpg_read_packet(demux,head); |
442 if(!ret) | |
443 if(--max_packs==0){ | |
444 demux->stream->eof=1; | |
1973
5216f108cb4f
all error/warn/info messages moved to help_mp-en.h for translation
arpi
parents:
1735
diff
changeset
|
445 mp_msg(MSGT_DEMUX,MSGL_ERR,MSGTR_DoesntContainSelectedStream); |
1 | 446 return 0; |
447 } | |
3255
ee28577dad02
combined PS/PES sync to allow .VDR playback from stdin
arpi
parents:
2555
diff
changeset
|
448 if(demux->synced==3) demux->synced=(ret==1)?2:0; // PES detect |
1 | 449 } else { |
450 if(head>=0x100 && head<0x1B0){ | |
1162 | 451 if(head==0x100) ++num_elementary_packets100; else |
9069
0d2b25a821c9
raw mpeg4-es support (you need to set -fps manually!)
arpi
parents:
8254
diff
changeset
|
452 if(head==0x101) ++num_elementary_packets101; else |
0d2b25a821c9
raw mpeg4-es support (you need to set -fps manually!)
arpi
parents:
8254
diff
changeset
|
453 if(head>=0x120 && head<=0x12F) ++num_elementary_packets12x; |
9824 | 454 |
455 if((head&~0x60) == 0x101) ++num_h264_slice; else | |
456 if((head&~0x60) == 0x102) ++num_h264_dpa; else | |
457 if((head&~0x60) == 0x103) ++num_h264_dpb; else | |
458 if((head&~0x60) == 0x104) ++num_h264_dpc; else | |
459 if((head&~0x60) == 0x105 && head != 0x105) ++num_h264_idr; else | |
460 if((head&~0x60) == 0x107 && head != 0x107) ++num_h264_sps; else | |
461 if((head&~0x60) == 0x108 && head != 0x108) ++num_h264_pps; | |
462 | |
1567 | 463 mp_msg(MSGT_DEMUX,MSGL_DBG3,"Opps... elementary video packet found: %03X\n",head); |
1162 | 464 } else |
4711 | 465 if((head>=0x1C0 && head<0x1F0) || head==0x1BD){ |
1338
345585097137
mpeg PES steram support (only 1E0 & 1C0 packets, no 1BA/1BB headers)
arpi
parents:
1331
diff
changeset
|
466 ++num_elementary_packetsPES; |
1567 | 467 mp_msg(MSGT_DEMUX,MSGL_DBG3,"Opps... PES packet found: %03X\n",head); |
1338
345585097137
mpeg PES steram support (only 1E0 & 1C0 packets, no 1BA/1BB headers)
arpi
parents:
1331
diff
changeset
|
468 } else |
1162 | 469 if(head==0x1B6) ++num_elementary_packets1B6; |
1 | 470 #if 1 |
1338
345585097137
mpeg PES steram support (only 1E0 & 1C0 packets, no 1BA/1BB headers)
arpi
parents:
1331
diff
changeset
|
471 if( ( (num_elementary_packets100>50 && num_elementary_packets101>50) || |
345585097137
mpeg PES steram support (only 1E0 & 1C0 packets, no 1BA/1BB headers)
arpi
parents:
1331
diff
changeset
|
472 (num_elementary_packetsPES>50) ) && skipped>4000000){ |
1567 | 473 mp_msg(MSGT_DEMUX,MSGL_V,"sync_mpeg_ps: seems to be ES/PES stream...\n"); |
1 | 474 demux->stream->eof=1; |
475 break; | |
476 } | |
4711 | 477 if(num_mp3audio_packets>100 && num_elementary_packets100<10){ |
478 mp_msg(MSGT_DEMUX,MSGL_V,"sync_mpeg_ps: seems to be MP3 stream...\n"); | |
479 demux->stream->eof=1; | |
480 break; | |
481 } | |
1 | 482 #endif |
483 } | |
484 } while(ret!=1); | |
1567 | 485 mp_dbg(MSGT_DEMUX,MSGL_DBG2,"demux: %d bad bytes skipped\n",skipped); |
1 | 486 if(demux->stream->eof){ |
1567 | 487 mp_msg(MSGT_DEMUX,MSGL_V,"MPEG Stream reached EOF\n"); |
1 | 488 return 0; |
489 } | |
490 return 1; | |
491 } | |
492 | |
8123
9fc45fe0d444
*HUGE* set of compiler warning fixes, unused variables removal
arpi
parents:
7671
diff
changeset
|
493 extern void resync_audio_stream(sh_audio_t *sh_audio); |
9fc45fe0d444
*HUGE* set of compiler warning fixes, unused variables removal
arpi
parents:
7671
diff
changeset
|
494 extern void skip_audio_frame(sh_audio_t *sh_audio); |
9fc45fe0d444
*HUGE* set of compiler warning fixes, unused variables removal
arpi
parents:
7671
diff
changeset
|
495 |
1466 | 496 void demux_seek_mpg(demuxer_t *demuxer,float rel_seek_secs,int flags){ |
497 demux_stream_t *d_audio=demuxer->audio; | |
498 demux_stream_t *d_video=demuxer->video; | |
499 sh_audio_t *sh_audio=d_audio->sh; | |
500 sh_video_t *sh_video=d_video->sh; | |
13738
39004f891def
seeking based on the largest timestamp in an mpeg stream
aurel
parents:
13649
diff
changeset
|
501 mpg_demuxer_t *mpg_d=(mpg_demuxer_t*)demuxer->priv; |
39004f891def
seeking based on the largest timestamp in an mpeg stream
aurel
parents:
13649
diff
changeset
|
502 int precision = 1; |
14426
3faa873334d7
fixed broken seeking in mpeg-es files; syncword seeking for all 3 video codecs
nicodvb
parents:
13738
diff
changeset
|
503 float oldpts = 0; |
13738
39004f891def
seeking based on the largest timestamp in an mpeg stream
aurel
parents:
13649
diff
changeset
|
504 off_t oldpos = demuxer->filepos; |
14426
3faa873334d7
fixed broken seeking in mpeg-es files; syncword seeking for all 3 video codecs
nicodvb
parents:
13738
diff
changeset
|
505 float newpts = 0; |
13738
39004f891def
seeking based on the largest timestamp in an mpeg stream
aurel
parents:
13649
diff
changeset
|
506 off_t newpos = (flags & 1) ? demuxer->movi_start : oldpos; |
1466 | 507 |
14426
3faa873334d7
fixed broken seeking in mpeg-es files; syncword seeking for all 3 video codecs
nicodvb
parents:
13738
diff
changeset
|
508 if(mpg_d) |
3faa873334d7
fixed broken seeking in mpeg-es files; syncword seeking for all 3 video codecs
nicodvb
parents:
13738
diff
changeset
|
509 oldpts = mpg_d->last_pts; |
3faa873334d7
fixed broken seeking in mpeg-es files; syncword seeking for all 3 video codecs
nicodvb
parents:
13738
diff
changeset
|
510 newpts = (flags & 1) ? 0.0 : oldpts; |
1466 | 511 //================= seek in MPEG ========================== |
13738
39004f891def
seeking based on the largest timestamp in an mpeg stream
aurel
parents:
13649
diff
changeset
|
512 //calculate the pts to seek to |
39004f891def
seeking based on the largest timestamp in an mpeg stream
aurel
parents:
13649
diff
changeset
|
513 if(flags & 2) { |
14426
3faa873334d7
fixed broken seeking in mpeg-es files; syncword seeking for all 3 video codecs
nicodvb
parents:
13738
diff
changeset
|
514 if (mpg_d && mpg_d->final_pts > 0.0) |
13738
39004f891def
seeking based on the largest timestamp in an mpeg stream
aurel
parents:
13649
diff
changeset
|
515 newpts += mpg_d->final_pts * rel_seek_secs; |
39004f891def
seeking based on the largest timestamp in an mpeg stream
aurel
parents:
13649
diff
changeset
|
516 else |
39004f891def
seeking based on the largest timestamp in an mpeg stream
aurel
parents:
13649
diff
changeset
|
517 newpts += rel_seek_secs * (demuxer->movi_end - demuxer->movi_start) * oldpts / oldpos; |
39004f891def
seeking based on the largest timestamp in an mpeg stream
aurel
parents:
13649
diff
changeset
|
518 } else |
39004f891def
seeking based on the largest timestamp in an mpeg stream
aurel
parents:
13649
diff
changeset
|
519 newpts += rel_seek_secs; |
39004f891def
seeking based on the largest timestamp in an mpeg stream
aurel
parents:
13649
diff
changeset
|
520 if (newpts < 0) newpts = 0; |
1628
bd1ef18cdf33
seeking flags implemented: 0x1=rel/abs and 0x2=time/percent
arpi
parents:
1597
diff
changeset
|
521 |
bd1ef18cdf33
seeking flags implemented: 0x1=rel/abs and 0x2=time/percent
arpi
parents:
1597
diff
changeset
|
522 if(flags&2){ |
bd1ef18cdf33
seeking flags implemented: 0x1=rel/abs and 0x2=time/percent
arpi
parents:
1597
diff
changeset
|
523 // float seek 0..1 |
bd1ef18cdf33
seeking flags implemented: 0x1=rel/abs and 0x2=time/percent
arpi
parents:
1597
diff
changeset
|
524 newpos+=(demuxer->movi_end-demuxer->movi_start)*rel_seek_secs; |
bd1ef18cdf33
seeking flags implemented: 0x1=rel/abs and 0x2=time/percent
arpi
parents:
1597
diff
changeset
|
525 } else { |
bd1ef18cdf33
seeking flags implemented: 0x1=rel/abs and 0x2=time/percent
arpi
parents:
1597
diff
changeset
|
526 // time seek (secs) |
13738
39004f891def
seeking based on the largest timestamp in an mpeg stream
aurel
parents:
13649
diff
changeset
|
527 if (mpg_d && mpg_d->has_valid_timestamps) { |
39004f891def
seeking based on the largest timestamp in an mpeg stream
aurel
parents:
13649
diff
changeset
|
528 if (mpg_d->final_pts > 0.0) |
39004f891def
seeking based on the largest timestamp in an mpeg stream
aurel
parents:
13649
diff
changeset
|
529 newpos += rel_seek_secs * (demuxer->movi_end - demuxer->movi_start) / mpg_d->final_pts; |
39004f891def
seeking based on the largest timestamp in an mpeg stream
aurel
parents:
13649
diff
changeset
|
530 else if (oldpts > 0.0) |
39004f891def
seeking based on the largest timestamp in an mpeg stream
aurel
parents:
13649
diff
changeset
|
531 newpos += rel_seek_secs * (oldpos - demuxer->movi_start) / oldpts; |
39004f891def
seeking based on the largest timestamp in an mpeg stream
aurel
parents:
13649
diff
changeset
|
532 } else if(!sh_video || !sh_video->i_bps) // unspecified or VBR |
1628
bd1ef18cdf33
seeking flags implemented: 0x1=rel/abs and 0x2=time/percent
arpi
parents:
1597
diff
changeset
|
533 newpos+=2324*75*rel_seek_secs; // 174.3 kbyte/sec |
1466 | 534 else |
1628
bd1ef18cdf33
seeking flags implemented: 0x1=rel/abs and 0x2=time/percent
arpi
parents:
1597
diff
changeset
|
535 newpos+=sh_video->i_bps*rel_seek_secs; |
bd1ef18cdf33
seeking flags implemented: 0x1=rel/abs and 0x2=time/percent
arpi
parents:
1597
diff
changeset
|
536 } |
1466 | 537 |
13738
39004f891def
seeking based on the largest timestamp in an mpeg stream
aurel
parents:
13649
diff
changeset
|
538 while (1) { |
4391
6394c1e9e770
DVD start position handling changed (progbar/eta fix)
arpi
parents:
3975
diff
changeset
|
539 if(newpos<demuxer->movi_start){ |
6394c1e9e770
DVD start position handling changed (progbar/eta fix)
arpi
parents:
3975
diff
changeset
|
540 if(demuxer->stream->type!=STREAMTYPE_VCD) demuxer->movi_start=0; // for VCD |
6394c1e9e770
DVD start position handling changed (progbar/eta fix)
arpi
parents:
3975
diff
changeset
|
541 if(newpos<demuxer->movi_start) newpos=demuxer->movi_start; |
6394c1e9e770
DVD start position handling changed (progbar/eta fix)
arpi
parents:
3975
diff
changeset
|
542 } |
1628
bd1ef18cdf33
seeking flags implemented: 0x1=rel/abs and 0x2=time/percent
arpi
parents:
1597
diff
changeset
|
543 |
1466 | 544 #ifdef _LARGEFILE_SOURCE |
545 newpos&=~((long long)STREAM_BUFFER_SIZE-1); /* sector boundary */ | |
546 #else | |
547 newpos&=~(STREAM_BUFFER_SIZE-1); /* sector boundary */ | |
548 #endif | |
549 stream_seek(demuxer->stream,newpos); | |
550 | |
551 // re-sync video: | |
552 videobuf_code_len=0; // reset ES stream buffer | |
553 | |
554 ds_fill_buffer(d_video); | |
555 if(sh_audio){ | |
556 ds_fill_buffer(d_audio); | |
557 resync_audio_stream(sh_audio); | |
558 } | |
559 | |
560 while(1){ | |
561 int i; | |
562 if(sh_audio && !d_audio->eof && d_video->pts && d_audio->pts){ | |
563 float a_pts=d_audio->pts; | |
564 a_pts+=(ds_tell_pts(d_audio)-sh_audio->a_in_buffer_len)/(float)sh_audio->i_bps; | |
565 if(d_video->pts>a_pts){ | |
566 skip_audio_frame(sh_audio); // sync audio | |
567 continue; | |
568 } | |
569 } | |
570 i=sync_video_packet(d_video); | |
14426
3faa873334d7
fixed broken seeking in mpeg-es files; syncword seeking for all 3 video codecs
nicodvb
parents:
13738
diff
changeset
|
571 if(sh_video->format == 0x10000004) { //mpeg4 |
14923
658fc109eefc
added support for other codecs (mpeg4/h264/aac) in mpeg-ps parsing the PSM
nicodvb
parents:
14502
diff
changeset
|
572 if(i==0x1B6) { //vop (frame) startcode |
658fc109eefc
added support for other codecs (mpeg4/h264/aac) in mpeg-ps parsing the PSM
nicodvb
parents:
14502
diff
changeset
|
573 int pos = videobuf_len; |
658fc109eefc
added support for other codecs (mpeg4/h264/aac) in mpeg-ps parsing the PSM
nicodvb
parents:
14502
diff
changeset
|
574 if(!read_video_packet(d_video)) break; // EOF |
658fc109eefc
added support for other codecs (mpeg4/h264/aac) in mpeg-ps parsing the PSM
nicodvb
parents:
14502
diff
changeset
|
575 if((videobuffer[pos+4] & 0x3F) == 0) break; //I-frame |
658fc109eefc
added support for other codecs (mpeg4/h264/aac) in mpeg-ps parsing the PSM
nicodvb
parents:
14502
diff
changeset
|
576 } |
14426
3faa873334d7
fixed broken seeking in mpeg-es files; syncword seeking for all 3 video codecs
nicodvb
parents:
13738
diff
changeset
|
577 } else if(sh_video->format == 0x10000005){ //h264 |
3faa873334d7
fixed broken seeking in mpeg-es files; syncword seeking for all 3 video codecs
nicodvb
parents:
13738
diff
changeset
|
578 if((i & ~0x60) == 0x101 || (i & ~0x60) == 0x102 || (i & ~0x60) == 0x105) break; |
3faa873334d7
fixed broken seeking in mpeg-es files; syncword seeking for all 3 video codecs
nicodvb
parents:
13738
diff
changeset
|
579 } else { //default mpeg1/2 |
3faa873334d7
fixed broken seeking in mpeg-es files; syncword seeking for all 3 video codecs
nicodvb
parents:
13738
diff
changeset
|
580 if(i==0x1B3 || i==0x1B8) break; // found it! |
3faa873334d7
fixed broken seeking in mpeg-es files; syncword seeking for all 3 video codecs
nicodvb
parents:
13738
diff
changeset
|
581 } |
1466 | 582 if(!i || !skip_video_packet(d_video)) break; // EOF? |
583 } | |
14426
3faa873334d7
fixed broken seeking in mpeg-es files; syncword seeking for all 3 video codecs
nicodvb
parents:
13738
diff
changeset
|
584 if(!mpg_d) |
3faa873334d7
fixed broken seeking in mpeg-es files; syncword seeking for all 3 video codecs
nicodvb
parents:
13738
diff
changeset
|
585 break; |
13738
39004f891def
seeking based on the largest timestamp in an mpeg stream
aurel
parents:
13649
diff
changeset
|
586 if (!precision || abs(newpts - mpg_d->last_pts) < 0.5 || (mpg_d->last_pts == oldpts)) break; |
39004f891def
seeking based on the largest timestamp in an mpeg stream
aurel
parents:
13649
diff
changeset
|
587 if ((newpos - oldpos) * (mpg_d->last_pts - oldpts) < 0) { // invalid timestamps |
39004f891def
seeking based on the largest timestamp in an mpeg stream
aurel
parents:
13649
diff
changeset
|
588 mpg_d->has_valid_timestamps = 0; |
39004f891def
seeking based on the largest timestamp in an mpeg stream
aurel
parents:
13649
diff
changeset
|
589 break; |
39004f891def
seeking based on the largest timestamp in an mpeg stream
aurel
parents:
13649
diff
changeset
|
590 } |
39004f891def
seeking based on the largest timestamp in an mpeg stream
aurel
parents:
13649
diff
changeset
|
591 precision--; |
39004f891def
seeking based on the largest timestamp in an mpeg stream
aurel
parents:
13649
diff
changeset
|
592 //prepare another seek because we are off by more than 0.5s |
14426
3faa873334d7
fixed broken seeking in mpeg-es files; syncword seeking for all 3 video codecs
nicodvb
parents:
13738
diff
changeset
|
593 if(mpg_d) { |
13738
39004f891def
seeking based on the largest timestamp in an mpeg stream
aurel
parents:
13649
diff
changeset
|
594 newpos += (newpts - mpg_d->last_pts) * (newpos - oldpos) / (mpg_d->last_pts - oldpts); |
39004f891def
seeking based on the largest timestamp in an mpeg stream
aurel
parents:
13649
diff
changeset
|
595 ds_free_packs(d_audio); |
39004f891def
seeking based on the largest timestamp in an mpeg stream
aurel
parents:
13649
diff
changeset
|
596 ds_free_packs(d_video); |
39004f891def
seeking based on the largest timestamp in an mpeg stream
aurel
parents:
13649
diff
changeset
|
597 demuxer->stream->eof=0; // clear eof flag |
39004f891def
seeking based on the largest timestamp in an mpeg stream
aurel
parents:
13649
diff
changeset
|
598 d_video->eof=0; |
39004f891def
seeking based on the largest timestamp in an mpeg stream
aurel
parents:
13649
diff
changeset
|
599 d_audio->eof=0; |
14426
3faa873334d7
fixed broken seeking in mpeg-es files; syncword seeking for all 3 video codecs
nicodvb
parents:
13738
diff
changeset
|
600 } |
13738
39004f891def
seeking based on the largest timestamp in an mpeg stream
aurel
parents:
13649
diff
changeset
|
601 } |
1466 | 602 } |
603 | |
8208
ae5a2ae1c349
demuxer_control(), percent position and time length query implemented in
arpi
parents:
8123
diff
changeset
|
604 int demux_mpg_control(demuxer_t *demuxer,int cmd, void *arg){ |
8254
772d6d27fd66
warning patch by (Dominik Mierzejewski <dominik at rangers dot eu dot org>)
michael
parents:
8208
diff
changeset
|
605 /* demux_stream_t *d_audio=demuxer->audio;*/ |
8208
ae5a2ae1c349
demuxer_control(), percent position and time length query implemented in
arpi
parents:
8123
diff
changeset
|
606 demux_stream_t *d_video=demuxer->video; |
8254
772d6d27fd66
warning patch by (Dominik Mierzejewski <dominik at rangers dot eu dot org>)
michael
parents:
8208
diff
changeset
|
607 /* sh_audio_t *sh_audio=d_audio->sh;*/ |
8208
ae5a2ae1c349
demuxer_control(), percent position and time length query implemented in
arpi
parents:
8123
diff
changeset
|
608 sh_video_t *sh_video=d_video->sh; |
13738
39004f891def
seeking based on the largest timestamp in an mpeg stream
aurel
parents:
13649
diff
changeset
|
609 mpg_demuxer_t *mpg_d=(mpg_demuxer_t*)demuxer->priv; |
8208
ae5a2ae1c349
demuxer_control(), percent position and time length query implemented in
arpi
parents:
8123
diff
changeset
|
610 |
ae5a2ae1c349
demuxer_control(), percent position and time length query implemented in
arpi
parents:
8123
diff
changeset
|
611 switch(cmd) { |
ae5a2ae1c349
demuxer_control(), percent position and time length query implemented in
arpi
parents:
8123
diff
changeset
|
612 case DEMUXER_CTRL_GET_TIME_LENGTH: |
13738
39004f891def
seeking based on the largest timestamp in an mpeg stream
aurel
parents:
13649
diff
changeset
|
613 if (mpg_d && mpg_d->has_valid_timestamps) { |
39004f891def
seeking based on the largest timestamp in an mpeg stream
aurel
parents:
13649
diff
changeset
|
614 *((unsigned long *)arg)=(long)mpg_d->final_pts; |
39004f891def
seeking based on the largest timestamp in an mpeg stream
aurel
parents:
13649
diff
changeset
|
615 return DEMUXER_CTRL_GUESS; |
39004f891def
seeking based on the largest timestamp in an mpeg stream
aurel
parents:
13649
diff
changeset
|
616 } |
8208
ae5a2ae1c349
demuxer_control(), percent position and time length query implemented in
arpi
parents:
8123
diff
changeset
|
617 return DEMUXER_CTRL_DONTKNOW; |
ae5a2ae1c349
demuxer_control(), percent position and time length query implemented in
arpi
parents:
8123
diff
changeset
|
618 |
ae5a2ae1c349
demuxer_control(), percent position and time length query implemented in
arpi
parents:
8123
diff
changeset
|
619 case DEMUXER_CTRL_GET_PERCENT_POS: |
13738
39004f891def
seeking based on the largest timestamp in an mpeg stream
aurel
parents:
13649
diff
changeset
|
620 if (mpg_d && mpg_d->has_valid_timestamps && mpg_d->final_pts > 0.0) { |
39004f891def
seeking based on the largest timestamp in an mpeg stream
aurel
parents:
13649
diff
changeset
|
621 *((int *)arg)=(int)(100 * mpg_d->last_pts / mpg_d->final_pts); |
39004f891def
seeking based on the largest timestamp in an mpeg stream
aurel
parents:
13649
diff
changeset
|
622 return DEMUXER_CTRL_OK; |
39004f891def
seeking based on the largest timestamp in an mpeg stream
aurel
parents:
13649
diff
changeset
|
623 } |
14502
8769fa370f83
Move generic length and percent pos calculation to demuxer.c
reimar
parents:
14426
diff
changeset
|
624 return DEMUXER_CTRL_DONTKNOW; |
8208
ae5a2ae1c349
demuxer_control(), percent position and time length query implemented in
arpi
parents:
8123
diff
changeset
|
625 |
ae5a2ae1c349
demuxer_control(), percent position and time length query implemented in
arpi
parents:
8123
diff
changeset
|
626 default: |
ae5a2ae1c349
demuxer_control(), percent position and time length query implemented in
arpi
parents:
8123
diff
changeset
|
627 return DEMUXER_CTRL_NOTIMPL; |
ae5a2ae1c349
demuxer_control(), percent position and time length query implemented in
arpi
parents:
8123
diff
changeset
|
628 } |
ae5a2ae1c349
demuxer_control(), percent position and time length query implemented in
arpi
parents:
8123
diff
changeset
|
629 } |