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 "avcodec.h"
28 #include "codec_internal.h"
29 #include "encode.h"
30 
32  const AVFrame *p, int *got_packet)
33 {
34  uint8_t *bytestream, *bytestream_start, *bytestream_end;
35  int i, h, h1, c, n, linesize, ret;
36  uint8_t *ptr, *ptr1, *ptr2;
38  avctx->width, avctx->height, 1);
39 
40  if ((ret = ff_get_encode_buffer(avctx, pkt, size + 200, 0)) < 0)
41  return ret;
42 
43  bytestream_start =
44  bytestream = pkt->data;
45  bytestream_end = pkt->data + pkt->size;
46 
47  h = avctx->height;
48  h1 = h;
49  switch (avctx->pix_fmt) {
51  c = '4';
52  n = (avctx->width + 7) >> 3;
53  break;
54  case AV_PIX_FMT_GRAY8:
55  c = '5';
56  n = avctx->width;
57  break;
59  c = '5';
60  n = avctx->width * 2;
61  break;
62  case AV_PIX_FMT_RGB24:
63  c = '6';
64  n = avctx->width * 3;
65  break;
66  case AV_PIX_FMT_RGB48BE:
67  c = '6';
68  n = avctx->width * 6;
69  break;
70  case AV_PIX_FMT_YUV420P:
71  if (avctx->width & 1 || avctx->height & 1) {
72  av_log(avctx, AV_LOG_ERROR, "pgmyuv needs even width and height\n");
73  return AVERROR(EINVAL);
74  }
75  c = '5';
76  n = avctx->width;
77  h1 = (h * 3) / 2;
78  break;
80  c = '5';
81  n = avctx->width * 2;
82  h1 = (h * 3) / 2;
83  break;
84  case AV_PIX_FMT_GBRPF32:
85  c = 'F';
86  n = avctx->width * 4;
87  break;
88  default:
89  return -1;
90  }
91  snprintf(bytestream, bytestream_end - bytestream,
92  "P%c\n%d %d\n", c, avctx->width, h1);
93  bytestream += strlen(bytestream);
94  if (avctx->pix_fmt == AV_PIX_FMT_GBRPF32)
95  snprintf(bytestream, bytestream_end - bytestream,
96  "%f\n", avctx->pix_fmt == AV_PIX_FMT_GBRPF32BE ? 1.f: -1.f);
97  bytestream += strlen(bytestream);
98  if (avctx->pix_fmt != AV_PIX_FMT_MONOWHITE &&
99  avctx->pix_fmt != AV_PIX_FMT_GBRPF32) {
100  int maxdepth = (1 << av_pix_fmt_desc_get(avctx->pix_fmt)->comp[0].depth) - 1;
101  snprintf(bytestream, bytestream_end - bytestream,
102  "%d\n", maxdepth);
103  bytestream += strlen(bytestream);
104  }
105 
106  if (avctx->pix_fmt == AV_PIX_FMT_GBRPF32) {
107  float *r = (float *)p->data[2];
108  float *g = (float *)p->data[0];
109  float *b = (float *)p->data[1];
110 
111  for (int i = 0; i < avctx->height; i++) {
112  for (int j = 0; j < avctx->width; j++) {
113  AV_WN32(bytestream + 0, av_float2int(r[j]));
114  AV_WN32(bytestream + 4, av_float2int(g[j]));
115  AV_WN32(bytestream + 8, av_float2int(b[j]));
116  bytestream += 12;
117  }
118 
119  r += p->linesize[2] / 4;
120  g += p->linesize[0] / 4;
121  b += p->linesize[1] / 4;
122  }
123  } else {
124  ptr = p->data[0];
125  linesize = p->linesize[0];
126  for (i = 0; i < h; i++) {
127  memcpy(bytestream, ptr, n);
128  bytestream += n;
129  ptr += linesize;
130  }
131  }
132 
133  if (avctx->pix_fmt == AV_PIX_FMT_YUV420P || avctx->pix_fmt == AV_PIX_FMT_YUV420P16BE) {
134  h >>= 1;
135  n >>= 1;
136  ptr1 = p->data[1];
137  ptr2 = p->data[2];
138  for (i = 0; i < h; i++) {
139  memcpy(bytestream, ptr1, n);
140  bytestream += n;
141  memcpy(bytestream, ptr2, n);
142  bytestream += n;
143  ptr1 += p->linesize[1];
144  ptr2 += p->linesize[2];
145  }
146  }
147  av_shrink_packet(pkt, bytestream - bytestream_start);
148  *got_packet = 1;
149 
150  return 0;
151 }
152 
153 #if CONFIG_PGM_ENCODER
154 const FFCodec ff_pgm_encoder = {
155  .p.name = "pgm",
156  .p.long_name = NULL_IF_CONFIG_SMALL("PGM (Portable GrayMap) image"),
157  .p.type = AVMEDIA_TYPE_VIDEO,
158  .p.id = AV_CODEC_ID_PGM,
159  .p.capabilities = AV_CODEC_CAP_DR1,
161  .p.pix_fmts = (const enum AVPixelFormat[]){
163  },
164  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
165 };
166 #endif
167 
168 #if CONFIG_PGMYUV_ENCODER
169 const FFCodec ff_pgmyuv_encoder = {
170  .p.name = "pgmyuv",
171  .p.long_name = NULL_IF_CONFIG_SMALL("PGMYUV (Portable GrayMap YUV) image"),
172  .p.type = AVMEDIA_TYPE_VIDEO,
173  .p.id = AV_CODEC_ID_PGMYUV,
174  .p.capabilities = AV_CODEC_CAP_DR1,
176  .p.pix_fmts = (const enum AVPixelFormat[]){
178  },
179  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
180 };
181 #endif
182 
183 #if CONFIG_PPM_ENCODER
184 const FFCodec ff_ppm_encoder = {
185  .p.name = "ppm",
186  .p.long_name = NULL_IF_CONFIG_SMALL("PPM (Portable PixelMap) image"),
187  .p.type = AVMEDIA_TYPE_VIDEO,
188  .p.id = AV_CODEC_ID_PPM,
189  .p.capabilities = AV_CODEC_CAP_DR1,
191  .p.pix_fmts = (const enum AVPixelFormat[]){
193  },
194  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
195 };
196 #endif
197 
198 #if CONFIG_PBM_ENCODER
199 const FFCodec ff_pbm_encoder = {
200  .p.name = "pbm",
201  .p.long_name = NULL_IF_CONFIG_SMALL("PBM (Portable BitMap) image"),
202  .p.type = AVMEDIA_TYPE_VIDEO,
203  .p.id = AV_CODEC_ID_PBM,
204  .p.capabilities = AV_CODEC_CAP_DR1,
206  .p.pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_MONOWHITE,
207  AV_PIX_FMT_NONE },
208  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
209 };
210 #endif
211 
212 #if CONFIG_PFM_ENCODER
213 const FFCodec ff_pfm_encoder = {
214  .p.name = "pfm",
215  .p.long_name = NULL_IF_CONFIG_SMALL("PFM (Portable FloatMap) image"),
216  .p.type = AVMEDIA_TYPE_VIDEO,
217  .p.id = AV_CODEC_ID_PFM,
218  .p.capabilities = AV_CODEC_CAP_DR1,
220  .p.pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_GBRPF32,
221  AV_PIX_FMT_NONE },
222  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
223 };
224 #endif
ff_pbm_encoder
const FFCodec ff_pbm_encoder
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:2662
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:325
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:310
AVPacket::data
uint8_t * data
Definition: packet.h:374
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:34
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
FFCodec
Definition: codec_internal.h:112
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:346
AV_PIX_FMT_GRAY16BE
@ AV_PIX_FMT_GRAY16BE
Y , 16bpp, big-endian.
Definition: pixfmt.h:97
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:116
av_shrink_packet
void av_shrink_packet(AVPacket *pkt, int size)
Reduce packet size, correctly zeroing padding.
Definition: avpacket.c:112
FF_CODEC_ENCODE_CB
#define FF_CODEC_ENCODE_CB(func)
Definition: codec_internal.h:263
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
ff_pfm_encoder
const FFCodec ff_pfm_encoder
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
ff_ppm_encoder
const FFCodec ff_ppm_encoder
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:375
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:116
codec_internal.h
AV_WN32
#define AV_WN32(p, v)
Definition: intreadwrite.h:376
for
for(k=2;k<=8;++k)
Definition: h264pred_template.c:425
AV_PIX_FMT_GBRPF32
#define AV_PIX_FMT_GBRPF32
Definition: pixfmt.h:435
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:102
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:271
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: codec_internal.h:31
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:203
AVCodecContext::height
int height
Definition: avcodec.h:562
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:599
avcodec.h
ff_pgm_encoder
const FFCodec ff_pgm_encoder
ret
ret
Definition: filter_design.txt:187
AVCodecContext
main external API structure.
Definition: avcodec.h:389
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:79
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:351
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:562
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:370
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:31
h
h
Definition: vp9dsp_template.c:2038
snprintf
#define snprintf
Definition: snprintf.h:34