+ /* 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;
+}
+
+void ff_id3v2_finish(ID3v2EncContext *id3, AVIOContext *pb)
+{
+ int64_t cur_pos = avio_tell(pb);
+ avio_seek(pb, id3->size_pos, SEEK_SET);
+ id3v2_put_size(pb, id3->len);
+ avio_seek(pb, cur_pos, SEEK_SET);
+}
+
+int ff_id3v2_write_simple(struct AVFormatContext *s, int id3v2_version,
+ const char *magic)
+{
+ ID3v2EncContext id3 = { 0 };
+ int ret;
+
+ ff_id3v2_start(&id3, s->pb, id3v2_version, magic);
+ if ((ret = ff_id3v2_write_metadata(s, &id3)) < 0)
+ return ret;
+ ff_id3v2_finish(&id3, s->pb);
+