742
int c = coefs[(x)-1];\
748
static av_always_inline void encode_residual_lpc_unrolled(int32_t *res,
749
const int32_t *smp, int n, int order,
750
const int32_t *coefs, int shift, int big)
753
for (i = order; i < n; i += 2) {
754
int s = smp[i-order];
803
res[i ] = smp[i ] - (p0 >> shift);
804
res[i+1] = smp[i+1] - (p1 >> shift);
809
static void encode_residual_lpc(int32_t *res, const int32_t *smp, int n,
810
int order, const int32_t *coefs, int shift)
813
for (i = 0; i < order; i++)
816
for (i = order; i < n; i += 2) {
820
for (j = 0; j < order; j++) {
826
res[i ] = smp[i ] - (p0 >> shift);
827
res[i+1] = smp[i+1] - (p1 >> shift);
831
case 1: encode_residual_lpc_unrolled(res, smp, n, 1, coefs, shift, 0); break;
832
case 2: encode_residual_lpc_unrolled(res, smp, n, 2, coefs, shift, 0); break;
833
case 3: encode_residual_lpc_unrolled(res, smp, n, 3, coefs, shift, 0); break;
834
case 4: encode_residual_lpc_unrolled(res, smp, n, 4, coefs, shift, 0); break;
835
case 5: encode_residual_lpc_unrolled(res, smp, n, 5, coefs, shift, 0); break;
836
case 6: encode_residual_lpc_unrolled(res, smp, n, 6, coefs, shift, 0); break;
837
case 7: encode_residual_lpc_unrolled(res, smp, n, 7, coefs, shift, 0); break;
838
case 8: encode_residual_lpc_unrolled(res, smp, n, 8, coefs, shift, 0); break;
839
default: encode_residual_lpc_unrolled(res, smp, n, order, coefs, shift, 1); break;
845
729
static int encode_residual_ch(FlacEncodeContext *s, int ch)
1249
static void update_md5_sum(FlacEncodeContext *s, const int16_t *samples)
1170
static int update_md5_sum(FlacEncodeContext *s, const void *samples)
1173
int buf_size = s->frame.blocksize * s->channels *
1174
((s->avctx->bits_per_raw_sample + 7) / 8);
1176
if (s->avctx->bits_per_raw_sample > 16 || HAVE_BIGENDIAN) {
1177
av_fast_malloc(&s->md5_buffer, &s->md5_buffer_size, buf_size);
1179
return AVERROR(ENOMEM);
1182
if (s->avctx->bits_per_raw_sample <= 16) {
1183
buf = (const uint8_t *)samples;
1251
1184
#if HAVE_BIGENDIAN
1253
for (i = 0; i < s->frame.blocksize * s->channels; i++) {
1254
int16_t smp = av_le2ne16(samples[i]);
1255
av_md5_update(s->md5ctx, (uint8_t *)&smp, 2);
1185
s->dsp.bswap16_buf((uint16_t *)s->md5_buffer,
1186
(const uint16_t *)samples, buf_size / 2);
1187
buf = s->md5_buffer;
1191
const int32_t *samples0 = samples;
1192
uint8_t *tmp = s->md5_buffer;
1194
for (i = 0; i < s->frame.blocksize * s->channels; i++) {
1195
int32_t v = samples0[i] >> 8;
1196
*tmp++ = (v ) & 0xFF;
1197
*tmp++ = (v >> 8) & 0xFF;
1198
*tmp++ = (v >> 16) & 0xFF;
1200
buf = s->md5_buffer;
1258
av_md5_update(s->md5ctx, (const uint8_t *)samples, s->frame.blocksize*s->channels*2);
1202
av_md5_update(s->md5ctx, buf, buf_size);
1263
static int flac_encode_frame(AVCodecContext *avctx, uint8_t *frame,
1264
int buf_size, void *data)
1208
static int flac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
1209
const AVFrame *frame, int *got_packet_ptr)
1266
1211
FlacEncodeContext *s;
1267
const int16_t *samples = data;
1268
int frame_bytes, out_bytes;
1212
int frame_bytes, out_bytes, ret;
1270
1214
s = avctx->priv_data;
1272
1216
/* when the last block is reached, update the header in extradata */
1274
1218
s->max_framesize = s->max_encoded_framesize;
1275
1219
av_md5_final(s->md5ctx, s->md5sum);
1276
1220
write_streaminfo(s, avctx->extradata);
1280
1224
/* change max_framesize for small final frame */
1281
if (avctx->frame_size < s->frame.blocksize) {
1282
s->max_framesize = ff_flac_get_max_frame_size(avctx->frame_size,
1225
if (frame->nb_samples < s->frame.blocksize) {
1226
s->max_framesize = ff_flac_get_max_frame_size(frame->nb_samples,
1228
avctx->bits_per_raw_sample);
1231
init_frame(s, frame->nb_samples);
1288
copy_samples(s, samples);
1233
copy_samples(s, frame->data[0]);
1290
1235
channel_decorrelation(s);
1237
remove_wasted_bits(s);
1292
1239
frame_bytes = encode_frame(s);
1294
1241
/* fallback to verbatim mode if the compressed frame is larger than it
1295
1242
would be if encoded uncompressed. */
1296
if (frame_bytes > s->max_framesize) {
1243
if (frame_bytes < 0 || frame_bytes > s->max_framesize) {
1297
1244
s->frame.verbatim_only = 1;
1298
1245
frame_bytes = encode_frame(s);
1301
if (buf_size < frame_bytes) {
1302
av_log(avctx, AV_LOG_ERROR, "output buffer too small\n");
1305
out_bytes = write_frame(s, frame, buf_size);
1246
if (frame_bytes < 0) {
1247
av_log(avctx, AV_LOG_ERROR, "Bad frame count\n");
1252
if ((ret = ff_alloc_packet(avpkt, frame_bytes))) {
1253
av_log(avctx, AV_LOG_ERROR, "Error getting output packet\n");
1257
out_bytes = write_frame(s, avpkt);
1307
1259
s->frame_count++;
1308
avctx->coded_frame->pts = s->sample_count;
1309
s->sample_count += avctx->frame_size;
1310
update_md5_sum(s, samples);
1260
s->sample_count += frame->nb_samples;
1261
if ((ret = update_md5_sum(s, frame->data[0])) < 0) {
1262
av_log(avctx, AV_LOG_ERROR, "Error updating MD5 checksum\n");
1311
1265
if (out_bytes > s->max_encoded_framesize)
1312
1266
s->max_encoded_framesize = out_bytes;
1313
1267
if (out_bytes < s->min_framesize)
1314
1268
s->min_framesize = out_bytes;
1270
avpkt->pts = frame->pts;
1271
avpkt->duration = ff_samples_to_time_base(avctx, frame->nb_samples);
1272
avpkt->size = out_bytes;
1273
*got_packet_ptr = 1;
1322
1280
if (avctx->priv_data) {
1323
1281
FlacEncodeContext *s = avctx->priv_data;
1324
1282
av_freep(&s->md5ctx);
1283
av_freep(&s->md5_buffer);
1325
1284
ff_lpc_end(&s->lpc_ctx);
1327
1286
av_freep(&avctx->extradata);
1328
1287
avctx->extradata_size = 0;
1288
#if FF_API_OLD_ENCODE_AUDIO
1329
1289
av_freep(&avctx->coded_frame);
1333
1294
#define FLAGS AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM
1334
1295
static const AVOption options[] = {
1335
{ "lpc_coeff_precision", "LPC coefficient precision", offsetof(FlacEncodeContext, options.lpc_coeff_precision), AV_OPT_TYPE_INT, {.dbl = 15 }, 0, MAX_LPC_PRECISION, FLAGS },
1336
{ "lpc_type", "LPC algorithm", offsetof(FlacEncodeContext, options.lpc_type), AV_OPT_TYPE_INT, {.dbl = FF_LPC_TYPE_DEFAULT }, FF_LPC_TYPE_DEFAULT, FF_LPC_TYPE_NB-1, FLAGS, "lpc_type" },
1337
{ "none", NULL, 0, AV_OPT_TYPE_CONST, {.dbl = FF_LPC_TYPE_NONE }, INT_MIN, INT_MAX, FLAGS, "lpc_type" },
1338
{ "fixed", NULL, 0, AV_OPT_TYPE_CONST, {.dbl = FF_LPC_TYPE_FIXED }, INT_MIN, INT_MAX, FLAGS, "lpc_type" },
1339
{ "levinson", NULL, 0, AV_OPT_TYPE_CONST, {.dbl = FF_LPC_TYPE_LEVINSON }, INT_MIN, INT_MAX, FLAGS, "lpc_type" },
1340
{ "cholesky", NULL, 0, AV_OPT_TYPE_CONST, {.dbl = FF_LPC_TYPE_CHOLESKY }, INT_MIN, INT_MAX, FLAGS, "lpc_type" },
1341
{ "lpc_passes", "Number of passes to use for Cholesky factorization during LPC analysis", offsetof(FlacEncodeContext, options.lpc_passes), AV_OPT_TYPE_INT, {.dbl = -1 }, INT_MIN, INT_MAX, FLAGS },
1342
{ "min_partition_order", NULL, offsetof(FlacEncodeContext, options.min_partition_order), AV_OPT_TYPE_INT, {.dbl = -1 }, -1, MAX_PARTITION_ORDER, FLAGS },
1343
{ "max_partition_order", NULL, offsetof(FlacEncodeContext, options.max_partition_order), AV_OPT_TYPE_INT, {.dbl = -1 }, -1, MAX_PARTITION_ORDER, FLAGS },
1344
{ "prediction_order_method", "Search method for selecting prediction order", offsetof(FlacEncodeContext, options.prediction_order_method), AV_OPT_TYPE_INT, {.dbl = -1 }, -1, ORDER_METHOD_LOG, FLAGS, "predm" },
1345
{ "estimation", NULL, 0, AV_OPT_TYPE_CONST, {.dbl = ORDER_METHOD_EST }, INT_MIN, INT_MAX, FLAGS, "predm" },
1346
{ "2level", NULL, 0, AV_OPT_TYPE_CONST, {.dbl = ORDER_METHOD_2LEVEL }, INT_MIN, INT_MAX, FLAGS, "predm" },
1347
{ "4level", NULL, 0, AV_OPT_TYPE_CONST, {.dbl = ORDER_METHOD_4LEVEL }, INT_MIN, INT_MAX, FLAGS, "predm" },
1348
{ "8level", NULL, 0, AV_OPT_TYPE_CONST, {.dbl = ORDER_METHOD_8LEVEL }, INT_MIN, INT_MAX, FLAGS, "predm" },
1349
{ "search", NULL, 0, AV_OPT_TYPE_CONST, {.dbl = ORDER_METHOD_SEARCH }, INT_MIN, INT_MAX, FLAGS, "predm" },
1350
{ "log", NULL, 0, AV_OPT_TYPE_CONST, {.dbl = ORDER_METHOD_LOG }, INT_MIN, INT_MAX, FLAGS, "predm" },
1296
{ "lpc_coeff_precision", "LPC coefficient precision", offsetof(FlacEncodeContext, options.lpc_coeff_precision), AV_OPT_TYPE_INT, {.i64 = 15 }, 0, MAX_LPC_PRECISION, FLAGS },
1297
{ "lpc_type", "LPC algorithm", offsetof(FlacEncodeContext, options.lpc_type), AV_OPT_TYPE_INT, {.i64 = FF_LPC_TYPE_DEFAULT }, FF_LPC_TYPE_DEFAULT, FF_LPC_TYPE_NB-1, FLAGS, "lpc_type" },
1298
{ "none", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = FF_LPC_TYPE_NONE }, INT_MIN, INT_MAX, FLAGS, "lpc_type" },
1299
{ "fixed", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = FF_LPC_TYPE_FIXED }, INT_MIN, INT_MAX, FLAGS, "lpc_type" },
1300
{ "levinson", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = FF_LPC_TYPE_LEVINSON }, INT_MIN, INT_MAX, FLAGS, "lpc_type" },
1301
{ "cholesky", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = FF_LPC_TYPE_CHOLESKY }, INT_MIN, INT_MAX, FLAGS, "lpc_type" },
1302
{ "lpc_passes", "Number of passes to use for Cholesky factorization during LPC analysis", offsetof(FlacEncodeContext, options.lpc_passes), AV_OPT_TYPE_INT, {.i64 = 1 }, 1, INT_MAX, FLAGS },
1303
{ "min_partition_order", NULL, offsetof(FlacEncodeContext, options.min_partition_order), AV_OPT_TYPE_INT, {.i64 = -1 }, -1, MAX_PARTITION_ORDER, FLAGS },
1304
{ "max_partition_order", NULL, offsetof(FlacEncodeContext, options.max_partition_order), AV_OPT_TYPE_INT, {.i64 = -1 }, -1, MAX_PARTITION_ORDER, FLAGS },
1305
{ "prediction_order_method", "Search method for selecting prediction order", offsetof(FlacEncodeContext, options.prediction_order_method), AV_OPT_TYPE_INT, {.i64 = -1 }, -1, ORDER_METHOD_LOG, FLAGS, "predm" },
1306
{ "estimation", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = ORDER_METHOD_EST }, INT_MIN, INT_MAX, FLAGS, "predm" },
1307
{ "2level", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = ORDER_METHOD_2LEVEL }, INT_MIN, INT_MAX, FLAGS, "predm" },
1308
{ "4level", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = ORDER_METHOD_4LEVEL }, INT_MIN, INT_MAX, FLAGS, "predm" },
1309
{ "8level", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = ORDER_METHOD_8LEVEL }, INT_MIN, INT_MAX, FLAGS, "predm" },
1310
{ "search", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = ORDER_METHOD_SEARCH }, INT_MIN, INT_MAX, FLAGS, "predm" },
1311
{ "log", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = ORDER_METHOD_LOG }, INT_MIN, INT_MAX, FLAGS, "predm" },
1312
{ "ch_mode", "Stereo decorrelation mode", offsetof(FlacEncodeContext, options.ch_mode), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, FLAC_CHMODE_MID_SIDE, FLAGS, "ch_mode" },
1313
{ "auto", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = -1 }, INT_MIN, INT_MAX, FLAGS, "ch_mode" },
1314
{ "indep", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FLAC_CHMODE_INDEPENDENT }, INT_MIN, INT_MAX, FLAGS, "ch_mode" },
1315
{ "left_side", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FLAC_CHMODE_LEFT_SIDE }, INT_MIN, INT_MAX, FLAGS, "ch_mode" },
1316
{ "right_side", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FLAC_CHMODE_RIGHT_SIDE }, INT_MIN, INT_MAX, FLAGS, "ch_mode" },
1317
{ "mid_side", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FLAC_CHMODE_MID_SIDE }, INT_MIN, INT_MAX, FLAGS, "ch_mode" },