diff --git a/libeplayer3/include/pes.h b/libeplayer3/include/pes.h index 8d6f947..6a8d342 100644 --- a/libeplayer3/include/pes.h +++ b/libeplayer3/include/pes.h @@ -9,7 +9,6 @@ #define PES_FLAGS_BYTE 7 #define PES_EXTENSION_DATA_PRESENT 0x01 #define PES_HEADER_DATA_LENGTH_BYTE 8 -#define PES_MIN_HEADER_SIZE 9 #define PES_START_CODE_RESERVED_4 0xfd #define PES_VERSION_FAKE_START_CODE 0x31 diff --git a/libeplayer3/output/writer/vc1.c b/libeplayer3/output/writer/vc1.c index 9145daf..7f13a40 100644 --- a/libeplayer3/output/writer/vc1.c +++ b/libeplayer3/output/writer/vc1.c @@ -20,7 +20,7 @@ */ /* ***************************** */ -/* Includes */ +/* Includes */ /* ***************************** */ #include @@ -48,19 +48,19 @@ #include "writer.h" /* ***************************** */ -/* Makros/Constants */ +/* Makros/Constants */ /* ***************************** */ -#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_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_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 #define VC1_DEBUG @@ -82,13 +82,13 @@ if (debug_level >= level) printf("[%s:%s] " fmt, __FILE__, __FUNCTION__, ## x); /* ***************************** */ -/* Types */ +/* Types */ /* ***************************** */ -static const unsigned char SequenceLayerStartCode[] = {0x00, 0x00, 0x01, VC1_SEQUENCE_LAYER_METADATA_START_CODE}; +static const unsigned char SequenceLayerStartCode[] = {0x00, 0x00, 0x01, VC1_SEQUENCE_LAYER_METADATA_START_CODE}; -static const unsigned char Metadata[] = +static const unsigned char Metadata[] = { 0x00, 0x00, 0x00, 0xc5, 0x04, 0x00, 0x00, 0x00, @@ -130,8 +130,18 @@ static int writeData(void* _call) vc1_printf(10, "\n"); if (call == NULL) { - vc1_err("call data is NULL...\n"); - return 0; + vc1_err("call data is NULL...\n"); + return 0; + } + + if ((call->data == NULL) || (call->len <= 0)) { + vc1_err("parsing NULL Data. ignoring...\n"); + return 0; + } + + if (call->fd < 0) { + vc1_err("file pointer < 0. ignoring ...\n"); + return 0; } vc1_printf(10, "VideoPts %lld\n", call->Pts); @@ -139,118 +149,100 @@ static int writeData(void* _call) vc1_printf(10, "Got Private Size %d\n", call->private_size); - if ((call->data == NULL) || (call->len <= 0)) { - vc1_err("parsing NULL Data. ignoring...\n"); - return 0; - } - - if (call->fd < 0) { - vc1_err("file pointer < 0. ignoring ...\n"); - return 0; - } - if (initialHeader) { - unsigned char PesPacket[PES_MIN_HEADER_SIZE+128]; - unsigned char* PesPtr; - unsigned int MetadataLength; - unsigned int crazyFramerate = 0; + unsigned char PesHeader[PES_MAX_HEADER_SIZE]; + unsigned char PesPayload[128]; + unsigned char* PesPtr; + unsigned int crazyFramerate = 0; + struct iovec iov[2]; - vc1_printf(10, "Framerate: %u\n", call->FrameRate); - vc1_printf(10, "biWidth: %d\n", call->Width); - vc1_printf(10, "biHeight: %d\n", call->Height); + vc1_printf(10, "Framerate: %u\n", call->FrameRate); + vc1_printf(10, "biWidth: %d\n", call->Width); + vc1_printf(10, "biHeight: %d\n", call->Height); - crazyFramerate = ((10000000.0 / call->FrameRate) * 1000.0); - vc1_printf(10, "crazyFramerate: %u\n", crazyFramerate); + crazyFramerate = ((10000000.0 / call->FrameRate) * 1000.0); + vc1_printf(10, "crazyFramerate: %u\n", crazyFramerate); - { - PesPtr = &PesPacket[PES_MIN_HEADER_SIZE]; + memset(PesPayload, 0, sizeof(PesPayload)); - memcpy (PesPtr, SequenceLayerStartCode, sizeof(SequenceLayerStartCode)); - PesPtr += sizeof(SequenceLayerStartCode); + PesPtr = PesPayload; - memcpy (PesPtr, Metadata, sizeof(Metadata)); - PesPtr += METADATA_STRUCT_C_START; + memcpy (PesPtr, SequenceLayerStartCode, sizeof(SequenceLayerStartCode)); + PesPtr += sizeof(SequenceLayerStartCode); - // - PesPtr += WMV3_PRIVATE_DATA_LENGTH; + memcpy (PesPtr, Metadata, sizeof(Metadata)); + PesPtr += METADATA_STRUCT_C_START; + PesPtr += WMV3_PRIVATE_DATA_LENGTH; - /* Metadata Header Struct A */ - *PesPtr++ = (call->Height >> 0) & 0xff; - *PesPtr++ = (call->Height >> 8) & 0xff; - *PesPtr++ = (call->Height >> 16) & 0xff; - *PesPtr++ = call->Height >> 24; - *PesPtr++ = (call->Width >> 0) & 0xff; - *PesPtr++ = (call->Width >> 8) & 0xff; - *PesPtr++ = (call->Width >> 16) & 0xff; - *PesPtr++ = call->Width >> 24; + /* Metadata Header Struct A */ + *PesPtr++ = (call->Height >> 0) & 0xff; + *PesPtr++ = (call->Height >> 8) & 0xff; + *PesPtr++ = (call->Height >> 16) & 0xff; + *PesPtr++ = call->Height >> 24; + *PesPtr++ = (call->Width >> 0) & 0xff; + *PesPtr++ = (call->Width >> 8) & 0xff; + *PesPtr++ = (call->Width >> 16) & 0xff; + *PesPtr++ = call->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++ = (crazyFramerate >> 0) & 0xff; - *PesPtr++ = (crazyFramerate >> 8) & 0xff; - *PesPtr++ = (crazyFramerate >> 16) & 0xff; - *PesPtr++ = crazyFramerate >> 24; + *PesPtr++ = (crazyFramerate >> 0) & 0xff; + *PesPtr++ = (crazyFramerate >> 8) & 0xff; + *PesPtr++ = (crazyFramerate >> 16) & 0xff; + *PesPtr++ = crazyFramerate >> 24; - MetadataLength = PesPtr - &PesPacket[PES_MIN_HEADER_SIZE]; + 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); + len = writev(call->fd, iov, 2); - int HeaderLength = InsertPesHeader (PesPacket, MetadataLength, VC1_VIDEO_PES_START_CODE, INVALID_PTS_VALUE, 0); + /* 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 = call->private_data; + iov[1].iov_len = call->private_size; + iov[0].iov_len = InsertPesHeader (PesHeader, iov[1].iov_len, VC1_VIDEO_PES_START_CODE, INVALID_PTS_VALUE, 0); + len = writev(call->fd, iov, 2); - len = write(call->fd, PesPacket, HeaderLength + MetadataLength); - } - - { - unsigned int i; - - /* For VC1 the codec private data is a standard vc1 sequence header so we just copy it to the output */ - memcpy (&PesPacket[PES_MIN_HEADER_SIZE], call->private_data, call->private_size); - - vc1_printf(10, "Private Data:\n"); - for (i = 0; i < call->private_size; i++) - vc1_printf(10, "%02x ", PesPacket[PES_MIN_HEADER_SIZE+i]); - vc1_printf(10, "\n"); - - int HeaderLength = InsertPesHeader (PesPacket, call->private_size, VC1_VIDEO_PES_START_CODE, INVALID_PTS_VALUE, 0); - len = write(call->fd, PesPacket, call->private_size + HeaderLength); - } - initialHeader = 0; + initialHeader = 0; } if(call->len > 0 && call->data) { - unsigned int Position = 0; - unsigned char insertSampleHeader = 1; + unsigned int Position = 0; + unsigned char insertSampleHeader = 1; - while(Position < call->len) { + while(Position < call->len) { - int PacketLength = (call->len - Position) <= MAX_PES_PACKET_SIZE ? - (call->len - Position) : MAX_PES_PACKET_SIZE; + int PacketLength = (call->len - Position) <= MAX_PES_PACKET_SIZE ? + (call->len - Position) : MAX_PES_PACKET_SIZE; - int Remaining = call->len - Position - PacketLength; + int Remaining = call->len - Position - PacketLength; - vc1_printf(20, "PacketLength=%d, Remaining=%d, Position=%d\n", PacketLength, Remaining, Position); + vc1_printf(20, "PacketLength=%d, Remaining=%d, Position=%d\n", PacketLength, Remaining, Position); - unsigned char PesHeader[PES_MAX_HEADER_SIZE]; - int HeaderLength = InsertPesHeader (PesHeader, PacketLength, VC1_VIDEO_PES_START_CODE, call->Pts, 0); + unsigned char PesHeader[PES_MAX_HEADER_SIZE]; + int HeaderLength = InsertPesHeader (PesHeader, PacketLength, VC1_VIDEO_PES_START_CODE, call->Pts, 0); - if(insertSampleHeader) { - const unsigned char Vc1FrameStartCode[] = {0, 0, 1, VC1_FRAME_START_CODE}; + if(insertSampleHeader) { + const unsigned char Vc1FrameStartCode[] = {0, 0, 1, VC1_FRAME_START_CODE}; /* - vc1_printf(10, "Data Start: {00 00 01 0d} - "); - int i; - for (i = 0; i < 4; i++) vc1_printf(10, "%02x ", call->data[i]); - vc1_printf(10, "\n"); + vc1_printf(10, "Data Start: {00 00 01 0d} - "); + int i; + for (i = 0; i < 4; i++) vc1_printf(10, "%02x ", call->data[i]); + vc1_printf(10, "\n"); */ - if (!FrameHeaderSeen && (call->len > 3) && (memcmp (call->data, Vc1FrameStartCode, 4) == 0)) - FrameHeaderSeen = 1; - if (!FrameHeaderSeen) - { - memcpy (&PesHeader[HeaderLength], Vc1FrameStartCode, sizeof(Vc1FrameStartCode)); - HeaderLength += sizeof(Vc1FrameStartCode); - } - insertSampleHeader = 0; - } + if (!FrameHeaderSeen && (call->len > 3) && (memcmp (call->data, Vc1FrameStartCode, 4) == 0)) + FrameHeaderSeen = 1; + if (!FrameHeaderSeen) + { + memcpy (&PesHeader[HeaderLength], Vc1FrameStartCode, sizeof(Vc1FrameStartCode)); + HeaderLength += sizeof(Vc1FrameStartCode); + } + insertSampleHeader = 0; + } struct iovec iov[2]; iov[0].iov_base = PesHeader; @@ -258,16 +250,16 @@ static int writeData(void* _call) iov[1].iov_base = call->data + Position; iov[1].iov_len = PacketLength; - ssize_t l = writev(call->fd, iov, 2); + ssize_t l = writev(call->fd, iov, 2); if (l < 0) { len = l; break; } len += l; - Position += PacketLength; - call->Pts = INVALID_PTS_VALUE; - } + Position += PacketLength; + call->Pts = INVALID_PTS_VALUE; + } } vc1_printf(10, "< %d\n", len); @@ -275,7 +267,7 @@ static int writeData(void* _call) } /* ***************************** */ -/* Writer Definition */ +/* Writer Definition */ /* ***************************** */ static WriterCaps_t caps = { diff --git a/libeplayer3/output/writer/wmv.c b/libeplayer3/output/writer/wmv.c index 8b1dd14..0a4b7a8 100644 --- a/libeplayer3/output/writer/wmv.c +++ b/libeplayer3/output/writer/wmv.c @@ -134,17 +134,6 @@ static int writeData(void* _call) return 0; } - wmv_printf(10, "VideoPts %lld\n", call->Pts); - - wmv_printf(10, "Got Private Size %d\n", call->private_size); - - 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; - if ((call->data == NULL) || (call->len <= 0)) { wmv_err("parsing NULL Data. ignoring...\n"); return 0; @@ -155,10 +144,20 @@ static int writeData(void* _call) return 0; } + wmv_printf(10, "VideoPts %lld\n", call->Pts); + wmv_printf(10, "Got Private Size %d\n", call->private_size); + + 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; + if (initialHeader) { - unsigned char PesPacket[PES_MIN_HEADER_SIZE+128]; - unsigned char* PesPtr; - unsigned int MetadataLength; + unsigned char PesHeader[PES_MAX_HEADER_SIZE]; + unsigned char PesPayload[128]; + unsigned char* PesPtr = PesPayload; unsigned int crazyFramerate = 0; wmv_printf(10, "Framerate: %u\n", private_data.framerate); @@ -168,7 +167,7 @@ static int writeData(void* _call) crazyFramerate = ((10000000.0 / private_data.framerate) * 1000.0); wmv_printf(10, "crazyFramerate: %u\n", crazyFramerate); - PesPtr = &PesPacket[PES_MIN_HEADER_SIZE]; + memset (PesPayload, 0, sizeof(PesPayload)); memcpy (PesPtr, Metadata, sizeof(Metadata)); PesPtr += METADATA_STRUCT_C_START; @@ -193,11 +192,13 @@ static int writeData(void* _call) *PesPtr++ = (crazyFramerate >> 16) & 0xff; *PesPtr++ = crazyFramerate >> 24; - MetadataLength = PesPtr - &PesPacket[PES_MIN_HEADER_SIZE]; + struct iovec iov[2]; + 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); - int HeaderLength = InsertPesHeader (PesPacket, MetadataLength, VC1_VIDEO_PES_START_CODE, INVALID_PTS_VALUE, 0); - - len = write(call->fd,PesPacket, HeaderLength + MetadataLength); + len = writev(call->fd, iov, 2); initialHeader = 0; }