2 * Copyright (C) 2006 Benjamin Zores
3 * Set of helper routines for building MPEG 1/2 PS/PES packets.
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>
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.
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.
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
30 #include "mpeg_packetizer.h"
32 #define PES_MAX_SIZE 2048
34 static const unsigned char ps2_header
[] = {
35 0x00, 0x00, 0x01, 0xba, 0x44, 0x00, 0x04, 0x00,
36 0x04, 0x01, 0x01, 0x86, 0xa3, 0xf8
40 static const unsigned char ps1_header
[] = {
41 0x00, 0x00, 0x01, 0xba, 0x21, 0x00,
42 0xb9, 0x37, 0x83, 0x80, 0xc3, 0x51,
45 /* Send MPEG <type> PES packet */
47 send_mpeg_pes_packet_ll(unsigned char *data
, int len
, int id
, uint64_t pts
,
48 int type
, unsigned char *header
, int header_len
,
49 int align4
, int my_write (const unsigned char *data
, int len
))
51 int ptslen
= (pts
? 5 : 0);
55 unsigned char pes_header
[PES_MAX_SIZE
];
57 mp_msg (MSGT_HEADER
, MSGL_DBG2
,
58 "MPEG%d PES packet: 0x%x => %lu \n", type
, id
, pts
);
59 memset (pes_header
, '\0', PES_MAX_SIZE
);
65 pes_header
[3] = id
; /* stream id */
69 int payload_size
= len
; /* data + PTS */
73 hdr
= (ptslen
? 0 : 1);
74 if (6 + hdr
+ ptslen
+ payload_size
+ header_len
> PES_MAX_SIZE
)
75 payload_size
= PES_MAX_SIZE
- 6 - hdr
- ptslen
- header_len
;
79 /* construct PES header: packetize */
80 plen
= payload_size
+ hdr
+ ptslen
+ header_len
;
81 pes_header
[4] = plen
>> 8;
82 pes_header
[5] = plen
& 255;
91 pes_header
[idx
++] = 0x81;
92 pes_header
[idx
++] = 0x80;
93 pes_header
[idx
++] = ptslen
;
96 /* presentation time stamp */
97 x
= (0x02 << 4) | (((pts
>> 30) & 0x07) << 1) | 1;
98 pes_header
[idx
++] = x
;
100 x
= ((((pts
>> 15) & 0x7fff) << 1) | 1);
101 pes_header
[idx
++] = x
>>8;
102 pes_header
[idx
++] = x
& 255;
104 x
= (((pts
& 0x7fff) << 1) | 1);
105 pes_header
[idx
++] = x
>> 8;
106 pes_header
[idx
++] = x
& 255;
112 pes_header
[idx
++] = 0x81;
113 pes_header
[idx
++] = 0x00;
114 pes_header
[idx
++] = 0x00;
117 pes_header
[idx
++] = 0x0f;
122 memcpy(&pes_header
[idx
], header
, header_len
);
126 my_write (pes_header
, idx
);
127 n
= my_write (data
, payload_size
);
131 ptslen
= 0; /* store PTS only once, at first packet! */
132 if(align4
&& len
< 4)
140 send_mpeg_pes_packet (unsigned char *data
, int len
, int id
, uint64_t pts
,
141 int type
, int my_write (const unsigned char *data
, int len
))
143 return send_mpeg_pes_packet_ll(data
, len
, id
, pts
, type
, NULL
, 0, 0, my_write
);
147 /* Send MPEG <type> PS packet */
149 send_mpeg_ps_packet(unsigned char *data
, int len
, int id
, uint64_t pts
, int type
,
150 int my_write (const unsigned char *data
, int len
))
153 my_write (ps2_header
, sizeof (ps2_header
));
155 my_write (ps1_header
, sizeof (ps1_header
));
156 return send_mpeg_pes_packet (data
, len
, id
, pts
, type
, my_write
);
159 /* Send MPEG 2 LPCM packet */
161 send_mpeg_lpcm_packet(unsigned char* data
, int len
,
162 int id
, uint64_t pts
, int freq_id
,
163 int my_write (const unsigned char *data
, int len
))
165 unsigned char header
[7] = {0xA0, 0x07, 0x00, 0x04, 0x0C, 1 | (freq_id
<< 4), 0x80};
166 return send_mpeg_pes_packet_ll(data
, len
, 0xBD, pts
, 2, header
, sizeof(header
), 1, my_write
);