| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Copyright (C) 2017 foo86 | ||
| 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/avassert.h" | ||
| 22 | #include "get_bits.h" | ||
| 23 | #include "put_bits.h" | ||
| 24 | #include "dolby_e.h" | ||
| 25 | |||
| 26 | static const uint8_t nb_programs_tab[MAX_PROG_CONF + 1] = { | ||
| 27 | 2, 3, 2, 3, 4, 5, 4, 5, 6, 7, 8, 1, 2, 3, 3, 4, 5, 6, 1, 2, 3, 4, 1, 1 | ||
| 28 | }; | ||
| 29 | |||
| 30 | static const uint8_t nb_channels_tab[MAX_PROG_CONF + 1] = { | ||
| 31 | 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 6, 6, 6, 6, 6, 6, 6, 4, 4, 4, 4, 8, 8 | ||
| 32 | }; | ||
| 33 | |||
| 34 | static const uint16_t sample_rate_tab[16] = { | ||
| 35 | 0, 42965, 43008, 44800, 53706, 53760 | ||
| 36 | }; | ||
| 37 | |||
| 38 | 1284 | static int skip_input(DBEContext *s, int nb_words) | |
| 39 | { | ||
| 40 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1284 times.
|
1284 | if (nb_words > s->input_size) { |
| 41 | ✗ | return AVERROR_INVALIDDATA; | |
| 42 | } | ||
| 43 | |||
| 44 | 1284 | s->input += nb_words * s->word_bytes; | |
| 45 | 1284 | s->input_size -= nb_words; | |
| 46 | 1284 | return 0; | |
| 47 | } | ||
| 48 | |||
| 49 | 642 | static int parse_key(DBEContext *s) | |
| 50 | { | ||
| 51 |
1/2✓ Branch 0 taken 642 times.
✗ Branch 1 not taken.
|
642 | if (s->key_present) { |
| 52 | 642 | const uint8_t *key = s->input; | |
| 53 | 642 | int ret = skip_input(s, 1); | |
| 54 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 642 times.
|
642 | if (ret < 0) |
| 55 | ✗ | return ret; | |
| 56 | 642 | return AV_RB24(key) >> 24 - s->word_bits; | |
| 57 | } | ||
| 58 | ✗ | return 0; | |
| 59 | } | ||
| 60 | |||
| 61 | 4380 | int ff_dolby_e_convert_input(DBEContext *s, int nb_words, int key) | |
| 62 | { | ||
| 63 | 4380 | const uint8_t *src = s->input; | |
| 64 | 4380 | uint8_t *dst = s->buffer; | |
| 65 | PutBitContext pb; | ||
| 66 | int i; | ||
| 67 | |||
| 68 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4380 times.
|
4380 | av_assert0(nb_words <= 1024u); |
| 69 | |||
| 70 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4380 times.
|
4380 | if (nb_words > s->input_size) { |
| 71 | ✗ | if (s->avctx) | |
| 72 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "Packet too short\n"); | |
| 73 | ✗ | return AVERROR_INVALIDDATA; | |
| 74 | } | ||
| 75 | |||
| 76 |
1/4✓ Branch 0 taken 4380 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
4380 | switch (s->word_bits) { |
| 77 | 4380 | case 16: | |
| 78 |
2/2✓ Branch 0 taken 946764 times.
✓ Branch 1 taken 4380 times.
|
951144 | for (i = 0; i < nb_words; i++, src += 2, dst += 2) |
| 79 | 946764 | AV_WB16(dst, AV_RB16(src) ^ key); | |
| 80 | 4380 | break; | |
| 81 | ✗ | case 20: | |
| 82 | ✗ | init_put_bits(&pb, s->buffer, sizeof(s->buffer)); | |
| 83 | ✗ | for (i = 0; i < nb_words; i++, src += 3) | |
| 84 | ✗ | put_bits(&pb, 20, AV_RB24(src) >> 4 ^ key); | |
| 85 | ✗ | flush_put_bits(&pb); | |
| 86 | ✗ | break; | |
| 87 | ✗ | case 24: | |
| 88 | ✗ | for (i = 0; i < nb_words; i++, src += 3, dst += 3) | |
| 89 | ✗ | AV_WB24(dst, AV_RB24(src) ^ key); | |
| 90 | ✗ | break; | |
| 91 | ✗ | default: | |
| 92 | ✗ | av_unreachable("ff_dolby_e_parse_header() only sets 16, 20, 24 and errors out otherwise"); | |
| 93 | } | ||
| 94 | |||
| 95 | 4380 | return init_get_bits(&s->gb, s->buffer, nb_words * s->word_bits); | |
| 96 | } | ||
| 97 | |||
| 98 | 643 | int ff_dolby_e_parse_header(DBEContext *s, const uint8_t *buf, int buf_size) | |
| 99 | { | ||
| 100 | 643 | DolbyEHeaderInfo *const header = &s->metadata; | |
| 101 | int hdr, ret, key, mtd_size; | ||
| 102 | |||
| 103 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 642 times.
|
643 | if (buf_size < 3) |
| 104 | 1 | return AVERROR_INVALIDDATA; | |
| 105 | |||
| 106 | 642 | hdr = AV_RB24(buf); | |
| 107 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 642 times.
|
642 | if ((hdr & 0xfffffe) == 0x7888e) { |
| 108 | ✗ | s->word_bits = 24; | |
| 109 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 642 times.
|
642 | } else if ((hdr & 0xffffe0) == 0x788e0) { |
| 110 | ✗ | s->word_bits = 20; | |
| 111 |
1/2✓ Branch 0 taken 642 times.
✗ Branch 1 not taken.
|
642 | } else if ((hdr & 0xfffe00) == 0x78e00) { |
| 112 | 642 | s->word_bits = 16; | |
| 113 | } else { | ||
| 114 | ✗ | if (s->avctx) | |
| 115 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "Invalid frame header\n"); | |
| 116 | ✗ | return AVERROR_INVALIDDATA; | |
| 117 | } | ||
| 118 | |||
| 119 | 642 | s->word_bytes = s->word_bits + 7 >> 3; | |
| 120 | 642 | s->input = buf + s->word_bytes; | |
| 121 | 642 | s->input_size = buf_size / s->word_bytes - 1; | |
| 122 | 642 | s->key_present = hdr >> 24 - s->word_bits & 1; | |
| 123 | |||
| 124 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 642 times.
|
642 | if ((key = parse_key(s)) < 0) |
| 125 | ✗ | return key; | |
| 126 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 642 times.
|
642 | if ((ret = ff_dolby_e_convert_input(s, 1, key)) < 0) |
| 127 | ✗ | return ret; | |
| 128 | |||
| 129 | 642 | skip_bits(&s->gb, 4); | |
| 130 | 642 | mtd_size = get_bits(&s->gb, 10); | |
| 131 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 642 times.
|
642 | if (!mtd_size) { |
| 132 | ✗ | if (s->avctx) | |
| 133 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "Invalid metadata size\n"); | |
| 134 | ✗ | return AVERROR_INVALIDDATA; | |
| 135 | } | ||
| 136 | |||
| 137 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 642 times.
|
642 | if ((ret = ff_dolby_e_convert_input(s, mtd_size, key)) < 0) |
| 138 | ✗ | return ret; | |
| 139 | |||
| 140 | 642 | skip_bits(&s->gb, 14); | |
| 141 | 642 | header->prog_conf = get_bits(&s->gb, 6); | |
| 142 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 642 times.
|
642 | if (header->prog_conf > MAX_PROG_CONF) { |
| 143 | ✗ | if (s->avctx) | |
| 144 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "Invalid program configuration\n"); | |
| 145 | ✗ | return AVERROR_INVALIDDATA; | |
| 146 | } | ||
| 147 | |||
| 148 | 642 | header->nb_channels = nb_channels_tab[header->prog_conf]; | |
| 149 | 642 | header->nb_programs = nb_programs_tab[header->prog_conf]; | |
| 150 | |||
| 151 | 642 | header->fr_code = get_bits(&s->gb, 4); | |
| 152 | 642 | header->fr_code_orig = get_bits(&s->gb, 4); | |
| 153 |
1/2✓ Branch 0 taken 642 times.
✗ Branch 1 not taken.
|
642 | if (!(header->sample_rate = sample_rate_tab[header->fr_code]) || |
| 154 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 642 times.
|
642 | !sample_rate_tab[header->fr_code_orig]) { |
| 155 | ✗ | if (s->avctx) | |
| 156 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "Invalid frame rate code\n"); | |
| 157 | ✗ | return AVERROR_INVALIDDATA; | |
| 158 | } | ||
| 159 | |||
| 160 | 642 | skip_bits_long(&s->gb, 88); | |
| 161 |
2/2✓ Branch 0 taken 3852 times.
✓ Branch 1 taken 642 times.
|
4494 | for (int i = 0; i < header->nb_channels; i++) |
| 162 | 3852 | header->ch_size[i] = get_bits(&s->gb, 10); | |
| 163 | 642 | header->mtd_ext_size = get_bits(&s->gb, 8); | |
| 164 | 642 | header->meter_size = get_bits(&s->gb, 8); | |
| 165 | |||
| 166 | 642 | skip_bits_long(&s->gb, 10 * header->nb_programs); | |
| 167 |
2/2✓ Branch 0 taken 3852 times.
✓ Branch 1 taken 642 times.
|
4494 | for (int i = 0; i < header->nb_channels; i++) { |
| 168 | 3852 | header->rev_id[i] = get_bits(&s->gb, 4); | |
| 169 | 3852 | skip_bits1(&s->gb); | |
| 170 | 3852 | header->begin_gain[i] = get_bits(&s->gb, 10); | |
| 171 | 3852 | header->end_gain[i] = get_bits(&s->gb, 10); | |
| 172 | } | ||
| 173 | |||
| 174 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 642 times.
|
642 | if (get_bits_left(&s->gb) < 0) { |
| 175 | ✗ | if (s->avctx) | |
| 176 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "Read past end of metadata\n"); | |
| 177 | ✗ | return AVERROR_INVALIDDATA; | |
| 178 | } | ||
| 179 | |||
| 180 | 642 | return skip_input(s, mtd_size + 1); | |
| 181 | } | ||
| 182 |