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 "codec_internal.h" | ||
27 | #include "encode.h" | ||
28 | #include "version.h" | ||
29 | #include "dpx.h" | ||
30 | |||
31 | typedef struct DPXContext { | ||
32 | int big_endian; | ||
33 | int bits_per_component; | ||
34 | int num_components; | ||
35 | int descriptor; | ||
36 | int planar; | ||
37 | } DPXContext; | ||
38 | |||
39 | 6 | static av_cold int encode_init(AVCodecContext *avctx) | |
40 | { | ||
41 | 6 | DPXContext *s = avctx->priv_data; | |
42 | 6 | const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt); | |
43 | |||
44 | 6 | s->big_endian = !!(desc->flags & AV_PIX_FMT_FLAG_BE); | |
45 | 6 | s->bits_per_component = desc->comp[0].depth; | |
46 | 6 | s->num_components = desc->nb_components; | |
47 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 5 times.
|
6 | s->descriptor = (desc->flags & AV_PIX_FMT_FLAG_ALPHA) ? 51 : 50; |
48 | 6 | s->planar = !!(desc->flags & AV_PIX_FMT_FLAG_PLANAR); | |
49 | |||
50 |
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) { |
51 | ✗ | case AV_PIX_FMT_ABGR: | |
52 | ✗ | s->descriptor = 52; | |
53 | ✗ | break; | |
54 | ✗ | case AV_PIX_FMT_GRAY16BE: | |
55 | case AV_PIX_FMT_GRAY16LE: | ||
56 | case AV_PIX_FMT_GRAY8: | ||
57 | ✗ | s->descriptor = 6; | |
58 | ✗ | break; | |
59 | 4 | case AV_PIX_FMT_GBRP10BE: | |
60 | case AV_PIX_FMT_GBRP10LE: | ||
61 | case AV_PIX_FMT_GBRP12BE: | ||
62 | case AV_PIX_FMT_GBRP12LE: | ||
63 | case AV_PIX_FMT_RGB24: | ||
64 | case AV_PIX_FMT_RGBA64BE: | ||
65 | case AV_PIX_FMT_RGBA64LE: | ||
66 | case AV_PIX_FMT_RGBA: | ||
67 | 4 | break; | |
68 | 2 | case AV_PIX_FMT_RGB48LE: | |
69 | case AV_PIX_FMT_RGB48BE: | ||
70 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | if (avctx->bits_per_raw_sample) |
71 | 1 | s->bits_per_component = avctx->bits_per_raw_sample; | |
72 | 2 | break; | |
73 | } | ||
74 | |||
75 | 6 | return 0; | |
76 | } | ||
77 | |||
78 | 3953898 | static av_always_inline void write16_internal(int big_endian, void *p, int value) | |
79 | { | ||
80 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3953898 times.
|
3953898 | if (big_endian) AV_WB16(p, value); |
81 | 3953898 | else AV_WL16(p, value); | |
82 | 3953898 | } | |
83 | |||
84 | 2636634 | static av_always_inline void write32_internal(int big_endian, void *p, int value) | |
85 | { | ||
86 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2636634 times.
|
2636634 | if (big_endian) AV_WB32(p, value); |
87 | 2636634 | else AV_WL32(p, value); | |
88 | 2636634 | } | |
89 | |||
90 | #define write16(p, value) write16_internal(s->big_endian, p, value) | ||
91 | #define write32(p, value) write32_internal(s->big_endian, p, value) | ||
92 | |||
93 | 13 | static void encode_rgb48_10bit(AVCodecContext *avctx, const AVFrame *pic, | |
94 | uint8_t *dst) | ||
95 | { | ||
96 | 13 | DPXContext *s = avctx->priv_data; | |
97 | 13 | const uint8_t *src = pic->data[0]; | |
98 | int x, y; | ||
99 | |||
100 |
2/2✓ Branch 0 taken 3744 times.
✓ Branch 1 taken 13 times.
|
3757 | for (y = 0; y < avctx->height; y++) { |
101 |
2/2✓ Branch 0 taken 1317888 times.
✓ Branch 1 taken 3744 times.
|
1321632 | for (x = 0; x < avctx->width; x++) { |
102 | int value; | ||
103 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1317888 times.
|
1317888 | if (s->big_endian) { |
104 | ✗ | value = ((AV_RB16(src + 6*x + 4) & 0xFFC0U) >> 4) | |
105 | ✗ | | ((AV_RB16(src + 6*x + 2) & 0xFFC0U) << 6) | |
106 | ✗ | | ((AV_RB16(src + 6*x + 0) & 0xFFC0U) << 16); | |
107 | } else { | ||
108 | 1317888 | value = ((AV_RL16(src + 6*x + 4) & 0xFFC0U) >> 4) | |
109 | 1317888 | | ((AV_RL16(src + 6*x + 2) & 0xFFC0U) << 6) | |
110 | 1317888 | | ((AV_RL16(src + 6*x + 0) & 0xFFC0U) << 16); | |
111 | } | ||
112 | 1317888 | write32(dst, value); | |
113 | 1317888 | dst += 4; | |
114 | } | ||
115 | 3744 | src += pic->linesize[0]; | |
116 | } | ||
117 | 13 | } | |
118 | |||
119 | 13 | static void encode_gbrp10(AVCodecContext *avctx, const AVFrame *pic, uint8_t *dst) | |
120 | { | ||
121 | 13 | DPXContext *s = avctx->priv_data; | |
122 | 13 | const uint8_t *src[3] = {pic->data[0], pic->data[1], pic->data[2]}; | |
123 | int x, y, i; | ||
124 | |||
125 |
2/2✓ Branch 0 taken 3744 times.
✓ Branch 1 taken 13 times.
|
3757 | for (y = 0; y < avctx->height; y++) { |
126 |
2/2✓ Branch 0 taken 1317888 times.
✓ Branch 1 taken 3744 times.
|
1321632 | for (x = 0; x < avctx->width; x++) { |
127 | int value; | ||
128 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1317888 times.
|
1317888 | if (s->big_endian) { |
129 | ✗ | value = (AV_RB16(src[0] + 2*x) << 12) | |
130 | ✗ | | (AV_RB16(src[1] + 2*x) << 2) | |
131 | ✗ | | ((unsigned)AV_RB16(src[2] + 2*x) << 22); | |
132 | } else { | ||
133 | 1317888 | value = (AV_RL16(src[0] + 2*x) << 12) | |
134 | 1317888 | | (AV_RL16(src[1] + 2*x) << 2) | |
135 | 1317888 | | ((unsigned)AV_RL16(src[2] + 2*x) << 22); | |
136 | } | ||
137 | 1317888 | write32(dst, value); | |
138 | 1317888 | dst += 4; | |
139 | } | ||
140 |
2/2✓ Branch 0 taken 11232 times.
✓ Branch 1 taken 3744 times.
|
14976 | for (i = 0; i < 3; i++) |
141 | 11232 | src[i] += pic->linesize[i]; | |
142 | } | ||
143 | 13 | } | |
144 | |||
145 | 13 | static void encode_gbrp12(AVCodecContext *avctx, const AVFrame *pic, uint8_t *dst) | |
146 | { | ||
147 | 13 | DPXContext *s = avctx->priv_data; | |
148 | 13 | const uint16_t *src[3] = {(uint16_t*)pic->data[0], | |
149 | 13 | (uint16_t*)pic->data[1], | |
150 | 13 | (uint16_t*)pic->data[2]}; | |
151 | int x, y, i, pad; | ||
152 | 13 | pad = avctx->width*6; | |
153 | 13 | pad = (FFALIGN(pad, 4) - pad) >> 1; | |
154 |
2/2✓ Branch 0 taken 3744 times.
✓ Branch 1 taken 13 times.
|
3757 | for (y = 0; y < avctx->height; y++) { |
155 |
2/2✓ Branch 0 taken 1317888 times.
✓ Branch 1 taken 3744 times.
|
1321632 | for (x = 0; x < avctx->width; x++) { |
156 | uint16_t value[3]; | ||
157 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1317888 times.
|
1317888 | if (s->big_endian) { |
158 | ✗ | value[1] = AV_RB16(src[0] + x) << 4; | |
159 | ✗ | value[2] = AV_RB16(src[1] + x) << 4; | |
160 | ✗ | value[0] = AV_RB16(src[2] + x) << 4; | |
161 | } else { | ||
162 | 1317888 | value[1] = AV_RL16(src[0] + x) << 4; | |
163 | 1317888 | value[2] = AV_RL16(src[1] + x) << 4; | |
164 | 1317888 | value[0] = AV_RL16(src[2] + x) << 4; | |
165 | } | ||
166 |
2/2✓ Branch 0 taken 3953664 times.
✓ Branch 1 taken 1317888 times.
|
5271552 | for (i = 0; i < 3; i++, dst += 2) |
167 | 3953664 | write16(dst, value[i]); | |
168 | } | ||
169 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3744 times.
|
3744 | for (i = 0; i < pad; i++, dst += 2) |
170 | ✗ | AV_WN16(dst, 0); | |
171 |
2/2✓ Branch 0 taken 11232 times.
✓ Branch 1 taken 3744 times.
|
14976 | for (i = 0; i < 3; i++) |
172 | 11232 | src[i] += pic->linesize[i]/2; | |
173 | } | ||
174 | 13 | } | |
175 | |||
176 | 78 | static int encode_frame(AVCodecContext *avctx, AVPacket *pkt, | |
177 | const AVFrame *frame, int *got_packet) | ||
178 | { | ||
179 | 78 | DPXContext *s = avctx->priv_data; | |
180 | int size, ret, need_align, len; | ||
181 | uint8_t *buf; | ||
182 | int color_trc, color_spec; | ||
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 | |||
223 |
1/6✗ Branch 0 not taken.
✗ Branch 1 not taken.
✓ Branch 2 taken 78 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
78 | switch (avctx->color_trc) { |
224 | ✗ | case AVCOL_TRC_BT709: | |
225 | ✗ | color_trc = DPX_TRC_ITU_R_709_4; | |
226 | ✗ | break; | |
227 | ✗ | default: | |
228 | ✗ | av_log(avctx, AV_LOG_WARNING, "unsupported color transfer\n"); | |
229 | 78 | case AVCOL_TRC_UNSPECIFIED: | |
230 | 78 | color_trc = DPX_TRC_UNSPECIFIED_VIDEO; | |
231 | 78 | break; | |
232 | ✗ | case AVCOL_TRC_GAMMA28: | |
233 | ✗ | color_trc = DPX_TRC_ITU_R_624_4_PAL; | |
234 | ✗ | break; | |
235 | ✗ | case AVCOL_TRC_SMPTE170M: | |
236 | ✗ | color_trc = DPX_TRC_SMPTE_170; | |
237 | ✗ | break; | |
238 | ✗ | case AVCOL_TRC_LINEAR: | |
239 | ✗ | color_trc = DPX_TRC_LINEAR; | |
240 | ✗ | break; | |
241 | } | ||
242 | 78 | buf[801] = color_trc; | |
243 | |||
244 |
1/5✗ Branch 0 not taken.
✗ Branch 1 not taken.
✓ Branch 2 taken 78 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
78 | switch (avctx->color_primaries) { |
245 | ✗ | case AVCOL_PRI_BT709: | |
246 | ✗ | color_spec = DPX_COL_SPEC_ITU_R_709_4; | |
247 | ✗ | break; | |
248 | ✗ | default: | |
249 | ✗ | av_log(avctx, AV_LOG_WARNING, "unsupported colorimetric specification\n"); | |
250 | 78 | case AVCOL_PRI_UNSPECIFIED: | |
251 | 78 | color_spec = DPX_COL_SPEC_UNSPECIFIED_VIDEO; | |
252 | 78 | break; | |
253 | ✗ | case AVCOL_PRI_BT470BG: | |
254 | ✗ | color_spec = DPX_COL_SPEC_ITU_R_624_4_PAL; | |
255 | ✗ | break; | |
256 | ✗ | case AVCOL_PRI_SMPTE170M: | |
257 | ✗ | color_spec = DPX_COL_SPEC_SMPTE_170; | |
258 | ✗ | break; | |
259 | } | ||
260 | 78 | buf[802] = color_spec; | |
261 | |||
262 | 78 | buf[803] = s->bits_per_component; | |
263 |
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) ? |
264 | 1 : 0); /* packing method */ | ||
265 | 78 | write32(buf + 808, HEADER_SIZE); /* data offset */ | |
266 | |||
267 | /* Image source information header */ | ||
268 | 78 | write32(buf + 1628, avctx->sample_aspect_ratio.num); | |
269 | 78 | write32(buf + 1632, avctx->sample_aspect_ratio.den); | |
270 | |||
271 |
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) { |
272 | 39 | case 8: | |
273 | case 16: | ||
274 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 39 times.
|
39 | if (need_align) { |
275 | int j; | ||
276 | ✗ | const uint8_t *src = frame->data[0]; | |
277 | ✗ | uint8_t *dst = pkt->data + HEADER_SIZE; | |
278 | ✗ | size = (len + need_align) * avctx->height; | |
279 | ✗ | for (j=0; j<avctx->height; j++) { | |
280 | ✗ | memcpy(dst, src, len); | |
281 | ✗ | memset(dst + len, 0, need_align); | |
282 | ✗ | dst += len + need_align; | |
283 | ✗ | src += frame->linesize[0]; | |
284 | } | ||
285 | } else { | ||
286 | 39 | size = av_image_copy_to_buffer(buf + HEADER_SIZE, pkt->size - HEADER_SIZE, | |
287 | 39 | (const uint8_t**)frame->data, frame->linesize, | |
288 | avctx->pix_fmt, | ||
289 | avctx->width, avctx->height, 1); | ||
290 | } | ||
291 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 39 times.
|
39 | if (size < 0) |
292 | ✗ | return size; | |
293 | 39 | break; | |
294 | 26 | case 10: | |
295 |
2/2✓ Branch 0 taken 13 times.
✓ Branch 1 taken 13 times.
|
26 | if (s->planar) |
296 | 13 | encode_gbrp10(avctx, frame, buf + HEADER_SIZE); | |
297 | else | ||
298 | 13 | encode_rgb48_10bit(avctx, frame, buf + HEADER_SIZE); | |
299 | 26 | break; | |
300 | 13 | case 12: | |
301 | 13 | encode_gbrp12(avctx, frame, buf + HEADER_SIZE); | |
302 | 13 | break; | |
303 | ✗ | default: | |
304 | ✗ | av_log(avctx, AV_LOG_ERROR, "Unsupported bit depth: %d\n", s->bits_per_component); | |
305 | ✗ | return -1; | |
306 | } | ||
307 | |||
308 | 78 | size += HEADER_SIZE; | |
309 | |||
310 | 78 | write32(buf + 16, size); /* file size */ | |
311 | |||
312 | 78 | *got_packet = 1; | |
313 | |||
314 | 78 | return 0; | |
315 | } | ||
316 | |||
317 | const FFCodec ff_dpx_encoder = { | ||
318 | .p.name = "dpx", | ||
319 | CODEC_LONG_NAME("DPX (Digital Picture Exchange) image"), | ||
320 | .p.type = AVMEDIA_TYPE_VIDEO, | ||
321 | .p.id = AV_CODEC_ID_DPX, | ||
322 | .p.capabilities = AV_CODEC_CAP_DR1, | ||
323 | .priv_data_size = sizeof(DPXContext), | ||
324 | .init = encode_init, | ||
325 | FF_CODEC_ENCODE_CB(encode_frame), | ||
326 | CODEC_PIXFMTS(AV_PIX_FMT_GRAY8, | ||
327 | AV_PIX_FMT_RGB24, AV_PIX_FMT_RGBA, AV_PIX_FMT_ABGR, | ||
328 | AV_PIX_FMT_GRAY16LE, AV_PIX_FMT_GRAY16BE, | ||
329 | AV_PIX_FMT_RGB48LE, AV_PIX_FMT_RGB48BE, | ||
330 | AV_PIX_FMT_RGBA64LE, AV_PIX_FMT_RGBA64BE, | ||
331 | AV_PIX_FMT_GBRP10LE, AV_PIX_FMT_GBRP10BE, | ||
332 | AV_PIX_FMT_GBRP12LE, AV_PIX_FMT_GBRP12BE), | ||
333 | }; | ||
334 |