Mercurial > libavcodec.hg
comparison pnm.c @ 2349:f7f4f06a55c9 libavcodec
merge pam_decode_frame() into pnm_decode_frame()
author | michael |
---|---|
date | Fri, 12 Nov 2004 23:38:43 +0000 |
parents | d02fb928ca44 |
children | 18b8b2dcc037 |
comparison
equal
deleted
inserted
replaced
2348:d02fb928ca44 | 2349:f7f4f06a55c9 |
---|---|
65 | 65 |
66 return 0; | 66 return 0; |
67 } | 67 } |
68 | 68 |
69 static int pnm_decode_header(AVCodecContext *avctx, PNMContext * const s){ | 69 static int pnm_decode_header(AVCodecContext *avctx, PNMContext * const s){ |
70 char buf1[32]; | 70 char buf1[32], tuple_type[32]; |
71 int h; | 71 int h, w, depth, maxval;; |
72 | 72 |
73 pnm_get(s, buf1, sizeof(buf1)); | 73 pnm_get(s, buf1, sizeof(buf1)); |
74 if (!strcmp(buf1, "P4")) { | 74 if (!strcmp(buf1, "P4")) { |
75 avctx->pix_fmt = PIX_FMT_MONOWHITE; | 75 avctx->pix_fmt = PIX_FMT_MONOWHITE; |
76 } else if (!strcmp(buf1, "P5")) { | 76 } else if (!strcmp(buf1, "P5")) { |
78 avctx->pix_fmt = PIX_FMT_YUV420P; | 78 avctx->pix_fmt = PIX_FMT_YUV420P; |
79 else | 79 else |
80 avctx->pix_fmt = PIX_FMT_GRAY8; | 80 avctx->pix_fmt = PIX_FMT_GRAY8; |
81 } else if (!strcmp(buf1, "P6")) { | 81 } else if (!strcmp(buf1, "P6")) { |
82 avctx->pix_fmt = PIX_FMT_RGB24; | 82 avctx->pix_fmt = PIX_FMT_RGB24; |
83 } else if (!strcmp(buf1, "P7")) { | |
84 w = -1; | |
85 h = -1; | |
86 maxval = -1; | |
87 depth = -1; | |
88 tuple_type[0] = '\0'; | |
89 for(;;) { | |
90 pnm_get(s, buf1, sizeof(buf1)); | |
91 if (!strcmp(buf1, "WIDTH")) { | |
92 pnm_get(s, buf1, sizeof(buf1)); | |
93 w = strtol(buf1, NULL, 10); | |
94 } else if (!strcmp(buf1, "HEIGHT")) { | |
95 pnm_get(s, buf1, sizeof(buf1)); | |
96 h = strtol(buf1, NULL, 10); | |
97 } else if (!strcmp(buf1, "DEPTH")) { | |
98 pnm_get(s, buf1, sizeof(buf1)); | |
99 depth = strtol(buf1, NULL, 10); | |
100 } else if (!strcmp(buf1, "MAXVAL")) { | |
101 pnm_get(s, buf1, sizeof(buf1)); | |
102 maxval = strtol(buf1, NULL, 10); | |
103 } else if (!strcmp(buf1, "TUPLETYPE")) { | |
104 pnm_get(s, tuple_type, sizeof(tuple_type)); | |
105 } else if (!strcmp(buf1, "ENDHDR")) { | |
106 break; | |
107 } else { | |
108 return -1; | |
109 } | |
110 } | |
111 /* check that all tags are present */ | |
112 if (w <= 0 || h <= 0 || maxval <= 0 || depth <= 0 || tuple_type[0] == '\0') | |
113 return -1; | |
114 avctx->width = w; | |
115 avctx->height = h; | |
116 if (depth == 1) { | |
117 if (maxval == 1) | |
118 avctx->pix_fmt = PIX_FMT_MONOWHITE; | |
119 else | |
120 avctx->pix_fmt = PIX_FMT_GRAY8; | |
121 } else if (depth == 3) { | |
122 avctx->pix_fmt = PIX_FMT_RGB24; | |
123 } else if (depth == 4) { | |
124 avctx->pix_fmt = PIX_FMT_RGBA32; | |
125 } else { | |
126 return -1; | |
127 } | |
128 return 0; | |
83 } else { | 129 } else { |
84 return -1; | 130 return -1; |
85 } | 131 } |
86 pnm_get(s, buf1, sizeof(buf1)); | 132 pnm_get(s, buf1, sizeof(buf1)); |
87 avctx->width = atoi(buf1); | 133 avctx->width = atoi(buf1); |
126 s->bytestream_start= | 172 s->bytestream_start= |
127 s->bytestream= buf; | 173 s->bytestream= buf; |
128 s->bytestream_end= buf + buf_size; | 174 s->bytestream_end= buf + buf_size; |
129 | 175 |
130 if(pnm_decode_header(avctx, s) < 0) | 176 if(pnm_decode_header(avctx, s) < 0) |
131 return h; | 177 return -1; |
132 | 178 |
133 if(p->data[0]) | 179 if(p->data[0]) |
134 avctx->release_buffer(avctx, p); | 180 avctx->release_buffer(avctx, p); |
135 | 181 |
136 p->reference= 0; | 182 p->reference= 0; |
149 goto do_read; | 195 goto do_read; |
150 case PIX_FMT_GRAY8: | 196 case PIX_FMT_GRAY8: |
151 n = avctx->width; | 197 n = avctx->width; |
152 goto do_read; | 198 goto do_read; |
153 case PIX_FMT_MONOWHITE: | 199 case PIX_FMT_MONOWHITE: |
200 case PIX_FMT_MONOBLACK: | |
154 n = (avctx->width + 7) >> 3; | 201 n = (avctx->width + 7) >> 3; |
155 do_read: | 202 do_read: |
156 ptr = p->data[0]; | 203 ptr = p->data[0]; |
157 linesize = p->linesize[0]; | 204 linesize = p->linesize[0]; |
158 for(i = 0; i < avctx->height; i++) { | 205 for(i = 0; i < avctx->height; i++) { |
185 ptr1 += p->linesize[1]; | 232 ptr1 += p->linesize[1]; |
186 ptr2 += p->linesize[2]; | 233 ptr2 += p->linesize[2]; |
187 } | 234 } |
188 } | 235 } |
189 break; | 236 break; |
237 case PIX_FMT_RGBA32: | |
238 ptr = p->data[0]; | |
239 linesize = p->linesize[0]; | |
240 for(i = 0; i < avctx->height; i++) { | |
241 int j, r, g, b, a; | |
242 | |
243 for(j = 0;j < avctx->width; j++) { | |
244 r = *s->bytestream++; | |
245 g = *s->bytestream++; | |
246 b = *s->bytestream++; | |
247 a = *s->bytestream++; | |
248 ((uint32_t *)ptr)[j] = (a << 24) | (r << 16) | (g << 8) | b; | |
249 } | |
250 ptr += linesize; | |
251 } | |
252 break; | |
190 } | 253 } |
191 *picture= *(AVFrame*)&s->picture; | 254 *picture= *(AVFrame*)&s->picture; |
192 *data_size = sizeof(AVPicture); | 255 *data_size = sizeof(AVPicture); |
193 | 256 |
194 return s->bytestream - s->bytestream_start; | 257 return s->bytestream - s->bytestream_start; |
265 } | 328 } |
266 } | 329 } |
267 return s->bytestream - s->bytestream_start; | 330 return s->bytestream - s->bytestream_start; |
268 } | 331 } |
269 | 332 |
270 static int pam_decode_frame(AVCodecContext *avctx, | |
271 void *data, int *data_size, | |
272 uint8_t *buf, int buf_size) | |
273 { | |
274 PNMContext * const s = avctx->priv_data; | |
275 AVFrame *picture = data; | |
276 AVFrame * const p= (AVFrame*)&s->picture; | |
277 int i, n, linesize, h, w, depth, maxval; | |
278 char buf1[32], tuple_type[32]; | |
279 unsigned char *ptr; | |
280 | |
281 /* special case for last picture */ | |
282 if (buf_size == 0) { | |
283 return 0; | |
284 } | |
285 | |
286 s->bytestream_start= | |
287 s->bytestream= buf; | |
288 s->bytestream_end= buf + buf_size; | |
289 | |
290 pnm_get(s, buf1, sizeof(buf1)); | |
291 if (strcmp(buf1, "P7") != 0) | |
292 return -1; | |
293 w = -1; | |
294 h = -1; | |
295 maxval = -1; | |
296 depth = -1; | |
297 tuple_type[0] = '\0'; | |
298 for(;;) { | |
299 pnm_get(s, buf1, sizeof(buf1)); | |
300 if (!strcmp(buf1, "WIDTH")) { | |
301 pnm_get(s, buf1, sizeof(buf1)); | |
302 w = strtol(buf1, NULL, 10); | |
303 } else if (!strcmp(buf1, "HEIGHT")) { | |
304 pnm_get(s, buf1, sizeof(buf1)); | |
305 h = strtol(buf1, NULL, 10); | |
306 } else if (!strcmp(buf1, "DEPTH")) { | |
307 pnm_get(s, buf1, sizeof(buf1)); | |
308 depth = strtol(buf1, NULL, 10); | |
309 } else if (!strcmp(buf1, "MAXVAL")) { | |
310 pnm_get(s, buf1, sizeof(buf1)); | |
311 maxval = strtol(buf1, NULL, 10); | |
312 } else if (!strcmp(buf1, "TUPLETYPE")) { | |
313 pnm_get(s, tuple_type, sizeof(tuple_type)); | |
314 } else if (!strcmp(buf1, "ENDHDR")) { | |
315 break; | |
316 } else { | |
317 return -1; | |
318 } | |
319 } | |
320 /* check that all tags are present */ | |
321 if (w <= 0 || h <= 0 || maxval <= 0 || depth <= 0 || tuple_type[0] == '\0') | |
322 return -1; | |
323 avctx->width = w; | |
324 avctx->height = h; | |
325 if (depth == 1) { | |
326 if (maxval == 1) | |
327 avctx->pix_fmt = PIX_FMT_MONOWHITE; | |
328 else | |
329 avctx->pix_fmt = PIX_FMT_GRAY8; | |
330 } else if (depth == 3) { | |
331 avctx->pix_fmt = PIX_FMT_RGB24; | |
332 } else if (depth == 4) { | |
333 avctx->pix_fmt = PIX_FMT_RGBA32; | |
334 } else { | |
335 return -1; | |
336 } | |
337 | |
338 if(p->data[0]) | |
339 avctx->release_buffer(avctx, p); | |
340 | |
341 p->reference= 0; | |
342 if(avctx->get_buffer(avctx, p) < 0){ | |
343 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n"); | |
344 return -1; | |
345 } | |
346 p->pict_type= FF_I_TYPE; | |
347 p->key_frame= 1; | |
348 | |
349 switch(avctx->pix_fmt) { | |
350 default: | |
351 return -1; | |
352 case PIX_FMT_RGB24: | |
353 n = avctx->width * 3; | |
354 goto do_read; | |
355 case PIX_FMT_GRAY8: | |
356 n = avctx->width; | |
357 goto do_read; | |
358 case PIX_FMT_MONOWHITE: | |
359 n = (avctx->width + 7) >> 3; | |
360 do_read: | |
361 ptr = p->data[0]; | |
362 linesize = p->linesize[0]; | |
363 for(i = 0; i < avctx->height; i++) { | |
364 memcpy(ptr, s->bytestream, n); | |
365 s->bytestream += n; | |
366 ptr += linesize; | |
367 } | |
368 break; | |
369 case PIX_FMT_RGBA32: | |
370 ptr = p->data[0]; | |
371 linesize = p->linesize[0]; | |
372 for(i = 0; i < avctx->height; i++) { | |
373 int j, r, g, b, a; | |
374 | |
375 for(j = 0;j < w; j++) { | |
376 r = *s->bytestream++; | |
377 g = *s->bytestream++; | |
378 b = *s->bytestream++; | |
379 a = *s->bytestream++; | |
380 ((uint32_t *)ptr)[j] = (a << 24) | (r << 16) | (g << 8) | b; | |
381 } | |
382 ptr += linesize; | |
383 } | |
384 break; | |
385 } | |
386 *picture= *(AVFrame*)&s->picture; | |
387 *data_size = sizeof(AVPicture); | |
388 | |
389 return s->bytestream - s->bytestream_start; | |
390 } | |
391 | |
392 static int pam_encode_frame(AVCodecContext *avctx, unsigned char *outbuf, int buf_size, void *data){ | 333 static int pam_encode_frame(AVCodecContext *avctx, unsigned char *outbuf, int buf_size, void *data){ |
393 PNMContext *s = avctx->priv_data; | 334 PNMContext *s = avctx->priv_data; |
394 AVFrame *pict = data; | 335 AVFrame *pict = data; |
395 AVFrame * const p= (AVFrame*)&s->picture; | 336 AVFrame * const p= (AVFrame*)&s->picture; |
396 int i, h, w, n, linesize, depth, maxval; | 337 int i, h, w, n, linesize, depth, maxval; |
561 *poutbuf_size = buf_size; | 502 *poutbuf_size = buf_size; |
562 return next; | 503 return next; |
563 } | 504 } |
564 | 505 |
565 AVCodecParser pnm_parser = { | 506 AVCodecParser pnm_parser = { |
566 { CODEC_ID_PGM, CODEC_ID_PGMYUV, CODEC_ID_PPM, CODEC_ID_PBM}, | 507 { CODEC_ID_PGM, CODEC_ID_PGMYUV, CODEC_ID_PPM, CODEC_ID_PBM, CODEC_ID_PAM}, |
567 sizeof(ParseContext), | 508 sizeof(ParseContext), |
568 NULL, | 509 NULL, |
569 pnm_parse, | 510 pnm_parse, |
570 ff_parse_close, | 511 ff_parse_close, |
571 }; | 512 }; |
624 CODEC_ID_PAM, | 565 CODEC_ID_PAM, |
625 sizeof(PNMContext), | 566 sizeof(PNMContext), |
626 common_init, | 567 common_init, |
627 pam_encode_frame, | 568 pam_encode_frame, |
628 NULL, //encode_end, | 569 NULL, //encode_end, |
629 pam_decode_frame, | 570 pnm_decode_frame, |
630 .pix_fmts= (enum PixelFormat[]){PIX_FMT_RGB24, PIX_FMT_RGBA32, PIX_FMT_GRAY8, PIX_FMT_MONOWHITE, -1}, | 571 .pix_fmts= (enum PixelFormat[]){PIX_FMT_RGB24, PIX_FMT_RGBA32, PIX_FMT_GRAY8, PIX_FMT_MONOWHITE, -1}, |
631 }; | 572 }; |