FFmpeg
Loading...
Searching...
No Matches
qpfile_parser.c File Reference

Shared parser for the per-frame QP-offset file format consumed by libx264, libsvtav1, and libaom-av1 encoder adapters. More...

#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "libavutil/common.h"
#include "libavutil/avstring.h"
#include "libavutil/error.h"
#include "libavutil/file_open.h"
#include "libavutil/log.h"
#include "libavutil/macros.h"
#include "libavutil/mem.h"
#include "qpfile_parser.h"

Go to the source code of this file.

Data Structures

struct  QpLoadState
 

Macros

#define QPFILE_MAX_FRAMES   (1u << 20)
 
#define QPFILE_MAX_BLOCKS_PER_DIM   (1u << 14)
 
#define QPFILE_MAX_LINE_BYTES   (1u << 20)
 

Functions

static int parse_int64 (const char *s, char **end, int64_t *out)
 
static int clamp_offset (int64_t v)
 
static const char * skip_blank (const char *p)
 
static int is_frame_type (char c)
 
static int looks_like_header (const char *line)
 
static int only_space (const char *p)
 
static int parse_header (const char *line, FFQpFrame *f)
 
static unsigned count_tokens (const char *line)
 
static int parse_row (const char *line, int8_t *row_out, unsigned bw)
 
static int qpfile_line_alloc (char **buf, size_t *cap)
 
static int qpfile_line_grow (char **buf, size_t *cap, size_t pos)
 
static int qpfile_line_eof (FILE *fp, char *buf, size_t pos, ptrdiff_t *out_len)
 
static int qpfile_getline (char **buf, size_t *cap, FILE *fp, ptrdiff_t *out_len)
 
static int qpfile_finish_frame (QpLoadState *st)
 
static int qpfile_add_header (FFQpFile *out, QpLoadState *st, const char *line)
 
static int qpfile_set_row_width (QpLoadState *st, unsigned tokens)
 
static int qpfile_add_row (QpLoadState *st, const char *line)
 
static int qpfile_line_error (const QpLoadState *st, const char *fmt_kind, unsigned line, int err)
 
static int qpfile_read_frames (FILE *fp, FFQpFile *out, QpLoadState *st, char **line, size_t *line_cap)
 
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.
 
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.
 
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.
 
static void qpfile_histogram (const int8_t *vals, size_t n, uint8_t *seen, int *min_off, int *max_off, unsigned *distinct)
 
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 max_segments (at least 2) of them, else with max_segments evenly spaced values over their range; return the count, 0 when n is 0.
 
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, returning its size, or 0 without offsets.
 
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.
 
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 QPFILE_DELTA_MAX.
 

Detailed Description

Shared parser for the per-frame QP-offset file format consumed by libx264, libsvtav1, and libaom-av1 encoder adapters.

The format consists of <frame_idx> <I|P|B> <qp> followed by per-MB-row deltas.

Format (ASCII, one logical record per frame):

<frame_idx> <I|P|B> <baseline_qp>
<delta_0_0> <delta_0_1> ... <delta_0_(bw-1)>
...

Lines are read with fgetc(), not POSIX getline().

Definition in file qpfile_parser.c.

Macro Definition Documentation

◆ QPFILE_MAX_FRAMES

#define QPFILE_MAX_FRAMES   (1u << 20)

Definition at line 48 of file qpfile_parser.c.

Referenced by qpfile_add_header().

◆ QPFILE_MAX_BLOCKS_PER_DIM

#define QPFILE_MAX_BLOCKS_PER_DIM   (1u << 14)

Definition at line 49 of file qpfile_parser.c.

Referenced by count_tokens(), qpfile_add_row(), and qpfile_set_row_width().

◆ QPFILE_MAX_LINE_BYTES

#define QPFILE_MAX_LINE_BYTES   (1u << 20)

Definition at line 50 of file qpfile_parser.c.

Referenced by qpfile_getline(), and qpfile_line_grow().

Function Documentation

◆ parse_int64()

static int parse_int64 ( const char * s,
char ** end,
int64_t * out )
static

Definition at line 53 of file qpfile_parser.c.

Referenced by parse_header(), and parse_row().

◆ clamp_offset()

static int clamp_offset ( int64_t v)
static

Definition at line 67 of file qpfile_parser.c.

Referenced by parse_row().

◆ skip_blank()

static const char * skip_blank ( const char * p)
static

Definition at line 73 of file qpfile_parser.c.

Referenced by looks_like_header(), and parse_header().

◆ is_frame_type()

static int is_frame_type ( char c)
static

Definition at line 81 of file qpfile_parser.c.

Referenced by looks_like_header(), and parse_header().

◆ looks_like_header()

static int looks_like_header ( const char * line)
static

Definition at line 87 of file qpfile_parser.c.

Referenced by qpfile_read_frames().

◆ only_space()

static int only_space ( const char * p)
static

Definition at line 99 of file qpfile_parser.c.

