libeplayer3: optimizations (untested)

This commit is contained in:
martii
2014-04-15 17:12:22 +02:00
parent 62d526f799
commit b4ec2c34b9
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) 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) if (pts == AV_NOPTS_VALUE)
return INVALID_PTS_VALUE; return INVALID_PTS_VALUE;
pts = 90000 * pts * stream->time_base.num / stream->time_base.den; pts = 90000 * pts * stream->time_base.num / stream->time_base.den;
if (avfc->start_time != AV_NOPTS_VALUE) 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 INVALID_PTS_VALUE;
return pts; return pts;

View File

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

View File

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

View File

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

View File

@@ -254,8 +254,10 @@ bool WriterPCM::Write(int fd, AVFormatContext *avfc, AVStream *stream, AVPacket
AVCodec *codec = avcodec_find_decoder(c->codec_id); 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__); fprintf(stderr, "%s %d: avcodec_open2 failed\n", __func__, __LINE__);
return false;
}
} }
while (packet_size > 0) { while (packet_size > 0) {
@@ -326,11 +328,12 @@ bool WriterPCM::Write(int fd, AVFormatContext *avfc, AVStream *stream, AVPacket
} }
// FIXME. PTS calculation is probably broken. // FIXME. PTS calculation is probably broken.
int64_t next_in_pts = av_rescale(av_frame_get_best_effort_timestamp(decoded_frame), 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); stream->time_base.den);
int64_t next_out_pts = av_rescale(swr_next_pts(swr, next_in_pts), int64_t next_out_pts = av_rescale(swr_next_pts(swr, next_in_pts),
stream->time_base.den, 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); pts = calcPts(avfc, stream, next_out_pts);
out_samples = swr_convert(swr, &output, out_samples, (const uint8_t **) &decoded_frame->data[0], in_samples); 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 PesHeader[PES_MAX_HEADER_SIZE];
uint8_t PesPayload[128]; uint8_t PesPayload[128];
uint8_t *PesPtr; 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]; struct iovec iov[2];

View File

@@ -65,7 +65,7 @@ class WriterWMV : public Writer
void WriterWMV::Init() void WriterWMV::Init()
{ {
initialHeader = 1; initialHeader = true;
} }
bool WriterWMV::Write(int fd, AVFormatContext * /* avfc */, AVStream *stream, AVPacket *packet, int64_t pts) 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 PesPacket[PES_MIN_HEADER_SIZE + 128];
uint8_t *PesPtr; uint8_t *PesPtr;
unsigned int MetadataLength; 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]; PesPtr = &PesPacket[PES_MIN_HEADER_SIZE];