1 /*
2 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
3 *
4 * This source code is subject to the terms of the BSD 2 Clause License and
5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6 * was not distributed with this source code in the LICENSE file, you can
7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8 * Media Patent License 1.0 was not distributed with this source code in the
9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10 */
11
12 #include <assert.h>
13 #include <math.h>
14 #include <stdarg.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18
19 #include "common/tools_common.h"
20
21 #if CONFIG_AV1_ENCODER
22 #include "aom/aomcx.h"
23 #endif
24
25 #if CONFIG_AV1_DECODER
26 #include "aom/aomdx.h"
27 #endif
28
29 #if defined(_WIN32) || defined(__OS2__)
30 #include <io.h>
31 #include <fcntl.h>
32
33 #ifdef __OS2__
34 #define _setmode setmode
35 #define _fileno fileno
36 #define _O_BINARY O_BINARY
37 #endif
38 #endif
39
40 #define LOG_ERROR(label) \
41 do { \
42 const char *l = label; \
43 va_list ap; \
44 va_start(ap, fmt); \
45 if (l) fprintf(stderr, "%s: ", l); \
46 vfprintf(stderr, fmt, ap); \
47 fprintf(stderr, "\n"); \
48 va_end(ap); \
49 } while (0)
50
set_binary_mode(FILE * stream)51 FILE *set_binary_mode(FILE *stream) {
52 (void)stream;
53 #if defined(_WIN32) || defined(__OS2__)
54 _setmode(_fileno(stream), _O_BINARY);
55 #endif
56 return stream;
57 }
58
die(const char * fmt,...)59 void die(const char *fmt, ...) {
60 LOG_ERROR(NULL);
61 usage_exit();
62 }
63
fatal(const char * fmt,...)64 void fatal(const char *fmt, ...) {
65 LOG_ERROR("Fatal");
66 exit(EXIT_FAILURE);
67 }
68
aom_tools_warn(const char * fmt,...)69 void aom_tools_warn(const char *fmt, ...) { LOG_ERROR("Warning"); }
70
die_codec(aom_codec_ctx_t * ctx,const char * s)71 void die_codec(aom_codec_ctx_t *ctx, const char *s) {
72 const char *detail = aom_codec_error_detail(ctx);
73
74 printf("%s: %s\n", s, aom_codec_error(ctx));
75 if (detail) printf(" %s\n", detail);
76 exit(EXIT_FAILURE);
77 }
78
read_yuv_frame(struct AvxInputContext * input_ctx,aom_image_t * yuv_frame)79 int read_yuv_frame(struct AvxInputContext *input_ctx, aom_image_t *yuv_frame) {
80 FILE *f = input_ctx->file;
81 struct FileTypeDetectionBuffer *detect = &input_ctx->detect;
82 int plane = 0;
83 int shortread = 0;
84 const int bytespp = (yuv_frame->fmt & AOM_IMG_FMT_HIGHBITDEPTH) ? 2 : 1;
85
86 for (plane = 0; plane < 3; ++plane) {
87 uint8_t *ptr;
88 int w = aom_img_plane_width(yuv_frame, plane);
89 const int h = aom_img_plane_height(yuv_frame, plane);
90 int r;
91 // Assuming that for nv12 we read all chroma data at one time
92 if (yuv_frame->fmt == AOM_IMG_FMT_NV12 && plane > 1) break;
93 if (yuv_frame->fmt == AOM_IMG_FMT_NV12 && plane == 1) w *= 2;
94 /* Determine the correct plane based on the image format. The for-loop
95 * always counts in Y,U,V order, but this may not match the order of
96 * the data on disk.
97 */
98 switch (plane) {
99 case 1:
100 ptr =
101 yuv_frame->planes[yuv_frame->fmt == AOM_IMG_FMT_YV12 ? AOM_PLANE_V
102 : AOM_PLANE_U];
103 break;
104 case 2:
105 ptr =
106 yuv_frame->planes[yuv_frame->fmt == AOM_IMG_FMT_YV12 ? AOM_PLANE_U
107 : AOM_PLANE_V];
108 break;
109 default: ptr = yuv_frame->planes[plane];
110 }
111
112 for (r = 0; r < h; ++r) {
113 size_t needed = w * bytespp;
114 size_t buf_position = 0;
115 const size_t left = detect->buf_read - detect->position;
116 if (left > 0) {
117 const size_t more = (left < needed) ? left : needed;
118 memcpy(ptr, detect->buf + detect->position, more);
119 buf_position = more;
120 needed -= more;
121 detect->position += more;
122 }
123 if (needed > 0) {
124 shortread |= (fread(ptr + buf_position, 1, needed, f) < needed);
125 }
126
127 ptr += yuv_frame->stride[plane];
128 }
129 }
130
131 return shortread;
132 }
133
134 struct CodecInfo {
135 // Pointer to a function of zero arguments that returns an aom_codec_iface_t.
136 aom_codec_iface_t *(*const interface)();
137 char *short_name;
138 uint32_t fourcc;
139 };
140
141 #if CONFIG_AV1_ENCODER
142 static const struct CodecInfo aom_encoders[] = {
143 { &aom_codec_av1_cx, "av1", AV1_FOURCC },
144 };
145
get_aom_encoder_count(void)146 int get_aom_encoder_count(void) {
147 return sizeof(aom_encoders) / sizeof(aom_encoders[0]);
148 }
149
get_aom_encoder_by_index(int i)150 aom_codec_iface_t *get_aom_encoder_by_index(int i) {
151 assert(i >= 0 && i < get_aom_encoder_count());
152 return aom_encoders[i].interface();
153 }
154
get_aom_encoder_by_short_name(const char * name)155 aom_codec_iface_t *get_aom_encoder_by_short_name(const char *name) {
156 for (int i = 0; i < get_aom_encoder_count(); ++i) {
157 const struct CodecInfo *info = &aom_encoders[i];
158 if (strcmp(info->short_name, name) == 0) return info->interface();
159 }
160 return NULL;
161 }
162
get_fourcc_by_aom_encoder(aom_codec_iface_t * iface)163 uint32_t get_fourcc_by_aom_encoder(aom_codec_iface_t *iface) {
164 for (int i = 0; i < get_aom_encoder_count(); ++i) {
165 const struct CodecInfo *info = &aom_encoders[i];
166 if (info->interface() == iface) {
167 return info->fourcc;
168 }
169 }
170 return 0;
171 }
172
get_short_name_by_aom_encoder(aom_codec_iface_t * iface)173 const char *get_short_name_by_aom_encoder(aom_codec_iface_t *iface) {
174 for (int i = 0; i < get_aom_encoder_count(); ++i) {
175 const struct CodecInfo *info = &aom_encoders[i];
176 if (info->interface() == iface) {
177 return info->short_name;
178 }
179 }
180 return NULL;
181 }
182
183 #endif // CONFIG_AV1_ENCODER
184
185 #if CONFIG_AV1_DECODER
186 static const struct CodecInfo aom_decoders[] = {
187 { &aom_codec_av1_dx, "av1", AV1_FOURCC },
188 };
189
get_aom_decoder_count(void)190 int get_aom_decoder_count(void) {
191 return sizeof(aom_decoders) / sizeof(aom_decoders[0]);
192 }
193
get_aom_decoder_by_index(int i)194 aom_codec_iface_t *get_aom_decoder_by_index(int i) {
195 assert(i >= 0 && i < get_aom_decoder_count());
196 return aom_decoders[i].interface();
197 }
198
get_aom_decoder_by_short_name(const char * name)199 aom_codec_iface_t *get_aom_decoder_by_short_name(const char *name) {
200 for (int i = 0; i < get_aom_decoder_count(); ++i) {
201 const struct CodecInfo *info = &aom_decoders[i];
202 if (strcmp(info->short_name, name) == 0) return info->interface();
203 }
204 return NULL;
205 }
206
get_aom_decoder_by_fourcc(uint32_t fourcc)207 aom_codec_iface_t *get_aom_decoder_by_fourcc(uint32_t fourcc) {
208 for (int i = 0; i < get_aom_decoder_count(); ++i) {
209 const struct CodecInfo *info = &aom_decoders[i];
210 if (info->fourcc == fourcc) return info->interface();
211 }
212 return NULL;
213 }
214
get_short_name_by_aom_decoder(aom_codec_iface_t * iface)215 const char *get_short_name_by_aom_decoder(aom_codec_iface_t *iface) {
216 for (int i = 0; i < get_aom_decoder_count(); ++i) {
217 const struct CodecInfo *info = &aom_decoders[i];
218 if (info->interface() == iface) {
219 return info->short_name;
220 }
221 }
222 return NULL;
223 }
224
get_fourcc_by_aom_decoder(aom_codec_iface_t * iface)225 uint32_t get_fourcc_by_aom_decoder(aom_codec_iface_t *iface) {
226 for (int i = 0; i < get_aom_decoder_count(); ++i) {
227 const struct CodecInfo *info = &aom_decoders[i];
228 if (info->interface() == iface) {
229 return info->fourcc;
230 }
231 }
232 return 0;
233 }
234
235 #endif // CONFIG_AV1_DECODER
236
aom_img_write(const aom_image_t * img,FILE * file)237 void aom_img_write(const aom_image_t *img, FILE *file) {
238 int plane;
239
240 for (plane = 0; plane < 3; ++plane) {
241 const unsigned char *buf = img->planes[plane];
242 const int stride = img->stride[plane];
243 const int w = aom_img_plane_width(img, plane) *
244 ((img->fmt & AOM_IMG_FMT_HIGHBITDEPTH) ? 2 : 1);
245 const int h = aom_img_plane_height(img, plane);
246 int y;
247
248 for (y = 0; y < h; ++y) {
249 fwrite(buf, 1, w, file);
250 buf += stride;
251 }
252 }
253 }
254
aom_img_read(aom_image_t * img,FILE * file)255 bool aom_img_read(aom_image_t *img, FILE *file) {
256 int plane;
257 const int bytespp = (img->fmt & AOM_IMG_FMT_HIGHBITDEPTH) ? 2 : 1;
258
259 for (plane = 0; plane < 3; ++plane) {
260 unsigned char *buf = img->planes[plane];
261 const int stride = img->stride[plane];
262 const int w = aom_img_plane_width(img, plane) * bytespp;
263 const int h = aom_img_plane_height(img, plane);
264 int y;
265
266 for (y = 0; y < h; ++y) {
267 if (fread(buf, 1, w, file) != (size_t)w) return false;
268 buf += stride;
269 }
270 }
271
272 return true;
273 }
274
275 // TODO(dkovalev) change sse_to_psnr signature: double -> int64_t
sse_to_psnr(double samples,double peak,double sse)276 double sse_to_psnr(double samples, double peak, double sse) {
277 static const double kMaxPSNR = 100.0;
278
279 if (sse > 0.0) {
280 const double psnr = 10.0 * log10(samples * peak * peak / sse);
281 return psnr > kMaxPSNR ? kMaxPSNR : psnr;
282 } else {
283 return kMaxPSNR;
284 }
285 }
286
287 // TODO(debargha): Consolidate the functions below into a separate file.
highbd_img_upshift(aom_image_t * dst,const aom_image_t * src,int input_shift)288 static void highbd_img_upshift(aom_image_t *dst, const aom_image_t *src,
289 int input_shift) {
290 // Note the offset is 1 less than half.
291 const int offset = input_shift > 0 ? (1 << (input_shift - 1)) - 1 : 0;
292 int plane;
293 if (dst->d_w != src->d_w || dst->d_h != src->d_h ||
294 dst->x_chroma_shift != src->x_chroma_shift ||
295 dst->y_chroma_shift != src->y_chroma_shift || dst->fmt != src->fmt ||
296 input_shift < 0) {
297 fatal("Unsupported image conversion");
298 }
299 switch (src->fmt) {
300 case AOM_IMG_FMT_I42016:
301 case AOM_IMG_FMT_I42216:
302 case AOM_IMG_FMT_I44416: break;
303 default: fatal("Unsupported image conversion"); break;
304 }
305 for (plane = 0; plane < 3; plane++) {
306 int w = src->d_w;
307 int h = src->d_h;
308 int x, y;
309 if (plane) {
310 w = (w + src->x_chroma_shift) >> src->x_chroma_shift;
311 h = (h + src->y_chroma_shift) >> src->y_chroma_shift;
312 }
313 for (y = 0; y < h; y++) {
314 const uint16_t *p_src =
315 (const uint16_t *)(src->planes[plane] + y * src->stride[plane]);
316 uint16_t *p_dst =
317 (uint16_t *)(dst->planes[plane] + y * dst->stride[plane]);
318 for (x = 0; x < w; x++) *p_dst++ = (*p_src++ << input_shift) + offset;
319 }
320 }
321 }
322
lowbd_img_upshift(aom_image_t * dst,const aom_image_t * src,int input_shift)323 static void lowbd_img_upshift(aom_image_t *dst, const aom_image_t *src,
324 int input_shift) {
325 // Note the offset is 1 less than half.
326 const int offset = input_shift > 0 ? (1 << (input_shift - 1)) - 1 : 0;
327 int plane;
328 if (dst->d_w != src->d_w || dst->d_h != src->d_h ||
329 dst->x_chroma_shift != src->x_chroma_shift ||
330 dst->y_chroma_shift != src->y_chroma_shift ||
331 dst->fmt != src->fmt + AOM_IMG_FMT_HIGHBITDEPTH || input_shift < 0) {
332 fatal("Unsupported image conversion");
333 }
334 switch (src->fmt) {
335 case AOM_IMG_FMT_YV12:
336 case AOM_IMG_FMT_I420:
337 case AOM_IMG_FMT_I422:
338 case AOM_IMG_FMT_I444: break;
339 default: fatal("Unsupported image conversion"); break;
340 }
341 for (plane = 0; plane < 3; plane++) {
342 int w = src->d_w;
343 int h = src->d_h;
344 int x, y;
345 if (plane) {
346 w = (w + src->x_chroma_shift) >> src->x_chroma_shift;
347 h = (h + src->y_chroma_shift) >> src->y_chroma_shift;
348 }
349 for (y = 0; y < h; y++) {
350 const uint8_t *p_src = src->planes[plane] + y * src->stride[plane];
351 uint16_t *p_dst =
352 (uint16_t *)(dst->planes[plane] + y * dst->stride[plane]);
353 for (x = 0; x < w; x++) {
354 *p_dst++ = (*p_src++ << input_shift) + offset;
355 }
356 }
357 }
358 }
359
aom_img_upshift(aom_image_t * dst,const aom_image_t * src,int input_shift)360 void aom_img_upshift(aom_image_t *dst, const aom_image_t *src,
361 int input_shift) {
362 if (src->fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
363 highbd_img_upshift(dst, src, input_shift);
364 } else {
365 lowbd_img_upshift(dst, src, input_shift);
366 }
367 }
368
aom_img_truncate_16_to_8(aom_image_t * dst,const aom_image_t * src)369 void aom_img_truncate_16_to_8(aom_image_t *dst, const aom_image_t *src) {
370 int plane;
371 if (dst->fmt + AOM_IMG_FMT_HIGHBITDEPTH != src->fmt || dst->d_w != src->d_w ||
372 dst->d_h != src->d_h || dst->x_chroma_shift != src->x_chroma_shift ||
373 dst->y_chroma_shift != src->y_chroma_shift) {
374 fatal("Unsupported image conversion");
375 }
376 switch (dst->fmt) {
377 case AOM_IMG_FMT_I420:
378 case AOM_IMG_FMT_I422:
379 case AOM_IMG_FMT_I444: break;
380 default: fatal("Unsupported image conversion"); break;
381 }
382 for (plane = 0; plane < 3; plane++) {
383 int w = src->d_w;
384 int h = src->d_h;
385 int x, y;
386 if (plane) {
387 w = (w + src->x_chroma_shift) >> src->x_chroma_shift;
388 h = (h + src->y_chroma_shift) >> src->y_chroma_shift;
389 }
390 for (y = 0; y < h; y++) {
391 const uint16_t *p_src =
392 (const uint16_t *)(src->planes[plane] + y * src->stride[plane]);
393 uint8_t *p_dst = dst->planes[plane] + y * dst->stride[plane];
394 for (x = 0; x < w; x++) {
395 *p_dst++ = (uint8_t)(*p_src++);
396 }
397 }
398 }
399 }
400
highbd_img_downshift(aom_image_t * dst,const aom_image_t * src,int down_shift)401 static void highbd_img_downshift(aom_image_t *dst, const aom_image_t *src,
402 int down_shift) {
403 int plane;
404 if (dst->d_w != src->d_w || dst->d_h != src->d_h ||
405 dst->x_chroma_shift != src->x_chroma_shift ||
406 dst->y_chroma_shift != src->y_chroma_shift || dst->fmt != src->fmt ||
407 down_shift < 0) {
408 fatal("Unsupported image conversion");
409 }
410 switch (src->fmt) {
411 case AOM_IMG_FMT_I42016:
412 case AOM_IMG_FMT_I42216:
413 case AOM_IMG_FMT_I44416: break;
414 default: fatal("Unsupported image conversion"); break;
415 }
416 for (plane = 0; plane < 3; plane++) {
417 int w = src->d_w;
418 int h = src->d_h;
419 int x, y;
420 if (plane) {
421 w = (w + src->x_chroma_shift) >> src->x_chroma_shift;
422 h = (h + src->y_chroma_shift) >> src->y_chroma_shift;
423 }
424 for (y = 0; y < h; y++) {
425 const uint16_t *p_src =
426 (const uint16_t *)(src->planes[plane] + y * src->stride[plane]);
427 uint16_t *p_dst =
428 (uint16_t *)(dst->planes[plane] + y * dst->stride[plane]);
429 for (x = 0; x < w; x++) *p_dst++ = *p_src++ >> down_shift;
430 }
431 }
432 }
433
lowbd_img_downshift(aom_image_t * dst,const aom_image_t * src,int down_shift)434 static void lowbd_img_downshift(aom_image_t *dst, const aom_image_t *src,
435 int down_shift) {
436 int plane;
437 if (dst->d_w != src->d_w || dst->d_h != src->d_h ||
438 dst->x_chroma_shift != src->x_chroma_shift ||
439 dst->y_chroma_shift != src->y_chroma_shift ||
440 src->fmt != dst->fmt + AOM_IMG_FMT_HIGHBITDEPTH || down_shift < 0) {
441 fatal("Unsupported image conversion");
442 }
443 switch (dst->fmt) {
444 case AOM_IMG_FMT_I420:
445 case AOM_IMG_FMT_I422:
446 case AOM_IMG_FMT_I444: break;
447 default: fatal("Unsupported image conversion"); break;
448 }
449 for (plane = 0; plane < 3; plane++) {
450 int w = src->d_w;
451 int h = src->d_h;
452 int x, y;
453 if (plane) {
454 w = (w + src->x_chroma_shift) >> src->x_chroma_shift;
455 h = (h + src->y_chroma_shift) >> src->y_chroma_shift;
456 }
457 for (y = 0; y < h; y++) {
458 const uint16_t *p_src =
459 (const uint16_t *)(src->planes[plane] + y * src->stride[plane]);
460 uint8_t *p_dst = dst->planes[plane] + y * dst->stride[plane];
461 for (x = 0; x < w; x++) {
462 *p_dst++ = *p_src++ >> down_shift;
463 }
464 }
465 }
466 }
467
aom_img_downshift(aom_image_t * dst,const aom_image_t * src,int down_shift)468 void aom_img_downshift(aom_image_t *dst, const aom_image_t *src,
469 int down_shift) {
470 if (dst->fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
471 highbd_img_downshift(dst, src, down_shift);
472 } else {
473 lowbd_img_downshift(dst, src, down_shift);
474 }
475 }
476
img_shifted_realloc_required(const aom_image_t * img,const aom_image_t * shifted,aom_img_fmt_t required_fmt)477 static int img_shifted_realloc_required(const aom_image_t *img,
478 const aom_image_t *shifted,
479 aom_img_fmt_t required_fmt) {
480 return img->d_w != shifted->d_w || img->d_h != shifted->d_h ||
481 required_fmt != shifted->fmt;
482 }
483
aom_shift_img(unsigned int output_bit_depth,aom_image_t ** img_ptr,aom_image_t ** img_shifted_ptr)484 bool aom_shift_img(unsigned int output_bit_depth, aom_image_t **img_ptr,
485 aom_image_t **img_shifted_ptr) {
486 aom_image_t *img = *img_ptr;
487 aom_image_t *img_shifted = *img_shifted_ptr;
488
489 const aom_img_fmt_t shifted_fmt = output_bit_depth == 8
490 ? img->fmt & ~AOM_IMG_FMT_HIGHBITDEPTH
491 : img->fmt | AOM_IMG_FMT_HIGHBITDEPTH;
492
493 if (shifted_fmt != img->fmt || output_bit_depth != img->bit_depth) {
494 if (img_shifted &&
495 img_shifted_realloc_required(img, img_shifted, shifted_fmt)) {
496 aom_img_free(img_shifted);
497 img_shifted = NULL;
498 }
499 if (img_shifted) {
500 img_shifted->monochrome = img->monochrome;
501 }
502 if (!img_shifted) {
503 img_shifted = aom_img_alloc(NULL, shifted_fmt, img->d_w, img->d_h, 16);
504 if (!img_shifted) {
505 *img_shifted_ptr = NULL;
506 return false;
507 }
508 img_shifted->bit_depth = output_bit_depth;
509 img_shifted->monochrome = img->monochrome;
510 img_shifted->csp = img->csp;
511 }
512 if (output_bit_depth > img->bit_depth) {
513 aom_img_upshift(img_shifted, img, output_bit_depth - img->bit_depth);
514 } else {
515 aom_img_downshift(img_shifted, img, img->bit_depth - output_bit_depth);
516 }
517 *img_shifted_ptr = img_shifted;
518 *img_ptr = img_shifted;
519 }
520
521 return true;
522 }
523
524 // Related to I420, NV12 format has one luma "luminance" plane Y and one plane
525 // with U and V values interleaved.
aom_img_write_nv12(const aom_image_t * img,FILE * file)526 void aom_img_write_nv12(const aom_image_t *img, FILE *file) {
527 // Y plane
528 const unsigned char *buf = img->planes[0];
529 int stride = img->stride[0];
530 int w = aom_img_plane_width(img, 0) *
531 ((img->fmt & AOM_IMG_FMT_HIGHBITDEPTH) ? 2 : 1);
532 int h = aom_img_plane_height(img, 0);
533 int x, y;
534
535 for (y = 0; y < h; ++y) {
536 fwrite(buf, 1, w, file);
537 buf += stride;
538 }
539
540 // Interleaved U and V plane
541 const unsigned char *ubuf = img->planes[1];
542 const unsigned char *vbuf = img->planes[2];
543 const size_t size = (img->fmt & AOM_IMG_FMT_HIGHBITDEPTH) ? 2 : 1;
544 stride = img->stride[1];
545 w = aom_img_plane_width(img, 1);
546 h = aom_img_plane_height(img, 1);
547
548 for (y = 0; y < h; ++y) {
549 for (x = 0; x < w; ++x) {
550 fwrite(ubuf, size, 1, file);
551 fwrite(vbuf, size, 1, file);
552 ubuf += size;
553 vbuf += size;
554 }
555 ubuf += (stride - w * size);
556 vbuf += (stride - w * size);
557 }
558 }
559
read_from_input(struct AvxInputContext * input_ctx,size_t n,unsigned char * buf)560 size_t read_from_input(struct AvxInputContext *input_ctx, size_t n,
561 unsigned char *buf) {
562 const size_t buffered_bytes =
563 input_ctx->detect.buf_read - input_ctx->detect.position;
564 size_t read_n;
565 if (buffered_bytes == 0) {
566 read_n = fread(buf, 1, n, input_ctx->file);
567 } else if (n <= buffered_bytes) {
568 memcpy(buf, input_ctx->detect.buf + input_ctx->detect.position, n);
569 input_ctx->detect.position += n;
570 read_n = n;
571 } else {
572 memcpy(buf, input_ctx->detect.buf + input_ctx->detect.position,
573 buffered_bytes);
574 input_ctx->detect.position += buffered_bytes;
575 read_n = buffered_bytes;
576 read_n +=
577 fread(buf + buffered_bytes, 1, n - buffered_bytes, input_ctx->file);
578 }
579 return read_n;
580 }
581
input_to_detect_buf(struct AvxInputContext * input_ctx,size_t n)582 size_t input_to_detect_buf(struct AvxInputContext *input_ctx, size_t n) {
583 if (n + input_ctx->detect.position > DETECT_BUF_SZ) {
584 die("Failed to store in the detect buffer, maximum size exceeded.");
585 }
586 const size_t buffered_bytes =
587 input_ctx->detect.buf_read - input_ctx->detect.position;
588 size_t read_n;
589 if (buffered_bytes == 0) {
590 read_n = fread(input_ctx->detect.buf + input_ctx->detect.buf_read, 1, n,
591 input_ctx->file);
592 input_ctx->detect.buf_read += read_n;
593 } else if (n <= buffered_bytes) {
594 // In this case, don't need to do anything as the data is already in
595 // the detect buffer
596 read_n = n;
597 } else {
598 read_n = fread(input_ctx->detect.buf + input_ctx->detect.buf_read, 1,
599 n - buffered_bytes, input_ctx->file);
600 input_ctx->detect.buf_read += read_n;
601 read_n += buffered_bytes;
602 }
603 return read_n;
604 }
605
606 // Read from detect buffer to a buffer. If not enough, read from input and also
607 // buffer them first.
buffer_input(struct AvxInputContext * input_ctx,size_t n,unsigned char * buf,bool buffered)608 size_t buffer_input(struct AvxInputContext *input_ctx, size_t n,
609 unsigned char *buf, bool buffered) {
610 if (!buffered) {
611 return read_from_input(input_ctx, n, buf);
612 }
613 const size_t buf_n = input_to_detect_buf(input_ctx, n);
614 if (buf_n < n) {
615 return buf_n;
616 }
617 return read_from_input(input_ctx, n, buf);
618 }
619
rewind_detect(struct AvxInputContext * input_ctx)620 void rewind_detect(struct AvxInputContext *input_ctx) {
621 input_ctx->detect.position = 0;
622 }
623
input_eof(struct AvxInputContext * input_ctx)624 bool input_eof(struct AvxInputContext *input_ctx) {
625 return feof(input_ctx->file) &&
626 input_ctx->detect.position == input_ctx->detect.buf_read;
627 }
628