+int ff_id3v2_write_apic(AVFormatContext *s, ID3v2EncContext *id3, AVPacket *pkt)
+{
+ AVStream *st = s->streams[pkt->stream_index];
+ AVDictionaryEntry *e;
+
+ AVIOContext *dyn_buf;
+ uint8_t *buf;
+ const CodecMime *mime = ff_id3v2_mime_tags;
+ const char *mimetype = NULL, *desc = "";
+ int enc = id3->version == 3 ? ID3v2_ENCODING_UTF16BOM :
+ ID3v2_ENCODING_UTF8;
+ int i, len, type = 0;
+
+ /* get the mimetype*/
+ while (mime->id != AV_CODEC_ID_NONE) {
+ if (mime->id == st->codec->codec_id) {
+ mimetype = mime->str;
+ break;
+ }
+ mime++;
+ }
+ if (!mimetype) {
+ av_log(s, AV_LOG_ERROR, "No mimetype is known for stream %d, cannot "
+ "write an attached picture.\n", st->index);
+ return AVERROR(EINVAL);
+ }
+
+ /* get the picture type */
+ e = av_dict_get(st->metadata, "comment", NULL, 0);
+ for (i = 0; e && i < FF_ARRAY_ELEMS(ff_id3v2_picture_types); i++) {
+ if (strstr(ff_id3v2_picture_types[i], e->value) == ff_id3v2_picture_types[i]) {
+ type = i;
+ break;
+ }
+ }
+
+ /* get the description */
+ if ((e = av_dict_get(st->metadata, "title", NULL, 0)))
+ desc = e->value;
+
+ /* start writing */
+ if (avio_open_dyn_buf(&dyn_buf) < 0)
+ return AVERROR(ENOMEM);
+
+ avio_w8(dyn_buf, enc);
+ avio_put_str(dyn_buf, mimetype);
+ avio_w8(dyn_buf, type);
+ id3v2_encode_string(dyn_buf, desc, enc);
+ avio_write(dyn_buf, pkt->data, pkt->size);
+ len = avio_close_dyn_buf(dyn_buf, &buf);
+
+ avio_wb32(s->pb, MKBETAG('A', 'P', 'I', 'C'));
+ if (id3->version == 3)
+ avio_wb32(s->pb, len);
+ else
+ id3v2_put_size(s->pb, len);
+ avio_wb16(s->pb, 0);
+ avio_write(s->pb, buf, len);
+ av_freep(&buf);
+
+ id3->len += len + ID3v2_HEADER_SIZE;
+
+ return 0;
+}
+