~medibuntu-maintainers/mplayer/medibuntu.precise

« back to all changes in this revision

Viewing changes to ffmpeg/libavcodec/alacenc.c

  • Committer: Package Import Robot
  • Author(s): Reinhard Tartler
  • Date: 2012-01-12 22:23:28 UTC
  • mfrom: (0.4.7 sid)
  • mto: This revision was merged to the branch mainline in revision 76.
  • Revision ID: package-import@ubuntu.com-20120112222328-8jqdyodym3p84ygu
Tags: 2:1.0~rc4.dfsg1+svn34540-1
* New upstream snapshot
* upload to unstable

Show diffs side-by-side

added added

removed removed

Lines of Context:
75
75
} AlacEncodeContext;
76
76
 
77
77
 
78
 
static void init_sample_buffers(AlacEncodeContext *s, const int16_t *input_samples)
 
78
static void init_sample_buffers(AlacEncodeContext *s,
 
79
                                const int16_t *input_samples)
79
80
{
80
81
    int ch, i;
81
82
 
82
 
    for(ch=0;ch<s->avctx->channels;ch++) {
 
83
    for (ch = 0; ch < s->avctx->channels; ch++) {
83
84
        const int16_t *sptr = input_samples + ch;
84
 
        for(i=0;i<s->avctx->frame_size;i++) {
 
85
        for (i = 0; i < s->avctx->frame_size; i++) {
85
86
            s->sample_buf[ch][i] = *sptr;
86
87
            sptr += s->avctx->channels;
87
88
        }
88
89
    }
89
90
}
90
91
 
91
 
static void encode_scalar(AlacEncodeContext *s, int x, int k, int write_sample_size)
 
92
static void encode_scalar(AlacEncodeContext *s, int x,
 
93
                          int k, int write_sample_size)
92
94
{
93
95
    int divisor, q, r;
94
96
 
97
99
    q = x / divisor;
98
100
    r = x % divisor;
99
101
 
100
 
    if(q > 8) {
 
102
    if (q > 8) {
101
103
        // write escape code and sample value directly
102
104
        put_bits(&s->pbctx, 9, ALAC_ESCAPE_CODE);
103
105
        put_bits(&s->pbctx, write_sample_size, x);
104
106
    } else {
105
 
        if(q)
 
107
        if (q)
106
108
            put_bits(&s->pbctx, q, (1<<q) - 1);
107
109
        put_bits(&s->pbctx, 1, 0);
108
110
 
109
 
        if(k != 1) {
110
 
            if(r > 0)
 
111
        if (k != 1) {
 
112
            if (r > 0)
111
113
                put_bits(&s->pbctx, k, r+1);
112
114
            else
113
115
                put_bits(&s->pbctx, k-1, 0);
164
166
 
165
167
    /* calculate sum of 2nd order residual for each channel */
166
168
    sum[0] = sum[1] = sum[2] = sum[3] = 0;
167
 
    for(i=2; i<n; i++) {
 
169
    for (i = 2; i < n; i++) {
168
170
        lt = left_ch[i] - 2*left_ch[i-1] + left_ch[i-2];
169
171
        rt = right_ch[i] - 2*right_ch[i-1] + right_ch[i-2];
170
172
        sum[2] += FFABS((lt + rt) >> 1);
181
183
 
182
184
    /* return mode with lowest score */
183
185
    best = 0;
184
 
    for(i=1; i<4; i++) {
185
 
        if(score[i] < score[best]) {
 
186
    for (i = 1; i < 4; i++) {
 
187
        if (score[i] < score[best]) {
186
188
            best = i;
187
189
        }
188
190
    }
205
207
            break;
206
208
 
207
209
        case ALAC_CHMODE_LEFT_SIDE:
208
 
            for(i=0; i<n; i++) {
 
210
            for (i = 0; i < n; i++) {
209
211
                right[i] = left[i] - right[i];
210
212
            }
211
213
            s->interlacing_leftweight = 1;
213
215
            break;
214
216
 
215
217
        case ALAC_CHMODE_RIGHT_SIDE:
216
 
            for(i=0; i<n; i++) {
 
218
            for (i = 0; i < n; i++) {
217
219
                tmp = right[i];
218
220
                right[i] = left[i] - right[i];
219
221
                left[i] = tmp + (right[i] >> 31);
223
225
            break;
224
226
 
225
227
        default:
226
 
            for(i=0; i<n; i++) {
 
228
            for (i = 0; i < n; i++) {
227
229
                tmp = left[i];
228
230
                left[i] = (tmp + right[i]) >> 1;
229
231
                right[i] = tmp - right[i];
239
241
    int i;
240
242
    AlacLPCContext lpc = s->lpc[ch];
241
243
 
242
 
    if(lpc.lpc_order == 31) {
 
244
    if (lpc.lpc_order == 31) {
243
245
        s->predictor_buf[0] = s->sample_buf[ch][0];
244
246
 
245
 
        for(i=1; i<s->avctx->frame_size; i++)
 
247
        for (i = 1; i < s->avctx->frame_size; i++)
246
248
            s->predictor_buf[i] = s->sample_buf[ch][i] - s->sample_buf[ch][i-1];
247
249
 
248
250
        return;
250
252
 
251
253
    // generalised linear predictor
252
254
 
253
 
    if(lpc.lpc_order > 0) {
 
255
    if (lpc.lpc_order > 0) {
254
256
        int32_t *samples  = s->sample_buf[ch];
255
257
        int32_t *residual = s->predictor_buf;
256
258
 
257
259
        // generate warm-up samples
258
260
        residual[0] = samples[0];
259
 
        for(i=1;i<=lpc.lpc_order;i++)
 
261
        for (i = 1; i <= lpc.lpc_order; i++)
260
262
            residual[i] = samples[i] - samples[i-1];
261
263
 
262
264
        // perform lpc on remaining samples
263
 
        for(i = lpc.lpc_order + 1; i < s->avctx->frame_size; i++) {
 
265
        for (i = lpc.lpc_order + 1; i < s->avctx->frame_size; i++) {
264
266
            int sum = 1 << (lpc.lpc_quant - 1), res_val, j;
265
267
 
266
268
            for (j = 0; j < lpc.lpc_order; j++) {
303
305
    int sign_modifier = 0, i, k;
304
306
    int32_t *samples = s->predictor_buf;
305
307
 
306
 
    for(i=0;i < s->avctx->frame_size;) {
 
308
    for (i = 0; i < s->avctx->frame_size;) {
307
309
        int x;
308
310
 
309
311
        k = av_log2((history >> 9) + 3);
320
322
                   - ((history * s->rc.history_mult) >> 9);
321
323
 
322
324
        sign_modifier = 0;
323
 
        if(x > 0xFFFF)
 
325
        if (x > 0xFFFF)
324
326
            history = 0xFFFF;
325
327
 
326
 
        if((history < 128) && (i < s->avctx->frame_size)) {
 
328
        if (history < 128 && i < s->avctx->frame_size) {
327
329
            unsigned int block_size = 0;
328
330
 
329
331
            k = 7 - av_log2(history) + ((history + 16) >> 6);
330
332
 
331
 
            while((*samples == 0) && (i < s->avctx->frame_size)) {
 
333
            while (*samples == 0 && i < s->avctx->frame_size) {
332
334
                samples++;
333
335
                i++;
334
336
                block_size++;
346
348
static void write_compressed_frame(AlacEncodeContext *s)
347
349
{
348
350
    int i, j;
 
351
    int prediction_type = 0;
349
352
 
350
 
    if(s->avctx->channels == 2)
 
353
    if (s->avctx->channels == 2)
351
354
        alac_stereo_decorrelation(s);
352
355
    put_bits(&s->pbctx, 8, s->interlacing_shift);
353
356
    put_bits(&s->pbctx, 8, s->interlacing_leftweight);
354
357
 
355
 
    for(i=0;i<s->avctx->channels;i++) {
 
358
    for (i = 0; i < s->avctx->channels; i++) {
356
359
 
357
360
        calc_predictor_params(s, i);
358
361
 
359
 
        put_bits(&s->pbctx, 4, 0);  // prediction type : currently only type 0 has been RE'd
 
362
        put_bits(&s->pbctx, 4, prediction_type);
360
363
        put_bits(&s->pbctx, 4, s->lpc[i].lpc_quant);
361
364
 
362
365
        put_bits(&s->pbctx, 3, s->rc.rice_modifier);
363
366
        put_bits(&s->pbctx, 5, s->lpc[i].lpc_order);
364
367
        // predictor coeff. table
365
 
        for(j=0;j<s->lpc[i].lpc_order;j++) {
 
368
        for (j = 0; j < s->lpc[i].lpc_order; j++) {
366
369
            put_sbits(&s->pbctx, 16, s->lpc[i].lpc_coeff[j]);
367
370
        }
368
371
    }
369
372
 
370
373
    // apply lpc and entropy coding to audio samples
371
374
 
372
 
    for(i=0;i<s->avctx->channels;i++) {
 
375
    for (i = 0; i < s->avctx->channels; i++) {
373
376
        alac_linear_predictor(s, i);
 
377
 
 
378
        // TODO: determine when this will actually help. for now it's not used.
 
379
        if (prediction_type == 15) {
 
380
            // 2nd pass 1st order filter
 
381
            for (j = s->avctx->frame_size - 1; j > 0; j--)
 
382
                s->predictor_buf[j] -= s->predictor_buf[j - 1];
 
383
        }
 
384
 
374
385
        alac_entropy_coder(s);
375
386
    }
376
387
}
384
395
    avctx->frame_size      = DEFAULT_FRAME_SIZE;
385
396
    avctx->bits_per_coded_sample = DEFAULT_SAMPLE_SIZE;
386
397
 
387
 
    if(avctx->sample_fmt != AV_SAMPLE_FMT_S16) {
 
398
    if (avctx->sample_fmt != AV_SAMPLE_FMT_S16) {
388
399
        av_log(avctx, AV_LOG_ERROR, "only pcm_s16 input samples are supported\n");
389
400
        return -1;
390
401
    }
391
402
 
 
403
    /* TODO: Correctly implement multi-channel ALAC.
 
404
             It is similar to multi-channel AAC, in that it has a series of
 
405
             single-channel (SCE), channel-pair (CPE), and LFE elements. */
 
406
    if (avctx->channels > 2) {
 
407
        av_log(avctx, AV_LOG_ERROR, "only mono or stereo input is currently supported\n");
 
408
        return AVERROR_PATCHWELCOME;
 
409
    }
 
410
 
392
411
    // Set default compression level
393
 
    if(avctx->compression_level == FF_COMPRESSION_DEFAULT)
 
412
    if (avctx->compression_level == FF_COMPRESSION_DEFAULT)
394
413
        s->compression_level = 2;
395
414
    else
396
415
        s->compression_level = av_clip(avctx->compression_level, 0, 2);
411
430
    AV_WB8 (alac_extradata+17, avctx->bits_per_coded_sample);
412
431
    AV_WB8 (alac_extradata+21, avctx->channels);
413
432
    AV_WB32(alac_extradata+24, s->max_coded_frame_size);
414
 
    AV_WB32(alac_extradata+28, avctx->sample_rate*avctx->channels*avctx->bits_per_coded_sample); // average bitrate
 
433
    AV_WB32(alac_extradata+28,
 
434
            avctx->sample_rate * avctx->channels * avctx->bits_per_coded_sample); // average bitrate
415
435
    AV_WB32(alac_extradata+32, avctx->sample_rate);
416
436
 
417
437
    // Set relevant extradata fields
418
 
    if(s->compression_level > 0) {
 
438
    if (s->compression_level > 0) {
419
439
        AV_WB8(alac_extradata+18, s->rc.history_mult);
420
440
        AV_WB8(alac_extradata+19, s->rc.initial_history);
421
441
        AV_WB8(alac_extradata+20, s->rc.k_modifier);
422
442
    }
423
443
 
424
444
    s->min_prediction_order = DEFAULT_MIN_PRED_ORDER;
425
 
    if(avctx->min_prediction_order >= 0) {
426
 
        if(avctx->min_prediction_order < MIN_LPC_ORDER ||
 
445
    if (avctx->min_prediction_order >= 0) {
 
446
        if (avctx->min_prediction_order < MIN_LPC_ORDER ||
427
447
           avctx->min_prediction_order > ALAC_MAX_LPC_ORDER) {
428
 
            av_log(avctx, AV_LOG_ERROR, "invalid min prediction order: %d\n", avctx->min_prediction_order);
 
448
            av_log(avctx, AV_LOG_ERROR, "invalid min prediction order: %d\n",
 
449
                   avctx->min_prediction_order);
429
450
                return -1;
430
451
        }
431
452
 
433
454
    }
434
455
 
435
456
    s->max_prediction_order = DEFAULT_MAX_PRED_ORDER;
436
 
    if(avctx->max_prediction_order >= 0) {
437
 
        if(avctx->max_prediction_order < MIN_LPC_ORDER ||
438
 
           avctx->max_prediction_order > ALAC_MAX_LPC_ORDER) {
439
 
            av_log(avctx, AV_LOG_ERROR, "invalid max prediction order: %d\n", avctx->max_prediction_order);
 
457
    if (avctx->max_prediction_order >= 0) {
 
458
        if (avctx->max_prediction_order < MIN_LPC_ORDER ||
 
459
            avctx->max_prediction_order > ALAC_MAX_LPC_ORDER) {
 
460
            av_log(avctx, AV_LOG_ERROR, "invalid max prediction order: %d\n",
 
461
                   avctx->max_prediction_order);
440
462
                return -1;
441
463
        }
442
464
 
443
465
        s->max_prediction_order = avctx->max_prediction_order;
444
466
    }
445
467
 
446
 
    if(s->max_prediction_order < s->min_prediction_order) {
447
 
        av_log(avctx, AV_LOG_ERROR, "invalid prediction orders: min=%d max=%d\n",
 
468
    if (s->max_prediction_order < s->min_prediction_order) {
 
469
        av_log(avctx, AV_LOG_ERROR,
 
470
               "invalid prediction orders: min=%d max=%d\n",
448
471
               s->min_prediction_order, s->max_prediction_order);
449
472
        return -1;
450
473
    }
469
492
    PutBitContext *pb = &s->pbctx;
470
493
    int i, out_bytes, verbatim_flag = 0;
471
494
 
472
 
    if(avctx->frame_size > DEFAULT_FRAME_SIZE) {
 
495
    if (avctx->frame_size > DEFAULT_FRAME_SIZE) {
473
496
        av_log(avctx, AV_LOG_ERROR, "input frame size exceeded\n");
474
497
        return -1;
475
498
    }
476
499
 
477
 
    if(buf_size < 2*s->max_coded_frame_size) {
 
500
    if (buf_size < 2 * s->max_coded_frame_size) {
478
501
        av_log(avctx, AV_LOG_ERROR, "buffer size is too small\n");
479
502
        return -1;
480
503
    }
482
505
verbatim:
483
506
    init_put_bits(pb, frame, buf_size);
484
507
 
485
 
    if((s->compression_level == 0) || verbatim_flag) {
 
508
    if (s->compression_level == 0 || verbatim_flag) {
486
509
        // Verbatim mode
487
510
        const int16_t *samples = data;
488
511
        write_frame_header(s, 1);
489
 
        for(i=0; i<avctx->frame_size*avctx->channels; i++) {
 
512
        for (i = 0; i < avctx->frame_size * avctx->channels; i++) {
490
513
            put_sbits(pb, 16, *samples++);
491
514
        }
492
515
    } else {
499
522
    flush_put_bits(pb);
500
523
    out_bytes = put_bits_count(pb) >> 3;
501
524
 
502
 
    if(out_bytes > s->max_coded_frame_size) {
 
525
    if (out_bytes > s->max_coded_frame_size) {
503
526
        /* frame too large. use verbatim mode */
504
 
        if(verbatim_flag || (s->compression_level == 0)) {
 
527
        if (verbatim_flag || s->compression_level == 0) {
505
528
            /* still too large. must be an error. */
506
529
            av_log(avctx, AV_LOG_ERROR, "error encoding frame\n");
507
530
            return -1;
524
547
}
525
548
 
526
549
AVCodec ff_alac_encoder = {
527
 
    "alac",
528
 
    AVMEDIA_TYPE_AUDIO,
529
 
    CODEC_ID_ALAC,
530
 
    sizeof(AlacEncodeContext),
531
 
    alac_encode_init,
532
 
    alac_encode_frame,
533
 
    alac_encode_close,
 
550
    .name           = "alac",
 
551
    .type           = AVMEDIA_TYPE_AUDIO,
 
552
    .id             = CODEC_ID_ALAC,
 
553
    .priv_data_size = sizeof(AlacEncodeContext),
 
554
    .init           = alac_encode_init,
 
555
    .encode         = alac_encode_frame,
 
556
    .close          = alac_encode_close,
534
557
    .capabilities = CODEC_CAP_SMALL_LAST_FRAME,
535
 
    .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE},
 
558
    .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
 
559
                                                  AV_SAMPLE_FMT_NONE },
536
560
    .long_name = NULL_IF_CONFIG_SMALL("ALAC (Apple Lossless Audio Codec)"),
537
561
};