9ac2e797 |
/*
* PNM image format |
406792e7 |
* Copyright (c) 2002, 2003 Fabrice Bellard |
9ac2e797 |
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/ |
d3067047 |
|
9ac2e797 |
#include "avcodec.h" |
1ea57550 |
#include "internal.h" |
9ac2e797 |
#include "pnm.h"
|
1ea57550 |
static int pnm_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
const AVFrame *pict, int *got_packet) |
d3067047 |
{
PNMContext *s = avctx->priv_data; |
562b6c74 |
AVFrame * const p = &s->picture; |
1ea57550 |
int i, h, h1, c, n, linesize, ret; |
9ac2e797 |
uint8_t *ptr, *ptr1, *ptr2;
|
6a74c48c |
if ((ret = ff_alloc_packet2(avctx, pkt, avpicture_get_size(avctx->pix_fmt, |
1ea57550 |
avctx->width, |
ae2c33b0 |
avctx->height) + 200)) < 0) |
1ea57550 |
return ret; |
9ac2e797 |
|
d3067047 |
*p = *pict; |
ce5e49b0 |
p->pict_type = AV_PICTURE_TYPE_I; |
d3067047 |
p->key_frame = 1; |
9ac2e797 |
|
d3067047 |
s->bytestream_start = |
1ea57550 |
s->bytestream = pkt->data;
s->bytestream_end = pkt->data + pkt->size; |
9ac2e797 |
|
d3067047 |
h = avctx->height; |
9ac2e797 |
h1 = h; |
d3067047 |
switch (avctx->pix_fmt) { |
716d413c |
case AV_PIX_FMT_MONOWHITE: |
d3067047 |
c = '4';
n = (avctx->width + 7) >> 3; |
9ac2e797 |
break; |
716d413c |
case AV_PIX_FMT_GRAY8: |
d3067047 |
c = '5';
n = avctx->width; |
9ac2e797 |
break; |
716d413c |
case AV_PIX_FMT_GRAY16BE: |
d3067047 |
c = '5';
n = avctx->width * 2; |
9ac2e797 |
break; |
716d413c |
case AV_PIX_FMT_RGB24: |
d3067047 |
c = '6';
n = avctx->width * 3; |
9ac2e797 |
break; |
716d413c |
case AV_PIX_FMT_RGB48BE: |
d3067047 |
c = '6';
n = avctx->width * 6; |
00182190 |
break; |
716d413c |
case AV_PIX_FMT_YUV420P: |
6ddb03ca |
if (avctx->width & 1 || avctx->height & 1) {
av_log(avctx, AV_LOG_ERROR, "pgmyuv needs even width and height\n"); |
a41340f8 |
return AVERROR(EINVAL);
} |
d3067047 |
c = '5';
n = avctx->width; |
9ac2e797 |
h1 = (h * 3) / 2;
break;
default:
return -1;
}
snprintf(s->bytestream, s->bytestream_end - s->bytestream, |
d3067047 |
"P%c\n%d %d\n", c, avctx->width, h1); |
9ac2e797 |
s->bytestream += strlen(s->bytestream); |
716d413c |
if (avctx->pix_fmt != AV_PIX_FMT_MONOWHITE) { |
9ac2e797 |
snprintf(s->bytestream, s->bytestream_end - s->bytestream, |
716d413c |
"%d\n", (avctx->pix_fmt != AV_PIX_FMT_GRAY16BE && avctx->pix_fmt != AV_PIX_FMT_RGB48BE) ? 255 : 65535); |
9ac2e797 |
s->bytestream += strlen(s->bytestream);
}
|
d3067047 |
ptr = p->data[0]; |
9ac2e797 |
linesize = p->linesize[0]; |
d3067047 |
for (i = 0; i < h; i++) { |
9ac2e797 |
memcpy(s->bytestream, ptr, n);
s->bytestream += n; |
d3067047 |
ptr += linesize; |
9ac2e797 |
}
|
716d413c |
if (avctx->pix_fmt == AV_PIX_FMT_YUV420P) { |
9ac2e797 |
h >>= 1;
n >>= 1;
ptr1 = p->data[1];
ptr2 = p->data[2]; |
d3067047 |
for (i = 0; i < h; i++) { |
9ac2e797 |
memcpy(s->bytestream, ptr1, n);
s->bytestream += n;
memcpy(s->bytestream, ptr2, n);
s->bytestream += n;
ptr1 += p->linesize[1];
ptr2 += p->linesize[2];
}
} |
1ea57550 |
pkt->size = s->bytestream - s->bytestream_start;
pkt->flags |= AV_PKT_FLAG_KEY;
*got_packet = 1;
return 0; |
9ac2e797 |
}
|
b250f9c6 |
#if CONFIG_PGM_ENCODER |
e7e2df27 |
AVCodec ff_pgm_encoder = { |
ec6402b7 |
.name = "pgm",
.type = AVMEDIA_TYPE_VIDEO, |
36ef5369 |
.id = AV_CODEC_ID_PGM, |
ec6402b7 |
.priv_data_size = sizeof(PNMContext),
.init = ff_pnm_init, |
1ea57550 |
.encode2 = pnm_encode_frame, |
716d413c |
.pix_fmts = (const enum AVPixelFormat[]){
AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY16BE, AV_PIX_FMT_NONE |
00c3b67b |
},
.long_name = NULL_IF_CONFIG_SMALL("PGM (Portable GrayMap) image"), |
9ac2e797 |
}; |
d3067047 |
#endif |
9ac2e797 |
|
b250f9c6 |
#if CONFIG_PGMYUV_ENCODER |
e7e2df27 |
AVCodec ff_pgmyuv_encoder = { |
ec6402b7 |
.name = "pgmyuv",
.type = AVMEDIA_TYPE_VIDEO, |
36ef5369 |
.id = AV_CODEC_ID_PGMYUV, |
ec6402b7 |
.priv_data_size = sizeof(PNMContext),
.init = ff_pnm_init, |
1ea57550 |
.encode2 = pnm_encode_frame, |
716d413c |
.pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE }, |
00c3b67b |
.long_name = NULL_IF_CONFIG_SMALL("PGMYUV (Portable GrayMap YUV) image"), |
9ac2e797 |
}; |
d3067047 |
#endif |
9ac2e797 |
|
b250f9c6 |
#if CONFIG_PPM_ENCODER |
e7e2df27 |
AVCodec ff_ppm_encoder = { |
ec6402b7 |
.name = "ppm",
.type = AVMEDIA_TYPE_VIDEO, |
36ef5369 |
.id = AV_CODEC_ID_PPM, |
ec6402b7 |
.priv_data_size = sizeof(PNMContext),
.init = ff_pnm_init, |
1ea57550 |
.encode2 = pnm_encode_frame, |
716d413c |
.pix_fmts = (const enum AVPixelFormat[]){
AV_PIX_FMT_RGB24, AV_PIX_FMT_RGB48BE, AV_PIX_FMT_NONE |
00c3b67b |
},
.long_name = NULL_IF_CONFIG_SMALL("PPM (Portable PixelMap) image"), |
9ac2e797 |
}; |
d3067047 |
#endif |
9ac2e797 |
|
b250f9c6 |
#if CONFIG_PBM_ENCODER |
e7e2df27 |
AVCodec ff_pbm_encoder = { |
ec6402b7 |
.name = "pbm",
.type = AVMEDIA_TYPE_VIDEO, |
36ef5369 |
.id = AV_CODEC_ID_PBM, |
ec6402b7 |
.priv_data_size = sizeof(PNMContext),
.init = ff_pnm_init, |
1ea57550 |
.encode2 = pnm_encode_frame, |
716d413c |
.pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_MONOWHITE,
AV_PIX_FMT_NONE }, |
00c3b67b |
.long_name = NULL_IF_CONFIG_SMALL("PBM (Portable BitMap) image"), |
9ac2e797 |
}; |
d3067047 |
#endif |