36 #define FREEZE_INTERVAL 128
40 #define MAX_FRAME_SIZE 32768
45 #define MAX_TRELLIS 16
51 for (
i = 0;
i < 2;
i++) {
63 c->band[0].scale_factor = 8;
64 c->band[1].scale_factor = 2;
65 c->prev_samples_pos = 22;
80 "allowed. Using %d instead of %d\n", new_frame_size,
96 "allowed. Using %d instead of %d\n", new_trellis,
101 int frontier = 1 << avctx->
trellis;
104 for (
int i = 0;
i < 2;
i++) {
106 c->node_buf[
i] =
av_calloc(frontier, 2 *
sizeof(**
c->node_buf));
107 c->nodep_buf[
i] =
av_calloc(frontier, 2 *
sizeof(**
c->nodep_buf));
108 if (!
c->paths[
i] || !
c->node_buf[
i] || !
c->nodep_buf[
i])
120 35, 72, 110, 150, 190, 233, 276, 323,
121 370, 422, 473, 530, 587, 650, 714, 786,
122 858, 940, 1023, 1121, 1219, 1339, 1458, 1612,
123 1765, 1980, 2195, 2557, 2919
127 int *xlow,
int *xhigh)
130 c->prev_samples[
c->prev_samples_pos++] = samples[0];
131 c->prev_samples[
c->prev_samples_pos++] = samples[1];
132 c->dsp.apply_qmf(
c->prev_samples +
c->prev_samples_pos - 24, xout);
133 *xlow = xout[0] + xout[1] >> 14;
134 *xhigh = xout[0] - xout[1] >> 14;
136 memmove(
c->prev_samples,
137 c->prev_samples +
c->prev_samples_pos - 22,
138 22 *
sizeof(
c->prev_samples[0]));
139 c->prev_samples_pos = 22;
146 int pred = 141 *
state->scale_factor >> 8;
157 limit = limit + 1 << 10;
162 return (
diff < 0 ? (
i < 2 ? 63 : 33) : 61) -
i;
167 const int16_t *samples)
170 int frontier = 1 << trellis;
173 int pathn[2] = {0, 0}, froze = -1;
176 for (
i = 0;
i < 2;
i++) {
177 nodes[
i] =
c->nodep_buf[
i];
178 nodes_next[
i] =
c->nodep_buf[
i] + frontier;
179 memset(
c->nodep_buf[
i], 0, 2 * frontier *
sizeof(*
c->nodep_buf[
i]));
180 nodes[
i][0] =
c->node_buf[
i] + frontier;
181 nodes[
i][0]->
ssd = 0;
182 nodes[
i][0]->
path = 0;
183 nodes[
i][0]->state =
c->band[
i];
186 for (
i = 0; i < nb_samples >> 1;
i++) {
189 int heap_pos[2] = {0, 0};
191 for (j = 0; j < 2; j++) {
192 next[j] =
c->node_buf[j] + frontier*(
i & 1);
193 memset(nodes_next[j], 0, frontier *
sizeof(**nodes_next));
198 for (j = 0; j < frontier && nodes[0][j]; j++) {
204 int range = j < frontier/2 ? 4 : 0;
207 int ilow =
encode_low(&cur_node->state, xlow);
209 for (k = ilow - range; k <= ilow + range && k <= 63; k += 4) {
210 int decoded, dec_diff,
pos;
219 + cur_node->state.s_predictor, 14);
220 dec_diff = xlow - decoded;
222 #define STORE_NODE(index, UPDATE, VALUE)\
223 ssd = cur_node->ssd + dec_diff*dec_diff;\
226 if (ssd < cur_node->ssd)\
228 if (heap_pos[index] < frontier) {\
229 pos = heap_pos[index]++;\
230 av_assert2(pathn[index] < FREEZE_INTERVAL * frontier);\
231 node = nodes_next[index][pos] = next[index]++;\
232 node->path = pathn[index]++;\
236 pos = (frontier>>1) + (heap_pos[index] & ((frontier>>1) - 1));\
237 if (ssd >= nodes_next[index][pos]->ssd)\
240 node = nodes_next[index][pos];\
243 node->state = cur_node->state;\
245 c->paths[index][node->path].value = VALUE;\
246 c->paths[index][node->path].prev = cur_node->path;\
250 int parent = (pos - 1) >> 1;\
251 if (nodes_next[index][parent]->ssd <= ssd)\
253 FFSWAP(struct TrellisNode*, nodes_next[index][parent],\
254 nodes_next[index][pos]);\
261 for (j = 0; j < frontier && nodes[1][j]; j++) {
269 for (ihigh = 0; ihigh < 4; ihigh++) {
270 int dhigh, decoded, dec_diff,
pos;
274 dhigh = cur_node->state.scale_factor *
276 decoded =
av_clip_intp2(dhigh + cur_node->state.s_predictor, 14);
277 dec_diff = xhigh - decoded;
283 for (j = 0; j < 2; j++) {
286 if (nodes[j][0]->
ssd > (1 << 16)) {
287 for (k = 1; k < frontier && nodes[j][k]; k++)
288 nodes[j][k]->
ssd -= nodes[j][0]->
ssd;
289 nodes[j][0]->ssd = 0;
294 p[0] = &
c->paths[0][nodes[0][0]->path];
295 p[1] = &
c->paths[1][nodes[1][0]->path];
296 for (j =
i; j > froze; j--) {
297 dst[j] = p[1]->value << 6 | p[0]->value;
298 p[0] = &
c->paths[0][p[0]->
prev];
299 p[1] = &
c->paths[1][p[1]->
prev];
302 pathn[0] = pathn[1] = 0;
303 memset(nodes[0] + 1, 0, (frontier - 1)*
sizeof(**nodes));
304 memset(nodes[1] + 1, 0, (frontier - 1)*
sizeof(**nodes));
308 p[0] = &
c->paths[0][nodes[0][0]->
path];
309 p[1] = &
c->paths[1][nodes[1][0]->
path];
310 for (j =
i; j > froze; j--) {
311 dst[j] = p[1]->value << 6 | p[0]->value;
312 p[0] = &
c->paths[0][p[0]->
prev];
313 p[1] = &
c->paths[1][p[1]->
prev];
315 c->band[0] = nodes[0][0]->state;
316 c->band[1] = nodes[1][0]->state;
320 const int16_t *samples)
322 int xlow, xhigh, ilow, ihigh;
329 *dst = ihigh << 6 | ilow;
334 const int16_t *samples)
337 for (
i = 0;
i < nb_samples;
i += 2)
345 const int16_t *samples = (
const int16_t *)
frame->
data[0];
360 if (nb_samples < frame->nb_samples) {
361 int16_t last_samples[2] = { samples[nb_samples], samples[nb_samples] };
static enum AVSampleFormat sample_fmts[]
simple assert() macros that are a bit more flexible than ISO C assert().
Libavcodec external API header.
static av_cold int init(AVCodecContext *avctx)
common internal and external API header
#define FFSWAP(type, a, b)
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
Check AVPacket size and/or allocate data.
#define PREV_SAMPLES_BUF_SIZE
static int encode_low(const struct G722Band *state, int xlow)
static void filter_samples(G722Context *c, const int16_t *samples, int *xlow, int *xhigh)
static av_cold int g722_encode_init(AVCodecContext *avctx)
static int g722_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
static void g722_encode_no_trellis(G722Context *c, uint8_t *dst, int nb_samples, const int16_t *samples)
static int encode_high(const struct G722Band *state, int xhigh)
#define STORE_NODE(index, UPDATE, VALUE)
static av_always_inline void encode_byte(G722Context *c, uint8_t *dst, const int16_t *samples)
static av_cold int g722_encode_close(AVCodecContext *avctx)
static const int16_t low_quant[33]
static void g722_encode_trellis(G722Context *c, int trellis, uint8_t *dst, int nb_samples, const int16_t *samples)
AVCodec ff_adpcm_g722_encoder
#define AV_CH_LAYOUT_MONO
#define AV_CODEC_CAP_SMALL_LAST_FRAME
Codec can be fed a final frame with a smaller size.
#define AV_LOG_WARNING
Something somehow does not look correct.
void * av_calloc(size_t nmemb, size_t size)
Non-inlined equivalent of av_mallocz_array().
AVSampleFormat
Audio sample formats.
@ AV_SAMPLE_FMT_S16
signed 16 bits
#define AV_NOPTS_VALUE
Undefined timestamp value.
const int16_t ff_g722_low_inv_quant6[64]
void ff_g722_update_high_predictor(struct G722Band *band, const int dhigh, const int ihigh)
const int16_t ff_g722_high_inv_quant[4]
void ff_g722_update_low_predictor(struct G722Band *band, const int ilow)
av_cold void ff_g722dsp_init(G722DSPContext *c)
static av_always_inline int64_t ff_samples_to_time_base(AVCodecContext *avctx, int64_t samples)
Rescale from sample rate to AVCodecContext.time_base.
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
static const float pred[4]
main external API structure.
int trellis
trellis RD quantization
int initial_padding
Audio only.
int frame_size
Number of samples per channel in an audio frame.
const char * name
Name of the codec implementation.
This structure describes decoded (raw) audio or video data.
int nb_samples
number of audio samples (per channel) described by this frame
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
This structure stores compressed data.
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
static av_always_inline int diff(const uint32_t a, const uint32_t b)