3 * Copyright (c) 2002, 2003 Fabrice Bellard
5 * This file is part of Libav.
7 * Libav 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 * Libav 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 Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "libavutil/imgutils.h"
25 #include "bytestream.h"
28 static int pam_encode_frame(AVCodecContext
*avctx
, AVPacket
*pkt
,
29 const AVFrame
*pict
, int *got_packet
)
31 uint8_t *bytestream_start
, *bytestream
, *bytestream_end
;
32 const AVFrame
* const p
= pict
;
33 int i
, h
, w
, n
, linesize
, depth
, maxval
, ret
;
34 const char *tuple_type
;
36 int size
= av_image_get_buffer_size(avctx
->pix_fmt
,
37 avctx
->width
, avctx
->height
, 1);
39 if ((ret
= ff_alloc_packet(pkt
, size
+ 200)) < 0) {
40 av_log(avctx
, AV_LOG_ERROR
, "encoded frame too large\n");
45 bytestream
= pkt
->data
;
46 bytestream_end
= pkt
->data
+ pkt
->size
;
50 switch (avctx
->pix_fmt
) {
51 case AV_PIX_FMT_MONOWHITE
:
55 tuple_type
= "BLACKANDWHITE";
57 case AV_PIX_FMT_GRAY8
:
61 tuple_type
= "GRAYSCALE";
63 case AV_PIX_FMT_RGB24
:
69 case AV_PIX_FMT_RGB32
:
73 tuple_type
= "RGB_ALPHA";
78 snprintf(bytestream
, bytestream_end
- bytestream
,
79 "P7\nWIDTH %d\nHEIGHT %d\nDEPTH %d\nMAXVAL %d\nTUPLTYPE %s\nENDHDR\n",
80 w
, h
, depth
, maxval
, tuple_type
);
81 bytestream
+= strlen(bytestream
);
84 linesize
= p
->linesize
[0];
86 if (avctx
->pix_fmt
== AV_PIX_FMT_RGB32
) {
90 for (i
= 0; i
< h
; i
++) {
91 for (j
= 0; j
< w
; j
++) {
92 v
= ((uint32_t *)ptr
)[j
];
93 bytestream_put_be24(&bytestream
, v
);
94 *bytestream
++ = v
>> 24;
99 for (i
= 0; i
< h
; i
++) {
100 memcpy(bytestream
, ptr
, n
);
106 pkt
->size
= bytestream
- bytestream_start
;
107 pkt
->flags
|= AV_PKT_FLAG_KEY
;
112 static av_cold
int pam_encode_init(AVCodecContext
*avctx
)
114 #if FF_API_CODED_FRAME
115 FF_DISABLE_DEPRECATION_WARNINGS
116 avctx
->coded_frame
->pict_type
= AV_PICTURE_TYPE_I
;
117 avctx
->coded_frame
->key_frame
= 1;
118 FF_ENABLE_DEPRECATION_WARNINGS
124 AVCodec ff_pam_encoder
= {
126 .long_name
= NULL_IF_CONFIG_SMALL("PAM (Portable AnyMap) image"),
127 .type
= AVMEDIA_TYPE_VIDEO
,
128 .id
= AV_CODEC_ID_PAM
,
129 .init
= pam_encode_init
,
130 .encode2
= pam_encode_frame
,
131 .pix_fmts
= (const enum AVPixelFormat
[]){
132 AV_PIX_FMT_RGB24
, AV_PIX_FMT_RGB32
, AV_PIX_FMT_GRAY8
, AV_PIX_FMT_MONOWHITE
,