Libav
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/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 {
32 } DPXContext;
33 
35 {
36  DPXContext *s = avctx->priv_data;
37 
38  avctx->coded_frame = av_frame_alloc();
39  if (!avctx->coded_frame)
40  return AVERROR(ENOMEM);
41 
43  avctx->coded_frame->key_frame = 1;
44 
45  s->big_endian = 1;
46  s->bits_per_component = 8;
47  s->descriptor = 50; /* RGB */
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;
60  default:
61  av_log(avctx, AV_LOG_INFO, "unsupported pixel format\n");
62  return -1;
63  }
64 
65  return 0;
66 }
67 
68 #define write16(p, value) \
69 do { \
70  if (s->big_endian) AV_WB16(p, value); \
71  else AV_WL16(p, value); \
72 } while(0)
73 
74 #define write32(p, value) \
75 do { \
76  if (s->big_endian) AV_WB32(p, value); \
77  else AV_WL32(p, value); \
78 } while(0)
79 
80 static void encode_rgb48_10bit(AVCodecContext *avctx, const AVPicture *pic,
81  uint8_t *dst)
82 {
83  DPXContext *s = avctx->priv_data;
84  const uint8_t *src = pic->data[0];
85  int x, y;
86 
87  for (y = 0; y < avctx->height; y++) {
88  for (x = 0; x < avctx->width; x++) {
89  int value;
90  if ((avctx->pix_fmt & 1)) {
91  value = ((AV_RB16(src + 6*x + 4) & 0xFFC0) >> 4)
92  | ((AV_RB16(src + 6*x + 2) & 0xFFC0) << 6)
93  | ((AV_RB16(src + 6*x + 0) & 0xFFC0) << 16);
94  } else {
95  value = ((AV_RL16(src + 6*x + 4) & 0xFFC0) >> 4)
96  | ((AV_RL16(src + 6*x + 2) & 0xFFC0) << 6)
97  | ((AV_RL16(src + 6*x + 0) & 0xFFC0) << 16);
98  }
99  write32(dst, value);
100  dst += 4;
101  }
102  src += pic->linesize[0];
103  }
104 }
105 
106 static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
107  const AVFrame *frame, int *got_packet)
108 {
109  DPXContext *s = avctx->priv_data;
110  int size, ret;
111  uint8_t *buf;
112 
113 #define HEADER_SIZE 1664 /* DPX Generic header */
114  if (s->bits_per_component == 10)
115  size = avctx->height * avctx->width * 4;
116  else
117  size = avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height);
118  if ((ret = ff_alloc_packet(pkt, size + HEADER_SIZE)) < 0) {
119  av_log(avctx, AV_LOG_ERROR, "Error getting output packet.\n");
120  return ret;
121  }
122  buf = pkt->data;
123 
124  memset(buf, 0, HEADER_SIZE);
125 
126  /* File information header */
127  write32(buf, MKBETAG('S','D','P','X'));
128  write32(buf + 4, HEADER_SIZE);
129  memcpy (buf + 8, "V1.0", 4);
130  write32(buf + 20, 1); /* new image */
131  write32(buf + 24, HEADER_SIZE);
132  if (!(avctx->flags & CODEC_FLAG_BITEXACT))
133  memcpy (buf + 160, LIBAVCODEC_IDENT, FFMIN(sizeof(LIBAVCODEC_IDENT), 100));
134  write32(buf + 660, 0xFFFFFFFF); /* unencrypted */
135 
136  /* Image information header */
137  write16(buf + 768, 0); /* orientation; left to right, top to bottom */
138  write16(buf + 770, 1); /* number of elements */
139  write32(buf + 772, avctx->width);
140  write32(buf + 776, avctx->height);
141  buf[800] = s->descriptor;
142  buf[801] = 2; /* linear transfer */
143  buf[802] = 2; /* linear colorimetric */
144  buf[803] = s->bits_per_component;
145  write16(buf + 804, s->bits_per_component == 10 ? 1 : 0); /* packing method */
146 
147  /* Image source information header */
148  write32(buf + 1628, avctx->sample_aspect_ratio.num);
149  write32(buf + 1632, avctx->sample_aspect_ratio.den);
150 
151  switch (s->bits_per_component) {
152  case 8:
153  case 16:
154  size = avpicture_layout((const AVPicture*)frame, avctx->pix_fmt,
155  avctx->width, avctx->height,
156  buf + HEADER_SIZE, pkt->size - HEADER_SIZE);
157  if (size < 0)
158  return size;
159  break;
160  case 10:
161  encode_rgb48_10bit(avctx, (const AVPicture*)frame, buf + HEADER_SIZE);
162  break;
163  default:
164  av_log(avctx, AV_LOG_ERROR, "Unsupported bit depth: %d\n", s->bits_per_component);
165  return -1;
166  }
167 
168  size += HEADER_SIZE;
169 
170  write32(buf + 16, size); /* file size */
171 
172  pkt->flags |= AV_PKT_FLAG_KEY;
173  *got_packet = 1;
174 
175  return 0;
176 }
177 
179 {
180  av_frame_free(&avctx->coded_frame);
181  return 0;
182 }
183 
185  .name = "dpx",
186  .long_name = NULL_IF_CONFIG_SMALL("DPX image"),
187  .type = AVMEDIA_TYPE_VIDEO,
188  .id = AV_CODEC_ID_DPX,
189  .priv_data_size = sizeof(DPXContext),
190  .init = encode_init,
191  .encode2 = encode_frame,
192  .close = encode_close,
193  .pix_fmts = (const enum AVPixelFormat[]){
199 };
#define write32(p, value)
Definition: dpxenc.c:74
int size
int linesize[AV_NUM_DATA_POINTERS]
number of bytes per line
Definition: avcodec.h:3028
This structure describes decoded (raw) audio or video data.
Definition: frame.h:135
int big_endian
Definition: dpxenc.c:29
misc image utilities
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:67
AVFrame * coded_frame
the picture in the bitstream
Definition: avcodec.h:2532
static av_cold int encode_init(AVCodecContext *avctx)
Definition: dpxenc.c:34
int num
numerator
Definition: rational.h:44
int size
Definition: avcodec.h:974
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:1429
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1254
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.
Definition: avpicture.c:49
#define AV_RL16
Definition: intreadwrite.h:42
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:2514
four components are given, that's all.
Definition: avcodec.h:3026
AVCodec.
Definition: avcodec.h:2796
static av_cold int encode_close(AVCodecContext *avctx)
Definition: dpxenc.c:178
uint8_t
#define av_cold
Definition: attributes.h:66
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:57
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:113
uint8_t * data[AV_NUM_DATA_POINTERS]
Definition: avcodec.h:3027
uint8_t * data
Definition: avcodec.h:973
#define CODEC_FLAG_BITEXACT
Use only bitexact stuff (except (I)DCT).
Definition: avcodec.h:658
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1019
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
#define HEADER_SIZE
#define AV_RB16
Definition: intreadwrite.h:53
#define AVERROR(e)
Definition: error.h:43
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:69
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:150
int flags
CODEC_FLAG_*.
Definition: avcodec.h:1144
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:168
const char * name
Name of the codec implementation.
Definition: avcodec.h:2803
int bits_per_component
Definition: dpxenc.c:30
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:96
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:979
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:196
#define FFMIN(a, b)
Definition: common.h:57
int width
picture width / height.
Definition: avcodec.h:1224
AVCodec ff_dpx_encoder
Definition: dpxenc.c:184
int ff_alloc_packet(AVPacket *avpkt, int size)
Check AVPacket size and/or allocate data.
Definition: utils.c:1245
int descriptor
Definition: dpxenc.c:31
#define AV_LOG_INFO
Standard information.
Definition: log.h:134
Libavcodec external API header.
main external API structure.
Definition: avcodec.h:1050
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:490
static void encode_rgb48_10bit(AVCodecContext *avctx, const AVPicture *pic, uint8_t *dst)
Definition: dpxenc.c:80
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:112
#define write16(p, value)
Definition: dpxenc.c:68
int den
denominator
Definition: rational.h:45
static av_cold int init(AVCodecParserContext *s)
Definition: h264_parser.c:499
#define MKBETAG(a, b, c, d)
Definition: common.h:239
void * priv_data
Definition: avcodec.h:1092
static int encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet)
Definition: dpxenc.c:106
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:85
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:191
#define LIBAVCODEC_IDENT
Definition: version.h:43
AVPixelFormat
Pixel format.
Definition: pixfmt.h:63
This structure stores compressed data.
Definition: avcodec.h:950