FFmpeg
Loading...
Searching...
No Matches
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
24#include "libavcodec/png.h"
25#include "avformat.h"
26#include "avio_internal.h"
27#include "demux.h"
28#include "flac_picture.h"
29#include "id3v2.h"
30#include "internal.h"
31
32#define MAX_TRUNC_PICTURE_SIZE (500 * 1024 * 1024)
33
34int ff_flac_parse_picture(AVFormatContext *s, uint8_t **bufp, int buf_size,
35 int truncate_workaround)
36{
37 const CodecMime *mime = ff_id3v2_mime_tags;
40 uint8_t mimetype[64], *buf = *bufp;
41 const uint8_t *desc = NULL;
43 AVStream *st;
44 int width, height, ret = 0;
45 unsigned int type;
46 uint32_t len, left, trunclen = 0;
47
48 if (buf_size < 34) {
49 av_log(s, AV_LOG_ERROR, "Attached picture metadata block too short\n");
50 if (s->error_recognition & AV_EF_EXPLODE)
52 return 0;
53 }
54
55 bytestream2_init(&g, buf, buf_size);
56
57 /* read the picture type */
58 type = bytestream2_get_be32u(&g);
60 av_log(s, AV_LOG_ERROR, "Invalid picture type: %d.\n", type);
61 if (s->error_recognition & AV_EF_EXPLODE) {
63 }
64 type = 0;
65 }
66
67 /* picture mimetype */
68 len = bytestream2_get_be32u(&g);
69 if (len <= 0 || len >= sizeof(mimetype)) {
70 av_log(s, AV_LOG_ERROR, "Could not read mimetype from an attached "
71 "picture.\n");
72 if (s->error_recognition & AV_EF_EXPLODE)
74 return 0;
75 }
76 if (len + 24 > bytestream2_get_bytes_left(&g)) {
77 av_log(s, AV_LOG_ERROR, "Attached picture metadata block too short\n");
78 if (s->error_recognition & AV_EF_EXPLODE)
80 return 0;
81 }
82 bytestream2_get_bufferu(&g, mimetype, len);
83 mimetype[len] = 0;
84
85 while (mime->id != AV_CODEC_ID_NONE) {
86 if (!strncmp(mime->str, mimetype, sizeof(mimetype))) {
87 id = mime->id;
88 break;
89 }
90 mime++;
91 }
92 if (id == AV_CODEC_ID_NONE) {
93 av_log(s, AV_LOG_WARNING, "Unknown attached picture mimetype: %s.\n",
94 mimetype);
95 return 0;
96 }
97
98 /* picture description */
99 len = bytestream2_get_be32u(&g);
100 if (len > bytestream2_get_bytes_left(&g) - 20) {
101 av_log(s, AV_LOG_ERROR, "Attached picture metadata block too short\n");
102 if (s->error_recognition & AV_EF_EXPLODE)
103 return AVERROR_INVALIDDATA;
104 return 0;
105 }
106 if (len > 0) {
107 desc = g.buffer;
109 }
110
111 /* picture metadata */
112 width = bytestream2_get_be32u(&g);
113 ((uint8_t*)g.buffer)[-4] = '\0'; // NUL-terminate desc.
114 height = bytestream2_get_be32u(&g);
115 bytestream2_skipu(&g, 8);
116
117 /* picture data */
118 len = bytestream2_get_be32u(&g);
119
123 av_log(s, AV_LOG_ERROR, "Attached picture metadata block too big %"PRIu32"\n", len);
124 if (s->error_recognition & AV_EF_EXPLODE)
125 return AVERROR_INVALIDDATA;
126 return 0;
127 }
128
129 // Workaround bug for flac muxers that writs truncated metadata picture block size if
130 // the picture size do not fit in 24 bits. lavf flacenc used to have the issue and based
131 // on existing broken files other unknown flac muxers seems to truncate also.
132 if (truncate_workaround &&
133 s->strict_std_compliance <= FF_COMPLIANCE_NORMAL &&
134 len > left && (len & 0xffffff) == left) {
135 av_log(s, AV_LOG_INFO, "Correcting truncated metadata picture size from %"PRIu32" to %"PRIu32"\n", left, len);
136 trunclen = len - left;
137 } else {
138 av_log(s, AV_LOG_ERROR, "Attached picture metadata block too short\n");
139 if (s->error_recognition & AV_EF_EXPLODE)
140 return AVERROR_INVALIDDATA;
141 return 0;
142 }
143 }
144 if (trunclen == 0 && len >= buf_size - (buf_size >> 4)) {
147 if (!data)
148 return AVERROR(ENOMEM);
149 *bufp = NULL;
150 data->data += bytestream2_tell(&g);
152 } else {
154 return AVERROR(ENOMEM);
155
156 if (trunclen == 0) {
158 } else {
159 // If truncation was detected copy all data from block and
160 // read missing bytes not included in the block size.
162 ret = ffio_read_size(s->pb, data->data + len - trunclen, trunclen);
163 if (ret < 0)
164 goto fail;
165 }
166 }
167 memset(data->data + len, 0, AV_INPUT_BUFFER_PADDING_SIZE);
168
169 if (AV_RB64(data->data) == PNGSIG)
170 id = AV_CODEC_ID_PNG;
171
172 ret = ff_add_attached_pic(s, NULL, NULL, &data, 0);
173 if (ret < 0)
174 RETURN_ERROR(ret);
175
176 st = s->streams[s->nb_streams - 1];
177 st->codecpar->codec_id = id;
178 st->codecpar->width = width;
179 st->codecpar->height = height;
180 av_dict_set(&st->metadata, "comment", ff_id3v2_picture_types[type], 0);
181 if (desc)
182 av_dict_set(&st->metadata, "title", desc, 0);
183
184 return 0;
185
186fail:
188
189 return ret;
190}
Main libavformat public API header.
int ffio_read_size(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition aviobuf.c:665
static int BS_FUNC left(const BSCTX *bc)
Return the number of the bits left in a buffer.
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(const 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
static av_always_inline int bytestream2_tell(const GetByteContext *g)
Definition bytestream.h:192
#define s(width, name)
Definition cbs_vp9.c:198
#define NULL
Definition coverity.c:32
#define AV_EF_EXPLODE
abort decoding on minor error detection
Definition defs.h:51
#define FF_COMPLIANCE_NORMAL
Definition defs.h:60
int ff_add_attached_pic(AVFormatContext *s, AVStream *st, AVIOContext *pb, AVBufferRef **buf, int size)
Add an attached pic to an AVStream.
enum AVCodecID id
Definition dts2pts.c:607
#define MAX_TRUNC_PICTURE_SIZE
int ff_flac_parse_picture(AVFormatContext *s, uint8_t **bufp, int buf_size, int truncate_workaround)
Parse a FLAC METADATA_BLOCK_PICTURE.
#define RETURN_ERROR(code)
#define fail
Definition test.h:479
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition codec_id.h:47
@ AV_CODEC_ID_PNG
Definition codec_id.h:111
@ AV_CODEC_ID_NONE
Definition codec_id.h:48
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding.
Definition defs.h:40
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:139
void av_buffer_default_free(void *opaque, uint8_t *data)
Default free callback, which calls av_free() on the buffer data.
Definition buffer.c:72
AVBufferRef * av_buffer_alloc(size_t size)
Allocate an AVBuffer of the given size using av_malloc().
Definition buffer.c:77
AVBufferRef * av_buffer_create(uint8_t *data, size_t size, void(*free)(void *opaque, uint8_t *data), void *opaque, int flags)
Create an AVBuffer from an existing array.
Definition buffer.c:55
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:86
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_INFO
Standard information.
Definition log.h:221
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
cl_device_type type
const char *const ff_id3v2_picture_types[21]
Definition id3v2.c:114
const CodecMime ff_id3v2_mime_tags[]
Definition id3v2.c:138
#define AV_RB64(p)
const char * desc
Definition libsvtav1.c:83
const char data[16]
Definition mxf.c:149
#define PNGSIG
Definition png.h:49
#define FF_ARRAY_ELEMS(a)
A reference to a data buffer.
Definition buffer.h:82
int height
The height of the video frame in pixels.
Definition codec_par.h:150
int width
The width of the video frame in pixels.
Definition codec_par.h:143
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition codec_par.h:57
Format I/O context.
Definition avformat.h:1333
Stream structure.
Definition avformat.h:766
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:789
AVDictionary * metadata
Definition avformat.h:846
char str[32]
Definition internal.h:48
enum AVCodecID id
Definition internal.h:49
#define av_log(a,...)
#define height
Definition dsp.h:89
#define width
Definition dsp.h:89
const char * g
Definition vf_curves.c:128
int len