2 * DPX (.dpx) image encoder
3 * Copyright (c) 2011 Peter Ross <pross@xvid.org>
5 * This file is part of FFmpeg.
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.
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.
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
22 #include "libavutil/common.h"
23 #include "libavutil/intreadwrite.h"
24 #include "libavutil/imgutils.h"
28 typedef struct DPXContext {
31 int bits_per_component;
36 static av_cold int encode_init(AVCodecContext *avctx)
38 DPXContext *s = avctx->priv_data;
40 avctx->coded_frame = &s->picture;
41 avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
42 avctx->coded_frame->key_frame = 1;
45 s->bits_per_component = 8;
46 s->descriptor = 50; /* RGB */
49 switch (avctx->pix_fmt) {
50 case AV_PIX_FMT_RGB24:
53 s->descriptor = 51; /* RGBA */
55 case AV_PIX_FMT_RGB48LE:
57 case AV_PIX_FMT_RGB48BE:
58 s->bits_per_component = avctx->bits_per_raw_sample ? avctx->bits_per_raw_sample : 16;
60 case AV_PIX_FMT_RGBA64LE:
62 case AV_PIX_FMT_RGBA64BE:
64 s->bits_per_component = 16;
66 case AV_PIX_FMT_GBRP10LE:
68 case AV_PIX_FMT_GBRP10BE:
69 s->bits_per_component = 10;
72 case AV_PIX_FMT_GBRP12LE:
74 case AV_PIX_FMT_GBRP12BE:
75 s->bits_per_component = 12;
79 av_log(avctx, AV_LOG_INFO, "unsupported pixel format\n");
86 #define write16(p, value) \
88 if (s->big_endian) AV_WB16(p, value); \
89 else AV_WL16(p, value); \
92 #define write32(p, value) \
94 if (s->big_endian) AV_WB32(p, value); \
95 else AV_WL32(p, value); \
98 static void encode_rgb48_10bit(AVCodecContext *avctx, const AVPicture *pic, uint8_t *dst)
100 DPXContext *s = avctx->priv_data;
101 const uint8_t *src = pic->data[0];
104 for (y = 0; y < avctx->height; y++) {
105 for (x = 0; x < avctx->width; x++) {
108 value = ((AV_RB16(src + 6*x + 4) & 0xFFC0U) >> 4)
109 | ((AV_RB16(src + 6*x + 2) & 0xFFC0U) << 6)
110 | ((AV_RB16(src + 6*x + 0) & 0xFFC0U) << 16);
112 value = ((AV_RL16(src + 6*x + 4) & 0xFFC0U) >> 4)
113 | ((AV_RL16(src + 6*x + 2) & 0xFFC0U) << 6)
114 | ((AV_RL16(src + 6*x + 0) & 0xFFC0U) << 16);
119 src += pic->linesize[0];
123 static void encode_gbrp10(AVCodecContext *avctx, const AVPicture *pic, uint8_t *dst)
125 DPXContext *s = avctx->priv_data;
126 const uint8_t *src[3] = {pic->data[0], pic->data[1], pic->data[2]};
129 for (y = 0; y < avctx->height; y++) {
130 for (x = 0; x < avctx->width; x++) {
133 value = (AV_RB16(src[0] + 2*x) << 12)
134 | (AV_RB16(src[1] + 2*x) << 2)
135 | (AV_RB16(src[2] + 2*x) << 22);
137 value = (AV_RL16(src[0] + 2*x) << 12)
138 | (AV_RL16(src[1] + 2*x) << 2)
139 | (AV_RL16(src[2] + 2*x) << 22);
144 for (i = 0; i < 3; i++)
145 src[i] += pic->linesize[i];
149 static void encode_gbrp12(AVCodecContext *avctx, const AVPicture *pic, uint16_t *dst)
151 DPXContext *s = avctx->priv_data;
152 const uint16_t *src[3] = {(uint16_t*)pic->data[0],
153 (uint16_t*)pic->data[1],
154 (uint16_t*)pic->data[2]};
156 for (y = 0; y < avctx->height; y++) {
157 for (x = 0; x < avctx->width; x++) {
160 value[1] = AV_RB16(src[0] + x) << 4;
161 value[2] = AV_RB16(src[1] + x) << 4;
162 value[0] = AV_RB16(src[2] + x) << 4;
164 value[1] = AV_RL16(src[0] + x) << 4;
165 value[2] = AV_RL16(src[1] + x) << 4;
166 value[0] = AV_RL16(src[2] + x) << 4;
168 for (i = 0; i < 3; i++)
169 write16(dst++, value[i]);
171 for (i = 0; i < 3; i++)
172 src[i] += pic->linesize[i]/2;
176 static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
177 const AVFrame *frame, int *got_packet)
179 DPXContext *s = avctx->priv_data;
183 #define HEADER_SIZE 1664 /* DPX Generic header */
184 if (s->bits_per_component == 10)
185 size = avctx->height * avctx->width * 4;
187 size = avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height);
188 if ((ret = ff_alloc_packet2(avctx, pkt, size + HEADER_SIZE)) < 0)
192 memset(buf, 0, HEADER_SIZE);
194 /* File information header */
195 write32(buf, MKBETAG('S','D','P','X'));
196 write32(buf + 4, HEADER_SIZE);
197 memcpy (buf + 8, "V1.0", 4);
198 write32(buf + 20, 1); /* new image */
199 write32(buf + 24, HEADER_SIZE);
200 if (!(avctx->flags & CODEC_FLAG_BITEXACT))
201 memcpy (buf + 160, LIBAVCODEC_IDENT, FFMIN(sizeof(LIBAVCODEC_IDENT), 100));
202 write32(buf + 660, 0xFFFFFFFF); /* unencrypted */
204 /* Image information header */
205 write16(buf + 768, 0); /* orientation; left to right, top to bottom */
206 write16(buf + 770, 1); /* number of elements */
207 write32(buf + 772, avctx->width);
208 write32(buf + 776, avctx->height);
209 buf[800] = s->descriptor;
210 buf[801] = 2; /* linear transfer */
211 buf[802] = 2; /* linear colorimetric */
212 buf[803] = s->bits_per_component;
213 write16(buf + 804, (s->bits_per_component == 10 || s->bits_per_component == 12) ?
214 1 : 0); /* packing method */
215 write32(buf + 808, HEADER_SIZE); /* data offset */
217 /* Image source information header */
218 write32(buf + 1628, avctx->sample_aspect_ratio.num);
219 write32(buf + 1632, avctx->sample_aspect_ratio.den);
221 switch(s->bits_per_component) {
224 size = avpicture_layout((const AVPicture*)frame, avctx->pix_fmt,
225 avctx->width, avctx->height,
226 buf + HEADER_SIZE, pkt->size - HEADER_SIZE);
232 encode_gbrp10(avctx, (const AVPicture*)frame, buf + HEADER_SIZE);
234 encode_rgb48_10bit(avctx, (const AVPicture*)frame, buf + HEADER_SIZE);
237 encode_gbrp12(avctx, (const AVPicture*)frame, (uint16_t*)(buf + HEADER_SIZE));
240 av_log(avctx, AV_LOG_ERROR, "Unsupported bit depth: %d\n", s->bits_per_component);
246 write32(buf + 16, size); /* file size */
248 pkt->flags |= AV_PKT_FLAG_KEY;
254 AVCodec ff_dpx_encoder = {
256 .type = AVMEDIA_TYPE_VIDEO,
257 .id = AV_CODEC_ID_DPX,
258 .priv_data_size = sizeof(DPXContext),
260 .encode2 = encode_frame,
261 .pix_fmts = (const enum AVPixelFormat[]){
273 .long_name = NULL_IF_CONFIG_SMALL("DPX image"),