Mercurial > mplayer.hg
annotate libmpdemux/mpeg_packetizer.c @ 26346:55f8d56ed7fe
Rename ASS make variable to LIBASS.
author | diego |
---|---|
date | Wed, 09 Apr 2008 18:26:44 +0000 |
parents | a721f266b21b |
children | 0c1db5fd3f79 |
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 | |
25672 | 34 static const unsigned char ps2_header[] = { |
19141 | 35 0x00, 0x00, 0x01, 0xba, 0x44, 0x00, 0x04, 0x00, |
36 0x04, 0x01, 0x01, 0x86, 0xa3, 0xf8 | |
37 }; | |
38 | |
39 | |
25672 | 40 static const unsigned char ps1_header[] = { |
19148
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
41 0x00, 0x00, 0x01, 0xba, 0x21, 0x00, |
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
42 0xb9, 0x37, 0x83, 0x80, 0xc3, 0x51, |
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
43 }; |
19141 | 44 |
19149
73d8ea32ebb8
cosmetics: comments and reindentation asked by gxben
nicodvb
parents:
19148
diff
changeset
|
45 /* 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
|
46 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
|
47 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
|
48 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
|
49 int align4, int my_write (const unsigned char *data, int len)) |
19141 | 50 { |
19240 | 51 int ptslen = (pts ? 5 : 0); |
19141 | 52 int n = 0; |
19148
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
53 int idx, plen; |
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
54 int hdr; |
25676
a721f266b21b
moved pes_header from file-static to send_mpeg_pes_packet_ll()
nicodvb
parents:
25673
diff
changeset
|
55 unsigned char pes_header[PES_MAX_SIZE]; |
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); |
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
|
74 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
|
75 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
|
76 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
|
77 payload_size &= ~3; |
19141 | 78 |
79 /* 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
|
80 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
|
81 pes_header[4] = plen >> 8; |
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
82 pes_header[5] = plen & 255; |
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
83 idx = 6; |
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
84 |
19141 | 85 if (ptslen) |
86 { | |
87 int x; | |
19148
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 if(type == 2) |
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
90 { |
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
91 pes_header[idx++] = 0x81; |
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
92 pes_header[idx++] = 0x80; |
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
93 pes_header[idx++] = ptslen; |
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
94 } |
19141 | 95 |
96 /* presentation time stamp */ | |
97 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
|
98 pes_header[idx++] = x; |
19141 | 99 |
100 x = ((((pts >> 15) & 0x7fff) << 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 >>8; |
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
102 pes_header[idx++] = x & 255; |
19141 | 103 |
104 x = (((pts & 0x7fff) << 1) | 1); | |
19148
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
105 pes_header[idx++] = x >> 8; |
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
106 pes_header[idx++] = x & 255; |
19141 | 107 } |
108 else | |
109 { | |
19148
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
110 if(type == 2) |
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
111 { |
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
112 pes_header[idx++] = 0x81; |
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
113 pes_header[idx++] = 0x00; |
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
114 pes_header[idx++] = 0x00; |
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
115 } |
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
116 else |
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
117 pes_header[idx++] = 0x0f; |
19141 | 118 } |
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
|
119 |
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
|
120 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
|
121 { |
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 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
|
123 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
|
124 } |
19148
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
125 |
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
126 my_write (pes_header, idx); |
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
127 n = my_write (data, payload_size); |
19141 | 128 |
19148
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
129 len -= n; |
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
130 data += n; |
19141 | 131 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
|
132 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
|
133 break; |
19141 | 134 } |
135 | |
136 return n; | |
137 } | |
138 | |
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
|
139 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
|
140 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
|
141 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
|
142 { |
20116 | 143 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
|
144 } |
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 |
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
|
146 |
19149
73d8ea32ebb8
cosmetics: comments and reindentation asked by gxben
nicodvb
parents:
19148
diff
changeset
|
147 /* Send MPEG <type> PS packet */ |
19141 | 148 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
|
149 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
|
150 int my_write (const unsigned char *data, int len)) |
19141 | 151 { |
19148
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
152 if(type == 2) |
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
153 my_write (ps2_header, sizeof (ps2_header)); |
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
154 else |
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
155 my_write (ps1_header, sizeof (ps1_header)); |
3b839a8d297a
simplified mpeg packetizer used by hw mpeg decoders/vo - first round
nicodvb
parents:
19141
diff
changeset
|
156 return send_mpeg_pes_packet (data, len, id, pts, type, my_write); |
19141 | 157 } |
158 | |
19149
73d8ea32ebb8
cosmetics: comments and reindentation asked by gxben
nicodvb
parents:
19148
diff
changeset
|
159 /* Send MPEG 2 LPCM packet */ |
19141 | 160 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
|
161 send_mpeg_lpcm_packet(unsigned char* data, int len, |
19141 | 162 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
|
163 int my_write (const unsigned char *data, int len)) |
19141 | 164 { |
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
|
165 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
|
166 return send_mpeg_pes_packet_ll(data, len, 0xBD, pts, 2, header, sizeof(header), 1, my_write); |
19141 | 167 } |