changeset 9213:782d31263979 libavcodec

share sample rate and blocksize tables between the FLAC encoder and FLAC decoder
author jbr
date Sat, 21 Mar 2009 01:16:38 +0000
parents c7a8e302b98a
children 78b55e071bb9
files Makefile flacdata.c flacdata.h flacdec.c flacenc.c
diffstat 5 files changed, 84 insertions(+), 45 deletions(-) [+]
line wrap: on
line diff
--- a/Makefile	Sat Mar 21 00:50:19 2009 +0000
+++ b/Makefile	Sat Mar 21 01:16:38 2009 +0000
@@ -83,8 +83,8 @@
 OBJS-$(CONFIG_FFV1_ENCODER)            += ffv1.o rangecoder.o
 OBJS-$(CONFIG_FFVHUFF_DECODER)         += huffyuv.o
 OBJS-$(CONFIG_FFVHUFF_ENCODER)         += huffyuv.o
-OBJS-$(CONFIG_FLAC_DECODER)            += flacdec.o
-OBJS-$(CONFIG_FLAC_ENCODER)            += flacenc.o lpc.o
+OBJS-$(CONFIG_FLAC_DECODER)            += flacdec.o flacdata.o
+OBJS-$(CONFIG_FLAC_ENCODER)            += flacenc.o flacdata.o lpc.o
 OBJS-$(CONFIG_FLASHSV_DECODER)         += flashsv.o
 OBJS-$(CONFIG_FLASHSV_ENCODER)         += flashsvenc.o
 OBJS-$(CONFIG_FLIC_DECODER)            += flicvideo.o
@@ -346,17 +346,17 @@
 
 # libavformat dependencies
 OBJS-$(CONFIG_EAC3_DEMUXER)            += ac3_parser.o ac3tab.o aac_ac3_parser.o
-OBJS-$(CONFIG_FLAC_DEMUXER)            += flacdec.o
-OBJS-$(CONFIG_FLAC_MUXER)              += flacdec.o
+OBJS-$(CONFIG_FLAC_DEMUXER)            += flacdec.o flacdata.o
+OBJS-$(CONFIG_FLAC_MUXER)              += flacdec.o flacdata.o
 OBJS-$(CONFIG_GXF_DEMUXER)             += mpeg12data.o
-OBJS-$(CONFIG_MATROSKA_AUDIO_MUXER)    += xiph.o mpeg4audio.o flacdec.o
+OBJS-$(CONFIG_MATROSKA_AUDIO_MUXER)    += xiph.o mpeg4audio.o flacdec.o flacdata.o
 OBJS-$(CONFIG_MATROSKA_DEMUXER)        += mpeg4audio.o
-OBJS-$(CONFIG_MATROSKA_MUXER)          += xiph.o mpeg4audio.o flacdec.o
+OBJS-$(CONFIG_MATROSKA_MUXER)          += xiph.o mpeg4audio.o flacdec.o flacdata.o
 OBJS-$(CONFIG_MOV_DEMUXER)             += mpeg4audio.o mpegaudiodata.o
 OBJS-$(CONFIG_MPEGTS_MUXER)            += mpegvideo.o
 OBJS-$(CONFIG_NUT_MUXER)               += mpegaudiodata.o
-OBJS-$(CONFIG_OGG_DEMUXER)             += flacdec.o
-OBJS-$(CONFIG_OGG_MUXER)               += xiph.o flacdec.o
+OBJS-$(CONFIG_OGG_DEMUXER)             += flacdec.o flacdata.o
+OBJS-$(CONFIG_OGG_MUXER)               += xiph.o flacdec.o flacdata.o
 OBJS-$(CONFIG_RTP_MUXER)               += mpegvideo.o
 
 # external codec libraries
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flacdata.c	Sat Mar 21 01:16:38 2009 +0000
@@ -0,0 +1,33 @@
+/*
+ * FLAC data
+ * Copyright (c) 2003 Alex Beregszaszi
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include "internal.h"
+
+const int ff_flac_sample_rate_table[16] =
+{ 0,
+  88200, 176400, 192000,
+  8000, 16000, 22050, 24000, 32000, 44100, 48000, 96000,
+  0, 0, 0, 0 };
+
+const int16_t ff_flac_blocksize_table[16] = {
+     0,    192, 576<<0, 576<<1, 576<<2, 576<<3,      0,      0,
+256<<0, 256<<1, 256<<2, 256<<3, 256<<4, 256<<5, 256<<6, 256<<7
+};
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flacdata.h	Sat Mar 21 01:16:38 2009 +0000
@@ -0,0 +1,31 @@
+/*
+ * FLAC data header
+ * Copyright (c) 2003 Alex Beregszaszi
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#ifndef AVCODEC_FLACDATA_H
+#define AVCODEC_FLACDATA_H
+
+#include "internal.h"
+
+extern const int ff_flac_sample_rate_table[16];
+
+extern const int16_t ff_flac_blocksize_table[16];
+
+#endif /* AVCODEC_FLACDATA_H */
--- a/flacdec.c	Sat Mar 21 00:50:19 2009 +0000
+++ b/flacdec.c	Sat Mar 21 01:16:38 2009 +0000
@@ -42,6 +42,7 @@
 #include "bytestream.h"
 #include "golomb.h"
 #include "flac.h"
