FFmpeg
 All Data Structures 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 
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 *picture = data;
35  AVFrame * const p = &s->picture;
36  int i, j, n, linesize, h, upgrade = 0, is_mono = 0;
37  unsigned char *ptr;
38  int components, sample_len, ret;
39 
40  s->bytestream_start =
41  s->bytestream = (uint8_t *)buf;
42  s->bytestream_end = (uint8_t *)buf + buf_size;
43 
44  if ((ret = ff_pnm_decode_header(avctx, s)) < 0)
45  return ret;
46 
47  if (p->data[0])
48  avctx->release_buffer(avctx, p);
49 
50  p->reference = 0;
51  if ((ret = ff_get_buffer(avctx, p)) < 0) {
52  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
53  return ret;
54  }
56  p->key_frame = 1;
57 
58  switch (avctx->pix_fmt) {
59  default:
60  return AVERROR(EINVAL);
62  n = avctx->width * 8;
63  components=4;
64  sample_len=16;
65  goto do_read;
66  case AV_PIX_FMT_RGB48BE:
67  n = avctx->width * 6;
68  components=3;
69  sample_len=16;
70  goto do_read;
71  case AV_PIX_FMT_RGBA:
72  n = avctx->width * 4;
73  components=4;
74  sample_len=8;
75  goto do_read;
76  case AV_PIX_FMT_RGB24:
77  n = avctx->width * 3;
78  components=3;
79  sample_len=8;
80  goto do_read;
81  case AV_PIX_FMT_GRAY8:
82  n = avctx->width;
83  components=1;
84  sample_len=8;
85  if (s->maxval < 255)
86  upgrade = 1;
87  goto do_read;
88  case AV_PIX_FMT_GRAY8A:
89  n = avctx->width * 2;
90  components=2;
91  sample_len=8;
92  goto do_read;
95  n = avctx->width * 2;
96  components=1;
97  sample_len=16;
98  if (s->maxval < 65535)
99  upgrade = 2;
100  goto do_read;
103  n = (avctx->width + 7) >> 3;
104  components=1;
105  sample_len=1;
106  is_mono = 1;
107  do_read:
108  ptr = p->data[0];
109  linesize = p->linesize[0];
110  if (s->bytestream + n * avctx->height > s->bytestream_end)
111  return AVERROR_INVALIDDATA;
112  if(s->type < 4 || (is_mono && s->type==7)){
113  for (i=0; i<avctx->height; i++) {
114  PutBitContext pb;
115  init_put_bits(&pb, ptr, linesize);
116  for(j=0; j<avctx->width * components; j++){
117  unsigned int c=0;
118  int v=0;
119  if(s->type < 4)
120  while(s->bytestream < s->bytestream_end && (*s->bytestream < '0' || *s->bytestream > '9' ))
121  s->bytestream++;
122  if(s->bytestream >= s->bytestream_end)
123  return AVERROR_INVALIDDATA;
124  if (is_mono) {
125  /* read a single digit */
126  v = (*s->bytestream++)&1;
127  } else {
128  /* read a sequence of digits */
129  do {
130  v = 10*v + c;
131  c = (*s->bytestream++) - '0';
132  } while (c <= 9);
133  }
134  put_bits(&pb, sample_len, (((1<<sample_len)-1)*v + (s->maxval>>1))/s->maxval);
135  }
136  flush_put_bits(&pb);
137  ptr+= linesize;
138  }
139  }else{
140  for (i = 0; i < avctx->height; i++) {
141  if (!upgrade)
142  memcpy(ptr, s->bytestream, n);
143  else if (upgrade == 1) {
144  unsigned int j, f = (255 * 128 + s->maxval / 2) / s->maxval;
145  for (j = 0; j < n; j++)
146  ptr[j] = (s->bytestream[j] * f + 64) >> 7;
147  } else if (upgrade == 2) {
148  unsigned int j, v, f = (65535 * 32768 + s->maxval / 2) / s->maxval;
149  for (j = 0; j < n / 2; j++) {
150  v = av_be2ne16(((uint16_t *)s->bytestream)[j]);
151  ((uint16_t *)ptr)[j] = (v * f + 16384) >> 15;
152  }
153  }
154  s->bytestream += n;
155  ptr += linesize;
156  }
157  }
158  break;
159  case AV_PIX_FMT_YUV420P:
162  {
163  unsigned char *ptr1, *ptr2;
164 
165  n = avctx->width;
166  ptr = p->data[0];
167  linesize = p->linesize[0];
168  if (s->maxval >= 256)
169  n *= 2;
170  if (s->bytestream + n * avctx->height * 3 / 2 > s->bytestream_end)
171  return AVERROR_INVALIDDATA;
172  for (i = 0; i < avctx->height; i++) {
173  memcpy(ptr, s->bytestream, n);
174  s->bytestream += n;
175  ptr += linesize;
176  }
177  ptr1 = p->data[1];
178  ptr2 = p->data[2];
179  n >>= 1;
180  h = avctx->height >> 1;
181  for (i = 0; i < h; i++) {
182  memcpy(ptr1, s->bytestream, n);
183  s->bytestream += n;
184  memcpy(ptr2, s->bytestream, n);
185  s->bytestream += n;
186  ptr1 += p->linesize[1];
187  ptr2 += p->linesize[2];
188  }
189  }
190  break;
192  {
193  uint16_t *ptr1, *ptr2;
194  const int f = (65535 * 32768 + s->maxval / 2) / s->maxval;
195  unsigned int j, v;
196 
197  n = avctx->width * 2;
198  ptr = p->data[0];
199  linesize = p->linesize[0];
200  if (s->bytestream + n * avctx->height * 3 / 2 > s->bytestream_end)
201  return AVERROR_INVALIDDATA;
202  for (i = 0; i < avctx->height; i++) {
203  for (j = 0; j < n / 2; j++) {
204  v = av_be2ne16(((uint16_t *)s->bytestream)[j]);
205  ((uint16_t *)ptr)[j] = (v * f + 16384) >> 15;
206  }
207  s->bytestream += n;
208  ptr += linesize;
209  }
210  ptr1 = (uint16_t*)p->data[1];
211  ptr2 = (uint16_t*)p->data[2];
212  n >>= 1;
213  h = avctx->height >> 1;
214  for (i = 0; i < h; i++) {
215  for (j = 0; j < n / 2; j++) {
216  v = av_be2ne16(((uint16_t *)s->bytestream)[j]);
217  ptr1[j] = (v * f + 16384) >> 15;
218  }
219  s->bytestream += n;
220 
221  for (j = 0; j < n / 2; j++) {
222  v = av_be2ne16(((uint16_t *)s->bytestream)[j]);
223  ptr2[j] = (v * f + 16384) >> 15;
224  }
225  s->bytestream += n;
226 
227  ptr1 += p->linesize[1] / 2;
228  ptr2 += p->linesize[2] / 2;
229  }
230  }
231  break;
232  }
233  *picture = s->picture;
234  *got_frame = 1;
235 
236  return s->bytestream - s->bytestream_start;
237 }
238 
239 
240 #if CONFIG_PGM_DECODER
241 AVCodec ff_pgm_decoder = {
242  .name = "pgm",
243  .type = AVMEDIA_TYPE_VIDEO,
244  .id = AV_CODEC_ID_PGM,
245  .priv_data_size = sizeof(PNMContext),
246  .init = ff_pnm_init,
247  .close = ff_pnm_end,
249  .capabilities = CODEC_CAP_DR1,
250  .long_name = NULL_IF_CONFIG_SMALL("PGM (Portable GrayMap) image"),
251 };
252 #endif
253 
254 #if CONFIG_PGMYUV_DECODER
255 AVCodec ff_pgmyuv_decoder = {
256  .name = "pgmyuv",
257  .type = AVMEDIA_TYPE_VIDEO,
258  .id = AV_CODEC_ID_PGMYUV,
259  .priv_data_size = sizeof(PNMContext),
260  .init = ff_pnm_init,
261  .close = ff_pnm_end,
263  .capabilities = CODEC_CAP_DR1,
264  .long_name = NULL_IF_CONFIG_SMALL("PGMYUV (Portable GrayMap YUV) image"),
265 };
266 #endif
267 
268 #if CONFIG_PPM_DECODER
269 AVCodec ff_ppm_decoder = {
270  .name = "ppm",
271  .type = AVMEDIA_TYPE_VIDEO,
272  .id = AV_CODEC_ID_PPM,
273  .priv_data_size = sizeof(PNMContext),
274  .init = ff_pnm_init,
275  .close = ff_pnm_end,
277  .capabilities = CODEC_CAP_DR1,
278  .long_name = NULL_IF_CONFIG_SMALL("PPM (Portable PixelMap) image"),
279 };
280 #endif
281 
282 #if CONFIG_PBM_DECODER
283 AVCodec ff_pbm_decoder = {
284  .name = "pbm",
285  .type = AVMEDIA_TYPE_VIDEO,
286  .id = AV_CODEC_ID_PBM,
287  .priv_data_size = sizeof(PNMContext),
288  .init = ff_pnm_init,
289  .close = ff_pnm_end,
291  .capabilities = CODEC_CAP_DR1,
292  .long_name = NULL_IF_CONFIG_SMALL("PBM (Portable BitMap) image"),
293 };
294 #endif
295 
296 #if CONFIG_PAM_DECODER
297 AVCodec ff_pam_decoder = {
298  .name = "pam",
299  .type = AVMEDIA_TYPE_VIDEO,
300  .id = AV_CODEC_ID_PAM,
301  .priv_data_size = sizeof(PNMContext),
302  .init = ff_pnm_init,
303  .close = ff_pnm_end,
305  .capabilities = CODEC_CAP_DR1,
306  .long_name = NULL_IF_CONFIG_SMALL("PAM (Portable AnyMap) image"),
307 };
308 #endif