comparison flacenc.c @ 7861:f092dd630095 libavcodec

write MD5 checksum to FLAC header. update regression tests.
author jbr
date Sun, 14 Sep 2008 21:39:54 +0000
parents c11e175fc544
children 4820bd751284
comparison
equal deleted inserted replaced
7860:c11e175fc544 7861:f092dd630095
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */ 20 */
21 21
22 #include "libavutil/crc.h" 22 #include "libavutil/crc.h"
23 #include "libavutil/lls.h" 23 #include "libavutil/lls.h"
24 #include "libavutil/md5.h"
24 #include "avcodec.h" 25 #include "avcodec.h"
25 #include "bitstream.h" 26 #include "bitstream.h"
26 #include "dsputil.h" 27 #include "dsputil.h"
27 #include "golomb.h" 28 #include "golomb.h"
28 #include "lpc.h" 29 #include "lpc.h"
95 int samplerate; 96 int samplerate;
96 int sr_code[2]; 97 int sr_code[2];
97 int max_framesize; 98 int max_framesize;
98 uint32_t frame_count; 99 uint32_t frame_count;
99 uint64_t sample_count; 100 uint64_t sample_count;
101 uint8_t md5sum[16];
100 FlacFrame frame; 102 FlacFrame frame;
101 CompressionOptions options; 103 CompressionOptions options;
102 AVCodecContext *avctx; 104 AVCodecContext *avctx;
103 DSPContext dsp; 105 DSPContext dsp;
106 struct AVMD5 *md5ctx;
104 } FlacEncodeContext; 107 } FlacEncodeContext;
105 108
106 static const int flac_samplerates[16] = { 109 static const int flac_samplerates[16] = {
107 0, 0, 0, 0, 110 0, 0, 0, 0,
108 8000, 16000, 22050, 24000, 32000, 44100, 48000, 96000, 111 8000, 16000, 22050, 24000, 32000, 44100, 48000, 96000,
137 put_bits(&pb, 5, 15); /* bits per sample - 1 */ 140 put_bits(&pb, 5, 15); /* bits per sample - 1 */
138 /* write 36-bit sample count in 2 put_bits() calls */ 141 /* write 36-bit sample count in 2 put_bits() calls */
139 put_bits(&pb, 24, (s->sample_count & 0xFFFFFF000LL) >> 12); 142 put_bits(&pb, 24, (s->sample_count & 0xFFFFFF000LL) >> 12);
140 put_bits(&pb, 12, s->sample_count & 0x000000FFFLL); 143 put_bits(&pb, 12, s->sample_count & 0x000000FFFLL);
141 flush_put_bits(&pb); 144 flush_put_bits(&pb);
142 /* MD5 signature = 0 */ 145 memcpy(&header[18], s->md5sum, 16);
143 } 146 }
144 147
145 /** 148 /**
146 * Sets blocksize based on samplerate 149 * Sets blocksize based on samplerate
147 * Chooses the closest predefined blocksize >= BLOCK_TIME_MS milliseconds 150 * Chooses the closest predefined blocksize >= BLOCK_TIME_MS milliseconds
369 if(s->channels == 2) { 372 if(s->channels == 2) {
370 s->max_framesize = 14 + ((s->avctx->frame_size * 33 + 7) >> 3); 373 s->max_framesize = 14 + ((s->avctx->frame_size * 33 + 7) >> 3);
371 } else { 374 } else {
372 s->max_framesize = 14 + (s->avctx->frame_size * s->channels * 2); 375 s->max_framesize = 14 + (s->avctx->frame_size * s->channels * 2);
373 } 376 }
377
378 /* initialize MD5 context */
379 s->md5ctx = av_malloc(av_md5_size);
380 if(!s->md5ctx)
381 return AVERROR_NOMEM;
382 av_md5_init(s->md5ctx);
374 383
375 streaminfo = av_malloc(FLAC_STREAMINFO_SIZE); 384 streaminfo = av_malloc(FLAC_STREAMINFO_SIZE);
376 write_streaminfo(s, streaminfo); 385 write_streaminfo(s, streaminfo);
377 avctx->extradata = streaminfo; 386 avctx->extradata = streaminfo;
378 avctx->extradata_size = FLAC_STREAMINFO_SIZE; 387 avctx->extradata_size = FLAC_STREAMINFO_SIZE;
1236 s->pb.buf, put_bits_count(&s->pb)>>3)); 1245 s->pb.buf, put_bits_count(&s->pb)>>3));
1237 put_bits(&s->pb, 16, crc); 1246 put_bits(&s->pb, 16, crc);
1238 flush_put_bits(&s->pb); 1247 flush_put_bits(&s->pb);
1239 } 1248 }
1240 1249
1250 static void update_md5_sum(FlacEncodeContext *s, int16_t *samples)
1251 {
1252 #ifdef WORDS_BIGENDIAN
1253 int i;
1254 for(i = 0; i < s->frame.blocksize*s->channels; i++) {
1255 int16_t smp = le2me_16(samples[i]);
1256 av_md5_update(s->md5ctx, (uint8_t *)&smp, 2);
1257 }
1258 #else
1259 av_md5_update(s->md5ctx, (uint8_t *)samples, s->frame.blocksize*s->channels*2);
1260 #endif
1261 }
1262
1241 static int flac_encode_frame(AVCodecContext *avctx, uint8_t *frame, 1263 static int flac_encode_frame(AVCodecContext *avctx, uint8_t *frame,
1242 int buf_size, void *data) 1264 int buf_size, void *data)
1243 { 1265 {
1244 int ch; 1266 int ch;
1245 FlacEncodeContext *s; 1267 FlacEncodeContext *s;
1254 return 0; 1276 return 0;
1255 } 1277 }
1256 1278
1257 /* when the last block is reached, update the header in extradata */ 1279 /* when the last block is reached, update the header in extradata */
1258 if (!data) { 1280 if (!data) {
1281 av_md5_final(s->md5ctx, s->md5sum);
1259 write_streaminfo(s, avctx->extradata); 1282 write_streaminfo(s, avctx->extradata);
1260 return 0; 1283 return 0;
1261 } 1284 }
1262 1285
1263 init_frame(s); 1286 init_frame(s);
1292 goto write_frame; 1315 goto write_frame;
1293 } 1316 }
1294 1317
1295 s->frame_count++; 1318 s->frame_count++;
1296 s->sample_count += avctx->frame_size; 1319 s->sample_count += avctx->frame_size;
1320 update_md5_sum(s, samples);
1297 1321
1298 return out_bytes; 1322 return out_bytes;
1299 } 1323 }
1300 1324
1301 static av_cold int flac_encode_close(AVCodecContext *avctx) 1325 static av_cold int flac_encode_close(AVCodecContext *avctx)
1302 { 1326 {
1327 if (avctx->priv_data) {
1328 FlacEncodeContext *s = avctx->priv_data;
1329 av_freep(&s->md5ctx);
1330 }
1303 av_freep(&avctx->extradata); 1331 av_freep(&avctx->extradata);
1304 avctx->extradata_size = 0; 1332 avctx->extradata_size = 0;
1305 av_freep(&avctx->coded_frame); 1333 av_freep(&avctx->coded_frame);
1306 return 0; 1334 return 0;
1307 } 1335 }