- formatting code using astyle

Conflicts:
	libarmbox/dmx.cpp
	libgeneric-pc/video_lib.h
	libspark/dmx.cpp

Signed-off-by: Thilo Graf <dbt@novatux.de>
This commit is contained in:
svenhoefer
2021-05-17 23:47:39 +02:00
committed by Thilo Graf
parent 42264ba4af
commit 7e5b1fc5d2
161 changed files with 13043 additions and 11396 deletions

View File

@@ -41,7 +41,8 @@
static inline void Hexdump(unsigned char *Data, int length)
{
int k;
for (k = 0; k < length; k++) {
for (k = 0; k < length; k++)
{
printf("%02x ", Data[k]);
if (((k + 1) & 31) == 0)
printf("\n");
@@ -84,7 +85,7 @@ static inline int aac_get_sample_rate_index(uint32_t sample_rate)
}
#if 0
static unsigned char DefaultAACHeader[] = {0xff,0xf1,0x50,0x80,0x00,0x1f,0xfc};
static unsigned char DefaultAACHeader[] = {0xff, 0xf1, 0x50, 0x80, 0x00, 0x1f, 0xfc};
#endif
class WriterAAC : public Writer
@@ -109,7 +110,7 @@ void WriterAAC::Init(int _fd, AVStream *_stream, Player *_player)
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 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)
@@ -123,7 +124,7 @@ void WriterAAC::Init(int _fd, AVStream *_stream, Player *_player)
printf("aac sample_index %d\n", sample_index);
printf("aac chan_config %d\n", chan_config);
#endif
object_type -= 1; // Cause of ADTS
object_type -= 1; // Cause of ADTS
aacbuflen = AAC_HEADER_LENGTH;
aacbuf[0] = 0xFF;
aacbuf[1] = 0xF1;
@@ -147,7 +148,7 @@ bool WriterAAC::Write(AVPacket *packet, int64_t pts)
uint8_t PesHeader[PES_MAX_HEADER_SIZE];
uint8_t ExtraData[AAC_HEADER_LENGTH];
for (int pos = 0; pos < packet->size + 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);
@@ -182,4 +183,4 @@ WriterAAC::WriterAAC()
Register(this, AV_CODEC_ID_AAC, AUDIO_ENCODING_AAC);
}
static WriterAAC writer_aac __attribute__ ((init_priority (300)));
static WriterAAC writer_aac __attribute__((init_priority(300)));

View File

@@ -46,7 +46,8 @@ bool WriterAC3::Write(AVPacket *packet, int64_t pts)
uint8_t PesHeader[PES_MAX_HEADER_SIZE];
for (int pos = 0; pos < packet->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;
@@ -69,4 +70,4 @@ WriterAC3::WriterAC3()
Register(this, AV_CODEC_ID_EAC3, AUDIO_ENCODING_AC3);
}
static WriterAC3 writer_ac3 __attribute__ ((init_priority (300)));
static WriterAC3 writer_ac3 __attribute__((init_priority(300)));

View File

