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 {
33 } DPXContext;
34 
36 {
37  DPXContext *s = avctx->priv_data;
38 
39  avctx->coded_frame = &s->picture;
41  avctx->coded_frame->key_frame = 1;
42 
43  s->big_endian = 1;
44  s->bits_per_component = 8;
45  s->descriptor = 50; /* RGB */
46 
47  switch (avctx->pix_fmt) {
48  case AV_PIX_FMT_RGB24:
49  break;
50  case AV_PIX_FMT_RGBA:
51  s->descriptor = 51; /* RGBA */
52  break;
53  case AV_PIX_FMT_RGB48LE:
54  s->big_endian = 0;
55  case AV_PIX_FMT_RGB48BE:
57  break;
58  default:
59  av_log(avctx, AV_LOG_INFO, "unsupported pixel format\n");
60  return -1;
61  }
62 
63  return 0;
64 }
65 
66 #define write16(p, value) \
67 do { \
68  if (s->big_endian) AV_WB16(p, value); \
69  else AV_WL16(p, value); \
70 } while(0)
71 
72 #define write32(p, value) \
73 do { \
74  if (s->big_endian) AV_WB32(p, value); \
75  else AV_WL32(p, value); \
76 } while(0)
77 
78 static void encode_rgb48_10bit(AVCodecContext *avctx, const AVPicture *pic,
79  uint8_t *dst)
80 {
81  DPXContext *s = avctx->priv_data;
82  const uint8_t *src = pic->data[0];
83  int x, y;
84 
85  for (y = 0; y < avctx->height; y++) {
86  for (x = 0; x < avctx->width; x++) {
87  int value;
88  if ((avctx->pix_fmt & 1)) {
89  value = ((AV_RB16(src + 6*x + 4) & 0xFFC0) >> 4)
90  | ((AV_RB16(src + 6*x + 2) & 0xFFC0) << 6)
91  | ((AV_RB16(src + 6*x + 0) & 0xFFC0) << 16);
92  } else {
93  value = ((AV_RL16(src + 6*x + 4) & 0xFFC0) >> 4)
94  | ((AV_RL16(src + 6*x + 2) & 0xFFC0) << 6)
95  | ((AV_RL16(src + 6*x + 0) & 0xFFC0) << 16);
96  }
97  write32(dst, value);
98  dst += 4;
99  }
100  src += pic->linesize[0];
101  }
102 }
103 
104 static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
105  const AVFrame *frame, int *got_packet)
106 {
107  DPXContext *s = avctx->priv_data;
108  int size, ret;
109  uint8_t *buf;
110 
111 #define HEADER_SIZE 1664 /* DPX Generic header */
112  if (s->bits_per_component == 10)
113  size = avctx->height * avctx->width * 4;
114  else
115  size = avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height);
116  if ((ret = ff_alloc_packet(pkt, size + HEADER_SIZE)) < 0) {
117  av_log(avctx, AV_LOG_ERROR, "Error getting output packet.\n");
118  return ret;
119  }
120  buf = pkt->data;
121 
122  memset(buf, 0, HEADER_SIZE);
123 
124  /* File information header */
125  write32(buf, MKBETAG('S','D','P','X'));
126  write32(buf + 4, HEADER_SIZE);
127  memcpy (buf + 8, "V1.0", 4);
128  write32(buf + 20, 1); /* new image */
129  write32(buf + 24, HEADER_SIZE);
130  if (!(avctx->flags & CODEC_FLAG_BITEXACT))
131  memcpy (buf + 160, LIBAVCODEC_IDENT, FFMIN(sizeof(LIBAVCODEC_IDENT), 100));
132  write32(buf + 660, 0xFFFFFFFF); /* unencrypted */
133 
134  /* Image information header */
135  write16(buf + 768, 0); /* orientation; left to right, top to bottom */
136  write16(buf + 770, 1); /* number of elements */
137  write32(buf + 772, avctx->width);
138  write32(buf + 776, avctx->height);
139  buf[800] = s->descriptor;
140  buf[801] = 2; /* linear transfer */
141  buf[802] = 2; /* linear colorimetric */
142  buf[803] = s->bits_per_component;
143  write16(buf + 804, s->bits_per_component == 10 ? 1 : 0); /* packing method */
144 
145  /* Image source information header */
146  write32(buf + 1628, avctx->sample_aspect_ratio.num);
147  write32(buf + 1632, avctx->sample_aspect_ratio.den);
148 
149  switch (s->bits_per_component) {
150  case 8:
151  case 16:
152  size = avpicture_layout((const AVPicture*)frame, avctx->pix_fmt,
153  avctx->width, avctx->height,
154  buf + HEADER_SIZE, pkt->size - HEADER_SIZE);
155  if (size < 0)
156  return size;
157  break;
158  case 10:
159  encode_rgb48_10bit(avctx, (const AVPicture*)frame, buf + HEADER_SIZE);
160  break;
161  default:
162  av_log(avctx, AV_LOG_ERROR, "Unsupported bit depth: %d\n", s->bits_per_component);
163  return -1;
164  }
165 
166  size += HEADER_SIZE;
167 
168  write32(buf + 16, size); /* file size */
169 
170  pkt->flags |= AV_PKT_FLAG_KEY;
171  *got_packet = 1;
172 
173  return 0;
174 }
175 
177  .name = "dpx",
178  .type = AVMEDIA_TYPE_VIDEO,
179  .id = AV_CODEC_ID_DPX,
180  .priv_data_size = sizeof(DPXContext),
181  .init = encode_init,
182  .encode2 = encode_frame,
183  .pix_fmts = (const enum AVPixelFormat[]){
189  .long_name = NULL_IF_CONFIG_SMALL("DPX image"),
190 };
#define write32(p, value)
Definition: dpxenc.c:72
int size
int linesize[AV_NUM_DATA_POINTERS]
number of bytes per line
Definition: avcodec.h:3155
This structure describes decoded (raw) audio or video data.
Definition: avcodec.h:989
int big_endian
Definition: dpxenc.c:30
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:2725
struct DPXContext DPXContext
static av_cold int encode_init(AVCodecContext *avctx)
Definition: dpxenc.c:35
int num
numerator
Definition: rational.h:44
int size
Definition: avcodec.h:916
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:1724
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1533
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:2711
four components are given, that's all.
Definition: avcodec.h:3153
AVCodec.
Definition: avcodec.h:2960
uint8_t
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:109
uint8_t * data[AV_NUM_DATA_POINTERS]
Definition: avcodec.h:3154
uint8_t * data
Definition: avcodec.h:915
static int init(AVCodecParserContext *s)
Definition: h264_parser.c:335
Definition: dpx.c:28
#define HEADER_SIZE
#define AV_RB16
Definition: intreadwrite.h:53
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:88
int flags
CODEC_FLAG_*.
Definition: avcodec.h:1434
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:146
const char * name
Name of the codec implementation.
Definition: avcodec.h:2967
int bits_per_component
Definition: dpxenc.c:31
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:94
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:921
enum AVPictureType pict_type
Picture type of the frame, see ?_TYPE below.
Definition: avcodec.h:1065
int width
picture width / height.
Definition: avcodec.h:1508
AVCodec ff_dpx_encoder
Definition: dpxenc.c:176
int ff_alloc_packet(AVPacket *avpkt, int size)
Check AVPacket size and/or allocate data.
Definition: utils.c:880
int descriptor
Definition: dpxenc.c:32
external API header
main external API structure.
Definition: avcodec.h:1339
static void encode_rgb48_10bit(AVCodecContext *avctx, const AVPicture *pic, uint8_t *dst)
Definition: dpxenc.c:78
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:108
#define write16(p, value)
Definition: dpxenc.c:66
int den
denominator
Definition: rational.h:45
void * priv_data
Definition: avcodec.h:1382
static int encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet)
Definition: dpxenc.c:104
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: avcodec.h:1058
AVFrame picture
Definition: dpx.c:29
AVPixelFormat
Pixel format.
Definition: pixfmt.h:63
This structure stores compressed data.
Definition: avcodec.h:898