[FFmpeg-cvslog] r18092 - in trunk/libavcodec: Makefile flac.c flac.h flacdec.c flacenc.c

jbr subversion
Sat Mar 21 02:54:31 CET 2009


Author: jbr
Date: Sat Mar 21 02:54:31 2009
New Revision: 18092

Log:
add a function to calculate a more accurate estimate for maximum FLAC 
frame size and use the function in the FLAC decoder and FLAC encoder

Added:
   trunk/libavcodec/flac.c
Modified:
   trunk/libavcodec/Makefile
   trunk/libavcodec/flac.h
   trunk/libavcodec/flacdec.c
   trunk/libavcodec/flacenc.c

Modified: trunk/libavcodec/Makefile
==============================================================================
--- trunk/libavcodec/Makefile	Sat Mar 21 02:27:28 2009	(r18091)
+++ trunk/libavcodec/Makefile	Sat Mar 21 02:54:31 2009	(r18092)
@@ -83,8 +83,8 @@ OBJS-$(CONFIG_FFV1_DECODER)            +
 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 flacdata.o
-OBJS-$(CONFIG_FLAC_ENCODER)            += flacenc.o flacdata.o lpc.o
+OBJS-$(CONFIG_FLAC_DECODER)            += flacdec.o flacdata.o flac.o
+OBJS-$(CONFIG_FLAC_ENCODER)            += flacenc.o flacdata.o flac.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 @@ OBJS-$(CONFIG_ADPCM_YAMAHA_ENCODER)     
 
 # libavformat dependencies
 OBJS-$(CONFIG_EAC3_DEMUXER)            += ac3_parser.o ac3tab.o aac_ac3_parser.o
-OBJS-$(CONFIG_FLAC_DEMUXER)            += flacdec.o flacdata.o
-OBJS-$(CONFIG_FLAC_MUXER)              += flacdec.o flacdata.o
+OBJS-$(CONFIG_FLAC_DEMUXER)            += flacdec.o flacdata.o flac.o
+OBJS-$(CONFIG_FLAC_MUXER)              += flacdec.o flacdata.o flac.o
 OBJS-$(CONFIG_GXF_DEMUXER)             += mpeg12data.o
-OBJS-$(CONFIG_MATROSKA_AUDIO_MUXER)    += xiph.o mpeg4audio.o flacdec.o flacdata.o
+OBJS-$(CONFIG_MATROSKA_AUDIO_MUXER)    += xiph.o mpeg4audio.o flacdec.o flacdata.o flac.o
 OBJS-$(CONFIG_MATROSKA_DEMUXER)        += mpeg4audio.o
-OBJS-$(CONFIG_MATROSKA_MUXER)          += xiph.o mpeg4audio.o flacdec.o flacdata.o
+OBJS-$(CONFIG_MATROSKA_MUXER)          += xiph.o mpeg4audio.o flacdec.o flacdata.o flac.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 flacdata.o
-OBJS-$(CONFIG_OGG_MUXER)               += xiph.o flacdec.o flacdata.o
+OBJS-$(CONFIG_OGG_DEMUXER)             += flacdec.o flacdata.o flac.o
+OBJS-$(CONFIG_OGG_MUXER)               += xiph.o flacdec.o flacdata.o flac.o
 OBJS-$(CONFIG_RTP_MUXER)               += mpegvideo.o
 
 # external codec libraries

Added: trunk/libavcodec/flac.c
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ trunk/libavcodec/flac.c	Sat Mar 21 02:54:31 2009	(r18092)
@@ -0,0 +1,43 @@
+/*
+ * FLAC common code
+ * Copyright (c) 2009 Justin Ruggles
+ *
+ * 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 "flac.h"
+
+int ff_flac_get_max_frame_size(int blocksize, int ch, int bps)
+{
+    /* Technically, there is no limit to FLAC frame size, but an encoder
+       should not write a frame that is larger than if verbatim encoding mode
+       were to be used. */
+
+    int count;
+
+    count = 16;                  /* frame header */
+    count += ch * ((7+bps+7)/8); /* subframe headers */
+    if (ch == 2) {
+        /* for stereo, need to account for using decorrelation */
+        count += (( 2*bps+1) * blocksize + 7) / 8;
+    } else {
+        count += ( ch*bps    * blocksize + 7) / 8;
+    }
+    count += 2; /* frame footer */
+
+    return count;
+}

Modified: trunk/libavcodec/flac.h
==============================================================================
--- trunk/libavcodec/flac.h	Sat Mar 21 02:27:28 2009	(r18091)
+++ trunk/libavcodec/flac.h	Sat Mar 21 02:54:31 2009	(r18092)
@@ -103,4 +103,12 @@ int ff_flac_is_extradata_valid(AVCodecCo
 void ff_flac_parse_block_header(const uint8_t *block_header,
                                 int *last, int *type, int *size);
 
+/**
+ * Calculate an estimate for the maximum frame size based on verbatim mode.
+ * @param blocksize block size, in samples
+ * @param ch number of channels
+ * @param bps bits-per-sample
+ */
+int ff_flac_get_max_frame_size(int blocksize, int ch, int bps);
+
 #endif /* AVCODEC_FLAC_H */

Modified: trunk/libavcodec/flacdec.c
==============================================================================
--- trunk/libavcodec/flacdec.c	Sat Mar 21 02:27:28 2009	(r18091)
+++ trunk/libavcodec/flacdec.c	Sat Mar 21 02:54:31 2009	(r18092)
@@ -147,7 +147,8 @@ static void allocate_buffers(FLACContext
     assert(s->max_blocksize);
 
     if (s->max_framesize == 0 && s->max_blocksize) {
-        s->max_framesize = 23 + (s->channels * s->bps * s->max_blocksize + 7) / 8;
+        s->max_framesize = ff_flac_get_max_frame_size(s->max_blocksize,
+                                                      s->channels, s->bps);
     }
 
     for (i = 0; i < s->channels; i++) {

Modified: trunk/libavcodec/flacenc.c
==============================================================================
--- trunk/libavcodec/flacenc.c	Sat Mar 21 02:27:28 2009	(r18091)
+++ trunk/libavcodec/flacenc.c	Sat Mar 21 02:54:31 2009	(r18092)
@@ -345,11 +345,8 @@ static av_cold int flac_encode_init(AVCo
            s->options.lpc_coeff_precision);
 
     /* set maximum encoded frame size in verbatim mode */
-    if(s->channels == 2) {
-        s->max_framesize = 14 + ((s->avctx->frame_size * 33 + 7) >> 3);
-    } else {
-        s->max_framesize = 14 + (s->avctx->frame_size * s->channels * 2);
-    }
+    s->max_framesize = ff_flac_get_max_frame_size(s->avctx->frame_size,
+                                                  s->channels, 16);
 
     /* initialize MD5 context */
     s->md5ctx = av_malloc(av_md5_size);




More information about the ffmpeg-cvslog mailing list