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 {
30 int bits_per_component;
35 static av_cold int encode_init(AVCodecContext *avctx)
37 DPXContext *s = avctx->priv_data;
38 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
40 s->big_endian = !!(desc->flags & AV_PIX_FMT_FLAG_BE);
41 s->bits_per_component = desc->comp[0].depth_minus1 + 1;
42 s->descriptor = (desc->flags & AV_PIX_FMT_FLAG_ALPHA) ? 51 : 50;
43 s->planar = !!(desc->flags & AV_PIX_FMT_FLAG_PLANAR);
45 switch (avctx->pix_fmt) {
46 case AV_PIX_FMT_GBRP10BE:
47 case AV_PIX_FMT_GBRP10LE:
48 case AV_PIX_FMT_GBRP12BE:
49 case AV_PIX_FMT_GBRP12LE:
50 case AV_PIX_FMT_RGB24:
51 case AV_PIX_FMT_RGBA64BE:
52 case AV_PIX_FMT_RGBA64LE:
55 case AV_PIX_FMT_RGB48LE:
56 case AV_PIX_FMT_RGB48BE:
57 if (avctx->bits_per_raw_sample)
58 s->bits_per_component = avctx->bits_per_raw_sample;
61 av_log(avctx, AV_LOG_INFO, "unsupported pixel format\n");
68 #define write16(p, value) \
70 if (s->big_endian) AV_WB16(p, value); \
71 else AV_WL16(p, value); \
74 #define write32(p, value) \
76 if (s->big_endian) AV_WB32(p, value); \
77 else AV_WL32(p, value); \
80 static void encode_rgb48_10bit(AVCodecContext *avctx, const AVPicture *pic, uint8_t *dst)
82 DPXContext *s = avctx->priv_data;
83 const uint8_t *src = pic->data[0];
86 for (y = 0; y < avctx->height; y++) {
87 for (x = 0; x < avctx->width; x++) {
90 value = ((AV_RB16(src + 6*x + 4) & 0xFFC0U) >> 4)
91 | ((AV_RB16(src + 6*x + 2) & 0xFFC0U) << 6)
92 | ((AV_RB16(src + 6*x + 0) & 0xFFC0U) << 16);
94 value = ((AV_RL16(src + 6*x + 4) & 0xFFC0U) >> 4)
95 | ((AV_RL16(src + 6*x + 2) & 0xFFC0U) << 6)
96 | ((AV_RL16(src + 6*x + 0) & 0xFFC0U) << 16);
101 src += pic->linesize[0];
105 static void encode_gbrp10(AVCodecContext *avctx, const AVPicture *pic, uint8_t *dst)
107 DPXContext *s = avctx->priv_data;
108 const uint8_t *src[3] = {pic->data[0], pic->data[1], pic->data[2]};
111 for (y = 0; y < avctx->height; y++) {
112 for (x = 0; x < avctx->width; x++) {
115 value = (AV_RB16(src[0] + 2*x) << 12)
116 | (AV_RB16(src[1] + 2*x) << 2)
117 | ((unsigned)AV_RB16(src[2] + 2*x) << 22);
119 value = (AV_RL16(src[0] + 2*x) << 12)
120 | (AV_RL16(src[1] + 2*x) << 2)
121 | ((unsigned)AV_RL16(src[2] + 2*x) << 22);
126 for (i = 0; i < 3; i++)
127 src[i] += pic->linesize[i];
131 static void encode_gbrp12(AVCodecContext *avctx, const AVPicture *pic, uint16_t *dst)
133 DPXContext *s = avctx->priv_data;
134 const uint16_t *src[3] = {(uint16_t*)pic->data[0],
135 (uint16_t*)pic->data[1],
136 (uint16_t*)pic->data[2]};
138 for (y = 0; y < avctx->height; y++) {
139 for (x = 0; x < avctx->width; x++) {
142 value[1] = AV_RB16(src[0] + x) << 4;
143 value[2] = AV_RB16(src[1] + x) << 4;
144 value[0] = AV_RB16(src[2] + x) << 4;
146 value[1] = AV_RL16(src[0] + x) << 4;
147 value[2] = AV_RL16(src[1] + x) << 4;
148 value[0] = AV_RL16(src[2] + x) << 4;
150 for (i = 0; i < 3; i++)
151 write16(dst++, value[i]);
153 for (i = 0; i < 3; i++)
154 src[i] += pic->linesize[i]/2;
158 static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
159 const AVFrame *frame, int *got_packet)
161 DPXContext *s = avctx->priv_data;
165 #define HEADER_SIZE 1664 /* DPX Generic header */
166 if (s->bits_per_component == 10)
167 size = avctx->height * avctx->width * 4;
169 size = avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height);
170 if ((ret = ff_alloc_packet2(avctx, pkt, size + HEADER_SIZE)) < 0)
174 memset(buf, 0, HEADER_SIZE);
176 /* File information header */
177 write32(buf, MKBETAG('S','D','P','X'));
178 write32(buf + 4, HEADER_SIZE);
179 memcpy (buf + 8, "V1.0", 4);
180 write32(buf + 20, 1); /* new image */
181 write32(buf + 24, HEADER_SIZE);
182 if (!(avctx->flags & CODEC_FLAG_BITEXACT))
183 memcpy (buf + 160, LIBAVCODEC_IDENT, FFMIN(sizeof(LIBAVCODEC_IDENT), 100));
184 write32(buf + 660, 0xFFFFFFFF); /* unencrypted */
186 /* Image information header */
187 write16(buf + 768, 0); /* orientation; left to right, top to bottom */
188 write16(buf + 770, 1); /* number of elements */
189 write32(buf + 772, avctx->width);
190 write32(buf + 776, avctx->height);
191 buf[800] = s->descriptor;
192 buf[801] = 2; /* linear transfer */
193 buf[802] = 2; /* linear colorimetric */
194 buf[803] = s->bits_per_component;
195 write16(buf + 804, (s->bits_per_component == 10 || s->bits_per_component == 12) ?
196 1 : 0); /* packing method */
197 write32(buf + 808, HEADER_SIZE); /* data offset */
199 /* Image source information header */
200 write32(buf + 1628, avctx->sample_aspect_ratio.num);
201 write32(buf + 1632, avctx->sample_aspect_ratio.den);
203 switch(s->bits_per_component) {
206 size = avpicture_layout((const AVPicture*)frame, avctx->pix_fmt,
207 avctx->width, avctx->height,
208 buf + HEADER_SIZE, pkt->size - HEADER_SIZE);
214 encode_gbrp10(avctx, (const AVPicture*)frame, buf + HEADER_SIZE);
216 encode_rgb48_10bit(avctx, (const AVPicture*)frame, buf + HEADER_SIZE);
219 encode_gbrp12(avctx, (const AVPicture*)frame, (uint16_t*)(buf + HEADER_SIZE));
222 av_log(avctx, AV_LOG_ERROR, "Unsupported bit depth: %d\n", s->bits_per_component);
228 write32(buf + 16, size); /* file size */
230 pkt->flags |= AV_PKT_FLAG_KEY;
236 AVCodec ff_dpx_encoder = {
238 .type = AVMEDIA_TYPE_VIDEO,
239 .id = AV_CODEC_ID_DPX,
240 .priv_data_size = sizeof(DPXContext),
242 .encode2 = encode_frame,
243 .pix_fmts = (const enum AVPixelFormat[]){
255 .long_name = NULL_IF_CONFIG_SMALL("DPX image"),