优化代码

This commit is contained in:
xiongziliang 2022-12-17 15:12:30 +08:00
parent d7356137f4
commit 8005c2b5ba
5 changed files with 89 additions and 78 deletions

View File

@ -39,7 +39,7 @@ typedef enum {
XX(CodecVP8, TrackVideo, 7, "VP8", PSI_STREAM_VP8) \
XX(CodecVP9, TrackVideo, 8, "VP9", PSI_STREAM_VP9) \
XX(CodecAV1, TrackVideo, 9, "AV1", PSI_STREAM_AV1) \
XX(CodecJPEG, TrackVideo, 10, "JPEG", PSI_STREAM_RESERVED)
XX(CodecJPEG, TrackVideo, 10, "JPEG", PSI_STREAM_JPEG_2000)
typedef enum {
CodecInvalid = -1,

View File

@ -1,4 +1,5 @@
#pagma once
#ifndef ZLMEDIAKIT_JPEG_H
#define ZLMEDIAKIT_JPEG_H
#include "Frame.h"
#include "Track.h"
@ -7,25 +8,47 @@ namespace mediakit {
class JPEGTrack : public VideoTrack {
public:
using Ptr = std::shared_ptr<H264Track>;
using VideoTrack::VideoTrack;
CodecId getCodecId() const override { return CodecJPEG; }
int getVideoHeight() const override { return _height; }
int getVideoWidth() const override { return _width; }
float getVideoFps() const override { return _fps; }
bool ready() override { return true; }
bool inputFrame(const Frame::Ptr &frame) override { return VideoTrack::inputFrame(frame); }
using Ptr = std::shared_ptr<JPEGTrack>;
CodecId getCodecId() const override { return CodecJPEG; }
int getVideoHeight() const override { return _height; }
int getVideoWidth() const override { return _width; }
float getVideoFps() const override { return _fps; }
bool ready() override { return true; }
bool inputFrame(const Frame::Ptr &frame) override { return VideoTrack::inputFrame(frame); }
private:
Sdp::Ptr getSdp() override { return std::make_shared<MJPEGSdp>(getBitRate() / 1024); };
Track::Ptr clone() override {
return std::make_shared<std::remove_reference<decltype(*this)>::type>(*this);
}
Sdp::Ptr getSdp() override { return nullptr;/*std::make_shared<MJPEGSdp>(getBitRate() / 1024);*/ };
Track::Ptr clone() override { return std::make_shared<std::remove_reference<decltype(*this)>::type>(*this); }
private:
int _width = 0;
int _height = 0;
float _fps = 0;
int _width = 0;
int _height = 0;
float _fps = 0;
};
class FrameJPEG : public Frame {
public:
FrameJPEG(toolkit::Buffer::Ptr buffer, uint64_t dts) {
_buffer = std::move(buffer);
_dts = dts;
}
~FrameJPEG() override = default;
uint64_t dts() const override { return _dts; }
size_t prefixSize() const override { return 0; }
bool keyFrame() const override { return true; }
bool configFrame() const override { return false; }
CodecId getCodecId() const override { return CodecJPEG; }
char *data() const override { return _buffer->data(); }
size_t size() const override { return _buffer->size(); }
private:
uint64_t _dts;
toolkit::Buffer::Ptr _buffer;
};
}//namespace mediakit
#endif //ZLMEDIAKIT_JPEG_H

View File

@ -1,13 +1,9 @@
#include "JPEGRtp.h"
#include "JPEG.h"
using namespace std;
using namespace mediakit;
extern "C" {
#include <libavformat/avformat.h>
#include <libavutil/intreadwrite.h>
}
/* JPEG marker codes */
enum JpegMarker {
/* start of frame */
@ -139,7 +135,7 @@ static unsigned int bytestream2_put_buffer(PutByteContext *p, const uint8_t *src
return 0;
}
size2 = MIN(p->buffer_end - p->buffer, size);
if (size2 != size) {
if (size2 != (int)size) {
p->eof = 1;
}
memcpy(p->buffer, src, size2);
@ -360,11 +356,11 @@ static int jpeg_create_header(uint8_t *buf, int size, uint32_t type, uint32_t w,
return bytestream2_tell_p(&pbc);
}
/*static inline int av_clip(int a, int amin, int amax) {
static inline int av_clip(int a, int amin, int amax) {
if (a < amin) { return amin; }
else if (a > amax) { return amax; }
else { return a; }
}*/
}
static void create_default_qtables(uint8_t *qtables, uint8_t q) {
int factor = q;
@ -564,21 +560,32 @@ static int jpeg_parse_packet(void *ctx, PayloadContext *jpeg, uint32_t *timestam
}
//----------------------------------------------------------------------------------
#define DEF(type, name, bytes, write) \
static av_always_inline void bytestream_put_ ## name(uint8_t **b, \
const type value) \
{ \
write(*b, value); \
(*b) += bytes; \
}
#define DEF(type, name, bytes, write) \
static inline void bytestream_put_##name(uint8_t **b, const type value) { \
write(*b, value); \
(*b) += bytes; \
}
#define AV_WB24(p, d) \
do { \
((uint8_t *)(p))[2] = (d); \
((uint8_t *)(p))[1] = (d) >> 8; \
((uint8_t *)(p))[0] = (d) >> 16; \
} while (0)
#define AV_WB16(p, d) \
do { \
((uint8_t *)(p))[1] = (d); \
((uint8_t *)(p))[0] = (d) >> 8; \
} while (0)
#define AV_WB8(p, d) do { ((uint8_t*)(p))[0] = (d); } while(0)
DEF(unsigned int, be24, 3, AV_WB24)
DEF(unsigned int, be16, 2, AV_WB16)
DEF(unsigned int, byte, 1, AV_WB8)
static av_always_inline void bytestream_put_buffer(uint8_t **b,
const uint8_t *src,
unsigned int size)
{
static inline void bytestream_put_buffer(uint8_t **b, const uint8_t *src, unsigned int size) {
memcpy(*b, src, size);
(*b) += size;
}
@ -612,11 +619,12 @@ void JPEGRtpEncoder::rtp_send_jpeg(JPEGRtpContext *s, const uint8_t *buf, int si
s->timestamp = s->cur_timestamp;
int w1, h1;
if(JPEGRtpDecoder::getVideoResolution(buf, size, w1, h1)) {
/* convert video pixel dimensions from pixels to blocks */
w = w1;
h = h1;
} else w = h = 0;
if (JPEGRtpDecoder::getVideoResolution(buf, size, w1, h1)) {
/* convert video pixel dimensions from pixels to blocks */
w = w1;
h = h1;
} else
w = h = 0;
/* get the pixel format type or fail */
/*if (s1->streams[0]->codecpar->format == AV_PIX_FMT_YUVJ422P ||
@ -767,7 +775,7 @@ void JPEGRtpEncoder::rtp_send_jpeg(JPEGRtpContext *s, const uint8_t *buf, int si
hdr_size += 4 + 64 * nb_qtables;
/* payload max in one packet */
len = FFMIN(size, s->max_payload_size - hdr_size);
len = MIN(size, s->max_payload_size - hdr_size);
/* set main header */
bytestream_put_byte(&p, 0);
@ -811,20 +819,6 @@ CodecId JPEGRtpDecoder::getCodecId() const {
return CodecJPEG;
}
class FrameJPEG : public FrameImp {
public:
FrameJPEG() = default;
~FrameJPEG() override = default;
CodecId getCodecId() const override {
return CodecJPEG;
}
bool keyFrame() const override {
return true;
}
};
bool JPEGRtpDecoder::inputRtp(const RtpPacket::Ptr &rtp, bool) {
auto payload = rtp->getPayload();
auto size = rtp->getPayloadSize();
@ -837,9 +831,8 @@ bool JPEGRtpDecoder::inputRtp(const RtpPacket::Ptr &rtp, bool) {
}
if (0 == jpeg_parse_packet(nullptr, &_ctx, &stamp, payload, size, seq, marker ? RTP_FLAG_MARKER : 0)) {
auto frame = FrameImp::create<FrameJPEG>();
frame->_dts = frame->_dts = stamp / 90;
frame->_buffer = std::move(_ctx.frame);
auto buffer = std::make_shared<toolkit::BufferString>(std::move(_ctx.frame));
auto frame = std::make_shared<FrameJPEG>(std::move(buffer), stamp / 90);
_ctx.frame.clear();
RtpCodec::inputFrame(std::move(frame));
}
@ -861,22 +854,16 @@ bool JPEGRtpDecoder::getVideoResolution(const uint8_t *buf, int len, int &width,
////////////////////////////////////////////////////////////////////////
JPEGRtpEncoder::JPEGRtpEncoder(uint32_t ui32Ssrc,
uint32_t ui32MtuSize,
uint32_t ui32SampleRate,
uint8_t ui8PayloadType,
uint8_t ui8Interleaved) :
RtpInfo(ui32Ssrc,
ui32MtuSize,
ui32SampleRate,
ui8PayloadType,
ui8Interleaved) {
}
JPEGRtpEncoder::JPEGRtpEncoder(
uint32_t ssrc, uint32_t mtu, uint32_t sample_rate, uint8_t payload_type, uint8_t interleaved)
: RtpInfo(ssrc, mtu, sample_rate, payload_type, interleaved) {}
bool JPEGRtpEncoder::inputFrame(const Frame::Ptr &frame) {
auto ptr = (uint8_t *) frame->data() + frame->prefixSize();
auto ptr = (uint8_t *)frame->data() + frame->prefixSize();
auto len = frame->size() - frame->prefixSize();
auto pts = frame->pts();
JPEGRtpContext _ctx;
_ctx.buf = ptr;
_ctx.pts = pts;

View File

@ -48,15 +48,16 @@ private:
};
struct JPEGRtpContext;
class JPEGRtpEncoder : public JPEGRtpDecoder , public RtpInfo {
public:
typedef std::shared_ptr<JPEGRtpEncoder> Ptr;
JPEGRtpEncoder(
uint32_t ui32Ssrc, uint32_t ui32MtuSize = 1400, uint32_t ui32SampleRate = 90000, uint8_t ui8PayloadType = 96, uint8_t ui8Interleaved = TrackVideo * 2);
~JPEGRtpEncoder() {}
class JPEGRtpEncoder : public JPEGRtpDecoder, public RtpInfo {
public:
using Ptr = std::shared_ptr<JPEGRtpEncoder>;
JPEGRtpEncoder(uint32_t ssrc, uint32_t mtu = 1400, uint32_t sample_rate = 90000, uint8_t payload_type = 96, uint8_t interleaved = TrackVideo * 2);
~JPEGRtpEncoder() = default;
bool inputFrame(const Frame::Ptr &frame) override;
private:
void rtp_send_jpeg(JPEGRtpContext *ctx, const uint8_t *buf, int size);
};

View File

@ -48,7 +48,7 @@ typedef enum {
XX(DVI4_22050, TrackAudio, 17, 22050, 1, CodecInvalid) \
XX(G729, TrackAudio, 18, 8000, 1, CodecInvalid) \
XX(CelB, TrackVideo, 25, 90000, 1, CodecInvalid) \
XX(JPEG, TrackVideo, 26, 90000, 1, CodecInvalid) \
XX(JPEG, TrackVideo, 26, 90000, 1, CodecJPEG) \
XX(nv, TrackVideo, 28, 90000, 1, CodecInvalid) \
XX(H261, TrackVideo, 31, 90000, 1, CodecInvalid) \
XX(MPV, TrackVideo, 32, 90000, 1, CodecInvalid) \