FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/framecrcenc.c
Date: 2025-06-23 20:06:14
Exec Total Coverage
Lines: 28 30 93.3%
Functions: 2 2 100.0%
Branches: 11 12 91.7%

Line Branch Exec Source
1 /*
2 * frame CRC encoder (for codec/format testing)
3 * Copyright (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 <inttypes.h>
23
24 #include "config.h"
25 #include "libavutil/adler32.h"
26 #include "libavutil/avstring.h"
27 #include "libavutil/hdr_dynamic_metadata.h"
28 #include "libavutil/intreadwrite.h"
29
30 #include "libavcodec/codec_id.h"
31 #include "libavcodec/codec_par.h"
32 #include "libavcodec/packet.h"
33
34 #include "avformat.h"
35 #include "internal.h"
36 #include "mux.h"
37
38 1571 static int framecrc_write_header(struct AVFormatContext *s)
39 {
40 int i;
41
2/2
✓ Branch 0 taken 1831 times.
✓ Branch 1 taken 1571 times.
3402 for (i = 0; i < s->nb_streams; i++) {
42 1831 AVStream *st = s->streams[i];
43 1831 AVCodecParameters *par = st->codecpar;
44
2/2
✓ Branch 0 taken 182 times.
✓ Branch 1 taken 1649 times.
1831 if (par->extradata) {
45 182 uint32_t crc = av_adler32_update(0, par->extradata, par->extradata_size);
46 182 avio_printf(s->pb, "#extradata %d: %8d, 0x%08"PRIx32"\n",
47 i, par->extradata_size, crc);
48 }
49 }
50
51 1571 return ff_framehash_write_header(s);
52 }
53
54 static av_unused void inline bswap(char *buf, int offset, int size)
55 {
56 if (size == 8) {
57 uint64_t val = AV_RN64(buf + offset);
58 AV_WN64(buf + offset, av_bswap64(val));
59 } else if (size == 4) {
60 uint32_t val = AV_RN32(buf + offset);
61 AV_WN32(buf + offset, av_bswap32(val));
62 } else if (size == 2) {
63 uint16_t val = AV_RN16(buf + offset);
64 AV_WN16(buf + offset, av_bswap16(val));
65 }
66 }
67
68 84158 static int framecrc_write_packet(struct AVFormatContext *s, AVPacket *pkt)
69 {
70 84158 uint32_t crc = av_adler32_update(0, pkt->data, pkt->size);
71 char buf[256];
72
73 84158 snprintf(buf, sizeof(buf), "%d, %10"PRId64", %10"PRId64", %8"PRId64", %8d, 0x%08"PRIx32,
74 pkt->stream_index, pkt->dts, pkt->pts, pkt->duration, pkt->size, crc);
75
2/2
✓ Branch 0 taken 4672 times.
✓ Branch 1 taken 79486 times.
84158 if (pkt->flags != AV_PKT_FLAG_KEY)
76 4672 av_strlcatf(buf, sizeof(buf), ", F=0x%0X", pkt->flags);
77
2/2
✓ Branch 0 taken 1238 times.
✓ Branch 1 taken 82920 times.
84158 if (pkt->side_data_elems) {
78 1238 av_strlcatf(buf, sizeof(buf), ", S=%d", pkt->side_data_elems);
79
80
2/2
✓ Branch 0 taken 1238 times.
✓ Branch 1 taken 1238 times.
2476 for (int i = 0; i < pkt->side_data_elems; i++) {
81 1238 const AVPacketSideData *const sd = &pkt->side_data[i];
82 1238 const uint8_t *data = sd->data;
83 1238 uint32_t side_data_crc = 0;
84
85
1/2
✓ Branch 0 taken 1238 times.
✗ Branch 1 not taken.
1238 switch (sd->type) {
86 #if HAVE_BIGENDIAN
87 uint8_t bswap_buf[FFMAX3(sizeof(AVCPBProperties),
88 sizeof(AVProducerReferenceTime),
89 sizeof(AVDynamicHDRPlus))];
90 case AV_PKT_DATA_PALETTE:
91 case AV_PKT_DATA_REPLAYGAIN:
92 case AV_PKT_DATA_DISPLAYMATRIX:
93 case AV_PKT_DATA_STEREO3D:
94 case AV_PKT_DATA_AUDIO_SERVICE_TYPE:
95 case AV_PKT_DATA_FALLBACK_TRACK:
96 case AV_PKT_DATA_MASTERING_DISPLAY_METADATA:
97 case AV_PKT_DATA_SPHERICAL:
98 case AV_PKT_DATA_CONTENT_LIGHT_LEVEL:
99 case AV_PKT_DATA_S12M_TIMECODE:
100 for (size_t j = 0; j < sd->size / 4; j++) {
101 uint8_t buf[4];
102 AV_WL32(buf, AV_RB32(sd->data + 4 * j));
103 side_data_crc = av_adler32_update(side_data_crc, buf, 4);
104 }
105 break;
106 case AV_PKT_DATA_CPB_PROPERTIES:
107 #define BSWAP(struct, field) bswap(bswap_buf, offsetof(struct, field), sizeof(((struct){0}).field))
108 #define BSWAP_RAT(struct, field) do {BSWAP(struct, field.num); BSWAP(struct, field.den);} while(0)
109 if (sd->size == sizeof(AVCPBProperties)) {
110 memcpy(bswap_buf, sd->data, sizeof(AVCPBProperties));
111 data = bswap_buf;
112 BSWAP(AVCPBProperties, max_bitrate);
113 BSWAP(AVCPBProperties, min_bitrate);
114 BSWAP(AVCPBProperties, avg_bitrate);
115 BSWAP(AVCPBProperties, buffer_size);
116 BSWAP(AVCPBProperties, vbv_delay);
117 }
118 goto pod;
119 case AV_PKT_DATA_PRFT:
120 if (sd->size == sizeof(AVProducerReferenceTime)) {
121 memcpy(bswap_buf, sd->data, sizeof(AVProducerReferenceTime));
122 data = bswap_buf;
123 BSWAP(AVProducerReferenceTime, wallclock);
124 BSWAP(AVProducerReferenceTime, flags);
125 }
126 goto pod;
127 case AV_PKT_DATA_DYNAMIC_HDR10_PLUS:
128 if (sd->size == sizeof(AVDynamicHDRPlus)) {
129 memcpy(bswap_buf, sd->data, sizeof(AVDynamicHDRPlus));
130 data = bswap_buf;
131 for (int i = 0; i < FF_ARRAY_ELEMS(((AVDynamicHDRPlus*)0)->params); i++) {
132 BSWAP_RAT(AVDynamicHDRPlus, params[i].window_upper_left_corner_x);
133 BSWAP_RAT(AVDynamicHDRPlus, params[i].window_upper_left_corner_y);
134 BSWAP_RAT(AVDynamicHDRPlus, params[i].window_lower_right_corner_x);
135 BSWAP_RAT(AVDynamicHDRPlus, params[i].window_lower_right_corner_y);
136 BSWAP(AVDynamicHDRPlus, params[i].center_of_ellipse_x);
137 BSWAP(AVDynamicHDRPlus, params[i].center_of_ellipse_y);
138 BSWAP(AVDynamicHDRPlus, params[i].semimajor_axis_internal_ellipse);
139 BSWAP(AVDynamicHDRPlus, params[i].semimajor_axis_external_ellipse);
140 BSWAP(AVDynamicHDRPlus, params[i].semiminor_axis_external_ellipse);
141 //!!! overlap_process_option
142 for(int j = 0; j < FF_ARRAY_ELEMS(((AVDynamicHDRPlus*)0)->params->maxscl); j++)
143 BSWAP_RAT(AVDynamicHDRPlus, params[i].maxscl[j]);
144 BSWAP_RAT(AVDynamicHDRPlus, params[i].average_maxrgb);
145 BSWAP_RAT(AVDynamicHDRPlus, params[i].fraction_bright_pixels);
146 BSWAP_RAT(AVDynamicHDRPlus, params[i].knee_point_x);
147 BSWAP_RAT(AVDynamicHDRPlus, params[i].knee_point_y);
148 for(int j = 0; j < FF_ARRAY_ELEMS(((AVDynamicHDRPlus*)0)->params->distribution_maxrgb); j++) {
149 BSWAP_RAT(AVDynamicHDRPlus, params[i].distribution_maxrgb[j].percentile);
150 BSWAP_RAT(AVDynamicHDRPlus, params[i].bezier_curve_anchors[j]);
151 }
152 BSWAP_RAT(AVDynamicHDRPlus, params[i].color_saturation_weight);
153 }
154 BSWAP_RAT(AVDynamicHDRPlus, targeted_system_display_maximum_luminance);
155 for(int i = 0; i<FF_ARRAY_ELEMS(((AVDynamicHDRPlus*)0)->targeted_system_display_actual_peak_luminance); i++)
156 for(int j = 0; j<FF_ARRAY_ELEMS(((AVDynamicHDRPlus*)0)->targeted_system_display_actual_peak_luminance[0]); j++) {
157 BSWAP_RAT(AVDynamicHDRPlus, targeted_system_display_actual_peak_luminance[i][j]);
158 BSWAP_RAT(AVDynamicHDRPlus, mastering_display_actual_peak_luminance[i][j]);
159 }
160 }
161 pod:
162 #endif
163
164 1238 default:
165 1238 side_data_crc = av_adler32_update(0, data, sd->size);
166 1238 break;
167 case AV_PKT_DATA_IAMF_MIX_GAIN_PARAM:
168 case AV_PKT_DATA_IAMF_DEMIXING_INFO_PARAM:
169 case AV_PKT_DATA_IAMF_RECON_GAIN_INFO_PARAM:
170 side_data_crc = 0;
171 }
172
173 1238 av_strlcatf(buf, sizeof(buf), ", %s, %8"SIZE_SPECIFIER", 0x%08"PRIx32,
174 1238 av_packet_side_data_name(sd->type), sd->size, side_data_crc);
175 }
176 }
177 84158 av_strlcatf(buf, sizeof(buf), "\n");
178 84158 avio_write(s->pb, buf, strlen(buf));
179 84158 return 0;
180 }
181
182 const FFOutputFormat ff_framecrc_muxer = {
183 .p.name = "framecrc",
184 .p.long_name = NULL_IF_CONFIG_SMALL("framecrc testing"),
185 .p.audio_codec = AV_CODEC_ID_PCM_S16LE,
186 .p.video_codec = AV_CODEC_ID_RAWVIDEO,
187 .write_header = framecrc_write_header,
188 .write_packet = framecrc_write_packet,
189 .p.flags = AVFMT_VARIABLE_FPS | AVFMT_TS_NONSTRICT |
190 AVFMT_TS_NEGATIVE | AVFMT_NODIMENSIONS,
191 };
192