../../../ffmpeg/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
0 / 119
0.0%
Branches:
0 / 96
0.0%
Functions:
0 / 4
0.0%

libavcodec/hdrdec.c
Line Branch Exec Source
1 /*
2 * Radiance HDR image format
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include "avcodec.h"
22 #include "bytestream.h"
23 #include "codec_internal.h"
24 #include "decode.h"
25 #include "thread.h"
26
27 #define MINELEN 8
28 #define MAXELEN 0x7fff
29
30 ✗ static int hdr_get_line(GetByteContext *gb, uint8_t *buffer, int size)
31 {
32 ✗ int n = 0, c;
33
34 ✗ memset(buffer, 0, size);
35
36 do {
37 ✗ c = bytestream2_get_byte(gb);
38
0/2
✗ Branch 4 → 5 not taken.
✗ Branch 4 → 6 not taken.
✗ if (n < size - 1)
39 ✗ buffer[n++] = c;
40
0/4
✗ Branch 7 → 8 not taken.
✗ Branch 7 → 10 not taken.
✗ Branch 8 → 9 not taken.
✗ Branch 8 → 10 not taken.
✗ } while (bytestream2_get_bytes_left(gb) > 0 && c != '\n');
41
42 ✗ return 0;
43 }
44
45 ✗ static float convert(int expo, int val)
46 {
47
0/2
✗ Branch 2 → 3 not taken.
✗ Branch 2 → 4 not taken.
✗ if (expo == -128) {
48 ✗ return 0.f;
49 } else {
50 ✗ const float v = val / 256.f;
51
52 ✗ return ldexpf(v, expo);
53 }
54 }
55
56 ✗ static int decompress(uint8_t *scanline, int w, GetByteContext *gb, const uint8_t *start)
57 {
58 ✗ int rshift = 0;
59
60
0/2
✗ Branch 23 → 3 not taken.
✗ Branch 23 → 24 not taken.
✗ while (w > 0) {
61
0/2
✗ Branch 4 → 5 not taken.
✗ Branch 4 → 6 not taken.
✗ if (bytestream2_get_bytes_left(gb) < 4)
62 ✗ return AVERROR_INVALIDDATA;
63 ✗ scanline[0] = bytestream2_get_byte(gb);
64 ✗ scanline[1] = bytestream2_get_byte(gb);
65 ✗ scanline[2] = bytestream2_get_byte(gb);
66 ✗ scanline[3] = bytestream2_get_byte(gb);
67
68
0/2
✗ Branch 10 → 11 not taken.
✗ Branch 10 → 21 not taken.
✗ if (scanline[0] == 1 &&
69
0/2
✗ Branch 11 → 12 not taken.
✗ Branch 11 → 21 not taken.
✗ scanline[1] == 1 &&
70
0/2
✗ Branch 12 → 13 not taken.
✗ Branch 12 → 21 not taken.
✗ scanline[2] == 1) {
71 ✗ int run = scanline[3];
72
0/6
✗ Branch 15 → 16 not taken.
✗ Branch 15 → 18 not taken.
✗ Branch 16 → 17 not taken.
✗ Branch 16 → 18 not taken.
✗ Branch 17 → 14 not taken.
✗ Branch 17 → 18 not taken.
✗ for (int i = run << rshift; i > 0 && w > 0 && scanline >= start + 4; i--) {
73 ✗ memcpy(scanline, scanline - 4, 4);
74 ✗ scanline += 4;
75 ✗ w--;
76 }
77 ✗ rshift += 8;
78
0/2
✗ Branch 18 → 19 not taken.
✗ Branch 18 → 20 not taken.
✗ if (rshift > 16)
79 ✗ break;
80 } else {
81 ✗ scanline += 4;
82 ✗ w--;
83 ✗ rshift = 0;
84 }
85 }
86
87 ✗ return 1;
88 }
89
90 ✗ static int hdr_decode_frame(AVCodecContext *avctx, AVFrame *p,
91 int *got_frame, AVPacket *avpkt)
92 {
93 ✗ int width = 0, height = 0;
94 GetByteContext gb;
95 uint8_t line[512];
96 float sar;
97 int ret;
98
99 ✗ bytestream2_init(&gb, avpkt->data, avpkt->size);
100 ✗ hdr_get_line(&gb, line, sizeof(line));
101
0/4
✗ Branch 4 → 5 not taken.
✗ Branch 4 → 7 not taken.
✗ Branch 5 → 6 not taken.
✗ Branch 5 → 7 not taken.
✗ if (memcmp("#?RADIANCE\n", line, 11) && memcmp("#?RGBE\n", line, 7))
102 ✗ return AVERROR_INVALIDDATA;
103
104 do {
105 ✗ hdr_get_line(&gb, line, sizeof(line));
106
0/2
✗ Branch 9 → 10 not taken.
✗ Branch 9 → 12 not taken.
✗ if (sscanf(line, "PIXASPECT=%f\n", &sar) == 1)
107 ✗ avctx->sample_aspect_ratio = p->sample_aspect_ratio = av_inv_q(av_d2q(sar, 4096));
108
0/4
✗ Branch 12 → 13 not taken.
✗ Branch 12 → 14 not taken.
✗ Branch 13 → 8 not taken.
✗ Branch 13 → 14 not taken.
✗ } while (line[0] != '\n' && line[0]);
109
110 ✗ hdr_get_line(&gb, line, sizeof(line));
111
0/2
✗ Branch 15 → 16 not taken.
✗ Branch 15 → 23 not taken.
✗ if (sscanf(line, "-Y %d +X %d\n", &height, &width) == 2) {
112 ;
113
0/2
✗ Branch 16 → 17 not taken.
✗ Branch 16 → 23 not taken.
✗ } else if (sscanf(line, "+Y %d +X %d\n", &height, &width) == 2) {
114 ;
115
0/2
✗ Branch 17 → 18 not taken.
✗ Branch 17 → 23 not taken.
✗ } else if (sscanf(line, "-Y %d -X %d\n", &height, &width) == 2) {
116 ;
117
0/2
✗ Branch 18 → 19 not taken.
✗ Branch 18 → 23 not taken.
✗ } else if (sscanf(line, "+Y %d -X %d\n", &height, &width) == 2) {
118 ;
119
0/2
✗ Branch 19 → 20 not taken.
✗ Branch 19 → 23 not taken.
✗ } else if (sscanf(line, "-X %d +Y %d\n", &width, &height) == 2) {
120 ;
121
0/2
✗ Branch 20 → 21 not taken.
✗ Branch 20 → 23 not taken.
✗ } else if (sscanf(line, "+X %d +Y %d\n", &width, &height) == 2) {
122 ;
123
0/2
✗ Branch 21 → 22 not taken.
✗ Branch 21 → 23 not taken.
✗ } else if (sscanf(line, "-X %d -Y %d\n", &width, &height) == 2) {
124 ;
125 ✗ } else if (sscanf(line, "+X %d -Y %d\n", &width, &height) == 2) {
126 ;
127 }
128
129
0/2
✗ Branch 24 → 25 not taken.
✗ Branch 24 → 26 not taken.
✗ if (bytestream2_get_bytes_left(&gb) < height * 4LL)
130 ✗ return AVERROR_INVALIDDATA;
131
132
0/2
✗ Branch 27 → 28 not taken.
✗ Branch 27 → 29 not taken.
✗ if ((ret = ff_set_dimensions(avctx, width, height)) < 0)
133 ✗ return ret;
134
135 ✗ avctx->pix_fmt = AV_PIX_FMT_GBRPF32;
136
137
0/2
✗ Branch 29 → 30 not taken.
✗ Branch 29 → 31 not taken.
✗ if (avctx->skip_frame >= AVDISCARD_ALL)
138 ✗ return avpkt->size;
139
140
0/2
✗ Branch 32 → 33 not taken.
✗ Branch 32 → 34 not taken.
✗ if ((ret = ff_thread_get_buffer(avctx, p, 0)) < 0)
141 ✗ return ret;
142
143
0/2
✗ Branch 89 → 35 not taken.
✗ Branch 89 → 90 not taken.
✗ for (int y = 0; y < height; y++) {
144 ✗ float *dst_r = (float *)(p->data[2] + y * p->linesize[2]);
145 ✗ float *dst_g = (float *)(p->data[0] + y * p->linesize[0]);
146 ✗ float *dst_b = (float *)(p->data[1] + y * p->linesize[1]);
147 ✗ uint8_t *scanline = p->data[0] + y * p->linesize[0];
148 int i;
149
150
0/4
✗ Branch 35 → 36 not taken.
✗ Branch 35 → 37 not taken.
✗ Branch 36 → 37 not taken.
✗ Branch 36 → 41 not taken.
✗ if (width < MINELEN || width > MAXELEN) {
151 ✗ ret = decompress(scanline, width, &gb, scanline);
152
0/2
✗ Branch 38 → 39 not taken.
✗ Branch 38 → 40 not taken.
✗ if (ret < 0)
153 ✗ return ret;
154 ✗ goto convert;
155 }
156
157 ✗ i = bytestream2_peek_byte(&gb);
158
0/2
✗ Branch 42 → 43 not taken.
✗ Branch 42 → 47 not taken.
✗ if (i != 2) {
159 ✗ ret = decompress(scanline, width, &gb, scanline);
160
0/2
✗ Branch 44 → 45 not taken.
✗ Branch 44 → 46 not taken.
✗ if (ret < 0)
161 ✗ return ret;
162 ✗ goto convert;
163 }
164 ✗ bytestream2_skip(&gb, 1);
165
166 ✗ scanline[1] = bytestream2_get_byte(&gb);
167 ✗ scanline[2] = bytestream2_get_byte(&gb);
168 ✗ i = bytestream2_get_byte(&gb);
169
170
0/4
✗ Branch 51 → 52 not taken.
✗ Branch 51 → 53 not taken.
✗ Branch 52 → 53 not taken.
✗ Branch 52 → 57 not taken.
✗ if (scanline[1] != 2 || scanline[2] & 128) {
171 ✗ scanline[0] = 2;
172 ✗ scanline[3] = i;
173 ✗ ret = decompress(scanline + 4, width - 1, &gb, scanline);
174
0/2
✗ Branch 54 → 55 not taken.
✗ Branch 54 → 56 not taken.
✗ if (ret < 0)
175 ✗ return ret;
176 ✗ goto convert;
177 }
178
179
0/2
✗ Branch 80 → 58 not taken.
✗ Branch 80 → 81 not taken.
✗ for (int i = 0; i < 4; i++) {
180 ✗ uint8_t *scanline = p->data[0] + y * p->linesize[0] + i;
181
182
0/4
✗ Branch 76 → 77 not taken.
✗ Branch 76 → 79 not taken.
✗ Branch 78 → 59 not taken.
✗ Branch 78 → 79 not taken.
✗ for (int j = 0; j < width * 4 && bytestream2_get_bytes_left(&gb) > 0;) {
183 ✗ int run = bytestream2_get_byte(&gb);
184
0/2
✗ Branch 60 → 61 not taken.
✗ Branch 60 → 68 not taken.
✗ if (run > 128) {
185 ✗ uint8_t val = bytestream2_get_byte(&gb);
186 ✗ run &= 127;
187
0/2
✗ Branch 66 → 63 not taken.
✗ Branch 66 → 67 not taken.
✗ while (run--) {
188
0/2
✗ Branch 63 → 64 not taken.
✗ Branch 63 → 65 not taken.
✗ if (j >= width * 4)
189 ✗ break;
190 ✗ scanline[j] = val;
191 ✗ j += 4;
192 }
193
0/2
✗ Branch 68 → 69 not taken.
✗ Branch 68 → 75 not taken.
✗ } else if (run > 0) {
194
0/2
✗ Branch 74 → 70 not taken.
✗ Branch 74 → 75 not taken.
✗ while (run--) {
195
0/2
✗ Branch 70 → 71 not taken.
✗ Branch 70 → 72 not taken.
✗ if (j >= width * 4)
196 ✗ break;
197 ✗ scanline[j] = bytestream2_get_byte(&gb);
198 ✗ j += 4;
199 }
200 }
201 }
202 }
203
204 ✗ convert:
205
0/2
✗ Branch 87 → 83 not taken.
✗ Branch 87 → 88 not taken.
✗ for (int x = 0; x < width; x++) {
206 uint8_t rgbe[4];
207 int expo;
208
209 ✗ memcpy(rgbe, p->data[0] + y * p->linesize[0] + x * 4, 4);
210 ✗ expo = rgbe[3] - 128;
211
212 ✗ dst_r[x] = convert(expo, rgbe[0]);
213 ✗ dst_b[x] = convert(expo, rgbe[2]);
214 ✗ dst_g[x] = convert(expo, rgbe[1]);
215 }
216 }
217
218 ✗ *got_frame = 1;
219
220 ✗ return avpkt->size;
221 }
222
223 const FFCodec ff_hdr_decoder = {
224 .p.name = "hdr",
225 CODEC_LONG_NAME("HDR (Radiance RGBE format) image"),
226 .p.type = AVMEDIA_TYPE_VIDEO,
227 .p.id = AV_CODEC_ID_RADIANCE_HDR,
228 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
229 .caps_internal = FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM,
230 FF_CODEC_DECODE_CB(hdr_decode_frame),
231 };
232