FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/ilbc.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 3 43 7.0%
Functions: 1 4 25.0%
Branches: 1 20 5.0%

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 7186 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 7186 times.
7186 if (!memcmp(p->buf, mode20_header, 6))
53 return AVPROBE_SCORE_MAX;
54 else
55 7186 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 if (avio_read(pb, header, 9) != 9)
65 return AVERROR_INVALIDDATA;
66
67 st = avformat_new_stream(s, NULL);
68 if (!st)
69 return AVERROR(ENOMEM);
70 st->codecpar->codec_id = AV_CODEC_ID_ILBC;
71 st->codecpar->sample_rate = 8000;
72 st->codecpar->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO;
73 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
74 st->start_time = 0;
75 avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
76 if (!memcmp(header, mode20_header, sizeof(mode20_header) - 1)) {
77 st->codecpar->block_align = 38;
78 st->codecpar->bit_rate = 15200;
79 } else if (!memcmp(header, mode30_header, sizeof(mode30_header) - 1)) {
80 st->codecpar->block_align = 50;
81 st->codecpar->bit_rate = 13333;
82 } else {
83 av_log(s, AV_LOG_ERROR, "Unrecognized iLBC file header\n");
84 return AVERROR_INVALIDDATA;
85 }
86
87 return 0;
88 }
89
90 static int ilbc_read_packet(AVFormatContext *s,
91 AVPacket *pkt)
92 {
93 AVCodecParameters *par = s->streams[0]->codecpar;
94 int ret;
95
96 if ((ret = av_get_packet(s->pb, pkt, par->block_align)) != par->block_align)
97 return ret < 0 ? ret : AVERROR_INVALIDDATA;
98
99 pkt->stream_index = 0;
100 pkt->duration = par->block_align == 38 ? 160 : 240;
101
102 return 0;
103 }
104
105 const FFInputFormat ff_ilbc_demuxer = {
106 .p.name = "ilbc",
107 .p.long_name = NULL_IF_CONFIG_SMALL("iLBC storage"),
108 .p.flags = AVFMT_GENERIC_INDEX,
109 .read_probe = ilbc_probe,
110 .read_header = ilbc_read_header,
111 .read_packet = ilbc_read_packet,
112 };
113
114 #if CONFIG_ILBC_MUXER
115 const FFOutputFormat ff_ilbc_muxer = {
116 .p.name = "ilbc",
117 .p.long_name = NULL_IF_CONFIG_SMALL("iLBC storage"),
118 .p.mime_type = "audio/iLBC",
119 .p.extensions = "lbc",
120 .p.video_codec = AV_CODEC_ID_NONE,
121 .p.audio_codec = AV_CODEC_ID_ILBC,
122 .p.subtitle_codec = AV_CODEC_ID_NONE,
123 .p.flags = AVFMT_NOTIMESTAMPS,
124 .flags_internal = FF_OFMT_FLAG_MAX_ONE_OF_EACH |
125 FF_OFMT_FLAG_ONLY_DEFAULT_CODECS,
126 .write_header = ilbc_write_header,
127 .write_packet = ff_raw_write_packet,
128 };
129 #endif
130