]> git.sesse.net Git - ffmpeg/blob - libavcodec/dvaudiodec.c
Merge commit '09f4822e4eaf61513b9092414450f3ae920ccd9d'
[ffmpeg] / libavcodec / dvaudiodec.c
1 /*
2  * Copyright (c) 2012 Laurent Aimar
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 #include "avcodec.h"
23 #include "internal.h"
24
25 typedef struct DVAudioContext {
26     int block_size;
27     int is_12bit;
28     int is_pal;
29     int16_t shuffle[2000];
30 } DVAudioContext;
31
32 static av_cold int decode_init(AVCodecContext *avctx)
33 {
34     DVAudioContext *s = avctx->priv_data;
35     int i;
36
37     if (avctx->channels != 2) {
38         av_log(avctx, AV_LOG_ERROR, "invalid number of channels\n");
39         return AVERROR(EINVAL);
40     }
41
42     if (avctx->codec_tag == 0x0215) {
43         s->block_size = 7200;
44     } else if (avctx->codec_tag == 0x0216) {
45         s->block_size = 8640;
46     } else if (avctx->block_align == 7200 ||
47                avctx->block_align == 8640) {
48         s->block_size = avctx->block_align;
49     } else {
50         return AVERROR(EINVAL);
51     }
52
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;
57
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;
61
62         s->shuffle[i] = 80 * ((21 * (i % 3) + 9 * (i / 3) + ((i / a) % 3)) % b) +
63                          (2 + s->is_12bit) * (i / b) + 8;
64     }
65
66     return 0;
67 }
68
69 static inline int dv_get_audio_sample_count(const uint8_t *buffer, int dsf)
70 {
71     int samples = buffer[0] & 0x3f; /* samples in this frame - min samples */
72
73     switch ((buffer[3] >> 3) & 0x07) {
74     case 0:
75         return samples + (dsf ? 1896 : 1580);
76     case 1:
77         return samples + (dsf ? 1742 : 1452);
78     case 2:
79     default:
80         return samples + (dsf ? 1264 : 1053);
81     }
82 }
83
84 static inline uint16_t dv_audio_12to16(uint16_t sample)
85 {
86     uint16_t shift, result;
87
88     sample = (sample < 0x800) ? sample : sample | 0xf000;
89     shift  = (sample & 0xf00) >> 8;
90
91     if (shift < 0x2 || shift > 0xd) {
92         result = sample;
93     } else if (shift < 0x8) {
94         shift--;
95         result = (sample - (256 * shift)) << shift;
96     } else {
97         shift  = 0xe - shift;
98         result = ((sample + ((256 * shift) + 1)) << shift) - 1;
99     }
100
101     return result;
102 }
103
104 static int decode_frame(AVCodecContext *avctx, void *data,
105                         int *got_frame_ptr, AVPacket *pkt)
106 {
107     DVAudioContext *s = avctx->priv_data;
108     AVFrame *frame = data;
109     const uint8_t *src = pkt->data;
110     int16_t *dst;
111     int ret, i;
112
113     if (pkt->size < s->block_size)
114         return AVERROR_INVALIDDATA;
115
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)
118         return ret;
119     dst = (int16_t *)frame->data[0];
120
121     for (i = 0; i < frame->nb_samples; i++) {
122        const uint8_t *v = &src[s->shuffle[i]];
123
124        if (s->is_12bit) {
125            *dst++ = dv_audio_12to16((v[0] << 4) | ((v[2] >> 4) & 0x0f));
126            *dst++ = dv_audio_12to16((v[1] << 4) | ((v[2] >> 0) & 0x0f));
127        } else {
128            *dst++ = AV_RB16(&v[0]);
129            *dst++ = AV_RB16(&v[s->is_pal ? 4320 : 3600]);
130        }
131     }
132
133     *got_frame_ptr = 1;
134
135     return s->block_size;
136 }
137
138 AVCodec ff_dvaudio_decoder = {
139     .name           = "dvaudio",
140     .long_name      = NULL_IF_CONFIG_SMALL("Ulead DV Audio"),
141     .type           = AVMEDIA_TYPE_AUDIO,
142     .id             = AV_CODEC_ID_DVAUDIO,
143     .init           = decode_init,
144     .decode         = decode_frame,
145     .capabilities   = AV_CODEC_CAP_DR1,
146     .priv_data_size = sizeof(DVAudioContext),
147 };