]> git.sesse.net Git - ffmpeg/blob - libavformat/flac_picture.c
avformat/hlsenc: free the original malloc pointer to avoid error when system function...
[ffmpeg] / libavformat / flac_picture.c
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/avassert.h"
23 #include "libavutil/intreadwrite.h"
24 #include "libavcodec/png.h"
25 #include "avformat.h"
26 #include "flac_picture.h"
27 #include "id3v2.h"
28 #include "internal.h"
29 #include "avio_internal.h"
30
31 int ff_flac_parse_picture(AVFormatContext *s, uint8_t *buf, int buf_size)
32 {
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;
38     AVStream *st;
39     int width, height, ret = 0;
40     int len;
41     unsigned int type;
42
43     ffio_init_context(pb, buf, buf_size, 0, NULL, NULL, NULL, NULL);
44
45     /* read the picture type */
46     type = avio_rb32(pb);
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);
51         }
52         type = 0;
53     }
54
55     /* picture mimetype */
56     len = avio_rb32(pb);
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 "
60                "picture.\n");
61         if (s->error_recognition & AV_EF_EXPLODE)
62             ret = AVERROR_INVALIDDATA;
63         goto fail;
64     }
65     av_assert0(len < sizeof(mimetype));
66     mimetype[len] = 0;
67
68     while (mime->id != AV_CODEC_ID_NONE) {
69         if (!strncmp(mime->str, mimetype, sizeof(mimetype))) {
70             id = mime->id;
71             break;
72         }
73         mime++;
74     }
75     if (id == AV_CODEC_ID_NONE) {
76         av_log(s, AV_LOG_ERROR, "Unknown attached picture mimetype: %s.\n",
77                mimetype);
78         if (s->error_recognition & AV_EF_EXPLODE)
79             ret = AVERROR_INVALIDDATA;
80         goto fail;
81     }
82
83     /* picture description */
84     len = avio_rb32(pb);
85     if (len > 0) {
86         if (!(desc = av_malloc(len + 1))) {
87             RETURN_ERROR(AVERROR(ENOMEM));
88         }
89
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)
93                 ret = AVERROR(EIO);
94             goto fail;
95         }
96         desc[len] = 0;
97     }
98
99     /* picture metadata */
100     width  = avio_rb32(pb);
101     height = avio_rb32(pb);
102     avio_skip(pb, 8);
103
104     /* picture data */
105     len = avio_rb32(pb);
106     if (len <= 0) {
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;
110         goto fail;
111     }
112     if (!(data = av_buffer_alloc(len + AV_INPUT_BUFFER_PADDING_SIZE))) {
113         RETURN_ERROR(AVERROR(ENOMEM));
114     }
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)
119             ret = AVERROR(EIO);
120         goto fail;
121     }
122
123     if (AV_RB64(data->data) == PNGSIG)
124         id = AV_CODEC_ID_PNG;
125
126     st = avformat_new_stream(s, NULL);
127     if (!st) {
128         RETURN_ERROR(AVERROR(ENOMEM));
129     }
130
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;
137
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);
144     if (desc)
145         av_dict_set(&st->metadata, "title", desc, AV_DICT_DONT_STRDUP_VAL);
146
147     return 0;
148
149 fail:
150     av_buffer_unref(&data);
151     av_freep(&desc);
152
153     return ret;
154 }