FFmpeg
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 FFmpeg.
6  *
7  * FFmpeg 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  * FFmpeg 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 FFmpeg; 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/common.h"
23 #include "libavutil/intreadwrite.h"
24 #include "libavutil/imgutils.h"
25 #include "avcodec.h"
26 #include "codec_internal.h"
27 #include "encode.h"
28 #include "version.h"
29 
30 typedef struct DPXContext {
35  int planar;
36 } DPXContext;
37 
39 {
40  DPXContext *s = avctx->priv_data;
42 
43  s->big_endian = !!(desc->flags & AV_PIX_FMT_FLAG_BE);
44  s->bits_per_component = desc->comp[0].depth;
45  s->num_components = desc->nb_components;
46  s->descriptor = (desc->flags & AV_PIX_FMT_FLAG_ALPHA) ? 51 : 50;
47  s->planar = !!(desc->flags & AV_PIX_FMT_FLAG_PLANAR);
48 
49  switch (avctx->pix_fmt) {
50  case AV_PIX_FMT_ABGR:
51  s->descriptor = 52;
52  break;
55  case AV_PIX_FMT_GRAY8:
56  s->descriptor = 6;
57  break;
62  case AV_PIX_FMT_RGB24:
65  case AV_PIX_FMT_RGBA:
66  break;
67  case AV_PIX_FMT_RGB48LE:
68  case AV_PIX_FMT_RGB48BE:
69  if (avctx->bits_per_raw_sample)
70  s->bits_per_component = avctx->bits_per_raw_sample;
71  break;
72  }
73 
74  return 0;
75 }
76 
77 static av_always_inline void write16_internal(int big_endian, void *p, int value)
78 {
79  if (big_endian) AV_WB16(p, value);
80  else AV_WL16(p, value);
81 }
82 
83 static av_always_inline void write32_internal(int big_endian, void *p, int value)
84 {
85  if (big_endian) AV_WB32(p, value);
86  else AV_WL32(p, value);
87 }
88 
89 #define write16(p, value) write16_internal(s->big_endian, p, value)
90 #define write32(p, value) write32_internal(s->big_endian, p, value)
91 
92 static void encode_rgb48_10bit(AVCodecContext *avctx, const AVFrame *pic,
93  uint8_t *dst)
94 {
95  DPXContext *s = avctx->priv_data;
96  const uint8_t *src = pic->data[0];
97  int x, y;
98 
99  for (y = 0; y < avctx->height; y++) {
100  for (x = 0; x < avctx->width; x++) {
101  int value;
102  if (s->big_endian) {
103  value = ((AV_RB16(src + 6*x + 4) & 0xFFC0U) >> 4)
104  | ((AV_RB16(src + 6*x + 2) & 0xFFC0U) << 6)
105  | ((AV_RB16(src + 6*x + 0) & 0xFFC0U) << 16);
106  } else {
107  value = ((AV_RL16(src + 6*x + 4) & 0xFFC0U) >> 4)
108  | ((AV_RL16(src + 6*x + 2) & 0xFFC0U) << 6)
109  | ((AV_RL16(src + 6*x + 0) & 0xFFC0U) << 16);
110  }
111  write32(dst, value);
112  dst += 4;
113  }
114  src += pic->linesize[0];
115  }
116 }
117 
118 static void encode_gbrp10(AVCodecContext *avctx, const AVFrame *pic, uint8_t *dst)
119 {
120  DPXContext *s = avctx->priv_data;
121  const uint8_t *src[3] = {pic->data[0], pic->data[1], pic->data[2]};
122  int x, y, i;
123 
124  for (y = 0; y < avctx->height; y++) {
125  for (x = 0; x < avctx->width; x++) {
126  int value;
127  if (s->big_endian) {
128  value = (AV_RB16(src[0] + 2*x) << 12)
129  | (AV_RB16(src[1] + 2*x) << 2)
130  | ((unsigned)AV_RB16(src[2] + 2*x) << 22);
131  } else {
132  value = (AV_RL16(src[0] + 2*x) << 12)
133  | (AV_RL16(src[1] + 2*x) << 2)
134  | ((unsigned)AV_RL16(src[2] + 2*x) << 22);
135  }
136  write32(dst, value);
137  dst += 4;
138  }
139  for (i = 0; i < 3; i++)
140  src[i] += pic->linesize[i];
141  }
142 }
143 
144 static void encode_gbrp12(AVCodecContext *avctx, const AVFrame *pic, uint8_t *dst)
145 {
146  DPXContext *s = avctx->priv_data;
147  const uint16_t *src[3] = {(uint16_t*)pic->data[0],
148  (uint16_t*)pic->data[1],
149  (uint16_t*)pic->data[2]};
150  int x, y, i, pad;
151  pad = avctx->width*6;
152  pad = (FFALIGN(pad, 4) - pad) >> 1;
153  for (y = 0; y < avctx->height; y++) {
154  for (x = 0; x < avctx->width; x++) {
155  uint16_t value[3];
156  if (s->big_endian) {
157  value[1] = AV_RB16(src[0] + x) << 4;
158  value[2] = AV_RB16(src[1] + x) << 4;
159  value[0] = AV_RB16(src[2] + x) << 4;
160  } else {
161  value[1] = AV_RL16(src[0] + x) << 4;
162  value[2] = AV_RL16(src[1] + x) << 4;
163  value[0] = AV_RL16(src[2] + x) << 4;
164  }
165  for (i = 0; i < 3; i++, dst += 2)
166  write16(dst, value[i]);
167  }
168  for (i = 0; i < pad; i++, dst += 2)
169  AV_WN16(dst, 0);
170  for (i = 0; i < 3; i++)
171  src[i] += pic->linesize[i]/2;
172  }
173 }
174 
176  const AVFrame *frame, int *got_packet)
177 {
178  DPXContext *s = avctx->priv_data;
179  int size, ret, need_align, len;
180  uint8_t *buf;
181 
182 #define HEADER_SIZE 1664 /* DPX Generic header */
183  if (s->bits_per_component == 10)
184  size = avctx->height * avctx->width * 4;
185  else if (s->bits_per_component == 12) {
186  // 3 components, 12 bits put on 16 bits
187  len = avctx->width*6;
188  size = FFALIGN(len, 4);
189  need_align = size - len;
190  size *= avctx->height;
191  } else {
192  // N components, M bits
193  len = avctx->width * s->num_components * s->bits_per_component >> 3;
194  size = FFALIGN(len, 4);
195  need_align = size - len;
196  size *= avctx->height;
197  }
198  if ((ret = ff_get_encode_buffer(avctx, pkt, size + HEADER_SIZE, 0)) < 0)
199  return ret;
200  buf = pkt->data;
201 
202  memset(buf, 0, HEADER_SIZE);
203 
204  /* File information header */
205  write32(buf, MKBETAG('S','D','P','X'));
206  write32(buf + 4, HEADER_SIZE);
207  memcpy (buf + 8, "V1.0", 4);
208  write32(buf + 20, 1); /* new image */
209  write32(buf + 24, HEADER_SIZE);
210  if (!(avctx->flags & AV_CODEC_FLAG_BITEXACT))
211  memcpy (buf + 160, LIBAVCODEC_IDENT, FFMIN(sizeof(LIBAVCODEC_IDENT), 100));
212  write32(buf + 660, 0xFFFFFFFF); /* unencrypted */
213 
214  /* Image information header */
215  write16(buf + 768, 0); /* orientation; left to right, top to bottom */
216  write16(buf + 770, 1); /* number of elements */
217  write32(buf + 772, avctx->width);
218  write32(buf + 776, avctx->height);
219  buf[800] = s->descriptor;
220  buf[801] = 2; /* linear transfer */
221  buf[802] = 2; /* linear colorimetric */
222  buf[803] = s->bits_per_component;
223  write16(buf + 804, (s->bits_per_component == 10 || s->bits_per_component == 12) ?
224  1 : 0); /* packing method */
225  write32(buf + 808, HEADER_SIZE); /* data offset */
226 
227  /* Image source information header */
228  write32(buf + 1628, avctx->sample_aspect_ratio.num);
229  write32(buf + 1632, avctx->sample_aspect_ratio.den);
230 
231  switch(s->bits_per_component) {
232  case 8:
233  case 16:
234  if (need_align) {
235  int j;
236  const uint8_t *src = frame->data[0];
237  uint8_t *dst = pkt->data + HEADER_SIZE;
238  size = (len + need_align) * avctx->height;
239  for (j=0; j<avctx->height; j++) {
240  memcpy(dst, src, len);
241  memset(dst + len, 0, need_align);
242  dst += len + need_align;
243  src += frame->linesize[0];
244  }
245  } else {
247  (const uint8_t**)frame->data, frame->linesize,
248  avctx->pix_fmt,
249  avctx->width, avctx->height, 1);
250  }
251  if (size < 0)
252  return size;
253  break;
254  case 10:
255  if (s->planar)
256  encode_gbrp10(avctx, frame, buf + HEADER_SIZE);
257  else
258  encode_rgb48_10bit(avctx, frame, buf + HEADER_SIZE);
259  break;
260  case 12:
261  encode_gbrp12(avctx, frame, buf + HEADER_SIZE);
262  break;
263  default:
264  av_log(avctx, AV_LOG_ERROR, "Unsupported bit depth: %d\n", s->bits_per_component);
265  return -1;
266  }
267 
268  size += HEADER_SIZE;
269 
270  write32(buf + 16, size); /* file size */
271 
272  *got_packet = 1;
273 
274  return 0;
275 }
276 
278  .p.name = "dpx",
279  CODEC_LONG_NAME("DPX (Digital Picture Exchange) image"),
280  .p.type = AVMEDIA_TYPE_VIDEO,
281  .p.id = AV_CODEC_ID_DPX,
282  .p.capabilities = AV_CODEC_CAP_DR1,
283  .priv_data_size = sizeof(DPXContext),
284  .init = encode_init,
286  .p.pix_fmts = (const enum AVPixelFormat[]){
295 };
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:71
AV_CODEC_ID_DPX
@ AV_CODEC_ID_DPX
Definition: codec_id.h:180
LIBAVCODEC_IDENT
#define LIBAVCODEC_IDENT
Definition: version.h:43
AV_WL32
#define AV_WL32(p, v)
Definition: intreadwrite.h:424
AV_PIX_FMT_GBRP10BE
@ AV_PIX_FMT_GBRP10BE
planar GBR 4:4:4 30bpp, big-endian
Definition: pixfmt.h:169
av_pix_fmt_desc_get
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2962
write16
#define write16(p, value)
Definition: dpxenc.c:89
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:344
AV_PIX_FMT_RGBA64BE
@ AV_PIX_FMT_RGBA64BE
packed RGBA 16:16:16:16, 64bpp, 16R, 16G, 16B, 16A, the 2-byte value for each R/G/B/A component is st...
Definition: pixfmt.h:202
AVPacket::data
uint8_t * data
Definition: packet.h:522
DPXContext::descriptor
int descriptor
Definition: dpxenc.c:34
encode.h
FFCodec
Definition: codec_internal.h:127
version.h
write16_internal
static av_always_inline void write16_internal(int big_endian, void *p, int value)
Definition: dpxenc.c:77
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:365
DPXContext::bits_per_component
int bits_per_component
Definition: dpxenc.c:32
encode_frame
static int encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet)
Definition: dpxenc.c:175
AV_PIX_FMT_GRAY16BE
@ AV_PIX_FMT_GRAY16BE
Y , 16bpp, big-endian.
Definition: pixfmt.h:104
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
ff_dpx_encoder
const FFCodec ff_dpx_encoder
Definition: dpxenc.c:277
AVCodecContext::flags
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:502
FF_CODEC_ENCODE_CB
#define FF_CODEC_ENCODE_CB(func)
Definition: codec_internal.h:296
AVRational::num
int num
Numerator.
Definition: rational.h:59
pkt
AVPacket * pkt
Definition: movenc.c:59
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
av_cold
#define av_cold
Definition: attributes.h:90
write32_internal
static av_always_inline void write32_internal(int big_endian, void *p, int value)
Definition: dpxenc.c:83
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:198
AV_PIX_FMT_GBRP12LE
@ AV_PIX_FMT_GBRP12LE
planar GBR 4:4:4 36bpp, little-endian
Definition: pixfmt.h:280
AVCodecContext::bits_per_raw_sample
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:1574
AV_PIX_FMT_FLAG_ALPHA
#define AV_PIX_FMT_FLAG_ALPHA
The pixel format has an alpha channel.
Definition: pixdesc.h:147
AV_RL16
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_RL16
Definition: bytestream.h:94
write32
#define write32(p, value)
Definition: dpxenc.c:90
AV_PIX_FMT_GBRP10LE
@ AV_PIX_FMT_GBRP10LE
planar GBR 4:4:4 30bpp, little-endian
Definition: pixfmt.h:170
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:272
AV_PIX_FMT_RGBA
@ AV_PIX_FMT_RGBA
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:100
frame
static AVFrame * frame
Definition: demux_decode.c:54
AV_WB16
#define AV_WB16(p, v)
Definition: intreadwrite.h:403
DPXContext::planar
int planar
Definition: dpxenc.c:35
AV_PIX_FMT_RGB48LE
@ AV_PIX_FMT_RGB48LE
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:110
AV_PIX_FMT_RGBA64LE
@ AV_PIX_FMT_RGBA64LE
packed RGBA 16:16:16:16, 64bpp, 16R, 16G, 16B, 16A, the 2-byte value for each R/G/B/A component is st...
Definition: pixfmt.h:203
AV_PIX_FMT_GRAY8
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition: pixfmt.h:81
AV_PIX_FMT_ABGR
@ AV_PIX_FMT_ABGR
packed ABGR 8:8:8:8, 32bpp, ABGRABGR...
Definition: pixfmt.h:101
DPXContext::num_components
int num_components
Definition: dpxenc.c:33
AV_WB32
#define AV_WB32(p, v)
Definition: intreadwrite.h:417
DPXContext
Definition: dpxenc.c:30
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts.c:365
AV_PIX_FMT_RGB24
@ AV_PIX_FMT_RGB24
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:75
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
AVPacket::size
int size
Definition: packet.h:523
codec_internal.h
encode_gbrp12
static void encode_gbrp12(AVCodecContext *avctx, const AVFrame *pic, uint8_t *dst)
Definition: dpxenc.c:144
for
for(k=2;k<=8;++k)
Definition: h264pred_template.c:425
size
int size
Definition: twinvq_data.h:10344
MKBETAG
#define MKBETAG(a, b, c, d)
Definition: macros.h:56
AV_WL16
#define AV_WL16(p, v)
Definition: intreadwrite.h:410
HEADER_SIZE
#define HEADER_SIZE
AV_PIX_FMT_RGB48BE
@ AV_PIX_FMT_RGB48BE
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:109
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:255
common.h
av_always_inline
#define av_always_inline
Definition: attributes.h:49
value
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default value
Definition: writing_filters.txt:86
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
AV_PIX_FMT_FLAG_BE
#define AV_PIX_FMT_FLAG_BE
Pixel format is big-endian.
Definition: pixdesc.h:116
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:194
len
int len
Definition: vorbis_enc_data.h:426
AVCodecContext::height
int height
Definition: avcodec.h:618
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:657
avcodec.h
ret
ret
Definition: filter_design.txt:187
AV_PIX_FMT_GBRP12BE
@ AV_PIX_FMT_GBRP12BE
planar GBR 4:4:4 36bpp, big-endian
Definition: pixfmt.h:279
U
#define U(x)
Definition: vpx_arith.h:37
AVCodecContext
main external API structure.
Definition: avcodec.h:445
encode_gbrp10
static void encode_gbrp10(AVCodecContext *avctx, const AVFrame *pic, uint8_t *dst)
Definition: dpxenc.c:118
encode_rgb48_10bit
static void encode_rgb48_10bit(AVCodecContext *avctx, const AVFrame *pic, uint8_t *dst)
Definition: dpxenc.c:92
ff_get_encode_buffer
int ff_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int flags)
Get a buffer for a packet.
Definition: encode.c:105
AVRational::den
int den
Denominator.
Definition: rational.h:60
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:72
AV_PIX_FMT_FLAG_PLANAR
#define AV_PIX_FMT_FLAG_PLANAR
At least one pixel component is not in the first data plane.
Definition: pixdesc.h:132
encode_init
static av_cold int encode_init(AVCodecContext *avctx)
Definition: dpxenc.c:38
desc
const char * desc
Definition: libsvtav1.c:75
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
AV_PIX_FMT_GRAY16LE
@ AV_PIX_FMT_GRAY16LE
Y , 16bpp, little-endian.
Definition: pixfmt.h:105
AV_CODEC_FLAG_BITEXACT
#define AV_CODEC_FLAG_BITEXACT
Use only bitexact stuff (except (I)DCT).
Definition: avcodec.h:342
AVPixFmtDescriptor
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:69
av_image_copy_to_buffer
int av_image_copy_to_buffer(uint8_t *dst, int dst_size, const uint8_t *const src_data[4], const int src_linesize[4], enum AVPixelFormat pix_fmt, int width, int height, int align)
Copy image data from an image into a buffer.
Definition: imgutils.c:501
FFALIGN
#define FFALIGN(x, a)
Definition: macros.h:78
AVPacket
This structure stores compressed data.
Definition: packet.h:499
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:472
src
INIT_CLIP pixel * src
Definition: h264pred_template.c:418
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:618
imgutils.h
AVFrame::linesize
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
Definition: frame.h:389
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVCodecContext::sample_aspect_ratio
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:642
DPXContext::big_endian
int big_endian
Definition: dpxenc.c:31
AV_RB16
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_WB24 unsigned int_TMPL AV_RB16
Definition: bytestream.h:98
AV_WN16
#define AV_WN16(p, v)
Definition: intreadwrite.h:370