]> git.sesse.net Git - ffmpeg/blob - libavcodec/dpxenc.c
aaa5d93f47d4851a2d35ffcd6c8f34bec3b77e65
[ffmpeg] / libavcodec / dpxenc.c
1 /*
2  * DPX (.dpx) image encoder
3  * Copyright (c) 2011 Peter Ross <pross@xvid.org>
4  *
5  * This file is part of Libav.
6  *
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.
11  *
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.
16  *
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
20  */
21
22 #include "libavutil/common.h"
23 #include "libavutil/intreadwrite.h"
24 #include "libavutil/imgutils.h"
25 #include "avcodec.h"
26 #include "internal.h"
27
28 typedef struct DPXContext {
29     int big_endian;
30     int bits_per_component;
31     int descriptor;
32 } DPXContext;
33
34 static av_cold int encode_init(AVCodecContext *avctx)
35 {
36     DPXContext *s = avctx->priv_data;
37
38     avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
39     avctx->coded_frame->key_frame = 1;
40
41     s->big_endian         = 1;
42     s->bits_per_component = 8;
43     s->descriptor         = 50; /* RGB */
44
45     switch (avctx->pix_fmt) {
46     case AV_PIX_FMT_RGB24:
47         break;
48     case AV_PIX_FMT_RGBA:
49         s->descriptor = 51; /* RGBA */
50         break;
51     case AV_PIX_FMT_RGB48LE:
52         s->big_endian = 0;
53         /* fall-through */
54     case AV_PIX_FMT_RGB48BE:
55         s->bits_per_component = avctx->bits_per_raw_sample ? avctx->bits_per_raw_sample : 16;
56         break;
57     default:
58         av_log(avctx, AV_LOG_INFO, "unsupported pixel format\n");
59         return -1;
60     }
61
62     return 0;
63 }
64
65 #define write16(p, value) \
66 do { \
67     if (s->big_endian) AV_WB16(p, value); \
68     else               AV_WL16(p, value); \
69 } while(0)
70
71 #define write32(p, value) \
72 do { \
73     if (s->big_endian) AV_WB32(p, value); \
74     else               AV_WL32(p, value); \
75 } while(0)
76
77 static void encode_rgb48_10bit(AVCodecContext *avctx, const AVPicture *pic,
78                                uint8_t *dst)
79 {
80     DPXContext *s = avctx->priv_data;
81     const uint8_t *src = pic->data[0];
82     int x, y;
83
84     for (y = 0; y < avctx->height; y++) {
85         for (x = 0; x < avctx->width; x++) {
86             int value;
87             if ((avctx->pix_fmt & 1)) {
88                 value = ((AV_RB16(src + 6*x + 4) & 0xFFC0) >> 4)
89                       | ((AV_RB16(src + 6*x + 2) & 0xFFC0) << 6)
90                       | ((AV_RB16(src + 6*x + 0) & 0xFFC0) << 16);
91             } else {
92                 value = ((AV_RL16(src + 6*x + 4) & 0xFFC0) >> 4)
93                       | ((AV_RL16(src + 6*x + 2) & 0xFFC0) << 6)
94                       | ((AV_RL16(src + 6*x + 0) & 0xFFC0) << 16);
95             }
96             write32(dst, value);
97             dst += 4;
98         }
99         src += pic->linesize[0];
100     }
101 }
102
103 static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
104                         const AVFrame *frame, int *got_packet)
105 {
106     DPXContext *s = avctx->priv_data;
107     int size, ret;
108     uint8_t *buf;
109
110 #define HEADER_SIZE 1664  /* DPX Generic header */
111     if (s->bits_per_component == 10)
112         size = avctx->height * avctx->width * 4;
113     else
114         size = avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height);
115     if ((ret = ff_alloc_packet(pkt, size + HEADER_SIZE)) < 0) {
116         av_log(avctx, AV_LOG_ERROR, "Error getting output packet.\n");
117         return ret;
118     }
119     buf = pkt->data;
120
121     memset(buf, 0, HEADER_SIZE);
122
123     /* File information header */
124     write32(buf,       MKBETAG('S','D','P','X'));
125     write32(buf +   4, HEADER_SIZE);
126     memcpy (buf +   8, "V1.0", 4);
127     write32(buf +  20, 1); /* new image */
128     write32(buf +  24, HEADER_SIZE);
129     if (!(avctx->flags & CODEC_FLAG_BITEXACT))
130         memcpy (buf + 160, LIBAVCODEC_IDENT, FFMIN(sizeof(LIBAVCODEC_IDENT), 100));
131     write32(buf + 660, 0xFFFFFFFF); /* unencrypted */
132
133     /* Image information header */
134     write16(buf + 768, 0); /* orientation; left to right, top to bottom */
135     write16(buf + 770, 1); /* number of elements */
136     write32(buf + 772, avctx->width);
137     write32(buf + 776, avctx->height);
138     buf[800] = s->descriptor;
139     buf[801] = 2; /* linear transfer */
140     buf[802] = 2; /* linear colorimetric */
141     buf[803] = s->bits_per_component;
142     write16(buf + 804, s->bits_per_component == 10 ? 1 : 0); /* packing method */
143     write32(buf + 808, HEADER_SIZE); /* data offset */
144
145     /* Image source information header */
146     write32(buf + 1628, avctx->sample_aspect_ratio.num);
147     write32(buf + 1632, avctx->sample_aspect_ratio.den);
148
149     switch (s->bits_per_component) {
150     case 8:
151     case 16:
152         size = avpicture_layout((const AVPicture*)frame, avctx->pix_fmt,
153                                 avctx->width, avctx->height,
154                                 buf + HEADER_SIZE, pkt->size - HEADER_SIZE);
155         if (size < 0)
156             return size;
157         break;
158     case 10:
159         encode_rgb48_10bit(avctx, (const AVPicture*)frame, buf + HEADER_SIZE);
160         break;
161     default:
162         av_log(avctx, AV_LOG_ERROR, "Unsupported bit depth: %d\n", s->bits_per_component);
163         return -1;
164     }
165
166     size += HEADER_SIZE;
167
168     write32(buf + 16, size); /* file size */
169
170     pkt->flags |= AV_PKT_FLAG_KEY;
171     *got_packet = 1;
172
173     return 0;
174 }
175
176 AVCodec ff_dpx_encoder = {
177     .name = "dpx",
178     .long_name = NULL_IF_CONFIG_SMALL("DPX image"),
179     .type = AVMEDIA_TYPE_VIDEO,
180     .id   = AV_CODEC_ID_DPX,
181     .priv_data_size = sizeof(DPXContext),
182     .init   = encode_init,
183     .encode2 = encode_frame,
184     .pix_fmts = (const enum AVPixelFormat[]){
185         AV_PIX_FMT_RGB24,
186         AV_PIX_FMT_RGBA,
187         AV_PIX_FMT_RGB48LE,
188         AV_PIX_FMT_RGB48BE,
189         AV_PIX_FMT_NONE},
190 };