~ubuntu-branches/ubuntu/lucid/blender/lucid

« back to all changes in this revision

Viewing changes to source/blender/src/hddaudio.c

  • Committer: Bazaar Package Importer
  • Author(s): Chris Coulson
  • Date: 2009-08-06 22:32:19 UTC
  • mfrom: (1.2.10 upstream)
  • Revision ID: james.westby@ubuntu.com-20090806223219-8z4eej1u8levu4pz
Tags: 2.49a+dfsg-0ubuntu1
* Merge from debian unstable, remaining changes:
  - debian/control: Build-depend on python-2.6 rather than python-2.5.
  - debian/misc/*.desktop: Add Spanish translation to .desktop 
    files.
  - debian/pyversions: 2.6.
  - debian/rules: Clean *.o of source/blender/python/api2_2x/
* New upstream release (LP: #382153).
* Refreshed patches:
  - 01_sanitize_sys.patch
  - 02_tmp_in_HOME
  - 10_use_systemwide_ftgl
  - 70_portability_platform_detection
* Removed patches merged upstream:
  - 30_fix_python_syntax_warning
  - 90_ubuntu_ffmpeg_52_changes

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/**
2
 
 * $Id: hddaudio.c 17043 2008-10-12 12:00:26Z schlaile $
 
2
 * $Id: hddaudio.c 20630 2009-06-04 15:58:47Z sirdude $
3
3
 *
4
4
 * ***** BEGIN GPL LICENSE BLOCK *****
5
5
 *
33
33
#endif
34
34
 
35
35
#ifdef WITH_FFMPEG
36
 
#include <ffmpeg/avformat.h>
37
 
#include <ffmpeg/avcodec.h>
38
 
#include <ffmpeg/rational.h>
 
36
#include <libavformat/avformat.h>
 
37
#include <libavcodec/avcodec.h>
 
38
#include <libavutil/rational.h>
39
39
#if LIBAVFORMAT_VERSION_INT < (49 << 16)
40
40
#define FFMPEG_OLD_FRAME_RATE 1
41
41
#else
216
216
}
217
217
 
218
218
#ifdef WITH_FFMPEG
 
219
 
 
220
#define RESAMPLE_FILL_RATIO (7.0/4.0)
 
221
 
 
222
static void sound_hdaudio_run_resampler_seek(
 
223
        struct hdaudio * hdaudio)
 
224
{
 
225
        int in_frame_size = (long long) hdaudio->sample_rate
 
226
                * hdaudio->frame_duration / AV_TIME_BASE;
 
227
 
 
228
        hdaudio->resample_samples_in = in_frame_size * RESAMPLE_FILL_RATIO;
 
229
        hdaudio->resample_samples_written
 
230
                = audio_resample(hdaudio->resampler,
 
231
                                 hdaudio->resample_cache,
 
232
                                 hdaudio->decode_cache_zero,
 
233
                                 in_frame_size * RESAMPLE_FILL_RATIO);
 
234
}
 
235
 
 
236
static void sound_hdaudio_run_resampler_continue(
 
237
        struct hdaudio * hdaudio)
 
238
{
 
239
        int target_rate = hdaudio->target_rate;
 
240
        int target_channels = hdaudio->target_channels;
 
241
 
 
242
        int frame_size = (long long) target_rate 
 
243
                * hdaudio->frame_duration / AV_TIME_BASE;
 
244
        int in_frame_size = (long long) hdaudio->sample_rate
 
245
                * hdaudio->frame_duration / AV_TIME_BASE;
 
246
 
 
247
        int reuse_tgt = (hdaudio->resample_samples_written  
 
248
                           - frame_size) * target_channels;
 
249
        int reuse_src = (hdaudio->resample_samples_in 
 
250
                           - in_frame_size) * hdaudio->channels;
 
251
        int next_samples_in = 
 
252
                in_frame_size * RESAMPLE_FILL_RATIO 
 
253
                - reuse_src / hdaudio->channels;
 
254
 
 
255
        memmove(hdaudio->resample_cache,
 
256
                hdaudio->resample_cache + frame_size * target_channels,
 
257
                reuse_tgt * sizeof(short));
 
258
 
 
259
        hdaudio->resample_samples_written
 
260
                = audio_resample(
 
261
                        hdaudio->resampler,
 
262
                        hdaudio->resample_cache + reuse_tgt,
 
263
                        hdaudio->decode_cache_zero + reuse_src, 
 
264
                        next_samples_in)
 
265
                + reuse_tgt / target_channels;
 
266
 
 
267
        hdaudio->resample_samples_in = next_samples_in 
 
268
                + reuse_src / hdaudio->channels;
 
269
}
 
270
 
 
271
static void sound_hdaudio_init_resampler(
 
272
        struct hdaudio * hdaudio, 
 
273
        int frame_position, int target_rate, int target_channels)
 
274
{
 
275
        int frame_size = (long long) target_rate 
 
276
                * hdaudio->frame_duration / AV_TIME_BASE;
 
277
 
 
278
        if (hdaudio->resampler && 
 
279
            (hdaudio->target_rate != target_rate
 
280
             || hdaudio->target_channels != target_channels)) {
 
281
                audio_resample_close(hdaudio->resampler);
 
282
                hdaudio->resampler = 0;
 
283
        }
 
284
        if (!hdaudio->resampler) {
 
285
                hdaudio->resampler = av_audio_resample_init(
 
286
                        target_channels, hdaudio->channels,
 
287
                        target_rate, hdaudio->sample_rate,
 
288
                        SAMPLE_FMT_S16, SAMPLE_FMT_S16,
 
289
                        16, 10, 0, 0.8);
 
290
                hdaudio->target_rate = target_rate;
 
291
                hdaudio->target_channels = target_channels;
 
292
                if (hdaudio->resample_cache) {
 
293
                        MEM_freeN(hdaudio->resample_cache);
 
294
                }
 
295
                
 
296
                
 
297
                hdaudio->resample_cache = (short*) MEM_mallocN(
 
298
                        (long long) 
 
299
                        hdaudio->target_channels 
 
300
                        * frame_size * 2
 
301
                        * sizeof(short), 
 
302
                        "hdaudio resample cache");
 
303
                if (frame_position == hdaudio->frame_position ||
 
304
                    frame_position == hdaudio->frame_position + 1) {
 
305
                        sound_hdaudio_run_resampler_seek(hdaudio);
 
306
                }
 
307
        }
 
308
}
 
309
 
219
310
static void sound_hdaudio_extract_small_block(
220
311
        struct hdaudio * hdaudio, 
221
312
        short * target_buffer,
225
316
        int nb_samples /* in target */)
