2006-07-19 21:46:06 +00:00
|
|
|
/*
|
2008-05-14 18:01:51 +00:00
|
|
|
* set of helper routines for building MPEG 1/2 PS/PES packets
|
2006-07-19 21:46:06 +00:00
|
|
|
*
|
2008-05-14 18:01:51 +00:00
|
|
|
* Copyright (C) 2006 Benjamin Zores
|
2006-07-19 21:46:06 +00:00
|
|
|
*
|
2008-05-14 18:01:51 +00:00
|
|
|
* Based on code borrowed from vo_mpegpes/vo_dxr2:
|
|
|
|
* (C) 2000 Ralph Metzler <ralph@convergence.de>
|
|
|
|
* Marcus Metzler <marcus@convergence.de>
|
|
|
|
* Gerard Lantau
|
2006-07-19 21:46:06 +00:00
|
|
|
*
|
2008-05-14 18:01:51 +00:00
|
|
|
* This file is part of MPlayer.
|
2006-07-19 21:46:06 +00:00
|
|
|
*
|
2008-05-14 18:01:51 +00:00
|
|
|
* MPlayer is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* MPlayer is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with MPlayer; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2006-07-19 21:46:06 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <inttypes.h>
|
|
|
|
|
|
|
|
#include "mp_msg.h"
|
|
|
|
#include "mpeg_packetizer.h"
|
|
|
|
|
|
|
|
#define PES_MAX_SIZE 2048
|
|
|
|
|
2008-01-12 15:55:52 +00:00
|
|
|
static const unsigned char ps2_header[] = {
|
2006-07-19 21:46:06 +00:00
|
|
|
0x00, 0x00, 0x01, 0xba, 0x44, 0x00, 0x04, 0x00,
|
|
|
|
0x04, 0x01, 0x01, 0x86, 0xa3, 0xf8
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2008-01-12 15:55:52 +00:00
|
|
|
static const unsigned char ps1_header[] = {
|
2006-07-22 09:45:34 +00:00
|
|
|
0x00, 0x00, 0x01, 0xba, 0x21, 0x00,
|
|
|
|
0xb9, 0x37, 0x83, 0x80, 0xc3, 0x51,
|
|
|
|
};
|
2006-07-19 21:46:06 +00:00
|
|
|
|
2006-07-22 09:59:44 +00:00
|
|
|
/* Send MPEG <type> PES packet */
|
2006-07-29 18:39:07 +00:00
|
|
|
static int
|
|
|
|
send_mpeg_pes_packet_ll(unsigned char *data, int len, int id, uint64_t pts,
|
|
|
|
int type, unsigned char *header, int header_len,
|
2008-01-12 16:05:04 +00:00
|
|
|
int align4, int my_write (const unsigned char *data, int len))
|
2006-07-19 21:46:06 +00:00
|
|
|
{
|
2006-07-29 16:21:14 +00:00
|
|
|
int ptslen = (pts ? 5 : 0);
|
2006-07-19 21:46:06 +00:00
|
|
|
int n = 0;
|
2006-07-22 09:45:34 +00:00
|
|
|
int idx, plen;
|
|
|
|
int hdr;
|
2008-01-13 09:34:43 +00:00
|
|
|
unsigned char pes_header[PES_MAX_SIZE];
|
2006-07-19 21:46:06 +00:00
|
|
|
|
|
|
|
mp_msg (MSGT_HEADER, MSGL_DBG2,
|
2008-07-08 08:17:50 +00:00
|
|
|
"MPEG%d PES packet: 0x%x => %"PRIu64" \n", type, id, pts);
|
2006-07-19 21:46:06 +00:00
|
|
|
memset (pes_header, '\0', PES_MAX_SIZE);
|
|
|
|
|
|
|
|
/* startcode */
|
|
|
|
pes_header[0] = 0;
|
|
|
|
pes_header[1] = 0;
|
|
|
|
pes_header[2] = 0x01;
|
|
|
|
pes_header[3] = id; /* stream id */
|
|
|
|
|
|
|
|
while (len > 0)
|
|
|
|
{
|
|
|
|
int payload_size = len; /* data + PTS */
|
2006-07-22 09:45:34 +00:00
|
|
|
if(type == 2)
|
|
|
|
hdr = 3;
|
|
|
|
else
|
|
|
|
hdr = (ptslen ? 0 : 1);
|
2006-07-29 18:39:07 +00:00
|
|
|
if (6 + hdr + ptslen + payload_size + header_len > PES_MAX_SIZE)
|
|
|
|
payload_size = PES_MAX_SIZE - 6 - hdr - ptslen - header_len;
|
|
|
|
if(align4)
|
|
|
|
payload_size &= ~3;
|
2006-07-19 21:46:06 +00:00
|
|
|
|
|
|
|
/* construct PES header: packetize */
|
2006-07-29 18:39:07 +00:00
|
|
|
plen = payload_size + hdr + ptslen + header_len;
|
2006-07-22 09:45:34 +00:00
|
|
|
pes_header[4] = plen >> 8;
|
|
|
|
pes_header[5] = plen & 255;
|
|
|
|
idx = 6;
|
|
|
|
|
2006-07-19 21:46:06 +00:00
|
|
|
if (ptslen)
|
|
|
|
{
|
|
|
|
int x;
|
2006-07-22 09:45:34 +00:00
|
|
|
|
|
|
|
if(type == 2)
|
|
|
|
{
|
|
|
|
pes_header[idx++] = 0x81;
|
|
|
|
pes_header[idx++] = 0x80;
|
|
|
|
pes_header[idx++] = ptslen;
|
|
|
|
}
|
2006-07-19 21:46:06 +00:00
|
|
|
|
|
|
|
/* presentation time stamp */
|
|
|
|
x = (0x02 << 4) | (((pts >> 30) & 0x07) << 1) | 1;
|
2006-07-22 09:45:34 +00:00
|
|
|
pes_header[idx++] = x;
|
2006-07-19 21:46:06 +00:00
|
|
|
|
|
|
|
x = ((((pts >> 15) & 0x7fff) << 1) | 1);
|
2006-07-22 09:45:34 +00:00
|
|
|
pes_header[idx++] = x >>8;
|
|
|
|
pes_header[idx++] = x & 255;
|
2006-07-19 21:46:06 +00:00
|
|
|
|
|
|
|
x = (((pts & 0x7fff) << 1) | 1);
|
2006-07-22 09:45:34 +00:00
|
|
|
pes_header[idx++] = x >> 8;
|
|
|
|
pes_header[idx++] = x & 255;
|
2006-07-19 21:46:06 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-07-22 09:45:34 +00:00
|
|
|
if(type == 2)
|
|
|
|
{
|
|
|
|
pes_header[idx++] = 0x81;
|
|
|
|
pes_header[idx++] = 0x00;
|
|
|
|
pes_header[idx++] = 0x00;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
pes_header[idx++] = 0x0f;
|
2006-07-19 21:46:06 +00:00
|
|
|
}
|
2006-07-29 18:39:07 +00:00
|
|
|
|
|
|
|
if(header_len)
|
|
|
|
{
|
|
|
|
memcpy(&pes_header[idx], header, header_len);
|
|
|
|
idx += header_len;
|
|
|
|
}
|
2006-07-22 09:45:34 +00:00
|
|
|
|
|
|
|
my_write (pes_header, idx);
|
|
|
|
n = my_write (data, payload_size);
|
2006-07-19 21:46:06 +00:00
|
|
|
|
2006-07-22 09:45:34 +00:00
|
|
|
len -= n;
|
|
|
|
data += n;
|
2006-07-19 21:46:06 +00:00
|
|
|
ptslen = 0; /* store PTS only once, at first packet! */
|
2006-07-29 18:39:07 +00:00
|
|
|
if(align4 && len < 4)
|
|
|
|
break;
|
2006-07-19 21:46:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2006-07-29 18:39:07 +00:00
|
|
|
int
|
|
|
|
send_mpeg_pes_packet (unsigned char *data, int len, int id, uint64_t pts,
|
2008-01-12 16:05:04 +00:00
|
|
|
int type, int my_write (const unsigned char *data, int len))
|
2006-07-29 18:39:07 +00:00
|
|
|
{
|
2006-10-08 22:43:29 +00:00
|
|
|
return send_mpeg_pes_packet_ll(data, len, id, pts, type, NULL, 0, 0, my_write);
|
2006-07-29 18:39:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-07-22 09:59:44 +00:00
|
|
|
/* Send MPEG <type> PS packet */
|
2006-07-19 21:46:06 +00:00
|
|
|
int
|
2006-07-29 18:39:07 +00:00
|
|
|
send_mpeg_ps_packet(unsigned char *data, int len, int id, uint64_t pts, int type,
|
2008-01-12 16:05:04 +00:00
|
|
|
int my_write (const unsigned char *data, int len))
|
2006-07-19 21:46:06 +00:00
|
|
|
{
|
2006-07-22 09:45:34 +00:00
|
|
|
if(type == 2)
|
|
|
|
my_write (ps2_header, sizeof (ps2_header));
|
|
|
|
else
|
|
|
|
my_write (ps1_header, sizeof (ps1_header));
|
|
|
|
return send_mpeg_pes_packet (data, len, id, pts, type, my_write);
|
2006-07-19 21:46:06 +00:00
|
|
|
}
|
|
|
|
|
2006-07-22 09:59:44 +00:00
|
|
|
/* Send MPEG 2 LPCM packet */
|
2006-07-19 21:46:06 +00:00
|
|
|
int
|
2006-07-29 18:39:07 +00:00
|
|
|
send_mpeg_lpcm_packet(unsigned char* data, int len,
|
2006-07-19 21:46:06 +00:00
|
|
|
int id, uint64_t pts, int freq_id,
|
2008-01-12 16:05:04 +00:00
|
|
|
int my_write (const unsigned char *data, int len))
|
2006-07-19 21:46:06 +00:00
|
|
|
{
|
2006-07-29 18:39:07 +00:00
|
|
|
unsigned char header[7] = {0xA0, 0x07, 0x00, 0x04, 0x0C, 1 | (freq_id << 4), 0x80};
|
2006-07-29 18:41:25 +00:00
|
|
|
return send_mpeg_pes_packet_ll(data, len, 0xBD, pts, 2, header, sizeof(header), 1, my_write);
|
2006-07-19 21:46:06 +00:00
|
|
|
}
|