FFmpeg coverage


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