Browse code

dca_parser: parse the sample rate and frame durations

Justin Ruggles authored on 2012/02/28 03:58:01
Showing 4 changed files
... ...
@@ -109,7 +109,8 @@ OBJS-$(CONFIG_CLJR_ENCODER)            += cljr.o
109 109
 OBJS-$(CONFIG_COOK_DECODER)            += cook.o
110 110
 OBJS-$(CONFIG_CSCD_DECODER)            += cscd.o
111 111
 OBJS-$(CONFIG_CYUV_DECODER)            += cyuv.o
112
-OBJS-$(CONFIG_DCA_DECODER)             += dca.o synth_filter.o dcadsp.o
112
+OBJS-$(CONFIG_DCA_DECODER)             += dca.o synth_filter.o dcadsp.o \
113
+                                          dca_parser.o
113 114
 OBJS-$(CONFIG_DFA_DECODER)             += dfa.o
114 115
 OBJS-$(CONFIG_DNXHD_DECODER)           += dnxhddec.o dnxhddata.o
115 116
 OBJS-$(CONFIG_DNXHD_ENCODER)           += dnxhdenc.o dnxhddata.o       \
... ...
@@ -38,6 +38,7 @@
38 38
 #include "dcadata.h"
39 39
 #include "dcahuff.h"
40 40
 #include "dca.h"
41
+#include "dca_parser.h"
41 42
 #include "synth_filter.h"
42 43
 #include "dcadsp.h"
43 44
 #include "fmtconvert.h"
... ...
@@ -1361,47 +1362,6 @@ static int dca_decode_block(DCAContext *s, int base_channel, int block_index)
1361 1361
 }
1362 1362
 
1363 1363
 /**
1364
- * Convert bitstream to one representation based on sync marker
1365
- */
1366
-static int dca_convert_bitstream(const uint8_t *src, int src_size, uint8_t *dst,
1367
-                                 int max_size)
1368
-{
1369
-    uint32_t mrk;
1370
-    int i, tmp;
1371
-    const uint16_t *ssrc = (const uint16_t *) src;
1372
-    uint16_t *sdst = (uint16_t *) dst;
1373
-    PutBitContext pb;
1374
-
1375
-    if ((unsigned) src_size > (unsigned) max_size) {
1376
-//        av_log(NULL, AV_LOG_ERROR, "Input frame size larger than DCA_MAX_FRAME_SIZE!\n");
1377
-//        return -1;
1378
-        src_size = max_size;
1379
-    }
1380
-
1381
-    mrk = AV_RB32(src);
1382
-    switch (mrk) {
1383
-    case DCA_MARKER_RAW_BE:
1384
-        memcpy(dst, src, src_size);
1385
-        return src_size;
1386
-    case DCA_MARKER_RAW_LE:
1387
-        for (i = 0; i < (src_size + 1) >> 1; i++)
1388
-            *sdst++ = av_bswap16(*ssrc++);
1389
-        return src_size;
1390
-    case DCA_MARKER_14B_BE:
1391
-    case DCA_MARKER_14B_LE:
1392
-        init_put_bits(&pb, dst, max_size);
1393
-        for (i = 0; i < (src_size + 1) >> 1; i++, src += 2) {
1394
-            tmp = ((mrk == DCA_MARKER_14B_BE) ? AV_RB16(src) : AV_RL16(src)) & 0x3FFF;
1395
-            put_bits(&pb, 14, tmp);
1396
-        }
1397
-        flush_put_bits(&pb);
1398
-        return (put_bits_count(&pb) + 7) >> 3;
1399
-    default:
1400
-        return AVERROR_INVALIDDATA;
1401
-    }
1402
-}
1403
-
1404
-/**
1405 1364
  * Return the number of channels in an ExSS speaker mask (HD)
1406 1365
  */
1407 1366
 static int dca_exss_mask2count(int mask)
