FFmpeg
Loading...
Searching...
No Matches
id3v2.c File Reference

ID3v2 header parser. More...

#include "config.h"
#include "libavutil/attributes.h"
#include "libavutil/attributes_internal.h"
#include "libavutil/avstring.h"
#include "libavutil/bprint.h"
#include "libavutil/dict.h"
#include "libavutil/intreadwrite.h"
#include "libavutil/mem.h"
#include "libavcodec/png.h"
#include "avio_internal.h"
#include "avlanguage.h"
#include "demux.h"
#include "id3v1.h"
#include "id3v2.h"

Go to the source code of this file.

Data Structures

struct  ExtraMetaList
 
struct  ID3v2EMFunc
 

Functions

int ff_id3v2_match (const uint8_t *buf, const char *magic)
 Detect ID3v2 Header.
 
int ff_id3v2_tag_len (const uint8_t *buf)
 Get the length of an ID3v2 tag.
 
static unsigned int get_size (AVIOContext *s, int len)
 
static unsigned int size_to_syncsafe (unsigned int size)
 
static int is_tag (const char *buf, unsigned int len)
 
static int check_tag (AVIOContext *s, int offset, unsigned int len)
 Return 1 if the tag of length len at the given offset is valid, 0 if not, -1 on error.
 
static void free_geobtag (void *obj)
 Free GEOB type extra metadata.
 
static int decode_str (AVFormatContext *s, AVIOContext *pb, int encoding, uint8_t **dst, int *maxread)
 Decode characters to UTF-8 according to encoding type.
 
static void read_ttag (AVFormatContext *s, AVIOContext *pb, int taglen, AVDictionary **metadata, const char *key)
 Parse a text tag.
 
static void read_lang_descr_tag (AVFormatContext *s, AVIOContext *pb, const char *key, int taglen, AVDictionary **metadata)
 Parse a lang descr tag such as COMM and USLT.
 
static void list_append (ID3v2ExtraMeta *new_elem, ExtraMetaList *list)
 
static void read_geobtag (AVFormatContext *s, AVIOContext *pb, int taglen, const char *tag, ExtraMetaList *extra_meta, int isv34)
 Parse GEOB tag into a ID3v2ExtraMetaGEOB struct.
 
static int is_number (const char *str)
 
static AVDictionaryEntryget_date_tag (AVDictionary *m, const char *tag)
 
static void merge_date (AVDictionary **m)
 
static void free_apic (void *obj)
 
static void rstrip_spaces (char *buf)
 
static void read_apic (AVFormatContext *s, AVIOContext *pb, int taglen, const char *tag, ExtraMetaList *extra_meta, int isv34)
 
static void free_chapter (void *obj)
 
static void read_chapter (AVFormatContext *s, AVIOContext *pb, int len, const char *ttag, ExtraMetaList *extra_meta, int isv34)
 
static void free_priv (void *obj)
 
static void read_priv (AVFormatContext *s, AVIOContext *pb, int taglen, const char *tag, ExtraMetaList *extra_meta, int isv34)
 
static const ID3v2EMFuncget_extra_meta_func (const char *tag, int isv34)
 Get the corresponding ID3v2EMFunc struct for a tag.
 
static void id3v2_parse (AVIOContext *pb, AVDictionary **metadata, AVFormatContext *s, int len, uint8_t version, uint8_t flags, ExtraMetaList *extra_meta)
 
static void id3v2_read_internal (AVIOContext *pb, AVDictionary **metadata, AVFormatContext *s, const char *magic, ID3v2ExtraMeta **extra_metap, int64_t max_search_size)
 
void ff_id3v2_read_dict (AVFormatContext *s, AVIOContext *pb, AVDictionary **metadata, const char *magic, ID3v2ExtraMeta **extra_meta)
 Read an ID3v2 tag into specified dictionary and retrieve supported extra metadata.
 
void ff_id3v2_read (AVFormatContext *s, const char *magic, ID3v2ExtraMeta **extra_meta, unsigned int max_search_size)
 Read an ID3v2 tag, including supported extra metadata.
 
