]> git.sesse.net Git - ffmpeg/blob - libavcodec/pcm.c
fraps: Make the input buffer size checks more strict
[ffmpeg] / libavcodec / pcm.c
1 /*
2  * PCM codecs
3  * Copyright (c) 2001 Fabrice Bellard
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  * PCM codecs
25  */
26
27 #include "libavutil/attributes.h"
28 #include "avcodec.h"
29 #include "bytestream.h"
30 #include "internal.h"
31 #include "mathops.h"
32 #include "pcm_tablegen.h"
33
34 static av_cold int pcm_encode_init(AVCodecContext *avctx)
35 {
36     avctx->frame_size = 0;
37     switch (avctx->codec->id) {
38     case AV_CODEC_ID_PCM_ALAW:
39         pcm_alaw_tableinit();
40         break;
41     case AV_CODEC_ID_PCM_MULAW:
42         pcm_ulaw_tableinit();
43         break;
44     default:
45         break;
46     }
47
48     avctx->bits_per_coded_sample = av_get_bits_per_sample(avctx->codec->id);
49     avctx->block_align           = avctx->channels * avctx->bits_per_coded_sample / 8;
50     avctx->bit_rate              = avctx->block_align * avctx->sample_rate * 8;
51     avctx->coded_frame           = avcodec_alloc_frame();
52     if (!avctx->coded_frame)
53         return AVERROR(ENOMEM);
54
55     return 0;
56 }
57
58 static av_cold int pcm_encode_close(AVCodecContext *avctx)
59 {
60     av_freep(&avctx->coded_frame);
61
62     return 0;
63 }
64
65 /**
66  * Write PCM samples macro
67  * @param type   Datatype of native machine format
68  * @param endian bytestream_put_xxx() suffix
69  * @param src    Source pointer (variable name)
70  * @param dst    Destination pointer (variable name)
71  * @param n      Total number of samples (variable name)
72  * @param shift  Bitshift (bits)
73  * @param offset Sample value offset
74  */
75 #define ENCODE(type, endian, src, dst, n, shift, offset)                \
76     samples_ ## type = (const type *) src;                              \
77     for (; n > 0; n--) {                                                \
78         register type v = (*samples_ ## type++ >> shift) + offset;      \
79         bytestream_put_ ## endian(&dst, v);                             \
80     }
81
82 static int pcm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
83                             const AVFrame *frame, int *got_packet_ptr)
84 {
85     int n, sample_size, v, ret;
86     const short *samples;
87     unsigned char *dst;
88     const uint8_t *srcu8;
89     const int16_t *samples_int16_t;
90     const int32_t *samples_int32_t;
91     const int64_t *samples_int64_t;
92     const uint16_t *samples_uint16_t;
93     const uint32_t *samples_uint32_t;
94
95     sample_size = av_get_bits_per_sample(avctx->codec->id) / 8;
96     n           = frame->nb_samples * avctx->channels;
97     samples     = (const short *)frame->data[0];
98
99     if ((ret = ff_alloc_packet(avpkt, n * sample_size))) {
100         av_log(avctx, AV_LOG_ERROR, "Error getting output packet\n");
101         return ret;
102     }
103     dst = avpkt->data;
104
105     switch (avctx->codec->id) {
106     case AV_CODEC_ID_PCM_U32LE:
107         ENCODE(uint32_t, le32, samples, dst, n, 0, 0x80000000)
108         break;
109     case AV_CODEC_ID_PCM_U32BE:
110         ENCODE(uint32_t, be32, samples, dst, n, 0, 0x80000000)
111         break;
112     case AV_CODEC_ID_PCM_S24LE:
113         ENCODE(int32_t, le24, samples, dst, n, 8, 0)
114         break;
115     case AV_CODEC_ID_PCM_S24BE:
116         ENCODE(int32_t, be24, samples, dst, n, 8, 0)
117         break;
118     case AV_CODEC_ID_PCM_U24LE:
119         ENCODE(uint32_t, le24, samples, dst, n, 8, 0x800000)
120         break;
121     case AV_CODEC_ID_PCM_U24BE:
122         ENCODE(uint32_t, be24, samples, dst, n, 8, 0x800000)
123         break;
124     case AV_CODEC_ID_PCM_S24DAUD:
125         for (; n > 0; n--) {
126             uint32_t tmp = ff_reverse[(*samples >> 8) & 0xff] +
127                            (ff_reverse[*samples & 0xff] << 8);
128             tmp <<= 4; // sync flags would go here
129             bytestream_put_be24(&dst, tmp);
130             samples++;
131         }
132         break;
133     case AV_CODEC_ID_PCM_U16LE:
134         ENCODE(uint16_t, le16, samples, dst, n, 0, 0x8000)
135         break;
136     case AV_CODEC_ID_PCM_U16BE:
137         ENCODE(uint16_t, be16, samples, dst, n, 0, 0x8000)
138         break;
139     case AV_CODEC_ID_PCM_S8:
140         srcu8 = frame->data[0];
141         for (; n > 0; n--) {
142             v      = *srcu8++;
143             *dst++ = v - 128;
144         }
145         break;
146 #if HAVE_BIGENDIAN
147     case AV_CODEC_ID_PCM_F64LE:
148         ENCODE(int64_t, le64, samples, dst, n, 0, 0)
149         break;
150     case AV_CODEC_ID_PCM_S32LE:
151     case AV_CODEC_ID_PCM_F32LE:
152         ENCODE(int32_t, le32, samples, dst, n, 0, 0)
153         break;
154     case AV_CODEC_ID_PCM_S16LE:
155         ENCODE(int16_t, le16, samples, dst, n, 0, 0)
156         break;
157     case AV_CODEC_ID_PCM_F64BE:
158     case AV_CODEC_ID_PCM_F32BE:
159     case AV_CODEC_ID_PCM_S32BE:
160     case AV_CODEC_ID_PCM_S16BE:
161 #else
162     case AV_CODEC_ID_PCM_F64BE:
163         ENCODE(int64_t, be64, samples, dst, n, 0, 0)
164         break;
165     case AV_CODEC_ID_PCM_F32BE:
166     case AV_CODEC_ID_PCM_S32BE:
167         ENCODE(int32_t, be32, samples, dst, n, 0, 0)
168         break;
169     case AV_CODEC_ID_PCM_S16BE:
170         ENCODE(int16_t, be16, samples, dst, n, 0, 0)
171         break;
172     case AV_CODEC_ID_PCM_F64LE:
173     case AV_CODEC_ID_PCM_F32LE:
174     case AV_CODEC_ID_PCM_S32LE:
175     case AV_CODEC_ID_PCM_S16LE:
176 #endif /* HAVE_BIGENDIAN */
177     case AV_CODEC_ID_PCM_U8:
178         memcpy(dst, samples, n * sample_size);
179         dst += n * sample_size;
180         break;
181     case AV_CODEC_ID_PCM_ALAW:
182         for (; n > 0; n--) {
183             v      = *samples++;
184             *dst++ = linear_to_alaw[(v + 32768) >> 2];
185         }
186         break;
187     case AV_CODEC_ID_PCM_MULAW:
188         for (; n > 0; n--) {
189             v      = *samples++;
190             *dst++ = linear_to_ulaw[(v + 32768) >> 2];
191         }
192         break;
193     default:
194         return -1;
195     }
196
197     *got_packet_ptr = 1;
198     return 0;
199 }
200
201 typedef struct PCMDecode {
202     short   table[256];
203 } PCMDecode;
204
205 static av_cold int pcm_decode_init(AVCodecContext *avctx)
206 {
207     PCMDecode *s = avctx->priv_data;
208     int i;
209
210     if (avctx->channels <= 0) {
211         av_log(avctx, AV_LOG_ERROR, "PCM channels out of bounds\n");
212         return AVERROR(EINVAL);
213     }
214
215     switch (avctx->codec->id) {
216     case AV_CODEC_ID_PCM_ALAW:
217         for (i = 0; i < 256; i++)
218             s->table[i] = alaw2linear(i);
219         break;
220     case AV_CODEC_ID_PCM_MULAW:
221         for (i = 0; i < 256; i++)
222             s->table[i] = ulaw2linear(i);
223         break;
224     default:
225         break;
226     }
227
228     avctx->sample_fmt = avctx->codec->sample_fmts[0];
229
230     if (avctx->sample_fmt == AV_SAMPLE_FMT_S32)
231         avctx->bits_per_raw_sample = av_get_bits_per_sample(avctx->codec->id);
232
233     return 0;
234 }
235
236 /**
237  * Read PCM samples macro
238  * @param size   Data size of native machine format
239  * @param endian bytestream_get_xxx() endian suffix
240  * @param src    Source pointer (variable name)
241  * @param dst    Destination pointer (variable name)
242  * @param n      Total number of samples (variable name)
243  * @param shift  Bitshift (bits)
244  * @param offset Sample value offset
245  */
246 #define DECODE(size, endian, src, dst, n, shift, offset)                \
247     for (; n > 0; n--) {                                                \
248         uint ## size ## _t v = bytestream_get_ ## endian(&src);         \
249         AV_WN ## size ## A(dst, (v - offset) << shift);                 \
250         dst += size / 8;                                                \
251     }
252
253 static int pcm_decode_frame(AVCodecContext *avctx, void *data,
254                             int *got_frame_ptr, AVPacket *avpkt)
255 {
256     const uint8_t *src = avpkt->data;
257     int buf_size       = avpkt->size;
258     PCMDecode *s       = avctx->priv_data;
259     AVFrame *frame     = data;
260     int sample_size, c, n, ret, samples_per_block;
261     uint8_t *samples;
262     int32_t *dst_int32_t;
263
264     sample_size = av_get_bits_per_sample(avctx->codec_id) / 8;
265
266     /* av_get_bits_per_sample returns 0 for AV_CODEC_ID_PCM_DVD */
267     samples_per_block = 1;
268     if (avctx->codec_id == AV_CODEC_ID_PCM_LXF) {
269         /* we process 40-bit blocks per channel for LXF */
270         samples_per_block = 2;
271         sample_size       = 5;
272     }
273
274     if (sample_size == 0) {
275         av_log(avctx, AV_LOG_ERROR, "Invalid sample_size\n");
276         return AVERROR(EINVAL);
277     }
278
279     n = avctx->channels * sample_size;
280
281     if (n && buf_size % n) {
282         if (buf_size < n) {
283             av_log(avctx, AV_LOG_ERROR, "invalid PCM packet\n");
284             return -1;
285         } else
286             buf_size -= buf_size % n;
287     }
288
289     n = buf_size / sample_size;
290
291     /* get output buffer */
292     frame->nb_samples = n * samples_per_block / avctx->channels;
293     if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
294         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
295         return ret;
296     }
297     samples = frame->data[0];
298
299     switch (avctx->codec->id) {
300     case AV_CODEC_ID_PCM_U32LE:
301         DECODE(32, le32, src, samples, n, 0, 0x80000000)
302         break;
303     case AV_CODEC_ID_PCM_U32BE:
304         DECODE(32, be32, src, samples, n, 0, 0x80000000)
305         break;
306     case AV_CODEC_ID_PCM_S24LE:
307         DECODE(32, le24, src, samples, n, 8, 0)
308         break;
309     case AV_CODEC_ID_PCM_S24BE:
310         DECODE(32, be24, src, samples, n, 8, 0)
311         break;
312     case AV_CODEC_ID_PCM_U24LE:
313         DECODE(32, le24, src, samples, n, 8, 0x800000)
314         break;
315     case AV_CODEC_ID_PCM_U24BE:
316         DECODE(32, be24, src, samples, n, 8, 0x800000)
317         break;
318     case AV_CODEC_ID_PCM_S24DAUD:
319         for (; n > 0; n--) {
320             uint32_t v = bytestream_get_be24(&src);
321             v >>= 4; // sync flags are here
322             AV_WN16A(samples, ff_reverse[(v >> 8) & 0xff] +
323                              (ff_reverse[v        & 0xff] << 8));
324             samples += 2;
325         }
326         break;
327     case AV_CODEC_ID_PCM_S16LE_PLANAR:
328     {
329         int av_unused n2;
330         n /= avctx->channels;
331         for (c = 0; c < avctx->channels; c++) {
332             samples = frame->extended_data[c];
333 #if HAVE_BIGENDIAN
334             n2 = n;
335             DECODE(16, le16, src, samples, n2, 0, 0)
336 #else
337             memcpy(samples, src, n * 2);
338             src += n * 2;
339 #endif
340         }
341         break;
342     }
343     case AV_CODEC_ID_PCM_U16LE:
344         DECODE(16, le16, src, samples, n, 0, 0x8000)
345         break;
346     case AV_CODEC_ID_PCM_U16BE:
347         DECODE(16, be16, src, samples, n, 0, 0x8000)
348         break;
349     case AV_CODEC_ID_PCM_S8:
350         for (; n > 0; n--)
351             *samples++ = *src++ + 128;
352         break;
353 #if HAVE_BIGENDIAN
354     case AV_CODEC_ID_PCM_F64LE:
355         DECODE(64, le64, src, samples, n, 0, 0)
356         break;
357     case AV_CODEC_ID_PCM_S32LE:
358     case AV_CODEC_ID_PCM_F32LE:
359         DECODE(32, le32, src, samples, n, 0, 0)
360         break;
361     case AV_CODEC_ID_PCM_S16LE:
362         DECODE(16, le16, src, samples, n, 0, 0)
363         break;
364     case AV_CODEC_ID_PCM_F64BE:
365     case AV_CODEC_ID_PCM_F32BE:
366     case AV_CODEC_ID_PCM_S32BE:
367     case AV_CODEC_ID_PCM_S16BE:
368 #else
369     case AV_CODEC_ID_PCM_F64BE:
370         DECODE(64, be64, src, samples, n, 0, 0)
371         break;
372     case AV_CODEC_ID_PCM_F32BE:
373     case AV_CODEC_ID_PCM_S32BE:
374         DECODE(32, be32, src, samples, n, 0, 0)
375         break;
376     case AV_CODEC_ID_PCM_S16BE:
377         DECODE(16, be16, src, samples, n, 0, 0)
378         break;
379     case AV_CODEC_ID_PCM_F64LE:
380     case AV_CODEC_ID_PCM_F32LE:
381     case AV_CODEC_ID_PCM_S32LE:
382     case AV_CODEC_ID_PCM_S16LE:
383 #endif /* HAVE_BIGENDIAN */
384     case AV_CODEC_ID_PCM_U8:
385         memcpy(samples, src, n * sample_size);
386         break;
387     case AV_CODEC_ID_PCM_ZORK:
388         for (; n > 0; n--) {
389             int v = *src++;
390             if (v < 128)
391                 v = 128 - v;
392             *samples++ = v;
393         }
394         break;
395     case AV_CODEC_ID_PCM_ALAW:
396     case AV_CODEC_ID_PCM_MULAW:
397         for (; n > 0; n--) {
398             AV_WN16A(samples, s->table[*src++]);
399             samples += 2;
400         }
401         break;
402     case AV_CODEC_ID_PCM_LXF:
403     {
404         int i;
405         n /= avctx->channels;
406         for (c = 0; c < avctx->channels; c++) {
407             dst_int32_t = (int32_t *)frame->extended_data[c];
408             for (i = 0; i < n; i++) {
409                 // extract low 20 bits and expand to 32 bits
410                 *dst_int32_t++ =  (src[2]         << 28) |
411                                   (src[1]         << 20) |
412                                   (src[0]         << 12) |
413                                  ((src[2] & 0x0F) <<  8) |
414                                    src[1];
415                 // extract high 20 bits and expand to 32 bits
416                 *dst_int32_t++ =  (src[4]         << 24) |
417                                   (src[3]         << 16) |
418                                  ((src[2] & 0xF0) <<  8) |
419                                   (src[4]         <<  4) |
420                                   (src[3]         >>  4);
421                 src += 5;
422             }
423         }
424         break;
425     }
426     default:
427         return -1;
428     }
429
430     *got_frame_ptr = 1;
431
432     return buf_size;
433 }
434
435 #define PCM_ENCODER_0(id_, sample_fmt_, name_, long_name_)
436 #define PCM_ENCODER_1(id_, sample_fmt_, name_, long_name_)                  \
437 AVCodec ff_ ## name_ ## _encoder = {                                        \
438     .name         = #name_,                                                 \
439     .type         = AVMEDIA_TYPE_AUDIO,                                     \
440     .id           = AV_CODEC_ID_ ## id_,                                    \
441     .init         = pcm_encode_init,                                        \
442     .encode2      = pcm_encode_frame,                                       \
443     .close        = pcm_encode_close,                                       \
444     .capabilities = CODEC_CAP_VARIABLE_FRAME_SIZE,                          \
445     .sample_fmts  = (const enum AVSampleFormat[]){ sample_fmt_,             \
446                                                    AV_SAMPLE_FMT_NONE },    \
447     .long_name    = NULL_IF_CONFIG_SMALL(long_name_),                       \
448 }
449
450 #define PCM_ENCODER_2(cf, id, sample_fmt, name, long_name)                  \
451     PCM_ENCODER_ ## cf(id, sample_fmt, name, long_name)
452 #define PCM_ENCODER_3(cf, id, sample_fmt, name, long_name)                  \
453     PCM_ENCODER_2(cf, id, sample_fmt, name, long_name)
454 #define PCM_ENCODER(id, sample_fmt, name, long_name)                        \
455     PCM_ENCODER_3(CONFIG_ ## id ## _ENCODER, id, sample_fmt, name, long_name)
456
457 #define PCM_DECODER_0(id, sample_fmt, name, long_name)
458 #define PCM_DECODER_1(id_, sample_fmt_, name_, long_name_)                  \
459 AVCodec ff_ ## name_ ## _decoder = {                                        \
460     .name           = #name_,                                               \
461     .type           = AVMEDIA_TYPE_AUDIO,                                   \
462     .id             = AV_CODEC_ID_ ## id_,                                  \
463     .priv_data_size = sizeof(PCMDecode),                                    \
464     .init           = pcm_decode_init,                                      \
465     .decode         = pcm_decode_frame,                                     \
466     .capabilities   = CODEC_CAP_DR1,                                        \
467     .sample_fmts    = (const enum AVSampleFormat[]){ sample_fmt_,           \
468                                                      AV_SAMPLE_FMT_NONE },  \
469     .long_name      = NULL_IF_CONFIG_SMALL(long_name_),                     \
470 }
471
472 #define PCM_DECODER_2(cf, id, sample_fmt, name, long_name)                  \
473     PCM_DECODER_ ## cf(id, sample_fmt, name, long_name)
474 #define PCM_DECODER_3(cf, id, sample_fmt, name, long_name)                  \
475     PCM_DECODER_2(cf, id, sample_fmt, name, long_name)
476 #define PCM_DECODER(id, sample_fmt, name, long_name)                        \
477     PCM_DECODER_3(CONFIG_ ## id ## _DECODER, id, sample_fmt, name, long_name)
478
479 #define PCM_CODEC(id, sample_fmt_, name, long_name_)                    \
480     PCM_ENCODER(id, sample_fmt_, name, long_name_);                     \
481     PCM_DECODER(id, sample_fmt_, name, long_name_)
482
483 /* Note: Do not forget to add new entries to the Makefile as well. */
484 PCM_CODEC  (PCM_ALAW,         AV_SAMPLE_FMT_S16, pcm_alaw,         "PCM A-law");
485 PCM_CODEC  (PCM_F32BE,        AV_SAMPLE_FMT_FLT, pcm_f32be,        "PCM 32-bit floating point big-endian");
486 PCM_CODEC  (PCM_F32LE,        AV_SAMPLE_FMT_FLT, pcm_f32le,        "PCM 32-bit floating point little-endian");
487 PCM_CODEC  (PCM_F64BE,        AV_SAMPLE_FMT_DBL, pcm_f64be,        "PCM 64-bit floating point big-endian");
488 PCM_CODEC  (PCM_F64LE,        AV_SAMPLE_FMT_DBL, pcm_f64le,        "PCM 64-bit floating point little-endian");
489 PCM_DECODER(PCM_LXF,          AV_SAMPLE_FMT_S32P, pcm_lxf,          "PCM signed 20-bit little-endian planar");
490 PCM_CODEC  (PCM_MULAW,        AV_SAMPLE_FMT_S16, pcm_mulaw,        "PCM mu-law");
491 PCM_CODEC  (PCM_S8,           AV_SAMPLE_FMT_U8,  pcm_s8,           "PCM signed 8-bit");
492 PCM_CODEC  (PCM_S16BE,        AV_SAMPLE_FMT_S16, pcm_s16be,        "PCM signed 16-bit big-endian");
493 PCM_CODEC  (PCM_S16LE,        AV_SAMPLE_FMT_S16, pcm_s16le,        "PCM signed 16-bit little-endian");
494 PCM_DECODER(PCM_S16LE_PLANAR, AV_SAMPLE_FMT_S16P, pcm_s16le_planar, "PCM 16-bit little-endian planar");
495 PCM_CODEC  (PCM_S24BE,        AV_SAMPLE_FMT_S32, pcm_s24be,        "PCM signed 24-bit big-endian");
496 PCM_CODEC  (PCM_S24DAUD,      AV_SAMPLE_FMT_S16, pcm_s24daud,      "PCM D-Cinema audio signed 24-bit");
497 PCM_CODEC  (PCM_S24LE,        AV_SAMPLE_FMT_S32, pcm_s24le,        "PCM signed 24-bit little-endian");
498 PCM_CODEC  (PCM_S32BE,        AV_SAMPLE_FMT_S32, pcm_s32be,        "PCM signed 32-bit big-endian");
499 PCM_CODEC  (PCM_S32LE,        AV_SAMPLE_FMT_S32, pcm_s32le,        "PCM signed 32-bit little-endian");
500 PCM_CODEC  (PCM_U8,           AV_SAMPLE_FMT_U8,  pcm_u8,           "PCM unsigned 8-bit");
501 PCM_CODEC  (PCM_U16BE,        AV_SAMPLE_FMT_S16, pcm_u16be,        "PCM unsigned 16-bit big-endian");
502 PCM_CODEC  (PCM_U16LE,        AV_SAMPLE_FMT_S16, pcm_u16le,        "PCM unsigned 16-bit little-endian");
503 PCM_CODEC  (PCM_U24BE,        AV_SAMPLE_FMT_S32, pcm_u24be,        "PCM unsigned 24-bit big-endian");
504 PCM_CODEC  (PCM_U24LE,        AV_SAMPLE_FMT_S32, pcm_u24le,        "PCM unsigned 24-bit little-endian");
505 PCM_CODEC  (PCM_U32BE,        AV_SAMPLE_FMT_S32, pcm_u32be,        "PCM unsigned 32-bit big-endian");
506 PCM_CODEC  (PCM_U32LE,        AV_SAMPLE_FMT_S32, pcm_u32le,        "PCM unsigned 32-bit little-endian");
507 PCM_DECODER(PCM_ZORK,         AV_SAMPLE_FMT_U8,  pcm_zork,         "PCM Zork");