FFmpeg  2.1.1
flac_picture.c
Go to the documentation of this file.
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 "avformat.h"
24 #include "flac_picture.h"
25 #include "id3v2.h"
26 #include "internal.h"
27 
29 {
30  const CodecMime *mime = ff_id3v2_mime_tags;
31  enum AVCodecID id = AV_CODEC_ID_NONE;
32  AVBufferRef *data = NULL;
33  uint8_t mimetype[64], *desc = NULL;
34  AVIOContext *pb = NULL;
35  AVStream *st;
36  int type, width, height;
37  int len, ret = 0;
38 
39  pb = avio_alloc_context(buf, buf_size, 0, NULL, NULL, NULL, NULL);
40  if (!pb)
41  return AVERROR(ENOMEM);
42 
43  /* read the picture type */
44  type = avio_rb32(pb);
45  if (type >= FF_ARRAY_ELEMS(ff_id3v2_picture_types) || type < 0) {
46  av_log(s, AV_LOG_ERROR, "Invalid picture type: %d.\n", type);
49  }
50  type = 0;
51  }
52 
53  /* picture mimetype */
54  len = avio_rb32(pb);
55  if (len <= 0 ||
56  avio_read(pb, mimetype, FFMIN(len, sizeof(mimetype) - 1)) != len) {
57  av_log(s, AV_LOG_ERROR, "Could not read mimetype from an attached "
58  "picture.\n");
60  ret = AVERROR_INVALIDDATA;
61  goto fail;
62  }
63  av_assert0(len < sizeof(mimetype));
64  mimetype[len] = 0;
65 
66  while (mime->id != AV_CODEC_ID_NONE) {
67  if (!strncmp(mime->str, mimetype, sizeof(mimetype))) {
68  id = mime->id;
69  break;
70  }
71  mime++;
72  }
73  if (id == AV_CODEC_ID_NONE) {
74  av_log(s, AV_LOG_ERROR, "Unknown attached picture mimetype: %s.\n",
75  mimetype);
77  ret = AVERROR_INVALIDDATA;
78  goto fail;
79  }
80 
81  /* picture description */
82  len = avio_rb32(pb);
83  if (len > 0) {
84  if (!(desc = av_malloc(len + 1))) {
85  RETURN_ERROR(AVERROR(ENOMEM));
86  }
87 
88  if (avio_read(pb, desc, len) != len) {
89  av_log(s, AV_LOG_ERROR, "Error reading attached picture description.\n");
91  ret = AVERROR(EIO);
92  goto fail;
93  }
94  desc[len] = 0;
95  }
96 
97  /* picture metadata */
98  width = avio_rb32(pb);
99  height = avio_rb32(pb);
100  avio_skip(pb, 8);
101 
102  /* picture data */
103  len = avio_rb32(pb);
104  if (len <= 0) {
105  av_log(s, AV_LOG_ERROR, "Invalid attached picture size: %d.\n", len);
107  ret = AVERROR_INVALIDDATA;
108  goto fail;
109  }
110  if (!(data = av_buffer_alloc(len))) {
111  RETURN_ERROR(AVERROR(ENOMEM));
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  RETURN_ERROR(AVERROR(ENOMEM));
123  }
124 
126  st->attached_pic.buf = data;
127  st->attached_pic.data = data->data;
128  st->attached_pic.size = len;
129  st->attached_pic.stream_index = st->index;
131 
134  st->codec->codec_id = id;
135  st->codec->width = width;
136  st->codec->height = height;
137  av_dict_set(&st->metadata, "comment", ff_id3v2_picture_types[type], 0);
138  if (desc)
139  av_dict_set(&st->metadata, "title", desc, AV_DICT_DONT_STRDUP_VAL);
140 
141  av_freep(&pb);
142 
143  return 0;
144 
145 fail:
146  av_buffer_unref(&data);
147  av_freep(&desc);
148  av_freep(&pb);
149 
150  return ret;
151 }
const char * s
Definition: avisynth_c.h:668
Bytestream IO Context.
Definition: avio.h:68
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:105
#define AV_DISPOSITION_ATTACHED_PIC
The stream is stored in the file as an attached picture/&quot;cover art&quot; (e.g.
Definition: avformat.h:644
enum AVCodecID id
Definition: mxfenc.c:90
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:686
int index
stream index in AVFormatContext
Definition: avformat.h:668
int size
Definition: avcodec.h:1064
void av_log(void *avcl, int level, const char *fmt,...) av_printf_format(3
Send the specified message to the log if the level is less than or equal to the current av_log_level...
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:624
AVBufferRef * buf
A reference to the reference-counted buffer where the packet data is stored.
Definition: avcodec.h:1046
void av_freep(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:234
AVDictionary * metadata
Definition: avformat.h:735
Format I/O context.
Definition: avformat.h:968
char str[32]
Definition: internal.h:45
uint8_t
const char data[16]
Definition: mxf.c:68
enum AVCodecID id
Definition: internal.h:46
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:3348
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1113
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: avcodec.h:102
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: avcodec.h:4147
const char * ff_id3v2_picture_types[21]
Definition: id3v2.c:102
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1069
goto fail
Definition: avfilter.c:963
const CodecMime ff_id3v2_mime_tags[]
Definition: id3v2.c:126
#define FF_ARRAY_ELEMS(a)
Definition: avcodec.h:929
uint8_t * data
The data buffer.
Definition: buffer.h:89
#define AV_DICT_DONT_STRDUP_VAL
Take ownership of a value that&#39;s been allocated with av_malloc() and chilren.
Definition: dict.h:72
ret
Definition: avfilter.c:961
int width
picture width / height.
Definition: avcodec.h:1314
void * av_malloc(size_t size) av_malloc_attrib 1(1)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:73
#define FFMIN(a, b)
Definition: avcodec.h:925
int ff_flac_parse_picture(AVFormatContext *s, uint8_t *buf, int buf_size)
Definition: flac_picture.c:28
Stream structure.
Definition: avformat.h:667
static int width
Definition: utils.c:158
AVBufferRef * av_buffer_alloc(int size)
Allocate an AVBuffer of the given size using av_malloc().
Definition: buffer.c:65
enum AVMediaType codec_type
Definition: avcodec.h:1154
enum AVCodecID codec_id
Definition: avcodec.h:1157
#define AV_EF_EXPLODE
abort decoding on minor error detection
Definition: avcodec.h:2480
void * buf
Definition: avisynth_c.h:594
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:62
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:109
BYTE int const BYTE int int int height
Definition: avisynth_c.h:713
uint8_t * data
Definition: avcodec.h:1063
#define RETURN_ERROR(code)
Definition: flac_picture.h:27
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:256
#define type
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:480
A reference to a data buffer.
Definition: buffer.h:81
int error_recognition
Error recognition; higher values will detect more errors but may misdetect some more or less valid pa...
Definition: avformat.h:1150
Main libavformat public API header.
int disposition
AV_DISPOSITION_* bit field.
Definition: avformat.h:724
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
Definition: avpacket.c:49
#define AVERROR_INVALIDDATA
int len
#define AVERROR(e)
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
int stream_index
Definition: avcodec.h:1065
AVPacket attached_pic
For streams with AV_DISPOSITION_ATTACHED_PIC disposition, this packet will contain the attached pictu...
Definition: avformat.h:749