FFmpeg
pamenc.c
Go to the documentation of this file.
1 /*
2  * PAM 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/avassert.h"
23 #include "avcodec.h"
24 #include "codec_internal.h"
25 #include "encode.h"
26 
28  const AVFrame *p, int *got_packet)
29 {
30  int i, h, w, n, linesize, depth, maxval, ret, header_size;
31  uint8_t *bytestream, *ptr;
32  const char *tuple_type;
33  char header[100];
34 
35  h = avctx->height;
36  w = avctx->width;
37  switch (avctx->pix_fmt) {
39  n = w;
40  depth = 1;
41  maxval = 1;
42  tuple_type = "BLACKANDWHITE";
43  break;
44  case AV_PIX_FMT_GRAY8:
45  n = w;
46  depth = 1;
47  maxval = 255;
48  tuple_type = "GRAYSCALE";
49  break;
51  n = w * 2;
52  depth = 1;
53  maxval = 0xFFFF;
54  tuple_type = "GRAYSCALE";
55  break;
56  case AV_PIX_FMT_GRAY8A:
57  n = w * 2;
58  depth = 2;
59  maxval = 255;
60  tuple_type = "GRAYSCALE_ALPHA";
61  break;
62  case AV_PIX_FMT_YA16BE:
63  n = w * 4;
64  depth = 2;
65  maxval = 0xFFFF;
66  tuple_type = "GRAYSCALE_ALPHA";
67  break;
68  case AV_PIX_FMT_RGB24:
69  n = w * 3;
70  depth = 3;
71  maxval = 255;
72  tuple_type = "RGB";
73  break;
74  case AV_PIX_FMT_RGBA:
75  n = w * 4;
76  depth = 4;
77  maxval = 255;
78  tuple_type = "RGB_ALPHA";
79  break;
80  case AV_PIX_FMT_RGB48BE:
81  n = w * 6;
82  depth = 3;
83  maxval = 0xFFFF;
84  tuple_type = "RGB";
85  break;
87  n = w * 8;
88  depth = 4;
89  maxval = 0xFFFF;
90  tuple_type = "RGB_ALPHA";
91  break;
92  default:
93  return -1;
94  }
95 
96  header_size = snprintf(header, sizeof(header),
97  "P7\nWIDTH %d\nHEIGHT %d\nDEPTH %d\nMAXVAL %d\nTUPLTYPE %s\nENDHDR\n",
98  w, h, depth, maxval, tuple_type);
99  av_assert1(header_size < sizeof(header));
100 
101  if ((ret = ff_get_encode_buffer(avctx, pkt, n*h + header_size, 0)) < 0)
102  return ret;
103 
104  bytestream = pkt->data;
105  memcpy(bytestream, header, header_size);
106  bytestream += header_size;
107 
108  ptr = p->data[0];
109  linesize = p->linesize[0];
110 
111  if (avctx->pix_fmt == AV_PIX_FMT_MONOBLACK){
112  int j;
113  for (i = 0; i < h; i++) {
114  for (j = 0; j < w; j++)
115  *bytestream++ = ptr[j >> 3] >> (7 - j & 7) & 1;
116  ptr += linesize;
117  }
118  } else {
119  for (i = 0; i < h; i++) {
120  memcpy(bytestream, ptr, n);
121  bytestream += n;
122  ptr += linesize;
123  }
124  }
125 
126  *got_packet = 1;
127  return 0;
128 }
129 
131  .p.name = "pam",
132  .p.long_name = NULL_IF_CONFIG_SMALL("PAM (Portable AnyMap) image"),
133  .p.type = AVMEDIA_TYPE_VIDEO,
134  .p.id = AV_CODEC_ID_PAM,
135  .p.capabilities = AV_CODEC_CAP_DR1,
137  .p.pix_fmts = (const enum AVPixelFormat[]){
143  },
144  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
145 };
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:325
AV_PIX_FMT_RGBA64BE
@ AV_PIX_FMT_RGBA64BE
packed RGBA 16:16:16:16, 64bpp, 16R, 16G, 16B, 16A, the 2-byte value for each R/G/B/A component is st...
Definition: pixfmt.h:195
w
uint8_t w
Definition: llviddspenc.c:38
AVPacket::data
uint8_t * data
Definition: packet.h:374
encode.h
FFCodec
Definition: codec_internal.h:112
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
FF_CODEC_ENCODE_CB
#define FF_CODEC_ENCODE_CB(func)
Definition: codec_internal.h:263
avassert.h
pkt
AVPacket * pkt
Definition: movenc.c:59
ff_pam_encoder
const FFCodec ff_pam_encoder
Definition: pamenc.c:130
AV_PIX_FMT_RGBA
@ AV_PIX_FMT_RGBA
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:93
AV_PIX_FMT_GRAY8A
@ AV_PIX_FMT_GRAY8A
alias for AV_PIX_FMT_YA8
Definition: pixfmt.h:136
AV_PIX_FMT_MONOBLACK
@ AV_PIX_FMT_MONOBLACK
Y , 1bpp, 0 is black, 1 is white, in each byte pixels are ordered from the msb to the lsb.
Definition: pixfmt.h:76
AV_PIX_FMT_GRAY8
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition: pixfmt.h:74
AV_CODEC_ID_PAM
@ AV_CODEC_ID_PAM
Definition: codec_id.h:116
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
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
codec_internal.h
AV_PIX_FMT_YA16BE
@ AV_PIX_FMT_YA16BE
16 bits gray, 16 bits alpha (big-endian)
Definition: pixfmt.h:202
header
static const uint8_t header[24]
Definition: sdr2.c:67
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:269
av_assert1
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:53
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
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
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
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
h
h
Definition: vp9dsp_template.c:2038
snprintf
#define snprintf
Definition: snprintf.h:34
pam_encode_frame
static int pam_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *p, int *got_packet)
Definition: pamenc.c:27