| @@ -0,0 +1,140 @@ |
| | 1 | /* |
| | 2 | * HCOM audio decoder |
| | 3 | * |
| | 4 | * This file is part of FFmpeg. |
| | 5 | * |
| | 6 | * FFmpeg is free software; you can redistribute it and/or |
| | 7 | * modify it under the terms of the GNU Lesser General Public |
| | 8 | * License as published by the Free Software Foundation; either |
| | 9 | * version 2.1 of the License, or (at your option) any later version. |
| | 10 | * |
| | 11 | * FFmpeg is distributed in the hope that it will be useful, |
| | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| | 14 | * Lesser General Public License for more details. |
| | 15 | * |
| | 16 | * You should have received a copy of the GNU Lesser General Public |
| | 17 | * License along with FFmpeg; if not, write to the Free Software |
| | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| | 19 | */ |
| | 20 | |
| | 21 | #include "libavutil/intreadwrite.h" |
| | 22 | |
| | 23 | #include "avcodec.h" |
| | 24 | #include "get_bits.h" |
| | 25 | #include "internal.h" |
| | 26 | |
| | 27 | typedef struct HEntry { |
| | 28 | int16_t l, r; |
| | 29 | } HEntry; |
| | 30 | |
| | 31 | typedef struct HCOMContext { |
| | 32 | AVCodecContext *avctx; |
| | 33 | |
| | 34 | uint8_t first_sample; |
| | 35 | uint8_t sample; |
| | 36 | int dict_entries; |
| | 37 | int dict_entry; |
| | 38 | int delta_compression; |
| | 39 | |
| | 40 | HEntry *dict; |
| | 41 | } HCOMContext; |
| | 42 | |
| | 43 | static av_cold int hcom_init(AVCodecContext *avctx) |
| | 44 | { |
| | 45 | HCOMContext *s = avctx->priv_data; |
| | 46 | |
| | 47 | if (avctx->channels != 1) { |
| | 48 | av_log(avctx, AV_LOG_ERROR, "invalid number of channels\n"); |
| | 49 | return AVERROR_INVALIDDATA; |
| | 50 | } |
| | 51 | |
| | 52 | if (avctx->extradata_size <= 7) |
| | 53 | return AVERROR_INVALIDDATA; |
| | 54 | s->dict_entries = AV_RB16(avctx->extradata); |
| | 55 | if (avctx->extradata_size < s->dict_entries * 4 + 7) |
| | 56 | return AVERROR_INVALIDDATA; |
| | 57 | s->delta_compression = AV_RB32(avctx->extradata + 2); |
| | 58 | s->sample = s->first_sample = avctx->extradata[avctx->extradata_size - 1]; |
| | 59 | |
| | 60 | s->dict = av_calloc(s->dict_entries, sizeof(*s->dict)); |
| | 61 | if (!s->dict) |
| | 62 | return AVERROR(ENOMEM); |
| | 63 | for (int i = 0; i < s->dict_entries; i++) { |
| | 64 | s->dict[i].l = AV_RB16(avctx->extradata + 6 + 4 * i); |
| | 65 | s->dict[i].r = AV_RB16(avctx->extradata + 6 + 4 * i + 2); |
| | 66 | } |
| | 67 | |
| | 68 | avctx->sample_fmt = AV_SAMPLE_FMT_U8; |
| | 69 | s->dict_entry = 0; |
| | 70 | |
| | 71 | return 0; |
| | 72 | } |
| | 73 | |
| | 74 | static int hcom_decode(AVCodecContext *avctx, void *data, |
| | 75 | int *got_frame, AVPacket *pkt) |
| | 76 | { |
| | 77 | HCOMContext *s = avctx->priv_data; |
| | 78 | AVFrame *frame = data; |
| | 79 | GetBitContext gb; |
| | 80 | int ret, n = 0; |
| | 81 | |
| | 82 | if (pkt->size > INT16_MAX) |
| | 83 | return AVERROR_INVALIDDATA; |
| | 84 | |
| | 85 | frame->nb_samples = pkt->size * 8; |
| | 86 | if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) |
| | 87 | return ret; |
| | 88 | |
| | 89 | if ((ret = init_get_bits8(&gb, pkt->data, pkt->size)) < 0) |
| | 90 | return ret; |
| | 91 | |
| | 92 | while (get_bits_left(&gb) > 0) { |
| | 93 | if (get_bits1(&gb)) { |
| | 94 | s->dict_entry = s->dict[s->dict_entry].r; |
| | 95 | } else { |
| | 96 | s->dict_entry = s->dict[s->dict_entry].l; |
| | 97 | } |
| | 98 | |
| | 99 | if (s->dict[s->dict_entry].l < 0) { |
| | 100 | int16_t datum; |
| | 101 | |
| | 102 | datum = s->dict[s->dict_entry].r; |
| | 103 | |
| | 104 | if (!s->delta_compression) |
| | 105 | s->sample = 0; |
| | 106 | s->sample = (s->sample + datum) & 0xFF; |
| | 107 | |
| | 108 | frame->data[0][n++] = s->sample; |
| | 109 | |
| | 110 | s->dict_entry = 0; |
| | 111 | } |
| | 112 | } |
| | 113 | |
| | 114 | frame->nb_samples = n; |
| | 115 | |
| | 116 | *got_frame = 1; |
| | 117 | |
| | 118 | return pkt->size; |
| | 119 | } |
| | 120 | |
| | 121 | static av_cold int hcom_close(AVCodecContext *avctx) |
| | 122 | { |
| | 123 | HCOMContext *s = avctx->priv_data; |
| | 124 | |
| | 125 | av_freep(&s->dict); |
| | 126 | |
| | 127 | return 0; |
| | 128 | } |
| | 129 | |
| | 130 | AVCodec ff_hcom_decoder = { |
| | 131 | .name = "hcom", |
| | 132 | .long_name = NULL_IF_CONFIG_SMALL("HCOM Audio"), |
| | 133 | .type = AVMEDIA_TYPE_AUDIO, |
| | 134 | .id = AV_CODEC_ID_HCOM, |
| | 135 | .priv_data_size = sizeof(HCOMContext), |
| | 136 | .init = hcom_init, |
| | 137 | .close = hcom_close, |
| | 138 | .decode = hcom_decode, |
| | 139 | .capabilities = AV_CODEC_CAP_DR1, |
| | 140 | }; |