dpx.c 9.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 * DPX (.dpx) image decoder
 * Copyright (c) 2009 Jimmy Christensen
 *
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * FFmpeg is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with FFmpeg; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include "libavutil/intreadwrite.h"
23
#include "libavutil/intfloat.h"
24
#include "libavutil/imgutils.h"
25 26
#include "bytestream.h"
#include "avcodec.h"
27
#include "internal.h"
28

29 30 31 32 33 34 35 36 37 38 39 40
static unsigned int read16(const uint8_t **ptr, int is_big)
{
    unsigned int temp;
    if (is_big) {
        temp = AV_RB16(*ptr);
    } else {
        temp = AV_RL16(*ptr);
    }
    *ptr += 2;
    return temp;
}

41 42 43 44 45 46 47 48 49 50 51 52
static unsigned int read32(const uint8_t **ptr, int is_big)
{
    unsigned int temp;
    if (is_big) {
        temp = AV_RB32(*ptr);
    } else {
        temp = AV_RL32(*ptr);
    }
    *ptr += 4;
    return temp;
}

Georg Lippitsch's avatar
Georg Lippitsch committed
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
static uint16_t read10in32(const uint8_t **ptr, uint32_t * lbuf,
                                  int * n_datum, int is_big)
{
    if (*n_datum)
        (*n_datum)--;
    else {
        *lbuf = read32(ptr, is_big);
        *n_datum = 2;
    }

    *lbuf = (*lbuf << 10) | (*lbuf >> 22);

    return *lbuf & 0x3FF;
}

