2 * Raw FLAC picture parser
4 * This file is part of Libav.
6 * Libav is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * Libav is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with Libav; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "flac_picture.h"
26 int ff_flac_parse_picture(AVFormatContext *s, uint8_t *buf, int buf_size)
28 const CodecMime *mime = ff_id3v2_mime_tags;
29 enum AVCodecID id = AV_CODEC_ID_NONE;
30 AVBufferRef *data = NULL;
31 uint8_t mimetype[64], *desc = NULL;
32 AVIOContext *pb = NULL;
34 int type, width, height;
37 pb = avio_alloc_context(buf, buf_size, 0, NULL, NULL, NULL, NULL);
39 return AVERROR(ENOMEM);
41 /* read the picture type */
43 if (type >= FF_ARRAY_ELEMS(ff_id3v2_picture_types) || type < 0) {
44 av_log(s, AV_LOG_ERROR, "Invalid picture type: %d.\n", type);
45 if (s->error_recognition & AV_EF_EXPLODE) {
46 ret = AVERROR_INVALIDDATA;
52 /* picture mimetype */
55 avio_read(pb, mimetype, FFMIN(len, sizeof(mimetype) - 1)) != len) {
56 av_log(s, AV_LOG_ERROR, "Could not read mimetype from an attached "
58 if (s->error_recognition & AV_EF_EXPLODE)
59 ret = AVERROR_INVALIDDATA;
64 while (mime->id != AV_CODEC_ID_NONE) {
65 if (!strncmp(mime->str, mimetype, sizeof(mimetype))) {
71 if (id == AV_CODEC_ID_NONE) {
72 av_log(s, AV_LOG_ERROR, "Unknown attached picture mimetype: %s.\n",
74 if (s->error_recognition & AV_EF_EXPLODE)
75 ret = AVERROR_INVALIDDATA;
79 /* picture description */
82 if (!(desc = av_malloc(len + 1))) {
83 ret = AVERROR(ENOMEM);
87 if (avio_read(pb, desc, len) != len) {
88 av_log(s, AV_LOG_ERROR, "Error reading attached picture description.\n");
89 if (s->error_recognition & AV_EF_EXPLODE)
96 /* picture metadata */
97 width = avio_rb32(pb);
98 height = avio_rb32(pb);
104 av_log(s, AV_LOG_ERROR, "Invalid attached picture size: %d.\n", len);
105 if (s->error_recognition & AV_EF_EXPLODE)
106 ret = AVERROR_INVALIDDATA;
109 if (!(data = av_buffer_alloc(len))) {
110 ret = AVERROR(ENOMEM);
113 if (avio_read(pb, data->data, len) != len) {
114 av_log(s, AV_LOG_ERROR, "Error reading attached picture data.\n");
115 if (s->error_recognition & AV_EF_EXPLODE)
120 st = avformat_new_stream(s, NULL);
122 ret = AVERROR(ENOMEM);
126 av_init_packet(&st->attached_pic);
127 st->attached_pic.buf = data;
128 st->attached_pic.data = data->data;
129 st->attached_pic.size = len;
130 st->attached_pic.stream_index = st->index;
131 st->attached_pic.flags |= AV_PKT_FLAG_KEY;
133 st->disposition |= AV_DISPOSITION_ATTACHED_PIC;
134 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
135 st->codec->codec_id = id;
136 st->codec->width = width;
137 st->codec->height = height;
138 av_dict_set(&st->metadata, "comment", ff_id3v2_picture_types[type], 0);
140 av_dict_set(&st->metadata, "title", desc, AV_DICT_DONT_STRDUP_VAL);
147 av_buffer_unref(&data);