FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/dpxenc.c
Date: 2021-09-24 20:55:06
Exec Total Coverage
Lines: 125 158 79.1%
Branches: 47 63 74.6%

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/common.h"
23 #include "libavutil/intreadwrite.h"
24 #include "libavutil/imgutils.h"
25 #include "avcodec.h"
26 #include "encode.h"
27 #include "internal.h"
28
29 typedef struct DPXContext {
30 int big_endian;
31 int bits_per_component;
32 int num_components;
33 int descriptor;
34 int planar;
35 } DPXContext;
36
37 6 static av_cold int encode_init(AVCodecContext *avctx)
38 {
39 6 DPXContext *s = avctx->priv_data;
40 6 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
41
42 6 s->big_endian = !!(desc->flags & AV_PIX_FMT_FLAG_BE);
43 6 s->bits_per_component = desc->comp[0].depth;
44 6 s->num_components = desc->nb_components;
45
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 5 times.
6 s->descriptor = (desc->flags & AV_PIX_FMT_FLAG_ALPHA) ? 51 : 50;
46 6 s->planar = !!(desc->flags & AV_PIX_FMT_FLAG_PLANAR);
47
48
2/5
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 2 times.
✗ Branch 4 not taken.
6 switch (avctx->pix_fmt) {
49 case AV_PIX_FMT_ABGR:
50 s->descriptor = 52;
51 break;
52 case AV_PIX_FMT_GRAY16BE:
53 case AV_PIX_FMT_GRAY16LE:
54 case AV_PIX_FMT_GRAY8:
55 s->descriptor = 6;
56 break;
57 4 case AV_PIX_FMT_GBRP10BE:
58 case AV_PIX_FMT_GBRP10LE:
59 case AV_PIX_FMT_GBRP12BE:
60 case AV_PIX_FMT_GBRP12LE:
61 case AV_PIX_FMT_RGB24:
62 case AV_PIX_FMT_RGBA64BE:
63 case AV_PIX_FMT_RGBA64LE:
64 case AV_PIX_FMT_RGBA:
65 4 break;
66 2 case AV_PIX_FMT_RGB48LE:
67 case AV_PIX_FMT_RGB48BE:
68
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (avctx->bits_per_raw_sample)
69 1 s->bits_per_component = avctx->bits_per_raw_sample;
70 2 break;
71 default:
72 av_log(avctx, AV_LOG_INFO, "unsupported pixel format\n");
73 return -1;
74 }
75
76 6 return 0;
77 }
78
79 3953898 static av_always_inline void write16_internal(int big_endian, void *p, int value)
80 {
81
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3953898 times.
3953898 if (big_endian) AV_WB16(p, value);
82 3953898 else AV_WL16(p, value);
83 3953898 }
84
85 2636634 static av_always_inline void write32_internal(int big_endian, void *p, int value)
86 {
87
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2636634 times.
2636634 if (big_endian) AV_WB32(p, value);
88 2636634 else AV_WL32(p, value);
89 2636634 }
90
91 #define write16(p, value) write16_internal(s->big_endian, p, value)
92 #define write32(p, value) write32_internal(s->big_endian, p, value)
93
94 13 static void encode_rgb48_10bit(AVCodecContext *avctx, const AVFrame *pic,
95 uint8_t *dst)
96 {
97 13 DPXContext *s = avctx->priv_data;
98 13 const uint8_t *src = pic->data[0];
99 int x, y;
100
101
2/2
✓ Branch 0 taken 3744 times.
✓ Branch 1 taken 13 times.
3757 for (y = 0; y < avctx->height; y++) {
102
2/2
✓ Branch 0 taken 1317888 times.
✓ Branch 1 taken 3744 times.
1321632 for (x = 0; x < avctx->width; x++) {
103 int value;
104
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1317888 times.
1317888 if (s->big_endian) {
105 value = ((AV_RB16(src + 6*x + 4) & 0xFFC0U) >> 4)
106 | ((AV_RB16(src + 6*x + 2) & 0xFFC0U) << 6)
107 | ((AV_RB16(src + 6*x + 0) & 0xFFC0U) << 16);
108 } else {
109 1317888 value = ((AV_RL16(src + 6*x + 4) & 0xFFC0U) >> 4)
110 1317888 | ((AV_RL16(src + 6*x + 2) & 0xFFC0U) << 6)
111 1317888 | ((AV_RL16(src + 6*x + 0) & 0xFFC0U) << 16);
112 }
113 1317888 write32(dst, value);
114 1317888 dst += 4;
115 }
116 3744 src += pic->linesize[0];
117 }
118 13 }
119
120 13 static void encode_gbrp10(AVCodecContext *avctx, const AVFrame *pic, uint8_t *dst)
121 {
122 13 DPXContext *s = avctx->priv_data;
123 13 const uint8_t *src[3] = {pic->data[0], pic->data[1], pic->data[2]};
124 int x, y, i;
125
126
2/2
✓ Branch 0 taken 3744 times.
✓ Branch 1 taken 13 times.
3757 for (y = 0; y < avctx->height; y++) {
127
2/2
✓ Branch 0 taken 1317888 times.
✓ Branch 1 taken 3744 times.
1321632 for (x = 0; x < avctx->width; x++) {
128 int value;
129
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1317888 times.
1317888 if (s->big_endian) {
130 value = (AV_RB16(src[0] + 2*x) << 12)
131 | (AV_RB16(src[1] + 2*x) << 2)
132 | ((unsigned)AV_RB16(src[2] + 2*x) << 22);
133 } else {
134 1317888 value = (AV_RL16(src[0] + 2*x) << 12)
135 1317888 | (AV_RL16(src[1] + 2*x) << 2)
136 1317888 | ((unsigned)AV_RL16(src[2] + 2*x) << 22);
137 }
138 1317888 write32(dst, value);
139 1317888 dst += 4;
140 }
141
2/2
✓ Branch 0 taken 11232 times.
✓ Branch 1 taken 3744 times.
14976 for (i = 0; i < 3; i++)
142 11232 src[i] += pic->linesize[i];
143 }
144 13 }
145
146 13 static void encode_gbrp12(AVCodecContext *avctx, const AVFrame *pic, uint8_t *dst)
147 {
148 13 DPXContext *s = avctx->priv_data;
149 13 const uint16_t *src[3] = {(uint16_t*)pic->data[0],
150 13 (uint16_t*)pic->data[1],
151 13 (uint16_t*)pic->data[2]};
152 int x, y, i, pad;
153 13 pad = avctx->width*6;
154 13 pad = (FFALIGN(pad, 4) - pad) >> 1;
155
2/2
✓ Branch 0 taken 3744 times.
✓ Branch 1 taken 13 times.
3757 for (y = 0; y < avctx->height; y++) {
156
2/2
✓ Branch 0 taken 1317888 times.
✓ Branch 1 taken 3744 times.
1321632 for (x = 0; x < avctx->width; x++) {
157 uint16_t value[3];
158
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1317888 times.
1317888 if (s->big_endian) {
159 value[1] = AV_RB16(src[0] + x) << 4;
160 value[2] = AV_RB16(src[1] + x) << 4;
161 value[0] = AV_RB16(src[2] + x) << 4;
162 } else {
163 1317888 value[1] = AV_RL16(src[0] + x) << 4;
164 1317888 value[2] = AV_RL16(src[1] + x) << 4;
165 1317888 value[0] = AV_RL16(src[2] + x) << 4;
166 }
167
2/2
✓ Branch 0 taken 3953664 times.
✓ Branch 1 taken 1317888 times.
5271552 for (i = 0; i < 3; i++, dst += 2)
168 3953664 write16(dst, value[i]);
169 }
170
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3744 times.
3744 for (i = 0; i < pad; i++, dst += 2)
171 AV_WN16(dst, 0);
172
2/2
✓ Branch 0 taken 11232 times.
✓ Branch 1 taken 3744 times.
14976 for (i = 0; i < 3; i++)
173 11232 src[i] += pic->linesize[i]/2;
174 }
175 13 }
176
177 78 static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
178 const AVFrame *frame, int *got_packet)
179 {
180 78 DPXContext *s = avctx->priv_data;
181 int size, ret, need_align, len;
182 uint8_t *buf;
183
184 #define HEADER_SIZE 1664 /* DPX Generic header */
185
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 52 times.
78 if (s->bits_per_component == 10)
186 26 size = avctx->height * avctx->width * 4;
187
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 39 times.
52 else if (s->bits_per_component == 12) {
188 // 3 components, 12 bits put on 16 bits
189 13 len = avctx->width*6;
190 13 size = FFALIGN(len, 4);
191 13 need_align = size - len;
192 13 size *= avctx->height;
193 } else {
194 // N components, M bits
195 39 len = avctx->width * s->num_components * s->bits_per_component >> 3;
196 39 size = FFALIGN(len, 4);
197 39 need_align = size - len;
198 39 size *= avctx->height;
199 }
200
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 78 times.
78 if ((ret = ff_get_encode_buffer(avctx, pkt, size + HEADER_SIZE, 0)) < 0)
201 return ret;
202 78 buf = pkt->data;
203
204 78 memset(buf, 0, HEADER_SIZE);
205
206 /* File information header */
207 78 write32(buf, MKBETAG('S','D','P','X'));
208 78 write32(buf + 4, HEADER_SIZE);
209 78 memcpy (buf + 8, "V1.0", 4);
210 78 write32(buf + 20, 1); /* new image */
211 78 write32(buf + 24, HEADER_SIZE);
212
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 78 times.
78 if (!(avctx->flags & AV_CODEC_FLAG_BITEXACT))
213 memcpy (buf + 160, LIBAVCODEC_IDENT, FFMIN(sizeof(LIBAVCODEC_IDENT), 100));
214 78 write32(buf + 660, 0xFFFFFFFF); /* unencrypted */
215
216 /* Image information header */
217 78 write16(buf + 768, 0); /* orientation; left to right, top to bottom */
218 78 write16(buf + 770, 1); /* number of elements */
219 78 write32(buf + 772, avctx->width);
220 78 write32(buf + 776, avctx->height);
221 78 buf[800] = s->descriptor;
222 78 buf[801] = 2; /* linear transfer */
223 78 buf[802] = 2; /* linear colorimetric */
224 78 buf[803] = s->bits_per_component;
225
4/4
✓ Branch 0 taken 52 times.
✓ Branch 1 taken 26 times.
✓ Branch 2 taken 13 times.
✓ Branch 3 taken 39 times.
78 write16(buf + 804, (s->bits_per_component == 10 || s->bits_per_component == 12) ?
226 1 : 0); /* packing method */
227 78 write32(buf + 808, HEADER_SIZE); /* data offset */
228
229 /* Image source information header */
230 78 write32(buf + 1628, avctx->sample_aspect_ratio.num);
231 78 write32(buf + 1632, avctx->sample_aspect_ratio.den);
232
233
3/4
✓ Branch 0 taken 39 times.
✓ Branch 1 taken 26 times.
✓ Branch 2 taken 13 times.
✗ Branch 3 not taken.
78 switch(s->bits_per_component) {
234 39 case 8:
235 case 16:
236
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 39 times.
39 if (need_align) {
237 int j;
238 const uint8_t *src = frame->data[0];
239 uint8_t *dst = pkt->data + HEADER_SIZE;
240 size = (len + need_align) * avctx->height;
241 for (j=0; j<avctx->height; j++) {
242 memcpy(dst, src, len);
243 memset(dst + len, 0, need_align);
244 dst += len + need_align;
245 src += frame->linesize[0];
246 }
247 } else {
248 39 size = av_image_copy_to_buffer(buf + HEADER_SIZE, pkt->size - HEADER_SIZE,
249 39 (const uint8_t**)frame->data, frame->linesize,
250 avctx->pix_fmt,
251 avctx->width, avctx->height, 1);
252 }
253
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 39 times.
39 if (size < 0)
254 return size;
255 39 break;
256 26 case 10:
257
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 13 times.
26 if (s->planar)
258 13 encode_gbrp10(avctx, frame, buf + HEADER_SIZE);
259 else
260 13 encode_rgb48_10bit(avctx, frame, buf + HEADER_SIZE);
261 26 break;
262 13 case 12:
263 13 encode_gbrp12(avctx, frame, buf + HEADER_SIZE);
264 13 break;
265 default:
266 av_log(avctx, AV_LOG_ERROR, "Unsupported bit depth: %d\n", s->bits_per_component);
267 return -1;
268 }
269
270 78 size += HEADER_SIZE;
271
272 78 write32(buf + 16, size); /* file size */
273
274 78 pkt->flags |= AV_PKT_FLAG_KEY;
275 78 *got_packet = 1;
276
277 78 return 0;
278 }
279
280 const AVCodec ff_dpx_encoder = {
281 .name = "dpx",
282 .long_name = NULL_IF_CONFIG_SMALL("DPX (Digital Picture Exchange) image"),
283 .type = AVMEDIA_TYPE_VIDEO,
284 .id = AV_CODEC_ID_DPX,
285 .capabilities = AV_CODEC_CAP_DR1,
286 .priv_data_size = sizeof(DPXContext),
287 .init = encode_init,
288 .encode2 = encode_frame,
289 .pix_fmts = (const enum AVPixelFormat[]){
290 AV_PIX_FMT_GRAY8,
291 AV_PIX_FMT_RGB24, AV_PIX_FMT_RGBA, AV_PIX_FMT_ABGR,
292 AV_PIX_FMT_GRAY16LE, AV_PIX_FMT_GRAY16BE,
293 AV_PIX_FMT_RGB48LE, AV_PIX_FMT_RGB48BE,
294 AV_PIX_FMT_RGBA64LE, AV_PIX_FMT_RGBA64BE,
295 AV_PIX_FMT_GBRP10LE, AV_PIX_FMT_GBRP10BE,
296 AV_PIX_FMT_GBRP12LE, AV_PIX_FMT_GBRP12BE,
297 AV_PIX_FMT_NONE},
298 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
299 };
300