avformat/flac_picture: Try to reuse buffer for attached picture

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
This commit is contained in:
Andreas Rheinhardt 2021-08-23 13:37:04 +02:00
parent b10a8a30db
commit c1f4858242
4 changed files with 29 additions and 6 deletions

View File

@ -29,12 +29,13 @@
#define MAX_TRUNC_PICTURE_SIZE (500 * 1024 * 1024) #define MAX_TRUNC_PICTURE_SIZE (500 * 1024 * 1024)
int ff_flac_parse_picture(AVFormatContext *s, uint8_t *buf, int buf_size, int truncate_workaround) int ff_flac_parse_picture(AVFormatContext *s, uint8_t **bufp, int buf_size,
int truncate_workaround)
{ {
const CodecMime *mime = ff_id3v2_mime_tags; const CodecMime *mime = ff_id3v2_mime_tags;
enum AVCodecID id = AV_CODEC_ID_NONE; enum AVCodecID id = AV_CODEC_ID_NONE;
AVBufferRef *data = NULL; AVBufferRef *data = NULL;
uint8_t mimetype[64], *desc = NULL; uint8_t mimetype[64], *desc = NULL, *buf = *bufp;
GetByteContext g; GetByteContext g;
AVStream *st; AVStream *st;
int width, height, ret = 0; int width, height, ret = 0;
@ -142,6 +143,15 @@ int ff_flac_parse_picture(AVFormatContext *s, uint8_t *buf, int buf_size, int tr
goto fail; goto fail;
} }
} }
if (trunclen == 0 && len >= buf_size - (buf_size >> 4)) {
data = av_buffer_create(buf, buf_size + AV_INPUT_BUFFER_PADDING_SIZE,
av_buffer_default_free, NULL, 0);
if (!data)
RETURN_ERROR(AVERROR(ENOMEM));
*bufp = NULL;
data->data += bytestream2_tell(&g);
data->size = len + AV_INPUT_BUFFER_PADDING_SIZE;
} else {
if (!(data = av_buffer_alloc(len + AV_INPUT_BUFFER_PADDING_SIZE))) { if (!(data = av_buffer_alloc(len + AV_INPUT_BUFFER_PADDING_SIZE))) {
RETURN_ERROR(AVERROR(ENOMEM)); RETURN_ERROR(AVERROR(ENOMEM));
} }
@ -155,6 +165,7 @@ int ff_flac_parse_picture(AVFormatContext *s, uint8_t *buf, int buf_size, int tr
if (avio_read(s->pb, data->data + len - trunclen, trunclen) < trunclen) if (avio_read(s->pb, data->data + len - trunclen, trunclen) < trunclen)
RETURN_ERROR(AVERROR_INVALIDDATA); RETURN_ERROR(AVERROR_INVALIDDATA);
} }
}
memset(data->data + len, 0, AV_INPUT_BUFFER_PADDING_SIZE); memset(data->data + len, 0, AV_INPUT_BUFFER_PADDING_SIZE);
if (AV_RB64(data->data) == PNGSIG) if (AV_RB64(data->data) == PNGSIG)

View File

@ -26,6 +26,18 @@
#define RETURN_ERROR(code) do { ret = (code); goto fail; } while (0) #define RETURN_ERROR(code) do { ret = (code); goto fail; } while (0)
int ff_flac_parse_picture(AVFormatContext *s, uint8_t *buf, int buf_size, int truncate_workaround); /**
* Parse a FLAC METADATA_BLOCK_PICTURE
*
* @param s AVFormatContext for logging and the attached picture stream.
* @param buf `*buf` points to the actual data which must be padded by
* AV_INPUT_BUFFER_PADDING_SIZE bytes not counted in buf_size.
* This function may take ownership of `*buf` and reset it.
* @param buf_size size of `*buf` (excluding padding)
* @param truncate_workaround If set, additional data may be read from s->pb if
* truncation has been detected.
*/
int ff_flac_parse_picture(AVFormatContext *s, uint8_t **buf, int buf_size,
int truncate_workaround);
#endif /* AVFORMAT_FLAC_PICTURE_H */ #endif /* AVFORMAT_FLAC_PICTURE_H */

View File

@ -145,7 +145,7 @@ static int flac_read_header(AVFormatContext *s)
} }
av_freep(&buffer); av_freep(&buffer);
} else if (metadata_type == FLAC_METADATA_TYPE_PICTURE) { } else if (metadata_type == FLAC_METADATA_TYPE_PICTURE) {
ret = ff_flac_parse_picture(s, buffer, metadata_size, 1); ret = ff_flac_parse_picture(s, &buffer, metadata_size, 1);
av_freep(&buffer); av_freep(&buffer);
if (ret < 0) { if (ret < 0) {
av_log(s, AV_LOG_ERROR, "Error parsing attached picture.\n"); av_log(s, AV_LOG_ERROR, "Error parsing attached picture.\n");

View File

@ -118,7 +118,7 @@ static int vorbis_parse_single_comment(AVFormatContext *as, AVDictionary **m,
*/ */
if (!av_strcasecmp(t, "METADATA_BLOCK_PICTURE") && parse_picture) { if (!av_strcasecmp(t, "METADATA_BLOCK_PICTURE") && parse_picture) {
int ret, len = AV_BASE64_DECODE_SIZE(vl); int ret, len = AV_BASE64_DECODE_SIZE(vl);
uint8_t *pict = av_malloc(len); uint8_t *pict = av_malloc(len + AV_INPUT_BUFFER_PADDING_SIZE);
if (!pict) { if (!pict) {
av_log(as, AV_LOG_WARNING, "out-of-memory error. Skipping cover art block.\n"); av_log(as, AV_LOG_WARNING, "out-of-memory error. Skipping cover art block.\n");
@ -126,7 +126,7 @@ static int vorbis_parse_single_comment(AVFormatContext *as, AVDictionary **m,
} }
ret = av_base64_decode(pict, v, len); ret = av_base64_decode(pict, v, len);
if (ret > 0) if (ret > 0)
ret = ff_flac_parse_picture(as, pict, ret, 0); ret = ff_flac_parse_picture(as, &pict, ret, 0);
av_freep(&pict); av_freep(&pict);
if (ret < 0) { if (ret < 0) {
av_log(as, AV_LOG_WARNING, "Failed to parse cover art block.\n"); av_log(as, AV_LOG_WARNING, "Failed to parse cover art block.\n");