48#define QPFILE_MAX_FRAMES (1u << 20)
49#define QPFILE_MAX_BLOCKS_PER_DIM (1u << 14)
50#define QPFILE_MAX_LINE_BYTES (1u << 20)
57 v = strtoll(
s, end, 10);
75 while (*p ==
' ' || *p ==
'\t')
83 return c && strchr(
"IPBipb",
c);
102 if (*p !=
' ' && *p !=
'\t' && *p !=
'\r' && *p !=
'\n')
120 if (idx < 0 || idx > UINT_MAX)
131 f->frame_index = (unsigned)idx;
132 f->block_offsets =
NULL;
143 for (
const char *p =
line; *p; p++) {
144 if (*p ==
' ' || *p ==
'\t' || *p ==
'\r' || *p ==
'\n') {
146 }
else if (!in_tok) {
160 const char *cur =
line;
162 for (
unsigned i = 0;
i < bw;
i++) {
169 while (*cur ==
' ' || *cur ==
'\t')
184 return *buf ? 0 :
AVERROR(ENOMEM);
214 *out_len = (ptrdiff_t)
pos;
237 (*buf)[
pos++] = (char)
c;
240 *out_len = (ptrdiff_t)
pos;
259 st->
bw =
f->block_cols;
265 if (!
f->block_offsets)
267 f->block_rows = st->
bh;
268 f->block_cols = st->
bw;
296 if (
out->frame_count > 0) {
298 previous = &
out->frames[
out->frame_count - 1u];
301 "qpfile: line %u frame index %u is not greater than "
319 if (!
f->block_cols) {
320 f->block_cols = tokens;
321 if (st->
bw && tokens != st->
bw)
323 }
else if (tokens !=
f->block_cols) {
332 unsigned tokens, this_bw, needed;
360 unsigned line,
int err)
369 char **
line,
size_t *line_cap)
408 memset(
out, 0,
sizeof(*
out));
412 return AVERROR(errno ? errno : ENOENT);
421 out->block_cols = st.
bw;
422 out->block_rows = st.
bh;
423 if (!
out->frame_count) {
425 "qpfile: %s holds no frame record.\n",
path);
440 const unsigned mbx = (
width + 15) / 16;
441 const unsigned mby = (
height + 15) / 16;
448 "%s: the qpfile has %ux%u blocks per frame, the %dx%d video has %ux%u "
472 unsigned frame_index)
474 unsigned low = 0,
high;
480 unsigned mid = low + (
high - low) / 2;
492 int *min_off,
int *max_off,
unsigned *distinct)
494 for (
size_t i = 0;
i < n;
i++)
499 *min_off =
FFMIN(*min_off, v);
507 unsigned max_segments)
511 unsigned distinct = 0;
513 if (!n || max_segments < 2)
516 if (distinct <= max_segments) {
519 for (
int v = min_off; v <= max_off; v++)
524 for (
unsigned i = 0;
i < max_segments;
i++)
525 seg_values[
i] = min_off +
526 (
int)((unsigned)(max_off - min_off) *
i / (max_segments - 1u));
532 unsigned max_segments)
538 seg_values, max_segments);
544 unsigned best_idx = 0;
545 int best_dist = INT_MAX;
547 for (
unsigned i = 0;
i < n_seg;
i++) {
548 int d =
FFABS(v - seg_values[
i]);
#define i(width, name, range_min, range_max)
common internal and external API header
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
void * av_realloc_array(void *ptr, size_t nmemb, size_t size)
void * av_memdup(const void *p, size_t size)
Duplicate a buffer with av_malloc().
static av_const int av_isdigit(int c)
Locale-independent conversion of ASCII isdigit.
FILE * avpriv_fopen_utf8(const char *path, const char *mode)
Open a file using a UTF-8 filename.
Utility Preprocessor macros.
void * av_calloc(size_t nmemb, size_t size)
Memory handling functions.
static const char * skip_blank(const char *p)
static int qpfile_line_alloc(char **buf, size_t *cap)
static int qpfile_line_error(const QpLoadState *st, const char *fmt_kind, unsigned line, int err)
#define QPFILE_MAX_LINE_BYTES
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 ...
static int parse_header(const char *line, FFQpFrame *f)
static int parse_int64(const char *s, char **end, int64_t *out)
static int qpfile_line_eof(FILE *fp, char *buf, size_t pos, ptrdiff_t *out_len)
static int is_frame_type(char c)
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.
static unsigned count_tokens(const char *line)
static int clamp_offset(int64_t v)
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 int qpfile_set_row_width(QpLoadState *st, unsigned tokens)
static int qpfile_line_grow(char **buf, size_t *cap, size_t pos)
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,...
static int qpfile_finish_frame(QpLoadState *st)
static int parse_row(const char *line, int8_t *row_out, unsigned bw)
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...
#define QPFILE_MAX_BLOCKS_PER_DIM
static int qpfile_add_row(QpLoadState *st, const char *line)
#define QPFILE_MAX_FRAMES
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.
static int qpfile_read_frames(FILE *fp, FFQpFile *out, QpLoadState *st, char **line, size_t *line_cap)
static void qpfile_histogram(const int8_t *vals, size_t n, uint8_t *seen, int *min_off, int *max_off, unsigned *distinct)
void ff_qpfile_free(FFQpFile *qpf)
Free all memory allocated inside an FFQpFile structure.
static int qpfile_getline(char **buf, size_t *cap, FILE *fp, ptrdiff_t *out_len)
static int only_space(const char *p)
static int looks_like_header(const char *line)
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.
static int qpfile_add_header(FFQpFile *out, QpLoadState *st, const char *line)
Shared parser for the per-frame QP-offset file format.