Statistics
| Branch: | Revision:

ffmpeg / libavcodec / dpx.c @ 3777ea13

History | View | Annotate | Download (6.93 KB)

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
    if (avpkt->size <= 0x324) {
72
        av_log(avctx, AV_LOG_ERROR, "Packet too small for DPX header\n");
73
        return AVERROR_INVALIDDATA;
74
    }
75

    
76
    magic_num = AV_RB32(buf);
77
    buf += 4;
78

    
79
    /* Check if the files "magic number" is "SDPX" which means it uses
80
     * big-endian or XPDS which is for little-endian files */
81
    if (magic_num == AV_RL32("SDPX")) {
82
        endian = 0;
83
    } else if (magic_num == AV_RB32("SDPX")) {
84
        endian = 1;
85
    } else {
86
        av_log(avctx, AV_LOG_ERROR, "DPX marker not found\n");
87
        return -1;
88
    }
89

    
90
    offset = read32(&buf, endian);
91
    if (avpkt->size <= offset) {
92
        av_log(avctx, AV_LOG_ERROR, "Invalid data start offset\n");
93
        return AVERROR_INVALIDDATA;
94
    }
95
    // Need to end in 0x304 offset from start of file
96
    buf = avpkt->data + 0x304;
97
    w = read32(&buf, endian);
98
    h = read32(&buf, endian);
99

    
100
    // Need to end in 0x320 to read the descriptor
101
    buf += 20;
102
    descriptor = buf[0];
103

    
104
    // Need to end in 0x323 to read the bits per color
105
    buf += 3;
106
    avctx->bits_per_raw_sample =
107
    bits_per_color = buf[0];
108

    
109
    switch (descriptor) {
110
        case 51: // RGBA
111
            elements = 4;
112
            break;
113
        case 50: // RGB
114
            elements = 3;
115
            break;
116
        default:
117
            av_log(avctx, AV_LOG_ERROR, "Unsupported descriptor %d\n", descriptor);
118
            return -1;
119
    }
120

    
121
    switch (bits_per_color) {
122
        case 8:
123
            if (elements == 4) {
124
                avctx->pix_fmt = PIX_FMT_RGBA;
125
            } else {
126
                avctx->pix_fmt = PIX_FMT_RGB24;
127
            }
128
            source_packet_size = elements;
129
            target_packet_size = elements;
130
            break;
131
        case 10:
132
            avctx->pix_fmt = PIX_FMT_RGB48;
133
            target_packet_size = 6;
134
            source_packet_size = 4;
135
            break;
136
        case 12:
137
        case 16:
138
            if (endian) {
139
                avctx->pix_fmt = PIX_FMT_RGB48BE;
140
            } else {
141
                avctx->pix_fmt = PIX_FMT_RGB48LE;
142
            }
143
            target_packet_size = 6;
144
            source_packet_size = elements * 2;
145
            break;
146
        default:
147
            av_log(avctx, AV_LOG_ERROR, "Unsupported color depth : %d\n", bits_per_color);
148
            return -1;
149
    }
150

    
151
    if (s->picture.data[0])
152
        avctx->release_buffer(avctx, &s->picture);
153
    if (av_image_check_size(w, h, 0, avctx))
154
        return -1;
155
    if (w != avctx->width || h != avctx->height)
156
        avcodec_set_dimensions(avctx, w, h);
157
    if (avctx->get_buffer(avctx, p) < 0) {
158
        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
159
        return -1;
160
    }
161

    
162
    // Move pointer to offset from start of file
163
    buf =  avpkt->data + offset;
164

    
165
    ptr    = p->data[0];
166
    stride = p->linesize[0];
167

    
168
    if (source_packet_size*avctx->width*avctx->height > buf_end - buf) {
169
        av_log(avctx, AV_LOG_ERROR, "Overread buffer. Invalid header?\n");
170
        return -1;
171
    }
172
    switch (bits_per_color) {
173
        case 10:
174
            for (x = 0; x < avctx->height; x++) {
175
               uint16_t *dst = (uint16_t*)ptr;
176
               for (y = 0; y < avctx->width; y++) {
177
                   rgbBuffer = read32(&buf, endian);
178
                   // Read out the 10-bit colors and convert to 16-bit
179
                   *dst++ = make_16bit(rgbBuffer >> 16);
180
                   *dst++ = make_16bit(rgbBuffer >>  6);
181
                   *dst++ = make_16bit(rgbBuffer <<  4);
182
               }
183
               ptr += stride;
184
            }
185
            break;
186
        case 8:
187
        case 12: // Treat 12-bit as 16-bit
188
        case 16:
189
            if (source_packet_size == target_packet_size) {
190
                for (x = 0; x < avctx->height; x++) {
191
                    memcpy(ptr, buf, target_packet_size*avctx->width);
192
                    ptr += stride;
193
                    buf += source_packet_size*avctx->width;
194
                }
195
            } else {
196
                for (x = 0; x < avctx->height; x++) {
197
                    uint8_t *dst = ptr;
198
                    for (y = 0; y < avctx->width; y++) {
199
                        memcpy(dst, buf, target_packet_size);
200
                        dst += target_packet_size;
201
                        buf += source_packet_size;
202
                    }
203
                    ptr += stride;
204
                }
205
            }
206
            break;
207
    }
208

    
209
    *picture   = s->picture;
210
    *data_size = sizeof(AVPicture);
211

    
212
    return buf_size;
213
}
214

    
215
static av_cold int decode_init(AVCodecContext *avctx)
216
{
217
    DPXContext *s = avctx->priv_data;
218
    avcodec_get_frame_defaults(&s->picture);
219
    avctx->coded_frame = &s->picture;
220
    return 0;
221
}
222

    
223
static av_cold int decode_end(AVCodecContext *avctx)
224
{
225
    DPXContext *s = avctx->priv_data;
226
    if (s->picture.data[0])
227
        avctx->release_buffer(avctx, &s->picture);
228

    
229
    return 0;
230
}
231

    
232
AVCodec ff_dpx_decoder = {
233
    "dpx",
234
    AVMEDIA_TYPE_VIDEO,
235
    CODEC_ID_DPX,
236
    sizeof(DPXContext),
237
    decode_init,
238
    NULL,
239
    decode_end,
240
    decode_frame,
241
    0,
242
    NULL,
243
    .long_name = NULL_IF_CONFIG_SMALL("DPX image"),
244
};