Revision f33aa120 libavcodec/flacenc.c

View differences:

libavcodec/flacenc.c
177 177
    s->blocksize = select_blocksize(s->samplerate);
178 178
    avctx->frame_size = s->blocksize;
179 179

  
180
    s->max_framesize = 14 + (s->blocksize * s->channels * 2);
180
    /* set maximum encoded frame size in verbatim mode */
181
    if(s->channels == 2) {
182
        s->max_framesize = 14 + ((s->blocksize * 33 + 7) >> 3);
183
    } else {
184
        s->max_framesize = 14 + (s->blocksize * s->channels * 2);
185
    }
181 186

  
182 187
    streaminfo = av_malloc(FLAC_STREAMINFO_SIZE);
183 188
    write_streaminfo(s, streaminfo);
......
192 197
    return 0;
193 198
}
194 199

  
195
static int init_frame(FlacEncodeContext *s)
200
static void init_frame(FlacEncodeContext *s)
196 201
{
197 202
    int i, ch;
198 203
    FlacFrame *frame;
......
221 226
    for(ch=0; ch<s->channels; ch++) {
222 227
        frame->subframes[ch].obits = 16;
223 228
    }
224
    if(s->channels == 2) {
225
        frame->ch_mode = FLAC_CHMODE_LEFT_RIGHT;
226
    } else {
227
        frame->ch_mode = FLAC_CHMODE_NOT_STEREO;
228
    }
229

  
230
    return 0;
231 229
}
232 230

  
233 231
/**
......
246 244
    }
247 245
}
248 246

  
247
static int estimate_stereo_mode(int32_t *left_ch, int32_t *right_ch, int n)
248
{
249
    int i, best;
250
    int32_t lt, rt;
251
    uint64_t left, right, mid, side;
252
    uint64_t score[4];
253

  
254
    /* calculate sum of squares for each channel */
255
    left = right = mid = side = 0;
256
    for(i=2; i<n; i++) {
257
        lt = left_ch[i] - 2*left_ch[i-1] + left_ch[i-2];
258
        rt = right_ch[i] - 2*right_ch[i-1] + right_ch[i-2];
259
        mid   += ABS((lt + rt) >> 1);
260
        side  += ABS(lt - rt);
261
        left  += ABS(lt);
262
        right += ABS(rt);
263
    }
264

  
265
    /* calculate score for each mode */
266
    score[0] = left  + right;
267
    score[1] = left  +  side;
268
    score[2] = right +  side;
269
    score[3] = mid   +  side;
270

  
271
    /* return mode with lowest score */
272
    best = 0;
273
    for(i=1; i<4; i++) {
274
        if(score[i] < score[best]) {
275
            best = i;
276
        }
277
    }
278
    if(best == 0) {
279
        return FLAC_CHMODE_LEFT_RIGHT;
280
    } else if(best == 1) {
281
        return FLAC_CHMODE_LEFT_SIDE;
282
    } else if(best == 2) {
283
        return FLAC_CHMODE_RIGHT_SIDE;
284
    } else {
285
        return FLAC_CHMODE_MID_SIDE;
286
    }
287
}
288

  
289
/**
290
 * Perform stereo channel decorrelation
291
 */
292
static void channel_decorrelation(FlacEncodeContext *ctx)
293
{
294
    FlacFrame *frame;
295
    int32_t *left, *right;
296
    int i, n;
297

  
298
    frame = &ctx->frame;
299
    n = frame->blocksize;
300
    left  = frame->subframes[0].samples;
301
    right = frame->subframes[1].samples;
302

  
303
    if(ctx->channels != 2) {
304
        frame->ch_mode = FLAC_CHMODE_NOT_STEREO;
305
        return;
306
    }
307

  
308
    frame->ch_mode = estimate_stereo_mode(left, right, n);
309

  
310
    /* perform decorrelation and adjust bits-per-sample */
311
    if(frame->ch_mode == FLAC_CHMODE_LEFT_RIGHT) {
312
        return;
313
    }
314
    if(frame->ch_mode == FLAC_CHMODE_MID_SIDE) {
315
        int32_t tmp;
316
        for(i=0; i<n; i++) {
317
            tmp = left[i];
318
            left[i] = (tmp + right[i]) >> 1;
319
            right[i] = tmp - right[i];
320
        }
321
        frame->subframes[1].obits++;
322
    } else if(frame->ch_mode == FLAC_CHMODE_LEFT_SIDE) {
323
        for(i=0; i<n; i++) {
324
            right[i] = left[i] - right[i];
325
        }
326
        frame->subframes[1].obits++;
327
    } else {
328
        for(i=0; i<n; i++) {
329
            left[i] -= right[i];
330
        }
331
        frame->subframes[0].obits++;
332
    }
333
}
334

  
249 335
static void encode_residual_verbatim(FlacEncodeContext *s, int ch)
250 336
{
251 337
    FlacFrame *frame;
......
359 445
    put_bits(&s->pb, 3, 4); /* bits-per-sample code */
360 446
    put_bits(&s->pb, 1, 0);
361 447
    write_utf8(&s->pb, s->frame_count);
362
    if(frame->bs_code[1] > 0) {
363
        if(frame->bs_code[1] < 256) {
364
            put_bits(&s->pb, 8, frame->bs_code[1]);
365
        } else {
366
            put_bits(&s->pb, 16, frame->bs_code[1]);
367
        }
448
    if(frame->bs_code[0] == 6) {
449
        put_bits(&s->pb, 8, frame->bs_code[1]);
450
    } else if(frame->bs_code[0] == 7) {
451
        put_bits(&s->pb, 16, frame->bs_code[1]);
368 452
    }
369
    if(s->sr_code[1] > 0) {
370
        if(s->sr_code[1] < 256) {
371
            put_bits(&s->pb, 8, s->sr_code[1]);
372
        } else {
373
            put_bits(&s->pb, 16, s->sr_code[1]);
374
        }
453
    if(s->sr_code[0] == 12) {
454
        put_bits(&s->pb, 8, s->sr_code[1]);
455
    } else if(s->sr_code[0] > 12) {
456
        put_bits(&s->pb, 16, s->sr_code[1]);
375 457
    }
376 458
    flush_put_bits(&s->pb);
377 459
    crc = av_crc(av_crc07, 0, s->pb.buf, put_bits_count(&s->pb)>>3);
......
493 575
    s = avctx->priv_data;
494 576

  
495 577
    s->blocksize = avctx->frame_size;
496
    if(init_frame(s)) {
497
        return 0;
498
    }
578
    init_frame(s);
499 579

  
500 580
    copy_samples(s, samples);
501 581

  
582
    channel_decorrelation(s);
583

  
502 584
    for(ch=0; ch<s->channels; ch++) {
503 585
        encode_residual(s, ch);
504 586
    }
......
532 614

  
533 615
static int flac_encode_close(AVCodecContext *avctx)
534 616
{
617
    av_freep(&avctx->extradata);
618
    avctx->extradata_size = 0;
535 619
    av_freep(&avctx->coded_frame);
536 620
    return 0;
537 621
}

Also available in: Unified diff