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 "libavutil/mem.h"
22 #include "asf.h"
23 #include "demux.h"
24 #include "id3v2.h"
25 #include "internal.h"
26 
27 /* List of official tags at http://msdn.microsoft.com/en-us/library/dd743066(VS.85).aspx */
29  { "WM/AlbumArtist", "album_artist" },
30  { "WM/AlbumTitle", "album" },
31  { "Author", "artist" },
32  { "Description", "comment" },
33  { "WM/Composer", "composer" },
34  { "WM/EncodedBy", "encoded_by" },
35  { "WM/EncodingSettings", "encoder" },
36  { "WM/Genre", "genre" },
37  { "WM/Language", "language" },
38  { "WM/OriginalFilename", "filename" },
39  { "WM/PartOfSet", "disc" },
40  { "WM/Publisher", "publisher" },
41  { "WM/Tool", "encoder" },
42  { "WM/TrackNumber", "track" },
43  { "WM/MediaStationCallSign", "service_provider" },
44  { "WM/MediaStationName", "service_name" },
45 // { "Year" , "date" }, TODO: conversion year<->date
46  { 0 }
47 };
48 
49 /* MSDN claims that this should be "compatible with the ID3 frame, APIC",
50  * but in reality this is only loosely similar */
52 {
53  const CodecMime *mime = ff_id3v2_mime_tags;
54  enum AVCodecID id = AV_CODEC_ID_NONE;
55  char mimetype[64];
56  uint8_t *desc = NULL;
57  AVStream *st = NULL;
58  int ret, type, picsize, desc_len;
59 
60  /* type + picsize + mime + desc */
61  if (len < 1 + 4 + 2 + 2) {
62  av_log(s, AV_LOG_ERROR, "Invalid attached picture size: %d.\n", len);
63  return AVERROR_INVALIDDATA;
64  }
65 
66  /* picture type */
67  type = avio_r8(s->pb);
68  len--;
70  av_log(s, AV_LOG_WARNING, "Unknown attached picture type: %d.\n", type);
71  type = 0;
72  }
73 
74  /* picture data size */
75  picsize = avio_rl32(s->pb);
76  len -= 4;
77 
78  /* picture MIME type */
79  len -= avio_get_str16le(s->pb, len, mimetype, sizeof(mimetype));
80  while (mime->id != AV_CODEC_ID_NONE) {
81  if (!strncmp(mime->str, mimetype, sizeof(mimetype))) {
82  id = mime->id;
83  break;
84  }
85  mime++;
86  }
87  if (id == AV_CODEC_ID_NONE) {
88  av_log(s, AV_LOG_ERROR, "Unknown attached picture mimetype: %s.\n",
89  mimetype);
90  return 0;
91  }
92 
93  if (picsize >= len) {
94  av_log(s, AV_LOG_ERROR, "Invalid attached picture data size: %d >= %d.\n",
95  picsize, len);
96  return AVERROR_INVALIDDATA;
97  }
98 
99  /* picture description */
100  desc_len = (len - picsize) * 2 + 1;
101  desc = av_malloc(desc_len);
102  if (!desc)
103  return AVERROR(ENOMEM);
104  len -= avio_get_str16le(s->pb, len - picsize, desc, desc_len);
105 
106  ret = ff_add_attached_pic(s, NULL, s->pb, NULL, picsize);
107  if (ret < 0)
108  goto fail;
109  st = s->streams[s->nb_streams - 1];
110 
111  st->codecpar->codec_id = id;
112 
113  if (*desc) {
114  if (av_dict_set(&st->metadata, "title", desc, AV_DICT_DONT_STRDUP_VAL) < 0)
115  av_log(s, AV_LOG_WARNING, "av_dict_set failed.\n");
116  } else
117  av_freep(&desc);
118 
119  if (av_dict_set(&st->metadata, "comment", ff_id3v2_picture_types[type], 0) < 0)
120  av_log(s, AV_LOG_WARNING, "av_dict_set failed.\n");
121 
122  return 0;
123 
124 fail:
125  av_freep(&desc);
126  return ret;
127 }
128 
129 static int get_id3_tag(AVFormatContext *s, int len)
130 {
131  ID3v2ExtraMeta *id3v2_extra_meta;
132 
133  ff_id3v2_read(s, ID3v2_DEFAULT_MAGIC, &id3v2_extra_meta, len);
134  if (id3v2_extra_meta) {
135  ff_id3v2_parse_apic(s, id3v2_extra_meta);
136  ff_id3v2_parse_chapters(s, id3v2_extra_meta);
137  ff_id3v2_free_extra_meta(&id3v2_extra_meta);
138  }
139  return 0;
140 }
141 
143  int val_len)
144 {
145  if (!strcmp(name, "WM/Picture")) // handle cover art
146  return asf_read_picture(s, val_len);
147  else if (!strcmp(name, "ID3")) // handle ID3 tag
148  return get_id3_tag(s, val_len);
149 
150  return 1;
151 }
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:1140
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:1193
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:28
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:1162
AVStream::metadata
AVDictionary * metadata
Definition: avformat.h:823
ff_id3v2_picture_types
const char *const ff_id3v2_picture_types[21]
Definition: id3v2.c:109
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:142
ff_id3v2_mime_tags
const CodecMime ff_id3v2_mime_tags[]
Definition: id3v2.c:133
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:730
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:603
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:365
asf.h
asf_read_picture
static int asf_read_picture(AVFormatContext *s, int len)
Definition: asf.c:51
CodecMime::str
char str[32]
Definition: internal.h:48
get_id3_tag
static int get_id3_tag(AVFormatContext *s, int len)
Definition: asf.c:129
CodecMime::id
enum AVCodecID id
Definition: internal.h:49
desc
const char * desc
Definition: libsvtav1.c:75
mem.h
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:1146
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