Avoid unnecessary copy on config packets demuxing

Use av_packet_ref() to reference the packet without copy.

This also simplifies the logic, by making the "offset" variable and the
memcpy() call local to the if-block.
This commit is contained in:
Romain Vimont 2023-01-02 15:55:46 +01:00
parent d8c2fe6ef2
commit bf8696d02e

View file

@ -95,29 +95,27 @@ sc_demuxer_push_packet(struct sc_demuxer *demuxer, AVPacket *packet) {
// A config packet must not be decoded immediately (it contains no // A config packet must not be decoded immediately (it contains no
// frame); instead, it must be concatenated with the future data packet. // frame); instead, it must be concatenated with the future data packet.
if (demuxer->pending || is_config) { if (demuxer->pending || is_config) {
size_t offset;
if (demuxer->pending) { if (demuxer->pending) {
offset = demuxer->pending->size; size_t offset = demuxer->pending->size;
if (av_grow_packet(demuxer->pending, packet->size)) { if (av_grow_packet(demuxer->pending, packet->size)) {
LOG_OOM(); LOG_OOM();
return false; return false;
} }
memcpy(demuxer->pending->data + offset, packet->data, packet->size);
} else { } else {
offset = 0;
demuxer->pending = av_packet_alloc(); demuxer->pending = av_packet_alloc();
if (!demuxer->pending) { if (!demuxer->pending) {
LOG_OOM(); LOG_OOM();
return false; return false;
} }
if (av_new_packet(demuxer->pending, packet->size)) { if (av_packet_ref(demuxer->pending, packet)) {
LOG_OOM(); LOG_OOM();
av_packet_free(&demuxer->pending); av_packet_free(&demuxer->pending);
return false; return false;
} }
} }
memcpy(demuxer->pending->data + offset, packet->data, packet->size);
if (!is_config) { if (!is_config) {
// prepare the concat packet to send to the decoder // prepare the concat packet to send to the decoder
demuxer->pending->pts = packet->pts; demuxer->pending->pts = packet->pts;