Mercurial > mplayer.hg
annotate libmpdemux/mpeg_packetizer.c @ 19171:c525ee7bc300
hu/ports.xml & man/hu/mplayer.1 synced with r19175
author | Gabrov |
---|---|
date | Mon, 24 Jul 2006 10:24:41 +0000 |
parents | a28bcc898ab4 |
children | 77d29dd9b08e |
rev | line source |
---|---|
19141 | 1 /* |
2 * Copyright (C) 2006 Benjamin Zores | |
3 * Set of helper routines for building MPEG 1/2 PS/PES packets. | |
4 * | |
5 * Based on various code bororwed from vo_mpegpes/vo_dxr2 : | |
6 * (C) 2000 Ralph Metzler <ralph@convergence.de> | |
7 * Marcus Metzler <marcus@convergence.de> | |
8 * Gerard Lantau | |
9 * | |
10 * This program is free software; you can redistribute it and/or modify | |
11 * it under the terms of the GNU General Public License as published by | |
12 * the Free Software Foundation; either version 2 of the License, or | |
13 * (at your option) any later version. | |
14 * | |
15 * This program is distributed in the hope that it will be useful, | |
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 * GNU General Public License for more details. | |
19 * | |
20 * You should have received a copy of the GNU General Public License | |
21 * along with this program; if not, write to the Free Software Foundation, | |
22 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
23 */ | |
24 | |
25 #include <unistd.h> | |
26 #include <string.h> | |
27 #include <inttypes.h> | |
28 | |
29 #include "mp_msg.h" | |
30 #include "mpeg_packetizer.h" | |
31 | |
32 #define PES_MAX_SIZE 2048 | |
33 | |
34 static unsigned char pes_header[PES_MAX_SIZE]; | |
35 | |
19148
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
36 static unsigned char ps2_header[] = { |
19141 | 37 0x00, 0x00, 0x01, 0xba, 0x44, 0x00, 0x04, 0x00, |
38 0x04, 0x01, 0x01, 0x86, 0xa3, 0xf8 | |
39 }; | |
40 | |
41 | |
19148
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
42 static unsigned char ps1_header[] = { |
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
43 0x00, 0x00, 0x01, 0xba, 0x21, 0x00, |
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
44 0xb9, 0x37, 0x83, 0x80, 0xc3, 0x51, |
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
45 }; |
19141 | 46 |
19149
73d8ea32ebb8
cosmetics: comments and reindentation asked by gxben
nicodvb
parents:
19148
diff
changeset
|
47 /* Send MPEG <type> PES packet */ |
19141 | 48 int |
19149
73d8ea32ebb8
cosmetics: comments and reindentation asked by gxben
nicodvb
parents:
19148
diff
changeset
|
49 send_mpeg_pes_packet (unsigned char *data, int len, int id, uint64_t pts, |
73d8ea32ebb8
cosmetics: comments and reindentation asked by gxben
nicodvb
parents:
19148
diff
changeset
|
50 int type, int my_write (unsigned char *data, int len)) |
19141 | 51 { |
52 int ptslen = 5; | |
53 int n = 0; | |
19148
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
54 int idx, plen; |
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
55 int hdr; |
19141 | 56 |
57 mp_msg (MSGT_HEADER, MSGL_DBG2, | |
19152 | 58 "MPEG%d PES packet: 0x%x => %lu \n", type, id, pts); |
19141 | 59 memset (pes_header, '\0', PES_MAX_SIZE); |
60 | |
61 /* startcode */ | |
62 pes_header[0] = 0; | |
63 pes_header[1] = 0; | |
64 pes_header[2] = 0x01; | |
65 pes_header[3] = id; /* stream id */ | |
66 | |
67 while (len > 0) | |
68 { | |
69 int payload_size = len; /* data + PTS */ | |
19148
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
70 if(type == 2) |
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
71 hdr = 3; |
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
72 else |
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
73 hdr = (ptslen ? 0 : 1); |
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
74 if (6 + hdr + ptslen + payload_size > PES_MAX_SIZE) |
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
75 payload_size = PES_MAX_SIZE - 6 - hdr - ptslen; |
19141 | 76 |
77 /* construct PES header: packetize */ | |
19148
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
78 plen = payload_size + hdr + ptslen; |
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
79 pes_header[4] = plen >> 8; |
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
80 pes_header[5] = plen & 255; |
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
81 idx = 6; |
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
82 |
19141 | 83 if (ptslen) |
84 { | |
85 int x; | |
19148
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
86 |
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
87 if(type == 2) |
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
88 { |
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
89 pes_header[idx++] = 0x81; |
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
90 pes_header[idx++] = 0x80; |
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
91 pes_header[idx++] = ptslen; |
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
92 } |
19141 | 93 |
94 /* presentation time stamp */ | |
95 x = (0x02 << 4) | (((pts >> 30) & 0x07) << 1) | 1; | |
19148
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
96 pes_header[idx++] = x; |
19141 | 97 |
98 x = ((((pts >> 15) & 0x7fff) << 1) | 1); | |
19148
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
99 pes_header[idx++] = x >>8; |
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
100 pes_header[idx++] = x & 255; |
19141 | 101 |
102 x = (((pts & 0x7fff) << 1) | 1); | |
19148
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
103 pes_header[idx++] = x >> 8; |
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
104 pes_header[idx++] = x & 255; |
19141 | 105 } |
106 else | |
107 { | |
19148
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
108 if(type == 2) |
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
109 { |
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
110 pes_header[idx++] = 0x81; |
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
111 pes_header[idx++] = 0x00; |
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
112 pes_header[idx++] = 0x00; |
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
113 } |
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
114 else |
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
115 pes_header[idx++] = 0x0f; |
19141 | 116 } |
19148
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
117 |
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
118 my_write (pes_header, idx); |
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
119 n = my_write (data, payload_size); |
19141 | 120 |
19148
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
121 len -= n; |
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
122 data += n; |
19141 | 123 ptslen = 0; /* store PTS only once, at first packet! */ |
124 } | |
125 | |
126 return n; | |
127 } | |
128 | |
19149
73d8ea32ebb8
cosmetics: comments and reindentation asked by gxben
nicodvb
parents:
19148
diff
changeset
|
129 /* Send MPEG <type> PS packet */ |
19141 | 130 int |
19148
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
131 send_mpeg_ps_packet (unsigned char *data, int len, int id, uint64_t pts, int type, |
19141 | 132 int my_write (unsigned char *data, int len)) |
133 { | |
19148
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
134 if(type == 2) |
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
135 my_write (ps2_header, sizeof (ps2_header)); |
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
136 else |
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
137 my_write (ps1_header, sizeof (ps1_header)); |
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
138 return send_mpeg_pes_packet (data, len, id, pts, type, my_write); |
19141 | 139 } |
140 | |
19149
73d8ea32ebb8
cosmetics: comments and reindentation asked by gxben
nicodvb
parents:
19148
diff
changeset
|
141 /* Send MPEG 2 LPCM packet */ |
19141 | 142 int |
143 send_mpeg_lpcm_packet (unsigned char* data, int len, | |
144 int id, uint64_t pts, int freq_id, | |
145 int my_write (unsigned char *data, int len)) | |
146 { | |
147 int ptslen = pts ? 5 : 0; | |
148 int n = 0; | |
149 | |
150 mp_msg (MSGT_HEADER, MSGL_DBG2, | |
151 "MPEG LPCM packet: 0x%x => %lu \n", id, pts); | |
152 memset (pes_header, '\0', PES_MAX_SIZE); | |
153 | |
154 /* startcode */ | |
155 pes_header[0] = 0; | |
156 pes_header[1] = 0; | |
157 pes_header[2] = 1; | |
158 pes_header[3] = 0xBD; /* stream id */ | |
159 | |
160 while (len >= 4) | |
161 { | |
162 int payload_size = PES_MAX_SIZE - 6 - 20; /* max possible data len */ | |
163 if( payload_size > len) | |
164 payload_size = len; | |
165 | |
166 payload_size &= ~3; /* align! */ | |
167 | |
168 /* packetsize */ | |
169 pes_header[4] = (payload_size + 3 + ptslen + 7) >> 8; | |
170 pes_header[5] = (payload_size + 3 + ptslen + 7) & 255; | |
171 | |
172 /* stuffing */ | |
173 /* TTCCxxxx CC=css TT=type: 1=STD 0=mpeg1 2=vob */ | |
174 pes_header[6] = 0x81; | |
175 | |
176 /* FFxxxxxx FF=pts flags=2 vs 0 */ | |
177 pes_header[7] = ptslen ? 0x80 : 0; | |
178 | |
179 /* header length */ | |
180 pes_header[8] = ptslen; | |
181 | |
182 if (ptslen) | |
183 { | |
184 int x; | |
185 | |
186 /* presentation time stamp */ | |
187 x = (0x02 << 4) | (((pts >> 30) & 0x07) << 1) | 1; | |
188 pes_header[9] = x; | |
189 | |
190 x = ((((pts >> 15) & 0x7fff) << 1) | 1); | |
191 pes_header[10] = x >> 8; | |
192 pes_header[11] = x &255; | |
193 | |
194 x = (((pts & 0x7fff) << 1) | 1); | |
195 pes_header[12] = x >> 8; | |
196 pes_header[13] = x & 255; | |
197 } | |
198 | |
199 /* ============ LPCM header: (7 bytes) ================= */ | |
200 /* Info by mocm@convergence.de */ | |
201 | |
202 /* ID */ | |
203 pes_header[ptslen + 9] = id; | |
204 | |
205 /* number of frames */ | |
206 pes_header[ptslen + 10] = 0x07; | |
207 | |
208 /* first acces unit pointer, i.e. start of audio frame */ | |
209 pes_header[ptslen + 11] = 0x00; | |
210 pes_header[ptslen + 12] = 0x04; | |
211 | |
212 /* audio emphasis on-off 1 bit */ | |
213 /* audio mute on-off 1 bit */ | |
214 /* reserved 1 bit */ | |
215 /* audio frame number 5 bit */ | |
216 pes_header[ptslen + 13] = 0x0C; | |
217 | |
218 /* quantization word length 2 bit */ | |
219 /* audio sampling frequency (48khz = 0, 96khz = 1) 2 bit */ | |
220 /* reserved 1 bit */ | |
221 /* number of audio channels - 1 (e.g. stereo = 1) 3 bit */ | |
222 pes_header[ptslen + 14] = 1 | (freq_id << 4); | |
223 | |
224 /* dynamic range control (0x80 if off) */ | |
225 pes_header [ptslen + 15] = 0x80; | |
226 | |
227 memcpy (&pes_header[6 + 3 + ptslen + 7], data, payload_size); | |
228 n += my_write (pes_header, 6 + 3 + ptslen + 7 + payload_size); | |
229 | |
230 len -= payload_size; | |
231 data += payload_size; | |
232 ptslen = 0; /* store PTS only once, at first packet! */ | |
233 } | |
234 | |
235 return n; | |
236 } |