summaryrefslogtreecommitdiffstats
path: root/third_party/jpeg-xl/lib/jpegli/input_suspension_test.cc
blob: dc5aee2fc5cc49760dfa774bc382925df25bee6a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
// Copyright (c) the JPEG XL Project Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

#include <jxl/types.h>

#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <ostream>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

#include "lib/jpegli/decode.h"
#include "lib/jpegli/libjpeg_test_util.h"
#include "lib/jpegli/test_params.h"
#include "lib/jpegli/test_utils.h"
#include "lib/jpegli/testing.h"
#include "lib/jxl/base/status.h"

namespace jpegli {
namespace {

constexpr uint8_t kFakeEoiMarker[2] = {0xff, 0xd9};

struct SourceManager {
  SourceManager(const uint8_t* data, size_t len, size_t max_chunk_size,
                bool is_partial_file)
      : data_(data),
        len_(len),
        pos_(0),
        max_chunk_size_(max_chunk_size),
        is_partial_file_(is_partial_file) {
    pub_.init_source = init_source;
    pub_.fill_input_buffer = fill_input_buffer;
    pub_.next_input_byte = nullptr;
    pub_.bytes_in_buffer = 0;
    pub_.skip_input_data = skip_input_data;
    pub_.resync_to_restart = jpegli_resync_to_restart;
    pub_.term_source = term_source;
    if (max_chunk_size_ == 0) max_chunk_size_ = len;
  }

  ~SourceManager() {
    EXPECT_EQ(0, pub_.bytes_in_buffer);
    if (!is_partial_file_) {
      EXPECT_EQ(len_, pos_);
    }
  }

  bool LoadNextChunk() {
    if (pos_ >= len_ && !is_partial_file_) {
      return false;
    }
    if (pub_.bytes_in_buffer > 0) {
      EXPECT_LE(pub_.bytes_in_buffer, buffer_.size());
      memmove(buffer_.data(), pub_.next_input_byte, pub_.bytes_in_buffer);
    }
    size_t chunk_size =
        pos_ < len_ ? std::min(len_ - pos_, max_chunk_size_) : 2;
    buffer_.resize(pub_.bytes_in_buffer + chunk_size);
    memcpy(&buffer_[pub_.bytes_in_buffer],
           pos_ < len_ ? data_ + pos_ : kFakeEoiMarker, chunk_size);
    pub_.next_input_byte = buffer_.data();
    pub_.bytes_in_buffer += chunk_size;
    pos_ += chunk_size;
    return true;
  }

 private:
  jpeg_source_mgr pub_;
  std::vector<uint8_t> buffer_;
  const uint8_t* data_;
  size_t len_;
  size_t pos_;
  size_t max_chunk_size_;
  bool is_partial_file_;

  static void init_source(j_decompress_ptr cinfo) {
    auto* src = reinterpret_cast<SourceManager*>(cinfo->src);
    src->pub_.next_input_byte = nullptr;
    src->pub_.bytes_in_buffer = 0;
  }

  static boolean fill_input_buffer(j_decompress_ptr cinfo) { return FALSE; }

  static void skip_input_data(j_decompress_ptr cinfo,
                              long num_bytes /* NOLINT*/) {
    auto* src = reinterpret_cast<SourceManager*>(cinfo->src);
    if (num_bytes <= 0) {
      return;
    }
    if (src->pub_.bytes_in_buffer >= static_cast<size_t>(num_bytes)) {
      src->pub_.bytes_in_buffer -= num_bytes;
      src->pub_.next_input_byte += num_bytes;
    } else {
      src->pos_ += num_bytes - src->pub_.bytes_in_buffer;
      src->pub_.bytes_in_buffer = 0;
    }
  }

