FFmpeg
asf.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2000, 2001 Fabrice Bellard
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg 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  * FFmpeg 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 FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include "asf.h"
22 #include "demux.h"
23 #include "id3v2.h"
24 #include "internal.h"
25 
26 /* List of official tags at http://msdn.microsoft.com/en-us/library/dd743066(VS.85).aspx */
28  { "WM/AlbumArtist", "album_artist" },
29  { "WM/AlbumTitle", "album" },
30  { "Author", "artist" },
31  { "Description", "comment" },
32  { "WM/Composer", "composer" },
33  { "WM/EncodedBy", "encoded_by" },
34  { "WM/EncodingSettings", "encoder" },
35  { "WM/Genre", "genre" },
36  { "WM/Language", "language" },
37  { "WM/OriginalFilename", "filename" },
38  { "WM/PartOfSet", "disc" },
39  { "WM/Publisher", "publisher" },
40  { "WM/Tool", "encoder" },
41  { "WM/TrackNumber", "track" },
42  { "WM/MediaStationCallSign", "service_provider" },
43  { "WM/MediaStationName", "service_name" },
44 // { "Year" , "date" }, TODO: conversion year<->date
45  { 0 }
46 };
47 
48 /* MSDN claims that this should be "compatible with the ID3 frame, APIC",
49  * but in reality this is only loosely similar */
51 {
52  const CodecMime *mime = ff_id3v2_mime_tags;
53  enum AVCodecID id = AV_CODEC_ID_NONE;
54  char mimetype[64];
55  uint8_t *desc = NULL;
56  AVStream *st = NULL;
57  int ret, type, picsize, desc_len;
58 
59  /* type + picsize + mime + desc */
60  if (len < 1 + 4 + 2 + 2) {
61  av_log(s, AV_LOG_ERROR, "Invalid attached picture size: %d.\n", len);
62  return AVERROR_INVALIDDATA;
63  }
64 
65  /* picture type */
66  type = avio_r8(s->pb);
67  len--;
69  av_log(s, AV_LOG_WARNING, "Unknown attached picture type: %d.\n", type);
70  type = 0;
71  }
72 
73  /* picture data size */
74  picsize = avio_rl32(s->pb);
75  len -= 4;
76 
77  /* picture MIME type */
78  len -= avio_get_str16le(s->pb, len, mimetype, sizeof(mimetype));
79  while (mime->id != AV_CODEC_ID_NONE) {
80  if (!strncmp(mime->str, mimetype, sizeof(mimetype))) {
81  id = mime->id;
82  break;
83  }
84  mime++;
85  }
86  if (id == AV_CODEC_ID_NONE) {
87  av_log(s, AV_LOG_ERROR, "Unknown attached picture mimetype: %s.\n",
88  mimetype);
89  return 0;
90  }
91 
92  if (picsize >= len) {
93  av_log(s, AV_LOG_ERROR, "Invalid attached picture data size: %d >= %d.\n",
94  picsize, len);
95  return AVERROR_INVALIDDATA;
96  }
97 
98  /* picture description */
99  desc_len = (len - picsize) * 2 + 1;
100  desc = av_malloc(desc_len);
101  if (!desc)
102  return AVERROR(ENOMEM);
103  len -= avio_get_str16le(s->pb, len - picsize, desc, desc_len);
104 
105  ret = ff_add_attached_pic(s, NULL, s->pb, NULL, picsize);
106  if (ret < 0)
107  goto fail;
108  st = s->streams[s->nb_streams - 1];
109 
110  st->codecpar->codec_id = id;
111 
112  if (*desc) {
113  if (av_dict_set(&st->metadata, "title", desc, AV_DICT_DONT_STRDUP_VAL) < 0)
114  av_log(s, AV_LOG_WARNING, "av_dict_set failed.\n");
115  } else
116  av_freep(&desc);
117 
118  if (av_dict_set(&st->metadata, "comment", ff_id3v2_picture_types[type], 0) < 0)
119  av_log(s, AV_LOG_WARNING, "av_dict_set failed.\n");
120 
121  return 0;
122 
123 fail:
124  av_freep(&desc);
125  return ret;
126 }
127 
128 static int get_id3_tag(AVFormatContext *s, int len)
129 {
130  ID3v2ExtraMeta *id3v2_extra_meta;
131 
132  ff_id3v2_read(s, ID3v2_DEFAULT_MAGIC, &id3v2_extra_meta, len);
133  if (id3v2_extra_meta) {
134  ff_id3v2_parse_apic(s, id3v2_extra_meta);
135  ff_id3v2_parse_chapters(s, id3v2_extra_meta);
136  ff_id3v2_free_extra_meta(&id3v2_extra_meta);
137  }
138  return 0;
139 }
140 
142  int val_len)
143 {
144  if (!strcmp(name, "WM/Picture")) // handle cover art
145  return asf_read_picture(s, val_len);
146  else if (!strcmp(name, "ID3")) // handle ID3 tag
147  return get_id3_tag(s, val_len);
148 
149  return 1;
150 }
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
name
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default minimum maximum flags name is the option name
Definition: writing_filters.txt:88
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
id3v2.h
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.
Definition: id3v2.c:1139
AVMetadataConv
Definition: metadata.h:34
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:30
CodecMime
Definition: internal.h:47
fail
#define fail()
Definition: checkasm.h:179
type
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf type
Definition: writing_filters.txt:86
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: id3v2.c:1192
AV_DICT_DONT_STRDUP_VAL
#define AV_DICT_DONT_STRDUP_VAL
Take ownership of a value that's been allocated with av_malloc() or another memory allocation functio...
Definition: dict.h:79
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
ID3v2ExtraMeta
Definition: id3v2.h:84
avio_get_str16le
int avio_get_str16le(AVIOContext *pb, int maxlen, char *buf, int buflen)
Read a UTF-16 string from pb and convert it to UTF-8.
ff_add_attached_pic
int ff_add_attached_pic(AVFormatContext *s, AVStream *st, AVIOContext *pb, AVBufferRef **buf, int size)
Add an attached pic to an AVStream.
Definition: demux_utils.c:116
s
#define s(width, name)
Definition: cbs_vp9.c:198
AVFormatContext
Format I/O context.
Definition: avformat.h:1255
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:766
NULL
#define NULL
Definition: coverity.c:32
ff_asf_metadata_conv
const AVMetadataConv ff_asf_metadata_conv[]
Definition: asf.c:27
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: id3v2.c:1161
AVStream::metadata
AVDictionary * metadata
Definition: avformat.h:823
ff_id3v2_picture_types
const char *const ff_id3v2_picture_types[21]
Definition: id3v2.c:108
ff_asf_handle_byte_array
int ff_asf_handle_byte_array(AVFormatContext *s, const char *name, int val_len)
Handles both attached pictures as well as id3 tags.
Definition: asf.c:141
ff_id3v2_mime_tags
const CodecMime ff_id3v2_mime_tags[]
Definition: id3v2.c:132
AVCodecID
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: codec_id.h:49
avio_rl32
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:729
ID3v2_DEFAULT_MAGIC
#define ID3v2_DEFAULT_MAGIC
Default magic bytes for ID3v2 header: "ID3".
Definition: id3v2.h:35
avio_r8
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:602
AV_CODEC_ID_NONE
@ AV_CODEC_ID_NONE
Definition: codec_id.h:50
demux.h
len
int len
Definition: vorbis_enc_data.h:426
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:743
id
enum AVCodecID id
Definition: dts2pts.c:364
asf.h
asf_read_picture
static int asf_read_picture(AVFormatContext *s, int len)
Definition: asf.c:50
CodecMime::str
char str[32]
Definition: internal.h:48
get_id3_tag
static int get_id3_tag(AVFormatContext *s, int len)
Definition: asf.c:128
CodecMime::id
enum AVCodecID id
Definition: internal.h:49
desc
const char * desc
Definition: libsvtav1.c:75
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:55
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
av_dict_set
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:88
ff_id3v2_free_extra_meta
void ff_id3v2_free_extra_meta(ID3v2ExtraMeta **extra_meta)
Free memory allocated parsing special (non-text) metadata.
Definition: id3v2.c:1145
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61