FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/asf.c
Date: 2026-08-31 23:16:59
Exec Total Coverage
Lines: 45 60 75.0%
Functions: 3 3 100.0%
Branches: 20 32 62.5%

Line Branch Exec Source
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 */
28 const AVMetadataConv ff_asf_metadata_conv[] = {
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 */
54 3 static int asf_read_picture(AVFormatContext *s, int len)
55 {
56 3 const CodecMime *mime = ff_id3v2_mime_tags;
57 3 enum AVCodecID id = AV_CODEC_ID_NONE;
58 char mimetype[64];
59 3 uint8_t *desc = NULL;
60 3 AVStream *st = NULL;
61 int ret, type, picsize, desc_len;
62
63 /* type + picsize + mime + desc */
64
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (len < 1 + 4 + 2 + 2) {
65 av_log(s, AV_LOG_ERROR, "Invalid attached picture size: %d.\n", len);
66 return AVERROR_INVALIDDATA;
67 }
68
69 /* picture type */
70 3 type = avio_r8(s->pb);
71 3 len--;
72
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
3 if (type >= FF_ARRAY_ELEMS(ff_id3v2_picture_types) || type < 0) {
73 av_log(s, AV_LOG_WARNING, "Unknown attached picture type: %d.\n", type);
74 type = 0;
75 }
76
77 /* picture data size */
78 3 picsize = avio_rl32(s->pb);
79 3 len -= 4;
80
81 /* picture MIME type */
82 3 len -= avio_get_str16le(s->pb, len, mimetype, sizeof(mimetype));
83
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 while (mime->id != AV_CODEC_ID_NONE) {
84
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
6 if (!strncmp(mime->str, mimetype, sizeof(mimetype))) {
85 3 id = mime->id;
86 3 break;
87 }
88 3 mime++;
89 }
90
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 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
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
3 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);
99 return AVERROR_INVALIDDATA;
100 }
101
102 /* picture description */
103 3 desc_len = (len - picsize) * 2 + 1;
104 3 desc = av_malloc(desc_len);
105
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (!desc)
106 return AVERROR(ENOMEM);
107 3 len -= avio_get_str16le(s->pb, len - picsize, desc, desc_len);
108
109 3 ret = ff_add_attached_pic(s, NULL, s->pb, NULL, picsize);
110
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (ret < 0)
111 goto fail;
112 3 st = s->streams[s->nb_streams - 1];
113
114 3 st->codecpar->codec_id = id;
115
116
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 if (*desc) {
117
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 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 1 av_freep(&desc);
121
122
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 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 3 return 0;
126
127 fail:
128 av_freep(&desc);
129 return ret;
130 }
131
132 4 static int get_id3_tag(AVFormatContext *s, int len)
133 {
134 ID3v2ExtraMeta *id3v2_extra_meta;
135
136 4 ff_id3v2_read(s, ID3v2_DEFAULT_MAGIC, &id3v2_extra_meta, len);
137
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (id3v2_extra_meta) {
138 4 ff_id3v2_parse_apic(s, id3v2_extra_meta);
139 4 ff_id3v2_parse_chapters(s, id3v2_extra_meta);
140 4 ff_id3v2_free_extra_meta(&id3v2_extra_meta);
141 }
142 4 return 0;
143 }
144
145 22 int ff_asf_handle_byte_array(AVFormatContext *s, const char *name,
146 int val_len)
147 {
148
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 19 times.
22 if (!strcmp(name, "WM/Picture")) // handle cover art
149 3 return asf_read_picture(s, val_len);
150
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 15 times.
19 else if (!strcmp(name, "ID3")) // handle ID3 tag
151 4 return get_id3_tag(s, val_len);
152
153 15 return 1;
154 }
155