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"
29 #include "libavutil/random_seed.h"
30 #include "libavutil/video_enc_params.h"
31
32 #include "libavformat/avformat.h"
33
34 #include "libavcodec/avcodec.h"
35
36 #include "libswscale/swscale.h"
37
38 typedef struct PrivData {
39 unsigned int random_seed;
40 AVLFG lfg;
41
42 struct SwsContext *scaler;
43
44 int v_shift_dst, h_shift_dst;
45 int v_shift_src, h_shift_src;
46
47 AVFrame *frame_ref;
48 AVFrame *frame_dst;
49 } PrivData;
50
process_frame(DecodeContext * dc,AVFrame * frame)51 static int process_frame(DecodeContext *dc, AVFrame *frame)
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,
62 pd->frame_ref->width, pd->frame_ref->height,
63 pd->frame_ref->format, 0, NULL, NULL, NULL);
64 if (!pd->scaler)
65 return AVERROR(ENOMEM);
66
67 av_pix_fmt_get_chroma_sub_sample(frame->format, &pd->h_shift_src, &pd->v_shift_src);
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 */
77 while (slice_start < frame->height) {
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 %d seed %u\n",
104 dc->decoder->frame_number - 1, pd->random_seed);
105 return AVERROR(EINVAL);
106 }
107 }
108
109 return 0;
110 }
111
main(int argc,char ** argv)112 int main(int argc, char **argv)
113 {
114 PrivData pd;
115 DecodeContext dc;
116
117 int width, height;
118 enum AVPixelFormat pix_fmt;
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
140 pd.random_seed = av_get_random_seed();
141
142 av_lfg_init(&pd.lfg, pd.random_seed);
143
144 av_pix_fmt_get_chroma_sub_sample(pix_fmt, &pd.h_shift_dst, &pd.v_shift_dst);
145
146 /* allocate the frames for scaler output */
147 for (int i = 0; i < 2; i++) {
148 AVFrame *frame = av_frame_alloc();
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
180 dc.process_frame = process_frame;
181 dc.opaque = &pd;
182
183 ret = ds_run(&dc);
184
185 av_frame_free(&pd.frame_dst);
186 av_frame_free(&pd.frame_ref);
187 sws_freeContext(pd.scaler);
188 ds_free(&dc);
189 return ret;
190 }
191