FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
flacdec_picture.c
Go to the documentation of this file.
1 /*
2  * Raw FLAC demuxer
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 "avformat.h"
23 #include "flacdec.h"
24 #include "id3v2.h"
25 #include "internal.h"
26 
28 {
29  const CodecMime *mime = ff_id3v2_mime_tags;
30  enum AVCodecID id = AV_CODEC_ID_NONE;
31  AVBufferRef *data = NULL;
32  uint8_t mimetype[64], *desc = NULL;
33  AVIOContext *pb = NULL;
34  AVStream *st;
35  int type, width, height;
36  int len, ret = 0;
37 
38  pb = avio_alloc_context(buf, buf_size, 0, NULL, NULL, NULL, NULL);
39  if (!pb)
40  return AVERROR(ENOMEM);
41 
42  /* read the picture type */
43  type = avio_rb32(pb);
44  if (type >= FF_ARRAY_ELEMS(ff_id3v2_picture_types) || type < 0) {
45  av_log(s, AV_LOG_ERROR, "Invalid picture type: %d.\n", type);
48  }
49  type = 0;
50  }
51 
52  /* picture mimetype */
53  len = avio_rb32(pb);
54  if (len <= 0 ||
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");
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);
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  RETURN_ERROR(AVERROR(ENOMEM));
84  }
85 
86  if (avio_read(pb, desc, len) != len) {
87  av_log(s, AV_LOG_ERROR, "Error reading attached picture description.\n");
89  ret = AVERROR(EIO);
90  goto fail;
91  }
92  desc[len] = 0;
93  }
94 
95  /* picture metadata */
96  width = avio_rb32(pb);
97  height = avio_rb32(pb);
98  avio_skip(pb, 8);
99 
100  /* picture data */
101  len = avio_rb32(pb);
102  if (len <= 0) {
103  av_log(s, AV_LOG_ERROR, "Invalid attached picture size: %d.\n", len);
105  ret = AVERROR_INVALIDDATA;
106  goto fail;
107  }
108  if (!(data = av_buffer_alloc(len))) {
109  RETURN_ERROR(AVERROR(ENOMEM));
110  }
111  if (avio_read(pb, data->data, len) != len) {
112  av_log(s, AV_LOG_ERROR, "Error reading attached picture data.\n");
114  ret = AVERROR(EIO);
115  goto fail;
116  }
117 
118  st = avformat_new_stream(s, NULL);
119  if (!st) {
120  RETURN_ERROR(AVERROR(ENOMEM));
121  }
122 
124  st->attached_pic.buf = data;
125  st->attached_pic.data = data->data;
126  st->attached_pic.size = len;
127  st->attached_pic.stream_index = st->index;
129 
132  st->codec->codec_id = id;
133  st->codec->width = width;
134  st->codec->height = height;
135  av_dict_set(&st->metadata, "comment", ff_id3v2_picture_types[type], 0);
136  if (desc)
137  av_dict_set(&st->metadata, "title", desc, AV_DICT_DONT_STRDUP_VAL);
138 
139  av_freep(&pb);
140 
141  return 0;
142 
143 fail:
144  av_buffer_unref(&data);
145  av_freep(&desc);
146  av_freep(&pb);
147  return ret;
148 }