Referenced by parse_header(), parse_row(), and qpfile_read_frames().

◆ parse_header()

static int parse_header ( const char * line,
FFQpFrame * f )
static

Definition at line 110 of file qpfile_parser.c.

Referenced by qpfile_add_header().

◆ count_tokens()

static unsigned count_tokens ( const char * line)
static

Definition at line 139 of file qpfile_parser.c.

Referenced by qpfile_add_row().

◆ parse_row()

static int parse_row ( const char * line,
int8_t * row_out,
unsigned bw )
static

Definition at line 157 of file qpfile_parser.c.

Referenced by qpfile_add_row().

◆ qpfile_line_alloc()

static int qpfile_line_alloc ( char ** buf,
size_t * cap )
static

Definition at line 178 of file qpfile_parser.c.

Referenced by qpfile_getline().

◆ qpfile_line_grow()

static int qpfile_line_grow ( char ** buf,
size_t * cap,
size_t pos )
static

Definition at line 188 of file qpfile_parser.c.

Referenced by qpfile_getline().

◆ qpfile_line_eof()

static int qpfile_line_eof ( FILE * fp,
char * buf,
size_t pos,
ptrdiff_t * out_len )
static

Definition at line 205 of file qpfile_parser.c.

Referenced by qpfile_getline().

◆ qpfile_getline()

static int qpfile_getline ( char ** buf,
size_t * cap,
FILE * fp,
ptrdiff_t * out_len )
static

Definition at line 219 of file qpfile_parser.c.

Referenced by qpfile_read_frames().

◆ qpfile_finish_frame()

static int qpfile_finish_frame ( QpLoadState * st)
static

Definition at line 254 of file qpfile_parser.c.

Referenced by qpfile_add_header(), and qpfile_read_frames().

◆ qpfile_add_header()

static int qpfile_add_header ( FFQpFile * out,
QpLoadState * st,
const char * line )
static

Definition at line 274 of file qpfile_parser.c.

Referenced by qpfile_read_frames().

◆ qpfile_set_row_width()

static int qpfile_set_row_width ( QpLoadState * st,
unsigned tokens )
static

Definition at line 313 of file qpfile_parser.c.

Referenced by qpfile_add_row().

◆ qpfile_add_row()

static int qpfile_add_row ( QpLoadState * st,
const char * line )
static

Definition at line 330 of file qpfile_parser.c.

Referenced by qpfile_read_frames().

◆ qpfile_line_error()

static int qpfile_line_error ( const QpLoadState * st,
const char * fmt_kind,
unsigned line,
int err )
static

Definition at line 359 of file qpfile_parser.c.

Referenced by qpfile_read_frames().

◆ qpfile_read_frames()

static int qpfile_read_frames ( FILE * fp,
FFQpFile * out,
QpLoadState * st,
char ** line,
size_t * line_cap )
static

Definition at line 368 of file qpfile_parser.c.

Referenced by ff_qpfile_load().

◆ ff_qpfile_load()

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.

Definition at line 398 of file qpfile_parser.c.

Referenced by load_qpfile(), run_fixture_test(), and X264_init().

◆ ff_qpfile_check_grid()

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.

Definition at line 437 of file qpfile_parser.c.

Referenced by check_header_only(), check_valid(), load_qpfile(), and X264_init().

◆ ff_qpfile_free()

void ff_qpfile_free ( FFQpFile * qpf)

Free all memory allocated inside an FFQpFile structure.

Definition at line 457 of file qpfile_parser.c.

Referenced by aom_free(), eb_enc_close(), ff_qpfile_load(), run_fixture_test(), and X264_close().

◆ ff_qpfile_frame_for_index()

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.

Definition at line 471 of file qpfile_parser.c.

Referenced by check_clearing(), check_valid(), and setup_qpfile().

◆ qpfile_histogram()

static void qpfile_histogram ( const int8_t * vals,
size_t n,
uint8_t * seen,
int * min_off,
int * max_off,
unsigned * distinct )
static

Definition at line 491 of file qpfile_parser.c.

Referenced by ff_qpfile_palette().

◆ ff_qpfile_palette()

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 max_segments (at least 2) of them, else with max_segments evenly spaced values over their range; return the count, 0 when n is 0.

Every value must lie in QPFILE_DELTA_MIN..QPFILE_DELTA_MAX.

Definition at line 506 of file qpfile_parser.c.

Referenced by ff_qpfile_segment_values(), and test_palette().

◆ ff_qpfile_segment_values()

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, returning its size, or 0 without offsets.

Definition at line 531 of file qpfile_parser.c.

Referenced by check_valid().

◆ ff_qpfile_nearest_segment()

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.

Definition at line 542 of file qpfile_parser.c.

Referenced by ff_qpfile_segment_lut().

◆ ff_qpfile_segment_lut()

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 QPFILE_DELTA_MAX.

Definition at line 559 of file qpfile_parser.c.

Referenced by test_lut().