Browse code

share sample rate and blocksize tables between the FLAC encoder and FLAC decoder

Originally committed as revision 18089 to svn://svn.ffmpeg.org/ffmpeg/trunk

Justin Ruggles authored on 2009/03/21 10:16:38
Showing 5 changed files
... ...
@@ -83,8 +83,8 @@ OBJS-$(CONFIG_FFV1_DECODER)            += ffv1.o rangecoder.o
83 83
 OBJS-$(CONFIG_FFV1_ENCODER)            += ffv1.o rangecoder.o
84 84
 OBJS-$(CONFIG_FFVHUFF_DECODER)         += huffyuv.o
85 85
 OBJS-$(CONFIG_FFVHUFF_ENCODER)         += huffyuv.o
86
-OBJS-$(CONFIG_FLAC_DECODER)            += flacdec.o
87
-OBJS-$(CONFIG_FLAC_ENCODER)            += flacenc.o lpc.o
86
+OBJS-$(CONFIG_FLAC_DECODER)            += flacdec.o flacdata.o
87
+OBJS-$(CONFIG_FLAC_ENCODER)            += flacenc.o flacdata.o lpc.o
88 88
 OBJS-$(CONFIG_FLASHSV_DECODER)         += flashsv.o
89 89
 OBJS-$(CONFIG_FLASHSV_ENCODER)         += flashsvenc.o
90 90
 OBJS-$(CONFIG_FLIC_DECODER)            += flicvideo.o
... ...
@@ -346,17 +346,17 @@ OBJS-$(CONFIG_ADPCM_YAMAHA_ENCODER)       += adpcm.o
346 346
 
347 347
 # libavformat dependencies
348 348
 OBJS-$(CONFIG_EAC3_DEMUXER)            += ac3_parser.o ac3tab.o aac_ac3_parser.o
349
-OBJS-$(CONFIG_FLAC_DEMUXER)            += flacdec.o
350
-OBJS-$(CONFIG_FLAC_MUXER)              += flacdec.o
349
+OBJS-$(CONFIG_FLAC_DEMUXER)            += flacdec.o flacdata.o
350
+OBJS-$(CONFIG_FLAC_MUXER)              += flacdec.o flacdata.o
351 351
 OBJS-$(CONFIG_GXF_DEMUXER)             += mpeg12data.o
352
-OBJS-$(CONFIG_MATROSKA_AUDIO_MUXER)    += xiph.o mpeg4audio.o flacdec.o
352
+OBJS-$(CONFIG_MATROSKA_AUDIO_MUXER)    += xiph.o mpeg4audio.o flacdec.o flacdata.o
353 353
 OBJS-$(CONFIG_MATROSKA_DEMUXER)        += mpeg4audio.o
354
-OBJS-$(CONFIG_MATROSKA_MUXER)          += xiph.o mpeg4audio.o flacdec.o
354
+OBJS-$(CONFIG_MATROSKA_MUXER)          += xiph.o mpeg4audio.o flacdec.o flacdata.o
355 355
 OBJS-$(CONFIG_MOV_DEMUXER)             += mpeg4audio.o mpegaudiodata.o
356 356
 OBJS-$(CONFIG_MPEGTS_MUXER)            += mpegvideo.o
357 357
 OBJS-$(CONFIG_NUT_MUXER)               += mpegaudiodata.o
358
-OBJS-$(CONFIG_OGG_DEMUXER)             += flacdec.o
359
-OBJS-$(CONFIG_OGG_MUXER)               += xiph.o flacdec.o
358
+OBJS-$(CONFIG_OGG_DEMUXER)             += flacdec.o flacdata.o
359
+OBJS-$(CONFIG_OGG_MUXER)               += xiph.o flacdec.o flacdata.o
360 360
 OBJS-$(CONFIG_RTP_MUXER)               += mpegvideo.o
361 361
 
362 362
 # external codec libraries
363 363
new file mode 100644
... ...
@@ -0,0 +1,33 @@
0
+/*
1
+ * FLAC data
2
+ * Copyright (c) 2003 Alex Beregszaszi
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
+#include "internal.h"
22
+
23
+const int ff_flac_sample_rate_table[16] =
24
+{ 0,
25
+  88200, 176400, 192000,
26
+  8000, 16000, 22050, 24000, 32000, 44100, 48000, 96000,
27
+  0, 0, 0, 0 };
28
+
29
+const int16_t ff_flac_blocksize_table[16] = {
30
+     0,    192, 576<<0, 576<<1, 576<<2, 576<<3,      0,      0,
31
+256<<0, 256<<1, 256<<2, 256<<3, 256<<4, 256<<5, 256<<6, 256<<7
32
+};
0 33
new file mode 100644
... ...
@@ -0,0 +1,31 @@
0
+/*
1
+ * FLAC data header
2
+ * Copyright (c) 2003 Alex Beregszaszi
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
+#ifndef AVCODEC_FLACDATA_H
22
+#define AVCODEC_FLACDATA_H
23
+
24
+#include "internal.h"
25
+
26
+extern const int ff_flac_sample_rate_table[16];
27
+
28
+extern const int16_t ff_flac_blocksize_table[16];
29
+
30
+#endif /* AVCODEC_FLACDATA_H */
... ...
@@ -42,6 +42,7 @@
42 42
 #include "bytestream.h"
43 43
 #include "golomb.h"
44 44
 #include "flac.h"
45
+#include "flacdata.h"
45 46
 
46 47
 #undef NDEBUG
47 48
 #include <assert.h>
... ...
@@ -66,20 +67,9 @@ typedef struct FLACContext {
66 66
     unsigned int allocated_bitstream_size;
67 67
 } FLACContext;
