]> git.sesse.net Git - ffmpeg/blob - libavcodec/dpx.c
avcodec/dpx: refactor pixel format selection
[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         total_size = avctx->width * avctx->height * elements;
166         break;
167     case 10:
168         if (!packing) {
169             av_log(avctx, AV_LOG_ERROR, "Packing to 32bit required\n");
170             return -1;
171         }
172         total_size = (avctx->width * elements + 2) / 3 * 4 * avctx->height;
173         break;
174     case 12:
175         if (!packing) {
176             av_log(avctx, AV_LOG_ERROR, "Packing to 16bit required\n");
177             return -1;
178         }
179         total_size = 2 * avctx->width * avctx->height * elements;
180         break;
181     case 16:
182         total_size = 2 * avctx->width * avctx->height * elements;
183         break;
184     case 1:
185     case 32:
186     case 64:
187         avpriv_report_missing_feature(avctx, "Depth %d", bits_per_color);
188         return AVERROR_PATCHWELCOME;
189     default:
190         return AVERROR_INVALIDDATA;
191     }
192
193     switch (1000 * descriptor + 10 * bits_per_color + endian) {
194     case 50081:
195     case 50080:
196         avctx->pix_fmt = AV_PIX_FMT_RGB24;
197         break;
198     case 51081:
199     case 51080:
200         avctx->pix_fmt = AV_PIX_FMT_RGBA;
201         break;
202     case 50100:
203     case 51100:
204     case 50101:
205     case 51101:
206         avctx->pix_fmt = AV_PIX_FMT_GBRP10;
207         break;
208     case 50120:
209     case 51120:
210     case 50121:
211     case 51121:
212         avctx->pix_fmt = AV_PIX_FMT_GBRP12;
213         break;
214     case 50161:
215         avctx->pix_fmt = AV_PIX_FMT_RGB48BE;
216         break;
217     case 50160:
218         avctx->pix_fmt = AV_PIX_FMT_RGB48LE;
219         break;
220     case 51161:
221         avctx->pix_fmt = AV_PIX_FMT_RGBA64BE;
222         break;
223     case 51160:
224         avctx->pix_fmt = AV_PIX_FMT_RGBA64LE;
225         break;
226     default:
227         av_log(avctx, AV_LOG_ERROR, "Unsupported format\n");
228         return AVERROR_PATCHWELCOME;
229     }
230
231     if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
232         return ret;
233
234     // Move pointer to offset from start of file
235     buf =  avpkt->data + offset;
236
237     for (i=0; i<AV_NUM_DATA_POINTERS; i++)
238         ptr[i] = p->data[i];
239
240     if (total_size + (int64_t)offset > avpkt->size) {
241         av_log(avctx, AV_LOG_ERROR, "Overread buffer. Invalid header?\n");
242         return AVERROR_INVALIDDATA;
243     }
244     switch (bits_per_color) {
245     case 10:
246         for (x = 0; x < avctx->height; x++) {
247             uint16_t *dst[3] = {(uint16_t*)ptr[0],
248                                 (uint16_t*)ptr[1],
249                                 (uint16_t*)ptr[2]};
250             for (y = 0; y < avctx->width; y++) {
251                 *dst[2]++ = read10in32(&buf, &rgbBuffer,
252                                        &n_datum, endian);
253                 *dst[0]++ = read10in32(&buf, &rgbBuffer,
254                                        &n_datum, endian);
255                 *dst[1]++ = read10in32(&buf, &rgbBuffer,
256                                        &n_datum, endian);
257                 // For 10 bit, ignore alpha
258                 if (elements == 4)
259                     read10in32(&buf, &rgbBuffer,
260                                &n_datum, endian);
261             }
262             n_datum = 0;
263             for (i = 0; i < 3; i++)
264                 ptr[i] += p->linesize[i];
265         }
266         break;
267     case 12:
268         for (x = 0; x < avctx->height; x++) {
269             uint16_t *dst[3] = {(uint16_t*)ptr[0],
270                                 (uint16_t*)ptr[1],
271                                 (uint16_t*)ptr[2]};
272             for (y = 0; y < avctx->width; y++) {
273                 *dst[2] = read16(&buf, endian) >> 4;
274                 dst[2]++;
275                 *dst[0] = read16(&buf, endian) >> 4;
276                 dst[0]++;
277                 *dst[1] = read16(&buf, endian) >> 4;
278                 dst[1]++;
279                 // For 12 bit, ignore alpha
280                 if (elements == 4)
281                     buf += 2;
282             }
283             for (i = 0; i < 3; i++)
284                 ptr[i] += p->linesize[i];
285         }
286         break;
287     case 16:
288         elements *= 2;
289     case 8:
290         av_image_copy_plane(ptr[0], p->linesize[0],
291                             buf, elements * avctx->width,
292                             elements * avctx->width, avctx->height);
293         break;
294     }
295
296     *got_frame = 1;
297
298     return buf_size;
299 }
300
301 AVCodec ff_dpx_decoder = {
302     .name           = "dpx",
303     .long_name      = NULL_IF_CONFIG_SMALL("DPX (Digital Picture Exchange) image"),
304     .type           = AVMEDIA_TYPE_VIDEO,
305     .id             = AV_CODEC_ID_DPX,
306     .decode         = decode_frame,
307     .capabilities   = CODEC_CAP_DR1,
308 };