]> git.sesse.net Git - ffmpeg/blob - libavcodec/pcm-dvd.c
shorten: Use a checked bytestream reader for the wave header
[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 Libav.
6  *
7  * Libav 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  * Libav 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 Libav; 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 samples_per_block;   // Number of samples per channel per block
35     int groups_per_block;    // Number of 20/24bit sample groups per block
36     uint8_t *extra_samples;  // Pointer to leftover samples from a frame
37     int extra_sample_count;  // Number of leftover samples in the buffer
38 } PCMDVDContext;
39
40 static av_cold int pcm_dvd_decode_init(AVCodecContext *avctx)
41 {
42     PCMDVDContext *s = avctx->priv_data;
43
44     /* Invalid header to force parsing of the first header */
45     s->last_header = -1;
46     /* reserve space for 8 channels, 3 bytes/sample, 4 samples/block */
47     if (!(s->extra_samples = av_malloc(8 * 3 * 4)))
48         return AVERROR(ENOMEM);
49     s->extra_sample_count = 0;
50
51     return 0;
52 }
53
54 static av_cold int pcm_dvd_decode_uninit(AVCodecContext *avctx)
55 {
56     PCMDVDContext *s = avctx->priv_data;
57
58     if (s->extra_samples)
59         av_free(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, "PCM DVD unsupported sample depth\n");
88         return AVERROR_INVALIDDATA;
89     }
90     avctx->sample_fmt = avctx->bits_per_coded_sample == 16 ? AV_SAMPLE_FMT_S16
91                                                            : AV_SAMPLE_FMT_S32;
92     avctx->bits_per_raw_sample = avctx->bits_per_coded_sample;
93
94     /* get the sample rate */
95     avctx->sample_rate = frequencies[header[1] >> 4 & 3];
96
97     /* get the number of channels */
98     avctx->channels = 1 + (header[1] & 7);
99     /* calculate the bitrate */
100     avctx->bit_rate = avctx->channels *
101                       avctx->sample_rate *
102                       avctx->bits_per_coded_sample;
103
104     /* 4 samples form a group in 20/24bit PCM on DVD Video.
105      * A block is formed by the number of groups that are
106      * needed to complete a set of samples for each channel. */
107     if (avctx->bits_per_coded_sample == 16) {
108         s->samples_per_block = 1;
109         s->block_size        = avctx->channels * 2;
110     } else {
111         switch (avctx->channels) {
112         case 1:
113         case 2:
114         case 4:
115             /* one group has all the samples needed */
116             s->block_size        = 4 * avctx->bits_per_coded_sample / 8;
117             s->samples_per_block = 4 / avctx->channels;
118             s->groups_per_block  = 1;
119             break;
120         case 8:
121             /* two groups have all the samples needed */
122             s->block_size        = 8 * avctx->bits_per_coded_sample / 8;
123             s->samples_per_block = 1;
124             s->groups_per_block  = 2;
125             break;
126         default:
127             /* need avctx->channels groups */
128             s->block_size        = 4 * avctx->channels *
129                                    avctx->bits_per_coded_sample / 8;
130             s->samples_per_block = 4;
131             s->groups_per_block  = avctx->channels;
132             break;
133         }
134     }
135
136     if (avctx->debug & FF_DEBUG_PICT_INFO)
137         av_dlog(avctx,
138                 "pcm_dvd_parse_header: %d channels, %d bits per sample, %d Hz, %d bit/s\n",
139                 avctx->channels, avctx->bits_per_coded_sample,
140                 avctx->sample_rate, avctx->bit_rate);
141
142     s->last_header = header_int;
143
144     return 0;
145 }
146
147 static void *pcm_dvd_decode_samples(AVCodecContext *avctx, const uint8_t *src,
148                                     void *dst, int blocks)
149 {
150     PCMDVDContext *s = avctx->priv_data;
151     int16_t *dst16   = dst;
152     int32_t *dst32   = dst;
153     GetByteContext gb;
154     int i;
155     uint8_t t;
156     int samples;
157
158     bytestream2_init(&gb, src, blocks * s->block_size);
159     switch (avctx->bits_per_coded_sample) {
160     case 16:
161 #if HAVE_BIGENDIAN
162         bytestream2_get_buffer(&gb, dst16, blocks * s->block_size);
163         dst16 += blocks * s->block_size / 2;
164 #else
165         samples = blocks * avctx->channels;
166         do {
167             *dst16++ = bytestream2_get_be16u(&gb);
168         } while (--samples);
169 #endif
170         return dst16;
171     case 20:
172         do {
173             for (i = s->groups_per_block; i; i--) {
174                 dst32[0] = bytestream2_get_be16u(&gb) << 16;
175                 dst32[1] = bytestream2_get_be16u(&gb) << 16;
176                 dst32[2] = bytestream2_get_be16u(&gb) << 16;
177                 dst32[3] = bytestream2_get_be16u(&gb) << 16;
178                 t = bytestream2_get_byteu(&gb);
179                 *dst32 += (t & 0xf0) << 8;
180                 *dst32 += (t & 0x0f) << 12;
181                 t = bytestream2_get_byteu(&gb);
182                 *dst32 += (t & 0xf0) << 8;
183                 *dst32 += (t & 0x0f) << 12;
184             }
185         } while (--blocks);
186         return dst32;
187     case 24:
188         do {
189             for (i = s->groups_per_block; i; i--) {
190                 dst32[0] = bytestream2_get_be16u(&gb) << 16;
191                 dst32[1] = bytestream2_get_be16u(&gb) << 16;
192                 dst32[2] = bytestream2_get_be16u(&gb) << 16;
193                 dst32[3] = bytestream2_get_be16u(&gb) << 16;
194                 *dst32++ += bytestream2_get_byteu(&gb) << 8;
195                 *dst32++ += bytestream2_get_byteu(&gb) << 8;
196                 *dst32++ += bytestream2_get_byteu(&gb) << 8;
197                 *dst32++ += bytestream2_get_byteu(&gb) << 8;
198             }
199         } while (--blocks);
200         return dst32;
201     default:
202         return NULL;
203     }
204 }
205
206 static int pcm_dvd_decode_frame(AVCodecContext *avctx, void *data,
207                                 int *got_frame_ptr, AVPacket *avpkt)
208 {
209     AVFrame *frame     = data;
210     const uint8_t *src = avpkt->data;
211     int buf_size       = avpkt->size;
212     PCMDVDContext *s   = avctx->priv_data;
213     int retval;
214     int blocks;
215     void *dst;
216
217     if (buf_size < 3) {
218         av_log(avctx, AV_LOG_ERROR, "PCM packet too small\n");
219         return AVERROR_INVALIDDATA;
220     }
221
222     if ((retval = pcm_dvd_parse_header(avctx, src)))
223         return retval;
224     src      += 3;
225     buf_size -= 3;
226
227     blocks = (buf_size + s->extra_sample_count) / s->block_size;
228
229     /* get output buffer */
230     frame->nb_samples = blocks * s->samples_per_block;
231     if ((retval = ff_get_buffer(avctx, frame, 0)) < 0) {
232         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
233         return retval;
234     }
235     dst = frame->data[0];
236
237     /* consume leftover samples from last packet */
238     if (s->extra_sample_count) {
239         int missing_samples = s->block_size - s->extra_sample_count;
240         if (buf_size >= missing_samples) {
241             memcpy(s->extra_samples + s->extra_sample_count, src,
242                    missing_samples);
243             dst = pcm_dvd_decode_samples(avctx, s->extra_samples, dst, 1);
244             src += missing_samples;
245             buf_size -= missing_samples;
246             s->extra_sample_count = 0;
247             blocks--;
248         } else {
249             /* new packet still doesn't have enough samples */
250             memcpy(s->extra_samples + s->extra_sample_count, src, buf_size);
251             s->extra_sample_count += buf_size;
252             return avpkt->size;
253         }
254     }
255
256     /* decode remaining complete samples */
257     if (blocks) {
258         pcm_dvd_decode_samples(avctx, src, dst, blocks);
259         buf_size -= blocks * s->block_size;
260     }
261
262     /* store leftover samples */
263     if (buf_size) {
264         src += blocks * s->block_size;
265         memcpy(s->extra_samples, src, buf_size);
266         s->extra_sample_count = buf_size;
267     }
268
269     *got_frame_ptr = 1;
270
271     return avpkt->size;
272 }
273
274 AVCodec ff_pcm_dvd_decoder = {
275     .name           = "pcm_dvd",
276     .long_name      = NULL_IF_CONFIG_SMALL("PCM signed 16|20|24-bit big-endian for DVD media"),
277     .type           = AVMEDIA_TYPE_AUDIO,
278     .id             = AV_CODEC_ID_PCM_DVD,
279     .priv_data_size = sizeof(PCMDVDContext),
280     .init           = pcm_dvd_decode_init,
281     .decode         = pcm_dvd_decode_frame,
282     .close          = pcm_dvd_decode_uninit,
283     .capabilities   = CODEC_CAP_DR1,
284     .sample_fmts    = (const enum AVSampleFormat[]) {
285         AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_NONE
286     }
287 };