#include "libavutil/imgutils.h"
#include "bytestream.h"
#include "avcodec.h"
-
-typedef struct DPXContext {
- AVFrame picture;
-} DPXContext;
-
+#include "internal.h"
static unsigned int read32(const uint8_t **ptr, int is_big)
{
static int decode_frame(AVCodecContext *avctx,
void *data,
- int *data_size,
+ int *got_frame,
AVPacket *avpkt)
{
const uint8_t *buf = avpkt->data;
const uint8_t *buf_end = avpkt->data + avpkt->size;
int buf_size = avpkt->size;
- DPXContext *const s = avctx->priv_data;
- AVFrame *picture = data;
- AVFrame *const p = &s->picture;
+ AVFrame *const p = data;
uint8_t *ptr;
- int magic_num, offset, endian;
- int x, y;
+ unsigned int offset;
+ int magic_num, endian;
+ int x, y, ret;
int w, h, stride, bits_per_color, descriptor, elements, target_packet_size, source_packet_size;
unsigned int rgbBuffer;
+ if (avpkt->size <= 1634) {
+ av_log(avctx, AV_LOG_ERROR, "Packet too small for DPX header\n");
+ return AVERROR_INVALIDDATA;
+ }
+
magic_num = AV_RB32(buf);
buf += 4;
endian = 1;
} else {
av_log(avctx, AV_LOG_ERROR, "DPX marker not found\n");
- return -1;
+ return AVERROR_INVALIDDATA;
}
offset = read32(&buf, endian);
+ if (avpkt->size <= offset) {
+ av_log(avctx, AV_LOG_ERROR, "Invalid data start offset\n");
+ return AVERROR_INVALIDDATA;
+ }
// Need to end in 0x304 offset from start of file
buf = avpkt->data + 0x304;
w = read32(&buf, endian);
avctx->bits_per_raw_sample =
bits_per_color = buf[0];
+ buf += 825;
+ avctx->sample_aspect_ratio.num = read32(&buf, endian);
+ avctx->sample_aspect_ratio.den = read32(&buf, endian);
+
switch (descriptor) {
case 51: // RGBA
elements = 4;
break;
default:
av_log(avctx, AV_LOG_ERROR, "Unsupported descriptor %d\n", descriptor);
- return -1;
+ return AVERROR_INVALIDDATA;
}
switch (bits_per_color) {
case 8:
if (elements == 4) {
- avctx->pix_fmt = PIX_FMT_RGBA;
+ avctx->pix_fmt = AV_PIX_FMT_RGBA;
} else {
- avctx->pix_fmt = PIX_FMT_RGB24;
+ avctx->pix_fmt = AV_PIX_FMT_RGB24;
}
source_packet_size = elements;
target_packet_size = elements;
break;
case 10:
- avctx->pix_fmt = PIX_FMT_RGB48;
+ avctx->pix_fmt = AV_PIX_FMT_RGB48;
target_packet_size = 6;
- source_packet_size = elements * 2;
+ source_packet_size = 4;
break;
case 12:
case 16:
if (endian) {
- avctx->pix_fmt = PIX_FMT_RGB48BE;
+ avctx->pix_fmt = AV_PIX_FMT_RGB48BE;
} else {
- avctx->pix_fmt = PIX_FMT_RGB48LE;
+ avctx->pix_fmt = AV_PIX_FMT_RGB48LE;
}
target_packet_size = 6;
source_packet_size = elements * 2;
break;
default:
av_log(avctx, AV_LOG_ERROR, "Unsupported color depth : %d\n", bits_per_color);
- return -1;
+ return AVERROR_INVALIDDATA;
}
- if (s->picture.data[0])
- avctx->release_buffer(avctx, &s->picture);
- if (av_image_check_size(w, h, 0, avctx))
- return -1;
+ if ((ret = av_image_check_size(w, h, 0, avctx)) < 0)
+ return ret;
if (w != avctx->width || h != avctx->height)
avcodec_set_dimensions(avctx, w, h);
- if (avctx->get_buffer(avctx, p) < 0) {
+ if ((ret = ff_get_buffer(avctx, p, 0)) < 0) {
av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
- return -1;
+ return ret;
}
// Move pointer to offset from start of file
ptr = p->data[0];
stride = p->linesize[0];
+ if (source_packet_size*avctx->width*avctx->height > buf_end - buf) {
+ av_log(avctx, AV_LOG_ERROR, "Overread buffer. Invalid header?\n");
+ return AVERROR_INVALIDDATA;
+ }
switch (bits_per_color) {
case 10:
for (x = 0; x < avctx->height; x++) {
case 8:
case 12: // Treat 12-bit as 16-bit
case 16:
- if (source_packet_size*avctx->width*avctx->height > buf_end - buf) {
- av_log(avctx, AV_LOG_ERROR, "Overread buffer. Invalid header?\n");
- return -1;
- }
if (source_packet_size == target_packet_size) {
for (x = 0; x < avctx->height; x++) {
memcpy(ptr, buf, target_packet_size*avctx->width);
break;
}
- *picture = s->picture;
- *data_size = sizeof(AVPicture);
+ *got_frame = 1;
return buf_size;
}
-static av_cold int decode_init(AVCodecContext *avctx)
-{
- DPXContext *s = avctx->priv_data;
- avcodec_get_frame_defaults(&s->picture);
- avctx->coded_frame = &s->picture;
- return 0;
-}
-
-static av_cold int decode_end(AVCodecContext *avctx)
-{
- DPXContext *s = avctx->priv_data;
- if (s->picture.data[0])
- avctx->release_buffer(avctx, &s->picture);
-
- return 0;
-}
-
AVCodec ff_dpx_decoder = {
- "dpx",
- AVMEDIA_TYPE_VIDEO,
- CODEC_ID_DPX,
- sizeof(DPXContext),
- decode_init,
- NULL,
- decode_end,
- decode_frame,
- 0,
- NULL,
- .long_name = NULL_IF_CONFIG_SMALL("DPX image"),
+ .name = "dpx",
+ .type = AVMEDIA_TYPE_VIDEO,
+ .id = AV_CODEC_ID_DPX,
+ .decode = decode_frame,
+ .long_name = NULL_IF_CONFIG_SMALL("DPX image"),
+ .capabilities = CODEC_CAP_DR1,
};