Revision a9f8587e libavcodec/flacenc.c

View differences:

libavcodec/flacenc.c
21 21

  
22 22
#include "libavutil/crc.h"
23 23
#include "libavutil/lls.h"
24
#include "libavutil/md5.h"
24 25
#include "avcodec.h"
25 26
#include "bitstream.h"
26 27
#include "dsputil.h"
......
97 98
    int max_framesize;
98 99
    uint32_t frame_count;
99 100
    uint64_t sample_count;
101
    uint8_t md5sum[16];
100 102
    FlacFrame frame;
101 103
    CompressionOptions options;
102 104
    AVCodecContext *avctx;
103 105
    DSPContext dsp;
106
    struct AVMD5 *md5ctx;
104 107
} FlacEncodeContext;
105 108

  
106 109
static const int flac_samplerates[16] = {
......
139 142
    put_bits(&pb, 24, (s->sample_count & 0xFFFFFF000LL) >> 12);
140 143
    put_bits(&pb, 12,  s->sample_count & 0x000000FFFLL);
141 144
    flush_put_bits(&pb);
142
    /* MD5 signature = 0 */
145
    memcpy(&header[18], s->md5sum, 16);
143 146
}
144 147

  
145 148
/**
......
372 375
        s->max_framesize = 14 + (s->avctx->frame_size * s->channels * 2);
373 376
    }
374 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);
383

  
375 384
    streaminfo = av_malloc(FLAC_STREAMINFO_SIZE);
376 385
    write_streaminfo(s, streaminfo);
377 386
    avctx->extradata = streaminfo;
......
1238 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 1263
static int flac_encode_frame(AVCodecContext *avctx, uint8_t *frame,
1242 1264
                             int buf_size, void *data)
1243 1265
{
......
1256 1278

  
1257 1279
    /* when the last block is reached, update the header in extradata */
1258 1280
    if (!data) {
1281
        av_md5_final(s->md5ctx, s->md5sum);
1259 1282
        write_streaminfo(s, avctx->extradata);
1260 1283
        return 0;
1261 1284
    }
......
1294 1317

  
1295 1318
    s->frame_count++;
1296 1319
    s->sample_count += avctx->frame_size;
1320
    update_md5_sum(s, samples);
1297 1321

  
1298 1322
    return out_bytes;
1299 1323
}
1300 1324

  
1301 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 1331
    av_freep(&avctx->extradata);
1304 1332
    avctx->extradata_size = 0;
1305 1333
    av_freep(&avctx->coded_frame);

Also available in: Unified diff