FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/tests/auenc.c
Date: 2026-10-29 07:45:57
Exec Total Coverage
Lines: 58 73 79.5%
Functions: 2 2 100.0%
Branches: 18 30 60.0%

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 License
6 * 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
12 * GNU Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public License
15 * 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 #include <inttypes.h>
20 #include <stdio.h>
21 #include <string.h>
22
23 #include "libavutil/mem.h"
24 #include "libavformat/avformat.h"
25 #include "libavformat/avio_internal.h"
26
27 #define AU_PAYLOAD 80
28
29 12 static int run_case(int channels, int bps, int64_t bit_rate)
30 {
31 12 AVFormatContext *out = NULL, *in = NULL;
32 12 AVIOContext *pb = NULL;
33 FFIOContext in_pb;
34 AVStream *st;
35 12 AVPacket *pkt = NULL;
36 12 uint8_t *buf = NULL;
37 int ret, size;
38
39 12 ret = avformat_alloc_output_context2(&out, NULL, "au", NULL);
40
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 if (ret < 0)
41 ✗ return 1;
42
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
12 if (avio_open_dyn_buf(&pb) < 0) {
43 ✗ ret = 1;
44 ✗ goto done;
45 }
46 12 out->pb = pb;
47
48 12 st = avformat_new_stream(out, NULL);
49
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 if (!st) {
50 ✗ ret = 1;
51 ✗ goto done;
52 }
53 12 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
54 12 st->codecpar->codec_id = AV_CODEC_ID_ADPCM_G726LE;
55 12 st->codecpar->sample_rate = 8000;
56 12 st->codecpar->bits_per_coded_sample = bps;
57 12 st->codecpar->bit_rate = bit_rate;
58 12 av_channel_layout_default(&st->codecpar->ch_layout, channels);
59
60 12 ret = avformat_write_header(out, NULL);
61
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 10 times.
12 if (ret < 0) {
62 2 printf("channels=%d bps=%d bit_rate=%"PRId64" rejected\n",
63 channels, bps, bit_rate);
64 2 ret = 0;
65 2 goto done;
66 }
67
68 10 pkt = av_packet_alloc();
69
2/4
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 10 times.
10 if (!pkt || (ret = av_new_packet(pkt, AU_PAYLOAD)) < 0) {
70 ✗ ret = 1;
71 ✗ goto done;
72 }
73 10 memset(pkt->data, 0, pkt->size);
74 10 pkt->stream_index = 0;
75 10 pkt->pts = pkt->dts = 0;
76
2/4
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 10 times.
20 if ((ret = av_write_frame(out, pkt)) < 0 ||
77 10 (ret = av_write_trailer(out)) < 0) {
78 ✗ ret = 1;
79 ✗ goto done;
80 }
81
82 10 size = avio_close_dyn_buf(pb, &buf);
83 10 pb = NULL;
84
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (size < 16) {
85 ✗ ret = 1;
86 ✗ goto done;
87 }
88
89 10 ffio_init_read_context(&in_pb, buf, size);
90 10 in = avformat_alloc_context();
91
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (!in) {
92 ✗ ret = 1;
93 ✗ goto done;
94 }
95 10 in->pb = &in_pb.pub;
96
1/2
✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
10 if ((ret = avformat_open_input(&in, NULL, av_find_input_format("au"), NULL)) < 0 ||
97
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 !in->nb_streams) {
98 ✗ ret = 1;
99 ✗ goto done;
100 }
101
102 10 st = in->streams[0];
103 10 printf("channels=%d bps=%d bit_rate=%"PRId64" tag=%02x %02x %02x %02x "
104 "read_bps=%d read_bit_rate=%"PRId64" duration=%"PRId64"\n",
105 10 channels, bps, bit_rate, buf[12], buf[13], buf[14], buf[15],
106 10 st->codecpar->bits_per_coded_sample, st->codecpar->bit_rate,
107 st->duration);
108 10 ret = 0;
109
110 12 done:
111 12 av_packet_free(&pkt);
112 12 avformat_close_input(&in);
113
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 10 times.
12 if (pb) {
114 2 uint8_t *tmp = NULL;
115 2 avio_close_dyn_buf(pb, &tmp);
116 2 av_free(tmp);
117 }
118 12 av_free(buf);
119 12 avformat_free_context(out);
120
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 return ret < 0 ? 1 : ret;
121 }
122
123 1 int main(void)
124 {
125 static const int cases[][3] = {
126 { 1, 2, 0 },
127 { 1, 3, 0 },
128 { 1, 4, 0 },
129 { 1, 5, 0 },
130 { 1, 0, 16000 },
131 { 1, 0, 40000 },
132 { 1, 1, 0 },
133 { 1, 0, 0 },
134 { 2, 2, 32000 },
135 { 2, 0, 32000 },
136 { 2, 4, 64000 },
137 { 2, 0, 64000 },
138 };
139 1 int i, ret = 0;
140
141
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 1 times.
13 for (i = 0; i < FF_ARRAY_ELEMS(cases); i++)
142 12 ret |= run_case(cases[i][0], cases[i][1], cases[i][2]);
143 1 return ret;
144 }
145