From 546eb23e0e0b3615c0058564a4f7bd1772d05492 Mon Sep 17 00:00:00 2001 From: martii Date: Sat, 8 Jun 2013 11:15:09 +0200 Subject: [PATCH] libeplayer3: further malloc-memcpy-write -> writev replacements; not fully regression tested --- libeplayer3/output/writer/aac.c | 29 +------------- libeplayer3/output/writer/ac3.c | 16 ++++---- libeplayer3/output/writer/divx.c | 23 +++++------ libeplayer3/output/writer/dts.c | 26 ++++++++---- libeplayer3/output/writer/flac.c | 16 ++++---- libeplayer3/output/writer/h263.c | 22 ++++------- libeplayer3/output/writer/mp3.c | 16 ++++---- libeplayer3/output/writer/mpeg2.c | 23 ++++++----- libeplayer3/output/writer/vc1.c | 22 ++++++----- libeplayer3/output/writer/vorbis.c | 16 ++++---- libeplayer3/output/writer/wma.c | 33 +++++++++------- libeplayer3/output/writer/wmv.c | 63 +++++++++++++++--------------- 12 files changed, 141 insertions(+), 164 deletions(-) diff --git a/libeplayer3/output/writer/aac.c b/libeplayer3/output/writer/aac.c index bca9281..4856d95 100644 --- a/libeplayer3/output/writer/aac.c +++ b/libeplayer3/output/writer/aac.c @@ -249,30 +249,17 @@ static int writeData(void* _call) if (call->private_data == NULL) { aac_printf(10, "private_data = NULL\n"); - -#ifdef MARTII memcpy (ExtraData, DefaultAACHeader, AAC_HEADER_LENGTH); -#else - call->private_data = DefaultAACHeader; - call->private_size = AAC_HEADER_LENGTH; -#endif } -#ifdef MARTII else -#endif + memcpy (ExtraData, call->private_data, AAC_HEADER_LENGTH); - memcpy (ExtraData, call->private_data, AAC_HEADER_LENGTH); -#ifdef MARTII ExtraData[3] |= (PacketLength >> 11) & 0x3; -#else - ExtraData[3] |= (PacketLength >> 12) & 0x3; -#endif ExtraData[4] = (PacketLength >> 3) & 0xff; ExtraData[5] |= (PacketLength << 5) & 0xe0; unsigned int HeaderLength = InsertPesHeader (PesHeader, PacketLength, AAC_AUDIO_PES_START_CODE, call->Pts, 0); -#ifdef MARTII struct iovec iov[3]; iov[0].iov_base = PesHeader; iov[0].iov_len = HeaderLength; @@ -281,20 +268,6 @@ static int writeData(void* _call) iov[2].iov_base = call->data; iov[2].iov_len = call->len; return writev(call->fd, iov, 3); -#else - unsigned char* PacketStart = malloc(HeaderLength + sizeof(ExtraData) + call->len); - memcpy (PacketStart, PesHeader, HeaderLength); - memcpy (PacketStart + HeaderLength, ExtraData, sizeof(ExtraData)); - memcpy (PacketStart + HeaderLength + sizeof(ExtraData), call->data, call->len); - - aac_printf(100, "H %d d %d ExtraData %d\n", HeaderLength, call->len, sizeof(ExtraData)); - - int len = write(call->fd, PacketStart, HeaderLength + call->len + sizeof(ExtraData)); - - free(PacketStart); - - return len; -#endif } /* ***************************** */ diff --git a/libeplayer3/output/writer/ac3.c b/libeplayer3/output/writer/ac3.c index 91faf67..d6b894c 100644 --- a/libeplayer3/output/writer/ac3.c +++ b/libeplayer3/output/writer/ac3.c @@ -31,6 +31,7 @@ #include #include #include +#include #include #include #include @@ -119,17 +120,14 @@ static int writeData(void* _call) return 0; } - int HeaderLength = InsertPesHeader (PesHeader, call->len, PRIVATE_STREAM_1_PES_START_CODE, call->Pts, 0); + struct iovec iov[2]; - unsigned char* PacketStart = malloc(call->len + HeaderLength); - memcpy (PacketStart, PesHeader, HeaderLength); - memcpy (PacketStart + HeaderLength, call->data, call->len); + iov[0].iov_base = PesHeader; + iov[0].iov_len = InsertPesHeader (PesHeader, call->len, PRIVATE_STREAM_1_PES_START_CODE, call->Pts, 0); + iov[1].iov_base = call->data; + iov[1].iov_len = call->len; - int len = write(call->fd, PacketStart, call->len + HeaderLength); - - free(PacketStart); - - return len; + return writev(call->fd, iov, 2); } /* ***************************** */ diff --git a/libeplayer3/output/writer/divx.c b/libeplayer3/output/writer/divx.c index eaab2fd..ad1b089 100644 --- a/libeplayer3/output/writer/divx.c +++ b/libeplayer3/output/writer/divx.c @@ -31,6 +31,7 @@ #include #include #include +#include #include #include #include @@ -98,7 +99,6 @@ static int writeData(void* _call) unsigned char PesHeader[PES_MAX_HEADER_SIZE]; unsigned char FakeHeaders[64]; // 64bytes should be enough to make the fake headers unsigned int FakeHeaderLength; - unsigned int ExtraLength = 0; unsigned char Version = 5; unsigned int FakeStartCode = (Version << 8) | PES_VERSION_FAKE_START_CODE; unsigned int HeaderLength = 0; @@ -148,21 +148,22 @@ static int writeData(void* _call) FakeHeaderLength = (ld.Ptr - (FakeHeaders)); - if (initialHeader) ExtraLength = call->private_size; + struct iovec iov[4]; + int ic = 0; + iov[ic].iov_base = PesHeader; + iov[ic++].iov_len = InsertPesHeader (PesHeader, call->len, MPEG_VIDEO_PES_START_CODE, call->Pts, FakeStartCode); + iov[ic].iov_base = FakeHeaders; + iov[ic++].iov_len = FakeHeaderLength; - HeaderLength = InsertPesHeader (PesHeader, call->len, MPEG_VIDEO_PES_START_CODE, call->Pts, FakeStartCode); - unsigned char* PacketStart = malloc(call->len + HeaderLength + FakeHeaderLength + ExtraLength); - memcpy (PacketStart, PesHeader, HeaderLength); - memcpy (PacketStart + HeaderLength, FakeHeaders, FakeHeaderLength); if (initialHeader) { - memcpy (PacketStart + HeaderLength + FakeHeaderLength, call->private_data, call->private_size); + iov[ic].iov_base = call->private_data; + iov[ic++].iov_len = call->private_size; initialHeader = 0; } - memcpy (PacketStart + HeaderLength + FakeHeaderLength + ExtraLength, call->data, call->len); + iov[ic].iov_base = call->data; + iov[ic++].iov_len = call->len; - int len = write(call->fd, PacketStart ,call->len + HeaderLength + FakeHeaderLength + ExtraLength); - - free(PacketStart); + int len = writev(call->fd, iov, ic); divx_printf(10, "xvid_Write < len=%d\n", len); diff --git a/libeplayer3/output/writer/dts.c b/libeplayer3/output/writer/dts.c index f22387a..971e6e6 100644 --- a/libeplayer3/output/writer/dts.c +++ b/libeplayer3/output/writer/dts.c @@ -31,6 +31,7 @@ #include #include #include +#include #include #include #include @@ -99,7 +100,6 @@ static int writeData(void* _call) int i = 0; unsigned char PesHeader[PES_AUDIO_HEADER_SIZE]; - unsigned char * Data = 0; dts_printf(10, "\n"); @@ -125,7 +125,9 @@ static int writeData(void* _call) memset (PesHeader, '0', PES_AUDIO_HEADER_SIZE); - Data = (unsigned char *) malloc(call->len); +// #define DO_BYTESWAP +#ifdef DO_BYTESWAP + unsigned char *Data = (unsigned char *) malloc(call->len); memcpy(Data, call->data, call->len); /* 16-bit byte swap all data before injecting it */ @@ -135,16 +137,24 @@ static int writeData(void* _call) Data[i] = Data[i+1]; Data[i+1] = Tmp; } +#endif - int HeaderLength = InsertPesHeader (PesHeader, call->len, MPEG_AUDIO_PES_START_CODE/*PRIVATE_STREAM_1_PES_START_CODE*/, call->Pts, 0); - unsigned char* PacketStart = malloc(call->len + HeaderLength); - memcpy (PacketStart, PesHeader, HeaderLength); - memcpy (PacketStart + HeaderLength, call->data, call->len); + struct iovec iov[2]; - int len = write(call->fd,PacketStart,call->len + HeaderLength); + iov[0].iov_base = PesHeader; + iov[0].iov_len = InsertPesHeader (PesHeader, call->len, MPEG_AUDIO_PES_START_CODE/*PRIVATE_STREAM_1_PES_START_CODE*/, call->Pts, 0); +#ifdef DO_BYTESPWAP + iov[1].iov_base = Data; +#else + iov[1].iov_base = call->data; +#endif + iov[1].iov_len = call->len; - free(PacketStart); + int len = writev(call->fd, iov, 2); + +#ifdef DO_BYTESWAP free(Data); +#endif dts_printf(10, "< len %d\n", len); return len; diff --git a/libeplayer3/output/writer/flac.c b/libeplayer3/output/writer/flac.c index b837a2b..09836bb 100644 --- a/libeplayer3/output/writer/flac.c +++ b/libeplayer3/output/writer/flac.c @@ -31,6 +31,7 @@ #include #include #include +#include #include #include #include @@ -117,16 +118,13 @@ static int writeData(void* _call) return 0; } - int HeaderLength = InsertPesHeader (PesHeader, call->len , MPEG_AUDIO_PES_START_CODE, call->Pts, 0); + struct iovec iov[2]; + iov[0].iov_base = PesHeader; + iov[0].iov_len = InsertPesHeader (PesHeader, call->len , MPEG_AUDIO_PES_START_CODE, call->Pts, 0); + iov[1].iov_base = call->data; + iov[1].iov_len = call->len; - unsigned char* PacketStart = malloc(call->len + HeaderLength); - - memcpy (PacketStart, PesHeader, HeaderLength); - memcpy (PacketStart + HeaderLength, call->data, call->len); - - int len = write(call->fd, PacketStart, call->len + HeaderLength); - - free(PacketStart); + int len = writev(call->fd, iov, 2); flac_printf(10, "flac_Write-< len=%d\n", len); return len; diff --git a/libeplayer3/output/writer/h263.c b/libeplayer3/output/writer/h263.c index abcc457..119fbfb 100644 --- a/libeplayer3/output/writer/h263.c +++ b/libeplayer3/output/writer/h263.c @@ -37,6 +37,7 @@ #include #include #include +#include #include "common.h" #include "output.h" @@ -130,21 +131,12 @@ static int writeData(void* _call) HeaderLength += PrivateHeaderLength; - unsigned char *PacketData = malloc(HeaderLength + call->len); - - if(PacketData != NULL) - { - memcpy(PacketData, PesHeader, HeaderLength); - memcpy(PacketData + HeaderLength, call->data, call->len); - - len = write(call->fd, PacketData, call->len + HeaderLength); - - free(PacketData); - } - else - { - h263_err("no mem\n"); - } + struct iovec iov[2]; + iov[0].iov_base = PesHeader; + iov[0].iov_len = HeaderLength; + iov[1].iov_base = call->data; + iov[1].iov_len = call->len; + len = writev(call->fd, iov, 2); h263_printf(10, "< len %d\n", len); return len; diff --git a/libeplayer3/output/writer/mp3.c b/libeplayer3/output/writer/mp3.c index 186c12d..64adc55 100644 --- a/libeplayer3/output/writer/mp3.c +++ b/libeplayer3/output/writer/mp3.c @@ -31,6 +31,7 @@ #include #include #include +#include #include #include #include @@ -117,16 +118,13 @@ static int writeData(void* _call) return 0; } - int HeaderLength = InsertPesHeader (PesHeader, call->len , MPEG_AUDIO_PES_START_CODE, call->Pts, 0); + struct iovec iov[2]; + iov[0].iov_base = PesHeader; + iov[0].iov_len = InsertPesHeader (PesHeader, call->len , MPEG_AUDIO_PES_START_CODE, call->Pts, 0); + iov[1].iov_base = call->data; + iov[1].iov_len = call->len; - unsigned char* PacketStart = malloc(call->len + HeaderLength); - - memcpy (PacketStart, PesHeader, HeaderLength); - memcpy (PacketStart + HeaderLength, call->data, call->len); - - int len = write(call->fd, PacketStart, call->len + HeaderLength); - - free(PacketStart); + int len = writev(call->fd, iov, 2); mp3_printf(10, "mp3_Write-< len=%d\n", len); return len; diff --git a/libeplayer3/output/writer/mpeg2.c b/libeplayer3/output/writer/mpeg2.c index 505f7bf..beb1010 100644 --- a/libeplayer3/output/writer/mpeg2.c +++ b/libeplayer3/output/writer/mpeg2.c @@ -31,6 +31,7 @@ #include #include #include +#include #include #include #include @@ -120,7 +121,7 @@ static int writeData(void* _call) return 0; } - while(1) { + while(Position < call->len) { int PacketLength = (call->len - Position) <= MAX_PES_PACKET_SIZE ? (call->len - Position) : MAX_PES_PACKET_SIZE; @@ -128,19 +129,21 @@ static int writeData(void* _call) mpeg2_printf(20, "PacketLength=%d, Remaining=%d, Position=%d\n", PacketLength, Remaining, Position); - int HeaderLength = InsertPesHeader (PesHeader, PacketLength, 0xe0, call->Pts, 0); - unsigned char* PacketStart = malloc(PacketLength + HeaderLength); - memcpy (PacketStart, PesHeader, HeaderLength); - memcpy (PacketStart + HeaderLength, call->data + Position, PacketLength); + struct iovec iov[2]; + iov[0].iov_base = PesHeader; + iov[0].iov_len = InsertPesHeader (PesHeader, PacketLength, 0xe0, call->Pts, 0); + iov[1].iov_base = call->data + Position; + iov[1].iov_len = PacketLength; - len = write(call->fd, PacketStart, PacketLength + HeaderLength); - free(PacketStart); + ssize_t l = writev(call->fd, iov, 2); + if (l < 0) { + len = l; + break; + } + len += l; Position += PacketLength; call->Pts = INVALID_PTS_VALUE; - - if (Position == call->len) - break; } mpeg2_printf(10, "< len %d\n", len); diff --git a/libeplayer3/output/writer/vc1.c b/libeplayer3/output/writer/vc1.c index d9e1565..27cecf3 100644 --- a/libeplayer3/output/writer/vc1.c +++ b/libeplayer3/output/writer/vc1.c @@ -31,6 +31,7 @@ #include #include #include +#include #include #include #include @@ -220,7 +221,7 @@ static int writeData(void* _call) int Position = 0; unsigned char insertSampleHeader = 1; - while(1) { + while(Position < call->len) { int PacketLength = (call->len - Position) <= MAX_PES_PACKET_SIZE ? (call->len - Position) : MAX_PES_PACKET_SIZE; @@ -254,18 +255,21 @@ static int writeData(void* _call) insertSampleHeader = 0; } - PacketStart = malloc(call->len + HeaderLength); - memcpy (PacketStart, PesHeader, HeaderLength); - memcpy (PacketStart + HeaderLength, call->data + Position, PacketLength); + struct iovec iov[2]; + iov[0].iov_base = PesHeader; + iov[0].iov_len = HeaderLength; + iov[1].iov_base = call->data + Position; + iov[1].iov_len = PacketLength; - len = write(call->fd, PacketStart, PacketLength + HeaderLength); - free(PacketStart); + size_t l = writev(call->fd, iov, 2); + if (l < 0) { + len = l; + break; + } + len += l; Position += PacketLength; call->Pts = INVALID_PTS_VALUE; - - if (Position == call->len) - break; } } diff --git a/libeplayer3/output/writer/vorbis.c b/libeplayer3/output/writer/vorbis.c index 1068f33..8442adc 100644 --- a/libeplayer3/output/writer/vorbis.c +++ b/libeplayer3/output/writer/vorbis.c @@ -31,6 +31,7 @@ #include #include #include +#include #include #include #include @@ -117,16 +118,13 @@ static int writeData(void* _call) return 0; } - int HeaderLength = InsertPesHeader (PesHeader, call->len , MPEG_AUDIO_PES_START_CODE, call->Pts, 0); + struct iovec iov[2]; + iov[0].iov_base = PesHeader; + iov[0].iov_len = InsertPesHeader (PesHeader, call->len , MPEG_AUDIO_PES_START_CODE, call->Pts, 0); + iov[1].iov_base = call->data; + iov[1].iov_len = call->len; - unsigned char* PacketStart = malloc(call->len + HeaderLength); - - memcpy (PacketStart, PesHeader, HeaderLength); - memcpy (PacketStart + HeaderLength, call->data, call->len); - - int len = write(call->fd, PacketStart, call->len + HeaderLength); - - free(PacketStart); + int len = writev(call->fd, iov, 2); vorbis_printf(10, "vorbis_Write-< len=%d\n", len); return len; diff --git a/libeplayer3/output/writer/wma.c b/libeplayer3/output/writer/wma.c index b025ba1..6bed650 100644 --- a/libeplayer3/output/writer/wma.c +++ b/libeplayer3/output/writer/wma.c @@ -31,6 +31,7 @@ #include #include #include +#include #include #include #include @@ -132,32 +133,34 @@ static int writeData(void* _call) return -1; } - HeaderLength = InsertPesHeader (PesHeader, call->private_size, MPEG_AUDIO_PES_START_CODE, 0, 0); - unsigned char* PacketStart = malloc(call->private_size + HeaderLength); - memcpy (PacketStart, PesHeader, HeaderLength); - memcpy (PacketStart + HeaderLength, call->private_data, call->private_size); + struct iovec iov[2]; + iov[0].iov_base = PesHeader; + iov[0].iov_len = InsertPesHeader (PesHeader, call->private_size, MPEG_AUDIO_PES_START_CODE, 0, 0); + iov[1].iov_base = call->private_data; + iov[1].iov_len = call->private_size; - len = write(call->fd, PacketStart, call->private_size + HeaderLength); - - free(PacketStart); + len = writev(call->fd, iov, 2); initialHeader = 0; } - if (call->len > 0 && call->data) + if (len > -1 && call->len > 0 && call->data) { unsigned char PesHeader[PES_MAX_HEADER_SIZE]; - int HeaderLength = InsertPesHeader (PesHeader, call->len, MPEG_AUDIO_PES_START_CODE, call->Pts, 0); - unsigned char* PacketStart = malloc(call->len + HeaderLength); - memcpy (PacketStart, PesHeader, HeaderLength); - memcpy (PacketStart + HeaderLength, call->data, call->len); + struct iovec iov[2]; + iov[0].iov_base = PesHeader; + iov[0].iov_len = InsertPesHeader (PesHeader, call->len, MPEG_AUDIO_PES_START_CODE, call->Pts, 0); + iov[1].iov_base = call->data; + iov[1].iov_len = call->len; - len = write(call->fd, PacketStart, call->len + HeaderLength); - - free(PacketStart); + ssize_t l = writev(call->fd, iov, 2); + if (l > -1) + len += l; + else + len = l; } wma_printf(10, "wma < %d\n", len); diff --git a/libeplayer3/output/writer/wmv.c b/libeplayer3/output/writer/wmv.c index b017925..c22a668 100644 --- a/libeplayer3/output/writer/wmv.c +++ b/libeplayer3/output/writer/wmv.c @@ -31,6 +31,7 @@ #include #include #include +#include #include #include #include @@ -124,7 +125,7 @@ static int writeData(void* _call) { WriterAVCallData_t* call = (WriterAVCallData_t*) _call; - awmv_t *private_data = (awmv_t *)malloc(sizeof(awmv_t)); + awmv_t private_data; int len = 0; wmv_printf(10, "\n"); @@ -138,12 +139,12 @@ static int writeData(void* _call) wmv_printf(10, "Got Private Size %d\n", call->private_size); - memcpy(private_data->privateData, call->private_data, + memcpy(private_data.privateData, call->private_data, call->private_size>WMV3_PRIVATE_DATA_LENGTH?WMV3_PRIVATE_DATA_LENGTH:call->private_size); - private_data->width = call->Width; - private_data->height = call->Height; - private_data->framerate = call->FrameRate; + private_data.width = call->Width; + private_data.height = call->Height; + private_data.framerate = call->FrameRate; if ((call->data == NULL) || (call->len <= 0)) { wmv_err("parsing NULL Data. ignoring...\n"); @@ -161,16 +162,11 @@ static int writeData(void* _call) unsigned int MetadataLength; unsigned int crazyFramerate = 0; - if (private_data == NULL) { - wmv_err("private_data NULL\n"); - return -1; - } + wmv_printf(10, "Framerate: %u\n", private_data.framerate); + wmv_printf(10, "biWidth: %d\n", private_data.width); + wmv_printf(10, "biHeight: %d\n", private_data.height); - wmv_printf(10, "Framerate: %u\n", private_data->framerate); - wmv_printf(10, "biWidth: %d\n", private_data->width); - wmv_printf(10, "biHeight: %d\n", private_data->height); - - crazyFramerate = ((10000000.0 / private_data->framerate) * 1000.0); + crazyFramerate = ((10000000.0 / private_data.framerate) * 1000.0); wmv_printf(10, "crazyFramerate: %u\n", crazyFramerate); PesPtr = &PesPacket[PES_MIN_HEADER_SIZE]; @@ -178,18 +174,18 @@ static int writeData(void* _call) memcpy (PesPtr, Metadata, sizeof(Metadata)); PesPtr += METADATA_STRUCT_C_START; - memcpy (PesPtr, private_data->privateData, WMV3_PRIVATE_DATA_LENGTH); + memcpy (PesPtr, private_data.privateData, WMV3_PRIVATE_DATA_LENGTH); PesPtr += WMV3_PRIVATE_DATA_LENGTH; /* Metadata Header Struct A */ - *PesPtr++ = (private_data->height >> 0) & 0xff; - *PesPtr++ = (private_data->height >> 8) & 0xff; - *PesPtr++ = (private_data->height >> 16) & 0xff; - *PesPtr++ = private_data->height >> 24; - *PesPtr++ = (private_data->width >> 0) & 0xff; - *PesPtr++ = (private_data->width >> 8) & 0xff; - *PesPtr++ = (private_data->width >> 16) & 0xff; - *PesPtr++ = private_data->width >> 24; + *PesPtr++ = (private_data.height >> 0) & 0xff; + *PesPtr++ = (private_data.height >> 8) & 0xff; + *PesPtr++ = (private_data.height >> 16) & 0xff; + *PesPtr++ = private_data.height >> 24; + *PesPtr++ = (private_data.width >> 0) & 0xff; + *PesPtr++ = (private_data.width >> 8) & 0xff; + *PesPtr++ = (private_data.width >> 16) & 0xff; + *PesPtr++ = private_data.width >> 24; PesPtr += 12; /* Skip flag word and Struct B first 8 bytes */ @@ -210,7 +206,7 @@ static int writeData(void* _call) if(call->len > 0 && call->data) { int Position = 0; unsigned char insertSampleHeader = 1; - while(1) { + while(Position < call->len) { int PacketLength = (call->len - Position) <= MAX_PES_PACKET_SIZE ? (call->len - Position) : MAX_PES_PACKET_SIZE; @@ -242,18 +238,21 @@ static int writeData(void* _call) insertSampleHeader = 0; } - PacketStart = malloc(call->len + HeaderLength); - memcpy (PacketStart, PesHeader, HeaderLength); - memcpy (PacketStart + HeaderLength, call->data + Position, PacketLength); + struct iovec iov[2]; + iov[0].iov_base = PesHeader; + iov[0].iov_len = HeaderLength; + iov[1].iov_base = call->data + Position; + iov[1].iov_len = PacketLength; - len = write(call->fd, PacketStart, PacketLength + HeaderLength); - free(PacketStart); + ssize_t l = writev(call->fd, iov, 2); + if (l < 0) { + len = l; + break; + } + len += l; Position += PacketLength; call->Pts = INVALID_PTS_VALUE; - - if (Position == call->len) - break; } }