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;
39 
40  avctx->coded_frame = &s->picture;
42  avctx->coded_frame->key_frame = 1;
43 
44  s->big_endian = 1;
45  s->bits_per_component = 8;
46  s->descriptor = 50; /* RGB */
47  s->planar = 0;
48 
49  switch (avctx->pix_fmt) {
50  case AV_PIX_FMT_RGB24:
51  break;
52  case AV_PIX_FMT_RGBA:
53  s->descriptor = 51; /* RGBA */
54  break;
55  case AV_PIX_FMT_RGB48LE:
56  s->big_endian = 0;
57  case AV_PIX_FMT_RGB48BE:
59  break;
61  s->big_endian = 0;
63  s->descriptor = 51;
64  s->bits_per_component = 16;
65  break;
67  s->big_endian = 0;
69  s->bits_per_component = 10;
70  s->planar = 1;
71  break;
73  s->big_endian = 0;
75  s->bits_per_component = 12;
76  s->planar = 1;
77  break;
78  default:
79  av_log(avctx, AV_LOG_INFO, "unsupported pixel format\n");
80  return -1;
81  }
82 
83  return 0;
84 }
85 
86 #define write16(p, value) \
87 do { \
88  if (s->big_endian) AV_WB16(p, value); \
89  else AV_WL16(p, value); \
90 } while(0)
91 
92 #define write32(p, value) \
93 do { \
94  if (s->big_endian) AV_WB32(p, value); \
95  else AV_WL32(p, value); \
96 } while(0)
97 
98 static void encode_rgb48_10bit(AVCodecContext *avctx, const AVPicture *pic, 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 AVPicture *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  | (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  | (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 AVPicture *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;
156  for (y = 0; y < avctx->height; y++) {
157  for (x = 0; x < avctx->width; x++) {
158  uint16_t value[3];
159  if (s->big_endian) {
160  value[1] = AV_RB16(src[0] + x) << 4;
161  value[2] = AV_RB16(src[1] + x) << 4;
162  value[0] = AV_RB16(src[2] + x) << 4;
163  } else {
164  value[1] = AV_RL16(src[0] + x) << 4;
165  value[2] = AV_RL16(src[1] + x) << 4;
166  value[0] = AV_RL16(src[2] + x) << 4;
167  }
168  for (i = 0; i < 3; i++)
169  write16(dst++, value[i]);
170  }
171  for (i = 0; i < 3; i++)
172  src[i] += pic->linesize[i]/2;
173  }
174 }
175 
177  const AVFrame *frame, int *got_packet)
178 {
179  DPXContext *s = avctx->priv_data;
180  int size, ret;
181  uint8_t *buf;
182 
183 #define HEADER_SIZE 1664 /* DPX Generic header */
184  if (s->bits_per_component == 10)
185  size = avctx->height * avctx->width * 4;
186  else
187  size = avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height);
188  if ((ret = ff_alloc_packet2(avctx, pkt, size + HEADER_SIZE)) < 0)
189  return ret;
190  buf = pkt->data;
191 
192  memset(buf, 0, HEADER_SIZE);
193 
194  /* File information header */
195  write32(buf, MKBETAG('S','D','P','X'));
196  write32(buf + 4, HEADER_SIZE);
197  memcpy (buf + 8, "V1.0", 4);
198  write32(buf + 20, 1); /* new image */
199  write32(buf + 24, HEADER_SIZE);
200  if (!(avctx->flags & CODEC_FLAG_BITEXACT))
201  memcpy (buf + 160, LIBAVCODEC_IDENT, FFMIN(sizeof(LIBAVCODEC_IDENT), 100));
202  write32(buf + 660, 0xFFFFFFFF); /* unencrypted */
203 
204  /* Image information header */
205  write16(buf + 768, 0); /* orientation; left to right, top to bottom */
206  write16(buf + 770, 1); /* number of elements */
207  write32(buf + 772, avctx->width);
208  write32(buf + 776, avctx->height);
209  buf[800] = s->descriptor;
210  buf[801] = 2; /* linear transfer */
211  buf[802] = 2; /* linear colorimetric */
212  buf[803] = s->bits_per_component;
213  write16(buf + 804, (s->bits_per_component == 10 || s->bits_per_component == 12) ?
214  1 : 0); /* packing method */
215  write32(buf + 808, HEADER_SIZE); /* data offset */
216 
217  /* Image source information header */
218  write32(buf + 1628, avctx->sample_aspect_ratio.num);
219  write32(buf + 1632, avctx->sample_aspect_ratio.den);
220 
221  switch(s->bits_per_component) {
222  case 8:
223  case 16:
224  size = avpicture_layout((const AVPicture*)frame, avctx->pix_fmt,
225  avctx->width, avctx->height,
226  buf + HEADER_SIZE, pkt->size - HEADER_SIZE);
227  if (size < 0)
228  return size;
229  break;
230  case 10:
231  if (s->planar)
232  encode_gbrp10(avctx, (const AVPicture*)frame, buf + HEADER_SIZE);
233  else
234  encode_rgb48_10bit(avctx, (const AVPicture*)frame, buf + HEADER_SIZE);
235  break;
236  case 12:
237  encode_gbrp12(avctx, (const AVPicture*)frame, (uint16_t*)(buf + HEADER_SIZE));
238  break;
239  default:
240  av_log(avctx, AV_LOG_ERROR, "Unsupported bit depth: %d\n", s->bits_per_component);
241  return -1;
242  }
243 
244  size += HEADER_SIZE;
245 
246  write32(buf + 16, size); /* file size */
247 
248  pkt->flags |= AV_PKT_FLAG_KEY;
249  *got_packet = 1;
250 
251  return 0;
252 }
253 
255  .name = "dpx",
256  .type = AVMEDIA_TYPE_VIDEO,
257  .id = AV_CODEC_ID_DPX,
258  .priv_data_size = sizeof(DPXContext),
259  .init = encode_init,
260  .encode2 = encode_frame,
261  .pix_fmts = (const enum AVPixelFormat[]){
273  .long_name = NULL_IF_CONFIG_SMALL("DPX image"),
274 };
const char * s
Definition: avisynth_c.h:668
#define write32(p, value)
Definition: dpxenc.c:92
int linesize[AV_NUM_DATA_POINTERS]
number of bytes per line
This structure describes decoded (raw) audio or video data.
Definition: frame.h:76
int big_endian
Definition: dpxenc.c:30
misc image utilities
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:70
AVFrame * coded_frame
the picture in the bitstream
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
struct DPXContext DPXContext
static av_cold int encode_init(AVCodecContext *avctx)
Definition: dpxenc.c:36
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...
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
int avpicture_layout(const AVPicture *src, enum AVPixelFormat pix_fmt, int width, int height, unsigned char *dest, int dest_size)
Copy pixel data from an AVPicture into a buffer, always assume a linesize alignment of 1...
Definition: avpicture.c:41
#define AV_RL16
planar GBR 4:4:4 36bpp, little-endian
Definition: pixfmt.h:234
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
four components are given, that&#39;s all.
planar GBR 4:4:4 36bpp, big-endian
Definition: pixfmt.h:233
uint8_t
#define av_cold
Definition: attributes.h:78
static void encode_gbrp10(AVCodecContext *avctx, const AVPicture *pic, uint8_t *dst)
Definition: dpxenc.c:123
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:112
uint8_t * data[AV_NUM_DATA_POINTERS]
static AVPacket pkt
Definition: demuxing.c:56
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:209
static void encode_gbrp12(AVCodecContext *avctx, const AVPicture *pic, uint16_t *dst)
Definition: dpxenc.c:149
uint8_t * data
#define CODEC_FLAG_BITEXACT
Use only bitexact stuff (except (I)DCT).
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
frame
Definition: stft.m:14
int planar
Definition: dpxenc.c:33
Discrete Time axis x
#define U(x)
#define HEADER_SIZE
#define AV_RB16
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
int flags
CODEC_FLAG_*.
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:246
const char * name
Name of the codec implementation.
int bits_per_component
Definition: dpxenc.c:31
external API header
int size
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:97
int flags
A combination of AV_PKT_FLAG values.
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:144
#define FFMIN(a, b)
Definition: common.h:58
ret
Definition: avfilter.c:821
int width
picture width / height.
AVCodec ff_dpx_encoder
Definition: dpxenc.c:254
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int size)
Check AVPacket size and/or allocate data.
int descriptor
Definition: dpxenc.c:32
AVS_Value src
Definition: avisynth_c.h:523
planar GBR 4:4:4 30bpp, big-endian
Definition: pixfmt.h:171
main external API structure.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:148
void * buf
Definition: avisynth_c.h:594
double value
Definition: eval.c:82
static void encode_rgb48_10bit(AVCodecContext *avctx, const AVPicture *pic, uint8_t *dst)
Definition: dpxenc.c:98
synthesis window for stochastic i
common internal api header.
common internal and external API header
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:111
#define write16(p, value)
Definition: dpxenc.c:86
int den
denominator
Definition: rational.h:45
function y
Definition: D.m:1
#define MKBETAG(a, b, c, d)
Definition: common.h:283
static int encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet)
Definition: dpxenc.c:176
else dst[i][x+y *dst_stride[i]]
Definition: vf_mcdeint.c:160
int avpicture_get_size(enum AVPixelFormat pix_fmt, int width, int height)
Calculate the size in bytes that a picture of the given width and height would occupy if stored in th...
Definition: avpicture.c:49
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:139
AVFrame picture
Definition: dpxenc.c:29
#define LIBAVCODEC_IDENT
#define AV_LOG_INFO
Definition: log.h:156
AVPixelFormat
Pixel format.
Definition: pixfmt.h:66
This structure stores compressed data.
planar GBR 4:4:4 30bpp, little-endian
Definition: pixfmt.h:172
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:210