| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * DFPWM encoder | ||
| 3 | * Copyright (c) 2022 Jack Bruienne | ||
| 4 | * Copyright (c) 2012, 2016 Ben "GreaseMonkey" Russell | ||
| 5 | * | ||
| 6 | * This file is part of FFmpeg. | ||
| 7 | * | ||
| 8 | * FFmpeg is free software; you can redistribute it and/or | ||
| 9 | * modify it under the terms of the GNU Lesser General Public | ||
| 10 | * License as published by the Free Software Foundation; either | ||
| 11 | * version 2.1 of the License, or (at your option) any later version. | ||
| 12 | * | ||
| 13 | * FFmpeg is distributed in the hope that it will be useful, | ||
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 16 | * Lesser General Public License for more details. | ||
| 17 | * | ||
| 18 | * You should have received a copy of the GNU Lesser General Public | ||
| 19 | * License along with FFmpeg; if not, write to the Free Software | ||
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 21 | */ | ||
| 22 | |||
| 23 | /** | ||
| 24 | * @file | ||
| 25 | * DFPWM1a encoder | ||
| 26 | */ | ||
| 27 | |||
| 28 | #include "avcodec.h" | ||
| 29 | #include "codec_id.h" | ||
| 30 | #include "codec_internal.h" | ||
| 31 | #include "encode.h" | ||
| 32 | #include "internal.h" | ||
| 33 | |||
| 34 | typedef struct { | ||
| 35 | int q, s, lt; | ||
| 36 | } DFPWMState; | ||
| 37 | |||
| 38 | // DFPWM codec from https://github.com/ChenThread/dfpwm/blob/master/1a/ | ||
| 39 | // Licensed in the public domain | ||
| 40 | |||
| 41 | // note, len denotes how many compressed bytes there are (uncompressed bytes / 8). | ||
| 42 | 11 | static void au_compress(DFPWMState *state, int len, uint8_t *outbuf, const uint8_t *inbuf) | |
| 43 | { | ||
| 44 | 11 | unsigned d = 0; | |
| 45 |
2/2✓ Branch 0 taken 5513 times.
✓ Branch 1 taken 11 times.
|
5524 | for (int i = 0; i < len; i++) { |
| 46 |
2/2✓ Branch 0 taken 44104 times.
✓ Branch 1 taken 5513 times.
|
49617 | for (int j = 0; j < 8; j++) { |
| 47 | int nq, st, ns; | ||
| 48 | // get sample | ||
| 49 | 44104 | int v = *(inbuf++) - 128; | |
| 50 | // set bit / target | ||
| 51 |
5/6✓ Branch 0 taken 22180 times.
✓ Branch 1 taken 21924 times.
✓ Branch 2 taken 4002 times.
✓ Branch 3 taken 18178 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 4002 times.
|
44104 | int t = (v > state->q || (v == state->q && v == 127) ? 127 : -128); |
| 52 | 44104 | d >>= 1; | |
| 53 |
2/2✓ Branch 0 taken 21924 times.
✓ Branch 1 taken 22180 times.
|
44104 | if(t > 0) |
| 54 | 21924 | d |= 0x80; | |
| 55 | |||
| 56 | // adjust charge | ||
| 57 | 44104 | nq = state->q + ((state->s * (t-state->q) + 512)>>10); | |
| 58 |
3/4✓ Branch 0 taken 1 times.
✓ Branch 1 taken 44103 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
44104 | if(nq == state->q && nq != t) |
| 59 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | nq += (t == 127 ? 1 : -1); |
| 60 | 44104 | state->q = nq; | |
| 61 | |||
| 62 | // adjust strength | ||
| 63 |
2/2✓ Branch 0 taken 22038 times.
✓ Branch 1 taken 22066 times.
|
44104 | st = (t != state->lt ? 0 : 1023); |
| 64 | 44104 | ns = state->s; | |
| 65 |
2/2✓ Branch 0 taken 44103 times.
✓ Branch 1 taken 1 times.
|
44104 | if(ns != st) |
| 66 |
2/2✓ Branch 0 taken 22066 times.
✓ Branch 1 taken 22037 times.
|
44103 | ns += (st != 0 ? 1 : -1); |
| 67 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 44103 times.
|
44104 | if(ns < 8) ns = 8; |
| 68 | 44104 | state->s = ns; | |
| 69 | |||
| 70 | 44104 | state->lt = t; | |
| 71 | } | ||
| 72 | |||
| 73 | // output bits | ||
| 74 | 5513 | *(outbuf++) = d; | |
| 75 | } | ||
| 76 | 11 | } | |
| 77 | |||
| 78 | 1 | static av_cold int dfpwm_enc_init(struct AVCodecContext *ctx) | |
| 79 | { | ||
| 80 | 1 | DFPWMState *state = ctx->priv_data; | |
| 81 | |||
| 82 | 1 | state->q = 0; | |
| 83 | 1 | state->s = 0; | |
| 84 | 1 | state->lt = -128; | |
| 85 | |||
| 86 | 1 | ctx->bits_per_coded_sample = 1; | |
| 87 | // Pad so that nb_samples * nb_channels is always a multiple of eight. | ||
| 88 | 1 | ctx->internal->pad_samples = (const uint8_t[]){ 1, 8, 4, 8, 2, 8, 4, 8 }[ctx->ch_layout.nb_channels & 7]; | |
| 89 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
1 | if (ctx->frame_size <= 0 || ctx->frame_size * ctx->ch_layout.nb_channels % 8U) |
| 90 | 1 | ctx->frame_size = 4096; | |
| 91 | |||
| 92 | 1 | return 0; | |
| 93 | } | ||
| 94 | |||
| 95 | 11 | static int dfpwm_enc_frame(struct AVCodecContext *ctx, struct AVPacket *packet, | |
| 96 | const struct AVFrame *frame, int *got_packet) | ||
| 97 | { | ||
| 98 | 11 | DFPWMState *state = ctx->priv_data; | |
| 99 | 11 | int size = frame->nb_samples * frame->ch_layout.nb_channels / 8U; | |
| 100 | 11 | int ret = ff_get_encode_buffer(ctx, packet, size, 0); | |
| 101 | |||
| 102 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
|
11 | if (ret) { |
| 103 | ✗ | *got_packet = 0; | |
| 104 | ✗ | return ret; | |
| 105 | } | ||
| 106 | |||
| 107 | 11 | au_compress(state, size, packet->data, frame->data[0]); | |
| 108 | |||
| 109 | 11 | *got_packet = 1; | |
| 110 | 11 | return 0; | |
| 111 | } | ||
| 112 | |||
| 113 | const FFCodec ff_dfpwm_encoder = { | ||
| 114 | .p.name = "dfpwm", | ||
| 115 | CODEC_LONG_NAME("DFPWM1a audio"), | ||
| 116 | .p.type = AVMEDIA_TYPE_AUDIO, | ||
| 117 | .p.id = AV_CODEC_ID_DFPWM, | ||
| 118 | .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE, | ||
| 119 | .priv_data_size = sizeof(DFPWMState), | ||
| 120 | .init = dfpwm_enc_init, | ||
| 121 | FF_CODEC_ENCODE_CB(dfpwm_enc_frame), | ||
| 122 | CODEC_SAMPLEFMTS(AV_SAMPLE_FMT_U8), | ||
| 123 | }; | ||
| 124 |