2 * Raw FLAC picture parser
3 * Copyright (c) 2001 Fabrice Bellard
5 * This file is part of FFmpeg.
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.
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.
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
22 #include "libavutil/avassert.h"
23 #include "libavutil/intreadwrite.h"
24 #include "libavcodec/png.h"
26 #include "flac_picture.h"
29 #include "avio_internal.h"
31 int ff_flac_parse_picture(AVFormatContext *s, uint8_t *buf, int buf_size)
33 const CodecMime *mime = ff_id3v2_mime_tags;
34 enum AVCodecID id = AV_CODEC_ID_NONE;
35 AVBufferRef *data = NULL;
36 uint8_t mimetype[64], *desc = NULL;
37 AVIOContext pb0, *pb = &pb0;
39 int width, height, ret = 0;
43 ffio_init_context(pb, buf, buf_size, 0, NULL, NULL, NULL, NULL);
45 /* read the picture type */
47 if (type >= FF_ARRAY_ELEMS(ff_id3v2_picture_types)) {
48 av_log(s, AV_LOG_ERROR, "Invalid picture type: %d.\n", type);
49 if (s->error_recognition & AV_EF_EXPLODE) {
50 RETURN_ERROR(AVERROR_INVALIDDATA);
55 /* picture mimetype */
57 if (len <= 0 || len >= 64 ||
58 avio_read(pb, mimetype, FFMIN(len, sizeof(mimetype) - 1)) != len) {
59 av_log(s, AV_LOG_ERROR, "Could not read mimetype from an attached "
61 if (s->error_recognition & AV_EF_EXPLODE)
62 ret = AVERROR_INVALIDDATA;
65 av_assert0(len < sizeof(mimetype));
68 while (mime->id != AV_CODEC_ID_NONE) {
69 if (!strncmp(mime->str, mimetype, sizeof(mimetype))) {
75 if (id == AV_CODEC_ID_NONE) {
76 av_log(s, AV_LOG_ERROR, "Unknown attached picture mimetype: %s.\n",
78 if (s->error_recognition & AV_EF_EXPLODE)
79 ret = AVERROR_INVALIDDATA;
83 /* picture description */
86 if (!(desc = av_malloc(len + 1))) {
87 RETURN_ERROR(AVERROR(ENOMEM));
90 if (avio_read(pb, desc, len) != len) {
91 av_log(s, AV_LOG_ERROR, "Error reading attached picture description.\n");
92 if (s->error_recognition & AV_EF_EXPLODE)
99 /* picture metadata */
100 width = avio_rb32(pb);
101 height = avio_rb32(pb);
107 av_log(s, AV_LOG_ERROR, "Invalid attached picture size: %d.\n", len);
108 if (s->error_recognition & AV_EF_EXPLODE)
109 ret = AVERROR_INVALIDDATA;
112 if (!(data = av_buffer_alloc(len + AV_INPUT_BUFFER_PADDING_SIZE))) {
113 RETURN_ERROR(AVERROR(ENOMEM));
115 memset(data->data + len, 0, AV_INPUT_BUFFER_PADDING_SIZE);
116 if (avio_read(pb, data->data, len) != len) {
117 av_log(s, AV_LOG_ERROR, "Error reading attached picture data.\n");
118 if (s->error_recognition & AV_EF_EXPLODE)
123 if (AV_RB64(data->data) == PNGSIG)
124 id = AV_CODEC_ID_PNG;
126 st = avformat_new_stream(s, NULL);
128 RETURN_ERROR(AVERROR(ENOMEM));
131 av_init_packet(&st->attached_pic);
132 st->attached_pic.buf = data;
133 st->attached_pic.data = data->data;
134 st->attached_pic.size = len;
135 st->attached_pic.stream_index = st->index;
136 st->attached_pic.flags |= AV_PKT_FLAG_KEY;
138 st->disposition |= AV_DISPOSITION_ATTACHED_PIC;
139 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
140 st->codecpar->codec_id = id;
141 st->codecpar->width = width;
142 st->codecpar->height = height;
143 av_dict_set(&st->metadata, "comment", ff_id3v2_picture_types[type], 0);
145 av_dict_set(&st->metadata, "title", desc, AV_DICT_DONT_STRDUP_VAL);
150 av_buffer_unref(&data);