]> git.sesse.net Git - ffmpeg/blob - libavformat/flac_picture.c
hevc_parser: fix standalone build with the hevc decoder disabled
[ffmpeg] / libavformat / flac_picture.c
1 /*
2  * Raw FLAC picture parser
3  *
4  * This file is part of Libav.
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "avformat.h"
22 #include "flac_picture.h"
23 #include "id3v2.h"
24 #include "internal.h"
25
26 int ff_flac_parse_picture(AVFormatContext *s, uint8_t *buf, int buf_size)
27 {
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;
33     AVStream *st;
34     int width, height, ret = 0;
35     unsigned int type, len;
36
37     pb = avio_alloc_context(buf, buf_size, 0, NULL, NULL, NULL, NULL);
38     if (!pb)
39         return AVERROR(ENOMEM);
40
41     /* read the picture type */
42     type = avio_rb32(pb);
43     if (type >= FF_ARRAY_ELEMS(ff_id3v2_picture_types)) {
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;
47             goto fail;
48         }
49         type = 0;
50     }
51
52     /* picture mimetype */
53     len = avio_rb32(pb);
54     if (!len || len >= 64 ||
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 "
57                "picture.\n");
58         if (s->error_recognition & AV_EF_EXPLODE)
59             ret = AVERROR_INVALIDDATA;
60         goto fail;
61     }
62     mimetype[len] = 0;
63
64     while (mime->id != AV_CODEC_ID_NONE) {
65         if (!strncmp(mime->str, mimetype, sizeof(mimetype))) {
66             id = mime->id;
67             break;
68         }
69         mime++;
70     }
71     if (id == AV_CODEC_ID_NONE) {
72         av_log(s, AV_LOG_ERROR, "Unknown attached picture mimetype: %s.\n",
73                mimetype);
74         if (s->error_recognition & AV_EF_EXPLODE)
75             ret = AVERROR_INVALIDDATA;
76         goto fail;
77     }
78
79     /* picture description */
80     len = avio_rb32(pb);
81     if (len > 0) {
82         if (!(desc = av_malloc(len + 1))) {
83             ret = AVERROR(ENOMEM);
84             goto fail;
85         }
86
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)
90                 ret = AVERROR(EIO);
91             goto fail;
92         }
93         desc[len] = 0;
94     }
95
96     /* picture metadata */
97     width  = avio_rb32(pb);
98     height = avio_rb32(pb);
99     avio_skip(pb, 8);
100
101     /* picture data */
102     len = avio_rb32(pb);
103     if (!len) {
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;
107         goto fail;
108     }
109     if (!(data = av_buffer_alloc(len))) {
110         ret = AVERROR(ENOMEM);
111         goto fail;
112     }
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)
116             ret = AVERROR(EIO);
117         goto fail;
118     }
119
120     st = avformat_new_stream(s, NULL);
121     if (!st) {
122         ret = AVERROR(ENOMEM);
123         goto fail;
124     }
125
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;
132
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);
139     if (desc)
140         av_dict_set(&st->metadata, "title", desc, AV_DICT_DONT_STRDUP_VAL);
141
142     av_freep(&pb);
143
144     return 0;
145
146 fail:
147     av_buffer_unref(&data);
148     av_freep(&desc);
149     av_freep(&pb);
150
151     return ret;
152 }