pnmdec.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 "avcodec.h"
23 #include "internal.h"
24 #include "put_bits.h"
25 #include "pnm.h"
26 
27 
28 static int pnm_decode_frame(AVCodecContext *avctx, void *data,
29  int *got_frame, AVPacket *avpkt)
30 {
31  const uint8_t *buf = avpkt->data;
32  int buf_size = avpkt->size;
33  PNMContext * const s = avctx->priv_data;
34  AVFrame * const p = data;
35  int i, j, n, linesize, h, upgrade = 0, is_mono = 0;
36  unsigned char *ptr;
37  int components, sample_len, ret;
38 
39  s->bytestream_start =
40  s->bytestream = (uint8_t *)buf;
41  s->bytestream_end = (uint8_t *)buf + buf_size;
42 
43  if ((ret = ff_pnm_decode_header(avctx, s)) < 0)
44  return ret;
45 
46  if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
47  return ret;
49  p->key_frame = 1;
50 
51  switch (avctx->pix_fmt) {
52  default:
53  return AVERROR(EINVAL);
55  n = avctx->width * 8;
56  components=4;
57  sample_len=16;
58  goto do_read;
59  case AV_PIX_FMT_RGB48BE:
60  n = avctx->width * 6;
61  components=3;
62  sample_len=16;
63  goto do_read;
64  case AV_PIX_FMT_RGBA:
65  n = avctx->width * 4;
66  components=4;
67  sample_len=8;
68  goto do_read;
69  case AV_PIX_FMT_RGB24:
70  n = avctx->width * 3;
71  components=3;
72  sample_len=8;
73  goto do_read;
74  case AV_PIX_FMT_GRAY8:
75  n = avctx->width;
76  components=1;
77  sample_len=8;
78  if (s->maxval < 255)
79  upgrade = 1;
80  goto do_read;
81  case AV_PIX_FMT_GRAY8A:
82  n = avctx->width * 2;
83  components=2;
84  sample_len=8;
85  goto do_read;
88  n = avctx->width * 2;
89  components=1;
90  sample_len=16;
91  if (s->maxval < 65535)
92  upgrade = 2;
93  goto do_read;
96  n = (avctx->width + 7) >> 3;
97  components=1;
98  sample_len=1;
99  is_mono = 1;
100  do_read:
101  ptr = p->data[0];
102  linesize = p->linesize[0];
103  if (s->bytestream + n * avctx->height > s->bytestream_end)
104  return AVERROR_INVALIDDATA;
105  if(s->type < 4 || (is_mono && s->type==7)){
106  for (i=0; i<avctx->height; i++) {
107  PutBitContext pb;
108  init_put_bits(&pb, ptr, linesize);
109  for(j=0; j<avctx->width * components; j++){
110  unsigned int c=0;
111  int v=0;
112  if(s->type < 4)
113  while(s->bytestream < s->bytestream_end && (*s->bytestream < '0' || *s->bytestream > '9' ))
114  s->bytestream++;
115  if(s->bytestream >= s->bytestream_end)
116  return AVERROR_INVALIDDATA;
117  if (is_mono) {
118  /* read a single digit */
119  v = (*s->bytestream++)&1;
120  } else {
121  /* read a sequence of digits */
122  do {
123  v = 10*v + c;
124  c = (*s->bytestream++) - '0';
125  } while (c <= 9);
126  }
127  put_bits(&pb, sample_len, (((1<<sample_len)-1)*v + (s->maxval>>1))/s->maxval);
128  }
129  flush_put_bits(&pb);
130  ptr+= linesize;
131  }
132  }else{
133  for (i = 0; i < avctx->height; i++) {
134  if (!upgrade)
135  memcpy(ptr, s->bytestream, n);
136  else if (upgrade == 1) {
137  unsigned int j, f = (255 * 128 + s->maxval / 2) / s->maxval;
138  for (j = 0; j < n; j++)
139  ptr[j] = (s->bytestream[j] * f + 64) >> 7;
140  } else if (upgrade == 2) {
141  unsigned int j, v, f = (65535 * 32768 + s->maxval / 2) / s->maxval;
142  for (j = 0; j < n / 2; j++) {
143  v = av_be2ne16(((uint16_t *)s->bytestream)[j]);
144  ((uint16_t *)ptr)[j] = (v * f + 16384) >> 15;
145  }
146  }
147  s->bytestream += n;
148  ptr += linesize;
149  }
150  }
151  break;
152  case AV_PIX_FMT_YUV420P:
155  {
156  unsigned char *ptr1, *ptr2;
157 
158  n = avctx->width;
159  ptr = p->data[0];
160  linesize = p->linesize[0];
161  if (s->maxval >= 256)
162  n *= 2;
163  if (s->bytestream + n * avctx->height * 3 / 2 > s->bytestream_end)
164  return AVERROR_INVALIDDATA;
165  for (i = 0; i < avctx->height; i++) {
166  memcpy(ptr, s->bytestream, n);
167  s->bytestream += n;
168  ptr += linesize;
169  }
170  ptr1 = p->data[1];
171  ptr2 = p->data[2];
172  n >>= 1;
173  h = avctx->height >> 1;
174  for (i = 0; i < h; i++) {
175  memcpy(ptr1, s->bytestream, n);
176  s->bytestream += n;
177  memcpy(ptr2, s->bytestream, n);
178  s->bytestream += n;
179  ptr1 += p->linesize[1];
180  ptr2 += p->linesize[2];
181  }
182  }
183  break;
185  {
186  uint16_t *ptr1, *ptr2;
187  const int f = (65535 * 32768 + s->maxval / 2) / s->maxval;
188  unsigned int j, v;
189 
190  n = avctx->width * 2;
191  ptr = p->data[0];
192  linesize = p->linesize[0];
193  if (s->bytestream + n * avctx->height * 3 / 2 > s->bytestream_end)
194  return AVERROR_INVALIDDATA;
195  for (i = 0; i < avctx->height; i++) {
196  for (j = 0; j < n / 2; j++) {
197  v = av_be2ne16(((uint16_t *)s->bytestream)[j]);
198  ((uint16_t *)ptr)[j] = (v * f + 16384) >> 15;
199  }
200  s->bytestream += n;
201  ptr += linesize;
202  }
203  ptr1 = (uint16_t*)p->data[1];
204  ptr2 = (uint16_t*)p->data[2];
205  n >>= 1;
206  h = avctx->height >> 1;
207  for (i = 0; i < h; i++) {
208  for (j = 0; j < n / 2; j++) {
209  v = av_be2ne16(((uint16_t *)s->bytestream)[j]);
210  ptr1[j] = (v * f + 16384) >> 15;
211  }
212  s->bytestream += n;
213 
214  for (j = 0; j < n / 2; j++) {
215  v = av_be2ne16(((uint16_t *)s->bytestream)[j]);
216  ptr2[j] = (v * f + 16384) >> 15;
217  }
218  s->bytestream += n;
219 
220  ptr1 += p->linesize[1] / 2;
221  ptr2 += p->linesize[2] / 2;
222  }
223  }
224  break;
225  }
226  *got_frame = 1;
227 
228  return s->bytestream - s->bytestream_start;
229 }
230 
231 
232 #if CONFIG_PGM_DECODER
233 AVCodec ff_pgm_decoder = {
234  .name = "pgm",
235  .type = AVMEDIA_TYPE_VIDEO,
236  .id = AV_CODEC_ID_PGM,
237  .priv_data_size = sizeof(PNMContext),
239  .capabilities = CODEC_CAP_DR1,
240  .long_name = NULL_IF_CONFIG_SMALL("PGM (Portable GrayMap) image"),
241 };
242 #endif
243 
244 #if CONFIG_PGMYUV_DECODER
245 AVCodec ff_pgmyuv_decoder = {
246  .name = "pgmyuv",
247  .type = AVMEDIA_TYPE_VIDEO,
248  .id = AV_CODEC_ID_PGMYUV,
249  .priv_data_size = sizeof(PNMContext),
251  .capabilities = CODEC_CAP_DR1,
252  .long_name = NULL_IF_CONFIG_SMALL("PGMYUV (Portable GrayMap YUV) image"),
253 };
254 #endif
255 
256 #if CONFIG_PPM_DECODER
257 AVCodec ff_ppm_decoder = {
258  .name = "ppm",
259  .type = AVMEDIA_TYPE_VIDEO,
260  .id = AV_CODEC_ID_PPM,
261  .priv_data_size = sizeof(PNMContext),
263  .capabilities = CODEC_CAP_DR1,
264  .long_name = NULL_IF_CONFIG_SMALL("PPM (Portable PixelMap) image"),
265 };
266 #endif
267 
268 #if CONFIG_PBM_DECODER
269 AVCodec ff_pbm_decoder = {
270  .name = "pbm",
271  .type = AVMEDIA_TYPE_VIDEO,
272  .id = AV_CODEC_ID_PBM,
273  .priv_data_size = sizeof(PNMContext),
275  .capabilities = CODEC_CAP_DR1,
276  .long_name = NULL_IF_CONFIG_SMALL("PBM (Portable BitMap) image"),
277 };
278 #endif
279 
280 #if CONFIG_PAM_DECODER
281 AVCodec ff_pam_decoder = {
282  .name = "pam",
283  .type = AVMEDIA_TYPE_VIDEO,
284  .id = AV_CODEC_ID_PAM,
285  .priv_data_size = sizeof(PNMContext),
287  .capabilities = CODEC_CAP_DR1,
288  .long_name = NULL_IF_CONFIG_SMALL("PAM (Portable AnyMap) image"),
289 };
290 #endif
struct PNMContext PNMContext
float v
const char * s
Definition: avisynth_c.h:668
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
This structure describes decoded (raw) audio or video data.
Definition: frame.h:76
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:70
int maxval
maximum value of a pixel
Definition: pnm.h:32
Sinusoidal phase f
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
uint8_t * bytestream
Definition: pnm.h:28
planar YUV 4:2:0, 13.5bpp, (1 Cr & Cb sample per 2x2 Y samples), big-endian
Definition: pixfmt.h:148
initialize output if(nPeaks >3)%at least 3 peaks in spectrum for trying to find f0 nf0peaks
uint8_t
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:209
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
uint8_t * data
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Spectrum Plot time data
uint8_t * bytestream_end
Definition: pnm.h:30
const char * name
Name of the codec implementation.
static void put_bits(J2kEncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:160
external API header
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:97
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:144
8bit gray, 8bit alpha
Definition: pixfmt.h:141
ret
Definition: avfilter.c:821
int width
picture width / height.
planar YUV 4:2:0, 15bpp, (1 Cr & Cb sample per 2x2 Y samples), big-endian
Definition: pixfmt.h:150
#define AV_PIX_FMT_YUV420P16
Definition: pixfmt.h:288
Definition: pnm.h:27
int ff_pnm_decode_header(AVCodecContext *avctx, PNMContext *const s)
Definition: pnm.c:60
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:101
main external API structure.
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
void * buf
Definition: avisynth_c.h:594
Y , 16bpp, big-endian.
Definition: pixfmt.h:101
synthesis window for stochastic i
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
static int pnm_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: pnmdec.c:28
Y , 1bpp, 0 is black, 1 is white, in each byte pixels are ordered from the msb to the lsb...
Definition: pixfmt.h:78
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.
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:81
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
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:54
uint8_t * bytestream_start
Definition: pnm.h:29
#define av_be2ne16(x)
Definition: bswap.h:92
Y , 16bpp, little-endian.
Definition: pixfmt.h:102
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:139
static int decode(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: crystalhd.c:868
This structure stores compressed data.
for(j=16;j >0;--j)
int type
Definition: pnm.h:33
bitstream writer API