return AVERROR(ENOMEM) if stream could not be allocated
[ffmpeg-lucabe.git] / libavcodec / dpx.c
blobfec12f94cac75d269907aef5ca03e32e921673f9
1 /*
2 * DPX (.dpx) image decoder
3 * Copyright (c) 2009 Jimmy Christensen
5 * This file is part of FFmpeg.
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.
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.
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
22 #include "libavutil/intreadwrite.h"
23 #include "bytestream.h"
24 #include "avcodec.h"
26 typedef struct DPXContext {
27 AVFrame picture;
28 } DPXContext;
31 static unsigned int read32(const uint8_t **ptr, int is_big)
33 unsigned int temp;
34 if (is_big) {
35 temp = AV_RB32(*ptr);
36 } else {
37 temp = AV_RL32(*ptr);
39 *ptr += 4;
40 return temp;
43 static inline unsigned make_16bit(unsigned value)
45 // mask away invalid bits
46 value &= 0xFFC0;
47 // correctly expand to 16 bits
48 return value + (value >> 10);
51 static int decode_frame(AVCodecContext *avctx,
52 void *data,
53 int *data_size,
54 AVPacket *avpkt)
56 const uint8_t *buf = avpkt->data;
57 int buf_size = avpkt->size;
58 DPXContext *const s = avctx->priv_data;
59 AVFrame *picture = data;
60 AVFrame *const p = &s->picture;
61 uint8_t *ptr;
63 int magic_num, offset, endian;
64 int x, y;
65 int w, h, stride, bits_per_color, descriptor, elements, target_packet_size, source_packet_size;
67 unsigned int rgbBuffer;
69 magic_num = AV_RB32(buf);
70 buf += 4;
72 /* Check if the files "magic number" is "SDPX" which means it uses
73 * big-endian or XPDS which is for little-endian files */
74 if (magic_num == AV_RL32("SDPX")) {
75 endian = 0;
76 } else if (magic_num == AV_RB32("SDPX")) {
77 endian = 1;
78 } else {
79 av_log(avctx, AV_LOG_ERROR, "DPX marker not found\n");
80 return -1;
83 offset = read32(&buf, endian);
84 // Need to end in 0x304 offset from start of file
85 buf = avpkt->data + 0x304;
86 w = read32(&buf, endian);
87 h = read32(&buf, endian);
89 // Need to end in 0x320 to read the descriptor
90 buf += 20;
91 descriptor = buf[0];
93 // Need to end in 0x323 to read the bits per color
94 buf += 3;
95 bits_per_color = buf[0];
97 switch (descriptor) {
98 case 51: // RGBA
99 elements = 4;
100 break;
101 case 50: // RGB
102 elements = 3;
103 break;
104 default:
105 av_log(avctx, AV_LOG_ERROR, "Unsupported descriptor %d\n", descriptor);
106 return -1;
109 switch (bits_per_color) {
110 case 8:
111 if (elements == 4) {
112 avctx->pix_fmt = PIX_FMT_RGBA;
113 } else {
114 avctx->pix_fmt = PIX_FMT_RGB24;
116 source_packet_size = elements;
117 target_packet_size = elements;
118 break;
119 case 10:
120 avctx->pix_fmt = PIX_FMT_RGB48;
121 target_packet_size = 6;
122 source_packet_size = elements * 2;
123 break;
124 case 12:
125 case 16:
126 if (endian) {
127 avctx->pix_fmt = PIX_FMT_RGB48BE;
128 } else {
129 avctx->pix_fmt = PIX_FMT_RGB48LE;
131 target_packet_size = 6;
132 source_packet_size = elements * 2;
133 break;
134 default:
135 av_log(avctx, AV_LOG_ERROR, "Unsupported color depth : %d\n", bits_per_color);
136 return -1;
139 if (s->picture.data[0])
140 avctx->release_buffer(avctx, &s->picture);
141 if (avcodec_check_dimensions(avctx, w, h))
142 return -1;
143 if (w != avctx->width || h != avctx->height)
144 avcodec_set_dimensions(avctx, w, h);
145 if (avctx->get_buffer(avctx, p) < 0) {
146 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
147 return -1;
150 // Move pointer to offset from start of file
151 buf = avpkt->data + offset;
153 ptr = p->data[0];
154 stride = p->linesize[0];
156 switch (bits_per_color) {
157 case 10:
158 for (x = 0; x < avctx->height; x++) {
159 uint16_t *dst = (uint16_t*)ptr;
160 for (y = 0; y < avctx->width; y++) {
161 rgbBuffer = read32(&buf, endian);
162 // Read out the 10-bit colors and convert to 16-bit
163 *dst++ = make_16bit(rgbBuffer >> 16);
164 *dst++ = make_16bit(rgbBuffer >> 6);
165 *dst++ = make_16bit(rgbBuffer << 4);
167 ptr += stride;
169 break;
170 case 8:
171 case 12: // Treat 12-bit as 16-bit
172 case 16:
173 if (source_packet_size == target_packet_size) {
174 for (x = 0; x < avctx->height; x++) {
175 memcpy(ptr, buf, target_packet_size*avctx->width);
176 ptr += stride;
177 buf += source_packet_size*avctx->width;
179 } else {
180 for (x = 0; x < avctx->height; x++) {
181 uint8_t *dst = ptr;
182 for (y = 0; y < avctx->width; y++) {
183 memcpy(dst, buf, target_packet_size);
184 dst += target_packet_size;
185 buf += source_packet_size;
187 ptr += stride;
190 break;
193 *picture = s->picture;
194 *data_size = sizeof(AVPicture);
196 return buf_size;
199 static av_cold int decode_init(AVCodecContext *avctx)
201 DPXContext *s = avctx->priv_data;
202 avcodec_get_frame_defaults(&s->picture);
203 avctx->coded_frame = &s->picture;
204 return 0;
207 static av_cold int decode_end(AVCodecContext *avctx)
209 DPXContext *s = avctx->priv_data;
210 if (s->picture.data[0])
211 avctx->release_buffer(avctx, &s->picture);
213 return 0;
216 AVCodec dpx_decoder = {
217 "dpx",
218 CODEC_TYPE_VIDEO,
219 CODEC_ID_DPX,
220 sizeof(DPXContext),
221 decode_init,
222 NULL,
223 decode_end,
224 decode_frame,
226 NULL,
227 .long_name = NULL_IF_CONFIG_SMALL("DPX image"),