comparison flacenc.c @ 12321:b2940a8f3db6 libavcodec

Remove duplicate code by adding a flag for encoding in verbatim mode.
author jbr
date Sat, 31 Jul 2010 16:46:32 +0000
parents 014e19fbfae7
children 530bf62f3940
comparison
equal deleted inserted replaced
12320:035ca6548e29 12321:b2940a8f3db6
75 FlacSubframe subframes[FLAC_MAX_CHANNELS]; 75 FlacSubframe subframes[FLAC_MAX_CHANNELS];
76 int blocksize; 76 int blocksize;
77 int bs_code[2]; 77 int bs_code[2];
78 uint8_t crc8; 78 uint8_t crc8;
79 int ch_mode; 79 int ch_mode;
80 int verbatim_only;
80 } FlacFrame; 81 } FlacFrame;
81 82
82 typedef struct FlacEncodeContext { 83 typedef struct FlacEncodeContext {
83 PutBitContext pb; 84 PutBitContext pb;
84 int channels; 85 int channels;
470 } 471 }
471 } 472 }
472 473
473 for (ch = 0; ch < s->channels; ch++) 474 for (ch = 0; ch < s->channels; ch++)
474 frame->subframes[ch].obits = 16; 475 frame->subframes[ch].obits = 16;
476
477 frame->verbatim_only = 0;
475 } 478 }
476 479
477 480
478 /** 481 /**
479 * Copy channel-interleaved input samples into separate subframes. 482 * Copy channel-interleaved input samples into separate subframes.
817 res[0] = smp[0]; 820 res[0] = smp[0];
818 return sub->obits; 821 return sub->obits;
819 } 822 }
820 823
821 /* VERBATIM */ 824 /* VERBATIM */
822 if (n < 5) { 825 if (frame->verbatim_only || n < 5) {
823 sub->type = sub->type_code = FLAC_SUBFRAME_VERBATIM; 826 sub->type = sub->type_code = FLAC_SUBFRAME_VERBATIM;
824 encode_residual_verbatim(res, smp, n); 827 encode_residual_verbatim(res, smp, n);
825 return sub->obits * n; 828 return sub->obits * n;
826 } 829 }
827 830
983 986
984 return count >> 3; 987 return count >> 3;
985 } 988 }
986 989
987 990
988 static int encode_residual_v(FlacEncodeContext *s, int ch)
989 {
990 int i, n;
991 FlacFrame *frame;
992 FlacSubframe *sub;
993 int32_t *res, *smp;
994
995 frame = &s->frame;
996 sub = &frame->subframes[ch];
997 res = sub->residual;
998 smp = sub->samples;
999 n = frame->blocksize;
1000
1001 /* CONSTANT */
1002 for (i = 1; i < n; i++)
1003 if (smp[i] != smp[0])
1004 break;
1005 if (i == n) {
1006 sub->type = sub->type_code = FLAC_SUBFRAME_CONSTANT;
1007 res[0] = smp[0];
1008 return sub->obits;
1009 }
1010
1011 /* VERBATIM */
1012 sub->type = sub->type_code = FLAC_SUBFRAME_VERBATIM;
1013 encode_residual_verbatim(res, smp, n);
1014 return sub->obits * n;
1015 }
1016
1017
1018 static int estimate_stereo_mode(int32_t *left_ch, int32_t *right_ch, int n) 991 static int estimate_stereo_mode(int32_t *left_ch, int32_t *right_ch, int n)
1019 { 992 {
1020 int i, best; 993 int i, best;
1021 int32_t lt, rt; 994 int32_t lt, rt;
1022 uint64_t sum[4]; 995 uint64_t sum[4];
1234 1207
1235 1208
1236 static int flac_encode_frame(AVCodecContext *avctx, uint8_t *frame, 1209 static int flac_encode_frame(AVCodecContext *avctx, uint8_t *frame,
1237 int buf_size, void *data) 1210 int buf_size, void *data)
1238 { 1211 {
1239 int ch;
1240 FlacEncodeContext *s; 1212 FlacEncodeContext *s;
1241 const int16_t *samples = data; 1213 const int16_t *samples = data;
1242 int out_bytes; 1214 int out_bytes;
1243 int reencoded=0; 1215 int reencoded=0;
1244 1216
1278 av_log(avctx, AV_LOG_ERROR, "error encoding frame\n"); 1250 av_log(avctx, AV_LOG_ERROR, "error encoding frame\n");
1279 return -1; 1251 return -1;
1280 } 1252 }
1281 1253
1282 /* frame too large. use verbatim mode */ 1254 /* frame too large. use verbatim mode */
1283 for (ch = 0; ch < s->channels; ch++) 1255 s->frame.verbatim_only = 1;
1284 encode_residual_v(s, ch); 1256 encode_frame(s);
1285 reencoded = 1; 1257 reencoded = 1;
1286 goto write_frame; 1258 goto write_frame;
1287 } 1259 }
1288 1260
1289 s->frame_count++; 1261 s->frame_count++;