FFmpeg
Loading...
Searching...
No Matches
qpfile_parser.c
Go to the documentation of this file.
1/*
2 * Copyright 2026 Lusoris
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or modify it under
7 * the terms of the GNU Lesser General Public License as published by the
8 * Free Software Foundation; either version 2.1 of the License, or (at
9 * your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14 * License for more details.
15 */
16
17#include <stdio.h>
18#include <string.h>
19
20#include "libavutil/error.h"
21#include "libavutil/macros.h"
24
25typedef struct FixtureCase {
26 const char *name;
27 const char *bytes;
28 size_t len;
29 void (*check)(const FFQpFile *qf);
31
32/* Return the short symbolic name of an error code. */
33static const char *err_name(int err)
34{
35 if (!err)
36 return "ok";
37 if (err == AVERROR(EINVAL))
38 return "EINVAL";
39 if (err == AVERROR(ERANGE))
40 return "ERANGE";
41 if (err == AVERROR(ENOMEM))
42 return "ENOMEM";
43 return "error";
44}
45
46/* Print the frame index and block offsets for one frame. */
47static void print_frame(const char *name, unsigned idx, const FFQpFrame *f)
48{
49 unsigned total = f->block_rows * f->block_cols;
50 printf("%s frame %u: index %u offsets", name, idx, f->frame_index);
51 for (unsigned i = 0; i < total; i++)
52 printf(" %d", (int)f->block_offsets[i]);
53 printf("\n");
54}
55
56/* Run additional validation assertions on the valid fixture. */
57static void check_valid(const FFQpFile *qf)
58{
59 static const unsigned probe_indices[] = { 1, 2, 4, 5, 100 };
60 int vals[8];
61 unsigned n_vals;
62 int err;
63
64 print_frame("valid", 0, &qf->frames[0]);
65 print_frame("valid", 1, &qf->frames[1]);
66
67 err = ff_qpfile_check_grid(NULL, "test", qf, 32, 16);
68 printf("valid check_grid 32x16: %s\n", err_name(err));
69 err = ff_qpfile_check_grid(NULL, "test", qf, 48, 16);
70 printf("valid check_grid 48x16: %s\n", err_name(err));
71
72 for (unsigned i = 0; i < 5; i++) {
73 const FFQpFrame *f = ff_qpfile_frame_for_index(qf, probe_indices[i]);
74 if (f)
75 printf("valid frame_for_index %u: %u\n", probe_indices[i], f->frame_index);
76 else
77 printf("valid frame_for_index %u: none\n", probe_indices[i]);
78 }
79
80 n_vals = ff_qpfile_segment_values(&qf->frames[1], vals, 8);
81 printf("valid segment_values 1:");
82 for (unsigned i = 0; i < n_vals; i++)
83 printf(" %d", vals[i]);
84 printf("\n");
85}
86
87/* Run additional validation assertions on the clip fixture. */
88static void check_clip(const FFQpFile *qf)
89{
90 print_frame("clip", 0, &qf->frames[0]);
91}
92
93/* Run additional validation assertions on the header-only fixture. */
94static void check_header_only(const FFQpFile *qf)
95{
96 int err = ff_qpfile_check_grid(NULL, "test", qf, 32, 16);
97 printf("header-only check_grid 32x16: %s\n", err_name(err));
98}
99
100/* Print lookup results across populated, clearing, and resumed records. */
101static void check_clearing(const FFQpFile *qf)
102{
103 static const unsigned probes[] = { 1, 2, 4, 5, 7, 8, 100 };
104
105 for (unsigned i = 0; i < FF_ARRAY_ELEMS(probes); i++) {
106 const FFQpFrame *f = ff_qpfile_frame_for_index(qf, probes[i]);
107
108 if (!f)
109 printf("clearing lookup %u: none\n", probes[i]);
110 else if (!f->block_offsets)
111 printf("clearing lookup %u: index %u clear\n",
112 probes[i], f->frame_index);
113 else
114 print_frame("clearing lookup", probes[i], f);
115 }
116}
117
118/* Run additional validation assertions on the index-max fixture. */
119static void check_index_max(const FFQpFile *qf)
120{
121 print_frame("index-max", 0, &qf->frames[0]);
122}
123
124/* Write bytes to a file path, returning 0 on success or -1 on error. */
125static int write_fixture(const char *path, const char *bytes, size_t len)
126{
127 FILE *fp = fopen(path, "wb");
128 if (!fp)
129 return -1;
130 if (fwrite(bytes, 1, len, fp) != len) {
131 fclose(fp);
132 return -1;
133 }
134 if (fclose(fp) != 0)
135 return -1;
136 return 0;
137}
138
139/* Run one fixture test case against the temporary file path. */
140static int run_fixture_test(const char *path, const FixtureCase *tc)
141{
142 FFQpFile qf = { 0 };
143 int err, ret = 0;
144
145 if (write_fixture(path, tc->bytes, tc->len) < 0) {
146 printf("%s: cannot write fixture\n", tc->name);
147 remove(path);
148 return 1;
149 }
150 err = ff_qpfile_load(NULL, path, &qf);
151 if (!err) {
152 printf("%s: ok frames %u grid %ux%u\n", tc->name,
154 if (tc->check)
155 tc->check(&qf);
156 } else {
157 printf("%s: %s\n", tc->name, err_name(err));
158 }
159 ff_qpfile_free(&qf);
160 if (remove(path) != 0) {
161 printf("%s: cannot remove fixture\n", tc->name);
162 ret = 1;
163 }
164 return ret;
165}
166
167/* Test and print one palette calculation result. */
168static void test_palette(const char *label, const int8_t *vals, size_t n, unsigned budget)
169{
170 int out[32];
171 unsigned count = ff_qpfile_palette(vals, n, out, budget);
172 printf("%s:", label);
173 if (!count) {
174 printf(" (none)\n");
175 return;
176 }
177 for (unsigned i = 0; i < count; i++)
178 printf(" %d", out[i]);
179 printf("\n");
180}
181
182/* Run the suite of palette calculation checks. */
183static void test_palettes(void)
184{
185 static const int8_t p1[3] = { -12, 0, 12 };
186 int8_t p_all[25];
187
188 for (int i = 0; i < FF_ARRAY_ELEMS(p_all); i++)
189 p_all[i] = (int8_t)(-12 + i);
190
191 test_palette("palette -12 0 12 budget 8", p1, 3, 8);
192 test_palette("palette -12 0 12 budget 2", p1, 3, 2);
193 test_palette("palette -12..12 budget 8", p_all, 25, 8);
194 test_palette("palette empty budget 8", NULL, 0, 8);
195 test_palette("palette -12 0 12 budget 1", p1, 3, 1);
196}
197
198/* Test and print segment lookup-table indices for all offsets. */
199static void test_lut(void)
200{
201 static const int seg[3] = { -12, 0, 12 };
202 uint8_t lut[QPFILE_LUT_SIZE];
203 ff_qpfile_segment_lut(lut, seg, 3);
204 printf("lut -12 0 12:");
205 for (unsigned i = 0; i < QPFILE_LUT_SIZE; i++)
206 printf(" %u", (unsigned)lut[i]);
207 printf("\n");
208}
209
210/* Main entry point for the QP file parser regression test. */
211int main(void)
212{
213 static const FixtureCase cases[] = {
214 { "valid", " \t\n# comment\n2 I 26\n-12 12\n \t\n5 P 30\n3 -3\n",
215 sizeof(" \t\n# comment\n2 I 26\n-12 12\n \t\n5 P 30\n3 -3\n") - 1, check_valid },
216 { "crlf", "2 I 26\r\n-12 12\r\n5 P 30\r\n3 -3\r\n",
217 sizeof("2 I 26\r\n-12 12\r\n5 P 30\r\n3 -3\r\n") - 1, NULL },
218 { "clip", "0 I 26\n-20 20\n",
219 sizeof("0 I 26\n-20 20\n") - 1, check_clip },
220 { "header-only", "0 I 26\n3 P 26\n",
221 sizeof("0 I 26\n3 P 26\n") - 1, check_header_only },
222 { "clearing", "2 I 26\n1 2\n5 P 26\n8 B 26\n-3 3\n",
223 sizeof("2 I 26\n1 2\n5 P 26\n8 B 26\n-3 3\n") - 1, check_clearing },
224 { "index-max", "4294967295 I 26\n-1 1\n",
225 sizeof("4294967295 I 26\n-1 1\n") - 1, check_index_max },
226 { "index-over", "4294967296 I 26\n",
227 sizeof("4294967296 I 26\n") - 1, NULL },
228 { "not-increasing", "5 I 26\n5 P 26\n",
229 sizeof("5 I 26\n5 P 26\n") - 1, NULL },
230 { "no-record", "# only a comment\n\n",
231 sizeof("# only a comment\n\n") - 1, NULL },
232 { "nul", "0 I 26\n-12 12\0junk\n",
233 sizeof("0 I 26\n-12 12\0junk\n") - 1, NULL },
234 { "row-width", "0 I 26\n1 2\n3\n",
235 sizeof("0 I 26\n1 2\n3\n") - 1, NULL },
236 { "frame-width", "0 I 26\n1 2\n1 P 26\n3\n",
237 sizeof("0 I 26\n1 2\n1 P 26\n3\n") - 1, NULL },
238 { "frame-rows", "0 I 26\n1 2\n1 P 26\n3 4\n5 6\n",
239 sizeof("0 I 26\n1 2\n1 P 26\n3 4\n5 6\n") - 1, NULL },
240 };
241 char path[64];
242 unsigned seed = av_get_random_seed();
243 int ret = 0;
244
245 snprintf(path, sizeof(path), "ff-qpfile-test-%08x.txt", seed);
246 for (size_t i = 0; i < FF_ARRAY_ELEMS(cases); i++) {
247 if (run_fixture_test(path, &cases[i]) != 0)
248 ret = 1;
249 }
251 test_lut();
252 return ret;
253}
static FILE * out
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define f(width, name)
Definition cbs_vp8.c:236
#define NULL
Definition coverity.c:32
__device__ int printf(const char *,...)
error code definitions
char * path
Definition ffplay.c:385
int ret
Definition ffplay.c:391
const char * label
uint32_t av_get_random_seed(void)
Get a seed to use in conjunction with random functions.
#define AVERROR(e)
Definition error.h:45
Utility Preprocessor macros.
unsigned ff_qpfile_palette(const int8_t *vals, size_t n, int *seg_values, unsigned max_segments)
Fill seg_values, ascending, with each distinct value of the n offsets in vals when there are at most ...
int ff_qpfile_load(void *log_ctx, const char *path, FFQpFile *out)
Load a per-frame QP-offset file into out, logging errors to log_ctx.
const FFQpFrame * ff_qpfile_frame_for_index(const FFQpFile *qf, unsigned frame_index)
Look up the QP frame record matching or preceding the given frame index.
unsigned ff_qpfile_segment_values(const FFQpFrame *qf, int *seg_values, unsigned max_segments)
Fill seg_values with the palette (see ff_qpfile_palette) of the frame's block offsets,...
void ff_qpfile_segment_lut(uint8_t *lut, const int *seg_values, unsigned n_seg)
Fill lut[QPFILE_LUT_SIZE] with the nearest segment index of each offset from QPFILE_DELTA_MIN to QPFI...
int ff_qpfile_check_grid(void *log_ctx, const char *encoder, const FFQpFile *qf, int width, int height)
Verify that the QP file grid matches the video macroblock dimensions.
void ff_qpfile_free(FFQpFile *qpf)
Free all memory allocated inside an FFQpFile structure.
Shared parser for the per-frame QP-offset file format.
#define QPFILE_LUT_SIZE
const char * name
Definition qsvenc.c:142
#define FF_ARRAY_ELEMS(a)
unsigned int frame_count
unsigned int block_cols
unsigned int block_rows
FFQpFrame * frames
const char * name
const char * bytes
void(* check)(const FFQpFile *qf)
static const TestCase cases[]
static void test_palette(const char *label, const int8_t *vals, size_t n, unsigned budget)
static void test_palettes(void)
static void print_frame(const char *name, unsigned idx, const FFQpFrame *f)
static void check_clearing(const FFQpFile *qf)
static void check_index_max(const FFQpFile *qf)
static int run_fixture_test(const char *path, const FixtureCase *tc)
int main(void)
static void check_header_only(const FFQpFile *qf)
static void test_lut(void)
static int write_fixture(const char *path, const char *bytes, size_t len)
static const char * err_name(int err)
static void check_clip(const FFQpFile *qf)
static void check_valid(const FFQpFile *qf)
FILE * fp
Definition swscale.c:145
static unsigned int seed
Definition videogen.c:78
int len