FFmpeg
Loading...
Searching...
No Matches
scale_slice_test.c
Go to the documentation of this file.
1/*
2 * This file is part of FFmpeg.
3 *
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19#include <stdio.h>
20#include <stdint.h>
21#include <stdlib.h>
22
23#include "decode_simple.h"
24
25#include "libavutil/common.h"
26#include "libavutil/pixdesc.h"
27#include "libavutil/error.h"
28#include "libavutil/lfg.h"
31
33
34#include "libavcodec/avcodec.h"
35
36#include "libswscale/swscale.h"
37
38typedef struct PrivData {
39 unsigned int random_seed;
41
42 struct SwsContext *scaler;
43
46
49} PrivData;
50
52{
53 PrivData *pd = dc->opaque;
54 int slice_start = 0;
55 int ret;
56
57 if (!frame)
58 return 0;
59
60 if (!pd->scaler) {
61 pd->scaler = sws_getContext(frame->width, frame->height, frame->format,
63 pd->frame_ref->format, 0, NULL, NULL, NULL);
64 if (!pd->scaler)
65 return AVERROR(ENOMEM);
66
68 }
69
70 /* scale the whole input frame as reference */
71 ret = sws_scale(pd->scaler, (const uint8_t **)frame->data, frame->linesize, 0, frame->height,
72 pd->frame_ref->data, pd->frame_ref->linesize);
73 if (ret < 0)
74 return ret;
75
76 /* scale slices with randomly generated heights */
78 int slice_height;
79 const uint8_t *src[4];
80
81 slice_height = av_lfg_get(&pd->lfg) % (frame->height - slice_start);
82 slice_height = FFALIGN(FFMAX(1, slice_height), 1 << pd->v_shift_src);
83
84 for (int j = 0; j < FF_ARRAY_ELEMS(src) && frame->data[j]; j++) {
85 int shift = (j == 1 || j == 2) ? pd->v_shift_src : 0;
86 src[j] = frame->data[j] + frame->linesize[j] * (slice_start >> shift);
87 }
88
89 ret = sws_scale(pd->scaler, src, frame->linesize, slice_start, slice_height,
90 pd->frame_dst->data, pd->frame_dst->linesize);
91 if (ret < 0)
92 return ret;
93
94 slice_start += slice_height;
95 }
96
97 /* compare the two results */
98 for (int i = 0; i < 4 && pd->frame_ref->data[i]; i++) {
99 int shift = (i == 1 || i == 2) ? pd->v_shift_dst : 0;
100
101 if (memcmp(pd->frame_ref->data[i], pd->frame_dst->data[i],
102 pd->frame_ref->linesize[i] * (pd->frame_ref->height >> shift))) {
103 fprintf(stderr, "mismatch frame %"PRId64" seed %u\n",
104 dc->decoder->frame_num - 1, pd->random_seed);
105 return AVERROR(EINVAL);
106 }
107 }
108
109 return 0;
110}
111
112int main(int argc, char **argv)
113{
114 PrivData pd;
115 DecodeContext dc;
116
117 int width, height;
119 const char *filename;
120 int ret = 0;
121
122 if (argc <= 4) {
123 fprintf(stderr,
124 "Usage: %s <input file> <dst width> <dst height> <dst pixfmt> [<random seed>] \n",
125 argv[0]);
126 return 0;
127 }
128
129 memset(&pd, 0, sizeof(pd));
130
131 filename = argv[1];
132 width = strtol(argv[2], NULL, 0);
133 height = strtol(argv[3], NULL, 0);
134 pix_fmt = av_get_pix_fmt(argv[4]);
135
136 /* init RNG for generating slice sizes */
137 if (argc >= 6)
138 pd.random_seed = strtoul(argv[5], NULL, 0);
139 else
141
142 av_lfg_init(&pd.lfg, pd.random_seed);
143
145
146 /* allocate the frames for scaler output */
147 for (int i = 0; i < 2; i++) {
149 if (!frame) {
150 fprintf(stderr, "Error allocating frames\n");
151 return AVERROR(ENOMEM);
152 }
153
154 frame->width = width;
155 frame->height = height;
156 frame->format = pix_fmt;
157
158 ret = av_frame_get_buffer(frame, 0);
159 if (ret < 0) {
160 fprintf(stderr, "Error allocating frame data\n");
161 return ret;
162 }
163
164 /* make sure the padding is zeroed */
165 for (int j = 0; j < 4 && frame->data[j]; j++) {
166 int shift = (j == 1 || j == 2) ? pd.v_shift_dst : 0;
167 memset(frame->data[j], 0,
168 frame->linesize[j] * (height >> shift));
169 }
170 if (i) pd.frame_ref = frame;
171 else pd.frame_dst = frame;
172 }
173
174 ret = ds_open(&dc, filename, 0);
175 if (ret < 0) {
176 fprintf(stderr, "Error opening the file\n");
177 return ret;
178 }
179
181 dc.opaque = &pd;
182
183 ret = ds_run(&dc);
184
188 ds_free(&dc);
189 return ret;
190}
Libavcodec external API header.
Main libavformat public API header.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
common internal and external API header
#define NULL
Definition coverity.c:32
int ds_open(DecodeContext *dc, const char *url, int stream_idx)
void ds_free(DecodeContext *dc)
int ds_run(DecodeContext *dc)
static enum AVPixelFormat pix_fmt
static AVFrame * frame
int main
Definition dovi_rpuenc.c:38
error code definitions
static av_always_inline int process_frame(AVTextFormatContext *tfc, InputFile *ifile, AVFrame *frame, const AVPacket *pkt, int *packet_new)
Definition ffprobe.c:1596
uint32_t av_get_random_seed(void)
Get a seed to use in conjunction with random functions.
#define AVERROR(e)
Definition error.h:45
int av_frame_get_buffer(AVFrame *frame, int align)
Allocate new buffer(s) for audio or video data.
Definition frame.c:206
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:64
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition frame.c:52
int attribute_align_arg sws_scale(SwsContext *sws, const uint8_t *const srcSlice[], const int srcStride[], int srcSliceY, int srcSliceH, uint8_t *const dst[], const int dstStride[])
swscale wrapper, so we don't need to export the SwsContext.
Definition swscale.c:1626
SwsContext * sws_getContext(int srcW, int srcH, enum AVPixelFormat srcFormat, int dstW, int dstH, enum AVPixelFormat dstFormat, int flags, SwsFilter *srcFilter, SwsFilter *dstFilter, const double *param)
Allocate and return an SwsContext.
Definition utils.c:1919
void sws_freeContext(SwsContext *swsContext)
Free the swscaler context swsContext.
Definition utils.c:2250
av_cold void av_lfg_init(AVLFG *c, unsigned int seed)
Definition lfg.c:32
static unsigned int av_lfg_get(AVLFG *c)
Get the next random unsigned 32-bit number using an ALFG.
Definition lfg.h:53
static int shift(int a, int b)
Definition bonk.c:261
#define FFMAX(a, b)
Definition macros.h:47
#define FFALIGN(x, a)
Definition macros.h:78
int av_pix_fmt_get_chroma_sub_sample(enum AVPixelFormat pix_fmt, int *h_shift, int *v_shift)
Utility function to access log2_chroma_w log2_chroma_h from the pixel format AVPixFmtDescriptor.
Definition pixdesc.c:3488
enum AVPixelFormat av_get_pix_fmt(const char *name)
Return the pixel format corresponding to name.
Definition pixdesc.c:3392
AVPixelFormat
Pixel format.
Definition pixfmt.h:71
static int process_frame(DecodeContext *dc, AVFrame *frame)
#define FF_ARRAY_ELEMS(a)
int64_t frame_num
Frame counter, set by libavcodec.
Definition avcodec.h:1883
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition frame.h:493
int width
Definition frame.h:544
int height
Definition frame.h:544
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
Definition frame.h:517
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames,...
Definition frame.h:559
Context structure for the Lagged Fibonacci PRNG.
Definition lfg.h:33
int(* process_frame)(struct DecodeContext *dc, AVFrame *frame)
AVCodecContext * decoder
AVFrame * frame_ref
struct SwsContext * scaler
AVFrame * frame_dst
unsigned int random_seed
Main external API structure.
Definition swscale.h:227
external API header
#define src
Definition vp8dsp.c:248
#define height
Definition dsp.h:89
#define width
Definition dsp.h:89
static int slice_start(SliceContext *sc, VVCContext *s, VVCFrameContext *fc, const CodedBitstreamUnit *unit, const int is_first_slice)
Definition dec.c:844