2 * Copyright (c) 2012 Laurent Aimar
4 * This file is part of FFmpeg.
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.
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.
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
21 #include "libavutil/intreadwrite.h"
25 typedef struct DVAudioContext {
29 int16_t shuffle[2000];
32 static av_cold int decode_init(AVCodecContext *avctx)
34 DVAudioContext *s = avctx->priv_data;
37 if (avctx->channels != 2) {
38 av_log(avctx, AV_LOG_ERROR, "invalid number of channels\n");
39 return AVERROR(EINVAL);
42 if (avctx->codec_tag == 0x0215) {
44 } else if (avctx->codec_tag == 0x0216) {
46 } else if (avctx->block_align == 7200 ||
47 avctx->block_align == 8640) {
48 s->block_size = avctx->block_align;
50 return AVERROR(EINVAL);
53 s->is_pal = s->block_size == 8640;
54 s->is_12bit = avctx->bits_per_raw_sample == 12;
55 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
56 avctx->channel_layout = AV_CH_LAYOUT_STEREO;
58 for (i = 0; i < FF_ARRAY_ELEMS(s->shuffle); i++) {
59 const unsigned a = s->is_pal ? 18 : 15;
60 const unsigned b = 3 * a;
62 s->shuffle[i] = 80 * ((21 * (i % 3) + 9 * (i / 3) + ((i / a) % 3)) % b) +
63 (2 + s->is_12bit) * (i / b) + 8;
69 static inline int dv_get_audio_sample_count(const uint8_t *buffer, int dsf)
71 int samples = buffer[0] & 0x3f; /* samples in this frame - min samples */
73 switch ((buffer[3] >> 3) & 0x07) {
75 return samples + (dsf ? 1896 : 1580);
77 return samples + (dsf ? 1742 : 1452);
80 return samples + (dsf ? 1264 : 1053);
84 static inline uint16_t dv_audio_12to16(uint16_t sample)
86 uint16_t shift, result;
88 sample = (sample < 0x800) ? sample : sample | 0xf000;
89 shift = (sample & 0xf00) >> 8;
91 if (shift < 0x2 || shift > 0xd) {
93 } else if (shift < 0x8) {
95 result = (sample - (256 * shift)) << shift;
98 result = ((sample + ((256 * shift) + 1)) << shift) - 1;
104 static int decode_frame(AVCodecContext *avctx, void *data,
105 int *got_frame_ptr, AVPacket *pkt)
107 DVAudioContext *s = avctx->priv_data;
108 AVFrame *frame = data;
109 const uint8_t *src = pkt->data;
113 if (pkt->size < s->block_size)
114 return AVERROR_INVALIDDATA;
116 frame->nb_samples = dv_get_audio_sample_count(pkt->data + 244, s->is_pal);
117 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
119 dst = (int16_t *)frame->data[0];
121 for (i = 0; i < frame->nb_samples; i++) {
122 const uint8_t *v = &src[s->shuffle[i]];
125 *dst++ = dv_audio_12to16((v[0] << 4) | ((v[2] >> 4) & 0x0f));
126 *dst++ = dv_audio_12to16((v[1] << 4) | ((v[2] >> 0) & 0x0f));
128 *dst++ = AV_RB16(&v[0]);
129 *dst++ = AV_RB16(&v[s->is_pal ? 4320 : 3600]);
135 return s->block_size;
138 AVCodec ff_dvaudio_decoder = {
140 .long_name = NULL_IF_CONFIG_SMALL("Ulead DV Audio"),
141 .type = AVMEDIA_TYPE_AUDIO,
142 .id = AV_CODEC_ID_DVAUDIO,
144 .decode = decode_frame,
145 .capabilities = AV_CODEC_CAP_DR1,
146 .priv_data_size = sizeof(DVAudioContext),