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/intreadwrite.h"
23 #include "libavutil/imgutils.h"
26 typedef struct DPXContext {
29 int bits_per_component;
33 static av_cold int encode_init(AVCodecContext *avctx)
35 DPXContext *s = avctx->priv_data;
37 avctx->coded_frame = &s->picture;
38 avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
39 avctx->coded_frame->key_frame = 1;
42 s->bits_per_component = 8;
43 s->descriptor = 50; /* RGB */
45 switch (avctx->pix_fmt) {
49 s->descriptor = 51; /* RGBA */
54 s->bits_per_component = avctx->bits_per_raw_sample ? avctx->bits_per_raw_sample : 16;
57 av_log(avctx, AV_LOG_INFO, "unsupported pixel format\n");
64 #define write16(p, value) \
66 if (s->big_endian) AV_WB16(p, value); \
67 else AV_WL16(p, value); \
70 #define write32(p, value) \
72 if (s->big_endian) AV_WB32(p, value); \
73 else AV_WL32(p, value); \
76 static void encode_rgb48_10bit(AVCodecContext *avctx, const AVPicture *pic, uint8_t *dst)
78 DPXContext *s = avctx->priv_data;
79 const uint8_t *src = pic->data[0];
82 for (y = 0; y < avctx->height; y++) {
83 for (x = 0; x < avctx->width; x++) {
85 if ((avctx->pix_fmt & 1)) {
86 value = ((AV_RB16(src + 6*x + 4) & 0xFFC0) >> 4)
87 | ((AV_RB16(src + 6*x + 2) & 0xFFC0) << 6)
88 | ((AV_RB16(src + 6*x + 0) & 0xFFC0) << 16);
90 value = ((AV_RL16(src + 6*x + 4) & 0xFFC0) >> 4)
91 | ((AV_RL16(src + 6*x + 2) & 0xFFC0) << 6)
92 | ((AV_RL16(src + 6*x + 0) & 0xFFC0) << 16);
97 src += pic->linesize[0];
101 static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data)
103 DPXContext *s = avctx->priv_data;
106 #define HEADER_SIZE 1664 /* DPX Generic header */
107 if (buf_size < HEADER_SIZE)
110 memset(buf, 0, HEADER_SIZE);
112 /* File information header */
113 write32(buf, MKBETAG('S','D','P','X'));
114 write32(buf + 4, HEADER_SIZE);
115 memcpy (buf + 8, "V1.0", 4);
116 write32(buf + 20, 1); /* new image */
117 write32(buf + 24, HEADER_SIZE);
118 memcpy (buf + 160, LIBAVCODEC_IDENT, FFMIN(sizeof(LIBAVCODEC_IDENT), 100));
119 write32(buf + 660, 0xFFFFFFFF); /* unencrypted */
121 /* Image information header */
122 write16(buf + 768, 0); /* orientation; left to right, top to bottom */
123 write16(buf + 770, 1); /* number of elements */
124 write32(buf + 772, avctx->width);
125 write32(buf + 776, avctx->height);
126 buf[800] = s->descriptor;
127 buf[801] = 2; /* linear transfer */
128 buf[802] = 2; /* linear colorimetric */
129 buf[803] = s->bits_per_component;
130 write16(buf + 804, s->bits_per_component == 10 ? 1 : 0); /* packing method */
132 /* Image source information header */
133 write32(buf + 1628, avctx->sample_aspect_ratio.num);
134 write32(buf + 1632, avctx->sample_aspect_ratio.den);
136 switch(s->bits_per_component) {
139 size = avpicture_layout(data, avctx->pix_fmt,
140 avctx->width, avctx->height,
141 buf + HEADER_SIZE, buf_size - HEADER_SIZE);
146 size = avctx->height * avctx->width * 4;
147 if (buf_size < HEADER_SIZE + size)
149 encode_rgb48_10bit(avctx, data, buf + HEADER_SIZE);
152 av_log(avctx, AV_LOG_ERROR, "Unsupported bit depth: %d\n", s->bits_per_component);
158 write32(buf + 16, size); /* file size */
162 AVCodec ff_dpx_encoder = {
164 .type = AVMEDIA_TYPE_VIDEO,
166 .priv_data_size = sizeof(DPXContext),
168 .encode = encode_frame,
169 .pix_fmts = (const enum PixelFormat[]){
175 .long_name = NULL_IF_CONFIG_SMALL("DPX image"),