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 "libavutil/intreadwrite.h"
23 #include "libavutil/imgutils.h"
24 #include "libavutil/pixdesc.h"
25 #include "avcodec.h"
26 #include "encode.h"
27 #include "internal.h"
28 
30  const AVFrame *p, int *got_packet)
31 {
32  uint8_t *bytestream, *bytestream_start, *bytestream_end;
33  int i, h, h1, c, n, linesize, ret;
34  uint8_t *ptr, *ptr1, *ptr2;
36  avctx->width, avctx->height, 1);
37 
38  if ((ret = ff_get_encode_buffer(avctx, pkt, size + 200, 0)) < 0)
39  return ret;
40 
41  bytestream_start =
42  bytestream = pkt->data;
43  bytestream_end = pkt->data + pkt->size;
44 
45  h = avctx->height;
46  h1 = h;
47  switch (avctx->pix_fmt) {
49  c = '4';
50  n = (avctx->width + 7) >> 3;
51  break;
52  case AV_PIX_FMT_GRAY8:
53  c = '5';
54  n = avctx->width;
55  break;
57  c = '5';
58  n = avctx->width * 2;
59  break;
60  case AV_PIX_FMT_RGB24:
61  c = '6';
62  n = avctx->width * 3;
63  break;
64  case AV_PIX_FMT_RGB48BE:
65  c = '6';
66  n = avctx->width * 6;
67  break;
68  case AV_PIX_FMT_YUV420P:
69  if (avctx->width & 1 || avctx->height & 1) {
70  av_log(avctx, AV_LOG_ERROR, "pgmyuv needs even width and height\n");
71  return AVERROR(EINVAL);
72  }
73  c = '5';
74  n = avctx->width;
75  h1 = (h * 3) / 2;
76  break;
78  c = '5';
79  n = avctx->width * 2;
80  h1 = (h * 3) / 2;
81  break;
82  case AV_PIX_FMT_GBRPF32:
83  c = 'F';
84  n = avctx->width * 4;
85  break;
86  default:
87  return -1;
88  }
89  snprintf(bytestream, bytestream_end - bytestream,
90  "P%c\n%d %d\n", c, avctx->width, h1);
91  bytestream += strlen(bytestream);
92  if (avctx->pix_fmt == AV_PIX_FMT_GBRPF32)
93  snprintf(bytestream, bytestream_end - bytestream,
94  "%f\n", avctx->pix_fmt == AV_PIX_FMT_GBRPF32BE ? 1.f: -1.f);
95  bytestream += strlen(bytestream);
96  if (avctx->pix_fmt != AV_PIX_FMT_MONOWHITE &&
97  avctx->pix_fmt != AV_PIX_FMT_GBRPF32) {
98  int maxdepth = (1 << av_pix_fmt_desc_get(avctx->pix_fmt)->comp[0].depth) - 1;
99  snprintf(bytestream, bytestream_end - bytestream,
100  "%d\n", maxdepth);
101  bytestream += strlen(bytestream);
102  }
103 
104  if (avctx->pix_fmt == AV_PIX_FMT_GBRPF32) {
105  float *r = (float *)p->data[2];
106  float *g = (float *)p->data[0];
107  float *b = (float *)p->data[1];
108 
109  for (int i = 0; i < avctx->height; i++) {
110  for (int j = 0; j < avctx->width; j++) {
111  AV_WN32(bytestream + 0, av_float2int(r[j]));
112  AV_WN32(bytestream + 4, av_float2int(g[j]));
113  AV_WN32(bytestream + 8, av_float2int(b[j]));
114  bytestream += 12;
115  }
116 
117  r += p->linesize[2] / 4;
118  g += p->linesize[0] / 4;
119  b += p->linesize[1] / 4;
120  }
121  } else {
122  ptr = p->data[0];
123  linesize = p->linesize[0];
124  for (i = 0; i < h; i++) {
125  memcpy(bytestream, ptr, n);
126  bytestream += n;
127  ptr += linesize;
128  }
129  }
130 
131  if (avctx->pix_fmt == AV_PIX_FMT_YUV420P || avctx->pix_fmt == AV_PIX_FMT_YUV420P16BE) {
132  h >>= 1;
133  n >>= 1;
134  ptr1 = p->data[1];
135  ptr2 = p->data[2];
136  for (i = 0; i < h; i++) {
137  memcpy(bytestream, ptr1, n);
138  bytestream += n;
139  memcpy(bytestream, ptr2, n);
140  bytestream += n;
141  ptr1 += p->linesize[1];
142  ptr2 += p->linesize[2];
143  }
144  }
145  av_shrink_packet(pkt, bytestream - bytestream_start);
146  *got_packet = 1;
147 
148  return 0;
149 }
150 
151 #if CONFIG_PGM_ENCODER
152 const AVCodec ff_pgm_encoder = {
153  .name = "pgm",
154  .long_name = NULL_IF_CONFIG_SMALL("PGM (Portable GrayMap) image"),
155  .type = AVMEDIA_TYPE_VIDEO,
156  .id = AV_CODEC_ID_PGM,
157  .capabilities = AV_CODEC_CAP_DR1,
158  .encode2 = pnm_encode_frame,
159  .pix_fmts = (const enum AVPixelFormat[]){
161  },
162  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
163 };
164 #endif
165 
166 #if CONFIG_PGMYUV_ENCODER
167 const AVCodec ff_pgmyuv_encoder = {
168  .name = "pgmyuv",
169  .long_name = NULL_IF_CONFIG_SMALL("PGMYUV (Portable GrayMap YUV) image"),
170  .type = AVMEDIA_TYPE_VIDEO,
171  .id = AV_CODEC_ID_PGMYUV,
172  .capabilities = AV_CODEC_CAP_DR1,
173  .encode2 = pnm_encode_frame,
174  .pix_fmts = (const enum AVPixelFormat[]){
176  },
177  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
178 };
179 #endif
180 
181 #if CONFIG_PPM_ENCODER
182 const AVCodec ff_ppm_encoder = {
183  .name = "ppm",
184  .long_name = NULL_IF_CONFIG_SMALL("PPM (Portable PixelMap) image"),
185  .type = AVMEDIA_TYPE_VIDEO,
186  .id = AV_CODEC_ID_PPM,
187  .capabilities = AV_CODEC_CAP_DR1,
188  .encode2 = pnm_encode_frame,
189  .pix_fmts = (const enum AVPixelFormat[]){
191  },
192  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
193 };
194 #endif
195 
196 #if CONFIG_PBM_ENCODER
197 const AVCodec ff_pbm_encoder = {
198  .name = "pbm",
199  .long_name = NULL_IF_CONFIG_SMALL("PBM (Portable BitMap) image"),
200  .type = AVMEDIA_TYPE_VIDEO,
201  .id = AV_CODEC_ID_PBM,
202  .capabilities = AV_CODEC_CAP_DR1,
203  .encode2 = pnm_encode_frame,
204  .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_MONOWHITE,
205  AV_PIX_FMT_NONE },
206  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
207 };
208 #endif
209 
210 #if CONFIG_PFM_ENCODER
211 const AVCodec ff_pfm_encoder = {
212  .name = "pfm",
213  .long_name = NULL_IF_CONFIG_SMALL("PFM (Portable FloatMap) image"),
214  .type = AVMEDIA_TYPE_VIDEO,
215  .id = AV_CODEC_ID_PFM,
216  .capabilities = AV_CODEC_CAP_DR1,
217  .encode2 = pnm_encode_frame,
218  .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_GBRPF32,
219  AV_PIX_FMT_NONE },
220  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
221 };
222 #endif
AVCodec
AVCodec.
Definition: codec.h:202
FF_CODEC_CAP_INIT_THREADSAFE
#define FF_CODEC_CAP_INIT_THREADSAFE
The codec does not modify any global variables in the init function, allowing to call the init functi...
Definition: internal.h:42
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
r
const char * r
Definition: vf_curves.c:116
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:113
av_pix_fmt_desc_get
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2660
ff_pfm_encoder
const AVCodec ff_pfm_encoder
AV_CODEC_ID_PFM
@ AV_CODEC_ID_PFM
Definition: codec_id.h:302
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:317
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:308
internal.h
AVPacket::data
uint8_t * data
Definition: packet.h:373
AV_CODEC_ID_PPM
@ AV_CODEC_ID_PPM
Definition: codec_id.h:112
AVComponentDescriptor::depth
int depth
Number of bits in the component.
Definition: pixdesc.h:57
encode.h
b
#define b
Definition: input.c:40
AV_CODEC_ID_PGM
@ AV_CODEC_ID_PGM
Definition: codec_id.h:114
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:75
av_float2int
static av_always_inline uint32_t av_float2int(float f)
Reinterpret a float as a 32-bit integer.
Definition: intfloat.h:50
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:338
AV_PIX_FMT_GRAY16BE
@ AV_PIX_FMT_GRAY16BE
Y , 16bpp, big-endian.
Definition: pixfmt.h:97
av_shrink_packet
void av_shrink_packet(AVPacket *pkt, int size)
Reduce packet size, correctly zeroing padding.
Definition: avpacket.c:114
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
intreadwrite.h
g
const char * g
Definition: vf_curves.c:117
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:66
AV_CODEC_ID_PGMYUV
@ AV_CODEC_ID_PGMYUV
Definition: codec_id.h:115
AV_PIX_FMT_GRAY8
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition: pixfmt.h:74
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
for
for(j=16;j >0;--j)
Definition: h264pred_template.c:469
AV_PIX_FMT_RGB24
@ AV_PIX_FMT_RGB24
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:68
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:374
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
AV_WN32
#define AV_WN32(p, v)
Definition: intreadwrite.h:376
AV_PIX_FMT_GBRPF32
#define AV_PIX_FMT_GBRPF32
Definition: pixfmt.h:433
size
int size
Definition: twinvq_data.h:10344
ff_pgmyuv_encoder
const AVCodec 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:102
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:271
ff_ppm_encoder
const AVCodec ff_ppm_encoder
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:209
AVCodecContext::height
int height
Definition: avcodec.h:556
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:593
avcodec.h
ret
ret
Definition: filter_design.txt:187
ff_pgm_encoder
const AVCodec ff_pgm_encoder
AVCodecContext
main external API structure.
Definition: avcodec.h:383
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:78
ff_pbm_encoder
const AVCodec ff_pbm_encoder
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
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:122
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
AVPacket
This structure stores compressed data.
Definition: packet.h:350
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:556
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:362
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
pnm_encode_frame
static int pnm_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *p, int *got_packet)
Definition: pnmenc.c:29
h
h
Definition: vp9dsp_template.c:2038
snprintf
#define snprintf
Definition: snprintf.h:34