00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "libavutil/common.h"
00023 #include "libavutil/intreadwrite.h"
00024 #include "libavutil/imgutils.h"
00025 #include "avcodec.h"
00026 #include "internal.h"
00027
00028 typedef struct DPXContext {
00029 AVFrame picture;
00030 int big_endian;
00031 int bits_per_component;
00032 int descriptor;
00033 int planar;
00034 } DPXContext;
00035
00036 static av_cold int encode_init(AVCodecContext *avctx)
00037 {
00038 DPXContext *s = avctx->priv_data;
00039
00040 avctx->coded_frame = &s->picture;
00041 avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
00042 avctx->coded_frame->key_frame = 1;
00043
00044 s->big_endian = 1;
00045 s->bits_per_component = 8;
00046 s->descriptor = 50;
00047 s->planar = 0;
00048
00049 switch (avctx->pix_fmt) {
00050 case PIX_FMT_RGB24:
00051 break;
00052 case PIX_FMT_RGBA:
00053 s->descriptor = 51;
00054 break;
00055 case PIX_FMT_RGB48LE:
00056 s->big_endian = 0;
00057 case PIX_FMT_RGB48BE:
00058 s->bits_per_component = avctx->bits_per_raw_sample ? avctx->bits_per_raw_sample : 16;
00059 break;
00060 case PIX_FMT_RGBA64LE:
00061 s->big_endian = 0;
00062 case PIX_FMT_RGBA64BE:
00063 s->descriptor = 51;
00064 s->bits_per_component = 16;
00065 break;
00066 case PIX_FMT_GBRP10LE:
00067 s->big_endian = 0;
00068 case PIX_FMT_GBRP10BE:
00069 s->bits_per_component = 10;
00070 s->planar = 1;
00071 break;
00072 case PIX_FMT_GBRP12LE:
00073 s->big_endian = 0;
00074 case PIX_FMT_GBRP12BE:
00075 s->bits_per_component = 12;
00076 s->planar = 1;
00077 break;
00078 default:
00079 av_log(avctx, AV_LOG_INFO, "unsupported pixel format\n");
00080 return -1;
00081 }
00082
00083 return 0;
00084 }
00085
00086 #define write16(p, value) \
00087 do { \
00088 if (s->big_endian) AV_WB16(p, value); \
00089 else AV_WL16(p, value); \
00090 } while(0)
00091
00092 #define write32(p, value) \
00093 do { \
00094 if (s->big_endian) AV_WB32(p, value); \
00095 else AV_WL32(p, value); \
00096 } while(0)
00097
00098 static void encode_rgb48_10bit(AVCodecContext *avctx, const AVPicture *pic, uint8_t *dst)
00099 {
00100 DPXContext *s = avctx->priv_data;
00101 const uint8_t *src = pic->data[0];
00102 int x, y;
00103
00104 for (y = 0; y < avctx->height; y++) {
00105 for (x = 0; x < avctx->width; x++) {
00106 int value;
00107 if ((avctx->pix_fmt & 1)) {
00108 value = ((AV_RB16(src + 6*x + 4) & 0xFFC0) >> 4)
00109 | ((AV_RB16(src + 6*x + 2) & 0xFFC0) << 6)
00110 | ((AV_RB16(src + 6*x + 0) & 0xFFC0) << 16);
00111 } else {
00112 value = ((AV_RL16(src + 6*x + 4) & 0xFFC0) >> 4)
00113 | ((AV_RL16(src + 6*x + 2) & 0xFFC0) << 6)
00114 | ((AV_RL16(src + 6*x + 0) & 0xFFC0) << 16);
00115 }
00116 write32(dst, value);
00117 dst += 4;
00118 }
00119 src += pic->linesize[0];
00120 }
00121 }
00122
00123 static void encode_gbrp10(AVCodecContext *avctx, const AVPicture *pic, uint8_t *dst)
00124 {
00125 DPXContext *s = avctx->priv_data;
00126 const uint8_t *src[3] = {pic->data[0], pic->data[1], pic->data[2]};
00127 int x, y, i;
00128
00129 for (y = 0; y < avctx->height; y++) {
00130 for (x = 0; x < avctx->width; x++) {
00131 int value;
00132 if ((avctx->pix_fmt & 1)) {
00133 value = (AV_RB16(src[0] + 2*x) << 12)
00134 | (AV_RB16(src[1] + 2*x) << 2)
00135 | (AV_RB16(src[2] + 2*x) << 22);
00136 } else {
00137 value = (AV_RL16(src[0] + 2*x) << 12)
00138 | (AV_RL16(src[1] + 2*x) << 2)
00139 | (AV_RL16(src[2] + 2*x) << 22);
00140 }
00141 write32(dst, value);
00142 dst += 4;
00143 }
00144 for (i = 0; i < 3; i++)
00145 src[i] += pic->linesize[i];
00146 }
00147 }
00148
00149 static void encode_gbrp12(AVCodecContext *avctx, const AVPicture *pic, uint16_t *dst)
00150 {
00151 const uint16_t *src[3] = {(uint16_t*)pic->data[0],
00152 (uint16_t*)pic->data[1],
00153 (uint16_t*)pic->data[2]};
00154 int x, y, i;
00155 for (y = 0; y < avctx->height; y++) {
00156 for (x = 0; x < avctx->width; x++) {
00157 for (i = 0; i < 3; i++)
00158 *dst++ = *(src[i] + x);
00159 }
00160 for (i = 0; i < 3; i++)
00161 src[i] += pic->linesize[i]/2;
00162 }
00163 }
00164
00165 static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
00166 const AVFrame *frame, int *got_packet)
00167 {
00168 DPXContext *s = avctx->priv_data;
00169 int size, ret;
00170 uint8_t *buf;
00171
00172 #define HEADER_SIZE 1664
00173 if (s->bits_per_component == 10)
00174 size = avctx->height * avctx->width * 4;
00175 else
00176 size = avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height);
00177 if ((ret = ff_alloc_packet2(avctx, pkt, size + HEADER_SIZE)) < 0)
00178 return ret;
00179 buf = pkt->data;
00180
00181 memset(buf, 0, HEADER_SIZE);
00182
00183
00184 write32(buf, MKBETAG('S','D','P','X'));
00185 write32(buf + 4, HEADER_SIZE);
00186 memcpy (buf + 8, "V1.0", 4);
00187 write32(buf + 20, 1);
00188 write32(buf + 24, HEADER_SIZE);
00189 if (!(avctx->flags & CODEC_FLAG_BITEXACT))
00190 memcpy (buf + 160, LIBAVCODEC_IDENT, FFMIN(sizeof(LIBAVCODEC_IDENT), 100));
00191 write32(buf + 660, 0xFFFFFFFF);
00192
00193
00194 write16(buf + 768, 0);
00195 write16(buf + 770, 1);
00196 write32(buf + 772, avctx->width);
00197 write32(buf + 776, avctx->height);
00198 buf[800] = s->descriptor;
00199 buf[801] = 2;
00200 buf[802] = 2;
00201 buf[803] = s->bits_per_component;
00202 write16(buf + 804, (s->bits_per_component == 10 || s->bits_per_component == 12) ?
00203 1 : 0);
00204
00205
00206 write32(buf + 1628, avctx->sample_aspect_ratio.num);
00207 write32(buf + 1632, avctx->sample_aspect_ratio.den);
00208
00209 switch(s->bits_per_component) {
00210 case 8:
00211 case 16:
00212 size = avpicture_layout((const AVPicture*)frame, avctx->pix_fmt,
00213 avctx->width, avctx->height,
00214 buf + HEADER_SIZE, pkt->size - HEADER_SIZE);
00215 if (size < 0)
00216 return size;
00217 break;
00218 case 10:
00219 if (s->planar)
00220 encode_gbrp10(avctx, (const AVPicture*)frame, buf + HEADER_SIZE);
00221 else
00222 encode_rgb48_10bit(avctx, (const AVPicture*)frame, buf + HEADER_SIZE);
00223 break;
00224 case 12:
00225 encode_gbrp12(avctx, (const AVPicture*)frame, (uint16_t*)(buf + HEADER_SIZE));
00226 break;
00227 default:
00228 av_log(avctx, AV_LOG_ERROR, "Unsupported bit depth: %d\n", s->bits_per_component);
00229 return -1;
00230 }
00231
00232 size += HEADER_SIZE;
00233
00234 write32(buf + 16, size);
00235
00236 pkt->flags |= AV_PKT_FLAG_KEY;
00237 *got_packet = 1;
00238
00239 return 0;
00240 }
00241
00242 AVCodec ff_dpx_encoder = {
00243 .name = "dpx",
00244 .type = AVMEDIA_TYPE_VIDEO,
00245 .id = AV_CODEC_ID_DPX,
00246 .priv_data_size = sizeof(DPXContext),
00247 .init = encode_init,
00248 .encode2 = encode_frame,
00249 .pix_fmts = (const enum PixelFormat[]){
00250 PIX_FMT_RGB24,
00251 PIX_FMT_RGBA,
00252 PIX_FMT_RGB48LE,
00253 PIX_FMT_RGB48BE,
00254 PIX_FMT_RGBA64LE,
00255 PIX_FMT_RGBA64BE,
00256 PIX_FMT_GBRP10LE,
00257 PIX_FMT_GBRP10BE,
00258 PIX_FMT_GBRP12LE,
00259 PIX_FMT_GBRP12BE,
00260 PIX_FMT_NONE},
00261 .long_name = NULL_IF_CONFIG_SMALL("DPX image"),
00262 };