+#include "flacdata.h"
 
 #undef NDEBUG
 #include <assert.h>
@@ -66,20 +67,9 @@
     unsigned int allocated_bitstream_size;
 } FLACContext;
 
-static const int sample_rate_table[] =
-{ 0,
-  88200, 176400, 192000,
-  8000, 16000, 22050, 24000, 32000, 44100, 48000, 96000,
-  0, 0, 0, 0 };
-
 static const int sample_size_table[] =
 { 0, 8, 12, 0, 16, 20, 24, 0 };
 
-static const int blocksize_table[] = {
-     0,    192, 576<<0, 576<<1, 576<<2, 576<<3,      0,      0,
-256<<0, 256<<1, 256<<2, 256<<3, 256<<4, 256<<5, 256<<6, 256<<7
-};
-
 static int64_t get_utf8(GetBitContext *gb)
 {
     int64_t val;
@@ -547,7 +537,7 @@
     else if (blocksize_code == 7)
         blocksize = get_bits(&s->gb, 16)+1;
     else
-        blocksize = blocksize_table[blocksize_code];
+        blocksize = ff_flac_blocksize_table[blocksize_code];
 
     if (blocksize > s->max_blocksize) {
         av_log(s->avctx, AV_LOG_ERROR, "blocksize %d > %d\n", blocksize,
@@ -561,7 +551,7 @@
     if (sample_rate_code == 0)
         samplerate= s->samplerate;
     else if (sample_rate_code < 12)
-        samplerate = sample_rate_table[sample_rate_code];
+        samplerate = ff_flac_sample_rate_table[sample_rate_code];
     else if (sample_rate_code == 12)
         samplerate = get_bits(&s->gb, 8) * 1000;
     else if (sample_rate_code == 13)
--- a/flacenc.c	Sat Mar 21 00:50:19 2009 +0000
+++ b/flacenc.c	Sat Mar 21 01:16:38 2009 +0000
@@ -28,6 +28,7 @@
 #include "golomb.h"
 #include "lpc.h"
 #include "flac.h"
+#include "flacdata.h"
 
 #define FLAC_SUBFRAME_CONSTANT  0
 #define FLAC_SUBFRAME_VERBATIM  1
@@ -79,12 +80,10 @@
 } FlacFrame;
 
 typedef struct FlacEncodeContext {
+    FLACSTREAMINFO
     PutBitContext pb;
-    int channels;
-    int samplerate;
     int sr_code[2];
     int min_framesize;
-    int max_framesize;
     int max_encoded_framesize;
     uint32_t frame_count;
     uint64_t sample_count;
@@ -96,20 +95,6 @@
     struct AVMD5 *md5ctx;
 } FlacEncodeContext;
 
-static const int flac_samplerates[16] = {
-    0, 0, 0, 0,
-    8000, 16000, 22050, 24000, 32000, 44100, 48000, 96000,
-    0, 0, 0, 0
-};
-
-static const int flac_blocksizes[16] = {
-    0,
-    192,
-    576, 1152, 2304, 4608,
-    0, 0,
-    256, 512, 1024, 2048, 4096, 8192, 16384, 32768
-};
-
 /**
  * Writes streaminfo metadata block to byte array
  */
@@ -146,11 +131,11 @@
     int blocksize;
 
     assert(samplerate > 0);
-    blocksize = flac_blocksizes[1];
+    blocksize = ff_flac_blocksize_table[1];
     target = (samplerate * block_time_ms) / 1000;
     for(i=0; i<16; i++) {
-        if(target >= flac_blocksizes[i] && flac_blocksizes[i] > blocksize) {
-            blocksize = flac_blocksizes[i];
+        if(target >= ff_flac_blocksize_table[i] && ff_flac_blocksize_table[i] > blocksize) {
+            blocksize = ff_flac_blocksize_table[i];
         }
     }
     return blocksize;
@@ -181,8 +166,8 @@
     if(freq < 1)
         return -1;
     for(i=4; i<12; i++) {
-        if(freq == flac_samplerates[i]) {
-            s->samplerate = flac_samplerates[i];
+        if(freq == ff_flac_sample_rate_table[i]) {
+            s->samplerate = ff_flac_sample_rate_table[i];
             s->sr_code[0] = i;
             s->sr_code[1] = 0;
             break;
@@ -392,8 +377,8 @@
     frame = &s->frame;
 
     for(i=0; i<16; i++) {
-        if(s->avctx->frame_size == flac_blocksizes[i]) {
-            frame->blocksize = flac_blocksizes[i];
+        if(s->avctx->frame_size == ff_flac_blocksize_table[i]) {
+            frame->blocksize = ff_flac_blocksize_table[i];
             frame->bs_code[0] = i;
             frame->bs_code[1] = 0;
             break;