FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/pamenc.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 58 79 73.4%
Functions: 1 1 100.0%
Branches: 15 20 75.0%

Line Branch Exec Source
1 /*
2 * PAM image format
3 * Copyright (c) 2002, 2003 Fabrice Bellard
4 *
5 * This file is part of FFmpeg.
6 *
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.
11 *
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.
16 *
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
20 */
21
22 #include "libavutil/avassert.h"
23 #include "avcodec.h"
24 #include "codec_internal.h"
25 #include "encode.h"
26
27 78 static int pam_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
28 const AVFrame *p, int *got_packet)
29 {
30 int i, h, w, n, linesize, depth, maxval, ret, header_size;
31 uint8_t *bytestream;
32 const uint8_t *ptr;
33 const char *tuple_type;
34 char header[100];
35
36 78 h = avctx->height;
37 78 w = avctx->width;
38
6/10
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 13 times.
✓ Branch 2 taken 13 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 13 times.
✓ Branch 6 taken 13 times.
✓ Branch 7 taken 13 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
78 switch (avctx->pix_fmt) {
39 13 case AV_PIX_FMT_MONOBLACK:
40 13 n = w;
41 13 depth = 1;
42 13 maxval = 1;
43 13 tuple_type = "BLACKANDWHITE";
44 13 break;
45 13 case AV_PIX_FMT_GRAY8:
46 13 n = w;
47 13 depth = 1;
48 13 maxval = 255;
49 13 tuple_type = "GRAYSCALE";
50 13 break;
51 13 case AV_PIX_FMT_GRAY16BE:
52 13 n = w * 2;
53 13 depth = 1;
54 13 maxval = 0xFFFF;
55 13 tuple_type = "GRAYSCALE";
56 13 break;
57 case AV_PIX_FMT_GRAY8A:
58 n = w * 2;
59 depth = 2;
60 maxval = 255;
61 tuple_type = "GRAYSCALE_ALPHA";
62 break;
63 case AV_PIX_FMT_YA16BE:
64 n = w * 4;
65 depth = 2;
66 maxval = 0xFFFF;
67 tuple_type = "GRAYSCALE_ALPHA";
68 break;
69 13 case AV_PIX_FMT_RGB24:
70 13 n = w * 3;
71 13 depth = 3;
72 13 maxval = 255;
73 13 tuple_type = "RGB";
74 13 break;
75 13 case AV_PIX_FMT_RGBA:
76 13 n = w * 4;
77 13 depth = 4;
78 13 maxval = 255;
79 13 tuple_type = "RGB_ALPHA";
80 13 break;
81 13 case AV_PIX_FMT_RGB48BE:
82 13 n = w * 6;
83 13 depth = 3;
84 13 maxval = 0xFFFF;
85 13 tuple_type = "RGB";
86 13 break;
87 case AV_PIX_FMT_RGBA64BE:
88 n = w * 8;
89 depth = 4;
90 maxval = 0xFFFF;
91 tuple_type = "RGB_ALPHA";
92 break;
93 default:
94 return -1;
95 }
96
97 78 header_size = snprintf(header, sizeof(header),
98 "P7\nWIDTH %d\nHEIGHT %d\nDEPTH %d\nMAXVAL %d\nTUPLTYPE %s\nENDHDR\n",
99 w, h, depth, maxval, tuple_type);
100 av_assert1(header_size < sizeof(header));
101
102
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 78 times.
78 if ((ret = ff_get_encode_buffer(avctx, pkt, n*h + header_size, 0)) < 0)
103 return ret;
104
105 78 bytestream = pkt->data;
106 78 memcpy(bytestream, header, header_size);
107 78 bytestream += header_size;
108
109 78 ptr = p->data[0];
110 78 linesize = p->linesize[0];
111
112
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 65 times.
78 if (avctx->pix_fmt == AV_PIX_FMT_MONOBLACK){
113 int j;
114
2/2
✓ Branch 0 taken 3744 times.
✓ Branch 1 taken 13 times.
3757 for (i = 0; i < h; i++) {
115
2/2
✓ Branch 0 taken 1317888 times.
✓ Branch 1 taken 3744 times.
1321632 for (j = 0; j < w; j++)
116 1317888 *bytestream++ = ptr[j >> 3] >> (7 - j & 7) & 1;
117 3744 ptr += linesize;
118 }
119 } else {
120
2/2
✓ Branch 0 taken 18720 times.
✓ Branch 1 taken 65 times.
18785 for (i = 0; i < h; i++) {
121 18720 memcpy(bytestream, ptr, n);
122 18720 bytestream += n;
123 18720 ptr += linesize;
124 }
125 }
126
127 78 *got_packet = 1;
128 78 return 0;
129 }
130
131 const FFCodec ff_pam_encoder = {
132 .p.name = "pam",
133 CODEC_LONG_NAME("PAM (Portable AnyMap) image"),
134 .p.type = AVMEDIA_TYPE_VIDEO,
135 .p.id = AV_CODEC_ID_PAM,
136 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE,
137 FF_CODEC_ENCODE_CB(pam_encode_frame),
138 .p.pix_fmts = (const enum AVPixelFormat[]){
139 AV_PIX_FMT_RGB24, AV_PIX_FMT_RGBA,
140 AV_PIX_FMT_RGB48BE, AV_PIX_FMT_RGBA64BE,
141 AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY8A,
142 AV_PIX_FMT_GRAY16BE, AV_PIX_FMT_YA16BE,
143 AV_PIX_FMT_MONOBLACK, AV_PIX_FMT_NONE
144 },
145 };
146