68 69
static int decode_frame(AVCodecContext *avctx,
                        void *data,
70
                        int *got_frame,
71 72 73 74
                        AVPacket *avpkt)
{
    const uint8_t *buf = avpkt->data;
    int buf_size       = avpkt->size;
75
    AVFrame *const p = data;
76
    uint8_t *ptr[AV_NUM_DATA_POINTERS];
77

78 79
    unsigned int offset;
    int magic_num, endian;
80
    int x, y, i, ret;
Georg Lippitsch's avatar
Georg Lippitsch committed
81
    int w, h, bits_per_color, descriptor, elements, packing, total_size;
82
    int encoding;
83

Georg Lippitsch's avatar
Georg Lippitsch committed
84 85
    unsigned int rgbBuffer = 0;
    int n_datum = 0;
86

87
    if (avpkt->size <= 1634) {
88 89 90 91
        av_log(avctx, AV_LOG_ERROR, "Packet too small for DPX header\n");
        return AVERROR_INVALIDDATA;
    }

92 93 94 95 96 97 98 99 100 101 102
    magic_num = AV_RB32(buf);
    buf += 4;

    /* Check if the files "magic number" is "SDPX" which means it uses
     * big-endian or XPDS which is for little-endian files */
    if (magic_num == AV_RL32("SDPX")) {
        endian = 0;
    } else if (magic_num == AV_RB32("SDPX")) {
        endian = 1;
    } else {
        av_log(avctx, AV_LOG_ERROR, "DPX marker not found\n");
103
        return AVERROR_INVALIDDATA;
104 105 106
    }

    offset = read32(&buf, endian);
107 108 109 110
    if (avpkt->size <= offset) {
        av_log(avctx, AV_LOG_ERROR, "Invalid data start offset\n");
        return AVERROR_INVALIDDATA;
    }
111 112 113 114
    // Need to end in 0x304 offset from start of file
    buf = avpkt->data + 0x304;
    w = read32(&buf, endian);
    h = read32(&buf, endian);
115

116 117
    if ((ret = ff_set_dimensions(avctx, w, h)) < 0)
        return ret;
118 119 120 121 122 123 124

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

    // Need to end in 0x323 to read the bits per color
    buf += 3;
125
    avctx->bits_per_raw_sample =
126
    bits_per_color = buf[0];
Georg Lippitsch's avatar
Georg Lippitsch committed
127
    buf++;
128
    packing = read16(&buf, endian);
129
    encoding = read16(&buf, endian);
130

131
    if (packing > 1) {
132
        avpriv_report_missing_feature(avctx, "Packing %d", packing);
133 134
        return AVERROR_PATCHWELCOME;
    }
135
    if (encoding) {
136
        avpriv_report_missing_feature(avctx, "Encoding %d", encoding);
137 138 139 140
        return AVERROR_PATCHWELCOME;
    }

    buf += 820;
141 142
    avctx->sample_aspect_ratio.num = read32(&buf, endian);
    avctx->sample_aspect_ratio.den = read32(&buf, endian);
143 144 145 146 147
    if (avctx->sample_aspect_ratio.num > 0 && avctx->sample_aspect_ratio.den > 0)
        av_reduce(&avctx->sample_aspect_ratio.num, &avctx->sample_aspect_ratio.den,
                   avctx->sample_aspect_ratio.num,  avctx->sample_aspect_ratio.den,
                  0x10000);
    else
148
        avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
149

150 151 152 153
    if (offset >= 1724 + 4) {
        buf = avpkt->data + 1724;
        i = read32(&buf, endian);
        if(i) {
154
            AVRational q = av_d2q(av_int2float(i), 4096);
155 156 157 158 159
            if (q.num > 0 && q.den > 0)
                avctx->time_base = av_inv_q(q);
        }
    }

160
    switch (descriptor) {
161 162 163
    case 6:  // Y
        elements = 1;
        break;
Paul B Mahol's avatar
Paul B Mahol committed
164
    case 52: // ABGR
165 166 167 168 169 170 171 172 173
    case 51: // RGBA
        elements = 4;
        break;
    case 50: // RGB
        elements = 3;
        break;
    default:
        avpriv_report_missing_feature(avctx, "Descriptor %d", descriptor);
        return AVERROR_PATCHWELCOME;
174 175 176
    }

    switch (bits_per_color) {
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
    case 8:
        total_size = avctx->width * avctx->height * elements;
        break;
    case 10:
        if (!packing) {
            av_log(avctx, AV_LOG_ERROR, "Packing to 32bit required\n");
            return -1;
        }
        total_size = (avctx->width * elements + 2) / 3 * 4 * avctx->height;
        break;
    case 12:
        if (!packing) {
            av_log(avctx, AV_LOG_ERROR, "Packing to 16bit required\n");
            return -1;
        }
        total_size = 2 * avctx->width * avctx->height * elements;
        break;
    case 16:
        total_size = 2 * avctx->width * avctx->height * elements;
        break;
197 198 199 200 201
    case 1:
    case 32:
    case 64:
        avpriv_report_missing_feature(avctx, "Depth %d", bits_per_color);
        return AVERROR_PATCHWELCOME;
202 203
    default:
        return AVERROR_INVALIDDATA;
204 205
    }

206
    switch (1000 * descriptor + 10 * bits_per_color + endian) {
207 208 209 210
    case 6081:
    case 6080:
        avctx->pix_fmt = AV_PIX_FMT_GRAY8;
        break;
211 212 213 214
    case 50081:
    case 50080:
        avctx->pix_fmt = AV_PIX_FMT_RGB24;
        break;
Paul B Mahol's avatar
Paul B Mahol committed
215 216 217 218
    case 52081:
    case 52080:
        avctx->pix_fmt = AV_PIX_FMT_ABGR;
        break;
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
    case 51081:
    case 51080:
        avctx->pix_fmt = AV_PIX_FMT_RGBA;
        break;
    case 50100:
    case 51100:
    case 50101:
    case 51101:
        avctx->pix_fmt = AV_PIX_FMT_GBRP10;
        break;
    case 50120:
    case 51120:
    case 50121:
    case 51121:
        avctx->pix_fmt = AV_PIX_FMT_GBRP12;
        break;
235 236 237 238 239 240
    case 6161:
        avctx->pix_fmt = AV_PIX_FMT_GRAY16BE;
        break;
    case 6160:
        avctx->pix_fmt = AV_PIX_FMT_GRAY16LE;
        break;
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
    case 50161:
        avctx->pix_fmt = AV_PIX_FMT_RGB48BE;
        break;
    case 50160:
        avctx->pix_fmt = AV_PIX_FMT_RGB48LE;
        break;
    case 51161:
        avctx->pix_fmt = AV_PIX_FMT_RGBA64BE;
        break;
    case 51160:
        avctx->pix_fmt = AV_PIX_FMT_RGBA64LE;
        break;
    default:
        av_log(avctx, AV_LOG_ERROR, "Unsupported format\n");
        return AVERROR_PATCHWELCOME;
    }

258
    if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
259
        return ret;
260 261 262 263

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

264 265
    for (i=0; i<AV_NUM_DATA_POINTERS; i++)
        ptr[i] = p->data[i];
266

267
    if (total_size + (int64_t)offset > avpkt->size) {
268
        av_log(avctx, AV_LOG_ERROR, "Overread buffer. Invalid header?\n");
269
        return AVERROR_INVALIDDATA;
270
    }
271
    switch (bits_per_color) {
Georg Lippitsch's avatar
Georg Lippitsch committed
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
    case 10:
        for (x = 0; x < avctx->height; x++) {
            uint16_t *dst[3] = {(uint16_t*)ptr[0],
                                (uint16_t*)ptr[1],
                                (uint16_t*)ptr[2]};
            for (y = 0; y < avctx->width; y++) {
                *dst[2]++ = read10in32(&buf, &rgbBuffer,
                                       &n_datum, endian);
                *dst[0]++ = read10in32(&buf, &rgbBuffer,
                                       &n_datum, endian);
                *dst[1]++ = read10in32(&buf, &rgbBuffer,
                                       &n_datum, endian);
                // For 10 bit, ignore alpha
                if (elements == 4)
                    read10in32(&buf, &rgbBuffer,
                               &n_datum, endian);
288
            }
289
            n_datum = 0;
Georg Lippitsch's avatar
Georg Lippitsch committed
290 291 292 293 294 295 296 297 298 299
            for (i = 0; i < 3; i++)
                ptr[i] += p->linesize[i];
        }
        break;
    case 12:
        for (x = 0; x < avctx->height; x++) {
            uint16_t *dst[3] = {(uint16_t*)ptr[0],
                                (uint16_t*)ptr[1],
                                (uint16_t*)ptr[2]};
            for (y = 0; y < avctx->width; y++) {
300
                *dst[2] = read16(&buf, endian) >> 4;
Georg Lippitsch's avatar
Georg Lippitsch committed
301
                dst[2]++;
302
                *dst[0] = read16(&buf, endian) >> 4;
Georg Lippitsch's avatar
Georg Lippitsch committed
303
                dst[0]++;
304
                *dst[1] = read16(&buf, endian) >> 4;
Georg Lippitsch's avatar
Georg Lippitsch committed
305 306 307 308
                dst[1]++;
                // For 12 bit, ignore alpha
                if (elements == 4)
                    buf += 2;
309
            }
Georg Lippitsch's avatar
Georg Lippitsch committed
310 311 312 313 314 315 316
            for (i = 0; i < 3; i++)
                ptr[i] += p->linesize[i];
        }
        break;
    case 16:
        elements *= 2;
    case 8:
317 318 319
        av_image_copy_plane(ptr[0], p->linesize[0],
                            buf, elements * avctx->width,
                            elements * avctx->width, avctx->height);
Georg Lippitsch's avatar
Georg Lippitsch committed
320
        break;
321 322
    }

323
    *got_frame = 1;
324 325 326 327

    return buf_size;
}

328
AVCodec ff_dpx_decoder = {
329
    .name           = "dpx",
330
    .long_name      = NULL_IF_CONFIG_SMALL("DPX (Digital Picture Exchange) image"),
331
    .type           = AVMEDIA_TYPE_VIDEO,
332
    .id             = AV_CODEC_ID_DPX,
333
    .decode         = decode_frame,
334
    .capabilities   = CODEC_CAP_DR1,
335
};