libeplayer3: optimizations (untested)

This commit is contained in:
martii
2014-04-15 17:12:22 +02:00
parent 8ccf1ba33b
commit 2289de62e8
7 changed files with 57 additions and 78 deletions

View File

@@ -59,19 +59,14 @@ Input::~Input()
int64_t calcPts(AVFormatContext *avfc, AVStream * stream, int64_t pts)
{
if (!avfc || !stream) {
fprintf(stderr, "context / stream null\n");
return INVALID_PTS_VALUE;
}
if (pts == AV_NOPTS_VALUE)
return INVALID_PTS_VALUE;
pts = 90000 * pts * stream->time_base.num / stream->time_base.den;
if (avfc->start_time != AV_NOPTS_VALUE)
pts -= 90000.0 * avfc->start_time / AV_TIME_BASE;
pts -= 90000 * avfc->start_time / AV_TIME_BASE;
if (pts & 0x8000000000000000ll)
if (pts < 0)
return INVALID_PTS_VALUE;
return pts;

View File

@@ -368,7 +368,7 @@ bool Player::GetChapters(std::vector<int> &positions, std::vector<std::string> &
input.UpdateTracks();
OpenThreads::ScopedLock<OpenThreads::Mutex> m_lock(chapterMutex);
for (std::vector<Chapter>::iterator it = chapters.begin(); it != chapters.end(); ++it) {
positions.push_back(1000 * it->start);
positions.push_back(it->start/1000);
titles.push_back(it->title);
}
return true;

View File

@@ -58,10 +58,9 @@ bool WriterDIVX::Write(int fd, AVFormatContext * /* avfc */, AVStream *stream, A
unsigned int FakeHeaderLength;
uint8_t Version = 5;
unsigned int FakeStartCode = (Version << 8) | PES_VERSION_FAKE_START_CODE;
unsigned int usecPerFrame = 41708; /* Hellmaster1024: default value */
BitPacker_t ld = { FakeHeaders, 0, 32 };
usecPerFrame = 1000000.0 / av_q2d(stream->r_frame_rate);
unsigned int usecPerFrame = AV_TIME_BASE * stream->r_frame_rate.den / stream->r_frame_rate.num;
/* Create info record for frame parser */
/* divx4 & 5

View File

@@ -27,7 +27,6 @@
#include <string.h>
#include <sys/uio.h>
#include <errno.h>
#include <assert.h>
#include "misc.h"
#include "pes.h"
@@ -37,8 +36,8 @@
#define CONTAINER_PARAMETERS_VERSION 0x00
typedef struct avcC_s {
uint8_t Version; // configurationVersion
uint8_t Profile; // AVCProfileIndication
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
@@ -46,7 +45,7 @@ typedef struct avcC_s {
uint8_t Params[1]; // {length,params}{length,params}...sequence then picture
} avcC_t;
const uint8_t Head[] = { 0, 0, 0, 1 };
static uint8_t Head[] = { 0, 0, 0, 1 };
class WriterH264 : public Writer
{
@@ -62,7 +61,6 @@ class WriterH264 : public Writer
void WriterH264::Init(void)
{
initialHeader = true;
initialHeader = 1;
NalLengthBytes = 1;
}
@@ -77,42 +75,36 @@ bool WriterH264::Write(int fd, AVFormatContext * /* avfc */, AVStream *stream, A
int ic = 0;
struct iovec iov[128];
TimeDelta = 1000.0 * stream->r_frame_rate.num / stream->r_frame_rate.den;
TimeDelta = 1000 * stream->r_frame_rate.num / stream->r_frame_rate.den;
TimeScale = (TimeDelta < 23970) ? 1001 : 1000; /* fixme: revise this */
if ((packet->size > 3)
&& ((packet->data[0] == 0x00 && packet->data[1] == 0x00 && packet->data[2] == 0x00 && packet->data[3] == 0x01)
|| (packet->data[0] == 0xff && packet->data[1] == 0xff && packet->data[2] == 0xff && packet->data[3] == 0xff))) {
unsigned int PacketLength = 0;
unsigned int FakeStartCode = /* (call->Version << 8) | */ PES_VERSION_FAKE_START_CODE;
iov[ic++].iov_base = PesHeader;
if (initialHeader) {
initialHeader = false;
iov[ic].iov_base = stream->codec->extradata;
iov[ic++].iov_len = stream->codec->extradata_size;
PacketLength += stream->codec->extradata_size;
len += stream->codec->extradata_size;
}
iov[ic].iov_base = packet->data;
iov[ic++].iov_len = packet->size;
PacketLength += packet->size;
len += packet->size;
// 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;
iov[0].iov_len = InsertPesHeader(PesHeader, PacketLength, MPEG_VIDEO_PES_START_CODE, pts, FakeStartCode);
iov[0].iov_len = InsertPesHeader(PesHeader, len, MPEG_VIDEO_PES_START_CODE, pts, FakeStartCode);
return writev(fd, iov, ic) > -1;
}
if (initialHeader) {
avcC_t *avcCHeader = (avcC_t *) stream->codec->extradata;
unsigned int i;
unsigned int ParamSets;
unsigned int ParamOffset;
unsigned int InitialHeaderLength = 0;
unsigned int ParametersLength;
if (avcCHeader == NULL) {
if (!avcCHeader) {
fprintf(stderr, "stream->codec->extradata == NULL\n");
return false;
}
@@ -120,56 +112,52 @@ bool WriterH264::Write(int fd, AVFormatContext * /* avfc */, AVStream *stream, A
if (avcCHeader->Version != 1)
fprintf(stderr, "Error unknown avcC version (%x). Expect problems.\n", avcCHeader->Version);
ParametersLength = 0;
uint8_t HeaderData[19];
HeaderData[ParametersLength++] = 0x00; // Start code
HeaderData[ParametersLength++] = 0x00;
HeaderData[ParametersLength++] = 0x01;
HeaderData[ParametersLength++] = NALU_TYPE_PLAYER2_CONTAINER_PARAMETERS;
uint8_t Header[19];
unsigned int HeaderLen = 0;
Header[HeaderLen++] = 0x00; // Start code
Header[HeaderLen++] = 0x00;
Header[HeaderLen++] = 0x01;
Header[HeaderLen++] = NALU_TYPE_PLAYER2_CONTAINER_PARAMETERS;
// Container message version - changes when/if we vary the format of the message
HeaderData[ParametersLength++] = CONTAINER_PARAMETERS_VERSION;
HeaderData[ParametersLength++] = 0xff; // Field separator
Header[HeaderLen++] = CONTAINER_PARAMETERS_VERSION;
Header[HeaderLen++] = 0xff; // Field separator
if (TimeDelta == 0xffffffff)
TimeDelta = (TimeScale > 1000) ? 1001 : 1;
HeaderData[ParametersLength++] = (TimeScale >> 24) & 0xff; // Output the timescale
HeaderData[ParametersLength++] = (TimeScale >> 16) & 0xff;
HeaderData[ParametersLength++] = 0xff;
HeaderData[ParametersLength++] = (TimeScale >> 8) & 0xff;
HeaderData[ParametersLength++] = TimeScale & 0xff;
HeaderData[ParametersLength++] = 0xff;
Header[HeaderLen++] = (TimeScale >> 24) & 0xff; // Output the timescale
Header[HeaderLen++] = (TimeScale >> 16) & 0xff;
Header[HeaderLen++] = 0xff;
Header[HeaderLen++] = (TimeScale >> 8) & 0xff;
Header[HeaderLen++] = TimeScale & 0xff;
Header[HeaderLen++] = 0xff;
HeaderData[ParametersLength++] = (TimeDelta >> 24) & 0xff; // Output frame period
HeaderData[ParametersLength++] = (TimeDelta >> 16) & 0xff;
HeaderData[ParametersLength++] = 0xff;
HeaderData[ParametersLength++] = (TimeDelta >> 8) & 0xff;
HeaderData[ParametersLength++] = TimeDelta & 0xff;
HeaderData[ParametersLength++] = 0xff;
HeaderData[ParametersLength++] = 0x80; // Rsbp trailing bits
assert(ParametersLength <= sizeof(HeaderData));
Header[HeaderLen++] = (TimeDelta >> 24) & 0xff; // Output frame period
Header[HeaderLen++] = (TimeDelta >> 16) & 0xff;
Header[HeaderLen++] = 0xff;
Header[HeaderLen++] = (TimeDelta >> 8) & 0xff;
Header[HeaderLen++] = TimeDelta & 0xff;
Header[HeaderLen++] = 0xff;
Header[HeaderLen++] = 0x80; // Rsbp trailing bits
ic = 0;
iov[ic].iov_base = PesHeader;
iov[ic++].iov_len = InsertPesHeader(PesHeader, ParametersLength, MPEG_VIDEO_PES_START_CODE, INVALID_PTS_VALUE, 0);
iov[ic].iov_base = HeaderData;
iov[ic++].iov_len = ParametersLength;
iov[ic++].iov_len = InsertPesHeader(PesHeader, HeaderLen, MPEG_VIDEO_PES_START_CODE, INVALID_PTS_VALUE, 0);
iov[ic].iov_base = Header;
iov[ic++].iov_len = HeaderLen;
len = writev(fd, iov, ic);
if (len < 0)
return false;
NalLengthBytes = (avcCHeader->NalLengthMinusOne & 0x03) + 1;
ParamSets = avcCHeader->NumParamSets & 0x1f;
unsigned int ParamSets = avcCHeader->NumParamSets & 0x1f;
unsigned int ParamOffset = 0;
unsigned int InitialHeaderLength = 0;
ParamOffset = 0;
ic = 0;
iov[ic++].iov_base = PesHeader;
for (i = 0; i < ParamSets; i++) {
unsigned int PsLength =
(avcCHeader->Params[ParamOffset] << 8) +
avcCHeader->Params[ParamOffset + 1];
for (unsigned int i = 0; i < ParamSets; i++) {
unsigned int PsLength = (avcCHeader->Params[ParamOffset] << 8) + avcCHeader->Params[ParamOffset + 1];
iov[ic].iov_base = (char *) Head;
iov[ic++].iov_len = sizeof(Head);
@@ -180,13 +168,10 @@ bool WriterH264::Write(int fd, AVFormatContext * /* avfc */, AVStream *stream, A
ParamOffset += PsLength + 2;
}
ParamSets = avcCHeader->Params[ParamOffset];
ParamSets = avcCHeader->Params[ParamOffset++];
ParamOffset++;
for (i = 0; i < ParamSets; i++) {
unsigned int PsLength =
(avcCHeader->Params[ParamOffset] << 8) +
avcCHeader->Params[ParamOffset + 1];
for (unsigned int i = 0; i < ParamSets; i++) {
unsigned int PsLength = (avcCHeader->Params[ParamOffset] << 8) + avcCHeader->Params[ParamOffset + 1];
iov[ic].iov_base = (char *) Head;
iov[ic++].iov_len = sizeof(Head);
@@ -213,7 +198,6 @@ bool WriterH264::Write(int fd, AVFormatContext * /* avfc */, AVStream *stream, A
do {
unsigned int NalLength;
uint8_t NalData[4];
int NalPresent = 1;
memcpy(NalData, packet->data + VideoPosition, NalLengthBytes);
VideoPosition += NalLengthBytes;
@@ -241,14 +225,12 @@ bool WriterH264::Write(int fd, AVFormatContext * /* avfc */, AVStream *stream, A
ic = 0;
iov[ic++].iov_base = PesHeader;
if (NalPresent) {
NalPresent = 0;
iov[ic].iov_base = (char *) Head;
iov[ic++].iov_len = sizeof(Head);
}
iov[ic].iov_base = Head;
iov[ic++].iov_len = sizeof(Head);
iov[ic].iov_base = packet->data + VideoPosition;
iov[ic++].iov_len = NalLength;
VideoPosition += NalLength;
iov[0].iov_len = InsertPesHeader(PesHeader, NalLength, MPEG_VIDEO_PES_START_CODE, pts, 0);

View File

@@ -158,7 +158,7 @@ int WriterPCM::writePCM(int fd, int64_t Pts, uint8_t *data, unsigned int size)
for (pos = 0; pos < size;) {
//printf("PCM %s - Position=%d\n", __FUNCTION__, pos);
if ((size - pos) < SubFrameLen) {
if ((size - pos) < SubFrameLen) {
breakBufferFillSize = size - pos;
memcpy(breakBuffer, &data[pos], sizeof(uint8_t) * breakBufferFillSize);
//printf("PCM %s - Unplayed=%d\n", __FUNCTION__, breakBufferFillSize);
@@ -254,8 +254,10 @@ bool WriterPCM::Write(int fd, AVFormatContext *avfc, AVStream *stream, AVPacket
AVCodec *codec = avcodec_find_decoder(c->codec_id);
if (!codec || avcodec_open2(c, codec, NULL))
if (!codec || avcodec_open2(c, codec, NULL)) {
fprintf(stderr, "%s %d: avcodec_open2 failed\n", __func__, __LINE__);
return false;
}
}
while (packet_size > 0) {
@@ -326,11 +328,12 @@ bool WriterPCM::Write(int fd, AVFormatContext *avfc, AVStream *stream, AVPacket
}
// FIXME. PTS calculation is probably broken.
int64_t next_in_pts = av_rescale(av_frame_get_best_effort_timestamp(decoded_frame),
stream->time_base.num * (int64_t) out_sample_rate * c->sample_rate,
stream->time_base.num * out_sample_rate * c->sample_rate,
stream->time_base.den);
int64_t next_out_pts = av_rescale(swr_next_pts(swr, next_in_pts),
stream->time_base.den,
stream->time_base.num * (int64_t) out_sample_rate * c->sample_rate);
stream->time_base.num * out_sample_rate * c->sample_rate);
pts = calcPts(avfc, stream, next_out_pts);
out_samples = swr_convert(swr, &output, out_samples, (const uint8_t **) &decoded_frame->data[0], in_samples);

View File

@@ -89,7 +89,7 @@ bool WriterVC1::Write(int fd, AVFormatContext * /* avfc */, AVStream *stream, AV
uint8_t PesHeader[PES_MAX_HEADER_SIZE];
uint8_t PesPayload[128];
uint8_t *PesPtr;
unsigned int usecPerFrame = ((10000000.0 / av_q2d(stream->r_frame_rate)));
unsigned int usecPerFrame = AV_TIME_BASE * stream->r_frame_rate.den / stream->r_frame_rate.num;
struct iovec iov[2];

View File

@@ -65,7 +65,7 @@ class WriterWMV : public Writer
void WriterWMV::Init()
{
initialHeader = 1;
initialHeader = true;
}
bool WriterWMV::Write(int fd, AVFormatContext * /* avfc */, AVStream *stream, AVPacket *packet, int64_t pts)
@@ -78,7 +78,7 @@ bool WriterWMV::Write(int fd, AVFormatContext * /* avfc */, AVStream *stream, AV
uint8_t PesPacket[PES_MIN_HEADER_SIZE + 128];
uint8_t *PesPtr;
unsigned int MetadataLength;
unsigned int usecPerFrame = ((10000000.0 / av_q2d(stream->r_frame_rate)));
unsigned int usecPerFrame = AV_TIME_BASE * stream->r_frame_rate.den / stream->r_frame_rate.num;
PesPtr = &PesPacket[PES_MIN_HEADER_SIZE];