FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/bit.c
Date: 2024-04-26 14:42:52
Exec Total Coverage
Lines: 5 70 7.1%
Functions: 1 5 20.0%
Branches: 2 44 4.5%

Line Branch Exec Source
1 /*
2 * G.729 bit format muxer and demuxer
3 * Copyright (c) 2007-2008 Vladimir Voroshilov
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 "libavcodec/get_bits.h"
29 #include "libavcodec/put_bits.h"
30
31 #define MAX_FRAME_SIZE 10
32
33 #define SYNC_WORD 0x6b21
34 #define BIT_0 0x7f
35 #define BIT_1 0x81
36
37 #if CONFIG_BIT_DEMUXER
38 7128 static int probe(const AVProbeData *p)
39 {
40 7128 int i = 0, j, valid = 0;
41
42
1/2
✓ Branch 0 taken 7128 times.
✗ Branch 1 not taken.
7128 while (2 * i + 3 < p->buf_size){
43
1/2
✓ Branch 0 taken 7128 times.
✗ Branch 1 not taken.
7128 if (AV_RL16(&p->buf[2 * i++]) != SYNC_WORD)
44 7128 return 0;
45 j = AV_RL16(&p->buf[2 * i++]);
46 if (j != 0 && j != 0x10 && j != 0x40 && j != 0x50 && j != 0x76)
47 return 0;
48 if (j)
49 valid++;
50 i += j;
51 }
52 if (valid > 10)
53 return AVPROBE_SCORE_MAX;
54 if (valid > 2)
55 return AVPROBE_SCORE_EXTENSION - 1;
56 return 0;
57 }
58
59 static int read_header(AVFormatContext *s)
60 {
61 AVStream* st;
62
63 st=avformat_new_stream(s, NULL);
64 if (!st)
65 return AVERROR(ENOMEM);
66
67 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
68 st->codecpar->codec_id=AV_CODEC_ID_G729;
69 st->codecpar->sample_rate=8000;
70 st->codecpar->block_align = 16;
71 st->codecpar->ch_layout.nb_channels = 1;
72
73 avpriv_set_pts_info(st, 64, 1, 100);
74 return 0;
75 }
76
77 static int read_packet(AVFormatContext *s,
78 AVPacket *pkt)
79 {
80 AVIOContext *pb = s->pb;
81 PutBitContext pbo;
82 uint16_t buf[8 * MAX_FRAME_SIZE + 2];
83 int packet_size;
84 uint16_t* src=buf;
85 int i, j, ret;
86 int64_t pos= avio_tell(pb);
87
88 if(avio_feof(pb))
89 return AVERROR_EOF;
90
91 avio_rl16(pb); // sync word
92 packet_size = avio_rl16(pb) / 8;
93 if(packet_size > MAX_FRAME_SIZE)
94 return AVERROR_INVALIDDATA;
95
96 ret = avio_read(pb, (uint8_t*)buf, (8 * packet_size) * sizeof(uint16_t));
97 if(ret<0)
98 return ret;
99 if(ret != 8 * packet_size * sizeof(uint16_t))
100 return AVERROR(EIO);
101
102 if ((ret = av_new_packet(pkt, packet_size)) < 0)
103 return ret;
104
105 init_put_bits(&pbo, pkt->data, packet_size);
106 for(j=0; j < packet_size; j++)
107 for(i=0; i<8;i++)
108 put_bits(&pbo,1, AV_RL16(src++) == BIT_1 ? 1 : 0);
109
110 flush_put_bits(&pbo);
111
112 pkt->duration=1;
113 pkt->pos = pos;
114 return 0;
115 }
116
117 const FFInputFormat ff_bit_demuxer = {
118 .p.name = "bit",
119 .p.long_name = NULL_IF_CONFIG_SMALL("G.729 BIT file format"),
120 .p.extensions = "bit",
121 .read_probe = probe,
122 .read_header = read_header,
123 .read_packet = read_packet,
124 };
125 #endif
126
127 #if CONFIG_BIT_MUXER
128 static av_cold int init(AVFormatContext *s)
129 {
130 AVCodecParameters *par = s->streams[0]->codecpar;
131
132 if (par->ch_layout.nb_channels != 1) {
133 av_log(s, AV_LOG_ERROR,
134 "only codec g729 with 1 channel is supported by this format\n");
135 return AVERROR(EINVAL);
136 }
137
138 par->bits_per_coded_sample = 16;
139 par->block_align = (par->bits_per_coded_sample * par->ch_layout.nb_channels) >> 3;
140
141 return 0;
142 }
143
144 static int write_packet(AVFormatContext *s, AVPacket *pkt)
145 {
146 AVIOContext *pb = s->pb;
147 GetBitContext gb;
148 int i;
149
150 if (pkt->size != 10)
151 return AVERROR(EINVAL);
152
153 avio_wl16(pb, SYNC_WORD);
154 avio_wl16(pb, 8 * pkt->size);
155
156 init_get_bits(&gb, pkt->data, 8 * pkt->size);
157 for (i = 0; i < 8 * pkt->size; i++)
158 avio_wl16(pb, get_bits1(&gb) ? BIT_1 : BIT_0);
159
160 return 0;
161 }
162
163 const FFOutputFormat ff_bit_muxer = {
164 .p.name = "bit",
165 .p.long_name = NULL_IF_CONFIG_SMALL("G.729 BIT file format"),
166 .p.mime_type = "audio/bit",
167 .p.extensions = "bit",
168 .p.audio_codec = AV_CODEC_ID_G729,
169 .p.video_codec = AV_CODEC_ID_NONE,
170 .p.subtitle_codec = AV_CODEC_ID_NONE,
171 .flags_internal = FF_OFMT_FLAG_MAX_ONE_OF_EACH |
172 FF_OFMT_FLAG_ONLY_DEFAULT_CODECS,
173 .init = init,
174 .write_packet = write_packet,
175 };
176 #endif
177