📄 flacenc.c
字号:
/** * FLAC audio encoder * Copyright (c) 2006 Justin Ruggles <jruggle@earthlink.net> * * 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 "avcodec.h"#include "bitstream.h"#include "crc.h"#include "dsputil.h"#include "golomb.h"#include "lls.h"#define FLAC_MAX_CH 8#define FLAC_MIN_BLOCKSIZE 16#define FLAC_MAX_BLOCKSIZE 65535#define FLAC_SUBFRAME_CONSTANT 0#define FLAC_SUBFRAME_VERBATIM 1#define FLAC_SUBFRAME_FIXED 8#define FLAC_SUBFRAME_LPC 32#define FLAC_CHMODE_NOT_STEREO 0#define FLAC_CHMODE_LEFT_RIGHT 1#define FLAC_CHMODE_LEFT_SIDE 8#define FLAC_CHMODE_RIGHT_SIDE 9#define FLAC_CHMODE_MID_SIDE 10#define ORDER_METHOD_EST 0#define ORDER_METHOD_2LEVEL 1#define ORDER_METHOD_4LEVEL 2#define ORDER_METHOD_8LEVEL 3#define ORDER_METHOD_SEARCH 4#define ORDER_METHOD_LOG 5#define FLAC_STREAMINFO_SIZE 34#define MIN_LPC_ORDER 1#define MAX_LPC_ORDER 32#define MAX_FIXED_ORDER 4#define MAX_PARTITION_ORDER 8#define MAX_PARTITIONS (1 << MAX_PARTITION_ORDER)#define MAX_LPC_PRECISION 15#define MAX_LPC_SHIFT 15#define MAX_RICE_PARAM 14typedef struct CompressionOptions { int compression_level; int block_time_ms; int use_lpc; int lpc_coeff_precision; int min_prediction_order; int max_prediction_order; int prediction_order_method; int min_partition_order; int max_partition_order;} CompressionOptions;typedef struct RiceContext { int porder; int params[MAX_PARTITIONS];} RiceContext;typedef struct FlacSubframe { int type; int type_code; int obits; int order; int32_t coefs[MAX_LPC_ORDER]; int shift; RiceContext rc; int32_t samples[FLAC_MAX_BLOCKSIZE]; int32_t residual[FLAC_MAX_BLOCKSIZE+1];} FlacSubframe;typedef struct FlacFrame { FlacSubframe subframes[FLAC_MAX_CH]; int blocksize; int bs_code[2]; uint8_t crc8; int ch_mode;} FlacFrame;typedef struct FlacEncodeContext { PutBitContext pb; int channels; int ch_code; int samplerate; int sr_code[2]; int blocksize; int max_framesize; uint32_t frame_count; FlacFrame frame; CompressionOptions options; AVCodecContext *avctx; DSPContext dsp;} 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 */static void write_streaminfo(FlacEncodeContext *s, uint8_t *header){ PutBitContext pb; memset(header, 0, FLAC_STREAMINFO_SIZE); init_put_bits(&pb, header, FLAC_STREAMINFO_SIZE); /* streaminfo metadata block */ put_bits(&pb, 16, s->blocksize); put_bits(&pb, 16, s->blocksize); put_bits(&pb, 24, 0); put_bits(&pb, 24, s->max_framesize); put_bits(&pb, 20, s->samplerate); put_bits(&pb, 3, s->channels-1); put_bits(&pb, 5, 15); /* bits per sample - 1 */ flush_put_bits(&pb); /* total samples = 0 */ /* MD5 signature = 0 */}/** * Sets blocksize based on samplerate * Chooses the closest predefined blocksize >= BLOCK_TIME_MS milliseconds */static int select_blocksize(int samplerate, int block_time_ms){ int i; int target; int blocksize; assert(samplerate > 0); blocksize = flac_blocksizes[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]; } } return blocksize;}static int flac_encode_init(AVCodecContext *avctx){ int freq = avctx->sample_rate; int channels = avctx->channels; FlacEncodeContext *s = avctx->priv_data; int i, level; uint8_t *streaminfo; s->avctx = avctx; dsputil_init(&s->dsp, avctx); if(avctx->sample_fmt != SAMPLE_FMT_S16) { return -1; } if(channels < 1 || channels > FLAC_MAX_CH) { return -1; } s->channels = channels; s->ch_code = s->channels-1; /* find samplerate in table */ if(freq < 1) return -1; for(i=4; i<12; i++) { if(freq == flac_samplerates[i]) { s->samplerate = flac_samplerates[i]; s->sr_code[0] = i; s->sr_code[1] = 0; break; } } /* if not in table, samplerate is non-standard */ if(i == 12) { if(freq % 1000 == 0 && freq < 255000) { s->sr_code[0] = 12; s->sr_code[1] = freq / 1000; } else if(freq % 10 == 0 && freq < 655350) { s->sr_code[0] = 14; s->sr_code[1] = freq / 10; } else if(freq < 65535) { s->sr_code[0] = 13; s->sr_code[1] = freq; } else { return -1; } s->samplerate = freq; } /* set compression option defaults based on avctx->compression_level */ if(avctx->compression_level < 0) { s->options.compression_level = 5; } else { s->options.compression_level = avctx->compression_level; } av_log(avctx, AV_LOG_DEBUG, " compression: %d\n", s->options.compression_level); level= s->options.compression_level; if(level > 12) { av_log(avctx, AV_LOG_ERROR, "invalid compression level: %d\n", s->options.compression_level); return -1; } s->options.block_time_ms = ((int[]){ 27, 27, 27,105,105,105,105,105,105,105,105,105,105})[level]; s->options.use_lpc = ((int[]){ 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1})[level]; s->options.min_prediction_order= ((int[]){ 2, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1})[level]; s->options.max_prediction_order= ((int[]){ 3, 4, 4, 6, 8, 8, 8, 8, 12, 12, 12, 32, 32})[level]; s->options.prediction_order_method = ((int[]){ ORDER_METHOD_EST, ORDER_METHOD_EST, ORDER_METHOD_EST, ORDER_METHOD_EST, ORDER_METHOD_EST, ORDER_METHOD_EST, ORDER_METHOD_4LEVEL, ORDER_METHOD_LOG, ORDER_METHOD_4LEVEL, ORDER_METHOD_LOG, ORDER_METHOD_SEARCH, ORDER_METHOD_LOG, ORDER_METHOD_SEARCH})[level]; s->options.min_partition_order = ((int[]){ 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0})[level]; s->options.max_partition_order = ((int[]){ 2, 2, 3, 3, 3, 8, 8, 8, 8, 8, 8, 8, 8})[level]; /* set compression option overrides from AVCodecContext */ if(avctx->use_lpc >= 0) { s->options.use_lpc = av_clip(avctx->use_lpc, 0, 11); } if(s->options.use_lpc == 1) av_log(avctx, AV_LOG_DEBUG, " use lpc: Levinson-Durbin recursion with Welch window\n"); else if(s->options.use_lpc > 1) av_log(avctx, AV_LOG_DEBUG, " use lpc: Cholesky factorization\n"); if(avctx->min_prediction_order >= 0) { if(s->options.use_lpc) { if(avctx->min_prediction_order < MIN_LPC_ORDER || avctx->min_prediction_order > MAX_LPC_ORDER) { av_log(avctx, AV_LOG_ERROR, "invalid min prediction order: %d\n", avctx->min_prediction_order); return -1; } } else { if(avctx->min_prediction_order > MAX_FIXED_ORDER) { av_log(avctx, AV_LOG_ERROR, "invalid min prediction order: %d\n", avctx->min_prediction_order); return -1; } } s->options.min_prediction_order = avctx->min_prediction_order; } if(avctx->max_prediction_order >= 0) { if(s->options.use_lpc) { if(avctx->max_prediction_order < MIN_LPC_ORDER || avctx->max_prediction_order > MAX_LPC_ORDER) { av_log(avctx, AV_LOG_ERROR, "invalid max prediction order: %d\n", avctx->max_prediction_order); return -1; } } else { if(avctx->max_prediction_order > MAX_FIXED_ORDER) { av_log(avctx, AV_LOG_ERROR, "invalid max prediction order: %d\n", avctx->max_prediction_order); return -1; } } s->options.max_prediction_order = avctx->max_prediction_order; } if(s->options.max_prediction_order < s->options.min_prediction_order) { av_log(avctx, AV_LOG_ERROR, "invalid prediction orders: min=%d max=%d\n", s->options.min_prediction_order, s->options.max_prediction_order); return -1; } av_log(avctx, AV_LOG_DEBUG, " prediction order: %d, %d\n", s->options.min_prediction_order, s->options.max_prediction_order); if(avctx->prediction_order_method >= 0) { if(avctx->prediction_order_method > ORDER_METHOD_LOG) { av_log(avctx, AV_LOG_ERROR, "invalid prediction order method: %d\n", avctx->prediction_order_method); return -1; } s->options.prediction_order_method = avctx->prediction_order_method; } switch(s->options.prediction_order_method) { case ORDER_METHOD_EST: av_log(avctx, AV_LOG_DEBUG, " order method: %s\n", "estimate"); break; case ORDER_METHOD_2LEVEL: av_log(avctx, AV_LOG_DEBUG, " order method: %s\n", "2-level"); break; case ORDER_METHOD_4LEVEL: av_log(avctx, AV_LOG_DEBUG, " order method: %s\n", "4-level"); break; case ORDER_METHOD_8LEVEL: av_log(avctx, AV_LOG_DEBUG, " order method: %s\n", "8-level"); break; case ORDER_METHOD_SEARCH: av_log(avctx, AV_LOG_DEBUG, " order method: %s\n", "full search"); break; case ORDER_METHOD_LOG: av_log(avctx, AV_LOG_DEBUG, " order method: %s\n", "log search"); break; } if(avctx->min_partition_order >= 0) { if(avctx->min_partition_order > MAX_PARTITION_ORDER) { av_log(avctx, AV_LOG_ERROR, "invalid min partition order: %d\n", avctx->min_partition_order); return -1; } s->options.min_partition_order = avctx->min_partition_order; } if(avctx->max_partition_order >= 0) { if(avctx->max_partition_order > MAX_PARTITION_ORDER) { av_log(avctx, AV_LOG_ERROR, "invalid max partition order: %d\n", avctx->max_partition_order); return -1; } s->options.max_partition_order = avctx->max_partition_order; } if(s->options.max_partition_order < s->options.min_partition_order) { av_log(avctx, AV_LOG_ERROR, "invalid partition orders: min=%d max=%d\n", s->options.min_partition_order, s->options.max_partition_order); return -1; } av_log(avctx, AV_LOG_DEBUG, " partition order: %d, %d\n", s->options.min_partition_order, s->options.max_partition_order); if(avctx->frame_size > 0) { if(avctx->frame_size < FLAC_MIN_BLOCKSIZE || avctx->frame_size > FLAC_MAX_BLOCKSIZE) { av_log(avctx, AV_LOG_ERROR, "invalid block size: %d\n", avctx->frame_size); return -1; } s->blocksize = avctx->frame_size; } else { s->blocksize = select_blocksize(s->samplerate, s->options.block_time_ms); avctx->frame_size = s->blocksize; } av_log(avctx, AV_LOG_DEBUG, " block size: %d\n", s->blocksize); /* set LPC precision */ if(avctx->lpc_coeff_precision > 0) { if(avctx->lpc_coeff_precision > MAX_LPC_PRECISION) { av_log(avctx, AV_LOG_ERROR, "invalid lpc coeff precision: %d\n", avctx->lpc_coeff_precision); return -1; } s->options.lpc_coeff_precision = avctx->lpc_coeff_precision; } else { /* select LPC precision based on block size */ if( s->blocksize <= 192) s->options.lpc_coeff_precision = 7; else if(s->blocksize <= 384) s->options.lpc_coeff_precision = 8; else if(s->blocksize <= 576) s->options.lpc_coeff_precision = 9; else if(s->blocksize <= 1152) s->options.lpc_coeff_precision = 10; else if(s->blocksize <= 2304) s->options.lpc_coeff_precision = 11; else if(s->blocksize <= 4608) s->options.lpc_coeff_precision = 12; else if(s->blocksize <= 8192) s->options.lpc_coeff_precision = 13; else if(s->blocksize <= 16384) s->options.lpc_coeff_precision = 14; else s->options.lpc_coeff_precision = 15; } av_log(avctx, AV_LOG_DEBUG, " lpc precision: %d\n", s->options.lpc_coeff_precision); /* set maximum encoded frame size in verbatim mode */ if(s->channels == 2) { s->max_framesize = 14 + ((s->blocksize * 33 + 7) >> 3); } else { s->max_framesize = 14 + (s->blocksize * s->channels * 2); } streaminfo = av_malloc(FLAC_STREAMINFO_SIZE); write_streaminfo(s, streaminfo); avctx->extradata = streaminfo; avctx->extradata_size = FLAC_STREAMINFO_SIZE; s->frame_count = 0; avctx->coded_frame = avcodec_alloc_frame(); avctx->coded_frame->key_frame = 1; return 0;}static void init_frame(FlacEncodeContext *s){ int i, ch; FlacFrame *frame; frame = &s->frame; for(i=0; i<16; i++) { if(s->blocksize == flac_blocksizes[i]) { frame->blocksize = flac_blocksizes[i]; frame->bs_code[0] = i; frame->bs_code[1] = 0; break; } } if(i == 16) { frame->blocksize = s->blocksize; if(frame->blocksize <= 256) { frame->bs_code[0] = 6; frame->bs_code[1] = frame->blocksize-1; } else { frame->bs_code[0] = 7; frame->bs_code[1] = frame->blocksize-1; } } for(ch=0; ch<s->channels; ch++) { frame->subframes[ch].obits = 16; }}/** * Copy channel-interleaved input samples into separate subframes */static void copy_samples(FlacEncodeContext *s, int16_t *samples){ int i, j, ch; FlacFrame *frame; frame = &s->frame; for(i=0,j=0; i<frame->blocksize; i++) { for(ch=0; ch<s->channels; ch++,j++) { frame->subframes[ch].samples[i] = samples[j]; } }}#define rice_encode_count(sum, n, k) (((n)*((k)+1))+((sum-(n>>1))>>(k)))/** * Solve for d/dk(rice_encode_count) = n-((sum-(n>>1))>>(k+1)) = 0 */static int find_optimal_param(uint32_t sum, int n){ int k; uint32_t sum2; if(sum <= n>>1) return 0; sum2 = sum-(n>>1); k = av_log2(n<256 ? FASTDIV(sum2,n) : sum2/n); return FFMIN(k, MAX_RICE_PARAM);}static uint32_t calc_optimal_rice_params(RiceContext *rc, int porder, uint32_t *sums, int n, int pred_order){ int i; int k, cnt, part; uint32_t all_bits; part = (1 << porder); all_bits = 0; cnt = (n >> porder) - pred_order; for(i=0; i<part; i++) { if(i == 1) cnt = (n >> porder); k = find_optimal_param(sums[i], cnt); rc->params[i] = k; all_bits += rice_encode_count(sums[i], cnt, k); } all_bits += (4 * part); rc->porder = porder; return all_bits;}static void calc_sums(int pmin, int pmax, uint32_t *data, int n, int pred_order, uint32_t sums[][MAX_PARTITIONS]){ int i, j; int parts; uint32_t *res, *res_end; /* sums for highest level */ parts = (1 << pmax);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -