dpxenc.c
Go to the documentation of this file.
1 /*
2  * DPX (.dpx) image encoder
3  * Copyright (c) 2011 Peter Ross <pross@xvid.org>
4  *
5  * This file is part of Libav.
6  *
7  * Libav is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * Libav is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include "libavutil/intreadwrite.h"
23 #include "libavutil/imgutils.h"
24 #include "avcodec.h"
25 
26 typedef struct DPXContext {
31 } DPXContext;
32 
34 {
35  DPXContext *s = avctx->priv_data;
36 
37  avctx->coded_frame = &s->picture;
39  avctx->coded_frame->key_frame = 1;
40 
41  s->big_endian = 1;
42  s->bits_per_component = 8;
43  s->descriptor = 50; /* RGB */
44 
45  switch (avctx->pix_fmt) {
46  case PIX_FMT_RGB24:
47  break;
48  case PIX_FMT_RGBA:
49  s->descriptor = 51; /* RGBA */
50  break;
51  case PIX_FMT_RGB48LE:
52  s->big_endian = 0;
53  case PIX_FMT_RGB48BE:
55  break;
56  default:
57  av_log(avctx, AV_LOG_INFO, "unsupported pixel format\n");
58  return -1;
59  }
60 
61  return 0;
62 }
63 
64 #define write16(p, value) \
65 do { \
66  if (s->big_endian) AV_WB16(p, value); \
67  else AV_WL16(p, value); \
68 } while(0)
69 
70 #define write32(p, value) \
71 do { \
72  if (s->big_endian) AV_WB32(p, value); \
73  else AV_WL32(p, value); \
74 } while(0)
75 
76 static void encode_rgb48_10bit(AVCodecContext *avctx, const AVPicture *pic,
77  uint8_t *dst)
78 {
79  DPXContext *s = avctx->priv_data;
80  const uint8_t *src = pic->data[0];
81  int x, y;
82 
83  for (y = 0; y < avctx->height; y++) {
84  for (x = 0; x < avctx->width; x++) {
85  int value;
86  if ((avctx->pix_fmt & 1)) {
87  value = ((AV_RB16(src + 6*x + 4) & 0xFFC0) >> 4)
88  | ((AV_RB16(src + 6*x + 2) & 0xFFC0) << 6)
89  | ((AV_RB16(src + 6*x + 0) & 0xFFC0) << 16);
90  } else {
91  value = ((AV_RL16(src + 6*x + 4) & 0xFFC0) >> 4)
92  | ((AV_RL16(src + 6*x + 2) & 0xFFC0) << 6)
93  | ((AV_RL16(src + 6*x + 0) & 0xFFC0) << 16);
94  }
95  write32(dst, value);
96  dst += 4;
97  }
98  src += pic->linesize[0];
99  }
100 }
101 
102 static int encode_frame(AVCodecContext *avctx, unsigned char *buf,
103  int buf_size, void *data)
104 {
105  DPXContext *s = avctx->priv_data;
106  int size;
107 
108 #define HEADER_SIZE 1664 /* DPX Generic header */
109  if (buf_size < HEADER_SIZE)
110  return -1;
111 
112  memset(buf, 0, HEADER_SIZE);
113 
114  /* File information header */
115  write32(buf, MKBETAG('S','D','P','X'));
116  write32(buf + 4, HEADER_SIZE);
117  memcpy (buf + 8, "V1.0", 4);
118  write32(buf + 20, 1); /* new image */
119  write32(buf + 24, HEADER_SIZE);
120  memcpy (buf + 160, LIBAVCODEC_IDENT, FFMIN(sizeof(LIBAVCODEC_IDENT), 100));
121  write32(buf + 660, 0xFFFFFFFF); /* unencrypted */
122 
123  /* Image information header */
124  write16(buf + 768, 0); /* orientation; left to right, top to bottom */
125  write16(buf + 770, 1); /* number of elements */
126  write32(buf + 772, avctx->width);
127  write32(buf + 776, avctx->height);
128  buf[800] = s->descriptor;
129  buf[801] = 2; /* linear transfer */
130  buf[802] = 2; /* linear colorimetric */
131  buf[803] = s->bits_per_component;
132  write16(buf + 804, s->bits_per_component == 10 ? 1 : 0); /* packing method */
133 
134  /* Image source information header */
135  write32(buf + 1628, avctx->sample_aspect_ratio.num);
136  write32(buf + 1632, avctx->sample_aspect_ratio.den);
137 
138  switch (s->bits_per_component) {
139  case 8:
140  case 16:
141  size = avpicture_layout(data, avctx->pix_fmt,
142  avctx->width, avctx->height,
143  buf + HEADER_SIZE, buf_size - HEADER_SIZE);
144  if (size < 0)
145  return size;
146  break;
147  case 10:
148  size = avctx->height * avctx->width * 4;
149  if (buf_size < HEADER_SIZE + size)
150  return -1;
151  encode_rgb48_10bit(avctx, data, buf + HEADER_SIZE);
152  break;
153  default:
154  av_log(avctx, AV_LOG_ERROR, "Unsupported bit depth: %d\n", s->bits_per_component);
155  return -1;
156  }
157 
158  size += HEADER_SIZE;
159 
160  write32(buf + 16, size); /* file size */
161  return size;
162 }
163 
165  .name = "dpx",
166  .type = AVMEDIA_TYPE_VIDEO,
167  .id = CODEC_ID_DPX,
168  .priv_data_size = sizeof(DPXContext),
169  .init = encode_init,
170  .encode = encode_frame,
171  .pix_fmts = (const enum PixelFormat[]){
173  PIX_FMT_RGBA,
176  PIX_FMT_NONE},
177  .long_name = NULL_IF_CONFIG_SMALL("DPX image"),
178 };
enum PixelFormat pix_fmt
Pixel format, see PIX_FMT_xxx.
Definition: avcodec.h:1426
#define write32(p, value)
Definition: dpxenc.c:70
AV_WL32 AV_WL24 AV_WL16 AV_WB32 AV_WB24 AV_RB16
Definition: bytestream.h:89
int size
int linesize[AV_NUM_DATA_POINTERS]
number of bytes per line
Definition: avcodec.h:3369
Audio Video Frame.
Definition: avcodec.h:985
int big_endian
Definition: dpxenc.c:28
misc image utilities
AVFrame * coded_frame
the picture in the bitstream
Definition: avcodec.h:2000
struct DPXContext DPXContext
static av_cold int encode_init(AVCodecContext *avctx)
Definition: dpxenc.c:33
int num
numerator
Definition: rational.h:44
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown) That is the width of a pixel divided by the height of the pixel...
Definition: avcodec.h:1993
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:2863
four components are given, that's all.
Definition: avcodec.h:3367
AVCodec.
Definition: avcodec.h:3189
#define av_cold
Definition: attributes.h:71
uint8_t * data[AV_NUM_DATA_POINTERS]
Definition: avcodec.h:3368
const char data[16]
Definition: mxf.c:60
packed RGB 16:16:16, 48bpp, 16R, 16G, 16B, the 2-byte value for each R/G/B component is stored as lit...
Definition: pixfmt.h:108
static int init(AVCodecParserContext *s)
Definition: h264_parser.c:336
Definition: dpx.c:27
#define HEADER_SIZE
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:191
packed RGB 16:16:16, 48bpp, 16R, 16G, 16B, the 2-byte value for each R/G/B component is stored as big...
Definition: pixfmt.h:107
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:140
const char * name
Name of the codec implementation.
Definition: avcodec.h:3196
int bits_per_component
Definition: dpxenc.c:29
enum AVPictureType pict_type
Picture type of the frame, see ?_TYPE below.
Definition: avcodec.h:1029
#define FFMIN(a, b)
Definition: common.h:55
int width
picture width / height.
Definition: avcodec.h:1408
AVCodec ff_dpx_encoder
Definition: dpxenc.c:164
int descriptor
Definition: dpxenc.c:30
external API header
main external API structure.
Definition: avcodec.h:1329
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:111
static void encode_rgb48_10bit(AVCodecContext *avctx, const AVPicture *pic, uint8_t *dst)
Definition: dpxenc.c:76
static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data)
Definition: dpxenc.c:102
PixelFormat
Pixel format.
Definition: pixfmt.h:62
AV_WL32 AV_WL24 AV_RL16
Definition: bytestream.h:89
#define write16(p, value)
Definition: dpxenc.c:64
int den
denominator
Definition: rational.h:45
#define MKBETAG(a, b, c, d)
Definition: common.h:232
void * priv_data
Definition: avcodec.h:1531
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:66
int key_frame
1 -> keyframe, 0-> not
Definition: avcodec.h:1022
AVFrame picture
Definition: dpx.c:28
#define LIBAVCODEC_IDENT
Definition: version.h:35
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:93
#define AV_LOG_INFO
Definition: log.h:119
int avpicture_layout(const AVPicture *src, enum PixelFormat pix_fmt, int width, int height, unsigned char *dest, int dest_size)
Copy pixel data from an AVPicture into a buffer.
Definition: imgconvert.c:443