1 /*
2  * Copyright © 2020, VideoLAN and dav1d authors
3  * Copyright © 2020, 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 #include "vcs_version.h"
30 #include "cli_config.h"
31 
32 #include <math.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 
37 #include "dav1d/dav1d.h"
38 #include "input/input.h"
39 #include "input/demuxer.h"
40 #include "dav1d_cli_parse.h"
41 
42 #define NUM_RAND_SEEK 3
43 #define NUM_REL_SEEK  4
44 #define NUM_END_SEEK  2
45 
46 const Demuxer annexb_demuxer = { .name = "" };
47 const Demuxer section5_demuxer = { .name = "" };
48 
49 #ifdef _WIN32
50 #include <windows.h>
get_seed(void)51 static unsigned get_seed(void) {
52     return GetTickCount();
53 }
54 #else
55 #ifdef __APPLE__
56 #include <mach/mach_time.h>
57 #else
58 #include <time.h>
59 #endif
get_seed(void)60 static unsigned get_seed(void) {
61 #ifdef __APPLE__
62     return (unsigned) mach_absolute_time();
63 #elif defined(HAVE_CLOCK_GETTIME)
64     struct timespec ts;
65     clock_gettime(CLOCK_MONOTONIC, &ts);
66     return (unsigned) (1000000000ULL * ts.tv_sec + ts.tv_nsec);
67 #endif
68 }
69 #endif
70 
71 static uint32_t xs_state[4];
72 
xor128_srand(unsigned seed)73 static void xor128_srand(unsigned seed) {
74     xs_state[0] = seed;
75     xs_state[1] = ( seed & 0xffff0000) | (~seed & 0x0000ffff);
76     xs_state[2] = (~seed & 0xffff0000) | ( seed & 0x0000ffff);
77     xs_state[3] = ~seed;
78 }
79 
80 // xor128 from Marsaglia, George (July 2003). "Xorshift RNGs".
81 //             Journal of Statistical Software. 8 (14).
82 //             doi:10.18637/jss.v008.i14.
xor128_rand(void)83 static int xor128_rand(void) {
84     const uint32_t x = xs_state[0];
85     const uint32_t t = x ^ (x << 11);
86 
87     xs_state[0] = xs_state[1];
88     xs_state[1] = xs_state[2];
89     xs_state[2] = xs_state[3];
90     uint32_t w = xs_state[3];
91 
92     w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));
93     xs_state[3] = w;
94 
95     return w >> 1;
96 }
97 
decode_frame(Dav1dPicture * const p,Dav1dContext * const c,Dav1dData * const data)98 static inline int decode_frame(Dav1dPicture *const p,
99                                Dav1dContext *const c, Dav1dData *const data)
100 {
101     int res;
102     memset(p, 0, sizeof(*p));
103     if ((res = dav1d_send_data(c, data)) < 0) {
104         if (res != DAV1D_ERR(EAGAIN)) {
105             fprintf(stderr, "Error decoding frame: %s\n",
106                     strerror(DAV1D_ERR(res)));
107             return res;
108         }
109     }
110     if ((res = dav1d_get_picture(c, p)) < 0) {
111         if (res != DAV1D_ERR(EAGAIN)) {
112             fprintf(stderr, "Error decoding frame: %s\n",
113                     strerror(DAV1D_ERR(res)));
114             return res;
115         }
116     } else dav1d_picture_unref(p);
117     return 0;
118 }
119 
decode_rand(DemuxerContext * const in,Dav1dContext * const c,Dav1dData * const data,const double fps)120 static int decode_rand(DemuxerContext *const in, Dav1dContext *const c,
121                        Dav1dData *const data, const double fps)
122 {
123     int res = 0;
124     Dav1dPicture p;
125     const int num_frames = xor128_rand() % (int)(fps * 5);
126     for (int i = 0; i < num_frames; i++) {
127         if ((res = decode_frame(&p, c, data))) break;
128         if (input_read(in, data) || data->sz == 0) break;
129     }
130     return res;
131 }
132 
decode_all(DemuxerContext * const in,Dav1dContext * const c,Dav1dData * const data)133 static int decode_all(DemuxerContext *const in,
134                       Dav1dContext *const c, Dav1dData *const data)
135 {
136     int res = 0;
137     Dav1dPicture p;
138     do { if ((res = decode_frame(&p, c, data))) break;
139     } while (!input_read(in, data) && data->sz > 0);
140     return res;
141 }
142 
seek(DemuxerContext * const in,Dav1dContext * const c,const uint64_t pts,Dav1dData * const data)143 static int seek(DemuxerContext *const in, Dav1dContext *const c,
144                 const uint64_t pts, Dav1dData *const data)
145 {
146     int res;
147     if ((res = input_seek(in, pts))) return res;
148     Dav1dSequenceHeader seq;
149     do { if ((res = input_read(in, data))) break;
150     } while (dav1d_parse_sequence_header(&seq, data->data, data->sz));
151     dav1d_flush(c);
152     return res;
153 }
154 
main(const int argc,char * const * const argv)155 int main(const int argc, char *const *const argv) {
156     const char *version = dav1d_version();
157     if (strcmp(version, DAV1D_VERSION)) {
158         fprintf(stderr, "Version mismatch (library: %s, executable: %s)\n",
159                 version, DAV1D_VERSION);
160         return EXIT_FAILURE;
161     }
162 
163     CLISettings cli_settings;
164     Dav1dSettings lib_settings;
165     DemuxerContext *in;
166     Dav1dContext *c;
167     Dav1dData data;
168     unsigned total, i_fps[2], i_timebase[2];
169     double timebase, spf, fps;
170     uint64_t pts;
171 
172     xor128_srand(get_seed());
173     parse(argc, argv, &cli_settings, &lib_settings);
174 
175     if (input_open(&in, "ivf", cli_settings.inputfile,
176                    i_fps, &total, i_timebase) < 0 ||
177         !i_timebase[0] || !i_timebase[1] ||  !i_fps[0] || !i_fps[1])
178     {
179         return EXIT_SUCCESS;
180     }
181     if (dav1d_open(&c, &lib_settings))
182         return EXIT_FAILURE;
183 
184     timebase = (double)i_timebase[1] / i_timebase[0];
185     spf = (double)i_fps[1] / i_fps[0];
186     fps = (double)i_fps[0] / i_fps[1];
187     if (fps < 1) goto end;
188 
189 #define FRAME_OFFSET_TO_PTS(foff) \
190     (uint64_t)llround(((foff) * spf) * 1000000000.0)
191 #define TS_TO_PTS(ts) \
192     (uint64_t)llround(((ts) * timebase) * 1000000000.0)
193 
194     // seek at random pts
195     for (int i = 0; i < NUM_RAND_SEEK; i++) {
196         pts = FRAME_OFFSET_TO_PTS(xor128_rand() % total);
197         if (seek(in, c, pts, &data)) continue;
198         if (decode_rand(in, c, &data, fps)) goto end;
199     }
200     pts = TS_TO_PTS(data.m.timestamp);
201 
202     // seek left / right randomly with random intervals within 1s
203     for (int i = 0, tries = 0;
204          i - tries < NUM_REL_SEEK && tries < NUM_REL_SEEK / 2;
205          i++)
206     {
207         const int sign = xor128_rand() & 1 ? -1 : +1;
208         const float diff = (xor128_rand() % 100) / 100.f;
209         int64_t new_pts = pts + sign * FRAME_OFFSET_TO_PTS(diff * fps);
210         const int64_t new_ts = llround(new_pts / (timebase * 1000000000.0));
211         new_pts = TS_TO_PTS(new_ts);
212         if (new_pts < 0 || (uint64_t)new_pts >= FRAME_OFFSET_TO_PTS(total)) {
213             if (seek(in, c, FRAME_OFFSET_TO_PTS(total / 2), &data)) break;
214             pts = TS_TO_PTS(data.m.timestamp);
215             tries++;
216             continue;
217         }
218         if (seek(in, c, new_pts, &data))
219             if (seek(in, c, 0, &data)) goto end;
220         if (decode_rand(in, c, &data, fps)) goto end;
221         pts = TS_TO_PTS(data.m.timestamp);
222     }
223 
224     unsigned shift = 0;
225     do {
226         shift += 5;
227         if (shift > total)
228             shift = total;
229     } while (seek(in, c, FRAME_OFFSET_TO_PTS(total - shift), &data));
230 
231     // simulate seeking after the end of the file
232     for (int i = 0; i < NUM_END_SEEK; i++) {
233         if (seek(in, c, FRAME_OFFSET_TO_PTS(total - shift), &data)) goto end;
234         if (decode_all(in, c, &data)) goto end;
235         int num_flush = 1 + 64 + xor128_rand() % 64;
236         while (num_flush--) dav1d_flush(c);
237     }
238 
239 end:
240     input_close(in);
241     dav1d_close(&c);
242     return EXIT_SUCCESS;
243 }
244