avformat/matroskaenc: Speed up reformatting WavPack

WavPack's blocks use a length field, so that parsing them is fast.
Therefore it makes sense to parse the block twice, once to get
the length of the output packet and once to write the actual data
instead of writing the data into a temporary buffer in a single pass.

This speeds up muxing from 1597092 to 761850 Decicycles per
write_packet call for a 2000kb/s stereo WavPack file muxed to /dev/null
with writing CRC-32 disabled.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
This commit is contained in:
Andreas Rheinhardt 2022-01-15 23:52:13 +01:00
parent c1b6acde36
commit 396efc73e3

View File

@ -2347,57 +2347,45 @@ static int mkv_blockgroup_size(int pkt_size, int track_num_size)
} }
#if CONFIG_MATROSKA_MUXER #if CONFIG_MATROSKA_MUXER
static int mkv_strip_wavpack(const uint8_t *src, uint8_t **pdst, int *size) static int mkv_reformat_wavpack(MatroskaMuxContext *mkv, AVIOContext *pb,
const AVPacket *pkt, int *size)
{ {
uint8_t *dst; const uint8_t *src = pkt->data;
int srclen = *size; int srclen = pkt->size;
int offset = 0; int offset = 0;
int ret; int ret;
dst = av_malloc(srclen);
if (!dst)
return AVERROR(ENOMEM);
while (srclen >= WV_HEADER_SIZE) { while (srclen >= WV_HEADER_SIZE) {
WvHeader header; WvHeader header;
ret = ff_wv_parse_header(&header, src); ret = ff_wv_parse_header(&header, src);
if (ret < 0) if (ret < 0)
goto fail; return ret;
src += WV_HEADER_SIZE; src += WV_HEADER_SIZE;
srclen -= WV_HEADER_SIZE; srclen -= WV_HEADER_SIZE;
if (srclen < header.blocksize) { if (srclen < header.blocksize)
ret = AVERROR_INVALIDDATA; return AVERROR_INVALIDDATA;
goto fail;
}
if (header.initial) { offset += 4 * !!header.initial + 8 + 4 * !(header.initial && header.final);
AV_WL32(dst + offset, header.samples); if (pb) {
offset += 4; if (header.initial)
} avio_wl32(pb, header.samples);
AV_WL32(dst + offset, header.flags); avio_wl32(pb, header.flags);
AV_WL32(dst + offset + 4, header.crc); avio_wl32(pb, header.crc);
offset += 8;
if (!(header.initial && header.final)) { if (!(header.initial && header.final))
AV_WL32(dst + offset, header.blocksize); avio_wl32(pb, header.blocksize);
offset += 4;
}
memcpy(dst + offset, src, header.blocksize); avio_write(pb, src, header.blocksize);
}
src += header.blocksize; src += header.blocksize;
srclen -= header.blocksize; srclen -= header.blocksize;
offset += header.blocksize; offset += header.blocksize;
} }
*pdst = dst;
*size = offset; *size = offset;
return 0; return 0;
fail:
av_freep(&dst);
return ret;
} }
#endif #endif
@ -2436,8 +2424,6 @@ static int mkv_write_block(AVFormatContext *s, AVIOContext *pb,
(AV_RB24(par->extradata) == 1 || AV_RB32(par->extradata) == 1)) { (AV_RB24(par->extradata) == 1 || AV_RB32(par->extradata) == 1)) {
/* extradata is Annex B, assume the bitstream is too and convert it */ /* extradata is Annex B, assume the bitstream is too and convert it */
err = ff_hevc_annexb2mp4_buf(pkt->data, &data, &size, 0, NULL); err = ff_hevc_annexb2mp4_buf(pkt->data, &data, &size, 0, NULL);
} else if (par->codec_id == AV_CODEC_ID_WAVPACK) {
err = mkv_strip_wavpack(pkt->data, &data, &size);
} else } else
#endif #endif
if (par->codec_id == AV_CODEC_ID_AV1) { if (par->codec_id == AV_CODEC_ID_AV1) {
@ -3111,8 +3097,17 @@ static int mkv_init(struct AVFormatContext *s)
for (i = 0; i < s->nb_streams; i++) { for (i = 0; i < s->nb_streams; i++) {
AVStream *st = s->streams[i]; AVStream *st = s->streams[i];
const AVCodecParameters *const par = st->codecpar;
mkv_track *track = &mkv->tracks[i]; mkv_track *track = &mkv->tracks[i];
switch (par->codec_id) {
#if CONFIG_MATROSKA_MUXER
case AV_CODEC_ID_WAVPACK:
track->reformat = mkv_reformat_wavpack;
break;
#endif
}
if (s->flags & AVFMT_FLAG_BITEXACT) { if (s->flags & AVFMT_FLAG_BITEXACT) {
track->uid = i + 1; track->uid = i + 1;
} else { } else {