void ff_id3v2_free_extra_meta (ID3v2ExtraMeta **extra_meta)
 Free memory allocated parsing special (non-text) metadata.
 
int ff_id3v2_parse_apic (AVFormatContext *s, ID3v2ExtraMeta *extra_meta)
 Create a stream for each APIC (attached picture) extracted from the ID3v2 header.
 
int ff_id3v2_parse_chapters (AVFormatContext *s, ID3v2ExtraMeta *cur)
 Create chapters for all CHAP tags found in the ID3v2 header.
 
int ff_id3v2_parse_priv_dict (AVDictionary **metadata, ID3v2ExtraMeta *extra_meta)
 Parse PRIV tags into a dictionary.
 
int ff_id3v2_parse_priv (AVFormatContext *s, ID3v2ExtraMeta *extra_meta)
 Add metadata for all PRIV tags in the ID3v2 header.
 

Variables

const AVMetadataConv ff_id3v2_34_metadata_conv []
 
const AVMetadataConv ff_id3v2_4_metadata_conv []
 
static const AVMetadataConv id3v2_2_metadata_conv []
 
attribute_nonstring const char ff_id3v2_tags [][4]
 A list of text information frames allowed in both ID3 v2.3 and v2.4 http://www.id3.org/id3v2.4.0-frames http://www.id3.org/id3v2.4.0-changes.
 
attribute_nonstring const char ff_id3v2_4_tags [][4]
 ID3v2.4-only text information frames.
 
attribute_nonstring const char ff_id3v2_3_tags [][4]
 ID3v2.3-only text information frames.
 
const char *const ff_id3v2_picture_types [21]
 
const CodecMime ff_id3v2_mime_tags []
 
static const ID3v2EMFunc id3v2_extra_meta_funcs []
 

Detailed Description

ID3v2 header parser.

Specifications available at: http://id3.org/Developer_Information

Definition in file id3v2.c.

Function Documentation

◆ ff_id3v2_match()

int ff_id3v2_match ( const uint8_t * buf,
const char * magic )

Detect ID3v2 Header.

Parameters
bufmust be ID3v2_HEADER_SIZE byte long
magicmagic bytes to identify the header. If in doubt, use ID3v2_DEFAULT_MAGIC.

Definition at line 152 of file id3v2.c.

Referenced by adts_aac_read_packet(), av_probe_input_format3(), id3v2_read_internal(), intercept_id3(), mp3_read_probe(), and oma_read_probe().

◆ ff_id3v2_tag_len()

int ff_id3v2_tag_len ( const uint8_t * buf)

Get the length of an ID3v2 tag.

Parameters
bufmust be ID3v2_HEADER_SIZE bytes long and point to the start of an already detected ID3v2 tag

Definition at line 165 of file id3v2.c.

Referenced by av_probe_input_format3(), handle_id3(), intercept_id3(), mp3_read_probe(), and oma_read_probe().

◆ get_size()

static unsigned int get_size ( AVIOContext * s,
int len )
static

Definition at line 177 of file id3v2.c.

Referenced by id3v2_parse().

◆ size_to_syncsafe()

static unsigned int size_to_syncsafe ( unsigned int size)
static

Definition at line 185 of file id3v2.c.

Referenced by id3v2_parse().

◆ is_tag()

static int is_tag ( const char * buf,
unsigned int len )
static

Definition at line 195 of file id3v2.c.

Referenced by check_tag().

◆ check_tag()

static int check_tag ( AVIOContext * s,
int offset,
unsigned int len )
static

Return 1 if the tag of length len at the given offset is valid, 0 if not, -1 on error.

Definition at line 213 of file id3v2.c.

Referenced by id3v2_parse().

◆ free_geobtag()

static void free_geobtag ( void * obj)
static

Free GEOB type extra metadata.

Definition at line 230 of file id3v2.c.

Referenced by read_geobtag().

◆ decode_str()

static int decode_str ( AVFormatContext * s,
AVIOContext * pb,
int encoding,
uint8_t ** dst,
int * maxread )
static

Decode characters to UTF-8 according to encoding type.

The decoded buffer is always null terminated. Stop reading when either *maxread bytes are read from pb or U+0000 character is found.