68 68
 
69
-static const int sample_rate_table[] =
70
-{ 0,
71
-  88200, 176400, 192000,
72
-  8000, 16000, 22050, 24000, 32000, 44100, 48000, 96000,
73
-  0, 0, 0, 0 };
74
-
75 69
 static const int sample_size_table[] =
76 70
 { 0, 8, 12, 0, 16, 20, 24, 0 };
77 71
 
78
-static const int blocksize_table[] = {
79
-     0,    192, 576<<0, 576<<1, 576<<2, 576<<3,      0,      0,
80
-256<<0, 256<<1, 256<<2, 256<<3, 256<<4, 256<<5, 256<<6, 256<<7
81
-};
82
-
83 72
 static int64_t get_utf8(GetBitContext *gb)
84 73
 {
85 74
     int64_t val;
... ...
@@ -547,7 +537,7 @@ static int decode_frame(FLACContext *s, int alloc_data_size)
547 547
     else if (blocksize_code == 7)
548 548
         blocksize = get_bits(&s->gb, 16)+1;
549 549
     else
550
-        blocksize = blocksize_table[blocksize_code];
550
+        blocksize = ff_flac_blocksize_table[blocksize_code];
551 551
 
552 552
     if (blocksize > s->max_blocksize) {
553 553
         av_log(s->avctx, AV_LOG_ERROR, "blocksize %d > %d\n", blocksize,
... ...
@@ -561,7 +551,7 @@ static int decode_frame(FLACContext *s, int alloc_data_size)
561 561
     if (sample_rate_code == 0)
562 562
         samplerate= s->samplerate;
563 563
     else if (sample_rate_code < 12)
564
-        samplerate = sample_rate_table[sample_rate_code];
564
+        samplerate = ff_flac_sample_rate_table[sample_rate_code];
565 565
     else if (sample_rate_code == 12)
566 566
         samplerate = get_bits(&s->gb, 8) * 1000;
567 567
     else if (sample_rate_code == 13)
... ...
@@ -28,6 +28,7 @@
28 28
 #include "golomb.h"
29 29
 #include "lpc.h"
30 30
 #include "flac.h"
31
+#include "flacdata.h"
31 32
 
32 33
 #define FLAC_SUBFRAME_CONSTANT  0
33 34
 #define FLAC_SUBFRAME_VERBATIM  1
... ...
@@ -79,12 +80,10 @@ typedef struct FlacFrame {
79 79
 } FlacFrame;
80 80
 
81 81
 typedef struct FlacEncodeContext {
82
+    FLACSTREAMINFO
82 83
     PutBitContext pb;
83
-    int channels;
84
-    int samplerate;
85 84
     int sr_code[2];
86 85
     int min_framesize;
87
-    int max_framesize;
88 86
     int max_encoded_framesize;
89 87
     uint32_t frame_count;
90 88
     uint64_t sample_count;
... ...
@@ -96,20 +95,6 @@ typedef struct FlacEncodeContext {
96 96
     struct AVMD5 *md5ctx;
97 97
 } FlacEncodeContext;
98 98
 
99
-static const int flac_samplerates[16] = {
100
-    0, 0, 0, 0,
101
-    8000, 16000, 22050, 24000, 32000, 44100, 48000, 96000,
102
-    0, 0, 0, 0
103
-};
104
-
105
-static const int flac_blocksizes[16] = {
106
-    0,
107
-    192,
108
-    576, 1152, 2304, 4608,
109
-    0, 0,
110
-    256, 512, 1024, 2048, 4096, 8192, 16384, 32768
111
-};
112
-
113 99
 /**
114 100
  * Writes streaminfo metadata block to byte array
115 101
  */
... ...
@@ -146,11 +131,11 @@ static int select_blocksize(int samplerate, int block_time_ms)
146 146
     int blocksize;
147 147
 
148 148
     assert(samplerate > 0);
149
-    blocksize = flac_blocksizes[1];
149
+    blocksize = ff_flac_blocksize_table[1];
150 150
     target = (samplerate * block_time_ms) / 1000;
151 151
     for(i=0; i<16; i++) {
152
-        if(target >= flac_blocksizes[i] && flac_blocksizes[i] > blocksize) {
153
-            blocksize = flac_blocksizes[i];
152
+        if(target >= ff_flac_blocksize_table[i] && ff_flac_blocksize_table[i] > blocksize) {
153
+            blocksize = ff_flac_blocksize_table[i];
154 154
         }
155 155
     }
156 156
     return blocksize;
... ...
@@ -181,8 +166,8 @@ static av_cold int flac_encode_init(AVCodecContext *avctx)
181 181
     if(freq < 1)
182 182
         return -1;
183 183
     for(i=4; i<12; i++) {
184
-        if(freq == flac_samplerates[i]) {
185
-            s->samplerate = flac_samplerates[i];
184
+        if(freq == ff_flac_sample_rate_table[i]) {
185
+            s->samplerate = ff_flac_sample_rate_table[i];
186 186
             s->sr_code[0] = i;
187 187
             s->sr_code[1] = 0;
188 188
             break;
... ...
@@ -392,8 +377,8 @@ static void init_frame(FlacEncodeContext *s)
392 392
     frame = &s->frame;
393 393
 
394 394
     for(i=0; i<16; i++) {
395
-        if(s->avctx->frame_size == flac_blocksizes[i]) {
396
-            frame->blocksize = flac_blocksizes[i];
395
+        if(s->avctx->frame_size == ff_flac_blocksize_table[i]) {
396
+            frame->blocksize = ff_flac_blocksize_table[i];
397 397
             frame->bs_code[0] = i;
398 398
             frame->bs_code[1] = 0;
399 399
             break;