FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/tests/api/api-skip-samples-test.c
Date: 2026-10-24 19:27:14
Exec Total Coverage
Lines: 43 53 81.1%
Functions: 2 2 100.0%
Branches: 14 24 58.3%

Line Branch Exec Source
1 /*
2 * This file is part of FFmpeg.
3 *
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 /**
20 * Decode one packet carrying AV_PKT_DATA_SKIP_SAMPLES and print how many
21 * samples are output, for skips at the start and padding at the end of the
22 * frame, alone, together and overlapping.
23 */
24
25 #include <stdio.h>
26 #include <string.h>
27
28 #include "libavcodec/avcodec.h"
29 #include "libavutil/channel_layout.h"
30 #include "libavutil/intreadwrite.h"
31 #include "libavutil/macros.h"
32
33 #define FRAME_SAMPLES 1000
34
35 7 static int decoded_samples(uint32_t skip, uint32_t discard)
36 {
37 7 const AVCodec *codec = avcodec_find_decoder(AV_CODEC_ID_PCM_S16LE);
38 7 AVCodecContext *ctx = NULL;
39 7 AVPacket *pkt = NULL;
40 7 AVFrame *frame = NULL;
41 uint8_t *side;
42 7 int nb_samples = 0;
43 int ret;
44
45 7 ctx = avcodec_alloc_context3(codec);
46 7 pkt = av_packet_alloc();
47 7 frame = av_frame_alloc();
48
3/6
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 7 times.
7 if (!ctx || !pkt || !frame) {
49 ✗ ret = AVERROR(ENOMEM);
50 ✗ goto end;
51 }
52
53 7 ctx->sample_rate = 48000;
54 7 ctx->pkt_timebase = (AVRational){ 1, 48000 };
55 7 av_channel_layout_default(&ctx->ch_layout, 1);
56 7 ret = avcodec_open2(ctx, codec, NULL);
57
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 if (ret < 0)
58 ✗ goto end;
59
60 7 ret = av_new_packet(pkt, FRAME_SAMPLES * 2);
61
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 if (ret < 0)
62 ✗ goto end;
63 7 memset(pkt->data, 0, pkt->size);
64 7 pkt->pts = 0;
65 7 side = av_packet_new_side_data(pkt, AV_PKT_DATA_SKIP_SAMPLES, 10);
66
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 if (!side) {
67 ✗ ret = AVERROR(ENOMEM);
68 ✗ goto end;
69 }
70 7 AV_WL32(side, skip);
71 7 AV_WL32(side + 4, discard);
72
73 7 ret = avcodec_send_packet(ctx, pkt);
74
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 if (ret < 0)
75 ✗ goto end;
76 7 ret = avcodec_send_packet(ctx, NULL);
77
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 if (ret < 0)
78 ✗ goto end;
79
2/2
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 7 times.
12 while ((ret = avcodec_receive_frame(ctx, frame)) >= 0) {
80 5 nb_samples += frame->nb_samples;
81 5 av_frame_unref(frame);
82 }
83
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 ret = ret == AVERROR_EOF ? nb_samples : ret;
84
85 7 end:
86 7 av_frame_free(&frame);
87 7 av_packet_free(&pkt);
88 7 avcodec_free_context(&ctx);
89 7 return ret;
90 }
91
92 1 int main(void)
93 {
94 static const uint32_t cases[][2] = {
95 { 0, 0 },
96 { 300, 0 },
97 { 0, 400 },
98 { 300, 400 },
99 { 300, 700 },
100 { 300, 800 },
101 { 300, 1100 },
102 };
103
104
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 1 times.
8 for (int i = 0; i < FF_ARRAY_ELEMS(cases); i++) {
105 7 int ret = decoded_samples(cases[i][0], cases[i][1]);
106
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 if (ret < 0) {
107 ✗ fprintf(stderr, "decoding failed: %s\n", av_err2str(ret));
108 ✗ return 1;
109 }
110 7 printf("frame %d skip %4"PRIu32" discard %4"PRIu32": %d samples\n",
111 7 FRAME_SAMPLES, cases[i][0], cases[i][1], ret);
112 }
113 1 return 0;
114 }
115