... ...
@@ -1688,8 +1648,8 @@ static int dca_decode_frame(AVCodecContext *avctx, void *data,
1688 1688
 
1689 1689
     s->xch_present = 0;
1690 1690
 
1691
-    s->dca_buffer_size = dca_convert_bitstream(buf, buf_size, s->dca_buffer,
1692
-                                               DCA_MAX_FRAME_SIZE + DCA_MAX_EXSS_HEADER_SIZE);
1691
+    s->dca_buffer_size = ff_dca_convert_bitstream(buf, buf_size, s->dca_buffer,
1692
+                                                  DCA_MAX_FRAME_SIZE + DCA_MAX_EXSS_HEADER_SIZE);
1693 1693
     if (s->dca_buffer_size == AVERROR_INVALIDDATA) {
1694 1694
         av_log(avctx, AV_LOG_ERROR, "Not a valid DCA frame\n");
1695 1695
         return AVERROR_INVALIDDATA;
... ...
@@ -1703,7 +1663,6 @@ static int dca_decode_frame(AVCodecContext *avctx, void *data,
1703 1703
     //set AVCodec values with parsed data
1704 1704
     avctx->sample_rate = s->sample_rate;
1705 1705
     avctx->bit_rate    = s->bit_rate;
1706
-    avctx->frame_size  = s->sample_blocks * 32;
1707 1706
 
1708 1707
     s->profile = FF_PROFILE_DTS;
1709 1708
 
... ...
@@ -24,6 +24,10 @@
24 24
 
25 25
 #include "parser.h"
26 26
 #include "dca.h"
27
+#include "dcadata.h"
28
+#include "dca_parser.h"
29
+#include "get_bits.h"
30
+#include "put_bits.h"
27 31
 
28 32
 typedef struct DCAParseContext {
29 33
     ParseContext pc;
... ...
@@ -100,6 +104,71 @@ static av_cold int dca_parse_init(AVCodecParserContext * s)
100 100
     return 0;
101 101
 }
102 102
 
103
+int ff_dca_convert_bitstream(const uint8_t *src, int src_size, uint8_t *dst,
104
+                             int max_size)
105
+{
106
+    uint32_t mrk;
107
+    int i, tmp;
108
+    const uint16_t *ssrc = (const uint16_t *) src;
109
+    uint16_t *sdst = (uint16_t *) dst;
110
+    PutBitContext pb;
111
+
112
+    if ((unsigned) src_size > (unsigned) max_size)
113
+        src_size = max_size;
114
+
115
+    mrk = AV_RB32(src);
116
+    switch (mrk) {
117
+    case DCA_MARKER_RAW_BE:
118
+        memcpy(dst, src, src_size);
119
+        return src_size;
120
+    case DCA_MARKER_RAW_LE:
121
+        for (i = 0; i < (src_size + 1) >> 1; i++)
122
+            *sdst++ = av_bswap16(*ssrc++);
123
+        return src_size;
124
+    case DCA_MARKER_14B_BE:
125
+    case DCA_MARKER_14B_LE:
126
+        init_put_bits(&pb, dst, max_size);
127
+        for (i = 0; i < (src_size + 1) >> 1; i++, src += 2) {
128
+            tmp = ((mrk == DCA_MARKER_14B_BE) ? AV_RB16(src) : AV_RL16(src)) & 0x3FFF;
129
+            put_bits(&pb, 14, tmp);
130
+        }
131
+        flush_put_bits(&pb);
132
+        return (put_bits_count(&pb) + 7) >> 3;
133
+    default:
134
+        return AVERROR_INVALIDDATA;
135
+    }
136
+}
137
+
138
+static int dca_parse_params(const uint8_t *buf, int buf_size, int *duration,
139
+                            int *sample_rate)
140
+{
141
+    GetBitContext gb;
142
+    uint8_t hdr[12 + FF_INPUT_BUFFER_PADDING_SIZE] = { 0 };
143
+    int ret, sample_blocks, sr_code;
144
+
145
+    if (buf_size < 12)
146
+        return AVERROR_INVALIDDATA;
147
+
148
+    if ((ret = ff_dca_convert_bitstream(buf, 12, hdr, 12)) < 0)
149
+        return ret;
150
+
151
+    init_get_bits(&gb, hdr, 96);
152
+
153
+    skip_bits_long(&gb, 39);
154
+    sample_blocks = get_bits(&gb, 7) + 1;
155
+    if (sample_blocks < 8)
156
+        return AVERROR_INVALIDDATA;
157
+    *duration = 256 * (sample_blocks / 8);
158
+
159
+    skip_bits(&gb, 20);
160
+    sr_code = get_bits(&gb, 4);
161
+    *sample_rate = dca_sample_rates[sr_code];
162
+    if (*sample_rate == 0)
163
+        return AVERROR_INVALIDDATA;
164
+
165
+    return 0;
166
+}
167
+
103 168
 static int dca_parse(AVCodecParserContext * s,
104 169
                      AVCodecContext * avctx,
105 170
                      const uint8_t ** poutbuf, int *poutbuf_size,
... ...
@@ -107,7 +176,7 @@ static int dca_parse(AVCodecParserContext * s,
107 107
 {
108 108
     DCAParseContext *pc1 = s->priv_data;
109 109
     ParseContext *pc = &pc1->pc;
110
-    int next;
110
+    int next, duration, sample_rate;
111 111
 
112 112
     if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
113 113
         next = buf_size;
... ...
@@ -120,6 +189,15 @@ static int dca_parse(AVCodecParserContext * s,
120 120
             return buf_size;
121 121
         }
122 122
     }
123
+
124
+    /* read the duration and sample rate from the frame header */
125
+    if (!dca_parse_params(buf, buf_size, &duration, &sample_rate)) {
126
+        s->duration = duration;
127
+        if (!avctx->sample_rate)
128
+            avctx->sample_rate = sample_rate;
129
+    } else
130
+        s->duration = 0;
131
+
123 132
     *poutbuf = buf;
124 133
     *poutbuf_size = buf_size;
125 134
     return next;
126 135
new file mode 100644
... ...
@@ -0,0 +1,36 @@
0
+/*
1
+ * DCA parser
2
+ * Copyright (C) 2004 Gildas Bazin
3
+ * Copyright (C) 2004 Benjamin Zores
4
+ * Copyright (C) 2006 Benjamin Larsson
5
+ * Copyright (C) 2007 Konstantin Shishkov
6
+ *
7
+ * This file is part of Libav.
8
+ *
9
+ * Libav is free software; you can redistribute it and/or
10
+ * modify it under the terms of the GNU Lesser General Public
11
+ * License as published by the Free Software Foundation; either
12
+ * version 2.1 of the License, or (at your option) any later version.
13
+ *
14
+ * Libav is distributed in the hope that it will be useful,
15
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17
+ * Lesser General Public License for more details.
18
+ *
19
+ * You should have received a copy of the GNU Lesser General Public
20
+ * License along with Libav; if not, write to the Free Software
21
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22
+ */
23
+
24
+#ifndef AVCODEC_DCA_PARSER_H
25
+#define AVCODEC_DCA_PARSER_H
26
+
27
+#include <stdint.h>
28
+
29
+/**
30
+ * Convert bitstream to one representation based on sync marker
31
+ */
32
+int ff_dca_convert_bitstream(const uint8_t *src, int src_size, uint8_t *dst,
33
+                             int max_size);
34
+
35
+#endif /* AVCODEC_DCA_PARSER_H */