/src/ffmpeg/libavcodec/dvaudiodec.c
Line | Count | Source (jump to first uncovered line) |
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/channel_layout.h" |
22 | | #include "libavutil/intreadwrite.h" |
23 | | #include "avcodec.h" |
24 | | #include "codec_internal.h" |
25 | | #include "decode.h" |
26 | | #include "dvaudio.h" |
27 | | |
28 | | typedef struct DVAudioContext { |
29 | | int block_size; |
30 | | int is_12bit; |
31 | | int is_pal; |
32 | | int16_t shuffle[2000]; |
33 | | } DVAudioContext; |
34 | | |
35 | | static av_cold int decode_init(AVCodecContext *avctx) |
36 | 743 | { |
37 | 743 | DVAudioContext *s = avctx->priv_data; |
38 | 743 | int i; |
39 | | |
40 | 743 | if (avctx->codec_tag == 0x0215) { |
41 | 5 | s->block_size = 7200; |
42 | 738 | } else if (avctx->codec_tag == 0x0216) { |
43 | 18 | s->block_size = 8640; |
44 | 720 | } else if (avctx->block_align == 7200 || |
45 | 720 | avctx->block_align == 8640) { |
46 | 615 | s->block_size = avctx->block_align; |
47 | 615 | } else { |
48 | 105 | return AVERROR(EINVAL); |
49 | 105 | } |
50 | | |
51 | 638 | s->is_pal = s->block_size == 8640; |
52 | 638 | s->is_12bit = avctx->bits_per_coded_sample == 12; |
53 | 638 | avctx->sample_fmt = AV_SAMPLE_FMT_S16; |
54 | 638 | av_channel_layout_uninit(&avctx->ch_layout); |
55 | 638 | avctx->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO; |
56 | | |
57 | 1.27M | for (i = 0; i < FF_ARRAY_ELEMS(s->shuffle); i++) { |
58 | 1.27M | const unsigned a = s->is_pal ? 18 : 15; |
59 | 1.27M | const unsigned b = 3 * a; |
60 | | |
61 | 1.27M | s->shuffle[i] = 80 * ((21 * (i % 3) + 9 * (i / 3) + ((i / a) % 3)) % b) + |
62 | 1.27M | (2 + s->is_12bit) * (i / b) + 8; |
63 | 1.27M | } |
64 | | |
65 | 638 | return 0; |
66 | 743 | } |
67 | | |
68 | | static inline uint16_t dv_audio_12to16(uint16_t sample) |
69 | 1.96M | { |
70 | 1.96M | uint16_t shift, result; |
71 | | |
72 | 1.96M | sample = (sample < 0x800) ? sample : sample | 0xf000; |
73 | 1.96M | shift = (sample & 0xf00) >> 8; |
74 | | |
75 | 1.96M | if (shift < 0x2 || shift > 0xd) { |
76 | 1.33M | result = sample; |
77 | 1.33M | } else if (shift < 0x8) { |
78 | 419k | shift--; |
79 | 419k | result = (sample - (256 * shift)) << shift; |
80 | 419k | } else { |
81 | 213k | shift = 0xe - shift; |
82 | 213k | result = ((sample + ((256 * shift) + 1)) << shift) - 1; |
83 | 213k | } |
84 | | |
85 | 1.96M | return result; |
86 | 1.96M | } |
87 | | |
88 | | static int decode_frame(AVCodecContext *avctx, AVFrame *frame, |
89 | | int *got_frame_ptr, AVPacket *pkt) |
90 | 110k | { |
91 | 110k | DVAudioContext *s = avctx->priv_data; |
92 | 110k | const uint8_t *src = pkt->data; |
93 | 110k | int16_t *dst; |
94 | 110k | int ret, i; |
95 | | |
96 | 110k | if (pkt->size < s->block_size) |
97 | 103k | return AVERROR_INVALIDDATA; |
98 | | |
99 | 6.78k | frame->nb_samples = dv_get_audio_sample_count(pkt->data + 244, s->is_pal); |
100 | 6.78k | if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) |
101 | 0 | return ret; |
102 | 6.78k | dst = (int16_t *)frame->data[0]; |
103 | | |
104 | 8.72M | for (i = 0; i < frame->nb_samples; i++) { |
105 | 8.71M | const uint8_t *v = &src[s->shuffle[i]]; |
106 | | |
107 | 8.71M | if (s->is_12bit) { |
108 | 984k | *dst++ = dv_audio_12to16((v[0] << 4) | ((v[2] >> 4) & 0x0f)); |
109 | 984k | *dst++ = dv_audio_12to16((v[1] << 4) | ((v[2] >> 0) & 0x0f)); |
110 | 7.73M | } else { |
111 | 7.73M | *dst++ = AV_RB16(&v[0]); |
112 | 7.73M | *dst++ = AV_RB16(&v[s->is_pal ? 4320 : 3600]); |
113 | 7.73M | } |
114 | 8.71M | } |
115 | | |
116 | 6.78k | *got_frame_ptr = 1; |
117 | | |
118 | 6.78k | return s->block_size; |
119 | 6.78k | } |
120 | | |
121 | | const FFCodec ff_dvaudio_decoder = { |
122 | | .p.name = "dvaudio", |
123 | | CODEC_LONG_NAME("Ulead DV Audio"), |
124 | | .p.type = AVMEDIA_TYPE_AUDIO, |
125 | | .p.id = AV_CODEC_ID_DVAUDIO, |
126 | | .init = decode_init, |
127 | | FF_CODEC_DECODE_CB(decode_frame), |
128 | | .p.capabilities = AV_CODEC_CAP_DR1, |
129 | | .priv_data_size = sizeof(DVAudioContext), |
130 | | }; |