| 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 "config_components.h" | ||
| 23 | |||
| 24 | #include "libavutil/intreadwrite.h" | ||
| 25 | #include "libavutil/imgutils.h" | ||
| 26 | #include "libavutil/pixdesc.h" | ||
| 27 | #include "libavutil/float2half.h" | ||
| 28 | #include "libavutil/intfloat.h" | ||
| 29 | #include "avcodec.h" | ||
| 30 | #include "codec_internal.h" | ||
| 31 | #include "encode.h" | ||
| 32 | |||
| 33 | typedef struct PHMEncContext { | ||
| 34 | Float2HalfTables f2h_tables; | ||
| 35 | } PHMEncContext; | ||
| 36 | |||
| 37 | 153 | static int pnm_encode_frame(AVCodecContext *avctx, AVPacket *pkt, | |
| 38 | const AVFrame *p, int *got_packet) | ||
| 39 | { | ||
| 40 | 153 | PHMEncContext *s = avctx->priv_data; | |
| 41 | uint8_t *bytestream, *bytestream_start, *bytestream_end; | ||
| 42 | int i, h, h1, c, n, linesize, ret; | ||
| 43 | 153 | int size = av_image_get_buffer_size(avctx->pix_fmt, | |
| 44 | avctx->width, avctx->height, 1); | ||
| 45 | |||
| 46 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 153 times.
|
153 | if (size < 0) |
| 47 | ✗ | return size; | |
| 48 | |||
| 49 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 153 times.
|
153 | if ((ret = ff_get_encode_buffer(avctx, pkt, size + 200U, 0)) < 0) |
| 50 | ✗ | return ret; | |
| 51 | |||
| 52 | 153 | bytestream_start = | |
| 53 | 153 | bytestream = pkt->data; | |
| 54 | 153 | bytestream_end = pkt->data + pkt->size; | |
| 55 | |||
| 56 | 153 | h = avctx->height; | |
| 57 | 153 | h1 = h; | |
| 58 |
5/10✓ 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 taken 26 times.
✓ Branch 8 taken 26 times.
✗ Branch 9 not taken.
|
153 | switch (avctx->pix_fmt) { |
| 59 | 25 | case AV_PIX_FMT_MONOWHITE: | |
| 60 | 25 | c = '4'; | |
| 61 | 25 | n = (avctx->width + 7) >> 3; | |
| 62 | 25 | break; | |
| 63 | 38 | case AV_PIX_FMT_GRAY8: | |
| 64 | 38 | c = '5'; | |
| 65 | 38 | n = avctx->width; | |
| 66 | 38 | break; | |
| 67 | ✗ | case AV_PIX_FMT_GRAY16BE: | |
| 68 | ✗ | c = '5'; | |
| 69 | ✗ | n = avctx->width * 2; | |
| 70 | ✗ | break; | |
| 71 | 38 | case AV_PIX_FMT_RGB24: | |
| 72 | 38 | c = '6'; | |
| 73 | 38 | n = avctx->width * 3; | |
| 74 | 38 | break; | |
| 75 | ✗ | case AV_PIX_FMT_RGB48BE: | |
| 76 | ✗ | c = '6'; | |
| 77 | ✗ | n = avctx->width * 6; | |
| 78 | ✗ | break; | |
| 79 | ✗ | case AV_PIX_FMT_YUV420P: | |
| 80 | ✗ | if (avctx->width & 1 || avctx->height & 1) { | |
| 81 | ✗ | av_log(avctx, AV_LOG_ERROR, "pgmyuv needs even width and height\n"); | |
| 82 | ✗ | return AVERROR(EINVAL); | |
| 83 | } | ||
| 84 | ✗ | c = '5'; | |
| 85 | ✗ | n = avctx->width; | |
| 86 | ✗ | h1 = (h * 3) / 2; | |
| 87 | ✗ | break; | |
| 88 | ✗ | case AV_PIX_FMT_YUV420P16BE: | |
| 89 | ✗ | c = '5'; | |
| 90 | ✗ | n = avctx->width * 2; | |
| 91 | ✗ | h1 = (h * 3) / 2; | |
| 92 | ✗ | break; | |
| 93 | 26 | case AV_PIX_FMT_GBRPF32BE: | |
| 94 | case AV_PIX_FMT_GBRPF32LE: | ||
| 95 |
1/2✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
|
26 | if (avctx->codec_id == AV_CODEC_ID_PFM) { |
| 96 | 26 | c = 'F'; | |
| 97 | 26 | n = avctx->width * 4; | |
| 98 | } else { | ||
| 99 | ✗ | c = 'H'; | |
| 100 | ✗ | n = avctx->width * 2; | |
| 101 | } | ||
| 102 | 26 | break; | |
| 103 | 26 | case AV_PIX_FMT_GRAYF32BE: | |
| 104 | case AV_PIX_FMT_GRAYF32LE: | ||
| 105 |
1/2✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
|
26 | if (avctx->codec_id == AV_CODEC_ID_PFM) { |
| 106 | 26 | c = 'f'; | |
| 107 | 26 | n = avctx->width * 4; | |
| 108 | } else { | ||
| 109 | ✗ | c = 'h'; | |
| 110 | ✗ | n = avctx->width * 2; | |
| 111 | } | ||
| 112 | 26 | break; | |
| 113 | ✗ | default: | |
| 114 | ✗ | return -1; | |
| 115 | } | ||
| 116 | 153 | snprintf(bytestream, bytestream_end - bytestream, | |
| 117 | "P%c\n%d %d\n", c, avctx->width, h1); | ||
| 118 | 153 | bytestream += strlen(bytestream); | |
| 119 |
2/2✓ Branch 0 taken 140 times.
✓ Branch 1 taken 13 times.
|
153 | if (avctx->pix_fmt == AV_PIX_FMT_GBRPF32LE || |
| 120 |
2/2✓ Branch 0 taken 127 times.
✓ Branch 1 taken 13 times.
|
140 | avctx->pix_fmt == AV_PIX_FMT_GRAYF32LE || |
| 121 |
2/2✓ Branch 0 taken 114 times.
✓ Branch 1 taken 13 times.
|
127 | avctx->pix_fmt == AV_PIX_FMT_GBRPF32BE || |
| 122 |
2/2✓ Branch 0 taken 13 times.
✓ Branch 1 taken 101 times.
|
114 | avctx->pix_fmt == AV_PIX_FMT_GRAYF32BE) |
| 123 | 52 | snprintf(bytestream, bytestream_end - bytestream, | |
| 124 |
2/2✓ Branch 0 taken 39 times.
✓ Branch 1 taken 13 times.
|
52 | "%f\n", (avctx->pix_fmt == AV_PIX_FMT_GBRPF32BE || |
| 125 |
2/2✓ Branch 0 taken 13 times.
✓ Branch 1 taken 26 times.
|
39 | avctx->pix_fmt == AV_PIX_FMT_GRAYF32BE) ? 1.f: -1.f); |
| 126 | 153 | bytestream += strlen(bytestream); | |
| 127 |
2/2✓ Branch 0 taken 128 times.
✓ Branch 1 taken 25 times.
|
153 | if (avctx->pix_fmt != AV_PIX_FMT_MONOWHITE && |
| 128 |
2/2✓ Branch 0 taken 115 times.
✓ Branch 1 taken 13 times.
|
128 | avctx->pix_fmt != AV_PIX_FMT_GBRPF32LE && |
| 129 |
2/2✓ Branch 0 taken 102 times.
✓ Branch 1 taken 13 times.
|
115 | avctx->pix_fmt != AV_PIX_FMT_GRAYF32LE && |
| 130 |
2/2✓ Branch 0 taken 89 times.
✓ Branch 1 taken 13 times.
|
102 | avctx->pix_fmt != AV_PIX_FMT_GBRPF32BE && |
| 131 |
2/2✓ Branch 0 taken 76 times.
✓ Branch 1 taken 13 times.
|
89 | avctx->pix_fmt != AV_PIX_FMT_GRAYF32BE) { |
| 132 | 76 | int maxdepth = (1 << av_pix_fmt_desc_get(avctx->pix_fmt)->comp[0].depth) - 1; | |
| 133 | 76 | snprintf(bytestream, bytestream_end - bytestream, | |
| 134 | "%d\n", maxdepth); | ||
| 135 | 76 | bytestream += strlen(bytestream); | |
| 136 | } | ||
| 137 | |||
| 138 |
2/2✓ Branch 0 taken 140 times.
✓ Branch 1 taken 13 times.
|
153 | if ((avctx->pix_fmt == AV_PIX_FMT_GBRPF32LE || |
| 139 |
3/4✓ Branch 0 taken 13 times.
✓ Branch 1 taken 127 times.
✓ Branch 2 taken 26 times.
✗ Branch 3 not taken.
|
179 | avctx->pix_fmt == AV_PIX_FMT_GBRPF32BE) && c == 'F') { |
| 140 | /* PFM is encoded from bottom to top */ | ||
| 141 | 26 | const float *r = (const float *)(p->data[2] + p->linesize[2] * (avctx->height - 1)); | |
| 142 | 26 | const float *g = (const float *)(p->data[0] + p->linesize[0] * (avctx->height - 1)); | |
| 143 | 26 | const float *b = (const float *)(p->data[1] + p->linesize[1] * (avctx->height - 1)); | |
| 144 | |||
| 145 |
2/2✓ Branch 0 taken 7488 times.
✓ Branch 1 taken 26 times.
|
7514 | for (int i = 0; i < avctx->height; i++) { |
| 146 |
2/2✓ Branch 0 taken 2635776 times.
✓ Branch 1 taken 7488 times.
|
2643264 | for (int j = 0; j < avctx->width; j++) { |
| 147 | 2635776 | AV_WN32(bytestream + 0, av_float2int(r[j])); | |
| 148 | 2635776 | AV_WN32(bytestream + 4, av_float2int(g[j])); | |
| 149 | 2635776 | AV_WN32(bytestream + 8, av_float2int(b[j])); | |
| 150 | 2635776 | bytestream += 12; | |
| 151 | } | ||
| 152 | |||
| 153 | 7488 | r -= p->linesize[2] / 4; | |
| 154 | 7488 | g -= p->linesize[0] / 4; | |
| 155 | 7488 | b -= p->linesize[1] / 4; | |
| 156 | } | ||
| 157 |
2/2✓ Branch 0 taken 114 times.
✓ Branch 1 taken 13 times.
|
127 | } else if ((avctx->pix_fmt == AV_PIX_FMT_GRAYF32LE || |
| 158 |
3/4✓ Branch 0 taken 13 times.
✓ Branch 1 taken 101 times.
✓ Branch 2 taken 26 times.
✗ Branch 3 not taken.
|
153 | avctx->pix_fmt == AV_PIX_FMT_GRAYF32BE) && c == 'f') { |
| 159 | /* PFM is encoded from bottom to top */ | ||
| 160 | 26 | const float *g = (const float *)(p->data[0] + p->linesize[0] * (avctx->height - 1)); | |
| 161 | |||
| 162 |
2/2✓ Branch 0 taken 7488 times.
✓ Branch 1 taken 26 times.
|
7514 | for (int i = 0; i < avctx->height; i++) { |
| 163 |
2/2✓ Branch 0 taken 2635776 times.
✓ Branch 1 taken 7488 times.
|
2643264 | for (int j = 0; j < avctx->width; j++) { |
| 164 | 2635776 | AV_WN32(bytestream, av_float2int(g[j])); | |
| 165 | 2635776 | bytestream += 4; | |
| 166 | } | ||
| 167 | |||
| 168 | 7488 | g -= p->linesize[0] / 4; | |
| 169 | } | ||
| 170 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 101 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
101 | } else if (avctx->pix_fmt == AV_PIX_FMT_GBRPF32 && c == 'H') { |
| 171 | ✗ | const float *r = (const float *)p->data[2]; | |
| 172 | ✗ | const float *g = (const float *)p->data[0]; | |
| 173 | ✗ | const float *b = (const float *)p->data[1]; | |
| 174 | |||
| 175 | ✗ | for (int i = 0; i < avctx->height; i++) { | |
| 176 | ✗ | for (int j = 0; j < avctx->width; j++) { | |
| 177 | ✗ | AV_WN16(bytestream + 0, float2half(av_float2int(r[j]), &s->f2h_tables)); | |
| 178 | ✗ | AV_WN16(bytestream + 2, float2half(av_float2int(g[j]), &s->f2h_tables)); | |
| 179 | ✗ | AV_WN16(bytestream + 4, float2half(av_float2int(b[j]), &s->f2h_tables)); | |
| 180 | ✗ | bytestream += 6; | |
| 181 | } | ||
| 182 | |||
| 183 | ✗ | r += p->linesize[2] / 4; | |
| 184 | ✗ | g += p->linesize[0] / 4; | |
| 185 | ✗ | b += p->linesize[1] / 4; | |
| 186 | } | ||
| 187 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 101 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
101 | } else if (avctx->pix_fmt == AV_PIX_FMT_GRAYF32 && c == 'h') { |
| 188 | ✗ | const float *g = (const float *)p->data[0]; | |
| 189 | |||
| 190 | ✗ | for (int i = 0; i < avctx->height; i++) { | |
| 191 | ✗ | for (int j = 0; j < avctx->width; j++) { | |
| 192 | ✗ | AV_WN16(bytestream, float2half(av_float2int(g[j]), &s->f2h_tables)); | |
| 193 | ✗ | bytestream += 2; | |
| 194 | } | ||
| 195 | |||
| 196 | ✗ | g += p->linesize[0] / 4; | |
| 197 | } | ||
| 198 | } else { | ||
| 199 | 101 | const uint8_t *ptr = p->data[0]; | |
| 200 | 101 | linesize = p->linesize[0]; | |
| 201 |
2/2✓ Branch 0 taken 29088 times.
✓ Branch 1 taken 101 times.
|
29189 | for (i = 0; i < h; i++) { |
| 202 | 29088 | memcpy(bytestream, ptr, n); | |
| 203 | 29088 | bytestream += n; | |
| 204 | 29088 | ptr += linesize; | |
| 205 | } | ||
| 206 | } | ||
| 207 | |||
| 208 |
2/4✓ Branch 0 taken 153 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 153 times.
|
153 | if (avctx->pix_fmt == AV_PIX_FMT_YUV420P || avctx->pix_fmt == AV_PIX_FMT_YUV420P16BE) { |
| 209 | ✗ | const uint8_t *ptr1 = p->data[1], *ptr2 = p->data[2]; | |
| 210 | ✗ | h >>= 1; | |
| 211 | ✗ | n >>= 1; | |
| 212 | ✗ | for (i = 0; i < h; i++) { | |
| 213 | ✗ | memcpy(bytestream, ptr1, n); | |
| 214 | ✗ | bytestream += n; | |
| 215 | ✗ | memcpy(bytestream, ptr2, n); | |
| 216 | ✗ | bytestream += n; | |
| 217 | ✗ | ptr1 += p->linesize[1]; | |
| 218 | ✗ | ptr2 += p->linesize[2]; | |
| 219 | } | ||
| 220 | } | ||
| 221 | 153 | av_shrink_packet(pkt, bytestream - bytestream_start); | |
| 222 | 153 | *got_packet = 1; | |
| 223 | |||
| 224 | 153 | return 0; | |
| 225 | } | ||
| 226 | |||
| 227 | #if CONFIG_PGM_ENCODER | ||
| 228 | const FFCodec ff_pgm_encoder = { | ||
| 229 | .p.name = "pgm", | ||
| 230 | CODEC_LONG_NAME("PGM (Portable GrayMap) image"), | ||
| 231 | .p.type = AVMEDIA_TYPE_VIDEO, | ||
| 232 | .p.id = AV_CODEC_ID_PGM, | ||
| 233 | .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE, | ||
| 234 | FF_CODEC_ENCODE_CB(pnm_encode_frame), | ||
| 235 | CODEC_PIXFMTS(AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY16BE), | ||
| 236 | }; | ||
| 237 | #endif | ||
| 238 | |||
| 239 | #if CONFIG_PGMYUV_ENCODER | ||
| 240 | const FFCodec ff_pgmyuv_encoder = { | ||
| 241 | .p.name = "pgmyuv", | ||
| 242 | CODEC_LONG_NAME("PGMYUV (Portable GrayMap YUV) image"), | ||
| 243 | .p.type = AVMEDIA_TYPE_VIDEO, | ||
| 244 | .p.id = AV_CODEC_ID_PGMYUV, | ||
| 245 | .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE, | ||
| 246 | FF_CODEC_ENCODE_CB(pnm_encode_frame), | ||
| 247 | CODEC_PIXFMTS(AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV420P16BE), | ||
| 248 | }; | ||
| 249 | #endif | ||
| 250 | |||
| 251 | #if CONFIG_PPM_ENCODER | ||
| 252 | const FFCodec ff_ppm_encoder = { | ||
| 253 | .p.name = "ppm", | ||
| 254 | CODEC_LONG_NAME("PPM (Portable PixelMap) image"), | ||
| 255 | .p.type = AVMEDIA_TYPE_VIDEO, | ||
| 256 | .p.id = AV_CODEC_ID_PPM, | ||
| 257 | .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE, | ||
| 258 | FF_CODEC_ENCODE_CB(pnm_encode_frame), | ||
| 259 | CODEC_PIXFMTS(AV_PIX_FMT_RGB24, AV_PIX_FMT_RGB48BE), | ||
| 260 | }; | ||
| 261 | #endif | ||
| 262 | |||
| 263 | #if CONFIG_PBM_ENCODER | ||
| 264 | const FFCodec ff_pbm_encoder = { | ||
| 265 | .p.name = "pbm", | ||
| 266 | CODEC_LONG_NAME("PBM (Portable BitMap) image"), | ||
| 267 | .p.type = AVMEDIA_TYPE_VIDEO, | ||
| 268 | .p.id = AV_CODEC_ID_PBM, | ||
| 269 | .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE, | ||
| 270 | FF_CODEC_ENCODE_CB(pnm_encode_frame), | ||
| 271 | CODEC_PIXFMTS(AV_PIX_FMT_MONOWHITE), | ||
| 272 | }; | ||
| 273 | #endif | ||
| 274 | |||
| 275 | #if CONFIG_PFM_ENCODER | ||
| 276 | const FFCodec ff_pfm_encoder = { | ||
| 277 | .p.name = "pfm", | ||
| 278 | CODEC_LONG_NAME("PFM (Portable FloatMap) image"), | ||
| 279 | .p.type = AVMEDIA_TYPE_VIDEO, | ||
| 280 | .p.id = AV_CODEC_ID_PFM, | ||
| 281 | .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE, | ||
| 282 | FF_CODEC_ENCODE_CB(pnm_encode_frame), | ||
| 283 | CODEC_PIXFMTS(AV_PIX_FMT_GBRPF32LE, AV_PIX_FMT_GRAYF32LE, | ||
| 284 | AV_PIX_FMT_GBRPF32BE, AV_PIX_FMT_GRAYF32BE), | ||
| 285 | }; | ||
| 286 | #endif | ||
| 287 | |||
| 288 | #if CONFIG_PHM_ENCODER | ||
| 289 | ✗ | static av_cold int phm_enc_init(AVCodecContext *avctx) | |
| 290 | { | ||
| 291 | ✗ | PHMEncContext *s = avctx->priv_data; | |
| 292 | |||
| 293 | ✗ | ff_init_float2half_tables(&s->f2h_tables); | |
| 294 | |||
| 295 | ✗ | return 0; | |
| 296 | } | ||
| 297 | |||
| 298 | const FFCodec ff_phm_encoder = { | ||
| 299 | .p.name = "phm", | ||
| 300 | CODEC_LONG_NAME("PHM (Portable HalfFloatMap) image"), | ||
| 301 | .p.type = AVMEDIA_TYPE_VIDEO, | ||
| 302 | .p.id = AV_CODEC_ID_PHM, | ||
| 303 | .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE, | ||
| 304 | .priv_data_size = sizeof(PHMEncContext), | ||
| 305 | .init = phm_enc_init, | ||
| 306 | FF_CODEC_ENCODE_CB(pnm_encode_frame), | ||
| 307 | CODEC_PIXFMTS(AV_PIX_FMT_GBRPF32, AV_PIX_FMT_GRAYF32), | ||
| 308 | }; | ||
| 309 | #endif | ||
| 310 |