X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=libavcodec%2Fdpx.c;h=b1833ed9ef06af59685ad3c76db30c3057178199;hb=b2f32d60eeaf883bb7d9e1b8cc2fb9a983d08f72;hp=1aa2cbd1c8acab8f7ad45656546fb6f150041d47;hpb=c00b218a8f75ed3eb87c213d95bd5775c0af5e12;p=ffmpeg diff --git a/libavcodec/dpx.c b/libavcodec/dpx.c index 1aa2cbd1c8a..b1833ed9ef0 100644 --- a/libavcodec/dpx.c +++ b/libavcodec/dpx.c @@ -19,6 +19,7 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ +#include "libavutil/avstring.h" #include "libavutil/intreadwrite.h" #include "libavutil/intfloat.h" #include "libavutil/imgutils.h" @@ -50,9 +51,11 @@ static unsigned int read32(const uint8_t **ptr, int is_big) return temp; } -static uint16_t read10in32(const uint8_t **ptr, uint32_t * lbuf, - int * n_datum, int is_big) +static uint16_t read10in32_gray(const uint8_t **ptr, uint32_t *lbuf, + int *n_datum, int is_big, int shift) { + uint16_t temp; + if (*n_datum) (*n_datum)--; else { @@ -60,11 +63,59 @@ static uint16_t read10in32(const uint8_t **ptr, uint32_t * lbuf, *n_datum = 2; } - *lbuf = (*lbuf << 10) | (*lbuf >> 22); + temp = *lbuf >> shift & 0x3FF; + *lbuf = *lbuf >> 10; + + return temp; +} + +static uint16_t read10in32(const uint8_t **ptr, uint32_t *lbuf, + int *n_datum, int is_big, int shift) +{ + if (*n_datum) + (*n_datum)--; + else { + *lbuf = read32(ptr, is_big); + *n_datum = 2; + } + + *lbuf = *lbuf << 10 | *lbuf >> shift & 0x3FFFFF; return *lbuf & 0x3FF; } +static uint16_t read12in32(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 = 7; + } + + switch (*n_datum){ + case 7: return *lbuf & 0xFFF; + case 6: return (*lbuf >> 12) & 0xFFF; + case 5: { + uint32_t c = *lbuf >> 24; + *lbuf = read32(ptr, is_big); + c |= *lbuf << 8; + return c & 0xFFF; + } + case 4: return (*lbuf >> 4) & 0xFFF; + case 3: return (*lbuf >> 16) & 0xFFF; + case 2: { + uint32_t c = *lbuf >> 28; + *lbuf = read32(ptr, is_big); + c |= *lbuf << 4; + return c & 0xFFF; + } + case 1: return (*lbuf >> 8) & 0xFFF; + default: return *lbuf >> 20; + } +} + static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, @@ -74,6 +125,9 @@ static int decode_frame(AVCodecContext *avctx, int buf_size = avpkt->size; AVFrame *const p = data; uint8_t *ptr[AV_NUM_DATA_POINTERS]; + uint32_t header_version, version = 0; + char creator[101]; + char input_device[33]; unsigned int offset; int magic_num, endian; @@ -109,6 +163,15 @@ static int decode_frame(AVCodecContext *avctx, return AVERROR_INVALIDDATA; } + header_version = read32(&buf, 0); + if (header_version == MKTAG('V','1','.','0')) + version = 1; + if (header_version == MKTAG('V','2','.','0')) + version = 2; + if (!version) + av_log(avctx, AV_LOG_WARNING, "Unknown header format version %s.\n", + av_fourcc2str(header_version)); + // Check encryption buf = avpkt->data + 660; ret = read32(&buf, endian); @@ -138,10 +201,6 @@ static int decode_frame(AVCodecContext *avctx, packing = read16(&buf, endian); encoding = read16(&buf, endian); - if (packing > 1) { - avpriv_report_missing_feature(avctx, "Packing %d", packing); - return AVERROR_PATCHWELCOME; - } if (encoding) { avpriv_report_missing_feature(avctx, "Encoding %d", encoding); return AVERROR_PATCHWELCOME; @@ -200,11 +259,18 @@ static int decode_frame(AVCodecContext *avctx, stride = (avctx->width * elements + 2) / 3 * 4; break; case 12: - if (!packing) { - av_log(avctx, AV_LOG_ERROR, "Packing to 16bit required\n"); - return -1; + stride = avctx->width * elements; + if (packing) { + stride *= 2; + } else { + stride *= 3; + if (stride % 8) { + stride /= 8; + stride++; + stride *= 8; + } + stride /= 2; } - stride = 2 * avctx->width * elements; break; case 16: stride = 2 * avctx->width * elements; @@ -275,6 +341,10 @@ static int decode_frame(AVCodecContext *avctx, case 51121: avctx->pix_fmt = AV_PIX_FMT_GBRAP12; break; + case 6100: + case 6101: + avctx->pix_fmt = AV_PIX_FMT_GRAY10; + break; case 6161: avctx->pix_fmt = AV_PIX_FMT_GRAY16BE; break; @@ -312,6 +382,14 @@ static int decode_frame(AVCodecContext *avctx, if ((ret = ff_get_buffer(avctx, p, 0)) < 0) return ret; + av_strlcpy(creator, avpkt->data + 160, 100); + creator[100] = '\0'; + av_dict_set(&p->metadata, "Creator", creator, 0); + + av_strlcpy(input_device, avpkt->data + 1556, 32); + input_device[32] = '\0'; + av_dict_set(&p->metadata, "Input Device", input_device, 0); + // Move pointer to offset from start of file buf = avpkt->data + offset; @@ -325,19 +403,27 @@ static int decode_frame(AVCodecContext *avctx, (uint16_t*)ptr[1], (uint16_t*)ptr[2], (uint16_t*)ptr[3]}; + int shift = elements > 1 ? packing == 1 ? 22 : 20 : packing == 1 ? 2 : 0; 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); + if (elements >= 3) + *dst[2]++ = read10in32(&buf, &rgbBuffer, + &n_datum, endian, shift); + if (elements == 1) + *dst[0]++ = read10in32_gray(&buf, &rgbBuffer, + &n_datum, endian, shift); + else + *dst[0]++ = read10in32(&buf, &rgbBuffer, + &n_datum, endian, shift); + if (elements >= 2) + *dst[1]++ = read10in32(&buf, &rgbBuffer, + &n_datum, endian, shift); if (elements == 4) *dst[3]++ = read10in32(&buf, &rgbBuffer, - &n_datum, endian); + &n_datum, endian, shift); } - n_datum = 0; + if (memcmp(input_device, "Scanity", 7)) + n_datum = 0; for (i = 0; i < elements; i++) ptr[i] += p->linesize[i]; } @@ -348,16 +434,31 @@ static int decode_frame(AVCodecContext *avctx, (uint16_t*)ptr[1], (uint16_t*)ptr[2], (uint16_t*)ptr[3]}; + int shift = packing == 1 ? 4 : 0; for (y = 0; y < avctx->width; y++) { - if (elements >= 3) - *dst[2]++ = read16(&buf, endian) >> 4; - *dst[0] = read16(&buf, endian) >> 4; - dst[0]++; - if (elements >= 2) - *dst[1]++ = read16(&buf, endian) >> 4; - if (elements == 4) - *dst[3]++ = read16(&buf, endian) >> 4; + if (packing) { + if (elements >= 3) + *dst[2]++ = read16(&buf, endian) >> shift & 0xFFF; + *dst[0]++ = read16(&buf, endian) >> shift & 0xFFF; + if (elements >= 2) + *dst[1]++ = read16(&buf, endian) >> shift & 0xFFF; + if (elements == 4) + *dst[3]++ = read16(&buf, endian) >> shift & 0xFFF; + } else { + if (elements >= 3) + *dst[2]++ = read12in32(&buf, &rgbBuffer, + &n_datum, endian); + *dst[0]++ = read12in32(&buf, &rgbBuffer, + &n_datum, endian); + if (elements >= 2) + *dst[1]++ = read12in32(&buf, &rgbBuffer, + &n_datum, endian); + if (elements == 4) + *dst[3]++ = read12in32(&buf, &rgbBuffer, + &n_datum, endian); + } } + n_datum = 0; for (i = 0; i < elements; i++) ptr[i] += p->linesize[i]; // Jump to next aligned position