Libav
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 Libav.
6  *
7  * Libav 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  * Libav 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 Libav; 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/imgutils.h"
23 
24 #include "avcodec.h"
25 #include "bytestream.h"
26 #include "internal.h"
27 
28 static int pam_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
29  const AVFrame *pict, int *got_packet)
30 {
31  uint8_t *bytestream_start, *bytestream, *bytestream_end;
32  const AVFrame * const p = pict;
33  int i, h, w, n, linesize, depth, maxval, ret;
34  const char *tuple_type;
35  uint8_t *ptr;
37  avctx->width, avctx->height, 1);
38 
39  if ((ret = ff_alloc_packet(pkt, size + 200)) < 0) {
40  av_log(avctx, AV_LOG_ERROR, "encoded frame too large\n");
41  return ret;
42  }
43 
44  bytestream_start =
45  bytestream = pkt->data;
46  bytestream_end = pkt->data + pkt->size;
47 
48  h = avctx->height;
49  w = avctx->width;
50  switch (avctx->pix_fmt) {
52  n = (w + 7) >> 3;
53  depth = 1;
54  maxval = 1;
55  tuple_type = "BLACKANDWHITE";
56  break;
57  case AV_PIX_FMT_GRAY8:
58  n = w;
59  depth = 1;
60  maxval = 255;
61  tuple_type = "GRAYSCALE";
62  break;
63  case AV_PIX_FMT_RGB24:
64  n = w * 3;
65  depth = 3;
66  maxval = 255;
67  tuple_type = "RGB";
68  break;
69  case AV_PIX_FMT_RGB32:
70  n = w * 4;
71  depth = 4;
72  maxval = 255;
73  tuple_type = "RGB_ALPHA";
74  break;
75  default:
76  return -1;
77  }
78  snprintf(bytestream, bytestream_end - bytestream,
79  "P7\nWIDTH %d\nHEIGHT %d\nDEPTH %d\nMAXVAL %d\nTUPLTYPE %s\nENDHDR\n",
80  w, h, depth, maxval, tuple_type);
81  bytestream += strlen(bytestream);
82 
83  ptr = p->data[0];
84  linesize = p->linesize[0];
85 
86  if (avctx->pix_fmt == AV_PIX_FMT_RGB32) {
87  int j;
88  unsigned int v;
89 
90  for (i = 0; i < h; i++) {
91  for (j = 0; j < w; j++) {
92  v = ((uint32_t *)ptr)[j];
93  bytestream_put_be24(&bytestream, v);
94  *bytestream++ = v >> 24;
95  }
96  ptr += linesize;
97  }
98  } else {
99  for (i = 0; i < h; i++) {
100  memcpy(bytestream, ptr, n);
101  bytestream += n;
102  ptr += linesize;
103  }
104  }
105 
106  pkt->size = bytestream - bytestream_start;
107  pkt->flags |= AV_PKT_FLAG_KEY;
108  *got_packet = 1;
109  return 0;
110 }
111 
113 {
114 #if FF_API_CODED_FRAME
117  avctx->coded_frame->key_frame = 1;
119 #endif
120 
121  return 0;
122 }
123 
125  .name = "pam",
126  .long_name = NULL_IF_CONFIG_SMALL("PAM (Portable AnyMap) image"),
127  .type = AVMEDIA_TYPE_VIDEO,
128  .id = AV_CODEC_ID_PAM,
129  .init = pam_encode_init,
130  .encode2 = pam_encode_frame,
131  .pix_fmts = (const enum AVPixelFormat[]){
134  },
135 };
int size
This structure describes decoded (raw) audio or video data.
Definition: frame.h:140
misc image utilities
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:61
int size
Definition: avcodec.h:1347
av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (%s)\, len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic ? ac->func_descr_generic :ac->func_descr)
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1621
AVCodec.
Definition: avcodec.h:3120
uint8_t
#define av_cold
Definition: attributes.h:66
uint8_t * data
Definition: avcodec.h:1346
static av_cold int pam_encode_init(AVCodecContext *avctx)
Definition: pamenc.c:112
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1378
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:323
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:124
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:148
const char * name
Name of the codec implementation.
Definition: avcodec.h:3127
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1352
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:201
int width
picture width / height.
Definition: avcodec.h:1580
int ff_alloc_packet(AVPacket *avpkt, int size)
Check AVPacket size and/or allocate data.
Definition: utils.c:1211
static int pam_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *pict, int *got_packet)
Definition: pamenc.c:28
Libavcodec external API header.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:158
main external API structure.
Definition: avcodec.h:1409
#define AV_PIX_FMT_RGB32
Definition: pixfmt.h:242
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:146
Y , 8bpp.
Definition: pixfmt.h:67
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:77
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:68
attribute_deprecated AVFrame * coded_frame
the picture in the bitstream
Definition: avcodec.h:2797
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:78
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:196
AVPixelFormat
Pixel format.
Definition: pixfmt.h:57
This structure stores compressed data.
Definition: avcodec.h:1323
AVCodec ff_pam_encoder
Definition: pamenc.c:124