Mercurial > mplayer.hg
annotate libmpdemux/mpeg_packetizer.c @ 27320:44341e4d5621
Do not include sys/socket.h when using winsock2, it is pointless
and breaks compilation under MinGW.
author | reimar |
---|---|
date | Sat, 26 Jul 2008 20:58:50 +0000 |
parents | 46e21b0705c8 |
children | 0f1b5b68af32 |
rev | line source |
---|---|
19141 | 1 /* |
26742
0c1db5fd3f79
Use standard license headers with standard formatting.
diego
parents:
25676
diff
changeset
|
2 * set of helper routines for building MPEG 1/2 PS/PES packets |
0c1db5fd3f79
Use standard license headers with standard formatting.
diego
parents:
25676
diff
changeset
|
3 * |
0c1db5fd3f79
Use standard license headers with standard formatting.
diego
parents:
25676
diff
changeset
|
4 * Copyright (C) 2006 Benjamin Zores |
19141 | 5 * |
26742
0c1db5fd3f79
Use standard license headers with standard formatting.
diego
parents:
25676
diff
changeset
|
6 * Based on code borrowed from vo_mpegpes/vo_dxr2: |
0c1db5fd3f79
Use standard license headers with standard formatting.
diego
parents:
25676
diff
changeset
|
7 * (C) 2000 Ralph Metzler <ralph@convergence.de> |
0c1db5fd3f79
Use standard license headers with standard formatting.
diego
parents:
25676
diff
changeset
|
8 * Marcus Metzler <marcus@convergence.de> |
0c1db5fd3f79
Use standard license headers with standard formatting.
diego
parents:
25676
diff
changeset
|
9 * Gerard Lantau |
0c1db5fd3f79
Use standard license headers with standard formatting.
diego
parents:
25676
diff
changeset
|
10 * |
0c1db5fd3f79
Use standard license headers with standard formatting.
diego
parents:
25676
diff
changeset
|
11 * This file is part of MPlayer. |
19141 | 12 * |
26742
0c1db5fd3f79
Use standard license headers with standard formatting.
diego
parents:
25676
diff
changeset
|
13 * MPlayer is free software; you can redistribute it and/or modify |
0c1db5fd3f79
Use standard license headers with standard formatting.
diego
parents:
25676
diff
changeset
|
14 * it under the terms of the GNU General Public License as published by |
0c1db5fd3f79
Use standard license headers with standard formatting.
diego
parents:
25676
diff
changeset
|
15 * the Free Software Foundation; either version 2 of the License, or |
0c1db5fd3f79
Use standard license headers with standard formatting.
diego
parents:
25676
diff
changeset
|
16 * (at your option) any later version. |
19141 | 17 * |
26742
0c1db5fd3f79
Use standard license headers with standard formatting.
diego
parents:
25676
diff
changeset
|
18 * MPlayer is distributed in the hope that it will be useful, |
0c1db5fd3f79
Use standard license headers with standard formatting.
diego
parents:
25676
diff
changeset
|
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
0c1db5fd3f79
Use standard license headers with standard formatting.
diego
parents:
25676
diff
changeset
|
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
0c1db5fd3f79
Use standard license headers with standard formatting.
diego
parents:
25676
diff
changeset
|
21 * GNU General Public License for more details. |
19141 | 22 * |
26742
0c1db5fd3f79
Use standard license headers with standard formatting.
diego
parents:
25676
diff
changeset
|
23 * You should have received a copy of the GNU General Public License along |
0c1db5fd3f79
Use standard license headers with standard formatting.
diego
parents:
25676
diff
changeset
|
24 * with MPlayer; if not, write to the Free Software Foundation, Inc., |
0c1db5fd3f79
Use standard license headers with standard formatting.
diego
parents:
25676
diff
changeset
|
25 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
19141 | 26 */ |
27 | |
28 #include <unistd.h> | |
29 #include <string.h> | |
30 #include <inttypes.h> | |
31 | |
32 #include "mp_msg.h" | |
33 #include "mpeg_packetizer.h" | |
34 | |
35 #define PES_MAX_SIZE 2048 | |
36 | |
25672 | 37 static const unsigned char ps2_header[] = { |
19141 | 38 0x00, 0x00, 0x01, 0xba, 0x44, 0x00, 0x04, 0x00, |
39 0x04, 0x01, 0x01, 0x86, 0xa3, 0xf8 | |
40 }; | |
41 | |
42 | |
25672 | 43 static const unsigned char ps1_header[] = { |
19148
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
44 0x00, 0x00, 0x01, 0xba, 0x21, 0x00, |
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
45 0xb9, 0x37, 0x83, 0x80, 0xc3, 0x51, |
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
46 }; |
19141 | 47 |
19149
73d8ea32ebb8
cosmetics: comments and reindentation asked by gxben
nicodvb
parents:
19148
diff
changeset
|
48 /* Send MPEG <type> PES packet */ |
19243
73443b22df17
final cleanup: implemented send_mpeg_pes_packet() and send_mpeg_lpcm_packet() using new send_mpeg_pes_packet_ll()
nicodvb
parents:
19240
diff
changeset
|
49 static int |
73443b22df17
final cleanup: implemented send_mpeg_pes_packet() and send_mpeg_lpcm_packet() using new send_mpeg_pes_packet_ll()
nicodvb
parents:
19240
diff
changeset
|
50 send_mpeg_pes_packet_ll(unsigned char *data, int len, int id, uint64_t pts, |
73443b22df17
final cleanup: implemented send_mpeg_pes_packet() and send_mpeg_lpcm_packet() using new send_mpeg_pes_packet_ll()
nicodvb
parents:
19240
diff
changeset
|
51 int type, unsigned char *header, int header_len, |
25673
d576b679747b
Write functions used by send_mpeg_*_packet may _not_ modify data
reimar
parents:
25672
diff
changeset
|
52 int align4, int my_write (const unsigned char *data, int len)) |
19141 | 53 { |
19240 | 54 int ptslen = (pts ? 5 : 0); |
19141 | 55 int n = 0; |
19148
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
56 int idx, plen; |
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
57 int hdr; |
25676
a721f266b21b
moved pes_header from file-static to send_mpeg_pes_packet_ll()
nicodvb
parents:
25673
diff
changeset
|
58 unsigned char pes_header[PES_MAX_SIZE]; |
19141 | 59 |
60 mp_msg (MSGT_HEADER, MSGL_DBG2, | |
27201
46e21b0705c8
Use correct PRIu64 length modifier for uint64_t value, fixes the warning:
diego
parents:
26742
diff
changeset
|
61 "MPEG%d PES packet: 0x%x => %"PRIu64" \n", type, id, pts); |
19141 | 62 memset (pes_header, '\0', PES_MAX_SIZE); |
63 | |
64 /* startcode */ | |
65 pes_header[0] = 0; | |
66 pes_header[1] = 0; | |
67 pes_header[2] = 0x01; | |
68 pes_header[3] = id; /* stream id */ | |
69 | |
70 while (len > 0) | |
71 { | |
72 int payload_size = len; /* data + PTS */ | |
19148
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
73 if(type == 2) |
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
74 hdr = 3; |
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
75 else |
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
76 hdr = (ptslen ? 0 : 1); |
19243
73443b22df17
final cleanup: implemented send_mpeg_pes_packet() and send_mpeg_lpcm_packet() using new send_mpeg_pes_packet_ll()
nicodvb
parents:
19240
diff
changeset
|
77 if (6 + hdr + ptslen + payload_size + header_len > PES_MAX_SIZE) |
73443b22df17
final cleanup: implemented send_mpeg_pes_packet() and send_mpeg_lpcm_packet() using new send_mpeg_pes_packet_ll()
nicodvb
parents:
19240
diff
changeset
|
78 payload_size = PES_MAX_SIZE - 6 - hdr - ptslen - header_len; |
73443b22df17
final cleanup: implemented send_mpeg_pes_packet() and send_mpeg_lpcm_packet() using new send_mpeg_pes_packet_ll()
nicodvb
parents:
19240
diff
changeset
|
79 if(align4) |
73443b22df17
final cleanup: implemented send_mpeg_pes_packet() and send_mpeg_lpcm_packet() using new send_mpeg_pes_packet_ll()
nicodvb
parents:
19240
diff
changeset
|
80 payload_size &= ~3; |
19141 | 81 |
82 /* construct PES header: packetize */ | |
19243
73443b22df17
final cleanup: implemented send_mpeg_pes_packet() and send_mpeg_lpcm_packet() using new send_mpeg_pes_packet_ll()
nicodvb
parents:
19240
diff
changeset
|
83 plen = payload_size + hdr + ptslen + header_len; |
19148
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
84 pes_header[4] = plen >> 8; |
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
85 pes_header[5] = plen & 255; |
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
86 idx = 6; |
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
87 |
19141 | 88 if (ptslen) |
89 { | |
90 int x; | |
19148
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
91 |
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
92 if(type == 2) |
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
93 { |
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
94 pes_header[idx++] = 0x81; |
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
95 pes_header[idx++] = 0x80; |
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
96 pes_header[idx++] = ptslen; |
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
97 } |
19141 | 98 |
99 /* presentation time stamp */ | |
100 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
|
101 pes_header[idx++] = x; |
19141 | 102 |
103 x = ((((pts >> 15) & 0x7fff) << 1) | 1); | |
19148
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
104 pes_header[idx++] = x >>8; |
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
105 pes_header[idx++] = x & 255; |
19141 | 106 |
107 x = (((pts & 0x7fff) << 1) | 1); | |
19148
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
108 pes_header[idx++] = x >> 8; |
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
109 pes_header[idx++] = x & 255; |
19141 | 110 } |
111 else | |
112 { | |
19148
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
113 if(type == 2) |
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
114 { |
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
115 pes_header[idx++] = 0x81; |
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
116 pes_header[idx++] = 0x00; |
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
117 pes_header[idx++] = 0x00; |
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
118 } |
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
119 else |
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
120 pes_header[idx++] = 0x0f; |
19141 | 121 } |
19243
73443b22df17
final cleanup: implemented send_mpeg_pes_packet() and send_mpeg_lpcm_packet() using new send_mpeg_pes_packet_ll()
nicodvb
parents:
19240
diff
changeset
|
122 |
73443b22df17
final cleanup: implemented send_mpeg_pes_packet() and send_mpeg_lpcm_packet() using new send_mpeg_pes_packet_ll()
nicodvb
parents:
19240
diff
changeset
|
123 if(header_len) |
73443b22df17
final cleanup: implemented send_mpeg_pes_packet() and send_mpeg_lpcm_packet() using new send_mpeg_pes_packet_ll()
nicodvb
parents:
19240
diff
changeset
|
124 { |
73443b22df17
final cleanup: implemented send_mpeg_pes_packet() and send_mpeg_lpcm_packet() using new send_mpeg_pes_packet_ll()
nicodvb
parents:
19240
diff
changeset
|
125 memcpy(&pes_header[idx], header, header_len); |
73443b22df17
final cleanup: implemented send_mpeg_pes_packet() and send_mpeg_lpcm_packet() using new send_mpeg_pes_packet_ll()
nicodvb
parents:
19240
diff
changeset
|
126 idx += header_len; |
73443b22df17
final cleanup: implemented send_mpeg_pes_packet() and send_mpeg_lpcm_packet() using new send_mpeg_pes_packet_ll()
nicodvb
parents:
19240
diff
changeset
|
127 } |
19148
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
128 |
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
129 my_write (pes_header, idx); |
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
130 n = my_write (data, payload_size); |
19141 | 131 |
19148
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
132 len -= n; |
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
133 data += n; |
19141 | 134 ptslen = 0; /* store PTS only once, at first packet! */ |
19243
73443b22df17
final cleanup: implemented send_mpeg_pes_packet() and send_mpeg_lpcm_packet() using new send_mpeg_pes_packet_ll()
nicodvb
parents:
19240
diff
changeset
|
135 if(align4 && len < 4) |
73443b22df17
final cleanup: implemented send_mpeg_pes_packet() and send_mpeg_lpcm_packet() using new send_mpeg_pes_packet_ll()
nicodvb
parents:
19240
diff
changeset
|
136 break; |
19141 | 137 } |
138 | |
139 return n; | |
140 } | |
141 | |
19243
73443b22df17
final cleanup: implemented send_mpeg_pes_packet() and send_mpeg_lpcm_packet() using new send_mpeg_pes_packet_ll()
nicodvb
parents:
19240
diff
changeset
|
142 int |
73443b22df17
final cleanup: implemented send_mpeg_pes_packet() and send_mpeg_lpcm_packet() using new send_mpeg_pes_packet_ll()
nicodvb
parents:
19240
diff
changeset
|
143 send_mpeg_pes_packet (unsigned char *data, int len, int id, uint64_t pts, |
25673
d576b679747b
Write functions used by send_mpeg_*_packet may _not_ modify data
reimar
parents:
25672
diff
changeset
|
144 int type, int my_write (const unsigned char *data, int len)) |
19243
73443b22df17
final cleanup: implemented send_mpeg_pes_packet() and send_mpeg_lpcm_packet() using new send_mpeg_pes_packet_ll()
nicodvb
parents:
19240
diff
changeset
|
145 { |
20116 | 146 return send_mpeg_pes_packet_ll(data, len, id, pts, type, NULL, 0, 0, my_write); |
19243
73443b22df17
final cleanup: implemented send_mpeg_pes_packet() and send_mpeg_lpcm_packet() using new send_mpeg_pes_packet_ll()
nicodvb
parents:
19240
diff
changeset
|
147 } |
73443b22df17
final cleanup: implemented send_mpeg_pes_packet() and send_mpeg_lpcm_packet() using new send_mpeg_pes_packet_ll()
nicodvb
parents:
19240
diff
changeset
|
148 |
73443b22df17
final cleanup: implemented send_mpeg_pes_packet() and send_mpeg_lpcm_packet() using new send_mpeg_pes_packet_ll()
nicodvb
parents:
19240
diff
changeset
|
149 |
19149
73d8ea32ebb8
cosmetics: comments and reindentation asked by gxben
nicodvb
parents:
19148
diff
changeset
|
150 /* Send MPEG <type> PS packet */ |
19141 | 151 int |
19243
73443b22df17
final cleanup: implemented send_mpeg_pes_packet() and send_mpeg_lpcm_packet() using new send_mpeg_pes_packet_ll()
nicodvb
parents:
19240
diff
changeset
|
152 send_mpeg_ps_packet(unsigned char *data, int len, int id, uint64_t pts, int type, |
25673
d576b679747b
Write functions used by send_mpeg_*_packet may _not_ modify data
reimar
parents:
25672
diff
changeset
|
153 int my_write (const unsigned char *data, int len)) |
19141 | 154 { |
19148
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
155 if(type == 2) |
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
156 my_write (ps2_header, sizeof (ps2_header)); |
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
157 else |
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
158 my_write (ps1_header, sizeof (ps1_header)); |
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
159 return send_mpeg_pes_packet (data, len, id, pts, type, my_write); |
19141 | 160 } |
161 | |
19149
73d8ea32ebb8
cosmetics: comments and reindentation asked by gxben
nicodvb
parents:
19148
diff
changeset
|
162 /* Send MPEG 2 LPCM packet */ |
19141 | 163 int |
19243
73443b22df17
final cleanup: implemented send_mpeg_pes_packet() and send_mpeg_lpcm_packet() using new send_mpeg_pes_packet_ll()
nicodvb
parents:
19240
diff
changeset
|
164 send_mpeg_lpcm_packet(unsigned char* data, int len, |
19141 | 165 int id, uint64_t pts, int freq_id, |
25673
d576b679747b
Write functions used by send_mpeg_*_packet may _not_ modify data
reimar
parents:
25672
diff
changeset
|
166 int my_write (const unsigned char *data, int len)) |
19141 | 167 { |
19243
73443b22df17
final cleanup: implemented send_mpeg_pes_packet() and send_mpeg_lpcm_packet() using new send_mpeg_pes_packet_ll()
nicodvb
parents:
19240
diff
changeset
|
168 unsigned char header[7] = {0xA0, 0x07, 0x00, 0x04, 0x0C, 1 | (freq_id << 4), 0x80}; |
19244
5e5d323b1934
re-added forgotten return in send_mpeg_lpcm_packet()
nicodvb
parents:
19243
diff
changeset
|
169 return send_mpeg_pes_packet_ll(data, len, 0xBD, pts, 2, header, sizeof(header), 1, my_write); |
19141 | 170 } |