../../../ffmpeg/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
133 / 190
70.0%
Branches:
50 / 78
64.1%
Functions:
7 / 7
100.0%

libavcodec/dpxenc.c
Line Branch Exec Source
1 /*
2 * DPX (.dpx) image encoder
3 * Copyright (c) 2011 Peter Ross <pross@xvid.org>
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include "libavutil/attributes.h"
23 #include "libavutil/common.h"
24 #include "libavutil/intreadwrite.h"
25 #include "libavutil/imgutils.h"
26 #include "avcodec.h"
27 #include "codec_internal.h"
28 #include "encode.h"
29 #include "version.h"
30 #include "dpx.h"
31
32 typedef struct DPXContext {
33 int big_endian;
34 int bits_per_component;
35 int num_components;
36 int descriptor;
37 int planar;
38 } DPXContext;
39
40 6 static av_cold int encode_init(AVCodecContext *avctx)
41 {
42 6 DPXContext *s = avctx->priv_data;
43 6 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
44
45 6 s->big_endian = !!(desc->flags & AV_PIX_FMT_FLAG_BE);
46 6 s->bits_per_component = desc->comp[0].depth;
47 6 s->num_components = desc->nb_components;
48
2/2
✓ Branch 3 → 4 taken 1 time.
✓ Branch 3 → 5 taken 5 times.
6 s->descriptor = (desc->flags & AV_PIX_FMT_FLAG_ALPHA) ? 51 : 50;
49 6 s->planar = !!(desc->flags & AV_PIX_FMT_FLAG_PLANAR);
50
51
2/5
✗ Branch 6 → 7 not taken.
✗ Branch 6 → 8 not taken.
✓ Branch 6 → 9 taken 4 times.
✓ Branch 6 → 10 taken 2 times.
✗ Branch 6 → 17 not taken.
6 switch (avctx->pix_fmt) {
52 ✗ case AV_PIX_FMT_ABGR:
53 ✗ s->descriptor = 52;
54 ✗ break;
55 ✗ case AV_PIX_FMT_GRAY16BE:
56 case AV_PIX_FMT_GRAY16LE:
57 case AV_PIX_FMT_GRAY8:
58 ✗ s->descriptor = 6;
59 ✗ break;
60 4 case AV_PIX_FMT_GBRP10BE:
61 case AV_PIX_FMT_GBRP10LE:
62 case AV_PIX_FMT_GBRP12BE:
63 case AV_PIX_FMT_GBRP12LE:
64 case AV_PIX_FMT_RGB24:
65 case AV_PIX_FMT_RGBA64BE:
66 case AV_PIX_FMT_RGBA64LE:
67 case AV_PIX_FMT_RGBA:
68 4 break;
69 2 case AV_PIX_FMT_RGB48LE:
70 case AV_PIX_FMT_RGB48BE:
71
2/2
✓ Branch 10 → 11 taken 1 time.
✓ Branch 10 → 12 taken 1 time.
2 if (avctx->bits_per_raw_sample == 10) {
72 1 s->bits_per_component = 10;
73
1/4
✗ Branch 12 → 13 not taken.
✓ Branch 12 → 16 taken 1 time.
✗ Branch 13 → 14 not taken.
✗ Branch 13 → 16 not taken.
1 } else if (avctx->bits_per_raw_sample && avctx->bits_per_raw_sample != 16) {
74 ✗ av_log(avctx, AV_LOG_ERROR, "Unsupported bit depth: %d\n", avctx->bits_per_raw_sample);
75 ✗ return AVERROR(EINVAL);
76 }
77 2 break;
78 }
79
80 6 return 0;
81 }
82
83 3953898 static av_always_inline void write16_internal(int big_endian, void *p, int value)
84 {
85
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 3953898 times.
3953898 if (big_endian) AV_WB16(p, value);
86 3953898 else AV_WL16(p, value);
87 3953898 }
88
89 2636634 static av_always_inline void write32_internal(int big_endian, void *p, int value)
90 {
91
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 2636634 times.
2636634 if (big_endian) AV_WB32(p, value);
92 2636634 else AV_WL32(p, value);
93 2636634 }
94
95 #define write16(p, value) write16_internal(s->big_endian, p, value)
96 #define write32(p, value) write32_internal(s->big_endian, p, value)
97
98 13 static void encode_rgb48_10bit(AVCodecContext *avctx, const AVFrame *pic,
99 uint8_t *dst)
100 {
101 13 DPXContext *s = avctx->priv_data;
102 13 const uint8_t *src = pic->data[0];
103 int x, y;
104
105
2/2
✓ Branch 11 → 3 taken 3744 times.
✓ Branch 11 → 12 taken 13 times.
3757 for (y = 0; y < avctx->height; y++) {
106
2/2
✓ Branch 9 → 4 taken 1317888 times.
✓ Branch 9 → 10 taken 3744 times.
1321632 for (x = 0; x < avctx->width; x++) {
107 int value;
108
1/2
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 1317888 times.
1317888 if (s->big_endian) {
109 ✗ value = ((AV_RB16(src + 6*x + 4) & 0xFFC0U) >> 4)
110 ✗ | ((AV_RB16(src + 6*x + 2) & 0xFFC0U) << 6)
111 ✗ | ((AV_RB16(src + 6*x + 0) & 0xFFC0U) << 16);
112 } else {
113 1317888 value = ((AV_RL16(src + 6*x + 4) & 0xFFC0U) >> 4)
114 1317888 | ((AV_RL16(src + 6*x + 2) & 0xFFC0U) << 6)
115 1317888 | ((AV_RL16(src + 6*x + 0) & 0xFFC0U) << 16);
116 }
117 1317888 write32(dst, value);
118 1317888 dst += 4;
119 }
120 3744 src += pic->linesize[0];
121 }
122 13 }
123
124 13 static void encode_gbrp10(AVCodecContext *avctx, const AVFrame *pic, uint8_t *dst)
125 {
126 13 DPXContext *s = avctx->priv_data;
127 13 const uint8_t *src[3] = {pic->data[0], pic->data[1], pic->data[2]};
128 int x, y, i;
129
130
2/2
✓ Branch 14 → 3 taken 3744 times.
✓ Branch 14 → 15 taken 13 times.
3757 for (y = 0; y < avctx->height; y++) {
131
2/2
✓ Branch 9 → 4 taken 1317888 times.
✓ Branch 9 → 10 taken 3744 times.
1321632 for (x = 0; x < avctx->width; x++) {
132 int value;
133
1/2
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 1317888 times.
1317888 if (s->big_endian) {
134 ✗ value = (AV_RB16(src[0] + 2*x) << 12)
135 ✗ | (AV_RB16(src[1] + 2*x) << 2)
136 ✗ | ((unsigned)AV_RB16(src[2] + 2*x) << 22);
137 } else {
138 1317888 value = (AV_RL16(src[0] + 2*x) << 12)
139 1317888 | (AV_RL16(src[1] + 2*x) << 2)
140 1317888 | ((unsigned)AV_RL16(src[2] + 2*x) << 22);
141 }
142 1317888 write32(dst, value);
143 1317888 dst += 4;
144 }
145
2/2
✓ Branch 12 → 11 taken 11232 times.
✓ Branch 12 → 13 taken 3744 times.
14976 for (i = 0; i < 3; i++)
146 11232 src[i] += pic->linesize[i];
147 }
148 13 }
149
150 13 static void encode_gbrp12(AVCodecContext *avctx, const AVFrame *pic, uint8_t *dst)
151 {
152 13 DPXContext *s = avctx->priv_data;
153 13 const uint16_t *src[3] = {(uint16_t*)pic->data[0],
154 13 (uint16_t*)pic->data[1],
155 13 (uint16_t*)pic->data[2]};
156 int x, y, i, pad;
157 13 pad = avctx->width*6;
158 13 pad = (FFALIGN(pad, 4) - pad) >> 1;
159
2/2
✓ Branch 20 → 3 taken 3744 times.
✓ Branch 20 → 21 taken 13 times.
3757 for (y = 0; y < avctx->height; y++) {
160
2/2
✓ Branch 12 → 4 taken 1317888 times.
✓ Branch 12 → 13 taken 3744 times.
1321632 for (x = 0; x < avctx->width; x++) {
161 uint16_t value[3];
162
1/2
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 1317888 times.
1317888 if (s->big_endian) {
163 ✗ value[1] = AV_RB16(src[0] + x) << 4;
164 ✗ value[2] = AV_RB16(src[1] + x) << 4;
165 ✗ value[0] = AV_RB16(src[2] + x) << 4;
166 } else {
167 1317888 value[1] = AV_RL16(src[0] + x) << 4;
168 1317888 value[2] = AV_RL16(src[1] + x) << 4;
169 1317888 value[0] = AV_RL16(src[2] + x) << 4;
170 }
171
2/2
✓ Branch 10 → 8 taken 3953664 times.
✓ Branch 10 → 11 taken 1317888 times.
5271552 for (i = 0; i < 3; i++, dst += 2)
172 3953664 write16(dst, value[i]);
173 }
174
1/2
✗ Branch 15 → 14 not taken.
✓ Branch 15 → 16 taken 3744 times.
3744 for (i = 0; i < pad; i++, dst += 2)
175 ✗ AV_WN16(dst, 0);
176
2/2
✓ Branch 18 → 17 taken 11232 times.
✓ Branch 18 → 19 taken 3744 times.
14976 for (i = 0; i < 3; i++)
177 11232 src[i] += pic->linesize[i]/2;
178 }
179 13 }
180
181 78 static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
182 const AVFrame *frame, int *got_packet)
183 {
184 78 DPXContext *s = avctx->priv_data;
185 int size, ret, need_align, len;
186 uint8_t *buf;
187 int color_trc, color_spec;
188
189 #define HEADER_SIZE 1664 /* DPX Generic header */
190
2/2
✓ Branch 2 → 3 taken 26 times.
✓ Branch 2 → 4 taken 52 times.
78 if (s->bits_per_component == 10)
191 26 size = avctx->height * avctx->width * 4;
192
2/2
✓ Branch 4 → 5 taken 13 times.
✓ Branch 4 → 6 taken 39 times.
52 else if (s->bits_per_component == 12) {
193 // 3 components, 12 bits put on 16 bits
194 13 len = avctx->width*6;
195 13 size = FFALIGN(len, 4);
196 13 need_align = size - len;
197 13 size *= avctx->height;
198 } else {
199 // N components, M bits
200 39 len = avctx->width * s->num_components * s->bits_per_component >> 3;
201 39 size = FFALIGN(len, 4);
202 39 need_align = size - len;
203 39 size *= avctx->height;
204 }
205
1/2
✗ Branch 8 → 9 not taken.
✓ Branch 8 → 10 taken 78 times.
78 if ((ret = ff_get_encode_buffer(avctx, pkt, size + HEADER_SIZE, 0)) < 0)
206 ✗ return ret;
207 78 buf = pkt->data;
208
209 78 memset(buf, 0, HEADER_SIZE);
210
211 /* File information header */
212 78 write32(buf, MKBETAG('S','D','P','X'));
213 78 write32(buf + 4, HEADER_SIZE);
214 78 memcpy (buf + 8, "V1.0", 4);
215 78 write32(buf + 20, 1); /* new image */
216 78 write32(buf + 24, HEADER_SIZE);
217
1/2
✗ Branch 14 → 15 not taken.
✓ Branch 14 → 16 taken 78 times.
78 if (!(avctx->flags & AV_CODEC_FLAG_BITEXACT))
218 ✗ memcpy (buf + 160, LIBAVCODEC_IDENT, FFMIN(sizeof(LIBAVCODEC_IDENT), 100));
219 78 write32(buf + 660, 0xFFFFFFFF); /* unencrypted */
220
221 /* Image information header */
222 78 write16(buf + 768, 0); /* orientation; left to right, top to bottom */
223 78 write16(buf + 770, 1); /* number of elements */
224 78 write32(buf + 772, avctx->width);
225 78 write32(buf + 776, avctx->height);
226 78 buf[800] = s->descriptor;
227
228
1/6
✗ Branch 21 → 22 not taken.
✗ Branch 21 → 23 not taken.
✓ Branch 21 → 24 taken 78 times.
✗ Branch 21 → 25 not taken.
✗ Branch 21 → 26 not taken.
✗ Branch 21 → 27 not taken.
78 switch (avctx->color_trc) {
229 ✗ case AVCOL_TRC_BT709:
230 ✗ color_trc = DPX_TRC_ITU_R_709_4;
231 ✗ break;
232 ✗ default:
233 ✗ av_log(avctx, AV_LOG_WARNING, "unsupported color transfer\n");
234 av_fallthrough;
235 78 case AVCOL_TRC_UNSPECIFIED:
236 78 color_trc = DPX_TRC_UNSPECIFIED_VIDEO;
237 78 break;
238 ✗ case AVCOL_TRC_GAMMA28:
239 ✗ color_trc = DPX_TRC_ITU_R_624_4_PAL;
240 ✗ break;
241 ✗ case AVCOL_TRC_SMPTE170M:
242 ✗ color_trc = DPX_TRC_SMPTE_170;
243 ✗ break;
244 ✗ case AVCOL_TRC_LINEAR:
245 ✗ color_trc = DPX_TRC_LINEAR;
246 ✗ break;
247 }
248 78 buf[801] = color_trc;
249
250
1/5
✗ Branch 28 → 29 not taken.
✗ Branch 28 → 30 not taken.
✓ Branch 28 → 31 taken 78 times.
✗ Branch 28 → 32 not taken.
✗ Branch 28 → 33 not taken.
78 switch (avctx->color_primaries) {
251 ✗ case AVCOL_PRI_BT709:
252 ✗ color_spec = DPX_COL_SPEC_ITU_R_709_4;
253 ✗ break;
254 ✗ default:
255 ✗ av_log(avctx, AV_LOG_WARNING, "unsupported colorimetric specification\n");
256 av_fallthrough;
257 78 case AVCOL_PRI_UNSPECIFIED:
258 78 color_spec = DPX_COL_SPEC_UNSPECIFIED_VIDEO;
259 78 break;
260 ✗ case AVCOL_PRI_BT470BG:
261 ✗ color_spec = DPX_COL_SPEC_ITU_R_624_4_PAL;
262 ✗ break;
263 ✗ case AVCOL_PRI_SMPTE170M:
264 ✗ color_spec = DPX_COL_SPEC_SMPTE_170;
265 ✗ break;
266 }
267 78 buf[802] = color_spec;
268
269 78 buf[803] = s->bits_per_component;
270
4/4
✓ Branch 34 → 35 taken 52 times.
✓ Branch 34 → 36 taken 26 times.
✓ Branch 35 → 36 taken 13 times.
✓ Branch 35 → 37 taken 39 times.
78 write16(buf + 804, (s->bits_per_component == 10 || s->bits_per_component == 12) ?
271 1 : 0); /* packing method */
272 78 write32(buf + 808, HEADER_SIZE); /* data offset */
273
274 /* Image source information header */
275 78 write32(buf + 1628, avctx->sample_aspect_ratio.num);
276 78 write32(buf + 1632, avctx->sample_aspect_ratio.den);
277
278
3/4
✓ Branch 42 → 43 taken 39 times.
✓ Branch 42 → 51 taken 26 times.
✓ Branch 42 → 55 taken 13 times.
✗ Branch 42 → 56 not taken.
78 switch(s->bits_per_component) {
279 39 case 8:
280 case 16:
281
1/2
✗ Branch 43 → 44 not taken.
✓ Branch 43 → 47 taken 39 times.
39 if (need_align) {
282 int j;
283 ✗ const uint8_t *src = frame->data[0];
284 ✗ uint8_t *dst = pkt->data + HEADER_SIZE;
285 ✗ size = (len + need_align) * avctx->height;
286
0/2
✗ Branch 46 → 45 not taken.
✗ Branch 46 → 48 not taken.
✗ for (j=0; j<avctx->height; j++) {
287 ✗ memcpy(dst, src, len);
288 ✗ memset(dst + len, 0, need_align);
289 ✗ dst += len + need_align;
290 ✗ src += frame->linesize[0];
291 }
292 } else {
293 39 size = av_image_copy_to_buffer(buf + HEADER_SIZE, pkt->size - HEADER_SIZE,
294 39 (const uint8_t**)frame->data, frame->linesize,
295 avctx->pix_fmt,
296 avctx->width, avctx->height, 1);
297 }
298
1/2
✗ Branch 48 → 49 not taken.
✓ Branch 48 → 50 taken 39 times.
39 if (size < 0)
299 ✗ return size;
300 39 break;
301 26 case 10:
302
2/2
✓ Branch 51 → 52 taken 13 times.
✓ Branch 51 → 53 taken 13 times.
26 if (s->planar)
303 13 encode_gbrp10(avctx, frame, buf + HEADER_SIZE);
304 else
305 13 encode_rgb48_10bit(avctx, frame, buf + HEADER_SIZE);
306 26 break;
307 13 case 12:
308 13 encode_gbrp12(avctx, frame, buf + HEADER_SIZE);
309 13 break;
310 ✗ default:
311 ✗ av_log(avctx, AV_LOG_ERROR, "Unsupported bit depth: %d\n", s->bits_per_component);
312 ✗ return -1;
313 }
314
315 78 size += HEADER_SIZE;
316
317 78 write32(buf + 16, size); /* file size */
318
319 78 *got_packet = 1;
320
321 78 return 0;
322 }
323
324 const FFCodec ff_dpx_encoder = {
325 .p.name = "dpx",
326 CODEC_LONG_NAME("DPX (Digital Picture Exchange) image"),
327 .p.type = AVMEDIA_TYPE_VIDEO,
328 .p.id = AV_CODEC_ID_DPX,
329 .p.capabilities = AV_CODEC_CAP_DR1,
330 .priv_data_size = sizeof(DPXContext),
331 .init = encode_init,
332 FF_CODEC_ENCODE_CB(encode_frame),
333 CODEC_PIXFMTS(AV_PIX_FMT_GRAY8,
334 AV_PIX_FMT_RGB24, AV_PIX_FMT_RGBA, AV_PIX_FMT_ABGR,
335 AV_PIX_FMT_GRAY16LE, AV_PIX_FMT_GRAY16BE,
336 AV_PIX_FMT_RGB48LE, AV_PIX_FMT_RGB48BE,
337 AV_PIX_FMT_RGBA64LE, AV_PIX_FMT_RGBA64BE,
338 AV_PIX_FMT_GBRP10LE, AV_PIX_FMT_GBRP10BE,
339 AV_PIX_FMT_GBRP12LE, AV_PIX_FMT_GBRP12BE),
340 };
341