1 /*
2 * Copyright © 2018, VideoLAN and dav1d authors
3 * Copyright © 2018, Two Orioles, LLC
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright notice, this
10 * list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright notice,
13 * this list of conditions and the following disclaimer in the documentation
14 * and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
20 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include "config.h"
29
30 #include <errno.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34
35 #include "common/intops.h"
36
37 #include "output/muxer.h"
38
39 static const uint32_t k[64] = {
40 0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee,
41 0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501,
42 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be,
43 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821,
44 0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa,
45 0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8,
46 0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed,
47 0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a,
48 0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c,
49 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70,
50 0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x04881d05,
51 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665,
52 0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039,
53 0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1,
54 0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1,
55 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391,
56 };
57
58 #if ENDIANNESS_BIG
59 #define NE2LE_32(x) (((x & 0x00ff) << 24) |\
60 ((x & 0xff00) << 8) |\
61 ((x >> 8) & 0xff00) |\
62 ((x >> 24) & 0x00ff))
63
64 #define NE2LE_64(x) (((x & 0x000000ff) << 56) |\
65 ((x & 0x0000ff00) << 40) |\
66 ((x & 0x00ff0000) << 24) |\
67 ((x & 0xff000000) << 8) |\
68 ((x >> 8) & 0xff000000) |\
69 ((x >> 24) & 0x00ff0000) |\
70 ((x >> 40) & 0x0000ff00) |\
71 ((x >> 56) & 0x000000ff))
72
73 #else
74 #define NE2LE_32(x) (x)
75 #define NE2LE_64(x) (x)
76 #endif
77
78 typedef struct MuxerPriv {
79 uint32_t abcd[4];
80 union {
81 uint8_t data[64];
82 uint32_t data32[16];
83 };
84 uint64_t len;
85 FILE *f;
86 #if ENDIANNESS_BIG
87 uint8_t *bswap;
88 int bswap_w;
89 #endif
90 } MD5Context;
91
md5_open(MD5Context * const md5,const char * const file,const Dav1dPictureParameters * const p,const unsigned fps[2])92 static int md5_open(MD5Context *const md5, const char *const file,
93 const Dav1dPictureParameters *const p,
94 const unsigned fps[2])
95 {
96 if (!strcmp(file, "-")) {
97 md5->f = stdout;
98 } else if (!(md5->f = fopen(file, "wb"))) {
99 fprintf(stderr, "Failed to open %s: %s\n", file, strerror(errno));
100 return -1;
101 }
102
103 #if ENDIANNESS_BIG
104 md5->bswap = NULL;
105 md5->bswap_w = 0;
106 #endif
107
108 md5->abcd[0] = 0x67452301;
109 md5->abcd[1] = 0xefcdab89;
110 md5->abcd[2] = 0x98badcfe;
111 md5->abcd[3] = 0x10325476;
112 md5->len = 0;
113
114 return 0;
115 }
116
leftrotate(const uint32_t x,const int c)117 static inline uint32_t leftrotate(const uint32_t x, const int c) {
118 return (x << c) | (x >> (32 - c));
119 }
120
121 #define F(i) do { \
122 a = b + leftrotate(a + ((b & c) | (~b & d)) + k[i + 0] + NE2LE_32(data[i + 0]), 7); \
123 d = a + leftrotate(d + ((a & b) | (~a & c)) + k[i + 1] + NE2LE_32(data[i + 1]), 12); \
124 c = d + leftrotate(c + ((d & a) | (~d & b)) + k[i + 2] + NE2LE_32(data[i + 2]), 17); \
125 b = c + leftrotate(b + ((c & d) | (~c & a)) + k[i + 3] + NE2LE_32(data[i + 3]), 22); \
126 } while (0)
127
128 #define G(i) do { \
129 a = b + leftrotate(a + ((d & b) | (~d & c)) + k[i + 0] + NE2LE_32(data[(i + 1) & 15]), 5); \
130 d = a + leftrotate(d + ((c & a) | (~c & b)) + k[i + 1] + NE2LE_32(data[(i + 6) & 15]), 9); \
131 c = d + leftrotate(c + ((b & d) | (~b & a)) + k[i + 2] + NE2LE_32(data[(i + 11) & 15]), 14); \
132 b = c + leftrotate(b + ((a & c) | (~a & d)) + k[i + 3] + NE2LE_32(data[(i + 0) & 15]), 20); \
133 } while (0)
134
135 #define H(i) do { \
136 a = b + leftrotate(a + (b ^ c ^ d) + k[i + 0] + NE2LE_32(data[( 5 - i) & 15]), 4); \
137 d = a + leftrotate(d + (a ^ b ^ c) + k[i + 1] + NE2LE_32(data[( 8 - i) & 15]), 11); \
138 c = d + leftrotate(c + (d ^ a ^ b) + k[i + 2] + NE2LE_32(data[(11 - i) & 15]), 16); \
139 b = c + leftrotate(b + (c ^ d ^ a) + k[i + 3] + NE2LE_32(data[(14 - i) & 15]), 23); \
140 } while (0)
141
142 #define I(i) do { \
143 a = b + leftrotate(a + (c ^ (b | ~d)) + k[i + 0] + NE2LE_32(data[( 0 - i) & 15]), 6); \
144 d = a + leftrotate(d + (b ^ (a | ~c)) + k[i + 1] + NE2LE_32(data[( 7 - i) & 15]), 10); \
145 c = d + leftrotate(c + (a ^ (d | ~b)) + k[i + 2] + NE2LE_32(data[(14 - i) & 15]), 15); \
146 b = c + leftrotate(b + (d ^ (c | ~a)) + k[i + 3] + NE2LE_32(data[( 5 - i) & 15]), 21); \
147 } while (0)
148
md5_body(MD5Context * const md5,const uint32_t * const data)149 static void md5_body(MD5Context *const md5, const uint32_t *const data) {
150 uint32_t a = md5->abcd[0];
151 uint32_t b = md5->abcd[1];
152 uint32_t c = md5->abcd[2];
153 uint32_t d = md5->abcd[3];
154
155 F( 0); F( 4); F( 8); F(12);
156 G(16); G(20); G(24); G(28);
157 H(32); H(36); H(40); H(44);
158 I(48); I(52); I(56); I(60);
159
160 md5->abcd[0] += a;
161 md5->abcd[1] += b;
162 md5->abcd[2] += c;
163 md5->abcd[3] += d;
164 }
165
md5_update(MD5Context * const md5,const uint8_t * data,unsigned len)166 static void md5_update(MD5Context *const md5, const uint8_t *data, unsigned len) {
167 if (!len) return;
168
169 if (md5->len & 63) {
170 const unsigned tmp = umin(len, 64 - (md5->len & 63));
171
172 memcpy(&md5->data[md5->len & 63], data, tmp);
173 len -= tmp;
174 data += tmp;
175 md5->len += tmp;
176 if (!(md5->len & 63))
177 md5_body(md5, md5->data32);
178 }
179
180 while (len >= 64) {
181 memcpy(md5->data, data, 64);
182 md5_body(md5, md5->data32);
183 md5->len += 64;
184 data += 64;
185 len -= 64;
186 }
187
188 if (len) {
189 memcpy(md5->data, data, len);
190 md5->len += len;
191 }
192 }
193
md5_write(MD5Context * const md5,Dav1dPicture * const p)194 static int md5_write(MD5Context *const md5, Dav1dPicture *const p) {
195 const int hbd = p->p.bpc > 8;
196 const int w = p->p.w, h = p->p.h;
197 uint8_t *yptr = p->data[0];
198
199 #if ENDIANNESS_BIG
200 if (hbd && (!md5->bswap || md5->bswap_w < p->p.w)) {
201 free(md5->bswap);
202 md5->bswap_w = 0;
203 md5->bswap = malloc(p->p.w << 1);
204 if (!md5->bswap) return -1;
205 md5->bswap_w = p->p.w;
206 }
207 #endif
208
209 for (int y = 0; y < h; y++) {
210 #if ENDIANNESS_BIG
211 if (hbd) {
212 for (int x = 0; x < w; x++) {
213 md5->bswap[2 * x + 1] = yptr[2 * x];
214 md5->bswap[2 * x] = yptr[2 * x + 1];
215 }
216 md5_update(md5, md5->bswap, w << hbd);
217 } else
218 #endif
219 md5_update(md5, yptr, w << hbd);
220 yptr += p->stride[0];
221 }
222
223 if (p->p.layout != DAV1D_PIXEL_LAYOUT_I400) {
224 const int ss_ver = p->p.layout == DAV1D_PIXEL_LAYOUT_I420;
225 const int ss_hor = p->p.layout != DAV1D_PIXEL_LAYOUT_I444;
226 const int cw = (w + ss_hor) >> ss_hor;
227 const int ch = (h + ss_ver) >> ss_ver;
228 for (int pl = 1; pl <= 2; pl++) {
229 uint8_t *uvptr = p->data[pl];
230
231 for (int y = 0; y < ch; y++) {
232 #if ENDIANNESS_BIG
233 if (hbd) {
234 for (int x = 0; x < cw; x++){
235 md5->bswap[2 * x + 1] = uvptr[2 * x];
236 md5->bswap[2 * x] = uvptr[2 * x + 1];
237 }
238 md5_update(md5, md5->bswap, cw << hbd);
239 } else
240 #endif
241 md5_update(md5, uvptr, cw << hbd);
242 uvptr += p->stride[1];
243 }
244 }
245 }
246
247 dav1d_picture_unref(p);
248
249 return 0;
250 }
251
md5_finish(MD5Context * const md5)252 static void md5_finish(MD5Context *const md5) {
253 static const uint8_t bit[2] = { 0x80, 0x00 };
254 const uint64_t len = NE2LE_64(md5->len << 3);
255
256 md5_update(md5, &bit[0], 1);
257 while ((md5->len & 63) != 56)
258 md5_update(md5, &bit[1], 1);
259 md5_update(md5, (const uint8_t *) &len, 8);
260 }
261
md5_close(MD5Context * const md5)262 static void md5_close(MD5Context *const md5) {
263 md5_finish(md5);
264 for (int i = 0; i < 4; i++)
265 fprintf(md5->f, "%2.2x%2.2x%2.2x%2.2x",
266 md5->abcd[i] & 0xff,
267 (md5->abcd[i] >> 8) & 0xff,
268 (md5->abcd[i] >> 16) & 0xff,
269 md5->abcd[i] >> 24);
270 fprintf(md5->f, "\n");
271
272 #if ENDIANNESS_BIG
273 free(md5->bswap);
274 md5->bswap_w = 0;
275 #endif
276
277 if (md5->f != stdout)
278 fclose(md5->f);
279 }
280
md5_verify(MD5Context * const md5,const char * md5_str)281 static int md5_verify(MD5Context *const md5, const char *md5_str) {
282 md5_finish(md5);
283
284 if (strlen(md5_str) < 32)
285 return -1;
286
287 uint32_t abcd[4] = { 0 };
288 char t[3] = { 0 };
289 for (int i = 0; i < 4; i++) {
290 for (int j = 0; j < 32; j += 8) {
291 char *ignore;
292 memcpy(t, md5_str, 2);
293 md5_str += 2;
294 abcd[i] |= (uint32_t) strtoul(t, &ignore, 16) << j;
295 }
296 }
297
298 #if ENDIANNESS_BIG
299 free(md5->bswap);
300 md5->bswap_w = 0;
301 #endif
302
303 return !!memcmp(abcd, md5->abcd, sizeof(abcd));
304 }
305
306 const Muxer md5_muxer = {
307 .priv_data_size = sizeof(MD5Context),
308 .name = "md5",
309 .extension = "md5",
310 .write_header = md5_open,
311 .write_picture = md5_write,
312 .write_trailer = md5_close,
313 .verify = md5_verify,
314 };
315