]> git.sesse.net Git - ffmpeg/blob - libavcodec/dpx.c
91545c6922324f630b254c588e3cc1aa27010b6f
[ffmpeg] / libavcodec / dpx.c
1 /*
2  * DPX (.dpx) image decoder
3  * Copyright (c) 2009 Jimmy Christensen
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 "bytestream.h"
25 #include "avcodec.h"
26 #include "internal.h"
27
28 static unsigned int read16(const uint8_t **ptr, int is_big)
29 {
30     unsigned int temp;
31     if (is_big) {
32         temp = AV_RB16(*ptr);
33     } else {
34         temp = AV_RL16(*ptr);
35     }
36     *ptr += 2;
37     return temp;
38 }
39
40 static unsigned int read32(const uint8_t **ptr, int is_big)
41 {
42     unsigned int temp;
43     if (is_big) {
44         temp = AV_RB32(*ptr);
45     } else {
46         temp = AV_RL32(*ptr);
47     }
48     *ptr += 4;
49     return temp;
50 }
51
52 static uint16_t read10in32(const uint8_t **ptr, uint32_t * lbuf,
53                                   int * n_datum, int is_big)
54 {
55     if (*n_datum)
56         (*n_datum)--;
57     else {
58         *lbuf = read32(ptr, is_big);
59         *n_datum = 2;
60     }
61
62     *lbuf = (*lbuf << 10) | (*lbuf >> 22);
63
64     return *lbuf & 0x3FF;
65 }
66
67 static int decode_frame(AVCodecContext *avctx,
68                         void *data,
69                         int *got_frame,
70                         AVPacket *avpkt)
71 {
72     const uint8_t *buf = avpkt->data;
73     int buf_size       = avpkt->size;
74     AVFrame *const p = data;
75     uint8_t *ptr[AV_NUM_DATA_POINTERS];
76
77     unsigned int offset;
78     int magic_num, endian;
79     int x, y, i, ret;
80     int w, h, bits_per_color, descriptor, elements, packing, total_size;
81     int encoding;
82
83     unsigned int rgbBuffer = 0;
84     int n_datum = 0;
85
86     if (avpkt->size <= 1634) {
87         av_log(avctx, AV_LOG_ERROR, "Packet too small for DPX header\n");
88         return AVERROR_INVALIDDATA;
89     }
90
91     magic_num = AV_RB32(buf);
92     buf += 4;
93
94     /* Check if the files "magic number" is "SDPX" which means it uses
95      * big-endian or XPDS which is for little-endian files */
96     if (magic_num == AV_RL32("SDPX")) {
97         endian = 0;
98     } else if (magic_num == AV_RB32("SDPX")) {
99         endian = 1;
100     } else {
101         av_log(avctx, AV_LOG_ERROR, "DPX marker not found\n");
102         return AVERROR_INVALIDDATA;
103     }
104
105     offset = read32(&buf, endian);
106     if (avpkt->size <= offset) {
107         av_log(avctx, AV_LOG_ERROR, "Invalid data start offset\n");
108         return AVERROR_INVALIDDATA;
109     }
110     // Need to end in 0x304 offset from start of file
111     buf = avpkt->data + 0x304;
112     w = read32(&buf, endian);
113     h = read32(&buf, endian);
114     if ((ret = av_image_check_size(w, h, 0, avctx)) < 0)
115         return ret;
116
117     if (w != avctx->width || h != avctx->height)
118         avcodec_set_dimensions(avctx, w, h);
119
120     // Need to end in 0x320 to read the descriptor
121     buf += 20;
122     descriptor = buf[0];
123
124     // Need to end in 0x323 to read the bits per color
125     buf += 3;
126     avctx->bits_per_raw_sample =
127     bits_per_color = buf[0];
128     buf++;
129     packing = read16(&buf, endian);
130     encoding = read16(&buf, endian);
131
132     if (packing > 1) {
133         avpriv_report_missing_feature(avctx, "Packing %d", packing);
134         return AVERROR_PATCHWELCOME;
135     }
136     if (encoding) {
137         avpriv_report_missing_feature(avctx, "Encoding %d", encoding);
138         return AVERROR_PATCHWELCOME;
139     }
140
141     buf += 820;
142     avctx->sample_aspect_ratio.num = read32(&buf, endian);
143     avctx->sample_aspect_ratio.den = read32(&buf, endian);
144     if (avctx->sample_aspect_ratio.num > 0 && avctx->sample_aspect_ratio.den > 0)
145         av_reduce(&avctx->sample_aspect_ratio.num, &avctx->sample_aspect_ratio.den,
146                    avctx->sample_aspect_ratio.num,  avctx->sample_aspect_ratio.den,
147                   0x10000);
148     else
149         avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
150
151     switch (descriptor) {
152         case 51: // RGBA
153             elements = 4;
154             break;
155         case 50: // RGB
156             elements = 3;
157             break;
158         default:
159             avpriv_report_missing_feature(avctx, "Descriptor %d", descriptor);
160             return AVERROR_PATCHWELCOME;
161     }
162
163     switch (bits_per_color) {
164         case 8:
165             if (elements == 4) {
166                 avctx->pix_fmt = AV_PIX_FMT_RGBA;
167             } else {
168                 avctx->pix_fmt = AV_PIX_FMT_RGB24;
169             }
170             total_size = avctx->width * avctx->height * elements;
171             break;
172         case 10:
173             if (!packing) {
174                 av_log(avctx, AV_LOG_ERROR, "Packing to 32bit required\n");
175                 return -1;
176             }
177             avctx->pix_fmt = AV_PIX_FMT_GBRP10;
178             total_size = (avctx->width * elements + 2) / 3 * 4 * avctx->height;
179             break;
180         case 12:
181             if (!packing) {
182                 av_log(avctx, AV_LOG_ERROR, "Packing to 16bit required\n");
183                 return -1;
184             }
185             avctx->pix_fmt = AV_PIX_FMT_GBRP12;
186             total_size = 2 * avctx->width * avctx->height * elements;
187             break;
188         case 16:
189             if (endian) {
190                 avctx->pix_fmt = elements == 4 ? AV_PIX_FMT_RGBA64BE : AV_PIX_FMT_RGB48BE;
191             } else {
192                 avctx->pix_fmt = elements == 4 ? AV_PIX_FMT_RGBA64LE : AV_PIX_FMT_RGB48LE;
193             }
194             total_size = 2 * avctx->width * avctx->height * elements;
195             break;
196         default:
197             av_log(avctx, AV_LOG_ERROR, "Unsupported color depth : %d\n", bits_per_color);
198             return AVERROR_INVALIDDATA;
199     }
200
201     if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
202         return ret;
203
204     // Move pointer to offset from start of file
205     buf =  avpkt->data + offset;
206
207     for (i=0; i<AV_NUM_DATA_POINTERS; i++)
208         ptr[i] = p->data[i];
209
210     if (total_size + (int64_t)offset > avpkt->size) {
211         av_log(avctx, AV_LOG_ERROR, "Overread buffer. Invalid header?\n");
212         return AVERROR_INVALIDDATA;
213     }
214     switch (bits_per_color) {
215     case 10:
216         for (x = 0; x < avctx->height; x++) {
217             uint16_t *dst[3] = {(uint16_t*)ptr[0],
218                                 (uint16_t*)ptr[1],
219                                 (uint16_t*)ptr[2]};
220             for (y = 0; y < avctx->width; y++) {
221                 *dst[2]++ = read10in32(&buf, &rgbBuffer,
222                                        &n_datum, endian);
223                 *dst[0]++ = read10in32(&buf, &rgbBuffer,
224                                        &n_datum, endian);
225                 *dst[1]++ = read10in32(&buf, &rgbBuffer,
226                                        &n_datum, endian);
227                 // For 10 bit, ignore alpha
228                 if (elements == 4)
229                     read10in32(&buf, &rgbBuffer,
230                                &n_datum, endian);
231             }
232             n_datum = 0;
233             for (i = 0; i < 3; i++)
234                 ptr[i] += p->linesize[i];
235         }
236         break;
237     case 12:
238         for (x = 0; x < avctx->height; x++) {
239             uint16_t *dst[3] = {(uint16_t*)ptr[0],
240                                 (uint16_t*)ptr[1],
241                                 (uint16_t*)ptr[2]};
242             for (y = 0; y < avctx->width; y++) {
243                 *dst[2] = read16(&buf, endian) >> 4;
244                 dst[2]++;
245                 *dst[0] = read16(&buf, endian) >> 4;
246                 dst[0]++;
247                 *dst[1] = read16(&buf, endian) >> 4;
248                 dst[1]++;
249                 // For 12 bit, ignore alpha
250                 if (elements == 4)
251                     buf += 2;
252             }
253             for (i = 0; i < 3; i++)
254                 ptr[i] += p->linesize[i];
255         }
256         break;
257     case 16:
258         elements *= 2;
259     case 8:
260         av_image_copy_plane(ptr[0], p->linesize[0],
261                             buf, elements * avctx->width,
262                             elements * avctx->width, avctx->height);
263         break;
264     }
265
266     *got_frame = 1;
267
268     return buf_size;
269 }
270
271 AVCodec ff_dpx_decoder = {
272     .name           = "dpx",
273     .long_name      = NULL_IF_CONFIG_SMALL("DPX image"),
274     .type           = AVMEDIA_TYPE_VIDEO,
275     .id             = AV_CODEC_ID_DPX,
276     .decode         = decode_frame,
277     .capabilities   = CODEC_CAP_DR1,
278 };