Browse code

Merge commit 'a42e3a6700547e4e49445bda81d3a89ec3e081a9'

* commit 'a42e3a6700547e4e49445bda81d3a89ec3e081a9':
pcm_dvd: consolidate pieces from pcm.c and mpeg.c

Conflicts:
libavcodec/pcm.c
libavcodec/version.h
libavformat/mpeg.c

Merged-by: Michael Niedermayer <michaelni@gmx.at>

Michael Niedermayer authored on 2013/09/01 20:00:08
Showing 5 changed files
... ...
@@ -522,7 +522,7 @@ OBJS-$(CONFIG_ZMBV_ENCODER)            += zmbvenc.o
522 522
 OBJS-$(CONFIG_PCM_ALAW_DECODER)           += pcm.o
523 523
 OBJS-$(CONFIG_PCM_ALAW_ENCODER)           += pcm.o
524 524
 OBJS-$(CONFIG_PCM_BLURAY_DECODER)         += pcm-bluray.o
525
-OBJS-$(CONFIG_PCM_DVD_DECODER)            += pcm.o
525
+OBJS-$(CONFIG_PCM_DVD_DECODER)            += pcm-dvd.o
526 526
 OBJS-$(CONFIG_PCM_F32BE_DECODER)          += pcm.o
527 527
 OBJS-$(CONFIG_PCM_F32BE_ENCODER)          += pcm.o
528 528
 OBJS-$(CONFIG_PCM_F32LE_DECODER)          += pcm.o
