FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/ilbc.c
Date: 2024-03-28 14:59:00
Exec Total Coverage
Lines: 3 42 7.1%
Functions: 1 4 25.0%
Branches: 1 18 5.6%

Line Branch Exec Source
1 /*
2 * iLBC storage file format
3 * Copyright (c) 2012 Martin Storsjo
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 "avformat.h"
25 #include "demux.h"
26 #include "internal.h"
27 #include "mux.h"
28 #include "rawenc.h"
29
30 static const char mode20_header[] = "#!iLBC20\n";
31 static const char mode30_header[] = "#!iLBC30\n";
32
33 static int ilbc_write_header(AVFormatContext *s)
34 {
35 AVIOContext *pb = s->pb;
36 AVCodecParameters *par = s->streams[0]->codecpar;
37
38 if (par->block_align == 50) {
39 avio_write(pb, mode30_header, sizeof(mode30_header) - 1);
40 } else if (par->block_align == 38) {
41 avio_write(pb, mode20_header, sizeof(mode20_header) - 1);
42 } else {
43 av_log(s, AV_LOG_ERROR, "Unsupported mode\n");
44 return AVERROR(EINVAL);
45 }
46 return 0;
47 }
48
49 7124 static int ilbc_probe(const AVProbeData *p)
50 {
51 // Only check for "#!iLBC" which matches both formats
52
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7124 times.
7124 if (!memcmp(p->buf, mode20_header, 6))
53 return AVPROBE_SCORE_MAX;
54 else
55 7124 return 0;
56 }
57
58 static int ilbc_read_header(AVFormatContext *s)
59 {
60 AVIOContext *pb = s->pb;
61 AVStream *st;
62 uint8_t header[9];
63
64 avio_read(pb, header, 9);
65
66 st = avformat_new_stream(s, NULL);
67 if (!st)
68 return AVERROR(ENOMEM);
69 st->codecpar->codec_id = AV_CODEC_ID_ILBC;
70 st->codecpar->sample_rate = 8000;
71 st->codecpar->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO;
72 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
73 st->start_time = 0;
74 avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
75 if (!memcmp(header, mode20_header, sizeof(mode20_header) - 1)) {
76 st->codecpar->block_align = 38;
77 st->codecpar->bit_rate = 15200;
78 } else if (!memcmp(header, mode30_header, sizeof(mode30_header) - 1)) {
79 st->codecpar->block_align = 50;
80 st->codecpar->bit_rate = 13333;
81 } else {
82 av_log(s, AV_LOG_ERROR, "Unrecognized iLBC file header\n");
83 return AVERROR_INVALIDDATA;
84 }
85
86 return 0;
87 }
88
89 static int ilbc_read_packet(AVFormatContext *s,
90 AVPacket *pkt)
91 {
92 AVCodecParameters *par = s->streams[0]->codecpar;
93 int ret;
94
95 if ((ret = av_get_packet(s->pb, pkt, par->block_align)) != par->block_align)
96 return ret < 0 ? ret : AVERROR_INVALIDDATA;
97
98 pkt->stream_index = 0;
99 pkt->duration = par->block_align == 38 ? 160 : 240;
100
101 return 0;
102 }
103
104 const FFInputFormat ff_ilbc_demuxer = {
105 .p.name = "ilbc",
106 .p.long_name = NULL_IF_CONFIG_SMALL("iLBC storage"),
107 .p.flags = AVFMT_GENERIC_INDEX,
108 .read_probe = ilbc_probe,
109 .read_header = ilbc_read_header,
110 .read_packet = ilbc_read_packet,
111 };
112
113 #if CONFIG_ILBC_MUXER
114 const FFOutputFormat ff_ilbc_muxer = {
115 .p.name = "ilbc",
116 .p.long_name = NULL_IF_CONFIG_SMALL("iLBC storage"),
117 .p.mime_type = "audio/iLBC",
118 .p.extensions = "lbc",
119 .p.video_codec = AV_CODEC_ID_NONE,
120 .p.audio_codec = AV_CODEC_ID_ILBC,
121 .p.subtitle_codec = AV_CODEC_ID_NONE,
122 .p.flags = AVFMT_NOTIMESTAMPS,
123 .flags_internal = FF_OFMT_FLAG_MAX_ONE_OF_EACH |
124 FF_OFMT_FLAG_ONLY_DEFAULT_CODECS,
125 .write_header = ilbc_write_header,
126 .write_packet = ff_raw_write_packet,
127 };
128 #endif
129