mirror of
https://github.com/tuxbox-fork-migrations/recycled-ni-libstb-hal.git
synced 2025-08-30 17:00:57 +02:00
move libeplayer3-sh4 code to libeplayer3-sh4 directory; ...
rename library to libeplayer3_sh4.la
Origin commit data
------------------
Branch: master
Commit: 974a07b900
Author: vanhofen <vanhofen@gmx.de>
Date: 2018-12-23 (Sun, 23 Dec 2018)
Origin message was:
------------------
- move libeplayer3-sh4 code to libeplayer3-sh4 directory; ...
rename library to libeplayer3_sh4.la
------------------
This commit was generated by Migit
This commit is contained in:
185
libeplayer3-sh4/writer/aac.cpp
Normal file
185
libeplayer3-sh4/writer/aac.cpp
Normal file
@@ -0,0 +1,185 @@
|
||||
/*
|
||||
* linuxdvb output/writer handling.
|
||||
*
|
||||
* Copyright (C) 2010 konfetti (based on code from libeplayer2)
|
||||
* Copyright (C) 2014 DboxOldie (based on code from libeplayer3)
|
||||
* inspired by martii
|
||||
*
|
||||
* This program 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.
|
||||
*
|
||||
* This program 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 this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <sys/uio.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
#include "misc.h"
|
||||
#include "pes.h"
|
||||
#include "writer.h"
|
||||
|
||||
#define AAC_HEADER_LENGTH 7
|
||||
#define AAC_DEBUG 0
|
||||
|
||||
#if AAC_DEBUG
|
||||
static inline void Hexdump(unsigned char *Data, int length)
|
||||
{
|
||||
int k;
|
||||
for (k = 0; k < length; k++) {
|
||||
printf("%02x ", Data[k]);
|
||||
if (((k + 1) & 31) == 0)
|
||||
printf("\n");
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
}
|
||||
#endif
|
||||
|
||||
static inline int aac_get_sample_rate_index(uint32_t sample_rate)
|
||||
{
|
||||
if (96000 <= sample_rate)
|
||||
return 0;
|
||||
else if (88200 <= sample_rate)
|
||||
return 1;
|
||||
else if (64000 <= sample_rate)
|
||||
return 2;
|
||||
else if (48000 <= sample_rate)
|
||||
return 3;
|
||||
else if (44100 <= sample_rate)
|
||||
return 4;
|
||||
else if (32000 <= sample_rate)
|
||||
return 5;
|
||||
else if (24000 <= sample_rate)
|
||||
return 6;
|
||||
else if (22050 <= sample_rate)
|
||||
return 7;
|
||||
else if (16000 <= sample_rate)
|
||||
return 8;
|
||||
else if (12000 <= sample_rate)
|
||||
return 9;
|
||||
else if (11025 <= sample_rate)
|
||||
return 10;
|
||||
else if (8000 <= sample_rate)
|
||||
return 11;
|
||||
else if (7350 <= sample_rate)
|
||||
return 12;
|
||||
else
|
||||
return 13;
|
||||
}
|
||||
|
||||
#if 0
|
||||
static unsigned char DefaultAACHeader[] = {0xff,0xf1,0x50,0x80,0x00,0x1f,0xfc};
|
||||
#endif
|
||||
|
||||
class WriterAAC : public Writer
|
||||
{
|
||||
private:
|
||||
uint8_t aacbuf[8];
|
||||
unsigned int aacbuflen;
|
||||
AVStream *stream;
|
||||
public:
|
||||
bool Write(AVPacket *packet, int64_t pts);
|
||||
void Init(int _fd, AVStream *_stream, Player *_player);
|
||||
WriterAAC();
|
||||
};
|
||||
|
||||
void WriterAAC::Init(int _fd, AVStream *_stream, Player *_player)
|
||||
{
|
||||
fd = _fd;
|
||||
stream = _stream;
|
||||
player = _player;
|
||||
#if AAC_DEBUG
|
||||
printf("Create AAC ExtraData\n");
|
||||
printf("stream->codec->extradata_size %d\n", stream->codec->extradata_size);
|
||||
Hexdump(stream->codec->extradata, stream->codec->extradata_size);
|
||||
#endif
|
||||
unsigned int object_type = 2; // LC
|
||||
unsigned int sample_index = aac_get_sample_rate_index(stream->codec->sample_rate);
|
||||
unsigned int chan_config = stream->codec->channels;
|
||||
if (stream->codec->extradata_size >= 2)
|
||||
{
|
||||
object_type = stream->codec->extradata[0] >> 3;
|
||||
sample_index = ((stream->codec->extradata[0] & 0x7) << 1) + (stream->codec->extradata[1] >> 7);
|
||||
chan_config = (stream->codec->extradata[1] >> 3) && 0xf;
|
||||
}
|
||||
#if AAC_DEBUG
|
||||
printf("aac object_type %d\n", object_type);
|
||||
printf("aac sample_index %d\n", sample_index);
|
||||
printf("aac chan_config %d\n", chan_config);
|
||||
#endif
|
||||
object_type -= 1; // Cause of ADTS
|
||||
aacbuflen = AAC_HEADER_LENGTH;
|
||||
aacbuf[0] = 0xFF;
|
||||
aacbuf[1] = 0xF1;
|
||||
aacbuf[2] = ((object_type & 0x03) << 6) | (sample_index << 2) | ((chan_config >> 2) & 0x01);
|
||||
aacbuf[3] = (chan_config & 0x03) << 6;
|
||||
aacbuf[4] = 0x00;
|
||||
aacbuf[5] = 0x1F;
|
||||
aacbuf[6] = 0xFC;
|
||||
aacbuf[7] = 0x00;
|
||||
#if AAC_DEBUG
|
||||
printf("AAC_HEADER -> ");
|
||||
Hexdump(aacbuf, 7);
|
||||
#endif
|
||||
}
|
||||
|
||||
bool WriterAAC::Write(AVPacket *packet, int64_t pts)
|
||||
{
|
||||
if (!packet || !packet->data)
|
||||
return false;
|
||||
|
||||
uint8_t PesHeader[PES_MAX_HEADER_SIZE];
|
||||
uint8_t ExtraData[AAC_HEADER_LENGTH];
|
||||
|
||||
for (int pos = 0; pos < packet->size + AAC_HEADER_LENGTH; )
|
||||
{
|
||||
int PacketLength = std::min(packet->size - pos + AAC_HEADER_LENGTH, MAX_PES_PACKET_SIZE);
|
||||
|
||||
memcpy(ExtraData, aacbuf, AAC_HEADER_LENGTH);
|
||||
|
||||
// ExtraData[3] |= (PacketLength >> 11) & 0x3;
|
||||
ExtraData[4] = (PacketLength >> 3) & 0xff;
|
||||
ExtraData[5] |= (PacketLength << 5) & 0xe0;
|
||||
|
||||
struct iovec iov[3];
|
||||
iov[0].iov_base = PesHeader;
|
||||
iov[0].iov_len = InsertPesHeader(PesHeader, PacketLength, AAC_AUDIO_PES_START_CODE, pts, 0);
|
||||
iov[1].iov_base = ExtraData;
|
||||
iov[1].iov_len = AAC_HEADER_LENGTH;
|
||||
iov[2].iov_base = packet->data;
|
||||
iov[2].iov_len = packet->size;
|
||||
|
||||
ssize_t l = writev(fd, iov, 3);
|
||||
#if AAC_DEBUG
|
||||
// printf("Packet Size + AAC_HEADER_LENGTH= %d Packet Size= %d Written= %d\n", PacketLength, packet->size, l);
|
||||
#endif
|
||||
if (l < 0)
|
||||
return false;
|
||||
pos += PacketLength;
|
||||
pts = INVALID_PTS_VALUE;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
WriterAAC::WriterAAC()
|
||||
{
|
||||
Register(this, AV_CODEC_ID_AAC, AUDIO_ENCODING_AAC);
|
||||
}
|
||||
|
||||
static WriterAAC writer_aac __attribute__ ((init_priority (300)));
|
72
libeplayer3-sh4/writer/ac3.cpp
Normal file
72
libeplayer3-sh4/writer/ac3.cpp
Normal file
@@ -0,0 +1,72 @@
|
||||
/*
|
||||
* linuxdvb output/writer handling
|
||||
*
|
||||
* Copyright (C) 2010 konfetti (based on code from libeplayer2)
|
||||
* Copyright (C) 2014 martii (based on code from libeplayer3)
|
||||
*
|
||||
* This program 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.
|
||||
*
|
||||
* This program 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 this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <sys/uio.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
#include "misc.h"
|
||||
#include "pes.h"
|
||||
#include "writer.h"
|
||||
|
||||
class WriterAC3 : public Writer
|
||||
{
|
||||
public:
|
||||
bool Write(AVPacket *packet, int64_t pts);
|
||||
WriterAC3();
|
||||
};
|
||||
|
||||
bool WriterAC3::Write(AVPacket *packet, int64_t pts)
|
||||
{
|
||||
if (!packet || !packet->data)
|
||||
return false;
|
||||
|
||||
uint8_t PesHeader[PES_MAX_HEADER_SIZE];
|
||||
|
||||
for (int pos = 0; pos < packet->size; ) {
|
||||
int PacketLength = std::min(packet->size - pos, MAX_PES_PACKET_SIZE);
|
||||
struct iovec iov[2];
|
||||
iov[0].iov_base = PesHeader;
|
||||
iov[0].iov_len = InsertPesHeader(PesHeader, PacketLength, PRIVATE_STREAM_1_PES_START_CODE, pts, 0);
|
||||
iov[1].iov_base = packet->data + pos;
|
||||
iov[1].iov_len = PacketLength;
|
||||
|
||||
ssize_t l = writev(fd, iov, 2);
|
||||
if (l < 0)
|
||||
return false;
|
||||
pos += PacketLength;
|
||||
pts = INVALID_PTS_VALUE;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
WriterAC3::WriterAC3()
|
||||
{
|
||||
Register(this, AV_CODEC_ID_AC3, AUDIO_ENCODING_AC3);
|
||||
Register(this, AV_CODEC_ID_EAC3, AUDIO_ENCODING_AC3);
|
||||
}
|
||||
|
||||
static WriterAC3 writer_ac3 __attribute__ ((init_priority (300)));
|
110
libeplayer3-sh4/writer/divx.cpp
Normal file
110
libeplayer3-sh4/writer/divx.cpp
Normal file
@@ -0,0 +1,110 @@
|
||||
/*
|
||||
* linuxdvb output/writer handling
|
||||
*
|
||||
* Copyright (C) 2010 konfetti (based on code from libeplayer2)
|
||||
* Copyright (C) 2014 martii (based on code from libeplayer3)
|
||||
*
|
||||
* This program 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.
|
||||
*
|
||||
* This program 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 this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/uio.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "misc.h"
|
||||
#include "pes.h"
|
||||
#include "writer.h"
|
||||
|
||||
class WriterDIVX : public Writer
|
||||
{
|
||||
private:
|
||||
bool initialHeader;
|
||||
AVStream *stream;
|
||||
public:
|
||||
bool Write(AVPacket *packet, int64_t pts);
|
||||
void Init(int fd, AVStream *_stream, Player *player);
|
||||
WriterDIVX();
|
||||
};
|
||||
|
||||
void WriterDIVX::Init(int _fd, AVStream *_stream, Player *_player)
|
||||
{
|
||||
fd = _fd;
|
||||
stream = _stream;
|
||||
player = _player;
|
||||
initialHeader = true;
|
||||
}
|
||||
|
||||
bool WriterDIVX::Write(AVPacket *packet, int64_t pts)
|
||||
{
|
||||
if (!packet || !packet->data)
|
||||
return false;
|
||||
|
||||
uint8_t PesHeader[PES_MAX_HEADER_SIZE];
|
||||
uint8_t FakeHeaders[64] = { 0 }; // 64bytes should be enough to make the fake headers
|
||||
unsigned int FakeHeaderLength;
|
||||
uint8_t Version = 5;
|
||||
unsigned int FakeStartCode = (Version << 8) | PES_VERSION_FAKE_START_CODE;
|
||||
BitPacker_t ld = { FakeHeaders, 0, 32 };
|
||||
|
||||
unsigned int usecPerFrame = av_rescale(AV_TIME_BASE, stream->r_frame_rate.den, stream->r_frame_rate.num);
|
||||
|
||||
/* Create info record for frame parser */
|
||||
/* divx4 & 5
|
||||
VOS
|
||||
PutBits(&ld, 0x0, 8);
|
||||
PutBits(&ld, 0x0, 8);
|
||||
*/
|
||||
PutBits(&ld, 0x1b0, 32); // startcode
|
||||
PutBits(&ld, 0, 8); // profile = reserved
|
||||
PutBits(&ld, 0x1b2, 32); // startcode (user data)
|
||||
PutBits(&ld, 0x53545443, 32); // STTC - an embedded ST timecode from an avi file
|
||||
PutBits(&ld, usecPerFrame, 32); // microseconds per frame
|
||||
FlushBits(&ld);
|
||||
|
||||
FakeHeaderLength = (ld.Ptr - FakeHeaders);
|
||||
|
||||
struct iovec iov[4];
|
||||
int ic = 0;
|
||||
iov[ic].iov_base = PesHeader;
|
||||
iov[ic++].iov_len = InsertPesHeader(PesHeader, packet->size, MPEG_VIDEO_PES_START_CODE, pts, FakeStartCode);
|
||||
iov[ic].iov_base = FakeHeaders;
|
||||
iov[ic++].iov_len = FakeHeaderLength;
|
||||
|
||||
if (initialHeader) {
|
||||
iov[ic].iov_base = stream->codec->extradata;
|
||||
iov[ic++].iov_len = stream->codec->extradata_size;
|
||||
initialHeader = false;
|
||||
}
|
||||
iov[ic].iov_base = packet->data;
|
||||
iov[ic++].iov_len = packet->size;
|
||||
|
||||
return writev(fd, iov, ic) > -1;
|
||||
}
|
||||
|
||||
WriterDIVX::WriterDIVX()
|
||||
{
|
||||
Register(this, AV_CODEC_ID_MPEG4, VIDEO_ENCODING_MPEG4P2);
|
||||
Register(this, AV_CODEC_ID_MSMPEG4V1, VIDEO_ENCODING_MPEG4P2);
|
||||
Register(this, AV_CODEC_ID_MSMPEG4V2, VIDEO_ENCODING_MPEG4P2);
|
||||
Register(this, AV_CODEC_ID_MSMPEG4V3, VIDEO_ENCODING_MPEG4P2);
|
||||
}
|
||||
|
||||
static WriterDIVX writer_divx __attribute__ ((init_priority (300)));
|
82
libeplayer3-sh4/writer/dts.cpp
Normal file
82
libeplayer3-sh4/writer/dts.cpp
Normal file
@@ -0,0 +1,82 @@
|
||||
/*
|
||||
* linuxdvb output/writer handling
|
||||
*
|
||||
* Copyright (C) 2010 konfetti (based on code from libeplayer2)
|
||||
* Copyright (C) 2014 martii (based on code from libeplayer3)
|
||||
*
|
||||
* This program 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.
|
||||
*
|
||||
* This program 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 this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <sys/uio.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "misc.h"
|
||||
#include "pes.h"
|
||||
#include "writer.h"
|
||||
|
||||
#define PES_AUDIO_PRIVATE_HEADER_SIZE 16 // consider maximum private header size.
|
||||
#define PES_AUDIO_HEADER_SIZE (32 + PES_AUDIO_PRIVATE_HEADER_SIZE)
|
||||
|
||||
class WriterDTS : public Writer
|
||||
{
|
||||
public:
|
||||
bool Write(AVPacket *packet, int64_t pts);
|
||||
WriterDTS();
|
||||
};
|
||||
|
||||
bool WriterDTS::Write(AVPacket *packet, int64_t pts)
|
||||
{
|
||||
if (!packet || !packet->data)
|
||||
return false;
|
||||
|
||||
uint8_t PesHeader[PES_AUDIO_HEADER_SIZE];
|
||||
|
||||
// #define DO_BYTESWAP
|
||||
#ifdef DO_BYTESWAP
|
||||
uint8_t Data[packet->size];
|
||||
memcpy(Data, packet->data, packet->size);
|
||||
|
||||
/* 16-bit byte swap all data before injecting it */
|
||||
for (i = 0; i < packet->size; i += 2) {
|
||||
uint8_t Tmp = Data[i];
|
||||
Data[i] = Data[i + 1];
|
||||
Data[i + 1] = Tmp;
|
||||
}
|
||||
#endif
|
||||
|
||||
struct iovec iov[2];
|
||||
|
||||
iov[0].iov_base = PesHeader;
|
||||
iov[0].iov_len = InsertPesHeader(PesHeader, packet->size, MPEG_AUDIO_PES_START_CODE /*PRIVATE_STREAM_1_PES_START_CODE */ , pts, 0);
|
||||
#ifdef DO_BYTESPWAP
|
||||
iov[1].iov_base = Data;
|
||||
#else
|
||||
iov[1].iov_base = packet->data;
|
||||
#endif
|
||||
iov[1].iov_len = packet->size;
|
||||
|
||||
return writev(fd, iov, 2) > -1;
|
||||
}
|
||||
|
||||
WriterDTS::WriterDTS()
|
||||
{
|
||||
Register(this, AV_CODEC_ID_DTS, AUDIO_ENCODING_DTS);
|
||||
}
|
||||
|
||||
static WriterDTS writer_dts __attribute__ ((init_priority (300)));
|
74
libeplayer3-sh4/writer/h263.cpp
Normal file
74
libeplayer3-sh4/writer/h263.cpp
Normal file
@@ -0,0 +1,74 @@
|
||||
/*
|
||||
* linuxdvb output/writer handling
|
||||
*
|
||||
* Copyright (C) 2010 crow
|
||||
* Copyright (C) 2010 konfetti (based on code from libeplayer2)
|
||||
* Copyright (C) 2014 martii (based on code from libeplayer3)
|
||||
*
|
||||
* This program 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.
|
||||
*
|
||||
* This program 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 this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <errno.h>
|
||||
#include <sys/uio.h>
|
||||
|
||||
#include "misc.h"
|
||||
#include "pes.h"
|
||||
#include "writer.h"
|
||||
|
||||
class WriterH263 : public Writer
|
||||
{
|
||||
public:
|
||||
bool Write(AVPacket *packet, int64_t pts);
|
||||
WriterH263();
|
||||
};
|
||||
|
||||
bool WriterH263::Write(AVPacket *packet, int64_t pts)
|
||||
{
|
||||
if (!packet || !packet->data)
|
||||
return false;
|
||||
uint8_t PesHeader[PES_MAX_HEADER_SIZE];
|
||||
|
||||
int HeaderLength = InsertPesHeader(PesHeader, packet->size, H263_VIDEO_PES_START_CODE, pts, 0);
|
||||
|
||||
int PrivateHeaderLength = InsertVideoPrivateDataHeader(&PesHeader[HeaderLength], packet->size);
|
||||
|
||||
int PesLength = PesHeader[PES_LENGTH_BYTE_0] + (PesHeader[PES_LENGTH_BYTE_1] << 8) + PrivateHeaderLength;
|
||||
|
||||
PesHeader[PES_LENGTH_BYTE_0] = PesLength & 0xff;
|
||||
PesHeader[PES_LENGTH_BYTE_1] = (PesLength >> 8) & 0xff;
|
||||
PesHeader[PES_HEADER_DATA_LENGTH_BYTE] += PrivateHeaderLength;
|
||||
PesHeader[PES_FLAGS_BYTE] |= PES_EXTENSION_DATA_PRESENT;
|
||||
|
||||
HeaderLength += PrivateHeaderLength;
|
||||
|
||||
struct iovec iov[2];
|
||||
iov[0].iov_base = PesHeader;
|
||||
iov[0].iov_len = HeaderLength;
|
||||
iov[1].iov_base = packet->data;
|
||||
iov[1].iov_len = packet->size;
|
||||
return writev(fd, iov, 2) > -1;
|
||||
}
|
||||
|
||||
WriterH263::WriterH263()
|
||||
{
|
||||
Register(this, AV_CODEC_ID_H263, VIDEO_ENCODING_H263);
|
||||
Register(this, AV_CODEC_ID_H263P, VIDEO_ENCODING_H263);
|
||||
Register(this, AV_CODEC_ID_H263I, VIDEO_ENCODING_H263);
|
||||
Register(this, AV_CODEC_ID_FLV1, VIDEO_ENCODING_FLV1);
|
||||
}
|
||||
|
||||
static WriterH263 writer_h263 __attribute__ ((init_priority (300)));
|
258
libeplayer3-sh4/writer/h264.cpp
Normal file
258
libeplayer3-sh4/writer/h264.cpp
Normal file
@@ -0,0 +1,258 @@
|
||||
/*
|
||||
* linuxdvb output/writer handling
|
||||
*
|
||||
* Copyright (C) 2010 konfetti (based on code from libeplayer2)
|
||||
* Copyright (C) 2014 martii (based on code from libeplayer3)
|
||||
*
|
||||
* This program 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.
|
||||
*
|
||||
* This program 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 this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <sys/uio.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "misc.h"
|
||||
#include "pes.h"
|
||||
#include "writer.h"
|
||||
|
||||
#define NALU_TYPE_PLAYER2_CONTAINER_PARAMETERS 24 // Reference: player/standards/h264.h
|
||||
#define CONTAINER_PARAMETERS_VERSION 0x00
|
||||
|
||||
typedef struct avcC_s {
|
||||
uint8_t Version; // configurationVersion
|
||||
uint8_t Profile; // AVCProfileIndication
|
||||
uint8_t Compatibility; // profile_compatibility
|
||||
uint8_t Level; // AVCLevelIndication
|
||||
uint8_t NalLengthMinusOne; // held in bottom two bits
|
||||
uint8_t NumParamSets; // held in bottom 5 bits
|
||||
uint8_t Params[1]; // {length,params}{length,params}...sequence then picture
|
||||
} avcC_t;
|
||||
|
||||
class WriterH264 : public Writer
|
||||
{
|
||||
private:
|
||||
bool initialHeader;
|
||||
unsigned int NalLengthBytes;
|
||||
AVStream *stream;
|
||||
public:
|
||||
bool Write(AVPacket *packet, int64_t pts);
|
||||
void Init(int _fd, AVStream *_stream, Player *_player);
|
||||
WriterH264();
|
||||
};
|
||||
|
||||
void WriterH264::Init(int _fd, AVStream *_stream, Player *_player)
|
||||
{
|
||||
fd = _fd;
|
||||
stream = _stream;
|
||||
player = _player;
|
||||
initialHeader = true;
|
||||
NalLengthBytes = 1;
|
||||
}
|
||||
|
||||
bool WriterH264::Write(AVPacket *packet, int64_t pts)
|
||||
{
|
||||
if (!packet || !packet->data)
|
||||
return false;
|
||||
uint8_t PesHeader[PES_MAX_HEADER_SIZE];
|
||||
struct iovec iov[512];
|
||||
|
||||
uint8_t *d = packet->data;
|
||||
|
||||
// byte-stream format
|
||||
if ((packet->size > 3) && ( (d[0] == 0x00 && d[1] == 0x00 && d[2] == 0x00 && d[3] == 0x01) // first NAL unit
|
||||
|| (d[0] == 0xff && d[1] == 0xff && d[2] == 0xff && d[3] == 0xff) // FIXME, needed???
|
||||
)) {
|
||||
unsigned int FakeStartCode = /* (call->Version << 8) | */ PES_VERSION_FAKE_START_CODE;
|
||||
int ic = 0;
|
||||
iov[ic++].iov_base = PesHeader;
|
||||
unsigned int len = 0;
|
||||
if (initialHeader) {
|
||||
initialHeader = false;
|
||||
iov[ic].iov_base = stream->codec->extradata;
|
||||
iov[ic++].iov_len = stream->codec->extradata_size;
|
||||
len += stream->codec->extradata_size;
|
||||
}
|
||||
iov[ic].iov_base = packet->data;
|
||||
iov[ic++].iov_len = packet->size;
|
||||
len += packet->size;
|
||||
#if 1 // FIXME: needed?
|
||||
// Hellmaster1024:
|
||||
// some packets will only be accepted by the player if we send one byte more than data is available.
|
||||
// The content of this byte does not matter. It will be ignored by the player
|
||||
iov[ic].iov_base = (void *) "";
|
||||
iov[ic++].iov_len = 1;
|
||||
len++;
|
||||
#endif
|
||||
iov[0].iov_len = InsertPesHeader(PesHeader, len, MPEG_VIDEO_PES_START_CODE, pts, FakeStartCode);
|
||||
return writev(fd, iov, ic) > -1;
|
||||
}
|
||||
|
||||
// convert NAL units without sync byte sequence to byte-stream format
|
||||
if (initialHeader) {
|
||||
avcC_t *avcCHeader = (avcC_t *) stream->codec->extradata;
|
||||
|
||||
if (!avcCHeader) {
|
||||
fprintf(stderr, "stream->codec->extradata == NULL\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (avcCHeader->Version != 1)
|
||||
fprintf(stderr, "Error unknown avcC version (%x). Expect problems.\n", avcCHeader->Version);
|
||||
|
||||
// The player will use FrameRate and TimeScale to calculate the default frame rate.
|
||||
// FIXME: TimeDelta should be used instead of FrameRate. This is a historic implementation bug.
|
||||
// Reference: player/frame_parser/frame_parser_video_h264.cpp FrameParser_VideoH264_c::ReadPlayer2ContainerParameters()
|
||||
unsigned int FrameRate = av_rescale(1000ll, stream->r_frame_rate.num, stream->r_frame_rate.den);
|
||||
unsigned int TimeScale = (FrameRate < 23970) ? 1001 : 1000; /* FIXME: revise this */
|
||||
|
||||
uint8_t Header[20];
|
||||
unsigned int len = 0;
|
||||
Header[len++] = 0x00; // Start code, 00 00 00 01 for first NAL unit
|
||||
Header[len++] = 0x00;
|
||||
Header[len++] = 0x00;
|
||||
Header[len++] = 0x01;
|
||||
Header[len++] = NALU_TYPE_PLAYER2_CONTAINER_PARAMETERS; // NAL unit header
|
||||
// Container message version - changes when/if we vary the format of the message
|
||||
Header[len++] = CONTAINER_PARAMETERS_VERSION;
|
||||
Header[len++] = 0xff; // marker bits
|
||||
|
||||
#if 0
|
||||
if (FrameRate == 0xffffffff)
|
||||
FrameRate = (TimeScale > 1000) ? 1001 : 1;
|
||||
#endif
|
||||
|
||||
Header[len++] = (TimeScale >> 24) & 0xff; // Output the timescale
|
||||
Header[len++] = (TimeScale >> 16) & 0xff;
|
||||
Header[len++] = 0xff; // marker bits
|
||||
Header[len++] = (TimeScale >> 8) & 0xff;
|
||||
Header[len++] = (TimeScale ) & 0xff;
|
||||
Header[len++] = 0xff; // marker bits
|
||||
|
||||
Header[len++] = (FrameRate >> 24) & 0xff; // Output frame period (should be: time delta)
|
||||
Header[len++] = (FrameRate >> 16) & 0xff;
|
||||
Header[len++] = 0xff; // marker bits
|
||||
Header[len++] = (FrameRate >> 8) & 0xff;
|
||||
Header[len++] = (FrameRate ) & 0xff;
|
||||
Header[len++] = 0xff; // marker bits
|
||||
|
||||
Header[len++] = 0x80; // Rsbp trailing bits
|
||||
|
||||
int ic = 0;
|
||||
iov[ic].iov_base = PesHeader;
|
||||
iov[ic++].iov_len = InsertPesHeader(PesHeader, len, MPEG_VIDEO_PES_START_CODE, INVALID_PTS_VALUE, 0);
|
||||
iov[ic].iov_base = Header;
|
||||
iov[ic++].iov_len = len;
|
||||
if (writev(fd, iov, ic) < 0)
|
||||
return false;
|
||||
|
||||
ic = 0;
|
||||
iov[ic++].iov_base = PesHeader;
|
||||
|
||||
NalLengthBytes = (avcCHeader->NalLengthMinusOne & 0x03) + 1;
|
||||
unsigned int ParamOffset = 0;
|
||||
len = 0;
|
||||
|
||||
// sequence parameter set
|
||||
unsigned int ParamSets = avcCHeader->NumParamSets & 0x1f;
|
||||
for (unsigned int i = 0; i < ParamSets; i++) {
|
||||
unsigned int PsLength = (avcCHeader->Params[ParamOffset] << 8) | avcCHeader->Params[ParamOffset + 1];
|
||||
|
||||
iov[ic].iov_base = (uint8_t *) "\0\0\0\1";
|
||||
iov[ic++].iov_len = 4;
|
||||
len += 4;
|
||||
iov[ic].iov_base = &avcCHeader->Params[ParamOffset + 2];
|
||||
iov[ic++].iov_len = PsLength;
|
||||
len += PsLength;
|
||||
ParamOffset += PsLength + 2;
|
||||
}
|
||||
|
||||
// picture parameter set
|
||||
ParamSets = avcCHeader->Params[ParamOffset++];
|
||||
|
||||
for (unsigned int i = 0; i < ParamSets; i++) {
|
||||
unsigned int PsLength = (avcCHeader->Params[ParamOffset] << 8) | avcCHeader->Params[ParamOffset + 1];
|
||||
|
||||
iov[ic].iov_base = (uint8_t *) "\0\0\0\1";
|
||||
iov[ic++].iov_len = 4;
|
||||
len += 4;
|
||||
iov[ic].iov_base = &avcCHeader->Params[ParamOffset + 2];
|
||||
iov[ic++].iov_len = PsLength;
|
||||
len += PsLength;
|
||||
ParamOffset += PsLength + 2;
|
||||
}
|
||||
|
||||
iov[0].iov_len = InsertPesHeader(PesHeader, len, MPEG_VIDEO_PES_START_CODE, INVALID_PTS_VALUE, 0);
|
||||
ssize_t l = writev(fd, iov, ic);
|
||||
if (l < 0)
|
||||
return false;
|
||||
|
||||
initialHeader = false;
|
||||
}
|
||||
|
||||
uint8_t *de = d + packet->size;
|
||||
do {
|
||||
unsigned int len = 0;
|
||||
switch (NalLengthBytes) {
|
||||
case 4:
|
||||
len = *d;
|
||||
d++;
|
||||
case 3:
|
||||
len <<= 8;
|
||||
len |= *d;
|
||||
d++;
|
||||
case 2:
|
||||
len <<= 8;
|
||||
len |= *d;
|
||||
d++;
|
||||
default:
|
||||
len <<= 8;
|
||||
len |= *d;
|
||||
d++;
|
||||
}
|
||||
|
||||
if (d + len > de) {
|
||||
fprintf(stderr, "NAL length past end of buffer - size %u frame offset %d left %d\n", len, (int) (d - packet->data), (int) (de - d));
|
||||
break;
|
||||
}
|
||||
|
||||
int ic = 0;
|
||||
iov[ic++].iov_base = PesHeader;
|
||||
iov[ic].iov_base = (uint8_t *) "\0\0\0\1";
|
||||
iov[ic++].iov_len = 4;
|
||||
|
||||
iov[ic].iov_base = d;
|
||||
iov[ic++].iov_len = len;
|
||||
iov[0].iov_len = InsertPesHeader(PesHeader, len + 3, MPEG_VIDEO_PES_START_CODE, pts, 0);
|
||||
ssize_t l = writev(fd, iov, ic);
|
||||
if (l < 0)
|
||||
return false;
|
||||
|
||||
d += len;
|
||||
pts = INVALID_PTS_VALUE;
|
||||
|
||||
} while (d < de);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
WriterH264::WriterH264()
|
||||
{
|
||||
Register(this, AV_CODEC_ID_H264, VIDEO_ENCODING_H264);
|
||||
}
|
||||
|
||||
static WriterH264 writerh264 __attribute__ ((init_priority (300)));
|
88
libeplayer3-sh4/writer/misc.cpp
Normal file
88
libeplayer3-sh4/writer/misc.cpp
Normal file
@@ -0,0 +1,88 @@
|
||||
/*
|
||||
* LinuxDVB Output handling.
|
||||
*
|
||||
* This program 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.
|
||||
*
|
||||
* This program 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 this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
#include <memory.h>
|
||||
#include <asm/types.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "misc.h"
|
||||
|
||||
void PutBits(BitPacker_t * ld, unsigned int code, unsigned int length)
|
||||
{
|
||||
unsigned int bit_buf;
|
||||
unsigned int bit_left;
|
||||
|
||||
bit_buf = ld->BitBuffer;
|
||||
bit_left = ld->Remaining;
|
||||
|
||||
#ifdef DEBUG_PUTBITS
|
||||
if (ld->debug)
|
||||
dprintf("code = %d, length = %d, bit_buf = 0x%x, bit_left = %d\n",
|
||||
code, length, bit_buf, bit_left);
|
||||
#endif
|
||||
|
||||
if (length < bit_left) {
|
||||
/* fits into current buffer */
|
||||
bit_buf = (bit_buf << length) | code;
|
||||
bit_left -= length;
|
||||
} else {
|
||||
/* doesn't fit */
|
||||
bit_buf <<= bit_left;
|
||||
bit_buf |= code >> (length - bit_left);
|
||||
ld->Ptr[0] = (uint8_t) (bit_buf >> 24);
|
||||
ld->Ptr[1] = (uint8_t) (bit_buf >> 16);
|
||||
ld->Ptr[2] = (uint8_t) (bit_buf >> 8);
|
||||
ld->Ptr[3] = (uint8_t) bit_buf;
|
||||
ld->Ptr += 4;
|
||||
length -= bit_left;
|
||||
bit_buf = code & ((1 << length) - 1);
|
||||
bit_left = 32 - length;
|
||||
bit_buf = code;
|
||||
}
|
||||
|
||||
#ifdef DEBUG_PUTBITS
|
||||
if (ld->debug)
|
||||
dprintf("bit_left = %d, bit_buf = 0x%x\n", bit_left, bit_buf);
|
||||
#endif
|
||||
|
||||
/* writeback */
|
||||
ld->BitBuffer = bit_buf;
|
||||
ld->Remaining = bit_left;
|
||||
}
|
||||
|
||||
void FlushBits(BitPacker_t * ld)
|
||||
{
|
||||
ld->BitBuffer <<= ld->Remaining;
|
||||
while (ld->Remaining < 32) {
|
||||
#ifdef DEBUG_PUTBITS
|
||||
if (ld->debug)
|
||||
dprintf("flushing 0x%2.2x\n", ld->BitBuffer >> 24);
|
||||
#endif
|
||||
*ld->Ptr++ = ld->BitBuffer >> 24;
|
||||
ld->BitBuffer <<= 8;
|
||||
ld->Remaining += 8;
|
||||
}
|
||||
ld->Remaining = 32;
|
||||
ld->BitBuffer = 0;
|
||||
}
|
74
libeplayer3-sh4/writer/mp3.cpp
Normal file
74
libeplayer3-sh4/writer/mp3.cpp
Normal file
@@ -0,0 +1,74 @@
|
||||
/*
|
||||
* linuxdvb output/writer handling
|
||||
*
|
||||
* Copyright (C) 2010 konfetti (based on code from libeplayer2)
|
||||
* Copyright (C) 2014 martii (based on code from libeplayer3)
|
||||
*
|
||||
* This program 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.
|
||||
*
|
||||
* This program 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 this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <sys/uio.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
#include "misc.h"
|
||||
#include "pes.h"
|
||||
#include "writer.h"
|
||||
|
||||
class WriterMP3 : public Writer
|
||||
{
|
||||
public:
|
||||
bool Write(AVPacket *packet, int64_t pts);
|
||||
WriterMP3();
|
||||
};
|
||||
|
||||
bool WriterMP3::Write(AVPacket *packet, int64_t pts)
|
||||
{
|
||||
if (!packet || !packet->data)
|
||||
return false;
|
||||
|
||||
uint8_t PesHeader[PES_MAX_HEADER_SIZE];
|
||||
|
||||
for (int pos = 0; pos < packet->size; ) {
|
||||
int PacketLength = std::min(packet->size - pos, MAX_PES_PACKET_SIZE);
|
||||
struct iovec iov[2];
|
||||
iov[0].iov_base = PesHeader;
|
||||
iov[0].iov_len = InsertPesHeader(PesHeader, PacketLength, MPEG_AUDIO_PES_START_CODE, pts, 0);
|
||||
iov[1].iov_base = packet->data + pos;
|
||||
iov[1].iov_len = PacketLength;
|
||||
|
||||
ssize_t l = writev(fd, iov, 2);
|
||||
if (l < 0)
|
||||
return false;
|
||||
pos += PacketLength;
|
||||
pts = INVALID_PTS_VALUE;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
WriterMP3::WriterMP3()
|
||||
{
|
||||
Register(this, AV_CODEC_ID_MP3, AUDIO_ENCODING_MP3);
|
||||
Register(this, AV_CODEC_ID_MP2, AUDIO_ENCODING_MPEG2);
|
||||
// Register(this, AV_CODEC_ID_VORBIS, AUDIO_ENCODING_VORBIS);
|
||||
Register(this, AV_CODEC_ID_FLAC, AUDIO_ENCODING_LPCM);
|
||||
}
|
||||
|
||||
static WriterMP3 writer_mp3 __attribute__ ((init_priority (300)));
|
71
libeplayer3-sh4/writer/mpeg2.cpp
Normal file
71
libeplayer3-sh4/writer/mpeg2.cpp
Normal file
@@ -0,0 +1,71 @@
|
||||
/*
|
||||
* linuxdvb output/writer handling
|
||||
*
|
||||
* Copyright (C) 2010 konfetti (based on code from libeplayer2)
|
||||
* Copyright (C) 2014 martii (based on code from libeplayer3)
|
||||
*
|
||||
* This program 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.
|
||||
*
|
||||
* This program 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 this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <sys/uio.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
#include "misc.h"
|
||||
#include "pes.h"
|
||||
#include "writer.h"
|
||||
|
||||
class WriterMPEG2 : public Writer
|
||||
{
|
||||
public:
|
||||
bool Write(AVPacket *packet, int64_t pts);
|
||||
WriterMPEG2();
|
||||
};
|
||||
|
||||
bool WriterMPEG2::Write(AVPacket *packet, int64_t pts)
|
||||
{
|
||||
if (!packet || !packet->data)
|
||||
return false;
|
||||
|
||||
uint8_t PesHeader[PES_MAX_HEADER_SIZE];
|
||||
|
||||
for (int pos = 0; pos < packet->size; ) {
|
||||
int PacketLength = std::min(packet->size - pos, MAX_PES_PACKET_SIZE);
|
||||
struct iovec iov[2];
|
||||
iov[0].iov_base = PesHeader;
|
||||
iov[0].iov_len = InsertPesHeader(PesHeader, PacketLength, MPEG_VIDEO_PES_START_CODE, pts, 0);
|
||||
iov[1].iov_base = packet->data + pos;
|
||||
iov[1].iov_len = PacketLength;
|
||||
|
||||
ssize_t l = writev(fd, iov, 2);
|
||||
if (l < 0)
|
||||
return false;
|
||||
pos += PacketLength;
|
||||
pts = INVALID_PTS_VALUE;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
WriterMPEG2::WriterMPEG2()
|
||||
{
|
||||
Register(this, AV_CODEC_ID_MPEG2TS, VIDEO_ENCODING_AUTO);
|
||||
}
|
||||
|
||||
static WriterMPEG2 writer_mpeg2 __attribute__ ((init_priority (300)));
|
376
libeplayer3-sh4/writer/pcm.cpp
Normal file
376
libeplayer3-sh4/writer/pcm.cpp
Normal file
@@ -0,0 +1,376 @@
|
||||
/*
|
||||
* linuxdvb output/writer handling.
|
||||
*
|
||||
* Copyright (C) 2010 konfetti (based on code from libeplayer2)
|
||||
* Copyright (C) 2014 martii (based on code from libeplayer3)
|
||||
*
|
||||
* This program 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.
|
||||
*
|
||||
* This program 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 this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/uio.h>
|
||||
#include <linux/dvb/audio.h>
|
||||
|
||||
#include "misc.h"
|
||||
#include "pes.h"
|
||||
#include "writer.h"
|
||||
#include "player.h"
|
||||
|
||||
extern "C" {
|
||||
#include <libavutil/avutil.h>
|
||||
#include <libavutil/time.h>
|
||||
#include <libavformat/avformat.h>
|
||||
#include <libswresample/swresample.h>
|
||||
#include <libavutil/opt.h>
|
||||
}
|
||||
|
||||
// reference: search for TypeLpcmDVDAudio in player/frame_parser/frame_parser_audio_lpcm.cpp
|
||||
static const uint8_t clpcm_prv[14] = {
|
||||
0xA0, //sub_stream_id
|
||||
0, 0, //resvd and UPC_EAN_ISRC stuff, unused
|
||||
0x0A, //private header length
|
||||
0, 9, //first_access_unit_pointer
|
||||
0x00, //emph,rsvd,stereo,downmix
|
||||
0x0F, //quantisation word length 1,2
|
||||
0x0F, //audio sampling freqency 1,2
|
||||
0, //resvd, multi channel type
|
||||
0, //bit shift on channel GR2, assignment
|
||||
0x80, //dynamic range control
|
||||
0, 0 //resvd for copyright management
|
||||
};
|
||||
|
||||
class WriterPCM : public Writer
|
||||
{
|
||||
private:
|
||||
unsigned int SubFrameLen;
|
||||
unsigned int SubFramesPerPES;
|
||||
uint8_t lpcm_prv[14];
|
||||
uint8_t injectBuffer[2048];
|
||||
uint8_t breakBuffer[sizeof(injectBuffer)];
|
||||
uint8_t *output;
|
||||
uint8_t out_samples_max;
|
||||
unsigned int breakBufferFillSize;
|
||||
int uNoOfChannels;
|
||||
int uSampleRate;
|
||||
int uBitsPerSample;
|
||||
|
||||
AVStream *stream;
|
||||
SwrContext *swr;
|
||||
AVFrame *decoded_frame;
|
||||
int out_sample_rate;
|
||||
int out_channels;
|
||||
uint64_t out_channel_layout;
|
||||
bool initialHeader;
|
||||
bool restart_audio_resampling;
|
||||
|
||||
public:
|
||||
bool Write(AVPacket *packet, int64_t pts);
|
||||
bool prepareClipPlay();
|
||||
bool writePCM(int64_t Pts, uint8_t *data, unsigned int size);
|
||||
void Init(int _fd, AVStream *_stream, Player *_player);
|
||||
WriterPCM();
|
||||
};
|
||||
|
||||
bool WriterPCM::prepareClipPlay()
|
||||
{
|
||||
SubFrameLen = 0;
|
||||
SubFramesPerPES = 0;
|
||||
breakBufferFillSize = 0;
|
||||
|
||||
memcpy(lpcm_prv, clpcm_prv, sizeof(lpcm_prv));
|
||||
|
||||
// figure out size of subframe and set up sample rate
|
||||
switch (uSampleRate) {
|
||||
case 48000:
|
||||
SubFrameLen = 40;
|
||||
break;
|
||||
case 96000:
|
||||
lpcm_prv[8] |= 0x10;
|
||||
SubFrameLen = 80;
|
||||
break;
|
||||
case 192000:
|
||||
lpcm_prv[8] |= 0x20;
|
||||
SubFrameLen = 160;
|
||||
break;
|
||||
case 44100:
|
||||
lpcm_prv[8] |= 0x80;
|
||||
SubFrameLen = 40;
|
||||
break;
|
||||
case 88200:
|
||||
lpcm_prv[8] |= 0x90;
|
||||
SubFrameLen = 80;
|
||||
break;
|
||||
case 176400:
|
||||
lpcm_prv[8] |= 0xA0;
|
||||
SubFrameLen = 160;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
SubFrameLen *= uNoOfChannels;
|
||||
SubFrameLen *= uBitsPerSample / 8;
|
||||
|
||||
//rewrite PES size to have as many complete subframes per PES as we can
|
||||
SubFramesPerPES = ((sizeof(injectBuffer) - 14) - sizeof(lpcm_prv)) / SubFrameLen;
|
||||
SubFrameLen *= SubFramesPerPES;
|
||||
|
||||
//set number of channels
|
||||
lpcm_prv[10] = uNoOfChannels - 1;
|
||||
|
||||
switch (uBitsPerSample) {
|
||||
case 24:
|
||||
lpcm_prv[7] |= 0x20;
|
||||
case 16:
|
||||
break;
|
||||
default:
|
||||
printf("inappropriate bits per sample (%d) - must be 16 or 24\n", uBitsPerSample);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool WriterPCM::writePCM(int64_t Pts, uint8_t *data, unsigned int size)
|
||||
{
|
||||
bool res = true;
|
||||
uint8_t PesHeader[PES_MAX_HEADER_SIZE];
|
||||
|
||||
if (initialHeader) {
|
||||
initialHeader = false;
|
||||
prepareClipPlay();
|
||||
ioctl(fd, AUDIO_CLEAR_BUFFER, NULL);
|
||||
}
|
||||
|
||||
size += breakBufferFillSize;
|
||||
|
||||
while (size >= SubFrameLen) {
|
||||
if (breakBufferFillSize)
|
||||
memcpy(injectBuffer, breakBuffer, breakBufferFillSize);
|
||||
memcpy(injectBuffer + breakBufferFillSize, data, SubFrameLen - breakBufferFillSize);
|
||||
size -= SubFrameLen;
|
||||
data += SubFrameLen - breakBufferFillSize;
|
||||
breakBufferFillSize = 0;
|
||||
|
||||
//write the PCM data
|
||||
if (uBitsPerSample == 16) {
|
||||
for (unsigned int n = 0; n < SubFrameLen; n += 2) {
|
||||
uint8_t tmp = injectBuffer[n];
|
||||
injectBuffer[n] = injectBuffer[n + 1];
|
||||
injectBuffer[n + 1] = tmp;
|
||||
}
|
||||
} else {
|
||||
// 0 1 2 3 4 5 6 7 8 9 10 11
|
||||
// A1c A1b A1a B1c B1b B1a A2c A2b A2a B2c B2b B2a
|
||||
// to A1a A1b B1a B1b A2a A2b B2a B2b A1c B1c A2c B2c
|
||||
for (unsigned int n = 0; n < SubFrameLen; n += 12) {
|
||||
uint8_t t, *p = injectBuffer + n;
|
||||
t = p[0];
|
||||
p[0] = p[2];
|
||||
p[2] = p[5];
|
||||
p[5] = p[7];
|
||||
p[7] = p[11];
|
||||
p[11] = p[9];
|
||||
p[9] = p[3];
|
||||
p[3] = p[4];
|
||||
p[4] = p[8];
|
||||
p[8] = t;
|
||||
}
|
||||
}
|
||||
|
||||
//increment err... subframe count?
|
||||
lpcm_prv[1] = ((lpcm_prv[1] + SubFramesPerPES) & 0x1F);
|
||||
|
||||
struct iovec iov[3];
|
||||
iov[0].iov_base = PesHeader;
|
||||
iov[1].iov_base = lpcm_prv;
|
||||
iov[1].iov_len = sizeof(lpcm_prv);
|
||||
iov[2].iov_base = injectBuffer;
|
||||
iov[2].iov_len = SubFrameLen;
|
||||
iov[0].iov_len = InsertPesHeader(PesHeader, iov[1].iov_len + iov[2].iov_len, PCM_PES_START_CODE, Pts, 0);
|
||||
int len = writev(fd, iov, 3);
|
||||
if (len < 0) {
|
||||
res = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (size && res) {
|
||||
breakBufferFillSize = size;
|
||||
memcpy(breakBuffer, data, size);
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
void WriterPCM::Init(int _fd, AVStream *_stream, Player *_player)
|
||||
{
|
||||
fd = _fd;
|
||||
stream = _stream;
|
||||
player = _player;
|
||||
initialHeader = true;
|
||||
restart_audio_resampling = true;
|
||||
}
|
||||
|
||||
bool WriterPCM::Write(AVPacket *packet, int64_t pts)
|
||||
{
|
||||
if (!packet) {
|
||||
restart_audio_resampling = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
AVCodecContext *c = stream->codec;
|
||||
|
||||
if (restart_audio_resampling) {
|
||||
restart_audio_resampling = false;
|
||||
initialHeader = true;
|
||||
|
||||
if (swr) {
|
||||
swr_free(&swr);
|
||||
swr = NULL;
|
||||
}
|
||||
if (decoded_frame) {
|
||||
av_frame_free(&decoded_frame);
|
||||
decoded_frame = NULL;
|
||||
}
|
||||
|
||||
AVCodec *codec = avcodec_find_decoder(c->codec_id);
|
||||
if (!codec) {
|
||||
fprintf(stderr, "%s %d: avcodec_find_decoder(%llx)\n", __func__, __LINE__, (unsigned long long) c->codec_id);
|
||||
return false;
|
||||
}
|
||||
avcodec_close(c);
|
||||
if (avcodec_open2(c, codec, NULL)) {
|
||||
fprintf(stderr, "%s %d: avcodec_open2 failed\n", __func__, __LINE__);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (!swr) {
|
||||
int in_rate = c->sample_rate;
|
||||
// rates in descending order
|
||||
int rates[] = {192000, 176400, 96000, 88200, 48000, 44100, 0};
|
||||
int i = 0;
|
||||
// find the next equal or smallest rate
|
||||
while (rates[i] && in_rate < rates[i])
|
||||
i++;
|
||||
out_sample_rate = rates[i] ? rates[i] : 44100;
|
||||
out_channels = c->channels;
|
||||
if (c->channel_layout == 0) {
|
||||
// FIXME -- need to guess, looks pretty much like a bug in the FFMPEG WMA decoder
|
||||
c->channel_layout = AV_CH_LAYOUT_STEREO;
|
||||
}
|
||||
|
||||
out_channel_layout = c->channel_layout;
|
||||
// player2 won't play mono
|
||||
if (out_channel_layout == AV_CH_LAYOUT_MONO) {
|
||||
out_channel_layout = AV_CH_LAYOUT_STEREO;
|
||||
out_channels = 2;
|
||||
}
|
||||
|
||||
uSampleRate = out_sample_rate;
|
||||
uNoOfChannels = av_get_channel_layout_nb_channels(out_channel_layout);
|
||||
uBitsPerSample = 16;
|
||||
|
||||
swr = swr_alloc();
|
||||
if (!swr) {
|
||||
fprintf(stderr, "%s %d: swr_alloc failed\n", __func__, __LINE__);
|
||||
return false;
|
||||
}
|
||||
av_opt_set_int(swr, "in_channel_layout", c->channel_layout, 0);
|
||||
av_opt_set_int(swr, "out_channel_layout", out_channel_layout, 0);
|
||||
av_opt_set_int(swr, "in_sample_rate", c->sample_rate, 0);
|
||||
av_opt_set_int(swr, "out_sample_rate", out_sample_rate, 0);
|
||||
av_opt_set_sample_fmt(swr, "in_sample_fmt", c->sample_fmt, 0);
|
||||
av_opt_set_sample_fmt(swr, "out_sample_fmt", AV_SAMPLE_FMT_S16, 0);
|
||||
|
||||
int e = swr_init(swr);
|
||||
if (e < 0) {
|
||||
fprintf(stderr, "swr_init: %d (icl=%d ocl=%d isr=%d osr=%d isf=%d osf=%d)\n",
|
||||
-e, (int) c->channel_layout,
|
||||
(int) out_channel_layout, c->sample_rate, out_sample_rate, c->sample_fmt, AV_SAMPLE_FMT_S16);
|
||||
restart_audio_resampling = true;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
unsigned int packet_size = packet->size;
|
||||
while (packet_size > 0 || (!packet_size && !packet->data)) {
|
||||
int got_frame = 0;
|
||||
|
||||
if (!decoded_frame) {
|
||||
if (!(decoded_frame = av_frame_alloc())) {
|
||||
fprintf(stderr, "out of memory\n");
|
||||
exit(1);
|
||||
}
|
||||
} else
|
||||
av_frame_unref(decoded_frame);
|
||||
|
||||
int len = avcodec_decode_audio4(c, decoded_frame, &got_frame, packet);
|
||||
if (len < 0) {
|
||||
restart_audio_resampling = true;
|
||||
break;
|
||||
}
|
||||
|
||||
if (packet->data)
|
||||
packet_size -= len;
|
||||
|
||||
if (!got_frame) {
|
||||
if (!packet->data || !packet_size)
|
||||
break;
|
||||
continue;
|
||||
}
|
||||
|
||||
pts = player->input.calcPts(stream, av_frame_get_best_effort_timestamp(decoded_frame));
|
||||
|
||||
int in_samples = decoded_frame->nb_samples;
|
||||
int out_samples = av_rescale_rnd(swr_get_delay(swr, c->sample_rate) + in_samples, out_sample_rate, c->sample_rate, AV_ROUND_UP);
|
||||
if (out_samples > out_samples_max) {
|
||||
if (output)
|
||||
av_freep(&output);
|
||||
int e = av_samples_alloc(&output, NULL, out_channels, out_samples, AV_SAMPLE_FMT_S16, 1);
|
||||
if (e < 0) {
|
||||
fprintf(stderr, "av_samples_alloc: %d\n", -e);
|
||||
break;
|
||||
}
|
||||
out_samples_max = out_samples;
|
||||
}
|
||||
|
||||
out_samples = swr_convert(swr, &output, out_samples, (const uint8_t **) &decoded_frame->data[0], in_samples);
|
||||
|
||||
if (!writePCM(pts, output, out_samples * sizeof(short) * out_channels)) {
|
||||
restart_audio_resampling = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return !packet_size;
|
||||
}
|
||||
|
||||
WriterPCM::WriterPCM()
|
||||
{
|
||||
swr = NULL;
|
||||
output = NULL;
|
||||
out_samples_max = 0;
|
||||
decoded_frame = av_frame_alloc();
|
||||
|
||||
Register(this, AV_CODEC_ID_INJECTPCM, AUDIO_ENCODING_LPCMA);
|
||||
}
|
||||
|
||||
static WriterPCM writer_pcm __attribute__ ((init_priority (300)));
|
116
libeplayer3-sh4/writer/pes.cpp
Normal file
116
libeplayer3-sh4/writer/pes.cpp
Normal file
@@ -0,0 +1,116 @@
|
||||
/*
|
||||
* linuxdvb output/writer handling.
|
||||
*
|
||||
* Copyright (C) 2010 konfetti (based on code from libeplayer2)
|
||||
* Copyright (C) 2014 martii (based on code from libeplayer3)
|
||||
*
|
||||
* This program 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.
|
||||
*
|
||||
* This program 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 this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
#include <memory.h>
|
||||
#include <asm/types.h>
|
||||
#include "misc.h"
|
||||
#include "pes.h"
|
||||
|
||||
int InsertVideoPrivateDataHeader(uint8_t *data, int payload_size)
|
||||
{
|
||||
BitPacker_t ld2 = { data, 0, 32 };
|
||||
int i;
|
||||
|
||||
PutBits(&ld2, PES_PRIVATE_DATA_FLAG, 8);
|
||||
PutBits(&ld2, payload_size & 0xff, 8);
|
||||
PutBits(&ld2, (payload_size >> 8) & 0xff, 8);
|
||||
PutBits(&ld2, (payload_size >> 16) & 0xff, 8);
|
||||
|
||||
for (i = 4; i < (PES_PRIVATE_DATA_LENGTH + 1); i++)
|
||||
PutBits(&ld2, 0, 8);
|
||||
|
||||
FlushBits(&ld2);
|
||||
|
||||
return PES_PRIVATE_DATA_LENGTH + 1;
|
||||
|
||||
}
|
||||
|
||||
int InsertPesHeader(uint8_t *data, int size, uint8_t stream_id, int64_t pts, int pic_start_code)
|
||||
{
|
||||
BitPacker_t ld2 = { data, 0, 32 };
|
||||
|
||||
/* if (size > MAX_PES_PACKET_SIZE)
|
||||
size = 0; // unbounded */
|
||||
|
||||
PutBits(&ld2, 0x0, 8);
|
||||
PutBits(&ld2, 0x0, 8);
|
||||
PutBits(&ld2, 0x1, 8); // Start Code
|
||||
PutBits(&ld2, stream_id, 8); // Stream_id = Audio Stream
|
||||
//4
|
||||
PutBits(&ld2, size + 3 + (pts != INVALID_PTS_VALUE ? 5 : 0) + (pic_start_code ? (5) : 0), 16); // PES_packet_length
|
||||
//6 = 4+2
|
||||
PutBits(&ld2, 0x2, 2); // 10
|
||||
PutBits(&ld2, 0x0, 2); // PES_Scrambling_control
|
||||
PutBits(&ld2, 0x0, 1); // PES_Priority
|
||||
PutBits(&ld2, 0x0, 1); // data_alignment_indicator
|
||||
PutBits(&ld2, 0x0, 1); // Copyright
|
||||
PutBits(&ld2, 0x0, 1); // Original or Copy
|
||||
//7 = 6+1
|
||||
|
||||
if (pts != INVALID_PTS_VALUE)
|
||||
PutBits(&ld2, 0x2, 2);
|
||||
else
|
||||
PutBits(&ld2, 0x0, 2); // PTS_DTS flag
|
||||
|
||||
PutBits(&ld2, 0x0, 1); // ESCR_flag
|
||||
PutBits(&ld2, 0x0, 1); // ES_rate_flag
|
||||
PutBits(&ld2, 0x0, 1); // DSM_trick_mode_flag
|
||||
PutBits(&ld2, 0x0, 1); // additional_copy_ingo_flag
|
||||
PutBits(&ld2, 0x0, 1); // PES_CRC_flag
|
||||
PutBits(&ld2, 0x0, 1); // PES_extension_flag
|
||||
//8 = 7+1
|
||||
|
||||
if (pts != INVALID_PTS_VALUE)
|
||||
PutBits(&ld2, 0x5, 8);
|
||||
else
|
||||
PutBits(&ld2, 0x0, 8); // PES_header_data_length
|
||||
//9 = 8+1
|
||||
|
||||
if (pts != INVALID_PTS_VALUE) {
|
||||
PutBits(&ld2, 0x2, 4);
|
||||
PutBits(&ld2, (pts >> 30) & 0x7, 3);
|
||||
PutBits(&ld2, 0x1, 1);
|
||||
PutBits(&ld2, (pts >> 15) & 0x7fff, 15);
|
||||
PutBits(&ld2, 0x1, 1);
|
||||
PutBits(&ld2, pts & 0x7fff, 15);
|
||||
PutBits(&ld2, 0x1, 1);
|
||||
}
|
||||
//14 = 9+5
|
||||
|
||||
if (pic_start_code) {
|
||||
PutBits(&ld2, 0x0, 8);
|
||||
PutBits(&ld2, 0x0, 8);
|
||||
PutBits(&ld2, 0x1, 8); // Start Code
|
||||
PutBits(&ld2, pic_start_code & 0xff, 8); // 00, for picture start
|
||||
PutBits(&ld2, (pic_start_code >> 8) & 0xff, 8); // For any extra information (like in mpeg4p2, the pic_start_code)
|
||||
//14 + 5 = 19
|
||||
}
|
||||
|
||||
FlushBits(&ld2);
|
||||
|
||||
return (ld2.Ptr - data);
|
||||
}
|
188
libeplayer3-sh4/writer/vc1.cpp
Normal file
188
libeplayer3-sh4/writer/vc1.cpp
Normal file
@@ -0,0 +1,188 @@
|
||||
/*
|
||||
* linuxdvb output/writer handling
|
||||
*
|
||||
* Copyright (C) 2010 konfetti (based on code from libeplayer2)
|
||||
* Copyright (C) 2014 martii (based on code from libeplayer3)
|
||||
*
|
||||
* This program 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.
|
||||
*
|
||||
* This program 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 this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <sys/uio.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
#include "misc.h"
|
||||
#include "pes.h"
|
||||
#include "writer.h"
|
||||
|
||||
#define WMV3_PRIVATE_DATA_LENGTH 4
|
||||
|
||||
#define METADATA_STRUCT_A_START 12
|
||||
#define METADATA_STRUCT_B_START 24
|
||||
#define METADATA_STRUCT_B_FRAMERATE_START 32
|
||||
#define METADATA_STRUCT_C_START 8
|
||||
|
||||
|
||||
#define VC1_SEQUENCE_LAYER_METADATA_START_CODE 0x80
|
||||
#define VC1_FRAME_START_CODE 0x0d
|
||||
|
||||
class WriterVC1 : public Writer
|
||||
{
|
||||
private:
|
||||
bool initialHeader;
|
||||
uint8_t FrameHeaderSeen;
|
||||
AVStream *stream;
|
||||
public:
|
||||
bool Write(AVPacket *packet, int64_t pts);
|
||||
void Init(int _fd, AVStream *_stream, Player *_player);
|
||||
WriterVC1();
|
||||
};
|
||||
|
||||
void WriterVC1::Init(int _fd, AVStream *_stream, Player *_player)
|
||||
{
|
||||
fd = _fd;
|
||||
stream = _stream;
|
||||
player = _player;
|
||||
initialHeader = true;
|
||||
}
|
||||
|
||||
bool WriterVC1::Write(AVPacket *packet, int64_t pts)
|
||||
{
|
||||
if (!packet || !packet->data)
|
||||
return false;
|
||||
|
||||
if (initialHeader) {
|
||||
initialHeader = false;
|
||||
FrameHeaderSeen = false;
|
||||
|
||||
const uint8_t SequenceLayerStartCode[] =
|
||||
{ 0x00, 0x00, 0x01, VC1_SEQUENCE_LAYER_METADATA_START_CODE };
|
||||
|
||||
|
||||
const uint8_t Metadata[] = {
|
||||
0x00, 0x00, 0x00, 0xc5,
|
||||
0x04, 0x00, 0x00, 0x00,
|
||||
0xc0, 0x00, 0x00, 0x00, /* Struct C set for for advanced profile */
|
||||
0x00, 0x00, 0x00, 0x00, /* Struct A */
|
||||
0x00, 0x00, 0x00, 0x00,
|
||||
0x0c, 0x00, 0x00, 0x00,
|
||||
0x60, 0x00, 0x00, 0x00, /* Struct B */
|
||||
0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00
|
||||
};
|
||||
|
||||
uint8_t PesHeader[PES_MAX_HEADER_SIZE];
|
||||
uint8_t PesPayload[128];
|
||||
uint8_t *PesPtr;
|
||||
unsigned int usecPerFrame = av_rescale(AV_TIME_BASE, stream->r_frame_rate.den, stream->r_frame_rate.num);
|
||||
struct iovec iov[2];
|
||||
|
||||
|
||||
memset(PesPayload, 0, sizeof(PesPayload));
|
||||
|
||||
PesPtr = PesPayload;
|
||||
|
||||
memcpy(PesPtr, SequenceLayerStartCode, sizeof(SequenceLayerStartCode));
|
||||
PesPtr += sizeof(SequenceLayerStartCode);
|
||||
|
||||
memcpy(PesPtr, Metadata, sizeof(Metadata));
|
||||
PesPtr += METADATA_STRUCT_C_START;
|
||||
PesPtr += WMV3_PRIVATE_DATA_LENGTH;
|
||||
|
||||
/* Metadata Header Struct A */
|
||||
*PesPtr++ = (stream->codec->height >> 0) & 0xff;
|
||||
*PesPtr++ = (stream->codec->height >> 8) & 0xff;
|
||||
*PesPtr++ = (stream->codec->height >> 16) & 0xff;
|
||||
*PesPtr++ = stream->codec->height >> 24;
|
||||
*PesPtr++ = (stream->codec->width >> 0) & 0xff;
|
||||
*PesPtr++ = (stream->codec->width >> 8) & 0xff;
|
||||
*PesPtr++ = (stream->codec->width >> 16) & 0xff;
|
||||
*PesPtr++ = stream->codec->width >> 24;
|
||||
|
||||
PesPtr += 12; /* Skip flag word and Struct B first 8 bytes */
|
||||
|
||||
*PesPtr++ = (usecPerFrame >> 0) & 0xff;
|
||||
*PesPtr++ = (usecPerFrame >> 8) & 0xff;
|
||||
*PesPtr++ = (usecPerFrame >> 16) & 0xff;
|
||||
*PesPtr++ = usecPerFrame >> 24;
|
||||
|
||||
iov[0].iov_base = PesHeader;
|
||||
iov[1].iov_base = PesPayload;
|
||||
iov[1].iov_len = PesPtr - PesPayload;
|
||||
iov[0].iov_len = InsertPesHeader(PesHeader, iov[1].iov_len, VC1_VIDEO_PES_START_CODE, INVALID_PTS_VALUE, 0);
|
||||
if (writev(fd, iov, 2) < 0)
|
||||
return false;
|
||||
|
||||
/* For VC1 the codec private data is a standard vc1 sequence header so we just copy it to the output */
|
||||
iov[0].iov_base = PesHeader;
|
||||
iov[1].iov_base = stream->codec->extradata;
|
||||
iov[1].iov_len = stream->codec->extradata_size;
|
||||
iov[0].iov_len = InsertPesHeader(PesHeader, iov[1].iov_len, VC1_VIDEO_PES_START_CODE, INVALID_PTS_VALUE, 0);
|
||||
if (writev(fd, iov, 2) < 0)
|
||||
return false;
|
||||
|
||||
initialHeader = false;
|
||||
}
|
||||
|
||||
if (packet->size > 0) {
|
||||
int Position = 0;
|
||||
bool insertSampleHeader = true;
|
||||
|
||||
while (Position < packet->size) {
|
||||
int PacketLength = std::min(packet->size - Position, MAX_PES_PACKET_SIZE);
|
||||
uint8_t PesHeader[PES_MAX_HEADER_SIZE];
|
||||
int HeaderLength = InsertPesHeader(PesHeader, PacketLength, VC1_VIDEO_PES_START_CODE, pts, 0);
|
||||
|
||||
if (insertSampleHeader) {
|
||||
const uint8_t Vc1FrameStartCode[] = { 0, 0, 1, VC1_FRAME_START_CODE };
|
||||
|
||||
if (!FrameHeaderSeen && (packet->size > 3) && (memcmp(packet->data, Vc1FrameStartCode, 4) == 0))
|
||||
FrameHeaderSeen = true;
|
||||
if (!FrameHeaderSeen) {
|
||||
memcpy(&PesHeader[HeaderLength], Vc1FrameStartCode, sizeof(Vc1FrameStartCode));
|
||||
HeaderLength += sizeof(Vc1FrameStartCode);
|
||||
}
|
||||
insertSampleHeader = false;
|
||||
}
|
||||
|
||||
struct iovec iov[2];
|
||||
iov[0].iov_base = PesHeader;
|
||||
iov[0].iov_len = HeaderLength;
|
||||
iov[1].iov_base = packet->data + Position;
|
||||
iov[1].iov_len = PacketLength;
|
||||
|
||||
ssize_t l = writev(fd, iov, 2);
|
||||
if (l < 0)
|
||||
return false;
|
||||
|
||||
Position += PacketLength;
|
||||
pts = INVALID_PTS_VALUE;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
WriterVC1::WriterVC1()
|
||||
{
|
||||
Register(this, AV_CODEC_ID_VC1, VIDEO_ENCODING_VC1);
|
||||
}
|
||||
|
||||
static WriterVC1 writer_vc1 __attribute__ ((init_priority (300)));
|
171
libeplayer3-sh4/writer/wmv.cpp
Normal file
171
libeplayer3-sh4/writer/wmv.cpp
Normal file
@@ -0,0 +1,171 @@
|
||||
/*
|
||||
* linuxdvb output/writer handling
|
||||
*
|
||||
* Copyright (C) 2010 konfetti (based on code from libeplayer2)
|
||||
* Copyright (C) 2014 martii (based on code from libeplayer3)
|
||||
*
|
||||
* This program 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.
|
||||
*
|
||||
* This program 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 this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/uio.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "misc.h"
|
||||
#include "pes.h"
|
||||
#include "writer.h"
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
#define WMV3_PRIVATE_DATA_LENGTH 4
|
||||
|
||||
static const uint8_t Metadata[] = {
|
||||
0x00, 0x00, 0x00, 0xc5,
|
||||
0x04, 0x00, 0x00, 0x00,
|
||||
#define METADATA_STRUCT_C_START 8
|
||||
0xc0, 0x00, 0x00, 0x00, /* Struct C set for for advanced profile */
|
||||
#define METADATA_STRUCT_A_START 12
|
||||
0x00, 0x00, 0x00, 0x00, /* Struct A */
|
||||
0x00, 0x00, 0x00, 0x00,
|
||||
0x0c, 0x00, 0x00, 0x00,
|
||||
#define METADATA_STRUCT_B_START 24
|
||||
0x60, 0x00, 0x00, 0x00, /* Struct B */
|
||||
0x00, 0x00, 0x00, 0x00,
|
||||
#define METADATA_STRUCT_B_FRAMERATE_START 32
|
||||
0x00, 0x00, 0x00, 0x00
|
||||
};
|
||||
|
||||
class WriterWMV : public Writer
|
||||
{
|
||||
private:
|
||||
bool initialHeader;
|
||||
AVStream *stream;
|
||||
public:
|
||||
bool Write(AVPacket *packet, int64_t pts);
|
||||
void Init(int _fd, AVStream *_stream, Player *_player);
|
||||
WriterWMV();
|
||||
};
|
||||
|
||||
void WriterWMV::Init(int _fd, AVStream *_stream, Player *_player)
|
||||
{
|
||||
fd = _fd;
|
||||
stream = _stream;
|
||||
player = _player;
|
||||
initialHeader = true;
|
||||
}
|
||||
|
||||
bool WriterWMV::Write(AVPacket *packet, int64_t pts)
|
||||
{
|
||||
if (!packet || !packet->data)
|
||||
return false;
|
||||
|
||||
if (initialHeader) {
|
||||
#define PES_MIN_HEADER_SIZE 9
|
||||
uint8_t PesPacket[PES_MIN_HEADER_SIZE + 128];
|
||||
uint8_t *PesPtr;
|
||||
unsigned int MetadataLength;
|
||||
unsigned int usecPerFrame = av_rescale(AV_TIME_BASE, stream->r_frame_rate.den, stream->r_frame_rate.num);
|
||||
|
||||
PesPtr = &PesPacket[PES_MIN_HEADER_SIZE];
|
||||
|
||||
memcpy(PesPtr, Metadata, sizeof(Metadata));
|
||||
PesPtr += METADATA_STRUCT_C_START;
|
||||
|
||||
uint8_t privateData[WMV3_PRIVATE_DATA_LENGTH] = { 0 };
|
||||
memcpy(privateData, stream->codec->extradata, stream->codec->extradata_size > WMV3_PRIVATE_DATA_LENGTH ? WMV3_PRIVATE_DATA_LENGTH : stream->codec->extradata_size);
|
||||
|
||||
memcpy(PesPtr, privateData, WMV3_PRIVATE_DATA_LENGTH);
|
||||
PesPtr += WMV3_PRIVATE_DATA_LENGTH;
|
||||
|
||||
/* Metadata Header Struct A */
|
||||
*PesPtr++ = (stream->codec->height >> 0) & 0xff;
|
||||
*PesPtr++ = (stream->codec->height >> 8) & 0xff;
|
||||
*PesPtr++ = (stream->codec->height >> 16) & 0xff;
|
||||
*PesPtr++ = stream->codec->height >> 24;
|
||||
*PesPtr++ = (stream->codec->width >> 0) & 0xff;
|
||||
*PesPtr++ = (stream->codec->width >> 8) & 0xff;
|
||||
*PesPtr++ = (stream->codec->width >> 16) & 0xff;
|
||||
*PesPtr++ = stream->codec->width >> 24;
|
||||
|
||||
PesPtr += 12; /* Skip flag word and Struct B first 8 bytes */
|
||||
|
||||
*PesPtr++ = (usecPerFrame >> 0) & 0xff;
|
||||
*PesPtr++ = (usecPerFrame >> 8) & 0xff;
|
||||
*PesPtr++ = (usecPerFrame >> 16) & 0xff;
|
||||
*PesPtr++ = usecPerFrame >> 24;
|
||||
|
||||
MetadataLength = PesPtr - &PesPacket[PES_MIN_HEADER_SIZE];
|
||||
|
||||
int HeaderLength = InsertPesHeader(PesPacket, MetadataLength, VC1_VIDEO_PES_START_CODE, INVALID_PTS_VALUE, 0);
|
||||
|
||||
if (write(fd, PesPacket, HeaderLength + MetadataLength) < 0)
|
||||
return false;
|
||||
|
||||
initialHeader = false;
|
||||
}
|
||||
|
||||
if (packet->size > 0 && packet->data) {
|
||||
int Position = 0;
|
||||
bool insertSampleHeader = true;
|
||||
|
||||
while (Position < packet->size) {
|
||||
|
||||
int PacketLength = std::min(packet->size - Position, MAX_PES_PACKET_SIZE);
|
||||
|
||||
uint8_t PesHeader[PES_MAX_HEADER_SIZE] = { 0 };
|
||||
int HeaderLength = InsertPesHeader(PesHeader, PacketLength, VC1_VIDEO_PES_START_CODE, pts, 0);
|
||||
|
||||
if (insertSampleHeader) {
|
||||
unsigned int PesLength;
|
||||
unsigned int PrivateHeaderLength;
|
||||
|
||||
PrivateHeaderLength = InsertVideoPrivateDataHeader(&PesHeader[HeaderLength], packet->size);
|
||||
/* Update PesLength */
|
||||
PesLength = PesHeader[PES_LENGTH_BYTE_0] + (PesHeader[PES_LENGTH_BYTE_1] << 8) + PrivateHeaderLength;
|
||||
PesHeader[PES_LENGTH_BYTE_0] = PesLength & 0xff;
|
||||
PesHeader[PES_LENGTH_BYTE_1] = (PesLength >> 8) & 0xff;
|
||||
PesHeader[PES_HEADER_DATA_LENGTH_BYTE] += PrivateHeaderLength;
|
||||
PesHeader[PES_FLAGS_BYTE] |= PES_EXTENSION_DATA_PRESENT;
|
||||
|
||||
HeaderLength += PrivateHeaderLength;
|
||||
insertSampleHeader = false;
|
||||
}
|
||||
|
||||
uint8_t PacketStart[packet->size + HeaderLength];
|
||||
memcpy(PacketStart, PesHeader, HeaderLength);
|
||||
memcpy(PacketStart + HeaderLength, packet->data + Position, PacketLength);
|
||||
if (write(fd, PacketStart, PacketLength + HeaderLength) < 0)
|
||||
return false;
|
||||
|
||||
Position += PacketLength;
|
||||
pts = INVALID_PTS_VALUE;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
WriterWMV::WriterWMV()
|
||||
{
|
||||
Register(this, AV_CODEC_ID_WMV1, VIDEO_ENCODING_WMV);
|
||||
Register(this, AV_CODEC_ID_WMV2, VIDEO_ENCODING_WMV);
|
||||
Register(this, AV_CODEC_ID_WMV3, VIDEO_ENCODING_WMV);
|
||||
}
|
||||
|
||||
static WriterWMV writer_wmv __attribute__ ((init_priority (300)));
|
106
libeplayer3-sh4/writer/writer.cpp
Normal file
106
libeplayer3-sh4/writer/writer.cpp
Normal file
@@ -0,0 +1,106 @@
|
||||
/*
|
||||
* linuxdvb output/writer handling
|
||||
*
|
||||
* Copyright (C) 2014 martii
|
||||
*
|
||||
* This program 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.
|
||||
*
|
||||
* This program 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 this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <string>
|
||||
#include <map>
|
||||
|
||||
#include "pes.h"
|
||||
#include "writer.h"
|
||||
|
||||
// This does suck ... the original idea was to just link the object files and let them register themselves.
|
||||
// Alas, that didn't work as expected.
|
||||
#include "ac3.cpp"
|
||||
#include "divx.cpp"
|
||||
#include "dts.cpp"
|
||||
#include "h263.cpp"
|
||||
#include "h264.cpp"
|
||||
#include "mp3.cpp"
|
||||
#include "mpeg2.cpp"
|
||||
#include "pcm.cpp"
|
||||
#include "vc1.cpp"
|
||||
#include "wmv.cpp"
|
||||
//#include "aac.cpp"
|
||||
|
||||
static std::map<enum AVCodecID,Writer *>writers __attribute__ ((init_priority (200)));
|
||||
static std::map<enum AVCodecID,video_encoding_t>vencoding __attribute__ ((init_priority (200)));
|
||||
static std::map<enum AVCodecID,audio_encoding_t>aencoding __attribute__ ((init_priority (200)));
|
||||
|
||||
void Writer::Register(Writer *w, enum AVCodecID id, video_encoding_t encoding)
|
||||
{
|
||||
writers[id] = w;
|
||||
vencoding[id] = encoding;
|
||||
}
|
||||
|
||||
void Writer::Register(Writer *w, enum AVCodecID id, audio_encoding_t encoding)
|
||||
{
|
||||
writers[id] = w;
|
||||
aencoding[id] = encoding;
|
||||
}
|
||||
|
||||
bool Writer::Write(AVPacket * /* packet */, int64_t /* pts */)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
static Writer writer __attribute__ ((init_priority (300)));
|
||||
|
||||
Writer *Writer::GetWriter(enum AVCodecID id, enum AVMediaType codec_type, int track_type)
|
||||
{
|
||||
fprintf(stderr, "GETWRITER %d %d %d", id, codec_type, track_type);
|
||||
if (track_type != 6) { // hack for ACC resampling
|
||||
std::map<enum AVCodecID,Writer*>::iterator it = writers.find(id);
|
||||
if (it != writers.end())
|
||||
return it->second;
|
||||
}
|
||||
switch (codec_type) {
|
||||
case AVMEDIA_TYPE_AUDIO:
|
||||
if (id == AV_CODEC_ID_INJECTPCM) // should not happen
|
||||
break;
|
||||
return GetWriter(AV_CODEC_ID_INJECTPCM, codec_type, 100);
|
||||
case AVMEDIA_TYPE_VIDEO:
|
||||
if (id == AV_CODEC_ID_MPEG2TS) // should not happen
|
||||
break;
|
||||
return GetWriter(AV_CODEC_ID_MPEG2TS, codec_type, 100);
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return &writer;
|
||||
}
|
||||
|
||||
video_encoding_t Writer::GetVideoEncoding(enum AVCodecID id)
|
||||
{
|
||||
std::map<enum AVCodecID,video_encoding_t>::iterator it = vencoding.find(id);
|
||||
if (it != vencoding.end())
|
||||
return it->second;
|
||||
return VIDEO_ENCODING_AUTO;
|
||||
}
|
||||
|
||||
audio_encoding_t Writer::GetAudioEncoding(enum AVCodecID id)
|
||||
{
|
||||
std::map<enum AVCodecID,audio_encoding_t>::iterator it = aencoding.find(id);
|
||||
if (it != aencoding.end())
|
||||
return it->second;
|
||||
return AUDIO_ENCODING_LPCMA;
|
||||
}
|
Reference in New Issue
Block a user