FFmpeg coverage


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