3 * Copyright (c) 2002, 2003 Fabrice Bellard
5 * This file is part of FFmpeg.
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.
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.
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
23 #include "bytestream.h"
27 static av_cold int common_init(AVCodecContext *avctx)
29 PNMContext *s = avctx->priv_data;
31 avcodec_get_frame_defaults((AVFrame*)&s->picture);
32 avctx->coded_frame = (AVFrame*)&s->picture;
37 static int pnm_decode_frame(AVCodecContext *avctx, void *data,
38 int *data_size, AVPacket *avpkt)
40 const uint8_t *buf = avpkt->data;
41 int buf_size = avpkt->size;
42 PNMContext * const s = avctx->priv_data;
43 AVFrame *picture = data;
44 AVFrame * const p = (AVFrame*)&s->picture;
45 int i, n, linesize, h, upgrade = 0;
50 s->bytestream_end = buf + buf_size;
52 if (ff_pnm_decode_header(avctx, s) < 0)
56 avctx->release_buffer(avctx, p);
59 if (avctx->get_buffer(avctx, p) < 0) {
60 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
63 p->pict_type = FF_I_TYPE;
66 switch (avctx->pix_fmt) {
80 case PIX_FMT_GRAY16BE:
81 case PIX_FMT_GRAY16LE:
83 if (s->maxval < 65535)
86 case PIX_FMT_MONOWHITE:
87 case PIX_FMT_MONOBLACK:
88 n = (avctx->width + 7) >> 3;
91 linesize = p->linesize[0];
92 if (s->bytestream + n * avctx->height > s->bytestream_end)
94 for (i = 0; i < avctx->height; i++) {
96 memcpy(ptr, s->bytestream, n);
97 else if (upgrade == 1) {
98 unsigned int j, f = (255 * 128 + s->maxval / 2) / s->maxval;
99 for (j = 0; j < n; j++)
100 ptr[j] = (s->bytestream[j] * f + 64) >> 7;
101 } else if (upgrade == 2) {
102 unsigned int j, v, f = (65535 * 32768 + s->maxval / 2) / s->maxval;
103 for (j = 0; j < n / 2; j++) {
104 v = be2me_16(((uint16_t *)s->bytestream)[j]);
105 ((uint16_t *)ptr)[j] = (v * f + 16384) >> 15;
112 case PIX_FMT_YUV420P:
114 unsigned char *ptr1, *ptr2;
118 linesize = p->linesize[0];
119 if (s->bytestream + n * avctx->height * 3 / 2 > s->bytestream_end)
121 for (i = 0; i < avctx->height; i++) {
122 memcpy(ptr, s->bytestream, n);
129 h = avctx->height >> 1;
130 for (i = 0; i < h; i++) {
131 memcpy(ptr1, s->bytestream, n);
133 memcpy(ptr2, s->bytestream, n);
135 ptr1 += p->linesize[1];
136 ptr2 += p->linesize[2];
142 linesize = p->linesize[0];
143 if (s->bytestream + avctx->width * avctx->height * 4 > s->bytestream_end)
145 for (i = 0; i < avctx->height; i++) {
148 for (j = 0; j < avctx->width; j++) {
149 r = *s->bytestream++;
150 g = *s->bytestream++;
151 b = *s->bytestream++;
152 a = *s->bytestream++;
153 ((uint32_t *)ptr)[j] = (a << 24) | (r << 16) | (g << 8) | b;
159 *picture = *(AVFrame*)&s->picture;
160 *data_size = sizeof(AVPicture);
162 return s->bytestream - s->bytestream_start;
165 static int pnm_encode_frame(AVCodecContext *avctx, unsigned char *outbuf,
166 int buf_size, void *data)
168 PNMContext *s = avctx->priv_data;
169 AVFrame *pict = data;
170 AVFrame * const p = (AVFrame*)&s->picture;
171 int i, h, h1, c, n, linesize;
172 uint8_t *ptr, *ptr1, *ptr2;
174 if (buf_size < avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height) + 200) {
175 av_log(avctx, AV_LOG_ERROR, "encoded frame too large\n");
180 p->pict_type = FF_I_TYPE;
183 s->bytestream_start =
184 s->bytestream = outbuf;
185 s->bytestream_end = outbuf + buf_size;
189 switch (avctx->pix_fmt) {
190 case PIX_FMT_MONOWHITE:
192 n = (avctx->width + 7) >> 3;
198 case PIX_FMT_GRAY16BE:
200 n = avctx->width * 2;
204 n = avctx->width * 3;
206 case PIX_FMT_RGB48BE:
208 n = avctx->width * 6;
210 case PIX_FMT_YUV420P:
218 snprintf(s->bytestream, s->bytestream_end - s->bytestream,
219 "P%c\n%d %d\n", c, avctx->width, h1);
220 s->bytestream += strlen(s->bytestream);
221 if (avctx->pix_fmt != PIX_FMT_MONOWHITE) {
222 snprintf(s->bytestream, s->bytestream_end - s->bytestream,
223 "%d\n", (avctx->pix_fmt != PIX_FMT_GRAY16BE && avctx->pix_fmt != PIX_FMT_RGB48BE) ? 255 : 65535);
224 s->bytestream += strlen(s->bytestream);
228 linesize = p->linesize[0];
229 for (i = 0; i < h; i++) {
230 memcpy(s->bytestream, ptr, n);
235 if (avctx->pix_fmt == PIX_FMT_YUV420P) {
240 for (i = 0; i < h; i++) {
241 memcpy(s->bytestream, ptr1, n);
243 memcpy(s->bytestream, ptr2, n);
245 ptr1 += p->linesize[1];
246 ptr2 += p->linesize[2];
249 return s->bytestream - s->bytestream_start;
252 static int pam_encode_frame(AVCodecContext *avctx, unsigned char *outbuf,
253 int buf_size, void *data)
255 PNMContext *s = avctx->priv_data;
256 AVFrame *pict = data;
257 AVFrame * const p = (AVFrame*)&s->picture;
258 int i, h, w, n, linesize, depth, maxval;
259 const char *tuple_type;
262 if (buf_size < avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height) + 200) {
263 av_log(avctx, AV_LOG_ERROR, "encoded frame too large\n");
268 p->pict_type = FF_I_TYPE;
271 s->bytestream_start =
272 s->bytestream = outbuf;
273 s->bytestream_end = outbuf+buf_size;
277 switch (avctx->pix_fmt) {
278 case PIX_FMT_MONOWHITE:
282 tuple_type = "BLACKANDWHITE";
288 tuple_type = "GRAYSCALE";
300 tuple_type = "RGB_ALPHA";
305 snprintf(s->bytestream, s->bytestream_end - s->bytestream,
306 "P7\nWIDTH %d\nHEIGHT %d\nDEPTH %d\nMAXVAL %d\nTUPLETYPE %s\nENDHDR\n",
307 w, h, depth, maxval, tuple_type);
308 s->bytestream += strlen(s->bytestream);
311 linesize = p->linesize[0];
313 if (avctx->pix_fmt == PIX_FMT_RGB32) {
317 for (i = 0; i < h; i++) {
318 for (j = 0; j < w; j++) {
319 v = ((uint32_t *)ptr)[j];
320 bytestream_put_be24(&s->bytestream, v);
321 *s->bytestream++ = v >> 24;
326 for (i = 0; i < h; i++) {
327 memcpy(s->bytestream, ptr, n);
332 return s->bytestream - s->bytestream_start;
335 static av_cold int common_end(AVCodecContext *avctx)
337 PNMContext *s = avctx->priv_data;
339 if (s->picture.data[0])
340 avctx->release_buffer(avctx, &s->picture);
346 #if CONFIG_PGM_DECODER
347 AVCodec pgm_decoder = {
357 .pix_fmts = (const enum PixelFormat[]){PIX_FMT_GRAY8, PIX_FMT_GRAY16BE, PIX_FMT_NONE},
358 .long_name = NULL_IF_CONFIG_SMALL("PGM (Portable GrayMap) image"),
362 #if CONFIG_PGM_ENCODER
363 AVCodec pgm_encoder = {
370 .pix_fmts = (const enum PixelFormat[]){PIX_FMT_GRAY8, PIX_FMT_GRAY16BE, PIX_FMT_NONE},
371 .long_name = NULL_IF_CONFIG_SMALL("PGM (Portable GrayMap) image"),
375 #if CONFIG_PGMYUV_DECODER
376 AVCodec pgmyuv_decoder = {
386 .pix_fmts = (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_NONE},
387 .long_name = NULL_IF_CONFIG_SMALL("PGMYUV (Portable GrayMap YUV) image"),
391 #if CONFIG_PGMYUV_ENCODER
392 AVCodec pgmyuv_encoder = {
399 .pix_fmts = (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_NONE},
400 .long_name = NULL_IF_CONFIG_SMALL("PGMYUV (Portable GrayMap YUV) image"),
404 #if CONFIG_PPM_DECODER
405 AVCodec ppm_decoder = {
415 .pix_fmts = (const enum PixelFormat[]){PIX_FMT_RGB24, PIX_FMT_RGB48BE, PIX_FMT_NONE},
416 .long_name = NULL_IF_CONFIG_SMALL("PPM (Portable PixelMap) image"),
420 #if CONFIG_PPM_ENCODER
421 AVCodec ppm_encoder = {
428 .pix_fmts = (const enum PixelFormat[]){PIX_FMT_RGB24, PIX_FMT_RGB48BE, PIX_FMT_NONE},
429 .long_name = NULL_IF_CONFIG_SMALL("PPM (Portable PixelMap) image"),
433 #if CONFIG_PBM_DECODER
434 AVCodec pbm_decoder = {
444 .pix_fmts = (const enum PixelFormat[]){PIX_FMT_MONOWHITE, PIX_FMT_NONE},
445 .long_name = NULL_IF_CONFIG_SMALL("PBM (Portable BitMap) image"),
449 #if CONFIG_PBM_ENCODER
450 AVCodec pbm_encoder = {
457 .pix_fmts = (const enum PixelFormat[]){PIX_FMT_MONOWHITE, PIX_FMT_NONE},
458 .long_name = NULL_IF_CONFIG_SMALL("PBM (Portable BitMap) image"),
462 #if CONFIG_PAM_DECODER
463 AVCodec pam_decoder = {
473 .pix_fmts = (const enum PixelFormat[]){PIX_FMT_RGB24, PIX_FMT_RGB32, PIX_FMT_GRAY8, PIX_FMT_MONOWHITE, PIX_FMT_NONE},
474 .long_name = NULL_IF_CONFIG_SMALL("PAM (Portable AnyMap) image"),
478 #if CONFIG_PAM_ENCODER
479 AVCodec pam_encoder = {
486 .pix_fmts = (const enum PixelFormat[]){PIX_FMT_RGB24, PIX_FMT_RGB32, PIX_FMT_GRAY8, PIX_FMT_MONOWHITE, PIX_FMT_NONE},
487 .long_name = NULL_IF_CONFIG_SMALL("PAM (Portable AnyMap) image"),