Parameters
dstPointer where the address of the buffer with the decoded bytes is stored. Buffer must be freed by caller.
maxreadPointer to maximum number of characters to read from the AVIOContext. After execution the value is decremented by the number of bytes actually read.
Returns
0 if no error occurred, dst is uninitialized on error

Definition at line 251 of file id3v2.c.

Referenced by read_apic(), read_chapter(), read_geobtag(), read_lang_descr_tag(), read_priv(), and read_ttag().

◆ read_ttag()

static void read_ttag ( AVFormatContext * s,
AVIOContext * pb,
int taglen,
AVDictionary ** metadata,
const char * key )
static

Parse a text tag.

Definition at line 340 of file id3v2.c.

Referenced by id3v2_parse(), and read_chapter().

◆ read_lang_descr_tag()

static void read_lang_descr_tag ( AVFormatContext * s,
AVIOContext * pb,
const char * key,
int taglen,
AVDictionary ** metadata )
static

Parse a lang descr tag such as COMM and USLT.

A non-empty descriptor produces "<base>-<descriptor>-<lang>" keys.

Definition at line 384 of file id3v2.c.

Referenced by id3v2_parse().

◆ list_append()

static void list_append ( ID3v2ExtraMeta * new_elem,
ExtraMetaList * list )
static

Definition at line 477 of file id3v2.c.

Referenced by read_apic(), read_chapter(), read_geobtag(), and read_priv().

◆ read_geobtag()

static void read_geobtag ( AVFormatContext * s,
AVIOContext * pb,
int taglen,
const char * tag,
ExtraMetaList * extra_meta,
int isv34 )
static

Parse GEOB tag into a ID3v2ExtraMetaGEOB struct.

Definition at line 489 of file id3v2.c.

◆ is_number()

static int is_number ( const char * str)
static

Definition at line 558 of file id3v2.c.

Referenced by get_date_tag().

◆ get_date_tag()

static AVDictionaryEntry * get_date_tag ( AVDictionary * m,
const char * tag )
static

Definition at line 565 of file id3v2.c.

Referenced by merge_date().

◆ merge_date()

static void merge_date ( AVDictionary ** m)
static

Definition at line 574 of file id3v2.c.

Referenced by id3v2_read_internal().

◆ free_apic()

static void free_apic ( void * obj)
static

Definition at line 606 of file id3v2.c.

Referenced by read_apic().

◆ rstrip_spaces()

static void rstrip_spaces ( char * buf)
static

Definition at line 613 of file id3v2.c.

Referenced by read_apic().

◆ read_apic()

static void read_apic ( AVFormatContext * s,
AVIOContext * pb,
int taglen,
const char * tag,
ExtraMetaList * extra_meta,
int isv34 )
static

Definition at line 620 of file id3v2.c.

◆ free_chapter()

static void free_chapter ( void * obj)
static

Definition at line 709 of file id3v2.c.

Referenced by read_chapter().

◆ read_chapter()

static void read_chapter ( AVFormatContext * s,
AVIOContext * pb,
int len,
const char * ttag,
ExtraMetaList * extra_meta,
int isv34 )
static

Definition at line 716 of file id3v2.c.

◆ free_priv()

static void free_priv ( void * obj)
static

Definition at line 770 of file id3v2.c.

Referenced by read_priv().

◆ read_priv()

static void read_priv ( AVFormatContext * s,
AVIOContext * pb,
int taglen,
const char * tag,
ExtraMetaList * extra_meta,
int isv34 )
static

Definition at line 777 of file id3v2.c.

◆ get_extra_meta_func()

static const ID3v2EMFunc * get_extra_meta_func ( const char * tag,
int isv34 )
static

Get the corresponding ID3v2EMFunc struct for a tag.

Parameters
isv34Determines if v2.2 or v2.3/4 strings are used
Returns
A pointer to the ID3v2EMFunc struct if found, NULL otherwise.

Definition at line 833 of file id3v2.c.

Referenced by ff_id3v2_free_extra_meta(), and id3v2_parse().

◆ id3v2_parse()

