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 "avcodec.h"
23 #include "internal.h"
24 
26  const AVFrame *p, int *got_packet)
27 {
28  uint8_t *bytestream_start, *bytestream, *bytestream_end;
29  int i, h, w, n, linesize, depth, maxval, ret;
30  const char *tuple_type;
31  uint8_t *ptr;
32 
33  h = avctx->height;
34  w = avctx->width;
35  switch (avctx->pix_fmt) {
37  n = w;
38  depth = 1;
39  maxval = 1;
40  tuple_type = "BLACKANDWHITE";
41  break;
42  case AV_PIX_FMT_GRAY8:
43  n = w;
44  depth = 1;
45  maxval = 255;
46  tuple_type = "GRAYSCALE";
47  break;
49  n = w * 2;
50  depth = 1;
51  maxval = 0xFFFF;
52  tuple_type = "GRAYSCALE";
53  break;
54  case AV_PIX_FMT_GRAY8A:
55  n = w * 2;
56  depth = 2;
57  maxval = 255;
58  tuple_type = "GRAYSCALE_ALPHA";
59  break;
60  case AV_PIX_FMT_YA16BE:
61  n = w * 4;
62  depth = 2;
63  maxval = 0xFFFF;
64  tuple_type = "GRAYSCALE_ALPHA";
65  break;
66  case AV_PIX_FMT_RGB24:
67  n = w * 3;
68  depth = 3;
69  maxval = 255;
70  tuple_type = "RGB";
71  break;
72  case AV_PIX_FMT_RGBA:
73  n = w * 4;
74  depth = 4;
75  maxval = 255;
76  tuple_type = "RGB_ALPHA";
77  break;
78  case AV_PIX_FMT_RGB48BE:
79  n = w * 6;
80  depth = 3;
81  maxval = 0xFFFF;
82  tuple_type = "RGB";
83  break;
85  n = w * 8;
86  depth = 4;
87  maxval = 0xFFFF;
88  tuple_type = "RGB_ALPHA";
89  break;
90  default:
91  return -1;
92  }
93 
94  if ((ret = ff_alloc_packet2(avctx, pkt, n*h + 200, 0)) < 0)
95  return ret;
96 
97  bytestream_start =
98  bytestream = pkt->data;
99  bytestream_end = pkt->data + pkt->size;
100 
101  snprintf(bytestream, bytestream_end - bytestream,
102  "P7\nWIDTH %d\nHEIGHT %d\nDEPTH %d\nMAXVAL %d\nTUPLTYPE %s\nENDHDR\n",
103  w, h, depth, maxval, tuple_type);
104  bytestream += strlen(bytestream);
105 
106  ptr = p->data[0];
107  linesize = p->linesize[0];
108 
109  if (avctx->pix_fmt == AV_PIX_FMT_MONOBLACK){
110  int j;
111  for (i = 0; i < h; i++) {
112  for (j = 0; j < w; j++)
113  *bytestream++ = ptr[j >> 3] >> (7 - j & 7) & 1;
114  ptr += linesize;
115  }
116  } else {
117  for (i = 0; i < h; i++) {
118  memcpy(bytestream, ptr, n);
119  bytestream += n;
120  ptr += linesize;
121  }
122  }
123 
124  pkt->size = bytestream - bytestream_start;
126  *got_packet = 1;
127  return 0;
128 }
129 
131 {
132 #if FF_API_CODED_FRAME
135  avctx->coded_frame->key_frame = 1;
137 #endif
138 
139  return 0;
140 }
141 
143  .name = "pam",
144  .long_name = NULL_IF_CONFIG_SMALL("PAM (Portable AnyMap) image"),
145  .type = AVMEDIA_TYPE_VIDEO,
146  .id = AV_CODEC_ID_PAM,
147  .init = pam_encode_init,
148  .encode2 = pam_encode_frame,
149  .pix_fmts = (const enum AVPixelFormat[]){
155  },
156 };
AVCodec
AVCodec.
Definition: codec.h:190
FF_ENABLE_DEPRECATION_WARNINGS
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:85
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:300
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:205
w
uint8_t w
Definition: llviddspenc.c:38
internal.h
AVPacket::data
uint8_t * data
Definition: packet.h:355
AV_PKT_FLAG_KEY
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: packet.h:388
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:314
AV_PIX_FMT_GRAY16BE
@ AV_PIX_FMT_GRAY16BE
Y , 16bpp, big-endian.
Definition: pixfmt.h:97
AVFrame::key_frame
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:378
av_cold
#define av_cold
Definition: attributes.h:90
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:146
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_PICTURE_TYPE_I
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:274
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:115
AVFrame::pict_type
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:383
AV_PIX_FMT_RGB24
@ AV_PIX_FMT_RGB24
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:68
AVPacket::size
int size
Definition: packet.h:356
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:188
AV_PIX_FMT_YA16BE
@ AV_PIX_FMT_YA16BE
16 bits gray, 16 bits alpha (big-endian)
Definition: pixfmt.h:212
AVPacket::flags
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:361
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
uint8_t
uint8_t
Definition: audio_convert.c:194
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:197
AVCodecContext::height
int height
Definition: avcodec.h:699
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:736
avcodec.h
ret
ret
Definition: filter_design.txt:187
ff_pam_encoder
AVCodec ff_pam_encoder
Definition: pamenc.c:142
AVCodecContext::coded_frame
attribute_deprecated AVFrame * coded_frame
the picture in the bitstream
Definition: avcodec.h:1776
AVCodecContext
main external API structure.
Definition: avcodec.h:526
pkt
static AVPacket pkt
Definition: demuxing_decoding.c:54
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
FF_DISABLE_DEPRECATION_WARNINGS
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:84
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
AVPacket
This structure stores compressed data.
Definition: packet.h:332
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:699
AVFrame::linesize
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:331
h
h
Definition: vp9dsp_template.c:2038
ff_alloc_packet2
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
Check AVPacket size and/or allocate data.
Definition: encode.c:32
snprintf
#define snprintf
Definition: snprintf.h:34
pam_encode_init
static av_cold int pam_encode_init(AVCodecContext *avctx)
Definition: pamenc.c:130
pam_encode_frame
static int pam_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *p, int *got_packet)
Definition: pamenc.c:25