FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/pamenc.c
Date: 2021-09-24 03:35:22
Exec Total Coverage
Lines: 59 80 73.8%
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 "encode.h"
25 #include "internal.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, *ptr;
32 const char *tuple_type;
33 char header[100];
34
35 78 h = avctx->height;
36 78 w = avctx->width;
37
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) {
38 13 case AV_PIX_FMT_MONOBLACK:
39 13 n = w;
40 13 depth = 1;
41 13 maxval = 1;
42 13 tuple_type = "BLACKANDWHITE";
43 13 break;
44 13 case AV_PIX_FMT_GRAY8:
45 13 n = w;
46 13 depth = 1;
47 13 maxval = 255;
48 13 tuple_type = "GRAYSCALE";
49 13 break;
50 13 case AV_PIX_FMT_GRAY16BE:
51 13 n = w * 2;
52 13 depth = 1;
53 13 maxval = 0xFFFF;
54 13 tuple_type = "GRAYSCALE";
55 13 break;
56 case AV_PIX_FMT_GRAY8A:
57 n = w * 2;
58 depth = 2;
59 maxval = 255;
60 tuple_type = "GRAYSCALE_ALPHA";
61 break;
62 case AV_PIX_FMT_YA16BE:
63 n = w * 4;
64 depth = 2;
65 maxval = 0xFFFF;
66 tuple_type = "GRAYSCALE_ALPHA";
67 break;
68 13 case AV_PIX_FMT_RGB24:
69 13 n = w * 3;
70 13 depth = 3;
71 13 maxval = 255;
72 13 tuple_type = "RGB";
73 13 break;
74 13 case AV_PIX_FMT_RGBA:
75 13 n = w * 4;
76 13 depth = 4;
77 13 maxval = 255;
78 13 tuple_type = "RGB_ALPHA";
79 13 break;
80 13 case AV_PIX_FMT_RGB48BE:
81 13 n = w * 6;
82 13 depth = 3;
83 13 maxval = 0xFFFF;
84 13 tuple_type = "RGB";
85 13 break;
86 case AV_PIX_FMT_RGBA64BE:
87 n = w * 8;
88 depth = 4;
89 maxval = 0xFFFF;
90 tuple_type = "RGB_ALPHA";
91 break;
92 default:
93 return -1;
94 }
95
96 78 header_size = snprintf(header, sizeof(header),
97 "P7\nWIDTH %d\nHEIGHT %d\nDEPTH %d\nMAXVAL %d\nTUPLTYPE %s\nENDHDR\n",
98 w, h, depth, maxval, tuple_type);
99 av_assert1(header_size < sizeof(header));
100
101
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)
102 return ret;
103
104 78 bytestream = pkt->data;
105 78 memcpy(bytestream, header, header_size);
106 78 bytestream += header_size;
107
108 78 ptr = p->data[0];
109 78 linesize = p->linesize[0];
110
111
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 65 times.
78 if (avctx->pix_fmt == AV_PIX_FMT_MONOBLACK){
112 int j;
113
2/2
✓ Branch 0 taken 3744 times.
✓ Branch 1 taken 13 times.
3757 for (i = 0; i < h; i++) {
114
2/2
✓ Branch 0 taken 1317888 times.
✓ Branch 1 taken 3744 times.
1321632 for (j = 0; j < w; j++)
115 1317888 *bytestream++ = ptr[j >> 3] >> (7 - j & 7) & 1;
116 3744 ptr += linesize;
117 }
118 } else {
119
2/2
✓ Branch 0 taken 18720 times.
✓ Branch 1 taken 65 times.
18785 for (i = 0; i < h; i++) {
120 18720 memcpy(bytestream, ptr, n);
121 18720 bytestream += n;
122 18720 ptr += linesize;
123 }
124 }
125
126 78 pkt->flags |= AV_PKT_FLAG_KEY;
127 78 *got_packet = 1;
128 78 return 0;
129 }
130
131 const AVCodec ff_pam_encoder = {
132 .name = "pam",
133 .long_name = NULL_IF_CONFIG_SMALL("PAM (Portable AnyMap) image"),
134 .type = AVMEDIA_TYPE_VIDEO,
135 .id = AV_CODEC_ID_PAM,
136 .capabilities = AV_CODEC_CAP_DR1,
137 .encode2 = pam_encode_frame,
138 .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 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
146 };
147