@@ -58,7 +58,7 @@ bool WriterDIVX::Write(AVPacket *packet, int64_t pts)
return false;
uint8_t PesHeader[PES_MAX_HEADER_SIZE];
uint8_t FakeHeaders[64] = { 0 }; // 64bytes should be enough to make the fake headers
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;
@@ -72,10 +72,10 @@ bool WriterDIVX::Write(AVPacket *packet, int64_t pts)
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, 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);
@@ -88,7 +88,8 @@ bool WriterDIVX::Write(AVPacket *packet, int64_t pts)
iov[ic].iov_base = FakeHeaders;
iov[ic++].iov_len = FakeHeaderLength;
if (initialHeader) {
if (initialHeader)
{
iov[ic].iov_base = get_codecpar(stream)->extradata;
iov[ic++].iov_len = get_codecpar(stream)->extradata_size;
initialHeader = false;
@@ -107,4 +108,4 @@ WriterDIVX::WriterDIVX()
Register(this, AV_CODEC_ID_MSMPEG4V3, VIDEO_ENCODING_MPEG4P2);
}
static WriterDIVX writer_divx __attribute__ ((init_priority (300)));
static WriterDIVX writer_divx __attribute__((init_priority(300)));

View File

@@ -30,8 +30,8 @@
#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)
#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
{
@@ -53,7 +53,8 @@ bool WriterDTS::Write(AVPacket *packet, int64_t pts)
memcpy(Data, packet->data, packet->size);
/* 16-bit byte swap all data before injecting it */
for (i = 0; i < packet->size; i += 2) {
for (i = 0; i < packet->size; i += 2)
{
uint8_t Tmp = Data[i];
Data[i] = Data[i + 1];
Data[i + 1] = Tmp;
@@ -63,7 +64,7 @@ bool WriterDTS::Write(AVPacket *packet, int64_t pts)
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);
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
@@ -79,4 +80,4 @@ WriterDTS::WriterDTS()
Register(this, AV_CODEC_ID_DTS, AUDIO_ENCODING_DTS);
}
static WriterDTS writer_dts __attribute__ ((init_priority (300)));
static WriterDTS writer_dts __attribute__((init_priority(300)));

View File

@@ -71,4 +71,4 @@ WriterH263::WriterH263()
Register(this, AV_CODEC_ID_FLV1, VIDEO_ENCODING_FLV1);
}
static WriterH263 writer_h263 __attribute__ ((init_priority (300)));
static WriterH263 writer_h263 __attribute__((init_priority(300)));

View File

@@ -30,17 +30,18 @@
#include "pes.h"
#include "writer.h"
#define NALU_TYPE_PLAYER2_CONTAINER_PARAMETERS 24 // Reference: player/standards/h264.h
#define CONTAINER_PARAMETERS_VERSION 0x00
#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
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
@@ -74,14 +75,16 @@ bool WriterH264::Write(AVPacket *packet, int64_t pts)
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???
)) {
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) {
if (initialHeader)
{
initialHeader = false;
iov[ic].iov_base = get_codecpar(stream)->extradata;
iov[ic++].iov_len = get_codecpar(stream)->extradata_size;
@@ -103,10 +106,12 @@ bool WriterH264::Write(AVPacket *packet, int64_t pts)
}
// convert NAL units without sync byte sequence to byte-stream format
if (initialHeader) {
if (initialHeader)
{
avcC_t *avcCHeader = (avcC_t *) get_codecpar(stream)->extradata;
if (!avcCHeader) {
if (!avcCHeader)
{
fprintf(stderr, "stream->codec->extradata == NULL\n");
return false;
}
@@ -122,35 +127,35 @@ bool WriterH264::Write(AVPacket *packet, int64_t pts)
uint8_t Header[20];
unsigned int len = 0;
Header[len++] = 0x00; // Start code, 00 00 00 01 for first NAL unit
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
Header[len++] = 0xff; // marker bits
#if 0
#if 0
if (FrameRate == 0xffffffff)
FrameRate = (TimeScale > 1000) ? 1001 : 1;
#endif
#endif
Header[len++] = (TimeScale >> 24) & 0xff; // Output the timescale
Header[len++] = (TimeScale >> 24) & 0xff; // Output the timescale
Header[len++] = (TimeScale >> 16) & 0xff;
Header[len++] = 0xff; // marker bits
Header[len++] = 0xff; // marker bits
Header[len++] = (TimeScale >> 8) & 0xff;
Header[len++] = (TimeScale ) & 0xff;
Header[len++] = 0xff; // marker bits
Header[len++] = (TimeScale) & 0xff;
Header[len++] = 0xff; // marker bits
Header[len++] = (FrameRate >> 24) & 0xff; // Output frame period (should be: time delta)
Header[len++] = (FrameRate >> 24) & 0xff; // Output frame period (should be: time delta)
Header[len++] = (FrameRate >> 16) & 0xff;
Header[len++] = 0xff; // marker bits
Header[len++] = 0xff; // marker bits
Header[len++] = (FrameRate >> 8) & 0xff;
Header[len++] = (FrameRate ) & 0xff;
Header[len++] = 0xff; // marker bits
Header[len++] = (FrameRate) & 0xff;
Header[len++] = 0xff; // marker bits
Header[len++] = 0x80; // Rsbp trailing bits
Header[len++] = 0x80; // Rsbp trailing bits
int ic = 0;
iov[ic].iov_base = PesHeader;
@@ -169,7 +174,8 @@ bool WriterH264::Write(AVPacket *packet, int64_t pts)
// sequence parameter set
unsigned int ParamSets = avcCHeader->NumParamSets & 0x1f;
for (unsigned int i = 0; i < ParamSets; i++) {
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";
@@ -184,7 +190,8 @@ bool WriterH264::Write(AVPacket *packet, int64_t pts)
// picture parameter set
ParamSets = avcCHeader->Params[ParamOffset++];
for (unsigned int i = 0; i < ParamSets; i++) {
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";
@@ -205,9 +212,11 @@ bool WriterH264::Write(AVPacket *packet, int64_t pts)
}
uint8_t *de = d + packet->size;
do {
do
{
unsigned int len = 0;
switch (NalLengthBytes) {
switch (NalLengthBytes)
{
case 4:
len = *d;
d++;
@@ -225,8 +234,9 @@ bool WriterH264::Write(AVPacket *packet, int64_t pts)
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));
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;
}
@@ -245,7 +255,8 @@ bool WriterH264::Write(AVPacket *packet, int64_t pts)
d += len;
pts = INVALID_PTS_VALUE;
} while (d < de);
}
while (d < de);
return true;
}
@@ -255,4 +266,4 @@ WriterH264::WriterH264()
Register(this, AV_CODEC_ID_H264, VIDEO_ENCODING_H264);
}
static WriterH264 writerh264 __attribute__ ((init_priority (300)));
static WriterH264 writerh264 __attribute__((init_priority(300)));

View File

@@ -28,7 +28,7 @@
#include "misc.h"
void PutBits(BitPacker_t * ld, unsigned int code, unsigned int length)
void PutBits(BitPacker_t *ld, unsigned int code, unsigned int length)
{
unsigned int bit_buf;
unsigned int bit_left;
@@ -39,20 +39,23 @@ void PutBits(BitPacker_t * ld, unsigned int code, unsigned int length)
#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);
code, length, bit_buf, bit_left);
#endif
if (length < bit_left) {
if (length < bit_left)
{
/* fits into current buffer */
bit_buf = (bit_buf << length) | code;
bit_left -= length;
} else {
}
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[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;
@@ -71,10 +74,11 @@ void PutBits(BitPacker_t * ld, unsigned int code, unsigned int length)
ld->Remaining = bit_left;
}
void FlushBits(BitPacker_t * ld)
void FlushBits(BitPacker_t *ld)
{
ld->BitBuffer <<= ld->Remaining;
while (ld->Remaining < 32) {
while (ld->Remaining < 32)
{
#ifdef DEBUG_PUTBITS
if (ld->debug)
dprintf("flushing 0x%2.2x\n", ld->BitBuffer >> 24);

View File

@@ -46,7 +46,8 @@ bool WriterMP3::Write(AVPacket *packet, int64_t pts)
uint8_t PesHeader[PES_MAX_HEADER_SIZE];
for (int pos = 0; pos < packet->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;
@@ -71,4 +72,4 @@ WriterMP3::WriterMP3()
Register(this, AV_CODEC_ID_FLAC, AUDIO_ENCODING_LPCM);
}
static WriterMP3 writer_mp3 __attribute__ ((init_priority (300)));
static WriterMP3 writer_mp3 __attribute__((init_priority(300)));

View File

@@ -46,7 +46,8 @@ bool WriterMPEG2::Write(AVPacket *packet, int64_t pts)
uint8_t PesHeader[PES_MAX_HEADER_SIZE];
for (int pos = 0; pos < packet->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;
@@ -68,4 +69,4 @@ WriterMPEG2::WriterMPEG2()
Register(this, AV_CODEC_ID_MPEG2TS, VIDEO_ENCODING_AUTO);
}
static WriterMPEG2 writer_mpeg2 __attribute__ ((init_priority (300)));
static WriterMPEG2 writer_mpeg2 __attribute__((init_priority(300)));

View File

@@ -43,18 +43,19 @@ extern "C" {
}
// 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
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
@@ -98,7 +99,8 @@ bool WriterPCM::prepareClipPlay()
memcpy(lpcm_prv, clpcm_prv, sizeof(lpcm_prv));
// figure out size of subframe and set up sample rate
switch (uSampleRate) {
switch (uSampleRate)
{
case 48000:
SubFrameLen = 40;
break;
@@ -136,7 +138,8 @@ bool WriterPCM::prepareClipPlay()
//set number of channels
lpcm_prv[10] = uNoOfChannels - 1;
switch (uBitsPerSample) {
switch (uBitsPerSample)
{
case 24:
lpcm_prv[7] |= 0x20;
case 16:
@@ -154,7 +157,8 @@ bool WriterPCM::writePCM(int64_t Pts, uint8_t *data, unsigned int size)
bool res = true;
uint8_t PesHeader[PES_MAX_HEADER_SIZE];
if (initialHeader) {
if (initialHeader)
{
initialHeader = false;
prepareClipPlay();
ioctl(fd, AUDIO_CLEAR_BUFFER, NULL);
@@ -162,7 +166,8 @@ bool WriterPCM::writePCM(int64_t Pts, uint8_t *data, unsigned int size)
size += breakBufferFillSize;
while (size >= SubFrameLen) {
while (size >= SubFrameLen)
{
if (breakBufferFillSize)
memcpy(injectBuffer, breakBuffer, breakBufferFillSize);
memcpy(injectBuffer + breakBufferFillSize, data, SubFrameLen - breakBufferFillSize);
@@ -171,17 +176,22 @@ bool WriterPCM::writePCM(int64_t Pts, uint8_t *data, unsigned int size)
breakBufferFillSize = 0;
//write the PCM data
if (uBitsPerSample == 16) {
for (unsigned int n = 0; n < SubFrameLen; n += 2) {
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 {
}
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) {
for (unsigned int n = 0; n < SubFrameLen; n += 12)
{
uint8_t t, *p = injectBuffer + n;
t = p[0];
p[0] = p[2];
@@ -207,12 +217,14 @@ bool WriterPCM::writePCM(int64_t Pts, uint8_t *data, unsigned int size)
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) {
if (len < 0)
{
res = false;
break;
}
}
if (size && res) {
if (size && res)
{
breakBufferFillSize = size;
memcpy(breakBuffer, data, size);
}
@@ -231,58 +243,67 @@ void WriterPCM::Init(int _fd, AVStream *_stream, Player *_player)
bool WriterPCM::Write(AVPacket *packet, int64_t pts)
{
if (!packet) {
if (!packet)
{
restart_audio_resampling = true;
return true;
}
AVCodecContext *c = player->input.GetCodecContext((unsigned int)stream->index);
if (restart_audio_resampling) {
if (restart_audio_resampling)
{
restart_audio_resampling = false;
initialHeader = true;
if (swr) {
if (swr)
{
swr_free(&swr);
swr = NULL;
}
if (decoded_frame) {
if (decoded_frame)
{
av_frame_free(&decoded_frame);
decoded_frame = NULL;
}
#if LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(57,25,101)
AVCodec *codec = avcodec_find_decoder(c->codec_id);
if (!codec) {
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)) {
if (avcodec_open2(c, codec, NULL))
{
fprintf(stderr, "%s %d: avcodec_open2 failed\n", __func__, __LINE__);
return false;
}
#endif
}
if (!swr) {
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++;
i++;
out_sample_rate = rates[i] ? rates[i] : 44100;
out_channels = c->channels;
if (c->channel_layout == 0) {
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) {
if (out_channel_layout == AV_CH_LAYOUT_MONO)
{
out_channel_layout = AV_CH_LAYOUT_STEREO;
out_channels = 2;
}
@@ -292,7 +313,8 @@ bool WriterPCM::Write(AVPacket *packet, int64_t pts)
uBitsPerSample = 16;
swr = swr_alloc();
if (!swr) {
if (!swr)
{
fprintf(stderr, "%s %d: swr_alloc failed\n", __func__, __LINE__);
return false;
}
@@ -304,31 +326,37 @@ bool WriterPCM::Write(AVPacket *packet, int64_t pts)
av_opt_set_sample_fmt(swr, "out_sample_fmt", AV_SAMPLE_FMT_S16, 0);
int e = swr_init(swr);
if (e < 0) {
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);
-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)) {
while (packet_size > 0 || (!packet_size && !packet->data))
{
if (!decoded_frame) {
if (!(decoded_frame = av_frame_alloc())) {
if (!decoded_frame)
{
if (!(decoded_frame = av_frame_alloc()))
{
fprintf(stderr, "out of memory\n");
exit(1);
}
} else
}
else
av_frame_unref(decoded_frame);
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(57,37,100)
int got_frame = 0;
int len = avcodec_decode_audio4(c, decoded_frame, &got_frame, packet);
if (len < 0) {
if (len < 0)
{
restart_audio_resampling = true;
break;
}
@@ -336,35 +364,43 @@ bool WriterPCM::Write(AVPacket *packet, int64_t pts)
if (packet->data)
packet_size -= len;
if (!got_frame) {
if (!got_frame)
{
if (!packet->data || !packet_size)
break;
continue;
}
#else
int ret = avcodec_send_packet(c, packet);
if (ret < 0) {
if (ret != AVERROR(EAGAIN) && ret != AVERROR_EOF) {
restart_audio_resampling = true;
break;
}
int ret = avcodec_send_packet(c, packet);
if (ret < 0)
{
if (ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
{
restart_audio_resampling = true;
break;
}
if (ret >= 0) {
packet_size = 0;
}
if (ret >= 0)
{
packet_size = 0;
}
ret = avcodec_receive_frame(c, decoded_frame);
if (ret < 0)
{
if (ret == AVERROR_EOF)
{
restart_audio_resampling = true;
break;
}
ret = avcodec_receive_frame(c, decoded_frame);
if (ret < 0) {
if (ret == AVERROR_EOF) {
restart_audio_resampling = true;
break;
}
if (ret != AVERROR(EAGAIN)) {
break;
}
else {
continue;
}
if (ret != AVERROR(EAGAIN))
{
break;
}
else
{
continue;
}
}
#endif
#if (LIBAVUTIL_VERSION_MAJOR < 54)
@@ -375,11 +411,13 @@ bool WriterPCM::Write(AVPacket *packet, int64_t pts)
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 (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) {
if (e < 0)
{
fprintf(stderr, "av_samples_alloc: %d\n", -e);
break;
}
@@ -388,7 +426,8 @@ bool WriterPCM::Write(AVPacket *packet, int64_t pts)
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)) {
if (!writePCM(pts, output, out_samples * sizeof(short) * out_channels))
{
restart_audio_resampling = true;
break;
}
@@ -409,4 +448,4 @@ WriterPCM::WriterPCM()
Register(this, AV_CODEC_ID_INJECTPCM, AUDIO_ENCODING_LPCMA);
}
static WriterPCM writer_pcm __attribute__ ((init_priority (300)));
static WriterPCM writer_pcm __attribute__((init_priority(300)));

View File

@@ -53,44 +53,45 @@ int InsertPesHeader(uint8_t *data, int size, uint8_t stream_id, int64_t pts, int
{
BitPacker_t ld2 = { data, 0, 32 };
/* if (size > MAX_PES_PACKET_SIZE)
size = 0; // unbounded */
/* 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
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
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
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, 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
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
PutBits(&ld2, 0x0, 8); // PES_header_data_length
//9 = 8+1
if (pts != INVALID_PTS_VALUE) {
if (pts != INVALID_PTS_VALUE)
{
PutBits(&ld2, 0x2, 4);
PutBits(&ld2, (pts >> 30) & 0x7, 3);
PutBits(&ld2, 0x1, 1);
@@ -101,12 +102,13 @@ int InsertPesHeader(uint8_t *data, int size, uint8_t stream_id, int64_t pts, int
}
//14 = 9+5
if (pic_start_code) {
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)
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
}

View File

@@ -32,16 +32,16 @@
#include "pes.h"
#include "writer.h"
#define WMV3_PRIVATE_DATA_LENGTH 4
#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 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
#define VC1_SEQUENCE_LAYER_METADATA_START_CODE 0x80
#define VC1_FRAME_START_CODE 0x0d
class WriterVC1 : public Writer
{
@@ -68,22 +68,24 @@ bool WriterVC1::Write(AVPacket *packet, int64_t pts)
if (!packet || !packet->data)
return false;
if (initialHeader) {
if (initialHeader)
{
initialHeader = false;
FrameHeaderSeen = false;
const uint8_t SequenceLayerStartCode[] =
{ 0x00, 0x00, 0x01, VC1_SEQUENCE_LAYER_METADATA_START_CODE };
{ 0x00, 0x00, 0x01, VC1_SEQUENCE_LAYER_METADATA_START_CODE };
const uint8_t Metadata[] = {
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 */
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 */
0x60, 0x00, 0x00, 0x00, /* Struct B */
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00
};
@@ -116,7 +118,7 @@ bool WriterVC1::Write(AVPacket *packet, int64_t pts)
*PesPtr++ = (get_codecpar(stream)->width >> 16) & 0xff;
*PesPtr++ = get_codecpar(stream)->width >> 24;
PesPtr += 12; /* Skip flag word and Struct B first 8 bytes */
PesPtr += 12; /* Skip flag word and Struct B first 8 bytes */
*PesPtr++ = (usecPerFrame >> 0) & 0xff;
*PesPtr++ = (usecPerFrame >> 8) & 0xff;
@@ -141,21 +143,25 @@ bool WriterVC1::Write(AVPacket *packet, int64_t pts)
initialHeader = false;
}
if (packet->size > 0) {
if (packet->size > 0)
{
int Position = 0;
bool insertSampleHeader = true;
while (Position < packet->size) {
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) {
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) {
if (!FrameHeaderSeen)
{
memcpy(&PesHeader[HeaderLength], Vc1FrameStartCode, sizeof(Vc1FrameStartCode));
HeaderLength += sizeof(Vc1FrameStartCode);
}
@@ -185,4 +191,4 @@ WriterVC1::WriterVC1()
Register(this, AV_CODEC_ID_VC1, VIDEO_ENCODING_VC1);
}
static WriterVC1 writer_vc1 __attribute__ ((init_priority (300)));
static WriterVC1 writer_vc1 __attribute__((init_priority(300)));

View File

@@ -33,21 +33,22 @@
#include <algorithm>
#define WMV3_PRIVATE_DATA_LENGTH 4
#define WMV3_PRIVATE_DATA_LENGTH 4
static const uint8_t Metadata[] = {
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 */
#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 */
#define METADATA_STRUCT_B_START 24
0x60, 0x00, 0x00, 0x00, /* Struct B */
0x00, 0x00, 0x00, 0x00,
#define METADATA_STRUCT_B_FRAMERATE_START 32
#define METADATA_STRUCT_B_FRAMERATE_START 32
0x00, 0x00, 0x00, 0x00
};
@@ -75,7 +76,8 @@ bool WriterWMV::Write(AVPacket *packet, int64_t pts)
if (!packet || !packet->data)
return false;
if (initialHeader) {
if (initialHeader)
{
#define PES_MIN_HEADER_SIZE 9
uint8_t PesPacket[PES_MIN_HEADER_SIZE + 128];
uint8_t *PesPtr;
@@ -103,7 +105,7 @@ bool WriterWMV::Write(AVPacket *packet, int64_t pts)
*PesPtr++ = (get_codecpar(stream)->width >> 16) & 0xff;
*PesPtr++ = get_codecpar(stream)->width >> 24;
PesPtr += 12; /* Skip flag word and Struct B first 8 bytes */
PesPtr += 12; /* Skip flag word and Struct B first 8 bytes */
*PesPtr++ = (usecPerFrame >> 0) & 0xff;
*PesPtr++ = (usecPerFrame >> 8) & 0xff;
@@ -120,18 +122,21 @@ bool WriterWMV::Write(AVPacket *packet, int64_t pts)
initialHeader = false;
}
if (packet->size > 0 && packet->data) {
if (packet->size > 0 && packet->data)
{
int Position = 0;
bool insertSampleHeader = true;
while (Position < packet->size) {
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) {
if (insertSampleHeader)
{
unsigned int PesLength;
unsigned int PrivateHeaderLength;
@@ -168,4 +173,4 @@ WriterWMV::WriterWMV()
Register(this, AV_CODEC_ID_WMV3, VIDEO_ENCODING_WMV);
}
static WriterWMV writer_wmv __attribute__ ((init_priority (300)));
static WriterWMV writer_wmv __attribute__((init_priority(300)));

View File

@@ -43,9 +43,9 @@
#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)));
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)
{
@@ -64,17 +64,19 @@ bool Writer::Write(AVPacket * /* packet */, int64_t /* pts */)
return false;
}
static Writer writer __attribute__ ((init_priority (300)));
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 (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) {
switch (codec_type)
{
case AVMEDIA_TYPE_AUDIO:
if (id == AV_CODEC_ID_INJECTPCM) // should not happen
break;
@@ -91,7 +93,7 @@ Writer *Writer::GetWriter(enum AVCodecID id, enum AVMediaType codec_type, int tr
video_encoding_t Writer::GetVideoEncoding(enum AVCodecID id)
{
std::map<enum AVCodecID,video_encoding_t>::iterator it = vencoding.find(id);
std::map<enum AVCodecID, video_encoding_t>::iterator it = vencoding.find(id);
if (it != vencoding.end())
return it->second;
return VIDEO_ENCODING_AUTO;
@@ -99,7 +101,7 @@ video_encoding_t Writer::GetVideoEncoding(enum AVCodecID id)
audio_encoding_t Writer::GetAudioEncoding(enum AVCodecID id)
{
std::map<enum AVCodecID,audio_encoding_t>::iterator it = aencoding.find(id);
std::map<enum AVCodecID, audio_encoding_t>::iterator it = aencoding.find(id);
if (it != aencoding.end())
return it->second;
return AUDIO_ENCODING_LPCMA;