summaryrefslogtreecommitdiff
path: root/tools/scale_slice_test.c
blob: 4480bf856972e1be32f2a4ddd710f11771755c8e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * FFmpeg is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with FFmpeg; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>

#include "decode_simple.h"

#include "libavutil/common.h"
#include "libavutil/pixdesc.h"
#include "libavutil/error.h"
#include "libavutil/lfg.h"
#include "libavutil/random_seed.h"
#include "libavutil/video_enc_params.h"

#include "libavformat/avformat.h"

#include "libavcodec/avcodec.h"

#include "libswscale/swscale.h"

typedef struct PrivData {
    unsigned int random_seed;
    AVLFG        lfg;

    struct SwsContext *scaler;

    int v_shift_dst, h_shift_dst;
    int v_shift_src, h_shift_src;

    AVFrame *frame_ref;
    AVFrame *frame_dst;
} PrivData;

static int process_frame(DecodeContext *dc, AVFrame *frame)
{
    PrivData *pd = dc->opaque;
    int slice_start = 0;
    int ret;

    if (!frame)
        return 0;

    if (!pd->scaler) {
        pd->scaler = sws_getContext(frame->width, frame->height, frame->format,
                                    pd->frame_ref->width, pd->frame_ref->height,
                                    pd->frame_ref->format, 0, NULL, NULL, NULL);
        if (!pd->scaler)
            return AVERROR(ENOMEM);

        av_pix_fmt_get_chroma_sub_sample(frame->format, &pd->h_shift_src, &pd->v_shift_src);
    }

    /* scale the whole input frame as reference */
    ret = sws_scale(pd->scaler, (const uint8_t **)frame->data, frame->linesize, 0, frame->height,
                    pd->frame_ref->data, pd->frame_ref->linesize);
    if (ret < 0)
        return ret;

    /* scale slices with randomly generated heights */
    while (slice_start < frame->height) {
        int slice_height;
        const uint8_t *src[4];

        slice_height = av_lfg_get(&pd->lfg) % (frame->height - slice_start);
        slice_height = FFALIGN(FFMAX(1, slice_height), 1 << pd->v_shift_src);

        for (int j = 0; j < FF_ARRAY_ELEMS(src) && frame->data[j]; j++) {
            int shift = (j == 1 || j == 2) ? pd->v_shift_src : 0;
            src[j] = frame->data[j] + frame->linesize[j] * (slice_start >> shift);
        }

        ret = sws_scale(pd->scaler, src, frame->linesize, slice_start, slice_height,
                        pd->frame_dst->data, pd->frame_dst->linesize);
        if (ret < 0)
            return ret;

        slice_start += slice_height;
    }

    /* compare the two results */
    for (int i = 0; i < 4 && pd->frame_ref->data[i]; i++) {
        int shift = (i == 1 || i == 2) ? pd->v_shift_dst : 0;

        if (memcmp(pd->frame_ref->data[i], pd->frame_dst->data[i],
                   pd->frame_ref->linesize[i] * (pd->frame_ref->height >> shift))) {
            fprintf(stderr, "mismatch frame %"PRId64" seed %u\n",
                    dc->decoder->frame_num - 1, pd->random_seed);
            return AVERROR(EINVAL);
        }
    }

    return 0;
}

int main(int argc, char **argv)
{
    PrivData      pd;
    DecodeContext dc;

    int width, height;
    enum AVPixelFormat pix_fmt;
    const char *filename;
    int ret = 0;

    if (argc <= 4) {
        fprintf(stderr,
                "Usage: %s <input file> <dst width> <dst height> <dst pixfmt> [<random seed>] \n",
                argv[0]);
        return 0;
    }

    memset(&pd, 0, sizeof(pd));

    filename     = argv[1];
    width        = strtol(argv[2], NULL, 0);
    height       = strtol(argv[3], NULL, 0);
    pix_fmt      = av_get_pix_fmt(argv[4]);

    /* init RNG for generating slice sizes */
    if (argc >= 6)
        pd.random_seed = strtoul(argv[5], NULL, 0);
    else
        pd.random_seed = av_get_random_seed();

    av_lfg_init(&pd.lfg, pd.random_seed);

    av_pix_fmt_get_chroma_sub_sample(pix_fmt, &pd.h_shift_dst, &pd.v_shift_dst);

    /* allocate the frames for scaler output */
    for (int i = 0; i < 2; i++) {
        AVFrame *frame = av_frame_alloc();
        if (!frame) {
            fprintf(stderr, "Error allocating frames\n");
            return AVERROR(ENOMEM);
        }

        frame->width  = width;
        frame->height = height;
        frame->format = pix_fmt;

        ret = av_frame_get_buffer(frame, 0);
        if (ret < 0) {
            fprintf(stderr, "Error allocating frame data\n");
            return ret;
        }

        /* make sure the padding is zeroed */
        for (int j = 0; j < 4 && frame->data[j]; j++) {
            int shift = (j == 1 || j == 2) ? pd.v_shift_dst : 0;
            memset(frame->data[j], 0,
                   frame->linesize[j] * (height >> shift));
        }
        if (i) pd.frame_ref = frame;
        else   pd.frame_dst = frame;
    }

    ret = ds_open(&dc, filename, 0);
    if (ret < 0) {
        fprintf(stderr, "Error opening the file\n");
        return ret;
    }

    dc.process_frame = process_frame;
    dc.opaque        = &pd;

    ret = ds_run(&dc);

    av_frame_free(&pd.frame_dst);
    av_frame_free(&pd.frame_ref);
    sws_freeContext(pd.scaler);
    ds_free(&dc);
    return ret;
}