FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/aliaspixenc.c
Date: 2024-04-26 14:42:52
Exec Total Coverage
Lines: 41 46 89.1%
Functions: 1 1 100.0%
Branches: 22 29 75.9%

Line Branch Exec Source
1 /*
2 * Alias PIX image encoder
3 * Copyright (C) 2014 Vittorio Giovara <vittorio.giovara@gmail.com>
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
24 #include "avcodec.h"
25 #include "bytestream.h"
26 #include "codec_internal.h"
27 #include "encode.h"
28
29 #define ALIAS_HEADER_SIZE 10
30
31 2 static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
32 const AVFrame *frame, int *got_packet)
33 {
34 int width, height, bits_pixel, length, ret;
35 uint8_t *buf;
36
37 2 width = avctx->width;
38 2 height = avctx->height;
39
40
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 if (width > 65535 || height > 65535 ||
41
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 width * height >= INT_MAX / 4 - ALIAS_HEADER_SIZE) {
42 av_log(avctx, AV_LOG_ERROR, "Invalid image size %dx%d.\n", width, height);
43 return AVERROR_INVALIDDATA;
44 }
45
46
2/3
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 switch (avctx->pix_fmt) {
47 1 case AV_PIX_FMT_GRAY8:
48 1 bits_pixel = 8;
49 1 break;
50 1 case AV_PIX_FMT_BGR24:
51 1 bits_pixel = 24;
52 1 break;
53 default:
54 return AVERROR(EINVAL);
55 }
56
57 2 length = ALIAS_HEADER_SIZE + 4 * width * height; // max possible
58
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if ((ret = ff_alloc_packet(avctx, pkt, length)) < 0)
59 return ret;
60
61 2 buf = pkt->data;
62
63 /* Encode header. */
64 2 bytestream_put_be16(&buf, width);
65 2 bytestream_put_be16(&buf, height);
66 2 bytestream_put_be32(&buf, 0); /* X, Y offset */
67 2 bytestream_put_be16(&buf, bits_pixel);
68
69
2/2
✓ Branch 0 taken 160 times.
✓ Branch 1 taken 2 times.
162 for (int j = 0, bytes_pixel = bits_pixel >> 3; j < height; j++) {
70 160 const uint8_t *in_buf = frame->data[0] + frame->linesize[0] * j;
71 160 const uint8_t *const line_end = in_buf + bytes_pixel * width;
72
2/2
✓ Branch 0 taken 30199 times.
✓ Branch 1 taken 160 times.
30359 while (in_buf < line_end) {
73 30199 int count = 0;
74 int pixel;
75
76
2/2
✓ Branch 0 taken 14429 times.
✓ Branch 1 taken 15770 times.
30199 if (avctx->pix_fmt == AV_PIX_FMT_GRAY8) {
77 14429 pixel = *in_buf;
78
5/6
✓ Branch 0 taken 30548 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 30467 times.
✓ Branch 3 taken 81 times.
✓ Branch 4 taken 16119 times.
✓ Branch 5 taken 14348 times.
30548 while (count < 255 && in_buf < line_end && pixel == *in_buf) {
79 16119 count++;
80 16119 in_buf++;
81 }
82 14429 bytestream_put_byte(&buf, count);
83 14429 bytestream_put_byte(&buf, pixel);
84 } else { /* AV_PIX_FMT_BGR24 */
85 15770 pixel = AV_RB24(in_buf);
86
3/4
✓ Branch 0 taken 31649 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 31570 times.
✓ Branch 3 taken 79 times.
31649 while (count < 255 && in_buf < line_end &&
87
2/2
✓ Branch 0 taken 15879 times.
✓ Branch 1 taken 15691 times.
31570 pixel == AV_RB24(in_buf)) {
88 15879 count++;
89 15879 in_buf += 3;
90 }
91 15770 bytestream_put_byte(&buf, count);
92 15770 bytestream_put_be24(&buf, pixel);
93 }
94 }
95 }
96
97 /* Total length */
98 2 av_shrink_packet(pkt, buf - pkt->data);
99 2 *got_packet = 1;
100
101 2 return 0;
102 }
103
104 const FFCodec ff_alias_pix_encoder = {
105 .p.name = "alias_pix",
106 CODEC_LONG_NAME("Alias/Wavefront PIX image"),
107 .p.type = AVMEDIA_TYPE_VIDEO,
108 .p.id = AV_CODEC_ID_ALIAS_PIX,
109 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE,
110 FF_CODEC_ENCODE_CB(encode_frame),
111 .p.pix_fmts = (const enum AVPixelFormat[]) {
112 AV_PIX_FMT_BGR24, AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE
113 },
114 };
115