00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "libavutil/intreadwrite.h"
00023 #include "libavutil/imgutils.h"
00024 #include "avcodec.h"
00025
00026 typedef struct DPXContext {
00027 AVFrame picture;
00028 int big_endian;
00029 int bits_per_component;
00030 int descriptor;
00031 } DPXContext;
00032
00033 static av_cold int encode_init(AVCodecContext *avctx)
00034 {
00035 DPXContext *s = avctx->priv_data;
00036
00037 avctx->coded_frame = &s->picture;
00038 avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
00039 avctx->coded_frame->key_frame = 1;
00040
00041 s->big_endian = 1;
00042 s->bits_per_component = 8;
00043 s->descriptor = 50;
00044
00045 switch (avctx->pix_fmt) {
00046 case PIX_FMT_RGB24:
00047 break;
00048 case PIX_FMT_RGBA:
00049 s->descriptor = 51;
00050 break;
00051 case PIX_FMT_RGB48LE:
00052 s->big_endian = 0;
00053 case PIX_FMT_RGB48BE:
00054 s->bits_per_component = avctx->bits_per_raw_sample ? avctx->bits_per_raw_sample : 16;
00055 break;
00056 case PIX_FMT_RGBA64LE:
00057 s->big_endian = 0;
00058 case PIX_FMT_RGBA64BE:
00059 s->descriptor = 51;
00060 s->bits_per_component = 16;
00061 break;
00062 default:
00063 av_log(avctx, AV_LOG_INFO, "unsupported pixel format\n");
00064 return -1;
00065 }
00066
00067 return 0;
00068 }
00069
00070 #define write16(p, value) \
00071 do { \
00072 if (s->big_endian) AV_WB16(p, value); \
00073 else AV_WL16(p, value); \
00074 } while(0)
00075
00076 #define write32(p, value) \
00077 do { \
00078 if (s->big_endian) AV_WB32(p, value); \
00079 else AV_WL32(p, value); \
00080 } while(0)
00081
00082 static void encode_rgb48_10bit(AVCodecContext *avctx, const AVPicture *pic, uint8_t *dst)
00083 {
00084 DPXContext *s = avctx->priv_data;
00085 const uint8_t *src = pic->data[0];
00086 int x, y;
00087
00088 for (y = 0; y < avctx->height; y++) {
00089 for (x = 0; x < avctx->width; x++) {
00090 int value;
00091 if ((avctx->pix_fmt & 1)) {
00092 value = ((AV_RB16(src + 6*x + 4) & 0xFFC0) >> 4)
00093 | ((AV_RB16(src + 6*x + 2) & 0xFFC0) << 6)
00094 | ((AV_RB16(src + 6*x + 0) & 0xFFC0) << 16);
00095 } else {
00096 value = ((AV_RL16(src + 6*x + 4) & 0xFFC0) >> 4)
00097 | ((AV_RL16(src + 6*x + 2) & 0xFFC0) << 6)
00098 | ((AV_RL16(src + 6*x + 0) & 0xFFC0) << 16);
00099 }
00100 write32(dst, value);
00101 dst += 4;
00102 }
00103 src += pic->linesize[0];
00104 }
00105 }
00106
00107 static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data)
00108 {
00109 DPXContext *s = avctx->priv_data;
00110 int size;
00111
00112 #define HEADER_SIZE 1664
00113 if (buf_size < HEADER_SIZE)
00114 return -1;
00115
00116 memset(buf, 0, HEADER_SIZE);
00117
00118
00119 write32(buf, MKBETAG('S','D','P','X'));
00120 write32(buf + 4, HEADER_SIZE);
00121 memcpy (buf + 8, "V1.0", 4);
00122 write32(buf + 20, 1);
00123 write32(buf + 24, HEADER_SIZE);
00124 if(!(avctx->flags & CODEC_FLAG_BITEXACT)){
00125 memcpy (buf + 160, LIBAVCODEC_IDENT, FFMIN(sizeof(LIBAVCODEC_IDENT), 100));
00126 }
00127 write32(buf + 660, 0xFFFFFFFF);
00128
00129
00130 write16(buf + 768, 0);
00131 write16(buf + 770, 1);
00132 write32(buf + 772, avctx->width);
00133 write32(buf + 776, avctx->height);
00134 buf[800] = s->descriptor;
00135 buf[801] = 2;
00136 buf[802] = 2;
00137 buf[803] = s->bits_per_component;
00138 write16(buf + 804, s->bits_per_component == 10 ? 1 : 0);
00139
00140
00141 write32(buf + 1628, avctx->sample_aspect_ratio.num);
00142 write32(buf + 1632, avctx->sample_aspect_ratio.den);
00143
00144 switch(s->bits_per_component) {
00145 case 8:
00146 case 16:
00147 size = avpicture_layout(data, avctx->pix_fmt,
00148 avctx->width, avctx->height,
00149 buf + HEADER_SIZE, buf_size - HEADER_SIZE);
00150 if (size < 0)
00151 return size;
00152 break;
00153 case 10:
00154 size = avctx->height * avctx->width * 4;
00155 if (buf_size < HEADER_SIZE + size)
00156 return -1;
00157 encode_rgb48_10bit(avctx, data, buf + HEADER_SIZE);
00158 break;
00159 default:
00160 av_log(avctx, AV_LOG_ERROR, "Unsupported bit depth: %d\n", s->bits_per_component);
00161 return -1;
00162 }
00163
00164 size += HEADER_SIZE;
00165
00166 write32(buf + 16, size);
00167 return size;
00168 }
00169
00170 AVCodec ff_dpx_encoder = {
00171 .name = "dpx",
00172 .type = AVMEDIA_TYPE_VIDEO,
00173 .id = CODEC_ID_DPX,
00174 .priv_data_size = sizeof(DPXContext),
00175 .init = encode_init,
00176 .encode = encode_frame,
00177 .pix_fmts = (const enum PixelFormat[]){
00178 PIX_FMT_RGB24,
00179 PIX_FMT_RGBA,
00180 PIX_FMT_RGB48LE,
00181 PIX_FMT_RGB48BE,
00182 PIX_FMT_RGBA64LE,
00183 PIX_FMT_RGBA64BE,
00184 PIX_FMT_NONE},
00185 .long_name = NULL_IF_CONFIG_SMALL("DPX image"),
00186 };