]> git.sesse.net Git - ffmpeg/blob - libavcodec/pcm-dvd.c
x86/simple_idct: use LOCAL_ALIGNED instead of DECLARE_ALIGNED
[ffmpeg] / libavcodec / pcm-dvd.c
1 /*
2  * LPCM codecs for PCM formats found in Video DVD streams
3  * Copyright (c) 2009-2013 Christian Schmidt
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 /**
23  * @file
24  * LPCM codecs for PCM formats found in Video DVD streams
25  */
26
27 #include "avcodec.h"
28 #include "bytestream.h"
29 #include "internal.h"
30
31 typedef struct PCMDVDContext {
32     uint32_t last_header;    // Cached header to see if parsing is needed
33     int block_size;          // Size of a block of samples in bytes
34     int last_block_size;     // Size of the last block of samples in bytes
35     int samples_per_block;   // Number of samples per channel per block
36     int groups_per_block;    // Number of 20/24bit sample groups per block
37     uint8_t *extra_samples;  // Pointer to leftover samples from a frame
38     int extra_sample_count;  // Number of leftover samples in the buffer
39 } PCMDVDContext;
40
41 static av_cold int pcm_dvd_decode_init(AVCodecContext *avctx)
42 {
43     PCMDVDContext *s = avctx->priv_data;
44
45     /* Invalid header to force parsing of the first header */
46     s->last_header = -1;
47     /* reserve space for 8 channels, 3 bytes/sample, 4 samples/block */
48     if (!(s->extra_samples = av_malloc(8 * 3 * 4)))
49         return AVERROR(ENOMEM);
50     s->extra_sample_count = 0;
51
52     return 0;
53 }
54
55 static av_cold int pcm_dvd_decode_uninit(AVCodecContext *avctx)
56 {
57     PCMDVDContext *s = avctx->priv_data;
58
59     av_freep(&s->extra_samples);
60
61     return 0;
62 }
63
64 static int pcm_dvd_parse_header(AVCodecContext *avctx, const uint8_t *header)
65 {
66     /* no traces of 44100 and 32000Hz in any commercial software or player */
67     static const uint32_t frequencies[4] = { 48000, 96000, 44100, 32000 };
68     PCMDVDContext *s = avctx->priv_data;
69     int header_int = (header[0] & 0xe0) | (header[1] << 8) | (header[2] << 16);
70
71     /* early exit if the header didn't change apart from the frame number */
72     if (s->last_header == header_int)
73         return 0;
74
75     if (avctx->debug & FF_DEBUG_PICT_INFO)
76         av_dlog(avctx, "pcm_dvd_parse_header: header = %02x%02x%02x\n",
77                 header[0], header[1], header[2]);
78     /*
79      * header[0] emphasis (1), muse(1), reserved(1), frame number(5)
80      * header[1] quant (2), freq(2), reserved(1), channels(3)
81      * header[2] dynamic range control (0x80 = off)
82      */
83
84     /* get the sample depth and derive the sample format from it */
85     avctx->bits_per_coded_sample = 16 + (header[1] >> 6 & 3) * 4;
86     if (avctx->bits_per_coded_sample == 28) {
87         av_log(avctx, AV_LOG_ERROR,
88                "PCM DVD unsupported sample depth %i\n",
89                avctx->bits_per_coded_sample);
90         return AVERROR_INVALIDDATA;
91     }
92     avctx->sample_fmt = avctx->bits_per_coded_sample == 16 ? AV_SAMPLE_FMT_S16
93                                                            : AV_SAMPLE_FMT_S32;
94     avctx->bits_per_raw_sample = avctx->bits_per_coded_sample;
95
96     /* get the sample rate */
97     avctx->sample_rate = frequencies[header[1] >> 4 & 3];
98
99     /* get the number of channels */
100     avctx->channels = 1 + (header[1] & 7);
101     /* calculate the bitrate */
102     avctx->bit_rate = avctx->channels *
103                       avctx->sample_rate *
104                       avctx->bits_per_coded_sample;
105
106     /* 4 samples form a group in 20/24bit PCM on DVD Video.
107      * A block is formed by the number of groups that are
108      * needed to complete a set of samples for each channel. */
109     if (avctx->bits_per_coded_sample == 16) {
110         s->samples_per_block = 1;
111         s->block_size        = avctx->channels * 2;
112     } else {
113         switch (avctx->channels) {
114         case 1:
115         case 2:
116         case 4:
117             /* one group has all the samples needed */
118             s->block_size        = 4 * avctx->bits_per_coded_sample / 8;
119             s->samples_per_block = 4 / avctx->channels;
120             s->groups_per_block  = 1;
121             break;
122         case 8:
123             /* two groups have all the samples needed */
124             s->block_size        = 8 * avctx->bits_per_coded_sample / 8;
125             s->samples_per_block = 1;
126             s->groups_per_block  = 2;
127             break;
128         default:
129             /* need avctx->channels groups */
130             s->block_size        = 4 * avctx->channels *
131                                    avctx->bits_per_coded_sample / 8;
132             s->samples_per_block = 4;
133             s->groups_per_block  = avctx->channels;
134             break;
135         }
136     }
137
138     if (avctx->debug & FF_DEBUG_PICT_INFO)
139         av_dlog(avctx,
140                 "pcm_dvd_parse_header: %d channels, %d bits per sample, %d Hz, %d bit/s\n",
141                 avctx->channels, avctx->bits_per_coded_sample,
142                 avctx->sample_rate, avctx->bit_rate);
143
144     s->last_header = header_int;
145
146     return 0;
147 }
148
149 static void *pcm_dvd_decode_samples(AVCodecContext *avctx, const uint8_t *src,
150                                     void *dst, int blocks)
151 {
152     PCMDVDContext *s = avctx->priv_data;
153     int16_t *dst16   = dst;
154     int32_t *dst32   = dst;
155     GetByteContext gb;
156     int i;
157     uint8_t t;
158     int samples;
159
160     bytestream2_init(&gb, src, blocks * s->block_size);
161     switch (avctx->bits_per_coded_sample) {
162     case 16:
163 #if HAVE_BIGENDIAN
164         bytestream2_get_buffer(&gb, dst16, blocks * s->block_size);
165         dst16 += blocks * s->block_size / 2;
166 #else
167         samples = blocks * avctx->channels;
168         do {
169             *dst16++ = bytestream2_get_be16u(&gb);
170         } while (--samples);
171 #endif
172         return dst16;
173     case 20:
174         do {
175             for (i = s->groups_per_block; i; i--) {
176                 dst32[0] = bytestream2_get_be16u(&gb) << 16;
177                 dst32[1] = bytestream2_get_be16u(&gb) << 16;
178                 dst32[2] = bytestream2_get_be16u(&gb) << 16;
179                 dst32[3] = bytestream2_get_be16u(&gb) << 16;
180                 t = bytestream2_get_byteu(&gb);
181                 *dst32 += (t & 0xf0) << 8;
182                 *dst32 += (t & 0x0f) << 12;
183                 t = bytestream2_get_byteu(&gb);
184                 *dst32 += (t & 0xf0) << 8;
185                 *dst32 += (t & 0x0f) << 12;
186             }
187         } while (--blocks);
188         return dst32;
189     case 24:
190         do {
191             for (i = s->groups_per_block; i; i--) {
192                 dst32[0] = bytestream2_get_be16u(&gb) << 16;
193                 dst32[1] = bytestream2_get_be16u(&gb) << 16;
194                 dst32[2] = bytestream2_get_be16u(&gb) << 16;
195                 dst32[3] = bytestream2_get_be16u(&gb) << 16;
196                 *dst32++ += bytestream2_get_byteu(&gb) << 8;
197                 *dst32++ += bytestream2_get_byteu(&gb) << 8;
198                 *dst32++ += bytestream2_get_byteu(&gb) << 8;
199                 *dst32++ += bytestream2_get_byteu(&gb) << 8;
200             }
201         } while (--blocks);
202         return dst32;
203     default:
204         return NULL;
205     }
206 }
207
208 static int pcm_dvd_decode_frame(AVCodecContext *avctx, void *data,
209                                 int *got_frame_ptr, AVPacket *avpkt)
210 {
211     AVFrame *frame     = data;
212     const uint8_t *src = avpkt->data;
213     int buf_size       = avpkt->size;
214     PCMDVDContext *s   = avctx->priv_data;
215     int retval;
216     int blocks;
217     void *dst;
218
219     if (buf_size < 3) {
220         av_log(avctx, AV_LOG_ERROR, "PCM packet too small\n");
221         return AVERROR_INVALIDDATA;
222     }
223
224     if ((retval = pcm_dvd_parse_header(avctx, src)))
225         return retval;
226     if (s->last_block_size != s->block_size) {
227         av_log(avctx, AV_LOG_WARNING, "block_size has changed\n");
228         s->extra_sample_count = 0;
229     }
230     s->last_block_size = s->block_size;
231     src      += 3;
232     buf_size -= 3;
233
234     blocks = (buf_size + s->extra_sample_count) / s->block_size;
235
236     /* get output buffer */
237     frame->nb_samples = blocks * s->samples_per_block;
238     if ((retval = ff_get_buffer(avctx, frame, 0)) < 0)
239         return retval;
240     dst = frame->data[0];
241
242     /* consume leftover samples from last packet */
243     if (s->extra_sample_count) {
244         int missing_samples = s->block_size - s->extra_sample_count;
245         if (buf_size >= missing_samples) {
246             memcpy(s->extra_samples + s->extra_sample_count, src,
247                    missing_samples);
248             dst = pcm_dvd_decode_samples(avctx, s->extra_samples, dst, 1);
249             src += missing_samples;
250             buf_size -= missing_samples;
251             s->extra_sample_count = 0;
252             blocks--;
253         } else {
254             /* new packet still doesn't have enough samples */
255             memcpy(s->extra_samples + s->extra_sample_count, src, buf_size);
256             s->extra_sample_count += buf_size;
257             return avpkt->size;
258         }
259     }
260
261     /* decode remaining complete samples */
262     if (blocks) {
263         pcm_dvd_decode_samples(avctx, src, dst, blocks);
264         buf_size -= blocks * s->block_size;
265     }
266
267     /* store leftover samples */
268     if (buf_size) {
269         src += blocks * s->block_size;
270         memcpy(s->extra_samples, src, buf_size);
271         s->extra_sample_count = buf_size;
272     }
273
274     *got_frame_ptr = 1;
275
276     return avpkt->size;
277 }
278
279 AVCodec ff_pcm_dvd_decoder = {
280     .name           = "pcm_dvd",
281     .long_name      = NULL_IF_CONFIG_SMALL("PCM signed 16|20|24-bit big-endian for DVD media"),
282     .type           = AVMEDIA_TYPE_AUDIO,
283     .id             = AV_CODEC_ID_PCM_DVD,
284     .priv_data_size = sizeof(PCMDVDContext),
285     .init           = pcm_dvd_decode_init,
286     .decode         = pcm_dvd_decode_frame,
287     .close          = pcm_dvd_decode_uninit,
288     .capabilities   = CODEC_CAP_DR1,
289     .sample_fmts    = (const enum AVSampleFormat[]) {
290         AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_NONE
291     }
292 };