]> git.sesse.net Git - ffmpeg/blob - libavcodec/dpxenc.c
Merge remote-tracking branch 'qatar/master'
[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/intreadwrite.h"
23 #include "libavutil/imgutils.h"
24 #include "avcodec.h"
25 #include "internal.h"
26
27 typedef struct DPXContext {
28     AVFrame picture;
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 = &s->picture;
39     avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
40     avctx->coded_frame->key_frame = 1;
41
42     s->big_endian         = 1;
43     s->bits_per_component = 8;
44     s->descriptor         = 50; /* RGB */
45
46     switch (avctx->pix_fmt) {
47     case PIX_FMT_RGB24:
48         break;
49     case PIX_FMT_RGBA:
50         s->descriptor = 51; /* RGBA */
51         break;
52     case PIX_FMT_RGB48LE:
53         s->big_endian = 0;
54     case PIX_FMT_RGB48BE:
55         s->bits_per_component = avctx->bits_per_raw_sample ? avctx->bits_per_raw_sample : 16;
56         break;
57     case PIX_FMT_RGBA64LE:
58         s->big_endian = 0;
59     case PIX_FMT_RGBA64BE:
60         s->descriptor = 51;
61         s->bits_per_component = 16;
62         break;
63     default:
64         av_log(avctx, AV_LOG_INFO, "unsupported pixel format\n");
65         return -1;
66     }
67
68     return 0;
69 }
70
71 #define write16(p, value) \
72 do { \
73     if (s->big_endian) AV_WB16(p, value); \
74     else               AV_WL16(p, value); \
75 } while(0)
76
77 #define write32(p, value) \
78 do { \
79     if (s->big_endian) AV_WB32(p, value); \
80     else               AV_WL32(p, value); \
81 } while(0)
82
83 static void encode_rgb48_10bit(AVCodecContext *avctx, const AVPicture *pic, uint8_t *dst)
84 {
85     DPXContext *s = avctx->priv_data;
86     const uint8_t *src = pic->data[0];
87     int x, y;
88
89     for (y = 0; y < avctx->height; y++) {
90         for (x = 0; x < avctx->width; x++) {
91             int value;
92             if ((avctx->pix_fmt & 1)) {
93                 value = ((AV_RB16(src + 6*x + 4) & 0xFFC0) >> 4)
94                       | ((AV_RB16(src + 6*x + 2) & 0xFFC0) << 6)
95                       | ((AV_RB16(src + 6*x + 0) & 0xFFC0) << 16);
96             } else {
97                 value = ((AV_RL16(src + 6*x + 4) & 0xFFC0) >> 4)
98                       | ((AV_RL16(src + 6*x + 2) & 0xFFC0) << 6)
99                       | ((AV_RL16(src + 6*x + 0) & 0xFFC0) << 16);
100             }
101             write32(dst, value);
102             dst += 4;
103         }
104         src += pic->linesize[0];
105     }
106 }
107
108 static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
109                         const AVFrame *frame, int *got_packet)
110 {
111     DPXContext *s = avctx->priv_data;
112     int size, ret;
113     uint8_t *buf;
114
115 #define HEADER_SIZE 1664  /* DPX Generic header */
116     if (s->bits_per_component == 10)
117         size = avctx->height * avctx->width * 4;
118     else
119         size = avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height);
120     if ((ret = ff_alloc_packet2(avctx, pkt, size + HEADER_SIZE)) < 0)
121         return ret;
122     buf = pkt->data;
123
124     memset(buf, 0, HEADER_SIZE);
125
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 */
135
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 */
146
147     /* Image source information header */
148     write32(buf + 1628, avctx->sample_aspect_ratio.num);
149     write32(buf + 1632, avctx->sample_aspect_ratio.den);
150
151     switch(s->bits_per_component) {
152     case 8:
153     case 16:
154         size = avpicture_layout((const AVPicture*)frame, avctx->pix_fmt,
155                                 avctx->width, avctx->height,
156                                 buf + HEADER_SIZE, pkt->size - HEADER_SIZE);
157         if (size < 0)
158             return size;
159         break;
160     case 10:
161         encode_rgb48_10bit(avctx, (const AVPicture*)frame, buf + HEADER_SIZE);
162         break;
163     default:
164         av_log(avctx, AV_LOG_ERROR, "Unsupported bit depth: %d\n", s->bits_per_component);
165         return -1;
166     }
167
168     size += HEADER_SIZE;
169
170     write32(buf + 16, size); /* file size */
171
172     pkt->flags |= AV_PKT_FLAG_KEY;
173     *got_packet = 1;
174
175     return 0;
176 }
177
178 AVCodec ff_dpx_encoder = {
179     .name = "dpx",
180     .type = AVMEDIA_TYPE_VIDEO,
181     .id   = CODEC_ID_DPX,
182     .priv_data_size = sizeof(DPXContext),
183     .init   = encode_init,
184     .encode2 = encode_frame,
185     .pix_fmts = (const enum PixelFormat[]){
186         PIX_FMT_RGB24,
187         PIX_FMT_RGBA,
188         PIX_FMT_RGB48LE,
189         PIX_FMT_RGB48BE,
190         PIX_FMT_RGBA64LE,
191         PIX_FMT_RGBA64BE,
192         PIX_FMT_NONE},
193     .long_name = NULL_IF_CONFIG_SMALL("DPX image"),
194 };