[FFmpeg-devel] [PATCH 31/36] avcodec/mjpeg2jpeg_bsf: Remove intermediate packet
Andreas Rheinhardt
andreas.rheinhardt at gmail.com
Sat May 30 19:05:36 EEST 2020
This commit ends using separate packets for in- and output. Instead,
the input is read directly into the packet destined for output via
ff_bsf_get_packet_ref() and only the buffer-related fields are modified;
the others are not touched.
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt at gmail.com>
---
libavcodec/mjpeg2jpeg_bsf.c | 29 +++++++++++++----------------
1 file changed, 13 insertions(+), 16 deletions(-)
diff --git a/libavcodec/mjpeg2jpeg_bsf.c b/libavcodec/mjpeg2jpeg_bsf.c
index b30f391bf9..dc729cfd95 100644
--- a/libavcodec/mjpeg2jpeg_bsf.c
+++ b/libavcodec/mjpeg2jpeg_bsf.c
@@ -77,40 +77,40 @@ static uint8_t *append_dht_segment(uint8_t *buf)
return buf;
}
-static int mjpeg2jpeg_filter(AVBSFContext *ctx, AVPacket *out)
+static int mjpeg2jpeg_filter(AVBSFContext *ctx, AVPacket *pkt)
{
- AVPacket *in;
+ AVBufferRef *out = NULL;
int ret = 0;
int input_skip, output_size;
uint8_t *output;
- ret = ff_bsf_get_packet(ctx, &in);
+ ret = ff_bsf_get_packet_ref(ctx, pkt);
if (ret < 0)
return ret;
- if (in->size < 12) {
+ if (pkt->size < 12) {
av_log(ctx, AV_LOG_ERROR, "input is truncated\n");
ret = AVERROR_INVALIDDATA;
goto fail;
}
- if (AV_RB16(in->data) != 0xffd8) {
+ if (AV_RB16(pkt->data) != 0xffd8) {
av_log(ctx, AV_LOG_ERROR, "input is not MJPEG\n");
ret = AVERROR_INVALIDDATA;
goto fail;
}
- if (in->data[2] == 0xff && in->data[3] == APP0) {
- input_skip = (in->data[4] << 8) + in->data[5] + 4;
+ if (pkt->data[2] == 0xff && pkt->data[3] == APP0) {
+ input_skip = AV_RB16(pkt->data + 4) + 4;
} else {
input_skip = 2;
}
- if (in->size < input_skip) {
+ if (pkt->size < input_skip) {
av_log(ctx, AV_LOG_ERROR, "input is truncated\n");
ret = AVERROR_INVALIDDATA;
goto fail;
}
- output_size = in->size - input_skip +
+ output_size = pkt->size - input_skip +
sizeof(jpeg_header) + dht_segment_size;
- ret = av_new_packet(out, output_size);
+ ret = ff_buffer_padded_realloc(&out, output_size);
if (ret < 0)
goto fail;
@@ -118,16 +118,13 @@ static int mjpeg2jpeg_filter(AVBSFContext *ctx, AVPacket *out)
output = append(output, jpeg_header, sizeof(jpeg_header));
output = append_dht_segment(output);
- output = append(output, in->data + input_skip, in->size - input_skip);
+ output = append(output, pkt->data + input_skip, pkt->size - input_skip);
- ret = av_packet_copy_props(out, in);
- if (ret < 0)
- goto fail;
+ ff_packet_replace_buffer(pkt, out);
fail:
if (ret < 0)
- av_packet_unref(out);
- av_packet_free(&in);
+ av_packet_unref(pkt);
return ret;
}
--
2.20.1
More information about the ffmpeg-devel
mailing list