55f71c987c62c05dd6194158051ab404b5ecedfb
[libav.git] / libavcodec / dpx.c
1 /*
2 * DPX (.dpx) image decoder
3 * Copyright (c) 2009 Jimmy Christensen
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/intreadwrite.h"
23 #include "libavutil/imgutils.h"
24 #include "bytestream.h"
25 #include "avcodec.h"
26
27 typedef struct DPXContext {
28 AVFrame picture;
29 } DPXContext;
30
31
32 static unsigned int read32(const uint8_t **ptr, int is_big)
33 {
34 unsigned int temp;
35 if (is_big) {
36 temp = AV_RB32(*ptr);
37 } else {
38 temp = AV_RL32(*ptr);
39 }
40 *ptr += 4;
41 return temp;
42 }
43
44 static inline unsigned make_16bit(unsigned value)
45 {
46 // mask away invalid bits
47 value &= 0xFFC0;
48 // correctly expand to 16 bits
49 return value + (value >> 10);
50 }
51
52 static int decode_frame(AVCodecContext *avctx,
53 void *data,
54 int *data_size,
55 AVPacket *avpkt)
56 {
57 const uint8_t *buf = avpkt->data;
58 const uint8_t *buf_end = avpkt->data + avpkt->size;
59 int buf_size = avpkt->size;
60 DPXContext *const s = avctx->priv_data;
61 AVFrame *picture = data;
62 AVFrame *const p = &s->picture;
63 uint8_t *ptr;
64
65 unsigned int offset;
66 int magic_num, endian;
67 int x, y;
68 int w, h, stride, bits_per_color, descriptor, elements, target_packet_size, source_packet_size;
69
70 unsigned int rgbBuffer;
71
72 if (avpkt->size <= 1634) {
73 av_log(avctx, AV_LOG_ERROR, "Packet too small for DPX header\n");
74 return AVERROR_INVALIDDATA;
75 }
76
77 magic_num = AV_RB32(buf);
78 buf += 4;
79
80 /* Check if the files "magic number" is "SDPX" which means it uses
81 * big-endian or XPDS which is for little-endian files */
82 if (magic_num == AV_RL32("SDPX")) {
83 endian = 0;
84 } else if (magic_num == AV_RB32("SDPX")) {
85 endian = 1;
86 } else {
87 av_log(avctx, AV_LOG_ERROR, "DPX marker not found\n");
88 return -1;
89 }
90
91 offset = read32(&buf, endian);
92 if (avpkt->size <= offset) {
93 av_log(avctx, AV_LOG_ERROR, "Invalid data start offset\n");
94 return AVERROR_INVALIDDATA;
95 }
96 // Need to end in 0x304 offset from start of file
97 buf = avpkt->data + 0x304;
98 w = read32(&buf, endian);
99 h = read32(&buf, endian);
100
101 // Need to end in 0x320 to read the descriptor
102 buf += 20;
103 descriptor = buf[0];
104
105 // Need to end in 0x323 to read the bits per color
106 buf += 3;
107 avctx->bits_per_raw_sample =
108 bits_per_color = buf[0];
109
110 buf += 825;
111 avctx->sample_aspect_ratio.num = read32(&buf, endian);
112 avctx->sample_aspect_ratio.den = read32(&buf, endian);
113
114 switch (descriptor) {
115 case 51: // RGBA
116 elements = 4;
117 break;
118 case 50: // RGB
119 elements = 3;
120 break;
121 default:
122 av_log(avctx, AV_LOG_ERROR, "Unsupported descriptor %d\n", descriptor);
123 return -1;
124 }
125
126 switch (bits_per_color) {
127 case 8:
128 if (elements == 4) {
129 avctx->pix_fmt = AV_PIX_FMT_RGBA;
130 } else {
131 avctx->pix_fmt = AV_PIX_FMT_RGB24;
132 }
133 source_packet_size = elements;
134 target_packet_size = elements;
135 break;
136 case 10:
137 avctx->pix_fmt = AV_PIX_FMT_RGB48;
138 target_packet_size = 6;
139 source_packet_size = 4;
140 break;
141 case 12:
142 case 16:
143 if (endian) {
144 avctx->pix_fmt = AV_PIX_FMT_RGB48BE;
145 } else {
146 avctx->pix_fmt = AV_PIX_FMT_RGB48LE;
147 }
148 target_packet_size = 6;
149 source_packet_size = elements * 2;
150 break;
151 default:
152 av_log(avctx, AV_LOG_ERROR, "Unsupported color depth : %d\n", bits_per_color);
153 return -1;
154 }
155
156 if (s->picture.data[0])
157 avctx->release_buffer(avctx, &s->picture);
158 if (av_image_check_size(w, h, 0, avctx))
159 return -1;
160 if (w != avctx->width || h != avctx->height)
161 avcodec_set_dimensions(avctx, w, h);
162 if (avctx->get_buffer(avctx, p) < 0) {
163 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
164 return -1;
165 }
166
167 // Move pointer to offset from start of file
168 buf = avpkt->data + offset;
169
170 ptr = p->data[0];
171 stride = p->linesize[0];
172
173 if (source_packet_size*avctx->width*avctx->height > buf_end - buf) {
174 av_log(avctx, AV_LOG_ERROR, "Overread buffer. Invalid header?\n");
175 return -1;
176 }
177 switch (bits_per_color) {
178 case 10:
179 for (x = 0; x < avctx->height; x++) {
180 uint16_t *dst = (uint16_t*)ptr;
181 for (y = 0; y < avctx->width; y++) {
182 rgbBuffer = read32(&buf, endian);
183 // Read out the 10-bit colors and convert to 16-bit
184 *dst++ = make_16bit(rgbBuffer >> 16);
185 *dst++ = make_16bit(rgbBuffer >> 6);
186 *dst++ = make_16bit(rgbBuffer << 4);
187 }
188 ptr += stride;
189 }
190 break;
191 case 8:
192 case 12: // Treat 12-bit as 16-bit
193 case 16:
194 if (source_packet_size == target_packet_size) {
195 for (x = 0; x < avctx->height; x++) {
196 memcpy(ptr, buf, target_packet_size*avctx->width);
197 ptr += stride;
198 buf += source_packet_size*avctx->width;
199 }
200 } else {
201 for (x = 0; x < avctx->height; x++) {
202 uint8_t *dst = ptr;
203 for (y = 0; y < avctx->width; y++) {
204 memcpy(dst, buf, target_packet_size);
205 dst += target_packet_size;
206 buf += source_packet_size;
207 }
208 ptr += stride;
209 }
210 }
211 break;
212 }
213
214 *picture = s->picture;
215 *data_size = sizeof(AVPicture);
216
217 return buf_size;
218 }
219
220 static av_cold int decode_init(AVCodecContext *avctx)
221 {
222 DPXContext *s = avctx->priv_data;
223 avcodec_get_frame_defaults(&s->picture);
224 avctx->coded_frame = &s->picture;
225 return 0;
226 }
227
228 static av_cold int decode_end(AVCodecContext *avctx)
229 {
230 DPXContext *s = avctx->priv_data;
231 if (s->picture.data[0])
232 avctx->release_buffer(avctx, &s->picture);
233
234 return 0;
235 }
236
237 AVCodec ff_dpx_decoder = {
238 .name = "dpx",
239 .type = AVMEDIA_TYPE_VIDEO,
240 .id = AV_CODEC_ID_DPX,
241 .priv_data_size = sizeof(DPXContext),
242 .init = decode_init,
243 .close = decode_end,
244 .decode = decode_frame,
245 .long_name = NULL_IF_CONFIG_SMALL("DPX image"),
246 .capabilities = CODEC_CAP_DR1,
247 };