2011-03-26 06:12:35 +02:00
|
|
|
/*
|
|
|
|
* DPX (.dpx) image encoder
|
|
|
|
* Copyright (c) 2011 Peter Ross <pross@xvid.org>
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2012-08-06 16:49:32 +03:00
|
|
|
#include "libavutil/common.h"
|
2011-03-26 06:12:35 +02:00
|
|
|
#include "libavutil/intreadwrite.h"
|
|
|
|
#include "libavutil/imgutils.h"
|
|
|
|
#include "avcodec.h"
|
2022-03-16 19:18:28 +02:00
|
|
|
#include "codec_internal.h"
|
2021-04-25 01:43:26 +02:00
|
|
|
#include "encode.h"
|
2022-02-23 12:55:45 +02:00
|
|
|
#include "version.h"
|
2011-03-26 06:12:35 +02:00
|
|
|
|
|
|
|
typedef struct DPXContext {
|
|
|
|
int big_endian;
|
|
|
|
int bits_per_component;
|
2014-08-13 13:21:52 +03:00
|
|
|
int num_components;
|
2011-03-26 06:12:35 +02:00
|
|
|
int descriptor;
|
2012-08-24 16:24:15 +03:00
|
|
|
int planar;
|
2011-03-26 06:12:35 +02:00
|
|
|
} DPXContext;
|
|
|
|
|
|
|
|
static av_cold int encode_init(AVCodecContext *avctx)
|
|
|
|
{
|
|
|
|
DPXContext *s = avctx->priv_data;
|
2013-05-13 00:27:48 +03:00
|
|
|
const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
|
2011-03-26 06:12:35 +02:00
|
|
|
|
2013-05-15 12:23:14 +03:00
|
|
|
s->big_endian = !!(desc->flags & AV_PIX_FMT_FLAG_BE);
|
2015-09-08 17:10:48 +02:00
|
|
|
s->bits_per_component = desc->comp[0].depth;
|
2014-08-13 13:21:52 +03:00
|
|
|
s->num_components = desc->nb_components;
|
2013-05-15 12:23:14 +03:00
|
|
|
s->descriptor = (desc->flags & AV_PIX_FMT_FLAG_ALPHA) ? 51 : 50;
|
|
|
|
s->planar = !!(desc->flags & AV_PIX_FMT_FLAG_PLANAR);
|
2011-03-26 06:12:35 +02:00
|
|
|
|
|
|
|
switch (avctx->pix_fmt) {
|
2013-10-08 14:29:29 +03:00
|
|
|
case AV_PIX_FMT_ABGR:
|
|
|
|
s->descriptor = 52;
|
|
|
|
break;
|
2013-10-07 13:10:39 +03:00
|
|
|
case AV_PIX_FMT_GRAY16BE:
|
|
|
|
case AV_PIX_FMT_GRAY16LE:
|
|
|
|
case AV_PIX_FMT_GRAY8:
|
|
|
|
s->descriptor = 6;
|
|
|
|
break;
|
2013-05-13 00:27:48 +03:00
|
|
|
case AV_PIX_FMT_GBRP10BE:
|
|
|
|
case AV_PIX_FMT_GBRP10LE:
|
|
|
|
case AV_PIX_FMT_GBRP12BE:
|
|
|
|
case AV_PIX_FMT_GBRP12LE:
|
2012-10-06 13:10:34 +03:00
|
|
|
case AV_PIX_FMT_RGB24:
|
2013-05-13 00:27:48 +03:00
|
|
|
case AV_PIX_FMT_RGBA64BE:
|
|
|
|
case AV_PIX_FMT_RGBA64LE:
|
2012-10-06 13:10:34 +03:00
|
|
|
case AV_PIX_FMT_RGBA:
|
2011-03-26 06:12:35 +02:00
|
|
|
break;
|
2012-10-06 13:10:34 +03:00
|
|
|
case AV_PIX_FMT_RGB48LE:
|
|
|
|
case AV_PIX_FMT_RGB48BE:
|
2013-05-13 00:27:48 +03:00
|
|
|
if (avctx->bits_per_raw_sample)
|
|
|
|
s->bits_per_component = avctx->bits_per_raw_sample;
|
2012-08-24 16:24:15 +03:00
|
|
|
break;
|
2011-03-26 06:12:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-06-19 16:46:06 +02:00
|
|
|
static av_always_inline void write16_internal(int big_endian, void *p, int value)
|
|
|
|
{
|
|
|
|
if (big_endian) AV_WB16(p, value);
|
|
|
|
else AV_WL16(p, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
static av_always_inline void write32_internal(int big_endian, void *p, int value)
|
|
|
|
{
|
|
|
|
if (big_endian) AV_WB32(p, value);
|
|
|
|
else AV_WL32(p, value);
|
|
|
|
}
|
2011-03-26 06:12:35 +02:00
|
|
|
|
2015-06-19 16:46:06 +02:00
|
|
|
#define write16(p, value) write16_internal(s->big_endian, p, value)
|
|
|
|
#define write32(p, value) write32_internal(s->big_endian, p, value)
|
2011-03-26 06:12:35 +02:00
|
|
|
|
2015-10-14 11:33:22 +02:00
|
|
|
static void encode_rgb48_10bit(AVCodecContext *avctx, const AVFrame *pic,
|
2011-03-26 06:12:35 +02:00
|
|
|
uint8_t *dst)
|
2011-04-02 07:59:28 +03:00
|
|
|
{
|
|
|
|
DPXContext *s = avctx->priv_data;
|
|
|
|
const uint8_t *src = pic->data[0];
|
|
|
|
int x, y;
|
|
|
|
|
|
|
|
for (y = 0; y < avctx->height; y++) {
|
|
|
|
for (x = 0; x < avctx->width; x++) {
|
|
|
|
int value;
|
2012-10-12 22:18:50 +03:00
|
|
|
if (s->big_endian) {
|
2012-10-14 06:48:02 +03:00
|
|
|
value = ((AV_RB16(src + 6*x + 4) & 0xFFC0U) >> 4)
|
|
|
|
| ((AV_RB16(src + 6*x + 2) & 0xFFC0U) << 6)
|
|
|
|
| ((AV_RB16(src + 6*x + 0) & 0xFFC0U) << 16);
|
2011-04-02 07:59:28 +03:00
|
|
|
} else {
|
2012-10-14 06:48:02 +03:00
|
|
|
value = ((AV_RL16(src + 6*x + 4) & 0xFFC0U) >> 4)
|
|
|
|
| ((AV_RL16(src + 6*x + 2) & 0xFFC0U) << 6)
|
|
|
|
| ((AV_RL16(src + 6*x + 0) & 0xFFC0U) << 16);
|
2011-04-02 07:59:28 +03:00
|
|
|
}
|
|
|
|
write32(dst, value);
|
|
|
|
dst += 4;
|
|
|
|
}
|
|
|
|
src += pic->linesize[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-22 20:31:09 +02:00
|
|
|
static void encode_gbrp10(AVCodecContext *avctx, const AVFrame *pic, uint8_t *dst)
|
2012-08-24 16:24:15 +03:00
|
|
|
{
|
|
|
|
DPXContext *s = avctx->priv_data;
|
|
|
|
const uint8_t *src[3] = {pic->data[0], pic->data[1], pic->data[2]};
|
|
|
|
int x, y, i;
|
|
|
|
|
|
|
|
for (y = 0; y < avctx->height; y++) {
|
|
|
|
for (x = 0; x < avctx->width; x++) {
|
|
|
|
int value;
|
2012-10-12 22:18:50 +03:00
|
|
|
if (s->big_endian) {
|
2012-08-24 16:24:15 +03:00
|
|
|
value = (AV_RB16(src[0] + 2*x) << 12)
|
|
|
|
| (AV_RB16(src[1] + 2*x) << 2)
|
2013-05-15 13:15:21 +03:00
|
|
|
| ((unsigned)AV_RB16(src[2] + 2*x) << 22);
|
2012-08-24 16:24:15 +03:00
|
|
|
} else {
|
|
|
|
value = (AV_RL16(src[0] + 2*x) << 12)
|
|
|
|
| (AV_RL16(src[1] + 2*x) << 2)
|
2013-05-15 13:15:21 +03:00
|
|
|
| ((unsigned)AV_RL16(src[2] + 2*x) << 22);
|
2012-08-24 16:24:15 +03:00
|
|
|
}
|
|
|
|
write32(dst, value);
|
|
|
|
dst += 4;
|
|
|
|
}
|
|
|
|
for (i = 0; i < 3; i++)
|
|
|
|
src[i] += pic->linesize[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-25 01:43:26 +02:00
|
|
|
static void encode_gbrp12(AVCodecContext *avctx, const AVFrame *pic, uint8_t *dst)
|
2012-08-24 16:24:15 +03:00
|
|
|
{
|
2012-10-12 22:18:50 +03:00
|
|
|
DPXContext *s = avctx->priv_data;
|
2012-08-24 16:24:15 +03:00
|
|
|
const uint16_t *src[3] = {(uint16_t*)pic->data[0],
|
|
|
|
(uint16_t*)pic->data[1],
|
|
|
|
(uint16_t*)pic->data[2]};
|
2014-08-13 13:21:52 +03:00
|
|
|
int x, y, i, pad;
|
|
|
|
pad = avctx->width*6;
|
|
|
|
pad = (FFALIGN(pad, 4) - pad) >> 1;
|
2012-08-24 16:24:15 +03:00
|
|
|
for (y = 0; y < avctx->height; y++) {
|
|
|
|
for (x = 0; x < avctx->width; x++) {
|
2012-10-12 22:18:50 +03:00
|
|
|
uint16_t value[3];
|
|
|
|
if (s->big_endian) {
|
|
|
|
value[1] = AV_RB16(src[0] + x) << 4;
|
|
|
|
value[2] = AV_RB16(src[1] + x) << 4;
|
|
|
|
value[0] = AV_RB16(src[2] + x) << 4;
|
|
|
|
} else {
|
|
|
|
value[1] = AV_RL16(src[0] + x) << 4;
|
|
|
|
value[2] = AV_RL16(src[1] + x) << 4;
|
|
|
|
value[0] = AV_RL16(src[2] + x) << 4;
|
|
|
|
}
|
2021-04-25 01:43:26 +02:00
|
|
|
for (i = 0; i < 3; i++, dst += 2)
|
|
|
|
write16(dst, value[i]);
|
2012-08-24 16:24:15 +03:00
|
|
|
}
|
2021-04-25 01:43:26 +02:00
|
|
|
for (i = 0; i < pad; i++, dst += 2)
|
|
|
|
AV_WN16(dst, 0);
|
2012-08-24 16:24:15 +03:00
|
|
|
for (i = 0; i < 3; i++)
|
|
|
|
src[i] += pic->linesize[i]/2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-13 14:00:38 +03:00
|
|
|
static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
|
|
|
|
const AVFrame *frame, int *got_packet)
|
2011-03-26 06:12:35 +02:00
|
|
|
{
|
|
|
|
DPXContext *s = avctx->priv_data;
|
2014-08-13 13:21:52 +03:00
|
|
|
int size, ret, need_align, len;
|
2012-02-13 14:00:38 +03:00
|
|
|
uint8_t *buf;
|
2011-03-26 06:12:35 +02:00
|
|
|
|
|
|
|
#define HEADER_SIZE 1664 /* DPX Generic header */
|
2012-02-13 14:00:38 +03:00
|
|
|
if (s->bits_per_component == 10)
|
|
|
|
size = avctx->height * avctx->width * 4;
|
2014-08-13 13:21:52 +03:00
|
|
|
else if (s->bits_per_component == 12) {
|
|
|
|
// 3 components, 12 bits put on 16 bits
|
|
|
|
len = avctx->width*6;
|
|
|
|
size = FFALIGN(len, 4);
|
|
|
|
need_align = size - len;
|
|
|
|
size *= avctx->height;
|
|
|
|
} else {
|
|
|
|
// N components, M bits
|
|
|
|
len = avctx->width * s->num_components * s->bits_per_component >> 3;
|
|
|
|
size = FFALIGN(len, 4);
|
|
|
|
need_align = size - len;
|
|
|
|
size *= avctx->height;
|
|
|
|
}
|
2021-04-25 01:43:26 +02:00
|
|
|
if ((ret = ff_get_encode_buffer(avctx, pkt, size + HEADER_SIZE, 0)) < 0)
|
2012-02-13 14:00:38 +03:00
|
|
|
return ret;
|
|
|
|
buf = pkt->data;
|
2011-03-26 06:12:35 +02:00
|
|
|
|
|
|
|
memset(buf, 0, HEADER_SIZE);
|
|
|
|
|
|
|
|
/* File information header */
|
|
|
|
write32(buf, MKBETAG('S','D','P','X'));
|
|
|
|
write32(buf + 4, HEADER_SIZE);
|
|
|
|
memcpy (buf + 8, "V1.0", 4);
|
|
|
|
write32(buf + 20, 1); /* new image */
|
|
|
|
write32(buf + 24, HEADER_SIZE);
|
2015-06-29 21:59:37 +02:00
|
|
|
if (!(avctx->flags & AV_CODEC_FLAG_BITEXACT))
|
2012-01-17 03:37:59 +03:00
|
|
|
memcpy (buf + 160, LIBAVCODEC_IDENT, FFMIN(sizeof(LIBAVCODEC_IDENT), 100));
|
2011-03-26 06:12:35 +02:00
|
|
|
write32(buf + 660, 0xFFFFFFFF); /* unencrypted */
|
|
|
|
|
|
|
|
/* Image information header */
|
|
|
|
write16(buf + 768, 0); /* orientation; left to right, top to bottom */
|
|
|
|
write16(buf + 770, 1); /* number of elements */
|
|
|
|
write32(buf + 772, avctx->width);
|
|
|
|
write32(buf + 776, avctx->height);
|
|
|
|
buf[800] = s->descriptor;
|
|
|
|
buf[801] = 2; /* linear transfer */
|
|
|
|
buf[802] = 2; /* linear colorimetric */
|
|
|
|
buf[803] = s->bits_per_component;
|
2012-08-24 16:24:15 +03:00
|
|
|
write16(buf + 804, (s->bits_per_component == 10 || s->bits_per_component == 12) ?
|
|
|
|
1 : 0); /* packing method */
|
2013-03-14 20:35:55 +03:00
|
|
|
write32(buf + 808, HEADER_SIZE); /* data offset */
|
2011-03-26 06:12:35 +02:00
|
|
|
|
|
|
|
/* Image source information header */
|
|
|
|
write32(buf + 1628, avctx->sample_aspect_ratio.num);
|
|
|
|
write32(buf + 1632, avctx->sample_aspect_ratio.den);
|
|
|
|
|
2011-04-02 07:59:28 +03:00
|
|
|
switch(s->bits_per_component) {
|
|
|
|
case 8:
|
|
|
|
case 16:
|
2014-08-13 13:21:52 +03:00
|
|
|
if (need_align) {
|
|
|
|
int j;
|
|
|
|
const uint8_t *src = frame->data[0];
|
|
|
|
uint8_t *dst = pkt->data + HEADER_SIZE;
|
|
|
|
size = (len + need_align) * avctx->height;
|
|
|
|
for (j=0; j<avctx->height; j++) {
|
|
|
|
memcpy(dst, src, len);
|
|
|
|
memset(dst + len, 0, need_align);
|
|
|
|
dst += len + need_align;
|
|
|
|
src += frame->linesize[0];
|
|
|
|
}
|
|
|
|
} else {
|
2015-10-22 20:31:09 +02:00
|
|
|
size = av_image_copy_to_buffer(buf + HEADER_SIZE, pkt->size - HEADER_SIZE,
|
2015-10-24 12:06:22 +02:00
|
|
|
(const uint8_t**)frame->data, frame->linesize,
|
2015-10-22 20:31:09 +02:00
|
|
|
avctx->pix_fmt,
|
|
|
|
avctx->width, avctx->height, 1);
|
2014-08-13 13:21:52 +03:00
|
|
|
}
|
2011-04-02 07:59:44 +03:00
|
|
|
if (size < 0)
|
|
|
|
return size;
|
2011-04-02 07:59:28 +03:00
|
|
|
break;
|
|
|
|
case 10:
|
2012-08-24 16:24:15 +03:00
|
|
|
if (s->planar)
|
2015-10-22 20:31:09 +02:00
|
|
|
encode_gbrp10(avctx, frame, buf + HEADER_SIZE);
|
2012-08-24 16:24:15 +03:00
|
|
|
else
|
2015-10-22 20:31:09 +02:00
|
|
|
encode_rgb48_10bit(avctx, frame, buf + HEADER_SIZE);
|
2012-08-24 16:24:15 +03:00
|
|
|
break;
|
|
|
|
case 12:
|
2021-04-25 01:43:26 +02:00
|
|
|
encode_gbrp12(avctx, frame, buf + HEADER_SIZE);
|
2011-04-02 07:59:28 +03:00
|
|
|
break;
|
|
|
|
default:
|
2011-04-02 20:26:47 +03:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "Unsupported bit depth: %d\n", s->bits_per_component);
|
2011-04-02 07:59:28 +03:00
|
|
|
return -1;
|
|
|
|
}
|
2011-03-26 06:12:35 +02:00
|
|
|
|
|
|
|
size += HEADER_SIZE;
|
|
|
|
|
|
|
|
write32(buf + 16, size); /* file size */
|
2012-02-13 14:00:38 +03:00
|
|
|
|
|
|
|
*got_packet = 1;
|
|
|
|
|
|
|
|
return 0;
|
2011-03-26 06:12:35 +02:00
|
|
|
}
|
|
|
|
|
2022-03-16 22:09:54 +02:00
|
|
|
const FFCodec ff_dpx_encoder = {
|
|
|
|
.p.name = "dpx",
|
|
|
|
.p.long_name = NULL_IF_CONFIG_SMALL("DPX (Digital Picture Exchange) image"),
|
|
|
|
.p.type = AVMEDIA_TYPE_VIDEO,
|
|
|
|
.p.id = AV_CODEC_ID_DPX,
|
|
|
|
.p.capabilities = AV_CODEC_CAP_DR1,
|
2011-03-26 06:12:35 +02:00
|
|
|
.priv_data_size = sizeof(DPXContext),
|
2013-07-27 23:45:48 +03:00
|
|
|
.init = encode_init,
|
2022-03-30 23:28:24 +02:00
|
|
|
FF_CODEC_ENCODE_CB(encode_frame),
|
2022-03-16 22:09:54 +02:00
|
|
|
.p.pix_fmts = (const enum AVPixelFormat[]){
|
2013-10-07 13:10:39 +03:00
|
|
|
AV_PIX_FMT_GRAY8,
|
2013-10-08 14:29:29 +03:00
|
|
|
AV_PIX_FMT_RGB24, AV_PIX_FMT_RGBA, AV_PIX_FMT_ABGR,
|
2013-10-07 13:10:39 +03:00
|
|
|
AV_PIX_FMT_GRAY16LE, AV_PIX_FMT_GRAY16BE,
|
2013-07-27 23:45:48 +03:00
|
|
|
AV_PIX_FMT_RGB48LE, AV_PIX_FMT_RGB48BE,
|
|
|
|
AV_PIX_FMT_RGBA64LE, AV_PIX_FMT_RGBA64BE,
|
|
|
|
AV_PIX_FMT_GBRP10LE, AV_PIX_FMT_GBRP10BE,
|
|
|
|
AV_PIX_FMT_GBRP12LE, AV_PIX_FMT_GBRP12BE,
|
2012-10-06 13:10:34 +03:00
|
|
|
AV_PIX_FMT_NONE},
|
2011-03-26 06:12:35 +02:00
|
|
|
};
|