FFmpeg
Loading...
Searching...
No Matches
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 /* SetSubTitle can be found in this mapping:
41 * https://learn.microsoft.com/en-gb/windows/win32/wmformat/id3-tag-support */
42 { "WM/SetSubTitle", "disc_subtitle" },
43 { "WM/Publisher", "publisher" },
44 { "WM/Tool", "encoder" },
45 { "WM/TrackNumber", "track" },
46 { "WM/MediaStationCallSign", "service_provider" },
47 { "WM/MediaStationName", "service_name" },
48// { "Year" , "date" }, TODO: conversion year<->date
49 { 0 }
50};
51
52/* MSDN claims that this should be "compatible with the ID3 frame, APIC",
53 * but in reality this is only loosely similar */
55{
56 const CodecMime *mime = ff_id3v2_mime_tags;
58 char mimetype[64];
59 uint8_t *desc = NULL;
60 AVStream *st = NULL;
61 int ret, type, picsize, desc_len;
62
63 /* type + picsize + mime + desc */
64 if (len < 1 + 4 + 2 + 2) {
65 av_log(s, AV_LOG_ERROR, "Invalid attached picture size: %d.\n", len);
67 }
68
69 /* picture type */
70 type = avio_r8(s->pb);
71 len--;
73 av_log(s, AV_LOG_WARNING, "Unknown attached picture type: %d.\n", type);
74 type = 0;
75 }
76
77 /* picture data size */
78 picsize = avio_rl32(s->pb);
79 len -= 4;
80
81 /* picture MIME type */
82 len -= avio_get_str16le(s->pb, len, mimetype, sizeof(mimetype));
83 while (mime->id != AV_CODEC_ID_NONE) {
84 if (!strncmp(mime->str, mimetype, sizeof(mimetype))) {
85 id = mime->id;
86 break;
87 }
88 mime++;
89 }
90 if (id == AV_CODEC_ID_NONE) {
91 av_log(s, AV_LOG_ERROR, "Unknown attached picture mimetype: %s.\n",
92 mimetype);
93 return 0;
94 }
95
96 if (picsize >= len || ((int64_t)len - picsize) * 2 + 1 > INT_MAX) {
97 av_log(s, AV_LOG_ERROR, "Invalid attached picture data size: %d (len = %d).\n",
98 picsize, len);
100 }
101
102 /* picture description */
103 desc_len = (len - picsize) * 2 + 1;
104 desc = av_malloc(desc_len);
105 if (!desc)
106 return AVERROR(ENOMEM);
107 len -= avio_get_str16le(s->pb, len - picsize, desc, desc_len);
108
109 ret = ff_add_attached_pic(s, NULL, s->pb, NULL, picsize);
110 if (ret < 0)
111 goto fail;
112 st = s->streams[s->nb_streams - 1];
113
114 st->codecpar->codec_id = id;
115
116 if (*desc) {
117 if (av_dict_set(&st->metadata, "title", desc, AV_DICT_DONT_STRDUP_VAL) < 0)
118 av_log(s, AV_LOG_WARNING, "av_dict_set failed.\n");
119 } else
120 av_freep(&desc);
121
122 if (av_dict_set(&st->metadata, "comment", ff_id3v2_picture_types[type], 0) < 0)
123 av_log(s, AV_LOG_WARNING, "av_dict_set failed.\n");
124
125 return 0;
126
127fail:
128 av_freep(&desc);
129 return ret;
130}
131
133{
134 ID3v2ExtraMeta *id3v2_extra_meta;
135
136 ff_id3v2_read(s, ID3v2_DEFAULT_MAGIC, &id3v2_extra_meta, len);
137 if (id3v2_extra_meta) {
138 ff_id3v2_parse_apic(s, id3v2_extra_meta);
139 ff_id3v2_parse_chapters(s, id3v2_extra_meta);
140 ff_id3v2_free_extra_meta(&id3v2_extra_meta);
141 }
142 return 0;
143}
144
146 int val_len)
147{
148 if (!strcmp(name, "WM/Picture")) // handle cover art
149 return asf_read_picture(s, val_len);
150 else if (!strcmp(name, "ID3")) // handle ID3 tag
151 return get_id3_tag(s, val_len);
152
153 return 1;
154}
static int asf_read_picture(AVFormatContext *s, int len)
Definition asf.c:54
const AVMetadataConv ff_asf_metadata_conv[]
Definition asf.c:28
static int get_id3_tag(AVFormatContext *s, int len)
Definition asf.c:132
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:145
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.
unsigned int avio_rl32(AVIOContext *s)
Definition aviobuf.c:733
int avio_r8(AVIOContext *s)
Definition aviobuf.c:606
#define s(width, name)
Definition cbs_vp9.c:198
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
int ff_add_attached_pic(AVFormatContext *s, AVStream *st, AVIOContext *pb, AVBufferRef **buf, int size)
Add an attached pic to an AVStream.
enum AVCodecID id
Definition dts2pts.c:607
#define fail
Definition test.h:479
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition codec_id.h:47
@ AV_CODEC_ID_NONE
Definition codec_id.h:48
#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
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:86
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
cl_device_type type
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:1180
const char *const ff_id3v2_picture_types[21]
Definition id3v2.c:114
void ff_id3v2_free_extra_meta(ID3v2ExtraMeta **extra_meta)
Free memory allocated parsing special (non-text) metadata.
Definition id3v2.c:1186
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:1202
int ff_id3v2_parse_chapters(AVFormatContext *s, ID3v2ExtraMeta *cur)
Create chapters for all CHAP tags found in the ID3v2 header.
Definition id3v2.c:1233
const CodecMime ff_id3v2_mime_tags[]
Definition id3v2.c:138
#define ID3v2_DEFAULT_MAGIC
Default magic bytes for ID3v2 header: "ID3".
Definition id3v2.h:35
const char * desc
Definition libsvtav1.c:83
Memory handling functions.
#define av_malloc(s)
Definition ops_static.c:52
const char * name
Definition qsvenc.c:142
#define FF_ARRAY_ELEMS(a)
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition codec_par.h:57
Format I/O context.
Definition avformat.h:1333
Stream structure.
Definition avformat.h:766
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:789
AVDictionary * metadata
Definition avformat.h:846
char str[32]
Definition internal.h:48
enum AVCodecID id
Definition internal.h:49
#define av_freep(p)
#define av_log(a,...)
int len