FFmpeg
Loading...
Searching...
No Matches
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
23#include "libavutil/common.h"
25#include "libavutil/imgutils.h"
26#include "avcodec.h"
27#include "codec_internal.h"
28#include "encode.h"
29#include "version.h"
30#include "dpx.h"
31
39
41{
42 DPXContext *s = avctx->priv_data;
44
45 s->big_endian = !!(desc->flags & AV_PIX_FMT_FLAG_BE);
46 s->bits_per_component = desc->comp[0].depth;
47 s->num_components = desc->nb_components;
48 s->descriptor = (desc->flags & AV_PIX_FMT_FLAG_ALPHA) ? 51 : 50;
49 s->planar = !!(desc->flags & AV_PIX_FMT_FLAG_PLANAR);
50
51 switch (avctx->pix_fmt) {
52 case AV_PIX_FMT_ABGR:
53 s->descriptor = 52;
54 break;
58 s->descriptor = 6;
59 break;
67 case AV_PIX_FMT_RGBA:
68 break;
71 if (avctx->bits_per_raw_sample == 10) {
72 s->bits_per_component = 10;
73 } else if (avctx->bits_per_raw_sample && avctx->bits_per_raw_sample != 16) {
74 av_log(avctx, AV_LOG_ERROR, "Unsupported bit depth: %d\n", avctx->bits_per_raw_sample);
75 return AVERROR(EINVAL);
76 }
77 break;
78 }
79
80 return 0;
81}
82
83static av_always_inline void write16_internal(int big_endian, void *p, int value)
84{
85 if (big_endian) AV_WB16(p, value);
86 else AV_WL16(p, value);
87}
88
89static av_always_inline void write32_internal(int big_endian, void *p, int value)
90{
91 if (big_endian) AV_WB32(p, value);
92 else AV_WL32(p, value);
93}
94
95#define write16(p, value) write16_internal(s->big_endian, p, value)
96#define write32(p, value) write32_internal(s->big_endian, p, value)
97
98static void encode_rgb48_10bit(AVCodecContext *avctx, const AVFrame *pic,
99 uint8_t *dst)
100{
101 DPXContext *s = avctx->priv_data;
102 const uint8_t *src = pic->data[0];
103 int x, y;
104
105 for (y = 0; y < avctx->height; y++) {
106 for (x = 0; x < avctx->width; x++) {
107 int value;
108 if (s->big_endian) {
109 value = ((AV_RB16(src + 6*x + 4) & 0xFFC0U) >> 4)
110 | ((AV_RB16(src + 6*x + 2) & 0xFFC0U) << 6)
111 | ((AV_RB16(src + 6*x + 0) & 0xFFC0U) << 16);
112 } else {
113 value = ((AV_RL16(src + 6*x + 4) & 0xFFC0U) >> 4)
114 | ((AV_RL16(src + 6*x + 2) & 0xFFC0U) << 6)
115 | ((AV_RL16(src + 6*x + 0) & 0xFFC0U) << 16);
116 }
117 write32(dst, value);
118 dst += 4;
119 }
120 src += pic->linesize[0];
121 }
122}
123
124static void encode_gbrp10(AVCodecContext *avctx, const AVFrame *pic, uint8_t *dst)
125{
126 DPXContext *s = avctx->priv_data;
127 const uint8_t *src[3] = {pic->data[0], pic->data[1], pic->data[2]};
128 int x, y, i;
129
130 for (y = 0; y < avctx->height; y++) {
131 for (x = 0; x < avctx->width; x++) {
132 int value;
133 if (s->big_endian) {
134 value = (AV_RB16(src[0] + 2*x) << 12)
135 | (AV_RB16(src[1] + 2*x) << 2)
136 | ((unsigned)AV_RB16(src[2] + 2*x) << 22);
137 } else {
138 value = (AV_RL16(src[0] + 2*x) << 12)
139 | (AV_RL16(src[1] + 2*x) << 2)
140 | ((unsigned)AV_RL16(src[2] + 2*x) << 22);
141 }
142 write32(dst, value);
143 dst += 4;
144 }
145 for (i = 0; i < 3; i++)
146 src[i] += pic->linesize[i];
147 }
148}
149
150static void encode_gbrp12(AVCodecContext *avctx, const AVFrame *pic, uint8_t *dst)
151{
152 DPXContext *s = avctx->priv_data;
153 const uint16_t *src[3] = {(uint16_t*)pic->data[0],
154 (uint16_t*)pic->data[1],
155 (uint16_t*)pic->data[2]};
156 int x, y, i, pad;
157 pad = avctx->width*6;
158 pad = (FFALIGN(pad, 4) - pad) >> 1;
159 for (y = 0; y < avctx->height; y++) {
160 for (x = 0; x < avctx->width; x++) {
161 uint16_t value[3];
162 if (s->big_endian) {
163 value[1] = AV_RB16(src[0] + x) << 4;
164 value[2] = AV_RB16(src[1] + x) << 4;
165 value[0] = AV_RB16(src[2] + x) << 4;
166 } else {
167 value[1] = AV_RL16(src[0] + x) << 4;
168 value[2] = AV_RL16(src[1] + x) << 4;
169 value[0] = AV_RL16(src[2] + x) << 4;
170 }
171 for (i = 0; i < 3; i++, dst += 2)
172 write16(dst, value[i]);
173 }
174 for (i = 0; i < pad; i++, dst += 2)
175 AV_WN16(dst, 0);
176 for (i = 0; i < 3; i++)
177 src[i] += pic->linesize[i]/2;
178 }
179}
180
182 const AVFrame *frame, int *got_packet)
183{
184 DPXContext *s = avctx->priv_data;
185 int size, ret, need_align, len;
186 uint8_t *buf;
187 int color_trc, color_spec;
188
189#define HEADER_SIZE 1664 /* DPX Generic header */
190 if (s->bits_per_component == 10)
191 size = avctx->height * avctx->width * 4;
192 else if (s->bits_per_component == 12) {
193 // 3 components, 12 bits put on 16 bits
194 len = avctx->width*6;
195 size = FFALIGN(len, 4);
196 need_align = size - len;
197 size *= avctx->height;
198 } else {
199 // N components, M bits
200 len = avctx->width * s->num_components * s->bits_per_component >> 3;
201 size = FFALIGN(len, 4);
202 need_align = size - len;
203 size *= avctx->height;
204 }
205 if ((ret = ff_get_encode_buffer(avctx, pkt, size + HEADER_SIZE, 0)) < 0)
206 return ret;
207 buf = pkt->data;
208
209 memset(buf, 0, HEADER_SIZE);
210
211 /* File information header */
212 write32(buf, MKBETAG('S','D','P','X'));
213 write32(buf + 4, HEADER_SIZE);
214 memcpy (buf + 8, "V1.0", 4);
215 write32(buf + 20, 1); /* new image */
216 write32(buf + 24, HEADER_SIZE);
217 if (!(avctx->flags & AV_CODEC_FLAG_BITEXACT))
218 memcpy (buf + 160, LIBAVCODEC_IDENT, FFMIN(sizeof(LIBAVCODEC_IDENT), 100));
219 write32(buf + 660, 0xFFFFFFFF); /* unencrypted */
220
221 /* Image information header */
222 write16(buf + 768, 0); /* orientation; left to right, top to bottom */
223 write16(buf + 770, 1); /* number of elements */
224 write32(buf + 772, avctx->width);
225 write32(buf + 776, avctx->height);
226 buf[800] = s->descriptor;
227
228 switch (avctx->color_trc) {
229 case AVCOL_TRC_BT709:
230 color_trc = DPX_TRC_ITU_R_709_4;
231 break;
232 default:
233 av_log(avctx, AV_LOG_WARNING, "unsupported color transfer\n");
236 color_trc = DPX_TRC_UNSPECIFIED_VIDEO;
237 break;
239 color_trc = DPX_TRC_ITU_R_624_4_PAL;
240 break;
242 color_trc = DPX_TRC_SMPTE_170;
243 break;
244 case AVCOL_TRC_LINEAR:
245 color_trc = DPX_TRC_LINEAR;
246 break;
247 }
248 buf[801] = color_trc;
249
250 switch (avctx->color_primaries) {
251 case AVCOL_PRI_BT709:
252 color_spec = DPX_COL_SPEC_ITU_R_709_4;
253 break;
254 default:
255 av_log(avctx, AV_LOG_WARNING, "unsupported colorimetric specification\n");
259 break;
261 color_spec = DPX_COL_SPEC_ITU_R_624_4_PAL;
262 break;
264 color_spec = DPX_COL_SPEC_SMPTE_170;
265 break;
266 }
267 buf[802] = color_spec;
268
269 buf[803] = s->bits_per_component;
270 write16(buf + 804, (s->bits_per_component == 10 || s->bits_per_component == 12) ?
271 1 : 0); /* packing method */
272 write32(buf + 808, HEADER_SIZE); /* data offset */
273
274 /* Image source information header */
275 write32(buf + 1628, avctx->sample_aspect_ratio.num);
276 write32(buf + 1632, avctx->sample_aspect_ratio.den);
277
278 switch(s->bits_per_component) {
279 case 8:
280 case 16:
281 if (need_align) {
282 int j;
283 const uint8_t *src = frame->data[0];
284 uint8_t *dst = pkt->data + HEADER_SIZE;
285 size = (len + need_align) * avctx->height;
286 for (j=0; j<avctx->height; j++) {
287 memcpy(dst, src, len);
288 memset(dst + len, 0, need_align);
289 dst += len + need_align;
290 src += frame->linesize[0];
291 }
292 } else {
294 (const uint8_t**)frame->data, frame->linesize,
295 avctx->pix_fmt,
296 avctx->width, avctx->height, 1);
297 }
298 if (size < 0)
299 return size;
300 break;
301 case 10:
302 if (s->planar)
303 encode_gbrp10(avctx, frame, buf + HEADER_SIZE);
304 else
305 encode_rgb48_10bit(avctx, frame, buf + HEADER_SIZE);
306 break;
307 case 12:
308 encode_gbrp12(avctx, frame, buf + HEADER_SIZE);
309 break;
310 default:
311 av_log(avctx, AV_LOG_ERROR, "Unsupported bit depth: %d\n", s->bits_per_component);
312 return -1;
313 }
314
315 size += HEADER_SIZE;
316
317 write32(buf + 16, size); /* file size */
318
319 *got_packet = 1;
320
321 return 0;
322}
323
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:97
#define HEADER_SIZE
Definition adxenc.c:99
const FFCodec ff_dpx_encoder
Definition dpxenc.c:324
static av_cold int encode_init(AVCodecContext *avctx)
Definition asvenc.c:373
Libavcodec external API header.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
#define CODEC_PIXFMTS(...)
#define FF_CODEC_ENCODE_CB(func)
#define CODEC_LONG_NAME(str)
common internal and external API header
static AVPacket * pkt
static AVFrame * frame
@ DPX_COL_SPEC_SMPTE_170
Definition dpx.h:54
@ DPX_COL_SPEC_UNSPECIFIED_VIDEO
Definition dpx.h:49
@ DPX_COL_SPEC_ITU_R_624_4_PAL
Definition dpx.h:55
@ DPX_COL_SPEC_ITU_R_709_4
Definition dpx.h:51
@ DPX_TRC_UNSPECIFIED_VIDEO
Definition dpx.h:33
@ DPX_TRC_ITU_R_624_4_PAL
Definition dpx.h:39
@ DPX_TRC_ITU_R_709_4
Definition dpx.h:35
@ DPX_TRC_LINEAR
Definition dpx.h:31
@ DPX_TRC_SMPTE_170
Definition dpx.h:38
static void encode_gbrp10(AVCodecContext *avctx, const AVFrame *pic, uint8_t *dst)
Definition dpxenc.c:124
#define write16(p, value)
Definition dpxenc.c:95
static void encode_rgb48_10bit(AVCodecContext *avctx, const AVFrame *pic, uint8_t *dst)
Definition dpxenc.c:98
static av_always_inline void write32_internal(int big_endian, void *p, int value)
Definition dpxenc.c:89
static av_cold int encode_init(AVCodecContext *avctx)
Definition dpxenc.c:40
static int encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet)
Definition dpxenc.c:181
static av_always_inline void write16_internal(int big_endian, void *p, int value)
Definition dpxenc.c:83
#define write32(p, value)
Definition dpxenc.c:96
static void encode_gbrp12(AVCodecContext *avctx, const AVFrame *pic, uint8_t *dst)
Definition dpxenc.c:150
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:613
int ff_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int flags)
Get a buffer for a packet.
Definition encode.c:107
double value
Definition eval.c:102
static int encode_frame(OutputFile *of, OutputStream *ost, AVFrame *frame, AVPacket *pkt)
Definition ffmpeg_enc.c:683
int ret
Definition ffplay.c:391
#define AV_CODEC_FLAG_BITEXACT
Use only bitexact stuff (except (I)DCT).
Definition avcodec.h:322
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition codec.h:49
@ AV_CODEC_ID_DPX
Definition codec_id.h:178
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
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
misc image utilities
#define AV_WB32(p, v)
#define AV_WL32(p, v)
#define AV_RL16(p)
#define AV_WN16(p, v)
#define AV_WB16(p, v)
#define AV_WL16(p, v)
#define AV_RB16(p)
Libavcodec version macros.
#define LIBAVCODEC_IDENT
Definition version.h:43
Macro definitions for various function/variable attributes.
#define av_always_inline
Definition attributes.h:72
#define av_fallthrough
Definition attributes.h:67
#define av_cold
Definition attributes.h:117
const char * desc
Definition libsvtav1.c:83
#define FFMIN(a, b)
Definition macros.h:49
#define MKBETAG(a, b, c, d)
Definition macros.h:56
#define FFALIGN(x, a)
Definition macros.h:78
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition pixdesc.c:3484
#define AV_PIX_FMT_FLAG_ALPHA
The pixel format has an alpha channel.
Definition pixdesc.h:147
#define AV_PIX_FMT_FLAG_PLANAR
At least one pixel component is not in the first data plane.
Definition pixdesc.h:132
#define AV_PIX_FMT_FLAG_BE
Pixel format is big-endian.
Definition pixdesc.h:116
@ AV_PIX_FMT_GRAY16BE
Y , 16bpp, big-endian.
Definition pixfmt.h:104
@ AV_PIX_FMT_RGB24
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition pixfmt.h:75
@ AV_PIX_FMT_GBRP10BE
planar GBR 4:4:4 30bpp, big-endian
Definition pixfmt.h:169
@ AV_PIX_FMT_GBRP12BE
planar GBR 4:4:4 36bpp, big-endian
Definition pixfmt.h:279
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition pixfmt.h:81
@ 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
@ AV_PIX_FMT_ABGR
packed ABGR 8:8:8:8, 32bpp, ABGRABGR...
Definition pixfmt.h:101
@ 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
@ 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_RGBA
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition pixfmt.h:100
@ AV_PIX_FMT_GBRP12LE
planar GBR 4:4:4 36bpp, little-endian
Definition pixfmt.h:280
@ 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_GRAY16LE
Y , 16bpp, little-endian.
Definition pixfmt.h:105
@ AV_PIX_FMT_GBRP10LE
planar GBR 4:4:4 30bpp, little-endian
Definition pixfmt.h:170
@ AVCOL_PRI_BT470BG
also ITU-R BT601-6 625 / ITU-R BT1358 625 / ITU-R BT1700 625 PAL & SECAM
Definition pixfmt.h:654
@ AVCOL_PRI_BT709
also ITU-R BT1361 / IEC 61966-2-4 / SMPTE RP 177 Annex B
Definition pixfmt.h:649
@ AVCOL_PRI_UNSPECIFIED
Definition pixfmt.h:650
@ AVCOL_PRI_SMPTE170M
also ITU-R BT601-6 525 / ITU-R BT1358 525 / ITU-R BT1700 NTSC
Definition pixfmt.h:655
@ AVCOL_TRC_SMPTE170M
also ITU-R BT601-6 525 or 625 / ITU-R BT1358 525 or 625 / ITU-R BT1700 NTSC
Definition pixfmt.h:684
@ AVCOL_TRC_LINEAR
"Linear transfer characteristics"
Definition pixfmt.h:686
@ AVCOL_TRC_GAMMA28
also ITU-R BT470BG
Definition pixfmt.h:683
@ AVCOL_TRC_UNSPECIFIED
Definition pixfmt.h:680
@ AVCOL_TRC_BT709
also ITU-R BT1361
Definition pixfmt.h:679
main external API structure.
Definition avcodec.h:443
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition avcodec.h:644
int width
picture width / height.
Definition avcodec.h:605
enum AVColorPrimaries color_primaries
Chromaticity coordinates of the source primaries.
Definition avcodec.h:658
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:629
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition avcodec.h:1581
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
Definition avcodec.h:665
int flags
AV_CODEC_FLAG_*.
Definition avcodec.h:500
void * priv_data
Definition avcodec.h:470
This structure describes decoded (raw) audio or video data.
Definition frame.h:479
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition frame.h:500
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:524
This structure stores compressed data.
Definition packet.h:587
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition pixdesc.h:69
int num
Numerator.
Definition rational.h:59
int den
Denominator.
Definition rational.h:60
int big_endian
Definition dpxenc.c:33
int num_components
Definition dpxenc.c:35
int bits_per_component
Definition dpxenc.c:34
int descriptor
Definition dpxenc.c:36
int planar
Definition dpxenc.c:37
#define av_log(a,...)
#define src
Definition vp8dsp.c:248
int size
int len