static void id3v2_parse ( AVIOContext * pb,
AVDictionary ** metadata,
AVFormatContext * s,
int len,
uint8_t version,
uint8_t flags,
ExtraMetaList * extra_meta )
static

Definition at line 847 of file id3v2.c.

Referenced by id3v2_read_internal().

◆ id3v2_read_internal()

static void id3v2_read_internal ( AVIOContext * pb,
AVDictionary ** metadata,
AVFormatContext * s,
const char * magic,
ID3v2ExtraMeta ** extra_metap,
int64_t max_search_size )
static

Definition at line 1121 of file id3v2.c.

Referenced by ff_id3v2_read(), and ff_id3v2_read_dict().

◆ ff_id3v2_read_dict()

void ff_id3v2_read_dict ( AVFormatContext * s,
AVIOContext * pb,
AVDictionary ** metadata,
const char * magic,
ID3v2ExtraMeta ** extra_meta )

Read an ID3v2 tag into specified dictionary and retrieve supported extra metadata.

Parameters
sUsed for logging and for reading demuxer options, may be NULL
metadataParsed metadata is stored here
[out]extra_metaIf not NULL, extra metadata is parsed into a list of ID3v2ExtraMeta structs and *extra_meta points to the head of the list

Definition at line 1174 of file id3v2.c.

Referenced by avformat_open_input(), handle_id3(), parse_id3(), and timed_id3_update_metadata().

◆ ff_id3v2_read()

void ff_id3v2_read ( AVFormatContext * s,
const char * magic,
ID3v2ExtraMeta ** extra_meta,
unsigned int max_search_size )

Read an ID3v2 tag, including supported extra metadata.

Data is read from and stored to AVFormatContext.

Parameters
[out]extra_metaIf not NULL, extra metadata is parsed into a list of ID3v2ExtraMeta structs and *extra_meta points to the head of the list
[opt]max_search_search restrict ID3 magic number search (bytes from start)

Definition at line 1180 of file id3v2.c.

Referenced by aiff_read_header(), bonk_read_header(), get_id3_tag(), main(), oma_read_header(), parse_dsd_prop(), and read_id3().

◆ ff_id3v2_free_extra_meta()

void ff_id3v2_free_extra_meta ( ID3v2ExtraMeta ** extra_meta)

Free memory allocated parsing special (non-text) metadata.

Parameters
extra_metaPointer to a pointer to the head of a ID3v2ExtraMeta list, *extra_meta is set to NULL.

Definition at line 1186 of file id3v2.c.

Referenced by aiff_read_header(), avformat_open_input(), bonk_read_header(), free_playlist_list(), get_id3_tag(), handle_id3(), handle_id3(), hls_read_header(), main(), oma_read_header(), parse_dsd_prop(), read_id3(), and timed_id3_update_metadata().

◆ ff_id3v2_parse_apic()

int ff_id3v2_parse_apic ( AVFormatContext * s,
ID3v2ExtraMeta * extra_meta )

Create a stream for each APIC (attached picture) extracted from the ID3v2 header.

Definition at line 1202 of file id3v2.c.

Referenced by aiff_read_header(), avformat_open_input(), bonk_read_header(), get_id3_tag(), handle_id3(), hls_read_header(), parse_dsd_prop(), and read_id3().

◆ ff_id3v2_parse_chapters()

int ff_id3v2_parse_chapters ( AVFormatContext * s,
ID3v2ExtraMeta * cur )

Create chapters for all CHAP tags found in the ID3v2 header.

Definition at line 1233 of file id3v2.c.

Referenced by aiff_read_header(), avformat_open_input(), get_id3_tag(), oma_read_header(), parse_dsd_prop(), and read_id3().

◆ ff_id3v2_parse_priv_dict()

int ff_id3v2_parse_priv_dict ( AVDictionary ** d,
ID3v2ExtraMeta * extra_meta )

Parse PRIV tags into a dictionary.

The PRIV owner is the metadata key. The PRIV data is the value, with non-printable characters escaped.

Definition at line 1258 of file id3v2.c.

Referenced by ff_id3v2_parse_priv(), handle_id3(), handle_id3(), and timed_id3_update_metadata().

