3 * Copyright (c) 2002, 2003 Fabrice Bellard.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include "mpegvideo.h" //only for ParseContext
22 typedef struct PNMContext {
24 uint8_t *bytestream_start;
25 uint8_t *bytestream_end;
29 static inline int pnm_space(int c)
31 return (c == ' ' || c == '\n' || c == '\r' || c == '\t');
34 static void pnm_get(PNMContext *sc, char *str, int buf_size)
39 /* skip spaces and comments */
41 c = *sc->bytestream++;
44 c = *sc->bytestream++;
45 } while (c != '\n' && sc->bytestream < sc->bytestream_end);
46 } else if (!pnm_space(c)) {
52 while (sc->bytestream < sc->bytestream_end && !pnm_space(c)) {
53 if ((s - str) < buf_size - 1)
55 c = *sc->bytestream++;
60 static int common_init(AVCodecContext *avctx){
61 PNMContext *s = avctx->priv_data;
63 avcodec_get_frame_defaults((AVFrame*)&s->picture);
64 avctx->coded_frame= (AVFrame*)&s->picture;
69 static int pnm_decode_header(AVCodecContext *avctx, PNMContext * const s){
70 char buf1[32], tuple_type[32];
71 int h, w, depth, maxval;;
73 pnm_get(s, buf1, sizeof(buf1));
74 if (!strcmp(buf1, "P4")) {
75 avctx->pix_fmt = PIX_FMT_MONOWHITE;
76 } else if (!strcmp(buf1, "P5")) {
77 if (avctx->codec_id == CODEC_ID_PGMYUV)
78 avctx->pix_fmt = PIX_FMT_YUV420P;
80 avctx->pix_fmt = PIX_FMT_GRAY8;
81 } else if (!strcmp(buf1, "P6")) {
82 avctx->pix_fmt = PIX_FMT_RGB24;
83 } else if (!strcmp(buf1, "P7")) {
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")) {
111 /* check that all tags are present */
112 if (w <= 0 || h <= 0 || maxval <= 0 || depth <= 0 || tuple_type[0] == '\0' || avcodec_check_dimensions(avctx, w, h))
119 avctx->pix_fmt = PIX_FMT_MONOWHITE;
121 avctx->pix_fmt = PIX_FMT_GRAY8;
122 } else if (depth == 3) {
123 avctx->pix_fmt = PIX_FMT_RGB24;
124 } else if (depth == 4) {
125 avctx->pix_fmt = PIX_FMT_RGBA32;
133 pnm_get(s, buf1, sizeof(buf1));
134 avctx->width = atoi(buf1);
135 if (avctx->width <= 0)
137 pnm_get(s, buf1, sizeof(buf1));
138 avctx->height = atoi(buf1);
139 if(avcodec_check_dimensions(avctx, avctx->width, avctx->height))
141 if (avctx->pix_fmt != PIX_FMT_MONOWHITE) {
142 pnm_get(s, buf1, sizeof(buf1));
145 /* more check if YUV420 */
146 if (avctx->pix_fmt == PIX_FMT_YUV420P) {
147 if ((avctx->width & 1) != 0)
149 h = (avctx->height * 2);
158 static int pnm_decode_frame(AVCodecContext *avctx,
159 void *data, int *data_size,
160 uint8_t *buf, int buf_size)
162 PNMContext * const s = avctx->priv_data;
163 AVFrame *picture = data;
164 AVFrame * const p= (AVFrame*)&s->picture;
165 int i, n, linesize, h;
168 /* special case for last picture */
175 s->bytestream_end= buf + buf_size;
177 if(pnm_decode_header(avctx, s) < 0)
181 avctx->release_buffer(avctx, p);
184 if(avctx->get_buffer(avctx, p) < 0){
185 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
188 p->pict_type= FF_I_TYPE;
191 switch(avctx->pix_fmt) {
195 n = avctx->width * 3;
200 case PIX_FMT_MONOWHITE:
201 case PIX_FMT_MONOBLACK:
202 n = (avctx->width + 7) >> 3;
205 linesize = p->linesize[0];
206 for(i = 0; i < avctx->height; i++) {
207 memcpy(ptr, s->bytestream, n);
212 case PIX_FMT_YUV420P:
214 unsigned char *ptr1, *ptr2;
218 linesize = p->linesize[0];
219 for(i = 0; i < avctx->height; i++) {
220 memcpy(ptr, s->bytestream, n);
227 h = avctx->height >> 1;
228 for(i = 0; i < h; i++) {
229 memcpy(ptr1, s->bytestream, n);
231 memcpy(ptr2, s->bytestream, n);
233 ptr1 += p->linesize[1];
234 ptr2 += p->linesize[2];
240 linesize = p->linesize[0];
241 for(i = 0; i < avctx->height; i++) {
244 for(j = 0;j < avctx->width; j++) {
245 r = *s->bytestream++;
246 g = *s->bytestream++;
247 b = *s->bytestream++;
248 a = *s->bytestream++;
249 ((uint32_t *)ptr)[j] = (a << 24) | (r << 16) | (g << 8) | b;
255 *picture= *(AVFrame*)&s->picture;
256 *data_size = sizeof(AVPicture);
258 return s->bytestream - s->bytestream_start;
261 static int pnm_encode_frame(AVCodecContext *avctx, unsigned char *outbuf, int buf_size, void *data){
262 PNMContext *s = avctx->priv_data;
263 AVFrame *pict = data;
264 AVFrame * const p= (AVFrame*)&s->picture;
265 int i, h, h1, c, n, linesize;
266 uint8_t *ptr, *ptr1, *ptr2;
268 if(buf_size < avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height) + 200){
269 av_log(avctx, AV_LOG_ERROR, "encoded frame too large\n");
274 p->pict_type= FF_I_TYPE;
278 s->bytestream= outbuf;
279 s->bytestream_end= outbuf+buf_size;
283 switch(avctx->pix_fmt) {
284 case PIX_FMT_MONOWHITE:
286 n = (avctx->width + 7) >> 3;
294 n = avctx->width * 3;
296 case PIX_FMT_YUV420P:
304 snprintf(s->bytestream, s->bytestream_end - s->bytestream,
306 c, avctx->width, h1);
307 s->bytestream += strlen(s->bytestream);
308 if (avctx->pix_fmt != PIX_FMT_MONOWHITE) {
309 snprintf(s->bytestream, s->bytestream_end - s->bytestream,
311 s->bytestream += strlen(s->bytestream);
315 linesize = p->linesize[0];
317 memcpy(s->bytestream, ptr, n);
322 if (avctx->pix_fmt == PIX_FMT_YUV420P) {
328 memcpy(s->bytestream, ptr1, n);
330 memcpy(s->bytestream, ptr2, n);
332 ptr1 += p->linesize[1];
333 ptr2 += p->linesize[2];
336 return s->bytestream - s->bytestream_start;
339 static int pam_encode_frame(AVCodecContext *avctx, unsigned char *outbuf, int buf_size, void *data){
340 PNMContext *s = avctx->priv_data;
341 AVFrame *pict = data;
342 AVFrame * const p= (AVFrame*)&s->picture;
343 int i, h, w, n, linesize, depth, maxval;
344 const char *tuple_type;
347 if(buf_size < avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height) + 200){
348 av_log(avctx, AV_LOG_ERROR, "encoded frame too large\n");
353 p->pict_type= FF_I_TYPE;
357 s->bytestream= outbuf;
358 s->bytestream_end= outbuf+buf_size;
362 switch(avctx->pix_fmt) {
363 case PIX_FMT_MONOWHITE:
367 tuple_type = "BLACKANDWHITE";
373 tuple_type = "GRAYSCALE";
385 tuple_type = "RGB_ALPHA";
390 snprintf(s->bytestream, s->bytestream_end - s->bytestream,
391 "P7\nWIDTH %d\nHEIGHT %d\nDEPTH %d\nMAXVAL %d\nTUPLETYPE %s\nENDHDR\n",
392 w, h, depth, maxval, tuple_type);
393 s->bytestream += strlen(s->bytestream);
396 linesize = p->linesize[0];
398 if (avctx->pix_fmt == PIX_FMT_RGBA32) {
404 v = ((uint32_t *)ptr)[j];
405 *s->bytestream++ = v >> 16;
406 *s->bytestream++ = v >> 8;
407 *s->bytestream++ = v;
408 *s->bytestream++ = v >> 24;
414 memcpy(s->bytestream, ptr, n);
419 return s->bytestream - s->bytestream_start;
423 static int pnm_probe(AVProbeData *pd)
425 const char *p = pd->buf;
426 if (pd->buf_size >= 8 &&
428 p[1] >= '4' && p[1] <= '6' &&
430 return AVPROBE_SCORE_MAX - 1; /* to permit pgmyuv probe */
435 static int pgmyuv_probe(AVProbeData *pd)
437 if (match_ext(pd->filename, "pgmyuv"))
438 return AVPROBE_SCORE_MAX;
443 static int pam_probe(AVProbeData *pd)
445 const char *p = pd->buf;
446 if (pd->buf_size >= 8 &&
450 return AVPROBE_SCORE_MAX;
456 static int pnm_parse(AVCodecParserContext *s,
457 AVCodecContext *avctx,
458 uint8_t **poutbuf, int *poutbuf_size,
459 const uint8_t *buf, int buf_size)
461 ParseContext *pc = s->priv_data;
465 for(; pc->overread>0; pc->overread--){
466 pc->buffer[pc->index++]= pc->buffer[pc->overread_index++];
470 pnmctx.bytestream_start=
471 pnmctx.bytestream= pc->buffer;
472 pnmctx.bytestream_end= pc->buffer + pc->index;
474 pnmctx.bytestream_start=
475 pnmctx.bytestream= buf;
476 pnmctx.bytestream_end= buf + buf_size;
478 if(pnm_decode_header(avctx, &pnmctx) < 0){
479 if(pnmctx.bytestream < pnmctx.bytestream_end){
489 if(pc->index && pc->index*2 + FF_INPUT_BUFFER_PADDING_SIZE < pc->buffer_size && buf_size > pc->index){
490 memcpy(pc->buffer + pc->index, buf, pc->index);
491 pc->index += pc->index;
493 buf_size -= pc->index;
499 next= pnmctx.bytestream - pnmctx.bytestream_start
500 + avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height);
501 if(pnmctx.bytestream_start!=buf)
507 if(ff_combine_frame(pc, next, (uint8_t **)&buf, &buf_size)<0){
512 *poutbuf = (uint8_t *)buf;
513 *poutbuf_size = buf_size;
517 AVCodecParser pnm_parser = {
518 { CODEC_ID_PGM, CODEC_ID_PGMYUV, CODEC_ID_PPM, CODEC_ID_PBM, CODEC_ID_PAM},
519 sizeof(ParseContext),
525 AVCodec pgm_encoder = {
534 .pix_fmts= (enum PixelFormat[]){PIX_FMT_GRAY8, -1},
537 AVCodec pgmyuv_encoder = {
546 .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUV420P, -1},
549 AVCodec ppm_encoder = {
558 .pix_fmts= (enum PixelFormat[]){PIX_FMT_RGB24, -1},
561 AVCodec pbm_encoder = {
570 .pix_fmts= (enum PixelFormat[]){PIX_FMT_MONOWHITE, -1},
573 AVCodec pam_encoder = {
582 .pix_fmts= (enum PixelFormat[]){PIX_FMT_RGB24, PIX_FMT_RGBA32, PIX_FMT_GRAY8, PIX_FMT_MONOWHITE, -1},