529 529
new file mode 100644
... ...
@@ -0,0 +1,289 @@
0
+/*
1
+ * LPCM codecs for PCM formats found in Video DVD streams
2
+ * Copyright (c) 2009-2013 Christian Schmidt
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
+/**
22
+ * @file
23
+ * LPCM codecs for PCM formats found in Video DVD streams
24
+ */
25
+
26
+#include "avcodec.h"
27
+#include "bytestream.h"
28
+#include "internal.h"
29
+
30
+typedef struct PCMDVDContext {
31
+    uint32_t last_header;    // Cached header to see if parsing is needed
32
+    int block_size;          // Size of a block of samples in bytes
33
+    int samples_per_block;   // Number of samples per channel per block
34
+    int groups_per_block;    // Number of 20/24bit sample groups per block
35
+    uint8_t *extra_samples;  // Pointer to leftover samples from a frame
36
+    int extra_sample_count;  // Number of leftover samples in the buffer
37
+} PCMDVDContext;
38
+
39
+static av_cold int pcm_dvd_decode_init(AVCodecContext *avctx)
40
+{
41
+    PCMDVDContext *s = avctx->priv_data;
42
+
43
+    /* Invalid header to force parsing of the first header */
44
+    s->last_header = -1;
45
+    /* reserve space for 8 channels, 3 bytes/sample, 4 samples/block */
46
+    if (!(s->extra_samples = av_malloc(8 * 3 * 4)))
47
+        return AVERROR(ENOMEM);
48
+    s->extra_sample_count = 0;
49
+
50
+    return 0;
51
+}
52
+
53
+static av_cold int pcm_dvd_decode_uninit(AVCodecContext *avctx)
54
+{
55
+    PCMDVDContext *s = avctx->priv_data;
56
+
57
+    if (s->extra_samples)
58
+        av_free(s->extra_samples);
59
+
60
+    return 0;
61
+}
62
+
63
+static int pcm_dvd_parse_header(AVCodecContext *avctx, const uint8_t *header)
64
+{
65
+    /* no traces of 44100 and 32000Hz in any commercial software or player */
66
+    static const uint32_t frequencies[4] = { 48000, 96000, 44100, 32000 };
67
+    PCMDVDContext *s = avctx->priv_data;
68
+    int header_int = (header[0] & 0xe0) | (header[1] << 8) | (header[2] << 16);
69
+
70
+    /* early exit if the header didn't change apart from the frame number */
71
+    if (s->last_header == header_int)
72
+        return 0;
73
+
74
+    if (avctx->debug & FF_DEBUG_PICT_INFO)
75
+        av_dlog(avctx, "pcm_dvd_parse_header: header = %02x%02x%02x\n",
76
+                header[0], header[1], header[2]);
77
+    /*
78
+     * header[0] emphasis (1), muse(1), reserved(1), frame number(5)
79
+     * header[1] quant (2), freq(2), reserved(1), channels(3)
80
+     * header[2] dynamic range control (0x80 = off)
81
+     */
82
+
83
+    /* get the sample depth and derive the sample format from it */
84
+    avctx->bits_per_coded_sample = 16 + (header[1] >> 6 & 3) * 4;
85
+    if (avctx->bits_per_coded_sample == 28) {
86
+        av_log(avctx, AV_LOG_ERROR,
87
+               "PCM DVD unsupported sample depth %i\n",
88
+               avctx->bits_per_coded_sample);
89
+        return AVERROR_INVALIDDATA;
90
+    }
91
+    avctx->sample_fmt = avctx->bits_per_coded_sample == 16 ? AV_SAMPLE_FMT_S16
92
+                                                           : AV_SAMPLE_FMT_S32;
93
+    avctx->bits_per_raw_sample = avctx->bits_per_coded_sample;
94
+
95
+    /* get the sample rate */
96
+    avctx->sample_rate = frequencies[header[1] >> 4 & 3];
97
+
98
+    /* get the number of channels */
99
+    avctx->channels = 1 + (header[1] & 7);
100
+    /* calculate the bitrate */
101
+    avctx->bit_rate = avctx->channels *
102
+                      avctx->sample_rate *
103
+                      avctx->bits_per_coded_sample;
104
+
105
+    /* 4 samples form a group in 20/24bit PCM on DVD Video.
106
+     * A block is formed by the number of groups that are
107
+     * needed to complete a set of samples for each channel. */
108
+    if (avctx->bits_per_coded_sample == 16) {
109
+        s->samples_per_block = 1;
110
+        s->block_size        = avctx->channels * 2;
111
+    } else {
112
+        switch (avctx->channels) {
113
+        case 1:
114
+        case 2:
115
+        case 4:
116
+            /* one group has all the samples needed */
117
+            s->block_size        = 4 * avctx->bits_per_coded_sample / 8;
118
+            s->samples_per_block = 4 / avctx->channels;
119
+            s->groups_per_block  = 1;
120
+            break;
121
+        case 8:
122
+            /* two groups have all the samples needed */
123
+            s->block_size        = 8 * avctx->bits_per_coded_sample / 8;
124
+            s->samples_per_block = 1;
125
+            s->groups_per_block  = 2;
126
+            break;
127
+        default:
128
+            /* need avctx->channels groups */
129
+            s->block_size        = 4 * avctx->channels *
130
+                                   avctx->bits_per_coded_sample / 8;
131
+            s->samples_per_block = 4;
132
+            s->groups_per_block  = avctx->channels;
133
+            break;
134
+        }
135
+    }
136
+
137
+    if (avctx->debug & FF_DEBUG_PICT_INFO)
138
+        av_dlog(avctx,
139
+                "pcm_dvd_parse_header: %d channels, %d bits per sample, %d Hz, %d bit/s\n",
140
+                avctx->channels, avctx->bits_per_coded_sample,
141
+                avctx->sample_rate, avctx->bit_rate);
142
+
143
+    s->last_header = header_int;
144
+
145
+    return 0;
146
+}
147
+
148
+static void *pcm_dvd_decode_samples(AVCodecContext *avctx, const uint8_t *src,
149
+                                    void *dst, int blocks)
150
+{
151
+    PCMDVDContext *s = avctx->priv_data;
152
+    int16_t *dst16   = dst;
153
+    int32_t *dst32   = dst;
154
+    GetByteContext gb;
155
+    int i;
156
+    uint8_t t;
157
+    int samples;
158
+
159
+    bytestream2_init(&gb, src, blocks * s->block_size);
160
+    switch (avctx->bits_per_coded_sample) {
161
+    case 16:
162
+#if HAVE_BIGENDIAN
163
+        bytestream2_get_buffer(&gb, dst16, blocks * s->block_size);
164
+        dst16 += blocks * block_size / 2;
165
+#else
166
+        samples = blocks * avctx->channels;
167
+        do {
168
+            *dst16++ = bytestream2_get_be16u(&gb);
169
+        } while (--samples);
170
+#endif
171
+        return dst16;
172
+    case 20:
173
+        do {
174
+            for (i = s->groups_per_block; i; i--) {
175
+                dst32[0] = bytestream2_get_be16u(&gb) << 16;
176
+                dst32[1] = bytestream2_get_be16u(&gb) << 16;
177
+                dst32[2] = bytestream2_get_be16u(&gb) << 16;
178
+                dst32[3] = bytestream2_get_be16u(&gb) << 16;
179
+                t = bytestream2_get_byteu(&gb);
180
+                *dst32 += (t & 0xf0) << 8;
181
+                *dst32 += (t & 0x0f) << 12;
182
+                t = bytestream2_get_byteu(&gb);
183
+                *dst32 += (t & 0xf0) << 8;
184
+                *dst32 += (t & 0x0f) << 12;
185
+            }
186
+        } while (--blocks);
187
+        return dst32;
188
+    case 24:
189
+        do {
190
+            for (i = s->groups_per_block; i; i--) {
191
+                dst32[0] = bytestream2_get_be16u(&gb) << 16;
192
+                dst32[1] = bytestream2_get_be16u(&gb) << 16;
193
+                dst32[2] = bytestream2_get_be16u(&gb) << 16;
194
+                dst32[3] = bytestream2_get_be16u(&gb) << 16;
195
+                *dst32++ += bytestream2_get_byteu(&gb) << 8;
196
+                *dst32++ += bytestream2_get_byteu(&gb) << 8;
197
+                *dst32++ += bytestream2_get_byteu(&gb) << 8;
198
+                *dst32++ += bytestream2_get_byteu(&gb) << 8;
199
+            }
200
+        } while (--blocks);
201
+        return dst32;
202
+    default:
203
+        return NULL;
204
+    }
205
+}
206
+
207
+static int pcm_dvd_decode_frame(AVCodecContext *avctx, void *data,
208
+                                int *got_frame_ptr, AVPacket *avpkt)
209
+{
210
+    AVFrame *frame     = data;
211
+    const uint8_t *src = avpkt->data;
212
+    int buf_size       = avpkt->size;
213
+    PCMDVDContext *s   = avctx->priv_data;
214
+    int retval;
215
+    int blocks;
216
+    void *dst;
217
+
218
+    if (buf_size < 3) {
219
+        av_log(avctx, AV_LOG_ERROR, "PCM packet too small\n");
220
+        return AVERROR_INVALIDDATA;
221
+    }
222
+
223
+    if ((retval = pcm_dvd_parse_header(avctx, src)))
224
+        return retval;
225
+    src      += 3;
226
+    buf_size -= 3;
227
+
228
+    blocks = (buf_size + s->extra_sample_count) / s->block_size;
229
+
230
+    /* get output buffer */
231
+    frame->nb_samples = blocks * s->samples_per_block;
232
+    if ((retval = ff_get_buffer(avctx, frame, 0)) < 0) {
233
+        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
234
+        return retval;
235
+    }
236
+    dst = frame->data[0];
237
+
238
+    /* consume leftover samples from last packet */
239
+    if (s->extra_sample_count) {
240
+        int missing_samples = s->block_size - s->extra_sample_count;
241
+        if (buf_size >= missing_samples) {
242
+            memcpy(s->extra_samples + s->extra_sample_count, src,
243
+                   missing_samples);
244
+            dst = pcm_dvd_decode_samples(avctx, s->extra_samples, dst, 1);
245
+            src += missing_samples;
246
+            buf_size -= missing_samples;
247
+            s->extra_sample_count = 0;
248
+            blocks--;
249
+        } else {
250
+            /* new packet still doesn't have enough samples */
251
+            memcpy(s->extra_samples + s->extra_sample_count, src, buf_size);
252
+            s->extra_sample_count += buf_size;
253
+            return avpkt->size;
254
+        }
255
+    }
256
+
257
+    /* decode remaining complete samples */
258
+    if (blocks) {
259
+        pcm_dvd_decode_samples(avctx, src, dst, blocks);
260
+        buf_size -= blocks * s->block_size;
261
+    }
262
+
263
+    /* store leftover samples */
264
+    if (buf_size) {
265
+        src += blocks * s->block_size;
266
+        memcpy(s->extra_samples, src, buf_size);
267
+        s->extra_sample_count = buf_size;
268
+    }
269
+
270
+    *got_frame_ptr = 1;
271
+
272
+    return avpkt->size;
273
+}
274
+
275
+AVCodec ff_pcm_dvd_decoder = {
276
+    .name           = "pcm_dvd",
277
+    .long_name      = NULL_IF_CONFIG_SMALL("PCM signed 16|20|24-bit big-endian for DVD media"),
278
+    .type           = AVMEDIA_TYPE_AUDIO,
279
+    .id             = AV_CODEC_ID_PCM_DVD,
280
+    .priv_data_size = sizeof(PCMDVDContext),
281
+    .init           = pcm_dvd_decode_init,
282
+    .decode         = pcm_dvd_decode_frame,
283
+    .close          = pcm_dvd_decode_uninit,
284
+    .capabilities   = CODEC_CAP_DR1,
285
+    .sample_fmts    = (const enum AVSampleFormat[]) {
286
+        AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_NONE
287
+    }
288
+};
... ...
@@ -298,18 +298,7 @@ static int pcm_decode_frame(AVCodecContext *avctx, void *data,
298 298
 
299 299
     /* av_get_bits_per_sample returns 0 for AV_CODEC_ID_PCM_DVD */
300 300
     samples_per_block = 1;
301
-    if (avctx->codec_id == AV_CODEC_ID_PCM_DVD) {
302
-        if (avctx->bits_per_coded_sample != 20 &&
303
-            avctx->bits_per_coded_sample != 24) {
304
-            av_log(avctx, AV_LOG_ERROR,
305
-                   "PCM DVD unsupported sample depth %i\n",
306
-                   avctx->bits_per_coded_sample);
307
-            return AVERROR(EINVAL);
308
-        }
309
-        /* 2 samples are interleaved per block in PCM_DVD */
310
-        samples_per_block = 2;
311
-        sample_size       = avctx->bits_per_coded_sample * 2 / 8;
312
-    } else if (avctx->codec_id == AV_CODEC_ID_PCM_LXF) {
301
+    if (avctx->codec_id == AV_CODEC_ID_PCM_LXF) {
313 302
         /* we process 40-bit blocks per channel for LXF */
314 303
         samples_per_block = 2;
315 304
         sample_size       = 5;
... ...
@@ -470,37 +459,6 @@ static int pcm_decode_frame(AVCodecContext *avctx, void *data,
470 470
             samples += 2;
471 471
         }
472 472
         break;
473
-    case AV_CODEC_ID_PCM_DVD:
474
-    {
475
-        const uint8_t *src8;
476
-        dst_int32_t = (int32_t *)frame->data[0];
477
-        n /= avctx->channels;
478
-        switch (avctx->bits_per_coded_sample) {
479
-        case 20:
480
-            while (n--) {
481
-                c    = avctx->channels;
482
-                src8 = src + 4 * c;
483
-                while (c--) {
484
-                    *dst_int32_t++ = (bytestream_get_be16(&src) << 16) + ((*src8   & 0xf0) <<  8);
485
-                    *dst_int32_t++ = (bytestream_get_be16(&src) << 16) + ((*src8++ & 0x0f) << 12);
486
-                }
487
-                src = src8;
488
-            }
489
-            break;
490
-        case 24:
491
-            while (n--) {
492
-                c    = avctx->channels;
493
-                src8 = src + 4 * c;
494
-                while (c--) {
495
-                    *dst_int32_t++ = (bytestream_get_be16(&src) << 16) + ((*src8++) << 8);
496
-                    *dst_int32_t++ = (bytestream_get_be16(&src) << 16) + ((*src8++) << 8);
497
-                }
498
-                src = src8;
499
-            }
500
-            break;
501
-        }
502
-        break;
503
-    }
504 473
     case AV_CODEC_ID_PCM_LXF:
505 474
     {
506 475
         int i;
... ...
@@ -583,7 +541,6 @@ AVCodec ff_ ## name_ ## _decoder = {                                        \
583 583
 
584 584
 /* Note: Do not forget to add new entries to the Makefile as well. */
585 585
 PCM_CODEC  (PCM_ALAW,         AV_SAMPLE_FMT_S16, pcm_alaw,         "PCM A-law / G.711 A-law");
586
-PCM_DECODER(PCM_DVD,          AV_SAMPLE_FMT_S32, pcm_dvd,          "PCM signed 20|24-bit big-endian");
587 586
 PCM_CODEC  (PCM_F32BE,        AV_SAMPLE_FMT_FLT, pcm_f32be,        "PCM 32-bit floating point big-endian");
588 587
 PCM_CODEC  (PCM_F32LE,        AV_SAMPLE_FMT_FLT, pcm_f32le,        "PCM 32-bit floating point little-endian");
589 588
 PCM_CODEC  (PCM_F64BE,        AV_SAMPLE_FMT_DBL, pcm_f64be,        "PCM 64-bit floating point big-endian");
... ...
@@ -29,7 +29,7 @@
29 29
 #include "libavutil/avutil.h"
30 30
 
31 31
 #define LIBAVCODEC_VERSION_MAJOR 55
32
-#define LIBAVCODEC_VERSION_MINOR  29
32
+#define LIBAVCODEC_VERSION_MINOR  30
33 33
 #define LIBAVCODEC_VERSION_MICRO 100
34 34
 
35 35
 #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
... ...
@@ -535,7 +535,6 @@ static int mpegps_read_packet(AVFormatContext *s,
535 535
         if(lpcm_header_len == 6) {
536 536
             codec_id = AV_CODEC_ID_MLP;
537 537
         } else {
538
-            /* 16 bit form will be handled as AV_CODEC_ID_PCM_S16BE */
539 538
             codec_id = AV_CODEC_ID_PCM_DVD;
540 539
         }
541 540
     } else if (startcode >= 0xb0 && startcode <= 0xbf) {
... ...
@@ -570,8 +569,7 @@ static int mpegps_read_packet(AVFormatContext *s,
570 570
         st->codec->sample_rate = 8000;
571 571
     }
572 572
     st->request_probe     = request_probe;
573
-    if (codec_id != AV_CODEC_ID_PCM_S16BE)
574
-        st->need_parsing = AVSTREAM_PARSE_FULL;
573
+    st->need_parsing = AVSTREAM_PARSE_FULL;
575 574
  found:
576 575
     if(st->discard >= AVDISCARD_ALL)
577 576
         goto skip;
... ...
@@ -581,28 +579,6 @@ static int mpegps_read_packet(AVFormatContext *s,
581 581
                 goto skip;
582 582
             avio_skip(s->pb, 6);
583 583
             len -=6;
584
-      } else {
585
-        int b1, freq;
586
-
587
-        /* for LPCM, we just skip the header and consider it is raw
588
-           audio data */
589
-        if (len <= 3)
590
-            goto skip;
591
-        avio_r8(s->pb); /* emphasis (1), muse(1), reserved(1), frame number(5) */
592
-        b1 = avio_r8(s->pb); /* quant (2), freq(2), reserved(1), channels(3) */
593
-        avio_r8(s->pb); /* dynamic range control (0x80 = off) */
594
-        len -= 3;
595
-        freq = (b1 >> 4) & 3;
596
-        st->codec->sample_rate = lpcm_freq_tab[freq];
597
-        st->codec->channels = 1 + (b1 & 7);
598
-        st->codec->bits_per_coded_sample = 16 + ((b1 >> 6) & 3) * 4;
599
-        st->codec->bit_rate = st->codec->channels *
600
-                              st->codec->sample_rate *
601
-                              st->codec->bits_per_coded_sample;
602
-        if (st->codec->bits_per_coded_sample == 16)
603
-            st->codec->codec_id = AV_CODEC_ID_PCM_S16BE;
604
-        else if (st->codec->bits_per_coded_sample == 28)
605
-            return AVERROR(EINVAL);
606 584
       }
607 585
     }
608 586
     ret = av_get_packet(s->pb, pkt, len);