FFmpeg  4.4.8
flac_picture.c
Go to the documentation of this file.
1 /*
2  * Raw FLAC picture parser
3  * Copyright (c) 2001 Fabrice Bellard
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include "libavutil/intreadwrite.h"
23 #include "libavcodec/bytestream.h"
24 #include "libavcodec/png.h"
25 #include "avformat.h"
26 #include "avio_internal.h"
27 #include "flac_picture.h"
28 #include "id3v2.h"
29 #include "internal.h"
30 
31 #define MAX_TRUNC_PICTURE_SIZE (500 * 1024 * 1024)
32 
33 int ff_flac_parse_picture(AVFormatContext *s, uint8_t *buf, int buf_size, int truncate_workaround)
34 {
35  const CodecMime *mime = ff_id3v2_mime_tags;
36  enum AVCodecID id = AV_CODEC_ID_NONE;
38  uint8_t mimetype[64], *desc = NULL;
40  AVStream *st;
41  int width, height, ret = 0;
42  unsigned int type;
43  uint32_t len, left, trunclen = 0;
44 
45  if (buf_size < 34) {
46  av_log(s, AV_LOG_ERROR, "Attached picture metadata block too short\n");
47  if (s->error_recognition & AV_EF_EXPLODE)
48  return AVERROR_INVALIDDATA;
49  return 0;
50  }
51 
52  bytestream2_init(&g, buf, buf_size);
53 
54  /* read the picture type */
55  type = bytestream2_get_be32u(&g);
57  av_log(s, AV_LOG_ERROR, "Invalid picture type: %d.\n", type);
58  if (s->error_recognition & AV_EF_EXPLODE) {
59  return AVERROR_INVALIDDATA;
60  }
61  type = 0;
62  }
63 
64  /* picture mimetype */
65  len = bytestream2_get_be32u(&g);
66  if (len <= 0 || len >= sizeof(mimetype)) {
67  av_log(s, AV_LOG_ERROR, "Could not read mimetype from an attached "
68  "picture.\n");
69  if (s->error_recognition & AV_EF_EXPLODE)
70  return AVERROR_INVALIDDATA;
71  return 0;
72  }
73  if (len + 24 > bytestream2_get_bytes_left(&g)) {
74  av_log(s, AV_LOG_ERROR, "Attached picture metadata block too short\n");
75  if (s->error_recognition & AV_EF_EXPLODE)
76  return AVERROR_INVALIDDATA;
77  return 0;
78  }
79  bytestream2_get_bufferu(&g, mimetype, len);
80  mimetype[len] = 0;
81 
82  while (mime->id != AV_CODEC_ID_NONE) {
83  if (!strncmp(mime->str, mimetype, sizeof(mimetype))) {
84  id = mime->id;
85  break;
86  }
87  mime++;
88  }
89  if (id == AV_CODEC_ID_NONE) {
90  av_log(s, AV_LOG_ERROR, "Unknown attached picture mimetype: %s.\n",
91  mimetype);
92  if (s->error_recognition & AV_EF_EXPLODE)
93  return AVERROR_INVALIDDATA;
94  return 0;
95  }
96 
97  /* picture description */
98  len = bytestream2_get_be32u(&g);
99  if (len > bytestream2_get_bytes_left(&g) - 20) {
100  av_log(s, AV_LOG_ERROR, "Attached picture metadata block too short\n");
101  if (s->error_recognition & AV_EF_EXPLODE)
102  return AVERROR_INVALIDDATA;
103  return 0;
104  }
105  if (len > 0) {
106  if (!(desc = av_malloc(len + 1))) {
107  return AVERROR(ENOMEM);
108  }
109 
111  desc[len] = 0;
112  }
113 
114  /* picture metadata */
115  width = bytestream2_get_be32u(&g);
116  height = bytestream2_get_be32u(&g);
117  bytestream2_skipu(&g, 8);
118 
119  /* picture data */
120  len = bytestream2_get_be32u(&g);
121 
122  left = bytestream2_get_bytes_left(&g);
123  if (len <= 0 || len > left) {
125  av_log(s, AV_LOG_ERROR, "Attached picture metadata block too big %u\n", len);
126  if (s->error_recognition & AV_EF_EXPLODE)
127  ret = AVERROR_INVALIDDATA;
128  goto fail;
129  }
130 
131  // Workaround bug for flac muxers that writs truncated metadata picture block size if
132  // the picture size do not fit in 24 bits. lavf flacenc used to have the issue and based
133  // on existing broken files other unknown flac muxers seems to truncate also.
134  if (truncate_workaround &&
135  s->strict_std_compliance <= FF_COMPLIANCE_NORMAL &&
136  len > left && (len & 0xffffff) == left) {
137  av_log(s, AV_LOG_INFO, "Correcting truncated metadata picture size from %u to %u\n", left, len);
138  trunclen = len - left;
139  } else {
140  av_log(s, AV_LOG_ERROR, "Attached picture metadata block too short\n");
141  if (s->error_recognition & AV_EF_EXPLODE)
142  ret = AVERROR_INVALIDDATA;
143  goto fail;
144  }
145  }
147  RETURN_ERROR(AVERROR(ENOMEM));
148  }
149 
150  if (trunclen == 0) {
151  bytestream2_get_bufferu(&g, data->data, len);
152  } else {
153  // If truncation was detected copy all data from block and read missing bytes
154  // not included in the block size
155  bytestream2_get_bufferu(&g, data->data, left);
156  ret = ffio_read_size(s->pb, data->data + len - trunclen, trunclen);
157  if (ret < 0)
158  goto fail;
159  }
160  memset(data->data + len, 0, AV_INPUT_BUFFER_PADDING_SIZE);
161 
162  if (AV_RB64(data->data) == PNGSIG)
163  id = AV_CODEC_ID_PNG;
164 
165  st = avformat_new_stream(s, NULL);
166  if (!st) {
167  RETURN_ERROR(AVERROR(ENOMEM));
168  }
169 
171  st->attached_pic.buf = data;
172  st->attached_pic.data = data->data;
173  st->attached_pic.size = len;
174  st->attached_pic.stream_index = st->index;
176 
179  st->codecpar->codec_id = id;
180  st->codecpar->width = width;
181  st->codecpar->height = height;
182  av_dict_set(&st->metadata, "comment", ff_id3v2_picture_types[type], 0);
183  if (desc)
185 
186  return 0;
187 
188 fail:
190  av_freep(&desc);
191 
192  return ret;
193 }
uint8_t
#define AV_EF_EXPLODE
abort decoding on minor error detection
Definition: avcodec.h:1660
#define FF_COMPLIANCE_NORMAL
Definition: avcodec.h:1608
Main libavformat public API header.
#define AV_DISPOSITION_ATTACHED_PIC
The stream is stored in the file as an attached picture/"cover art" (e.g.
Definition: avformat.h:841
#define RETURN_ERROR(code)
Definition: avidec.c:490
int ffio_read_size(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:682
#define AV_RB64
Definition: intreadwrite.h:164
static av_always_inline void bytestream2_skipu(GetByteContext *g, unsigned int size)
Definition: bytestream.h:174
static av_always_inline unsigned int bytestream2_get_bufferu(GetByteContext *g, uint8_t *dst, unsigned int size)
Definition: bytestream.h:277
static av_always_inline int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:158
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:137
#define s(width, name)
Definition: cbs_vp9.c:257
#define fail()
Definition: checkasm.h:133
#define NULL
Definition: coverity.c:32
enum AVCodecID id
#define MAX_TRUNC_PICTURE_SIZE
Definition: flac_picture.c:31
int ff_flac_parse_picture(AVFormatContext *s, uint8_t *buf, int buf_size, int truncate_workaround)
Definition: flac_picture.c:33
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: codec_id.h:46
@ AV_CODEC_ID_PNG
Definition: codec_id.h:110
@ AV_CODEC_ID_NONE
Definition: codec_id.h:47
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding.
Definition: avcodec.h:215
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:634
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: packet.h:410
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4509
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it.
Definition: buffer.c:125
AVBufferRef * av_buffer_alloc(buffer_size_t size)
Allocate an AVBuffer of the given size using av_malloc().
Definition: buffer.c:67
#define AV_DICT_DONT_STRDUP_VAL
Take ownership of a value that's been allocated with av_malloc() or another memory allocation functio...
Definition: dict.h:74
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition: dict.c:70
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
#define AVERROR(e)
Definition: error.h:43
#define AV_LOG_INFO
Standard information.
Definition: log.h:205
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
cl_device_type type
const char *const ff_id3v2_picture_types[21]
Definition: id3v2.c:107
const CodecMime ff_id3v2_mime_tags[]
Definition: id3v2.c:131
common internal API header
const char * desc
Definition: libsvtav1.c:79
const char data[16]
Definition: mxf.c:142
#define PNGSIG
Definition: png.h:49
#define FF_ARRAY_ELEMS(a)
A reference to a data buffer.
Definition: buffer.h:84
int width
Video only.
Definition: codec_par.h:126
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:56
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:60
Format I/O context.
Definition: avformat.h:1232
int stream_index
Definition: packet.h:371
AVBufferRef * buf
A reference to the reference-counted buffer where the packet data is stored.
Definition: packet.h:352
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:375
int size
Definition: packet.h:370
uint8_t * data
Definition: packet.h:369
Stream structure.
Definition: avformat.h:873
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1038
AVDictionary * metadata
Definition: avformat.h:937
int index
stream index in AVFormatContext
Definition: avformat.h:874
AVPacket attached_pic
For streams with AV_DISPOSITION_ATTACHED_PIC disposition, this packet will contain the attached pictu...
Definition: avformat.h:955
int disposition
AV_DISPOSITION_* bit field.
Definition: avformat.h:926
char str[32]
Definition: internal.h:48
enum AVCodecID id
Definition: internal.h:49
#define av_freep(p)
#define av_malloc(s)
#define av_log(a,...)
#define height
#define width
const char * g
Definition: vf_curves.c:118
int len