FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 static void samplecpy(uint8_t *dst, const uint8_t *src, int n, int maxval)
28 {
29  if (maxval <= 255) {
30  memcpy(dst, src, n);
31  } else {
32  int i;
33  for (i=0; i<n/2; i++) {
34  ((uint16_t *)dst)[i] = AV_RB16(src+2*i);
35  }
36  }
37 }
38 
39 static int pnm_decode_frame(AVCodecContext *avctx, void *data,
40  int *got_frame, AVPacket *avpkt)
41 {
42  const uint8_t *buf = avpkt->data;
43  int buf_size = avpkt->size;
44  PNMContext * const s = avctx->priv_data;
45  AVFrame * const p = data;
46  int i, j, n, linesize, h, upgrade = 0, is_mono = 0;
47  unsigned char *ptr;
48  int components, sample_len, ret;
49 
50  s->bytestream_start =
51  s->bytestream = (uint8_t *)buf;
52  s->bytestream_end = (uint8_t *)buf + buf_size;
53 
54  if ((ret = ff_pnm_decode_header(avctx, s)) < 0)
55  return ret;
56 
57  if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
58  return ret;
60  p->key_frame = 1;
61  avctx->bits_per_raw_sample = av_log2(s->maxval) + 1;
62 
63  switch (avctx->pix_fmt) {
64  default:
65  return AVERROR(EINVAL);
66  case AV_PIX_FMT_RGBA64:
67  n = avctx->width * 8;
68  components=4;
69  sample_len=16;
70  if (s->maxval < 65535)
71  upgrade = 2;
72  goto do_read;
73  case AV_PIX_FMT_RGB48:
74  n = avctx->width * 6;
75  components=3;
76  sample_len=16;
77  if (s->maxval < 65535)
78  upgrade = 2;
79  goto do_read;
80  case AV_PIX_FMT_RGBA:
81  n = avctx->width * 4;
82  components=4;
83  sample_len=8;
84  goto do_read;
85  case AV_PIX_FMT_RGB24:
86  n = avctx->width * 3;
87  components=3;
88  sample_len=8;
89  if (s->maxval < 255)
90  upgrade = 1;
91  goto do_read;
92  case AV_PIX_FMT_GRAY8:
93  n = avctx->width;
94  components=1;
95  sample_len=8;
96  if (s->maxval < 255)
97  upgrade = 1;
98  goto do_read;
99  case AV_PIX_FMT_GRAY8A:
100  n = avctx->width * 2;
101  components=2;
102  sample_len=8;
103  goto do_read;
104  case AV_PIX_FMT_GRAY16:
105  n = avctx->width * 2;
106  components=1;
107  sample_len=16;
108  if (s->maxval < 65535)
109  upgrade = 2;
110  goto do_read;
111  case AV_PIX_FMT_YA16:
112  n = avctx->width * 4;
113  components=2;
114  sample_len=16;
115  if (s->maxval < 65535)
116  upgrade = 2;
117  goto do_read;
120  n = (avctx->width + 7) >> 3;
121  components=1;
122  sample_len=1;
123  is_mono = 1;
124  do_read:
125  ptr = p->data[0];
126  linesize = p->linesize[0];
127  if (s->bytestream + n * avctx->height > s->bytestream_end)
128  return AVERROR_INVALIDDATA;
129  if(s->type < 4 || (is_mono && s->type==7)){
130  for (i=0; i<avctx->height; i++) {
131  PutBitContext pb;
132  init_put_bits(&pb, ptr, linesize);
133  for(j=0; j<avctx->width * components; j++){
134  unsigned int c=0;
135  int v=0;
136  if(s->type < 4)
137  while(s->bytestream < s->bytestream_end && (*s->bytestream < '0' || *s->bytestream > '9' ))
138  s->bytestream++;
139  if(s->bytestream >= s->bytestream_end)
140  return AVERROR_INVALIDDATA;
141  if (is_mono) {
142  /* read a single digit */
143  v = (*s->bytestream++)&1;
144  } else {
145  /* read a sequence of digits */
146  do {
147  v = 10*v + c;
148  c = (*s->bytestream++) - '0';
149  } while (c <= 9);
150  }
151  if (sample_len == 16) {
152  ((uint16_t*)ptr)[j] = (((1<<sample_len)-1)*v + (s->maxval>>1))/s->maxval;
153  } else
154  put_bits(&pb, sample_len, (((1<<sample_len)-1)*v + (s->maxval>>1))/s->maxval);
155  }
156  if (sample_len != 16)
157  flush_put_bits(&pb);
158  ptr+= linesize;
159  }
160  }else{
161  for (i = 0; i < avctx->height; i++) {
162  if (!upgrade)
163  samplecpy(ptr, s->bytestream, n, s->maxval);
164  else if (upgrade == 1) {
165  unsigned int j, f = (255 * 128 + s->maxval / 2) / s->maxval;
166  for (j = 0; j < n; j++)
167  ptr[j] = (s->bytestream[j] * f + 64) >> 7;
168  } else if (upgrade == 2) {
169  unsigned int j, v, f = (65535 * 32768 + s->maxval / 2) / s->maxval;
170  for (j = 0; j < n / 2; j++) {
171  v = av_be2ne16(((uint16_t *)s->bytestream)[j]);
172  ((uint16_t *)ptr)[j] = (v * f + 16384) >> 15;
173  }
174  }
175  s->bytestream += n;
176  ptr += linesize;
177  }
178  }
179  break;
180  case AV_PIX_FMT_YUV420P:
181  case AV_PIX_FMT_YUV420P9:
183  {
184  unsigned char *ptr1, *ptr2;
185 
186  n = avctx->width;
187  ptr = p->data[0];
188  linesize = p->linesize[0];
189  if (s->maxval >= 256)
190  n *= 2;
191  if (s->bytestream + n * avctx->height * 3 / 2 > s->bytestream_end)
192  return AVERROR_INVALIDDATA;
193  for (i = 0; i < avctx->height; i++) {
194  samplecpy(ptr, s->bytestream, n, s->maxval);
195  s->bytestream += n;
196  ptr += linesize;
197  }
198  ptr1 = p->data[1];
199  ptr2 = p->data[2];
200  n >>= 1;
201  h = avctx->height >> 1;
202  for (i = 0; i < h; i++) {
203  samplecpy(ptr1, s->bytestream, n, s->maxval);
204  s->bytestream += n;
205  samplecpy(ptr2, s->bytestream, n, s->maxval);
206  s->bytestream += n;
207  ptr1 += p->linesize[1];
208  ptr2 += p->linesize[2];
209  }
210  }
211  break;
213  {
214  uint16_t *ptr1, *ptr2;
215  const int f = (65535 * 32768 + s->maxval / 2) / s->maxval;
216  unsigned int j, v;
217 
218  n = avctx->width * 2;
219  ptr = p->data[0];
220  linesize = p->linesize[0];
221  if (s->bytestream + n * avctx->height * 3 / 2 > s->bytestream_end)
222  return AVERROR_INVALIDDATA;
223  for (i = 0; i < avctx->height; i++) {
224  for (j = 0; j < n / 2; j++) {
225  v = av_be2ne16(((uint16_t *)s->bytestream)[j]);
226  ((uint16_t *)ptr)[j] = (v * f + 16384) >> 15;
227  }
228  s->bytestream += n;
229  ptr += linesize;
230  }
231  ptr1 = (uint16_t*)p->data[1];
232  ptr2 = (uint16_t*)p->data[2];
233  n >>= 1;
234  h = avctx->height >> 1;
235  for (i = 0; i < h; i++) {
236  for (j = 0; j < n / 2; j++) {
237  v = av_be2ne16(((uint16_t *)s->bytestream)[j]);
238  ptr1[j] = (v * f + 16384) >> 15;
239  }
240  s->bytestream += n;
241 
242  for (j = 0; j < n / 2; j++) {
243  v = av_be2ne16(((uint16_t *)s->bytestream)[j]);
244  ptr2[j] = (v * f + 16384) >> 15;
245  }
246  s->bytestream += n;
247 
248  ptr1 += p->linesize[1] / 2;
249  ptr2 += p->linesize[2] / 2;
250  }
251  }
252  break;
253  }
254  *got_frame = 1;
255 
256  return s->bytestream - s->bytestream_start;
257 }
258 
259 
260 #if CONFIG_PGM_DECODER
261 AVCodec ff_pgm_decoder = {
262  .name = "pgm",
263  .long_name = NULL_IF_CONFIG_SMALL("PGM (Portable GrayMap) image"),
264  .type = AVMEDIA_TYPE_VIDEO,
265  .id = AV_CODEC_ID_PGM,
266  .priv_data_size = sizeof(PNMContext),
268  .capabilities = AV_CODEC_CAP_DR1,
269 };
270 #endif
271 
272 #if CONFIG_PGMYUV_DECODER
273 AVCodec ff_pgmyuv_decoder = {
274  .name = "pgmyuv",
275  .long_name = NULL_IF_CONFIG_SMALL("PGMYUV (Portable GrayMap YUV) image"),
276  .type = AVMEDIA_TYPE_VIDEO,
277  .id = AV_CODEC_ID_PGMYUV,
278  .priv_data_size = sizeof(PNMContext),
280  .capabilities = AV_CODEC_CAP_DR1,
281 };
282 #endif
283 
284 #if CONFIG_PPM_DECODER
285 AVCodec ff_ppm_decoder = {
286  .name = "ppm",
287  .long_name = NULL_IF_CONFIG_SMALL("PPM (Portable PixelMap) image"),
288  .type = AVMEDIA_TYPE_VIDEO,
289  .id = AV_CODEC_ID_PPM,
290  .priv_data_size = sizeof(PNMContext),
292  .capabilities = AV_CODEC_CAP_DR1,
293 };
294 #endif
295 
296 #if CONFIG_PBM_DECODER
297 AVCodec ff_pbm_decoder = {
298  .name = "pbm",
299  .long_name = NULL_IF_CONFIG_SMALL("PBM (Portable BitMap) image"),
300  .type = AVMEDIA_TYPE_VIDEO,
301  .id = AV_CODEC_ID_PBM,
302  .priv_data_size = sizeof(PNMContext),
304  .capabilities = AV_CODEC_CAP_DR1,
305 };
306 #endif
307 
308 #if CONFIG_PAM_DECODER
309 AVCodec ff_pam_decoder = {
310  .name = "pam",
311  .long_name = NULL_IF_CONFIG_SMALL("PAM (Portable AnyMap) image"),
312  .type = AVMEDIA_TYPE_VIDEO,
313  .id = AV_CODEC_ID_PAM,
314  .priv_data_size = sizeof(PNMContext),
316  .capabilities = AV_CODEC_CAP_DR1,
317 };
318 #endif
const char * s
Definition: avisynth_c.h:631
#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:181
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:168
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:65
#define AV_PIX_FMT_RGBA64
Definition: pixfmt.h:320
int maxval
maximum value of a pixel
Definition: pnm.h:31
int size
Definition: avcodec.h:1468
int av_log2(unsigned v)
Definition: intmath.c:26
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1752
uint8_t * bytestream
Definition: pnm.h:28
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:2924
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_WB24 unsigned int_TMPL AV_RB16
Definition: bytestream.h:87
AVCodec.
Definition: avcodec.h:3392
uint8_t
uint8_t * data
Definition: avcodec.h:1467
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:176
#define AV_PIX_FMT_YA16
Definition: pixfmt.h:315
#define AV_PIX_FMT_RGB48
Definition: pixfmt.h:316
uint8_t * bytestream_end
Definition: pnm.h:30
const char * name
Name of the codec implementation.
Definition: avcodec.h:3399
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:95
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:252
alias for AV_PIX_FMT_YA8
Definition: pixfmt.h:158
#define AV_PIX_FMT_GRAY16
Definition: pixfmt.h:314
int width
picture width / height.
Definition: avcodec.h:1711
int n
Definition: avisynth_c.h:547
#define src
Definition: vp9dsp.c:530
#define AV_PIX_FMT_YUV420P16
Definition: pixfmt.h:341
Definition: pnm.h:27
Libavcodec external API header.
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:209
main external API structure.
Definition: avcodec.h:1532
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:894
void * buf
Definition: avisynth_c.h:553
#define AV_PIX_FMT_YUV420P10
Definition: pixfmt.h:330
#define AV_PIX_FMT_YUV420P9
Definition: pixfmt.h:327
static void samplecpy(uint8_t *dst, const uint8_t *src, int n, int maxval)
Definition: pnmdec.c:27
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:192
static int pnm_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: pnmdec.c:39
static int decode(AVCodecContext *avctx, void *data, int *got_sub, AVPacket *avpkt)
Definition: ccaption_dec.c:572
Y , 1bpp, 0 is black, 1 is white, in each byte pixels are ordered from the msb to the lsb...
Definition: pixfmt.h:73
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:63
Y , 8bpp.
Definition: pixfmt.h:71
common internal api header.
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:101
if(ret< 0)
Definition: vf_mcdeint.c:282
Y , 1bpp, 0 is white, 1 is black, in each byte pixels are ordered from the msb to the lsb...
Definition: pixfmt.h:72
static double c[64]
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:48
uint8_t * bytestream_start
Definition: pnm.h:29
void * priv_data
Definition: avcodec.h:1574
#define av_be2ne16(x)
Definition: bswap.h:92
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:247
This structure stores compressed data.
Definition: avcodec.h:1444
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:856
for(j=16;j >0;--j)
int type
Definition: pnm.h:32
bitstream writer API