Libav
flac_picture.c
Go to the documentation of this file.
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;
31  uint8_t mimetype[64], *desc = NULL;
32  AVIOContext *pb = NULL;
33  AVStream *st;
34  int type, width, height;
35  int len, ret = 0;
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) || type < 0) {
44  av_log(s, AV_LOG_ERROR, "Invalid picture type: %d.\n", type);
46  ret = AVERROR_INVALIDDATA;
47  goto fail;
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  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");
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 <= 0) {
104  av_log(s, AV_LOG_ERROR, "Invalid attached picture size: %d.\n", len);
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");
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 
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;
132 
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 }
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:62
Bytestream IO Context.
Definition: avio.h:68
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it...
Definition: buffer.c:106
enum AVCodecID id
Definition: mxfenc.c:84
int index
stream index in AVFormatContext
Definition: avformat.h:700
int size
Definition: avcodec.h:974
#define FF_ARRAY_ELEMS(a)
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:198
Format I/O context.
Definition: avformat.h:922
char str[32]
Definition: internal.h:41
uint8_t
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:595
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:2521
const char data[16]
Definition: mxf.c:70
uint8_t * data
Definition: avcodec.h:973
enum AVCodecID id
Definition: internal.h:42
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:452
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1019
AVIOContext * avio_alloc_context(unsigned char *buffer, int buffer_size, int write_flag, void *opaque, int(*read_packet)(void *opaque, uint8_t *buf, int buf_size), int(*write_packet)(void *opaque, uint8_t *buf, int buf_size), int64_t(*seek)(void *opaque, int64_t offset, int whence))
Allocate and initialize an AVIOContext for buffered I/O.
Definition: aviobuf.c:107
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: avcodec.h:105
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
#define AVERROR(e)
Definition: error.h:43
AVBufferRef * buf
A reference to the reference-counted buffer where the packet data is stored.
Definition: avcodec.h:956
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:168
const char * ff_id3v2_picture_types[21]
Definition: id3v2.c:89
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:979
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:718
const CodecMime ff_id3v2_mime_tags[]
Definition: id3v2.c:113
#define FFMIN(a, b)
Definition: common.h:57
#define AV_DICT_DONT_STRDUP_VAL
Take ownership of a value that's been allocated with av_malloc() and chilren.
Definition: dict.h:66
int width
picture width / height.
Definition: avcodec.h:1224
static av_always_inline int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: avio.h:210
int ff_flac_parse_picture(AVFormatContext *s, uint8_t *buf, int buf_size)
Definition: flac_picture.c:26
#define AV_EF_EXPLODE
Definition: avcodec.h:2417
AVDictionary * metadata
Definition: avformat.h:771
#define AV_DISPOSITION_ATTACHED_PIC
The stream is stored in the file as an attached picture/"cover art" (e.g.
Definition: avformat.h:690
Stream structure.
Definition: avformat.h:699
NULL
Definition: eval.c:55
static int width
Definition: utils.c:156
enum AVMediaType codec_type
Definition: avcodec.h:1058
enum AVCodecID codec_id
Definition: avcodec.h:1067
AVBufferRef * av_buffer_alloc(int size)
Allocate an AVBuffer of the given size using av_malloc().
Definition: buffer.c:66
uint8_t * data
The data buffer.
Definition: buffer.h:89
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition: dict.c:68
int height
Definition: gxfenc.c:72
A reference to a data buffer.
Definition: buffer.h:81
Main libavformat public API header.
int error_recognition
Error recognition; higher values will detect more errors but may misdetect some more or less valid pa...
Definition: avformat.h:1152
int disposition
AV_DISPOSITION_* bit field.
Definition: avformat.h:760
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
Definition: avpacket.c:47
int len
int stream_index
Definition: avcodec.h:975
AVPacket attached_pic
For streams with AV_DISPOSITION_ATTACHED_PIC disposition, this packet will contain the attached pictu...
Definition: avformat.h:789