]> git.sesse.net Git - ffmpeg/blob - libavcodec/dpx.c
libavdevice: Define _XOPEN_SOURCE for usleep
[ffmpeg] / 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     int magic_num, offset, endian;
66     int x, y;
67     int w, h, stride, bits_per_color, descriptor, elements, target_packet_size, source_packet_size;
68
69     unsigned int rgbBuffer;
70
71     magic_num = AV_RB32(buf);
72     buf += 4;
73
74     /* Check if the files "magic number" is "SDPX" which means it uses
75      * big-endian or XPDS which is for little-endian files */
76     if (magic_num == AV_RL32("SDPX")) {
77         endian = 0;
78     } else if (magic_num == AV_RB32("SDPX")) {
79         endian = 1;
80     } else {
81         av_log(avctx, AV_LOG_ERROR, "DPX marker not found\n");
82         return -1;
83     }
84
85     offset = read32(&buf, endian);
86     // Need to end in 0x304 offset from start of file
87     buf = avpkt->data + 0x304;
88     w = read32(&buf, endian);
89     h = read32(&buf, endian);
90
91     // Need to end in 0x320 to read the descriptor
92     buf += 20;
93     descriptor = buf[0];
94
95     // Need to end in 0x323 to read the bits per color
96     buf += 3;
97     avctx->bits_per_raw_sample =
98     bits_per_color = buf[0];
99
100     switch (descriptor) {
101         case 51: // RGBA
102             elements = 4;
103             break;
104         case 50: // RGB
105             elements = 3;
106             break;
107         default:
108             av_log(avctx, AV_LOG_ERROR, "Unsupported descriptor %d\n", descriptor);
109             return -1;
110     }
111
112     switch (bits_per_color) {
113         case 8:
114             if (elements == 4) {
115                 avctx->pix_fmt = PIX_FMT_RGBA;
116             } else {
117                 avctx->pix_fmt = PIX_FMT_RGB24;
118             }
119             source_packet_size = elements;
120             target_packet_size = elements;
121             break;
122         case 10:
123             avctx->pix_fmt = PIX_FMT_RGB48;
124             target_packet_size = 6;
125             source_packet_size = elements * 2;
126             break;
127         case 12:
128         case 16:
129             if (endian) {
130                 avctx->pix_fmt = PIX_FMT_RGB48BE;
131             } else {
132                 avctx->pix_fmt = PIX_FMT_RGB48LE;
133             }
134             target_packet_size = 6;
135             source_packet_size = elements * 2;
136             break;
137         default:
138             av_log(avctx, AV_LOG_ERROR, "Unsupported color depth : %d\n", bits_per_color);
139             return -1;
140     }
141
142     if (s->picture.data[0])
143         avctx->release_buffer(avctx, &s->picture);
144     if (av_image_check_size(w, h, 0, avctx))
145         return -1;
146     if (w != avctx->width || h != avctx->height)
147         avcodec_set_dimensions(avctx, w, h);
148     if (avctx->get_buffer(avctx, p) < 0) {
149         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
150         return -1;
151     }
152
153     // Move pointer to offset from start of file
154     buf =  avpkt->data + offset;
155
156     ptr    = p->data[0];
157     stride = p->linesize[0];
158
159     switch (bits_per_color) {
160         case 10:
161             for (x = 0; x < avctx->height; x++) {
162                uint16_t *dst = (uint16_t*)ptr;
163                for (y = 0; y < avctx->width; y++) {
164                    rgbBuffer = read32(&buf, endian);
165                    // Read out the 10-bit colors and convert to 16-bit
166                    *dst++ = make_16bit(rgbBuffer >> 16);
167                    *dst++ = make_16bit(rgbBuffer >>  6);
168                    *dst++ = make_16bit(rgbBuffer <<  4);
169                }
170                ptr += stride;
171             }
172             break;
173         case 8:
174         case 12: // Treat 12-bit as 16-bit
175         case 16:
176             if (source_packet_size*avctx->width*avctx->height > buf_end - buf) {
177                 av_log(avctx, AV_LOG_ERROR, "Overread buffer. Invalid header?\n");
178                 return -1;
179             }
180             if (source_packet_size == target_packet_size) {
181                 for (x = 0; x < avctx->height; x++) {
182                     memcpy(ptr, buf, target_packet_size*avctx->width);
183                     ptr += stride;
184                     buf += source_packet_size*avctx->width;
185                 }
186             } else {
187                 for (x = 0; x < avctx->height; x++) {
188                     uint8_t *dst = ptr;
189                     for (y = 0; y < avctx->width; y++) {
190                         memcpy(dst, buf, target_packet_size);
191                         dst += target_packet_size;
192                         buf += source_packet_size;
193                     }
194                     ptr += stride;
195                 }
196             }
197             break;
198     }
199
200     *picture   = s->picture;
201     *data_size = sizeof(AVPicture);
202
203     return buf_size;
204 }
205
206 static av_cold int decode_init(AVCodecContext *avctx)
207 {
208     DPXContext *s = avctx->priv_data;
209     avcodec_get_frame_defaults(&s->picture);
210     avctx->coded_frame = &s->picture;
211     return 0;
212 }
213
214 static av_cold int decode_end(AVCodecContext *avctx)
215 {
216     DPXContext *s = avctx->priv_data;
217     if (s->picture.data[0])
218         avctx->release_buffer(avctx, &s->picture);
219
220     return 0;
221 }
222
223 AVCodec ff_dpx_decoder = {
224     "dpx",
225     AVMEDIA_TYPE_VIDEO,
226     CODEC_ID_DPX,
227     sizeof(DPXContext),
228     decode_init,
229     NULL,
230     decode_end,
231     decode_frame,
232     0,
233     NULL,
234     .long_name = NULL_IF_CONFIG_SMALL("DPX image"),
235 };