1 /*
2  * Copyright © 2018-2021, VideoLAN and dav1d authors
3  * Copyright © 2018-2021, 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 #define XXH_INLINE_ALL
36 #include "xxhash.h"
37 
38 #include "output/muxer.h"
39 
40 typedef struct MuxerPriv {
41     XXH3_state_t* state;
42     FILE *f;
43 } xxh3Context;
44 
xxh3_open(xxh3Context * const xxh3,const char * const file,const Dav1dPictureParameters * const p,const unsigned fps[2])45 static int xxh3_open(xxh3Context *const xxh3, const char *const file,
46                     const Dav1dPictureParameters *const p,
47                     const unsigned fps[2])
48 {
49     xxh3->state = XXH3_createState();
50     if (!xxh3->state) return DAV1D_ERR(ENOMEM);
51     XXH_errorcode err = XXH3_128bits_reset(xxh3->state);
52     if (err != XXH_OK) {
53         XXH3_freeState(xxh3->state);
54         xxh3->state = NULL;
55         return DAV1D_ERR(ENOMEM);
56     }
57 
58     if (!strcmp(file, "-")) {
59         xxh3->f = stdout;
60     } else if (!(xxh3->f = fopen(file, "wb"))) {
61         XXH3_freeState(xxh3->state);
62         xxh3->state = NULL;
63         fprintf(stderr, "Failed to open %s: %s\n", file, strerror(errno));
64         return -1;
65     }
66 
67     return 0;
68 }
69 
xxh3_write(xxh3Context * const xxh3,Dav1dPicture * const p)70 static int xxh3_write(xxh3Context *const xxh3, Dav1dPicture *const p) {
71     const int hbd = p->p.bpc > 8;
72     const int w = p->p.w, h = p->p.h;
73     uint8_t *yptr = p->data[0];
74 
75     for (int y = 0; y < h; y++) {
76         XXH3_128bits_update(xxh3->state, yptr, w << hbd);
77         yptr += p->stride[0];
78     }
79 
80     if (p->p.layout != DAV1D_PIXEL_LAYOUT_I400) {
81         const int ss_ver = p->p.layout == DAV1D_PIXEL_LAYOUT_I420;
82         const int ss_hor = p->p.layout != DAV1D_PIXEL_LAYOUT_I444;
83         const int cw = (w + ss_hor) >> ss_hor;
84         const int ch = (h + ss_ver) >> ss_ver;
85         for (int pl = 1; pl <= 2; pl++) {
86             uint8_t *uvptr = p->data[pl];
87 
88             for (int y = 0; y < ch; y++) {
89                 XXH3_128bits_update(xxh3->state, uvptr, cw << hbd);
90                 uvptr += p->stride[1];
91             }
92         }
93     }
94 
95     dav1d_picture_unref(p);
96 
97     return 0;
98 }
99 
xxh3_close(xxh3Context * const xxh3)100 static void xxh3_close(xxh3Context *const xxh3) {
101     XXH128_hash_t hash = XXH3_128bits_digest(xxh3->state);
102     XXH3_freeState(xxh3->state);
103     XXH128_canonical_t c;
104     XXH128_canonicalFromHash(&c, hash);
105 
106     for (int i = 0; i < 16; i++)
107         fprintf(xxh3->f, "%2.2x", c.digest[i]);
108     fprintf(xxh3->f, "\n");
109 
110     if (xxh3->f != stdout)
111         fclose(xxh3->f);
112 }
113 
xxh3_verify(xxh3Context * const xxh3,const char * xxh3_str)114 static int xxh3_verify(xxh3Context *const xxh3, const char * xxh3_str) {
115     XXH128_hash_t hash = XXH3_128bits_digest(xxh3->state);
116     XXH3_freeState(xxh3->state);
117 
118     if (strlen(xxh3_str) < 32)
119         return -1;
120 
121     XXH128_canonical_t c;
122     char t[3] = { 0 };
123     for (int i = 0; i < 16; i++) {
124         char *ignore;
125         memcpy(t, xxh3_str, 2);
126         xxh3_str += 2;
127         c.digest[i] = (unsigned char) strtoul(t, &ignore, 16);
128     }
129     XXH128_hash_t verify = XXH128_hashFromCanonical(&c);
130 
131     return !XXH128_isEqual(hash, verify);
132 }
133 
134 const Muxer xxh3_muxer = {
135     .priv_data_size = sizeof(xxh3Context),
136     .name = "xxh3",
137     .extension = "xxh3",
138     .write_header = xxh3_open,
139     .write_picture = xxh3_write,
140     .write_trailer = xxh3_close,
141     .verify = xxh3_verify,
142 };
143