2 * DPX (.dpx) image encoder
3 * Copyright (c) 2011 Peter Ross <pross@xvid.org>
5 * This file is part of Libav.
7 * Libav 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 * Libav 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 Libav; 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;
34 static av_cold int encode_init(AVCodecContext *avctx)
36 DPXContext *s = avctx->priv_data;
38 avctx->coded_frame = av_frame_alloc();
39 if (!avctx->coded_frame)
40 return AVERROR(ENOMEM);
42 avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
43 avctx->coded_frame->key_frame = 1;
46 s->bits_per_component = 8;
47 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;
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,
83 DPXContext *s = avctx->priv_data;
84 const uint8_t *src = pic->data[0];
87 for (y = 0; y < avctx->height; y++) {
88 for (x = 0; x < avctx->width; x++) {
90 if ((avctx->pix_fmt & 1)) {
91 value = ((AV_RB16(src + 6*x + 4) & 0xFFC0) >> 4)
92 | ((AV_RB16(src + 6*x + 2) & 0xFFC0) << 6)
93 | ((AV_RB16(src + 6*x + 0) & 0xFFC0) << 16);
95 value = ((AV_RL16(src + 6*x + 4) & 0xFFC0) >> 4)
96 | ((AV_RL16(src + 6*x + 2) & 0xFFC0) << 6)
97 | ((AV_RL16(src + 6*x + 0) & 0xFFC0) << 16);
102 src += pic->linesize[0];
106 static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
107 const AVFrame *frame, int *got_packet)
109 DPXContext *s = avctx->priv_data;
113 #define HEADER_SIZE 1664 /* DPX Generic header */
114 if (s->bits_per_component == 10)
115 size = avctx->height * avctx->width * 4;
117 size = avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height);
118 if ((ret = ff_alloc_packet(pkt, size + HEADER_SIZE)) < 0) {
119 av_log(avctx, AV_LOG_ERROR, "Error getting output packet.\n");
124 memset(buf, 0, HEADER_SIZE);
126 /* File information header */
127 write32(buf, MKBETAG('S','D','P','X'));
128 write32(buf + 4, HEADER_SIZE);
129 memcpy (buf + 8, "V1.0", 4);
130 write32(buf + 20, 1); /* new image */
131 write32(buf + 24, HEADER_SIZE);
132 if (!(avctx->flags & CODEC_FLAG_BITEXACT))
133 memcpy (buf + 160, LIBAVCODEC_IDENT, FFMIN(sizeof(LIBAVCODEC_IDENT), 100));
134 write32(buf + 660, 0xFFFFFFFF); /* unencrypted */
136 /* Image information header */
137 write16(buf + 768, 0); /* orientation; left to right, top to bottom */
138 write16(buf + 770, 1); /* number of elements */
139 write32(buf + 772, avctx->width);
140 write32(buf + 776, avctx->height);
141 buf[800] = s->descriptor;
142 buf[801] = 2; /* linear transfer */
143 buf[802] = 2; /* linear colorimetric */
144 buf[803] = s->bits_per_component;
145 write16(buf + 804, s->bits_per_component == 10 ? 1 : 0); /* packing method */
147 /* Image source information header */
148 write32(buf + 1628, avctx->sample_aspect_ratio.num);
149 write32(buf + 1632, avctx->sample_aspect_ratio.den);
151 switch (s->bits_per_component) {
154 size = avpicture_layout((const AVPicture*)frame, avctx->pix_fmt,
155 avctx->width, avctx->height,
156 buf + HEADER_SIZE, pkt->size - HEADER_SIZE);
161 encode_rgb48_10bit(avctx, (const AVPicture*)frame, buf + HEADER_SIZE);
164 av_log(avctx, AV_LOG_ERROR, "Unsupported bit depth: %d\n", s->bits_per_component);
170 write32(buf + 16, size); /* file size */
172 pkt->flags |= AV_PKT_FLAG_KEY;
178 static av_cold int encode_close(AVCodecContext *avctx)
180 av_frame_free(&avctx->coded_frame);
184 AVCodec ff_dpx_encoder = {
186 .long_name = NULL_IF_CONFIG_SMALL("DPX image"),
187 .type = AVMEDIA_TYPE_VIDEO,
188 .id = AV_CODEC_ID_DPX,
189 .priv_data_size = sizeof(DPXContext),
191 .encode2 = encode_frame,
192 .close = encode_close,
193 .pix_fmts = (const enum AVPixelFormat[]){