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