FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/pnmenc.c
Date: 2021-09-22 05:58:22
Exec Total Coverage
Lines: 42 95 44.2%
Branches: 13 37 35.1%

Line Branch Exec Source
1 /*
2 * PNM 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/intreadwrite.h"
23 #include "libavutil/imgutils.h"
24 #include "libavutil/pixdesc.h"
25 #include "avcodec.h"
26 #include "encode.h"
27 #include "internal.h"
28
29 101 static int pnm_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
30 const AVFrame *p, int *got_packet)
31 {
32 uint8_t *bytestream, *bytestream_start, *bytestream_end;
33 int i, h, h1, c, n, linesize, ret;
34 uint8_t *ptr, *ptr1, *ptr2;
35 101 int size = av_image_get_buffer_size(avctx->pix_fmt,
36 avctx->width, avctx->height, 1);
37
38
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 101 times.
101 if ((ret = ff_get_encode_buffer(avctx, pkt, size + 200, 0)) < 0)
39 return ret;
40
41 101 bytestream_start =
42 101 bytestream = pkt->data;
43 101 bytestream_end = pkt->data + pkt->size;
44
45 101 h = avctx->height;
46 101 h1 = h;
47
3/9
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 38 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 38 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
101 switch (avctx->pix_fmt) {
48 25 case AV_PIX_FMT_MONOWHITE:
49 25 c = '4';
50 25 n = (avctx->width + 7) >> 3;
51 25 break;
52 38 case AV_PIX_FMT_GRAY8:
53 38 c = '5';
54 38 n = avctx->width;
55 38 break;
56 case AV_PIX_FMT_GRAY16BE:
57 c = '5';
58 n = avctx->width * 2;
59 break;
60 38 case AV_PIX_FMT_RGB24:
61 38 c = '6';
62 38 n = avctx->width * 3;
63 38 break;
64 case AV_PIX_FMT_RGB48BE:
65 c = '6';
66 n = avctx->width * 6;
67 break;
68 case AV_PIX_FMT_YUV420P:
69 if (avctx->width & 1 || avctx->height & 1) {
70 av_log(avctx, AV_LOG_ERROR, "pgmyuv needs even width and height\n");
71 return AVERROR(EINVAL);
72 }
73 c = '5';
74 n = avctx->width;
75 h1 = (h * 3) / 2;
76 break;
77 case AV_PIX_FMT_YUV420P16BE:
78 c = '5';
79 n = avctx->width * 2;
80 h1 = (h * 3) / 2;
81 break;
82 case AV_PIX_FMT_GBRPF32:
83 c = 'F';
84 n = avctx->width * 4;
85 break;
86 default:
87 return -1;
88 }
89 101 snprintf(bytestream, bytestream_end - bytestream,
90 "P%c\n%d %d\n", c, avctx->width, h1);
91 101 bytestream += strlen(bytestream);
92
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 101 times.
101 if (avctx->pix_fmt == AV_PIX_FMT_GBRPF32)
93 snprintf(bytestream, bytestream_end - bytestream,
94 "%f\n", avctx->pix_fmt == AV_PIX_FMT_GBRPF32BE ? 1.f: -1.f);
95 101 bytestream += strlen(bytestream);
96
2/2
✓ Branch 0 taken 76 times.
✓ Branch 1 taken 25 times.
101 if (avctx->pix_fmt != AV_PIX_FMT_MONOWHITE &&
97
1/2
✓ Branch 0 taken 76 times.
✗ Branch 1 not taken.
76 avctx->pix_fmt != AV_PIX_FMT_GBRPF32) {
98 76 int maxdepth = (1 << av_pix_fmt_desc_get(avctx->pix_fmt)->comp[0].depth) - 1;
99 76 snprintf(bytestream, bytestream_end - bytestream,
100 "%d\n", maxdepth);
101 76 bytestream += strlen(bytestream);
102 }
103
104
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 101 times.
101 if (avctx->pix_fmt == AV_PIX_FMT_GBRPF32) {
105 float *r = (float *)p->data[2];
106 float *g = (float *)p->data[0];
107 float *b = (float *)p->data[1];
108
109 for (int i = 0; i < avctx->height; i++) {
110 for (int j = 0; j < avctx->width; j++) {
111 AV_WN32(bytestream + 0, av_float2int(r[j]));
112 AV_WN32(bytestream + 4, av_float2int(g[j]));
113 AV_WN32(bytestream + 8, av_float2int(b[j]));
114 bytestream += 12;
115 }
116
117 r += p->linesize[2] / 4;
118 g += p->linesize[0] / 4;
119 b += p->linesize[1] / 4;
120 }
121 } else {
122 101 ptr = p->data[0];
123 101 linesize = p->linesize[0];
124
2/2
✓ Branch 0 taken 29088 times.
✓ Branch 1 taken 101 times.
29189 for (i = 0; i < h; i++) {
125 29088 memcpy(bytestream, ptr, n);
126 29088 bytestream += n;
127 29088 ptr += linesize;
128 }
129 }
130
131
2/4
✓ Branch 0 taken 101 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 101 times.
101 if (avctx->pix_fmt == AV_PIX_FMT_YUV420P || avctx->pix_fmt == AV_PIX_FMT_YUV420P16BE) {
132 h >>= 1;
133 n >>= 1;
134 ptr1 = p->data[1];
135 ptr2 = p->data[2];
136 for (i = 0; i < h; i++) {
137 memcpy(bytestream, ptr1, n);
138 bytestream += n;
139 memcpy(bytestream, ptr2, n);
140 bytestream += n;
141 ptr1 += p->linesize[1];
142 ptr2 += p->linesize[2];
143 }
144 }
145 101 pkt->flags |= AV_PKT_FLAG_KEY;
146 101 av_shrink_packet(pkt, bytestream - bytestream_start);
147 101 *got_packet = 1;
148
149 101 return 0;
150 }
151
152 #if CONFIG_PGM_ENCODER
153 const AVCodec ff_pgm_encoder = {
154 .name = "pgm",
155 .long_name = NULL_IF_CONFIG_SMALL("PGM (Portable GrayMap) image"),
156 .type = AVMEDIA_TYPE_VIDEO,
157 .id = AV_CODEC_ID_PGM,
158 .capabilities = AV_CODEC_CAP_DR1,
159 .encode2 = pnm_encode_frame,
160 .pix_fmts = (const enum AVPixelFormat[]){
161 AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY16BE, AV_PIX_FMT_NONE
162 },
163 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
164 };
165 #endif
166
167 #if CONFIG_PGMYUV_ENCODER
168 const AVCodec ff_pgmyuv_encoder = {
169 .name = "pgmyuv",
170 .long_name = NULL_IF_CONFIG_SMALL("PGMYUV (Portable GrayMap YUV) image"),
171 .type = AVMEDIA_TYPE_VIDEO,
172 .id = AV_CODEC_ID_PGMYUV,
173 .capabilities = AV_CODEC_CAP_DR1,
174 .encode2 = pnm_encode_frame,
175 .pix_fmts = (const enum AVPixelFormat[]){
176 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV420P16BE, AV_PIX_FMT_NONE
177 },
178 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
179 };
180 #endif
181
182 #if CONFIG_PPM_ENCODER
183 const AVCodec ff_ppm_encoder = {
184 .name = "ppm",
185 .long_name = NULL_IF_CONFIG_SMALL("PPM (Portable PixelMap) image"),
186 .type = AVMEDIA_TYPE_VIDEO,
187 .id = AV_CODEC_ID_PPM,
188 .capabilities = AV_CODEC_CAP_DR1,
189 .encode2 = pnm_encode_frame,
190 .pix_fmts = (const enum AVPixelFormat[]){
191 AV_PIX_FMT_RGB24, AV_PIX_FMT_RGB48BE, AV_PIX_FMT_NONE
192 },
193 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
194 };
195 #endif
196
197 #if CONFIG_PBM_ENCODER
198 const AVCodec ff_pbm_encoder = {
199 .name = "pbm",
200 .long_name = NULL_IF_CONFIG_SMALL("PBM (Portable BitMap) image"),
201 .type = AVMEDIA_TYPE_VIDEO,
202 .id = AV_CODEC_ID_PBM,
203 .capabilities = AV_CODEC_CAP_DR1,
204 .encode2 = pnm_encode_frame,
205 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_MONOWHITE,
206 AV_PIX_FMT_NONE },
207 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
208 };
209 #endif
210
211 #if CONFIG_PFM_ENCODER
212 const AVCodec ff_pfm_encoder = {
213 .name = "pfm",
214 .long_name = NULL_IF_CONFIG_SMALL("PFM (Portable FloatMap) image"),
215 .type = AVMEDIA_TYPE_VIDEO,
216 .id = AV_CODEC_ID_PFM,
217 .capabilities = AV_CODEC_CAP_DR1,
218 .encode2 = pnm_encode_frame,
219 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_GBRPF32,
220 AV_PIX_FMT_NONE },
221 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
222 };
223 #endif
224