| Line | Branch | Exec | Source |
|---|---|---|---|
| 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 | |||
| 51 | 15 | static int process_frame(DecodeContext *dc, AVFrame *frame) | |
| 52 | { | ||
| 53 | 15 | PrivData *pd = dc->opaque; | |
| 54 | 15 | int slice_start = 0; | |
| 55 | int ret; | ||
| 56 | |||
| 57 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 11 times.
|
15 | if (!frame) |
| 58 | 4 | return 0; | |
| 59 | |||
| 60 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 9 times.
|
11 | if (!pd->scaler) { |
| 61 | 4 | pd->scaler = sws_getContext(frame->width, frame->height, frame->format, | |
| 62 | 2 | pd->frame_ref->width, pd->frame_ref->height, | |
| 63 | 2 | pd->frame_ref->format, 0, NULL, NULL, NULL); | |
| 64 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (!pd->scaler) |
| 65 | ✗ | return AVERROR(ENOMEM); | |
| 66 | |||
| 67 | 2 | 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 | 11 | ret = sws_scale(pd->scaler, (const uint8_t **)frame->data, frame->linesize, 0, frame->height, | |
| 72 | 11 | pd->frame_ref->data, pd->frame_ref->linesize); | |
| 73 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
|
11 | if (ret < 0) |
| 74 | ✗ | return ret; | |
| 75 | |||
| 76 | /* scale slices with randomly generated heights */ | ||
| 77 |
2/2✓ Branch 0 taken 77 times.
✓ Branch 1 taken 11 times.
|
88 | while (slice_start < frame->height) { |
| 78 | int slice_height; | ||
| 79 | const uint8_t *src[4]; | ||
| 80 | |||
| 81 | 77 | slice_height = av_lfg_get(&pd->lfg) % (frame->height - slice_start); | |
| 82 | 77 | slice_height = FFALIGN(FFMAX(1, slice_height), 1 << pd->v_shift_src); | |
| 83 | |||
| 84 |
3/4✓ Branch 0 taken 290 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 213 times.
✓ Branch 3 taken 77 times.
|
290 | for (int j = 0; j < FF_ARRAY_ELEMS(src) && frame->data[j]; j++) { |
| 85 |
4/4✓ Branch 0 taken 145 times.
✓ Branch 1 taken 68 times.
✓ Branch 2 taken 68 times.
✓ Branch 3 taken 77 times.
|
213 | int shift = (j == 1 || j == 2) ? pd->v_shift_src : 0; |
| 86 | 213 | src[j] = frame->data[j] + frame->linesize[j] * (slice_start >> shift); | |
| 87 | } | ||
| 88 | |||
| 89 | 77 | ret = sws_scale(pd->scaler, src, frame->linesize, slice_start, slice_height, | |
| 90 | 77 | pd->frame_dst->data, pd->frame_dst->linesize); | |
| 91 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 77 times.
|
77 | if (ret < 0) |
| 92 | ✗ | return ret; | |
| 93 | |||
| 94 | 77 | slice_start += slice_height; | |
| 95 | } | ||
| 96 | |||
| 97 | /* compare the two results */ | ||
| 98 |
3/4✓ Branch 0 taken 23 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✓ Branch 3 taken 11 times.
|
23 | for (int i = 0; i < 4 && pd->frame_ref->data[i]; i++) { |
| 99 |
3/4✓ Branch 0 taken 11 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 11 times.
|
12 | int shift = (i == 1 || i == 2) ? pd->v_shift_dst : 0; |
| 100 | |||
| 101 | 12 | if (memcmp(pd->frame_ref->data[i], pd->frame_dst->data[i], | |
| 102 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
|
12 | 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 | 11 | return 0; | |
| 110 | } | ||
| 111 | |||
| 112 | 2 | 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 | 2 | int ret = 0; | |
| 121 | |||
| 122 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | 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 | 2 | memset(&pd, 0, sizeof(pd)); | |
| 130 | |||
| 131 | 2 | filename = argv[1]; | |
| 132 | 2 | width = strtol(argv[2], NULL, 0); | |
| 133 | 2 | height = strtol(argv[3], NULL, 0); | |
| 134 | 2 | pix_fmt = av_get_pix_fmt(argv[4]); | |
| 135 | |||
| 136 | /* init RNG for generating slice sizes */ | ||
| 137 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (argc >= 6) |
| 138 | ✗ | pd.random_seed = strtoul(argv[5], NULL, 0); | |
| 139 | else | ||
| 140 | 2 | pd.random_seed = av_get_random_seed(); | |
| 141 | |||
| 142 | 2 | av_lfg_init(&pd.lfg, pd.random_seed); | |
| 143 | |||
| 144 | 2 | 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 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
|
6 | for (int i = 0; i < 2; i++) { |
| 148 | 4 | AVFrame *frame = av_frame_alloc(); | |
| 149 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | if (!frame) { |
| 150 | ✗ | fprintf(stderr, "Error allocating frames\n"); | |
| 151 | ✗ | return AVERROR(ENOMEM); | |
| 152 | } | ||
| 153 | |||
| 154 | 4 | frame->width = width; | |
| 155 | 4 | frame->height = height; | |
| 156 | 4 | frame->format = pix_fmt; | |
| 157 | |||
| 158 | 4 | ret = av_frame_get_buffer(frame, 0); | |
| 159 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | if (ret < 0) { |
| 160 | ✗ | fprintf(stderr, "Error allocating frame data\n"); | |
| 161 | ✗ | return ret; | |
| 162 | } | ||
| 163 | |||
| 164 | /* make sure the padding is zeroed */ | ||
| 165 |
3/4✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 4 times.
|
10 | for (int j = 0; j < 4 && frame->data[j]; j++) { |
| 166 |
3/4✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
|
6 | int shift = (j == 1 || j == 2) ? pd.v_shift_dst : 0; |
| 167 | 6 | memset(frame->data[j], 0, | |
| 168 | 6 | frame->linesize[j] * (height >> shift)); | |
| 169 | } | ||
| 170 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
|
4 | if (i) pd.frame_ref = frame; |
| 171 | 2 | else pd.frame_dst = frame; | |
| 172 | } | ||
| 173 | |||
| 174 | 2 | ret = ds_open(&dc, filename, 0); | |
| 175 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (ret < 0) { |
| 176 | ✗ | fprintf(stderr, "Error opening the file\n"); | |
| 177 | ✗ | return ret; | |
| 178 | } | ||
| 179 | |||
| 180 | 2 | dc.process_frame = process_frame; | |
| 181 | 2 | dc.opaque = &pd; | |
| 182 | |||
| 183 | 2 | ret = ds_run(&dc); | |
| 184 | |||
| 185 | 2 | av_frame_free(&pd.frame_dst); | |
| 186 | 2 | av_frame_free(&pd.frame_ref); | |
| 187 | 2 | sws_freeContext(pd.scaler); | |
| 188 | 2 | ds_free(&dc); | |
| 189 | 2 | return ret; | |
| 190 | } | ||
| 191 |