226
317
{
227
318
        AVPacket packet;
228
 
        int frame_position;
229
 
        int frame_size = (long long) target_rate 
230
 
                * hdaudio->frame_duration / AV_TIME_BASE;
231
 
        int in_frame_size = (long long) hdaudio->sample_rate
232
 
                * hdaudio->frame_duration / AV_TIME_BASE;
233
 
        int rate_conversion = 
 
319
        int frame_position, frame_size, in_frame_size, rate_conversion; 
 
320
        int sample_ofs;
 
321
 
 
322
        if (hdaudio == 0) return;
 
323
 
 
324
        frame_size = (long long) target_rate 
 
325
                * hdaudio->frame_duration / AV_TIME_BASE;
 
326
        in_frame_size = (long long) hdaudio->sample_rate
 
327
                * hdaudio->frame_duration / AV_TIME_BASE;
 
328
        rate_conversion = 
234
329
                (target_rate != hdaudio->sample_rate) 
235
330
                || (target_channels != hdaudio->channels);
236
 
        int sample_ofs = target_channels * (sample_position % frame_size);
 
331
        sample_ofs = target_channels * (sample_position % frame_size);
237
332
 
238
333
        frame_position = sample_position / frame_size; 
239
334
 
240
 
        if (hdaudio == 0) return;
241
 
 
242
335
        if (rate_conversion) {
243
 
                if (hdaudio->resampler && 
244
 
                    (hdaudio->target_rate != target_rate
245
 
                     || hdaudio->target_channels != target_channels)) {
246
 
                        audio_resample_close(hdaudio->resampler);
247
 
                        hdaudio->resampler = 0;
248
 
                }
249
 
                if (!hdaudio->resampler) {
250
 
                        hdaudio->resampler = audio_resample_init(
251
 
                                target_channels, hdaudio->channels,
252
 
                                target_rate, hdaudio->sample_rate);
253
 
                        hdaudio->target_rate = target_rate;
254
 
                        hdaudio->target_channels = target_channels;
255
 
                        if (hdaudio->resample_cache) {
256
 
                                MEM_freeN(hdaudio->resample_cache);
257
 
                        }
258
 
                        
259
 
 
260
 
                        hdaudio->resample_cache = (short*) MEM_mallocN(
261
 
                                (long long) 
262
 
                                hdaudio->target_channels 
263
 
                                * frame_size * 2
264
 
                                * sizeof(short), 
265
 
                                "hdaudio resample cache");
266
 
                        if (frame_position == hdaudio->frame_position) {
267
 
                                hdaudio->resample_samples_in = 
268
 
                                        in_frame_size * 7 / 4;
269
 
                                hdaudio->resample_samples_written
270
 
                                        = audio_resample(
271
 
                                                hdaudio->resampler,
272
 
                                                hdaudio->resample_cache,
273
 
                                                hdaudio->decode_cache_zero,
274
 
                                                in_frame_size * 7 / 4);
275
 
                        }
276
 
                }
 
336
                sound_hdaudio_init_resampler(
 
337
                        hdaudio, frame_position,
 
338
                        target_rate, target_channels);
277
339
        }
278
340
 
279
341
        if (frame_position == hdaudio->frame_position + 1
311
373
                                audio_pkt_size = packet.size;
312
374
 
313
375
                                while (audio_pkt_size > 0) {
314
 
                                        len = avcodec_decode_audio(
 
376
                                        data_size=AVCODEC_MAX_AUDIO_FRAME_SIZE;
 
377
                                        len = avcodec_decode_audio2(
315
378
                                                hdaudio->pCodecCtx, 
316
379
                                                hdaudio->decode_cache 
317
380
                                                + decode_pos, 
346
409
                        }
347
410
                }
348
411
 
 
412
                hdaudio->decode_pos = decode_pos;
 
413
 
349
414
                if (rate_conversion) {
350
 
                        int written = hdaudio->resample_samples_written
351
 
                                * target_channels;
352
 
                        int ofs = target_channels * frame_size;
353
 
                        int recycle = written - ofs;
354
 
                        int next_in = in_frame_size 
355
 
                                + (3.0/4.0 
356
 
                                   - (double) recycle / (double) 
357
 
                                   (frame_size * target_channels)
358
 
                                        ) * in_frame_size;
359
 
 
360
 
                        memmove(hdaudio->resample_cache,
361
 
                                hdaudio->resample_cache + ofs,
362
 
                                recycle * sizeof(short));
363
 
 
364
 
                        hdaudio->resample_samples_written
365
 
                               = audio_resample(
366
 
                                       hdaudio->resampler,
367
 
                                       hdaudio->resample_cache + recycle,
368
 
                                       hdaudio->decode_cache_zero
369
 
                                       + hdaudio->resample_samples_in
370
 
                                       * hdaudio->channels
371
 
                                       - bl_size,
372
 
                                       next_in)
373
 
                                + recycle / target_channels;
374
 
 
375
 
                        hdaudio->resample_samples_in = next_in;
 
415
                        sound_hdaudio_run_resampler_continue(hdaudio);
376
416
                }
377
 
 
378
 
                hdaudio->decode_pos = decode_pos;
379
417
        }
380
418
 
381
419
        if (frame_position != hdaudio->frame_position) { 
389
427
                        * hdaudio->frame_duration / AV_TIME_BASE;
390
428
 
391
429
                long long seek_pos;
 
430
                int decode_cache_zero_init = 0;
392
431
 
393
432
                hdaudio->frame_position = frame_position;
394
433
 
434
473
                        audio_pkt_data = packet.data;
435
474
                        audio_pkt_size = packet.size;
436
475
 
437
 
                        if (!hdaudio->decode_cache_zero 
438
 
                            && audio_pkt_size > 0) { 
 
476
                        if (!decode_cache_zero_init && audio_pkt_size > 0) { 
439
477
                                long long diff;
440
478
 
441
479
                                if (packet.pts == AV_NOPTS_VALUE) {
475
513
 
476
514
                                hdaudio->decode_cache_zero
477
515
                                        = hdaudio->decode_cache + diff;
 
516
                                decode_cache_zero_init = 1;
478
517
                        }
479
518
 
480
519
                        while (audio_pkt_size > 0) {
481
 
                                len = avcodec_decode_audio(
 
520
                                data_size=AVCODEC_MAX_AUDIO_FRAME_SIZE;
 
521
                                len = avcodec_decode_audio2(
482
522
                                        hdaudio->pCodecCtx, 
483
523
                                        hdaudio->decode_cache 
484
524
                                        + decode_pos, 
512
552
                                break;
513
553
                        }
514
554
                }
 
555
                hdaudio->decode_pos = decode_pos;
 
556
 
515
557
                if (rate_conversion) {
516
 
                        hdaudio->resample_samples_written
517
 
                                = audio_resample(hdaudio->resampler,
518
 
                                                 hdaudio->resample_cache,
519
 
                                                 hdaudio->decode_cache_zero,
520
 
                                                 in_frame_size * 7 / 4);
521
 
                        hdaudio->resample_samples_in = 
522
 
                                in_frame_size * 7 / 4;
 
558
                        sound_hdaudio_run_resampler_seek(hdaudio);
523
559
                }
524
 
                hdaudio->decode_pos = decode_pos;
525
560
        }
526
561
 
527
562
        memcpy(target_buffer, (rate_conversion 
528
563
                               ? hdaudio->resample_cache 
529
564
                               : hdaudio->decode_cache_zero) + sample_ofs, 
530
565
               nb_samples * target_channels * sizeof(short));
 
566
 
531
567
}
532
568
#endif
533
569
 
573
609
                avcodec_close(hdaudio->pCodecCtx);
574
610
                av_close_input_file(hdaudio->pFormatCtx);
575
611
                MEM_freeN (hdaudio->decode_cache);
 
612
                if (hdaudio->resampler) {
 
613
                        audio_resample_close(hdaudio->resampler);
 
614
                }
576
615
                if (hdaudio->resample_cache) {
577
616
                        MEM_freeN(hdaudio->resample_cache);
578
617
                }