  static void term_source(j_decompress_ptr cinfo) {}
};

uint8_t markers_seen[kMarkerSequenceLen];
size_t num_markers_seen = 0;

uint8_t get_next_byte(j_decompress_ptr cinfo) {
  cinfo->src->bytes_in_buffer--;
  return *cinfo->src->next_input_byte++;
}

boolean test_marker_processor(j_decompress_ptr cinfo) {
  markers_seen[num_markers_seen] = cinfo->unread_marker;
  if (cinfo->src->bytes_in_buffer < 2) {
    return FALSE;
  }
  size_t marker_len = (get_next_byte(cinfo) << 8) + get_next_byte(cinfo);
  EXPECT_EQ(2 + ((num_markers_seen + 2) % sizeof(kMarkerData)), marker_len);
  if (marker_len > 2) {
    (*cinfo->src->skip_input_data)(cinfo, marker_len - 2);
  }
  ++num_markers_seen;
  return TRUE;
}

void ReadOutputImage(const DecompressParams& dparams, j_decompress_ptr cinfo,
                     SourceManager* src, TestImage* output) {
  output->ysize = cinfo->output_height;
  output->xsize = cinfo->output_width;
  output->components = cinfo->num_components;
  if (cinfo->raw_data_out) {
    output->color_space = cinfo->jpeg_color_space;
    for (int c = 0; c < cinfo->num_components; ++c) {
      size_t xsize = cinfo->comp_info[c].width_in_blocks * DCTSIZE;
      size_t ysize = cinfo->comp_info[c].height_in_blocks * DCTSIZE;
      std::vector<uint8_t> plane(ysize * xsize);
      output->raw_data.emplace_back(std::move(plane));
    }
  } else {
    output->color_space = cinfo->out_color_space;
    output->AllocatePixels();
  }
  size_t total_output_lines = 0;
  while (cinfo->output_scanline < cinfo->output_height) {
    size_t max_lines;
    size_t num_output_lines;
    if (cinfo->raw_data_out) {
      size_t iMCU_height = cinfo->max_v_samp_factor * DCTSIZE;
      EXPECT_EQ(cinfo->output_scanline, cinfo->output_iMCU_row * iMCU_height);
      max_lines = iMCU_height;
      std::vector<std::vector<JSAMPROW>> rowdata(cinfo->num_components);
      std::vector<JSAMPARRAY> data(cinfo->num_components);
      for (int c = 0; c < cinfo->num_components; ++c) {
        size_t xsize = cinfo->comp_info[c].width_in_blocks * DCTSIZE;
        size_t ysize = cinfo->comp_info[c].height_in_blocks * DCTSIZE;
        size_t num_lines = cinfo->comp_info[c].v_samp_factor * DCTSIZE;
        rowdata[c].resize(num_lines);
        size_t y0 = cinfo->output_iMCU_row * num_lines;
        for (size_t i = 0; i < num_lines; ++i) {
          rowdata[c][i] =
              y0 + i < ysize ? &output->raw_data[c][(y0 + i) * xsize] : nullptr;
        }
        data[c] = rowdata[c].data();
      }
      while ((num_output_lines =
                  jpegli_read_raw_data(cinfo, data.data(), max_lines)) == 0) {
        JXL_CHECK(src && src->LoadNextChunk());
      }
    } else {
      size_t max_output_lines = dparams.max_output_lines;
      if (max_output_lines == 0) max_output_lines = cinfo->output_height;
      size_t lines_left = cinfo->output_height - cinfo->output_scanline;
      max_lines = std::min<size_t>(max_output_lines, lines_left);
      size_t stride = cinfo->output_width * cinfo->num_components;
      std::vector<JSAMPROW> scanlines(max_lines);
      for (size_t i = 0; i < max_lines; ++i) {
        size_t yidx = cinfo->output_scanline + i;
        scanlines[i] = &output->pixels[yidx * stride];
      }
      while ((num_output_lines = jpegli_read_scanlines(cinfo, scanlines.data(),
                                                       max_lines)) == 0) {
        JXL_CHECK(src && src->LoadNextChunk());
      }
    }
    total_output_lines += num_output_lines;
    EXPECT_EQ(total_output_lines, cinfo->output_scanline);
    if (num_output_lines < max_lines) {
      JXL_CHECK(src && src->LoadNextChunk());
    }
  }
}

struct TestConfig {
  std::string fn;
  std::string fn_desc;
  TestImage input;
  CompressParams jparams;
  DecompressParams dparams;
  float max_rms_dist = 1.0f;
};

std::vector<uint8_t> GetTestJpegData(TestConfig& config) {
  if (!config.fn.empty()) {
    return ReadTestData(config.fn);
  }
  GeneratePixels(&config.input);
  std::vector<uint8_t> compressed;
  JXL_CHECK(EncodeWithJpegli(config.input, config.jparams, &compressed));
  return compressed;
}

bool IsSequential(const TestConfig& config) {
  if (!config.fn.empty()) {
    return config.fn_desc.find("PROGR") == std::string::npos;
  }
  return config.jparams.progressive_mode <= 0;
}

class InputSuspensionTestParam : public ::testing::TestWithParam<TestConfig> {};

TEST_P(InputSuspensionTestParam, InputOutputLockStepNonBuffered) {
  TestConfig config = GetParam();
  const DecompressParams& dparams = config.dparams;
  std::vector<uint8_t> compressed = GetTestJpegData(config);
  bool is_partial = config.dparams.size_factor < 1.0f;
  if (is_partial) {
    compressed.resize(compressed.size() * config.dparams.size_factor);
  }
  SourceManager src(compressed.data(), compressed.size(), dparams.chunk_size,
                    is_partial);
  TestImage output0;
  jpeg_decompress_struct cinfo;
  const auto try_catch_block = [&]() -> bool {
    ERROR_HANDLER_SETUP(jpegli);
    jpegli_create_decompress(&cinfo);
    cinfo.src = reinterpret_cast<jpeg_source_mgr*>(&src);

    if (config.jparams.add_marker) {
      jpegli_save_markers(&cinfo, kSpecialMarker0, 0xffff);
      jpegli_save_markers(&cinfo, kSpecialMarker1, 0xffff);
      num_markers_seen = 0;
      jpegli_set_marker_processor(&cinfo, 0xe6, test_marker_processor);
      jpegli_set_marker_processor(&cinfo, 0xe7, test_marker_processor);
      jpegli_set_marker_processor(&cinfo, 0xe8, test_marker_processor);
    }
    while (jpegli_read_header(&cinfo, TRUE) == JPEG_SUSPENDED) {
      JXL_CHECK(src.LoadNextChunk());
    }
    SetDecompressParams(dparams, &cinfo);
    jpegli_set_output_format(&cinfo, dparams.data_type, dparams.endianness);
    if (config.jparams.add_marker) {
      EXPECT_EQ(num_markers_seen, kMarkerSequenceLen);
      EXPECT_EQ(0, memcmp(markers_seen, kMarkerSequence, num_markers_seen));
    }
    VerifyHeader(config.jparams, &cinfo);
    cinfo.raw_data_out = TO_JXL_BOOL(dparams.output_mode == RAW_DATA);

    if (dparams.output_mode == COEFFICIENTS) {
      jvirt_barray_ptr* coef_arrays;
      while ((coef_arrays = jpegli_read_coefficients(&cinfo)) == nullptr) {
        JXL_CHECK(src.LoadNextChunk());
      }
      CopyCoefficients(&cinfo, coef_arrays, &output0);
    } else {
      while (!jpegli_start_decompress(&cinfo)) {
        JXL_CHECK(src.LoadNextChunk());
      }
      ReadOutputImage(dparams, &cinfo, &src, &output0);
    }

    while (!jpegli_finish_decompress(&cinfo)) {
      JXL_CHECK(src.LoadNextChunk());
    }
    return true;
  };
  ASSERT_TRUE(try_catch_block());
  jpegli_destroy_decompress(&cinfo);

  TestImage output1;
  DecodeWithLibjpeg(config.jparams, dparams, compressed, &output1);
  VerifyOutputImage(output1, output0, config.max_rms_dist);
}

TEST_P(InputSuspensionTestParam, InputOutputLockStepBuffered) {
  TestConfig config = GetParam();
  if (config.jparams.add_marker) return;
  const DecompressParams& dparams = config.dparams;
  std::vector<uint8_t> compressed = GetTestJpegData(config);
  bool is_partial = config.dparams.size_factor < 1.0f;
  if (is_partial) {
    compressed.resize(compressed.size() * config.dparams.size_factor);
  }
  SourceManager src(compressed.data(), compressed.size(), dparams.chunk_size,
                    is_partial);
  std::vector<TestImage> output_progression0;
  jpeg_decompress_struct cinfo;
  const auto try_catch_block = [&]() -> bool {
    ERROR_HANDLER_SETUP(jpegli);
    jpegli_create_decompress(&cinfo);

    cinfo.src = reinterpret_cast<jpeg_source_mgr*>(&src);

    while (jpegli_read_header(&cinfo, TRUE) == JPEG_SUSPENDED) {
      JXL_CHECK(src.LoadNextChunk());
    }
    SetDecompressParams(dparams, &cinfo);
    jpegli_set_output_format(&cinfo, dparams.data_type, dparams.endianness);

    cinfo.buffered_image = TRUE;
    cinfo.raw_data_out = TO_JXL_BOOL(dparams.output_mode == RAW_DATA);

    EXPECT_TRUE(jpegli_start_decompress(&cinfo));
    EXPECT_FALSE(jpegli_input_complete(&cinfo));
    EXPECT_EQ(0, cinfo.output_scan_number);

    int sos_marker_cnt = 1;  // read_header reads the first SOS marker
    while (!jpegli_input_complete(&cinfo)) {
      EXPECT_EQ(cinfo.input_scan_number, sos_marker_cnt);
      EXPECT_TRUE(jpegli_start_output(&cinfo, cinfo.input_scan_number));
      // start output sets output_scan_number, but does not change
      // input_scan_number
      EXPECT_EQ(cinfo.output_scan_number, cinfo.input_scan_number);
      EXPECT_EQ(cinfo.input_scan_number, sos_marker_cnt);
      TestImage output;
      ReadOutputImage(dparams, &cinfo, &src, &output);
      output_progression0.emplace_back(std::move(output));
      // read scanlines/read raw data does not change input/output scan number
      EXPECT_EQ(cinfo.input_scan_number, sos_marker_cnt);
      EXPECT_EQ(cinfo.output_scan_number, cinfo.input_scan_number);
      while (!jpegli_finish_output(&cinfo)) {
        JXL_CHECK(src.LoadNextChunk());
      }
      ++sos_marker_cnt;  // finish output reads the next SOS marker or EOI
      if (dparams.output_mode == COEFFICIENTS) {
        jvirt_barray_ptr* coef_arrays = jpegli_read_coefficients(&cinfo);
        JXL_CHECK(coef_arrays != nullptr);
        CopyCoefficients(&cinfo, coef_arrays, &output_progression0.back());
      }
    }

    EXPECT_TRUE(jpegli_finish_decompress(&cinfo));
    return true;
  };
  ASSERT_TRUE(try_catch_block());
  jpegli_destroy_decompress(&cinfo);

  std::vector<TestImage> output_progression1;
  DecodeAllScansWithLibjpeg(config.jparams, dparams, compressed,
                            &output_progression1);
  ASSERT_EQ(output_progression0.size(), output_progression1.size());
  for (size_t i = 0; i < output_progression0.size(); ++i) {
    const TestImage& output = output_progression0[i];
    const TestImage& expected = output_progression1[i];
    VerifyOutputImage(expected, output, config.max_rms_dist);
  }
}

TEST_P(InputSuspensionTestParam, PreConsumeInputBuffered) {
  TestConfig config = GetParam();
  if (config.jparams.add_marker) return;
  const DecompressParams& dparams = config.dparams;
  std::vector<uint8_t> compressed = GetTestJpegData(config);
  bool is_partial = config.dparams.size_factor < 1.0f;
  if (is_partial) {
    compressed.resize(compressed.size() * config.dparams.size_factor);
  }
  std::vector<TestImage> output_progression1;
  DecodeAllScansWithLibjpeg(config.jparams, dparams, compressed,
                            &output_progression1);
  SourceManager src(compressed.data(), compressed.size(), dparams.chunk_size,
                    is_partial);
  TestImage output0;
  jpeg_decompress_struct cinfo;
  const auto try_catch_block = [&]() -> bool {
    ERROR_HANDLER_SETUP(jpegli);
    jpegli_create_decompress(&cinfo);
    cinfo.src = reinterpret_cast<jpeg_source_mgr*>(&src);

    int status;
    while ((status = jpegli_consume_input(&cinfo)) != JPEG_REACHED_SOS) {
      if (status == JPEG_SUSPENDED) {
        JXL_CHECK(src.LoadNextChunk());
      }
    }
    EXPECT_EQ(JPEG_REACHED_SOS, jpegli_consume_input(&cinfo));
    cinfo.buffered_image = TRUE;
    cinfo.raw_data_out = TO_JXL_BOOL(dparams.output_mode == RAW_DATA);
    cinfo.do_block_smoothing = TO_JXL_BOOL(dparams.do_block_smoothing);

    EXPECT_TRUE(jpegli_start_decompress(&cinfo));
    EXPECT_FALSE(jpegli_input_complete(&cinfo));
    EXPECT_EQ(1, cinfo.input_scan_number);
    EXPECT_EQ(0, cinfo.output_scan_number);

    while ((status = jpegli_consume_input(&cinfo)) != JPEG_REACHED_EOI) {
      if (status == JPEG_SUSPENDED) {
        JXL_CHECK(src.LoadNextChunk());
      }
    }

    EXPECT_TRUE(jpegli_input_complete(&cinfo));
    EXPECT_EQ(output_progression1.size(), cinfo.input_scan_number);
    EXPECT_EQ(0, cinfo.output_scan_number);

    EXPECT_TRUE(jpegli_start_output(&cinfo, cinfo.input_scan_number));
    EXPECT_EQ(output_progression1.size(), cinfo.input_scan_number);
    EXPECT_EQ(cinfo.output_scan_number, cinfo.input_scan_number);

    ReadOutputImage(dparams, &cinfo, nullptr, &output0);
    EXPECT_EQ(output_progression1.size(), cinfo.input_scan_number);
    EXPECT_EQ(cinfo.output_scan_number, cinfo.input_scan_number);

    EXPECT_TRUE(jpegli_finish_output(&cinfo));
    if (dparams.output_mode == COEFFICIENTS) {
      jvirt_barray_ptr* coef_arrays = jpegli_read_coefficients(&cinfo);
      JXL_CHECK(coef_arrays != nullptr);
      CopyCoefficients(&cinfo, coef_arrays, &output0);
    }
    EXPECT_TRUE(jpegli_finish_decompress(&cinfo));
    return true;
  };
  ASSERT_TRUE(try_catch_block());
  jpegli_destroy_decompress(&cinfo);

  VerifyOutputImage(output_progression1.back(), output0, config.max_rms_dist);
}

TEST_P(InputSuspensionTestParam, PreConsumeInputNonBuffered) {
  TestConfig config = GetParam();
  if (config.jparams.add_marker || IsSequential(config)) return;
  const DecompressParams& dparams = config.dparams;
  std::vector<uint8_t> compressed = GetTestJpegData(config);
  bool is_partial = config.dparams.size_factor < 1.0f;
  if (is_partial) {
    compressed.resize(compressed.size() * config.dparams.size_factor);
  }
  SourceManager src(compressed.data(), compressed.size(), dparams.chunk_size,
                    is_partial);
  TestImage output0;
  jpeg_decompress_struct cinfo;
  const auto try_catch_block = [&]() -> bool {
    ERROR_HANDLER_SETUP(jpegli);
    jpegli_create_decompress(&cinfo);
    cinfo.src = reinterpret_cast<jpeg_source_mgr*>(&src);

    int status;
    while ((status = jpegli_consume_input(&cinfo)) != JPEG_REACHED_SOS) {
      if (status == JPEG_SUSPENDED) {
        JXL_CHECK(src.LoadNextChunk());
      }
    }
    EXPECT_EQ(JPEG_REACHED_SOS, jpegli_consume_input(&cinfo));
    cinfo.raw_data_out = TO_JXL_BOOL(dparams.output_mode == RAW_DATA);
    cinfo.do_block_smoothing = TO_JXL_BOOL(dparams.do_block_smoothing);

    if (dparams.output_mode == COEFFICIENTS) {
      jpegli_read_coefficients(&cinfo);
    } else {
      while (!jpegli_start_decompress(&cinfo)) {
        JXL_CHECK(src.LoadNextChunk());
      }
    }

    while ((status = jpegli_consume_input(&cinfo)) != JPEG_REACHED_EOI) {
      if (status == JPEG_SUSPENDED) {
        JXL_CHECK(src.LoadNextChunk());
      }
    }

    if (dparams.output_mode == COEFFICIENTS) {
      jvirt_barray_ptr* coef_arrays = jpegli_read_coefficients(&cinfo);
      JXL_CHECK(coef_arrays != nullptr);
      CopyCoefficients(&cinfo, coef_arrays, &output0);
    } else {
      ReadOutputImage(dparams, &cinfo, nullptr, &output0);
    }

    EXPECT_TRUE(jpegli_finish_decompress(&cinfo));
    return true;
  };
  ASSERT_TRUE(try_catch_block());
  jpegli_destroy_decompress(&cinfo);

  TestImage output1;
  DecodeWithLibjpeg(config.jparams, dparams, compressed, &output1);
  VerifyOutputImage(output1, output0, config.max_rms_dist);
}

std::vector<TestConfig> GenerateTests() {
  std::vector<TestConfig> all_tests;
  std::vector<std::pair<std::string, std::string>> testfiles({
      {"jxl/flower/flower.png.im_q85_444.jpg", "Q85YUV444"},
      {"jxl/flower/flower.png.im_q85_420_R13B.jpg", "Q85YUV420R13B"},
      {"jxl/flower/flower.png.im_q85_420_progr.jpg", "Q85YUV420PROGR"},
  });
  for (const auto& it : testfiles) {
    for (size_t chunk_size : {1, 64, 65536}) {
      for (size_t max_output_lines : {0, 1, 8, 16}) {
        TestConfig config;
        config.fn = it.first;
        config.fn_desc = it.second;
        config.dparams.chunk_size = chunk_size;
        config.dparams.max_output_lines = max_output_lines;
        all_tests.push_back(config);
        if (max_output_lines == 16) {
          config.dparams.output_mode = RAW_DATA;
          all_tests.push_back(config);
          config.dparams.output_mode = COEFFICIENTS;
          all_tests.push_back(config);
        }
      }
    }
  }
  for (size_t r : {1, 17, 1024}) {
    for (size_t chunk_size : {1, 65536}) {
      TestConfig config;
      config.dparams.chunk_size = chunk_size;
      config.jparams.progressive_mode = 2;
      config.jparams.restart_interval = r;
      all_tests.push_back(config);
    }
  }
  for (size_t chunk_size : {1, 4, 1024}) {
    TestConfig config;
    config.input.xsize = 256;
    config.input.ysize = 256;
    config.dparams.chunk_size = chunk_size;
    config.jparams.add_marker = true;
    all_tests.push_back(config);
  }
  // Tests for partial input.
  for (float size_factor : {0.1f, 0.33f, 0.5f, 0.75f}) {
    for (int progr : {0, 1, 3}) {
      for (int samp : {1, 2}) {
        for (JpegIOMode output_mode : {PIXELS, RAW_DATA}) {
          TestConfig config;
          config.input.xsize = 517;
          config.input.ysize = 523;
          config.jparams.h_sampling = {samp, 1, 1};
          config.jparams.v_sampling = {samp, 1, 1};
          config.jparams.progressive_mode = progr;
          config.dparams.size_factor = size_factor;
          config.dparams.output_mode = output_mode;
          // The last partially available block can behave differently.
          // TODO(szabadka) Figure out if we can make the behaviour more
          // similar.
          config.max_rms_dist = samp == 1 ? 1.75f : 3.0f;
          all_tests.push_back(config);
        }
      }
    }
  }
  // Tests for block smoothing.
  for (float size_factor : {0.1f, 0.33f, 0.5f, 0.75f, 1.0f}) {
    for (int samp : {1, 2}) {
      TestConfig config;
      config.input.xsize = 517;
      config.input.ysize = 523;
      config.jparams.h_sampling = {samp, 1, 1};
      config.jparams.v_sampling = {samp, 1, 1};
      config.jparams.progressive_mode = 2;
      config.dparams.size_factor = size_factor;
      config.dparams.do_block_smoothing = true;
      // libjpeg does smoothing for incomplete scans differently at
      // the border between current and previous scans.
      config.max_rms_dist = 8.0f;
      all_tests.push_back(config);
    }
  }
  return all_tests;
}

std::ostream& operator<<(std::ostream& os, const TestConfig& c) {
  if (!c.fn.empty()) {
    os << c.fn_desc;
  } else {
    os << c.input;
  }
  os << c.jparams;
  if (c.dparams.chunk_size == 0) {
    os << "CompleteInput";
  } else {
    os << "InputChunks" << c.dparams.chunk_size;
  }
  if (c.dparams.size_factor < 1.0f) {
    os << "Partial" << static_cast<int>(c.dparams.size_factor * 100) << "p";
  }
  if (c.dparams.max_output_lines == 0) {
    os << "CompleteOutput";
  } else {
    os << "OutputLines" << c.dparams.max_output_lines;
  }
  if (c.dparams.output_mode == RAW_DATA) {
    os << "RawDataOut";
  } else if (c.dparams.output_mode == COEFFICIENTS) {
    os << "CoeffsOut";
  }
  if (c.dparams.do_block_smoothing) {
    os << "BlockSmoothing";
  }
  return os;
}

std::string TestDescription(
    const testing::TestParamInfo<InputSuspensionTestParam::ParamType>& info) {
  std::stringstream name;
  name << info.param;
  return name.str();
}

JPEGLI_INSTANTIATE_TEST_SUITE_P(InputSuspensionTest, InputSuspensionTestParam,
                                testing::ValuesIn(GenerateTests()),
                                TestDescription);

}  // namespace
}  // namespace jpegli