FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/hashenc.c
Date: 2026-10-14 20:10:01
Exec Total Coverage
Lines: 102 135 75.6%
Functions: 10 13 76.9%
Branches: 32 58 55.2%

Line Branch Exec Source
1 /*
2 * Hash/MD5 encoder (for codec/format testing)
3 * Copyright (c) 2009 Reimar Döffinger, based on crcenc (c) 2002 Fabrice Bellard
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include "config_components.h"
23
24 #include "libavutil/avstring.h"
25 #include "libavutil/hash.h"
26 #include "libavutil/intreadwrite.h"
27 #include "libavutil/mem.h"
28 #include "libavutil/opt.h"
29 #include "avformat.h"
30 #include "internal.h"
31 #include "mux.h"
32
33 struct HashContext {
34 const AVClass *avclass;
35 struct {
36 struct AVHashContext *hash;
37 int64_t stream_size;
38 } *hashes;
39 char *hash_name;
40 int per_stream;
41 int format_version;
42 };
43
44 #define OFFSET(x) offsetof(struct HashContext, x)
45 #define ENC AV_OPT_FLAG_ENCODING_PARAM
46 #define HASH_OPT(defaulttype) \
47 { "hash", "set hash to use", OFFSET(hash_name), AV_OPT_TYPE_STRING, {.str = defaulttype}, 0, 0, ENC }
48 #define FORMAT_VERSION_OPT \
49 { "format_version", "file format version", OFFSET(format_version), AV_OPT_TYPE_INT, {.i64 = 2}, 1, 2, ENC }
50
51 #if CONFIG_FRAMEHASH_MUXER | CONFIG_STREAMHASH_MUXER | CONFIG_HASH_MUXER
52 static const AVOption hash_options[] = {
53 FORMAT_VERSION_OPT,
54 HASH_OPT("sha256"),
55 { NULL },
56 };
57 #endif
58
59 #if CONFIG_MD5_MUXER
60 static const AVOption md5_options[] = {
61 HASH_OPT("md5"),
62 { NULL },
63 };
64 #endif
65
66 #if CONFIG_FRAMEMD5_MUXER
67 static const AVOption framemd5_options[] = {
68 HASH_OPT("md5"),
69 FORMAT_VERSION_OPT,
70 { NULL },
71 };
72 #endif
73
74 #if CONFIG_HASH_MUXER || CONFIG_MD5_MUXER
75 ✗ static int hash_init(struct AVFormatContext *s)
76 {
77 int res;
78 ✗ struct HashContext *c = s->priv_data;
79 ✗ c->per_stream = 0;
80 ✗ c->hashes = av_mallocz(sizeof(*c->hashes));
81 ✗ if (!c->hashes)
82 ✗ return AVERROR(ENOMEM);
83 ✗ res = av_hash_alloc(&c->hashes->hash, c->hash_name);
84 ✗ if (res < 0)
85 ✗ return res;
86 ✗ av_hash_init(c->hashes->hash);
87 ✗ return 0;
88 }
89
90 ✗ static int hash_write_packet(struct AVFormatContext *s, AVPacket *pkt)
91 {
92 ✗ struct HashContext *c = s->priv_data;
93 ✗ av_hash_update(c->hashes->hash, pkt->data, pkt->size);
94 ✗ return 0;
95 }
96 #endif
97
98 #if CONFIG_STREAMHASH_MUXER || CONFIG_FRAMEHASH_MUXER || CONFIG_FRAMEMD5_MUXER
99 312 static void hash_print_extradata(struct AVFormatContext *s)
100 {
101 int i;
102
103
2/2
✓ Branch 0 taken 316 times.
✓ Branch 1 taken 312 times.
628 for (i = 0; i < s->nb_streams; i++) {
104 316 const AVStream *st = s->streams[i];
105 316 const AVCodecParameters *par = st->codecpar;
106
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 298 times.
316 if (par->extradata) {
107 18 struct HashContext *c = s->priv_data;
108 char buf[AV_HASH_MAX_SIZE*2+1];
109
110 18 av_hash_init(c->hashes->hash);
111 18 av_hash_update(c->hashes->hash, par->extradata, par->extradata_size);
112 18 av_hash_final_hex(c->hashes->hash, buf, sizeof(buf));
113 18 avio_printf(s->pb, "#extradata %d, %31d, %s\n", i, par->extradata_size, buf);
114 }
115 }
116 312 }
117 #endif
118
119 2 static void hash_print_sidedata(struct AVFormatContext *s,
120 const AVPacketSideData *sd, int side_data_elems)
121 {
122 2 struct HashContext *c = s->priv_data;
123 char buf[AV_HASH_MAX_SIZE*2+128];
124
125 2 avio_printf(s->pb, ", S=%d", side_data_elems);
126
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 for (int i = 0; i < side_data_elems; i++) {
127 2 av_hash_init(c->hashes->hash);
128 if (HAVE_BIGENDIAN && sd[i].type == AV_PKT_DATA_PALETTE) {
129 for (size_t j = 0; j < sd[i].size; j += sizeof(uint32_t)) {
130 uint32_t data = AV_RL32(sd[i].data + j);
131 av_hash_update(c->hashes->hash, (uint8_t *)&data, sizeof(uint32_t));
132 }
133
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 } else if (sd[i].type != AV_PKT_DATA_IAMF_MIX_GAIN_PARAM &&
134
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 sd[i].type != AV_PKT_DATA_IAMF_DEMIXING_INFO_PARAM &&
135
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 sd[i].type != AV_PKT_DATA_IAMF_RECON_GAIN_INFO_PARAM)
136 2 av_hash_update(c->hashes->hash, sd[i].data, sd[i].size);
137 2 snprintf(buf, sizeof(buf) - (AV_HASH_MAX_SIZE * 2 + 1),
138 2 ", %8zu, ", sd[i].size);
139 2 size_t len = strlen(buf);
140 2 av_hash_final_hex(c->hashes->hash, buf + len, sizeof(buf) - len);
141 2 avio_write(s->pb, buf, strlen(buf));
142 }
143 2 }
144
145 #if CONFIG_HASH_MUXER || CONFIG_MD5_MUXER || CONFIG_STREAMHASH_MUXER
146 ✗ static char get_media_type_char(enum AVMediaType type)
147 {
148 ✗ switch (type) {
149 ✗ case AVMEDIA_TYPE_VIDEO: return 'v';
150 ✗ case AVMEDIA_TYPE_AUDIO: return 'a';
151 ✗ case AVMEDIA_TYPE_DATA: return 'd';
152 ✗ case AVMEDIA_TYPE_SUBTITLE: return 's';
153 ✗ case AVMEDIA_TYPE_ATTACHMENT: return 't';
154 ✗ default: return '?';
155 }
156 }
157
158 4 static int hash_write_trailer(struct AVFormatContext *s)
159 {
160 4 struct HashContext *c = s->priv_data;
161
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 int num_hashes = c->per_stream ? s->nb_streams : 1;
162
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 4 times.
9 for (int i = 0; i < num_hashes; i++) {
163 char buf[AV_HASH_MAX_SIZE*2+128];
164
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 if (c->per_stream) {
165
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (c->format_version < 2) {
166 ✗ AVStream *st = s->streams[i];
167 ✗ snprintf(buf, sizeof(buf) - 200, "%d,%c,%s=", i, get_media_type_char(st->codecpar->codec_type),
168 ✗ av_hash_get_name(c->hashes[i].hash));
169 } else
170 5 snprintf(buf, sizeof(buf) - 200, "%d, %11"PRId64", ", i, c->hashes[i].stream_size);
171 } else {
172 ✗ snprintf(buf, sizeof(buf) - 200, "%s=", av_hash_get_name(c->hashes[i].hash));
173 }
174 5 av_hash_final_hex(c->hashes[i].hash, buf + strlen(buf), sizeof(buf) - strlen(buf));
175 5 avio_write(s->pb, buf, strlen(buf));
176
177
2/4
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
5 if (c->per_stream && c->format_version >= 2) {
178 5 const AVStream *st = s->streams[i];
179
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (st->codecpar->nb_coded_side_data)
180 ✗ hash_print_sidedata(s, st->codecpar->coded_side_data, st->codecpar->nb_coded_side_data);
181 }
182
183 5 avio_printf(s->pb, "\n");
184 }
185
186 4 return 0;
187 }
188 #endif
189
190 312 static void hash_free(struct AVFormatContext *s)
191 {
192 312 struct HashContext *c = s->priv_data;
193
1/2
✓ Branch 0 taken 312 times.
✗ Branch 1 not taken.
312 if (c->hashes) {
194
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 308 times.
312 int num_hashes = c->per_stream ? s->nb_streams : 1;
195
2/2
✓ Branch 0 taken 313 times.
✓ Branch 1 taken 312 times.
625 for (int i = 0; i < num_hashes; i++) {
196 313 av_hash_freep(&c->hashes[i].hash);
197 }
198 }
199 312 av_freep(&c->hashes);
200 312 }
201
202 #if CONFIG_HASH_MUXER
203 static const AVClass hash_hashenc_class = {
204 .class_name = "hash muxer",
205 .item_name = av_default_item_name,
206 .option = &hash_options[1],
207 .version = LIBAVUTIL_VERSION_INT,
208 };
209
210 const FFOutputFormat ff_hash_muxer = {
211 .p.name = "hash",
212 .p.long_name = NULL_IF_CONFIG_SMALL("Hash testing"),
213 .priv_data_size = sizeof(struct HashContext),
214 .p.audio_codec = AV_CODEC_ID_PCM_S16LE,
215 .p.video_codec = AV_CODEC_ID_RAWVIDEO,
216 .init = hash_init,
217 .write_packet = hash_write_packet,
218 .write_trailer = hash_write_trailer,
219 .deinit = hash_free,
220 .p.flags = AVFMT_VARIABLE_FPS | AVFMT_TS_NONSTRICT |
221 AVFMT_TS_NEGATIVE,
222 .p.priv_class = &hash_hashenc_class,
223 };
224 #endif
225
226 #if CONFIG_MD5_MUXER
227 static const AVClass md5enc_class = {
228 .class_name = "MD5 muxer",
229 .item_name = av_default_item_name,
230 .option = md5_options,
231 .version = LIBAVUTIL_VERSION_INT,
232 };
233
234 const FFOutputFormat ff_md5_muxer = {
235 .p.name = "md5",
236 .p.long_name = NULL_IF_CONFIG_SMALL("MD5 testing"),
237 .priv_data_size = sizeof(struct HashContext),
238 .p.audio_codec = AV_CODEC_ID_PCM_S16LE,
239 .p.video_codec = AV_CODEC_ID_RAWVIDEO,
240 .init = hash_init,
241 .write_packet = hash_write_packet,
242 .write_trailer = hash_write_trailer,
243 .deinit = hash_free,
244 .p.flags = AVFMT_VARIABLE_FPS | AVFMT_TS_NONSTRICT |
245 AVFMT_TS_NEGATIVE,
246 .p.priv_class = &md5enc_class,
247 };
248 #endif
249
250 #if CONFIG_STREAMHASH_MUXER
251 4 static int streamhash_init(struct AVFormatContext *s)
252 {
253 int res, i;
254 4 struct HashContext *c = s->priv_data;
255 4 c->per_stream = 1;
256 4 c->hashes = av_calloc(s->nb_streams, sizeof(*c->hashes));
257
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (!c->hashes)
258 ✗ return AVERROR(ENOMEM);
259
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 4 times.
9 for (i = 0; i < s->nb_streams; i++) {
260 5 res = av_hash_alloc(&c->hashes[i].hash, c->hash_name);
261
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (res < 0) {
262 ✗ return res;
263 }
264 5 av_hash_init(c->hashes[i].hash);
265 }
266 4 return 0;
267 }
268
269 4 static int streamhash_write_header(struct AVFormatContext *s)
270 {
271 4 struct HashContext *c = s->priv_data;
272
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (c->format_version < 2)
273 ✗ return 0;
274 4 avio_printf(s->pb, "#format: stream checksums\n"
275 "#version: %d\n"
276 4 "#hash: %s\n", c->format_version, av_hash_get_name(c->hashes->hash));
277 4 hash_print_extradata(s);
278 4 ff_framehash_write_header(s);
279 4 avio_printf(s->pb, "#stream#, size, hash\n");
280 4 av_hash_init(c->hashes->hash);
281 4 return 0;
282 }
283
284 2866 static int streamhash_write_packet(struct AVFormatContext *s, AVPacket *pkt)
285 {
286 2866 struct HashContext *c = s->priv_data;
287 2866 av_hash_update(c->hashes[pkt->stream_index].hash, pkt->data, pkt->size);
288 2866 c->hashes[pkt->stream_index].stream_size += pkt->size;
289 2866 return 0;
290 }
291
292 static const AVClass streamhashenc_class = {
293 .class_name = "stream hash muxer",
294 .item_name = av_default_item_name,
295 .option = hash_options,
296 .version = LIBAVUTIL_VERSION_INT,
297 };
298
299 const FFOutputFormat ff_streamhash_muxer = {
300 .p.name = "streamhash",
301 .p.long_name = NULL_IF_CONFIG_SMALL("Per-stream hash testing"),
302 .priv_data_size = sizeof(struct HashContext),
303 .p.audio_codec = AV_CODEC_ID_PCM_S16LE,
304 .p.video_codec = AV_CODEC_ID_RAWVIDEO,
305 .init = streamhash_init,
306 .write_header = streamhash_write_header,
307 .write_packet = streamhash_write_packet,
308 .write_trailer = hash_write_trailer,
309 .deinit = hash_free,
310 .p.flags = AVFMT_VARIABLE_FPS | AVFMT_TS_NONSTRICT |
311 AVFMT_TS_NEGATIVE,
312 .p.priv_class = &streamhashenc_class,
313 };
314 #endif
315
316 #if CONFIG_FRAMEHASH_MUXER || CONFIG_FRAMEMD5_MUXER
317 308 static int framehash_init(struct AVFormatContext *s)
318 {
319 int res;
320 308 struct HashContext *c = s->priv_data;
321 308 c->per_stream = 0;
322 308 c->hashes = av_mallocz(sizeof(*c->hashes));
323
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 308 times.
308 if (!c->hashes)
324 ✗ return AVERROR(ENOMEM);
325 308 res = av_hash_alloc(&c->hashes->hash, c->hash_name);
326
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 308 times.
308 if (res < 0)
327 ✗ return res;
328 308 return 0;
329 }
330
331 308 static int framehash_write_header(struct AVFormatContext *s)
332 {
333 308 struct HashContext *c = s->priv_data;
334 308 avio_printf(s->pb, "#format: frame checksums\n");
335 308 avio_printf(s->pb, "#version: %d\n", c->format_version);
336 308 avio_printf(s->pb, "#hash: %s\n", av_hash_get_name(c->hashes->hash));
337 308 hash_print_extradata(s);
338 308 ff_framehash_write_header(s);
339 308 avio_printf(s->pb, "#stream#, dts, pts, duration, size, hash\n");
340 308 return 0;
341 }
342
343 7242 static int framehash_write_packet(struct AVFormatContext *s, AVPacket *pkt)
344 {
345 7242 struct HashContext *c = s->priv_data;
346 char buf[AV_HASH_MAX_SIZE*2+128];
347 int len;
348 7242 av_hash_init(c->hashes->hash);
349 7242 av_hash_update(c->hashes->hash, pkt->data, pkt->size);
350
351 7242 snprintf(buf, sizeof(buf) - (AV_HASH_MAX_SIZE * 2 + 1), "%d, %10"PRId64", %10"PRId64", %8"PRId64", %8d, ",
352 pkt->stream_index, pkt->dts, pkt->pts, pkt->duration, pkt->size);
353 7242 len = strlen(buf);
354 7242 av_hash_final_hex(c->hashes->hash, buf + len, sizeof(buf) - len);
355 7242 avio_write(s->pb, buf, strlen(buf));
356
357
3/4
✓ Branch 0 taken 7242 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 7240 times.
7242 if (c->format_version > 1 && pkt->side_data_elems) {
358 2 hash_print_sidedata(s, pkt->side_data, pkt->side_data_elems);
359 }
360
361 7242 avio_printf(s->pb, "\n");
362 7242 return 0;
363 }
364 #endif
365
366 #if CONFIG_FRAMEHASH_MUXER
367 static const AVClass framehash_class = {
368 .class_name = "frame hash muxer",
369 .item_name = av_default_item_name,
370 .option = hash_options,
371 .version = LIBAVUTIL_VERSION_INT,
372 };
373
374 const FFOutputFormat ff_framehash_muxer = {
375 .p.name = "framehash",
376 .p.long_name = NULL_IF_CONFIG_SMALL("Per-frame hash testing"),
377 .priv_data_size = sizeof(struct HashContext),
378 .p.audio_codec = AV_CODEC_ID_PCM_S16LE,
379 .p.video_codec = AV_CODEC_ID_RAWVIDEO,
380 .init = framehash_init,
381 .write_header = framehash_write_header,
382 .write_packet = framehash_write_packet,
383 .deinit = hash_free,
384 .p.flags = AVFMT_VARIABLE_FPS | AVFMT_TS_NONSTRICT |
385 AVFMT_TS_NEGATIVE,
386 .p.priv_class = &framehash_class,
387 };
388 #endif
389
390 #if CONFIG_FRAMEMD5_MUXER
391 static const AVClass framemd5_class = {
392 .class_name = "frame MD5 muxer",
393 .item_name = av_default_item_name,
394 .option = framemd5_options,
395 .version = LIBAVUTIL_VERSION_INT,
396 };
397
398 const FFOutputFormat ff_framemd5_muxer = {
399 .p.name = "framemd5",
400 .p.long_name = NULL_IF_CONFIG_SMALL("Per-frame MD5 testing"),
401 .priv_data_size = sizeof(struct HashContext),
402 .p.audio_codec = AV_CODEC_ID_PCM_S16LE,
403 .p.video_codec = AV_CODEC_ID_RAWVIDEO,
404 .init = framehash_init,
405 .write_header = framehash_write_header,
406 .write_packet = framehash_write_packet,
407 .deinit = hash_free,
408 .p.flags = AVFMT_VARIABLE_FPS | AVFMT_TS_NONSTRICT |
409 AVFMT_TS_NEGATIVE,
410 .p.priv_class = &framemd5_class,
411 };
412 #endif
413