dca: fix misaligned access in ff_dca_convert_bitstream
[libav.git] / libavcodec / dca.c
CommitLineData
9e4bca16
DB
1/*
2 * DCA compatible decoder data
3 *
4 * This file is part of Libav.
5 *
6 * Libav 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 * Libav 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 Libav; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#include <stdint.h>
1db6a080 22#include <string.h>
9e4bca16 23
973dc4e8
VG
24#include "libavutil/error.h"
25
9e4bca16 26#include "dca.h"
25f613f8 27#include "dca_syncwords.h"
973dc4e8 28#include "put_bits.h"
9e4bca16 29
45ff7c93 30const uint32_t avpriv_dca_sample_rates[16] = {
9e4bca16
DB
31 0, 8000, 16000, 32000, 0, 0, 11025, 22050, 44100, 0, 0,
32 12000, 24000, 48000, 96000, 192000
33};
1db6a080
DB
34
35int ff_dca_convert_bitstream(const uint8_t *src, int src_size, uint8_t *dst,
36 int max_size)
37{
38 uint32_t mrk;
39 int i, tmp;
1db6a080
DB
40 PutBitContext pb;
41
42 if ((unsigned) src_size > (unsigned) max_size)
43 src_size = max_size;
44
45 mrk = AV_RB32(src);
46 switch (mrk) {
25f613f8 47 case DCA_SYNCWORD_CORE_BE:
1db6a080
DB
48 memcpy(dst, src, src_size);
49 return src_size;
25f613f8 50 case DCA_SYNCWORD_CORE_LE:
b06cb15b
AC
51 for (i = 0; i < (src_size + 1) >> 1; i++) {
52 AV_WB16(dst, AV_RL16(src));
53 src += 2;
54 dst += 2;
55 }
1db6a080 56 return src_size;
25f613f8
DB
57 case DCA_SYNCWORD_CORE_14B_BE:
58 case DCA_SYNCWORD_CORE_14B_LE:
1db6a080
DB
59 init_put_bits(&pb, dst, max_size);
60 for (i = 0; i < (src_size + 1) >> 1; i++, src += 2) {
25f613f8 61 tmp = ((mrk == DCA_SYNCWORD_CORE_14B_BE) ? AV_RB16(src) : AV_RL16(src)) & 0x3FFF;
1db6a080
DB
62 put_bits(&pb, 14, tmp);
63 }
64 flush_put_bits(&pb);
65 return (put_bits_count(&pb) + 7) >> 3;
66 default:
67 return AVERROR_INVALIDDATA;
68 }
69}