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