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/pixdesc.h"
23 #include "avcodec.h"
24 #include "internal.h"
25 #include "pnm.h"
26 
27 
29  const AVFrame *pict, int *got_packet)
30 {
31  PNMContext *s = avctx->priv_data;
32  AVFrame * const p = &s->picture;
33  int i, h, h1, c, n, linesize, ret;
34  uint8_t *ptr, *ptr1, *ptr2;
35 
36  if ((ret = ff_alloc_packet2(avctx, pkt, avpicture_get_size(avctx->pix_fmt,
37  avctx->width,
38  avctx->height) + 200)) < 0)
39  return ret;
40 
41  *p = *pict;
43  p->key_frame = 1;
44 
45  s->bytestream_start =
46  s->bytestream = pkt->data;
47  s->bytestream_end = pkt->data + pkt->size;
48 
49  h = avctx->height;
50  h1 = h;
51  switch (avctx->pix_fmt) {
53  c = '4';
54  n = (avctx->width + 7) >> 3;
55  break;
56  case AV_PIX_FMT_GRAY8:
57  c = '5';
58  n = avctx->width;
59  break;
61  c = '5';
62  n = avctx->width * 2;
63  break;
64  case AV_PIX_FMT_RGB24:
65  c = '6';
66  n = avctx->width * 3;
67  break;
68  case AV_PIX_FMT_RGB48BE:
69  c = '6';
70  n = avctx->width * 6;
71  break;
72  case AV_PIX_FMT_YUV420P:
73  if (avctx->width & 1 || avctx->height & 1) {
74  av_log(avctx, AV_LOG_ERROR, "pgmyuv needs even width and height\n");
75  return AVERROR(EINVAL);
76  }
77  c = '5';
78  n = avctx->width;
79  h1 = (h * 3) / 2;
80  break;
82  c = '5';
83  n = avctx->width * 2;
84  h1 = (h * 3) / 2;
85  break;
86  default:
87  return -1;
88  }
90  "P%c\n%d %d\n", c, avctx->width, h1);
91  s->bytestream += strlen(s->bytestream);
92  if (avctx->pix_fmt != AV_PIX_FMT_MONOWHITE) {
93  int maxdepth = (1 << (av_pix_fmt_desc_get(avctx->pix_fmt)->comp[0].depth_minus1 + 1)) - 1;
95  "%d\n", maxdepth);
96  s->bytestream += strlen(s->bytestream);
97  }
98 
99  ptr = p->data[0];
100  linesize = p->linesize[0];
101  for (i = 0; i < h; i++) {
102  memcpy(s->bytestream, ptr, n);
103  s->bytestream += n;
104  ptr += linesize;
105  }
106 
107  if (avctx->pix_fmt == AV_PIX_FMT_YUV420P || avctx->pix_fmt == AV_PIX_FMT_YUV420P16BE) {
108  h >>= 1;
109  n >>= 1;
110  ptr1 = p->data[1];
111  ptr2 = p->data[2];
112  for (i = 0; i < h; i++) {
113  memcpy(s->bytestream, ptr1, n);
114  s->bytestream += n;
115  memcpy(s->bytestream, ptr2, n);
116  s->bytestream += n;
117  ptr1 += p->linesize[1];
118  ptr2 += p->linesize[2];
119  }
120  }
121  pkt->size = s->bytestream - s->bytestream_start;
122  pkt->flags |= AV_PKT_FLAG_KEY;
123  *got_packet = 1;
124 
125  return 0;
126 }
127 
128 
129 #if CONFIG_PGM_ENCODER
130 AVCodec ff_pgm_encoder = {
131  .name = "pgm",
132  .type = AVMEDIA_TYPE_VIDEO,
133  .id = AV_CODEC_ID_PGM,
134  .priv_data_size = sizeof(PNMContext),
135  .init = ff_pnm_init,
136  .encode2 = pnm_encode_frame,
137  .pix_fmts = (const enum AVPixelFormat[]){
139  },
140  .long_name = NULL_IF_CONFIG_SMALL("PGM (Portable GrayMap) image"),
141 };
142 #endif
143 
144 #if CONFIG_PGMYUV_ENCODER
145 AVCodec ff_pgmyuv_encoder = {
146  .name = "pgmyuv",
147  .type = AVMEDIA_TYPE_VIDEO,
148  .id = AV_CODEC_ID_PGMYUV,
149  .priv_data_size = sizeof(PNMContext),
150  .init = ff_pnm_init,
151  .encode2 = pnm_encode_frame,
152  .pix_fmts = (const enum AVPixelFormat[]){
154  },
155  .long_name = NULL_IF_CONFIG_SMALL("PGMYUV (Portable GrayMap YUV) image"),
156 };
157 #endif
158 
159 #if CONFIG_PPM_ENCODER
160 AVCodec ff_ppm_encoder = {
161  .name = "ppm",
162  .type = AVMEDIA_TYPE_VIDEO,
163  .id = AV_CODEC_ID_PPM,
164  .priv_data_size = sizeof(PNMContext),
165  .init = ff_pnm_init,
166  .encode2 = pnm_encode_frame,
167  .pix_fmts = (const enum AVPixelFormat[]){
169  },
170  .long_name = NULL_IF_CONFIG_SMALL("PPM (Portable PixelMap) image"),
171 };
172 #endif
173 
174 #if CONFIG_PBM_ENCODER
175 AVCodec ff_pbm_encoder = {
176  .name = "pbm",
177  .type = AVMEDIA_TYPE_VIDEO,
178  .id = AV_CODEC_ID_PBM,
179  .priv_data_size = sizeof(PNMContext),
180  .init = ff_pnm_init,
181  .encode2 = pnm_encode_frame,
182  .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_MONOWHITE,
183  AV_PIX_FMT_NONE },
184  .long_name = NULL_IF_CONFIG_SMALL("PBM (Portable BitMap) image"),
185 };
186 #endif
struct PNMContext PNMContext
const char * s
Definition: avisynth_c.h:668
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:1778
This structure describes decoded (raw) audio or video data.
Definition: frame.h:76
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:70
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
AVFrame picture
Definition: pnm.h:31
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
uint8_t * bytestream
Definition: pnm.h:28
AVComponentDescriptor comp[4]
Parameters that describe how pixels are packed.
Definition: pixdesc.h:86
uint8_t
static AVPacket pkt
Definition: demuxing.c:56
uint8_t * data
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
uint16_t depth_minus1
number of bits in the component minus 1
Definition: pixdesc.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:246
uint8_t * bytestream_end
Definition: pnm.h:30
const char * name
Name of the codec implementation.
external API header
int flags
A combination of AV_PKT_FLAG values.
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:144
ret
Definition: avfilter.c:821
int width
picture width / height.
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int size)
Check AVPacket size and/or allocate data.
Definition: pnm.h:27
planar YUV 4:2:0, 24bpp, (1 Cr & Cb sample per 2x2 Y samples), big-endian
Definition: pixfmt.h:129
static int pnm_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *pict, int *got_packet)
Definition: pnmenc.c:28
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:101
main external API structure.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:148
Y , 16bpp, big-endian.
Definition: pixfmt.h:101
synthesis window for stochastic i
#define snprintf
Definition: snprintf.h:34
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer 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.Buffer references ownership and permissions
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:87
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:68
Y , 8bpp.
Definition: pixfmt.h:76
common internal api header.
Y , 1bpp, 0 is white, 1 is black, in each byte pixels are ordered from the msb to the lsb...
Definition: pixfmt.h:77
static double c[64]
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:111
uint8_t * bytestream_start
Definition: pnm.h:29
av_cold int ff_pnm_init(AVCodecContext *avctx)
Definition: pnm.c:196
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:49
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:139
AVPixelFormat
Pixel format.
Definition: pixfmt.h:66
This structure stores compressed data.