◆ ff_id3v2_parse_priv()

int ff_id3v2_parse_priv ( AVFormatContext * s,
ID3v2ExtraMeta * extra_meta )

Add metadata for all PRIV tags in the ID3v2 header.

The PRIV owner is the metadata key. The PRIV data is the value, with non-printable characters escaped.

Definition at line 1298 of file id3v2.c.

Referenced by avformat_open_input(), bonk_read_header(), and hls_read_header().

Variable Documentation

◆ ff_id3v2_34_metadata_conv

const AVMetadataConv ff_id3v2_34_metadata_conv[]
Initial value:
= {
{ "COMM", "comment" },
{ "TALB", "album" },
{ "TCOM", "composer" },
{ "TCON", "genre" },
{ "TCOP", "copyright" },
{ "TENC", "encoded_by" },
{ "TIT2", "title" },
{ "TLAN", "language" },
{ "TPE1", "artist" },
{ "TPE2", "album_artist" },
{ "TPE3", "performer" },
{ "TPOS", "disc" },
{ "TPUB", "publisher" },
{ "TRCK", "track" },
{ "TSSE", "encoder" },
{ "USLT", "lyrics" },
{ 0 }
}

Definition at line 49 of file id3v2.c.

Referenced by id3v2_read_internal(), read_chapter(), and write_metadata().

◆ ff_id3v2_4_metadata_conv

const AVMetadataConv ff_id3v2_4_metadata_conv[]
Initial value:
= {
{ "TCMP", "compilation" },
{ "TDRC", "date" },
{ "TDRL", "date" },
{ "TDEN", "creation_time" },
{ "TSOA", "album-sort" },
{ "TSOP", "artist-sort" },
{ "TSOT", "title-sort" },
{ "TSST", "disc_subtitle" },
{ "TIT1", "grouping" },
{ 0 }
}

Definition at line 69 of file id3v2.c.

Referenced by id3v2_read_internal(), read_chapter(), and write_metadata().

◆ id3v2_2_metadata_conv

const AVMetadataConv id3v2_2_metadata_conv[]
static
Initial value:
= {
{ "TAL", "album" },
{ "TCO", "genre" },
{ "TCP", "compilation" },
{ "TT2", "title" },
{ "TEN", "encoded_by" },
{ "TP1", "artist" },
{ "TP2", "album_artist" },
{ "TP3", "performer" },
{ "TRK", "track" },
{ 0 }
}

Definition at line 82 of file id3v2.c.

Referenced by id3v2_read_internal().

◆ ff_id3v2_tags

attribute_nonstring const char ff_id3v2_tags[][4]
Initial value:
= {
"TALB", "TBPM", "TCOM", "TCON", "TCOP", "TDLY", "TENC", "TEXT",
"TFLT", "TIT1", "TIT2", "TIT3", "TKEY", "TLAN", "TLEN", "TMED",
"TOAL", "TOFN", "TOLY", "TOPE", "TOWN", "TPE1", "TPE2", "TPE3",
"TPE4", "TPOS", "TPUB", "TRCK", "TRSN", "TRSO", "TSRC", "TSSE",
{ 0 },
}

A list of text information frames allowed in both ID3 v2.3 and v2.4 http://www.id3.org/id3v2.4.0-frames http://www.id3.org/id3v2.4.0-changes.

Definition at line 95 of file id3v2.c.

Referenced by write_metadata().

◆ ff_id3v2_4_tags

attribute_nonstring const char ff_id3v2_4_tags[][4]
Initial value:
= {
"TDEN", "TDOR", "TDRC", "TDRL", "TDTG", "TIPL", "TMCL", "TMOO",
"TPRO", "TSOA", "TSOP", "TSOT", "TSST",
{ 0 },
}

ID3v2.4-only text information frames.

Definition at line 103 of file id3v2.c.

Referenced by write_metadata().

◆ ff_id3v2_3_tags

attribute_nonstring const char ff_id3v2_3_tags[][4]
Initial value:
= {
"TDAT", "TIME", "TORY", "TRDA", "TSIZ", "TYER",
{ 0 },
}

