FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/bit.c
Date: 2023-09-24 13:02:57
Exec Total Coverage
Lines: 5 70 7.1%
Functions: 1 5 20.0%
Branches: 2 46 4.3%

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