FFmpeg  4.4.9
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 "internal.h"
27 
28 typedef struct DPXContext {
33  int planar;
34 } DPXContext;
35 
37 {
38  DPXContext *s = avctx->priv_data;
40 
41  s->big_endian = !!(desc->flags & AV_PIX_FMT_FLAG_BE);
42  s->bits_per_component = desc->comp[0].depth;
43  s->num_components = desc->nb_components;
44  s->descriptor = (desc->flags & AV_PIX_FMT_FLAG_ALPHA) ? 51 : 50;
45  s->planar = !!(desc->flags & AV_PIX_FMT_FLAG_PLANAR);
46 
47  switch (avctx->pix_fmt) {
48  case AV_PIX_FMT_ABGR:
49  s->descriptor = 52;
50  break;
53  case AV_PIX_FMT_GRAY8:
54  s->descriptor = 6;
55  break;
60  case AV_PIX_FMT_RGB24:
63  case AV_PIX_FMT_RGBA:
64  break;
65  case AV_PIX_FMT_RGB48LE:
66  case AV_PIX_FMT_RGB48BE:
67  if (avctx->bits_per_raw_sample == 10) {
68  s->bits_per_component = 10;
69  } else if (avctx->bits_per_raw_sample && avctx->bits_per_raw_sample != 16) {
70  av_log(avctx, AV_LOG_ERROR, "Unsupported bit depth: %d\n", avctx->bits_per_raw_sample);
71  return AVERROR(EINVAL);
72  }
73  break;
74  default:
75  av_log(avctx, AV_LOG_INFO, "unsupported pixel format\n");
76  return -1;
77  }
78 
79  return 0;
80 }
81 
82 static av_always_inline void write16_internal(int big_endian, void *p, int value)
83 {
84  if (big_endian) AV_WB16(p, value);
85  else AV_WL16(p, value);
86 }
87 
88 static av_always_inline void write32_internal(int big_endian, void *p, int value)
89 {
90  if (big_endian) AV_WB32(p, value);
91  else AV_WL32(p, value);
92 }
93 
94 #define write16(p, value) write16_internal(s->big_endian, p, value)
95 #define write32(p, value) write32_internal(s->big_endian, p, value)
96 
97 static void encode_rgb48_10bit(AVCodecContext *avctx, const AVFrame *pic,
98  uint8_t *dst)
99 {
100  DPXContext *s = avctx->priv_data;
101  const uint8_t *src = pic->data[0];
102  int x, y;
103 
104  for (y = 0; y < avctx->height; y++) {
105  for (x = 0; x < avctx->width; x++) {
106  int value;
107  if (s->big_endian) {
108  value = ((AV_RB16(src + 6*x + 4) & 0xFFC0U) >> 4)
109  | ((AV_RB16(src + 6*x + 2) & 0xFFC0U) << 6)
110  | ((AV_RB16(src + 6*x + 0) & 0xFFC0U) << 16);
111  } else {
112  value = ((AV_RL16(src + 6*x + 4) & 0xFFC0U) >> 4)
113  | ((AV_RL16(src + 6*x + 2) & 0xFFC0U) << 6)
114  | ((AV_RL16(src + 6*x + 0) & 0xFFC0U) << 16);
115  }
116  write32(dst, value);
117  dst += 4;
118  }
119  src += pic->linesize[0];
120  }
121 }
122 
123 static void encode_gbrp10(AVCodecContext *avctx, const AVFrame *pic, uint8_t *dst)
124 {
125  DPXContext *s = avctx->priv_data;
126  const uint8_t *src[3] = {pic->data[0], pic->data[1], pic->data[2]};
127  int x, y, i;
128 
129  for (y = 0; y < avctx->height; y++) {
130  for (x = 0; x < avctx->width; x++) {
131  int value;
132  if (s->big_endian) {
133  value = (AV_RB16(src[0] + 2*x) << 12)
134  | (AV_RB16(src[1] + 2*x) << 2)
135  | ((unsigned)AV_RB16(src[2] + 2*x) << 22);
136  } else {
137  value = (AV_RL16(src[0] + 2*x) << 12)
138  | (AV_RL16(src[1] + 2*x) << 2)
139  | ((unsigned)AV_RL16(src[2] + 2*x) << 22);
140  }
141  write32(dst, value);
142  dst += 4;
143  }
144  for (i = 0; i < 3; i++)
145  src[i] += pic->linesize[i];
146  }
147 }
148 
149 static void encode_gbrp12(AVCodecContext *avctx, const AVFrame *pic, uint16_t *dst)
150 {
151  DPXContext *s = avctx->priv_data;
152  const uint16_t *src[3] = {(uint16_t*)pic->data[0],
153  (uint16_t*)pic->data[1],
154  (uint16_t*)pic->data[2]};
155  int x, y, i, pad;
156  pad = avctx->width*6;
157  pad = (FFALIGN(pad, 4) - pad) >> 1;
158  for (y = 0; y < avctx->height; y++) {
159  for (x = 0; x < avctx->width; x++) {
160  uint16_t value[3];
161  if (s->big_endian) {
162  value[1] = AV_RB16(src[0] + x) << 4;
163  value[2] = AV_RB16(src[1] + x) << 4;
164  value[0] = AV_RB16(src[2] + x) << 4;
165  } else {
166  value[1] = AV_RL16(src[0] + x) << 4;
167  value[2] = AV_RL16(src[1] + x) << 4;
168  value[0] = AV_RL16(src[2] + x) << 4;
169  }
170  for (i = 0; i < 3; i++)
171  write16(dst++, value[i]);
172  }
173  for (i = 0; i < pad; i++)
174  *dst++ = 0;
175  for (i = 0; i < 3; i++)
176  src[i] += pic->linesize[i]/2;
177  }
178 }
179 
181  const AVFrame *frame, int *got_packet)
182 {
183  DPXContext *s = avctx->priv_data;
184  int size, ret, need_align, len;
185  uint8_t *buf;
186 
187 #define HEADER_SIZE 1664 /* DPX Generic header */
188  if (s->bits_per_component == 10)
189  size = avctx->height * avctx->width * 4;
190  else if (s->bits_per_component == 12) {
191  // 3 components, 12 bits put on 16 bits
192  len = avctx->width*6;
193  size = FFALIGN(len, 4);
194  need_align = size - len;
195  size *= avctx->height;
196  } else {
197  // N components, M bits
198  len = avctx->width * s->num_components * s->bits_per_component >> 3;
199  size = FFALIGN(len, 4);
200  need_align = size - len;
201  size *= avctx->height;
202  }
203  if ((ret = ff_alloc_packet2(avctx, pkt, size + HEADER_SIZE, 0)) < 0)
204  return ret;
205  buf = pkt->data;
206 
207  memset(buf, 0, HEADER_SIZE);
208 
209  /* File information header */
210  write32(buf, MKBETAG('S','D','P','X'));
211  write32(buf + 4, HEADER_SIZE);
212  memcpy (buf + 8, "V1.0", 4);
213  write32(buf + 20, 1); /* new image */
214  write32(buf + 24, HEADER_SIZE);
215  if (!(avctx->flags & AV_CODEC_FLAG_BITEXACT))
216  memcpy (buf + 160, LIBAVCODEC_IDENT, FFMIN(sizeof(LIBAVCODEC_IDENT), 100));
217  write32(buf + 660, 0xFFFFFFFF); /* unencrypted */
218 
219  /* Image information header */
220  write16(buf + 768, 0); /* orientation; left to right, top to bottom */
221  write16(buf + 770, 1); /* number of elements */
222  write32(buf + 772, avctx->width);
223  write32(buf + 776, avctx->height);
224  buf[800] = s->descriptor;
225  buf[801] = 2; /* linear transfer */
226  buf[802] = 2; /* linear colorimetric */
227  buf[803] = s->bits_per_component;
228  write16(buf + 804, (s->bits_per_component == 10 || s->bits_per_component == 12) ?
229  1 : 0); /* packing method */
230  write32(buf + 808, HEADER_SIZE); /* data offset */
231 
232  /* Image source information header */
233  write32(buf + 1628, avctx->sample_aspect_ratio.num);
234  write32(buf + 1632, avctx->sample_aspect_ratio.den);
235 
236  switch(s->bits_per_component) {
237  case 8:
238  case 16:
239  if (need_align) {
240  int j;
241  const uint8_t *src = frame->data[0];
242  uint8_t *dst = pkt->data + HEADER_SIZE;
243  size = (len + need_align) * avctx->height;
244  for (j=0; j<avctx->height; j++) {
245  memcpy(dst, src, len);
246  memset(dst + len, 0, need_align);
247  dst += len + need_align;
248  src += frame->linesize[0];
249  }
250  } else {
252  (const uint8_t**)frame->data, frame->linesize,
253  avctx->pix_fmt,
254  avctx->width, avctx->height, 1);
255  }
256  if (size < 0)
257  return size;
258  break;
259  case 10:
260  if (s->planar)
261  encode_gbrp10(avctx, frame, buf + HEADER_SIZE);
262  else
263  encode_rgb48_10bit(avctx, frame, buf + HEADER_SIZE);
264  break;
265  case 12:
266  encode_gbrp12(avctx, frame, (uint16_t*)(buf + HEADER_SIZE));
267  break;
268  default:
269  av_log(avctx, AV_LOG_ERROR, "Unsupported bit depth: %d\n", s->bits_per_component);
270  return -1;
271  }
272 
273  size += HEADER_SIZE;
274 
275  write32(buf + 16, size); /* file size */
276 
278  *got_packet = 1;
279 
280  return 0;
281 }
282 
284  .name = "dpx",
285  .long_name = NULL_IF_CONFIG_SMALL("DPX (Digital Picture Exchange) image"),
286  .type = AVMEDIA_TYPE_VIDEO,
287  .id = AV_CODEC_ID_DPX,
288  .priv_data_size = sizeof(DPXContext),
289  .init = encode_init,
290  .encode2 = encode_frame,
291  .pix_fmts = (const enum AVPixelFormat[]){
300 };
#define av_always_inline
Definition: attributes.h:45
#define av_cold
Definition: attributes.h:88
uint8_t
Libavcodec external API header.
#define AV_RL16
Definition: intreadwrite.h:42
#define AV_RB16
Definition: intreadwrite.h:53
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:31
#define s(width, name)
Definition: cbs_vp9.c:257
common internal and external API header
#define FFMIN(a, b)
Definition: common.h:105
#define MKBETAG(a, b, c, d)
Definition: common.h:479
static AVFrame * frame
static void encode_gbrp10(AVCodecContext *avctx, const AVFrame *pic, uint8_t *dst)
Definition: dpxenc.c:123
#define write16(p, value)
Definition: dpxenc.c:94
static void encode_rgb48_10bit(AVCodecContext *avctx, const AVFrame *pic, uint8_t *dst)
Definition: dpxenc.c:97
#define HEADER_SIZE
static av_always_inline void write32_internal(int big_endian, void *p, int value)
Definition: dpxenc.c:88
AVCodec ff_dpx_encoder
Definition: dpxenc.c:283
static av_cold int encode_init(AVCodecContext *avctx)
Definition: dpxenc.c:36
static int encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet)
Definition: dpxenc.c:180
static av_always_inline void write16_internal(int big_endian, void *p, int value)
Definition: dpxenc.c:82
static void encode_gbrp12(AVCodecContext *avctx, const AVFrame *pic, uint16_t *dst)
Definition: dpxenc.c:149
#define write32(p, value)
Definition: dpxenc.c:95
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
Check AVPacket size and/or allocate data.
Definition: encode.c:34
double value
Definition: eval.c:100
#define AV_CODEC_FLAG_BITEXACT
Use only bitexact stuff (except (I)DCT).
Definition: avcodec.h:333
@ AV_CODEC_ID_DPX
Definition: codec_id.h:177
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: packet.h:410
#define AVERROR(e)
Definition: error.h:43
#define AV_LOG_INFO
Standard information.
Definition: log.h:205
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
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:508
for(j=16;j >0;--j)
misc image utilities
int i
Definition: input.c:407
#define AV_WB32(p, v)
Definition: intreadwrite.h:419
#define AV_WL32(p, v)
Definition: intreadwrite.h:426
#define AV_WB16(p, v)
Definition: intreadwrite.h:405
#define AV_WL16(p, v)
Definition: intreadwrite.h:412
#define LIBAVCODEC_IDENT
Definition: version.h:42
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:309
const char * desc
Definition: libsvtav1.c:83
#define FFALIGN(x, a)
Definition: macros.h:48
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2573
#define AV_PIX_FMT_FLAG_ALPHA
The pixel format has an alpha channel.
Definition: pixdesc.h:179
#define AV_PIX_FMT_FLAG_PLANAR
At least one pixel component is not in the first data plane.
Definition: pixdesc.h:144
#define AV_PIX_FMT_FLAG_BE
Pixel format is big-endian.
Definition: pixdesc.h:128
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
@ AV_PIX_FMT_GRAY16BE
Y , 16bpp, big-endian.
Definition: pixfmt.h:97
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
@ AV_PIX_FMT_RGB24
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:68
@ AV_PIX_FMT_GBRP10BE
planar GBR 4:4:4 30bpp, big-endian
Definition: pixfmt.h:172
@ AV_PIX_FMT_GBRP12BE
planar GBR 4:4:4 36bpp, big-endian
Definition: pixfmt.h:254
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition: pixfmt.h:74
@ 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:102
@ AV_PIX_FMT_ABGR
packed ABGR 8:8:8:8, 32bpp, ABGRABGR...
Definition: pixfmt.h:94
@ 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:205
@ 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:206
@ AV_PIX_FMT_RGBA
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:93
@ AV_PIX_FMT_GBRP12LE
planar GBR 4:4:4 36bpp, little-endian
Definition: pixfmt.h:255
@ 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:103
@ AV_PIX_FMT_GRAY16LE
Y , 16bpp, little-endian.
Definition: pixfmt.h:98
@ AV_PIX_FMT_GBRP10LE
planar GBR 4:4:4 30bpp, little-endian
Definition: pixfmt.h:173
main external API structure.
Definition: avcodec.h:536
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:746
int width
picture width / height.
Definition: avcodec.h:709
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:915
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:1751
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:616
void * priv_data
Definition: avcodec.h:563
AVCodec.
Definition: codec.h:197
const char * name
Name of the codec implementation.
Definition: codec.h:204
This structure describes decoded (raw) audio or video data.
Definition: frame.h:318
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:332
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:349
This structure stores compressed data.
Definition: packet.h:346
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:375
int size
Definition: packet.h:370
uint8_t * data
Definition: packet.h:369
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:81
int num
Numerator.
Definition: rational.h:59
int den
Denominator.
Definition: rational.h:60
int big_endian
Definition: dpxenc.c:29
int num_components
Definition: dpxenc.c:31
int bits_per_component
Definition: dpxenc.c:30
int descriptor
Definition: dpxenc.c:32
int planar
Definition: dpxenc.c:33
#define av_log(a,...)
#define src
Definition: vp8dsp.c:255
AVPacket * pkt
Definition: movenc.c:59
int size
int len