FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/roqaudioenc.c
Date: 2021-09-22 05:58:22
Exec Total Coverage
Lines: 72 82 87.8%
Branches: 34 46 73.9%

Line Branch Exec Source
1 /*
2 * RoQ audio encoder
3 *
4 * Copyright (c) 2005 Eric Lasota
5 * Based on RoQ specs (c)2001 Tim Ferguson
6 *
7 * This file is part of FFmpeg.
8 *
9 * FFmpeg is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * FFmpeg is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with FFmpeg; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24 #include "avcodec.h"
25 #include "bytestream.h"
26 #include "encode.h"
27 #include "internal.h"
28 #include "mathops.h"
29
30 #define ROQ_FRAME_SIZE 735
31 #define ROQ_HEADER_SIZE 8
32
33 #define MAX_DPCM (127*127)
34
35
36 typedef struct ROQDPCMContext {
37 short lastSample[2];
38 int input_frames;
39 int buffered_samples;
40 int16_t *frame_buffer;
41 int64_t first_pts;
42 } ROQDPCMContext;
43
44
45 1 static av_cold int roq_dpcm_encode_close(AVCodecContext *avctx)
46 {
47 1 ROQDPCMContext *context = avctx->priv_data;
48
49 1 av_freep(&context->frame_buffer);
50
51 1 return 0;
52 }
53
54 1 static av_cold int roq_dpcm_encode_init(AVCodecContext *avctx)
55 {
56 1 ROQDPCMContext *context = avctx->priv_data;
57
58
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (avctx->channels > 2) {
59 av_log(avctx, AV_LOG_ERROR, "Audio must be mono or stereo\n");
60 return AVERROR(EINVAL);
61 }
62
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (avctx->sample_rate != 22050) {
63 av_log(avctx, AV_LOG_ERROR, "Audio must be 22050 Hz\n");
64 return AVERROR(EINVAL);
65 }
66
67 1 avctx->frame_size = ROQ_FRAME_SIZE;
68 1 avctx->bit_rate = (ROQ_HEADER_SIZE + ROQ_FRAME_SIZE * avctx->channels) *
69 1 (22050 / ROQ_FRAME_SIZE) * 8;
70
71 1 context->frame_buffer = av_malloc(8 * ROQ_FRAME_SIZE * avctx->channels *
72 sizeof(*context->frame_buffer));
73
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!context->frame_buffer)
74 return AVERROR(ENOMEM);
75
76 1 context->lastSample[0] = context->lastSample[1] = 0;
77
78 1 return 0;
79 }
80
81 264600 static unsigned char dpcm_predict(short *previous, short current)
82 {
83 int diff;
84 int negative;
85 int result;
86 int predicted;
87
88 264600 diff = current - *previous;
89
90 264600 negative = diff<0;
91 264600 diff = FFABS(diff);
92
93
2/2
✓ Branch 0 taken 10896 times.
✓ Branch 1 taken 253704 times.
264600 if (diff >= MAX_DPCM)
94 10896 result = 127;
95 else {
96 253704 result = ff_sqrt(diff);
97 253704 result += diff > result*result+result;
98 }
99
100 /* See if this overflows */
101 264600 retry:
102 264600 diff = result*result;
103
2/2
✓ Branch 0 taken 131603 times.
✓ Branch 1 taken 132997 times.
264600 if (negative)
104 131603 diff = -diff;
105 264600 predicted = *previous + diff;
106
107 /* If it overflows, back off a step */
108
2/4
✓ Branch 0 taken 264600 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 264600 times.
264600 if (predicted > 32767 || predicted < -32768) {
109 result--;
110 goto retry;
111 }
112
113 /* Add the sign bit */
114 264600 result |= negative << 7; //if (negative) result |= 128;
115
116 264600 *previous = predicted;
117
118 264600 return result;
119 }
120
121 181 static int roq_dpcm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
122 const AVFrame *frame, int *got_packet_ptr)
123 {
124 int i, stereo, data_size, ret;
125
2/2
✓ Branch 0 taken 180 times.
✓ Branch 1 taken 1 times.
181 const int16_t *in = frame ? (const int16_t *)frame->data[0] : NULL;
126 uint8_t *out;
127 181 ROQDPCMContext *context = avctx->priv_data;
128
129 181 stereo = (avctx->channels == 2);
130
131
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 180 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
181 if (!in && context->input_frames >= 8)
132 1 return 0;
133
134
3/4
✓ Branch 0 taken 180 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✓ Branch 3 taken 172 times.
180 if (in && context->input_frames < 8) {
135 8 memcpy(&context->frame_buffer[context->buffered_samples * avctx->channels],
136 8 in, avctx->frame_size * avctx->channels * sizeof(*in));
137 8 context->buffered_samples += avctx->frame_size;
138
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 7 times.
8 if (context->input_frames == 0)
139 1 context->first_pts = frame->pts;
140
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 1 times.
8 if (context->input_frames < 7) {
141 7 context->input_frames++;
142 7 return 0;
143 }
144 }
145
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 172 times.
173 if (context->input_frames < 8)
146 1 in = context->frame_buffer;
147
148
1/2
✓ Branch 0 taken 173 times.
✗ Branch 1 not taken.
173 if (stereo) {
149 173 context->lastSample[0] &= 0xFF00;
150 173 context->lastSample[1] &= 0xFF00;
151 }
152
153
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 172 times.
173 if (context->input_frames == 7)
154 1 data_size = avctx->channels * context->buffered_samples;
155 else
156 172 data_size = avctx->channels * avctx->frame_size;
157
158 173 ret = ff_get_encode_buffer(avctx, avpkt, ROQ_HEADER_SIZE + data_size, 0);
159
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 173 times.
173 if (ret < 0)
160 return ret;
161 173 out = avpkt->data;
162
163
1/2
✓ Branch 0 taken 173 times.
✗ Branch 1 not taken.
173 bytestream_put_byte(&out, stereo ? 0x21 : 0x20);
164 173 bytestream_put_byte(&out, 0x10);
165 173 bytestream_put_le32(&out, data_size);
166
167
1/2
✓ Branch 0 taken 173 times.
✗ Branch 1 not taken.
173 if (stereo) {
168 173 bytestream_put_byte(&out, (context->lastSample[1])>>8);
169 173 bytestream_put_byte(&out, (context->lastSample[0])>>8);
170 } else
171 bytestream_put_le16(&out, context->lastSample[0]);
172
173 /* Write the actual samples */
174
2/2
✓ Branch 0 taken 264600 times.
✓ Branch 1 taken 173 times.
264773 for (i = 0; i < data_size; i++)
175 264600 *out++ = dpcm_predict(&context->lastSample[i & 1], *in++);
176
177
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 172 times.
173 avpkt->pts = context->input_frames <= 7 ? context->first_pts : frame->pts;
178 173 avpkt->duration = data_size / avctx->channels;
179
180 173 context->input_frames++;
181
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 173 times.
173 if (!in)
182 context->input_frames = FFMAX(context->input_frames, 8);
183
184 173 *got_packet_ptr = 1;
185 173 return 0;
186 }
187
188 const AVCodec ff_roq_dpcm_encoder = {
189 .name = "roq_dpcm",
190 .long_name = NULL_IF_CONFIG_SMALL("id RoQ DPCM"),
191 .type = AVMEDIA_TYPE_AUDIO,
192 .id = AV_CODEC_ID_ROQ_DPCM,
193 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY,
194 .priv_data_size = sizeof(ROQDPCMContext),
195 .init = roq_dpcm_encode_init,
196 .encode2 = roq_dpcm_encode_frame,
197 .close = roq_dpcm_encode_close,
198 .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
199 AV_SAMPLE_FMT_NONE },
200 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
201 };
202