FFmpeg
Loading...
Searching...
No Matches
qpfile_parser.h
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/**
18 * @file
19 * Shared parser for the per-frame QP-offset file format. Consumed by libx264,
20 * libsvtav1, and libaom-av1 encoder adapters via the new -qpfile
21 * AVOption.
22 *
23 * Keep the parser shared across encoder adapters so future
24 * encoder additions inherit the same format handling and
25 * validation rules.
26 *
27 * The parsed block offsets are QP deltas for libx264, deltas on libaom's
28 * 0..63 quantizer scale for libaom-av1 (libaom converts each to a
29 * quantizer index delta, 12 becoming 48) and AV1 quantizer index deltas
30 * for libsvtav1.
31 */
32
33#ifndef AVCODEC_QPFILE_PARSER_H
34#define AVCODEC_QPFILE_PARSER_H
35
36#include <stddef.h>
37#include <stdint.h>
38
39#define QPFILE_DELTA_MIN (-12)
40#define QPFILE_DELTA_MAX (12)
41#define QPFILE_LUT_SIZE (QPFILE_DELTA_MAX - QPFILE_DELTA_MIN + 1)
42
43typedef struct FFQpFrame {
44 unsigned int frame_index;
46 unsigned int block_cols;
47 unsigned int block_rows;
48} FFQpFrame;
49
50typedef struct FFQpFile {
52 unsigned int frame_count;
53 unsigned int block_cols;
54 unsigned int block_rows;
55} FFQpFile;
56
57/** Load a per-frame QP-offset file into out, logging errors to log_ctx. */
58int ff_qpfile_load(void *log_ctx, const char *path, FFQpFile *out);
59/** Verify that the QP file grid matches the video macroblock dimensions. */
60int ff_qpfile_check_grid(void *log_ctx, const char *encoder, const FFQpFile *qf,
61 int width, int height);
62/** Free all memory allocated inside an FFQpFile structure. */
63void ff_qpfile_free(FFQpFile *qpf);
64/** Look up the QP frame record matching or preceding the given frame index. */
66 unsigned frame_index);
67/** Fill seg_values with the palette (see ff_qpfile_palette) of the frame's
68 * block offsets, returning its size, or 0 without offsets. */
69unsigned ff_qpfile_segment_values(const FFQpFrame *qf, int *seg_values,
70 unsigned max_segments);
71/** Return the index of the segment value nearest v, the first one on a tie. */
72unsigned ff_qpfile_nearest_segment(int v, const int *seg_values, unsigned n_seg);
73/** Fill seg_values, ascending, with each distinct value of the n offsets in
74 * vals when there are at most max_segments (at least 2) of them, else with
75 * max_segments evenly spaced values over their range; return the count,
76 * 0 when n is 0. Every value must lie in QPFILE_DELTA_MIN..QPFILE_DELTA_MAX. */
77unsigned ff_qpfile_palette(const int8_t *vals, size_t n, int *seg_values,
78 unsigned max_segments);
79/** Fill lut[QPFILE_LUT_SIZE] with the nearest segment index of each offset
80 * from QPFILE_DELTA_MIN to QPFILE_DELTA_MAX. */
81void ff_qpfile_segment_lut(uint8_t *lut, const int *seg_values, unsigned n_seg);
82
83#endif /* AVCODEC_QPFILE_PARSER_H */
static FILE * out
char * path
Definition ffplay.c:385
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.
unsigned ff_qpfile_nearest_segment(int v, const int *seg_values, unsigned n_seg)
Return the index of the segment value nearest v, the first one on a tie.
unsigned int frame_count
unsigned int block_cols
unsigned int block_rows
FFQpFrame * frames
unsigned int block_cols
int8_t * block_offsets
unsigned int frame_index
unsigned int block_rows
#define height
Definition dsp.h:99
#define width
Definition dsp.h:99