FFmpeg
Loading...
Searching...
No Matches
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
25#include "libavutil/imgutils.h"
26#include "libavutil/pixdesc.h"
28#include "libavutil/intfloat.h"
29#include "avcodec.h"
30#include "codec_internal.h"
31#include "encode.h"
32
36
38 const AVFrame *p, int *got_packet)
39{
40 PHMEncContext *s = avctx->priv_data;
41 uint8_t *bytestream, *bytestream_start, *bytestream_end;
42 int i, h, h1, c, n, linesize, ret;
44 avctx->width, avctx->height, 1);
45
46 if (size < 0)
47 return size;
48
49 if ((ret = ff_get_encode_buffer(avctx, pkt, size + 200U, 0)) < 0)
50 return ret;
51
52 bytestream_start =
53 bytestream = pkt->data;
54 bytestream_end = pkt->data + pkt->size;
55
56 h = avctx->height;
57 h1 = h;
58 switch (avctx->pix_fmt) {
60 c = '4';
61 n = (avctx->width + 7) >> 3;
62 break;
64 c = '5';
65 n = avctx->width;
66 break;
68 c = '5';
69 n = avctx->width * 2;
70 break;
72 c = '6';
73 n = avctx->width * 3;
74 break;
76 c = '6';
77 n = avctx->width * 6;
78 break;
80 if (avctx->width & 1 || avctx->height & 1) {
81 av_log(avctx, AV_LOG_ERROR, "pgmyuv needs even width and height\n");
82 return AVERROR(EINVAL);
83 }
84 c = '5';
85 n = avctx->width;
86 h1 = (h * 3) / 2;
87 break;
89 c = '5';
90 n = avctx->width * 2;
91 h1 = (h * 3) / 2;
92 break;
95 if (avctx->codec_id == AV_CODEC_ID_PFM) {
96 c = 'F';
97 n = avctx->width * 4;
98 } else {
99 c = 'H';
100 n = avctx->width * 2;
101 }
102 break;
105 if (avctx->codec_id == AV_CODEC_ID_PFM) {
106 c = 'f';
107 n = avctx->width * 4;
108 } else {
109 c = 'h';
110 n = avctx->width * 2;
111 }
112 break;
113 default:
114 return -1;
115 }
116 snprintf(bytestream, bytestream_end - bytestream,
117 "P%c\n%d %d\n", c, avctx->width, h1);
118 bytestream += strlen(bytestream);
119 if (avctx->pix_fmt == AV_PIX_FMT_GBRPF32LE ||
120 avctx->pix_fmt == AV_PIX_FMT_GRAYF32LE ||
121 avctx->pix_fmt == AV_PIX_FMT_GBRPF32BE ||
123 snprintf(bytestream, bytestream_end - bytestream,
124 "%f\n", (avctx->pix_fmt == AV_PIX_FMT_GBRPF32BE ||
125 avctx->pix_fmt == AV_PIX_FMT_GRAYF32BE) ? 1.f: -1.f);
126 bytestream += strlen(bytestream);
127 if (avctx->pix_fmt != AV_PIX_FMT_MONOWHITE &&
128 avctx->pix_fmt != AV_PIX_FMT_GBRPF32LE &&
129 avctx->pix_fmt != AV_PIX_FMT_GRAYF32LE &&
130 avctx->pix_fmt != AV_PIX_FMT_GBRPF32BE &&
131 avctx->pix_fmt != AV_PIX_FMT_GRAYF32BE) {
132 int maxdepth = (1 << av_pix_fmt_desc_get(avctx->pix_fmt)->comp[0].depth) - 1;
133 snprintf(bytestream, bytestream_end - bytestream,
134 "%d\n", maxdepth);
135 bytestream += strlen(bytestream);
136 }
137
138 if ((avctx->pix_fmt == AV_PIX_FMT_GBRPF32LE ||
139 avctx->pix_fmt == AV_PIX_FMT_GBRPF32BE) && c == 'F') {
140 /* PFM is encoded from bottom to top */
141 const float *r = (const float *)(p->data[2] + p->linesize[2] * (avctx->height - 1));
142 const float *g = (const float *)(p->data[0] + p->linesize[0] * (avctx->height - 1));
143 const float *b = (const float *)(p->data[1] + p->linesize[1] * (avctx->height - 1));
144
145 for (int i = 0; i < avctx->height; i++) {
146 for (int j = 0; j < avctx->width; j++) {
147 AV_WN32(bytestream + 0, av_float2int(r[j]));
148 AV_WN32(bytestream + 4, av_float2int(g[j]));
149 AV_WN32(bytestream + 8, av_float2int(b[j]));
150 bytestream += 12;
151 }
152
153 r -= p->linesize[2] / 4;
154 g -= p->linesize[0] / 4;
155 b -= p->linesize[1] / 4;
156 }
157 } else if ((avctx->pix_fmt == AV_PIX_FMT_GRAYF32LE ||
158 avctx->pix_fmt == AV_PIX_FMT_GRAYF32BE) && c == 'f') {
159 /* PFM is encoded from bottom to top */
160 const float *g = (const float *)(p->data[0] + p->linesize[0] * (avctx->height - 1));
161
162 for (int i = 0; i < avctx->height; i++) {
163 for (int j = 0; j < avctx->width; j++) {
164 AV_WN32(bytestream, av_float2int(g[j]));
165 bytestream += 4;
166 }
167
168 g -= p->linesize[0] / 4;
169 }
170 } else if (avctx->pix_fmt == AV_PIX_FMT_GBRPF32 && c == 'H') {
171 const float *r = (const float *)p->data[2];
172 const float *g = (const float *)p->data[0];
173 const float *b = (const float *)p->data[1];
174
175 for (int i = 0; i < avctx->height; i++) {
176 for (int j = 0; j < avctx->width; j++) {
177 AV_WN16(bytestream + 0, float2half(av_float2int(r[j]), &s->f2h_tables));
178 AV_WN16(bytestream + 2, float2half(av_float2int(g[j]), &s->f2h_tables));
179 AV_WN16(bytestream + 4, float2half(av_float2int(b[j]), &s->f2h_tables));
180 bytestream += 6;
181 }
182
183 r += p->linesize[2] / 4;
184 g += p->linesize[0] / 4;
185 b += p->linesize[1] / 4;
186 }
187 } else if (avctx->pix_fmt == AV_PIX_FMT_GRAYF32 && c == 'h') {
188 const float *g = (const float *)p->data[0];
189
190 for (int i = 0; i < avctx->height; i++) {
191 for (int j = 0; j < avctx->width; j++) {
192 AV_WN16(bytestream, float2half(av_float2int(g[j]), &s->f2h_tables));
193 bytestream += 2;
194 }
195
196 g += p->linesize[0] / 4;
197 }
198 } else {
199 const uint8_t *ptr = p->data[0];
200 linesize = p->linesize[0];
201 for (i = 0; i < h; i++) {
202 memcpy(bytestream, ptr, n);
203 bytestream += n;
204 ptr += linesize;
205 }
206 }
207
208 if (avctx->pix_fmt == AV_PIX_FMT_YUV420P || avctx->pix_fmt == AV_PIX_FMT_YUV420P16BE) {
209 const uint8_t *ptr1 = p->data[1], *ptr2 = p->data[2];
210 h >>= 1;
211 n >>= 1;
212 for (i = 0; i < h; i++) {
213 memcpy(bytestream, ptr1, n);
214 bytestream += n;
215 memcpy(bytestream, ptr2, n);
216 bytestream += n;
217 ptr1 += p->linesize[1];
218 ptr2 += p->linesize[2];
219 }
220 }
221 av_shrink_packet(pkt, bytestream - bytestream_start);
222 *got_packet = 1;
223
224 return 0;
225}
226
227#if CONFIG_PGM_ENCODER
228const FFCodec ff_pgm_encoder = {
229 .p.name = "pgm",
230 CODEC_LONG_NAME("PGM (Portable GrayMap) image"),
231 .p.type = AVMEDIA_TYPE_VIDEO,
232 .p.id = AV_CODEC_ID_PGM,
236};
237#endif
238
239#if CONFIG_PGMYUV_ENCODER
241 .p.name = "pgmyuv",
242 CODEC_LONG_NAME("PGMYUV (Portable GrayMap YUV) image"),
243 .p.type = AVMEDIA_TYPE_VIDEO,
244 .p.id = AV_CODEC_ID_PGMYUV,
248};
249#endif
250
251#if CONFIG_PPM_ENCODER
252const FFCodec ff_ppm_encoder = {
253 .p.name = "ppm",
254 CODEC_LONG_NAME("PPM (Portable PixelMap) image"),
255 .p.type = AVMEDIA_TYPE_VIDEO,
256 .p.id = AV_CODEC_ID_PPM,
260};
261#endif
262
263#if CONFIG_PBM_ENCODER
264const FFCodec ff_pbm_encoder = {
265 .p.name = "pbm",
266 CODEC_LONG_NAME("PBM (Portable BitMap) image"),
267 .p.type = AVMEDIA_TYPE_VIDEO,
268 .p.id = AV_CODEC_ID_PBM,
272};
273#endif
274
275#if CONFIG_PFM_ENCODER
276const FFCodec ff_pfm_encoder = {
277 .p.name = "pfm",
278 CODEC_LONG_NAME("PFM (Portable FloatMap) image"),
279 .p.type = AVMEDIA_TYPE_VIDEO,
280 .p.id = AV_CODEC_ID_PFM,
285};
286#endif
287
288#if CONFIG_PHM_ENCODER
289static av_cold int phm_enc_init(AVCodecContext *avctx)
290{
291 PHMEncContext *s = avctx->priv_data;
292
293 ff_init_float2half_tables(&s->f2h_tables);
294
295 return 0;
296}
297
298const FFCodec ff_phm_encoder = {
299 .p.name = "phm",
300 CODEC_LONG_NAME("PHM (Portable HalfFloatMap) image"),
301 .p.type = AVMEDIA_TYPE_VIDEO,
302 .p.id = AV_CODEC_ID_PHM,
304 .priv_data_size = sizeof(PHMEncContext),
305 .init = phm_enc_init,
308};
309#endif
const FFCodec ff_ppm_encoder
const FFCodec ff_pgm_encoder
const FFCodec ff_pbm_encoder
const FFCodec ff_pfm_encoder
const FFCodec ff_pgmyuv_encoder
const FFCodec ff_phm_encoder
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)
static AVPacket * pkt
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
int ff_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int flags)
Get a buffer for a packet.
Definition encode.c:106
static uint16_t float2half(uint32_t f, const Float2HalfTables *t)
Definition float2half.h:38
#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:147
#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_PGM
Definition codec_id.h:114
@ AV_CODEC_ID_PHM
Definition codec_id.h:310
@ AV_CODEC_ID_PBM
Definition codec_id.h:113
@ AV_CODEC_ID_PGMYUV
Definition codec_id.h:115
@ AV_CODEC_ID_PPM
Definition codec_id.h:112
@ AV_CODEC_ID_PFM
Definition codec_id.h:298
void av_shrink_packet(AVPacket *pkt, int size)
Reduce packet size, correctly zeroing padding.
Definition packet.c:113
#define AVERROR(e)
Definition error.h:45
#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_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
misc image utilities
#define r
Definition input.c:42
#define b
Definition input.c:43
static av_always_inline uint32_t av_float2int(float f)
Reinterpret a float as a 32-bit integer.
Definition intfloat.h:50
#define AV_WN32(p, v)
#define AV_WN16(p, v)
#define av_cold
Definition attributes.h:117
void ff_init_float2half_tables(Float2HalfTables *t)
Definition float2half.c:21
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition pixdesc.c:3460
#define AV_PIX_FMT_GBRPF32
Definition pixfmt.h:584
#define AV_PIX_FMT_GRAYF32
Definition pixfmt.h:588
@ AV_PIX_FMT_GRAY16BE
Y , 16bpp, big-endian.
Definition pixfmt.h:104
@ AV_PIX_FMT_YUV420P16BE
planar YUV 4:2:0, 24bpp, (1 Cr & Cb sample per 2x2 Y samples), big-endian
Definition pixfmt.h:129
@ AV_PIX_FMT_RGB24
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition pixfmt.h:75
@ AV_PIX_FMT_GRAYF32LE
IEEE-754 single precision Y, 32bpp, little-endian.
Definition pixfmt.h:364
@ 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_GBRPF32BE
IEEE-754 single precision planar GBR 4:4:4, 96bpp, big-endian.
Definition pixfmt.h:341
@ 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_GBRPF32LE
IEEE-754 single precision planar GBR 4:4:4, 96bpp, little-endian.
Definition pixfmt.h:342
@ AV_PIX_FMT_GRAYF32BE
IEEE-754 single precision Y, 32bpp, big-endian.
Definition pixfmt.h:363
@ 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
static int pnm_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *p, int *got_packet)
Definition pnmenc.c:37
#define snprintf
Definition snprintf.h:34
main external API structure.
Definition avcodec.h:443
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition avcodec.h:643
int width
picture width / height.
Definition avcodec.h:604
enum AVCodecID codec_id
Definition avcodec.h:453
void * priv_data
Definition avcodec.h:470
int depth
Number of bits in the component.
Definition pixdesc.h:57
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
This structure stores compressed data.
Definition packet.h:580
AVComponentDescriptor comp[4]
Parameters that describe how pixels are packed.
Definition pixdesc.h:105
Float2HalfTables f2h_tables
Definition pnmenc.c:34
#define av_log(a,...)
int size
const char * g
Definition vf_curves.c:128
static double c[64]