ID3v2.3-only text information frames.

Definition at line 109 of file id3v2.c.

Referenced by write_metadata().

◆ ff_id3v2_picture_types

const char* const ff_id3v2_picture_types[21]
Initial value:
= {
"Other",
"32x32 pixels 'file icon'",
"Other file icon",
"Cover (front)",
"Cover (back)",
"Leaflet page",
"Media (e.g. label side of CD)",
"Lead artist/lead performer/soloist",
"Artist/performer",
"Conductor",
"Band/Orchestra",
"Composer",
"Lyricist/text writer",
"Recording Location",
"During recording",
"During performance",
"Movie/video screen capture",
"A bright coloured fish",
"Illustration",
"Band/artist logotype",
"Publisher/Studio logotype",
}

Definition at line 114 of file id3v2.c.

Referenced by asf_read_picture(), ff_flac_parse_picture(), ff_id3v2_write_apic(), flac_write_picture(), and read_apic().

◆ ff_id3v2_mime_tags

const CodecMime ff_id3v2_mime_tags[]
Initial value:
= {
{ "image/gif", AV_CODEC_ID_GIF },
{ "image/jpeg", AV_CODEC_ID_MJPEG },
{ "image/jpg", AV_CODEC_ID_MJPEG },
{ "image/jxl", AV_CODEC_ID_JPEGXL },
{ "image/png", AV_CODEC_ID_PNG },
{ "image/tiff", AV_CODEC_ID_TIFF },
{ "image/bmp", AV_CODEC_ID_BMP },
{ "image/webp", AV_CODEC_ID_WEBP },
{ "JPG", AV_CODEC_ID_MJPEG },
{ "PNG", AV_CODEC_ID_PNG },
}
@ AV_CODEC_ID_PNG
Definition codec_id.h:111
@ AV_CODEC_ID_GIF
Definition codec_id.h:147
@ AV_CODEC_ID_NONE
Definition codec_id.h:48
@ AV_CODEC_ID_JPEGXL
Definition codec_id.h:308
@ AV_CODEC_ID_TIFF
Definition codec_id.h:146
@ AV_CODEC_ID_BMP
Definition codec_id.h:128
@ AV_CODEC_ID_WEBP
Definition codec_id.h:221
@ AV_CODEC_ID_MJPEG
Definition codec_id.h:57

Definition at line 138 of file id3v2.c.

Referenced by asf_read_picture(), ff_flac_parse_picture(), ff_id3v2_write_apic(), flac_write_picture(), query_codec(), and read_apic().

◆ id3v2_extra_meta_funcs

const ID3v2EMFunc id3v2_extra_meta_funcs[]
static
Initial value:
= {
{ "GEO", "GEOB", read_geobtag, free_geobtag },
{ "PIC", "APIC", read_apic, free_apic },
{ "CHAP","CHAP", read_chapter, free_chapter },
{ "PRIV","PRIV", read_priv, free_priv },
{ NULL }
}
#define NULL
Definition coverity.c:32
static AVChapter * read_chapter(AVFormatContext *s)
Definition ffmetadec.c:100
static void free_apic(void *obj)
Definition id3v2.c:606
static void free_priv(void *obj)
Definition id3v2.c:770
static void read_priv(AVFormatContext *s, AVIOContext *pb, int taglen, const char *tag, ExtraMetaList *extra_meta, int isv34)
Definition id3v2.c:777
static void free_geobtag(void *obj)
Free GEOB type extra metadata.
Definition id3v2.c:230
static void read_geobtag(AVFormatContext *s, AVIOContext *pb, int taglen, const char *tag, ExtraMetaList *extra_meta, int isv34)
Parse GEOB tag into a ID3v2ExtraMetaGEOB struct.
Definition id3v2.c:489
static void free_chapter(void *obj)
Definition id3v2.c:709
static void read_apic(AVFormatContext *s, AVIOContext *pb, int taglen, const char *tag, ExtraMetaList *extra_meta, int isv34)
Definition id3v2.c:620

Definition at line 820 of file id3v2.c.

Referenced by get_extra_meta_func().