FFmpeg
pnmenc.c
Go to the documentation of this file.
1 /*
2  * PNM image format
3  * Copyright (c) 2002, 2003 Fabrice Bellard
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 "config_components.h"
23 
24 #include "libavutil/intreadwrite.h"
25 #include "libavutil/imgutils.h"
26 #include "libavutil/pixdesc.h"
27 #include "libavutil/float2half.h"
28 #include "avcodec.h"
29 #include "codec_internal.h"
30 #include "encode.h"
31 
32 typedef struct PHMEncContext {
35 
37  const AVFrame *p, int *got_packet)
38 {
39  PHMEncContext *s = avctx->priv_data;
40  uint8_t *bytestream, *bytestream_start, *bytestream_end;
41  int i, h, h1, c, n, linesize, ret;
43  avctx->width, avctx->height, 1);
44 
45  if (size < 0)
46  return size;
47 
48  if ((ret = ff_get_encode_buffer(avctx, pkt, size + 200U, 0)) < 0)
49  return ret;
50 
51  bytestream_start =
52  bytestream = pkt->data;
53  bytestream_end = pkt->data + pkt->size;
54 
55  h = avctx->height;
56  h1 = h;
57  switch (avctx->pix_fmt) {
59  c = '4';
60  n = (avctx->width + 7) >> 3;
61  break;
62  case AV_PIX_FMT_GRAY8:
63  c = '5';
64  n = avctx->width;
65  break;
67  c = '5';
68  n = avctx->width * 2;
69  break;
70  case AV_PIX_FMT_RGB24:
71  c = '6';
72  n = avctx->width * 3;
73  break;
74  case AV_PIX_FMT_RGB48BE:
75  c = '6';
76  n = avctx->width * 6;
77  break;
78  case AV_PIX_FMT_YUV420P:
79  if (avctx->width & 1 || avctx->height & 1) {
80  av_log(avctx, AV_LOG_ERROR, "pgmyuv needs even width and height\n");
81  return AVERROR(EINVAL);
82  }
83  c = '5';
84  n = avctx->width;
85  h1 = (h * 3) / 2;
86  break;
88  c = '5';
89  n = avctx->width * 2;
90  h1 = (h * 3) / 2;
91  break;
94  if (avctx->codec_id == AV_CODEC_ID_PFM) {
95  c = 'F';
96  n = avctx->width * 4;
97  } else {
98  c = 'H';
99  n = avctx->width * 2;
100  }
101  break;
104  if (avctx->codec_id == AV_CODEC_ID_PFM) {
105  c = 'f';
106  n = avctx->width * 4;
107  } else {
108  c = 'h';
109  n = avctx->width * 2;
110  }
111  break;
112  default:
113  return -1;
114  }
115  snprintf(bytestream, bytestream_end - bytestream,
116  "P%c\n%d %d\n", c, avctx->width, h1);
117  bytestream += strlen(bytestream);
118  if (avctx->pix_fmt == AV_PIX_FMT_GBRPF32LE ||
119  avctx->pix_fmt == AV_PIX_FMT_GRAYF32LE ||
120  avctx->pix_fmt == AV_PIX_FMT_GBRPF32BE ||
121  avctx->pix_fmt == AV_PIX_FMT_GRAYF32BE)
122  snprintf(bytestream, bytestream_end - bytestream,
123  "%f\n", (avctx->pix_fmt == AV_PIX_FMT_GBRPF32BE ||
124  avctx->pix_fmt == AV_PIX_FMT_GRAYF32BE) ? 1.f: -1.f);
125  bytestream += strlen(bytestream);
126  if (avctx->pix_fmt != AV_PIX_FMT_MONOWHITE &&
127  avctx->pix_fmt != AV_PIX_FMT_GBRPF32LE &&
128  avctx->pix_fmt != AV_PIX_FMT_GRAYF32LE &&
129  avctx->pix_fmt != AV_PIX_FMT_GBRPF32BE &&
130  avctx->pix_fmt != AV_PIX_FMT_GRAYF32BE) {
131  int maxdepth = (1 << av_pix_fmt_desc_get(avctx->pix_fmt)->comp[0].depth) - 1;
132  snprintf(bytestream, bytestream_end - bytestream,
133  "%d\n", maxdepth);
134  bytestream += strlen(bytestream);
135  }
136 
137  if ((avctx->pix_fmt == AV_PIX_FMT_GBRPF32LE ||
138  avctx->pix_fmt == AV_PIX_FMT_GBRPF32BE) && c == 'F') {
139  /* PFM is encoded from bottom to top */
140  const float *r = (const float *)(p->data[2] + p->linesize[2] * (avctx->height - 1));
141  const float *g = (const float *)(p->data[0] + p->linesize[0] * (avctx->height - 1));
142  const float *b = (const float *)(p->data[1] + p->linesize[1] * (avctx->height - 1));
143 
144  for (int i = 0; i < avctx->height; i++) {
145  for (int j = 0; j < avctx->width; j++) {
146  AV_WN32(bytestream + 0, av_float2int(r[j]));
147  AV_WN32(bytestream + 4, av_float2int(g[j]));
148  AV_WN32(bytestream + 8, av_float2int(b[j]));
149  bytestream += 12;
150  }
151 
152  r -= p->linesize[2] / 4;
153  g -= p->linesize[0] / 4;
154  b -= p->linesize[1] / 4;
155  }
156  } else if ((avctx->pix_fmt == AV_PIX_FMT_GRAYF32LE ||
157  avctx->pix_fmt == AV_PIX_FMT_GRAYF32BE) && c == 'f') {
158  /* PFM is encoded from bottom to top */
159  const float *g = (const float *)(p->data[0] + p->linesize[0] * (avctx->height - 1));
160 
161  for (int i = 0; i < avctx->height; i++) {
162  for (int j = 0; j < avctx->width; j++) {
163  AV_WN32(bytestream, av_float2int(g[j]));
164  bytestream += 4;
165  }
166 
167  g -= p->linesize[0] / 4;
168  }
169  } else if (avctx->pix_fmt == AV_PIX_FMT_GBRPF32 && c == 'H') {
170  const float *r = (const float *)p->data[2];
171  const float *g = (const float *)p->data[0];
172  const float *b = (const float *)p->data[1];
173 
174  for (int i = 0; i < avctx->height; i++) {
175  for (int j = 0; j < avctx->width; j++) {
176  AV_WN16(bytestream + 0, float2half(av_float2int(r[j]), &s->f2h_tables));
177  AV_WN16(bytestream + 2, float2half(av_float2int(g[j]), &s->f2h_tables));
178  AV_WN16(bytestream + 4, float2half(av_float2int(b[j]), &s->f2h_tables));
179  bytestream += 6;
180  }
181 
182  r += p->linesize[2] / 4;
183  g += p->linesize[0] / 4;
184  b += p->linesize[1] / 4;
185  }
186  } else if (avctx->pix_fmt == AV_PIX_FMT_GRAYF32 && c == 'h') {
187  const float *g = (const float *)p->data[0];
188 
189  for (int i = 0; i < avctx->height; i++) {
190  for (int j = 0; j < avctx->width; j++) {
191  AV_WN16(bytestream, float2half(av_float2int(g[j]), &s->f2h_tables));
192  bytestream += 2;
193  }
194 
195  g += p->linesize[0] / 4;
196  }
197  } else {
198  const uint8_t *ptr = p->data[0];
199  linesize = p->linesize[0];
200  for (i = 0; i < h; i++) {
201  memcpy(bytestream, ptr, n);
202  bytestream += n;
203  ptr += linesize;
204  }
205  }
206 
207  if (avctx->pix_fmt == AV_PIX_FMT_YUV420P || avctx->pix_fmt == AV_PIX_FMT_YUV420P16BE) {
208  const uint8_t *ptr1 = p->data[1], *ptr2 = p->data[2];
209  h >>= 1;
210  n >>= 1;
211  for (i = 0; i < h; i++) {
212  memcpy(bytestream, ptr1, n);
213  bytestream += n;
214  memcpy(bytestream, ptr2, n);
215  bytestream += n;
216  ptr1 += p->linesize[1];
217  ptr2 += p->linesize[2];
218  }
219  }
220  av_shrink_packet(pkt, bytestream - bytestream_start);
221  *got_packet = 1;
222 
223  return 0;
224 }
225 
226 #if CONFIG_PGM_ENCODER
227 const FFCodec ff_pgm_encoder = {
228  .p.name = "pgm",
229  CODEC_LONG_NAME("PGM (Portable GrayMap) image"),
230  .p.type = AVMEDIA_TYPE_VIDEO,
231  .p.id = AV_CODEC_ID_PGM,
234  .p.pix_fmts = (const enum AVPixelFormat[]){
236  },
237 };
238 #endif
239 
240 #if CONFIG_PGMYUV_ENCODER
241 const FFCodec ff_pgmyuv_encoder = {
242  .p.name = "pgmyuv",
243  CODEC_LONG_NAME("PGMYUV (Portable GrayMap YUV) image"),
244  .p.type = AVMEDIA_TYPE_VIDEO,
245  .p.id = AV_CODEC_ID_PGMYUV,
248  .p.pix_fmts = (const enum AVPixelFormat[]){
250  },
251 };
252 #endif
253 
254 #if CONFIG_PPM_ENCODER
255 const FFCodec ff_ppm_encoder = {
256  .p.name = "ppm",
257  CODEC_LONG_NAME("PPM (Portable PixelMap) image"),
258  .p.type = AVMEDIA_TYPE_VIDEO,
259  .p.id = AV_CODEC_ID_PPM,
262  .p.pix_fmts = (const enum AVPixelFormat[]){
264  },
265 };
266 #endif
267 
268 #if CONFIG_PBM_ENCODER
269 const FFCodec ff_pbm_encoder = {
270  .p.name = "pbm",
271  CODEC_LONG_NAME("PBM (Portable BitMap) image"),
272  .p.type = AVMEDIA_TYPE_VIDEO,
273  .p.id = AV_CODEC_ID_PBM,
276  .p.pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_MONOWHITE,
277  AV_PIX_FMT_NONE },
278 };
279 #endif
280 
281 #if CONFIG_PFM_ENCODER
282 const FFCodec ff_pfm_encoder = {
283  .p.name = "pfm",
284  CODEC_LONG_NAME("PFM (Portable FloatMap) image"),
285  .p.type = AVMEDIA_TYPE_VIDEO,
286  .p.id = AV_CODEC_ID_PFM,
289  .p.pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_GBRPF32LE,
293  AV_PIX_FMT_NONE },
294 };
295 #endif
296 
297 #if CONFIG_PHM_ENCODER
298 static av_cold int phm_enc_init(AVCodecContext *avctx)
299 {
300  PHMEncContext *s = avctx->priv_data;
301 
302  ff_init_float2half_tables(&s->f2h_tables);
303 
304  return 0;
305 }
306 
307 const FFCodec ff_phm_encoder = {
308  .p.name = "phm",
309  CODEC_LONG_NAME("PHM (Portable HalfFloatMap) image"),
310  .p.type = AVMEDIA_TYPE_VIDEO,
311  .p.id = AV_CODEC_ID_PHM,
313  .priv_data_size = sizeof(PHMEncContext),
314  .init = phm_enc_init,
316  .p.pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_GBRPF32,
318  AV_PIX_FMT_NONE },
319 };
320 #endif
ff_pbm_encoder
const FFCodec ff_pbm_encoder
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:71
r
const char * r
Definition: vf_curves.c:126
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
AV_CODEC_ID_PBM
@ AV_CODEC_ID_PBM
Definition: codec_id.h:115
Float2HalfTables
Definition: float2half.h:27
ff_init_float2half_tables
void ff_init_float2half_tables(Float2HalfTables *t)
Definition: float2half.c:21
av_pix_fmt_desc_get
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2962
AV_CODEC_ID_PFM
@ AV_CODEC_ID_PFM
Definition: codec_id.h:303
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:340
pixdesc.h
AV_PIX_FMT_GBRPF32BE
@ AV_PIX_FMT_GBRPF32BE
IEEE-754 single precision planar GBR 4:4:4, 96bpp, big-endian.
Definition: pixfmt.h:341
AVPacket::data
uint8_t * data
Definition: packet.h:522
AV_CODEC_ID_PPM
@ AV_CODEC_ID_PPM
Definition: codec_id.h:114
AVComponentDescriptor::depth
int depth
Number of bits in the component.
Definition: pixdesc.h:57
encode.h
b
#define b
Definition: input.c:41
AV_CODEC_ID_PGM
@ AV_CODEC_ID_PGM
Definition: codec_id.h:116
AV_PIX_FMT_MONOWHITE
@ AV_PIX_FMT_MONOWHITE
Y , 1bpp, 0 is white, 1 is black, in each byte pixels are ordered from the msb to the lsb.
Definition: pixfmt.h:82
FFCodec
Definition: codec_internal.h:127
av_float2int
static av_always_inline uint32_t av_float2int(float f)
Reinterpret a float as a 32-bit integer.
Definition: intfloat.h:50
AV_PIX_FMT_GRAYF32LE
@ AV_PIX_FMT_GRAYF32LE
IEEE-754 single precision Y, 32bpp, little-endian.
Definition: pixfmt.h:364
AV_CODEC_ID_PHM
@ AV_CODEC_ID_PHM
Definition: codec_id.h:315
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:361
AV_PIX_FMT_GRAY16BE
@ AV_PIX_FMT_GRAY16BE
Y , 16bpp, big-endian.
Definition: pixfmt.h:104
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
av_shrink_packet
void av_shrink_packet(AVPacket *pkt, int size)
Reduce packet size, correctly zeroing padding.
Definition: avpacket.c:113
FF_CODEC_ENCODE_CB
#define FF_CODEC_ENCODE_CB(func)
Definition: codec_internal.h:296
pkt
AVPacket * pkt
Definition: movenc.c:59
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
av_cold
#define av_cold
Definition: attributes.h:90
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:198
g
const char * g
Definition: vf_curves.c:127
AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE
#define AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE
This encoder can reorder user opaque values from input AVFrames and return them with corresponding ou...
Definition: codec.h:159
AV_PIX_FMT_YUV420P
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:73
AV_PIX_FMT_GRAYF32
#define AV_PIX_FMT_GRAYF32
Definition: pixfmt.h:511
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:272
AVCodecContext::codec_id
enum AVCodecID codec_id
Definition: avcodec.h:455
PHMEncContext
Definition: pnmenc.c:32
ff_pfm_encoder
const FFCodec ff_pfm_encoder
AV_CODEC_ID_PGMYUV
@ AV_CODEC_ID_PGMYUV
Definition: codec_id.h:117
ff_phm_encoder
const FFCodec ff_phm_encoder
AV_PIX_FMT_GRAY8
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition: pixfmt.h:81
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
ff_ppm_encoder
const FFCodec ff_ppm_encoder
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts.c:365
AV_PIX_FMT_RGB24
@ AV_PIX_FMT_RGB24
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:75
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
AVPacket::size
int size
Definition: packet.h:523
codec_internal.h
AV_WN32
#define AV_WN32(p, v)
Definition: intreadwrite.h:374
for
for(k=2;k<=8;++k)
Definition: h264pred_template.c:425
AV_PIX_FMT_GBRPF32
#define AV_PIX_FMT_GBRPF32
Definition: pixfmt.h:508
size
int size
Definition: twinvq_data.h:10344
ff_pgmyuv_encoder
const FFCodec ff_pgmyuv_encoder
av_image_get_buffer_size
int av_image_get_buffer_size(enum AVPixelFormat pix_fmt, int width, int height, int align)
Return the size in bytes of the amount of data required to store an image with the given parameters.
Definition: imgutils.c:466
AV_PIX_FMT_RGB48BE
@ 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
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:255
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:194
AVCodecContext::height
int height
Definition: avcodec.h:618
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:657
AV_PIX_FMT_GBRPF32LE
@ AV_PIX_FMT_GBRPF32LE
IEEE-754 single precision planar GBR 4:4:4, 96bpp, little-endian.
Definition: pixfmt.h:342
avcodec.h
ff_pgm_encoder
const FFCodec ff_pgm_encoder
ret
ret
Definition: filter_design.txt:187
PHMEncContext::f2h_tables
Float2HalfTables f2h_tables
Definition: pnmenc.c:33
AV_PIX_FMT_GRAYF32BE
@ AV_PIX_FMT_GRAYF32BE
IEEE-754 single precision Y, 32bpp, big-endian.
Definition: pixfmt.h:363
float2half
static uint16_t float2half(uint32_t f, const Float2HalfTables *t)
Definition: float2half.h:38
U
#define U(x)
Definition: vpx_arith.h:37
AVCodecContext
main external API structure.
Definition: avcodec.h:445
ff_get_encode_buffer
int ff_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int flags)
Get a buffer for a packet.
Definition: encode.c:105
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:72
AVPixFmtDescriptor::comp
AVComponentDescriptor comp[4]
Parameters that describe how pixels are packed.
Definition: pixdesc.h:105
AV_PIX_FMT_YUV420P16BE
@ AV_PIX_FMT_YUV420P16BE
planar YUV 4:2:0, 24bpp, (1 Cr & Cb sample per 2x2 Y samples), big-endian
Definition: pixfmt.h:129
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:472
AVPacket
This structure stores compressed data.
Definition: packet.h:499
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:618
imgutils.h
AVFrame::linesize
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:385
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
pnm_encode_frame
static int pnm_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *p, int *got_packet)
Definition: pnmenc.c:36
h
h
Definition: vp9dsp_template.c:2038
snprintf
#define snprintf
Definition: snprintf.h:34
float2half.h
AV_WN16
#define AV_WN16(p, v)
Definition: intreadwrite.h:370